]> Pileus Git - ~andy/gtk/blob - gdk/quartz/GdkQuartzView.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gdk / quartz / GdkQuartzView.c
1 /* GdkQuartzView.m
2  *
3  * Copyright (C) 2005-2007 Imendio AB
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #import "GdkQuartzView.h"
20 #include "gdkquartzwindow.h"
21 #include "gdkprivate-quartz.h"
22 #include "gdkquartz.h"
23
24 @implementation GdkQuartzView
25
26 -(void)dealloc
27 {
28   if (trackingRect)
29     {
30       [self removeTrackingRect:trackingRect];
31       trackingRect = 0;
32     }
33
34   [super dealloc];
35 }
36
37 -(void)setGdkWindow:(GdkWindow *)window
38 {
39   gdk_window = window;
40 }
41
42 -(GdkWindow *)gdkWindow
43 {
44   return gdk_window;
45 }
46
47 -(NSTrackingRectTag)trackingRect
48 {
49   return trackingRect;
50 }
51
52 -(BOOL)isFlipped
53 {
54   return YES;
55 }
56
57 -(BOOL)isOpaque
58 {
59   if (GDK_WINDOW_DESTROYED (gdk_window))
60     return YES;
61
62   /* A view is opaque if its GdkWindow doesn't have the RGBA visual */
63   return gdk_window_get_visual (gdk_window) != gdk_screen_get_rgba_visual (_gdk_screen);
64 }
65
66 -(void)drawRect:(NSRect)rect 
67 {
68   GdkRectangle gdk_rect;
69   GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (gdk_window->impl);
70   const NSRect *drawn_rects;
71   NSInteger count;
72   int i;
73   cairo_region_t *region;
74
75   if (GDK_WINDOW_DESTROYED (gdk_window))
76     return;
77
78   if (!(gdk_window->event_mask & GDK_EXPOSURE_MASK))
79     return;
80
81   if (NSEqualRects (rect, NSZeroRect))
82     return;
83
84   if (!GDK_WINDOW_IS_MAPPED (gdk_window))
85     {
86       /* If the window is not yet mapped, clip_region_with_children
87        * will be empty causing the usual code below to draw nothing.
88        * To not see garbage on the screen, we draw an aesthetic color
89        * here. The garbage would be visible if any widget enabled
90        * the NSView's CALayer in order to add sublayers for custom
91        * native rendering.
92        */
93       [NSGraphicsContext saveGraphicsState];
94
95       [[NSColor windowBackgroundColor] setFill];
96       [NSBezierPath fillRect:rect];
97
98       [NSGraphicsContext restoreGraphicsState];
99
100       return;
101     }
102
103   /* Clear our own bookkeeping of regions that need display */
104   if (impl->needs_display_region)
105     {
106       cairo_region_destroy (impl->needs_display_region);
107       impl->needs_display_region = NULL;
108     }
109
110   [self getRectsBeingDrawn:&drawn_rects count:&count];
111   region = cairo_region_create ();
112   
113   for (i = 0; i < count; i++)
114     {
115       gdk_rect.x = drawn_rects[i].origin.x;
116       gdk_rect.y = drawn_rects[i].origin.y;
117       gdk_rect.width = drawn_rects[i].size.width;
118       gdk_rect.height = drawn_rects[i].size.height;
119
120       cairo_region_union_rectangle (region, &gdk_rect);
121     }
122
123   impl->in_paint_rect_count++;
124   _gdk_window_process_updates_recurse (gdk_window, region);
125   impl->in_paint_rect_count--;
126
127   cairo_region_destroy (region);
128
129   if (needsInvalidateShadow)
130     {
131       [[self window] invalidateShadow];
132       needsInvalidateShadow = NO;
133     }
134 }
135
136 -(void)setNeedsInvalidateShadow:(BOOL)invalidate
137 {
138   needsInvalidateShadow = invalidate;
139 }
140
141 /* For information on setting up tracking rects properly, see here:
142  * http://developer.apple.com/documentation/Cocoa/Conceptual/EventOverview/EventOverview.pdf
143  */
144 -(void)updateTrackingRect
145 {
146   GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (gdk_window->impl);
147   NSRect rect;
148
149   if (!impl->toplevel)
150     return;
151
152   if (trackingRect)
153     {
154       [self removeTrackingRect:trackingRect];
155       trackingRect = 0;
156     }
157
158   if (!impl->toplevel)
159     return;
160
161   /* Note, if we want to set assumeInside we can use:
162    * NSPointInRect ([[self window] convertScreenToBase:[NSEvent mouseLocation]], rect)
163    */
164
165   rect = [self bounds];
166   trackingRect = [self addTrackingRect:rect
167                                  owner:self
168                               userData:nil
169                           assumeInside:NO];
170 }
171
172 -(void)viewDidMoveToWindow
173 {
174   if (![self window]) /* We are destroyed already */
175       return;
176
177   [self updateTrackingRect];
178 }
179
180 -(void)viewWillMoveToWindow:(NSWindow *)newWindow
181 {
182   if (newWindow == nil && trackingRect)
183     {
184       [self removeTrackingRect:trackingRect];
185       trackingRect = 0;
186     }
187 }
188
189 -(void)setFrame:(NSRect)frame
190 {
191   [super setFrame:frame];
192
193   if ([self window])
194     [self updateTrackingRect];
195 }
196
197 @end