quick.c 6.8 KB

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