* Fixes and new features in wxObject*Stream
[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 // wxChoice
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
43 IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
44
45 wxChoice::wxChoice(void)
46 {
47 };
48
49 wxChoice::wxChoice( wxWindow *parent, wxWindowID id,
50 const wxPoint &pos, const wxSize &size,
51 int n, const wxString choices[],
52 long style, const wxString &name )
53 {
54 Create( parent, id, pos, size, n, choices, style, name );
55 };
56
57 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
58 const wxPoint &pos, const wxSize &size,
59 int n, const wxString choices[],
60 long style, const wxString &name )
61 {
62 m_needParent = TRUE;
63
64 PreCreation( parent, id, pos, size, style, name );
65
66 m_widget = gtk_option_menu_new();
67
68 wxSize newSize = size;
69 if (newSize.x == -1) newSize.x = 80;
70 if (newSize.y == -1) newSize.y = 26;
71 SetSize( newSize.x, newSize.y );
72
73 GtkWidget *menu;
74 menu = gtk_menu_new();
75
76 for (int i = 0; i < n; i++)
77 {
78 GtkWidget *item;
79 item = gtk_menu_item_new_with_label( choices[i] );
80 gtk_signal_connect( GTK_OBJECT( item ), "activate",
81 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
82 gtk_menu_append( GTK_MENU(menu), item );
83 gtk_widget_show( item );
84 };
85 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
86
87 PostCreation();
88
89 Show( TRUE );
90
91 return TRUE;
92 };
93
94 void wxChoice::Append( const wxString &item )
95 {
96 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
97 GtkWidget *menu_item;
98 menu_item = gtk_menu_item_new_with_label( item );
99 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
100 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
101 gtk_menu_append( GTK_MENU(menu), menu_item );
102 gtk_widget_show( menu_item );
103 };
104
105 void wxChoice::Clear(void)
106 {
107 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
108 GtkWidget *menu = gtk_menu_new();
109 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
110 };
111
112 int wxChoice::FindString( const wxString &string ) const
113 {
114 // If you read this code once and you think you understand
115 // it, then you are very wrong. Robert Roebling.
116
117 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
118 int count = 0;
119 GList *child = menu_shell->children;
120 while (child)
121 {
122 GtkBin *bin = GTK_BIN( child->data );
123 GtkLabel *label = NULL;
124 if (bin->child) label = GTK_LABEL(bin->child);
125 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
126 if (string == label->label) return count;
127 child = child->next;
128 count++;
129 };
130 return -1;
131 };
132
133 int wxChoice::GetColumns(void) const
134 {
135 return 1;
136 };
137
138 int wxChoice::GetSelection(void)
139 {
140 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
141 int count = 0;
142 GList *child = menu_shell->children;
143 while (child)
144 {
145 GtkBin *bin = GTK_BIN( child->data );
146 if (!bin->child) return count;
147 child = child->next;
148 count++;
149 };
150 return -1;
151 };
152
153 wxString wxChoice::GetString( int n ) const
154 {
155 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
156 int count = 0;
157 GList *child = menu_shell->children;
158 while (child)
159 {
160 GtkBin *bin = GTK_BIN( child->data );
161 if (count == n)
162 {
163 GtkLabel *label = NULL;
164 if (bin->child) label = GTK_LABEL(bin->child);
165 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
166 return label->label;
167 };
168 child = child->next;
169 count++;
170 };
171 return "";
172 };
173
174 wxString wxChoice::GetStringSelection(void) const
175 {
176 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
177 return label->label;
178 };
179
180 int wxChoice::Number(void) const
181 {
182 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
183 int count = 0;
184 GList *child = menu_shell->children;
185 while (child)
186 {
187 count++;
188 child = child->next;
189 };
190 return count;
191 };
192
193 void wxChoice::SetColumns( int WXUNUSED(n) )
194 {
195 };
196
197 void wxChoice::SetSelection( int n )
198 {
199 int tmp = n;
200 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
201
202 gtk_choice_clicked_callback( NULL, this );
203 };
204
205 void wxChoice::SetStringSelection( const wxString &string )
206 {
207 int n = FindString( string );
208 if (n != -1) SetSelection( n );
209 };
210