]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bmpcbox.cpp
new file added
[wxWidgets.git] / src / gtk / bmpcbox.cpp
CommitLineData
e78c1d78 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/gtk/bmpcbox.cpp
e78c1d78
RR
3// Purpose: wxBitmapComboBox
4// Author: Jaakko Salli
5// Created: 2008-05-19
2903e699 6// RCS-ID: $Id$
e78c1d78
RR
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
9dc44eff 33#include <gtk/gtk.h>
e78c1d78
RR
34#include "wx/gtk/private.h"
35
e78c1d78
RR
36// ============================================================================
37// implementation
38// ============================================================================
39
40
41IMPLEMENT_DYNAMIC_CLASS(wxBitmapComboBox, wxComboBox)
42
43
44// ----------------------------------------------------------------------------
45// wxBitmapComboBox creation
46// ----------------------------------------------------------------------------
47
48void wxBitmapComboBox::Init()
49{
50 m_bitmapCellIndex = 0;
51 m_stringCellIndex = 1;
60b71826 52 m_bitmapSize = wxSize(-1, -1);
e78c1d78
RR
53}
54
55wxBitmapComboBox::wxBitmapComboBox(wxWindow *parent,
56 wxWindowID id,
57 const wxString& value,
58 const wxPoint& pos,
59 const wxSize& size,
60 const wxArrayString& choices,
61 long style,
62 const wxValidator& validator,
63 const wxString& name)
64 : wxComboBox(),
65 wxBitmapComboBoxBase()
66{
67 Init();
68
69 Create(parent,id,value,pos,size,choices,style,validator,name);
70}
71
72bool wxBitmapComboBox::Create(wxWindow *parent,
73 wxWindowID id,
74 const wxString& value,
75 const wxPoint& pos,
76 const wxSize& size,
77 const wxArrayString& choices,
78 long style,
79 const wxValidator& validator,
80 const wxString& name)
81{
82 wxCArrayString chs(choices);
83 return Create(parent, id, value, pos, size, chs.GetCount(),
84 chs.GetStrings(), style, validator, name);
85}
86
87bool wxBitmapComboBox::Create(wxWindow *parent,
88 wxWindowID id,
89 const wxString& value,
90 const wxPoint& pos,
91 const wxSize& size,
92 int n,
93 const wxString choices[],
94 long style,
95 const wxValidator& validator,
96 const wxString& name)
97{
98 if ( !wxComboBox::Create(parent, id, value, pos, size,
99 n, choices, style, validator, name) )
100 return false;
101
102 // Select 'value' in entry-less mode
103 if ( !GetEntry() )
104 {
105 int n = FindString(value);
106 if ( n != wxNOT_FOUND )
107 SetSelection(n);
108 }
109
110 return true;
111}
112
113void wxBitmapComboBox::GTKCreateComboBoxWidget()
114{
115 GtkListStore *store;
116
117 store = gtk_list_store_new( 2, G_TYPE_OBJECT, G_TYPE_STRING );
118
119 if ( HasFlag(wxCB_READONLY) )
120 {
121 m_widget = gtk_combo_box_new_with_model( GTK_TREE_MODEL(store) );
122 }
123 else
124 {
9dc44eff
PC
125#ifdef __WXGTK3__
126 m_widget = gtk_combo_box_new_with_model_and_entry(GTK_TREE_MODEL(store));
127#else
e78c1d78 128 m_widget = gtk_combo_box_entry_new_with_model( GTK_TREE_MODEL(store), m_stringCellIndex );
9dc44eff 129#endif
385e8575
PC
130 m_entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget)));
131 gtk_editable_set_editable(GTK_EDITABLE(m_entry), true);
e78c1d78 132 }
9ff9d30c 133 g_object_ref(m_widget);
e78c1d78
RR
134
135 // This must be called as gtk_combo_box_entry_new_with_model adds
136 // automatically adds one text column.
60b71826 137 gtk_cell_layout_clear( GTK_CELL_LAYOUT(m_widget) );
e78c1d78
RR
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
152wxBitmapComboBox::~wxBitmapComboBox()
153{
154}
155
156GtkWidget* wxBitmapComboBox::GetConnectWidget()
157{
158 if ( GetEntry() )
159 return wxComboBox::GetConnectWidget();
160
161 return wxChoice::GetConnectWidget();
162}
163
164GdkWindow *wxBitmapComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
165{
166 if ( GetEntry() )
167 return wxComboBox::GTKGetWindow(windows);
60b71826 168
e78c1d78
RR
169 return wxChoice::GTKGetWindow(windows);
170}
171
c90e98dc
JS
172wxSize 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
e78c1d78
RR
185// ----------------------------------------------------------------------------
186// Item manipulation
187// ----------------------------------------------------------------------------
188
189void wxBitmapComboBox::SetItemBitmap(unsigned int n, const wxBitmap& bitmap)
190{
191 if ( bitmap.IsOk() )
192 {
60b71826 193 if ( m_bitmapSize.x < 0 )
e78c1d78
RR
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
215wxBitmap 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 );
54195d23 232 bitmap = wxBitmap(pixbuf);
e78c1d78
RR
233 }
234 g_value_unset( &value );
235 }
236
237 return bitmap;
238}
239
240int 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
248int 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
257int 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
266int 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
276int 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
90fae9d2
JS
285int 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
e78c1d78
RR
294void 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
314void wxBitmapComboBox::WriteText(const wxString& value)
315{
316 if ( GetEntry() )
317 wxComboBox::WriteText(value);
268203fb
JS
318 else
319 SetStringSelection(value);
e78c1d78
RR
320}
321
322wxString wxBitmapComboBox::GetValue() const
323{
324 if ( GetEntry() )
325 return wxComboBox::GetValue();
326
327 return GetStringSelection();
328}
329
330void wxBitmapComboBox::Remove(long from, long to)
331{
332 if ( GetEntry() )
333 wxComboBox::Remove(from, to);
334}
335
336void wxBitmapComboBox::SetInsertionPoint(long pos)
337{
338 if ( GetEntry() )
339 wxComboBox::SetInsertionPoint(pos);
340}
341
342long wxBitmapComboBox::GetInsertionPoint() const
343{
344 if ( GetEntry() )
345 return wxComboBox::GetInsertionPoint();
346
347 return 0;
348 }
349long wxBitmapComboBox::GetLastPosition() const
350{
351 if ( GetEntry() )
352 return wxComboBox::GetLastPosition();
353
354 return 0;
355 }
356
357void wxBitmapComboBox::SetSelection(long from, long to)
358{
359 if ( GetEntry() )
360 wxComboBox::SetSelection(from, to);
361}
362
363void wxBitmapComboBox::GetSelection(long *from, long *to) const
364{
365 if ( GetEntry() )
366 wxComboBox::GetSelection(from, to);
367}
368
369bool wxBitmapComboBox::IsEditable() const
370{
371 if ( GetEntry() )
372 return wxTextEntry::IsEditable();
373
374 return false;
375}
376
377void wxBitmapComboBox::SetEditable(bool editable)
378{
379 if ( GetEntry() )
380 wxComboBox::SetEditable(editable);
381}
382
383#endif // wxUSE_BITMAPCOMBOBOX