quick.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. Widget based utility functions.
  3. Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  4. 2005, 2006, 2007, 2008, 2009, 2010, 2011
  5. The Free Software Foundation, Inc.
  6. Authors:
  7. Miguel de Icaza, 1994, 1995, 1996
  8. Radek Doulik, 1994, 1995
  9. Jakub Jelinek, 1995
  10. Andrej Borsenkow, 1995
  11. Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
  12. This file is part of the Midnight Commander.
  13. The Midnight Commander is free software: you can redistribute it
  14. and/or modify it under the terms of the GNU General Public License as
  15. published by the Free Software Foundation, either version 3 of the License,
  16. or (at your option) any later version.
  17. The Midnight Commander is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /** \file quick.c
  25. * \brief Source: quick dialog engine
  26. */
  27. #include <config.h>
  28. #include <stdlib.h>
  29. #include <stdio.h> /* fprintf() */
  30. #include "lib/global.h"
  31. #include "lib/util.h" /* tilde_expand() */
  32. #include "lib/widget.h"
  33. /*** global variables ****************************************************************************/
  34. /*** file scope macro definitions ****************************************************************/
  35. /*** file scope type declarations ****************************************************************/
  36. /*** file scope variables ************************************************************************/
  37. /*** file scope functions ************************************************************************/
  38. /* --------------------------------------------------------------------------------------------- */
  39. /*** public functions ****************************************************************************/
  40. /* --------------------------------------------------------------------------------------------- */
  41. int
  42. quick_dialog_skip (QuickDialog * qd, int nskip)
  43. {
  44. #ifdef ENABLE_NLS
  45. #define I18N(x) (x = !qd->i18n && x != NULL && *x != '\0' ? _(x): x)
  46. #else
  47. #define I18N(x) (x = x)
  48. #endif
  49. Dlg_head *dd;
  50. QuickWidget *qw;
  51. WInput *in;
  52. WRadio *r;
  53. int return_val;
  54. I18N (qd->title);
  55. if ((qd->xpos == -1) || (qd->ypos == -1))
  56. dd = create_dlg (TRUE, 0, 0, qd->ylen, qd->xlen,
  57. dialog_colors, qd->callback, qd->mouse, qd->help, qd->title,
  58. DLG_CENTER | DLG_TRYUP | DLG_REVERSE);
  59. else
  60. dd = create_dlg (TRUE, qd->ypos, qd->xpos, qd->ylen, qd->xlen,
  61. dialog_colors, qd->callback, qd->mouse, qd->help, qd->title,
  62. DLG_REVERSE);
  63. for (qw = qd->widgets; qw->widget_type != quick_end; qw++)
  64. {
  65. int xpos;
  66. int ypos;
  67. xpos = (qd->xlen * qw->relative_x) / qw->x_divisions;
  68. ypos = (qd->ylen * qw->relative_y) / qw->y_divisions;
  69. switch (qw->widget_type)
  70. {
  71. case quick_checkbox:
  72. qw->widget =
  73. (Widget *) check_new (ypos, xpos, *qw->u.checkbox.state,
  74. I18N (qw->u.checkbox.text));
  75. break;
  76. case quick_button:
  77. qw->widget = (Widget *) button_new (ypos, xpos, qw->u.button.action,
  78. (qw->u.button.action ==
  79. B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
  80. I18N (qw->u.button.text), qw->u.button.callback);
  81. break;
  82. case quick_input:
  83. in = input_new (ypos, xpos, input_get_default_colors (),
  84. qw->u.input.len, qw->u.input.text, qw->u.input.histname,
  85. INPUT_COMPLETE_DEFAULT);
  86. in->is_password = (qw->u.input.flags == 1);
  87. if ((qw->u.input.flags & 2) != 0)
  88. in->completion_flags |= INPUT_COMPLETE_CD;
  89. if ((qw->u.input.flags & 4) != 0)
  90. in->strip_password = TRUE;
  91. qw->widget = (Widget *) in;
  92. *qw->u.input.result = NULL;
  93. break;
  94. case quick_label:
  95. qw->widget = (Widget *) label_new (ypos, xpos, I18N (qw->u.label.text));
  96. break;
  97. case quick_groupbox:
  98. qw->widget = (Widget *) groupbox_new (ypos, xpos,
  99. qw->u.groupbox.height,
  100. qw->u.groupbox.width,
  101. I18N (qw->u.groupbox.title));
  102. break;
  103. case quick_radio:
  104. {
  105. int i;
  106. char **items = NULL;
  107. /* create the copy of radio_items to avoid mwmory leak */
  108. items = g_new0 (char *, qw->u.radio.count + 1);
  109. if (!qd->i18n)
  110. for (i = 0; i < qw->u.radio.count; i++)
  111. items[i] = g_strdup (_(qw->u.radio.items[i]));
  112. else
  113. for (i = 0; i < qw->u.radio.count; i++)
  114. items[i] = g_strdup (qw->u.radio.items[i]);
  115. r = radio_new (ypos, xpos, qw->u.radio.count, (const char **) items);
  116. r->pos = r->sel = *qw->u.radio.value;
  117. qw->widget = (Widget *) r;
  118. g_strfreev (items);
  119. break;
  120. }
  121. default:
  122. qw->widget = NULL;
  123. fprintf (stderr, "QuickWidget: unknown widget type\n");
  124. break;
  125. }
  126. if (qw->widget != NULL)
  127. {
  128. qw->widget->options |= qw->options; /* FIXME: cannot reset flags, setup only */
  129. add_widget (dd, qw->widget);
  130. }
  131. }
  132. while (nskip-- != 0)
  133. {
  134. dd->current = g_list_next (dd->current);
  135. if (dd->current == NULL)
  136. dd->current = dd->widgets;
  137. }
  138. return_val = run_dlg (dd);
  139. /* Get the data if we found something interesting */
  140. if (return_val != B_CANCEL)
  141. {
  142. for (qw = qd->widgets; qw->widget_type != quick_end; qw++)
  143. {
  144. switch (qw->widget_type)
  145. {
  146. case quick_checkbox:
  147. *qw->u.checkbox.state = ((WCheck *) qw->widget)->state & C_BOOL;
  148. break;
  149. case quick_input:
  150. if ((qw->u.input.flags & 2) != 0)
  151. *qw->u.input.result = tilde_expand (((WInput *) qw->widget)->buffer);
  152. else
  153. *qw->u.input.result = g_strdup (((WInput *) qw->widget)->buffer);
  154. break;
  155. case quick_radio:
  156. *qw->u.radio.value = ((WRadio *) qw->widget)->sel;
  157. break;
  158. default:
  159. break;
  160. }
  161. }
  162. }
  163. destroy_dlg (dd);
  164. return return_val;
  165. #undef I18N
  166. }
  167. /* --------------------------------------------------------------------------------------------- */