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