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