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
28 #include "txt_mouseinput.h"
31 #include "txt_label.h"
32 #include "txt_window.h"
34 #define MOUSE_INPUT_WIDTH 8
36 static int MousePressCallback(txt_window_t
*window
,
38 TXT_UNCAST_ARG(mouse_input
))
40 TXT_CAST_ARG(txt_mouse_input_t
, mouse_input
);
42 // Got the mouse press. Save to the variable and close the window.
44 *mouse_input
->variable
= b
- TXT_MOUSE_BASE
;
45 TXT_EmitSignal(mouse_input
, "set");
46 TXT_CloseWindow(window
);
51 static void OpenPromptWindow(txt_mouse_input_t
*mouse_input
)
56 window
= TXT_NewWindow(NULL
);
57 TXT_SetWindowAction(window
, TXT_HORIZ_LEFT
, NULL
);
58 TXT_SetWindowAction(window
, TXT_HORIZ_CENTER
,
59 TXT_NewWindowAbortAction(window
));
60 TXT_SetWindowAction(window
, TXT_HORIZ_RIGHT
, NULL
);
62 label
= TXT_NewLabel("Press the new mouse button...");
64 TXT_AddWidget(window
, label
);
65 TXT_SetWidgetAlign(label
, TXT_HORIZ_CENTER
);
67 TXT_SetMouseListener(window
, MousePressCallback
, mouse_input
);
70 static void TXT_MouseInputSizeCalc(TXT_UNCAST_ARG(mouse_input
))
72 TXT_CAST_ARG(txt_mouse_input_t
, mouse_input
);
74 // All mouseinputs are the same size.
76 mouse_input
->widget
.w
= MOUSE_INPUT_WIDTH
;
77 mouse_input
->widget
.h
= 1;
80 static void GetMouseButtonDescription(int button
, char *buf
)
94 sprintf(buf
, "BUTTON #%i", button
);
99 static void TXT_MouseInputDrawer(TXT_UNCAST_ARG(mouse_input
), int selected
)
101 TXT_CAST_ARG(txt_mouse_input_t
, mouse_input
);
105 if (*mouse_input
->variable
< 0)
107 strcpy(buf
, "(none)");
111 GetMouseButtonDescription(*mouse_input
->variable
, buf
);
116 TXT_BGColor(TXT_COLOR_GREY
, 0);
120 TXT_BGColor(TXT_COLOR_BLUE
, 0);
123 TXT_FGColor(TXT_COLOR_BRIGHT_WHITE
);
127 for (i
=strlen(buf
); i
<MOUSE_INPUT_WIDTH
; ++i
)
133 static void TXT_MouseInputDestructor(TXT_UNCAST_ARG(mouse_input
))
137 static int TXT_MouseInputKeyPress(TXT_UNCAST_ARG(mouse_input
), int mouse
)
139 TXT_CAST_ARG(txt_mouse_input_t
, mouse_input
);
141 if (mouse
== KEY_ENTER
)
143 // Open a window to prompt for the new mouse press
145 OpenPromptWindow(mouse_input
);
153 static void TXT_MouseInputMousePress(TXT_UNCAST_ARG(widget
), int x
, int y
, int b
)
155 TXT_CAST_ARG(txt_mouse_input_t
, widget
);
157 // Clicking is like pressing enter
159 if (b
== TXT_MOUSE_LEFT
)
161 TXT_MouseInputKeyPress(widget
, KEY_ENTER
);
165 txt_widget_class_t txt_mouse_input_class
=
167 TXT_MouseInputSizeCalc
,
168 TXT_MouseInputDrawer
,
169 TXT_MouseInputKeyPress
,
170 TXT_MouseInputDestructor
,
171 TXT_MouseInputMousePress
,
175 txt_mouse_input_t
*TXT_NewMouseInput(int *variable
)
177 txt_mouse_input_t
*mouse_input
;
179 mouse_input
= malloc(sizeof(txt_mouse_input_t
));
181 TXT_InitWidget(mouse_input
, &txt_mouse_input_class
);
182 mouse_input
->variable
= variable
;