]>
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 |
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 | ||
26 | extern void wxapp_install_idle_handler(); | |
27 | extern bool g_isIdle; | |
28 | ||
66bd6b93 RR |
29 | //----------------------------------------------------------------------------- |
30 | // data | |
31 | //----------------------------------------------------------------------------- | |
32 | ||
33 | extern bool g_blockEventsOnDrag; | |
34 | ||
c801d85f | 35 | //----------------------------------------------------------------------------- |
e1e955e1 | 36 | // "activate" |
c801d85f KB |
37 | //----------------------------------------------------------------------------- |
38 | ||
66bd6b93 | 39 | static 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 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 | 59 | IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl) |
c801d85f | 60 | |
fd0eed64 | 61 | wxChoice::wxChoice() |
c801d85f | 62 | { |
e01c8145 | 63 | m_strings = (wxSortedArrayString *)NULL; |
6de97a3b | 64 | } |
c801d85f | 65 | |
debe6624 | 66 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, |
fd0eed64 RR |
67 | const wxPoint &pos, const wxSize &size, |
68 | int n, const wxString choices[], | |
69 | long style, const wxValidator& validator, const wxString &name ) | |
c801d85f | 70 | { |
fd0eed64 | 71 | m_needParent = TRUE; |
034be888 RR |
72 | #if (GTK_MINOR_VERSION > 0) |
73 | m_acceptsFocus = TRUE; | |
74 | #endif | |
29006414 | 75 | |
4dcaf11a RR |
76 | if (!PreCreation( parent, pos, size ) || |
77 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
78 | { | |
223d09f6 | 79 | wxFAIL_MSG( wxT("wxChoice creation failed") ); |
e01c8145 | 80 | return FALSE; |
4dcaf11a | 81 | } |
6de97a3b | 82 | |
fd0eed64 | 83 | m_widget = gtk_option_menu_new(); |
29006414 VZ |
84 | |
85 | wxSize newSize(size); | |
86 | if (newSize.x == -1) | |
87 | newSize.x = 80; | |
88 | if (newSize.y == -1) | |
89 | newSize.y = 26; | |
fd0eed64 | 90 | SetSize( newSize.x, newSize.y ); |
29006414 | 91 | |
e01c8145 VZ |
92 | if ( style & wxCB_SORT ) |
93 | { | |
94 | // if our m_strings != NULL, DoAppend() will check for it and insert | |
95 | // items in the correct order | |
96 | m_strings = new wxSortedArrayString; | |
97 | } | |
98 | ||
fd0eed64 | 99 | GtkWidget *menu = gtk_menu_new(); |
29006414 | 100 | |
fd0eed64 RR |
101 | for (int i = 0; i < n; i++) |
102 | { | |
e01c8145 | 103 | AppendHelper(menu, choices[i]); |
fd0eed64 | 104 | } |
e01c8145 | 105 | |
fd0eed64 | 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 |
121 | wxChoice::~wxChoice() |
122 | { | |
f5e27805 | 123 | Clear(); |
e01c8145 VZ |
124 | |
125 | delete m_strings; | |
fd0eed64 RR |
126 | } |
127 | ||
9abe166a | 128 | int wxChoice::DoAppend( const wxString &item ) |
fd0eed64 | 129 | { |
9abe166a | 130 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") ); |
29006414 | 131 | |
fd0eed64 | 132 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); |
29006414 | 133 | |
e01c8145 | 134 | return AppendHelper(menu, item); |
fd0eed64 | 135 | } |
f96aa4d9 | 136 | |
9abe166a | 137 | void wxChoice::DoSetClientData( int n, void* clientData ) |
fd0eed64 | 138 | { |
223d09f6 | 139 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
29006414 | 140 | |
d6538e2c | 141 | wxNode *node = m_clientList.Nth( n ); |
9abe166a | 142 | wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetClientData") ); |
29006414 | 143 | |
f5e27805 | 144 | node->SetData( (wxObject*) clientData ); |
fd0eed64 RR |
145 | } |
146 | ||
9abe166a | 147 | void* wxChoice::DoGetClientData( int n ) const |
fd0eed64 | 148 | { |
223d09f6 | 149 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") ); |
29006414 | 150 | |
d6538e2c | 151 | wxNode *node = m_clientList.Nth( n ); |
9abe166a | 152 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetClientData") ); |
29006414 | 153 | |
f5e27805 | 154 | return node->Data(); |
6de97a3b | 155 | } |
fd0eed64 | 156 | |
9abe166a | 157 | void wxChoice::DoSetClientObject( int n, wxClientData* clientData ) |
fd0eed64 | 158 | { |
223d09f6 | 159 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
29006414 | 160 | |
d6538e2c | 161 | wxNode *node = m_clientList.Nth( n ); |
9abe166a | 162 | wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetClientObject") ); |
29006414 | 163 | |
fd0eed64 | 164 | wxClientData *cd = (wxClientData*) node->Data(); |
9abe166a | 165 | delete cd; |
29006414 | 166 | |
fd0eed64 RR |
167 | node->SetData( (wxObject*) clientData ); |
168 | } | |
169 | ||
9abe166a | 170 | wxClientData* wxChoice::DoGetClientObject( int n ) const |
fd0eed64 | 171 | { |
223d09f6 | 172 | wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid combobox") ); |
29006414 | 173 | |
d6538e2c | 174 | wxNode *node = m_clientList.Nth( n ); |
9abe166a VZ |
175 | wxCHECK_MSG( node, (wxClientData *)NULL, |
176 | wxT("invalid index in wxChoice::DoGetClientObject") ); | |
29006414 | 177 | |
fd0eed64 RR |
178 | return (wxClientData*) node->Data(); |
179 | } | |
29006414 | 180 | |
fd0eed64 | 181 | void wxChoice::Clear() |
c801d85f | 182 | { |
223d09f6 | 183 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
f96aa4d9 | 184 | |
fd0eed64 RR |
185 | gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) ); |
186 | GtkWidget *menu = gtk_menu_new(); | |
187 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
29006414 | 188 | |
d6538e2c | 189 | if (m_clientDataItemsType == ClientData_Object) |
fd0eed64 | 190 | { |
e01c8145 | 191 | wxNode *node = m_clientList.First(); |
d6538e2c RR |
192 | while (node) |
193 | { | |
194 | wxClientData *cd = (wxClientData*)node->Data(); | |
195 | if (cd) delete cd; | |
196 | node = node->Next(); | |
e01c8145 | 197 | } |
fd0eed64 | 198 | } |
d6538e2c | 199 | m_clientList.Clear(); |
6de97a3b | 200 | } |
c801d85f | 201 | |
2f6407b9 RR |
202 | void wxChoice::Delete( int WXUNUSED(n) ) |
203 | { | |
223d09f6 | 204 | wxFAIL_MSG( wxT("wxChoice:Delete not implemented") ); |
2f6407b9 RR |
205 | } |
206 | ||
c801d85f KB |
207 | int wxChoice::FindString( const wxString &string ) const |
208 | { | |
223d09f6 | 209 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") ); |
fd0eed64 RR |
210 | |
211 | // If you read this code once and you think you understand | |
212 | // it, then you are very wrong. Robert Roebling. | |
29006414 | 213 | |
fd0eed64 RR |
214 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
215 | int count = 0; | |
216 | GList *child = menu_shell->children; | |
217 | while (child) | |
218 | { | |
219 | GtkBin *bin = GTK_BIN( child->data ); | |
220 | GtkLabel *label = (GtkLabel *) NULL; | |
221 | if (bin->child) label = GTK_LABEL(bin->child); | |
222 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); | |
29006414 | 223 | |
223d09f6 | 224 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); |
29006414 | 225 | |
dcf924a3 | 226 | if (string == wxString(label->label,*wxConvCurrent)) |
29006414 VZ |
227 | return count; |
228 | ||
fd0eed64 RR |
229 | child = child->next; |
230 | count++; | |
231 | } | |
29006414 | 232 | |
fd0eed64 | 233 | return -1; |
6de97a3b | 234 | } |
c801d85f | 235 | |
9abe166a | 236 | int wxChoice::GetSelection() const |
c801d85f | 237 | { |
223d09f6 | 238 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") ); |
fd0eed64 RR |
239 | |
240 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
241 | int count = 0; | |
242 | GList *child = menu_shell->children; | |
243 | while (child) | |
244 | { | |
245 | GtkBin *bin = GTK_BIN( child->data ); | |
246 | if (!bin->child) return count; | |
247 | child = child->next; | |
248 | count++; | |
249 | } | |
29006414 | 250 | |
fd0eed64 | 251 | return -1; |
6de97a3b | 252 | } |
c801d85f | 253 | |
debe6624 | 254 | wxString wxChoice::GetString( int n ) const |
c801d85f | 255 | { |
223d09f6 | 256 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") ); |
fd0eed64 RR |
257 | |
258 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
259 | int count = 0; | |
260 | GList *child = menu_shell->children; | |
261 | while (child) | |
c801d85f | 262 | { |
fd0eed64 RR |
263 | GtkBin *bin = GTK_BIN( child->data ); |
264 | if (count == n) | |
265 | { | |
266 | GtkLabel *label = (GtkLabel *) NULL; | |
267 | if (bin->child) label = GTK_LABEL(bin->child); | |
268 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); | |
29006414 | 269 | |
223d09f6 | 270 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); |
29006414 | 271 | |
dcf924a3 | 272 | return wxString(label->label,*wxConvCurrent); |
fd0eed64 RR |
273 | } |
274 | child = child->next; | |
275 | count++; | |
6de97a3b | 276 | } |
29006414 | 277 | |
223d09f6 | 278 | wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") ); |
29006414 | 279 | |
223d09f6 | 280 | return wxT(""); |
6de97a3b | 281 | } |
c801d85f | 282 | |
9abe166a | 283 | int wxChoice::GetCount() const |
c801d85f | 284 | { |
223d09f6 | 285 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") ); |
fd0eed64 RR |
286 | |
287 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
288 | int count = 0; | |
289 | GList *child = menu_shell->children; | |
290 | while (child) | |
291 | { | |
292 | count++; | |
293 | child = child->next; | |
294 | } | |
295 | return count; | |
6de97a3b | 296 | } |
c801d85f | 297 | |
debe6624 | 298 | void wxChoice::SetSelection( int n ) |
c801d85f | 299 | { |
223d09f6 | 300 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
f96aa4d9 | 301 | |
fd0eed64 RR |
302 | int tmp = n; |
303 | gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); | |
6de97a3b | 304 | } |
c801d85f | 305 | |
953704c1 RR |
306 | void wxChoice::DisableEvents() |
307 | { | |
308 | /* | |
309 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
310 | GList *child = menu_shell->children; | |
311 | while (child) | |
312 | { | |
313 | gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ), | |
314 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
315 | ||
316 | child = child->next; | |
317 | } | |
318 | */ | |
319 | } | |
320 | ||
321 | void wxChoice::EnableEvents() | |
322 | { | |
323 | /* | |
324 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
325 | GList *child = menu_shell->children; | |
326 | while (child) | |
327 | { | |
328 | gtk_signal_connect( GTK_OBJECT( child->data ), "activate", | |
329 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
330 | ||
331 | child = child->next; | |
332 | } | |
333 | */ | |
334 | } | |
335 | ||
58614078 | 336 | void wxChoice::ApplyWidgetStyle() |
868a2826 | 337 | { |
fd0eed64 | 338 | SetWidgetStyle(); |
29006414 | 339 | |
fd0eed64 | 340 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
29006414 | 341 | |
fd0eed64 RR |
342 | gtk_widget_set_style( m_widget, m_widgetStyle ); |
343 | gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle ); | |
29006414 | 344 | |
fd0eed64 RR |
345 | GList *child = menu_shell->children; |
346 | while (child) | |
347 | { | |
348 | gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle ); | |
29006414 | 349 | |
fd0eed64 RR |
350 | GtkBin *bin = GTK_BIN( child->data ); |
351 | GtkWidget *label = (GtkWidget *) NULL; | |
352 | if (bin->child) label = bin->child; | |
353 | if (!label) label = GTK_BUTTON(m_widget)->child; | |
29006414 | 354 | |
fd0eed64 | 355 | gtk_widget_set_style( label, m_widgetStyle ); |
29006414 | 356 | |
fd0eed64 RR |
357 | child = child->next; |
358 | } | |
f96aa4d9 RR |
359 | } |
360 | ||
e01c8145 VZ |
361 | size_t wxChoice::AppendHelper(GtkWidget *menu, const wxString& item) |
362 | { | |
363 | GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() ); | |
364 | ||
365 | size_t index; | |
366 | if ( m_strings ) | |
367 | { | |
368 | // sorted control, need to insert at the correct index | |
369 | index = m_strings->Add(item); | |
370 | ||
371 | gtk_menu_insert( GTK_MENU(menu), menu_item, index ); | |
372 | ||
373 | if ( index ) | |
374 | { | |
375 | m_clientList.Insert( m_clientList.Item(index - 1), | |
376 | (wxObject*) NULL ); | |
377 | } | |
378 | else | |
379 | { | |
380 | // can't use Insert() :-( | |
381 | m_clientList.Append( (wxObject*) NULL ); | |
382 | } | |
383 | } | |
384 | else | |
385 | { | |
386 | // normal control, just append | |
387 | gtk_menu_append( GTK_MENU(menu), menu_item ); | |
388 | ||
389 | m_clientList.Append( (wxObject*) NULL ); | |
390 | ||
391 | // don't call wxChoice::GetCount() from here because it doesn't work | |
392 | // if we're called from ctor (and GtkMenuShell is still NULL) | |
393 | index = m_clientList.GetCount(); | |
394 | } | |
395 | ||
396 | if (GTK_WIDGET_REALIZED(m_widget)) | |
397 | { | |
398 | gtk_widget_realize( menu_item ); | |
399 | gtk_widget_realize( GTK_BIN(menu_item)->child ); | |
400 | ||
401 | if (m_widgetStyle) ApplyWidgetStyle(); | |
402 | } | |
403 | ||
404 | gtk_signal_connect( GTK_OBJECT( menu_item ), "activate", | |
405 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
406 | ||
407 | gtk_widget_show( menu_item ); | |
408 | ||
409 | // return the index of the item in the control | |
410 | return index; | |
411 | } | |
412 | ||
ce4169a4 | 413 | #endif |