Did much work on colors. It doesn't work and I guess
[wxWidgets.git] / src / gtk1 / 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 //-----------------------------------------------------------------------------
18 // data
19 //-----------------------------------------------------------------------------
20
21 extern bool g_blockEventsOnDrag;
22
23 //-----------------------------------------------------------------------------
24 // "activate"
25 //-----------------------------------------------------------------------------
26
27 static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
28 {
29 if (!choice->HasVMT()) return;
30 if (g_blockEventsOnDrag) return;
31
32 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
33 event.SetInt( choice->GetSelection() );
34 wxString tmp( choice->GetStringSelection() );
35 event.SetString( WXSTRINGCAST(tmp) );
36 event.SetEventObject(choice);
37 choice->GetEventHandler()->ProcessEvent(event);
38 }
39
40 //-----------------------------------------------------------------------------
41 // wxChoice
42 //-----------------------------------------------------------------------------
43
44 IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
45
46 wxChoice::wxChoice(void)
47 {
48 }
49
50 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
51 const wxPoint &pos, const wxSize &size,
52 int n, const wxString choices[],
53 long style, const wxValidator& validator, const wxString &name )
54 {
55 m_needParent = TRUE;
56
57 PreCreation( parent, id, pos, size, style, name );
58
59 SetValidator( validator );
60
61 m_widget = gtk_option_menu_new();
62
63 wxSize newSize = size;
64 if (newSize.x == -1) newSize.x = 80;
65 if (newSize.y == -1) newSize.y = 26;
66 SetSize( newSize.x, newSize.y );
67
68 GtkWidget *menu = gtk_menu_new();
69
70 for (int i = 0; i < n; i++)
71 {
72 GtkWidget *item = gtk_menu_item_new_with_label( choices[i] );
73 gtk_signal_connect( GTK_OBJECT( item ), "activate",
74 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
75
76 gtk_menu_append( GTK_MENU(menu), item );
77
78 gtk_widget_show( item );
79 gtk_widget_realize( item );
80 gtk_widget_realize( GTK_BIN(item)->child );
81 }
82 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
83
84 PostCreation();
85
86 SetBackgroundColour( parent->GetBackgroundColour() );
87
88 Show( TRUE );
89
90 return TRUE;
91 }
92
93 void wxChoice::Append( const wxString &item )
94 {
95 wxCHECK_RET( m_widget != NULL, "invalid choice" );
96
97 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
98 GtkWidget *menu_item = gtk_menu_item_new_with_label( item );
99
100 gtk_menu_append( GTK_MENU(menu), menu_item );
101
102 gtk_widget_realize( menu_item );
103 gtk_widget_realize( GTK_BIN(menu_item)->child );
104
105 if (m_hasOwnStyle)
106 {
107
108 GtkBin *bin = GTK_BIN( menu_item );
109
110 gtk_widget_set_style( bin->child,
111 gtk_style_ref(
112 gtk_widget_get_style( m_widget ) ) );
113
114 gtk_widget_set_style( GTK_WIDGET( bin ),
115 gtk_style_ref(
116 gtk_widget_get_style( m_widget ) ) );
117 }
118
119 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
120 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
121
122 gtk_widget_show( menu_item );
123 }
124
125 void wxChoice::Clear(void)
126 {
127 wxCHECK_RET( m_widget != NULL, "invalid choice" );
128
129 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
130 GtkWidget *menu = gtk_menu_new();
131 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
132 }
133
134 void wxChoice::Delete( int WXUNUSED(n) )
135 {
136 wxFAIL_MSG( "wxChoice:Delete not implemented" );
137 }
138
139 int wxChoice::FindString( const wxString &string ) const
140 {
141 wxCHECK_MSG( m_widget != NULL, -1, "invalid choice" );
142
143 // If you read this code once and you think you understand
144 // it, then you are very wrong. Robert Roebling.
145
146 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
147 int count = 0;
148 GList *child = menu_shell->children;
149 while (child)
150 {
151 GtkBin *bin = GTK_BIN( child->data );
152 GtkLabel *label = (GtkLabel *) NULL;
153 if (bin->child) label = GTK_LABEL(bin->child);
154 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
155
156 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
157
158 if (string == label->label) return count;
159 child = child->next;
160 count++;
161 }
162
163 wxFAIL_MSG( "wxChoice: string not found" );
164
165 return -1;
166 }
167
168 int wxChoice::GetColumns(void) const
169 {
170 return 1;
171 }
172
173 int wxChoice::GetSelection(void)
174 {
175 wxCHECK_MSG( m_widget != NULL, -1, "invalid choice" );
176
177 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
178 int count = 0;
179 GList *child = menu_shell->children;
180 while (child)
181 {
182 GtkBin *bin = GTK_BIN( child->data );
183 if (!bin->child) return count;
184 child = child->next;
185 count++;
186 }
187
188 wxFAIL_MSG( "wxChoice: no selection" );
189
190 return -1;
191 }
192
193 wxString wxChoice::GetString( int n ) const
194 {
195 wxCHECK_MSG( m_widget != NULL, "", "invalid choice" );
196
197 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
198 int count = 0;
199 GList *child = menu_shell->children;
200 while (child)
201 {
202 GtkBin *bin = GTK_BIN( child->data );
203 if (count == n)
204 {
205 GtkLabel *label = (GtkLabel *) NULL;
206 if (bin->child) label = GTK_LABEL(bin->child);
207 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
208
209 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
210
211 return label->label;
212 }
213 child = child->next;
214 count++;
215 }
216
217 wxFAIL_MSG( "wxChoice: string not found" );
218
219 return "";
220 }
221
222 wxString wxChoice::GetStringSelection(void) const
223 {
224 wxCHECK_MSG( m_widget != NULL, "", "invalid choice" );
225
226 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
227
228 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
229
230 return label->label;
231 }
232
233 int wxChoice::Number(void) const
234 {
235 wxCHECK_MSG( m_widget != NULL, 0, "invalid choice" );
236
237 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
238 int count = 0;
239 GList *child = menu_shell->children;
240 while (child)
241 {
242 count++;
243 child = child->next;
244 }
245 return count;
246 }
247
248 void wxChoice::SetColumns( int WXUNUSED(n) )
249 {
250 }
251
252 void wxChoice::SetSelection( int n )
253 {
254 wxCHECK_RET( m_widget != NULL, "invalid choice" );
255
256 int tmp = n;
257 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
258
259 gtk_choice_clicked_callback( (GtkWidget *) NULL, this );
260 }
261
262 void wxChoice::SetStringSelection( const wxString &string )
263 {
264 wxCHECK_RET( m_widget != NULL, "invalid choice" );
265
266 int n = FindString( string );
267 if (n != -1) SetSelection( n );
268 }
269
270 void wxChoice::SetFont( const wxFont &font )
271 {
272 wxCHECK_RET( m_widget != NULL, "invalid choice" );
273
274 wxControl::SetFont( font );
275
276 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
277 GList *child = menu_shell->children;
278 while (child)
279 {
280 GtkBin *bin = GTK_BIN( child->data );
281 GtkWidget *label = (GtkWidget *) NULL;
282 if (bin->child) label = bin->child;
283 if (!label) label = GTK_BUTTON(m_widget)->child;
284
285 gtk_widget_set_style( label,
286 gtk_style_ref(
287 gtk_widget_get_style( m_widget ) ) );
288
289 child = child->next;
290 }
291 }
292
293 void wxChoice::SetBackgroundColour( const wxColour &colour )
294 {
295 return;
296
297 wxCHECK_RET( m_widget != NULL, "invalid choice" );
298
299 wxControl::SetBackgroundColour( colour );
300
301 if (!m_backgroundColour.Ok()) return;
302
303 GtkStyle *style = gtk_widget_get_style( m_widget );
304
305 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
306
307 gtk_widget_set_style( GTK_WIDGET( menu_shell ), gtk_style_ref( style ) );
308
309 GList *child = menu_shell->children;
310 while (child)
311 {
312 gtk_widget_set_style( GTK_WIDGET( child->data ), gtk_style_ref( style ) );
313 child = child->next;
314 }
315 }
316
317
318