]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/toplevel.cpp
fix for preserving the clip rgn (control redraws missing after switch to faster redra...
[wxWidgets.git] / src / mac / carbon / toplevel.cpp
CommitLineData
a15eb0a5
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: mac/toplevel.cpp
3// Purpose: implements wxTopLevelWindow for MSW
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 24.09.01
7// RCS-ID: $Id$
8// Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
6aa89a22 9// License: wxWindows licence
a15eb0a5
SC
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "toplevel.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/toplevel.h"
422644a3 34 #include "wx/frame.h"
a15eb0a5
SC
35 #include "wx/string.h"
36 #include "wx/log.h"
37 #include "wx/intl.h"
38#endif //WX_PRECOMP
39
5f0b2f22 40#include "wx/mac/uma.h"
422644a3 41#include "wx/mac/aga.h"
7c091673 42#include "wx/app.h"
5f0b2f22 43#include "wx/tooltip.h"
a07c1212 44#include "wx/dnd.h"
5f0b2f22 45
dd49c691
SC
46#include "ToolUtils.h"
47
48
34dc8f91
SC
49#define wxMAC_DEBUG_REDRAW 0
50#ifndef wxMAC_DEBUG_REDRAW
51#define wxMAC_DEBUG_REDRAW 0
52#endif
53
a15eb0a5
SC
54// ----------------------------------------------------------------------------
55// globals
56// ----------------------------------------------------------------------------
57
58// list of all frames and modeless dialogs
32b5be3d
RR
59wxWindowList wxModelessWindows;
60
61// double click testing
62static Point gs_lastWhere;
63static long gs_lastWhen = 0;
64
6a7e6411 65
45ff8005 66#if TARGET_CARBON
6a7e6411 67static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param);
45ff8005 68#endif
6a7e6411 69
a15eb0a5
SC
70// ============================================================================
71// wxTopLevelWindowMac implementation
72// ============================================================================
73
851b3a88
SC
74// ---------------------------------------------------------------------------
75// Carbon Events
76// ---------------------------------------------------------------------------
77
78#if TARGET_CARBON
79
80extern long wxMacTranslateKey(unsigned char key, unsigned char code) ;
81
1542ea39 82static const EventTypeSpec eventList[] =
851b3a88
SC
83{
84 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
c5c9378c 85
e40298d5 86 { kEventClassKeyboard, kEventRawKeyDown } ,
c5c9378c
SC
87 { kEventClassKeyboard, kEventRawKeyRepeat } ,
88 { kEventClassKeyboard, kEventRawKeyUp } ,
89 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
90
851b3a88
SC
91 { kEventClassWindow , kEventWindowUpdate } ,
92 { kEventClassWindow , kEventWindowActivated } ,
93 { kEventClassWindow , kEventWindowDeactivated } ,
94 { kEventClassWindow , kEventWindowBoundsChanging } ,
95 { kEventClassWindow , kEventWindowBoundsChanged } ,
96 { kEventClassWindow , kEventWindowClose } ,
97
98 { kEventClassMouse , kEventMouseDown } ,
99 { kEventClassMouse , kEventMouseUp } ,
100 { kEventClassMouse , kEventMouseMoved } ,
101 { kEventClassMouse , kEventMouseDragged } ,
102
103} ;
104
105static pascal OSStatus TextInputEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
106{
107 OSStatus result = eventNotHandledErr ;
851b3a88 108
c5c9378c 109 wxWindow* focus = wxWindow::FindFocus() ;
e40298d5 110 char charCode ;
1542ea39 111 UInt32 keyCode ;
c5c9378c 112 UInt32 modifiers ;
e40298d5 113 Point point ;
c5c9378c
SC
114
115 EventRef rawEvent ;
1542ea39 116
c5c9378c 117 GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
1542ea39 118
e40298d5
JS
119 GetEventParameter( rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL,sizeof(char), NULL,&charCode );
120 GetEventParameter( rawEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
121 GetEventParameter( rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers);
122 GetEventParameter( rawEvent, kEventParamMouseLocation, typeQDPoint, NULL,
123 sizeof( Point ), NULL, &point );
124
e40298d5
JS
125 switch ( GetEventKind( event ) )
126 {
127 case kEventTextInputUnicodeForKeyEvent :
ace07c80 128 // this is only called when no default handler has jumped in, eg a wxControl on a floater window does not
5d2e69e8 129 // get its own kEventTextInputUnicodeForKeyEvent, so we route back the
ace07c80
SC
130 wxControl* control = wxDynamicCast( focus , wxControl ) ;
131 if ( control )
132 {
133 ControlHandle macControl = (ControlHandle) control->GetMacControl() ;
134 if ( macControl )
135 {
136 ::HandleControlKey( macControl , keyCode , charCode , modifiers ) ;
137 result = noErr ;
138 }
139 }
140 /*
141 // this may lead to double events sent to a window in case all handlers have skipped the key down event
77a4354e
VZ
142 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
143 UInt32 message = (keyCode << 8) + charCode;
144
1542ea39 145 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
e40298d5
JS
146 focus , message , modifiers , when , point.h , point.v ) )
147 {
148 result = noErr ;
149 }
ace07c80 150 */
e40298d5
JS
151 break ;
152 }
c5c9378c
SC
153
154 return result ;
155}
156
157static pascal OSStatus KeyboardEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
158{
159 OSStatus result = eventNotHandledErr ;
160
161 wxWindow* focus = wxWindow::FindFocus() ;
e40298d5 162 char charCode ;
1542ea39 163 UInt32 keyCode ;
c5c9378c 164 UInt32 modifiers ;
e40298d5
JS
165 Point point ;
166 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
167
168 GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL,sizeof(char), NULL,&charCode );
169 GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
170 GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers);
171 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
172 sizeof( Point ), NULL, &point );
173
174 UInt32 message = (keyCode << 8) + charCode;
175 switch( GetEventKind( event ) )
176 {
177 case kEventRawKeyRepeat :
178 case kEventRawKeyDown :
e40298d5 179 {
404f1d80
SC
180 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
181 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
182 wxTheApp->MacSetCurrentEvent( event , handler ) ;
183 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
184 focus , message , modifiers , when , point.h , point.v ) )
185 {
186 result = noErr ;
187 }
188 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
e40298d5
JS
189 }
190 break ;
191 case kEventRawKeyUp :
1542ea39 192 if ( (focus != NULL) && wxTheApp->MacSendKeyUpEvent(
e40298d5
JS
193 focus , message , modifiers , when , point.h , point.v ) )
194 {
195 result = noErr ;
196 }
197 break ;
198 case kEventRawKeyModifiersChanged :
199 {
200 wxKeyEvent event(wxEVT_KEY_DOWN);
201
202 event.m_shiftDown = modifiers & shiftKey;
203 event.m_controlDown = modifiers & controlKey;
204 event.m_altDown = modifiers & optionKey;
205 event.m_metaDown = modifiers & cmdKey;
206
207 event.m_x = point.h;
208 event.m_y = point.v;
209 event.m_timeStamp = when;
210 wxWindow* focus = wxWindow::FindFocus() ;
211 event.SetEventObject(focus);
212
8d60dc8a 213 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & controlKey )
e40298d5
JS
214 {
215 event.m_keyCode = WXK_CONTROL ;
216 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
217 focus->GetEventHandler()->ProcessEvent( event ) ;
218 }
8d60dc8a 219 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & shiftKey )
e40298d5
JS
220 {
221 event.m_keyCode = WXK_SHIFT ;
222 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
223 focus->GetEventHandler()->ProcessEvent( event ) ;
224 }
8d60dc8a 225 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & optionKey )
e40298d5
JS
226 {
227 event.m_keyCode = WXK_ALT ;
228 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
229 focus->GetEventHandler()->ProcessEvent( event ) ;
230 }
b7aec135
SC
231 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & cmdKey )
232 {
233 event.m_keyCode = WXK_COMMAND ;
234 event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
235 focus->GetEventHandler()->ProcessEvent( event ) ;
236 }
e40298d5
JS
237 wxTheApp->s_lastModifiers = modifiers ;
238 }
239 break ;
240 }
851b3a88
SC
241
242 return result ;
243}
244
67245665 245pascal OSStatus MouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
851b3a88
SC
246{
247 OSStatus result = eventNotHandledErr ;
248
e40298d5
JS
249 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
250 Point point ;
251 UInt32 modifiers = 0;
252 EventMouseButton button = 0 ;
253 UInt32 click = 0 ;
1542ea39 254
e40298d5
JS
255 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
256 sizeof( Point ), NULL, &point );
257 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL,
258 sizeof( UInt32 ), NULL, &modifiers );
259 GetEventParameter( event, kEventParamMouseButton, typeMouseButton, NULL,
260 sizeof( EventMouseButton ), NULL, &button );
261 GetEventParameter( event, kEventParamClickCount, typeUInt32, NULL,
262 sizeof( UInt32 ), NULL, &click );
1542ea39 263
e40298d5
JS
264 if ( button == 0 || GetEventKind( event ) == kEventMouseUp )
265 modifiers += btnState ;
1542ea39 266
e40298d5 267 WindowRef window ;
851b3a88 268 short windowPart = ::FindWindow(point, &window);
a3195b73 269
67245665
SC
270 // either we really are active or we are capturing mouse events
271
272 if ( (IsWindowActive(window) && windowPart == inContent) ||
273 (wxTheApp->s_captureWindow && wxTheApp->s_captureWindow->MacGetTopLevelWindow() == toplevelWindow) )
851b3a88 274 {
e40298d5
JS
275 switch ( GetEventKind( event ) )
276 {
277 case kEventMouseDown :
278 toplevelWindow->MacFireMouseEvent( mouseDown , point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
279 result = noErr ;
280 break ;
281 case kEventMouseUp :
282 toplevelWindow->MacFireMouseEvent( mouseUp , point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
283 result = noErr ;
284 break ;
285 case kEventMouseMoved :
3c589c7f 286 wxTheApp->MacHandleMouseMovedEvent( point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
e40298d5
JS
287 result = noErr ;
288 break ;
289 case kEventMouseDragged :
290 toplevelWindow->MacFireMouseEvent( nullEvent , point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
291 result = noErr ;
292 break ;
293 default :
294 break ;
295 }
296 }
1542ea39 297
e40298d5 298 return result ;
1542ea39 299
851b3a88
SC
300
301}
302static pascal OSStatus WindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
303{
304 OSStatus result = eventNotHandledErr ;
305 OSStatus err = noErr ;
306
e40298d5
JS
307 UInt32 attributes;
308 WindowRef windowRef ;
309 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
1542ea39 310
e40298d5
JS
311 GetEventParameter( event, kEventParamDirectObject, typeWindowRef, NULL,
312 sizeof( WindowRef ), NULL, &windowRef );
1542ea39 313
e40298d5
JS
314 switch( GetEventKind( event ) )
315 {
316 case kEventWindowUpdate :
317 if ( !wxPendingDelete.Member(toplevelWindow) )
318 toplevelWindow->MacUpdate( EventTimeToTicks( GetEventTime( event ) ) ) ;
319 result = noErr ;
320 break ;
321 case kEventWindowActivated :
322 toplevelWindow->MacActivate( EventTimeToTicks( GetEventTime( event ) ) , true) ;
323 result = noErr ;
324 break ;
325 case kEventWindowDeactivated :
326 toplevelWindow->MacActivate( EventTimeToTicks( GetEventTime( event ) ) , false) ;
327 result = noErr ;
328 break ;
329 case kEventWindowClose :
330 toplevelWindow->Close() ;
331 result = noErr ;
332 break ;
333 case kEventWindowBoundsChanged :
334 err = GetEventParameter( event, kEventParamAttributes, typeUInt32,
335 NULL, sizeof( UInt32 ), NULL, &attributes );
336 if ( err == noErr )
337 {
338 Rect newContentRect ;
339
340 GetEventParameter( event, kEventParamCurrentBounds, typeQDRectangle, NULL,
341 sizeof( newContentRect ), NULL, &newContentRect );
1542ea39
RD
342
343 toplevelWindow->SetSize( newContentRect.left , newContentRect.top ,
344 newContentRect.right - newContentRect.left ,
851b3a88
SC
345 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
346
e40298d5
JS
347 result = noErr;
348 }
349 break ;
f81bfef9
SC
350 case kEventWindowBoundsChanging :
351 err = GetEventParameter( event, kEventParamAttributes, typeUInt32,
352 NULL, sizeof( UInt32 ), NULL, &attributes );
353 if ( err == noErr )
354 {
355 Rect newContentRect ;
356
357 GetEventParameter( event, kEventParamCurrentBounds, typeQDRectangle, NULL,
358 sizeof( newContentRect ), NULL, &newContentRect );
5d2e69e8 359
83901ec2
SC
360 wxSize formerSize = toplevelWindow->GetSize() ;
361
362 if ( (attributes & kWindowBoundsChangeSizeChanged ) ||
363 ( attributes & kWindowBoundsChangeOriginChanged ) )
364 toplevelWindow->SetSize( newContentRect.left , newContentRect.top ,
365 newContentRect.right - newContentRect.left ,
366 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
367
368 int x , y , w , h ;
369 toplevelWindow->GetPosition( &x , &y ) ;
370 toplevelWindow->GetSize( &w , &h ) ;
371 Rect adjustedRect = { y , x , y + h , x + w } ;
372
373 if ( !EqualRect( &newContentRect , &adjustedRect ) )
f81bfef9 374 {
83901ec2 375 SetEventParameter( event , kEventParamCurrentBounds , typeQDRectangle, sizeof( adjustedRect ) , &adjustedRect ) ;
f81bfef9 376 }
83901ec2
SC
377
378 if ( toplevelWindow->GetSize() != formerSize )
379 toplevelWindow->Update() ;
380
f81bfef9
SC
381 result = noErr ;
382 }
383 break ;
e40298d5
JS
384 default :
385 break ;
386 }
387 return result ;
851b3a88
SC
388}
389
390pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
391{
392 OSStatus result = eventNotHandledErr ;
393
394 switch ( GetEventClass( event ) )
395 {
c5c9378c 396 case kEventClassKeyboard :
e40298d5 397 result = KeyboardEventHandler( handler, event , data ) ;
c5c9378c 398 break ;
851b3a88 399 case kEventClassTextInput :
e40298d5 400 result = TextInputEventHandler( handler, event , data ) ;
851b3a88
SC
401 break ;
402 case kEventClassWindow :
e40298d5
JS
403 result = WindowEventHandler( handler, event , data ) ;
404 break ;
851b3a88 405 case kEventClassMouse :
e40298d5
JS
406 result = MouseEventHandler( handler, event , data ) ;
407 break ;
851b3a88
SC
408 default :
409 break ;
410 }
411 return result ;
412}
413
414DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler )
415
416#endif
417
5f0b2f22
SC
418// ---------------------------------------------------------------------------
419// wxWindowMac utility functions
420// ---------------------------------------------------------------------------
421
422// Find an item given the Macintosh Window Reference
423
424wxList *wxWinMacWindowList = NULL;
76a5e5d2 425wxTopLevelWindowMac *wxFindWinFromMacWindow(WXWindow inWindowRef)
5f0b2f22 426{
46d0c707
SC
427 if ( wxWinMacWindowList == NULL )
428 return NULL ;
5f0b2f22
SC
429 wxNode *node = wxWinMacWindowList->Find((long)inWindowRef);
430 if (!node)
431 return NULL;
eb22f2a6 432 return (wxTopLevelWindowMac *)node->GetData();
5f0b2f22
SC
433}
434
76a5e5d2 435void wxAssociateWinWithMacWindow(WXWindow inWindowRef, wxTopLevelWindowMac *win)
5f0b2f22
SC
436{
437 // adding NULL WindowRef is (first) surely a result of an error and
438 // (secondly) breaks menu command processing
427ff662 439 wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
5f0b2f22
SC
440
441 if ( !wxWinMacWindowList->Find((long)inWindowRef) )
442 wxWinMacWindowList->Append((long)inWindowRef, win);
443}
444
445void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
446{
447 wxWinMacWindowList->DeleteObject(win);
448}
449
450
a15eb0a5
SC
451// ----------------------------------------------------------------------------
452// wxTopLevelWindowMac creation
453// ----------------------------------------------------------------------------
454
76a5e5d2 455WXHWND wxTopLevelWindowMac::s_macWindowInUpdate = NULL;
245f3581 456wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
5f0b2f22 457
a15eb0a5
SC
458void wxTopLevelWindowMac::Init()
459{
460 m_iconized =
461 m_maximizeOnShow = FALSE;
5f0b2f22
SC
462 m_macNoEraseUpdateRgn = NewRgn() ;
463 m_macNeedsErasing = false ;
6a17ca35 464 m_macWindow = NULL ;
851b3a88 465#if TARGET_CARBON
7c091673 466 m_macEventHandler = NULL ;
851b3a88 467 #endif
a15eb0a5
SC
468}
469
118f012e
SC
470class wxMacDeferredWindowDeleter : public wxObject
471{
472public :
1542ea39
RD
473 wxMacDeferredWindowDeleter( WindowRef windowRef )
474 {
475 m_macWindow = windowRef ;
118f012e 476 }
1542ea39
RD
477 virtual ~wxMacDeferredWindowDeleter()
478 {
479 UMADisposeWindow( (WindowRef) m_macWindow ) ;
118f012e
SC
480 }
481 protected :
482 WindowRef m_macWindow ;
483} ;
484
a15eb0a5
SC
485bool wxTopLevelWindowMac::Create(wxWindow *parent,
486 wxWindowID id,
487 const wxString& title,
488 const wxPoint& pos,
489 const wxSize& size,
490 long style,
491 const wxString& name)
492{
493 // init our fields
494 Init();
495
496 m_windowStyle = style;
497
498 SetName(name);
499
500 m_windowId = id == -1 ? NewControlId() : id;
501
502 wxTopLevelWindows.Append(this);
503
504 if ( parent )
505 parent->AddChild(this);
506
507 return TRUE;
508}
509
510wxTopLevelWindowMac::~wxTopLevelWindowMac()
511{
6a17ca35
SC
512 if ( m_macWindow )
513 {
514 wxToolTip::NotifyWindowDelete(m_macWindow) ;
118f012e 515 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
6a17ca35 516 }
1542ea39 517
f75363ee 518#if TARGET_CARBON
7c091673
SC
519 if ( m_macEventHandler )
520 {
521 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
522 m_macEventHandler = NULL ;
523 }
1542ea39 524#endif
851b3a88 525
5f0b2f22
SC
526 wxRemoveMacWindowAssociation( this ) ;
527
a15eb0a5
SC
528 if ( wxModelessWindows.Find(this) )
529 wxModelessWindows.DeleteObject(this);
530
76a5e5d2 531 DisposeRgn( (RgnHandle) m_macNoEraseUpdateRgn ) ;
a15eb0a5
SC
532}
533
534
535// ----------------------------------------------------------------------------
536// wxTopLevelWindowMac maximize/minimize
537// ----------------------------------------------------------------------------
538
539void wxTopLevelWindowMac::Maximize(bool maximize)
540{
b7aec135
SC
541 ZoomWindow( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , false ) ;
542
543 Rect tempRect ;
544 GrafPtr port ;
545 GetPort( &port ) ;
546 Point pt = { 0, 0 } ;
547 SetPortWindowPort((WindowRef)m_macWindow) ;
548 LocalToGlobal( &pt ) ;
549 SetPort( port ) ;
550
551 GetWindowPortBounds((WindowRef)m_macWindow, &tempRect ) ;
552 SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
553 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
a15eb0a5
SC
554}
555
556bool wxTopLevelWindowMac::IsMaximized() const
557{
b7aec135 558 return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
a15eb0a5
SC
559}
560
561void wxTopLevelWindowMac::Iconize(bool iconize)
562{
b7aec135
SC
563 if ( IsWindowCollapsable((WindowRef)m_macWindow) )
564 CollapseWindow((WindowRef)m_macWindow , iconize ) ;
a15eb0a5
SC
565}
566
567bool wxTopLevelWindowMac::IsIconized() const
568{
b7aec135 569 return IsWindowCollapsed((WindowRef)m_macWindow ) ;
a15eb0a5
SC
570}
571
572void wxTopLevelWindowMac::Restore()
573{
574 // not available on mac
575}
576
577// ----------------------------------------------------------------------------
578// wxTopLevelWindowMac misc
579// ----------------------------------------------------------------------------
580
581void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
582{
583 // this sets m_icon
584 wxTopLevelWindowBase::SetIcon(icon);
585}
5f0b2f22
SC
586
587void wxTopLevelWindowMac::MacCreateRealWindow( const wxString& title,
588 const wxPoint& pos,
589 const wxSize& size,
590 long style,
1542ea39 591 const wxString& name )
5f0b2f22 592{
47ac4f9b 593 OSStatus err = noErr ;
e40298d5
JS
594 SetName(name);
595 m_windowStyle = style;
596 m_isShown = FALSE;
1542ea39 597
e40298d5 598 // create frame.
1542ea39 599
5f0b2f22 600 Rect theBoundsRect;
1542ea39 601
e40298d5
JS
602 m_x = (int)pos.x;
603 m_y = (int)pos.y;
604 if ( m_y < 50 )
605 m_y = 50 ;
606 if ( m_x < 20 )
607 m_x = 20 ;
1542ea39 608
e40298d5 609 m_width = size.x;
1542ea39 610 if (m_width == -1)
5f0b2f22 611 m_width = 20;
e40298d5 612 m_height = size.y;
1542ea39 613 if (m_height == -1)
5f0b2f22 614 m_height = 20;
1542ea39 615
5f0b2f22 616 ::SetRect(&theBoundsRect, m_x, m_y , m_x + m_width, m_y + m_height);
1542ea39 617
5f0b2f22 618 // translate the window attributes in the appropriate window class and attributes
1542ea39 619
5f0b2f22
SC
620 WindowClass wclass = 0;
621 WindowAttributes attr = kWindowNoAttributes ;
1542ea39 622
f5744893 623 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
5f0b2f22 624 {
1542ea39 625 if (
f5744893
SC
626 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
627 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
628 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
e40298d5 629 )
5f0b2f22 630 {
f5744893
SC
631 wclass = kFloatingWindowClass ;
632 if ( HasFlag(wxTINY_CAPTION_VERT) )
633 {
634 attr |= kWindowSideTitlebarAttribute ;
635 }
636 }
637 else
638 {
2b5f62a0 639#if TARGET_CARBON
f5744893 640 wclass = kPlainWindowClass ;
2b5f62a0
VZ
641#else
642 wclass = kFloatingWindowClass ;
643#endif
5f0b2f22
SC
644 }
645 }
646 else if ( HasFlag( wxCAPTION ) )
647 {
1542ea39 648 wclass = kDocumentWindowClass ;
5f0b2f22
SC
649 }
650 else
651 {
f5744893 652 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
47ac4f9b 653 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
f5744893
SC
654 {
655 wclass = kDocumentWindowClass ;
656 }
657 else
658 {
2b5f62a0 659#if TARGET_CARBON
f5744893 660 wclass = kPlainWindowClass ;
2b5f62a0
VZ
661#else
662 wclass = kModalWindowClass ;
663#endif
f5744893 664 }
5f0b2f22 665 }
1542ea39 666
47ac4f9b 667 if ( HasFlag( wxMINIMIZE_BOX ) )
5f0b2f22 668 {
5f0b2f22
SC
669 attr |= kWindowCollapseBoxAttribute ;
670 }
47ac4f9b
SC
671 if ( HasFlag( wxMAXIMIZE_BOX ) )
672 {
673 attr |= kWindowFullZoomAttribute ;
674 }
5f0b2f22
SC
675 if ( HasFlag( wxRESIZE_BORDER ) )
676 {
677 attr |= kWindowResizableAttribute ;
678 }
47ac4f9b 679 if ( HasFlag( wxCLOSE_BOX) )
5f0b2f22
SC
680 {
681 attr |= kWindowCloseBoxAttribute ;
682 }
1542ea39 683
b9c10231
SC
684#if TARGET_CARBON
685#if 0 // having problems right now with that
6a7e6411
RD
686 if (HasFlag(wxSTAY_ON_TOP))
687 wclass = kUtilityWindowClass;
b9c10231
SC
688#endif
689#endif
6a7e6411 690
45ff8005 691#if TARGET_CARBON
6a7e6411
RD
692 if ( HasFlag(wxFRAME_SHAPED) )
693 {
694 WindowDefSpec customWindowDefSpec;
695 customWindowDefSpec.defType = kWindowDefProcPtr;
696 customWindowDefSpec.u.defProc = NewWindowDefUPP(wxShapedMacWindowDef);
697
47ac4f9b 698 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
6a7e6411
RD
699 attr, &theBoundsRect,
700 (WindowRef*) &m_macWindow);
701 }
702 else
45ff8005 703#endif
6a7e6411 704 {
47ac4f9b 705 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
6a7e6411
RD
706 }
707
47ac4f9b 708 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
5f0b2f22 709 wxAssociateWinWithMacWindow( m_macWindow , this ) ;
427ff662 710 UMASetWTitle( (WindowRef)m_macWindow , title ) ;
76a5e5d2 711 ::CreateRootControl( (WindowRef)m_macWindow , (ControlHandle*)&m_macRootControl ) ;
851b3a88 712#if TARGET_CARBON
e40298d5 713 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
1542ea39 714 InstallWindowEventHandler(MAC_WXHWND(m_macWindow), GetwxMacWindowEventHandlerUPP(),
73bdd73a 715 GetEventTypeCount(eventList), eventList, this, (EventHandlerRef *)&m_macEventHandler);
851b3a88 716#endif
5f0b2f22 717 m_macFocus = NULL ;
6a7e6411
RD
718
719
45ff8005 720#if TARGET_CARBON
6a7e6411
RD
721 if ( HasFlag(wxFRAME_SHAPED) )
722 {
723 // default shape matches the window size
724 wxRegion rgn(0, 0, m_width, m_height);
725 SetShape(rgn);
726 }
45ff8005 727#endif
5f0b2f22
SC
728}
729
1542ea39 730void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin, WXRECTPTR clipRect, WXHWND *window , wxWindowMac** rootwin)
5f0b2f22 731{
76a5e5d2
SC
732 ((Point*)localOrigin)->h = 0;
733 ((Point*)localOrigin)->v = 0;
734 ((Rect*)clipRect)->left = 0;
735 ((Rect*)clipRect)->top = 0;
736 ((Rect*)clipRect)->right = m_width;
737 ((Rect*)clipRect)->bottom = m_height;
5f0b2f22
SC
738 *window = m_macWindow ;
739 *rootwin = this ;
740}
741
5d2e69e8 742void wxTopLevelWindowMac::ClearBackground()
5f0b2f22 743{
5d2e69e8 744 wxWindow::ClearBackground() ;
5f0b2f22
SC
745}
746
1542ea39 747WXWidget wxTopLevelWindowMac::MacGetContainerForEmbedding()
5f0b2f22
SC
748{
749 return m_macRootControl ;
750}
751
752
753void wxTopLevelWindowMac::MacUpdate( long timestamp)
754{
66a09d47 755 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( (WindowRef) m_macWindow) ) ;
76a5e5d2 756
1925be65
SC
757 RgnHandle visRgn = NewRgn() ;
758 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), visRgn );
76a5e5d2 759 BeginUpdate( (WindowRef)m_macWindow ) ;
5f0b2f22 760
1542ea39 761 RgnHandle updateRgn = NewRgn();
5f0b2f22 762 RgnHandle diffRgn = NewRgn() ;
1925be65 763
5f0b2f22
SC
764 if ( updateRgn && diffRgn )
765 {
732b3a75
SC
766#if 1
767 // macos internal control redraws clean up areas we'd like to redraw ourselves
768 // therefore we pick the boundary rect and make sure we can redraw it
1925be65
SC
769 // this has to be intersected by the visRgn in order to avoid drawing over its own
770 // boundaries
732b3a75
SC
771 RgnHandle trueUpdateRgn = NewRgn() ;
772 Rect trueUpdateRgnBoundary ;
773 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), trueUpdateRgn );
774 GetRegionBounds( trueUpdateRgn , &trueUpdateRgnBoundary ) ;
1925be65
SC
775 RectRgn( updateRgn , &trueUpdateRgnBoundary ) ;
776 SectRgn( updateRgn , visRgn , updateRgn ) ;
732b3a75
SC
777 if ( trueUpdateRgn )
778 DisposeRgn( trueUpdateRgn ) ;
779 SetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn ) ;
780#else
76a5e5d2 781 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn );
732b3a75 782#endif
76a5e5d2 783 DiffRgn( updateRgn , (RgnHandle) m_macNoEraseUpdateRgn , diffRgn ) ;
5f0b2f22
SC
784 if ( !EmptyRgn( updateRgn ) )
785 {
786 MacRedraw( updateRgn , timestamp , m_macNeedsErasing || !EmptyRgn( diffRgn ) ) ;
787 }
788 }
789 if ( updateRgn )
790 DisposeRgn( updateRgn );
791 if ( diffRgn )
792 DisposeRgn( diffRgn );
1925be65
SC
793 if ( visRgn )
794 DisposeRgn( visRgn ) ;
5d2e69e8 795
76a5e5d2
SC
796 EndUpdate( (WindowRef)m_macWindow ) ;
797 SetEmptyRgn( (RgnHandle) m_macNoEraseUpdateRgn ) ;
5f0b2f22
SC
798 m_macNeedsErasing = false ;
799}
800
801
802// Raise the window to the top of the Z order
803void wxTopLevelWindowMac::Raise()
804{
7f3c339c 805 ::SelectWindow( (WindowRef)m_macWindow ) ;
5f0b2f22
SC
806}
807
808// Lower the window to the bottom of the Z order
809void wxTopLevelWindowMac::Lower()
810{
76a5e5d2 811 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
5f0b2f22
SC
812}
813
1542ea39 814void wxTopLevelWindowMac::MacFireMouseEvent(
e40298d5 815 wxUint16 kind , wxInt32 x , wxInt32 y ,wxUint32 modifiers , long timestamp )
5f0b2f22
SC
816{
817 wxMouseEvent event(wxEVT_LEFT_DOWN);
851b3a88
SC
818 bool isDown = !(modifiers & btnState) ; // 1 is for up
819 bool controlDown = modifiers & controlKey ; // for simulating right mouse
1542ea39 820
5f0b2f22 821 event.m_leftDown = isDown && !controlDown;
1542ea39 822
5f0b2f22
SC
823 event.m_middleDown = FALSE;
824 event.m_rightDown = isDown && controlDown;
825
851b3a88 826 if ( kind == mouseDown )
5f0b2f22
SC
827 {
828 if ( controlDown )
829 event.SetEventType(wxEVT_RIGHT_DOWN ) ;
830 else
831 event.SetEventType(wxEVT_LEFT_DOWN ) ;
832 }
851b3a88 833 else if ( kind == mouseUp )
5f0b2f22
SC
834 {
835 if ( controlDown )
836 event.SetEventType(wxEVT_RIGHT_UP ) ;
837 else
838 event.SetEventType(wxEVT_LEFT_UP ) ;
839 }
840 else
841 {
842 event.SetEventType(wxEVT_MOTION ) ;
843 }
844
851b3a88
SC
845 event.m_shiftDown = modifiers & shiftKey;
846 event.m_controlDown = modifiers & controlKey;
847 event.m_altDown = modifiers & optionKey;
848 event.m_metaDown = modifiers & cmdKey;
5f0b2f22 849
851b3a88
SC
850 Point localwhere ;
851 localwhere.h = x ;
852 localwhere.v = y ;
1542ea39
RD
853
854 GrafPtr port ;
5f0b2f22 855 ::GetPort( &port ) ;
76a5e5d2 856 ::SetPort( UMAGetWindowPort( (WindowRef)m_macWindow ) ) ;
5f0b2f22
SC
857 ::GlobalToLocal( &localwhere ) ;
858 ::SetPort( port ) ;
859
851b3a88 860 if ( kind == mouseDown )
5f0b2f22 861 {
5be55d56 862 if ( timestamp - gs_lastWhen <= (long) GetDblTime() )
5f0b2f22 863 {
32b5be3d 864 if ( abs( localwhere.h - gs_lastWhere.h ) < 3 && abs( localwhere.v - gs_lastWhere.v ) < 3 )
5f0b2f22 865 {
32b5be3d
RR
866 // This is not right if the second mouse down
867 // event occured in a differen window. We
868 // correct this in MacDispatchMouseEvent.
5f0b2f22
SC
869 if ( controlDown )
870 event.SetEventType(wxEVT_RIGHT_DCLICK ) ;
871 else
872 event.SetEventType(wxEVT_LEFT_DCLICK ) ;
873 }
32b5be3d 874 gs_lastWhen = 0 ;
5f0b2f22
SC
875 }
876 else
877 {
851b3a88 878 gs_lastWhen = timestamp ;
5f0b2f22 879 }
32b5be3d 880 gs_lastWhere = localwhere ;
5f0b2f22
SC
881 }
882
883 event.m_x = localwhere.h;
884 event.m_y = localwhere.v;
885 event.m_x += m_x;
886 event.m_y += m_y;
887
851b3a88 888 event.m_timeStamp = timestamp;
5f0b2f22
SC
889 event.SetEventObject(this);
890 if ( wxTheApp->s_captureWindow )
891 {
892 int x = event.m_x ;
893 int y = event.m_y ;
894 wxTheApp->s_captureWindow->ScreenToClient( &x , &y ) ;
895 event.m_x = x ;
896 event.m_y = y ;
2e6857fa 897 event.SetEventObject( wxTheApp->s_captureWindow ) ;
5f0b2f22 898 wxTheApp->s_captureWindow->GetEventHandler()->ProcessEvent( event ) ;
1542ea39 899
851b3a88 900 if ( kind == mouseUp )
5f0b2f22
SC
901 {
902 wxTheApp->s_captureWindow = NULL ;
6b57b49a 903 if ( !wxIsBusy() )
5f0b2f22
SC
904 {
905 m_cursor.MacInstall() ;
906 }
907 }
908 }
909 else
910 {
911 MacDispatchMouseEvent( event ) ;
912 }
913}
e40298d5 914
851b3a88 915#if !TARGET_CARBON
5f0b2f22 916
76a5e5d2 917void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev , short part)
5f0b2f22 918{
1542ea39 919 MacFireMouseEvent( mouseDown , ((EventRecord*)ev)->where.h , ((EventRecord*)ev)->where.v ,
e40298d5 920 ((EventRecord*)ev)->modifiers , ((EventRecord*)ev)->when ) ;
5f0b2f22
SC
921}
922
76a5e5d2 923void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev , short part)
5f0b2f22
SC
924{
925 switch (part)
926 {
1542ea39 927 case inContent:
5f0b2f22 928 {
1542ea39 929 MacFireMouseEvent( mouseUp , ((EventRecord*)ev)->where.h , ((EventRecord*)ev)->where.v ,
e40298d5 930 ((EventRecord*)ev)->modifiers , ((EventRecord*)ev)->when ) ;
5f0b2f22
SC
931 }
932 break ;
933 }
934}
935
76a5e5d2 936void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev , short part)
5f0b2f22
SC
937{
938 switch (part)
939 {
1542ea39 940 case inContent:
5f0b2f22 941 {
1542ea39 942 MacFireMouseEvent( nullEvent /*moved*/ , ((EventRecord*)ev)->where.h , ((EventRecord*)ev)->where.v ,
e40298d5 943 ((EventRecord*)ev)->modifiers , ((EventRecord*)ev)->when ) ;
5f0b2f22
SC
944 }
945 break ;
946 }
947}
851b3a88
SC
948
949#endif
950
245f3581
DE
951void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
952{
953 if(s_macDeactivateWindow)
954 {
365796b1 955 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow);
245f3581
DE
956 s_macDeactivateWindow->MacActivate(timestamp, false);
957 }
958}
959
851b3a88 960void wxTopLevelWindowMac::MacActivate( long timestamp , bool inIsActivating )
5f0b2f22 961{
17f8d2a7
VZ
962 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
963
245f3581
DE
964 if(s_macDeactivateWindow==this)
965 s_macDeactivateWindow=NULL;
966 MacDelayedDeactivation(timestamp);
5f0b2f22 967 wxActivateEvent event(wxEVT_ACTIVATE, inIsActivating , m_windowId);
851b3a88 968 event.m_timeStamp = timestamp ;
5f0b2f22 969 event.SetEventObject(this);
1542ea39 970
5f0b2f22 971 GetEventHandler()->ProcessEvent(event);
1542ea39 972
76a5e5d2 973 UMAHighlightAndActivateWindow( (WindowRef)m_macWindow , inIsActivating ) ;
1542ea39 974
a006e78b
JS
975 // Early versions of MacOS X don't refresh backgrounds properly,
976 // so refresh the whole window on activation and deactivation.
977 long osVersion = UMAGetSystemVersion();
1925be65 978 if (osVersion >= 0x1000 && osVersion < 0x1020 )
732b3a75 979 {
a006e78b 980 Refresh(TRUE);
732b3a75 981 }
a006e78b 982 else
732b3a75
SC
983 {
984 // for the moment we have to resolve some redrawing issues like this
985 // the OS is stealing some redrawing areas as soon as it draws a control
986 Refresh(TRUE);
987 }
5f0b2f22
SC
988}
989
851b3a88
SC
990#if !TARGET_CARBON
991
1542ea39 992void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev )
5f0b2f22
SC
993{
994}
995
851b3a88
SC
996#endif
997
5f0b2f22
SC
998void wxTopLevelWindowMac::SetTitle(const wxString& title)
999{
1000 wxWindow::SetTitle( title ) ;
427ff662 1001 UMASetWTitle( (WindowRef)m_macWindow , title ) ;
5f0b2f22
SC
1002}
1003
1004bool wxTopLevelWindowMac::Show(bool show)
1005{
1006 if ( !wxWindow::Show(show) )
1007 return FALSE;
1008
1009 if (show)
1542ea39 1010 {
1925be65 1011 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowShowTransitionAction,nil);
76a5e5d2 1012 ::SelectWindow( (WindowRef)m_macWindow ) ;
5f0b2f22
SC
1013 // no need to generate events here, they will get them triggered by macos
1014 // actually they should be , but apparently they are not
1015 wxSize size(m_width, m_height);
1016 wxSizeEvent event(size, m_windowId);
1017 event.SetEventObject(this);
1018 GetEventHandler()->ProcessEvent(event);
1019 }
1020 else
1021 {
1925be65 1022 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowHideTransitionAction,nil);
5f0b2f22
SC
1023 }
1024
1025 if ( !show )
1026 {
1027 }
1028 else
1029 {
1542ea39 1030 Refresh() ;
5f0b2f22
SC
1031 }
1032
1033 return TRUE;
1034}
1035
1036void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1037{
1038 int former_x = m_x ;
1039 int former_y = m_y ;
1040 int former_w = m_width ;
1041 int former_h = m_height ;
1542ea39 1042
e40298d5
JS
1043 int actualWidth = width;
1044 int actualHeight = height;
1045 int actualX = x;
1046 int actualY = y;
1542ea39
RD
1047
1048 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
5f0b2f22 1049 actualWidth = m_minWidth;
1542ea39 1050 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
5f0b2f22 1051 actualHeight = m_minHeight;
1542ea39 1052 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
5f0b2f22 1053 actualWidth = m_maxWidth;
1542ea39 1054 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
5f0b2f22
SC
1055 actualHeight = m_maxHeight;
1056
1057 bool doMove = false ;
1058 bool doResize = false ;
1542ea39 1059
5f0b2f22
SC
1060 if ( actualX != former_x || actualY != former_y )
1061 {
1062 doMove = true ;
1063 }
1064 if ( actualWidth != former_w || actualHeight != former_h )
1065 {
1066 doResize = true ;
1067 }
1068
1069 if ( doMove || doResize )
1070 {
1071 m_x = actualX ;
1072 m_y = actualY ;
5f0b2f22
SC
1073
1074 if ( doMove )
76a5e5d2 1075 ::MoveWindow((WindowRef)m_macWindow, m_x, m_y , false); // don't make frontmost
1542ea39 1076
789a620a
SC
1077 m_width = actualWidth ;
1078 m_height = actualHeight ;
1079
5f0b2f22 1080 if ( doResize )
1542ea39
RD
1081 ::SizeWindow((WindowRef)m_macWindow, m_width, m_height , true);
1082
2b5f62a0
VZ
1083 // the OS takes care of invalidating and erasing the new area so we only have to
1084 // take care of refreshing for full repaints
1085
1086 if ( doResize && !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE) )
1087 Refresh() ;
1542ea39
RD
1088
1089
5f0b2f22
SC
1090 if ( IsKindOf( CLASSINFO( wxFrame ) ) )
1091 {
1092 wxFrame* frame = (wxFrame*) this ;
1093 frame->PositionStatusBar();
1094 frame->PositionToolBar();
1095 }
1096 if ( doMove )
1097 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
1098
1099 MacRepositionScrollBars() ;
1100 if ( doMove )
1101 {
1102 wxPoint point(m_x, m_y);
1103 wxMoveEvent event(point, m_windowId);
1104 event.SetEventObject(this);
1105 GetEventHandler()->ProcessEvent(event) ;
1106 }
1107 if ( doResize )
1108 {
1109 MacRepositionScrollBars() ;
1110 wxSize size(m_width, m_height);
1111 wxSizeEvent event(size, m_windowId);
1112 event.SetEventObject(this);
1113 GetEventHandler()->ProcessEvent(event);
1114 }
1115 }
1542ea39 1116
5f0b2f22
SC
1117}
1118
1119/*
1120 * Invalidation Mechanism
1121 *
1122 * The update mechanism reflects exactely the windows mechanism
1123 * the rect gets added to the window invalidate region, if the eraseBackground flag
1124 * has been true for any part of the update rgn the background is erased in the entire region
1125 * not just in the specified rect.
1126 *
1542ea39 1127 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
5f0b2f22
SC
1128 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1129 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1130 * will get the eraseBackground event first
1131 */
1542ea39
RD
1132
1133void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect, bool eraseBackground )
5f0b2f22 1134{
e40298d5
JS
1135 GrafPtr formerPort ;
1136 GetPort( &formerPort ) ;
1137 SetPortWindowPort( (WindowRef)m_macWindow ) ;
1542ea39 1138
e40298d5 1139 m_macNeedsErasing |= eraseBackground ;
1542ea39 1140
e40298d5
JS
1141 // if we already know that we will have to erase, there's no need to track the rest
1142 if ( !m_macNeedsErasing)
5f0b2f22 1143 {
e40298d5
JS
1144 // we end only here if eraseBackground is false
1145 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1146 // we will have to erase anyway
1542ea39
RD
1147
1148 RgnHandle updateRgn = NewRgn();
e40298d5
JS
1149 RgnHandle diffRgn = NewRgn() ;
1150 if ( updateRgn && diffRgn )
1151 {
1152 GetWindowUpdateRgn( (WindowRef)m_macWindow , updateRgn );
1153 Point pt = {0,0} ;
1154 LocalToGlobal( &pt ) ;
1155 OffsetRgn( updateRgn , -pt.h , -pt.v ) ;
1156 DiffRgn( updateRgn , (RgnHandle) m_macNoEraseUpdateRgn , diffRgn ) ;
1157 if ( !EmptyRgn( diffRgn ) )
1158 {
1159 m_macNeedsErasing = true ;
1160 }
1161 }
1162 if ( updateRgn )
1163 DisposeRgn( updateRgn );
1164 if ( diffRgn )
1165 DisposeRgn( diffRgn );
1542ea39 1166
e40298d5 1167 if ( !m_macNeedsErasing )
5f0b2f22 1168 {
e40298d5
JS
1169 RgnHandle rectRgn = NewRgn() ;
1170 SetRectRgn( rectRgn , ((Rect*)rect)->left , ((Rect*)rect)->top , ((Rect*)rect)->right , ((Rect*)rect)->bottom ) ;
1171 UnionRgn( (RgnHandle) m_macNoEraseUpdateRgn , rectRgn , (RgnHandle) m_macNoEraseUpdateRgn ) ;
1172 DisposeRgn( rectRgn ) ;
5f0b2f22
SC
1173 }
1174 }
e40298d5
JS
1175 InvalWindowRect( (WindowRef)m_macWindow , (Rect*)rect ) ;
1176 // turn this on to debug the refreshing cycle
34dc8f91 1177#if wxMAC_DEBUG_REDRAW
e40298d5 1178 PaintRect( rect ) ;
5f0b2f22 1179#endif
e40298d5 1180 SetPort( formerPort ) ;
5f0b2f22 1181}
a07c1212 1182
6a7e6411 1183
1542ea39
RD
1184bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1185{
6a7e6411
RD
1186 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), FALSE,
1187 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1188
45ff8005 1189#if TARGET_CARBON
6a7e6411
RD
1190 // The empty region signifies that the shape should be removed from the
1191 // window.
1192 if ( region.IsEmpty() )
1193 {
1194 wxSize sz = GetClientSize();
1195 wxRegion rgn(0, 0, sz.x, sz.y);
1196 return SetShape(rgn);
1197 }
1198
1199 // Make a copy of the region
1200 RgnHandle shapeRegion = NewRgn();
1201 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1202
1203 // Dispose of any shape region we may already have
1204 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1205 if ( oldRgn )
1206 DisposeRgn(oldRgn);
1207
1208 // Save the region so we can use it later
1209 SetWRefCon((WindowRef)MacGetWindowRef(), (SInt32)shapeRegion);
1210
1211 // Tell the window manager that the window has changed shape
1212 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
1213 return TRUE;
45ff8005
RD
1214#else
1215 return FALSE;
1216#endif
6a7e6411
RD
1217}
1218
6a7e6411
RD
1219// ---------------------------------------------------------------------------
1220// Support functions for shaped windows, based on Apple's CustomWindow sample at
1221// http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1222// ---------------------------------------------------------------------------
1223
45ff8005 1224#if TARGET_CARBON
1925be65 1225
6a7e6411
RD
1226static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1227{
1228 GetWindowPortBounds(window, inRect);
1229 Point pt = {inRect->left, inRect->top};
1230 SetPort((GrafPtr) GetWindowPort(window));
1231 LocalToGlobal(&pt);
1232 inRect->top = pt.v;
1233 inRect->left = pt.h;
1234 inRect->bottom += pt.v;
1235 inRect->right += pt.h;
1236}
1237
1238
1239static SInt32 wxShapedMacWindowGetFeatures(WindowRef window, SInt32 param)
1240{
1241 /*------------------------------------------------------
1242 Define which options your custom window supports.
1243 --------------------------------------------------------*/
1244 //just enable everything for our demo
1245 *(OptionBits*)param=//kWindowCanGrow|
1246 //kWindowCanZoom|
1247 //kWindowCanCollapse|
1248 //kWindowCanGetWindowRegion|
1249 //kWindowHasTitleBar|
1250 //kWindowSupportsDragHilite|
1251 kWindowCanDrawInCurrentPort|
1252 //kWindowCanMeasureTitle|
1253 kWindowWantsDisposeAtProcessDeath|
1254 kWindowSupportsSetGrowImageRegion|
1255 kWindowDefSupportsColorGrafPort;
1256 return 1;
1542ea39 1257}
6a7e6411
RD
1258
1259// The content region is left as a rectangle matching the window size, this is
1260// so the origin in the paint event, and etc. still matches what the
1261// programmer expects.
1262static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1263{
1264 SetEmptyRgn(rgn);
1265 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1266 if (win)
1267 {
1268 wxRect r = win->GetRect();
1269 SetRectRgn(rgn, r.GetLeft(), r.GetTop(), r.GetRight(), r.GetBottom());
1270 }
1271}
1272
1273// The structure region is set to the shape given to the SetShape method.
1274static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1275{
1276 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1277
1278 SetEmptyRgn(rgn);
1279 if (cachedRegion)
1280 {
1281 Rect windowRect;
1282 wxShapedMacWindowGetPos(window, &windowRect); //how big is the window
1283 CopyRgn(cachedRegion, rgn); //make a copy of our cached region
1284 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
1285 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1286 }
1287}
1288
1289
1290
1291static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1292{
1293 GetWindowRegionPtr rgnRec=(GetWindowRegionPtr)param;
1294
1295 switch(rgnRec->regionCode)
1296 {
1297 case kWindowStructureRgn:
1298 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1299 break;
1300 case kWindowContentRgn:
1301 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1302 break;
1303 default:
1304 SetEmptyRgn(rgnRec->winRgn);
1305 } //switch
1306
1307 return noErr;
1308}
1309
1310
1311static SInt32 wxShapedMacWindowHitTest(WindowRef window,SInt32 param)
1312{
1313 /*------------------------------------------------------
1314 Determine the region of the window which was hit
1315 --------------------------------------------------------*/
1316 Point hitPoint;
1317 static RgnHandle tempRgn=nil;
1318
1319 if(!tempRgn)
1320 tempRgn=NewRgn();
1321
1322 SetPt(&hitPoint,LoWord(param),HiWord(param));//get the point clicked
1323
1324 //Mac OS 8.5 or later
1325 wxShapedMacWindowStructureRegion(window, tempRgn);
1326 if (PtInRgn(hitPoint, tempRgn)) //in window content region?
1327 return wInContent;
1328
1329 return wNoHit;//no significant area was hit.
1330}
1331
1332
1333static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param)
1334{
1335 switch(message)
1336 {
1337 case kWindowMsgHitTest:
1338 return wxShapedMacWindowHitTest(window,param);
1339
1340 case kWindowMsgGetFeatures:
1341 return wxShapedMacWindowGetFeatures(window,param);
1342
1343 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1344 case kWindowMsgGetRegion:
1345 return wxShapedMacWindowGetRegion(window,param);
1346 }
1347
1348 return 0;
1349}
1925be65 1350
45ff8005 1351#endif
6a7e6411 1352// ---------------------------------------------------------------------------
1925be65 1353