Commit 6bdf3a6a authored by Colomban Wendling's avatar Colomban Wendling

OVCCGtk: port the board widget to GTK3

parent 2edf7aaa
...@@ -100,13 +100,17 @@ static void ovccgtk_board_get_property (GObject *object, ...@@ -100,13 +100,17 @@ static void ovccgtk_board_get_property (GObject *object,
guint prop_id, guint prop_id,
GValue *value, GValue *value,
GParamSpec *pspec); GParamSpec *pspec);
static void ovccgtk_board_realize (GtkWidget *self); static void ovccgtk_board_realize (GtkWidget *self);
static void ovccgtk_board_size_request (GtkWidget *widget, static void ovccgtk_board_get_preferred_width (GtkWidget *widget,
GtkRequisition *requisition); gint *minimum_width,
gint *natural_width);
static void ovccgtk_board_get_preferred_height (GtkWidget *widget,
gint *minimum_height,
gint *natural_height);
static void ovccgtk_board_size_allocate (GtkWidget *widget, static void ovccgtk_board_size_allocate (GtkWidget *widget,
GtkAllocation *requisition); GtkAllocation *requisition);
static gboolean ovccgtk_board_expose (GtkWidget *self, static gboolean ovccgtk_board_draw (GtkWidget *self,
GdkEventExpose *event); cairo_t *cr);
static gboolean ovccgtk_board_button_release_event (GtkWidget *widget, static gboolean ovccgtk_board_button_release_event (GtkWidget *widget,
GdkEventButton *event); GdkEventButton *event);
static gboolean ovccgtk_board_button_press_event (GtkWidget *widget, static gboolean ovccgtk_board_button_press_event (GtkWidget *widget,
...@@ -115,9 +119,6 @@ static gboolean ovccgtk_board_scroll (GtkWidget *widget, ...@@ -115,9 +119,6 @@ static gboolean ovccgtk_board_scroll (GtkWidget *widget,
GdkEventScroll *event); GdkEventScroll *event);
static gboolean ovccgtk_board_key_press (GtkWidget *widget, static gboolean ovccgtk_board_key_press (GtkWidget *widget,
GdkEventKey *event); GdkEventKey *event);
static void ovccgtk_board_set_scroll_adjustments (OVCCGtkBoard *board,
GtkAdjustment *hadjustment,
GtkAdjustment *vadjustment);
G_DEFINE_TYPE (OVCCGtkBoard, ovccgtk_board, GTK_TYPE_DRAWING_AREA) G_DEFINE_TYPE (OVCCGtkBoard, ovccgtk_board, GTK_TYPE_DRAWING_AREA)
...@@ -131,15 +132,15 @@ static void ovccgtk_board_class_init (OVCCGtkBoardClass *klass) ...@@ -131,15 +132,15 @@ static void ovccgtk_board_class_init (OVCCGtkBoardClass *klass)
object_class->set_property = ovccgtk_board_set_property; object_class->set_property = ovccgtk_board_set_property;
object_class->get_property = ovccgtk_board_get_property; object_class->get_property = ovccgtk_board_get_property;
widget_class->realize = ovccgtk_board_realize; widget_class->realize = ovccgtk_board_realize;
widget_class->size_request = ovccgtk_board_size_request; widget_class->get_preferred_width = ovccgtk_board_get_preferred_width;
widget_class->get_preferred_height = ovccgtk_board_get_preferred_height;
widget_class->size_allocate = ovccgtk_board_size_allocate; widget_class->size_allocate = ovccgtk_board_size_allocate;
widget_class->expose_event = ovccgtk_board_expose; widget_class->draw = ovccgtk_board_draw;
widget_class->button_release_event = ovccgtk_board_button_release_event; widget_class->button_release_event = ovccgtk_board_button_release_event;
widget_class->button_press_event = ovccgtk_board_button_press_event; widget_class->button_press_event = ovccgtk_board_button_press_event;
widget_class->scroll_event = ovccgtk_board_scroll; widget_class->scroll_event = ovccgtk_board_scroll;
widget_class->key_press_event = ovccgtk_board_key_press; widget_class->key_press_event = ovccgtk_board_key_press;
klass->add_tile = NULL; klass->add_tile = NULL;
klass->set_scroll_adjustments = ovccgtk_board_set_scroll_adjustments;
g_object_class_install_property ( g_object_class_install_property (
object_class, PROP_BOARD, object_class, PROP_BOARD,
...@@ -240,16 +241,6 @@ static void ovccgtk_board_class_init (OVCCGtkBoardClass *klass) ...@@ -240,16 +241,6 @@ static void ovccgtk_board_class_init (OVCCGtkBoardClass *klass)
OVCC_TYPE_POSITION, OVCC_TYPE_POSITION,
OVCC_TYPE_TILE_OBJECT, OVCC_TYPE_TILE_OBJECT,
OVCC_TYPE_PAWN_KIND); OVCC_TYPE_PAWN_KIND);
/*widget_class->set_scroll_adjustments_signal =
g_signal_new ("set-scroll-adjustments",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (OVCCGtkBoardClass, set_scroll_adjustments),
NULL, NULL,
_gtk_marshal_VOID__OBJECT_OBJECT,
G_TYPE_NONE, 2,
GTK_TYPE_ADJUSTMENT,
GTK_TYPE_ADJUSTMENT);*/
g_type_class_add_private ((gpointer)klass, sizeof(OVCCGtkBoardPrivate)); g_type_class_add_private ((gpointer)klass, sizeof(OVCCGtkBoardPrivate));
} }
...@@ -283,14 +274,6 @@ ovccgtk_board_finalize (GObject *object) ...@@ -283,14 +274,6 @@ ovccgtk_board_finalize (GObject *object)
G_OBJECT_CLASS (ovccgtk_board_parent_class)->finalize (object); G_OBJECT_CLASS (ovccgtk_board_parent_class)->finalize (object);
} }
static void
ovccgtk_board_set_scroll_adjustments (OVCCGtkBoard *board,
GtkAdjustment *hadjustment,
GtkAdjustment *vadjustment)
{
}
static void static void
ovccgtk_board_set_property (GObject *object, ovccgtk_board_set_property (GObject *object,
guint prop_id, guint prop_id,
...@@ -355,8 +338,6 @@ ovccgtk_board_realize (GtkWidget *widget) ...@@ -355,8 +338,6 @@ ovccgtk_board_realize (GtkWidget *widget)
GdkWindowAttr attributes; GdkWindowAttr attributes;
GtkAllocation allocation; GtkAllocation allocation;
GdkWindow *window; GdkWindow *window;
GtkStyle *style;
guint attributes_mask;
gtk_widget_get_allocation (widget, &allocation); gtk_widget_get_allocation (widget, &allocation);
attributes.window_type = GDK_WINDOW_CHILD; attributes.window_type = GDK_WINDOW_CHILD;
...@@ -370,16 +351,13 @@ ovccgtk_board_realize (GtkWidget *widget) ...@@ -370,16 +351,13 @@ ovccgtk_board_realize (GtkWidget *widget)
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK; GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK;
attributes_mask = GDK_WA_X | GDK_WA_Y;
window = gdk_window_new (gtk_widget_get_parent_window (widget), window = gdk_window_new (gtk_widget_get_parent_window (widget),
&attributes, attributes_mask); &attributes, GDK_WA_X | GDK_WA_Y);
gdk_window_set_user_data (window, widget); gdk_window_set_user_data (window, widget);
gtk_widget_set_window (widget, window); gtk_widget_set_window (widget, window);
style = gtk_style_attach (gtk_widget_get_style (widget), window); gtk_style_context_set_background (gtk_widget_get_style_context (widget),
gtk_widget_set_style (widget, style); window);
gtk_style_set_background (style, window, GTK_STATE_NORMAL);
gtk_widget_set_realized (widget, TRUE); gtk_widget_set_realized (widget, TRUE);
gtk_widget_set_can_focus (widget, TRUE); gtk_widget_set_can_focus (widget, TRUE);
...@@ -429,10 +407,12 @@ auto_update_zoom (OVCCGtkBoard *self, ...@@ -429,10 +407,12 @@ auto_update_zoom (OVCCGtkBoard *self,
} }
static void static void
ovccgtk_board_size_request (GtkWidget *widget, ovccgtk_board_preferred_size (OVCCGtkBoard *self,
GtkRequisition *requisition) gint *minimum_width,
gint *natural_width,
gint *minimum_height,
gint *natural_height)
{ {
OVCCGtkBoard *self = OVCCGTK_BOARD (widget);
GtkAllocation allocation; GtkAllocation allocation;
gint width, height; gint width, height;
gint tile_size; gint tile_size;
...@@ -440,14 +420,39 @@ ovccgtk_board_size_request (GtkWidget *widget, ...@@ -440,14 +420,39 @@ ovccgtk_board_size_request (GtkWidget *widget,
ovcc_board_get_bounds (self->priv->board, NULL, NULL, NULL, NULL, ovcc_board_get_bounds (self->priv->board, NULL, NULL, NULL, NULL,
&width, &height); &width, &height);
g_debug ("req (board size): [%u,%u]", width, height); g_debug ("req (board size): [%u,%u]", width, height);
gtk_widget_get_allocation (widget, &allocation); gtk_widget_get_allocation (GTK_WIDGET (self), &allocation);
auto_update_zoom (self, &allocation); auto_update_zoom (self, &allocation);
tile_size = (IS_USER_ZOOM (self)) tile_size = (IS_USER_ZOOM (self))
? (gint)(TILE_SIZE * self->priv->zoom) ? (gint)(TILE_SIZE * self->priv->zoom)
: TILE_SIZE_MIN; : TILE_SIZE_MIN;
requisition->width = (width * (tile_size + 1)) + (2 * self->priv->margin); if (minimum_width)
requisition->height = (height * (tile_size + 1)) + (2 * self->priv->margin); *minimum_width = (width * (tile_size + 1)) + (2 * self->priv->margin);
g_debug ("req: [%u,%u]", requisition->width, requisition->height); if (natural_width)
*natural_width = (width * (TILE_SIZE + 1)) + (2 * self->priv->margin);
if (minimum_height)
*minimum_height = (height * (tile_size + 1)) + (2 * self->priv->margin);
if (natural_height)
*natural_height = (height * (TILE_SIZE + 1)) + (2 * self->priv->margin);
}
static void
ovccgtk_board_get_preferred_width (GtkWidget *widget,
gint *minimum_width,
gint *natural_width)
{
ovccgtk_board_preferred_size (OVCCGTK_BOARD (widget),
minimum_width, natural_width,
NULL, NULL);
}
static void
ovccgtk_board_get_preferred_height (GtkWidget *widget,
gint *minimum_height,
gint *natural_height)
{
ovccgtk_board_preferred_size (OVCCGTK_BOARD (widget),
NULL, NULL,
minimum_height, natural_height);
} }
static void static void
...@@ -782,14 +787,14 @@ draw_tile_cb (OVCCBoard *board, ...@@ -782,14 +787,14 @@ draw_tile_cb (OVCCBoard *board,
static void static void
ovccgtk_cairo_pattern_add_color_stop_color_a (cairo_pattern_t *pattern, ovccgtk_cairo_pattern_add_color_stop_color_a (cairo_pattern_t *pattern,
gdouble offset, gdouble offset,
const GdkColor *color, const GdkRGBA *color,
gdouble alpha) gdouble alpha)
{ {
cairo_pattern_add_color_stop_rgba (pattern, cairo_pattern_add_color_stop_rgba (pattern,
offset, offset,
color->red / 65535.0, color->red,
color->green / 65535.0, color->green,
color->blue / 65535.0, color->blue,
alpha); alpha);
} }
...@@ -798,10 +803,14 @@ paint_cursor (OVCCGtkBoard *self) ...@@ -798,10 +803,14 @@ paint_cursor (OVCCGtkBoard *self)
{ {
gint dest_x, dest_y; gint dest_x, dest_y;
gint size = (gint)(TILE_SIZE * self->priv->zoom); gint size = (gint)(TILE_SIZE * self->priv->zoom);
GtkStyle *style = gtk_widget_get_style (GTK_WIDGET (self)); GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (self));
GdkColor *color = &style->bg[GTK_STATE_SELECTED]; GdkRGBA color;
cairo_pattern_t *pattern; cairo_pattern_t *pattern;
gtk_style_context_get_background_color (context,
GTK_STATE_FLAG_SELECTED,
&color);
ovccgtk_board_board_to_widget_coords (self, self->priv->cursor_pos.x, ovccgtk_board_board_to_widget_coords (self, self->priv->cursor_pos.x,
self->priv->cursor_pos.y, self->priv->cursor_pos.y,
&dest_x, &dest_y); &dest_x, &dest_y);
...@@ -812,36 +821,31 @@ paint_cursor (OVCCGtkBoard *self) ...@@ -812,36 +821,31 @@ paint_cursor (OVCCGtkBoard *self)
dest_x + size / 2, dest_x + size / 2,
dest_y + size / 2, dest_y + size / 2,
0.0); 0.0);
ovccgtk_cairo_pattern_add_color_stop_color_a (pattern, 0.0, color, 0.25); ovccgtk_cairo_pattern_add_color_stop_color_a (pattern, 0.0, &color, 0.25);
ovccgtk_cairo_pattern_add_color_stop_color_a (pattern, 1.0, color, 0.0); ovccgtk_cairo_pattern_add_color_stop_color_a (pattern, 1.0, &color, 0.0);
cairo_set_source (self->priv->cr, pattern); cairo_set_source (self->priv->cr, pattern);
cairo_pattern_destroy (pattern); cairo_pattern_destroy (pattern);
cairo_rectangle (self->priv->cr, cairo_rectangle (self->priv->cr,
dest_x - 0.5, dest_y - 0.5, dest_x - 0.5, dest_y - 0.5,
size + 1, size + 1); size + 1, size + 1);
cairo_fill_preserve (self->priv->cr); cairo_fill_preserve (self->priv->cr);
gdk_cairo_set_source_color (self->priv->cr, color); gdk_cairo_set_source_rgba (self->priv->cr, &color);
cairo_stroke (self->priv->cr); cairo_stroke (self->priv->cr);
} }
static gboolean static gboolean
ovccgtk_board_expose (GtkWidget *widget, ovccgtk_board_draw (GtkWidget *widget,
GdkEventExpose *event) cairo_t *cr)
{ {
if (gtk_widget_is_drawable (widget)) { OVCCGtkBoard *self = (OVCCGtkBoard *) widget;
OVCCGtkBoard *self = (OVCCGtkBoard *) widget;
self->priv->cr = cr;
self->priv->cr = gdk_cairo_create (GDK_DRAWABLE (event->window)); cairo_set_line_width (self->priv->cr, 1.0);
cairo_set_line_width (self->priv->cr, 1.0);
gdk_cairo_rectangle (self->priv->cr, &event->area); ovcc_board_foreach (self->priv->board, draw_tile_cb, self);
cairo_clip (self->priv->cr); paint_cursor (self);
ovcc_board_foreach (self->priv->board, draw_tile_cb, self); self->priv->cr = NULL;
paint_cursor (self);
cairo_destroy (self->priv->cr);
self->priv->cr = NULL;
}
return TRUE; return TRUE;
} }
......
...@@ -63,9 +63,6 @@ struct _OVCCGtkBoardClass ...@@ -63,9 +63,6 @@ struct _OVCCGtkBoardClass
const OVCCPosition *pos, const OVCCPosition *pos,
OVCCTileObject *obj, OVCCTileObject *obj,
OVCCPawnKind kind); OVCCPawnKind kind);
void (*set_scroll_adjustments) (OVCCGtkBoard *board,
GtkAdjustment *hadjustment,
GtkAdjustment *vadjustment);
}; };
......
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