Freeciv-3.2
Loading...
Searching...
No Matches
widget_window.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2006 - The Freeciv Project
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18/* SDL2 */
19#ifdef SDL2_PLAIN_INCLUDE
20#include <SDL.h>
21#else /* SDL2_PLAIN_INCLUDE */
22#include <SDL2/SDL.h>
23#endif /* SDL2_PLAIN_INCLUDE */
24
25/* utility */
26#include "log.h"
27
28/* gui-sdl2 */
29#include "colors.h"
30#include "graphics.h"
31#include "gui_id.h"
32#include "gui_tilespec.h"
33#include "mapview.h"
34#include "themespec.h"
35
36#include "widget.h"
37#include "widget_p.h"
38
39struct move {
40 bool moved;
41 struct widget *pwindow;
42 int prev_x;
43 int prev_y;
44};
45
46static int (*baseclass_redraw)(struct widget *pwidget);
47
48/**********************************************************************/
51static int redraw_window(struct widget *pwindow)
52{
53 int ret;
54 SDL_Color title_bg_color = {255, 255, 255, 200};
56 SDL_Rect dst = pwindow->size;
57
58 ret = (*baseclass_redraw)(pwindow);
59 if (ret != 0) {
60 return ret;
61 }
62
63 /* Draw theme */
64 clear_surface(pwindow->dst->surface, &dst);
65 alphablit(pwindow->theme, NULL, pwindow->dst->surface, &dst, 255);
66
67 /* window has title string == has title bar */
68 if (pwindow->string_utf8 != NULL) {
69
70 /* Draw Window's TitleBar */
71 dst = pwindow->area;
72 dst.y -= (WINDOW_TITLE_HEIGHT + 1);
75
76 /* Draw Text on Window's TitleBar */
78 dst.x += adj_size(4);
79 if (tmp) {
80 dst.y += ((WINDOW_TITLE_HEIGHT - tmp->h) / 2);
81 alphablit(tmp, NULL, pwindow->dst->surface, &dst, 255);
83 }
84
85 dst = pwindow->area;
86
87 create_line(pwindow->dst->surface,
88 dst.x, dst.y - 1,
89 dst.x + dst.w - 1, dst.y - 1,
91 }
92
93 /* Draw frame */
95 widget_draw_frame(pwindow);
96 }
97
98 return 0;
99}
100
101/**************************************************************************
102 Window mechanism.
103
104 Active Window schould be first on list (All Widgets on this
105 Window that are on List must be above)
106
107 LIST:
108
109 *pFirst_Widget_on_Active_Window.
110
111 *pN__Widget_on_Active_Window.
112 *pActive_Window. <------
113 *pRest_Widgets.
114
115 This trick give us:
116 - if any Widget is under ( area of ) this Window and Mouse
117 cursor is above them, 'WidgetListScaner(...)' return
118 pointer to Active Window not to this Widget.
119**************************************************************************/
120
121/**********************************************************************/
124static void window_set_position(struct widget *pwindow, int x, int y)
125{
126 struct gui_layer *gui_layer;
127
128 pwindow->size.x = 0;
129 pwindow->size.y = 0;
130
131 gui_layer = get_gui_layer(pwindow->dst->surface);
132 gui_layer->dest_rect.x = x;
133 gui_layer->dest_rect.y = y;
134}
135
136/**********************************************************************/
139static void window_select(struct widget *pwindow)
140{
141 /* nothing */
142}
143
144/**********************************************************************/
147static void window_unselect(struct widget *pwindow)
148{
149 /* nothing */
150}
151
152/**********************************************************************/
155static void set_client_area(struct widget *pwindow)
156{
157 SDL_Rect area;
158
160 area.x = current_theme->fr_left->w;
161 area.y = current_theme->fr_top->h;
162 area.w = pwindow->size.w - current_theme->fr_left->w - current_theme->fr_right->w;
163 area.h = pwindow->size.h - current_theme->fr_top->h - current_theme->fr_bottom->h;
164 } else {
165 area = pwindow->size;
166 }
167
168 if (pwindow->string_utf8 != NULL) {
169 area.y += (WINDOW_TITLE_HEIGHT + 1);
170 area.h -= (WINDOW_TITLE_HEIGHT + 1);
171 }
172
173 widget_set_area(pwindow, area);
174}
175
176/**********************************************************************/
181 utf8_str *title, Uint32 flags)
182{
183 int w = 0, h = 0;
184 struct widget *pwindow = widget_new();
185
187
188 baseclass_redraw = pwindow->redraw;
189 pwindow->redraw = redraw_window;
190 pwindow->select = window_select;
191 pwindow->unselect = window_unselect;
192
193 pwindow->string_utf8 = title;
196 set_wstate(pwindow, FC_WS_DISABLED);
197 set_wtype(pwindow, WT_WINDOW);
198 pwindow->mod = KMOD_NONE;
199
201 w += current_theme->fr_left->w + current_theme->fr_right->w;
202 h += current_theme->fr_top->h + current_theme->fr_bottom->h;
203 }
204
205 if (title != NULL) {
207
209
210 w += size.w + adj_size(10);
211 h += MAX(size.h, WINDOW_TITLE_HEIGHT + 1);
212 }
213
214 pwindow->size.w = w;
215 pwindow->size.h = h;
216
217 set_client_area(pwindow);
218
219 if (pdest) {
220 pwindow->dst = pdest;
221 } else {
222 pwindow->dst = add_gui_layer(w, h);
223 }
224
225 return pwindow;
226}
227
228/**********************************************************************/
232 Uint16 w, Uint16 h, Uint32 flags)
233{
234 struct widget *pwindow = create_window_skeleton(pdest, title, flags);
235
236 resize_window(pwindow, NULL, NULL, w, h);
237
238 return pwindow;
239}
240
241/**********************************************************************/
255bool resize_window(struct widget *pwindow, SDL_Surface *bcgd,
257{
259
260 /* Window */
261 if ((new_w != pwindow->size.w) || (new_h != pwindow->size.h)) {
262 pwindow->size.w = new_w;
263 pwindow->size.h = new_h;
264
265 set_client_area(pwindow);
266
267 if (get_wflags(pwindow) & WF_RESTORE_BACKGROUND) {
269 }
270
271 FREESURFACE(pwindow->dst->surface);
272 pwindow->dst->surface = create_surf(pwindow->size.w,
273 pwindow->size.h,
275 }
276
277 if (bcgd != pwindow->theme) {
278 FREESURFACE(pwindow->theme);
279 }
280
281 if (bcgd) {
282 if (bcgd->w != new_w || bcgd->h != new_h) {
283 pwindow->theme = resize_surface(bcgd, new_w, new_h, 2);
284 return 1;
285 } else {
286 pwindow->theme = bcgd;
287 return FALSE;
288 }
289 } else {
291
292 if (pcolor == NULL) {
294
295 pcolor = &color;
296 }
297
298 SDL_FillRect(pwindow->theme, NULL, map_rgba(pwindow->theme->format, *pcolor));
299
300 return TRUE;
301 }
302}
303
304/**********************************************************************/
308 void *data)
309{
310 struct move *pmove = (struct move *)data;
311 int xrel, yrel;
312
313 if (!pmove->moved) {
314 pmove->moved = TRUE;
315 }
316
317 widget_mark_dirty(pmove->pwindow);
318
319 xrel = motion_event->x - pmove->prev_x;
320 yrel = motion_event->y - pmove->prev_y;
321 pmove->prev_x = motion_event->x;
322 pmove->prev_y = motion_event->y;
323
324 widget_set_position(pmove->pwindow,
325 (pmove->pwindow->dst->dest_rect.x + pmove->pwindow->size.x) + xrel,
326 (pmove->pwindow->dst->dest_rect.y + pmove->pwindow->size.y) + yrel);
327
328 widget_mark_dirty(pmove->pwindow);
329 flush_dirty();
330
331 return ID_ERROR;
332}
333
334/**********************************************************************/
338 void *data)
339{
340 struct move *pmove = (struct move *)data;
341
342 if (pmove && pmove->moved) {
343 return (Uint16)ID_MOVED_WINDOW;
344 }
345
346 return (Uint16)ID_WINDOW;
347}
348
349/**********************************************************************/
353{
354 bool ret;
355 struct move pmove;
356
358 pmove.moved = FALSE;
359 SDL_GetMouseState(&pmove.prev_x, &pmove.prev_y);
360 /* Filter mouse motion events */
362 ret = (gui_event_loop((void *)&pmove, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
364 /* Turn off Filter mouse motion events */
366
367 return ret;
368}
struct canvas int int int int struct sprite *sprite struct canvas struct color * pcolor
Definition canvas_g.h:56
char * incite_cost
Definition comments.c:75
QString current_theme
Definition fc_client.cpp:65
void flush_dirty(void)
Definition mapview.c:468
const char * title
Definition repodlgs.c:1314
SDL_Color * get_theme_color(enum theme_color themecolor)
Definition colors.c:47
SDL_Surface * resize_surface(const SDL_Surface *psrc, Uint16 new_width, Uint16 new_height, int smooth)
Definition graphics.c:1237
int fill_rect_alpha(SDL_Surface *surf, SDL_Rect *prect, SDL_Color *pcolor)
Definition graphics.c:865
void create_line(SDL_Surface *dest, Sint16 x0, Sint16 y0, Sint16 x1, Sint16 y1, SDL_Color *pcolor)
Definition graphics.c:1379
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:199
struct gui_layer * get_gui_layer(SDL_Surface *surface)
Definition graphics.c:90
SDL_Surface * create_surf(int width, int height, Uint32 flags)
Definition graphics.c:351
struct gui_layer * add_gui_layer(int width, int height)
Definition graphics.c:112
int clear_surface(SDL_Surface *surf, SDL_Rect *dstrect)
Definition graphics.c:400
#define FREESURFACE(ptr)
Definition graphics.h:322
#define map_rgba(format, color)
Definition graphics.h:315
@ ID_MOVED_WINDOW
Definition gui_id.h:31
@ ID_ERROR
Definition gui_id.h:26
@ ID_WINDOW
Definition gui_id.h:30
int FilterMouseMotionEvents(void *data, SDL_Event *event)
Definition gui_main.c:581
Uint16 gui_event_loop(void *data, void(*loop_action)(void *data), Uint16(*key_down_handler)(SDL_Keysym key, void *data), Uint16(*key_up_handler)(SDL_Keysym key, void *data), Uint16(*textinput_handler)(const char *text, void *data), Uint16(*finger_down_handler)(SDL_TouchFingerEvent *touch_event, void *data), Uint16(*finger_up_handler)(SDL_TouchFingerEvent *touch_event, void *data), Uint16(*finger_motion_handler)(SDL_TouchFingerEvent *touch_event, void *data), Uint16(*mouse_button_down_handler)(SDL_MouseButtonEvent *button_event, void *data), Uint16(*mouse_button_up_handler)(SDL_MouseButtonEvent *button_event, void *data), Uint16(*mouse_motion_handler)(SDL_MouseMotionEvent *motion_event, void *data))
Definition gui_main.c:602
#define adj_size(size)
Definition gui_main.h:141
void utf8_str_size(utf8_str *pstr, SDL_Rect *fill)
Definition gui_string.c:106
SDL_Surface * create_text_surf_from_utf8(utf8_str *pstr)
Definition gui_string.c:425
@ COLOR_THEME_BACKGROUND
Definition themecolors.h:23
@ COLOR_THEME_WINDOW_TITLEBAR_SEPARATOR
Definition themecolors.h:43
void refresh_widget_background(struct widget *pwidget)
Definition widget.c:1151
static void widget_set_position(struct widget *pwidget, int x, int y)
Definition widget.h:266
#define WINDOW_TITLE_HEIGHT
Definition widget.h:35
@ FC_WS_DISABLED
Definition widget.h:99
static void widget_draw_frame(struct widget *pwidget)
Definition widget.h:281
static void widget_mark_dirty(struct widget *pwidget)
Definition widget.h:286
enum widget_flag get_wflags(const struct widget *pwidget)
Definition widget_core.c:86
void set_wstate(struct widget *pwidget, enum widget_state state)
Definition widget_core.c:36
@ WF_FREE_GFX
Definition widget.h:70
@ WF_DRAW_FRAME_AROUND_WIDGET
Definition widget.h:86
@ WF_FREE_STRING
Definition widget.h:76
@ WF_RESTORE_BACKGROUND
Definition widget.h:85
@ WF_FREE_THEME
Definition widget.h:72
void set_wtype(struct widget *pwidget, enum widget_type type)
Definition widget_core.c:45
static void widget_set_area(struct widget *pwidget, SDL_Rect area)
Definition widget.h:261
void set_wflag(struct widget *pwidget, enum widget_flag flag)
Definition widget_core.c:54
@ WT_WINDOW
Definition widget.h:52
struct widget * widget_new(void)
bool resize_window(struct widget *pwindow, SDL_Surface *bcgd, SDL_Color *pcolor, Uint16 new_w, Uint16 new_h)
static Uint16 move_window_motion(SDL_MouseMotionEvent *motion_event, void *data)
static void window_select(struct widget *pwindow)
static int(* baseclass_redraw)(struct widget *pwidget)
struct widget * create_window_skeleton(struct gui_layer *pdest, utf8_str *title, Uint32 flags)
static void window_set_position(struct widget *pwindow, int x, int y)
static int redraw_window(struct widget *pwindow)
static void window_unselect(struct widget *pwindow)
static void set_client_area(struct widget *pwindow)
static Uint16 move_window_button_up(SDL_MouseButtonEvent *button_event, void *data)
bool move_window(struct widget *pwindow)
struct widget * create_window(struct gui_layer *pdest, utf8_str *title, Uint16 w, Uint16 h, Uint32 flags)
#define MAX(x, y)
Definition shared.h:54
size_t size
Definition specvec.h:72
struct sprite int int y
Definition sprite_g.h:31
struct sprite int x
Definition sprite_g.h:31
Definition colors.h:21
SDL_Surface * surface
Definition graphics.h:229
SDL_Rect dest_rect
Definition graphics.h:228
struct widget * pwindow
bool moved
int prev_y
int prev_x
void(* unselect)(struct widget *pwidget)
Definition widget.h:168
union widget::@194 data
void(* set_position)(struct widget *pwidget, int x, int y)
Definition widget.h:160
SDL_Surface * theme
Definition widget.h:118
int(* redraw)(struct widget *pwidget)
Definition widget.h:163
struct gui_layer * dst
Definition widget.h:116
Uint16 mod
Definition widget.h:154
utf8_str * string_utf8
Definition widget.h:121
SDL_Rect area
Definition widget.h:149
void(* select)(struct widget *pwidget)
Definition widget.h:167
SDL_Rect size
Definition widget.h:145
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47