]>
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 | |
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 | |
fd0eed64 RR |
123 | for (int i = 0; i < n; i++) |
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 | ||
154 | int wxChoice::DoInsert( const wxString &item, int pos ) | |
155 | { | |
156 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") ); | |
157 | wxCHECK_MSG( (pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); | |
158 | ||
159 | if (pos == GetCount()) | |
160 | return DoAppend(item); | |
161 | ||
162 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); | |
163 | ||
16edee16 RR |
164 | // if the item to insert is at or before the selection, and the selection is valid |
165 | if ((pos <= m_selection_hack) && (m_selection_hack != wxNOT_FOUND)) | |
166 | { | |
167 | // move the selection forward one | |
168 | m_selection_hack++; | |
169 | } | |
170 | ||
243dbf1a | 171 | return GtkAddHelper(menu, pos, item); |
fd0eed64 | 172 | } |
f96aa4d9 | 173 | |
6c8a980f | 174 | void wxChoice::DoSetItemClientData( 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 | ||
6c8a980f | 184 | void* wxChoice::DoGetItemClientData( 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 | |
6c8a980f | 194 | void wxChoice::DoSetItemClientObject( 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 | ||
6c8a980f | 206 | wxClientData* wxChoice::DoGetItemClientObject( 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 | |
645420d8 | 246 | void wxChoice::Delete( int n ) |
2f6407b9 | 247 | { |
645420d8 VZ |
248 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
249 | ||
250 | // VZ: apparently GTK+ doesn't have a built-in function to do it (not even | |
e2380ce1 | 251 | // in 2.0), hence this dumb implementation -- still better than nothing |
645420d8 VZ |
252 | int i, |
253 | count = GetCount(); | |
254 | ||
255 | wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") ); | |
256 | ||
16edee16 RR |
257 | // if the item to delete is before the selection, and the selection is valid |
258 | if ((n < m_selection_hack) && (m_selection_hack != wxNOT_FOUND)) | |
259 | { | |
260 | // move the selection back one | |
261 | m_selection_hack--; | |
262 | } | |
263 | else if (n == m_selection_hack) | |
264 | { | |
265 | // invalidate the selection | |
266 | m_selection_hack = wxNOT_FOUND; | |
267 | } | |
268 | ||
e2380ce1 VZ |
269 | const bool hasClientData = m_clientDataItemsType != wxClientData_None; |
270 | const bool hasObjectData = m_clientDataItemsType == wxClientData_Object; | |
271 | ||
272 | wxList::compatibility_iterator node = m_clientList.GetFirst(); | |
273 | ||
645420d8 | 274 | wxArrayString items; |
e2380ce1 | 275 | wxArrayPtrVoid itemsData; |
645420d8 VZ |
276 | items.Alloc(count); |
277 | for ( i = 0; i < count; i++ ) | |
278 | { | |
279 | if ( i != n ) | |
e2380ce1 | 280 | { |
645420d8 | 281 | items.Add(GetString(i)); |
e2380ce1 VZ |
282 | if ( hasClientData ) |
283 | { | |
284 | // also save the client data | |
285 | itemsData.Add(node->GetData()); | |
286 | } | |
287 | } | |
288 | else // need to delete the client object too | |
289 | { | |
290 | if ( hasObjectData ) | |
291 | { | |
292 | delete (wxClientData *)node->GetData(); | |
293 | } | |
294 | } | |
295 | ||
296 | if ( hasClientData ) | |
297 | { | |
298 | node = node->GetNext(); | |
299 | } | |
300 | } | |
301 | ||
302 | if ( hasObjectData ) | |
303 | { | |
304 | // prevent Clear() from destroying all client data | |
305 | m_clientDataItemsType = wxClientData_None; | |
645420d8 VZ |
306 | } |
307 | ||
308 | Clear(); | |
309 | ||
310 | for ( i = 0; i < count - 1; i++ ) | |
311 | { | |
312 | Append(items[i]); | |
e2380ce1 VZ |
313 | |
314 | if ( hasObjectData ) | |
315 | SetClientObject(i, (wxClientData *)itemsData[i]); | |
316 | else if ( hasClientData ) | |
f6bc4102 | 317 | SetClientData(i, itemsData[i]); |
645420d8 | 318 | } |
2f6407b9 RR |
319 | } |
320 | ||
11e62fe6 | 321 | int wxChoice::FindString( const wxString &string, bool bCase ) const |
c801d85f | 322 | { |
11e62fe6 | 323 | wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") ); |
fd0eed64 RR |
324 | |
325 | // If you read this code once and you think you understand | |
326 | // it, then you are very wrong. Robert Roebling. | |
29006414 | 327 | |
fd0eed64 RR |
328 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
329 | int count = 0; | |
330 | GList *child = menu_shell->children; | |
331 | while (child) | |
332 | { | |
333 | GtkBin *bin = GTK_BIN( child->data ); | |
334 | GtkLabel *label = (GtkLabel *) NULL; | |
9e691f46 VZ |
335 | if (bin->child) |
336 | label = GTK_LABEL(bin->child); | |
337 | if (!label) | |
afa7bd1e | 338 | label = GTK_LABEL(GTK_BIN(m_widget)->child); |
29006414 | 339 | |
223d09f6 | 340 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); |
e2380ce1 | 341 | |
2e1d7104 | 342 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) ); |
11e62fe6 | 343 | if (string.IsSameAs( tmp, bCase )) |
9e691f46 | 344 | return count; |
29006414 | 345 | |
9e691f46 VZ |
346 | child = child->next; |
347 | count++; | |
fd0eed64 | 348 | } |
29006414 | 349 | |
0a164d4c | 350 | return wxNOT_FOUND; |
6de97a3b | 351 | } |
c801d85f | 352 | |
9abe166a | 353 | int wxChoice::GetSelection() const |
c801d85f | 354 | { |
11e62fe6 | 355 | wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") ); |
e2380ce1 | 356 | |
16edee16 RR |
357 | return m_selection_hack; |
358 | ||
6de97a3b | 359 | } |
c801d85f | 360 | |
a912c184 | 361 | void wxChoice::SetString( int n, const wxString& str ) |
6c8a980f VZ |
362 | { |
363 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); | |
364 | ||
34b5e560 RR |
365 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
366 | int count = 0; | |
367 | GList *child = menu_shell->children; | |
368 | while (child) | |
369 | { | |
370 | GtkBin *bin = GTK_BIN( child->data ); | |
371 | if (count == n) | |
372 | { | |
373 | GtkLabel *label = (GtkLabel *) NULL; | |
374 | if (bin->child) | |
375 | label = GTK_LABEL(bin->child); | |
376 | if (!label) | |
afa7bd1e | 377 | label = GTK_LABEL(GTK_BIN(m_widget)->child); |
34b5e560 RR |
378 | |
379 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); | |
380 | ||
0a164d4c WS |
381 | gtk_label_set_text( label, wxGTK_CONV( str ) ); |
382 | ||
34b5e560 RR |
383 | return; |
384 | } | |
385 | child = child->next; | |
386 | count++; | |
387 | } | |
6c8a980f VZ |
388 | } |
389 | ||
debe6624 | 390 | wxString wxChoice::GetString( int n ) const |
c801d85f | 391 | { |
0a164d4c | 392 | wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid choice") ); |
fd0eed64 RR |
393 | |
394 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
395 | int count = 0; | |
396 | GList *child = menu_shell->children; | |
397 | while (child) | |
c801d85f | 398 | { |
fd0eed64 RR |
399 | GtkBin *bin = GTK_BIN( child->data ); |
400 | if (count == n) | |
401 | { | |
402 | GtkLabel *label = (GtkLabel *) NULL; | |
9e691f46 VZ |
403 | if (bin->child) |
404 | label = GTK_LABEL(bin->child); | |
405 | if (!label) | |
afa7bd1e | 406 | label = GTK_LABEL(GTK_BIN(m_widget)->child); |
29006414 | 407 | |
223d09f6 | 408 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); |
29006414 | 409 | |
2e1d7104 | 410 | return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) ); |
fd0eed64 RR |
411 | } |
412 | child = child->next; | |
413 | count++; | |
6de97a3b | 414 | } |
29006414 | 415 | |
223d09f6 | 416 | wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") ); |
29006414 | 417 | |
0a164d4c | 418 | return wxEmptyString; |
6de97a3b | 419 | } |
c801d85f | 420 | |
9abe166a | 421 | int wxChoice::GetCount() const |
c801d85f | 422 | { |
223d09f6 | 423 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") ); |
fd0eed64 RR |
424 | |
425 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
426 | int count = 0; | |
427 | GList *child = menu_shell->children; | |
428 | while (child) | |
429 | { | |
430 | count++; | |
431 | child = child->next; | |
432 | } | |
433 | return count; | |
6de97a3b | 434 | } |
c801d85f | 435 | |
debe6624 | 436 | void wxChoice::SetSelection( int n ) |
c801d85f | 437 | { |
223d09f6 | 438 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
f96aa4d9 | 439 | |
fd0eed64 RR |
440 | int tmp = n; |
441 | gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); | |
16edee16 RR |
442 | |
443 | // set the local selection variable manually | |
444 | if ((n >= 0) && (n < GetCount())) | |
445 | { | |
446 | // a valid selection has been made | |
447 | m_selection_hack = n; | |
448 | } | |
449 | else if ((n == wxNOT_FOUND) || (GetCount() == 0)) | |
450 | { | |
451 | // invalidates the selection if there are no items | |
452 | // or if it is specifically set to wxNOT_FOUND | |
453 | m_selection_hack = wxNOT_FOUND; | |
454 | } | |
455 | else | |
456 | { | |
457 | // this selects the first item by default if the selection is out of bounds | |
458 | m_selection_hack = 0; | |
459 | } | |
6de97a3b | 460 | } |
c801d85f | 461 | |
f40fdaa3 | 462 | void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style) |
868a2826 | 463 | { |
fd0eed64 | 464 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
29006414 | 465 | |
f40fdaa3 VS |
466 | gtk_widget_modify_style( m_widget, style ); |
467 | gtk_widget_modify_style( GTK_WIDGET( menu_shell ), style ); | |
29006414 | 468 | |
fd0eed64 RR |
469 | GList *child = menu_shell->children; |
470 | while (child) | |
471 | { | |
f40fdaa3 | 472 | gtk_widget_modify_style( GTK_WIDGET( child->data ), style ); |
29006414 | 473 | |
fd0eed64 RR |
474 | GtkBin *bin = GTK_BIN( child->data ); |
475 | GtkWidget *label = (GtkWidget *) NULL; | |
9e691f46 VZ |
476 | if (bin->child) |
477 | label = bin->child; | |
478 | if (!label) | |
afa7bd1e | 479 | label = GTK_BIN(m_widget)->child; |
29006414 | 480 | |
f40fdaa3 | 481 | gtk_widget_modify_style( label, style ); |
29006414 | 482 | |
fd0eed64 RR |
483 | child = child->next; |
484 | } | |
f96aa4d9 RR |
485 | } |
486 | ||
243dbf1a | 487 | int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item) |
e01c8145 | 488 | { |
243dbf1a VZ |
489 | wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index")); |
490 | ||
fab591c5 | 491 | GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) ); |
e01c8145 VZ |
492 | |
493 | size_t index; | |
494 | if ( m_strings ) | |
495 | { | |
496 | // sorted control, need to insert at the correct index | |
497 | index = m_strings->Add(item); | |
498 | ||
1ab9e06d | 499 | gtk_menu_shell_insert( GTK_MENU_SHELL(menu), menu_item, index ); |
e01c8145 VZ |
500 | |
501 | if ( index ) | |
502 | { | |
503 | m_clientList.Insert( m_clientList.Item(index - 1), | |
504 | (wxObject*) NULL ); | |
505 | } | |
506 | else | |
507 | { | |
6c8a980f | 508 | m_clientList.Insert( (wxObject*) NULL ); |
e01c8145 VZ |
509 | } |
510 | } | |
511 | else | |
512 | { | |
243dbf1a VZ |
513 | // don't call wxChoice::GetCount() from here because it doesn't work |
514 | // if we're called from ctor (and GtkMenuShell is still NULL) | |
515 | ||
e01c8145 | 516 | // normal control, just append |
243dbf1a VZ |
517 | if (pos == (int)m_clientList.GetCount()) |
518 | { | |
1ab9e06d | 519 | gtk_menu_shell_append( GTK_MENU_SHELL(menu), menu_item ); |
0a164d4c WS |
520 | m_clientList.Append( (wxObject*) NULL ); |
521 | index = m_clientList.GetCount() - 1; | |
243dbf1a VZ |
522 | } |
523 | else | |
524 | { | |
1ab9e06d | 525 | gtk_menu_shell_insert( GTK_MENU_SHELL(menu), menu_item, pos ); |
243dbf1a VZ |
526 | m_clientList.Insert( pos, (wxObject*) NULL ); |
527 | index = pos; | |
528 | } | |
e01c8145 VZ |
529 | } |
530 | ||
531 | if (GTK_WIDGET_REALIZED(m_widget)) | |
532 | { | |
533 | gtk_widget_realize( menu_item ); | |
534 | gtk_widget_realize( GTK_BIN(menu_item)->child ); | |
535 | ||
f40fdaa3 | 536 | ApplyWidgetStyle(); |
e01c8145 VZ |
537 | } |
538 | ||
9ddf4854 RR |
539 | // The best size of a wxChoice should probably |
540 | // be changed everytime the control has been | |
541 | // changed, but at least after adding an item | |
542 | // it has to change. Adapted from Matt Ownby. | |
543 | InvalidateBestSize(); | |
0a164d4c | 544 | |
9fa72bd2 MR |
545 | g_signal_connect_after (menu_item, "activate", |
546 | G_CALLBACK (gtk_choice_clicked_callback), | |
547 | this); | |
e01c8145 VZ |
548 | |
549 | gtk_widget_show( menu_item ); | |
550 | ||
551 | // return the index of the item in the control | |
552 | return index; | |
553 | } | |
554 | ||
f68586e5 VZ |
555 | wxSize wxChoice::DoGetBestSize() const |
556 | { | |
db434467 | 557 | wxSize ret( wxControl::DoGetBestSize() ); |
29f54b9b VZ |
558 | |
559 | // we know better our horizontal extent: it depends on the longest string | |
560 | // we have | |
561 | ret.x = 0; | |
562 | if ( m_widget ) | |
563 | { | |
60d85ccb | 564 | int width; |
29f54b9b VZ |
565 | size_t count = GetCount(); |
566 | for ( size_t n = 0; n < count; n++ ) | |
567 | { | |
2b1ff57f | 568 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL ); |
29f54b9b VZ |
569 | if ( width > ret.x ) |
570 | ret.x = width; | |
571 | } | |
572 | ||
df336b7f VZ |
573 | // add extra for the choice "=" button |
574 | ||
575 | // VZ: I don't know how to get the right value, it seems to be in | |
576 | // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get | |
577 | // to it - maybe we can use gtk_option_menu_size_request() for this | |
578 | // somehow? | |
579 | // | |
580 | // This default value works only for the default GTK+ theme (i.e. | |
581 | // no theme at all) (FIXME) | |
582 | static const int widthChoiceIndicator = 35; | |
583 | ret.x += widthChoiceIndicator; | |
29f54b9b VZ |
584 | } |
585 | ||
586 | // but not less than the minimal width | |
587 | if ( ret.x < 80 ) | |
588 | ret.x = 80; | |
589 | ||
ebbb22bd RR |
590 | // If this request_size is called with no entries then |
591 | // the returned height is wrong. Give it a reasonable | |
592 | // default value. | |
593 | if (ret.y <= 18) | |
594 | ret.y = 8 + GetCharHeight(); | |
9e691f46 | 595 | |
9f884528 | 596 | CacheBestSize(ret); |
db434467 | 597 | return ret; |
f68586e5 VZ |
598 | } |
599 | ||
2b904684 RR |
600 | bool wxChoice::IsOwnGtkWindow( GdkWindow *window ) |
601 | { | |
2b904684 | 602 | return GTK_BUTTON(m_widget)->event_window; |
2b904684 RR |
603 | } |
604 | ||
9d522606 RD |
605 | // static |
606 | wxVisualAttributes | |
607 | wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
608 | { | |
609 | return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new); | |
610 | } | |
611 | ||
2b904684 | 612 | |
df336b7f | 613 | #endif // wxUSE_CHOICE |