1 /* Pioneers - Implementation of the excellent Settlers of Catan board game.
2  *   Go buy a copy.
3  *
4  * Copyright (C) 1999 Dave Cole
5  * Copyright (C) 2003 Bas Wijnen <shevek@fmf.nl>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #include "config.h"
23 #include "frontend.h"
24 #include "resource-table.h"
25 
26 static struct {
27 	GtkWidget *dlg;
28 	GtkWidget *resource_widget;
29 	gint bank[NO_RESOURCE];
30 } plenty;
31 
amount_changed_cb(G_GNUC_UNUSED ResourceTable * rt,G_GNUC_UNUSED gpointer user_data)32 static void amount_changed_cb(G_GNUC_UNUSED ResourceTable * rt,
33 			      G_GNUC_UNUSED gpointer user_data)
34 {
35 	frontend_gui_update();
36 }
37 
plenty_resources(gint * resources)38 void plenty_resources(gint * resources)
39 {
40 	resource_table_get_amount(RESOURCETABLE(plenty.resource_widget),
41 				  resources);
42 }
43 
plenty_destroyed(G_GNUC_UNUSED GtkWidget * widget,G_GNUC_UNUSED gpointer data)44 static void plenty_destroyed(G_GNUC_UNUSED GtkWidget * widget,
45 			     G_GNUC_UNUSED gpointer data)
46 {
47 	if (callback_mode == MODE_PLENTY)
48 		plenty_create_dlg(NULL);
49 }
50 
plenty_create_dlg(const gint * bank)51 void plenty_create_dlg(const gint * bank)
52 {
53 	GtkWidget *dlg_vbox;
54 	GtkWidget *vbox;
55 	const char *str;
56 	gint r;
57 	gint total;
58 
59 	plenty.dlg = gtk_dialog_new_with_buttons(
60 							/* Dialog caption */
61 							_(""
62 							  "Year of Plenty"),
63 							GTK_WINDOW
64 							(app_window),
65 							GTK_DIALOG_DESTROY_WITH_PARENT,
66 							/* Button text */
67 							_("_OK"),
68 							GTK_RESPONSE_OK,
69 							NULL);
70 	g_signal_connect(G_OBJECT(plenty.dlg), "destroy",
71 			 G_CALLBACK(gtk_widget_destroyed), &plenty.dlg);
72 	gtk_widget_realize(plenty.dlg);
73 	/* Disable close */
74 	gdk_window_set_functions(gtk_widget_get_window(plenty.dlg),
75 				 GDK_FUNC_ALL | GDK_FUNC_CLOSE);
76 
77 	dlg_vbox = gtk_dialog_get_content_area(GTK_DIALOG(plenty.dlg));
78 	gtk_widget_show(dlg_vbox);
79 
80 	vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
81 	gtk_widget_show(vbox);
82 	gtk_box_pack_start(GTK_BOX(dlg_vbox), vbox, FALSE, TRUE, 0);
83 	gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
84 
85 	total = 0;
86 	for (r = 0; r < NO_RESOURCE; ++r) {
87 		if (bank != NULL)
88 			plenty.bank[r] = bank[r];
89 		total += plenty.bank[r];
90 	}
91 	if (total == 1)
92 		str = _("Please choose one resource from the bank");
93 	else if (total >= 2) {
94 		total = 2;
95 		str = _("Please choose two resources from the bank");
96 	} else
97 		str = _("The bank is empty");
98 	plenty.resource_widget =
99 	    resource_table_new(str, RESOURCE_TABLE_MORE_IN_HAND, TRUE);
100 	resource_table_set_total(RESOURCETABLE(plenty.resource_widget),
101 				 /* Text for total in year of plenty dialog */
102 				 _("Total resources"), total);
103 	resource_table_limit_bank(RESOURCETABLE(plenty.resource_widget),
104 				  TRUE);
105 	resource_table_set_bank(RESOURCETABLE(plenty.resource_widget),
106 				plenty.bank);
107 
108 	gtk_widget_show(plenty.resource_widget);
109 	gtk_box_pack_start(GTK_BOX(vbox), plenty.resource_widget, FALSE,
110 			   TRUE, 0);
111 	g_signal_connect(G_OBJECT(plenty.resource_widget), "change",
112 			 G_CALLBACK(amount_changed_cb), NULL);
113 
114 	frontend_gui_register(gui_get_dialog_button
115 			      (GTK_DIALOG(plenty.dlg), 0), GUI_PLENTY,
116 			      "clicked");
117 	/* This _must_ be after frontend_gui_register, otherwise the
118 	 * regeneration of the button happens before the destruction, which
119 	 * results in an incorrectly sensitive OK button. */
120 	g_signal_connect(gui_get_dialog_button(GTK_DIALOG(plenty.dlg), 0),
121 			 "destroy", G_CALLBACK(plenty_destroyed), NULL);
122 	gtk_widget_show(plenty.dlg);
123 	frontend_gui_update();
124 }
125 
plenty_destroy_dlg(void)126 void plenty_destroy_dlg(void)
127 {
128 	if (plenty.dlg == NULL)
129 		return;
130 	gtk_widget_destroy(plenty.dlg);
131 	plenty.dlg = NULL;
132 }
133 
plenty_can_activate(void)134 gboolean plenty_can_activate(void)
135 {
136 	return
137 	    resource_table_is_total_reached(RESOURCETABLE
138 					    (plenty.resource_widget));
139 }
140