]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
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 | ||
66bd6b93 RR |
18 | //----------------------------------------------------------------------------- |
19 | // data | |
20 | //----------------------------------------------------------------------------- | |
21 | ||
22 | extern bool g_blockEventsOnDrag; | |
23 | ||
c801d85f | 24 | //----------------------------------------------------------------------------- |
e1e955e1 | 25 | // "activate" |
c801d85f KB |
26 | //----------------------------------------------------------------------------- |
27 | ||
66bd6b93 | 28 | static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice ) |
c801d85f | 29 | { |
66bd6b93 RR |
30 | if (!choice->HasVMT()) return; |
31 | if (g_blockEventsOnDrag) return; | |
32 | ||
47908e25 | 33 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() ); |
c801d85f KB |
34 | event.SetInt( choice->GetSelection() ); |
35 | wxString tmp( choice->GetStringSelection() ); | |
36 | event.SetString( WXSTRINGCAST(tmp) ); | |
37 | event.SetEventObject(choice); | |
47908e25 | 38 | choice->GetEventHandler()->ProcessEvent(event); |
6de97a3b | 39 | } |
c801d85f | 40 | |
e1e955e1 RR |
41 | //----------------------------------------------------------------------------- |
42 | // wxChoice | |
c801d85f KB |
43 | //----------------------------------------------------------------------------- |
44 | ||
7f4dc78d | 45 | IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl) |
c801d85f KB |
46 | |
47 | wxChoice::wxChoice(void) | |
48 | { | |
6de97a3b | 49 | } |
c801d85f | 50 | |
debe6624 JS |
51 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, |
52 | const wxPoint &pos, const wxSize &size, | |
53 | int n, const wxString choices[], | |
6de97a3b | 54 | long style, const wxValidator& validator, const wxString &name ) |
c801d85f KB |
55 | { |
56 | m_needParent = TRUE; | |
57 | ||
58 | PreCreation( parent, id, pos, size, style, name ); | |
59 | ||
6de97a3b RR |
60 | SetValidator( validator ); |
61 | ||
c801d85f KB |
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 ); | |
6de97a3b | 80 | } |
c801d85f KB |
81 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); |
82 | ||
83 | PostCreation(); | |
84 | ||
85 | Show( TRUE ); | |
86 | ||
87 | return TRUE; | |
6de97a3b | 88 | } |
c801d85f KB |
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 ); | |
6de97a3b | 99 | } |
c801d85f KB |
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 ); | |
6de97a3b | 106 | } |
c801d85f | 107 | |
2f6407b9 RR |
108 | void wxChoice::Delete( int WXUNUSED(n) ) |
109 | { | |
110 | wxFAIL_MSG( "wxChoice:Delete not implemented" ); | |
111 | } | |
112 | ||
c801d85f KB |
113 | int wxChoice::FindString( const wxString &string ) const |
114 | { | |
47908e25 RR |
115 | // If you read this code once and you think you understand |
116 | // it, then you are very wrong. Robert Roebling. | |
c801d85f KB |
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 ); | |
c67daf87 | 124 | GtkLabel *label = (GtkLabel *) NULL; |
47908e25 | 125 | if (bin->child) label = GTK_LABEL(bin->child); |
b6af8d80 RR |
126 | |
127 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); | |
128 | ||
c801d85f KB |
129 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); |
130 | if (string == label->label) return count; | |
131 | child = child->next; | |
132 | count++; | |
6de97a3b | 133 | } |
b6af8d80 RR |
134 | |
135 | wxFAIL_MSG( "wxChoice: string not found" ); | |
136 | ||
c801d85f | 137 | return -1; |
6de97a3b | 138 | } |
c801d85f KB |
139 | |
140 | int wxChoice::GetColumns(void) const | |
141 | { | |
142 | return 1; | |
6de97a3b | 143 | } |
c801d85f KB |
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++; | |
6de97a3b | 156 | } |
b6af8d80 RR |
157 | |
158 | wxFAIL_MSG( "wxChoice: no selection" ); | |
159 | ||
c801d85f | 160 | return -1; |
6de97a3b | 161 | } |
c801d85f | 162 | |
debe6624 | 163 | wxString wxChoice::GetString( int n ) const |
c801d85f KB |
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 | { | |
c67daf87 | 173 | GtkLabel *label = (GtkLabel *) NULL; |
47908e25 | 174 | if (bin->child) label = GTK_LABEL(bin->child); |
b6af8d80 RR |
175 | |
176 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); | |
177 | ||
c801d85f KB |
178 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); |
179 | return label->label; | |
6de97a3b | 180 | } |
c801d85f KB |
181 | child = child->next; |
182 | count++; | |
6de97a3b | 183 | } |
b6af8d80 RR |
184 | |
185 | wxFAIL_MSG( "wxChoice: string not found" ); | |
186 | ||
c801d85f | 187 | return ""; |
6de97a3b | 188 | } |
c801d85f KB |
189 | |
190 | wxString wxChoice::GetStringSelection(void) const | |
191 | { | |
192 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); | |
b6af8d80 RR |
193 | |
194 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); | |
195 | ||
c801d85f | 196 | return label->label; |
6de97a3b | 197 | } |
c801d85f KB |
198 | |
199 | int wxChoice::Number(void) const | |
200 | { | |
7a4b9130 | 201 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
c801d85f | 202 | int count = 0; |
7a4b9130 | 203 | GList *child = menu_shell->children; |
c801d85f KB |
204 | while (child) |
205 | { | |
206 | count++; | |
207 | child = child->next; | |
6de97a3b | 208 | } |
c801d85f | 209 | return count; |
6de97a3b | 210 | } |
c801d85f | 211 | |
debe6624 | 212 | void wxChoice::SetColumns( int WXUNUSED(n) ) |
c801d85f | 213 | { |
6de97a3b | 214 | } |
c801d85f | 215 | |
debe6624 | 216 | void wxChoice::SetSelection( int n ) |
c801d85f KB |
217 | { |
218 | int tmp = n; | |
219 | gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); | |
47908e25 | 220 | |
c67daf87 | 221 | gtk_choice_clicked_callback( (GtkWidget *) NULL, this ); |
6de97a3b | 222 | } |
c801d85f KB |
223 | |
224 | void wxChoice::SetStringSelection( const wxString &string ) | |
225 | { | |
226 | int n = FindString( string ); | |
227 | if (n != -1) SetSelection( n ); | |
6de97a3b | 228 | } |
c801d85f | 229 |