]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/choice.cpp
Changed version to 2.5.1
[wxWidgets.git] / src / gtk / choice.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: choice.cpp
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
29006414 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
14f355c2 11#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
12#pragma implementation "choice.h"
13#endif
14
1e6feb95 15#include "wx/defs.h"
c801d85f 16
ce4169a4
RR
17#if wxUSE_CHOICE
18
1e6feb95 19#include "wx/choice.h"
2da2f941 20#include "wx/arrstr.h"
1e6feb95 21
9e691f46 22#include "wx/gtk/private.h"
83624f79 23
acfd422a
RR
24//-----------------------------------------------------------------------------
25// idle system
26//-----------------------------------------------------------------------------
27
28extern void wxapp_install_idle_handler();
29extern bool g_isIdle;
30
66bd6b93
RR
31//-----------------------------------------------------------------------------
32// data
33//-----------------------------------------------------------------------------
34
35extern bool g_blockEventsOnDrag;
36
c801d85f 37//-----------------------------------------------------------------------------
e1e955e1 38// "activate"
c801d85f
KB
39//-----------------------------------------------------------------------------
40
66bd6b93 41static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
c801d85f 42{
e01c8145 43 if (g_isIdle)
4dcaf11a 44 wxapp_install_idle_handler();
acfd422a 45
a2053b27 46 if (!choice->m_hasVMT) return;
29006414 47
acfd422a 48 if (g_blockEventsOnDrag) return;
29006414 49
acfd422a 50 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
6c8a980f
VZ
51 int n = choice->GetSelection();
52
53 event.SetInt( n );
acfd422a
RR
54 event.SetString( choice->GetStringSelection() );
55 event.SetEventObject(choice);
6c8a980f
VZ
56
57 if ( choice->HasClientObjectData() )
58 event.SetClientObject( choice->GetClientObject(n) );
59 else if ( choice->HasClientUntypedData() )
60 event.SetClientData( choice->GetClientData(n) );
61
acfd422a 62 choice->GetEventHandler()->ProcessEvent(event);
6de97a3b 63}
c801d85f 64
e1e955e1
RR
65//-----------------------------------------------------------------------------
66// wxChoice
c801d85f
KB
67//-----------------------------------------------------------------------------
68
7f4dc78d 69IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
c801d85f 70
fd0eed64 71wxChoice::wxChoice()
c801d85f 72{
e01c8145 73 m_strings = (wxSortedArrayString *)NULL;
6de97a3b 74}
c801d85f 75
debe6624 76bool wxChoice::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
77 const wxPoint &pos, const wxSize &size,
78 int n, const wxString choices[],
79 long style, const wxValidator& validator, const wxString &name )
c801d85f 80{
fd0eed64 81 m_needParent = TRUE;
034be888
RR
82#if (GTK_MINOR_VERSION > 0)
83 m_acceptsFocus = TRUE;
84#endif
29006414 85
4dcaf11a
RR
86 if (!PreCreation( parent, pos, size ) ||
87 !CreateBase( parent, id, pos, size, style, validator, name ))
88 {
223d09f6 89 wxFAIL_MSG( wxT("wxChoice creation failed") );
e01c8145 90 return FALSE;
4dcaf11a 91 }
6de97a3b 92
fd0eed64 93 m_widget = gtk_option_menu_new();
29006414 94
e01c8145
VZ
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
fd0eed64 102 GtkWidget *menu = gtk_menu_new();
29006414 103
fd0eed64
RR
104 for (int i = 0; i < n; i++)
105 {
243dbf1a 106 GtkAddHelper(menu, i, choices[i]);
fd0eed64 107 }
e01c8145 108
fd0eed64 109 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 110
f03fc89f 111 m_parent->DoAddChild( this );
29006414 112
fd0eed64 113 PostCreation();
29006414 114
db434467
RR
115 SetFont( parent->GetFont() );
116
df336b7f 117 SetBestSize(size);
db434467 118
fd0eed64
RR
119 SetBackgroundColour( parent->GetBackgroundColour() );
120 SetForegroundColour( parent->GetForegroundColour() );
f96aa4d9 121
fd0eed64 122 Show( TRUE );
29006414 123
fd0eed64 124 return TRUE;
6de97a3b 125}
29006414 126
fd0eed64
RR
127wxChoice::~wxChoice()
128{
f5e27805 129 Clear();
e01c8145
VZ
130
131 delete m_strings;
fd0eed64
RR
132}
133
9abe166a 134int wxChoice::DoAppend( const wxString &item )
fd0eed64 135{
2ee3ee1b 136 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
29006414 137
fd0eed64 138 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
29006414 139
243dbf1a
VZ
140 return GtkAddHelper(menu, GetCount(), item);
141}
142
143int 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);
fd0eed64 154}
f96aa4d9 155
6c8a980f 156void wxChoice::DoSetItemClientData( int n, void* clientData )
fd0eed64 157{
2ee3ee1b 158 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 159
222ed1d6 160 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 161 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
29006414 162
f5e27805 163 node->SetData( (wxObject*) clientData );
fd0eed64
RR
164}
165
6c8a980f 166void* wxChoice::DoGetItemClientData( int n ) const
fd0eed64 167{
2ee3ee1b 168 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
29006414 169
222ed1d6 170 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 171 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
29006414 172
b1d4dd7a 173 return node->GetData();
6de97a3b 174}
fd0eed64 175
6c8a980f 176void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
fd0eed64 177{
2ee3ee1b 178 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 179
222ed1d6 180 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 181 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
29006414 182
edc973b1 183 // wxItemContainer already deletes data for us
29006414 184
fd0eed64
RR
185 node->SetData( (wxObject*) clientData );
186}
187
6c8a980f 188wxClientData* wxChoice::DoGetItemClientObject( int n ) const
fd0eed64 189{
2ee3ee1b 190 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
29006414 191
222ed1d6 192 wxList::compatibility_iterator node = m_clientList.Item( n );
9abe166a 193 wxCHECK_MSG( node, (wxClientData *)NULL,
6c8a980f 194 wxT("invalid index in wxChoice::DoGetItemClientObject") );
29006414 195
b1d4dd7a 196 return (wxClientData*) node->GetData();
fd0eed64 197}
29006414 198
fd0eed64 199void wxChoice::Clear()
c801d85f 200{
223d09f6 201 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 202
fd0eed64
RR
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 );
29006414 206
6c8a980f
VZ
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!
222ed1d6 212 wxList::compatibility_iterator node = m_clientList.GetFirst();
6c8a980f
VZ
213 while ( node )
214 {
b1d4dd7a
RL
215 delete (wxClientData *)node->GetData();
216 node = node->GetNext();
6c8a980f
VZ
217 }
218 }
d6538e2c 219 m_clientList.Clear();
2ee3ee1b
VZ
220
221 if ( m_strings )
222 m_strings->Clear();
6de97a3b 223}
c801d85f 224
645420d8 225void wxChoice::Delete( int n )
2f6407b9 226{
645420d8
VZ
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 }
2f6407b9
RR
250}
251
c801d85f
KB
252int wxChoice::FindString( const wxString &string ) const
253{
223d09f6 254 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
255
256 // If you read this code once and you think you understand
257 // it, then you are very wrong. Robert Roebling.
29006414 258
fd0eed64
RR
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;
9e691f46
VZ
266 if (bin->child)
267 label = GTK_LABEL(bin->child);
268 if (!label)
269 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
29006414 270
223d09f6 271 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
fab591c5 272
2e1d7104
RR
273#ifdef __WXGTK20__
274 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
275#else
276 wxString tmp( label->label );
277#endif
fab591c5 278 if (string == tmp)
9e691f46 279 return count;
29006414 280
9e691f46
VZ
281 child = child->next;
282 count++;
fd0eed64 283 }
29006414 284
fd0eed64 285 return -1;
6de97a3b 286}
c801d85f 287
9abe166a 288int wxChoice::GetSelection() const
c801d85f 289{
223d09f6 290 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
2e1d7104
RR
291
292#ifdef __WXGTK20__
fd0eed64 293
2e1d7104
RR
294 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) );
295
296#else
fd0eed64
RR
297 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
298 int count = 0;
2e1d7104 299
fd0eed64
RR
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 }
29006414 308
fd0eed64 309 return -1;
2e1d7104 310#endif
6de97a3b 311}
c801d85f 312
6c8a980f
VZ
313void 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
debe6624 320wxString wxChoice::GetString( int n ) const
c801d85f 321{
223d09f6 322 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
fd0eed64
RR
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)
c801d85f 328 {
fd0eed64
RR
329 GtkBin *bin = GTK_BIN( child->data );
330 if (count == n)
331 {
332 GtkLabel *label = (GtkLabel *) NULL;
9e691f46
VZ
333 if (bin->child)
334 label = GTK_LABEL(bin->child);
335 if (!label)
336 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
29006414 337
223d09f6 338 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 339
2e1d7104
RR
340#ifdef __WXGTK20__
341 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
342#else
343 return wxString( label->label );
344#endif
fd0eed64
RR
345 }
346 child = child->next;
347 count++;
6de97a3b 348 }
29006414 349
223d09f6 350 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
29006414 351
223d09f6 352 return wxT("");
6de97a3b 353}
c801d85f 354
9abe166a 355int wxChoice::GetCount() const
c801d85f 356{
223d09f6 357 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
fd0eed64
RR
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;
6de97a3b 368}
c801d85f 369
debe6624 370void wxChoice::SetSelection( int n )
c801d85f 371{
223d09f6 372 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 373
fd0eed64
RR
374 int tmp = n;
375 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
6de97a3b 376}
c801d85f 377
58614078 378void wxChoice::ApplyWidgetStyle()
868a2826 379{
fd0eed64 380 SetWidgetStyle();
29006414 381
fd0eed64 382 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 383
fd0eed64
RR
384 gtk_widget_set_style( m_widget, m_widgetStyle );
385 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 386
fd0eed64
RR
387 GList *child = menu_shell->children;
388 while (child)
389 {
390 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 391
fd0eed64
RR
392 GtkBin *bin = GTK_BIN( child->data );
393 GtkWidget *label = (GtkWidget *) NULL;
9e691f46
VZ
394 if (bin->child)
395 label = bin->child;
396 if (!label)
397 label = BUTTON_CHILD(m_widget);
29006414 398
fd0eed64 399 gtk_widget_set_style( label, m_widgetStyle );
29006414 400
fd0eed64
RR
401 child = child->next;
402 }
f96aa4d9
RR
403}
404
243dbf1a 405int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item)
e01c8145 406{
243dbf1a
VZ
407 wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index"));
408
fab591c5 409 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
e01c8145
VZ
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 {
6c8a980f 426 m_clientList.Insert( (wxObject*) NULL );
e01c8145
VZ
427 }
428 }
429 else
430 {
243dbf1a
VZ
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
e01c8145 434 // normal control, just append
243dbf1a
VZ
435 if (pos == (int)m_clientList.GetCount())
436 {
e01c8145 437 gtk_menu_append( GTK_MENU(menu), menu_item );
e01c8145 438 m_clientList.Append( (wxObject*) NULL );
11e1c70d 439 index = m_clientList.GetCount() - 1;
243dbf1a
VZ
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 }
e01c8145
VZ
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
f68586e5
VZ
466wxSize wxChoice::DoGetBestSize() const
467{
db434467 468 wxSize ret( wxControl::DoGetBestSize() );
29f54b9b
VZ
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 {
60d85ccb 475 int width;
29f54b9b
VZ
476 size_t count = GetCount();
477 for ( size_t n = 0; n < count; n++ )
478 {
60d85ccb 479 GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font );
29f54b9b
VZ
480 if ( width > ret.x )
481 ret.x = width;
482 }
483
df336b7f
VZ
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;
29f54b9b
VZ
495 }
496
497 // but not less than the minimal width
498 if ( ret.x < 80 )
499 ret.x = 80;
500
288059b2 501 ret.y = 16 + GetCharHeight();
9e691f46 502
db434467 503 return ret;
f68586e5
VZ
504}
505
2b904684
RR
506bool 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
df336b7f
VZ
516#endif // wxUSE_CHOICE
517