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; |
---|
27 | Widget help_button; |
---|
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) { |
---|
59 | aww->get_root()->awar(AWAR_PROMPT_INPUT)->write_string(""); |
---|
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) { |
---|
63 | /*! pops up an text input prompter |
---|
64 | * |
---|
65 | * (macro-capable replacement for aw_input) |
---|
66 | * |
---|
67 | * @param title window title |
---|
68 | * @param prompt question or request shown above input field |
---|
69 | * @param defaultResult default value for input field |
---|
70 | * @param button text shown on "ENTER" button (maxlen: 7) |
---|
71 | * @param handle_result called with entered result (even if empty) |
---|
72 | * @param helpfile context sensitive helpfile (defaults to NULp) |
---|
73 | * @param srt SRT command will be applied to userinput (defaults to NULp; should not be dynamic) |
---|
74 | * Use e.g. to correct name-prefixes like "tree_" or "ali_" |
---|
75 | * |
---|
76 | * Note that all activated prompts use the same window. Activating a second |
---|
77 | * prompt (while the user did not answer the first one), will hide the first prompt. |
---|
78 | */ |
---|
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 | |
---|
85 | AW_root *awr = AW_root::SINGLETON; |
---|
86 | if (!aws) { |
---|
87 | awr->awar_string(AWAR_PROMPT_TEXT, ""); |
---|
88 | awr->awar_string(AWAR_PROMPT_INPUT, ""); |
---|
89 | awr->awar_string(AWAR_PROMPT_BUTTON, "OK"); |
---|
90 | awr->awar_string(AWAR_PROMPT_ERROR, ""); |
---|
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 | } |
---|