This source file includes following definitions.
- dlg_default_get_colors
- refresh_cmd
- dlg_help
- dlg_execute_cmd
- dlg_handle_key
- dlg_key_event
- dlg_handle_mouse_event
- frontend_dlg_run
- dlg_default_destroy
- dlg_default_callback
- dlg_default_mouse_callback
- dlg_create
- dlg_close
- dlg_init
- dlg_process_event
- dlg_run_done
- dlg_run
- dlg_get_title
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 #include <config.h>
28
29 #include <stdlib.h>
30
31 #include "lib/global.h"
32
33 #include "lib/tty/tty.h"
34 #include "lib/skin.h"
35 #include "lib/tty/key.h"
36 #include "lib/strutil.h"
37 #include "lib/event.h"
38 #include "lib/util.h"
39
40 #include "lib/widget.h"
41 #include "lib/widget/mouse.h"
42
43
44
45
46 const dlg_colors_t dialog_colors = {
47 [DLG_COLOR_NORMAL] = DIALOG_NORMAL_COLOR,
48 [DLG_COLOR_FOCUS] = DIALOG_FOCUS_COLOR,
49 [DLG_COLOR_HOT_NORMAL] = DIALOG_HOT_NORMAL_COLOR,
50 [DLG_COLOR_HOT_FOCUS] = DIALOG_HOT_FOCUS_COLOR,
51 [DLG_COLOR_SELECTED_NORMAL] = DIALOG_SELECTED_NORMAL_COLOR,
52 [DLG_COLOR_SELECTED_FOCUS] = DIALOG_SELECTED_FOCUS_COLOR,
53 [DLG_COLOR_TITLE] = DIALOG_TITLE_COLOR,
54 [DLG_COLOR_FRAME] = DIALOG_FRAME_COLOR,
55 };
56 const dlg_colors_t alarm_colors = {
57 [DLG_COLOR_NORMAL] = ERROR_NORMAL_COLOR,
58 [DLG_COLOR_FOCUS] = ERROR_FOCUS_COLOR,
59 [DLG_COLOR_HOT_NORMAL] = ERROR_HOT_NORMAL_COLOR,
60 [DLG_COLOR_HOT_FOCUS] = ERROR_HOT_FOCUS_COLOR,
61 [DLG_COLOR_SELECTED_NORMAL] = ERROR_HOT_FOCUS_COLOR,
62 [DLG_COLOR_SELECTED_FOCUS] = ERROR_FOCUS_COLOR,
63 [DLG_COLOR_TITLE] = ERROR_TITLE_COLOR,
64 [DLG_COLOR_FRAME] = ERROR_FRAME_COLOR,
65 };
66
67
68 hook_t *idle_hook = NULL;
69
70
71 gboolean mouse_close_dialog = FALSE;
72
73 const global_keymap_t *dialog_map = NULL;
74
75
76
77
78
79
80
81
82
83
84
85
86
87 static const int *
88 dlg_default_get_colors (const Widget *w)
89 {
90 return CONST_DIALOG (w)->colors;
91 }
92
93
94
95 static void
96 refresh_cmd (void)
97 {
98 #ifdef HAVE_SLANG
99 tty_touch_screen ();
100 mc_refresh ();
101 #else
102
103 tty_clear_screen ();
104 repaint_screen ();
105 #endif
106 }
107
108
109
110 static void
111 dlg_help (const WDialog *h)
112 {
113 ev_help_t event_data = { NULL, h->help_ctx };
114
115 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
116 }
117
118
119
120 static cb_ret_t
121 dlg_execute_cmd (WDialog *h, long command)
122 {
123 WGroup *g = GROUP (h);
124 cb_ret_t ret = MSG_HANDLED;
125
126 if (send_message (h, NULL, MSG_ACTION, command, NULL) == MSG_HANDLED)
127 return MSG_HANDLED;
128
129 switch (command)
130 {
131 case CK_Ok:
132 h->ret_value = B_ENTER;
133 dlg_close (h);
134 break;
135 case CK_Cancel:
136 h->ret_value = B_CANCEL;
137 dlg_close (h);
138 break;
139
140 case CK_Up:
141 case CK_Left:
142 group_select_prev_widget (g);
143 break;
144 case CK_Down:
145 case CK_Right:
146 group_select_next_widget (g);
147 break;
148
149 case CK_Help:
150 dlg_help (h);
151 break;
152
153 case CK_Suspend:
154 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
155 refresh_cmd ();
156 break;
157 case CK_Refresh:
158 refresh_cmd ();
159 break;
160
161 case CK_ScreenList:
162 if (!widget_get_state (WIDGET (h), WST_MODAL))
163 dialog_switch_list ();
164 else
165 ret = MSG_NOT_HANDLED;
166 break;
167 case CK_ScreenNext:
168 if (!widget_get_state (WIDGET (h), WST_MODAL))
169 dialog_switch_next ();
170 else
171 ret = MSG_NOT_HANDLED;
172 break;
173 case CK_ScreenPrev:
174 if (!widget_get_state (WIDGET (h), WST_MODAL))
175 dialog_switch_prev ();
176 else
177 ret = MSG_NOT_HANDLED;
178 break;
179
180 default:
181 ret = MSG_NOT_HANDLED;
182 }
183
184 return ret;
185 }
186
187
188
189 static cb_ret_t
190 dlg_handle_key (WDialog *h, int d_key)
191 {
192 long command;
193
194 command = widget_lookup_key (WIDGET (h), d_key);
195 if (command == CK_IgnoreKey)
196 command = keybind_lookup_keymap_command (dialog_map, d_key);
197 if (command != CK_IgnoreKey)
198 return dlg_execute_cmd (h, command);
199
200 return MSG_NOT_HANDLED;
201 }
202
203
204
205 static void
206 dlg_key_event (WDialog *h, int d_key)
207 {
208 Widget *w = WIDGET (h);
209 WGroup *g = GROUP (h);
210 cb_ret_t handled;
211
212 if (g->widgets == NULL)
213 return;
214
215 if (g->current == NULL)
216 g->current = g->widgets;
217
218
219 if (!widget_get_options (w, WOP_WANT_TAB))
220 {
221 if (d_key == '\t')
222 {
223 group_select_next_widget (g);
224 return;
225 }
226 else if ((d_key & ~(KEY_M_SHIFT | KEY_M_CTRL)) == '\t')
227 {
228 group_select_prev_widget (g);
229 return;
230 }
231 }
232
233
234 handled = send_message (h, NULL, MSG_KEY, d_key, NULL);
235
236 if (handled == MSG_NOT_HANDLED)
237 handled = group_default_callback (w, NULL, MSG_KEY, d_key, NULL);
238
239 if (handled == MSG_NOT_HANDLED)
240 handled = dlg_handle_key (h, d_key);
241
242 (void) handled;
243 send_message (h, NULL, MSG_POST_KEY, d_key, NULL);
244 }
245
246
247
248 static int
249 dlg_handle_mouse_event (Widget *w, Gpm_Event *event)
250 {
251 if (w->mouse_callback != NULL)
252 {
253 int mou;
254
255 mou = mouse_handle_event (w, event);
256 if (mou != MOU_UNHANDLED)
257 return mou;
258 }
259
260 return group_handle_mouse_event (w, event);
261 }
262
263
264
265 static void
266 frontend_dlg_run (WDialog *h)
267 {
268 Widget *wh = WIDGET (h);
269 Gpm_Event event;
270
271 event.x = -1;
272
273
274 if (!widget_get_state (wh, WST_MODAL) && mc_global.midnight_shutdown)
275 {
276 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
277 return;
278 }
279
280 while (widget_get_state (wh, WST_ACTIVE))
281 {
282 int d_key;
283
284 if (tty_got_winch ())
285 dialog_change_screen_size ();
286
287 if (is_idle ())
288 {
289 if (idle_hook)
290 execute_hooks (idle_hook);
291
292 while (widget_get_state (wh, WST_IDLE) && is_idle ())
293 send_message (wh, NULL, MSG_IDLE, 0, NULL);
294
295
296 if (!widget_get_state (wh, WST_ACTIVE))
297 break;
298 }
299
300 widget_update_cursor (wh);
301
302
303 tty_got_interrupt ();
304 d_key = tty_get_event (&event, GROUP (h)->mouse_status == MOU_REPEAT, TRUE);
305
306 dlg_process_event (h, d_key, &event);
307
308 if (widget_get_state (wh, WST_CLOSED))
309 send_message (h, NULL, MSG_VALIDATE, 0, NULL);
310 }
311 }
312
313
314
315 static void
316 dlg_default_destroy (Widget *w)
317 {
318 WDialog *h = DIALOG (w);
319
320
321 history_save (h, NULL);
322 group_default_callback (w, NULL, MSG_DESTROY, 0, NULL);
323 send_message (w, NULL, MSG_DESTROY, 0, NULL);
324 mc_event_group_del (h->event_group);
325 g_free (h->event_group);
326 g_free (h);
327
328 do_refresh ();
329 }
330
331
332
333
334
335
336 cb_ret_t
337 dlg_default_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
338 {
339 switch (msg)
340 {
341 case MSG_INIT:
342
343 return MSG_HANDLED;
344
345 case MSG_IDLE:
346
347 widget_idle (w, FALSE);
348 return MSG_HANDLED;
349
350 case MSG_DESTROY:
351
352 return MSG_HANDLED;
353
354 default:
355 return group_default_callback (w, sender, msg, parm, data);
356 }
357 }
358
359
360
361 void
362 dlg_default_mouse_callback (Widget *w, mouse_msg_t msg, mouse_event_t *event)
363 {
364 switch (msg)
365 {
366 case MSG_MOUSE_CLICK:
367 if (event->y < 0 || event->y >= w->rect.lines || event->x < 0 || event->x >= w->rect.cols)
368 {
369 DIALOG (w)->ret_value = B_CANCEL;
370 dlg_close (DIALOG (w));
371 }
372 break;
373
374 default:
375
376 event->result.abort = TRUE;
377 break;
378 }
379 }
380
381
382
383 WDialog *
384 dlg_create (gboolean modal, int y1, int x1, int lines, int cols, widget_pos_flags_t pos_flags,
385 gboolean compact, const int *colors, widget_cb_fn callback,
386 widget_mouse_cb_fn mouse_callback, const char *help_ctx, const char *title)
387 {
388 WRect r = { y1, x1, lines, cols };
389 WDialog *new_d;
390 Widget *w;
391 WGroup *g;
392
393 new_d = g_new0 (WDialog, 1);
394 w = WIDGET (new_d);
395 g = GROUP (new_d);
396 widget_adjust_position (pos_flags, &r);
397 group_init (g, &r, callback != NULL ? callback : dlg_default_callback,
398 mouse_callback != NULL ? mouse_callback : dlg_default_mouse_callback);
399
400 w->pos_flags = pos_flags;
401 w->options |= WOP_SELECTABLE | WOP_TOP_SELECT;
402 w->state |= WST_FOCUSED;
403
404 w->owner = g;
405
406 w->keymap = dialog_map;
407
408 w->mouse_handler = dlg_handle_mouse_event;
409 w->mouse.forced_capture = mouse_close_dialog && (w->pos_flags & WPOS_FULLSCREEN) == 0;
410
411 w->destroy = dlg_default_destroy;
412 w->get_colors = dlg_default_get_colors;
413
414 new_d->colors = colors;
415 new_d->help_ctx = help_ctx;
416 new_d->compact = compact;
417 new_d->data.p = NULL;
418
419 if (modal)
420 {
421 w->state |= WST_MODAL;
422
423 new_d->bg =
424 WIDGET (frame_new (0, 0, w->rect.lines, w->rect.cols, title, FALSE, new_d->compact));
425 group_add_widget (g, new_d->bg);
426 frame_set_title (FRAME (new_d->bg), title);
427 }
428
429
430 new_d->event_group = g_strdup_printf ("%s_%p", MCEVENT_GROUP_DIALOG, (void *) new_d);
431
432 return new_d;
433 }
434
435
436
437 void
438 dlg_close (WDialog *h)
439 {
440 widget_set_state (WIDGET (h), WST_CLOSED, TRUE);
441 }
442
443
444
445
446 void
447 dlg_init (WDialog *h)
448 {
449 WGroup *g = GROUP (h);
450 Widget *wh = WIDGET (h);
451
452 if (top_dlg != NULL && widget_get_state (WIDGET (top_dlg->data), WST_MODAL))
453 widget_set_state (wh, WST_MODAL, TRUE);
454
455
456 top_dlg = g_list_prepend (top_dlg, h);
457
458
459 if (widget_get_state (wh, WST_CONSTRUCT))
460 {
461 if (!widget_get_state (wh, WST_MODAL))
462 dialog_switch_add (h);
463
464 send_message (h, NULL, MSG_INIT, 0, NULL);
465 group_default_callback (wh, NULL, MSG_INIT, 0, NULL);
466 history_load (h, NULL);
467 }
468
469
470 while (g->current != NULL && !widget_is_focusable (g->current->data))
471 group_set_current_widget_next (g);
472
473 widget_set_state (wh, WST_ACTIVE, TRUE);
474 widget_draw (wh);
475
476 h->ret_value = 0;
477 }
478
479
480
481 void
482 dlg_process_event (WDialog *h, int key, Gpm_Event *event)
483 {
484 switch (key)
485 {
486 case EV_NONE:
487 if (tty_got_interrupt ())
488 dlg_execute_cmd (h, CK_Cancel);
489 break;
490
491 case EV_MOUSE:
492 {
493 Widget *w = WIDGET (h);
494
495 GROUP (h)->mouse_status = w->mouse_handler (w, event);
496 break;
497 }
498
499 default:
500 dlg_key_event (h, key);
501 break;
502 }
503 }
504
505
506
507
508 void
509 dlg_run_done (WDialog *h)
510 {
511 top_dlg = g_list_remove (top_dlg, h);
512
513 if (widget_get_state (WIDGET (h), WST_CLOSED))
514 {
515 send_message (h, GROUP (h)->current == NULL ? NULL : WIDGET (GROUP (h)->current->data),
516 MSG_END, 0, NULL);
517 if (!widget_get_state (WIDGET (h), WST_MODAL))
518 dialog_switch_remove (h);
519 }
520 }
521
522
523
524
525
526
527
528
529
530 int
531 dlg_run (WDialog *h)
532 {
533 dlg_init (h);
534 frontend_dlg_run (h);
535 dlg_run_done (h);
536 return h->ret_value;
537 }
538
539
540
541 char *
542 dlg_get_title (const WDialog *h, const ssize_t width)
543 {
544 char *t;
545
546 if (h == NULL)
547 abort ();
548
549 if (h->get_title != NULL)
550 t = h->get_title (h, width);
551 else
552 t = g_strdup ("");
553
554 return t;
555 }
556
557