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