]> git.saurik.com Git - wxWidgets.git/blame - src/mac/choice.cpp
1. added native wxMessageDialog implementation for GTK+2
[wxWidgets.git] / src / mac / choice.cpp
CommitLineData
2ac013b1 1/////////////////////////////////////////////////////////////////////////////
e9576ca5
SC
2// Name: choice.cpp
3// Purpose: wxChoice
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
e9576ca5
SC
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
e9576ca5
SC
11#ifdef __GNUG__
12#pragma implementation "choice.h"
13#endif
d8c736e5 14#include "wx/defs.h"
e9576ca5 15#include "wx/choice.h"
03e11df5 16#include "wx/menu.h"
519cb848 17#include "wx/mac/uma.h"
2f1ae414 18#if !USE_SHARED_LIBRARY
e9576ca5 19IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
2f1ae414 20#endif
892e461e 21extern MenuHandle NewUniqueMenu() ;
5b781a67
SC
22wxChoice::~wxChoice()
23{
d76240bf
MB
24 if ( HasClientObjectData() )
25 {
26 size_t i, max = GetCount();
27
28 for ( i = 0; i < max; ++i )
29 delete GetClientObject(i);
30 }
31
32 // DeleteMenu( m_macPopUpMenuId ) ;
e7b596fb 33 // DisposeMenu( m_macPopUpMenuHandle ) ;
5b781a67 34}
e9576ca5
SC
35bool wxChoice::Create(wxWindow *parent, wxWindowID id,
36 const wxPoint& pos,
37 const wxSize& size,
03e11df5
GD
38 int n, const wxString choices[],
39 long style,
e9576ca5
SC
40 const wxValidator& validator,
41 const wxString& name)
42{
519cb848
SC
43 Rect bounds ;
44 Str255 title ;
45
46 MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ;
76a5e5d2 47 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , 0 , -12345 , 0 ,
8208e181 48 kControlPopupButtonProc + kControlPopupFixedWidthVariant , (long) this ) ;
519cb848 49
892e461e 50 m_macPopUpMenuHandle = NewUniqueMenu() ;
76a5e5d2 51 SetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlPopupButtonMenuHandleTag , sizeof( MenuHandle ) , (char*) &m_macPopUpMenuHandle) ;
467e3168
SC
52 SetControl32BitMinimum( (ControlHandle) m_macControl , 0 ) ;
53 SetControl32BitMaximum( (ControlHandle) m_macControl , 0) ;
2f1ae414 54 if ( n > 0 )
467e3168 55 SetControl32BitValue( (ControlHandle) m_macControl , 1 ) ;
519cb848 56 MacPostControlCreate() ;
b668a735
SC
57 for ( int i = 0; i < n; i++ )
58 {
59 Append(choices[i]);
60 }
519cb848 61 return TRUE;
e9576ca5 62}
b668a735
SC
63// ----------------------------------------------------------------------------
64// adding/deleting items to/from the list
65// ----------------------------------------------------------------------------
2597135a 66int wxChoice::DoAppend(const wxString& item)
e9576ca5 67{
2e97b58c 68 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item);
2f1ae414 69 m_strings.Add( item ) ;
b668a735
SC
70 m_datas.Add( NULL ) ;
71 int index = m_strings.GetCount() - 1 ;
72 DoSetItemClientData( index , NULL ) ;
467e3168 73 SetControl32BitMaximum( (ControlHandle) m_macControl , GetCount()) ;
b668a735 74 return index ;
2597135a 75}
b668a735 76void wxChoice::Delete(int n)
2597135a 77{
b668a735 78 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
b668a735 79 if ( HasClientObjectData() )
2597135a 80 {
b668a735 81 delete GetClientObject(n);
2597135a 82 }
76a5e5d2 83 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1) ;
2f1ae414 84 m_strings.Remove( n ) ;
3ef585df 85 m_datas.RemoveAt( n ) ;
467e3168 86 SetControl32BitMaximum( (ControlHandle) m_macControl , GetCount()) ;
e9576ca5 87}
e9576ca5
SC
88void wxChoice::Clear()
89{
4b651a46 90 FreeData();
b668a735 91 for ( int i = 0 ; i < GetCount() ; i++ )
519cb848 92 {
76a5e5d2 93 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , 1 ) ;
4b651a46 94 }
b668a735
SC
95 m_strings.Empty() ;
96 m_datas.Empty() ;
467e3168 97 SetControl32BitMaximum( (ControlHandle) m_macControl , 0 ) ;
e9576ca5 98}
4b651a46 99void wxChoice::FreeData()
b668a735
SC
100{
101 if ( HasClientObjectData() )
102 {
103 size_t count = GetCount();
104 for ( size_t n = 0; n < count; n++ )
105 {
106 delete GetClientObject(n);
107 }
108 }
109}
b668a735
SC
110// ----------------------------------------------------------------------------
111// selection
112// ----------------------------------------------------------------------------
e9576ca5
SC
113int wxChoice::GetSelection() const
114{
467e3168 115 return GetControl32BitValue( (ControlHandle) m_macControl ) -1 ;
e9576ca5 116}
b668a735 117void wxChoice::SetSelection(int n)
519cb848 118{
467e3168 119 SetControl32BitValue( (ControlHandle) m_macControl , n + 1 ) ;
519cb848 120}
b668a735
SC
121// ----------------------------------------------------------------------------
122// string list functions
123// ----------------------------------------------------------------------------
b668a735 124int wxChoice::GetCount() const
e9576ca5 125{
b668a735 126 return m_strings.GetCount() ;
e9576ca5 127}
e9576ca5
SC
128int wxChoice::FindString(const wxString& s) const
129{
b668a735 130 for( int i = 0 ; i < GetCount() ; i++ )
519cb848 131 {
b668a735 132 if ( GetString( i ).IsSameAs(s, FALSE) )
519cb848
SC
133 return i ;
134 }
b668a735
SC
135 return wxNOT_FOUND ;
136}
b668a735
SC
137void wxChoice::SetString(int n, const wxString& s)
138{
4b651a46 139 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
b668a735
SC
140#if 0 // should do this, but no Insert() so far
141 Delete(n);
142 Insert(n + 1, s);
143#endif
e9576ca5
SC
144}
145
146wxString wxChoice::GetString(int n) const
147{
2f1ae414 148 return m_strings[n] ;
e9576ca5 149}
b668a735
SC
150// ----------------------------------------------------------------------------
151// client data
152// ----------------------------------------------------------------------------
b668a735
SC
153void wxChoice::DoSetItemClientData( int n, void* clientData )
154{
d81cc883 155 wxCHECK_RET( n >= 0 && (size_t)n < m_datas.GetCount(),
b668a735 156 "invalid index in wxChoice::SetClientData" );
b668a735 157
d81cc883 158 m_datas[n] = (char*) clientData ;
b668a735 159}
d81cc883 160void *wxChoice::DoGetItemClientData(int n) const
b668a735 161{
d81cc883 162 wxCHECK_MSG( n >= 0 && (size_t)n < m_datas.GetCount(), NULL,
b668a735 163 "invalid index in wxChoice::GetClientData" );
d81cc883 164 return (void *)m_datas[n];
b668a735 165}
b668a735
SC
166void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
167{
168 DoSetItemClientData(n, clientData);
169}
b668a735 170wxClientData* wxChoice::DoGetItemClientObject( int n ) const
e9576ca5 171{
b668a735 172 return (wxClientData *)DoGetItemClientData(n);
e9576ca5 173}
76a5e5d2 174void wxChoice::MacHandleControlClick( WXWidget control , wxInt16 controlpart )
b668a735
SC
175{
176 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId );
f2d990ad
SC
177 int n = GetSelection();
178 // actually n should be made sure by the os to be a valid selection, but ...
179 if ( n > -1 )
180 {
181 event.SetInt( n );
182 event.SetString(GetStringSelection());
183 event.SetEventObject(this);
f2d990ad
SC
184 if ( HasClientObjectData() )
185 event.SetClientObject( GetClientObject(n) );
186 else if ( HasClientUntypedData() )
187 event.SetClientData( GetClientData(n) );
f2d990ad
SC
188 ProcessCommand(event);
189 }
9453cf2b 190}
f2d990ad 191wxSize wxChoice::DoGetBestSize() const
b668a735 192{
f2d990ad
SC
193 int lbWidth = 100; // some defaults
194 int lbHeight = 20;
195 int wLine;
196#if TARGET_CARBON
197 long metric ;
198 GetThemeMetric(kThemeMetricPopupButtonHeight , &metric );
199 lbHeight = metric ;
200#endif
201 {
202 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetRootWindow() ) ) ;
203 Rect drawRect ;
f2d990ad 204 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
f2d990ad
SC
205 if ( font )
206 {
207 ::TextFont( font->m_macFontNum ) ;
208 ::TextSize( short(font->m_macFontSize) ) ;
209 ::TextFace( font->m_macFontStyle ) ;
210 }
211 else
212 {
213 ::TextFont( kFontIDMonaco ) ;
214 ::TextSize( 9 );
215 ::TextFace( 0 ) ;
216 }
f2d990ad
SC
217 // Find the widest line
218 for(int i = 0; i < GetCount(); i++) {
219 wxString str(GetString(i));
220 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
221 lbWidth = wxMax(lbWidth, wLine);
222 }
f2d990ad
SC
223 // Add room for the popup arrow
224 lbWidth += 2 * lbHeight ;
f2d990ad
SC
225 // And just a bit more
226 int cy = 12 ;
227 int cx = ::TextWidth( "X" , 0 , 1 ) ;
228 lbWidth += cx ;
229
230 }
231 return wxSize(lbWidth, lbHeight);
d76240bf 232}