]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: choice.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
dbf858b5 | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
c801d85f KB |
7 | // Licence: wxWindows licence |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "choice.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/choice.h" | |
16 | ||
66bd6b93 RR |
17 | //----------------------------------------------------------------------------- |
18 | // data | |
19 | //----------------------------------------------------------------------------- | |
20 | ||
21 | extern bool g_blockEventsOnDrag; | |
22 | ||
c801d85f | 23 | //----------------------------------------------------------------------------- |
e1e955e1 | 24 | // "activate" |
c801d85f KB |
25 | //----------------------------------------------------------------------------- |
26 | ||
66bd6b93 | 27 | static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice ) |
c801d85f | 28 | { |
66bd6b93 RR |
29 | if (!choice->HasVMT()) return; |
30 | if (g_blockEventsOnDrag) return; | |
31 | ||
47908e25 | 32 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() ); |
c801d85f KB |
33 | event.SetInt( choice->GetSelection() ); |
34 | wxString tmp( choice->GetStringSelection() ); | |
35 | event.SetString( WXSTRINGCAST(tmp) ); | |
36 | event.SetEventObject(choice); | |
47908e25 | 37 | choice->GetEventHandler()->ProcessEvent(event); |
6de97a3b | 38 | } |
c801d85f | 39 | |
e1e955e1 RR |
40 | //----------------------------------------------------------------------------- |
41 | // wxChoice | |
c801d85f KB |
42 | //----------------------------------------------------------------------------- |
43 | ||
7f4dc78d | 44 | IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl) |
c801d85f | 45 | |
fd0eed64 | 46 | wxChoice::wxChoice() |
c801d85f | 47 | { |
6de97a3b | 48 | } |
c801d85f | 49 | |
debe6624 | 50 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, |
fd0eed64 RR |
51 | const wxPoint &pos, const wxSize &size, |
52 | int n, const wxString choices[], | |
53 | long style, const wxValidator& validator, const wxString &name ) | |
c801d85f | 54 | { |
fd0eed64 | 55 | m_needParent = TRUE; |
c801d85f | 56 | |
fd0eed64 | 57 | PreCreation( parent, id, pos, size, style, name ); |
c801d85f | 58 | |
fd0eed64 | 59 | SetValidator( validator ); |
6de97a3b | 60 | |
fd0eed64 | 61 | m_widget = gtk_option_menu_new(); |
c801d85f | 62 | |
fd0eed64 RR |
63 | wxSize newSize = size; |
64 | if (newSize.x == -1) newSize.x = 80; | |
65 | if (newSize.y == -1) newSize.y = 26; | |
66 | SetSize( newSize.x, newSize.y ); | |
c801d85f | 67 | |
fd0eed64 | 68 | GtkWidget *menu = gtk_menu_new(); |
c801d85f | 69 | |
fd0eed64 RR |
70 | for (int i = 0; i < n; i++) |
71 | { | |
72 | m_clientDataList.Append( (wxObject*) NULL ); | |
f96aa4d9 | 73 | |
fd0eed64 RR |
74 | GtkWidget *item = gtk_menu_item_new_with_label( choices[i] ); |
75 | gtk_menu_append( GTK_MENU(menu), item ); | |
58614078 | 76 | |
fd0eed64 RR |
77 | gtk_widget_realize( item ); |
78 | gtk_widget_realize( GTK_BIN(item)->child ); | |
58614078 | 79 | |
fd0eed64 RR |
80 | gtk_widget_show( item ); |
81 | ||
82 | gtk_signal_connect( GTK_OBJECT( item ), "activate", | |
83 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
84 | } | |
85 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
c801d85f | 86 | |
fd0eed64 | 87 | m_parent->AddChild( this ); |
6ca41e57 | 88 | |
fd0eed64 | 89 | (m_parent->m_insertCallback)( m_parent, this ); |
6ca41e57 | 90 | |
fd0eed64 | 91 | PostCreation(); |
c801d85f | 92 | |
fd0eed64 RR |
93 | SetBackgroundColour( parent->GetBackgroundColour() ); |
94 | SetForegroundColour( parent->GetForegroundColour() ); | |
f96aa4d9 | 95 | |
fd0eed64 | 96 | Show( TRUE ); |
c801d85f | 97 | |
fd0eed64 | 98 | return TRUE; |
6de97a3b | 99 | } |
c801d85f | 100 | |
fd0eed64 RR |
101 | wxChoice::~wxChoice() |
102 | { | |
103 | wxNode *node = m_clientDataList.First(); | |
104 | while (node) | |
105 | { | |
106 | wxClientData *cd = (wxClientData*)node->Data(); | |
107 | if (cd) delete cd; | |
108 | node = node->Next(); | |
109 | } | |
110 | m_clientDataList.Clear(); | |
111 | } | |
112 | ||
113 | void wxChoice::AppendCommon( const wxString &item ) | |
114 | { | |
115 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); | |
116 | ||
117 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); | |
118 | GtkWidget *menu_item = gtk_menu_item_new_with_label( item ); | |
119 | ||
120 | gtk_menu_append( GTK_MENU(menu), menu_item ); | |
121 | ||
122 | gtk_widget_realize( menu_item ); | |
123 | gtk_widget_realize( GTK_BIN(menu_item)->child ); | |
124 | ||
125 | if (m_widgetStyle) ApplyWidgetStyle(); | |
126 | ||
127 | gtk_signal_connect( GTK_OBJECT( menu_item ), "activate", | |
128 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
129 | ||
130 | gtk_widget_show( menu_item ); | |
131 | } | |
132 | ||
c801d85f KB |
133 | void wxChoice::Append( const wxString &item ) |
134 | { | |
fd0eed64 RR |
135 | m_clientDataList.Append( (wxObject*)NULL ); |
136 | ||
137 | AppendCommon( item ); | |
138 | } | |
139 | ||
140 | void wxChoice::Append( const wxString &item, void *clientData ) | |
141 | { | |
142 | if (clientData) | |
143 | m_clientDataList.Append( (wxObject*) new wxVoidClientData( clientData ) ); | |
144 | else | |
145 | m_clientDataList.Append( (wxObject*)NULL ); | |
146 | ||
147 | AppendCommon( item ); | |
148 | } | |
149 | ||
150 | void wxChoice::Append( const wxString &item, wxClientData *clientData ) | |
151 | { | |
152 | m_clientDataList.Append( (wxObject*) clientData ); | |
153 | ||
154 | AppendCommon( item ); | |
155 | } | |
f96aa4d9 | 156 | |
fd0eed64 RR |
157 | void wxChoice::SetClientData( int n, void* clientData ) |
158 | { | |
159 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
f96aa4d9 | 160 | |
fd0eed64 RR |
161 | wxNode *node = m_clientDataList.Nth( n ); |
162 | if (!node) return; | |
f96aa4d9 | 163 | |
fd0eed64 RR |
164 | wxClientData *cd = (wxClientData*) node->Data(); |
165 | if (cd) delete cd; | |
868a2826 | 166 | |
fd0eed64 RR |
167 | if (clientData) |
168 | node->SetData( (wxObject*) new wxVoidClientData(clientData) ); | |
169 | else | |
170 | node->SetData( (wxObject*) NULL ); | |
171 | } | |
172 | ||
173 | void* wxChoice::GetClientData( int n ) | |
174 | { | |
175 | wxCHECK_MSG( m_widget != NULL, NULL, "invalid combobox" ); | |
868a2826 | 176 | |
fd0eed64 RR |
177 | wxNode *node = m_clientDataList.Nth( n ); |
178 | if (!node) return NULL; | |
868a2826 | 179 | |
fd0eed64 RR |
180 | wxVoidClientData *cd = (wxVoidClientData*) node->Data(); |
181 | if (cd) | |
182 | return cd->GetData(); | |
183 | else | |
184 | return (void*) NULL; | |
6de97a3b | 185 | } |
fd0eed64 RR |
186 | |
187 | void wxChoice::SetClientObject( int n, wxClientData* clientData ) | |
188 | { | |
189 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
190 | ||
191 | wxNode *node = m_clientDataList.Nth( n ); | |
192 | if (!node) return; | |
193 | ||
194 | wxClientData *cd = (wxClientData*) node->Data(); | |
195 | if (cd) delete cd; | |
196 | ||
197 | node->SetData( (wxObject*) clientData ); | |
198 | } | |
199 | ||
200 | wxClientData* wxChoice::GetClientObject( int n ) | |
201 | { | |
202 | wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, "invalid combobox" ); | |
203 | ||
204 | wxNode *node = m_clientDataList.Nth( n ); | |
205 | if (!node) return (wxClientData*) NULL; | |
206 | ||
207 | return (wxClientData*) node->Data(); | |
208 | } | |
209 | ||
c801d85f | 210 | |
fd0eed64 | 211 | void wxChoice::Clear() |
c801d85f | 212 | { |
fd0eed64 | 213 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
f96aa4d9 | 214 | |
fd0eed64 RR |
215 | gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) ); |
216 | GtkWidget *menu = gtk_menu_new(); | |
217 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
218 | ||
219 | wxNode *node = m_clientDataList.First(); | |
220 | while (node) | |
221 | { | |
222 | wxClientData *cd = (wxClientData*)node->Data(); | |
223 | if (cd) delete cd; | |
224 | node = node->Next(); | |
225 | } | |
226 | m_clientDataList.Clear(); | |
6de97a3b | 227 | } |
c801d85f | 228 | |
2f6407b9 RR |
229 | void wxChoice::Delete( int WXUNUSED(n) ) |
230 | { | |
fd0eed64 | 231 | wxFAIL_MSG( "wxChoice:Delete not implemented" ); |
2f6407b9 RR |
232 | } |
233 | ||
c801d85f KB |
234 | int wxChoice::FindString( const wxString &string ) const |
235 | { | |
fd0eed64 RR |
236 | wxCHECK_MSG( m_widget != NULL, -1, "invalid choice" ); |
237 | ||
238 | // If you read this code once and you think you understand | |
239 | // it, then you are very wrong. Robert Roebling. | |
240 | ||
241 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
242 | int count = 0; | |
243 | GList *child = menu_shell->children; | |
244 | while (child) | |
245 | { | |
246 | GtkBin *bin = GTK_BIN( child->data ); | |
247 | GtkLabel *label = (GtkLabel *) NULL; | |
248 | if (bin->child) label = GTK_LABEL(bin->child); | |
249 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); | |
b6af8d80 | 250 | |
fd0eed64 | 251 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); |
b6af8d80 | 252 | |
fd0eed64 RR |
253 | if (string == label->label) return count; |
254 | child = child->next; | |
255 | count++; | |
256 | } | |
b6af8d80 | 257 | |
fd0eed64 | 258 | wxFAIL_MSG( "wxChoice: string not found" ); |
b6af8d80 | 259 | |
fd0eed64 | 260 | return -1; |
6de97a3b | 261 | } |
c801d85f | 262 | |
fd0eed64 | 263 | int wxChoice::GetColumns() const |
c801d85f | 264 | { |
fd0eed64 | 265 | return 1; |
6de97a3b | 266 | } |
c801d85f | 267 | |
fd0eed64 | 268 | int wxChoice::GetSelection() |
c801d85f | 269 | { |
fd0eed64 RR |
270 | wxCHECK_MSG( m_widget != NULL, -1, "invalid choice" ); |
271 | ||
272 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
273 | int count = 0; | |
274 | GList *child = menu_shell->children; | |
275 | while (child) | |
276 | { | |
277 | GtkBin *bin = GTK_BIN( child->data ); | |
278 | if (!bin->child) return count; | |
279 | child = child->next; | |
280 | count++; | |
281 | } | |
282 | ||
283 | wxFAIL_MSG( "wxChoice: no selection" ); | |
284 | ||
285 | return -1; | |
6de97a3b | 286 | } |
c801d85f | 287 | |
debe6624 | 288 | wxString wxChoice::GetString( int n ) const |
c801d85f | 289 | { |
fd0eed64 RR |
290 | wxCHECK_MSG( m_widget != NULL, "", "invalid choice" ); |
291 | ||
292 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
293 | int count = 0; | |
294 | GList *child = menu_shell->children; | |
295 | while (child) | |
c801d85f | 296 | { |
fd0eed64 RR |
297 | GtkBin *bin = GTK_BIN( child->data ); |
298 | if (count == n) | |
299 | { | |
300 | GtkLabel *label = (GtkLabel *) NULL; | |
301 | if (bin->child) label = GTK_LABEL(bin->child); | |
302 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); | |
b6af8d80 | 303 | |
fd0eed64 | 304 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); |
b6af8d80 | 305 | |
fd0eed64 RR |
306 | return label->label; |
307 | } | |
308 | child = child->next; | |
309 | count++; | |
6de97a3b | 310 | } |
b6af8d80 | 311 | |
fd0eed64 | 312 | wxFAIL_MSG( "wxChoice: string not found" ); |
b6af8d80 | 313 | |
fd0eed64 | 314 | return ""; |
6de97a3b | 315 | } |
c801d85f | 316 | |
fd0eed64 | 317 | wxString wxChoice::GetStringSelection() const |
c801d85f | 318 | { |
fd0eed64 | 319 | wxCHECK_MSG( m_widget != NULL, "", "invalid choice" ); |
f96aa4d9 | 320 | |
fd0eed64 | 321 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); |
b6af8d80 | 322 | |
fd0eed64 | 323 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); |
b6af8d80 | 324 | |
fd0eed64 | 325 | return label->label; |
6de97a3b | 326 | } |
c801d85f | 327 | |
fd0eed64 | 328 | int wxChoice::Number() const |
c801d85f | 329 | { |
fd0eed64 RR |
330 | wxCHECK_MSG( m_widget != NULL, 0, "invalid choice" ); |
331 | ||
332 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
333 | int count = 0; | |
334 | GList *child = menu_shell->children; | |
335 | while (child) | |
336 | { | |
337 | count++; | |
338 | child = child->next; | |
339 | } | |
340 | return count; | |
6de97a3b | 341 | } |
c801d85f | 342 | |
debe6624 | 343 | void wxChoice::SetColumns( int WXUNUSED(n) ) |
c801d85f | 344 | { |
6de97a3b | 345 | } |
c801d85f | 346 | |
debe6624 | 347 | void wxChoice::SetSelection( int n ) |
c801d85f | 348 | { |
fd0eed64 | 349 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
f96aa4d9 | 350 | |
fd0eed64 RR |
351 | int tmp = n; |
352 | gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); | |
47908e25 | 353 | |
fd0eed64 | 354 | gtk_choice_clicked_callback( (GtkWidget *) NULL, this ); |
6de97a3b | 355 | } |
c801d85f KB |
356 | |
357 | void wxChoice::SetStringSelection( const wxString &string ) | |
358 | { | |
fd0eed64 | 359 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
f96aa4d9 | 360 | |
fd0eed64 RR |
361 | int n = FindString( string ); |
362 | if (n != -1) SetSelection( n ); | |
6de97a3b | 363 | } |
c801d85f | 364 | |
58614078 | 365 | void wxChoice::ApplyWidgetStyle() |
868a2826 | 366 | { |
fd0eed64 | 367 | SetWidgetStyle(); |
f96aa4d9 | 368 | |
fd0eed64 | 369 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
f96aa4d9 | 370 | |
fd0eed64 RR |
371 | gtk_widget_set_style( m_widget, m_widgetStyle ); |
372 | gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle ); | |
f96aa4d9 | 373 | |
fd0eed64 RR |
374 | GList *child = menu_shell->children; |
375 | while (child) | |
376 | { | |
377 | gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle ); | |
58614078 | 378 | |
fd0eed64 RR |
379 | GtkBin *bin = GTK_BIN( child->data ); |
380 | GtkWidget *label = (GtkWidget *) NULL; | |
381 | if (bin->child) label = bin->child; | |
382 | if (!label) label = GTK_BUTTON(m_widget)->child; | |
58614078 | 383 | |
fd0eed64 | 384 | gtk_widget_set_style( label, m_widgetStyle ); |
58614078 | 385 | |
fd0eed64 RR |
386 | child = child->next; |
387 | } | |
f96aa4d9 RR |
388 | } |
389 |