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