]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/window.cpp
1c98aa536bdf1e0d2f519f1e28eeb350921a784e
[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 if ( m_macBackgroundBrush.Ok() == false || m_macBackgroundBrush.GetStyle() == wxTRANSPARENT )
1713 {
1714 event.Skip() ;
1715 }
1716 else
1717 event.GetDC()->Clear() ;
1718 }
1719
1720 void wxWindowMac::OnNcPaint( wxNcPaintEvent& event )
1721 {
1722 wxWindowDC dc(this) ;
1723 wxMacPortSetter helper(&dc) ;
1724
1725 MacPaintBorders( dc.m_macLocalOrigin.x , dc.m_macLocalOrigin.y) ;
1726 }
1727
1728 int wxWindowMac::GetScrollPos(int orient) const
1729 {
1730 if ( orient == wxHORIZONTAL )
1731 {
1732 if ( m_hScrollBar )
1733 return m_hScrollBar->GetThumbPosition() ;
1734 }
1735 else
1736 {
1737 if ( m_vScrollBar )
1738 return m_vScrollBar->GetThumbPosition() ;
1739 }
1740 return 0;
1741 }
1742
1743 // This now returns the whole range, not just the number
1744 // of positions that we can scroll.
1745 int wxWindowMac::GetScrollRange(int orient) const
1746 {
1747 if ( orient == wxHORIZONTAL )
1748 {
1749 if ( m_hScrollBar )
1750 return m_hScrollBar->GetRange() ;
1751 }
1752 else
1753 {
1754 if ( m_vScrollBar )
1755 return m_vScrollBar->GetRange() ;
1756 }
1757 return 0;
1758 }
1759
1760 int wxWindowMac::GetScrollThumb(int orient) const
1761 {
1762 if ( orient == wxHORIZONTAL )
1763 {
1764 if ( m_hScrollBar )
1765 return m_hScrollBar->GetThumbSize() ;
1766 }
1767 else
1768 {
1769 if ( m_vScrollBar )
1770 return m_vScrollBar->GetThumbSize() ;
1771 }
1772 return 0;
1773 }
1774
1775 void wxWindowMac::SetScrollPos(int orient, int pos, bool refresh)
1776 {
1777 if ( orient == wxHORIZONTAL )
1778 {
1779 if ( m_hScrollBar )
1780 m_hScrollBar->SetThumbPosition( pos ) ;
1781 }
1782 else
1783 {
1784 if ( m_vScrollBar )
1785 m_vScrollBar->SetThumbPosition( pos ) ;
1786 }
1787 }
1788
1789 void wxWindowMac::MacPaintBorders( int left , int top )
1790 {
1791 if( IsTopLevel() )
1792 return ;
1793
1794 int major,minor;
1795 wxGetOsVersion( &major, &minor );
1796
1797 RGBColor white = { 0xFFFF, 0xFFFF , 0xFFFF } ;
1798 RGBColor face = { 0xDDDD, 0xDDDD , 0xDDDD } ;
1799
1800 RGBColor darkShadow = { 0x0000, 0x0000 , 0x0000 } ;
1801 RGBColor lightShadow = { 0x4444, 0x4444 , 0x4444 } ;
1802 // OS X has lighter border edges than classic:
1803 if (major >= 10)
1804 {
1805 darkShadow.red = 0x8E8E;
1806 darkShadow.green = 0x8E8E;
1807 darkShadow.blue = 0x8E8E;
1808 lightShadow.red = 0xBDBD;
1809 lightShadow.green = 0xBDBD;
1810 lightShadow.blue = 0xBDBD;
1811 }
1812
1813 PenNormal() ;
1814
1815 int w , h ;
1816 GetSize( &w , &h ) ;
1817 if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
1818 {
1819 #if wxMAC_USE_THEME_BORDER
1820 Rect rect = { top , left , m_height + top , m_width + left } ;
1821 SInt32 border = 0 ;
1822 /*
1823 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
1824 InsetRect( &rect , border , border );
1825 DrawThemeListBoxFrame(&rect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
1826 */
1827
1828 DrawThemePrimaryGroup(&rect ,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
1829 #else
1830 bool sunken = HasFlag( wxSUNKEN_BORDER ) ;
1831 RGBForeColor( &face );
1832 MoveTo( left + 0 , top + h - 2 );
1833 LineTo( left + 0 , top + 0 );
1834 LineTo( left + w - 2 , top + 0 );
1835
1836 MoveTo( left + 2 , top + h - 3 );
1837 LineTo( left + w - 3 , top + h - 3 );
1838 LineTo( left + w - 3 , top + 2 );
1839
1840 RGBForeColor( sunken ? &face : &darkShadow );
1841 MoveTo( left + 0 , top + h - 1 );
1842 LineTo( left + w - 1 , top + h - 1 );
1843 LineTo( left + w - 1 , top + 0 );
1844
1845 RGBForeColor( sunken ? &lightShadow : &white );
1846 MoveTo( left + 1 , top + h - 3 );
1847 LineTo( left + 1, top + 1 );
1848 LineTo( left + w - 3 , top + 1 );
1849
1850 RGBForeColor( sunken ? &white : &lightShadow );
1851 MoveTo( left + 1 , top + h - 2 );
1852 LineTo( left + w - 2 , top + h - 2 );
1853 LineTo( left + w - 2 , top + 1 );
1854
1855 RGBForeColor( sunken ? &darkShadow : &face );
1856 MoveTo( left + 2 , top + h - 4 );
1857 LineTo( left + 2 , top + 2 );
1858 LineTo( left + w - 4 , top + 2 );
1859 #endif
1860 }
1861 else if (HasFlag(wxSIMPLE_BORDER))
1862 {
1863 Rect rect = { top , left , h + top , w + left } ;
1864 RGBForeColor( &darkShadow ) ;
1865 FrameRect( &rect ) ;
1866 }
1867 }
1868
1869 void wxWindowMac::RemoveChild( wxWindowBase *child )
1870 {
1871 if ( child == m_hScrollBar )
1872 m_hScrollBar = NULL ;
1873 if ( child == m_vScrollBar )
1874 m_vScrollBar = NULL ;
1875
1876 wxWindowBase::RemoveChild( child ) ;
1877 }
1878
1879 // New function that will replace some of the above.
1880 void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible,
1881 int range, bool refresh)
1882 {
1883 if ( orient == wxHORIZONTAL )
1884 {
1885 if ( m_hScrollBar )
1886 {
1887 if ( range == 0 || thumbVisible >= range )
1888 {
1889 if ( m_hScrollBar->IsShown() )
1890 m_hScrollBar->Show(false) ;
1891 }
1892 else
1893 {
1894 if ( !m_hScrollBar->IsShown() )
1895 m_hScrollBar->Show(true) ;
1896 m_hScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
1897 }
1898 }
1899 }
1900 else
1901 {
1902 if ( m_vScrollBar )
1903 {
1904 if ( range == 0 || thumbVisible >= range )
1905 {
1906 if ( m_vScrollBar->IsShown() )
1907 m_vScrollBar->Show(false) ;
1908 }
1909 else
1910 {
1911 if ( !m_vScrollBar->IsShown() )
1912 m_vScrollBar->Show(true) ;
1913 m_vScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
1914 }
1915 }
1916 }
1917 MacRepositionScrollBars() ;
1918 }
1919
1920 // Does a physical scroll
1921 void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect)
1922 {
1923 if( dx == 0 && dy ==0 )
1924 return ;
1925
1926
1927 {
1928 wxClientDC dc(this) ;
1929 wxMacPortSetter helper(&dc) ;
1930
1931 int width , height ;
1932 GetClientSize( &width , &height ) ;
1933
1934
1935 wxPoint pos;
1936 pos.x = pos.y = 0;
1937
1938 Rect scrollrect;
1939 // TODO take out the boundaries
1940 GetControlBounds( (ControlRef) m_macControl, &scrollrect);
1941
1942 RgnHandle updateRgn = NewRgn() ;
1943 if ( rect )
1944 {
1945 Rect r = { dc.YLOG2DEVMAC(rect->y) , dc.XLOG2DEVMAC(rect->x) , dc.YLOG2DEVMAC(rect->y + rect->height) ,
1946 dc.XLOG2DEVMAC(rect->x + rect->width) } ;
1947 SectRect( &scrollrect , &r , &scrollrect ) ;
1948 }
1949 ScrollRect( &scrollrect , dx , dy , updateRgn ) ;
1950 #if TARGET_CARBON
1951 //KO: The docs say ScrollRect creates an update region, which thus calls an update event
1952 // but it seems the update only refreshes the background of the control, rather than calling
1953 // kEventControlDraw, so we need to force a proper update here. There has to be a better
1954 // way of doing this... (Note that code below under !TARGET_CARBON does not work either...)
1955 Update();
1956 #endif
1957 // we also have to scroll the update rgn in this rectangle
1958 // in order not to loose updates
1959 #if !TARGET_CARBON
1960 WindowRef rootWindow = (WindowRef) MacGetTopLevelWindowRef() ;
1961 RgnHandle formerUpdateRgn = NewRgn() ;
1962 RgnHandle scrollRgn = NewRgn() ;
1963 RectRgn( scrollRgn , &scrollrect ) ;
1964 GetWindowUpdateRgn( rootWindow , formerUpdateRgn ) ;
1965 Point pt = {0,0} ;
1966 LocalToGlobal( &pt ) ;
1967 OffsetRgn( formerUpdateRgn , -pt.h , -pt.v ) ;
1968 SectRgn( formerUpdateRgn , scrollRgn , formerUpdateRgn ) ;
1969 if ( !EmptyRgn( formerUpdateRgn ) )
1970 {
1971 MacOffsetRgn( formerUpdateRgn , dx , dy ) ;
1972 SectRgn( formerUpdateRgn , scrollRgn , formerUpdateRgn ) ;
1973 InvalWindowRgn(rootWindow , formerUpdateRgn ) ;
1974 }
1975 InvalWindowRgn(rootWindow , updateRgn ) ;
1976 DisposeRgn( updateRgn ) ;
1977 DisposeRgn( formerUpdateRgn ) ;
1978 DisposeRgn( scrollRgn ) ;
1979 #endif
1980 }
1981
1982 for (wxWindowListNode *node = GetChildren().GetFirst(); node; node = node->GetNext())
1983 {
1984 wxWindowMac *child = node->GetData();
1985 if (child == m_vScrollBar) continue;
1986 if (child == m_hScrollBar) continue;
1987 if (child->IsTopLevel()) continue;
1988
1989 int x,y;
1990 child->GetPosition( &x, &y );
1991 int w,h;
1992 child->GetSize( &w, &h );
1993 if (rect)
1994 {
1995 wxRect rc(x,y,w,h);
1996 if (rect->Intersects(rc))
1997 child->SetSize( x+dx, y+dy, w, h );
1998 }
1999 else
2000 {
2001 child->SetSize( x+dx, y+dy, w, h );
2002 }
2003 }
2004
2005 // TODO remove, was moved higher up Update() ;
2006
2007 }
2008
2009 void wxWindowMac::MacOnScroll(wxScrollEvent &event )
2010 {
2011 if ( event.m_eventObject == m_vScrollBar || event.m_eventObject == m_hScrollBar )
2012 {
2013 wxScrollWinEvent wevent;
2014 wevent.SetPosition(event.GetPosition());
2015 wevent.SetOrientation(event.GetOrientation());
2016 wevent.m_eventObject = this;
2017
2018 if (event.m_eventType == wxEVT_SCROLL_TOP)
2019 wevent.m_eventType = wxEVT_SCROLLWIN_TOP;
2020 else if (event.m_eventType == wxEVT_SCROLL_BOTTOM)
2021 wevent.m_eventType = wxEVT_SCROLLWIN_BOTTOM;
2022 else if (event.m_eventType == wxEVT_SCROLL_LINEUP)
2023 wevent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
2024 else if (event.m_eventType == wxEVT_SCROLL_LINEDOWN)
2025 wevent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
2026 else if (event.m_eventType == wxEVT_SCROLL_PAGEUP)
2027 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEUP;
2028 else if (event.m_eventType == wxEVT_SCROLL_PAGEDOWN)
2029 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEDOWN;
2030 else if (event.m_eventType == wxEVT_SCROLL_THUMBTRACK)
2031 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBTRACK;
2032 else if (event.m_eventType == wxEVT_SCROLL_THUMBRELEASE)
2033 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBRELEASE;
2034
2035 GetEventHandler()->ProcessEvent(wevent);
2036 }
2037 }
2038
2039 // Get the window with the focus
2040 wxWindowMac *wxWindowBase::FindFocus()
2041 {
2042 return gFocusWindow ;
2043 }
2044
2045 void wxWindowMac::OnSetFocus(wxFocusEvent& event)
2046 {
2047 // panel wants to track the window which was the last to have focus in it,
2048 // so we want to set ourselves as the window which last had focus
2049 //
2050 // notice that it's also important to do it upwards the tree becaus
2051 // otherwise when the top level panel gets focus, it won't set it back to
2052 // us, but to some other sibling
2053
2054 // CS:don't know if this is still needed:
2055 //wxChildFocusEvent eventFocus(this);
2056 //(void)GetEventHandler()->ProcessEvent(eventFocus);
2057
2058 event.Skip();
2059 }
2060
2061 void wxWindowMac::OnInternalIdle()
2062 {
2063 // This calls the UI-update mechanism (querying windows for
2064 // menu/toolbar/control state information)
2065 if (wxUpdateUIEvent::CanUpdate(this))
2066 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2067 }
2068
2069 // Raise the window to the top of the Z order
2070 void wxWindowMac::Raise()
2071 {
2072 }
2073
2074 // Lower the window to the bottom of the Z order
2075 void wxWindowMac::Lower()
2076 {
2077 }
2078
2079
2080 // static wxWindow *gs_lastWhich = NULL;
2081
2082 bool wxWindowMac::MacSetupCursor( const wxPoint& pt)
2083 {
2084 // first trigger a set cursor event
2085
2086 wxPoint clientorigin = GetClientAreaOrigin() ;
2087 wxSize clientsize = GetClientSize() ;
2088 wxCursor cursor ;
2089 if ( wxRect2DInt( clientorigin.x , clientorigin.y , clientsize.x , clientsize.y ).Contains( wxPoint2DInt( pt ) ) )
2090 {
2091 wxSetCursorEvent event( pt.x , pt.y );
2092
2093 bool processedEvtSetCursor = GetEventHandler()->ProcessEvent(event);
2094 if ( processedEvtSetCursor && event.HasCursor() )
2095 {
2096 cursor = event.GetCursor() ;
2097 }
2098 else
2099 {
2100
2101 // the test for processedEvtSetCursor is here to prevent using m_cursor
2102 // if the user code caught EVT_SET_CURSOR() and returned nothing from
2103 // it - this is a way to say that our cursor shouldn't be used for this
2104 // point
2105 if ( !processedEvtSetCursor && m_cursor.Ok() )
2106 {
2107 cursor = m_cursor ;
2108 }
2109 if ( wxIsBusy() )
2110 {
2111 }
2112 else
2113 {
2114 if ( !GetParent() )
2115 cursor = *wxSTANDARD_CURSOR ;
2116 }
2117 }
2118 if ( cursor.Ok() )
2119 cursor.MacInstall() ;
2120 }
2121 return cursor.Ok() ;
2122 }
2123
2124 wxString wxWindowMac::MacGetToolTipString( wxPoint &pt )
2125 {
2126 if ( m_tooltip )
2127 {
2128 return m_tooltip->GetTip() ;
2129 }
2130 return wxEmptyString ;
2131 }
2132
2133 void wxWindowMac::Update()
2134 {
2135 #if TARGET_API_MAC_OSX
2136 HIViewSetNeedsDisplay( (ControlRef) m_macControl , true ) ;
2137 #else
2138 ::Draw1Control( (ControlRef) m_macControl ) ;
2139 #endif
2140 }
2141
2142 wxTopLevelWindowMac* wxWindowMac::MacGetTopLevelWindow() const
2143 {
2144 wxTopLevelWindowMac* win = NULL ;
2145 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
2146 if ( window )
2147 {
2148 win = wxFindWinFromMacWindow( window ) ;
2149 }
2150 return win ;
2151 }
2152 wxRegion wxWindowMac::MacGetVisibleRegion( bool includeOuterStructures )
2153 {
2154
2155 Rect r ;
2156 RgnHandle visRgn = NewRgn() ;
2157 RgnHandle tempRgn = NewRgn() ;
2158 if ( IsControlVisible( (ControlRef) m_macControl ) )
2159 {
2160 GetControlBounds( (ControlRef) m_macControl , &r ) ;
2161 if (! MacGetTopLevelWindow()->MacUsesCompositing() )
2162 {
2163 MacRootWindowToWindow( &r.left , & r.top ) ;
2164 MacRootWindowToWindow( &r.right , & r.bottom ) ;
2165 }
2166 else
2167 {
2168 r.right -= r.left ;
2169 r.bottom -= r.top ;
2170 r.left = 0 ;
2171 r.top = 0 ;
2172 }
2173 if ( includeOuterStructures )
2174 InsetRect( &r , -3 , -3 ) ;
2175 RectRgn( visRgn , &r ) ;
2176 if ( !IsTopLevel() )
2177 {
2178 wxWindow* child = this ;
2179 wxWindow* parent = child->GetParent() ;
2180 while( parent )
2181 {
2182 int x , y ;
2183 wxSize size ;
2184 // we have to find a better clipping algorithm here, in order not to clip things
2185 // positioned like status and toolbar
2186 if ( 1 /* parent->IsTopLevel() && child->IsKindOf( CLASSINFO( wxToolBar ) ) */ )
2187 {
2188 size = parent->GetSize() ;
2189 x = y = 0 ;
2190 }
2191 else
2192 {
2193 size = parent->GetClientSize() ;
2194 wxPoint origin = parent->GetClientAreaOrigin() ;
2195 x = origin.x ;
2196 y = origin.y ;
2197 }
2198 parent->MacWindowToRootWindow( &x, &y ) ;
2199 MacRootWindowToWindow( &x , &y ) ;
2200
2201 SetRectRgn( tempRgn ,
2202 x + parent->MacGetLeftBorderSize() , y + parent->MacGetTopBorderSize() ,
2203 x + size.x - parent->MacGetRightBorderSize(),
2204 y + size.y - parent->MacGetBottomBorderSize()) ;
2205
2206 SectRgn( visRgn , tempRgn , visRgn ) ;
2207 if ( parent->IsTopLevel() )
2208 break ;
2209 child = parent ;
2210 parent = child->GetParent() ;
2211 }
2212 }
2213 }
2214
2215 wxRegion vis = visRgn ;
2216 DisposeRgn( visRgn ) ;
2217 DisposeRgn( tempRgn ) ;
2218 return vis ;
2219 }
2220
2221 /*
2222 This function must not change the updatergn !
2223 */
2224 bool wxWindowMac::MacDoRedraw( WXHRGN updatergnr , long time )
2225 {
2226 RgnHandle updatergn = (RgnHandle) updatergnr ;
2227 bool handled = false ;
2228
2229 // calculate a client-origin version of the update rgn and set m_updateRegion to that
2230 {
2231 RgnHandle newupdate = NewRgn() ;
2232 wxSize point = GetClientSize() ;
2233 wxPoint origin = GetClientAreaOrigin() ;
2234 SetRectRgn( newupdate , origin.x , origin.y , origin.x + point.x , origin.y+point.y ) ;
2235 SectRgn( newupdate , updatergn , newupdate ) ;
2236 OffsetRgn( newupdate , -origin.x , -origin.y ) ;
2237 m_updateRegion = newupdate ;
2238 DisposeRgn( newupdate ) ;
2239 }
2240
2241 if ( !EmptyRgn(updatergn) )
2242 {
2243 wxWindowDC dc(this);
2244 if (!EmptyRgn(updatergn))
2245 dc.SetClippingRegion(wxRegion(updatergn));
2246
2247 wxEraseEvent eevent( GetId(), &dc );
2248 eevent.SetEventObject( this );
2249 GetEventHandler()->ProcessEvent( eevent );
2250
2251 if ( !m_updateRegion.Empty() )
2252 {
2253 // paint the window itself
2254 wxPaintEvent event;
2255 event.m_timeStamp = time ;
2256 event.SetEventObject(this);
2257 handled = GetEventHandler()->ProcessEvent(event);
2258
2259 // paint custom borders
2260 wxNcPaintEvent eventNc( GetId() );
2261 eventNc.SetEventObject( this );
2262 GetEventHandler()->ProcessEvent( eventNc );
2263 }
2264 }
2265 return handled ;
2266 }
2267
2268 void wxWindowMac::MacRedraw( WXHRGN updatergnr , long time, bool erase)
2269 {
2270 RgnHandle updatergn = (RgnHandle) updatergnr ;
2271 // updatergn is always already clipped to our boundaries
2272 // if we are in compositing mode then it is in relative to the upper left of the control
2273 // if we are in non-compositing, then it is relatvie to the uppder left of the content area
2274 // of the toplevel window
2275 // it is in window coordinates, not in client coordinates
2276
2277 // ownUpdateRgn is the area that this window has to repaint, it is in window coordinates
2278 RgnHandle ownUpdateRgn = NewRgn() ;
2279 CopyRgn( updatergn , ownUpdateRgn ) ;
2280
2281 if ( MacGetTopLevelWindow()->MacUsesCompositing() == false )
2282 {
2283 Rect bounds;
2284 UMAGetControlBoundsInWindowCoords( (ControlRef)m_macControl, &bounds );
2285 RgnHandle controlRgn = NewRgn();
2286 RectRgn( controlRgn, &bounds );
2287 //KO: This sets the ownUpdateRgn to the area of this control that is inside
2288 // the window update region
2289 SectRgn( ownUpdateRgn, controlRgn, ownUpdateRgn );
2290 DisposeRgn( controlRgn );
2291
2292 //KO: convert ownUpdateRgn to local coordinates
2293 OffsetRgn( ownUpdateRgn, -bounds.left, -bounds.top );
2294 }
2295
2296 MacDoRedraw( ownUpdateRgn , time ) ;
2297 DisposeRgn( ownUpdateRgn ) ;
2298
2299 }
2300
2301 WXWindow wxWindowMac::MacGetTopLevelWindowRef() const
2302 {
2303 wxWindowMac *iter = (wxWindowMac*)this ;
2304
2305 while( iter )
2306 {
2307 if ( iter->IsTopLevel() )
2308 return ((wxTopLevelWindow*)iter)->MacGetWindowRef() ;
2309
2310 iter = iter->GetParent() ;
2311 }
2312 wxASSERT_MSG( 1 , wxT("No valid mac root window") ) ;
2313 return NULL ;
2314 }
2315
2316 void wxWindowMac::MacCreateScrollBars( long style )
2317 {
2318 wxASSERT_MSG( m_vScrollBar == NULL && m_hScrollBar == NULL , wxT("attempt to create window twice") ) ;
2319
2320 bool hasBoth = ( style & wxVSCROLL ) && ( style & wxHSCROLL ) ;
2321 int adjust = hasBoth ? MAC_SCROLLBAR_SIZE - 1: 0 ;
2322 int width, height ;
2323 GetClientSize( &width , &height ) ;
2324
2325 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
2326 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
2327 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
2328 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
2329
2330 m_vScrollBar = new wxScrollBar(this, wxWINDOW_VSCROLL, vPoint,
2331 vSize , wxVERTICAL);
2332
2333 if ( style & wxVSCROLL )
2334 {
2335
2336 }
2337 else
2338 {
2339 m_vScrollBar->Show(false) ;
2340 }
2341 m_hScrollBar = new wxScrollBar(this, wxWINDOW_HSCROLL, hPoint,
2342 hSize , wxHORIZONTAL);
2343 if ( style & wxHSCROLL )
2344 {
2345 }
2346 else
2347 {
2348 m_hScrollBar->Show(false) ;
2349 }
2350
2351 // because the create does not take into account the client area origin
2352 MacRepositionScrollBars() ; // we might have a real position shift
2353 }
2354
2355 void wxWindowMac::MacRepositionScrollBars()
2356 {
2357 bool hasBoth = ( m_hScrollBar && m_hScrollBar->IsShown()) && ( m_vScrollBar && m_vScrollBar->IsShown()) ;
2358 int adjust = hasBoth ? MAC_SCROLLBAR_SIZE - 1 : 0 ;
2359
2360 // get real client area
2361
2362 int width ;
2363 int height ;
2364 GetSize( &width , &height ) ;
2365
2366 width -= MacGetLeftBorderSize() + MacGetRightBorderSize();
2367 height -= MacGetTopBorderSize() + MacGetBottomBorderSize();
2368
2369 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
2370 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
2371 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
2372 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
2373
2374 int x = 0 ;
2375 int y = 0 ;
2376 int w ;
2377 int h ;
2378 GetSize( &w , &h ) ;
2379
2380 MacClientToRootWindow( &x , &y ) ;
2381 MacClientToRootWindow( &w , &h ) ;
2382
2383 wxWindowMac *iter = (wxWindowMac*)this ;
2384
2385 int totW = 10000 , totH = 10000;
2386 while( iter )
2387 {
2388 if ( iter->IsTopLevel() )
2389 {
2390 iter->GetSize( &totW , &totH ) ;
2391 break ;
2392 }
2393
2394 iter = iter->GetParent() ;
2395 }
2396
2397 if ( x == 0 )
2398 {
2399 hPoint.x = -1 ;
2400 hSize.x += 1 ;
2401 }
2402 if ( y == 0 )
2403 {
2404 vPoint.y = -1 ;
2405 vSize.y += 1 ;
2406 }
2407
2408 if ( w-x >= totW )
2409 {
2410 hSize.x += 1 ;
2411 vPoint.x += 1 ;
2412 }
2413
2414 if ( h-y >= totH )
2415 {
2416 vSize.y += 1 ;
2417 hPoint.y += 1 ;
2418 }
2419
2420 if ( m_vScrollBar )
2421 {
2422 m_vScrollBar->SetSize( vPoint.x , vPoint.y, vSize.x, vSize.y , wxSIZE_ALLOW_MINUS_ONE);
2423 }
2424 if ( m_hScrollBar )
2425 {
2426 m_hScrollBar->SetSize( hPoint.x , hPoint.y, hSize.x, hSize.y, wxSIZE_ALLOW_MINUS_ONE);
2427 }
2428 }
2429
2430 bool wxWindowMac::AcceptsFocus() const
2431 {
2432 return MacCanFocus() && wxWindowBase::AcceptsFocus();
2433 }
2434
2435 void wxWindowMac::MacSuperChangedPosition()
2436 {
2437 // only window-absolute structures have to be moved i.e. controls
2438
2439 wxWindowListNode *node = GetChildren().GetFirst();
2440 while ( node )
2441 {
2442 wxWindowMac *child = node->GetData();
2443 child->MacSuperChangedPosition() ;
2444 node = node->GetNext();
2445 }
2446 }
2447
2448 void wxWindowMac::MacTopLevelWindowChangedPosition()
2449 {
2450 // only screen-absolute structures have to be moved i.e. glcanvas
2451
2452 wxWindowListNode *node = GetChildren().GetFirst();
2453 while ( node )
2454 {
2455 wxWindowMac *child = node->GetData();
2456 child->MacTopLevelWindowChangedPosition() ;
2457 node = node->GetNext();
2458 }
2459 }
2460
2461 long wxWindowMac::MacGetLeftBorderSize( ) const
2462 {
2463 if( IsTopLevel() )
2464 return 0 ;
2465
2466 if (m_windowStyle & wxRAISED_BORDER || m_windowStyle & wxSUNKEN_BORDER )
2467 {
2468 SInt32 border = 3 ;
2469 #if wxMAC_USE_THEME_BORDER
2470 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2471 #endif
2472 return border ;
2473 }
2474 else if ( m_windowStyle &wxDOUBLE_BORDER)
2475 {
2476 SInt32 border = 3 ;
2477 #if wxMAC_USE_THEME_BORDER
2478 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
2479 #endif
2480 return border ;
2481 }
2482 else if (m_windowStyle &wxSIMPLE_BORDER)
2483 {
2484 return 1 ;
2485 }
2486 return 0 ;
2487 }
2488
2489 long wxWindowMac::MacGetRightBorderSize( ) const
2490 {
2491 // they are all symmetric in mac themes
2492 return MacGetLeftBorderSize() ;
2493 }
2494
2495 long wxWindowMac::MacGetTopBorderSize( ) const
2496 {
2497 // they are all symmetric in mac themes
2498 return MacGetLeftBorderSize() ;
2499 }
2500
2501 long wxWindowMac::MacGetBottomBorderSize( ) const
2502 {
2503 // they are all symmetric in mac themes
2504 return MacGetLeftBorderSize() ;
2505 }
2506
2507 long wxWindowMac::MacRemoveBordersFromStyle( long style )
2508 {
2509 return style & ~( wxDOUBLE_BORDER | wxSUNKEN_BORDER | wxRAISED_BORDER | wxBORDER | wxSTATIC_BORDER ) ;
2510 }
2511
2512 // Find the wxWindowMac at the current mouse position, returning the mouse
2513 // position.
2514 wxWindowMac* wxFindWindowAtPointer(wxPoint& pt)
2515 {
2516 pt = wxGetMousePosition();
2517 wxWindowMac* found = wxFindWindowAtPoint(pt);
2518 return found;
2519 }
2520
2521 // Get the current mouse position.
2522 wxPoint wxGetMousePosition()
2523 {
2524 int x, y;
2525 wxGetMousePosition(& x, & y);
2526 return wxPoint(x, y);
2527 }
2528
2529 void wxWindowMac::OnMouseEvent( wxMouseEvent &event )
2530 {
2531 if ( event.GetEventType() == wxEVT_RIGHT_DOWN )
2532 {
2533 // copied from wxGTK : CS
2534 // generate a "context menu" event: this is similar to wxEVT_RIGHT_DOWN
2535 // except that:
2536 //
2537 // (a) it's a command event and so is propagated to the parent
2538 // (b) under MSW it can be generated from kbd too
2539 // (c) it uses screen coords (because of (a))
2540 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
2541 this->GetId(),
2542 this->ClientToScreen(event.GetPosition()));
2543 if ( ! GetEventHandler()->ProcessEvent(evtCtx) )
2544 event.Skip() ;
2545 }
2546 else if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK )
2547 {
2548
2549 int x = event.m_x ;
2550 int y = event.m_y ;
2551
2552 if ( MacGetTopLevelWindow()->MacUsesCompositing() == false )
2553 {
2554 // OS Needs it in tlw content area coordinates
2555 MacClientToRootWindow( &x , &y ) ;
2556 }
2557 else
2558 {
2559 // OS Needs it in window not client coordinates
2560 wxPoint origin = GetClientAreaOrigin() ;
2561 x += origin.x ;
2562 y += origin.y ;
2563 }
2564 Point localwhere ;
2565 SInt16 controlpart ;
2566
2567 localwhere.h = x ;
2568 localwhere.v = y ;
2569
2570 short modifiers = 0;
2571
2572 if ( !event.m_leftDown && !event.m_rightDown )
2573 modifiers |= btnState ;
2574
2575 if ( event.m_shiftDown )
2576 modifiers |= shiftKey ;
2577
2578 if ( event.m_controlDown )
2579 modifiers |= controlKey ;
2580
2581 if ( event.m_altDown )
2582 modifiers |= optionKey ;
2583
2584 if ( event.m_metaDown )
2585 modifiers |= cmdKey ;
2586
2587 bool handled = false ;
2588
2589 if ( ::IsControlActive( (ControlRef) m_macControl ) )
2590 {
2591 controlpart = ::HandleControlClick( (ControlRef) m_macControl , localwhere , modifiers , (ControlActionUPP) -1 ) ;
2592 wxTheApp->s_lastMouseDown = 0 ;
2593 if ( controlpart != kControlNoPart )
2594 {
2595 MacHandleControlClick((WXWidget) (ControlRef) m_macControl , controlpart , false /* mouse not down anymore */ ) ;
2596 handled = true ;
2597 }
2598 }
2599 if ( !handled )
2600 event.Skip() ;
2601 }
2602 else
2603 {
2604 event.Skip() ;
2605 }
2606 }
2607
2608 void wxWindowMac::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED( mouseStillDown ) )
2609 {
2610 wxASSERT_MSG( (ControlRef) m_macControl != NULL , wxT("No valid mac control") ) ;
2611 }
2612
2613 Rect wxMacGetBoundsForControl( wxWindow* window , const wxPoint& pos , const wxSize &size )
2614 {
2615 int x ,y , w ,h ;
2616
2617 window->MacGetBoundsForControl( pos , size , x , y, w, h ) ;
2618 Rect bounds = { y , x , y+h , x+w };
2619 return bounds ;
2620 }
2621
2622