]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/choice.cpp
uninitialized variable warning fixed (modified patch 1434065)
[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/defs.h"
11
12 #if wxUSE_CHOICE
13
14 #include "wx/choice.h"
15 #include "wx/arrstr.h"
16
17 #include "wx/gtk1/private.h"
18
19 //-----------------------------------------------------------------------------
20 // idle system
21 //-----------------------------------------------------------------------------
22
23 extern void wxapp_install_idle_handler();
24 extern bool g_isIdle;
25
26 //-----------------------------------------------------------------------------
27 // data
28 //-----------------------------------------------------------------------------
29
30 extern bool g_blockEventsOnDrag;
31
32 //-----------------------------------------------------------------------------
33 // "activate"
34 //-----------------------------------------------------------------------------
35
36 extern "C" {
37 static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
38 {
39 if (g_isIdle)
40 wxapp_install_idle_handler();
41
42 if (!choice->m_hasVMT) return;
43
44 if (g_blockEventsOnDrag) return;
45
46 int selection = wxNOT_FOUND;
47
48 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(choice->GetHandle()) ) );
49 int count = 0;
50
51 GList *child = menu_shell->children;
52 while (child)
53 {
54 GtkBin *bin = GTK_BIN( child->data );
55 if (!bin->child)
56 {
57 selection = count;
58 break;
59 }
60 child = child->next;
61 count++;
62 }
63
64 choice->m_selection_hack = selection;
65
66 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
67 int n = choice->GetSelection();
68
69 event.SetInt( n );
70 event.SetString( choice->GetStringSelection() );
71 event.SetEventObject(choice);
72
73 if ( choice->HasClientObjectData() )
74 event.SetClientObject( choice->GetClientObject(n) );
75 else if ( choice->HasClientUntypedData() )
76 event.SetClientData( choice->GetClientData(n) );
77
78 choice->GetEventHandler()->ProcessEvent(event);
79 }
80 }
81
82 //-----------------------------------------------------------------------------
83 // wxChoice
84 //-----------------------------------------------------------------------------
85
86 IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
87
88 wxChoice::wxChoice()
89 {
90 m_strings = (wxSortedArrayString *)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, DoAppend() 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 (int i = 0; i < 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 SetBestSize(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::DoAppend( const wxString &item )
159 {
160 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
161
162 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
163
164 return GtkAddHelper(menu, GetCount(), item);
165 }
166
167 int wxChoice::DoInsert( const wxString &item, int pos )
168 {
169 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
170 wxCHECK_MSG( (pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
171
172 if (pos == GetCount())
173 return DoAppend(item);
174
175 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
176
177 // if the item to insert is at or before the selection, and the selection is valid
178 if ((pos <= m_selection_hack) && (m_selection_hack != wxNOT_FOUND))
179 {
180 // move the selection forward one
181 m_selection_hack++;
182 }
183
184 return GtkAddHelper(menu, pos, item);
185 }
186
187 void wxChoice::DoSetItemClientData( 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( 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::DoSetItemClientObject( int n, wxClientData* clientData )
208 {
209 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
210
211 wxList::compatibility_iterator node = m_clientList.Item( n );
212 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
213
214 // wxItemContainer already deletes data for us
215
216 node->SetData( (wxObject*) clientData );
217 }
218
219 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
220 {
221 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
222
223 wxList::compatibility_iterator node = m_clientList.Item( n );
224 wxCHECK_MSG( node, (wxClientData *)NULL,
225 wxT("invalid index in wxChoice::DoGetItemClientObject") );
226
227 return (wxClientData*) node->GetData();
228 }
229
230 void wxChoice::Clear()
231 {
232 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
233
234 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
235 GtkWidget *menu = gtk_menu_new();
236 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
237
238 if ( HasClientObjectData() )
239 {
240 // destroy the data (due to Robert's idea of using wxList<wxObject>
241 // and not wxList<wxClientData> we can't just say
242 // m_clientList.DeleteContents(true) - this would crash!
243 wxList::compatibility_iterator node = m_clientList.GetFirst();
244 while ( node )
245 {
246 delete (wxClientData *)node->GetData();
247 node = node->GetNext();
248 }
249 }
250 m_clientList.Clear();
251
252 if ( m_strings )
253 m_strings->Clear();
254
255 // begin with no selection
256 m_selection_hack = wxNOT_FOUND;
257 }
258
259 void wxChoice::Delete( int n )
260 {
261 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
262
263 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
264 // in 2.0), hence this dumb implementation -- still better than nothing
265 int i,
266 count = GetCount();
267
268 wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") );
269
270 // if the item to delete is before the selection, and the selection is valid
271 if ((n < m_selection_hack) && (m_selection_hack != wxNOT_FOUND))
272 {
273 // move the selection back one
274 m_selection_hack--;
275 }
276 else if (n == m_selection_hack)
277 {
278 // invalidate the selection
279 m_selection_hack = wxNOT_FOUND;
280 }
281
282 const bool hasClientData = m_clientDataItemsType != wxClientData_None;
283 const bool hasObjectData = m_clientDataItemsType == wxClientData_Object;
284
285 wxList::compatibility_iterator node = m_clientList.GetFirst();
286
287 wxArrayString items;
288 wxArrayPtrVoid itemsData;
289 items.Alloc(count);
290 for ( i = 0; i < count; i++ )
291 {
292 if ( i != n )
293 {
294 items.Add(GetString(i));
295 if ( hasClientData )
296 {
297 // also save the client data
298 itemsData.Add(node->GetData());
299 }
300 }
301 else // need to delete the client object too
302 {
303 if ( hasObjectData )
304 {
305 delete (wxClientData *)node->GetData();
306 }
307 }
308
309 if ( hasClientData )
310 {
311 node = node->GetNext();
312 }
313 }
314
315 if ( hasObjectData )
316 {
317 // prevent Clear() from destroying all client data
318 m_clientDataItemsType = wxClientData_None;
319 }
320
321 Clear();
322
323 for ( i = 0; i < count - 1; i++ )
324 {
325 Append(items[i]);
326
327 if ( hasObjectData )
328 SetClientObject(i, (wxClientData *)itemsData[i]);
329 else if ( hasClientData )
330 SetClientData(i, itemsData[i]);
331 }
332 }
333
334 int wxChoice::FindString( const wxString &string, bool bCase ) const
335 {
336 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") );
337
338 // If you read this code once and you think you understand
339 // it, then you are very wrong. Robert Roebling.
340
341 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
342 int count = 0;
343 GList *child = menu_shell->children;
344 while (child)
345 {
346 GtkBin *bin = GTK_BIN( child->data );
347 GtkLabel *label = (GtkLabel *) NULL;
348 if (bin->child)
349 label = GTK_LABEL(bin->child);
350 if (!label)
351 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
352
353 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
354
355 wxString tmp( label->label );
356
357 if (string.IsSameAs( tmp, bCase ))
358 return count;
359
360 child = child->next;
361 count++;
362 }
363
364 return wxNOT_FOUND;
365 }
366
367 int wxChoice::GetSelection() const
368 {
369 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") );
370
371 return m_selection_hack;
372
373 }
374
375 void wxChoice::SetString( int n, const wxString& str )
376 {
377 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
378
379 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
380 int count = 0;
381 GList *child = menu_shell->children;
382 while (child)
383 {
384 GtkBin *bin = GTK_BIN( child->data );
385 if (count == n)
386 {
387 GtkLabel *label = (GtkLabel *) NULL;
388 if (bin->child)
389 label = GTK_LABEL(bin->child);
390 if (!label)
391 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
392
393 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
394
395 gtk_label_set_text( label, wxGTK_CONV( str ) );
396
397 return;
398 }
399 child = child->next;
400 count++;
401 }
402 }
403
404 wxString wxChoice::GetString( int n ) const
405 {
406 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid choice") );
407
408 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
409 int count = 0;
410 GList *child = menu_shell->children;
411 while (child)
412 {
413 GtkBin *bin = GTK_BIN( child->data );
414 if (count == n)
415 {
416 GtkLabel *label = (GtkLabel *) NULL;
417 if (bin->child)
418 label = GTK_LABEL(bin->child);
419 if (!label)
420 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
421
422 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
423
424 return wxString( label->label );
425 }
426 child = child->next;
427 count++;
428 }
429
430 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
431
432 return wxEmptyString;
433 }
434
435 int wxChoice::GetCount() const
436 {
437 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
438
439 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
440 int count = 0;
441 GList *child = menu_shell->children;
442 while (child)
443 {
444 count++;
445 child = child->next;
446 }
447 return count;
448 }
449
450 void wxChoice::SetSelection( int n )
451 {
452 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
453
454 int tmp = n;
455 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
456
457 // set the local selection variable manually
458 if ((n >= 0) && (n < GetCount()))
459 {
460 // a valid selection has been made
461 m_selection_hack = n;
462 }
463 else if ((n == wxNOT_FOUND) || (GetCount() == 0))
464 {
465 // invalidates the selection if there are no items
466 // or if it is specifically set to wxNOT_FOUND
467 m_selection_hack = wxNOT_FOUND;
468 }
469 else
470 {
471 // this selects the first item by default if the selection is out of bounds
472 m_selection_hack = 0;
473 }
474 }
475
476 void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style)
477 {
478 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
479
480 gtk_widget_modify_style( m_widget, style );
481 gtk_widget_modify_style( GTK_WIDGET( menu_shell ), style );
482
483 GList *child = menu_shell->children;
484 while (child)
485 {
486 gtk_widget_modify_style( GTK_WIDGET( child->data ), style );
487
488 GtkBin *bin = GTK_BIN( child->data );
489 GtkWidget *label = (GtkWidget *) NULL;
490 if (bin->child)
491 label = bin->child;
492 if (!label)
493 label = BUTTON_CHILD(m_widget);
494
495 gtk_widget_modify_style( label, style );
496
497 child = child->next;
498 }
499 }
500
501 int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item)
502 {
503 wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index"));
504
505 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
506
507 size_t index;
508 if ( m_strings )
509 {
510 // sorted control, need to insert at the correct index
511 index = m_strings->Add(item);
512
513 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
514
515 if ( index )
516 {
517 m_clientList.Insert( m_clientList.Item(index - 1),
518 (wxObject*) NULL );
519 }
520 else
521 {
522 m_clientList.Insert( (wxObject*) NULL );
523 }
524 }
525 else
526 {
527 // don't call wxChoice::GetCount() from here because it doesn't work
528 // if we're called from ctor (and GtkMenuShell is still NULL)
529
530 // normal control, just append
531 if (pos == (int)m_clientList.GetCount())
532 {
533 gtk_menu_append( GTK_MENU(menu), menu_item );
534 m_clientList.Append( (wxObject*) NULL );
535 index = m_clientList.GetCount() - 1;
536 }
537 else
538 {
539 gtk_menu_insert( GTK_MENU(menu), menu_item, pos );
540 m_clientList.Insert( pos, (wxObject*) NULL );
541 index = pos;
542 }
543 }
544
545 if (GTK_WIDGET_REALIZED(m_widget))
546 {
547 gtk_widget_realize( menu_item );
548 gtk_widget_realize( GTK_BIN(menu_item)->child );
549
550 ApplyWidgetStyle();
551 }
552
553 // The best size of a wxChoice should probably
554 // be changed everytime the control has been
555 // changed, but at least after adding an item
556 // it has to change. Adapted from Matt Ownby.
557 InvalidateBestSize();
558
559 gtk_signal_connect_after( GTK_OBJECT( menu_item ), "activate",
560 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
561
562 gtk_widget_show( menu_item );
563
564 // return the index of the item in the control
565 return index;
566 }
567
568 wxSize wxChoice::DoGetBestSize() const
569 {
570 wxSize ret( wxControl::DoGetBestSize() );
571
572 // we know better our horizontal extent: it depends on the longest string
573 // we have
574 ret.x = 0;
575 if ( m_widget )
576 {
577 int width;
578 size_t count = GetCount();
579 for ( size_t n = 0; n < count; n++ )
580 {
581 GetTextExtent( GetString(n), &width, NULL, NULL, NULL );
582 if ( width > ret.x )
583 ret.x = width;
584 }
585
586 // add extra for the choice "=" button
587
588 // VZ: I don't know how to get the right value, it seems to be in
589 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
590 // to it - maybe we can use gtk_option_menu_size_request() for this
591 // somehow?
592 //
593 // This default value works only for the default GTK+ theme (i.e.
594 // no theme at all) (FIXME)
595 static const int widthChoiceIndicator = 35;
596 ret.x += widthChoiceIndicator;
597 }
598
599 // but not less than the minimal width
600 if ( ret.x < 80 )
601 ret.x = 80;
602
603 // If this request_size is called with no entries then
604 // the returned height is wrong. Give it a reasonable
605 // default value.
606 if (ret.y <= 18)
607 ret.y = 8 + GetCharHeight();
608
609 CacheBestSize(ret);
610 return ret;
611 }
612
613 bool wxChoice::IsOwnGtkWindow( GdkWindow *window )
614 {
615 return (window == m_widget->window);
616 }
617
618 // static
619 wxVisualAttributes
620 wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
621 {
622 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new);
623 }
624
625
626 #endif // wxUSE_CHOICE