Freeciv-3.3: /fast/freeciv/nightly/main/client/gui-sdl2/widget_window.c Source File

Freeciv-3.3

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
39 struct move {
40 bool moved;
41 struct widget *pwindow;
42 int prev_x;
43 int prev_y;
44};
45
46 static int (*baseclass_redraw)(struct widget *pwidget);
47
48/**********************************************************************/
51 static int redraw_window(struct widget *pwindow)
52{
53 int ret;
54 SDL_Color title_bg_color = {255, 255, 255, 200};
55 SDL_Surface *tmp = NULL;
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);
73 dst.h = WINDOW_TITLE_HEIGHT;
74 fill_rect_alpha(pwindow->dst->surface, &dst, &title_bg_color);
75
76 /* Draw Text on Window's TitleBar */
77 tmp = create_text_surf_from_utf8(pwindow->string_utf8);
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);
82 FREESURFACE(tmp);
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,
90 get_theme_color(COLOR_THEME_WINDOW_TITLEBAR_SEPARATOR));
91 }
92
93 /* Draw frame */
94 if (get_wflags(pwindow) & WF_DRAW_FRAME_AROUND_WIDGET) {
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/**********************************************************************/
124 static 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/**********************************************************************/
139 static void window_select(struct widget *pwindow)
140{
141 /* nothing */
142}
143
144/**********************************************************************/
147 static void window_unselect(struct widget *pwindow)
148{
149 /* nothing */
150}
151
152/**********************************************************************/
155 static void set_client_area(struct widget *pwindow)
156{
157 SDL_Rect area;
158
159 if (get_wflags(pwindow) & WF_DRAW_FRAME_AROUND_WIDGET) {
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/**********************************************************************/
180 struct widget *create_window_skeleton(struct gui_layer *pdest,
181 utf8_str *title, Uint32 flags)
182{
183 int w = 0, h = 0;
184 struct widget *pwindow = widget_new();
185
186 pwindow->set_position = window_set_position;
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;
194 set_wflag(pwindow, WF_FREE_STRING | WF_FREE_GFX | WF_FREE_THEME |
195 WF_DRAW_FRAME_AROUND_WIDGET| flags);
196 set_wstate(pwindow, FC_WS_DISABLED);
197 set_wtype(pwindow, WT_WINDOW);
198 pwindow->mod = KMOD_NONE;
199
200 if (get_wflags(pwindow) & WF_DRAW_FRAME_AROUND_WIDGET) {
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) {
206 SDL_Rect size;
207
208 utf8_str_size(title, &size);
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/**********************************************************************/
231 struct widget *create_window(struct gui_layer *pdest, utf8_str *title,
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/**********************************************************************/
255 bool resize_window(struct widget *pwindow, SDL_Surface *bcgd,
256 SDL_Color *pcolor, Uint16 new_w, Uint16 new_h)
257{
258 SDL_Color color;
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) {
268 refresh_widget_background(pwindow);
269 }
270
271 FREESURFACE(pwindow->dst->surface);
272 pwindow->dst->surface = create_surf(pwindow->size.w,
273 pwindow->size.h,
274 SDL_SWSURFACE);
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 {
290 pwindow->theme = create_surf(new_w, new_h, SDL_SWSURFACE);
291
292 if (pcolor == NULL) {
293 color = *get_theme_color(COLOR_THEME_BACKGROUND);
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/**********************************************************************/
307 static Uint16 move_window_motion(SDL_MouseMotionEvent *motion_event,
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/**********************************************************************/
337 static Uint16 move_window_button_up(SDL_MouseButtonEvent *button_event,
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/**********************************************************************/
352 bool move_window(struct widget *pwindow)
353{
354 bool ret;
355 struct move pmove;
356
357 pmove.pwindow = pwindow;
358 pmove.moved = FALSE;
359 SDL_GetMouseState(&pmove.prev_x, &pmove.prev_y);
360 /* Filter mouse motion events */
361 SDL_SetEventFilter(FilterMouseMotionEvents, NULL);
362 ret = (gui_event_loop((void *)&pmove, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
363 move_window_button_up, move_window_motion) == ID_MOVED_WINDOW);
364 /* Turn off Filter mouse motion events */
365 SDL_SetEventFilter(NULL, NULL);
366
367 return ret;
368}
pcolor
struct canvas int int int int struct sprite *sprite struct canvas struct color * pcolor
Definition canvas_g.h:56
incite_cost
char * incite_cost
Definition comments.c:74
current_theme
QString current_theme
Definition fc_client.cpp:64
flush_dirty
void flush_dirty(void)
Definition mapview.c:468
title
const char * title
Definition repodlgs.c:1314
get_theme_color
SDL_Color * get_theme_color(enum theme_color themecolor)
Definition colors.c:47
resize_surface
SDL_Surface * resize_surface(const SDL_Surface *psrc, Uint16 new_width, Uint16 new_height, int smooth)
Definition graphics.c:1237
fill_rect_alpha
int fill_rect_alpha(SDL_Surface *surf, SDL_Rect *prect, SDL_Color *pcolor)
Definition graphics.c:865
create_line
void create_line(SDL_Surface *dest, Sint16 x0, Sint16 y0, Sint16 x1, Sint16 y1, SDL_Color *pcolor)
Definition graphics.c:1381
alphablit
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:199
get_gui_layer
struct gui_layer * get_gui_layer(SDL_Surface *surface)
Definition graphics.c:90
create_surf
SDL_Surface * create_surf(int width, int height, Uint32 flags)
Definition graphics.c:351
add_gui_layer
struct gui_layer * add_gui_layer(int width, int height)
Definition graphics.c:112
clear_surface
int clear_surface(SDL_Surface *surf, SDL_Rect *dstrect)
Definition graphics.c:400
FREESURFACE
#define FREESURFACE(ptr)
Definition graphics.h:322
map_rgba
#define map_rgba(format, color)
Definition graphics.h:315
ID_MOVED_WINDOW
@ ID_MOVED_WINDOW
Definition gui_id.h:31
ID_ERROR
@ ID_ERROR
Definition gui_id.h:26
ID_WINDOW
@ ID_WINDOW
Definition gui_id.h:30
FilterMouseMotionEvents
int FilterMouseMotionEvents(void *data, SDL_Event *event)
Definition gui_main.c:581
gui_event_loop
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)(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
adj_size
#define adj_size(size)
Definition gui_main.h:141
utf8_str_size
void utf8_str_size(utf8_str *pstr, SDL_Rect *fill)
Definition gui_string.c:106
create_text_surf_from_utf8
SDL_Surface * create_text_surf_from_utf8(utf8_str *pstr)
Definition gui_string.c:425
COLOR_THEME_BACKGROUND
@ COLOR_THEME_BACKGROUND
Definition themecolors.h:24
COLOR_THEME_WINDOW_TITLEBAR_SEPARATOR
@ COLOR_THEME_WINDOW_TITLEBAR_SEPARATOR
Definition themecolors.h:44
refresh_widget_background
void refresh_widget_background(struct widget *pwidget)
Definition widget.c:1151
widget_set_position
static void widget_set_position(struct widget *pwidget, int x, int y)
Definition widget.h:266
WINDOW_TITLE_HEIGHT
#define WINDOW_TITLE_HEIGHT
Definition widget.h:35
FC_WS_DISABLED
@ FC_WS_DISABLED
Definition widget.h:99
widget_draw_frame
static void widget_draw_frame(struct widget *pwidget)
Definition widget.h:281
widget_mark_dirty
static void widget_mark_dirty(struct widget *pwidget)
Definition widget.h:286
get_wflags
enum widget_flag get_wflags(const struct widget *pwidget)
Definition widget_core.c:86
set_wstate
void set_wstate(struct widget *pwidget, enum widget_state state)
Definition widget_core.c:36
WF_FREE_GFX
@ WF_FREE_GFX
Definition widget.h:70
WF_DRAW_FRAME_AROUND_WIDGET
@ WF_DRAW_FRAME_AROUND_WIDGET
Definition widget.h:86
WF_FREE_STRING
@ WF_FREE_STRING
Definition widget.h:76
WF_RESTORE_BACKGROUND
@ WF_RESTORE_BACKGROUND
Definition widget.h:85
WF_FREE_THEME
@ WF_FREE_THEME
Definition widget.h:72
set_wtype
void set_wtype(struct widget *pwidget, enum widget_type type)
Definition widget_core.c:45
widget_set_area
static void widget_set_area(struct widget *pwidget, SDL_Rect area)
Definition widget.h:261
set_wflag
void set_wflag(struct widget *pwidget, enum widget_flag flag)
Definition widget_core.c:54
WT_WINDOW
@ WT_WINDOW
Definition widget.h:52
widget_new
struct widget * widget_new(void)
Definition widget_core.c:252
resize_window
bool resize_window(struct widget *pwindow, SDL_Surface *bcgd, SDL_Color *pcolor, Uint16 new_w, Uint16 new_h)
Definition widget_window.c:255
move_window_motion
static Uint16 move_window_motion(SDL_MouseMotionEvent *motion_event, void *data)
Definition widget_window.c:307
window_select
static void window_select(struct widget *pwindow)
Definition widget_window.c:139
baseclass_redraw
static int(* baseclass_redraw)(struct widget *pwidget)
Definition widget_window.c:46
create_window_skeleton
struct widget * create_window_skeleton(struct gui_layer *pdest, utf8_str *title, Uint32 flags)
Definition widget_window.c:180
window_set_position
static void window_set_position(struct widget *pwindow, int x, int y)
Definition widget_window.c:124
redraw_window
static int redraw_window(struct widget *pwindow)
Definition widget_window.c:51
window_unselect
static void window_unselect(struct widget *pwindow)
Definition widget_window.c:147
set_client_area
static void set_client_area(struct widget *pwindow)
Definition widget_window.c:155
move_window_button_up
static Uint16 move_window_button_up(SDL_MouseButtonEvent *button_event, void *data)
Definition widget_window.c:337
move_window
bool move_window(struct widget *pwindow)
Definition widget_window.c:352
create_window
struct widget * create_window(struct gui_layer *pdest, utf8_str *title, Uint16 w, Uint16 h, Uint32 flags)
Definition widget_window.c:231
gui_id.h
gui_tilespec.h
themespec.h
widget.h
widget_p.h
colors.h
graphics.h
mapview.h
log.h
MAX
#define MAX(x, y)
Definition shared.h:54
size
size_t size
Definition specvec.h:72
y
struct sprite int int y
Definition sprite_g.h:31
x
struct sprite int x
Definition sprite_g.h:31
color
Definition colors.h:21
gui_layer
Definition graphics.h:227
gui_layer::surface
SDL_Surface * surface
Definition graphics.h:229
gui_layer::dest_rect
SDL_Rect dest_rect
Definition graphics.h:228
move
Definition widget_window.c:39
move::pwindow
struct widget * pwindow
Definition widget_window.c:41
move::moved
bool moved
Definition widget_window.c:40
move::prev_y
int prev_y
Definition widget_window.c:43
move::prev_x
int prev_x
Definition widget_window.c:42
utf8_str
Definition gui_string.h:52
widget
Definition widget.h:112
widget::unselect
void(* unselect)(struct widget *pwidget)
Definition widget.h:168
widget::set_position
void(* set_position)(struct widget *pwidget, int x, int y)
Definition widget.h:160
widget::theme
SDL_Surface * theme
Definition widget.h:118
widget::redraw
int(* redraw)(struct widget *pwidget)
Definition widget.h:163
widget::dst
struct gui_layer * dst
Definition widget.h:116
widget::mod
Uint16 mod
Definition widget.h:154
widget::string_utf8
utf8_str * string_utf8
Definition widget.h:121
widget::area
SDL_Rect area
Definition widget.h:149
widget::select
void(* select)(struct widget *pwidget)
Definition widget.h:167
widget::size
SDL_Rect size
Definition widget.h:145
widget::data
union widget::@217 data
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47