]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/choice.cpp
no message
[wxWidgets.git] / src / gtk1 / choice.cpp
CommitLineData
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
22extern bool g_blockEventsOnDrag;
23
c801d85f 24//-----------------------------------------------------------------------------
e1e955e1 25// "activate"
c801d85f
KB
26//-----------------------------------------------------------------------------
27
66bd6b93 28static 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 45IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
c801d85f
KB
46
47wxChoice::wxChoice(void)
48{
6de97a3b 49}
c801d85f 50
debe6624
JS
51bool 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
90void 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 );
868a2826
RR
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
c801d85f
KB
104 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
105 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
868a2826 106
c801d85f
KB
107 gtk_menu_append( GTK_MENU(menu), menu_item );
108 gtk_widget_show( menu_item );
6de97a3b 109}
c801d85f
KB
110
111void 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 );
6de97a3b 116}
c801d85f 117
2f6407b9
RR
118void wxChoice::Delete( int WXUNUSED(n) )
119{
120 wxFAIL_MSG( "wxChoice:Delete not implemented" );
121}
122
c801d85f
KB
123int wxChoice::FindString( const wxString &string ) const
124{
47908e25
RR
125 // If you read this code once and you think you understand
126 // it, then you are very wrong. Robert Roebling.
c801d85f
KB
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 );
c67daf87 134 GtkLabel *label = (GtkLabel *) NULL;
47908e25 135 if (bin->child) label = GTK_LABEL(bin->child);
868a2826 136 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
b6af8d80
RR
137
138 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
139
c801d85f
KB
140 if (string == label->label) return count;
141 child = child->next;
142 count++;
6de97a3b 143 }
b6af8d80
RR
144
145 wxFAIL_MSG( "wxChoice: string not found" );
146
c801d85f 147 return -1;
6de97a3b 148}
c801d85f
KB
149
150int wxChoice::GetColumns(void) const
151{
152 return 1;
6de97a3b 153}
c801d85f
KB
154
155int 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++;
6de97a3b 166 }
b6af8d80
RR
167
168 wxFAIL_MSG( "wxChoice: no selection" );
169
c801d85f 170 return -1;
6de97a3b 171}
c801d85f 172
debe6624 173wxString wxChoice::GetString( int n ) const
c801d85f
KB
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 {
c67daf87 183 GtkLabel *label = (GtkLabel *) NULL;
47908e25 184 if (bin->child) label = GTK_LABEL(bin->child);
868a2826 185 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
b6af8d80
RR
186
187 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
188
c801d85f 189 return label->label;
6de97a3b 190 }
c801d85f
KB
191 child = child->next;
192 count++;
6de97a3b 193 }
b6af8d80
RR
194
195 wxFAIL_MSG( "wxChoice: string not found" );
196
c801d85f 197 return "";
6de97a3b 198}
c801d85f
KB
199
200wxString wxChoice::GetStringSelection(void) const
201{
202 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
b6af8d80
RR
203
204 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
205
c801d85f 206 return label->label;
6de97a3b 207}
c801d85f
KB
208
209int wxChoice::Number(void) const
210{
7a4b9130 211 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
c801d85f 212 int count = 0;
7a4b9130 213 GList *child = menu_shell->children;
c801d85f
KB
214 while (child)
215 {
216 count++;
217 child = child->next;
6de97a3b 218 }
c801d85f 219 return count;
6de97a3b 220}
c801d85f 221
debe6624 222void wxChoice::SetColumns( int WXUNUSED(n) )
c801d85f 223{
6de97a3b 224}
c801d85f 225
debe6624 226void wxChoice::SetSelection( int n )
c801d85f
KB
227{
228 int tmp = n;
229 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
47908e25 230
c67daf87 231 gtk_choice_clicked_callback( (GtkWidget *) NULL, this );
6de97a3b 232}
c801d85f
KB
233
234void wxChoice::SetStringSelection( const wxString &string )
235{
236 int n = FindString( string );
237 if (n != -1) SetSelection( n );
6de97a3b 238}
c801d85f 239
868a2826
RR
240void 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}