wrong method call, we need to add the second update region not to replace it
[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 int outerBorder = MacGetLeftBorderSize() ;
1611 if ( m_peer->NeedsFocusRect() && m_peer->HasFocus() )
1612 outerBorder = 4 ;
1613
1614 if ( vis && ( outerBorder > 0 ) )
1615 {
1616 // as the borders are drawn on the parent we have to properly invalidate all these areas
1617 RgnHandle updateInner = NewRgn() , updateOuter = NewRgn() , updateTotal = NewRgn() ;
1618
1619 Rect rect ;
1620
1621 m_peer->GetRect( &rect ) ;
1622 RectRgn( updateInner , &rect ) ;
1623 InsetRect( &rect , -outerBorder , -outerBorder ) ;
1624 RectRgn( updateOuter , &rect ) ;
1625 DiffRgn( updateOuter , updateInner ,updateOuter ) ;
1626 wxPoint parent(0,0);
1627 GetParent()->MacWindowToRootWindow( &parent.x , &parent.y ) ;
1628 parent -= GetParent()->GetClientAreaOrigin() ;
1629 OffsetRgn( updateOuter , -parent.x , -parent.y ) ;
1630 CopyRgn( updateOuter , updateTotal ) ;
1631
1632 rect = r ;
1633 RectRgn( updateInner , &rect ) ;
1634 InsetRect( &rect , -outerBorder , -outerBorder ) ;
1635 RectRgn( updateOuter , &rect ) ;
1636 DiffRgn( updateOuter , updateInner ,updateOuter ) ;
1637 wxPoint parentorig(0,0);
1638 GetParent()->MacWindowToRootWindow( &parentorig.x , &parentorig.y ) ;
1639 parent -= GetParent()->GetClientAreaOrigin() ;
1640 OffsetRgn( updateOuter , -parentorig.x , -parentorig.y ) ;
1641 UnionRgn( updateOuter , updateTotal , updateTotal ) ;
1642
1643 GetParent()->m_peer->SetNeedsDisplay( true , updateTotal ) ;
1644 DisposeRgn(updateOuter) ;
1645 DisposeRgn(updateInner) ;
1646 DisposeRgn(updateTotal) ;
1647 }
1648
1649 // the HIViewSetFrame call itself should invalidate the areas, but when testing with the UnicodeTextCtrl it does not !
1650 if ( vis )
1651 m_peer->SetVisibility( false , true ) ;
1652
1653 m_peer->SetRect( &r ) ;
1654 if ( vis )
1655 m_peer->SetVisibility( true , true ) ;
1656
1657 MacRepositionScrollBars() ;
1658 if ( doMove )
1659 {
1660 wxPoint point(actualX,actualY);
1661 wxMoveEvent event(point, m_windowId);
1662 event.SetEventObject(this);
1663 GetEventHandler()->ProcessEvent(event) ;
1664 }
1665 if ( doResize )
1666 {
1667 MacRepositionScrollBars() ;
1668 wxSize size(actualWidth, actualHeight);
1669 wxSizeEvent event(size, m_windowId);
1670 event.SetEventObject(this);
1671 GetEventHandler()->ProcessEvent(event);
1672 }
1673 }
1674
1675 }
1676
1677 wxSize wxWindowMac::DoGetBestSize() const
1678 {
1679 if ( m_macIsUserPane || IsTopLevel() )
1680 return wxWindowBase::DoGetBestSize() ;
1681
1682 Rect bestsize = { 0 , 0 , 0 , 0 } ;
1683 int bestWidth, bestHeight ;
1684 m_peer->GetBestRect( &bestsize ) ;
1685
1686 if ( EmptyRect( &bestsize ) )
1687 {
1688 bestsize.left = bestsize.top = 0 ;
1689 bestsize.right = 16 ;
1690 bestsize.bottom = 16 ;
1691 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
1692 {
1693 bestsize.bottom = 16 ;
1694 }
1695 else if ( IsKindOf( CLASSINFO( wxSpinButton ) ) )
1696 {
1697 bestsize.bottom = 24 ;
1698 }
1699 else
1700 {
1701 // return wxWindowBase::DoGetBestSize() ;
1702 }
1703 }
1704
1705 bestWidth = bestsize.right - bestsize.left ;
1706 bestHeight = bestsize.bottom - bestsize.top ;
1707 if ( bestHeight < 10 )
1708 bestHeight = 13 ;
1709
1710 return wxSize(bestWidth, bestHeight);
1711 }
1712
1713
1714 // set the size of the window: if the dimensions are positive, just use them,
1715 // but if any of them is equal to -1, it means that we must find the value for
1716 // it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
1717 // which case -1 is a valid value for x and y)
1718 //
1719 // If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
1720 // the width/height to best suit our contents, otherwise we reuse the current
1721 // width/height
1722 void wxWindowMac::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1723 {
1724 // get the current size and position...
1725 int currentX, currentY;
1726 GetPosition(&currentX, &currentY);
1727
1728 int currentW,currentH;
1729 GetSize(&currentW, &currentH);
1730
1731 // ... and don't do anything (avoiding flicker) if it's already ok
1732 if ( x == currentX && y == currentY &&
1733 width == currentW && height == currentH && ( height != -1 && width != -1 ) )
1734 {
1735 // TODO REMOVE
1736 MacRepositionScrollBars() ; // we might have a real position shift
1737 return;
1738 }
1739
1740 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1741 x = currentX;
1742 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1743 y = currentY;
1744
1745 AdjustForParentClientOrigin(x, y, sizeFlags);
1746
1747 wxSize size(-1, -1);
1748 if ( width == -1 )
1749 {
1750 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
1751 {
1752 size = DoGetBestSize();
1753 width = size.x;
1754 }
1755 else
1756 {
1757 // just take the current one
1758 width = currentW;
1759 }
1760 }
1761
1762 if ( height == -1 )
1763 {
1764 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
1765 {
1766 if ( size.x == -1 )
1767 {
1768 size = DoGetBestSize();
1769 }
1770 //else: already called DoGetBestSize() above
1771
1772 height = size.y;
1773 }
1774 else
1775 {
1776 // just take the current one
1777 height = currentH;
1778 }
1779 }
1780
1781 DoMoveWindow(x, y, width, height);
1782
1783 }
1784
1785 wxPoint wxWindowMac::GetClientAreaOrigin() const
1786 {
1787 RgnHandle rgn = NewRgn() ;
1788 Rect content ;
1789 m_peer->GetRegion( kControlContentMetaPart , rgn ) ;
1790 GetRegionBounds( rgn , &content ) ;
1791 DisposeRgn( rgn ) ;
1792 #if !TARGET_API_MAC_OSX
1793 // if the content rgn is empty / not supported
1794 // don't attempt to correct the coordinates to wxWindow relative ones
1795 if (!::EmptyRect( &content ) )
1796 {
1797 Rect structure ;
1798 m_peer->GetRect( &structure ) ;
1799 OffsetRect( &content , -structure.left , -structure.top ) ;
1800 }
1801 #endif
1802
1803 return wxPoint( content.left + MacGetLeftBorderSize( ) , content.top + MacGetTopBorderSize( ) );
1804 }
1805
1806 void wxWindowMac::DoSetClientSize(int clientwidth, int clientheight)
1807 {
1808 if ( clientheight != -1 || clientheight != -1 )
1809 {
1810 int currentclientwidth , currentclientheight ;
1811 int currentwidth , currentheight ;
1812
1813 GetClientSize( &currentclientwidth , &currentclientheight ) ;
1814 GetSize( &currentwidth , &currentheight ) ;
1815
1816 DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth ,
1817 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
1818 }
1819 }
1820
1821 void wxWindowMac::SetTitle(const wxString& title)
1822 {
1823 m_label = wxStripMenuCodes(title) ;
1824
1825 if ( m_peer && m_peer->Ok() )
1826 {
1827 m_peer->SetTitle( m_label ) ;
1828 }
1829 Refresh() ;
1830 }
1831
1832 wxString wxWindowMac::GetTitle() const
1833 {
1834 return m_label ;
1835 }
1836
1837 bool wxWindowMac::Show(bool show)
1838 {
1839 if ( !wxWindowBase::Show(show) )
1840 return FALSE;
1841
1842 // TODO use visibilityChanged Carbon Event for OSX
1843 bool former = MacIsReallyShown() ;
1844
1845 m_peer->SetVisibility( show , true ) ;
1846 if ( former != MacIsReallyShown() )
1847 MacPropagateVisibilityChanged() ;
1848 return TRUE;
1849 }
1850
1851 bool wxWindowMac::Enable(bool enable)
1852 {
1853 wxASSERT( m_peer->Ok() ) ;
1854 if ( !wxWindowBase::Enable(enable) )
1855 return FALSE;
1856
1857 bool former = MacIsReallyEnabled() ;
1858 m_peer->Enable( enable ) ;
1859
1860 if ( former != MacIsReallyEnabled() )
1861 MacPropagateEnabledStateChanged() ;
1862 return TRUE;
1863 }
1864
1865 //
1866 // status change propagations (will be not necessary for OSX later )
1867 //
1868
1869 void wxWindowMac::MacPropagateVisibilityChanged()
1870 {
1871 #if !TARGET_API_MAC_OSX
1872 MacVisibilityChanged() ;
1873
1874 wxWindowListNode *node = GetChildren().GetFirst();
1875 while ( node )
1876 {
1877 wxWindowMac *child = node->GetData();
1878 if ( child->IsShown() )
1879 child->MacPropagateVisibilityChanged( ) ;
1880 node = node->GetNext();
1881 }
1882 #endif
1883 }
1884
1885 void wxWindowMac::MacPropagateEnabledStateChanged( )
1886 {
1887 #if !TARGET_API_MAC_OSX
1888 MacEnabledStateChanged() ;
1889
1890 wxWindowListNode *node = GetChildren().GetFirst();
1891 while ( node )
1892 {
1893 wxWindowMac *child = node->GetData();
1894 if ( child->IsEnabled() )
1895 child->MacPropagateEnabledStateChanged() ;
1896 node = node->GetNext();
1897 }
1898 #endif
1899 }
1900
1901 void wxWindowMac::MacPropagateHiliteChanged( )
1902 {
1903 #if !TARGET_API_MAC_OSX
1904 MacHiliteChanged() ;
1905
1906 wxWindowListNode *node = GetChildren().GetFirst();
1907 while ( node )
1908 {
1909 wxWindowMac *child = node->GetData();
1910 // if ( child->IsEnabled() )
1911 child->MacPropagateHiliteChanged() ;
1912 node = node->GetNext();
1913 }
1914 #endif
1915 }
1916
1917 //
1918 // status change notifications
1919 //
1920
1921 void wxWindowMac::MacVisibilityChanged()
1922 {
1923 }
1924
1925 void wxWindowMac::MacHiliteChanged()
1926 {
1927 }
1928
1929 void wxWindowMac::MacEnabledStateChanged()
1930 {
1931 }
1932
1933 //
1934 // status queries on the inherited window's state
1935 //
1936
1937 bool wxWindowMac::MacIsReallyShown()
1938 {
1939 // only under OSX the visibility of the TLW is taken into account
1940 #if TARGET_API_MAC_OSX
1941 if ( m_peer && m_peer->Ok() )
1942 return m_peer->IsVisible();
1943 #endif
1944 wxWindow* win = this ;
1945 while( win->IsShown() )
1946 {
1947 if ( win->IsTopLevel() )
1948 return true ;
1949
1950 win = win->GetParent() ;
1951 if ( win == NULL )
1952 return true ;
1953
1954 } ;
1955 return false ;
1956 }
1957
1958 bool wxWindowMac::MacIsReallyEnabled()
1959 {
1960 return m_peer->IsEnabled() ;
1961 }
1962
1963 bool wxWindowMac::MacIsReallyHilited()
1964 {
1965 return m_peer->IsActive();
1966 }
1967
1968 void wxWindowMac::MacFlashInvalidAreas()
1969 {
1970 #if TARGET_API_MAC_OSX
1971 HIViewFlashDirtyArea( (WindowRef) MacGetTopLevelWindowRef() ) ;
1972 #endif
1973 }
1974
1975 //
1976 //
1977 //
1978
1979 int wxWindowMac::GetCharHeight() const
1980 {
1981 wxClientDC dc ( (wxWindowMac*)this ) ;
1982 return dc.GetCharHeight() ;
1983 }
1984
1985 int wxWindowMac::GetCharWidth() const
1986 {
1987 wxClientDC dc ( (wxWindowMac*)this ) ;
1988 return dc.GetCharWidth() ;
1989 }
1990
1991 void wxWindowMac::GetTextExtent(const wxString& string, int *x, int *y,
1992 int *descent, int *externalLeading, const wxFont *theFont ) const
1993 {
1994 const wxFont *fontToUse = theFont;
1995 if ( !fontToUse )
1996 fontToUse = &m_font;
1997
1998 wxClientDC dc( (wxWindowMac*) this ) ;
1999 long lx,ly,ld,le ;
2000 dc.GetTextExtent( string , &lx , &ly , &ld, &le, (wxFont *)fontToUse ) ;
2001 if ( externalLeading )
2002 *externalLeading = le ;
2003 if ( descent )
2004 *descent = ld ;
2005 if ( x )
2006 *x = lx ;
2007 if ( y )
2008 *y = ly ;
2009 }
2010
2011 /*
2012 * Rect is given in client coordinates, for further reading, read wxTopLevelWindowMac::InvalidateRect
2013 * we always intersect with the entire window, not only with the client area
2014 */
2015
2016 void wxWindowMac::Refresh(bool eraseBack, const wxRect *rect)
2017 {
2018 if ( m_peer == NULL )
2019 return ;
2020
2021 #if TARGET_API_MAC_OSX
2022 if ( rect == NULL )
2023 m_peer->SetNeedsDisplay( true ) ;
2024 else
2025 {
2026 RgnHandle update = NewRgn() ;
2027 SetRectRgn( update , rect->x , rect->y , rect->x + rect->width , rect->y + rect->height ) ;
2028 SectRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , update , update ) ;
2029 wxPoint origin = GetClientAreaOrigin() ;
2030 OffsetRgn( update, origin.x , origin.y ) ;
2031 // right now this is wx' window coordinates, as our native peer does not have borders, this is
2032 // inset
2033 OffsetRgn( update , -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2034 m_peer->SetNeedsDisplay( true , update) ;
2035 DisposeRgn( update ) ;
2036 }
2037 #else
2038 /*
2039 RgnHandle updateRgn = NewRgn() ;
2040 if ( rect == NULL )
2041 {
2042 CopyRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , updateRgn ) ;
2043 }
2044 else
2045 {
2046 SetRectRgn( updateRgn , rect->x , rect->y , rect->x + rect->width , rect->y + rect->height ) ;
2047 SectRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , updateRgn , updateRgn ) ;
2048 }
2049 InvalWindowRgn( (WindowRef) MacGetTopLevelWindowRef() , updateRgn ) ;
2050 DisposeRgn(updateRgn) ;
2051 */
2052 if ( m_peer->IsVisible())
2053 {
2054 m_peer->SetVisibility( false , false ) ;
2055 m_peer->SetVisibility( true , true ) ;
2056 }
2057 /*
2058 if ( MacGetTopLevelWindow() == NULL )
2059 return ;
2060
2061 if ( !m_peer->IsVisible())
2062 return ;
2063
2064 wxPoint client = GetClientAreaOrigin();
2065 int x1 = -client.x;
2066 int y1 = -client.y;
2067 int x2 = m_width - client.x;
2068 int y2 = m_height - client.y;
2069
2070 if (IsKindOf( CLASSINFO(wxButton)))
2071 {
2072 // buttons have an "aura"
2073 y1 -= 5;
2074 x1 -= 5;
2075 y2 += 5;
2076 x2 += 5;
2077 }
2078
2079 Rect clientrect = { y1, x1, y2, x2 };
2080
2081 if ( rect )
2082 {
2083 Rect r = { rect->y , rect->x , rect->y + rect->height , rect->x + rect->width } ;
2084 SectRect( &clientrect , &r , &clientrect ) ;
2085 }
2086
2087 if ( !EmptyRect( &clientrect ) )
2088 {
2089 int top = 0 , left = 0 ;
2090
2091 MacClientToRootWindow( &left , &top ) ;
2092 OffsetRect( &clientrect , left , top ) ;
2093
2094 MacGetTopLevelWindow()->MacInvalidate( &clientrect , eraseBack ) ;
2095 }
2096 */
2097 #endif
2098 }
2099
2100 void wxWindowMac::Freeze()
2101 {
2102 #if TARGET_API_MAC_OSX
2103 if ( !m_frozenness++ )
2104 {
2105 m_peer->SetDrawingEnabled( false ) ;
2106 }
2107 #endif
2108 }
2109
2110
2111 void wxWindowMac::Thaw()
2112 {
2113 #if TARGET_API_MAC_OSX
2114 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
2115
2116 if ( !--m_frozenness )
2117 {
2118 m_peer->SetDrawingEnabled( true ) ;
2119 m_peer->InvalidateWithChildren() ;
2120 }
2121 #endif
2122 }
2123
2124 void wxWindowMac::MacRedrawControl()
2125 {
2126 /*
2127 if ( *m_peer && MacGetTopLevelWindowRef() && m_peer->IsVisible())
2128 {
2129 #if TARGET_API_MAC_CARBON
2130 Update() ;
2131 #else
2132 wxClientDC dc(this) ;
2133 wxMacPortSetter helper(&dc) ;
2134 wxMacWindowClipper clipper(this) ;
2135 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
2136 UMADrawControl( *m_peer ) ;
2137 #endif
2138 }
2139 */
2140 }
2141
2142 /* TODO
2143 void wxWindowMac::OnPaint(wxPaintEvent& event)
2144 {
2145 // why don't we skip that here ?
2146 }
2147 */
2148
2149 wxWindowMac *wxGetActiveWindow()
2150 {
2151 // actually this is a windows-only concept
2152 return NULL;
2153 }
2154
2155 // Coordinates relative to the window
2156 void wxWindowMac::WarpPointer (int x_pos, int y_pos)
2157 {
2158 // We really don't move the mouse programmatically under Mac.
2159 }
2160
2161 void wxWindowMac::OnEraseBackground(wxEraseEvent& event)
2162 {
2163 #if TARGET_API_MAC_OSX
2164 if ( m_macBackgroundBrush.Ok() == false || m_macBackgroundBrush.GetStyle() == wxTRANSPARENT )
2165 {
2166 event.Skip() ;
2167 }
2168 else
2169 #endif
2170 event.GetDC()->Clear() ;
2171 }
2172
2173 void wxWindowMac::OnNcPaint( wxNcPaintEvent& event )
2174 {
2175 event.Skip() ;
2176 }
2177
2178 int wxWindowMac::GetScrollPos(int orient) const
2179 {
2180 if ( orient == wxHORIZONTAL )
2181 {
2182 if ( m_hScrollBar )
2183 return m_hScrollBar->GetThumbPosition() ;
2184 }
2185 else
2186 {
2187 if ( m_vScrollBar )
2188 return m_vScrollBar->GetThumbPosition() ;
2189 }
2190 return 0;
2191 }
2192
2193 // This now returns the whole range, not just the number
2194 // of positions that we can scroll.
2195 int wxWindowMac::GetScrollRange(int orient) const
2196 {
2197 if ( orient == wxHORIZONTAL )
2198 {
2199 if ( m_hScrollBar )
2200 return m_hScrollBar->GetRange() ;
2201 }
2202 else
2203 {
2204 if ( m_vScrollBar )
2205 return m_vScrollBar->GetRange() ;
2206 }
2207 return 0;
2208 }
2209
2210 int wxWindowMac::GetScrollThumb(int orient) const
2211 {
2212 if ( orient == wxHORIZONTAL )
2213 {
2214 if ( m_hScrollBar )
2215 return m_hScrollBar->GetThumbSize() ;
2216 }
2217 else
2218 {
2219 if ( m_vScrollBar )
2220 return m_vScrollBar->GetThumbSize() ;
2221 }
2222 return 0;
2223 }
2224
2225 void wxWindowMac::SetScrollPos(int orient, int pos, bool refresh)
2226 {
2227 if ( orient == wxHORIZONTAL )
2228 {
2229 if ( m_hScrollBar )
2230 m_hScrollBar->SetThumbPosition( pos ) ;
2231 }
2232 else
2233 {
2234 if ( m_vScrollBar )
2235 m_vScrollBar->SetThumbPosition( pos ) ;
2236 }
2237 }
2238
2239 void wxWindowMac::MacPaintBorders( int left , int top )
2240 {
2241 if( IsTopLevel() )
2242 return ;
2243
2244 Rect rect ;
2245 m_peer->GetRect( &rect ) ;
2246 InsetRect( &rect, -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2247
2248 if ( !IsTopLevel() )
2249 {
2250 wxTopLevelWindowMac* top = MacGetTopLevelWindow();
2251 if (top)
2252 {
2253 wxPoint pt(0,0) ;
2254 wxMacControl::Convert( &pt , GetParent()->m_peer , top->m_peer ) ;
2255 rect.left += pt.x ;
2256 rect.right += pt.x ;
2257 rect.top += pt.y ;
2258 rect.bottom += pt.y ;
2259 }
2260 }
2261
2262 if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
2263 {
2264 Rect srect = rect ;
2265 SInt32 border = 0 ;
2266 GetThemeMetric( kThemeMetricEditTextFrameOutset , &border ) ;
2267 InsetRect( &srect , border , border );
2268 DrawThemeEditTextFrame(&srect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
2269 }
2270 else if (HasFlag(wxSIMPLE_BORDER))
2271 {
2272 Rect srect = rect ;
2273 SInt32 border = 0 ;
2274 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2275 InsetRect( &srect , border , border );
2276 DrawThemeListBoxFrame(&rect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
2277 }
2278 }
2279
2280 void wxWindowMac::RemoveChild( wxWindowBase *child )
2281 {
2282 if ( child == m_hScrollBar )
2283 m_hScrollBar = NULL ;
2284 if ( child == m_vScrollBar )
2285 m_vScrollBar = NULL ;
2286
2287 wxWindowBase::RemoveChild( child ) ;
2288 }
2289
2290 // New function that will replace some of the above.
2291 void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible,
2292 int range, bool refresh)
2293 {
2294 if ( orient == wxHORIZONTAL )
2295 {
2296 if ( m_hScrollBar )
2297 {
2298 if ( range == 0 || thumbVisible >= range )
2299 {
2300 if ( m_hScrollBar->IsShown() )
2301 m_hScrollBar->Show(false) ;
2302 }
2303 else
2304 {
2305 if ( !m_hScrollBar->IsShown() )
2306 m_hScrollBar->Show(true) ;
2307 m_hScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2308 }
2309 }
2310 }
2311 else
2312 {
2313 if ( m_vScrollBar )
2314 {
2315 if ( range == 0 || thumbVisible >= range )
2316 {
2317 if ( m_vScrollBar->IsShown() )
2318 m_vScrollBar->Show(false) ;
2319 }
2320 else
2321 {
2322 if ( !m_vScrollBar->IsShown() )
2323 m_vScrollBar->Show(true) ;
2324 m_vScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2325 }
2326 }
2327 }
2328 MacRepositionScrollBars() ;
2329 }
2330
2331 // Does a physical scroll
2332 void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect)
2333 {
2334 if( dx == 0 && dy ==0 )
2335 return ;
2336
2337
2338 {
2339
2340 int width , height ;
2341 GetClientSize( &width , &height ) ;
2342 #if TARGET_API_MAC_OSX
2343 // note there currently is a bug in OSX which makes inefficient refreshes in case an entire control
2344 // area is scrolled, this does not occur if width and height are 2 pixels less,
2345 // TODO write optimal workaround
2346 wxRect scrollrect( MacGetLeftBorderSize() , MacGetTopBorderSize() , width , height ) ;
2347 if ( rect )
2348 {
2349 scrollrect.Intersect( *rect ) ;
2350 }
2351 if ( m_peer->GetNeedsDisplay() )
2352 {
2353 // becuase HIViewScrollRect does not scroll the already invalidated area we have two options
2354 // either immediate redraw or full invalidate
2355 #if 1
2356 // is the better overall solution, as it does not slow down scrolling
2357 m_peer->SetNeedsDisplay( true ) ;
2358 #else
2359 // this would be the preferred version for fast drawing controls
2360 if( UMAGetSystemVersion() < 0x1030 )
2361 Update() ;
2362 else
2363 HIViewRender(m_peer->GetControlRef()) ;
2364 #endif
2365 }
2366 // as the native control might be not a 0/0 wx window coordinates, we have to offset
2367 scrollrect.Offset( -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2368 m_peer->ScrollRect( scrollrect , dx , dy ) ;
2369 #else
2370
2371 wxPoint pos;
2372 pos.x = pos.y = 0;
2373
2374 Rect scrollrect;
2375 RgnHandle updateRgn = NewRgn() ;
2376
2377 {
2378 wxClientDC dc(this) ;
2379 wxMacPortSetter helper(&dc) ;
2380
2381 m_peer->GetRect( &scrollrect ) ;
2382 scrollrect.top += MacGetTopBorderSize() ;
2383 scrollrect.left += MacGetLeftBorderSize() ;
2384 scrollrect.bottom = scrollrect.top + height ;
2385 scrollrect.right = scrollrect.left + width ;
2386
2387 if ( rect )
2388 {
2389 Rect r = { dc.YLOG2DEVMAC(rect->y) , dc.XLOG2DEVMAC(rect->x) , dc.YLOG2DEVMAC(rect->y + rect->height) ,
2390 dc.XLOG2DEVMAC(rect->x + rect->width) } ;
2391 SectRect( &scrollrect , &r , &scrollrect ) ;
2392 }
2393 ScrollRect( &scrollrect , dx , dy , updateRgn ) ;
2394 }
2395 // ScrollWindowRect( (WindowRef) MacGetTopLevelWindowRef() , &scrollrect , dx , dy , kScrollWindowInvalidate, updateRgn ) ;
2396 #endif
2397 }
2398
2399 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
2400 {
2401 wxWindowMac *child = node->GetData();
2402 if (child == m_vScrollBar) continue;
2403 if (child == m_hScrollBar) continue;
2404 if (child->IsTopLevel()) continue;
2405
2406 int x,y;
2407 child->GetPosition( &x, &y );
2408 int w,h;
2409 child->GetSize( &w, &h );
2410 if (rect)
2411 {
2412 wxRect rc(x,y,w,h);
2413 if (rect->Intersects(rc))
2414 child->SetSize( x+dx, y+dy, w, h );
2415 }
2416 else
2417 {
2418 child->SetSize( x+dx, y+dy, w, h );
2419 }
2420 }
2421 }
2422
2423 void wxWindowMac::MacOnScroll(wxScrollEvent &event )
2424 {
2425 if ( event.m_eventObject == m_vScrollBar || event.m_eventObject == m_hScrollBar )
2426 {
2427 wxScrollWinEvent wevent;
2428 wevent.SetPosition(event.GetPosition());
2429 wevent.SetOrientation(event.GetOrientation());
2430 wevent.m_eventObject = this;
2431
2432 if (event.m_eventType == wxEVT_SCROLL_TOP)
2433 wevent.m_eventType = wxEVT_SCROLLWIN_TOP;
2434 else if (event.m_eventType == wxEVT_SCROLL_BOTTOM)
2435 wevent.m_eventType = wxEVT_SCROLLWIN_BOTTOM;
2436 else if (event.m_eventType == wxEVT_SCROLL_LINEUP)
2437 wevent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
2438 else if (event.m_eventType == wxEVT_SCROLL_LINEDOWN)
2439 wevent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
2440 else if (event.m_eventType == wxEVT_SCROLL_PAGEUP)
2441 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEUP;
2442 else if (event.m_eventType == wxEVT_SCROLL_PAGEDOWN)
2443 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEDOWN;
2444 else if (event.m_eventType == wxEVT_SCROLL_THUMBTRACK)
2445 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBTRACK;
2446 else if (event.m_eventType == wxEVT_SCROLL_THUMBRELEASE)
2447 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBRELEASE;
2448
2449 GetEventHandler()->ProcessEvent(wevent);
2450 }
2451 }
2452
2453 // Get the window with the focus
2454 wxWindowMac *wxWindowBase::FindFocus()
2455 {
2456 ControlRef control ;
2457 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
2458 return wxFindControlFromMacControl( control ) ;
2459 }
2460
2461 void wxWindowMac::OnSetFocus(wxFocusEvent& event)
2462 {
2463 // panel wants to track the window which was the last to have focus in it,
2464 // so we want to set ourselves as the window which last had focus
2465 //
2466 // notice that it's also important to do it upwards the tree becaus
2467 // otherwise when the top level panel gets focus, it won't set it back to
2468 // us, but to some other sibling
2469
2470 // CS:don't know if this is still needed:
2471 //wxChildFocusEvent eventFocus(this);
2472 //(void)GetEventHandler()->ProcessEvent(eventFocus);
2473
2474 if ( MacGetTopLevelWindow() && m_peer->NeedsFocusRect() )
2475 {
2476 wxMacWindowStateSaver sv( this ) ;
2477
2478 int w , h ;
2479 int x , y ;
2480 x = y = 0 ;
2481 MacWindowToRootWindow( &x , &y ) ;
2482 GetSize( &w , &h ) ;
2483 Rect rect = {y , x , h + y , w + x } ;
2484
2485 if ( event.GetEventType() == wxEVT_SET_FOCUS )
2486 DrawThemeFocusRect( &rect , true ) ;
2487 else
2488 {
2489 DrawThemeFocusRect( &rect , false ) ;
2490
2491 // as this erases part of the frame we have to redraw borders
2492 // and because our z-ordering is not always correct (staticboxes)
2493 // we have to invalidate things, we cannot simple redraw
2494 RgnHandle updateInner = NewRgn() , updateOuter = NewRgn() ;
2495 RectRgn( updateInner , &rect ) ;
2496 InsetRect( &rect , -4 , -4 ) ;
2497 RectRgn( updateOuter , &rect ) ;
2498 DiffRgn( updateOuter , updateInner ,updateOuter ) ;
2499 wxPoint parent(0,0);
2500 GetParent()->MacWindowToRootWindow( &parent.x , &parent.y ) ;
2501 parent -= GetParent()->GetClientAreaOrigin() ;
2502 OffsetRgn( updateOuter , -parent.x , -parent.y ) ;
2503 GetParent()->m_peer->SetNeedsDisplay( true , updateOuter ) ;
2504 DisposeRgn(updateOuter) ;
2505 DisposeRgn(updateInner) ;
2506 }
2507 }
2508
2509 event.Skip();
2510 }
2511
2512 void wxWindowMac::OnInternalIdle()
2513 {
2514 // This calls the UI-update mechanism (querying windows for
2515 // menu/toolbar/control state information)
2516 if (wxUpdateUIEvent::CanUpdate(this))
2517 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2518 }
2519
2520 // Raise the window to the top of the Z order
2521 void wxWindowMac::Raise()
2522 {
2523 m_peer->SetZOrder( true , NULL ) ;
2524 }
2525
2526 // Lower the window to the bottom of the Z order
2527 void wxWindowMac::Lower()
2528 {
2529 m_peer->SetZOrder( false , NULL ) ;
2530 }
2531
2532
2533 // static wxWindow *gs_lastWhich = NULL;
2534
2535 bool wxWindowMac::MacSetupCursor( const wxPoint& pt)
2536 {
2537 // first trigger a set cursor event
2538
2539 wxPoint clientorigin = GetClientAreaOrigin() ;
2540 wxSize clientsize = GetClientSize() ;
2541 wxCursor cursor ;
2542 if ( wxRect2DInt( clientorigin.x , clientorigin.y , clientsize.x , clientsize.y ).Contains( wxPoint2DInt( pt ) ) )
2543 {
2544 wxSetCursorEvent event( pt.x , pt.y );
2545
2546 bool processedEvtSetCursor = GetEventHandler()->ProcessEvent(event);
2547 if ( processedEvtSetCursor && event.HasCursor() )
2548 {
2549 cursor = event.GetCursor() ;
2550 }
2551 else
2552 {
2553
2554 // the test for processedEvtSetCursor is here to prevent using m_cursor
2555 // if the user code caught EVT_SET_CURSOR() and returned nothing from
2556 // it - this is a way to say that our cursor shouldn't be used for this
2557 // point
2558 if ( !processedEvtSetCursor && m_cursor.Ok() )
2559 {
2560 cursor = m_cursor ;
2561 }
2562 if ( wxIsBusy() )
2563 {
2564 }
2565 else
2566 {
2567 if ( !GetParent() )
2568 cursor = *wxSTANDARD_CURSOR ;
2569 }
2570 }
2571 if ( cursor.Ok() )
2572 cursor.MacInstall() ;
2573 }
2574 return cursor.Ok() ;
2575 }
2576
2577 wxString wxWindowMac::MacGetToolTipString( wxPoint &pt )
2578 {
2579 if ( m_tooltip )
2580 {
2581 return m_tooltip->GetTip() ;
2582 }
2583 return wxEmptyString ;
2584 }
2585
2586 void wxWindowMac::Update()
2587 {
2588 #if TARGET_API_MAC_OSX
2589 WindowRef window = (WindowRef)MacGetTopLevelWindowRef() ;
2590 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
2591 // for composited windows this also triggers a redraw of all
2592 // invalid views in the window
2593 if( UMAGetSystemVersion() >= 0x1030 )
2594 HIWindowFlush(window) ;
2595 else
2596 #endif
2597 {
2598 // the only way to trigger the redrawing on earlier systems is to call
2599 // ReceiveNextEvent
2600
2601 EventRef currentEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
2602 UInt32 currentEventClass = 0 ;
2603 UInt32 currentEventKind = 0 ;
2604 if ( currentEvent != NULL )
2605 {
2606 currentEventClass = ::GetEventClass( currentEvent ) ;
2607 currentEventKind = ::GetEventKind( currentEvent ) ;
2608 }
2609 if ( currentEventClass != kEventClassMenu )
2610 {
2611 // when tracking a menu, strange redraw errors occur if we flush now, so leave..
2612
2613 EventRef theEvent;
2614 OSStatus status = noErr ;
2615 status = ReceiveNextEvent( 0 , NULL , kEventDurationNoWait , false , &theEvent ) ;
2616 }
2617 else
2618 m_peer->SetNeedsDisplay( true ) ;
2619 }
2620 #else
2621 ::Draw1Control( m_peer->GetControlRef() ) ;
2622 #endif
2623 }
2624
2625 wxTopLevelWindowMac* wxWindowMac::MacGetTopLevelWindow() const
2626 {
2627 wxTopLevelWindowMac* win = NULL ;
2628 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
2629 if ( window )
2630 {
2631 win = wxFindWinFromMacWindow( window ) ;
2632 }
2633 return win ;
2634 }
2635 wxRegion wxWindowMac::MacGetVisibleRegion( bool includeOuterStructures )
2636 {
2637 // includeOuterStructures is true if we try to draw somthing like a focus ring etc.
2638 // also a window dc uses this, in this case we only clip in the hierarchy for hard
2639 // borders like a scrollwindow, splitter etc otherwise we end up in a paranoia having
2640 // to add focus borders everywhere
2641
2642 Rect r ;
2643 RgnHandle visRgn = NewRgn() ;
2644 RgnHandle tempRgn = NewRgn() ;
2645 if ( m_peer->IsVisible())
2646 {
2647 m_peer->GetRect( &r ) ;
2648 r.left -= MacGetLeftBorderSize() ;
2649 r.top -= MacGetTopBorderSize() ;
2650 r.bottom += MacGetBottomBorderSize() ;
2651 r.right += MacGetRightBorderSize() ;
2652
2653 if (! MacGetTopLevelWindow()->MacUsesCompositing() )
2654 {
2655 MacRootWindowToWindow( &r.left , & r.top ) ;
2656 MacRootWindowToWindow( &r.right , & r.bottom ) ;
2657 }
2658 else
2659 {
2660 r.right -= r.left ;
2661 r.bottom -= r.top ;
2662 r.left = 0 ;
2663 r.top = 0 ;
2664 }
2665 if ( includeOuterStructures )
2666 InsetRect( &r , -4 , -4 ) ;
2667 RectRgn( visRgn , &r ) ;
2668
2669 if ( !IsTopLevel() )
2670 {
2671 wxWindow* child = this ;
2672 wxWindow* parent = child->GetParent() ;
2673 while( parent )
2674 {
2675 int x , y ;
2676 wxSize size ;
2677 // we have to find a better clipping algorithm here, in order not to clip things
2678 // positioned like status and toolbar
2679 if ( 1 /* parent->IsTopLevel() && child->IsKindOf( CLASSINFO( wxToolBar ) ) */ )
2680 {
2681 size = parent->GetSize() ;
2682 x = y = 0 ;
2683 }
2684 else
2685 {
2686 size = parent->GetClientSize() ;
2687 wxPoint origin = parent->GetClientAreaOrigin() ;
2688 x = origin.x ;
2689 y = origin.y ;
2690 }
2691 parent->MacWindowToRootWindow( &x, &y ) ;
2692 MacRootWindowToWindow( &x , &y ) ;
2693
2694 if ( !includeOuterStructures || (
2695 parent->IsKindOf( CLASSINFO( wxScrolledWindow ) ) ||
2696 parent->IsKindOf( CLASSINFO( wxSashLayoutWindow ) ) ||
2697 ( parent->GetParent() && parent->GetParent()->IsKindOf( CLASSINFO( wxSplitterWindow ) ) )
2698 ) )
2699 {
2700 SetRectRgn( tempRgn ,
2701 x + parent->MacGetLeftBorderSize() , y + parent->MacGetTopBorderSize() ,
2702 x + size.x - parent->MacGetRightBorderSize(),
2703 y + size.y - parent->MacGetBottomBorderSize()) ;
2704
2705 SectRgn( visRgn , tempRgn , visRgn ) ;
2706 }
2707 if ( parent->IsTopLevel() )
2708 break ;
2709 child = parent ;
2710 parent = child->GetParent() ;
2711 }
2712 }
2713 }
2714
2715 wxRegion vis = visRgn ;
2716 DisposeRgn( visRgn ) ;
2717 DisposeRgn( tempRgn ) ;
2718 return vis ;
2719 }
2720
2721 /*
2722 This function must not change the updatergn !
2723 */
2724 bool wxWindowMac::MacDoRedraw( WXHRGN updatergnr , long time )
2725 {
2726 RgnHandle updatergn = (RgnHandle) updatergnr ;
2727 bool handled = false ;
2728 Rect updatebounds ;
2729 GetRegionBounds( updatergn , &updatebounds ) ;
2730 // wxLogDebug("update for %s bounds %d , %d , %d , %d",typeid(*this).name() , updatebounds.left , updatebounds.top , updatebounds.right , updatebounds.bottom ) ;
2731 if ( !EmptyRgn(updatergn) )
2732 {
2733 RgnHandle newupdate = NewRgn() ;
2734 wxSize point = GetClientSize() ;
2735 wxPoint origin = GetClientAreaOrigin() ;
2736 SetRectRgn( newupdate , origin.x , origin.y , origin.x + point.x , origin.y+point.y ) ;
2737 SectRgn( newupdate , updatergn , newupdate ) ;
2738
2739 // first send an erase event to the entire update area
2740 {
2741 wxWindowDC dc(this);
2742 dc.SetClippingRegion(wxRegion(updatergn));
2743 wxEraseEvent eevent( GetId(), &dc );
2744 eevent.SetEventObject( this );
2745 GetEventHandler()->ProcessEvent( eevent );
2746 }
2747
2748 // calculate a client-origin version of the update rgn and set m_updateRegion to that
2749 OffsetRgn( newupdate , -origin.x , -origin.y ) ;
2750 m_updateRegion = newupdate ;
2751 DisposeRgn( newupdate ) ;
2752
2753 if ( !m_updateRegion.Empty() )
2754 {
2755 // paint the window itself
2756 wxPaintEvent event;
2757 event.m_timeStamp = time ;
2758 event.SetEventObject(this);
2759 handled = GetEventHandler()->ProcessEvent(event);
2760
2761 // we have to call the default built-in handler, as otherwise our frames will be drawn and immediately erased afterwards
2762 if ( !handled )
2763 {
2764 if ( wxTheApp->MacGetCurrentEvent() != NULL && wxTheApp->MacGetCurrentEventHandlerCallRef() != NULL )
2765 {
2766 CallNextEventHandler((EventHandlerCallRef)wxTheApp->MacGetCurrentEventHandlerCallRef() , (EventRef) wxTheApp->MacGetCurrentEvent() ) ;
2767 handled = true ;
2768 }
2769 }
2770
2771 }
2772
2773 // now we cannot rely on having its borders drawn by a window itself, as it does not
2774 // get the updateRgn wide enough to always do so, so we do it from the parent
2775 // this would also be the place to draw any custom backgrounds for native controls
2776 // in Composited windowing
2777 wxPoint clientOrigin = GetClientAreaOrigin() ;
2778
2779 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
2780 {
2781 wxWindowMac *child = node->GetData();
2782 if (child == m_vScrollBar) continue;
2783 if (child == m_hScrollBar) continue;
2784 if (child->IsTopLevel()) continue;
2785 if (!child->IsShown()) continue;
2786
2787 int x,y;
2788 child->GetPosition( &x, &y );
2789 int w,h;
2790 child->GetSize( &w, &h );
2791 Rect childRect = { y , x , y + h , x + w } ;
2792 OffsetRect( &childRect , clientOrigin.x , clientOrigin.y ) ;
2793 if ( child->MacGetTopBorderSize() )
2794 {
2795 if ( RectInRgn( &childRect , updatergn ) )
2796 {
2797 // paint custom borders
2798 wxNcPaintEvent eventNc( child->GetId() );
2799 eventNc.SetEventObject( child );
2800 if ( !child->GetEventHandler()->ProcessEvent( eventNc ) )
2801 {
2802 wxWindowDC dc(this) ;
2803 dc.SetClippingRegion(wxRegion(updatergn));
2804 wxMacPortSetter helper(&dc) ;
2805 child->MacPaintBorders( dc.m_macLocalOrigin.x + childRect.left , dc.m_macLocalOrigin.y + childRect.top) ;
2806 }
2807 }
2808 }
2809 if ( child->m_peer->NeedsFocusRect() && child->m_peer->HasFocus() )
2810 {
2811 wxWindowDC dc(this) ;
2812 dc.SetClippingRegion(wxRegion(updatergn));
2813 wxMacPortSetter helper(&dc) ;
2814 OffsetRect( &childRect , dc.m_macLocalOrigin.x , dc.m_macLocalOrigin.y ) ;
2815 DrawThemeFocusRect( &childRect , true ) ;
2816 }
2817 }
2818 }
2819 return handled ;
2820 }
2821
2822 void wxWindowMac::MacRedraw( WXHRGN updatergnr , long time, bool erase)
2823 {
2824 RgnHandle updatergn = (RgnHandle) updatergnr ;
2825 // updatergn is always already clipped to our boundaries
2826 // if we are in compositing mode then it is in relative to the upper left of the control
2827 // if we are in non-compositing, then it is relatvie to the uppder left of the content area
2828 // of the toplevel window
2829 // it is in window coordinates, not in client coordinates
2830
2831 // ownUpdateRgn is the area that this window has to repaint, it is in window coordinates
2832 RgnHandle ownUpdateRgn = NewRgn() ;
2833 CopyRgn( updatergn , ownUpdateRgn ) ;
2834
2835 if ( MacGetTopLevelWindow()->MacUsesCompositing() == false )
2836 {
2837 Rect bounds;
2838 m_peer->GetRectInWindowCoords( &bounds );
2839 RgnHandle controlRgn = NewRgn();
2840 RectRgn( controlRgn, &bounds );
2841 //KO: This sets the ownUpdateRgn to the area of this control that is inside
2842 // the window update region
2843 SectRgn( ownUpdateRgn, controlRgn, ownUpdateRgn );
2844 DisposeRgn( controlRgn );
2845
2846 //KO: convert ownUpdateRgn to local coordinates
2847 OffsetRgn( ownUpdateRgn, -bounds.left, -bounds.top );
2848 }
2849
2850 MacDoRedraw( ownUpdateRgn , time ) ;
2851 DisposeRgn( ownUpdateRgn ) ;
2852
2853 }
2854
2855 WXWindow wxWindowMac::MacGetTopLevelWindowRef() const
2856 {
2857 wxWindowMac *iter = (wxWindowMac*)this ;
2858
2859 while( iter )
2860 {
2861 if ( iter->IsTopLevel() )
2862 return ((wxTopLevelWindow*)iter)->MacGetWindowRef() ;
2863
2864 iter = iter->GetParent() ;
2865 }
2866 wxASSERT_MSG( 1 , wxT("No valid mac root window") ) ;
2867 return NULL ;
2868 }
2869
2870 void wxWindowMac::MacCreateScrollBars( long style )
2871 {
2872 wxASSERT_MSG( m_vScrollBar == NULL && m_hScrollBar == NULL , wxT("attempt to create window twice") ) ;
2873
2874 if ( style & ( wxVSCROLL | wxHSCROLL ) )
2875 {
2876 bool hasBoth = ( style & wxVSCROLL ) && ( style & wxHSCROLL ) ;
2877 int scrlsize = MAC_SCROLLBAR_SIZE ;
2878 wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL ;
2879 if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL || GetWindowVariant() == wxWINDOW_VARIANT_MINI )
2880 {
2881 scrlsize = MAC_SMALL_SCROLLBAR_SIZE ;
2882 variant = wxWINDOW_VARIANT_SMALL ;
2883 }
2884
2885 int adjust = hasBoth ? scrlsize - 1: 0 ;
2886 int width, height ;
2887 GetClientSize( &width , &height ) ;
2888
2889 wxPoint vPoint(width-scrlsize, 0) ;
2890 wxSize vSize(scrlsize, height - adjust) ;
2891 wxPoint hPoint(0 , height-scrlsize ) ;
2892 wxSize hSize( width - adjust, scrlsize) ;
2893
2894
2895 if ( style & wxVSCROLL )
2896 {
2897 m_vScrollBar = new wxScrollBar(this, wxWINDOW_VSCROLL, vPoint,
2898 vSize , wxVERTICAL);
2899 }
2900
2901 if ( style & wxHSCROLL )
2902 {
2903 m_hScrollBar = new wxScrollBar(this, wxWINDOW_HSCROLL, hPoint,
2904 hSize , wxHORIZONTAL);
2905 }
2906 }
2907
2908
2909 // because the create does not take into account the client area origin
2910 MacRepositionScrollBars() ; // we might have a real position shift
2911 }
2912
2913 void wxWindowMac::MacRepositionScrollBars()
2914 {
2915 if ( !m_hScrollBar && !m_vScrollBar )
2916 return ;
2917
2918 bool hasBoth = ( m_hScrollBar && m_hScrollBar->IsShown()) && ( m_vScrollBar && m_vScrollBar->IsShown()) ;
2919 int scrlsize = m_hScrollBar ? m_hScrollBar->GetSize().y : ( m_vScrollBar ? m_vScrollBar->GetSize().x : MAC_SCROLLBAR_SIZE ) ;
2920 int adjust = hasBoth ? scrlsize - 1 : 0 ;
2921
2922 // get real client area
2923
2924 int width ;
2925 int height ;
2926 GetSize( &width , &height ) ;
2927
2928 width -= MacGetLeftBorderSize() + MacGetRightBorderSize();
2929 height -= MacGetTopBorderSize() + MacGetBottomBorderSize();
2930
2931 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
2932 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
2933 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
2934 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
2935 /*
2936 int x = 0 ;
2937 int y = 0 ;
2938 int w ;
2939 int h ;
2940 GetSize( &w , &h ) ;
2941
2942 MacClientToRootWindow( &x , &y ) ;
2943 MacClientToRootWindow( &w , &h ) ;
2944
2945 wxWindowMac *iter = (wxWindowMac*)this ;
2946
2947 int totW = 10000 , totH = 10000;
2948 while( iter )
2949 {
2950 if ( iter->IsTopLevel() )
2951 {
2952 iter->GetSize( &totW , &totH ) ;
2953 break ;
2954 }
2955
2956 iter = iter->GetParent() ;
2957 }
2958
2959 if ( x == 0 )
2960 {
2961 hPoint.x = -1 ;
2962 hSize.x += 1 ;
2963 }
2964 if ( y == 0 )
2965 {
2966 vPoint.y = -1 ;
2967 vSize.y += 1 ;
2968 }
2969
2970 if ( w-x >= totW )
2971 {
2972 hSize.x += 1 ;
2973 vPoint.x += 1 ;
2974 }
2975
2976 if ( h-y >= totH )
2977 {
2978 vSize.y += 1 ;
2979 hPoint.y += 1 ;
2980 }
2981 */
2982 if ( m_vScrollBar )
2983 {
2984 m_vScrollBar->SetSize( vPoint.x , vPoint.y, vSize.x, vSize.y , wxSIZE_ALLOW_MINUS_ONE);
2985 }
2986 if ( m_hScrollBar )
2987 {
2988 m_hScrollBar->SetSize( hPoint.x , hPoint.y, hSize.x, hSize.y, wxSIZE_ALLOW_MINUS_ONE);
2989 }
2990 }
2991
2992 bool wxWindowMac::AcceptsFocus() const
2993 {
2994 return MacCanFocus() && wxWindowBase::AcceptsFocus();
2995 }
2996
2997 void wxWindowMac::MacSuperChangedPosition()
2998 {
2999 // only window-absolute structures have to be moved i.e. controls
3000
3001 wxWindowListNode *node = GetChildren().GetFirst();
3002 while ( node )
3003 {
3004 wxWindowMac *child = node->GetData();
3005 child->MacSuperChangedPosition() ;
3006 node = node->GetNext();
3007 }
3008 }
3009
3010 void wxWindowMac::MacTopLevelWindowChangedPosition()
3011 {
3012 // only screen-absolute structures have to be moved i.e. glcanvas
3013
3014 wxWindowListNode *node = GetChildren().GetFirst();
3015 while ( node )
3016 {
3017 wxWindowMac *child = node->GetData();
3018 child->MacTopLevelWindowChangedPosition() ;
3019 node = node->GetNext();
3020 }
3021 }
3022
3023 long wxWindowMac::MacGetLeftBorderSize( ) const
3024 {
3025 if( IsTopLevel() )
3026 return 0 ;
3027
3028 if (m_windowStyle & wxRAISED_BORDER || m_windowStyle & wxSUNKEN_BORDER )
3029 {
3030 SInt32 border = 3 ;
3031 return border ;
3032 }
3033 else if ( m_windowStyle &wxDOUBLE_BORDER)
3034 {
3035 SInt32 border = 3 ;
3036 return border ;
3037 }
3038 else if (m_windowStyle &wxSIMPLE_BORDER)
3039 {
3040 return 1 ;
3041 }
3042 return 0 ;
3043 }
3044
3045 long wxWindowMac::MacGetRightBorderSize( ) const
3046 {
3047 // they are all symmetric in mac themes
3048 return MacGetLeftBorderSize() ;
3049 }
3050
3051 long wxWindowMac::MacGetTopBorderSize( ) const
3052 {
3053 // they are all symmetric in mac themes
3054 return MacGetLeftBorderSize() ;
3055 }
3056
3057 long wxWindowMac::MacGetBottomBorderSize( ) const
3058 {
3059 // they are all symmetric in mac themes
3060 return MacGetLeftBorderSize() ;
3061 }
3062
3063 long wxWindowMac::MacRemoveBordersFromStyle( long style )
3064 {
3065 return style & ~wxBORDER_MASK ;
3066 }
3067
3068 // Find the wxWindowMac at the current mouse position, returning the mouse
3069 // position.
3070 wxWindowMac* wxFindWindowAtPointer(wxPoint& pt)
3071 {
3072 pt = wxGetMousePosition();
3073 wxWindowMac* found = wxFindWindowAtPoint(pt);
3074 return found;
3075 }
3076
3077 // Get the current mouse position.
3078 wxPoint wxGetMousePosition()
3079 {
3080 int x, y;
3081 wxGetMousePosition(& x, & y);
3082 return wxPoint(x, y);
3083 }
3084
3085 void wxWindowMac::OnMouseEvent( wxMouseEvent &event )
3086 {
3087 if ( event.GetEventType() == wxEVT_RIGHT_DOWN )
3088 {
3089 // copied from wxGTK : CS
3090 // generate a "context menu" event: this is similar to wxEVT_RIGHT_DOWN
3091 // except that:
3092 //
3093 // (a) it's a command event and so is propagated to the parent
3094 // (b) under MSW it can be generated from kbd too
3095 // (c) it uses screen coords (because of (a))
3096 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
3097 this->GetId(),
3098 this->ClientToScreen(event.GetPosition()));
3099 if ( ! GetEventHandler()->ProcessEvent(evtCtx) )
3100 event.Skip() ;
3101 }
3102 else
3103 {
3104 event.Skip() ;
3105 }
3106 }
3107
3108 void wxWindowMac::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED( mouseStillDown ) )
3109 {
3110 }
3111
3112 Rect wxMacGetBoundsForControl( wxWindow* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
3113 {
3114 int x ,y , w ,h ;
3115
3116 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin) ;
3117 Rect bounds = { y , x , y+h , x+w };
3118 return bounds ;
3119 }
3120
3121 wxInt32 wxWindowMac::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
3122 {
3123 return eventNotHandledErr ;
3124 }
3125
3126