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