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