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