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