1 /***************************************************************************
2                          window.h  -  Window manager
3                              -------------------
4     begin                : Thu Aug 28 2003
5     copyright            : (C) 2003 by Gabor Torok
6     email                : cctorok@yahoo.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef WINDOW_H
19 #define WINDOW_H
20 #pragma once
21 
22 #include "gui.h"
23 #include "widget.h"
24 #include "button.h"
25 #include "label.h"
26 #include "checkbox.h"
27 #include "textfield.h"
28 #include "guitheme.h"
29 #include <vector>
30 
31 /**
32   *@author Gabor Torok
33   */
34 
35 class Widget;
36 class Button;
37 class Label;
38 class Checkbox;
39 class TextField;
40 class EventHandler;
41 class RawEventHandler;
42 
43 #define TITLE_HEIGHT 21
44 
45 /// Looks whether a window is closing.
46 class WindowListener {
47 public:
WindowListener()48 	WindowListener() {
49 	}
50 
~WindowListener()51 	virtual ~WindowListener() {
52 	}
53 
54 	virtual void windowClosing() = 0;
55 };
56 
57 /// A GUI window.
58 class Window : public Widget {
59 private:
60 	// the image is 510x270
61 	static const int TILE_W = 510 / 2;
62 	static const int TILE_H = 270 / 2;
63 
64 	static const int MAX_WINDOW = 1000;
65 	static const int MAX_WIDGET = 1000;
66 
67 	char title[255];
68 	GLuint texture, texture2;
69 	ScourgeGui *scourgeGui;
70 	Widget *widget[MAX_WIDGET];
71 	int tileWidth, tileHeight;
72 	int widgetCount;
73 	bool dragging;
74 	int dragX, dragY;
75 	int openHeight, currentY;
76 	GLint lastTick;
77 	int z;
78 	bool modal;
79 	int type;
80 	bool locked;
81 	GuiTheme *theme;
82 	bool opening;
83 	int gutter;
84 	RawEventHandler *rawEventHandler;
85 
86 	static Window *window[];
87 	static int windowCount;
88 
89 	static Window *message_dialog;
90 	static Label *message_label;
91 	static Window *currentWin;
92 
93 	Widget *lastWidget;
94 	int animation;
95 
96 	static Window *mouseLockWindow;
97 	static Widget *mouseLockWidget;
98 
99 	std::vector<WindowListener*> listeners;
100 	Widget *escapeHandler;
101 
102 public:
103 
104 	enum {
105 		DEFAULT_ANIMATION = 0,
106 		SLIDE_UP
107 	};
108 
109 	static const char ROLL_OVER_SOUND[80];
110 	static const char ACTION_SOUND[80];
111 	static const char DROP_SUCCESS[80];
112 	static const char DROP_FAILED[80];
113 
114 	static bool windowWasClosed;
115 
116 	Button *closeButton;
117 
118 	enum {
119 		BASIC_WINDOW = 0,
120 		SIMPLE_WINDOW,
121 		INVISIBLE_WINDOW
122 	};
123 
124 	static const int SCREEN_GUTTER = 0;
125 
126 	Window( ScourgeGui *scourgeGui, int x, int y, int w, int h, char const* title = NULL,
127 		Texture const& texture = Texture::none(), bool hasCloseButton = true, int type = BASIC_WINDOW,
128 		Texture const& texture2 = Texture::none() );
129 
130 	Window( ScourgeGui *scourgeGui, int x, int y, int w, int h, char const* title = NULL,
131 	        bool hasCloseButton = true, int type = BASIC_WINDOW, const char *themeName = NULL );
132 
133 	~Window();
134 
135 	virtual void resize( int w, int h );
136 
setEscapeHandler(Widget * widget)137 	inline void setEscapeHandler( Widget *widget ) { this->escapeHandler = widget; }
getEscapeHandler()138 	inline Widget *getEscapeHandler() { return this->escapeHandler; }
139 	void registerEventHandler( EventHandler *eventHandler );
140 	void unregisterEventHandler();
setRawEventHandler(RawEventHandler * rawEventHandler)141 	inline void setRawEventHandler( RawEventHandler *rawEventHandler ) {
142 		this->rawEventHandler = rawEventHandler;
143 	}
getRawEventHandler()144 	inline RawEventHandler *getRawEventHandler() {
145 		return rawEventHandler;
146 	}
147 
148 
addWindowListener(WindowListener * listener)149 	void addWindowListener( WindowListener *listener ) {
150 		listeners.push_back( listener );
151 	}
152 
153 	void setMouseLock( Widget *widget );
154 
setAnimation(int a)155 	inline void setAnimation( int a ) {
156 		animation = a;
157 	}
getAnimation()158 	inline int getAnimation() {
159 		return animation;
160 	}
161 
getTheme()162 	inline GuiTheme *getTheme() {
163 		return theme;
164 	}
165 
setTitle(char const * s)166 	inline void setTitle( char const* s ) {
167 		strncpy( title, ( s ? s : "" ), 255 ); title[254] = '\0';
168 	}
169 
getTitle()170   inline char *getTitle() { return title; }
171 
isOpening()172 	inline bool isOpening() {
173 		return ( opening );
174 	}
175 
setBackgroundTileWidth(int n)176 	inline void setBackgroundTileWidth( int n ) {
177 		tileWidth = n;
178 	}
setBackgroundTileHeight(int n)179 	inline void setBackgroundTileHeight( int n ) {
180 		tileHeight = n;
181 	}
182 
setZ(int z)183 	inline void setZ( int z ) {
184 		this->z = z;
185 	}
getZ()186 	inline int getZ() {
187 		return z;
188 	}
189 
setModal(bool b)190 	inline void setModal( bool b ) {
191 		modal = b;
192 	}
isModal()193 	inline bool isModal() {
194 		return modal;
195 	}
196 
197 	void setVisible( bool b, bool animate = true );
getScourgeGui()198 	inline ScourgeGui *getScourgeGui() {
199 		return scourgeGui;
200 	}
201 	void toTop();
202 	void toBottom();
203 
setLocked(bool locked)204 	inline void setLocked( bool locked ) {
205 		this->locked = locked; if ( locked ) toBottom();
206 	}
isLocked()207 	inline bool isLocked() {
208 		return locked;
209 	}
210 
211 	// crop view to window area. Don't forget to call glDisable( GL_SCISSOR_TEST ) after!
212 	void scissorToWindow( bool insideOnly = true );
213 
214 	// widget managment functions
215 	Button    * createButton( int x1, int y1, int x2, int y2, char *label, bool toggle = false, Texture const& texture = Texture::none() );
216 	Label     * createLabel( int x1, int x2, char const* label, int color = Constants::DEFAULT_COLOR );
217 	Checkbox  * createCheckbox( int x1, int y1, int x2, int y2, char *label );
218 	TextField * createTextField( int x, int y, int numChars );
219 	void addWidget( Widget *widget );
220 	void removeWidget( Widget *widget );
221 	Widget *handleWindowEvent( SDL_Event *event, int x, int y );
222 	void setFocus( Widget *w );
223 	void nextFocus();
224 	void prevFocus();
225 
226 	// from Widget
227 	void drawWidget( Widget *parent );
228 	bool handleEvent( Widget *parent, SDL_Event *event, int x, int y );
229 	bool isInside( int x, int y );
230 
231 
232 	// window management
233 	static void drawVisibleWindows();
234 	static void addWindow( Window *win );
235 	static void removeWindow( Window *win );
236 	static Widget *delegateEvent( SDL_Event *event, int x, int y, Window **selectedWindow );
237 	static void toTop( Window *win );
238 	static void toBottom( Window *win );
239 	static void nextWindowToTop( Window *win, bool includeLocked = true );
240 	static void prevWindowToTop( Window *win, bool includeLocked = true );
241 	static bool anyFloatingWindowsOpen();
242 	static Window *getTopWindow();
243 
244 	// static message dialog
245 	static Button *message_button; // so you can check for it in other classes
246 	static void showMessageDialog( ScourgeGui *scourgeGui,
247 	                               int x, int y, int w, int h,
248 	                               char *title, Texture texture,
249 	                               char const* message,
250 	                               char *buttonLabel = _( Constants::messages[Constants::OK_LABEL][0] ) );
251 
252 	void move( int x, int y );
253 
254 	void setLastWidget( Widget *w );
255 
getGutter()256 	inline int getGutter() {
257 		return gutter;
258 	}
259 
260 	void setTopWindowBorderColor();
261 	void setWindowBorderColor();
262 
263 protected:
264 	void commonInit( ScourgeGui *scourgeGui, int x, int y, int w, int h, char const* title, bool hasCloseButton, int type );
265 	void drawBorder( int topY, int openHeight );
266 	void drawLineBorder( int topY, int openHeight );
267 	void drawTitle( int topY, int openHeight );
268 	void drawCloseButton( int topY, int openHeight );
269 	void drawDropShadow( int topY, int openHeight );
270 	void drawBackground( int topY, int openHeight );
271 };
272 
273 #endif
274 
275