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