]> git.saurik.com Git - wxWidgets.git/blob - src/mac/choice.cpp
Copied/merged from the 2.2 branch.
[wxWidgets.git] / src / mac / choice.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: choice.cpp
3 // Purpose: wxChoice
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 "choice.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/choice.h"
18 #include "wx/menu.h"
19 #include "wx/mac/uma.h"
20
21 #if !USE_SHARED_LIBRARY
22 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
23 #endif
24
25 short nextMenuId = 100 ; // wxMenu takes the lower ids
26
27 wxChoice::~wxChoice()
28 {
29 // DeleteMenu( m_macPopUpMenuId ) ;
30 DisposeMenu( m_macPopUpMenuHandle ) ;
31 }
32
33 bool wxChoice::Create(wxWindow *parent, wxWindowID id,
34 const wxPoint& pos,
35 const wxSize& size,
36 int n, const wxString choices[],
37 long style,
38 const wxValidator& validator,
39 const wxString& name)
40 {
41
42 Rect bounds ;
43 Str255 title ;
44
45 MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ;
46
47 m_macPopUpMenuHandle = NewMenu( 1 , "\pPopUp Menu" ) ;
48 m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , title , true , 0 , -12345 , 0 ,
49 kControlPopupButtonProc + kControlPopupFixedWidthVariant , (long) this ) ;
50
51 m_macPopUpMenuHandle = NewMenu( 1 , "\pPopUp Menu" ) ;
52 SetControlData( m_macControl , kControlNoPart , kControlPopupButtonMenuHandleTag , sizeof( MenuHandle ) , (char*) &m_macPopUpMenuHandle) ;
53 for ( int i = 0 ; i < n ; i++ )
54 {
55 Str255 label;
56 wxMenuItem::MacBuildMenuString( label , NULL , NULL , choices[i] ,false);
57 AppendMenu( m_macPopUpMenuHandle , label ) ;
58 m_strings.Add( choices[i] ) ;
59 }
60 SetControlMinimum( m_macControl , 0 ) ;
61 SetControlMaximum( m_macControl , Number()) ;
62 if ( n > 0 )
63 SetControlValue( m_macControl , 1 ) ;
64
65 MacPostControlCreate() ;
66
67 return TRUE;
68 }
69
70 void wxChoice::Append(const wxString& item)
71 {
72 Str255 label;
73 wxMenuItem::MacBuildMenuString( label , NULL , NULL , item ,false);
74 AppendMenu( m_macPopUpMenuHandle , label ) ;
75 m_strings.Add( item ) ;
76 SetControlMaximum( m_macControl , Number()) ;
77 }
78
79 void wxChoice::Append(const wxString &item, void *client_data)
80 {
81 }
82
83 void *wxChoice::GetClientData(int index) const
84 {
85 return NULL;
86 }
87
88 void wxChoice::Delete(int n)
89 {
90 ::DeleteMenuItem( m_macPopUpMenuHandle , n + 1) ;
91 m_strings.Remove( n ) ;
92 SetControlMaximum( m_macControl , Number()) ;
93 }
94
95 void wxChoice::Clear()
96 {
97 for ( int i = 0 ; i < Number() ; i++ )
98 {
99 ::DeleteMenuItem( m_macPopUpMenuHandle , 1 ) ;
100 }
101 m_strings.Clear() ;
102 SetControlMaximum( m_macControl , Number()) ;
103 }
104
105 int wxChoice::GetSelection() const
106 {
107 return GetControlValue( m_macControl ) -1 ;
108 }
109
110 void wxChoice::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
111 {
112 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId );
113 event.SetInt(GetSelection());
114 event.SetEventObject(this);
115 event.SetString(GetStringSelection());
116 ProcessCommand(event);
117 }
118
119
120 void wxChoice::SetSelection(int n)
121 {
122 SetControlValue( m_macControl , n + 1 ) ;
123 }
124
125 int wxChoice::FindString(const wxString& s) const
126 {
127 for( int i = 0 ; i < Number() ; i++ )
128 {
129 if ( GetString( i ) == s )
130 return i ;
131 }
132 return -1;
133 }
134
135 wxString wxChoice::GetString(int n) const
136 {
137 return m_strings[n] ;
138 }
139
140 void wxChoice::SetSize(int x, int y, int width, int height, int sizeFlags)
141 {
142 wxControl::SetSize( x,y,width,height,sizeFlags ) ;
143 }
144
145 wxString wxChoice::GetStringSelection () const
146 {
147 int sel = GetSelection ();
148 if (sel > -1)
149 return wxString(this->GetString (sel));
150 else
151 return wxString("");
152 }
153
154 bool wxChoice::SetStringSelection (const wxString& s)
155 {
156 int sel = FindString (s);
157 if (sel > -1)
158 {
159 SetSelection (sel);
160 return TRUE;
161 }
162 else
163 return FALSE;
164 }
165
166 void wxChoice::Command(wxCommandEvent & event)
167 {
168 SetSelection (event.GetInt());
169 ProcessCommand (event);
170 }
171