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