]>
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 |
29006414 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "choice.h" | |
13 | #endif | |
14 | ||
1e6feb95 | 15 | #include "wx/defs.h" |
c801d85f | 16 | |
ce4169a4 RR |
17 | #if wxUSE_CHOICE |
18 | ||
1e6feb95 VZ |
19 | #include "wx/choice.h" |
20 | ||
9e691f46 | 21 | #include "wx/gtk/private.h" |
83624f79 | 22 | |
acfd422a RR |
23 | //----------------------------------------------------------------------------- |
24 | // idle system | |
25 | //----------------------------------------------------------------------------- | |
26 | ||
27 | extern void wxapp_install_idle_handler(); | |
28 | extern bool g_isIdle; | |
29 | ||
66bd6b93 RR |
30 | //----------------------------------------------------------------------------- |
31 | // data | |
32 | //----------------------------------------------------------------------------- | |
33 | ||
34 | extern bool g_blockEventsOnDrag; | |
35 | ||
c801d85f | 36 | //----------------------------------------------------------------------------- |
e1e955e1 | 37 | // "activate" |
c801d85f KB |
38 | //----------------------------------------------------------------------------- |
39 | ||
66bd6b93 | 40 | static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice ) |
c801d85f | 41 | { |
e01c8145 | 42 | if (g_isIdle) |
4dcaf11a | 43 | wxapp_install_idle_handler(); |
acfd422a | 44 | |
a2053b27 | 45 | if (!choice->m_hasVMT) return; |
29006414 | 46 | |
acfd422a | 47 | if (g_blockEventsOnDrag) return; |
29006414 | 48 | |
acfd422a | 49 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() ); |
6c8a980f VZ |
50 | int n = choice->GetSelection(); |
51 | ||
52 | event.SetInt( n ); | |
acfd422a RR |
53 | event.SetString( choice->GetStringSelection() ); |
54 | event.SetEventObject(choice); | |
6c8a980f VZ |
55 | |
56 | if ( choice->HasClientObjectData() ) | |
57 | event.SetClientObject( choice->GetClientObject(n) ); | |
58 | else if ( choice->HasClientUntypedData() ) | |
59 | event.SetClientData( choice->GetClientData(n) ); | |
60 | ||
acfd422a | 61 | choice->GetEventHandler()->ProcessEvent(event); |
6de97a3b | 62 | } |
c801d85f | 63 | |
e1e955e1 RR |
64 | //----------------------------------------------------------------------------- |
65 | // wxChoice | |
c801d85f KB |
66 | //----------------------------------------------------------------------------- |
67 | ||
7f4dc78d | 68 | IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl) |
c801d85f | 69 | |
fd0eed64 | 70 | wxChoice::wxChoice() |
c801d85f | 71 | { |
e01c8145 | 72 | m_strings = (wxSortedArrayString *)NULL; |
6de97a3b | 73 | } |
c801d85f | 74 | |
debe6624 | 75 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, |
fd0eed64 RR |
76 | const wxPoint &pos, const wxSize &size, |
77 | int n, const wxString choices[], | |
78 | long style, const wxValidator& validator, const wxString &name ) | |
c801d85f | 79 | { |
fd0eed64 | 80 | m_needParent = TRUE; |
034be888 RR |
81 | #if (GTK_MINOR_VERSION > 0) |
82 | m_acceptsFocus = TRUE; | |
83 | #endif | |
29006414 | 84 | |
4dcaf11a RR |
85 | if (!PreCreation( parent, pos, size ) || |
86 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
87 | { | |
223d09f6 | 88 | wxFAIL_MSG( wxT("wxChoice creation failed") ); |
e01c8145 | 89 | return FALSE; |
4dcaf11a | 90 | } |
6de97a3b | 91 | |
fd0eed64 | 92 | m_widget = gtk_option_menu_new(); |
29006414 | 93 | |
e01c8145 VZ |
94 | if ( style & wxCB_SORT ) |
95 | { | |
96 | // if our m_strings != NULL, DoAppend() will check for it and insert | |
97 | // items in the correct order | |
98 | m_strings = new wxSortedArrayString; | |
99 | } | |
100 | ||
fd0eed64 | 101 | GtkWidget *menu = gtk_menu_new(); |
29006414 | 102 | |
fd0eed64 RR |
103 | for (int i = 0; i < n; i++) |
104 | { | |
071a2d78 | 105 | GtkAppendHelper(menu, choices[i]); |
fd0eed64 | 106 | } |
e01c8145 | 107 | |
fd0eed64 | 108 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); |
29006414 | 109 | |
f03fc89f | 110 | m_parent->DoAddChild( this ); |
29006414 | 111 | |
fd0eed64 | 112 | PostCreation(); |
29006414 | 113 | |
db434467 RR |
114 | SetFont( parent->GetFont() ); |
115 | ||
df336b7f | 116 | SetBestSize(size); |
db434467 | 117 | |
fd0eed64 RR |
118 | SetBackgroundColour( parent->GetBackgroundColour() ); |
119 | SetForegroundColour( parent->GetForegroundColour() ); | |
f96aa4d9 | 120 | |
fd0eed64 | 121 | Show( TRUE ); |
29006414 | 122 | |
fd0eed64 | 123 | return TRUE; |
6de97a3b | 124 | } |
29006414 | 125 | |
fd0eed64 RR |
126 | wxChoice::~wxChoice() |
127 | { | |
f5e27805 | 128 | Clear(); |
e01c8145 VZ |
129 | |
130 | delete m_strings; | |
fd0eed64 RR |
131 | } |
132 | ||
9abe166a | 133 | int wxChoice::DoAppend( const wxString &item ) |
fd0eed64 | 134 | { |
2ee3ee1b | 135 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") ); |
29006414 | 136 | |
fd0eed64 | 137 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); |
29006414 | 138 | |
071a2d78 | 139 | return GtkAppendHelper(menu, item); |
fd0eed64 | 140 | } |
f96aa4d9 | 141 | |
6c8a980f | 142 | void wxChoice::DoSetItemClientData( int n, void* clientData ) |
fd0eed64 | 143 | { |
2ee3ee1b | 144 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); |
29006414 | 145 | |
d6538e2c | 146 | wxNode *node = m_clientList.Nth( n ); |
6c8a980f | 147 | wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") ); |
29006414 | 148 | |
f5e27805 | 149 | node->SetData( (wxObject*) clientData ); |
fd0eed64 RR |
150 | } |
151 | ||
6c8a980f | 152 | void* wxChoice::DoGetItemClientData( int n ) const |
fd0eed64 | 153 | { |
2ee3ee1b | 154 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") ); |
29006414 | 155 | |
d6538e2c | 156 | wxNode *node = m_clientList.Nth( n ); |
6c8a980f | 157 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") ); |
29006414 | 158 | |
f5e27805 | 159 | return node->Data(); |
6de97a3b | 160 | } |
fd0eed64 | 161 | |
6c8a980f | 162 | void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData ) |
fd0eed64 | 163 | { |
2ee3ee1b | 164 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); |
29006414 | 165 | |
d6538e2c | 166 | wxNode *node = m_clientList.Nth( n ); |
6c8a980f | 167 | wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") ); |
29006414 | 168 | |
fd0eed64 | 169 | wxClientData *cd = (wxClientData*) node->Data(); |
9abe166a | 170 | delete cd; |
29006414 | 171 | |
fd0eed64 RR |
172 | node->SetData( (wxObject*) clientData ); |
173 | } | |
174 | ||
6c8a980f | 175 | wxClientData* wxChoice::DoGetItemClientObject( int n ) const |
fd0eed64 | 176 | { |
2ee3ee1b | 177 | wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") ); |
29006414 | 178 | |
d6538e2c | 179 | wxNode *node = m_clientList.Nth( n ); |
9abe166a | 180 | wxCHECK_MSG( node, (wxClientData *)NULL, |
6c8a980f | 181 | wxT("invalid index in wxChoice::DoGetItemClientObject") ); |
29006414 | 182 | |
fd0eed64 RR |
183 | return (wxClientData*) node->Data(); |
184 | } | |
29006414 | 185 | |
fd0eed64 | 186 | void wxChoice::Clear() |
c801d85f | 187 | { |
223d09f6 | 188 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
f96aa4d9 | 189 | |
fd0eed64 RR |
190 | gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) ); |
191 | GtkWidget *menu = gtk_menu_new(); | |
192 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
29006414 | 193 | |
6c8a980f VZ |
194 | if ( HasClientObjectData() ) |
195 | { | |
196 | // destroy the data (due to Robert's idea of using wxList<wxObject> | |
197 | // and not wxList<wxClientData> we can't just say | |
198 | // m_clientList.DeleteContents(TRUE) - this would crash! | |
199 | wxNode *node = m_clientList.First(); | |
200 | while ( node ) | |
201 | { | |
202 | delete (wxClientData *)node->Data(); | |
203 | node = node->Next(); | |
204 | } | |
205 | } | |
d6538e2c | 206 | m_clientList.Clear(); |
2ee3ee1b VZ |
207 | |
208 | if ( m_strings ) | |
209 | m_strings->Clear(); | |
6de97a3b | 210 | } |
c801d85f | 211 | |
645420d8 | 212 | void wxChoice::Delete( int n ) |
2f6407b9 | 213 | { |
645420d8 VZ |
214 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
215 | ||
216 | // VZ: apparently GTK+ doesn't have a built-in function to do it (not even | |
217 | // in 2.0), hence this dump implementation - still better than nothing | |
218 | int i, | |
219 | count = GetCount(); | |
220 | ||
221 | wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") ); | |
222 | ||
223 | wxArrayString items; | |
224 | items.Alloc(count); | |
225 | for ( i = 0; i < count; i++ ) | |
226 | { | |
227 | if ( i != n ) | |
228 | items.Add(GetString(i)); | |
229 | } | |
230 | ||
231 | Clear(); | |
232 | ||
233 | for ( i = 0; i < count - 1; i++ ) | |
234 | { | |
235 | Append(items[i]); | |
236 | } | |
2f6407b9 RR |
237 | } |
238 | ||
c801d85f KB |
239 | int wxChoice::FindString( const wxString &string ) const |
240 | { | |
223d09f6 | 241 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") ); |
fd0eed64 RR |
242 | |
243 | // If you read this code once and you think you understand | |
244 | // it, then you are very wrong. Robert Roebling. | |
29006414 | 245 | |
fd0eed64 RR |
246 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
247 | int count = 0; | |
248 | GList *child = menu_shell->children; | |
249 | while (child) | |
250 | { | |
251 | GtkBin *bin = GTK_BIN( child->data ); | |
252 | GtkLabel *label = (GtkLabel *) NULL; | |
9e691f46 VZ |
253 | if (bin->child) |
254 | label = GTK_LABEL(bin->child); | |
255 | if (!label) | |
256 | label = GTK_LABEL( BUTTON_CHILD(m_widget) ); | |
29006414 | 257 | |
223d09f6 | 258 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); |
fab591c5 RR |
259 | |
260 | wxString tmp( wxGTK_CONV_BACK( label->label ) ); | |
261 | if (string == tmp) | |
9e691f46 | 262 | return count; |
29006414 | 263 | |
9e691f46 VZ |
264 | child = child->next; |
265 | count++; | |
fd0eed64 | 266 | } |
29006414 | 267 | |
fd0eed64 | 268 | return -1; |
6de97a3b | 269 | } |
c801d85f | 270 | |
9abe166a | 271 | int wxChoice::GetSelection() const |
c801d85f | 272 | { |
223d09f6 | 273 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") ); |
fd0eed64 RR |
274 | |
275 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
276 | int count = 0; | |
277 | GList *child = menu_shell->children; | |
278 | while (child) | |
279 | { | |
280 | GtkBin *bin = GTK_BIN( child->data ); | |
281 | if (!bin->child) return count; | |
282 | child = child->next; | |
283 | count++; | |
284 | } | |
29006414 | 285 | |
fd0eed64 | 286 | return -1; |
6de97a3b | 287 | } |
c801d85f | 288 | |
6c8a980f VZ |
289 | void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) ) |
290 | { | |
291 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); | |
292 | ||
293 | wxFAIL_MSG(wxT("not implemented")); | |
294 | } | |
295 | ||
debe6624 | 296 | wxString wxChoice::GetString( int n ) const |
c801d85f | 297 | { |
223d09f6 | 298 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") ); |
fd0eed64 RR |
299 | |
300 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
301 | int count = 0; | |
302 | GList *child = menu_shell->children; | |
303 | while (child) | |
c801d85f | 304 | { |
fd0eed64 RR |
305 | GtkBin *bin = GTK_BIN( child->data ); |
306 | if (count == n) | |
307 | { | |
308 | GtkLabel *label = (GtkLabel *) NULL; | |
9e691f46 VZ |
309 | if (bin->child) |
310 | label = GTK_LABEL(bin->child); | |
311 | if (!label) | |
312 | label = GTK_LABEL( BUTTON_CHILD(m_widget) ); | |
29006414 | 313 | |
223d09f6 | 314 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); |
29006414 | 315 | |
fab591c5 | 316 | return wxString( wxGTK_CONV_BACK(label->label) ); |
fd0eed64 RR |
317 | } |
318 | child = child->next; | |
319 | count++; | |
6de97a3b | 320 | } |
29006414 | 321 | |
223d09f6 | 322 | wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") ); |
29006414 | 323 | |
223d09f6 | 324 | return wxT(""); |
6de97a3b | 325 | } |
c801d85f | 326 | |
9abe166a | 327 | int wxChoice::GetCount() const |
c801d85f | 328 | { |
223d09f6 | 329 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") ); |
fd0eed64 RR |
330 | |
331 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
332 | int count = 0; | |
333 | GList *child = menu_shell->children; | |
334 | while (child) | |
335 | { | |
336 | count++; | |
337 | child = child->next; | |
338 | } | |
339 | return count; | |
6de97a3b | 340 | } |
c801d85f | 341 | |
debe6624 | 342 | void wxChoice::SetSelection( int n ) |
c801d85f | 343 | { |
223d09f6 | 344 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
f96aa4d9 | 345 | |
fd0eed64 RR |
346 | int tmp = n; |
347 | gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); | |
6de97a3b | 348 | } |
c801d85f | 349 | |
58614078 | 350 | void wxChoice::ApplyWidgetStyle() |
868a2826 | 351 | { |
fd0eed64 | 352 | SetWidgetStyle(); |
29006414 | 353 | |
fd0eed64 | 354 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
29006414 | 355 | |
fd0eed64 RR |
356 | gtk_widget_set_style( m_widget, m_widgetStyle ); |
357 | gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle ); | |
29006414 | 358 | |
fd0eed64 RR |
359 | GList *child = menu_shell->children; |
360 | while (child) | |
361 | { | |
362 | gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle ); | |
29006414 | 363 | |
fd0eed64 RR |
364 | GtkBin *bin = GTK_BIN( child->data ); |
365 | GtkWidget *label = (GtkWidget *) NULL; | |
9e691f46 VZ |
366 | if (bin->child) |
367 | label = bin->child; | |
368 | if (!label) | |
369 | label = BUTTON_CHILD(m_widget); | |
29006414 | 370 | |
fd0eed64 | 371 | gtk_widget_set_style( label, m_widgetStyle ); |
29006414 | 372 | |
fd0eed64 RR |
373 | child = child->next; |
374 | } | |
f96aa4d9 RR |
375 | } |
376 | ||
071a2d78 | 377 | size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item) |
e01c8145 | 378 | { |
fab591c5 | 379 | GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) ); |
e01c8145 VZ |
380 | |
381 | size_t index; | |
382 | if ( m_strings ) | |
383 | { | |
384 | // sorted control, need to insert at the correct index | |
385 | index = m_strings->Add(item); | |
386 | ||
387 | gtk_menu_insert( GTK_MENU(menu), menu_item, index ); | |
388 | ||
389 | if ( index ) | |
390 | { | |
391 | m_clientList.Insert( m_clientList.Item(index - 1), | |
392 | (wxObject*) NULL ); | |
393 | } | |
394 | else | |
395 | { | |
6c8a980f | 396 | m_clientList.Insert( (wxObject*) NULL ); |
e01c8145 VZ |
397 | } |
398 | } | |
399 | else | |
400 | { | |
401 | // normal control, just append | |
402 | gtk_menu_append( GTK_MENU(menu), menu_item ); | |
403 | ||
404 | m_clientList.Append( (wxObject*) NULL ); | |
405 | ||
406 | // don't call wxChoice::GetCount() from here because it doesn't work | |
407 | // if we're called from ctor (and GtkMenuShell is still NULL) | |
11e1c70d | 408 | index = m_clientList.GetCount() - 1; |
e01c8145 VZ |
409 | } |
410 | ||
411 | if (GTK_WIDGET_REALIZED(m_widget)) | |
412 | { | |
413 | gtk_widget_realize( menu_item ); | |
414 | gtk_widget_realize( GTK_BIN(menu_item)->child ); | |
415 | ||
416 | if (m_widgetStyle) ApplyWidgetStyle(); | |
417 | } | |
418 | ||
419 | gtk_signal_connect( GTK_OBJECT( menu_item ), "activate", | |
420 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
421 | ||
422 | gtk_widget_show( menu_item ); | |
423 | ||
424 | // return the index of the item in the control | |
425 | return index; | |
426 | } | |
427 | ||
f68586e5 VZ |
428 | wxSize wxChoice::DoGetBestSize() const |
429 | { | |
db434467 | 430 | wxSize ret( wxControl::DoGetBestSize() ); |
29f54b9b VZ |
431 | |
432 | // we know better our horizontal extent: it depends on the longest string | |
433 | // we have | |
434 | ret.x = 0; | |
435 | if ( m_widget ) | |
436 | { | |
437 | GdkFont *font = m_font.GetInternalFont(); | |
438 | ||
439 | wxCoord width; | |
440 | size_t count = GetCount(); | |
441 | for ( size_t n = 0; n < count; n++ ) | |
442 | { | |
fab591c5 RR |
443 | // FIXME GTK 2.0 |
444 | width = (wxCoord)gdk_string_width(font, wxGTK_CONV( GetString(n) ) ); | |
29f54b9b VZ |
445 | if ( width > ret.x ) |
446 | ret.x = width; | |
447 | } | |
448 | ||
df336b7f VZ |
449 | // add extra for the choice "=" button |
450 | ||
451 | // VZ: I don't know how to get the right value, it seems to be in | |
452 | // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get | |
453 | // to it - maybe we can use gtk_option_menu_size_request() for this | |
454 | // somehow? | |
455 | // | |
456 | // This default value works only for the default GTK+ theme (i.e. | |
457 | // no theme at all) (FIXME) | |
458 | static const int widthChoiceIndicator = 35; | |
459 | ret.x += widthChoiceIndicator; | |
29f54b9b VZ |
460 | } |
461 | ||
462 | // but not less than the minimal width | |
463 | if ( ret.x < 80 ) | |
464 | ret.x = 80; | |
465 | ||
fab591c5 | 466 | ret.y = 16 + gdk_char_height(GET_STYLE_FONT( m_widget->style ), 'H' ); |
9e691f46 | 467 | |
db434467 | 468 | return ret; |
f68586e5 VZ |
469 | } |
470 | ||
df336b7f VZ |
471 | #endif // wxUSE_CHOICE |
472 |