source: tags/ms_r18q1/DIST/DI_view_matrix.cxx

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