1 /*
2  * See Licensing and Copyright notice in naev.h
3  */
4 
5 
6 
7 #ifndef TOOLKIT_PRIV_H
8 #  define TOOLKIT_PRIV_H
9 
10 
11 #include "naev.h"
12 #include "log.h"
13 
14 #include "tk/widget.h"
15 
16 
17 /*
18  * Colours to use.
19  */
20 extern const glColour* toolkit_colLight;
21 extern const glColour* toolkit_col;
22 extern const glColour* toolkit_colDark;
23 
24 
25 /**
26  * @typedef WidgetType
27  *
28  * @brief Represents the widget types.
29  */
30 typedef enum WidgetType_ {
31    WIDGET_NULL,
32    WIDGET_BUTTON,
33    WIDGET_TEXT,
34    WIDGET_IMAGE,
35    WIDGET_LIST,
36    WIDGET_RECT,
37    WIDGET_CUST,
38    WIDGET_INPUT,
39    WIDGET_IMAGEARRAY,
40    WIDGET_FADER,
41    WIDGET_TABBEDWINDOW,
42    WIDGET_CHECKBOX
43 } WidgetType;
44 
45 
46 /**
47  * @typedef WidgetStatus
48  *
49  * @brief Represents widget status.
50  *
51  * Only really used by buttons.
52  */
53 typedef enum WidgetStatus_ {
54    WIDGET_STATUS_NORMAL,
55    WIDGET_STATUS_MOUSEOVER,
56    WIDGET_STATUS_MOUSEDOWN,
57    WIDGET_STATUS_SCROLLING
58 } WidgetStatus;
59 
60 
61 #define WGT_FLAG_CANFOCUS     (1<<0)   /**< Widget can get focus. */
62 #define WGT_FLAG_RAWINPUT     (1<<1)   /**< Widget should always get raw input. */
63 #define WGT_FLAG_ALWAYSMMOVE  (1<<2)   /**< Widget should always get mouse motion events. */
64 #define WGT_FLAG_FOCUSED      (1<<3)   /**< Widget is focused. */
65 #define WGT_FLAG_KILL         (1<<9)   /**< Widget should die. */
66 #define wgt_setFlag(w,f)      ((w)->flags |= (f)) /**< Sets a widget flag. */
67 #define wgt_rmFlag(w,f)       ((w)->flags &= ~(f)) /**< Removes a widget flag. */
68 #define wgt_isFlag(w,f)       ((w)->flags & (f)) /**< Checks if a widget has a fla.g */
69 
70 
71 /**
72  * @struct Widget
73  *
74  * @brief Represents a widget.
75  */
76 typedef struct Widget_ {
77    struct Widget_ *next; /**< Linked list. */
78 
79    /* Basic properties. */
80    char* name; /**< Widget's name. */
81    WidgetType type; /**< Widget's type. */
82    int id; /**< Widget ID. */
83 
84    /* Inheritance. */
85    unsigned int wdw; /**< Widget's parent window. */
86 
87    /* Position and dimensions. */
88    int x; /**< X position within the window. */
89    int y; /**< Y position within the window. */
90    int w; /**< Widget width. */
91    int h; /**< Widget height. */
92 
93    unsigned int flags; /**< Widget flags. */
94 
95    /* Event abstraction. */
96    int (*keyevent) ( struct Widget_ *wgt, SDLKey k, SDLMod m ); /**< Key event handler function for the widget. */
97    int (*textevent) ( struct Widget_ *wgt, const char *text ); /**< Text event function handler for the widget. */
98    int (*mmoveevent) ( struct Widget_ *wgt, int x, int y, int rx, int ry); /**< Mouse movement handler function for the widget. */
99    int (*mclickevent) ( struct Widget_ *wgt, int button, int x, int y ); /**< Mouse click event handler function for the widget. */
100 #if SDL_VERSION_ATLEAST(2,0,0)
101    int (*mwheelevent) ( struct Widget_ *wgt, SDL_MouseWheelEvent event ); /**< Mouse click event handler function for the widget. */
102 #endif /* SDL_VERSION_ATLEAST(2,0,0) */
103    void (*scrolldone) ( struct Widget_ *wgt ); /**< Scrolling is over. */
104    int (*rawevent) ( struct Widget_ *wgt, SDL_Event *event ); /**< Raw event handler function for widget. */
105    void (*exposeevent) ( struct Widget_ *wgt, int exposed ); /**< Widget show and hide handler. */
106 
107    /* Misc. routines. */
108    void (*render) ( struct Widget_ *wgt, double x, double y ); /**< Render function for the widget. */
109    void (*renderOverlay) ( struct Widget_ *wgt, double x, double y ); /**< Overlay render fuction for the widget. */
110    void (*cleanup) ( struct Widget_ *wgt ); /**< Clean up function for the widget. */
111    void (*focusGain) ( struct Widget_ *wgt ); /**< Get focus. */
112    void (*focusLose) ( struct Widget_ *wgt ); /**< Lose focus. */
113 
114    /* Status of the widget. */
115    WidgetStatus status; /**< Widget status. */
116 
117    /* Type specific data (defined by type). */
118    union {
119       WidgetButtonData btn; /**< WIDGET_BUTTON */
120       WidgetTextData txt; /**< WIDGET_TEXT */
121       WidgetImageData img; /**< WIDGET_IMAGE */
122       WidgetListData lst; /**< WIDGET_LIST */
123       WidgetRectData rct; /**< WIDGET_RECT */
124       WidgetCustData cst; /**< WIDGET_CUST */
125       WidgetInputData inp; /**< WIDGET_INPUT */
126       WidgetImageArrayData iar; /**< WIDGET_IMAGEARRAY */
127       WidgetFaderData fad; /**< WIDGET_FADER */
128       WidgetTabbedWindowData tab; /**< WIDGET_TABBEDWINDOW */
129       WidgetCheckboxData chk; /**< WIDGET_CHECKBOX */
130    } dat; /**< Stores the widget specific data. */
131 } Widget;
132 
133 
134 #define WINDOW_NOFOCUS     (1<<0) /**< Window can not be active window. */
135 #define WINDOW_NOINPUT     (1<<1) /**< Window receives no input. */
136 #define WINDOW_NORENDER    (1<<2) /**< Window does not render even if it should. */
137 #define WINDOW_NOBORDER    (1<<3) /**< Window does not need border. */
138 #define WINDOW_FULLSCREEN  (1<<4) /**< Window is fullscreen. */
139 #define WINDOW_KILL        (1<<9) /**< Window should die. */
140 #define window_isFlag(w,f) ((w)->flags & (f)) /**< Checks a window flag. */
141 #define window_setFlag(w,f) ((w)->flags |= (f)) /**< Sets a window flag. */
142 #define window_rmFlag(w,f) ((w)->flags &= ~(f)) /**< Removes a window flag. */
143 
144 
145 /**
146  * @struct Window
147  *
148  * @brief Represents a graphical window.
149  */
150 typedef struct Window_ {
151    struct Window_ *next; /* Linked list. */
152 
153    unsigned int id; /**< Unique ID. */
154    char *name; /**< Window name - should be unique. */
155    unsigned int flags; /**< Window flags. */
156    int idgen; /**< ID generator for widgets. */
157 
158    unsigned int parent; /**< Parent window, will close if this one closes. */
159    void (*close_fptr)(unsigned int wid, char* name); /**< How to close the window. */
160 
161    void (*accept_fptr)(unsigned int wid, char* name); /**< Triggered by hitting 'enter' with no widget that catches the keypress. */
162    void (*cancel_fptr)(unsigned int wid, char* name); /**< Triggered by hitting 'escape' with no widget that catches the keypress. */
163    int (*keyevent)(unsigned int wid,SDLKey,SDLMod); /**< User defined custom key event handler. */
164    int (*eventevent)(unsigned int wid,SDL_Event *evt); /**< User defined event handler. */
165 
166    /* Position and dimensions. */
167    int x; /**< X position of the window. */
168    int y; /**< Y position of the window. */
169    double xrel; /**< X position relative to screen width. */
170    double yrel; /**< Y position relative to screen height. */
171    int w; /**< Window width. */
172    int h; /**< Window height. */
173 
174    int exposed; /**< Whether window is visible or hidden. */
175    int focus; /**< Current focused widget. */
176    Widget *widgets; /**< Widget storage. */
177    void *udata; /**< Custom data of the window. */
178 } Window;
179 
180 
181 /* Window stuff. */
182 Window* toolkit_getActiveWindow (void);
183 Window* window_wget( const unsigned int wid );
184 void toolkit_setWindowPos( Window *wdw, int x, int y );
185 int toolkit_inputWindow( Window *wdw, SDL_Event *event, int purge );
186 void window_render( Window* w );
187 void window_renderOverlay( Window* w );
188 
189 
190 /* Widget stuff. */
191 Widget* window_newWidget( Window* w, const char *name );
192 void widget_cleanup( Widget *widget );
193 Widget* window_getwgt( const unsigned int wid, const char* name );
194 void toolkit_setPos( Window *wdw, Widget *wgt, int x, int y );
195 void toolkit_focusSanitize( Window *wdw );
196 void toolkit_focusClear( Window *wdw );
197 void toolkit_nextFocus( Window *wdw );
198 void toolkit_prevFocus( Window *wdw );
199 void toolkit_focusWidget( Window *wdw, Widget *wgt );
200 void toolkit_defocusWidget( Window *wdw, Widget *wgt );
201 
202 
203 /* Render stuff. */
204 void toolkit_drawOutline( int x, int y, int w, int h, int b,
205                           const glColour* c, const glColour* lc );
206 void toolkit_drawOutlineThick( int x, int y, int w, int h, int b,
207                           int thick, const glColour* c, const glColour* lc );
208 void toolkit_drawScrollbar( int x, int y, int w, int h, double pos );
209 void toolkit_drawRect( int x, int y, int w, int h,
210                        const glColour* c, const glColour* lc );
211 void toolkit_drawAltText( int bx, int by, const char *alt );
212 
213 
214 /* Input stuff. */
215 Uint32 toolkit_inputTranslateCoords( Window *w, SDL_Event *event,
216       int *x, int *y, int *rx, int *ry );
217 
218 
219 #endif /* TOOLKIT_PRIV_H */
220 
221