2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2024
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-2022
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/strutil.h"
43 #include "lib/widget.h"
45 /*** global variables ****************************************************************************/
47 /*** file scope macro definitions ****************************************************************/
49 /*** file scope type declarations ****************************************************************/
51 /*** file scope variables ************************************************************************/
53 /* --------------------------------------------------------------------------------------------- */
54 /*** file scope functions ************************************************************************/
55 /* --------------------------------------------------------------------------------------------- */
57 /* --------------------------------------------------------------------------------------------- */
58 /*** public functions ****************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
62 button_default_callback (Widget
*w
, Widget
*sender
, widget_msg_t msg
, int parm
, void *data
)
64 WButton
*b
= BUTTON (w
);
66 WDialog
*h
= DIALOG (g
);
73 * Don't let the default button steal Enter from the current
74 * button. This is a workaround for the flawed event model
75 * when hotkeys are sent to all widgets before the key is
76 * handled by the current widget.
78 if (parm
== '\n' && WIDGET (g
->current
->data
) == w
)
80 send_message (w
, sender
, MSG_KEY
, ' ', data
);
84 if (parm
== '\n' && b
->flags
== DEFPUSH_BUTTON
)
86 send_message (w
, sender
, MSG_KEY
, ' ', data
);
90 if (b
->text
.hotkey
!= NULL
&& g_ascii_tolower ((gchar
) b
->text
.hotkey
[0]) == parm
)
92 send_message (w
, sender
, MSG_KEY
, ' ', data
);
95 return MSG_NOT_HANDLED
;
98 if (parm
!= ' ' && parm
!= '\n')
99 return MSG_NOT_HANDLED
;
101 h
->ret_value
= b
->action
;
102 if (b
->callback
== NULL
|| b
->callback (b
, b
->action
) != 0)
124 widget_gotoyx (w
, 0, b
->hotpos
+ off
);
131 focused
= widget_get_state (w
, WST_FOCUSED
);
133 widget_selectcolor (w
, focused
, FALSE
);
134 widget_gotoyx (w
, 0, 0);
139 tty_print_string ("[< ");
142 tty_print_string ("[ ");
145 tty_print_string ("[");
152 hotkey_draw (w
, b
->text
, focused
);
157 tty_print_string (" >]");
160 tty_print_string (" ]");
163 tty_print_string ("]");
173 hotkey_free (b
->text
);
177 return widget_default_callback (w
, sender
, msg
, parm
, data
);
181 /* --------------------------------------------------------------------------------------------- */
184 button_mouse_default_callback (Widget
*w
, mouse_msg_t msg
, mouse_event_t
*event
)
194 case MSG_MOUSE_CLICK
:
195 send_message (w
, NULL
, MSG_KEY
, ' ', NULL
);
196 send_message (w
->owner
, w
, MSG_POST_KEY
, ' ', NULL
);
204 /* --------------------------------------------------------------------------------------------- */
207 button_new (int y
, int x
, int action
, button_flags_t flags
, const char *text
, bcback_fn callback
)
209 WRect r
= { y
, x
, 1, 1 };
213 b
= g_new (WButton
, 1);
218 b
->text
= hotkey_new (text
);
219 r
.cols
= button_get_len (b
);
220 widget_init (w
, &r
, button_default_callback
, button_mouse_default_callback
);
221 w
->options
|= WOP_SELECTABLE
| WOP_WANT_CURSOR
| WOP_WANT_HOTKEY
;
222 b
->callback
= callback
;
223 b
->hotpos
= (b
->text
.hotkey
!= NULL
) ? str_term_width1 (b
->text
.start
) : -1;
228 /* --------------------------------------------------------------------------------------------- */
231 button_get_text (const WButton
*b
)
233 return hotkey_get_text (b
->text
);
236 /* --------------------------------------------------------------------------------------------- */
239 button_set_text (WButton
*b
, const char *text
)
241 Widget
*w
= WIDGET (b
);
244 hk
= hotkey_new (text
);
245 if (hotkey_equal (b
->text
, hk
))
251 hotkey_free (b
->text
);
253 b
->hotpos
= (b
->text
.hotkey
!= NULL
) ? str_term_width1 (b
->text
.start
) : -1;
254 w
->rect
.cols
= button_get_len (b
);
258 /* --------------------------------------------------------------------------------------------- */
261 button_get_len (const WButton
*b
)
263 int ret
= hotkey_width (b
->text
);
284 /* --------------------------------------------------------------------------------------------- */