Fixed two small things in wxListCtrl
[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 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
96 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
97 gtk_menu_append( GTK_MENU(menu), menu_item );
98 gtk_widget_show( menu_item );
99 }
100
101 void wxChoice::Clear(void)
102 {
103 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
104 GtkWidget *menu = gtk_menu_new();
105 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
106 }
107
108 void wxChoice::Delete( int WXUNUSED(n) )
109 {
110 wxFAIL_MSG( "wxChoice:Delete not implemented" );
111 }
112
113 int wxChoice::FindString( const wxString &string ) const
114 {
115 // If you read this code once and you think you understand
116 // it, then you are very wrong. Robert Roebling.
117
118 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
119 int count = 0;
120 GList *child = menu_shell->children;
121 while (child)
122 {
123 GtkBin *bin = GTK_BIN( child->data );
124 GtkLabel *label = (GtkLabel *) NULL;
125 if (bin->child) label = GTK_LABEL(bin->child);
126
127 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
128
129 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
130 if (string == label->label) return count;
131 child = child->next;
132 count++;
133 }
134
135 wxFAIL_MSG( "wxChoice: string not found" );
136
137 return -1;
138 }
139
140 int wxChoice::GetColumns(void) const
141 {
142 return 1;
143 }
144
145 int wxChoice::GetSelection(void)
146 {
147 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
148 int count = 0;
149 GList *child = menu_shell->children;
150 while (child)
151 {
152 GtkBin *bin = GTK_BIN( child->data );
153 if (!bin->child) return count;
154 child = child->next;
155 count++;
156 }
157
158 wxFAIL_MSG( "wxChoice: no selection" );
159
160 return -1;
161 }
162
163 wxString wxChoice::GetString( int n ) const
164 {
165 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
166 int count = 0;
167 GList *child = menu_shell->children;
168 while (child)
169 {
170 GtkBin *bin = GTK_BIN( child->data );
171 if (count == n)
172 {
173 GtkLabel *label = (GtkLabel *) NULL;
174 if (bin->child) label = GTK_LABEL(bin->child);
175
176 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
177
178 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
179 return label->label;
180 }
181 child = child->next;
182 count++;
183 }
184
185 wxFAIL_MSG( "wxChoice: string not found" );
186
187 return "";
188 }
189
190 wxString wxChoice::GetStringSelection(void) const
191 {
192 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
193
194 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
195
196 return label->label;
197 }
198
199 int wxChoice::Number(void) const
200 {
201 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
202 int count = 0;
203 GList *child = menu_shell->children;
204 while (child)
205 {
206 count++;
207 child = child->next;
208 }
209 return count;
210 }
211
212 void wxChoice::SetColumns( int WXUNUSED(n) )
213 {
214 }
215
216 void wxChoice::SetSelection( int n )
217 {
218 int tmp = n;
219 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
220
221 gtk_choice_clicked_callback( (GtkWidget *) NULL, this );
222 }
223
224 void wxChoice::SetStringSelection( const wxString &string )
225 {
226 int n = FindString( string );
227 if (n != -1) SetSelection( n );
228 }
229