]>
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 | ||
66bd6b93 RR |
17 | //----------------------------------------------------------------------------- |
18 | // data | |
19 | //----------------------------------------------------------------------------- | |
20 | ||
21 | extern bool g_blockEventsOnDrag; | |
22 | ||
c801d85f | 23 | //----------------------------------------------------------------------------- |
e1e955e1 | 24 | // "activate" |
c801d85f KB |
25 | //----------------------------------------------------------------------------- |
26 | ||
66bd6b93 | 27 | static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice ) |
c801d85f | 28 | { |
66bd6b93 RR |
29 | if (!choice->HasVMT()) return; |
30 | if (g_blockEventsOnDrag) return; | |
31 | ||
47908e25 | 32 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() ); |
c801d85f KB |
33 | event.SetInt( choice->GetSelection() ); |
34 | wxString tmp( choice->GetStringSelection() ); | |
35 | event.SetString( WXSTRINGCAST(tmp) ); | |
36 | event.SetEventObject(choice); | |
47908e25 | 37 | choice->GetEventHandler()->ProcessEvent(event); |
6de97a3b | 38 | } |
c801d85f | 39 | |
e1e955e1 RR |
40 | //----------------------------------------------------------------------------- |
41 | // wxChoice | |
c801d85f KB |
42 | //----------------------------------------------------------------------------- |
43 | ||
7f4dc78d | 44 | IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl) |
c801d85f KB |
45 | |
46 | wxChoice::wxChoice(void) | |
47 | { | |
6de97a3b | 48 | } |
c801d85f | 49 | |
debe6624 JS |
50 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, |
51 | const wxPoint &pos, const wxSize &size, | |
52 | int n, const wxString choices[], | |
6de97a3b | 53 | long style, const wxValidator& validator, const wxString &name ) |
c801d85f KB |
54 | { |
55 | m_needParent = TRUE; | |
56 | ||
57 | PreCreation( parent, id, pos, size, style, name ); | |
58 | ||
6de97a3b RR |
59 | SetValidator( validator ); |
60 | ||
c801d85f KB |
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 | ||
f96aa4d9 | 68 | GtkWidget *menu = gtk_menu_new(); |
c801d85f KB |
69 | |
70 | for (int i = 0; i < n; i++) | |
71 | { | |
f96aa4d9 | 72 | GtkWidget *item = gtk_menu_item_new_with_label( choices[i] ); |
c801d85f | 73 | gtk_menu_append( GTK_MENU(menu), item ); |
f96aa4d9 | 74 | |
f96aa4d9 RR |
75 | gtk_widget_realize( item ); |
76 | gtk_widget_realize( GTK_BIN(item)->child ); | |
58614078 RR |
77 | |
78 | gtk_widget_show( item ); | |
79 | ||
80 | gtk_signal_connect( GTK_OBJECT( item ), "activate", | |
81 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
6de97a3b | 82 | } |
c801d85f KB |
83 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); |
84 | ||
6ca41e57 RR |
85 | m_parent->AddChild( this ); |
86 | ||
87 | (m_parent->m_insertCallback)( m_parent, this ); | |
88 | ||
c801d85f KB |
89 | PostCreation(); |
90 | ||
f96aa4d9 | 91 | SetBackgroundColour( parent->GetBackgroundColour() ); |
58614078 | 92 | SetForegroundColour( parent->GetForegroundColour() ); |
f96aa4d9 | 93 | |
c801d85f KB |
94 | Show( TRUE ); |
95 | ||
96 | return TRUE; | |
6de97a3b | 97 | } |
c801d85f KB |
98 | |
99 | void wxChoice::Append( const wxString &item ) | |
100 | { | |
f96aa4d9 RR |
101 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
102 | ||
c801d85f | 103 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); |
f96aa4d9 RR |
104 | GtkWidget *menu_item = gtk_menu_item_new_with_label( item ); |
105 | ||
106 | gtk_menu_append( GTK_MENU(menu), menu_item ); | |
107 | ||
108 | gtk_widget_realize( menu_item ); | |
109 | gtk_widget_realize( GTK_BIN(menu_item)->child ); | |
868a2826 | 110 | |
58614078 | 111 | if (m_widgetStyle) ApplyWidgetStyle(); |
868a2826 | 112 | |
c801d85f KB |
113 | gtk_signal_connect( GTK_OBJECT( menu_item ), "activate", |
114 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
868a2826 | 115 | |
c801d85f | 116 | gtk_widget_show( menu_item ); |
6de97a3b | 117 | } |
c801d85f KB |
118 | |
119 | void wxChoice::Clear(void) | |
120 | { | |
f96aa4d9 RR |
121 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
122 | ||
c801d85f KB |
123 | gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) ); |
124 | GtkWidget *menu = gtk_menu_new(); | |
125 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
6de97a3b | 126 | } |
c801d85f | 127 | |
2f6407b9 RR |
128 | void wxChoice::Delete( int WXUNUSED(n) ) |
129 | { | |
130 | wxFAIL_MSG( "wxChoice:Delete not implemented" ); | |
131 | } | |
132 | ||
c801d85f KB |
133 | int wxChoice::FindString( const wxString &string ) const |
134 | { | |
f96aa4d9 RR |
135 | wxCHECK_MSG( m_widget != NULL, -1, "invalid choice" ); |
136 | ||
47908e25 RR |
137 | // If you read this code once and you think you understand |
138 | // it, then you are very wrong. Robert Roebling. | |
c801d85f KB |
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 ); | |
c67daf87 | 146 | GtkLabel *label = (GtkLabel *) NULL; |
47908e25 | 147 | if (bin->child) label = GTK_LABEL(bin->child); |
868a2826 | 148 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); |
b6af8d80 RR |
149 | |
150 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); | |
151 | ||
c801d85f KB |
152 | if (string == label->label) return count; |
153 | child = child->next; | |
154 | count++; | |
6de97a3b | 155 | } |
b6af8d80 RR |
156 | |
157 | wxFAIL_MSG( "wxChoice: string not found" ); | |
158 | ||
c801d85f | 159 | return -1; |
6de97a3b | 160 | } |
c801d85f KB |
161 | |
162 | int wxChoice::GetColumns(void) const | |
163 | { | |
164 | return 1; | |
6de97a3b | 165 | } |
c801d85f KB |
166 | |
167 | int wxChoice::GetSelection(void) | |
168 | { | |
f96aa4d9 RR |
169 | wxCHECK_MSG( m_widget != NULL, -1, "invalid choice" ); |
170 | ||
c801d85f KB |
171 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
172 | int count = 0; | |
173 | GList *child = menu_shell->children; | |
174 | while (child) | |
175 | { | |
176 | GtkBin *bin = GTK_BIN( child->data ); | |
177 | if (!bin->child) return count; | |
178 | child = child->next; | |
179 | count++; | |
6de97a3b | 180 | } |
b6af8d80 RR |
181 | |
182 | wxFAIL_MSG( "wxChoice: no selection" ); | |
183 | ||
c801d85f | 184 | return -1; |
6de97a3b | 185 | } |
c801d85f | 186 | |
debe6624 | 187 | wxString wxChoice::GetString( int n ) const |
c801d85f | 188 | { |
f96aa4d9 RR |
189 | wxCHECK_MSG( m_widget != NULL, "", "invalid choice" ); |
190 | ||
c801d85f KB |
191 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
192 | int count = 0; | |
193 | GList *child = menu_shell->children; | |
194 | while (child) | |
195 | { | |
196 | GtkBin *bin = GTK_BIN( child->data ); | |
197 | if (count == n) | |
198 | { | |
c67daf87 | 199 | GtkLabel *label = (GtkLabel *) NULL; |
47908e25 | 200 | if (bin->child) label = GTK_LABEL(bin->child); |
868a2826 | 201 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); |
b6af8d80 RR |
202 | |
203 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); | |
204 | ||
c801d85f | 205 | return label->label; |
6de97a3b | 206 | } |
c801d85f KB |
207 | child = child->next; |
208 | count++; | |
6de97a3b | 209 | } |
b6af8d80 RR |
210 | |
211 | wxFAIL_MSG( "wxChoice: string not found" ); | |
212 | ||
c801d85f | 213 | return ""; |
6de97a3b | 214 | } |
c801d85f KB |
215 | |
216 | wxString wxChoice::GetStringSelection(void) const | |
217 | { | |
f96aa4d9 RR |
218 | wxCHECK_MSG( m_widget != NULL, "", "invalid choice" ); |
219 | ||
c801d85f | 220 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); |
b6af8d80 RR |
221 | |
222 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); | |
223 | ||
c801d85f | 224 | return label->label; |
6de97a3b | 225 | } |
c801d85f KB |
226 | |
227 | int wxChoice::Number(void) const | |
228 | { | |
f96aa4d9 RR |
229 | wxCHECK_MSG( m_widget != NULL, 0, "invalid choice" ); |
230 | ||
7a4b9130 | 231 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
c801d85f | 232 | int count = 0; |
7a4b9130 | 233 | GList *child = menu_shell->children; |
c801d85f KB |
234 | while (child) |
235 | { | |
236 | count++; | |
237 | child = child->next; | |
6de97a3b | 238 | } |
c801d85f | 239 | return count; |
6de97a3b | 240 | } |
c801d85f | 241 | |
debe6624 | 242 | void wxChoice::SetColumns( int WXUNUSED(n) ) |
c801d85f | 243 | { |
6de97a3b | 244 | } |
c801d85f | 245 | |
debe6624 | 246 | void wxChoice::SetSelection( int n ) |
c801d85f | 247 | { |
f96aa4d9 RR |
248 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
249 | ||
c801d85f KB |
250 | int tmp = n; |
251 | gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); | |
47908e25 | 252 | |
c67daf87 | 253 | gtk_choice_clicked_callback( (GtkWidget *) NULL, this ); |
6de97a3b | 254 | } |
c801d85f KB |
255 | |
256 | void wxChoice::SetStringSelection( const wxString &string ) | |
257 | { | |
f96aa4d9 RR |
258 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
259 | ||
c801d85f KB |
260 | int n = FindString( string ); |
261 | if (n != -1) SetSelection( n ); | |
6de97a3b | 262 | } |
c801d85f | 263 | |
58614078 | 264 | void wxChoice::ApplyWidgetStyle() |
868a2826 | 265 | { |
58614078 | 266 | SetWidgetStyle(); |
f96aa4d9 | 267 | |
f96aa4d9 RR |
268 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
269 | ||
a81258be RR |
270 | gtk_widget_set_style( m_widget, m_widgetStyle ); |
271 | gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle ); | |
f96aa4d9 RR |
272 | |
273 | GList *child = menu_shell->children; | |
274 | while (child) | |
275 | { | |
a81258be | 276 | gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle ); |
58614078 RR |
277 | |
278 | GtkBin *bin = GTK_BIN( child->data ); | |
279 | GtkWidget *label = (GtkWidget *) NULL; | |
280 | if (bin->child) label = bin->child; | |
281 | if (!label) label = GTK_BUTTON(m_widget)->child; | |
282 | ||
283 | gtk_widget_set_style( label, m_widgetStyle ); | |
284 | ||
f96aa4d9 RR |
285 | child = child->next; |
286 | } | |
287 | } | |
288 |