]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: choice.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
dbf858b5 | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
c801d85f KB |
7 | // Licence: wxWindows licence |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "choice.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/choice.h" | |
16 | ||
83624f79 RR |
17 | #include "gdk/gdk.h" |
18 | #include "gtk/gtk.h" | |
19 | ||
66bd6b93 RR |
20 | //----------------------------------------------------------------------------- |
21 | // data | |
22 | //----------------------------------------------------------------------------- | |
23 | ||
24 | extern bool g_blockEventsOnDrag; | |
25 | ||
c801d85f | 26 | //----------------------------------------------------------------------------- |
e1e955e1 | 27 | // "activate" |
c801d85f KB |
28 | //----------------------------------------------------------------------------- |
29 | ||
66bd6b93 | 30 | static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice ) |
c801d85f | 31 | { |
66bd6b93 RR |
32 | if (!choice->HasVMT()) return; |
33 | if (g_blockEventsOnDrag) return; | |
34 | ||
47908e25 | 35 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() ); |
c801d85f KB |
36 | event.SetInt( choice->GetSelection() ); |
37 | wxString tmp( choice->GetStringSelection() ); | |
38 | event.SetString( WXSTRINGCAST(tmp) ); | |
39 | event.SetEventObject(choice); | |
47908e25 | 40 | choice->GetEventHandler()->ProcessEvent(event); |
6de97a3b | 41 | } |
c801d85f | 42 | |
e1e955e1 RR |
43 | //----------------------------------------------------------------------------- |
44 | // wxChoice | |
c801d85f KB |
45 | //----------------------------------------------------------------------------- |
46 | ||
7f4dc78d | 47 | IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl) |
c801d85f | 48 | |
fd0eed64 | 49 | wxChoice::wxChoice() |
c801d85f | 50 | { |
6de97a3b | 51 | } |
c801d85f | 52 | |
debe6624 | 53 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, |
fd0eed64 RR |
54 | const wxPoint &pos, const wxSize &size, |
55 | int n, const wxString choices[], | |
56 | long style, const wxValidator& validator, const wxString &name ) | |
c801d85f | 57 | { |
fd0eed64 | 58 | m_needParent = TRUE; |
c801d85f | 59 | |
fd0eed64 | 60 | PreCreation( parent, id, pos, size, style, name ); |
f5e27805 | 61 | |
fd0eed64 | 62 | SetValidator( validator ); |
6de97a3b | 63 | |
fd0eed64 | 64 | m_widget = gtk_option_menu_new(); |
c801d85f | 65 | |
fd0eed64 RR |
66 | wxSize newSize = size; |
67 | if (newSize.x == -1) newSize.x = 80; | |
68 | if (newSize.y == -1) newSize.y = 26; | |
69 | SetSize( newSize.x, newSize.y ); | |
c801d85f | 70 | |
fd0eed64 | 71 | GtkWidget *menu = gtk_menu_new(); |
c801d85f | 72 | |
fd0eed64 RR |
73 | for (int i = 0; i < n; i++) |
74 | { | |
75 | m_clientDataList.Append( (wxObject*) NULL ); | |
f5e27805 RR |
76 | m_clientObjectList.Append( (wxObject*) NULL ); |
77 | ||
fd0eed64 RR |
78 | GtkWidget *item = gtk_menu_item_new_with_label( choices[i] ); |
79 | gtk_menu_append( GTK_MENU(menu), item ); | |
58614078 | 80 | |
fd0eed64 RR |
81 | gtk_widget_realize( item ); |
82 | gtk_widget_realize( GTK_BIN(item)->child ); | |
58614078 | 83 | |
fd0eed64 RR |
84 | gtk_widget_show( item ); |
85 | ||
86 | gtk_signal_connect( GTK_OBJECT( item ), "activate", | |
87 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
88 | } | |
89 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
c801d85f | 90 | |
fd0eed64 | 91 | m_parent->AddChild( this ); |
6ca41e57 | 92 | |
fd0eed64 | 93 | (m_parent->m_insertCallback)( m_parent, this ); |
6ca41e57 | 94 | |
fd0eed64 | 95 | PostCreation(); |
c801d85f | 96 | |
fd0eed64 RR |
97 | SetBackgroundColour( parent->GetBackgroundColour() ); |
98 | SetForegroundColour( parent->GetForegroundColour() ); | |
a7ac4461 | 99 | SetFont( parent->GetFont() ); |
f96aa4d9 | 100 | |
fd0eed64 | 101 | Show( TRUE ); |
c801d85f | 102 | |
fd0eed64 | 103 | return TRUE; |
6de97a3b | 104 | } |
c801d85f | 105 | |
fd0eed64 RR |
106 | wxChoice::~wxChoice() |
107 | { | |
f5e27805 | 108 | Clear(); |
fd0eed64 RR |
109 | } |
110 | ||
111 | void wxChoice::AppendCommon( const wxString &item ) | |
112 | { | |
113 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); | |
114 | ||
115 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); | |
116 | GtkWidget *menu_item = gtk_menu_item_new_with_label( item ); | |
117 | ||
118 | gtk_menu_append( GTK_MENU(menu), menu_item ); | |
119 | ||
120 | gtk_widget_realize( menu_item ); | |
121 | gtk_widget_realize( GTK_BIN(menu_item)->child ); | |
122 | ||
123 | if (m_widgetStyle) ApplyWidgetStyle(); | |
124 | ||
125 | gtk_signal_connect( GTK_OBJECT( menu_item ), "activate", | |
126 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
127 | ||
128 | gtk_widget_show( menu_item ); | |
129 | } | |
130 | ||
c801d85f KB |
131 | void wxChoice::Append( const wxString &item ) |
132 | { | |
f5e27805 RR |
133 | m_clientDataList.Append( (wxObject*) NULL ); |
134 | m_clientObjectList.Append( (wxObject*) NULL ); | |
fd0eed64 RR |
135 | |
136 | AppendCommon( item ); | |
137 | } | |
138 | ||
139 | void wxChoice::Append( const wxString &item, void *clientData ) | |
140 | { | |
f5e27805 RR |
141 | m_clientDataList.Append( (wxObject*) clientData ); |
142 | m_clientObjectList.Append( (wxObject*) NULL ); | |
fd0eed64 RR |
143 | |
144 | AppendCommon( item ); | |
145 | } | |
146 | ||
147 | void wxChoice::Append( const wxString &item, wxClientData *clientData ) | |
148 | { | |
f5e27805 RR |
149 | m_clientObjectList.Append( (wxObject*) clientData ); |
150 | m_clientDataList.Append( (wxObject*) NULL ); | |
fd0eed64 RR |
151 | |
152 | AppendCommon( item ); | |
153 | } | |
f96aa4d9 | 154 | |
fd0eed64 RR |
155 | void wxChoice::SetClientData( int n, void* clientData ) |
156 | { | |
157 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
f96aa4d9 | 158 | |
fd0eed64 RR |
159 | wxNode *node = m_clientDataList.Nth( n ); |
160 | if (!node) return; | |
f96aa4d9 | 161 | |
f5e27805 | 162 | node->SetData( (wxObject*) clientData ); |
fd0eed64 RR |
163 | } |
164 | ||
165 | void* wxChoice::GetClientData( int n ) | |
166 | { | |
167 | wxCHECK_MSG( m_widget != NULL, NULL, "invalid combobox" ); | |
868a2826 | 168 | |
fd0eed64 RR |
169 | wxNode *node = m_clientDataList.Nth( n ); |
170 | if (!node) return NULL; | |
868a2826 | 171 | |
f5e27805 | 172 | return node->Data(); |
6de97a3b | 173 | } |
fd0eed64 RR |
174 | |
175 | void wxChoice::SetClientObject( int n, wxClientData* clientData ) | |
176 | { | |
177 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
178 | ||
f5e27805 | 179 | wxNode *node = m_clientObjectList.Nth( n ); |
fd0eed64 RR |
180 | if (!node) return; |
181 | ||
182 | wxClientData *cd = (wxClientData*) node->Data(); | |
183 | if (cd) delete cd; | |
f5e27805 | 184 | |
fd0eed64 RR |
185 | node->SetData( (wxObject*) clientData ); |
186 | } | |
187 | ||
188 | wxClientData* wxChoice::GetClientObject( int n ) | |
189 | { | |
f5e27805 | 190 | wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, "invalid combobox" ); |
fd0eed64 | 191 | |
f5e27805 | 192 | wxNode *node = m_clientObjectList.Nth( n ); |
fd0eed64 RR |
193 | if (!node) return (wxClientData*) NULL; |
194 | ||
195 | return (wxClientData*) node->Data(); | |
196 | } | |
c801d85f | 197 | |
fd0eed64 | 198 | void wxChoice::Clear() |
c801d85f | 199 | { |
fd0eed64 | 200 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
f96aa4d9 | 201 | |
fd0eed64 RR |
202 | gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) ); |
203 | GtkWidget *menu = gtk_menu_new(); | |
204 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
205 | ||
f5e27805 | 206 | wxNode *node = m_clientObjectList.First(); |
fd0eed64 RR |
207 | while (node) |
208 | { | |
209 | wxClientData *cd = (wxClientData*)node->Data(); | |
210 | if (cd) delete cd; | |
211 | node = node->Next(); | |
212 | } | |
f5e27805 RR |
213 | m_clientObjectList.Clear(); |
214 | ||
fd0eed64 | 215 | m_clientDataList.Clear(); |
6de97a3b | 216 | } |
c801d85f | 217 | |
2f6407b9 RR |
218 | void wxChoice::Delete( int WXUNUSED(n) ) |
219 | { | |
fd0eed64 | 220 | wxFAIL_MSG( "wxChoice:Delete not implemented" ); |
2f6407b9 RR |
221 | } |
222 | ||
c801d85f KB |
223 | int wxChoice::FindString( const wxString &string ) const |
224 | { | |
fd0eed64 RR |
225 | wxCHECK_MSG( m_widget != NULL, -1, "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 ); | |
b6af8d80 | 239 | |
fd0eed64 | 240 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); |
b6af8d80 | 241 | |
fd0eed64 RR |
242 | if (string == label->label) return count; |
243 | child = child->next; | |
244 | count++; | |
245 | } | |
b6af8d80 | 246 | |
fd0eed64 | 247 | wxFAIL_MSG( "wxChoice: string not found" ); |
b6af8d80 | 248 | |
fd0eed64 | 249 | return -1; |
6de97a3b | 250 | } |
c801d85f | 251 | |
fd0eed64 | 252 | int wxChoice::GetColumns() const |
c801d85f | 253 | { |
fd0eed64 | 254 | return 1; |
6de97a3b | 255 | } |
c801d85f | 256 | |
fd0eed64 | 257 | int wxChoice::GetSelection() |
c801d85f | 258 | { |
fd0eed64 RR |
259 | wxCHECK_MSG( m_widget != NULL, -1, "invalid choice" ); |
260 | ||
261 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
262 | int count = 0; | |
263 | GList *child = menu_shell->children; | |
264 | while (child) | |
265 | { | |
266 | GtkBin *bin = GTK_BIN( child->data ); | |
267 | if (!bin->child) return count; | |
268 | child = child->next; | |
269 | count++; | |
270 | } | |
271 | ||
272 | wxFAIL_MSG( "wxChoice: no selection" ); | |
273 | ||
274 | return -1; | |
6de97a3b | 275 | } |
c801d85f | 276 | |
debe6624 | 277 | wxString wxChoice::GetString( int n ) const |
c801d85f | 278 | { |
fd0eed64 RR |
279 | wxCHECK_MSG( m_widget != NULL, "", "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) | |
c801d85f | 285 | { |
fd0eed64 RR |
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 ); | |
b6af8d80 | 292 | |
fd0eed64 | 293 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); |
b6af8d80 | 294 | |
fd0eed64 RR |
295 | return label->label; |
296 | } | |
297 | child = child->next; | |
298 | count++; | |
6de97a3b | 299 | } |
b6af8d80 | 300 | |
fd0eed64 | 301 | wxFAIL_MSG( "wxChoice: string not found" ); |
b6af8d80 | 302 | |
fd0eed64 | 303 | return ""; |
6de97a3b | 304 | } |
c801d85f | 305 | |
fd0eed64 | 306 | wxString wxChoice::GetStringSelection() const |
c801d85f | 307 | { |
fd0eed64 | 308 | wxCHECK_MSG( m_widget != NULL, "", "invalid choice" ); |
f96aa4d9 | 309 | |
fd0eed64 | 310 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); |
b6af8d80 | 311 | |
fd0eed64 | 312 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); |
b6af8d80 | 313 | |
fd0eed64 | 314 | return label->label; |
6de97a3b | 315 | } |
c801d85f | 316 | |
fd0eed64 | 317 | int wxChoice::Number() const |
c801d85f | 318 | { |
fd0eed64 RR |
319 | wxCHECK_MSG( m_widget != NULL, 0, "invalid choice" ); |
320 | ||
321 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
322 | int count = 0; | |
323 | GList *child = menu_shell->children; | |
324 | while (child) | |
325 | { | |
326 | count++; | |
327 | child = child->next; | |
328 | } | |
329 | return count; | |
6de97a3b | 330 | } |
c801d85f | 331 | |
debe6624 | 332 | void wxChoice::SetColumns( int WXUNUSED(n) ) |
c801d85f | 333 | { |
6de97a3b | 334 | } |
c801d85f | 335 | |
debe6624 | 336 | void wxChoice::SetSelection( int n ) |
c801d85f | 337 | { |
fd0eed64 | 338 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
f96aa4d9 | 339 | |
fd0eed64 RR |
340 | int tmp = n; |
341 | gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); | |
47908e25 | 342 | |
fd0eed64 | 343 | gtk_choice_clicked_callback( (GtkWidget *) NULL, this ); |
6de97a3b | 344 | } |
c801d85f KB |
345 | |
346 | void wxChoice::SetStringSelection( const wxString &string ) | |
347 | { | |
fd0eed64 | 348 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
f96aa4d9 | 349 | |
fd0eed64 RR |
350 | int n = FindString( string ); |
351 | if (n != -1) SetSelection( n ); | |
6de97a3b | 352 | } |
c801d85f | 353 | |
58614078 | 354 | void wxChoice::ApplyWidgetStyle() |
868a2826 | 355 | { |
fd0eed64 | 356 | SetWidgetStyle(); |
f96aa4d9 | 357 | |
fd0eed64 | 358 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
f96aa4d9 | 359 | |
fd0eed64 RR |
360 | gtk_widget_set_style( m_widget, m_widgetStyle ); |
361 | gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle ); | |
f96aa4d9 | 362 | |
fd0eed64 RR |
363 | GList *child = menu_shell->children; |
364 | while (child) | |
365 | { | |
366 | gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle ); | |
58614078 | 367 | |
fd0eed64 RR |
368 | GtkBin *bin = GTK_BIN( child->data ); |
369 | GtkWidget *label = (GtkWidget *) NULL; | |
370 | if (bin->child) label = bin->child; | |
371 | if (!label) label = GTK_BUTTON(m_widget)->child; | |
58614078 | 372 | |
fd0eed64 | 373 | gtk_widget_set_style( label, m_widgetStyle ); |
58614078 | 374 | |
fd0eed64 RR |
375 | child = child->next; |
376 | } | |
f96aa4d9 RR |
377 | } |
378 |