quick.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. Widget based utility functions.
  3. Copyright (C) 1994-2021
  4. Free Software Foundation, Inc.
  5. Authors:
  6. Miguel de Icaza, 1994, 1995, 1996
  7. Radek Doulik, 1994, 1995
  8. Jakub Jelinek, 1995
  9. Andrej Borsenkow, 1995
  10. Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2011, 2012, 2013
  11. This file is part of the Midnight Commander.
  12. The Midnight Commander is free software: you can redistribute it
  13. and/or modify it under the terms of the GNU General Public License as
  14. published by the Free Software Foundation, either version 3 of the License,
  15. or (at your option) any later version.
  16. The Midnight Commander is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. /** \file quick.c
  24. * \brief Source: quick dialog engine
  25. */
  26. #include <config.h>
  27. #include <stdlib.h>
  28. #include <stdio.h> /* fprintf() */
  29. #include "lib/global.h"
  30. #include "lib/strutil.h" /* str_term_width1() */
  31. #include "lib/util.h" /* tilde_expand() */
  32. #include "lib/widget.h"
  33. /*** global variables ****************************************************************************/
  34. /*** file scope macro definitions ****************************************************************/
  35. #ifdef ENABLE_NLS
  36. #define I18N(x) (x = x != NULL && *x != '\0' ? _(x) : x)
  37. #else
  38. #define I18N(x) (x = x)
  39. #endif
  40. /*** file scope type declarations ****************************************************************/
  41. typedef struct
  42. {
  43. Widget *widget;
  44. quick_widget_t *quick_widget;
  45. } quick_widget_item_t;
  46. /*** file scope variables ************************************************************************/
  47. /* --------------------------------------------------------------------------------------------- */
  48. /*** file scope functions ************************************************************************/
  49. /* --------------------------------------------------------------------------------------------- */
  50. static WInput *
  51. quick_create_input (int y, int x, const quick_widget_t * qw)
  52. {
  53. WInput *in;
  54. in = input_new (y, x, input_colors, 8, qw->u.input.text, qw->u.input.histname,
  55. qw->u.input.completion_flags);
  56. in->is_password = qw->u.input.is_passwd;
  57. in->strip_password = qw->u.input.strip_passwd;
  58. return in;
  59. }
  60. /* --------------------------------------------------------------------------------------------- */
  61. static void
  62. quick_create_labeled_input (GArray * widgets, int *y, int x, quick_widget_t * quick_widget,
  63. int *width)
  64. {
  65. quick_widget_item_t in, label;
  66. label.quick_widget = g_new0 (quick_widget_t, 1);
  67. label.quick_widget->widget_type = quick_label;
  68. label.quick_widget->options = quick_widget->options;
  69. label.quick_widget->state = quick_widget->state;
  70. /* FIXME: this should be turned in depend of label_location */
  71. label.quick_widget->pos_flags = quick_widget->pos_flags;
  72. switch (quick_widget->u.input.label_location)
  73. {
  74. case input_label_above:
  75. label.widget = WIDGET (label_new (*y, x, I18N (quick_widget->u.input.label_text)));
  76. *y += label.widget->lines - 1;
  77. g_array_append_val (widgets, label);
  78. in.widget = WIDGET (quick_create_input (++(*y), x, quick_widget));
  79. in.quick_widget = quick_widget;
  80. g_array_append_val (widgets, in);
  81. *width = MAX (label.widget->cols, in.widget->cols);
  82. break;
  83. case input_label_left:
  84. label.widget = WIDGET (label_new (*y, x, I18N (quick_widget->u.input.label_text)));
  85. g_array_append_val (widgets, label);
  86. in.widget = WIDGET (quick_create_input (*y, x + label.widget->cols + 1, quick_widget));
  87. in.quick_widget = quick_widget;
  88. g_array_append_val (widgets, in);
  89. *width = label.widget->cols + in.widget->cols + 1;
  90. break;
  91. case input_label_right:
  92. in.widget = WIDGET (quick_create_input (*y, x, quick_widget));
  93. in.quick_widget = quick_widget;
  94. g_array_append_val (widgets, in);
  95. label.widget =
  96. WIDGET (label_new
  97. (*y, x + in.widget->cols + 1, I18N (quick_widget->u.input.label_text)));
  98. g_array_append_val (widgets, label);
  99. *width = label.widget->cols + in.widget->cols + 1;
  100. break;
  101. case input_label_below:
  102. in.widget = WIDGET (quick_create_input (*y, x, quick_widget));
  103. in.quick_widget = quick_widget;
  104. g_array_append_val (widgets, in);
  105. label.widget = WIDGET (label_new (++(*y), x, I18N (quick_widget->u.input.label_text)));
  106. *y += label.widget->lines - 1;
  107. g_array_append_val (widgets, label);
  108. *width = MAX (label.widget->cols, in.widget->cols);
  109. break;
  110. default:
  111. return;
  112. }
  113. INPUT (in.widget)->label = LABEL (label.widget);
  114. /* cross references */
  115. label.quick_widget->u.label.input = in.quick_widget;
  116. in.quick_widget->u.input.label = label.quick_widget;
  117. }
  118. /* --------------------------------------------------------------------------------------------- */
  119. /*** public functions ****************************************************************************/
  120. /* --------------------------------------------------------------------------------------------- */
  121. int
  122. quick_dialog_skip (quick_dialog_t * quick_dlg, int nskip)
  123. {
  124. int len;
  125. int blen = 0;
  126. int x, y; /* current positions */
  127. int y1 = 0; /* bottom of 1st column in case of two columns */
  128. int y2 = -1; /* start of two columns */
  129. int width1 = 0; /* width of single column */
  130. int width2 = 0; /* width of each of two columns */
  131. gboolean have_groupbox = FALSE;
  132. gboolean two_columns = FALSE;
  133. gboolean put_buttons = FALSE;
  134. /* x position of 1st column is 3 */
  135. const int x1 = 3;
  136. /* x position of 2nd column is 4 and it will be fixed later, after creation of all widgets */
  137. int x2 = 4;
  138. GArray *widgets;
  139. size_t i;
  140. quick_widget_t *quick_widget;
  141. WGroupbox *g = NULL;
  142. WDialog *dd;
  143. GList *input_labels = NULL; /* Widgets not directly requested by the user. */
  144. int return_val;
  145. len = str_term_width1 (I18N (quick_dlg->title)) + 6;
  146. quick_dlg->cols = MAX (quick_dlg->cols, len);
  147. y = 1;
  148. x = x1;
  149. /* create widgets */
  150. widgets = g_array_sized_new (FALSE, FALSE, sizeof (quick_widget_item_t), 8);
  151. for (quick_widget = quick_dlg->widgets; quick_widget->widget_type != quick_end; quick_widget++)
  152. {
  153. quick_widget_item_t item = { NULL, quick_widget };
  154. int width = 0;
  155. switch (quick_widget->widget_type)
  156. {
  157. case quick_checkbox:
  158. item.widget =
  159. WIDGET (check_new
  160. (++y, x, *quick_widget->u.checkbox.state,
  161. I18N (quick_widget->u.checkbox.text)));
  162. g_array_append_val (widgets, item);
  163. width = item.widget->cols;
  164. if (g != NULL)
  165. width += 2;
  166. if (two_columns)
  167. width2 = MAX (width2, width);
  168. else
  169. width1 = MAX (width1, width);
  170. break;
  171. case quick_button:
  172. /* single button */
  173. item.widget = WIDGET (button_new (++y, x, quick_widget->u.button.action,
  174. quick_widget->u.button.action == B_ENTER ?
  175. DEFPUSH_BUTTON : NORMAL_BUTTON,
  176. I18N (quick_widget->u.button.text),
  177. quick_widget->u.button.callback));
  178. g_array_append_val (widgets, item);
  179. width = item.widget->cols;
  180. if (g != NULL)
  181. width += 2;
  182. if (two_columns)
  183. width2 = MAX (width2, width);
  184. else
  185. width1 = MAX (width1, width);
  186. break;
  187. case quick_input:
  188. *quick_widget->u.input.result = NULL;
  189. y++;
  190. if (quick_widget->u.input.label_location != input_label_none)
  191. {
  192. quick_create_labeled_input (widgets, &y, x, quick_widget, &width);
  193. input_labels = g_list_prepend (input_labels, quick_widget->u.input.label);
  194. }
  195. else
  196. {
  197. item.widget = WIDGET (quick_create_input (y, x, quick_widget));
  198. g_array_append_val (widgets, item);
  199. width = item.widget->cols;
  200. }
  201. if (g != NULL)
  202. width += 2;
  203. if (two_columns)
  204. width2 = MAX (width2, width);
  205. else
  206. width1 = MAX (width1, width);
  207. break;
  208. case quick_label:
  209. item.widget = WIDGET (label_new (++y, x, I18N (quick_widget->u.label.text)));
  210. g_array_append_val (widgets, item);
  211. y += item.widget->lines - 1;
  212. width = item.widget->cols;
  213. if (g != NULL)
  214. width += 2;
  215. if (two_columns)
  216. width2 = MAX (width2, width);
  217. else
  218. width1 = MAX (width1, width);
  219. break;
  220. case quick_radio:
  221. {
  222. WRadio *r;
  223. char **items = NULL;
  224. /* create the copy of radio_items to avoid mwmory leak */
  225. items = g_new (char *, quick_widget->u.radio.count + 1);
  226. for (i = 0; i < (size_t) quick_widget->u.radio.count; i++)
  227. items[i] = g_strdup (_(quick_widget->u.radio.items[i]));
  228. items[i] = NULL;
  229. r = radio_new (++y, x, quick_widget->u.radio.count, (const char **) items);
  230. r->pos = r->sel = *quick_widget->u.radio.value;
  231. g_strfreev (items);
  232. item.widget = WIDGET (r);
  233. g_array_append_val (widgets, item);
  234. y += item.widget->lines - 1;
  235. width = item.widget->cols;
  236. if (g != NULL)
  237. width += 2;
  238. if (two_columns)
  239. width2 = MAX (width2, width);
  240. else
  241. width1 = MAX (width1, width);
  242. }
  243. break;
  244. case quick_start_groupbox:
  245. I18N (quick_widget->u.groupbox.title);
  246. len = str_term_width1 (quick_widget->u.groupbox.title);
  247. g = groupbox_new (++y, x, 1, len + 4, quick_widget->u.groupbox.title);
  248. item.widget = WIDGET (g);
  249. g_array_append_val (widgets, item);
  250. have_groupbox = TRUE;
  251. break;
  252. case quick_stop_groupbox:
  253. if (g != NULL)
  254. {
  255. Widget *w = WIDGET (g);
  256. y++;
  257. w->lines = y + 1 - w->y;
  258. g = NULL;
  259. g_array_append_val (widgets, item);
  260. }
  261. break;
  262. case quick_separator:
  263. y++;
  264. if (quick_widget->u.separator.line)
  265. {
  266. item.widget = WIDGET (hline_new (y, x, 1));
  267. g_array_append_val (widgets, item);
  268. }
  269. break;
  270. case quick_start_columns:
  271. y2 = y;
  272. g_array_append_val (widgets, item);
  273. two_columns = TRUE;
  274. break;
  275. case quick_next_column:
  276. x = x2;
  277. y1 = y;
  278. y = y2;
  279. break;
  280. case quick_stop_columns:
  281. x = x1;
  282. y = MAX (y1, y);
  283. g_array_append_val (widgets, item);
  284. two_columns = FALSE;
  285. break;
  286. case quick_buttons:
  287. /* start put several buttons in bottom line */
  288. if (quick_widget->u.separator.space)
  289. {
  290. y++;
  291. if (quick_widget->u.separator.line)
  292. item.widget = WIDGET (hline_new (y, 1, -1));
  293. }
  294. g_array_append_val (widgets, item);
  295. /* several buttons in bottom line */
  296. y++;
  297. quick_widget++;
  298. for (; quick_widget->widget_type == quick_button; quick_widget++)
  299. {
  300. item.widget = WIDGET (button_new (y, x++, quick_widget->u.button.action,
  301. quick_widget->u.button.action == B_ENTER ?
  302. DEFPUSH_BUTTON : NORMAL_BUTTON,
  303. I18N (quick_widget->u.button.text),
  304. quick_widget->u.button.callback));
  305. item.quick_widget = quick_widget;
  306. g_array_append_val (widgets, item);
  307. blen += item.widget->cols + 1;
  308. }
  309. /* stop dialog build here */
  310. blen--;
  311. quick_widget->widget_type = quick_end;
  312. quick_widget--;
  313. break;
  314. default:
  315. break;
  316. }
  317. }
  318. /* adjust dialog width */
  319. quick_dlg->cols = MAX (quick_dlg->cols, blen + 6);
  320. if (have_groupbox)
  321. {
  322. if (width1 != 0)
  323. width1 += 2;
  324. if (width2 != 0)
  325. width2 += 2;
  326. }
  327. if (width2 == 0)
  328. len = width1 + 6;
  329. else
  330. {
  331. len = width2 * 2 + 7;
  332. if (width1 != 0)
  333. len = MAX (len, width1 + 6);
  334. }
  335. quick_dlg->cols = MAX (quick_dlg->cols, len);
  336. width1 = quick_dlg->cols - 6;
  337. width2 = (quick_dlg->cols - 7) / 2;
  338. if (quick_dlg->x == -1 || quick_dlg->y == -1)
  339. dd = dlg_create (TRUE, 0, 0, y + 3, quick_dlg->cols, WPOS_CENTER | WPOS_TRYUP, FALSE,
  340. dialog_colors, quick_dlg->callback, quick_dlg->mouse_callback,
  341. quick_dlg->help, quick_dlg->title);
  342. else
  343. dd = dlg_create (TRUE, quick_dlg->y, quick_dlg->x, y + 3, quick_dlg->cols,
  344. WPOS_KEEP_DEFAULT, FALSE, dialog_colors, quick_dlg->callback,
  345. quick_dlg->mouse_callback, quick_dlg->help, quick_dlg->title);
  346. /* add widgets into the dialog */
  347. x2 = x1 + width2 + 1;
  348. g = NULL;
  349. two_columns = FALSE;
  350. x = (WIDGET (dd)->cols - blen) / 2;
  351. for (i = 0; i < widgets->len; i++)
  352. {
  353. quick_widget_item_t *item;
  354. int column_width;
  355. item = &g_array_index (widgets, quick_widget_item_t, i);
  356. column_width = two_columns ? width2 : width1;
  357. /* adjust widget width and x position */
  358. switch (item->quick_widget->widget_type)
  359. {
  360. case quick_label:
  361. {
  362. quick_widget_t *input = item->quick_widget->u.label.input;
  363. if (input != NULL && input->u.input.label_location == input_label_right)
  364. {
  365. /* location of this label will be adjusted later */
  366. break;
  367. }
  368. }
  369. MC_FALLTHROUGH;
  370. case quick_checkbox:
  371. case quick_radio:
  372. if (item->widget->x != x1)
  373. item->widget->x = x2;
  374. if (g != NULL)
  375. item->widget->x += 2;
  376. break;
  377. case quick_button:
  378. if (!put_buttons)
  379. {
  380. if (item->widget->x != x1)
  381. item->widget->x = x2;
  382. if (g != NULL)
  383. item->widget->x += 2;
  384. }
  385. else
  386. {
  387. item->widget->x = x;
  388. x += item->widget->cols + 1;
  389. }
  390. break;
  391. case quick_input:
  392. {
  393. Widget *label = WIDGET (INPUT (item->widget)->label);
  394. int width = column_width;
  395. if (g != NULL)
  396. width -= 4;
  397. switch (item->quick_widget->u.input.label_location)
  398. {
  399. case input_label_left:
  400. /* label was adjusted before; adjust input line */
  401. item->widget->x = label->x + label->cols + 1 - WIDGET (label->owner)->x;
  402. item->widget->cols = width - label->cols - 1;
  403. break;
  404. case input_label_right:
  405. if (item->widget->x != x1)
  406. item->widget->x = x2;
  407. if (g != NULL)
  408. item->widget->x += 2;
  409. item->widget->cols = width - label->cols - 1;
  410. label->x = item->widget->x + item->widget->cols + 1;
  411. break;
  412. default:
  413. if (item->widget->x != x1)
  414. item->widget->x = x2;
  415. if (g != NULL)
  416. item->widget->x += 2;
  417. item->widget->cols = width;
  418. break;
  419. }
  420. /* forced update internal variables of inpuit line */
  421. widget_set_size (item->widget, item->widget->y, item->widget->x, 1,
  422. item->widget->cols);
  423. }
  424. break;
  425. case quick_start_groupbox:
  426. g = GROUPBOX (item->widget);
  427. if (item->widget->x != x1)
  428. item->widget->x = x2;
  429. item->widget->cols = column_width;
  430. break;
  431. case quick_stop_groupbox:
  432. g = NULL;
  433. break;
  434. case quick_separator:
  435. if (item->widget != NULL)
  436. {
  437. if (g != NULL)
  438. {
  439. Widget *wg = WIDGET (g);
  440. HLINE (item->widget)->auto_adjust_cols = FALSE;
  441. item->widget->x = wg->x + 1 - WIDGET (wg->owner)->x;
  442. item->widget->cols = wg->cols;
  443. }
  444. else if (two_columns)
  445. {
  446. HLINE (item->widget)->auto_adjust_cols = FALSE;
  447. if (item->widget->x != x1)
  448. item->widget->x = x2;
  449. item->widget->x--;
  450. item->widget->cols = column_width + 2;
  451. }
  452. else
  453. HLINE (item->widget)->auto_adjust_cols = TRUE;
  454. }
  455. break;
  456. case quick_start_columns:
  457. two_columns = TRUE;
  458. break;
  459. case quick_stop_columns:
  460. two_columns = FALSE;
  461. break;
  462. case quick_buttons:
  463. /* several buttons in bottom line */
  464. put_buttons = TRUE;
  465. break;
  466. default:
  467. break;
  468. }
  469. if (item->widget != NULL)
  470. {
  471. unsigned long id;
  472. /* add widget into dialog */
  473. item->widget->options |= item->quick_widget->options; /* FIXME: cannot reset flags, setup only */
  474. item->widget->state |= item->quick_widget->state; /* FIXME: cannot reset flags, setup only */
  475. id = group_add_widget_autopos (GROUP (dd), item->widget, item->quick_widget->pos_flags,
  476. NULL);
  477. if (item->quick_widget->id != NULL)
  478. *item->quick_widget->id = id;
  479. }
  480. }
  481. /* skip frame widget */
  482. if (dd->bg != NULL)
  483. nskip++;
  484. while (nskip-- != 0)
  485. group_set_current_widget_next (GROUP (dd));
  486. return_val = dlg_run (dd);
  487. /* Get the data if we found something interesting */
  488. if (return_val != B_CANCEL)
  489. for (i = 0; i < widgets->len; i++)
  490. {
  491. quick_widget_item_t *item;
  492. item = &g_array_index (widgets, quick_widget_item_t, i);
  493. switch (item->quick_widget->widget_type)
  494. {
  495. case quick_checkbox:
  496. *item->quick_widget->u.checkbox.state = CHECK (item->widget)->state;
  497. break;
  498. case quick_input:
  499. if ((item->quick_widget->u.input.completion_flags & INPUT_COMPLETE_CD) != 0)
  500. *item->quick_widget->u.input.result =
  501. tilde_expand (INPUT (item->widget)->buffer);
  502. else
  503. *item->quick_widget->u.input.result = g_strdup (INPUT (item->widget)->buffer);
  504. break;
  505. case quick_radio:
  506. *item->quick_widget->u.radio.value = RADIO (item->widget)->sel;
  507. break;
  508. default:
  509. break;
  510. }
  511. }
  512. widget_destroy (WIDGET (dd));
  513. g_list_free_full (input_labels, g_free); /* destroy input labels created before */
  514. g_array_free (widgets, TRUE);
  515. return return_val;
  516. }
  517. /* --------------------------------------------------------------------------------------------- */