]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/window.cpp
make sure we are getting the gui mutex before shutting down
[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 #if 0
939 // there is currently no way to determinate whether the window is running in full keyboard
940 // access mode, therefore we cannot rely on these features yet
941 UInt32 features = 0 ;
942 GetControlFeatures( (ControlRef) m_macControl , &features ) ;
943 return features & ( kControlSupportsFocus | kControlGetsFocusOnClick ) ;
944 #endif
945 return true ;
946 }
947
948
949 void wxWindowMac::SetFocus()
950 {
951 if ( AcceptsFocus() )
952 {
953 #if !TARGET_API_MAC_OSX
954 wxWindow* former = FindFocus() ;
955 #endif
956 OSStatus err = SetKeyboardFocus( (WindowRef) MacGetTopLevelWindowRef() , (ControlRef) GetHandle() , kControlFocusNextPart ) ;
957 // as we cannot rely on the control features to find out whether we are in full keyboard mode, we can only
958 // leave in case of an error
959 if ( err == errCouldntSetFocus )
960 return ;
961
962 #if !TARGET_API_MAC_OSX
963 // emulate carbon events when running under carbonlib where they are not natively available
964 if ( former )
965 {
966 EventRef evRef = NULL ;
967 verify_noerr( MacCreateEvent( NULL , kEventClassControl , kEventControlSetFocusPart , TicksToEventTime( TickCount() ) , kEventAttributeUserEvent ,
968 &evRef ) );
969
970 wxMacCarbonEvent cEvent( evRef ) ;
971 cEvent.SetParameter<ControlRef>( kEventParamDirectObject , (ControlRef) former->GetHandle() ) ;
972 cEvent.SetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode , kControlFocusNoPart ) ;
973
974 wxMacWindowEventHandler( NULL , evRef , former ) ;
975 ReleaseEvent(evRef) ;
976 }
977 // send new focus event
978 {
979 EventRef evRef = NULL ;
980 verify_noerr( MacCreateEvent( NULL , kEventClassControl , kEventControlSetFocusPart , TicksToEventTime( TickCount() ) , kEventAttributeUserEvent ,
981 &evRef ) );
982
983 wxMacCarbonEvent cEvent( evRef ) ;
984 cEvent.SetParameter<ControlRef>( kEventParamDirectObject , (ControlRef) GetHandle() ) ;
985 cEvent.SetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode , kControlFocusNextPart ) ;
986
987 wxMacWindowEventHandler( NULL , evRef , this ) ;
988 ReleaseEvent(evRef) ;
989 }
990 #endif
991 }
992 }
993
994
995 void wxWindowMac::DoCaptureMouse()
996 {
997 wxTheApp->s_captureWindow = this ;
998 }
999
1000 wxWindow* wxWindowBase::GetCapture()
1001 {
1002 return wxTheApp->s_captureWindow ;
1003 }
1004
1005 void wxWindowMac::DoReleaseMouse()
1006 {
1007 wxTheApp->s_captureWindow = NULL ;
1008 }
1009
1010 #if wxUSE_DRAG_AND_DROP
1011
1012 void wxWindowMac::SetDropTarget(wxDropTarget *pDropTarget)
1013 {
1014 if ( m_dropTarget != 0 ) {
1015 delete m_dropTarget;
1016 }
1017
1018 m_dropTarget = pDropTarget;
1019 if ( m_dropTarget != 0 )
1020 {
1021 // TODO
1022 }
1023 }
1024
1025 #endif
1026
1027 // Old style file-manager drag&drop
1028 void wxWindowMac::DragAcceptFiles(bool accept)
1029 {
1030 // TODO
1031 }
1032
1033 void wxWindowMac::MacGetPositionAndSizeFromControl(int& x, int& y,
1034 int& w, int& h) const
1035 {
1036 Rect bounds ;
1037 GetControlBounds( (ControlRef) m_macControl , &bounds ) ;
1038
1039
1040 x = bounds.left ;
1041 y = bounds.top ;
1042 w = bounds.right - bounds.left ;
1043 h = bounds.bottom - bounds.top ;
1044
1045 wxTopLevelWindow* tlw = wxDynamicCast( this , wxTopLevelWindow ) ;
1046 if ( tlw )
1047 {
1048 Point tlworigin = { 0 , 0 } ;
1049 GrafPtr port ;
1050 bool swapped = QDSwapPort( UMAGetWindowPort( (WindowRef) tlw->MacGetWindowRef() ) , &port ) ;
1051 ::LocalToGlobal( &tlworigin ) ;
1052 if ( swapped )
1053 ::SetPort( port ) ;
1054 x = tlworigin.h ;
1055 y = tlworigin.v ;
1056 }
1057 }
1058
1059 bool wxWindowMac::MacGetBoundsForControl(const wxPoint& pos,
1060 const wxSize& size,
1061 int& x, int& y,
1062 int& w, int& h , bool adjustOrigin ) const
1063 {
1064 x = (int)pos.x;
1065 y = (int)pos.y;
1066 // todo the default calls may be used as soon as PostCreateControl Is moved here
1067 w = size.x ; // WidthDefault( size.x );
1068 h = size.y ; // HeightDefault( size.y ) ;
1069 #if !TARGET_API_MAC_OSX
1070 GetParent()->MacWindowToRootWindow( &x , &y ) ;
1071 #endif
1072 if ( adjustOrigin )
1073 AdjustForParentClientOrigin( x , y ) ;
1074 return true ;
1075 }
1076
1077 // Get total size
1078 void wxWindowMac::DoGetSize(int *x, int *y) const
1079 {
1080 #if TARGET_API_MAC_OSX
1081 int x1 , y1 , w1 ,h1 ;
1082 MacGetPositionAndSizeFromControl( x1 , y1, w1 ,h1 ) ;
1083 if(x) *x = w1 ;
1084 if(y) *y = h1 ;
1085
1086 #else
1087 Rect bounds ;
1088 GetControlBounds( (ControlRef) m_macControl , &bounds ) ;
1089 if(x) *x = bounds.right - bounds.left ;
1090 if(y) *y = bounds.bottom - bounds.top ;
1091 #endif
1092 }
1093
1094 void wxWindowMac::DoGetPosition(int *x, int *y) const
1095 {
1096 #if TARGET_API_MAC_OSX
1097 int x1 , y1 , w1 ,h1 ;
1098 MacGetPositionAndSizeFromControl( x1 , y1, w1 ,h1 ) ;
1099 if ( !IsTopLevel() )
1100 {
1101 wxWindow *parent = GetParent();
1102 if ( parent )
1103 {
1104 wxPoint pt(parent->GetClientAreaOrigin());
1105 x1 -= pt.x ;
1106 y1 -= pt.y ;
1107 }
1108 }
1109 if(x) *x = x1 ;
1110 if(y) *y = y1 ;
1111 #else
1112 Rect bounds ;
1113 GetControlBounds( (ControlRef) m_macControl , &bounds ) ;
1114 wxCHECK_RET( GetParent() , wxT("Missing Parent") ) ;
1115
1116 int xx = bounds.left ;
1117 int yy = bounds.top ;
1118
1119 if ( !GetParent()->IsTopLevel() )
1120 {
1121 GetControlBounds( (ControlRef) GetParent()->GetHandle() , &bounds ) ;
1122
1123 xx -= bounds.left ;
1124 yy -= bounds.top ;
1125 }
1126
1127 wxPoint pt(GetParent()->GetClientAreaOrigin());
1128 xx -= pt.x;
1129 yy -= pt.y;
1130
1131 if(x) *x = xx;
1132 if(y) *y = yy;
1133 #endif
1134 }
1135
1136 void wxWindowMac::DoScreenToClient(int *x, int *y) const
1137 {
1138 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
1139
1140 wxCHECK_RET( window , wxT("TopLevel Window Missing") ) ;
1141
1142 {
1143 Point localwhere = {0,0} ;
1144
1145 if(x) localwhere.h = * x ;
1146 if(y) localwhere.v = * y ;
1147
1148 wxMacPortSaver s((GrafPtr)GetWindowPort( window )) ;
1149 ::GlobalToLocal( &localwhere ) ;
1150 if(x) *x = localwhere.h ;
1151 if(y) *y = localwhere.v ;
1152
1153 }
1154 MacRootWindowToWindow( x , y ) ;
1155
1156 wxPoint origin = GetClientAreaOrigin() ;
1157 if(x) *x -= origin.x ;
1158 if(y) *y -= origin.y ;
1159 }
1160
1161 void wxWindowMac::DoClientToScreen(int *x, int *y) const
1162 {
1163 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
1164 wxCHECK_RET( window , wxT("TopLevel Window Missing") ) ;
1165
1166 wxPoint origin = GetClientAreaOrigin() ;
1167 if(x) *x += origin.x ;
1168 if(y) *y += origin.y ;
1169
1170 MacWindowToRootWindow( x , y ) ;
1171
1172 {
1173 Point localwhere = { 0,0 };
1174 if(x) localwhere.h = * x ;
1175 if(y) localwhere.v = * y ;
1176
1177 wxMacPortSaver s((GrafPtr)GetWindowPort( window )) ;
1178 ::LocalToGlobal( &localwhere ) ;
1179 if(x) *x = localwhere.h ;
1180 if(y) *y = localwhere.v ;
1181 }
1182 }
1183
1184 void wxWindowMac::MacClientToRootWindow( int *x , int *y ) const
1185 {
1186 wxPoint origin = GetClientAreaOrigin() ;
1187 if(x) *x += origin.x ;
1188 if(y) *y += origin.y ;
1189
1190 MacWindowToRootWindow( x , y ) ;
1191 }
1192
1193 void wxWindowMac::MacRootWindowToClient( int *x , int *y ) const
1194 {
1195 MacRootWindowToWindow( x , y ) ;
1196
1197 wxPoint origin = GetClientAreaOrigin() ;
1198 if(x) *x -= origin.x ;
1199 if(y) *y -= origin.y ;
1200 }
1201
1202 void wxWindowMac::MacWindowToRootWindow( int *x , int *y ) const
1203 {
1204 #if TARGET_API_MAC_OSX
1205 HIPoint pt ;
1206 if ( x ) pt.x = *x ;
1207 if ( y ) pt.y = *y ;
1208
1209 if ( !IsTopLevel() )
1210 HIViewConvertPoint( &pt , (ControlRef) m_macControl , (ControlRef) MacGetTopLevelWindow()->GetHandle() ) ;
1211
1212 if ( x ) *x = (int) pt.x ;
1213 if ( y ) *y = (int) pt.y ;
1214 #else
1215 if ( !IsTopLevel() )
1216 {
1217 Rect bounds ;
1218 GetControlBounds( (ControlRef) m_macControl , &bounds ) ;
1219 if(x) *x += bounds.left ;
1220 if(y) *y += bounds.top ;
1221 }
1222 #endif
1223 }
1224
1225 void wxWindowMac::MacWindowToRootWindow( short *x , short *y ) const
1226 {
1227 int x1 , y1 ;
1228 if ( x ) x1 = *x ;
1229 if ( y ) y1 = *y ;
1230 MacWindowToRootWindow( &x1 , &y1 ) ;
1231 if ( x ) *x = x1 ;
1232 if ( y ) *y = y1 ;
1233 }
1234
1235 void wxWindowMac::MacRootWindowToWindow( int *x , int *y ) const
1236 {
1237 #if TARGET_API_MAC_OSX
1238 HIPoint pt ;
1239 if ( x ) pt.x = *x ;
1240 if ( y ) pt.y = *y ;
1241
1242 if ( !IsTopLevel() )
1243 HIViewConvertPoint( &pt , (ControlRef) MacGetTopLevelWindow()->GetHandle() , (ControlRef) m_macControl ) ;
1244
1245 if ( x ) *x = (int) pt.x ;
1246 if ( y ) *y = (int) pt.y ;
1247 #else
1248 if ( !IsTopLevel() )
1249 {
1250 Rect bounds ;
1251 GetControlBounds( (ControlRef) m_macControl , &bounds ) ;
1252 if(x) *x -= bounds.left ;
1253 if(y) *y -= bounds.top ;
1254 }
1255 #endif
1256 }
1257
1258 void wxWindowMac::MacRootWindowToWindow( short *x , short *y ) const
1259 {
1260 int x1 , y1 ;
1261 if ( x ) x1 = *x ;
1262 if ( y ) y1 = *y ;
1263 MacRootWindowToWindow( &x1 , &y1 ) ;
1264 if ( x ) *x = x1 ;
1265 if ( y ) *y = y1 ;
1266 }
1267
1268 void wxWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1269 {
1270 RgnHandle rgn = NewRgn() ;
1271 Rect content ;
1272 if ( GetControlRegion( (ControlRef) m_macControl , kControlContentMetaPart , rgn ) == noErr )
1273 {
1274 GetRegionBounds( rgn , &content ) ;
1275 DisposeRgn( rgn ) ;
1276 }
1277 else
1278 {
1279 GetControlBounds( (ControlRef) m_macControl , &content ) ;
1280 }
1281 Rect structure ;
1282 GetControlBounds( (ControlRef) m_macControl , &structure ) ;
1283 #if !TARGET_API_MAC_OSX
1284 OffsetRect( &content , -structure.left , -structure.top ) ;
1285 #endif
1286 left = content.left - structure.left ;
1287 top = content.top - structure.top ;
1288 right = structure.right - content.right ;
1289 bottom = structure.bottom - content.bottom ;
1290 }
1291
1292 wxSize wxWindowMac::DoGetSizeFromClientSize( const wxSize & size ) const
1293 {
1294 wxSize sizeTotal = size;
1295
1296 RgnHandle rgn = NewRgn() ;
1297
1298 Rect content ;
1299
1300 if ( GetControlRegion( (ControlRef) m_macControl , kControlContentMetaPart , rgn ) == noErr )
1301 {
1302 GetRegionBounds( rgn , &content ) ;
1303 DisposeRgn( rgn ) ;
1304 }
1305 else
1306 {
1307 GetControlBounds( (ControlRef) m_macControl , &content ) ;
1308 }
1309 Rect structure ;
1310 GetControlBounds( (ControlRef) m_macControl , &structure ) ;
1311 #if !TARGET_API_MAC_OSX
1312 OffsetRect( &content , -structure.left , -structure.top ) ;
1313 #endif
1314
1315 sizeTotal.x += (structure.right - structure.left) - (content.right - content.left) ;
1316 sizeTotal.y += (structure.bottom - structure.top) - (content.bottom - content.top ) ;
1317
1318 sizeTotal.x += MacGetLeftBorderSize( ) + MacGetRightBorderSize( ) ;
1319 sizeTotal.y += MacGetTopBorderSize( ) + MacGetBottomBorderSize( ) ;
1320
1321 return sizeTotal;
1322 }
1323
1324
1325 // Get size *available for subwindows* i.e. excluding menu bar etc.
1326 void wxWindowMac::DoGetClientSize(int *x, int *y) const
1327 {
1328 int ww, hh;
1329
1330 RgnHandle rgn = NewRgn() ;
1331 Rect content ;
1332 if ( GetControlRegion( (ControlRef) m_macControl , kControlContentMetaPart , rgn ) == noErr )
1333 {
1334 GetRegionBounds( rgn , &content ) ;
1335 DisposeRgn( rgn ) ;
1336 }
1337 else
1338 {
1339 GetControlBounds( (ControlRef) m_macControl , &content ) ;
1340 }
1341 #if !TARGET_API_MAC_OSX
1342 Rect structure ;
1343 GetControlBounds( (ControlRef) m_macControl , &structure ) ;
1344 OffsetRect( &content , -structure.left , -structure.top ) ;
1345 #endif
1346 ww = content.right - content.left ;
1347 hh = content.bottom - content.top ;
1348
1349 ww -= MacGetLeftBorderSize( ) + MacGetRightBorderSize( ) ;
1350 hh -= MacGetTopBorderSize( ) + MacGetBottomBorderSize( );
1351
1352 if ( (m_vScrollBar && m_vScrollBar->IsShown()) || (m_hScrollBar && m_hScrollBar->IsShown()) )
1353 {
1354 int x1 = 0 ;
1355 int y1 = 0 ;
1356 int w ;
1357 int h ;
1358 GetSize( &w , &h ) ;
1359
1360 MacClientToRootWindow( &x1 , &y1 ) ;
1361 MacClientToRootWindow( &w , &h ) ;
1362
1363 wxWindowMac *iter = (wxWindowMac*)this ;
1364
1365 int totW = 10000 , totH = 10000;
1366 while( iter )
1367 {
1368 if ( iter->IsTopLevel() )
1369 {
1370 iter->GetSize( &totW , &totH ) ;
1371 break ;
1372 }
1373
1374 iter = iter->GetParent() ;
1375 }
1376
1377 if (m_hScrollBar && m_hScrollBar->IsShown() )
1378 {
1379 hh -= MAC_SCROLLBAR_SIZE;
1380 if ( h-y1 >= totH )
1381 {
1382 hh += 1 ;
1383 }
1384 }
1385 if (m_vScrollBar && m_vScrollBar->IsShown() )
1386 {
1387 ww -= MAC_SCROLLBAR_SIZE;
1388 if ( w-x1 >= totW )
1389 {
1390 ww += 1 ;
1391 }
1392 }
1393 }
1394 if(x) *x = ww;
1395 if(y) *y = hh;
1396
1397 }
1398
1399 bool wxWindowMac::SetCursor(const wxCursor& cursor)
1400 {
1401 if (m_cursor == cursor)
1402 return FALSE;
1403
1404 if (wxNullCursor == cursor)
1405 {
1406 if ( ! wxWindowBase::SetCursor( *wxSTANDARD_CURSOR ) )
1407 return FALSE ;
1408 }
1409 else
1410 {
1411 if ( ! wxWindowBase::SetCursor( cursor ) )
1412 return FALSE ;
1413 }
1414
1415 wxASSERT_MSG( m_cursor.Ok(),
1416 wxT("cursor must be valid after call to the base version"));
1417
1418 /*
1419
1420 TODO why do we have to use current coordinates ?
1421
1422 Point pt ;
1423 wxWindowMac *mouseWin ;
1424 GetMouse( &pt ) ;
1425
1426 // Change the cursor NOW if we're within the correct window
1427
1428
1429 if ( MacGetWindowFromPoint( wxPoint( pt.h , pt.v ) , &mouseWin ) )
1430 {
1431 if ( mouseWin == this && !wxIsBusy() )
1432 {
1433 m_cursor.MacInstall() ;
1434 }
1435 }
1436 */
1437 if ( !wxIsBusy() )
1438 {
1439 m_cursor.MacInstall() ;
1440 }
1441
1442 return TRUE ;
1443 }
1444
1445 #if wxUSE_MENUS
1446 bool wxWindowMac::DoPopupMenu(wxMenu *menu, int x, int y)
1447 {
1448 menu->SetInvokingWindow(this);
1449 menu->UpdateUI();
1450 ClientToScreen( &x , &y ) ;
1451
1452 menu->MacBeforeDisplay( true ) ;
1453 long menuResult = ::PopUpMenuSelect((MenuHandle) menu->GetHMenu() ,y,x, 0) ;
1454 if ( HiWord(menuResult) != 0 )
1455 {
1456 MenuCommand id ;
1457 GetMenuItemCommandID( GetMenuHandle(HiWord(menuResult)) , LoWord(menuResult) , &id ) ;
1458 wxMenuItem* item = NULL ;
1459 wxMenu* realmenu ;
1460 item = menu->FindItem(id, &realmenu) ;
1461 if (item->IsCheckable())
1462 {
1463 item->Check( !item->IsChecked() ) ;
1464 }
1465 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
1466 }
1467 menu->MacAfterDisplay( true ) ;
1468
1469 menu->SetInvokingWindow(NULL);
1470
1471 return TRUE;
1472 }
1473 #endif
1474
1475 // ----------------------------------------------------------------------------
1476 // tooltips
1477 // ----------------------------------------------------------------------------
1478
1479 #if wxUSE_TOOLTIPS
1480
1481 void wxWindowMac::DoSetToolTip(wxToolTip *tooltip)
1482 {
1483 wxWindowBase::DoSetToolTip(tooltip);
1484
1485 if ( m_tooltip )
1486 m_tooltip->SetWindow(this);
1487 }
1488
1489 #endif // wxUSE_TOOLTIPS
1490
1491 void wxWindowMac::DoMoveWindow(int x, int y, int width, int height)
1492 {
1493 int former_x , former_y , former_w, former_h ;
1494 #if !TARGET_API_MAC_OSX
1495 DoGetPosition( &former_x , &former_y ) ;
1496 DoGetSize( &former_w , &former_h ) ;
1497 #else
1498 MacGetPositionAndSizeFromControl( former_x , former_y , former_w , former_h ) ;
1499 #endif
1500
1501 int actualWidth = width;
1502 int actualHeight = height;
1503 int actualX = x;
1504 int actualY = y;
1505
1506 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
1507 actualWidth = m_minWidth;
1508 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
1509 actualHeight = m_minHeight;
1510 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
1511 actualWidth = m_maxWidth;
1512 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
1513 actualHeight = m_maxHeight;
1514
1515 bool doMove = false ;
1516 bool doResize = false ;
1517
1518 if ( actualX != former_x || actualY != former_y )
1519 {
1520 doMove = true ;
1521 }
1522 if ( actualWidth != former_w || actualHeight != former_h )
1523 {
1524 doResize = true ;
1525 }
1526
1527 if ( doMove || doResize )
1528 {
1529 // we don't adjust twice for the origin
1530 Rect r = wxMacGetBoundsForControl(this , wxPoint( actualX,actualY), wxSize( actualWidth, actualHeight ) , false ) ;
1531 bool vis = IsControlVisible( (ControlRef) m_macControl ) ;
1532 #if TARGET_API_MAC_OSX
1533 // the HIViewSetFrame call itself should invalidate the areas, but when testing with the UnicodeTextCtrl it does not !
1534 if ( vis )
1535 SetControlVisibility( (ControlRef)m_macControl , false , true ) ;
1536 HIRect hir = { r.left , r.top , r.right - r.left , r.bottom - r.top } ;
1537 HIViewSetFrame ( (ControlRef) m_macControl , &hir ) ;
1538 if ( vis )
1539 SetControlVisibility( (ControlRef)m_macControl , true , true ) ;
1540 #else
1541 if ( vis )
1542 SetControlVisibility( (ControlRef)m_macControl , false , true ) ;
1543 SetControlBounds( (ControlRef) m_macControl , &r ) ;
1544 if ( vis )
1545 SetControlVisibility( (ControlRef)m_macControl , true , true ) ;
1546 #endif
1547 MacRepositionScrollBars() ;
1548 if ( doMove )
1549 {
1550 wxPoint point(actualX,actualY);
1551 wxMoveEvent event(point, m_windowId);
1552 event.SetEventObject(this);
1553 GetEventHandler()->ProcessEvent(event) ;
1554 }
1555 if ( doResize )
1556 {
1557 MacRepositionScrollBars() ;
1558 wxSize size(actualWidth, actualHeight);
1559 wxSizeEvent event(size, m_windowId);
1560 event.SetEventObject(this);
1561 GetEventHandler()->ProcessEvent(event);
1562 }
1563 }
1564
1565 }
1566
1567 wxSize wxWindowMac::DoGetBestSize() const
1568 {
1569 if ( m_macIsUserPane || IsTopLevel() )
1570 return wxWindowBase::DoGetBestSize() ;
1571
1572 Rect bestsize = { 0 , 0 , 0 , 0 } ;
1573 short baselineoffset ;
1574 int bestWidth, bestHeight ;
1575 ::GetBestControlRect( (ControlRef) m_macControl , &bestsize , &baselineoffset ) ;
1576
1577 if ( EmptyRect( &bestsize ) )
1578 {
1579 baselineoffset = 0;
1580 bestsize.left = bestsize.top = 0 ;
1581 bestsize.right = 16 ;
1582 bestsize.bottom = 16 ;
1583 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
1584 {
1585 bestsize.bottom = 16 ;
1586 }
1587 else if ( IsKindOf( CLASSINFO( wxSpinButton ) ) )
1588 {
1589 bestsize.bottom = 24 ;
1590 }
1591 else
1592 {
1593 // return wxWindowBase::DoGetBestSize() ;
1594 }
1595 }
1596
1597 bestWidth = bestsize.right - bestsize.left ;
1598 bestHeight = bestsize.bottom - bestsize.top ;
1599 if ( bestHeight < 10 )
1600 bestHeight = 13 ;
1601
1602 return wxSize(bestWidth, bestHeight);
1603 // return wxWindowBase::DoGetBestSize() ;
1604 }
1605
1606
1607 // set the size of the window: if the dimensions are positive, just use them,
1608 // but if any of them is equal to -1, it means that we must find the value for
1609 // it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
1610 // which case -1 is a valid value for x and y)
1611 //
1612 // If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
1613 // the width/height to best suit our contents, otherwise we reuse the current
1614 // width/height
1615 void wxWindowMac::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1616 {
1617 // get the current size and position...
1618 int currentX, currentY;
1619 GetPosition(&currentX, &currentY);
1620
1621 int currentW,currentH;
1622 GetSize(&currentW, &currentH);
1623
1624 // ... and don't do anything (avoiding flicker) if it's already ok
1625 if ( x == currentX && y == currentY &&
1626 width == currentW && height == currentH && ( height != -1 && width != -1 ) )
1627 {
1628 // TODO REMOVE
1629 MacRepositionScrollBars() ; // we might have a real position shift
1630 return;
1631 }
1632
1633 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1634 x = currentX;
1635 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1636 y = currentY;
1637
1638 AdjustForParentClientOrigin(x, y, sizeFlags);
1639
1640 wxSize size(-1, -1);
1641 if ( width == -1 )
1642 {
1643 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
1644 {
1645 size = DoGetBestSize();
1646 width = size.x;
1647 }
1648 else
1649 {
1650 // just take the current one
1651 width = currentW;
1652 }
1653 }
1654
1655 if ( height == -1 )
1656 {
1657 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
1658 {
1659 if ( size.x == -1 )
1660 {
1661 size = DoGetBestSize();
1662 }
1663 //else: already called DoGetBestSize() above
1664
1665 height = size.y;
1666 }
1667 else
1668 {
1669 // just take the current one
1670 height = currentH;
1671 }
1672 }
1673
1674 DoMoveWindow(x, y, width, height);
1675
1676 }
1677
1678 wxPoint wxWindowMac::GetClientAreaOrigin() const
1679 {
1680 RgnHandle rgn = NewRgn() ;
1681 Rect content ;
1682 GetControlRegion( (ControlRef) m_macControl , kControlContentMetaPart , rgn ) ;
1683 GetRegionBounds( rgn , &content ) ;
1684 DisposeRgn( rgn ) ;
1685 #if !TARGET_API_MAC_OSX
1686 // if the content rgn is empty / not supported
1687 // don't attempt to correct the coordinates to wxWindow relative ones
1688 if (!::EmptyRect( &content ) )
1689 {
1690 Rect structure ;
1691 GetControlBounds( (ControlRef) m_macControl , &structure ) ;
1692 OffsetRect( &content , -structure.left , -structure.top ) ;
1693 }
1694 #endif
1695
1696 return wxPoint( content.left + MacGetLeftBorderSize( ) , content.top + MacGetTopBorderSize( ) );
1697 }
1698
1699 void wxWindowMac::DoSetClientSize(int clientwidth, int clientheight)
1700 {
1701 if ( clientheight != -1 || clientheight != -1 )
1702 {
1703 int currentclientwidth , currentclientheight ;
1704 int currentwidth , currentheight ;
1705
1706 GetClientSize( &currentclientwidth , &currentclientheight ) ;
1707 GetSize( &currentwidth , &currentheight ) ;
1708
1709 DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth ,
1710 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
1711 }
1712 }
1713
1714 void wxWindowMac::SetTitle(const wxString& title)
1715 {
1716 m_label = wxStripMenuCodes(title) ;
1717
1718 if ( m_macControl )
1719 {
1720 UMASetControlTitle( (ControlRef) m_macControl , m_label , m_font.GetEncoding() ) ;
1721 }
1722 Refresh() ;
1723 }
1724
1725 wxString wxWindowMac::GetTitle() const
1726 {
1727 return m_label ;
1728 }
1729
1730 bool wxWindowMac::Show(bool show)
1731 {
1732 if ( !wxWindowBase::Show(show) )
1733 return FALSE;
1734
1735 // TODO use visibilityChanged Carbon Event for OSX
1736 bool former = MacIsReallyShown() ;
1737
1738 SetControlVisibility( (ControlRef) m_macControl , show , true ) ;
1739 if ( former != MacIsReallyShown() )
1740 MacPropagateVisibilityChanged() ;
1741 return TRUE;
1742 }
1743
1744 bool wxWindowMac::Enable(bool enable)
1745 {
1746 wxASSERT( m_macControl != NULL ) ;
1747 if ( !wxWindowBase::Enable(enable) )
1748 return FALSE;
1749
1750 bool former = MacIsReallyEnabled() ;
1751 #if TARGET_API_MAC_OSX
1752 if ( enable )
1753 EnableControl( (ControlRef) m_macControl ) ;
1754 else
1755 DisableControl( (ControlRef) m_macControl ) ;
1756 #else
1757 if ( enable )
1758 ActivateControl( (ControlRef) m_macControl ) ;
1759 else
1760 DeactivateControl( (ControlRef) m_macControl ) ;
1761 #endif
1762
1763 if ( former != MacIsReallyEnabled() )
1764 MacPropagateEnabledStateChanged() ;
1765 return TRUE;
1766 }
1767
1768 //
1769 // status change propagations (will be not necessary for OSX later )
1770 //
1771
1772 void wxWindowMac::MacPropagateVisibilityChanged()
1773 {
1774 #if !TARGET_API_MAC_OSX
1775 MacVisibilityChanged() ;
1776
1777 wxWindowListNode *node = GetChildren().GetFirst();
1778 while ( node )
1779 {
1780 wxWindowMac *child = node->GetData();
1781 if ( child->IsShown() )
1782 child->MacPropagateVisibilityChanged( ) ;
1783 node = node->GetNext();
1784 }
1785 #endif
1786 }
1787
1788 void wxWindowMac::MacPropagateEnabledStateChanged( )
1789 {
1790 #if !TARGET_API_MAC_OSX
1791 MacEnabledStateChanged() ;
1792
1793 wxWindowListNode *node = GetChildren().GetFirst();
1794 while ( node )
1795 {
1796 wxWindowMac *child = node->GetData();
1797 if ( child->IsEnabled() )
1798 child->MacPropagateEnabledStateChanged() ;
1799 node = node->GetNext();
1800 }
1801 #endif
1802 }
1803
1804 void wxWindowMac::MacPropagateHiliteChanged( )
1805 {
1806 #if !TARGET_API_MAC_OSX
1807 MacHiliteChanged() ;
1808
1809 wxWindowListNode *node = GetChildren().GetFirst();
1810 while ( node )
1811 {
1812 wxWindowMac *child = node->GetData();
1813 // if ( child->IsEnabled() )
1814 child->MacPropagateHiliteChanged() ;
1815 node = node->GetNext();
1816 }
1817 #endif
1818 }
1819
1820 //
1821 // status change notifications
1822 //
1823
1824 void wxWindowMac::MacVisibilityChanged()
1825 {
1826 }
1827
1828 void wxWindowMac::MacHiliteChanged()
1829 {
1830 }
1831
1832 void wxWindowMac::MacEnabledStateChanged()
1833 {
1834 }
1835
1836 //
1837 // status queries on the inherited window's state
1838 //
1839
1840 bool wxWindowMac::MacIsReallyShown()
1841 {
1842 // only under OSX the visibility of the TLW is taken into account
1843 #if TARGET_API_MAC_OSX
1844 return IsControlVisible( (ControlRef) m_macControl ) ;
1845 #else
1846 wxWindow* win = this ;
1847 while( win->IsShown() )
1848 {
1849 if ( win->IsTopLevel() )
1850 return true ;
1851
1852 win = win->GetParent() ;
1853 if ( win == NULL )
1854 return true ;
1855
1856 } ;
1857 return false ;
1858 #endif
1859 }
1860
1861 bool wxWindowMac::MacIsReallyEnabled()
1862 {
1863 #if TARGET_API_MAC_OSX
1864 return IsControlEnabled( (ControlRef) m_macControl ) ;
1865 #else
1866 return IsControlActive( (ControlRef) m_macControl ) ;
1867 #endif
1868 }
1869
1870 bool wxWindowMac::MacIsReallyHilited()
1871 {
1872 return IsControlActive( (ControlRef) m_macControl ) ;
1873 }
1874
1875 void wxWindowMac::MacFlashInvalidAreas()
1876 {
1877 #if TARGET_API_MAC_OSX
1878 HIViewFlashDirtyArea( (WindowRef) MacGetTopLevelWindowRef() ) ;
1879 #endif
1880 }
1881
1882 //
1883 //
1884 //
1885
1886 int wxWindowMac::GetCharHeight() const
1887 {
1888 wxClientDC dc ( (wxWindowMac*)this ) ;
1889 return dc.GetCharHeight() ;
1890 }
1891
1892 int wxWindowMac::GetCharWidth() const
1893 {
1894 wxClientDC dc ( (wxWindowMac*)this ) ;
1895 return dc.GetCharWidth() ;
1896 }
1897
1898 void wxWindowMac::GetTextExtent(const wxString& string, int *x, int *y,
1899 int *descent, int *externalLeading, const wxFont *theFont ) const
1900 {
1901 const wxFont *fontToUse = theFont;
1902 if ( !fontToUse )
1903 fontToUse = &m_font;
1904
1905 wxClientDC dc( (wxWindowMac*) this ) ;
1906 long lx,ly,ld,le ;
1907 dc.GetTextExtent( string , &lx , &ly , &ld, &le, (wxFont *)fontToUse ) ;
1908 if ( externalLeading )
1909 *externalLeading = le ;
1910 if ( descent )
1911 *descent = ld ;
1912 if ( x )
1913 *x = lx ;
1914 if ( y )
1915 *y = ly ;
1916 }
1917
1918 /*
1919 * Rect is given in client coordinates, for further reading, read wxTopLevelWindowMac::InvalidateRect
1920 * we always intersect with the entire window, not only with the client area
1921 */
1922
1923 void wxWindowMac::Refresh(bool eraseBack, const wxRect *rect)
1924 {
1925 #if TARGET_API_MAC_OSX
1926 if ( rect == NULL )
1927 HIViewSetNeedsDisplay( (ControlRef) m_macControl , true ) ;
1928 else
1929 {
1930 RgnHandle update = NewRgn() ;
1931 SetRectRgn( update , rect->x , rect->y , rect->x + rect->width , rect->y + rect->height ) ;
1932 SectRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , update , update ) ;
1933 wxPoint origin = GetClientAreaOrigin() ;
1934 OffsetRgn( update, origin.x , origin.y ) ;
1935 HIViewSetNeedsDisplayInRegion( (ControlRef) m_macControl , update , true ) ;
1936 }
1937 #else
1938 /*
1939 RgnHandle updateRgn = NewRgn() ;
1940 if ( rect == NULL )
1941 {
1942 CopyRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , updateRgn ) ;
1943 }
1944 else
1945 {
1946 SetRectRgn( updateRgn , rect->x , rect->y , rect->x + rect->width , rect->y + rect->height ) ;
1947 SectRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , updateRgn , updateRgn ) ;
1948 }
1949 InvalWindowRgn( (WindowRef) MacGetTopLevelWindowRef() , updateRgn ) ;
1950 DisposeRgn(updateRgn) ;
1951 */
1952 if ( IsControlVisible( (ControlRef) m_macControl ) )
1953 {
1954 SetControlVisibility( (ControlRef) m_macControl , false , false ) ;
1955 SetControlVisibility( (ControlRef) m_macControl , true , true ) ;
1956 }
1957 /*
1958 if ( MacGetTopLevelWindow() == NULL )
1959 return ;
1960
1961 if ( !IsControlVisible( (ControlRef) m_macControl ) )
1962 return ;
1963
1964 wxPoint client = GetClientAreaOrigin();
1965 int x1 = -client.x;
1966 int y1 = -client.y;
1967 int x2 = m_width - client.x;
1968 int y2 = m_height - client.y;
1969
1970 if (IsKindOf( CLASSINFO(wxButton)))
1971 {
1972 // buttons have an "aura"
1973 y1 -= 5;
1974 x1 -= 5;
1975 y2 += 5;
1976 x2 += 5;
1977 }
1978
1979 Rect clientrect = { y1, x1, y2, x2 };
1980
1981 if ( rect )
1982 {
1983 Rect r = { rect->y , rect->x , rect->y + rect->height , rect->x + rect->width } ;
1984 SectRect( &clientrect , &r , &clientrect ) ;
1985 }
1986
1987 if ( !EmptyRect( &clientrect ) )
1988 {
1989 int top = 0 , left = 0 ;
1990
1991 MacClientToRootWindow( &left , &top ) ;
1992 OffsetRect( &clientrect , left , top ) ;
1993
1994 MacGetTopLevelWindow()->MacInvalidate( &clientrect , eraseBack ) ;
1995 }
1996 */
1997 #endif
1998 }
1999
2000 void wxWindowMac::Freeze()
2001 {
2002 #if TARGET_API_MAC_OSX
2003 if ( !m_frozenness++ )
2004 {
2005 HIViewSetDrawingEnabled( (HIViewRef) m_macControl , false ) ;
2006 }
2007 #endif
2008 }
2009
2010 #if TARGET_API_MAC_OSX
2011 static void InvalidateControlAndChildren( HIViewRef control )
2012 {
2013 HIViewSetNeedsDisplay( control , true ) ;
2014 UInt16 childrenCount = 0 ;
2015 OSStatus err = CountSubControls( control , &childrenCount ) ;
2016 if ( err == errControlIsNotEmbedder )
2017 return ;
2018 wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") ) ;
2019
2020 for ( UInt16 i = childrenCount ; i >=1 ; --i )
2021 {
2022 HIViewRef child ;
2023 err = GetIndexedSubControl( control , i , & child ) ;
2024 if ( err == errControlIsNotEmbedder )
2025 return ;
2026 InvalidateControlAndChildren( child ) ;
2027 }
2028 }
2029 #endif
2030
2031 void wxWindowMac::Thaw()
2032 {
2033 #if TARGET_API_MAC_OSX
2034 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
2035
2036 if ( !--m_frozenness )
2037 {
2038 HIViewSetDrawingEnabled( (HIViewRef) m_macControl , true ) ;
2039 InvalidateControlAndChildren( (HIViewRef) m_macControl ) ;
2040 // HIViewSetNeedsDisplay( (HIViewRef) m_macControl , true ) ;
2041 }
2042 #endif
2043 }
2044
2045 void wxWindowMac::MacRedrawControl()
2046 {
2047 /*
2048 if ( (ControlRef) m_macControl && MacGetTopLevelWindowRef() && IsControlVisible( (ControlRef) m_macControl ) )
2049 {
2050 #if TARGET_API_MAC_CARBON
2051 Update() ;
2052 #else
2053 wxClientDC dc(this) ;
2054 wxMacPortSetter helper(&dc) ;
2055 wxMacWindowClipper clipper(this) ;
2056 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
2057 UMADrawControl( (ControlRef) m_macControl ) ;
2058 #endif
2059 }
2060 */
2061 }
2062
2063 /* TODO
2064 void wxWindowMac::OnPaint(wxPaintEvent& event)
2065 {
2066 // why don't we skip that here ?
2067 }
2068 */
2069
2070 wxWindowMac *wxGetActiveWindow()
2071 {
2072 // actually this is a windows-only concept
2073 return NULL;
2074 }
2075
2076 // Coordinates relative to the window
2077 void wxWindowMac::WarpPointer (int x_pos, int y_pos)
2078 {
2079 // We really don't move the mouse programmatically under Mac.
2080 }
2081
2082 void wxWindowMac::OnEraseBackground(wxEraseEvent& event)
2083 {
2084 if ( m_macBackgroundBrush.Ok() == false || m_macBackgroundBrush.GetStyle() == wxTRANSPARENT )
2085 {
2086 event.Skip() ;
2087 }
2088 else
2089 event.GetDC()->Clear() ;
2090 }
2091
2092 void wxWindowMac::OnNcPaint( wxNcPaintEvent& event )
2093 {
2094 wxWindowDC dc(this) ;
2095 wxMacPortSetter helper(&dc) ;
2096
2097 MacPaintBorders( dc.m_macLocalOrigin.x , dc.m_macLocalOrigin.y) ;
2098 }
2099
2100 int wxWindowMac::GetScrollPos(int orient) const
2101 {
2102 if ( orient == wxHORIZONTAL )
2103 {
2104 if ( m_hScrollBar )
2105 return m_hScrollBar->GetThumbPosition() ;
2106 }
2107 else
2108 {
2109 if ( m_vScrollBar )
2110 return m_vScrollBar->GetThumbPosition() ;
2111 }
2112 return 0;
2113 }
2114
2115 // This now returns the whole range, not just the number
2116 // of positions that we can scroll.
2117 int wxWindowMac::GetScrollRange(int orient) const
2118 {
2119 if ( orient == wxHORIZONTAL )
2120 {
2121 if ( m_hScrollBar )
2122 return m_hScrollBar->GetRange() ;
2123 }
2124 else
2125 {
2126 if ( m_vScrollBar )
2127 return m_vScrollBar->GetRange() ;
2128 }
2129 return 0;
2130 }
2131
2132 int wxWindowMac::GetScrollThumb(int orient) const
2133 {
2134 if ( orient == wxHORIZONTAL )
2135 {
2136 if ( m_hScrollBar )
2137 return m_hScrollBar->GetThumbSize() ;
2138 }
2139 else
2140 {
2141 if ( m_vScrollBar )
2142 return m_vScrollBar->GetThumbSize() ;
2143 }
2144 return 0;
2145 }
2146
2147 void wxWindowMac::SetScrollPos(int orient, int pos, bool refresh)
2148 {
2149 if ( orient == wxHORIZONTAL )
2150 {
2151 if ( m_hScrollBar )
2152 m_hScrollBar->SetThumbPosition( pos ) ;
2153 }
2154 else
2155 {
2156 if ( m_vScrollBar )
2157 m_vScrollBar->SetThumbPosition( pos ) ;
2158 }
2159 }
2160
2161 void wxWindowMac::MacPaintBorders( int left , int top )
2162 {
2163 if( IsTopLevel() )
2164 return ;
2165
2166 int major,minor;
2167 wxGetOsVersion( &major, &minor );
2168
2169 RGBColor white = { 0xFFFF, 0xFFFF , 0xFFFF } ;
2170 RGBColor face = { 0xDDDD, 0xDDDD , 0xDDDD } ;
2171
2172 RGBColor darkShadow = { 0x0000, 0x0000 , 0x0000 } ;
2173 RGBColor lightShadow = { 0x4444, 0x4444 , 0x4444 } ;
2174 // OS X has lighter border edges than classic:
2175 if (major >= 10)
2176 {
2177 darkShadow.red = 0x8E8E;
2178 darkShadow.green = 0x8E8E;
2179 darkShadow.blue = 0x8E8E;
2180 lightShadow.red = 0xBDBD;
2181 lightShadow.green = 0xBDBD;
2182 lightShadow.blue = 0xBDBD;
2183 }
2184
2185 PenNormal() ;
2186
2187 int w , h ;
2188 GetSize( &w , &h ) ;
2189 if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
2190 {
2191 #if wxMAC_USE_THEME_BORDER
2192 Rect rect = { top , left , m_height + top , m_width + left } ;
2193 SInt32 border = 0 ;
2194 /*
2195 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2196 InsetRect( &rect , border , border );
2197 DrawThemeListBoxFrame(&rect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
2198 */
2199
2200 DrawThemePrimaryGroup(&rect ,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
2201 #else
2202 bool sunken = HasFlag( wxSUNKEN_BORDER ) ;
2203 RGBForeColor( &face );
2204 MoveTo( left + 0 , top + h - 2 );
2205 LineTo( left + 0 , top + 0 );
2206 LineTo( left + w - 2 , top + 0 );
2207
2208 MoveTo( left + 2 , top + h - 3 );
2209 LineTo( left + w - 3 , top + h - 3 );
2210 LineTo( left + w - 3 , top + 2 );
2211
2212 RGBForeColor( sunken ? &face : &darkShadow );
2213 MoveTo( left + 0 , top + h - 1 );
2214 LineTo( left + w - 1 , top + h - 1 );
2215 LineTo( left + w - 1 , top + 0 );
2216
2217 RGBForeColor( sunken ? &lightShadow : &white );
2218 MoveTo( left + 1 , top + h - 3 );
2219 LineTo( left + 1, top + 1 );
2220 LineTo( left + w - 3 , top + 1 );
2221
2222 RGBForeColor( sunken ? &white : &lightShadow );
2223 MoveTo( left + 1 , top + h - 2 );
2224 LineTo( left + w - 2 , top + h - 2 );
2225 LineTo( left + w - 2 , top + 1 );
2226
2227 RGBForeColor( sunken ? &darkShadow : &face );
2228 MoveTo( left + 2 , top + h - 4 );
2229 LineTo( left + 2 , top + 2 );
2230 LineTo( left + w - 4 , top + 2 );
2231 #endif
2232 }
2233 else if (HasFlag(wxSIMPLE_BORDER))
2234 {
2235 Rect rect = { top , left , h + top , w + left } ;
2236 RGBForeColor( &darkShadow ) ;
2237 FrameRect( &rect ) ;
2238 }
2239 }
2240
2241 void wxWindowMac::RemoveChild( wxWindowBase *child )
2242 {
2243 if ( child == m_hScrollBar )
2244 m_hScrollBar = NULL ;
2245 if ( child == m_vScrollBar )
2246 m_vScrollBar = NULL ;
2247
2248 wxWindowBase::RemoveChild( child ) ;
2249 }
2250
2251 // New function that will replace some of the above.
2252 void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible,
2253 int range, bool refresh)
2254 {
2255 if ( orient == wxHORIZONTAL )
2256 {
2257 if ( m_hScrollBar )
2258 {
2259 if ( range == 0 || thumbVisible >= range )
2260 {
2261 if ( m_hScrollBar->IsShown() )
2262 m_hScrollBar->Show(false) ;
2263 }
2264 else
2265 {
2266 if ( !m_hScrollBar->IsShown() )
2267 m_hScrollBar->Show(true) ;
2268 m_hScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2269 }
2270 }
2271 }
2272 else
2273 {
2274 if ( m_vScrollBar )
2275 {
2276 if ( range == 0 || thumbVisible >= range )
2277 {
2278 if ( m_vScrollBar->IsShown() )
2279 m_vScrollBar->Show(false) ;
2280 }
2281 else
2282 {
2283 if ( !m_vScrollBar->IsShown() )
2284 m_vScrollBar->Show(true) ;
2285 m_vScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2286 }
2287 }
2288 }
2289 MacRepositionScrollBars() ;
2290 }
2291
2292 // Does a physical scroll
2293 void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect)
2294 {
2295 if( dx == 0 && dy ==0 )
2296 return ;
2297
2298
2299 {
2300
2301 int width , height ;
2302 GetClientSize( &width , &height ) ;
2303 #if TARGET_API_MAC_OSX
2304 // note there currently is a bug in OSX which makes inefficient refreshes in case an entire control
2305 // area is scrolled, this does not occur if width and height are 2 pixels less,
2306 // TODO write optimal workaround
2307 HIRect scrollrect = CGRectMake( MacGetLeftBorderSize() , MacGetTopBorderSize() , width , height ) ;
2308 if ( rect )
2309 {
2310 HIRect scrollarea = CGRectMake( rect->x , rect->y , rect->width , rect->height) ;
2311 scrollrect = CGRectIntersection( scrollrect , scrollarea ) ;
2312 }
2313 if ( HIViewGetNeedsDisplay( (ControlRef) m_macControl ) )
2314 {
2315 // becuase HIViewScrollRect does not scroll the already invalidated area we have two options
2316 // either immediate redraw or full invalidate
2317 #if 1
2318 // is the better overall solution, as it does not slow down scrolling
2319 HIViewSetNeedsDisplay( (ControlRef) m_macControl , true ) ;
2320 #else
2321 // this would be the preferred version for fast drawing controls
2322 if( UMAGetSystemVersion() < 0x1030 )
2323 Update() ;
2324 else
2325 HIViewRender((ControlRef) m_macControl) ;
2326 #endif
2327 }
2328 HIViewScrollRect ( (ControlRef) m_macControl , &scrollrect , dx ,dy ) ;
2329 #else
2330
2331 wxPoint pos;
2332 pos.x = pos.y = 0;
2333
2334 Rect scrollrect;
2335 RgnHandle updateRgn = NewRgn() ;
2336
2337 {
2338 wxClientDC dc(this) ;
2339 wxMacPortSetter helper(&dc) ;
2340
2341 GetControlBounds( (ControlRef) m_macControl, &scrollrect);
2342 scrollrect.top += MacGetTopBorderSize() ;
2343 scrollrect.left += MacGetLeftBorderSize() ;
2344 scrollrect.bottom = scrollrect.top + height ;
2345 scrollrect.right = scrollrect.left + width ;
2346
2347 if ( rect )
2348 {
2349 Rect r = { dc.YLOG2DEVMAC(rect->y) , dc.XLOG2DEVMAC(rect->x) , dc.YLOG2DEVMAC(rect->y + rect->height) ,
2350 dc.XLOG2DEVMAC(rect->x + rect->width) } ;
2351 SectRect( &scrollrect , &r , &scrollrect ) ;
2352 }
2353 ScrollRect( &scrollrect , dx , dy , updateRgn ) ;
2354 }
2355 // ScrollWindowRect( (WindowRef) MacGetTopLevelWindowRef() , &scrollrect , dx , dy , kScrollWindowInvalidate, updateRgn ) ;
2356 #endif
2357 }
2358
2359 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
2360 {
2361 wxWindowMac *child = node->GetData();
2362 if (child == m_vScrollBar) continue;
2363 if (child == m_hScrollBar) continue;
2364 if (child->IsTopLevel()) continue;
2365
2366 int x,y;
2367 child->GetPosition( &x, &y );
2368 int w,h;
2369 child->GetSize( &w, &h );
2370 if (rect)
2371 {
2372 wxRect rc(x,y,w,h);
2373 if (rect->Intersects(rc))
2374 child->SetSize( x+dx, y+dy, w, h );
2375 }
2376 else
2377 {
2378 child->SetSize( x+dx, y+dy, w, h );
2379 }
2380 }
2381 }
2382
2383 void wxWindowMac::MacOnScroll(wxScrollEvent &event )
2384 {
2385 if ( event.m_eventObject == m_vScrollBar || event.m_eventObject == m_hScrollBar )
2386 {
2387 wxScrollWinEvent wevent;
2388 wevent.SetPosition(event.GetPosition());
2389 wevent.SetOrientation(event.GetOrientation());
2390 wevent.m_eventObject = this;
2391
2392 if (event.m_eventType == wxEVT_SCROLL_TOP)
2393 wevent.m_eventType = wxEVT_SCROLLWIN_TOP;
2394 else if (event.m_eventType == wxEVT_SCROLL_BOTTOM)
2395 wevent.m_eventType = wxEVT_SCROLLWIN_BOTTOM;
2396 else if (event.m_eventType == wxEVT_SCROLL_LINEUP)
2397 wevent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
2398 else if (event.m_eventType == wxEVT_SCROLL_LINEDOWN)
2399 wevent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
2400 else if (event.m_eventType == wxEVT_SCROLL_PAGEUP)
2401 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEUP;
2402 else if (event.m_eventType == wxEVT_SCROLL_PAGEDOWN)
2403 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEDOWN;
2404 else if (event.m_eventType == wxEVT_SCROLL_THUMBTRACK)
2405 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBTRACK;
2406 else if (event.m_eventType == wxEVT_SCROLL_THUMBRELEASE)
2407 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBRELEASE;
2408
2409 GetEventHandler()->ProcessEvent(wevent);
2410 }
2411 }
2412
2413 // Get the window with the focus
2414 wxWindowMac *wxWindowBase::FindFocus()
2415 {
2416 ControlRef control ;
2417 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
2418 return wxFindControlFromMacControl( control ) ;
2419 }
2420
2421 void wxWindowMac::OnSetFocus(wxFocusEvent& event)
2422 {
2423 // panel wants to track the window which was the last to have focus in it,
2424 // so we want to set ourselves as the window which last had focus
2425 //
2426 // notice that it's also important to do it upwards the tree becaus
2427 // otherwise when the top level panel gets focus, it won't set it back to
2428 // us, but to some other sibling
2429
2430 // CS:don't know if this is still needed:
2431 //wxChildFocusEvent eventFocus(this);
2432 //(void)GetEventHandler()->ProcessEvent(eventFocus);
2433
2434 event.Skip();
2435 }
2436
2437 void wxWindowMac::OnInternalIdle()
2438 {
2439 // This calls the UI-update mechanism (querying windows for
2440 // menu/toolbar/control state information)
2441 if (wxUpdateUIEvent::CanUpdate(this))
2442 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2443 }
2444
2445 // Raise the window to the top of the Z order
2446 void wxWindowMac::Raise()
2447 {
2448 #if TARGET_API_MAC_OSX
2449 HIViewSetZOrder((ControlRef)m_macControl,kHIViewZOrderAbove, NULL) ;
2450 #endif
2451 }
2452
2453 // Lower the window to the bottom of the Z order
2454 void wxWindowMac::Lower()
2455 {
2456 #if TARGET_API_MAC_OSX
2457 HIViewSetZOrder((ControlRef)m_macControl,kHIViewZOrderBelow, NULL) ;
2458 #endif
2459 }
2460
2461
2462 // static wxWindow *gs_lastWhich = NULL;
2463
2464 bool wxWindowMac::MacSetupCursor( const wxPoint& pt)
2465 {
2466 // first trigger a set cursor event
2467
2468 wxPoint clientorigin = GetClientAreaOrigin() ;
2469 wxSize clientsize = GetClientSize() ;
2470 wxCursor cursor ;
2471 if ( wxRect2DInt( clientorigin.x , clientorigin.y , clientsize.x , clientsize.y ).Contains( wxPoint2DInt( pt ) ) )
2472 {
2473 wxSetCursorEvent event( pt.x , pt.y );
2474
2475 bool processedEvtSetCursor = GetEventHandler()->ProcessEvent(event);
2476 if ( processedEvtSetCursor && event.HasCursor() )
2477 {
2478 cursor = event.GetCursor() ;
2479 }
2480 else
2481 {
2482
2483 // the test for processedEvtSetCursor is here to prevent using m_cursor
2484 // if the user code caught EVT_SET_CURSOR() and returned nothing from
2485 // it - this is a way to say that our cursor shouldn't be used for this
2486 // point
2487 if ( !processedEvtSetCursor && m_cursor.Ok() )
2488 {
2489 cursor = m_cursor ;
2490 }
2491 if ( wxIsBusy() )
2492 {
2493 }
2494 else
2495 {
2496 if ( !GetParent() )
2497 cursor = *wxSTANDARD_CURSOR ;
2498 }
2499 }
2500 if ( cursor.Ok() )
2501 cursor.MacInstall() ;
2502 }
2503 return cursor.Ok() ;
2504 }
2505
2506 wxString wxWindowMac::MacGetToolTipString( wxPoint &pt )
2507 {
2508 if ( m_tooltip )
2509 {
2510 return m_tooltip->GetTip() ;
2511 }
2512 return wxEmptyString ;
2513 }
2514
2515 void wxWindowMac::Update()
2516 {
2517 #if TARGET_API_MAC_OSX
2518 WindowRef window = (WindowRef)MacGetTopLevelWindowRef() ;
2519 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
2520 // for composited windows this also triggers a redraw of all
2521 // invalid views in the window
2522 if( UMAGetSystemVersion() >= 0x1030 )
2523 HIWindowFlush(window) ;
2524 else
2525 #endif
2526 {
2527 // the only way to trigger the redrawing on earlier systems is to call
2528 // ReceiveNextEvent
2529
2530 EventRef currentEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
2531 UInt32 currentEventClass = 0 ;
2532 UInt32 currentEventKind = 0 ;
2533 if ( currentEvent != NULL )
2534 {
2535 currentEventClass = ::GetEventClass( currentEvent ) ;
2536 currentEventKind = ::GetEventKind( currentEvent ) ;
2537 }
2538 if ( currentEventClass != kEventClassMenu )
2539 {
2540 // when tracking a menu, strange redraw errors occur if we flush now, so leave..
2541
2542 EventRef theEvent;
2543 OSStatus status = noErr ;
2544 status = ReceiveNextEvent( 0 , NULL , kEventDurationNoWait , false , &theEvent ) ;
2545 }
2546 else
2547 HIViewSetNeedsDisplay( (ControlRef) m_macControl , true ) ;
2548 }
2549 #else
2550 ::Draw1Control( (ControlRef) m_macControl ) ;
2551 #endif
2552 }
2553
2554 wxTopLevelWindowMac* wxWindowMac::MacGetTopLevelWindow() const
2555 {
2556 wxTopLevelWindowMac* win = NULL ;
2557 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
2558 if ( window )
2559 {
2560 win = wxFindWinFromMacWindow( window ) ;
2561 }
2562 return win ;
2563 }
2564 wxRegion wxWindowMac::MacGetVisibleRegion( bool includeOuterStructures )
2565 {
2566
2567 Rect r ;
2568 RgnHandle visRgn = NewRgn() ;
2569 RgnHandle tempRgn = NewRgn() ;
2570 if ( IsControlVisible( (ControlRef) m_macControl ) )
2571 {
2572 GetControlBounds( (ControlRef) m_macControl , &r ) ;
2573 if (! MacGetTopLevelWindow()->MacUsesCompositing() )
2574 {
2575 MacRootWindowToWindow( &r.left , & r.top ) ;
2576 MacRootWindowToWindow( &r.right , & r.bottom ) ;
2577 }
2578 else
2579 {
2580 r.right -= r.left ;
2581 r.bottom -= r.top ;
2582 r.left = 0 ;
2583 r.top = 0 ;
2584 }
2585 if ( includeOuterStructures )
2586 InsetRect( &r , -3 , -3 ) ;
2587 RectRgn( visRgn , &r ) ;
2588 if ( !IsTopLevel() )
2589 {
2590 wxWindow* child = this ;
2591 wxWindow* parent = child->GetParent() ;
2592 while( parent )
2593 {
2594 int x , y ;
2595 wxSize size ;
2596 // we have to find a better clipping algorithm here, in order not to clip things
2597 // positioned like status and toolbar
2598 if ( 1 /* parent->IsTopLevel() && child->IsKindOf( CLASSINFO( wxToolBar ) ) */ )
2599 {
2600 size = parent->GetSize() ;
2601 x = y = 0 ;
2602 }
2603 else
2604 {
2605 size = parent->GetClientSize() ;
2606 wxPoint origin = parent->GetClientAreaOrigin() ;
2607 x = origin.x ;
2608 y = origin.y ;
2609 }
2610 parent->MacWindowToRootWindow( &x, &y ) ;
2611 MacRootWindowToWindow( &x , &y ) ;
2612
2613 SetRectRgn( tempRgn ,
2614 x + parent->MacGetLeftBorderSize() , y + parent->MacGetTopBorderSize() ,
2615 x + size.x - parent->MacGetRightBorderSize(),
2616 y + size.y - parent->MacGetBottomBorderSize()) ;
2617
2618 SectRgn( visRgn , tempRgn , visRgn ) ;
2619 if ( parent->IsTopLevel() )
2620 break ;
2621 child = parent ;
2622 parent = child->GetParent() ;
2623 }
2624 }
2625 }
2626
2627 wxRegion vis = visRgn ;
2628 DisposeRgn( visRgn ) ;
2629 DisposeRgn( tempRgn ) ;
2630 return vis ;
2631 }
2632
2633 /*
2634 This function must not change the updatergn !
2635 */
2636 bool wxWindowMac::MacDoRedraw( WXHRGN updatergnr , long time )
2637 {
2638 RgnHandle updatergn = (RgnHandle) updatergnr ;
2639 bool handled = false ;
2640
2641 // calculate a client-origin version of the update rgn and set m_updateRegion to that
2642 {
2643 RgnHandle newupdate = NewRgn() ;
2644 wxSize point = GetClientSize() ;
2645 wxPoint origin = GetClientAreaOrigin() ;
2646 SetRectRgn( newupdate , origin.x , origin.y , origin.x + point.x , origin.y+point.y ) ;
2647 SectRgn( newupdate , updatergn , newupdate ) ;
2648 OffsetRgn( newupdate , -origin.x , -origin.y ) ;
2649 m_updateRegion = newupdate ;
2650 DisposeRgn( newupdate ) ;
2651 }
2652
2653 if ( !EmptyRgn(updatergn) )
2654 {
2655 wxWindowDC dc(this);
2656 if (!EmptyRgn(updatergn))
2657 dc.SetClippingRegion(wxRegion(updatergn));
2658
2659 wxEraseEvent eevent( GetId(), &dc );
2660 eevent.SetEventObject( this );
2661 GetEventHandler()->ProcessEvent( eevent );
2662
2663 if ( !m_updateRegion.Empty() )
2664 {
2665 // paint the window itself
2666 wxPaintEvent event;
2667 event.m_timeStamp = time ;
2668 event.SetEventObject(this);
2669 handled = GetEventHandler()->ProcessEvent(event);
2670
2671 // paint custom borders
2672 wxNcPaintEvent eventNc( GetId() );
2673 eventNc.SetEventObject( this );
2674 GetEventHandler()->ProcessEvent( eventNc );
2675 }
2676 }
2677 return handled ;
2678 }
2679
2680 void wxWindowMac::MacRedraw( WXHRGN updatergnr , long time, bool erase)
2681 {
2682 RgnHandle updatergn = (RgnHandle) updatergnr ;
2683 // updatergn is always already clipped to our boundaries
2684 // if we are in compositing mode then it is in relative to the upper left of the control
2685 // if we are in non-compositing, then it is relatvie to the uppder left of the content area
2686 // of the toplevel window
2687 // it is in window coordinates, not in client coordinates
2688
2689 // ownUpdateRgn is the area that this window has to repaint, it is in window coordinates
2690 RgnHandle ownUpdateRgn = NewRgn() ;
2691 CopyRgn( updatergn , ownUpdateRgn ) ;
2692
2693 if ( MacGetTopLevelWindow()->MacUsesCompositing() == false )
2694 {
2695 Rect bounds;
2696 UMAGetControlBoundsInWindowCoords( (ControlRef)m_macControl, &bounds );
2697 RgnHandle controlRgn = NewRgn();
2698 RectRgn( controlRgn, &bounds );
2699 //KO: This sets the ownUpdateRgn to the area of this control that is inside
2700 // the window update region
2701 SectRgn( ownUpdateRgn, controlRgn, ownUpdateRgn );
2702 DisposeRgn( controlRgn );
2703
2704 //KO: convert ownUpdateRgn to local coordinates
2705 OffsetRgn( ownUpdateRgn, -bounds.left, -bounds.top );
2706 }
2707
2708 MacDoRedraw( ownUpdateRgn , time ) ;
2709 DisposeRgn( ownUpdateRgn ) ;
2710
2711 }
2712
2713 WXWindow wxWindowMac::MacGetTopLevelWindowRef() const
2714 {
2715 wxWindowMac *iter = (wxWindowMac*)this ;
2716
2717 while( iter )
2718 {
2719 if ( iter->IsTopLevel() )
2720 return ((wxTopLevelWindow*)iter)->MacGetWindowRef() ;
2721
2722 iter = iter->GetParent() ;
2723 }
2724 wxASSERT_MSG( 1 , wxT("No valid mac root window") ) ;
2725 return NULL ;
2726 }
2727
2728 void wxWindowMac::MacCreateScrollBars( long style )
2729 {
2730 wxASSERT_MSG( m_vScrollBar == NULL && m_hScrollBar == NULL , wxT("attempt to create window twice") ) ;
2731
2732 bool hasBoth = ( style & wxVSCROLL ) && ( style & wxHSCROLL ) ;
2733 int adjust = hasBoth ? MAC_SCROLLBAR_SIZE - 1: 0 ;
2734 int width, height ;
2735 GetClientSize( &width , &height ) ;
2736
2737 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
2738 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
2739 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
2740 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
2741
2742 m_vScrollBar = new wxScrollBar(this, wxWINDOW_VSCROLL, vPoint,
2743 vSize , wxVERTICAL);
2744
2745 if ( style & wxVSCROLL )
2746 {
2747
2748 }
2749 else
2750 {
2751 m_vScrollBar->Show(false) ;
2752 }
2753 m_hScrollBar = new wxScrollBar(this, wxWINDOW_HSCROLL, hPoint,
2754 hSize , wxHORIZONTAL);
2755 if ( style & wxHSCROLL )
2756 {
2757 }
2758 else
2759 {
2760 m_hScrollBar->Show(false) ;
2761 }
2762
2763 // because the create does not take into account the client area origin
2764 MacRepositionScrollBars() ; // we might have a real position shift
2765 }
2766
2767 void wxWindowMac::MacRepositionScrollBars()
2768 {
2769 bool hasBoth = ( m_hScrollBar && m_hScrollBar->IsShown()) && ( m_vScrollBar && m_vScrollBar->IsShown()) ;
2770 int adjust = hasBoth ? MAC_SCROLLBAR_SIZE - 1 : 0 ;
2771
2772 // get real client area
2773
2774 int width ;
2775 int height ;
2776 GetSize( &width , &height ) ;
2777
2778 width -= MacGetLeftBorderSize() + MacGetRightBorderSize();
2779 height -= MacGetTopBorderSize() + MacGetBottomBorderSize();
2780
2781 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
2782 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
2783 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
2784 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
2785
2786 int x = 0 ;
2787 int y = 0 ;
2788 int w ;
2789 int h ;
2790 GetSize( &w , &h ) ;
2791
2792 MacClientToRootWindow( &x , &y ) ;
2793 MacClientToRootWindow( &w , &h ) ;
2794
2795 wxWindowMac *iter = (wxWindowMac*)this ;
2796
2797 int totW = 10000 , totH = 10000;
2798 while( iter )
2799 {
2800 if ( iter->IsTopLevel() )
2801 {
2802 iter->GetSize( &totW , &totH ) ;
2803 break ;
2804 }
2805
2806 iter = iter->GetParent() ;
2807 }
2808
2809 if ( x == 0 )
2810 {
2811 hPoint.x = -1 ;
2812 hSize.x += 1 ;
2813 }
2814 if ( y == 0 )
2815 {
2816 vPoint.y = -1 ;
2817 vSize.y += 1 ;
2818 }
2819
2820 if ( w-x >= totW )
2821 {
2822 hSize.x += 1 ;
2823 vPoint.x += 1 ;
2824 }
2825
2826 if ( h-y >= totH )
2827 {
2828 vSize.y += 1 ;
2829 hPoint.y += 1 ;
2830 }
2831
2832 if ( m_vScrollBar )
2833 {
2834 m_vScrollBar->SetSize( vPoint.x , vPoint.y, vSize.x, vSize.y , wxSIZE_ALLOW_MINUS_ONE);
2835 }
2836 if ( m_hScrollBar )
2837 {
2838 m_hScrollBar->SetSize( hPoint.x , hPoint.y, hSize.x, hSize.y, wxSIZE_ALLOW_MINUS_ONE);
2839 }
2840 }
2841
2842 bool wxWindowMac::AcceptsFocus() const
2843 {
2844 return MacCanFocus() && wxWindowBase::AcceptsFocus();
2845 }
2846
2847 void wxWindowMac::MacSuperChangedPosition()
2848 {
2849 // only window-absolute structures have to be moved i.e. controls
2850
2851 wxWindowListNode *node = GetChildren().GetFirst();
2852 while ( node )
2853 {
2854 wxWindowMac *child = node->GetData();
2855 child->MacSuperChangedPosition() ;
2856 node = node->GetNext();
2857 }
2858 }
2859
2860 void wxWindowMac::MacTopLevelWindowChangedPosition()
2861 {
2862 // only screen-absolute structures have to be moved i.e. glcanvas
2863
2864 wxWindowListNode *node = GetChildren().GetFirst();
2865 while ( node )
2866 {
2867 wxWindowMac *child = node->GetData();
2868 child->MacTopLevelWindowChangedPosition() ;
2869 node = node->GetNext();
2870 }
2871 }
2872
2873 long wxWindowMac::MacGetLeftBorderSize( ) const
2874 {
2875 if( IsTopLevel() )
2876 return 0 ;
2877
2878 if (m_windowStyle & wxRAISED_BORDER || m_windowStyle & wxSUNKEN_BORDER )
2879 {
2880 SInt32 border = 3 ;
2881 #if wxMAC_USE_THEME_BORDER
2882 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2883 #endif
2884 return border ;
2885 }
2886 else if ( m_windowStyle &wxDOUBLE_BORDER)
2887 {
2888 SInt32 border = 3 ;
2889 #if wxMAC_USE_THEME_BORDER
2890 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2891 #endif
2892 return border ;
2893 }
2894 else if (m_windowStyle &wxSIMPLE_BORDER)
2895 {
2896 return 1 ;
2897 }
2898 return 0 ;
2899 }
2900
2901 long wxWindowMac::MacGetRightBorderSize( ) const
2902 {
2903 // they are all symmetric in mac themes
2904 return MacGetLeftBorderSize() ;
2905 }
2906
2907 long wxWindowMac::MacGetTopBorderSize( ) const
2908 {
2909 // they are all symmetric in mac themes
2910 return MacGetLeftBorderSize() ;
2911 }
2912
2913 long wxWindowMac::MacGetBottomBorderSize( ) const
2914 {
2915 // they are all symmetric in mac themes
2916 return MacGetLeftBorderSize() ;
2917 }
2918
2919 long wxWindowMac::MacRemoveBordersFromStyle( long style )
2920 {
2921 return style & ~( wxDOUBLE_BORDER | wxSUNKEN_BORDER | wxRAISED_BORDER | wxBORDER | wxSTATIC_BORDER ) ;
2922 }
2923
2924 // Find the wxWindowMac at the current mouse position, returning the mouse
2925 // position.
2926 wxWindowMac* wxFindWindowAtPointer(wxPoint& pt)
2927 {
2928 pt = wxGetMousePosition();
2929 wxWindowMac* found = wxFindWindowAtPoint(pt);
2930 return found;
2931 }
2932
2933 // Get the current mouse position.
2934 wxPoint wxGetMousePosition()
2935 {
2936 int x, y;
2937 wxGetMousePosition(& x, & y);
2938 return wxPoint(x, y);
2939 }
2940
2941 void wxWindowMac::OnMouseEvent( wxMouseEvent &event )
2942 {
2943 if ( event.GetEventType() == wxEVT_RIGHT_DOWN )
2944 {
2945 // copied from wxGTK : CS
2946 // generate a "context menu" event: this is similar to wxEVT_RIGHT_DOWN
2947 // except that:
2948 //
2949 // (a) it's a command event and so is propagated to the parent
2950 // (b) under MSW it can be generated from kbd too
2951 // (c) it uses screen coords (because of (a))
2952 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
2953 this->GetId(),
2954 this->ClientToScreen(event.GetPosition()));
2955 if ( ! GetEventHandler()->ProcessEvent(evtCtx) )
2956 event.Skip() ;
2957 }
2958 else
2959 {
2960 event.Skip() ;
2961 }
2962 }
2963
2964 void wxWindowMac::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED( mouseStillDown ) )
2965 {
2966 }
2967
2968 Rect wxMacGetBoundsForControl( wxWindow* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
2969 {
2970 int x ,y , w ,h ;
2971
2972 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin) ;
2973 Rect bounds = { y , x , y+h , x+w };
2974 return bounds ;
2975 }
2976
2977 wxInt32 wxWindowMac::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
2978 {
2979 return eventNotHandledErr ;
2980 }
2981
2982