]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/control.cpp
VTK wrapper of vtkRenderWindow for wxPython. Tested on MSW so far.
[wxWidgets.git] / src / mac / carbon / control.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: control.cpp
3 // Purpose: wxControl class
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "control.h"
14 #endif
15
16 #include "wx/control.h"
17 #include "wx/notebook.h"
18 #include "wx/tabctrl.h"
19 #include "wx/spinbutt.h"
20
21 #if !USE_SHARED_LIBRARY
22 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
23
24 BEGIN_EVENT_TABLE(wxControl, wxWindow)
25 EVT_MOUSE_EVENTS( wxControl::OnMouseEvent )
26 EVT_CHAR( wxControl::OnKeyDown )
27 EVT_PAINT( wxControl::OnPaint )
28 END_EVENT_TABLE()
29 #endif
30
31 #include <wx/mac/uma.h>
32
33 // Item members
34
35 ControlActionUPP wxMacLiveScrollbarActionUPP = NULL ;
36
37 pascal void wxMacLiveScrollbarActionProc( ControlHandle control , ControlPartCode partCode )
38 {
39 if ( partCode != 0)
40 {
41 wxControl* wx = (wxControl*) GetControlReference( control ) ;
42 if ( wx )
43 {
44 wx->MacHandleControlClick( control , partCode ) ;
45 }
46 }
47 }
48
49 wxControl::wxControl()
50 {
51 m_macControl = NULL ;
52 m_macHorizontalBorder = 0 ; // additional pixels around the real control
53 m_macVerticalBorder = 0 ;
54 m_backgroundColour = *wxWHITE;
55 m_foregroundColour = *wxBLACK;
56 #if WXWIN_COMPATIBILITY
57 m_callback = 0;
58 #endif // WXWIN_COMPATIBILITY
59
60 if ( wxMacLiveScrollbarActionUPP == NULL )
61 {
62 wxMacLiveScrollbarActionUPP = NewControlActionProc( wxMacLiveScrollbarActionProc ) ;
63 }
64 }
65
66 wxControl::~wxControl()
67 {
68 m_isBeingDeleted = TRUE;
69 // If we delete an item, we should initialize the parent panel,
70 // because it could now be invalid.
71 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
72 if ( panel )
73 {
74 if (panel->GetDefaultItem() == (wxButton*) this)
75 panel->SetDefaultItem(NULL);
76 }
77 if ( m_macControl )
78 {
79 UMADisposeControl( m_macControl ) ;
80 m_macControl = NULL ;
81 }
82 }
83
84 void wxControl::SetLabel(const wxString& title)
85 {
86 m_label = title ;
87
88 if ( m_macControl )
89 {
90 Str255 maclabel ;
91 wxString label ;
92
93 if( wxApp::s_macDefaultEncodingIsPC )
94 label = wxMacMakeMacStringFromPC( title ) ;
95 else
96 label = title ;
97
98 strcpy( (char*) maclabel , label ) ;
99 c2pstr( (char*) maclabel ) ;
100
101 ::SetControlTitle( m_macControl , maclabel ) ;
102 }
103 }
104
105 wxSize wxControl::DoGetBestSize()
106 {
107 return wxSize(20, 20);
108 }
109
110 bool wxControl::ProcessCommand (wxCommandEvent & event)
111 {
112 // Tries:
113 // 1) A callback function (to become obsolete)
114 // 2) OnCommand, starting at this window and working up parent hierarchy
115 // 3) OnCommand then calls ProcessEvent to search the event tables.
116 #if WXWIN_COMPATIBILITY
117 if ( m_callback )
118 {
119 (void)(*m_callback)(this, event);
120
121 return TRUE;
122 }
123 else
124 #endif // WXWIN_COMPATIBILITY
125 {
126 return GetEventHandler()->ProcessEvent(event);
127 }
128 }
129
130 // ------------------------
131 wxList *wxWinMacControlList = NULL;
132 wxControl *wxFindControlFromMacControl(ControlHandle inControl )
133 {
134 wxNode *node = wxWinMacControlList->Find((long)inControl);
135 if (!node)
136 return NULL;
137 return (wxControl *)node->Data();
138 }
139
140 void wxAssociateControlWithMacControl(ControlHandle inControl, wxControl *control)
141 {
142 // adding NULL WindowRef is (first) surely a result of an error and
143 // (secondly) breaks menu command processing
144 wxCHECK_RET( inControl != (ControlHandle) NULL, "attempt to add a NULL WindowRef to window list" );
145
146 if ( !wxWinMacControlList->Find((long)inControl) )
147 wxWinMacControlList->Append((long)inControl, control);
148 }
149
150 void wxRemoveMacControlAssociation(wxControl *control)
151 {
152 wxWinMacControlList->DeleteObject(control);
153 }
154
155 void wxControl::MacPreControlCreate( wxWindow *parent, wxWindowID id, wxString label ,
156 const wxPoint& pos,
157 const wxSize& size, long style,
158 const wxValidator& validator,
159 const wxString& name , Rect *outBounds , StringPtr maclabel )
160 {
161 m_label = label ;
162 SetName(name);
163 if ( &validator )
164 SetValidator(validator);
165
166 m_windowStyle = style;
167 parent->AddChild((wxButton *)this);
168
169 m_backgroundColour = parent->GetBackgroundColour() ;
170 m_foregroundColour = parent->GetForegroundColour() ;
171
172 if (id == -1)
173 m_windowId = NewControlId();
174 else
175 m_windowId = id;
176
177 m_width = size.x ;
178 m_height = size.y ;
179 int x = pos.x ;
180 int y = pos.y ;
181 AdjustForParentClientOrigin(x, y, wxSIZE_USE_EXISTING);
182 m_x = x ;
183 m_y = y ;
184
185
186 Point localOrigin ;
187 Rect clipRect ;
188
189 parent->MacClientToRootWindow( &x , &y ) ;
190 outBounds->top = y + m_macVerticalBorder ;
191 outBounds->left = x + m_macHorizontalBorder ;
192 outBounds->bottom = outBounds->top + m_height - 2 * m_macVerticalBorder;
193 outBounds->right = outBounds->left + m_width - 2 * m_macHorizontalBorder ;
194
195 strcpy( (char*) maclabel , label ) ;
196 if( wxApp::s_macDefaultEncodingIsPC )
197 {
198 wxMacConvertFromPCForControls( (char*) maclabel ) ;
199 }
200
201 c2pstr( (char*) maclabel ) ;
202 }
203
204 void wxControl::MacPostControlCreate()
205 {
206 wxASSERT_MSG( m_macControl != NULL , "No valid mac control" ) ;
207
208 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
209 {
210 // no font
211 }
212 else if ( IsKindOf( CLASSINFO( wxStaticBox ) ) )
213 {
214 ControlFontStyleRec controlstyle ;
215 controlstyle.flags = kControlUseFontMask ;
216 controlstyle.font = kControlFontSmallBoldSystemFont ;
217
218 ::UMASetControlFontStyle( m_macControl , &controlstyle ) ;
219 }
220 else
221 {
222 ControlFontStyleRec controlstyle ;
223 controlstyle.flags = kControlUseFontMask ;
224 controlstyle.font = kControlFontSmallSystemFont ;
225
226 ::UMASetControlFontStyle( m_macControl , &controlstyle ) ;
227 }
228 ControlHandle container = GetParent()->MacGetContainerForEmbedding() ;
229 wxASSERT_MSG( container != NULL , "No valid mac container control" ) ;
230 ::UMAEmbedControl( m_macControl , container ) ;
231 MacAdjustControlRect() ;
232 wxAssociateControlWithMacControl( m_macControl , this ) ;
233 }
234
235 void wxControl::MacAdjustControlRect()
236 {
237 wxASSERT_MSG( m_macControl != NULL , "No valid mac control" ) ;
238 if ( m_width == -1 || m_height == -1 )
239 {
240 Rect bestsize = { 0 , 0 , 0 , 0 } ;
241 short baselineoffset ;
242
243 UMAGetBestControlRect( m_macControl , &bestsize , &baselineoffset ) ;
244
245 if ( EmptyRect( &bestsize ) )
246 {
247 baselineoffset = 0;
248 bestsize.left = bestsize.top = 0 ;
249 bestsize.right = 16 ;
250 bestsize.bottom = 16 ;
251 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
252 {
253 bestsize.bottom = 16 ;
254 }
255 else if ( IsKindOf( CLASSINFO( wxSpinButton ) ) )
256 {
257 bestsize.bottom = 24 ;
258 }
259 }
260
261 if ( m_width == -1 )
262 {
263 if ( IsKindOf( CLASSINFO( wxButton ) ) )
264 {
265 m_width = m_label.Length() * 8 + 12 + 2 * m_macHorizontalBorder;
266 }
267 else if ( IsKindOf( CLASSINFO( wxStaticText ) ) )
268 {
269 m_width = m_label.Length() * 8 ;
270 }
271 else
272 m_width = bestsize.right - bestsize.left + 2 * m_macHorizontalBorder;
273 }
274 if ( m_height == -1 )
275 {
276 m_height = bestsize.bottom - bestsize.top ;
277 if ( m_height < 10 )
278 m_height = 13 ;
279
280 m_height += 2 * m_macVerticalBorder;
281 }
282
283 wxMacDrawingHelper helper ( wxFindWinFromMacWindow( GetMacRootWindow() ) ) ;
284 if ( helper.Ok() )
285 {
286 UMASizeControl( m_macControl , m_width - 2 * m_macHorizontalBorder, m_height - 2 * m_macVerticalBorder ) ;
287 }
288 }
289 }
290 ControlHandle wxControl::MacGetContainerForEmbedding()
291 {
292 if ( m_macControl )
293 return m_macControl ;
294
295 return wxWindow::MacGetContainerForEmbedding() ;
296 }
297
298 void wxControl::MacSuperChangedPosition()
299 {
300 if ( m_macControl )
301 {
302 int former_mac_x = (**m_macControl).contrlRect.left ;
303 int former_mac_y = (**m_macControl).contrlRect.top ;
304 int mac_x = m_x ;
305 int mac_y = m_y ;
306 GetParent()->MacClientToRootWindow( & mac_x , & mac_y ) ;
307
308 WindowRef rootwindow = GetMacRootWindow() ;
309 wxWindow* wxrootwindow = wxFindWinFromMacWindow( rootwindow ) ;
310 UMASetThemeWindowBackground( rootwindow , kThemeBrushDialogBackgroundActive , false ) ;
311 wxMacDrawingHelper focus( wxrootwindow ) ;
312
313 if ( mac_x != former_mac_x || mac_y != former_mac_y )
314 {
315 {
316 Rect inval = { former_mac_y , former_mac_x , former_mac_y + m_height , former_mac_x + m_width } ;
317 InvalRect( &inval ) ;
318 }
319 UMAMoveControl( m_macControl , mac_x + m_macHorizontalBorder , mac_y + m_macVerticalBorder ) ;
320 {
321 Rect inval = { mac_y , mac_x , mac_y + m_height , mac_x + m_width } ;
322 InvalRect( &inval ) ;
323 }
324 }
325 if ( wxrootwindow->IsKindOf( CLASSINFO( wxDialog ) ) )
326 {
327 }
328 else
329 {
330 UMASetThemeWindowBackground( rootwindow , kThemeBrushDocumentWindowBackground , false ) ;
331 }
332 }
333
334 wxWindow::MacSuperChangedPosition() ;
335 }
336
337 void wxControl::MacSuperEnabled( bool enabled )
338 {
339 /*
340 if ( m_macControl )
341 {
342 if ( UMAHasAppearance() )
343 {
344 if ( !enabled )
345 {
346 ::DeactivateControl( m_macControl ) ;
347 }
348 else
349 {
350 if ( m_macEnabled )
351 ::ActivateControl( m_macControl ) ;
352 }
353 }
354 else
355 {
356 if ( !enabled )
357 {
358 ::HiliteControl( m_macControl , 255 ) ;
359 }
360 else
361 {
362 if ( m_macEnabled )
363 ::HiliteControl( m_macControl , 0 ) ;
364 }
365 }
366 }
367 wxWindow::MacSuperEnabled( enabled ) ;
368 */
369 }
370
371 void wxControl::MacSuperShown( bool show )
372 {
373 /*
374 if ( m_macControl )
375 {
376 if ( !show )
377 {
378 ::UMAHideControl( m_macControl ) ;
379 }
380 else
381 {
382 if ( m_macShown )
383 ::UMAShowControl( m_macControl ) ;
384 }
385 }
386
387 wxWindow::MacSuperShown( show ) ;
388 */
389 }
390
391 void wxControl::DoSetSize(int x, int y,
392 int width, int height,
393 int sizeFlags )
394 {
395 if ( m_macControl == NULL )
396 {
397 wxWindow::DoSetSize( x , y ,width , height ,sizeFlags ) ;
398 return ;
399 }
400
401 WindowRef rootwindow = GetMacRootWindow() ;
402 wxWindow* wxrootwindow = wxFindWinFromMacWindow( rootwindow ) ;
403 UMASetThemeWindowBackground( rootwindow , kThemeBrushDialogBackgroundActive , false ) ;
404
405 int former_x = m_x ;
406 int former_y = m_y ;
407 int former_w = m_width ;
408 int former_h = m_height ;
409
410 int former_mac_x = (**m_macControl).contrlRect.left ;
411 int former_mac_y = (**m_macControl).contrlRect.top ;
412
413 int currentX, currentY;
414 GetPosition(&currentX, &currentY);
415 int currentW,currentH;
416 GetSize(&currentW, &currentH);
417
418 int actualWidth = width;
419 int actualHeight = height;
420 int actualX = x;
421 int actualY = y;
422 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
423 actualX = currentX;
424 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
425 actualY = currentY;
426 if (width == -1)
427 actualWidth = currentW ;
428 if (height == -1)
429 actualHeight = currentH ;
430
431 if ( actualX == currentX && actualY == currentY && actualWidth == currentW && actualHeight == currentH)
432 return ;
433
434 AdjustForParentClientOrigin(actualX, actualY, sizeFlags);
435 wxMacDrawingHelper focus( wxFindWinFromMacWindow( GetMacRootWindow() ) ) ;
436
437 int mac_x = actualX ;
438 int mac_y = actualY ;
439 GetParent()->MacClientToRootWindow( & mac_x , & mac_y ) ;
440
441 if ( mac_x != former_mac_x || mac_y != former_mac_y )
442 {
443 {
444 Rect inval = { former_mac_y , former_mac_x , former_mac_y + m_height , former_mac_x + m_width } ;
445 InvalRect( &inval ) ;
446 }
447 UMAMoveControl( m_macControl , mac_x + m_macHorizontalBorder , mac_y + m_macVerticalBorder ) ;
448 {
449 Rect inval = { mac_y , mac_x , mac_y + m_height , mac_x + m_width } ;
450 InvalRect( &inval ) ;
451 }
452 }
453
454 if ( actualX != former_x || actualY != former_y )
455 {
456 m_x = actualX ;
457 m_y = actualY ;
458
459 MacRepositionScrollBars() ;
460 // To consider -> should the parameters be the effective or the virtual coordinates (AdjustForParent..)
461 wxMoveEvent event(wxPoint(m_x, m_y), m_windowId);
462 event.SetEventObject(this);
463 GetEventHandler()->ProcessEvent(event);
464 }
465 if ( actualWidth != former_w || actualHeight != former_h )
466 {
467 {
468 Rect inval = { mac_y , mac_x , mac_y + former_h , mac_x + former_w } ;
469 InvalRect( &inval ) ;
470 }
471 m_width = actualWidth ;
472 m_height = actualHeight ;
473
474 UMASizeControl( m_macControl , m_width - 2 * m_macHorizontalBorder, m_height - 2 * m_macVerticalBorder ) ;
475 {
476 Rect inval = { mac_y , mac_x , mac_y + m_height , mac_x + m_width } ;
477 InvalRect( &inval ) ;
478 }
479
480 MacRepositionScrollBars() ;
481 wxSizeEvent event(wxSize(m_width, m_height), m_windowId);
482 event.SetEventObject(this);
483 GetEventHandler()->ProcessEvent(event);
484 }
485 if ( wxrootwindow->IsKindOf( CLASSINFO( wxDialog ) ) )
486 {
487 }
488 else
489 {
490 UMASetThemeWindowBackground( rootwindow , kThemeBrushDocumentWindowBackground , false ) ;
491 }
492 }
493
494 bool wxControl::Show(bool show)
495 {
496 if ( !wxWindow::Show( show ) )
497 return FALSE ;
498
499 if ( m_macControl )
500 {
501 if ( show )
502 ::UMAShowControl( m_macControl ) ;
503 else
504 ::UMAHideControl( m_macControl ) ;
505 }
506 return TRUE ;
507 }
508
509 bool wxControl::Enable(bool enable)
510 {
511 if ( !wxWindow::Enable(enable) )
512 return FALSE;
513
514 if ( m_macControl )
515 {
516
517 if ( UMAHasAppearance() )
518 {
519 if ( enable )
520 ::ActivateControl( m_macControl ) ;
521 else
522 ::DeactivateControl( m_macControl ) ;
523 }
524 else
525 {
526 if ( enable )
527 ::HiliteControl( m_macControl , 0 ) ;
528 else
529 ::HiliteControl( m_macControl , 255 ) ;
530 }
531 }
532 return TRUE ;
533 }
534
535 void wxControl::Refresh(bool eraseBack, const wxRect *rect)
536 {
537 if ( m_macControl )
538 {
539 wxWindow::Refresh( eraseBack , rect ) ;
540 }
541 else
542 {
543 wxWindow::Refresh( eraseBack , rect ) ;
544 }
545 }
546
547 void wxControl::OnPaint(wxPaintEvent& event)
548 {
549 if ( m_macControl )
550 {
551 WindowRef window = GetMacRootWindow() ;
552 if ( window )
553 {
554 wxWindow* win = wxFindWinFromMacWindow( window ) ;
555 if ( win )
556 {
557 wxMacDrawingHelper help( win ) ;
558 SetOrigin( 0 , 0 ) ;
559
560 bool hasTabBehind = false ;
561 wxWindow* parent = GetParent() ;
562 while ( parent )
563 {
564 if( parent->MacGetWindowData() )
565 {
566 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , kThemeBrushDialogBackgroundActive , false ) ;
567 break ;
568 }
569
570 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) ))
571 {
572 if ( ((wxControl*)parent)->m_macControl )
573 SetUpControlBackground( ((wxControl*)parent)->m_macControl , -1 , true ) ;
574 break ;
575 }
576
577 parent = parent->GetParent() ;
578 }
579
580 UMADrawControl( m_macControl ) ;
581 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , win->MacGetWindowData()->m_macWindowBackgroundTheme , false ) ;
582 }
583 }
584 }
585 else
586 {
587 // wxWindow::OnPaint( event ) ;
588 }
589 }
590 void wxControl::OnEraseBackground(wxEraseEvent& event)
591 {
592 // In general, you don't want to erase the background of a control,
593 // or you'll get a flicker.
594 // TODO: move this 'null' function into each control that
595 // might flicker.
596 }
597
598
599 void wxControl::OnKeyDown( wxKeyEvent &event )
600 {
601 if ( m_macControl == NULL )
602 return ;
603
604 EventRecord *ev = wxTheApp->MacGetCurrentEvent() ;
605 short keycode ;
606 short keychar ;
607 keychar = short(ev->message & charCodeMask);
608 keycode = short(ev->message & keyCodeMask) >> 8 ;
609
610 UMAHandleControlKey( m_macControl , keycode , keychar , ev->modifiers ) ;
611 }
612
613 void wxControl::OnMouseEvent( wxMouseEvent &event )
614 {
615 if ( m_macControl == NULL )
616 {
617 event.Skip() ;
618 return ;
619 }
620
621 if (event.GetEventType() == wxEVT_LEFT_DOWN )
622 {
623
624 int x = event.m_x ;
625 int y = event.m_y ;
626
627 MacClientToRootWindow( &x , &y ) ;
628
629 ControlHandle control ;
630 Point localwhere ;
631 GrafPtr port ;
632 SInt16 controlpart ;
633 WindowRef window = GetMacRootWindow() ;
634
635 localwhere.h = x ;
636 localwhere.v = y ;
637
638 short modifiers = 0;
639
640 if ( !event.m_leftDown && !event.m_rightDown )
641 modifiers |= btnState ;
642
643 if ( event.m_shiftDown )
644 modifiers |= shiftKey ;
645
646 if ( event.m_controlDown )
647 modifiers |= controlKey ;
648
649 if ( event.m_altDown )
650 modifiers |= optionKey ;
651
652 if ( event.m_metaDown )
653 modifiers |= cmdKey ;
654
655 controlpart = FindControl( localwhere , window , &control ) ;
656 {
657 if ( AcceptsFocus() && FindFocus() != this )
658 {
659 SetFocus() ;
660 }
661 if ( control && UMAIsControlActive( control ) )
662 {
663 {
664 if ( controlpart == kControlIndicatorPart && !UMAHasAppearance() )
665 controlpart = UMAHandleControlClick( control , localwhere , modifiers , (ControlActionUPP) NULL ) ;
666 else
667 controlpart = UMAHandleControlClick( control , localwhere , modifiers , (ControlActionUPP) -1 ) ;
668 wxTheApp->s_lastMouseDown = 0 ;
669 if ( controlpart && ! ( ( UMAHasAppearance() || (controlpart != kControlIndicatorPart) )
670 && (IsKindOf( CLASSINFO( wxScrollBar ) ) ) ) ) // otherwise we will get the event twice
671 {
672 MacHandleControlClick( control , controlpart ) ;
673 }
674 }
675 }
676 }
677 }
678 }
679
680 bool wxControl::MacCanFocus() const
681 {
682 { if ( m_macControl == NULL )
683 return true ;
684 else
685 return false ;
686 }
687 }
688
689 void wxControl::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
690 {
691 wxASSERT_MSG( m_macControl != NULL , "No valid mac control" ) ;
692 }
693