Freeciv-3.2
Loading...
Searching...
No Matches
cma_fe.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2001 - R. Falke, M. Kaufman
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 <gdk/gdkkeysyms.h>
19
20/* utility */
21#include "fcintl.h"
22#include "log.h"
23#include "mem.h"
24#include "support.h"
25
26/* common */
27#include "events.h"
28#include "game.h"
29
30/* client */
31#include "chatline_g.h"
32#include "citydlg_g.h"
33#include "client_main.h"
34#include "cma_fec.h"
35#include "messagewin_g.h"
36#include "options.h"
37
38/* client/gui-gtk-4.0 */
39#include "cityrep.h"
40#include "dialogs.h"
41#include "gui_main.h"
42#include "gui_stuff.h"
43#include "helpdlg.h"
44#include "inputdlg.h"
45
46#include "cma_fe.h"
47
48#define BUFFER_SIZE 64
49
50#define SPECLIST_TAG dialog
51#define SPECLIST_TYPE struct cma_dialog
52#include "speclist.h"
53
54#define dialog_list_iterate(dialoglist, pdialog) \
55 TYPED_LIST_ITERATE(struct cma_dialog, dialoglist, pdialog)
56#define dialog_list_iterate_end LIST_ITERATE_END
57
58static struct dialog_list *dialog_list;
59
60static int allow_refreshes = 1;
61
62static struct cma_dialog *get_cma_dialog(struct city *pcity);
63
64static void update_cma_preset_list(struct cma_dialog *pdialog);
65
68 GdkModifierType state, gpointer data);
69static void cma_del_preset_callback(GtkWidget *w, gpointer data);
70static void cma_preset_remove(struct cma_dialog *pdialog, int preset_index);
71static void cma_preset_remove_response(GtkWidget *w, gint response,
72 gpointer data);
73
74static void cma_add_preset_callback(GtkWidget *w, gpointer data);
75static void cma_preset_add_popup_callback(gpointer data, gint response,
76 const char *input);
77
78static void cma_active_callback(GtkWidget *w, gpointer data);
81
82static void hscale_changed(GtkWidget *get, gpointer data);
83static void set_hscales(const struct cm_parameter *const parameter,
84 struct cma_dialog *pdialog);
85
86/**********************************************************************/
89void cma_fe_init(void)
90{
92}
93
94/**********************************************************************/
101
102/**********************************************************************/
106{
107 struct cma_dialog *pdialog = get_cma_dialog(pcity);
108
109 if (pdialog == NULL) {
110 /* A city which is being investigated doesn't contain cma dialog */
111 return;
112 }
113
115 pdialog->shell);
116}
117
118/**********************************************************************/
122{
123 struct cma_dialog *pdialog = (struct cma_dialog *) data;
124
126 free(pdialog);
127}
128
129/**********************************************************************/
133{
135 if (pdialog->pcity == pcity) {
136 return pdialog;
137 }
139
140 return NULL;
141}
142
143/**********************************************************************/
147 double x, double y)
148{
150 struct cma_dialog *pdialog
151 = (struct cma_dialog *) g_object_get_data(G_OBJECT(widget), "dialog");
152 GtkTreePath *path;
154
156 x, y, &path, &column, NULL, NULL)) {
157 if (n_press == 1) {
159 path, column, pdialog);
160 } else if (n_press == 2) {
161 struct cm_parameter param;
162
163 cmafec_get_fe_parameter(pdialog->pcity, &param);
164 cma_put_city_under_agent(pdialog->pcity, &param);
165 refresh_city_dialog(pdialog->pcity);
166 }
167 }
168 gtk_tree_path_free(path);
169
170 return FALSE;
171}
172
173/**********************************************************************/
180
181/**********************************************************************/
185 GtkTreeModel *model, GtkTreeIter *it, gpointer data)
186{
187 struct cma_dialog *pdialog = (struct cma_dialog *) data;
188 char *s1;
189 const char *s2;
190 int i1, i2;
191 struct cm_parameter param;
192 GtkTreePath *path;
193
194 gtk_tree_model_get(model, it, 0, &s1, -1);
195 if (s1 == NULL) {
196 return;
197 }
198 path = gtk_tree_model_get_path(model, it);
199 i1 = gtk_tree_path_get_indices(path)[0];
200 gtk_tree_path_free(path);
201
202 cmafec_get_fe_parameter(pdialog->pcity, &param);
203 s2 = cmafec_get_short_descr(&param);
205
206 if (!strcmp(s1, s2) && i1 == i2) {
208 "weight", PANGO_WEIGHT_BOLD, NULL);
209 } else {
211 "weight", PANGO_WEIGHT_NORMAL, NULL);
212 }
213
214 g_free(s1);
215}
216
217/**********************************************************************/
221{
222 struct cma_dialog *pdialog;
223 struct cm_parameter param;
224 GtkWidget *frame, *page, *hbox, *vbox, *label, *table;
225 GtkWidget *sw, *hscale, *button;
226 GtkListStore *store;
232 int shell_row = 0;
234
235 cmafec_get_fe_parameter(pcity, &param);
236 pdialog = fc_malloc(sizeof(struct cma_dialog));
237 pdialog->pcity = pcity;
238 pdialog->shell = gtk_grid_new();
243 gtk_widget_set_margin_end(pdialog->shell, 8);
244 gtk_widget_set_margin_top(pdialog->shell, 8);
246 g_signal_connect(pdialog->shell, "destroy",
248
250 gtk_grid_attach(GTK_GRID(pdialog->shell), page, 0, shell_row++, 1, 1);
251
254 gtk_box_append(GTK_BOX(page), vbox);
255
260
262 pdialog->store = store;
263
267 g_object_unref(store);
269 pdialog->preset_list = view;
271
272 g_object_set_data(G_OBJECT(pdialog->preset_list), "dialog", pdialog);
274 g_signal_connect(controller, "pressed",
277
279 _("For information on\n"
280 "the citizen governor and governor presets,\n"
281 "including sample presets,\n"
282 "see README.governor."));
283
286 "text", 0, NULL);
289 pdialog, NULL);
290
292 "use-underline", TRUE,
293 "mnemonic-widget", view,
294 "label", _("Prese_ts:"),
295 "xalign", 0.0, "yalign", 0.5, NULL);
296 gtk_box_append(GTK_BOX(vbox), label);
297
299 gtk_box_append(GTK_BOX(vbox), sw);
300
301 g_signal_connect(view, "row_activated",
304 g_signal_connect(controller, "key-pressed",
307
310
311 button = icon_label_button_new("document-new", _("Ne_w"));
312 gtk_box_append(GTK_BOX(hbox), button);
313 g_signal_connect(button, "clicked",
315 pdialog->add_preset_command = button;
316
317 pdialog->del_preset_command = icon_label_button_new("edit-delete",
318 _("_Delete"));
320 g_signal_connect(pdialog->del_preset_command, "clicked",
322
323 /* The right-hand side */
329 gtk_box_append(GTK_BOX(page), vbox);
330
331 /* Result */
332 if (!tiny) {
333 frame = gtk_frame_new(_("Results"));
336 gtk_box_append(GTK_BOX(vbox), frame);
337
338 pdialog->result_label =
339 gtk_label_new("food\n prod\n trade\n\n people\n grow\n prod\n name");
340 gtk_widget_set_name(pdialog->result_label, "city_label");
343 } else {
344 pdialog->result_label = NULL;
345 }
346
347 /* Minimal Surplus and Factor */
354
355 label = gtk_label_new(_("Minimal Surplus"));
358 gtk_grid_attach(GTK_GRID(table), label, 1, 0, 1, 1);
359 label = gtk_label_new(_("Factor"));
362 gtk_grid_attach(GTK_GRID(table), label, 2, 0, 1, 1);
363
366 gtk_grid_attach(GTK_GRID(table), label, 0, i + 1, 1, 1);
369
370 pdialog->minimal_surplus[i] = hscale =
377 if (layout != NULL) {
380 }
381
382 gtk_grid_attach(GTK_GRID(table), hscale, 1, i + 1, 1, 1);
385
387 "value-changed",
388 G_CALLBACK(hscale_changed), pdialog);
389
390 pdialog->factor[i] = hscale
395 if (layout != NULL) {
397 } else {
398 layout_width = 20;
399 }
401
402 gtk_grid_attach(GTK_GRID(table), hscale, 2, i + 1, 1, 1);
405
406 g_signal_connect(pdialog->factor[i], "value-changed",
407 G_CALLBACK(hscale_changed), pdialog);
409
410 /* Happy Surplus and Factor */
411 label = gtk_label_new(_("Celebrate"));
412 gtk_grid_attach(GTK_GRID(table), label, 0, O_LAST + 1, 1, 1);
415
419 FALSE);
420 gtk_grid_attach(GTK_GRID(table), pdialog->happy_button, 1, O_LAST + 1, 1, 1);
421
422 g_signal_connect(pdialog->happy_button, "toggled",
423 G_CALLBACK(hscale_changed), pdialog);
424
425 pdialog->factor[O_LAST] = hscale
430 if (layout != NULL) {
433 }
434
435 gtk_grid_attach(GTK_GRID(table), hscale, 2, O_LAST + 1, 1, 1);
438
440 "value-changed",
441 G_CALLBACK(hscale_changed), pdialog);
442
443 /* Maximize Growth */
444 label = gtk_label_new(_("Maximize growth"));
445 gtk_grid_attach(GTK_GRID(table), label, 0, O_LAST + 2, 1, 1);
448
452 FALSE);
453 gtk_grid_attach(GTK_GRID(table), pdialog->growth_button, 1, O_LAST + 2, 1, 1);
454
455 g_signal_connect(pdialog->growth_button, "toggled",
456 G_CALLBACK(hscale_changed), pdialog);
457
458 /* Buttons */
461
462 button = icon_label_button_new("help-browser", _("Help"));
463 g_signal_connect(button, "clicked",
465 gtk_box_append(GTK_BOX(hbox), button);
466
469 gtk_widget_set_name(pdialog->active_command, "comment_label");
471
473
475
476 update_cma_preset_list(pdialog);
477
479
480 /* Refresh is done in refresh_city_dialog() */
481
482 return pdialog;
483}
484
485/**********************************************************************/
488void refresh_cma_dialog(struct city *pcity, enum cma_refresh refresh)
489{
490 struct cm_result *result = cm_result_new(pcity);
491 struct cm_parameter param;
492 struct cma_dialog *pdialog = get_cma_dialog(pcity);
494
496
497 if (pdialog->result_label != NULL) {
498 /* fill in result label */
501 cmafec_get_result_descr(pcity, result, &param));
502 }
503
504 /* if called from a hscale, we _don't_ want to do this */
505 if (refresh != DONT_REFRESH_HSCALES) {
506 set_hscales(&param, pdialog);
507 }
508
510
512
517 controlled);
518 g_signal_connect(pdialog->active_command, "clicked",
520
521 if (controlled) {
523 _("Governor Enabl_ed"));
524 } else {
526 _("Governor Disabl_ed"));
527 }
528
529 if (pdialog->result_label != NULL) {
531 }
532
533 cm_result_destroy(result);
534}
535
536/**********************************************************************/
539static void update_cma_preset_list(struct cma_dialog *pdialog)
540{
541 char buf[BUFFER_SIZE];
542 GtkTreeIter it;
543 int i;
544
545 /* Fill preset list */
546 gtk_list_store_clear(pdialog->store);
547
548 /* Append the presets */
549 if (cmafec_preset_num()) {
550 for (i = 0; i < cmafec_preset_num(); i++) {
552 gtk_list_store_append(pdialog->store, &it);
553 gtk_list_store_set(pdialog->store, &it, 0, buf, -1);
554 }
555 }
556}
557
558/**********************************************************************/
563{
564 struct cma_dialog *pdialog = (struct cma_dialog *) data;
565 int preset_index;
566 const struct cm_parameter *pparam;
567
569
571
572 /* Save the change */
574
575 if (cma_is_city_under_agent(pdialog->pcity, NULL)) {
576 cma_release_city(pdialog->pcity);
578 }
579 refresh_city_dialog(pdialog->pcity);
580}
581
582/**********************************************************************/
586{
587 struct cma_dialog *pdialog = (struct cma_dialog *) data;
588 const char *default_name;
590 int index;
591
592 if ((index = gtk_tree_selection_get_row(pdialog->selection)) != -1) {
594 } else {
595 default_name = _("new preset");
596 }
597
599 _("Name new preset"),
600 _("What should we name the preset?"),
603}
604
605/**********************************************************************/
608static void cma_preset_add_popup_callback(gpointer data, gint response,
609 const char *input)
610{
611 struct cma_dialog *pdialog = (struct cma_dialog *) data;
612
613 if (pdialog) {
614 if (response == GTK_RESPONSE_OK) {
615 struct cm_parameter param;
616
617 cmafec_get_fe_parameter(pdialog->pcity, &param);
618 cmafec_preset_add(input, &param);
619 update_cma_preset_list(pdialog);
621 /* If this or other cities have this set as "custom" */
623 } /* else CANCEL or DELETE_EVENT */
624
625 pdialog->name_shell = NULL;
626 }
627}
628
629/**********************************************************************/
634 GdkModifierType state, gpointer data)
635{
636 struct cma_dialog *pdialog = (struct cma_dialog *) data;
637 int index;
638
639 if ((index = gtk_tree_selection_get_row(pdialog->selection)) == -1) {
640 return FALSE;
641 }
642
643 switch (keyval) {
644 case GDK_KEY_Delete:
645 cma_preset_remove(pdialog, index);
646 break;
647 case GDK_KEY_Insert:
649 break;
650 default:
651 return FALSE;
652 }
653
654 return TRUE;
655}
656
657/**********************************************************************/
661{
662 struct cma_dialog *pdialog = (struct cma_dialog *) data;
663 int index;
664
665 if ((index = gtk_tree_selection_get_row(pdialog->selection)) == -1) {
666 return;
667 }
668
669 cma_preset_remove(pdialog, index);
670}
671
672/**********************************************************************/
696
697/**********************************************************************/
701 gpointer data)
702{
703 struct cma_dialog *pdialog = (struct cma_dialog *) data;
704
705 if (response == GTK_RESPONSE_YES) {
706 cmafec_preset_remove(pdialog->id);
707 pdialog->id = -1;
708 update_cma_preset_list(pdialog);
710 /* if this or other cities have this set, reset to "custom" */
712 }
714
715 pdialog->preset_remove_shell = NULL;
716}
717
718/**********************************************************************/
722{
723 struct cma_dialog *pdialog = (struct cma_dialog *) data;
724
725 if (cma_is_city_under_agent(pdialog->pcity, NULL)) {
726 cma_release_city(pdialog->pcity);
727 } else {
728 struct cm_parameter param;
729
730 cmafec_get_fe_parameter(pdialog->pcity, &param);
731 cma_put_city_under_agent(pdialog->pcity, &param);
732 }
733 refresh_city_dialog(pdialog->pcity);
734}
735
736/**********************************************************************/
740static void set_hscales(const struct cm_parameter *const parameter,
741 struct cma_dialog *pdialog)
742{
743 allow_refreshes = 0;
746 parameter->minimal_surplus[i]);
747 gtk_range_set_value(GTK_RANGE(pdialog->factor[i]), parameter->factor[i]);
750 parameter->require_happy);
752 parameter->max_growth);
754 parameter->happy_factor);
755 allow_refreshes = 1;
756}
757
758/**********************************************************************/
761static void hscale_changed(GtkWidget *get, gpointer data)
762{
763 struct cma_dialog *pdialog = (struct cma_dialog *) data;
764 struct cm_parameter param;
765
766 if (!allow_refreshes) {
767 return;
768 }
769
770 cmafec_get_fe_parameter(pdialog->pcity, &param);
772 param.minimal_surplus[i] =
774 param.factor[i] =
775 (int) (gtk_range_get_value(GTK_RANGE(pdialog->factor[i])));
777 param.require_happy =
779 param.max_growth =
781 param.happy_factor =
783
784 /* Save the change */
785 cmafec_set_fe_parameter(pdialog->pcity, &param);
786
787 /* Refreshes the cma */
788 if (cma_is_city_under_agent(pdialog->pcity, NULL)) {
789 cma_release_city(pdialog->pcity);
790 cma_put_city_under_agent(pdialog->pcity, &param);
791 refresh_city_dialog(pdialog->pcity);
792 } else {
794 }
795}
const char * get_output_name(Output_type_id output)
Definition city.c:629
#define output_type_iterate(output)
Definition city.h:845
#define output_type_iterate_end
Definition city.h:851
void refresh_city_dialog(struct city *pcity)
void city_report_dialog_update(void)
bool can_client_issue_orders(void)
struct cm_result * cm_result_new(struct city *pcity)
Definition cm.c:345
void cm_result_from_main_map(struct cm_result *result, const struct city *pcity)
Definition cm.c:2261
void cm_result_destroy(struct cm_result *result)
Definition cm.c:368
bool cma_is_city_under_agent(const struct city *pcity, struct cm_parameter *parameter)
Definition cma_core.c:552
void cma_put_city_under_agent(struct city *pcity, const struct cm_parameter *const parameter)
Definition cma_core.c:524
void cma_release_city(struct city *pcity)
Definition cma_core.c:542
const char * cmafec_get_short_descr(const struct cm_parameter *const parameter)
Definition cma_fec.c:236
void cmafec_set_fe_parameter(struct city *pcity, const struct cm_parameter *const parameter)
Definition cma_fec.c:105
char * cmafec_preset_get_descr(int idx)
Definition cma_fec.c:169
const char * cmafec_get_result_descr(struct city *pcity, const struct cm_result *result, const struct cm_parameter *const parameter)
Definition cma_fec.c:322
const struct cm_parameter * cmafec_preset_get_parameter(int idx)
Definition cma_fec.c:182
void cmafec_preset_add(const char *descr_name, struct cm_parameter *pparam)
Definition cma_fec.c:136
int cmafec_preset_get_index_of_parameter(const struct cm_parameter *const parameter)
Definition cma_fec.c:196
void cmafec_preset_remove(int idx)
Definition cma_fec.c:153
int cmafec_preset_num(void)
Definition cma_fec.c:213
void cmafec_get_fe_parameter(struct city *pcity, struct cm_parameter *dest)
Definition cma_fec.c:115
char * incite_cost
Definition comments.c:75
@ O_LAST
Definition fc_types.h:101
#define _(String)
Definition fcintl.h:67
static PangoLayout * layout
Definition canvas.c:325
static gboolean button_press_callback(GtkTreeView *view, GdkEventButton *ev, gpointer data)
Definition cma_fe.c:143
static void cma_preset_remove(struct cma_dialog *pdialog, int preset_index)
Definition cma_fe.c:659
void close_cma_dialog(struct city *pcity)
Definition cma_fe.c:104
struct cma_dialog * create_cma_dialog(struct city *pcity, bool tiny)
Definition cma_fe.c:214
void refresh_cma_dialog(struct city *pcity, enum cma_refresh refresh)
Definition cma_fe.c:471
static void cma_del_preset_callback(GtkWidget *w, gpointer data)
Definition cma_fe.c:644
static void cma_dialog_destroy_callback(GtkWidget *w, gpointer data)
Definition cma_fe.c:118
void cma_fe_init(void)
Definition cma_fe.c:88
static void cma_add_preset_callback(GtkWidget *w, gpointer data)
Definition cma_fe.c:568
static void help_callback(GtkWidget *w, gpointer data)
Definition cma_fe.c:170
static void update_cma_preset_list(struct cma_dialog *pdialog)
Definition cma_fe.c:522
#define BUFFER_SIZE
Definition cma_fe.c:48
#define dialog_list_iterate_end
Definition cma_fe.c:56
static void hscale_changed(GtkWidget *get, gpointer data)
Definition cma_fe.c:745
static void cell_data_func(GtkTreeViewColumn *col, GtkCellRenderer *cell, GtkTreeModel *model, GtkTreeIter *it, gpointer data)
Definition cma_fe.c:178
static void cma_preset_add_popup_callback(gpointer data, gint response, const char *input)
Definition cma_fe.c:591
static struct cma_dialog * get_cma_dialog(struct city *pcity)
Definition cma_fe.c:129
#define dialog_list_iterate(dialoglist, pdialog)
Definition cma_fe.c:54
static struct dialog_list * dialog_list
Definition cma_fe.c:58
static void cma_preset_remove_response(GtkWidget *w, gint response, gpointer data)
Definition cma_fe.c:684
static void cma_activate_preset_callback(GtkTreeView *view, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
Definition cma_fe.c:544
static void cma_active_callback(GtkWidget *w, gpointer data)
Definition cma_fe.c:705
static void set_hscales(const struct cm_parameter *const parameter, struct cma_dialog *pdialog)
Definition cma_fe.c:724
void cma_fe_done(void)
Definition cma_fe.c:96
static int allow_refreshes
Definition cma_fe.c:60
cma_refresh
Definition cma_fe.h:25
@ DONT_REFRESH_HSCALES
Definition cma_fe.h:28
#define GUI_GTK_OPTION(optname)
Definition gui_main.h:25
void gtk_tree_view_focus(GtkTreeView *view)
Definition gui_stuff.c:236
gint gtk_tree_selection_get_row(GtkTreeSelection *selection)
Definition gui_stuff.c:215
GtkWidget * icon_label_button_new(const gchar *icon_name, const gchar *label_text)
Definition gui_stuff.c:76
void setup_dialog(GtkWidget *shell, GtkWidget *parent)
Definition gui_stuff.c:287
void popup_help_dialog_string(const char *item)
Definition helpdlg.c:212
GtkWidget * input_dialog_create(GtkWindow *parent, const char *dialogname, const char *text, const char *postinputtest, input_dialog_callback_t response_callback, gpointer response_cli_data)
Definition inputdlg.c:66
static gboolean cma_preset_key_pressed(GtkEventControllerKey *controller, guint keyval, guint keycode, GdkModifierType state, gpointer data)
Definition cma_fe.c:632
#define HELP_CMA_ITEM
#define fc_malloc(sz)
Definition mem.h:34
struct sprite int int y
Definition sprite_g.h:31
struct sprite int x
Definition sprite_g.h:31
Definition city.h:320
int factor[O_LAST]
Definition cm.h:47
bool max_growth
Definition cm.h:42
bool require_happy
Definition cm.h:43
int minimal_surplus[O_LAST]
Definition cm.h:41
int happy_factor
Definition cm.h:48
Definition cm.h:52
GtkWidget * preset_list
Definition cma_fe.h:36
GtkWidget * active_command
Definition cma_fe.h:40
GtkWidget * factor[O_LAST+1]
Definition cma_fe.h:44
GtkWidget * shell
Definition cma_fe.h:33
GtkWidget * happy_button
Definition cma_fe.h:42
GtkWidget * del_preset_command
Definition cma_fe.h:39
GtkWidget * growth_button
Definition cma_fe.h:43
GtkWidget * result_label
Definition cma_fe.h:37
GtkWidget * add_preset_command
Definition cma_fe.h:38
GtkWidget * minimal_surplus[O_LAST]
Definition cma_fe.h:41
GtkListStore * store
Definition cma_fe.h:46
GtkWidget * name_shell
Definition cma_fe.h:34
GtkWidget * preset_remove_shell
Definition cma_fe.h:35
int id
Definition cma_fe.h:47
struct city * pcity
Definition cma_fe.h:32
GtkTreeSelection * selection
Definition cma_fe.h:45
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:791
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47