ARB
AW_modal.cxx
Go to the documentation of this file.
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 
29 using namespace std;
30 
32 
34  if (result == -1) { // exit
35  exit(EXIT_FAILURE);
36  }
37  aw_message_cb_result = result;
38 }
39 
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()) {
47  }
48  return 0;
49 }
50 
51 // -----------------
52 // aw_input
53 
54 static 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 
65 static void create_input_awars(AW_root *aw_root) {
68 }
69 
70 static void create_fileSelection_awars(AW_root *aw_root) {
75 }
76 
77 
78 // -------------------------
79 // aw_input history
80 
81 static 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)
89 static 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 
97 static 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 
121 void 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 
151 void 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 
162 void 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 
175 static 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);
195 
196  aw_msg->at_newline();
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 
252 char *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  {
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 
304 char *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 
311 char *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 
325  root->awar(AW_FILE_SELECT_FILE_AWAR) ->write_string(edef_name);
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 
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  {
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 
#define arb_assert(cond)
Definition: arb_assert.h:245
string result
void button_length(int length)
Definition: AW_at.cxx:288
#define AW_INPUT_AWAR
Definition: AW_modal.cxx:56
size_t size() const
Definition: arb_strarray.h:85
#define AW_MESSAGE_LISTEN_DELAY
#define AW_FILE_SELECT_TITLE_AWAR
Definition: AW_modal.cxx:63
void at(int x, int y)
Definition: AW_at.cxx:93
void input_cb(AW_window *aw, int buttonNr)
Definition: AW_modal.cxx:151
void AW_create_standard_fileselection(AW_window *aws, const char *awar_prefix)
Definition: aw_file.hxx:30
static AW_window_message * new_input_window(AW_root *root, const char *title, const char *buttons)
Definition: AW_modal.cxx:175
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:203
const char * title
Definition: readseq.c:22
STL namespace.
void init(AW_root *root_in, const char *wid, const char *windowname, bool allow_close)
Definition: AW_window.cxx:2944
void add_timed_callback_never_disabled(int ms, const TimedCallback &tcb)
Definition: AW_root.cxx:555
void set_window_title(const char *title)
Definition: AW_window.cxx:1078
static void input_history_insert(const char *str, bool front)
Definition: AW_modal.cxx:97
static char * aw_input_cb_result
Definition: AW_modal.cxx:54
unsigned aw_message_timer_listen_event(AW_root *, AW_window *aww)
Definition: AW_modal.cxx:40
static AW_root * SINGLETON
Definition: aw_root.hxx:102
fflush(stdout)
void message_cb(AW_window *, int result)
Definition: AW_modal.cxx:33
static deque< string > input_history
Definition: AW_modal.cxx:81
#define aw_assert(bed)
Definition: aw_position.hxx:29
void show_modal()
Definition: AW_window.cxx:1850
#define AW_INPUT_TITLE_AWAR
Definition: AW_modal.cxx:57
void process_events()
Definition: AW_root.cxx:43
char * read_as_string() const
void file_selection_cb(AW_window *aw, int ok_cancel_flag)
Definition: AW_modal.cxx:162
char * read_string() const
Definition: AW_awar.cxx:198
AW_awar * awar(const char *awar)
Definition: AW_root.cxx:568
void window_fit()
Definition: AW_window.cxx:1095
#define INPUT_SIZE
Definition: AW_modal.cxx:173
#define EXIT_FAILURE
Definition: arb_a2ps.c:157
#define AW_FILE_SELECT_BASE
Definition: AW_modal.cxx:59
long int flag
Definition: f2c.h:39
int aw_message_cb_result
Definition: AW_modal.cxx:31
void create_input_field(const char *awar_name, int columns=0)
Definition: AW_button.cxx:857
void auto_space(int xspace, int yspace)
Definition: AW_at.cxx:259
static void create_input_awars(AW_root *aw_root)
Definition: AW_modal.cxx:65
GBDATA * gb_var
Definition: aw_awar.hxx:97
bool disable_callbacks
Definition: aw_root.hxx:114
#define AW_FILE_SELECT_FILE_AWAR
Definition: AW_modal.cxx:61
bool is_shown() const
Definition: AW_window.cxx:1668
void hide()
Definition: AW_window.cxx:1856
AW_root * get_root()
Definition: aw_window.hxx:359
#define MAXBUTTONSPERLINE
#define AW_FILE_SELECT_FILTER_AWAR
Definition: AW_modal.cxx:62
char * GBS_eval_env(GB_CSTR p)
Definition: adstring.cxx:212
static void create_fileSelection_awars(AW_root *aw_root)
Definition: AW_modal.cxx:70
char * aw_modal_file_selection(const char *title, const char *dir, const char *def_name, const char *suffix)
Definition: AW_modal.cxx:311
#define NULp
Definition: cxxforward.h:116
#define AW_FILE_SELECT_DIR_AWAR
Definition: AW_modal.cxx:60
GB_ERROR write_string(const char *aw_string)
void GBT_split_string(ConstStrArray &dest, const char *namelist, const char *separator, SplitMode mode)
Definition: arb_strarray.h:223
int GB_get_transaction_level(GBDATA *gbd)
Definition: arbdb.cxx:2590
void callback(const WindowCallback &cb)
Definition: AW_window.cxx:142
AW_awar * awar_string(const char *var_name, const char *default_value="", AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:584
char * aw_input(const char *title, const char *prompt, const char *default_input)
Definition: AW_modal.cxx:252
void label_length(int length)
Definition: AW_at.cxx:284
void at_newline()
Definition: AW_at.cxx:119
const char * replace(int i, const char *elem)
Definition: arb_strarray.h:206
#define AW_ROOT_DEFAULT
Definition: aw_base.hxx:106
void create_button(const char *macro_name, AW_label label, const char *mnemonic=NULp, const char *color=NULp)
Definition: AW_button.cxx:448
void input_history_cb(AW_window *aw, int mode)
Definition: AW_modal.cxx:121
GB_write_int const char s
Definition: AW_awar.cxx:154