File: ui_simple_sb_view.c

package info (click to toggle)
mlterm 3.9.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,340 kB
  • sloc: ansic: 154,713; sh: 5,302; cpp: 2,953; objc: 2,776; java: 2,472; makefile: 2,445; perl: 1,674; xml: 44
file content (298 lines) | stat: -rw-r--r-- 7,539 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */

#include <stdio.h>
#include <stdlib.h>

#include <pobl/bl_debug.h>
#include <pobl/bl_mem.h>

#include "ui_sb_view.h"
#include "ui_window.h"

#define TOP_MARGIN 14
#define BOTTOM_MARGIN 14
#define HEIGHT_MARGIN (TOP_MARGIN + BOTTOM_MARGIN)
#define WIDTH 13

typedef struct simple_sb_view {
  ui_sb_view_t view;

  ui_color_t black;
  ui_color_t white;
  ui_color_t gray;

  int8_t dpr;
  int8_t is_transparent;

} simple_sb_view_t;

/* --- static variables --- */

static char *arrow_up_src[] = {
  "            #",
  " -----------#",
  " -----+-----#",
  " -----#-----#",
  " ----+#+----#",
  " ----###----#",
  " ---+###+---#",
  " ---#####---#",
  " --+#####+--#",
  " --#######--#",
  " -+#######+-#",
  " -----------#",
  " -----------#",
  "#############"

};

static char *arrow_down_src[] = {
  "            #",
  " -----------#",
  " -----------#",
  " -+#######+-#",
  " --#######--#",
  " --+#####+--#",
  " ---#####---#",
  " ---+###+---#",
  " ----###----#",
  " ----+#+----#",
  " -----#-----#",
  " -----+-----#",
  " -----------#",
  "#############"

};

static char *arrow_up_dent_src[] = {
  "            #",
  " -----------#",
  " ----- -----#",
  " ----- -----#",
  " ----   ----#",
  " ----   ----#",
  " ---     ---#",
  " ---     ---#",
  " --       --#",
  " --       --#",
  " -         -#",
  " -----------#",
  " -----------#",
  "#############"

};

static char *arrow_down_dent_src[] = {
  "            #",
  " -----------#",
  " -----------#",
  " -         -#",
  " --       --#",
  " --       --#",
  " ---     ---#",
  " ---     ---#",
  " ----   ----#",
  " ----   ----#",
  " ----- -----#",
  " ----- -----#",
  " -----------#",
  "#############"

};

/* --- static functions --- */

static size_t count_char(char c, char *str) {
  char *p;

  p = str;

  while (*p == c) {
    p++;
  }

  return p - str;
}

static int draw_icon(ui_sb_view_t *view, int y, char **data, unsigned int width,
                     unsigned int height) {
  simple_sb_view_t *simple_sb = (simple_sb_view_t*)view;
  int dpr = simple_sb->dpr;
  int x_off;
  int y_off;

  if (simple_sb->is_transparent) {
    ui_window_clear(view->win, 0, y, width, height);
  }

  for (y_off = 0; y_off < height; y_off++) {
    for (x_off = 0; x_off < width;) {
      size_t count;

      count = count_char(data[y_off][x_off], data[y_off] + x_off);

      switch(data[y_off][x_off]) {
      case '-':
        if (!simple_sb->is_transparent) {
          ui_window_fill(view->win, x_off * dpr,
                         y + y_off * dpr, count * dpr, 1 * dpr);
        }
        break;

      case '#':
        ui_window_fill_with(view->win, &simple_sb->black,
                            x_off * dpr, y + y_off * dpr,
                            count * dpr, 1 * dpr);
        break;

      case '+':
        ui_window_fill_with(view->win, &simple_sb->gray,
                            x_off * dpr, y + y_off * dpr,
                            count * dpr, 1 * dpr);
        break;

      default:
        ui_window_fill_with(view->win, &simple_sb->white,
                            x_off * dpr, y + y_off * dpr,
                            count * dpr, 1 * dpr);
        break;
      }

      x_off += count;
    }
  }

  return 1;
}

static void get_geometry_hints(ui_sb_view_t *view, unsigned int *width, unsigned int *top_margin,
                               unsigned int *bottom_margin, int *up_button_y,
                               unsigned int *up_button_height, int *down_button_y,
                               unsigned int *down_button_height) {
  int dpr = ((simple_sb_view_t*)view)->dpr;

  *width = WIDTH * dpr;
  *top_margin = TOP_MARGIN * dpr;
  *bottom_margin = BOTTOM_MARGIN * dpr;
  *up_button_y = 0;
  *up_button_height = TOP_MARGIN * dpr;
  *down_button_y = -BOTTOM_MARGIN * dpr;
  *down_button_height = BOTTOM_MARGIN * dpr;
}

static void get_default_color(ui_sb_view_t *view, char **fg_color, char **bg_color) {
  *fg_color = "gray";
  *bg_color = "lightgray";
}

static void realized(ui_sb_view_t *view, Display *display, int screen, Window window, GC gc,
                     unsigned int height) {
  simple_sb_view_t *simple_sb;

  simple_sb = (simple_sb_view_t*)view;

  view->display = display;
  view->screen = screen;
  view->window = window;
  view->gc = gc;
  view->height = height;

  ui_load_rgb_xcolor(view->win->disp, &simple_sb->black, 0x00, 0x00, 0x00, 0xff);
  ui_load_rgb_xcolor(view->win->disp, &simple_sb->gray, 0x50, 0x50, 0x50, 0xff);
  ui_load_rgb_xcolor(view->win->disp, &simple_sb->white, 0xff, 0xff, 0xff, 0xff);
}

static void resized(ui_sb_view_t *view, Window window, unsigned int height) {
  view->window = window;
  view->height = height;
}

static void destroy(ui_sb_view_t *view) { free(view); }

static void draw_arrow_up_icon(ui_sb_view_t *view, int is_dent) {
  draw_icon(view, 0, is_dent ? arrow_up_dent_src : arrow_up_src,
            WIDTH, TOP_MARGIN);
}

static void draw_arrow_down_icon(ui_sb_view_t *view, int is_dent) {
  draw_icon(view, view->height - BOTTOM_MARGIN * ((simple_sb_view_t*)view)->dpr,
            is_dent ? arrow_down_dent_src : arrow_down_src,
            WIDTH, BOTTOM_MARGIN);
}

static void draw_scrollbar(ui_sb_view_t *view, int bar_top_y, unsigned int bar_height) {
  simple_sb_view_t *simple_sb = (simple_sb_view_t*)view;
  int dpr = simple_sb->dpr;

  /* drawing bar */
  if (simple_sb->is_transparent) {
    ui_window_clear(view->win, 1 * dpr, bar_top_y, (WIDTH - 1) * dpr, bar_height);
  } else {
    ui_window_fill(view->win, 1 * dpr, bar_top_y, (WIDTH - 1) * dpr, bar_height);
  }

  /* left side shade */
  ui_window_fill_with(view->win, &simple_sb->white, 0, bar_top_y, 1 * dpr, bar_height);

  /* up side shade */
  ui_window_fill_with(view->win, &simple_sb->white, 0, bar_top_y, WIDTH * dpr, 1);

  /* right side shade */
  ui_window_fill_with(view->win, &simple_sb->black, (WIDTH - 1) * dpr, bar_top_y,
                      1 * dpr, bar_height);

  /* down side shade */
  ui_window_fill_with(view->win, &simple_sb->black, 1 * dpr, bar_top_y + bar_height - 1,
                      (WIDTH - 1) * dpr, 1);
}

static void draw_background(ui_sb_view_t *view, int y, unsigned int height) {
  ui_window_clear(view->win, 0, y, WIDTH * ((simple_sb_view_t*)view)->dpr, height);
}

static void draw_up_button(ui_sb_view_t *view, int is_pressed) {
  draw_arrow_up_icon(view, is_pressed);
}

static void draw_down_button(ui_sb_view_t *view, int is_pressed) {
  draw_arrow_down_icon(view, is_pressed);
}

/* --- global functions --- */

ui_sb_view_t *ui_simple_sb_view_new(int dpr) {
  simple_sb_view_t *simple_sb;

  if ((simple_sb = calloc(1, sizeof(simple_sb_view_t))) == NULL) {
    return NULL;
  }

  simple_sb->view.version = 1;

  simple_sb->view.get_geometry_hints = get_geometry_hints;
  simple_sb->view.get_default_color = get_default_color;
  simple_sb->view.realized = realized;
  simple_sb->view.resized = resized;
  simple_sb->view.destroy = destroy;

  simple_sb->view.draw_scrollbar = draw_scrollbar;
  simple_sb->view.draw_background = draw_background;
  simple_sb->view.draw_up_button = draw_up_button;
  simple_sb->view.draw_down_button = draw_down_button;

  simple_sb->dpr = dpr;

  return &simple_sb->view;
}

ui_sb_view_t *ui_simple_transparent_sb_view_new(int dpr) {
  simple_sb_view_t *simple_sb;

  if ((simple_sb = (simple_sb_view_t*)ui_simple_sb_view_new(dpr)) == NULL) {
    return NULL;
  }

  simple_sb->is_transparent = 1;

  return &simple_sb->view;
}