cleanup
[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 RgnHandle rgn = NewRgn() ;
1279 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
1280 {
1281 Rect structure ;
1282 Rect content ;
1283 GetRegionBounds( rgn , &content ) ;
1284 m_peer->GetRect( &structure ) ;
1285 OffsetRect( &structure, -structure.left , -structure.top ) ;
1286
1287 left = content.left - structure.left ;
1288 top = content.top - structure.top ;
1289 right = structure.right - content.right ;
1290 bottom = structure.bottom - content.bottom ;
1291 }
1292 else
1293 {
1294 left = top = right = bottom = 0 ;
1295 }
1296 DisposeRgn( rgn ) ;
1297 }
1298
1299 wxSize wxWindowMac::DoGetSizeFromClientSize( const wxSize & size ) const
1300 {
1301 wxSize sizeTotal = size;
1302
1303 RgnHandle rgn = NewRgn() ;
1304
1305 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
1306 {
1307 Rect content ;
1308 Rect structure ;
1309 GetRegionBounds( rgn , &content ) ;
1310
1311 m_peer->GetRect( &structure ) ;
1312 // structure is in parent coordinates, but we only need width and height, so it's ok
1313
1314 sizeTotal.x += (structure.right - structure.left) - (content.right - content.left) ;
1315 sizeTotal.y += (structure.bottom - structure.top) - (content.bottom - content.top ) ;
1316 }
1317 DisposeRgn( rgn ) ;
1318
1319 sizeTotal.x += MacGetLeftBorderSize( ) + MacGetRightBorderSize( ) ;
1320 sizeTotal.y += MacGetTopBorderSize( ) + MacGetBottomBorderSize( ) ;
1321
1322 return sizeTotal;
1323 }
1324
1325
1326 // Get size *available for subwindows* i.e. excluding menu bar etc.
1327 void wxWindowMac::DoGetClientSize(int *x, int *y) const
1328 {
1329 int ww, hh;
1330
1331 RgnHandle rgn = NewRgn() ;
1332 Rect content ;
1333 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) == noErr )
1334 {
1335 GetRegionBounds( rgn , &content ) ;
1336 }
1337 else
1338 {
1339 m_peer->GetRect( &content ) ;
1340 }
1341 DisposeRgn( rgn ) ;
1342
1343 ww = content.right - content.left ;
1344 hh = content.bottom - content.top ;
1345
1346 if (m_hScrollBar && m_hScrollBar->IsShown() )
1347 {
1348 hh -= m_hScrollBar->GetSize().y ; // MAC_SCROLLBAR_SIZE ;
1349 }
1350 if (m_vScrollBar && m_vScrollBar->IsShown() )
1351 {
1352 ww -= m_vScrollBar->GetSize().x ; // MAC_SCROLLBAR_SIZE;
1353 }
1354 if(x) *x = ww;
1355 if(y) *y = hh;
1356
1357 }
1358
1359 bool wxWindowMac::SetCursor(const wxCursor& cursor)
1360 {
1361 if (m_cursor == cursor)
1362 return FALSE;
1363
1364 if (wxNullCursor == cursor)
1365 {
1366 if ( ! wxWindowBase::SetCursor( *wxSTANDARD_CURSOR ) )
1367 return FALSE ;
1368 }
1369 else
1370 {
1371 if ( ! wxWindowBase::SetCursor( cursor ) )
1372 return FALSE ;
1373 }
1374
1375 wxASSERT_MSG( m_cursor.Ok(),
1376 wxT("cursor must be valid after call to the base version"));
1377
1378
1379 wxWindowMac *mouseWin = 0 ;
1380 {
1381 wxTopLevelWindowMac *tlw = MacGetTopLevelWindow() ;
1382 WindowRef window = (WindowRef) ( tlw ? tlw->MacGetWindowRef() : 0 ) ;
1383 CGrafPtr savePort ;
1384 Boolean swapped = QDSwapPort( GetWindowPort( window ) , &savePort ) ;
1385
1386 // TODO If we ever get a GetCurrentEvent.. replacement for the mouse
1387 // position, use it...
1388
1389 Point pt ;
1390 GetMouse( &pt ) ;
1391 ControlPartCode part ;
1392 ControlRef control ;
1393 control = wxMacFindControlUnderMouse( tlw , pt , window , &part ) ;
1394 if ( control )
1395 mouseWin = wxFindControlFromMacControl( control ) ;
1396
1397 if ( swapped )
1398 QDSwapPort( savePort , NULL ) ;
1399 }
1400
1401 if ( mouseWin == this && !wxIsBusy() )
1402 {
1403 m_cursor.MacInstall() ;
1404 }
1405
1406 return TRUE ;
1407 }
1408
1409 #if wxUSE_MENUS
1410 bool wxWindowMac::DoPopupMenu(wxMenu *menu, int x, int y)
1411 {
1412 menu->SetInvokingWindow(this);
1413 menu->UpdateUI();
1414
1415 if ( x == -1 && y == -1 )
1416 {
1417 wxPoint mouse = wxGetMousePosition();
1418 x = mouse.x; y = mouse.y;
1419 }
1420 else
1421 {
1422 ClientToScreen( &x , &y ) ;
1423 }
1424
1425 menu->MacBeforeDisplay( true ) ;
1426 long menuResult = ::PopUpMenuSelect((MenuHandle) menu->GetHMenu() ,y,x, 0) ;
1427 if ( HiWord(menuResult) != 0 )
1428 {
1429 MenuCommand id ;
1430 GetMenuItemCommandID( GetMenuHandle(HiWord(menuResult)) , LoWord(menuResult) , &id ) ;
1431 wxMenuItem* item = NULL ;
1432 wxMenu* realmenu ;
1433 item = menu->FindItem(id, &realmenu) ;
1434 if (item->IsCheckable())
1435 {
1436 item->Check( !item->IsChecked() ) ;
1437 }
1438 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
1439 }
1440 menu->MacAfterDisplay( true ) ;
1441
1442 menu->SetInvokingWindow(NULL);
1443
1444 return TRUE;
1445 }
1446 #endif
1447
1448 // ----------------------------------------------------------------------------
1449 // tooltips
1450 // ----------------------------------------------------------------------------
1451
1452 #if wxUSE_TOOLTIPS
1453
1454 void wxWindowMac::DoSetToolTip(wxToolTip *tooltip)
1455 {
1456 wxWindowBase::DoSetToolTip(tooltip);
1457
1458 if ( m_tooltip )
1459 m_tooltip->SetWindow(this);
1460 }
1461
1462 #endif // wxUSE_TOOLTIPS
1463
1464 void wxWindowMac::MacInvalidateBorders()
1465 {
1466 if ( m_peer == NULL )
1467 return ;
1468
1469 bool vis = MacIsReallyShown() ;
1470 if ( !vis )
1471 return ;
1472
1473 int outerBorder = MacGetLeftBorderSize() ;
1474 if ( m_peer->NeedsFocusRect() && m_peer->HasFocus() )
1475 outerBorder += 4 ;
1476
1477 if ( outerBorder == 0 )
1478 return ;
1479
1480 // now we know that we have something to do at all
1481
1482 // as the borders are drawn on the parent we have to properly invalidate all these areas
1483 RgnHandle updateInner = NewRgn() ,
1484 updateOuter = NewRgn() ;
1485
1486 // this rectangle is in HIViewCoordinates under OSX and in Window Coordinates under Carbon
1487 Rect rect ;
1488 m_peer->GetRect( &rect ) ;
1489 RectRgn( updateInner , &rect ) ;
1490 InsetRect( &rect , -outerBorder , -outerBorder ) ;
1491 RectRgn( updateOuter , &rect ) ;
1492 DiffRgn( updateOuter , updateInner ,updateOuter ) ;
1493 #ifdef __WXMAC_OSX__
1494 GetParent()->m_peer->SetNeedsDisplay( updateOuter ) ;
1495 #else
1496 WindowRef tlw = (WindowRef) MacGetTopLevelWindowRef() ;
1497 if ( tlw )
1498 InvalWindowRgn( tlw , updateOuter ) ;
1499 #endif
1500 DisposeRgn(updateOuter) ;
1501 DisposeRgn(updateInner) ;
1502 /*
1503 RgnHandle updateInner = NewRgn() , updateOuter = NewRgn() ;
1504 RectRgn( updateInner , &rect ) ;
1505 InsetRect( &rect , -4 , -4 ) ;
1506 RectRgn( updateOuter , &rect ) ;
1507 DiffRgn( updateOuter , updateInner ,updateOuter ) ;
1508 wxPoint parent(0,0);
1509 GetParent()->MacWindowToRootWindow( &parent.x , &parent.y ) ;
1510 parent -= GetParent()->GetClientAreaOrigin() ;
1511 OffsetRgn( updateOuter , -parent.x , -parent.y ) ;
1512 GetParent()->m_peer->SetNeedsDisplay( true , updateOuter ) ;
1513 DisposeRgn(updateOuter) ;
1514 DisposeRgn(updateInner) ;
1515 */
1516 /*
1517 if ( m_peer )
1518 {
1519 // deleting a window while it is shown invalidates the region occupied by border or
1520 // focus
1521
1522 if ( IsShown() && ( outerBorder > 0 ) )
1523 {
1524 // as the borders are drawn on the parent we have to properly invalidate all these areas
1525 RgnHandle updateInner = NewRgn() , updateOuter = NewRgn() , updateTotal = NewRgn() ;
1526
1527 Rect rect ;
1528
1529 m_peer->GetRect( &rect ) ;
1530 RectRgn( updateInner , &rect ) ;
1531 InsetRect( &rect , -outerBorder , -outerBorder ) ;
1532 RectRgn( updateOuter , &rect ) ;
1533 DiffRgn( updateOuter , updateInner ,updateOuter ) ;
1534 wxPoint parent(0,0);
1535 GetParent()->MacWindowToRootWindow( &parent.x , &parent.y ) ;
1536 parent -= GetParent()->GetClientAreaOrigin() ;
1537 OffsetRgn( updateOuter , -parent.x , -parent.y ) ;
1538 CopyRgn( updateOuter , updateTotal ) ;
1539
1540 GetParent()->m_peer->SetNeedsDisplay( true , updateTotal ) ;
1541 DisposeRgn(updateOuter) ;
1542 DisposeRgn(updateInner) ;
1543 DisposeRgn(updateTotal) ;
1544 }
1545 }
1546 */
1547 #if 0
1548 Rect r = wxMacGetBoundsForControl(this , wxPoint( actualX,actualY), wxSize( actualWidth, actualHeight ) , false ) ;
1549
1550 int outerBorder = MacGetLeftBorderSize() ;
1551 if ( m_peer->NeedsFocusRect() && m_peer->HasFocus() )
1552 outerBorder += 4 ;
1553
1554 if ( vis && ( outerBorder > 0 ) )
1555 {
1556 // as the borders are drawn on the parent we have to properly invalidate all these areas
1557 RgnHandle updateInner = NewRgn() , updateOuter = NewRgn() , updateTotal = NewRgn() ;
1558
1559 Rect rect ;
1560
1561 m_peer->GetRect( &rect ) ;
1562 RectRgn( updateInner , &rect ) ;
1563 InsetRect( &rect , -outerBorder , -outerBorder ) ;
1564 RectRgn( updateOuter , &rect ) ;
1565 DiffRgn( updateOuter , updateInner ,updateOuter ) ;
1566 /*
1567 wxPoint parent(0,0);
1568 #if TARGET_API_MAC_OSX
1569 // no offsetting needed when compositing
1570 #else
1571 GetParent()->MacWindowToRootWindow( &parent.x , &parent.y ) ;
1572 parent -= GetParent()->GetClientAreaOrigin() ;
1573 OffsetRgn( updateOuter , -parent.x , -parent.y ) ;
1574 #endif
1575 */
1576 CopyRgn( updateOuter , updateTotal ) ;
1577
1578 rect = r ;
1579 RectRgn( updateInner , &rect ) ;
1580 InsetRect( &rect , -outerBorder , -outerBorder ) ;
1581 RectRgn( updateOuter , &rect ) ;
1582 DiffRgn( updateOuter , updateInner ,updateOuter ) ;
1583 /*
1584 OffsetRgn( updateOuter , -parent.x , -parent.y ) ;
1585 */
1586 UnionRgn( updateOuter , updateTotal , updateTotal ) ;
1587
1588 GetParent()->m_peer->SetNeedsDisplay( updateTotal ) ;
1589 DisposeRgn(updateOuter) ;
1590 DisposeRgn(updateInner) ;
1591 DisposeRgn(updateTotal) ;
1592 }
1593 #endif
1594 }
1595
1596 void wxWindowMac::DoMoveWindow(int x, int y, int width, int height)
1597 {
1598 // this is never called for a toplevel window, so we know we have a parent
1599 int former_x , former_y , former_w, former_h ;
1600
1601 // Get true coordinates of former position
1602 DoGetPosition( &former_x , &former_y ) ;
1603 DoGetSize( &former_w , &former_h ) ;
1604
1605 wxWindow *parent = GetParent();
1606 if ( parent )
1607 {
1608 wxPoint pt(parent->GetClientAreaOrigin());
1609 former_x += pt.x ;
1610 former_y += pt.y ;
1611 }
1612
1613 int actualWidth = width ;
1614 int actualHeight = height ;
1615 int actualX = x;
1616 int actualY = y;
1617
1618 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
1619 actualWidth = m_minWidth;
1620 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
1621 actualHeight = m_minHeight;
1622 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
1623 actualWidth = m_maxWidth;
1624 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
1625 actualHeight = m_maxHeight;
1626
1627 bool doMove = false ;
1628 bool doResize = false ;
1629
1630 if ( actualX != former_x || actualY != former_y )
1631 {
1632 doMove = true ;
1633 }
1634 if ( actualWidth != former_w || actualHeight != former_h )
1635 {
1636 doResize = true ;
1637 }
1638
1639 if ( doMove || doResize )
1640 {
1641 // as the borders are drawn outside the native control, we adjust now
1642
1643 wxRect bounds( wxPoint( actualX + MacGetLeftBorderSize() ,actualY + MacGetTopBorderSize() ),
1644 wxSize( actualWidth - (MacGetLeftBorderSize() + MacGetRightBorderSize()) ,
1645 actualHeight - (MacGetTopBorderSize() + MacGetBottomBorderSize()) ) ) ;
1646
1647 Rect r ;
1648 wxMacRectToNative( &bounds , &r ) ;
1649
1650 if ( !GetParent()->IsTopLevel() )
1651 {
1652 wxMacWindowToNative( GetParent() , &r ) ;
1653 }
1654
1655 MacInvalidateBorders() ;
1656
1657 m_peer->SetRect( &r ) ;
1658
1659 if ( doMove )
1660 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
1661
1662 MacInvalidateBorders() ;
1663
1664 MacRepositionScrollBars() ;
1665 if ( doMove )
1666 {
1667 wxPoint point(actualX,actualY);
1668 wxMoveEvent event(point, m_windowId);
1669 event.SetEventObject(this);
1670 GetEventHandler()->ProcessEvent(event) ;
1671 }
1672 if ( doResize )
1673 {
1674 MacRepositionScrollBars() ;
1675 wxSize size(actualWidth, actualHeight);
1676 wxSizeEvent event(size, m_windowId);
1677 event.SetEventObject(this);
1678 GetEventHandler()->ProcessEvent(event);
1679 }
1680 }
1681
1682 }
1683
1684 wxSize wxWindowMac::DoGetBestSize() const
1685 {
1686 if ( m_macIsUserPane || IsTopLevel() )
1687 return wxWindowBase::DoGetBestSize() ;
1688
1689 Rect bestsize = { 0 , 0 , 0 , 0 } ;
1690 int bestWidth, bestHeight ;
1691 m_peer->GetBestRect( &bestsize ) ;
1692
1693 if ( EmptyRect( &bestsize ) )
1694 {
1695 bestsize.left = bestsize.top = 0 ;
1696 bestsize.right = 16 ;
1697 bestsize.bottom = 16 ;
1698 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
1699 {
1700 bestsize.bottom = 16 ;
1701 }
1702 #if wxUSE_SPINBTN
1703 else if ( IsKindOf( CLASSINFO( wxSpinButton ) ) )
1704 {
1705 bestsize.bottom = 24 ;
1706 }
1707 #endif // wxUSE_SPINBTN
1708 else
1709 {
1710 // return wxWindowBase::DoGetBestSize() ;
1711 }
1712 }
1713
1714 bestWidth = bestsize.right - bestsize.left ;
1715 bestHeight = bestsize.bottom - bestsize.top ;
1716 if ( bestHeight < 10 )
1717 bestHeight = 13 ;
1718
1719 return wxSize(bestWidth, bestHeight);
1720 }
1721
1722
1723 // set the size of the window: if the dimensions are positive, just use them,
1724 // but if any of them is equal to -1, it means that we must find the value for
1725 // it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
1726 // which case -1 is a valid value for x and y)
1727 //
1728 // If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
1729 // the width/height to best suit our contents, otherwise we reuse the current
1730 // width/height
1731 void wxWindowMac::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1732 {
1733 // get the current size and position...
1734 int currentX, currentY;
1735 GetPosition(&currentX, &currentY);
1736
1737 int currentW,currentH;
1738 GetSize(&currentW, &currentH);
1739
1740 // ... and don't do anything (avoiding flicker) if it's already ok
1741 if ( x == currentX && y == currentY &&
1742 width == currentW && height == currentH && ( height != -1 && width != -1 ) )
1743 {
1744 // TODO REMOVE
1745 MacRepositionScrollBars() ; // we might have a real position shift
1746 return;
1747 }
1748
1749 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1750 x = currentX;
1751 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1752 y = currentY;
1753
1754 AdjustForParentClientOrigin(x, y, sizeFlags);
1755
1756 wxSize size(-1, -1);
1757 if ( width == -1 )
1758 {
1759 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
1760 {
1761 size = DoGetBestSize();
1762 width = size.x;
1763 }
1764 else
1765 {
1766 // just take the current one
1767 width = currentW;
1768 }
1769 }
1770
1771 if ( height == -1 )
1772 {
1773 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
1774 {
1775 if ( size.x == -1 )
1776 {
1777 size = DoGetBestSize();
1778 }
1779 //else: already called DoGetBestSize() above
1780
1781 height = size.y;
1782 }
1783 else
1784 {
1785 // just take the current one
1786 height = currentH;
1787 }
1788 }
1789
1790 DoMoveWindow(x, y, width, height);
1791
1792 }
1793
1794 wxPoint wxWindowMac::GetClientAreaOrigin() const
1795 {
1796 RgnHandle rgn = NewRgn() ;
1797 Rect content ;
1798 if ( m_peer->GetRegion( kControlContentMetaPart , rgn ) )
1799 {
1800 GetRegionBounds( rgn , &content ) ;
1801 }
1802 else
1803 {
1804 content.left = content.top = 0 ;
1805 }
1806 DisposeRgn( rgn ) ;
1807 return wxPoint( content.left + MacGetLeftBorderSize( ) , content.top + MacGetTopBorderSize( ) );
1808 }
1809
1810 void wxWindowMac::DoSetClientSize(int clientwidth, int clientheight)
1811 {
1812 if ( clientheight != -1 || clientheight != -1 )
1813 {
1814 int currentclientwidth , currentclientheight ;
1815 int currentwidth , currentheight ;
1816
1817 GetClientSize( &currentclientwidth , &currentclientheight ) ;
1818 GetSize( &currentwidth , &currentheight ) ;
1819
1820 DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth ,
1821 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
1822 }
1823 }
1824
1825 void wxWindowMac::SetTitle(const wxString& title)
1826 {
1827 m_label = wxStripMenuCodes(title) ;
1828
1829 if ( m_peer && m_peer->Ok() )
1830 {
1831 m_peer->SetTitle( m_label ) ;
1832 }
1833 Refresh() ;
1834 }
1835
1836 wxString wxWindowMac::GetTitle() const
1837 {
1838 return m_label ;
1839 }
1840
1841 bool wxWindowMac::Show(bool show)
1842 {
1843 bool former = MacIsReallyShown() ;
1844 if ( !wxWindowBase::Show(show) )
1845 return FALSE;
1846
1847 // TODO use visibilityChanged Carbon Event for OSX
1848 if ( m_peer )
1849 {
1850 m_peer->SetVisibility( show , true ) ;
1851 }
1852 if ( former != MacIsReallyShown() )
1853 MacPropagateVisibilityChanged() ;
1854 return TRUE;
1855 }
1856
1857 bool wxWindowMac::Enable(bool enable)
1858 {
1859 wxASSERT( m_peer->Ok() ) ;
1860 bool former = MacIsReallyEnabled() ;
1861 if ( !wxWindowBase::Enable(enable) )
1862 return FALSE;
1863
1864 m_peer->Enable( enable ) ;
1865
1866 if ( former != MacIsReallyEnabled() )
1867 MacPropagateEnabledStateChanged() ;
1868 return TRUE;
1869 }
1870
1871 //
1872 // status change propagations (will be not necessary for OSX later )
1873 //
1874
1875 void wxWindowMac::MacPropagateVisibilityChanged()
1876 {
1877 #if !TARGET_API_MAC_OSX
1878 MacVisibilityChanged() ;
1879
1880 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1881 while ( node )
1882 {
1883 wxWindowMac *child = node->GetData();
1884 if ( child->IsShown() )
1885 child->MacPropagateVisibilityChanged( ) ;
1886 node = node->GetNext();
1887 }
1888 #endif
1889 }
1890
1891 void wxWindowMac::MacPropagateEnabledStateChanged( )
1892 {
1893 #if !TARGET_API_MAC_OSX
1894 MacEnabledStateChanged() ;
1895
1896 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1897 while ( node )
1898 {
1899 wxWindowMac *child = node->GetData();
1900 if ( child->IsEnabled() )
1901 child->MacPropagateEnabledStateChanged() ;
1902 node = node->GetNext();
1903 }
1904 #endif
1905 }
1906
1907 void wxWindowMac::MacPropagateHiliteChanged( )
1908 {
1909 #if !TARGET_API_MAC_OSX
1910 MacHiliteChanged() ;
1911
1912 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1913 while ( node )
1914 {
1915 wxWindowMac *child = node->GetData();
1916 // if ( child->IsEnabled() )
1917 child->MacPropagateHiliteChanged() ;
1918 node = node->GetNext();
1919 }
1920 #endif
1921 }
1922
1923 //
1924 // status change notifications
1925 //
1926
1927 void wxWindowMac::MacVisibilityChanged()
1928 {
1929 }
1930
1931 void wxWindowMac::MacHiliteChanged()
1932 {
1933 }
1934
1935 void wxWindowMac::MacEnabledStateChanged()
1936 {
1937 }
1938
1939 //
1940 // status queries on the inherited window's state
1941 //
1942
1943 bool wxWindowMac::MacIsReallyShown()
1944 {
1945 // only under OSX the visibility of the TLW is taken into account
1946 if ( m_isBeingDeleted )
1947 return false ;
1948
1949 #if TARGET_API_MAC_OSX
1950 if ( m_peer && m_peer->Ok() )
1951 return m_peer->IsVisible();
1952 #endif
1953 wxWindow* win = this ;
1954 while( win->IsShown() )
1955 {
1956 if ( win->IsTopLevel() )
1957 return true ;
1958
1959 win = win->GetParent() ;
1960 if ( win == NULL )
1961 return true ;
1962
1963 } ;
1964 return false ;
1965 }
1966
1967 bool wxWindowMac::MacIsReallyEnabled()
1968 {
1969 return m_peer->IsEnabled() ;
1970 }
1971
1972 bool wxWindowMac::MacIsReallyHilited()
1973 {
1974 return m_peer->IsActive();
1975 }
1976
1977 void wxWindowMac::MacFlashInvalidAreas()
1978 {
1979 #if TARGET_API_MAC_OSX
1980 HIViewFlashDirtyArea( (WindowRef) MacGetTopLevelWindowRef() ) ;
1981 #endif
1982 }
1983
1984 //
1985 //
1986 //
1987
1988 int wxWindowMac::GetCharHeight() const
1989 {
1990 wxClientDC dc ( (wxWindowMac*)this ) ;
1991 return dc.GetCharHeight() ;
1992 }
1993
1994 int wxWindowMac::GetCharWidth() const
1995 {
1996 wxClientDC dc ( (wxWindowMac*)this ) ;
1997 return dc.GetCharWidth() ;
1998 }
1999
2000 void wxWindowMac::GetTextExtent(const wxString& string, int *x, int *y,
2001 int *descent, int *externalLeading, const wxFont *theFont ) const
2002 {
2003 const wxFont *fontToUse = theFont;
2004 if ( !fontToUse )
2005 fontToUse = &m_font;
2006
2007 wxClientDC dc( (wxWindowMac*) this ) ;
2008 long lx,ly,ld,le ;
2009 dc.GetTextExtent( string , &lx , &ly , &ld, &le, (wxFont *)fontToUse ) ;
2010 if ( externalLeading )
2011 *externalLeading = le ;
2012 if ( descent )
2013 *descent = ld ;
2014 if ( x )
2015 *x = lx ;
2016 if ( y )
2017 *y = ly ;
2018 }
2019
2020 /*
2021 * Rect is given in client coordinates, for further reading, read wxTopLevelWindowMac::InvalidateRect
2022 * we always intersect with the entire window, not only with the client area
2023 */
2024
2025 void wxWindowMac::Refresh(bool eraseBack, const wxRect *rect)
2026 {
2027 if ( m_peer == NULL )
2028 return ;
2029
2030 if ( !MacIsReallyShown() )
2031 return ;
2032
2033 if ( rect )
2034 {
2035 Rect r ;
2036 wxMacRectToNative( rect , &r ) ;
2037 m_peer->SetNeedsDisplay( &r ) ;
2038 }
2039 else
2040 {
2041 m_peer->SetNeedsDisplay() ;
2042 }
2043 }
2044
2045 void wxWindowMac::Freeze()
2046 {
2047 #if TARGET_API_MAC_OSX
2048 if ( !m_frozenness++ )
2049 {
2050 if ( m_peer && m_peer->Ok() )
2051 m_peer->SetDrawingEnabled( false ) ;
2052 }
2053 #endif
2054 }
2055
2056
2057 void wxWindowMac::Thaw()
2058 {
2059 #if TARGET_API_MAC_OSX
2060 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
2061
2062 if ( !--m_frozenness )
2063 {
2064 if ( m_peer && m_peer->Ok() )
2065 {
2066 m_peer->SetDrawingEnabled( true ) ;
2067 m_peer->InvalidateWithChildren() ;
2068 }
2069 }
2070 #endif
2071 }
2072
2073 wxWindowMac *wxGetActiveWindow()
2074 {
2075 // actually this is a windows-only concept
2076 return NULL;
2077 }
2078
2079 // Coordinates relative to the window
2080 void wxWindowMac::WarpPointer (int x_pos, int y_pos)
2081 {
2082 // We really don't move the mouse programmatically under Mac.
2083 }
2084
2085 void wxWindowMac::OnEraseBackground(wxEraseEvent& event)
2086 {
2087 #if TARGET_API_MAC_OSX
2088 if ( MacGetTopLevelWindow()->MacUsesCompositing() && (m_macBackgroundBrush.Ok() == false || m_macBackgroundBrush.GetStyle() == wxTRANSPARENT ) )
2089 {
2090 event.Skip() ;
2091 }
2092 else
2093 #endif
2094 {
2095 event.GetDC()->Clear() ;
2096 }
2097 }
2098
2099 void wxWindowMac::OnNcPaint( wxNcPaintEvent& event )
2100 {
2101 event.Skip() ;
2102 }
2103
2104 int wxWindowMac::GetScrollPos(int orient) const
2105 {
2106 if ( orient == wxHORIZONTAL )
2107 {
2108 if ( m_hScrollBar )
2109 return m_hScrollBar->GetThumbPosition() ;
2110 }
2111 else
2112 {
2113 if ( m_vScrollBar )
2114 return m_vScrollBar->GetThumbPosition() ;
2115 }
2116 return 0;
2117 }
2118
2119 // This now returns the whole range, not just the number
2120 // of positions that we can scroll.
2121 int wxWindowMac::GetScrollRange(int orient) const
2122 {
2123 if ( orient == wxHORIZONTAL )
2124 {
2125 if ( m_hScrollBar )
2126 return m_hScrollBar->GetRange() ;
2127 }
2128 else
2129 {
2130 if ( m_vScrollBar )
2131 return m_vScrollBar->GetRange() ;
2132 }
2133 return 0;
2134 }
2135
2136 int wxWindowMac::GetScrollThumb(int orient) const
2137 {
2138 if ( orient == wxHORIZONTAL )
2139 {
2140 if ( m_hScrollBar )
2141 return m_hScrollBar->GetThumbSize() ;
2142 }
2143 else
2144 {
2145 if ( m_vScrollBar )
2146 return m_vScrollBar->GetThumbSize() ;
2147 }
2148 return 0;
2149 }
2150
2151 void wxWindowMac::SetScrollPos(int orient, int pos, bool refresh)
2152 {
2153 if ( orient == wxHORIZONTAL )
2154 {
2155 if ( m_hScrollBar )
2156 m_hScrollBar->SetThumbPosition( pos ) ;
2157 }
2158 else
2159 {
2160 if ( m_vScrollBar )
2161 m_vScrollBar->SetThumbPosition( pos ) ;
2162 }
2163 }
2164
2165 //
2166 // we draw borders and grow boxes, are already set up and clipped in the current port / cgContextRef
2167 // our own window origin is at leftOrigin/rightOrigin
2168 //
2169
2170 void wxWindowMac::MacPaintBorders( int leftOrigin , int rightOrigin )
2171 {
2172 if( IsTopLevel() )
2173 return ;
2174
2175 Rect rect ;
2176 bool hasFocus = m_peer->NeedsFocusRect() && m_peer->HasFocus() ;
2177 bool hasBothScrollbars = ( m_hScrollBar && m_hScrollBar->IsShown()) && ( m_vScrollBar && m_vScrollBar->IsShown()) ;
2178
2179 m_peer->GetRect( &rect ) ;
2180 InsetRect( &rect, -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2181
2182 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
2183 if ( UMAGetSystemVersion() >= 0x1030 )
2184 {
2185 Rect srect = rect ;
2186 HIThemeFrameDrawInfo info ;
2187 memset( &info, 0 , sizeof( info ) ) ;
2188
2189 info.version = 0 ;
2190 info.kind = 0 ;
2191 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive ;
2192 info.isFocused = hasFocus ;
2193 bool draw = false ;
2194
2195 CGContextRef cgContext = (CGContextRef) GetParent()->MacGetCGContextRef() ;
2196 wxASSERT( cgContext ) ;
2197
2198 if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
2199 {
2200 SInt32 border = 0 ;
2201 GetThemeMetric( kThemeMetricEditTextFrameOutset , &border ) ;
2202 InsetRect( &srect , border , border );
2203 info.kind = kHIThemeFrameTextFieldSquare ;
2204 draw = true ;
2205 }
2206 else if (HasFlag(wxSIMPLE_BORDER))
2207 {
2208 SInt32 border = 0 ;
2209 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2210 InsetRect( &srect , border , border );
2211 info.kind = kHIThemeFrameListBox ;
2212 draw = true ;
2213 }
2214
2215 if ( draw )
2216 {
2217 CGRect cgrect = CGRectMake( srect.left , srect.top , srect.right - srect.left ,
2218 srect.bottom - srect.top ) ;
2219 HIThemeDrawFrame( &cgrect , &info , cgContext , kHIThemeOrientationNormal ) ;
2220 }
2221 else if ( hasFocus )
2222 {
2223 srect = rect ;
2224 CGRect cgrect = CGRectMake( srect.left , srect.top , srect.right - srect.left ,
2225 srect.bottom - srect.top ) ;
2226 HIThemeDrawFocusRect( &cgrect , true , cgContext , kHIThemeOrientationNormal ) ;
2227 }
2228
2229 m_peer->GetRect( &rect ) ;
2230 if ( hasBothScrollbars )
2231 {
2232 srect = rect ;
2233 int size = m_hScrollBar->GetWindowVariant() == wxWINDOW_VARIANT_NORMAL ? 16 : 12 ;
2234 CGRect cgrect = CGRectMake( srect.right - size , srect.bottom - size , size , size ) ;
2235 CGPoint cgpoint = CGPointMake( srect.right - size , srect.bottom - size ) ;
2236 HIThemeGrowBoxDrawInfo info ;
2237 memset( &info, 0 , sizeof( info ) ) ;
2238 info.version = 0 ;
2239 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive ;
2240 info.kind = kHIThemeGrowBoxKindNone ;
2241 info.size = kHIThemeGrowBoxSizeNormal ;
2242 info.direction = kThemeGrowRight | kThemeGrowDown ;
2243 HIThemeDrawGrowBox( &cgpoint , &info , cgContext , kHIThemeOrientationNormal ) ;
2244 }
2245 }
2246 else
2247 #endif
2248 {
2249 wxTopLevelWindowMac* top = MacGetTopLevelWindow();
2250 if (top )
2251 {
2252 wxPoint pt(0,0) ;
2253 wxMacControl::Convert( &pt , GetParent()->m_peer , top->m_peer ) ;
2254 OffsetRect( &rect , pt.x , pt.y ) ;
2255 }
2256
2257 if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
2258 {
2259 Rect srect = rect ;
2260 SInt32 border = 0 ;
2261 GetThemeMetric( kThemeMetricEditTextFrameOutset , &border ) ;
2262 InsetRect( &srect , border , border );
2263 DrawThemeEditTextFrame(&srect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
2264 }
2265 else if (HasFlag(wxSIMPLE_BORDER))
2266 {
2267 Rect srect = rect ;
2268 SInt32 border = 0 ;
2269 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2270 InsetRect( &srect , border , border );
2271 DrawThemeListBoxFrame(&rect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
2272 }
2273
2274 if ( hasFocus )
2275 {
2276 Rect srect = rect ;
2277 DrawThemeFocusRect( &srect , true ) ;
2278 }
2279 if ( hasBothScrollbars )
2280 {
2281 // GetThemeStandaloneGrowBoxBounds
2282 //DrawThemeStandaloneNoGrowBox
2283 }
2284 }
2285 }
2286
2287 void wxWindowMac::RemoveChild( wxWindowBase *child )
2288 {
2289 if ( child == m_hScrollBar )
2290 m_hScrollBar = NULL ;
2291 if ( child == m_vScrollBar )
2292 m_vScrollBar = NULL ;
2293
2294 wxWindowBase::RemoveChild( child ) ;
2295 }
2296
2297 // New function that will replace some of the above.
2298 void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible,
2299 int range, bool refresh)
2300 {
2301 if ( orient == wxHORIZONTAL )
2302 {
2303 if ( m_hScrollBar )
2304 {
2305 if ( range == 0 || thumbVisible >= range )
2306 {
2307 if ( m_hScrollBar->IsShown() )
2308 m_hScrollBar->Show(false) ;
2309 }
2310 else
2311 {
2312 if ( !m_hScrollBar->IsShown() )
2313 m_hScrollBar->Show(true) ;
2314 }
2315 m_hScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2316 }
2317 }
2318 else
2319 {
2320 if ( m_vScrollBar )
2321 {
2322 if ( range == 0 || thumbVisible >= range )
2323 {
2324 if ( m_vScrollBar->IsShown() )
2325 m_vScrollBar->Show(false) ;
2326 }
2327 else
2328 {
2329 if ( !m_vScrollBar->IsShown() )
2330 m_vScrollBar->Show(true) ;
2331 }
2332 m_vScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
2333 }
2334 }
2335 MacRepositionScrollBars() ;
2336 }
2337
2338 // Does a physical scroll
2339 void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect)
2340 {
2341 if( dx == 0 && dy ==0 )
2342 return ;
2343
2344
2345 {
2346
2347 int width , height ;
2348 GetClientSize( &width , &height ) ;
2349 #if TARGET_API_MAC_OSX
2350 // note there currently is a bug in OSX which makes inefficient refreshes in case an entire control
2351 // area is scrolled, this does not occur if width and height are 2 pixels less,
2352 // TODO write optimal workaround
2353 wxRect scrollrect( MacGetLeftBorderSize() , MacGetTopBorderSize() , width , height ) ;
2354 if ( rect )
2355 {
2356 scrollrect.Intersect( *rect ) ;
2357 }
2358 if ( m_peer->GetNeedsDisplay() )
2359 {
2360 // becuase HIViewScrollRect does not scroll the already invalidated area we have two options
2361 // either immediate redraw or full invalidate
2362 #if 1
2363 // is the better overall solution, as it does not slow down scrolling
2364 m_peer->SetNeedsDisplay() ;
2365 #else
2366 // this would be the preferred version for fast drawing controls
2367 if( UMAGetSystemVersion() < 0x1030 )
2368 Update() ;
2369 else
2370 HIViewRender(m_peer->GetControlRef()) ;
2371 #endif
2372 }
2373 // as the native control might be not a 0/0 wx window coordinates, we have to offset
2374 scrollrect.Offset( -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2375 m_peer->ScrollRect( scrollrect , dx , dy ) ;
2376 #else
2377
2378 wxPoint pos;
2379 pos.x = pos.y = 0;
2380
2381 Rect scrollrect;
2382 RgnHandle updateRgn = NewRgn() ;
2383
2384 {
2385 wxClientDC dc(this) ;
2386 wxMacPortSetter helper(&dc) ;
2387
2388 m_peer->GetRect( &scrollrect ) ;
2389 scrollrect.top += MacGetTopBorderSize() ;
2390 scrollrect.left += MacGetLeftBorderSize() ;
2391 scrollrect.bottom = scrollrect.top + height ;
2392 scrollrect.right = scrollrect.left + width ;
2393
2394 if ( rect )
2395 {
2396 Rect r = { dc.YLOG2DEVMAC(rect->y) , dc.XLOG2DEVMAC(rect->x) , dc.YLOG2DEVMAC(rect->y + rect->height) ,
2397 dc.XLOG2DEVMAC(rect->x + rect->width) } ;
2398 SectRect( &scrollrect , &r , &scrollrect ) ;
2399 }
2400 ScrollRect( &scrollrect , dx , dy , updateRgn ) ;
2401
2402 // now scroll the former update region as well and add the new update region
2403
2404 WindowRef rootWindow = (WindowRef) MacGetTopLevelWindowRef() ;
2405 RgnHandle formerUpdateRgn = NewRgn() ;
2406 RgnHandle scrollRgn = NewRgn() ;
2407 RectRgn( scrollRgn , &scrollrect ) ;
2408 GetWindowUpdateRgn( rootWindow , formerUpdateRgn ) ;
2409 Point pt = {0,0} ;
2410 LocalToGlobal( &pt ) ;
2411 OffsetRgn( formerUpdateRgn , -pt.h , -pt.v ) ;
2412 SectRgn( formerUpdateRgn , scrollRgn , formerUpdateRgn ) ;
2413 if ( !EmptyRgn( formerUpdateRgn ) )
2414 {
2415 MacOffsetRgn( formerUpdateRgn , dx , dy ) ;
2416 SectRgn( formerUpdateRgn , scrollRgn , formerUpdateRgn ) ;
2417 InvalWindowRgn(rootWindow , formerUpdateRgn ) ;
2418 }
2419 InvalWindowRgn(rootWindow , updateRgn ) ;
2420 DisposeRgn( updateRgn ) ;
2421 DisposeRgn( formerUpdateRgn ) ;
2422 DisposeRgn( scrollRgn ) ;
2423 }
2424 #endif
2425 }
2426
2427 for (wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); node; node = node->GetNext())
2428 {
2429 wxWindowMac *child = node->GetData();
2430 if (child == m_vScrollBar) continue;
2431 if (child == m_hScrollBar) continue;
2432 if (child->IsTopLevel()) continue;
2433
2434 int x,y;
2435 child->GetPosition( &x, &y );
2436 int w,h;
2437 child->GetSize( &w, &h );
2438 if (rect)
2439 {
2440 wxRect rc(x,y,w,h);
2441 if (rect->Intersects(rc))
2442 child->SetSize( x+dx, y+dy, w, h );
2443 }
2444 else
2445 {
2446 child->SetSize( x+dx, y+dy, w, h );
2447 }
2448 }
2449 }
2450
2451 void wxWindowMac::MacOnScroll(wxScrollEvent &event )
2452 {
2453 if ( event.GetEventObject() == m_vScrollBar || event.GetEventObject() == m_hScrollBar )
2454 {
2455 wxScrollWinEvent wevent;
2456 wevent.SetPosition(event.GetPosition());
2457 wevent.SetOrientation(event.GetOrientation());
2458 wevent.SetEventObject(this);
2459
2460 if (event.GetEventType() == wxEVT_SCROLL_TOP)
2461 wevent.SetEventType( wxEVT_SCROLLWIN_TOP );
2462 else if (event.GetEventType() == wxEVT_SCROLL_BOTTOM)
2463 wevent.SetEventType( wxEVT_SCROLLWIN_BOTTOM );
2464 else if (event.GetEventType() == wxEVT_SCROLL_LINEUP)
2465 wevent.SetEventType( wxEVT_SCROLLWIN_LINEUP );
2466 else if (event.GetEventType() == wxEVT_SCROLL_LINEDOWN)
2467 wevent.SetEventType( wxEVT_SCROLLWIN_LINEDOWN );
2468 else if (event.GetEventType() == wxEVT_SCROLL_PAGEUP)
2469 wevent.SetEventType( wxEVT_SCROLLWIN_PAGEUP );
2470 else if (event.GetEventType() == wxEVT_SCROLL_PAGEDOWN)
2471 wevent.SetEventType( wxEVT_SCROLLWIN_PAGEDOWN );
2472 else if (event.GetEventType() == wxEVT_SCROLL_THUMBTRACK)
2473 wevent.SetEventType( wxEVT_SCROLLWIN_THUMBTRACK );
2474 else if (event.GetEventType() == wxEVT_SCROLL_THUMBRELEASE)
2475 wevent.SetEventType( wxEVT_SCROLLWIN_THUMBRELEASE );
2476
2477 GetEventHandler()->ProcessEvent(wevent);
2478 }
2479 }
2480
2481 // Get the window with the focus
2482 wxWindowMac *wxWindowBase::DoFindFocus()
2483 {
2484 ControlRef control ;
2485 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
2486 return wxFindControlFromMacControl( control ) ;
2487 }
2488
2489 void wxWindowMac::OnSetFocus(wxFocusEvent& event)
2490 {
2491 // panel wants to track the window which was the last to have focus in it,
2492 // so we want to set ourselves as the window which last had focus
2493 //
2494 // notice that it's also important to do it upwards the tree becaus
2495 // otherwise when the top level panel gets focus, it won't set it back to
2496 // us, but to some other sibling
2497
2498 // CS:don't know if this is still needed:
2499 //wxChildFocusEvent eventFocus(this);
2500 //(void)GetEventHandler()->ProcessEvent(eventFocus);
2501
2502 if ( MacGetTopLevelWindow() && m_peer->NeedsFocusRect() )
2503 {
2504 #if !wxMAC_USE_CORE_GRAPHICS
2505 wxMacWindowStateSaver sv( this ) ;
2506 Rect rect ;
2507 m_peer->GetRect( &rect ) ;
2508 InsetRect( &rect, -MacGetLeftBorderSize() , -MacGetTopBorderSize() ) ;
2509
2510 wxTopLevelWindowMac* top = MacGetTopLevelWindow();
2511 if (top )
2512 {
2513 wxPoint pt(0,0) ;
2514 wxMacControl::Convert( &pt , GetParent()->m_peer , top->m_peer ) ;
2515 rect.left += pt.x ;
2516 rect.right += pt.x ;
2517 rect.top += pt.y ;
2518 rect.bottom += pt.y ;
2519 }
2520
2521 if ( event.GetEventType() == wxEVT_SET_FOCUS )
2522 DrawThemeFocusRect( &rect , true ) ;
2523 else
2524 {
2525 DrawThemeFocusRect( &rect , false ) ;
2526
2527 // as this erases part of the frame we have to redraw borders
2528 // and because our z-ordering is not always correct (staticboxes)
2529 // we have to invalidate things, we cannot simple redraw
2530 MacInvalidateBorders() ;
2531 }
2532 #else
2533 GetParent()->Refresh() ;
2534 #endif
2535 }
2536
2537 event.Skip();
2538 }
2539
2540 void wxWindowMac::OnInternalIdle()
2541 {
2542 // This calls the UI-update mechanism (querying windows for
2543 // menu/toolbar/control state information)
2544 if (wxUpdateUIEvent::CanUpdate(this))
2545 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2546 }
2547
2548 // Raise the window to the top of the Z order
2549 void wxWindowMac::Raise()
2550 {
2551 m_peer->SetZOrder( true , NULL ) ;
2552 }
2553
2554 // Lower the window to the bottom of the Z order
2555 void wxWindowMac::Lower()
2556 {
2557 m_peer->SetZOrder( false , NULL ) ;
2558 }
2559
2560
2561 // static wxWindow *gs_lastWhich = NULL;
2562
2563 bool wxWindowMac::MacSetupCursor( const wxPoint& pt)
2564 {
2565 // first trigger a set cursor event
2566
2567 wxPoint clientorigin = GetClientAreaOrigin() ;
2568 wxSize clientsize = GetClientSize() ;
2569 wxCursor cursor ;
2570 if ( wxRect2DInt( clientorigin.x , clientorigin.y , clientsize.x , clientsize.y ).Contains( wxPoint2DInt( pt ) ) )
2571 {
2572 wxSetCursorEvent event( pt.x , pt.y );
2573
2574 bool processedEvtSetCursor = GetEventHandler()->ProcessEvent(event);
2575 if ( processedEvtSetCursor && event.HasCursor() )
2576 {
2577 cursor = event.GetCursor() ;
2578 }
2579 else
2580 {
2581
2582 // the test for processedEvtSetCursor is here to prevent using m_cursor
2583 // if the user code caught EVT_SET_CURSOR() and returned nothing from
2584 // it - this is a way to say that our cursor shouldn't be used for this
2585 // point
2586 if ( !processedEvtSetCursor && m_cursor.Ok() )
2587 {
2588 cursor = m_cursor ;
2589 }
2590 if ( wxIsBusy() )
2591 {
2592 }
2593 else
2594 {
2595 if ( !GetParent() )
2596 cursor = *wxSTANDARD_CURSOR ;
2597 }
2598 }
2599 if ( cursor.Ok() )
2600 cursor.MacInstall() ;
2601 }
2602 return cursor.Ok() ;
2603 }
2604
2605 wxString wxWindowMac::MacGetToolTipString( wxPoint &pt )
2606 {
2607 if ( m_tooltip )
2608 {
2609 return m_tooltip->GetTip() ;
2610 }
2611 return wxEmptyString ;
2612 }
2613
2614 void wxWindowMac::ClearBackground()
2615 {
2616 Refresh() ;
2617 Update() ;
2618 }
2619
2620 void wxWindowMac::Update()
2621 {
2622 #if TARGET_API_MAC_OSX
2623
2624 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
2625 WindowRef window = (WindowRef)MacGetTopLevelWindowRef() ;
2626
2627 // for composited windows this also triggers a redraw of all
2628 // invalid views in the window
2629 if( UMAGetSystemVersion() >= 0x1030 )
2630 HIWindowFlush(window) ;
2631 else
2632 #endif
2633 {
2634 // the only way to trigger the redrawing on earlier systems is to call
2635 // ReceiveNextEvent
2636
2637 EventRef currentEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
2638 UInt32 currentEventClass = 0 ;
2639 UInt32 currentEventKind = 0 ;
2640 if ( currentEvent != NULL )
2641 {
2642 currentEventClass = ::GetEventClass( currentEvent ) ;
2643 currentEventKind = ::GetEventKind( currentEvent ) ;
2644 }
2645 if ( currentEventClass != kEventClassMenu )
2646 {
2647 // when tracking a menu, strange redraw errors occur if we flush now, so leave..
2648
2649 EventRef theEvent;
2650 OSStatus status = noErr ;
2651 status = ReceiveNextEvent( 0 , NULL , kEventDurationNoWait , false , &theEvent ) ;
2652 }
2653 else
2654 m_peer->SetNeedsDisplay() ;
2655 }
2656 #else
2657 ::Draw1Control( m_peer->GetControlRef() ) ;
2658 #endif
2659 }
2660
2661 wxTopLevelWindowMac* wxWindowMac::MacGetTopLevelWindow() const
2662 {
2663 wxTopLevelWindowMac* win = NULL ;
2664 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
2665 if ( window )
2666 {
2667 win = wxFindWinFromMacWindow( window ) ;
2668 }
2669 return win ;
2670 }
2671 wxRegion wxWindowMac::MacGetVisibleRegion( bool includeOuterStructures )
2672 {
2673 // includeOuterStructures is true if we try to draw somthing like a focus ring etc.
2674 // also a window dc uses this, in this case we only clip in the hierarchy for hard
2675 // borders like a scrollwindow, splitter etc otherwise we end up in a paranoia having
2676 // to add focus borders everywhere
2677
2678 Rect r ;
2679 RgnHandle visRgn = NewRgn() ;
2680 RgnHandle tempRgn = NewRgn() ;
2681 if ( !m_isBeingDeleted && MacIsReallyShown() /*m_peer->IsVisible() */ )
2682 {
2683 m_peer->GetRect( &r ) ;
2684 r.left -= MacGetLeftBorderSize() ;
2685 r.top -= MacGetTopBorderSize() ;
2686 r.bottom += MacGetBottomBorderSize() ;
2687 r.right += MacGetRightBorderSize() ;
2688
2689 r.right -= r.left ;
2690 r.bottom -= r.top ;
2691 r.left = 0 ;
2692 r.top = 0 ;
2693
2694 if ( includeOuterStructures )
2695 InsetRect( &r , -4 , -4 ) ;
2696 RectRgn( visRgn , &r ) ;
2697
2698 if ( !IsTopLevel() )
2699 {
2700 wxWindow* child = this ;
2701 wxWindow* parent = child->GetParent() ;
2702 while( parent )
2703 {
2704 int x , y ;
2705 wxSize size ;
2706 // we have to find a better clipping algorithm here, in order not to clip things
2707 // positioned like status and toolbar
2708 if ( 1 /* parent->IsTopLevel() && child->IsKindOf( CLASSINFO( wxToolBar ) ) */ )
2709 {
2710 size = parent->GetSize() ;
2711 x = y = 0 ;
2712 }
2713 else
2714 {
2715 size = parent->GetClientSize() ;
2716 wxPoint origin = parent->GetClientAreaOrigin() ;
2717 x = origin.x ;
2718 y = origin.y ;
2719 }
2720 parent->MacWindowToRootWindow( &x, &y ) ;
2721 MacRootWindowToWindow( &x , &y ) ;
2722
2723 if ( !includeOuterStructures || (
2724 parent->MacClipChildren() ||
2725 ( parent->GetParent() && parent->GetParent()->MacClipGrandChildren() )
2726 ) )
2727 {
2728 SetRectRgn( tempRgn ,
2729 x + parent->MacGetLeftBorderSize() , y + parent->MacGetTopBorderSize() ,
2730 x + size.x - parent->MacGetRightBorderSize(),
2731 y + size.y - parent->MacGetBottomBorderSize()) ;
2732
2733 SectRgn( visRgn , tempRgn , visRgn ) ;
2734 }
2735 if ( parent->IsTopLevel() )
2736 break ;
2737 child = parent ;
2738 parent = child->GetParent() ;
2739 }
2740 }
2741 }
2742
2743 wxRegion vis = visRgn ;
2744 DisposeRgn( visRgn ) ;
2745 DisposeRgn( tempRgn ) ;
2746 return vis ;
2747 }
2748
2749 /*
2750 This function must not change the updatergn !
2751 */
2752 bool wxWindowMac::MacDoRedraw( WXHRGN updatergnr , long time )
2753 {
2754 RgnHandle updatergn = (RgnHandle) updatergnr ;
2755 bool handled = false ;
2756 Rect updatebounds ;
2757 GetRegionBounds( updatergn , &updatebounds ) ;
2758
2759 // wxLogDebug(wxT("update for %s bounds %d , %d , %d , %d"),wxString(GetClassInfo()->GetClassName()).c_str(), updatebounds.left , updatebounds.top , updatebounds.right , updatebounds.bottom ) ;
2760
2761 if ( !EmptyRgn(updatergn) )
2762 {
2763 RgnHandle newupdate = NewRgn() ;
2764 wxSize point = GetClientSize() ;
2765 wxPoint origin = GetClientAreaOrigin() ;
2766 SetRectRgn( newupdate , origin.x , origin.y , origin.x + point.x , origin.y+point.y ) ;
2767 SectRgn( newupdate , updatergn , newupdate ) ;
2768
2769 // first send an erase event to the entire update area
2770 {
2771 // for the toplevel window this really is the entire area
2772 // for all the others only their client area, otherwise they
2773 // might be drawing with full alpha and eg put blue into
2774 // the grow-box area of a scrolled window (scroll sample)
2775 wxDC* dc ;
2776 if ( IsTopLevel() )
2777 dc = new wxWindowDC(this);
2778 else
2779 dc = new wxClientDC(this);
2780 dc->SetClippingRegion(wxRegion(updatergn));
2781 wxEraseEvent eevent( GetId(), dc );
2782 eevent.SetEventObject( this );
2783 GetEventHandler()->ProcessEvent( eevent );
2784 delete dc ;
2785 }
2786
2787 // calculate a client-origin version of the update rgn and set m_updateRegion to that
2788 OffsetRgn( newupdate , -origin.x , -origin.y ) ;
2789 m_updateRegion = newupdate ;
2790 DisposeRgn( newupdate ) ;
2791
2792 if ( !m_updateRegion.Empty() )
2793 {
2794 // paint the window itself
2795
2796 wxPaintEvent event;
2797 event.SetTimestamp(time);
2798 event.SetEventObject(this);
2799 GetEventHandler()->ProcessEvent(event);
2800 handled = true ;
2801 }
2802
2803 // now we cannot rely on having its borders drawn by a window itself, as it does not
2804 // get the updateRgn wide enough to always do so, so we do it from the parent
2805 // this would also be the place to draw any custom backgrounds for native controls
2806 // in Composited windowing
2807 wxPoint clientOrigin = GetClientAreaOrigin() ;
2808
2809 for (wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); node; node = node->GetNext())
2810 {
2811 wxWindowMac *child = node->GetData();
2812 if (child == m_vScrollBar) continue;
2813 if (child == m_hScrollBar) continue;
2814 if (child->IsTopLevel()) continue;
2815 if (!child->IsShown()) continue;
2816
2817 // only draw those in the update region (add a safety margin of 10 pixels for shadow effects
2818
2819 int x,y;
2820 child->GetPosition( &x, &y );
2821 int w,h;
2822 child->GetSize( &w, &h );
2823 Rect childRect = { y , x , y + h , x + w } ;
2824 OffsetRect( &childRect , clientOrigin.x , clientOrigin.y ) ;
2825 InsetRect( &childRect , -10 , -10) ;
2826
2827 if ( RectInRgn( &childRect , updatergn ) )
2828 {
2829
2830 // paint custom borders
2831 wxNcPaintEvent eventNc( child->GetId() );
2832 eventNc.SetEventObject( child );
2833 if ( !child->GetEventHandler()->ProcessEvent( eventNc ) )
2834 {
2835 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
2836 if ( UMAGetSystemVersion() >= 0x1030 )
2837 {
2838 child->MacPaintBorders(0,0) ;
2839 }
2840 else
2841 #endif
2842 {
2843 wxWindowDC dc(this) ;
2844 dc.SetClippingRegion(wxRegion(updatergn));
2845 wxMacPortSetter helper(&dc) ;
2846 child->MacPaintBorders(0,0) ;
2847 }
2848 }
2849 }
2850 }
2851 }
2852 return handled ;
2853 }
2854
2855
2856 WXWindow wxWindowMac::MacGetTopLevelWindowRef() const
2857 {
2858 wxWindowMac *iter = (wxWindowMac*)this ;
2859
2860 while( iter )
2861 {
2862 if ( iter->IsTopLevel() )
2863 return ((wxTopLevelWindow*)iter)->MacGetWindowRef() ;
2864
2865 iter = iter->GetParent() ;
2866 }
2867 wxASSERT_MSG( 1 , wxT("No valid mac root window") ) ;
2868 return NULL ;
2869 }
2870
2871 void wxWindowMac::MacCreateScrollBars( long style )
2872 {
2873 wxASSERT_MSG( m_vScrollBar == NULL && m_hScrollBar == NULL , wxT("attempt to create window twice") ) ;
2874
2875 if ( style & ( wxVSCROLL | wxHSCROLL ) )
2876 {
2877 bool hasBoth = ( style & wxVSCROLL ) && ( style & wxHSCROLL ) ;
2878 int scrlsize = MAC_SCROLLBAR_SIZE ;
2879 wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL ;
2880 if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL || GetWindowVariant() == wxWINDOW_VARIANT_MINI )
2881 {
2882 scrlsize = MAC_SMALL_SCROLLBAR_SIZE ;
2883 variant = wxWINDOW_VARIANT_SMALL ;
2884 }
2885
2886 int adjust = hasBoth ? scrlsize - 1: 0 ;
2887 int width, height ;
2888 GetClientSize( &width , &height ) ;
2889
2890 wxPoint vPoint(width-scrlsize, 0) ;
2891 wxSize vSize(scrlsize, height - adjust) ;
2892 wxPoint hPoint(0 , height-scrlsize ) ;
2893 wxSize hSize( width - adjust, scrlsize) ;
2894
2895
2896 if ( style & wxVSCROLL )
2897 {
2898 m_vScrollBar = new wxScrollBar(this, wxID_ANY, vPoint,
2899 vSize , wxVERTICAL);
2900 }
2901
2902 if ( style & wxHSCROLL )
2903 {
2904 m_hScrollBar = new wxScrollBar(this, wxID_ANY, hPoint,
2905 hSize , wxHORIZONTAL);
2906 }
2907 }
2908
2909
2910 // because the create does not take into account the client area origin
2911 MacRepositionScrollBars() ; // we might have a real position shift
2912 }
2913
2914 void wxWindowMac::MacRepositionScrollBars()
2915 {
2916 if ( !m_hScrollBar && !m_vScrollBar )
2917 return ;
2918
2919 bool hasBoth = ( m_hScrollBar && m_hScrollBar->IsShown()) && ( m_vScrollBar && m_vScrollBar->IsShown()) ;
2920 int scrlsize = m_hScrollBar ? m_hScrollBar->GetSize().y : ( m_vScrollBar ? m_vScrollBar->GetSize().x : MAC_SCROLLBAR_SIZE ) ;
2921 int adjust = hasBoth ? scrlsize - 1 : 0 ;
2922
2923 // get real client area
2924
2925 int width ;
2926 int height ;
2927 GetSize( &width , &height ) ;
2928
2929 width -= MacGetLeftBorderSize() + MacGetRightBorderSize();
2930 height -= MacGetTopBorderSize() + MacGetBottomBorderSize();
2931
2932 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
2933 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
2934 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
2935 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
2936 /*
2937 int x = 0 ;
2938 int y = 0 ;
2939 int w ;
2940 int h ;
2941 GetSize( &w , &h ) ;
2942
2943 MacClientToRootWindow( &x , &y ) ;
2944 MacClientToRootWindow( &w , &h ) ;
2945
2946 wxWindowMac *iter = (wxWindowMac*)this ;
2947
2948 int totW = 10000 , totH = 10000;
2949 while( iter )
2950 {
2951 if ( iter->IsTopLevel() )
2952 {
2953 iter->GetSize( &totW , &totH ) ;
2954 break ;
2955 }
2956
2957 iter = iter->GetParent() ;
2958 }
2959
2960 if ( x == 0 )
2961 {
2962 hPoint.x = -1 ;
2963 hSize.x += 1 ;
2964 }
2965 if ( y == 0 )
2966 {
2967 vPoint.y = -1 ;
2968 vSize.y += 1 ;
2969 }
2970
2971 if ( w-x >= totW )
2972 {
2973 hSize.x += 1 ;
2974 vPoint.x += 1 ;
2975 }
2976
2977 if ( h-y >= totH )
2978 {
2979 vSize.y += 1 ;
2980 hPoint.y += 1 ;
2981 }
2982 */
2983 if ( m_vScrollBar )
2984 {
2985 m_vScrollBar->SetSize( vPoint.x , vPoint.y, vSize.x, vSize.y , wxSIZE_ALLOW_MINUS_ONE);
2986 }
2987 if ( m_hScrollBar )
2988 {
2989 m_hScrollBar->SetSize( hPoint.x , hPoint.y, hSize.x, hSize.y, wxSIZE_ALLOW_MINUS_ONE);
2990 }
2991 }
2992
2993 bool wxWindowMac::AcceptsFocus() const
2994 {
2995 return MacCanFocus() && wxWindowBase::AcceptsFocus();
2996 }
2997
2998 void wxWindowMac::MacSuperChangedPosition()
2999 {
3000 // only window-absolute structures have to be moved i.e. controls
3001
3002 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
3003 while ( node )
3004 {
3005 wxWindowMac *child = node->GetData();
3006 child->MacSuperChangedPosition() ;
3007 node = node->GetNext();
3008 }
3009 }
3010
3011 void wxWindowMac::MacTopLevelWindowChangedPosition()
3012 {
3013 // only screen-absolute structures have to be moved i.e. glcanvas
3014
3015 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
3016 while ( node )
3017 {
3018 wxWindowMac *child = node->GetData();
3019 child->MacTopLevelWindowChangedPosition() ;
3020 node = node->GetNext();
3021 }
3022 }
3023
3024 long wxWindowMac::MacGetLeftBorderSize( ) const
3025 {
3026 if( IsTopLevel() )
3027 return 0 ;
3028
3029 if (m_windowStyle & wxRAISED_BORDER || m_windowStyle & wxSUNKEN_BORDER )
3030 {
3031 SInt32 border = 3 ;
3032 return border ;
3033 }
3034 else if ( m_windowStyle &wxDOUBLE_BORDER)
3035 {
3036 SInt32 border = 3 ;
3037 return border ;
3038 }
3039 else if (m_windowStyle &wxSIMPLE_BORDER)
3040 {
3041 return 1 ;
3042 }
3043 return 0 ;
3044 }
3045
3046 long wxWindowMac::MacGetRightBorderSize( ) const
3047 {
3048 // they are all symmetric in mac themes
3049 return MacGetLeftBorderSize() ;
3050 }
3051
3052 long wxWindowMac::MacGetTopBorderSize( ) const
3053 {
3054 // they are all symmetric in mac themes
3055 return MacGetLeftBorderSize() ;
3056 }
3057
3058 long wxWindowMac::MacGetBottomBorderSize( ) const
3059 {
3060 // they are all symmetric in mac themes
3061 return MacGetLeftBorderSize() ;
3062 }
3063
3064 long wxWindowMac::MacRemoveBordersFromStyle( long style )
3065 {
3066 return style & ~wxBORDER_MASK ;
3067 }
3068
3069 // Find the wxWindowMac at the current mouse position, returning the mouse
3070 // position.
3071 wxWindowMac* wxFindWindowAtPointer(wxPoint& pt)
3072 {
3073 pt = wxGetMousePosition();
3074 wxWindowMac* found = wxFindWindowAtPoint(pt);
3075 return found;
3076 }
3077
3078 // Get the current mouse position.
3079 wxPoint wxGetMousePosition()
3080 {
3081 int x, y;
3082 wxGetMousePosition(& x, & y);
3083 return wxPoint(x, y);
3084 }
3085
3086 void wxWindowMac::OnMouseEvent( wxMouseEvent &event )
3087 {
3088 if ( event.GetEventType() == wxEVT_RIGHT_DOWN )
3089 {
3090 // copied from wxGTK : CS
3091 // generate a "context menu" event: this is similar to wxEVT_RIGHT_DOWN
3092 // except that:
3093 //
3094 // (a) it's a command event and so is propagated to the parent
3095 // (b) under MSW it can be generated from kbd too
3096 // (c) it uses screen coords (because of (a))
3097 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
3098 this->GetId(),
3099 this->ClientToScreen(event.GetPosition()));
3100 if ( ! GetEventHandler()->ProcessEvent(evtCtx) )
3101 event.Skip() ;
3102 }
3103 else
3104 {
3105 event.Skip() ;
3106 }
3107 }
3108
3109 void wxWindowMac::OnPaint( wxPaintEvent & event )
3110 {
3111 if ( wxTheApp->MacGetCurrentEvent() != NULL && wxTheApp->MacGetCurrentEventHandlerCallRef() != NULL )
3112 {
3113 CallNextEventHandler((EventHandlerCallRef)wxTheApp->MacGetCurrentEventHandlerCallRef() , (EventRef) wxTheApp->MacGetCurrentEvent() ) ;
3114 }
3115 }
3116
3117 void wxWindowMac::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED( mouseStillDown ) )
3118 {
3119 }
3120
3121 Rect wxMacGetBoundsForControl( wxWindow* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
3122 {
3123 int x ,y , w ,h ;
3124
3125 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin) ;
3126 Rect bounds = { y , x , y+h , x+w };
3127 return bounds ;
3128 }
3129
3130 wxInt32 wxWindowMac::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
3131 {
3132 return eventNotHandledErr ;
3133 }
3134
3135