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  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", "<<");
222  aw_msg->callback(makeWindowCallback(input_history_cb, 1)); aw_msg->create_button("fwd", ">>");
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 
251 char *aw_input(const char *title, const char *prompt, const char *default_input) {
252  // Please DO NOT USE (see #179 or AWT_activate_prompt)!
253  //
254  // prompt user to enter a string
255  //
256  // title = title of window
257  // prompt = question
258  // default_input = default for answer (NULp -> "")
259  //
260  // result is NULp, if cancel was pressed
261  // otherwise result contains the user input (maybe an empty string)
262 
263  static AW_window_message *aw_msg = NULp;
264 
265  AW_root *root = AW_root::SINGLETON;
266  if (!aw_msg) create_input_awars(root); // first call -> create awars
267 
268  root->awar(AW_INPUT_TITLE_AWAR)->write_string(prompt);
269  aw_assert(strlen(prompt) <= INPUT_SIZE);
270 
271  AW_awar *inAwar = root->awar(AW_INPUT_AWAR);
272  if (default_input) {
273  input_history_insert(default_input, true); // insert default into history
274  inAwar->write_string(default_input);
275  }
276  else {
277  inAwar->write_string("");
278  }
279 
280  aw_assert(GB_get_transaction_level(inAwar->gb_var) <= 0); // otherwise history would not work
281 
282  if (!aw_msg) aw_msg = new_input_window(root, title, "Ok,-Abort");
283  else aw_msg->set_window_title(title);
284 
285  aw_msg->window_fit();
286  aw_msg->show_modal();
287  char dummy[] = "";
288  aw_input_cb_result = dummy;
289 
290  root->add_timed_callback_never_disabled(AW_MESSAGE_LISTEN_DELAY, makeTimedCallback(aw_message_timer_listen_event, static_cast<AW_window*>(aw_msg)));
291  {
293  while (aw_input_cb_result == dummy) {
294  root->process_events();
295  }
296  }
297  aw_msg->hide();
298 
299  if (aw_input_cb_result) input_history_insert(aw_input_cb_result, true);
300  return aw_input_cb_result;
301 }
302 
303 char *aw_input(const char *prompt, const char *default_input) {
304  return aw_input("Enter string", prompt, default_input);
305 }
306 
307 // --------------------------
308 // aw_file_selection
309 
310 char *aw_file_selection(const char *title, const char *dir, const char *def_name, const char *suffix) {
311  AW_root *root = AW_root::SINGLETON;
312 
313  static AW_window_simple *aw_msg = NULp;
314  if (!aw_msg) create_fileSelection_awars(root);
315 
316  {
317  char *edir = GBS_eval_env(dir);
318  char *edef_name = GBS_eval_env(def_name);
319 
322  root->awar(AW_FILE_SELECT_FILE_AWAR) ->write_string(edef_name);
324 
325  free(edef_name);
326  free(edir);
327  }
328 
329  if (!aw_msg) {
330  aw_msg = new AW_window_simple;
331 
332  aw_msg->init(root, "AW_FILE_SELECTION", "File selection");
333  aw_msg->allow_delete_window(false); // disable closing the window
334 
335  aw_msg->load_xfig("fileselect.fig");
336 
337  aw_msg->at("title");
338  aw_msg->create_button(NULp, AW_FILE_SELECT_TITLE_AWAR);
339 
341 
342  aw_msg->button_length(7);
343 
344  aw_msg->at("ok");
345  aw_msg->callback(makeWindowCallback(file_selection_cb, 0));
346  aw_msg->create_button("OK", "OK", "O");
347 
348  aw_msg->at("cancel");
349  aw_msg->callback(makeWindowCallback(file_selection_cb, -1));
350  aw_msg->create_button("CANCEL", "CANCEL", "C");
351 
352  aw_msg->window_fit();
353  }
354 
355  aw_msg->show_modal();
356  char dummy[] = "";
357  aw_input_cb_result = dummy;
358 
359  root->add_timed_callback_never_disabled(AW_MESSAGE_LISTEN_DELAY, makeTimedCallback(aw_message_timer_listen_event, static_cast<AW_window*>(aw_msg)));
360  {
362  while (aw_input_cb_result == dummy) {
363  root->process_events();
364  }
365  }
366  aw_msg->hide();
367 
368  return aw_input_cb_result;
369 }
370 
371 
string result
void button_length(int length)
Definition: AW_at.cxx:283
#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:204
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:2895
void add_timed_callback_never_disabled(int ms, const TimedCallback &tcb)
Definition: AW_root.cxx:541
void set_window_title(const char *title)
Definition: AW_window.cxx:1066
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
void GBT_split_string(ConstStrArray &dest, const char *namelist, const char *separator, bool dropEmptyTokens)
Definition: arb_strarray.h:232
unsigned aw_message_timer_listen_event(AW_root *, AW_window *aww)
Definition: AW_modal.cxx:40
char * aw_file_selection(const char *title, const char *dir, const char *def_name, const char *suffix)
Definition: AW_modal.cxx:310
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:1801
#define AW_INPUT_TITLE_AWAR
Definition: AW_modal.cxx:57
void process_events()
Definition: AW_root.cxx:42
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:201
AW_awar * awar(const char *awar)
Definition: AW_root.cxx:554
void window_fit()
Definition: AW_window.cxx:1083
#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:855
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:1622
void hide()
Definition: AW_window.cxx:1807
AW_root * get_root()
Definition: aw_window.hxx:348
#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
#define NULp
Definition: cxxforward.h:97
#define AW_FILE_SELECT_DIR_AWAR
Definition: AW_modal.cxx:60
GB_ERROR write_string(const char *aw_string)
int GB_get_transaction_level(GBDATA *gbd)
Definition: arbdb.cxx:2578
void callback(const WindowCallback &cb)
Definition: AW_window.cxx:130
AW_awar * awar_string(const char *var_name, const char *default_value="", AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:570
char * aw_input(const char *title, const char *prompt, const char *default_input)
Definition: AW_modal.cxx:251
void label_length(int length)
Definition: AW_at.cxx:279
void at_newline()
Definition: AW_at.cxx:119
const char * replace(int i, const char *elem)
Definition: arb_strarray.h:217
#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:446
void input_history_cb(AW_window *aw, int mode)
Definition: AW_modal.cxx:121
GB_write_int const char s
Definition: AW_awar.cxx:156