1 // Emacs style mode select   -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright(C) 2006 Simon Howard
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 // 02111-1307, USA.
20 //
21 
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "doomkeys.h"
26 
27 #include "txt_window_action.h"
28 #include "txt_gui.h"
29 #include "txt_io.h"
30 #include "txt_main.h"
31 #include "txt_window.h"
32 
TXT_WindowActionSizeCalc(TXT_UNCAST_ARG (action))33 static void TXT_WindowActionSizeCalc(TXT_UNCAST_ARG(action))
34 {
35     TXT_CAST_ARG(txt_window_action_t, action);
36     char buf[10];
37 
38     TXT_GetKeyDescription(action->key, buf);
39 
40     // Width is label length, plus key description length, plus '='
41     // and two surrounding spaces.
42 
43     action->widget.w = strlen(action->label) + strlen(buf) + 3;
44     action->widget.h = 1;
45 }
46 
TXT_WindowActionDrawer(TXT_UNCAST_ARG (action))47 static void TXT_WindowActionDrawer(TXT_UNCAST_ARG(action))
48 {
49     TXT_CAST_ARG(txt_window_action_t, action);
50     char buf[10];
51 
52     TXT_GetKeyDescription(action->key, buf);
53 
54     if (TXT_HoveringOverWidget(action))
55     {
56         TXT_BGColor(TXT_COLOR_BLACK, 0);
57     }
58 
59     TXT_DrawString(" ");
60     TXT_FGColor(TXT_COLOR_BRIGHT_GREEN);
61     TXT_DrawString(buf);
62     TXT_FGColor(TXT_COLOR_BRIGHT_CYAN);
63     TXT_DrawString("=");
64 
65     TXT_FGColor(TXT_COLOR_BRIGHT_WHITE);
66     TXT_DrawString(action->label);
67     TXT_DrawString(" ");
68 }
69 
TXT_WindowActionDestructor(TXT_UNCAST_ARG (action))70 static void TXT_WindowActionDestructor(TXT_UNCAST_ARG(action))
71 {
72     TXT_CAST_ARG(txt_window_action_t, action);
73 
74     free(action->label);
75 }
76 
TXT_WindowActionKeyPress(TXT_UNCAST_ARG (action),int key)77 static int TXT_WindowActionKeyPress(TXT_UNCAST_ARG(action), int key)
78 {
79     TXT_CAST_ARG(txt_window_action_t, action);
80 
81     if (tolower(key) == tolower(action->key))
82     {
83         TXT_EmitSignal(action, "pressed");
84         return 1;
85     }
86 
87     return 0;
88 }
89 
TXT_WindowActionMousePress(TXT_UNCAST_ARG (action),int x,int y,int b)90 static void TXT_WindowActionMousePress(TXT_UNCAST_ARG(action),
91                                        int x, int y, int b)
92 {
93     TXT_CAST_ARG(txt_window_action_t, action);
94 
95     // Simulate a press of the key
96 
97     if (b == TXT_MOUSE_LEFT)
98     {
99         TXT_WindowActionKeyPress(action, action->key);
100     }
101 }
102 
103 txt_widget_class_t txt_window_action_class =
104 {
105     TXT_AlwaysSelectable,
106     TXT_WindowActionSizeCalc,
107     TXT_WindowActionDrawer,
108     TXT_WindowActionKeyPress,
109     TXT_WindowActionDestructor,
110     TXT_WindowActionMousePress,
111     NULL,
112 };
113 
TXT_NewWindowAction(int key,const char * label)114 txt_window_action_t *TXT_NewWindowAction(int key, const char *label)
115 {
116     txt_window_action_t *action;
117 
118     action = malloc(sizeof(txt_window_action_t));
119 
120     TXT_InitWidget(action, &txt_window_action_class);
121     action->key = key;
122     action->label = strdup(label);
123 
124     return action;
125 }
126 
WindowCloseCallback(TXT_UNCAST_ARG (widget),TXT_UNCAST_ARG (window))127 static void WindowCloseCallback(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(window))
128 {
129     TXT_CAST_ARG(txt_window_t, window);
130 
131     TXT_CloseWindow(window);
132 }
133 
WindowSelectCallback(TXT_UNCAST_ARG (widget),TXT_UNCAST_ARG (window))134 static void WindowSelectCallback(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(window))
135 {
136     TXT_CAST_ARG(txt_window_t, window);
137 
138     TXT_WidgetKeyPress(window, KEY_ENTER);
139 }
140 
141 // An action with the name "close" the closes the window
142 
TXT_NewWindowEscapeAction(txt_window_t * window)143 txt_window_action_t *TXT_NewWindowEscapeAction(txt_window_t *window)
144 {
145     txt_window_action_t *action;
146 
147     action = TXT_NewWindowAction(KEY_ESCAPE, "Close");
148     TXT_SignalConnect(action, "pressed", WindowCloseCallback, window);
149 
150     return action;
151 }
152 
153 // Exactly the same as the above, but the button is named "abort"
154 
TXT_NewWindowAbortAction(txt_window_t * window)155 txt_window_action_t *TXT_NewWindowAbortAction(txt_window_t *window)
156 {
157     txt_window_action_t *action;
158 
159     action = TXT_NewWindowAction(KEY_ESCAPE, "Abort");
160     TXT_SignalConnect(action, "pressed", WindowCloseCallback, window);
161 
162     return action;
163 }
164 
TXT_NewWindowSelectAction(txt_window_t * window)165 txt_window_action_t *TXT_NewWindowSelectAction(txt_window_t *window)
166 {
167     txt_window_action_t *action;
168 
169     action = TXT_NewWindowAction(KEY_ENTER, "Select");
170     TXT_SignalConnect(action, "pressed", WindowSelectCallback, window);
171 
172     return action;
173 }
174 
175