call AddChild() when the control is already reallly created
[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 else if ( IsKindOf( CLASSINFO( wxSpinButton ) ) )
1724 {
1725 bestsize.bottom = 24 ;
1726 }
1727 else
1728 {
1729 // return wxWindowBase::DoGetBestSize() ;
1730 }
1731 }
1732
1733 bestWidth = bestsize.right - bestsize.left ;
1734 bestHeight = bestsize.bottom - bestsize.top ;
1735 if ( bestHeight < 10 )
1736 bestHeight = 13 ;
1737
1738 return wxSize(bestWidth, bestHeight);
1739 }
1740
1741
1742 // set the size of the window: if the dimensions are positive, just use them,
1743 // but if any of them is equal to -1, it means that we must find the value for
1744 // it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
1745 // which case -1 is a valid value for x and y)
1746 //
1747 // If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
1748 // the width/height to best suit our contents, otherwise we reuse the current
1749 // width/height
1750 void wxWindowMac::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1751 {
1752 // get the current size and position...
1753 int currentX, currentY;
1754 GetPosition(&currentX, &currentY);
1755
1756 int currentW,currentH;
1757 GetSize(&currentW, &currentH);
1758
1759 // ... and don't do anything (avoiding flicker) if it's already ok
1760 if ( x == currentX && y == currentY &&
1761 width == currentW && height == currentH && ( height != -1 && width != -1 ) )
1762 {
1763 // TODO REMOVE
1764 MacRepositionScrollBars() ; // we might have a real position shift
1765 return;
1766 }
1767
1768 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1769 x = currentX;
1770 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1771 y = currentY;
1772
1773 AdjustForParentClientOrigin(x, y, sizeFlags);
1774
1775 wxSize size(-1, -1);
1776 if ( width == -1 )
1777 {
1778 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
1779 {
1780 size = DoGetBestSize();
1781 width = size.x;
1782 }
1783 else
1784 {
1785 // just take the current one
1786 width = currentW;
1787 }
1788 }
1789
1790 if ( height == -1 )
1791 {
1792 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
1793 {
1794 if ( size.x == -1 )
1795 {
1796 size = DoGetBestSize();
1797 }
1798 //else: already called DoGetBestSize() above
1799
1800 height = size.y;
1801 }
1802 else
1803 {
1804 // just take the current one
1805 height = currentH;
1806 }
1807 }
1808
1809 DoMoveWindow(x, y, width, height);
1810
1811 }
1812
1813 wxPoint wxWindowMac::GetClientAreaOrigin() const
1814 {
1815 RgnHandle rgn = NewRgn() ;
1816 Rect content ;
1817 m_peer->GetRegion( kControlContentMetaPart , rgn ) ;
1818 GetRegionBounds( rgn , &content ) ;
1819 DisposeRgn( rgn ) ;
1820 #if !TARGET_API_MAC_OSX
1821 // if the content rgn is empty / not supported
1822 // don't attempt to correct the coordinates to wxWindow relative ones
1823 if (!::EmptyRect( &content ) )
1824 {
1825 Rect structure ;
1826 m_peer->GetRect( &structure ) ;
1827 OffsetRect( &content , -structure.left , -structure.top ) ;
1828 }
1829 #endif
1830
1831 return wxPoint( content.left + MacGetLeftBorderSize( ) , content.top + MacGetTopBorderSize( ) );
1832 }
1833
1834 void wxWindowMac::DoSetClientSize(int clientwidth, int clientheight)
1835 {
1836 if ( clientheight != -1 || clientheight != -1 )
1837 {
1838 int currentclientwidth , currentclientheight ;
1839 int currentwidth , currentheight ;
1840
1841 GetClientSize( &currentclientwidth , &currentclientheight ) ;
1842 GetSize( &currentwidth , &currentheight ) ;
1843
1844 DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth ,
1845 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
1846 }
1847 }
1848
1849 void wxWindowMac::SetTitle(const wxString& title)
1850 {
1851 m_label = wxStripMenuCodes(title) ;
1852
1853 if ( m_peer && m_peer->Ok() )
1854 {
1855 m_peer->SetTitle( m_label ) ;
1856 }
1857 Refresh() ;
1858 }
1859
1860 wxString wxWindowMac::GetTitle() const
1861 {
1862 return m_label ;
1863 }
1864
1865 bool wxWindowMac::Show(bool show)
1866 {
1867 if ( !wxWindowBase::Show(show) )
1868 return FALSE;
1869
1870 // TODO use visibilityChanged Carbon Event for OSX
1871 bool former = MacIsReallyShown() ;
1872
1873 m_peer->SetVisibility( show , true ) ;
1874 if ( former != MacIsReallyShown() )
1875 MacPropagateVisibilityChanged() ;
1876 return TRUE;
1877 }
1878
1879 bool wxWindowMac::Enable(bool enable)
1880 {
1881 wxASSERT( m_peer->Ok() ) ;
1882 if ( !wxWindowBase::Enable(enable) )
1883 return FALSE;
1884
1885 bool former = MacIsReallyEnabled() ;
1886 m_peer->Enable( enable ) ;
1887
1888 if ( former != MacIsReallyEnabled() )
1889 MacPropagateEnabledStateChanged() ;
1890 return TRUE;
1891 }
1892
1893 //
1894 // status change propagations (will be not necessary for OSX later )
1895 //
1896
1897 void wxWindowMac::MacPropagateVisibilityChanged()
1898 {
1899 #if !TARGET_API_MAC_OSX
1900 MacVisibilityChanged() ;
1901
1902 wxWindowListNode *node = GetChildren().GetFirst();
1903 while ( node )
1904 {
1905 wxWindowMac *child = node->GetData();
1906 if ( child->IsShown() )
1907 child->MacPropagateVisibilityChanged( ) ;
1908 node = node->GetNext();
1909 }
1910 #endif
1911 }
1912
1913 void wxWindowMac::MacPropagateEnabledStateChanged( )
1914 {
1915 #if !TARGET_API_MAC_OSX
1916 MacEnabledStateChanged() ;
1917
1918 wxWindowListNode *node = GetChildren().GetFirst();
1919 while ( node )
1920 {
1921 wxWindowMac *child = node->GetData();
1922 if ( child->IsEnabled() )
1923 child->MacPropagateEnabledStateChanged() ;
1924 node = node->GetNext();
1925 }
1926 #endif
1927 }
1928
1929 void wxWindowMac::MacPropagateHiliteChanged( )
1930 {
1931 #if !TARGET_API_MAC_OSX
1932 MacHiliteChanged() ;
1933
1934 wxWindowListNode *node = GetChildren().GetFirst();
1935 while ( node )
1936 {
1937 wxWindowMac *child = node->GetData();
1938 // if ( child->IsEnabled() )
1939 child->MacPropagateHiliteChanged() ;
1940 node = node->GetNext();
1941 }
1942 #endif
1943 }
1944
1945 //
1946 // status change notifications
1947 //
1948
1949 void wxWindowMac::MacVisibilityChanged()
1950 {
1951 }
1952
1953 void wxWindowMac::MacHiliteChanged()
1954 {
1955 }
1956
1957 void wxWindowMac::MacEnabledStateChanged()
1958 {
1959 }
1960
1961 //
1962 // status queries on the inherited window's state
1963 //
1964
1965 bool wxWindowMac::MacIsReallyShown()
1966 {
1967 // only under OSX the visibility of the TLW is taken into account
1968 #if TARGET_API_MAC_OSX
1969 if ( m_peer && m_peer->Ok() )
1970 return m_peer->IsVisible();
1971 #endif
1972 wxWindow* win = this ;
1973 while( win->IsShown() )
1974 {
1975 if ( win->IsTopLevel() )
1976 return true ;
1977
1978 win = win->GetParent() ;
1979 if ( win == NULL )
1980 return true ;
1981
1982 } ;
1983 return false ;
1984 }
1985
1986 bool wxWindowMac::MacIsReallyEnabled()
1987 {
1988 return m_peer->IsEnabled() ;
1989 }
1990
1991 bool wxWindowMac::MacIsReallyHilited()
1992 {
1993 return m_peer->IsActive();
1994 }
1995
1996 void wxWindowMac::MacFlashInvalidAreas()
1997 {
1998 #if TARGET_API_MAC_OSX
1999 HIViewFlashDirtyArea( (WindowRef) MacGetTopLevelWindowRef() ) ;
2000 #endif
2001 }
2002
2003 //
2004 //
2005 //
2006
2007 int wxWindowMac::GetCharHeight() const
2008 {
2009 wxClientDC dc ( (wxWindowMac*)this ) ;
2010 return dc.GetCharHeight() ;
2011 }
2012
2013 int wxWindowMac::GetCharWidth() const
2014 {
2015 wxClientDC dc ( (wxWindowMac*)this ) ;
2016 return dc.GetCharWidth() ;
2017 }
2018
2019 void wxWindowMac::GetTextExtent(const wxString& string, int *x, int *y,
2020 int *descent, int *externalLeading, const wxFont *theFont ) const
2021 {
2022 const wxFont *fontToUse = theFont;
2023 if ( !fontToUse )
2024 fontToUse = &m_font;
2025
2026 wxClientDC dc( (wxWindowMac*) this ) ;
2027 long lx,ly,ld,le ;
2028 dc.GetTextExtent( string , &lx , &ly , &ld, &le, (wxFont *)fontToUse ) ;
2029 if ( externalLeading )
2030 *externalLeading = le ;
2031 if ( descent )
2032 *descent = ld ;
2033 if ( x )
2034 *x = lx ;
2035 if ( y )
2036 *y = ly ;
2037 }
2038
2039 /*
2040 * Rect is given in client coordinates, for further reading, read wxTopLevelWindowMac::InvalidateRect
2041 * we always intersect with the entire window, not only with the client area
2042 */
2043
2044 void wxWindowMac::Refresh(bool eraseBack, const wxRect *rect)
2045 {
2046 if ( m_peer == NULL )
2047 return ;
2048
2049 #if TARGET_API_MAC_OSX
2050 if ( rect == NULL )
2051 m_peer->SetNeedsDisplay( true ) ;
2052 else
2053 {
2054 RgnHandle update = NewRgn() ;
2055 SetRectRgn( update , rect->x , rect->y , rect->x + rect->width , rect->y + rect->height ) ;
2056 SectRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , update , update ) ;
2057 wxPoint origin = GetClientAreaOrigin() ;
2058 OffsetRgn( update, origin.x , origin.y ) ;
2059 // right now this is wx' window coordinates, as our native peer does not have borders, this is
2060 // inset
2061 OffsetRgn( update , -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2062 m_peer->SetNeedsDisplay( true , update) ;
2063 DisposeRgn( update ) ;
2064 }
2065 #else
2066 /*
2067 RgnHandle updateRgn = NewRgn() ;
2068 if ( rect == NULL )
2069 {
2070 CopyRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , updateRgn ) ;
2071 }
2072 else
2073 {
2074 SetRectRgn( updateRgn , rect->x , rect->y , rect->x + rect->width , rect->y + rect->height ) ;
2075 SectRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , updateRgn , updateRgn ) ;
2076 }
2077 InvalWindowRgn( (WindowRef) MacGetTopLevelWindowRef() , updateRgn ) ;
2078 DisposeRgn(updateRgn) ;
2079 */
2080 if ( m_peer->IsVisible())
2081 {
2082 m_peer->SetVisibility( false , false ) ;
2083 m_peer->SetVisibility( true , true ) ;
2084 }
2085 /*
2086 if ( MacGetTopLevelWindow() == NULL )
2087 return ;
2088
2089 if ( !m_peer->IsVisible())
2090 return ;
2091
2092 wxPoint client = GetClientAreaOrigin();
2093 int x1 = -client.x;
2094 int y1 = -client.y;
2095 int x2 = m_width - client.x;
2096 int y2 = m_height - client.y;
2097
2098 if (IsKindOf( CLASSINFO(wxButton)))
2099 {
2100 // buttons have an "aura"
2101 y1 -= 5;
2102 x1 -= 5;
2103 y2 += 5;
2104 x2 += 5;
2105 }
2106
2107 Rect clientrect = { y1, x1, y2, x2 };
2108
2109 if ( rect )
2110 {
2111 Rect r = { rect->y , rect->x , rect->y + rect->height , rect->x + rect->width } ;
2112 SectRect( &clientrect , &r , &clientrect ) ;
2113 }
2114
2115 if ( !EmptyRect( &clientrect ) )
2116 {
2117 int top = 0 , left = 0 ;
2118
2119 MacClientToRootWindow( &left , &top ) ;
2120 OffsetRect( &clientrect , left , top ) ;
2121
2122 MacGetTopLevelWindow()->MacInvalidate( &clientrect , eraseBack ) ;
2123 }
2124 */
2125 #endif
2126 }
2127
2128 void wxWindowMac::Freeze()
2129 {
2130 #if TARGET_API_MAC_OSX
2131 if ( !m_frozenness++ )
2132 {
2133 if ( m_peer && m_peer->Ok() )
2134 m_peer->SetDrawingEnabled( false ) ;
2135 }
2136 #endif
2137 }
2138
2139
2140 void wxWindowMac::Thaw()
2141 {
2142 #if TARGET_API_MAC_OSX
2143 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
2144
2145 if ( !--m_frozenness )
2146 {
2147 if ( m_peer && m_peer->Ok() )
2148 {
2149 m_peer->SetDrawingEnabled( true ) ;
2150 m_peer->InvalidateWithChildren() ;
2151 }
2152 }
2153 #endif
2154 }
2155
2156 void wxWindowMac::MacRedrawControl()
2157 {
2158 /*
2159 if ( *m_peer && MacGetTopLevelWindowRef() && m_peer->IsVisible())
2160 {
2161 #if TARGET_API_MAC_CARBON
2162 Update() ;
2163 #else
2164 wxClientDC dc(this) ;
2165 wxMacPortSetter helper(&dc) ;
2166 wxMacWindowClipper clipper(this) ;
2167 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
2168 UMADrawControl( *m_peer ) ;
2169 #endif
2170 }
2171 */
2172 }
2173
2174 /* TODO
2175 void wxWindowMac::OnPaint(wxPaintEvent& event)
2176 {
2177 // why don't we skip that here ?
2178 }
2179 */
2180
2181 wxWindowMac *wxGetActiveWindow()
2182 {
2183 // actually this is a windows-only concept
2184 return NULL;
2185 }
2186
2187 // Coordinates relative to the window
2188 void wxWindowMac::WarpPointer (int x_pos, int y_pos)
2189 {
2190 // We really don't move the mouse programmatically under Mac.
2191 }
2192
2193 void wxWindowMac::OnEraseBackground(wxEraseEvent& event)
2194 {
2195 #if TARGET_API_MAC_OSX
2196 if ( m_macBackgroundBrush.Ok() == false || m_macBackgroundBrush.GetStyle() == wxTRANSPARENT )
2197 {
2198 event.Skip() ;
2199 }
2200 else
2201 #endif
2202 {
2203 event.GetDC()->Clear() ;
2204 }
2205 }
2206
2207 void wxWindowMac::OnNcPaint( wxNcPaintEvent& event )
2208 {
2209 event.Skip() ;
2210 }
2211
2212 int wxWindowMac::GetScrollPos(int orient) const
2213 {
2214 if ( orient == wxHORIZONTAL )
2215 {
2216 if ( m_hScrollBar )
2217 return m_hScrollBar->GetThumbPosition() ;
2218 }
2219 else
2220 {
2221 if ( m_vScrollBar )
2222 return m_vScrollBar->GetThumbPosition() ;
2223 }
2224 return 0;
2225 }
2226
2227 // This now returns the whole range, not just the number
2228 // of positions that we can scroll.
2229 int wxWindowMac::GetScrollRange(int orient) const
2230 {
2231 if ( orient == wxHORIZONTAL )
2232 {
2233 if ( m_hScrollBar )
2234 return m_hScrollBar->GetRange() ;
2235 }
2236 else
2237 {
2238 if ( m_vScrollBar )
2239 return m_vScrollBar->GetRange() ;
2240 }
2241 return 0;
2242 }
2243
2244 int wxWindowMac::GetScrollThumb(int orient) const
2245 {
2246 if ( orient == wxHORIZONTAL )
2247 {
2248 if ( m_hScrollBar )
2249 return m_hScrollBar->GetThumbSize() ;
2250 }
2251 else
2252 {
2253 if ( m_vScrollBar )
2254 return m_vScrollBar->GetThumbSize() ;
2255 }
2256 return 0;
2257 }
2258
2259 void wxWindowMac::SetScrollPos(int orient, int pos, bool refresh)
2260 {
2261 if ( orient == wxHORIZONTAL )
2262 {
2263 if ( m_hScrollBar )
2264 m_hScrollBar->SetThumbPosition( pos ) ;
2265 }
2266 else
2267 {
2268 if ( m_vScrollBar )
2269 m_vScrollBar->SetThumbPosition( pos ) ;
2270 }
2271 }
2272
2273 void wxWindowMac::MacPaintBorders( int left , int top )
2274 {
2275 if( IsTopLevel() )
2276 return ;
2277
2278 Rect rect ;
2279 m_peer->GetRect( &rect ) ;
2280 InsetRect( &rect, -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2281
2282 if ( !IsTopLevel() )
2283 {
2284 wxTopLevelWindowMac* top = MacGetTopLevelWindow();
2285 if (top)
2286 {
2287 wxPoint pt(0,0) ;
2288 wxMacControl::Convert( &pt , GetParent()->m_peer , top->m_peer ) ;
2289 rect.left += pt.x ;
2290 rect.right += pt.x ;
2291 rect.top += pt.y ;
2292 rect.bottom += pt.y ;
2293 }
2294 }
2295
2296 if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
2297 {
2298 Rect srect = rect ;
2299 SInt32 border = 0 ;
2300 GetThemeMetric( kThemeMetricEditTextFrameOutset , &border ) ;
2301 InsetRect( &srect , border , border );
2302 DrawThemeEditTextFrame(&srect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
2303 }
2304 else if (HasFlag(wxSIMPLE_BORDER))
2305 {
2306 Rect srect = rect ;
2307 SInt32 border = 0 ;
2308 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2309 InsetRect( &srect , border , border );
2310 DrawThemeListBoxFrame(&rect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
2311 }
2312 }
2313
2314 void wxWindowMac::RemoveChild( wxWindowBase *child )
2315 {
2316 if ( child == m_hScrollBar )
2317 m_hScrollBar = NULL ;
2318 if ( child == m_vScrollBar )
2319 m_vScrollBar = NULL ;
2320
2321 wxWindowBase::RemoveChild( child ) ;
2322 }
2323
2324 // New function that will replace some of the above.
2325 void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible,
2326 int range, bool refresh)
2327 {
2328 if ( orient == wxHORIZONTAL )
2329 {
2330 if ( m_hScrollBar )
2331 {
2332 if ( range == 0 || thumbVisible >= range )
2333 {
2334 if ( m_hScrollBar->IsShown() )
2335 m_hScrollBar->Show(false) ;
2336 }
2337 else
2338 {
2339 if ( !m_hScrollBar->IsShown() )
2340 m_hScrollBar->Show(true) ;
2341 m_hScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2342 }
2343 }
2344 }
2345 else
2346 {
2347 if ( m_vScrollBar )
2348 {
2349 if ( range == 0 || thumbVisible >= range )
2350 {
2351 if ( m_vScrollBar->IsShown() )
2352 m_vScrollBar->Show(false) ;
2353 }
2354 else
2355 {
2356 if ( !m_vScrollBar->IsShown() )
2357 m_vScrollBar->Show(true) ;
2358 m_vScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2359 }
2360 }
2361 }
2362 MacRepositionScrollBars() ;
2363 }
2364
2365 // Does a physical scroll
2366 void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect)
2367 {
2368 if( dx == 0 && dy ==0 )
2369 return ;
2370
2371
2372 {
2373
2374 int width , height ;
2375 GetClientSize( &width , &height ) ;
2376 #if TARGET_API_MAC_OSX
2377 // note there currently is a bug in OSX which makes inefficient refreshes in case an entire control
2378 // area is scrolled, this does not occur if width and height are 2 pixels less,
2379 // TODO write optimal workaround
2380 wxRect scrollrect( MacGetLeftBorderSize() , MacGetTopBorderSize() , width , height ) ;
2381 if ( rect )
2382 {
2383 scrollrect.Intersect( *rect ) ;
2384 }
2385 if ( m_peer->GetNeedsDisplay() )
2386 {
2387 // becuase HIViewScrollRect does not scroll the already invalidated area we have two options
2388 // either immediate redraw or full invalidate
2389 #if 1
2390 // is the better overall solution, as it does not slow down scrolling
2391 m_peer->SetNeedsDisplay( true ) ;
2392 #else
2393 // this would be the preferred version for fast drawing controls
2394 if( UMAGetSystemVersion() < 0x1030 )
2395 Update() ;
2396 else
2397 HIViewRender(m_peer->GetControlRef()) ;
2398 #endif
2399 }
2400 // as the native control might be not a 0/0 wx window coordinates, we have to offset
2401 scrollrect.Offset( -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2402 m_peer->ScrollRect( scrollrect , dx , dy ) ;
2403 #else
2404
2405 wxPoint pos;
2406 pos.x = pos.y = 0;
2407
2408 Rect scrollrect;
2409 RgnHandle updateRgn = NewRgn() ;
2410
2411 {
2412 wxClientDC dc(this) ;
2413 wxMacPortSetter helper(&dc) ;
2414
2415 m_peer->GetRect( &scrollrect ) ;
2416 scrollrect.top += MacGetTopBorderSize() ;
2417 scrollrect.left += MacGetLeftBorderSize() ;
2418 scrollrect.bottom = scrollrect.top + height ;
2419 scrollrect.right = scrollrect.left + width ;
2420
2421 if ( rect )
2422 {
2423 Rect r = { dc.YLOG2DEVMAC(rect->y) , dc.XLOG2DEVMAC(rect->x) , dc.YLOG2DEVMAC(rect->y + rect->height) ,
2424 dc.XLOG2DEVMAC(rect->x + rect->width) } ;
2425 SectRect( &scrollrect , &r , &scrollrect ) ;
2426 }
2427 ScrollRect( &scrollrect , dx , dy , updateRgn ) ;
2428 }
2429 // ScrollWindowRect( (WindowRef) MacGetTopLevelWindowRef() , &scrollrect , dx , dy , kScrollWindowInvalidate, updateRgn ) ;
2430 #endif
2431 }
2432
2433 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
2434 {
2435 wxWindowMac *child = node->GetData();
2436 if (child == m_vScrollBar) continue;
2437 if (child == m_hScrollBar) continue;
2438 if (child->IsTopLevel()) continue;
2439
2440 int x,y;
2441 child->GetPosition( &x, &y );
2442 int w,h;
2443 child->GetSize( &w, &h );
2444 if (rect)
2445 {
2446 wxRect rc(x,y,w,h);
2447 if (rect->Intersects(rc))
2448 child->SetSize( x+dx, y+dy, w, h );
2449 }
2450 else
2451 {
2452 child->SetSize( x+dx, y+dy, w, h );
2453 }
2454 }
2455 }
2456
2457 void wxWindowMac::MacOnScroll(wxScrollEvent &event )
2458 {
2459 if ( event.m_eventObject == m_vScrollBar || event.m_eventObject == m_hScrollBar )
2460 {
2461 wxScrollWinEvent wevent;
2462 wevent.SetPosition(event.GetPosition());
2463 wevent.SetOrientation(event.GetOrientation());
2464 wevent.m_eventObject = this;
2465
2466 if (event.m_eventType == wxEVT_SCROLL_TOP)
2467 wevent.m_eventType = wxEVT_SCROLLWIN_TOP;
2468 else if (event.m_eventType == wxEVT_SCROLL_BOTTOM)
2469 wevent.m_eventType = wxEVT_SCROLLWIN_BOTTOM;
2470 else if (event.m_eventType == wxEVT_SCROLL_LINEUP)
2471 wevent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
2472 else if (event.m_eventType == wxEVT_SCROLL_LINEDOWN)
2473 wevent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
2474 else if (event.m_eventType == wxEVT_SCROLL_PAGEUP)
2475 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEUP;
2476 else if (event.m_eventType == wxEVT_SCROLL_PAGEDOWN)
2477 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEDOWN;
2478 else if (event.m_eventType == wxEVT_SCROLL_THUMBTRACK)
2479 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBTRACK;
2480 else if (event.m_eventType == wxEVT_SCROLL_THUMBRELEASE)
2481 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBRELEASE;
2482
2483 GetEventHandler()->ProcessEvent(wevent);
2484 }
2485 }
2486
2487 // Get the window with the focus
2488 wxWindowMac *wxWindowBase::DoFindFocus()
2489 {
2490 ControlRef control ;
2491 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
2492 return wxFindControlFromMacControl( control ) ;
2493 }
2494
2495 void wxWindowMac::OnSetFocus(wxFocusEvent& event)
2496 {
2497 // panel wants to track the window which was the last to have focus in it,
2498 // so we want to set ourselves as the window which last had focus
2499 //
2500 // notice that it's also important to do it upwards the tree becaus
2501 // otherwise when the top level panel gets focus, it won't set it back to
2502 // us, but to some other sibling
2503
2504 // CS:don't know if this is still needed:
2505 //wxChildFocusEvent eventFocus(this);
2506 //(void)GetEventHandler()->ProcessEvent(eventFocus);
2507
2508 if ( MacGetTopLevelWindow() && m_peer->NeedsFocusRect() )
2509 {
2510 wxMacWindowStateSaver sv( this ) ;
2511
2512 int w , h ;
2513 int x , y ;
2514 x = y = 0 ;
2515 MacWindowToRootWindow( &x , &y ) ;
2516 GetSize( &w , &h ) ;
2517 Rect rect = {y , x , h + y , w + x } ;
2518
2519 if ( event.GetEventType() == wxEVT_SET_FOCUS )
2520 DrawThemeFocusRect( &rect , true ) ;
2521 else
2522 {
2523 DrawThemeFocusRect( &rect , false ) ;
2524
2525 // as this erases part of the frame we have to redraw borders
2526 // and because our z-ordering is not always correct (staticboxes)
2527 // we have to invalidate things, we cannot simple redraw
2528 RgnHandle updateInner = NewRgn() , updateOuter = NewRgn() ;
2529 RectRgn( updateInner , &rect ) ;
2530 InsetRect( &rect , -4 , -4 ) ;
2531 RectRgn( updateOuter , &rect ) ;
2532 DiffRgn( updateOuter , updateInner ,updateOuter ) ;
2533 wxPoint parent(0,0);
2534 GetParent()->MacWindowToRootWindow( &parent.x , &parent.y ) ;
2535 parent -= GetParent()->GetClientAreaOrigin() ;
2536 OffsetRgn( updateOuter , -parent.x , -parent.y ) ;
2537 GetParent()->m_peer->SetNeedsDisplay( true , updateOuter ) ;
2538 DisposeRgn(updateOuter) ;
2539 DisposeRgn(updateInner) ;
2540 }
2541 }
2542
2543 event.Skip();
2544 }
2545
2546 void wxWindowMac::OnInternalIdle()
2547 {
2548 // This calls the UI-update mechanism (querying windows for
2549 // menu/toolbar/control state information)
2550 if (wxUpdateUIEvent::CanUpdate(this))
2551 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2552 }
2553
2554 // Raise the window to the top of the Z order
2555 void wxWindowMac::Raise()
2556 {
2557 m_peer->SetZOrder( true , NULL ) ;
2558 }
2559
2560 // Lower the window to the bottom of the Z order
2561 void wxWindowMac::Lower()
2562 {
2563 m_peer->SetZOrder( false , NULL ) ;
2564 }
2565
2566
2567 // static wxWindow *gs_lastWhich = NULL;
2568
2569 bool wxWindowMac::MacSetupCursor( const wxPoint& pt)
2570 {
2571 // first trigger a set cursor event
2572
2573 wxPoint clientorigin = GetClientAreaOrigin() ;
2574 wxSize clientsize = GetClientSize() ;
2575 wxCursor cursor ;
2576 if ( wxRect2DInt( clientorigin.x , clientorigin.y , clientsize.x , clientsize.y ).Contains( wxPoint2DInt( pt ) ) )
2577 {
2578 wxSetCursorEvent event( pt.x , pt.y );
2579
2580 bool processedEvtSetCursor = GetEventHandler()->ProcessEvent(event);
2581 if ( processedEvtSetCursor && event.HasCursor() )
2582 {
2583 cursor = event.GetCursor() ;
2584 }
2585 else
2586 {
2587
2588 // the test for processedEvtSetCursor is here to prevent using m_cursor
2589 // if the user code caught EVT_SET_CURSOR() and returned nothing from
2590 // it - this is a way to say that our cursor shouldn't be used for this
2591 // point
2592 if ( !processedEvtSetCursor && m_cursor.Ok() )
2593 {
2594 cursor = m_cursor ;
2595 }
2596 if ( wxIsBusy() )
2597 {
2598 }
2599 else
2600 {
2601 if ( !GetParent() )
2602 cursor = *wxSTANDARD_CURSOR ;
2603 }
2604 }
2605 if ( cursor.Ok() )
2606 cursor.MacInstall() ;
2607 }
2608 return cursor.Ok() ;
2609 }
2610
2611 wxString wxWindowMac::MacGetToolTipString( wxPoint &pt )
2612 {
2613 if ( m_tooltip )
2614 {
2615 return m_tooltip->GetTip() ;
2616 }
2617 return wxEmptyString ;
2618 }
2619
2620 void wxWindowMac::Update()
2621 {
2622 #if TARGET_API_MAC_OSX
2623
2624 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
2625 WindowRef window = (WindowRef)MacGetTopLevelWindowRef() ;
2626
2627 // for composited windows this also triggers a redraw of all
2628 // invalid views in the window
2629 if( UMAGetSystemVersion() >= 0x1030 )
2630 HIWindowFlush(window) ;
2631 else
2632 #endif
2633 {
2634 // the only way to trigger the redrawing on earlier systems is to call
2635 // ReceiveNextEvent
2636
2637 EventRef currentEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
2638 UInt32 currentEventClass = 0 ;
2639 UInt32 currentEventKind = 0 ;
2640 if ( currentEvent != NULL )
2641 {
2642 currentEventClass = ::GetEventClass( currentEvent ) ;
2643 currentEventKind = ::GetEventKind( currentEvent ) ;
2644 }
2645 if ( currentEventClass != kEventClassMenu )
2646 {
2647 // when tracking a menu, strange redraw errors occur if we flush now, so leave..
2648
2649 EventRef theEvent;
2650 OSStatus status = noErr ;
2651 status = ReceiveNextEvent( 0 , NULL , kEventDurationNoWait , false , &theEvent ) ;
2652 }
2653 else
2654 m_peer->SetNeedsDisplay( true ) ;
2655 }
2656 #else
2657 ::Draw1Control( m_peer->GetControlRef() ) ;
2658 #endif
2659 }
2660
2661 wxTopLevelWindowMac* wxWindowMac::MacGetTopLevelWindow() const
2662 {
2663 wxTopLevelWindowMac* win = NULL ;
2664 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
2665 if ( window )
2666 {
2667 win = wxFindWinFromMacWindow( window ) ;
2668 }
2669 return win ;
2670 }
2671 wxRegion wxWindowMac::MacGetVisibleRegion( bool includeOuterStructures )
2672 {
2673 // includeOuterStructures is true if we try to draw somthing like a focus ring etc.
2674 // also a window dc uses this, in this case we only clip in the hierarchy for hard
2675 // borders like a scrollwindow, splitter etc otherwise we end up in a paranoia having
2676 // to add focus borders everywhere
2677
2678 Rect r ;
2679 RgnHandle visRgn = NewRgn() ;
2680 RgnHandle tempRgn = NewRgn() ;
2681 if ( m_peer->IsVisible())
2682 {
2683 m_peer->GetRect( &r ) ;
2684 r.left -= MacGetLeftBorderSize() ;
2685 r.top -= MacGetTopBorderSize() ;
2686 r.bottom += MacGetBottomBorderSize() ;
2687 r.right += MacGetRightBorderSize() ;
2688
2689 if (! MacGetTopLevelWindow()->MacUsesCompositing() )
2690 {
2691 MacRootWindowToWindow( &r.left , & r.top ) ;
2692 MacRootWindowToWindow( &r.right , & r.bottom ) ;
2693 }
2694 else
2695 {
2696 r.right -= r.left ;
2697 r.bottom -= r.top ;
2698 r.left = 0 ;
2699 r.top = 0 ;
2700 }
2701 if ( includeOuterStructures )
2702 InsetRect( &r , -4 , -4 ) ;
2703 RectRgn( visRgn , &r ) ;
2704
2705 if ( !IsTopLevel() )
2706 {
2707 wxWindow* child = this ;
2708 wxWindow* parent = child->GetParent() ;
2709 while( parent )
2710 {
2711 int x , y ;
2712 wxSize size ;
2713 // we have to find a better clipping algorithm here, in order not to clip things
2714 // positioned like status and toolbar
2715 if ( 1 /* parent->IsTopLevel() && child->IsKindOf( CLASSINFO( wxToolBar ) ) */ )
2716 {
2717 size = parent->GetSize() ;
2718 x = y = 0 ;
2719 }
2720 else
2721 {
2722 size = parent->GetClientSize() ;
2723 wxPoint origin = parent->GetClientAreaOrigin() ;
2724 x = origin.x ;
2725 y = origin.y ;
2726 }
2727 parent->MacWindowToRootWindow( &x, &y ) ;
2728 MacRootWindowToWindow( &x , &y ) ;
2729
2730 if ( !includeOuterStructures || (
2731 parent->MacClipChildren() ||
2732 ( parent->GetParent() && parent->GetParent()->MacClipGrandChildren() )
2733 ) )
2734 {
2735 SetRectRgn( tempRgn ,
2736 x + parent->MacGetLeftBorderSize() , y + parent->MacGetTopBorderSize() ,
2737 x + size.x - parent->MacGetRightBorderSize(),
2738 y + size.y - parent->MacGetBottomBorderSize()) ;
2739
2740 SectRgn( visRgn , tempRgn , visRgn ) ;
2741 }
2742 if ( parent->IsTopLevel() )
2743 break ;
2744 child = parent ;
2745 parent = child->GetParent() ;
2746 }
2747 }
2748 }
2749
2750 wxRegion vis = visRgn ;
2751 DisposeRgn( visRgn ) ;
2752 DisposeRgn( tempRgn ) ;
2753 return vis ;
2754 }
2755
2756 /*
2757 This function must not change the updatergn !
2758 */
2759 bool wxWindowMac::MacDoRedraw( WXHRGN updatergnr , long time )
2760 {
2761 RgnHandle updatergn = (RgnHandle) updatergnr ;
2762 bool handled = false ;
2763 Rect updatebounds ;
2764 GetRegionBounds( updatergn , &updatebounds ) ;
2765 // wxLogDebug("update for %s bounds %d , %d , %d , %d",typeid(*this).name() , updatebounds.left , updatebounds.top , updatebounds.right , updatebounds.bottom ) ;
2766 if ( !EmptyRgn(updatergn) )
2767 {
2768 RgnHandle newupdate = NewRgn() ;
2769 wxSize point = GetClientSize() ;
2770 wxPoint origin = GetClientAreaOrigin() ;
2771 SetRectRgn( newupdate , origin.x , origin.y , origin.x + point.x , origin.y+point.y ) ;
2772 SectRgn( newupdate , updatergn , newupdate ) ;
2773
2774 // first send an erase event to the entire update area
2775 {
2776 wxWindowDC dc(this);
2777 dc.SetClippingRegion(wxRegion(updatergn));
2778 wxEraseEvent eevent( GetId(), &dc );
2779 eevent.SetEventObject( this );
2780 GetEventHandler()->ProcessEvent( eevent );
2781 }
2782
2783 // calculate a client-origin version of the update rgn and set m_updateRegion to that
2784 OffsetRgn( newupdate , -origin.x , -origin.y ) ;
2785 m_updateRegion = newupdate ;
2786 DisposeRgn( newupdate ) ;
2787
2788 if ( !m_updateRegion.Empty() )
2789 {
2790 // paint the window itself
2791 wxPaintEvent event;
2792 event.m_timeStamp = time ;
2793 event.SetEventObject(this);
2794 handled = GetEventHandler()->ProcessEvent(event);
2795
2796 // we have to call the default built-in handler, as otherwise our frames will be drawn and immediately erased afterwards
2797 if ( !handled )
2798 {
2799 if ( wxTheApp->MacGetCurrentEvent() != NULL && wxTheApp->MacGetCurrentEventHandlerCallRef() != NULL )
2800 {
2801 CallNextEventHandler((EventHandlerCallRef)wxTheApp->MacGetCurrentEventHandlerCallRef() , (EventRef) wxTheApp->MacGetCurrentEvent() ) ;
2802 handled = true ;
2803 }
2804 }
2805
2806 }
2807
2808 // now we cannot rely on having its borders drawn by a window itself, as it does not
2809 // get the updateRgn wide enough to always do so, so we do it from the parent
2810 // this would also be the place to draw any custom backgrounds for native controls
2811 // in Composited windowing
2812 wxPoint clientOrigin = GetClientAreaOrigin() ;
2813
2814 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
2815 {
2816 wxWindowMac *child = node->GetData();
2817 if (child == m_vScrollBar) continue;
2818 if (child == m_hScrollBar) continue;
2819 if (child->IsTopLevel()) continue;
2820 if (!child->IsShown()) continue;
2821
2822 int x,y;
2823 child->GetPosition( &x, &y );
2824 int w,h;
2825 child->GetSize( &w, &h );
2826 Rect childRect = { y , x , y + h , x + w } ;
2827 OffsetRect( &childRect , clientOrigin.x , clientOrigin.y ) ;
2828 if ( child->MacGetTopBorderSize() )
2829 {
2830 if ( RectInRgn( &childRect , updatergn ) )
2831 {
2832 // paint custom borders
2833 wxNcPaintEvent eventNc( child->GetId() );
2834 eventNc.SetEventObject( child );
2835 if ( !child->GetEventHandler()->ProcessEvent( eventNc ) )
2836 {
2837 wxWindowDC dc(this) ;
2838 dc.SetClippingRegion(wxRegion(updatergn));
2839 wxMacPortSetter helper(&dc) ;
2840 child->MacPaintBorders( dc.m_macLocalOrigin.x + childRect.left , dc.m_macLocalOrigin.y + childRect.top) ;
2841 }
2842 }
2843 }
2844 if ( child->m_peer->NeedsFocusRect() && child->m_peer->HasFocus() )
2845 {
2846 wxWindowDC dc(this) ;
2847 dc.SetClippingRegion(wxRegion(updatergn));
2848 wxMacPortSetter helper(&dc) ;
2849 Rect r = childRect ;
2850 OffsetRect( &r , dc.m_macLocalOrigin.x , dc.m_macLocalOrigin.y ) ;
2851 DrawThemeFocusRect( &r , true ) ;
2852 }
2853 }
2854 }
2855 return handled ;
2856 }
2857
2858 void wxWindowMac::MacRedraw( WXHRGN updatergnr , long time, bool erase)
2859 {
2860 RgnHandle updatergn = (RgnHandle) updatergnr ;
2861 // updatergn is always already clipped to our boundaries
2862 // if we are in compositing mode then it is in relative to the upper left of the control
2863 // if we are in non-compositing, then it is relatvie to the uppder left of the content area
2864 // of the toplevel window
2865 // it is in window coordinates, not in client coordinates
2866
2867 // ownUpdateRgn is the area that this window has to repaint, it is in window coordinates
2868 RgnHandle ownUpdateRgn = NewRgn() ;
2869 CopyRgn( updatergn , ownUpdateRgn ) ;
2870
2871 if ( MacGetTopLevelWindow()->MacUsesCompositing() == false )
2872 {
2873 Rect bounds;
2874 m_peer->GetRectInWindowCoords( &bounds );
2875 RgnHandle controlRgn = NewRgn();
2876 RectRgn( controlRgn, &bounds );
2877 //KO: This sets the ownUpdateRgn to the area of this control that is inside
2878 // the window update region
2879 SectRgn( ownUpdateRgn, controlRgn, ownUpdateRgn );
2880 DisposeRgn( controlRgn );
2881
2882 //KO: convert ownUpdateRgn to local coordinates
2883 OffsetRgn( ownUpdateRgn, -bounds.left, -bounds.top );
2884 }
2885
2886 MacDoRedraw( ownUpdateRgn , time ) ;
2887 DisposeRgn( ownUpdateRgn ) ;
2888
2889 }
2890
2891 WXWindow wxWindowMac::MacGetTopLevelWindowRef() const
2892 {
2893 wxWindowMac *iter = (wxWindowMac*)this ;
2894
2895 while( iter )
2896 {
2897 if ( iter->IsTopLevel() )
2898 return ((wxTopLevelWindow*)iter)->MacGetWindowRef() ;
2899
2900 iter = iter->GetParent() ;
2901 }
2902 wxASSERT_MSG( 1 , wxT("No valid mac root window") ) ;
2903 return NULL ;
2904 }
2905
2906 void wxWindowMac::MacCreateScrollBars( long style )
2907 {
2908 wxASSERT_MSG( m_vScrollBar == NULL && m_hScrollBar == NULL , wxT("attempt to create window twice") ) ;
2909
2910 if ( style & ( wxVSCROLL | wxHSCROLL ) )
2911 {
2912 bool hasBoth = ( style & wxVSCROLL ) && ( style & wxHSCROLL ) ;
2913 int scrlsize = MAC_SCROLLBAR_SIZE ;
2914 wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL ;
2915 if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL || GetWindowVariant() == wxWINDOW_VARIANT_MINI )
2916 {
2917 scrlsize = MAC_SMALL_SCROLLBAR_SIZE ;
2918 variant = wxWINDOW_VARIANT_SMALL ;
2919 }
2920
2921 int adjust = hasBoth ? scrlsize - 1: 0 ;
2922 int width, height ;
2923 GetClientSize( &width , &height ) ;
2924
2925 wxPoint vPoint(width-scrlsize, 0) ;
2926 wxSize vSize(scrlsize, height - adjust) ;
2927 wxPoint hPoint(0 , height-scrlsize ) ;
2928 wxSize hSize( width - adjust, scrlsize) ;
2929
2930
2931 if ( style & wxVSCROLL )
2932 {
2933 m_vScrollBar = new wxScrollBar(this, wxWINDOW_VSCROLL, vPoint,
2934 vSize , wxVERTICAL);
2935 }
2936
2937 if ( style & wxHSCROLL )
2938 {
2939 m_hScrollBar = new wxScrollBar(this, wxWINDOW_HSCROLL, hPoint,
2940 hSize , wxHORIZONTAL);
2941 }
2942 }
2943
2944
2945 // because the create does not take into account the client area origin
2946 MacRepositionScrollBars() ; // we might have a real position shift
2947 }
2948
2949 void wxWindowMac::MacRepositionScrollBars()
2950 {
2951 if ( !m_hScrollBar && !m_vScrollBar )
2952 return ;
2953
2954 bool hasBoth = ( m_hScrollBar && m_hScrollBar->IsShown()) && ( m_vScrollBar && m_vScrollBar->IsShown()) ;
2955 int scrlsize = m_hScrollBar ? m_hScrollBar->GetSize().y : ( m_vScrollBar ? m_vScrollBar->GetSize().x : MAC_SCROLLBAR_SIZE ) ;
2956 int adjust = hasBoth ? scrlsize - 1 : 0 ;
2957
2958 // get real client area
2959
2960 int width ;
2961 int height ;
2962 GetSize( &width , &height ) ;
2963
2964 width -= MacGetLeftBorderSize() + MacGetRightBorderSize();
2965 height -= MacGetTopBorderSize() + MacGetBottomBorderSize();
2966
2967 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
2968 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
2969 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
2970 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
2971 /*
2972 int x = 0 ;
2973 int y = 0 ;
2974 int w ;
2975 int h ;
2976 GetSize( &w , &h ) ;
2977
2978 MacClientToRootWindow( &x , &y ) ;
2979 MacClientToRootWindow( &w , &h ) ;
2980
2981 wxWindowMac *iter = (wxWindowMac*)this ;
2982
2983 int totW = 10000 , totH = 10000;
2984 while( iter )
2985 {
2986 if ( iter->IsTopLevel() )
2987 {
2988 iter->GetSize( &totW , &totH ) ;
2989 break ;
2990 }
2991
2992 iter = iter->GetParent() ;
2993 }
2994
2995 if ( x == 0 )
2996 {
2997 hPoint.x = -1 ;
2998 hSize.x += 1 ;
2999 }
3000 if ( y == 0 )
3001 {
3002 vPoint.y = -1 ;
3003 vSize.y += 1 ;
3004 }
3005
3006 if ( w-x >= totW )
3007 {
3008 hSize.x += 1 ;
3009 vPoint.x += 1 ;
3010 }
3011
3012 if ( h-y >= totH )
3013 {
3014 vSize.y += 1 ;
3015 hPoint.y += 1 ;
3016 }
3017 */
3018 if ( m_vScrollBar )
3019 {
3020 m_vScrollBar->SetSize( vPoint.x , vPoint.y, vSize.x, vSize.y , wxSIZE_ALLOW_MINUS_ONE);
3021 }
3022 if ( m_hScrollBar )
3023 {
3024 m_hScrollBar->SetSize( hPoint.x , hPoint.y, hSize.x, hSize.y, wxSIZE_ALLOW_MINUS_ONE);
3025 }
3026 }
3027
3028 bool wxWindowMac::AcceptsFocus() const
3029 {
3030 return MacCanFocus() && wxWindowBase::AcceptsFocus();
3031 }
3032
3033 void wxWindowMac::MacSuperChangedPosition()
3034 {
3035 // only window-absolute structures have to be moved i.e. controls
3036
3037 wxWindowListNode *node = GetChildren().GetFirst();
3038 while ( node )
3039 {
3040 wxWindowMac *child = node->GetData();
3041 child->MacSuperChangedPosition() ;
3042 node = node->GetNext();
3043 }
3044 }
3045
3046 void wxWindowMac::MacTopLevelWindowChangedPosition()
3047 {
3048 // only screen-absolute structures have to be moved i.e. glcanvas
3049
3050 wxWindowListNode *node = GetChildren().GetFirst();
3051 while ( node )
3052 {
3053 wxWindowMac *child = node->GetData();
3054 child->MacTopLevelWindowChangedPosition() ;
3055 node = node->GetNext();
3056 }
3057 }
3058
3059 long wxWindowMac::MacGetLeftBorderSize( ) const
3060 {
3061 if( IsTopLevel() )
3062 return 0 ;
3063
3064 if (m_windowStyle & wxRAISED_BORDER || m_windowStyle & wxSUNKEN_BORDER )
3065 {
3066 SInt32 border = 3 ;
3067 return border ;
3068 }
3069 else if ( m_windowStyle &wxDOUBLE_BORDER)
3070 {
3071 SInt32 border = 3 ;
3072 return border ;
3073 }
3074 else if (m_windowStyle &wxSIMPLE_BORDER)
3075 {
3076 return 1 ;
3077 }
3078 return 0 ;
3079 }
3080
3081 long wxWindowMac::MacGetRightBorderSize( ) const
3082 {
3083 // they are all symmetric in mac themes
3084 return MacGetLeftBorderSize() ;
3085 }
3086
3087 long wxWindowMac::MacGetTopBorderSize( ) const
3088 {
3089 // they are all symmetric in mac themes
3090 return MacGetLeftBorderSize() ;
3091 }
3092
3093 long wxWindowMac::MacGetBottomBorderSize( ) const
3094 {
3095 // they are all symmetric in mac themes
3096 return MacGetLeftBorderSize() ;
3097 }
3098
3099 long wxWindowMac::MacRemoveBordersFromStyle( long style )
3100 {
3101 return style & ~wxBORDER_MASK ;
3102 }
3103
3104 // Find the wxWindowMac at the current mouse position, returning the mouse
3105 // position.
3106 wxWindowMac* wxFindWindowAtPointer(wxPoint& pt)
3107 {
3108 pt = wxGetMousePosition();
3109 wxWindowMac* found = wxFindWindowAtPoint(pt);
3110 return found;
3111 }
3112
3113 // Get the current mouse position.
3114 wxPoint wxGetMousePosition()
3115 {
3116 int x, y;
3117 wxGetMousePosition(& x, & y);
3118 return wxPoint(x, y);
3119 }
3120
3121 void wxWindowMac::OnMouseEvent( wxMouseEvent &event )
3122 {
3123 if ( event.GetEventType() == wxEVT_RIGHT_DOWN )
3124 {
3125 // copied from wxGTK : CS
3126 // generate a "context menu" event: this is similar to wxEVT_RIGHT_DOWN
3127 // except that:
3128 //
3129 // (a) it's a command event and so is propagated to the parent
3130 // (b) under MSW it can be generated from kbd too
3131 // (c) it uses screen coords (because of (a))
3132 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
3133 this->GetId(),
3134 this->ClientToScreen(event.GetPosition()));
3135 if ( ! GetEventHandler()->ProcessEvent(evtCtx) )
3136 event.Skip() ;
3137 }
3138 else
3139 {
3140 event.Skip() ;
3141 }
3142 }
3143
3144 void wxWindowMac::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED( mouseStillDown ) )
3145 {
3146 }
3147
3148 Rect wxMacGetBoundsForControl( wxWindow* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
3149 {
3150 int x ,y , w ,h ;
3151
3152 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin) ;
3153 Rect bounds = { y , x , y+h , x+w };
3154 return bounds ;
3155 }
3156
3157 wxInt32 wxWindowMac::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
3158 {
3159 return eventNotHandledErr ;
3160 }
3161
3162