]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/choice.cpp
wxControl and wxDialog coded and supporting module def file.
[wxWidgets.git] / src / gtk / choice.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: choice.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "choice.h"
13 #endif
14
15 #include "wx/choice.h"
16
17 #if wxUSE_CHOICE
18
19 #include <gdk/gdk.h>
20 #include <gtk/gtk.h>
21
22 //-----------------------------------------------------------------------------
23 // idle system
24 //-----------------------------------------------------------------------------
25
26 extern void wxapp_install_idle_handler();
27 extern bool g_isIdle;
28
29 //-----------------------------------------------------------------------------
30 // data
31 //-----------------------------------------------------------------------------
32
33 extern bool g_blockEventsOnDrag;
34
35 //-----------------------------------------------------------------------------
36 // "activate"
37 //-----------------------------------------------------------------------------
38
39 static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
40 {
41 if (g_isIdle)
42 wxapp_install_idle_handler();
43
44 if (!choice->m_hasVMT) return;
45
46 if (g_blockEventsOnDrag) return;
47
48 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
49 int n = choice->GetSelection();
50
51 event.SetInt( n );
52 event.SetString( choice->GetStringSelection() );
53 event.SetEventObject(choice);
54
55 if ( choice->HasClientObjectData() )
56 event.SetClientObject( choice->GetClientObject(n) );
57 else if ( choice->HasClientUntypedData() )
58 event.SetClientData( choice->GetClientData(n) );
59
60 choice->GetEventHandler()->ProcessEvent(event);
61 }
62
63 //-----------------------------------------------------------------------------
64 // wxChoice
65 //-----------------------------------------------------------------------------
66
67 IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
68
69 wxChoice::wxChoice()
70 {
71 m_strings = (wxSortedArrayString *)NULL;
72 }
73
74 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
75 const wxPoint &pos, const wxSize &size,
76 int n, const wxString choices[],
77 long style, const wxValidator& validator, const wxString &name )
78 {
79 m_needParent = TRUE;
80 #if (GTK_MINOR_VERSION > 0)
81 m_acceptsFocus = TRUE;
82 #endif
83
84 if (!PreCreation( parent, pos, size ) ||
85 !CreateBase( parent, id, pos, size, style, validator, name ))
86 {
87 wxFAIL_MSG( wxT("wxChoice creation failed") );
88 return FALSE;
89 }
90
91 m_widget = gtk_option_menu_new();
92
93 if ( style & wxCB_SORT )
94 {
95 // if our m_strings != NULL, DoAppend() will check for it and insert
96 // items in the correct order
97 m_strings = new wxSortedArrayString;
98 }
99
100 GtkWidget *menu = gtk_menu_new();
101
102 for (int i = 0; i < n; i++)
103 {
104 GtkAppendHelper(menu, choices[i]);
105 }
106
107 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
108
109 m_parent->DoAddChild( this );
110
111 PostCreation();
112
113 SetFont( parent->GetFont() );
114
115 wxSize size_best( DoGetBestSize() );
116 wxSize new_size( size );
117 if (new_size.x == -1)
118 new_size.x = size_best.x;
119 if (new_size.y == -1)
120 new_size.y = size_best.y;
121 if ((new_size.x != size.x) || (new_size.y != size.y))
122 SetSize( new_size.x, new_size.y );
123
124 SetBackgroundColour( parent->GetBackgroundColour() );
125 SetForegroundColour( parent->GetForegroundColour() );
126
127 Show( TRUE );
128
129 return TRUE;
130 }
131
132 wxChoice::~wxChoice()
133 {
134 Clear();
135
136 delete m_strings;
137 }
138
139 int wxChoice::DoAppend( const wxString &item )
140 {
141 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
142
143 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
144
145 return GtkAppendHelper(menu, item);
146 }
147
148 void wxChoice::DoSetItemClientData( int n, void* clientData )
149 {
150 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
151
152 wxNode *node = m_clientList.Nth( n );
153 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
154
155 node->SetData( (wxObject*) clientData );
156 }
157
158 void* wxChoice::DoGetItemClientData( int n ) const
159 {
160 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
161
162 wxNode *node = m_clientList.Nth( n );
163 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
164
165 return node->Data();
166 }
167
168 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
169 {
170 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
171
172 wxNode *node = m_clientList.Nth( n );
173 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
174
175 wxClientData *cd = (wxClientData*) node->Data();
176 delete cd;
177
178 node->SetData( (wxObject*) clientData );
179 }
180
181 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
182 {
183 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
184
185 wxNode *node = m_clientList.Nth( n );
186 wxCHECK_MSG( node, (wxClientData *)NULL,
187 wxT("invalid index in wxChoice::DoGetItemClientObject") );
188
189 return (wxClientData*) node->Data();
190 }
191
192 void wxChoice::Clear()
193 {
194 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
195
196 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
197 GtkWidget *menu = gtk_menu_new();
198 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
199
200 if ( HasClientObjectData() )
201 {
202 // destroy the data (due to Robert's idea of using wxList<wxObject>
203 // and not wxList<wxClientData> we can't just say
204 // m_clientList.DeleteContents(TRUE) - this would crash!
205 wxNode *node = m_clientList.First();
206 while ( node )
207 {
208 delete (wxClientData *)node->Data();
209 node = node->Next();
210 }
211 }
212 m_clientList.Clear();
213
214 if ( m_strings )
215 m_strings->Clear();
216 }
217
218 void wxChoice::Delete( int WXUNUSED(n) )
219 {
220 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
221 }
222
223 int wxChoice::FindString( const wxString &string ) const
224 {
225 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
226
227 // If you read this code once and you think you understand
228 // it, then you are very wrong. Robert Roebling.
229
230 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
231 int count = 0;
232 GList *child = menu_shell->children;
233 while (child)
234 {
235 GtkBin *bin = GTK_BIN( child->data );
236 GtkLabel *label = (GtkLabel *) NULL;
237 if (bin->child) label = GTK_LABEL(bin->child);
238 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
239
240 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
241
242 if (string == wxString(label->label,*wxConvCurrent))
243 return count;
244
245 child = child->next;
246 count++;
247 }
248
249 return -1;
250 }
251
252 int wxChoice::GetSelection() const
253 {
254 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
255
256 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
257 int count = 0;
258 GList *child = menu_shell->children;
259 while (child)
260 {
261 GtkBin *bin = GTK_BIN( child->data );
262 if (!bin->child) return count;
263 child = child->next;
264 count++;
265 }
266
267 return -1;
268 }
269
270 void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) )
271 {
272 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
273
274 wxFAIL_MSG(wxT("not implemented"));
275 }
276
277 wxString wxChoice::GetString( int n ) const
278 {
279 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
280
281 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
282 int count = 0;
283 GList *child = menu_shell->children;
284 while (child)
285 {
286 GtkBin *bin = GTK_BIN( child->data );
287 if (count == n)
288 {
289 GtkLabel *label = (GtkLabel *) NULL;
290 if (bin->child) label = GTK_LABEL(bin->child);
291 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
292
293 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
294
295 return wxString(label->label,*wxConvCurrent);
296 }
297 child = child->next;
298 count++;
299 }
300
301 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
302
303 return wxT("");
304 }
305
306 int wxChoice::GetCount() const
307 {
308 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
309
310 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
311 int count = 0;
312 GList *child = menu_shell->children;
313 while (child)
314 {
315 count++;
316 child = child->next;
317 }
318 return count;
319 }
320
321 void wxChoice::SetSelection( int n )
322 {
323 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
324
325 int tmp = n;
326 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
327 }
328
329 void wxChoice::ApplyWidgetStyle()
330 {
331 SetWidgetStyle();
332
333 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
334
335 gtk_widget_set_style( m_widget, m_widgetStyle );
336 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
337
338 GList *child = menu_shell->children;
339 while (child)
340 {
341 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
342
343 GtkBin *bin = GTK_BIN( child->data );
344 GtkWidget *label = (GtkWidget *) NULL;
345 if (bin->child) label = bin->child;
346 if (!label) label = GTK_BUTTON(m_widget)->child;
347
348 gtk_widget_set_style( label, m_widgetStyle );
349
350 child = child->next;
351 }
352 }
353
354 size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item)
355 {
356 GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() );
357
358 size_t index;
359 if ( m_strings )
360 {
361 // sorted control, need to insert at the correct index
362 index = m_strings->Add(item);
363
364 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
365
366 if ( index )
367 {
368 m_clientList.Insert( m_clientList.Item(index - 1),
369 (wxObject*) NULL );
370 }
371 else
372 {
373 m_clientList.Insert( (wxObject*) NULL );
374 }
375 }
376 else
377 {
378 // normal control, just append
379 gtk_menu_append( GTK_MENU(menu), menu_item );
380
381 m_clientList.Append( (wxObject*) NULL );
382
383 // don't call wxChoice::GetCount() from here because it doesn't work
384 // if we're called from ctor (and GtkMenuShell is still NULL)
385 index = m_clientList.GetCount() - 1;
386 }
387
388 if (GTK_WIDGET_REALIZED(m_widget))
389 {
390 gtk_widget_realize( menu_item );
391 gtk_widget_realize( GTK_BIN(menu_item)->child );
392
393 if (m_widgetStyle) ApplyWidgetStyle();
394 }
395
396 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
397 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
398
399 gtk_widget_show( menu_item );
400
401 // return the index of the item in the control
402 return index;
403 }
404
405 wxSize wxChoice::DoGetBestSize() const
406 {
407 wxSize ret( wxControl::DoGetBestSize() );
408 if (ret.x < 80) ret.x = 80;
409 ret.y = 16 + gdk_char_height( m_widget->style->font, 'H' );
410 return ret;
411 }
412
413 #endif