Freeciv-3.3: /fast/freeciv/nightly/main/client/gui-gtk-5.0/finddlg.c Source File

Freeciv-3.3

Loading...
Searching...
No Matches
finddlg.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#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include <gtk/gtk.h>
23
24/* utility */
25#include "fcintl.h"
26#include "log.h"
27
28/* common */
29#include "game.h"
30#include "player.h"
31
32/* client */
33#include "options.h"
34
35/* client/gui-gtk-5.0 */
36#include "dialogs.h"
37#include "gui_main.h"
38#include "gui_stuff.h"
39#include "mapview.h"
40
41#include "finddlg.h"
42
43 static struct gui_dialog *find_dialog_shell;
44 static GtkWidget *find_view;
45
46static void update_find_dialog(GtkListStore *store);
47
48static void find_response(struct gui_dialog *dlg, int response, gpointer data);
49static void find_destroy_callback(GtkWidget *w, gpointer data);
50static void find_selection_callback(GtkTreeSelection *selection,
51 GtkTreeModel *model);
52
53 static struct tile *pos;
54
55/**********************************************************************/
58 void popup_find_dialog(void)
59{
60 if (!find_dialog_shell) {
61 GtkWidget *label;
62 GtkWidget *sw;
63 GtkListStore *store;
64 GtkTreeSelection *selection;
65 GtkCellRenderer *renderer;
66 GtkTreeViewColumn *column;
67
68 pos = get_center_tile_mapcanvas();
69
70 gui_dialog_new(&find_dialog_shell, GTK_NOTEBOOK(bottom_notebook), NULL,
71 TRUE);
72 gui_dialog_set_title(find_dialog_shell, _("Find City"));
73 gui_dialog_set_default_size(find_dialog_shell, -1, 240);
74
75 gui_dialog_add_button(find_dialog_shell, "edit-find", _("_Find"),
76 GTK_RESPONSE_ACCEPT);
77 gui_dialog_add_button(find_dialog_shell, NULL, _("_Cancel"),
78 GTK_RESPONSE_CANCEL);
79
80 gui_dialog_response_set_callback(find_dialog_shell, find_response);
81
82 g_signal_connect(find_dialog_shell->grid, "destroy",
83 G_CALLBACK(find_destroy_callback), NULL);
84
85 store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
86 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store),
87 0, GTK_SORT_ASCENDING);
88
89 find_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
90 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(find_view));
91 g_object_unref(store);
92 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(find_view));
93 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(find_view), FALSE);
94
95 renderer = gtk_cell_renderer_text_new();
96 column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
97 "text", 0, NULL);
98 gtk_tree_view_column_set_sort_order(column, GTK_SORT_ASCENDING);
99 gtk_tree_view_append_column(GTK_TREE_VIEW(find_view), column);
100
101 sw = gtk_scrolled_window_new();
102 gtk_widget_set_margin_bottom(sw, 2);
103 gtk_widget_set_margin_end(sw, 2);
104 gtk_widget_set_margin_start(sw, 2);
105 gtk_widget_set_margin_top(sw, 2);
106 gtk_scrolled_window_set_has_frame(GTK_SCROLLED_WINDOW(sw), TRUE);
107 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
108 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
109 gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(sw), find_view);
110
111 gtk_widget_set_hexpand(GTK_WIDGET(find_view), TRUE);
112 gtk_widget_set_vexpand(GTK_WIDGET(find_view), TRUE);
113
114 label = g_object_new(GTK_TYPE_LABEL,
115 "use-underline", TRUE,
116 "mnemonic-widget", find_view,
117 "label", _("Ci_ties:"),
118 "xalign", 0.0, "yalign", 0.5, NULL);
119 gui_dialog_add_content_widget(find_dialog_shell, label);
120 gui_dialog_add_content_widget(find_dialog_shell, sw);
121
122 g_signal_connect(selection, "changed",
123 G_CALLBACK(find_selection_callback), store);
124
125 update_find_dialog(store);
126 gtk_tree_view_focus(GTK_TREE_VIEW(find_view));
127
128 gui_dialog_show_all(find_dialog_shell);
129 }
130
131 gui_dialog_raise(find_dialog_shell);
132}
133
134/**********************************************************************/
137 static void update_find_dialog(GtkListStore *store)
138{
139 GtkTreeIter it;
140
141 gtk_list_store_clear(store);
142
143 players_iterate(pplayer) {
144 city_list_iterate(pplayer->cities, pcity) {
145 GValue value = { 0, };
146
147 gtk_list_store_append(store, &it);
148
149 g_value_init(&value, G_TYPE_STRING);
150 g_value_set_static_string(&value, city_name_get(pcity));
151 gtk_list_store_set_value(store, &it, 0, &value);
152 g_value_unset(&value);
153
154 gtk_list_store_set(store, &it, 1, pcity, -1);
155 } city_list_iterate_end;
156 } players_iterate_end;
157}
158
159/**********************************************************************/
162 static void find_response(struct gui_dialog *dlg, int response, gpointer data)
163{
164 if (response == GTK_RESPONSE_ACCEPT) {
165 GtkTreeSelection *selection;
166 GtkTreeModel *model;
167 GtkTreeIter it;
168
169 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(find_view));
170
171 if (gtk_tree_selection_get_selected(selection, &model, &it)) {
172 struct city *pcity;
173
174 gtk_tree_model_get(model, &it, 1, &pcity, -1);
175
176 if (pcity) {
177 pos = pcity->tile;
178 }
179 }
180 }
181
182 gui_dialog_destroy(dlg);
183}
184
185/**********************************************************************/
188 static void find_destroy_callback(GtkWidget *w, gpointer data)
189{
190 can_slide = FALSE;
191 center_tile_mapcanvas(pos);
192 can_slide = TRUE;
193}
194
195/**********************************************************************/
198 static void find_selection_callback(GtkTreeSelection *selection,
199 GtkTreeModel *model)
200{
201 GtkTreeIter it;
202 struct city *pcity;
203
204 if (!gtk_tree_selection_get_selected(selection, NULL, &it))
205 return;
206
207 gtk_tree_model_get(model, &it, 1, &pcity, -1);
208
209 if (pcity) {
210 can_slide = FALSE;
211 center_tile_mapcanvas(pcity->tile);
212 can_slide = TRUE;
213 }
214}
city_name_get
const char * city_name_get(const struct city *pcity)
Definition city.c:1133
city_list_iterate
#define city_list_iterate(citylist, pcity)
Definition city.h:508
city_list_iterate_end
#define city_list_iterate_end
Definition city.h:510
incite_cost
char * incite_cost
Definition comments.c:74
fcintl.h
_
#define _(String)
Definition fcintl.h:67
game.h
find_dialog_shell
static struct gui_dialog * find_dialog_shell
Definition finddlg.c:43
find_response
static void find_response(struct gui_dialog *dlg, int response, gpointer data)
Definition finddlg.c:162
find_view
static GtkWidget * find_view
Definition finddlg.c:44
popup_find_dialog
void popup_find_dialog(void)
Definition finddlg.c:58
update_find_dialog
static void update_find_dialog(GtkListStore *store)
Definition finddlg.c:137
find_destroy_callback
static void find_destroy_callback(GtkWidget *w, gpointer data)
Definition finddlg.c:187
find_selection_callback
static void find_selection_callback(GtkTreeSelection *selection, GtkTreeModel *model)
Definition finddlg.c:197
pos
static struct tile * pos
Definition finddlg.c:53
bottom_notebook
GtkWidget * bottom_notebook
Definition gui_main.c:129
gtk_tree_view_focus
void gtk_tree_view_focus(GtkTreeView *view)
Definition gui_stuff.c:236
gui_dialog_destroy
void gui_dialog_destroy(struct gui_dialog *dlg)
Definition gui_stuff.c:954
gui_dialog_raise
void gui_dialog_raise(struct gui_dialog *dlg)
Definition gui_stuff.c:865
gui_dialog_new
void gui_dialog_new(struct gui_dialog **pdlg, GtkNotebook *notebook, gpointer user_data, bool check_top)
Definition gui_stuff.c:517
gui_dialog_response_set_callback
void gui_dialog_response_set_callback(struct gui_dialog *dlg, GUI_DIALOG_RESPONSE_FUN fun)
Definition gui_stuff.c:988
gui_dialog_show_all
void gui_dialog_show_all(struct gui_dialog *dlg)
Definition gui_stuff.c:795
gui_dialog_set_title
void gui_dialog_set_title(struct gui_dialog *dlg, const char *title)
Definition gui_stuff.c:935
gui_dialog_set_default_size
void gui_dialog_set_default_size(struct gui_dialog *dlg, int width, int height)
Definition gui_stuff.c:919
gui_dialog_add_button
GtkWidget * gui_dialog_add_button(struct gui_dialog *dlg, const char *icon_name, const char *text, int response)
Definition gui_stuff.c:706
gui_dialog_add_content_widget
void gui_dialog_add_content_widget(struct gui_dialog *dlg, GtkWidget *wdg)
Definition gui_stuff.c:1105
gui_stuff.h
dialogs.h
finddlg.h
gui_main.h
mapview.h
log.h
get_center_tile_mapcanvas
struct tile * get_center_tile_mapcanvas(void)
Definition mapview_common.c:1227
center_tile_mapcanvas
void center_tile_mapcanvas(const struct tile *ptile)
Definition mapview_common.c:1237
can_slide
bool can_slide
Definition mapview_common.c:76
options.h
player.h
players_iterate_end
#define players_iterate_end
Definition player.h:539
players_iterate
#define players_iterate(_pplayer)
Definition player.h:534
city
Definition city.h:320
city::tile
struct tile * tile
Definition city.h:322
gui_dialog
Definition gui_stuff.h:70
gui_dialog::grid
GtkWidget * grid
Definition gui_stuff.h:71
tile
Definition tile.h:50
tile::label
char * label
Definition tile.h:66
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47