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