Freeciv-3.1: /fast/freeciv/nightly/S3_1/client/gui-sdl2/messagewin.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
messagewin.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
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/**********************************************************************
15 messagewin.c - description
16 -------------------
17 begin : Feb 2 2003
18 copyright : (C) 2003 by Rafał Bursig
19 email : Rafał Bursig <bursig@poczta.fm>
20 **********************************************************************/
21
22#ifdef HAVE_CONFIG_H
23#include <fc_config.h>
24#endif
25
26/* SDL2 */
27#ifdef SDL2_PLAIN_INCLUDE
28#include <SDL.h>
29#else /* SDL2_PLAIN_INCLUDE */
30#include <SDL2/SDL.h>
31#endif /* SDL2_PLAIN_INCLUDE */
32
33/* utility */
34#include "fcintl.h"
35#include "log.h"
36
37/* client */
38#include "options.h"
39
40/* gui-sdl2 */
41#include "citydlg.h"
42#include "colors.h"
43#include "graphics.h"
44#include "gui_id.h"
45#include "gui_main.h"
46#include "gui_tilespec.h"
47#include "mapview.h"
48#include "themespec.h"
49#include "utf8string.h"
50#include "widget.h"
51
52#include "messagewin.h"
53
54
55#ifdef SMALL_SCREEN
56#define N_MSG_VIEW 3 /* max before scrolling happens */
57#else
58#define N_MSG_VIEW 6
59#endif
60
61/* 0 -> use theme default */
62#define PTSIZE_LOG_FONT 0
63
64 static struct advanced_dialog *msg_dlg = NULL;
65
66/**********************************************************************/
69 static int msg_callback(struct widget *pwidget)
70{
71 if (PRESSED_EVENT(main_data.event)) {
72 int message_index = *(int*)pwidget->data.ptr;
73
74 pwidget->string_utf8->fgcol = *get_theme_color(COLOR_THEME_MESWIN_ACTIVE_TEXT2);
75 unselect_widget_action();
76
77 meswin_double_click(message_index);
78 meswin_set_visited_state(message_index, TRUE);
79 }
80
81 return -1;
82}
83
84/**********************************************************************/
87 static int move_msg_window_callback(struct widget *pwindow)
88{
89 if (PRESSED_EVENT(main_data.event)) {
90 move_window_group(msg_dlg->begin_widget_list, pwindow);
91 }
92
93 return -1;
94}
95
96/* ======================================================================
97 Public
98 ====================================================================== */
99
100/**********************************************************************/
103 void real_meswin_dialog_update(void *unused)
104{
105 int msg_count;
106 int current_count;
107 const struct message *msg = NULL;
108 struct widget *buf = NULL, *pwindow = NULL;
109 utf8_str *pstr = NULL;
110 SDL_Rect area = {0, 0, 0, 0};
111 bool create;
112 int label_width;
113
114 if (msg_dlg == NULL) {
115 meswin_dialog_popup(TRUE);
116 }
117
118 msg_count = meswin_get_num_messages();
119 current_count = msg_dlg->scroll->count;
120
121 if (current_count > 0) {
122 undraw_group(msg_dlg->begin_active_widget_list, msg_dlg->end_active_widget_list);
123 del_group_of_widgets_from_gui_list(msg_dlg->begin_active_widget_list,
124 msg_dlg->end_active_widget_list);
125 msg_dlg->begin_active_widget_list = NULL;
126 msg_dlg->end_active_widget_list = NULL;
127 msg_dlg->active_widget_list = NULL;
128 /* hide scrollbar */
129 hide_scrollbar(msg_dlg->scroll);
130 msg_dlg->scroll->count = 0;
131 current_count = 0;
132 }
133 create = (current_count == 0);
134
135 pwindow = msg_dlg->end_widget_list;
136
137 area = pwindow->area;
138
139 label_width = area.w - msg_dlg->scroll->up_left_button->size.w - adj_size(3);
140
141 if (msg_count > 0) {
142 for (; current_count < msg_count; current_count++) {
143 msg = meswin_get_message(current_count);
144 pstr = create_utf8_from_char(msg->descr, PTSIZE_LOG_FONT);
145
146 if (convert_utf8_str_to_const_surface_width(pstr, label_width - adj_size(10))) {
147 /* string must be divided to fit into the given area */
148 utf8_str *pstr2;
149 char **utf8_texts = create_new_line_utf8strs(pstr->text);
150 int count = 0;
151
152 while (utf8_texts[count]) {
153 pstr2 = create_utf8_str(utf8_texts[count],
154 strlen(utf8_texts[count]) + 1, PTSIZE_LOG_FONT);
155
156 buf = create_iconlabel(NULL, pwindow->dst, pstr2,
157 (WF_RESTORE_BACKGROUND|WF_DRAW_TEXT_LABEL_WITH_SPACE|WF_FREE_DATA));
158
159 /* this block is duplicated in the "else" branch */
160 {
161 buf->string_utf8->bgcol = (SDL_Color) {0, 0, 0, 0};
162
163 buf->size.w = label_width;
164 buf->data.ptr = fc_calloc(1, sizeof(int));
165 *(int*)buf->data.ptr = current_count;
166 buf->action = msg_callback;
167 if (msg->tile) {
168 set_wstate(buf, FC_WS_NORMAL);
169 if (msg->visited) {
170 buf->string_utf8->fgcol = *get_theme_color(COLOR_THEME_MESWIN_ACTIVE_TEXT2);
171 } else {
172 buf->string_utf8->fgcol = *get_theme_color(COLOR_THEME_MESWIN_ACTIVE_TEXT);
173 }
174 }
175
176 buf->id = ID_LABEL;
177
178 widget_set_area(buf, area);
179
180 /* add to widget list */
181 if (create) {
182 add_widget_to_vertical_scroll_widget_list(msg_dlg, buf, pwindow, FALSE,
183 area.x, area.y);
184 create = FALSE;
185 } else {
186 add_widget_to_vertical_scroll_widget_list(msg_dlg, buf,
187 msg_dlg->begin_active_widget_list,
188 FALSE, area.x, area.y);
189 }
190 }
191 count++;
192 } /* while */
193 FREEUTF8STR(pstr);
194 } else {
195 buf = create_iconlabel(NULL, pwindow->dst, pstr,
196 (WF_RESTORE_BACKGROUND|WF_DRAW_TEXT_LABEL_WITH_SPACE|WF_FREE_DATA));
197
198 /* duplicated block */
199 {
200 buf->string_utf8->bgcol = (SDL_Color) {0, 0, 0, 0};
201
202 buf->size.w = label_width;
203 buf->data.ptr = fc_calloc(1, sizeof(int));
204 *(int*)buf->data.ptr = current_count;
205 buf->action = msg_callback;
206 if (msg->tile) {
207 set_wstate(buf, FC_WS_NORMAL);
208 if (msg->visited) {
209 buf->string_utf8->fgcol = *get_theme_color(COLOR_THEME_MESWIN_ACTIVE_TEXT2);
210 } else {
211 buf->string_utf8->fgcol = *get_theme_color(COLOR_THEME_MESWIN_ACTIVE_TEXT);
212 }
213 }
214
215 buf->id = ID_LABEL;
216
217 widget_set_area(buf, area);
218
219 /* add to widget list */
220 if (create) {
221 add_widget_to_vertical_scroll_widget_list(msg_dlg, buf,
222 pwindow, FALSE,
223 area.x, area.y);
224 create = FALSE;
225 } else {
226 add_widget_to_vertical_scroll_widget_list(msg_dlg, buf,
227 msg_dlg->begin_active_widget_list,
228 FALSE, area.x, area.y);
229 }
230 }
231 } /* if */
232 } /* for */
233 } /* if */
234
235 redraw_group(msg_dlg->begin_widget_list, pwindow, 0);
236 widget_flush(pwindow);
237}
238
239/**********************************************************************/
242 void meswin_dialog_popup(bool raise)
243{
244 utf8_str *pstr;
245 struct widget *pwindow = NULL;
246 SDL_Surface *background;
247 SDL_Rect area;
248 SDL_Rect size;
249
250 if (msg_dlg) {
251 return;
252 }
253
254 msg_dlg = fc_calloc(1, sizeof(struct advanced_dialog));
255
256 /* Create window */
257 pstr = create_utf8_from_char_fonto(_("Messages"), FONTO_ATTENTION);
258 pstr->style = TTF_STYLE_BOLD;
259
260 pwindow = create_window_skeleton(NULL, pstr, 0);
261
262 pwindow->action = move_msg_window_callback;
263 set_wstate(pwindow, FC_WS_NORMAL);
264 add_to_gui_list(ID_CHATLINE_WINDOW, pwindow);
265
266 msg_dlg->end_widget_list = pwindow;
267 msg_dlg->begin_widget_list = pwindow;
268
269 /* create scrollbar */
270 create_vertical_scrollbar(msg_dlg, 1, N_MSG_VIEW, TRUE, TRUE);
271
272 pstr = create_utf8_from_char("sample text", PTSIZE_LOG_FONT);
273
274 /* define content area */
275 area.w = (adj_size(520) - (pwindow->size.w - pwindow->area.w));
276 utf8_str_size(pstr, &size);
277 area.h = (N_MSG_VIEW + 1) * size.h;
278
279 FREEUTF8STR(pstr);
280
281 /* create window background */
282 background = theme_get_background(active_theme, BACKGROUND_MESSAGEWIN);
283 if (resize_window(pwindow, background, NULL,
284 (pwindow->size.w - pwindow->area.w) + area.w,
285 (pwindow->size.h - pwindow->area.h) + area.h)) {
286 FREESURFACE(background);
287 }
288
289 area = pwindow->area;
290
291 setup_vertical_scrollbar_area(msg_dlg->scroll,
292 area.x + area.w, area.y,
293 area.h, TRUE);
294
295 hide_scrollbar(msg_dlg->scroll);
296
297 widget_set_position(pwindow, (main_window_width() - pwindow->size.w)/2, adj_size(25));
298
299 widget_redraw(pwindow);
300
301 real_meswin_dialog_update(NULL);
302}
303
304/**********************************************************************/
307 void meswin_dialog_popdown(void)
308{
309 if (msg_dlg) {
310 popdown_window_group_dialog(msg_dlg->begin_widget_list,
311 msg_dlg->end_widget_list);
312 FC_FREE(msg_dlg->scroll);
313 FC_FREE(msg_dlg);
314 }
315}
316
317/**********************************************************************/
320 bool meswin_dialog_is_open(void)
321{
322 return (msg_dlg != NULL);
323}
fcintl.h
_
#define _(String)
Definition fcintl.h:67
meswin_dialog_popdown
void meswin_dialog_popdown(void)
Definition messagewin.c:431
meswin_dialog_popup
void meswin_dialog_popup(bool raise)
Definition messagewin.c:416
real_meswin_dialog_update
void real_meswin_dialog_update(void *unused)
Definition messagewin.c:450
meswin_dialog_is_open
bool meswin_dialog_is_open(void)
Definition messagewin.c:442
get_theme_color
SDL_Color * get_theme_color(enum theme_color themecolor)
Definition colors.c:44
main_window_width
int main_window_width(void)
Definition graphics.c:685
main_data
struct sdl2_data main_data
Definition graphics.c:57
FREESURFACE
#define FREESURFACE(ptr)
Definition graphics.h:322
adj_size
#define adj_size(size)
Definition gui_main.h:138
PRESSED_EVENT
#define PRESSED_EVENT(event)
Definition gui_main.h:68
msg_dlg
static struct advanced_dialog * msg_dlg
Definition messagewin.c:64
N_MSG_VIEW
#define N_MSG_VIEW
Definition messagewin.c:58
move_msg_window_callback
static int move_msg_window_callback(struct widget *pwindow)
Definition messagewin.c:87
msg_callback
static int msg_callback(struct widget *pwidget)
Definition messagewin.c:69
PTSIZE_LOG_FONT
#define PTSIZE_LOG_FONT
Definition messagewin.c:62
citydlg.h
colors.h
graphics.h
gui_main.h
mapview.h
messagewin.h
gui_id.h
ID_CHATLINE_WINDOW
@ ID_CHATLINE_WINDOW
Definition gui_id.h:57
ID_LABEL
@ ID_LABEL
Definition gui_id.h:27
utf8_str_size
void utf8_str_size(utf8_str *pstr, SDL_Rect *fill)
Definition gui_string.c:106
create_utf8_str
utf8_str * create_utf8_str(char *in_text, size_t n_alloc, Uint16 ptsize)
Definition gui_string.c:206
convert_utf8_str_to_const_surface_width
bool convert_utf8_str_to_const_surface_width(utf8_str *pstr, int width)
Definition gui_string.c:449
create_utf8_from_char
#define create_utf8_from_char(string_in, ptsize)
Definition gui_string.h:102
FREEUTF8STR
#define FREEUTF8STR(pstr)
Definition gui_string.h:92
FONTO_ATTENTION
@ FONTO_ATTENTION
Definition gui_string.h:66
create_utf8_from_char_fonto
#define create_utf8_from_char_fonto(string_in, fonto)
Definition gui_string.h:107
gui_tilespec.h
log.h
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
FC_FREE
#define FC_FREE(ptr)
Definition mem.h:41
meswin_get_num_messages
int meswin_get_num_messages(void)
Definition messagewin_common.c:167
meswin_get_message
const struct message * meswin_get_message(int message_index)
Definition messagewin_common.c:154
meswin_double_click
void meswin_double_click(int message_index)
Definition messagewin_common.c:225
meswin_set_visited_state
void meswin_set_visited_state(int message_index, bool state)
Definition messagewin_common.c:175
options.h
size
size_t size
Definition specvec.h:72
advanced_dialog
Definition widget.h:180
advanced_dialog::active_widget_list
struct widget * active_widget_list
Definition widget.h:186
advanced_dialog::end_widget_list
struct widget * end_widget_list
Definition widget.h:182
advanced_dialog::begin_widget_list
struct widget * begin_widget_list
Definition widget.h:181
advanced_dialog::begin_active_widget_list
struct widget * begin_active_widget_list
Definition widget.h:184
advanced_dialog::end_active_widget_list
struct widget * end_active_widget_list
Definition widget.h:185
advanced_dialog::scroll
struct scroll_bar * scroll
Definition widget.h:187
message
Definition messagewin_common.h:29
message::descr
char * descr
Definition messagewin_common.h:30
message::visited
bool visited
Definition messagewin_common.h:36
message::tile
struct tile * tile
Definition messagewin_common.h:32
scroll_bar::up_left_button
struct widget * up_left_button
Definition widget_scrollbar.h:21
scroll_bar::count
Uint16 count
Definition widget_scrollbar.h:27
sdl2_data::event
SDL_Event event
Definition graphics.h:217
utf8_str
Definition gui_string.h:52
utf8_str::fgcol
SDL_Color fgcol
Definition gui_string.h:57
widget
Definition widget.h:112
widget::ptr
void * ptr
Definition widget.h:133
widget::data
union widget::@214 data
widget::action
int(* action)(struct widget *)
Definition widget.h:157
widget::cont
struct container * cont
Definition widget.h:127
widget::string_utf8
utf8_str * string_utf8
Definition widget.h:121
widget::area
SDL_Rect area
Definition widget.h:149
widget::size
SDL_Rect size
Definition widget.h:145
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
theme_get_background
SDL_Surface * theme_get_background(const struct theme *t, enum theme_background background)
Definition themebackgrounds.c:94
BACKGROUND_MESSAGEWIN
@ BACKGROUND_MESSAGEWIN
Definition themebackgrounds.h:40
COLOR_THEME_MESWIN_ACTIVE_TEXT
@ COLOR_THEME_MESWIN_ACTIVE_TEXT
Definition themecolors.h:97
COLOR_THEME_MESWIN_ACTIVE_TEXT2
@ COLOR_THEME_MESWIN_ACTIVE_TEXT2
Definition themecolors.h:98
active_theme
struct theme * active_theme
Definition themespec.c:154
themespec.h
create_new_line_utf8strs
char ** create_new_line_utf8strs(const char *pstr)
Definition utf8string.c:35
utf8string.h
add_to_gui_list
void add_to_gui_list(Uint16 id, struct widget *gui)
Definition widget.c:585
del_group_of_widgets_from_gui_list
void del_group_of_widgets_from_gui_list(struct widget *begin_group_widget_list, struct widget *end_group_widget_list)
Definition widget.c:862
unselect_widget_action
void unselect_widget_action(void)
Definition widget.c:417
undraw_group
void undraw_group(struct widget *begin_group_widget_list, struct widget *end_group_widget_list)
Definition widget.c:753
redraw_group
Uint16 redraw_group(const struct widget *begin_group_widget_list, const struct widget *end_group_widget_list, int add_to_update)
Definition widget.c:719
popdown_window_group_dialog
void popdown_window_group_dialog(struct widget *begin_group_widget_list, struct widget *end_group_widget_list)
Definition widget.c:982
move_window_group
void move_window_group(struct widget *begin_widget_list, struct widget *pwindow)
Definition widget.c:1038
widget.h
widget_set_position
static void widget_set_position(struct widget *pwidget, int x, int y)
Definition widget.h:266
FC_WS_NORMAL
@ FC_WS_NORMAL
Definition widget.h:96
add_widget_to_vertical_scroll_widget_list
bool add_widget_to_vertical_scroll_widget_list(struct advanced_dialog *dlg, struct widget *new_widget, struct widget *add_dock, bool dir, Sint16 start_x, Sint16 start_y) fc__attribute((nonnull(2)))
Definition widget_scrollbar.c:1151
widget_flush
static void widget_flush(struct widget *pwidget)
Definition widget.h:291
set_wstate
void set_wstate(struct widget *pwidget, enum widget_state state)
Definition widget_core.c:36
WF_FREE_DATA
@ WF_FREE_DATA
Definition widget.h:78
WF_RESTORE_BACKGROUND
@ WF_RESTORE_BACKGROUND
Definition widget.h:85
WF_DRAW_TEXT_LABEL_WITH_SPACE
@ WF_DRAW_TEXT_LABEL_WITH_SPACE
Definition widget.h:87
widget_redraw
static int widget_redraw(struct widget *pwidget)
Definition widget.h:276
widget_set_area
static void widget_set_area(struct widget *pwidget, SDL_Rect area)
Definition widget.h:261
create_iconlabel
struct widget * create_iconlabel(SDL_Surface *icon, struct gui_layer *pdest, utf8_str *pstr, Uint32 flags)
Definition widget_label.c:215
setup_vertical_scrollbar_area
void setup_vertical_scrollbar_area(struct scroll_bar *scroll, Sint16 start_x, Sint16 start_y, Uint16 height, bool swap_start_x)
Definition widget_scrollbar.c:572
create_vertical_scrollbar
Uint32 create_vertical_scrollbar(struct advanced_dialog *dlg, Uint8 step, Uint8 active, bool create_scrollbar, bool create_buttons)
Definition widget_scrollbar.c:491
hide_scrollbar
#define hide_scrollbar(scrollbar)
Definition widget_scrollbar.h:36
resize_window
int resize_window(struct widget *pwindow, SDL_Surface *bcgd, SDL_Color *pcolor, Uint16 new_w, Uint16 new_h)
Definition widget_window.c:255
create_window_skeleton
struct widget * create_window_skeleton(struct gui_layer *pdest, utf8_str *title, Uint32 flags)
Definition widget_window.c:180