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