1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
4 // Copyright(C) 2006 Simon Howard
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.
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.
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
27 #include "txt_keyinput.h"
30 #include "txt_label.h"
31 #include "txt_window.h"
33 #define KEY_INPUT_WIDTH 8
35 static int KeyPressCallback(txt_window_t
*window
, int key
,
36 TXT_UNCAST_ARG(key_input
))
38 TXT_CAST_ARG(txt_key_input_t
, key_input
);
40 if (key
!= KEY_ESCAPE
)
42 // Got the key press. Save to the variable and close the window.
44 *key_input
->variable
= key
;
45 TXT_EmitSignal(key_input
, "set");
46 TXT_CloseWindow(window
);
48 // Re-enable key mappings now that we have the key
50 TXT_EnableKeyMapping(1);
60 static void ReleaseGrab(TXT_UNCAST_ARG(window
), TXT_UNCAST_ARG(unused
))
62 SDL_WM_GrabInput(SDL_GRAB_OFF
);
65 static void OpenPromptWindow(txt_key_input_t
*key_input
)
70 window
= TXT_NewWindow(NULL
);
71 TXT_SetWindowAction(window
, TXT_HORIZ_LEFT
, NULL
);
72 TXT_SetWindowAction(window
, TXT_HORIZ_CENTER
,
73 TXT_NewWindowAbortAction(window
));
74 TXT_SetWindowAction(window
, TXT_HORIZ_RIGHT
, NULL
);
76 label
= TXT_NewLabel("Press the new key...");
78 TXT_AddWidget(window
, label
);
79 TXT_SetWidgetAlign(label
, TXT_HORIZ_CENTER
);
81 TXT_SetKeyListener(window
, KeyPressCallback
, key_input
);
83 // Disable key mappings while we prompt for the key press
85 TXT_EnableKeyMapping(0);
87 // Grab input while reading the key. On Windows Mobile
88 // handheld devices, the hardware keypresses are only
89 // detected when input is grabbed.
91 SDL_WM_GrabInput(SDL_GRAB_ON
);
92 TXT_SignalConnect(window
, "closed", ReleaseGrab
, NULL
);
95 static void TXT_KeyInputSizeCalc(TXT_UNCAST_ARG(key_input
))
97 TXT_CAST_ARG(txt_key_input_t
, key_input
);
99 // All keyinputs are the same size.
101 key_input
->widget
.w
= KEY_INPUT_WIDTH
;
102 key_input
->widget
.h
= 1;
106 static void TXT_KeyInputDrawer(TXT_UNCAST_ARG(key_input
), int selected
)
108 TXT_CAST_ARG(txt_key_input_t
, key_input
);
112 if (*key_input
->variable
== 0)
114 strcpy(buf
, "(none)");
118 TXT_GetKeyDescription(*key_input
->variable
, buf
);
123 TXT_BGColor(TXT_COLOR_GREY
, 0);
127 TXT_BGColor(TXT_COLOR_BLUE
, 0);
130 TXT_FGColor(TXT_COLOR_BRIGHT_WHITE
);
134 for (i
=strlen(buf
); i
<KEY_INPUT_WIDTH
; ++i
)
140 static void TXT_KeyInputDestructor(TXT_UNCAST_ARG(key_input
))
144 static int TXT_KeyInputKeyPress(TXT_UNCAST_ARG(key_input
), int key
)
146 TXT_CAST_ARG(txt_key_input_t
, key_input
);
148 if (key
== KEY_ENTER
)
150 // Open a window to prompt for the new key press
152 OpenPromptWindow(key_input
);
160 static void TXT_KeyInputMousePress(TXT_UNCAST_ARG(widget
), int x
, int y
, int b
)
162 TXT_CAST_ARG(txt_key_input_t
, widget
);
164 // Clicking is like pressing enter
166 if (b
== TXT_MOUSE_LEFT
)
168 TXT_KeyInputKeyPress(widget
, KEY_ENTER
);
172 txt_widget_class_t txt_key_input_class
=
174 TXT_KeyInputSizeCalc
,
176 TXT_KeyInputKeyPress
,
177 TXT_KeyInputDestructor
,
178 TXT_KeyInputMousePress
,
182 txt_key_input_t
*TXT_NewKeyInput(int *variable
)
184 txt_key_input_t
*key_input
;
186 key_input
= malloc(sizeof(txt_key_input_t
));
188 TXT_InitWidget(key_input
, &txt_key_input_class
);
189 key_input
->variable
= variable
;