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