]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/overlay.mm
The alignment controls are now left-aligned if the floating controls are not shown.
[wxWidgets.git] / src / osx / cocoa / overlay.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/overlay.mm
3 // Purpose: common wxOverlay code
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 2006-10-20
7 // Copyright: (c) wxWidgets team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/overlay.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/dcclient.h"
30 #endif
31
32 #include "wx/dcgraph.h"
33
34 #include "wx/private/overlay.h"
35
36 #ifdef wxHAS_NATIVE_OVERLAY
37
38 // ============================================================================
39 // implementation
40 // ============================================================================
41
42 wxOverlayImpl::wxOverlayImpl()
43 {
44 m_window = NULL ;
45 m_overlayContext = NULL ;
46 m_overlayWindow = NULL ;
47 }
48
49 wxOverlayImpl::~wxOverlayImpl()
50 {
51 Reset();
52 }
53
54 bool wxOverlayImpl::IsOk()
55 {
56 return m_overlayWindow != NULL ;
57 }
58
59 void wxOverlayImpl::CreateOverlayWindow()
60 {
61 if ( m_window )
62 {
63 m_overlayParentWindow = m_window->MacGetTopLevelWindowRef();
64 [m_overlayParentWindow makeKeyAndOrderFront:nil];
65
66 NSView* view = m_window->GetHandle();
67
68 NSPoint viewOriginBase, viewOriginScreen;
69 viewOriginBase = [view convertPoint:NSMakePoint(0, 0) toView:nil];
70 viewOriginScreen = [m_overlayParentWindow convertBaseToScreen:viewOriginBase];
71
72 NSSize viewSize = [view frame].size;
73 if ( [view isFlipped] )
74 viewOriginScreen.y -= viewSize.height;
75
76 m_overlayWindow=[[NSWindow alloc] initWithContentRect:NSMakeRect(viewOriginScreen.x,viewOriginScreen.y,
77 viewSize.width,
78 viewSize.height)
79 styleMask:NSBorderlessWindowMask
80 backing:NSBackingStoreBuffered
81 defer:YES];
82
83 [m_overlayParentWindow addChildWindow:m_overlayWindow ordered:NSWindowAbove];
84 }
85 else
86 {
87 m_overlayParentWindow = NULL ;
88 CGRect cgbounds ;
89 cgbounds = CGDisplayBounds(CGMainDisplayID());
90
91 m_overlayWindow=[[NSWindow alloc] initWithContentRect:NSMakeRect(cgbounds.origin.x,cgbounds.origin.y,
92 cgbounds.size.width,
93 cgbounds.size.height)
94 styleMask:NSBorderlessWindowMask
95 backing:NSBackingStoreBuffered
96 defer:YES];
97 }
98 [m_overlayWindow setOpaque:NO];
99 [m_overlayWindow setIgnoresMouseEvents:YES];
100 [m_overlayWindow setAlphaValue:1.0];
101
102 [m_overlayWindow orderFront:nil];
103 }
104
105 void wxOverlayImpl::Init( wxDC* dc, int x , int y , int width , int height )
106 {
107 wxASSERT_MSG( !IsOk() , _("You cannot Init an overlay twice") );
108
109 m_window = dc->GetWindow();
110 m_x = x ;
111 m_y = y ;
112 if ( dc->IsKindOf( CLASSINFO( wxClientDC ) ))
113 {
114 wxPoint origin = m_window->GetClientAreaOrigin();
115 m_x += origin.x;
116 m_y += origin.y;
117 }
118 m_width = width ;
119 m_height = height ;
120
121 CreateOverlayWindow();
122 wxASSERT_MSG( m_overlayWindow != NULL , _("Couldn't create the overlay window") );
123 m_overlayContext = (CGContextRef) [[m_overlayWindow graphicsContext] graphicsPort];
124 wxASSERT_MSG( m_overlayContext != NULL , _("Couldn't init the context on the overlay window") );
125
126 int ySize = 0;
127 if ( m_window )
128 {
129 NSView* view = m_window->GetHandle();
130 NSSize viewSize = [view frame].size;
131 ySize = viewSize.height;
132 }
133 else
134 {
135 CGRect cgbounds ;
136 cgbounds = CGDisplayBounds(CGMainDisplayID());
137 ySize = cgbounds.size.height;
138
139
140
141 }
142 CGContextTranslateCTM( m_overlayContext, 0, ySize );
143 CGContextScaleCTM( m_overlayContext, 1, -1 );
144 CGContextTranslateCTM( m_overlayContext, -m_x , -m_y );
145 }
146
147 void wxOverlayImpl::BeginDrawing( wxDC* dc)
148 {
149 wxDCImpl *impl = dc->GetImpl();
150 wxGCDCImpl *win_impl = wxDynamicCast(impl,wxGCDCImpl);
151 if (win_impl)
152 {
153 win_impl->SetGraphicsContext( wxGraphicsContext::CreateFromNative( m_overlayContext ) );
154 dc->SetClippingRegion( m_x , m_y , m_width , m_height ) ;
155 }
156 }
157
158 void wxOverlayImpl::EndDrawing( wxDC* dc)
159 {
160 wxDCImpl *impl = dc->GetImpl();
161 wxGCDCImpl *win_impl = wxDynamicCast(impl,wxGCDCImpl);
162 if (win_impl)
163 win_impl->SetGraphicsContext(NULL);
164
165 CGContextFlush( m_overlayContext );
166 }
167
168 void wxOverlayImpl::Clear(wxDC* WXUNUSED(dc))
169 {
170 wxASSERT_MSG( IsOk() , _("You cannot Clear an overlay that is not inited") );
171 CGRect box = CGRectMake( m_x - 1, m_y - 1 , m_width + 2 , m_height + 2 );
172 CGContextClearRect( m_overlayContext, box );
173 }
174
175 void wxOverlayImpl::Reset()
176 {
177 if ( m_overlayContext )
178 {
179 m_overlayContext = NULL ;
180 }
181
182 // todo : don't dispose, only hide and reposition on next run
183 if (m_overlayWindow)
184 {
185 [m_overlayParentWindow removeChildWindow:m_overlayWindow];
186 [m_overlayWindow release];
187 m_overlayWindow = NULL ;
188 }
189 }
190
191 #endif // wxHAS_NATIVE_OVERLAY