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