]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bmpcbox.cpp
Set missing Language: headers in PO files.
[wxWidgets.git] / src / gtk / bmpcbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/bmpcbox.cpp
3 // Purpose: wxBitmapComboBox
4 // Author: Jaakko Salli
5 // Created: 2008-05-19
6 // Copyright: (c) 2008 Jaakko Salli
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ============================================================================
11 // declarations
12 // ============================================================================
13
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #if wxUSE_BITMAPCOMBOBOX
25
26 #include "wx/bmpcbox.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/log.h"
30 #endif
31
32 #include <gtk/gtk.h>
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(-1, -1);
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 i = FindString(value);
105 if (i != wxNOT_FOUND)
106 SetSelection(i);
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 #ifdef __WXGTK3__
125 m_widget = gtk_combo_box_new_with_model_and_entry(GTK_TREE_MODEL(store));
126 gtk_combo_box_set_entry_text_column(GTK_COMBO_BOX(m_widget), m_stringCellIndex);
127 #else
128 m_widget = gtk_combo_box_entry_new_with_model( GTK_TREE_MODEL(store), m_stringCellIndex );
129 #endif
130 m_entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget)));
131 gtk_editable_set_editable(GTK_EDITABLE(m_entry), true);
132 }
133 g_object_ref(m_widget);
134
135 // This must be called as gtk_combo_box_entry_new_with_model adds
136 // automatically adds one text column.
137 gtk_cell_layout_clear( GTK_CELL_LAYOUT(m_widget) );
138
139 GtkCellRenderer* imageRenderer = gtk_cell_renderer_pixbuf_new();
140 gtk_cell_layout_pack_start( GTK_CELL_LAYOUT(m_widget),
141 imageRenderer, FALSE);
142 gtk_cell_layout_add_attribute( GTK_CELL_LAYOUT(m_widget),
143 imageRenderer, "pixbuf", 0);
144
145 GtkCellRenderer* textRenderer = gtk_cell_renderer_text_new();
146 gtk_cell_layout_pack_end( GTK_CELL_LAYOUT(m_widget),
147 textRenderer, TRUE );
148 gtk_cell_layout_add_attribute( GTK_CELL_LAYOUT(m_widget),
149 textRenderer, "text", 1);
150 }
151
152 wxBitmapComboBox::~wxBitmapComboBox()
153 {
154 }
155
156 GtkWidget* wxBitmapComboBox::GetConnectWidget()
157 {
158 if ( GetEntry() )
159 return wxComboBox::GetConnectWidget();
160
161 return wxChoice::GetConnectWidget();
162 }
163
164 GdkWindow *wxBitmapComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
165 {
166 if ( GetEntry() )
167 return wxComboBox::GTKGetWindow(windows);
168
169 return wxChoice::GTKGetWindow(windows);
170 }
171
172 wxSize wxBitmapComboBox::DoGetBestSize() const
173 {
174 wxSize best = wxComboBox::DoGetBestSize();
175
176 int delta = GetBitmapSize().y - GetCharHeight();
177 if ( delta > 0 )
178 {
179 best.y += delta;
180 CacheBestSize(best);
181 }
182 return best;
183 }
184
185 // ----------------------------------------------------------------------------
186 // Item manipulation
187 // ----------------------------------------------------------------------------
188
189 void wxBitmapComboBox::SetItemBitmap(unsigned int n, const wxBitmap& bitmap)
190 {
191 if ( bitmap.IsOk() )
192 {
193 if ( m_bitmapSize.x < 0 )
194 {
195 m_bitmapSize.x = bitmap.GetWidth();
196 m_bitmapSize.y = bitmap.GetHeight();
197 }
198
199 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
200 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
201 GtkTreeIter iter;
202
203 if ( gtk_tree_model_iter_nth_child( model, &iter, NULL, n ) )
204 {
205 GValue value0 = { 0, };
206 g_value_init( &value0, G_TYPE_OBJECT );
207 g_value_set_object( &value0, bitmap.GetPixbuf() );
208 gtk_list_store_set_value( GTK_LIST_STORE(model), &iter,
209 m_bitmapCellIndex, &value0 );
210 g_value_unset( &value0 );
211 }
212 }
213 }
214
215 wxBitmap wxBitmapComboBox::GetItemBitmap(unsigned int n) const
216 {
217 wxBitmap bitmap;
218
219 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
220 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
221 GtkTreeIter iter;
222
223 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
224 {
225 GValue value = { 0, };
226 gtk_tree_model_get_value( model, &iter,
227 m_bitmapCellIndex, &value );
228 GdkPixbuf* pixbuf = (GdkPixbuf*) g_value_get_object( &value );
229 if ( pixbuf )
230 {
231 g_object_ref( pixbuf );
232 bitmap = wxBitmap(pixbuf);
233 }
234 g_value_unset( &value );
235 }
236
237 return bitmap;
238 }
239
240 int wxBitmapComboBox::Append(const wxString& item, const wxBitmap& bitmap)
241 {
242 const int n = wxComboBox::Append(item);
243 if ( n != wxNOT_FOUND )
244 SetItemBitmap(n, bitmap);
245 return n;
246 }
247
248 int wxBitmapComboBox::Append(const wxString& item, const wxBitmap& bitmap,
249 void *clientData)
250 {
251 const int n = wxComboBox::Append(item, clientData);
252 if ( n != wxNOT_FOUND )
253 SetItemBitmap(n, bitmap);
254 return n;
255 }
256
257 int wxBitmapComboBox::Append(const wxString& item, const wxBitmap& bitmap,
258 wxClientData *clientData)
259 {
260 const int n = wxComboBox::Append(item, clientData);
261 if ( n != wxNOT_FOUND )
262 SetItemBitmap(n, bitmap);
263 return n;
264 }
265
266 int wxBitmapComboBox::Insert(const wxString& item,
267 const wxBitmap& bitmap,
268 unsigned int pos)
269 {
270 const int n = wxComboBox::Insert(item, pos);
271 if ( n != wxNOT_FOUND )
272 SetItemBitmap(n, bitmap);
273 return n;
274 }
275
276 int wxBitmapComboBox::Insert(const wxString& item, const wxBitmap& bitmap,
277 unsigned int pos, wxClientData *clientData)
278 {
279 const int n = wxComboBox::Insert(item, pos, clientData);
280 if ( n != wxNOT_FOUND )
281 SetItemBitmap(n, bitmap);
282 return n;
283 }
284
285 int wxBitmapComboBox::Insert(const wxString& item, const wxBitmap& bitmap,
286 unsigned int pos, void *clientData)
287 {
288 const int n = wxComboBox::Insert(item, pos, clientData);
289 if ( n != wxNOT_FOUND )
290 SetItemBitmap(n, bitmap);
291 return n;
292 }
293
294 void wxBitmapComboBox::GTKInsertComboBoxTextItem( unsigned int n, const wxString& text )
295 {
296 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
297 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
298 GtkListStore *store = GTK_LIST_STORE( model );
299 GtkTreeIter iter;
300
301 gtk_list_store_insert( store, &iter, n );
302
303 GValue value = { 0, };
304 g_value_init( &value, G_TYPE_STRING );
305 g_value_set_string( &value, wxGTK_CONV( text ) );
306 gtk_list_store_set_value( store, &iter, m_stringCellIndex, &value );
307 g_value_unset( &value );
308 }
309
310 // ----------------------------------------------------------------------------
311 // wxTextEntry interface override
312 // ----------------------------------------------------------------------------
313
314 void wxBitmapComboBox::WriteText(const wxString& value)
315 {
316 if ( GetEntry() )
317 wxComboBox::WriteText(value);
318 else
319 SetStringSelection(value);
320 }
321
322 wxString wxBitmapComboBox::GetValue() const
323 {
324 if ( GetEntry() )
325 return wxComboBox::GetValue();
326
327 return GetStringSelection();
328 }
329
330 void wxBitmapComboBox::Remove(long from, long to)
331 {
332 if ( GetEntry() )
333 wxComboBox::Remove(from, to);
334 }
335
336 void wxBitmapComboBox::SetInsertionPoint(long pos)
337 {
338 if ( GetEntry() )
339 wxComboBox::SetInsertionPoint(pos);
340 }
341
342 long wxBitmapComboBox::GetInsertionPoint() const
343 {
344 if ( GetEntry() )
345 return wxComboBox::GetInsertionPoint();
346
347 return 0;
348 }
349 long wxBitmapComboBox::GetLastPosition() const
350 {
351 if ( GetEntry() )
352 return wxComboBox::GetLastPosition();
353
354 return 0;
355 }
356
357 void wxBitmapComboBox::SetSelection(long from, long to)
358 {
359 if ( GetEntry() )
360 wxComboBox::SetSelection(from, to);
361 }
362
363 void wxBitmapComboBox::GetSelection(long *from, long *to) const
364 {
365 if ( GetEntry() )
366 wxComboBox::GetSelection(from, to);
367 }
368
369 bool wxBitmapComboBox::IsEditable() const
370 {
371 if ( GetEntry() )
372 return wxTextEntry::IsEditable();
373
374 return false;
375 }
376
377 void wxBitmapComboBox::SetEditable(bool editable)
378 {
379 if ( GetEntry() )
380 wxComboBox::SetEditable(editable);
381 }
382
383 #endif // wxUSE_BITMAPCOMBOBOX