]>
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 | ||
85 | PostCreation(); | |
86 | ||
f96aa4d9 | 87 | SetBackgroundColour( parent->GetBackgroundColour() ); |
58614078 | 88 | SetForegroundColour( parent->GetForegroundColour() ); |
f96aa4d9 | 89 | |
c801d85f KB |
90 | Show( TRUE ); |
91 | ||
92 | return TRUE; | |
6de97a3b | 93 | } |
c801d85f KB |
94 | |
95 | void wxChoice::Append( const wxString &item ) | |
96 | { | |
f96aa4d9 RR |
97 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
98 | ||
c801d85f | 99 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); |
f96aa4d9 RR |
100 | GtkWidget *menu_item = gtk_menu_item_new_with_label( item ); |
101 | ||
102 | gtk_menu_append( GTK_MENU(menu), menu_item ); | |
103 | ||
104 | gtk_widget_realize( menu_item ); | |
105 | gtk_widget_realize( GTK_BIN(menu_item)->child ); | |
868a2826 | 106 | |
58614078 | 107 | if (m_widgetStyle) ApplyWidgetStyle(); |
868a2826 | 108 | |
c801d85f KB |
109 | gtk_signal_connect( GTK_OBJECT( menu_item ), "activate", |
110 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
868a2826 | 111 | |
c801d85f | 112 | gtk_widget_show( menu_item ); |
6de97a3b | 113 | } |
c801d85f KB |
114 | |
115 | void wxChoice::Clear(void) | |
116 | { | |
f96aa4d9 RR |
117 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
118 | ||
c801d85f KB |
119 | gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) ); |
120 | GtkWidget *menu = gtk_menu_new(); | |
121 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
6de97a3b | 122 | } |
c801d85f | 123 | |
2f6407b9 RR |
124 | void wxChoice::Delete( int WXUNUSED(n) ) |
125 | { | |
126 | wxFAIL_MSG( "wxChoice:Delete not implemented" ); | |
127 | } | |
128 | ||
c801d85f KB |
129 | int wxChoice::FindString( const wxString &string ) const |
130 | { | |
f96aa4d9 RR |
131 | wxCHECK_MSG( m_widget != NULL, -1, "invalid choice" ); |
132 | ||
47908e25 RR |
133 | // If you read this code once and you think you understand |
134 | // it, then you are very wrong. Robert Roebling. | |
c801d85f KB |
135 | |
136 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
137 | int count = 0; | |
138 | GList *child = menu_shell->children; | |
139 | while (child) | |
140 | { | |
141 | GtkBin *bin = GTK_BIN( child->data ); | |
c67daf87 | 142 | GtkLabel *label = (GtkLabel *) NULL; |
47908e25 | 143 | if (bin->child) label = GTK_LABEL(bin->child); |
868a2826 | 144 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); |
b6af8d80 RR |
145 | |
146 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); | |
147 | ||
c801d85f KB |
148 | if (string == label->label) return count; |
149 | child = child->next; | |
150 | count++; | |
6de97a3b | 151 | } |
b6af8d80 RR |
152 | |
153 | wxFAIL_MSG( "wxChoice: string not found" ); | |
154 | ||
c801d85f | 155 | return -1; |
6de97a3b | 156 | } |
c801d85f KB |
157 | |
158 | int wxChoice::GetColumns(void) const | |
159 | { | |
160 | return 1; | |
6de97a3b | 161 | } |
c801d85f KB |
162 | |
163 | int wxChoice::GetSelection(void) | |
164 | { | |
f96aa4d9 RR |
165 | wxCHECK_MSG( m_widget != NULL, -1, "invalid choice" ); |
166 | ||
c801d85f KB |
167 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
168 | int count = 0; | |
169 | GList *child = menu_shell->children; | |
170 | while (child) | |
171 | { | |
172 | GtkBin *bin = GTK_BIN( child->data ); | |
173 | if (!bin->child) return count; | |
174 | child = child->next; | |
175 | count++; | |
6de97a3b | 176 | } |
b6af8d80 RR |
177 | |
178 | wxFAIL_MSG( "wxChoice: no selection" ); | |
179 | ||
c801d85f | 180 | return -1; |
6de97a3b | 181 | } |
c801d85f | 182 | |
debe6624 | 183 | wxString wxChoice::GetString( int n ) const |
c801d85f | 184 | { |
f96aa4d9 RR |
185 | wxCHECK_MSG( m_widget != NULL, "", "invalid choice" ); |
186 | ||
c801d85f KB |
187 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
188 | int count = 0; | |
189 | GList *child = menu_shell->children; | |
190 | while (child) | |
191 | { | |
192 | GtkBin *bin = GTK_BIN( child->data ); | |
193 | if (count == n) | |
194 | { | |
c67daf87 | 195 | GtkLabel *label = (GtkLabel *) NULL; |
47908e25 | 196 | if (bin->child) label = GTK_LABEL(bin->child); |
868a2826 | 197 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); |
b6af8d80 RR |
198 | |
199 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); | |
200 | ||
c801d85f | 201 | return label->label; |
6de97a3b | 202 | } |
c801d85f KB |
203 | child = child->next; |
204 | count++; | |
6de97a3b | 205 | } |
b6af8d80 RR |
206 | |
207 | wxFAIL_MSG( "wxChoice: string not found" ); | |
208 | ||
c801d85f | 209 | return ""; |
6de97a3b | 210 | } |
c801d85f KB |
211 | |
212 | wxString wxChoice::GetStringSelection(void) const | |
213 | { | |
f96aa4d9 RR |
214 | wxCHECK_MSG( m_widget != NULL, "", "invalid choice" ); |
215 | ||
c801d85f | 216 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); |
b6af8d80 RR |
217 | |
218 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); | |
219 | ||
c801d85f | 220 | return label->label; |
6de97a3b | 221 | } |
c801d85f KB |
222 | |
223 | int wxChoice::Number(void) const | |
224 | { | |
f96aa4d9 RR |
225 | wxCHECK_MSG( m_widget != NULL, 0, "invalid choice" ); |
226 | ||
7a4b9130 | 227 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
c801d85f | 228 | int count = 0; |
7a4b9130 | 229 | GList *child = menu_shell->children; |
c801d85f KB |
230 | while (child) |
231 | { | |
232 | count++; | |
233 | child = child->next; | |
6de97a3b | 234 | } |
c801d85f | 235 | return count; |
6de97a3b | 236 | } |
c801d85f | 237 | |
debe6624 | 238 | void wxChoice::SetColumns( int WXUNUSED(n) ) |
c801d85f | 239 | { |
6de97a3b | 240 | } |
c801d85f | 241 | |
debe6624 | 242 | void wxChoice::SetSelection( int n ) |
c801d85f | 243 | { |
f96aa4d9 RR |
244 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
245 | ||
c801d85f KB |
246 | int tmp = n; |
247 | gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); | |
47908e25 | 248 | |
c67daf87 | 249 | gtk_choice_clicked_callback( (GtkWidget *) NULL, this ); |
6de97a3b | 250 | } |
c801d85f KB |
251 | |
252 | void wxChoice::SetStringSelection( const wxString &string ) | |
253 | { | |
f96aa4d9 RR |
254 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); |
255 | ||
c801d85f KB |
256 | int n = FindString( string ); |
257 | if (n != -1) SetSelection( n ); | |
6de97a3b | 258 | } |
c801d85f | 259 | |
58614078 | 260 | void wxChoice::ApplyWidgetStyle() |
868a2826 | 261 | { |
58614078 | 262 | SetWidgetStyle(); |
f96aa4d9 | 263 | |
f96aa4d9 RR |
264 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
265 | ||
a81258be RR |
266 | gtk_widget_set_style( m_widget, m_widgetStyle ); |
267 | gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle ); | |
f96aa4d9 RR |
268 | |
269 | GList *child = menu_shell->children; | |
270 | while (child) | |
271 | { | |
a81258be | 272 | gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle ); |
58614078 RR |
273 | |
274 | GtkBin *bin = GTK_BIN( child->data ); | |
275 | GtkWidget *label = (GtkWidget *) NULL; | |
276 | if (bin->child) label = bin->child; | |
277 | if (!label) label = GTK_BUTTON(m_widget)->child; | |
278 | ||
279 | gtk_widget_set_style( label, m_widgetStyle ); | |
280 | ||
f96aa4d9 RR |
281 | child = child->next; |
282 | } | |
283 | } | |
284 |