]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/listbox.cpp
GTK
[wxWidgets.git] / src / gtk / listbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: listbox.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 "listbox.h"
14 #endif
15
16 #include "wx/listbox.h"
17
18 //-----------------------------------------------------------------------------
19 // wxListBox
20 //-----------------------------------------------------------------------------
21
22 void gtk_listitem_select_callback( GtkWidget *widget, gpointer data )
23 {
24 wxListBox *listbox = (wxListBox*)data;
25
26 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
27
28 event.SetInt( listbox->GetIndex( widget ) );
29
30 GtkBin *bin = GTK_BIN( widget );
31 GtkLabel *label = GTK_LABEL( bin->child );
32 wxString tmp( label->label );
33 event.SetString( WXSTRINGCAST(tmp) );
34
35 event.SetEventObject( listbox );
36
37 listbox->ProcessEvent( event );
38 };
39
40 //-----------------------------------------------------------------------------
41
42 IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
43
44 wxListBox::wxListBox(void)
45 {
46 m_list = NULL;
47 };
48
49 wxListBox::wxListBox( wxWindow *parent, wxWindowID id,
50 const wxPoint &pos, const wxSize &size,
51 const int n, const wxString choices[],
52 const long style, const wxString &name )
53 {
54 Create( parent, id, pos, size, n, choices, style, name );
55 };
56
57 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
58 const wxPoint &pos, const wxSize &size,
59 const int n, const wxString choices[],
60 const long style, const wxString &name )
61 {
62 m_needParent = TRUE;
63
64 PreCreation( parent, id, pos, size, style, name );
65
66 m_widget = gtk_scrolled_window_new( NULL, NULL );
67 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
68 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
69
70 m_list = GTK_LIST( gtk_list_new() );
71 gtk_list_set_selection_mode( GTK_LIST(m_list), GTK_SELECTION_BROWSE );
72
73 gtk_container_add (GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) );
74 gtk_widget_show( GTK_WIDGET(m_list) );
75
76 for (int i = 0; i < n; i++)
77 {
78 GtkWidget *list_item;
79 list_item = gtk_list_item_new_with_label( choices[i] );
80
81 gtk_signal_connect( GTK_OBJECT(list_item), "select",
82 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
83
84 gtk_container_add( GTK_CONTAINER(m_list), list_item );
85
86 gtk_widget_show( list_item );
87 };
88
89 PostCreation();
90
91 Show( TRUE );
92
93 return TRUE;
94 };
95
96 void wxListBox::Append( const wxString &item )
97 {
98 GtkWidget *list_item;
99 list_item = gtk_list_item_new_with_label( item );
100
101 gtk_signal_connect( GTK_OBJECT(list_item), "select",
102 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
103
104 gtk_container_add( GTK_CONTAINER(m_list), list_item );
105
106 gtk_widget_show( list_item );
107 };
108
109 void wxListBox::Append( const wxString &WXUNUSED(item), char *WXUNUSED(clientData) )
110 {
111 };
112
113 void wxListBox::Clear(void)
114 {
115 gtk_list_clear_items( m_list, 0, Number() );
116 };
117
118 void wxListBox::Delete( int n )
119 {
120 gtk_list_clear_items( m_list, n, n );
121 };
122
123 void wxListBox::Deselect( int n )
124 {
125 gtk_list_unselect_item( m_list, n );
126 };
127
128 int wxListBox::FindString( const wxString &item ) const
129 {
130 GList *child = m_list->children;
131 int count = 0;
132 while (child)
133 {
134 GtkBin *bin = GTK_BIN( child->data );
135 GtkLabel *label = GTK_LABEL( bin->child );
136 if (item == label->label) return count;
137 count++;
138 child = child->next;
139 };
140 return -1;
141 };
142
143 char *wxListBox::GetClientData( const int WXUNUSED(n) ) const
144 {
145 return NULL;
146 };
147
148 int wxListBox::GetSelection(void) const
149 {
150 GList *selection = m_list->selection;
151 if (selection)
152 {
153 GList *child = m_list->children;
154 int count = 0;
155 while (child)
156 {
157 if (child->data == selection->data) return count;
158 count++;
159 child = child->next;
160 };
161 };
162 return -1;
163 };
164
165 int wxListBox::GetSelections( int **WXUNUSED(selections) ) const
166 {
167 return 0;
168 };
169
170 wxString wxListBox::GetString( int n ) const
171 {
172 GList *child = g_list_nth( m_list->children, n );
173 if (child)
174 {
175 GtkBin *bin = GTK_BIN( child->data );
176 GtkLabel *label = GTK_LABEL( bin->child );
177 return label->label;
178 };
179 return "";
180 };
181
182 wxString wxListBox::GetStringSelection(void) const
183 {
184 GList *selection = m_list->selection;
185 if (selection)
186 {
187 GtkBin *bin = GTK_BIN( selection->data );
188 wxString tmp = GTK_LABEL( bin->child )->label;
189 return tmp;
190 };
191 return "";
192 };
193
194 int wxListBox::Number(void)
195 {
196 GList *child = m_list->children;
197 int count = 0;
198 while (child) { count++; child = child->next; };
199 return count;
200 };
201
202 bool wxListBox::Selected( const int n )
203 {
204 GList *target = g_list_nth( m_list->children, n );
205 if (target)
206 {
207 GList *child = m_list->selection;
208 while (child)
209 {
210 if (child->data == target->data) return TRUE;
211 child = child->next;
212 };
213 };
214 return FALSE;
215 };
216
217 void wxListBox::Set( const int WXUNUSED(n), const wxString *WXUNUSED(choices) )
218 {
219 };
220
221 void wxListBox::SetClientData( const int WXUNUSED(n), char *WXUNUSED(clientData) )
222 {
223 };
224
225 void wxListBox::SetFirstItem( int WXUNUSED(n) )
226 {
227 };
228
229 void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
230 {
231 };
232
233 void wxListBox::SetSelection( const int n, const bool select )
234 {
235 if (select)
236 gtk_list_select_item( m_list, n );
237 else
238 gtk_list_unselect_item( m_list, n );
239 };
240
241 void wxListBox::SetString( const int WXUNUSED(n), const wxString &WXUNUSED(string) )
242 {
243 };
244
245 void wxListBox::SetStringSelection( const wxString &string, const bool select )
246 {
247 SetSelection( FindString(string), select );
248 };
249
250 int wxListBox::GetIndex( GtkWidget *item ) const
251 {
252 if (item)
253 {
254 GList *child = m_list->children;
255 int count = 0;
256 while (child)
257 {
258 if (GTK_WIDGET(child->data) == item) return count;
259 count++;
260 child = child->next;
261 };
262 };
263 return -1;
264 };
265
266