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