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