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