Check for m_peer != NULL in Freeze and Thaw
[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 // wxRemoveMacControlAssociation( this ) ;
746 // If we delete an item, we should initialize the parent panel,
747 // because it could now be invalid.
748 wxWindow *parent = GetParent() ;
749 if ( parent )
750 {
751 if (parent->GetDefaultItem() == (wxButton*) this)
752 parent->SetDefaultItem(NULL);
753 }
754 if ( m_peer && m_peer->Ok() )
755 {
756 // in case the callback might be called during destruction
757 wxRemoveMacControlAssociation( this) ;
758 // we currently are not using this hook
759 // ::SetControlColorProc( *m_peer , NULL ) ;
760 m_peer->Dispose() ;
761 }
762
763 if ( g_MacLastWindow == this )
764 {
765 g_MacLastWindow = NULL ;
766 }
767
768 wxFrame* frame = wxDynamicCast( wxGetTopLevelParent( this ) , wxFrame ) ;
769 if ( frame )
770 {
771 if ( frame->GetLastFocus() == this )
772 frame->SetLastFocus( NULL ) ;
773 }
774
775 DestroyChildren();
776
777 // delete our drop target if we've got one
778 #if wxUSE_DRAG_AND_DROP
779 if ( m_dropTarget != NULL )
780 {
781 delete m_dropTarget;
782 m_dropTarget = NULL;
783 }
784 #endif // wxUSE_DRAG_AND_DROP
785 delete m_peer ;
786 }
787
788 WXWidget wxWindowMac::GetHandle() const
789 {
790 return (WXWidget) m_peer->GetControlRef() ;
791 }
792
793
794 void wxWindowMac::MacInstallEventHandler( WXWidget control )
795 {
796 wxAssociateControlWithMacControl( (ControlRef) control , this ) ;
797 InstallControlEventHandler( (ControlRef) control , GetwxMacWindowEventHandlerUPP(),
798 GetEventTypeCount(eventList), eventList, this,
799 (EventHandlerRef *)&m_macControlEventHandler);
800
801 }
802
803 // Constructor
804 bool wxWindowMac::Create(wxWindowMac *parent, wxWindowID id,
805 const wxPoint& pos,
806 const wxSize& size,
807 long style,
808 const wxString& name)
809 {
810 wxCHECK_MSG( parent, FALSE, wxT("can't create wxWindowMac without parent") );
811
812 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
813 return FALSE;
814
815 parent->AddChild(this);
816
817 m_windowVariant = parent->GetWindowVariant() ;
818
819 if ( m_macIsUserPane )
820 {
821 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
822
823 UInt32 features = 0
824 | kControlSupportsEmbedding
825 // | kControlSupportsLiveFeedback
826 // | kControlHasSpecialBackground
827 // | kControlSupportsCalcBestRect
828 // | kControlHandlesTracking
829 | kControlSupportsFocus
830 // | kControlWantsActivate
831 // | kControlWantsIdle
832 ;
833
834 m_peer = new wxMacControl() ;
835 ::CreateUserPaneControl( MAC_WXHWND(GetParent()->MacGetTopLevelWindowRef()) , &bounds, features , m_peer->GetControlRefAddr() );
836
837
838 MacPostControlCreate(pos,size) ;
839 #if !TARGET_API_MAC_OSX
840 m_peer->SetData<ControlUserPaneDrawUPP>(kControlEntireControl,kControlUserPaneDrawProcTag,&gControlUserPaneDrawUPP) ;
841 m_peer->SetData<ControlUserPaneHitTestUPP>(kControlEntireControl,kControlUserPaneHitTestProcTag,&gControlUserPaneHitTestUPP) ;
842 m_peer->SetData<ControlUserPaneTrackingUPP>(kControlEntireControl,kControlUserPaneTrackingProcTag,&gControlUserPaneTrackingUPP) ;
843 m_peer->SetData<ControlUserPaneIdleUPP>(kControlEntireControl,kControlUserPaneIdleProcTag,&gControlUserPaneIdleUPP) ;
844 m_peer->SetData<ControlUserPaneKeyDownUPP>(kControlEntireControl,kControlUserPaneKeyDownProcTag,&gControlUserPaneKeyDownUPP) ;
845 m_peer->SetData<ControlUserPaneActivateUPP>(kControlEntireControl,kControlUserPaneActivateProcTag,&gControlUserPaneActivateUPP) ;
846 m_peer->SetData<ControlUserPaneFocusUPP>(kControlEntireControl,kControlUserPaneFocusProcTag,&gControlUserPaneFocusUPP) ;
847 m_peer->SetData<ControlUserPaneBackgroundUPP>(kControlEntireControl,kControlUserPaneBackgroundProcTag,&gControlUserPaneBackgroundUPP) ;
848 #endif
849 }
850 #ifndef __WXUNIVERSAL__
851 // Don't give scrollbars to wxControls unless they ask for them
852 if ( (! IsKindOf(CLASSINFO(wxControl)) && ! IsKindOf(CLASSINFO(wxStatusBar))) ||
853 (IsKindOf(CLASSINFO(wxControl)) && ( style & wxHSCROLL || style & wxVSCROLL)))
854 {
855 MacCreateScrollBars( style ) ;
856 }
857 #endif
858
859 wxWindowCreateEvent event(this);
860 GetEventHandler()->AddPendingEvent(event);
861
862 return TRUE;
863 }
864
865 void wxWindowMac::MacPostControlCreate(const wxPoint& pos, const wxSize& size)
866 {
867 wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid mac control") ) ;
868
869 m_peer->SetReference( (long) this ) ;
870
871 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() );
872
873 ControlRef container = (ControlRef) GetParent()->GetHandle() ;
874 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
875 ::EmbedControl( m_peer->GetControlRef() , container ) ;
876
877 // adjust font, controlsize etc
878 DoSetWindowVariant( m_windowVariant ) ;
879
880 #if !TARGET_API_MAC_OSX
881 // eventually we can fix some clipping issues be reactivating this hook
882 //if ( m_macIsUserPane )
883 // SetControlColorProc( m_peer->GetControlRef() , wxMacSetupControlBackgroundUPP ) ;
884 #endif
885 m_peer->SetTitle( wxStripMenuCodes(m_label) ) ;
886
887 if (!m_macIsUserPane)
888 {
889 SetInitialBestSize(size);
890 }
891
892 SetCursor( *wxSTANDARD_CURSOR ) ;
893 }
894
895 void wxWindowMac::DoSetWindowVariant( wxWindowVariant variant )
896 {
897 // Don't assert, in case we set the window variant before
898 // the window is created
899 // wxASSERT( m_peer->Ok() ) ;
900
901 m_windowVariant = variant ;
902
903 if (m_peer == NULL || !m_peer->Ok())
904 return;
905
906 ControlSize size ;
907 ThemeFontID themeFont = kThemeSystemFont ;
908
909 // we will get that from the settings later
910 // and make this NORMAL later, but first
911 // we have a few calculations that we must fix
912
913 switch ( variant )
914 {
915 case wxWINDOW_VARIANT_NORMAL :
916 size = kControlSizeNormal;
917 themeFont = kThemeSystemFont ;
918 break ;
919 case wxWINDOW_VARIANT_SMALL :
920 size = kControlSizeSmall;
921 themeFont = kThemeSmallSystemFont ;
922 break ;
923 case wxWINDOW_VARIANT_MINI :
924 if (UMAGetSystemVersion() >= 0x1030 )
925 {
926 // not always defined in the headers
927 size = 3 ;
928 themeFont = 109 ;
929 }
930 else
931 {
932 size = kControlSizeSmall;
933 themeFont = kThemeSmallSystemFont ;
934 }
935 break ;
936 case wxWINDOW_VARIANT_LARGE :
937 size = kControlSizeLarge;
938 themeFont = kThemeSystemFont ;
939 break ;
940 default:
941 wxFAIL_MSG(_T("unexpected window variant"));
942 break ;
943 }
944 m_peer->SetData<ControlSize>(kControlEntireControl, kControlSizeTag,&size ) ;
945
946 wxFont font ;
947 font.MacCreateThemeFont( themeFont ) ;
948 SetFont( font ) ;
949 }
950
951 void wxWindowMac::MacUpdateControlFont()
952 {
953 m_peer->SetFont( GetFont() , GetForegroundColour() , GetWindowStyle() ) ;
954 Refresh() ;
955 }
956
957 bool wxWindowMac::SetFont(const wxFont& font)
958 {
959 bool retval = wxWindowBase::SetFont( font ) ;
960
961 MacUpdateControlFont() ;
962
963 return retval;
964 }
965
966 bool wxWindowMac::SetForegroundColour(const wxColour& col )
967 {
968 if ( !wxWindowBase::SetForegroundColour(col) )
969 return false ;
970
971 MacUpdateControlFont() ;
972
973 return true ;
974 }
975
976 bool wxWindowMac::SetBackgroundColour(const wxColour& col )
977 {
978 if ( !wxWindowBase::SetBackgroundColour(col) && m_hasBgCol )
979 return false ;
980
981 wxBrush brush ;
982 wxColour newCol(GetBackgroundColour());
983 if ( newCol == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE) )
984 {
985 brush.MacSetTheme( kThemeBrushDocumentWindowBackground ) ;
986 }
987 else if ( newCol == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE ) )
988 {
989 brush.MacSetTheme( kThemeBrushDialogBackgroundActive ) ;
990 }
991 else
992 {
993 brush.SetColour( newCol ) ;
994 }
995 MacSetBackgroundBrush( brush ) ;
996
997 MacUpdateControlFont() ;
998
999 return true ;
1000 }
1001
1002 void wxWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
1003 {
1004 m_macBackgroundBrush = brush ;
1005 m_peer->SetBackground( brush ) ;
1006 }
1007
1008 bool wxWindowMac::MacCanFocus() const
1009 {
1010 // there is currently no way to determine whether the window is running in full keyboard
1011 // access mode, therefore we cannot rely on these features, yet the only other way would be
1012 // to issue a SetKeyboardFocus event and verify after whether it succeeded, this would risk problems
1013 // in event handlers...
1014 UInt32 features = 0 ;
1015 m_peer->GetFeatures( & features ) ;
1016 return features & ( kControlSupportsFocus | kControlGetsFocusOnClick ) ;
1017 }
1018
1019
1020 void wxWindowMac::SetFocus()
1021 {
1022 if ( AcceptsFocus() )
1023 {
1024
1025 wxWindow* former = FindFocus() ;
1026 if ( former == this )
1027 return ;
1028
1029 OSStatus err = m_peer->SetFocus( kControlFocusNextPart ) ;
1030 // as we cannot rely on the control features to find out whether we are in full keyboard mode, we can only
1031 // leave in case of an error
1032 if ( err == errCouldntSetFocus )
1033 return ;
1034
1035 #if !TARGET_API_MAC_OSX
1036 // emulate carbon events when running under carbonlib where they are not natively available
1037 if ( former )
1038 {
1039 EventRef evRef = NULL ;
1040 verify_noerr( MacCreateEvent( NULL , kEventClassControl , kEventControlSetFocusPart , TicksToEventTime( TickCount() ) , kEventAttributeUserEvent ,
1041 &evRef ) );
1042
1043 wxMacCarbonEvent cEvent( evRef ) ;
1044 cEvent.SetParameter<ControlRef>( kEventParamDirectObject , (ControlRef) former->GetHandle() ) ;
1045 cEvent.SetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode , kControlFocusNoPart ) ;
1046
1047 wxMacWindowEventHandler( NULL , evRef , former ) ;
1048 ReleaseEvent(evRef) ;
1049 }
1050 // send new focus event
1051 {
1052 EventRef evRef = NULL ;
1053 verify_noerr( MacCreateEvent( NULL , kEventClassControl , kEventControlSetFocusPart , TicksToEventTime( TickCount() ) , kEventAttributeUserEvent ,
1054 &evRef ) );
1055
1056 wxMacCarbonEvent cEvent( evRef ) ;
1057 cEvent.SetParameter<ControlRef>( kEventParamDirectObject , (ControlRef) GetHandle() ) ;
1058 cEvent.SetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode , kControlFocusNextPart ) ;
1059
1060 wxMacWindowEventHandler( NULL , evRef , this ) ;
1061 ReleaseEvent(evRef) ;
1062 }
1063 #endif
1064 }
1065 }
1066
1067
1068 void wxWindowMac::DoCaptureMouse()
1069 {
1070 wxTheApp->s_captureWindow = this ;
1071 }
1072
1073 wxWindow* wxWindowBase::GetCapture()
1074 {
1075 return wxTheApp->s_captureWindow ;
1076 }
1077
1078 void wxWindowMac::DoReleaseMouse()
1079 {
1080 wxTheApp->s_captureWindow = NULL ;
1081 }
1082
1083 #if wxUSE_DRAG_AND_DROP
1084
1085 void wxWindowMac::SetDropTarget(wxDropTarget *pDropTarget)
1086 {
1087 if ( m_dropTarget != 0 ) {
1088 delete m_dropTarget;
1089 }
1090
1091 m_dropTarget = pDropTarget;
1092 if ( m_dropTarget != 0 )
1093 {
1094 // TODO
1095 }
1096 }
1097
1098 #endif
1099
1100 // Old style file-manager drag&drop
1101 void wxWindowMac::DragAcceptFiles(bool accept)
1102 {
1103 // TODO
1104 }
1105
1106 // Returns the size of the native control. In the case of the toplevel window
1107 // this is the content area root control
1108
1109 void wxWindowMac::MacGetPositionAndSizeFromControl(int& x, int& y,
1110 int& w, int& h) const
1111 {
1112 Rect bounds ;
1113 m_peer->GetRect( &bounds ) ;
1114
1115
1116 x = bounds.left ;
1117 y = bounds.top ;
1118 w = bounds.right - bounds.left ;
1119 h = bounds.bottom - bounds.top ;
1120 }
1121
1122 // From a wx position / size calculate the appropriate size of the native control
1123
1124 bool wxWindowMac::MacGetBoundsForControl(const wxPoint& pos,
1125 const wxSize& size,
1126 int& x, int& y,
1127 int& w, int& h , bool adjustOrigin ) const
1128 {
1129 // the desired size, minus the border pixels gives the correct size of the control
1130
1131 x = (int)pos.x;
1132 y = (int)pos.y;
1133 // todo the default calls may be used as soon as PostCreateControl Is moved here
1134 w = size.x ; // WidthDefault( size.x );
1135 h = size.y ; // HeightDefault( size.y ) ;
1136 #if !TARGET_API_MAC_OSX
1137 GetParent()->MacWindowToRootWindow( &x , &y ) ;
1138 #endif
1139
1140 x += MacGetLeftBorderSize() ;
1141 y += MacGetTopBorderSize() ;
1142 w -= MacGetLeftBorderSize() + MacGetRightBorderSize() ;
1143 h -= MacGetTopBorderSize() + MacGetBottomBorderSize() ;
1144
1145 if ( adjustOrigin )
1146 AdjustForParentClientOrigin( x , y ) ;
1147 #if TARGET_API_MAC_OSX
1148 // this is in window relative coordinate, as this parent may have a border, its physical position is offset by this border
1149 if ( ! GetParent()->IsTopLevel() )
1150 {
1151 x -= GetParent()->MacGetLeftBorderSize() ;
1152 y -= GetParent()->MacGetTopBorderSize() ;
1153 }
1154 #endif
1155 return true ;
1156 }
1157
1158 // Get window size (not client size)
1159 void wxWindowMac::DoGetSize(int *x, int *y) const
1160 {
1161 // take the size of the control and add the borders that have to be drawn outside
1162 int x1 , y1 , w1 , h1 ;
1163
1164 MacGetPositionAndSizeFromControl( x1 , y1, w1 ,h1 ) ;
1165
1166 w1 += MacGetLeftBorderSize() + MacGetRightBorderSize() ;
1167 h1 += MacGetTopBorderSize() + MacGetBottomBorderSize() ;
1168
1169 if(x) *x = w1 ;
1170 if(y) *y = h1 ;
1171 }
1172
1173 // get the position of the bounds of this window in client coordinates of its parent
1174 void wxWindowMac::DoGetPosition(int *x, int *y) const
1175 {
1176 int x1 , y1 , w1 ,h1 ;
1177 MacGetPositionAndSizeFromControl( x1 , y1, w1 ,h1 ) ;
1178 x1 -= MacGetLeftBorderSize() ;
1179 y1 -= MacGetTopBorderSize() ;
1180 // to non-client
1181 #if !TARGET_API_MAC_OSX
1182 if ( !GetParent()->IsTopLevel() )
1183 {
1184 Rect bounds ;
1185 GetControlBounds( (ControlRef) GetParent()->GetHandle() , &bounds ) ;
1186 x1 -= bounds.left ;
1187 y1 -= bounds.top ;
1188 }
1189 #endif
1190 if ( !IsTopLevel() )
1191 {
1192 wxWindow *parent = GetParent();
1193 if ( parent )
1194 {
1195 // we must first adjust it to be in window coordinates of the parent, as otherwise it gets lost by the clientareaorigin fix
1196 x1 += parent->MacGetLeftBorderSize() ;
1197 y1 += parent->MacGetTopBorderSize() ;
1198 // and now to client coordinates
1199 wxPoint pt(parent->GetClientAreaOrigin());
1200 x1 -= pt.x ;
1201 y1 -= pt.y ;
1202 }
1203 }
1204 if(x) *x = x1 ;
1205 if(y) *y = y1 ;
1206 }
1207
1208 void wxWindowMac::DoScreenToClient(int *x, int *y) const
1209 {
1210 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
1211
1212 wxCHECK_RET( window , wxT("TopLevel Window Missing") ) ;
1213
1214 {
1215 Point localwhere = {0,0} ;
1216
1217 if(x) localwhere.h = * x ;
1218 if(y) localwhere.v = * y ;
1219
1220 QDGlobalToLocalPoint( GetWindowPort( window ) , &localwhere ) ;
1221 if(x) *x = localwhere.h ;
1222 if(y) *y = localwhere.v ;
1223
1224 }
1225 MacRootWindowToWindow( x , y ) ;
1226
1227 wxPoint origin = GetClientAreaOrigin() ;
1228 if(x) *x -= origin.x ;
1229 if(y) *y -= origin.y ;
1230 }
1231
1232 void wxWindowMac::DoClientToScreen(int *x, int *y) const
1233 {
1234 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
1235 wxCHECK_RET( window , wxT("TopLevel Window Missing") ) ;
1236
1237 wxPoint origin = GetClientAreaOrigin() ;
1238 if(x) *x += origin.x ;
1239 if(y) *y += origin.y ;
1240
1241 MacWindowToRootWindow( x , y ) ;
1242
1243 {
1244 Point localwhere = { 0,0 };
1245 if(x) localwhere.h = * x ;
1246 if(y) localwhere.v = * y ;
1247 QDLocalToGlobalPoint( GetWindowPort( window ) , &localwhere ) ;
1248 if(x) *x = localwhere.h ;
1249 if(y) *y = localwhere.v ;
1250 }
1251 }
1252
1253 void wxWindowMac::MacClientToRootWindow( int *x , int *y ) const
1254 {
1255 wxPoint origin = GetClientAreaOrigin() ;
1256 if(x) *x += origin.x ;
1257 if(y) *y += origin.y ;
1258
1259 MacWindowToRootWindow( x , y ) ;
1260 }
1261
1262 void wxWindowMac::MacRootWindowToClient( int *x , int *y ) const
1263 {
1264 MacRootWindowToWindow( x , y ) ;
1265
1266 wxPoint origin = GetClientAreaOrigin() ;
1267 if(x) *x -= origin.x ;
1268 if(y) *y -= origin.y ;
1269 }
1270
1271 void wxWindowMac::MacWindowToRootWindow( int *x , int *y ) const
1272 {
1273 #if TARGET_API_MAC_OSX
1274 wxPoint pt ;
1275 if ( x ) pt.x = *x ;
1276 if ( y ) pt.y = *y ;
1277
1278 if ( !IsTopLevel() )
1279 {
1280 wxTopLevelWindowMac* top = MacGetTopLevelWindow();
1281 if (top)
1282 {
1283 pt.x -= MacGetLeftBorderSize() ;
1284 pt.y -= MacGetTopBorderSize() ;
1285 wxMacControl::Convert( &pt , m_peer , top->m_peer ) ;
1286 }
1287 }
1288
1289 if ( x ) *x = (int) pt.x ;
1290 if ( y ) *y = (int) pt.y ;
1291 #else
1292 if ( !IsTopLevel() )
1293 {
1294 Rect bounds ;
1295 m_peer->GetRect( &bounds ) ;
1296 if(x) *x += bounds.left - MacGetLeftBorderSize() ;
1297 if(y) *y += bounds.top - MacGetTopBorderSize() ;
1298 }
1299 #endif
1300 }
1301
1302 void wxWindowMac::MacWindowToRootWindow( short *x , short *y ) const
1303 {
1304 int x1 , y1 ;
1305 if ( x ) x1 = *x ;
1306 if ( y ) y1 = *y ;
1307 MacWindowToRootWindow( &x1 , &y1 ) ;
1308 if ( x ) *x = x1 ;
1309 if ( y ) *y = y1 ;
1310 }
1311
1312 void wxWindowMac::MacRootWindowToWindow( int *x , int *y ) const
1313 {
1314 #if TARGET_API_MAC_OSX
1315 wxPoint pt ;
1316 if ( x ) pt.x = *x ;
1317 if ( y ) pt.y = *y ;
1318
1319 if ( !IsTopLevel() )
1320 {
1321 wxMacControl::Convert( &pt , MacGetTopLevelWindow()->m_peer , m_peer ) ;
1322 pt.x += MacGetLeftBorderSize() ;
1323 pt.y += MacGetTopBorderSize() ;
1324 }
1325
1326 if ( x ) *x = (int) pt.x ;
1327 if ( y ) *y = (int) pt.y ;
1328 #else
1329 if ( !IsTopLevel() )
1330 {
1331 Rect bounds ;
1332 m_peer->GetRect( &bounds ) ;
1333 if(x) *x -= bounds.left + MacGetLeftBorderSize() ;
1334 if(y) *y -= bounds.top + MacGetTopBorderSize() ;
1335 }
1336 #endif
1337 }
1338
1339 void wxWindowMac::MacRootWindowToWindow( short *x , short *y ) const
1340 {
1341 int x1 , y1 ;
1342 if ( x ) x1 = *x ;
1343 if ( y ) y1 = *y ;
1344 MacRootWindowToWindow( &x1 , &y1 ) ;
1345 if ( x ) *x = x1 ;
1346 if ( y ) *y = y1 ;
1347 }
1348
1349 void wxWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1350 {
1351 RgnHandle rgn = NewRgn() ;
1352 Rect content ;
1353 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
1354 {
1355 GetRegionBounds( rgn , &content ) ;
1356 DisposeRgn( rgn ) ;
1357 }
1358 else
1359 {
1360 m_peer->GetRect( &content ) ;
1361 }
1362 Rect structure ;
1363 m_peer->GetRect( &structure ) ;
1364 #if !TARGET_API_MAC_OSX
1365 OffsetRect( &content , -structure.left , -structure.top ) ;
1366 #endif
1367 left = content.left - structure.left ;
1368 top = content.top - structure.top ;
1369 right = structure.right - content.right ;
1370 bottom = structure.bottom - content.bottom ;
1371 }
1372
1373 wxSize wxWindowMac::DoGetSizeFromClientSize( const wxSize & size ) const
1374 {
1375 wxSize sizeTotal = size;
1376
1377 RgnHandle rgn = NewRgn() ;
1378
1379 Rect content ;
1380
1381 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
1382 {
1383 GetRegionBounds( rgn , &content ) ;
1384 DisposeRgn( rgn ) ;
1385 }
1386 else
1387 {
1388 m_peer->GetRect( &content ) ;
1389 }
1390 Rect structure ;
1391 m_peer->GetRect( &structure ) ;
1392 #if !TARGET_API_MAC_OSX
1393 OffsetRect( &content , -structure.left , -structure.top ) ;
1394 #endif
1395
1396 sizeTotal.x += (structure.right - structure.left) - (content.right - content.left) ;
1397 sizeTotal.y += (structure.bottom - structure.top) - (content.bottom - content.top ) ;
1398
1399 sizeTotal.x += MacGetLeftBorderSize( ) + MacGetRightBorderSize( ) ;
1400 sizeTotal.y += MacGetTopBorderSize( ) + MacGetBottomBorderSize( ) ;
1401
1402 return sizeTotal;
1403 }
1404
1405
1406 // Get size *available for subwindows* i.e. excluding menu bar etc.
1407 void wxWindowMac::DoGetClientSize(int *x, int *y) const
1408 {
1409 int ww, hh;
1410
1411 RgnHandle rgn = NewRgn() ;
1412 Rect content ;
1413 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
1414 {
1415 GetRegionBounds( rgn , &content ) ;
1416 DisposeRgn( rgn ) ;
1417 }
1418 else
1419 {
1420 m_peer->GetRect( &content ) ;
1421 }
1422 #if !TARGET_API_MAC_OSX
1423 Rect structure ;
1424 m_peer->GetRect( &structure ) ;
1425 OffsetRect( &content , -structure.left , -structure.top ) ;
1426 #endif
1427 ww = content.right - content.left ;
1428 hh = content.bottom - content.top ;
1429 /*
1430 ww -= MacGetLeftBorderSize( ) + MacGetRightBorderSize( ) ;
1431 hh -= MacGetTopBorderSize( ) + MacGetBottomBorderSize( );
1432 */
1433 /*
1434 if ( (m_vScrollBar && m_vScrollBar->IsShown()) || (m_hScrollBar && m_hScrollBar->IsShown()) )
1435 {
1436 int x1 = 0 ;
1437 int y1 = 0 ;
1438 int w ;
1439 int h ;
1440 GetSize( &w , &h ) ;
1441
1442 MacClientToRootWindow( &x1 , &y1 ) ;
1443 MacClientToRootWindow( &w , &h ) ;
1444
1445 wxWindowMac *iter = (wxWindowMac*)this ;
1446
1447 int totW = 10000 , totH = 10000;
1448 while( iter )
1449 {
1450 if ( iter->IsTopLevel() )
1451 {
1452 iter->GetSize( &totW , &totH ) ;
1453 break ;
1454 }
1455
1456 iter = iter->GetParent() ;
1457 }
1458
1459 if (m_hScrollBar && m_hScrollBar->IsShown() )
1460 {
1461 hh -= m_hScrollBar->GetSize().y ; // MAC_SCROLLBAR_SIZE ;
1462 if ( h-y1 >= totH )
1463 {
1464 hh += 1 ;
1465 }
1466 }
1467 if (m_vScrollBar && m_vScrollBar->IsShown() )
1468 {
1469 ww -= m_vScrollBar->GetSize().x ; // MAC_SCROLLBAR_SIZE;
1470 if ( w-x1 >= totW )
1471 {
1472 ww += 1 ;
1473 }
1474 }
1475 }
1476 */
1477 if (m_hScrollBar && m_hScrollBar->IsShown() )
1478 {
1479 hh -= m_hScrollBar->GetSize().y ; // MAC_SCROLLBAR_SIZE ;
1480 }
1481 if (m_vScrollBar && m_vScrollBar->IsShown() )
1482 {
1483 ww -= m_vScrollBar->GetSize().x ; // MAC_SCROLLBAR_SIZE;
1484 }
1485 if(x) *x = ww;
1486 if(y) *y = hh;
1487
1488 }
1489
1490 bool wxWindowMac::SetCursor(const wxCursor& cursor)
1491 {
1492 if (m_cursor == cursor)
1493 return FALSE;
1494
1495 if (wxNullCursor == cursor)
1496 {
1497 if ( ! wxWindowBase::SetCursor( *wxSTANDARD_CURSOR ) )
1498 return FALSE ;
1499 }
1500 else
1501 {
1502 if ( ! wxWindowBase::SetCursor( cursor ) )
1503 return FALSE ;
1504 }
1505
1506 wxASSERT_MSG( m_cursor.Ok(),
1507 wxT("cursor must be valid after call to the base version"));
1508
1509
1510 wxWindowMac *mouseWin = 0 ;
1511 {
1512 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
1513 CGrafPtr savePort ;
1514 Boolean swapped = QDSwapPort( GetWindowPort( window ) , &savePort ) ;
1515
1516 // TODO If we ever get a GetCurrentEvent.. replacement for the mouse
1517 // position, use it...
1518
1519 Point pt ;
1520 GetMouse( &pt ) ;
1521 ControlPartCode part ;
1522 ControlRef control ;
1523 control = wxMacFindControlUnderMouse( pt , window , &part ) ;
1524 if ( control )
1525 mouseWin = wxFindControlFromMacControl( control ) ;
1526
1527 if ( swapped )
1528 QDSwapPort( savePort , NULL ) ;
1529 }
1530
1531 if ( mouseWin == this && !wxIsBusy() )
1532 {
1533 m_cursor.MacInstall() ;
1534 }
1535
1536 return TRUE ;
1537 }
1538
1539 #if wxUSE_MENUS
1540 bool wxWindowMac::DoPopupMenu(wxMenu *menu, int x, int y)
1541 {
1542 menu->SetInvokingWindow(this);
1543 menu->UpdateUI();
1544
1545 if ( x == -1 && y == -1 )
1546 {
1547 wxPoint mouse = wxGetMousePosition();
1548 x = mouse.x; y = mouse.y;
1549 }
1550 else
1551 {
1552 ClientToScreen( &x , &y ) ;
1553 }
1554
1555 menu->MacBeforeDisplay( true ) ;
1556 long menuResult = ::PopUpMenuSelect((MenuHandle) menu->GetHMenu() ,y,x, 0) ;
1557 if ( HiWord(menuResult) != 0 )
1558 {
1559 MenuCommand id ;
1560 GetMenuItemCommandID( GetMenuHandle(HiWord(menuResult)) , LoWord(menuResult) , &id ) ;
1561 wxMenuItem* item = NULL ;
1562 wxMenu* realmenu ;
1563 item = menu->FindItem(id, &realmenu) ;
1564 if (item->IsCheckable())
1565 {
1566 item->Check( !item->IsChecked() ) ;
1567 }
1568 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
1569 }
1570 menu->MacAfterDisplay( true ) ;
1571
1572 menu->SetInvokingWindow(NULL);
1573
1574 return TRUE;
1575 }
1576 #endif
1577
1578 // ----------------------------------------------------------------------------
1579 // tooltips
1580 // ----------------------------------------------------------------------------
1581
1582 #if wxUSE_TOOLTIPS
1583
1584 void wxWindowMac::DoSetToolTip(wxToolTip *tooltip)
1585 {
1586 wxWindowBase::DoSetToolTip(tooltip);
1587
1588 if ( m_tooltip )
1589 m_tooltip->SetWindow(this);
1590 }
1591
1592 #endif // wxUSE_TOOLTIPS
1593
1594 void wxWindowMac::DoMoveWindow(int x, int y, int width, int height)
1595 {
1596 // this is never called for a toplevel window, so we know we have a parent
1597 int former_x , former_y , former_w, former_h ;
1598
1599 // Get true coordinates of former position
1600 DoGetPosition( &former_x , &former_y ) ;
1601 DoGetSize( &former_w , &former_h ) ;
1602
1603 wxWindow *parent = GetParent();
1604 if ( parent )
1605 {
1606 wxPoint pt(parent->GetClientAreaOrigin());
1607 former_x += pt.x ;
1608 former_y += pt.y ;
1609 }
1610
1611 int actualWidth = width;
1612 int actualHeight = height;
1613 int actualX = x;
1614 int actualY = y;
1615
1616 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
1617 actualWidth = m_minWidth;
1618 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
1619 actualHeight = m_minHeight;
1620 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
1621 actualWidth = m_maxWidth;
1622 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
1623 actualHeight = m_maxHeight;
1624
1625 bool doMove = false ;
1626 bool doResize = false ;
1627
1628 if ( actualX != former_x || actualY != former_y )
1629 {
1630 doMove = true ;
1631 }
1632 if ( actualWidth != former_w || actualHeight != former_h )
1633 {
1634 doResize = true ;
1635 }
1636
1637 if ( doMove || doResize )
1638 {
1639 // we don't adjust twice for the origin
1640 Rect r = wxMacGetBoundsForControl(this , wxPoint( actualX,actualY), wxSize( actualWidth, actualHeight ) , false ) ;
1641 bool vis = m_peer->IsVisible();
1642
1643 int outerBorder = MacGetLeftBorderSize() ;
1644 if ( m_peer->NeedsFocusRect() && m_peer->HasFocus() )
1645 outerBorder += 4 ;
1646
1647 if ( vis && ( outerBorder > 0 ) )
1648 {
1649 // as the borders are drawn on the parent we have to properly invalidate all these areas
1650 RgnHandle updateInner = NewRgn() , updateOuter = NewRgn() , updateTotal = NewRgn() ;
1651
1652 Rect rect ;
1653
1654 m_peer->GetRect( &rect ) ;
1655 RectRgn( updateInner , &rect ) ;
1656 InsetRect( &rect , -outerBorder , -outerBorder ) ;
1657 RectRgn( updateOuter , &rect ) ;
1658 DiffRgn( updateOuter , updateInner ,updateOuter ) ;
1659 wxPoint parent(0,0);
1660 GetParent()->MacWindowToRootWindow( &parent.x , &parent.y ) ;
1661 parent -= GetParent()->GetClientAreaOrigin() ;
1662 OffsetRgn( updateOuter , -parent.x , -parent.y ) ;
1663 CopyRgn( updateOuter , updateTotal ) ;
1664
1665 rect = r ;
1666 RectRgn( updateInner , &rect ) ;
1667 InsetRect( &rect , -outerBorder , -outerBorder ) ;
1668 RectRgn( updateOuter , &rect ) ;
1669 DiffRgn( updateOuter , updateInner ,updateOuter ) ;
1670
1671 OffsetRgn( updateOuter , -parent.x , -parent.y ) ;
1672 UnionRgn( updateOuter , updateTotal , updateTotal ) ;
1673
1674 GetParent()->m_peer->SetNeedsDisplay( true , updateTotal ) ;
1675 DisposeRgn(updateOuter) ;
1676 DisposeRgn(updateInner) ;
1677 DisposeRgn(updateTotal) ;
1678 }
1679
1680 // the HIViewSetFrame call itself should invalidate the areas, but when testing with the UnicodeTextCtrl it does not !
1681 if ( vis )
1682 m_peer->SetVisibility( false , true ) ;
1683
1684 m_peer->SetRect( &r ) ;
1685 if ( vis )
1686 m_peer->SetVisibility( true , true ) ;
1687
1688 MacRepositionScrollBars() ;
1689 if ( doMove )
1690 {
1691 wxPoint point(actualX,actualY);
1692 wxMoveEvent event(point, m_windowId);
1693 event.SetEventObject(this);
1694 GetEventHandler()->ProcessEvent(event) ;
1695 }
1696 if ( doResize )
1697 {
1698 MacRepositionScrollBars() ;
1699 wxSize size(actualWidth, actualHeight);
1700 wxSizeEvent event(size, m_windowId);
1701 event.SetEventObject(this);
1702 GetEventHandler()->ProcessEvent(event);
1703 }
1704 }
1705
1706 }
1707
1708 wxSize wxWindowMac::DoGetBestSize() const
1709 {
1710 if ( m_macIsUserPane || IsTopLevel() )
1711 return wxWindowBase::DoGetBestSize() ;
1712
1713 Rect bestsize = { 0 , 0 , 0 , 0 } ;
1714 int bestWidth, bestHeight ;
1715 m_peer->GetBestRect( &bestsize ) ;
1716
1717 if ( EmptyRect( &bestsize ) )
1718 {
1719 bestsize.left = bestsize.top = 0 ;
1720 bestsize.right = 16 ;
1721 bestsize.bottom = 16 ;
1722 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
1723 {
1724 bestsize.bottom = 16 ;
1725 }
1726 else if ( IsKindOf( CLASSINFO( wxSpinButton ) ) )
1727 {
1728 bestsize.bottom = 24 ;
1729 }
1730 else
1731 {
1732 // return wxWindowBase::DoGetBestSize() ;
1733 }
1734 }
1735
1736 bestWidth = bestsize.right - bestsize.left ;
1737 bestHeight = bestsize.bottom - bestsize.top ;
1738 if ( bestHeight < 10 )
1739 bestHeight = 13 ;
1740
1741 return wxSize(bestWidth, bestHeight);
1742 }
1743
1744
1745 // set the size of the window: if the dimensions are positive, just use them,
1746 // but if any of them is equal to -1, it means that we must find the value for
1747 // it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
1748 // which case -1 is a valid value for x and y)
1749 //
1750 // If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
1751 // the width/height to best suit our contents, otherwise we reuse the current
1752 // width/height
1753 void wxWindowMac::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1754 {
1755 // get the current size and position...
1756 int currentX, currentY;
1757 GetPosition(&currentX, &currentY);
1758
1759 int currentW,currentH;
1760 GetSize(&currentW, &currentH);
1761
1762 // ... and don't do anything (avoiding flicker) if it's already ok
1763 if ( x == currentX && y == currentY &&
1764 width == currentW && height == currentH && ( height != -1 && width != -1 ) )
1765 {
1766 // TODO REMOVE
1767 MacRepositionScrollBars() ; // we might have a real position shift
1768 return;
1769 }
1770
1771 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1772 x = currentX;
1773 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1774 y = currentY;
1775
1776 AdjustForParentClientOrigin(x, y, sizeFlags);
1777
1778 wxSize size(-1, -1);
1779 if ( width == -1 )
1780 {
1781 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
1782 {
1783 size = DoGetBestSize();
1784 width = size.x;
1785 }
1786 else
1787 {
1788 // just take the current one
1789 width = currentW;
1790 }
1791 }
1792
1793 if ( height == -1 )
1794 {
1795 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
1796 {
1797 if ( size.x == -1 )
1798 {
1799 size = DoGetBestSize();
1800 }
1801 //else: already called DoGetBestSize() above
1802
1803 height = size.y;
1804 }
1805 else
1806 {
1807 // just take the current one
1808 height = currentH;
1809 }
1810 }
1811
1812 DoMoveWindow(x, y, width, height);
1813
1814 }
1815
1816 wxPoint wxWindowMac::GetClientAreaOrigin() const
1817 {
1818 RgnHandle rgn = NewRgn() ;
1819 Rect content ;
1820 m_peer->GetRegion( kControlContentMetaPart , rgn ) ;
1821 GetRegionBounds( rgn , &content ) ;
1822 DisposeRgn( rgn ) ;
1823 #if !TARGET_API_MAC_OSX
1824 // if the content rgn is empty / not supported
1825 // don't attempt to correct the coordinates to wxWindow relative ones
1826 if (!::EmptyRect( &content ) )
1827 {
1828 Rect structure ;
1829 m_peer->GetRect( &structure ) ;
1830 OffsetRect( &content , -structure.left , -structure.top ) ;
1831 }
1832 #endif
1833
1834 return wxPoint( content.left + MacGetLeftBorderSize( ) , content.top + MacGetTopBorderSize( ) );
1835 }
1836
1837 void wxWindowMac::DoSetClientSize(int clientwidth, int clientheight)
1838 {
1839 if ( clientheight != -1 || clientheight != -1 )
1840 {
1841 int currentclientwidth , currentclientheight ;
1842 int currentwidth , currentheight ;
1843
1844 GetClientSize( &currentclientwidth , &currentclientheight ) ;
1845 GetSize( &currentwidth , &currentheight ) ;
1846
1847 DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth ,
1848 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
1849 }
1850 }
1851
1852 void wxWindowMac::SetTitle(const wxString& title)
1853 {
1854 m_label = wxStripMenuCodes(title) ;
1855
1856 if ( m_peer && m_peer->Ok() )
1857 {
1858 m_peer->SetTitle( m_label ) ;
1859 }
1860 Refresh() ;
1861 }
1862
1863 wxString wxWindowMac::GetTitle() const
1864 {
1865 return m_label ;
1866 }
1867
1868 bool wxWindowMac::Show(bool show)
1869 {
1870 if ( !wxWindowBase::Show(show) )
1871 return FALSE;
1872
1873 // TODO use visibilityChanged Carbon Event for OSX
1874 bool former = MacIsReallyShown() ;
1875
1876 m_peer->SetVisibility( show , true ) ;
1877 if ( former != MacIsReallyShown() )
1878 MacPropagateVisibilityChanged() ;
1879 return TRUE;
1880 }
1881
1882 bool wxWindowMac::Enable(bool enable)
1883 {
1884 wxASSERT( m_peer->Ok() ) ;
1885 if ( !wxWindowBase::Enable(enable) )
1886 return FALSE;
1887
1888 bool former = MacIsReallyEnabled() ;
1889 m_peer->Enable( enable ) ;
1890
1891 if ( former != MacIsReallyEnabled() )
1892 MacPropagateEnabledStateChanged() ;
1893 return TRUE;
1894 }
1895
1896 //
1897 // status change propagations (will be not necessary for OSX later )
1898 //
1899
1900 void wxWindowMac::MacPropagateVisibilityChanged()
1901 {
1902 #if !TARGET_API_MAC_OSX
1903 MacVisibilityChanged() ;
1904
1905 wxWindowListNode *node = GetChildren().GetFirst();
1906 while ( node )
1907 {
1908 wxWindowMac *child = node->GetData();
1909 if ( child->IsShown() )
1910 child->MacPropagateVisibilityChanged( ) ;
1911 node = node->GetNext();
1912 }
1913 #endif
1914 }
1915
1916 void wxWindowMac::MacPropagateEnabledStateChanged( )
1917 {
1918 #if !TARGET_API_MAC_OSX
1919 MacEnabledStateChanged() ;
1920
1921 wxWindowListNode *node = GetChildren().GetFirst();
1922 while ( node )
1923 {
1924 wxWindowMac *child = node->GetData();
1925 if ( child->IsEnabled() )
1926 child->MacPropagateEnabledStateChanged() ;
1927 node = node->GetNext();
1928 }
1929 #endif
1930 }
1931
1932 void wxWindowMac::MacPropagateHiliteChanged( )
1933 {
1934 #if !TARGET_API_MAC_OSX
1935 MacHiliteChanged() ;
1936
1937 wxWindowListNode *node = GetChildren().GetFirst();
1938 while ( node )
1939 {
1940 wxWindowMac *child = node->GetData();
1941 // if ( child->IsEnabled() )
1942 child->MacPropagateHiliteChanged() ;
1943 node = node->GetNext();
1944 }
1945 #endif
1946 }
1947
1948 //
1949 // status change notifications
1950 //
1951
1952 void wxWindowMac::MacVisibilityChanged()
1953 {
1954 }
1955
1956 void wxWindowMac::MacHiliteChanged()
1957 {
1958 }
1959
1960 void wxWindowMac::MacEnabledStateChanged()
1961 {
1962 }
1963
1964 //
1965 // status queries on the inherited window's state
1966 //
1967
1968 bool wxWindowMac::MacIsReallyShown()
1969 {
1970 // only under OSX the visibility of the TLW is taken into account
1971 #if TARGET_API_MAC_OSX
1972 if ( m_peer && m_peer->Ok() )
1973 return m_peer->IsVisible();
1974 #endif
1975 wxWindow* win = this ;
1976 while( win->IsShown() )
1977 {
1978 if ( win->IsTopLevel() )
1979 return true ;
1980
1981 win = win->GetParent() ;
1982 if ( win == NULL )
1983 return true ;
1984
1985 } ;
1986 return false ;
1987 }
1988
1989 bool wxWindowMac::MacIsReallyEnabled()
1990 {
1991 return m_peer->IsEnabled() ;
1992 }
1993
1994 bool wxWindowMac::MacIsReallyHilited()
1995 {
1996 return m_peer->IsActive();
1997 }
1998
1999 void wxWindowMac::MacFlashInvalidAreas()
2000 {
2001 #if TARGET_API_MAC_OSX
2002 HIViewFlashDirtyArea( (WindowRef) MacGetTopLevelWindowRef() ) ;
2003 #endif
2004 }
2005
2006 //
2007 //
2008 //
2009
2010 int wxWindowMac::GetCharHeight() const
2011 {
2012 wxClientDC dc ( (wxWindowMac*)this ) ;
2013 return dc.GetCharHeight() ;
2014 }
2015
2016 int wxWindowMac::GetCharWidth() const
2017 {
2018 wxClientDC dc ( (wxWindowMac*)this ) ;
2019 return dc.GetCharWidth() ;
2020 }
2021
2022 void wxWindowMac::GetTextExtent(const wxString& string, int *x, int *y,
2023 int *descent, int *externalLeading, const wxFont *theFont ) const
2024 {
2025 const wxFont *fontToUse = theFont;
2026 if ( !fontToUse )
2027 fontToUse = &m_font;
2028
2029 wxClientDC dc( (wxWindowMac*) this ) ;
2030 long lx,ly,ld,le ;
2031 dc.GetTextExtent( string , &lx , &ly , &ld, &le, (wxFont *)fontToUse ) ;
2032 if ( externalLeading )
2033 *externalLeading = le ;
2034 if ( descent )
2035 *descent = ld ;
2036 if ( x )
2037 *x = lx ;
2038 if ( y )
2039 *y = ly ;
2040 }
2041
2042 /*
2043 * Rect is given in client coordinates, for further reading, read wxTopLevelWindowMac::InvalidateRect
2044 * we always intersect with the entire window, not only with the client area
2045 */
2046
2047 void wxWindowMac::Refresh(bool eraseBack, const wxRect *rect)
2048 {
2049 if ( m_peer == NULL )
2050 return ;
2051
2052 #if TARGET_API_MAC_OSX
2053 if ( rect == NULL )
2054 m_peer->SetNeedsDisplay( true ) ;
2055 else
2056 {
2057 RgnHandle update = NewRgn() ;
2058 SetRectRgn( update , rect->x , rect->y , rect->x + rect->width , rect->y + rect->height ) ;
2059 SectRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , update , update ) ;
2060 wxPoint origin = GetClientAreaOrigin() ;
2061 OffsetRgn( update, origin.x , origin.y ) ;
2062 // right now this is wx' window coordinates, as our native peer does not have borders, this is
2063 // inset
2064 OffsetRgn( update , -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2065 m_peer->SetNeedsDisplay( true , update) ;
2066 DisposeRgn( update ) ;
2067 }
2068 #else
2069 /*
2070 RgnHandle updateRgn = NewRgn() ;
2071 if ( rect == NULL )
2072 {
2073 CopyRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , updateRgn ) ;
2074 }
2075 else
2076 {
2077 SetRectRgn( updateRgn , rect->x , rect->y , rect->x + rect->width , rect->y + rect->height ) ;
2078 SectRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , updateRgn , updateRgn ) ;
2079 }
2080 InvalWindowRgn( (WindowRef) MacGetTopLevelWindowRef() , updateRgn ) ;
2081 DisposeRgn(updateRgn) ;
2082 */
2083 if ( m_peer->IsVisible())
2084 {
2085 m_peer->SetVisibility( false , false ) ;
2086 m_peer->SetVisibility( true , true ) ;
2087 }
2088 /*
2089 if ( MacGetTopLevelWindow() == NULL )
2090 return ;
2091
2092 if ( !m_peer->IsVisible())
2093 return ;
2094
2095 wxPoint client = GetClientAreaOrigin();
2096 int x1 = -client.x;
2097 int y1 = -client.y;
2098 int x2 = m_width - client.x;
2099 int y2 = m_height - client.y;
2100
2101 if (IsKindOf( CLASSINFO(wxButton)))
2102 {
2103 // buttons have an "aura"
2104 y1 -= 5;
2105 x1 -= 5;
2106 y2 += 5;
2107 x2 += 5;
2108 }
2109
2110 Rect clientrect = { y1, x1, y2, x2 };
2111
2112 if ( rect )
2113 {
2114 Rect r = { rect->y , rect->x , rect->y + rect->height , rect->x + rect->width } ;
2115 SectRect( &clientrect , &r , &clientrect ) ;
2116 }
2117
2118 if ( !EmptyRect( &clientrect ) )
2119 {
2120 int top = 0 , left = 0 ;
2121
2122 MacClientToRootWindow( &left , &top ) ;
2123 OffsetRect( &clientrect , left , top ) ;
2124
2125 MacGetTopLevelWindow()->MacInvalidate( &clientrect , eraseBack ) ;
2126 }
2127 */
2128 #endif
2129 }
2130
2131 void wxWindowMac::Freeze()
2132 {
2133 #if TARGET_API_MAC_OSX
2134 if ( !m_frozenness++ )
2135 {
2136 if ( m_peer && m_peer->Ok() )
2137 m_peer->SetDrawingEnabled( false ) ;
2138 }
2139 #endif
2140 }
2141
2142
2143 void wxWindowMac::Thaw()
2144 {
2145 #if TARGET_API_MAC_OSX
2146 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
2147
2148 if ( !--m_frozenness )
2149 {
2150 if ( m_peer && m_peer->Ok() )
2151 {
2152 m_peer->SetDrawingEnabled( true ) ;
2153 m_peer->InvalidateWithChildren() ;
2154 }
2155 }
2156 #endif
2157 }
2158
2159 void wxWindowMac::MacRedrawControl()
2160 {
2161 /*
2162 if ( *m_peer && MacGetTopLevelWindowRef() && m_peer->IsVisible())
2163 {
2164 #if TARGET_API_MAC_CARBON
2165 Update() ;
2166 #else
2167 wxClientDC dc(this) ;
2168 wxMacPortSetter helper(&dc) ;
2169 wxMacWindowClipper clipper(this) ;
2170 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
2171 UMADrawControl( *m_peer ) ;
2172 #endif
2173 }
2174 */
2175 }
2176
2177 /* TODO
2178 void wxWindowMac::OnPaint(wxPaintEvent& event)
2179 {
2180 // why don't we skip that here ?
2181 }
2182 */
2183
2184 wxWindowMac *wxGetActiveWindow()
2185 {
2186 // actually this is a windows-only concept
2187 return NULL;
2188 }
2189
2190 // Coordinates relative to the window
2191 void wxWindowMac::WarpPointer (int x_pos, int y_pos)
2192 {
2193 // We really don't move the mouse programmatically under Mac.
2194 }
2195
2196 void wxWindowMac::OnEraseBackground(wxEraseEvent& event)
2197 {
2198 #if TARGET_API_MAC_OSX
2199 if ( m_macBackgroundBrush.Ok() == false || m_macBackgroundBrush.GetStyle() == wxTRANSPARENT )
2200 {
2201 event.Skip() ;
2202 }
2203 else
2204 #endif
2205 {
2206 event.GetDC()->Clear() ;
2207 }
2208 }
2209
2210 void wxWindowMac::OnNcPaint( wxNcPaintEvent& event )
2211 {
2212 event.Skip() ;
2213 }
2214
2215 int wxWindowMac::GetScrollPos(int orient) const
2216 {
2217 if ( orient == wxHORIZONTAL )
2218 {
2219 if ( m_hScrollBar )
2220 return m_hScrollBar->GetThumbPosition() ;
2221 }
2222 else
2223 {
2224 if ( m_vScrollBar )
2225 return m_vScrollBar->GetThumbPosition() ;
2226 }
2227 return 0;
2228 }
2229
2230 // This now returns the whole range, not just the number
2231 // of positions that we can scroll.
2232 int wxWindowMac::GetScrollRange(int orient) const
2233 {
2234 if ( orient == wxHORIZONTAL )
2235 {
2236 if ( m_hScrollBar )
2237 return m_hScrollBar->GetRange() ;
2238 }
2239 else
2240 {
2241 if ( m_vScrollBar )
2242 return m_vScrollBar->GetRange() ;
2243 }
2244 return 0;
2245 }
2246
2247 int wxWindowMac::GetScrollThumb(int orient) const
2248 {
2249 if ( orient == wxHORIZONTAL )
2250 {
2251 if ( m_hScrollBar )
2252 return m_hScrollBar->GetThumbSize() ;
2253 }
2254 else
2255 {
2256 if ( m_vScrollBar )
2257 return m_vScrollBar->GetThumbSize() ;
2258 }
2259 return 0;
2260 }
2261
2262 void wxWindowMac::SetScrollPos(int orient, int pos, bool refresh)
2263 {
2264 if ( orient == wxHORIZONTAL )
2265 {
2266 if ( m_hScrollBar )
2267 m_hScrollBar->SetThumbPosition( pos ) ;
2268 }
2269 else
2270 {
2271 if ( m_vScrollBar )
2272 m_vScrollBar->SetThumbPosition( pos ) ;
2273 }
2274 }
2275
2276 void wxWindowMac::MacPaintBorders( int left , int top )
2277 {
2278 if( IsTopLevel() )
2279 return ;
2280
2281 Rect rect ;
2282 m_peer->GetRect( &rect ) ;
2283 InsetRect( &rect, -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2284
2285 if ( !IsTopLevel() )
2286 {
2287 wxTopLevelWindowMac* top = MacGetTopLevelWindow();
2288 if (top)
2289 {
2290 wxPoint pt(0,0) ;
2291 wxMacControl::Convert( &pt , GetParent()->m_peer , top->m_peer ) ;
2292 rect.left += pt.x ;
2293 rect.right += pt.x ;
2294 rect.top += pt.y ;
2295 rect.bottom += pt.y ;
2296 }
2297 }
2298
2299 if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
2300 {
2301 Rect srect = rect ;
2302 SInt32 border = 0 ;
2303 GetThemeMetric( kThemeMetricEditTextFrameOutset , &border ) ;
2304 InsetRect( &srect , border , border );
2305 DrawThemeEditTextFrame(&srect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
2306 }
2307 else if (HasFlag(wxSIMPLE_BORDER))
2308 {
2309 Rect srect = rect ;
2310 SInt32 border = 0 ;
2311 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2312 InsetRect( &srect , border , border );
2313 DrawThemeListBoxFrame(&rect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
2314 }
2315 }
2316
2317 void wxWindowMac::RemoveChild( wxWindowBase *child )
2318 {
2319 if ( child == m_hScrollBar )
2320 m_hScrollBar = NULL ;
2321 if ( child == m_vScrollBar )
2322 m_vScrollBar = NULL ;
2323
2324 wxWindowBase::RemoveChild( child ) ;
2325 }
2326
2327 // New function that will replace some of the above.
2328 void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible,
2329 int range, bool refresh)
2330 {
2331 if ( orient == wxHORIZONTAL )
2332 {
2333 if ( m_hScrollBar )
2334 {
2335 if ( range == 0 || thumbVisible >= range )
2336 {
2337 if ( m_hScrollBar->IsShown() )
2338 m_hScrollBar->Show(false) ;
2339 }
2340 else
2341 {
2342 if ( !m_hScrollBar->IsShown() )
2343 m_hScrollBar->Show(true) ;
2344 m_hScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2345 }
2346 }
2347 }
2348 else
2349 {
2350 if ( m_vScrollBar )
2351 {
2352 if ( range == 0 || thumbVisible >= range )
2353 {
2354 if ( m_vScrollBar->IsShown() )
2355 m_vScrollBar->Show(false) ;
2356 }
2357 else
2358 {
2359 if ( !m_vScrollBar->IsShown() )
2360 m_vScrollBar->Show(true) ;
2361 m_vScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2362 }
2363 }
2364 }
2365 MacRepositionScrollBars() ;
2366 }
2367
2368 // Does a physical scroll
2369 void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect)
2370 {
2371 if( dx == 0 && dy ==0 )
2372 return ;
2373
2374
2375 {
2376
2377 int width , height ;
2378 GetClientSize( &width , &height ) ;
2379 #if TARGET_API_MAC_OSX
2380 // note there currently is a bug in OSX which makes inefficient refreshes in case an entire control
2381 // area is scrolled, this does not occur if width and height are 2 pixels less,
2382 // TODO write optimal workaround
2383 wxRect scrollrect( MacGetLeftBorderSize() , MacGetTopBorderSize() , width , height ) ;
2384 if ( rect )
2385 {
2386 scrollrect.Intersect( *rect ) ;
2387 }
2388 if ( m_peer->GetNeedsDisplay() )
2389 {
2390 // becuase HIViewScrollRect does not scroll the already invalidated area we have two options
2391 // either immediate redraw or full invalidate
2392 #if 1
2393 // is the better overall solution, as it does not slow down scrolling
2394 m_peer->SetNeedsDisplay( true ) ;
2395 #else
2396 // this would be the preferred version for fast drawing controls
2397 if( UMAGetSystemVersion() < 0x1030 )
2398 Update() ;
2399 else
2400 HIViewRender(m_peer->GetControlRef()) ;
2401 #endif
2402 }
2403 // as the native control might be not a 0/0 wx window coordinates, we have to offset
2404 scrollrect.Offset( -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2405 m_peer->ScrollRect( scrollrect , dx , dy ) ;
2406 #else
2407
2408 wxPoint pos;
2409 pos.x = pos.y = 0;
2410
2411 Rect scrollrect;
2412 RgnHandle updateRgn = NewRgn() ;
2413
2414 {
2415 wxClientDC dc(this) ;
2416 wxMacPortSetter helper(&dc) ;
2417
2418 m_peer->GetRect( &scrollrect ) ;
2419 scrollrect.top += MacGetTopBorderSize() ;
2420 scrollrect.left += MacGetLeftBorderSize() ;
2421 scrollrect.bottom = scrollrect.top + height ;
2422 scrollrect.right = scrollrect.left + width ;
2423
2424 if ( rect )
2425 {
2426 Rect r = { dc.YLOG2DEVMAC(rect->y) , dc.XLOG2DEVMAC(rect->x) , dc.YLOG2DEVMAC(rect->y + rect->height) ,
2427 dc.XLOG2DEVMAC(rect->x + rect->width) } ;
2428 SectRect( &scrollrect , &r , &scrollrect ) ;
2429 }
2430 ScrollRect( &scrollrect , dx , dy , updateRgn ) ;
2431 }
2432 // ScrollWindowRect( (WindowRef) MacGetTopLevelWindowRef() , &scrollrect , dx , dy , kScrollWindowInvalidate, updateRgn ) ;
2433 #endif
2434 }
2435
2436 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
2437 {
2438 wxWindowMac *child = node->GetData();
2439 if (child == m_vScrollBar) continue;
2440 if (child == m_hScrollBar) continue;
2441 if (child->IsTopLevel()) continue;
2442
2443 int x,y;
2444 child->GetPosition( &x, &y );
2445 int w,h;
2446 child->GetSize( &w, &h );
2447 if (rect)
2448 {
2449 wxRect rc(x,y,w,h);
2450 if (rect->Intersects(rc))
2451 child->SetSize( x+dx, y+dy, w, h );
2452 }
2453 else
2454 {
2455 child->SetSize( x+dx, y+dy, w, h );
2456 }
2457 }
2458 }
2459
2460 void wxWindowMac::MacOnScroll(wxScrollEvent &event )
2461 {
2462 if ( event.m_eventObject == m_vScrollBar || event.m_eventObject == m_hScrollBar )
2463 {
2464 wxScrollWinEvent wevent;
2465 wevent.SetPosition(event.GetPosition());
2466 wevent.SetOrientation(event.GetOrientation());
2467 wevent.m_eventObject = this;
2468
2469 if (event.m_eventType == wxEVT_SCROLL_TOP)
2470 wevent.m_eventType = wxEVT_SCROLLWIN_TOP;
2471 else if (event.m_eventType == wxEVT_SCROLL_BOTTOM)
2472 wevent.m_eventType = wxEVT_SCROLLWIN_BOTTOM;
2473 else if (event.m_eventType == wxEVT_SCROLL_LINEUP)
2474 wevent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
2475 else if (event.m_eventType == wxEVT_SCROLL_LINEDOWN)
2476 wevent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
2477 else if (event.m_eventType == wxEVT_SCROLL_PAGEUP)
2478 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEUP;
2479 else if (event.m_eventType == wxEVT_SCROLL_PAGEDOWN)
2480 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEDOWN;
2481 else if (event.m_eventType == wxEVT_SCROLL_THUMBTRACK)
2482 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBTRACK;
2483 else if (event.m_eventType == wxEVT_SCROLL_THUMBRELEASE)
2484 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBRELEASE;
2485
2486 GetEventHandler()->ProcessEvent(wevent);
2487 }
2488 }
2489
2490 // Get the window with the focus
2491 wxWindowMac *wxWindowBase::FindFocus()
2492 {
2493 ControlRef control ;
2494 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
2495 return wxFindControlFromMacControl( control ) ;
2496 }
2497
2498 void wxWindowMac::OnSetFocus(wxFocusEvent& event)
2499 {
2500 // panel wants to track the window which was the last to have focus in it,
2501 // so we want to set ourselves as the window which last had focus
2502 //
2503 // notice that it's also important to do it upwards the tree becaus
2504 // otherwise when the top level panel gets focus, it won't set it back to
2505 // us, but to some other sibling
2506
2507 // CS:don't know if this is still needed:
2508 //wxChildFocusEvent eventFocus(this);
2509 //(void)GetEventHandler()->ProcessEvent(eventFocus);
2510
2511 if ( MacGetTopLevelWindow() && m_peer->NeedsFocusRect() )
2512 {
2513 wxMacWindowStateSaver sv( this ) ;
2514
2515 int w , h ;
2516 int x , y ;
2517 x = y = 0 ;
2518 MacWindowToRootWindow( &x , &y ) ;
2519 GetSize( &w , &h ) ;
2520 Rect rect = {y , x , h + y , w + x } ;
2521
2522 if ( event.GetEventType() == wxEVT_SET_FOCUS )
2523 DrawThemeFocusRect( &rect , true ) ;
2524 else
2525 {
2526 DrawThemeFocusRect( &rect , false ) ;
2527
2528 // as this erases part of the frame we have to redraw borders
2529 // and because our z-ordering is not always correct (staticboxes)
2530 // we have to invalidate things, we cannot simple redraw
2531 RgnHandle updateInner = NewRgn() , updateOuter = NewRgn() ;
2532 RectRgn( updateInner , &rect ) ;
2533 InsetRect( &rect , -4 , -4 ) ;
2534 RectRgn( updateOuter , &rect ) ;
2535 DiffRgn( updateOuter , updateInner ,updateOuter ) ;
2536 wxPoint parent(0,0);
2537 GetParent()->MacWindowToRootWindow( &parent.x , &parent.y ) ;
2538 parent -= GetParent()->GetClientAreaOrigin() ;
2539 OffsetRgn( updateOuter , -parent.x , -parent.y ) ;
2540 GetParent()->m_peer->SetNeedsDisplay( true , updateOuter ) ;
2541 DisposeRgn(updateOuter) ;
2542 DisposeRgn(updateInner) ;
2543 }
2544 }
2545
2546 event.Skip();
2547 }
2548
2549 void wxWindowMac::OnInternalIdle()
2550 {
2551 // This calls the UI-update mechanism (querying windows for
2552 // menu/toolbar/control state information)
2553 if (wxUpdateUIEvent::CanUpdate(this))
2554 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2555 }
2556
2557 // Raise the window to the top of the Z order
2558 void wxWindowMac::Raise()
2559 {
2560 m_peer->SetZOrder( true , NULL ) ;
2561 }
2562
2563 // Lower the window to the bottom of the Z order
2564 void wxWindowMac::Lower()
2565 {
2566 m_peer->SetZOrder( false , NULL ) ;
2567 }
2568
2569
2570 // static wxWindow *gs_lastWhich = NULL;
2571
2572 bool wxWindowMac::MacSetupCursor( const wxPoint& pt)
2573 {
2574 // first trigger a set cursor event
2575
2576 wxPoint clientorigin = GetClientAreaOrigin() ;
2577 wxSize clientsize = GetClientSize() ;
2578 wxCursor cursor ;
2579 if ( wxRect2DInt( clientorigin.x , clientorigin.y , clientsize.x , clientsize.y ).Contains( wxPoint2DInt( pt ) ) )
2580 {
2581 wxSetCursorEvent event( pt.x , pt.y );
2582
2583 bool processedEvtSetCursor = GetEventHandler()->ProcessEvent(event);
2584 if ( processedEvtSetCursor && event.HasCursor() )
2585 {
2586 cursor = event.GetCursor() ;
2587 }
2588 else
2589 {
2590
2591 // the test for processedEvtSetCursor is here to prevent using m_cursor
2592 // if the user code caught EVT_SET_CURSOR() and returned nothing from
2593 // it - this is a way to say that our cursor shouldn't be used for this
2594 // point
2595 if ( !processedEvtSetCursor && m_cursor.Ok() )
2596 {
2597 cursor = m_cursor ;
2598 }
2599 if ( wxIsBusy() )
2600 {
2601 }
2602 else
2603 {
2604 if ( !GetParent() )
2605 cursor = *wxSTANDARD_CURSOR ;
2606 }
2607 }
2608 if ( cursor.Ok() )
2609 cursor.MacInstall() ;
2610 }
2611 return cursor.Ok() ;
2612 }
2613
2614 wxString wxWindowMac::MacGetToolTipString( wxPoint &pt )
2615 {
2616 if ( m_tooltip )
2617 {
2618 return m_tooltip->GetTip() ;
2619 }
2620 return wxEmptyString ;
2621 }
2622
2623 void wxWindowMac::Update()
2624 {
2625 #if TARGET_API_MAC_OSX
2626 WindowRef window = (WindowRef)MacGetTopLevelWindowRef() ;
2627 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
2628 // for composited windows this also triggers a redraw of all
2629 // invalid views in the window
2630 if( UMAGetSystemVersion() >= 0x1030 )
2631 HIWindowFlush(window) ;
2632 else
2633 #endif
2634 {
2635 // the only way to trigger the redrawing on earlier systems is to call
2636 // ReceiveNextEvent
2637
2638 EventRef currentEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
2639 UInt32 currentEventClass = 0 ;
2640 UInt32 currentEventKind = 0 ;
2641 if ( currentEvent != NULL )
2642 {
2643 currentEventClass = ::GetEventClass( currentEvent ) ;
2644 currentEventKind = ::GetEventKind( currentEvent ) ;
2645 }
2646 if ( currentEventClass != kEventClassMenu )
2647 {
2648 // when tracking a menu, strange redraw errors occur if we flush now, so leave..
2649
2650 EventRef theEvent;
2651 OSStatus status = noErr ;
2652 status = ReceiveNextEvent( 0 , NULL , kEventDurationNoWait , false , &theEvent ) ;
2653 }
2654 else
2655 m_peer->SetNeedsDisplay( true ) ;
2656 }
2657 #else
2658 ::Draw1Control( m_peer->GetControlRef() ) ;
2659 #endif
2660 }
2661
2662 wxTopLevelWindowMac* wxWindowMac::MacGetTopLevelWindow() const
2663 {
2664 wxTopLevelWindowMac* win = NULL ;
2665 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
2666 if ( window )
2667 {
2668 win = wxFindWinFromMacWindow( window ) ;
2669 }
2670 return win ;
2671 }
2672 wxRegion wxWindowMac::MacGetVisibleRegion( bool includeOuterStructures )
2673 {
2674 // includeOuterStructures is true if we try to draw somthing like a focus ring etc.
2675 // also a window dc uses this, in this case we only clip in the hierarchy for hard
2676 // borders like a scrollwindow, splitter etc otherwise we end up in a paranoia having
2677 // to add focus borders everywhere
2678
2679 Rect r ;
2680 RgnHandle visRgn = NewRgn() ;
2681 RgnHandle tempRgn = NewRgn() ;
2682 if ( m_peer->IsVisible())
2683 {
2684 m_peer->GetRect( &r ) ;
2685 r.left -= MacGetLeftBorderSize() ;
2686 r.top -= MacGetTopBorderSize() ;
2687 r.bottom += MacGetBottomBorderSize() ;
2688 r.right += MacGetRightBorderSize() ;
2689
2690 if (! MacGetTopLevelWindow()->MacUsesCompositing() )
2691 {
2692 MacRootWindowToWindow( &r.left , & r.top ) ;
2693 MacRootWindowToWindow( &r.right , & r.bottom ) ;
2694 }
2695 else
2696 {
2697 r.right -= r.left ;
2698 r.bottom -= r.top ;
2699 r.left = 0 ;
2700 r.top = 0 ;
2701 }
2702 if ( includeOuterStructures )
2703 InsetRect( &r , -4 , -4 ) ;
2704 RectRgn( visRgn , &r ) ;
2705
2706 if ( !IsTopLevel() )
2707 {
2708 wxWindow* child = this ;
2709 wxWindow* parent = child->GetParent() ;
2710 while( parent )
2711 {
2712 int x , y ;
2713 wxSize size ;
2714 // we have to find a better clipping algorithm here, in order not to clip things
2715 // positioned like status and toolbar
2716 if ( 1 /* parent->IsTopLevel() && child->IsKindOf( CLASSINFO( wxToolBar ) ) */ )
2717 {
2718 size = parent->GetSize() ;
2719 x = y = 0 ;
2720 }
2721 else
2722 {
2723 size = parent->GetClientSize() ;
2724 wxPoint origin = parent->GetClientAreaOrigin() ;
2725 x = origin.x ;
2726 y = origin.y ;
2727 }
2728 parent->MacWindowToRootWindow( &x, &y ) ;
2729 MacRootWindowToWindow( &x , &y ) ;
2730
2731 if ( !includeOuterStructures || (
2732 parent->IsKindOf( CLASSINFO( wxScrolledWindow ) ) ||
2733 parent->IsKindOf( CLASSINFO( wxSashLayoutWindow ) ) ||
2734 ( parent->GetParent() && parent->GetParent()->IsKindOf( CLASSINFO( wxSplitterWindow ) ) )
2735 ) )
2736 {
2737 SetRectRgn( tempRgn ,
2738 x + parent->MacGetLeftBorderSize() , y + parent->MacGetTopBorderSize() ,
2739 x + size.x - parent->MacGetRightBorderSize(),
2740 y + size.y - parent->MacGetBottomBorderSize()) ;
2741
2742 SectRgn( visRgn , tempRgn , visRgn ) ;
2743 }
2744 if ( parent->IsTopLevel() )
2745 break ;
2746 child = parent ;
2747 parent = child->GetParent() ;
2748 }
2749 }
2750 }
2751
2752 wxRegion vis = visRgn ;
2753 DisposeRgn( visRgn ) ;
2754 DisposeRgn( tempRgn ) ;
2755 return vis ;
2756 }
2757
2758 /*
2759 This function must not change the updatergn !
2760 */
2761 bool wxWindowMac::MacDoRedraw( WXHRGN updatergnr , long time )
2762 {
2763 RgnHandle updatergn = (RgnHandle) updatergnr ;
2764 bool handled = false ;
2765 Rect updatebounds ;
2766 GetRegionBounds( updatergn , &updatebounds ) ;
2767 // wxLogDebug("update for %s bounds %d , %d , %d , %d",typeid(*this).name() , updatebounds.left , updatebounds.top , updatebounds.right , updatebounds.bottom ) ;
2768 if ( !EmptyRgn(updatergn) )
2769 {
2770 RgnHandle newupdate = NewRgn() ;
2771 wxSize point = GetClientSize() ;
2772 wxPoint origin = GetClientAreaOrigin() ;
2773 SetRectRgn( newupdate , origin.x , origin.y , origin.x + point.x , origin.y+point.y ) ;
2774 SectRgn( newupdate , updatergn , newupdate ) ;
2775
2776 // first send an erase event to the entire update area
2777 {
2778 wxWindowDC dc(this);
2779 dc.SetClippingRegion(wxRegion(updatergn));
2780 wxEraseEvent eevent( GetId(), &dc );
2781 eevent.SetEventObject( this );
2782 GetEventHandler()->ProcessEvent( eevent );
2783 }
2784
2785 // calculate a client-origin version of the update rgn and set m_updateRegion to that
2786 OffsetRgn( newupdate , -origin.x , -origin.y ) ;
2787 m_updateRegion = newupdate ;
2788 DisposeRgn( newupdate ) ;
2789
2790 if ( !m_updateRegion.Empty() )
2791 {
2792 // paint the window itself
2793 wxPaintEvent event;
2794 event.m_timeStamp = time ;
2795 event.SetEventObject(this);
2796 handled = GetEventHandler()->ProcessEvent(event);
2797
2798 // we have to call the default built-in handler, as otherwise our frames will be drawn and immediately erased afterwards
2799 if ( !handled )
2800 {
2801 if ( wxTheApp->MacGetCurrentEvent() != NULL && wxTheApp->MacGetCurrentEventHandlerCallRef() != NULL )
2802 {
2803 CallNextEventHandler((EventHandlerCallRef)wxTheApp->MacGetCurrentEventHandlerCallRef() , (EventRef) wxTheApp->MacGetCurrentEvent() ) ;
2804 handled = true ;
2805 }
2806 }
2807
2808 }
2809
2810 // now we cannot rely on having its borders drawn by a window itself, as it does not
2811 // get the updateRgn wide enough to always do so, so we do it from the parent
2812 // this would also be the place to draw any custom backgrounds for native controls
2813 // in Composited windowing
2814 wxPoint clientOrigin = GetClientAreaOrigin() ;
2815
2816 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
2817 {
2818 wxWindowMac *child = node->GetData();
2819 if (child == m_vScrollBar) continue;
2820 if (child == m_hScrollBar) continue;
2821 if (child->IsTopLevel()) continue;
2822 if (!child->IsShown()) continue;
2823
2824 int x,y;
2825 child->GetPosition( &x, &y );
2826 int w,h;
2827 child->GetSize( &w, &h );
2828 Rect childRect = { y , x , y + h , x + w } ;
2829 OffsetRect( &childRect , clientOrigin.x , clientOrigin.y ) ;
2830 if ( child->MacGetTopBorderSize() )
2831 {
2832 if ( RectInRgn( &childRect , updatergn ) )
2833 {
2834 // paint custom borders
2835 wxNcPaintEvent eventNc( child->GetId() );
2836 eventNc.SetEventObject( child );
2837 if ( !child->GetEventHandler()->ProcessEvent( eventNc ) )
2838 {
2839 wxWindowDC dc(this) ;
2840 dc.SetClippingRegion(wxRegion(updatergn));
2841 wxMacPortSetter helper(&dc) ;
2842 child->MacPaintBorders( dc.m_macLocalOrigin.x + childRect.left , dc.m_macLocalOrigin.y + childRect.top) ;
2843 }
2844 }
2845 }
2846 if ( child->m_peer->NeedsFocusRect() && child->m_peer->HasFocus() )
2847 {
2848 wxWindowDC dc(this) ;
2849 dc.SetClippingRegion(wxRegion(updatergn));
2850 wxMacPortSetter helper(&dc) ;
2851 Rect r = childRect ;
2852 OffsetRect( &r , dc.m_macLocalOrigin.x , dc.m_macLocalOrigin.y ) ;
2853 DrawThemeFocusRect( &r , true ) ;
2854 }
2855 }
2856 }
2857 return handled ;
2858 }
2859
2860 void wxWindowMac::MacRedraw( WXHRGN updatergnr , long time, bool erase)
2861 {
2862 RgnHandle updatergn = (RgnHandle) updatergnr ;
2863 // updatergn is always already clipped to our boundaries
2864 // if we are in compositing mode then it is in relative to the upper left of the control
2865 // if we are in non-compositing, then it is relatvie to the uppder left of the content area
2866 // of the toplevel window
2867 // it is in window coordinates, not in client coordinates
2868
2869 // ownUpdateRgn is the area that this window has to repaint, it is in window coordinates
2870 RgnHandle ownUpdateRgn = NewRgn() ;
2871 CopyRgn( updatergn , ownUpdateRgn ) ;
2872
2873 if ( MacGetTopLevelWindow()->MacUsesCompositing() == false )
2874 {
2875 Rect bounds;
2876 m_peer->GetRectInWindowCoords( &bounds );
2877 RgnHandle controlRgn = NewRgn();
2878 RectRgn( controlRgn, &bounds );
2879 //KO: This sets the ownUpdateRgn to the area of this control that is inside
2880 // the window update region
2881 SectRgn( ownUpdateRgn, controlRgn, ownUpdateRgn );
2882 DisposeRgn( controlRgn );
2883
2884 //KO: convert ownUpdateRgn to local coordinates
2885 OffsetRgn( ownUpdateRgn, -bounds.left, -bounds.top );
2886 }
2887
2888 MacDoRedraw( ownUpdateRgn , time ) ;
2889 DisposeRgn( ownUpdateRgn ) ;
2890
2891 }
2892
2893 WXWindow wxWindowMac::MacGetTopLevelWindowRef() const
2894 {
2895 wxWindowMac *iter = (wxWindowMac*)this ;
2896
2897 while( iter )
2898 {
2899 if ( iter->IsTopLevel() )
2900 return ((wxTopLevelWindow*)iter)->MacGetWindowRef() ;
2901
2902 iter = iter->GetParent() ;
2903 }
2904 wxASSERT_MSG( 1 , wxT("No valid mac root window") ) ;
2905 return NULL ;
2906 }
2907
2908 void wxWindowMac::MacCreateScrollBars( long style )
2909 {
2910 wxASSERT_MSG( m_vScrollBar == NULL && m_hScrollBar == NULL , wxT("attempt to create window twice") ) ;
2911
2912 if ( style & ( wxVSCROLL | wxHSCROLL ) )
2913 {
2914 bool hasBoth = ( style & wxVSCROLL ) && ( style & wxHSCROLL ) ;
2915 int scrlsize = MAC_SCROLLBAR_SIZE ;
2916 wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL ;
2917 if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL || GetWindowVariant() == wxWINDOW_VARIANT_MINI )
2918 {
2919 scrlsize = MAC_SMALL_SCROLLBAR_SIZE ;
2920 variant = wxWINDOW_VARIANT_SMALL ;
2921 }
2922
2923 int adjust = hasBoth ? scrlsize - 1: 0 ;
2924 int width, height ;
2925 GetClientSize( &width , &height ) ;
2926
2927 wxPoint vPoint(width-scrlsize, 0) ;
2928 wxSize vSize(scrlsize, height - adjust) ;
2929 wxPoint hPoint(0 , height-scrlsize ) ;
2930 wxSize hSize( width - adjust, scrlsize) ;
2931
2932
2933 if ( style & wxVSCROLL )
2934 {
2935 m_vScrollBar = new wxScrollBar(this, wxWINDOW_VSCROLL, vPoint,
2936 vSize , wxVERTICAL);
2937 }
2938
2939 if ( style & wxHSCROLL )
2940 {
2941 m_hScrollBar = new wxScrollBar(this, wxWINDOW_HSCROLL, hPoint,
2942 hSize , wxHORIZONTAL);
2943 }
2944 }
2945
2946
2947 // because the create does not take into account the client area origin
2948 MacRepositionScrollBars() ; // we might have a real position shift
2949 }
2950
2951 void wxWindowMac::MacRepositionScrollBars()
2952 {
2953 if ( !m_hScrollBar && !m_vScrollBar )
2954 return ;
2955
2956 bool hasBoth = ( m_hScrollBar && m_hScrollBar->IsShown()) && ( m_vScrollBar && m_vScrollBar->IsShown()) ;
2957 int scrlsize = m_hScrollBar ? m_hScrollBar->GetSize().y : ( m_vScrollBar ? m_vScrollBar->GetSize().x : MAC_SCROLLBAR_SIZE ) ;
2958 int adjust = hasBoth ? scrlsize - 1 : 0 ;
2959
2960 // get real client area
2961
2962 int width ;
2963 int height ;
2964 GetSize( &width , &height ) ;
2965
2966 width -= MacGetLeftBorderSize() + MacGetRightBorderSize();
2967 height -= MacGetTopBorderSize() + MacGetBottomBorderSize();
2968
2969 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
2970 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
2971 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
2972 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
2973 /*
2974 int x = 0 ;
2975 int y = 0 ;
2976 int w ;
2977 int h ;
2978 GetSize( &w , &h ) ;
2979
2980 MacClientToRootWindow( &x , &y ) ;
2981 MacClientToRootWindow( &w , &h ) ;
2982
2983 wxWindowMac *iter = (wxWindowMac*)this ;
2984
2985 int totW = 10000 , totH = 10000;
2986 while( iter )
2987 {
2988 if ( iter->IsTopLevel() )
2989 {
2990 iter->GetSize( &totW , &totH ) ;
2991 break ;
2992 }
2993
2994 iter = iter->GetParent() ;
2995 }
2996
2997 if ( x == 0 )
2998 {
2999 hPoint.x = -1 ;
3000 hSize.x += 1 ;
3001 }
3002 if ( y == 0 )
3003 {
3004 vPoint.y = -1 ;
3005 vSize.y += 1 ;
3006 }
3007
3008 if ( w-x >= totW )
3009 {
3010 hSize.x += 1 ;
3011 vPoint.x += 1 ;
3012 }
3013
3014 if ( h-y >= totH )
3015 {
3016 vSize.y += 1 ;
3017 hPoint.y += 1 ;
3018 }
3019 */
3020 if ( m_vScrollBar )
3021 {
3022 m_vScrollBar->SetSize( vPoint.x , vPoint.y, vSize.x, vSize.y , wxSIZE_ALLOW_MINUS_ONE);
3023 }
3024 if ( m_hScrollBar )
3025 {
3026 m_hScrollBar->SetSize( hPoint.x , hPoint.y, hSize.x, hSize.y, wxSIZE_ALLOW_MINUS_ONE);
3027 }
3028 }
3029
3030 bool wxWindowMac::AcceptsFocus() const
3031 {
3032 return MacCanFocus() && wxWindowBase::AcceptsFocus();
3033 }
3034
3035 void wxWindowMac::MacSuperChangedPosition()
3036 {
3037 // only window-absolute structures have to be moved i.e. controls
3038
3039 wxWindowListNode *node = GetChildren().GetFirst();
3040 while ( node )
3041 {
3042 wxWindowMac *child = node->GetData();
3043 child->MacSuperChangedPosition() ;
3044 node = node->GetNext();
3045 }
3046 }
3047
3048 void wxWindowMac::MacTopLevelWindowChangedPosition()
3049 {
3050 // only screen-absolute structures have to be moved i.e. glcanvas
3051
3052 wxWindowListNode *node = GetChildren().GetFirst();
3053 while ( node )
3054 {
3055 wxWindowMac *child = node->GetData();
3056 child->MacTopLevelWindowChangedPosition() ;
3057 node = node->GetNext();
3058 }
3059 }
3060
3061 long wxWindowMac::MacGetLeftBorderSize( ) const
3062 {
3063 if( IsTopLevel() )
3064 return 0 ;
3065
3066 if (m_windowStyle & wxRAISED_BORDER || m_windowStyle & wxSUNKEN_BORDER )
3067 {
3068 SInt32 border = 3 ;
3069 return border ;
3070 }
3071 else if ( m_windowStyle &wxDOUBLE_BORDER)
3072 {
3073 SInt32 border = 3 ;
3074 return border ;
3075 }
3076 else if (m_windowStyle &wxSIMPLE_BORDER)
3077 {
3078 return 1 ;
3079 }
3080 return 0 ;
3081 }
3082
3083 long wxWindowMac::MacGetRightBorderSize( ) const
3084 {
3085 // they are all symmetric in mac themes
3086 return MacGetLeftBorderSize() ;
3087 }
3088
3089 long wxWindowMac::MacGetTopBorderSize( ) const
3090 {
3091 // they are all symmetric in mac themes
3092 return MacGetLeftBorderSize() ;
3093 }
3094
3095 long wxWindowMac::MacGetBottomBorderSize( ) const
3096 {
3097 // they are all symmetric in mac themes
3098 return MacGetLeftBorderSize() ;
3099 }
3100
3101 long wxWindowMac::MacRemoveBordersFromStyle( long style )
3102 {
3103 return style & ~wxBORDER_MASK ;
3104 }
3105
3106 // Find the wxWindowMac at the current mouse position, returning the mouse
3107 // position.
3108 wxWindowMac* wxFindWindowAtPointer(wxPoint& pt)
3109 {
3110 pt = wxGetMousePosition();
3111 wxWindowMac* found = wxFindWindowAtPoint(pt);
3112 return found;
3113 }
3114
3115 // Get the current mouse position.
3116 wxPoint wxGetMousePosition()
3117 {
3118 int x, y;
3119 wxGetMousePosition(& x, & y);
3120 return wxPoint(x, y);
3121 }
3122
3123 void wxWindowMac::OnMouseEvent( wxMouseEvent &event )
3124 {
3125 if ( event.GetEventType() == wxEVT_RIGHT_DOWN )
3126 {
3127 // copied from wxGTK : CS
3128 // generate a "context menu" event: this is similar to wxEVT_RIGHT_DOWN
3129 // except that:
3130 //
3131 // (a) it's a command event and so is propagated to the parent
3132 // (b) under MSW it can be generated from kbd too
3133 // (c) it uses screen coords (because of (a))
3134 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
3135 this->GetId(),
3136 this->ClientToScreen(event.GetPosition()));
3137 if ( ! GetEventHandler()->ProcessEvent(evtCtx) )
3138 event.Skip() ;
3139 }
3140 else
3141 {
3142 event.Skip() ;
3143 }
3144 }
3145
3146 void wxWindowMac::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED( mouseStillDown ) )
3147 {
3148 }
3149
3150 Rect wxMacGetBoundsForControl( wxWindow* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
3151 {
3152 int x ,y , w ,h ;
3153
3154 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin) ;
3155 Rect bounds = { y , x , y+h , x+w };
3156 return bounds ;
3157 }
3158
3159 wxInt32 wxWindowMac::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
3160 {
3161 return eventNotHandledErr ;
3162 }
3163
3164