source: tags/ms_r16q3/DIST/DI_view_matrix.cxx

Last change on this file was 15256, checked in by westram, 8 years ago
  • replace redundant data AW_font_limits::height with get_height()
  • 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 -> fall-through
54        case GC_COLOR_CHANGED:
55            redisplay_needed(NULL, disp);
56            break;
57        case GC_FONT_CHANGED:
58            resize_needed(NULL, disp);
59            break;
60    }
61}
62
63void 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
133void 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
180enum ClickAction {
181    CLICK_SELECT_SPECIES = 1,
182    CLICK_SET_MINMAX,
183};
184
185#define MINMAX_GRANULARITY 10000L
186
187void 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
198void 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
203void 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
222static 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
231static 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 = 0;
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
299void 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(GLOBAL_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 = 0;
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
469void 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#if defined(WARN_TODO)
477#warning test scrolling again with fixed box_impl()
478#endif
479
480void MatrixDisplay::monitor_vertical_scroll_cb(AW_window *aww) { // draw area
481    if (!device) return;
482
483    int old_vert_page_start = vert_page_start;
484
485    vert_last_view_start = aww->slider_pos_vertical;
486    vert_page_start      = aww->slider_pos_vertical/cell_height;
487
488    int diff = vert_page_start-old_vert_page_start; // amount of rows to scroll
489    if (diff) {
490        int diff_pix   = abs(diff)*cell_height;
491        int top_y      = off_dy-cell_height;
492        int keep_cells = vert_page_size-abs(diff);
493        int keep_pix   = keep_cells*cell_height;
494
495        if (diff>0 && diff<vert_page_size) { // scroll some positions up
496            device->move_region(0, top_y+diff_pix, screen_width, keep_pix, 0, top_y);
497            device->clear_part (0, top_y+keep_pix, screen_width, diff_pix, AW_SCREEN);
498            device->push_clip_scale();
499            device->set_top_clip_border(top_y+keep_pix, true);
500        }
501        else if (diff>-vert_page_size && diff<0) { // scroll some positions down
502            device->move_region(0, top_y, screen_width, keep_pix, 0, top_y+diff_pix);
503            device->clear_part (0, top_y, screen_width, diff_pix, AW_SCREEN);
504            device->push_clip_scale();
505            device->set_bottom_clip_border(top_y+diff_pix, true);
506        }
507        else { // repaint
508            device->push_clip_scale();
509            mark(NEED_CLEAR);
510        }
511
512        update_display();
513        device->pop_clip_scale();
514    }
515}
516
517void MatrixDisplay::monitor_horizontal_scroll_cb(AW_window *aww) { // draw area
518    if (!device) return;
519
520    int old_horiz_page_start = horiz_page_start;
521
522    horiz_last_view_start = aww->slider_pos_horizontal;
523    horiz_page_start      = aww->slider_pos_horizontal/cell_width;
524
525    int diff = horiz_page_start-old_horiz_page_start; // amount of columns to scroll
526
527    if (diff) {
528        int diff_pix   = abs(diff)*cell_width;
529        int keep_cells = horiz_page_size-abs(diff);
530        int keep_pix   = keep_cells*cell_width;
531
532        if (diff>0 && diff<horiz_page_size) {      // scroll some positions left
533            device->move_region(off_dx+diff_pix, 0, keep_pix, screen_height, off_dx, 0);
534            device->clear_part (off_dx+keep_pix, 0, diff_pix, screen_height, AW_SCREEN);
535            device->push_clip_scale();
536            device->set_left_clip_border(keep_pix, true);
537        }
538        else if (diff>-horiz_page_size && diff<0) { // scroll some positions right
539            device->move_region(off_dx, 0, keep_pix, screen_height, off_dx+diff_pix, 0);
540            device->clear_part (off_dx, 0, diff_pix, screen_height, AW_SCREEN);
541            device->push_clip_scale();
542            device->set_right_clip_border(off_dx+diff_pix, true);
543        }
544        else { // repaint
545            device->push_clip_scale();
546            mark(NEED_CLEAR);
547        }
548
549        update_display();
550        device->pop_clip_scale();
551    }
552}
553
554static bool update_display_on_dist_change = true;
555
556static void di_view_set_max_dist(AW_window *aww, int max_dist) {
557    AW_root *aw_root = aww->get_root();
558    {
559        LocallyModify<bool> flag(update_display_on_dist_change, false);
560        aw_root->awar(AWAR_DIST_MIN_DIST)->write_float(0.0);
561    }
562    aw_root->awar(AWAR_DIST_MAX_DIST)->write_float(max_dist*0.01);
563}
564
565static void di_view_set_distances(AW_root *awr, int setmax, MatrixDisplay *disp) {
566    // cl_dmatrix: 0 -> set min and fix max, 1 -> set max and fix min, 2 -> set both
567    float max_dist = awr->awar(AWAR_DIST_MAX_DIST)->read_float();
568    float min_dist = awr->awar(AWAR_DIST_MIN_DIST)->read_float();
569
570    {
571        LocallyModify<bool> flag(update_display_on_dist_change, false);
572
573        switch (setmax) {
574            case 2:                 // both
575                disp->set_slider_max(max_dist);
576                // fall-through
577            case 0:                 // set min and fix max
578                disp->set_slider_min(min_dist);
579                if (min_dist>max_dist) awr->awar(AWAR_DIST_MAX_DIST)->write_float(min_dist);
580                break;
581            case 1:                 // set max and fix min
582                disp->set_slider_max(max_dist);
583                if (min_dist>max_dist) awr->awar(AWAR_DIST_MIN_DIST)->write_float(max_dist);
584                break;
585
586            default:
587                di_assert(0);
588                break;
589        }
590    }
591    if (update_display_on_dist_change) {
592        disp->mark(MatrixDisplay::NEED_CLEAR);
593        disp->update_display();
594    }
595}
596
597static void di_bind_dist_awars(AW_root *aw_root, MatrixDisplay *disp) {
598    aw_root->awar_float(AWAR_DIST_MIN_DIST)->add_callback(makeRootCallback(di_view_set_distances, 0, disp));
599    aw_root->awar_float(AWAR_DIST_MAX_DIST)->add_callback(makeRootCallback(di_view_set_distances, 1, disp));
600}
601
602static void create_matrix_awars(AW_root *awr, MatrixDisplay *disp) {
603    RootCallback reinit_needed_cb = makeRootCallback(reinit_needed, disp);
604
605    awr->awar_int(AWAR_MATRIX_SHOWZERO,       1)                      ->add_callback(reinit_needed_cb);
606    awr->awar_int(AWAR_MATRIX_PADDINGX,       4) ->set_minmax(-10, 10)->add_callback(reinit_needed_cb);
607    awr->awar_int(AWAR_MATRIX_PADDINGY,       4) ->set_minmax(-10, 10)->add_callback(reinit_needed_cb);
608    awr->awar_int(AWAR_MATRIX_DIGITS,         4) ->set_minmax(0, 10)  ->add_callback(reinit_needed_cb);
609    awr->awar_int(AWAR_MATRIX_NAMECHARS_TOP,  8) ->set_minmax(0, 10)  ->add_callback(reinit_needed_cb);
610    awr->awar_int(AWAR_MATRIX_NAMECHARS_LEFT, 10)->set_minmax(0, 10)  ->add_callback(reinit_needed_cb);
611}
612
613static AWT_config_mapping_def matrixConfigMapping[] = {
614    { AWAR_MATRIX_PADDINGX,       "paddingx" },
615    { AWAR_MATRIX_PADDINGY,       "paddingy" },
616    { AWAR_MATRIX_SHOWZERO,       "showzero" },
617    { AWAR_MATRIX_DIGITS,         "precision" },
618    { AWAR_MATRIX_NAMECHARS_TOP,  "namechars_top" },
619    { AWAR_MATRIX_NAMECHARS_LEFT, "namechars_left" },
620
621    { 0, 0 }
622};
623
624static AWT_predefined_config predefinedMatrixConfig[] = {
625    { "*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'" },
626    { 0, 0, 0 }
627};
628
629static AW_window *create_matrix_settings_window(AW_root *awr) {
630    AW_window_simple *aws = new AW_window_simple;
631    aws->init(awr, "MATRIX_SETTINGS", "Matrix settings");
632
633    const int FIELDWIDTH = 3;
634    const int SCALERWIDTH = 200;
635
636    aws->auto_space(10, 10);
637
638    aws->callback(AW_POPDOWN);
639    aws->create_button("CLOSE", "CLOSE", "C");
640
641    aws->callback(makeHelpCallback("matrix_settings.hlp"));
642    aws->create_button("HELP", "HELP");
643
644    AWT_insert_config_manager(aws, AW_ROOT_DEFAULT, "matrix_settings", matrixConfigMapping, NULL, predefinedMatrixConfig);
645
646    aws->label_length(21);
647
648    aws->at_newline();
649    aws->label("X-padding (pixels)");
650    aws->create_input_field_with_scaler(AWAR_MATRIX_PADDINGX, FIELDWIDTH, SCALERWIDTH);
651
652    aws->at_newline();
653    aws->label("Y-padding (pixels)");
654    aws->create_input_field_with_scaler(AWAR_MATRIX_PADDINGY, FIELDWIDTH, SCALERWIDTH);
655
656    aws->at_newline();
657    aws->label("Show leading zero");
658    aws->create_toggle(AWAR_MATRIX_SHOWZERO);
659
660    aws->at_newline();
661    aws->label("Precision (digits)");
662    aws->create_input_field_with_scaler(AWAR_MATRIX_DIGITS, FIELDWIDTH, SCALERWIDTH);
663
664    aws->at_newline();
665    aws->label("Min. namechars (top)");
666    aws->create_input_field_with_scaler(AWAR_MATRIX_NAMECHARS_TOP, FIELDWIDTH, SCALERWIDTH);
667
668    aws->at_newline();
669    aws->label("Min. namechars (left)");
670    aws->create_input_field_with_scaler(AWAR_MATRIX_NAMECHARS_LEFT, FIELDWIDTH, SCALERWIDTH);
671
672    aws->window_fit();
673    return aws;
674}
675
676static void selected_species_changed_cb(AW_root*, MatrixDisplay *disp) {
677    if (disp) redisplay_needed(NULL, disp);
678}
679
680AW_window *DI_create_view_matrix_window(AW_root *awr, MatrixDisplay *disp, save_matrix_params *sparam) {
681    di_bind_dist_awars(awr, disp);
682    create_matrix_awars(awr, disp);
683   
684    AW_window_menu *awm = new AW_window_menu;
685    awm->init(awr, "SHOW_MATRIX", "ARB distance matrix", 800, 600);
686
687    disp->device = awm->get_device(AW_MIDDLE_AREA);
688    disp->awm    = awm;
689
690    awr->awar(AWAR_SPECIES_NAME)->add_callback(makeRootCallback(selected_species_changed_cb, disp));
691   
692    awm->set_vertical_change_callback  (makeWindowCallback(vertical_change_cb,   disp));
693    awm->set_horizontal_change_callback(makeWindowCallback(horizontal_change_cb, disp));
694
695    awm->set_resize_callback(AW_MIDDLE_AREA, makeWindowCallback(resize_needed,    disp));
696    awm->set_expose_callback(AW_MIDDLE_AREA, makeWindowCallback(redisplay_needed, disp));
697    awm->set_input_callback (AW_MIDDLE_AREA, makeWindowCallback(input_cb,         disp));
698    awm->set_motion_callback(AW_MIDDLE_AREA, makeWindowCallback(motion_cb,        disp));
699
700    AW_gc_manager *gc_manager =
701        AW_manage_GC(awm,
702                     awm->get_window_id(),
703                     disp->device, DI_G_LAST, AW_GCM_DATA_AREA,
704                     makeGcChangedCallback(gc_changed_cb, disp),
705                     "#D0D0D0",
706                     "#Standard$#000000",
707                     "#Names$#000000",
708                     "+-Ruler$#555", "-Display$#00AA55",
709                     "#BelowDist$#008732",
710                     "#AboveDist$#DB008D",
711                     NULL);
712
713    awm->create_menu("File", "F");
714    awm->insert_menu_topic("save_matrix", "Save Matrix to File", "S", "save_matrix.hlp", AWM_ALL, makeCreateWindowCallback(DI_create_save_matrix_window, sparam));
715    awm->insert_menu_topic("close",       "Close",               "C", 0,                 AWM_ALL, AW_POPDOWN);
716
717    awm->create_menu("Range", "R");
718    awm->insert_menu_topic("deselect_range",    "Deselect range",           "D", 0, AWM_ALL, makeWindowCallback(di_view_set_max_dist, 0));
719    awm->insert_menu_topic("show_dist_species", "Species range [ <=  2% ]", "S", 0, AWM_ALL, makeWindowCallback(di_view_set_max_dist, 2));
720    awm->insert_menu_topic("show_dist_genus",   "Genus range   [ <=  5% ]", "G", 0, AWM_ALL, makeWindowCallback(di_view_set_max_dist, 5));
721    awm->insert_menu_topic("show_dist_010",     "Range         [ <= 10% ]", "1", 0, AWM_ALL, makeWindowCallback(di_view_set_max_dist, 10));
722    awm->insert_menu_topic("show_dist_025",     "Range         [ <= 25% ]", "2", 0, AWM_ALL, makeWindowCallback(di_view_set_max_dist, 25));
723    awm->insert_menu_topic("show_dist_050",     "Range         [ <= 50% ]", "5", 0, AWM_ALL, makeWindowCallback(di_view_set_max_dist, 50));
724    awm->insert_menu_topic("show_dist_100",     "Whole range   [ 0-100% ]", "0", 0, AWM_ALL, makeWindowCallback(di_view_set_max_dist, 100));
725
726    awm->create_menu("Properties", "P");
727    awm->insert_menu_topic("matrix_settings", "Settings ...",               "S", "matrix_settings.hlp", AWM_ALL, create_matrix_settings_window);
728    awm->insert_menu_topic("matrix_colors",   "Colors and Fonts ...",       "C", "color_props.hlp",     AWM_ALL, makeCreateWindowCallback(AW_create_gc_window, gc_manager));
729    awm->insert_menu_topic("save_props",      "Save Properties (dist.arb)", "P", "savedef.hlp",         AWM_ALL, AW_save_properties);
730
731#define FIELD_SIZE  7
732#define SCALER_SIZE 200
733
734    awm->auto_space(5, 5);
735
736    awm->label("Dist min:"); awm->create_input_field_with_scaler(AWAR_DIST_MIN_DIST, FIELD_SIZE, SCALER_SIZE, AW_SCALER_EXP_LOWER);
737    awm->label("Dist max:"); awm->create_input_field_with_scaler(AWAR_DIST_MAX_DIST, FIELD_SIZE, SCALER_SIZE, AW_SCALER_EXP_LOWER);
738
739    awm->set_info_area_height(35);
740
741    di_view_set_distances(awm->get_root(), 2, disp);
742
743    return awm;
744}
Note: See TracBrowser for help on using the repository browser.