]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/toplevel.cpp
Include wx/textctrl.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / mac / classic / toplevel.cpp
CommitLineData
2646f485 1///////////////////////////////////////////////////////////////////////////////
fb5246be 2// Name: src/mac/classic/toplevel.cpp
2646f485
SC
3// Purpose: implements wxTopLevelWindow for Mac
4// Author: Stefan Csomor
5// Modified by:
6// Created: 24.09.01
7// RCS-ID: $Id$
8// Copyright: (c) 2001-2004 Stefan Csomor
65571936 9// License: wxWindows licence
2646f485
SC
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2646f485
SC
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#ifndef WX_PRECOMP
28 #include "wx/app.h"
29 #include "wx/toplevel.h"
30 #include "wx/frame.h"
31 #include "wx/string.h"
32 #include "wx/log.h"
33 #include "wx/intl.h"
34#endif //WX_PRECOMP
35
36#include "wx/mac/uma.h"
37#include "wx/mac/aga.h"
2646f485
SC
38#include "wx/tooltip.h"
39#include "wx/dnd.h"
40#if wxUSE_SYSTEM_OPTIONS
41 #include "wx/sysopt.h"
42#endif
43
44#include <ToolUtils.h>
45
46
47#define wxMAC_DEBUG_REDRAW 0
48#ifndef wxMAC_DEBUG_REDRAW
49#define wxMAC_DEBUG_REDRAW 0
50#endif
51
52// ----------------------------------------------------------------------------
53// globals
54// ----------------------------------------------------------------------------
55
56// list of all frames and modeless dialogs
57wxWindowList wxModelessWindows;
58
59// double click testing
60static Point gs_lastWhere;
61static long gs_lastWhen = 0;
62
63
64#if TARGET_CARBON
65static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param);
66#endif
67
68// ============================================================================
69// wxTopLevelWindowMac implementation
70// ============================================================================
71
72// ---------------------------------------------------------------------------
73// Carbon Events
74// ---------------------------------------------------------------------------
75
76#if TARGET_CARBON
77
78extern long wxMacTranslateKey(unsigned char key, unsigned char code) ;
79
80static const EventTypeSpec eventList[] =
81{
82 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
83
84 { kEventClassKeyboard, kEventRawKeyDown } ,
85 { kEventClassKeyboard, kEventRawKeyRepeat } ,
86 { kEventClassKeyboard, kEventRawKeyUp } ,
87 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
88
89 { kEventClassWindow , kEventWindowShown } ,
90 { kEventClassWindow , kEventWindowUpdate } ,
91 { kEventClassWindow , kEventWindowActivated } ,
92 { kEventClassWindow , kEventWindowDeactivated } ,
93 { kEventClassWindow , kEventWindowBoundsChanging } ,
94 { kEventClassWindow , kEventWindowBoundsChanged } ,
95 { kEventClassWindow , kEventWindowClose } ,
96
97 { kEventClassMouse , kEventMouseDown } ,
98 { kEventClassMouse , kEventMouseUp } ,
99 { kEventClassMouse , kEventMouseWheelMoved } ,
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 {
180 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
181 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
182 wxTheApp->MacSetCurrentEvent( event , handler ) ;
183 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
184 focus , message , modifiers , when , point.h , point.v ) )
185 {
186 result = noErr ;
187 }
188 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
189 }
190 break ;
191 case kEventRawKeyUp :
192 if ( (focus != NULL) && wxTheApp->MacSendKeyUpEvent(
193 focus , message , modifiers , when , point.h , point.v ) )
194 {
195 result = noErr ;
196 }
197 break ;
198 case kEventRawKeyModifiersChanged :
199 {
200 wxKeyEvent event(wxEVT_KEY_DOWN);
201
202 event.m_shiftDown = modifiers & shiftKey;
203 event.m_controlDown = modifiers & controlKey;
204 event.m_altDown = modifiers & optionKey;
205 event.m_metaDown = modifiers & cmdKey;
206
207 event.m_x = point.h;
208 event.m_y = point.v;
687706f5 209 event.SetTimestamp(when);
2646f485
SC
210 wxWindow* focus = wxWindow::FindFocus() ;
211 event.SetEventObject(focus);
212
213 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & controlKey )
214 {
215 event.m_keyCode = WXK_CONTROL ;
216 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
217 focus->GetEventHandler()->ProcessEvent( event ) ;
218 }
219 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & shiftKey )
220 {
221 event.m_keyCode = WXK_SHIFT ;
222 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
223 focus->GetEventHandler()->ProcessEvent( event ) ;
224 }
225 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & optionKey )
226 {
227 event.m_keyCode = WXK_ALT ;
228 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
229 focus->GetEventHandler()->ProcessEvent( event ) ;
230 }
231 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & cmdKey )
232 {
233 event.m_keyCode = WXK_COMMAND ;
234 event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
235 focus->GetEventHandler()->ProcessEvent( event ) ;
236 }
237 wxTheApp->s_lastModifiers = modifiers ;
238 }
239 break ;
240 }
241
242 return result ;
243}
244
245pascal OSStatus MouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
246{
247 OSStatus result = eventNotHandledErr ;
248
249 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
250 Point point ;
251 UInt32 modifiers = 0;
252 EventMouseButton button = 0 ;
253 UInt32 click = 0 ;
254
255 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
256 sizeof( Point ), NULL, &point );
257 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL,
258 sizeof( UInt32 ), NULL, &modifiers );
259 GetEventParameter( event, kEventParamMouseButton, typeMouseButton, NULL,
260 sizeof( EventMouseButton ), NULL, &button );
261 GetEventParameter( event, kEventParamClickCount, typeUInt32, NULL,
262 sizeof( UInt32 ), NULL, &click );
263
264 if ( button == 0 || GetEventKind( event ) == kEventMouseUp )
265 modifiers += btnState ;
266
fb5246be
WS
267 // temporary hack to support true two button mouse
268 if ( button == kEventMouseButtonSecondary )
269 {
270 modifiers |= controlKey ;
271 }
2646f485
SC
272 WindowRef window ;
273 short windowPart = ::FindWindow(point, &window);
274
275 // either we really are active or we are capturing mouse events
276
fb5246be 277 if ( (IsWindowActive(window) && windowPart == inContent) ||
2646f485
SC
278 (wxTheApp->s_captureWindow && wxTheApp->s_captureWindow->MacGetTopLevelWindow() == toplevelWindow) )
279 {
280 switch ( GetEventKind( event ) )
281 {
282 case kEventMouseDown :
283 toplevelWindow->MacFireMouseEvent( mouseDown , point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
284 result = noErr ;
285 break ;
286 case kEventMouseUp :
287 toplevelWindow->MacFireMouseEvent( mouseUp , point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
288 result = noErr ;
289 break ;
290 case kEventMouseMoved :
291 wxTheApp->MacHandleMouseMovedEvent( point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
292 result = noErr ;
293 break ;
294 case kEventMouseDragged :
295 toplevelWindow->MacFireMouseEvent( nullEvent , point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
296 result = noErr ;
297 break ;
298 case kEventMouseWheelMoved :
299 {
300 //bClearTooltip = false;
301 EventMouseWheelAxis axis = kEventMouseWheelAxisY;
302 SInt32 delta = 0;
303 Point mouseLoc = {0, 0};
304 if (::GetEventParameter(event, kEventParamMouseWheelAxis, typeMouseWheelAxis,
305 NULL, sizeof(EventMouseWheelAxis), NULL, &axis) == noErr &&
306 ::GetEventParameter(event, kEventParamMouseWheelDelta, typeLongInteger,
307 NULL, sizeof(SInt32), NULL, &delta) == noErr &&
308 ::GetEventParameter(event, kEventParamMouseLocation, typeQDPoint,
309 NULL, sizeof(Point), NULL, &mouseLoc) == noErr)
310 {
311 wxMouseEvent wheelEvent(wxEVT_MOUSEWHEEL);
fb5246be 312
2646f485
SC
313 wheelEvent.m_x = mouseLoc.h;
314 wheelEvent.m_y = mouseLoc.v;
fb5246be 315
2646f485
SC
316 wheelEvent.m_wheelRotation = delta;
317 wheelEvent.m_wheelDelta = 1;
318 wheelEvent.m_linesPerAction = 1;
319
320 wxWindow* currentMouseWindow = NULL;
321 wxWindow::MacGetWindowFromPoint(wxPoint(mouseLoc.h, mouseLoc.v), &currentMouseWindow);
fb5246be 322
2646f485
SC
323 if (currentMouseWindow)
324 {
325 currentMouseWindow->GetEventHandler()->ProcessEvent(wheelEvent);
326 result = noErr;
327 }
328 }
329 }
330 break ;
331 default :
332 break ;
333 }
334 }
335
336 return result ;
337
338
339}
340static pascal OSStatus WindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
341{
342 OSStatus result = eventNotHandledErr ;
343 OSStatus err = noErr ;
344
345 UInt32 attributes;
346 WindowRef windowRef ;
347 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
348
349 GetEventParameter( event, kEventParamDirectObject, typeWindowRef, NULL,
350 sizeof( WindowRef ), NULL, &windowRef );
351
352 switch( GetEventKind( event ) )
353 {
354 case kEventWindowUpdate :
355 if ( !wxPendingDelete.Member(toplevelWindow) )
356 toplevelWindow->MacUpdate( EventTimeToTicks( GetEventTime( event ) ) ) ;
357 result = noErr ;
358 break ;
359 case kEventWindowActivated :
360 toplevelWindow->MacActivate( EventTimeToTicks( GetEventTime( event ) ) , true) ;
361 result = noErr ;
362 break ;
363 case kEventWindowDeactivated :
364 toplevelWindow->MacActivate( EventTimeToTicks( GetEventTime( event ) ) , false) ;
365 result = noErr ;
366 break ;
fb5246be
WS
367 case kEventWindowShown :
368 toplevelWindow->Refresh() ;
369 result = noErr ;
370 break ;
2646f485
SC
371 case kEventWindowClose :
372 toplevelWindow->Close() ;
373 result = noErr ;
374 break ;
375 case kEventWindowBoundsChanged :
376 err = GetEventParameter( event, kEventParamAttributes, typeUInt32,
377 NULL, sizeof( UInt32 ), NULL, &attributes );
378 if ( err == noErr )
379 {
380 Rect newContentRect ;
381
382 GetEventParameter( event, kEventParamCurrentBounds, typeQDRectangle, NULL,
383 sizeof( newContentRect ), NULL, &newContentRect );
384
385 toplevelWindow->SetSize( newContentRect.left , newContentRect.top ,
386 newContentRect.right - newContentRect.left ,
387 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
388
389 result = noErr;
390 }
391 break ;
392 case kEventWindowBoundsChanging :
393 err = GetEventParameter( event, kEventParamAttributes, typeUInt32,
394 NULL, sizeof( UInt32 ), NULL, &attributes );
395 if ( err == noErr )
396 {
397 Rect newContentRect ;
398
399 GetEventParameter( event, kEventParamCurrentBounds, typeQDRectangle, NULL,
400 sizeof( newContentRect ), NULL, &newContentRect );
401
402 wxSize formerSize = toplevelWindow->GetSize() ;
403
fb5246be 404 if ( (attributes & kWindowBoundsChangeSizeChanged ) ||
2646f485
SC
405 ( attributes & kWindowBoundsChangeOriginChanged ) )
406 toplevelWindow->SetSize( newContentRect.left , newContentRect.top ,
407 newContentRect.right - newContentRect.left ,
408 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
fb5246be 409
2646f485
SC
410 int x , y , w , h ;
411 toplevelWindow->GetPosition( &x , &y ) ;
412 toplevelWindow->GetSize( &w , &h ) ;
413 Rect adjustedRect = { y , x , y + h , x + w } ;
414
415 if ( !EqualRect( &newContentRect , &adjustedRect ) )
416 {
417 SetEventParameter( event , kEventParamCurrentBounds , typeQDRectangle, sizeof( adjustedRect ) , &adjustedRect ) ;
418 }
fb5246be 419
2646f485
SC
420 if ( toplevelWindow->GetSize() != formerSize )
421 toplevelWindow->Update() ;
fb5246be 422
2646f485
SC
423 result = noErr ;
424 }
425 break ;
426 default :
427 break ;
428 }
429 return result ;
430}
431
432pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
433{
434 OSStatus result = eventNotHandledErr ;
435
436 switch ( GetEventClass( event ) )
437 {
438 case kEventClassKeyboard :
439 result = KeyboardEventHandler( handler, event , data ) ;
440 break ;
441 case kEventClassTextInput :
442 result = TextInputEventHandler( handler, event , data ) ;
443 break ;
444 case kEventClassWindow :
445 result = WindowEventHandler( handler, event , data ) ;
446 break ;
447 case kEventClassMouse :
448 result = MouseEventHandler( handler, event , data ) ;
449 break ;
450 default :
451 break ;
452 }
453 return result ;
454}
455
456DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler )
457
458#endif
459
460// ---------------------------------------------------------------------------
461// wxWindowMac utility functions
462// ---------------------------------------------------------------------------
463
464// Find an item given the Macintosh Window Reference
465
466wxList *wxWinMacWindowList = NULL;
467wxTopLevelWindowMac *wxFindWinFromMacWindow(WXWindow inWindowRef)
468{
469 if ( wxWinMacWindowList == NULL )
470 return NULL ;
471 wxNode *node = wxWinMacWindowList->Find((long)inWindowRef);
472 if (!node)
473 return NULL;
474 return (wxTopLevelWindowMac *)node->GetData();
475}
476
6bc3b8e9
DS
477void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win);
478void wxAssociateWinWithMacWindow(WindowRef inWindowRef, wxTopLevelWindowMac *win)
2646f485
SC
479{
480 // adding NULL WindowRef is (first) surely a result of an error and
481 // (secondly) breaks menu command processing
482 wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
483
484 if ( !wxWinMacWindowList->Find((long)inWindowRef) )
485 wxWinMacWindowList->Append((long)inWindowRef, win);
486}
487
488void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
489{
490 wxWinMacWindowList->DeleteObject(win);
491}
492
493
494// ----------------------------------------------------------------------------
495// wxTopLevelWindowMac creation
496// ----------------------------------------------------------------------------
497
6bc3b8e9 498WXWindow wxTopLevelWindowMac::s_macWindowInUpdate = NULL;
2646f485 499wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
fb5246be 500bool wxTopLevelWindowMac::s_macWindowCompositing = false;
2646f485
SC
501
502void wxTopLevelWindowMac::Init()
503{
504 m_iconized =
fb5246be 505 m_maximizeOnShow = false;
2646f485
SC
506 m_macNoEraseUpdateRgn = NewRgn() ;
507 m_macNeedsErasing = false ;
508 m_macWindow = NULL ;
fb5246be 509 m_macUsesCompositing = false ;
2646f485
SC
510#if TARGET_CARBON
511 m_macEventHandler = NULL ;
670f9935 512#endif
2646f485
SC
513}
514
515class wxMacDeferredWindowDeleter : public wxObject
516{
517public :
518 wxMacDeferredWindowDeleter( WindowRef windowRef )
519 {
520 m_macWindow = windowRef ;
521 }
522 virtual ~wxMacDeferredWindowDeleter()
523 {
524 UMADisposeWindow( (WindowRef) m_macWindow ) ;
525 }
526 protected :
527 WindowRef m_macWindow ;
528} ;
529
530bool wxTopLevelWindowMac::Create(wxWindow *parent,
531 wxWindowID id,
532 const wxString& title,
533 const wxPoint& pos,
534 const wxSize& size,
535 long style,
536 const wxString& name)
537{
538 // init our fields
539 Init();
540
541 m_windowStyle = style;
542
543 SetName(name);
544
545 m_windowId = id == -1 ? NewControlId() : id;
546
547 wxTopLevelWindows.Append(this);
548
549 if ( parent )
550 parent->AddChild(this);
551
fb5246be 552 return true;
2646f485
SC
553}
554
555wxTopLevelWindowMac::~wxTopLevelWindowMac()
556{
557 if ( m_macWindow )
558 {
559 wxToolTip::NotifyWindowDelete(m_macWindow) ;
560 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
561 }
562
563#if TARGET_CARBON
564 if ( m_macEventHandler )
565 {
566 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
567 m_macEventHandler = NULL ;
568 }
569#endif
570
571 wxRemoveMacWindowAssociation( this ) ;
572
573 if ( wxModelessWindows.Find(this) )
574 wxModelessWindows.DeleteObject(this);
575
576 DisposeRgn( (RgnHandle) m_macNoEraseUpdateRgn ) ;
577}
578
579
580// ----------------------------------------------------------------------------
581// wxTopLevelWindowMac maximize/minimize
582// ----------------------------------------------------------------------------
583
584void wxTopLevelWindowMac::Maximize(bool maximize)
585{
586 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( (WindowRef) m_macWindow) ) ;
587 wxMacWindowClipper clip (this);
588 ZoomWindow( (WindowRef)m_macWindow , maximize ? inZoomOut : inZoomIn , false ) ;
589
590 Rect tempRect ;
591 GrafPtr port ;
592 GetPort( &port ) ;
593 Point pt = { 0, 0 } ;
594 SetPortWindowPort((WindowRef)m_macWindow) ;
595 LocalToGlobal( &pt ) ;
596 SetPort( port ) ;
597
598 GetWindowPortBounds((WindowRef)m_macWindow, &tempRect ) ;
599 SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
600 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
601}
602
603bool wxTopLevelWindowMac::IsMaximized() const
604{
605 return IsWindowInStandardState( (WindowRef)m_macWindow , NULL , NULL ) ;
606}
607
608void wxTopLevelWindowMac::Iconize(bool iconize)
609{
610 if ( IsWindowCollapsable((WindowRef)m_macWindow) )
611 CollapseWindow((WindowRef)m_macWindow , iconize ) ;
612}
613
614bool wxTopLevelWindowMac::IsIconized() const
615{
616 return IsWindowCollapsed((WindowRef)m_macWindow ) ;
617}
618
619void wxTopLevelWindowMac::Restore()
620{
621 // not available on mac
622}
623
624// ----------------------------------------------------------------------------
625// wxTopLevelWindowMac misc
626// ----------------------------------------------------------------------------
627
628void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
629{
630 // this sets m_icon
631 wxTopLevelWindowBase::SetIcon(icon);
632}
633
634void wxTopLevelWindowMac::MacCreateRealWindow( const wxString& title,
635 const wxPoint& pos,
636 const wxSize& size,
637 long style,
638 const wxString& name )
639{
640 OSStatus err = noErr ;
641 SetName(name);
642 m_windowStyle = style;
fb5246be 643 m_isShown = false;
2646f485
SC
644
645 // create frame.
646
647 Rect theBoundsRect;
648
649 m_x = (int)pos.x;
650 m_y = (int)pos.y;
651 if ( m_y < 50 )
652 m_y = 50 ;
653 if ( m_x < 20 )
654 m_x = 20 ;
655
656 m_width = WidthDefault(size.x);
657 m_height = HeightDefault(size.y);
658
659 ::SetRect(&theBoundsRect, m_x, m_y , m_x + m_width, m_y + m_height);
660
661 // translate the window attributes in the appropriate window class and attributes
662
663 WindowClass wclass = 0;
664 WindowAttributes attr = kWindowNoAttributes ;
665
666 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
667 {
668 if (
669 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
670 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
671 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
672 )
673 {
674 wclass = kFloatingWindowClass ;
675 if ( HasFlag(wxTINY_CAPTION_VERT) )
676 {
677 attr |= kWindowSideTitlebarAttribute ;
678 }
679 }
680 else
681 {
682#if TARGET_CARBON
683 wclass = kPlainWindowClass ;
684#else
685 wclass = kFloatingWindowClass ;
686#endif
687 }
688 }
689 else if ( HasFlag( wxCAPTION ) )
690 {
691 wclass = kDocumentWindowClass ;
692 }
693 else
694 {
695 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
696 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
697 {
698 wclass = kDocumentWindowClass ;
699 }
700 else
701 {
702#if TARGET_CARBON
703 wclass = kPlainWindowClass ;
704#else
705 wclass = kModalWindowClass ;
706#endif
707 }
708 }
709
710 if ( HasFlag( wxMINIMIZE_BOX ) )
711 {
712 attr |= kWindowCollapseBoxAttribute ;
713 }
714 if ( HasFlag( wxMAXIMIZE_BOX ) )
715 {
716 attr |= kWindowFullZoomAttribute ;
717 }
718 if ( HasFlag( wxRESIZE_BORDER ) )
719 {
720 attr |= kWindowResizableAttribute ;
721 }
722 if ( HasFlag( wxCLOSE_BOX) )
723 {
724 attr |= kWindowCloseBoxAttribute ;
725 }
726
727 if (UMAGetSystemVersion() >= 0x1000)
728 {
729 //turn on live resizing (OS X only)
730 attr |= kWindowLiveResizeAttribute;
731 }
732
733#if TARGET_CARBON
734#if 0 // having problems right now with that
735 if (HasFlag(wxSTAY_ON_TOP))
736 wclass = kUtilityWindowClass;
737#endif
738#endif
739
fb5246be
WS
740 //this setup lets us have compositing and non-compositing
741 //windows in the same application.
742
743#if UNIVERSAL_INTERFACES_VERSION >= 0x0400
2646f485
SC
744 if ( wxTopLevelWindowMac::s_macWindowCompositing )
745 {
746 attr |= kWindowCompositingAttribute;
fb5246be 747 m_macUsesCompositing = true;
2646f485
SC
748 }
749 else
750#endif
751 {
fb5246be 752 m_macUsesCompositing = false;
2646f485 753 }
fb5246be 754
2646f485
SC
755#if TARGET_CARBON
756 if ( HasFlag(wxFRAME_SHAPED) )
757 {
758 WindowDefSpec customWindowDefSpec;
759 customWindowDefSpec.defType = kWindowDefProcPtr;
760 customWindowDefSpec.u.defProc = NewWindowDefUPP(wxShapedMacWindowDef);
761
762 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
763 attr, &theBoundsRect,
764 (WindowRef*) &m_macWindow);
765 }
766 else
767#endif
768 {
769 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
770 }
771
772 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
6bc3b8e9 773 wxAssociateWinWithMacWindow( (WindowRef) m_macWindow , this ) ;
2646f485
SC
774 UMASetWTitle( (WindowRef)m_macWindow , title , m_font.GetEncoding() ) ;
775 if ( wxTopLevelWindowMac::s_macWindowCompositing )
776 {
777 ::GetRootControl( (WindowRef)m_macWindow, (ControlHandle*)&m_macRootControl ) ;
778 }
779 else
780 {
781 ::CreateRootControl( (WindowRef)m_macWindow , (ControlHandle*)&m_macRootControl ) ;
782 }
783#if TARGET_CARBON
784 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
785 InstallWindowEventHandler(MAC_WXHWND(m_macWindow), GetwxMacWindowEventHandlerUPP(),
786 GetEventTypeCount(eventList), eventList, this, (EventHandlerRef *)&m_macEventHandler);
787#endif
788 m_macFocus = NULL ;
789
790
791#if TARGET_CARBON
792 if ( HasFlag(wxFRAME_SHAPED) )
793 {
794 // default shape matches the window size
795 wxRegion rgn(0, 0, m_width, m_height);
796 SetShape(rgn);
797 }
798#endif
799
800 wxWindowCreateEvent event(this);
801 GetEventHandler()->ProcessEvent(event);
802}
803
804bool wxTopLevelWindowMac::MacEnableCompositing( bool useCompositing )
805{
806 bool oldval = s_macWindowCompositing;
fb5246be
WS
807 s_macWindowCompositing = useCompositing;
808 return oldval;
2646f485
SC
809}
810
6bc3b8e9 811void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin, WXRECTPTR clipRect, WXWindow *window , wxWindowMac** rootwin)
2646f485
SC
812{
813 ((Point*)localOrigin)->h = 0;
814 ((Point*)localOrigin)->v = 0;
815 ((Rect*)clipRect)->left = 0;
816 ((Rect*)clipRect)->top = 0;
817 ((Rect*)clipRect)->right = m_width;
818 ((Rect*)clipRect)->bottom = m_height;
819 *window = m_macWindow ;
820 *rootwin = this ;
821}
822
823void wxTopLevelWindowMac::ClearBackground()
824{
825 wxWindow::ClearBackground() ;
826}
827
828WXWidget wxTopLevelWindowMac::MacGetContainerForEmbedding()
829{
830 return m_macRootControl ;
831}
832
833
834void wxTopLevelWindowMac::MacUpdate( long timestamp)
835{
836 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( (WindowRef) m_macWindow) ) ;
837
838 RgnHandle visRgn = NewRgn() ;
839 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), visRgn );
840 BeginUpdate( (WindowRef)m_macWindow ) ;
841
842 RgnHandle updateRgn = NewRgn();
843 RgnHandle diffRgn = NewRgn() ;
844
845 if ( updateRgn && diffRgn )
846 {
847#if 1
848 // macos internal control redraws clean up areas we'd like to redraw ourselves
849 // therefore we pick the boundary rect and make sure we can redraw it
850 // this has to be intersected by the visRgn in order to avoid drawing over its own
851 // boundaries
852 RgnHandle trueUpdateRgn = NewRgn() ;
853 Rect trueUpdateRgnBoundary ;
854 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), trueUpdateRgn );
855 GetRegionBounds( trueUpdateRgn , &trueUpdateRgnBoundary ) ;
856 RectRgn( updateRgn , &trueUpdateRgnBoundary ) ;
857 SectRgn( updateRgn , visRgn , updateRgn ) ;
858 if ( trueUpdateRgn )
859 DisposeRgn( trueUpdateRgn ) ;
860 SetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn ) ;
861#else
862 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn );
863#endif
864 DiffRgn( updateRgn , (RgnHandle) m_macNoEraseUpdateRgn , diffRgn ) ;
865 if ( !EmptyRgn( updateRgn ) )
866 {
867 MacRedraw( updateRgn , timestamp , m_macNeedsErasing || !EmptyRgn( diffRgn ) ) ;
868 }
869 }
870 if ( updateRgn )
871 DisposeRgn( updateRgn );
872 if ( diffRgn )
873 DisposeRgn( diffRgn );
874 if ( visRgn )
875 DisposeRgn( visRgn ) ;
876
877 EndUpdate( (WindowRef)m_macWindow ) ;
878 SetEmptyRgn( (RgnHandle) m_macNoEraseUpdateRgn ) ;
879 m_macNeedsErasing = false ;
880}
881
882
883// Raise the window to the top of the Z order
884void wxTopLevelWindowMac::Raise()
885{
886 ::SelectWindow( (WindowRef)m_macWindow ) ;
887}
888
889// Lower the window to the bottom of the Z order
890void wxTopLevelWindowMac::Lower()
891{
892 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
893}
894
895void wxTopLevelWindowMac::MacFireMouseEvent(
896 wxUint16 kind , wxInt32 x , wxInt32 y ,wxUint32 modifiers , long timestamp )
897{
898 wxMouseEvent event(wxEVT_LEFT_DOWN);
899 bool isDown = !(modifiers & btnState) ; // 1 is for up
900 bool controlDown = modifiers & controlKey ; // for simulating right mouse
901
902 event.m_leftDown = isDown && !controlDown;
903
fb5246be 904 event.m_middleDown = false;
2646f485
SC
905 event.m_rightDown = isDown && controlDown;
906
907 if ( kind == mouseDown )
908 {
909 if ( controlDown )
910 event.SetEventType(wxEVT_RIGHT_DOWN ) ;
911 else
912 event.SetEventType(wxEVT_LEFT_DOWN ) ;
913 }
914 else if ( kind == mouseUp )
915 {
916 if ( controlDown )
917 event.SetEventType(wxEVT_RIGHT_UP ) ;
918 else
919 event.SetEventType(wxEVT_LEFT_UP ) ;
920 }
921 else
922 {
923 event.SetEventType(wxEVT_MOTION ) ;
924 }
925
926 event.m_shiftDown = modifiers & shiftKey;
927 event.m_controlDown = modifiers & controlKey;
928 event.m_altDown = modifiers & optionKey;
929 event.m_metaDown = modifiers & cmdKey;
930
931 Point localwhere ;
932 localwhere.h = x ;
933 localwhere.v = y ;
934
935 GrafPtr port ;
936 ::GetPort( &port ) ;
937 ::SetPort( UMAGetWindowPort( (WindowRef)m_macWindow ) ) ;
938 ::GlobalToLocal( &localwhere ) ;
939 ::SetPort( port ) ;
940
941 if ( kind == mouseDown )
942 {
943 if ( timestamp - gs_lastWhen <= (long) GetDblTime() )
944 {
945 if ( abs( localwhere.h - gs_lastWhere.h ) < 3 && abs( localwhere.v - gs_lastWhere.v ) < 3 )
946 {
947 // This is not right if the second mouse down
3103e8a9 948 // event occurred in a different window. We
2646f485
SC
949 // correct this in MacDispatchMouseEvent.
950 if ( controlDown )
951 event.SetEventType(wxEVT_RIGHT_DCLICK ) ;
952 else
953 event.SetEventType(wxEVT_LEFT_DCLICK ) ;
954 }
955 gs_lastWhen = 0 ;
956 }
957 else
958 {
959 gs_lastWhen = timestamp ;
960 }
961 gs_lastWhere = localwhere ;
962 }
963
964 event.m_x = localwhere.h;
965 event.m_y = localwhere.v;
966 event.m_x += m_x;
967 event.m_y += m_y;
968
687706f5 969 event.SetTimestamp(timestamp);
2646f485
SC
970 event.SetEventObject(this);
971 if ( wxTheApp->s_captureWindow )
972 {
973 int x = event.m_x ;
974 int y = event.m_y ;
975 wxTheApp->s_captureWindow->ScreenToClient( &x , &y ) ;
976 event.m_x = x ;
977 event.m_y = y ;
978 event.SetEventObject( wxTheApp->s_captureWindow ) ;
979 wxTheApp->s_captureWindow->GetEventHandler()->ProcessEvent( event ) ;
980
981 if ( kind == mouseUp )
982 {
983 wxTheApp->s_captureWindow = NULL ;
984 if ( !wxIsBusy() )
985 {
986 m_cursor.MacInstall() ;
987 }
988 }
989 }
990 else
991 {
992 MacDispatchMouseEvent( event ) ;
993 }
994}
995
996#if !TARGET_CARBON
997
998void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev , short part)
999{
1000 MacFireMouseEvent( mouseDown , ((EventRecord*)ev)->where.h , ((EventRecord*)ev)->where.v ,
1001 ((EventRecord*)ev)->modifiers , ((EventRecord*)ev)->when ) ;
1002}
1003
1004void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev , short part)
1005{
1006 switch (part)
1007 {
1008 case inContent:
1009 {
1010 MacFireMouseEvent( mouseUp , ((EventRecord*)ev)->where.h , ((EventRecord*)ev)->where.v ,
1011 ((EventRecord*)ev)->modifiers , ((EventRecord*)ev)->when ) ;
1012 }
1013 break ;
1014 }
1015}
1016
1017void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev , short part)
1018{
1019 switch (part)
1020 {
1021 case inContent:
1022 {
1023 MacFireMouseEvent( nullEvent /*moved*/ , ((EventRecord*)ev)->where.h , ((EventRecord*)ev)->where.v ,
1024 ((EventRecord*)ev)->modifiers , ((EventRecord*)ev)->when ) ;
1025 }
1026 break ;
1027 }
1028}
1029
1030#endif
1031
1032void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
1033{
1034 if(s_macDeactivateWindow)
1035 {
1036 wxLogDebug(wxT("Doing delayed deactivation of %p"),s_macDeactivateWindow);
1037 s_macDeactivateWindow->MacActivate(timestamp, false);
1038 }
1039}
1040
1041void wxTopLevelWindowMac::MacActivate( long timestamp , bool inIsActivating )
1042{
1043 // wxLogDebug(wxT("TopLevel=%p::MacActivate"),this);
1044
1045 if(s_macDeactivateWindow==this)
1046 s_macDeactivateWindow=NULL;
1047 MacDelayedDeactivation(timestamp);
1048 wxActivateEvent event(wxEVT_ACTIVATE, inIsActivating , m_windowId);
687706f5 1049 event.SetTimestamp(timestamp);
2646f485
SC
1050 event.SetEventObject(this);
1051
1052 GetEventHandler()->ProcessEvent(event);
1053
1054 UMAHighlightAndActivateWindow( (WindowRef)m_macWindow , inIsActivating ) ;
1055
1056 // Early versions of MacOS X don't refresh backgrounds properly,
1057 // so refresh the whole window on activation and deactivation.
1058 long osVersion = UMAGetSystemVersion();
1059 if (osVersion >= 0x1000 && osVersion < 0x1020 )
1060 {
fb5246be 1061 Refresh(true);
2646f485
SC
1062 }
1063 else
1064 {
1065 // for the moment we have to resolve some redrawing issues like this
1066 // the OS is stealing some redrawing areas as soon as it draws a control
fb5246be 1067 Refresh(true);
2646f485
SC
1068 }
1069}
1070
1071#if !TARGET_CARBON
1072
1073void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev )
1074{
1075}
1076
1077#endif
1078
1079void wxTopLevelWindowMac::SetTitle(const wxString& title)
1080{
fb5246be 1081 wxWindow::SetLabel( title ) ;
2646f485
SC
1082 UMASetWTitle( (WindowRef)m_macWindow , title , m_font.GetEncoding() ) ;
1083}
1084
67eb4cb2 1085wxString wxTopLevelWindowMac::GetTitle() const
fb5246be
WS
1086{
1087 return wxWindow::GetLabel();
1088}
1089
2646f485
SC
1090bool wxTopLevelWindowMac::Show(bool show)
1091{
1092 if ( !wxWindow::Show(show) )
fb5246be 1093 return false;
2646f485
SC
1094
1095 if (show)
1096 {
fb5246be 1097 #if wxUSE_SYSTEM_OPTIONS //code contributed by Ryan Wilcox December 18, 2003
2646f485
SC
1098 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1) )
1099 {
1100 ::ShowWindow( (WindowRef)m_macWindow );
1101 }
1102 else
1103 #endif
1104 {
1105 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowShowTransitionAction,nil);
1106 }
1107 ::SelectWindow( (WindowRef)m_macWindow ) ;
1108 // no need to generate events here, they will get them triggered by macos
1109 // actually they should be , but apparently they are not
1110 wxSize size(m_width, m_height);
1111 wxSizeEvent event(size, m_windowId);
1112 event.SetEventObject(this);
1113 GetEventHandler()->ProcessEvent(event);
1114 }
1115 else
1116 {
1117 #if wxUSE_SYSTEM_OPTIONS
1118 if ( (wxSystemOptions::HasOption(wxMAC_WINDOW_PLAIN_TRANSITION) ) && ( wxSystemOptions::GetOptionInt( wxMAC_WINDOW_PLAIN_TRANSITION ) == 1) )
1119 {
1120 ::HideWindow((WindowRef) m_macWindow );
1121 }
1122 else
1123 #endif
1124 {
1125 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowHideTransitionAction,nil);
1126 }
1127 }
1128
1129 if ( !show )
1130 {
1131 }
1132 else
1133 {
1134 Refresh() ;
1135 }
1136
fb5246be 1137 return true;
2646f485
SC
1138}
1139
1140void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1141{
1142 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( (WindowRef) m_macWindow) ) ;
1143 wxMacWindowClipper clip (this);
1144
1145 int former_x = m_x ;
1146 int former_y = m_y ;
1147 int former_w = m_width ;
1148 int former_h = m_height ;
1149
1150 int actualWidth = width;
1151 int actualHeight = height;
1152 int actualX = x;
1153 int actualY = y;
1154
1155 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
1156 actualWidth = m_minWidth;
1157 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
1158 actualHeight = m_minHeight;
1159 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
1160 actualWidth = m_maxWidth;
1161 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
1162 actualHeight = m_maxHeight;
1163
1164 bool doMove = false ;
1165 bool doResize = false ;
1166
1167 if ( actualX != former_x || actualY != former_y )
1168 {
1169 doMove = true ;
1170 }
1171 if ( actualWidth != former_w || actualHeight != former_h )
1172 {
1173 doResize = true ;
1174 }
1175
1176 if ( doMove || doResize )
1177 {
1178 m_x = actualX ;
1179 m_y = actualY ;
1180
1181 if ( doMove )
1182 ::MoveWindow((WindowRef)m_macWindow, m_x, m_y , false); // don't make frontmost
1183
1184 m_width = actualWidth ;
1185 m_height = actualHeight ;
1186
1187 if ( doResize )
1188 ::SizeWindow((WindowRef)m_macWindow, m_width, m_height , true);
1189
1190 // the OS takes care of invalidating and erasing the new area so we only have to
1191 // take care of refreshing for full repaints
1192
1193 if ( doResize && HasFlag(wxFULL_REPAINT_ON_RESIZE) )
1194 Refresh() ;
1195
1196
1197 if ( IsKindOf( CLASSINFO( wxFrame ) ) )
1198 {
1199 wxFrame* frame = (wxFrame*) this ;
1200#if wxUSE_STATUSBAR
1201 frame->PositionStatusBar();
fb5246be 1202#endif
2646f485
SC
1203#if wxUSE_TOOLBAR
1204 frame->PositionToolBar();
fb5246be 1205#endif
2646f485
SC
1206 }
1207 if ( doMove )
1208 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
1209
1210 MacRepositionScrollBars() ;
1211 if ( doMove )
1212 {
1213 wxPoint point(m_x, m_y);
1214 wxMoveEvent event(point, m_windowId);
1215 event.SetEventObject(this);
1216 GetEventHandler()->ProcessEvent(event) ;
1217 }
1218 if ( doResize )
1219 {
1220 MacRepositionScrollBars() ;
1221 wxSize size(m_width, m_height);
1222 wxSizeEvent event(size, m_windowId);
1223 event.SetEventObject(this);
1224 GetEventHandler()->ProcessEvent(event);
1225 }
1226 }
1227
1228}
1229
1230/*
1231 * Invalidation Mechanism
1232 *
1233 * The update mechanism reflects exactely the windows mechanism
1234 * the rect gets added to the window invalidate region, if the eraseBackground flag
1235 * has been true for any part of the update rgn the background is erased in the entire region
1236 * not just in the specified rect.
1237 *
1238 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
1239 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1240 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1241 * will get the eraseBackground event first
1242 */
1243
1244void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect, bool eraseBackground )
1245{
1246 GrafPtr formerPort ;
1247 GetPort( &formerPort ) ;
1248 SetPortWindowPort( (WindowRef)m_macWindow ) ;
1249
1250 m_macNeedsErasing |= eraseBackground ;
1251
1252 // if we already know that we will have to erase, there's no need to track the rest
1253 if ( !m_macNeedsErasing)
1254 {
1255 // we end only here if eraseBackground is false
1256 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1257 // we will have to erase anyway
1258
1259 RgnHandle updateRgn = NewRgn();
1260 RgnHandle diffRgn = NewRgn() ;
1261 if ( updateRgn && diffRgn )
1262 {
1263 GetWindowUpdateRgn( (WindowRef)m_macWindow , updateRgn );
1264 Point pt = {0,0} ;
1265 LocalToGlobal( &pt ) ;
1266 OffsetRgn( updateRgn , -pt.h , -pt.v ) ;
1267 DiffRgn( updateRgn , (RgnHandle) m_macNoEraseUpdateRgn , diffRgn ) ;
1268 if ( !EmptyRgn( diffRgn ) )
1269 {
1270 m_macNeedsErasing = true ;
1271 }
1272 }
1273 if ( updateRgn )
1274 DisposeRgn( updateRgn );
1275 if ( diffRgn )
1276 DisposeRgn( diffRgn );
1277
1278 if ( !m_macNeedsErasing )
1279 {
1280 RgnHandle rectRgn = NewRgn() ;
1281 SetRectRgn( rectRgn , ((Rect*)rect)->left , ((Rect*)rect)->top , ((Rect*)rect)->right , ((Rect*)rect)->bottom ) ;
1282 UnionRgn( (RgnHandle) m_macNoEraseUpdateRgn , rectRgn , (RgnHandle) m_macNoEraseUpdateRgn ) ;
1283 DisposeRgn( rectRgn ) ;
1284 }
1285 }
1286 InvalWindowRect( (WindowRef)m_macWindow , (Rect*)rect ) ;
1287 // turn this on to debug the refreshing cycle
1288#if wxMAC_DEBUG_REDRAW
1289 PaintRect( rect ) ;
1290#endif
1291 SetPort( formerPort ) ;
1292}
1293
1294
1295bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1296{
fb5246be 1297 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
2646f485
SC
1298 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1299
1300#if TARGET_CARBON
1301 // The empty region signifies that the shape should be removed from the
1302 // window.
1303 if ( region.IsEmpty() )
1304 {
1305 wxSize sz = GetClientSize();
1306 wxRegion rgn(0, 0, sz.x, sz.y);
1307 return SetShape(rgn);
1308 }
1309
1310 // Make a copy of the region
1311 RgnHandle shapeRegion = NewRgn();
1312 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1313
1314 // Dispose of any shape region we may already have
1315 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1316 if ( oldRgn )
1317 DisposeRgn(oldRgn);
1318
1319 // Save the region so we can use it later
1320 SetWRefCon((WindowRef)MacGetWindowRef(), (SInt32)shapeRegion);
1321
1322 // Tell the window manager that the window has changed shape
1323 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
fb5246be 1324 return true;
2646f485 1325#else
fb5246be 1326 return false;
2646f485
SC
1327#endif
1328}
1329
1330// ---------------------------------------------------------------------------
1331// Support functions for shaped windows, based on Apple's CustomWindow sample at
1332// http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1333// ---------------------------------------------------------------------------
1334
1335#if TARGET_CARBON
1336
1337static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1338{
1339 GetWindowPortBounds(window, inRect);
1340 Point pt = {inRect->left, inRect->top};
1341 SetPort((GrafPtr) GetWindowPort(window));
1342 LocalToGlobal(&pt);
1343 inRect->top = pt.v;
1344 inRect->left = pt.h;
1345 inRect->bottom += pt.v;
1346 inRect->right += pt.h;
1347}
1348
1349
1350static SInt32 wxShapedMacWindowGetFeatures(WindowRef window, SInt32 param)
1351{
1352 /*------------------------------------------------------
1353 Define which options your custom window supports.
1354 --------------------------------------------------------*/
1355 //just enable everything for our demo
1356 *(OptionBits*)param=//kWindowCanGrow|
1357 //kWindowCanZoom|
1358 //kWindowCanCollapse|
1359 //kWindowCanGetWindowRegion|
1360 //kWindowHasTitleBar|
1361 //kWindowSupportsDragHilite|
1362 kWindowCanDrawInCurrentPort|
1363 //kWindowCanMeasureTitle|
1364 kWindowWantsDisposeAtProcessDeath|
1365 kWindowSupportsSetGrowImageRegion|
1366 kWindowDefSupportsColorGrafPort;
1367 return 1;
1368}
1369
1370// The content region is left as a rectangle matching the window size, this is
1371// so the origin in the paint event, and etc. still matches what the
1372// programmer expects.
1373static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1374{
1375 SetEmptyRgn(rgn);
1376 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1377 if (win)
1378 {
1379 wxRect r = win->GetRect();
1380 SetRectRgn(rgn, r.GetLeft(), r.GetTop(), r.GetRight(), r.GetBottom());
1381 }
1382}
1383
1384// The structure region is set to the shape given to the SetShape method.
1385static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1386{
1387 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1388
1389 SetEmptyRgn(rgn);
1390 if (cachedRegion)
1391 {
1392 Rect windowRect;
fb5246be
WS
1393 wxShapedMacWindowGetPos(window, &windowRect); //how big is the window
1394 CopyRgn(cachedRegion, rgn); //make a copy of our cached region
2646f485 1395 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
fb5246be 1396 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
2646f485
SC
1397 }
1398}
1399
1400
1401
1402static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1403{
1404 GetWindowRegionPtr rgnRec=(GetWindowRegionPtr)param;
1405
1406 switch(rgnRec->regionCode)
1407 {
1408 case kWindowStructureRgn:
1409 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1410 break;
1411 case kWindowContentRgn:
1412 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1413 break;
1414 default:
1415 SetEmptyRgn(rgnRec->winRgn);
1416 } //switch
1417
1418 return noErr;
1419}
1420
1421
1422static SInt32 wxShapedMacWindowHitTest(WindowRef window,SInt32 param)
1423{
1424 /*------------------------------------------------------
1425 Determine the region of the window which was hit
1426 --------------------------------------------------------*/
1427 Point hitPoint;
1428 static RgnHandle tempRgn=nil;
1429
1430 if(!tempRgn)
fb5246be 1431 tempRgn=NewRgn();
2646f485
SC
1432
1433 SetPt(&hitPoint,LoWord(param),HiWord(param));//get the point clicked
1434
1435 //Mac OS 8.5 or later
1436 wxShapedMacWindowStructureRegion(window, tempRgn);
1437 if (PtInRgn(hitPoint, tempRgn)) //in window content region?
1438 return wInContent;
1439
1440 return wNoHit;//no significant area was hit.
1441}
1442
1443
1444static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param)
1445{
1446 switch(message)
1447 {
1448 case kWindowMsgHitTest:
1449 return wxShapedMacWindowHitTest(window,param);
1450
1451 case kWindowMsgGetFeatures:
1452 return wxShapedMacWindowGetFeatures(window,param);
1453
1454 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1455 case kWindowMsgGetRegion:
1456 return wxShapedMacWindowGetRegion(window,param);
1457 }
1458
1459 return 0;
1460}
1461
1462#endif
1463// ---------------------------------------------------------------------------