source: tags/arb_5.1/WINDOW/AW_preset.cxx

Last change on this file was 5963, checked in by westram, 15 years ago

Note: Still ongoing work..

  • macro feature was broken, because the used ids were not unique. Which callbacks were executed, depended on the order in which some windows were opened. This patch fixes many of them, but this patch as well breaks with most existing macro-code - sorry there's really no way how this could have been avoided.
  • create unique window ids for different input masks
  • accept duplicate remote commands, if only parent window differs (used for duplicated menus)
  • unique 'query_id' does no longer depend on order in which query-boxes were opened (instead it's passed by caller)
  • create unique menu-entry-ids for querybox submenus
  • changed querybox awar names
  • id "RADIAL_DISPLAY_TYPE" was used 3 times
  • unique ids in NDS config
  • added (AW_window) local_id()
  • use local_id() to create unique ids for ARB_NTREE clone window
  • callallcallbacks handles some callbacks special
    • if id begins with '!' callback is skipped
    • if id begins with '-' callback is delayed
  • excluded several callbacks calling external tools to speed up things
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.1 KB
Line 
1
2//  ----------------------------------------------------
3//      This file is located in WINDOW/AW_preset.cxx
4//      (AWT/AWT_preset.cxx is just a link)
5//  ----------------------------------------------------
6
7#ifndef IN_ARB_AWT
8#ifndef IN_ARB_WINDOW
9#error MODULE_... is not known
10#endif
11#endif
12
13#include <stdio.h>
14#include <string.h>
15#include <memory.h>
16#include <arbdbt.h>
17#include <aw_root.hxx>
18#include <aw_device.hxx>
19#include <aw_window.hxx>
20#include <aw_color_groups.hxx>
21// PJ vectorfont stuff:
22//#include <aw_xfig.hxx>
23//#include <aw_xfigfont.hxx>
24
25#include "awt.hxx"
26#include "awt_advice.hxx"
27
28#include <stdarg.h>
29#include <stdlib.h>
30#include <awt_canvas.hxx>
31#include "aw_preset.hxx"
32#include "aw_def.hxx"
33
34#ifdef IN_ARB_WINDOW
35
36void AW_save_defaults( AW_window *aw ) {
37    aw->get_root()->save_default(  "window/font" );
38}
39
40void AW_save_specific_defaults( AW_window *aw, const char *filename) { // special version for EDIT4
41    aw->get_root()->save_default(  "window/font", filename);
42}
43
44void aw_message_reload(AW_root *){
45    aw_message( "Sorry, to activate new colors:\n"
46                "   save properties\n"
47                "   and restart application");
48}
49char *aw_glob_font_awar_name = 0;
50
51static void aw_set_color(AW_window *aww, AW_CL cl_color_name){
52    const char *color_name = (const char *)cl_color_name;
53    aww->get_root()->awar(aw_glob_font_awar_name)->write_string(color_name);
54}
55
56// --------------------------------------------------------------------------------
57//     static int hex2dez(char c)
58// --------------------------------------------------------------------------------
59static int hex2dez(char c) {
60    if (c>='0' && c<='9') return c-'0';
61    if (c>='A' && c<='F') return c-'A'+10;
62    if (c>='a' && c<='f') return c-'a'+10;
63    return -1;
64}
65
66// --------------------------------------------------------------------------------
67//     void aw_incdec_color(AW_window *aww,const char *action)
68// --------------------------------------------------------------------------------
69void aw_incdec_color(AW_window *aww,const char *action){
70    // action is sth like "r+" "b-" "g++" "r--"
71    AW_awar *awar  = aww->get_root()->awar(aw_glob_font_awar_name);
72    char    *color = awar->read_string();
73    bool     err   = true;
74
75    fprintf(stderr, "current color is '%s'\n", color);
76
77    if (color[0]=='#') {
78        int len = strlen(color);
79        if (len==4 || len==7) {
80            len = (len-1)/3; // len of one color channel (1 or 2)
81            gb_assert(len==1 || len==2);
82
83            int diff = action[2]==action[1] ? 7 : 1;
84
85            int channel[3];
86            for (int c=0; c<3; ++c) {
87                if (len==2) channel[c] = hex2dez(color[c*len+1])*16+hex2dez(color[c*len+2]);
88                else        channel[c] = hex2dez(color[c*len+1])*16;
89            }
90
91            int rgb;
92            for (rgb=0; rgb<3;++rgb) {
93                if (action[0]=="rgb"[rgb] || action[0]=='a') {
94                    if (action[1]=='+') { channel[rgb] += diff; if (channel[rgb]>255) channel[rgb]=255; }
95                    else                { channel[rgb] -= diff; if (channel[rgb]<0)   channel[rgb]=0; }
96                }
97            }
98
99            sprintf(color, "#%2.2X%2.2X%2.2X", channel[0], channel[1], channel[2]);
100
101            err = false;
102            awar->write_string(color);
103        }
104    }
105
106    if (err) {
107        aw_message("Only color values in #rgb- or #rrggbb-style \n"
108                   "can be modified by these buttons. \n"
109                   "Choose a color below and try again.");
110    }
111}
112
113#define AWAR_GLOBAL_COLOR_NAME "tmp/aw/color_label"
114
115void aw_create_color_chooser_window(AW_window *aww, const char *awar_name,const char *label_name){
116    AW_root *awr = aww->get_root();
117    static AW_window_simple *aws = 0;
118    if (!aws){
119        int x1, y1, x2, y2;
120
121        awr->awar_string(AWAR_GLOBAL_COLOR_NAME);
122        aws = new AW_window_simple;
123        aws->init(awr, "COLORS", "COLORS");
124        aws->at(10, 10);
125        aws->auto_space(3, 3);
126        aws->callback     ( AW_POPDOWN );
127        aws->create_button( "CLOSE","CLOSE", "C" );
128        aws->get_at_position(&x1, &y1);
129        aws->at_newline();
130
131        aws->button_length(20);
132        aws->create_button( "LABEL",AWAR_GLOBAL_COLOR_NAME, "A" );
133        aws->get_at_position(&x2, &y2);
134        aws->at_newline();
135
136        x1 = x1>x2 ? x1 : x2;
137
138        int red,green,blue,grey;
139
140        for (int minus = 0; minus<=1; ++minus) {
141            aws->at(x1, minus==0 ? y1 : y2);
142            for (int rgb=0; rgb<4; ++rgb) {
143                for (int big=0; big<=1; ++big) {
144                    aws->button_length(2+big);
145
146                    char action[4] = "xxx";
147                    action[0] = "rgba"[rgb];
148                    action[1] = "+-"[minus];
149                    action[2] = big ? action[1] : 0;
150
151                    char color_name[10];
152                    sprintf(color_name, "#%2.2X%2.2X%2.2X", rgb==0 ? 0xff : 0x55, rgb==1 ? 0xff : 0x55, rgb==2 ? 0xff : 0x55);
153                    aws->callback((AW_CB1)aw_incdec_color, (AW_CL)strdup(action));
154                    aws->create_button(action, action+1,0,color_name);
155                }
156            }
157        }
158
159        aws->button_length(2);
160        aws->at_newline();
161
162        for (red = 0; red <= 255; red += 255/3){
163            for (green = 0; green <= 255; green += 255/3){
164                for (blue = 0; blue <= 255; blue += 255/3){
165                    char color_name[256];
166                    sprintf(color_name,"#%2.2X%2.2X%2.2X", red, green,blue);
167                    aws->callback((AW_CB1)aw_set_color,(AW_CL)strdup(color_name));
168                    aws->create_button(color_name,"=",0,color_name); 
169                }
170            }
171            aws->at_newline();
172        }
173        for (grey = 256/32; grey < 256; grey += 256/16){ // grey buttons (skip black/white - already present above)
174            char color_name[256];
175            sprintf(color_name,"#%2.2X%2.2X%2.2X", grey, grey, grey);
176            aws->callback(aw_set_color,(AW_CL)strdup(color_name));
177            aws->create_button(color_name,"=",0,color_name); 
178        }
179        aws->at_newline();
180
181        aws->window_fit();
182    }
183    awr->awar(AWAR_GLOBAL_COLOR_NAME)->write_string(label_name);
184    freedup(aw_glob_font_awar_name, awar_name);
185    aws->activate();
186}
187
188void AW_preset_create_color_chooser(AW_window *aws, const char *awar_name, const char *label,bool message_reload, bool show_label)
189{
190    if (message_reload) aws->get_root()->awar(awar_name)->add_callback(aw_message_reload);
191    if (show_label) {
192        aw_assert(label);
193        aws->label(label);
194    }
195    aws->callback((AW_CB)aw_create_color_chooser_window,(AW_CL)strdup(awar_name),(AW_CL)strdup(label));
196    char *color     = aws->get_root()->awar(awar_name)->read_string();
197    char *button_id = GBS_global_string_copy("sel_color[%s]", awar_name);
198    aws->create_button(button_id, " ", 0, color);
199    free(button_id);
200    free(color);
201}
202
203void AW_preset_create_font_chooser(AW_window *aws, const char *awar, const char *label,bool message_reload)
204{
205    if (message_reload){
206        aws->get_root()->awar(awar)->add_callback(aw_message_reload);
207    }
208
209    aws->create_option_menu( awar, label );
210    aws->insert_option        ( "5x8",  "5", "5x8" );
211    aws->insert_option        ( "6x10",  "6", "6x10" );
212    aws->insert_option        ( "7x13",  "7", "7x13" );
213    aws->insert_option        ( "7x13bold",  "7", "7x13bold" );
214    aws->insert_option        ( "8x13",  "8", "8x13" );
215    aws->insert_option        ( "8x13bold",  "8", "8x13bold" );
216    aws->insert_option        ( "9x15",  "9", "9x15" );
217    aws->insert_option        ( "9x15bold",  "9", "9x15bold" );
218    aws->insert_option        ( "helvetica-12",  "9", "helvetica-12" );
219    aws->insert_option        ( "helvetica-bold-12",  "9", "helvetica-bold-12" );
220    aws->insert_option        ( "helvetica-13",  "9", "helvetica-13" );
221    aws->insert_option        ( "helvetica-bold-13",  "9", "helvetica-bold-13" );
222    aws->insert_default_option( "other", "o", ""     );
223    aws->update_option_menu();
224}
225
226//PJ - vectorfont stuff - user may set a factor for global scaling
227void AW_preset_create_scale_chooser(AW_window *aws, char *awar, char *label)
228{
229    char buffer[2]; buffer[0] = label[0]; buffer[1] = 0;
230    aws->create_option_menu( awar, label, buffer );
231    aws->insert_option        ( "0.5",   " ", (float) 0.5);
232    aws->insert_option        ( "0.8",   "0", (float) 0.8);
233    aws->insert_option        ( "1.0",   "1", (float) 1.0);
234    aws->insert_default_option( "other", "o", (float) 1.0 );
235    aws->update_option_menu();
236}
237
238struct AW_MGC_awar_cb_struct;
239
240class aw_gc_manager {
241    const char            *field;
242    const char            *default_value;
243    AW_option_menu_struct *font_size_handle; // the option menu to define the font size of the GC
244    AW_MGC_awar_cb_struct *font_change_cb_parameter;
245    aw_gc_manager         *next;
246
247public:
248    aw_gc_manager(const char *field_, const char *default_value_)
249        : field(field_)
250        , default_value(default_value_)
251        , font_size_handle(0)
252        , next(0)
253    {}
254
255    void enqueue(aw_gc_manager *next_) {
256        aw_assert(!next);
257        next = next_;
258    }
259
260    const char *get_field() const { return field; }
261    const char *get_default_value() const { return default_value; }
262    const aw_gc_manager *get_next() const { return next; }
263    aw_gc_manager *get_next() { return next; }
264
265    void set_font_size_handle(AW_option_menu_struct *oms) { font_size_handle = oms; }
266    AW_option_menu_struct *get_font_size_handle() const { return font_size_handle; }
267
268    void set_font_change_parameter(AW_MGC_awar_cb_struct *cb_data) { font_change_cb_parameter = cb_data; }
269    AW_MGC_awar_cb_struct *get_font_change_parameter() const { return font_change_cb_parameter; }
270};
271
272struct AW_MGC_cb_struct {   // one for each window
273    AW_MGC_cb_struct(AW_window *awi, void (*g)(AW_window*,AW_CL ,AW_CL), AW_CL cd1i, AW_CL cd2i);
274
275    AW_window *aw;
276    void (*f)(AW_window*,AW_CL ,AW_CL);
277    AW_CL      cd1;
278    AW_CL      cd2;
279    char      *window_awar_name;
280    AW_device *device;
281
282    struct AW_MGC_awar_cb_struct *next_drag;
283};
284
285AW_MGC_cb_struct::AW_MGC_cb_struct( AW_window *awi, void (*g)(AW_window*,AW_CL,AW_CL), AW_CL cd1i, AW_CL cd2i ) {
286    memset((char*)this,0,sizeof(AW_MGC_cb_struct));
287    aw  = awi;
288    f   = g;
289    cd1 = cd1i;
290    cd2 = cd2i;
291
292    window_awar_name = strdup(awi->get_window_id());
293}
294
295struct AW_MGC_awar_cb_struct {  // one for each awar
296    struct AW_MGC_cb_struct      *cbs;
297    const char                   *fontbasename;
298    const char                   *colorbasename;
299    short                         gc;
300    short                         gc_drag;
301    short                         colorindex;
302    aw_gc_manager                *gcmgr;
303    AW_window                    *gc_def_window;
304    struct AW_MGC_awar_cb_struct *next;
305};
306
307static void add_font_sizes_to_option_menu(AW_window *aww, int count, int *available_sizes) {
308    char ssize[20];
309    bool default_size_set = false;
310
311    for (int idx = 0; idx < count; ++idx) {
312        int size = available_sizes[idx];
313        if (!default_size_set && size > DEF_FONTSIZE) { // insert default size if missing
314            sprintf(ssize, "%i", DEF_FONTSIZE);
315            aww->insert_default_option(ssize, 0, (int) DEF_FONTSIZE);
316            default_size_set = true;
317        }
318        sprintf(ssize, "%i", size);
319        if (size == DEF_FONTSIZE) {
320            aww->insert_default_option(ssize, 0, (int) size);
321            default_size_set = true;
322        }
323        else {
324            aww->insert_option(ssize, 0, (int) size);
325        }
326    }
327
328    if (!default_size_set) {
329        sprintf(ssize, "%i", DEF_FONTSIZE);
330        aww->insert_default_option(ssize, 0, (int) DEF_FONTSIZE);
331    }
332
333    aww->update_option_menu();
334}
335
336static void aw_init_font_sizes(AW_root *awr, AW_MGC_awar_cb_struct *cbs, bool firstCall) {
337    AW_option_menu_struct *oms = cbs->gcmgr->get_font_size_handle();
338    aw_assert(oms);
339
340    if (oms) {                  // has font size definition
341        int  available_sizes[MAX_FONTSIZE-MIN_FONTSIZE+1];
342        char awar_name[256];
343        sprintf(awar_name,AWP_FONTNAME_TEMPLATE,cbs->cbs->window_awar_name,cbs->fontbasename);
344
345        int        font_nr     = awr->awar(awar_name)->read_int();
346        int        found_sizes = cbs->cbs->device->get_available_fontsizes(cbs->gc, font_nr, available_sizes);
347        AW_window *aww         = cbs->gc_def_window;
348        aw_assert(aww);
349
350        if (!firstCall) aww->clear_option_menu(oms);
351        add_font_sizes_to_option_menu(aww, found_sizes, available_sizes);
352    }
353}
354
355static void aw_font_changed_cb(AW_root *awr, AW_CL cl_cbs) {
356    AW_MGC_awar_cb_struct *cbs = (AW_MGC_awar_cb_struct*)cl_cbs;
357    aw_init_font_sizes(awr, cbs, false);
358}
359
360static void aw_gc_changed_cb(AW_root *awr,AW_MGC_awar_cb_struct *cbs, long mode)
361{
362    static int dont_recurse = 0;
363
364    if (dont_recurse == 0) {
365        ++dont_recurse;
366        // mode == -1 -> no callback
367        char awar_name[256];
368        int  font;
369        int  size;
370
371        sprintf(awar_name,AWP_FONTNAME_TEMPLATE,cbs->cbs->window_awar_name,cbs->fontbasename);
372        font = awr->awar(awar_name)->read_int();
373
374        sprintf(awar_name,AWP_FONTSIZE_TEMPLATE,cbs->cbs->window_awar_name,cbs->fontbasename);
375        AW_awar *awar_font_size = awr->awar(awar_name);
376        size                    = awar_font_size->read_int();
377
378        int found_font_size;
379        cbs->cbs->device->set_font(cbs->gc, font, size, &found_font_size);
380        cbs->cbs->device->set_font(cbs->gc_drag, font, size, 0);
381        if (found_font_size != -1 && found_font_size != size) {
382            // correct awar value if exact fontsize wasn't found
383            awar_font_size->write_int(found_font_size);
384        }
385
386        if (mode != -1) {
387            cbs->cbs->f(cbs->cbs->aw, cbs->cbs->cd1, cbs->cbs->cd2);
388        }
389        --dont_recurse;
390    }
391}
392
393void aw_gc_color_changed_cb(AW_root *root,AW_MGC_awar_cb_struct *cbs, long mode)
394{
395    char awar_name[256];
396    char *colorname;
397
398    sprintf(awar_name,AWP_COLORNAME_TEMPLATE,cbs->cbs->window_awar_name,cbs->colorbasename);
399    colorname = root->awar(awar_name)->read_string();
400    AW_color color = (AW_color)cbs->colorindex;
401    cbs->cbs->aw->alloc_named_data_color(color,colorname);
402    if (color != AW_DATA_BG) {
403        cbs->cbs->device->set_foreground_color(cbs->gc,color);
404        cbs->cbs->device->set_foreground_color(cbs->gc_drag,color);
405    }else{
406        struct AW_MGC_awar_cb_struct *acbs;
407        for (acbs = cbs->cbs->next_drag; acbs; acbs=acbs->next){
408            cbs->cbs->device->set_foreground_color(acbs->gc_drag,(AW_color)acbs->colorindex);
409        }
410    }
411    if (mode != -1) {
412        cbs->cbs->f(cbs->cbs->aw,cbs->cbs->cd1,cbs->cbs->cd2);
413    }
414    free(colorname);
415}
416
417static bool color_groups_initialized = false;
418static bool use_color_groups = false;
419
420const char *AW_get_color_group_name_awarname(int color_group) {
421    if (color_group>0 && color_group <= AW_COLOR_GROUPS) {
422        static char buf[sizeof(AWAR_COLOR_GROUPS_PREFIX)+1+4+2+1];
423        sprintf(buf, AWAR_COLOR_GROUPS_PREFIX "/name%i", color_group);
424        return buf;
425    }
426    return 0;
427}
428
429char *AW_get_color_group_name(AW_root *awr, int color_group) {
430    aw_assert(color_groups_initialized);
431    aw_assert(color_group>0 && color_group <= AW_COLOR_GROUPS);
432    return awr->awar(AW_get_color_group_name_awarname(color_group))->read_string();
433}
434
435void AW_color_group_name_changed_cb(AW_root *) {
436    AWT_advice("To activate the new names for color groups you have to\n"
437               "save properties and restart the program.",
438               AWT_ADVICE_TOGGLE, "Color group name has been changed", 0);
439}
440void AW_color_group_usage_changed_cb(AW_root *awr, AW_CL /*cl_ntw*/) {
441    use_color_groups       = awr->awar(AWAR_COLOR_GROUPS_USE)->read_int();
442    //     AWT_canvas *ntw = (AWT_canvas*)cl_ntw;
443    //     ntw->refresh();
444    // @@@ FIXME: a working refresh is missing
445}
446
447void AW_init_color_groups(AW_root *awr, AW_default def) {
448    if (!color_groups_initialized) {
449        AW_awar *useAwar = awr->awar_int(AWAR_COLOR_GROUPS_USE, 1, def);
450        use_color_groups = useAwar->read_int();
451        useAwar->add_callback(AW_color_group_usage_changed_cb, (AW_CL)0);
452
453        char name_buf[AW_COLOR_GROUP_NAME_LEN+1];
454        for (int i = 1; i <= AW_COLOR_GROUPS; ++i) {
455            sprintf(name_buf, "color_group_%i", i);
456            awr->awar_string(AW_get_color_group_name_awarname(i), name_buf, def)->add_callback(AW_color_group_name_changed_cb);
457        }
458        color_groups_initialized = true;
459    }
460}
461
462// values optimized for ARB_NTREE :
463static const char *ARB_NTREE_color_group[AW_COLOR_GROUPS+1] = {
464    "+-" AW_COLOR_GROUP_PREFIX  "1$#D50000", "-" AW_COLOR_GROUP_PREFIX  "2$#00ffff",
465    "+-" AW_COLOR_GROUP_PREFIX  "3$#00FF77", "-" AW_COLOR_GROUP_PREFIX  "4$#c700c7",
466    "+-" AW_COLOR_GROUP_PREFIX  "5$#0000ff", "-" AW_COLOR_GROUP_PREFIX  "6$#FFCE5B",
467
468    "+-" AW_COLOR_GROUP_PREFIX  "7$#AB2323", "-" AW_COLOR_GROUP_PREFIX  "8$#008888",
469    "+-" AW_COLOR_GROUP_PREFIX  "9$#008800", "-" AW_COLOR_GROUP_PREFIX "10$#880088",
470    "+-" AW_COLOR_GROUP_PREFIX "11$#000088", "-" AW_COLOR_GROUP_PREFIX "12$#888800",
471
472    0
473};
474
475// values optimized for ARB_EDIT4 :
476static const char *ARB_EDIT4_color_group[AW_COLOR_GROUPS+1] = {
477    "+-" AW_COLOR_GROUP_PREFIX  "1$#FFAFAF", "-" AW_COLOR_GROUP_PREFIX  "2$#A1FFFF",
478    "+-" AW_COLOR_GROUP_PREFIX  "3$#AAFFAA", "-" AW_COLOR_GROUP_PREFIX  "4$#c700c7",
479    "+-" AW_COLOR_GROUP_PREFIX  "5$#C5C5FF", "-" AW_COLOR_GROUP_PREFIX  "6$#FFE370",
480
481    "+-" AW_COLOR_GROUP_PREFIX  "7$#F87070", "-" AW_COLOR_GROUP_PREFIX  "8$#DAFFFF",
482    "+-" AW_COLOR_GROUP_PREFIX  "9$#8DE28D", "-" AW_COLOR_GROUP_PREFIX "10$#880088",
483    "+-" AW_COLOR_GROUP_PREFIX "11$#000088", "-" AW_COLOR_GROUP_PREFIX "12$#F1F169",
484
485    0
486};
487
488static const char **color_group_gc_defaults = 0;
489
490void AW_init_color_group_defaults(const char *for_program) {
491    // if for_program == NULL defaults of arb_ntree are silently used
492    // if for_program is unknown a warning is shown
493
494    aw_assert(color_group_gc_defaults == 0); // oops - called twice
495
496    if (for_program) {
497        if (strcmp(for_program, "arb_ntree")      == 0) color_group_gc_defaults = ARB_NTREE_color_group;
498        else if (strcmp(for_program, "arb_edit4") == 0) color_group_gc_defaults = ARB_EDIT4_color_group;
499    }
500
501    if (!color_group_gc_defaults) {
502        if (for_program) { // unknown program ?
503#if defined(DEBUG)
504            fprintf(stderr, "No specific defaults for color groups defined (using those from ARB_NTREE)\n");
505#endif // DEBUG
506        }
507        color_group_gc_defaults = ARB_NTREE_color_group;
508    }
509
510    aw_assert(color_group_gc_defaults);
511}
512
513
514long AW_find_color_group(GBDATA *gbd, bool ignore_usage_flag) {
515    /* species/genes etc. may have a color group entry ('ARB_color')
516     * call with ignore_usage_flag == true to read color group regardless of global usage flag (AWAR_COLOR_GROUPS_USE)
517     */
518    aw_assert(color_groups_initialized);
519    if (!use_color_groups && !ignore_usage_flag) return 0;
520
521    GBDATA *gb_group = GB_entry(gbd, AW_COLOR_GROUP_ENTRY);
522    if (gb_group) return GB_read_int(gb_group);
523    return 0;                   /* no color group */
524}
525
526GB_ERROR AW_set_color_group(GBDATA *gbd, long color_group) {
527    return GBT_write_int(gbd, AW_COLOR_GROUP_ENTRY, color_group);
528}
529
530AW_gc_manager AW_manage_GC(AW_window   *aww,
531                           AW_device   *device,
532                           int          base_gc,
533                           int          base_drag,
534                           AW_GCM_AREA  area,
535                           void (*changecb)(AW_window*,AW_CL,AW_CL),
536                           AW_CL        cd1,
537                           AW_CL        cd2,
538                           bool         define_color_groups,
539                           const char  *default_background_color,
540                           ...)
541{
542    /*
543     *  Parameter:  aww:    base window
544     *          device: screen device
545     *          base_gc:    first gc number
546     *          base_drag:  one after last gc
547     *          area:       middle,top ...
548     *          changecb:   cb if changed
549     *          cd1,cd2:    free Parameters to changecb
550     *          define_color_groups: true -> add colors for color groups
551     *
552     *          ...:        NULL terminated list of \0 terminated strings:
553     *
554     *          first GC is fixed: '-background'
555     *
556     *          optionsSTRING   name of GC and AWAR
557     *          options:        #   fixed fonts only
558     *                          -   no fonts
559     *                          --  completely hide GC
560     *                          =   no color selector
561     *                          +   append next in same line
562     *
563     *                          $color at end of string = > define default color value
564     *                          ${GCname} at end of string = > use default of previously defined color
565     */
566
567    AW_root    *aw_root = aww->get_root();
568    AW_default  aw_def  = AW_ROOT_DEFAULT;
569
570    AW_init_color_groups(aw_root, aw_def);
571
572    const char           *id;
573    va_list               parg;
574    va_start(parg,default_background_color);
575    AW_font               def_font;
576    struct aw_gc_manager *gcmgrlast = 0,*gcmgr2=0,*gcmgrfirst=0;
577
578    AW_MGC_cb_struct *mcbs = new AW_MGC_cb_struct(aww,changecb,cd1,cd2);
579    mcbs->device = device;
580
581    int col = AW_WINDOW_BG;
582    if (area == AW_GCM_DATA_AREA) {
583        col = AW_DATA_BG;
584    }
585    bool first = true;
586
587    aww->main_drag_gc = base_drag;
588    gcmgrfirst = gcmgrlast = new aw_gc_manager(mcbs->window_awar_name, 0);
589
590    const char *old_font_base_name = "default";
591
592    char background[50];
593    gb_assert(default_background_color[0]);
594
595    sprintf(background, "-background$%s", default_background_color);
596
597    for (int loop = 1; loop <= 2; ++loop) {
598        int color_group_counter = 0;
599
600        if (loop == 1) { // set default colors
601            id = background;
602        }
603        else { // set color_group colors
604            id = 0;
605            if (define_color_groups) {
606                aw_assert(color_group_gc_defaults); // forgot to call AW_init_color_group_defaults ?
607                id = color_group_gc_defaults[color_group_counter++];
608            }
609        }
610
611        while (id) {
612            bool flag_fixed_fonts_only    = false;
613            bool flag_no_color_selector   = false;
614            bool flag_append_in_same_line = false;
615            bool flag_no_fonts            = false;
616
617            AW_MGC_awar_cb_struct *acbs = 0;
618            {
619                char       *id_copy       = strdup(id);
620                const char *default_color = 0;
621
622                {
623                    char *color = strchr(id_copy, '$'); // search color def
624
625                    if (color) {
626                        *color++ = 0;
627
628                        if (color[0] == '{') {
629                            char *close = strchr(color+1, '}');
630                            aw_assert(close); // format is '${KNOWN_GC}'
631                            aw_assert(close[1] == 0); // unexpected chars behind
632                            if (close) {
633                                close[0] = 0;
634                                color++;
635
636                                aw_gc_manager *known = gcmgrfirst;
637                                aw_assert(known);
638                                while (known) {
639                                    if (strcmp(known->get_field(), color) == 0) { // found referred gc
640                                        default_color = known->get_default_value(); // use it's default color
641                                        break;
642                                    }
643                                    known = known->get_next();
644                                }
645                                aw_assert(known); // referred to unknown {GC}
646                            }
647                        }
648                        else {
649                            default_color = color;
650                        }
651                    }
652                }
653
654                if (!default_color) default_color = first ? "white" : "black";
655
656                gcmgr2 = new aw_gc_manager(strdup(id_copy), strdup(default_color));
657
658                gcmgrlast->enqueue(gcmgr2);
659                gcmgrlast = gcmgr2;
660
661                acbs                = new AW_MGC_awar_cb_struct;
662                acbs->cbs           = mcbs;
663                acbs->colorbasename = GBS_string_2_key(id_copy);
664                acbs->gc            = base_gc;
665                acbs->gc_drag       = base_drag;
666                acbs->gcmgr         = gcmgr2;
667                acbs->gc_def_window = 0;
668
669                if (!first) {
670                    acbs->next = mcbs->next_drag;
671                    mcbs->next_drag = acbs;
672                }
673
674                int offset = 0;
675                while (1){
676                    switch( id_copy[offset] ){
677                        case '#': flag_fixed_fonts_only=    true; offset++; continue;
678                        case '=': flag_no_color_selector=   true; offset++; continue;
679                        case '+': flag_append_in_same_line= true; offset++; continue;
680                        case '-': flag_no_fonts=            true; offset++; continue;
681                        default:  break;
682                    }
683                    break;
684                }
685
686                freeset(id_copy, 0);
687            }
688
689            if (flag_fixed_fonts_only) def_font = AW_DEFAULT_FIXED_FONT; // AW_LUCIDA_SANS_TYPEWRITER;
690            else def_font                       = AW_DEFAULT_NORMAL_FONT; // AW_LUCIDA_SANS;
691
692            if ((area != AW_GCM_DATA_AREA) || !first) {
693                device->new_gc(base_gc);
694                device->set_line_attributes(base_gc,0.0,AW_SOLID);
695                device->set_function(base_gc,AW_COPY);
696
697                device->new_gc(base_drag);
698                device->set_line_attributes(base_drag,0.0,AW_SOLID);
699                device->set_function(base_drag,AW_XOR);
700            }
701
702            char awar_name[256]; memset(awar_name,0,256);
703            sprintf(awar_name,AWP_COLORNAME_TEMPLATE,mcbs->window_awar_name,acbs->colorbasename);
704            acbs->colorindex = col;
705
706            aw_root->awar_string(awar_name, gcmgr2->get_default_value(), aw_def);
707            aw_root->awar(awar_name)->add_callback( (AW_RCB)aw_gc_color_changed_cb,(AW_CL)acbs,(AW_CL)0);
708
709            aw_gc_color_changed_cb(aw_root,acbs,-1);
710
711            if (flag_no_fonts) acbs->fontbasename = old_font_base_name;
712            else old_font_base_name = acbs->fontbasename = acbs->colorbasename;
713
714            {
715                sprintf(awar_name,AWP_FONTNAME_TEMPLATE,mcbs->window_awar_name,acbs->fontbasename);
716                AW_awar *font_awar = aw_root->awar_int(awar_name,def_font,aw_def);
717                sprintf(awar_name,AWP_FONTSIZE_TEMPLATE,   mcbs->window_awar_name,acbs->fontbasename);
718                AW_awar *font_size_awar = aw_root->awar_int(awar_name,DEF_FONTSIZE,aw_def);
719
720                if (!flag_no_fonts) {
721                    font_awar->add_callback(aw_font_changed_cb, (AW_CL)acbs);
722                    gcmgr2->set_font_change_parameter(acbs);
723                }
724
725                font_awar->add_callback((AW_RCB)aw_gc_changed_cb,(AW_CL)acbs,(AW_CL)0);
726                font_size_awar->add_callback((AW_RCB)aw_gc_changed_cb,(AW_CL)acbs,(AW_CL)0);
727            }
728
729            if (!first) {
730                aw_gc_changed_cb(aw_root,acbs,-1);
731                base_gc++;
732                base_drag++;
733            }
734            col++;
735            first = false;
736
737            // switch to next default:
738
739            if (loop == 1) id = va_arg(parg, char*);
740            else {
741                aw_assert(color_group_gc_defaults); // forgot to call AW_init_color_group_defaults ?
742                id = color_group_gc_defaults[color_group_counter++];
743            }
744        }
745    }
746
747    va_end(parg);
748
749    return (AW_gc_manager)gcmgrfirst;
750}
751
752void AW_copy_GCs(AW_root *aw_root, const char *source_window, const char *dest_window, bool has_font_info, const char *id0, ...) {
753    // read the values of the specified GCs from 'source_window'
754    // and write the values into same-named GCs of 'dest_window'
755    //
756    // 'id0' is the first of a list of color ids
757    // a NULL pointer has to be given behind the last color!
758
759    va_list parg;
760    va_start(parg, id0);
761
762    const char *id = id0;
763    while (id) {
764        char *value = aw_root->awar(GBS_global_string(AWP_COLORNAME_TEMPLATE, source_window, id))->read_string();
765        aw_root->awar(GBS_global_string(AWP_COLORNAME_TEMPLATE, dest_window, id))->write_string(value);
766        free(value);
767
768        if (has_font_info) {
769            int ivalue = aw_root->awar(GBS_global_string(AWP_FONTNAME_TEMPLATE, source_window, id))->read_int();
770            aw_root->awar(GBS_global_string(AWP_FONTNAME_TEMPLATE, dest_window, id))->write_int(ivalue);
771            ivalue     = aw_root->awar(GBS_global_string(AWP_FONTSIZE_TEMPLATE, source_window, id))->read_int();
772            aw_root->awar(GBS_global_string(AWP_FONTSIZE_TEMPLATE, dest_window, id))->write_int(ivalue);
773        }
774
775        id = va_arg(parg, const char*); // another argument ?
776    }
777
778    va_end(parg);
779}
780
781static bool aw_insert_gcs(AW_root *aw_root, AW_window_simple *aws, aw_gc_manager *gcmgr, bool insert_color_groups) {
782    // returns true if GCs starting with COLOR_GROUP_PREFIX were found
783
784    bool        has_color_groups = false;
785    const char *window_awar_name = gcmgr->get_field();
786    bool        first            = true;
787
788    for (gcmgr = gcmgr->get_next(); gcmgr; gcmgr = gcmgr->get_next()) {
789        const char *id = gcmgr->get_field();
790
791        bool flag_fixed_fonts_only    = false;
792        bool flag_no_color_selector   = false;
793        bool flag_append_in_same_line = false;
794        bool flag_no_fonts            = false;
795        bool flag_hide_this_gc        = false;
796
797        while (1){
798            switch( id[0] ){
799                case '#': flag_fixed_fonts_only=    true; id++; continue;
800                case '=': flag_no_color_selector=   true; id++; continue;
801                case '+': flag_append_in_same_line= true; id++; continue;
802                case '-':   {
803                    if (flag_no_fonts) flag_hide_this_gc = true; // if gc definition contains -- the gc is completely hidden
804                    else flag_no_fonts = true;
805                    id++;
806                    continue;
807                }
808                default:    break;
809            }
810            break;
811        }
812
813        char *fontbasename   = GBS_string_2_key(id);
814        char  awar_name[256];
815        bool  is_color_group = strncmp(id, AW_COLOR_GROUP_PREFIX, AW_COLOR_GROUP_PREFIX_LEN) == 0;
816        int   color_group    = -1;
817
818        if (is_color_group) {
819            has_color_groups = true;
820            color_group      = atoi(id+AW_COLOR_GROUP_PREFIX_LEN);
821            if (!insert_color_groups) continue;
822        }
823        else { // is no color group
824            if (insert_color_groups) continue;
825        }
826
827        if (!flag_hide_this_gc) {
828            sprintf(awar_name, AWP_COLORNAME_TEMPLATE, window_awar_name, fontbasename);
829            aws->label_length(15);
830
831            if (is_color_group) {
832                aw_assert(color_group > 0);
833                char *color_group_name = AW_get_color_group_name(aw_root, color_group);
834                aws->label(color_group_name);
835                free(color_group_name);
836            }
837            else {
838                aws->label(id);
839            }
840
841            if (!flag_no_color_selector){
842                aws->button_length(5);
843                AW_preset_create_color_chooser(aws, awar_name, id);
844            }
845            aws->create_input_field(awar_name, 7);
846
847            if (!flag_no_fonts) {
848                sprintf(awar_name, AWP_FONTNAME_TEMPLATE, window_awar_name, fontbasename);
849
850                aws->label_length(5);
851                aws->create_option_menu(awar_name, "Font", 0);
852                {
853                    int font_nr;
854                    const char *font_string;
855
856                    for (font_nr = 0;; font_nr++) {
857                        font_string = aw_root->font_2_ascii((AW_font) font_nr);
858                        if (!font_string) break;
859                        if (flag_fixed_fonts_only && aw_root->font_2_xfig((AW_font) font_nr) >= 0) continue;
860                        aws->insert_option(font_string, 0, (int) font_nr);
861                    }
862                    aws->update_option_menu();
863                }
864
865                sprintf(awar_name, AWP_FONTSIZE_TEMPLATE,window_awar_name, fontbasename);
866                aws->label_length(5);
867
868                AW_option_menu_struct *oms = aws->create_option_menu(awar_name, "size", 0);
869                gcmgr->set_font_size_handle(oms);
870
871                AW_MGC_awar_cb_struct *acs = gcmgr->get_font_change_parameter();
872                acs->gc_def_window         = aws; 
873
874                aw_init_font_sizes(aw_root, acs, true); // does update_option_menu
875            }
876            if (!flag_append_in_same_line)  aws->at_newline();
877        }
878        first = false;
879        free(fontbasename);
880    }
881
882    return has_color_groups;
883}
884
885struct attached_window {
886    AW_window_simple       *aws;
887    AW_CL                   attached_to;
888    struct attached_window *next;
889};
890
891void AW_create_gc_color_groups_name_window(AW_window */*aww*/, AW_CL cl_aw_root, AW_CL cl_gcmgr) {
892    AW_root       *aw_root = (AW_root*)cl_aw_root;
893//     aw_gc_manager *gcmgr   = (aw_gc_manager*)cl_gcmgr;
894
895    static struct attached_window *head = 0;
896
897    // search for attached window:
898
899    struct attached_window *look = head;
900    while (look) {
901        if (look->attached_to == cl_gcmgr) break;
902        look = look->next;
903    }
904
905    AW_window_simple *aws = 0;
906
907    if (look) {
908        aws = look->aws;
909    }
910    else {
911        look = new struct attached_window;
912
913        look->aws         = new AW_window_simple;
914        look->attached_to = cl_gcmgr;
915        look->next        = head;
916        head              = look;
917
918        aws = look->aws;
919
920        aws->init(aw_root, "NAME_COLOR_GROUPS", "COLORS GROUP NAMES");
921
922        aws->at(10, 10);
923        aws->auto_space(5, 5);
924
925        aws->callback((AW_CB0) AW_POPDOWN);
926        aws->create_button("CLOSE","CLOSE", "C");
927
928        for (int i = 1; i <= AW_COLOR_GROUPS; ++i) {
929            aws->at_newline();
930
931            aws->label(GBS_global_string("Name for color group #%i%s", i, (i >= 10) ? "" : " "));
932            aws->create_input_field(AW_get_color_group_name_awarname(i), AW_COLOR_GROUP_NAME_LEN);
933        }
934
935        aws->window_fit();
936    }
937
938    aws->activate();
939}
940
941//  ------------------------------------------------------------------------------------------------
942//      void AW_create_gc_color_groups_window(AW_window *aww, AW_CL cl_aw_root, AW_CL cl_gcmgr)
943//  ------------------------------------------------------------------------------------------------
944void AW_create_gc_color_groups_window(AW_window */*aww*/, AW_CL cl_aw_root, AW_CL cl_gcmgr) {
945    aw_assert(color_groups_initialized);
946
947    AW_root       *aw_root = (AW_root*)cl_aw_root;
948    aw_gc_manager *gcmgr   = (aw_gc_manager*)cl_gcmgr;
949
950    static struct attached_window *head = 0;
951
952    // search for attached window:
953
954    struct attached_window *look = head;
955    while (look) {
956        if (look->attached_to == cl_gcmgr) break;
957        look = look->next;
958    }
959
960    AW_window_simple *aws = 0;
961
962    if (look) {
963        aws = look->aws;
964    }
965    else {
966        look = new struct attached_window;
967
968        look->aws         = new AW_window_simple;
969        look->attached_to = cl_gcmgr;
970        look->next        = head;
971        head              = look;
972
973        aws = look->aws;
974
975        aws->init(aw_root, "PROPS_COLOR_GROUPS", "COLORS GROUPS");
976
977        aws->at(10, 10);
978        aws->auto_space(5, 5);
979
980        aws->callback((AW_CB0) AW_POPDOWN);
981        aws->create_button("CLOSE","CLOSE", "C");
982
983        aws->callback(AW_POPUP_HELP,(AW_CL)"color_props_groups.hlp");
984        aws->create_button("HELP","HELP", "H");
985
986        aws->at_newline();
987
988        aw_insert_gcs(aw_root, aws, gcmgr, true);
989
990        aws->at_newline();
991
992        aws->label_length(16);
993        aws->label("Use color groups");
994        aws->create_toggle(AWAR_COLOR_GROUPS_USE);
995
996        aws->callback((AW_CB)AW_create_gc_color_groups_name_window, (AW_CL)aw_root, cl_gcmgr);
997        aws->create_autosize_button("DEF_NAMES", "Define names", "D");
998
999        aws->window_fit();
1000    }
1001
1002    aws->activate();
1003}
1004
1005AW_window *AW_create_gc_window_named(AW_root * aw_root, AW_gc_manager id_par, const char *wid, const char *windowname) {
1006    AW_window_simple * aws = new AW_window_simple;
1007   
1008    aws->init(aw_root, wid, windowname);
1009
1010    aw_gc_manager *gcmgr = (aw_gc_manager *)id_par;
1011
1012    aws->at(10, 10);
1013    aws->auto_space(5, 5);
1014
1015    aws->callback((AW_CB0) AW_POPDOWN);
1016    aws->create_button("CLOSE","CLOSE", "C");
1017
1018    aws->callback(AW_POPUP_HELP,(AW_CL)"color_props.hlp");
1019    aws->create_button("HELP","HELP", "H");
1020
1021    aws->at_newline();
1022
1023    bool has_color_groups = aw_insert_gcs(aw_root, aws, gcmgr, false);
1024
1025    if (has_color_groups) {
1026        aws->callback((AW_CB)AW_create_gc_color_groups_window, (AW_CL)aw_root, (AW_CL)id_par);
1027        aws->create_autosize_button("EDIT_COLOR_GROUP","Edit color groups", "E");
1028        aws->at_newline();
1029    }
1030
1031    aws->window_fit();
1032    return (AW_window *) aws;
1033   
1034}
1035AW_window *AW_create_gc_window(AW_root * aw_root, AW_gc_manager id_par)
1036{
1037    return AW_create_gc_window_named(aw_root, id_par, "PROPS_GC", "Colors and Fonts");
1038}
1039#endif // IN_ARB_WINDOW
1040
1041
1042#ifdef IN_ARB_AWT
1043
1044// callback to reset to default font
1045void awt_xfig_font_resetfont_cb(AW_window *aws){
1046    AW_root *aw_root = aws->get_root();
1047    aw_root->awar("vectorfont/file_name")->write_string("lib/pictures/fontgfx.vfont");
1048    //AW_POPDOWN(aws);
1049}
1050
1051void awt_xfig_font_create_filerequest(AW_window *aw) {
1052    static AW_window_simple *aws = 0;
1053    AW_root *aw_root = aw->get_root();
1054
1055    if (!aws) {
1056        // called *ONCE* to open the window
1057        aws = new AW_window_simple;
1058        aws->init( aw_root, "SELECT_VECTORFONT", "Select Xfig Vectorfont Resource");
1059        aws->load_xfig("fontgfx_request.fig");
1060        aws->at("close");aws->callback((AW_CB0)AW_POPDOWN);
1061        aws->create_button("CLOSE","CLOSE","C");
1062        aws->at("reset");aws->callback((AW_CB0)awt_xfig_font_resetfont_cb);
1063        aws->create_button("RESET","RESET","R");
1064        // AWT_sel_boxes.cxx
1065        awt_create_selection_box((AW_window *)aws,"vectorfont","","ARBHOME");
1066    }
1067    aw_root->awar("vectorfont/file_name")->write_string(aw_root->vectorfont_name);
1068    aws->activate();
1069}
1070
1071AW_window *AWT_preset_window( AW_root *root )
1072
1073#else // IN_ARB_WINDOW :
1074
1075    AW_window *AW_preset_window( AW_root *root )
1076   
1077#endif
1078   
1079{
1080    AW_window_simple *aws = new AW_window_simple;
1081    const int   tabstop = 400;
1082    aws->init( root, "PROPS_FRAME", "WINDOW_PROPERTIES");
1083
1084    aws->label_length( 25 );
1085    aws->button_length( 20 );
1086
1087    aws->at           ( 10,10 );
1088    aws->auto_space(10,10);
1089
1090    aws->callback     ( AW_POPDOWN );
1091    aws->create_button( "CLOSE","CLOSE", "C" );
1092
1093    aws->callback(AW_POPUP_HELP,(AW_CL)"props_frame.hlp");
1094    aws->create_button("HELP","HELP", "H");
1095
1096    aws->at_newline();
1097
1098    //PJ vectorfont stuff
1099    aws->label("Vectorfont Resource");
1100#ifdef IN_ARB_AWT
1101    aws->callback( (AW_CB0) awt_xfig_font_create_filerequest);
1102#endif // IN_ARB_AWT
1103    aws->create_button( "SELECT VECTORFONT", "Vectorfont Select", "V" );
1104    aws->at_x(tabstop);
1105    aws->create_input_field( "vectorfont/file_name",20);
1106    aws->at_newline();
1107
1108    AW_preset_create_font_chooser(aws,"window/font", "Main Menu Font", 1);
1109    aws->at_x(tabstop);
1110    aws->create_input_field( "window/font", 12 );
1111    aws->at_newline();
1112
1113    aws->button_length(10);
1114    AW_preset_create_color_chooser(aws,"window/background", "Application Background", true, true);
1115    aws->at_x(tabstop);
1116    aws->create_input_field( "window/background", 12 );
1117    aws->at_newline();
1118
1119    AW_preset_create_color_chooser(aws,"window/foreground", "Application Foreground", true, true );
1120    aws->at_x(tabstop);
1121    aws->create_input_field( "window/foreground", 12 );
1122    aws->at_newline();
1123
1124
1125    AW_preset_create_color_chooser(aws,"window/color_1", "Color 1", true, true );
1126    aws->at_x(tabstop);
1127    aws->create_input_field( "window/color_1", 12 );
1128    aws->at_newline();
1129
1130    AW_preset_create_color_chooser(aws,"window/color_2", "Color 2", true, true );
1131    aws->at_x(tabstop);
1132    aws->create_input_field( "window/color_2", 12 );
1133    aws->at_newline();
1134
1135    AW_preset_create_color_chooser(aws,"window/color_3", "Color 3", true, true );
1136    aws->at_x(tabstop);
1137    aws->create_input_field( "window/color_3", 12 );
1138    aws->at_newline();
1139
1140
1141    aws->window_fit();
1142    return (AW_window *)aws;
1143
1144}
Note: See TracBrowser for help on using the repository browser.