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