File: window_card.c

package info (click to toggle)
tenace 0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,440 kB
  • ctags: 543
  • sloc: ansic: 5,303; sh: 3,683; makefile: 98
file content (191 lines) | stat: -rw-r--r-- 4,559 bytes parent folder | download
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
183
184
185
186
187
188
189
190
191
/*
 *  tenace - bridge hand viewer and editor
 *  Copyright (C) 2005-2007 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 <stdlib.h>

#include "bridge.h"
#include "functions.h"
#include "interface.h"
#include "support.h"
#include "window_board.h" /* board b */

#include "window_card.h" /* board b */

static GtkWidget *window_card = 0;
static HandDisplay *hand_display = 0;
static int seat_null = 0;
seat new_card_seat = west;

void
card_window_update (seat *cards)
{
	if (!window_card)
		return;
	static seat *cards_ptr = NULL;
	if (cards)
		cards_ptr = cards;
	if (!cards_ptr)
		return;

	int c;
	for (c = 0; c < 52; c++)
		hand_display_set_card (hand_display, c,
			cards_ptr[c] ?
			( cards_ptr[c] == new_card_seat ?
			  HAND_DISPLAY_HILIGHT_CARD : HAND_DISPLAY_GREY_CARD )
			: HAND_DISPLAY_CARD);
	hand_display_draw (GTK_WIDGET (hand_display));
}

void
set_new_card_seat (seat s)
{
	new_card_seat = s;
	card_window_update (NULL);
}

static void
card_clicked (HandDisplay *handdisp, int c, int *seatp)
{
	if (c == -1)
		return;

	assert (c >= 0 && c < 56);
	assert (new_card_seat >= 1 && new_card_seat <= 4);
	board *b = win->boards[win->cur];

	/* x clicked */
	if (c >= 52) {
		if (b->hand_cards[new_card_seat-1] == 13) {
			board_statusbar(_("Hand has already 13 cards"));
			return;
		}

		int i;
		for (i = 0; i < 13; i++) {
			int new = (c - 52) * 13 + i;
			if (b->dealt_cards[new] == 0) {
				add_card(b, new_card_seat, new);
				b->par_score = -1;

				board_statusbar(NULL);
				card_window_update(b->dealt_cards);
				hand_display_draw(GTK_WIDGET (hand_display)); /* force redraw */
				show_board(b, REDRAW_HANDS | REDRAW_PAR);
				return;
			}
		}
		board_statusbar(_("All cards of that suit dealt"));
		return;
	}

	if (b->dealt_cards[c] && !b->cards[c]) {
		board_statusbar(_("Card is in play and cannot be removed"));
		return;
	}

	if (b->dealt_cards[c] == new_card_seat)
		remove_card(b, new_card_seat, c);
	else {
		if (b->hand_cards[new_card_seat-1] == 13) {
			board_statusbar(_("Hand has already 13 cards"));
			return;
		}

		if (b->dealt_cards[c])
			remove_card(b, b->dealt_cards[c], c);
		add_card(b, new_card_seat, c);
	}
	b->par_score = -1;

	board_statusbar(NULL);
	card_window_update(b->dealt_cards);
	show_board(b, REDRAW_HANDS | REDRAW_PAR);
}

static void
window_card_drag_drop (HandDisplay *handdisp, int c, int on_card, int *seatp /* unused, always 0 here */)
{
	printf ("dropped %d on %d on card window\n", c, on_card);
	if (c == -1)
		return;

	board *b = win->boards[win->cur];

	if (b->dealt_cards[c] && !b->cards[c]) {
		board_statusbar(_("Card is in play and cannot be removed"));
		return;
	}

	seat from_seat = b->dealt_cards[c];
	if (! from_seat)
		return;
	remove_card(b, from_seat, c);

	if (on_card >= 0 && b->dealt_cards[on_card] == 0)
		add_card (b, from_seat, on_card);

	b->par_score = -1;

	board_statusbar (NULL);
	card_window_update (b->dealt_cards);
	show_board (b, REDRAW_HANDS | REDRAW_PAR);
}

void
window_card_set_style (int style)
{
	/* doesn't actually set the style, but makes the right drag icon appear */
	if (window_card)
		hand_display_set_style(hand_display, style);
}

void
window_card_init (int style)
{
	if (window_card)
		return;

	GtkWidget *w = hand_display_new (HAND_DISPLAY_MODE_HAND_X);
	hand_display = HAND_DISPLAY (w);
	int c;
	for (c = 0; c < 52; c++) {
		hand_display_set_card (hand_display, c, HAND_DISPLAY_CARD);
	}
	g_signal_connect (hand_display, "card-clicked", G_CALLBACK (card_clicked), NULL);
	g_signal_connect (hand_display,
			"card-drag-drop", G_CALLBACK (window_card_drag_drop), &seat_null);
	hand_display_set_drag (hand_display, 1);

	window_card = create_window_card ();
	GtkWidget *vbox = lookup_widget (window_card, "vbox2");
	gtk_widget_show (w);
	gtk_box_pack_start_defaults (GTK_BOX (vbox), w);
	gtk_widget_show (window_card);
	card_window_update(CUR_BOARD->dealt_cards);

	window_card_set_style (style);
}

void
window_card_delete (void)
{
	if (!window_card)
		return;

	gtk_widget_destroy (window_card);
	window_card = 0;
}