colorplane: Port to be a no-window widget

This commit is contained in:
Benjamin Otte 2016-11-03 00:11:39 +01:00
parent 0508526658
commit 7e9796568d
2 changed files with 75 additions and 10 deletions

View File

@ -32,6 +32,7 @@ struct _GtkColorPlanePrivate
GtkAdjustment *s_adj;
GtkAdjustment *v_adj;
GdkWindow *input_window;
cairo_surface_t *surface;
GtkGesture *drag_gesture;
@ -45,7 +46,7 @@ enum {
PROP_V_ADJUSTMENT
};
G_DEFINE_TYPE_WITH_PRIVATE (GtkColorPlane, gtk_color_plane, GTK_TYPE_DRAWING_AREA)
G_DEFINE_TYPE_WITH_PRIVATE (GtkColorPlane, gtk_color_plane, GTK_TYPE_WIDGET)
static void
sv_to_xy (GtkColorPlane *plane,
@ -176,19 +177,85 @@ static void
plane_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
GTK_WIDGET_CLASS (gtk_color_plane_parent_class)->size_allocate (widget, allocation);
create_surface (GTK_COLOR_PLANE (widget));
if (gtk_widget_get_realized (widget))
gdk_window_move_resize (plane->priv->input_window,
allocation->x, allocation->y,
allocation->width, allocation->height);
create_surface (plane);
}
static void
plane_realize (GtkWidget *widget)
{
GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
GtkColorPlanePrivate *priv = gtk_color_plane_get_instance_private (plane);
GtkAllocation allocation;
GTK_WIDGET_CLASS (gtk_color_plane_parent_class)->realize (widget);
gtk_widget_get_allocation (widget, &allocation);
priv->input_window = gdk_window_new_input (gtk_widget_get_window (widget),
gtk_widget_get_events (widget)
| GDK_KEY_PRESS_MASK
| GDK_TOUCH_MASK
| GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
| GDK_POINTER_MOTION_MASK,
&allocation);
gtk_widget_register_window (widget, priv->input_window);
gtk_gesture_set_window (priv->drag_gesture, priv->input_window);
gtk_gesture_set_window (priv->long_press_gesture, priv->input_window);
create_surface (GTK_COLOR_PLANE (widget));
}
static void
plane_unrealize (GtkWidget *widget)
{
GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
GtkColorPlanePrivate *priv = gtk_color_plane_get_instance_private (plane);
g_clear_pointer (&priv->surface, cairo_surface_destroy);
gtk_gesture_set_window (priv->drag_gesture, NULL);
gtk_gesture_set_window (priv->long_press_gesture, NULL);
gtk_widget_unregister_window (widget, priv->input_window);
gdk_window_destroy (priv->input_window);
priv->input_window = NULL;
GTK_WIDGET_CLASS (gtk_color_plane_parent_class)->unrealize (widget);
}
static void
plane_map (GtkWidget *widget)
{
GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
GtkColorPlanePrivate *priv = gtk_color_plane_get_instance_private (plane);
gdk_window_show (priv->input_window);
GTK_WIDGET_CLASS (gtk_color_plane_parent_class)->map (widget);
}
static void
plane_unmap (GtkWidget *widget)
{
GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
GtkColorPlanePrivate *priv = gtk_color_plane_get_instance_private (plane);
GTK_WIDGET_CLASS (gtk_color_plane_parent_class)->unmap (widget);
gdk_window_hide (priv->input_window);
}
static void
set_cross_cursor (GtkWidget *widget,
gboolean enabled)
@ -396,13 +463,8 @@ gtk_color_plane_init (GtkColorPlane *plane)
plane->priv = gtk_color_plane_get_instance_private (plane);
gtk_widget_set_has_window (GTK_WIDGET (plane), TRUE);
gtk_widget_set_has_window (GTK_WIDGET (plane), FALSE);
gtk_widget_set_can_focus (GTK_WIDGET (plane), TRUE);
gtk_widget_set_events (GTK_WIDGET (plane), GDK_KEY_PRESS_MASK
| GDK_TOUCH_MASK
| GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
| GDK_POINTER_MOTION_MASK);
atk_obj = gtk_widget_get_accessible (GTK_WIDGET (plane));
if (GTK_IS_ACCESSIBLE (atk_obj))
@ -501,6 +563,9 @@ gtk_color_plane_class_init (GtkColorPlaneClass *class)
widget_class->draw = plane_draw;
widget_class->size_allocate = plane_size_allocate;
widget_class->realize = plane_realize;
widget_class->unrealize = plane_unrealize;
widget_class->map = plane_map;
widget_class->unmap = plane_unmap;
widget_class->key_press_event = plane_key_press;
g_object_class_install_property (object_class,

View File

@ -37,14 +37,14 @@ typedef struct _GtkColorPlanePrivate GtkColorPlanePrivate;
struct _GtkColorPlane
{
GtkDrawingArea parent_instance;
GtkWidget parent_instance;
GtkColorPlanePrivate *priv;
};
struct _GtkColorPlaneClass
{
GtkDrawingAreaClass parent_class;
GtkWidgetClass parent_class;
/* Padding for future expansion */
void (*_gtk_reserved1) (void);