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