]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/choice.cpp
Fix/hack 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
11#ifdef __GNUG__
12#pragma implementation "choice.h"
13#endif
14
15#include "wx/choice.h"
16
ce4169a4
RR
17#if wxUSE_CHOICE
18
071a2d78
RR
19#include <gdk/gdk.h>
20#include <gtk/gtk.h>
83624f79 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 92
e01c8145
VZ
93 if ( style & wxCB_SORT )
94 {
95 // if our m_strings != NULL, DoAppend() will check for it and insert
96 // items in the correct order
97 m_strings = new wxSortedArrayString;
98 }
99
fd0eed64 100 GtkWidget *menu = gtk_menu_new();
29006414 101
fd0eed64
RR
102 for (int i = 0; i < n; i++)
103 {
071a2d78 104 GtkAppendHelper(menu, choices[i]);
fd0eed64 105 }
e01c8145 106
fd0eed64 107 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 108
f03fc89f 109 m_parent->DoAddChild( this );
29006414 110
fd0eed64 111 PostCreation();
29006414 112
db434467
RR
113 SetFont( parent->GetFont() );
114
115 wxSize size_best( DoGetBestSize() );
116 wxSize new_size( size );
117 if (new_size.x == -1)
118 new_size.x = size_best.x;
119 if (new_size.y == -1)
120 new_size.y = size_best.y;
121 if ((new_size.x != size.x) || (new_size.y != size.y))
122 SetSize( new_size.x, new_size.y );
123
fd0eed64
RR
124 SetBackgroundColour( parent->GetBackgroundColour() );
125 SetForegroundColour( parent->GetForegroundColour() );
f96aa4d9 126
fd0eed64 127 Show( TRUE );
29006414 128
fd0eed64 129 return TRUE;
6de97a3b 130}
29006414 131
fd0eed64
RR
132wxChoice::~wxChoice()
133{
f5e27805 134 Clear();
e01c8145
VZ
135
136 delete m_strings;
fd0eed64
RR
137}
138
9abe166a 139int wxChoice::DoAppend( const wxString &item )
fd0eed64 140{
2ee3ee1b 141 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
29006414 142
fd0eed64 143 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
29006414 144
071a2d78 145 return GtkAppendHelper(menu, item);
fd0eed64 146}
f96aa4d9 147
6c8a980f 148void wxChoice::DoSetItemClientData( int n, void* clientData )
fd0eed64 149{
2ee3ee1b 150 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 151
d6538e2c 152 wxNode *node = m_clientList.Nth( n );
6c8a980f 153 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
29006414 154
f5e27805 155 node->SetData( (wxObject*) clientData );
fd0eed64
RR
156}
157
6c8a980f 158void* wxChoice::DoGetItemClientData( int n ) const
fd0eed64 159{
2ee3ee1b 160 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
29006414 161
d6538e2c 162 wxNode *node = m_clientList.Nth( n );
6c8a980f 163 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
29006414 164
f5e27805 165 return node->Data();
6de97a3b 166}
fd0eed64 167
6c8a980f 168void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
fd0eed64 169{
2ee3ee1b 170 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 171
d6538e2c 172 wxNode *node = m_clientList.Nth( n );
6c8a980f 173 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
29006414 174
fd0eed64 175 wxClientData *cd = (wxClientData*) node->Data();
9abe166a 176 delete cd;
29006414 177
fd0eed64
RR
178 node->SetData( (wxObject*) clientData );
179}
180
6c8a980f 181wxClientData* wxChoice::DoGetItemClientObject( int n ) const
fd0eed64 182{
2ee3ee1b 183 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
29006414 184
d6538e2c 185 wxNode *node = m_clientList.Nth( n );
9abe166a 186 wxCHECK_MSG( node, (wxClientData *)NULL,
6c8a980f 187 wxT("invalid index in wxChoice::DoGetItemClientObject") );
29006414 188
fd0eed64
RR
189 return (wxClientData*) node->Data();
190}
29006414 191
fd0eed64 192void wxChoice::Clear()
c801d85f 193{
223d09f6 194 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 195
fd0eed64
RR
196 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
197 GtkWidget *menu = gtk_menu_new();
198 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 199
6c8a980f
VZ
200 if ( HasClientObjectData() )
201 {
202 // destroy the data (due to Robert's idea of using wxList<wxObject>
203 // and not wxList<wxClientData> we can't just say
204 // m_clientList.DeleteContents(TRUE) - this would crash!
205 wxNode *node = m_clientList.First();
206 while ( node )
207 {
208 delete (wxClientData *)node->Data();
209 node = node->Next();
210 }
211 }
d6538e2c 212 m_clientList.Clear();
2ee3ee1b
VZ
213
214 if ( m_strings )
215 m_strings->Clear();
6de97a3b 216}
c801d85f 217
2f6407b9
RR
218void wxChoice::Delete( int WXUNUSED(n) )
219{
223d09f6 220 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
2f6407b9
RR
221}
222
c801d85f
KB
223int wxChoice::FindString( const wxString &string ) const
224{
223d09f6 225 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
226
227 // If you read this code once and you think you understand
228 // it, then you are very wrong. Robert Roebling.
29006414 229
fd0eed64
RR
230 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
231 int count = 0;
232 GList *child = menu_shell->children;
233 while (child)
234 {
235 GtkBin *bin = GTK_BIN( child->data );
236 GtkLabel *label = (GtkLabel *) NULL;
237 if (bin->child) label = GTK_LABEL(bin->child);
238 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 239
223d09f6 240 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 241
dcf924a3 242 if (string == wxString(label->label,*wxConvCurrent))
29006414
VZ
243 return count;
244
fd0eed64
RR
245 child = child->next;
246 count++;
247 }
29006414 248
fd0eed64 249 return -1;
6de97a3b 250}
c801d85f 251
9abe166a 252int wxChoice::GetSelection() const
c801d85f 253{
223d09f6 254 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
255
256 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
257 int count = 0;
258 GList *child = menu_shell->children;
259 while (child)
260 {
261 GtkBin *bin = GTK_BIN( child->data );
262 if (!bin->child) return count;
263 child = child->next;
264 count++;
265 }
29006414 266
fd0eed64 267 return -1;
6de97a3b 268}
c801d85f 269
6c8a980f
VZ
270void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) )
271{
272 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
273
274 wxFAIL_MSG(wxT("not implemented"));
275}
276
debe6624 277wxString wxChoice::GetString( int n ) const
c801d85f 278{
223d09f6 279 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
fd0eed64
RR
280
281 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
282 int count = 0;
283 GList *child = menu_shell->children;
284 while (child)
c801d85f 285 {
fd0eed64
RR
286 GtkBin *bin = GTK_BIN( child->data );
287 if (count == n)
288 {
289 GtkLabel *label = (GtkLabel *) NULL;
290 if (bin->child) label = GTK_LABEL(bin->child);
291 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 292
223d09f6 293 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 294
dcf924a3 295 return wxString(label->label,*wxConvCurrent);
fd0eed64
RR
296 }
297 child = child->next;
298 count++;
6de97a3b 299 }
29006414 300
223d09f6 301 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
29006414 302
223d09f6 303 return wxT("");
6de97a3b 304}
c801d85f 305
9abe166a 306int wxChoice::GetCount() const
c801d85f 307{
223d09f6 308 wxCHECK_MSG( m_widget != NULL, 0, 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)
314 {
315 count++;
316 child = child->next;
317 }
318 return count;
6de97a3b 319}
c801d85f 320
debe6624 321void wxChoice::SetSelection( int n )
c801d85f 322{
223d09f6 323 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 324
fd0eed64
RR
325 int tmp = n;
326 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
6de97a3b 327}
c801d85f 328
58614078 329void wxChoice::ApplyWidgetStyle()
868a2826 330{
fd0eed64 331 SetWidgetStyle();
29006414 332
fd0eed64 333 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 334
fd0eed64
RR
335 gtk_widget_set_style( m_widget, m_widgetStyle );
336 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 337
fd0eed64
RR
338 GList *child = menu_shell->children;
339 while (child)
340 {
341 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 342
fd0eed64
RR
343 GtkBin *bin = GTK_BIN( child->data );
344 GtkWidget *label = (GtkWidget *) NULL;
345 if (bin->child) label = bin->child;
346 if (!label) label = GTK_BUTTON(m_widget)->child;
29006414 347
fd0eed64 348 gtk_widget_set_style( label, m_widgetStyle );
29006414 349
fd0eed64
RR
350 child = child->next;
351 }
f96aa4d9
RR
352}
353
071a2d78 354size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item)
e01c8145
VZ
355{
356 GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() );
357
358 size_t index;
359 if ( m_strings )
360 {
361 // sorted control, need to insert at the correct index
362 index = m_strings->Add(item);
363
364 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
365
366 if ( index )
367 {
368 m_clientList.Insert( m_clientList.Item(index - 1),
369 (wxObject*) NULL );
370 }
371 else
372 {
6c8a980f 373 m_clientList.Insert( (wxObject*) NULL );
e01c8145
VZ
374 }
375 }
376 else
377 {
378 // normal control, just append
379 gtk_menu_append( GTK_MENU(menu), menu_item );
380
381 m_clientList.Append( (wxObject*) NULL );
382
383 // don't call wxChoice::GetCount() from here because it doesn't work
384 // if we're called from ctor (and GtkMenuShell is still NULL)
11e1c70d 385 index = m_clientList.GetCount() - 1;
e01c8145
VZ
386 }
387
388 if (GTK_WIDGET_REALIZED(m_widget))
389 {
390 gtk_widget_realize( menu_item );
391 gtk_widget_realize( GTK_BIN(menu_item)->child );
392
393 if (m_widgetStyle) ApplyWidgetStyle();
394 }
395
396 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
397 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
398
399 gtk_widget_show( menu_item );
400
401 // return the index of the item in the control
402 return index;
403}
404
f68586e5
VZ
405wxSize wxChoice::DoGetBestSize() const
406{
db434467
RR
407 wxSize ret( wxControl::DoGetBestSize() );
408 if (ret.x < 80) ret.x = 80;
409 ret.y = 16 + gdk_char_height( m_widget->style->font, 'H' );
410 return ret;
f68586e5
VZ
411}
412
ce4169a4 413#endif