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