]> git.saurik.com Git - wxWidgets.git/blame - src/mac/toplevel.cpp
reSWIGged
[wxWidgets.git] / src / mac / toplevel.cpp
CommitLineData
a15eb0a5
SC
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)
6aa89a22 9// License: wxWindows licence
a15eb0a5
SC
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"
422644a3 34 #include "wx/frame.h"
a15eb0a5
SC
35 #include "wx/string.h"
36 #include "wx/log.h"
37 #include "wx/intl.h"
38#endif //WX_PRECOMP
39
5f0b2f22 40#include "wx/mac/uma.h"
422644a3 41#include "wx/mac/aga.h"
7c091673 42#include "wx/app.h"
5f0b2f22 43#include "wx/tooltip.h"
a07c1212 44#include "wx/dnd.h"
5f0b2f22 45
dd49c691
SC
46#include "ToolUtils.h"
47
48
34dc8f91
SC
49#define wxMAC_DEBUG_REDRAW 0
50#ifndef wxMAC_DEBUG_REDRAW
51#define wxMAC_DEBUG_REDRAW 0
52#endif
53
a15eb0a5
SC
54// ----------------------------------------------------------------------------
55// globals
56// ----------------------------------------------------------------------------
57
58// list of all frames and modeless dialogs
32b5be3d
RR
59wxWindowList wxModelessWindows;
60
61// double click testing
62static Point gs_lastWhere;
63static long gs_lastWhen = 0;
64
6a7e6411 65
45ff8005 66#if TARGET_CARBON
6a7e6411 67static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param);
45ff8005 68#endif
6a7e6411 69
a15eb0a5
SC
70// ============================================================================
71// wxTopLevelWindowMac implementation
72// ============================================================================
73
851b3a88
SC
74// ---------------------------------------------------------------------------
75// Carbon Events
76// ---------------------------------------------------------------------------
77
78#if TARGET_CARBON
79
80extern long wxMacTranslateKey(unsigned char key, unsigned char code) ;
81
1542ea39 82static const EventTypeSpec eventList[] =
851b3a88
SC
83{
84 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
c5c9378c 85
e40298d5 86 { kEventClassKeyboard, kEventRawKeyDown } ,
c5c9378c
SC
87 { kEventClassKeyboard, kEventRawKeyRepeat } ,
88 { kEventClassKeyboard, kEventRawKeyUp } ,
89 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
90
851b3a88
SC
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 ;
851b3a88 108
c5c9378c 109 wxWindow* focus = wxWindow::FindFocus() ;
e40298d5 110 char charCode ;
1542ea39 111 UInt32 keyCode ;
c5c9378c 112 UInt32 modifiers ;
e40298d5 113 Point point ;
c5c9378c
SC
114
115 EventRef rawEvent ;
1542ea39 116
c5c9378c 117 GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
1542ea39 118
e40298d5
JS
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
e40298d5
JS
125 switch ( GetEventKind( event ) )
126 {
127 case kEventTextInputUnicodeForKeyEvent :
ace07c80 128 // this is only called when no default handler has jumped in, eg a wxControl on a floater window does not
5d2e69e8 129 // get its own kEventTextInputUnicodeForKeyEvent, so we route back the
ace07c80
SC
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
77a4354e
VZ
142 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
143 UInt32 message = (keyCode << 8) + charCode;
144
1542ea39 145 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
e40298d5
JS
146 focus , message , modifiers , when , point.h , point.v ) )
147 {
148 result = noErr ;
149 }
ace07c80 150 */
e40298d5
JS
151 break ;
152 }
c5c9378c
SC
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() ;
e40298d5 162 char charCode ;
1542ea39 163 UInt32 keyCode ;
c5c9378c 164 UInt32 modifiers ;
e40298d5
JS
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 :
1542ea39 179 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
e40298d5
JS
180 focus , message , modifiers , when , point.h , point.v ) )
181 {
182 result = noErr ;
183 }
184 break ;
185 case kEventRawKeyUp :
1542ea39 186 if ( (focus != NULL) && wxTheApp->MacSendKeyUpEvent(
e40298d5
JS
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
8d60dc8a 207 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & controlKey )
e40298d5
JS
208 {
209 event.m_keyCode = WXK_CONTROL ;
210 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
211 focus->GetEventHandler()->ProcessEvent( event ) ;
212 }
8d60dc8a 213 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & shiftKey )
e40298d5
JS
214 {
215 event.m_keyCode = WXK_SHIFT ;
216 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
217 focus->GetEventHandler()->ProcessEvent( event ) ;
218 }
8d60dc8a 219 if ( focus && (modifiers ^ wxTheApp->s_lastModifiers ) & optionKey )
e40298d5
JS
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 }
851b3a88
SC
229
230 return result ;
231}
232
233static pascal OSStatus MouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
234{
235 OSStatus result = eventNotHandledErr ;
236
e40298d5
JS
237 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
238 Point point ;
239 UInt32 modifiers = 0;
240 EventMouseButton button = 0 ;
241 UInt32 click = 0 ;
1542ea39 242
e40298d5
JS
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 );
1542ea39 251
e40298d5
JS
252 if ( button == 0 || GetEventKind( event ) == kEventMouseUp )
253 modifiers += btnState ;
1542ea39 254
e40298d5 255 WindowRef window ;
851b3a88 256 short windowPart = ::FindWindow(point, &window);
a3195b73 257
e40298d5 258 if ( IsWindowActive(window) && windowPart == inContent )
851b3a88 259 {
e40298d5
JS
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 }
1542ea39 282
e40298d5 283 return result ;
1542ea39 284
851b3a88
SC
285
286}
287static pascal OSStatus WindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
288{
289 OSStatus result = eventNotHandledErr ;
290 OSStatus err = noErr ;
291
e40298d5
JS
292 UInt32 attributes;
293 WindowRef windowRef ;
294 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
1542ea39 295
e40298d5
JS
296 GetEventParameter( event, kEventParamDirectObject, typeWindowRef, NULL,
297 sizeof( WindowRef ), NULL, &windowRef );
1542ea39 298
e40298d5
JS
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 );
1542ea39
RD
327
328 toplevelWindow->SetSize( newContentRect.left , newContentRect.top ,
329 newContentRect.right - newContentRect.left ,
851b3a88
SC
330 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
331
e40298d5
JS
332 result = noErr;
333 }
334 break ;
f81bfef9
SC
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 );
5d2e69e8 344
f81bfef9
SC
345 wxRect contentRect(newContentRect.left , newContentRect.top ,
346 newContentRect.right - newContentRect.left ,
347 newContentRect.bottom - newContentRect.top) ;
5d2e69e8 348
f81bfef9 349 bool handled = false ;
5d2e69e8 350 if ((attributes & kWindowBoundsChangeSizeChanged) != 0)
f81bfef9
SC
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 ;
e40298d5
JS
372 default :
373 break ;
374 }
375 return result ;
851b3a88
SC
376}
377
378pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
379{
380 OSStatus result = eventNotHandledErr ;
381
382 switch ( GetEventClass( event ) )
383 {
c5c9378c 384 case kEventClassKeyboard :
e40298d5 385 result = KeyboardEventHandler( handler, event , data ) ;
c5c9378c 386 break ;
851b3a88 387 case kEventClassTextInput :
e40298d5 388 result = TextInputEventHandler( handler, event , data ) ;
851b3a88
SC
389 break ;
390 case kEventClassWindow :
e40298d5
JS
391 result = WindowEventHandler( handler, event , data ) ;
392 break ;
851b3a88 393 case kEventClassMouse :
e40298d5
JS
394 result = MouseEventHandler( handler, event , data ) ;
395 break ;
851b3a88
SC
396 default :
397 break ;
398 }
399 return result ;
400}
401
402DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler )
403
404#endif
405
5f0b2f22
SC
406// ---------------------------------------------------------------------------
407// wxWindowMac utility functions
408// ---------------------------------------------------------------------------
409
410// Find an item given the Macintosh Window Reference
411
412wxList *wxWinMacWindowList = NULL;
76a5e5d2 413wxTopLevelWindowMac *wxFindWinFromMacWindow(WXWindow inWindowRef)
5f0b2f22
SC
414{
415 wxNode *node = wxWinMacWindowList->Find((long)inWindowRef);
416 if (!node)
417 return NULL;
eb22f2a6 418 return (wxTopLevelWindowMac *)node->GetData();
5f0b2f22
SC
419}
420
76a5e5d2 421void wxAssociateWinWithMacWindow(WXWindow inWindowRef, wxTopLevelWindowMac *win)
5f0b2f22
SC
422{
423 // adding NULL WindowRef is (first) surely a result of an error and
424 // (secondly) breaks menu command processing
427ff662 425 wxCHECK_RET( inWindowRef != (WindowRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
5f0b2f22
SC
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
a15eb0a5
SC
437// ----------------------------------------------------------------------------
438// wxTopLevelWindowMac creation
439// ----------------------------------------------------------------------------
440
76a5e5d2 441WXHWND wxTopLevelWindowMac::s_macWindowInUpdate = NULL;
245f3581 442wxTopLevelWindowMac *wxTopLevelWindowMac::s_macDeactivateWindow = NULL;
5f0b2f22 443
a15eb0a5
SC
444void wxTopLevelWindowMac::Init()
445{
446 m_iconized =
447 m_maximizeOnShow = FALSE;
5f0b2f22
SC
448 m_macNoEraseUpdateRgn = NewRgn() ;
449 m_macNeedsErasing = false ;
6a17ca35 450 m_macWindow = NULL ;
851b3a88 451#if TARGET_CARBON
7c091673 452 m_macEventHandler = NULL ;
851b3a88 453 #endif
a15eb0a5
SC
454}
455
118f012e
SC
456class wxMacDeferredWindowDeleter : public wxObject
457{
458public :
1542ea39
RD
459 wxMacDeferredWindowDeleter( WindowRef windowRef )
460 {
461 m_macWindow = windowRef ;
118f012e 462 }
1542ea39
RD
463 virtual ~wxMacDeferredWindowDeleter()
464 {
465 UMADisposeWindow( (WindowRef) m_macWindow ) ;
118f012e
SC
466 }
467 protected :
468 WindowRef m_macWindow ;
469} ;
470
a15eb0a5
SC
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{
6a17ca35
SC
498 if ( m_macWindow )
499 {
500 wxToolTip::NotifyWindowDelete(m_macWindow) ;
118f012e 501 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
6a17ca35 502 }
1542ea39 503
f75363ee 504#if TARGET_CARBON
7c091673
SC
505 if ( m_macEventHandler )
506 {
507 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
508 m_macEventHandler = NULL ;
509 }
1542ea39 510#endif
851b3a88 511
5f0b2f22
SC
512 wxRemoveMacWindowAssociation( this ) ;
513
a15eb0a5
SC
514 if ( wxModelessWindows.Find(this) )
515 wxModelessWindows.DeleteObject(this);
516
76a5e5d2 517 DisposeRgn( (RgnHandle) m_macNoEraseUpdateRgn ) ;
a15eb0a5
SC
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{
1542ea39 532 return false ;
a15eb0a5
SC
533}
534
535void wxTopLevelWindowMac::Iconize(bool iconize)
536{
537 // not available on mac
538}
539
540bool wxTopLevelWindowMac::IsIconized() const
541{
ed60b502 542 // mac dialogs cannot be iconized
a15eb0a5
SC
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}
5f0b2f22
SC
560
561void wxTopLevelWindowMac::MacCreateRealWindow( const wxString& title,
562 const wxPoint& pos,
563 const wxSize& size,
564 long style,
1542ea39 565 const wxString& name )
5f0b2f22 566{
47ac4f9b 567 OSStatus err = noErr ;
e40298d5
JS
568 SetName(name);
569 m_windowStyle = style;
570 m_isShown = FALSE;
1542ea39 571
e40298d5 572 // create frame.
1542ea39 573
5f0b2f22 574 Rect theBoundsRect;
1542ea39 575
e40298d5
JS
576 m_x = (int)pos.x;
577 m_y = (int)pos.y;
578 if ( m_y < 50 )
579 m_y = 50 ;
580 if ( m_x < 20 )
581 m_x = 20 ;
1542ea39 582
e40298d5 583 m_width = size.x;
1542ea39 584 if (m_width == -1)
5f0b2f22 585 m_width = 20;
e40298d5 586 m_height = size.y;
1542ea39 587 if (m_height == -1)
5f0b2f22 588 m_height = 20;
1542ea39 589
5f0b2f22 590 ::SetRect(&theBoundsRect, m_x, m_y , m_x + m_width, m_y + m_height);
1542ea39 591
5f0b2f22 592 // translate the window attributes in the appropriate window class and attributes
1542ea39 593
5f0b2f22
SC
594 WindowClass wclass = 0;
595 WindowAttributes attr = kWindowNoAttributes ;
1542ea39 596
f5744893 597 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
5f0b2f22 598 {
1542ea39 599 if (
f5744893
SC
600 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
601 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
602 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
e40298d5 603 )
5f0b2f22 604 {
f5744893
SC
605 wclass = kFloatingWindowClass ;
606 if ( HasFlag(wxTINY_CAPTION_VERT) )
607 {
608 attr |= kWindowSideTitlebarAttribute ;
609 }
610 }
611 else
612 {
2b5f62a0 613#if TARGET_CARBON
f5744893 614 wclass = kPlainWindowClass ;
2b5f62a0
VZ
615#else
616 wclass = kFloatingWindowClass ;
617#endif
5f0b2f22
SC
618 }
619 }
620 else if ( HasFlag( wxCAPTION ) )
621 {
1542ea39 622 wclass = kDocumentWindowClass ;
5f0b2f22
SC
623 }
624 else
625 {
f5744893 626 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
47ac4f9b 627 HasFlag( wxCLOSE_BOX ) || HasFlag( wxSYSTEM_MENU ) )
f5744893
SC
628 {
629 wclass = kDocumentWindowClass ;
630 }
631 else
632 {
2b5f62a0 633#if TARGET_CARBON
f5744893 634 wclass = kPlainWindowClass ;
2b5f62a0
VZ
635#else
636 wclass = kModalWindowClass ;
637#endif
f5744893 638 }
5f0b2f22 639 }
1542ea39 640
47ac4f9b 641 if ( HasFlag( wxMINIMIZE_BOX ) )
5f0b2f22 642 {
5f0b2f22
SC
643 attr |= kWindowCollapseBoxAttribute ;
644 }
47ac4f9b
SC
645 if ( HasFlag( wxMAXIMIZE_BOX ) )
646 {
647 attr |= kWindowFullZoomAttribute ;
648 }
5f0b2f22
SC
649 if ( HasFlag( wxRESIZE_BORDER ) )
650 {
651 attr |= kWindowResizableAttribute ;
652 }
47ac4f9b 653 if ( HasFlag( wxCLOSE_BOX) )
5f0b2f22
SC
654 {
655 attr |= kWindowCloseBoxAttribute ;
656 }
1542ea39 657
b9c10231
SC
658#if TARGET_CARBON
659#if 0 // having problems right now with that
6a7e6411
RD
660 if (HasFlag(wxSTAY_ON_TOP))
661 wclass = kUtilityWindowClass;
b9c10231
SC
662#endif
663#endif
6a7e6411 664
45ff8005 665#if TARGET_CARBON
6a7e6411
RD
666 if ( HasFlag(wxFRAME_SHAPED) )
667 {
668 WindowDefSpec customWindowDefSpec;
669 customWindowDefSpec.defType = kWindowDefProcPtr;
670 customWindowDefSpec.u.defProc = NewWindowDefUPP(wxShapedMacWindowDef);
671
47ac4f9b 672 err = ::CreateCustomWindow( &customWindowDefSpec, wclass,
6a7e6411
RD
673 attr, &theBoundsRect,
674 (WindowRef*) &m_macWindow);
675 }
676 else
45ff8005 677#endif
6a7e6411 678 {
47ac4f9b 679 err = ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
6a7e6411
RD
680 }
681
47ac4f9b 682 wxCHECK_RET( err == noErr, wxT("Mac OS error when trying to create new window") );
5f0b2f22 683 wxAssociateWinWithMacWindow( m_macWindow , this ) ;
427ff662 684 UMASetWTitle( (WindowRef)m_macWindow , title ) ;
76a5e5d2 685 ::CreateRootControl( (WindowRef)m_macWindow , (ControlHandle*)&m_macRootControl ) ;
851b3a88 686#if TARGET_CARBON
e40298d5 687 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
1542ea39 688 InstallWindowEventHandler(MAC_WXHWND(m_macWindow), GetwxMacWindowEventHandlerUPP(),
73bdd73a 689 GetEventTypeCount(eventList), eventList, this, (EventHandlerRef *)&m_macEventHandler);
851b3a88 690#endif
5f0b2f22 691 m_macFocus = NULL ;
6a7e6411
RD
692
693
45ff8005 694#if TARGET_CARBON
6a7e6411
RD
695 if ( HasFlag(wxFRAME_SHAPED) )
696 {
697 // default shape matches the window size
698 wxRegion rgn(0, 0, m_width, m_height);
699 SetShape(rgn);
700 }
45ff8005 701#endif
5f0b2f22
SC
702}
703
1542ea39 704void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin, WXRECTPTR clipRect, WXHWND *window , wxWindowMac** rootwin)
5f0b2f22 705{
76a5e5d2
SC
706 ((Point*)localOrigin)->h = 0;
707 ((Point*)localOrigin)->v = 0;
708 ((Rect*)clipRect)->left = 0;
709 ((Rect*)clipRect)->top = 0;
710 ((Rect*)clipRect)->right = m_width;
711 ((Rect*)clipRect)->bottom = m_height;
5f0b2f22
SC
712 *window = m_macWindow ;
713 *rootwin = this ;
714}
715
5d2e69e8 716void wxTopLevelWindowMac::ClearBackground()
5f0b2f22 717{
5d2e69e8 718 wxWindow::ClearBackground() ;
5f0b2f22
SC
719}
720
1542ea39 721WXWidget wxTopLevelWindowMac::MacGetContainerForEmbedding()
5f0b2f22
SC
722{
723 return m_macRootControl ;
724}
725
726
727void wxTopLevelWindowMac::MacUpdate( long timestamp)
728{
66a09d47 729 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( (WindowRef) m_macWindow) ) ;
76a5e5d2 730
1925be65
SC
731 RgnHandle visRgn = NewRgn() ;
732 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), visRgn );
76a5e5d2 733 BeginUpdate( (WindowRef)m_macWindow ) ;
5f0b2f22 734
1542ea39 735 RgnHandle updateRgn = NewRgn();
5f0b2f22 736 RgnHandle diffRgn = NewRgn() ;
1925be65 737
5f0b2f22
SC
738 if ( updateRgn && diffRgn )
739 {
732b3a75
SC
740#if 1
741 // macos internal control redraws clean up areas we'd like to redraw ourselves
742 // therefore we pick the boundary rect and make sure we can redraw it
1925be65
SC
743 // this has to be intersected by the visRgn in order to avoid drawing over its own
744 // boundaries
732b3a75
SC
745 RgnHandle trueUpdateRgn = NewRgn() ;
746 Rect trueUpdateRgnBoundary ;
747 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), trueUpdateRgn );
748 GetRegionBounds( trueUpdateRgn , &trueUpdateRgnBoundary ) ;
1925be65
SC
749 RectRgn( updateRgn , &trueUpdateRgnBoundary ) ;
750 SectRgn( updateRgn , visRgn , updateRgn ) ;
732b3a75
SC
751 if ( trueUpdateRgn )
752 DisposeRgn( trueUpdateRgn ) ;
753 SetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn ) ;
754#else
76a5e5d2 755 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn );
732b3a75 756#endif
76a5e5d2 757 DiffRgn( updateRgn , (RgnHandle) m_macNoEraseUpdateRgn , diffRgn ) ;
5f0b2f22
SC
758 if ( !EmptyRgn( updateRgn ) )
759 {
760 MacRedraw( updateRgn , timestamp , m_macNeedsErasing || !EmptyRgn( diffRgn ) ) ;
761 }
762 }
763 if ( updateRgn )
764 DisposeRgn( updateRgn );
765 if ( diffRgn )
766 DisposeRgn( diffRgn );
1925be65
SC
767 if ( visRgn )
768 DisposeRgn( visRgn ) ;
5d2e69e8 769
76a5e5d2
SC
770 EndUpdate( (WindowRef)m_macWindow ) ;
771 SetEmptyRgn( (RgnHandle) m_macNoEraseUpdateRgn ) ;
5f0b2f22
SC
772 m_macNeedsErasing = false ;
773}
774
775
776// Raise the window to the top of the Z order
777void wxTopLevelWindowMac::Raise()
778{
7f3c339c 779 ::SelectWindow( (WindowRef)m_macWindow ) ;
5f0b2f22
SC
780}
781
782// Lower the window to the bottom of the Z order
783void wxTopLevelWindowMac::Lower()
784{
76a5e5d2 785 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
5f0b2f22
SC
786}
787
1542ea39 788void wxTopLevelWindowMac::MacFireMouseEvent(
e40298d5 789 wxUint16 kind , wxInt32 x , wxInt32 y ,wxUint32 modifiers , long timestamp )
5f0b2f22
SC
790{
791 wxMouseEvent event(wxEVT_LEFT_DOWN);
851b3a88
SC
792 bool isDown = !(modifiers & btnState) ; // 1 is for up
793 bool controlDown = modifiers & controlKey ; // for simulating right mouse
1542ea39 794
5f0b2f22 795 event.m_leftDown = isDown && !controlDown;
1542ea39 796
5f0b2f22
SC
797 event.m_middleDown = FALSE;
798 event.m_rightDown = isDown && controlDown;
799
851b3a88 800 if ( kind == mouseDown )
5f0b2f22
SC
801 {
802 if ( controlDown )
803 event.SetEventType(wxEVT_RIGHT_DOWN ) ;
804 else
805 event.SetEventType(wxEVT_LEFT_DOWN ) ;
806 }
851b3a88 807 else if ( kind == mouseUp )
5f0b2f22
SC
808 {
809 if ( controlDown )
810 event.SetEventType(wxEVT_RIGHT_UP ) ;
811 else
812 event.SetEventType(wxEVT_LEFT_UP ) ;
813 }
814 else
815 {
816 event.SetEventType(wxEVT_MOTION ) ;
817 }
818
851b3a88
SC
819 event.m_shiftDown = modifiers & shiftKey;
820 event.m_controlDown = modifiers & controlKey;
821 event.m_altDown = modifiers & optionKey;
822 event.m_metaDown = modifiers & cmdKey;
5f0b2f22 823
851b3a88
SC
824 Point localwhere ;
825 localwhere.h = x ;
826 localwhere.v = y ;
1542ea39
RD
827
828 GrafPtr port ;
5f0b2f22 829 ::GetPort( &port ) ;
76a5e5d2 830 ::SetPort( UMAGetWindowPort( (WindowRef)m_macWindow ) ) ;
5f0b2f22
SC
831 ::GlobalToLocal( &localwhere ) ;
832 ::SetPort( port ) ;
833
851b3a88 834 if ( kind == mouseDown )
5f0b2f22 835 {
5be55d56 836 if ( timestamp - gs_lastWhen <= (long) GetDblTime() )
5f0b2f22 837 {
32b5be3d 838 if ( abs( localwhere.h - gs_lastWhere.h ) < 3 && abs( localwhere.v - gs_lastWhere.v ) < 3 )
5f0b2f22 839 {
32b5be3d
RR
840 // This is not right if the second mouse down
841 // event occured in a differen window. We
842 // correct this in MacDispatchMouseEvent.
5f0b2f22
SC
843 if ( controlDown )
844 event.SetEventType(wxEVT_RIGHT_DCLICK ) ;
845 else
846 event.SetEventType(wxEVT_LEFT_DCLICK ) ;
847 }
32b5be3d 848 gs_lastWhen = 0 ;
5f0b2f22
SC
849 }
850 else
851 {
851b3a88 852 gs_lastWhen = timestamp ;
5f0b2f22 853 }
32b5be3d 854 gs_lastWhere = localwhere ;
5f0b2f22
SC
855 }
856
857 event.m_x = localwhere.h;
858 event.m_y = localwhere.v;
859 event.m_x += m_x;
860 event.m_y += m_y;
861
851b3a88 862 event.m_timeStamp = timestamp;
5f0b2f22
SC
863 event.SetEventObject(this);
864 if ( wxTheApp->s_captureWindow )
865 {
866 int x = event.m_x ;
867 int y = event.m_y ;
868 wxTheApp->s_captureWindow->ScreenToClient( &x , &y ) ;
869 event.m_x = x ;
870 event.m_y = y ;
2e6857fa 871 event.SetEventObject( wxTheApp->s_captureWindow ) ;
5f0b2f22 872 wxTheApp->s_captureWindow->GetEventHandler()->ProcessEvent( event ) ;
1542ea39 873
851b3a88 874 if ( kind == mouseUp )
5f0b2f22
SC
875 {
876 wxTheApp->s_captureWindow = NULL ;
6b57b49a 877 if ( !wxIsBusy() )
5f0b2f22
SC
878 {
879 m_cursor.MacInstall() ;
880 }
881 }
882 }
883 else
884 {
885 MacDispatchMouseEvent( event ) ;
886 }
887}
e40298d5 888
851b3a88 889#if !TARGET_CARBON
5f0b2f22 890
76a5e5d2 891void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev , short part)
5f0b2f22 892{
1542ea39 893 MacFireMouseEvent( mouseDown , ((EventRecord*)ev)->where.h , ((EventRecord*)ev)->where.v ,
e40298d5 894 ((EventRecord*)ev)->modifiers , ((EventRecord*)ev)->when ) ;
5f0b2f22
SC
895}
896
76a5e5d2 897void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev , short part)
5f0b2f22
SC
898{
899 switch (part)
900 {
1542ea39 901 case inContent:
5f0b2f22 902 {
1542ea39 903 MacFireMouseEvent( mouseUp , ((EventRecord*)ev)->where.h , ((EventRecord*)ev)->where.v ,
e40298d5 904 ((EventRecord*)ev)->modifiers , ((EventRecord*)ev)->when ) ;
5f0b2f22
SC
905 }
906 break ;
907 }
908}
909
76a5e5d2 910void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev , short part)
5f0b2f22
SC
911{
912 switch (part)
913 {
1542ea39 914 case inContent:
5f0b2f22 915 {
1542ea39 916 MacFireMouseEvent( nullEvent /*moved*/ , ((EventRecord*)ev)->where.h , ((EventRecord*)ev)->where.v ,
e40298d5 917 ((EventRecord*)ev)->modifiers , ((EventRecord*)ev)->when ) ;
5f0b2f22
SC
918 }
919 break ;
920 }
921}
851b3a88
SC
922
923#endif
924
245f3581
DE
925void wxTopLevelWindowMac::MacDelayedDeactivation(long timestamp)
926{
927 if(s_macDeactivateWindow)
928 {
929 wxLogDebug("Doing delayed deactivation of %p",s_macDeactivateWindow);
930 s_macDeactivateWindow->MacActivate(timestamp, false);
931 }
932}
933
851b3a88 934void wxTopLevelWindowMac::MacActivate( long timestamp , bool inIsActivating )
5f0b2f22 935{
245f3581
DE
936 wxLogDebug("TopLevel=%p::MacActivate",this);
937 if(s_macDeactivateWindow==this)
938 s_macDeactivateWindow=NULL;
939 MacDelayedDeactivation(timestamp);
5f0b2f22 940 wxActivateEvent event(wxEVT_ACTIVATE, inIsActivating , m_windowId);
851b3a88 941 event.m_timeStamp = timestamp ;
5f0b2f22 942 event.SetEventObject(this);
1542ea39 943
5f0b2f22 944 GetEventHandler()->ProcessEvent(event);
1542ea39 945
76a5e5d2 946 UMAHighlightAndActivateWindow( (WindowRef)m_macWindow , inIsActivating ) ;
1542ea39 947
a006e78b
JS
948 // Early versions of MacOS X don't refresh backgrounds properly,
949 // so refresh the whole window on activation and deactivation.
950 long osVersion = UMAGetSystemVersion();
1925be65 951 if (osVersion >= 0x1000 && osVersion < 0x1020 )
732b3a75 952 {
a006e78b 953 Refresh(TRUE);
732b3a75 954 }
a006e78b 955 else
732b3a75
SC
956 {
957 // for the moment we have to resolve some redrawing issues like this
958 // the OS is stealing some redrawing areas as soon as it draws a control
959 Refresh(TRUE);
960 }
5f0b2f22
SC
961}
962
851b3a88
SC
963#if !TARGET_CARBON
964
1542ea39 965void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev )
5f0b2f22
SC
966{
967}
968
851b3a88
SC
969#endif
970
5f0b2f22
SC
971void wxTopLevelWindowMac::SetTitle(const wxString& title)
972{
973 wxWindow::SetTitle( title ) ;
427ff662 974 UMASetWTitle( (WindowRef)m_macWindow , title ) ;
5f0b2f22
SC
975}
976
977bool wxTopLevelWindowMac::Show(bool show)
978{
979 if ( !wxWindow::Show(show) )
980 return FALSE;
981
982 if (show)
1542ea39 983 {
1925be65 984 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowShowTransitionAction,nil);
76a5e5d2 985 ::SelectWindow( (WindowRef)m_macWindow ) ;
5f0b2f22
SC
986 // no need to generate events here, they will get them triggered by macos
987 // actually they should be , but apparently they are not
988 wxSize size(m_width, m_height);
989 wxSizeEvent event(size, m_windowId);
990 event.SetEventObject(this);
991 GetEventHandler()->ProcessEvent(event);
992 }
993 else
994 {
1925be65 995 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowHideTransitionAction,nil);
5f0b2f22
SC
996 }
997
998 if ( !show )
999 {
1000 }
1001 else
1002 {
1542ea39 1003 Refresh() ;
5f0b2f22
SC
1004 }
1005
1006 return TRUE;
1007}
1008
1009void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
1010{
1011 int former_x = m_x ;
1012 int former_y = m_y ;
1013 int former_w = m_width ;
1014 int former_h = m_height ;
1542ea39 1015
e40298d5
JS
1016 int actualWidth = width;
1017 int actualHeight = height;
1018 int actualX = x;
1019 int actualY = y;
1542ea39
RD
1020
1021 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
5f0b2f22 1022 actualWidth = m_minWidth;
1542ea39 1023 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
5f0b2f22 1024 actualHeight = m_minHeight;
1542ea39 1025 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
5f0b2f22 1026 actualWidth = m_maxWidth;
1542ea39 1027 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
5f0b2f22
SC
1028 actualHeight = m_maxHeight;
1029
1030 bool doMove = false ;
1031 bool doResize = false ;
1542ea39 1032
5f0b2f22
SC
1033 if ( actualX != former_x || actualY != former_y )
1034 {
1035 doMove = true ;
1036 }
1037 if ( actualWidth != former_w || actualHeight != former_h )
1038 {
1039 doResize = true ;
1040 }
1041
1042 if ( doMove || doResize )
1043 {
1044 m_x = actualX ;
1045 m_y = actualY ;
1046 m_width = actualWidth ;
1047 m_height = actualHeight ;
1048
1049 if ( doMove )
76a5e5d2 1050 ::MoveWindow((WindowRef)m_macWindow, m_x, m_y , false); // don't make frontmost
1542ea39 1051
5f0b2f22 1052 if ( doResize )
1542ea39
RD
1053 ::SizeWindow((WindowRef)m_macWindow, m_width, m_height , true);
1054
2b5f62a0
VZ
1055 // the OS takes care of invalidating and erasing the new area so we only have to
1056 // take care of refreshing for full repaints
1057
1058 if ( doResize && !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE) )
1059 Refresh() ;
1542ea39
RD
1060
1061
5f0b2f22
SC
1062 if ( IsKindOf( CLASSINFO( wxFrame ) ) )
1063 {
1064 wxFrame* frame = (wxFrame*) this ;
1065 frame->PositionStatusBar();
1066 frame->PositionToolBar();
1067 }
1068 if ( doMove )
1069 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
1070
1071 MacRepositionScrollBars() ;
1072 if ( doMove )
1073 {
1074 wxPoint point(m_x, m_y);
1075 wxMoveEvent event(point, m_windowId);
1076 event.SetEventObject(this);
1077 GetEventHandler()->ProcessEvent(event) ;
1078 }
1079 if ( doResize )
1080 {
1081 MacRepositionScrollBars() ;
1082 wxSize size(m_width, m_height);
1083 wxSizeEvent event(size, m_windowId);
1084 event.SetEventObject(this);
1085 GetEventHandler()->ProcessEvent(event);
1086 }
1087 }
1542ea39 1088
5f0b2f22
SC
1089}
1090
1091/*
1092 * Invalidation Mechanism
1093 *
1094 * The update mechanism reflects exactely the windows mechanism
1095 * the rect gets added to the window invalidate region, if the eraseBackground flag
1096 * has been true for any part of the update rgn the background is erased in the entire region
1097 * not just in the specified rect.
1098 *
1542ea39 1099 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
5f0b2f22
SC
1100 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
1101 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
1102 * will get the eraseBackground event first
1103 */
1542ea39
RD
1104
1105void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect, bool eraseBackground )
5f0b2f22 1106{
e40298d5
JS
1107 GrafPtr formerPort ;
1108 GetPort( &formerPort ) ;
1109 SetPortWindowPort( (WindowRef)m_macWindow ) ;
1542ea39 1110
e40298d5 1111 m_macNeedsErasing |= eraseBackground ;
1542ea39 1112
e40298d5
JS
1113 // if we already know that we will have to erase, there's no need to track the rest
1114 if ( !m_macNeedsErasing)
5f0b2f22 1115 {
e40298d5
JS
1116 // we end only here if eraseBackground is false
1117 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
1118 // we will have to erase anyway
1542ea39
RD
1119
1120 RgnHandle updateRgn = NewRgn();
e40298d5
JS
1121 RgnHandle diffRgn = NewRgn() ;
1122 if ( updateRgn && diffRgn )
1123 {
1124 GetWindowUpdateRgn( (WindowRef)m_macWindow , updateRgn );
1125 Point pt = {0,0} ;
1126 LocalToGlobal( &pt ) ;
1127 OffsetRgn( updateRgn , -pt.h , -pt.v ) ;
1128 DiffRgn( updateRgn , (RgnHandle) m_macNoEraseUpdateRgn , diffRgn ) ;
1129 if ( !EmptyRgn( diffRgn ) )
1130 {
1131 m_macNeedsErasing = true ;
1132 }
1133 }
1134 if ( updateRgn )
1135 DisposeRgn( updateRgn );
1136 if ( diffRgn )
1137 DisposeRgn( diffRgn );
1542ea39 1138
e40298d5 1139 if ( !m_macNeedsErasing )
5f0b2f22 1140 {
e40298d5
JS
1141 RgnHandle rectRgn = NewRgn() ;
1142 SetRectRgn( rectRgn , ((Rect*)rect)->left , ((Rect*)rect)->top , ((Rect*)rect)->right , ((Rect*)rect)->bottom ) ;
1143 UnionRgn( (RgnHandle) m_macNoEraseUpdateRgn , rectRgn , (RgnHandle) m_macNoEraseUpdateRgn ) ;
1144 DisposeRgn( rectRgn ) ;
5f0b2f22
SC
1145 }
1146 }
e40298d5
JS
1147 InvalWindowRect( (WindowRef)m_macWindow , (Rect*)rect ) ;
1148 // turn this on to debug the refreshing cycle
34dc8f91 1149#if wxMAC_DEBUG_REDRAW
e40298d5 1150 PaintRect( rect ) ;
5f0b2f22 1151#endif
e40298d5 1152 SetPort( formerPort ) ;
5f0b2f22 1153}
a07c1212 1154
6a7e6411 1155
1542ea39
RD
1156bool wxTopLevelWindowMac::SetShape(const wxRegion& region)
1157{
6a7e6411
RD
1158 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), FALSE,
1159 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
1160
45ff8005 1161#if TARGET_CARBON
6a7e6411
RD
1162 // The empty region signifies that the shape should be removed from the
1163 // window.
1164 if ( region.IsEmpty() )
1165 {
1166 wxSize sz = GetClientSize();
1167 wxRegion rgn(0, 0, sz.x, sz.y);
1168 return SetShape(rgn);
1169 }
1170
1171 // Make a copy of the region
1172 RgnHandle shapeRegion = NewRgn();
1173 CopyRgn( (RgnHandle)region.GetWXHRGN(), shapeRegion );
1174
1175 // Dispose of any shape region we may already have
1176 RgnHandle oldRgn = (RgnHandle)GetWRefCon( (WindowRef)MacGetWindowRef() );
1177 if ( oldRgn )
1178 DisposeRgn(oldRgn);
1179
1180 // Save the region so we can use it later
1181 SetWRefCon((WindowRef)MacGetWindowRef(), (SInt32)shapeRegion);
1182
1183 // Tell the window manager that the window has changed shape
1184 ReshapeCustomWindow((WindowRef)MacGetWindowRef());
1185 return TRUE;
45ff8005
RD
1186#else
1187 return FALSE;
1188#endif
6a7e6411
RD
1189}
1190
6a7e6411
RD
1191// ---------------------------------------------------------------------------
1192// Support functions for shaped windows, based on Apple's CustomWindow sample at
1193// http://developer.apple.com/samplecode/Sample_Code/Human_Interface_Toolbox/Mac_OS_High_Level_Toolbox/CustomWindow.htm
1194// ---------------------------------------------------------------------------
1195
45ff8005 1196#if TARGET_CARBON
1925be65 1197
6a7e6411
RD
1198static void wxShapedMacWindowGetPos(WindowRef window, Rect* inRect)
1199{
1200 GetWindowPortBounds(window, inRect);
1201 Point pt = {inRect->left, inRect->top};
1202 SetPort((GrafPtr) GetWindowPort(window));
1203 LocalToGlobal(&pt);
1204 inRect->top = pt.v;
1205 inRect->left = pt.h;
1206 inRect->bottom += pt.v;
1207 inRect->right += pt.h;
1208}
1209
1210
1211static SInt32 wxShapedMacWindowGetFeatures(WindowRef window, SInt32 param)
1212{
1213 /*------------------------------------------------------
1214 Define which options your custom window supports.
1215 --------------------------------------------------------*/
1216 //just enable everything for our demo
1217 *(OptionBits*)param=//kWindowCanGrow|
1218 //kWindowCanZoom|
1219 //kWindowCanCollapse|
1220 //kWindowCanGetWindowRegion|
1221 //kWindowHasTitleBar|
1222 //kWindowSupportsDragHilite|
1223 kWindowCanDrawInCurrentPort|
1224 //kWindowCanMeasureTitle|
1225 kWindowWantsDisposeAtProcessDeath|
1226 kWindowSupportsSetGrowImageRegion|
1227 kWindowDefSupportsColorGrafPort;
1228 return 1;
1542ea39 1229}
6a7e6411
RD
1230
1231// The content region is left as a rectangle matching the window size, this is
1232// so the origin in the paint event, and etc. still matches what the
1233// programmer expects.
1234static void wxShapedMacWindowContentRegion(WindowRef window, RgnHandle rgn)
1235{
1236 SetEmptyRgn(rgn);
1237 wxTopLevelWindowMac* win = wxFindWinFromMacWindow(window);
1238 if (win)
1239 {
1240 wxRect r = win->GetRect();
1241 SetRectRgn(rgn, r.GetLeft(), r.GetTop(), r.GetRight(), r.GetBottom());
1242 }
1243}
1244
1245// The structure region is set to the shape given to the SetShape method.
1246static void wxShapedMacWindowStructureRegion(WindowRef window, RgnHandle rgn)
1247{
1248 RgnHandle cachedRegion = (RgnHandle) GetWRefCon(window);
1249
1250 SetEmptyRgn(rgn);
1251 if (cachedRegion)
1252 {
1253 Rect windowRect;
1254 wxShapedMacWindowGetPos(window, &windowRect); //how big is the window
1255 CopyRgn(cachedRegion, rgn); //make a copy of our cached region
1256 OffsetRgn(rgn, windowRect.left, windowRect.top); // position it over window
1257 //MapRgn(rgn, &mMaskSize, &windowRect); //scale it to our actual window size
1258 }
1259}
1260
1261
1262
1263static SInt32 wxShapedMacWindowGetRegion(WindowRef window, SInt32 param)
1264{
1265 GetWindowRegionPtr rgnRec=(GetWindowRegionPtr)param;
1266
1267 switch(rgnRec->regionCode)
1268 {
1269 case kWindowStructureRgn:
1270 wxShapedMacWindowStructureRegion(window, rgnRec->winRgn);
1271 break;
1272 case kWindowContentRgn:
1273 wxShapedMacWindowContentRegion(window, rgnRec->winRgn);
1274 break;
1275 default:
1276 SetEmptyRgn(rgnRec->winRgn);
1277 } //switch
1278
1279 return noErr;
1280}
1281
1282
1283static SInt32 wxShapedMacWindowHitTest(WindowRef window,SInt32 param)
1284{
1285 /*------------------------------------------------------
1286 Determine the region of the window which was hit
1287 --------------------------------------------------------*/
1288 Point hitPoint;
1289 static RgnHandle tempRgn=nil;
1290
1291 if(!tempRgn)
1292 tempRgn=NewRgn();
1293
1294 SetPt(&hitPoint,LoWord(param),HiWord(param));//get the point clicked
1295
1296 //Mac OS 8.5 or later
1297 wxShapedMacWindowStructureRegion(window, tempRgn);
1298 if (PtInRgn(hitPoint, tempRgn)) //in window content region?
1299 return wInContent;
1300
1301 return wNoHit;//no significant area was hit.
1302}
1303
1304
1305static pascal long wxShapedMacWindowDef(short varCode, WindowRef window, SInt16 message, SInt32 param)
1306{
1307 switch(message)
1308 {
1309 case kWindowMsgHitTest:
1310 return wxShapedMacWindowHitTest(window,param);
1311
1312 case kWindowMsgGetFeatures:
1313 return wxShapedMacWindowGetFeatures(window,param);
1314
1315 // kWindowMsgGetRegion is sent during CreateCustomWindow and ReshapeCustomWindow
1316 case kWindowMsgGetRegion:
1317 return wxShapedMacWindowGetRegion(window,param);
1318 }
1319
1320 return 0;
1321}
1925be65 1322
45ff8005 1323#endif
6a7e6411 1324// ---------------------------------------------------------------------------
1925be65 1325