2 * GNT - The GLib Ncurses Toolkit
4 * GNT is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include "gntbutton.h"
35 static GntWidgetClass
*parent_class
= NULL
;
36 static gboolean small_button
= FALSE
;
39 gnt_button_draw(GntWidget
*widget
)
41 GntButton
*button
= GNT_BUTTON(widget
);
45 if ((focus
= gnt_widget_has_focus(widget
)))
46 type
= GNT_COLOR_HIGHLIGHT
;
48 type
= GNT_COLOR_NORMAL
;
50 wbkgdset(widget
->window
, '\0' | gnt_color_pair(type
));
51 mvwaddstr(widget
->window
, (small_button
) ? 0 : 1, 2, button
->priv
->text
);
53 type
= GNT_COLOR_HIGHLIGHT
;
54 mvwchgat(widget
->window
, 0, 0, widget
->priv
.width
, focus
? A_BOLD
: A_REVERSE
, type
, NULL
);
61 gnt_button_size_request(GntWidget
*widget
)
63 GntButton
*button
= GNT_BUTTON(widget
);
64 gnt_util_get_text_bound(button
->priv
->text
,
65 &widget
->priv
.width
, &widget
->priv
.height
);
66 widget
->priv
.width
+= 4;
67 if (!GNT_WIDGET_IS_FLAG_SET(widget
, GNT_WIDGET_NO_BORDER
))
68 widget
->priv
.height
+= 2;
72 gnt_button_map(GntWidget
*widget
)
74 if (widget
->priv
.width
== 0 || widget
->priv
.height
== 0)
75 gnt_widget_size_request(widget
);
80 gnt_button_key_pressed(GntWidget
*widget
, const char *key
)
82 if (strcmp(key
, GNT_KEY_ENTER
) == 0)
84 gnt_widget_activate(widget
);
91 gnt_button_clicked(GntWidget
*widget
, GntMouseEvent event
, int x
, int y
)
93 if (event
== GNT_LEFT_MOUSE_DOWN
) {
94 gnt_widget_activate(widget
);
101 gnt_button_destroy(GntWidget
*widget
)
103 GntButton
*button
= GNT_BUTTON(widget
);
104 g_free(button
->priv
->text
);
105 g_free(button
->priv
);
109 gnt_button_class_init(GntWidgetClass
*klass
)
113 parent_class
= GNT_WIDGET_CLASS(klass
);
114 parent_class
->draw
= gnt_button_draw
;
115 parent_class
->map
= gnt_button_map
;
116 parent_class
->size_request
= gnt_button_size_request
;
117 parent_class
->key_pressed
= gnt_button_key_pressed
;
118 parent_class
->clicked
= gnt_button_clicked
;
119 parent_class
->destroy
= gnt_button_destroy
;
121 style
= gnt_style_get_from_name(NULL
, "small-button");
122 small_button
= gnt_style_parse_bool(style
);
128 gnt_button_init(GTypeInstance
*instance
, gpointer
class)
130 GntWidget
*widget
= GNT_WIDGET(instance
);
131 GntButton
*button
= GNT_BUTTON(instance
);
132 button
->priv
= g_new0(GntButtonPriv
, 1);
134 widget
->priv
.minw
= 4;
135 widget
->priv
.minh
= small_button
? 1 : 3;
137 GNT_WIDGET_SET_FLAGS(widget
, GNT_WIDGET_NO_BORDER
| GNT_WIDGET_NO_SHADOW
);
138 GNT_WIDGET_UNSET_FLAGS(widget
, GNT_WIDGET_GROW_X
| GNT_WIDGET_GROW_Y
);
142 /******************************************************************************
144 *****************************************************************************/
146 gnt_button_get_gtype(void) {
147 static GType type
= 0;
150 static const GTypeInfo info
= {
151 sizeof(GntButtonClass
),
152 NULL
, /* base_init */
153 NULL
, /* base_finalize */
154 (GClassInitFunc
)gnt_button_class_init
,
155 NULL
, /* class_finalize */
156 NULL
, /* class_data */
159 gnt_button_init
, /* instance_init */
160 NULL
/* value_table */
163 type
= g_type_register_static(GNT_TYPE_WIDGET
,
171 GntWidget
*gnt_button_new(const char *text
)
173 GntWidget
*widget
= g_object_new(GNT_TYPE_BUTTON
, NULL
);
174 GntButton
*button
= GNT_BUTTON(widget
);
176 button
->priv
->text
= gnt_util_onscreen_fit_string(text
, -1);
177 gnt_widget_set_take_focus(widget
, TRUE
);