General tidy-up (mainly typecasts) to allow the use of the SGI native
[wxWidgets.git] / src / gtk1 / choice.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: choice.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Id:
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 #ifdef __GNUG__
13 #pragma implementation "choice.h"
14 #endif
15
16 #include "wx/choice.h"
17
18 //-----------------------------------------------------------------------------
19 // data
20 //-----------------------------------------------------------------------------
21
22 extern bool g_blockEventsOnDrag;
23
24 //-----------------------------------------------------------------------------
25 // wxChoice
26 //-----------------------------------------------------------------------------
27
28 static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
29 {
30 if (!choice->HasVMT()) return;
31 if (g_blockEventsOnDrag) return;
32
33 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
34 event.SetInt( choice->GetSelection() );
35 wxString tmp( choice->GetStringSelection() );
36 event.SetString( WXSTRINGCAST(tmp) );
37 event.SetEventObject(choice);
38 choice->GetEventHandler()->ProcessEvent(event);
39 }
40
41 //-----------------------------------------------------------------------------
42
43 IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
44
45 wxChoice::wxChoice(void)
46 {
47 }
48
49 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
50 const wxPoint &pos, const wxSize &size,
51 int n, const wxString choices[],
52 long style, const wxValidator& validator, const wxString &name )
53 {
54 m_needParent = TRUE;
55
56 PreCreation( parent, id, pos, size, style, name );
57
58 SetValidator( validator );
59
60 m_widget = gtk_option_menu_new();
61
62 wxSize newSize = size;
63 if (newSize.x == -1) newSize.x = 80;
64 if (newSize.y == -1) newSize.y = 26;
65 SetSize( newSize.x, newSize.y );
66
67 GtkWidget *menu;
68 menu = gtk_menu_new();
69
70 for (int i = 0; i < n; i++)
71 {
72 GtkWidget *item;
73 item = gtk_menu_item_new_with_label( choices[i] );
74 gtk_signal_connect( GTK_OBJECT( item ), "activate",
75 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
76 gtk_menu_append( GTK_MENU(menu), item );
77 gtk_widget_show( item );
78 }
79 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
80
81 PostCreation();
82
83 Show( TRUE );
84
85 return TRUE;
86 }
87
88 void wxChoice::Append( const wxString &item )
89 {
90 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
91 GtkWidget *menu_item;
92 menu_item = gtk_menu_item_new_with_label( item );
93 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
94 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
95 gtk_menu_append( GTK_MENU(menu), menu_item );
96 gtk_widget_show( menu_item );
97 }
98
99 void wxChoice::Clear(void)
100 {
101 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
102 GtkWidget *menu = gtk_menu_new();
103 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
104 }
105
106 int wxChoice::FindString( const wxString &string ) const
107 {
108 // If you read this code once and you think you understand
109 // it, then you are very wrong. Robert Roebling.
110
111 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
112 int count = 0;
113 GList *child = menu_shell->children;
114 while (child)
115 {
116 GtkBin *bin = GTK_BIN( child->data );
117 GtkLabel *label = (GtkLabel *) NULL;
118 if (bin->child) label = GTK_LABEL(bin->child);
119
120 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
121
122 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
123 if (string == label->label) return count;
124 child = child->next;
125 count++;
126 }
127
128 wxFAIL_MSG( "wxChoice: string not found" );
129
130 return -1;
131 }
132
133 int wxChoice::GetColumns(void) const
134 {
135 return 1;
136 }
137
138 int wxChoice::GetSelection(void)
139 {
140 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
141 int count = 0;
142 GList *child = menu_shell->children;
143 while (child)
144 {
145 GtkBin *bin = GTK_BIN( child->data );
146 if (!bin->child) return count;
147 child = child->next;
148 count++;
149 }
150
151 wxFAIL_MSG( "wxChoice: no selection" );
152
153 return -1;
154 }
155
156 wxString wxChoice::GetString( int n ) const
157 {
158 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
159 int count = 0;
160 GList *child = menu_shell->children;
161 while (child)
162 {
163 GtkBin *bin = GTK_BIN( child->data );
164 if (count == n)
165 {
166 GtkLabel *label = (GtkLabel *) NULL;
167 if (bin->child) label = GTK_LABEL(bin->child);
168
169 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
170
171 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
172 return label->label;
173 }
174 child = child->next;
175 count++;
176 }
177
178 wxFAIL_MSG( "wxChoice: string not found" );
179
180 return "";
181 }
182
183 wxString wxChoice::GetStringSelection(void) const
184 {
185 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
186
187 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
188
189 return label->label;
190 }
191
192 int wxChoice::Number(void) const
193 {
194 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
195 int count = 0;
196 GList *child = menu_shell->children;
197 while (child)
198 {
199 count++;
200 child = child->next;
201 }
202 return count;
203 }
204
205 void wxChoice::SetColumns( int WXUNUSED(n) )
206 {
207 }
208
209 void wxChoice::SetSelection( int n )
210 {
211 int tmp = n;
212 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
213
214 gtk_choice_clicked_callback( (GtkWidget *) NULL, this );
215 }
216
217 void wxChoice::SetStringSelection( const wxString &string )
218 {
219 int n = FindString( string );
220 if (n != -1) SetSelection( n );
221 }
222