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