1 /*
2
3 eboard - chess client
4 http://www.bergo.eng.br/eboard
5 https://github.com/fbergo/eboard
6 Copyright (C) 2000-2016 Felipe Bergo
7 fbergo/at/gmail/dot/com
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23 */
24
25 #include <iostream>
26 #include "promote.h"
27 #include "eboard.h"
28
29 #include "xpm/q18.xpm"
30 #include "xpm/r18.xpm"
31 #include "xpm/b18.xpm"
32 #include "xpm/n18.xpm"
33 #include "xpm/k18.xpm"
34
UglyHack(PromotionPicker * a,int b)35 UglyHack::UglyHack(PromotionPicker *a,int b) {
36 picker=a;
37 index=b;
38 }
39
PromotionPicker(GdkWindow * window)40 PromotionPicker::PromotionPicker(GdkWindow *window) {
41 GtkWidget *l,*pm;
42 GdkPixmap *d;
43 GdkBitmap *b;
44 GtkStyle *style,*dstyle;
45 GtkWidget *h;
46 GdkColor red,red2;
47 int i;
48
49 widget=gtk_frame_new(0);
50 gtk_frame_set_shadow_type(GTK_FRAME(widget),GTK_SHADOW_OUT);
51
52 h=gtk_hbox_new(FALSE,0);
53 gtk_container_add(GTK_CONTAINER(widget),h);
54
55 l=gtk_label_new(_("Promotion Piece "));
56 gtk_box_pack_start(GTK_BOX(h),l,FALSE,TRUE,0);
57 gshow(l);
58
59 style=gtk_widget_get_style(h);
60
61 // 0 button
62 button[0]=gtk_toggle_button_new();
63 d = gdk_pixmap_create_from_xpm_d (window, &b,
64 &style->bg[GTK_STATE_NORMAL],
65 (gchar **) q18_xpm);
66 pm=gtk_pixmap_new(d,b);
67 gtk_container_add(GTK_CONTAINER(button[0]),pm);
68 gshow(pm);
69
70 // 1 button
71 button[1]=gtk_toggle_button_new();
72 d = gdk_pixmap_create_from_xpm_d (window, &b,
73 &style->bg[GTK_STATE_NORMAL],
74 (gchar **) r18_xpm);
75 pm=gtk_pixmap_new(d,b);
76 gtk_container_add(GTK_CONTAINER(button[1]),pm);
77 gshow(pm);
78
79 // 2 button
80 button[2]=gtk_toggle_button_new();
81 d = gdk_pixmap_create_from_xpm_d (window, &b,
82 &style->bg[GTK_STATE_NORMAL],
83 (gchar **) b18_xpm);
84 pm=gtk_pixmap_new(d,b);
85 gtk_container_add(GTK_CONTAINER(button[2]),pm);
86 gshow(pm);
87
88 // 3 button
89 button[3]=gtk_toggle_button_new();
90 d = gdk_pixmap_create_from_xpm_d (window, &b,
91 &style->bg[GTK_STATE_NORMAL],
92 (gchar **) n18_xpm);
93 pm=gtk_pixmap_new(d,b);
94 gtk_container_add(GTK_CONTAINER(button[3]),pm);
95 gshow(pm);
96
97 // 4 button
98 button[4]=gtk_toggle_button_new();
99 d = gdk_pixmap_create_from_xpm_d (window, &b,
100 &style->bg[GTK_STATE_NORMAL],
101 (gchar **) k18_xpm);
102 pm=gtk_pixmap_new(d,b);
103 gtk_container_add(GTK_CONTAINER(button[4]),pm);
104 gshow(pm);
105
106 dstyle=gtk_style_copy( gtk_widget_get_style(button[0]) );
107 red.red=0xffff;
108 red.green=0x6000;
109 red.blue=0x0000;
110
111 red2.red=0xffff;
112 red2.green=0xadad;
113 red2.blue=0x7c7c;
114
115 dstyle->bg[GTK_STATE_ACTIVE]=red;
116 dstyle->bg[GTK_STATE_SELECTED]=red;
117 dstyle->bg[GTK_STATE_PRELIGHT]=red2;
118
119 /*
120 GTK_STATE_NORMAL,
121 GTK_STATE_ACTIVE,
122 GTK_STATE_PRELIGHT,
123 GTK_STATE_SELECTED,
124 GTK_STATE_INSENSITIVE
125 */
126
127 for(i=0;i<5;i++) {
128 gtk_widget_set_style( button[i], dstyle );
129 gtk_box_pack_start(GTK_BOX(h),button[i],FALSE,FALSE,0);
130 gshow(button[i]);
131 gtset(GTK_TOGGLE_BUTTON(button[i]),!i);
132 gtk_signal_connect(GTK_OBJECT(button[i]),"toggled",
133 GTK_SIGNAL_FUNC(promote_toggle),
134 new UglyHack(this,i));
135 }
136 gshow(h);
137 promotion=0;
138 }
139
setPromotion(int index)140 void PromotionPicker::setPromotion(int index) {
141 int i;
142 promotion=index;
143 for(i=0;i<5;i++)
144 gtset(GTK_TOGGLE_BUTTON(button[i]),(i==promotion));
145 }
146
getPromotion()147 int PromotionPicker::getPromotion() {
148 return(promotion);
149 }
150
getPromotionPiece()151 piece PromotionPicker::getPromotionPiece() {
152 switch(promotion) {
153 case 1: return ROOK;
154 case 2: return BISHOP;
155 case 3: return KNIGHT;
156 case 4: return KING;
157 default: return QUEEN;
158 }
159 }
160
getPiece()161 piece PromotionPicker::getPiece() {
162 return(getPromotionPiece());
163 }
164
promote_toggle(GtkWidget * widget,gpointer data)165 void promote_toggle(GtkWidget *widget,gpointer data)
166 {
167 UglyHack *uh;
168 int ia,i;
169
170 uh=(UglyHack *)data;
171
172 ia=gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
173
174 if (ia) { // activated
175 uh->picker->promotion=uh->index;
176 for(i=0;i<5;i++)
177 if (i!=uh->index)
178 gtset(GTK_TOGGLE_BUTTON(uh->picker->button[i]),0);
179 } else { // deactivated
180 if (uh->picker->promotion != uh->index)
181 return;
182 gtset(GTK_TOGGLE_BUTTON(uh->picker->button[uh->index]),1);
183 }
184 }
185
186