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