ARB
AWT_prompt.cxx
Go to the documentation of this file.
1 // =============================================================== //
2 // //
3 // File : AWT_prompt.cxx //
4 // Purpose : prompt for text (macro-compatible) //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in October 2017 //
7 // http://www.arb-home.de/ //
8 // //
9 // =============================================================== //
10 
11 #include "awt_prompt.hxx"
12 
13 #include <aw_window.hxx>
14 #include <aw_root.hxx>
15 #include <aw_awar.hxx>
16 
17 #define PROMPT_AWAR_PREFIX "tmp/prompt/"
18 
19 #define AWAR_PROMPT_TEXT PROMPT_AWAR_PREFIX "text"
20 #define AWAR_PROMPT_INPUT PROMPT_AWAR_PREFIX "input"
21 #define AWAR_PROMPT_BUTTON PROMPT_AWAR_PREFIX "button"
22 #define AWAR_PROMPT_ERROR PROMPT_AWAR_PREFIX "error"
23 
24 struct PromptData : virtual Noncopyable {
25  ResultHandler handler;
26  const char *helpfile;
28 
29  PromptData(const ResultHandler& handler_) : handler(handler_), helpfile(NULp) {}
30 };
31 
32 static GB_ERROR hidden_ok_pressed() { return "disabled"; }
33 
34 static void ok_pressed(AW_window *aww, PromptData *pdata) {
35  AW_root *awr = aww->get_root();
36  char *input = awr->awar(AWAR_PROMPT_INPUT)->read_string();
37 
38  GB_ERROR error = pdata->handler(input);
39  if (error) {
40  awr->awar(AWAR_PROMPT_ERROR)->write_string(error);
41  }
42  else {
43  pdata->handler = makeResultHandler(hidden_ok_pressed);
44  aww->hide(); // close prompter when handled
45  }
46 
47  free(input);
48 }
49 static void run_help_cb(AW_window *aww, const PromptData *pdata) {
50  if (pdata->helpfile) {
51  AW_help_popup(NULp, pdata->helpfile);
52  }
53  else {
54  aww->get_root()->awar(AWAR_PROMPT_ERROR)->write_string("No help :-(");
55  }
56 }
57 
58 static void clear_input_cb(AW_window *aww) {
60 }
61 
62 void AWT_activate_prompt(const char *title, const char *prompt, const char *defaultResult, const char *button, const ResultHandler& handle_result, const char *helpfile, const char *srt) {
79  static AW_window_simple *aws = NULp; // one window used for all prompts
80  static PromptData pdata(handle_result);
81 
82  pdata.handler = handle_result;
83  pdata.helpfile = helpfile;
84 
86  if (!aws) {
87  awr->awar_string(AWAR_PROMPT_TEXT, "");
89  awr->awar_string(AWAR_PROMPT_BUTTON, "OK");
91 
92  aws = new AW_window_simple;
93  aws->init(awr, "PROMPT", "Prompt");
94 
95  const int PAD = 10;
96  const int IF_YSIZE = 32; // lineheight of attached input field
97  const int TX_YSIZE = 18; // lineheight of text (button w/o cb)
98  const int BT_YSIZE = 22; // lineheight of button
99 
100  aws->at(PAD, PAD);
101  aws->auto_space(PAD/2, PAD/2);
102 
103  aws->at_attach_to(true, false, -PAD, TX_YSIZE);
104  aws->create_button(NULp, AWAR_PROMPT_TEXT);
105  aws->at_unattach();
106 
107  aws->at_newline();
108 
109  WindowCallback ok_cb = makeWindowCallback(ok_pressed, &pdata);
110 
111  aws->at_attach_to(true, false, -PAD, IF_YSIZE);
112  aws->d_callback(ok_cb); // enable ENTER in textfield to press "ENTER"-button
113  aws->create_input_field(AWAR_PROMPT_INPUT, 50);
114  aws->at_unattach();
115 
116  aws->at_shift(300, 0); // defines minimum window width
117 
118  aws->at_newline();
119 
120  aws->button_length(8);
121 
122  aws->callback(ok_cb);
123  aws->highlight();
124  aws->create_button("ENTER", AWAR_PROMPT_BUTTON, NULp, "+");
125 
126  int xb1 = aws->get_at_xposition();
127 
128  aws->callback(AW_POPDOWN);
129  aws->create_button("CANCEL", "Cancel");
130 
131  int xb2 = aws->get_at_xposition();
132  int bwidth = xb2 - xb1 - PAD;
133 
134  aws->at_attach(-bwidth-PAD, 0);
135  aws->at_attach_to(true, false, -PAD, BT_YSIZE);
136  aws->callback(clear_input_cb);
137  aws->create_button("CLEAR", "Clear");
138 
139  aws->at_attach(2*(-bwidth-PAD), 0);
140  aws->at_attach_to(true, false, -bwidth-2*PAD, BT_YSIZE);
141  aws->callback(makeWindowCallback(run_help_cb, &pdata));
142  aws->create_button("HELP", "Help");
143  aws->at_unattach();
144 
145  pdata.help_button = aws->get_last_widget();
146 
147  aws->at_newline();
148  const int ERR_YSIZE = 2*TX_YSIZE;
149  aws->at_shift(0, ERR_YSIZE); // defines minimum height of error display
150  aws->at_shift(0, -ERR_YSIZE); // go back
151 
152  aws->at_attach_to(true, true, -PAD, -PAD);
153  aws->create_button(NULp, AWAR_PROMPT_ERROR, NULp, "-");
154  aws->at_unattach();
155 
156  aws->recalc_pos_atShow(AW_REPOS_TO_MOUSE);
157  }
158  else {
159  aws->hide(); // hide previously displayed prompt
160  }
161 
162  // configure active prompt:
163  aws->set_window_title(title);
164  awr->set_active(pdata.help_button, pdata.helpfile);
165 
166  awr->awar(AWAR_PROMPT_TEXT) ->write_string(prompt);
167  awr->awar(AWAR_PROMPT_BUTTON)->write_string(button);
168  awr->awar(AWAR_PROMPT_ERROR) ->write_string("");
169 
170  AW_awar *awar_input = awr->awar(AWAR_PROMPT_INPUT);
171  awar_input->set_srt(srt); // set or reset SRT
172  awar_input->write_string(defaultResult);
173 
174  aws->recalc_size_atShow(AW_RESIZE_USER); // allows user size + forces minimum size (when shrinked)
175 
176  aws->activate();
177 }
ResultHandler handler
Definition: AWT_prompt.cxx:25
const char * GB_ERROR
Definition: arb_core.h:25
AW_awar * set_srt(const char *srt)
Definition: AW_awar.cxx:569
#define AWAR_PROMPT_BUTTON
Definition: AWT_prompt.cxx:21
const char * title
Definition: readseq.c:22
void AW_POPDOWN(AW_window *window)
Definition: AW_window.cxx:52
PromptData(const ResultHandler &handler_)
Definition: AWT_prompt.cxx:29
#define AWAR_PROMPT_ERROR
Definition: AWT_prompt.cxx:22
static AW_root * SINGLETON
Definition: aw_root.hxx:102
const char * helpfile
Definition: AWT_prompt.cxx:26
static GB_ERROR hidden_ok_pressed()
Definition: AWT_prompt.cxx:32
static void error(const char *msg)
Definition: mkptypes.cxx:96
void set_active(Widget w, bool active)
Definition: AW_window.cxx:3261
char * read_string() const
Definition: AW_awar.cxx:201
AW_awar * awar(const char *awar)
Definition: AW_root.cxx:554
#define AWAR_PROMPT_INPUT
Definition: AWT_prompt.cxx:20
static void run_help_cb(AW_window *aww, const PromptData *pdata)
Definition: AWT_prompt.cxx:49
static void ok_pressed(AW_window *aww, PromptData *pdata)
Definition: AWT_prompt.cxx:34
void hide()
Definition: AW_window.cxx:1807
AW_root * get_root()
Definition: aw_window.hxx:348
#define NULp
Definition: cxxforward.h:97
void AW_help_popup(UNFIXED, const char *help_file)
Definition: AW_help.cxx:622
GB_ERROR write_string(const char *aw_string)
static void clear_input_cb(AW_window *aww)
Definition: AWT_prompt.cxx:58
AW_awar * awar_string(const char *var_name, const char *default_value="", AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:570
Widget help_button
Definition: AWT_prompt.cxx:27
#define AWAR_PROMPT_TEXT
Definition: AWT_prompt.cxx:19
struct _WidgetRec * Widget
Definition: aw_base.hxx:48
void AWT_activate_prompt(const char *title, const char *prompt, const char *defaultResult, const char *button, const ResultHandler &handle_result, const char *helpfile, const char *srt)
Definition: AWT_prompt.cxx:62