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