]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/choice.cpp
no message
[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 // "activate"
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 // wxChoice
43 //-----------------------------------------------------------------------------
44
45 IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
46
47 wxChoice::wxChoice(void)
48 {
49 }
50
51 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
52 const wxPoint &pos, const wxSize &size,
53 int n, const wxString choices[],
54 long style, const wxValidator& validator, const wxString &name )
55 {
56 m_needParent = TRUE;
57
58 PreCreation( parent, id, pos, size, style, name );
59
60 SetValidator( validator );
61
62 m_widget = gtk_option_menu_new();
63
64 wxSize newSize = size;
65 if (newSize.x == -1) newSize.x = 80;
66 if (newSize.y == -1) newSize.y = 26;
67 SetSize( newSize.x, newSize.y );
68
69 GtkWidget *menu;
70 menu = gtk_menu_new();
71
72 for (int i = 0; i < n; i++)
73 {
74 GtkWidget *item;
75 item = gtk_menu_item_new_with_label( choices[i] );
76 gtk_signal_connect( GTK_OBJECT( item ), "activate",
77 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
78 gtk_menu_append( GTK_MENU(menu), item );
79 gtk_widget_show( item );
80 }
81 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
82
83 PostCreation();
84
85 Show( TRUE );
86
87 return TRUE;
88 }
89
90 void wxChoice::Append( const wxString &item )
91 {
92 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
93 GtkWidget *menu_item;
94 menu_item = gtk_menu_item_new_with_label( item );
95
96 if (m_hasOwnStyle)
97 {
98 GtkBin *bin = GTK_BIN( menu_item );
99 gtk_widget_set_style( bin->child,
100 gtk_style_ref(
101 gtk_widget_get_style( m_widget ) ) );
102 }
103
104 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
105 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
106
107 gtk_menu_append( GTK_MENU(menu), menu_item );
108 gtk_widget_show( menu_item );
109 }
110
111 void wxChoice::Clear(void)
112 {
113 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
114 GtkWidget *menu = gtk_menu_new();
115 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
116 }
117
118 void wxChoice::Delete( int WXUNUSED(n) )
119 {
120 wxFAIL_MSG( "wxChoice:Delete not implemented" );
121 }
122
123 int wxChoice::FindString( const wxString &string ) const
124 {
125 // If you read this code once and you think you understand
126 // it, then you are very wrong. Robert Roebling.
127
128 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
129 int count = 0;
130 GList *child = menu_shell->children;
131 while (child)
132 {
133 GtkBin *bin = GTK_BIN( child->data );
134 GtkLabel *label = (GtkLabel *) NULL;
135 if (bin->child) label = GTK_LABEL(bin->child);
136 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
137
138 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
139
140 if (string == label->label) return count;
141 child = child->next;
142 count++;
143 }
144
145 wxFAIL_MSG( "wxChoice: string not found" );
146
147 return -1;
148 }
149
150 int wxChoice::GetColumns(void) const
151 {
152 return 1;
153 }
154
155 int wxChoice::GetSelection(void)
156 {
157 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
158 int count = 0;
159 GList *child = menu_shell->children;
160 while (child)
161 {
162 GtkBin *bin = GTK_BIN( child->data );
163 if (!bin->child) return count;
164 child = child->next;
165 count++;
166 }
167
168 wxFAIL_MSG( "wxChoice: no selection" );
169
170 return -1;
171 }
172
173 wxString wxChoice::GetString( int n ) const
174 {
175 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
176 int count = 0;
177 GList *child = menu_shell->children;
178 while (child)
179 {
180 GtkBin *bin = GTK_BIN( child->data );
181 if (count == n)
182 {
183 GtkLabel *label = (GtkLabel *) NULL;
184 if (bin->child) label = GTK_LABEL(bin->child);
185 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
186
187 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
188
189 return label->label;
190 }
191 child = child->next;
192 count++;
193 }
194
195 wxFAIL_MSG( "wxChoice: string not found" );
196
197 return "";
198 }
199
200 wxString wxChoice::GetStringSelection(void) const
201 {
202 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
203
204 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
205
206 return label->label;
207 }
208
209 int wxChoice::Number(void) const
210 {
211 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
212 int count = 0;
213 GList *child = menu_shell->children;
214 while (child)
215 {
216 count++;
217 child = child->next;
218 }
219 return count;
220 }
221
222 void wxChoice::SetColumns( int WXUNUSED(n) )
223 {
224 }
225
226 void wxChoice::SetSelection( int n )
227 {
228 int tmp = n;
229 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
230
231 gtk_choice_clicked_callback( (GtkWidget *) NULL, this );
232 }
233
234 void wxChoice::SetStringSelection( const wxString &string )
235 {
236 int n = FindString( string );
237 if (n != -1) SetSelection( n );
238 }
239
240 void wxChoice::SetFont( const wxFont &font )
241 {
242 wxWindow::SetFont( font );
243
244 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
245 GList *child = menu_shell->children;
246 while (child)
247 {
248 GtkBin *bin = GTK_BIN( child->data );
249 GtkWidget *label = (GtkWidget *) NULL;
250 if (bin->child) label = bin->child;
251 if (!label) label = GTK_BUTTON(m_widget)->child;
252
253 gtk_widget_set_style( label,
254 gtk_style_ref(
255 gtk_widget_get_style( m_widget ) ) );
256
257 child = child->next;
258 }
259 }