wtools.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /* Widget based utility functions.
  2. Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  3. 2005, 2006, 2007 Free Software Foundation, Inc.
  4. Authors: 1994, 1995, 1996 Miguel de Icaza
  5. 1994, 1995 Radek Doulik
  6. 1995 Jakub Jelinek
  7. 1995 Andrej Borsenkow
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include <config.h>
  21. #include <stdarg.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "global.h"
  25. #include "tty.h"
  26. #include "color.h" /* dialog_colors */
  27. #include "dialog.h"
  28. #include "widget.h"
  29. #include "wtools.h"
  30. #include "key.h" /* mi_getch() */
  31. #include "complete.h" /* INPUT_COMPLETE_CD */
  32. #include "background.h" /* parent_call */
  33. Listbox *
  34. create_listbox_window (int cols, int lines, const char *title, const char *help)
  35. {
  36. int xpos, ypos, len;
  37. Listbox *listbox = g_new (Listbox, 1);
  38. const char *cancel_string = _("&Cancel");
  39. /* Adjust sizes */
  40. lines = (lines > LINES - 6) ? LINES - 6 : lines;
  41. if (title && (cols < (len = strlen (title) + 2)))
  42. cols = len;
  43. /* no &, but 4 spaces around button for brackets and such */
  44. if (cols < (len = strlen (cancel_string) + 3))
  45. cols = len;
  46. cols = cols > COLS - 6 ? COLS - 6 : cols;
  47. xpos = (COLS - cols) / 2;
  48. ypos = (LINES - lines) / 2 - 2;
  49. /* Create components */
  50. listbox->dlg =
  51. create_dlg (ypos, xpos, lines + 6, cols + 4, dialog_colors, NULL,
  52. help, title, DLG_CENTER | DLG_REVERSE);
  53. listbox->list = listbox_new (2, 2, cols, lines, 0);
  54. add_widget (listbox->dlg,
  55. button_new (lines + 3, (cols / 2 + 2) - len / 2, B_CANCEL,
  56. NORMAL_BUTTON, cancel_string, 0));
  57. add_widget (listbox->dlg, listbox->list);
  58. return listbox;
  59. }
  60. /* Returns the number of the item selected */
  61. int run_listbox (Listbox *l)
  62. {
  63. int val;
  64. run_dlg (l->dlg);
  65. if (l->dlg->ret_value == B_CANCEL)
  66. val = -1;
  67. else
  68. val = l->list->pos;
  69. destroy_dlg (l->dlg);
  70. g_free (l);
  71. return val;
  72. }
  73. static Dlg_head *last_query_dlg;
  74. static int sel_pos = 0;
  75. /* Used to ask questions to the user */
  76. int
  77. query_dialog (const char *header, const char *text, int flags, int count, ...)
  78. {
  79. va_list ap;
  80. Dlg_head *query_dlg;
  81. WButton *button;
  82. WButton *defbutton = NULL;
  83. int win_len = 0;
  84. int i;
  85. int result = -1;
  86. int xpos, ypos;
  87. int cols, lines;
  88. char *cur_name;
  89. static const int *query_colors;
  90. /* set dialog colors */
  91. if (flags & D_ERROR)
  92. query_colors = alarm_colors;
  93. else
  94. query_colors = dialog_colors;
  95. if (header == MSG_ERROR)
  96. header = _("Error");
  97. if (count > 0) {
  98. va_start (ap, count);
  99. for (i = 0; i < count; i++) {
  100. char *cp = va_arg (ap, char *);
  101. win_len += strlen (cp) + 6;
  102. if (strchr (cp, '&') != NULL)
  103. win_len--;
  104. }
  105. va_end (ap);
  106. }
  107. /* count coordinates */
  108. msglen (text, &lines, &cols);
  109. cols = 6 + max (win_len, max ((int) strlen (header), cols));
  110. lines += 4 + (count > 0 ? 2 : 0);
  111. xpos = COLS / 2 - cols / 2;
  112. ypos = LINES / 3 - (lines - 3) / 2;
  113. /* prepare dialog */
  114. query_dlg =
  115. create_dlg (ypos, xpos, lines, cols, query_colors, NULL,
  116. "[QueryBox]", header, DLG_NONE);
  117. if (count > 0) {
  118. cols = (cols - win_len - 2) / 2 + 2;
  119. va_start (ap, count);
  120. for (i = 0; i < count; i++) {
  121. cur_name = va_arg (ap, char *);
  122. xpos = strlen (cur_name) + 6;
  123. if (strchr (cur_name, '&') != NULL)
  124. xpos--;
  125. button =
  126. button_new (lines - 3, cols, B_USER + i, NORMAL_BUTTON,
  127. cur_name, 0);
  128. add_widget (query_dlg, button);
  129. cols += xpos;
  130. if (i == sel_pos)
  131. defbutton = button;
  132. }
  133. va_end (ap);
  134. add_widget (query_dlg, label_new (2, 3, text));
  135. if (defbutton)
  136. dlg_select_widget (defbutton);
  137. /* run dialog and make result */
  138. run_dlg (query_dlg);
  139. switch (query_dlg->ret_value) {
  140. case B_CANCEL:
  141. break;
  142. default:
  143. result = query_dlg->ret_value - B_USER;
  144. }
  145. /* free used memory */
  146. destroy_dlg (query_dlg);
  147. } else {
  148. add_widget (query_dlg, label_new (2, 3, text));
  149. add_widget (query_dlg,
  150. button_new (0, 0, 0, HIDDEN_BUTTON, "-", 0));
  151. last_query_dlg = query_dlg;
  152. }
  153. sel_pos = 0;
  154. return result;
  155. }
  156. void query_set_sel (int new_sel)
  157. {
  158. sel_pos = new_sel;
  159. }
  160. /* Create message dialog */
  161. static struct Dlg_head *
  162. do_create_message (int flags, const char *title, const char *text)
  163. {
  164. char *p;
  165. Dlg_head *d;
  166. /* Add empty lines before and after the message */
  167. p = g_strconcat ("\n", text, "\n", (char *) NULL);
  168. query_dialog (title, p, flags, 0);
  169. d = last_query_dlg;
  170. init_dlg (d);
  171. g_free (p);
  172. return d;
  173. }
  174. /*
  175. * Create message dialog. The caller must call dlg_run_done() and
  176. * destroy_dlg() to dismiss it. Not safe to call from background.
  177. */
  178. struct Dlg_head *
  179. create_message (int flags, const char *title, const char *text, ...)
  180. {
  181. va_list args;
  182. Dlg_head *d;
  183. char *p;
  184. va_start (args, text);
  185. p = g_strdup_vprintf (text, args);
  186. va_end (args);
  187. d = do_create_message (flags, title, p);
  188. g_free (p);
  189. return d;
  190. }
  191. /*
  192. * Show message dialog. Dismiss it when any key is pressed.
  193. * Not safe to call from background.
  194. */
  195. static void
  196. fg_message (int flags, const char *title, const char *text)
  197. {
  198. Dlg_head *d;
  199. d = do_create_message (flags, title, text);
  200. mi_getch ();
  201. dlg_run_done (d);
  202. destroy_dlg (d);
  203. }
  204. /* Show message box from background */
  205. #ifdef WITH_BACKGROUND
  206. static void
  207. bg_message (int dummy, int *flags, char *title, const char *text)
  208. {
  209. (void) dummy;
  210. title = g_strconcat (_("Background process:"), " ", title, (char *) NULL);
  211. fg_message (*flags, title, text);
  212. g_free (title);
  213. }
  214. #endif /* WITH_BACKGROUND */
  215. /* Show message box, background safe */
  216. void
  217. message (int flags, const char *title, const char *text, ...)
  218. {
  219. char *p;
  220. va_list ap;
  221. va_start (ap, text);
  222. p = g_strdup_vprintf (text, ap);
  223. va_end (ap);
  224. if (title == MSG_ERROR)
  225. title = _("Error");
  226. #ifdef WITH_BACKGROUND
  227. if (we_are_background) {
  228. parent_call ((void *) bg_message, NULL, 3, sizeof (flags), &flags,
  229. strlen (title), title, strlen (p), p);
  230. } else
  231. #endif /* WITH_BACKGROUND */
  232. fg_message (flags, title, p);
  233. g_free (p);
  234. }
  235. /* {{{ Quick dialog routines */
  236. #define I18N(x) (do_int && *x ? (x = _(x)): x)
  237. int
  238. quick_dialog_skip (QuickDialog *qd, int nskip)
  239. {
  240. Dlg_head *dd;
  241. void *widget;
  242. WRadio *r;
  243. int xpos;
  244. int ypos;
  245. int return_val;
  246. WInput *input;
  247. QuickWidget *qw;
  248. int do_int;
  249. int count = 0; /* number of quick widgets */
  250. int curr_widget; /* number of the current quick widget */
  251. Widget **widgets; /* table of corresponding widgets */
  252. if (!qd->i18n) {
  253. qd->i18n = 1;
  254. do_int = 1;
  255. if (*qd->title)
  256. qd->title = _(qd->title);
  257. } else
  258. do_int = 0;
  259. if (qd->xpos == -1)
  260. dd = create_dlg (0, 0, qd->ylen, qd->xlen, dialog_colors, NULL,
  261. qd->help, qd->title,
  262. DLG_CENTER | DLG_TRYUP | DLG_REVERSE);
  263. else
  264. dd = create_dlg (qd->ypos, qd->xpos, qd->ylen, qd->xlen,
  265. dialog_colors, NULL, qd->help, qd->title,
  266. DLG_REVERSE);
  267. /* Count widgets */
  268. for (qw = qd->widgets; qw->widget_type; qw++) {
  269. count++;
  270. }
  271. widgets = (Widget **) g_new (Widget *, count);
  272. for (curr_widget = 0, qw = qd->widgets; qw->widget_type; qw++) {
  273. xpos = (qd->xlen * qw->relative_x) / qw->x_divisions;
  274. ypos = (qd->ylen * qw->relative_y) / qw->y_divisions;
  275. switch (qw->widget_type) {
  276. case quick_checkbox:
  277. widget = check_new (ypos, xpos, *qw->result, I18N (qw->text));
  278. break;
  279. case quick_radio:
  280. r = radio_new (ypos, xpos, qw->hotkey_pos, const_cast(const char **, qw->str_result));
  281. r->pos = r->sel = qw->value;
  282. widget = r;
  283. break;
  284. case quick_button:
  285. widget =
  286. button_new (ypos, xpos, qw->value,
  287. (qw->value ==
  288. B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
  289. I18N (qw->text), 0);
  290. break;
  291. /* We use the hotkey pos as the field length */
  292. case quick_input:
  293. input =
  294. input_new (ypos, xpos, INPUT_COLOR, qw->hotkey_pos,
  295. qw->text, qw->histname);
  296. input->is_password = qw->value == 1;
  297. input->point = 0;
  298. if (qw->value & 2)
  299. input->completion_flags |= INPUT_COMPLETE_CD;
  300. widget = input;
  301. break;
  302. case quick_label:
  303. widget = label_new (ypos, xpos, I18N (qw->text));
  304. break;
  305. default:
  306. widget = 0;
  307. fprintf (stderr, "QuickWidget: unknown widget type\n");
  308. break;
  309. }
  310. widgets[curr_widget++] = widget;
  311. add_widget (dd, widget);
  312. }
  313. while (nskip--)
  314. dd->current = dd->current->next;
  315. run_dlg (dd);
  316. /* Get the data if we found something interesting */
  317. if (dd->ret_value != B_CANCEL) {
  318. for (curr_widget = 0, qw = qd->widgets; qw->widget_type; qw++) {
  319. Widget *w = widgets[curr_widget++];
  320. switch (qw->widget_type) {
  321. case quick_checkbox:
  322. *qw->result = ((WCheck *) w)->state & C_BOOL;
  323. break;
  324. case quick_radio:
  325. *qw->result = ((WRadio *) w)->sel;
  326. break;
  327. case quick_input:
  328. if (qw->value & 2)
  329. *qw->str_result =
  330. tilde_expand (((WInput *) w)->buffer);
  331. else
  332. *qw->str_result = g_strdup (((WInput *) w)->buffer);
  333. break;
  334. }
  335. }
  336. }
  337. return_val = dd->ret_value;
  338. destroy_dlg (dd);
  339. g_free (widgets);
  340. return return_val;
  341. }
  342. int quick_dialog (QuickDialog *qd)
  343. {
  344. return quick_dialog_skip (qd, 0);
  345. }
  346. /* }}} */
  347. /* {{{ Input routines */
  348. #define INPUT_INDEX 2
  349. /*
  350. * Show dialog, not background safe.
  351. *
  352. * If the arguments "header" and "text" should be translated,
  353. * that MUST be done by the caller of fg_input_dialog_help().
  354. *
  355. * The argument "history_name" holds the name of a section
  356. * in the history file. Data entered in the input field of
  357. * the dialog box will be stored there.
  358. *
  359. */
  360. static char *
  361. fg_input_dialog_help (const char *header, const char *text, const char *help,
  362. const char *history_name, const char *def_text)
  363. {
  364. QuickDialog Quick_input;
  365. QuickWidget quick_widgets[] = {
  366. {quick_button, 6, 10, 1, 0, N_("&Cancel"), 0, B_CANCEL, 0, 0,
  367. NULL},
  368. {quick_button, 3, 10, 1, 0, N_("&OK"), 0, B_ENTER, 0, 0, NULL},
  369. {quick_input, 4, 80, 0, 0, "", 58, 0, 0, 0, NULL},
  370. {quick_label, 4, 80, 2, 0, "", 0, 0, 0, 0, NULL},
  371. NULL_QuickWidget
  372. };
  373. int len;
  374. int i;
  375. int lines, cols;
  376. int ret;
  377. char *my_str;
  378. char histname[64] = "inp|";
  379. char *p_text;
  380. if (history_name != NULL && *history_name != '\0') {
  381. g_strlcpy (histname + 3, history_name, 61);
  382. quick_widgets[2].histname = histname;
  383. }
  384. msglen (text, &lines, &cols);
  385. len = max ((int) strlen (header), cols) + 4;
  386. len = max (len, 64);
  387. /* The special value of def_text is used to identify password boxes
  388. and hide characters with "*". Don't save passwords in history! */
  389. if (def_text == INPUT_PASSWORD) {
  390. quick_widgets[INPUT_INDEX].value = 1;
  391. histname[3] = 0;
  392. def_text = "";
  393. } else {
  394. quick_widgets[INPUT_INDEX].value = 0;
  395. }
  396. #ifdef ENABLE_NLS
  397. /*
  398. * An attempt to place buttons symmetrically, based on actual i18n
  399. * length of the string. It looks nicer with i18n (IMO) - alex
  400. */
  401. quick_widgets[0].text = _(quick_widgets[0].text);
  402. quick_widgets[1].text = _(quick_widgets[1].text);
  403. quick_widgets[0].relative_x = len / 2 + 4;
  404. quick_widgets[1].relative_x =
  405. len / 2 - (strlen (quick_widgets[1].text) + 9);
  406. quick_widgets[0].x_divisions = quick_widgets[1].x_divisions = len;
  407. #endif /* ENABLE_NLS */
  408. Quick_input.xlen = len;
  409. Quick_input.xpos = -1;
  410. Quick_input.title = header;
  411. Quick_input.help = help;
  412. Quick_input.i18n = 1; /* The dialog is already translated. */
  413. p_text = g_strstrip (g_strdup (text));
  414. quick_widgets[INPUT_INDEX + 1].text = p_text;
  415. quick_widgets[INPUT_INDEX].text = def_text;
  416. for (i = 0; i < 4; i++)
  417. quick_widgets[i].y_divisions = lines + 6;
  418. Quick_input.ylen = lines + 6;
  419. for (i = 0; i < 3; i++)
  420. quick_widgets[i].relative_y += 2 + lines;
  421. quick_widgets[INPUT_INDEX].str_result = &my_str;
  422. Quick_input.widgets = quick_widgets;
  423. ret = quick_dialog (&Quick_input);
  424. g_free (p_text);
  425. if (ret != B_CANCEL) {
  426. return my_str;
  427. } else
  428. return 0;
  429. }
  430. /*
  431. * Show input dialog, background safe.
  432. *
  433. * If the arguments "header" and "text" should be translated,
  434. * that MUST be done by the caller of these wrappers.
  435. */
  436. char *
  437. input_dialog_help (const char *header, const char *text, const char *help,
  438. const char *history_name, const char *def_text)
  439. {
  440. #ifdef WITH_BACKGROUND
  441. if (we_are_background)
  442. return parent_call_string ((void *) fg_input_dialog_help, 5,
  443. strlen (header), header, strlen (text),
  444. text, strlen (help), help,
  445. strlen (history_name), history_name,
  446. strlen (def_text), def_text);
  447. else
  448. #endif /* WITH_BACKGROUND */
  449. return fg_input_dialog_help (header, text, help, history_name, def_text);
  450. }
  451. /* Show input dialog with default help, background safe */
  452. char *input_dialog (const char *header, const char *text,
  453. const char *history_name, const char *def_text)
  454. {
  455. return input_dialog_help (header, text, "[Input Line Keys]", history_name, def_text);
  456. }
  457. char *
  458. input_expand_dialog (const char *header, const char *text,
  459. const char *history_name, const char *def_text)
  460. {
  461. char *result;
  462. char *expanded;
  463. result = input_dialog (header, text, history_name, def_text);
  464. if (result) {
  465. expanded = tilde_expand (result);
  466. g_free (result);
  467. return expanded;
  468. }
  469. return result;
  470. }
  471. /* }}} */
  472. /* }}} */
  473. /*
  474. Cause emacs to enter folding mode for this file:
  475. Local variables:
  476. end:
  477. */