Ticket #2111: allow pause in copy/move/delete file operation.
[midnight-commander.git] / lib / widget / button.c
blob4740f9373af00eb816a03688bd2b3ed0e5e271d0
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 * sender, widget_msg_t msg, int parm, void *data)
60 WButton *b = BUTTON (w);
61 WDialog *h = w->owner;
62 int off = 0;
64 switch (msg)
66 case MSG_HOTKEY:
68 * Don't let the default button steal Enter from the current
69 * button. This is a workaround for the flawed event model
70 * when hotkeys are sent to all widgets before the key is
71 * handled by the current widget.
73 if (parm == '\n' && WIDGET (h->current->data) == WIDGET (b))
75 send_message (w, sender, MSG_KEY, ' ', data);
76 return MSG_HANDLED;
79 if (parm == '\n' && b->flags == DEFPUSH_BUTTON)
81 send_message (w, sender, MSG_KEY, ' ', data);
82 return MSG_HANDLED;
85 if (b->text.hotkey != NULL && g_ascii_tolower ((gchar) b->text.hotkey[0]) == parm)
87 send_message (w, sender, MSG_KEY, ' ', data);
88 return MSG_HANDLED;
90 return MSG_NOT_HANDLED;
92 case MSG_KEY:
93 if (parm != ' ' && parm != '\n')
94 return MSG_NOT_HANDLED;
96 h->ret_value = b->action;
97 if (b->callback == NULL || b->callback (b, b->action) != 0)
98 dlg_stop (h);
100 return MSG_HANDLED;
102 case MSG_CURSOR:
103 switch (b->flags)
105 case DEFPUSH_BUTTON:
106 off = 3;
107 break;
108 case NORMAL_BUTTON:
109 off = 2;
110 break;
111 case NARROW_BUTTON:
112 off = 1;
113 break;
114 case HIDDEN_BUTTON:
115 default:
116 off = 0;
117 break;
119 widget_move (w, 0, b->hotpos + off);
120 return MSG_HANDLED;
122 case MSG_UNFOCUS:
123 case MSG_FOCUS:
124 case MSG_DRAW:
125 if (msg == MSG_UNFOCUS)
126 b->selected = FALSE;
127 else if (msg == MSG_FOCUS)
128 b->selected = TRUE;
130 widget_selectcolor (w, b->selected, FALSE);
131 widget_move (w, 0, 0);
133 switch (b->flags)
135 case DEFPUSH_BUTTON:
136 tty_print_string ("[< ");
137 break;
138 case NORMAL_BUTTON:
139 tty_print_string ("[ ");
140 break;
141 case NARROW_BUTTON:
142 tty_print_string ("[");
143 break;
144 case HIDDEN_BUTTON:
145 default:
146 return MSG_HANDLED;
149 hotkey_draw (w, b->text, b->selected);
151 switch (b->flags)
153 case DEFPUSH_BUTTON:
154 tty_print_string (" >]");
155 break;
156 case NORMAL_BUTTON:
157 tty_print_string (" ]");
158 break;
159 case NARROW_BUTTON:
160 tty_print_string ("]");
161 break;
162 default:
163 break;
165 return MSG_HANDLED;
167 case MSG_DESTROY:
168 release_hotkey (b->text);
169 return MSG_HANDLED;
171 default:
172 return widget_default_callback (w, sender, msg, parm, data);
176 /* --------------------------------------------------------------------------------------------- */
178 static int
179 button_event (Gpm_Event * event, void *data)
181 Widget *w = WIDGET (data);
183 if (!mouse_global_in_widget (event, w))
184 return MOU_UNHANDLED;
186 if ((event->type & (GPM_DOWN | GPM_UP)) != 0)
188 dlg_select_widget (w);
189 if ((event->type & GPM_UP) != 0)
191 send_message (w, NULL, MSG_KEY, ' ', NULL);
192 send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
196 return MOU_NORMAL;
199 /* --------------------------------------------------------------------------------------------- */
200 /*** public functions ****************************************************************************/
201 /* --------------------------------------------------------------------------------------------- */
203 WButton *
204 button_new (int y, int x, int action, button_flags_t flags, const char *text, bcback_fn callback)
206 WButton *b;
207 Widget *w;
209 b = g_new (WButton, 1);
210 w = WIDGET (b);
212 b->action = action;
213 b->flags = flags;
214 b->text = parse_hotkey (text);
215 init_widget (w, y, x, 1, button_get_len (b), button_callback, button_event);
216 b->selected = FALSE;
217 b->callback = callback;
218 widget_want_hotkey (w, 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 Widget *w = WIDGET (b);
241 release_hotkey (b->text);
242 b->text = parse_hotkey (text);
243 w->cols = button_get_len (b);
244 if (w->owner != NULL)
245 send_message (w, NULL, MSG_DRAW, 0, NULL);
248 /* --------------------------------------------------------------------------------------------- */
251 button_get_len (const WButton * b)
253 int ret = hotkey_width (b->text);
255 switch (b->flags)
257 case DEFPUSH_BUTTON:
258 ret += 6;
259 break;
260 case NORMAL_BUTTON:
261 ret += 4;
262 break;
263 case NARROW_BUTTON:
264 ret += 2;
265 break;
266 case HIDDEN_BUTTON:
267 default:
268 return 0;
271 return ret;
274 /* --------------------------------------------------------------------------------------------- */