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