source: tags/svn.1.5.4/WINDOW/AW_global_awars.cxx

Last change on this file was 8423, checked in by westram, 12 years ago
  • use LocallyModify for several flags
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1//  ==================================================================== //
2//                                                                       //
3//    File      : AW_global_awars.cxx                                    //
4//    Purpose   : Make some awars accessible from ALL arb applications   //
5//                                                                       //
6//  Coded by Ralf Westram (coder@reallysoft.de) in January 2003          //
7//  Copyright Department of Microbiology (Technical University Munich)   //
8//                                                                       //
9//  Visit our web site at: http://www.arb-home.de/                       //
10//                                                                       //
11//  ==================================================================== //
12
13#define TEMP_DB_PATH "tmp/global_awars"
14
15#include <arbdb.h>
16#include <aw_global_awars.hxx>
17#include <aw_awars.hxx>
18#include <aw_root.hxx>
19#include <aw_window.hxx>
20
21static GBDATA *gb_main4awar = 0; // gb_main used for global awars
22
23inline const char *get_db_path(const AW_awar *awar) {
24    return GBS_global_string("%s/%s", TEMP_DB_PATH, awar->awar_name);
25}
26
27static bool in_global_awar_cb = false;
28
29static void awar_updated_cb(AW_root * /* aw_root */, AW_CL cl_awar) {
30    if (!in_global_awar_cb) {
31        AW_awar        *awar    = (AW_awar*)cl_awar;
32        char           *content = awar->read_as_string();
33        const char     *db_path = get_db_path(awar);
34        GB_transaction  dummy(gb_main4awar);
35        GBDATA         *gbd     = GB_search(gb_main4awar, db_path, GB_FIND);
36
37        aw_assert(gbd);             // should exists
38
39        LocallyModify<bool> flag(in_global_awar_cb, true);
40        GB_write_string(gbd, content);
41        free(content);
42    }
43}
44
45static void db_updated_cb(GBDATA *gbd, int *cl_awar, GB_CB_TYPE /* cbtype */) {
46    if (!in_global_awar_cb) {
47        AW_awar        *awar = (AW_awar*)cl_awar;
48        GB_transaction  dummy(gb_main4awar);
49
50        LocallyModify<bool> flag(in_global_awar_cb, true);
51        awar->write_as_string(GB_read_char_pntr(gbd));
52    }
53}
54
55GB_ERROR AW_awar::make_global() {
56#if defined(DEBUG)
57    aw_assert(!is_global);      // don't make awars global twice!
58    aw_assert(gb_main4awar);
59    is_global = true;
60#endif // DEBUG
61
62    add_callback(awar_updated_cb, (AW_CL)this);
63
64    GB_transaction  dummy(gb_main4awar);
65    const char     *db_path = get_db_path(this);
66    GBDATA         *gbd     = GB_search(gb_main4awar, db_path, GB_FIND);
67    GB_ERROR        error   = 0;
68
69    if (gbd) { // was already set by another ARB application
70        // -> read db value and store in awar
71
72        const char *content = GB_read_char_pntr(gbd);
73        write_as_string(content);
74    }
75    else {
76        // store awar value in db
77
78        char *content   = read_as_string();
79        gbd             = GB_search(gb_main4awar, db_path, GB_STRING);
80        if (!gbd) error = GB_await_error();
81        else  error     = GB_write_string(gbd, content);
82        free(content);
83    }
84
85    if (!error) GB_add_callback(gbd, GB_CB_CHANGED, db_updated_cb, (int*)this);
86    return error;
87}
88
89static bool initialized = false;
90
91bool ARB_global_awars_initialized() {
92    return initialized;
93}
94
95static void AWAR_AWM_MASK_changed_cb(AW_root *awr) {
96    int mask = awr->awar(AWAR_AWM_MASK)->read_int();
97#if defined(DEBUG)
98    printf("AWAR_AWM_MASK changed, calling apply_sensitivity(%i)\n", mask);
99#endif
100    awr->apply_sensitivity(mask);
101}
102
103static void AWAR_AW_FOCUS_FOLLOWS_MOUSE_changed_cb(AW_root *awr) {
104    int focus = awr->awar(AWAR_AW_FOCUS_FOLLOWS_MOUSE)->read_int();
105#if defined(DEBUG)
106    printf("AWAR_AW_FOCUS_FOLLOWS_MOUSE changed, calling apply_focus_policy(%i)\n", focus);
107#endif
108    awr->apply_focus_policy(focus);
109}
110
111#if defined(DARWIN)
112#define OPENURL "open"   
113#else
114#define OPENURL "xdg-open"
115#endif // DARWIN
116
117#define MAX_GLOBAL_AWARS 5
118
119static AW_awar *declared_awar[MAX_GLOBAL_AWARS];
120static int      declared_awar_count = 0;
121
122inline void declare_awar_global(AW_awar *awar) {
123    aw_assert(declared_awar_count<MAX_GLOBAL_AWARS);
124    declared_awar[declared_awar_count++] = awar;
125}
126
127void ARB_declare_global_awars(AW_root *aw_root, AW_default aw_def) {
128    aw_assert(!declared_awar_count);
129
130    declare_awar_global(aw_root->awar_string(AWAR_WWW_BROWSER, OPENURL " \"$(URL)\"", aw_def));
131    declare_awar_global(aw_root->awar_int(AWAR_AWM_MASK, AWM_MASK_UNKNOWN, aw_def)->add_callback(AWAR_AWM_MASK_changed_cb));
132
133    AW_awar *awar_focus          = aw_root->awar_int(AWAR_AW_FOCUS_FOLLOWS_MOUSE, 0, aw_def);
134    aw_root->focus_follows_mouse = awar_focus->read_int();
135    awar_focus->add_callback(AWAR_AW_FOCUS_FOLLOWS_MOUSE_changed_cb);
136    declare_awar_global(awar_focus);
137}
138
139GB_ERROR ARB_bind_global_awars(GBDATA *gb_main) {
140    aw_assert(!initialized);                        // don't call twice!
141    aw_assert(!gb_main4awar);
142    aw_assert(declared_awar_count);                 // b4 call ARB_declare_global_awars!
143
144    initialized  = true;
145    gb_main4awar = gb_main;
146
147    GB_ERROR error = NULL;
148    for (int a = 0; a<declared_awar_count && !error; ++a) {
149        error = declared_awar[a]->make_global();
150    }
151
152    return error;
153}
154
Note: See TracBrowser for help on using the repository browser.