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