]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/choice.cpp
Only when wxTE-PROCESS_ENTER has been set.
[wxWidgets.git] / src / gtk1 / 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
584ad2a3
MB
76bool wxChoice::Create( wxWindow *parent, wxWindowID id,
77 const wxPoint &pos, const wxSize &size,
78 const wxArrayString& choices,
79 long style, const wxValidator& validator,
80 const wxString &name )
81{
82 wxCArrayString chs(choices);
83
84 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
85 style, validator, name );
86}
87
debe6624 88bool wxChoice::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
89 const wxPoint &pos, const wxSize &size,
90 int n, const wxString choices[],
91 long style, const wxValidator& validator, const wxString &name )
c801d85f 92{
fd0eed64 93 m_needParent = TRUE;
034be888
RR
94#if (GTK_MINOR_VERSION > 0)
95 m_acceptsFocus = TRUE;
96#endif
29006414 97
4dcaf11a
RR
98 if (!PreCreation( parent, pos, size ) ||
99 !CreateBase( parent, id, pos, size, style, validator, name ))
100 {
223d09f6 101 wxFAIL_MSG( wxT("wxChoice creation failed") );
e01c8145 102 return FALSE;
4dcaf11a 103 }
6de97a3b 104
fd0eed64 105 m_widget = gtk_option_menu_new();
29006414 106
e01c8145
VZ
107 if ( style & wxCB_SORT )
108 {
109 // if our m_strings != NULL, DoAppend() will check for it and insert
110 // items in the correct order
111 m_strings = new wxSortedArrayString;
112 }
113
fd0eed64 114 GtkWidget *menu = gtk_menu_new();
29006414 115
fd0eed64
RR
116 for (int i = 0; i < n; i++)
117 {
243dbf1a 118 GtkAddHelper(menu, i, choices[i]);
fd0eed64 119 }
e01c8145 120
fd0eed64 121 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 122
f03fc89f 123 m_parent->DoAddChild( this );
29006414 124
fd0eed64 125 PostCreation();
e8e24dfa 126 InheritAttributes();
db434467 127
df336b7f 128 SetBestSize(size);
db434467 129
fd0eed64 130 Show( TRUE );
29006414 131
fd0eed64 132 return TRUE;
6de97a3b 133}
29006414 134
fd0eed64
RR
135wxChoice::~wxChoice()
136{
f5e27805 137 Clear();
e01c8145
VZ
138
139 delete m_strings;
fd0eed64
RR
140}
141
9abe166a 142int wxChoice::DoAppend( const wxString &item )
fd0eed64 143{
2ee3ee1b 144 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
29006414 145
fd0eed64 146 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
29006414 147
243dbf1a
VZ
148 return GtkAddHelper(menu, GetCount(), item);
149}
150
151int wxChoice::DoInsert( const wxString &item, int pos )
152{
153 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
154 wxCHECK_MSG( (pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
155
156 if (pos == GetCount())
157 return DoAppend(item);
158
159 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
160
161 return GtkAddHelper(menu, pos, item);
fd0eed64 162}
f96aa4d9 163
6c8a980f 164void wxChoice::DoSetItemClientData( int n, void* clientData )
fd0eed64 165{
2ee3ee1b 166 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 167
222ed1d6 168 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 169 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
29006414 170
f5e27805 171 node->SetData( (wxObject*) clientData );
fd0eed64
RR
172}
173
6c8a980f 174void* wxChoice::DoGetItemClientData( int n ) const
fd0eed64 175{
2ee3ee1b 176 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
29006414 177
222ed1d6 178 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 179 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
29006414 180
b1d4dd7a 181 return node->GetData();
6de97a3b 182}
fd0eed64 183
6c8a980f 184void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
fd0eed64 185{
2ee3ee1b 186 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 187
222ed1d6 188 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 189 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
29006414 190
edc973b1 191 // wxItemContainer already deletes data for us
29006414 192
fd0eed64
RR
193 node->SetData( (wxObject*) clientData );
194}
195
6c8a980f 196wxClientData* wxChoice::DoGetItemClientObject( int n ) const
fd0eed64 197{
2ee3ee1b 198 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
29006414 199
222ed1d6 200 wxList::compatibility_iterator node = m_clientList.Item( n );
9abe166a 201 wxCHECK_MSG( node, (wxClientData *)NULL,
6c8a980f 202 wxT("invalid index in wxChoice::DoGetItemClientObject") );
29006414 203
b1d4dd7a 204 return (wxClientData*) node->GetData();
fd0eed64 205}
29006414 206
fd0eed64 207void wxChoice::Clear()
c801d85f 208{
223d09f6 209 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 210
fd0eed64
RR
211 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
212 GtkWidget *menu = gtk_menu_new();
213 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 214
6c8a980f
VZ
215 if ( HasClientObjectData() )
216 {
217 // destroy the data (due to Robert's idea of using wxList<wxObject>
218 // and not wxList<wxClientData> we can't just say
219 // m_clientList.DeleteContents(TRUE) - this would crash!
222ed1d6 220 wxList::compatibility_iterator node = m_clientList.GetFirst();
6c8a980f
VZ
221 while ( node )
222 {
b1d4dd7a
RL
223 delete (wxClientData *)node->GetData();
224 node = node->GetNext();
6c8a980f
VZ
225 }
226 }
d6538e2c 227 m_clientList.Clear();
2ee3ee1b
VZ
228
229 if ( m_strings )
230 m_strings->Clear();
6de97a3b 231}
c801d85f 232
645420d8 233void wxChoice::Delete( int n )
2f6407b9 234{
645420d8
VZ
235 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
236
237 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
e2380ce1 238 // in 2.0), hence this dumb implementation -- still better than nothing
645420d8
VZ
239 int i,
240 count = GetCount();
241
242 wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") );
243
e2380ce1
VZ
244 const bool hasClientData = m_clientDataItemsType != wxClientData_None;
245 const bool hasObjectData = m_clientDataItemsType == wxClientData_Object;
246
247 wxList::compatibility_iterator node = m_clientList.GetFirst();
248
645420d8 249 wxArrayString items;
e2380ce1 250 wxArrayPtrVoid itemsData;
645420d8
VZ
251 items.Alloc(count);
252 for ( i = 0; i < count; i++ )
253 {
254 if ( i != n )
e2380ce1 255 {
645420d8 256 items.Add(GetString(i));
e2380ce1
VZ
257 if ( hasClientData )
258 {
259 // also save the client data
260 itemsData.Add(node->GetData());
261 }
262 }
263 else // need to delete the client object too
264 {
265 if ( hasObjectData )
266 {
267 delete (wxClientData *)node->GetData();
268 }
269 }
270
271 if ( hasClientData )
272 {
273 node = node->GetNext();
274 }
275 }
276
277 if ( hasObjectData )
278 {
279 // prevent Clear() from destroying all client data
280 m_clientDataItemsType = wxClientData_None;
645420d8
VZ
281 }
282
283 Clear();
284
285 for ( i = 0; i < count - 1; i++ )
286 {
287 Append(items[i]);
e2380ce1
VZ
288
289 if ( hasObjectData )
290 SetClientObject(i, (wxClientData *)itemsData[i]);
291 else if ( hasClientData )
f6bc4102 292 SetClientData(i, itemsData[i]);
645420d8 293 }
2f6407b9
RR
294}
295
c801d85f
KB
296int wxChoice::FindString( const wxString &string ) const
297{
223d09f6 298 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
299
300 // If you read this code once and you think you understand
301 // it, then you are very wrong. Robert Roebling.
29006414 302
fd0eed64
RR
303 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
304 int count = 0;
305 GList *child = menu_shell->children;
306 while (child)
307 {
308 GtkBin *bin = GTK_BIN( child->data );
309 GtkLabel *label = (GtkLabel *) NULL;
9e691f46
VZ
310 if (bin->child)
311 label = GTK_LABEL(bin->child);
312 if (!label)
313 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
29006414 314
223d09f6 315 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
e2380ce1 316
2e1d7104
RR
317#ifdef __WXGTK20__
318 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
319#else
320 wxString tmp( label->label );
321#endif
fab591c5 322 if (string == tmp)
9e691f46 323 return count;
29006414 324
9e691f46
VZ
325 child = child->next;
326 count++;
fd0eed64 327 }
29006414 328
fd0eed64 329 return -1;
6de97a3b 330}
c801d85f 331
9abe166a 332int wxChoice::GetSelection() const
c801d85f 333{
223d09f6 334 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
e2380ce1 335
2e1d7104 336#ifdef __WXGTK20__
fd0eed64 337
2e1d7104 338 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) );
e2380ce1 339
2e1d7104 340#else
fd0eed64
RR
341 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
342 int count = 0;
e2380ce1 343
fd0eed64
RR
344 GList *child = menu_shell->children;
345 while (child)
346 {
347 GtkBin *bin = GTK_BIN( child->data );
348 if (!bin->child) return count;
349 child = child->next;
350 count++;
351 }
29006414 352
fd0eed64 353 return -1;
2e1d7104 354#endif
6de97a3b 355}
c801d85f 356
a912c184 357void wxChoice::SetString( int n, const wxString& str )
6c8a980f
VZ
358{
359 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
360
34b5e560
RR
361 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
362 int count = 0;
363 GList *child = menu_shell->children;
364 while (child)
365 {
366 GtkBin *bin = GTK_BIN( child->data );
367 if (count == n)
368 {
369 GtkLabel *label = (GtkLabel *) NULL;
370 if (bin->child)
371 label = GTK_LABEL(bin->child);
372 if (!label)
373 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
374
375 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
376
377 gtk_label_set_text( label, wxGTK_CONV( str ) );
378
379 return;
380 }
381 child = child->next;
382 count++;
383 }
6c8a980f
VZ
384}
385
debe6624 386wxString wxChoice::GetString( int n ) const
c801d85f 387{
223d09f6 388 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
fd0eed64
RR
389
390 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
391 int count = 0;
392 GList *child = menu_shell->children;
393 while (child)
c801d85f 394 {
fd0eed64
RR
395 GtkBin *bin = GTK_BIN( child->data );
396 if (count == n)
397 {
398 GtkLabel *label = (GtkLabel *) NULL;
9e691f46
VZ
399 if (bin->child)
400 label = GTK_LABEL(bin->child);
401 if (!label)
402 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
29006414 403
223d09f6 404 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 405
2e1d7104
RR
406#ifdef __WXGTK20__
407 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
408#else
409 return wxString( label->label );
410#endif
fd0eed64
RR
411 }
412 child = child->next;
413 count++;
6de97a3b 414 }
29006414 415
223d09f6 416 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
29006414 417
223d09f6 418 return wxT("");
6de97a3b 419}
c801d85f 420
9abe166a 421int wxChoice::GetCount() const
c801d85f 422{
223d09f6 423 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
fd0eed64
RR
424
425 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
426 int count = 0;
427 GList *child = menu_shell->children;
428 while (child)
429 {
430 count++;
431 child = child->next;
432 }
433 return count;
6de97a3b 434}
c801d85f 435
debe6624 436void wxChoice::SetSelection( int n )
c801d85f 437{
223d09f6 438 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 439
fd0eed64
RR
440 int tmp = n;
441 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
6de97a3b 442}
c801d85f 443
58614078 444void wxChoice::ApplyWidgetStyle()
868a2826 445{
fd0eed64 446 SetWidgetStyle();
29006414 447
fd0eed64 448 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 449
fd0eed64
RR
450 gtk_widget_set_style( m_widget, m_widgetStyle );
451 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 452
fd0eed64
RR
453 GList *child = menu_shell->children;
454 while (child)
455 {
456 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 457
fd0eed64
RR
458 GtkBin *bin = GTK_BIN( child->data );
459 GtkWidget *label = (GtkWidget *) NULL;
9e691f46
VZ
460 if (bin->child)
461 label = bin->child;
462 if (!label)
463 label = BUTTON_CHILD(m_widget);
29006414 464
fd0eed64 465 gtk_widget_set_style( label, m_widgetStyle );
29006414 466
fd0eed64
RR
467 child = child->next;
468 }
f96aa4d9
RR
469}
470
243dbf1a 471int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item)
e01c8145 472{
243dbf1a
VZ
473 wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index"));
474
fab591c5 475 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
e01c8145
VZ
476
477 size_t index;
478 if ( m_strings )
479 {
480 // sorted control, need to insert at the correct index
481 index = m_strings->Add(item);
482
483 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
484
485 if ( index )
486 {
487 m_clientList.Insert( m_clientList.Item(index - 1),
488 (wxObject*) NULL );
489 }
490 else
491 {
6c8a980f 492 m_clientList.Insert( (wxObject*) NULL );
e01c8145
VZ
493 }
494 }
495 else
496 {
243dbf1a
VZ
497 // don't call wxChoice::GetCount() from here because it doesn't work
498 // if we're called from ctor (and GtkMenuShell is still NULL)
499
e01c8145 500 // normal control, just append
243dbf1a
VZ
501 if (pos == (int)m_clientList.GetCount())
502 {
e01c8145 503 gtk_menu_append( GTK_MENU(menu), menu_item );
e01c8145 504 m_clientList.Append( (wxObject*) NULL );
11e1c70d 505 index = m_clientList.GetCount() - 1;
243dbf1a
VZ
506 }
507 else
508 {
509 gtk_menu_insert( GTK_MENU(menu), menu_item, pos );
510 m_clientList.Insert( pos, (wxObject*) NULL );
511 index = pos;
512 }
e01c8145
VZ
513 }
514
515 if (GTK_WIDGET_REALIZED(m_widget))
516 {
517 gtk_widget_realize( menu_item );
518 gtk_widget_realize( GTK_BIN(menu_item)->child );
519
520 if (m_widgetStyle) ApplyWidgetStyle();
521 }
522
523 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
524 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
525
526 gtk_widget_show( menu_item );
527
528 // return the index of the item in the control
529 return index;
530}
531
f68586e5
VZ
532wxSize wxChoice::DoGetBestSize() const
533{
db434467 534 wxSize ret( wxControl::DoGetBestSize() );
29f54b9b
VZ
535
536 // we know better our horizontal extent: it depends on the longest string
537 // we have
538 ret.x = 0;
539 if ( m_widget )
540 {
60d85ccb 541 int width;
29f54b9b
VZ
542 size_t count = GetCount();
543 for ( size_t n = 0; n < count; n++ )
544 {
60d85ccb 545 GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font );
29f54b9b
VZ
546 if ( width > ret.x )
547 ret.x = width;
548 }
549
df336b7f
VZ
550 // add extra for the choice "=" button
551
552 // VZ: I don't know how to get the right value, it seems to be in
553 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
554 // to it - maybe we can use gtk_option_menu_size_request() for this
555 // somehow?
556 //
557 // This default value works only for the default GTK+ theme (i.e.
558 // no theme at all) (FIXME)
559 static const int widthChoiceIndicator = 35;
560 ret.x += widthChoiceIndicator;
29f54b9b
VZ
561 }
562
563 // but not less than the minimal width
564 if ( ret.x < 80 )
565 ret.x = 80;
566
288059b2 567 ret.y = 16 + GetCharHeight();
9e691f46 568
db434467 569 return ret;
f68586e5
VZ
570}
571
2b904684
RR
572bool wxChoice::IsOwnGtkWindow( GdkWindow *window )
573{
574#ifdef __WXGTK20__
575 return GTK_BUTTON(m_widget)->event_window;
576#else
577 return (window == m_widget->window);
578#endif
579}
580
581
df336b7f
VZ
582#endif // wxUSE_CHOICE
583