ChessboardWidget.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set for gtkmm
2 //
3 //! @file ChessboardWidget.h This file contains the declaration of the gtkmm class ChessboardWidget.
4 //
5 // Copyright (C) 2008, by
6 //
7 // Carlo Wood, Run on IRC <carlo@alinoe.com>
8 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
9 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef CHESSBOARD_H
25 #define CHESSBOARD_H
26 
27 #ifndef USE_PCH
28 #include "debug.h"
29 #endif
30 
31 #include <gtkmm/drawingarea.h>
32 #include "CwChessboard.h"
33 
34 /** @namespace cwmm
35  * @brief A namespace for all gtkmm related objects.
36 * /
37 namespace cwmm {
38 
39 /** @internal
40 *
41  * A helper class, needed because we must initialize CwChessboard before we can initialize Gtk::DrawingArea.
42 * /
44  protected:
45  CwChessboard* M_chessboard;
46  CwChessboardPtr(void) : M_chessboard(CW_CHESSBOARD(cw_chessboard_new())) { }
47 };
48 
49 /** @class ChessboardWidget
50  * @brief A gtkmm chessboard widget.
51 *
52  * This is a chessboard widget for use with gtkmm.
53 *
54  * A detailed overview can be found on the <a href="index.html">Main Page</a>.
55 *
56  * @sa ChessPositionWidget
57 * /
58 class ChessboardWidget : public CwChessboardPtr, public Gtk::DrawingArea {
59  public:
60  /** @name Construction/Destruction* /
61  //@{
62 
63  //! @brief Create a ChessboardWidget object.
64  ChessboardWidget(void);
65  //! @brief Destructor.
66  virtual ~ChessboardWidget();
67 
68  //@}
69 
70  private:
71  static void S_draw_pawn_hook(CwChessboard* chessboard, cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white);
72  static void S_draw_rook_hook(CwChessboard* chessboard, cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white);
73  static void S_draw_knight_hook(CwChessboard* chessboard, cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white);
74  static void S_draw_bishop_hook(CwChessboard* chessboard, cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white);
75  static void S_draw_queen_hook(CwChessboard* chessboard, cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white);
76  static void S_draw_king_hook(CwChessboard* chessboard, cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white);
77  static void S_draw_hud_layer_hook(CwChessboard* chessboard, cairo_t* cr, gint sside, guint hud);
78  static gboolean S_draw_hud_square_hook(CwChessboard* chessboard, cairo_t* cr, gint col, gint row, gint sside, guint hud);
79  static void S_draw_border_hook(CwChessboard* chessboard);
80  static void S_draw_turn_indicator_hook(CwChessboard* chessboard, gboolean white, gboolean on);
81  static void S_on_cursor_left_chessboard_hook(CwChessboard* chessboard, gint prev_col, gint prev_row);
82  static void S_on_cursor_entered_square_hook(CwChessboard* chessboard, gint prev_col, gint prev_row, gint col, gint row);
83 
84  /** @name Events* /
85  //@{
86 
87  protected:
88  /** @brief Called when the mouse button is pressed while on the chessboard widget.
89  *
90  * This function should be overridden. See the example tstcpp.cc in the source code for an example.
91  * /
92  virtual bool on_button_press_event(GdkEventButton* event) { return Gtk::DrawingArea::on_button_press_event(event); }
93 
94  /** @brief Called when the mouse button is released again.
95  *
96  * This function should be overridden. See the example tstcpp.cc in the source code for an example.
97  * /
98  virtual bool on_button_release_event(GdkEventButton* event) { return Gtk::DrawingArea::on_button_release_event(event); }
99 
100  /** @brief Called when the mouse pointer left the chessboard.
101  *
102  * @param prev_col : The column of the last square.
103  * @param prev_row : The row of the last square.
104  * /
105  virtual void on_cursor_left_chessboard(gint prev_col, gint prev_row)
106  { Dout(dc::notice, "Calling on_cursor_left_chessboard(" << prev_col << ", " << prev_row << ")"); }
107 
108  /** @brief Called when the mouse pointer entered a new square.
109  *
110  * @param prev_col : The column of the last square, or -1 if we entered from outside the chessboard.
111  * @param prev_row : The row of the last square, or -1 if we entered from outside the chessboard.
112  * @param col : The column of the entered square.
113  * @param row : The row of the entered square.
114  * /
115  virtual void on_cursor_entered_square(gint prev_col, gint prev_row, gint col, gint row)
116  { Dout(dc::notice, "Calling on_cursor_entered_square(" << prev_col << ", " << prev_row << ", " << col << ", " << row << ")"); }
117 
118  //@}
119 
120  /** @name Drawing primitives* /
121  //@{
122 
123  /** @brief Called to draw a pawn.
124  *
125  * The default calls #cw_chessboard_draw_pawn.
126  * /
127  virtual void draw_pawn(cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white)
128  { cw_chessboard_draw_pawn(M_chessboard, cr, x, y, sside, white); }
129 
130  /** @brief Called to draw a rook.
131  *
132  * The default calls #cw_chessboard_draw_rook.
133  * /
134  virtual void draw_rook(cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white)
135  { cw_chessboard_draw_rook(M_chessboard, cr, x, y, sside, white); }
136 
137  /** @brief Called to draw a knight.
138  *
139  * The default calls #cw_chessboard_draw_knight.
140  * /
141  virtual void draw_knight(cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white)
142  { cw_chessboard_draw_knight(M_chessboard, cr, x, y, sside, white); }
143 
144  /** @brief Called to draw a bishop.
145  *
146  * The default calls #cw_chessboard_draw_bishop.
147  * /
148  virtual void draw_bishop(cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white)
149  { cw_chessboard_draw_bishop(M_chessboard, cr, x, y, sside, white); }
150 
151  /** @brief Called to draw a queen.
152  *
153  * The default calls #cw_chessboard_draw_queen.
154  * /
155  virtual void draw_queen(cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white)
156  { cw_chessboard_draw_queen(M_chessboard, cr, x, y, sside, white); }
157 
158  /** @brief Called to draw a king.
159  *
160  * The default calls #cw_chessboard_draw_king.
161  * /
162  virtual void draw_king(cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white)
163  { cw_chessboard_draw_king(M_chessboard, cr, x, y, sside, white); }
164 
165  /** @brief Draw the HUD layer.
166  *
167  * This is a layer in between the background (existing of the 64 squares with
168  * a single color) and the layer with the pieces. It can be used to add some
169  * texture to the background.
170  *
171  * If the HUD layer is active, then this function is called
172  * whenever the widget is resized.
173  *
174  * @param cr : A cairo drawing context.
175  * @param sside : The size of one side of a square, in pixels.
176  * @param hud : The HUD index number, see the detailed documentation on the \ref index "main page" for more info.
177  *
178  * The default calls #cw_chessboard_default_draw_hud_layer.
179  *
180  * @sa enable_hud_layer, disable_hud_layer
181  * /
182  virtual void draw_hud_layer(cairo_t* cr, gint sside, guint hud)
183  { cw_chessboard_default_draw_hud_layer(M_chessboard, cr, sside, hud); }
184 
185  /** @brief Draw a single HUD square at \a col, \a row.
186  *
187  * This function is called by @ref draw_hud_layer "ChessboardWidget::draw_hud_layer" for each square.
188  * You can use it if you don't override #draw_hud_layer.
189  *
190  * @param cr : A cairo drawing context.
191  * @param col : The column of the square.
192  * @param row : The row of the square.
193  * @param sside : The size of one side of the square, in pixels.
194  * @param hud : The HUD index number, see the detailed documentation of \ref index "main page" for more info.
195  *
196  * @returns TRUE if anything was drawn at all, FALSE if the HUD square is fully transparent.
197  *
198  * The default calls #cw_chessboard_default_draw_hud_square.
199  * /
200  virtual gboolean draw_hud_square(cairo_t* cr, gint col, gint row, gint sside, guint hud)
201  { return cw_chessboard_default_draw_hud_square(M_chessboard, cr, col, row, sside, hud); }
202 
203  /** @brief Draw the border around the chessboard.
204  *
205  * This function is called when the border is first drawn, and every time the chessboard is resized.
206  * The width of the drawn border should be the value returned by CwChessboardClass::calc_board_border_width.
207  *
208  * The default calls #cw_chessboard_default_draw_border.
209  *
210  * @sa set_draw_border
211  * /
212  virtual void draw_border(void)
213  { return cw_chessboard_default_draw_border(M_chessboard); }
214 
215  /** @brief Draw the indicator that indicates whose turn it is.
216  *
217  * This function is called every time the border is redrawn,
218  * as well as every time #set_draw_turn_indicators is called.
219  *
220  * @param white True if the indicator of the white color has to be drawn.
221  * @param on True if the indictor is on, false if it is off.
222  *
223  * The default calls #cw_chessboard_default_draw_turn_indicator.
224  *
225  * @sa set_draw_border, set_draw_turn_indicators
226  * /
227  virtual void draw_turn_indicator(gboolean white, gboolean on)
228  { return cw_chessboard_default_draw_turn_indicator(M_chessboard, white, on); }
229 
230  //@}
231 
232  public:
233 
234  // Typedefs
235 
236  /** @typedef ColorHandle
237  * @brief A color handle used for background markers.
238  *
239  * The five least significant bits determine the color
240  * from a user defined color palet, used by the background squares
241  * and markers. A value of zero meaning the default background value
242  * for that square, or no marker - respectively.
243  *
244  * @sa allocate_color_handle_rgb, allocate_color_handle, free_color_handle,
245  * set_background_color, get_background_color, set_background_colors,
246  * get_background_colors, set_marker_color, get_marker_color
247  * /
249 
250  /** @typedef code_t
251  * @brief A code to specify a chess piece.
252  *
253  * One of the following constants:
254  * #empty_square, #black_pawn, #white_pawn, #black_rook, #white_rook, #black_knight, #white_knight,
255  * #black_bishop, #white_bishop, #black_queen, #white_queen, #black_king or #white_king.
256  *
257  * @sa set_square, floating_piece, get_floating_piece
258  * /
260 
261  /** @name Accessors* /
262  //@{
263 
264  //! @brief The side of a square in pixels.
265  gint sside(void) const { return M_chessboard->sside; }
266  /** @brief The x coordinate of the top-left pixel of square a1.
267  *
268  * If the board is flipped, then the x coordinate of the top-left pixel of square h8 is returned.
269  * In other words, this value is not depending on whether or not the board is flipped.
270  * /
271  gint top_left_a1_x(void) const { return M_chessboard->top_left_a1_x; }
272  /** @brief The y coordinate of the top-left pixel of square a1.
273  *
274  * If the board is flipped, then the y coordinate of the top-left pixel of square h8 is returned.
275  * In other words, this value is not depending on whether or not the board is flipped.
276  * /
277  gint top_left_a1_y(void) const { return M_chessboard->top_left_a1_y; }
278 
279  //@}
280 
281  /** @name Public Conversion Functions* /
282  //@{
283 
284  /** @brief Convert a (\a col, \a row) pair to the top-left coordinates of the corresponding square, relative to the top-left of the widget.
285  *
286  * @param col : A column, in the range [0, 7].
287  * @param row : A row, in the range [0, 7].
288  * @param x : A reference to where the x-coordinate of the result will be written to.
289  * @param y : A reference to where the y-coordinate of the result will be written to.
290  * /
291  void colrow2xy(gint col, gint row, gint& x, gint& y) const
292  { cw_chessboard_colrow2xy(M_chessboard, col, row,& x,& y); }
293 
294  /** @brief Convert an x-coordinate to the column number that it matches.
295  *
296  * If the x coordinate falls outside the board, then the returned value
297  * will be outside the range [0, 7].
298  *
299  * @param x : An x coordinate, relative to the left-side of the widget.
300  *
301  * @returns A column number.
302  * /
303  gint x2col(gdouble x) const
304  { return cw_chessboard_x2col(M_chessboard, x); }
305 
306  /** @brief Convert a y-coordinate to the row number that it matches.
307  *
308  * If the y coordinate falls outside the board, then the returned value
309  * will be outside the range [0, 7].
310  *
311  * @param y : A y coordinate, relative to the top-side of the widget.
312  *
313  * @returns A row number.
314  * /
315  gint y2row(gdouble y) const
316  { return cw_chessboard_y2row(M_chessboard, y); }
317 
318  /** @brief Test if a given column and row are on the chessboard.
319  *
320  * @param col : A column, in the range [0, 7].
321  * @param row : A row, in the range [0, 7].
322  *
323  * @returns True if (col, row) falls inside the chessboard.
324  * /
325  static gboolean is_inside_board(gint col, gint row)
326  { return !((col | row) & ~0x7); }
327 
328  //@}
329 
330  /** @name Chess Position* /
331  //@{
332 
333  /** @brief Change the piece on a square.
334  *
335  * Change or remove the piece on the square (\a col, \a row),
336  * by replacing the contents of the square with \a code.
337  * This does not change any other attribute of the square,
338  * like it's background color or marker.
339  *
340  * @param col : A column [0..7]
341  * @param row : A row [0..7]
342  * @param code : A #code_t.
343  * /
344  void set_square(gint col, gint row, code_t code)
345  { cw_chessboard_set_square(M_chessboard, col, row, code); }
346 
347  /** @brief Get what is currently on a square.
348  *
349  * Get the chess piece code for the square at (\a col, \a row).
350  * /
351  code_t get_square(gint col, gint row) const
352  { return cw_chessboard_get_square(M_chessboard, col, row); }
353 
354  //@}
355 
356  /** @name Border* /
357  //@{
358 
359  /** @brief Set the boolean which determines whether or not the chessboard widget draws a border around the chessboard.
360  *
361  * Default: TRUE (draw border).
362  *
363  * @param draw : Boolean, determining if the border should be drawn.
364  *
365  * @sa draw_border
366  * /
367  void set_draw_border(gboolean draw)
368  { cw_chessboard_set_draw_border(M_chessboard, draw); }
369 
370  /** @brief Get the boolean that determines whether or not the chessboard widget draws a border around the chessboard.
371  *
372  * @returns <code>TRUE</code> if the border is being drawn.
373  *
374  * @sa set_draw_border
375  * /
376  gboolean get_draw_border(void) const
377  { return cw_chessboard_get_draw_border(M_chessboard); }
378 
379  /** @brief Set the boolean which determines whether or not to draw turn indicators.
380  *
381  * Indicators will only be drawn if also the border is drawn.
382  * Default: TRUE (draw indicators).
383  *
384  * @param draw Boolean, determining if the indicators should be drawn.
385  *
386  * @sa set_draw_border, draw_turn_indicator
387  * /
388  void set_draw_turn_indicators(gboolean draw)
389  { return cw_chessboard_set_draw_turn_indicators(M_chessboard, draw); }
390 
391  /** @brief Get the boolean which determines whether or not to draw turn indicators.
392  *
393  * @sa set_draw_turn_indicators
394  * /
395  gboolean get_draw_turn_indicators(void) const
396  { return cw_chessboard_get_draw_turn_indicators(M_chessboard); }
397 
398  /** @brief Set the color of the active turn indicator.
399  *
400  * Default: TRUE
401  *
402  * @param white TRUE if whites turn indicator should be active.
403  *
404  * @sa set_draw_turn_indicators
405  * /
406  void set_active_turn_indicator(gboolean white)
407  { return cw_chessboard_set_active_turn_indicator(M_chessboard, white); }
408 
409  /** @brief Get the boolean which determines whether whites or black turn indicator is active.
410  *
411  * @sa set_active_turn_indicator
412  * /
413  gboolean get_active_turn_indicator(void) const
414  { return cw_chessboard_get_active_turn_indicator(M_chessboard); }
415 
416  /** @brief Set the boolean which determines whether white is playing bottom up or top down.
417  *
418  * Default: FALSE (white plays upwards).
419  *
420  * @param flip Boolean, determining if white plays upwards or not.
421  * /
422  void set_flip_board(gboolean flip)
423  { return cw_chessboard_set_flip_board(M_chessboard, flip); }
424 
425  /** @brief Get the boolean which determines whether white is playing bottom up or top down.
426  *
427  * @sa set_flip_board
428  * /
429  gboolean get_flip_board(void) const
430  { return cw_chessboard_get_flip_board(M_chessboard); }
431 
432  /** @brief Set the calc_board_border_width function.
433  *
434  * Change the function that calculates the border width as function of the size of the squares.
435  *
436  * @param new_calc_board_border_width : The new function.
437  *
438  * @sa CwChessboardClass::calc_board_border_width
439  * /
440  void set_calc_board_border_width(gint (*new_calc_board_border_width)(CwChessboard const*, gint))
441  {
442  CW_CHESSBOARD_GET_CLASS(M_chessboard)->calc_board_border_width = new_calc_board_border_width;
443  }
444  //@}
445 
446  /** @name Colors* /
447  //@{
448 
449  /** @brief Set the color of the dark squares.
450  *
451  * Set the background color of the dark squares (a1, c1 etc).
452  * Default: light green.
453  *
454  * @param color : The new color of the dark squares.
455  * /
456  void set_dark_square_color(GdkColor const& color)
457  { cw_chessboard_set_dark_square_color(M_chessboard,& color); }
458 
459  /** @brief Set the color of the light squares.
460  *
461  * Set the background color of the light squares (b1, d1 etc).
462  * Default: yellow/white.
463  *
464  * @param color : The new color of the light squares.
465  * /
466  void set_light_square_color(GdkColor const& color)
467  { cw_chessboard_set_light_square_color(M_chessboard,& color); }
468 
469  /** @brief Set the color of the border around the chessboard.
470  *
471  * @param color : The new color of the border.
472  *
473  * @sa get_border_color, set_draw_border
474  * /
475  void set_border_color(GdkColor const& color)
476  { cw_chessboard_set_border_color(M_chessboard,& color); }
477 
478  /** @brief Set the fill color of the white chess pieces.
479  *
480  * Default: white
481  *
482  * @param color : The new fill color of the white pieces.
483  * /
484  void set_white_fill_color(GdkColor const& color)
485  { cw_chessboard_set_white_fill_color(M_chessboard,& color); }
486 
487  /** @brief Set the line color of the white chess pieces.
488  *
489  * Default: black
490  *
491  * @param color : The new line color of the white pieces.
492  * /
493  void set_white_line_color(GdkColor const& color)
494  { cw_chessboard_set_white_line_color(M_chessboard,& color); }
495 
496  /** @brief Set the fill color of the black chess pieces.
497  *
498  * Default: black
499  *
500  * @param color : The new fill color of the black pieces.
501  * /
502  void set_black_fill_color(GdkColor const& color)
503  { cw_chessboard_set_black_fill_color(M_chessboard,& color); }
504 
505  /** @brief Set the line color of the black chess pieces.
506  *
507  * Default: white
508  *
509  * @param color : The new line color of the black pieces.
510  * /
511  void set_black_line_color(GdkColor const& color)
512  { cw_chessboard_set_black_line_color(M_chessboard,& color); }
513 
514  /** @brief Retrieve the current background color of the dark squares.
515  *
516  * @param color : Pointer to the output variable.
517  *
518  * @sa set_dark_square_color
519  * /
520  void get_dark_square_color(GdkColor& color) const
521  { cw_chessboard_get_dark_square_color(M_chessboard,& color); }
522 
523  /** @brief Retrieve the current background color of the light squares.
524  *
525  * @param color : Pointer to the output variable.
526  *
527  * @sa set_light_square_color
528  * /
529  void get_light_square_color(GdkColor& color) const
530  { cw_chessboard_get_light_square_color(M_chessboard,& color); }
531 
532  /** @brief Retrieve the current color of the border around the chessboard.
533  *
534  * @param color : Pointer to the output variable.
535  *
536  * @sa set_border_color
537  * /
538  void get_border_color(GdkColor& color) const
539  { cw_chessboard_get_border_color(M_chessboard,& color); }
540 
541  /** @brief Retrieve the current fill color of the white chess pieces.
542  *
543  * @param color : Pointer to the output variable.
544  *
545  * @sa set_white_fill_color
546  * /
547  void get_white_fill_color(GdkColor& color) const
548  { cw_chessboard_get_white_fill_color(M_chessboard,& color); }
549 
550  /** @brief Retrieve the current line color of the white chess pieces.
551  *
552  * @param color : Pointer to the output variable.
553  *
554  * @sa set_white_line_color
555  * /
556  void get_white_line_color(GdkColor& color) const
557  { cw_chessboard_get_white_line_color(M_chessboard,& color); }
558 
559  /** @brief Retrieve the current fill color of the black chess pieces.
560  *
561  * @param color : Pointer to the output variable.
562  *
563  * @sa set_black_fill_color
564  * /
565  void get_black_fill_color(GdkColor& color) const
566  { cw_chessboard_get_black_fill_color(M_chessboard,& color); }
567 
568  /** @brief Retrieve the current line color of the black chess pieces.
569  *
570  * @param color : Pointer to the output variable.
571  *
572  * @sa set_black_line_color
573  * /
574  void get_black_line_color(GdkColor& color) const
575  { cw_chessboard_get_black_line_color(M_chessboard,& color); }
576 
577  /** @brief Allocate a new ColorHandle.
578  *
579  * Simultaneous, there can be at most 31 different colors.
580  * It is the responsibility of the user to free the colors if they are no longer used.
581  *
582  * @param red : The red component of the color in the range [0...1].
583  * @param green : The green component of the color in the range [0...1].
584  * @param blue : The blue component of the color in the range [0...1].
585  *
586  * @returns A color handle that can be used with #set_background_color and #set_marker_color.
587  *
588  * @sa allocate_color_handle, free_color_handle
589  * /
590  ColorHandle allocate_color_handle_rgb(gdouble red, gdouble green, gdouble blue)
591  { return cw_chessboard_allocate_color_handle_rgb(M_chessboard, red, green, blue); }
592 
593  /** @brief Allocate a new CwChessboardColorHandle.
594  *
595  * From more information, see #allocate_color_handle_rgb.
596  *
597  * @param color : The color to allocate.
598  *
599  * @returns A color handle that can be used with #set_background_color and #set_marker_color.
600  *
601  * @sa free_color_handle
602  * /
603  ColorHandle allocate_color_handle(GdkColor const& color)
604  { return cw_chessboard_allocate_color_handle(M_chessboard,& color); }
605 
606  /** @brief Free up the color handle \a handle, so it can be reused.
607  *
608  * @param handle : A color handle as returned by allocate_color_handle_rgb or allocate_color_handle.
609  *
610  * @sa allocate_color_handle_rgb, allocate_color_handle
611  * /
612  void free_color_handle(ColorHandle handle)
613  { cw_chessboard_free_color_handle(M_chessboard, handle); }
614 
615  /** @brief Set the background color of the square at \a col, \a row.
616  *
617  * @param col : The column of the square.
618  * @param row : The row of the square.
619  * @param handle : A color handle as returned by #allocate_color_handle_rgb or
620  * #allocate_color_handle. A handle with a value of 0 means the
621  * default background color.
622  * /
623  void set_background_color(gint col, gint row, ColorHandle handle)
624  { cw_chessboard_set_background_color(M_chessboard, col, row, handle); }
625 
626  /** @brief Get the current background color handle.
627  *
628  * Convenience function.
629  *
630  * @param col : The column of the square.
631  * @param row : The row of the square.
632  *
633  * @returns The handle that was passed to #set_background_color
634  * for this square, or 0 if the square is not associated
635  * with a color handle.
636  * /
637  ColorHandle get_background_color(gint col, gint row) const
638  { return cw_chessboard_get_background_color(M_chessboard, col, row); }
639 
640  /** @brief Set new background colors of any number of squares.
641  *
642  * @param handles : Array of 64 ColorHandles.
643  * A handle with a value of 0 means the default background color.
644  * /
645  void set_background_colors(ColorHandle const* handles)
646  { cw_chessboard_set_background_colors(M_chessboard, handles); }
647 
648  /** @brief Get all background colors handles.
649  *
650  * Fill the array \a handles with the current color handles.
651  *
652  * @param handles : The output array. Should be an array of 64 ColorHandles.
653  * /
654  void get_background_colors(ColorHandle* handles) const
655  { cw_chessboard_get_background_colors(M_chessboard, handles); }
656 
657  //@}
658 
659  /** @name Floating Pieces* /
660  //@{
661 
662  /** @brief Add a new floating chess piece.
663  *
664  * This function displays a chess piece with code \a code at widget coordinates (\a x, \a y).
665  * Half the side of a square will be subtracted from the coordinates passed, and
666  * the result truncated, in order to determine where to draw the top-left corner
667  * of the piece. The result is that (\a x, \a y) is more or less the center of the piece.
668  *
669  * Setting \a pointer_device will cause gdk_window_get_pointer to be called
670  * after the next redraw has finished. This is needed to receive the next motion
671  * notify event with GDK_POINTER_MOTION_HINT_MASK being used.
672  *
673  * There may only be one floating piece related the pointer device at a time.
674  * If there is already another floating piece related to the pointer device
675  * then the value of \a pointer_device is ignored.
676  *
677  * @param code : The code of the chess piece to be drawn.
678  * @param x : The center x-coordinate of the piece.
679  * @param y : The center y-coordinate of the piece.
680  * @param pointer_device : Whether this piece is under the pointer device or not.
681  *
682  * @returns A handle that can be passed to each of the functions below.
683  *
684  * @sa get_floating_piece, remove_floating_piece, move_floating_piece
685  * /
686  gint add_floating_piece(code_t code, gdouble x, gdouble y, gboolean pointer_device)
687  { return cw_chessboard_add_floating_piece(M_chessboard, code, x, y, pointer_device); }
688 
689  /** @brief Move a floating chess piece to a new position.
690  *
691  * Move a floating piece with handle \a handle to the new widget
692  * coordinates at (\a x, \a y). \a handle must be a handle as
693  * returned by #add_floating_piece.
694  *
695  * @param handle : The floating piece handle.
696  * @param x : The new x coordinate.
697  * @param y : The new y coordinate.
698  *
699  * @sa add_floating_piece
700  * /
701  void move_floating_piece(gint handle, gdouble x, gdouble y)
702  { cw_chessboard_move_floating_piece(M_chessboard, handle, x, y); }
703 
704  /** @brief Remove a floating piece.
705  *
706  * Delete the floating piece with handle \a handle.
707  * \a handle must be a handle as returned by #add_floating_piece.
708  *
709  * @param handle : The floating piece handle.
710  * /
711  void remove_floating_piece(gint handle)
712  { cw_chessboard_remove_floating_piece(M_chessboard, handle); }
713 
714  /** @brief Determine what piece a given floating piece is.
715  *
716  * Get the code_t of the floating piece represented by \a handle.
717  * \a handle must be a handle as returned by #add_floating_piece.
718  *
719  * @param handle : The floating piece handle.
720  * /
721  code_t get_floating_piece(gint handle) const
722  { return cw_chessboard_get_floating_piece(M_chessboard, handle); }
723 
724  //@}
725 
726  /** @name HUD Layers* /
727  //@{
728 
729  /** @brief Active a HUD layer.
730  *
731  * HUD 0 lays between the background and the pieces.
732  * HUD 1 lays above the pieces.
733  * A custom HUD layer can be created by setting CwChessboardClass::draw_hud_layer.
734  *
735  * @param hud : The HUD layer (0 or 1).
736  *
737  * @sa CwChessboardClass::draw_hud_layer, disable_hud_layer
738  * /
739  void enable_hud_layer(guint hud)
740  { cw_chessboard_enable_hud_layer(M_chessboard, hud); }
741 
742  /** @brief Disable the HUD layer again.
743  *
744  * Used resources are returned to the system.
745  *
746  * @param hud : The HUD layer (0 or 1).
747  *
748  * @sa enable_hud_layer
749  * /
750  void disable_hud_layer(guint hud)
751  { cw_chessboard_disable_hud_layer(M_chessboard, hud); }
752 
753  //@}
754 
755  /** @name Markers* /
756  //@{
757 
758  /** @brief Change the color of the marker.
759  *
760  * Add a marker to, or remove a marker from the square at \a col, \a row.
761  *
762  * @param col : The column of the square.
763  * @param row : The row of the square.
764  * @param mahandle : A color handle as returned by #allocate_color_handle_rgb or #allocate_color_handle.
765  * A handle with a value of 0 removes the marker (if any was there).
766  * /
767  void set_marker_color(gint col, gint row, ColorHandle mahandle)
768  { cw_chessboard_set_marker_color(M_chessboard, col, row, mahandle); }
769 
770  /** @brief Get marker color.
771  *
772  * Convenience function.
773  *
774  * @param col : The column of the square.
775  * @param row : The row of the square.
776  *
777  * @returns The handle that was passed to #set_marker_color
778  * for this square, or 0 if the square doesn't have a marker.
779  * /
780  ColorHandle get_marker_color(gint col, gint row) const
781  { return cw_chessboard_get_marker_color(M_chessboard, col, row); }
782 
783  /** @brief Set the marker thickness.
784  *
785  * This is a value between 0 and 0.5.
786  *
787  * @param thickness : The thickness of the marker as fraction of sside. Range [0...0.5]
788  *
789  * @sa get_marker_thickness
790  * /
791  void set_marker_thickness(gdouble thickness)
792  { return cw_chessboard_set_marker_thickness(M_chessboard, thickness); }
793 
794  /** @brief Get the current marker thickness as fraction of sside.
795  *
796  * @sa set_marker_thickness
797  * /
798  gdouble get_marker_thickness(void) const
799  { return cw_chessboard_get_marker_thickness(M_chessboard); }
800 
801  /** @brief Choose whether markers should be drawn below or above HUD layer 0.
802  *
803  * Markers can be drawn directly below or directly above HUD layer 0.
804  *
805  * @param below : TRUE when markers should be drawn below HUD layer 0.
806  * /
807  void set_marker_level(gboolean below)
808  { return cw_chessboard_set_marker_level(M_chessboard, below); }
809 
810  //@}
811 
812  /** @name Cursor* /
813  //@{
814 
815  /** @brief Show the cursor.
816  *
817  * This high-lights the square under the mouse by drawing a
818  * square with a configurable thickness and color.
819  *
820  * @sa set_cursor_thickness, get_cursor_thickness
821  * /
822  void show_cursor(void)
823  { cw_chessboard_show_cursor(M_chessboard); }
824 
825  /** @brief Hide the cursor.
826  *
827  * @sa show_cursor
828  * /
829  void hide_cursor(void)
830  { cw_chessboard_hide_cursor(M_chessboard); }
831 
832  /** @brief Set the thickness of the cursor.
833  *
834  * This is a value between 0 and 0.5.
835  *
836  * @param thickness : The thickness of the cursor as fraction of sside. Range [0...0.5]
837  *
838  * @sa get_cursor_thickness
839  * /
840  void set_cursor_thickness(gdouble thickness)
841  { cw_chessboard_set_cursor_thickness(M_chessboard, thickness); }
842 
843  /** @brief Get the current cursor thickness as fraction of sside.
844  *
845  * @sa set_cursor_thickness
846  * /
847  gdouble get_cursor_thickness(void) const
848  { return cw_chessboard_get_cursor_thickness(M_chessboard); }
849 
850  /** @brief Set the color of the cursor.
851  *
852  * @param color : The color to be used for the cursor.
853  * /
854  void set_cursor_color(GdkColor const& color)
855  { cw_chessboard_set_cursor_color(M_chessboard,& color); }
856 
857  /** @brief Get the current cursor color.
858  *
859  * @param color : Pointer to the output variable.
860  * /
861  void get_cursor_color(GdkColor& color) const
862  { cw_chessboard_get_cursor_color(M_chessboard,& color); }
863 
864  //@}
865 
866  /** @name Arrows* /
867  //@{
868 
869  /** @brief Draw an arrow on the board.
870  *
871  * @param begin_col : The column of the starting square.
872  * @param begin_row : The row of the starting square.
873  * @param end_col : The column of the ending square.
874  * @param end_row : The row of the ending square.
875  * @param color : The color to draw the arrow in.
876  *
877  * @returns A handle that can be used to remove the arrow again.
878  *
879  * @sa remove_arrow
880  * /
881  gpointer add_arrow(gint begin_col, gint begin_row, gint end_col, gint end_row, GdkColor const& color)
882  { return cw_chessboard_add_arrow(M_chessboard, begin_col, begin_row, end_col, end_row,& color); }
883 
884  /** @brief Remove a previously added arrow.
885  *
886  * @param ptr : The arrow handle as returned by #add_arrow.
887  * /
888  void remove_arrow(gpointer ptr)
889  { cw_chessboard_remove_arrow(M_chessboard, ptr); }
890 
891  //@}
892 };
893 
894 } // namespace cwmm
895 
896 #endif // CHESSBOARD_H
virtual void draw_knight(cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white)
Called to draw a knight.
void colrow2xy(gint col, gint row, gint& x, gint& y) const
Convert a (col, row) pair to the top-left coordinates of the corresponding square, relative to the top-left of the widget.
void cw_chessboard_set_square(CwChessboard* chessboard, gint col, gint row, CwChessboardCode code)
void get_white_line_color(GdkColor& color) const
Retrieve the current line color of the white chess pieces.
void set_draw_border(gboolean draw)
Set the boolean which determines whether or not the chessboard widget draws a border around the chess...
void set_active_turn_indicator(gboolean white)
Set the color of the active turn indicator.
void cw_chessboard_set_cursor_thickness(CwChessboard* chessboard, gdouble thickness)
void set_background_colors(ColorHandle const* handles)
Set new background colors of any number of squares.
void free_color_handle(ColorHandle handle)
Free up the color handle handle, so it can be reused.
code_t get_square(gint col, gint row) const
Get what is currently on a square.
void get_border_color(GdkColor& color) const
Retrieve the current color of the border around the chessboard.
void set_square(gint col, gint row, code_t code)
Change the piece on a square.
void cw_chessboard_get_border_color(CwChessboard* chessboard, GdkColor* color)
uint16_t CwChessboardCode
A code to specify a chess piece.
gboolean cw_chessboard_get_active_turn_indicator(CwChessboard* chessboard)
void cw_chessboard_free_color_handle(CwChessboard* chessboard, CwChessboardColorHandle handle)
void disable_hud_layer(guint hud)
Disable the HUD layer again.
void cw_chessboard_draw_knight(CwChessboard* chessboard, cairo_t* cr, gdouble x, gdouble y, gdouble scale, gboolean white)
virtual void draw_border(void)
Draw the border around the chessboard.
ColorData const white
A constant representing the color white.
Definition: Color.h:55
void set_calc_board_border_width(gint(*new_calc_board_border_width)(CwChessboard const* , gint))
Set the calc_board_border_width function.
CWCHESSBOARD_INLINE gint cw_chessboard_x2col(CwChessboard* chessboard, gdouble x)
void set_white_line_color(GdkColor const& color)
Set the line color of the white chess pieces.
gint top_left_a1_x(void) const
The x coordinate of the top-left pixel of square a1.
static gboolean is_inside_board(gint col, gint row)
Test if a given column and row are on the chessboard.
void cw_chessboard_set_white_line_color(CwChessboard* chessboard, GdkColor const* color)
virtual void draw_turn_indicator(gboolean white, gboolean on)
Draw the indicator that indicates whose turn it is.
void cw_chessboard_draw_bishop(CwChessboard* chessboard, cairo_t* cr, gdouble x, gdouble y, gdouble scale, gboolean white)
gboolean get_active_turn_indicator(void) const
Get the boolean which determines whether whites or black turn indicator is active.
void cw_chessboard_draw_pawn(CwChessboard* chessboard, cairo_t* cr, gdouble x, gdouble y, gdouble scale, gboolean white)
void cw_chessboard_set_background_colors(CwChessboard* chessboard, CwChessboardColorHandle const* handles)
void set_marker_thickness(gdouble thickness)
Set the marker thickness.
virtual void draw_pawn(cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white)
Called to draw a pawn.
A gtkmm chessboard widget.
CWCHESSBOARD_INLINE gint cw_chessboard_y2row(CwChessboard* chessboard, gdouble y)
gboolean cw_chessboard_get_draw_border(CwChessboard* chessboard)
void cw_chessboard_get_white_line_color(CwChessboard* chessboard, GdkColor* color)
gdouble get_marker_thickness(void) const
Get the current marker thickness as fraction of sside.
void cw_chessboard_default_draw_turn_indicator(CwChessboard* chessboard, gboolean white, gboolean on)
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)
void set_light_square_color(GdkColor const& color)
Set the color of the light squares.
void cw_chessboard_draw_king(CwChessboard* chessboard, cairo_t* cr, gdouble x, gdouble y, gdouble scale, gboolean white)
void get_black_line_color(GdkColor& color) const
Retrieve the current line color of the black chess pieces.
gint add_floating_piece(code_t code, gdouble x, gdouble y, gboolean pointer_device)
Add a new floating chess piece.
void show_cursor(void)
Show the cursor.
void set_draw_turn_indicators(gboolean draw)
Set the boolean which determines whether or not to draw turn indicators.
gint top_left_a1_y(void) const
The y coordinate of the top-left pixel of square a1.
virtual void draw_bishop(cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white)
Called to draw a bishop.
A GTK+ chessboard widget.
Definition: CwChessboard.h:101
CwChessboardCode cw_chessboard_get_floating_piece(CwChessboard* chessboard, gint handle)
void cw_chessboard_default_draw_hud_layer(CwChessboard* chessboard, cairo_t* cr, gint sside, guint hud)
void cw_chessboard_set_marker_level(CwChessboard* chessboard, gboolean below)
ColorHandle get_marker_color(gint col, gint row) const
Get marker color.
void set_border_color(GdkColor const& color)
Set the color of the border around the chessboard.
gint sside(void) const
The side of a square in pixels.
virtual void draw_queen(cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white)
Called to draw a queen.
void cw_chessboard_set_marker_thickness(CwChessboard* chessboard, gdouble thickness)
gint x2col(gdouble x) const
Convert an x-coordinate to the column number that it matches.
void cw_chessboard_default_draw_border(CwChessboard* chessboard)
gboolean cw_chessboard_get_draw_turn_indicators(CwChessboard* chessboard)
virtual gboolean draw_hud_square(cairo_t* cr, gint col, gint row, gint sside, guint hud)
Draw a single HUD square at col, row.
CwChessboardCode cw_chessboard_get_square(CwChessboard* chessboard, gint col, gint row)
void hide_cursor(void)
Hide the cursor.
gdouble cw_chessboard_get_cursor_thickness(CwChessboard* chessboard)
void cw_chessboard_draw_queen(CwChessboard* chessboard, cairo_t* cr, gdouble x, gdouble y, gdouble scale, gboolean white)
gboolean get_draw_turn_indicators(void) const
Get the boolean which determines whether or not to draw turn indicators.
void cw_chessboard_draw_rook(CwChessboard* chessboard, cairo_t* cr, gdouble x, gdouble y, gdouble scale, gboolean white)
void cw_chessboard_hide_cursor(CwChessboard* chessboard)
void cw_chessboard_set_flip_board(CwChessboard* chessboard, gboolean flip)
void cw_chessboard_get_black_line_color(CwChessboard* chessboard, GdkColor* color)
unsigned char CwChessboardColorHandle
A color handle used for background markers.
Definition: CwChessboard.h:79
void cw_chessboard_get_cursor_color(CwChessboard* chessboard, GdkColor* color)
CWCHESSBOARD_INLINE void cw_chessboard_colrow2xy(CwChessboard* chessboard, gint col, gint row, gint* x, gint* y)
ColorHandle get_background_color(gint col, gint row) const
Get the current background color handle.
virtual bool on_button_press_event(GdkEventButton* event)
Called when the mouse button is pressed while on the chessboard widget.
void cw_chessboard_disable_hud_layer(CwChessboard* chessboard, guint hud)
void cw_chessboard_set_draw_border(CwChessboard* chessboard, gboolean draw)
gpointer cw_chessboard_add_arrow(CwChessboard* chessboard, gint begin_col, gint begin_row, gint end_col, gint end_row, GdkColor const* color)
void cw_chessboard_get_light_square_color(CwChessboard* chessboard, GdkColor* color)
void remove_arrow(gpointer ptr)
Remove a previously added arrow.
CwChessboardColorHandle cw_chessboard_get_marker_color(CwChessboard* chessboard, gint col, gint row)
void get_dark_square_color(GdkColor& color) const
Retrieve the current background color of the dark squares.
ColorHandle allocate_color_handle(GdkColor const& color)
Allocate a new CwChessboardColorHandle.
code_t get_floating_piece(gint handle) const
Determine what piece a given floating piece is.
void set_cursor_thickness(gdouble thickness)
Set the thickness of the cursor.
virtual void on_cursor_entered_square(gint prev_col, gint prev_row, gint col, gint row)
Called when the mouse pointer entered a new square.
CwChessboardCode code_t
A code to specify a chess piece.
void cw_chessboard_set_black_line_color(CwChessboard* chessboard, GdkColor const* color)
virtual bool on_button_release_event(GdkEventButton* event)
Called when the mouse button is released again.
void cw_chessboard_set_white_fill_color(CwChessboard* chessboard, GdkColor const* color)
A namespace for all gtkmm related objects.
gint cw_chessboard_add_floating_piece(CwChessboard* chessboard, CwChessboardCode code, gdouble x, gdouble y, gboolean pointer_device)
void set_black_line_color(GdkColor const& color)
Set the line color of the black chess pieces.
void remove_floating_piece(gint handle)
Remove a floating piece.
void cw_chessboard_get_dark_square_color(CwChessboard* chessboard, GdkColor* color)
void cw_chessboard_set_border_color(CwChessboard* chessboard, GdkColor const* color)
void cw_chessboard_set_draw_turn_indicators(CwChessboard* chessboard, gboolean draw)
void set_marker_level(gboolean below)
Choose whether markers should be drawn below or above HUD layer 0.
void cw_chessboard_set_dark_square_color(CwChessboard* chessboard, GdkColor const* color)
void cw_chessboard_set_cursor_color(CwChessboard* chessboard, GdkColor const* color)
void get_background_colors(ColorHandle* handles) const
Get all background colors handles.
void set_cursor_color(GdkColor const& color)
Set the color of the cursor.
void set_background_color(gint col, gint row, ColorHandle handle)
Set the background color of the square at col, row.
virtual void draw_rook(cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white)
Called to draw a rook.
void cw_chessboard_set_black_fill_color(CwChessboard* chessboard, GdkColor const* color)
gint y2row(gdouble y) const
Convert a y-coordinate to the row number that it matches.
gboolean cw_chessboard_default_draw_hud_square(CwChessboard* chessboard, cairo_t* cr, gint col, gint row, gint sside, guint hud)
void cw_chessboard_get_white_fill_color(CwChessboard* chessboard, GdkColor* color)
void set_dark_square_color(GdkColor const& color)
Set the color of the dark squares.
CwChessboardColorHandle ColorHandle
A color handle used for background markers.
void set_marker_color(gint col, gint row, ColorHandle mahandle)
Change the color of the marker.
void cw_chessboard_set_marker_color(CwChessboard* chessboard, gint col, gint row, CwChessboardColorHandle mahandle)
void cw_chessboard_move_floating_piece(CwChessboard* chessboard, gint handle, gdouble x, gdouble y)
gboolean get_draw_border(void) const
Get the boolean that determines whether or not the chessboard widget draws a border around the chessb...
GtkWidget * cw_chessboard_new(void)
void cw_chessboard_enable_hud_layer(CwChessboard* chessboard, guint hud)
void get_light_square_color(GdkColor& color) const
Retrieve the current background color of the light squares.
gpointer add_arrow(gint begin_col, gint begin_row, gint end_col, gint end_row, GdkColor const& color)
Draw an arrow on the board.
virtual void draw_king(cairo_t* cr, gdouble x, gdouble y, gdouble sside, gboolean white)
Called to draw a king.
virtual void draw_hud_layer(cairo_t* cr, gint sside, guint hud)
Draw the HUD layer.
void cw_chessboard_set_active_turn_indicator(CwChessboard* chessboard, gboolean white)
gboolean cw_chessboard_get_flip_board(CwChessboard* chessboard)
void move_floating_piece(gint handle, gdouble x, gdouble y)
Move a floating chess piece to a new position.
void cw_chessboard_show_cursor(CwChessboard* chessboard)
gdouble cw_chessboard_get_marker_thickness(CwChessboard* chessboard)
void cw_chessboard_remove_arrow(CwChessboard* chessboard, gpointer ptr)
CwChessboardColorHandle cw_chessboard_get_background_color(CwChessboard* chessboard, gint col, gint row)
void enable_hud_layer(guint hud)
Active a HUD layer.
gdouble get_cursor_thickness(void) const
Get the current cursor thickness as fraction of sside.
void cw_chessboard_get_background_colors(CwChessboard* chessboard, CwChessboardColorHandle* handles)
void cw_chessboard_set_background_color(CwChessboard* chessboard, gint col, gint row, CwChessboardColorHandle bghandle)
void set_white_fill_color(GdkColor const& color)
Set the fill color of the white chess pieces.
gboolean get_flip_board(void) const
Get the boolean which determines whether white is playing bottom up or top down.
void set_black_fill_color(GdkColor const& color)
Set the fill color of the black chess pieces.
void get_cursor_color(GdkColor& color) const
Get the current cursor color.
CWCHESSBOARD_INLINE CwChessboardColorHandle cw_chessboard_allocate_color_handle(CwChessboard* chessboard, GdkColor const* color)
void cw_chessboard_set_light_square_color(CwChessboard* chessboard, GdkColor const* color)
void set_flip_board(gboolean flip)
Set the boolean which determines whether white is playing bottom up or top down.
void get_white_fill_color(GdkColor& color) const
Retrieve the current fill color of the white chess pieces.
ColorHandle allocate_color_handle_rgb(gdouble red, gdouble green, gdouble blue)
Allocate a new ColorHandle.
void get_black_fill_color(GdkColor& color) const
Retrieve the current fill color of the black chess pieces.
void cw_chessboard_remove_floating_piece(CwChessboard* chessboard, gint handle)
void cw_chessboard_get_black_fill_color(CwChessboard* chessboard, GdkColor* color)
virtual void on_cursor_left_chessboard(gint prev_col, gint prev_row)
Called when the mouse pointer left the chessboard.

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.