1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : DI_view_matrix.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #include "di_view_matrix.hxx" |
---|
12 | |
---|
13 | #include <aw_awars.hxx> |
---|
14 | #include <aw_preset.hxx> |
---|
15 | #include <aw_msg.hxx> |
---|
16 | #include <aw_root.hxx> |
---|
17 | |
---|
18 | #include <awt_canvas.hxx> |
---|
19 | |
---|
20 | #include <arb_algo.h> |
---|
21 | #include <awt_config_manager.hxx> |
---|
22 | |
---|
23 | #define AWAR_MATRIX "matrix/" |
---|
24 | #define AWAR_MATRIX_PADDINGX AWAR_MATRIX "paddingx" |
---|
25 | #define AWAR_MATRIX_PADDINGY AWAR_MATRIX "paddingy" |
---|
26 | #define AWAR_MATRIX_SHOWZERO AWAR_MATRIX "show_zero" |
---|
27 | #define AWAR_MATRIX_DIGITS AWAR_MATRIX "show_digits" |
---|
28 | #define AWAR_MATRIX_NAMECHARS_TOP AWAR_MATRIX "namechars_top" |
---|
29 | #define AWAR_MATRIX_NAMECHARS_LEFT AWAR_MATRIX "namechars_left" |
---|
30 | |
---|
31 | static void vertical_change_cb (AW_window *aww, MatrixDisplay *disp) { disp->monitor_vertical_scroll_cb(aww); } |
---|
32 | static void horizontal_change_cb(AW_window *aww, MatrixDisplay *disp) { disp->monitor_horizontal_scroll_cb(aww); } |
---|
33 | |
---|
34 | static void redisplay_needed(UNFIXED, MatrixDisplay *disp) { |
---|
35 | disp->mark(MatrixDisplay::NEED_CLEAR); |
---|
36 | disp->update_display(); |
---|
37 | } |
---|
38 | |
---|
39 | static void reinit_needed(UNFIXED, MatrixDisplay *disp) { |
---|
40 | disp->mark(MatrixDisplay::NEED_SETUP); |
---|
41 | disp->update_display(); |
---|
42 | } |
---|
43 | |
---|
44 | static void resize_needed(UNFIXED, MatrixDisplay *disp) { |
---|
45 | disp->mark(MatrixDisplay::NEED_SETUP); // @@@ why not NEED_RESIZE? |
---|
46 | disp->update_display(); |
---|
47 | } |
---|
48 | |
---|
49 | static void gc_changed_cb(GcChange whatChanged, MatrixDisplay *disp) { |
---|
50 | switch (whatChanged) { |
---|
51 | case GC_COLOR_GROUP_USE_CHANGED: |
---|
52 | di_assert(0); // not used atm |
---|
53 | FALLTHROUGH; // in NDEBUG |
---|
54 | case GC_COLOR_CHANGED: |
---|
55 | redisplay_needed(NULp, disp); |
---|
56 | break; |
---|
57 | case GC_FONT_CHANGED: |
---|
58 | resize_needed(NULp, disp); |
---|
59 | break; |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | void MatrixDisplay::setup() { |
---|
64 | DI_MATRIX *m = get_matrix(); |
---|
65 | AW_root *awr = awm->get_root(); |
---|
66 | |
---|
67 | leadZero = awr->awar(AWAR_MATRIX_SHOWZERO)->read_int(); |
---|
68 | digits = awr->awar(AWAR_MATRIX_DIGITS)->read_int(); |
---|
69 | |
---|
70 | sprintf(format_string, "%%%i.%if", digits+2, digits); |
---|
71 | |
---|
72 | // calculate cell width and height |
---|
73 | { |
---|
74 | cell_width = 0; |
---|
75 | cell_height = 0; |
---|
76 | |
---|
77 | int max_chars[DI_G_LAST+1]; |
---|
78 | memset(max_chars, 0, sizeof(*max_chars)*(DI_G_LAST+1)); |
---|
79 | |
---|
80 | max_chars[DI_G_STANDARD] = leadZero+2; // standard cell contains "0.0", "1.0" or "---" |
---|
81 | max_chars[DI_G_BELOW_DIST] = leadZero+1+digits; // size of numeric distance |
---|
82 | max_chars[DI_G_ABOVE_DIST] = max_chars[DI_G_BELOW_DIST]; |
---|
83 | max_chars[DI_G_NAMES] = awr->awar(AWAR_MATRIX_NAMECHARS_TOP)->read_int(); |
---|
84 | |
---|
85 | for (int igc=DI_G_STANDARD; igc<=DI_G_LAST; ++igc) { |
---|
86 | DI_gc gc = DI_gc(igc); |
---|
87 | if (max_chars[gc]) { |
---|
88 | const AW_font_limits& lim = device->get_font_limits(gc, 0); |
---|
89 | |
---|
90 | cell_width = std::max(cell_width, lim.width*max_chars[gc]); |
---|
91 | cell_height = std::max(cell_height, int(lim.get_height())); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | { |
---|
96 | // ensure cell-dimensions are > 0 |
---|
97 | AW_awar *pad_awarx = awr->awar(AWAR_MATRIX_PADDINGX); |
---|
98 | AW_awar *pad_awary = awr->awar(AWAR_MATRIX_PADDINGY); |
---|
99 | |
---|
100 | cell_paddx = pad_awarx->read_int(); |
---|
101 | cell_paddy = pad_awary->read_int(); |
---|
102 | |
---|
103 | if (cell_paddx<0 && -cell_paddx >= cell_width) { |
---|
104 | cell_paddx = -cell_width+1; |
---|
105 | pad_awarx->write_int(cell_paddx); |
---|
106 | } |
---|
107 | if (cell_paddy<0 && -cell_paddy >= cell_height) { |
---|
108 | cell_paddy = -cell_height+1; |
---|
109 | pad_awary->write_int(cell_paddy); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | cell_width += cell_paddx; |
---|
114 | cell_height += cell_paddy; |
---|
115 | } |
---|
116 | |
---|
117 | { |
---|
118 | const AW_font_limits& lim = device->get_font_limits(DI_G_NAMES, 0); |
---|
119 | |
---|
120 | off_dx = awr->awar(AWAR_MATRIX_NAMECHARS_LEFT)->read_int() * lim.width + 1 + cell_paddx; |
---|
121 | off_dy = lim.get_height() + cell_height; // off_dy corresponds to "lower" y of cell |
---|
122 | } |
---|
123 | |
---|
124 | if (m) { |
---|
125 | total_cells_horiz=m->nentries; |
---|
126 | total_cells_vert=m->nentries; |
---|
127 | } |
---|
128 | set_scrollbar_steps(cell_width, cell_height, 50, 50); |
---|
129 | |
---|
130 | mark(NEED_RESIZE); |
---|
131 | } |
---|
132 | |
---|
133 | void MatrixDisplay::adapt_to_canvas_size() { |
---|
134 | const AW_font_limits& lim = device->get_font_limits(DI_G_STANDARD, 0); |
---|
135 | |
---|
136 | DI_MATRIX *m = get_matrix(); |
---|
137 | long n = 0; |
---|
138 | |
---|
139 | if (m) n = m->nentries; |
---|
140 | |
---|
141 | const AW_screen_area& squ = device->get_area_size(); |
---|
142 | screen_width = squ.r-squ.l; |
---|
143 | screen_height = squ.b-squ.t; |
---|
144 | |
---|
145 | AW_screen_area rect; // @@@ used uninitialized if !m |
---|
146 | if (m) { |
---|
147 | long horiz_paint_size = (squ.r-lim.width-off_dx)/cell_width; |
---|
148 | long vert_paint_size = (squ.b-off_dy)/cell_height; |
---|
149 | |
---|
150 | horiz_page_size = (n > horiz_paint_size) ? horiz_paint_size : n; |
---|
151 | vert_page_size = (n > vert_paint_size) ? vert_paint_size : n; |
---|
152 | |
---|
153 | rect.l = 0; |
---|
154 | rect.t = 0; |
---|
155 | rect.r = (int)((n-horiz_page_size)*cell_width+squ.r); |
---|
156 | rect.b = (int)((n-vert_page_size)*cell_height+squ.b); |
---|
157 | } |
---|
158 | else { |
---|
159 | horiz_page_size = 0; |
---|
160 | vert_page_size = 0; |
---|
161 | } |
---|
162 | |
---|
163 | horiz_page_start = 0; |
---|
164 | horiz_last_view_start = 0; |
---|
165 | vert_page_start = 0; |
---|
166 | vert_last_view_start = 0; |
---|
167 | |
---|
168 | device->reset(); // clip_size == device_size |
---|
169 | device->clear(-1); |
---|
170 | device->set_right_clip_border((int)(off_dx+cell_width*horiz_page_size)); |
---|
171 | device->reset(); // reset shift_x and shift_y |
---|
172 | awm->set_vertical_scrollbar_position(0); |
---|
173 | awm->set_horizontal_scrollbar_position(0); |
---|
174 | awm->tell_scrolled_picture_size(rect); |
---|
175 | awm->calculate_scrollbars(); |
---|
176 | |
---|
177 | mark(NEED_CLEAR); |
---|
178 | } |
---|
179 | |
---|
180 | enum ClickAction { |
---|
181 | CLICK_SELECT_SPECIES = 1, |
---|
182 | CLICK_SET_MINMAX, |
---|
183 | }; |
---|
184 | |
---|
185 | #define MINMAX_GRANULARITY 10000L |
---|
186 | |
---|
187 | void MatrixDisplay::scroll_to(int sxpos, int sypos) { |
---|
188 | sxpos = force_in_range(0, sxpos, int(awm->get_scrolled_picture_width())-screen_width); |
---|
189 | sypos = force_in_range(0, sypos, int(awm->get_scrolled_picture_height())-screen_height); |
---|
190 | |
---|
191 | awm->set_horizontal_scrollbar_position(sxpos); |
---|
192 | awm->set_vertical_scrollbar_position(sypos); |
---|
193 | |
---|
194 | monitor_vertical_scroll_cb(awm); |
---|
195 | monitor_horizontal_scroll_cb(awm); |
---|
196 | } |
---|
197 | |
---|
198 | void MatrixDisplay::scroll_cells(int cells_x, int cells_y) { |
---|
199 | scroll_to(awm->slider_pos_horizontal + cells_x*cell_width, |
---|
200 | awm->slider_pos_vertical + cells_y*cell_height); |
---|
201 | } |
---|
202 | |
---|
203 | void MatrixDisplay::handle_move(AW_event& event) { |
---|
204 | static int clickx, clicky; // original click pos |
---|
205 | static int startx, starty; // original slider position |
---|
206 | |
---|
207 | if (event.type == AW_Mouse_Press) { |
---|
208 | clickx = event.x; |
---|
209 | clicky = event.y; |
---|
210 | |
---|
211 | startx = awm->slider_pos_horizontal; |
---|
212 | starty = awm->slider_pos_vertical; |
---|
213 | } |
---|
214 | else if (event.type == AW_Mouse_Drag || event.type == AW_Mouse_Release) { |
---|
215 | int x_screen_diff = clickx - event.x; |
---|
216 | int y_screen_diff = clicky - event.y; |
---|
217 | |
---|
218 | scroll_to(startx + x_screen_diff, starty + y_screen_diff); |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | static void motion_cb(AW_window *aww, MatrixDisplay *disp) { |
---|
223 | AW_event event; |
---|
224 | aww->get_event(&event); |
---|
225 | |
---|
226 | if (event.button == AW_BUTTON_MIDDLE) { |
---|
227 | disp->handle_move(event); |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | static void input_cb(AW_window *aww, MatrixDisplay *disp) { |
---|
232 | AW_event event; |
---|
233 | aww->get_event(&event); |
---|
234 | |
---|
235 | if (event.button == AW_WHEEL_UP || event.button == AW_WHEEL_DOWN) { |
---|
236 | if (event.type == AW_Mouse_Press) { |
---|
237 | bool horizontal = event.keymodifier & AW_KEYMODE_ALT; |
---|
238 | int direction = event.button == AW_WHEEL_UP ? -1 : 1; |
---|
239 | disp->scroll_cells(horizontal*direction, !horizontal*direction); |
---|
240 | } |
---|
241 | } |
---|
242 | else if (event.button == AW_BUTTON_MIDDLE) { |
---|
243 | disp->handle_move(event); |
---|
244 | } |
---|
245 | else { |
---|
246 | AW_device_click *click_device = aww->get_click_device(AW_MIDDLE_AREA, event.x, event.y, AWT_CATCH); |
---|
247 | |
---|
248 | click_device->set_filter(AW_CLICK); |
---|
249 | click_device->reset(); |
---|
250 | |
---|
251 | { |
---|
252 | AW_device *oldMatrixDevice = disp->device; |
---|
253 | |
---|
254 | disp->device = click_device; |
---|
255 | disp->update_display(); // detect clicked element |
---|
256 | disp->device = oldMatrixDevice; |
---|
257 | } |
---|
258 | |
---|
259 | if (event.type == AW_Mouse_Press) { |
---|
260 | AWT_graphic_event gevent(AWT_MODE_NONE, event, false, click_device); |
---|
261 | const AW_clicked_element *clicked = gevent.best_click(); |
---|
262 | |
---|
263 | if (clicked) { |
---|
264 | ClickAction action = static_cast<ClickAction>(clicked->cd1()); |
---|
265 | |
---|
266 | if (action == CLICK_SELECT_SPECIES) { |
---|
267 | size_t idx = size_t(clicked->cd2()); |
---|
268 | DI_MATRIX *matrix = disp->get_matrix(); |
---|
269 | if (idx >= matrix->nentries) { |
---|
270 | aw_message(GBS_global_string("Illegal idx %zi [allowed: 0-%zi]", idx, matrix->nentries)); |
---|
271 | } |
---|
272 | else { |
---|
273 | DI_ENTRY *entry = matrix->entries[idx]; |
---|
274 | const char *species_name = entry->name; |
---|
275 | |
---|
276 | aww->get_root()->awar(AWAR_SPECIES_NAME)->write_string(species_name); |
---|
277 | } |
---|
278 | } |
---|
279 | else if (action == CLICK_SET_MINMAX) { |
---|
280 | AW_root *aw_root = aww->get_root(); |
---|
281 | AW_awar *awar_bound = NULp; |
---|
282 | |
---|
283 | switch (event.button) { |
---|
284 | case AW_BUTTON_LEFT: awar_bound = aw_root->awar(AWAR_DIST_MIN_DIST); break; |
---|
285 | case AW_BUTTON_RIGHT: awar_bound = aw_root->awar(AWAR_DIST_MAX_DIST); break; |
---|
286 | default: break; |
---|
287 | } |
---|
288 | |
---|
289 | if (awar_bound) { |
---|
290 | float val = float(clicked->cd2())/MINMAX_GRANULARITY; |
---|
291 | awar_bound->write_float(val); |
---|
292 | } |
---|
293 | } |
---|
294 | } |
---|
295 | } |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | void MatrixDisplay::draw() { |
---|
300 | // draw matrix |
---|
301 | |
---|
302 | if (!device) return; |
---|
303 | |
---|
304 | long x, y, xpos, ypos; |
---|
305 | |
---|
306 | DI_MATRIX *m = get_matrix(); |
---|
307 | if (!autopop(m)) return; |
---|
308 | |
---|
309 | GB_transaction ta(gb_main); |
---|
310 | |
---|
311 | if (beforeUpdate&NEED_CLEAR) device->clear(-1); |
---|
312 | device->set_offset(AW::Vector(off_dx, off_dy)); |
---|
313 | xpos = 0; |
---|
314 | |
---|
315 | char *selSpecies = NULp; |
---|
316 | if (awm) selSpecies = awm->get_root()->awar(AWAR_SPECIES_NAME)->read_string(); |
---|
317 | |
---|
318 | int name_display_width_top; |
---|
319 | int name_display_width_left; |
---|
320 | { |
---|
321 | const AW_font_limits& lim = device->get_font_limits(DI_G_NAMES, 0); |
---|
322 | |
---|
323 | name_display_width_top = (cell_width-1)/lim.width; |
---|
324 | name_display_width_left = (off_dx-1)/lim.width; |
---|
325 | } |
---|
326 | |
---|
327 | int BUFLEN = std::max(200, std::max(name_display_width_left, name_display_width_top)); |
---|
328 | char buf[BUFLEN]; |
---|
329 | |
---|
330 | int sel_x_pos = -1; |
---|
331 | |
---|
332 | // device->set_line_attributes(DI_G_RULER, 1, AW_SOLID); // @@@ try AW_DOTTED here (need merges from dev!) |
---|
333 | |
---|
334 | for (x = horiz_page_start; |
---|
335 | x < (horiz_page_start + horiz_page_size) && (x < total_cells_horiz); |
---|
336 | x++) |
---|
337 | { |
---|
338 | ypos = 0; |
---|
339 | for (y = vert_page_start; |
---|
340 | y < (vert_page_start + vert_page_size) && (y < total_cells_vert); |
---|
341 | y++) |
---|
342 | { |
---|
343 | bool is_identity = (x == y); |
---|
344 | |
---|
345 | // lower(!) left corner of cell: |
---|
346 | AW_pos cellx = xpos*cell_width; |
---|
347 | AW_pos celly = ypos*cell_height; |
---|
348 | |
---|
349 | if (is_identity) { |
---|
350 | device->text(DI_G_STANDARD, "---"+(1-leadZero), cellx, celly); |
---|
351 | } |
---|
352 | else { |
---|
353 | double val2 = m->matrix->get(x, y); |
---|
354 | AW_click_cd cd(device, CLICK_SET_MINMAX, val2*MINMAX_GRANULARITY+1); |
---|
355 | |
---|
356 | if (val2>=min_view_dist && val2<=max_view_dist) { // display ruler |
---|
357 | int maxw = cell_width-cell_paddx; |
---|
358 | |
---|
359 | int h = cell_height - cell_paddy-1; |
---|
360 | |
---|
361 | int hbox, hruler; |
---|
362 | if (cell_paddy >= 0) { |
---|
363 | hbox = h*2/3; |
---|
364 | hruler = h-hbox; |
---|
365 | } |
---|
366 | else { |
---|
367 | hbox = h; |
---|
368 | hruler = 0; |
---|
369 | } |
---|
370 | |
---|
371 | int y1 = celly - h; |
---|
372 | int y2 = y1+hbox; |
---|
373 | int y3 = y2+hruler/2; |
---|
374 | int y4 = y2+hruler; |
---|
375 | int x2 = cellx; |
---|
376 | |
---|
377 | double len = ((val2-min_view_dist)/(max_view_dist-min_view_dist)) * maxw; |
---|
378 | if (len >= 0) { |
---|
379 | device->box(DI_G_RULER_DISPLAY, AW::FillStyle::SOLID, x2, y1, int(len+0.5), hbox); |
---|
380 | } |
---|
381 | else { |
---|
382 | device->text(DI_G_STANDARD, "???", cellx, celly); |
---|
383 | } |
---|
384 | |
---|
385 | if (hruler) { // do not paint ruler if cell_paddy is negative |
---|
386 | double v; |
---|
387 | int cnt; |
---|
388 | int maxx = x2+maxw-1; |
---|
389 | for (v = x2, cnt = 0; v < x2 + maxw; v += maxw * .24999, ++cnt) { |
---|
390 | int xr = std::min(int(v+0.5), maxx); |
---|
391 | device->line(DI_G_RULER, xr, (cnt&1) ? y3 : y2, xr, y4); |
---|
392 | } |
---|
393 | device->line(DI_G_RULER, x2, y4, maxx, y4); |
---|
394 | } |
---|
395 | } |
---|
396 | else { |
---|
397 | DI_gc gc; |
---|
398 | if (val2 == 0.0) { |
---|
399 | strcpy(buf, "0.0"); |
---|
400 | gc = DI_G_STANDARD; |
---|
401 | } |
---|
402 | else if (val2 == 1.0) { |
---|
403 | strcpy(buf, leadZero ? "1.0" : " 1"); |
---|
404 | gc = DI_G_STANDARD; |
---|
405 | } |
---|
406 | else { |
---|
407 | sprintf(buf, format_string, val2); |
---|
408 | gc = val2<min_view_dist ? DI_G_BELOW_DIST : DI_G_ABOVE_DIST; |
---|
409 | } |
---|
410 | device->text(gc, leadZero ? buf : buf+1, cellx, celly); |
---|
411 | } |
---|
412 | } |
---|
413 | ypos++; |
---|
414 | } |
---|
415 | |
---|
416 | // display horizontal (top) speciesnames: |
---|
417 | strcpy(buf, m->entries[x]->name); |
---|
418 | if (selSpecies && strcmp(buf, selSpecies) == 0) sel_x_pos = xpos; // remember x-position for selected species |
---|
419 | buf[name_display_width_top] = 0; // cut group-names if too long |
---|
420 | |
---|
421 | AW_click_cd cd(device, CLICK_SELECT_SPECIES, x); |
---|
422 | device->text(DI_G_NAMES, buf, xpos * cell_width, cell_height - off_dy); |
---|
423 | |
---|
424 | xpos++; |
---|
425 | } |
---|
426 | |
---|
427 | device->set_offset(AW::Vector(off_dx, 0)); |
---|
428 | |
---|
429 | AW::Rectangle area(device->get_area_size(), AW::INCLUSIVE_OUTLINE); |
---|
430 | |
---|
431 | // highlight selected species (vertically) |
---|
432 | if (sel_x_pos != -1) { |
---|
433 | AW_pos linex1 = sel_x_pos*cell_width - cell_paddx/2-1; |
---|
434 | AW_pos linex2 = linex1+cell_width; |
---|
435 | AW_pos height = area.height(); |
---|
436 | device->line(DI_G_STANDARD, linex1, 0, linex1, height); |
---|
437 | device->line(DI_G_STANDARD, linex2, 0, linex2, height); |
---|
438 | } |
---|
439 | |
---|
440 | device->set_offset(AW::Vector(0, off_dy)); |
---|
441 | |
---|
442 | // display vertical (left) speciesnames |
---|
443 | ypos = 0; |
---|
444 | int sel_y_pos = -1; |
---|
445 | for (y = vert_page_start; y < vert_page_start + vert_page_size; y++) { |
---|
446 | strcpy(buf, m->entries[y]->name); |
---|
447 | if (selSpecies && strcmp(buf, selSpecies) == 0) sel_y_pos = ypos; // remember x-position for selected species |
---|
448 | buf[name_display_width_left] = 0; // cut group-names if too long |
---|
449 | AW_click_cd cd(device, CLICK_SELECT_SPECIES, y); |
---|
450 | device->text(DI_G_NAMES, buf, 0, ypos * cell_height); |
---|
451 | ypos++; |
---|
452 | } |
---|
453 | |
---|
454 | // highlight selected species (horizontally) |
---|
455 | if (sel_y_pos != -1) { |
---|
456 | AW_pos liney2 = sel_y_pos*cell_height + cell_paddy/2+1; |
---|
457 | AW_pos liney1 = liney2-cell_height; |
---|
458 | AW_pos width = area.width(); |
---|
459 | device->line(DI_G_STANDARD, 0, liney1, width, liney1); |
---|
460 | device->line(DI_G_STANDARD, 0, liney2, width, liney2); |
---|
461 | } |
---|
462 | |
---|
463 | free(selSpecies); |
---|
464 | |
---|
465 | device->set_offset(AW::Vector(0, 0)); |
---|
466 | #undef BUFLEN |
---|
467 | } |
---|
468 | |
---|
469 | void MatrixDisplay::set_scrollbar_steps(long width_h, long width_v, long page_h, long page_v) { |
---|
470 | awm->window_local_awar("scroll_width_horizontal")->write_int(width_h); |
---|
471 | awm->window_local_awar("scroll_width_vertical")->write_int(width_v); |
---|
472 | awm->window_local_awar("horizontal_page_increment")->write_int(page_h); |
---|
473 | awm->window_local_awar("vertical_page_increment")->write_int(page_v); |
---|
474 | } |
---|
475 | |
---|
476 | // @@@ test scrolling again with fixed box_impl() |
---|
477 | |
---|
478 | void MatrixDisplay::monitor_vertical_scroll_cb(AW_window *aww) { // draw area |
---|
479 | if (!device) return; |
---|
480 | |
---|
481 | int old_vert_page_start = vert_page_start; |
---|
482 | |
---|
483 | vert_last_view_start = aww->slider_pos_vertical; |
---|
484 | vert_page_start = aww->slider_pos_vertical/cell_height; |
---|
485 | |
---|
486 | int diff = vert_page_start-old_vert_page_start; // amount of rows to scroll |
---|
487 | if (diff) { |
---|
488 | int diff_pix = abs(diff)*cell_height; |
---|
489 | int top_y = off_dy-cell_height; |
---|
490 | int keep_cells = vert_page_size-abs(diff); |
---|
491 | int keep_pix = keep_cells*cell_height; |
---|
492 | |
---|
493 | if (diff>0 && diff<vert_page_size) { // scroll some positions up |
---|
494 | device->move_region(0, top_y+diff_pix, screen_width, keep_pix, 0, top_y); |
---|
495 | device->clear_part (0, top_y+keep_pix, screen_width, diff_pix, AW_SCREEN); |
---|
496 | device->push_clip_scale(); |
---|
497 | device->set_top_clip_border(top_y+keep_pix, true); |
---|
498 | } |
---|
499 | else if (diff>-vert_page_size && diff<0) { // scroll some positions down |
---|
500 | device->move_region(0, top_y, screen_width, keep_pix, 0, top_y+diff_pix); |
---|
501 | device->clear_part (0, top_y, screen_width, diff_pix, AW_SCREEN); |
---|
502 | device->push_clip_scale(); |
---|
503 | device->set_bottom_clip_border(top_y+diff_pix, true); |
---|
504 | } |
---|
505 | else { // repaint |
---|
506 | device->push_clip_scale(); |
---|
507 | mark(NEED_CLEAR); |
---|
508 | } |
---|
509 | |
---|
510 | update_display(); |
---|
511 | device->pop_clip_scale(); |
---|
512 | } |
---|
513 | } |
---|
514 | |
---|
515 | void MatrixDisplay::monitor_horizontal_scroll_cb(AW_window *aww) { // draw area |
---|
516 | if (!device) return; |
---|
517 | |
---|
518 | int old_horiz_page_start = horiz_page_start; |
---|
519 | |
---|
520 | horiz_last_view_start = aww->slider_pos_horizontal; |
---|
521 | horiz_page_start = aww->slider_pos_horizontal/cell_width; |
---|
522 | |
---|
523 | int diff = horiz_page_start-old_horiz_page_start; // amount of columns to scroll |
---|
524 | |
---|
525 | if (diff) { |
---|
526 | int diff_pix = abs(diff)*cell_width; |
---|
527 | int keep_cells = horiz_page_size-abs(diff); |
---|
528 | int keep_pix = keep_cells*cell_width; |
---|
529 | |
---|
530 | if (diff>0 && diff<horiz_page_size) { // scroll some positions left |
---|
531 | device->move_region(off_dx+diff_pix, 0, keep_pix, screen_height, off_dx, 0); |
---|
532 | device->clear_part (off_dx+keep_pix, 0, diff_pix, screen_height, AW_SCREEN); |
---|
533 | device->push_clip_scale(); |
---|
534 | device->set_left_clip_border(keep_pix, true); |
---|
535 | } |
---|
536 | else if (diff>-horiz_page_size && diff<0) { // scroll some positions right |
---|
537 | device->move_region(off_dx, 0, keep_pix, screen_height, off_dx+diff_pix, 0); |
---|
538 | device->clear_part (off_dx, 0, diff_pix, screen_height, AW_SCREEN); |
---|
539 | device->push_clip_scale(); |
---|
540 | device->set_right_clip_border(off_dx+diff_pix, true); |
---|
541 | } |
---|
542 | else { // repaint |
---|
543 | device->push_clip_scale(); |
---|
544 | mark(NEED_CLEAR); |
---|
545 | } |
---|
546 | |
---|
547 | update_display(); |
---|
548 | device->pop_clip_scale(); |
---|
549 | } |
---|
550 | } |
---|
551 | |
---|
552 | static bool update_display_on_dist_change = true; |
---|
553 | |
---|
554 | static void di_view_set_max_dist(AW_window *aww, int max_dist) { |
---|
555 | AW_root *aw_root = aww->get_root(); |
---|
556 | { |
---|
557 | LocallyModify<bool> flag(update_display_on_dist_change, false); |
---|
558 | aw_root->awar(AWAR_DIST_MIN_DIST)->write_float(0.0); |
---|
559 | } |
---|
560 | aw_root->awar(AWAR_DIST_MAX_DIST)->write_float(max_dist*0.01); |
---|
561 | } |
---|
562 | |
---|
563 | static void di_view_set_distances(AW_root *awr, int setmax, MatrixDisplay *disp) { |
---|
564 | // cl_dmatrix: 0 -> set min and fix max, 1 -> set max and fix min, 2 -> set both |
---|
565 | float max_dist = awr->awar(AWAR_DIST_MAX_DIST)->read_float(); |
---|
566 | float min_dist = awr->awar(AWAR_DIST_MIN_DIST)->read_float(); |
---|
567 | |
---|
568 | { |
---|
569 | LocallyModify<bool> flag(update_display_on_dist_change, false); |
---|
570 | |
---|
571 | switch (setmax) { |
---|
572 | case 2: // both |
---|
573 | disp->set_slider_max(max_dist); |
---|
574 | // fall-through |
---|
575 | case 0: // set min and fix max |
---|
576 | disp->set_slider_min(min_dist); |
---|
577 | if (min_dist>max_dist) awr->awar(AWAR_DIST_MAX_DIST)->write_float(min_dist); |
---|
578 | break; |
---|
579 | case 1: // set max and fix min |
---|
580 | disp->set_slider_max(max_dist); |
---|
581 | if (min_dist>max_dist) awr->awar(AWAR_DIST_MIN_DIST)->write_float(max_dist); |
---|
582 | break; |
---|
583 | |
---|
584 | default: |
---|
585 | di_assert(0); |
---|
586 | break; |
---|
587 | } |
---|
588 | } |
---|
589 | if (update_display_on_dist_change) { |
---|
590 | disp->mark(MatrixDisplay::NEED_CLEAR); |
---|
591 | disp->update_display(); |
---|
592 | } |
---|
593 | } |
---|
594 | |
---|
595 | static void di_bind_dist_awars(AW_root *aw_root, MatrixDisplay *disp) { |
---|
596 | aw_root->awar_float(AWAR_DIST_MIN_DIST)->add_callback(makeRootCallback(di_view_set_distances, 0, disp)); |
---|
597 | aw_root->awar_float(AWAR_DIST_MAX_DIST)->add_callback(makeRootCallback(di_view_set_distances, 1, disp)); |
---|
598 | } |
---|
599 | |
---|
600 | static void create_matrix_awars(AW_root *awr, MatrixDisplay *disp) { |
---|
601 | RootCallback reinit_needed_cb = makeRootCallback(reinit_needed, disp); |
---|
602 | |
---|
603 | awr->awar_int(AWAR_MATRIX_SHOWZERO, 1) ->add_callback(reinit_needed_cb); |
---|
604 | awr->awar_int(AWAR_MATRIX_PADDINGX, 4) ->set_minmax(-10, 10)->add_callback(reinit_needed_cb); |
---|
605 | awr->awar_int(AWAR_MATRIX_PADDINGY, 4) ->set_minmax(-10, 10)->add_callback(reinit_needed_cb); |
---|
606 | awr->awar_int(AWAR_MATRIX_DIGITS, 4) ->set_minmax(0, 10) ->add_callback(reinit_needed_cb); |
---|
607 | awr->awar_int(AWAR_MATRIX_NAMECHARS_TOP, 8) ->set_minmax(0, 10) ->add_callback(reinit_needed_cb); |
---|
608 | awr->awar_int(AWAR_MATRIX_NAMECHARS_LEFT, 10)->set_minmax(0, 10) ->add_callback(reinit_needed_cb); |
---|
609 | } |
---|
610 | |
---|
611 | static AWT_config_mapping_def matrixConfigMapping[] = { |
---|
612 | { AWAR_MATRIX_PADDINGX, "paddingx" }, |
---|
613 | { AWAR_MATRIX_PADDINGY, "paddingy" }, |
---|
614 | { AWAR_MATRIX_SHOWZERO, "showzero" }, |
---|
615 | { AWAR_MATRIX_DIGITS, "precision" }, |
---|
616 | { AWAR_MATRIX_NAMECHARS_TOP, "namechars_top" }, |
---|
617 | { AWAR_MATRIX_NAMECHARS_LEFT, "namechars_left" }, |
---|
618 | |
---|
619 | { NULp, NULp } |
---|
620 | }; |
---|
621 | |
---|
622 | static AWT_predefined_config predefinedMatrixConfig[] = { |
---|
623 | { "*compact", "Compact matrix view\n- use with fontsize=8\n- use without correction only (hides leading digits)", "namechars_left='10';namechars_top='3';paddingx='-3';paddingy='-2';precision='2';showzero='0'" }, |
---|
624 | { NULp, NULp, NULp } |
---|
625 | }; |
---|
626 | |
---|
627 | static AW_window *create_matrix_settings_window(AW_root *awr) { |
---|
628 | AW_window_simple *aws = new AW_window_simple; |
---|
629 | aws->init(awr, "MATRIX_SETTINGS", "Matrix settings"); |
---|
630 | |
---|
631 | const int FIELDWIDTH = 3; |
---|
632 | const int SCALERWIDTH = 200; |
---|
633 | |
---|
634 | aws->auto_space(10, 10); |
---|
635 | |
---|
636 | aws->callback(AW_POPDOWN); |
---|
637 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
638 | |
---|
639 | aws->callback(makeHelpCallback("matrix_settings.hlp")); |
---|
640 | aws->create_button("HELP", "HELP"); |
---|
641 | |
---|
642 | AWT_insert_config_manager(aws, AW_ROOT_DEFAULT, "matrix_settings", matrixConfigMapping, NULp, predefinedMatrixConfig); |
---|
643 | |
---|
644 | aws->label_length(21); |
---|
645 | |
---|
646 | aws->at_newline(); |
---|
647 | aws->label("X-padding (pixels)"); |
---|
648 | aws->create_input_field_with_scaler(AWAR_MATRIX_PADDINGX, FIELDWIDTH, SCALERWIDTH); |
---|
649 | |
---|
650 | aws->at_newline(); |
---|
651 | aws->label("Y-padding (pixels)"); |
---|
652 | aws->create_input_field_with_scaler(AWAR_MATRIX_PADDINGY, FIELDWIDTH, SCALERWIDTH); |
---|
653 | |
---|
654 | aws->at_newline(); |
---|
655 | aws->label("Show leading zero"); |
---|
656 | aws->create_toggle(AWAR_MATRIX_SHOWZERO); |
---|
657 | |
---|
658 | aws->at_newline(); |
---|
659 | aws->label("Precision (digits)"); |
---|
660 | aws->create_input_field_with_scaler(AWAR_MATRIX_DIGITS, FIELDWIDTH, SCALERWIDTH); |
---|
661 | |
---|
662 | aws->at_newline(); |
---|
663 | aws->label("Min. namechars (top)"); |
---|
664 | aws->create_input_field_with_scaler(AWAR_MATRIX_NAMECHARS_TOP, FIELDWIDTH, SCALERWIDTH); |
---|
665 | |
---|
666 | aws->at_newline(); |
---|
667 | aws->label("Min. namechars (left)"); |
---|
668 | aws->create_input_field_with_scaler(AWAR_MATRIX_NAMECHARS_LEFT, FIELDWIDTH, SCALERWIDTH); |
---|
669 | |
---|
670 | aws->window_fit(); |
---|
671 | return aws; |
---|
672 | } |
---|
673 | |
---|
674 | static void selected_species_changed_cb(AW_root*, MatrixDisplay *disp) { |
---|
675 | if (disp) redisplay_needed(NULp, disp); |
---|
676 | } |
---|
677 | |
---|
678 | AW_window *DI_create_view_matrix_window(AW_root *awr, MatrixDisplay *disp, save_matrix_params *sparam) { |
---|
679 | di_bind_dist_awars(awr, disp); |
---|
680 | create_matrix_awars(awr, disp); |
---|
681 | |
---|
682 | AW_window_menu *awm = new AW_window_menu; |
---|
683 | awm->init(awr, "SHOW_MATRIX", "ARB distance matrix", 800, 600); |
---|
684 | |
---|
685 | disp->device = awm->get_device(AW_MIDDLE_AREA); |
---|
686 | disp->awm = awm; |
---|
687 | |
---|
688 | awr->awar(AWAR_SPECIES_NAME)->add_callback(makeRootCallback(selected_species_changed_cb, disp)); |
---|
689 | |
---|
690 | awm->set_vertical_change_callback (makeWindowCallback(vertical_change_cb, disp)); |
---|
691 | awm->set_horizontal_change_callback(makeWindowCallback(horizontal_change_cb, disp)); |
---|
692 | |
---|
693 | awm->set_resize_callback(AW_MIDDLE_AREA, makeWindowCallback(resize_needed, disp)); |
---|
694 | awm->set_expose_callback(AW_MIDDLE_AREA, makeWindowCallback(redisplay_needed, disp)); |
---|
695 | awm->set_input_callback (AW_MIDDLE_AREA, makeWindowCallback(input_cb, disp)); |
---|
696 | awm->set_motion_callback(AW_MIDDLE_AREA, makeWindowCallback(motion_cb, disp)); |
---|
697 | |
---|
698 | AW_gc_manager *gc_manager = |
---|
699 | AW_manage_GC(awm, |
---|
700 | awm->get_window_id(), |
---|
701 | disp->device, DI_G_LAST, AW_GCM_DATA_AREA, |
---|
702 | makeGcChangedCallback(gc_changed_cb, disp), |
---|
703 | "#D0D0D0", |
---|
704 | "#Standard$#000000", |
---|
705 | "#Names$#000000", |
---|
706 | "+-Ruler$#555", "-Display$#00AA55", |
---|
707 | "#BelowDist$#008732", |
---|
708 | "#AboveDist$#DB008D", |
---|
709 | NULp); |
---|
710 | |
---|
711 | awm->create_menu("File", "F"); |
---|
712 | awm->insert_menu_topic("save_matrix", "Save Matrix to File", "S", "save_matrix.hlp", AWM_ALL, makeCreateWindowCallback(DI_create_save_matrix_window, sparam)); |
---|
713 | awm->insert_menu_topic("close", "Close", "C", NULp, AWM_ALL, AW_POPDOWN); |
---|
714 | |
---|
715 | awm->create_menu("Range", "R"); |
---|
716 | awm->insert_menu_topic("deselect_range", "Deselect range", "D", NULp, AWM_ALL, makeWindowCallback(di_view_set_max_dist, 0)); |
---|
717 | awm->insert_menu_topic("show_dist_species", "Species range [ <= 2% ]", "S", NULp, AWM_ALL, makeWindowCallback(di_view_set_max_dist, 2)); |
---|
718 | awm->insert_menu_topic("show_dist_genus", "Genus range [ <= 5% ]", "G", NULp, AWM_ALL, makeWindowCallback(di_view_set_max_dist, 5)); |
---|
719 | awm->insert_menu_topic("show_dist_010", "Range [ <= 10% ]", "1", NULp, AWM_ALL, makeWindowCallback(di_view_set_max_dist, 10)); |
---|
720 | awm->insert_menu_topic("show_dist_025", "Range [ <= 25% ]", "2", NULp, AWM_ALL, makeWindowCallback(di_view_set_max_dist, 25)); |
---|
721 | awm->insert_menu_topic("show_dist_050", "Range [ <= 50% ]", "5", NULp, AWM_ALL, makeWindowCallback(di_view_set_max_dist, 50)); |
---|
722 | awm->insert_menu_topic("show_dist_100", "Whole range [ 0-100% ]", "0", NULp, AWM_ALL, makeWindowCallback(di_view_set_max_dist, 100)); |
---|
723 | |
---|
724 | awm->create_menu("Properties", "P"); |
---|
725 | awm->insert_menu_topic("matrix_settings", "Settings ...", "S", "matrix_settings.hlp", AWM_ALL, create_matrix_settings_window); |
---|
726 | awm->insert_menu_topic("matrix_colors", "Colors and Fonts ...", "C", "color_props.hlp", AWM_ALL, makeCreateWindowCallback(AW_create_gc_window, gc_manager)); |
---|
727 | awm->insert_menu_topic("save_props", "Save Properties (dist.arb)", "P", "savedef.hlp", AWM_ALL, AW_save_properties); |
---|
728 | |
---|
729 | #define FIELD_SIZE 7 |
---|
730 | #define SCALER_SIZE 200 |
---|
731 | |
---|
732 | awm->auto_space(5, 5); |
---|
733 | |
---|
734 | awm->label("Dist min:"); awm->create_input_field_with_scaler(AWAR_DIST_MIN_DIST, FIELD_SIZE, SCALER_SIZE, AW_SCALER_EXP_LOWER); |
---|
735 | awm->label("Dist max:"); awm->create_input_field_with_scaler(AWAR_DIST_MAX_DIST, FIELD_SIZE, SCALER_SIZE, AW_SCALER_EXP_LOWER); |
---|
736 | |
---|
737 | awm->set_info_area_height(35); |
---|
738 | |
---|
739 | di_view_set_distances(awm->get_root(), 2, disp); |
---|
740 | |
---|
741 | return awm; |
---|
742 | } |
---|