]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/window.cpp
final sweep over docs - replace & with \&
[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 m_windowVariant = parent->GetWindowVariant() ;
814
815 if ( m_macIsUserPane )
816 {
817 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
818
819 UInt32 features = 0
820 | kControlSupportsEmbedding
821 // | kControlSupportsLiveFeedback
822 // | kControlHasSpecialBackground
823 // | kControlSupportsCalcBestRect
824 // | kControlHandlesTracking
825 | kControlSupportsFocus
826 // | kControlWantsActivate
827 // | kControlWantsIdle
828 ;
829
830 m_peer = new wxMacControl() ;
831 ::CreateUserPaneControl( MAC_WXHWND(GetParent()->MacGetTopLevelWindowRef()) , &bounds, features , m_peer->GetControlRefAddr() );
832
833
834 MacPostControlCreate(pos,size) ;
835 #if !TARGET_API_MAC_OSX
836 m_peer->SetData<ControlUserPaneDrawUPP>(kControlEntireControl,kControlUserPaneDrawProcTag,&gControlUserPaneDrawUPP) ;
837 m_peer->SetData<ControlUserPaneHitTestUPP>(kControlEntireControl,kControlUserPaneHitTestProcTag,&gControlUserPaneHitTestUPP) ;
838 m_peer->SetData<ControlUserPaneTrackingUPP>(kControlEntireControl,kControlUserPaneTrackingProcTag,&gControlUserPaneTrackingUPP) ;
839 m_peer->SetData<ControlUserPaneIdleUPP>(kControlEntireControl,kControlUserPaneIdleProcTag,&gControlUserPaneIdleUPP) ;
840 m_peer->SetData<ControlUserPaneKeyDownUPP>(kControlEntireControl,kControlUserPaneKeyDownProcTag,&gControlUserPaneKeyDownUPP) ;
841 m_peer->SetData<ControlUserPaneActivateUPP>(kControlEntireControl,kControlUserPaneActivateProcTag,&gControlUserPaneActivateUPP) ;
842 m_peer->SetData<ControlUserPaneFocusUPP>(kControlEntireControl,kControlUserPaneFocusProcTag,&gControlUserPaneFocusUPP) ;
843 m_peer->SetData<ControlUserPaneBackgroundUPP>(kControlEntireControl,kControlUserPaneBackgroundProcTag,&gControlUserPaneBackgroundUPP) ;
844 #endif
845 }
846 #ifndef __WXUNIVERSAL__
847 // Don't give scrollbars to wxControls unless they ask for them
848 if ( (! IsKindOf(CLASSINFO(wxControl)) && ! IsKindOf(CLASSINFO(wxStatusBar))) ||
849 (IsKindOf(CLASSINFO(wxControl)) && ( style & wxHSCROLL || style & wxVSCROLL)))
850 {
851 MacCreateScrollBars( style ) ;
852 }
853 #endif
854
855 wxWindowCreateEvent event(this);
856 GetEventHandler()->AddPendingEvent(event);
857
858 return TRUE;
859 }
860
861 void wxWindowMac::MacPostControlCreate(const wxPoint& pos, const wxSize& size)
862 {
863 wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid mac control") ) ;
864
865 m_peer->SetReference( (long) this ) ;
866 GetParent()->AddChild(this);
867
868 MacInstallEventHandler( (WXWidget) m_peer->GetControlRef() );
869
870 ControlRef container = (ControlRef) GetParent()->GetHandle() ;
871 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
872 ::EmbedControl( m_peer->GetControlRef() , container ) ;
873
874 // adjust font, controlsize etc
875 DoSetWindowVariant( m_windowVariant ) ;
876
877 #if !TARGET_API_MAC_OSX
878 // eventually we can fix some clipping issues be reactivating this hook
879 //if ( m_macIsUserPane )
880 // SetControlColorProc( m_peer->GetControlRef() , wxMacSetupControlBackgroundUPP ) ;
881 #endif
882 m_peer->SetTitle( wxStripMenuCodes(m_label) ) ;
883
884 if (!m_macIsUserPane)
885 {
886 SetInitialBestSize(size);
887 }
888
889 SetCursor( *wxSTANDARD_CURSOR ) ;
890 }
891
892 void wxWindowMac::DoSetWindowVariant( wxWindowVariant variant )
893 {
894 // Don't assert, in case we set the window variant before
895 // the window is created
896 // wxASSERT( m_peer->Ok() ) ;
897
898 m_windowVariant = variant ;
899
900 if (m_peer == NULL || !m_peer->Ok())
901 return;
902
903 ControlSize size ;
904 ThemeFontID themeFont = kThemeSystemFont ;
905
906 // we will get that from the settings later
907 // and make this NORMAL later, but first
908 // we have a few calculations that we must fix
909
910 switch ( variant )
911 {
912 case wxWINDOW_VARIANT_NORMAL :
913 size = kControlSizeNormal;
914 themeFont = kThemeSystemFont ;
915 break ;
916 case wxWINDOW_VARIANT_SMALL :
917 size = kControlSizeSmall;
918 themeFont = kThemeSmallSystemFont ;
919 break ;
920 case wxWINDOW_VARIANT_MINI :
921 if (UMAGetSystemVersion() >= 0x1030 )
922 {
923 // not always defined in the headers
924 size = 3 ;
925 themeFont = 109 ;
926 }
927 else
928 {
929 size = kControlSizeSmall;
930 themeFont = kThemeSmallSystemFont ;
931 }
932 break ;
933 case wxWINDOW_VARIANT_LARGE :
934 size = kControlSizeLarge;
935 themeFont = kThemeSystemFont ;
936 break ;
937 default:
938 wxFAIL_MSG(_T("unexpected window variant"));
939 break ;
940 }
941 m_peer->SetData<ControlSize>(kControlEntireControl, kControlSizeTag,&size ) ;
942
943 wxFont font ;
944 font.MacCreateThemeFont( themeFont ) ;
945 SetFont( font ) ;
946 }
947
948 void wxWindowMac::MacUpdateControlFont()
949 {
950 m_peer->SetFont( GetFont() , GetForegroundColour() , GetWindowStyle() ) ;
951 Refresh() ;
952 }
953
954 bool wxWindowMac::SetFont(const wxFont& font)
955 {
956 bool retval = wxWindowBase::SetFont( font ) ;
957
958 MacUpdateControlFont() ;
959
960 return retval;
961 }
962
963 bool wxWindowMac::SetForegroundColour(const wxColour& col )
964 {
965 if ( !wxWindowBase::SetForegroundColour(col) )
966 return false ;
967
968 MacUpdateControlFont() ;
969
970 return true ;
971 }
972
973 bool wxWindowMac::SetBackgroundColour(const wxColour& col )
974 {
975 if ( !wxWindowBase::SetBackgroundColour(col) && m_hasBgCol )
976 return false ;
977
978 wxBrush brush ;
979 wxColour newCol(GetBackgroundColour());
980 if ( newCol == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE) )
981 {
982 brush.MacSetTheme( kThemeBrushDocumentWindowBackground ) ;
983 }
984 else if ( newCol == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE ) )
985 {
986 brush.MacSetTheme( kThemeBrushDialogBackgroundActive ) ;
987 }
988 else
989 {
990 brush.SetColour( newCol ) ;
991 }
992 MacSetBackgroundBrush( brush ) ;
993
994 MacUpdateControlFont() ;
995
996 return true ;
997 }
998
999 void wxWindowMac::MacSetBackgroundBrush( const wxBrush &brush )
1000 {
1001 m_macBackgroundBrush = brush ;
1002 m_peer->SetBackground( brush ) ;
1003 }
1004
1005 bool wxWindowMac::MacCanFocus() const
1006 {
1007 // there is currently no way to determine whether the window is running in full keyboard
1008 // access mode, therefore we cannot rely on these features, yet the only other way would be
1009 // to issue a SetKeyboardFocus event and verify after whether it succeeded, this would risk problems
1010 // in event handlers...
1011 UInt32 features = 0 ;
1012 m_peer->GetFeatures( & features ) ;
1013 return features & ( kControlSupportsFocus | kControlGetsFocusOnClick ) ;
1014 }
1015
1016
1017 void wxWindowMac::SetFocus()
1018 {
1019 if ( AcceptsFocus() )
1020 {
1021
1022 wxWindow* former = FindFocus() ;
1023 if ( former == this )
1024 return ;
1025
1026 OSStatus err = m_peer->SetFocus( kControlFocusNextPart ) ;
1027 // as we cannot rely on the control features to find out whether we are in full keyboard mode, we can only
1028 // leave in case of an error
1029 if ( err == errCouldntSetFocus )
1030 return ;
1031
1032 #if !TARGET_API_MAC_OSX
1033 // emulate carbon events when running under carbonlib where they are not natively available
1034 if ( former )
1035 {
1036 EventRef evRef = NULL ;
1037 verify_noerr( MacCreateEvent( NULL , kEventClassControl , kEventControlSetFocusPart , TicksToEventTime( TickCount() ) , kEventAttributeUserEvent ,
1038 &evRef ) );
1039
1040 wxMacCarbonEvent cEvent( evRef ) ;
1041 cEvent.SetParameter<ControlRef>( kEventParamDirectObject , (ControlRef) former->GetHandle() ) ;
1042 cEvent.SetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode , kControlFocusNoPart ) ;
1043
1044 wxMacWindowEventHandler( NULL , evRef , former ) ;
1045 ReleaseEvent(evRef) ;
1046 }
1047 // send new focus event
1048 {
1049 EventRef evRef = NULL ;
1050 verify_noerr( MacCreateEvent( NULL , kEventClassControl , kEventControlSetFocusPart , TicksToEventTime( TickCount() ) , kEventAttributeUserEvent ,
1051 &evRef ) );
1052
1053 wxMacCarbonEvent cEvent( evRef ) ;
1054 cEvent.SetParameter<ControlRef>( kEventParamDirectObject , (ControlRef) GetHandle() ) ;
1055 cEvent.SetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode , kControlFocusNextPart ) ;
1056
1057 wxMacWindowEventHandler( NULL , evRef , this ) ;
1058 ReleaseEvent(evRef) ;
1059 }
1060 #endif
1061 }
1062 }
1063
1064
1065 void wxWindowMac::DoCaptureMouse()
1066 {
1067 wxTheApp->s_captureWindow = this ;
1068 }
1069
1070 wxWindow* wxWindowBase::GetCapture()
1071 {
1072 return wxTheApp->s_captureWindow ;
1073 }
1074
1075 void wxWindowMac::DoReleaseMouse()
1076 {
1077 wxTheApp->s_captureWindow = NULL ;
1078 }
1079
1080 #if wxUSE_DRAG_AND_DROP
1081
1082 void wxWindowMac::SetDropTarget(wxDropTarget *pDropTarget)
1083 {
1084 if ( m_dropTarget != 0 ) {
1085 delete m_dropTarget;
1086 }
1087
1088 m_dropTarget = pDropTarget;
1089 if ( m_dropTarget != 0 )
1090 {
1091 // TODO
1092 }
1093 }
1094
1095 #endif
1096
1097 // Old style file-manager drag&drop
1098 void wxWindowMac::DragAcceptFiles(bool accept)
1099 {
1100 // TODO
1101 }
1102
1103 // Returns the size of the native control. In the case of the toplevel window
1104 // this is the content area root control
1105
1106 void wxWindowMac::MacGetPositionAndSizeFromControl(int& x, int& y,
1107 int& w, int& h) const
1108 {
1109 Rect bounds ;
1110 m_peer->GetRect( &bounds ) ;
1111
1112
1113 x = bounds.left ;
1114 y = bounds.top ;
1115 w = bounds.right - bounds.left ;
1116 h = bounds.bottom - bounds.top ;
1117 }
1118
1119 // From a wx position / size calculate the appropriate size of the native control
1120
1121 bool wxWindowMac::MacGetBoundsForControl(const wxPoint& pos,
1122 const wxSize& size,
1123 int& x, int& y,
1124 int& w, int& h , bool adjustOrigin ) const
1125 {
1126 // the desired size, minus the border pixels gives the correct size of the control
1127
1128 x = (int)pos.x;
1129 y = (int)pos.y;
1130 // todo the default calls may be used as soon as PostCreateControl Is moved here
1131 w = size.x ; // WidthDefault( size.x );
1132 h = size.y ; // HeightDefault( size.y ) ;
1133 #if !TARGET_API_MAC_OSX
1134 GetParent()->MacWindowToRootWindow( &x , &y ) ;
1135 #endif
1136
1137 x += MacGetLeftBorderSize() ;
1138 y += MacGetTopBorderSize() ;
1139 w -= MacGetLeftBorderSize() + MacGetRightBorderSize() ;
1140 h -= MacGetTopBorderSize() + MacGetBottomBorderSize() ;
1141
1142 if ( adjustOrigin )
1143 AdjustForParentClientOrigin( x , y ) ;
1144 #if TARGET_API_MAC_OSX
1145 // this is in window relative coordinate, as this parent may have a border, its physical position is offset by this border
1146 if ( ! GetParent()->IsTopLevel() )
1147 {
1148 x -= GetParent()->MacGetLeftBorderSize() ;
1149 y -= GetParent()->MacGetTopBorderSize() ;
1150 }
1151 #endif
1152 return true ;
1153 }
1154
1155 // Get window size (not client size)
1156 void wxWindowMac::DoGetSize(int *x, int *y) const
1157 {
1158 // take the size of the control and add the borders that have to be drawn outside
1159 int x1 , y1 , w1 , h1 ;
1160
1161 MacGetPositionAndSizeFromControl( x1 , y1, w1 ,h1 ) ;
1162
1163 w1 += MacGetLeftBorderSize() + MacGetRightBorderSize() ;
1164 h1 += MacGetTopBorderSize() + MacGetBottomBorderSize() ;
1165
1166 if(x) *x = w1 ;
1167 if(y) *y = h1 ;
1168 }
1169
1170 // get the position of the bounds of this window in client coordinates of its parent
1171 void wxWindowMac::DoGetPosition(int *x, int *y) const
1172 {
1173 int x1 , y1 , w1 ,h1 ;
1174 MacGetPositionAndSizeFromControl( x1 , y1, w1 ,h1 ) ;
1175 x1 -= MacGetLeftBorderSize() ;
1176 y1 -= MacGetTopBorderSize() ;
1177 // to non-client
1178 #if !TARGET_API_MAC_OSX
1179 if ( !GetParent()->IsTopLevel() )
1180 {
1181 Rect bounds ;
1182 GetControlBounds( (ControlRef) GetParent()->GetHandle() , &bounds ) ;
1183 x1 -= bounds.left ;
1184 y1 -= bounds.top ;
1185 }
1186 #endif
1187 if ( !IsTopLevel() )
1188 {
1189 wxWindow *parent = GetParent();
1190 if ( parent )
1191 {
1192 // we must first adjust it to be in window coordinates of the parent, as otherwise it gets lost by the clientareaorigin fix
1193 x1 += parent->MacGetLeftBorderSize() ;
1194 y1 += parent->MacGetTopBorderSize() ;
1195 // and now to client coordinates
1196 wxPoint pt(parent->GetClientAreaOrigin());
1197 x1 -= pt.x ;
1198 y1 -= pt.y ;
1199 }
1200 }
1201 if(x) *x = x1 ;
1202 if(y) *y = y1 ;
1203 }
1204
1205 void wxWindowMac::DoScreenToClient(int *x, int *y) const
1206 {
1207 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
1208
1209 wxCHECK_RET( window , wxT("TopLevel Window Missing") ) ;
1210
1211 {
1212 Point localwhere = {0,0} ;
1213
1214 if(x) localwhere.h = * x ;
1215 if(y) localwhere.v = * y ;
1216
1217 QDGlobalToLocalPoint( GetWindowPort( window ) , &localwhere ) ;
1218 if(x) *x = localwhere.h ;
1219 if(y) *y = localwhere.v ;
1220
1221 }
1222 MacRootWindowToWindow( x , y ) ;
1223
1224 wxPoint origin = GetClientAreaOrigin() ;
1225 if(x) *x -= origin.x ;
1226 if(y) *y -= origin.y ;
1227 }
1228
1229 void wxWindowMac::DoClientToScreen(int *x, int *y) const
1230 {
1231 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
1232 wxCHECK_RET( window , wxT("TopLevel Window Missing") ) ;
1233
1234 wxPoint origin = GetClientAreaOrigin() ;
1235 if(x) *x += origin.x ;
1236 if(y) *y += origin.y ;
1237
1238 MacWindowToRootWindow( x , y ) ;
1239
1240 {
1241 Point localwhere = { 0,0 };
1242 if(x) localwhere.h = * x ;
1243 if(y) localwhere.v = * y ;
1244 QDLocalToGlobalPoint( GetWindowPort( window ) , &localwhere ) ;
1245 if(x) *x = localwhere.h ;
1246 if(y) *y = localwhere.v ;
1247 }
1248 }
1249
1250 void wxWindowMac::MacClientToRootWindow( int *x , int *y ) const
1251 {
1252 wxPoint origin = GetClientAreaOrigin() ;
1253 if(x) *x += origin.x ;
1254 if(y) *y += origin.y ;
1255
1256 MacWindowToRootWindow( x , y ) ;
1257 }
1258
1259 void wxWindowMac::MacRootWindowToClient( int *x , int *y ) const
1260 {
1261 MacRootWindowToWindow( x , y ) ;
1262
1263 wxPoint origin = GetClientAreaOrigin() ;
1264 if(x) *x -= origin.x ;
1265 if(y) *y -= origin.y ;
1266 }
1267
1268 void wxWindowMac::MacWindowToRootWindow( int *x , int *y ) const
1269 {
1270 #if TARGET_API_MAC_OSX
1271 wxPoint pt ;
1272 if ( x ) pt.x = *x ;
1273 if ( y ) pt.y = *y ;
1274
1275 if ( !IsTopLevel() )
1276 {
1277 wxTopLevelWindowMac* top = MacGetTopLevelWindow();
1278 if (top)
1279 {
1280 pt.x -= MacGetLeftBorderSize() ;
1281 pt.y -= MacGetTopBorderSize() ;
1282 wxMacControl::Convert( &pt , m_peer , top->m_peer ) ;
1283 }
1284 }
1285
1286 if ( x ) *x = (int) pt.x ;
1287 if ( y ) *y = (int) pt.y ;
1288 #else
1289 if ( !IsTopLevel() )
1290 {
1291 Rect bounds ;
1292 m_peer->GetRect( &bounds ) ;
1293 if(x) *x += bounds.left - MacGetLeftBorderSize() ;
1294 if(y) *y += bounds.top - MacGetTopBorderSize() ;
1295 }
1296 #endif
1297 }
1298
1299 void wxWindowMac::MacWindowToRootWindow( short *x , short *y ) const
1300 {
1301 int x1 , y1 ;
1302 if ( x ) x1 = *x ;
1303 if ( y ) y1 = *y ;
1304 MacWindowToRootWindow( &x1 , &y1 ) ;
1305 if ( x ) *x = x1 ;
1306 if ( y ) *y = y1 ;
1307 }
1308
1309 void wxWindowMac::MacRootWindowToWindow( int *x , int *y ) const
1310 {
1311 #if TARGET_API_MAC_OSX
1312 wxPoint pt ;
1313 if ( x ) pt.x = *x ;
1314 if ( y ) pt.y = *y ;
1315
1316 if ( !IsTopLevel() )
1317 {
1318 wxMacControl::Convert( &pt , MacGetTopLevelWindow()->m_peer , m_peer ) ;
1319 pt.x += MacGetLeftBorderSize() ;
1320 pt.y += MacGetTopBorderSize() ;
1321 }
1322
1323 if ( x ) *x = (int) pt.x ;
1324 if ( y ) *y = (int) pt.y ;
1325 #else
1326 if ( !IsTopLevel() )
1327 {
1328 Rect bounds ;
1329 m_peer->GetRect( &bounds ) ;
1330 if(x) *x -= bounds.left + MacGetLeftBorderSize() ;
1331 if(y) *y -= bounds.top + MacGetTopBorderSize() ;
1332 }
1333 #endif
1334 }
1335
1336 void wxWindowMac::MacRootWindowToWindow( short *x , short *y ) const
1337 {
1338 int x1 , y1 ;
1339 if ( x ) x1 = *x ;
1340 if ( y ) y1 = *y ;
1341 MacRootWindowToWindow( &x1 , &y1 ) ;
1342 if ( x ) *x = x1 ;
1343 if ( y ) *y = y1 ;
1344 }
1345
1346 void wxWindowMac::MacGetContentAreaInset( int &left , int &top , int &right , int &bottom )
1347 {
1348 RgnHandle rgn = NewRgn() ;
1349 Rect content ;
1350 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
1351 {
1352 GetRegionBounds( rgn , &content ) ;
1353 DisposeRgn( rgn ) ;
1354 }
1355 else
1356 {
1357 m_peer->GetRect( &content ) ;
1358 }
1359 Rect structure ;
1360 m_peer->GetRect( &structure ) ;
1361 #if !TARGET_API_MAC_OSX
1362 OffsetRect( &content , -structure.left , -structure.top ) ;
1363 #endif
1364 left = content.left - structure.left ;
1365 top = content.top - structure.top ;
1366 right = structure.right - content.right ;
1367 bottom = structure.bottom - content.bottom ;
1368 }
1369
1370 wxSize wxWindowMac::DoGetSizeFromClientSize( const wxSize & size ) const
1371 {
1372 wxSize sizeTotal = size;
1373
1374 RgnHandle rgn = NewRgn() ;
1375
1376 Rect content ;
1377
1378 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
1379 {
1380 GetRegionBounds( rgn , &content ) ;
1381 DisposeRgn( rgn ) ;
1382 }
1383 else
1384 {
1385 m_peer->GetRect( &content ) ;
1386 }
1387 Rect structure ;
1388 m_peer->GetRect( &structure ) ;
1389 #if !TARGET_API_MAC_OSX
1390 OffsetRect( &content , -structure.left , -structure.top ) ;
1391 #endif
1392
1393 sizeTotal.x += (structure.right - structure.left) - (content.right - content.left) ;
1394 sizeTotal.y += (structure.bottom - structure.top) - (content.bottom - content.top ) ;
1395
1396 sizeTotal.x += MacGetLeftBorderSize( ) + MacGetRightBorderSize( ) ;
1397 sizeTotal.y += MacGetTopBorderSize( ) + MacGetBottomBorderSize( ) ;
1398
1399 return sizeTotal;
1400 }
1401
1402
1403 // Get size *available for subwindows* i.e. excluding menu bar etc.
1404 void wxWindowMac::DoGetClientSize(int *x, int *y) const
1405 {
1406 int ww, hh;
1407
1408 RgnHandle rgn = NewRgn() ;
1409 Rect content ;
1410 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
1411 {
1412 GetRegionBounds( rgn , &content ) ;
1413 DisposeRgn( rgn ) ;
1414 }
1415 else
1416 {
1417 m_peer->GetRect( &content ) ;
1418 }
1419 #if !TARGET_API_MAC_OSX
1420 Rect structure ;
1421 m_peer->GetRect( &structure ) ;
1422 OffsetRect( &content , -structure.left , -structure.top ) ;
1423 #endif
1424 ww = content.right - content.left ;
1425 hh = content.bottom - content.top ;
1426 /*
1427 ww -= MacGetLeftBorderSize( ) + MacGetRightBorderSize( ) ;
1428 hh -= MacGetTopBorderSize( ) + MacGetBottomBorderSize( );
1429 */
1430 /*
1431 if ( (m_vScrollBar && m_vScrollBar->IsShown()) || (m_hScrollBar && m_hScrollBar->IsShown()) )
1432 {
1433 int x1 = 0 ;
1434 int y1 = 0 ;
1435 int w ;
1436 int h ;
1437 GetSize( &w , &h ) ;
1438
1439 MacClientToRootWindow( &x1 , &y1 ) ;
1440 MacClientToRootWindow( &w , &h ) ;
1441
1442 wxWindowMac *iter = (wxWindowMac*)this ;
1443
1444 int totW = 10000 , totH = 10000;
1445 while( iter )
1446 {
1447 if ( iter->IsTopLevel() )
1448 {
1449 iter->GetSize( &totW , &totH ) ;
1450 break ;
1451 }
1452
1453 iter = iter->GetParent() ;
1454 }
1455
1456 if (m_hScrollBar && m_hScrollBar->IsShown() )
1457 {
1458 hh -= m_hScrollBar->GetSize().y ; // MAC_SCROLLBAR_SIZE ;
1459 if ( h-y1 >= totH )
1460 {
1461 hh += 1 ;
1462 }
1463 }
1464 if (m_vScrollBar && m_vScrollBar->IsShown() )
1465 {
1466 ww -= m_vScrollBar->GetSize().x ; // MAC_SCROLLBAR_SIZE;
1467 if ( w-x1 >= totW )
1468 {
1469 ww += 1 ;
1470 }
1471 }
1472 }
1473 */
1474 if (m_hScrollBar && m_hScrollBar->IsShown() )
1475 {
1476 hh -= m_hScrollBar->GetSize().y ; // MAC_SCROLLBAR_SIZE ;
1477 }
1478 if (m_vScrollBar && m_vScrollBar->IsShown() )
1479 {
1480 ww -= m_vScrollBar->GetSize().x ; // MAC_SCROLLBAR_SIZE;
1481 }
1482 if(x) *x = ww;
1483 if(y) *y = hh;
1484
1485 }
1486
1487 bool wxWindowMac::SetCursor(const wxCursor& cursor)
1488 {
1489 if (m_cursor == cursor)
1490 return FALSE;
1491
1492 if (wxNullCursor == cursor)
1493 {
1494 if ( ! wxWindowBase::SetCursor( *wxSTANDARD_CURSOR ) )
1495 return FALSE ;
1496 }
1497 else
1498 {
1499 if ( ! wxWindowBase::SetCursor( cursor ) )
1500 return FALSE ;
1501 }
1502
1503 wxASSERT_MSG( m_cursor.Ok(),
1504 wxT("cursor must be valid after call to the base version"));
1505
1506
1507 wxWindowMac *mouseWin = 0 ;
1508 {
1509 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
1510 CGrafPtr savePort ;
1511 Boolean swapped = QDSwapPort( GetWindowPort( window ) , &savePort ) ;
1512
1513 // TODO If we ever get a GetCurrentEvent.. replacement for the mouse
1514 // position, use it...
1515
1516 Point pt ;
1517 GetMouse( &pt ) ;
1518 ControlPartCode part ;
1519 ControlRef control ;
1520 control = wxMacFindControlUnderMouse( pt , window , &part ) ;
1521 if ( control )
1522 mouseWin = wxFindControlFromMacControl( control ) ;
1523
1524 if ( swapped )
1525 QDSwapPort( savePort , NULL ) ;
1526 }
1527
1528 if ( mouseWin == this && !wxIsBusy() )
1529 {
1530 m_cursor.MacInstall() ;
1531 }
1532
1533 return TRUE ;
1534 }
1535
1536 #if wxUSE_MENUS
1537 bool wxWindowMac::DoPopupMenu(wxMenu *menu, int x, int y)
1538 {
1539 menu->SetInvokingWindow(this);
1540 menu->UpdateUI();
1541
1542 if ( x == -1 && y == -1 )
1543 {
1544 wxPoint mouse = wxGetMousePosition();
1545 x = mouse.x; y = mouse.y;
1546 }
1547 else
1548 {
1549 ClientToScreen( &x , &y ) ;
1550 }
1551
1552 menu->MacBeforeDisplay( true ) ;
1553 long menuResult = ::PopUpMenuSelect((MenuHandle) menu->GetHMenu() ,y,x, 0) ;
1554 if ( HiWord(menuResult) != 0 )
1555 {
1556 MenuCommand id ;
1557 GetMenuItemCommandID( GetMenuHandle(HiWord(menuResult)) , LoWord(menuResult) , &id ) ;
1558 wxMenuItem* item = NULL ;
1559 wxMenu* realmenu ;
1560 item = menu->FindItem(id, &realmenu) ;
1561 if (item->IsCheckable())
1562 {
1563 item->Check( !item->IsChecked() ) ;
1564 }
1565 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
1566 }
1567 menu->MacAfterDisplay( true ) ;
1568
1569 menu->SetInvokingWindow(NULL);
1570
1571 return TRUE;
1572 }
1573 #endif
1574
1575 // ----------------------------------------------------------------------------
1576 // tooltips
1577 // ----------------------------------------------------------------------------
1578
1579 #if wxUSE_TOOLTIPS
1580
1581 void wxWindowMac::DoSetToolTip(wxToolTip *tooltip)
1582 {
1583 wxWindowBase::DoSetToolTip(tooltip);
1584
1585 if ( m_tooltip )
1586 m_tooltip->SetWindow(this);
1587 }
1588
1589 #endif // wxUSE_TOOLTIPS
1590
1591 void wxWindowMac::DoMoveWindow(int x, int y, int width, int height)
1592 {
1593 // this is never called for a toplevel window, so we know we have a parent
1594 int former_x , former_y , former_w, former_h ;
1595
1596 // Get true coordinates of former position
1597 DoGetPosition( &former_x , &former_y ) ;
1598 DoGetSize( &former_w , &former_h ) ;
1599
1600 wxWindow *parent = GetParent();
1601 if ( parent )
1602 {
1603 wxPoint pt(parent->GetClientAreaOrigin());
1604 former_x += pt.x ;
1605 former_y += pt.y ;
1606 }
1607
1608 int actualWidth = width;
1609 int actualHeight = height;
1610 int actualX = x;
1611 int actualY = y;
1612
1613 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
1614 actualWidth = m_minWidth;
1615 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
1616 actualHeight = m_minHeight;
1617 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
1618 actualWidth = m_maxWidth;
1619 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
1620 actualHeight = m_maxHeight;
1621
1622 bool doMove = false ;
1623 bool doResize = false ;
1624
1625 if ( actualX != former_x || actualY != former_y )
1626 {
1627 doMove = true ;
1628 }
1629 if ( actualWidth != former_w || actualHeight != former_h )
1630 {
1631 doResize = true ;
1632 }
1633
1634 if ( doMove || doResize )
1635 {
1636 // we don't adjust twice for the origin
1637 Rect r = wxMacGetBoundsForControl(this , wxPoint( actualX,actualY), wxSize( actualWidth, actualHeight ) , false ) ;
1638 bool vis = m_peer->IsVisible();
1639
1640 int outerBorder = MacGetLeftBorderSize() ;
1641 if ( m_peer->NeedsFocusRect() && m_peer->HasFocus() )
1642 outerBorder += 4 ;
1643
1644 if ( vis && ( outerBorder > 0 ) )
1645 {
1646 // as the borders are drawn on the parent we have to properly invalidate all these areas
1647 RgnHandle updateInner = NewRgn() , updateOuter = NewRgn() , updateTotal = NewRgn() ;
1648
1649 Rect rect ;
1650
1651 m_peer->GetRect( &rect ) ;
1652 RectRgn( updateInner , &rect ) ;
1653 InsetRect( &rect , -outerBorder , -outerBorder ) ;
1654 RectRgn( updateOuter , &rect ) ;
1655 DiffRgn( updateOuter , updateInner ,updateOuter ) ;
1656 wxPoint parent(0,0);
1657 GetParent()->MacWindowToRootWindow( &parent.x , &parent.y ) ;
1658 parent -= GetParent()->GetClientAreaOrigin() ;
1659 OffsetRgn( updateOuter , -parent.x , -parent.y ) ;
1660 CopyRgn( updateOuter , updateTotal ) ;
1661
1662 rect = r ;
1663 RectRgn( updateInner , &rect ) ;
1664 InsetRect( &rect , -outerBorder , -outerBorder ) ;
1665 RectRgn( updateOuter , &rect ) ;
1666 DiffRgn( updateOuter , updateInner ,updateOuter ) ;
1667
1668 OffsetRgn( updateOuter , -parent.x , -parent.y ) ;
1669 UnionRgn( updateOuter , updateTotal , updateTotal ) ;
1670
1671 GetParent()->m_peer->SetNeedsDisplay( true , updateTotal ) ;
1672 DisposeRgn(updateOuter) ;
1673 DisposeRgn(updateInner) ;
1674 DisposeRgn(updateTotal) ;
1675 }
1676
1677 // the HIViewSetFrame call itself should invalidate the areas, but when testing with the UnicodeTextCtrl it does not !
1678 if ( vis )
1679 m_peer->SetVisibility( false , true ) ;
1680
1681 m_peer->SetRect( &r ) ;
1682 if ( vis )
1683 m_peer->SetVisibility( true , true ) ;
1684
1685 MacRepositionScrollBars() ;
1686 if ( doMove )
1687 {
1688 wxPoint point(actualX,actualY);
1689 wxMoveEvent event(point, m_windowId);
1690 event.SetEventObject(this);
1691 GetEventHandler()->ProcessEvent(event) ;
1692 }
1693 if ( doResize )
1694 {
1695 MacRepositionScrollBars() ;
1696 wxSize size(actualWidth, actualHeight);
1697 wxSizeEvent event(size, m_windowId);
1698 event.SetEventObject(this);
1699 GetEventHandler()->ProcessEvent(event);
1700 }
1701 }
1702
1703 }
1704
1705 wxSize wxWindowMac::DoGetBestSize() const
1706 {
1707 if ( m_macIsUserPane || IsTopLevel() )
1708 return wxWindowBase::DoGetBestSize() ;
1709
1710 Rect bestsize = { 0 , 0 , 0 , 0 } ;
1711 int bestWidth, bestHeight ;
1712 m_peer->GetBestRect( &bestsize ) ;
1713
1714 if ( EmptyRect( &bestsize ) )
1715 {
1716 bestsize.left = bestsize.top = 0 ;
1717 bestsize.right = 16 ;
1718 bestsize.bottom = 16 ;
1719 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
1720 {
1721 bestsize.bottom = 16 ;
1722 }
1723 else if ( IsKindOf( CLASSINFO( wxSpinButton ) ) )
1724 {
1725 bestsize.bottom = 24 ;
1726 }
1727 else
1728 {
1729 // return wxWindowBase::DoGetBestSize() ;
1730 }
1731 }
1732
1733 bestWidth = bestsize.right - bestsize.left ;
1734 bestHeight = bestsize.bottom - bestsize.top ;
1735 if ( bestHeight < 10 )
1736 bestHeight = 13 ;
1737
1738 return wxSize(bestWidth, bestHeight);
1739 }
1740
1741
1742 // set the size of the window: if the dimensions are positive, just use them,
1743 // but if any of them is equal to -1, it means that we must find the value for
1744 // it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
1745 // which case -1 is a valid value for x and y)
1746 //
1747 // If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
1748 // the width/height to best suit our contents, otherwise we reuse the current
1749 // width/height
1750 void wxWindowMac::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1751 {
1752 // get the current size and position...
1753 int currentX, currentY;
1754 GetPosition(&currentX, &currentY);
1755
1756 int currentW,currentH;
1757 GetSize(&currentW, &currentH);
1758
1759 // ... and don't do anything (avoiding flicker) if it's already ok
1760 if ( x == currentX && y == currentY &&
1761 width == currentW && height == currentH && ( height != -1 && width != -1 ) )
1762 {
1763 // TODO REMOVE
1764 MacRepositionScrollBars() ; // we might have a real position shift
1765 return;
1766 }
1767
1768 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1769 x = currentX;
1770 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1771 y = currentY;
1772
1773 AdjustForParentClientOrigin(x, y, sizeFlags);
1774
1775 wxSize size(-1, -1);
1776 if ( width == -1 )
1777 {
1778 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
1779 {
1780 size = DoGetBestSize();
1781 width = size.x;
1782 }
1783 else
1784 {
1785 // just take the current one
1786 width = currentW;
1787 }
1788 }
1789
1790 if ( height == -1 )
1791 {
1792 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
1793 {
1794 if ( size.x == -1 )
1795 {
1796 size = DoGetBestSize();
1797 }
1798 //else: already called DoGetBestSize() above
1799
1800 height = size.y;
1801 }
1802 else
1803 {
1804 // just take the current one
1805 height = currentH;
1806 }
1807 }
1808
1809 DoMoveWindow(x, y, width, height);
1810
1811 }
1812
1813 wxPoint wxWindowMac::GetClientAreaOrigin() const
1814 {
1815 RgnHandle rgn = NewRgn() ;
1816 Rect content ;
1817 m_peer->GetRegion( kControlContentMetaPart , rgn ) ;
1818 GetRegionBounds( rgn , &content ) ;
1819 DisposeRgn( rgn ) ;
1820 #if !TARGET_API_MAC_OSX
1821 // if the content rgn is empty / not supported
1822 // don't attempt to correct the coordinates to wxWindow relative ones
1823 if (!::EmptyRect( &content ) )
1824 {
1825 Rect structure ;
1826 m_peer->GetRect( &structure ) ;
1827 OffsetRect( &content , -structure.left , -structure.top ) ;
1828 }
1829 #endif
1830
1831 return wxPoint( content.left + MacGetLeftBorderSize( ) , content.top + MacGetTopBorderSize( ) );
1832 }
1833
1834 void wxWindowMac::DoSetClientSize(int clientwidth, int clientheight)
1835 {
1836 if ( clientheight != -1 || clientheight != -1 )
1837 {
1838 int currentclientwidth , currentclientheight ;
1839 int currentwidth , currentheight ;
1840
1841 GetClientSize( &currentclientwidth , &currentclientheight ) ;
1842 GetSize( &currentwidth , &currentheight ) ;
1843
1844 DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth ,
1845 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
1846 }
1847 }
1848
1849 void wxWindowMac::SetTitle(const wxString& title)
1850 {
1851 m_label = wxStripMenuCodes(title) ;
1852
1853 if ( m_peer && m_peer->Ok() )
1854 {
1855 m_peer->SetTitle( m_label ) ;
1856 }
1857 Refresh() ;
1858 }
1859
1860 wxString wxWindowMac::GetTitle() const
1861 {
1862 return m_label ;
1863 }
1864
1865 bool wxWindowMac::Show(bool show)
1866 {
1867 if ( !wxWindowBase::Show(show) )
1868 return FALSE;
1869
1870 // TODO use visibilityChanged Carbon Event for OSX
1871 if ( m_peer )
1872 {
1873 bool former = MacIsReallyShown() ;
1874
1875 m_peer->SetVisibility( show , true ) ;
1876 if ( former != MacIsReallyShown() )
1877 MacPropagateVisibilityChanged() ;
1878 }
1879 return TRUE;
1880 }
1881
1882 bool wxWindowMac::Enable(bool enable)
1883 {
1884 wxASSERT( m_peer->Ok() ) ;
1885 if ( !wxWindowBase::Enable(enable) )
1886 return FALSE;
1887
1888 bool former = MacIsReallyEnabled() ;
1889 m_peer->Enable( enable ) ;
1890
1891 if ( former != MacIsReallyEnabled() )
1892 MacPropagateEnabledStateChanged() ;
1893 return TRUE;
1894 }
1895
1896 //
1897 // status change propagations (will be not necessary for OSX later )
1898 //
1899
1900 void wxWindowMac::MacPropagateVisibilityChanged()
1901 {
1902 #if !TARGET_API_MAC_OSX
1903 MacVisibilityChanged() ;
1904
1905 wxWindowListNode *node = GetChildren().GetFirst();
1906 while ( node )
1907 {
1908 wxWindowMac *child = node->GetData();
1909 if ( child->IsShown() )
1910 child->MacPropagateVisibilityChanged( ) ;
1911 node = node->GetNext();
1912 }
1913 #endif
1914 }
1915
1916 void wxWindowMac::MacPropagateEnabledStateChanged( )
1917 {
1918 #if !TARGET_API_MAC_OSX
1919 MacEnabledStateChanged() ;
1920
1921 wxWindowListNode *node = GetChildren().GetFirst();
1922 while ( node )
1923 {
1924 wxWindowMac *child = node->GetData();
1925 if ( child->IsEnabled() )
1926 child->MacPropagateEnabledStateChanged() ;
1927 node = node->GetNext();
1928 }
1929 #endif
1930 }
1931
1932 void wxWindowMac::MacPropagateHiliteChanged( )
1933 {
1934 #if !TARGET_API_MAC_OSX
1935 MacHiliteChanged() ;
1936
1937 wxWindowListNode *node = GetChildren().GetFirst();
1938 while ( node )
1939 {
1940 wxWindowMac *child = node->GetData();
1941 // if ( child->IsEnabled() )
1942 child->MacPropagateHiliteChanged() ;
1943 node = node->GetNext();
1944 }
1945 #endif
1946 }
1947
1948 //
1949 // status change notifications
1950 //
1951
1952 void wxWindowMac::MacVisibilityChanged()
1953 {
1954 }
1955
1956 void wxWindowMac::MacHiliteChanged()
1957 {
1958 }
1959
1960 void wxWindowMac::MacEnabledStateChanged()
1961 {
1962 }
1963
1964 //
1965 // status queries on the inherited window's state
1966 //
1967
1968 bool wxWindowMac::MacIsReallyShown()
1969 {
1970 // only under OSX the visibility of the TLW is taken into account
1971 #if TARGET_API_MAC_OSX
1972 if ( m_peer && m_peer->Ok() )
1973 return m_peer->IsVisible();
1974 #endif
1975 wxWindow* win = this ;
1976 while( win->IsShown() )
1977 {
1978 if ( win->IsTopLevel() )
1979 return true ;
1980
1981 win = win->GetParent() ;
1982 if ( win == NULL )
1983 return true ;
1984
1985 } ;
1986 return false ;
1987 }
1988
1989 bool wxWindowMac::MacIsReallyEnabled()
1990 {
1991 return m_peer->IsEnabled() ;
1992 }
1993
1994 bool wxWindowMac::MacIsReallyHilited()
1995 {
1996 return m_peer->IsActive();
1997 }
1998
1999 void wxWindowMac::MacFlashInvalidAreas()
2000 {
2001 #if TARGET_API_MAC_OSX
2002 HIViewFlashDirtyArea( (WindowRef) MacGetTopLevelWindowRef() ) ;
2003 #endif
2004 }
2005
2006 //
2007 //
2008 //
2009
2010 int wxWindowMac::GetCharHeight() const
2011 {
2012 wxClientDC dc ( (wxWindowMac*)this ) ;
2013 return dc.GetCharHeight() ;
2014 }
2015
2016 int wxWindowMac::GetCharWidth() const
2017 {
2018 wxClientDC dc ( (wxWindowMac*)this ) ;
2019 return dc.GetCharWidth() ;
2020 }
2021
2022 void wxWindowMac::GetTextExtent(const wxString& string, int *x, int *y,
2023 int *descent, int *externalLeading, const wxFont *theFont ) const
2024 {
2025 const wxFont *fontToUse = theFont;
2026 if ( !fontToUse )
2027 fontToUse = &m_font;
2028
2029 wxClientDC dc( (wxWindowMac*) this ) ;
2030 long lx,ly,ld,le ;
2031 dc.GetTextExtent( string , &lx , &ly , &ld, &le, (wxFont *)fontToUse ) ;
2032 if ( externalLeading )
2033 *externalLeading = le ;
2034 if ( descent )
2035 *descent = ld ;
2036 if ( x )
2037 *x = lx ;
2038 if ( y )
2039 *y = ly ;
2040 }
2041
2042 /*
2043 * Rect is given in client coordinates, for further reading, read wxTopLevelWindowMac::InvalidateRect
2044 * we always intersect with the entire window, not only with the client area
2045 */
2046
2047 void wxWindowMac::Refresh(bool eraseBack, const wxRect *rect)
2048 {
2049 if ( m_peer == NULL )
2050 return ;
2051
2052 #if TARGET_API_MAC_OSX
2053 if ( rect == NULL )
2054 m_peer->SetNeedsDisplay( true ) ;
2055 else
2056 {
2057 RgnHandle update = NewRgn() ;
2058 SetRectRgn( update , rect->x , rect->y , rect->x + rect->width , rect->y + rect->height ) ;
2059 SectRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , update , update ) ;
2060 wxPoint origin = GetClientAreaOrigin() ;
2061 OffsetRgn( update, origin.x , origin.y ) ;
2062 // right now this is wx' window coordinates, as our native peer does not have borders, this is
2063 // inset
2064 OffsetRgn( update , -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2065 m_peer->SetNeedsDisplay( true , update) ;
2066 DisposeRgn( update ) ;
2067 }
2068 #else
2069 /*
2070 RgnHandle updateRgn = NewRgn() ;
2071 if ( rect == NULL )
2072 {
2073 CopyRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , updateRgn ) ;
2074 }
2075 else
2076 {
2077 SetRectRgn( updateRgn , rect->x , rect->y , rect->x + rect->width , rect->y + rect->height ) ;
2078 SectRgn( (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , updateRgn , updateRgn ) ;
2079 }
2080 InvalWindowRgn( (WindowRef) MacGetTopLevelWindowRef() , updateRgn ) ;
2081 DisposeRgn(updateRgn) ;
2082 */
2083 if ( m_peer->IsVisible())
2084 {
2085 m_peer->SetVisibility( false , false ) ;
2086 m_peer->SetVisibility( true , true ) ;
2087 }
2088 /*
2089 if ( MacGetTopLevelWindow() == NULL )
2090 return ;
2091
2092 if ( !m_peer->IsVisible())
2093 return ;
2094
2095 wxPoint client = GetClientAreaOrigin();
2096 int x1 = -client.x;
2097 int y1 = -client.y;
2098 int x2 = m_width - client.x;
2099 int y2 = m_height - client.y;
2100
2101 if (IsKindOf( CLASSINFO(wxButton)))
2102 {
2103 // buttons have an "aura"
2104 y1 -= 5;
2105 x1 -= 5;
2106 y2 += 5;
2107 x2 += 5;
2108 }
2109
2110 Rect clientrect = { y1, x1, y2, x2 };
2111
2112 if ( rect )
2113 {
2114 Rect r = { rect->y , rect->x , rect->y + rect->height , rect->x + rect->width } ;
2115 SectRect( &clientrect , &r , &clientrect ) ;
2116 }
2117
2118 if ( !EmptyRect( &clientrect ) )
2119 {
2120 int top = 0 , left = 0 ;
2121
2122 MacClientToRootWindow( &left , &top ) ;
2123 OffsetRect( &clientrect , left , top ) ;
2124
2125 MacGetTopLevelWindow()->MacInvalidate( &clientrect , eraseBack ) ;
2126 }
2127 */
2128 #endif
2129 }
2130
2131 void wxWindowMac::Freeze()
2132 {
2133 #if TARGET_API_MAC_OSX
2134 if ( !m_frozenness++ )
2135 {
2136 if ( m_peer && m_peer->Ok() )
2137 m_peer->SetDrawingEnabled( false ) ;
2138 }
2139 #endif
2140 }
2141
2142
2143 void wxWindowMac::Thaw()
2144 {
2145 #if TARGET_API_MAC_OSX
2146 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
2147
2148 if ( !--m_frozenness )
2149 {
2150 if ( m_peer && m_peer->Ok() )
2151 {
2152 m_peer->SetDrawingEnabled( true ) ;
2153 m_peer->InvalidateWithChildren() ;
2154 }
2155 }
2156 #endif
2157 }
2158
2159 void wxWindowMac::MacRedrawControl()
2160 {
2161 /*
2162 if ( *m_peer && MacGetTopLevelWindowRef() && m_peer->IsVisible())
2163 {
2164 #if TARGET_API_MAC_CARBON
2165 Update() ;
2166 #else
2167 wxClientDC dc(this) ;
2168 wxMacPortSetter helper(&dc) ;
2169 wxMacWindowClipper clipper(this) ;
2170 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
2171 UMADrawControl( *m_peer ) ;
2172 #endif
2173 }
2174 */
2175 }
2176
2177 /* TODO
2178 void wxWindowMac::OnPaint(wxPaintEvent& event)
2179 {
2180 // why don't we skip that here ?
2181 }
2182 */
2183
2184 wxWindowMac *wxGetActiveWindow()
2185 {
2186 // actually this is a windows-only concept
2187 return NULL;
2188 }
2189
2190 // Coordinates relative to the window
2191 void wxWindowMac::WarpPointer (int x_pos, int y_pos)
2192 {
2193 // We really don't move the mouse programmatically under Mac.
2194 }
2195
2196 void wxWindowMac::OnEraseBackground(wxEraseEvent& event)
2197 {
2198 #if TARGET_API_MAC_OSX
2199 if ( m_macBackgroundBrush.Ok() == false || m_macBackgroundBrush.GetStyle() == wxTRANSPARENT )
2200 {
2201 event.Skip() ;
2202 }
2203 else
2204 #endif
2205 {
2206 event.GetDC()->Clear() ;
2207 }
2208 }
2209
2210 void wxWindowMac::OnNcPaint( wxNcPaintEvent& event )
2211 {
2212 event.Skip() ;
2213 }
2214
2215 int wxWindowMac::GetScrollPos(int orient) const
2216 {
2217 if ( orient == wxHORIZONTAL )
2218 {
2219 if ( m_hScrollBar )
2220 return m_hScrollBar->GetThumbPosition() ;
2221 }
2222 else
2223 {
2224 if ( m_vScrollBar )
2225 return m_vScrollBar->GetThumbPosition() ;
2226 }
2227 return 0;
2228 }
2229
2230 // This now returns the whole range, not just the number
2231 // of positions that we can scroll.
2232 int wxWindowMac::GetScrollRange(int orient) const
2233 {
2234 if ( orient == wxHORIZONTAL )
2235 {
2236 if ( m_hScrollBar )
2237 return m_hScrollBar->GetRange() ;
2238 }
2239 else
2240 {
2241 if ( m_vScrollBar )
2242 return m_vScrollBar->GetRange() ;
2243 }
2244 return 0;
2245 }
2246
2247 int wxWindowMac::GetScrollThumb(int orient) const
2248 {
2249 if ( orient == wxHORIZONTAL )
2250 {
2251 if ( m_hScrollBar )
2252 return m_hScrollBar->GetThumbSize() ;
2253 }
2254 else
2255 {
2256 if ( m_vScrollBar )
2257 return m_vScrollBar->GetThumbSize() ;
2258 }
2259 return 0;
2260 }
2261
2262 void wxWindowMac::SetScrollPos(int orient, int pos, bool refresh)
2263 {
2264 if ( orient == wxHORIZONTAL )
2265 {
2266 if ( m_hScrollBar )
2267 m_hScrollBar->SetThumbPosition( pos ) ;
2268 }
2269 else
2270 {
2271 if ( m_vScrollBar )
2272 m_vScrollBar->SetThumbPosition( pos ) ;
2273 }
2274 }
2275
2276 void wxWindowMac::MacPaintBorders( int left , int top )
2277 {
2278 if( IsTopLevel() )
2279 return ;
2280
2281 Rect rect ;
2282 m_peer->GetRect( &rect ) ;
2283 InsetRect( &rect, -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2284
2285 if ( !IsTopLevel() )
2286 {
2287 wxTopLevelWindowMac* top = MacGetTopLevelWindow();
2288 if (top)
2289 {
2290 wxPoint pt(0,0) ;
2291 wxMacControl::Convert( &pt , GetParent()->m_peer , top->m_peer ) ;
2292 rect.left += pt.x ;
2293 rect.right += pt.x ;
2294 rect.top += pt.y ;
2295 rect.bottom += pt.y ;
2296 }
2297 }
2298
2299 if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
2300 {
2301 Rect srect = rect ;
2302 SInt32 border = 0 ;
2303 GetThemeMetric( kThemeMetricEditTextFrameOutset , &border ) ;
2304 InsetRect( &srect , border , border );
2305 DrawThemeEditTextFrame(&srect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
2306 }
2307 else if (HasFlag(wxSIMPLE_BORDER))
2308 {
2309 Rect srect = rect ;
2310 SInt32 border = 0 ;
2311 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2312 InsetRect( &srect , border , border );
2313 DrawThemeListBoxFrame(&rect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
2314 }
2315 }
2316
2317 void wxWindowMac::RemoveChild( wxWindowBase *child )
2318 {
2319 if ( child == m_hScrollBar )
2320 m_hScrollBar = NULL ;
2321 if ( child == m_vScrollBar )
2322 m_vScrollBar = NULL ;
2323
2324 wxWindowBase::RemoveChild( child ) ;
2325 }
2326
2327 // New function that will replace some of the above.
2328 void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible,
2329 int range, bool refresh)
2330 {
2331 if ( orient == wxHORIZONTAL )
2332 {
2333 if ( m_hScrollBar )
2334 {
2335 if ( range == 0 || thumbVisible >= range )
2336 {
2337 if ( m_hScrollBar->IsShown() )
2338 m_hScrollBar->Show(false) ;
2339 }
2340 else
2341 {
2342 if ( !m_hScrollBar->IsShown() )
2343 m_hScrollBar->Show(true) ;
2344 m_hScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2345 }
2346 }
2347 }
2348 else
2349 {
2350 if ( m_vScrollBar )
2351 {
2352 if ( range == 0 || thumbVisible >= range )
2353 {
2354 if ( m_vScrollBar->IsShown() )
2355 m_vScrollBar->Show(false) ;
2356 }
2357 else
2358 {
2359 if ( !m_vScrollBar->IsShown() )
2360 m_vScrollBar->Show(true) ;
2361 m_vScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2362 }
2363 }
2364 }
2365 MacRepositionScrollBars() ;
2366 }
2367
2368 // Does a physical scroll
2369 void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect)
2370 {
2371 if( dx == 0 && dy ==0 )
2372 return ;
2373
2374
2375 {
2376
2377 int width , height ;
2378 GetClientSize( &width , &height ) ;
2379 #if TARGET_API_MAC_OSX
2380 // note there currently is a bug in OSX which makes inefficient refreshes in case an entire control
2381 // area is scrolled, this does not occur if width and height are 2 pixels less,
2382 // TODO write optimal workaround
2383 wxRect scrollrect( MacGetLeftBorderSize() , MacGetTopBorderSize() , width , height ) ;
2384 if ( rect )
2385 {
2386 scrollrect.Intersect( *rect ) ;
2387 }
2388 if ( m_peer->GetNeedsDisplay() )
2389 {
2390 // becuase HIViewScrollRect does not scroll the already invalidated area we have two options
2391 // either immediate redraw or full invalidate
2392 #if 1
2393 // is the better overall solution, as it does not slow down scrolling
2394 m_peer->SetNeedsDisplay( true ) ;
2395 #else
2396 // this would be the preferred version for fast drawing controls
2397 if( UMAGetSystemVersion() < 0x1030 )
2398 Update() ;
2399 else
2400 HIViewRender(m_peer->GetControlRef()) ;
2401 #endif
2402 }
2403 // as the native control might be not a 0/0 wx window coordinates, we have to offset
2404 scrollrect.Offset( -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2405 m_peer->ScrollRect( scrollrect , dx , dy ) ;
2406 #else
2407
2408 wxPoint pos;
2409 pos.x = pos.y = 0;
2410
2411 Rect scrollrect;
2412 RgnHandle updateRgn = NewRgn() ;
2413
2414 {
2415 wxClientDC dc(this) ;
2416 wxMacPortSetter helper(&dc) ;
2417
2418 m_peer->GetRect( &scrollrect ) ;
2419 scrollrect.top += MacGetTopBorderSize() ;
2420 scrollrect.left += MacGetLeftBorderSize() ;
2421 scrollrect.bottom = scrollrect.top + height ;
2422 scrollrect.right = scrollrect.left + width ;
2423
2424 if ( rect )
2425 {
2426 Rect r = { dc.YLOG2DEVMAC(rect->y) , dc.XLOG2DEVMAC(rect->x) , dc.YLOG2DEVMAC(rect->y + rect->height) ,
2427 dc.XLOG2DEVMAC(rect->x + rect->width) } ;
2428 SectRect( &scrollrect , &r , &scrollrect ) ;
2429 }
2430 ScrollRect( &scrollrect , dx , dy , updateRgn ) ;
2431 }
2432 // ScrollWindowRect( (WindowRef) MacGetTopLevelWindowRef() , &scrollrect , dx , dy , kScrollWindowInvalidate, updateRgn ) ;
2433 #endif
2434 }
2435
2436 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
2437 {
2438 wxWindowMac *child = node->GetData();
2439 if (child == m_vScrollBar) continue;
2440 if (child == m_hScrollBar) continue;
2441 if (child->IsTopLevel()) continue;
2442
2443 int x,y;
2444 child->GetPosition( &x, &y );
2445 int w,h;
2446 child->GetSize( &w, &h );
2447 if (rect)
2448 {
2449 wxRect rc(x,y,w,h);
2450 if (rect->Intersects(rc))
2451 child->SetSize( x+dx, y+dy, w, h );
2452 }
2453 else
2454 {
2455 child->SetSize( x+dx, y+dy, w, h );
2456 }
2457 }
2458 }
2459
2460 void wxWindowMac::MacOnScroll(wxScrollEvent &event )
2461 {
2462 if ( event.m_eventObject == m_vScrollBar || event.m_eventObject == m_hScrollBar )
2463 {
2464 wxScrollWinEvent wevent;
2465 wevent.SetPosition(event.GetPosition());
2466 wevent.SetOrientation(event.GetOrientation());
2467 wevent.m_eventObject = this;
2468
2469 if (event.m_eventType == wxEVT_SCROLL_TOP)
2470 wevent.m_eventType = wxEVT_SCROLLWIN_TOP;
2471 else if (event.m_eventType == wxEVT_SCROLL_BOTTOM)
2472 wevent.m_eventType = wxEVT_SCROLLWIN_BOTTOM;
2473 else if (event.m_eventType == wxEVT_SCROLL_LINEUP)
2474 wevent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
2475 else if (event.m_eventType == wxEVT_SCROLL_LINEDOWN)
2476 wevent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
2477 else if (event.m_eventType == wxEVT_SCROLL_PAGEUP)
2478 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEUP;
2479 else if (event.m_eventType == wxEVT_SCROLL_PAGEDOWN)
2480 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEDOWN;
2481 else if (event.m_eventType == wxEVT_SCROLL_THUMBTRACK)
2482 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBTRACK;
2483 else if (event.m_eventType == wxEVT_SCROLL_THUMBRELEASE)
2484 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBRELEASE;
2485
2486 GetEventHandler()->ProcessEvent(wevent);
2487 }
2488 }
2489
2490 // Get the window with the focus
2491 wxWindowMac *wxWindowBase::DoFindFocus()
2492 {
2493 ControlRef control ;
2494 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
2495 return wxFindControlFromMacControl( control ) ;
2496 }
2497
2498 void wxWindowMac::OnSetFocus(wxFocusEvent& event)
2499 {
2500 // panel wants to track the window which was the last to have focus in it,
2501 // so we want to set ourselves as the window which last had focus
2502 //
2503 // notice that it's also important to do it upwards the tree becaus
2504 // otherwise when the top level panel gets focus, it won't set it back to
2505 // us, but to some other sibling
2506
2507 // CS:don't know if this is still needed:
2508 //wxChildFocusEvent eventFocus(this);
2509 //(void)GetEventHandler()->ProcessEvent(eventFocus);
2510
2511 if ( MacGetTopLevelWindow() && m_peer->NeedsFocusRect() )
2512 {
2513 wxMacWindowStateSaver sv( this ) ;
2514
2515 int w , h ;
2516 int x , y ;
2517 x = y = 0 ;
2518 MacWindowToRootWindow( &x , &y ) ;
2519 GetSize( &w , &h ) ;
2520 Rect rect = {y , x , h + y , w + x } ;
2521
2522 if ( event.GetEventType() == wxEVT_SET_FOCUS )
2523 DrawThemeFocusRect( &rect , true ) ;
2524 else
2525 {
2526 DrawThemeFocusRect( &rect , false ) ;
2527
2528 // as this erases part of the frame we have to redraw borders
2529 // and because our z-ordering is not always correct (staticboxes)
2530 // we have to invalidate things, we cannot simple redraw
2531 RgnHandle updateInner = NewRgn() , updateOuter = NewRgn() ;
2532 RectRgn( updateInner , &rect ) ;
2533 InsetRect( &rect , -4 , -4 ) ;
2534 RectRgn( updateOuter , &rect ) ;
2535 DiffRgn( updateOuter , updateInner ,updateOuter ) ;
2536 wxPoint parent(0,0);
2537 GetParent()->MacWindowToRootWindow( &parent.x , &parent.y ) ;
2538 parent -= GetParent()->GetClientAreaOrigin() ;
2539 OffsetRgn( updateOuter , -parent.x , -parent.y ) ;
2540 GetParent()->m_peer->SetNeedsDisplay( true , updateOuter ) ;
2541 DisposeRgn(updateOuter) ;
2542 DisposeRgn(updateInner) ;
2543 }
2544 }
2545
2546 event.Skip();
2547 }
2548
2549 void wxWindowMac::OnInternalIdle()
2550 {
2551 // This calls the UI-update mechanism (querying windows for
2552 // menu/toolbar/control state information)
2553 if (wxUpdateUIEvent::CanUpdate(this))
2554 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2555 }
2556
2557 // Raise the window to the top of the Z order
2558 void wxWindowMac::Raise()
2559 {
2560 m_peer->SetZOrder( true , NULL ) ;
2561 }
2562
2563 // Lower the window to the bottom of the Z order
2564 void wxWindowMac::Lower()
2565 {
2566 m_peer->SetZOrder( false , NULL ) ;
2567 }
2568
2569
2570 // static wxWindow *gs_lastWhich = NULL;
2571
2572 bool wxWindowMac::MacSetupCursor( const wxPoint& pt)
2573 {
2574 // first trigger a set cursor event
2575
2576 wxPoint clientorigin = GetClientAreaOrigin() ;
2577 wxSize clientsize = GetClientSize() ;
2578 wxCursor cursor ;
2579 if ( wxRect2DInt( clientorigin.x , clientorigin.y , clientsize.x , clientsize.y ).Contains( wxPoint2DInt( pt ) ) )
2580 {
2581 wxSetCursorEvent event( pt.x , pt.y );
2582
2583 bool processedEvtSetCursor = GetEventHandler()->ProcessEvent(event);
2584 if ( processedEvtSetCursor && event.HasCursor() )
2585 {
2586 cursor = event.GetCursor() ;
2587 }
2588 else
2589 {
2590
2591 // the test for processedEvtSetCursor is here to prevent using m_cursor
2592 // if the user code caught EVT_SET_CURSOR() and returned nothing from
2593 // it - this is a way to say that our cursor shouldn't be used for this
2594 // point
2595 if ( !processedEvtSetCursor && m_cursor.Ok() )
2596 {
2597 cursor = m_cursor ;
2598 }
2599 if ( wxIsBusy() )
2600 {
2601 }
2602 else
2603 {
2604 if ( !GetParent() )
2605 cursor = *wxSTANDARD_CURSOR ;
2606 }
2607 }
2608 if ( cursor.Ok() )
2609 cursor.MacInstall() ;
2610 }
2611 return cursor.Ok() ;
2612 }
2613
2614 wxString wxWindowMac::MacGetToolTipString( wxPoint &pt )
2615 {
2616 if ( m_tooltip )
2617 {
2618 return m_tooltip->GetTip() ;
2619 }
2620 return wxEmptyString ;
2621 }
2622
2623 void wxWindowMac::Update()
2624 {
2625 #if TARGET_API_MAC_OSX
2626
2627 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
2628 WindowRef window = (WindowRef)MacGetTopLevelWindowRef() ;
2629
2630 // for composited windows this also triggers a redraw of all
2631 // invalid views in the window
2632 if( UMAGetSystemVersion() >= 0x1030 )
2633 HIWindowFlush(window) ;
2634 else
2635 #endif
2636 {
2637 // the only way to trigger the redrawing on earlier systems is to call
2638 // ReceiveNextEvent
2639
2640 EventRef currentEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
2641 UInt32 currentEventClass = 0 ;
2642 UInt32 currentEventKind = 0 ;
2643 if ( currentEvent != NULL )
2644 {
2645 currentEventClass = ::GetEventClass( currentEvent ) ;
2646 currentEventKind = ::GetEventKind( currentEvent ) ;
2647 }
2648 if ( currentEventClass != kEventClassMenu )
2649 {
2650 // when tracking a menu, strange redraw errors occur if we flush now, so leave..
2651
2652 EventRef theEvent;
2653 OSStatus status = noErr ;
2654 status = ReceiveNextEvent( 0 , NULL , kEventDurationNoWait , false , &theEvent ) ;
2655 }
2656 else
2657 m_peer->SetNeedsDisplay( true ) ;
2658 }
2659 #else
2660 ::Draw1Control( m_peer->GetControlRef() ) ;
2661 #endif
2662 }
2663
2664 wxTopLevelWindowMac* wxWindowMac::MacGetTopLevelWindow() const
2665 {
2666 wxTopLevelWindowMac* win = NULL ;
2667 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
2668 if ( window )
2669 {
2670 win = wxFindWinFromMacWindow( window ) ;
2671 }
2672 return win ;
2673 }
2674 wxRegion wxWindowMac::MacGetVisibleRegion( bool includeOuterStructures )
2675 {
2676 // includeOuterStructures is true if we try to draw somthing like a focus ring etc.
2677 // also a window dc uses this, in this case we only clip in the hierarchy for hard
2678 // borders like a scrollwindow, splitter etc otherwise we end up in a paranoia having
2679 // to add focus borders everywhere
2680
2681 Rect r ;
2682 RgnHandle visRgn = NewRgn() ;
2683 RgnHandle tempRgn = NewRgn() ;
2684 if ( m_peer->IsVisible())
2685 {
2686 m_peer->GetRect( &r ) ;
2687 r.left -= MacGetLeftBorderSize() ;
2688 r.top -= MacGetTopBorderSize() ;
2689 r.bottom += MacGetBottomBorderSize() ;
2690 r.right += MacGetRightBorderSize() ;
2691
2692 if (! MacGetTopLevelWindow()->MacUsesCompositing() )
2693 {
2694 MacRootWindowToWindow( &r.left , & r.top ) ;
2695 MacRootWindowToWindow( &r.right , & r.bottom ) ;
2696 }
2697 else
2698 {
2699 r.right -= r.left ;
2700 r.bottom -= r.top ;
2701 r.left = 0 ;
2702 r.top = 0 ;
2703 }
2704 if ( includeOuterStructures )
2705 InsetRect( &r , -4 , -4 ) ;
2706 RectRgn( visRgn , &r ) ;
2707
2708 if ( !IsTopLevel() )
2709 {
2710 wxWindow* child = this ;
2711 wxWindow* parent = child->GetParent() ;
2712 while( parent )
2713 {
2714 int x , y ;
2715 wxSize size ;
2716 // we have to find a better clipping algorithm here, in order not to clip things
2717 // positioned like status and toolbar
2718 if ( 1 /* parent->IsTopLevel() && child->IsKindOf( CLASSINFO( wxToolBar ) ) */ )
2719 {
2720 size = parent->GetSize() ;
2721 x = y = 0 ;
2722 }
2723 else
2724 {
2725 size = parent->GetClientSize() ;
2726 wxPoint origin = parent->GetClientAreaOrigin() ;
2727 x = origin.x ;
2728 y = origin.y ;
2729 }
2730 parent->MacWindowToRootWindow( &x, &y ) ;
2731 MacRootWindowToWindow( &x , &y ) ;
2732
2733 if ( !includeOuterStructures || (
2734 parent->MacClipChildren() ||
2735 ( parent->GetParent() && parent->GetParent()->MacClipGrandChildren() )
2736 ) )
2737 {
2738 SetRectRgn( tempRgn ,
2739 x + parent->MacGetLeftBorderSize() , y + parent->MacGetTopBorderSize() ,
2740 x + size.x - parent->MacGetRightBorderSize(),
2741 y + size.y - parent->MacGetBottomBorderSize()) ;
2742
2743 SectRgn( visRgn , tempRgn , visRgn ) ;
2744 }
2745 if ( parent->IsTopLevel() )
2746 break ;
2747 child = parent ;
2748 parent = child->GetParent() ;
2749 }
2750 }
2751 }
2752
2753 wxRegion vis = visRgn ;
2754 DisposeRgn( visRgn ) ;
2755 DisposeRgn( tempRgn ) ;
2756 return vis ;
2757 }
2758
2759 /*
2760 This function must not change the updatergn !
2761 */
2762 bool wxWindowMac::MacDoRedraw( WXHRGN updatergnr , long time )
2763 {
2764 RgnHandle updatergn = (RgnHandle) updatergnr ;
2765 bool handled = false ;
2766 Rect updatebounds ;
2767 GetRegionBounds( updatergn , &updatebounds ) ;
2768 // wxLogDebug("update for %s bounds %d , %d , %d , %d",typeid(*this).name() , updatebounds.left , updatebounds.top , updatebounds.right , updatebounds.bottom ) ;
2769 if ( !EmptyRgn(updatergn) )
2770 {
2771 RgnHandle newupdate = NewRgn() ;
2772 wxSize point = GetClientSize() ;
2773 wxPoint origin = GetClientAreaOrigin() ;
2774 SetRectRgn( newupdate , origin.x , origin.y , origin.x + point.x , origin.y+point.y ) ;
2775 SectRgn( newupdate , updatergn , newupdate ) ;
2776
2777 // first send an erase event to the entire update area
2778 {
2779 wxWindowDC dc(this);
2780 dc.SetClippingRegion(wxRegion(updatergn));
2781 wxEraseEvent eevent( GetId(), &dc );
2782 eevent.SetEventObject( this );
2783 GetEventHandler()->ProcessEvent( eevent );
2784 }
2785
2786 // calculate a client-origin version of the update rgn and set m_updateRegion to that
2787 OffsetRgn( newupdate , -origin.x , -origin.y ) ;
2788 m_updateRegion = newupdate ;
2789 DisposeRgn( newupdate ) ;
2790
2791 if ( !m_updateRegion.Empty() )
2792 {
2793 // paint the window itself
2794 wxPaintEvent event;
2795 event.m_timeStamp = time ;
2796 event.SetEventObject(this);
2797 handled = GetEventHandler()->ProcessEvent(event);
2798
2799 // we have to call the default built-in handler, as otherwise our frames will be drawn and immediately erased afterwards
2800 if ( !handled )
2801 {
2802 if ( wxTheApp->MacGetCurrentEvent() != NULL && wxTheApp->MacGetCurrentEventHandlerCallRef() != NULL )
2803 {
2804 CallNextEventHandler((EventHandlerCallRef)wxTheApp->MacGetCurrentEventHandlerCallRef() , (EventRef) wxTheApp->MacGetCurrentEvent() ) ;
2805 handled = true ;
2806 }
2807 }
2808
2809 }
2810
2811 // now we cannot rely on having its borders drawn by a window itself, as it does not
2812 // get the updateRgn wide enough to always do so, so we do it from the parent
2813 // this would also be the place to draw any custom backgrounds for native controls
2814 // in Composited windowing
2815 wxPoint clientOrigin = GetClientAreaOrigin() ;
2816
2817 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
2818 {
2819 wxWindowMac *child = node->GetData();
2820 if (child == m_vScrollBar) continue;
2821 if (child == m_hScrollBar) continue;
2822 if (child->IsTopLevel()) continue;
2823 if (!child->IsShown()) continue;
2824
2825 int x,y;
2826 child->GetPosition( &x, &y );
2827 int w,h;
2828 child->GetSize( &w, &h );
2829 Rect childRect = { y , x , y + h , x + w } ;
2830 OffsetRect( &childRect , clientOrigin.x , clientOrigin.y ) ;
2831 if ( child->MacGetTopBorderSize() )
2832 {
2833 if ( RectInRgn( &childRect , updatergn ) )
2834 {
2835 // paint custom borders
2836 wxNcPaintEvent eventNc( child->GetId() );
2837 eventNc.SetEventObject( child );
2838 if ( !child->GetEventHandler()->ProcessEvent( eventNc ) )
2839 {
2840 wxWindowDC dc(this) ;
2841 dc.SetClippingRegion(wxRegion(updatergn));
2842 wxMacPortSetter helper(&dc) ;
2843 child->MacPaintBorders( dc.m_macLocalOrigin.x + childRect.left , dc.m_macLocalOrigin.y + childRect.top) ;
2844 }
2845 }
2846 }
2847 if ( child->m_peer->NeedsFocusRect() && child->m_peer->HasFocus() )
2848 {
2849 wxWindowDC dc(this) ;
2850 dc.SetClippingRegion(wxRegion(updatergn));
2851 wxMacPortSetter helper(&dc) ;
2852 Rect r = childRect ;
2853 OffsetRect( &r , dc.m_macLocalOrigin.x , dc.m_macLocalOrigin.y ) ;
2854 DrawThemeFocusRect( &r , true ) ;
2855 }
2856 }
2857 }
2858 return handled ;
2859 }
2860
2861 void wxWindowMac::MacRedraw( WXHRGN updatergnr , long time, bool erase)
2862 {
2863 RgnHandle updatergn = (RgnHandle) updatergnr ;
2864 // updatergn is always already clipped to our boundaries
2865 // if we are in compositing mode then it is in relative to the upper left of the control
2866 // if we are in non-compositing, then it is relatvie to the uppder left of the content area
2867 // of the toplevel window
2868 // it is in window coordinates, not in client coordinates
2869
2870 // ownUpdateRgn is the area that this window has to repaint, it is in window coordinates
2871 RgnHandle ownUpdateRgn = NewRgn() ;
2872 CopyRgn( updatergn , ownUpdateRgn ) ;
2873
2874 if ( MacGetTopLevelWindow()->MacUsesCompositing() == false )
2875 {
2876 Rect bounds;
2877 m_peer->GetRectInWindowCoords( &bounds );
2878 RgnHandle controlRgn = NewRgn();
2879 RectRgn( controlRgn, &bounds );
2880 //KO: This sets the ownUpdateRgn to the area of this control that is inside
2881 // the window update region
2882 SectRgn( ownUpdateRgn, controlRgn, ownUpdateRgn );
2883 DisposeRgn( controlRgn );
2884
2885 //KO: convert ownUpdateRgn to local coordinates
2886 OffsetRgn( ownUpdateRgn, -bounds.left, -bounds.top );
2887 }
2888
2889 MacDoRedraw( ownUpdateRgn , time ) ;
2890 DisposeRgn( ownUpdateRgn ) ;
2891
2892 }
2893
2894 WXWindow wxWindowMac::MacGetTopLevelWindowRef() const
2895 {
2896 wxWindowMac *iter = (wxWindowMac*)this ;
2897
2898 while( iter )
2899 {
2900 if ( iter->IsTopLevel() )
2901 return ((wxTopLevelWindow*)iter)->MacGetWindowRef() ;
2902
2903 iter = iter->GetParent() ;
2904 }
2905 wxASSERT_MSG( 1 , wxT("No valid mac root window") ) ;
2906 return NULL ;
2907 }
2908
2909 void wxWindowMac::MacCreateScrollBars( long style )
2910 {
2911 wxASSERT_MSG( m_vScrollBar == NULL && m_hScrollBar == NULL , wxT("attempt to create window twice") ) ;
2912
2913 if ( style & ( wxVSCROLL | wxHSCROLL ) )
2914 {
2915 bool hasBoth = ( style & wxVSCROLL ) && ( style & wxHSCROLL ) ;
2916 int scrlsize = MAC_SCROLLBAR_SIZE ;
2917 wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL ;
2918 if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL || GetWindowVariant() == wxWINDOW_VARIANT_MINI )
2919 {
2920 scrlsize = MAC_SMALL_SCROLLBAR_SIZE ;
2921 variant = wxWINDOW_VARIANT_SMALL ;
2922 }
2923
2924 int adjust = hasBoth ? scrlsize - 1: 0 ;
2925 int width, height ;
2926 GetClientSize( &width , &height ) ;
2927
2928 wxPoint vPoint(width-scrlsize, 0) ;
2929 wxSize vSize(scrlsize, height - adjust) ;
2930 wxPoint hPoint(0 , height-scrlsize ) ;
2931 wxSize hSize( width - adjust, scrlsize) ;
2932
2933
2934 if ( style & wxVSCROLL )
2935 {
2936 m_vScrollBar = new wxScrollBar(this, wxWINDOW_VSCROLL, vPoint,
2937 vSize , wxVERTICAL);
2938 }
2939
2940 if ( style & wxHSCROLL )
2941 {
2942 m_hScrollBar = new wxScrollBar(this, wxWINDOW_HSCROLL, hPoint,
2943 hSize , wxHORIZONTAL);
2944 }
2945 }
2946
2947
2948 // because the create does not take into account the client area origin
2949 MacRepositionScrollBars() ; // we might have a real position shift
2950 }
2951
2952 void wxWindowMac::MacRepositionScrollBars()
2953 {
2954 if ( !m_hScrollBar && !m_vScrollBar )
2955 return ;
2956
2957 bool hasBoth = ( m_hScrollBar && m_hScrollBar->IsShown()) && ( m_vScrollBar && m_vScrollBar->IsShown()) ;
2958 int scrlsize = m_hScrollBar ? m_hScrollBar->GetSize().y : ( m_vScrollBar ? m_vScrollBar->GetSize().x : MAC_SCROLLBAR_SIZE ) ;
2959 int adjust = hasBoth ? scrlsize - 1 : 0 ;
2960
2961 // get real client area
2962
2963 int width ;
2964 int height ;
2965 GetSize( &width , &height ) ;
2966
2967 width -= MacGetLeftBorderSize() + MacGetRightBorderSize();
2968 height -= MacGetTopBorderSize() + MacGetBottomBorderSize();
2969
2970 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
2971 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
2972 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
2973 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
2974 /*
2975 int x = 0 ;
2976 int y = 0 ;
2977 int w ;
2978 int h ;
2979 GetSize( &w , &h ) ;
2980
2981 MacClientToRootWindow( &x , &y ) ;
2982 MacClientToRootWindow( &w , &h ) ;
2983
2984 wxWindowMac *iter = (wxWindowMac*)this ;
2985
2986 int totW = 10000 , totH = 10000;
2987 while( iter )
2988 {
2989 if ( iter->IsTopLevel() )
2990 {
2991 iter->GetSize( &totW , &totH ) ;
2992 break ;
2993 }
2994
2995 iter = iter->GetParent() ;
2996 }
2997
2998 if ( x == 0 )
2999 {
3000 hPoint.x = -1 ;
3001 hSize.x += 1 ;
3002 }
3003 if ( y == 0 )
3004 {
3005 vPoint.y = -1 ;
3006 vSize.y += 1 ;
3007 }
3008
3009 if ( w-x >= totW )
3010 {
3011 hSize.x += 1 ;
3012 vPoint.x += 1 ;
3013 }
3014
3015 if ( h-y >= totH )
3016 {
3017 vSize.y += 1 ;
3018 hPoint.y += 1 ;
3019 }
3020 */
3021 if ( m_vScrollBar )
3022 {
3023 m_vScrollBar->SetSize( vPoint.x , vPoint.y, vSize.x, vSize.y , wxSIZE_ALLOW_MINUS_ONE);
3024 }
3025 if ( m_hScrollBar )
3026 {
3027 m_hScrollBar->SetSize( hPoint.x , hPoint.y, hSize.x, hSize.y, wxSIZE_ALLOW_MINUS_ONE);
3028 }
3029 }
3030
3031 bool wxWindowMac::AcceptsFocus() const
3032 {
3033 return MacCanFocus() && wxWindowBase::AcceptsFocus();
3034 }
3035
3036 void wxWindowMac::MacSuperChangedPosition()
3037 {
3038 // only window-absolute structures have to be moved i.e. controls
3039
3040 wxWindowListNode *node = GetChildren().GetFirst();
3041 while ( node )
3042 {
3043 wxWindowMac *child = node->GetData();
3044 child->MacSuperChangedPosition() ;
3045 node = node->GetNext();
3046 }
3047 }
3048
3049 void wxWindowMac::MacTopLevelWindowChangedPosition()
3050 {
3051 // only screen-absolute structures have to be moved i.e. glcanvas
3052
3053 wxWindowListNode *node = GetChildren().GetFirst();
3054 while ( node )
3055 {
3056 wxWindowMac *child = node->GetData();
3057 child->MacTopLevelWindowChangedPosition() ;
3058 node = node->GetNext();
3059 }
3060 }
3061
3062 long wxWindowMac::MacGetLeftBorderSize( ) const
3063 {
3064 if( IsTopLevel() )
3065 return 0 ;
3066
3067 if (m_windowStyle & wxRAISED_BORDER || m_windowStyle & wxSUNKEN_BORDER )
3068 {
3069 SInt32 border = 3 ;
3070 return border ;
3071 }
3072 else if ( m_windowStyle &wxDOUBLE_BORDER)
3073 {
3074 SInt32 border = 3 ;
3075 return border ;
3076 }
3077 else if (m_windowStyle &wxSIMPLE_BORDER)
3078 {
3079 return 1 ;
3080 }
3081 return 0 ;
3082 }
3083
3084 long wxWindowMac::MacGetRightBorderSize( ) const
3085 {
3086 // they are all symmetric in mac themes
3087 return MacGetLeftBorderSize() ;
3088 }
3089
3090 long wxWindowMac::MacGetTopBorderSize( ) const
3091 {
3092 // they are all symmetric in mac themes
3093 return MacGetLeftBorderSize() ;
3094 }
3095
3096 long wxWindowMac::MacGetBottomBorderSize( ) const
3097 {
3098 // they are all symmetric in mac themes
3099 return MacGetLeftBorderSize() ;
3100 }
3101
3102 long wxWindowMac::MacRemoveBordersFromStyle( long style )
3103 {
3104 return style & ~wxBORDER_MASK ;
3105 }
3106
3107 // Find the wxWindowMac at the current mouse position, returning the mouse
3108 // position.
3109 wxWindowMac* wxFindWindowAtPointer(wxPoint& pt)
3110 {
3111 pt = wxGetMousePosition();
3112 wxWindowMac* found = wxFindWindowAtPoint(pt);
3113 return found;
3114 }
3115
3116 // Get the current mouse position.
3117 wxPoint wxGetMousePosition()
3118 {
3119 int x, y;
3120 wxGetMousePosition(& x, & y);
3121 return wxPoint(x, y);
3122 }
3123
3124 void wxWindowMac::OnMouseEvent( wxMouseEvent &event )
3125 {
3126 if ( event.GetEventType() == wxEVT_RIGHT_DOWN )
3127 {
3128 // copied from wxGTK : CS
3129 // generate a "context menu" event: this is similar to wxEVT_RIGHT_DOWN
3130 // except that:
3131 //
3132 // (a) it's a command event and so is propagated to the parent
3133 // (b) under MSW it can be generated from kbd too
3134 // (c) it uses screen coords (because of (a))
3135 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
3136 this->GetId(),
3137 this->ClientToScreen(event.GetPosition()));
3138 if ( ! GetEventHandler()->ProcessEvent(evtCtx) )
3139 event.Skip() ;
3140 }
3141 else
3142 {
3143 event.Skip() ;
3144 }
3145 }
3146
3147 void wxWindowMac::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED( mouseStillDown ) )
3148 {
3149 }
3150
3151 Rect wxMacGetBoundsForControl( wxWindow* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
3152 {
3153 int x ,y , w ,h ;
3154
3155 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin) ;
3156 Rect bounds = { y , x , y+h , x+w };
3157 return bounds ;
3158 }
3159
3160 wxInt32 wxWindowMac::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
3161 {
3162 return eventNotHandledErr ;
3163 }
3164
3165