Check perl, python and ruby programs and substitute them in various files.
[midnight-commander.git] / lib / widget / button.c
blob51fdaac3985ecca0d33cd87839e201e6de6ed434
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
5 2004, 2005, 2006, 2007, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 Authors:
9 Radek Doulik, 1994, 1995
10 Miguel de Icaza, 1994, 1995
11 Jakub Jelinek, 1995
12 Andrej Borsenkow, 1996
13 Norbert Warmuth, 1997
14 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
16 This file is part of the Midnight Commander.
18 The Midnight Commander is free software: you can redistribute it
19 and/or modify it under the terms of the GNU General Public License as
20 published by the Free Software Foundation, either version 3 of the License,
21 or (at your option) any later version.
23 The Midnight Commander is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 /** \file button.c
33 * \brief Source: WButton widget
36 #include <config.h>
38 #include <stdlib.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/mouse.h"
44 #include "lib/strutil.h"
45 #include "lib/widget.h"
47 /*** global variables ****************************************************************************/
49 /*** file scope macro definitions ****************************************************************/
51 /*** file scope type declarations ****************************************************************/
53 /*** file scope variables ************************************************************************/
55 /*** file scope functions ************************************************************************/
57 static cb_ret_t
58 button_callback (Widget * w, widget_msg_t msg, int parm)
60 WButton *b = (WButton *) w;
61 int stop = 0;
62 int off = 0;
63 Dlg_head *h = b->widget.owner;
65 switch (msg)
67 case WIDGET_HOTKEY:
69 * Don't let the default button steal Enter from the current
70 * button. This is a workaround for the flawed event model
71 * when hotkeys are sent to all widgets before the key is
72 * handled by the current widget.
74 if (parm == '\n' && (Widget *) h->current->data == &b->widget)
76 button_callback (w, WIDGET_KEY, ' ');
77 return MSG_HANDLED;
80 if (parm == '\n' && b->flags == DEFPUSH_BUTTON)
82 button_callback (w, WIDGET_KEY, ' ');
83 return MSG_HANDLED;
86 if (b->text.hotkey != NULL && g_ascii_tolower ((gchar) b->text.hotkey[0]) == parm)
88 button_callback (w, WIDGET_KEY, ' ');
89 return MSG_HANDLED;
91 return MSG_NOT_HANDLED;
93 case WIDGET_KEY:
94 if (parm != ' ' && parm != '\n')
95 return MSG_NOT_HANDLED;
97 if (b->callback != NULL)
98 stop = b->callback (b, b->action);
99 if (b->callback == NULL || stop != 0)
101 h->ret_value = b->action;
102 dlg_stop (h);
104 return MSG_HANDLED;
106 case WIDGET_CURSOR:
107 switch (b->flags)
109 case DEFPUSH_BUTTON:
110 off = 3;
111 break;
112 case NORMAL_BUTTON:
113 off = 2;
114 break;
115 case NARROW_BUTTON:
116 off = 1;
117 break;
118 case HIDDEN_BUTTON:
119 default:
120 off = 0;
121 break;
123 widget_move (&b->widget, 0, b->hotpos + off);
124 return MSG_HANDLED;
126 case WIDGET_UNFOCUS:
127 case WIDGET_FOCUS:
128 case WIDGET_DRAW:
129 if (msg == WIDGET_UNFOCUS)
130 b->selected = FALSE;
131 else if (msg == WIDGET_FOCUS)
132 b->selected = TRUE;
134 widget_selectcolor (w, b->selected, FALSE);
135 widget_move (w, 0, 0);
137 switch (b->flags)
139 case DEFPUSH_BUTTON:
140 tty_print_string ("[< ");
141 break;
142 case NORMAL_BUTTON:
143 tty_print_string ("[ ");
144 break;
145 case NARROW_BUTTON:
146 tty_print_string ("[");
147 break;
148 case HIDDEN_BUTTON:
149 default:
150 return MSG_HANDLED;
153 hotkey_draw (w, b->text, b->selected);
155 switch (b->flags)
157 case DEFPUSH_BUTTON:
158 tty_print_string (" >]");
159 break;
160 case NORMAL_BUTTON:
161 tty_print_string (" ]");
162 break;
163 case NARROW_BUTTON:
164 tty_print_string ("]");
165 break;
166 default:
167 break;
169 return MSG_HANDLED;
171 case WIDGET_DESTROY:
172 release_hotkey (b->text);
173 return MSG_HANDLED;
175 default:
176 return default_proc (msg, parm);
180 /* --------------------------------------------------------------------------------------------- */
182 static int
183 button_event (Gpm_Event * event, void *data)
185 WButton *b = data;
187 if ((event->type & (GPM_DOWN | GPM_UP)) != 0)
189 Dlg_head *h = b->widget.owner;
191 dlg_select_widget (b);
192 if ((event->type & GPM_UP) != 0)
194 button_callback (&b->widget, WIDGET_KEY, ' ');
195 h->callback (h, &b->widget, DLG_POST_KEY, ' ', NULL);
196 return MOU_NORMAL;
199 return MOU_NORMAL;
202 /* --------------------------------------------------------------------------------------------- */
203 /*** public functions ****************************************************************************/
204 /* --------------------------------------------------------------------------------------------- */
206 WButton *
207 button_new (int y, int x, int action, button_flags_t flags, const char *text, bcback_fn callback)
209 WButton *b;
211 b = g_new (WButton, 1);
212 b->action = action;
213 b->flags = flags;
214 b->text = parse_hotkey (text);
216 init_widget (&b->widget, y, x, 1, button_get_len (b), button_callback, button_event);
218 b->selected = FALSE;
219 b->callback = callback;
220 widget_want_hotkey (b->widget, TRUE);
221 b->hotpos = (b->text.hotkey != NULL) ? str_term_width1 (b->text.start) : -1;
223 return b;
226 /* --------------------------------------------------------------------------------------------- */
228 const char *
229 button_get_text (const WButton * b)
231 if (b->text.hotkey != NULL)
232 return g_strconcat (b->text.start, "&", b->text.hotkey, b->text.end, (char *) NULL);
233 return g_strdup (b->text.start);
236 /* --------------------------------------------------------------------------------------------- */
238 void
239 button_set_text (WButton * b, const char *text)
241 release_hotkey (b->text);
242 b->text = parse_hotkey (text);
243 b->widget.cols = button_get_len (b);
244 dlg_redraw (b->widget.owner);
247 /* --------------------------------------------------------------------------------------------- */
250 button_get_len (const WButton * b)
252 int ret = hotkey_width (b->text);
254 switch (b->flags)
256 case DEFPUSH_BUTTON:
257 ret += 6;
258 break;
259 case NORMAL_BUTTON:
260 ret += 4;
261 break;
262 case NARROW_BUTTON:
263 ret += 2;
264 break;
265 case HIDDEN_BUTTON:
266 default:
267 return 0;
270 return ret;
273 /* --------------------------------------------------------------------------------------------- */