]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/choice.cpp
Added ODBC database classes and sample from RemStar (sample needs work for wxWin 2)
[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), gpointer data )
23 {
24 wxChoice *choice = (wxChoice*)data;
25 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId());
26 event.SetInt( choice->GetSelection() );
27 wxString tmp( choice->GetStringSelection() );
28 event.SetString( WXSTRINGCAST(tmp) );
29 event.SetEventObject(choice);
30 choice->ProcessEvent(event);
31 };
32
33 //-----------------------------------------------------------------------------
34
35 IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
36
37 wxChoice::wxChoice(void)
38 {
39 };
40
41 wxChoice::wxChoice( wxWindow *parent, wxWindowID id,
42 const wxPoint &pos, const wxSize &size,
43 int n, const wxString choices[],
44 long style, const wxString &name )
45 {
46 Create( parent, id, pos, size, n, choices, style, name );
47 };
48
49 bool wxChoice::Create( 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 m_needParent = TRUE;
55
56 PreCreation( parent, id, pos, size, style, name );
57
58 m_widget = gtk_option_menu_new();
59
60 wxSize newSize = size;
61 if (newSize.x == -1) newSize.x = 80;
62 if (newSize.y == -1) newSize.y = 26;
63 SetSize( newSize.x, newSize.y );
64
65 GtkWidget *menu;
66 menu = gtk_menu_new();
67
68 for (int i = 0; i < n; i++)
69 {
70 GtkWidget *item;
71 item = gtk_menu_item_new_with_label( choices[i] );
72 gtk_signal_connect( GTK_OBJECT( item ), "activate",
73 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
74 gtk_menu_append( GTK_MENU(menu), item );
75 gtk_widget_show( item );
76 };
77 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
78
79 PostCreation();
80
81 Show( TRUE );
82
83 return TRUE;
84 };
85
86 void wxChoice::Append( const wxString &item )
87 {
88 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
89 GtkWidget *menu_item;
90 menu_item = gtk_menu_item_new_with_label( item );
91 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
92 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
93 gtk_menu_append( GTK_MENU(menu), menu_item );
94 gtk_widget_show( menu_item );
95 };
96
97 void wxChoice::Clear(void)
98 {
99 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
100 GtkWidget *menu = gtk_menu_new();
101 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
102 };
103
104 int wxChoice::FindString( const wxString &string ) const
105 {
106 // If you read this code once and you think you undestand
107 // it, then you are very wrong. RR
108
109 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
110 int count = 0;
111 GList *child = menu_shell->children;
112 while (child)
113 {
114 GtkBin *bin = GTK_BIN( child->data );
115 GtkLabel *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 = GTK_LABEL(bin->child);
155 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
156 return label->label;
157 };
158 child = child->next;
159 count++;
160 };
161 return "";
162 };
163
164 wxString wxChoice::GetStringSelection(void) const
165 {
166 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
167 return label->label;
168 };
169
170 int wxChoice::Number(void) const
171 {
172 GtkMenu *menu = GTK_MENU( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
173 int count = 0;
174 GList *child = menu->children;
175 while (child)
176 {
177 count++;
178 child = child->next;
179 };
180 return count;
181 };
182
183 void wxChoice::SetColumns( int WXUNUSED(n) )
184 {
185 };
186
187 void wxChoice::SetSelection( int n )
188 {
189 int tmp = n;
190 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
191 };
192
193 void wxChoice::SetStringSelection( const wxString &string )
194 {
195 int n = FindString( string );
196 if (n != -1) SetSelection( n );
197 };
198