cdb9fd1668d88284e48bd36f0d5e427957136e87
[midnight-commander.git] / lib / widget / check.c
blobcdb9fd1668d88284e48bd36f0d5e427957136e87
1 /*
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.
8 Authors:
9 Radek Doulik, 1994, 1995
10 Miguel de Icaza, 1994, 1995
11 Jakub Jelinek, 1995
12 Andrej Borsenkow, 1996
13 Norbert Warmuth, 1997
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/>.
32 /** \file check.c
33 * \brief Source: WCheck widget (checkbutton)
36 #include <config.h>
38 #include <stdlib.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/mouse.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 ************************************************************************/
56 static cb_ret_t
57 check_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
59 WCheck *c = (WCheck *) w;
61 switch (msg)
63 case MSG_HOTKEY:
64 if (c->text.hotkey != NULL)
66 if (g_ascii_tolower ((gchar) c->text.hotkey[0]) == parm)
68 /* make action */
69 send_message (w, sender, MSG_KEY, ' ', data);
70 return MSG_HANDLED;
73 return MSG_NOT_HANDLED;
75 case MSG_KEY:
76 if (parm != ' ')
77 return MSG_NOT_HANDLED;
78 c->state ^= C_BOOL;
79 c->state ^= C_CHANGE;
80 send_message (WIDGET (w)->owner, w, MSG_ACTION, 0, NULL);
81 send_message (w, sender, MSG_FOCUS, ' ', data);
82 return MSG_HANDLED;
84 case MSG_CURSOR:
85 widget_move (c, 0, 1);
86 return MSG_HANDLED;
88 case MSG_FOCUS:
89 case MSG_UNFOCUS:
90 case MSG_DRAW:
91 widget_selectcolor (w, msg == MSG_FOCUS, FALSE);
92 widget_move (c, 0, 0);
93 tty_print_string ((c->state & C_BOOL) ? "[x] " : "[ ] ");
94 hotkey_draw (w, c->text, msg == MSG_FOCUS);
95 return MSG_HANDLED;
97 case MSG_DESTROY:
98 release_hotkey (c->text);
99 return MSG_HANDLED;
101 default:
102 return widget_default_callback (w, sender, msg, parm, data);
106 /* --------------------------------------------------------------------------------------------- */
108 static int
109 check_event (Gpm_Event * event, void *data)
111 Widget *w = WIDGET (data);
113 if (!mouse_global_in_widget (event, w))
114 return MOU_UNHANDLED;
116 if ((event->type & (GPM_DOWN | GPM_UP)) != 0)
118 dlg_select_widget (w);
119 if ((event->type & GPM_UP) != 0)
121 send_message (w, NULL, MSG_KEY, ' ', NULL);
122 send_message (w, NULL, MSG_FOCUS, 0, NULL);
123 send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
127 return MOU_NORMAL;
130 /* --------------------------------------------------------------------------------------------- */
131 /*** public functions ****************************************************************************/
132 /* --------------------------------------------------------------------------------------------- */
134 WCheck *
135 check_new (int y, int x, int state, const char *text)
137 WCheck *c;
138 Widget *w;
140 c = g_new (WCheck, 1);
141 w = WIDGET (c);
142 c->text = parse_hotkey (text);
143 init_widget (w, y, x, 1, 4 + hotkey_width (c->text), check_callback, check_event);
144 /* 4 is width of "[X] " */
145 c->state = state ? C_BOOL : 0;
146 widget_want_hotkey (w, TRUE);
148 return c;
151 /* --------------------------------------------------------------------------------------------- */