]>
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 | |
9e691f46 | 17 | #include "wx/gtk/private.h" |
83624f79 | 18 | |
acfd422a RR |
19 | //----------------------------------------------------------------------------- |
20 | // idle system | |
21 | //----------------------------------------------------------------------------- | |
22 | ||
23 | extern void wxapp_install_idle_handler(); | |
24 | extern bool g_isIdle; | |
25 | ||
66bd6b93 RR |
26 | //----------------------------------------------------------------------------- |
27 | // data | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | extern bool g_blockEventsOnDrag; | |
31 | ||
c801d85f | 32 | //----------------------------------------------------------------------------- |
e1e955e1 | 33 | // "activate" |
c801d85f KB |
34 | //----------------------------------------------------------------------------- |
35 | ||
865bb325 | 36 | extern "C" { |
66bd6b93 | 37 | static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice ) |
c801d85f | 38 | { |
e01c8145 | 39 | if (g_isIdle) |
4dcaf11a | 40 | wxapp_install_idle_handler(); |
acfd422a | 41 | |
a2053b27 | 42 | if (!choice->m_hasVMT) return; |
29006414 | 43 | |
acfd422a | 44 | if (g_blockEventsOnDrag) return; |
29006414 | 45 | |
5f3565a2 RR |
46 | int selection = wxNOT_FOUND; |
47 | ||
5f3565a2 | 48 | selection = gtk_option_menu_get_history( GTK_OPTION_MENU(choice->GetHandle()) ); |
5f3565a2 | 49 | |
5f3565a2 RR |
50 | choice->m_selection_hack = selection; |
51 | ||
acfd422a | 52 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() ); |
6c8a980f VZ |
53 | int n = choice->GetSelection(); |
54 | ||
55 | event.SetInt( n ); | |
acfd422a RR |
56 | event.SetString( choice->GetStringSelection() ); |
57 | event.SetEventObject(choice); | |
6c8a980f VZ |
58 | |
59 | if ( choice->HasClientObjectData() ) | |
60 | event.SetClientObject( choice->GetClientObject(n) ); | |
61 | else if ( choice->HasClientUntypedData() ) | |
62 | event.SetClientData( choice->GetClientData(n) ); | |
63 | ||
acfd422a | 64 | choice->GetEventHandler()->ProcessEvent(event); |
6de97a3b | 65 | } |
865bb325 | 66 | } |
c801d85f | 67 | |
e1e955e1 RR |
68 | //----------------------------------------------------------------------------- |
69 | // wxChoice | |
c801d85f KB |
70 | //----------------------------------------------------------------------------- |
71 | ||
7f4dc78d | 72 | IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl) |
c801d85f | 73 | |
fd0eed64 | 74 | wxChoice::wxChoice() |
c801d85f | 75 | { |
e01c8145 | 76 | m_strings = (wxSortedArrayString *)NULL; |
6de97a3b | 77 | } |
c801d85f | 78 | |
584ad2a3 MB |
79 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, |
80 | const wxPoint &pos, const wxSize &size, | |
81 | const wxArrayString& choices, | |
82 | long style, const wxValidator& validator, | |
83 | const wxString &name ) | |
84 | { | |
85 | wxCArrayString chs(choices); | |
86 | ||
87 | return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
88 | style, validator, name ); | |
89 | } | |
90 | ||
debe6624 | 91 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, |
fd0eed64 RR |
92 | const wxPoint &pos, const wxSize &size, |
93 | int n, const wxString choices[], | |
94 | long style, const wxValidator& validator, const wxString &name ) | |
c801d85f | 95 | { |
0a164d4c | 96 | m_needParent = true; |
034be888 | 97 | #if (GTK_MINOR_VERSION > 0) |
0a164d4c | 98 | m_acceptsFocus = true; |
034be888 | 99 | #endif |
29006414 | 100 | |
4dcaf11a RR |
101 | if (!PreCreation( parent, pos, size ) || |
102 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
103 | { | |
223d09f6 | 104 | wxFAIL_MSG( wxT("wxChoice creation failed") ); |
0a164d4c | 105 | return false; |
4dcaf11a | 106 | } |
6de97a3b | 107 | |
fd0eed64 | 108 | m_widget = gtk_option_menu_new(); |
29006414 | 109 | |
e01c8145 VZ |
110 | if ( style & wxCB_SORT ) |
111 | { | |
112 | // if our m_strings != NULL, DoAppend() will check for it and insert | |
113 | // items in the correct order | |
114 | m_strings = new wxSortedArrayString; | |
115 | } | |
116 | ||
16edee16 RR |
117 | // begin with no selection |
118 | m_selection_hack = wxNOT_FOUND; | |
119 | ||
fd0eed64 | 120 | GtkWidget *menu = gtk_menu_new(); |
29006414 | 121 | |
fd0eed64 RR |
122 | for (int i = 0; i < n; i++) |
123 | { | |
243dbf1a | 124 | GtkAddHelper(menu, i, choices[i]); |
fd0eed64 | 125 | } |
e01c8145 | 126 | |
fd0eed64 | 127 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); |
29006414 | 128 | |
f03fc89f | 129 | m_parent->DoAddChild( this ); |
29006414 | 130 | |
abdeb9e7 RD |
131 | PostCreation(size); |
132 | SetBestSize(size); // need this too because this is a wxControlWithItems | |
29006414 | 133 | |
0a164d4c | 134 | return true; |
6de97a3b | 135 | } |
29006414 | 136 | |
fd0eed64 RR |
137 | wxChoice::~wxChoice() |
138 | { | |
f5e27805 | 139 | Clear(); |
e01c8145 VZ |
140 | |
141 | delete m_strings; | |
fd0eed64 RR |
142 | } |
143 | ||
9abe166a | 144 | int wxChoice::DoAppend( const wxString &item ) |
fd0eed64 | 145 | { |
2ee3ee1b | 146 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") ); |
29006414 | 147 | |
fd0eed64 | 148 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); |
29006414 | 149 | |
243dbf1a VZ |
150 | return GtkAddHelper(menu, GetCount(), item); |
151 | } | |
152 | ||
153 | int wxChoice::DoInsert( const wxString &item, int pos ) | |
154 | { | |
155 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") ); | |
156 | wxCHECK_MSG( (pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); | |
157 | ||
158 | if (pos == GetCount()) | |
159 | return DoAppend(item); | |
160 | ||
161 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); | |
162 | ||
16edee16 RR |
163 | // if the item to insert is at or before the selection, and the selection is valid |
164 | if ((pos <= m_selection_hack) && (m_selection_hack != wxNOT_FOUND)) | |
165 | { | |
166 | // move the selection forward one | |
167 | m_selection_hack++; | |
168 | } | |
169 | ||
243dbf1a | 170 | return GtkAddHelper(menu, pos, item); |
fd0eed64 | 171 | } |
f96aa4d9 | 172 | |
6c8a980f | 173 | void wxChoice::DoSetItemClientData( int n, void* clientData ) |
fd0eed64 | 174 | { |
2ee3ee1b | 175 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); |
29006414 | 176 | |
222ed1d6 | 177 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
6c8a980f | 178 | wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") ); |
29006414 | 179 | |
f5e27805 | 180 | node->SetData( (wxObject*) clientData ); |
fd0eed64 RR |
181 | } |
182 | ||
6c8a980f | 183 | void* wxChoice::DoGetItemClientData( int n ) const |
fd0eed64 | 184 | { |
2ee3ee1b | 185 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") ); |
29006414 | 186 | |
222ed1d6 | 187 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
6c8a980f | 188 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") ); |
29006414 | 189 | |
b1d4dd7a | 190 | return node->GetData(); |
6de97a3b | 191 | } |
fd0eed64 | 192 | |
6c8a980f | 193 | void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData ) |
fd0eed64 | 194 | { |
2ee3ee1b | 195 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") ); |
29006414 | 196 | |
222ed1d6 | 197 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
6c8a980f | 198 | wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") ); |
29006414 | 199 | |
edc973b1 | 200 | // wxItemContainer already deletes data for us |
29006414 | 201 | |
fd0eed64 RR |
202 | node->SetData( (wxObject*) clientData ); |
203 | } | |
204 | ||
6c8a980f | 205 | wxClientData* wxChoice::DoGetItemClientObject( int n ) const |
fd0eed64 | 206 | { |
2ee3ee1b | 207 | wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") ); |
29006414 | 208 | |
222ed1d6 | 209 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
9abe166a | 210 | wxCHECK_MSG( node, (wxClientData *)NULL, |
6c8a980f | 211 | wxT("invalid index in wxChoice::DoGetItemClientObject") ); |
29006414 | 212 | |
b1d4dd7a | 213 | return (wxClientData*) node->GetData(); |
fd0eed64 | 214 | } |
29006414 | 215 | |
fd0eed64 | 216 | void wxChoice::Clear() |
c801d85f | 217 | { |
223d09f6 | 218 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
f96aa4d9 | 219 | |
fd0eed64 RR |
220 | gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) ); |
221 | GtkWidget *menu = gtk_menu_new(); | |
222 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
29006414 | 223 | |
6c8a980f VZ |
224 | if ( HasClientObjectData() ) |
225 | { | |
226 | // destroy the data (due to Robert's idea of using wxList<wxObject> | |
227 | // and not wxList<wxClientData> we can't just say | |
0a164d4c | 228 | // m_clientList.DeleteContents(true) - this would crash! |
222ed1d6 | 229 | wxList::compatibility_iterator node = m_clientList.GetFirst(); |
6c8a980f VZ |
230 | while ( node ) |
231 | { | |
b1d4dd7a RL |
232 | delete (wxClientData *)node->GetData(); |
233 | node = node->GetNext(); | |
6c8a980f VZ |
234 | } |
235 | } | |
d6538e2c | 236 | m_clientList.Clear(); |
2ee3ee1b VZ |
237 | |
238 | if ( m_strings ) | |
239 | m_strings->Clear(); | |
16edee16 RR |
240 | |
241 | // begin with no selection | |
242 | m_selection_hack = wxNOT_FOUND; | |
6de97a3b | 243 | } |
c801d85f | 244 | |
645420d8 | 245 | void wxChoice::Delete( int n ) |
2f6407b9 | 246 | { |
645420d8 VZ |
247 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
248 | ||
249 | // VZ: apparently GTK+ doesn't have a built-in function to do it (not even | |
e2380ce1 | 250 | // in 2.0), hence this dumb implementation -- still better than nothing |
645420d8 VZ |
251 | int i, |
252 | count = GetCount(); | |
253 | ||
254 | wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") ); | |
255 | ||
16edee16 RR |
256 | // if the item to delete is before the selection, and the selection is valid |
257 | if ((n < m_selection_hack) && (m_selection_hack != wxNOT_FOUND)) | |
258 | { | |
259 | // move the selection back one | |
260 | m_selection_hack--; | |
261 | } | |
262 | else if (n == m_selection_hack) | |
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 | { | |
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) | |
337 | label = GTK_LABEL( BUTTON_CHILD(m_widget) ); | |
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 | |
a912c184 | 360 | void wxChoice::SetString( int n, const wxString& str ) |
6c8a980f VZ |
361 | { |
362 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); | |
363 | ||
34b5e560 RR |
364 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
365 | int count = 0; | |
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) | |
376 | label = GTK_LABEL( BUTTON_CHILD(m_widget) ); | |
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 | ||
debe6624 | 389 | wxString wxChoice::GetString( 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) ) ); | |
394 | int count = 0; | |
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) | |
405 | label = GTK_LABEL( BUTTON_CHILD(m_widget) ); | |
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 | |
9abe166a | 420 | 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) ) ); | |
425 | int count = 0; | |
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 | |
443 | if ((n >= 0) && (n < GetCount())) | |
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) | |
478 | label = BUTTON_CHILD(m_widget); | |
29006414 | 479 | |
f40fdaa3 | 480 | gtk_widget_modify_style( label, style ); |
29006414 | 481 | |
fd0eed64 RR |
482 | child = child->next; |
483 | } | |
f96aa4d9 RR |
484 | } |
485 | ||
243dbf1a | 486 | int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item) |
e01c8145 | 487 | { |
243dbf1a VZ |
488 | wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index")); |
489 | ||
fab591c5 | 490 | GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) ); |
e01c8145 VZ |
491 | |
492 | size_t index; | |
493 | if ( m_strings ) | |
494 | { | |
495 | // sorted control, need to insert at the correct index | |
496 | index = m_strings->Add(item); | |
497 | ||
498 | gtk_menu_insert( GTK_MENU(menu), menu_item, index ); | |
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 |
243dbf1a VZ |
516 | if (pos == (int)m_clientList.GetCount()) |
517 | { | |
0a164d4c WS |
518 | gtk_menu_append( GTK_MENU(menu), menu_item ); |
519 | m_clientList.Append( (wxObject*) NULL ); | |
520 | index = m_clientList.GetCount() - 1; | |
243dbf1a VZ |
521 | } |
522 | else | |
523 | { | |
524 | gtk_menu_insert( GTK_MENU(menu), menu_item, pos ); | |
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; |
29f54b9b VZ |
564 | size_t count = GetCount(); |
565 | for ( size_t n = 0; n < count; n++ ) | |
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 | ||
2b904684 | 611 | |
df336b7f | 612 | #endif // wxUSE_CHOICE |