]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/control.cpp
compilation fix for wxUSE_DYNLOAD
[wxWidgets.git] / src / mac / control.cpp
... / ...
CommitLineData
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/defs.h"
17
18#include "wx/control.h"
19#include "wx/panel.h"
20#include "wx/app.h"
21#include "wx/dc.h"
22#include "wx/notebook.h"
23#include "wx/tabctrl.h"
24#include "wx/radiobox.h"
25#include "wx/spinbutt.h"
26#include "wx/scrolbar.h"
27#include "wx/button.h"
28#include "wx/dialog.h"
29#include "wx/statbox.h"
30#include "wx/sizer.h"
31#include "wx/stattext.h"
32
33#if !USE_SHARED_LIBRARY
34IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
35
36BEGIN_EVENT_TABLE(wxControl, wxWindow)
37 EVT_MOUSE_EVENTS( wxControl::OnMouseEvent )
38 EVT_CHAR( wxControl::OnKeyDown )
39 EVT_PAINT( wxControl::OnPaint )
40END_EVENT_TABLE()
41#endif
42
43#include "wx/mac/uma.h"
44
45// Item members
46
47ControlActionUPP wxMacLiveScrollbarActionUPP = NULL ;
48
49pascal void wxMacLiveScrollbarActionProc( ControlHandle control , ControlPartCode partCode ) ;
50pascal void wxMacLiveScrollbarActionProc( ControlHandle control , ControlPartCode partCode )
51{
52 if ( partCode != 0)
53 {
54 wxControl* wx = (wxControl*) GetControlReference( control ) ;
55 if ( wx )
56 {
57 wx->MacHandleControlClick( control , partCode ) ;
58 }
59 }
60}
61
62wxControl::wxControl()
63{
64 m_macControl = NULL ;
65 m_macHorizontalBorder = 0 ; // additional pixels around the real control
66 m_macVerticalBorder = 0 ;
67 m_backgroundColour = *wxWHITE;
68 m_foregroundColour = *wxBLACK;
69#if WXWIN_COMPATIBILITY
70 m_callback = 0;
71#endif // WXWIN_COMPATIBILITY
72
73 if ( wxMacLiveScrollbarActionUPP == NULL )
74 {
75#if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
76 wxMacLiveScrollbarActionUPP = NewControlActionUPP( wxMacLiveScrollbarActionProc );
77#else
78 wxMacLiveScrollbarActionUPP = NewControlActionProc( wxMacLiveScrollbarActionProc ) ;
79#endif
80 }
81}
82
83bool wxControl::Create(wxWindow *parent, wxWindowID id,
84 const wxPoint& pos,
85 const wxSize& size, long style,
86 const wxValidator& validator,
87 const wxString& name)
88{
89 m_macControl = NULL ;
90 m_macHorizontalBorder = 0 ; // additional pixels around the real control
91 m_macVerticalBorder = 0 ;
92 bool rval = wxWindow::Create(parent, id, pos, size, style, name);
93 if (rval) {
94#if wxUSE_VALIDATORS
95 SetValidator(validator);
96#endif
97 }
98 return rval;
99}
100
101wxControl::~wxControl()
102{
103 m_isBeingDeleted = TRUE;
104 // If we delete an item, we should initialize the parent panel,
105 // because it could now be invalid.
106 wxWindow *parent = GetParent() ;
107 if ( parent )
108 {
109 if (parent->GetDefaultItem() == (wxButton*) this)
110 parent->SetDefaultItem(NULL);
111 }
112 if ( m_macControl )
113 {
114 ::DisposeControl( m_macControl ) ;
115 m_macControl = NULL ;
116 }
117}
118
119void wxControl::SetLabel(const wxString& title)
120{
121 m_label = title ;
122
123 if ( m_macControl )
124 {
125 Str255 maclabel ;
126 wxString label ;
127
128 if( wxApp::s_macDefaultEncodingIsPC )
129 label = wxMacMakeMacStringFromPC( title ) ;
130 else
131 label = title ;
132
133#if TARGET_CARBON
134 c2pstrcpy( (StringPtr) maclabel , label ) ;
135#else
136 strcpy( (char *) maclabel , label ) ;
137 c2pstr( (char *) maclabel ) ;
138#endif
139 ::SetControlTitle( m_macControl , maclabel ) ;
140 }
141 Refresh() ;
142}
143
144wxSize wxControl::DoGetBestSize() const
145{
146 Rect bestsize = { 0 , 0 , 0 , 0 } ;
147 short baselineoffset ;
148 int bestWidth, bestHeight ;
149 ::GetBestControlRect( m_macControl , &bestsize , &baselineoffset ) ;
150
151 if ( EmptyRect( &bestsize ) )
152 {
153 baselineoffset = 0;
154 bestsize.left = bestsize.top = 0 ;
155 bestsize.right = 16 ;
156 bestsize.bottom = 16 ;
157 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
158 {
159 bestsize.bottom = 16 ;
160 }
161 else if ( IsKindOf( CLASSINFO( wxSpinButton ) ) )
162 {
163 bestsize.bottom = 24 ;
164 }
165 }
166
167 if ( IsKindOf( CLASSINFO( wxButton ) ) )
168 {
169 bestWidth = m_label.Length() * 8 + 12 ;
170 if ( bestWidth < 70 )
171 bestWidth = 70 ;
172 }
173 else if ( IsKindOf( CLASSINFO( wxStaticText ) ) )
174 {
175 bestWidth = m_label.Length() * 8 ;
176 }
177 else
178 bestWidth = bestsize.right - bestsize.left ;
179
180 bestWidth += 2 * m_macHorizontalBorder ;
181
182 bestHeight = bestsize.bottom - bestsize.top ;
183 if ( bestHeight < 10 )
184 bestHeight = 13 ;
185
186 bestHeight += 2 * m_macVerticalBorder;
187
188
189 return wxSize(bestWidth, bestHeight);
190}
191
192bool wxControl::ProcessCommand (wxCommandEvent & event)
193{
194 // Tries:
195 // 1) A callback function (to become obsolete)
196 // 2) OnCommand, starting at this window and working up parent hierarchy
197 // 3) OnCommand then calls ProcessEvent to search the event tables.
198#if WXWIN_COMPATIBILITY
199 if ( m_callback )
200 {
201 (void)(*m_callback)(this, event);
202
203 return TRUE;
204 }
205 else
206#endif // WXWIN_COMPATIBILITY
207 {
208 return GetEventHandler()->ProcessEvent(event);
209 }
210}
211
212// ------------------------
213wxList *wxWinMacControlList = NULL;
214wxControl *wxFindControlFromMacControl(ControlHandle inControl )
215{
216 wxNode *node = wxWinMacControlList->Find((long)inControl);
217 if (!node)
218 return NULL;
219 return (wxControl *)node->Data();
220}
221
222void wxAssociateControlWithMacControl(ControlHandle inControl, wxControl *control)
223{
224 // adding NULL WindowRef is (first) surely a result of an error and
225 // (secondly) breaks menu command processing
226 wxCHECK_RET( inControl != (ControlHandle) NULL, "attempt to add a NULL WindowRef to window list" );
227
228 if ( !wxWinMacControlList->Find((long)inControl) )
229 wxWinMacControlList->Append((long)inControl, control);
230}
231
232void wxRemoveMacControlAssociation(wxControl *control)
233{
234 wxWinMacControlList->DeleteObject(control);
235}
236
237void wxControl::MacPreControlCreate( wxWindow *parent, wxWindowID id, wxString label ,
238 const wxPoint& pos,
239 const wxSize& size, long style,
240 const wxValidator& validator,
241 const wxString& name , Rect *outBounds , StringPtr maclabel )
242{
243 m_label = label ;
244 SetName(name);
245 if ( &validator )
246 SetValidator(validator);
247
248 m_windowStyle = style;
249 parent->AddChild(this);
250
251 m_backgroundColour = parent->GetBackgroundColour() ;
252 m_foregroundColour = parent->GetForegroundColour() ;
253
254 if (id == -1)
255 m_windowId = NewControlId();
256 else
257 m_windowId = id;
258
259 // These sizes will be adjusted in MacPostControlCreate
260
261 m_width = size.x ;
262 m_height = size.y ;
263 m_x = pos.x ;
264 m_y = pos.y ;
265
266 outBounds->top = -10;
267 outBounds->left = -10;
268 outBounds->bottom = 0;
269 outBounds->right = 0;
270
271 char c_text[255];
272 strcpy( c_text , label ) ;
273 if( wxApp::s_macDefaultEncodingIsPC )
274 {
275 wxMacConvertFromPCForControls( c_text ) ;
276 }
277
278#if TARGET_CARBON
279 c2pstrcpy( (StringPtr) maclabel , c_text ) ;
280#else
281 strcpy( (char *) maclabel , c_text ) ;
282 c2pstr( (char *) maclabel ) ;
283#endif
284}
285
286void wxControl::MacPostControlCreate()
287{
288 wxASSERT_MSG( m_macControl != NULL , "No valid mac control" ) ;
289
290 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
291 {
292 // no font
293 }
294 else if ( IsKindOf( CLASSINFO( wxStaticBox ) ) || IsKindOf( CLASSINFO( wxRadioBox ) ) || IsKindOf( CLASSINFO( wxButton ) ) )
295 {
296 ControlFontStyleRec controlstyle ;
297 controlstyle.flags = kControlUseFontMask ;
298 controlstyle.font = kControlFontSmallBoldSystemFont ;
299
300 ::SetControlFontStyle( m_macControl , &controlstyle ) ;
301 }
302 else
303 {
304 ControlFontStyleRec controlstyle ;
305 controlstyle.flags = kControlUseFontMask ;
306 controlstyle.font = kControlFontSmallSystemFont ;
307
308 ::SetControlFontStyle( m_macControl , &controlstyle ) ;
309 }
310 ControlHandle container = GetParent()->MacGetContainerForEmbedding() ;
311 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
312 ::EmbedControl( m_macControl , container ) ;
313 m_macControlIsShown = true ;
314
315 wxAssociateControlWithMacControl( m_macControl , this ) ;
316
317
318 // Adjust the controls size and position
319 wxPoint pos(m_x, m_y);
320 wxSize best_size( DoGetBestSize() );
321 wxSize new_size( m_width, m_height );
322
323 m_x = m_y = m_width = m_height = -1; // Forces SetSize to move/size the control
324
325 if (new_size.x == -1) {
326 new_size.x = best_size.x;
327 }
328 if (new_size.y == -1) {
329 new_size.y = best_size.y;
330 }
331
332 SetSize(pos.x, pos.y, new_size.x, new_size.y);
333
334 UMAShowControl( m_macControl ) ;
335 Refresh() ;
336}
337
338void wxControl::MacAdjustControlRect()
339{
340 wxASSERT_MSG( m_macControl != NULL , wxT("No valid mac control") ) ;
341 if ( m_width == -1 || m_height == -1 )
342 {
343 Rect bestsize = { 0 , 0 , 0 , 0 } ;
344 short baselineoffset ;
345
346 ::GetBestControlRect( m_macControl , &bestsize , &baselineoffset ) ;
347
348 if ( EmptyRect( &bestsize ) )
349 {
350 baselineoffset = 0;
351 bestsize.left = bestsize.top = 0 ;
352 bestsize.right = 16 ;
353 bestsize.bottom = 16 ;
354 if ( IsKindOf( CLASSINFO( wxScrollBar ) ) )
355 {
356 bestsize.bottom = 16 ;
357 }
358 else if ( IsKindOf( CLASSINFO( wxSpinButton ) ) )
359 {
360 bestsize.bottom = 24 ;
361 }
362 }
363
364 if ( m_width == -1 )
365 {
366 if ( IsKindOf( CLASSINFO( wxButton ) ) )
367 {
368 m_width = m_label.Length() * 8 + 12 ;
369 if ( m_width < 70 )
370 m_width = 70 ;
371 }
372 else if ( IsKindOf( CLASSINFO( wxStaticText ) ) )
373 {
374 m_width = m_label.Length() * 8 ;
375 }
376 else
377 m_width = bestsize.right - bestsize.left ;
378
379 m_width += 2 * m_macHorizontalBorder ;
380 }
381 if ( m_height == -1 )
382 {
383 m_height = bestsize.bottom - bestsize.top ;
384 if ( m_height < 10 )
385 m_height = 13 ;
386
387 m_height += 2 * m_macVerticalBorder;
388 }
389
390 UMASizeControl( m_macControl , m_width - 2 * m_macHorizontalBorder, m_height - 2 * m_macVerticalBorder ) ;
391 }
392}
393ControlHandle wxControl::MacGetContainerForEmbedding()
394{
395 if ( m_macControl )
396 return m_macControl ;
397
398 return wxWindow::MacGetContainerForEmbedding() ;
399}
400
401void wxControl::MacSuperChangedPosition()
402{
403 if ( m_macControl )
404 {
405 Rect contrlRect ;
406 GetControlBounds( m_macControl , &contrlRect ) ;
407 int former_mac_x = contrlRect.left ;
408 int former_mac_y = contrlRect.top ;
409 int mac_x = m_x ;
410 int mac_y = m_y ;
411 GetParent()->MacWindowToRootWindow( & mac_x , & mac_y ) ;
412
413 WindowRef rootwindow = MacGetRootWindow() ;
414
415 if ( mac_x + m_macHorizontalBorder != former_mac_x ||
416 mac_y + m_macVerticalBorder != former_mac_y )
417 {
418 {
419 Rect inval = { former_mac_y , former_mac_x , former_mac_y + m_height , former_mac_x + m_width } ;
420 InvalWindowRect( rootwindow , &inval ) ;
421 }
422 UMAMoveControl( m_macControl , mac_x + m_macHorizontalBorder , mac_y + m_macVerticalBorder ) ;
423 {
424 Rect inval = { mac_y , mac_x , mac_y + m_height , mac_x + m_width } ;
425 InvalWindowRect( rootwindow , &inval ) ;
426 }
427 }
428 }
429
430 wxWindow::MacSuperChangedPosition() ;
431}
432
433void wxControl::MacSuperEnabled( bool enabled )
434{
435 Refresh(FALSE) ;
436 wxWindow::MacSuperEnabled( enabled ) ;
437}
438
439void wxControl::MacSuperShown( bool show )
440{
441 if ( m_macControl )
442 {
443 if ( !show )
444 {
445 if ( m_macControlIsShown )
446 {
447 ::UMAHideControl( m_macControl ) ;
448 m_macControlIsShown = false ;
449 }
450 }
451 else
452 {
453 if ( MacIsReallyShown() && !m_macControlIsShown )
454 {
455 ::UMAShowControl( m_macControl ) ;
456 m_macControlIsShown = true ;
457 }
458 }
459 }
460
461 wxWindow::MacSuperShown( show ) ;
462}
463
464void wxControl::DoSetSize(int x, int y,
465 int width, int height,
466 int sizeFlags )
467{
468 if ( m_macControl == NULL )
469 {
470 wxWindow::DoSetSize( x , y ,width , height ,sizeFlags ) ;
471 return ;
472 }
473
474 Rect oldbounds, newbounds;
475 int new_x, new_y, new_width, new_height;
476 int mac_x, mac_y;
477
478 new_x = m_x;
479 new_y = m_y;
480 new_width = m_width;
481 new_height = m_height;
482
483 if (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)
484 {
485 new_x = x;
486 new_y = y;
487 new_width = width;
488 new_height = height;
489 }
490 else
491 {
492 if (x != -1) new_x = x;
493 if (y != -1) new_y = y;
494 if (width != -1) new_width = width;
495 if (height != -1) new_height = height;
496 }
497
498 if(sizeFlags & wxSIZE_AUTO)
499 {
500 wxSize size = GetBestSize();
501 if (sizeFlags & wxSIZE_AUTO_WIDTH)
502 {
503 if (width == -1) new_width = size.x;
504 }
505 if (sizeFlags & wxSIZE_AUTO_HEIGHT)
506 {
507 if (height == -1) new_height = size.y;
508 }
509 }
510 AdjustForParentClientOrigin(new_x, new_y, sizeFlags);
511
512 mac_x = new_x;
513 mac_y = new_y;
514 if(GetParent()) {
515 GetParent()->MacWindowToRootWindow(&mac_x, &mac_y);
516 }
517 GetControlBounds(m_macControl, &oldbounds);
518 oldbounds.right = oldbounds.left + m_width;
519 oldbounds.bottom = oldbounds.top + m_height;
520
521 bool doMove = false;
522 bool doResize = false;
523
524 if ( mac_x != (oldbounds.left - m_macHorizontalBorder) ||
525 mac_y != (oldbounds.top - m_macVerticalBorder) )
526 {
527 doMove = true ;
528 }
529 if ( new_width != oldbounds.right - oldbounds.left - 2 * m_macHorizontalBorder ||
530 new_height != oldbounds.bottom - oldbounds.top - 2 * m_macVerticalBorder)
531 {
532 doResize = true ;
533 }
534
535 if ( doMove || doResize )
536 {
537 Refresh() ;
538
539 // Ensure resize is within constraints
540 if ((m_minWidth != -1) && (new_width < m_minWidth)) {
541 new_width = m_minWidth;
542 }
543 if ((m_minHeight != -1) && (new_height < m_minHeight)) {
544 new_height = m_minHeight;
545 }
546 if ((m_maxWidth != -1) && (new_width > m_maxWidth)) {
547 new_width = m_maxWidth;
548 }
549 if ((m_maxHeight != -1) && (new_height > m_maxHeight)) {
550 new_height = m_maxHeight;
551 }
552
553 if ( doMove )
554 {
555 m_x = new_x;
556 m_y = new_y;
557
558 UMAMoveControl(m_macControl,
559 mac_x + m_macHorizontalBorder, mac_y + m_macVerticalBorder);
560
561 wxMoveEvent event(wxPoint(m_x, m_y), m_windowId);
562 event.SetEventObject(this);
563 GetEventHandler()->ProcessEvent(event) ;
564 }
565 if ( doResize )
566 {
567 m_width = new_width;
568 m_height = new_height;
569
570 UMASizeControl( m_macControl,
571 m_width - 2 * m_macHorizontalBorder,
572 m_height - 2 * m_macVerticalBorder ) ;
573
574
575 wxSizeEvent event(wxSize(m_width, m_height), m_windowId);
576 event.SetEventObject(this);
577 GetEventHandler()->ProcessEvent(event);
578 }
579
580 Refresh() ;
581 }
582}
583
584bool wxControl::Show(bool show)
585{
586 if ( !wxWindow::Show( show ) )
587 return FALSE ;
588
589 if ( m_macControl )
590 {
591 if ( !show )
592 {
593 if ( m_macControlIsShown )
594 {
595 ::UMAHideControl( m_macControl ) ;
596 m_macControlIsShown = false ;
597 }
598 }
599 else
600 {
601 if ( MacIsReallyShown() && !m_macControlIsShown )
602 {
603 ::UMAShowControl( m_macControl ) ;
604 m_macControlIsShown = true ;
605 }
606 }
607 }
608 return TRUE ;
609}
610
611bool wxControl::Enable(bool enable)
612{
613 if ( !wxWindow::Enable(enable) )
614 return FALSE;
615
616 if ( m_macControl )
617 {
618 if ( enable )
619 UMAActivateControl( m_macControl ) ;
620 else
621 UMADeactivateControl( m_macControl ) ;
622 }
623 return TRUE ;
624}
625
626void wxControl::Refresh(bool eraseBack, const wxRect *rect)
627{
628 wxWindow::Refresh( eraseBack , rect ) ;
629}
630
631void wxControl::MacRedrawControl()
632{
633 if ( m_macControl && MacGetRootWindow() )
634 {
635 wxClientDC dc(this) ;
636 wxMacPortSetter helper(&dc) ;
637
638 // the controls sometimes draw outside their boundaries, this
639 // should be resolved differently but is not trivial (e.g. drop shadows)
640 // since adding them to the border would yield in enormous gaps between
641 // the controls
642 Rect r = { 0 , 0 , 32000 , 32000 } ;
643 ClipRect( &r ) ;
644 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
645 UMADrawControl( m_macControl ) ;
646 }
647}
648
649void wxControl::OnPaint(wxPaintEvent& event)
650{
651 if ( m_macControl )
652 {
653 wxPaintDC dc(this) ;
654 wxMacPortSetter helper(&dc) ;
655 // the controls sometimes draw outside their boundaries, this
656 // should be resolved differently but is not trivial (e.g. drop shadows)
657 // since adding them to the border would yield in enormous gaps between
658 // the controls
659 Rect r = { 0 , 0 , 32000 , 32000 } ;
660 ClipRect( &r ) ;
661
662 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
663 UMADrawControl( m_macControl ) ;
664 }
665 else
666 {
667 event.Skip() ;
668 }
669}
670void wxControl::OnEraseBackground(wxEraseEvent& event)
671{
672 wxWindow::OnEraseBackground( event ) ;
673}
674
675
676void wxControl::OnKeyDown( wxKeyEvent &event )
677{
678 if ( m_macControl == NULL )
679 return ;
680
681 EventRecord *ev = wxTheApp->MacGetCurrentEvent() ;
682 short keycode ;
683 short keychar ;
684 keychar = short(ev->message & charCodeMask);
685 keycode = short(ev->message & keyCodeMask) >> 8 ;
686
687 ::HandleControlKey( m_macControl , keycode , keychar , ev->modifiers ) ;
688}
689
690void wxControl::OnMouseEvent( wxMouseEvent &event )
691{
692 if ( m_macControl == NULL )
693 {
694 event.Skip() ;
695 return ;
696 }
697
698 if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK )
699 {
700
701 int x = event.m_x ;
702 int y = event.m_y ;
703
704 MacClientToRootWindow( &x , &y ) ;
705
706 ControlHandle control ;
707 Point localwhere ;
708 SInt16 controlpart ;
709 WindowRef window = MacGetRootWindow() ;
710
711 localwhere.h = x ;
712 localwhere.v = y ;
713
714 short modifiers = 0;
715
716 if ( !event.m_leftDown && !event.m_rightDown )
717 modifiers |= btnState ;
718
719 if ( event.m_shiftDown )
720 modifiers |= shiftKey ;
721
722 if ( event.m_controlDown )
723 modifiers |= controlKey ;
724
725 if ( event.m_altDown )
726 modifiers |= optionKey ;
727
728 if ( event.m_metaDown )
729 modifiers |= cmdKey ;
730/*
731#if TARGET_CARBON
732 control = FindControlUnderMouse( localwhere , window , &controlpart ) ;
733#else
734 controlpart = FindControl( localwhere , window , &control ) ;
735#endif
736*/
737 {
738 /*
739 if ( AcceptsFocus() && FindFocus() != this )
740 {
741 SetFocus() ;
742 }
743 */
744 control = m_macControl ;
745 if ( control && ::IsControlActive( control ) )
746 {
747 {
748 controlpart = ::HandleControlClick( control , localwhere , modifiers , (ControlActionUPP) -1 ) ;
749 wxTheApp->s_lastMouseDown = 0 ;
750 if ( control && controlpart != kControlNoPart &&
751 ! IsKindOf( CLASSINFO( wxScrollBar ) )
752 ) // otherwise we will get the event twice for scrollbar
753 {
754 MacHandleControlClick( control , controlpart ) ;
755 }
756 }
757 }
758 }
759 }
760}
761
762bool wxControl::MacCanFocus() const
763{
764 { if ( m_macControl == NULL )
765 return true ;
766 else
767 return false ;
768 }
769}
770
771void wxControl::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
772{
773 wxASSERT_MSG( m_macControl != NULL , "No valid mac control" ) ;
774}
775