1 /*
2 
3     Euchre - a free as in freedom and as in beer version of the
4              euchre card game
5 
6     Copyright 2002 C Nathan Buckles (nbuckles@bigfoot.com)
7 
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 
22 */
23 
24 #include <stdio.h>
25 #include <ctype.h>
26 
27 #include <Debug.hpp>
28 
29 #include "globals.hpp"
30 #include "support.hpp"
31 #include "GuiOptions.hpp"
32 
GuiOptions()33 GuiOptions::GuiOptions() {
34   itsShowTrickStats = 1;
35   itsShowRoundStats = 1;
36   itsShowGameStats  = 1;
37   itsCardBack       = 0;
38 }
39 
init()40 void GuiOptions::init() {
41   /* do base class init */
42   Options::init();
43 
44   /* now update gui */
45   updateGui();
46 }
47 
load()48 int GuiOptions::load() {
49   int ret = Options::load();
50   updateGui();
51   return ret;
52 }
53 
updateGui()54 void GuiOptions::updateGui() {
55   GtkWidget* temp;
56   char       widget_name[100];
57 
58   /* get skill and aggression settings */
59   for (int i = 0; i < Common::PLAYERS_PER_GAME; i++) {
60     if (i == Common::SOUTH) {
61       continue;
62     }
63 
64     char c = Common::getPlayerPositionChar((Common::PlayerPosition) i);
65     c = tolower(c);
66 
67     sprintf(widget_name, "%cskill", c);
68     temp = lookup_widget(prefdiag,  widget_name);
69 
70     GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(temp));
71     gtk_adjustment_set_value(adj, itsLevel[i]);
72 
73     sprintf(widget_name, "%cagg", c);
74     temp = lookup_widget(prefdiag,  widget_name);
75 
76     adj = gtk_range_get_adjustment(GTK_RANGE(temp));
77     gtk_adjustment_set_value(adj, itsAgg[i]);
78   }
79 
80   for (int i = Options::NODELAY; i < (Options::PAUSE+1); i++) {
81     sprintf(widget_name, "delay_style_%d", i);
82     temp = lookup_widget(prefdiag, widget_name);
83     if (itsDelayMode == (DelayMode) i) {
84       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(temp), true);
85     } else {
86       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(temp), false);
87     }
88   }
89 
90   temp = lookup_widget(prefdiag, "tricks_one_point");
91   gtk_spin_button_set_value(GTK_SPIN_BUTTON(temp), itsTricksOnePoint);
92 
93   temp = lookup_widget(prefdiag, "points_for_game");
94   gtk_spin_button_set_value(GTK_SPIN_BUTTON(temp), itsPointsForGame);
95 
96   temp = lookup_widget(prefdiag, "show_trick_stats");
97   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(temp), itsShowTrickStats);
98 
99   temp = lookup_widget(prefdiag, "show_round_stats");
100   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(temp), itsShowRoundStats);
101 
102   temp = lookup_widget(prefdiag, "show_game_stats");
103   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(temp), itsShowGameStats);
104 
105   temp = lookup_widget(prefdiag, "delay_between_plays");
106   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(temp), itsDelayBetweenPlays);
107 
108   temp = lookup_widget(prefdiag, "play_for_me");
109   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(temp), itsAutoPlay);
110 
111   temp = lookup_widget(prefdiag, "auto_deal_end");
112   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(temp), itsAutoDealEnd);
113 
114   temp = lookup_widget(prefdiag, "partner_loner");
115   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(temp), itsPartnerLoner);
116 
117   temp = lookup_widget(prefdiag, "stick_the_dealer");
118   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(temp), itsStickTheDealer);
119 
120   for (int i = 0; i < NUM_CARD_BACKS; i++) {
121     sprintf(widget_name, "cardback%d_button", i);
122     temp = lookup_widget(prefdiag, widget_name);
123     if (itsCardBack == i) {
124       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(temp), true);
125     } else {
126       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(temp), false);
127     }
128   }
129 }
130 
configure()131 void GuiOptions::configure() {
132   LOG("enter GuiOptions::configure()" << std::endl);
133 
134   GtkWidget* temp;
135   char       widget_name[100];
136 
137   /* get skill and aggression settings */
138   for (int i = 0; i < Common::PLAYERS_PER_GAME; i++) {
139     if (i == Common::SOUTH) {
140       continue;
141     }
142 
143     char c = Common::getPlayerPositionChar((Common::PlayerPosition) i);
144     c = tolower(c);
145 
146     sprintf(widget_name, "%cskill", c);
147     temp = lookup_widget(prefdiag,  widget_name);
148     GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(temp));
149     itsLevel[i] = (AILevel) adj->value;
150 
151     sprintf(widget_name, "%cagg", c);
152     temp = lookup_widget(prefdiag,  widget_name);
153     adj = gtk_range_get_adjustment(GTK_RANGE(temp));
154     itsAgg[i] = (AIAgg) adj->value;
155   }
156 
157   for (int i = Options::NODELAY; i < (Options::PAUSE+1); i++) {
158     sprintf(widget_name, "delay_style_%d", i);
159     temp = lookup_widget(prefdiag, widget_name);
160     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp))) {
161       itsDelayMode = (DelayMode) i;
162       break;
163     }
164   }
165 
166   temp = lookup_widget(prefdiag, "tricks_one_point");
167   itsTricksOnePoint = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(temp));
168 
169   temp = lookup_widget(prefdiag, "points_for_game");
170   itsPointsForGame = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(temp));
171 
172   temp = lookup_widget(prefdiag, "show_trick_stats");
173   itsShowTrickStats = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp));
174 
175   temp = lookup_widget(prefdiag, "show_round_stats");
176   itsShowRoundStats = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp));
177 
178   temp = lookup_widget(prefdiag, "show_game_stats");
179   itsShowGameStats = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp));
180 
181   temp = lookup_widget(prefdiag, "delay_between_plays");
182   itsDelayBetweenPlays = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp));
183 
184   temp = lookup_widget(prefdiag, "play_for_me");
185   itsAutoPlay = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp));
186 
187   temp = lookup_widget(prefdiag, "auto_deal_end");
188   itsAutoDealEnd = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp));
189 
190   temp = lookup_widget(prefdiag, "partner_loner");
191   itsPartnerLoner = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp));
192 
193   temp = lookup_widget(prefdiag, "stick_the_dealer");
194   itsStickTheDealer = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp));
195 
196   for (int i = 0; i < NUM_CARD_BACKS; i++) {
197     sprintf(widget_name, "cardback%d_button", i);
198     temp = lookup_widget(prefdiag, widget_name);
199     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp))) {
200       itsCardBack = i;
201       break;
202     }
203   }
204 
205   /* save off the new settings so they will be automatically restored
206      the next time we start up */
207   save();
208 
209   LOG("exit GuiOptions::configure()" << std::endl);
210 }
211 
writeOutOptions(std::ofstream & out)212 int GuiOptions::writeOutOptions(std::ofstream& out) {
213   Options::writeOutOptions(out);
214 
215   out << itsShowTrickStats << ' ' << itsShowRoundStats << ' '
216       << itsShowGameStats << ' ' << itsCardBack << std::endl;
217   return 0;
218 }
219 
readInOptions(std::ifstream & in)220 int GuiOptions::readInOptions(std::ifstream& in) {
221   Options::readInOptions(in);
222 
223   if (itsVersion == (int) CURRENT_VERSION) {
224     LOG("good version" << std::endl);
225     in >> itsShowTrickStats >> itsShowRoundStats
226        >> itsShowGameStats >> itsCardBack;
227   } else {
228     LOG("bad version" << std::endl);
229     in >> itsShowTrickStats >> itsShowRoundStats
230        >> itsShowGameStats;
231   }
232 
233   LOG("itsCardBack is " << itsCardBack << std::endl);
234   return 0;
235 }
236 
get()237 GuiOptions* GuiOptions::get() {
238   return (GuiOptions*) itsOptions;
239 }
240 
set(GuiOptions * opt)241 void GuiOptions::set(GuiOptions* opt) {
242   Options::set(opt);
243 }
244