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