quick.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. qw->widget = (Widget *) in;
  89. *qw->u.input.result = NULL;
  90. break;
  91. case quick_label:
  92. qw->widget = (Widget *) label_new (ypos, xpos, I18N (qw->u.label.text));
  93. break;
  94. case quick_groupbox:
  95. qw->widget = (Widget *) groupbox_new (ypos, xpos,
  96. qw->u.groupbox.height,
  97. qw->u.groupbox.width,
  98. I18N (qw->u.groupbox.title));
  99. break;
  100. case quick_radio:
  101. {
  102. int i;
  103. char **items = NULL;
  104. /* create the copy of radio_items to avoid mwmory leak */
  105. items = g_new0 (char *, qw->u.radio.count + 1);
  106. if (!qd->i18n)
  107. for (i = 0; i < qw->u.radio.count; i++)
  108. items[i] = g_strdup (_(qw->u.radio.items[i]));
  109. else
  110. for (i = 0; i < qw->u.radio.count; i++)
  111. items[i] = g_strdup (qw->u.radio.items[i]);
  112. r = radio_new (ypos, xpos, qw->u.radio.count, (const char **) items);
  113. r->pos = r->sel = *qw->u.radio.value;
  114. qw->widget = (Widget *) r;
  115. g_strfreev (items);
  116. break;
  117. }
  118. default:
  119. qw->widget = NULL;
  120. fprintf (stderr, "QuickWidget: unknown widget type\n");
  121. break;
  122. }
  123. if (qw->widget != NULL)
  124. {
  125. qw->widget->options |= qw->options; /* FIXME: cannot reset flags, setup only */
  126. add_widget (dd, qw->widget);
  127. }
  128. }
  129. while (nskip-- != 0)
  130. {
  131. dd->current = g_list_next (dd->current);
  132. if (dd->current == NULL)
  133. dd->current = dd->widgets;
  134. }
  135. return_val = run_dlg (dd);
  136. /* Get the data if we found something interesting */
  137. if (return_val != B_CANCEL)
  138. {
  139. for (qw = qd->widgets; qw->widget_type != quick_end; qw++)
  140. {
  141. switch (qw->widget_type)
  142. {
  143. case quick_checkbox:
  144. *qw->u.checkbox.state = ((WCheck *) qw->widget)->state & C_BOOL;
  145. break;
  146. case quick_input:
  147. if ((qw->u.input.flags & 2) != 0)
  148. *qw->u.input.result = tilde_expand (((WInput *) qw->widget)->buffer);
  149. else
  150. *qw->u.input.result = g_strdup (((WInput *) qw->widget)->buffer);
  151. break;
  152. case quick_radio:
  153. *qw->u.radio.value = ((WRadio *) qw->widget)->sel;
  154. break;
  155. default:
  156. break;
  157. }
  158. }
  159. }
  160. destroy_dlg (dd);
  161. return return_val;
  162. #undef I18N
  163. }
  164. /* --------------------------------------------------------------------------------------------- */