]>
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 |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
1e6feb95 | 10 | #include "wx/defs.h" |
c801d85f | 11 | |
ce4169a4 RR |
12 | #if wxUSE_CHOICE |
13 | ||
1e6feb95 | 14 | #include "wx/choice.h" |
2da2f941 | 15 | #include "wx/arrstr.h" |
1e6feb95 | 16 | |
9e691f46 | 17 | #include "wx/gtk/private.h" |
83624f79 | 18 | |
acfd422a RR |
19 | //----------------------------------------------------------------------------- |
20 | // idle system | |
21 | //----------------------------------------------------------------------------- | |
22 | ||
23 | extern void wxapp_install_idle_handler(); | |
24 | extern bool g_isIdle; | |
25 | ||
66bd6b93 RR |
26 | //----------------------------------------------------------------------------- |
27 | // data | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | extern bool g_blockEventsOnDrag; | |
31 | ||
c801d85f | 32 | //----------------------------------------------------------------------------- |
e1e955e1 | 33 | // "activate" |
c801d85f KB |
34 | //----------------------------------------------------------------------------- |
35 | ||
865bb325 | 36 | extern "C" { |
66bd6b93 | 37 | static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice ) |
c801d85f | 38 | { |
e01c8145 | 39 | if (g_isIdle) |
4dcaf11a | 40 | wxapp_install_idle_handler(); |
acfd422a | 41 | |
a2053b27 | 42 | if (!choice->m_hasVMT) return; |
29006414 | 43 | |
acfd422a | 44 | if (g_blockEventsOnDrag) return; |
29006414 | 45 | |
5f3565a2 RR |
46 | int selection = wxNOT_FOUND; |
47 | ||
48 | #ifdef __WXGTK20__ | |
49 | selection = gtk_option_menu_get_history( GTK_OPTION_MENU(choice->GetHandle()) ); | |
50 | #else | |
51 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(choice->GetHandle()) ) ); | |
52 | int count = 0; | |
53 | ||
54 | GList *child = menu_shell->children; | |
55 | while (child) | |
56 | { | |
57 | GtkBin *bin = GTK_BIN( child->data ); | |
58 | if (!bin->child) | |
59 | { | |
914ca1d8 | 60 | selection = count; |
5f3565a2 RR |
61 | break; |
62 | } | |
63 | child = child->next; | |
64 | count++; | |
65 | } | |
66 | #endif | |
67 | choice->m_selection_hack = selection; | |
68 | ||
acfd422a | 69 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() ); |
6c8a980f VZ |
70 | int n = choice->GetSelection(); |
71 | ||
72 | event.SetInt( n ); | |
acfd422a RR |
73 | event.SetString( choice->GetStringSelection() ); |
74 | event.SetEventObject(choice); | |
6c8a980f VZ |
75 | |
76 | if ( choice->HasClientObjectData() ) | |
77 | event.SetClientObject( choice->GetClientObject(n) ); | |
78 | else if ( choice->HasClientUntypedData() ) | |
79 | event.SetClientData( choice->GetClientData(n) ); | |
80 | ||
acfd422a | 81 | choice->GetEventHandler()->ProcessEvent(event); |
6de97a3b | 82 | } |
865bb325 | 83 | } |
c801d85f | 84 | |
e1e955e1 RR |
85 | //----------------------------------------------------------------------------- |
86 | // wxChoice | |
c801d85f KB |
87 | //----------------------------------------------------------------------------- |
88 | ||
7f4dc78d | 89 | IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl) |
c801d85f | 90 | |
fd0eed64 | 91 | wxChoice::wxChoice() |
c801d85f | 92 | { |
e01c8145 | 93 | m_strings = (wxSortedArrayString *)NULL; |
6de97a3b | 94 | } |
c801d85f | 95 | |
584ad2a3 MB |
96 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, |
97 | const wxPoint &pos, const wxSize &size, | |
98 | const wxArrayString& choices, | |
99 | long style, const wxValidator& validator, | |
100 | const wxString &name ) | |
101 | { | |
102 | wxCArrayString chs(choices); | |
103 | ||
104 | return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
105 | style, validator, name ); | |
106 | } | |
107 | ||
debe6624 | 108 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, |
fd0eed64 RR |
109 | const wxPoint &pos, const wxSize &size, |
110 | int n, const wxString choices[], | |
111 | long style, const wxValidator& validator, const wxString &name ) | |
c801d85f | 112 | { |
0a164d4c | 113 | m_needParent = true; |
034be888 | 114 | #if (GTK_MINOR_VERSION > 0) |
0a164d4c | 115 | m_acceptsFocus = true; |
034be888 | 116 | #endif |
29006414 | 117 | |
4dcaf11a RR |
118 | if (!PreCreation( parent, pos, size ) || |
119 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
120 | { | |
223d09f6 | 121 | wxFAIL_MSG( wxT("wxChoice creation failed") ); |
0a164d4c | 122 | return false; |
4dcaf11a | 123 | } |
6de97a3b | 124 | |
fd0eed64 | 125 | m_widget = gtk_option_menu_new(); |
29006414 | 126 | |
e01c8145 VZ |
127 | if ( style & wxCB_SORT ) |
128 | { | |
129 | // if our m_strings != NULL, DoAppend() will check for it and insert | |
130 | // items in the correct order | |
131 | m_strings = new wxSortedArrayString; | |
132 | } | |
133 | ||
16edee16 RR |
134 | // begin with no selection |
135 | m_selection_hack = wxNOT_FOUND; | |
136 | ||
fd0eed64 | 137 | GtkWidget *menu = gtk_menu_new(); |
29006414 | 138 | |
fd0eed64 RR |
139 | for (int i = 0; i < n; i++) |
140 | { | |
243dbf1a | 141 | GtkAddHelper(menu, i, choices[i]); |
fd0eed64 | 142 | } |
e01c8145 | 143 | |
fd0eed64 | 144 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); |
29006414 | 145 | |
f03fc89f | 146 | m_parent->DoAddChild( this ); |
29006414 | 147 | |
abdeb9e7 RD |
148 | PostCreation(size); |
149 | SetBestSize(size); // need this too because this is a wxControlWithItems | |
29006414 | 150 | |
0a164d4c | 151 | return true; |
6de97a3b | 152 | } |
29006414 | 153 | |
fd0eed64 RR |
154 | wxChoice::~wxChoice() |
155 | { | |
f5e27805 | 156 | Clear(); |
e01c8145 VZ |
157 | |
158 | delete m_strings; | |
fd0eed64 RR |
159 | } |
160 | ||
9abe166a | 161 | int wxChoice::DoAppend( const wxString &item ) |
fd0eed64 | 162 | { |
2ee3ee1b | 163 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") ); |
29006414 | 164 | |
fd0eed64 | 165 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); |
29006414 | 166 | |
243dbf1a VZ |
167 | return GtkAddHelper(menu, GetCount(), item); |
168 | } | |
169 | ||
170 | int wxChoice::DoInsert( const wxString &item, int pos ) | |
171 | { | |
172 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") ); | |
173 | wxCHECK_MSG( (pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); | |
174 | ||
175 | if (pos == GetCount()) | |
176 | return DoAppend(item); | |
177 | ||
178 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); | |
179 | ||
16edee16 RR |
180 | // if the item to insert is at or before the selection, and the selection is valid |
181 | if ((pos <= m_selection_hack) && (m_selection_hack != wxNOT_FOUND)) | |
182 | { | |
183 | // move the selection forward one | |
184 | m_selection_hack++; | |
185 | } | |
186 | ||
243dbf1a | 187 | return GtkAddHelper(menu, pos, item); |
fd0eed64 | 188 | } |
f96aa4d9 | 189 | |
6c8a980f | 190 | void wxChoice::DoSetItemClientData( int n, void* clientData ) |
fd0eed64 | 191 | { |
2ee3ee1b | 192 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); |
29006414 | 193 | |
222ed1d6 | 194 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
6c8a980f | 195 | wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") ); |
29006414 | 196 | |
f5e27805 | 197 | node->SetData( (wxObject*) clientData ); |
fd0eed64 RR |
198 | } |
199 | ||
6c8a980f | 200 | void* wxChoice::DoGetItemClientData( int n ) const |
fd0eed64 | 201 | { |
2ee3ee1b | 202 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") ); |
29006414 | 203 | |
222ed1d6 | 204 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
6c8a980f | 205 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") ); |
29006414 | 206 | |
b1d4dd7a | 207 | return node->GetData(); |
6de97a3b | 208 | } |
fd0eed64 | 209 | |
6c8a980f | 210 | void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData ) |
fd0eed64 | 211 | { |
2ee3ee1b | 212 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); |
29006414 | 213 | |
222ed1d6 | 214 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
6c8a980f | 215 | wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") ); |
29006414 | 216 | |
edc973b1 | 217 | // wxItemContainer already deletes data for us |
29006414 | 218 | |
fd0eed64 RR |
219 | node->SetData( (wxObject*) clientData ); |
220 | } | |
221 | ||
6c8a980f | 222 | wxClientData* wxChoice::DoGetItemClientObject( int n ) const |
fd0eed64 | 223 | { |
2ee3ee1b | 224 | wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") ); |
29006414 | 225 | |
222ed1d6 | 226 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
9abe166a | 227 | wxCHECK_MSG( node, (wxClientData *)NULL, |
6c8a980f | 228 | wxT("invalid index in wxChoice::DoGetItemClientObject") ); |
29006414 | 229 | |
b1d4dd7a | 230 | return (wxClientData*) node->GetData(); |
fd0eed64 | 231 | } |
29006414 | 232 | |
fd0eed64 | 233 | void wxChoice::Clear() |
c801d85f | 234 | { |
223d09f6 | 235 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
f96aa4d9 | 236 | |
fd0eed64 RR |
237 | gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) ); |
238 | GtkWidget *menu = gtk_menu_new(); | |
239 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
29006414 | 240 | |
6c8a980f VZ |
241 | if ( HasClientObjectData() ) |
242 | { | |
243 | // destroy the data (due to Robert's idea of using wxList<wxObject> | |
244 | // and not wxList<wxClientData> we can't just say | |
0a164d4c | 245 | // m_clientList.DeleteContents(true) - this would crash! |
222ed1d6 | 246 | wxList::compatibility_iterator node = m_clientList.GetFirst(); |
6c8a980f VZ |
247 | while ( node ) |
248 | { | |
b1d4dd7a RL |
249 | delete (wxClientData *)node->GetData(); |
250 | node = node->GetNext(); | |
6c8a980f VZ |
251 | } |
252 | } | |
d6538e2c | 253 | m_clientList.Clear(); |
2ee3ee1b VZ |
254 | |
255 | if ( m_strings ) | |
256 | m_strings->Clear(); | |
16edee16 RR |
257 | |
258 | // begin with no selection | |
259 | m_selection_hack = wxNOT_FOUND; | |
6de97a3b | 260 | } |
c801d85f | 261 | |
645420d8 | 262 | void wxChoice::Delete( int n ) |
2f6407b9 | 263 | { |
645420d8 VZ |
264 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
265 | ||
266 | // VZ: apparently GTK+ doesn't have a built-in function to do it (not even | |
e2380ce1 | 267 | // in 2.0), hence this dumb implementation -- still better than nothing |
645420d8 VZ |
268 | int i, |
269 | count = GetCount(); | |
270 | ||
271 | wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") ); | |
272 | ||
16edee16 RR |
273 | // if the item to delete is before the selection, and the selection is valid |
274 | if ((n < m_selection_hack) && (m_selection_hack != wxNOT_FOUND)) | |
275 | { | |
276 | // move the selection back one | |
277 | m_selection_hack--; | |
278 | } | |
279 | else if (n == m_selection_hack) | |
280 | { | |
281 | // invalidate the selection | |
282 | m_selection_hack = wxNOT_FOUND; | |
283 | } | |
284 | ||
e2380ce1 VZ |
285 | const bool hasClientData = m_clientDataItemsType != wxClientData_None; |
286 | const bool hasObjectData = m_clientDataItemsType == wxClientData_Object; | |
287 | ||
288 | wxList::compatibility_iterator node = m_clientList.GetFirst(); | |
289 | ||
645420d8 | 290 | wxArrayString items; |
e2380ce1 | 291 | wxArrayPtrVoid itemsData; |
645420d8 VZ |
292 | items.Alloc(count); |
293 | for ( i = 0; i < count; i++ ) | |
294 | { | |
295 | if ( i != n ) | |
e2380ce1 | 296 | { |
645420d8 | 297 | items.Add(GetString(i)); |
e2380ce1 VZ |
298 | if ( hasClientData ) |
299 | { | |
300 | // also save the client data | |
301 | itemsData.Add(node->GetData()); | |
302 | } | |
303 | } | |
304 | else // need to delete the client object too | |
305 | { | |
306 | if ( hasObjectData ) | |
307 | { | |
308 | delete (wxClientData *)node->GetData(); | |
309 | } | |
310 | } | |
311 | ||
312 | if ( hasClientData ) | |
313 | { | |
314 | node = node->GetNext(); | |
315 | } | |
316 | } | |
317 | ||
318 | if ( hasObjectData ) | |
319 | { | |
320 | // prevent Clear() from destroying all client data | |
321 | m_clientDataItemsType = wxClientData_None; | |
645420d8 VZ |
322 | } |
323 | ||
324 | Clear(); | |
325 | ||
326 | for ( i = 0; i < count - 1; i++ ) | |
327 | { | |
328 | Append(items[i]); | |
e2380ce1 VZ |
329 | |
330 | if ( hasObjectData ) | |
331 | SetClientObject(i, (wxClientData *)itemsData[i]); | |
332 | else if ( hasClientData ) | |
f6bc4102 | 333 | SetClientData(i, itemsData[i]); |
645420d8 | 334 | } |
2f6407b9 RR |
335 | } |
336 | ||
c801d85f KB |
337 | int wxChoice::FindString( const wxString &string ) const |
338 | { | |
223d09f6 | 339 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") ); |
fd0eed64 RR |
340 | |
341 | // If you read this code once and you think you understand | |
342 | // it, then you are very wrong. Robert Roebling. | |
29006414 | 343 | |
fd0eed64 RR |
344 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
345 | int count = 0; | |
346 | GList *child = menu_shell->children; | |
347 | while (child) | |
348 | { | |
349 | GtkBin *bin = GTK_BIN( child->data ); | |
350 | GtkLabel *label = (GtkLabel *) NULL; | |
9e691f46 VZ |
351 | if (bin->child) |
352 | label = GTK_LABEL(bin->child); | |
353 | if (!label) | |
354 | label = GTK_LABEL( BUTTON_CHILD(m_widget) ); | |
29006414 | 355 | |
223d09f6 | 356 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); |
e2380ce1 | 357 | |
2e1d7104 RR |
358 | #ifdef __WXGTK20__ |
359 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) ); | |
360 | #else | |
361 | wxString tmp( label->label ); | |
362 | #endif | |
fab591c5 | 363 | if (string == tmp) |
9e691f46 | 364 | return count; |
29006414 | 365 | |
9e691f46 VZ |
366 | child = child->next; |
367 | count++; | |
fd0eed64 | 368 | } |
29006414 | 369 | |
0a164d4c | 370 | return wxNOT_FOUND; |
6de97a3b | 371 | } |
c801d85f | 372 | |
9abe166a | 373 | int wxChoice::GetSelection() const |
c801d85f | 374 | { |
223d09f6 | 375 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") ); |
e2380ce1 | 376 | |
16edee16 RR |
377 | return m_selection_hack; |
378 | ||
6de97a3b | 379 | } |
c801d85f | 380 | |
a912c184 | 381 | void wxChoice::SetString( int n, const wxString& str ) |
6c8a980f VZ |
382 | { |
383 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); | |
384 | ||
34b5e560 RR |
385 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
386 | int count = 0; | |
387 | GList *child = menu_shell->children; | |
388 | while (child) | |
389 | { | |
390 | GtkBin *bin = GTK_BIN( child->data ); | |
391 | if (count == n) | |
392 | { | |
393 | GtkLabel *label = (GtkLabel *) NULL; | |
394 | if (bin->child) | |
395 | label = GTK_LABEL(bin->child); | |
396 | if (!label) | |
397 | label = GTK_LABEL( BUTTON_CHILD(m_widget) ); | |
398 | ||
399 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); | |
400 | ||
0a164d4c WS |
401 | gtk_label_set_text( label, wxGTK_CONV( str ) ); |
402 | ||
34b5e560 RR |
403 | return; |
404 | } | |
405 | child = child->next; | |
406 | count++; | |
407 | } | |
6c8a980f VZ |
408 | } |
409 | ||
debe6624 | 410 | wxString wxChoice::GetString( int n ) const |
c801d85f | 411 | { |
0a164d4c | 412 | wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid choice") ); |
fd0eed64 RR |
413 | |
414 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
415 | int count = 0; | |
416 | GList *child = menu_shell->children; | |
417 | while (child) | |
c801d85f | 418 | { |
fd0eed64 RR |
419 | GtkBin *bin = GTK_BIN( child->data ); |
420 | if (count == n) | |
421 | { | |
422 | GtkLabel *label = (GtkLabel *) NULL; | |
9e691f46 VZ |
423 | if (bin->child) |
424 | label = GTK_LABEL(bin->child); | |
425 | if (!label) | |
426 | label = GTK_LABEL( BUTTON_CHILD(m_widget) ); | |
29006414 | 427 | |
223d09f6 | 428 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); |
29006414 | 429 | |
2e1d7104 RR |
430 | #ifdef __WXGTK20__ |
431 | return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) ); | |
432 | #else | |
433 | return wxString( label->label ); | |
434 | #endif | |
fd0eed64 RR |
435 | } |
436 | child = child->next; | |
437 | count++; | |
6de97a3b | 438 | } |
29006414 | 439 | |
223d09f6 | 440 | wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") ); |
29006414 | 441 | |
0a164d4c | 442 | return wxEmptyString; |
6de97a3b | 443 | } |
c801d85f | 444 | |
9abe166a | 445 | int wxChoice::GetCount() const |
c801d85f | 446 | { |
223d09f6 | 447 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") ); |
fd0eed64 RR |
448 | |
449 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
450 | int count = 0; | |
451 | GList *child = menu_shell->children; | |
452 | while (child) | |
453 | { | |
454 | count++; | |
455 | child = child->next; | |
456 | } | |
457 | return count; | |
6de97a3b | 458 | } |
c801d85f | 459 | |
debe6624 | 460 | void wxChoice::SetSelection( int n ) |
c801d85f | 461 | { |
223d09f6 | 462 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
f96aa4d9 | 463 | |
fd0eed64 RR |
464 | int tmp = n; |
465 | gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); | |
16edee16 RR |
466 | |
467 | // set the local selection variable manually | |
468 | if ((n >= 0) && (n < GetCount())) | |
469 | { | |
470 | // a valid selection has been made | |
471 | m_selection_hack = n; | |
472 | } | |
473 | else if ((n == wxNOT_FOUND) || (GetCount() == 0)) | |
474 | { | |
475 | // invalidates the selection if there are no items | |
476 | // or if it is specifically set to wxNOT_FOUND | |
477 | m_selection_hack = wxNOT_FOUND; | |
478 | } | |
479 | else | |
480 | { | |
481 | // this selects the first item by default if the selection is out of bounds | |
482 | m_selection_hack = 0; | |
483 | } | |
6de97a3b | 484 | } |
c801d85f | 485 | |
f40fdaa3 | 486 | void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style) |
868a2826 | 487 | { |
fd0eed64 | 488 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
29006414 | 489 | |
f40fdaa3 VS |
490 | gtk_widget_modify_style( m_widget, style ); |
491 | gtk_widget_modify_style( GTK_WIDGET( menu_shell ), style ); | |
29006414 | 492 | |
fd0eed64 RR |
493 | GList *child = menu_shell->children; |
494 | while (child) | |
495 | { | |
f40fdaa3 | 496 | gtk_widget_modify_style( GTK_WIDGET( child->data ), style ); |
29006414 | 497 | |
fd0eed64 RR |
498 | GtkBin *bin = GTK_BIN( child->data ); |
499 | GtkWidget *label = (GtkWidget *) NULL; | |
9e691f46 VZ |
500 | if (bin->child) |
501 | label = bin->child; | |
502 | if (!label) | |
503 | label = BUTTON_CHILD(m_widget); | |
29006414 | 504 | |
f40fdaa3 | 505 | gtk_widget_modify_style( label, style ); |
29006414 | 506 | |
fd0eed64 RR |
507 | child = child->next; |
508 | } | |
f96aa4d9 RR |
509 | } |
510 | ||
243dbf1a | 511 | int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item) |
e01c8145 | 512 | { |
243dbf1a VZ |
513 | wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index")); |
514 | ||
fab591c5 | 515 | GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) ); |
e01c8145 VZ |
516 | |
517 | size_t index; | |
518 | if ( m_strings ) | |
519 | { | |
520 | // sorted control, need to insert at the correct index | |
521 | index = m_strings->Add(item); | |
522 | ||
523 | gtk_menu_insert( GTK_MENU(menu), menu_item, index ); | |
524 | ||
525 | if ( index ) | |
526 | { | |
527 | m_clientList.Insert( m_clientList.Item(index - 1), | |
528 | (wxObject*) NULL ); | |
529 | } | |
530 | else | |
531 | { | |
6c8a980f | 532 | m_clientList.Insert( (wxObject*) NULL ); |
e01c8145 VZ |
533 | } |
534 | } | |
535 | else | |
536 | { | |
243dbf1a VZ |
537 | // don't call wxChoice::GetCount() from here because it doesn't work |
538 | // if we're called from ctor (and GtkMenuShell is still NULL) | |
539 | ||
e01c8145 | 540 | // normal control, just append |
243dbf1a VZ |
541 | if (pos == (int)m_clientList.GetCount()) |
542 | { | |
0a164d4c WS |
543 | gtk_menu_append( GTK_MENU(menu), menu_item ); |
544 | m_clientList.Append( (wxObject*) NULL ); | |
545 | index = m_clientList.GetCount() - 1; | |
243dbf1a VZ |
546 | } |
547 | else | |
548 | { | |
549 | gtk_menu_insert( GTK_MENU(menu), menu_item, pos ); | |
550 | m_clientList.Insert( pos, (wxObject*) NULL ); | |
551 | index = pos; | |
552 | } | |
e01c8145 VZ |
553 | } |
554 | ||
555 | if (GTK_WIDGET_REALIZED(m_widget)) | |
556 | { | |
557 | gtk_widget_realize( menu_item ); | |
558 | gtk_widget_realize( GTK_BIN(menu_item)->child ); | |
559 | ||
f40fdaa3 | 560 | ApplyWidgetStyle(); |
e01c8145 VZ |
561 | } |
562 | ||
9ddf4854 RR |
563 | // The best size of a wxChoice should probably |
564 | // be changed everytime the control has been | |
565 | // changed, but at least after adding an item | |
566 | // it has to change. Adapted from Matt Ownby. | |
567 | InvalidateBestSize(); | |
0a164d4c | 568 | |
58b907f6 | 569 | gtk_signal_connect_after( GTK_OBJECT( menu_item ), "activate", |
e01c8145 VZ |
570 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); |
571 | ||
572 | gtk_widget_show( menu_item ); | |
573 | ||
574 | // return the index of the item in the control | |
575 | return index; | |
576 | } | |
577 | ||
f68586e5 VZ |
578 | wxSize wxChoice::DoGetBestSize() const |
579 | { | |
db434467 | 580 | wxSize ret( wxControl::DoGetBestSize() ); |
29f54b9b VZ |
581 | |
582 | // we know better our horizontal extent: it depends on the longest string | |
583 | // we have | |
584 | ret.x = 0; | |
585 | if ( m_widget ) | |
586 | { | |
60d85ccb | 587 | int width; |
29f54b9b VZ |
588 | size_t count = GetCount(); |
589 | for ( size_t n = 0; n < count; n++ ) | |
590 | { | |
2b1ff57f | 591 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL ); |
29f54b9b VZ |
592 | if ( width > ret.x ) |
593 | ret.x = width; | |
594 | } | |
595 | ||
df336b7f VZ |
596 | // add extra for the choice "=" button |
597 | ||
598 | // VZ: I don't know how to get the right value, it seems to be in | |
599 | // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get | |
600 | // to it - maybe we can use gtk_option_menu_size_request() for this | |
601 | // somehow? | |
602 | // | |
603 | // This default value works only for the default GTK+ theme (i.e. | |
604 | // no theme at all) (FIXME) | |
605 | static const int widthChoiceIndicator = 35; | |
606 | ret.x += widthChoiceIndicator; | |
29f54b9b VZ |
607 | } |
608 | ||
609 | // but not less than the minimal width | |
610 | if ( ret.x < 80 ) | |
611 | ret.x = 80; | |
612 | ||
ebbb22bd RR |
613 | // If this request_size is called with no entries then |
614 | // the returned height is wrong. Give it a reasonable | |
615 | // default value. | |
616 | if (ret.y <= 18) | |
617 | ret.y = 8 + GetCharHeight(); | |
9e691f46 | 618 | |
9f884528 | 619 | CacheBestSize(ret); |
db434467 | 620 | return ret; |
f68586e5 VZ |
621 | } |
622 | ||
2b904684 RR |
623 | bool wxChoice::IsOwnGtkWindow( GdkWindow *window ) |
624 | { | |
625 | #ifdef __WXGTK20__ | |
626 | return GTK_BUTTON(m_widget)->event_window; | |
627 | #else | |
628 | return (window == m_widget->window); | |
629 | #endif | |
630 | } | |
631 | ||
9d522606 RD |
632 | // static |
633 | wxVisualAttributes | |
634 | wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
635 | { | |
636 | return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new); | |
637 | } | |
638 | ||
2b904684 | 639 | |
df336b7f | 640 | #endif // wxUSE_CHOICE |