]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bmpcbox.cpp
adjust the labels before centering the dialog as doing it can change the dialog size
[wxWidgets.git] / src / gtk / bmpcbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/bmpcboxg.cpp
3 // Purpose: wxBitmapComboBox
4 // Author: Jaakko Salli
5 // Created: 2008-05-19
6 // RCS-ID: $Id:
7 // Copyright: (c) 2008 Jaakko Salli
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #if wxUSE_BITMAPCOMBOBOX
26
27 #include "wx/bmpcbox.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/log.h"
31 #endif
32
33 #include "wx/gtk/private.h"
34
35 // ============================================================================
36 // implementation
37 // ============================================================================
38
39
40 IMPLEMENT_DYNAMIC_CLASS(wxBitmapComboBox, wxComboBox)
41
42
43 // ----------------------------------------------------------------------------
44 // wxBitmapComboBox creation
45 // ----------------------------------------------------------------------------
46
47 void wxBitmapComboBox::Init()
48 {
49 m_bitmapCellIndex = 0;
50 m_stringCellIndex = 1;
51 m_bitmapSize = wxSize(0, 0);
52 }
53
54 wxBitmapComboBox::wxBitmapComboBox(wxWindow *parent,
55 wxWindowID id,
56 const wxString& value,
57 const wxPoint& pos,
58 const wxSize& size,
59 const wxArrayString& choices,
60 long style,
61 const wxValidator& validator,
62 const wxString& name)
63 : wxComboBox(),
64 wxBitmapComboBoxBase()
65 {
66 Init();
67
68 Create(parent,id,value,pos,size,choices,style,validator,name);
69 }
70
71 bool wxBitmapComboBox::Create(wxWindow *parent,
72 wxWindowID id,
73 const wxString& value,
74 const wxPoint& pos,
75 const wxSize& size,
76 const wxArrayString& choices,
77 long style,
78 const wxValidator& validator,
79 const wxString& name)
80 {
81 wxCArrayString chs(choices);
82 return Create(parent, id, value, pos, size, chs.GetCount(),
83 chs.GetStrings(), style, validator, name);
84 }
85
86 bool wxBitmapComboBox::Create(wxWindow *parent,
87 wxWindowID id,
88 const wxString& value,
89 const wxPoint& pos,
90 const wxSize& size,
91 int n,
92 const wxString choices[],
93 long style,
94 const wxValidator& validator,
95 const wxString& name)
96 {
97 if ( !wxComboBox::Create(parent, id, value, pos, size,
98 n, choices, style, validator, name) )
99 return false;
100
101 // Select 'value' in entry-less mode
102 if ( !GetEntry() )
103 {
104 int n = FindString(value);
105 if ( n != wxNOT_FOUND )
106 SetSelection(n);
107 }
108
109 return true;
110 }
111
112 void wxBitmapComboBox::GTKCreateComboBoxWidget()
113 {
114 GtkListStore *store;
115
116 store = gtk_list_store_new( 2, G_TYPE_OBJECT, G_TYPE_STRING );
117
118 if ( HasFlag(wxCB_READONLY) )
119 {
120 m_widget = gtk_combo_box_new_with_model( GTK_TREE_MODEL(store) );
121 }
122 else
123 {
124 m_widget = gtk_combo_box_entry_new_with_model( GTK_TREE_MODEL(store), m_stringCellIndex );
125 m_entry = GTK_ENTRY( GTK_BIN(m_widget)->child );
126 gtk_entry_set_editable( m_entry, TRUE );
127 }
128 g_object_ref(m_widget);
129
130 // This must be called as gtk_combo_box_entry_new_with_model adds
131 // automatically adds one text column.
132 gtk_cell_layout_clear( GTK_CELL_LAYOUT(m_widget) );
133
134 GtkCellRenderer* imageRenderer = gtk_cell_renderer_pixbuf_new();
135 gtk_cell_layout_pack_start( GTK_CELL_LAYOUT(m_widget),
136 imageRenderer, FALSE);
137 gtk_cell_layout_add_attribute( GTK_CELL_LAYOUT(m_widget),
138 imageRenderer, "pixbuf", 0);
139
140 GtkCellRenderer* textRenderer = gtk_cell_renderer_text_new();
141 gtk_cell_layout_pack_end( GTK_CELL_LAYOUT(m_widget),
142 textRenderer, TRUE );
143 gtk_cell_layout_add_attribute( GTK_CELL_LAYOUT(m_widget),
144 textRenderer, "text", 1);
145 }
146
147 wxBitmapComboBox::~wxBitmapComboBox()
148 {
149 }
150
151 GtkWidget* wxBitmapComboBox::GetConnectWidget()
152 {
153 if ( GetEntry() )
154 return wxComboBox::GetConnectWidget();
155
156 return wxChoice::GetConnectWidget();
157 }
158
159 GdkWindow *wxBitmapComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
160 {
161 if ( GetEntry() )
162 return wxComboBox::GTKGetWindow(windows);
163
164 return wxChoice::GTKGetWindow(windows);
165 }
166
167 // ----------------------------------------------------------------------------
168 // Item manipulation
169 // ----------------------------------------------------------------------------
170
171 void wxBitmapComboBox::SetItemBitmap(unsigned int n, const wxBitmap& bitmap)
172 {
173 if ( bitmap.IsOk() )
174 {
175 if ( m_bitmapSize.x == 0 )
176 {
177 m_bitmapSize.x = bitmap.GetWidth();
178 m_bitmapSize.y = bitmap.GetHeight();
179 }
180
181 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
182 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
183 GtkTreeIter iter;
184
185 if ( gtk_tree_model_iter_nth_child( model, &iter, NULL, n ) )
186 {
187 GValue value0 = { 0, };
188 g_value_init( &value0, G_TYPE_OBJECT );
189 g_value_set_object( &value0, bitmap.GetPixbuf() );
190 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter,
191 m_bitmapCellIndex, &value0 );
192 g_value_unset( &value0 );
193 }
194 }
195 }
196
197 wxBitmap wxBitmapComboBox::GetItemBitmap(unsigned int n) const
198 {
199 wxBitmap bitmap;
200
201 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
202 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
203 GtkTreeIter iter;
204
205 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
206 {
207 GValue value = { 0, };
208 gtk_tree_model_get_value( model, &iter,
209 m_bitmapCellIndex, &value );
210 GdkPixbuf* pixbuf = (GdkPixbuf*) g_value_get_object( &value );
211 if ( pixbuf )
212 {
213 g_object_ref( pixbuf );
214 bitmap.SetPixbuf( pixbuf );
215 }
216 g_value_unset( &value );
217 }
218
219 return bitmap;
220 }
221
222 int wxBitmapComboBox::Append(const wxString& item, const wxBitmap& bitmap)
223 {
224 const int n = wxComboBox::Append(item);
225 if ( n != wxNOT_FOUND )
226 SetItemBitmap(n, bitmap);
227 return n;
228 }
229
230 int wxBitmapComboBox::Append(const wxString& item, const wxBitmap& bitmap,
231 void *clientData)
232 {
233 const int n = wxComboBox::Append(item, clientData);
234 if ( n != wxNOT_FOUND )
235 SetItemBitmap(n, bitmap);
236 return n;
237 }
238
239 int wxBitmapComboBox::Append(const wxString& item, const wxBitmap& bitmap,
240 wxClientData *clientData)
241 {
242 const int n = wxComboBox::Append(item, clientData);
243 if ( n != wxNOT_FOUND )
244 SetItemBitmap(n, bitmap);
245 return n;
246 }
247
248 int wxBitmapComboBox::Insert(const wxString& item,
249 const wxBitmap& bitmap,
250 unsigned int pos)
251 {
252 const int n = wxComboBox::Insert(item, pos);
253 if ( n != wxNOT_FOUND )
254 SetItemBitmap(n, bitmap);
255 return n;
256 }
257
258 int wxBitmapComboBox::Insert(const wxString& item, const wxBitmap& bitmap,
259 unsigned int pos, wxClientData *clientData)
260 {
261 const int n = wxComboBox::Insert(item, pos, clientData);
262 if ( n != wxNOT_FOUND )
263 SetItemBitmap(n, bitmap);
264 return n;
265 }
266
267 void wxBitmapComboBox::GTKInsertComboBoxTextItem( unsigned int n, const wxString& text )
268 {
269 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
270 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
271 GtkListStore *store = GTK_LIST_STORE( model );
272 GtkTreeIter iter;
273
274 gtk_list_store_insert( store, &iter, n );
275
276 GValue value = { 0, };
277 g_value_init( &value, G_TYPE_STRING );
278 g_value_set_string( &value, wxGTK_CONV( text ) );
279 gtk_list_store_set_value( store, &iter, m_stringCellIndex, &value );
280 g_value_unset( &value );
281 }
282
283 // ----------------------------------------------------------------------------
284 // wxTextEntry interface override
285 // ----------------------------------------------------------------------------
286
287 void wxBitmapComboBox::WriteText(const wxString& value)
288 {
289 if ( GetEntry() )
290 wxComboBox::WriteText(value);
291 }
292
293 wxString wxBitmapComboBox::GetValue() const
294 {
295 if ( GetEntry() )
296 return wxComboBox::GetValue();
297
298 return GetStringSelection();
299 }
300
301 void wxBitmapComboBox::Remove(long from, long to)
302 {
303 if ( GetEntry() )
304 wxComboBox::Remove(from, to);
305 }
306
307 void wxBitmapComboBox::SetInsertionPoint(long pos)
308 {
309 if ( GetEntry() )
310 wxComboBox::SetInsertionPoint(pos);
311 }
312
313 long wxBitmapComboBox::GetInsertionPoint() const
314 {
315 if ( GetEntry() )
316 return wxComboBox::GetInsertionPoint();
317
318 return 0;
319 }
320 long wxBitmapComboBox::GetLastPosition() const
321 {
322 if ( GetEntry() )
323 return wxComboBox::GetLastPosition();
324
325 return 0;
326 }
327
328 void wxBitmapComboBox::SetSelection(long from, long to)
329 {
330 if ( GetEntry() )
331 wxComboBox::SetSelection(from, to);
332 }
333
334 void wxBitmapComboBox::GetSelection(long *from, long *to) const
335 {
336 if ( GetEntry() )
337 wxComboBox::GetSelection(from, to);
338 }
339
340 bool wxBitmapComboBox::IsEditable() const
341 {
342 if ( GetEntry() )
343 return wxTextEntry::IsEditable();
344
345 return false;
346 }
347
348 void wxBitmapComboBox::SetEditable(bool editable)
349 {
350 if ( GetEntry() )
351 wxComboBox::SetEditable(editable);
352 }
353
354 #endif // wxUSE_BITMAPCOMBOBOX