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