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