]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/toplevel.cpp
minor corrections for compilation with gcc 3.3 (use cast of address instead
[wxWidgets.git] / src / mac / carbon / toplevel.cpp
... / ...
CommitLineData
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
59wxWindowList wxModelessWindows;
60
61// double click testing
62static Point gs_lastWhere;
63static long gs_lastWhen = 0;
64
65
66#if TARGET_CARBON
67static 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
80extern long wxMacTranslateKey(unsigned char key, unsigned char code) ;
81
82static 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
105static 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
157static 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
233static 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}
287static 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
378pascal 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
402DEFINE_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
412wxList *wxWinMacWindowList = NULL;
413wxTopLevelWindowMac *wxFindWinFromMacWindow(WXWindow inWindowRef)
414{
415 wxNode *node = wxWinMacWindowList->Find((long)inWindowRef);
416 if (!node)
417 return NULL;
418 return (wxTopLevelWindowMac *)node->GetData();
419}
420
421void 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
431void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
432{
433 wxWinMacWindowList->DeleteObject(win);
434}
435
436
437// ----------------------------------------------------------------------------
438// wxTopLevelWindowMac creation
439// ----------------------------------------------------------------------------
440
441WXHWND wxTopLevelWindowMac::s_macWindowInUpdate = NULL;
442wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
443
444void wxTopLevelWindowMac::Init()
445{
446 m_iconized =
447 m_maximizeOnShow = FALSE;
448 m_macNoEraseUpdateRgn = NewRgn() ;
449 m_macNeedsErasing = false ;
450 m_macWindow = NULL ;
451#if TARGET_CARBON
452 m_macEventHandler = NULL ;
453 #endif
454}
455
456class wxMacDeferredWindowDeleter : public wxObject
457{
458public :
459 wxMacDeferredWindowDeleter( WindowRef windowRef )
460 {
461 m_macWindow = windowRef ;
462 }
463 virtual ~wxMacDeferredWindowDeleter()
464 {
465 UMADisposeWindow( (WindowRef) m_macWindow ) ;
466 }
467 protected :
468 WindowRef m_macWindow ;
469} ;
470
471bool wxTopLevelWindowMac::Create(wxWindow *parent,
472 wxWindowID id,
473 const wxString& title,
474 const wxPoint& pos,
475 const wxSize& size,
476 long style,
477 const wxString& name)
478{
479 // init our fields
480 Init();
481
482 m_windowStyle = style;
483
484 SetName(name);
485
486 m_windowId = id == -1 ? NewControlId() : id;
487
488 wxTopLevelWindows.Append(this);
489
490 if ( parent )
491 parent->AddChild(this);
492
493 return TRUE;
494}
495
496wxTopLevelWindowMac::~wxTopLevelWindowMac()
497{
498 if ( m_macWindow )
499 {
500 wxToolTip::NotifyWindowDelete(m_macWindow) ;
501 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
502 }
503
504#if TARGET_CARBON
505 if ( m_macEventHandler )
506 {
507 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
508 m_macEventHandler = NULL ;
509 }
510#endif
511
512 wxRemoveMacWindowAssociation( this ) ;
513
514 if ( wxModelessWindows.Find(this) )
515 wxModelessWindows.DeleteObject(this);
516
517 DisposeRgn( (RgnHandle) m_macNoEraseUpdateRgn ) ;
518}
519
520
521// ----------------------------------------------------------------------------
522// wxTopLevelWindowMac maximize/minimize
523// ----------------------------------------------------------------------------
524
525void wxTopLevelWindowMac::Maximize(bool maximize)
526{
527 // not available on mac
528}
529
530bool wxTopLevelWindowMac::IsMaximized() const
531{
532 return false ;
533}
534
535void wxTopLevelWindowMac::Iconize(bool iconize)
536{
537 // not available on mac
538}
539
540bool wxTopLevelWindowMac::IsIconized() const
541{
542 // mac dialogs cannot be iconized
543 return FALSE;
544}
545
546void wxTopLevelWindowMac::Restore()
547{
548 // not available on mac
549}
550
551// ----------------------------------------------------------------------------
552// wxTopLevelWindowMac misc
553// ----------------------------------------------------------------------------
554
555void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
556{
557 // this sets m_icon
558 wxTopLevelWindowBase::SetIcon(icon);
559}
560
561void wxTopLevelWindowMac::MacCreateRealWindow( const wxString& title,
562 const wxPoint& pos,
563 const wxSize& size,
564 long style,
565 const wxString& name )
566{
567 SetName(name);
568 m_windowStyle = style;
569 m_isShown = FALSE;
570
571 // create frame.
572
573 Rect theBoundsRect;
574
575 m_x = (int)pos.x;
576 m_y = (int)pos.y;
577 if ( m_y < 50 )
578 m_y = 50 ;
579 if ( m_x < 20 )
580 m_x = 20 ;
581
582 m_width = size.x;
583 if (m_width == -1)
584 m_width = 20;
585 m_height = size.y;
586 if (m_height == -1)
587 m_height = 20;
588
589 ::SetRect(&theBoundsRect, m_x, m_y , m_x + m_width, m_y + m_height);
590
591 // translate the window attributes in the appropriate window class and attributes
592
593 WindowClass wclass = 0;
594 WindowAttributes attr = kWindowNoAttributes ;
595
596 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
597 {
598 if (
599 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
600 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
601 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
602 )
603 {
604 wclass = kFloatingWindowClass ;
605 if ( HasFlag(wxTINY_CAPTION_VERT) )
606 {
607 attr |= kWindowSideTitlebarAttribute ;
608 }
609 }
610 else
611 {
612#if TARGET_CARBON
613 wclass = kPlainWindowClass ;
614#else
615 wclass = kFloatingWindowClass ;
616#endif
617 }
618 }
619 else if ( HasFlag( wxCAPTION ) )
620 {
621 wclass = kDocumentWindowClass ;
622 }
623 else
624 {
625 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
626 HasFlag( wxSYSTEM_MENU ) )
627 {
628 wclass = kDocumentWindowClass ;
629 }
630 else
631 {
632#if TARGET_CARBON
633 wclass = kPlainWindowClass ;
634#else
635 wclass = kModalWindowClass ;
636#endif
637 }
638 }
639
640 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) )
641 {
642 attr |= kWindowFullZoomAttribute ;
643 attr |= kWindowCollapseBoxAttribute ;
644 }
645 if ( HasFlag( wxRESIZE_BORDER ) )
646 {
647 attr |= kWindowResizableAttribute ;
648 }
649 if ( HasFlag( wxSYSTEM_MENU ) )
650 {
651 attr |= kWindowCloseBoxAttribute ;
652 }
653
654#if TARGET_CARBON
655#if 0 // having problems right now with that
656 if (HasFlag(wxSTAY_ON_TOP))
657 wclass = kUtilityWindowClass;
658#endif
659#endif
660
661#if TARGET_CARBON
662 if ( HasFlag(wxFRAME_SHAPED) )
663 {
664 WindowDefSpec customWindowDefSpec;
665 customWindowDefSpec.defType = kWindowDefProcPtr;
666 customWindowDefSpec.u.defProc = NewWindowDefUPP(wxShapedMacWindowDef);
667
668 ::CreateCustomWindow( &customWindowDefSpec, wclass,
669 attr, &theBoundsRect,
670 (WindowRef*) &m_macWindow);
671 }
672 else
673#endif
674 {
675 ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
676 }
677
678 wxAssociateWinWithMacWindow( m_macWindow , this ) ;
679 UMASetWTitle( (WindowRef)m_macWindow , title ) ;
680 ::CreateRootControl( (WindowRef)m_macWindow , (ControlHandle*)&m_macRootControl ) ;
681#if TARGET_CARBON
682 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
683 InstallWindowEventHandler(MAC_WXHWND(m_macWindow), GetwxMacWindowEventHandlerUPP(),
684 GetEventTypeCount(eventList), eventList, this, (EventHandlerRef *)&m_macEventHandler);
685#endif
686 m_macFocus = NULL ;
687
688
689#if TARGET_CARBON
690 if ( HasFlag(wxFRAME_SHAPED) )
691 {
692 // default shape matches the window size
693 wxRegion rgn(0, 0, m_width, m_height);
694 SetShape(rgn);
695 }
696#endif
697}
698
699void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin, WXRECTPTR clipRect, WXHWND *window , wxWindowMac** rootwin)
700{
701 ((Point*)localOrigin)->h = 0;
702 ((Point*)localOrigin)->v = 0;
703 ((Rect*)clipRect)->left = 0;
704 ((Rect*)clipRect)->top = 0;
705 ((Rect*)clipRect)->right = m_width;
706 ((Rect*)clipRect)->bottom = m_height;
707 *window = m_macWindow ;
708 *rootwin = this ;
709}
710
711void wxTopLevelWindowMac::Clear()
712{
713 wxWindow::Clear() ;
714}
715
716WXWidget wxTopLevelWindowMac::MacGetContainerForEmbedding()
717{
718 return m_macRootControl ;
719}
720
721
722void wxTopLevelWindowMac::MacUpdate( long timestamp)
723{
724 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( (WindowRef) m_macWindow) ) ;
725
726 RgnHandle visRgn = NewRgn() ;
727 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), visRgn );
728 BeginUpdate( (WindowRef)m_macWindow ) ;
729
730 RgnHandle updateRgn = NewRgn();
731 RgnHandle diffRgn = NewRgn() ;
732
733 if ( updateRgn && diffRgn )
734 {
735#if 1
736 // macos internal control redraws clean up areas we'd like to redraw ourselves
737 // therefore we pick the boundary rect and make sure we can redraw it
738 // this has to be intersected by the visRgn in order to avoid drawing over its own
739 // boundaries
740 RgnHandle trueUpdateRgn = NewRgn() ;
741 Rect trueUpdateRgnBoundary ;
742 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), trueUpdateRgn );
743 GetRegionBounds( trueUpdateRgn , &trueUpdateRgnBoundary ) ;
744 RectRgn( updateRgn , &trueUpdateRgnBoundary ) ;
745 SectRgn( updateRgn , visRgn , updateRgn ) ;
746 if ( trueUpdateRgn )
747 DisposeRgn( trueUpdateRgn ) ;
748 SetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn ) ;
749#else
750 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn );
751#endif
752 DiffRgn( updateRgn , (RgnHandle) m_macNoEraseUpdateRgn , diffRgn ) ;
753 if ( !EmptyRgn( updateRgn ) )
754 {
755 MacRedraw( updateRgn , timestamp , m_macNeedsErasing || !EmptyRgn( diffRgn ) ) ;
756 }
757 }
758 if ( updateRgn )
759 DisposeRgn( updateRgn );
760 if ( diffRgn )
761 DisposeRgn( diffRgn );
762 if ( visRgn )
763 DisposeRgn( visRgn ) ;
764
765 EndUpdate( (WindowRef)m_macWindow ) ;
766 SetEmptyRgn( (RgnHandle) m_macNoEraseUpdateRgn ) ;
767 m_macNeedsErasing = false ;
768}
769
770
771// Raise the window to the top of the Z order
772void wxTopLevelWindowMac::Raise()
773{
774 ::SelectWindow( (WindowRef)m_macWindow ) ;
775}
776
777// Lower the window to the bottom of the Z order
778void wxTopLevelWindowMac::Lower()
779{
780 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
781}
782
783void wxTopLevelWindowMac::MacFireMouseEvent(
784 wxUint16 kind , wxInt32 x , wxInt32 y ,wxUint32 modifiers , long timestamp )
785{
786 wxMouseEvent event(wxEVT_LEFT_DOWN);
787 bool isDown = !(modifiers & btnState) ; // 1 is for up
788 bool controlDown = modifiers & controlKey ; // for simulating right mouse
789
790 event.m_leftDown = isDown && !controlDown;
791
792 event.m_middleDown = FALSE;
793 event.m_rightDown = isDown && controlDown;
794
795 if ( kind == mouseDown )
796 {
797 if ( controlDown )
798 event.SetEventType(wxEVT_RIGHT_DOWN ) ;
799 else
800 event.SetEventType(wxEVT_LEFT_DOWN ) ;
801 }
802 else if ( kind == mouseUp )
803 {
804 if ( controlDown )
805 event.SetEventType(wxEVT_RIGHT_UP ) ;
806 else
807 event.SetEventType(wxEVT_LEFT_UP ) ;
808 }
809 else
810 {
811 event.SetEventType(wxEVT_MOTION ) ;
812 }
813
814 event.m_shiftDown = modifiers & shiftKey;
815 event.m_controlDown = modifiers & controlKey;
816 event.m_altDown = modifiers & optionKey;
817 event.m_metaDown = modifiers & cmdKey;
818
819 Point localwhere ;
820 localwhere.h = x ;
821 localwhere.v = y ;
822
823 GrafPtr port ;
824 ::GetPort( &port ) ;
825 ::SetPort( UMAGetWindowPort( (WindowRef)m_macWindow ) ) ;
826 ::GlobalToLocal( &localwhere ) ;
827 ::SetPort( port ) ;
828
829 if ( kind == mouseDown )
830 {
831 if ( timestamp - gs_lastWhen <= (long) GetDblTime() )
832 {
833 if ( abs( localwhere.h - gs_lastWhere.h ) < 3 && abs( localwhere.v - gs_lastWhere.v ) < 3 )
834 {
835 // This is not right if the second mouse down
836 // event occured in a differen window. We
837 // correct this in MacDispatchMouseEvent.
838 if ( controlDown )
839 event.SetEventType(wxEVT_RIGHT_DCLICK ) ;
840 else
841 event.SetEventType(wxEVT_LEFT_DCLICK ) ;
842 }
843 gs_lastWhen = 0 ;
844 }
845 else
846 {
847 gs_lastWhen = timestamp ;
848 }
849 gs_lastWhere = localwhere ;
850 }
851
852 event.m_x = localwhere.h;
853 event.m_y = localwhere.v;
854 event.m_x += m_x;
855 event.m_y += m_y;
856
857 event.m_timeStamp = timestamp;
858 event.SetEventObject(this);
859 if ( wxTheApp->s_captureWindow )
860 {
861 int x = event.m_x ;
862 int y = event.m_y ;
863 wxTheApp->s_captureWindow->ScreenToClient( &x , &y ) ;
864 event.m_x = x ;
865 event.m_y = y ;
866 event.SetEventObject( wxTheApp->s_captureWindow ) ;
867 wxTheApp->s_captureWindow->GetEventHandler()->ProcessEvent( event ) ;
868
869 if ( kind == mouseUp )
870 {
871 wxTheApp->s_captureWindow = NULL ;
872 if ( !wxIsBusy() )
873 {
874 m_cursor.MacInstall() ;
875 }
876 }
877 }
878 else
879 {
880 MacDispatchMouseEvent( event ) ;
881 }
882}
883
884#if !TARGET_CARBON
885
886void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev , short part)
887{
888 MacFireMouseEvent( mouseDown , ((EventRecord*)ev)->where.h , ((EventRecord*)ev)->where.v ,
889 ((EventRecord*)ev)->modifiers , ((EventRecord*)ev)->when ) ;
890}
891
892void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev , short part)
893{
894 switch (part)
895 {
896 case inContent:
897 {
898 MacFireMouseEvent( mouseUp , ((EventRecord*)ev)->where.h , ((EventRecord*)ev)->where.v ,
899 ((EventRecord*)ev)->modifiers , ((EventRecord*)ev)->when ) ;
900 }
901 break ;
902 }
903}
904
905void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev , short part)
906{
907 switch (part)
908 {
909 case inContent:
910 {
911 MacFireMouseEvent( nullEvent /*moved*/ , ((EventRecord*)ev)->where.h , ((EventRecord*)ev)->where.v ,
912 ((EventRecord*)ev)->modifiers , ((EventRecord*)ev)->when ) ;
913 }
914 break ;
915 }
916}
917
918#endif
919
920void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
921{
922 if(s_macDeactivateWindow)
923 {
924 wxLogDebug("Doing delayed deactivation of %p",s_macDeactivateWindow);
925 s_macDeactivateWindow->MacActivate(timestamp, false);
926 }
927}
928
929void wxTopLevelWindowMac::MacActivate( long timestamp , bool inIsActivating )
930{
931 wxLogDebug("TopLevel=%p::MacActivate",this);
932 if(s_macDeactivateWindow==this)
933 s_macDeactivateWindow=NULL;
934 MacDelayedDeactivation(timestamp);
935 wxActivateEvent event(wxEVT_ACTIVATE, inIsActivating , m_windowId);
936 event.m_timeStamp = timestamp ;
937 event.SetEventObject(this);
938
939 GetEventHandler()->ProcessEvent(event);
940
941 UMAHighlightAndActivateWindow( (WindowRef)m_macWindow , inIsActivating ) ;
942
943 // Early versions of MacOS X don't refresh backgrounds properly,
944 // so refresh the whole window on activation and deactivation.
945 long osVersion = UMAGetSystemVersion();
946 if (osVersion >= 0x1000 && osVersion < 0x1020 )
947 {
948 Refresh(TRUE);
949 }
950 else
951 {
952 // for the moment we have to resolve some redrawing issues like this
953 // the OS is stealing some redrawing areas as soon as it draws a control
954 Refresh(TRUE);
955 }
956}
957
958#if !TARGET_CARBON
959
960void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev )
961{
962}
963
964#endif
965
966void wxTopLevelWindowMac::SetTitle(const wxString& title)
967{
968 wxWindow::SetTitle( title ) ;
969 UMASetWTitle( (WindowRef)m_macWindow , title ) ;
970}
971
972bool wxTopLevelWindowMac::Show(bool show)
973{
974 if ( !wxWindow::Show(show) )
975 return FALSE;
976
977 if (show)
978 {
979 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowShowTransitionAction,nil);
980 ::SelectWindow( (WindowRef)m_macWindow ) ;
981 // no need to generate events here, they will get them triggered by macos
982 // actually they should be , but apparently they are not
983 wxSize size(m_width, m_height);
984 wxSizeEvent event(size, m_windowId);
985 event.SetEventObject(this);
986 GetEventHandler()->ProcessEvent(event);
987 }
988 else
989 {
990 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowHideTransitionAction,nil);
991 }
992
993 if ( !show )
994 {
995 }
996 else
997 {
998 Refresh() ;
999 }
1000
1001 return TRUE;
1002}
1003
1004void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1005{
1006 int former_x = m_x ;
1007 int former_y = m_y ;
1008 int former_w = m_width ;
1009 int former_h = m_height ;
1010
1011 int actualWidth = width;
1012 int actualHeight = height;
1013 int actualX = x;
1014 int actualY = y;
1015
1016 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
1017 actualWidth = m_minWidth;
1018 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
1019 actualHeight = m_minHeight;
1020 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
1021 actualWidth = m_maxWidth;
1022 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
1023 actualHeight = m_maxHeight;
1024
1025 bool doMove = false ;
1026 bool doResize = false ;
1027
1028 if ( actualX != former_x || actualY != former_y )
1029 {
1030 doMove = true ;
1031 }
1032 if ( actualWidth != former_w || actualHeight != former_h )
1033 {
1034 doResize = true ;
1035 }
1036
1037 if ( doMove || doResize )
1038 {
1039 m_x = actualX ;
1040 m_y = actualY ;
1041 m_width = actualWidth ;
1042 m_height = actualHeight ;
1043
1044 if ( doMove )
1045 ::MoveWindow((WindowRef)m_macWindow, m_x, m_y , false); // don't make frontmost
1046
1047 if ( doResize )
1048 ::SizeWindow((WindowRef)m_macWindow, m_width, m_height , true);
1049
1050 // the OS takes care of invalidating and erasing the new area so we only have to
1051 // take care of refreshing for full repaints
1052
1053 if ( doResize && !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE) )
1054 Refresh() ;
1055
1056
1057 if ( IsKindOf( CLASSINFO( wxFrame ) ) )
1058 {
1059 wxFrame* frame = (wxFrame*) this ;
1060 frame->PositionStatusBar();
1061 frame->PositionToolBar();
1062 }
1063 if ( doMove )
1064 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
1065
1066 MacRepositionScrollBars() ;
1067 if ( doMove )
1068 {
1069 wxPoint point(m_x, m_y);
1070 wxMoveEvent event(point, m_windowId);
1071 event.SetEventObject(this);
1072 GetEventHandler()->ProcessEvent(event) ;
1073 }
1074 if ( doResize )
1075 {
1076 MacRepositionScrollBars() ;
1077 wxSize size(m_width, m_height);
1078 wxSizeEvent event(size, m_windowId);
1079 event.SetEventObject(this);
1080 GetEventHandler()->ProcessEvent(event);
1081 }
1082 }
1083
1084}
1085
1086/*
1087 * Invalidation Mechanism
1088 *
1089 * The update mechanism reflects exactely the windows mechanism
1090 * the rect gets added to the window invalidate region, if the eraseBackground flag
1091 * has been true for any part of the update rgn the background is erased in the entire region
1092 * not just in the specified rect.
1093 *
1094 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
1095 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1096 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1097 * will get the eraseBackground event first
1098 */
1099
1100void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect, bool eraseBackground )
1101{
1102 GrafPtr formerPort ;
1103 GetPort( &formerPort ) ;
1104 SetPortWindowPort( (WindowRef)m_macWindow ) ;
1105
1106 m_macNeedsErasing |= eraseBackground ;
1107
1108 // if we already know that we will have to erase, there's no need to track the rest
1109 if ( !m_macNeedsErasing)
1110 {
1111 // we end only here if eraseBackground is false
1112 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1113 // we will have to erase anyway
1114
1115 RgnHandle updateRgn = NewRgn();
1116 RgnHandle diffRgn = NewRgn() ;
1117 if ( updateRgn && diffRgn )
1118 {
1119 GetWindowUpdateRgn( (WindowRef)m_macWindow , updateRgn );
1120 Point pt = {0,0} ;
1121 LocalToGlobal( &pt ) ;
1122 OffsetRgn( updateRgn , -pt.h , -pt.v ) ;
1123 DiffRgn( updateRgn , (RgnHandle) m_macNoEraseUpdateRgn , diffRgn ) ;
1124 if ( !EmptyRgn( diffRgn ) )
1125 {
1126 m_macNeedsErasing = true ;
1127 }
1128 }
1129 if ( updateRgn )
1130 DisposeRgn( updateRgn );
1131 if ( diffRgn )
1132 DisposeRgn( diffRgn );
1133
1134 if ( !m_macNeedsErasing )
1135 {
1136 RgnHandle rectRgn = NewRgn() ;
1137 SetRectRgn( rectRgn , ((Rect*)rect)->left , ((Rect*)rect)->top , ((Rect*)rect)->right , ((Rect*)rect)->bottom ) ;
1138 UnionRgn( (RgnHandle) m_macNoEraseUpdateRgn , rectRgn , (RgnHandle) m_macNoEraseUpdateRgn ) ;
1139 DisposeRgn( rectRgn ) ;
1140 }
1141 }
1142 InvalWindowRect( (WindowRef)m_macWindow , (Rect*)rect ) ;
1143 // turn this on to debug the refreshing cycle
1144#if wxMAC_DEBUG_REDRAW
1145 PaintRect( rect ) ;
1146#endif
1147 SetPort( formerPort ) ;
1148}
1149
1150
1151bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1152{
1153 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), FALSE,
1154 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1155
1156#if TARGET_CARBON
1157 // The empty region signifies that the shape should be removed from the
1158 // window.
1159 if ( region.IsEmpty() )
1160 {
1161 wxSize sz = GetClientSize();
1162 wxRegion rgn(0, 0, sz.x, sz.y);
1163 return SetShape(rgn);
1164 }
1165
1166 // Make a copy of the region
1167 RgnHandle shapeRegion = NewRgn();
1168 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1169
1170 // Dispose of any shape region we may already have
1171 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1172 if ( oldRgn )
1173 DisposeRgn(oldRgn);
1174
1175 // Save the region so we can use it later
1176 SetWRefCon((WindowRef)MacGetWindowRef(), (SInt32)shapeRegion);
1177
1178 // Tell the window manager that the window has changed shape
1179 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
1180 return TRUE;
1181#else
1182 return FALSE;
1183#endif
1184}
1185
1186// ---------------------------------------------------------------------------
1187// Support functions for shaped windows, based on Apple's CustomWindow sample at
1188// http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1189// ---------------------------------------------------------------------------
1190
1191#if TARGET_CARBON
1192
1193static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1194{
1195 GetWindowPortBounds(window, inRect);
1196 Point pt = {inRect->left, inRect->top};
1197 SetPort((GrafPtr) GetWindowPort(window));
1198 LocalToGlobal(&pt);
1199 inRect->top = pt.v;
1200 inRect->left = pt.h;
1201 inRect->bottom += pt.v;
1202 inRect->right += pt.h;
1203}
1204
1205
1206static SInt32 wxShapedMacWindowGetFeatures(WindowRef window, SInt32 param)
1207{
1208 /*------------------------------------------------------
1209 Define which options your custom window supports.
1210 --------------------------------------------------------*/
1211 //just enable everything for our demo
1212 *(OptionBits*)param=//kWindowCanGrow|
1213 //kWindowCanZoom|
1214 //kWindowCanCollapse|
1215 //kWindowCanGetWindowRegion|
1216 //kWindowHasTitleBar|
1217 //kWindowSupportsDragHilite|
1218 kWindowCanDrawInCurrentPort|
1219 //kWindowCanMeasureTitle|
1220 kWindowWantsDisposeAtProcessDeath|
1221 kWindowSupportsSetGrowImageRegion|
1222 kWindowDefSupportsColorGrafPort;
1223 return 1;
1224}
1225
1226// The content region is left as a rectangle matching the window size, this is
1227// so the origin in the paint event, and etc. still matches what the
1228// programmer expects.
1229static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1230{
1231 SetEmptyRgn(rgn);
1232 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1233 if (win)
1234 {
1235 wxRect r = win->GetRect();
1236 SetRectRgn(rgn, r.GetLeft(), r.GetTop(), r.GetRight(), r.GetBottom());
1237 }
1238}
1239
1240// The structure region is set to the shape given to the SetShape method.
1241static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1242{
1243 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1244
1245 SetEmptyRgn(rgn);
1246 if (cachedRegion)
1247 {
1248 Rect windowRect;
1249 wxShapedMacWindowGetPos(window, &windowRect); //how big is the window
1250 CopyRgn(cachedRegion, rgn); //make a copy of our cached region
1251 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
1252 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1253 }
1254}
1255
1256
1257
1258static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1259{
1260 GetWindowRegionPtr rgnRec=(GetWindowRegionPtr)param;
1261
1262 switch(rgnRec->regionCode)
1263 {
1264 case kWindowStructureRgn:
1265 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1266 break;
1267 case kWindowContentRgn:
1268 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1269 break;
1270 default:
1271 SetEmptyRgn(rgnRec->winRgn);
1272 } //switch
1273
1274 return noErr;
1275}
1276
1277
1278static SInt32 wxShapedMacWindowHitTest(WindowRef window,SInt32 param)
1279{
1280 /*------------------------------------------------------
1281 Determine the region of the window which was hit
1282 --------------------------------------------------------*/
1283 Point hitPoint;
1284 static RgnHandle tempRgn=nil;
1285
1286 if(!tempRgn)
1287 tempRgn=NewRgn();
1288
1289 SetPt(&hitPoint,LoWord(param),HiWord(param));//get the point clicked
1290
1291 //Mac OS 8.5 or later
1292 wxShapedMacWindowStructureRegion(window, tempRgn);
1293 if (PtInRgn(hitPoint, tempRgn)) //in window content region?
1294 return wInContent;
1295
1296 return wNoHit;//no significant area was hit.
1297}
1298
1299
1300static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param)
1301{
1302 switch(message)
1303 {
1304 case kWindowMsgHitTest:
1305 return wxShapedMacWindowHitTest(window,param);
1306
1307 case kWindowMsgGetFeatures:
1308 return wxShapedMacWindowGetFeatures(window,param);
1309
1310 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1311 case kWindowMsgGetRegion:
1312 return wxShapedMacWindowGetRegion(window,param);
1313 }
1314
1315 return 0;
1316}
1317
1318#endif
1319// ---------------------------------------------------------------------------
1320