28 static void destroy_event(gchar* response)
33 static gint handle = -1;
34 static gint arrow_begin_col;
35 static gint arrow_begin_row;
37 static inline gboolean is_inside_board(gint col, gint row)
39 return !((col | row) & ~0x7);
42 gboolean button_press_event(GtkWidget* widget, GdkEventButton* event)
46 if (event->button == 1)
52 if (is_inside_board(col, row))
61 double hsside = 0.5 * chessboard->
sside;
62 double fraction = hsside - (gint)hsside;
64 event->x - fraction, event->y - fraction, TRUE);
68 else if (event->button == 2)
72 if (is_inside_board(col, row))
76 arrow_begin_col = col;
77 arrow_begin_row = row;
80 else if (event->button == 3)
83 return (handle != -1);
86 gboolean button_release_event(GtkWidget* widget, GdkEventButton* event)
89 if (event->button == 1)
96 if (is_inside_board(col, row))
107 else if (event->button == 2)
109 GdkColor color = { 0, 0, 0, 65535 };
114 if ((arrow_begin_col != col || arrow_begin_row != row)&&
115 is_inside_board(arrow_begin_col, arrow_begin_row)&&
116 is_inside_board(col, row))
119 else if (event->button == 3)
124 static void setup_menu(GtkWidget* vbox)
127 GtkWidget* file_menu = gtk_menu_new();
130 GtkWidget* quit_item = gtk_menu_item_new_with_label(
"Quit");
133 gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), quit_item);
136 g_signal_connect_swapped(G_OBJECT(quit_item),
"activate", G_CALLBACK(destroy_event), (gpointer)
"file.quit");
139 gtk_widget_show(quit_item);
141 GtkWidget* menu_bar = gtk_menu_bar_new();
142 gtk_box_pack_start(GTK_BOX(vbox), menu_bar, FALSE, FALSE, 2);
143 gtk_widget_show(menu_bar);
145 GtkWidget* file_item = gtk_menu_item_new_with_label(
"File");
146 gtk_widget_show(file_item);
148 gtk_menu_item_set_submenu(GTK_MENU_ITEM(file_item), file_menu);
149 gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), file_item);
152 int main(
int argc,
char* argv[])
154 if (!gtk_init_check(&argc,& argv))
156 printf(
"gtk_init_check failed\n");
162 CwChessboard* chessboard = CW_CHESSBOARD(chessboard_widget);
171 for (
int row = 1; row < 7; ++row)
200 GdkColor green = { 0, 0, 32000, 0 };
202 GdkColor red2 = { 0, 65535, 0, 0 };
206 GtkWidget* main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
209 gtk_window_set_default_size(GTK_WINDOW(main_window), 500, 532);
212 GtkWidget* vbox = gtk_vbox_new(FALSE, 0);
213 gtk_container_add(GTK_CONTAINER(main_window), vbox);
214 gtk_widget_show(vbox);
220 gtk_container_add(GTK_CONTAINER(vbox), chessboard_widget);
223 gtk_widget_show(chessboard_widget);
226 g_signal_connect(G_OBJECT(chessboard),
"button-press-event", G_CALLBACK(button_press_event), NULL);
227 g_signal_connect(G_OBJECT(chessboard),
"button-release-event", G_CALLBACK(button_release_event), NULL);
230 g_signal_connect(G_OBJECT(main_window),
"destroy", G_CALLBACK(destroy_event), NULL);
233 gtk_widget_show(main_window);
void cw_chessboard_set_square(CwChessboard* chessboard, gint col, gint row, CwChessboardCode code)
CwChessboardCode const empty_square
gint const sside
Square side in pixels (read only).
uint16_t CwChessboardCode
A code to specify a chess piece.
CWCHESSBOARD_INLINE gint cw_chessboard_x2col(CwChessboard* chessboard, gdouble x)
CWCHESSBOARD_INLINE gint cw_chessboard_y2row(CwChessboard* chessboard, gdouble y)
CwChessboardCode const white_pawn
This file contains the declaration of the GTK+ widget CwChessboard.
CwChessboardColorHandle cw_chessboard_allocate_color_handle_rgb(CwChessboard* chessboard, gdouble red, gdouble green, gdouble blue)
A GTK+ chessboard widget.
CwChessboardCode const white_queen
CwChessboardCode cw_chessboard_get_floating_piece(CwChessboard* chessboard, gint handle)
void cw_chessboard_set_marker_thickness(CwChessboard* chessboard, gdouble thickness)
CwChessboardCode cw_chessboard_get_square(CwChessboard* chessboard, gint col, gint row)
CwChessboardCode const black_pawn
void cw_chessboard_hide_cursor(CwChessboard* chessboard)
CwChessboardCode const black_bishop
unsigned char CwChessboardColorHandle
A color handle used for background markers.
CwChessboardCode const black_knight
gpointer cw_chessboard_add_arrow(CwChessboard* chessboard, gint begin_col, gint begin_row, gint end_col, gint end_row, GdkColor const* color)
CwChessboardCode const white_knight
gint cw_chessboard_add_floating_piece(CwChessboard* chessboard, CwChessboardCode code, gdouble x, gdouble y, gboolean pointer_device)
void cw_chessboard_set_marker_color(CwChessboard* chessboard, gint col, gint row, CwChessboardColorHandle mahandle)
GtkWidget * cw_chessboard_new(void)
void cw_chessboard_show_cursor(CwChessboard* chessboard)
void cw_chessboard_set_background_color(CwChessboard* chessboard, gint col, gint row, CwChessboardColorHandle bghandle)
void cw_chessboard_remove_floating_piece(CwChessboard* chessboard, gint handle)