]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/window.cpp
mouse handling fixed, skipping if we have no native control handling
[wxWidgets.git] / src / mac / carbon / window.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: windows.cpp
3 // Purpose: wxWindowMac
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "window.h"
14 #endif
15
16 #include "wx/setup.h"
17 #include "wx/menu.h"
18 #include "wx/window.h"
19 #include "wx/dc.h"
20 #include "wx/dcclient.h"
21 #include "wx/utils.h"
22 #include "wx/app.h"
23 #include "wx/panel.h"
24 #include "wx/layout.h"
25 #include "wx/dialog.h"
26 #include "wx/scrolbar.h"
27 #include "wx/statbox.h"
28 #include "wx/button.h"
29 #include "wx/settings.h"
30 #include "wx/msgdlg.h"
31 #include "wx/frame.h"
32 #include "wx/tooltip.h"
33 #include "wx/statusbr.h"
34 #include "wx/menuitem.h"
35 #include "wx/spinctrl.h"
36 #include "wx/log.h"
37 #include "wx/geometry.h"
38
39 #include "wx/toolbar.h"
40 #include "wx/dc.h"
41
42 #if wxUSE_CARET
43 #include "wx/caret.h"
44 #endif // wxUSE_CARET
45
46 #define wxWINDOW_HSCROLL 5998
47 #define wxWINDOW_VSCROLL 5997
48 #define MAC_SCROLLBAR_SIZE 16
49
50 #include "wx/mac/uma.h"
51 #ifndef __DARWIN__
52 #include <Windows.h>
53 #include <ToolUtils.h>
54 #endif
55
56 #ifndef __HIVIEW__
57 #include <HIToolbox/HIView.h>
58 #endif
59
60 #if wxUSE_DRAG_AND_DROP
61 #include "wx/dnd.h"
62 #endif
63
64 #include <string.h>
65
66 extern wxList wxPendingDelete;
67 wxWindowMac* gFocusWindow = NULL ;
68
69 #ifdef __WXUNIVERSAL__
70 IMPLEMENT_ABSTRACT_CLASS(wxWindowMac, wxWindowBase)
71 #else // __WXMAC__
72 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
73 #endif // __WXUNIVERSAL__/__WXMAC__
74
75 #if !USE_SHARED_LIBRARY
76
77 BEGIN_EVENT_TABLE(wxWindowMac, wxWindowBase)
78 EVT_NC_PAINT(wxWindowMac::OnNcPaint)
79 EVT_ERASE_BACKGROUND(wxWindowMac::OnEraseBackground)
80 // TODO EVT_PAINT(wxWindowMac::OnPaint)
81 EVT_SYS_COLOUR_CHANGED(wxWindowMac::OnSysColourChanged)
82 EVT_INIT_DIALOG(wxWindowMac::OnInitDialog)
83 EVT_SET_FOCUS(wxWindowMac::OnSetFocus)
84 EVT_MOUSE_EVENTS(wxWindowMac::OnMouseEvent)
85 END_EVENT_TABLE()
86
87 #endif
88
89 #define wxMAC_DEBUG_REDRAW 0
90 #ifndef wxMAC_DEBUG_REDRAW
91 #define wxMAC_DEBUG_REDRAW 0
92 #endif
93
94 #define wxMAC_USE_THEME_BORDER 0
95
96 // ---------------------------------------------------------------------------
97 // Carbon Events
98 // ---------------------------------------------------------------------------
99
100 extern long wxMacTranslateKey(unsigned char key, unsigned char code) ;
101 pascal OSStatus wxMacSetupControlBackground( ControlRef iControl , SInt16 iMessage , SInt16 iDepth , Boolean iIsColor ) ;
102
103 static const EventTypeSpec eventList[] =
104 {
105 #if TARGET_API_MAC_OSX
106 { kEventClassControl , kEventControlDraw } ,
107 // { kEventClassControl , kEventControlInvalidateForSizeChange } , // 10.3 only
108 // { kEventClassControl , kEventControlBoundsChanged } ,
109 #else
110 {}
111 #endif
112 } ;
113
114 static pascal OSStatus wxMacWindowControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
115 {
116 OSStatus result = eventNotHandledErr ;
117
118 wxMacCarbonEvent cEvent( event ) ;
119
120 ControlRef controlRef ;
121 wxWindowMac* thisWindow = (wxWindowMac*) data ;
122
123 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
124
125 switch( GetEventKind( event ) )
126 {
127 case kEventControlDraw :
128 {
129 RgnHandle updateRgn = cEvent.GetParameter<RgnHandle>(kEventParamRgnHandle) ;
130 // GrafPtr myport = cEvent.GetParameter<GrafPtr>(kEventParamGrafPort,typeGrafPtr) ;
131
132 #if 0 // in case we would need a coregraphics compliant background erase first
133 CGContextRef cgContext = cEvent.GetParameter<CGContextRef>(kEventParamCGContextRef) ;
134 if ( thisWindow->MacIsUserPane() )
135 {
136 HIRect bounds;
137 err = HIViewGetBounds( controlRef, &bounds );
138 CGContextSetRGBFillColor( cgContext, 1 , 1 , 1 , 1 );
139 // CGContextSetRGBFillColor( cgContext, .95, .95, .95, 1 );
140 CGContextFillRect( cgContext, bounds );
141 }
142 #endif
143 if ( thisWindow->MacDoRedraw( updateRgn , cEvent.GetTicks() ) )
144 result = noErr ;
145 else
146 result = eventNotHandledErr;
147 }
148 break ;
149 default :
150 break ;
151 }
152 return result ;
153 }
154
155 pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
156 {
157 OSStatus result = eventNotHandledErr ;
158
159 switch ( GetEventClass( event ) )
160 {
161 case kEventClassControl :
162 result = wxMacWindowControlEventHandler( handler, event, data ) ;
163 break ;
164 default :
165 break ;
166 }
167 return result ;
168 }
169
170 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler )
171
172 // ---------------------------------------------------------------------------
173 // UserPane events for non OSX builds
174 // ---------------------------------------------------------------------------
175
176 static pascal void wxMacControlUserPaneDrawProc(ControlRef control, SInt16 part)
177 {
178 wxWindow * win = wxFindControlFromMacControl(control) ;
179 wxCHECK_RET( win , wxT("Callback from unkown control") ) ;
180 win->MacControlUserPaneDrawProc(part) ;
181 }
182
183 static pascal ControlPartCode wxMacControlUserPaneHitTestProc(ControlRef control, Point where)
184 {
185 wxWindow * win = wxFindControlFromMacControl(control) ;
186 wxCHECK_MSG( win , kControlNoPart , wxT("Callback from unkown control") ) ;
187 return win->MacControlUserPaneHitTestProc(where.h , where.v) ;
188 }
189
190 static pascal ControlPartCode wxMacControlUserPaneTrackingProc(ControlRef control, Point startPt, ControlActionUPP actionProc)
191 {
192 wxWindow * win = wxFindControlFromMacControl(control) ;
193 wxCHECK_MSG( win , kControlNoPart , wxT("Callback from unkown control") ) ;
194 return win->MacControlUserPaneTrackingProc( startPt.h , startPt.v , (void*) actionProc) ;
195 }
196
197 static pascal void wxMacControlUserPaneIdleProc(ControlRef control)
198 {
199 wxWindow * win = wxFindControlFromMacControl(control) ;
200 wxCHECK_RET( win , wxT("Callback from unkown control") ) ;
201 win->MacControlUserPaneIdleProc() ;
202 }
203
204 static pascal ControlPartCode wxMacControlUserPaneKeyDownProc(ControlRef control, SInt16 keyCode, SInt16 charCode, SInt16 modifiers)
205 {
206 wxWindow * win = wxFindControlFromMacControl(control) ;
207 wxCHECK_MSG( win , kControlNoPart , wxT("Callback from unkown control") ) ;
208 return win->MacControlUserPaneKeyDownProc(keyCode,charCode,modifiers) ;
209 }
210
211 static pascal void wxMacControlUserPaneActivateProc(ControlRef control, Boolean activating)
212 {
213 wxWindow * win = wxFindControlFromMacControl(control) ;
214 wxCHECK_RET( win , wxT("Callback from unkown control") ) ;
215 win->MacControlUserPaneActivateProc(activating) ;
216 }
217
218 static pascal ControlPartCode wxMacControlUserPaneFocusProc(ControlRef control, ControlFocusPart action)
219 {
220 wxWindow * win = wxFindControlFromMacControl(control) ;
221 wxCHECK_MSG( win , kControlNoPart , wxT("Callback from unkown control") ) ;
222 return win->MacControlUserPaneFocusProc(action) ;
223 }
224
225 static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control, ControlBackgroundPtr info)
226 {
227 wxWindow * win = wxFindControlFromMacControl(control) ;
228 wxCHECK_RET( win , wxT("Callback from unkown control") ) ;
229 win->MacControlUserPaneBackgroundProc(info) ;
230 }
231
232 void wxWindowMac::MacControlUserPaneDrawProc(wxInt16 part)
233 {
234 MacDoRedraw( MacGetVisibleRegion().GetWXHRGN() , 0 ) ;
235 }
236
237 wxInt16 wxWindowMac::MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y)
238 {
239 return kControlNoPart ;
240 }
241
242 wxInt16 wxWindowMac::MacControlUserPaneTrackingProc(wxInt16 x, wxInt16 y, void* actionProc)
243 {
244 return kControlNoPart ;
245 }
246
247 void wxWindowMac::MacControlUserPaneIdleProc()
248 {
249 }
250
251 wxInt16 wxWindowMac::MacControlUserPaneKeyDownProc(wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers)
252 {
253 return kControlNoPart ;
254 }
255
256 void wxWindowMac::MacControlUserPaneActivateProc(bool activating)
257 {
258 }
259
260 wxInt16 wxWindowMac::MacControlUserPaneFocusProc(wxInt16 action)
261 {
262 return kControlNoPart ;
263 }
264
265 void wxWindowMac::MacControlUserPaneBackgroundProc(void* info)
266 {
267 }
268
269 ControlUserPaneDrawUPP gControlUserPaneDrawUPP = NULL ;
270 ControlUserPaneHitTestUPP gControlUserPaneHitTestUPP = NULL ;
271 ControlUserPaneTrackingUPP gControlUserPaneTrackingUPP = NULL ;
272 ControlUserPaneIdleUPP gControlUserPaneIdleUPP = NULL ;
273 ControlUserPaneKeyDownUPP gControlUserPaneKeyDownUPP = NULL ;
274 ControlUserPaneActivateUPP gControlUserPaneActivateUPP = NULL ;
275 ControlUserPaneFocusUPP gControlUserPaneFocusUPP = NULL ;
276 ControlUserPaneBackgroundUPP gControlUserPaneBackgroundUPP = NULL ;
277
278 // ===========================================================================
279 // implementation
280 // ===========================================================================
281
282 wxList wxWinMacControlList(wxKEY_INTEGER);
283
284 wxWindow *wxFindControlFromMacControl(ControlRef inControl )
285 {
286 wxNode *node = wxWinMacControlList.Find((long)inControl);
287 if (!node)
288 return NULL;
289 return (wxControl *)node->GetData();
290 }
291
292 void wxAssociateControlWithMacControl(ControlRef inControl, wxWindow *control)
293 {
294 // adding NULL ControlRef is (first) surely a result of an error and
295 // (secondly) breaks native event processing
296 wxCHECK_RET( inControl != (ControlRef) NULL, wxT("attempt to add a NULL WindowRef to window list") );
297
298 if ( !wxWinMacControlList.Find((long)inControl) )
299 wxWinMacControlList.Append((long)inControl, control);
300 }
301
302 void wxRemoveMacControlAssociation(wxWindow *control)
303 {
304 wxWinMacControlList.DeleteObject(control);
305 }
306
307 // UPP functions
308 ControlActionUPP wxMacLiveScrollbarActionUPP = NULL ;
309
310 ControlColorUPP wxMacSetupControlBackgroundUPP = NULL ;
311
312 // we have to setup the brush in the current port and return noErr
313 // or return an error code so that the control manager walks further up the
314 // hierarchy to find a correct background
315
316 pascal OSStatus wxMacSetupControlBackground( ControlRef iControl , SInt16 iMessage , SInt16 iDepth , Boolean iIsColor )
317 {
318 OSStatus status = paramErr ;
319 switch( iMessage )
320 {
321 case kControlMsgApplyTextColor :
322 break ;
323 case kControlMsgSetUpBackground :
324 {
325 wxWindow* wx = (wxWindow*) wxFindControlFromMacControl( iControl ) ;
326 if ( wx != NULL )
327 {
328 /*
329 const wxBrush &brush = wx->MacGetBackgroundBrush() ;
330 if ( brush.Ok() )
331 {
332
333 wxDC::MacSetupBackgroundForCurrentPort( brush ) ;
334 */
335 // this clipping is only needed for non HIView
336
337 RgnHandle clip = NewRgn() ;
338 int x = 0 , y = 0;
339
340 wx->MacWindowToRootWindow( &x,&y ) ;
341 CopyRgn( (RgnHandle) wx->MacGetVisibleRegion().GetWXHRGN() , clip ) ;
342 OffsetRgn( clip , x , y ) ;
343 SetClip( clip ) ;
344 DisposeRgn( clip ) ;
345
346 status = noErr ;
347 /*
348 }
349 else if ( wx->MacIsUserPane() )
350 {
351 // if we don't have a valid brush for such a control, we have to call the
352 // setup of our parent ourselves
353 status = SetUpControlBackground( (ControlRef) wx->GetParent()->GetHandle() , iDepth , iIsColor ) ;
354 }
355 */
356 }
357 }
358 break ;
359 default :
360 break ;
361 }
362 return status ;
363 }
364
365
366 pascal void wxMacLiveScrollbarActionProc( ControlRef control , ControlPartCode partCode ) ;
367 pascal void wxMacLiveScrollbarActionProc( ControlRef control , ControlPartCode partCode )
368 {
369 if ( partCode != 0)
370 {
371 wxWindow* wx = wxFindControlFromMacControl( control ) ;
372 if ( wx )
373 {
374 wx->MacHandleControlClick( (WXWidget) control , partCode , true /* stillDown */ ) ;
375 }
376 }
377 }
378
379 // ----------------------------------------------------------------------------
380 // constructors and such
381 // ----------------------------------------------------------------------------
382
383 void wxWindowMac::Init()
384 {
385 m_backgroundTransparent = FALSE;
386
387 // as all windows are created with WS_VISIBLE style...
388 m_isShown = TRUE;
389
390 m_hScrollBar = NULL ;
391 m_vScrollBar = NULL ;
392 m_macBackgroundBrush = wxNullBrush ;
393
394 m_macControl = NULL ;
395
396 m_macIsUserPane = TRUE;
397
398 // make sure all proc ptrs are available
399
400 if ( gControlUserPaneDrawUPP == NULL )
401 {
402 gControlUserPaneDrawUPP = NewControlUserPaneDrawUPP( wxMacControlUserPaneDrawProc ) ;
403 gControlUserPaneHitTestUPP = NewControlUserPaneHitTestUPP( wxMacControlUserPaneHitTestProc ) ;
404 gControlUserPaneTrackingUPP = NewControlUserPaneTrackingUPP( wxMacControlUserPaneTrackingProc ) ;
405 gControlUserPaneIdleUPP = NewControlUserPaneIdleUPP( wxMacControlUserPaneIdleProc ) ;
406 gControlUserPaneKeyDownUPP = NewControlUserPaneKeyDownUPP( wxMacControlUserPaneKeyDownProc ) ;
407 gControlUserPaneActivateUPP = NewControlUserPaneActivateUPP( wxMacControlUserPaneActivateProc ) ;
408 gControlUserPaneFocusUPP = NewControlUserPaneFocusUPP( wxMacControlUserPaneFocusProc ) ;
409 gControlUserPaneBackgroundUPP = NewControlUserPaneBackgroundUPP( wxMacControlUserPaneBackgroundProc ) ;
410 }
411 if ( wxMacLiveScrollbarActionUPP == NULL )
412 {
413 wxMacLiveScrollbarActionUPP = NewControlActionUPP( wxMacLiveScrollbarActionProc );
414 }
415
416 if ( wxMacSetupControlBackgroundUPP == NULL )
417 {
418 wxMacSetupControlBackgroundUPP = NewControlColorUPP( wxMacSetupControlBackground ) ;
419 }
420
421 }
422
423 // Destructor
424 wxWindowMac::~wxWindowMac()
425 {
426 SendDestroyEvent();
427
428 m_isBeingDeleted = TRUE;
429
430 #ifndef __WXUNIVERSAL__
431 // VS: make sure there's no wxFrame with last focus set to us:
432 for ( wxWindow *win = GetParent(); win; win = win->GetParent() )
433 {
434 wxFrame *frame = wxDynamicCast(win, wxFrame);
435 if ( frame )
436 {
437 if ( frame->GetLastFocus() == this )
438 {
439 frame->SetLastFocus((wxWindow*)NULL);
440 }
441 break;
442 }
443 }
444 #endif // __WXUNIVERSAL__
445
446 // wxRemoveMacControlAssociation( this ) ;
447 // If we delete an item, we should initialize the parent panel,
448 // because it could now be invalid.
449 wxWindow *parent = GetParent() ;
450 if ( parent )
451 {
452 if (parent->GetDefaultItem() == (wxButton*) this)
453 parent->SetDefaultItem(NULL);
454 }
455 if ( (ControlRef) m_macControl )
456 {
457 // in case the callback might be called during destruction
458 wxRemoveMacControlAssociation( this) ;
459 ::SetControlColorProc( (ControlRef) m_macControl , NULL ) ;
460 ::DisposeControl( (ControlRef) m_macControl ) ;
461 m_macControl = NULL ;
462 }
463
464 if ( g_MacLastWindow == this )
465 {
466 g_MacLastWindow = NULL ;
467 }
468
469 wxFrame* frame = wxDynamicCast( wxGetTopLevelParent( this ) , wxFrame ) ;
470 if ( frame )
471 {
472 if ( frame->GetLastFocus() == this )
473 frame->SetLastFocus( NULL ) ;
474 }
475
476 if ( gFocusWindow == this )
477 {
478 gFocusWindow = NULL ;
479 }
480
481 DestroyChildren();
482
483 // delete our drop target if we've got one
484 #if wxUSE_DRAG_AND_DROP
485 if ( m_dropTarget != NULL )
486 {
487 delete m_dropTarget;
488 m_dropTarget = NULL;
489 }
490 #endif // wxUSE_DRAG_AND_DROP
491 }
492
493 //
494
495 void wxWindowMac::MacInstallEventHandler()
496 {
497 InstallControlEventHandler( (ControlRef) m_macControl, GetwxMacWindowEventHandlerUPP(),
498 GetEventTypeCount(eventList), eventList, this,
499 (EventHandlerRef *)&m_macControlEventHandler);
500
501 }
502
503 // Constructor
504 bool wxWindowMac::Create(wxWindowMac *parent, wxWindowID id,
505 const wxPoint& pos,
506 const wxSize& size,
507 long style,
508 const wxString& name)
509 {
510 wxCHECK_MSG( parent, FALSE, wxT("can't create wxWindowMac without parent") );
511
512 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
513 return FALSE;
514
515 parent->AddChild(this);
516
517 m_windowVariant = parent->GetWindowVariant() ;
518
519 if ( m_macIsUserPane )
520 {
521 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
522
523 UInt32 features = kControlSupportsEmbedding | kControlSupportsLiveFeedback /*| kControlHasSpecialBackground */ | kControlSupportsCalcBestRect | kControlHandlesTracking | kControlSupportsFocus | kControlWantsActivate | kControlWantsIdle;
524
525 ::CreateUserPaneControl( MAC_WXHWND(GetParent()->MacGetTopLevelWindowRef()) , &bounds, features, (ControlRef*) &m_macControl);
526
527 MacPostControlCreate(pos,size) ;
528 #if !TARGET_API_MAC_OSX
529 SetControlData((ControlRef) m_macControl,kControlEntireControl,kControlUserPaneDrawProcTag,
530 sizeof(gControlUserPaneDrawUPP),(Ptr) &gControlUserPaneDrawUPP);
531 SetControlData((ControlRef) m_macControl,kControlEntireControl,kControlUserPaneHitTestProcTag,
532 sizeof(gControlUserPaneHitTestUPP),(Ptr) &gControlUserPaneHitTestUPP);
533 SetControlData((ControlRef) m_macControl,kControlEntireControl,kControlUserPaneTrackingProcTag,
534 sizeof(gControlUserPaneTrackingUPP),(Ptr) &gControlUserPaneTrackingUPP);
535 SetControlData((ControlRef) m_macControl,kControlEntireControl,kControlUserPaneIdleProcTag,
536 sizeof(gControlUserPaneIdleUPP),(Ptr) &gControlUserPaneIdleUPP);
537 SetControlData((ControlRef) m_macControl,kControlEntireControl,kControlUserPaneKeyDownProcTag,
538 sizeof(gControlUserPaneKeyDownUPP),(Ptr) &gControlUserPaneKeyDownUPP);
539 SetControlData((ControlRef) m_macControl,kControlEntireControl,kControlUserPaneActivateProcTag,
540 sizeof(gControlUserPaneActivateUPP),(Ptr) &gControlUserPaneActivateUPP);
541 SetControlData((ControlRef) m_macControl,kControlEntireControl,kControlUserPaneFocusProcTag,
542 sizeof(gControlUserPaneFocusUPP),(Ptr) &gControlUserPaneFocusUPP);
543 SetControlData((ControlRef) m_macControl,kControlEntireControl,kControlUserPaneBackgroundProcTag,
544 sizeof(gControlUserPaneBackgroundUPP),(Ptr) &gControlUserPaneBackgroundUPP);
545 #endif
546 }
547 #ifndef __WXUNIVERSAL__
548 // Don't give scrollbars to wxControls unless they ask for them
549 if ( (! IsKindOf(CLASSINFO(wxControl)) && ! IsKindOf(CLASSINFO(wxStatusBar))) ||
550 (IsKindOf(CLASSINFO(wxControl)) && ( style & wxHSCROLL || style & wxVSCROLL)))
551 {
552 MacCreateScrollBars( style ) ;
553 }
554 #endif
555
556 wxWindowCreateEvent event(this);
557 GetEventHandler()->AddPendingEvent(event);
558
559 return TRUE;
560 }
561
562 void wxWindowMac::MacPostControlCreate(const wxPoint& pos, const wxSize& size)
563 {
564 wxASSERT_MSG( (ControlRef) m_macControl != NULL , wxT("No valid mac control") ) ;
565
566 wxAssociateControlWithMacControl( (ControlRef) m_macControl , this ) ;
567 ::SetControlReference( (ControlRef) m_macControl , (long) this ) ;
568
569 MacInstallEventHandler();
570
571 ControlRef container = (ControlRef) GetParent()->GetHandle() ;
572 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
573 ::EmbedControl( (ControlRef) m_macControl , container ) ;
574
575 // adjust font, controlsize etc
576 DoSetWindowVariant( m_windowVariant ) ;
577
578 #if !TARGET_API_MAC_OSX
579 // eventually we can fix some clipping issues be reactivating this hook
580 //if ( m_macIsUserPane )
581 // SetControlColorProc( (ControlRef) m_macControl , wxMacSetupControlBackgroundUPP ) ;
582 #endif
583
584 UMASetControlTitle( (ControlRef) m_macControl , wxStripMenuCodes(m_label) , m_font.GetEncoding() ) ;
585
586 wxSize new_size = size ;
587 if (!m_macIsUserPane)
588 {
589 wxSize best_size( DoGetBestSize() );
590
591 if (size.x == -1) {
592 new_size.x = best_size.x;
593 }
594 if (size.y == -1) {
595 new_size.y = best_size.y;
596 }
597 SetSize( pos.x, pos.y , new_size.x, new_size.y,wxSIZE_USE_EXISTING );
598 }
599
600 SetCursor( *wxSTANDARD_CURSOR ) ;
601
602 }
603
604 void wxWindowMac::DoSetWindowVariant( wxWindowVariant variant )
605 {
606 wxASSERT( m_macControl != NULL ) ;
607
608 m_windowVariant = variant ;
609
610 ControlSize size ;
611 ThemeFontID themeFont = kThemeSystemFont ;
612
613 // we will get that from the settings later
614 // and make this NORMAL later, but first
615 // we have a few calculations that we must fix
616
617 switch ( variant )
618 {
619 case wxWINDOW_VARIANT_NORMAL :
620 size = kControlSizeNormal;
621 themeFont = kThemeSystemFont ;
622 break ;
623 case wxWINDOW_VARIANT_SMALL :
624 size = kControlSizeSmall;
625 themeFont = kThemeSmallSystemFont ;
626 break ;
627 case wxWINDOW_VARIANT_MINI :
628 if (UMAGetSystemVersion() >= 0x1030 )
629 {
630 // not always defined in the headers
631 size = 3 ;
632 themeFont = 109 ;
633 }
634 else
635 {
636 size = kControlSizeSmall;
637 themeFont = kThemeSmallSystemFont ;
638 }
639 break ;
640 case wxWINDOW_VARIANT_LARGE :
641 size = kControlSizeLarge;
642 themeFont = kThemeSystemFont ;
643 break ;
644 default:
645 wxFAIL_MSG(_T("unexpected window variant"));
646 break ;
647 }
648 ::SetControlData( (ControlRef) m_macControl , kControlEntireControl, kControlSizeTag, sizeof( ControlSize ), &size );
649
650 wxFont font ;
651 font.MacCreateThemeFont( themeFont ) ;
652 SetFont( font ) ;
653 }
654
655 void wxWindowMac::MacUpdateControlFont()
656 {
657 ControlFontStyleRec fontStyle;
658 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
659 {
660 switch( m_font.MacGetThemeFontID() )
661 {
662 case kThemeSmallSystemFont : fontStyle.font = kControlFontSmallSystemFont ; break ;
663 case 109 /*mini font */ : fontStyle.font = -5 ; break ;
664 case kThemeSystemFont : fontStyle.font = kControlFontBigSystemFont ; break ;
665 default : fontStyle.font = kControlFontBigSystemFont ; break ;
666 }
667 fontStyle.flags = kControlUseFontMask ;
668 }
669 else
670 {
671 fontStyle.font = m_font.MacGetFontNum() ;
672 fontStyle.style = m_font.MacGetFontStyle() ;
673 fontStyle.size = m_font.MacGetFontSize() ;
674 fontStyle.flags = kControlUseFontMask | kControlUseFaceMask | kControlUseSizeMask ;
675 }
676 fontStyle.foreColor = MAC_WXCOLORREF(GetForegroundColour().GetPixel() ) ;
677 fontStyle.flags |= kControlUseForeColorMask ;
678 ::SetControlFontStyle( (ControlRef) m_macControl , &fontStyle );
679 Refresh() ;
680 }
681
682 bool wxWindowMac::SetFont(const wxFont& font)
683 {
684 bool retval = !wxWindowBase::SetFont( font ) ;
685
686 MacUpdateControlFont() ;
687
688 return retval;
689 }
690
691 bool wxWindowMac::SetForegroundColour(const wxColour& col )
692 {
693 if ( !wxWindowBase::SetForegroundColour(col) )
694 return false ;
695
696 MacUpdateControlFont() ;
697
698 return true ;
699 }
700
701 bool wxWindowMac::SetBackgroundColour(const wxColour& col )
702 {
703 if ( !wxWindowBase::SetBackgroundColour(col) && m_hasBgCol )
704 return false ;
705
706 wxBrush brush ;
707 if ( col == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE) )
708 {
709 brush.MacSetTheme( kThemeBrushDocumentWindowBackground ) ;
710 }
711 else if ( col == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE ) )
712 {
713 brush.MacSetTheme( kThemeBrushDialogBackgroundActive ) ;
714 }
715 else
716 {
717 brush.SetColour( col ) ;
718 }
719 MacSetBackgroundBrush( brush ) ;
720
721 MacUpdateControlFont() ;
722
723 return true ;
724 }
725
726
727 bool wxWindowMac::MacCanFocus() const
728 {
729 wxASSERT( m_macControl != NULL ) ;
730
731 return true ;
732 }
733
734
735 void wxWindowMac::SetFocus()
736 {
737 if ( gFocusWindow == this )
738 return ;
739
740 if ( AcceptsFocus() )
741 {
742 if (gFocusWindow )
743 {
744 #if wxUSE_CARET
745 // Deal with caret
746 if ( gFocusWindow->m_caret )
747 {
748 gFocusWindow->m_caret->OnKillFocus();
749 }
750 #endif // wxUSE_CARET
751 #ifndef __WXUNIVERSAL__
752 wxWindow* control = wxDynamicCast( gFocusWindow , wxWindow ) ;
753 // TODO we must use the built-in focusing
754 if ( control && control->GetHandle() /* && control->MacIsReallyShown() */ )
755 {
756 UMASetKeyboardFocus( (WindowRef) gFocusWindow->MacGetTopLevelWindowRef() , (ControlRef) control->GetHandle() , kControlFocusNoPart ) ;
757 control->MacRedrawControl() ;
758 }
759 #endif
760 // Without testing the window id, for some reason
761 // a kill focus event can still be sent to
762 // the control just being focussed.
763 int thisId = this->m_windowId;
764 int gFocusWindowId = gFocusWindow->m_windowId;
765 if (gFocusWindowId != thisId)
766 {
767 wxFocusEvent event(wxEVT_KILL_FOCUS, gFocusWindow->m_windowId);
768 event.SetEventObject(gFocusWindow);
769 gFocusWindow->GetEventHandler()->ProcessEvent(event) ;
770 }
771 }
772 gFocusWindow = this ;
773 {
774 #if wxUSE_CARET
775 // Deal with caret
776 if ( m_caret )
777 {
778 m_caret->OnSetFocus();
779 }
780 #endif // wxUSE_CARET
781 // panel wants to track the window which was the last to have focus in it
782 wxChildFocusEvent eventFocus(this);
783 GetEventHandler()->ProcessEvent(eventFocus);
784
785 #ifndef __WXUNIVERSAL__
786 wxControl* control = wxDynamicCast( gFocusWindow , wxControl ) ;
787 if ( control && control->GetHandle() )
788 {
789 UMASetKeyboardFocus( (WindowRef) gFocusWindow->MacGetTopLevelWindowRef() , (ControlRef) control->GetHandle() , kControlFocusNextPart ) ;
790 }
791 #endif
792 wxFocusEvent event(wxEVT_SET_FOCUS, m_windowId);
793 event.SetEventObject(this);
794 GetEventHandler()->ProcessEvent(event) ;
795 }
796 }
797 }
798
799
800 void wxWindowMac::DoCaptureMouse()
801 {
802 wxTheApp->s_captureWindow = this ;
803 }
804
805 wxWindow* wxWindowBase::GetCapture()
806 {
807 return wxTheApp->s_captureWindow ;
808 }
809
810 void wxWindowMac::DoReleaseMouse()
811 {
812 wxTheApp->s_captureWindow = NULL ;
813 }
814
815 #if wxUSE_DRAG_AND_DROP
816
817 void wxWindowMac::SetDropTarget(wxDropTarget *pDropTarget)
818 {
819 if ( m_dropTarget != 0 ) {
820 delete m_dropTarget;
821 }
822
823 m_dropTarget = pDropTarget;
824 if ( m_dropTarget != 0 )
825 {
826 // TODO
827 }
828 }
829
830 #endif
831
832 // Old style file-manager drag&drop
833 void wxWindowMac::DragAcceptFiles(bool accept)
834 {
835 // TODO
836 }
837
838 void wxWindowMac::MacGetPositionAndSizeFromControl(int& x, int& y,
839 int& w, int& h) const
840 {
841 Rect bounds ;
842 GetControlBounds( (ControlRef) m_macControl , &bounds ) ;
843
844
845 x = bounds.left ;
846 y = bounds.top ;
847 w = bounds.right - bounds.left ;
848 h = bounds.bottom - bounds.top ;
849
850 wxTopLevelWindow* tlw = wxDynamicCast( this , wxTopLevelWindow ) ;
851 if ( tlw )
852 {
853 Point tlworigin = { 0 , 0 } ;
854 GrafPtr port ;
855 ::GetPort( &port ) ;
856 ::SetPort( UMAGetWindowPort( (WindowRef) tlw->MacGetWindowRef() ) ) ;
857 ::LocalToGlobal( &tlworigin ) ;
858 ::SetPort( port ) ;
859 x = tlworigin.h ;
860 y = tlworigin.v ;
861 }
862 }
863
864 bool wxWindowMac::MacGetBoundsForControl(const wxPoint& pos,
865 const wxSize& size,
866 int& x, int& y,
867 int& w, int& h) const
868 {
869 x = (int)pos.x;
870 y = (int)pos.y;
871 // todo the default calls may be used as soon as PostCreateControl Is moved here
872 w = size.x ; // WidthDefault( size.x );
873 h = size.y ; // HeightDefault( size.y ) ;
874 #if !TARGET_API_MAC_OSX
875 GetParent()->MacWindowToRootWindow( &x , &y ) ;
876 #endif
877
878 return true ;
879 }
880
881 // Get total size
882 void wxWindowMac::DoGetSize(int *x, int *y) const
883 {
884 #if TARGET_API_MAC_OSX
885 int x1 , y1 , w1 ,h1 ;
886 MacGetPositionAndSizeFromControl( x1 , y1, w1 ,h1 ) ;
887 if(x) *x = w1 ;
888 if(y) *y = h1 ;
889
890 #else
891 Rect bounds ;
892 GetControlBounds( (ControlRef) m_macControl , &bounds ) ;
893 if(x) *x = bounds.right - bounds.left ;
894 if(y) *y = bounds.bottom - bounds.top ;
895 #endif
896 }
897
898 void wxWindowMac::DoGetPosition(int *x, int *y) const
899 {
900 #if TARGET_API_MAC_OSX
901 int x1 , y1 , w1 ,h1 ;
902 MacGetPositionAndSizeFromControl( x1 , y1, w1 ,h1 ) ;
903 if ( !IsTopLevel() )
904 {
905 wxWindow *parent = GetParent();
906 if ( parent )
907 {
908 wxPoint pt(parent->GetClientAreaOrigin());
909 x1 -= pt.x ;
910 y1 -= pt.y ;
911 }
912 }
913 if(x) *x = x1 ;
914 if(y) *y = y1 ;
915 #else
916 Rect bounds ;
917 GetControlBounds( (ControlRef) m_macControl , &bounds ) ;
918 wxCHECK_RET( GetParent() , wxT("Missing Parent") ) ;
919
920 int xx = bounds.left ;
921 int yy = bounds.top ;
922
923 if ( !GetParent()->IsTopLevel() )
924 {
925 GetControlBounds( (ControlRef) GetParent()->GetHandle() , &bounds ) ;
926
927 xx -= bounds.left ;
928 yy -= bounds.top ;
929 }
930
931 wxPoint pt(GetParent()->GetClientAreaOrigin());
932 xx -= pt.x;
933 yy -= pt.y;
934
935 if(x) *x = xx;
936 if(y) *y = yy;
937 #endif
938 }
939
940 void wxWindowMac::DoScreenToClient(int *x, int *y) const
941 {
942 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
943
944 wxCHECK_RET( window , wxT("TopLevel Window Missing") ) ;
945
946 {
947 Point localwhere = {0,0} ;
948
949 if(x) localwhere.h = * x ;
950 if(y) localwhere.v = * y ;
951
952 wxMacPortSaver s((GrafPtr)GetWindowPort( window )) ;
953 ::GlobalToLocal( &localwhere ) ;
954 if(x) *x = localwhere.h ;
955 if(y) *y = localwhere.v ;
956
957 }
958 MacRootWindowToWindow( x , y ) ;
959
960 wxPoint origin = GetClientAreaOrigin() ;
961 if(x) *x -= origin.x ;
962 if(y) *y -= origin.y ;
963 }
964
965 void wxWindowMac::DoClientToScreen(int *x, int *y) const
966 {
967 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
968 wxCHECK_RET( window , wxT("TopLevel Window Missing") ) ;
969
970 wxPoint origin = GetClientAreaOrigin() ;
971 if(x) *x += origin.x ;
972 if(y) *y += origin.y ;
973
974 MacWindowToRootWindow( x , y ) ;
975
976 {
977 Point localwhere = { 0,0 };
978 if(x) localwhere.h = * x ;
979 if(y) localwhere.v = * y ;
980
981 wxMacPortSaver s((GrafPtr)GetWindowPort( window )) ;
982 ::LocalToGlobal( &localwhere ) ;
983 if(x) *x = localwhere.h ;
984 if(y) *y = localwhere.v ;
985 }
986 }
987
988 void wxWindowMac::MacClientToRootWindow( int *x , int *y ) const
989 {
990 wxPoint origin = GetClientAreaOrigin() ;
991 if(x) *x += origin.x ;
992 if(y) *y += origin.y ;
993
994 MacWindowToRootWindow( x , y ) ;
995 }
996
997 void wxWindowMac::MacRootWindowToClient( int *x , int *y ) const
998 {
999 MacRootWindowToWindow( x , y ) ;
1000
1001 wxPoint origin = GetClientAreaOrigin() ;
1002 if(x) *x -= origin.x ;
1003 if(y) *y -= origin.y ;
1004 }
1005
1006 void wxWindowMac::MacWindowToRootWindow( int *x , int *y ) const
1007 {
1008 #if TARGET_API_MAC_OSX
1009 HIPoint pt ;
1010 if ( x ) pt.x = *x ;
1011 if ( y ) pt.y = *y ;
1012
1013 HIViewConvertPoint( &pt , (ControlRef) m_macControl , (ControlRef) MacGetTopLevelWindow()->GetHandle() ) ;
1014
1015 if ( x ) *x = (int) pt.x ;
1016 if ( y ) *y = (int) pt.y ;
1017 #else
1018 if ( !IsTopLevel() )
1019 {
1020 Rect bounds ;
1021 GetControlBounds( (ControlRef) m_macControl , &bounds ) ;
1022 if(x) *x += bounds.left ;
1023 if(y) *y += bounds.top ;
1024 }
1025 #endif
1026 }
1027
1028 void wxWindowMac::MacWindowToRootWindow( short *x , short *y ) const
1029 {
1030 int x1 , y1 ;
1031 if ( x ) x1 = *x ;
1032 if ( y ) y1 = *y ;
1033 MacWindowToRootWindow( &x1 , &y1 ) ;
1034 if ( x ) *x = x1 ;
1035 if ( y ) *y = y1 ;
1036 }
1037
1038 void wxWindowMac::MacRootWindowToWindow( int *x , int *y ) const
1039 {
1040 #if TARGET_API_MAC_OSX
1041 HIPoint pt ;
1042 if ( x ) pt.x = *x ;
1043 if ( y ) pt.y = *y ;
1044
1045 HIViewConvertPoint( &pt , (ControlRef) MacGetTopLevelWindow()->GetHandle() , (ControlRef) m_macControl ) ;
1046
1047 if ( x ) *x = (int) pt.x ;
1048 if ( y ) *y = (int) pt.y ;
1049 #else
1050 if ( !IsTopLevel() )
1051 {
1052 Rect bounds ;
1053 GetControlBounds( (ControlRef) m_macControl , &bounds ) ;
1054 if(x) *x -= bounds.left ;
1055 if(y) *y -= bounds.top ;
1056 }
1057 #endif
1058 }
1059
1060 void wxWindowMac::MacRootWindowToWindow( short *x , short *y ) const
1061 {
1062 int x1 , y1 ;
1063 if ( x ) x1 = *x ;
1064 if ( y ) y1 = *y ;
1065 MacRootWindowToWindow( &x1 , &y1 ) ;
1066 if ( x ) *x = x1 ;
1067 if ( y ) *y = y1 ;
1068 }
1069
1070 wxSize wxWindowMac::DoGetSizeFromClientSize( const wxSize & size ) const
1071 {
1072 wxSize sizeTotal = size;
1073
1074 RgnHandle rgn = NewRgn() ;
1075
1076 Rect content ;
1077
1078 if ( GetControlRegion( (ControlRef) m_macControl , kControlContentMetaPart , rgn ) == noErr )
1079 {
1080 GetRegionBounds( rgn , &content ) ;
1081 DisposeRgn( rgn ) ;
1082 }
1083 else
1084 {
1085 GetControlBounds( (ControlRef) m_macControl , &content ) ;
1086 }
1087 Rect structure ;
1088 GetControlBounds( (ControlRef) m_macControl , &structure ) ;
1089 #if !TARGET_API_MAC_OSX
1090 OffsetRect( &content , -structure.left , -structure.top ) ;
1091 #endif
1092
1093 sizeTotal.x += (structure.right - structure.left) - (content.right - content.left) ;
1094 sizeTotal.y += (structure.bottom - structure.top) - (content.bottom - content.top ) ;
1095
1096 sizeTotal.x += MacGetLeftBorderSize( ) + MacGetRightBorderSize( ) ;
1097 sizeTotal.y += MacGetTopBorderSize( ) + MacGetBottomBorderSize( ) ;
1098
1099 return sizeTotal;
1100 }
1101
1102
1103 // Get size *available for subwindows* i.e. excluding menu bar etc.
1104 void wxWindowMac::DoGetClientSize(int *x, int *y) const
1105 {
1106 int ww, hh;
1107
1108 RgnHandle rgn = NewRgn() ;
1109 Rect content ;
1110 if ( GetControlRegion( (ControlRef) m_macControl , kControlContentMetaPart , rgn ) == noErr )
1111 {
1112 GetRegionBounds( rgn , &content ) ;
1113 DisposeRgn( rgn ) ;
1114 }
1115 else
1116 {
1117 GetControlBounds( (ControlRef) m_macControl , &content ) ;
1118 }
1119 #if !TARGET_API_MAC_OSX
1120 Rect structure ;
1121 GetControlBounds( (ControlRef) m_macControl , &structure ) ;
1122 OffsetRect( &content , -structure.left , -structure.top ) ;
1123 #endif
1124 ww = content.right - content.left ;
1125 hh = content.bottom - content.top ;
1126
1127 ww -= MacGetLeftBorderSize( ) + MacGetRightBorderSize( ) ;
1128 hh -= MacGetTopBorderSize( ) + MacGetBottomBorderSize( );
1129
1130 if ( (m_vScrollBar && m_vScrollBar->IsShown()) || (m_hScrollBar && m_hScrollBar->IsShown()) )
1131 {
1132 int x1 = 0 ;
1133 int y1 = 0 ;
1134 int w ;
1135 int h ;
1136 GetSize( &w , &h ) ;
1137
1138 MacClientToRootWindow( &x1 , &y1 ) ;
1139 MacClientToRootWindow( &w , &h ) ;
1140
1141 wxWindowMac *iter = (wxWindowMac*)this ;
1142
1143 int totW = 10000 , totH = 10000;
1144 while( iter )
1145 {
1146 if ( iter->IsTopLevel() )
1147 {
1148 iter->GetSize( &totW , &totH ) ;
1149 break ;
1150 }
1151
1152 iter = iter->GetParent() ;
1153 }
1154
1155 if (m_hScrollBar && m_hScrollBar->IsShown() )
1156 {
1157 hh -= MAC_SCROLLBAR_SIZE;
1158 if ( h-y1 >= totH )
1159 {
1160 hh += 1 ;
1161 }
1162 }
1163 if (m_vScrollBar && m_vScrollBar->IsShown() )
1164 {
1165 ww -= MAC_SCROLLBAR_SIZE;
1166 if ( w-x1 >= totW )
1167 {
1168 ww += 1 ;
1169 }
1170 }
1171 }
1172 if(x) *x = ww;
1173 if(y) *y = hh;
1174
1175 }
1176
1177 bool wxWindowMac::SetCursor(const wxCursor& cursor)
1178 {
1179 if (m_cursor == cursor)
1180 return FALSE;
1181
1182 if (wxNullCursor == cursor)
1183 {
1184 if ( ! wxWindowBase::SetCursor( *wxSTANDARD_CURSOR ) )
1185 return FALSE ;
1186 }
1187 else
1188 {
1189 if ( ! wxWindowBase::SetCursor( cursor ) )
1190 return FALSE ;
1191 }
1192
1193 wxASSERT_MSG( m_cursor.Ok(),
1194 wxT("cursor must be valid after call to the base version"));
1195
1196 /*
1197
1198 TODO why do we have to use current coordinates ?
1199
1200 Point pt ;
1201 wxWindowMac *mouseWin ;
1202 GetMouse( &pt ) ;
1203
1204 // Change the cursor NOW if we're within the correct window
1205
1206
1207 if ( MacGetWindowFromPoint( wxPoint( pt.h , pt.v ) , &mouseWin ) )
1208 {
1209 if ( mouseWin == this && !wxIsBusy() )
1210 {
1211 m_cursor.MacInstall() ;
1212 }
1213 }
1214 */
1215 if ( !wxIsBusy() )
1216 {
1217 m_cursor.MacInstall() ;
1218 }
1219
1220 return TRUE ;
1221 }
1222
1223 #if wxUSE_MENUS
1224 bool wxWindowMac::DoPopupMenu(wxMenu *menu, int x, int y)
1225 {
1226 menu->SetInvokingWindow(this);
1227 menu->UpdateUI();
1228 ClientToScreen( &x , &y ) ;
1229
1230 menu->MacBeforeDisplay( true ) ;
1231 long menuResult = ::PopUpMenuSelect((MenuHandle) menu->GetHMenu() ,y,x, 0) ;
1232 if ( HiWord(menuResult) != 0 )
1233 {
1234 MenuCommand id ;
1235 GetMenuItemCommandID( GetMenuHandle(HiWord(menuResult)) , LoWord(menuResult) , &id ) ;
1236 wxMenuItem* item = NULL ;
1237 wxMenu* realmenu ;
1238 item = menu->FindItem(id, &realmenu) ;
1239 if (item->IsCheckable())
1240 {
1241 item->Check( !item->IsChecked() ) ;
1242 }
1243 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
1244 }
1245 menu->MacAfterDisplay( true ) ;
1246
1247 menu->SetInvokingWindow(NULL);
1248
1249 return TRUE;
1250 }
1251 #endif
1252
1253 // ----------------------------------------------------------------------------
1254 // tooltips
1255 // ----------------------------------------------------------------------------
1256
1257 #if wxUSE_TOOLTIPS
1258
1259 void wxWindowMac::DoSetToolTip(wxToolTip *tooltip)
1260 {
1261 wxWindowBase::DoSetToolTip(tooltip);
1262
1263 if ( m_tooltip )
1264 m_tooltip->SetWindow(this);
1265 }
1266
1267 #endif // wxUSE_TOOLTIPS
1268
1269 void wxWindowMac::DoMoveWindow(int x, int y, int width, int height)
1270 {
1271 int former_x , former_y , former_w, former_h ;
1272 #if !TARGET_API_MAC_OSX
1273 DoGetPosition( &former_x , &former_y ) ;
1274 DoGetSize( &former_w , &former_h ) ;
1275 #else
1276 MacGetPositionAndSizeFromControl( former_x , former_y , former_w , former_h ) ;
1277 #endif
1278
1279 int actualWidth = width;
1280 int actualHeight = height;
1281 int actualX = x;
1282 int actualY = y;
1283
1284 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
1285 actualWidth = m_minWidth;
1286 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
1287 actualHeight = m_minHeight;
1288 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
1289 actualWidth = m_maxWidth;
1290 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
1291 actualHeight = m_maxHeight;
1292
1293 bool doMove = false ;
1294 bool doResize = false ;
1295
1296 if ( actualX != former_x || actualY != former_y )
1297 {
1298 doMove = true ;
1299 }
1300 if ( actualWidth != former_w || actualHeight != former_h )
1301 {
1302 doResize = true ;
1303 }
1304
1305 if ( doMove || doResize )
1306 {
1307 Rect r = wxMacGetBoundsForControl(this , wxPoint( actualX,actualY), wxSize( actualWidth, actualHeight ) ) ;
1308 #if TARGET_API_MAC_OSX
1309 SetControlBounds( (ControlRef) m_macControl , &r ) ;
1310 #else
1311 if ( doMove )
1312 MoveControl( (ControlRef) m_macControl , r.left , r.top ) ;
1313 if ( doSize )
1314 SizeControl( (ControlRef) m_macControl , r.right-r.left , r.bottom-r.top ) ;
1315 #endif
1316 MacRepositionScrollBars() ;
1317 if ( doMove )
1318 {
1319 wxPoint point(actualX,actualY);
1320 wxMoveEvent event(point, m_windowId);
1321 event.SetEventObject(this);
1322 GetEventHandler()->ProcessEvent(event) ;
1323 }
1324 if ( doResize )
1325 {
1326 MacRepositionScrollBars() ;
1327 wxSize size(actualWidth, actualHeight);
1328 wxSizeEvent event(size, m_windowId);
1329 event.SetEventObject(this);
1330 GetEventHandler()->ProcessEvent(event);
1331 }
1332 }
1333
1334 }
1335
1336 wxSize wxWindowMac::DoGetBestSize() const
1337 {
1338 Rect bestsize = { 0 , 0 , 0 , 0 } ;
1339 short baselineoffset ;
1340 int bestWidth, bestHeight ;
1341 ::GetBestControlRect( (ControlRef) m_macControl , &bestsize , &baselineoffset ) ;
1342
1343 if ( EmptyRect( &bestsize ) )
1344 {
1345 baselineoffset = 0;
1346 bestsize.left = bestsize.top = 0 ;
1347 bestsize.right = 16 ;
1348 bestsize.bottom = 16 ;
1349 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
1350 {
1351 bestsize.bottom = 16 ;
1352 }
1353 else if ( IsKindOf( CLASSINFO( wxSpinButton ) ) )
1354 {
1355 bestsize.bottom = 24 ;
1356 }
1357 else
1358 {
1359 // return wxWindowBase::DoGetBestSize() ;
1360 }
1361 }
1362
1363 bestWidth = bestsize.right - bestsize.left ;
1364 bestHeight = bestsize.bottom - bestsize.top ;
1365 if ( bestHeight < 10 )
1366 bestHeight = 13 ;
1367
1368 return wxSize(bestWidth, bestHeight);
1369 }
1370
1371
1372 // set the size of the window: if the dimensions are positive, just use them,
1373 // but if any of them is equal to -1, it means that we must find the value for
1374 // it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
1375 // which case -1 is a valid value for x and y)
1376 //
1377 // If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
1378 // the width/height to best suit our contents, otherwise we reuse the current
1379 // width/height
1380 void wxWindowMac::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1381 {
1382 // get the current size and position...
1383 int currentX, currentY;
1384 GetPosition(&currentX, &currentY);
1385
1386 int currentW,currentH;
1387 GetSize(&currentW, &currentH);
1388
1389 // ... and don't do anything (avoiding flicker) if it's already ok
1390 if ( x == currentX && y == currentY &&
1391 width == currentW && height == currentH )
1392 {
1393 // TODO REMOVE
1394 MacRepositionScrollBars() ; // we might have a real position shift
1395 return;
1396 }
1397
1398 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1399 x = currentX;
1400 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1401 y = currentY;
1402
1403 AdjustForParentClientOrigin(x, y, sizeFlags);
1404
1405 wxSize size(-1, -1);
1406 if ( width == -1 )
1407 {
1408 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
1409 {
1410 size = DoGetBestSize();
1411 width = size.x;
1412 }
1413 else
1414 {
1415 // just take the current one
1416 width = currentW;
1417 }
1418 }
1419
1420 if ( height == -1 )
1421 {
1422 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
1423 {
1424 if ( size.x == -1 )
1425 {
1426 size = DoGetBestSize();
1427 }
1428 //else: already called DoGetBestSize() above
1429
1430 height = size.y;
1431 }
1432 else
1433 {
1434 // just take the current one
1435 height = currentH;
1436 }
1437 }
1438
1439 DoMoveWindow(x, y, width, height);
1440
1441 }
1442
1443 wxPoint wxWindowMac::GetClientAreaOrigin() const
1444 {
1445 RgnHandle rgn = NewRgn() ;
1446 Rect content ;
1447 GetControlRegion( (ControlRef) m_macControl , kControlContentMetaPart , rgn ) ;
1448 GetRegionBounds( rgn , &content ) ;
1449 DisposeRgn( rgn ) ;
1450 #if !TARGET_API_MAC_OSX
1451 Rect structure ;
1452 GetControlBounds( (ControlRef) m_macControl , &structure ) ;
1453 OffsetRect( &content , -structure.left , -structure.top ) ;
1454 #endif
1455
1456 return wxPoint( content.left + MacGetLeftBorderSize( ) , content.top + MacGetTopBorderSize( ) );
1457 }
1458
1459 void wxWindowMac::DoSetClientSize(int clientwidth, int clientheight)
1460 {
1461 if ( clientheight != -1 || clientheight != -1 )
1462 {
1463 int currentclientwidth , currentclientheight ;
1464 int currentwidth , currentheight ;
1465
1466 GetClientSize( &currentclientwidth , &currentclientheight ) ;
1467 GetSize( &currentwidth , &currentheight ) ;
1468
1469 DoSetSize( -1 , -1 , currentwidth + clientwidth - currentclientwidth ,
1470 currentheight + clientheight - currentclientheight , wxSIZE_USE_EXISTING ) ;
1471 }
1472 }
1473
1474 void wxWindowMac::SetTitle(const wxString& title)
1475 {
1476 m_label = wxStripMenuCodes(title) ;
1477
1478 if ( m_macControl )
1479 {
1480 UMASetControlTitle( (ControlRef) m_macControl , m_label , m_font.GetEncoding() ) ;
1481 }
1482 Refresh() ;
1483 }
1484
1485 wxString wxWindowMac::GetTitle() const
1486 {
1487 return m_label ;
1488 }
1489
1490 void wxWindowMac::MacPropagateVisibilityChanged()
1491 {
1492 MacVisibilityChanged() ;
1493
1494 wxWindowListNode *node = GetChildren().GetFirst();
1495 while ( node )
1496 {
1497 wxWindowMac *child = node->GetData();
1498 if ( child->IsShown() )
1499 child->MacPropagateVisibilityChanged( ) ;
1500 node = node->GetNext();
1501 }
1502 }
1503
1504 bool wxWindowMac::Show(bool show)
1505 {
1506 if ( !wxWindowBase::Show(show) )
1507 return FALSE;
1508
1509 // TODO use visibilityChanged Carbon Event for OSX
1510 bool former = MacIsReallyShown() ;
1511
1512 SetControlVisibility( (ControlRef) m_macControl , show , true ) ;
1513 if ( former != MacIsReallyShown() )
1514 MacPropagateVisibilityChanged() ;
1515 return TRUE;
1516 }
1517
1518 bool wxWindowMac::MacIsReallyShown()
1519 {
1520 // only under OSX the visibility of the TLW is taken into account
1521 #if TARGET_API_MAC_OSX
1522 return IsControlVisible( (ControlRef) m_macControl ) ;
1523 #else
1524 wxWindow* win = this ;
1525 while( win->IsShown() )
1526 {
1527 if ( win->IsTopLevel() )
1528 return true ;
1529
1530 win = win->GetParent() ;
1531 if ( win == NULL )
1532 return true ;
1533
1534 } ;
1535 return false ;
1536 #endif
1537 }
1538
1539 void wxWindowMac::MacVisibilityChanged()
1540 {
1541 }
1542
1543 void wxWindowMac::MacPropagateEnabledStateChanged( )
1544 {
1545 MacEnabledStateChanged() ;
1546
1547 wxWindowListNode *node = GetChildren().GetFirst();
1548 while ( node )
1549 {
1550 wxWindowMac *child = node->GetData();
1551 if ( child->IsEnabled() )
1552 child->MacPropagateEnabledStateChanged() ;
1553 node = node->GetNext();
1554 }
1555 }
1556
1557 bool wxWindowMac::Enable(bool enable)
1558 {
1559 wxASSERT( m_macControl != NULL ) ;
1560 if ( !wxWindowBase::Enable(enable) )
1561 return FALSE;
1562
1563 bool former = MacIsReallyEnabled() ;
1564 if ( enable )
1565 UMAActivateControl( (ControlRef) m_macControl ) ;
1566 else
1567 UMADeactivateControl( (ControlRef) m_macControl ) ;
1568
1569 if ( former != MacIsReallyEnabled() )
1570 MacPropagateEnabledStateChanged() ;
1571 return TRUE;
1572 }
1573
1574 bool wxWindowMac::MacIsReallyEnabled()
1575 {
1576 return IsControlEnabled( (ControlRef) m_macControl ) ;
1577 }
1578
1579 void wxWindowMac::MacEnabledStateChanged()
1580 {
1581 }
1582
1583 int wxWindowMac::GetCharHeight() const
1584 {
1585 wxClientDC dc ( (wxWindowMac*)this ) ;
1586 return dc.GetCharHeight() ;
1587 }
1588
1589 int wxWindowMac::GetCharWidth() const
1590 {
1591 wxClientDC dc ( (wxWindowMac*)this ) ;
1592 return dc.GetCharWidth() ;
1593 }
1594
1595 void wxWindowMac::GetTextExtent(const wxString& string, int *x, int *y,
1596 int *descent, int *externalLeading, const wxFont *theFont ) const
1597 {
1598 const wxFont *fontToUse = theFont;
1599 if ( !fontToUse )
1600 fontToUse = &m_font;
1601
1602 wxClientDC dc( (wxWindowMac*) this ) ;
1603 long lx,ly,ld,le ;
1604 dc.GetTextExtent( string , &lx , &ly , &ld, &le, (wxFont *)fontToUse ) ;
1605 if ( externalLeading )
1606 *externalLeading = le ;
1607 if ( descent )
1608 *descent = ld ;
1609 if ( x )
1610 *x = lx ;
1611 if ( y )
1612 *y = ly ;
1613 }
1614
1615 /*
1616 * Rect is given in client coordinates, for further reading, read wxTopLevelWindowMac::InvalidateRect
1617 * we always intersect with the entire window, not only with the client area
1618 */
1619
1620 void wxWindowMac::Refresh(bool eraseBack, const wxRect *rect)
1621 {
1622 #if TARGET_API_MAC_OSX
1623 HIViewSetNeedsDisplay( (ControlRef) m_macControl , true ) ;
1624 #else
1625 if ( IsControlVisible( (ControlRef) m_macControl ) )
1626 {
1627 SetControlVisibility( (ControlRef) m_macControl , false , false ) ;
1628 SetControlVisibility( (ControlRef) m_macControl , true , true ) ;
1629 }
1630 /*
1631 if ( MacGetTopLevelWindow() == NULL )
1632 return ;
1633
1634 if ( !IsControlVisible( (ControlRef) m_macControl ) )
1635 return ;
1636
1637 wxPoint client = GetClientAreaOrigin();
1638 int x1 = -client.x;
1639 int y1 = -client.y;
1640 int x2 = m_width - client.x;
1641 int y2 = m_height - client.y;
1642
1643 if (IsKindOf( CLASSINFO(wxButton)))
1644 {
1645 // buttons have an "aura"
1646 y1 -= 5;
1647 x1 -= 5;
1648 y2 += 5;
1649 x2 += 5;
1650 }
1651
1652 Rect clientrect = { y1, x1, y2, x2 };
1653
1654 if ( rect )
1655 {
1656 Rect r = { rect->y , rect->x , rect->y + rect->height , rect->x + rect->width } ;
1657 SectRect( &clientrect , &r , &clientrect ) ;
1658 }
1659
1660 if ( !EmptyRect( &clientrect ) )
1661 {
1662 int top = 0 , left = 0 ;
1663
1664 MacClientToRootWindow( &left , &top ) ;
1665 OffsetRect( &clientrect , left , top ) ;
1666
1667 MacGetTopLevelWindow()->MacInvalidate( &clientrect , eraseBack ) ;
1668 }
1669 */
1670 #endif
1671 }
1672
1673 void wxWindowMac::MacRedrawControl()
1674 {
1675 /*
1676 if ( (ControlRef) m_macControl && MacGetTopLevelWindowRef() && IsControlVisible( (ControlRef) m_macControl ) )
1677 {
1678 #if TARGET_API_MAC_CARBON
1679 Update() ;
1680 #else
1681 wxClientDC dc(this) ;
1682 wxMacPortSetter helper(&dc) ;
1683 wxMacWindowClipper clipper(this) ;
1684 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
1685 UMADrawControl( (ControlRef) m_macControl ) ;
1686 #endif
1687 }
1688 */
1689 }
1690
1691 /* TODO
1692 void wxWindowMac::OnPaint(wxPaintEvent& event)
1693 {
1694 // why don't we skip that here ?
1695 }
1696 */
1697
1698 wxWindowMac *wxGetActiveWindow()
1699 {
1700 // actually this is a windows-only concept
1701 return NULL;
1702 }
1703
1704 // Coordinates relative to the window
1705 void wxWindowMac::WarpPointer (int x_pos, int y_pos)
1706 {
1707 // We really don't move the mouse programmatically under Mac.
1708 }
1709
1710 void wxWindowMac::OnEraseBackground(wxEraseEvent& event)
1711 {
1712 event.Skip() ;
1713 /*
1714 if ( m_macBackgroundBrush.Ok() == false || m_macBackgroundBrush.GetStyle() == wxTRANSPARENT )
1715 {
1716 event.Skip() ;
1717 }
1718 else
1719 event.GetDC()->Clear() ;
1720 */
1721 }
1722
1723 void wxWindowMac::OnNcPaint( wxNcPaintEvent& event )
1724 {
1725 wxWindowDC dc(this) ;
1726 wxMacPortSetter helper(&dc) ;
1727
1728 MacPaintBorders( dc.m_macLocalOrigin.x , dc.m_macLocalOrigin.y) ;
1729 }
1730
1731 int wxWindowMac::GetScrollPos(int orient) const
1732 {
1733 if ( orient == wxHORIZONTAL )
1734 {
1735 if ( m_hScrollBar )
1736 return m_hScrollBar->GetThumbPosition() ;
1737 }
1738 else
1739 {
1740 if ( m_vScrollBar )
1741 return m_vScrollBar->GetThumbPosition() ;
1742 }
1743 return 0;
1744 }
1745
1746 // This now returns the whole range, not just the number
1747 // of positions that we can scroll.
1748 int wxWindowMac::GetScrollRange(int orient) const
1749 {
1750 if ( orient == wxHORIZONTAL )
1751 {
1752 if ( m_hScrollBar )
1753 return m_hScrollBar->GetRange() ;
1754 }
1755 else
1756 {
1757 if ( m_vScrollBar )
1758 return m_vScrollBar->GetRange() ;
1759 }
1760 return 0;
1761 }
1762
1763 int wxWindowMac::GetScrollThumb(int orient) const
1764 {
1765 if ( orient == wxHORIZONTAL )
1766 {
1767 if ( m_hScrollBar )
1768 return m_hScrollBar->GetThumbSize() ;
1769 }
1770 else
1771 {
1772 if ( m_vScrollBar )
1773 return m_vScrollBar->GetThumbSize() ;
1774 }
1775 return 0;
1776 }
1777
1778 void wxWindowMac::SetScrollPos(int orient, int pos, bool refresh)
1779 {
1780 if ( orient == wxHORIZONTAL )
1781 {
1782 if ( m_hScrollBar )
1783 m_hScrollBar->SetThumbPosition( pos ) ;
1784 }
1785 else
1786 {
1787 if ( m_vScrollBar )
1788 m_vScrollBar->SetThumbPosition( pos ) ;
1789 }
1790 }
1791
1792 void wxWindowMac::MacPaintBorders( int left , int top )
1793 {
1794 if( IsTopLevel() )
1795 return ;
1796
1797 int major,minor;
1798 wxGetOsVersion( &major, &minor );
1799
1800 RGBColor white = { 0xFFFF, 0xFFFF , 0xFFFF } ;
1801 RGBColor face = { 0xDDDD, 0xDDDD , 0xDDDD } ;
1802
1803 RGBColor darkShadow = { 0x0000, 0x0000 , 0x0000 } ;
1804 RGBColor lightShadow = { 0x4444, 0x4444 , 0x4444 } ;
1805 // OS X has lighter border edges than classic:
1806 if (major >= 10)
1807 {
1808 darkShadow.red = 0x8E8E;
1809 darkShadow.green = 0x8E8E;
1810 darkShadow.blue = 0x8E8E;
1811 lightShadow.red = 0xBDBD;
1812 lightShadow.green = 0xBDBD;
1813 lightShadow.blue = 0xBDBD;
1814 }
1815
1816 PenNormal() ;
1817
1818 int w , h ;
1819 GetSize( &w , &h ) ;
1820 if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
1821 {
1822 #if wxMAC_USE_THEME_BORDER
1823 Rect rect = { top , left , m_height + top , m_width + left } ;
1824 SInt32 border = 0 ;
1825 /*
1826 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
1827 InsetRect( &rect , border , border );
1828 DrawThemeListBoxFrame(&rect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
1829 */
1830
1831 DrawThemePrimaryGroup(&rect ,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
1832 #else
1833 bool sunken = HasFlag( wxSUNKEN_BORDER ) ;
1834 RGBForeColor( &face );
1835 MoveTo( left + 0 , top + h - 2 );
1836 LineTo( left + 0 , top + 0 );
1837 LineTo( left + w - 2 , top + 0 );
1838
1839 MoveTo( left + 2 , top + h - 3 );
1840 LineTo( left + w - 3 , top + h - 3 );
1841 LineTo( left + w - 3 , top + 2 );
1842
1843 RGBForeColor( sunken ? &face : &darkShadow );
1844 MoveTo( left + 0 , top + h - 1 );
1845 LineTo( left + w - 1 , top + h - 1 );
1846 LineTo( left + w - 1 , top + 0 );
1847
1848 RGBForeColor( sunken ? &lightShadow : &white );
1849 MoveTo( left + 1 , top + h - 3 );
1850 LineTo( left + 1, top + 1 );
1851 LineTo( left + w - 3 , top + 1 );
1852
1853 RGBForeColor( sunken ? &white : &lightShadow );
1854 MoveTo( left + 1 , top + h - 2 );
1855 LineTo( left + w - 2 , top + h - 2 );
1856 LineTo( left + w - 2 , top + 1 );
1857
1858 RGBForeColor( sunken ? &darkShadow : &face );
1859 MoveTo( left + 2 , top + h - 4 );
1860 LineTo( left + 2 , top + 2 );
1861 LineTo( left + w - 4 , top + 2 );
1862 #endif
1863 }
1864 else if (HasFlag(wxSIMPLE_BORDER))
1865 {
1866 Rect rect = { top , left , h + top , w + left } ;
1867 RGBForeColor( &darkShadow ) ;
1868 FrameRect( &rect ) ;
1869 }
1870 }
1871
1872 void wxWindowMac::RemoveChild( wxWindowBase *child )
1873 {
1874 if ( child == m_hScrollBar )
1875 m_hScrollBar = NULL ;
1876 if ( child == m_vScrollBar )
1877 m_vScrollBar = NULL ;
1878
1879 wxWindowBase::RemoveChild( child ) ;
1880 }
1881
1882 // New function that will replace some of the above.
1883 void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible,
1884 int range, bool refresh)
1885 {
1886 if ( orient == wxHORIZONTAL )
1887 {
1888 if ( m_hScrollBar )
1889 {
1890 if ( range == 0 || thumbVisible >= range )
1891 {
1892 if ( m_hScrollBar->IsShown() )
1893 m_hScrollBar->Show(false) ;
1894 }
1895 else
1896 {
1897 if ( !m_hScrollBar->IsShown() )
1898 m_hScrollBar->Show(true) ;
1899 m_hScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
1900 }
1901 }
1902 }
1903 else
1904 {
1905 if ( m_vScrollBar )
1906 {
1907 if ( range == 0 || thumbVisible >= range )
1908 {
1909 if ( m_vScrollBar->IsShown() )
1910 m_vScrollBar->Show(false) ;
1911 }
1912 else
1913 {
1914 if ( !m_vScrollBar->IsShown() )
1915 m_vScrollBar->Show(true) ;
1916 m_vScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
1917 }
1918 }
1919 }
1920 MacRepositionScrollBars() ;
1921 }
1922
1923 // Does a physical scroll
1924 void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect)
1925 {
1926 if( dx == 0 && dy ==0 )
1927 return ;
1928
1929
1930 {
1931 wxClientDC dc(this) ;
1932 wxMacPortSetter helper(&dc) ;
1933
1934 int width , height ;
1935 GetClientSize( &width , &height ) ;
1936
1937
1938 wxPoint pos;
1939 pos.x = pos.y = 0;
1940
1941 Rect scrollrect;
1942 // TODO take out the boundaries
1943 GetControlBounds( (ControlRef) m_macControl, &scrollrect);
1944
1945 RgnHandle updateRgn = NewRgn() ;
1946 if ( rect )
1947 {
1948 Rect r = { dc.YLOG2DEVMAC(rect->y) , dc.XLOG2DEVMAC(rect->x) , dc.YLOG2DEVMAC(rect->y + rect->height) ,
1949 dc.XLOG2DEVMAC(rect->x + rect->width) } ;
1950 SectRect( &scrollrect , &r , &scrollrect ) ;
1951 }
1952 ScrollRect( &scrollrect , dx , dy , updateRgn ) ;
1953 #if TARGET_CARBON
1954 //KO: The docs say ScrollRect creates an update region, which thus calls an update event
1955 // but it seems the update only refreshes the background of the control, rather than calling
1956 // kEventControlDraw, so we need to force a proper update here. There has to be a better
1957 // way of doing this... (Note that code below under !TARGET_CARBON does not work either...)
1958 Update();
1959 #endif
1960 // we also have to scroll the update rgn in this rectangle
1961 // in order not to loose updates
1962 #if !TARGET_CARBON
1963 WindowRef rootWindow = (WindowRef) MacGetTopLevelWindowRef() ;
1964 RgnHandle formerUpdateRgn = NewRgn() ;
1965 RgnHandle scrollRgn = NewRgn() ;
1966 RectRgn( scrollRgn , &scrollrect ) ;
1967 GetWindowUpdateRgn( rootWindow , formerUpdateRgn ) ;
1968 Point pt = {0,0} ;
1969 LocalToGlobal( &pt ) ;
1970 OffsetRgn( formerUpdateRgn , -pt.h , -pt.v ) ;
1971 SectRgn( formerUpdateRgn , scrollRgn , formerUpdateRgn ) ;
1972 if ( !EmptyRgn( formerUpdateRgn ) )
1973 {
1974 MacOffsetRgn( formerUpdateRgn , dx , dy ) ;
1975 SectRgn( formerUpdateRgn , scrollRgn , formerUpdateRgn ) ;
1976 InvalWindowRgn(rootWindow , formerUpdateRgn ) ;
1977 }
1978 InvalWindowRgn(rootWindow , updateRgn ) ;
1979 DisposeRgn( updateRgn ) ;
1980 DisposeRgn( formerUpdateRgn ) ;
1981 DisposeRgn( scrollRgn ) ;
1982 #endif
1983 }
1984
1985 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
1986 {
1987 wxWindowMac *child = node->GetData();
1988 if (child == m_vScrollBar) continue;
1989 if (child == m_hScrollBar) continue;
1990 if (child->IsTopLevel()) continue;
1991
1992 int x,y;
1993 child->GetPosition( &x, &y );
1994 int w,h;
1995 child->GetSize( &w, &h );
1996 if (rect)
1997 {
1998 wxRect rc(x,y,w,h);
1999 if (rect->Intersects(rc))
2000 child->SetSize( x+dx, y+dy, w, h );
2001 }
2002 else
2003 {
2004 child->SetSize( x+dx, y+dy, w, h );
2005 }
2006 }
2007
2008 // TODO remove, was moved higher up Update() ;
2009
2010 }
2011
2012 void wxWindowMac::MacOnScroll(wxScrollEvent &event )
2013 {
2014 if ( event.m_eventObject == m_vScrollBar || event.m_eventObject == m_hScrollBar )
2015 {
2016 wxScrollWinEvent wevent;
2017 wevent.SetPosition(event.GetPosition());
2018 wevent.SetOrientation(event.GetOrientation());
2019 wevent.m_eventObject = this;
2020
2021 if (event.m_eventType == wxEVT_SCROLL_TOP)
2022 wevent.m_eventType = wxEVT_SCROLLWIN_TOP;
2023 else if (event.m_eventType == wxEVT_SCROLL_BOTTOM)
2024 wevent.m_eventType = wxEVT_SCROLLWIN_BOTTOM;
2025 else if (event.m_eventType == wxEVT_SCROLL_LINEUP)
2026 wevent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
2027 else if (event.m_eventType == wxEVT_SCROLL_LINEDOWN)
2028 wevent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
2029 else if (event.m_eventType == wxEVT_SCROLL_PAGEUP)
2030 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEUP;
2031 else if (event.m_eventType == wxEVT_SCROLL_PAGEDOWN)
2032 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEDOWN;
2033 else if (event.m_eventType == wxEVT_SCROLL_THUMBTRACK)
2034 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBTRACK;
2035 else if (event.m_eventType == wxEVT_SCROLL_THUMBRELEASE)
2036 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBRELEASE;
2037
2038 GetEventHandler()->ProcessEvent(wevent);
2039 }
2040 }
2041
2042 // Get the window with the focus
2043 wxWindowMac *wxWindowBase::FindFocus()
2044 {
2045 return gFocusWindow ;
2046 }
2047
2048 void wxWindowMac::OnSetFocus(wxFocusEvent& event)
2049 {
2050 // panel wants to track the window which was the last to have focus in it,
2051 // so we want to set ourselves as the window which last had focus
2052 //
2053 // notice that it's also important to do it upwards the tree becaus
2054 // otherwise when the top level panel gets focus, it won't set it back to
2055 // us, but to some other sibling
2056
2057 // CS:don't know if this is still needed:
2058 //wxChildFocusEvent eventFocus(this);
2059 //(void)GetEventHandler()->ProcessEvent(eventFocus);
2060
2061 event.Skip();
2062 }
2063
2064 void wxWindowMac::OnInternalIdle()
2065 {
2066 // This calls the UI-update mechanism (querying windows for
2067 // menu/toolbar/control state information)
2068 if (wxUpdateUIEvent::CanUpdate(this))
2069 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2070 }
2071
2072 // Raise the window to the top of the Z order
2073 void wxWindowMac::Raise()
2074 {
2075 }
2076
2077 // Lower the window to the bottom of the Z order
2078 void wxWindowMac::Lower()
2079 {
2080 }
2081
2082
2083 // static wxWindow *gs_lastWhich = NULL;
2084
2085 bool wxWindowMac::MacSetupCursor( const wxPoint& pt)
2086 {
2087 // first trigger a set cursor event
2088
2089 wxPoint clientorigin = GetClientAreaOrigin() ;
2090 wxSize clientsize = GetClientSize() ;
2091 wxCursor cursor ;
2092 if ( wxRect2DInt( clientorigin.x , clientorigin.y , clientsize.x , clientsize.y ).Contains( wxPoint2DInt( pt ) ) )
2093 {
2094 wxSetCursorEvent event( pt.x , pt.y );
2095
2096 bool processedEvtSetCursor = GetEventHandler()->ProcessEvent(event);
2097 if ( processedEvtSetCursor && event.HasCursor() )
2098 {
2099 cursor = event.GetCursor() ;
2100 }
2101 else
2102 {
2103
2104 // the test for processedEvtSetCursor is here to prevent using m_cursor
2105 // if the user code caught EVT_SET_CURSOR() and returned nothing from
2106 // it - this is a way to say that our cursor shouldn't be used for this
2107 // point
2108 if ( !processedEvtSetCursor && m_cursor.Ok() )
2109 {
2110 cursor = m_cursor ;
2111 }
2112 if ( wxIsBusy() )
2113 {
2114 }
2115 else
2116 {
2117 if ( !GetParent() )
2118 cursor = *wxSTANDARD_CURSOR ;
2119 }
2120 }
2121 if ( cursor.Ok() )
2122 cursor.MacInstall() ;
2123 }
2124 return cursor.Ok() ;
2125 }
2126
2127 wxString wxWindowMac::MacGetToolTipString( wxPoint &pt )
2128 {
2129 if ( m_tooltip )
2130 {
2131 return m_tooltip->GetTip() ;
2132 }
2133 return wxEmptyString ;
2134 }
2135
2136 void wxWindowMac::Update()
2137 {
2138 #if TARGET_API_MAC_OSX
2139 HIViewSetNeedsDisplay( (ControlRef) m_macControl , true ) ;
2140 #else
2141 ::Draw1Control( (ControlRef) m_macControl ) ;
2142 #endif
2143 }
2144
2145 wxTopLevelWindowMac* wxWindowMac::MacGetTopLevelWindow() const
2146 {
2147 wxTopLevelWindowMac* win = NULL ;
2148 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
2149 if ( window )
2150 {
2151 win = wxFindWinFromMacWindow( window ) ;
2152 }
2153 return win ;
2154 }
2155 wxRegion wxWindowMac::MacGetVisibleRegion( bool includeOuterStructures )
2156 {
2157
2158 Rect r ;
2159 RgnHandle visRgn = NewRgn() ;
2160 RgnHandle tempRgn = NewRgn() ;
2161 if ( IsControlVisible( (ControlRef) m_macControl ) )
2162 {
2163 GetControlBounds( (ControlRef) m_macControl , &r ) ;
2164 if (! MacGetTopLevelWindow()->MacUsesCompositing() )
2165 {
2166 MacRootWindowToWindow( &r.left , & r.top ) ;
2167 MacRootWindowToWindow( &r.right , & r.bottom ) ;
2168 }
2169 else
2170 {
2171 r.right -= r.left ;
2172 r.bottom -= r.top ;
2173 r.left = 0 ;
2174 r.top = 0 ;
2175 }
2176 if ( includeOuterStructures )
2177 InsetRect( &r , -3 , -3 ) ;
2178 RectRgn( visRgn , &r ) ;
2179 if ( !IsTopLevel() )
2180 {
2181 wxWindow* child = this ;
2182 wxWindow* parent = child->GetParent() ;
2183 while( parent )
2184 {
2185 int x , y ;
2186 wxSize size ;
2187 // we have to find a better clipping algorithm here, in order not to clip things
2188 // positioned like status and toolbar
2189 if ( 1 /* parent->IsTopLevel() && child->IsKindOf( CLASSINFO( wxToolBar ) ) */ )
2190 {
2191 size = parent->GetSize() ;
2192 x = y = 0 ;
2193 }
2194 else
2195 {
2196 size = parent->GetClientSize() ;
2197 wxPoint origin = parent->GetClientAreaOrigin() ;
2198 x = origin.x ;
2199 y = origin.y ;
2200 }
2201 parent->MacWindowToRootWindow( &x, &y ) ;
2202 MacRootWindowToWindow( &x , &y ) ;
2203
2204 SetRectRgn( tempRgn ,
2205 x + parent->MacGetLeftBorderSize() , y + parent->MacGetTopBorderSize() ,
2206 x + size.x - parent->MacGetRightBorderSize(),
2207 y + size.y - parent->MacGetBottomBorderSize()) ;
2208
2209 SectRgn( visRgn , tempRgn , visRgn ) ;
2210 if ( parent->IsTopLevel() )
2211 break ;
2212 child = parent ;
2213 parent = child->GetParent() ;
2214 }
2215 }
2216 }
2217
2218 wxRegion vis = visRgn ;
2219 DisposeRgn( visRgn ) ;
2220 DisposeRgn( tempRgn ) ;
2221 return vis ;
2222 }
2223
2224 /*
2225 This function must not change the updatergn !
2226 */
2227 bool wxWindowMac::MacDoRedraw( WXHRGN updatergnr , long time )
2228 {
2229 RgnHandle updatergn = (RgnHandle) updatergnr ;
2230 bool handled = false ;
2231
2232 // calculate a client-origin version of the update rgn and set m_updateRegion to that
2233 {
2234 RgnHandle newupdate = NewRgn() ;
2235 wxSize point = GetClientSize() ;
2236 wxPoint origin = GetClientAreaOrigin() ;
2237 SetRectRgn( newupdate , origin.x , origin.y , origin.x + point.x , origin.y+point.y ) ;
2238 SectRgn( newupdate , updatergn , newupdate ) ;
2239 OffsetRgn( newupdate , -origin.x , -origin.y ) ;
2240 m_updateRegion = newupdate ;
2241 DisposeRgn( newupdate ) ;
2242 }
2243
2244 if ( !EmptyRgn(updatergn) )
2245 {
2246 wxWindowDC dc(this);
2247 if (!EmptyRgn(updatergn))
2248 dc.SetClippingRegion(wxRegion(updatergn));
2249
2250 wxEraseEvent eevent( GetId(), &dc );
2251 eevent.SetEventObject( this );
2252 GetEventHandler()->ProcessEvent( eevent );
2253
2254 if ( !m_updateRegion.Empty() )
2255 {
2256 // paint the window itself
2257 wxPaintEvent event;
2258 event.m_timeStamp = time ;
2259 event.SetEventObject(this);
2260 handled = GetEventHandler()->ProcessEvent(event);
2261
2262 // paint custom borders
2263 wxNcPaintEvent eventNc( GetId() );
2264 eventNc.SetEventObject( this );
2265 GetEventHandler()->ProcessEvent( eventNc );
2266 }
2267 }
2268 return handled ;
2269 }
2270
2271 void wxWindowMac::MacRedraw( WXHRGN updatergnr , long time, bool erase)
2272 {
2273 RgnHandle updatergn = (RgnHandle) updatergnr ;
2274 // updatergn is always already clipped to our boundaries
2275 // if we are in compositing mode then it is in relative to the upper left of the control
2276 // if we are in non-compositing, then it is relatvie to the uppder left of the content area
2277 // of the toplevel window
2278 // it is in window coordinates, not in client coordinates
2279
2280 // ownUpdateRgn is the area that this window has to repaint, it is in window coordinates
2281 RgnHandle ownUpdateRgn = NewRgn() ;
2282 CopyRgn( updatergn , ownUpdateRgn ) ;
2283
2284 if ( MacGetTopLevelWindow()->MacUsesCompositing() == false )
2285 {
2286 Rect bounds;
2287 UMAGetControlBoundsInWindowCoords( (ControlRef)m_macControl, &bounds );
2288 RgnHandle controlRgn = NewRgn();
2289 RectRgn( controlRgn, &bounds );
2290 //KO: This sets the ownUpdateRgn to the area of this control that is inside
2291 // the window update region
2292 SectRgn( ownUpdateRgn, controlRgn, ownUpdateRgn );
2293 DisposeRgn( controlRgn );
2294
2295 //KO: convert ownUpdateRgn to local coordinates
2296 OffsetRgn( ownUpdateRgn, -bounds.left, -bounds.top );
2297 }
2298
2299 MacDoRedraw( ownUpdateRgn , time ) ;
2300 DisposeRgn( ownUpdateRgn ) ;
2301
2302 }
2303
2304 WXWindow wxWindowMac::MacGetTopLevelWindowRef() const
2305 {
2306 wxWindowMac *iter = (wxWindowMac*)this ;
2307
2308 while( iter )
2309 {
2310 if ( iter->IsTopLevel() )
2311 return ((wxTopLevelWindow*)iter)->MacGetWindowRef() ;
2312
2313 iter = iter->GetParent() ;
2314 }
2315 wxASSERT_MSG( 1 , wxT("No valid mac root window") ) ;
2316 return NULL ;
2317 }
2318
2319 void wxWindowMac::MacCreateScrollBars( long style )
2320 {
2321 wxASSERT_MSG( m_vScrollBar == NULL && m_hScrollBar == NULL , wxT("attempt to create window twice") ) ;
2322
2323 bool hasBoth = ( style & wxVSCROLL ) && ( style & wxHSCROLL ) ;
2324 int adjust = hasBoth ? MAC_SCROLLBAR_SIZE - 1: 0 ;
2325 int width, height ;
2326 GetClientSize( &width , &height ) ;
2327
2328 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
2329 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
2330 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
2331 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
2332
2333 m_vScrollBar = new wxScrollBar(this, wxWINDOW_VSCROLL, vPoint,
2334 vSize , wxVERTICAL);
2335
2336 if ( style & wxVSCROLL )
2337 {
2338
2339 }
2340 else
2341 {
2342 m_vScrollBar->Show(false) ;
2343 }
2344 m_hScrollBar = new wxScrollBar(this, wxWINDOW_HSCROLL, hPoint,
2345 hSize , wxHORIZONTAL);
2346 if ( style & wxHSCROLL )
2347 {
2348 }
2349 else
2350 {
2351 m_hScrollBar->Show(false) ;
2352 }
2353
2354 // because the create does not take into account the client area origin
2355 MacRepositionScrollBars() ; // we might have a real position shift
2356 }
2357
2358 void wxWindowMac::MacRepositionScrollBars()
2359 {
2360 bool hasBoth = ( m_hScrollBar && m_hScrollBar->IsShown()) && ( m_vScrollBar && m_vScrollBar->IsShown()) ;
2361 int adjust = hasBoth ? MAC_SCROLLBAR_SIZE - 1 : 0 ;
2362
2363 // get real client area
2364
2365 int width ;
2366 int height ;
2367 GetSize( &width , &height ) ;
2368
2369 width -= MacGetLeftBorderSize() + MacGetRightBorderSize();
2370 height -= MacGetTopBorderSize() + MacGetBottomBorderSize();
2371
2372 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
2373 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
2374 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
2375 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
2376
2377 int x = 0 ;
2378 int y = 0 ;
2379 int w ;
2380 int h ;
2381 GetSize( &w , &h ) ;
2382
2383 MacClientToRootWindow( &x , &y ) ;
2384 MacClientToRootWindow( &w , &h ) ;
2385
2386 wxWindowMac *iter = (wxWindowMac*)this ;
2387
2388 int totW = 10000 , totH = 10000;
2389 while( iter )
2390 {
2391 if ( iter->IsTopLevel() )
2392 {
2393 iter->GetSize( &totW , &totH ) ;
2394 break ;
2395 }
2396
2397 iter = iter->GetParent() ;
2398 }
2399
2400 if ( x == 0 )
2401 {
2402 hPoint.x = -1 ;
2403 hSize.x += 1 ;
2404 }
2405 if ( y == 0 )
2406 {
2407 vPoint.y = -1 ;
2408 vSize.y += 1 ;
2409 }
2410
2411 if ( w-x >= totW )
2412 {
2413 hSize.x += 1 ;
2414 vPoint.x += 1 ;
2415 }
2416
2417 if ( h-y >= totH )
2418 {
2419 vSize.y += 1 ;
2420 hPoint.y += 1 ;
2421 }
2422
2423 if ( m_vScrollBar )
2424 {
2425 m_vScrollBar->SetSize( vPoint.x , vPoint.y, vSize.x, vSize.y , wxSIZE_ALLOW_MINUS_ONE);
2426 }
2427 if ( m_hScrollBar )
2428 {
2429 m_hScrollBar->SetSize( hPoint.x , hPoint.y, hSize.x, hSize.y, wxSIZE_ALLOW_MINUS_ONE);
2430 }
2431 }
2432
2433 bool wxWindowMac::AcceptsFocus() const
2434 {
2435 return MacCanFocus() && wxWindowBase::AcceptsFocus();
2436 }
2437
2438 void wxWindowMac::MacSuperChangedPosition()
2439 {
2440 // only window-absolute structures have to be moved i.e. controls
2441
2442 wxWindowListNode *node = GetChildren().GetFirst();
2443 while ( node )
2444 {
2445 wxWindowMac *child = node->GetData();
2446 child->MacSuperChangedPosition() ;
2447 node = node->GetNext();
2448 }
2449 }
2450
2451 void wxWindowMac::MacTopLevelWindowChangedPosition()
2452 {
2453 // only screen-absolute structures have to be moved i.e. glcanvas
2454
2455 wxWindowListNode *node = GetChildren().GetFirst();
2456 while ( node )
2457 {
2458 wxWindowMac *child = node->GetData();
2459 child->MacTopLevelWindowChangedPosition() ;
2460 node = node->GetNext();
2461 }
2462 }
2463
2464 long wxWindowMac::MacGetLeftBorderSize( ) const
2465 {
2466 if( IsTopLevel() )
2467 return 0 ;
2468
2469 if (m_windowStyle & wxRAISED_BORDER || m_windowStyle & wxSUNKEN_BORDER )
2470 {
2471 SInt32 border = 3 ;
2472 #if wxMAC_USE_THEME_BORDER
2473 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2474 #endif
2475 return border ;
2476 }
2477 else if ( m_windowStyle &wxDOUBLE_BORDER)
2478 {
2479 SInt32 border = 3 ;
2480 #if wxMAC_USE_THEME_BORDER
2481 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2482 #endif
2483 return border ;
2484 }
2485 else if (m_windowStyle &wxSIMPLE_BORDER)
2486 {
2487 return 1 ;
2488 }
2489 return 0 ;
2490 }
2491
2492 long wxWindowMac::MacGetRightBorderSize( ) const
2493 {
2494 // they are all symmetric in mac themes
2495 return MacGetLeftBorderSize() ;
2496 }
2497
2498 long wxWindowMac::MacGetTopBorderSize( ) const
2499 {
2500 // they are all symmetric in mac themes
2501 return MacGetLeftBorderSize() ;
2502 }
2503
2504 long wxWindowMac::MacGetBottomBorderSize( ) const
2505 {
2506 // they are all symmetric in mac themes
2507 return MacGetLeftBorderSize() ;
2508 }
2509
2510 long wxWindowMac::MacRemoveBordersFromStyle( long style )
2511 {
2512 return style & ~( wxDOUBLE_BORDER | wxSUNKEN_BORDER | wxRAISED_BORDER | wxBORDER | wxSTATIC_BORDER ) ;
2513 }
2514
2515 // Find the wxWindowMac at the current mouse position, returning the mouse
2516 // position.
2517 wxWindowMac* wxFindWindowAtPointer(wxPoint& pt)
2518 {
2519 pt = wxGetMousePosition();
2520 wxWindowMac* found = wxFindWindowAtPoint(pt);
2521 return found;
2522 }
2523
2524 // Get the current mouse position.
2525 wxPoint wxGetMousePosition()
2526 {
2527 int x, y;
2528 wxGetMousePosition(& x, & y);
2529 return wxPoint(x, y);
2530 }
2531
2532 void wxWindowMac::OnMouseEvent( wxMouseEvent &event )
2533 {
2534 if ( event.GetEventType() == wxEVT_RIGHT_DOWN )
2535 {
2536 // copied from wxGTK : CS
2537 // generate a "context menu" event: this is similar to wxEVT_RIGHT_DOWN
2538 // except that:
2539 //
2540 // (a) it's a command event and so is propagated to the parent
2541 // (b) under MSW it can be generated from kbd too
2542 // (c) it uses screen coords (because of (a))
2543 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
2544 this->GetId(),
2545 this->ClientToScreen(event.GetPosition()));
2546 if ( ! GetEventHandler()->ProcessEvent(evtCtx) )
2547 event.Skip() ;
2548 }
2549 else if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK )
2550 {
2551
2552 int x = event.m_x ;
2553 int y = event.m_y ;
2554
2555 if ( MacGetTopLevelWindow()->MacUsesCompositing() == false )
2556 {
2557 // OS Needs it in tlw content area coordinates
2558 MacClientToRootWindow( &x , &y ) ;
2559 }
2560 else
2561 {
2562 // OS Needs it in window not client coordinates
2563 wxPoint origin = GetClientAreaOrigin() ;
2564 x += origin.x ;
2565 y += origin.y ;
2566 }
2567 Point localwhere ;
2568 SInt16 controlpart ;
2569
2570 localwhere.h = x ;
2571 localwhere.v = y ;
2572
2573 short modifiers = 0;
2574
2575 if ( !event.m_leftDown && !event.m_rightDown )
2576 modifiers |= btnState ;
2577
2578 if ( event.m_shiftDown )
2579 modifiers |= shiftKey ;
2580
2581 if ( event.m_controlDown )
2582 modifiers |= controlKey ;
2583
2584 if ( event.m_altDown )
2585 modifiers |= optionKey ;
2586
2587 if ( event.m_metaDown )
2588 modifiers |= cmdKey ;
2589
2590 bool handled = false ;
2591
2592 if ( ::IsControlActive( (ControlRef) m_macControl ) )
2593 {
2594 controlpart = ::HandleControlClick( (ControlRef) m_macControl , localwhere , modifiers , (ControlActionUPP) -1 ) ;
2595 wxTheApp->s_lastMouseDown = 0 ;
2596 if ( controlpart != kControlNoPart )
2597 {
2598 MacHandleControlClick((WXWidget) (ControlRef) m_macControl , controlpart , false /* mouse not down anymore */ ) ;
2599 handled = true ;
2600 }
2601 }
2602 if ( !handled )
2603 event.Skip() ;
2604 }
2605 else
2606 {
2607 event.Skip() ;
2608 }
2609 }
2610
2611 void wxWindowMac::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED( mouseStillDown ) )
2612 {
2613 wxASSERT_MSG( (ControlRef) m_macControl != NULL , wxT("No valid mac control") ) ;
2614 }
2615
2616 Rect wxMacGetBoundsForControl( wxWindow* window , const wxPoint& pos , const wxSize &size )
2617 {
2618 int x ,y , w ,h ;
2619
2620 window->MacGetBoundsForControl( pos , size , x , y, w, h ) ;
2621 Rect bounds = { y , x , y+h , x+w };
2622 return bounds ;
2623 }
2624
2625