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