Commit 9ab55947 authored by Colomban Wendling's avatar Colomban Wendling

OVCCGtk: allow to remove the board object from the board widget

This makes OVCCGtkBoard a more normal widget by allowing to change the
OVCCBoard it displays, avoiding the need to create and destroy the
widget.
parent 5904cd62
......@@ -267,11 +267,7 @@ ovccgtk_board_finalize (GObject *object)
{
OVCCGtkBoard *self = OVCCGTK_BOARD (object);
if (self->priv->board) {
g_signal_handler_disconnect (self->priv->board,
self->priv->sig_board_add_tile);
g_object_unref (self->priv->board);
}
ovccgtk_board_set_board (self, NULL);
G_OBJECT_CLASS (ovccgtk_board_parent_class)->finalize (object);
}
......@@ -391,7 +387,7 @@ static void
auto_update_zoom (OVCCGtkBoard *self,
GtkAllocation *allocation)
{
if (! IS_USER_ZOOM (self)) {
if (self->priv->board && ! IS_USER_ZOOM (self)) {
gint width, height;
gint a_width, a_height;
gdouble zoom;
......@@ -417,12 +413,14 @@ ovccgtk_board_preferred_size (OVCCGtkBoard *self,
gint *natural_height)
{
GtkAllocation allocation;
gint width, height;
gint width = 0, height = 0;
gint tile_size;
ovcc_board_get_bounds (self->priv->board, NULL, NULL, NULL, NULL,
&width, &height);
g_debug ("req (board size): [%u,%u]", width, height);
if (self->priv->board) {
ovcc_board_get_bounds (self->priv->board, NULL, NULL, NULL, NULL,
&width, &height);
g_debug ("req (board size): [%u,%u]", width, height);
}
gtk_widget_get_allocation (GTK_WIDGET (self), &allocation);
auto_update_zoom (self, &allocation);
tile_size = (IS_USER_ZOOM (self))
......@@ -842,13 +840,15 @@ ovccgtk_board_draw (GtkWidget *widget,
{
OVCCGtkBoard *self = (OVCCGtkBoard *) widget;
self->priv->cr = cr;
cairo_set_line_width (self->priv->cr, 1.0);
ovcc_board_foreach (self->priv->board, draw_tile_cb, self);
paint_cursor (self);
self->priv->cr = NULL;
if (self->priv->board) {
self->priv->cr = cr;
cairo_set_line_width (self->priv->cr, 1.0);
ovcc_board_foreach (self->priv->board, draw_tile_cb, self);
paint_cursor (self);
self->priv->cr = NULL;
}
return TRUE;
}
......@@ -1037,13 +1037,15 @@ ovccgtk_board_widget_to_board_coords (OVCCGtkBoard *self,
{
GtkAllocation allocation;
gint margin_left, margin_right;
gint base_x, base_y;
gint base_x = 0, base_y = 0;
const gdouble coeff = floor (TILE_SIZE * self->priv->zoom) + 1;
gint width, height;
gint width = 0, height = 0;
gtk_widget_get_allocation (GTK_WIDGET (self), &allocation);
ovcc_board_get_bounds (self->priv->board,
&base_x, &base_y, NULL, NULL, &width, &height);
if (self->priv->board) {
ovcc_board_get_bounds (self->priv->board,
&base_x, &base_y, NULL, NULL, &width, &height);
}
margin_left = (gint)((allocation.width - (width * coeff)) / 2);
margin_right = (gint)((allocation.height - (height * coeff)) / 2);
*board_x = (gint)(floor ((widget_x - margin_left) / coeff) + base_x);
......@@ -1072,13 +1074,15 @@ ovccgtk_board_board_to_widget_coords (OVCCGtkBoard *self,
{
GtkAllocation allocation;
gint margin_left, margin_right;
gint base_x, base_y;
gint base_x = 0, base_y = 0;
const gdouble coeff = floor (TILE_SIZE * self->priv->zoom) + 1;
gint width, height;
gint width = 0, height = 0;
gtk_widget_get_allocation (GTK_WIDGET (self), &allocation);
ovcc_board_get_bounds (self->priv->board,
&base_x, &base_y, NULL, NULL, &width, &height);
if (self->priv->board) {
ovcc_board_get_bounds (self->priv->board,
&base_x, &base_y, NULL, NULL, &width, &height);
}
margin_left = (gint)((allocation.width - (width * coeff)) / 2);
margin_right = (gint)((allocation.height - (height * coeff)) / 2);
*widget_x = margin_left + ((-base_x + board_x) * coeff);
......@@ -1105,7 +1109,7 @@ ovccgtk_board_set_board (OVCCGtkBoard *self,
OVCCBoard *board)
{
g_return_if_fail (OVCCGTK_IS_BOARD (self));
g_return_if_fail (board != NULL);
g_return_if_fail (board == NULL || OVCC_IS_BOARD (board));
if (self->priv->board) {
g_signal_handler_disconnect (self->priv->board,
......@@ -1114,18 +1118,20 @@ ovccgtk_board_set_board (OVCCGtkBoard *self,
self->priv->sig_board_pawn_added);
g_signal_handler_disconnect (self->priv->board,
self->priv->sig_board_pawn_removed);
g_object_unref (self->priv->board);
g_clear_object (&self->priv->board);
}
if (board) {
self->priv->board = g_object_ref (board);
self->priv->sig_board_add_tile =
g_signal_connect (board, "tile-added",
G_CALLBACK (on_board_tile_added), self);
self->priv->sig_board_pawn_added =
g_signal_connect (board, "pawn-added",
G_CALLBACK (on_board_pawn_added), self);
self->priv->sig_board_pawn_removed =
g_signal_connect (board, "pawn-removed",
G_CALLBACK (on_board_pawn_removed), self);
}
self->priv->board = g_object_ref (board);
self->priv->sig_board_add_tile =
g_signal_connect (board, "tile-added",
G_CALLBACK (on_board_tile_added), self);
self->priv->sig_board_pawn_added =
g_signal_connect (board, "pawn-added",
G_CALLBACK (on_board_pawn_added), self);
self->priv->sig_board_pawn_removed =
g_signal_connect (board, "pawn-removed",
G_CALLBACK (on_board_pawn_removed), self);
}
OVCCBoard *
......@@ -1187,13 +1193,15 @@ gboolean
ovccgtk_board_move_cursor (OVCCGtkBoard *self,
const OVCCPosition *pos)
{
gint min_x, min_y, max_x, max_y;
gint min_x = 0, min_y = 0, max_x = 0, max_y = 0;
gboolean moved = FALSE;
g_return_val_if_fail (OVCCGTK_IS_BOARD (self), FALSE);
ovcc_board_get_bounds (self->priv->board,
&min_x, &min_y, &max_x, &max_y, NULL, NULL);
if (self->priv->board) {
ovcc_board_get_bounds (self->priv->board,
&min_x, &min_y, &max_x, &max_y, NULL, NULL);
}
min_x --; min_y --;
/* don't allow moves outside board bounds - 1 */
if (pos->x <= max_x && pos->x >= min_x &&
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment