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