quick.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "lib/global.h"
  27. #include "lib/util.h" /* tilde_expand() */
  28. #include "lib/widget.h"
  29. /*** global variables ****************************************************************************/
  30. /*** file scope macro definitions ****************************************************************/
  31. /*** file scope type declarations ****************************************************************/
  32. /*** file scope variables ************************************************************************/
  33. /*** file scope functions ************************************************************************/
  34. /* --------------------------------------------------------------------------------------------- */
  35. /*** public functions ****************************************************************************/
  36. /* --------------------------------------------------------------------------------------------- */
  37. int
  38. quick_dialog_skip (QuickDialog * qd, int nskip)
  39. {
  40. #ifdef ENABLE_NLS
  41. #define I18N(x) (x = !qd->i18n && x != NULL && *x != '\0' ? _(x): x)
  42. #else
  43. #define I18N(x) (x = x)
  44. #endif
  45. Dlg_head *dd;
  46. QuickWidget *qw;
  47. WInput *in;
  48. WRadio *r;
  49. int return_val;
  50. I18N (qd->title);
  51. if ((qd->xpos == -1) || (qd->ypos == -1))
  52. dd = create_dlg (TRUE, 0, 0, qd->ylen, qd->xlen,
  53. dialog_colors, qd->callback, qd->help, qd->title,
  54. DLG_CENTER | DLG_TRYUP | DLG_REVERSE);
  55. else
  56. dd = create_dlg (TRUE, qd->ypos, qd->xpos, qd->ylen, qd->xlen,
  57. dialog_colors, qd->callback, qd->help, qd->title, DLG_REVERSE);
  58. for (qw = qd->widgets; qw->widget_type != quick_end; qw++)
  59. {
  60. int xpos;
  61. int ypos;
  62. xpos = (qd->xlen * qw->relative_x) / qw->x_divisions;
  63. ypos = (qd->ylen * qw->relative_y) / qw->y_divisions;
  64. switch (qw->widget_type)
  65. {
  66. case quick_checkbox:
  67. qw->widget =
  68. (Widget *) check_new (ypos, xpos, *qw->u.checkbox.state,
  69. I18N (qw->u.checkbox.text));
  70. break;
  71. case quick_button:
  72. qw->widget = (Widget *) button_new (ypos, xpos, qw->u.button.action,
  73. (qw->u.button.action ==
  74. B_ENTER) ? DEFPUSH_BUTTON : NORMAL_BUTTON,
  75. I18N (qw->u.button.text), qw->u.button.callback);
  76. break;
  77. case quick_input:
  78. in = input_new (ypos, xpos, input_get_default_colors (),
  79. qw->u.input.len, qw->u.input.text, qw->u.input.histname,
  80. INPUT_COMPLETE_DEFAULT);
  81. in->is_password = (qw->u.input.flags == 1);
  82. if ((qw->u.input.flags & 2) != 0)
  83. in->completion_flags |= INPUT_COMPLETE_CD;
  84. qw->widget = (Widget *) in;
  85. *qw->u.input.result = NULL;
  86. break;
  87. case quick_label:
  88. qw->widget = (Widget *) label_new (ypos, xpos, I18N (qw->u.label.text));
  89. break;
  90. case quick_groupbox:
  91. qw->widget = (Widget *) groupbox_new (ypos, xpos,
  92. qw->u.groupbox.height,
  93. qw->u.groupbox.width,
  94. I18N (qw->u.groupbox.title));
  95. break;
  96. case quick_radio:
  97. {
  98. int i;
  99. char **items = NULL;
  100. /* create the copy of radio_items to avoid mwmory leak */
  101. items = g_new0 (char *, qw->u.radio.count + 1);
  102. if (!qd->i18n)
  103. for (i = 0; i < qw->u.radio.count; i++)
  104. items[i] = g_strdup (_(qw->u.radio.items[i]));
  105. else
  106. for (i = 0; i < qw->u.radio.count; i++)
  107. items[i] = g_strdup (qw->u.radio.items[i]);
  108. r = radio_new (ypos, xpos, qw->u.radio.count, (const char **) items);
  109. r->pos = r->sel = *qw->u.radio.value;
  110. qw->widget = (Widget *) r;
  111. g_strfreev (items);
  112. break;
  113. }
  114. default:
  115. qw->widget = NULL;
  116. fprintf (stderr, "QuickWidget: unknown widget type\n");
  117. break;
  118. }
  119. if (qw->widget != NULL)
  120. {
  121. qw->widget->options |= qw->options; /* FIXME: cannot reset flags, setup only */
  122. add_widget (dd, qw->widget);
  123. }
  124. }
  125. while (nskip-- != 0)
  126. {
  127. dd->current = g_list_next (dd->current);
  128. if (dd->current == NULL)
  129. dd->current = dd->widgets;
  130. }
  131. return_val = run_dlg (dd);
  132. /* Get the data if we found something interesting */
  133. if (return_val != B_CANCEL)
  134. {
  135. for (qw = qd->widgets; qw->widget_type != quick_end; qw++)
  136. {
  137. switch (qw->widget_type)
  138. {
  139. case quick_checkbox:
  140. *qw->u.checkbox.state = ((WCheck *) qw->widget)->state & C_BOOL;
  141. break;
  142. case quick_input:
  143. if ((qw->u.input.flags & 2) != 0)
  144. *qw->u.input.result = tilde_expand (((WInput *) qw->widget)->buffer);
  145. else
  146. *qw->u.input.result = g_strdup (((WInput *) qw->widget)->buffer);
  147. break;
  148. case quick_radio:
  149. *qw->u.radio.value = ((WRadio *) qw->widget)->sel;
  150. break;
  151. default:
  152. break;
  153. }
  154. }
  155. }
  156. destroy_dlg (dd);
  157. return return_val;
  158. #undef I18N
  159. }
  160. /* --------------------------------------------------------------------------------------------- */