1 From 08f7cca62c0a810d1bd5eaf52813b9ecb776478e Mon Sep 17 00:00:00 2001
2 From: Michael Natterer <mitch@gimp.org>
3 Date: Fri, 1 Mar 2013 12:39:41 +0100
4 Subject: [PATCH 44/68] gtknsview: clip drawRect to a parent GtkViewport's
7 not quite perfect yet, but getting there...
9 gtk/gtknsview.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++------
10 1 file changed, 109 insertions(+), 12 deletions(-)
12 diff --git a/gtk/gtknsview.c b/gtk/gtknsview.c
13 index a19a94e..84d758f 100644
17 #include <objc/runtime.h>
19 #include "gtknsview.h"
20 +#include "gtkviewport.h"
21 #include "gtkprivate.h"
24 @@ -52,6 +53,7 @@ struct _GtkNSViewPrivate
25 GTK_TYPE_NS_VIEW, GtkNSViewPrivate))
28 +static void gtk_ns_view_constructed (GObject *object);
29 static void gtk_ns_view_finalize (GObject *object);
30 static void gtk_ns_view_set_property (GObject *object,
32 @@ -89,7 +91,6 @@ static gboolean gtk_ns_view_forward_event (GtkWidget *widget,
34 G_DEFINE_TYPE (GtkNSView, gtk_ns_view, GTK_TYPE_WIDGET)
38 gtk_ns_view_class_init (GtkNSViewClass *klass)
40 @@ -98,6 +99,7 @@ gtk_ns_view_class_init (GtkNSViewClass *klass)
42 g_type_class_add_private (klass, sizeof (GtkNSViewPrivate));
44 + object_class->constructed = gtk_ns_view_constructed;
45 object_class->finalize = gtk_ns_view_finalize;
46 object_class->set_property = gtk_ns_view_set_property;
47 object_class->get_property = gtk_ns_view_get_property;
48 @@ -136,6 +138,111 @@ gtk_ns_view_init (GtkNSView *ns_view)
49 gtk_widget_set_has_window (GTK_WIDGET (ns_view), FALSE);
52 +@implementation NSView (myDrawRect)
53 +- (void) myDrawRect: (NSRect) dirtyRect
56 + GtkWidget *viewport;
59 + g_printerr ("drawRect called\n");
62 + ns_view = (GtkNSView *) objc_getAssociatedObject (self, "gtknsview");
66 + [self myDrawRect: dirtyRect];
70 + viewport = gtk_widget_get_ancestor (GTK_WIDGET (ns_view), GTK_TYPE_VIEWPORT);
74 + CGContextRef cg_context = [[NSGraphicsContext currentContext] graphicsPort];
75 + GtkAllocation viewport_allocation;
79 + g_printerr ("drawRect called on gtknsview in gtkviewport\n");
82 + gtk_widget_get_allocation (viewport, &viewport_allocation);
84 + if (gtk_viewport_get_shadow_type (GTK_VIEWPORT (viewport)) != GTK_SHADOW_NONE)
86 + GtkStyle *style = gtk_widget_get_style (viewport);
88 + viewport_allocation.x += style->xthickness;
89 + viewport_allocation.y += style->ythickness;
90 + viewport_allocation.width -= 2 * style->xthickness;
91 + viewport_allocation.height -= 2 * style->ythickness;
94 + gtk_widget_translate_coordinates (viewport, GTK_WIDGET (ns_view),
95 + viewport_allocation.x,
96 + viewport_allocation.y,
97 + &viewport_allocation.x,
98 + &viewport_allocation.y);
100 + rect.origin.x = viewport_allocation.x;
101 + rect.origin.y = viewport_allocation.y;
102 + rect.size.width = viewport_allocation.width;
103 + rect.size.height = viewport_allocation.height;
105 + CGContextSaveGState (cg_context);
106 + CGContextClipToRect (cg_context, rect);
108 + [self myDrawRect: dirtyRect];
110 + CGContextRestoreGState (cg_context);
114 + [self myDrawRect: dirtyRect];
120 +gtk_ns_view_constructed (GObject *object)
122 + GtkNSView *ns_view = GTK_NS_VIEW (object);
123 + Method original_drawRect;
124 + Method my_drawRect;
126 + G_OBJECT_CLASS (gtk_ns_view_parent_class)->constructed (object);
128 + gtk_widget_set_can_focus (GTK_WIDGET (ns_view),
129 + [ns_view->priv->view acceptsFirstResponder]);
132 + g_printerr ("%s can focus: %d\n",
133 + class_getName ([ns_view->priv->view class]),
134 + gtk_widget_get_can_focus (GTK_WIDGET (ns_view)));
137 + original_drawRect = class_getInstanceMethod ([ns_view->priv->view class],
138 + @selector (drawRect:));
139 + my_drawRect = class_getInstanceMethod ([ns_view->priv->view class],
140 + @selector (myDrawRect:));
142 + if (class_addMethod ([ns_view->priv->view class],
143 + @selector (myDrawRect:),
144 + method_getImplementation (original_drawRect),
145 + method_getTypeEncoding (original_drawRect)))
147 + class_replaceMethod ([ns_view->priv->view class],
148 + @selector (drawRect:),
149 + method_getImplementation (my_drawRect),
150 + method_getTypeEncoding (my_drawRect));
153 + objc_setAssociatedObject (ns_view->priv->view, "gtknsview", (id) ns_view,
154 + OBJC_ASSOCIATION_ASSIGN);
158 gtk_ns_view_finalize (GObject *object)
160 @@ -163,17 +270,7 @@ gtk_ns_view_set_property (GObject *object,
162 ns_view->priv->view = g_value_get_pointer (value);
163 if (ns_view->priv->view)
165 - [ns_view->priv->view retain];
166 - gtk_widget_set_can_focus (GTK_WIDGET (ns_view),
167 - [ns_view->priv->view acceptsFirstResponder]);
170 - g_printerr ("%s can focus: %d\n",
171 - class_getName ([ns_view->priv->view class]),
172 - gtk_widget_get_can_focus (GTK_WIDGET (ns_view)));
175 + [ns_view->priv->view retain];
180 1.7.10.2 (Apple Git-33)