]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/choice.cpp
Fixed bug that caused wrong block of cells to be selected if the
[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
15#include "wx/choice.h"
16
ce4169a4
RR
17#if wxUSE_CHOICE
18
83624f79
RR
19#include "gdk/gdk.h"
20#include "gtk/gtk.h"
21
acfd422a
RR
22//-----------------------------------------------------------------------------
23// idle system
24//-----------------------------------------------------------------------------
25
26extern void wxapp_install_idle_handler();
27extern bool g_isIdle;
28
66bd6b93
RR
29//-----------------------------------------------------------------------------
30// data
31//-----------------------------------------------------------------------------
32
33extern bool g_blockEventsOnDrag;
34
c801d85f 35//-----------------------------------------------------------------------------
e1e955e1 36// "activate"
c801d85f
KB
37//-----------------------------------------------------------------------------
38
66bd6b93 39static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
c801d85f 40{
e01c8145 41 if (g_isIdle)
4dcaf11a 42 wxapp_install_idle_handler();
acfd422a 43
a2053b27 44 if (!choice->m_hasVMT) return;
29006414 45
acfd422a 46 if (g_blockEventsOnDrag) return;
29006414 47
acfd422a 48 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
6c8a980f
VZ
49 int n = choice->GetSelection();
50
51 event.SetInt( n );
acfd422a
RR
52 event.SetString( choice->GetStringSelection() );
53 event.SetEventObject(choice);
6c8a980f
VZ
54
55 if ( choice->HasClientObjectData() )
56 event.SetClientObject( choice->GetClientObject(n) );
57 else if ( choice->HasClientUntypedData() )
58 event.SetClientData( choice->GetClientData(n) );
59
acfd422a 60 choice->GetEventHandler()->ProcessEvent(event);
6de97a3b 61}
c801d85f 62
e1e955e1
RR
63//-----------------------------------------------------------------------------
64// wxChoice
c801d85f
KB
65//-----------------------------------------------------------------------------
66
7f4dc78d 67IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
c801d85f 68
fd0eed64 69wxChoice::wxChoice()
c801d85f 70{
e01c8145 71 m_strings = (wxSortedArrayString *)NULL;
6de97a3b 72}
c801d85f 73
debe6624 74bool wxChoice::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
75 const wxPoint &pos, const wxSize &size,
76 int n, const wxString choices[],
77 long style, const wxValidator& validator, const wxString &name )
c801d85f 78{
fd0eed64 79 m_needParent = TRUE;
034be888
RR
80#if (GTK_MINOR_VERSION > 0)
81 m_acceptsFocus = TRUE;
82#endif
29006414 83
4dcaf11a
RR
84 if (!PreCreation( parent, pos, size ) ||
85 !CreateBase( parent, id, pos, size, style, validator, name ))
86 {
223d09f6 87 wxFAIL_MSG( wxT("wxChoice creation failed") );
e01c8145 88 return FALSE;
4dcaf11a 89 }
6de97a3b 90
fd0eed64 91 m_widget = gtk_option_menu_new();
29006414
VZ
92
93 wxSize newSize(size);
94 if (newSize.x == -1)
95 newSize.x = 80;
96 if (newSize.y == -1)
97 newSize.y = 26;
fd0eed64 98 SetSize( newSize.x, newSize.y );
29006414 99
e01c8145
VZ
100 if ( style & wxCB_SORT )
101 {
102 // if our m_strings != NULL, DoAppend() will check for it and insert
103 // items in the correct order
104 m_strings = new wxSortedArrayString;
105 }
106
fd0eed64 107 GtkWidget *menu = gtk_menu_new();
29006414 108
fd0eed64
RR
109 for (int i = 0; i < n; i++)
110 {
e01c8145 111 AppendHelper(menu, choices[i]);
fd0eed64 112 }
e01c8145 113
fd0eed64 114 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 115
f03fc89f 116 m_parent->DoAddChild( this );
29006414 117
fd0eed64 118 PostCreation();
29006414 119
fd0eed64
RR
120 SetBackgroundColour( parent->GetBackgroundColour() );
121 SetForegroundColour( parent->GetForegroundColour() );
a7ac4461 122 SetFont( parent->GetFont() );
f96aa4d9 123
fd0eed64 124 Show( TRUE );
29006414 125
fd0eed64 126 return TRUE;
6de97a3b 127}
29006414 128
fd0eed64
RR
129wxChoice::~wxChoice()
130{
f5e27805 131 Clear();
e01c8145
VZ
132
133 delete m_strings;
fd0eed64
RR
134}
135
9abe166a 136int wxChoice::DoAppend( const wxString &item )
fd0eed64 137{
2ee3ee1b 138 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
29006414 139
fd0eed64 140 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
29006414 141
e01c8145 142 return AppendHelper(menu, item);
fd0eed64 143}
f96aa4d9 144
6c8a980f 145void wxChoice::DoSetItemClientData( int n, void* clientData )
fd0eed64 146{
2ee3ee1b 147 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 148
d6538e2c 149 wxNode *node = m_clientList.Nth( n );
6c8a980f 150 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
29006414 151
f5e27805 152 node->SetData( (wxObject*) clientData );
fd0eed64
RR
153}
154
6c8a980f 155void* wxChoice::DoGetItemClientData( int n ) const
fd0eed64 156{
2ee3ee1b 157 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
29006414 158
d6538e2c 159 wxNode *node = m_clientList.Nth( n );
6c8a980f 160 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
29006414 161
f5e27805 162 return node->Data();
6de97a3b 163}
fd0eed64 164
6c8a980f 165void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
fd0eed64 166{
2ee3ee1b 167 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 168
d6538e2c 169 wxNode *node = m_clientList.Nth( n );
6c8a980f 170 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
29006414 171
fd0eed64 172 wxClientData *cd = (wxClientData*) node->Data();
9abe166a 173 delete cd;
29006414 174
fd0eed64
RR
175 node->SetData( (wxObject*) clientData );
176}
177
6c8a980f 178wxClientData* wxChoice::DoGetItemClientObject( int n ) const
fd0eed64 179{
2ee3ee1b 180 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
29006414 181
d6538e2c 182 wxNode *node = m_clientList.Nth( n );
9abe166a 183 wxCHECK_MSG( node, (wxClientData *)NULL,
6c8a980f 184 wxT("invalid index in wxChoice::DoGetItemClientObject") );
29006414 185
fd0eed64
RR
186 return (wxClientData*) node->Data();
187}
29006414 188
fd0eed64 189void wxChoice::Clear()
c801d85f 190{
223d09f6 191 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 192
fd0eed64
RR
193 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
194 GtkWidget *menu = gtk_menu_new();
195 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 196
6c8a980f
VZ
197 if ( HasClientObjectData() )
198 {
199 // destroy the data (due to Robert's idea of using wxList<wxObject>
200 // and not wxList<wxClientData> we can't just say
201 // m_clientList.DeleteContents(TRUE) - this would crash!
202 wxNode *node = m_clientList.First();
203 while ( node )
204 {
205 delete (wxClientData *)node->Data();
206 node = node->Next();
207 }
208 }
d6538e2c 209 m_clientList.Clear();
2ee3ee1b
VZ
210
211 if ( m_strings )
212 m_strings->Clear();
6de97a3b 213}
c801d85f 214
2f6407b9
RR
215void wxChoice::Delete( int WXUNUSED(n) )
216{
223d09f6 217 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
2f6407b9
RR
218}
219
c801d85f
KB
220int wxChoice::FindString( const wxString &string ) const
221{
223d09f6 222 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
223
224 // If you read this code once and you think you understand
225 // it, then you are very wrong. Robert Roebling.
29006414 226
fd0eed64
RR
227 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
228 int count = 0;
229 GList *child = menu_shell->children;
230 while (child)
231 {
232 GtkBin *bin = GTK_BIN( child->data );
233 GtkLabel *label = (GtkLabel *) NULL;
234 if (bin->child) label = GTK_LABEL(bin->child);
235 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 236
223d09f6 237 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 238
dcf924a3 239 if (string == wxString(label->label,*wxConvCurrent))
29006414
VZ
240 return count;
241
fd0eed64
RR
242 child = child->next;
243 count++;
244 }
29006414 245
fd0eed64 246 return -1;
6de97a3b 247}
c801d85f 248
9abe166a 249int wxChoice::GetSelection() const
c801d85f 250{
223d09f6 251 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
252
253 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
254 int count = 0;
255 GList *child = menu_shell->children;
256 while (child)
257 {
258 GtkBin *bin = GTK_BIN( child->data );
259 if (!bin->child) return count;
260 child = child->next;
261 count++;
262 }
29006414 263
fd0eed64 264 return -1;
6de97a3b 265}
c801d85f 266
6c8a980f
VZ
267void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) )
268{
269 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
270
271 wxFAIL_MSG(wxT("not implemented"));
272}
273
debe6624 274wxString wxChoice::GetString( int n ) const
c801d85f 275{
223d09f6 276 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
fd0eed64
RR
277
278 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
279 int count = 0;
280 GList *child = menu_shell->children;
281 while (child)
c801d85f 282 {
fd0eed64
RR
283 GtkBin *bin = GTK_BIN( child->data );
284 if (count == n)
285 {
286 GtkLabel *label = (GtkLabel *) NULL;
287 if (bin->child) label = GTK_LABEL(bin->child);
288 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 289
223d09f6 290 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 291
dcf924a3 292 return wxString(label->label,*wxConvCurrent);
fd0eed64
RR
293 }
294 child = child->next;
295 count++;
6de97a3b 296 }
29006414 297
223d09f6 298 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
29006414 299
223d09f6 300 return wxT("");
6de97a3b 301}
c801d85f 302
9abe166a 303int wxChoice::GetCount() const
c801d85f 304{
223d09f6 305 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
fd0eed64
RR
306
307 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
308 int count = 0;
309 GList *child = menu_shell->children;
310 while (child)
311 {
312 count++;
313 child = child->next;
314 }
315 return count;
6de97a3b 316}
c801d85f 317
debe6624 318void wxChoice::SetSelection( int n )
c801d85f 319{
223d09f6 320 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 321
fd0eed64
RR
322 int tmp = n;
323 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
6de97a3b 324}
c801d85f 325
953704c1
RR
326void wxChoice::DisableEvents()
327{
328/*
329 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
330 GList *child = menu_shell->children;
331 while (child)
332 {
333 gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ),
334 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
335
336 child = child->next;
337 }
338*/
339}
340
341void wxChoice::EnableEvents()
342{
343/*
344 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
345 GList *child = menu_shell->children;
346 while (child)
347 {
348 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
349 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
350
351 child = child->next;
352 }
353*/
354}
355
58614078 356void wxChoice::ApplyWidgetStyle()
868a2826 357{
fd0eed64 358 SetWidgetStyle();
29006414 359
fd0eed64 360 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 361
fd0eed64
RR
362 gtk_widget_set_style( m_widget, m_widgetStyle );
363 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 364
fd0eed64
RR
365 GList *child = menu_shell->children;
366 while (child)
367 {
368 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 369
fd0eed64
RR
370 GtkBin *bin = GTK_BIN( child->data );
371 GtkWidget *label = (GtkWidget *) NULL;
372 if (bin->child) label = bin->child;
373 if (!label) label = GTK_BUTTON(m_widget)->child;
29006414 374
fd0eed64 375 gtk_widget_set_style( label, m_widgetStyle );
29006414 376
fd0eed64
RR
377 child = child->next;
378 }
f96aa4d9
RR
379}
380
e01c8145
VZ
381size_t wxChoice::AppendHelper(GtkWidget *menu, const wxString& item)
382{
383 GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() );
384
385 size_t index;
386 if ( m_strings )
387 {
388 // sorted control, need to insert at the correct index
389 index = m_strings->Add(item);
390
391 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
392
393 if ( index )
394 {
395 m_clientList.Insert( m_clientList.Item(index - 1),
396 (wxObject*) NULL );
397 }
398 else
399 {
6c8a980f 400 m_clientList.Insert( (wxObject*) NULL );
e01c8145
VZ
401 }
402 }
403 else
404 {
405 // normal control, just append
406 gtk_menu_append( GTK_MENU(menu), menu_item );
407
408 m_clientList.Append( (wxObject*) NULL );
409
410 // don't call wxChoice::GetCount() from here because it doesn't work
411 // if we're called from ctor (and GtkMenuShell is still NULL)
11e1c70d 412 index = m_clientList.GetCount() - 1;
e01c8145
VZ
413 }
414
415 if (GTK_WIDGET_REALIZED(m_widget))
416 {
417 gtk_widget_realize( menu_item );
418 gtk_widget_realize( GTK_BIN(menu_item)->child );
419
420 if (m_widgetStyle) ApplyWidgetStyle();
421 }
422
423 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
424 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
425
426 gtk_widget_show( menu_item );
427
428 // return the index of the item in the control
429 return index;
430}
431
ce4169a4 432#endif