source: branches/species/WINDOW/AW_modal.cxx

Last change on this file was 19665, checked in by westram, 2 weeks ago
  • fix string splitting
    • add test showing that splitting an empty string with mode SPLIT_DROPEMPTY results in an empty array.
    • document effect of 'mode' on empty arrays / empty elements.
    • check + fix all callers:
      • explicitly drop empty tokens where they are expected not to occur.
      • parse_helix_list: accept adding "" to helix list for empty list ⇒ raise error if no helices specified.
      • gb_hierarchy_location: explicitly accept EMPTY path elements. used correctly to encode absolute vs relative paths.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.8 KB
Line 
1// ================================================================ //
2//                                                                  //
3//   File      : AW_modal.cxx                                       //
4//   Purpose   : Modal dialogs                                      //
5//                                                                  //
6//   Institute of Microbiology (Technical University Munich)        //
7//   http://www.arb-home.de/                                        //
8//                                                                  //
9// ================================================================ //
10
11#include <aw_window.hxx>
12#include <aw_global.hxx>
13#include <aw_file.hxx>
14#include <aw_awar.hxx>
15#include "aw_root.hxx"
16#include "aw_question.hxx"
17#include "aw_advice.hxx"
18#include "aw_msg.hxx"
19#include "aw_select.hxx"
20#include "aw_window_Xm.hxx"
21
22#include <arbdbt.h>
23#include <arb_strarray.h>
24
25#include <deque>
26#include <string>
27#include <algorithm>
28
29using namespace std;
30
31int aw_message_cb_result;
32
33void message_cb(AW_window*, int result) {
34    if (result == -1) { // exit
35        exit(EXIT_FAILURE);
36    }
37    aw_message_cb_result = result;
38}
39
40unsigned aw_message_timer_listen_event(AW_root *, AW_window *aww) {
41#if defined(TRACE_STATUS_MORE)
42    fprintf(stderr, "in aw_message_timer_listen_event\n"); fflush(stdout);
43#endif // TRACE_STATUS_MORE
44
45    if (aww->is_shown()) {
46        return AW_MESSAGE_LISTEN_DELAY;
47    }
48    return 0;
49}
50
51// -----------------
52//      aw_input
53
54static char *aw_input_cb_result = NULp;
55
56#define AW_INPUT_AWAR       "tmp/input/string"
57#define AW_INPUT_TITLE_AWAR "tmp/input/title"
58
59#define AW_FILE_SELECT_BASE        "tmp/file_select"
60#define AW_FILE_SELECT_DIR_AWAR    AW_FILE_SELECT_BASE "/directory"
61#define AW_FILE_SELECT_FILE_AWAR   AW_FILE_SELECT_BASE "/file_name"
62#define AW_FILE_SELECT_FILTER_AWAR AW_FILE_SELECT_BASE "/filter"
63#define AW_FILE_SELECT_TITLE_AWAR  AW_FILE_SELECT_BASE "/title"
64
65static void create_input_awars(AW_root *aw_root) {
66    aw_root->awar_string(AW_INPUT_TITLE_AWAR, "", AW_ROOT_DEFAULT);
67    aw_root->awar_string(AW_INPUT_AWAR,       "", AW_ROOT_DEFAULT);
68}
69
70static void create_fileSelection_awars(AW_root *aw_root) {
71    aw_root->awar_string(AW_FILE_SELECT_TITLE_AWAR, "", AW_ROOT_DEFAULT);
72    aw_root->awar_string(AW_FILE_SELECT_DIR_AWAR, "", AW_ROOT_DEFAULT);
73    aw_root->awar_string(AW_FILE_SELECT_FILE_AWAR, "", AW_ROOT_DEFAULT);
74    aw_root->awar_string(AW_FILE_SELECT_FILTER_AWAR, "", AW_ROOT_DEFAULT);
75}
76
77
78// -------------------------
79//      aw_input history
80
81static deque<string> input_history; // front contains newest entries
82
83#if defined(DEBUG)
84// # define TRACE_HISTORY
85#endif // DEBUG
86
87
88#if defined(TRACE_HISTORY)
89static void dumpHistory(const char *where) {
90    printf("History [%s]:\n", where);
91    for (deque<string>::iterator h = input_history.begin(); h != input_history.end(); ++h) {
92        printf("'%s'\n", h->c_str());
93    }
94}
95#endif // TRACE_HISTORY
96
97static void input_history_insert(const char *str, bool front) {
98    string s(str);
99
100    if (input_history.empty()) {
101        input_history.push_front(""); // insert an empty string into history
102    }
103    else {
104        deque<string>::iterator found = find(input_history.begin(), input_history.end(), s);
105        if (found != input_history.end()) {
106            input_history.erase(found);
107        }
108    }
109    if (front) {
110        input_history.push_front(s);
111    }
112    else {
113        input_history.push_back(s);
114    }
115
116#if defined(TRACE_HISTORY)
117    dumpHistory(GBS_global_string("input_history_insert('%s', front=%i)", str, front));
118#endif // TRACE_HISTORY
119}
120
121void input_history_cb(AW_window *aw, int mode) {
122    // mode: -1 = '<' +1 = '>'
123    AW_root *aw_root = aw->get_root();
124    AW_awar *awar    = aw_root->awar(AW_INPUT_AWAR);
125    char    *content = awar->read_string();
126
127    if (content) input_history_insert(content, mode == 1);
128
129    if (!input_history.empty()) {
130        if (mode == -1) {
131            string s = input_history.front();
132            awar->write_string(s.c_str());
133            input_history.pop_front();
134            input_history.push_back(s);
135        }
136        else {
137            string s = input_history.back();
138            awar->write_string(s.c_str());
139            input_history.pop_back();
140            input_history.push_front(s);
141        }
142    }
143
144#if defined(TRACE_HISTORY)
145    dumpHistory(GBS_global_string("input_history_cb(mode=%i)", mode));
146#endif // TRACE_HISTORY
147
148    free(content);
149}
150
151void input_cb(AW_window *aw, int buttonNr) {
152    // any previous contents were passed to client (who is responsible to free the resources)
153    // so DON'T free aw_input_cb_result here:
154    aw_input_cb_result = NULp;
155
156    if (buttonNr >= 0) { // <0 = cancel button -> no result
157        // create heap-copy of result -> client will get the owner
158        aw_input_cb_result = aw->get_root()->awar(AW_INPUT_AWAR)->read_as_string();
159    }
160}
161
162void file_selection_cb(AW_window *aw, int ok_cancel_flag) {
163    // any previous contents were passed to client (who is responsible to free the resources)
164    // so DON'T free aw_input_cb_result here:
165    aw_input_cb_result = NULp;
166
167    if (ok_cancel_flag >= 0) { // <0 = cancel button -> no result
168        // create heap-copy of result -> client will get the owner
169        aw_input_cb_result = aw->get_root()->awar(AW_FILE_SELECT_FILE_AWAR)->read_as_string();
170    }
171}
172
173#define INPUT_SIZE 50           // size of input prompts in aw_input
174
175static AW_window_message *new_input_window(AW_root *root, const char *title, const char *buttons) {
176    // helper for aw_input
177    //
178    // 'buttons': comma separated list of button names
179    // - a buttonname starting with - marks the abort button (only one possible)
180    // - buttonnames starting with \n force a newline
181    // (has to be '-\nNAME' if combined)
182
183    aw_assert(buttons);
184
185    AW_window_message *aw_msg = new AW_window_message;
186
187    aw_msg->init(root, title, false);
188
189    aw_msg->label_length(0);
190    aw_msg->auto_space(10, 10);
191
192    aw_msg->at(10, 10);
193    aw_msg->button_length(INPUT_SIZE+1);
194    aw_msg->create_button(NULp, AW_INPUT_TITLE_AWAR);
195
196    aw_msg->at_newline();
197    aw_msg->create_input_field(AW_INPUT_AWAR, INPUT_SIZE);
198
199    ConstStrArray button_names;
200    int           maxlen = 0; // track max. button length (used as min.length for buttons)
201
202    arb_assert(buttons[0]); // check whether using SPLIT_DROPEMPTY would be better here. Currently will generate an array containing one empty string.
203    GBT_split_string(button_names, buttons, ',');
204    int butCount = button_names.size();
205
206    int abortButton = -1; // index of abort button (-1 means 'none')
207    for (int b = 0; b<butCount; b++) {
208        if (button_names[b][0] == '-') {
209            aw_assert(abortButton<0); // only one abort button possible!
210            abortButton        = b;
211            button_names.replace(b, button_names[b]+1); // point behind '-'
212        }
213        int len = strlen(button_names[b]);
214        if (len>maxlen) maxlen = len;
215    }
216
217    aw_msg->button_length(maxlen+1);
218
219#define MAXBUTTONSPERLINE 5
220
221    aw_msg->at_newline();
222    aw_msg->callback(makeWindowCallback(input_history_cb, -1)); aw_msg->create_button("bwd", "<<");
223    aw_msg->callback(makeWindowCallback(input_history_cb,  1)); aw_msg->create_button("fwd", ">>");
224    int thisLine = 2;
225
226    if (butCount>(MAXBUTTONSPERLINE-thisLine) && butCount <= MAXBUTTONSPERLINE) { // approx. 5 buttons (2+3) fit into one line
227        aw_msg->at_newline();
228        thisLine = 0;
229    }
230
231    for (int b = 0; b<butCount; b++) {
232        const char *name    = button_names[b];
233        bool        forceLF = name[0] == '\n';
234
235        if (thisLine >= MAXBUTTONSPERLINE || forceLF) {
236            aw_msg->at_newline();
237            thisLine = 0;
238            if (forceLF) name++;
239        }
240
241        // use 0 as result for 1st button, 1 for 2nd button, etc.
242        // use -1 for abort button
243        int resultCode = b == abortButton ? -1 : b;
244        aw_msg->callback(makeWindowCallback(input_cb, resultCode));
245        aw_msg->create_button(name, name, "");
246        thisLine++;
247    }
248
249    return aw_msg;
250}
251
252char *aw_input(const char *title, const char *prompt, const char *default_input) {
253    // Please DO NOT USE (see #179 or AWT_activate_prompt)!
254    //
255    // prompt user to enter a string
256    //
257    // title         = title of window
258    // prompt        = question
259    // default_input = default for answer (NULp -> "")
260    //
261    // result is NULp, if cancel was pressed
262    // otherwise result contains the user input (maybe an empty string)
263
264    static AW_window_message *aw_msg = NULp;
265
266    AW_root *root = AW_root::SINGLETON;
267    if (!aw_msg) create_input_awars(root); // first call -> create awars
268
269    root->awar(AW_INPUT_TITLE_AWAR)->write_string(prompt);
270    aw_assert(strlen(prompt) <= INPUT_SIZE);
271
272    AW_awar *inAwar = root->awar(AW_INPUT_AWAR);
273    if (default_input) {
274        input_history_insert(default_input, true); // insert default into history
275        inAwar->write_string(default_input);
276    }
277    else {
278        inAwar->write_string("");
279    }
280
281    aw_assert(GB_get_transaction_level(inAwar->gb_var) <= 0); // otherwise history would not work
282
283    if (!aw_msg) aw_msg = new_input_window(root, title, "Ok,-Abort");
284    else aw_msg->set_window_title(title);
285
286    aw_msg->window_fit();
287    aw_msg->show_modal();
288    char dummy[]       = "";
289    aw_input_cb_result = dummy;
290
291    root->add_timed_callback_never_disabled(AW_MESSAGE_LISTEN_DELAY, makeTimedCallback(aw_message_timer_listen_event, static_cast<AW_window*>(aw_msg)));
292    {
293        LocallyModify<bool> flag(root->disable_callbacks, true);
294        while (aw_input_cb_result == dummy) {
295            root->process_events();
296        }
297    }
298    aw_msg->hide();
299
300    if (aw_input_cb_result) input_history_insert(aw_input_cb_result, true);
301    return aw_input_cb_result;
302}
303
304char *aw_input(const char *prompt, const char *default_input) {
305    return aw_input("Enter string", prompt, default_input);
306}
307
308// --------------------------
309//      aw_modal_file_selection
310
311char *aw_modal_file_selection(const char *title, const char *dir, const char *def_name, const char *suffix) {
312    // Warning: this dialog is modal (i.e. not macro-capable)
313
314    AW_root *root = AW_root::SINGLETON;
315
316    static AW_window_simple *aw_msg = NULp;
317    if (!aw_msg) create_fileSelection_awars(root);
318
319    {
320        char *edir      = GBS_eval_env(dir);
321        char *edef_name = GBS_eval_env(def_name);
322
323        root->awar(AW_FILE_SELECT_TITLE_AWAR) ->write_string(title);
324        root->awar(AW_FILE_SELECT_DIR_AWAR)   ->write_string(edir);
325        root->awar(AW_FILE_SELECT_FILE_AWAR)  ->write_string(edef_name);
326        root->awar(AW_FILE_SELECT_FILTER_AWAR)->write_string(suffix);
327
328        free(edef_name);
329        free(edir);
330    }
331
332    if (!aw_msg) {
333        aw_msg = new AW_window_simple;
334
335        aw_msg->init(root, "AW_FILE_SELECTION", "File selection");
336        aw_msg->allow_delete_window(false); // disable closing the window
337
338        aw_msg->load_xfig("fileselect.fig");
339
340        aw_msg->at("title");
341        aw_msg->create_button(NULp, AW_FILE_SELECT_TITLE_AWAR);
342
343        AW_create_standard_fileselection(aw_msg, AW_FILE_SELECT_BASE);
344
345        aw_msg->button_length(7);
346
347        aw_msg->at("ok");
348        aw_msg->callback(makeWindowCallback(file_selection_cb, 0));
349        aw_msg->create_button("OK", "OK", "O");
350
351        aw_msg->at("cancel");
352        aw_msg->callback(makeWindowCallback(file_selection_cb, -1));
353        aw_msg->create_button("CANCEL", "CANCEL", "C");
354
355        aw_msg->window_fit();
356    }
357
358    aw_msg->show_modal();
359    char dummy[] = "";
360    aw_input_cb_result = dummy;
361
362    root->add_timed_callback_never_disabled(AW_MESSAGE_LISTEN_DELAY, makeTimedCallback(aw_message_timer_listen_event, static_cast<AW_window*>(aw_msg)));
363    {
364        LocallyModify<bool> flag(root->disable_callbacks, true);
365        while (aw_input_cb_result == dummy) {
366            root->process_events();
367        }
368    }
369    aw_msg->hide();
370
371    return aw_input_cb_result;
372}
373
374
Note: See TracBrowser for help on using the repository browser.