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