quick.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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->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->help, qd->title, DLG_REVERSE);
  62. for (qw = qd->widgets; qw->widget_type != quick_end; qw++)
  63. {
  64. int xpos;
  65. int ypos;
  66. xpos = (qd->xlen * qw->relative_x) / qw->x_divisions;
  67. ypos = (qd->ylen * qw->relative_y) / qw->y_divisions;
  68. switch (qw->widget_type)
  69. {
  70. case quick_checkbox:
  71. qw->widget =
  72. (Widget *) check_new (ypos, xpos, *qw->u.checkbox.state,
  73. I18N (qw->u.checkbox.text));
  74. break;
  75. case quick_button:
  76. qw->widget = (Widget *) button_new (ypos, xpos, qw->u.button.action,
  77. (qw->u.button.action ==
  78. B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
  79. I18N (qw->u.button.text), qw->u.button.callback);
  80. break;
  81. case quick_input:
  82. in = input_new (ypos, xpos, input_get_default_colors (),
  83. qw->u.input.len, qw->u.input.text, qw->u.input.histname,
  84. INPUT_COMPLETE_DEFAULT);
  85. in->is_password = (qw->u.input.flags == 1);
  86. if ((qw->u.input.flags & 2) != 0)
  87. in->completion_flags |= INPUT_COMPLETE_CD;
  88. if ((qw->u.input.flags & 4) != 0)
  89. in->strip_password = TRUE;
  90. qw->widget = (Widget *) in;
  91. *qw->u.input.result = NULL;
  92. break;
  93. case quick_label:
  94. qw->widget = (Widget *) label_new (ypos, xpos, I18N (qw->u.label.text));
  95. break;
  96. case quick_groupbox:
  97. qw->widget = (Widget *) groupbox_new (ypos, xpos,
  98. qw->u.groupbox.height,
  99. qw->u.groupbox.width,
  100. I18N (qw->u.groupbox.title));
  101. break;
  102. case quick_radio:
  103. {
  104. int i;
  105. char **items = NULL;
  106. /* create the copy of radio_items to avoid mwmory leak */
  107. items = g_new0 (char *, qw->u.radio.count + 1);
  108. if (!qd->i18n)
  109. for (i = 0; i < qw->u.radio.count; i++)
  110. items[i] = g_strdup (_(qw->u.radio.items[i]));
  111. else
  112. for (i = 0; i < qw->u.radio.count; i++)
  113. items[i] = g_strdup (qw->u.radio.items[i]);
  114. r = radio_new (ypos, xpos, qw->u.radio.count, (const char **) items);
  115. r->pos = r->sel = *qw->u.radio.value;
  116. qw->widget = (Widget *) r;
  117. g_strfreev (items);
  118. break;
  119. }
  120. default:
  121. qw->widget = NULL;
  122. fprintf (stderr, "QuickWidget: unknown widget type\n");
  123. break;
  124. }
  125. if (qw->widget != NULL)
  126. {
  127. qw->widget->options |= qw->options; /* FIXME: cannot reset flags, setup only */
  128. add_widget (dd, qw->widget);
  129. }
  130. }
  131. while (nskip-- != 0)
  132. {
  133. dd->current = g_list_next (dd->current);
  134. if (dd->current == NULL)
  135. dd->current = dd->widgets;
  136. }
  137. return_val = run_dlg (dd);
  138. /* Get the data if we found something interesting */
  139. if (return_val != B_CANCEL)
  140. {
  141. for (qw = qd->widgets; qw->widget_type != quick_end; qw++)
  142. {
  143. switch (qw->widget_type)
  144. {
  145. case quick_checkbox:
  146. *qw->u.checkbox.state = ((WCheck *) qw->widget)->state & C_BOOL;
  147. break;
  148. case quick_input:
  149. if ((qw->u.input.flags & 2) != 0)
  150. *qw->u.input.result = tilde_expand (((WInput *) qw->widget)->buffer);
  151. else
  152. *qw->u.input.result = g_strdup (((WInput *) qw->widget)->buffer);
  153. break;
  154. case quick_radio:
  155. *qw->u.radio.value = ((WRadio *) qw->widget)->sel;
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. }
  162. destroy_dlg (dd);
  163. return return_val;
  164. #undef I18N
  165. }
  166. /* --------------------------------------------------------------------------------------------- */