File: window_bids.c

package info (click to toggle)
tenace 0.12-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,132 kB
  • sloc: sh: 10,975; ansic: 8,269; cpp: 4,767; makefile: 119
file content (182 lines) | stat: -rw-r--r-- 5,196 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 *  tenace - bridge hand viewer and editor
 *  Copyright (C) 2005-2009 Christoph Berg <cb@df7cb.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

#include <assert.h>
#include <string.h>

#include "bridge.h"
#include "functions.h"

#include "window_board.h"

static GtkWidget *window_bids = 0;

static void
bid_clicked (GtkWidget *lab, int *bid)
{
	static GtkWidget *alert_entry = NULL;
	if (! alert_entry)
		alert_entry = get_widget ("alert_entry");

	board *b = CUR_BOARD;
	int ret = board_append_bid (b, *bid, 1);

	if (!ret)
		return;

	PROTECT_BEGIN;
	const char *alert = gtk_entry_get_text (GTK_ENTRY (alert_entry));
	if (*alert) {
		board_set_alert (b, !strcmp (alert, "!") ? "" : alert);
		gtk_entry_set_text (GTK_ENTRY (alert_entry), "");
	}

	show_board (b, (ret == 2 ? REDRAW_FULL : REDRAW_BIDDING) | REDRAW_BIDDING_SCROLL);

	if (ret == 2) {
		char buf[50];
		snprintf (buf, sizeof (buf), _("Contract set to %s"),
			contract_string (b->level, b->trumps, b->declarer, b->doubled));
		board_statusbar (buf);
	}
	PROTECT_END;
}

void
window_bids_init ()
{
	if (window_bids)
		return;

	static int pass = bid_pass, x = bid_x, xx = bid_xx;
	static int bid[35];

	window_bids = get_widget ("window_bids");
	gtk_widget_show (window_bids);
	GtkTable *bids_table = GTK_TABLE(get_widget ("bids_table"));
	assert (bids_table);
	GtkWidget *lab;

	lab = gtk_button_new_with_label (_("PASS"));
	gtk_table_attach (bids_table, lab, 0, 3, 0, 1, GTK_FILL, 0, 0, 0);
	g_signal_connect (lab, "clicked", G_CALLBACK (bid_clicked), &pass);
	lab = gtk_button_new_with_label (_("X"));
	gtk_table_attach (bids_table, lab, 3, 4, 0, 1, GTK_FILL, 0, 0, 0);
	g_signal_connect (lab, "clicked", G_CALLBACK (bid_clicked), &x);
	lab = gtk_button_new_with_label (_("XX"));
	gtk_table_attach (bids_table, lab, 4, 5, 0, 1, GTK_FILL, 0, 0, 0);
	g_signal_connect (lab, "clicked", G_CALLBACK (bid_clicked), &xx);

	int d, l;
	for (l = 1; l <= 7; l++) {
		for (d = 0; d <= 4; d++) {
			GString *b = bid_string (5 * l + d, 0);
			lab = gtk_button_new_with_label (_(b->str));
			gtk_label_set_use_markup (GTK_LABEL (gtk_bin_get_child (GTK_BIN (lab))), TRUE);
			gtk_table_attach(bids_table, lab, d, d+1, l, l+1, GTK_FILL, 0, 0, 0);
			bid[5 * (l - 1) + d] = 5 * l + d;
			g_signal_connect (lab, "clicked",
					G_CALLBACK (bid_clicked), bid + (5 * (l - 1) + d));
		}
	}
	gtk_widget_show_all (window_bids);
}

void
window_bids_delete (void)
{
	if (!window_bids)
		return;

	gtk_widget_hide (window_bids);
	window_bids = 0;
}

G_MODULE_EXPORT void
on_bid_clear_clicked                   (GtkToolButton   *toolbutton,
                                        gpointer         user_data)
{
	board *b = CUR_BOARD;
	board_clear_bidding (b);
	show_board(b, REDRAW_BIDDING);
}

G_MODULE_EXPORT void
on_bid_undo_clicked                    (GtkToolButton   *toolbutton,
                                        gpointer         user_data)
{
	board *b = CUR_BOARD;
	if (b->n_bids) {
		board_remove_bid (b);
	}
	show_board(b, REDRAW_BIDDING | REDRAW_BIDDING_SCROLL);
}

#define TRY(x) { if (!(x)) goto end; }

G_MODULE_EXPORT void
on_bid_set_contract_clicked            (GtkToolButton   *toolbutton,
                                        gpointer         user_data)
{
	int i;
	board *b = CUR_BOARD;
	seat cur_seat = seat_mod (b->dealer + b->n_bids);
	int passes = (b->declarer - cur_seat) % 4;

	for (i = (b->n_bids + passes - 2) % 4; i < b->n_bids; i += 4)
		if (b->bidding[i] >= 5 && DENOM (b->bidding[i]) == b->trumps) {
			board_statusbar (_("Suit was already bid from wrong side"));
			goto end;
		}

	for (i = 0; i < passes; i++)
		TRY (board_append_bid (b, bid_pass, 0));

	TRY (board_append_bid (b, 5 * b->level + b->trumps, 0));
	if (b->doubled)
		TRY (board_append_bid (b, bid_x, 0));
	if (b->doubled == bid_xx)
		TRY (board_append_bid (b, bid_xx, 0));
	TRY (board_append_bid (b, bid_pass, 0));
	TRY (board_append_bid (b, bid_pass, 0));
	TRY (board_append_bid (b, bid_pass, 0));

end:
	show_board(b, REDRAW_BIDDING | REDRAW_BIDDING_SCROLL);
}

G_MODULE_EXPORT gboolean
on_window_bids_delete_event            (GtkWidget       *widget,
                                        GdkEvent        *event,
                                        gpointer         user_data)
{
	PROTECT_BEGIN_BOOL;
	GtkCheckMenuItem *menuitem = GTK_CHECK_MENU_ITEM (get_widget ("bids1"));
	gtk_check_menu_item_set_active (menuitem, FALSE);
	window_bids_delete ();
	PROTECT_END;
	return FALSE;
}

G_MODULE_EXPORT void
on_bids1_activate                      (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	if (gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (menuitem)))
		window_bids_init ();
	else
		window_bids_delete ();
}