]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/choice.cpp
Layout things in wxMessageBox
[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
83624f79
RR
17#include "gdk/gdk.h"
18#include "gtk/gtk.h"
19
66bd6b93
RR
20//-----------------------------------------------------------------------------
21// data
22//-----------------------------------------------------------------------------
23
24extern bool g_blockEventsOnDrag;
25
c801d85f 26//-----------------------------------------------------------------------------
e1e955e1 27// "activate"
c801d85f
KB
28//-----------------------------------------------------------------------------
29
66bd6b93 30static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
c801d85f 31{
29006414
VZ
32 if (!choice->HasVMT())
33 return;
34
35 if (g_blockEventsOnDrag)
36 return;
37
47908e25 38 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
c801d85f 39 event.SetInt( choice->GetSelection() );
29006414 40 event.SetString( choice->GetStringSelection() );
c801d85f 41 event.SetEventObject(choice);
47908e25 42 choice->GetEventHandler()->ProcessEvent(event);
6de97a3b 43}
c801d85f 44
e1e955e1
RR
45//-----------------------------------------------------------------------------
46// wxChoice
c801d85f
KB
47//-----------------------------------------------------------------------------
48
7f4dc78d 49IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
c801d85f 50
fd0eed64 51wxChoice::wxChoice()
c801d85f 52{
6de97a3b 53}
c801d85f 54
debe6624 55bool wxChoice::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
56 const wxPoint &pos, const wxSize &size,
57 int n, const wxString choices[],
58 long style, const wxValidator& validator, const wxString &name )
c801d85f 59{
fd0eed64 60 m_needParent = TRUE;
034be888
RR
61#if (GTK_MINOR_VERSION > 0)
62 m_acceptsFocus = TRUE;
63#endif
29006414 64
fd0eed64 65 PreCreation( parent, id, pos, size, style, name );
29006414 66
fd0eed64 67 SetValidator( validator );
6de97a3b 68
fd0eed64 69 m_widget = gtk_option_menu_new();
29006414
VZ
70
71 wxSize newSize(size);
72 if (newSize.x == -1)
73 newSize.x = 80;
74 if (newSize.y == -1)
75 newSize.y = 26;
fd0eed64 76 SetSize( newSize.x, newSize.y );
29006414 77
fd0eed64 78 GtkWidget *menu = gtk_menu_new();
29006414 79
fd0eed64
RR
80 for (int i = 0; i < n; i++)
81 {
82 m_clientDataList.Append( (wxObject*) NULL );
f5e27805 83 m_clientObjectList.Append( (wxObject*) NULL );
29006414 84
93c5dd39 85 GtkWidget *item = gtk_menu_item_new_with_label( choices[i].mbc_str() );
fd0eed64 86 gtk_menu_append( GTK_MENU(menu), item );
29006414 87
fd0eed64 88 gtk_widget_show( item );
29006414
VZ
89
90 gtk_signal_connect( GTK_OBJECT( item ), "activate",
fd0eed64
RR
91 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
92 }
93 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 94
fd0eed64 95 m_parent->AddChild( this );
6ca41e57 96
fd0eed64 97 (m_parent->m_insertCallback)( m_parent, this );
29006414 98
fd0eed64 99 PostCreation();
29006414 100
fd0eed64
RR
101 SetBackgroundColour( parent->GetBackgroundColour() );
102 SetForegroundColour( parent->GetForegroundColour() );
a7ac4461 103 SetFont( parent->GetFont() );
f96aa4d9 104
fd0eed64 105 Show( TRUE );
29006414 106
fd0eed64 107 return TRUE;
6de97a3b 108}
29006414 109
fd0eed64
RR
110wxChoice::~wxChoice()
111{
f5e27805 112 Clear();
fd0eed64
RR
113}
114
115void wxChoice::AppendCommon( const wxString &item )
116{
93c5dd39 117 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
29006414 118
fd0eed64 119 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
93c5dd39 120 GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() );
29006414 121
fd0eed64 122 gtk_menu_append( GTK_MENU(menu), menu_item );
29006414 123
2b07d713
RR
124 if (GTK_WIDGET_REALIZED(m_widget))
125 {
126 gtk_widget_realize( menu_item );
127 gtk_widget_realize( GTK_BIN(menu_item)->child );
29006414 128
2b07d713
RR
129 if (m_widgetStyle) ApplyWidgetStyle();
130 }
29006414
VZ
131
132 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
fd0eed64 133 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
29006414 134
fd0eed64
RR
135 gtk_widget_show( menu_item );
136}
137
c801d85f
KB
138void wxChoice::Append( const wxString &item )
139{
f5e27805
RR
140 m_clientDataList.Append( (wxObject*) NULL );
141 m_clientObjectList.Append( (wxObject*) NULL );
29006414 142
fd0eed64
RR
143 AppendCommon( item );
144}
145
146void wxChoice::Append( const wxString &item, void *clientData )
147{
f5e27805
RR
148 m_clientDataList.Append( (wxObject*) clientData );
149 m_clientObjectList.Append( (wxObject*) NULL );
29006414 150
fd0eed64
RR
151 AppendCommon( item );
152}
153
154void wxChoice::Append( const wxString &item, wxClientData *clientData )
155{
f5e27805
RR
156 m_clientObjectList.Append( (wxObject*) clientData );
157 m_clientDataList.Append( (wxObject*) NULL );
29006414 158
fd0eed64
RR
159 AppendCommon( item );
160}
f96aa4d9 161
fd0eed64
RR
162void wxChoice::SetClientData( int n, void* clientData )
163{
93c5dd39 164 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
29006414 165
fd0eed64
RR
166 wxNode *node = m_clientDataList.Nth( n );
167 if (!node) return;
29006414 168
f5e27805 169 node->SetData( (wxObject*) clientData );
fd0eed64
RR
170}
171
172void* wxChoice::GetClientData( int n )
173{
93c5dd39 174 wxCHECK_MSG( m_widget != NULL, NULL, _T("invalid combobox") );
29006414 175
fd0eed64
RR
176 wxNode *node = m_clientDataList.Nth( n );
177 if (!node) return NULL;
29006414 178
f5e27805 179 return node->Data();
6de97a3b 180}
fd0eed64
RR
181
182void wxChoice::SetClientObject( int n, wxClientData* clientData )
183{
93c5dd39 184 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
29006414 185
f5e27805 186 wxNode *node = m_clientObjectList.Nth( n );
fd0eed64 187 if (!node) return;
29006414 188
fd0eed64
RR
189 wxClientData *cd = (wxClientData*) node->Data();
190 if (cd) delete cd;
29006414 191
fd0eed64
RR
192 node->SetData( (wxObject*) clientData );
193}
194
195wxClientData* wxChoice::GetClientObject( int n )
196{
93c5dd39 197 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, _T("invalid combobox") );
29006414 198
f5e27805 199 wxNode *node = m_clientObjectList.Nth( n );
fd0eed64 200 if (!node) return (wxClientData*) NULL;
29006414 201
fd0eed64
RR
202 return (wxClientData*) node->Data();
203}
29006414 204
fd0eed64 205void wxChoice::Clear()
c801d85f 206{
93c5dd39 207 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
f96aa4d9 208
fd0eed64
RR
209 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
210 GtkWidget *menu = gtk_menu_new();
211 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 212
f5e27805 213 wxNode *node = m_clientObjectList.First();
fd0eed64
RR
214 while (node)
215 {
216 wxClientData *cd = (wxClientData*)node->Data();
217 if (cd) delete cd;
218 node = node->Next();
219 }
f5e27805 220 m_clientObjectList.Clear();
29006414 221
fd0eed64 222 m_clientDataList.Clear();
6de97a3b 223}
c801d85f 224
2f6407b9
RR
225void wxChoice::Delete( int WXUNUSED(n) )
226{
93c5dd39 227 wxFAIL_MSG( _T("wxChoice:Delete not implemented") );
2f6407b9
RR
228}
229
c801d85f
KB
230int wxChoice::FindString( const wxString &string ) const
231{
93c5dd39 232 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid choice") );
fd0eed64
RR
233
234 // If you read this code once and you think you understand
235 // it, then you are very wrong. Robert Roebling.
29006414 236
fd0eed64
RR
237 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
238 int count = 0;
239 GList *child = menu_shell->children;
240 while (child)
241 {
242 GtkBin *bin = GTK_BIN( child->data );
243 GtkLabel *label = (GtkLabel *) NULL;
244 if (bin->child) label = GTK_LABEL(bin->child);
245 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 246
93c5dd39 247 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
29006414
VZ
248
249 if (string == label->label)
250 return count;
251
fd0eed64
RR
252 child = child->next;
253 count++;
254 }
29006414 255
fd0eed64 256 return -1;
6de97a3b 257}
c801d85f 258
fd0eed64 259int wxChoice::GetColumns() const
c801d85f 260{
fd0eed64 261 return 1;
6de97a3b 262}
c801d85f 263
fd0eed64 264int wxChoice::GetSelection()
c801d85f 265{
93c5dd39 266 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid choice") );
fd0eed64
RR
267
268 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
269 int count = 0;
270 GList *child = menu_shell->children;
271 while (child)
272 {
273 GtkBin *bin = GTK_BIN( child->data );
274 if (!bin->child) return count;
275 child = child->next;
276 count++;
277 }
29006414 278
93c5dd39 279 wxFAIL_MSG( _T("wxChoice: no selection") );
29006414 280
fd0eed64 281 return -1;
6de97a3b 282}
c801d85f 283
debe6624 284wxString wxChoice::GetString( int n ) const
c801d85f 285{
93c5dd39 286 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid choice") );
fd0eed64
RR
287
288 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
289 int count = 0;
290 GList *child = menu_shell->children;
291 while (child)
c801d85f 292 {
fd0eed64
RR
293 GtkBin *bin = GTK_BIN( child->data );
294 if (count == n)
295 {
296 GtkLabel *label = (GtkLabel *) NULL;
297 if (bin->child) label = GTK_LABEL(bin->child);
298 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 299
93c5dd39 300 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
29006414 301
fd0eed64
RR
302 return label->label;
303 }
304 child = child->next;
305 count++;
6de97a3b 306 }
29006414 307
93c5dd39 308 wxFAIL_MSG( _T("wxChoice: invalid index in GetString()") );
29006414 309
fd0eed64 310 return "";
6de97a3b 311}
c801d85f 312
fd0eed64 313wxString wxChoice::GetStringSelection() const
c801d85f 314{
93c5dd39 315 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid choice") );
f96aa4d9 316
fd0eed64 317 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
29006414 318
93c5dd39 319 wxASSERT_MSG( label != NULL , _T("wxChoice: invalid label") );
29006414 320
fd0eed64 321 return label->label;
6de97a3b 322}
c801d85f 323
fd0eed64 324int wxChoice::Number() const
c801d85f 325{
93c5dd39 326 wxCHECK_MSG( m_widget != NULL, 0, _T("invalid choice") );
fd0eed64
RR
327
328 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
329 int count = 0;
330 GList *child = menu_shell->children;
331 while (child)
332 {
333 count++;
334 child = child->next;
335 }
336 return count;
6de97a3b 337}
c801d85f 338
debe6624 339void wxChoice::SetColumns( int WXUNUSED(n) )
c801d85f 340{
6de97a3b 341}
c801d85f 342
debe6624 343void wxChoice::SetSelection( int n )
c801d85f 344{
93c5dd39 345 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
f96aa4d9 346
fd0eed64
RR
347 int tmp = n;
348 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
29006414 349
fd0eed64 350 gtk_choice_clicked_callback( (GtkWidget *) NULL, this );
6de97a3b 351}
c801d85f
KB
352
353void wxChoice::SetStringSelection( const wxString &string )
354{
93c5dd39 355 wxCHECK_RET( m_widget != NULL, _T("invalid choice") );
f96aa4d9 356
fd0eed64
RR
357 int n = FindString( string );
358 if (n != -1) SetSelection( n );
6de97a3b 359}
c801d85f 360
58614078 361void wxChoice::ApplyWidgetStyle()
868a2826 362{
fd0eed64 363 SetWidgetStyle();
29006414 364
fd0eed64 365 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 366
fd0eed64
RR
367 gtk_widget_set_style( m_widget, m_widgetStyle );
368 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 369
fd0eed64
RR
370 GList *child = menu_shell->children;
371 while (child)
372 {
373 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 374
fd0eed64
RR
375 GtkBin *bin = GTK_BIN( child->data );
376 GtkWidget *label = (GtkWidget *) NULL;
377 if (bin->child) label = bin->child;
378 if (!label) label = GTK_BUTTON(m_widget)->child;
29006414 379
fd0eed64 380 gtk_widget_set_style( label, m_widgetStyle );
29006414 381
fd0eed64
RR
382 child = child->next;
383 }
f96aa4d9
RR
384}
385