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