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