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