source: tags/arb-6.0/DIST/DI_view_matrix.cxx

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