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