Sort include order.
[gemrb.git] / gemrb / core / Control.cpp
blob8ff2d7bcf24c24d4a9a9da1eb326f3e0a746df2a
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "Control.h"
23 #include "win32def.h"
25 #include "ControlAnimation.h"
26 #include "Interface.h"
27 #include "ScriptEngine.h"
28 #include "Video.h"
29 #include "Window.h"
31 #include <cstdio>
32 #include <cstring>
34 Control::Control()
36 hasFocus = false;
37 Changed = true;
38 InHandler = false;
39 VarName[0] = 0;
40 Value = 0;
41 Flags = 0;
42 Tooltip = NULL;
43 Owner = NULL;
44 XPos = 0;
45 YPos = 0;
47 sb = NULL;
48 animation = NULL;
49 AnimPicture = NULL;
50 ControlType = IE_GUI_INVALID;
53 Control::~Control()
55 if (InHandler) {
56 printMessage("Control","Destroying control inside event handler, crash may occur!\n", LIGHT_RED);
58 core->DisplayTooltip( 0, 0, NULL );
59 free (Tooltip);
61 delete animation;
63 core->GetVideoDriver()->FreeSprite(AnimPicture);
66 /** Sets the Tooltip text of the current control */
67 int Control::SetTooltip(const char* string)
69 free(Tooltip);
71 if ((string == NULL) || (string[0] == 0)) {
72 Tooltip = NULL;
73 } else {
74 Tooltip = strdup (string);
76 Changed = true;
77 return 0;
80 /** Sets the tooltip to be displayed on the screen now */
81 void Control::DisplayTooltip()
83 if (Tooltip)
84 core->DisplayTooltip( Owner->XPos + XPos + Width / 2, Owner->YPos + YPos + Height / 2, this );
85 else
86 core->DisplayTooltip( 0, 0, NULL );
89 void Control::ResetEventHandler(EventHandler handler)
91 handler[0] = 0;
94 void Control::SetEventHandler(EventHandler handler, const char* funcName)
96 strncpy( handler, funcName, sizeof( EventHandler ) );
99 int Control::RunEventHandler(EventHandler handler)
101 if (InHandler) {
102 printMessage("Control","Nested event handlers are not supported!\n", YELLOW);
103 return -1;
105 if (handler[0]) {
106 Window *wnd = Owner;
107 if (!wnd) {
108 return -1;
110 unsigned short WID = wnd->WindowID;
111 unsigned short ID = (unsigned short) ControlID;
112 InHandler = true;
113 core->GetGUIScriptEngine()->RunFunction( (char*)handler );
114 if (!core->IsValidWindow(WID,wnd) ) {
115 printMessage ("Control","Owner window destructed!\n", LIGHT_RED);
116 return -1;
118 if (!wnd->IsValidControl(ID,this) ) {
119 printMessage ("Control","Control destructed!\n", LIGHT_RED);
120 return -1;
122 InHandler = false;
124 return 0;
127 /** Key Press Event */
128 void Control::OnKeyPress(unsigned char /*Key*/, unsigned short /*Mod*/)
130 //printf("OnKeyPress: CtrlID = 0x%08X, Key = %c (0x%02hX)\n", (unsigned int) ControlID, Key, Key);
133 /** Key Release Event */
134 void Control::OnKeyRelease(unsigned char /*Key*/, unsigned short /*Mod*/)
136 //printf( "OnKeyRelease: CtrlID = 0x%08X, Key = %c (0x%02hX)\n", (unsigned int) ControlID, Key, Key );
139 /** Mouse Enter Event */
140 void Control::OnMouseEnter(unsigned short /*x*/, unsigned short /*y*/)
142 // printf("OnMouseEnter: CtrlID = 0x%08X, x = %hd, y = %hd\n", (unsigned int) ControlID, x, y);
145 /** Mouse Leave Event */
146 void Control::OnMouseLeave(unsigned short /*x*/, unsigned short /*y*/)
148 // printf("OnMouseLeave: CtrlID = 0x%08X, x = %hd, y = %hd\n", (unsigned int) ControlID, x, y);
151 /** Mouse Over Event */
152 void Control::OnMouseOver(unsigned short /*x*/, unsigned short /*y*/)
154 //printf("OnMouseOver: CtrlID = 0x%08X, x = %hd, y = %hd\n", (unsigned int) ControlID, x, y);
157 /** Mouse Button Down */
158 void Control::OnMouseDown(unsigned short x, unsigned short y,
159 unsigned short Button, unsigned short Mod)
161 if (Button == GEM_MB_SCRLUP || Button == GEM_MB_SCRLDOWN) {
162 Control *ctrl = Owner->GetScrollControl();
163 if (ctrl && (ctrl!=this)) {
164 ctrl->OnMouseDown(x,y,Button,Mod);
169 /** Mouse Button Up */
170 void Control::OnMouseUp(unsigned short /*x*/, unsigned short /*y*/,
171 unsigned short /*Button*/, unsigned short /*Mod*/)
173 //printf("OnMouseUp: CtrlID = 0x%08X, x = %hd, y = %hd, Button = %d, Mos = %hd\n", (unsigned int) ControlID, x, y, Button, Mod);
176 /** Special Key Press */
177 void Control::OnSpecialKeyPress(unsigned char Key)
179 if (Key == GEM_UP || Key == GEM_DOWN) {
180 Control *ctrl = Owner->GetScrollControl();
181 if (ctrl && (ctrl!=this)) {
182 ctrl->OnSpecialKeyPress(Key);
187 /** Sets the Display Flags */
188 int Control::SetFlags(int arg_flags, int opcode)
190 if ((arg_flags >>24) != ControlType)
191 return -2;
192 switch (opcode) {
193 case BM_SET:
194 Flags = arg_flags; //set
195 break;
196 case BM_AND:
197 Flags &= arg_flags;
198 break;
199 case BM_OR:
200 Flags |= arg_flags; //turn on
201 break;
202 case BM_XOR:
203 Flags ^= arg_flags;
204 break;
205 case BM_NAND:
206 Flags &= ~arg_flags;//turn off
207 break;
208 default:
209 return -1;
211 Changed = true;
212 Owner->Invalidate();
213 return 0;
216 void Control::SetAnimPicture(Sprite2D* newpic)
218 core->GetVideoDriver()->FreeSprite(AnimPicture);
219 AnimPicture = newpic;
220 //apparently this is needed too, so the artifacts are not visible
221 if (Owner->Visible==WINDOW_VISIBLE) {
222 Changed = true;
223 Owner->InvalidateForControl(this);
227 /** Sets the Scroll Bar Pointer. If 'ptr' is NULL no Scroll Bar will be linked
228 to this Control. */
229 int Control::SetScrollBar(Control* ptr)
231 if (ptr && (ptr->ControlType!=IE_GUI_SCROLLBAR)) {
232 ptr = NULL;
233 printMessage("Control","Attached control is not a ScrollBar!\n",YELLOW);
234 return -1;
236 sb = ptr;
237 Changed = true;
238 if (ptr) return 1;
239 return 0;