15818
|
1 #include <string.h>
|
|
2
|
|
3 #include "gntbutton.h"
|
|
4 #include "gntutils.h"
|
|
5
|
|
6 enum
|
|
7 {
|
|
8 SIGS = 1,
|
|
9 };
|
|
10
|
|
11 static GntWidgetClass *parent_class = NULL;
|
|
12
|
|
13 static void
|
|
14 gnt_button_draw(GntWidget *widget)
|
|
15 {
|
|
16 GntButton *button = GNT_BUTTON(widget);
|
|
17 GntColorType type;
|
|
18
|
|
19 if (gnt_widget_has_focus(widget))
|
|
20 type = GNT_COLOR_HIGHLIGHT;
|
|
21 else
|
|
22 type = GNT_COLOR_NORMAL;
|
|
23
|
|
24 wbkgdset(widget->window, '\0' | COLOR_PAIR(type));
|
|
25 mvwaddstr(widget->window, 1, 2, button->priv->text);
|
|
26
|
|
27 GNTDEBUG;
|
|
28 }
|
|
29
|
|
30 static void
|
|
31 gnt_button_size_request(GntWidget *widget)
|
|
32 {
|
|
33 GntButton *button = GNT_BUTTON(widget);
|
|
34 gnt_util_get_text_bound(button->priv->text,
|
|
35 &widget->priv.width, &widget->priv.height);
|
|
36 widget->priv.width += 4;
|
|
37 if (!GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER))
|
|
38 widget->priv.height += 2;
|
|
39 }
|
|
40
|
|
41 static void
|
|
42 gnt_button_map(GntWidget *widget)
|
|
43 {
|
|
44 if (widget->priv.width == 0 || widget->priv.height == 0)
|
|
45 gnt_widget_size_request(widget);
|
|
46 GNTDEBUG;
|
|
47 }
|
|
48
|
|
49 static gboolean
|
|
50 gnt_button_key_pressed(GntWidget *widget, const char *key)
|
|
51 {
|
|
52 if (strcmp(key, GNT_KEY_ENTER) == 0)
|
|
53 {
|
|
54 gnt_widget_activate(widget);
|
|
55 return TRUE;
|
|
56 }
|
|
57 return FALSE;
|
|
58 }
|
|
59
|
|
60 static gboolean
|
|
61 gnt_button_clicked(GntWidget *widget, GntMouseEvent event, int x, int y)
|
|
62 {
|
|
63 if (event == GNT_LEFT_MOUSE_DOWN) {
|
|
64 gnt_widget_activate(widget);
|
|
65 return TRUE;
|
|
66 }
|
|
67 return FALSE;
|
|
68 }
|
|
69
|
|
70 static void
|
|
71 gnt_button_class_init(GntWidgetClass *klass)
|
|
72 {
|
|
73 parent_class = GNT_WIDGET_CLASS(klass);
|
|
74 parent_class->draw = gnt_button_draw;
|
|
75 parent_class->map = gnt_button_map;
|
|
76 parent_class->size_request = gnt_button_size_request;
|
|
77 parent_class->key_pressed = gnt_button_key_pressed;
|
|
78 parent_class->clicked = gnt_button_clicked;
|
|
79
|
|
80 GNTDEBUG;
|
|
81 }
|
|
82
|
|
83 static void
|
|
84 gnt_button_init(GTypeInstance *instance, gpointer class)
|
|
85 {
|
|
86 GntWidget *widget = GNT_WIDGET(instance);
|
|
87 GntButton *button = GNT_BUTTON(instance);
|
|
88 button->priv = g_new0(GntButtonPriv, 1);
|
|
89
|
|
90 widget->priv.minw = 4;
|
|
91 widget->priv.minh = 3;
|
|
92 GNTDEBUG;
|
|
93 }
|
|
94
|
|
95 /******************************************************************************
|
|
96 * GntButton API
|
|
97 *****************************************************************************/
|
|
98 GType
|
|
99 gnt_button_get_gtype(void) {
|
|
100 static GType type = 0;
|
|
101
|
|
102 if(type == 0) {
|
|
103 static const GTypeInfo info = {
|
|
104 sizeof(GntButtonClass),
|
|
105 NULL, /* base_init */
|
|
106 NULL, /* base_finalize */
|
|
107 (GClassInitFunc)gnt_button_class_init,
|
|
108 NULL, /* class_finalize */
|
|
109 NULL, /* class_data */
|
|
110 sizeof(GntButton),
|
|
111 0, /* n_preallocs */
|
|
112 gnt_button_init, /* instance_init */
|
|
113 NULL /* value_table */
|
|
114 };
|
|
115
|
|
116 type = g_type_register_static(GNT_TYPE_WIDGET,
|
|
117 "GntButton",
|
|
118 &info, 0);
|
|
119 }
|
|
120
|
|
121 return type;
|
|
122 }
|
|
123
|
|
124 GntWidget *gnt_button_new(const char *text)
|
|
125 {
|
|
126 GntWidget *widget = g_object_new(GNT_TYPE_BUTTON, NULL);
|
|
127 GntButton *button = GNT_BUTTON(widget);
|
|
128
|
|
129 button->priv->text = gnt_util_onscreen_fit_string(text, -1);
|
|
130 gnt_widget_set_take_focus(widget, TRUE);
|
|
131
|
|
132 return widget;
|
|
133 }
|
|
134
|