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