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