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