]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/window.cpp
deactivate read-only implementation for unicodetextcontrol because it cannot be selec...
[wxWidgets.git] / src / mac / carbon / window.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
fb5246be 2// Name: src/mac/carbon/window.cpp
e766c8a9 3// Purpose: wxWindowMac
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
3d1a4878
SC
12#include "wx/wxprec.h"
13
e9576ca5 14#include "wx/menu.h"
5fde6fcc 15#include "wx/window.h"
e9576ca5
SC
16#include "wx/dc.h"
17#include "wx/dcclient.h"
14c9cbdb 18#include "wx/utils.h"
e9576ca5
SC
19#include "wx/app.h"
20#include "wx/panel.h"
21#include "wx/layout.h"
22#include "wx/dialog.h"
03e11df5
GD
23#include "wx/scrolbar.h"
24#include "wx/statbox.h"
e9576ca5
SC
25#include "wx/button.h"
26#include "wx/settings.h"
27#include "wx/msgdlg.h"
28#include "wx/frame.h"
2f1ae414 29#include "wx/tooltip.h"
c809f3be 30#include "wx/statusbr.h"
e9576ca5 31#include "wx/menuitem.h"
4ac219f6 32#include "wx/spinctrl.h"
e9576ca5 33#include "wx/log.h"
467e3168 34#include "wx/geometry.h"
00627198 35#include "wx/textctrl.h"
e9576ca5 36
facd6764
SC
37#include "wx/toolbar.h"
38#include "wx/dc.h"
39
7c551d95
SC
40#if wxUSE_CARET
41 #include "wx/caret.h"
e15f0a5e 42#endif
7c551d95 43
db7a550b
SC
44#define MAC_SCROLLBAR_SIZE 15
45#define MAC_SMALL_SCROLLBAR_SIZE 11
519cb848 46
d497dca4 47#include "wx/mac/uma.h"
898d9035 48
66a09d47
SC
49#ifndef __DARWIN__
50#include <Windows.h>
51#include <ToolUtils.h>
1d879215
DS
52#include <Scrap.h>
53#include <MacTextEditor.h>
66a09d47 54#endif
519cb848 55
41218df1 56#if TARGET_API_MAC_OSX
facd6764 57#ifndef __HIVIEW__
8b573fb8 58 #include <HIToolbox/HIView.h>
facd6764 59#endif
41218df1 60#endif
facd6764 61
e9576ca5
SC
62#if wxUSE_DRAG_AND_DROP
63#include "wx/dnd.h"
64#endif
65
66#include <string.h>
67
68extern wxList wxPendingDelete;
69
fc0daf84
SC
70#ifdef __WXUNIVERSAL__
71 IMPLEMENT_ABSTRACT_CLASS(wxWindowMac, wxWindowBase)
898d9035 72#else
fc0daf84 73 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
898d9035 74#endif
fc0daf84 75
fc0daf84 76BEGIN_EVENT_TABLE(wxWindowMac, wxWindowBase)
facd6764
SC
77 EVT_NC_PAINT(wxWindowMac::OnNcPaint)
78 EVT_ERASE_BACKGROUND(wxWindowMac::OnEraseBackground)
ff3795ee
SC
79#if TARGET_API_MAC_OSX
80 EVT_PAINT(wxWindowMac::OnPaint)
81#endif
5ca0d812
SC
82 EVT_SET_FOCUS(wxWindowMac::OnSetFocus)
83 EVT_KILL_FOCUS(wxWindowMac::OnSetFocus)
facd6764 84 EVT_MOUSE_EVENTS(wxWindowMac::OnMouseEvent)
e9576ca5
SC
85END_EVENT_TABLE()
86
94abc21f
SC
87#define wxMAC_DEBUG_REDRAW 0
88#ifndef wxMAC_DEBUG_REDRAW
89#define wxMAC_DEBUG_REDRAW 0
90#endif
91
5ca0d812 92#define wxMAC_USE_THEME_BORDER 1
e9576ca5 93
42ef83fa
SC
94// ---------------------------------------------------------------------------
95// Utility Routines to move between different coordinate systems
96// ---------------------------------------------------------------------------
97
98/*
99 * Right now we have the following setup :
100 * a border that is not part of the native control is always outside the
101 * control's border (otherwise we loose all native intelligence, future ways
102 * may be to have a second embedding control responsible for drawing borders
103 * and backgrounds eventually)
104 * so all this border calculations have to be taken into account when calling
105 * native methods or getting native oriented data
106 * so we have three coordinate systems here
107 * wx client coordinates
108 * wx window coordinates (including window frames)
109 * native coordinates
110 */
8b573fb8 111
42ef83fa
SC
112//
113// originating from native control
114//
115
116
117void wxMacNativeToWindow( const wxWindow* window , RgnHandle handle )
118{
8b573fb8 119 OffsetRgn( handle , window->MacGetLeftBorderSize() , window->MacGetTopBorderSize() ) ;
42ef83fa
SC
120}
121
122void wxMacNativeToWindow( const wxWindow* window , Rect *rect )
123{
8b573fb8 124 OffsetRect( rect , window->MacGetLeftBorderSize() , window->MacGetTopBorderSize() ) ;
42ef83fa
SC
125}
126
127//
128// directed towards native control
129//
130
131void wxMacWindowToNative( const wxWindow* window , RgnHandle handle )
132{
8b573fb8 133 OffsetRgn( handle , -window->MacGetLeftBorderSize() , -window->MacGetTopBorderSize() );
42ef83fa
SC
134}
135
136void wxMacWindowToNative( const wxWindow* window , Rect *rect )
137{
8b573fb8 138 OffsetRect( rect , -window->MacGetLeftBorderSize() , -window->MacGetTopBorderSize() ) ;
42ef83fa
SC
139}
140
facd6764
SC
141// ---------------------------------------------------------------------------
142// Carbon Events
143// ---------------------------------------------------------------------------
8b573fb8 144
facd6764
SC
145extern long wxMacTranslateKey(unsigned char key, unsigned char code) ;
146pascal OSStatus wxMacSetupControlBackground( ControlRef iControl , SInt16 iMessage , SInt16 iDepth , Boolean iIsColor ) ;
147
f1d527c1
SC
148#if TARGET_API_MAC_OSX
149
8b573fb8 150#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_3
898d9035
DS
151enum
152{
153 kEventControlVisibilityChanged = 157
c2697b87
SC
154};
155#endif
156
f1d527c1
SC
157#endif
158
facd6764
SC
159static const EventTypeSpec eventList[] =
160{
f1d527c1 161 { kEventClassControl , kEventControlHit } ,
898d9035 162
facd6764 163#if TARGET_API_MAC_OSX
980ee83f
SC
164 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
165 { kEventClassTextInput, kEventTextInputUpdateActiveInputArea } ,
166
73fe67bd
SC
167 { kEventClassControl , kEventControlDraw } ,
168 { kEventClassControl , kEventControlVisibilityChanged } ,
169 { kEventClassControl , kEventControlEnabledStateChanged } ,
170 { kEventClassControl , kEventControlHiliteChanged } ,
f1d527c1 171 { kEventClassControl , kEventControlSetFocusPart } ,
8b573fb8
VZ
172
173 { kEventClassService , kEventServiceGetTypes },
174 { kEventClassService , kEventServiceCopy },
175 { kEventClassService , kEventServicePaste },
176
898d9035
DS
177// { kEventClassControl , kEventControlInvalidateForSizeChange } , // 10.3 only
178// { kEventClassControl , kEventControlBoundsChanged } ,
facd6764
SC
179#endif
180} ;
181
182static pascal OSStatus wxMacWindowControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
183{
184 OSStatus result = eventNotHandledErr ;
185
186 wxMacCarbonEvent cEvent( event ) ;
8b573fb8 187
facd6764
SC
188 ControlRef controlRef ;
189 wxWindowMac* thisWindow = (wxWindowMac*) data ;
190
191 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
192
898d9035 193 switch ( GetEventKind( event ) )
facd6764 194 {
f1d527c1 195#if TARGET_API_MAC_OSX
facd6764
SC
196 case kEventControlDraw :
197 {
1e8cde71 198 RgnHandle updateRgn = NULL ;
055a486b 199 RgnHandle allocatedRgn = NULL ;
1e8cde71 200 wxRegion visRegion = thisWindow->MacGetVisibleRegion() ;
e6616741 201 Rect controlBounds ;
898d9035
DS
202
203 if ( ! thisWindow->GetPeer()->IsCompositing() )
e6616741 204 {
898d9035 205 if ( thisWindow->GetPeer()->IsRootControl() )
e6616741 206 thisWindow->GetPeer()->GetRect( &controlBounds ) ;
898d9035
DS
207 else
208 GetControlBounds( thisWindow->GetPeer()->GetControlRef() , &controlBounds ) ;
e6616741 209 }
902725ee 210
76c7012c 211 if ( cEvent.GetParameter<RgnHandle>(kEventParamRgnHandle, &updateRgn) != noErr )
1e8cde71
SC
212 {
213 updateRgn = (RgnHandle) visRegion.GetWXHRGN() ;
214 }
055a486b
SC
215 else
216 {
898d9035 217 if ( ! thisWindow->GetPeer()->IsCompositing() )
055a486b
SC
218 {
219 allocatedRgn = NewRgn() ;
220 CopyRgn( updateRgn , allocatedRgn ) ;
e6616741 221 OffsetRgn( allocatedRgn , -controlBounds.left , -controlBounds.top ) ;
898d9035 222
42ef83fa
SC
223 // hide the given region by the new region that must be shifted
224 wxMacNativeToWindow( thisWindow , allocatedRgn ) ;
902725ee 225 updateRgn = allocatedRgn ;
e6616741
SC
226 }
227 else
228 {
229 if ( thisWindow->MacGetLeftBorderSize() != 0 || thisWindow->MacGetTopBorderSize() != 0 )
230 {
231 // as this update region is in native window locals we must adapt it to wx window local
232 allocatedRgn = NewRgn() ;
233 CopyRgn( updateRgn , allocatedRgn ) ;
898d9035 234
e6616741
SC
235 // hide the given region by the new region that must be shifted
236 wxMacNativeToWindow( thisWindow , allocatedRgn ) ;
237 updateRgn = allocatedRgn ;
238 }
055a486b
SC
239 }
240 }
e15f0a5e 241
e6616741
SC
242 Rect rgnBounds ;
243 GetRegionBounds( updateRgn , &rgnBounds ) ;
898d9035 244
1f1c8bd4 245#if wxMAC_DEBUG_REDRAW
facd6764
SC
246 if ( thisWindow->MacIsUserPane() )
247 {
bcbd6987
SC
248 static float color = 0.5 ;
249 static channel = 0 ;
898d9035
DS
250 HIRect bounds;
251 CGContextRef cgContext = cEvent.GetParameter<CGContextRef>(kEventParamCGContextRef) ;
252
253 HIViewGetBounds( controlRef, &bounds );
8b573fb8
VZ
254 CGContextSetRGBFillColor( cgContext, channel == 0 ? color : 0.5 ,
255 channel == 1 ? color : 0.5 , channel == 2 ? color : 0.5 , 1 );
256 CGContextFillRect( cgContext, bounds );
257 color += 0.1 ;
258 if ( color > 0.9 )
259 {
260 color = 0.5 ;
261 channel++ ;
262 if ( channel == 3 )
263 channel = 0 ;
264 }
facd6764 265 }
20b69855 266#endif
898d9035 267
c79aad8b 268 {
20b69855 269#if wxMAC_USE_CORE_GRAPHICS
e6616741 270 bool created = false ;
8523a5f5 271 CGContextRef cgContext = NULL ;
e6616741
SC
272 if ( cEvent.GetParameter<CGContextRef>(kEventParamCGContextRef, &cgContext) != noErr )
273 {
274 wxASSERT( thisWindow->GetPeer()->IsCompositing() == false ) ;
902725ee 275
e6616741
SC
276 // this parameter is not provided on non-composited windows
277 created = true ;
898d9035 278
902725ee 279 // rest of the code expects this to be already transformed and clipped for local
e6616741
SC
280 CGrafPtr port = GetWindowPort( (WindowRef) thisWindow->MacGetTopLevelWindowRef() ) ;
281 Rect bounds ;
282 GetPortBounds( port , &bounds ) ;
283 CreateCGContextForPort( port , &cgContext ) ;
284
285 wxMacWindowToNative( thisWindow , updateRgn ) ;
286 OffsetRgn( updateRgn , controlBounds.left , controlBounds.top ) ;
287 ClipCGContextToRegion( cgContext , &bounds , updateRgn ) ;
288 wxMacNativeToWindow( thisWindow , updateRgn ) ;
289 OffsetRgn( updateRgn , -controlBounds.left , -controlBounds.top ) ;
290
291 CGContextTranslateCTM( cgContext , 0 , bounds.bottom - bounds.top ) ;
292 CGContextScaleCTM( cgContext , 1 , -1 ) ;
902725ee 293
e6616741 294 CGContextTranslateCTM( cgContext , controlBounds.left , controlBounds.top ) ;
902725ee 295
e15f0a5e 296#if 0
e6616741 297 CGContextSetRGBFillColor( cgContext , 1.0 , 1.0 , 1.0 , 1.0 ) ;
8523a5f5
DS
298 CGContextFillRect( cgContext ,
299 CGRectMake( 0 , 0 ,
300 controlBounds.right - controlBounds.left ,
301 controlBounds.bottom - controlBounds.top ) );
e15f0a5e 302#endif
e6616741 303 }
898d9035 304
c79aad8b 305 thisWindow->MacSetCGContextRef( cgContext ) ;
898d9035 306
e6616741
SC
307 {
308 wxMacCGContextStateSaver sg( cgContext ) ;
facd6764 309#endif
e6616741
SC
310 if ( thisWindow->MacDoRedraw( updateRgn , cEvent.GetTicks() ) )
311 result = noErr ;
898d9035 312
20b69855 313#if wxMAC_USE_CORE_GRAPHICS
e6616741
SC
314 thisWindow->MacSetCGContextRef( NULL ) ;
315 }
898d9035 316
e6616741 317 if ( created )
e6616741 318 CGContextRelease( cgContext ) ;
20b69855 319#endif
c79aad8b 320 }
e15f0a5e 321
055a486b
SC
322 if ( allocatedRgn )
323 DisposeRgn( allocatedRgn ) ;
facd6764
SC
324 }
325 break ;
e15f0a5e 326
73fe67bd 327 case kEventControlVisibilityChanged :
e15f0a5e 328 thisWindow->MacVisibilityChanged() ;
73fe67bd 329 break ;
e15f0a5e 330
73fe67bd 331 case kEventControlEnabledStateChanged :
e15f0a5e 332 thisWindow->MacEnabledStateChanged() ;
73fe67bd 333 break ;
e15f0a5e 334
73fe67bd 335 case kEventControlHiliteChanged :
e15f0a5e 336 thisWindow->MacHiliteChanged() ;
73fe67bd 337 break ;
6449b3a8 338#endif
e15f0a5e 339
6449b3a8 340 // we emulate this event under Carbon CFM
f1d527c1
SC
341 case kEventControlSetFocusPart :
342 {
343 Boolean focusEverything = false ;
344 ControlPartCode controlPart = cEvent.GetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode );
898d9035 345
6449b3a8 346#ifdef __WXMAC_OSX__
f1d527c1
SC
347 if ( cEvent.GetParameter<Boolean>(kEventParamControlFocusEverything , &focusEverything ) == noErr )
348 {
349 }
6449b3a8 350#endif
e15f0a5e 351
f1d527c1
SC
352 if ( controlPart == kControlFocusNoPart )
353 {
e15f0a5e 354#if wxUSE_CARET
f1d527c1 355 if ( thisWindow->GetCaret() )
f1d527c1 356 thisWindow->GetCaret()->OnKillFocus();
e15f0a5e
DS
357#endif
358
92b6cd62 359 static bool inKillFocusEvent = false ;
898d9035 360
92b6cd62
SC
361 if ( !inKillFocusEvent )
362 {
363 inKillFocusEvent = true ;
364 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
365 event.SetEventObject(thisWindow);
366 thisWindow->GetEventHandler()->ProcessEvent(event) ;
367 inKillFocusEvent = false ;
368 }
f1d527c1
SC
369 }
370 else
371 {
372 // panel wants to track the window which was the last to have focus in it
373 wxChildFocusEvent eventFocus(thisWindow);
374 thisWindow->GetEventHandler()->ProcessEvent(eventFocus);
8b573fb8 375
e15f0a5e 376#if wxUSE_CARET
f1d527c1 377 if ( thisWindow->GetCaret() )
f1d527c1 378 thisWindow->GetCaret()->OnSetFocus();
e15f0a5e 379#endif
f1d527c1
SC
380
381 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
382 event.SetEventObject(thisWindow);
383 thisWindow->GetEventHandler()->ProcessEvent(event) ;
384 }
e15f0a5e 385
64fec3ab
SC
386 if ( thisWindow->MacIsUserPane() )
387 result = noErr ;
f1d527c1
SC
388 }
389 break ;
e15f0a5e 390
f1d527c1 391 case kEventControlHit :
e15f0a5e 392 result = thisWindow->MacControlHit( handler , event ) ;
f1d527c1 393 break ;
e15f0a5e 394
facd6764
SC
395 default :
396 break ;
397 }
e15f0a5e 398
facd6764
SC
399 return result ;
400}
401
e4727773
SC
402static pascal OSStatus wxMacWindowServiceEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
403{
404 OSStatus result = eventNotHandledErr ;
405
406 wxMacCarbonEvent cEvent( event ) ;
8b573fb8 407
e4727773
SC
408 ControlRef controlRef ;
409 wxWindowMac* thisWindow = (wxWindowMac*) data ;
410 wxTextCtrl* textCtrl = wxDynamicCast( thisWindow , wxTextCtrl ) ;
411 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
412
e15f0a5e 413 switch ( GetEventKind( event ) )
e4727773
SC
414 {
415 case kEventServiceGetTypes :
e15f0a5e 416 if ( textCtrl )
e4727773
SC
417 {
418 long from, to ;
419 textCtrl->GetSelection( &from , &to ) ;
420
8b573fb8 421 CFMutableArrayRef copyTypes = 0 , pasteTypes = 0;
e15f0a5e 422 if ( from != to )
e4727773
SC
423 copyTypes = cEvent.GetParameter< CFMutableArrayRef >( kEventParamServiceCopyTypes , typeCFMutableArrayRef ) ;
424 if ( textCtrl->IsEditable() )
425 pasteTypes = cEvent.GetParameter< CFMutableArrayRef >( kEventParamServicePasteTypes , typeCFMutableArrayRef ) ;
8b573fb8 426
898d9035 427 static const OSType textDataTypes[] = { kTXNTextData /* , 'utxt', 'PICT', 'MooV', 'AIFF' */ };
e4727773
SC
428 for ( size_t i = 0 ; i < WXSIZEOF(textDataTypes) ; ++i )
429 {
430 CFStringRef typestring = CreateTypeStringWithOSType(textDataTypes[i]);
431 if ( typestring )
432 {
433 if ( copyTypes )
898d9035 434 CFArrayAppendValue(copyTypes, typestring) ;
e4727773 435 if ( pasteTypes )
898d9035 436 CFArrayAppendValue(pasteTypes, typestring) ;
e15f0a5e 437
e4727773
SC
438 CFRelease( typestring ) ;
439 }
440 }
e15f0a5e 441
e4727773
SC
442 result = noErr ;
443 }
444 break ;
e15f0a5e 445
e4727773
SC
446 case kEventServiceCopy :
447 if ( textCtrl )
448 {
449 long from, to ;
e15f0a5e 450
e4727773
SC
451 textCtrl->GetSelection( &from , &to ) ;
452 wxString val = textCtrl->GetValue() ;
453 val = val.Mid( from , to - from ) ;
454 ScrapRef scrapRef = cEvent.GetParameter< ScrapRef > ( kEventParamScrapRef , typeScrapRef ) ;
455 verify_noerr( ClearScrap( &scrapRef ) ) ;
fb5246be 456 verify_noerr( PutScrapFlavor( scrapRef , kTXNTextData , 0 , val.length() , val.c_str() ) ) ;
e4727773
SC
457 result = noErr ;
458 }
459 break ;
e15f0a5e 460
e4727773
SC
461 case kEventServicePaste :
462 if ( textCtrl )
463 {
464 ScrapRef scrapRef = cEvent.GetParameter< ScrapRef > ( kEventParamScrapRef , typeScrapRef ) ;
465 Size textSize, pastedSize ;
898d9035 466 verify_noerr( GetScrapFlavorSize(scrapRef, kTXNTextData, &textSize) ) ;
e4727773
SC
467 textSize++ ;
468 char *content = new char[textSize] ;
898d9035
DS
469 GetScrapFlavorData(scrapRef, kTXNTextData, &pastedSize, content );
470 content[textSize - 1] = 0 ;
e15f0a5e 471
8ef4d6e2 472#if wxUSE_UNICODE
58603178 473 textCtrl->WriteText( wxString( content , wxConvLocal ) );
8ef4d6e2 474#else
e4727773 475 textCtrl->WriteText( wxString( content ) ) ;
8ef4d6e2 476#endif
e15f0a5e 477
e4727773
SC
478 delete[] content ;
479 result = noErr ;
480 }
481 break ;
e15f0a5e
DS
482
483 default:
484 break ;
e4727773 485 }
8b573fb8 486
e4727773 487 return result ;
8b573fb8 488}
e4727773 489
980ee83f
SC
490pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
491{
492 OSStatus result = eventNotHandledErr ;
493 wxWindowMac* focus = (wxWindowMac*) data ;
494
495 wchar_t* uniChars = NULL ;
496 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
497
498 UniChar* charBuf;
499 UInt32 dataSize = 0 ;
500 int numChars = 0 ;
501 UniChar buf[2] ;
502 if ( GetEventParameter( event, kEventParamTextInputSendText, typeUnicodeText, NULL, 0 , &dataSize, NULL ) == noErr )
503 {
504 numChars = dataSize / sizeof( UniChar) ;
505 charBuf = buf ;
506
507 if ( dataSize > sizeof(buf) )
508 charBuf = new UniChar[ numChars ] ;
509 else
510 charBuf = buf ;
511
512 uniChars = new wchar_t[ numChars ] ;
513 GetEventParameter( event, kEventParamTextInputSendText, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ;
514#if SIZEOF_WCHAR_T == 2
515 uniChars = charBuf ;
516 memcpy( uniChars , charBuf , dataSize ) ;
517#else
518 // the resulting string will never have more chars than the utf16 version, so this is safe
519 wxMBConvUTF16 converter ;
520 numChars = converter.MB2WC( uniChars , (const char*)charBuf , numChars ) ;
521#endif
522 }
523
524 switch ( GetEventKind( event ) )
525 {
526 case kEventTextInputUpdateActiveInputArea :
527 {
528 // An IME input event may return several characters, but we need to send one char at a time to
529 // EVT_CHAR
530 for (int pos=0 ; pos < numChars ; pos++)
531 {
532
533 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
534 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
535 wxTheApp->MacSetCurrentEvent( event , handler ) ;
536
537 UInt32 message = (0 << 8) + ((char)uniChars[pos] );
538 if ( wxTheApp->MacSendCharEvent(
539 focus , message , 0 , when , 0 , 0 , uniChars[pos] ) )
540 {
541 result = noErr ;
542 }
543
544 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
545 }
546 }
547 break ;
548 case kEventTextInputUnicodeForKeyEvent :
549 {
550 UInt32 keyCode, modifiers ;
551 Point point ;
552 EventRef rawEvent ;
553 unsigned char charCode ;
554
555 GetEventParameter( event, kEventParamTextInputSendKeyboardEvent, typeEventRef, NULL, sizeof(rawEvent), NULL, &rawEvent ) ;
556 GetEventParameter( rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &charCode );
557 GetEventParameter( rawEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
558 GetEventParameter( rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers );
559 GetEventParameter( rawEvent, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &point );
560
561 UInt32 message = (keyCode << 8) + charCode;
aa44b55d 562
980ee83f
SC
563 // An IME input event may return several characters, but we need to send one char at a time to
564 // EVT_CHAR
565 for (int pos=0 ; pos < numChars ; pos++)
566 {
567
568 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
569 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
570 wxTheApp->MacSetCurrentEvent( event , handler ) ;
571
572 if ( wxTheApp->MacSendCharEvent(
573 focus , message , modifiers , when , point.h , point.v , uniChars[pos] ) )
574 {
575 result = noErr ;
576 }
577
578 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
579 }
580 }
581 break;
582 default:
583 break ;
584 }
585
586 delete[] uniChars ;
587 if ( charBuf != buf )
588 delete[] charBuf ;
589
590 return result ;
591
592}
593
facd6764
SC
594pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
595{
af6b7b80
SC
596 EventRef formerEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
597 EventHandlerCallRef formerEventHandlerCallRef = (EventHandlerCallRef) wxTheApp->MacGetCurrentEventHandlerCallRef() ;
598 wxTheApp->MacSetCurrentEvent( event , handler ) ;
facd6764
SC
599 OSStatus result = eventNotHandledErr ;
600
601 switch ( GetEventClass( event ) )
602 {
603 case kEventClassControl :
604 result = wxMacWindowControlEventHandler( handler, event, data ) ;
605 break ;
e15f0a5e 606
e4727773
SC
607 case kEventClassService :
608 result = wxMacWindowServiceEventHandler( handler, event , data ) ;
902725ee 609 break ;
e15f0a5e 610
980ee83f
SC
611 case kEventClassTextInput :
612 result = wxMacUnicodeTextEventHandler( handler , event , data ) ;
613 break ;
facd6764
SC
614 default :
615 break ;
616 }
e15f0a5e 617
af6b7b80 618 wxTheApp->MacSetCurrentEvent( formerEvent, formerEventHandlerCallRef ) ;
e15f0a5e 619
facd6764
SC
620 return result ;
621}
622
623DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler )
624
20b69855
SC
625#if !TARGET_API_MAC_OSX
626
facd6764
SC
627// ---------------------------------------------------------------------------
628// UserPane events for non OSX builds
629// ---------------------------------------------------------------------------
8b573fb8 630
facd6764
SC
631static pascal void wxMacControlUserPaneDrawProc(ControlRef control, SInt16 part)
632{
633 wxWindow * win = wxFindControlFromMacControl(control) ;
542f604f
SC
634 if ( win )
635 win->MacControlUserPaneDrawProc(part) ;
facd6764 636}
1f1c8bd4 637wxMAC_DEFINE_PROC_GETTER( ControlUserPaneDrawUPP , wxMacControlUserPaneDrawProc ) ;
facd6764
SC
638
639static pascal ControlPartCode wxMacControlUserPaneHitTestProc(ControlRef control, Point where)
640{
641 wxWindow * win = wxFindControlFromMacControl(control) ;
542f604f
SC
642 if ( win )
643 return win->MacControlUserPaneHitTestProc(where.h , where.v) ;
644 else
645 return kControlNoPart ;
facd6764 646}
1f1c8bd4 647wxMAC_DEFINE_PROC_GETTER( ControlUserPaneHitTestUPP , wxMacControlUserPaneHitTestProc ) ;
facd6764
SC
648
649static pascal ControlPartCode wxMacControlUserPaneTrackingProc(ControlRef control, Point startPt, ControlActionUPP actionProc)
650{
651 wxWindow * win = wxFindControlFromMacControl(control) ;
542f604f
SC
652 if ( win )
653 return win->MacControlUserPaneTrackingProc( startPt.h , startPt.v , (void*) actionProc) ;
654 else
655 return kControlNoPart ;
facd6764 656}
1f1c8bd4 657wxMAC_DEFINE_PROC_GETTER( ControlUserPaneTrackingUPP , wxMacControlUserPaneTrackingProc ) ;
facd6764
SC
658
659static pascal void wxMacControlUserPaneIdleProc(ControlRef control)
660{
661 wxWindow * win = wxFindControlFromMacControl(control) ;
542f604f
SC
662 if ( win )
663 win->MacControlUserPaneIdleProc() ;
facd6764 664}
1f1c8bd4 665wxMAC_DEFINE_PROC_GETTER( ControlUserPaneIdleUPP , wxMacControlUserPaneIdleProc ) ;
facd6764
SC
666
667static pascal ControlPartCode wxMacControlUserPaneKeyDownProc(ControlRef control, SInt16 keyCode, SInt16 charCode, SInt16 modifiers)
668{
669 wxWindow * win = wxFindControlFromMacControl(control) ;
542f604f
SC
670 if ( win )
671 return win->MacControlUserPaneKeyDownProc(keyCode,charCode,modifiers) ;
672 else
673 return kControlNoPart ;
facd6764 674}
1f1c8bd4 675wxMAC_DEFINE_PROC_GETTER( ControlUserPaneKeyDownUPP , wxMacControlUserPaneKeyDownProc ) ;
facd6764
SC
676
677static pascal void wxMacControlUserPaneActivateProc(ControlRef control, Boolean activating)
678{
679 wxWindow * win = wxFindControlFromMacControl(control) ;
542f604f
SC
680 if ( win )
681 win->MacControlUserPaneActivateProc(activating) ;
facd6764 682}
1f1c8bd4 683wxMAC_DEFINE_PROC_GETTER( ControlUserPaneActivateUPP , wxMacControlUserPaneActivateProc ) ;
facd6764
SC
684
685static pascal ControlPartCode wxMacControlUserPaneFocusProc(ControlRef control, ControlFocusPart action)
686{
687 wxWindow * win = wxFindControlFromMacControl(control) ;
542f604f
SC
688 if ( win )
689 return win->MacControlUserPaneFocusProc(action) ;
690 else
691 return kControlNoPart ;
facd6764 692}
1f1c8bd4 693wxMAC_DEFINE_PROC_GETTER( ControlUserPaneFocusUPP , wxMacControlUserPaneFocusProc ) ;
facd6764
SC
694
695static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control, ControlBackgroundPtr info)
696{
697 wxWindow * win = wxFindControlFromMacControl(control) ;
542f604f
SC
698 if ( win )
699 win->MacControlUserPaneBackgroundProc(info) ;
facd6764 700}
1f1c8bd4 701wxMAC_DEFINE_PROC_GETTER( ControlUserPaneBackgroundUPP , wxMacControlUserPaneBackgroundProc ) ;
facd6764 702
8b573fb8 703void wxWindowMac::MacControlUserPaneDrawProc(wxInt16 part)
facd6764 704{
e15f0a5e 705 int x = 0 , y = 0;
bcbd6987
SC
706 RgnHandle rgn = NewRgn() ;
707 GetClip( rgn ) ;
8523a5f5 708 MacWindowToRootWindow( &x, &y ) ;
d35bd499 709 OffsetRgn( rgn , -x , -y ) ;
bcbd6987
SC
710 wxMacWindowStateSaver sv( this ) ;
711 SectRgn( rgn , (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , rgn ) ;
712 MacDoRedraw( rgn , 0 ) ;
713 DisposeRgn( rgn ) ;
facd6764
SC
714}
715
8b573fb8 716wxInt16 wxWindowMac::MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y)
facd6764
SC
717{
718 return kControlNoPart ;
719}
720
8b573fb8 721wxInt16 wxWindowMac::MacControlUserPaneTrackingProc(wxInt16 x, wxInt16 y, void* actionProc)
facd6764
SC
722{
723 return kControlNoPart ;
724}
725
8b573fb8 726void wxWindowMac::MacControlUserPaneIdleProc()
facd6764
SC
727{
728}
729
8b573fb8 730wxInt16 wxWindowMac::MacControlUserPaneKeyDownProc(wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers)
facd6764
SC
731{
732 return kControlNoPart ;
733}
734
8b573fb8 735void wxWindowMac::MacControlUserPaneActivateProc(bool activating)
facd6764
SC
736{
737}
738
8b573fb8 739wxInt16 wxWindowMac::MacControlUserPaneFocusProc(wxInt16 action)
facd6764 740{
fb5246be
WS
741 if ( AcceptsFocus() )
742 return 1 ;
743 else
744 return kControlNoPart ;
facd6764
SC
745}
746
8b573fb8 747void wxWindowMac::MacControlUserPaneBackgroundProc(void* info)
facd6764
SC
748{
749}
750
20b69855
SC
751#endif
752
1f1c8bd4
SC
753// ---------------------------------------------------------------------------
754// Scrollbar Tracking for all
755// ---------------------------------------------------------------------------
756
757pascal void wxMacLiveScrollbarActionProc( ControlRef control , ControlPartCode partCode ) ;
758pascal void wxMacLiveScrollbarActionProc( ControlRef control , ControlPartCode partCode )
759{
760 if ( partCode != 0)
761 {
762 wxWindow* wx = wxFindControlFromMacControl( control ) ;
763 if ( wx )
1f1c8bd4 764 wx->MacHandleControlClick( (WXWidget) control , partCode , true /* stillDown */ ) ;
1f1c8bd4
SC
765 }
766}
767wxMAC_DEFINE_PROC_GETTER( ControlActionUPP , wxMacLiveScrollbarActionProc ) ;
768
e7549107
SC
769// ===========================================================================
770// implementation
771// ===========================================================================
772
71f2fb52
RN
773WX_DECLARE_HASH_MAP(ControlRef, wxWindow*, wxPointerHash, wxPointerEqual, MacControlMap);
774
775static MacControlMap wxWinMacControlList;
776
777wxWindow *wxFindControlFromMacControl(ControlRef inControl )
778{
779 MacControlMap::iterator node = wxWinMacControlList.find(inControl);
780
781 return (node == wxWinMacControlList.end()) ? NULL : node->second;
782}
783
784void wxAssociateControlWithMacControl(ControlRef inControl, wxWindow *control)
785{
786 // adding NULL ControlRef is (first) surely a result of an error and
787 // (secondly) breaks native event processing
788 wxCHECK_RET( inControl != (ControlRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
789
790 wxWinMacControlList[inControl] = control;
791}
792
793void wxRemoveMacControlAssociation(wxWindow *control)
794{
795 // iterate over all the elements in the class
a8683134
SC
796 // is the iterator stable ? as we might have two associations pointing to the same wxWindow
797 // we should go on...
798
799 bool found = true ;
e15f0a5e 800 while ( found )
71f2fb52 801 {
a8683134
SC
802 found = false ;
803 MacControlMap::iterator it;
804 for ( it = wxWinMacControlList.begin(); it != wxWinMacControlList.end(); ++it )
71f2fb52 805 {
a8683134
SC
806 if ( it->second == control )
807 {
808 wxWinMacControlList.erase(it);
809 found = true ;
810 break;
811 }
71f2fb52
RN
812 }
813 }
814}
facd6764 815
e7549107 816// ----------------------------------------------------------------------------
facd6764 817 // constructors and such
e7549107
SC
818// ----------------------------------------------------------------------------
819
94f9b1f0 820wxWindowMac::wxWindowMac()
8b573fb8
VZ
821{
822 Init();
94f9b1f0
SC
823}
824
825wxWindowMac::wxWindowMac(wxWindowMac *parent,
826 wxWindowID id,
827 const wxPoint& pos ,
828 const wxSize& size ,
829 long style ,
830 const wxString& name )
831{
832 Init();
833 Create(parent, id, pos, size, style, name);
834}
835
e766c8a9 836void wxWindowMac::Init()
519cb848 837{
21fd5529 838 m_peer = NULL ;
79392158 839 m_frozenness = 0 ;
898d9035 840
557d6f32 841#if WXWIN_COMPATIBILITY_2_4
902725ee 842 m_backgroundTransparent = false;
557d6f32 843#endif
e7549107 844
e15f0a5e
DS
845#if wxMAC_USE_CORE_GRAPHICS
846 m_cgContextRef = NULL ;
847#endif
848
e7549107 849 // as all windows are created with WS_VISIBLE style...
902725ee 850 m_isShown = true;
e7549107 851
6264b550
RR
852 m_hScrollBar = NULL ;
853 m_vScrollBar = NULL ;
facd6764
SC
854 m_macBackgroundBrush = wxNullBrush ;
855
902725ee 856 m_macIsUserPane = true;
8adc196b 857 m_clipChildren = false ;
5c840e5b 858 m_cachedClippedRectValid = false ;
e15f0a5e 859
c6f9fb05
SC
860 // we need a valid font for the encodings
861 wxWindowBase::SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
e9576ca5
SC
862}
863
864// Destructor
e766c8a9 865wxWindowMac::~wxWindowMac()
e9576ca5 866{
7de59551
RD
867 SendDestroyEvent();
868
902725ee 869 m_isBeingDeleted = true;
6ed71b4f 870
6449b3a8 871 MacInvalidateBorders() ;
7ebf5540 872
d4380aaf
SC
873#ifndef __WXUNIVERSAL__
874 // VS: make sure there's no wxFrame with last focus set to us:
875 for ( wxWindow *win = GetParent(); win; win = win->GetParent() )
876 {
877 wxFrame *frame = wxDynamicCast(win, wxFrame);
878 if ( frame )
879 {
880 if ( frame->GetLastFocus() == this )
d4380aaf 881 frame->SetLastFocus((wxWindow*)NULL);
d4380aaf
SC
882 break;
883 }
884 }
885#endif // __WXUNIVERSAL__
8b573fb8
VZ
886
887 // destroy children before destroying this window itself
888 DestroyChildren();
889
facd6764
SC
890 // wxRemoveMacControlAssociation( this ) ;
891 // If we delete an item, we should initialize the parent panel,
892 // because it could now be invalid.
893 wxWindow *parent = GetParent() ;
894 if ( parent )
895 {
896 if (parent->GetDefaultItem() == (wxButton*) this)
897 parent->SetDefaultItem(NULL);
898 }
e15f0a5e 899
21fd5529 900 if ( m_peer && m_peer->Ok() )
facd6764
SC
901 {
902 // in case the callback might be called during destruction
903 wxRemoveMacControlAssociation( this) ;
229f6270 904 ::RemoveEventHandler( (EventHandlerRef ) m_macControlEventHandler ) ;
5ca0d812
SC
905 // we currently are not using this hook
906 // ::SetControlColorProc( *m_peer , NULL ) ;
907 m_peer->Dispose() ;
facd6764 908 }
d4380aaf 909
facd6764 910 if ( g_MacLastWindow == this )
facd6764 911 g_MacLastWindow = NULL ;
7de59551 912
fd76aa8d
SC
913 wxFrame* frame = wxDynamicCast( wxGetTopLevelParent( this ) , wxFrame ) ;
914 if ( frame )
915 {
e40298d5
JS
916 if ( frame->GetLastFocus() == this )
917 frame->SetLastFocus( NULL ) ;
fd76aa8d 918 }
e7549107 919
42683dfb
SC
920 // delete our drop target if we've got one
921#if wxUSE_DRAG_AND_DROP
922 if ( m_dropTarget != NULL )
923 {
924 delete m_dropTarget;
925 m_dropTarget = NULL;
926 }
e15f0a5e
DS
927#endif
928
21fd5529
SC
929 delete m_peer ;
930}
931
8b573fb8
VZ
932WXWidget wxWindowMac::GetHandle() const
933{
934 return (WXWidget) m_peer->GetControlRef() ;
e9576ca5
SC
935}
936
5ca0d812 937void wxWindowMac::MacInstallEventHandler( WXWidget control )
facd6764 938{
898d9035
DS
939 wxAssociateControlWithMacControl( (ControlRef) control , this ) ;
940 InstallControlEventHandler( (ControlRef)control , GetwxMacWindowEventHandlerUPP(),
8b573fb8 941 GetEventTypeCount(eventList), eventList, this,
facd6764 942 (EventHandlerRef *)&m_macControlEventHandler);
e15f0a5e 943
4a63451b
SC
944#if !TARGET_API_MAC_OSX
945 if ( (ControlRef) control == m_peer->GetControlRef() )
946 {
898d9035
DS
947 m_peer->SetData<ControlUserPaneDrawUPP>(kControlEntireControl, kControlUserPaneDrawProcTag, GetwxMacControlUserPaneDrawProc()) ;
948 m_peer->SetData<ControlUserPaneHitTestUPP>(kControlEntireControl, kControlUserPaneHitTestProcTag, GetwxMacControlUserPaneHitTestProc()) ;
949 m_peer->SetData<ControlUserPaneTrackingUPP>(kControlEntireControl, kControlUserPaneTrackingProcTag, GetwxMacControlUserPaneTrackingProc()) ;
950 m_peer->SetData<ControlUserPaneIdleUPP>(kControlEntireControl, kControlUserPaneIdleProcTag, GetwxMacControlUserPaneIdleProc()) ;
951 m_peer->SetData<ControlUserPaneKeyDownUPP>(kControlEntireControl, kControlUserPaneKeyDownProcTag, GetwxMacControlUserPaneKeyDownProc()) ;
952 m_peer->SetData<ControlUserPaneActivateUPP>(kControlEntireControl, kControlUserPaneActivateProcTag, GetwxMacControlUserPaneActivateProc()) ;
953 m_peer->SetData<ControlUserPaneFocusUPP>(kControlEntireControl, kControlUserPaneFocusProcTag, GetwxMacControlUserPaneFocusProc()) ;
954 m_peer->SetData<ControlUserPaneBackgroundUPP>(kControlEntireControl, kControlUserPaneBackgroundProcTag, GetwxMacControlUserPaneBackgroundProc()) ;
4a63451b
SC
955 }
956#endif
facd6764
SC
957}
958
e9576ca5 959// Constructor
58603178
DS
960bool wxWindowMac::Create(wxWindowMac *parent,
961 wxWindowID id,
962 const wxPoint& pos,
963 const wxSize& size,
964 long style,
965 const wxString& name)
e9576ca5 966{
902725ee 967 wxCHECK_MSG( parent, false, wxT("can't create wxWindowMac without parent") );
e9576ca5 968
e7549107 969 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
902725ee 970 return false;
e9576ca5 971
facd6764 972 m_windowVariant = parent->GetWindowVariant() ;
8b573fb8 973
facd6764
SC
974 if ( m_macIsUserPane )
975 {
976 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
8b573fb8 977
ebe86b1e 978 UInt32 features = 0
8b573fb8 979 | kControlSupportsEmbedding
1f1c8bd4
SC
980 | kControlSupportsLiveFeedback
981 | kControlGetsFocusOnClick
8b573fb8
VZ
982// | kControlHasSpecialBackground
983// | kControlSupportsCalcBestRect
1f1c8bd4 984 | kControlHandlesTracking
8b573fb8 985 | kControlSupportsFocus
1f1c8bd4 986 | kControlWantsActivate
58603178 987 | kControlWantsIdle ;
ebe86b1e 988
6449b3a8 989 m_peer = new wxMacControl(this) ;
58603178
DS
990 OSStatus err =::CreateUserPaneControl( MAC_WXHWND(GetParent()->MacGetTopLevelWindowRef()) , &bounds, features , m_peer->GetControlRefAddr() );
991 verify_noerr( err );
8b573fb8 992
898d9035 993 MacPostControlCreate(pos, size) ;
facd6764 994 }
898d9035 995
e766c8a9 996#ifndef __WXUNIVERSAL__
14c9cbdb 997 // Don't give scrollbars to wxControls unless they ask for them
58603178
DS
998 if ( (! IsKindOf(CLASSINFO(wxControl)) && ! IsKindOf(CLASSINFO(wxStatusBar)))
999 || (IsKindOf(CLASSINFO(wxControl)) && ((style & wxHSCROLL) || (style & wxVSCROLL))))
6264b550
RR
1000 {
1001 MacCreateScrollBars( style ) ;
1002 }
e766c8a9 1003#endif
3dfafdb9
RD
1004
1005 wxWindowCreateEvent event(this);
7e4a196e 1006 GetEventHandler()->AddPendingEvent(event);
3dfafdb9 1007
902725ee 1008 return true;
e9576ca5
SC
1009}
1010
902725ee 1011void wxWindowMac::MacChildAdded()
08422003
SC
1012{
1013 if ( m_vScrollBar )
08422003 1014 m_vScrollBar->Raise() ;
08422003 1015 if ( m_hScrollBar )
08422003 1016 m_hScrollBar->Raise() ;
08422003
SC
1017}
1018
facd6764
SC
1019void wxWindowMac::MacPostControlCreate(const wxPoint& pos, const wxSize& size)
1020{
21fd5529 1021 wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid mac control") ) ;
facd6764 1022
58603178
DS
1023 m_peer->SetReference( (long)this ) ;
1024 GetParent()->AddChild( this );
facd6764 1025
5ca0d812 1026 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() );
facd6764
SC
1027
1028 ControlRef container = (ControlRef) GetParent()->GetHandle() ;
1029 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
5ca0d812 1030 ::EmbedControl( m_peer->GetControlRef() , container ) ;
08422003 1031 GetParent()->MacChildAdded() ;
facd6764
SC
1032
1033 // adjust font, controlsize etc
1034 DoSetWindowVariant( m_windowVariant ) ;
1035
fb5246be 1036 m_peer->SetLabel( wxStripMenuCodes(m_label) ) ;
facd6764 1037
facd6764 1038 if (!m_macIsUserPane)
d3b5db4b 1039 SetInitialBestSize(size);
facd6764
SC
1040
1041 SetCursor( *wxSTANDARD_CURSOR ) ;
facd6764
SC
1042}
1043
1044void wxWindowMac::DoSetWindowVariant( wxWindowVariant variant )
1045{
23176131
JS
1046 // Don't assert, in case we set the window variant before
1047 // the window is created
21fd5529 1048 // wxASSERT( m_peer->Ok() ) ;
facd6764 1049
23176131
JS
1050 m_windowVariant = variant ;
1051
21fd5529 1052 if (m_peer == NULL || !m_peer->Ok())
23176131 1053 return;
facd6764 1054
8b573fb8 1055 ControlSize size ;
facd6764
SC
1056 ThemeFontID themeFont = kThemeSystemFont ;
1057
1058 // we will get that from the settings later
8b573fb8 1059 // and make this NORMAL later, but first
facd6764 1060 // we have a few calculations that we must fix
8b573fb8 1061
facd6764
SC
1062 switch ( variant )
1063 {
1064 case wxWINDOW_VARIANT_NORMAL :
8b573fb8
VZ
1065 size = kControlSizeNormal;
1066 themeFont = kThemeSystemFont ;
facd6764 1067 break ;
e15f0a5e 1068
facd6764 1069 case wxWINDOW_VARIANT_SMALL :
8b573fb8
VZ
1070 size = kControlSizeSmall;
1071 themeFont = kThemeSmallSystemFont ;
facd6764 1072 break ;
e15f0a5e 1073
facd6764
SC
1074 case wxWINDOW_VARIANT_MINI :
1075 if (UMAGetSystemVersion() >= 0x1030 )
1076 {
8b573fb8
VZ
1077 // not always defined in the headers
1078 size = 3 ;
1079 themeFont = 109 ;
facd6764
SC
1080 }
1081 else
1082 {
8b573fb8
VZ
1083 size = kControlSizeSmall;
1084 themeFont = kThemeSmallSystemFont ;
facd6764
SC
1085 }
1086 break ;
e15f0a5e 1087
facd6764 1088 case wxWINDOW_VARIANT_LARGE :
8b573fb8
VZ
1089 size = kControlSizeLarge;
1090 themeFont = kThemeSystemFont ;
facd6764 1091 break ;
e15f0a5e 1092
facd6764
SC
1093 default:
1094 wxFAIL_MSG(_T("unexpected window variant"));
1095 break ;
1096 }
e15f0a5e 1097
898d9035 1098 m_peer->SetData<ControlSize>(kControlEntireControl, kControlSizeTag, &size ) ;
facd6764
SC
1099
1100 wxFont font ;
1101 font.MacCreateThemeFont( themeFont ) ;
1102 SetFont( font ) ;
1103}
1104
8b573fb8 1105void wxWindowMac::MacUpdateControlFont()
facd6764 1106{
ac99838a 1107 m_peer->SetFont( GetFont() , GetForegroundColour() , GetWindowStyle() ) ;
8b573fb8 1108 Refresh() ;
facd6764
SC
1109}
1110
1111bool wxWindowMac::SetFont(const wxFont& font)
1112{
58603178 1113 bool retval = wxWindowBase::SetFont( font );
8b573fb8 1114
facd6764 1115 MacUpdateControlFont() ;
8b573fb8 1116
facd6764
SC
1117 return retval;
1118}
1119
1120bool wxWindowMac::SetForegroundColour(const wxColour& col )
1121{
58603178 1122 bool retval = wxWindowBase::SetForegroundColour( col );
8b573fb8 1123
898d9035 1124 if (retval)
58603178 1125 MacUpdateControlFont();
8b573fb8 1126
898d9035 1127 return retval;
facd6764
SC
1128}
1129
1130bool wxWindowMac::SetBackgroundColour(const wxColour& col )
1131{
1132 if ( !wxWindowBase::SetBackgroundColour(col) && m_hasBgCol )
1133 return false ;
1134
1135 wxBrush brush ;
b52acd03 1136 wxColour newCol(GetBackgroundColour());
58603178
DS
1137
1138 if ( newCol == wxSystemSettings::GetColour( wxSYS_COLOUR_APPWORKSPACE ) )
facd6764 1139 brush.MacSetTheme( kThemeBrushDocumentWindowBackground ) ;
58603178 1140 else if ( newCol == wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ) )
8b573fb8 1141 brush.MacSetTheme( kThemeBrushDialogBackgroundActive ) ;
facd6764 1142 else
b52acd03 1143 brush.SetColour( newCol ) ;
e15f0a5e 1144
facd6764 1145 MacSetBackgroundBrush( brush ) ;
db7a550b 1146 MacUpdateControlFont() ;
8b573fb8 1147
facd6764
SC
1148 return true ;
1149}
1150
7ea087b7
SC
1151void wxWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
1152{
1153 m_macBackgroundBrush = brush ;
1154 m_peer->SetBackground( brush ) ;
1155}
facd6764
SC
1156
1157bool wxWindowMac::MacCanFocus() const
1158{
f6e3849c
SC
1159 // there is currently no way to determine whether the window is running in full keyboard
1160 // access mode, therefore we cannot rely on these features, yet the only other way would be
1161 // to issue a SetKeyboardFocus event and verify after whether it succeeded, this would risk problems
1162 // in event handlers...
f1d527c1 1163 UInt32 features = 0 ;
898d9035 1164 m_peer->GetFeatures( &features ) ;
e15f0a5e 1165
8b573fb8 1166 return features & ( kControlSupportsFocus | kControlGetsFocusOnClick ) ;
facd6764
SC
1167}
1168
e766c8a9 1169void wxWindowMac::SetFocus()
e9576ca5 1170{
e15f0a5e 1171 if ( !AcceptsFocus() )
7d0cfe71
SC
1172 return ;
1173
e15f0a5e
DS
1174 wxWindow* former = FindFocus() ;
1175 if ( former == this )
1176 return ;
1177
1178 // as we cannot rely on the control features to find out whether we are in full keyboard mode,
1179 // we can only leave in case of an error
1180 OSStatus err = m_peer->SetFocus( kControlFocusNextPart ) ;
1181 if ( err == errCouldntSetFocus )
1182 return ;
1183
e15f0a5e 1184 SetUserFocusWindow( (WindowRef)MacGetTopLevelWindowRef() );
f1d527c1
SC
1185
1186#if !TARGET_API_MAC_OSX
e15f0a5e
DS
1187 // emulate carbon events when running under CarbonLib where they are not natively available
1188 if ( former )
1189 {
1190 EventRef evRef = NULL ;
58603178
DS
1191
1192 err = MacCreateEvent(
1193 NULL , kEventClassControl , kEventControlSetFocusPart , TicksToEventTime( TickCount() ) ,
1194 kEventAttributeUserEvent , &evRef );
1195 verify_noerr( err );
f1d527c1 1196
e15f0a5e
DS
1197 wxMacCarbonEvent cEvent( evRef ) ;
1198 cEvent.SetParameter<ControlRef>( kEventParamDirectObject , (ControlRef) former->GetHandle() ) ;
1199 cEvent.SetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode , kControlFocusNoPart ) ;
8b573fb8 1200
e15f0a5e 1201 wxMacWindowEventHandler( NULL , evRef , former ) ;
58603178 1202 ReleaseEvent( evRef ) ;
e15f0a5e 1203 }
f1d527c1 1204
e15f0a5e
DS
1205 // send new focus event
1206 {
1207 EventRef evRef = NULL ;
58603178
DS
1208
1209 err = MacCreateEvent(
1210 NULL , kEventClassControl , kEventControlSetFocusPart , TicksToEventTime( TickCount() ) ,
1211 kEventAttributeUserEvent , &evRef );
1212 verify_noerr( err );
8b573fb8 1213
e15f0a5e
DS
1214 wxMacCarbonEvent cEvent( evRef ) ;
1215 cEvent.SetParameter<ControlRef>( kEventParamDirectObject , (ControlRef) GetHandle() ) ;
1216 cEvent.SetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode , kControlFocusNextPart ) ;
1217
1218 wxMacWindowEventHandler( NULL , evRef , this ) ;
58603178 1219 ReleaseEvent( evRef ) ;
6264b550 1220 }
e15f0a5e 1221#endif
e9576ca5
SC
1222}
1223
4116c221 1224void wxWindowMac::DoCaptureMouse()
e9576ca5 1225{
2a1f999f 1226 wxApp::s_captureWindow = this ;
e9576ca5
SC
1227}
1228
90b959ae
SC
1229wxWindow* wxWindowBase::GetCapture()
1230{
2a1f999f 1231 return wxApp::s_captureWindow ;
90b959ae
SC
1232}
1233
4116c221 1234void wxWindowMac::DoReleaseMouse()
e9576ca5 1235{
2a1f999f 1236 wxApp::s_captureWindow = NULL ;
e9576ca5
SC
1237}
1238
e15f0a5e 1239#if wxUSE_DRAG_AND_DROP
e9576ca5 1240
e766c8a9 1241void wxWindowMac::SetDropTarget(wxDropTarget *pDropTarget)
e9576ca5 1242{
e15f0a5e 1243 if ( m_dropTarget != NULL )
e40298d5 1244 delete m_dropTarget;
6ed71b4f 1245
e40298d5 1246 m_dropTarget = pDropTarget;
e15f0a5e 1247 if ( m_dropTarget != NULL )
e40298d5 1248 {
58603178 1249 // TODO:
e40298d5 1250 }
e9576ca5
SC
1251}
1252
1253#endif
1254
1255// Old style file-manager drag&drop
e766c8a9 1256void wxWindowMac::DragAcceptFiles(bool accept)
e9576ca5 1257{
58603178 1258 // TODO:
e9576ca5
SC
1259}
1260
055a486b
SC
1261// Returns the size of the native control. In the case of the toplevel window
1262// this is the content area root control
1263
facd6764 1264void wxWindowMac::MacGetPositionAndSizeFromControl(int& x, int& y,
8b573fb8 1265 int& w, int& h) const
e9576ca5 1266{
898d9035 1267 wxFAIL_MSG( wxT("Not currently supported") ) ;
e9576ca5
SC
1268}
1269
055a486b 1270// From a wx position / size calculate the appropriate size of the native control
8b573fb8 1271
898d9035
DS
1272bool wxWindowMac::MacGetBoundsForControl(
1273 const wxPoint& pos,
1274 const wxSize& size,
1275 int& x, int& y,
1276 int& w, int& h , bool adjustOrigin ) const
51abe921 1277{
789ae0cf 1278 bool isCompositing = MacGetTopLevelWindow()->MacUsesCompositing() ;
902725ee 1279
5ca0d812 1280 // the desired size, minus the border pixels gives the correct size of the control
8b573fb8 1281
facd6764
SC
1282 x = (int)pos.x;
1283 y = (int)pos.y;
58603178
DS
1284
1285 // TODO: the default calls may be used as soon as PostCreateControl Is moved here
898d9035
DS
1286 w = wxMax(size.x, 0) ; // WidthDefault( size.x );
1287 h = wxMax(size.y, 0) ; // HeightDefault( size.y ) ;
789ae0cf
SC
1288
1289 if ( !isCompositing )
1290 GetParent()->MacWindowToRootWindow( &x , &y ) ;
5ca0d812
SC
1291
1292 x += MacGetLeftBorderSize() ;
1293 y += MacGetTopBorderSize() ;
1294 w -= MacGetLeftBorderSize() + MacGetRightBorderSize() ;
1295 h -= MacGetTopBorderSize() + MacGetBottomBorderSize() ;
8b573fb8 1296
79392158
SC
1297 if ( adjustOrigin )
1298 AdjustForParentClientOrigin( x , y ) ;
789ae0cf 1299
7ebf5540 1300 // this is in window relative coordinate, as this parent may have a border, its physical position is offset by this border
789ae0cf 1301 if ( !GetParent()->IsTopLevel() )
7ebf5540
SC
1302 {
1303 x -= GetParent()->MacGetLeftBorderSize() ;
1304 y -= GetParent()->MacGetTopBorderSize() ;
1305 }
789ae0cf 1306
facd6764
SC
1307 return true ;
1308}
1309
42ef83fa 1310// Get window size (not client size)
facd6764
SC
1311void wxWindowMac::DoGetSize(int *x, int *y) const
1312{
1f1c8bd4
SC
1313 Rect bounds ;
1314 m_peer->GetRect( &bounds ) ;
8b573fb8 1315
e15f0a5e
DS
1316 if (x)
1317 *x = bounds.right - bounds.left + MacGetLeftBorderSize() + MacGetRightBorderSize() ;
1318 if (y)
1319 *y = bounds.bottom - bounds.top + MacGetTopBorderSize() + MacGetBottomBorderSize() ;
facd6764
SC
1320}
1321
42ef83fa 1322// get the position of the bounds of this window in client coordinates of its parent
facd6764
SC
1323void wxWindowMac::DoGetPosition(int *x, int *y) const
1324{
1f1c8bd4
SC
1325 Rect bounds ;
1326 m_peer->GetRect( &bounds ) ;
902725ee 1327
1f1c8bd4
SC
1328 int x1 = bounds.left ;
1329 int y1 = bounds.top ;
789ae0cf 1330
bc2b0c1b
SC
1331 // get the wx window position from the native one
1332 x1 -= MacGetLeftBorderSize() ;
1333 y1 -= MacGetTopBorderSize() ;
1334
facd6764 1335 if ( !IsTopLevel() )
2b5f62a0 1336 {
facd6764
SC
1337 wxWindow *parent = GetParent();
1338 if ( parent )
8950f7cc 1339 {
e15f0a5e
DS
1340 // we must first adjust it to be in window coordinates of the parent,
1341 // as otherwise it gets lost by the ClientAreaOrigin fix
1a02aff9
SC
1342 x1 += parent->MacGetLeftBorderSize() ;
1343 y1 += parent->MacGetTopBorderSize() ;
e15f0a5e 1344
1a02aff9 1345 // and now to client coordinates
facd6764
SC
1346 wxPoint pt(parent->GetClientAreaOrigin());
1347 x1 -= pt.x ;
1348 y1 -= pt.y ;
6ed71b4f 1349 }
2b5f62a0 1350 }
e15f0a5e
DS
1351
1352 if (x)
1353 *x = x1 ;
1354 if (y)
1355 *y = y1 ;
facd6764 1356}
51abe921 1357
e766c8a9 1358void wxWindowMac::DoScreenToClient(int *x, int *y) const
e9576ca5 1359{
facd6764 1360 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
58603178 1361 wxCHECK_RET( window , wxT("TopLevel Window missing") ) ;
8b573fb8 1362
58603178 1363 Point localwhere = { 0, 0 } ;
519cb848 1364
58603178
DS
1365 if (x)
1366 localwhere.h = *x ;
1367 if (y)
1368 localwhere.v = *y ;
8b573fb8 1369
58603178 1370 QDGlobalToLocalPoint( GetWindowPort( window ) , &localwhere ) ;
6ed71b4f 1371
58603178
DS
1372 if (x)
1373 *x = localwhere.h ;
1374 if (y)
1375 *y = localwhere.v ;
e15f0a5e 1376
2078220e 1377 MacRootWindowToWindow( x , y ) ;
8b573fb8 1378
facd6764 1379 wxPoint origin = GetClientAreaOrigin() ;
e15f0a5e
DS
1380 if (x)
1381 *x -= origin.x ;
1382 if (y)
1383 *y -= origin.y ;
e9576ca5
SC
1384}
1385
e766c8a9 1386void wxWindowMac::DoClientToScreen(int *x, int *y) const
e9576ca5 1387{
facd6764 1388 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
8523a5f5 1389 wxCHECK_RET( window , wxT("TopLevel window missing") ) ;
14c9cbdb 1390
facd6764 1391 wxPoint origin = GetClientAreaOrigin() ;
e15f0a5e
DS
1392 if (x)
1393 *x += origin.x ;
1394 if (y)
1395 *y += origin.y ;
14c9cbdb 1396
2078220e 1397 MacWindowToRootWindow( x , y ) ;
14c9cbdb 1398
58603178
DS
1399 Point localwhere = { 0, 0 };
1400 if (x)
1401 localwhere.h = *x ;
1402 if (y)
1403 localwhere.v = *y ;
e15f0a5e 1404
58603178 1405 QDLocalToGlobalPoint( GetWindowPort( window ) , &localwhere ) ;
e15f0a5e 1406
58603178
DS
1407 if (x)
1408 *x = localwhere.h ;
1409 if (y)
1410 *y = localwhere.v ;
519cb848
SC
1411}
1412
e766c8a9 1413void wxWindowMac::MacClientToRootWindow( int *x , int *y ) const
519cb848 1414{
1c310985 1415 wxPoint origin = GetClientAreaOrigin() ;
e15f0a5e
DS
1416 if (x)
1417 *x += origin.x ;
1418 if (y)
1419 *y += origin.y ;
14c9cbdb 1420
1c310985
SC
1421 MacWindowToRootWindow( x , y ) ;
1422}
1423
1424void wxWindowMac::MacRootWindowToClient( int *x , int *y ) const
1425{
1c310985 1426 MacRootWindowToWindow( x , y ) ;
facd6764
SC
1427
1428 wxPoint origin = GetClientAreaOrigin() ;
e15f0a5e
DS
1429 if (x)
1430 *x -= origin.x ;
1431 if (y)
1432 *y -= origin.y ;
1c310985
SC
1433}
1434
1435void wxWindowMac::MacWindowToRootWindow( int *x , int *y ) const
1436{
5ca0d812 1437 wxPoint pt ;
58603178 1438
e15f0a5e
DS
1439 if (x)
1440 pt.x = *x ;
1441 if (y)
1442 pt.y = *y ;
facd6764 1443
125c7984 1444 if ( !IsTopLevel() )
5437ff47
RD
1445 {
1446 wxTopLevelWindowMac* top = MacGetTopLevelWindow();
1447 if (top)
5ca0d812
SC
1448 {
1449 pt.x -= MacGetLeftBorderSize() ;
1450 pt.y -= MacGetTopBorderSize() ;
1451 wxMacControl::Convert( &pt , m_peer , top->m_peer ) ;
1452 }
5437ff47 1453 }
8b573fb8 1454
e15f0a5e
DS
1455 if (x)
1456 *x = (int) pt.x ;
1457 if (y)
1458 *y = (int) pt.y ;
facd6764
SC
1459}
1460
1461void wxWindowMac::MacWindowToRootWindow( short *x , short *y ) const
1462{
1463 int x1 , y1 ;
e15f0a5e
DS
1464
1465 if (x)
1466 x1 = *x ;
1467 if (y)
1468 y1 = *y ;
1469
facd6764 1470 MacWindowToRootWindow( &x1 , &y1 ) ;
e15f0a5e
DS
1471
1472 if (x)
1473 *x = x1 ;
1474 if (y)
1475 *y = y1 ;
519cb848
SC
1476}
1477
1c310985 1478void wxWindowMac::MacRootWindowToWindow( int *x , int *y ) const
519cb848 1479{
5ca0d812 1480 wxPoint pt ;
e15f0a5e
DS
1481
1482 if (x)
1483 pt.x = *x ;
1484 if (y)
1485 pt.y = *y ;
facd6764 1486
125c7984 1487 if ( !IsTopLevel() )
5ca0d812 1488 {
5c840e5b
SC
1489 wxTopLevelWindowMac* top = MacGetTopLevelWindow();
1490 if (top)
1491 {
1492 wxMacControl::Convert( &pt , top->m_peer , m_peer ) ;
1493 pt.x += MacGetLeftBorderSize() ;
1494 pt.y += MacGetTopBorderSize() ;
1495 }
5ca0d812 1496 }
8b573fb8 1497
e15f0a5e
DS
1498 if (x)
1499 *x = (int) pt.x ;
1500 if (y)
1501 *y = (int) pt.y ;
e9576ca5
SC
1502}
1503
facd6764 1504void wxWindowMac::MacRootWindowToWindow( short *x , short *y ) const
e9576ca5 1505{
facd6764 1506 int x1 , y1 ;
e15f0a5e
DS
1507
1508 if (x)
1509 x1 = *x ;
1510 if (y)
1511 y1 = *y ;
1512
facd6764 1513 MacRootWindowToWindow( &x1 , &y1 ) ;
e15f0a5e
DS
1514
1515 if (x)
1516 *x = x1 ;
1517 if (y)
1518 *y = y1 ;
facd6764 1519}
6ed71b4f 1520
29281095
SC
1521void wxWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1522{
1523 RgnHandle rgn = NewRgn() ;
898d9035 1524
5ca0d812 1525 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
29281095 1526 {
898d9035
DS
1527 Rect structure, content ;
1528
29281095 1529 GetRegionBounds( rgn , &content ) ;
1f1c8bd4
SC
1530 m_peer->GetRect( &structure ) ;
1531 OffsetRect( &structure, -structure.left , -structure.top ) ;
902725ee 1532
1f1c8bd4 1533 left = content.left - structure.left ;
898d9035 1534 top = content.top - structure.top ;
1f1c8bd4
SC
1535 right = structure.right - content.right ;
1536 bottom = structure.bottom - content.bottom ;
29281095
SC
1537 }
1538 else
1539 {
1f1c8bd4 1540 left = top = right = bottom = 0 ;
29281095 1541 }
898d9035 1542
7596e51d 1543 DisposeRgn( rgn ) ;
29281095
SC
1544}
1545
facd6764
SC
1546wxSize wxWindowMac::DoGetSizeFromClientSize( const wxSize & size ) const
1547{
1548 wxSize sizeTotal = size;
1549
1550 RgnHandle rgn = NewRgn() ;
5ca0d812 1551 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
6618870d 1552 {
898d9035 1553 Rect content, structure ;
facd6764 1554 GetRegionBounds( rgn , &content ) ;
1f1c8bd4 1555 m_peer->GetRect( &structure ) ;
898d9035 1556
1f1c8bd4 1557 // structure is in parent coordinates, but we only need width and height, so it's ok
6ed71b4f 1558
1f1c8bd4 1559 sizeTotal.x += (structure.right - structure.left) - (content.right - content.left) ;
8523a5f5 1560 sizeTotal.y += (structure.bottom - structure.top) - (content.bottom - content.top) ;
1f1c8bd4 1561 }
898d9035 1562
1f1c8bd4 1563 DisposeRgn( rgn ) ;
6ed71b4f 1564
e15f0a5e
DS
1565 sizeTotal.x += MacGetLeftBorderSize() + MacGetRightBorderSize() ;
1566 sizeTotal.y += MacGetTopBorderSize() + MacGetBottomBorderSize() ;
6ed71b4f 1567
facd6764 1568 return sizeTotal;
e9576ca5
SC
1569}
1570
e9576ca5 1571// Get size *available for subwindows* i.e. excluding menu bar etc.
e15f0a5e 1572void wxWindowMac::DoGetClientSize( int *x, int *y ) const
e9576ca5 1573{
9453cf2b 1574 int ww, hh;
6ed71b4f 1575
facd6764
SC
1576 RgnHandle rgn = NewRgn() ;
1577 Rect content ;
5ca0d812 1578 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
facd6764 1579 GetRegionBounds( rgn , &content ) ;
facd6764 1580 else
5ca0d812 1581 m_peer->GetRect( &content ) ;
7596e51d 1582 DisposeRgn( rgn ) ;
789ae0cf 1583
facd6764
SC
1584 ww = content.right - content.left ;
1585 hh = content.bottom - content.top ;
6ed71b4f 1586
db7a550b 1587 if (m_hScrollBar && m_hScrollBar->IsShown() )
e905b636 1588 hh -= m_hScrollBar->GetSize().y ;
e15f0a5e 1589
db7a550b 1590 if (m_vScrollBar && m_vScrollBar->IsShown() )
e905b636 1591 ww -= m_vScrollBar->GetSize().x ;
e15f0a5e
DS
1592
1593 if (x)
1594 *x = ww;
1595 if (y)
1596 *y = hh;
facd6764
SC
1597}
1598
1599bool wxWindowMac::SetCursor(const wxCursor& cursor)
1600{
1601 if (m_cursor == cursor)
902725ee 1602 return false;
facd6764
SC
1603
1604 if (wxNullCursor == cursor)
1605 {
1606 if ( ! wxWindowBase::SetCursor( *wxSTANDARD_CURSOR ) )
902725ee 1607 return false ;
facd6764
SC
1608 }
1609 else
1610 {
1611 if ( ! wxWindowBase::SetCursor( cursor ) )
902725ee 1612 return false ;
facd6764
SC
1613 }
1614
1615 wxASSERT_MSG( m_cursor.Ok(),
1616 wxT("cursor must be valid after call to the base version"));
8b573fb8 1617
2d1760d3 1618 wxWindowMac *mouseWin = 0 ;
facd6764 1619 {
789ae0cf
SC
1620 wxTopLevelWindowMac *tlw = MacGetTopLevelWindow() ;
1621 WindowRef window = (WindowRef) ( tlw ? tlw->MacGetWindowRef() : 0 ) ;
2d1760d3
SC
1622 CGrafPtr savePort ;
1623 Boolean swapped = QDSwapPort( GetWindowPort( window ) , &savePort ) ;
8b573fb8 1624
58603178
DS
1625 // TODO: If we ever get a GetCurrentEvent... replacement
1626 // for the mouse position, use it...
8b573fb8 1627
2d1760d3 1628 Point pt ;
2d1760d3
SC
1629 ControlPartCode part ;
1630 ControlRef control ;
58603178
DS
1631
1632 GetMouse( &pt ) ;
789ae0cf 1633 control = wxMacFindControlUnderMouse( tlw , pt , window , &part ) ;
2d1760d3
SC
1634 if ( control )
1635 mouseWin = wxFindControlFromMacControl( control ) ;
8b573fb8 1636
2d1760d3
SC
1637 if ( swapped )
1638 QDSwapPort( savePort , NULL ) ;
facd6764 1639 }
2d1760d3
SC
1640
1641 if ( mouseWin == this && !wxIsBusy() )
facd6764 1642 m_cursor.MacInstall() ;
facd6764 1643
902725ee 1644 return true ;
519cb848
SC
1645}
1646
facd6764
SC
1647#if wxUSE_MENUS
1648bool wxWindowMac::DoPopupMenu(wxMenu *menu, int x, int y)
1649{
1650 menu->SetInvokingWindow(this);
1651 menu->UpdateUI();
8b573fb8 1652
971562cb
VS
1653 if ( x == -1 && y == -1 )
1654 {
1655 wxPoint mouse = wxGetMousePosition();
898d9035
DS
1656 x = mouse.x;
1657 y = mouse.y;
971562cb
VS
1658 }
1659 else
1660 {
1661 ClientToScreen( &x , &y ) ;
1662 }
facd6764
SC
1663
1664 menu->MacBeforeDisplay( true ) ;
898d9035 1665 long menuResult = ::PopUpMenuSelect((MenuHandle) menu->GetHMenu() , y, x, 0) ;
facd6764
SC
1666 if ( HiWord(menuResult) != 0 )
1667 {
e8027adb
SC
1668 MenuCommand macid;
1669 GetMenuItemCommandID( GetMenuHandle(HiWord(menuResult)) , LoWord(menuResult) , &macid );
1670 int id = wxMacCommandToId( macid );
1671 wxMenuItem* item = NULL ;
facd6764 1672 wxMenu* realmenu ;
58603178 1673 item = menu->FindItem( id, &realmenu ) ;
e8027adb
SC
1674 if ( item )
1675 {
1676 if (item->IsCheckable())
1677 item->Check( !item->IsChecked() ) ;
e15f0a5e 1678
e8027adb
SC
1679 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
1680 }
facd6764 1681 }
facd6764 1682
898d9035 1683 menu->MacAfterDisplay( true ) ;
8523a5f5 1684 menu->SetInvokingWindow( NULL );
facd6764 1685
902725ee 1686 return true;
facd6764
SC
1687}
1688#endif
51abe921
SC
1689
1690// ----------------------------------------------------------------------------
1691// tooltips
1692// ----------------------------------------------------------------------------
1693
1694#if wxUSE_TOOLTIPS
1695
e766c8a9 1696void wxWindowMac::DoSetToolTip(wxToolTip *tooltip)
51abe921
SC
1697{
1698 wxWindowBase::DoSetToolTip(tooltip);
6ed71b4f 1699
6264b550
RR
1700 if ( m_tooltip )
1701 m_tooltip->SetWindow(this);
51abe921
SC
1702}
1703
898d9035 1704#endif
51abe921 1705
902725ee 1706void wxWindowMac::MacInvalidateBorders()
6449b3a8
SC
1707{
1708 if ( m_peer == NULL )
1709 return ;
1710
1711 bool vis = MacIsReallyShown() ;
1712 if ( !vis )
1713 return ;
902725ee 1714
6449b3a8
SC
1715 int outerBorder = MacGetLeftBorderSize() ;
1716 if ( m_peer->NeedsFocusRect() && m_peer->HasFocus() )
1717 outerBorder += 4 ;
1718
1719 if ( outerBorder == 0 )
1720 return ;
902725ee
WS
1721
1722 // now we know that we have something to do at all
6449b3a8
SC
1723
1724 // as the borders are drawn on the parent we have to properly invalidate all these areas
58603178
DS
1725 RgnHandle updateInner , updateOuter;
1726 Rect rect ;
6449b3a8
SC
1727
1728 // this rectangle is in HIViewCoordinates under OSX and in Window Coordinates under Carbon
58603178
DS
1729 updateInner = NewRgn() ;
1730 updateOuter = NewRgn() ;
1731
6449b3a8 1732 m_peer->GetRect( &rect ) ;
898d9035 1733 RectRgn( updateInner, &rect ) ;
6449b3a8 1734 InsetRect( &rect , -outerBorder , -outerBorder ) ;
898d9035
DS
1735 RectRgn( updateOuter, &rect ) ;
1736 DiffRgn( updateOuter, updateInner , updateOuter ) ;
e15f0a5e 1737
6449b3a8 1738#ifdef __WXMAC_OSX__
1f1c8bd4 1739 GetParent()->m_peer->SetNeedsDisplay( updateOuter ) ;
6449b3a8
SC
1740#else
1741 WindowRef tlw = (WindowRef) MacGetTopLevelWindowRef() ;
1742 if ( tlw )
1743 InvalWindowRgn( tlw , updateOuter ) ;
1744#endif
e15f0a5e 1745
58603178
DS
1746 DisposeRgn( updateOuter ) ;
1747 DisposeRgn( updateInner ) ;
6449b3a8
SC
1748}
1749
e766c8a9 1750void wxWindowMac::DoMoveWindow(int x, int y, int width, int height)
51abe921 1751{
db7a550b 1752 // this is never called for a toplevel window, so we know we have a parent
facd6764 1753 int former_x , former_y , former_w, former_h ;
db7a550b
SC
1754
1755 // Get true coordinates of former position
facd6764
SC
1756 DoGetPosition( &former_x , &former_y ) ;
1757 DoGetSize( &former_w , &former_h ) ;
db7a550b
SC
1758
1759 wxWindow *parent = GetParent();
1760 if ( parent )
1761 {
1762 wxPoint pt(parent->GetClientAreaOrigin());
1763 former_x += pt.x ;
1764 former_y += pt.y ;
1765 }
6ed71b4f 1766
29ee02df
SC
1767 int actualWidth = width ;
1768 int actualHeight = height ;
e40298d5
JS
1769 int actualX = x;
1770 int actualY = y;
6ed71b4f 1771
14c9cbdb 1772 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
6264b550 1773 actualWidth = m_minWidth;
14c9cbdb 1774 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
6264b550 1775 actualHeight = m_minHeight;
14c9cbdb 1776 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
6264b550 1777 actualWidth = m_maxWidth;
14c9cbdb 1778 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
902725ee 1779 actualHeight = m_maxHeight;
6ed71b4f 1780
898d9035 1781 bool doMove = false, doResize = false ;
6ed71b4f 1782
6264b550 1783 if ( actualX != former_x || actualY != former_y )
6264b550 1784 doMove = true ;
e15f0a5e 1785
6264b550 1786 if ( actualWidth != former_w || actualHeight != former_h )
6264b550 1787 doResize = true ;
6ed71b4f 1788
6264b550
RR
1789 if ( doMove || doResize )
1790 {
902725ee 1791 // as the borders are drawn outside the native control, we adjust now
1f1c8bd4 1792
902725ee
WS
1793 wxRect bounds( wxPoint( actualX + MacGetLeftBorderSize() ,actualY + MacGetTopBorderSize() ),
1794 wxSize( actualWidth - (MacGetLeftBorderSize() + MacGetRightBorderSize()) ,
1f1c8bd4
SC
1795 actualHeight - (MacGetTopBorderSize() + MacGetBottomBorderSize()) ) ) ;
1796
1797 Rect r ;
1798 wxMacRectToNative( &bounds , &r ) ;
1799
1800 if ( !GetParent()->IsTopLevel() )
1f1c8bd4 1801 wxMacWindowToNative( GetParent() , &r ) ;
8b573fb8 1802
6449b3a8 1803 MacInvalidateBorders() ;
902725ee 1804
5c840e5b 1805 m_cachedClippedRectValid = false ;
8b573fb8 1806 m_peer->SetRect( &r ) ;
902725ee 1807
e905b636 1808 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
91ae6e3a 1809
6449b3a8
SC
1810 MacInvalidateBorders() ;
1811
6264b550
RR
1812 MacRepositionScrollBars() ;
1813 if ( doMove )
1814 {
898d9035 1815 wxPoint point(actualX, actualY);
6264b550
RR
1816 wxMoveEvent event(point, m_windowId);
1817 event.SetEventObject(this);
1818 GetEventHandler()->ProcessEvent(event) ;
1819 }
e15f0a5e 1820
6264b550
RR
1821 if ( doResize )
1822 {
e40298d5 1823 MacRepositionScrollBars() ;
facd6764 1824 wxSize size(actualWidth, actualHeight);
e40298d5
JS
1825 wxSizeEvent event(size, m_windowId);
1826 event.SetEventObject(this);
1827 GetEventHandler()->ProcessEvent(event);
6264b550
RR
1828 }
1829 }
954fc50b
SC
1830}
1831
facd6764
SC
1832wxSize wxWindowMac::DoGetBestSize() const
1833{
eb69d46e
SC
1834 if ( m_macIsUserPane || IsTopLevel() )
1835 return wxWindowBase::DoGetBestSize() ;
8b573fb8 1836
facd6764 1837 Rect bestsize = { 0 , 0 , 0 , 0 } ;
facd6764 1838 int bestWidth, bestHeight ;
facd6764 1839
898d9035 1840 m_peer->GetBestRect( &bestsize ) ;
facd6764
SC
1841 if ( EmptyRect( &bestsize ) )
1842 {
898d9035
DS
1843 bestsize.left =
1844 bestsize.top = 0 ;
1845 bestsize.right =
facd6764 1846 bestsize.bottom = 16 ;
898d9035 1847
facd6764
SC
1848 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
1849 {
1850 bestsize.bottom = 16 ;
1851 }
902725ee 1852#if wxUSE_SPINBTN
facd6764
SC
1853 else if ( IsKindOf( CLASSINFO( wxSpinButton ) ) )
1854 {
8b573fb8 1855 bestsize.bottom = 24 ;
facd6764 1856 }
898d9035 1857#endif
facd6764
SC
1858 else
1859 {
8b573fb8 1860 // return wxWindowBase::DoGetBestSize() ;
facd6764
SC
1861 }
1862 }
1863
1864 bestWidth = bestsize.right - bestsize.left ;
1865 bestHeight = bestsize.bottom - bestsize.top ;
1866 if ( bestHeight < 10 )
1867 bestHeight = 13 ;
8b573fb8 1868
facd6764
SC
1869 return wxSize(bestWidth, bestHeight);
1870}
1871
954fc50b
SC
1872// set the size of the window: if the dimensions are positive, just use them,
1873// but if any of them is equal to -1, it means that we must find the value for
1874// it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
1875// which case -1 is a valid value for x and y)
1876//
1877// If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
1878// the width/height to best suit our contents, otherwise we reuse the current
1879// width/height
1880void wxWindowMac::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1881{
1882 // get the current size and position...
1883 int currentX, currentY;
898d9035 1884 int currentW, currentH;
6ed71b4f 1885
898d9035 1886 GetPosition(&currentX, &currentY);
954fc50b 1887 GetSize(&currentW, &currentH);
6ed71b4f 1888
954fc50b
SC
1889 // ... and don't do anything (avoiding flicker) if it's already ok
1890 if ( x == currentX && y == currentY &&
769ac869 1891 width == currentW && height == currentH && ( height != -1 && width != -1 ) )
954fc50b 1892 {
e15f0a5e 1893 // TODO: REMOVE
6264b550 1894 MacRepositionScrollBars() ; // we might have a real position shift
898d9035 1895
954fc50b
SC
1896 return;
1897 }
6ed71b4f 1898
58603178
DS
1899 if ( !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1900 {
1901 if ( x == wxDefaultCoord )
1902 x = currentX;
1903 if ( y == wxDefaultCoord )
1904 y = currentY;
1905 }
6ed71b4f 1906
58603178 1907 AdjustForParentClientOrigin( x, y, sizeFlags );
6ed71b4f 1908
3dee36ae
WS
1909 wxSize size = wxDefaultSize;
1910 if ( width == wxDefaultCoord )
954fc50b
SC
1911 {
1912 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
1913 {
1914 size = DoGetBestSize();
1915 width = size.x;
1916 }
1917 else
1918 {
1919 // just take the current one
1920 width = currentW;
1921 }
1922 }
6ed71b4f 1923
3dee36ae 1924 if ( height == wxDefaultCoord )
954fc50b
SC
1925 {
1926 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
1927 {
3dee36ae 1928 if ( size.x == wxDefaultCoord )
954fc50b 1929 size = DoGetBestSize();
898d9035 1930 // else: already called DoGetBestSize() above
6ed71b4f 1931
954fc50b
SC
1932 height = size.y;
1933 }
1934 else
1935 {
1936 // just take the current one
1937 height = currentH;
1938 }
1939 }
6ed71b4f 1940
58603178 1941 DoMoveWindow( x, y, width, height );
e9576ca5 1942}
519cb848 1943
e766c8a9 1944wxPoint wxWindowMac::GetClientAreaOrigin() const
e9576ca5 1945{
facd6764
SC
1946 RgnHandle rgn = NewRgn() ;
1947 Rect content ;
04d4e684 1948 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
21638402
SC
1949 {
1950 GetRegionBounds( rgn , &content ) ;
1951 }
1952 else
1953 {
898d9035
DS
1954 content.left =
1955 content.top = 0 ;
21638402 1956 }
898d9035 1957
facd6764 1958 DisposeRgn( rgn ) ;
e15f0a5e
DS
1959
1960 return wxPoint( content.left + MacGetLeftBorderSize() , content.top + MacGetTopBorderSize() );
facd6764
SC
1961}
1962
1963void wxWindowMac::DoSetClientSize(int clientwidth, int clientheight)
1964{
3dee36ae 1965 if ( clientheight != wxDefaultCoord || clientheight != wxDefaultCoord )
facd6764
SC
1966 {
1967 int currentclientwidth , currentclientheight ;
1968 int currentwidth , currentheight ;
1969
1970 GetClientSize( &currentclientwidth , &currentclientheight ) ;
1971 GetSize( &currentwidth , &currentheight ) ;
1972
3dee36ae 1973 DoSetSize( wxDefaultCoord , wxDefaultCoord , currentwidth + clientwidth - currentclientwidth ,
facd6764
SC
1974 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
1975 }
e9576ca5
SC
1976}
1977
fb5246be 1978void wxWindowMac::SetLabel(const wxString& title)
e9576ca5 1979{
facd6764
SC
1980 m_label = wxStripMenuCodes(title) ;
1981
21fd5529 1982 if ( m_peer && m_peer->Ok() )
fb5246be 1983 m_peer->SetLabel( m_label ) ;
e15f0a5e 1984
facd6764 1985 Refresh() ;
519cb848
SC
1986}
1987
fb5246be 1988wxString wxWindowMac::GetLabel() const
519cb848 1989{
ed60b502 1990 return m_label ;
519cb848
SC
1991}
1992
8ab50549
SC
1993bool wxWindowMac::Show(bool show)
1994{
542f604f 1995 bool former = MacIsReallyShown() ;
8ab50549 1996 if ( !wxWindowBase::Show(show) )
902725ee 1997 return false;
8b573fb8 1998
58603178 1999 // TODO: use visibilityChanged Carbon Event for OSX
2c899c20 2000 if ( m_peer )
2c899c20 2001 m_peer->SetVisibility( show , true ) ;
e15f0a5e 2002
542f604f
SC
2003 if ( former != MacIsReallyShown() )
2004 MacPropagateVisibilityChanged() ;
898d9035 2005
902725ee 2006 return true;
8ab50549
SC
2007}
2008
2009bool wxWindowMac::Enable(bool enable)
2010{
21fd5529 2011 wxASSERT( m_peer->Ok() ) ;
542f604f 2012 bool former = MacIsReallyEnabled() ;
8ab50549 2013 if ( !wxWindowBase::Enable(enable) )
902725ee 2014 return false;
8ab50549 2015
5ca0d812 2016 m_peer->Enable( enable ) ;
8ab50549
SC
2017
2018 if ( former != MacIsReallyEnabled() )
2019 MacPropagateEnabledStateChanged() ;
e15f0a5e 2020
902725ee 2021 return true;
8ab50549
SC
2022}
2023
8b573fb8 2024//
8ab50549
SC
2025// status change propagations (will be not necessary for OSX later )
2026//
2027
facd6764
SC
2028void wxWindowMac::MacPropagateVisibilityChanged()
2029{
73fe67bd 2030#if !TARGET_API_MAC_OSX
facd6764 2031 MacVisibilityChanged() ;
8b573fb8 2032
e15f0a5e 2033 wxWindowMac *child;
71f2fb52 2034 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
facd6764
SC
2035 while ( node )
2036 {
e15f0a5e 2037 child = node->GetData();
facd6764 2038 if ( child->IsShown() )
e15f0a5e
DS
2039 child->MacPropagateVisibilityChanged() ;
2040
facd6764
SC
2041 node = node->GetNext();
2042 }
73fe67bd 2043#endif
facd6764
SC
2044}
2045
e15f0a5e 2046void wxWindowMac::MacPropagateEnabledStateChanged()
e9576ca5 2047{
73fe67bd 2048#if !TARGET_API_MAC_OSX
8ab50549 2049 MacEnabledStateChanged() ;
8b573fb8 2050
e15f0a5e 2051 wxWindowMac *child;
71f2fb52 2052 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
8ab50549
SC
2053 while ( node )
2054 {
e15f0a5e 2055 child = node->GetData();
8ab50549
SC
2056 if ( child->IsEnabled() )
2057 child->MacPropagateEnabledStateChanged() ;
e15f0a5e 2058
8ab50549
SC
2059 node = node->GetNext();
2060 }
73fe67bd 2061#endif
8ab50549
SC
2062}
2063
e15f0a5e 2064void wxWindowMac::MacPropagateHiliteChanged()
8ab50549 2065{
73fe67bd 2066#if !TARGET_API_MAC_OSX
8ab50549 2067 MacHiliteChanged() ;
8b573fb8 2068
e15f0a5e 2069 wxWindowMac *child;
71f2fb52 2070 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
8ab50549
SC
2071 while ( node )
2072 {
e15f0a5e 2073 child = node->GetData();
898d9035 2074 if (child /* && child->IsEnabled() */)
8ab50549 2075 child->MacPropagateHiliteChanged() ;
e15f0a5e 2076
8ab50549
SC
2077 node = node->GetNext();
2078 }
73fe67bd 2079#endif
8ab50549
SC
2080}
2081
2082//
2083// status change notifications
8b573fb8 2084//
8ab50549 2085
8b573fb8 2086void wxWindowMac::MacVisibilityChanged()
8ab50549
SC
2087{
2088}
2089
8b573fb8 2090void wxWindowMac::MacHiliteChanged()
8ab50549
SC
2091{
2092}
2093
8b573fb8 2094void wxWindowMac::MacEnabledStateChanged()
8ab50549 2095{
facd6764 2096}
e7549107 2097
8ab50549
SC
2098//
2099// status queries on the inherited window's state
2100//
2101
8b573fb8 2102bool wxWindowMac::MacIsReallyShown()
facd6764
SC
2103{
2104 // only under OSX the visibility of the TLW is taken into account
66ffb23b
SC
2105 if ( m_isBeingDeleted )
2106 return false ;
902725ee 2107
facd6764 2108#if TARGET_API_MAC_OSX
aa522e33
SC
2109 if ( m_peer && m_peer->Ok() )
2110 return m_peer->IsVisible();
2111#endif
e15f0a5e 2112
facd6764 2113 wxWindow* win = this ;
898d9035 2114 while ( win->IsShown() )
facd6764
SC
2115 {
2116 if ( win->IsTopLevel() )
2117 return true ;
8b573fb8 2118
facd6764
SC
2119 win = win->GetParent() ;
2120 if ( win == NULL )
2121 return true ;
e15f0a5e 2122 }
8b573fb8 2123
facd6764 2124 return false ;
facd6764 2125}
4241baae 2126
8b573fb8 2127bool wxWindowMac::MacIsReallyEnabled()
facd6764 2128{
5ca0d812 2129 return m_peer->IsEnabled() ;
facd6764
SC
2130}
2131
8b573fb8 2132bool wxWindowMac::MacIsReallyHilited()
c809f3be 2133{
5ca0d812 2134 return m_peer->IsActive();
c809f3be
SC
2135}
2136
8b573fb8 2137void wxWindowMac::MacFlashInvalidAreas()
002c9672
SC
2138{
2139#if TARGET_API_MAC_OSX
2140 HIViewFlashDirtyArea( (WindowRef) MacGetTopLevelWindowRef() ) ;
2141#endif
2142}
2143
e766c8a9 2144int wxWindowMac::GetCharHeight() const
e9576ca5 2145{
e15f0a5e
DS
2146 wxClientDC dc( (wxWindowMac*)this ) ;
2147
6264b550 2148 return dc.GetCharHeight() ;
e9576ca5
SC
2149}
2150
e766c8a9 2151int wxWindowMac::GetCharWidth() const
e9576ca5 2152{
e15f0a5e
DS
2153 wxClientDC dc( (wxWindowMac*)this ) ;
2154
6264b550 2155 return dc.GetCharWidth() ;
e9576ca5
SC
2156}
2157
e766c8a9 2158void wxWindowMac::GetTextExtent(const wxString& string, int *x, int *y,
e7549107 2159 int *descent, int *externalLeading, const wxFont *theFont ) const
e9576ca5 2160{
e7549107
SC
2161 const wxFont *fontToUse = theFont;
2162 if ( !fontToUse )
2163 fontToUse = &m_font;
14c9cbdb 2164
e766c8a9 2165 wxClientDC dc( (wxWindowMac*) this ) ;
7c74e7fe 2166 long lx,ly,ld,le ;
5fde6fcc 2167 dc.GetTextExtent( string , &lx , &ly , &ld, &le, (wxFont *)fontToUse ) ;
2f1ae414 2168 if ( externalLeading )
6264b550 2169 *externalLeading = le ;
2f1ae414 2170 if ( descent )
6264b550 2171 *descent = ld ;
2f1ae414 2172 if ( x )
6264b550 2173 *x = lx ;
2f1ae414 2174 if ( y )
6264b550 2175 *y = ly ;
e9576ca5
SC
2176}
2177
0a67a93b 2178/*
14c9cbdb 2179 * Rect is given in client coordinates, for further reading, read wxTopLevelWindowMac::InvalidateRect
1c310985
SC
2180 * we always intersect with the entire window, not only with the client area
2181 */
14c9cbdb 2182
e766c8a9 2183void wxWindowMac::Refresh(bool eraseBack, const wxRect *rect)
e9576ca5 2184{
065ab451
SC
2185 if ( m_peer == NULL )
2186 return ;
8b573fb8 2187
1f1c8bd4
SC
2188 if ( !MacIsReallyShown() )
2189 return ;
789ae0cf 2190
1f1c8bd4 2191 if ( rect )
1e8cde71 2192 {
1f1c8bd4 2193 Rect r ;
898d9035 2194
1f1c8bd4
SC
2195 wxMacRectToNative( rect , &r ) ;
2196 m_peer->SetNeedsDisplay( &r ) ;
facd6764 2197 }
1f1c8bd4 2198 else
9a456218 2199 {
1f1c8bd4 2200 m_peer->SetNeedsDisplay() ;
e9576ca5 2201 }
facd6764
SC
2202}
2203
79392158
SC
2204void wxWindowMac::Freeze()
2205{
2206#if TARGET_API_MAC_OSX
2207 if ( !m_frozenness++ )
2208 {
52ef5c3c
RD
2209 if ( m_peer && m_peer->Ok() )
2210 m_peer->SetDrawingEnabled( false ) ;
79392158
SC
2211 }
2212#endif
2213}
2214
2215void wxWindowMac::Thaw()
2216{
2217#if TARGET_API_MAC_OSX
898d9035 2218 wxASSERT_MSG( m_frozenness > 0, wxT("Thaw() without matching Freeze()") );
79392158
SC
2219
2220 if ( !--m_frozenness )
2221 {
52ef5c3c
RD
2222 if ( m_peer && m_peer->Ok() )
2223 {
2224 m_peer->SetDrawingEnabled( true ) ;
2225 m_peer->InvalidateWithChildren() ;
2226 }
79392158
SC
2227 }
2228#endif
2229}
2230
e766c8a9 2231wxWindowMac *wxGetActiveWindow()
e9576ca5 2232{
519cb848 2233 // actually this is a windows-only concept
e9576ca5
SC
2234 return NULL;
2235}
2236
e9576ca5 2237// Coordinates relative to the window
898d9035 2238void wxWindowMac::WarpPointer(int x_pos, int y_pos)
e9576ca5 2239{
e40298d5 2240 // We really don't move the mouse programmatically under Mac.
e9576ca5
SC
2241}
2242
facd6764 2243void wxWindowMac::OnEraseBackground(wxEraseEvent& event)
e9576ca5 2244{
3dee36ae
WS
2245 if ( MacGetTopLevelWindow() == NULL )
2246 return ;
898d9035 2247
be346c26 2248#if TARGET_API_MAC_OSX
898d9035 2249 if ( MacGetTopLevelWindow()->MacUsesCompositing() && (!m_macBackgroundBrush.Ok() || m_macBackgroundBrush.GetStyle() == wxTRANSPARENT ) )
94abc21f 2250 {
facd6764 2251 event.Skip() ;
94abc21f
SC
2252 }
2253 else
be346c26 2254#endif
7ebf5540 2255 {
8b573fb8 2256 event.GetDC()->Clear() ;
7ebf5540 2257 }
1c310985
SC
2258}
2259
2260void wxWindowMac::OnNcPaint( wxNcPaintEvent& event )
2261{
af6b7b80 2262 event.Skip() ;
e9576ca5
SC
2263}
2264
e766c8a9 2265int wxWindowMac::GetScrollPos(int orient) const
e9576ca5 2266{
1c310985
SC
2267 if ( orient == wxHORIZONTAL )
2268 {
2269 if ( m_hScrollBar )
2270 return m_hScrollBar->GetThumbPosition() ;
2271 }
2272 else
2273 {
2274 if ( m_vScrollBar )
2275 return m_vScrollBar->GetThumbPosition() ;
2276 }
e15f0a5e 2277
e9576ca5
SC
2278 return 0;
2279}
2280
2281// This now returns the whole range, not just the number
2282// of positions that we can scroll.
e766c8a9 2283int wxWindowMac::GetScrollRange(int orient) const
e9576ca5 2284{
1c310985
SC
2285 if ( orient == wxHORIZONTAL )
2286 {
2287 if ( m_hScrollBar )
2288 return m_hScrollBar->GetRange() ;
2289 }
2290 else
2291 {
2292 if ( m_vScrollBar )
2293 return m_vScrollBar->GetRange() ;
2294 }
e15f0a5e 2295
e9576ca5
SC
2296 return 0;
2297}
2298
e766c8a9 2299int wxWindowMac::GetScrollThumb(int orient) const
e9576ca5 2300{
1c310985
SC
2301 if ( orient == wxHORIZONTAL )
2302 {
2303 if ( m_hScrollBar )
2304 return m_hScrollBar->GetThumbSize() ;
2305 }
2306 else
2307 {
2308 if ( m_vScrollBar )
2309 return m_vScrollBar->GetThumbSize() ;
2310 }
e15f0a5e 2311
e9576ca5
SC
2312 return 0;
2313}
2314
e766c8a9 2315void wxWindowMac::SetScrollPos(int orient, int pos, bool refresh)
e9576ca5 2316{
1c310985 2317 if ( orient == wxHORIZONTAL )
6264b550 2318 {
1c310985
SC
2319 if ( m_hScrollBar )
2320 m_hScrollBar->SetThumbPosition( pos ) ;
6264b550
RR
2321 }
2322 else
2323 {
1c310985
SC
2324 if ( m_vScrollBar )
2325 m_vScrollBar->SetThumbPosition( pos ) ;
6264b550 2326 }
2f1ae414
SC
2327}
2328
c79aad8b
SC
2329//
2330// we draw borders and grow boxes, are already set up and clipped in the current port / cgContextRef
2331// our own window origin is at leftOrigin/rightOrigin
2332//
2333
2334void wxWindowMac::MacPaintBorders( int leftOrigin , int rightOrigin )
2f1ae414 2335{
e15f0a5e 2336 if ( IsTopLevel() )
6264b550 2337 return ;
8b573fb8 2338
fd926bcc 2339 Rect rect ;
c79aad8b 2340 bool hasFocus = m_peer->NeedsFocusRect() && m_peer->HasFocus() ;
e15f0a5e 2341 bool hasBothScrollbars = (m_hScrollBar && m_hScrollBar->IsShown()) && (m_vScrollBar && m_vScrollBar->IsShown()) ;
c79aad8b 2342
8f39b6c4 2343 // back to the surrounding frame rectangle
58603178 2344 m_peer->GetRect( &rect ) ;
8f39b6c4 2345 InsetRect( &rect, -1 , -1 ) ;
fd926bcc 2346
c79aad8b 2347#if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
e15f0a5e 2348 if ( UMAGetSystemVersion() >= 0x1030 )
c79aad8b 2349 {
8f39b6c4
SC
2350 CGRect cgrect = CGRectMake( rect.left , rect.top , rect.right - rect.left ,
2351 rect.bottom - rect.top ) ;
2352
c79aad8b 2353 HIThemeFrameDrawInfo info ;
58603178 2354 memset( &info, 0 , sizeof(info) ) ;
902725ee 2355
c79aad8b
SC
2356 info.version = 0 ;
2357 info.kind = 0 ;
2358 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive ;
2359 info.isFocused = hasFocus ;
c79aad8b
SC
2360
2361 CGContextRef cgContext = (CGContextRef) GetParent()->MacGetCGContextRef() ;
2362 wxASSERT( cgContext ) ;
902725ee 2363
e15f0a5e 2364 if ( HasFlag(wxRAISED_BORDER) || HasFlag(wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
c79aad8b 2365 {
c79aad8b 2366 info.kind = kHIThemeFrameTextFieldSquare ;
8f39b6c4 2367 HIThemeDrawFrame( &cgrect , &info , cgContext , kHIThemeOrientationNormal ) ;
c79aad8b 2368 }
e15f0a5e 2369 else if ( HasFlag(wxSIMPLE_BORDER) )
c79aad8b 2370 {
c79aad8b 2371 info.kind = kHIThemeFrameListBox ;
c79aad8b
SC
2372 HIThemeDrawFrame( &cgrect , &info , cgContext , kHIThemeOrientationNormal ) ;
2373 }
2374 else if ( hasFocus )
2375 {
c79aad8b
SC
2376 HIThemeDrawFocusRect( &cgrect , true , cgContext , kHIThemeOrientationNormal ) ;
2377 }
902725ee 2378
c79aad8b
SC
2379 m_peer->GetRect( &rect ) ;
2380 if ( hasBothScrollbars )
2381 {
c79aad8b 2382 int size = m_hScrollBar->GetWindowVariant() == wxWINDOW_VARIANT_NORMAL ? 16 : 12 ;
8f39b6c4
SC
2383 CGRect cgrect = CGRectMake( rect.right - size , rect.bottom - size , size , size ) ;
2384 CGPoint cgpoint = CGPointMake( rect.right - size , rect.bottom - size ) ;
902725ee 2385 HIThemeGrowBoxDrawInfo info ;
e15f0a5e 2386 memset( &info, 0, sizeof(info) ) ;
c79aad8b
SC
2387 info.version = 0 ;
2388 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive ;
2389 info.kind = kHIThemeGrowBoxKindNone ;
2390 info.size = kHIThemeGrowBoxSizeNormal ;
2391 info.direction = kThemeGrowRight | kThemeGrowDown ;
2392 HIThemeDrawGrowBox( &cgpoint , &info , cgContext , kHIThemeOrientationNormal ) ;
2393 }
2394 }
2395 else
2396#endif
fd926bcc
SC
2397 {
2398 wxTopLevelWindowMac* top = MacGetTopLevelWindow();
e15f0a5e 2399 if ( top )
fd926bcc 2400 {
e15f0a5e 2401 wxPoint pt(0, 0) ;
fd926bcc 2402 wxMacControl::Convert( &pt , GetParent()->m_peer , top->m_peer ) ;
1f1c8bd4 2403 OffsetRect( &rect , pt.x , pt.y ) ;
fd926bcc 2404 }
8b573fb8 2405
e15f0a5e
DS
2406 if ( HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
2407 DrawThemeEditTextFrame( &rect, IsEnabled() ? kThemeStateActive : kThemeStateInactive ) ;
2408 else if ( HasFlag(wxSIMPLE_BORDER) )
2409 DrawThemeListBoxFrame( &rect, IsEnabled() ? kThemeStateActive : kThemeStateInactive ) ;
902725ee 2410
c79aad8b 2411 if ( hasFocus )
8f39b6c4 2412 DrawThemeFocusRect( &rect , true ) ;
8f39b6c4 2413
c79aad8b
SC
2414 if ( hasBothScrollbars )
2415 {
902725ee 2416 // GetThemeStandaloneGrowBoxBounds
e15f0a5e 2417 // DrawThemeStandaloneNoGrowBox
c79aad8b 2418 }
eec462f8 2419 }
8208e181
SC
2420}
2421
abda5788
SC
2422void wxWindowMac::RemoveChild( wxWindowBase *child )
2423{
2424 if ( child == m_hScrollBar )
2425 m_hScrollBar = NULL ;
2426 if ( child == m_vScrollBar )
2427 m_vScrollBar = NULL ;
14c9cbdb 2428
abda5788
SC
2429 wxWindowBase::RemoveChild( child ) ;
2430}
2431
e9576ca5 2432// New function that will replace some of the above.
e766c8a9 2433void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible,
e9576ca5
SC
2434 int range, bool refresh)
2435{
58603178
DS
2436 bool showScroller;
2437
e40298d5
JS
2438 if ( orient == wxHORIZONTAL )
2439 {
2440 if ( m_hScrollBar )
6264b550 2441 {
58603178
DS
2442 showScroller = ((range != 0) && (range > thumbVisible));
2443 if ( m_hScrollBar->IsShown() != showScroller )
2444 m_hScrollBar->Show( showScroller ) ;
e15f0a5e 2445
11ca2edf 2446 m_hScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
6264b550 2447 }
e40298d5
JS
2448 }
2449 else
2450 {
2451 if ( m_vScrollBar )
6264b550 2452 {
58603178
DS
2453 showScroller = ((range != 0) && (range > thumbVisible));
2454 if ( m_vScrollBar->IsShown() != showScroller )
2455 m_vScrollBar->Show( showScroller ) ;
e15f0a5e 2456
11ca2edf 2457 m_vScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
6264b550 2458 }
e40298d5 2459 }
e15f0a5e 2460
e40298d5 2461 MacRepositionScrollBars() ;
e9576ca5
SC
2462}
2463
2464// Does a physical scroll
e766c8a9 2465void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect)
e9576ca5 2466{
898d9035 2467 if ( dx == 0 && dy == 0 )
ba87f54c 2468 return ;
8b573fb8 2469
3dee36ae
WS
2470 int width , height ;
2471 GetClientSize( &width , &height ) ;
e15f0a5e 2472
adb8a71b 2473#if TARGET_API_MAC_OSX
e15f0a5e 2474 if ( true /* m_peer->IsCompositing() */ )
902725ee 2475 {
002c9672 2476 // note there currently is a bug in OSX which makes inefficient refreshes in case an entire control
8b573fb8 2477 // area is scrolled, this does not occur if width and height are 2 pixels less,
58603178 2478 // TODO: write optimal workaround
898d9035 2479 wxRect scrollrect( MacGetLeftBorderSize() , MacGetTopBorderSize() , width , height ) ;
8b573fb8 2480 if ( rect )
5ca0d812 2481 scrollrect.Intersect( *rect ) ;
e15f0a5e 2482
5ca0d812 2483 if ( m_peer->GetNeedsDisplay() )
002c9672 2484 {
58603178 2485 // because HIViewScrollRect does not scroll the already invalidated area we have two options:
002c9672
SC
2486 // either immediate redraw or full invalidate
2487#if 1
2488 // is the better overall solution, as it does not slow down scrolling
1f1c8bd4 2489 m_peer->SetNeedsDisplay() ;
002c9672 2490#else
8b573fb8 2491 // this would be the preferred version for fast drawing controls
92346151 2492
f474cc8c 2493#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
e15f0a5e 2494 if ( UMAGetSystemVersion() >= 0x1030 && m_peer->IsCompositing() )
42ef83fa 2495 HIViewRender(m_peer->GetControlRef()) ;
f474cc8c
SC
2496 else
2497#endif
2498 Update() ;
002c9672
SC
2499#endif
2500 }
e15f0a5e 2501
84e5d27d
SC
2502 // as the native control might be not a 0/0 wx window coordinates, we have to offset
2503 scrollrect.Offset( -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
898d9035 2504 m_peer->ScrollRect( &scrollrect , dx , dy ) ;
92346151
SC
2505
2506 // becuase HIViewScrollRect does not scroll the already invalidated area we have two options
2507 // either immediate redraw or full invalidate
2508#if 0
2509 // is the better overall solution, as it does not slow down scrolling
2510 m_peer->SetNeedsDisplay() ;
adb8a71b 2511#else
902725ee 2512 // this would be the preferred version for fast drawing controls
92346151 2513
f474cc8c 2514#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
e15f0a5e 2515 if ( UMAGetSystemVersion() >= 0x1030 && m_peer->IsCompositing() )
f474cc8c
SC
2516 HIViewRender(m_peer->GetControlRef()) ;
2517 else
2518#endif
902725ee 2519 Update() ;
92346151 2520#endif
902725ee
WS
2521 }
2522 else
92346151 2523#endif
902725ee 2524 {
facd6764 2525 wxPoint pos;
898d9035
DS
2526 pos.x =
2527 pos.y = 0;
430e1eed 2528
facd6764 2529 Rect scrollrect;
6264b550 2530 RgnHandle updateRgn = NewRgn() ;
430e1eed 2531
3dee36ae 2532 {
430e1eed
SC
2533 wxClientDC dc(this) ;
2534 wxMacPortSetter helper(&dc) ;
8b573fb8 2535
92346151
SC
2536 m_peer->GetRectInWindowCoords( &scrollrect ) ;
2537 //scrollrect.top += MacGetTopBorderSize() ;
2538 //scrollrect.left += MacGetLeftBorderSize() ;
430e1eed
SC
2539 scrollrect.bottom = scrollrect.top + height ;
2540 scrollrect.right = scrollrect.left + width ;
8b573fb8 2541
430e1eed
SC
2542 if ( rect )
2543 {
2544 Rect r = { dc.YLOG2DEVMAC(rect->y) , dc.XLOG2DEVMAC(rect->x) , dc.YLOG2DEVMAC(rect->y + rect->height) ,
2545 dc.XLOG2DEVMAC(rect->x + rect->width) } ;
2546 SectRect( &scrollrect , &r , &scrollrect ) ;
2547 }
e15f0a5e 2548
430e1eed 2549 ScrollRect( &scrollrect , dx , dy , updateRgn ) ;
20b69855
SC
2550
2551 // now scroll the former update region as well and add the new update region
20b69855
SC
2552 WindowRef rootWindow = (WindowRef) MacGetTopLevelWindowRef() ;
2553 RgnHandle formerUpdateRgn = NewRgn() ;
2554 RgnHandle scrollRgn = NewRgn() ;
2555 RectRgn( scrollRgn , &scrollrect ) ;
2556 GetWindowUpdateRgn( rootWindow , formerUpdateRgn ) ;
898d9035 2557 Point pt = {0, 0} ;
20b69855
SC
2558 LocalToGlobal( &pt ) ;
2559 OffsetRgn( formerUpdateRgn , -pt.h , -pt.v ) ;
2560 SectRgn( formerUpdateRgn , scrollRgn , formerUpdateRgn ) ;
e15f0a5e 2561
20b69855
SC
2562 if ( !EmptyRgn( formerUpdateRgn ) )
2563 {
2564 MacOffsetRgn( formerUpdateRgn , dx , dy ) ;
2565 SectRgn( formerUpdateRgn , scrollRgn , formerUpdateRgn ) ;
898d9035 2566 InvalWindowRgn( rootWindow, formerUpdateRgn ) ;
20b69855 2567 }
e15f0a5e 2568
898d9035 2569 InvalWindowRgn(rootWindow, updateRgn ) ;
20b69855
SC
2570 DisposeRgn( updateRgn ) ;
2571 DisposeRgn( formerUpdateRgn ) ;
2572 DisposeRgn( scrollRgn ) ;
ba87f54c 2573 }
e15f0a5e 2574
92346151 2575 Update() ;
6264b550 2576 }
6ed71b4f 2577
e15f0a5e
DS
2578 wxWindowMac *child;
2579 int x, y, w, h;
71f2fb52 2580 for (wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); node; node = node->GetNext())
6264b550 2581 {
e15f0a5e
DS
2582 child = node->GetData();
2583 if (child == NULL)
2584 continue;
2585 if (child == m_vScrollBar)
2586 continue;
2587 if (child == m_hScrollBar)
2588 continue;
2589 if (child->IsTopLevel())
2590 continue;
6ed71b4f 2591
6264b550 2592 child->GetPosition( &x, &y );
6264b550 2593 child->GetSize( &w, &h );
00f55394
SC
2594 if (rect)
2595 {
e15f0a5e
DS
2596 wxRect rc( x, y, w, h );
2597 if (rect->Intersects( rc ))
2598 child->SetSize( x + dx, y + dy, w, h );
00f55394
SC
2599 }
2600 else
2601 {
e15f0a5e 2602 child->SetSize( x + dx, y + dy, w, h );
8b573fb8 2603 }
6264b550 2604 }
e9576ca5
SC
2605}
2606
e15f0a5e 2607void wxWindowMac::MacOnScroll( wxScrollEvent &event )
7c74e7fe 2608{
687706f5 2609 if ( event.GetEventObject() == m_vScrollBar || event.GetEventObject() == m_hScrollBar )
6264b550
RR
2610 {
2611 wxScrollWinEvent wevent;
2612 wevent.SetPosition(event.GetPosition());
2613 wevent.SetOrientation(event.GetOrientation());
687706f5
KH
2614 wevent.SetEventObject(this);
2615
2616 if (event.GetEventType() == wxEVT_SCROLL_TOP)
2617 wevent.SetEventType( wxEVT_SCROLLWIN_TOP );
2618 else if (event.GetEventType() == wxEVT_SCROLL_BOTTOM)
2619 wevent.SetEventType( wxEVT_SCROLLWIN_BOTTOM );
2620 else if (event.GetEventType() == wxEVT_SCROLL_LINEUP)
2621 wevent.SetEventType( wxEVT_SCROLLWIN_LINEUP );
2622 else if (event.GetEventType() == wxEVT_SCROLL_LINEDOWN)
2623 wevent.SetEventType( wxEVT_SCROLLWIN_LINEDOWN );
2624 else if (event.GetEventType() == wxEVT_SCROLL_PAGEUP)
2625 wevent.SetEventType( wxEVT_SCROLLWIN_PAGEUP );
2626 else if (event.GetEventType() == wxEVT_SCROLL_PAGEDOWN)
2627 wevent.SetEventType( wxEVT_SCROLLWIN_PAGEDOWN );
2628 else if (event.GetEventType() == wxEVT_SCROLL_THUMBTRACK)
2629 wevent.SetEventType( wxEVT_SCROLLWIN_THUMBTRACK );
2630 else if (event.GetEventType() == wxEVT_SCROLL_THUMBRELEASE)
2631 wevent.SetEventType( wxEVT_SCROLLWIN_THUMBRELEASE );
6ed71b4f
VZ
2632
2633 GetEventHandler()->ProcessEvent(wevent);
7c74e7fe
SC
2634 }
2635}
2636
e9576ca5 2637// Get the window with the focus
0fe02759 2638wxWindowMac *wxWindowBase::DoFindFocus()
e9576ca5 2639{
f1d527c1
SC
2640 ControlRef control ;
2641 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
2642 return wxFindControlFromMacControl( control ) ;
519cb848
SC
2643}
2644
e15f0a5e 2645void wxWindowMac::OnSetFocus( wxFocusEvent& event )
7810c95b
SC
2646{
2647 // panel wants to track the window which was the last to have focus in it,
2648 // so we want to set ourselves as the window which last had focus
2649 //
dac390e9 2650 // notice that it's also important to do it upwards the tree because
7810c95b
SC
2651 // otherwise when the top level panel gets focus, it won't set it back to
2652 // us, but to some other sibling
6ed71b4f 2653
dac390e9 2654 // CS: don't know if this is still needed:
c1fb8167
SC
2655 //wxChildFocusEvent eventFocus(this);
2656 //(void)GetEventHandler()->ProcessEvent(eventFocus);
7810c95b 2657
5ca0d812
SC
2658 if ( MacGetTopLevelWindow() && m_peer->NeedsFocusRect() )
2659 {
dac390e9
DS
2660#if wxMAC_USE_CORE_GRAPHICS
2661 GetParent()->Refresh() ;
2662#else
788e118f 2663 wxMacWindowStateSaver sv( this ) ;
6449b3a8 2664 Rect rect ;
dac390e9 2665
6449b3a8 2666 m_peer->GetRect( &rect ) ;
8f39b6c4
SC
2667