]>
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 i
= FindString(value
);
106 if (i
!= 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
));
127 gtk_combo_box_set_entry_text_column(GTK_COMBO_BOX(m_widget
), m_stringCellIndex
);
129 m_widget
= gtk_combo_box_entry_new_with_model( GTK_TREE_MODEL(store
), m_stringCellIndex
);
131 m_entry
= GTK_ENTRY(gtk_bin_get_child(GTK_BIN(m_widget
)));
132 gtk_editable_set_editable(GTK_EDITABLE(m_entry
), true);
134 g_object_ref(m_widget
);
136 // This must be called as gtk_combo_box_entry_new_with_model adds
137 // automatically adds one text column.
138 gtk_cell_layout_clear( GTK_CELL_LAYOUT(m_widget
) );
140 GtkCellRenderer
* imageRenderer
= gtk_cell_renderer_pixbuf_new();
141 gtk_cell_layout_pack_start( GTK_CELL_LAYOUT(m_widget
),
142 imageRenderer
, FALSE
);
143 gtk_cell_layout_add_attribute( GTK_CELL_LAYOUT(m_widget
),
144 imageRenderer
, "pixbuf", 0);
146 GtkCellRenderer
* textRenderer
= gtk_cell_renderer_text_new();
147 gtk_cell_layout_pack_end( GTK_CELL_LAYOUT(m_widget
),
148 textRenderer
, TRUE
);
149 gtk_cell_layout_add_attribute( GTK_CELL_LAYOUT(m_widget
),
150 textRenderer
, "text", 1);
153 wxBitmapComboBox::~wxBitmapComboBox()
157 GtkWidget
* wxBitmapComboBox::GetConnectWidget()
160 return wxComboBox::GetConnectWidget();
162 return wxChoice::GetConnectWidget();
165 GdkWindow
*wxBitmapComboBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
168 return wxComboBox::GTKGetWindow(windows
);
170 return wxChoice::GTKGetWindow(windows
);
173 wxSize
wxBitmapComboBox::DoGetBestSize() const
175 wxSize best
= wxComboBox::DoGetBestSize();
177 int delta
= GetBitmapSize().y
- GetCharHeight();
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 void wxBitmapComboBox::SetItemBitmap(unsigned int n
, const wxBitmap
& bitmap
)
194 if ( m_bitmapSize
.x
< 0 )
196 m_bitmapSize
.x
= bitmap
.GetWidth();
197 m_bitmapSize
.y
= bitmap
.GetHeight();
200 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
201 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
204 if ( gtk_tree_model_iter_nth_child( model
, &iter
, NULL
, n
) )
206 GValue value0
= { 0, };
207 g_value_init( &value0
, G_TYPE_OBJECT
);
208 g_value_set_object( &value0
, bitmap
.GetPixbuf() );
209 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
,
210 m_bitmapCellIndex
, &value0
);
211 g_value_unset( &value0
);
216 wxBitmap
wxBitmapComboBox::GetItemBitmap(unsigned int n
) const
220 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
221 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
224 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
226 GValue value
= { 0, };
227 gtk_tree_model_get_value( model
, &iter
,
228 m_bitmapCellIndex
, &value
);
229 GdkPixbuf
* pixbuf
= (GdkPixbuf
*) g_value_get_object( &value
);
232 g_object_ref( pixbuf
);
233 bitmap
= wxBitmap(pixbuf
);
235 g_value_unset( &value
);
241 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
)
243 const int n
= wxComboBox::Append(item
);
244 if ( n
!= wxNOT_FOUND
)
245 SetItemBitmap(n
, bitmap
);
249 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
252 const int n
= wxComboBox::Append(item
, clientData
);
253 if ( n
!= wxNOT_FOUND
)
254 SetItemBitmap(n
, bitmap
);
258 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
259 wxClientData
*clientData
)
261 const int n
= wxComboBox::Append(item
, clientData
);
262 if ( n
!= wxNOT_FOUND
)
263 SetItemBitmap(n
, bitmap
);
267 int wxBitmapComboBox::Insert(const wxString
& item
,
268 const wxBitmap
& bitmap
,
271 const int n
= wxComboBox::Insert(item
, pos
);
272 if ( n
!= wxNOT_FOUND
)
273 SetItemBitmap(n
, bitmap
);
277 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
278 unsigned int pos
, wxClientData
*clientData
)
280 const int n
= wxComboBox::Insert(item
, pos
, clientData
);
281 if ( n
!= wxNOT_FOUND
)
282 SetItemBitmap(n
, bitmap
);
286 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
287 unsigned int pos
, void *clientData
)
289 const int n
= wxComboBox::Insert(item
, pos
, clientData
);
290 if ( n
!= wxNOT_FOUND
)
291 SetItemBitmap(n
, bitmap
);
295 void wxBitmapComboBox::GTKInsertComboBoxTextItem( unsigned int n
, const wxString
& text
)
297 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
298 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
299 GtkListStore
*store
= GTK_LIST_STORE( model
);
302 gtk_list_store_insert( store
, &iter
, n
);
304 GValue value
= { 0, };
305 g_value_init( &value
, G_TYPE_STRING
);
306 g_value_set_string( &value
, wxGTK_CONV( text
) );
307 gtk_list_store_set_value( store
, &iter
, m_stringCellIndex
, &value
);
308 g_value_unset( &value
);
311 // ----------------------------------------------------------------------------
312 // wxTextEntry interface override
313 // ----------------------------------------------------------------------------
315 void wxBitmapComboBox::WriteText(const wxString
& value
)
318 wxComboBox::WriteText(value
);
320 SetStringSelection(value
);
323 wxString
wxBitmapComboBox::GetValue() const
326 return wxComboBox::GetValue();
328 return GetStringSelection();
331 void wxBitmapComboBox::Remove(long from
, long to
)
334 wxComboBox::Remove(from
, to
);
337 void wxBitmapComboBox::SetInsertionPoint(long pos
)
340 wxComboBox::SetInsertionPoint(pos
);
343 long wxBitmapComboBox::GetInsertionPoint() const
346 return wxComboBox::GetInsertionPoint();
350 long wxBitmapComboBox::GetLastPosition() const
353 return wxComboBox::GetLastPosition();
358 void wxBitmapComboBox::SetSelection(long from
, long to
)
361 wxComboBox::SetSelection(from
, to
);
364 void wxBitmapComboBox::GetSelection(long *from
, long *to
) const
367 wxComboBox::GetSelection(from
, to
);
370 bool wxBitmapComboBox::IsEditable() const
373 return wxTextEntry::IsEditable();
378 void wxBitmapComboBox::SetEditable(bool editable
)
381 wxComboBox::SetEditable(editable
);
384 #endif // wxUSE_BITMAPCOMBOBOX