]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/window.cpp
Needs a foreground colour
[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 ClientToScreen( &x , &y ) ;
1443
1444 menu->MacBeforeDisplay( true ) ;
1445 long menuResult = ::PopUpMenuSelect((MenuHandle) menu->GetHMenu() ,y,x, 0) ;
1446 if ( HiWord(menuResult) != 0 )
1447 {
1448 MenuCommand id ;
1449 GetMenuItemCommandID( GetMenuHandle(HiWord(menuResult)) , LoWord(menuResult) , &id ) ;
1450 wxMenuItem* item = NULL ;
1451 wxMenu* realmenu ;
1452 item = menu->FindItem(id, &realmenu) ;
1453 if (item->IsCheckable())
1454 {
1455 item->Check( !item->IsChecked() ) ;
1456 }
1457 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
1458 }
1459 menu->MacAfterDisplay( true ) ;
1460
1461 menu->SetInvokingWindow(NULL);
1462
1463 return TRUE;
1464 }
1465 #endif
1466
1467 // ----------------------------------------------------------------------------
1468 // tooltips
1469 // ----------------------------------------------------------------------------
1470
1471 #if wxUSE_TOOLTIPS
1472
1473 void wxWindowMac::DoSetToolTip(wxToolTip *tooltip)
1474 {
1475 wxWindowBase::DoSetToolTip(tooltip);
1476
1477 if ( m_tooltip )
1478 m_tooltip->SetWindow(this);
1479 }
1480
1481 #endif // wxUSE_TOOLTIPS
1482
1483 void wxWindowMac::DoMoveWindow(int x, int y, int width, int height)
1484 {
1485 int former_x , former_y , former_w, former_h ;
1486 #if !TARGET_API_MAC_OSX
1487 DoGetPosition( &former_x , &former_y ) ;
1488 DoGetSize( &former_w , &former_h ) ;
1489 #else
1490 MacGetPositionAndSizeFromControl( former_x , former_y , former_w , former_h ) ;
1491 #endif
1492
1493 int actualWidth = width;
1494 int actualHeight = height;
1495 int actualX = x;
1496 int actualY = y;
1497
1498 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
1499 actualWidth = m_minWidth;
1500 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
1501 actualHeight = m_minHeight;
1502 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
1503 actualWidth = m_maxWidth;
1504 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
1505 actualHeight = m_maxHeight;
1506
1507 bool doMove = false ;
1508 bool doResize = false ;
1509
1510 if ( actualX != former_x || actualY != former_y )
1511 {
1512 doMove = true ;
1513 }
1514 if ( actualWidth != former_w || actualHeight != former_h )
1515 {
1516 doResize = true ;
1517 }
1518
1519 if ( doMove || doResize )
1520 {
1521 // we don't adjust twice for the origin
1522 Rect r = wxMacGetBoundsForControl(this , wxPoint( actualX,actualY), wxSize( actualWidth, actualHeight ) , false ) ;
1523 bool vis = IsControlVisible( (ControlRef) m_macControl ) ;
1524 #if TARGET_API_MAC_OSX
1525 // the HIViewSetFrame call itself should invalidate the areas, but when testing with the UnicodeTextCtrl it does not !
1526 if ( vis )
1527 SetControlVisibility( (ControlRef)m_macControl , false , true ) ;
1528 HIRect hir = { r.left , r.top , r.right - r.left , r.bottom - r.top } ;
1529 HIViewSetFrame ( (ControlRef) m_macControl , &hir ) ;
1530 if ( vis )
1531 SetControlVisibility( (ControlRef)m_macControl , true , true ) ;
1532 #else
1533 if ( vis )
1534 SetControlVisibility( (ControlRef)m_macControl , false , true ) ;
1535 SetControlBounds( (ControlRef) m_macControl , &r ) ;
1536 if ( vis )
1537 SetControlVisibility( (ControlRef)m_macControl , true , true ) ;
1538 #endif
1539 MacRepositionScrollBars() ;
1540 if ( doMove )
1541 {
1542 wxPoint point(actualX,actualY);
1543 wxMoveEvent event(point, m_windowId);
1544 event.SetEventObject(this);
1545 GetEventHandler()->ProcessEvent(event) ;
1546 }
1547 if ( doResize )
1548 {
1549 MacRepositionScrollBars() ;
1550 wxSize size(actualWidth, actualHeight);
1551 wxSizeEvent event(size, m_windowId);
1552 event.SetEventObject(this);
1553 GetEventHandler()->ProcessEvent(event);
1554 }
1555 }
1556
1557 }
1558
1559 wxSize wxWindowMac::DoGetBestSize() const
1560 {
1561 if ( m_macIsUserPane || IsTopLevel() )
1562 return wxWindowBase::DoGetBestSize() ;
1563
1564 Rect bestsize = { 0 , 0 , 0 , 0 } ;
1565 short baselineoffset ;
1566 int bestWidth, bestHeight ;
1567 ::GetBestControlRect( (ControlRef) m_macControl , &bestsize , &baselineoffset ) ;
1568
1569 if ( EmptyRect( &bestsize ) )
1570 {
1571 baselineoffset = 0;
1572 bestsize.left = bestsize.top = 0 ;
1573 bestsize.right = 16 ;
1574 bestsize.bottom = 16 ;
1575 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
1576 {
1577 bestsize.bottom = 16 ;
1578 }
1579 else if ( IsKindOf( CLASSINFO( wxSpinButton ) ) )
1580 {
1581 bestsize.bottom = 24 ;
1582 }
1583 else
1584 {
1585 // return wxWindowBase::DoGetBestSize() ;
1586 }
1587 }
1588
1589 bestWidth = bestsize.right - bestsize.left ;
1590 bestHeight = bestsize.bottom - bestsize.top ;
1591 if ( bestHeight < 10 )
1592 bestHeight = 13 ;
1593
1594 return wxSize(bestWidth, bestHeight);
1595 // return wxWindowBase::DoGetBestSize() ;
1596 }
1597
1598
1599 // set the size of the window: if the dimensions are positive, just use them,
1600 // but if any of them is equal to -1, it means that we must find the value for
1601 // it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
1602 // which case -1 is a valid value for x and y)
1603 //
1604 // If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
1605 // the width/height to best suit our contents, otherwise we reuse the current
1606 // width/height
1607 void wxWindowMac::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1608 {
1609 // get the current size and position...
1610 int currentX, currentY;
1611 GetPosition(&currentX, &currentY);
1612
1613 int currentW,currentH;
1614 GetSize(&currentW, &currentH);
1615
1616 // ... and don't do anything (avoiding flicker) if it's already ok
1617 if ( x == currentX && y == currentY &&
1618 width == currentW && height == currentH && ( height != -1 && width != -1 ) )
1619 {
1620 // TODO REMOVE
1621 MacRepositionScrollBars() ; // we might have a real position shift
1622 return;
1623 }
1624
1625 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1626 x = currentX;
1627 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1628 y = currentY;
1629
1630 AdjustForParentClientOrigin(x, y, sizeFlags);
1631
1632 wxSize size(-1, -1);
1633 if ( width == -1 )
1634 {
1635 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
1636 {
1637 size = DoGetBestSize();
1638 width = size.x;
1639 }
1640 else
1641 {
1642 // just take the current one
1643 width = currentW;
1644 }
1645 }
1646
1647 if ( height == -1 )
1648 {
1649 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
1650 {
1651 if ( size.x == -1 )
1652 {
1653 size = DoGetBestSize();
1654 }
1655 //else: already called DoGetBestSize() above
1656
1657 height = size.y;
1658 }
1659 else
1660 {
1661 // just take the current one
1662 height = currentH;
1663 }
1664 }
1665
1666 DoMoveWindow(x, y, width, height);
1667
1668 }
1669
1670 wxPoint wxWindowMac::GetClientAreaOrigin() const
1671 {
1672 RgnHandle rgn = NewRgn() ;
1673 Rect content ;
1674 GetControlRegion( (ControlRef) m_macControl , kControlContentMetaPart , rgn ) ;
1675 GetRegionBounds( rgn , &content ) ;
1676 DisposeRgn( rgn ) ;
1677 #if !TARGET_API_MAC_OSX
1678 // if the content rgn is empty / not supported
1679 // don't attempt to correct the coordinates to wxWindow relative ones
1680 if (!::EmptyRect( &content ) )
1681 {
1682 Rect structure ;
1683 GetControlBounds( (ControlRef) m_macControl , &structure ) ;
1684 OffsetRect( &content , -structure.left , -structure.top ) ;
1685 }
1686 #endif
1687
1688 return wxPoint( content.left + MacGetLeftBorderSize( ) , content.top + MacGetTopBorderSize( ) );
1689 }
1690
1691 void wxWindowMac::DoSetClientSize(int clientwidth, int clientheight)
1692 {
1693 if ( clientheight != -1 || clientheight != -1 )
1694 {
1695 int currentclientwidth , currentclientheight ;
1696 int currentwidth , currentheight ;
1697
1698 GetClientSize( &currentclientwidth , &currentclientheight ) ;
1699 GetSize( &currentwidth , &currentheight ) ;
1700
1701 DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth ,
1702 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
1703 }
1704 }
1705
1706 void wxWindowMac::SetTitle(const wxString& title)
1707 {
1708 m_label = wxStripMenuCodes(title) ;
1709
1710 if ( m_macControl )
1711 {
1712 UMASetControlTitle( (ControlRef) m_macControl , m_label , m_font.GetEncoding() ) ;
1713 }
1714 Refresh() ;
1715 }
1716
1717 wxString wxWindowMac::GetTitle() const
1718 {
1719 return m_label ;
1720 }
1721
1722 bool wxWindowMac::Show(bool show)
1723 {
1724 if ( !wxWindowBase::Show(show) )
1725 return FALSE;
1726
1727 // TODO use visibilityChanged Carbon Event for OSX
1728 bool former = MacIsReallyShown() ;
1729
1730 SetControlVisibility( (ControlRef) m_macControl , show , true ) ;
1731 if ( former != MacIsReallyShown() )
1732 MacPropagateVisibilityChanged() ;
1733 return TRUE;
1734 }
1735
1736 bool wxWindowMac::Enable(bool enable)
1737 {
1738 wxASSERT( m_macControl != NULL ) ;
1739 if ( !wxWindowBase::Enable(enable) )
1740 return FALSE;
1741
1742 bool former = MacIsReallyEnabled() ;
1743 #if TARGET_API_MAC_OSX
1744 if ( enable )
1745 EnableControl( (ControlRef) m_macControl ) ;
1746 else
1747 DisableControl( (ControlRef) m_macControl ) ;
1748 #else
1749 if ( enable )
1750 ActivateControl( (ControlRef) m_macControl ) ;
1751 else
1752 DeactivateControl( (ControlRef) m_macControl ) ;
1753 #endif
1754
1755 if ( former != MacIsReallyEnabled() )
1756 MacPropagateEnabledStateChanged() ;
1757 return TRUE;
1758 }
1759
1760 //
1761 // status change propagations (will be not necessary for OSX later )
1762 //
1763
1764 void wxWindowMac::MacPropagateVisibilityChanged()
1765 {
1766 #if !TARGET_API_MAC_OSX
1767 MacVisibilityChanged() ;
1768
1769 wxWindowListNode *node = GetChildren().GetFirst();
1770 while ( node )
1771 {
1772 wxWindowMac *child = node->GetData();
1773 if ( child->IsShown() )
1774 child->MacPropagateVisibilityChanged( ) ;
1775 node = node->GetNext();
1776 }
1777 #endif
1778 }
1779
1780 void wxWindowMac::MacPropagateEnabledStateChanged( )
1781 {
1782 #if !TARGET_API_MAC_OSX
1783 MacEnabledStateChanged() ;
1784
1785 wxWindowListNode *node = GetChildren().GetFirst();
1786 while ( node )
1787 {
1788 wxWindowMac *child = node->GetData();
1789 if ( child->IsEnabled() )
1790 child->MacPropagateEnabledStateChanged() ;
1791 node = node->GetNext();
1792 }
1793 #endif
1794 }
1795
1796 void wxWindowMac::MacPropagateHiliteChanged( )
1797 {
1798 #if !TARGET_API_MAC_OSX
1799 MacHiliteChanged() ;
1800
1801 wxWindowListNode *node = GetChildren().GetFirst();
1802 while ( node )
1803 {
1804 wxWindowMac *child = node->GetData();
1805 // if ( child->IsEnabled() )
1806 child->MacPropagateHiliteChanged() ;
1807 node = node->GetNext();
1808 }
1809 #endif
1810 }
1811
1812 //
1813 // status change notifications
1814 //
1815
1816 void wxWindowMac::MacVisibilityChanged()
1817 {
1818 }
1819
1820 void wxWindowMac::MacHiliteChanged()
1821 {
1822 }
1823
1824 void wxWindowMac::MacEnabledStateChanged()
1825 {
1826 }
1827
1828 //
1829 // status queries on the inherited window's state
1830 //
1831
1832 bool wxWindowMac::MacIsReallyShown()
1833 {
1834 // only under OSX the visibility of the TLW is taken into account
1835 #if TARGET_API_MAC_OSX
1836 return IsControlVisible( (ControlRef) m_macControl ) ;
1837 #else
1838 wxWindow* win = this ;
1839 while( win->IsShown() )
1840 {
1841 if ( win->IsTopLevel() )
1842 return true ;
1843
1844 win = win->GetParent() ;
1845 if ( win == NULL )
1846 return true ;
1847
1848 } ;
1849 return false ;
1850 #endif
1851 }
1852
1853 bool wxWindowMac::MacIsReallyEnabled()
1854 {
1855 #if TARGET_API_MAC_OSX
1856 return IsControlEnabled( (ControlRef) m_macControl ) ;
1857 #else
1858 return IsControlActive( (ControlRef) m_macControl ) ;
1859 #endif
1860 }
1861
1862 bool wxWindowMac::MacIsReallyHilited()
1863 {
1864 return IsControlActive( (ControlRef) m_macControl ) ;
1865 }
1866
1867 void wxWindowMac::MacFlashInvalidAreas()
1868 {
1869 #if TARGET_API_MAC_OSX
1870 HIViewFlashDirtyArea( (WindowRef) MacGetTopLevelWindowRef() ) ;
1871 #endif
1872 }
1873
1874 //
1875 //
1876 //
1877
1878 int wxWindowMac::GetCharHeight() const
1879 {
1880 wxClientDC dc ( (wxWindowMac*)this ) ;
1881 return dc.GetCharHeight() ;
1882 }
1883
1884 int wxWindowMac::GetCharWidth() const
1885 {
1886 wxClientDC dc ( (wxWindowMac*)this ) ;
1887 return dc.GetCharWidth() ;
1888 }
1889
1890 void wxWindowMac::GetTextExtent(const wxString& string, int *x, int *y,
1891 int *descent, int *externalLeading, const wxFont *theFont ) const
1892 {
1893 const wxFont *fontToUse = theFont;
1894 if ( !fontToUse )
1895 fontToUse = &m_font;
1896
1897 wxClientDC dc( (wxWindowMac*) this ) ;
1898 long lx,ly,ld,le ;
1899 dc.GetTextExtent( string , &lx , &ly , &ld, &le, (wxFont *)fontToUse ) ;
1900 if ( externalLeading )
1901 *externalLeading = le ;
1902 if ( descent )
1903 *descent = ld ;
1904 if ( x )
1905 *x = lx ;
1906 if ( y )
1907 *y = ly ;
1908 }
1909
1910 /*
1911 * Rect is given in client coordinates, for further reading, read wxTopLevelWindowMac::InvalidateRect
1912 * we always intersect with the entire window, not only with the client area
1913 */
1914
1915 void wxWindowMac::Refresh(bool eraseBack, const wxRect *rect)
1916 {
1917 #if TARGET_API_MAC_OSX
1918 if ( rect == NULL )
1919 HIViewSetNeedsDisplay( (ControlRef) m_macControl , true ) ;
1920 else
1921 {
1922 RgnHandle update = NewRgn() ;
1923 SetRectRgn( update , rect->x , rect->y , rect->x + rect->width , rect->y + rect->height ) ;
1924 SectRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , update , update ) ;
1925 wxPoint origin = GetClientAreaOrigin() ;
1926 OffsetRgn( update, origin.x , origin.y ) ;
1927 HIViewSetNeedsDisplayInRegion( (ControlRef) m_macControl , update , true ) ;
1928 }
1929 #else
1930 /*
1931 RgnHandle updateRgn = NewRgn() ;
1932 if ( rect == NULL )
1933 {
1934 CopyRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , updateRgn ) ;
1935 }
1936 else
1937 {
1938 SetRectRgn( updateRgn , rect->x , rect->y , rect->x + rect->width , rect->y + rect->height ) ;
1939 SectRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , updateRgn , updateRgn ) ;
1940 }
1941 InvalWindowRgn( (WindowRef) MacGetTopLevelWindowRef() , updateRgn ) ;
1942 DisposeRgn(updateRgn) ;
1943 */
1944 if ( IsControlVisible( (ControlRef) m_macControl ) )
1945 {
1946 SetControlVisibility( (ControlRef) m_macControl , false , false ) ;
1947 SetControlVisibility( (ControlRef) m_macControl , true , true ) ;
1948 }
1949 /*
1950 if ( MacGetTopLevelWindow() == NULL )
1951 return ;
1952
1953 if ( !IsControlVisible( (ControlRef) m_macControl ) )
1954 return ;
1955
1956 wxPoint client = GetClientAreaOrigin();
1957 int x1 = -client.x;
1958 int y1 = -client.y;
1959 int x2 = m_width - client.x;
1960 int y2 = m_height - client.y;
1961
1962 if (IsKindOf( CLASSINFO(wxButton)))
1963 {
1964 // buttons have an "aura"
1965 y1 -= 5;
1966 x1 -= 5;
1967 y2 += 5;
1968 x2 += 5;
1969 }
1970
1971 Rect clientrect = { y1, x1, y2, x2 };
1972
1973 if ( rect )
1974 {
1975 Rect r = { rect->y , rect->x , rect->y + rect->height , rect->x + rect->width } ;
1976 SectRect( &clientrect , &r , &clientrect ) ;
1977 }
1978
1979 if ( !EmptyRect( &clientrect ) )
1980 {
1981 int top = 0 , left = 0 ;
1982
1983 MacClientToRootWindow( &left , &top ) ;
1984 OffsetRect( &clientrect , left , top ) ;
1985
1986 MacGetTopLevelWindow()->MacInvalidate( &clientrect , eraseBack ) ;
1987 }
1988 */
1989 #endif
1990 }
1991
1992 void wxWindowMac::Freeze()
1993 {
1994 #if TARGET_API_MAC_OSX
1995 if ( !m_frozenness++ )
1996 {
1997 HIViewSetDrawingEnabled( (HIViewRef) m_macControl , false ) ;
1998 }
1999 #endif
2000 }
2001
2002 #if TARGET_API_MAC_OSX
2003 static void InvalidateControlAndChildren( HIViewRef control )
2004 {
2005 HIViewSetNeedsDisplay( control , true ) ;
2006 UInt16 childrenCount = 0 ;
2007 OSStatus err = CountSubControls( control , &childrenCount ) ;
2008 if ( err == errControlIsNotEmbedder )
2009 return ;
2010 wxASSERT_MSG( err == noErr , wxT("Unexpected error when accessing subcontrols") ) ;
2011
2012 for ( UInt16 i = childrenCount ; i >=1 ; --i )
2013 {
2014 HIViewRef child ;
2015 err = GetIndexedSubControl( control , i , & child ) ;
2016 if ( err == errControlIsNotEmbedder )
2017 return ;
2018 InvalidateControlAndChildren( child ) ;
2019 }
2020 }
2021 #endif
2022
2023 void wxWindowMac::Thaw()
2024 {
2025 #if TARGET_API_MAC_OSX
2026 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
2027
2028 if ( !--m_frozenness )
2029 {
2030 HIViewSetDrawingEnabled( (HIViewRef) m_macControl , true ) ;
2031 InvalidateControlAndChildren( (HIViewRef) m_macControl ) ;
2032 // HIViewSetNeedsDisplay( (HIViewRef) m_macControl , true ) ;
2033 }
2034 #endif
2035 }
2036
2037 void wxWindowMac::MacRedrawControl()
2038 {
2039 /*
2040 if ( (ControlRef) m_macControl && MacGetTopLevelWindowRef() && IsControlVisible( (ControlRef) m_macControl ) )
2041 {
2042 #if TARGET_API_MAC_CARBON
2043 Update() ;
2044 #else
2045 wxClientDC dc(this) ;
2046 wxMacPortSetter helper(&dc) ;
2047 wxMacWindowClipper clipper(this) ;
2048 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
2049 UMADrawControl( (ControlRef) m_macControl ) ;
2050 #endif
2051 }
2052 */
2053 }
2054
2055 /* TODO
2056 void wxWindowMac::OnPaint(wxPaintEvent& event)
2057 {
2058 // why don't we skip that here ?
2059 }
2060 */
2061
2062 wxWindowMac *wxGetActiveWindow()
2063 {
2064 // actually this is a windows-only concept
2065 return NULL;
2066 }
2067
2068 // Coordinates relative to the window
2069 void wxWindowMac::WarpPointer (int x_pos, int y_pos)
2070 {
2071 // We really don't move the mouse programmatically under Mac.
2072 }
2073
2074 void wxWindowMac::OnEraseBackground(wxEraseEvent& event)
2075 {
2076 if ( m_macBackgroundBrush.Ok() == false || m_macBackgroundBrush.GetStyle() == wxTRANSPARENT )
2077 {
2078 event.Skip() ;
2079 }
2080 else
2081 event.GetDC()->Clear() ;
2082 }
2083
2084 void wxWindowMac::OnNcPaint( wxNcPaintEvent& event )
2085 {
2086 wxWindowDC dc(this) ;
2087 wxMacPortSetter helper(&dc) ;
2088
2089 MacPaintBorders( dc.m_macLocalOrigin.x , dc.m_macLocalOrigin.y) ;
2090 }
2091
2092 int wxWindowMac::GetScrollPos(int orient) const
2093 {
2094 if ( orient == wxHORIZONTAL )
2095 {
2096 if ( m_hScrollBar )
2097 return m_hScrollBar->GetThumbPosition() ;
2098 }
2099 else
2100 {
2101 if ( m_vScrollBar )
2102 return m_vScrollBar->GetThumbPosition() ;
2103 }
2104 return 0;
2105 }
2106
2107 // This now returns the whole range, not just the number
2108 // of positions that we can scroll.
2109 int wxWindowMac::GetScrollRange(int orient) const
2110 {
2111 if ( orient == wxHORIZONTAL )
2112 {
2113 if ( m_hScrollBar )
2114 return m_hScrollBar->GetRange() ;
2115 }
2116 else
2117 {
2118 if ( m_vScrollBar )
2119 return m_vScrollBar->GetRange() ;
2120 }
2121 return 0;
2122 }
2123
2124 int wxWindowMac::GetScrollThumb(int orient) const
2125 {
2126 if ( orient == wxHORIZONTAL )
2127 {
2128 if ( m_hScrollBar )
2129 return m_hScrollBar->GetThumbSize() ;
2130 }
2131 else
2132 {
2133 if ( m_vScrollBar )
2134 return m_vScrollBar->GetThumbSize() ;
2135 }
2136 return 0;
2137 }
2138
2139 void wxWindowMac::SetScrollPos(int orient, int pos, bool refresh)
2140 {
2141 if ( orient == wxHORIZONTAL )
2142 {
2143 if ( m_hScrollBar )
2144 m_hScrollBar->SetThumbPosition( pos ) ;
2145 }
2146 else
2147 {
2148 if ( m_vScrollBar )
2149 m_vScrollBar->SetThumbPosition( pos ) ;
2150 }
2151 }
2152
2153 void wxWindowMac::MacPaintBorders( int left , int top )
2154 {
2155 if( IsTopLevel() )
2156 return ;
2157
2158 int major,minor;
2159 wxGetOsVersion( &major, &minor );
2160
2161 RGBColor white = { 0xFFFF, 0xFFFF , 0xFFFF } ;
2162 RGBColor face = { 0xDDDD, 0xDDDD , 0xDDDD } ;
2163
2164 RGBColor darkShadow = { 0x0000, 0x0000 , 0x0000 } ;
2165 RGBColor lightShadow = { 0x4444, 0x4444 , 0x4444 } ;
2166 // OS X has lighter border edges than classic:
2167 if (major >= 10)
2168 {
2169 darkShadow.red = 0x8E8E;
2170 darkShadow.green = 0x8E8E;
2171 darkShadow.blue = 0x8E8E;
2172 lightShadow.red = 0xBDBD;
2173 lightShadow.green = 0xBDBD;
2174 lightShadow.blue = 0xBDBD;
2175 }
2176
2177 PenNormal() ;
2178
2179 int w , h ;
2180 GetSize( &w , &h ) ;
2181 if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
2182 {
2183 #if wxMAC_USE_THEME_BORDER
2184 Rect rect = { top , left , m_height + top , m_width + left } ;
2185 SInt32 border = 0 ;
2186 /*
2187 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2188 InsetRect( &rect , border , border );
2189 DrawThemeListBoxFrame(&rect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
2190 */
2191
2192 DrawThemePrimaryGroup(&rect ,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
2193 #else
2194 bool sunken = HasFlag( wxSUNKEN_BORDER ) ;
2195 RGBForeColor( &face );
2196 MoveTo( left + 0 , top + h - 2 );
2197 LineTo( left + 0 , top + 0 );
2198 LineTo( left + w - 2 , top + 0 );
2199
2200 MoveTo( left + 2 , top + h - 3 );
2201 LineTo( left + w - 3 , top + h - 3 );
2202 LineTo( left + w - 3 , top + 2 );
2203
2204 RGBForeColor( sunken ? &face : &darkShadow );
2205 MoveTo( left + 0 , top + h - 1 );
2206 LineTo( left + w - 1 , top + h - 1 );
2207 LineTo( left + w - 1 , top + 0 );
2208
2209 RGBForeColor( sunken ? &lightShadow : &white );
2210 MoveTo( left + 1 , top + h - 3 );
2211 LineTo( left + 1, top + 1 );
2212 LineTo( left + w - 3 , top + 1 );
2213
2214 RGBForeColor( sunken ? &white : &lightShadow );
2215 MoveTo( left + 1 , top + h - 2 );
2216 LineTo( left + w - 2 , top + h - 2 );
2217 LineTo( left + w - 2 , top + 1 );
2218
2219 RGBForeColor( sunken ? &darkShadow : &face );
2220 MoveTo( left + 2 , top + h - 4 );
2221 LineTo( left + 2 , top + 2 );
2222 LineTo( left + w - 4 , top + 2 );
2223 #endif
2224 }
2225 else if (HasFlag(wxSIMPLE_BORDER))
2226 {
2227 Rect rect = { top , left , h + top , w + left } ;
2228 RGBForeColor( &darkShadow ) ;
2229 FrameRect( &rect ) ;
2230 }
2231 }
2232
2233 void wxWindowMac::RemoveChild( wxWindowBase *child )
2234 {
2235 if ( child == m_hScrollBar )
2236 m_hScrollBar = NULL ;
2237 if ( child == m_vScrollBar )
2238 m_vScrollBar = NULL ;
2239
2240 wxWindowBase::RemoveChild( child ) ;
2241 }
2242
2243 // New function that will replace some of the above.
2244 void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible,
2245 int range, bool refresh)
2246 {
2247 if ( orient == wxHORIZONTAL )
2248 {
2249 if ( m_hScrollBar )
2250 {
2251 if ( range == 0 || thumbVisible >= range )
2252 {
2253 if ( m_hScrollBar->IsShown() )
2254 m_hScrollBar->Show(false) ;
2255 }
2256 else
2257 {
2258 if ( !m_hScrollBar->IsShown() )
2259 m_hScrollBar->Show(true) ;
2260 m_hScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2261 }
2262 }
2263 }
2264 else
2265 {
2266 if ( m_vScrollBar )
2267 {
2268 if ( range == 0 || thumbVisible >= range )
2269 {
2270 if ( m_vScrollBar->IsShown() )
2271 m_vScrollBar->Show(false) ;
2272 }
2273 else
2274 {
2275 if ( !m_vScrollBar->IsShown() )
2276 m_vScrollBar->Show(true) ;
2277 m_vScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2278 }
2279 }
2280 }
2281 MacRepositionScrollBars() ;
2282 }
2283
2284 // Does a physical scroll
2285 void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect)
2286 {
2287 if( dx == 0 && dy ==0 )
2288 return ;
2289
2290
2291 {
2292
2293 int width , height ;
2294 GetClientSize( &width , &height ) ;
2295 #if TARGET_API_MAC_OSX
2296 // note there currently is a bug in OSX which makes inefficient refreshes in case an entire control
2297 // area is scrolled, this does not occur if width and height are 2 pixels less,
2298 // TODO write optimal workaround
2299 HIRect scrollrect = CGRectMake( MacGetLeftBorderSize() , MacGetTopBorderSize() , width , height ) ;
2300 if ( rect )
2301 {
2302 HIRect scrollarea = CGRectMake( rect->x , rect->y , rect->width , rect->height) ;
2303 scrollrect = CGRectIntersection( scrollrect , scrollarea ) ;
2304 }
2305 if ( HIViewGetNeedsDisplay( (ControlRef) m_macControl ) )
2306 {
2307 // becuase HIViewScrollRect does not scroll the already invalidated area we have two options
2308 // either immediate redraw or full invalidate
2309 #if 1
2310 // is the better overall solution, as it does not slow down scrolling
2311 HIViewSetNeedsDisplay( (ControlRef) m_macControl , true ) ;
2312 #else
2313 // this would be the preferred version for fast drawing controls
2314 if( UMAGetSystemVersion() < 0x1030 )
2315 Update() ;
2316 else
2317 HIViewRender((ControlRef) m_macControl) ;
2318 #endif
2319 }
2320 HIViewScrollRect ( (ControlRef) m_macControl , &scrollrect , dx ,dy ) ;
2321 #else
2322
2323 wxPoint pos;
2324 pos.x = pos.y = 0;
2325
2326 Rect scrollrect;
2327 RgnHandle updateRgn = NewRgn() ;
2328
2329 {
2330 wxClientDC dc(this) ;
2331 wxMacPortSetter helper(&dc) ;
2332
2333 GetControlBounds( (ControlRef) m_macControl, &scrollrect);
2334 scrollrect.top += MacGetTopBorderSize() ;
2335 scrollrect.left += MacGetLeftBorderSize() ;
2336 scrollrect.bottom = scrollrect.top + height ;
2337 scrollrect.right = scrollrect.left + width ;
2338
2339 if ( rect )
2340 {
2341 Rect r = { dc.YLOG2DEVMAC(rect->y) , dc.XLOG2DEVMAC(rect->x) , dc.YLOG2DEVMAC(rect->y + rect->height) ,
2342 dc.XLOG2DEVMAC(rect->x + rect->width) } ;
2343 SectRect( &scrollrect , &r , &scrollrect ) ;
2344 }
2345 ScrollRect( &scrollrect , dx , dy , updateRgn ) ;
2346 }
2347 // ScrollWindowRect( (WindowRef) MacGetTopLevelWindowRef() , &scrollrect , dx , dy , kScrollWindowInvalidate, updateRgn ) ;
2348 #endif
2349 }
2350
2351 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
2352 {
2353 wxWindowMac *child = node->GetData();
2354 if (child == m_vScrollBar) continue;
2355 if (child == m_hScrollBar) continue;
2356 if (child->IsTopLevel()) continue;
2357
2358 int x,y;
2359 child->GetPosition( &x, &y );
2360 int w,h;
2361 child->GetSize( &w, &h );
2362 if (rect)
2363 {
2364 wxRect rc(x,y,w,h);
2365 if (rect->Intersects(rc))
2366 child->SetSize( x+dx, y+dy, w, h );
2367 }
2368 else
2369 {
2370 child->SetSize( x+dx, y+dy, w, h );
2371 }
2372 }
2373 }
2374
2375 void wxWindowMac::MacOnScroll(wxScrollEvent &event )
2376 {
2377 if ( event.m_eventObject == m_vScrollBar || event.m_eventObject == m_hScrollBar )
2378 {
2379 wxScrollWinEvent wevent;
2380 wevent.SetPosition(event.GetPosition());
2381 wevent.SetOrientation(event.GetOrientation());
2382 wevent.m_eventObject = this;
2383
2384 if (event.m_eventType == wxEVT_SCROLL_TOP)
2385 wevent.m_eventType = wxEVT_SCROLLWIN_TOP;
2386 else if (event.m_eventType == wxEVT_SCROLL_BOTTOM)
2387 wevent.m_eventType = wxEVT_SCROLLWIN_BOTTOM;
2388 else if (event.m_eventType == wxEVT_SCROLL_LINEUP)
2389 wevent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
2390 else if (event.m_eventType == wxEVT_SCROLL_LINEDOWN)
2391 wevent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
2392 else if (event.m_eventType == wxEVT_SCROLL_PAGEUP)
2393 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEUP;
2394 else if (event.m_eventType == wxEVT_SCROLL_PAGEDOWN)
2395 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEDOWN;
2396 else if (event.m_eventType == wxEVT_SCROLL_THUMBTRACK)
2397 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBTRACK;
2398 else if (event.m_eventType == wxEVT_SCROLL_THUMBRELEASE)
2399 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBRELEASE;
2400
2401 GetEventHandler()->ProcessEvent(wevent);
2402 }
2403 }
2404
2405 // Get the window with the focus
2406 wxWindowMac *wxWindowBase::FindFocus()
2407 {
2408 ControlRef control ;
2409 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
2410 return wxFindControlFromMacControl( control ) ;
2411 }
2412
2413 void wxWindowMac::OnSetFocus(wxFocusEvent& event)
2414 {
2415 // panel wants to track the window which was the last to have focus in it,
2416 // so we want to set ourselves as the window which last had focus
2417 //
2418 // notice that it's also important to do it upwards the tree becaus
2419 // otherwise when the top level panel gets focus, it won't set it back to
2420 // us, but to some other sibling
2421
2422 // CS:don't know if this is still needed:
2423 //wxChildFocusEvent eventFocus(this);
2424 //(void)GetEventHandler()->ProcessEvent(eventFocus);
2425
2426 event.Skip();
2427 }
2428
2429 void wxWindowMac::OnInternalIdle()
2430 {
2431 // This calls the UI-update mechanism (querying windows for
2432 // menu/toolbar/control state information)
2433 if (wxUpdateUIEvent::CanUpdate(this))
2434 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2435 }
2436
2437 // Raise the window to the top of the Z order
2438 void wxWindowMac::Raise()
2439 {
2440 #if TARGET_API_MAC_OSX
2441 HIViewSetZOrder((ControlRef)m_macControl,kHIViewZOrderAbove, NULL) ;
2442 #endif
2443 }
2444
2445 // Lower the window to the bottom of the Z order
2446 void wxWindowMac::Lower()
2447 {
2448 #if TARGET_API_MAC_OSX
2449 HIViewSetZOrder((ControlRef)m_macControl,kHIViewZOrderBelow, NULL) ;
2450 #endif
2451 }
2452
2453
2454 // static wxWindow *gs_lastWhich = NULL;
2455
2456 bool wxWindowMac::MacSetupCursor( const wxPoint& pt)
2457 {
2458 // first trigger a set cursor event
2459
2460 wxPoint clientorigin = GetClientAreaOrigin() ;
2461 wxSize clientsize = GetClientSize() ;
2462 wxCursor cursor ;
2463 if ( wxRect2DInt( clientorigin.x , clientorigin.y , clientsize.x , clientsize.y ).Contains( wxPoint2DInt( pt ) ) )
2464 {
2465 wxSetCursorEvent event( pt.x , pt.y );
2466
2467 bool processedEvtSetCursor = GetEventHandler()->ProcessEvent(event);
2468 if ( processedEvtSetCursor && event.HasCursor() )
2469 {
2470 cursor = event.GetCursor() ;
2471 }
2472 else
2473 {
2474
2475 // the test for processedEvtSetCursor is here to prevent using m_cursor
2476 // if the user code caught EVT_SET_CURSOR() and returned nothing from
2477 // it - this is a way to say that our cursor shouldn't be used for this
2478 // point
2479 if ( !processedEvtSetCursor && m_cursor.Ok() )
2480 {
2481 cursor = m_cursor ;
2482 }
2483 if ( wxIsBusy() )
2484 {
2485 }
2486 else
2487 {
2488 if ( !GetParent() )
2489 cursor = *wxSTANDARD_CURSOR ;
2490 }
2491 }
2492 if ( cursor.Ok() )
2493 cursor.MacInstall() ;
2494 }
2495 return cursor.Ok() ;
2496 }
2497
2498 wxString wxWindowMac::MacGetToolTipString( wxPoint &pt )
2499 {
2500 if ( m_tooltip )
2501 {
2502 return m_tooltip->GetTip() ;
2503 }
2504 return wxEmptyString ;
2505 }
2506
2507 void wxWindowMac::Update()
2508 {
2509 #if TARGET_API_MAC_OSX
2510 WindowRef window = (WindowRef)MacGetTopLevelWindowRef() ;
2511 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
2512 // for composited windows this also triggers a redraw of all
2513 // invalid views in the window
2514 if( UMAGetSystemVersion() >= 0x1030 )
2515 HIWindowFlush(window) ;
2516 else
2517 #endif
2518 {
2519 // the only way to trigger the redrawing on earlier systems is to call
2520 // ReceiveNextEvent
2521
2522 EventRef currentEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
2523 UInt32 currentEventClass = 0 ;
2524 UInt32 currentEventKind = 0 ;
2525 if ( currentEvent != NULL )
2526 {
2527 currentEventClass = ::GetEventClass( currentEvent ) ;
2528 currentEventKind = ::GetEventKind( currentEvent ) ;
2529 }
2530 if ( currentEventClass != kEventClassMenu )
2531 {
2532 // when tracking a menu, strange redraw errors occur if we flush now, so leave..
2533
2534 EventRef theEvent;
2535 OSStatus status = noErr ;
2536 status = ReceiveNextEvent( 0 , NULL , kEventDurationNoWait , false , &theEvent ) ;
2537 }
2538 else
2539 HIViewSetNeedsDisplay( (ControlRef) m_macControl , true ) ;
2540 }
2541 #else
2542 ::Draw1Control( (ControlRef) m_macControl ) ;
2543 #endif
2544 }
2545
2546 wxTopLevelWindowMac* wxWindowMac::MacGetTopLevelWindow() const
2547 {
2548 wxTopLevelWindowMac* win = NULL ;
2549 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
2550 if ( window )
2551 {
2552 win = wxFindWinFromMacWindow( window ) ;
2553 }
2554 return win ;
2555 }
2556 wxRegion wxWindowMac::MacGetVisibleRegion( bool includeOuterStructures )
2557 {
2558
2559 Rect r ;
2560 RgnHandle visRgn = NewRgn() ;
2561 RgnHandle tempRgn = NewRgn() ;
2562 if ( IsControlVisible( (ControlRef) m_macControl ) )
2563 {
2564 GetControlBounds( (ControlRef) m_macControl , &r ) ;
2565 if (! MacGetTopLevelWindow()->MacUsesCompositing() )
2566 {
2567 MacRootWindowToWindow( &r.left , & r.top ) ;
2568 MacRootWindowToWindow( &r.right , & r.bottom ) ;
2569 }
2570 else
2571 {
2572 r.right -= r.left ;
2573 r.bottom -= r.top ;
2574 r.left = 0 ;
2575 r.top = 0 ;
2576 }
2577 if ( includeOuterStructures )
2578 InsetRect( &r , -3 , -3 ) ;
2579 RectRgn( visRgn , &r ) ;
2580 if ( !IsTopLevel() )
2581 {
2582 wxWindow* child = this ;
2583 wxWindow* parent = child->GetParent() ;
2584 while( parent )
2585 {
2586 int x , y ;
2587 wxSize size ;
2588 // we have to find a better clipping algorithm here, in order not to clip things
2589 // positioned like status and toolbar
2590 if ( 1 /* parent->IsTopLevel() && child->IsKindOf( CLASSINFO( wxToolBar ) ) */ )
2591 {
2592 size = parent->GetSize() ;
2593 x = y = 0 ;
2594 }
2595 else
2596 {
2597 size = parent->GetClientSize() ;
2598 wxPoint origin = parent->GetClientAreaOrigin() ;
2599 x = origin.x ;
2600 y = origin.y ;
2601 }
2602 parent->MacWindowToRootWindow( &x, &y ) ;
2603 MacRootWindowToWindow( &x , &y ) ;
2604
2605 SetRectRgn( tempRgn ,
2606 x + parent->MacGetLeftBorderSize() , y + parent->MacGetTopBorderSize() ,
2607 x + size.x - parent->MacGetRightBorderSize(),
2608 y + size.y - parent->MacGetBottomBorderSize()) ;
2609
2610 SectRgn( visRgn , tempRgn , visRgn ) ;
2611 if ( parent->IsTopLevel() )
2612 break ;
2613 child = parent ;
2614 parent = child->GetParent() ;
2615 }
2616 }
2617 }
2618
2619 wxRegion vis = visRgn ;
2620 DisposeRgn( visRgn ) ;
2621 DisposeRgn( tempRgn ) ;
2622 return vis ;
2623 }
2624
2625 /*
2626 This function must not change the updatergn !
2627 */
2628 bool wxWindowMac::MacDoRedraw( WXHRGN updatergnr , long time )
2629 {
2630 RgnHandle updatergn = (RgnHandle) updatergnr ;
2631 bool handled = false ;
2632
2633 // calculate a client-origin version of the update rgn and set m_updateRegion to that
2634 {
2635 RgnHandle newupdate = NewRgn() ;
2636 wxSize point = GetClientSize() ;
2637 wxPoint origin = GetClientAreaOrigin() ;
2638 SetRectRgn( newupdate , origin.x , origin.y , origin.x + point.x , origin.y+point.y ) ;
2639 SectRgn( newupdate , updatergn , newupdate ) ;
2640 OffsetRgn( newupdate , -origin.x , -origin.y ) ;
2641 m_updateRegion = newupdate ;
2642 DisposeRgn( newupdate ) ;
2643 }
2644
2645 if ( !EmptyRgn(updatergn) )
2646 {
2647 wxWindowDC dc(this);
2648 if (!EmptyRgn(updatergn))
2649 dc.SetClippingRegion(wxRegion(updatergn));
2650
2651 wxEraseEvent eevent( GetId(), &dc );
2652 eevent.SetEventObject( this );
2653 GetEventHandler()->ProcessEvent( eevent );
2654
2655 if ( !m_updateRegion.Empty() )
2656 {
2657 // paint the window itself
2658 wxPaintEvent event;
2659 event.m_timeStamp = time ;
2660 event.SetEventObject(this);
2661 handled = GetEventHandler()->ProcessEvent(event);
2662
2663 // paint custom borders
2664 wxNcPaintEvent eventNc( GetId() );
2665 eventNc.SetEventObject( this );
2666 GetEventHandler()->ProcessEvent( eventNc );
2667 }
2668 }
2669 return handled ;
2670 }
2671
2672 void wxWindowMac::MacRedraw( WXHRGN updatergnr , long time, bool erase)
2673 {
2674 RgnHandle updatergn = (RgnHandle) updatergnr ;
2675 // updatergn is always already clipped to our boundaries
2676 // if we are in compositing mode then it is in relative to the upper left of the control
2677 // if we are in non-compositing, then it is relatvie to the uppder left of the content area
2678 // of the toplevel window
2679 // it is in window coordinates, not in client coordinates
2680
2681 // ownUpdateRgn is the area that this window has to repaint, it is in window coordinates
2682 RgnHandle ownUpdateRgn = NewRgn() ;
2683 CopyRgn( updatergn , ownUpdateRgn ) ;
2684
2685 if ( MacGetTopLevelWindow()->MacUsesCompositing() == false )
2686 {
2687 Rect bounds;
2688 UMAGetControlBoundsInWindowCoords( (ControlRef)m_macControl, &bounds );
2689 RgnHandle controlRgn = NewRgn();
2690 RectRgn( controlRgn, &bounds );
2691 //KO: This sets the ownUpdateRgn to the area of this control that is inside
2692 // the window update region
2693 SectRgn( ownUpdateRgn, controlRgn, ownUpdateRgn );
2694 DisposeRgn( controlRgn );
2695
2696 //KO: convert ownUpdateRgn to local coordinates
2697 OffsetRgn( ownUpdateRgn, -bounds.left, -bounds.top );
2698 }
2699
2700 MacDoRedraw( ownUpdateRgn , time ) ;
2701 DisposeRgn( ownUpdateRgn ) ;
2702
2703 }
2704
2705 WXWindow wxWindowMac::MacGetTopLevelWindowRef() const
2706 {
2707 wxWindowMac *iter = (wxWindowMac*)this ;
2708
2709 while( iter )
2710 {
2711 if ( iter->IsTopLevel() )
2712 return ((wxTopLevelWindow*)iter)->MacGetWindowRef() ;
2713
2714 iter = iter->GetParent() ;
2715 }
2716 wxASSERT_MSG( 1 , wxT("No valid mac root window") ) ;
2717 return NULL ;
2718 }
2719
2720 void wxWindowMac::MacCreateScrollBars( long style )
2721 {
2722 wxASSERT_MSG( m_vScrollBar == NULL && m_hScrollBar == NULL , wxT("attempt to create window twice") ) ;
2723
2724 bool hasBoth = ( style & wxVSCROLL ) && ( style & wxHSCROLL ) ;
2725 int adjust = hasBoth ? MAC_SCROLLBAR_SIZE - 1: 0 ;
2726 int width, height ;
2727 GetClientSize( &width , &height ) ;
2728
2729 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
2730 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
2731 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
2732 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
2733
2734 m_vScrollBar = new wxScrollBar(this, wxWINDOW_VSCROLL, vPoint,
2735 vSize , wxVERTICAL);
2736
2737 if ( style & wxVSCROLL )
2738 {
2739
2740 }
2741 else
2742 {
2743 m_vScrollBar->Show(false) ;
2744 }
2745 m_hScrollBar = new wxScrollBar(this, wxWINDOW_HSCROLL, hPoint,
2746 hSize , wxHORIZONTAL);
2747 if ( style & wxHSCROLL )
2748 {
2749 }
2750 else
2751 {
2752 m_hScrollBar->Show(false) ;
2753 }
2754
2755 // because the create does not take into account the client area origin
2756 MacRepositionScrollBars() ; // we might have a real position shift
2757 }
2758
2759 void wxWindowMac::MacRepositionScrollBars()
2760 {
2761 bool hasBoth = ( m_hScrollBar && m_hScrollBar->IsShown()) && ( m_vScrollBar && m_vScrollBar->IsShown()) ;
2762 int adjust = hasBoth ? MAC_SCROLLBAR_SIZE - 1 : 0 ;
2763
2764 // get real client area
2765
2766 int width ;
2767 int height ;
2768 GetSize( &width , &height ) ;
2769
2770 width -= MacGetLeftBorderSize() + MacGetRightBorderSize();
2771 height -= MacGetTopBorderSize() + MacGetBottomBorderSize();
2772
2773 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
2774 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
2775 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
2776 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
2777
2778 int x = 0 ;
2779 int y = 0 ;
2780 int w ;
2781 int h ;
2782 GetSize( &w , &h ) ;
2783
2784 MacClientToRootWindow( &x , &y ) ;
2785 MacClientToRootWindow( &w , &h ) ;
2786
2787 wxWindowMac *iter = (wxWindowMac*)this ;
2788
2789 int totW = 10000 , totH = 10000;
2790 while( iter )
2791 {
2792 if ( iter->IsTopLevel() )
2793 {
2794 iter->GetSize( &totW , &totH ) ;
2795 break ;
2796 }
2797
2798 iter = iter->GetParent() ;
2799 }
2800
2801 if ( x == 0 )
2802 {
2803 hPoint.x = -1 ;
2804 hSize.x += 1 ;
2805 }
2806 if ( y == 0 )
2807 {
2808 vPoint.y = -1 ;
2809 vSize.y += 1 ;
2810 }
2811
2812 if ( w-x >= totW )
2813 {
2814 hSize.x += 1 ;
2815 vPoint.x += 1 ;
2816 }
2817
2818 if ( h-y >= totH )
2819 {
2820 vSize.y += 1 ;
2821 hPoint.y += 1 ;
2822 }
2823
2824 if ( m_vScrollBar )
2825 {
2826 m_vScrollBar->SetSize( vPoint.x , vPoint.y, vSize.x, vSize.y , wxSIZE_ALLOW_MINUS_ONE);
2827 }
2828 if ( m_hScrollBar )
2829 {
2830 m_hScrollBar->SetSize( hPoint.x , hPoint.y, hSize.x, hSize.y, wxSIZE_ALLOW_MINUS_ONE);
2831 }
2832 }
2833
2834 bool wxWindowMac::AcceptsFocus() const
2835 {
2836 return MacCanFocus() && wxWindowBase::AcceptsFocus();
2837 }
2838
2839 void wxWindowMac::MacSuperChangedPosition()
2840 {
2841 // only window-absolute structures have to be moved i.e. controls
2842
2843 wxWindowListNode *node = GetChildren().GetFirst();
2844 while ( node )
2845 {
2846 wxWindowMac *child = node->GetData();
2847 child->MacSuperChangedPosition() ;
2848 node = node->GetNext();
2849 }
2850 }
2851
2852 void wxWindowMac::MacTopLevelWindowChangedPosition()
2853 {
2854 // only screen-absolute structures have to be moved i.e. glcanvas
2855
2856 wxWindowListNode *node = GetChildren().GetFirst();
2857 while ( node )
2858 {
2859 wxWindowMac *child = node->GetData();
2860 child->MacTopLevelWindowChangedPosition() ;
2861 node = node->GetNext();
2862 }
2863 }
2864
2865 long wxWindowMac::MacGetLeftBorderSize( ) const
2866 {
2867 if( IsTopLevel() )
2868 return 0 ;
2869
2870 if (m_windowStyle & wxRAISED_BORDER || m_windowStyle & wxSUNKEN_BORDER )
2871 {
2872 SInt32 border = 3 ;
2873 #if wxMAC_USE_THEME_BORDER
2874 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2875 #endif
2876 return border ;
2877 }
2878 else if ( m_windowStyle &wxDOUBLE_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 &wxSIMPLE_BORDER)
2887 {
2888 return 1 ;
2889 }
2890 return 0 ;
2891 }
2892
2893 long wxWindowMac::MacGetRightBorderSize( ) const
2894 {
2895 // they are all symmetric in mac themes
2896 return MacGetLeftBorderSize() ;
2897 }
2898
2899 long wxWindowMac::MacGetTopBorderSize( ) const
2900 {
2901 // they are all symmetric in mac themes
2902 return MacGetLeftBorderSize() ;
2903 }
2904
2905 long wxWindowMac::MacGetBottomBorderSize( ) const
2906 {
2907 // they are all symmetric in mac themes
2908 return MacGetLeftBorderSize() ;
2909 }
2910
2911 long wxWindowMac::MacRemoveBordersFromStyle( long style )
2912 {
2913 return style & ~( wxDOUBLE_BORDER | wxSUNKEN_BORDER | wxRAISED_BORDER | wxBORDER | wxSTATIC_BORDER ) ;
2914 }
2915
2916 // Find the wxWindowMac at the current mouse position, returning the mouse
2917 // position.
2918 wxWindowMac* wxFindWindowAtPointer(wxPoint& pt)
2919 {
2920 pt = wxGetMousePosition();
2921 wxWindowMac* found = wxFindWindowAtPoint(pt);
2922 return found;
2923 }
2924
2925 // Get the current mouse position.
2926 wxPoint wxGetMousePosition()
2927 {
2928 int x, y;
2929 wxGetMousePosition(& x, & y);
2930 return wxPoint(x, y);
2931 }
2932
2933 void wxWindowMac::OnMouseEvent( wxMouseEvent &event )
2934 {
2935 if ( event.GetEventType() == wxEVT_RIGHT_DOWN )
2936 {
2937 // copied from wxGTK : CS
2938 // generate a "context menu" event: this is similar to wxEVT_RIGHT_DOWN
2939 // except that:
2940 //
2941 // (a) it's a command event and so is propagated to the parent
2942 // (b) under MSW it can be generated from kbd too
2943 // (c) it uses screen coords (because of (a))
2944 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
2945 this->GetId(),
2946 this->ClientToScreen(event.GetPosition()));
2947 if ( ! GetEventHandler()->ProcessEvent(evtCtx) )
2948 event.Skip() ;
2949 }
2950 else
2951 {
2952 event.Skip() ;
2953 }
2954 }
2955
2956 void wxWindowMac::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED( mouseStillDown ) )
2957 {
2958 }
2959
2960 Rect wxMacGetBoundsForControl( wxWindow* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
2961 {
2962 int x ,y , w ,h ;
2963
2964 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin) ;
2965 Rect bounds = { y , x , y+h , x+w };
2966 return bounds ;
2967 }
2968
2969 wxInt32 wxWindowMac::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
2970 {
2971 return eventNotHandledErr ;
2972 }
2973
2974