Added locale setting before calling dpkg to fix "link to" parsing on non-C locales
[midnight-commander.git] / lib / widget / button.c
blobf3f54c92bc6682d589f91a22588c701362801618
1 /* Widgets for the Midnight Commander
3 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
6 Authors: 1994, 1995 Radek Doulik
7 1994, 1995 Miguel de Icaza
8 1995 Jakub Jelinek
9 1996 Andrej Borsenkow
10 1997 Norbert Warmuth
11 2009, 2010 Andrew Borodin
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 /** \file button.c
30 * \brief Source: WButton widget
33 #include <config.h>
35 #include <stdlib.h>
37 #include "lib/global.h"
39 #include "lib/tty/tty.h"
40 #include "lib/tty/mouse.h"
41 #include "lib/strutil.h"
42 #include "lib/widget.h"
44 /*** global variables ****************************************************************************/
46 /*** file scope macro definitions ****************************************************************/
48 /*** file scope type declarations ****************************************************************/
50 /*** file scope variables ************************************************************************/
52 /*** file scope functions ************************************************************************/
54 static cb_ret_t
55 button_callback (Widget * w, widget_msg_t msg, int parm)
57 WButton *b = (WButton *) w;
58 int stop = 0;
59 int off = 0;
60 Dlg_head *h = b->widget.owner;
62 switch (msg)
64 case WIDGET_HOTKEY:
66 * Don't let the default button steal Enter from the current
67 * button. This is a workaround for the flawed event model
68 * when hotkeys are sent to all widgets before the key is
69 * handled by the current widget.
71 if (parm == '\n' && (Widget *) h->current->data == &b->widget)
73 button_callback (w, WIDGET_KEY, ' ');
74 return MSG_HANDLED;
77 if (parm == '\n' && b->flags == DEFPUSH_BUTTON)
79 button_callback (w, WIDGET_KEY, ' ');
80 return MSG_HANDLED;
83 if (b->text.hotkey != NULL &&
84 g_ascii_tolower ((gchar) b->text.hotkey[0]) == parm)
86 button_callback (w, WIDGET_KEY, ' ');
87 return MSG_HANDLED;
89 return MSG_NOT_HANDLED;
91 case WIDGET_KEY:
92 if (parm != ' ' && parm != '\n')
93 return MSG_NOT_HANDLED;
95 if (b->callback != NULL)
96 stop = b->callback (b, b->action);
97 if (b->callback == NULL || stop != 0)
99 h->ret_value = b->action;
100 dlg_stop (h);
102 return MSG_HANDLED;
104 case WIDGET_CURSOR:
105 switch (b->flags)
107 case DEFPUSH_BUTTON:
108 off = 3;
109 break;
110 case NORMAL_BUTTON:
111 off = 2;
112 break;
113 case NARROW_BUTTON:
114 off = 1;
115 break;
116 case HIDDEN_BUTTON:
117 default:
118 off = 0;
119 break;
121 widget_move (&b->widget, 0, b->hotpos + off);
122 return MSG_HANDLED;
124 case WIDGET_UNFOCUS:
125 case WIDGET_FOCUS:
126 case WIDGET_DRAW:
127 if (msg == WIDGET_UNFOCUS)
128 b->selected = FALSE;
129 else if (msg == WIDGET_FOCUS)
130 b->selected = TRUE;
132 widget_selectcolor (w, b->selected, FALSE);
133 widget_move (w, 0, 0);
135 switch (b->flags)
137 case DEFPUSH_BUTTON:
138 tty_print_string ("[< ");
139 break;
140 case NORMAL_BUTTON:
141 tty_print_string ("[ ");
142 break;
143 case NARROW_BUTTON:
144 tty_print_string ("[");
145 break;
146 case HIDDEN_BUTTON:
147 default:
148 return MSG_HANDLED;
151 hotkey_draw (w, b->text, b->selected);
153 switch (b->flags)
155 case DEFPUSH_BUTTON:
156 tty_print_string (" >]");
157 break;
158 case NORMAL_BUTTON:
159 tty_print_string (" ]");
160 break;
161 case NARROW_BUTTON:
162 tty_print_string ("]");
163 break;
164 default:
165 break;
167 return MSG_HANDLED;
169 case WIDGET_DESTROY:
170 release_hotkey (b->text);
171 return MSG_HANDLED;
173 default:
174 return default_proc (msg, parm);
178 /* --------------------------------------------------------------------------------------------- */
180 static int
181 button_event (Gpm_Event * event, void *data)
183 WButton *b = data;
185 if ((event->type & (GPM_DOWN | GPM_UP)) != 0)
187 Dlg_head *h = b->widget.owner;
189 dlg_select_widget (b);
190 if ((event->type & GPM_UP) != 0)
192 button_callback (&b->widget, WIDGET_KEY, ' ');
193 h->callback (h, &b->widget, DLG_POST_KEY, ' ', NULL);
194 return MOU_NORMAL;
197 return MOU_NORMAL;
200 /* --------------------------------------------------------------------------------------------- */
201 /*** public functions ****************************************************************************/
202 /* --------------------------------------------------------------------------------------------- */
204 WButton *
205 button_new (int y, int x, int action, button_flags_t flags, const char *text, bcback_fn callback)
207 WButton *b;
209 b = g_new (WButton, 1);
210 b->action = action;
211 b->flags = flags;
212 b->text = parse_hotkey (text);
214 init_widget (&b->widget, y, x, 1, button_get_len (b), button_callback, button_event);
216 b->selected = FALSE;
217 b->callback = callback;
218 widget_want_hotkey (b->widget, TRUE);
219 b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
221 return b;
224 /* --------------------------------------------------------------------------------------------- */
226 const char *
227 button_get_text (const WButton * b)
229 if (b->text.hotkey != NULL)
230 return g_strconcat (b->text.start, "&", b->text.hotkey, b->text.end, (char *) NULL);
231 return g_strdup (b->text.start);
234 /* --------------------------------------------------------------------------------------------- */
236 void
237 button_set_text (WButton * b, const char *text)
239 release_hotkey (b->text);
240 b->text = parse_hotkey (text);
241 b->widget.cols = button_get_len (b);
242 dlg_redraw (b->widget.owner);
245 /* --------------------------------------------------------------------------------------------- */
248 button_get_len (const WButton * b)
250 int ret = hotkey_width (b->text);
252 switch (b->flags)
254 case DEFPUSH_BUTTON:
255 ret += 6;
256 break;
257 case NORMAL_BUTTON:
258 ret += 4;
259 break;
260 case NARROW_BUTTON:
261 ret += 2;
262 break;
263 case HIDDEN_BUTTON:
264 default:
265 return 0;
268 return ret;
271 /* --------------------------------------------------------------------------------------------- */