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