]>
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
7 // Copyright: (c) 2008 Jaakko Salli
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
25 #if wxUSE_BITMAPCOMBOBOX
27 #include "wx/bmpcbox.h"
34 #include "wx/gtk/private.h"
36 // ============================================================================
38 // ============================================================================
41 IMPLEMENT_DYNAMIC_CLASS(wxBitmapComboBox
, wxComboBox
)
44 // ----------------------------------------------------------------------------
45 // wxBitmapComboBox creation
46 // ----------------------------------------------------------------------------
48 void wxBitmapComboBox::Init()
50 m_bitmapCellIndex
= 0;
51 m_stringCellIndex
= 1;
52 m_bitmapSize
= wxSize(-1, -1);
55 wxBitmapComboBox::wxBitmapComboBox(wxWindow
*parent
,
57 const wxString
& value
,
60 const wxArrayString
& choices
,
62 const wxValidator
& validator
,
65 wxBitmapComboBoxBase()
69 Create(parent
,id
,value
,pos
,size
,choices
,style
,validator
,name
);
72 bool wxBitmapComboBox::Create(wxWindow
*parent
,
74 const wxString
& value
,
77 const wxArrayString
& choices
,
79 const wxValidator
& validator
,
82 wxCArrayString
chs(choices
);
83 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
84 chs
.GetStrings(), style
, validator
, name
);
87 bool wxBitmapComboBox::Create(wxWindow
*parent
,
89 const wxString
& value
,
93 const wxString choices
[],
95 const wxValidator
& validator
,
98 if ( !wxComboBox::Create(parent
, id
, value
, pos
, size
,
99 n
, choices
, style
, validator
, name
) )
102 // Select 'value' in entry-less mode
105 int n
= FindString(value
);
106 if ( n
!= wxNOT_FOUND
)
113 void wxBitmapComboBox::GTKCreateComboBoxWidget()
117 store
= gtk_list_store_new( 2, G_TYPE_OBJECT
, G_TYPE_STRING
);
119 if ( HasFlag(wxCB_READONLY
) )
121 m_widget
= gtk_combo_box_new_with_model( GTK_TREE_MODEL(store
) );
126 m_widget
= gtk_combo_box_new_with_model_and_entry(GTK_TREE_MODEL(store
));
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