]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/choice.cpp
Don't document wxSortedArrayString as deriving from wxArrayString.
[wxWidgets.git] / src / gtk1 / choice.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/choice.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 #include "wx/wxprec.h"
10
11 #if wxUSE_CHOICE
12
13 #include "wx/choice.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/arrstr.h"
17 #endif
18
19 #include "wx/gtk1/private.h"
20
21 //-----------------------------------------------------------------------------
22 // idle system
23 //-----------------------------------------------------------------------------
24
25 extern void wxapp_install_idle_handler();
26 extern bool g_isIdle;
27
28 //-----------------------------------------------------------------------------
29 // data
30 //-----------------------------------------------------------------------------
31
32 extern bool g_blockEventsOnDrag;
33
34 //-----------------------------------------------------------------------------
35 // "activate"
36 //-----------------------------------------------------------------------------
37
38 extern "C" {
39 static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
40 {
41 if (g_isIdle)
42 wxapp_install_idle_handler();
43
44 if (!choice->m_hasVMT) return;
45
46 if (g_blockEventsOnDrag) return;
47
48 int selection = wxNOT_FOUND;
49
50 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(choice->GetHandle()) ) );
51 int count = 0;
52
53 GList *child = menu_shell->children;
54 while (child)
55 {
56 GtkBin *bin = GTK_BIN( child->data );
57 if (!bin->child)
58 {
59 selection = count;
60 break;
61 }
62 child = child->next;
63 count++;
64 }
65
66 choice->m_selection_hack = selection;
67
68 wxCommandEvent event(wxEVT_CHOICE, choice->GetId() );
69 int n = choice->GetSelection();
70
71 event.SetInt( n );
72 event.SetString( choice->GetStringSelection() );
73 event.SetEventObject(choice);
74
75 if ( choice->HasClientObjectData() )
76 event.SetClientObject( choice->GetClientObject(n) );
77 else if ( choice->HasClientUntypedData() )
78 event.SetClientData( choice->GetClientData(n) );
79
80 choice->HandleWindowEvent(event);
81 }
82 }
83
84 //-----------------------------------------------------------------------------
85 // wxChoice
86 //-----------------------------------------------------------------------------
87
88 wxChoice::wxChoice()
89 {
90 m_strings = NULL;
91 }
92
93 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
94 const wxPoint &pos, const wxSize &size,
95 const wxArrayString& choices,
96 long style, const wxValidator& validator,
97 const wxString &name )
98 {
99 wxCArrayString chs(choices);
100
101 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
102 style, validator, name );
103 }
104
105 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
106 const wxPoint &pos, const wxSize &size,
107 int n, const wxString choices[],
108 long style, const wxValidator& validator, const wxString &name )
109 {
110 m_needParent = true;
111 #if (GTK_MINOR_VERSION > 0)
112 m_acceptsFocus = true;
113 #endif
114
115 if (!PreCreation( parent, pos, size ) ||
116 !CreateBase( parent, id, pos, size, style, validator, name ))
117 {
118 wxFAIL_MSG( wxT("wxChoice creation failed") );
119 return false;
120 }
121
122 m_widget = gtk_option_menu_new();
123
124 if ( style & wxCB_SORT )
125 {
126 // if our m_strings != NULL, Append() will check for it and insert
127 // items in the correct order
128 m_strings = new wxSortedArrayString;
129 }
130
131 // begin with no selection
132 m_selection_hack = wxNOT_FOUND;
133
134 GtkWidget *menu = gtk_menu_new();
135
136 for (unsigned int i = 0; i < (unsigned int)n; i++)
137 {
138 GtkAddHelper(menu, i, choices[i]);
139 }
140
141 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
142
143 m_parent->DoAddChild( this );
144
145 PostCreation(size);
146 SetInitialSize(size); // need this too because this is a wxControlWithItems
147
148 return true;
149 }
150
151 wxChoice::~wxChoice()
152 {
153 Clear();
154
155 delete m_strings;
156 }
157
158 int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
159 unsigned int pos,
160 void **clientData, wxClientDataType type)
161 {
162 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
163
164 const unsigned int count = items.GetCount();
165
166 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
167
168 for ( unsigned int i = 0; i < count; ++i, ++pos )
169 {
170 int n = GtkAddHelper(menu, pos, items[i]);
171 if ( n == wxNOT_FOUND )
172 return n;
173
174 AssignNewItemClientData(n, clientData, i, type);
175 }
176
177 // if the item to insert is at or before the selection, and the selection is valid
178 if (((int)pos <= m_selection_hack) && (m_selection_hack != wxNOT_FOUND))
179 {
180 // move the selection forward
181 m_selection_hack += count;
182 }
183
184 return pos - 1;
185 }
186
187 void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
188 {
189 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
190
191 wxList::compatibility_iterator node = m_clientList.Item( n );
192 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
193
194 node->SetData( (wxObject*) clientData );
195 }
196
197 void* wxChoice::DoGetItemClientData(unsigned int n) const
198 {
199 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
200
201 wxList::compatibility_iterator node = m_clientList.Item( n );
202 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
203
204 return node->GetData();
205 }
206
207 void wxChoice::DoClear()
208 {
209 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
210
211 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
212 GtkWidget *menu = gtk_menu_new();
213 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
214
215 m_clientList.Clear();
216
217 if ( m_strings )
218 m_strings->Clear();
219
220 // begin with no selection
221 m_selection_hack = wxNOT_FOUND;
222 }
223
224 void wxChoice::DoDeleteOneItem(unsigned int n)
225 {
226 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
227
228 wxCHECK_RET( IsValid(n), wxT("invalid index in wxChoice::Delete") );
229
230 // if the item to delete is before the selection, and the selection is valid
231 if (((int)n < m_selection_hack) && (m_selection_hack != wxNOT_FOUND))
232 {
233 // move the selection back one
234 m_selection_hack--;
235 }
236 else if ((int)n == m_selection_hack)
237 {
238 // invalidate the selection
239 m_selection_hack = wxNOT_FOUND;
240 }
241
242 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
243 // in 2.0), hence this dumb implementation -- still better than nothing
244 const unsigned int count = GetCount();
245
246 wxList::compatibility_iterator node = m_clientList.GetFirst();
247
248 wxArrayString items;
249 wxArrayPtrVoid itemsData;
250 items.Alloc(count);
251 for ( unsigned i = 0; i < count; i++, node = node->GetNext() )
252 {
253 if ( i != n )
254 {
255 items.Add(GetString(i));
256 itemsData.Add(node->GetData());
257 }
258 }
259
260 wxChoice::DoClear();
261
262 void ** const data = &itemsData[0];
263 if ( HasClientObjectData() )
264 Append(items, reinterpret_cast<wxClientData **>(data));
265 else
266 Append(items, data);
267 }
268
269 int wxChoice::FindString( const wxString &string, bool bCase ) const
270 {
271 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") );
272
273 // If you read this code once and you think you understand
274 // it, then you are very wrong. Robert Roebling.
275
276 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
277 int count = 0;
278 GList *child = menu_shell->children;
279 while (child)
280 {
281 GtkBin *bin = GTK_BIN( child->data );
282 GtkLabel *label = NULL;
283 if (bin->child)
284 label = GTK_LABEL(bin->child);
285 if (!label)
286 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
287
288 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
289
290 wxString tmp( label->label );
291
292 if (string.IsSameAs( tmp, bCase ))
293 return count;
294
295 child = child->next;
296 count++;
297 }
298
299 return wxNOT_FOUND;
300 }
301
302 int wxChoice::GetSelection() const
303 {
304 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") );
305
306 return m_selection_hack;
307
308 }
309
310 void wxChoice::SetString(unsigned int n, const wxString& str )
311 {
312 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
313
314 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
315 unsigned int count = 0;
316 GList *child = menu_shell->children;
317 while (child)
318 {
319 GtkBin *bin = GTK_BIN( child->data );
320 if (count == n)
321 {
322 GtkLabel *label = NULL;
323 if (bin->child)
324 label = GTK_LABEL(bin->child);
325 if (!label)
326 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
327
328 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
329
330 gtk_label_set_text( label, wxGTK_CONV( str ) );
331
332 return;
333 }
334 child = child->next;
335 count++;
336 }
337 }
338
339 wxString wxChoice::GetString(unsigned int n) const
340 {
341 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid choice") );
342
343 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
344 unsigned int count = 0;
345 GList *child = menu_shell->children;
346 while (child)
347 {
348 GtkBin *bin = GTK_BIN( child->data );
349 if (count == n)
350 {
351 GtkLabel *label = NULL;
352 if (bin->child)
353 label = GTK_LABEL(bin->child);
354 if (!label)
355 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
356
357 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
358
359 return wxString( label->label );
360 }
361 child = child->next;
362 count++;
363 }
364
365 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
366
367 return wxEmptyString;
368 }
369
370 unsigned int wxChoice::GetCount() const
371 {
372 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
373
374 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
375 unsigned int count = 0;
376 GList *child = menu_shell->children;
377 while (child)
378 {
379 count++;
380 child = child->next;
381 }
382 return count;
383 }
384
385 void wxChoice::SetSelection( int n )
386 {
387 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
388
389 int tmp = n;
390 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
391
392 // set the local selection variable manually
393 if (IsValid((unsigned int)n))
394 {
395 // a valid selection has been made
396 m_selection_hack = n;
397 }
398 else if ((n == wxNOT_FOUND) || (GetCount() == 0))
399 {
400 // invalidates the selection if there are no items
401 // or if it is specifically set to wxNOT_FOUND
402 m_selection_hack = wxNOT_FOUND;
403 }
404 else
405 {
406 // this selects the first item by default if the selection is out of bounds
407 m_selection_hack = 0;
408 }
409 }
410
411 void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style)
412 {
413 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
414
415 gtk_widget_modify_style( m_widget, style );
416 gtk_widget_modify_style( GTK_WIDGET( menu_shell ), style );
417
418 GList *child = menu_shell->children;
419 while (child)
420 {
421 gtk_widget_modify_style( GTK_WIDGET( child->data ), style );
422
423 GtkBin *bin = GTK_BIN( child->data );
424 GtkWidget *label = NULL;
425 if (bin->child)
426 label = bin->child;
427 if (!label)
428 label = BUTTON_CHILD(m_widget);
429
430 gtk_widget_modify_style( label, style );
431
432 child = child->next;
433 }
434 }
435
436 int wxChoice::GtkAddHelper(GtkWidget *menu, unsigned int pos, const wxString& item)
437 {
438 wxCHECK_MSG(pos<=m_clientList.GetCount(), -1, wxT("invalid index"));
439
440 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
441
442 size_t index;
443 if ( m_strings )
444 {
445 // sorted control, need to insert at the correct index
446 index = m_strings->Add(item);
447
448 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
449
450 if ( index )
451 {
452 m_clientList.Insert( m_clientList.Item(index - 1),
453 NULL );
454 }
455 else
456 {
457 m_clientList.Insert( NULL );
458 }
459 }
460 else
461 {
462 // don't call wxChoice::GetCount() from here because it doesn't work
463 // if we're called from ctor (and GtkMenuShell is still NULL)
464
465 // normal control, just append
466 if (pos == m_clientList.GetCount())
467 {
468 gtk_menu_append( GTK_MENU(menu), menu_item );
469 m_clientList.Append( NULL );
470 index = m_clientList.GetCount() - 1;
471 }
472 else
473 {
474 gtk_menu_insert( GTK_MENU(menu), menu_item, pos );
475 m_clientList.Insert( pos, NULL );
476 index = pos;
477 }
478 }
479
480 if (GTK_WIDGET_REALIZED(m_widget))
481 {
482 gtk_widget_realize( menu_item );
483 gtk_widget_realize( GTK_BIN(menu_item)->child );
484
485 ApplyWidgetStyle();
486 }
487
488 // The best size of a wxChoice should probably
489 // be changed everytime the control has been
490 // changed, but at least after adding an item
491 // it has to change. Adapted from Matt Ownby.
492 InvalidateBestSize();
493
494 gtk_signal_connect_after( GTK_OBJECT( menu_item ), "activate",
495 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
496
497 gtk_widget_show( menu_item );
498
499 // return the index of the item in the control
500 return index;
501 }
502
503 wxSize wxChoice::DoGetBestSize() const
504 {
505 wxSize ret( wxControl::DoGetBestSize() );
506
507 // we know better our horizontal extent: it depends on the longest string
508 // we have
509 ret.x = 0;
510 if ( m_widget )
511 {
512 int width;
513 unsigned int count = GetCount();
514 for ( unsigned int n = 0; n < count; n++ )
515 {
516 GetTextExtent(GetString(n), &width, NULL, NULL, NULL );
517 if ( width > ret.x )
518 ret.x = width;
519 }
520
521 // add extra for the choice "=" button
522
523 // VZ: I don't know how to get the right value, it seems to be in
524 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
525 // to it - maybe we can use gtk_option_menu_size_request() for this
526 // somehow?
527 //
528 // This default value works only for the default GTK+ theme (i.e.
529 // no theme at all) (FIXME)
530 static const int widthChoiceIndicator = 35;
531 ret.x += widthChoiceIndicator;
532 }
533
534 // but not less than the minimal width
535 if ( ret.x < 80 )
536 ret.x = 80;
537
538 // If this request_size is called with no entries then
539 // the returned height is wrong. Give it a reasonable
540 // default value.
541 if (ret.y <= 18)
542 ret.y = 8 + GetCharHeight();
543
544 CacheBestSize(ret);
545 return ret;
546 }
547
548 bool wxChoice::IsOwnGtkWindow( GdkWindow *window )
549 {
550 return (window == m_widget->window);
551 }
552
553 // static
554 wxVisualAttributes
555 wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
556 {
557 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new);
558 }
559
560
561 #endif // wxUSE_CHOICE