2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
8 Radek Doulik, 1994, 1995
9 Miguel de Icaza, 1994, 1995
11 Andrej Borsenkow, 1996
13 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013
15 This file is part of the Midnight Commander.
17 The Midnight Commander is free software: you can redistribute it
18 and/or modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation, either version 3 of the License,
20 or (at your option) any later version.
22 The Midnight Commander is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 * \brief Source: WButton widget
39 #include "lib/global.h"
41 #include "lib/tty/tty.h"
42 #include "lib/tty/mouse.h"
43 #include "lib/strutil.h"
44 #include "lib/widget.h"
46 /*** global variables ****************************************************************************/
48 /*** file scope macro definitions ****************************************************************/
50 /*** file scope type declarations ****************************************************************/
52 /*** file scope variables ************************************************************************/
54 /*** file scope functions ************************************************************************/
57 button_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
59 WButton
*b
= BUTTON (w
);
60 WDialog
*h
= w
->owner
;
67 * Don't let the default button steal Enter from the current
68 * button. This is a workaround for the flawed event model
69 * when hotkeys are sent to all widgets before the key is
70 * handled by the current widget.
72 if (parm
== '\n' && WIDGET (h
->current
->data
) == WIDGET (b
))
74 send_message (w
, sender
, MSG_KEY
, ' ', data
);
78 if (parm
== '\n' && b
->flags
== DEFPUSH_BUTTON
)
80 send_message (w
, sender
, MSG_KEY
, ' ', data
);
84 if (b
->text
.hotkey
!= NULL
&& g_ascii_tolower ((gchar
) b
->text
.hotkey
[0]) == parm
)
86 send_message (w
, sender
, MSG_KEY
, ' ', data
);
89 return MSG_NOT_HANDLED
;
92 if (parm
!= ' ' && parm
!= '\n')
93 return MSG_NOT_HANDLED
;
95 h
->ret_value
= b
->action
;
96 if (b
->callback
== NULL
|| b
->callback (b
, b
->action
) != 0)
118 widget_move (w
, 0, b
->hotpos
+ off
);
124 if (msg
== MSG_UNFOCUS
)
126 else if (msg
== MSG_FOCUS
)
129 widget_selectcolor (w
, b
->selected
, FALSE
);
130 widget_move (w
, 0, 0);
135 tty_print_string ("[< ");
138 tty_print_string ("[ ");
141 tty_print_string ("[");
148 hotkey_draw (w
, b
->text
, b
->selected
);
153 tty_print_string (" >]");
156 tty_print_string (" ]");
159 tty_print_string ("]");
167 release_hotkey (b
->text
);
171 return widget_default_callback (w
, sender
, msg
, parm
, data
);
175 /* --------------------------------------------------------------------------------------------- */
178 button_event (Gpm_Event
* event
, void *data
)
180 Widget
*w
= WIDGET (data
);
182 if (!mouse_global_in_widget (event
, w
))
183 return MOU_UNHANDLED
;
185 if ((event
->type
& (GPM_DOWN
| GPM_UP
)) != 0)
187 dlg_select_widget (w
);
188 if ((event
->type
& GPM_UP
) != 0)
190 send_message (w
, NULL
, MSG_KEY
, ' ', NULL
);
191 send_message (w
->owner
, w
, MSG_POST_KEY
, ' ', NULL
);
198 /* --------------------------------------------------------------------------------------------- */
199 /*** public functions ****************************************************************************/
200 /* --------------------------------------------------------------------------------------------- */
203 button_new (int y
, int x
, int action
, button_flags_t flags
, const char *text
, bcback_fn callback
)
208 b
= g_new (WButton
, 1);
213 b
->text
= parse_hotkey (text
);
214 widget_init (w
, y
, x
, 1, button_get_len (b
), button_callback
, button_event
);
216 b
->callback
= callback
;
217 widget_want_hotkey (w
, TRUE
);
218 b
->hotpos
= (b
->text
.hotkey
!= NULL
) ? str_term_width1 (b
->text
.start
) : -1;
223 /* --------------------------------------------------------------------------------------------- */
226 button_get_text (const WButton
* b
)
228 if (b
->text
.hotkey
!= NULL
)
229 return g_strconcat (b
->text
.start
, "&", b
->text
.hotkey
, b
->text
.end
, (char *) NULL
);
230 return g_strdup (b
->text
.start
);
233 /* --------------------------------------------------------------------------------------------- */
236 button_set_text (WButton
* b
, const char *text
)
238 Widget
*w
= WIDGET (b
);
240 release_hotkey (b
->text
);
241 b
->text
= parse_hotkey (text
);
242 b
->hotpos
= (b
->text
.hotkey
!= NULL
) ? str_term_width1 (b
->text
.start
) : -1;
243 w
->cols
= button_get_len (b
);
247 /* --------------------------------------------------------------------------------------------- */
250 button_get_len (const WButton
* b
)
252 int ret
= hotkey_width (b
->text
);
273 /* --------------------------------------------------------------------------------------------- */