source: tags/testbuild/WINDOW/AW_modal.cxx

Last change on this file was 12850, checked in by westram, 10 years ago
  • remove aw_string_selection and related code
  • fix signed warnings in new_input_window
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.5 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 = 0;
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 = 0;
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 = 0;
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(0, 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    GBT_split_string(button_names, buttons, ',');
203    int butCount = button_names.size();
204
205    int abortButton = -1; // index of abort button (-1 means 'none')
206    for (int b = 0; b<butCount; b++) {
207        if (button_names[b][0] == '-') {
208            aw_assert(abortButton<0); // only one abort button possible!
209            abortButton        = b;
210            button_names.replace(b, button_names[b]+1); // point behind '-'
211        }
212        int len = strlen(button_names[b]);
213        if (len>maxlen) maxlen = len;
214    }
215
216    aw_msg->button_length(maxlen+1);
217
218#define MAXBUTTONSPERLINE 5
219
220    aw_msg->at_newline();
221    aw_msg->callback(makeWindowCallback(input_history_cb, -1)); aw_msg->create_button("bwd", "<<", 0);
222    aw_msg->callback(makeWindowCallback(input_history_cb,  1)); aw_msg->create_button("fwd", ">>", 0);
223    int thisLine = 2;
224
225    if (butCount>(MAXBUTTONSPERLINE-thisLine) && butCount <= MAXBUTTONSPERLINE) { // approx. 5 buttons (2+3) fit into one line
226        aw_msg->at_newline();
227        thisLine = 0;
228    }
229
230    for (int b = 0; b<butCount; b++) {
231        const char *name    = button_names[b];
232        bool        forceLF = name[0] == '\n';
233
234        if (thisLine >= MAXBUTTONSPERLINE || forceLF) {
235            aw_msg->at_newline();
236            thisLine = 0;
237            if (forceLF) name++;
238        }
239
240        // use 0 as result for 1st button, 1 for 2nd button, etc.
241        // use -1 for abort button
242        int resultCode = b == abortButton ? -1 : b;
243        aw_msg->callback(makeWindowCallback(input_cb, resultCode));
244        aw_msg->create_button(name, name, "");
245        thisLine++;
246    }
247
248    return aw_msg;
249}
250
251char *aw_input(const char *title, const char *prompt, const char *default_input) {
252    // prompt user to enter a string
253    //
254    // title         = title of window
255    // prompt        = question
256    // default_input = default for answer (NULL -> "")
257    //
258    // result is NULL, if cancel was pressed
259    // otherwise result contains the user input (maybe an empty string)
260
261    static AW_window_message *aw_msg = 0;
262
263    AW_root *root = AW_root::SINGLETON;
264    if (!aw_msg) create_input_awars(root); // first call -> create awars
265
266    root->awar(AW_INPUT_TITLE_AWAR)->write_string(prompt);
267    aw_assert(strlen(prompt) <= INPUT_SIZE);
268
269    AW_awar *inAwar = root->awar(AW_INPUT_AWAR);
270    if (default_input) {
271        input_history_insert(default_input, true); // insert default into history
272        inAwar->write_string(default_input);
273    }
274    else {
275        inAwar->write_string("");
276    }
277
278    aw_assert(GB_get_transaction_level(inAwar->gb_var) <= 0); // otherwise history would not work
279
280    if (!aw_msg) aw_msg = new_input_window(root, title, "Ok,-Abort");
281    else aw_msg->set_window_title(title);
282
283    aw_msg->window_fit();
284    aw_msg->show_modal();
285    char dummy[]       = "";
286    aw_input_cb_result = dummy;
287
288    root->add_timed_callback_never_disabled(AW_MESSAGE_LISTEN_DELAY, makeTimedCallback(aw_message_timer_listen_event, static_cast<AW_window*>(aw_msg)));
289    {
290        LocallyModify<bool> flag(root->disable_callbacks, true);
291        while (aw_input_cb_result == dummy) {
292            root->process_events();
293        }
294    }
295    aw_msg->hide();
296
297    if (aw_input_cb_result) input_history_insert(aw_input_cb_result, true);
298    return aw_input_cb_result;
299}
300
301char *aw_input(const char *prompt, const char *default_input) {
302    return aw_input("Enter string", prompt, default_input);
303}
304
305// --------------------------
306//      aw_file_selection
307
308char *aw_file_selection(const char *title, const char *dir, const char *def_name, const char *suffix) {
309    AW_root *root = AW_root::SINGLETON;
310
311    static AW_window_simple *aw_msg = 0;
312    if (!aw_msg) create_fileSelection_awars(root);
313
314    {
315        char *edir      = GBS_eval_env(dir);
316        char *edef_name = GBS_eval_env(def_name);
317
318        root->awar(AW_FILE_SELECT_TITLE_AWAR) ->write_string(title);
319        root->awar(AW_FILE_SELECT_DIR_AWAR)   ->write_string(edir);
320        root->awar(AW_FILE_SELECT_FILE_AWAR)  ->write_string(edef_name);
321        root->awar(AW_FILE_SELECT_FILTER_AWAR)->write_string(suffix);
322
323        free(edef_name);
324        free(edir);
325    }
326
327    if (!aw_msg) {
328        aw_msg = new AW_window_simple;
329
330        aw_msg->init(root, "AW_FILE_SELECTION", "File selection");
331        aw_msg->allow_delete_window(false); // disable closing the window
332
333        aw_msg->load_xfig("fileselect.fig");
334
335        aw_msg->at("title");
336        aw_msg->create_button(0, AW_FILE_SELECT_TITLE_AWAR);
337
338        AW_create_standard_fileselection(aw_msg, AW_FILE_SELECT_BASE);
339
340        aw_msg->button_length(7);
341
342        aw_msg->at("ok");
343        aw_msg->callback(makeWindowCallback(file_selection_cb, 0));
344        aw_msg->create_button("OK", "OK", "O");
345
346        aw_msg->at("cancel");
347        aw_msg->callback(makeWindowCallback(file_selection_cb, -1));
348        aw_msg->create_button("CANCEL", "CANCEL", "C");
349
350        aw_msg->window_fit();
351    }
352
353    aw_msg->show_modal();
354    char dummy[] = "";
355    aw_input_cb_result = dummy;
356
357    root->add_timed_callback_never_disabled(AW_MESSAGE_LISTEN_DELAY, makeTimedCallback(aw_message_timer_listen_event, static_cast<AW_window*>(aw_msg)));
358    {
359        LocallyModify<bool> flag(root->disable_callbacks, true);
360        while (aw_input_cb_result == dummy) {
361            root->process_events();
362        }
363    }
364    aw_msg->hide();
365
366    return aw_input_cb_result;
367}
368
369
Note: See TracBrowser for help on using the repository browser.