1 /* Widgets for the Midnight Commander
3 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
6 Authors: 1994, 1995 Radek Doulik
7 1994, 1995 Miguel de Icaza
11 2009, 2010 Andrew Borodin
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 * \brief Source: WButton widget
37 #include "lib/global.h"
39 #include "lib/tty/tty.h"
40 #include "lib/tty/mouse.h"
41 #include "lib/strutil.h"
42 #include "lib/widget.h"
44 /*** global variables ****************************************************************************/
46 /*** file scope macro definitions ****************************************************************/
48 /*** file scope type declarations ****************************************************************/
50 /*** file scope variables ************************************************************************/
52 /*** file scope functions ************************************************************************/
55 button_callback (Widget
* w
, widget_msg_t msg
, int parm
)
57 WButton
*b
= (WButton
*) w
;
60 Dlg_head
*h
= b
->widget
.owner
;
66 * Don't let the default button steal Enter from the current
67 * button. This is a workaround for the flawed event model
68 * when hotkeys are sent to all widgets before the key is
69 * handled by the current widget.
71 if (parm
== '\n' && (Widget
*) h
->current
->data
== &b
->widget
)
73 button_callback (w
, WIDGET_KEY
, ' ');
77 if (parm
== '\n' && b
->flags
== DEFPUSH_BUTTON
)
79 button_callback (w
, WIDGET_KEY
, ' ');
83 if (b
->text
.hotkey
!= NULL
&&
84 g_ascii_tolower ((gchar
) b
->text
.hotkey
[0]) == parm
)
86 button_callback (w
, WIDGET_KEY
, ' ');
89 return MSG_NOT_HANDLED
;
92 if (parm
!= ' ' && parm
!= '\n')
93 return MSG_NOT_HANDLED
;
95 if (b
->callback
!= NULL
)
96 stop
= b
->callback (b
, b
->action
);
97 if (b
->callback
== NULL
|| stop
!= 0)
99 h
->ret_value
= b
->action
;
121 widget_move (&b
->widget
, 0, b
->hotpos
+ off
);
127 if (msg
== WIDGET_UNFOCUS
)
129 else if (msg
== WIDGET_FOCUS
)
132 widget_selectcolor (w
, b
->selected
, FALSE
);
133 widget_move (w
, 0, 0);
138 tty_print_string ("[< ");
141 tty_print_string ("[ ");
144 tty_print_string ("[");
151 hotkey_draw (w
, b
->text
, b
->selected
);
156 tty_print_string (" >]");
159 tty_print_string (" ]");
162 tty_print_string ("]");
170 release_hotkey (b
->text
);
174 return default_proc (msg
, parm
);
178 /* --------------------------------------------------------------------------------------------- */
181 button_event (Gpm_Event
* event
, void *data
)
185 if ((event
->type
& (GPM_DOWN
| GPM_UP
)) != 0)
187 Dlg_head
*h
= b
->widget
.owner
;
189 dlg_select_widget (b
);
190 if ((event
->type
& GPM_UP
) != 0)
192 button_callback (&b
->widget
, WIDGET_KEY
, ' ');
193 h
->callback (h
, &b
->widget
, DLG_POST_KEY
, ' ', NULL
);
200 /* --------------------------------------------------------------------------------------------- */
201 /*** public functions ****************************************************************************/
202 /* --------------------------------------------------------------------------------------------- */
205 button_new (int y
, int x
, int action
, button_flags_t flags
, const char *text
, bcback_fn callback
)
209 b
= g_new (WButton
, 1);
212 b
->text
= parse_hotkey (text
);
214 init_widget (&b
->widget
, y
, x
, 1, button_get_len (b
), button_callback
, button_event
);
217 b
->callback
= callback
;
218 widget_want_hotkey (b
->widget
, 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 release_hotkey (b
->text
);
240 b
->text
= parse_hotkey (text
);
241 b
->widget
.cols
= button_get_len (b
);
242 dlg_redraw (b
->widget
.owner
);
245 /* --------------------------------------------------------------------------------------------- */
248 button_get_len (const WButton
* b
)
250 int ret
= hotkey_width (b
->text
);
271 /* --------------------------------------------------------------------------------------------- */