]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/choice.cpp
It's possible now to save to a PNG. OK, I still
[wxWidgets.git] / src / gtk / choice.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: choice.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
6// Id:
7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11
12#ifdef __GNUG__
13#pragma implementation "choice.h"
14#endif
15
16#include "wx/choice.h"
17
66bd6b93
RR
18//-----------------------------------------------------------------------------
19// data
20//-----------------------------------------------------------------------------
21
22extern bool g_blockEventsOnDrag;
23
c801d85f
KB
24//-----------------------------------------------------------------------------
25// wxChoice
26//-----------------------------------------------------------------------------
27
66bd6b93 28static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
c801d85f 29{
66bd6b93
RR
30 if (!choice->HasVMT()) return;
31 if (g_blockEventsOnDrag) return;
32
47908e25 33 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
c801d85f
KB
34 event.SetInt( choice->GetSelection() );
35 wxString tmp( choice->GetStringSelection() );
36 event.SetString( WXSTRINGCAST(tmp) );
37 event.SetEventObject(choice);
47908e25 38 choice->GetEventHandler()->ProcessEvent(event);
6de97a3b 39}
c801d85f
KB
40
41//-----------------------------------------------------------------------------
42
7f4dc78d 43IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
c801d85f
KB
44
45wxChoice::wxChoice(void)
46{
6de97a3b 47}
c801d85f 48
debe6624
JS
49bool wxChoice::Create( wxWindow *parent, wxWindowID id,
50 const wxPoint &pos, const wxSize &size,
51 int n, const wxString choices[],
6de97a3b 52 long style, const wxValidator& validator, const wxString &name )
c801d85f
KB
53{
54 m_needParent = TRUE;
55
56 PreCreation( parent, id, pos, size, style, name );
57
6de97a3b
RR
58 SetValidator( validator );
59
c801d85f
KB
60 m_widget = gtk_option_menu_new();
61
62 wxSize newSize = size;
63 if (newSize.x == -1) newSize.x = 80;
64 if (newSize.y == -1) newSize.y = 26;
65 SetSize( newSize.x, newSize.y );
66
67 GtkWidget *menu;
68 menu = gtk_menu_new();
69
70 for (int i = 0; i < n; i++)
71 {
72 GtkWidget *item;
73 item = gtk_menu_item_new_with_label( choices[i] );
74 gtk_signal_connect( GTK_OBJECT( item ), "activate",
75 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
76 gtk_menu_append( GTK_MENU(menu), item );
77 gtk_widget_show( item );
6de97a3b 78 }
c801d85f
KB
79 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
80
81 PostCreation();
82
83 Show( TRUE );
84
85 return TRUE;
6de97a3b 86}
c801d85f
KB
87
88void wxChoice::Append( const wxString &item )
89{
90 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
91 GtkWidget *menu_item;
92 menu_item = gtk_menu_item_new_with_label( item );
93 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
94 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
95 gtk_menu_append( GTK_MENU(menu), menu_item );
96 gtk_widget_show( menu_item );
6de97a3b 97}
c801d85f
KB
98
99void wxChoice::Clear(void)
100{
101 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
102 GtkWidget *menu = gtk_menu_new();
103 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
6de97a3b 104}
c801d85f 105
2f6407b9
RR
106void wxChoice::Delete( int WXUNUSED(n) )
107{
108 wxFAIL_MSG( "wxChoice:Delete not implemented" );
109}
110
c801d85f
KB
111int wxChoice::FindString( const wxString &string ) const
112{
47908e25
RR
113 // If you read this code once and you think you understand
114 // it, then you are very wrong. Robert Roebling.
c801d85f
KB
115
116 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
117 int count = 0;
118 GList *child = menu_shell->children;
119 while (child)
120 {
121 GtkBin *bin = GTK_BIN( child->data );
c67daf87 122 GtkLabel *label = (GtkLabel *) NULL;
47908e25 123 if (bin->child) label = GTK_LABEL(bin->child);
b6af8d80
RR
124
125 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
126
c801d85f
KB
127 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
128 if (string == label->label) return count;
129 child = child->next;
130 count++;
6de97a3b 131 }
b6af8d80
RR
132
133 wxFAIL_MSG( "wxChoice: string not found" );
134
c801d85f 135 return -1;
6de97a3b 136}
c801d85f
KB
137
138int wxChoice::GetColumns(void) const
139{
140 return 1;
6de97a3b 141}
c801d85f
KB
142
143int wxChoice::GetSelection(void)
144{
145 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
146 int count = 0;
147 GList *child = menu_shell->children;
148 while (child)
149 {
150 GtkBin *bin = GTK_BIN( child->data );
151 if (!bin->child) return count;
152 child = child->next;
153 count++;
6de97a3b 154 }
b6af8d80
RR
155
156 wxFAIL_MSG( "wxChoice: no selection" );
157
c801d85f 158 return -1;
6de97a3b 159}
c801d85f 160
debe6624 161wxString wxChoice::GetString( int n ) const
c801d85f
KB
162{
163 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
164 int count = 0;
165 GList *child = menu_shell->children;
166 while (child)
167 {
168 GtkBin *bin = GTK_BIN( child->data );
169 if (count == n)
170 {
c67daf87 171 GtkLabel *label = (GtkLabel *) NULL;
47908e25 172 if (bin->child) label = GTK_LABEL(bin->child);
b6af8d80
RR
173
174 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
175
c801d85f
KB
176 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
177 return label->label;
6de97a3b 178 }
c801d85f
KB
179 child = child->next;
180 count++;
6de97a3b 181 }
b6af8d80
RR
182
183 wxFAIL_MSG( "wxChoice: string not found" );
184
c801d85f 185 return "";
6de97a3b 186}
c801d85f
KB
187
188wxString wxChoice::GetStringSelection(void) const
189{
190 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
b6af8d80
RR
191
192 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
193
c801d85f 194 return label->label;
6de97a3b 195}
c801d85f
KB
196
197int wxChoice::Number(void) const
198{
7a4b9130 199 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
c801d85f 200 int count = 0;
7a4b9130 201 GList *child = menu_shell->children;
c801d85f
KB
202 while (child)
203 {
204 count++;
205 child = child->next;
6de97a3b 206 }
c801d85f 207 return count;
6de97a3b 208}
c801d85f 209
debe6624 210void wxChoice::SetColumns( int WXUNUSED(n) )
c801d85f 211{
6de97a3b 212}
c801d85f 213
debe6624 214void wxChoice::SetSelection( int n )
c801d85f
KB
215{
216 int tmp = n;
217 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
47908e25 218
c67daf87 219 gtk_choice_clicked_callback( (GtkWidget *) NULL, this );
6de97a3b 220}
c801d85f
KB
221
222void wxChoice::SetStringSelection( const wxString &string )
223{
224 int n = FindString( string );
225 if (n != -1) SetSelection( n );
6de97a3b 226}
c801d85f 227