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