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