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 
26 #include <Debug.hpp>
27 
28 #include "globals.hpp"
29 #include "support.hpp"
30 #include "GuiPlayer.hpp"
31 
GuiPlayer(Common::PlayerPosition myPos)32 GuiPlayer::GuiPlayer(Common::PlayerPosition myPos) : itsPlPos(myPos) {
33     char pixname[100];
34     switch (myPos) {
35     case Common::NORTH:
36         sprintf(pixname, "nplayedcard");
37         itsCardCoords[0] = 3;
38         itsCardCoords[1] = 6;
39         itsCardCoords[2] = 1;
40         itsCardCoords[3] = 2;
41 
42         itsSuitCoords[0] = 5;
43         itsSuitCoords[1] = 6;
44         itsSuitCoords[2] = 0;
45         itsSuitCoords[3] = 1;
46 
47         itsBidCoords[0] = 3;
48         itsBidCoords[1] = 4;
49         itsBidCoords[2] = 0;
50         itsBidCoords[3] = 1;
51 
52         itsMarkerCoords[0] = 1;
53         itsMarkerCoords[1] = 2;
54         itsMarkerCoords[2] = 0;
55         itsMarkerCoords[3] = 1;
56 
57         break;
58 
59     case Common::EAST:
60         sprintf(pixname, "eplayedcard");
61         itsCardCoords[0] = 6;
62         itsCardCoords[1] = 7;
63         itsCardCoords[2] = 2;
64         itsCardCoords[3] = 5;
65 
66         itsSuitCoords[0] = 7;
67         itsSuitCoords[1] = 8;
68         itsSuitCoords[2] = 4;
69         itsSuitCoords[3] = 5;
70 
71         itsBidCoords[0] = 7;
72         itsBidCoords[1] = 8;
73         itsBidCoords[2] = 2;
74         itsBidCoords[3] = 3;
75 
76         itsMarkerCoords[0] = 2;
77         itsMarkerCoords[1] = 3;
78         itsMarkerCoords[2] = 1;
79         itsMarkerCoords[3] = 2;
80         break;
81 
82     case Common::SOUTH:
83         sprintf(pixname, "splayedcard");
84         itsCardCoords[0] = 3;
85         itsCardCoords[1] = 6;
86         itsCardCoords[2] = 5;
87         itsCardCoords[3] = 6;
88 
89         itsSuitCoords[0] = 5;
90         itsSuitCoords[1] = 6;
91         itsSuitCoords[2] = 7;
92         itsSuitCoords[3] = 8;
93 
94         itsBidCoords[0] = 3;
95         itsBidCoords[1] = 4;
96         itsBidCoords[2] = 7;
97         itsBidCoords[3] = 8;
98 
99         itsMarkerCoords[0] = 1;
100         itsMarkerCoords[1] = 2;
101         itsMarkerCoords[2] = 2;
102         itsMarkerCoords[3] = 3;
103         break;
104 
105     case Common::WEST:
106         sprintf(pixname, "wplayedcard");
107         itsCardCoords[0] = 2;
108         itsCardCoords[1] = 3;
109         itsCardCoords[2] = 2;
110         itsCardCoords[3] = 5;
111 
112         itsSuitCoords[0] = 1;
113         itsSuitCoords[1] = 2;
114         itsSuitCoords[2] = 4;
115         itsSuitCoords[3] = 5;
116 
117         itsBidCoords[0] = 1;
118         itsBidCoords[1] = 2;
119         itsBidCoords[2] = 2;
120         itsBidCoords[3] = 3;
121 
122         itsMarkerCoords[0] = 0;
123         itsMarkerCoords[1] = 1;
124         itsMarkerCoords[2] = 1;
125         itsMarkerCoords[3] = 2;
126         break;
127 
128     default:
129         LOG("position " << myPos << " uh-oh!" << std::endl);
130         break;
131     }
132 
133     itsTable         = lookup_widget(mainwin, "table1");
134     itsMarkerTable   = lookup_widget(mainwin, "marker_table");
135     itsPixmap        = lookup_widget(mainwin, pixname);
136     itsOldPixmap     = itsPixmap;
137     itsBidSuitPixmap = NULL;
138     itsBidCallPixmap = NULL;
139     itsMarkerPixmap  = NULL;
140 
141     if (GTK_WIDGET(bidSuitEmptyPixmaps[itsPlPos])->parent != NULL) {
142         gtk_container_remove(GTK_CONTAINER(itsTable),
143                              bidSuitEmptyPixmaps[itsPlPos]);
144     }
145     gtk_table_attach(GTK_TABLE(itsTable), bidSuitEmptyPixmaps[itsPlPos],
146                      itsSuitCoords[0], itsSuitCoords[1],
147                      itsSuitCoords[2], itsSuitCoords[3],
148                      (GtkAttachOptions) (GTK_FILL),
149                      (GtkAttachOptions) (GTK_FILL), 0, 0);
150 
151     if (GTK_WIDGET(bidEmptyPixmaps[itsPlPos])->parent != NULL) {
152         gtk_container_remove(GTK_CONTAINER(itsTable), bidEmptyPixmaps[itsPlPos]);
153     }
154     gtk_table_attach(GTK_TABLE(itsTable), bidEmptyPixmaps[itsPlPos],
155                      itsBidCoords[0], itsBidCoords[1],
156                      itsBidCoords[2], itsBidCoords[3],
157                      (GtkAttachOptions) (GTK_FILL),
158                      (GtkAttachOptions) (GTK_FILL), 0, 0);
159 
160     LOG("about to remove marker for " << itsPlPos << std::endl);
161     if (GTK_WIDGET(emptyMarkers[itsPlPos])->parent != NULL) {
162         gtk_widget_ref(emptyMarkers[itsPlPos]);
163         gtk_container_remove(GTK_CONTAINER(itsMarkerTable), emptyMarkers[itsPlPos]);
164     }
165     gtk_table_attach(GTK_TABLE(itsMarkerTable), emptyMarkers[itsPlPos],
166                      itsMarkerCoords[0], itsMarkerCoords[1],
167                      itsMarkerCoords[2], itsMarkerCoords[3],
168                      (GtkAttachOptions) (GTK_FILL),
169                      (GtkAttachOptions) (GTK_FILL), 0, 0);
170 }
171 
~GuiPlayer()172 GuiPlayer::~GuiPlayer() {
173     if (itsPixmap != itsOldPixmap) {
174         setCardPixmap(itsOldPixmap);
175     }
176 
177     setBidSuitPixmap(NULL);
178     setMarker(NULL);
179 }
180 
setCardPixmap(GtkWidget * pixmap)181 void GuiPlayer::setCardPixmap(GtkWidget* pixmap) {
182     // don't repack the same thing
183     if (itsPixmap == pixmap) {
184         return;
185     }
186 
187 #if 0
188     // hack to keep our pixmaps from being deleted by 0 references
189     if (GTK_OBJECT(itsPixmap)->ref_count == 1) {
190         gtk_widget_ref(itsPixmap);
191     }
192 #endif
193 
194     // remove the old pixmap
195     gtk_container_remove(GTK_CONTAINER(itsTable), itsPixmap);
196 
197     // and add the new one
198     itsPixmap = pixmap;
199     gtk_table_attach(GTK_TABLE(itsTable), itsPixmap, itsCardCoords[0],
200                      itsCardCoords[1], itsCardCoords[2], itsCardCoords[3],
201                      (GtkAttachOptions) (GTK_FILL),
202                      (GtkAttachOptions) (GTK_FILL), 0, 0);
203 }
204 
setBidSuitPixmap(GtkWidget * pixmap)205 void GuiPlayer::setBidSuitPixmap(GtkWidget* pixmap) {
206     /* if NULL then remove the pixmap */
207     if (pixmap == NULL) {
208         if (itsBidSuitPixmap != NULL) {
209 #if 0
210             // hack to keep our pixmaps from being deleted by 0 references
211             if (GTK_OBJECT(itsBidSuitPixmap)->ref_count == 1) {
212                 gtk_widget_ref(itsBidSuitPixmap);
213             }
214 #endif
215 
216             // remove the bid pixmap
217             gtk_container_remove(GTK_CONTAINER(itsTable), itsBidSuitPixmap);
218 
219             // remember that it's gone
220             itsBidSuitPixmap = NULL;
221 
222             // fill the empty space with a transparent pixmap
223             gtk_table_attach(GTK_TABLE(itsTable), bidSuitEmptyPixmaps[itsPlPos],
224                              itsSuitCoords[0], itsSuitCoords[1],
225                              itsSuitCoords[2], itsSuitCoords[3],
226                              (GtkAttachOptions) (GTK_FILL),
227                              (GtkAttachOptions) (GTK_FILL), 0, 0);
228         }
229     } else {
230         // set the pixmap
231         itsBidSuitPixmap = pixmap;
232         gtk_container_remove(GTK_CONTAINER(itsTable), bidSuitEmptyPixmaps[itsPlPos]);
233         gtk_table_attach(GTK_TABLE(itsTable), itsBidSuitPixmap, itsSuitCoords[0],
234                          itsSuitCoords[1], itsSuitCoords[2], itsSuitCoords[3],
235                          (GtkAttachOptions) (GTK_FILL),
236                          (GtkAttachOptions) (GTK_FILL), 0, 0);
237     }
238 }
239 
setBidCallPixmap(GtkWidget * pixmap)240 void GuiPlayer::setBidCallPixmap(GtkWidget* pixmap) {
241     /* if NULL then remove the pixmap */
242     if (pixmap == NULL) {
243         if (itsBidCallPixmap != NULL) {
244 #if 0
245             // hack to keep our pixmaps from being deleted by 0 references
246             if (GTK_OBJECT(itsBidCallPixmap)->ref_count == 1) {
247                 gtk_widget_ref(itsBidCallPixmap);
248             }
249 #endif
250 
251             // remove the bid pixmap
252             gtk_container_remove(GTK_CONTAINER(itsTable), itsBidCallPixmap);
253 
254             // remember that it's gone
255             itsBidCallPixmap = NULL;
256 
257             // fill the empty space with a transparent pixmap
258             gtk_table_attach(GTK_TABLE(itsTable), bidEmptyPixmaps[itsPlPos],
259                              itsBidCoords[0], itsBidCoords[1],
260                              itsBidCoords[2], itsBidCoords[3],
261                              (GtkAttachOptions) (GTK_FILL),
262                              (GtkAttachOptions) (GTK_FILL), 0, 0);
263         }
264     } else {
265         // set the pixmap
266         itsBidCallPixmap = pixmap;
267         gtk_container_remove(GTK_CONTAINER(itsTable), bidEmptyPixmaps[itsPlPos]);
268         gtk_table_attach(GTK_TABLE(itsTable), itsBidCallPixmap, itsBidCoords[0],
269                          itsBidCoords[1], itsBidCoords[2], itsBidCoords[3],
270                          (GtkAttachOptions) (GTK_FILL),
271                          (GtkAttachOptions) (GTK_FILL), 0, 0);
272     }
273 }
274 
setMarker(GtkWidget * pixmap)275 void GuiPlayer::setMarker(GtkWidget* pixmap) {
276     /* if NULL then remove the pixmap */
277     if (pixmap == NULL) {
278         if (itsMarkerPixmap != NULL) {
279 #if 0
280             // hack to keep our pixmaps from being deleted by 0 references
281             if (GTK_OBJECT(itsMarkerPixmap)->ref_count == 1) {
282                 gtk_widget_ref(itsMarkerPixmap);
283             }
284 #endif
285 
286             // remove the bid pixmap
287             gtk_container_remove(GTK_CONTAINER(itsMarkerTable), itsMarkerPixmap);
288 
289             // remember that it's gone
290             itsMarkerPixmap = NULL;
291 
292             // fill the empty space with a transparent pixmap
293             gtk_table_attach(GTK_TABLE(itsMarkerTable), emptyMarkers[itsPlPos],
294                              itsMarkerCoords[0], itsMarkerCoords[1],
295                              itsMarkerCoords[2], itsMarkerCoords[3],
296                              (GtkAttachOptions) (GTK_FILL),
297                              (GtkAttachOptions) (GTK_FILL), 0, 0);
298         }
299     } else {
300         // set the pixmap
301         itsMarkerPixmap = pixmap;
302         gtk_container_remove(GTK_CONTAINER(itsMarkerTable), emptyMarkers[itsPlPos]);
303         gtk_table_attach(GTK_TABLE(itsMarkerTable), itsMarkerPixmap,
304                          itsMarkerCoords[0], itsMarkerCoords[1],
305                          itsMarkerCoords[2], itsMarkerCoords[3],
306                          (GtkAttachOptions) (GTK_FILL),
307                          (GtkAttachOptions) (GTK_FILL), 0, 0);
308     }
309 }
310