1 //
2 // Copyright(C) 2005-2014 Simon Howard
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "doomkeys.h"
19
20 #include "txt_button.h"
21 #include "txt_gui.h"
22 #include "txt_io.h"
23 #include "txt_main.h"
24 #include "txt_utf8.h"
25 #include "txt_window.h"
26
TXT_ButtonSizeCalc(TXT_UNCAST_ARG (button))27 static void TXT_ButtonSizeCalc(TXT_UNCAST_ARG(button))
28 {
29 TXT_CAST_ARG(txt_button_t, button);
30
31 button->widget.w = TXT_UTF8_Strlen(button->label);
32 button->widget.h = 1;
33 }
34
TXT_ButtonDrawer(TXT_UNCAST_ARG (button))35 static void TXT_ButtonDrawer(TXT_UNCAST_ARG(button))
36 {
37 TXT_CAST_ARG(txt_button_t, button);
38 int i;
39 int w;
40
41 w = button->widget.w;
42
43 TXT_SetWidgetBG(button);
44
45 TXT_DrawString(button->label);
46
47 for (i = TXT_UTF8_Strlen(button->label); i < w; ++i)
48 {
49 TXT_DrawString(" ");
50 }
51 }
52
TXT_ButtonDestructor(TXT_UNCAST_ARG (button))53 static void TXT_ButtonDestructor(TXT_UNCAST_ARG(button))
54 {
55 TXT_CAST_ARG(txt_button_t, button);
56
57 free(button->label);
58 }
59
TXT_ButtonKeyPress(TXT_UNCAST_ARG (button),int key)60 static int TXT_ButtonKeyPress(TXT_UNCAST_ARG(button), int key)
61 {
62 TXT_CAST_ARG(txt_button_t, button);
63
64 if (key == KEY_ENTER)
65 {
66 TXT_EmitSignal(button, "pressed");
67 return 1;
68 }
69
70 return 0;
71 }
72
TXT_ButtonMousePress(TXT_UNCAST_ARG (button),int x,int y,int b)73 static void TXT_ButtonMousePress(TXT_UNCAST_ARG(button), int x, int y, int b)
74 {
75 TXT_CAST_ARG(txt_button_t, button);
76
77 if (b == TXT_MOUSE_LEFT)
78 {
79 // Equivalent to pressing enter
80
81 TXT_ButtonKeyPress(button, KEY_ENTER);
82 }
83 }
84
85 txt_widget_class_t txt_button_class =
86 {
87 TXT_AlwaysSelectable,
88 TXT_ButtonSizeCalc,
89 TXT_ButtonDrawer,
90 TXT_ButtonKeyPress,
91 TXT_ButtonDestructor,
92 TXT_ButtonMousePress,
93 NULL,
94 };
95
TXT_SetButtonLabel(txt_button_t * button,const char * label)96 void TXT_SetButtonLabel(txt_button_t *button, const char *label)
97 {
98 free(button->label);
99 button->label = strdup(label);
100 }
101
TXT_NewButton(const char * label)102 txt_button_t *TXT_NewButton(const char *label)
103 {
104 txt_button_t *button;
105
106 button = malloc(sizeof(txt_button_t));
107
108 TXT_InitWidget(button, &txt_button_class);
109 button->label = strdup(label);
110
111 return button;
112 }
113
114 // Button with a callback set automatically
115
TXT_NewButton2(const char * label,TxtWidgetSignalFunc func,void * user_data)116 txt_button_t *TXT_NewButton2(const char *label, TxtWidgetSignalFunc func,
117 void *user_data)
118 {
119 txt_button_t *button;
120
121 button = TXT_NewButton(label);
122
123 TXT_SignalConnect(button, "pressed", func, user_data);
124
125 return button;
126 }
127
128