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