This source file includes following definitions.
- query_default_callback
- do_create_message
- fg_message
- bg_message
- fg_input_dialog_help
- wtools_parent_call
- wtools_parent_call_string
- query_dialog
- query_set_sel
- create_message
- message
- mc_error_message
- input_dialog_help
- input_dialog
- input_expand_dialog
- status_msg_create
- status_msg_destroy
- status_msg_init
- status_msg_deinit
- status_msg_common_update
- simple_status_msg_init_cb
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
28
29
30
31
32
33
34 #include <config.h>
35
36 #include <stdarg.h>
37 #include <stdlib.h>
38
39 #include "lib/global.h"
40 #include "lib/tty/tty.h"
41 #include "lib/tty/key.h"
42 #include "lib/strutil.h"
43 #include "lib/util.h"
44 #include "lib/widget.h"
45 #include "lib/event.h"
46
47
48
49
50
51
52
53
54
55
56
57 static WDialog *last_query_dlg;
58
59 static int sel_pos = 0;
60
61
62
63
64
65
66
67 static cb_ret_t
68 query_default_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
69 {
70 WDialog *h = DIALOG (w);
71
72 switch (msg)
73 {
74 case MSG_RESIZE:
75 if ((w->pos_flags & WPOS_CENTER) == 0)
76 {
77 WDialog *prev_dlg = NULL;
78 int ypos, xpos;
79 WRect r;
80
81
82 if (top_dlg != NULL)
83 {
84 if (top_dlg->data != (void *) h)
85 prev_dlg = DIALOG (top_dlg->data);
86 else
87 {
88 GList *p;
89
90
91
92 p = g_list_next (top_dlg);
93 if (p != NULL)
94 prev_dlg = DIALOG (p->data);
95 }
96 }
97
98
99 if (prev_dlg == NULL || (WIDGET (prev_dlg)->pos_flags & WPOS_FULLSCREEN) != 0)
100 ypos = LINES / 3 - (w->rect.lines - 3) / 2;
101 else
102 ypos = WIDGET (prev_dlg)->rect.y + 2;
103
104
105 if (ypos + w->rect.lines < LINES / 2)
106 w->pos_flags |= WPOS_CENTER;
107
108 xpos = COLS / 2 - w->rect.cols / 2;
109
110
111 rect_init (&r, ypos, xpos, w->rect.lines, w->rect.cols);
112
113 return dlg_default_callback (w, NULL, MSG_RESIZE, 0, &r);
114 }
115 MC_FALLTHROUGH;
116
117 default:
118 return dlg_default_callback (w, sender, msg, parm, data);
119 }
120 }
121
122
123
124
125 static WDialog *
126 do_create_message (int flags, const char *title, const char *text)
127 {
128 char *p;
129 WDialog *d;
130
131
132 p = g_strconcat ("\n", text, "\n", (char *) NULL);
133 query_dialog (title, p, flags, 0);
134 d = last_query_dlg;
135
136
137 send_message (d, NULL, MSG_RESIZE, 0, NULL);
138
139 dlg_init (d);
140 g_free (p);
141
142 return d;
143 }
144
145
146
147
148
149
150
151 static void
152 fg_message (int flags, const char *title, const char *text)
153 {
154 WDialog *d;
155
156 d = do_create_message (flags, title, text);
157 tty_getch ();
158 dlg_run_done (d);
159 widget_destroy (WIDGET (d));
160 }
161
162
163
164
165 #ifdef ENABLE_BACKGROUND
166 static void
167 bg_message (int dummy, int *flags, char *title, const char *text)
168 {
169 (void) dummy;
170 title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
171 fg_message (*flags, title, text);
172 g_free (title);
173 }
174 #endif
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189 static char *
190 fg_input_dialog_help (const char *header, const char *text, const char *help,
191 const char *history_name, const char *def_text, gboolean strip_password,
192 input_complete_t completion_flags)
193 {
194 char *p_text;
195 char histname[64] = "inp|";
196 gboolean is_passwd = FALSE;
197 char *my_str;
198 int ret;
199
200
201 p_text = g_strstrip (g_strdup (text));
202
203
204 if (history_name != NULL && *history_name != '\0')
205 g_strlcpy (histname + 3, history_name, sizeof (histname) - 3);
206
207
208
209 if (def_text == INPUT_PASSWORD)
210 {
211 is_passwd = TRUE;
212 histname[3] = '\0';
213 def_text = "";
214 }
215
216 {
217 quick_widget_t quick_widgets[] = {
218
219 QUICK_LABELED_INPUT (p_text, input_label_above, def_text, histname, &my_str,
220 NULL, is_passwd, strip_password, completion_flags),
221 QUICK_BUTTONS_OK_CANCEL,
222 QUICK_END
223
224 };
225
226 WRect r = { -1, -1, 0, COLS / 2 };
227
228 quick_dialog_t qdlg = {
229 r, header, help,
230 quick_widgets, NULL, NULL
231 };
232
233 ret = quick_dialog (&qdlg);
234 }
235
236 g_free (p_text);
237
238 return (ret != B_CANCEL) ? my_str : NULL;
239 }
240
241
242
243 #ifdef ENABLE_BACKGROUND
244 static int
245 wtools_parent_call (void *routine, gpointer ctx, int argc, ...)
246 {
247 ev_background_parent_call_t event_data;
248
249 event_data.routine = routine;
250 event_data.ctx = ctx;
251 event_data.argc = argc;
252 va_start (event_data.ap, argc);
253 mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call", (gpointer) & event_data);
254 va_end (event_data.ap);
255 return event_data.ret.i;
256 }
257
258
259
260 static char *
261 wtools_parent_call_string (void *routine, int argc, ...)
262 {
263 ev_background_parent_call_t event_data;
264
265 event_data.routine = routine;
266 event_data.argc = argc;
267 va_start (event_data.ap, argc);
268 mc_event_raise (MCEVENT_GROUP_CORE, "background_parent_call_string", (gpointer) & event_data);
269 va_end (event_data.ap);
270 return event_data.ret.s;
271 }
272 #endif
273
274
275
276
277
278
279 int
280 query_dialog (const char *header, const char *text, int flags, int count, ...)
281 {
282 va_list ap;
283 WDialog *query_dlg;
284 WGroup *g;
285 WButton *button;
286 int win_len = 0;
287 int i;
288 int result = -1;
289 int cols, lines;
290 const int *query_colors = (flags & D_ERROR) != 0 ? alarm_colors : dialog_colors;
291 widget_pos_flags_t pos_flags =
292 (flags & D_CENTER) != 0 ? (WPOS_CENTER | WPOS_TRYUP) : WPOS_KEEP_DEFAULT;
293
294 if (header == MSG_ERROR)
295 header = _("Error");
296
297 if (count > 0)
298 {
299 va_start (ap, count);
300 for (i = 0; i < count; i++)
301 {
302 char *cp = va_arg (ap, char *);
303
304 win_len += str_term_width1 (cp) + 6;
305 if (strchr (cp, '&') != NULL)
306 win_len--;
307 }
308 va_end (ap);
309 }
310
311
312 str_msg_term_size (text, &lines, &cols);
313 cols = 6 + MAX (win_len, MAX (str_term_width1 (header), cols));
314 lines += 4 + (count > 0 ? 2 : 0);
315
316
317 query_dlg =
318 dlg_create (TRUE, 0, 0, lines, cols, pos_flags, FALSE, query_colors, query_default_callback,
319 NULL, "[QueryBox]", header);
320 g = GROUP (query_dlg);
321
322 if (count > 0)
323 {
324 WButton *defbutton = NULL;
325
326 group_add_widget_autopos (g, label_new (2, 3, text), WPOS_KEEP_TOP | WPOS_CENTER_HORZ,
327 NULL);
328 group_add_widget (g, hline_new (lines - 4, -1, -1));
329
330 cols = (cols - win_len - 2) / 2 + 2;
331 va_start (ap, count);
332 for (i = 0; i < count; i++)
333 {
334 int xpos;
335 char *cur_name;
336
337 cur_name = va_arg (ap, char *);
338 xpos = str_term_width1 (cur_name) + 6;
339 if (strchr (cur_name, '&') != NULL)
340 xpos--;
341
342 button = button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON, cur_name, NULL);
343 group_add_widget (g, button);
344 cols += xpos;
345 if (i == sel_pos)
346 defbutton = button;
347 }
348 va_end (ap);
349
350
351 send_message (query_dlg, NULL, MSG_RESIZE, 0, NULL);
352
353 if (defbutton != NULL)
354 widget_select (WIDGET (defbutton));
355
356
357 switch (dlg_run (query_dlg))
358 {
359 case B_CANCEL:
360 break;
361 default:
362 result = query_dlg->ret_value - B_USER;
363 }
364
365
366 widget_destroy (WIDGET (query_dlg));
367 }
368 else
369 {
370 group_add_widget_autopos (g, label_new (2, 3, text), WPOS_KEEP_TOP | WPOS_CENTER_HORZ,
371 NULL);
372 group_add_widget (g, button_new (0, 0, 0, HIDDEN_BUTTON, "-", NULL));
373 last_query_dlg = query_dlg;
374 }
375 sel_pos = 0;
376 return result;
377 }
378
379
380
381 void
382 query_set_sel (int new_sel)
383 {
384 sel_pos = new_sel;
385 }
386
387
388
389
390
391
392
393 WDialog *
394 create_message (int flags, const char *title, const char *text, ...)
395 {
396 va_list args;
397 WDialog *d;
398 char *p;
399
400 va_start (args, text);
401 p = g_strdup_vprintf (text, args);
402 va_end (args);
403
404 d = do_create_message (flags, title, p);
405 g_free (p);
406
407 return d;
408 }
409
410
411
412
413 void
414 message (int flags, const char *title, const char *text, ...)
415 {
416 char *p;
417 va_list ap;
418
419 va_start (ap, text);
420 p = g_strdup_vprintf (text, ap);
421 va_end (ap);
422
423 if (title == MSG_ERROR)
424 title = _("Error");
425
426 #ifdef ENABLE_BACKGROUND
427 if (mc_global.we_are_background)
428 {
429 union
430 {
431 void *p;
432 void (*f) (int, int *, char *, const char *);
433 } func;
434 func.f = bg_message;
435
436 wtools_parent_call (func.p, NULL, 3, sizeof (flags), &flags, strlen (title), title,
437 strlen (p), p);
438 }
439 else
440 #endif
441 fg_message (flags, title, p);
442
443 g_free (p);
444 }
445
446
447
448
449 gboolean
450 mc_error_message (GError **mcerror, int *code)
451 {
452 if (mcerror == NULL || *mcerror == NULL)
453 return FALSE;
454
455 if ((*mcerror)->code == 0)
456 message (D_ERROR, MSG_ERROR, "%s", (*mcerror)->message);
457 else
458 message (D_ERROR, MSG_ERROR, _("%s (%d)"), (*mcerror)->message, (*mcerror)->code);
459
460 if (code != NULL)
461 *code = (*mcerror)->code;
462
463 g_error_free (*mcerror);
464 *mcerror = NULL;
465
466 return TRUE;
467 }
468
469
470
471
472
473
474
475
476
477 char *
478 input_dialog_help (const char *header, const char *text, const char *help,
479 const char *history_name, const char *def_text, gboolean strip_password,
480 input_complete_t completion_flags)
481 {
482 #ifdef ENABLE_BACKGROUND
483 if (mc_global.we_are_background)
484 {
485 union
486 {
487 void *p;
488 char *(*f) (const char *, const char *, const char *, const char *, const char *,
489 gboolean, input_complete_t);
490 } func;
491 func.f = fg_input_dialog_help;
492 return wtools_parent_call_string (func.p, 7,
493 strlen (header), header, strlen (text),
494 text, strlen (help), help,
495 strlen (history_name), history_name,
496 strlen (def_text), def_text,
497 sizeof (gboolean), strip_password,
498 sizeof (input_complete_t), completion_flags);
499 }
500 else
501 #endif
502 return fg_input_dialog_help (header, text, help, history_name, def_text, strip_password,
503 completion_flags);
504 }
505
506
507
508
509 char *
510 input_dialog (const char *header, const char *text, const char *history_name, const char *def_text,
511 input_complete_t completion_flags)
512 {
513 return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text, FALSE,
514 completion_flags);
515 }
516
517
518
519 char *
520 input_expand_dialog (const char *header, const char *text,
521 const char *history_name, const char *def_text,
522 input_complete_t completion_flags)
523 {
524 char *result;
525
526 result = input_dialog (header, text, history_name, def_text, completion_flags);
527 if (result)
528 {
529 char *expanded;
530
531 expanded = tilde_expand (result);
532 g_free (result);
533 return expanded;
534 }
535 return result;
536 }
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551 status_msg_t *
552 status_msg_create (const char *title, double delay, status_msg_cb init_cb,
553 status_msg_update_cb update_cb, status_msg_cb deinit_cb)
554 {
555 status_msg_t *sm;
556
557 sm = g_try_new (status_msg_t, 1);
558 status_msg_init (sm, title, delay, init_cb, update_cb, deinit_cb);
559
560 return sm;
561 }
562
563
564
565
566
567
568
569
570 void
571 status_msg_destroy (status_msg_t *sm)
572 {
573 status_msg_deinit (sm);
574 g_free (sm);
575 }
576
577
578
579
580
581
582
583
584
585
586
587
588
589 void
590 status_msg_init (status_msg_t *sm, const char *title, double delay, status_msg_cb init_cb,
591 status_msg_update_cb update_cb, status_msg_cb deinit_cb)
592 {
593 gint64 start;
594
595
596 mc_refresh ();
597
598 start = g_get_monotonic_time ();
599
600 sm->dlg = dlg_create (TRUE, 0, 0, 7, MIN (MAX (40, COLS / 2), COLS), WPOS_CENTER, FALSE,
601 dialog_colors, NULL, NULL, NULL, title);
602 sm->start = start;
603 sm->delay = (gint64) (delay * G_USEC_PER_SEC);
604 sm->block = FALSE;
605
606 sm->init = init_cb;
607 sm->update = update_cb;
608 sm->deinit = deinit_cb;
609
610 if (sm->init != NULL)
611 sm->init (sm);
612
613 if (mc_time_elapsed (&start, sm->delay))
614 {
615
616 dlg_init (sm->dlg);
617 }
618 }
619
620
621
622
623
624
625
626
627 void
628 status_msg_deinit (status_msg_t *sm)
629 {
630 if (sm == NULL)
631 return;
632
633 if (sm->deinit != NULL)
634 sm->deinit (sm);
635
636
637 dlg_run_done (sm->dlg);
638 widget_destroy (WIDGET (sm->dlg));
639 }
640
641
642
643
644
645
646
647
648
649
650 int
651 status_msg_common_update (status_msg_t *sm)
652 {
653 int c;
654 Gpm_Event event;
655
656 if (sm == NULL)
657 return B_ENTER;
658
659
660 if (sm->dlg == NULL)
661 return B_ENTER;
662
663 if (widget_get_state (WIDGET (sm->dlg), WST_CONSTRUCT))
664 {
665
666
667
668 gint64 start = sm->start;
669
670 if (mc_time_elapsed (&start, sm->delay))
671 dlg_init (sm->dlg);
672
673 return B_ENTER;
674 }
675
676 event.x = -1;
677 c = tty_get_event (&event, FALSE, sm->block);
678 if (c == EV_NONE)
679 return B_ENTER;
680
681
682
683 sm->dlg->ret_value = B_ENTER;
684 dlg_process_event (sm->dlg, c, &event);
685
686 return sm->dlg->ret_value;
687 }
688
689
690
691
692
693
694
695
696 void
697 simple_status_msg_init_cb (status_msg_t *sm)
698 {
699 simple_status_msg_t *ssm = SIMPLE_STATUS_MSG (sm);
700 Widget *wd = WIDGET (sm->dlg);
701 WGroup *wg = GROUP (sm->dlg);
702 WRect r;
703
704 const char *b_name = N_("&Abort");
705 int b_width;
706 int wd_width, y;
707 Widget *b;
708
709 #ifdef ENABLE_NLS
710 b_name = _(b_name);
711 #endif
712
713 b_width = str_term_width1 (b_name) + 4;
714 wd_width = MAX (wd->rect.cols, b_width + 6);
715
716 y = 2;
717 ssm->label = label_new (y++, 3, NULL);
718 group_add_widget_autopos (wg, ssm->label, WPOS_KEEP_TOP | WPOS_CENTER_HORZ, NULL);
719 group_add_widget (wg, hline_new (y++, -1, -1));
720 b = WIDGET (button_new (y++, 3, B_CANCEL, NORMAL_BUTTON, b_name, NULL));
721 group_add_widget_autopos (wg, b, WPOS_KEEP_TOP | WPOS_CENTER_HORZ, NULL);
722
723 r = wd->rect;
724 r.lines = y + 2;
725 r.cols = wd_width;
726 widget_set_size_rect (wd, &r);
727 }
728
729