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.
9 Radek Doulik, 1994, 1995
10 Miguel de Icaza, 1994, 1995
12 Andrej Borsenkow, 1996
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/>.
33 * \brief Source: WButton widget
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 ************************************************************************/
58 button_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
60 WButton
*b
= (WButton
*) w
;
63 WDialog
*h
= w
->owner
;
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
) == WIDGET (b
))
76 send_message (w
, sender
, WIDGET_KEY
, ' ', data
);
80 if (parm
== '\n' && b
->flags
== DEFPUSH_BUTTON
)
82 send_message (w
, sender
, WIDGET_KEY
, ' ', data
);
86 if (b
->text
.hotkey
!= NULL
&& g_ascii_tolower ((gchar
) b
->text
.hotkey
[0]) == parm
)
88 send_message (w
, sender
, WIDGET_KEY
, ' ', data
);
91 return MSG_NOT_HANDLED
;
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
;
123 widget_move (w
, 0, b
->hotpos
+ off
);
129 if (msg
== WIDGET_UNFOCUS
)
131 else if (msg
== WIDGET_FOCUS
)
134 widget_selectcolor (w
, b
->selected
, FALSE
);
135 widget_move (w
, 0, 0);
140 tty_print_string ("[< ");
143 tty_print_string ("[ ");
146 tty_print_string ("[");
153 hotkey_draw (w
, b
->text
, b
->selected
);
158 tty_print_string (" >]");
161 tty_print_string (" ]");
164 tty_print_string ("]");
172 release_hotkey (b
->text
);
176 return widget_default_callback (sender
, msg
, parm
, data
);
180 /* --------------------------------------------------------------------------------------------- */
183 button_event (Gpm_Event
* event
, void *data
)
185 Widget
*w
= WIDGET (data
);
187 if (!mouse_global_in_widget (event
, w
))
188 return MOU_UNHANDLED
;
190 if ((event
->type
& (GPM_DOWN
| GPM_UP
)) != 0)
192 dlg_select_widget (w
);
193 if ((event
->type
& GPM_UP
) != 0)
195 send_message (w
, NULL
, WIDGET_KEY
, ' ', NULL
);
196 w
->owner
->callback (w
->owner
, w
, DLG_POST_KEY
, ' ', NULL
);
203 /* --------------------------------------------------------------------------------------------- */
204 /*** public functions ****************************************************************************/
205 /* --------------------------------------------------------------------------------------------- */
208 button_new (int y
, int x
, int action
, button_flags_t flags
, const char *text
, bcback_fn callback
)
213 b
= g_new (WButton
, 1);
218 b
->text
= parse_hotkey (text
);
219 init_widget (w
, y
, x
, 1, button_get_len (b
), button_callback
, button_event
);
221 b
->callback
= callback
;
222 widget_want_hotkey (w
, TRUE
);
223 b
->hotpos
= (b
->text
.hotkey
!= NULL
) ? str_term_width1 (b
->text
.start
) : -1;
228 /* --------------------------------------------------------------------------------------------- */
231 button_get_text (const WButton
* b
)
233 if (b
->text
.hotkey
!= NULL
)
234 return g_strconcat (b
->text
.start
, "&", b
->text
.hotkey
, b
->text
.end
, (char *) NULL
);
235 return g_strdup (b
->text
.start
);
238 /* --------------------------------------------------------------------------------------------- */
241 button_set_text (WButton
* b
, const char *text
)
243 Widget
*w
= WIDGET (b
);
245 release_hotkey (b
->text
);
246 b
->text
= parse_hotkey (text
);
247 w
->cols
= button_get_len (b
);
248 if (w
->owner
!= NULL
)
249 send_message (w
, NULL
, WIDGET_DRAW
, 0, NULL
);
252 /* --------------------------------------------------------------------------------------------- */
255 button_get_len (const WButton
* b
)
257 int ret
= hotkey_width (b
->text
);
278 /* --------------------------------------------------------------------------------------------- */