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