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
= BUTTON (w
);
61 WDialog
*h
= w
->owner
;
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
);
79 if (parm
== '\n' && b
->flags
== DEFPUSH_BUTTON
)
81 send_message (w
, sender
, MSG_KEY
, ' ', data
);
85 if (b
->text
.hotkey
!= NULL
&& g_ascii_tolower ((gchar
) b
->text
.hotkey
[0]) == parm
)
87 send_message (w
, sender
, MSG_KEY
, ' ', data
);
90 return MSG_NOT_HANDLED
;
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)
119 widget_move (w
, 0, b
->hotpos
+ off
);
125 if (msg
== MSG_UNFOCUS
)
127 else if (msg
== MSG_FOCUS
)
130 widget_selectcolor (w
, b
->selected
, FALSE
);
131 widget_move (w
, 0, 0);
136 tty_print_string ("[< ");
139 tty_print_string ("[ ");
142 tty_print_string ("[");
149 hotkey_draw (w
, b
->text
, b
->selected
);
154 tty_print_string (" >]");
157 tty_print_string (" ]");
160 tty_print_string ("]");
168 release_hotkey (b
->text
);
172 return widget_default_callback (w
, sender
, msg
, parm
, data
);
176 /* --------------------------------------------------------------------------------------------- */
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
);
199 /* --------------------------------------------------------------------------------------------- */
200 /*** public functions ****************************************************************************/
201 /* --------------------------------------------------------------------------------------------- */
204 button_new (int y
, int x
, int action
, button_flags_t flags
, const char *text
, bcback_fn callback
)
209 b
= g_new (WButton
, 1);
214 b
->text
= parse_hotkey (text
);
215 init_widget (w
, y
, x
, 1, button_get_len (b
), button_callback
, button_event
);
217 b
->callback
= callback
;
218 widget_want_hotkey (w
, TRUE
);
219 b
->hotpos
= (b
->text
.hotkey
!= NULL
) ? str_term_width1 (b
->text
.start
) : -1;
224 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
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
);
274 /* --------------------------------------------------------------------------------------------- */