]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/bmpcbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/bmpcbox.cpp
3 // Purpose: wxBitmapComboBox
4 // Author: Jaakko Salli
6 // Copyright: (c) 2008 Jaakko Salli
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 #include "wx/wxprec.h"
24 #if wxUSE_BITMAPCOMBOBOX
26 #include "wx/bmpcbox.h"
33 #include "wx/gtk/private.h"
35 // ============================================================================
37 // ============================================================================
40 IMPLEMENT_DYNAMIC_CLASS(wxBitmapComboBox
, wxComboBox
)
43 // ----------------------------------------------------------------------------
44 // wxBitmapComboBox creation
45 // ----------------------------------------------------------------------------
47 void wxBitmapComboBox::Init()
49 m_bitmapCellIndex
= 0;
50 m_stringCellIndex
= 1;
51 m_bitmapSize
= wxSize(-1, -1);
54 wxBitmapComboBox::wxBitmapComboBox(wxWindow
*parent
,
56 const wxString
& value
,
59 const wxArrayString
& choices
,
61 const wxValidator
& validator
,
64 wxBitmapComboBoxBase()
68 Create(parent
,id
,value
,pos
,size
,choices
,style
,validator
,name
);
71 bool wxBitmapComboBox::Create(wxWindow
*parent
,
73 const wxString
& value
,
76 const wxArrayString
& choices
,
78 const wxValidator
& validator
,
81 wxCArrayString
chs(choices
);
82 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
83 chs
.GetStrings(), style
, validator
, name
);
86 bool wxBitmapComboBox::Create(wxWindow
*parent
,
88 const wxString
& value
,
92 const wxString choices
[],
94 const wxValidator
& validator
,
97 if ( !wxComboBox::Create(parent
, id
, value
, pos
, size
,
98 n
, choices
, style
, validator
, name
) )
101 // Select 'value' in entry-less mode
104 int i
= FindString(value
);
105 if (i
!= wxNOT_FOUND
)
112 void wxBitmapComboBox::GTKCreateComboBoxWidget()
116 store
= gtk_list_store_new( 2, G_TYPE_OBJECT
, G_TYPE_STRING
);
118 if ( HasFlag(wxCB_READONLY
) )
120 m_widget
= gtk_combo_box_new_with_model( GTK_TREE_MODEL(store
) );
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
);
128 m_widget
= gtk_combo_box_entry_new_with_model( GTK_TREE_MODEL(store
), m_stringCellIndex
);
130 m_entry
= GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget
)));
131 gtk_editable_set_editable(GTK_EDITABLE(m_entry
), true);
133 g_object_ref(m_widget
);
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
) );
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);
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);
152 wxBitmapComboBox::~wxBitmapComboBox()
156 GtkWidget
* wxBitmapComboBox::GetConnectWidget()
159 return wxComboBox::GetConnectWidget();
161 return wxChoice::GetConnectWidget();
164 GdkWindow
*wxBitmapComboBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
167 return wxComboBox::GTKGetWindow(windows
);
169 return wxChoice::GTKGetWindow(windows
);
172 wxSize
wxBitmapComboBox::DoGetBestSize() const
174 wxSize best
= wxComboBox::DoGetBestSize();
176 int delta
= GetBitmapSize().y
- GetCharHeight();
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 void wxBitmapComboBox::SetItemBitmap(unsigned int n
, const wxBitmap
& bitmap
)
193 if ( m_bitmapSize
.x
< 0 )
195 m_bitmapSize
.x
= bitmap
.GetWidth();
196 m_bitmapSize
.y
= bitmap
.GetHeight();
199 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
200 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
203 if ( gtk_tree_model_iter_nth_child( model
, &iter
, NULL
, n
) )
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
);
215 wxBitmap
wxBitmapComboBox::GetItemBitmap(unsigned int n
) const
219 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
220 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
223 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
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
);
231 g_object_ref( pixbuf
);
232 bitmap
= wxBitmap(pixbuf
);
234 g_value_unset( &value
);
240 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
)
242 const int n
= wxComboBox::Append(item
);
243 if ( n
!= wxNOT_FOUND
)
244 SetItemBitmap(n
, bitmap
);
248 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
251 const int n
= wxComboBox::Append(item
, clientData
);
252 if ( n
!= wxNOT_FOUND
)
253 SetItemBitmap(n
, bitmap
);
257 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
258 wxClientData
*clientData
)
260 const int n
= wxComboBox::Append(item
, clientData
);
261 if ( n
!= wxNOT_FOUND
)
262 SetItemBitmap(n
, bitmap
);
266 int wxBitmapComboBox::Insert(const wxString
& item
,
267 const wxBitmap
& bitmap
,
270 const int n
= wxComboBox::Insert(item
, pos
);
271 if ( n
!= wxNOT_FOUND
)
272 SetItemBitmap(n
, bitmap
);
276 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
277 unsigned int pos
, wxClientData
*clientData
)
279 const int n
= wxComboBox::Insert(item
, pos
, clientData
);
280 if ( n
!= wxNOT_FOUND
)
281 SetItemBitmap(n
, bitmap
);
285 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
286 unsigned int pos
, void *clientData
)
288 const int n
= wxComboBox::Insert(item
, pos
, clientData
);
289 if ( n
!= wxNOT_FOUND
)
290 SetItemBitmap(n
, bitmap
);
294 void wxBitmapComboBox::GTKInsertComboBoxTextItem( unsigned int n
, const wxString
& text
)
296 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
297 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
298 GtkListStore
*store
= GTK_LIST_STORE( model
);
301 gtk_list_store_insert( store
, &iter
, n
);
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
);
310 // ----------------------------------------------------------------------------
311 // wxTextEntry interface override
312 // ----------------------------------------------------------------------------
314 void wxBitmapComboBox::WriteText(const wxString
& value
)
317 wxComboBox::WriteText(value
);
319 SetStringSelection(value
);
322 wxString
wxBitmapComboBox::GetValue() const
325 return wxComboBox::GetValue();
327 return GetStringSelection();
330 void wxBitmapComboBox::Remove(long from
, long to
)
333 wxComboBox::Remove(from
, to
);
336 void wxBitmapComboBox::SetInsertionPoint(long pos
)
339 wxComboBox::SetInsertionPoint(pos
);
342 long wxBitmapComboBox::GetInsertionPoint() const
345 return wxComboBox::GetInsertionPoint();
349 long wxBitmapComboBox::GetLastPosition() const
352 return wxComboBox::GetLastPosition();
357 void wxBitmapComboBox::SetSelection(long from
, long to
)
360 wxComboBox::SetSelection(from
, to
);
363 void wxBitmapComboBox::GetSelection(long *from
, long *to
) const
366 wxComboBox::GetSelection(from
, to
);
369 bool wxBitmapComboBox::IsEditable() const
372 return wxTextEntry::IsEditable();
377 void wxBitmapComboBox::SetEditable(bool editable
)
380 wxComboBox::SetEditable(editable
);
383 #endif // wxUSE_BITMAPCOMBOBOX