]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/bmpcbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/bmpcboxg.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"
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 n
= FindString(value
);
105 if ( n
!= 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
) );
124 m_widget
= gtk_combo_box_entry_new_with_model( GTK_TREE_MODEL(store
), m_stringCellIndex
);
125 m_entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
126 gtk_entry_set_editable( m_entry
, TRUE
);
128 g_object_ref(m_widget
);
130 // This must be called as gtk_combo_box_entry_new_with_model adds
131 // automatically adds one text column.
132 gtk_cell_layout_clear( GTK_CELL_LAYOUT(m_widget
) );
134 GtkCellRenderer
* imageRenderer
= gtk_cell_renderer_pixbuf_new();
135 gtk_cell_layout_pack_start( GTK_CELL_LAYOUT(m_widget
),
136 imageRenderer
, FALSE
);
137 gtk_cell_layout_add_attribute( GTK_CELL_LAYOUT(m_widget
),
138 imageRenderer
, "pixbuf", 0);
140 GtkCellRenderer
* textRenderer
= gtk_cell_renderer_text_new();
141 gtk_cell_layout_pack_end( GTK_CELL_LAYOUT(m_widget
),
142 textRenderer
, TRUE
);
143 gtk_cell_layout_add_attribute( GTK_CELL_LAYOUT(m_widget
),
144 textRenderer
, "text", 1);
147 wxBitmapComboBox::~wxBitmapComboBox()
151 GtkWidget
* wxBitmapComboBox::GetConnectWidget()
154 return wxComboBox::GetConnectWidget();
156 return wxChoice::GetConnectWidget();
159 GdkWindow
*wxBitmapComboBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
162 return wxComboBox::GTKGetWindow(windows
);
164 return wxChoice::GTKGetWindow(windows
);
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 void wxBitmapComboBox::SetItemBitmap(unsigned int n
, const wxBitmap
& bitmap
)
175 if ( m_bitmapSize
.x
< 0 )
177 m_bitmapSize
.x
= bitmap
.GetWidth();
178 m_bitmapSize
.y
= bitmap
.GetHeight();
181 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
182 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
185 if ( gtk_tree_model_iter_nth_child( model
, &iter
, NULL
, n
) )
187 GValue value0
= { 0, };
188 g_value_init( &value0
, G_TYPE_OBJECT
);
189 g_value_set_object( &value0
, bitmap
.GetPixbuf() );
190 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
,
191 m_bitmapCellIndex
, &value0
);
192 g_value_unset( &value0
);
197 wxBitmap
wxBitmapComboBox::GetItemBitmap(unsigned int n
) const
201 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
202 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
205 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
207 GValue value
= { 0, };
208 gtk_tree_model_get_value( model
, &iter
,
209 m_bitmapCellIndex
, &value
);
210 GdkPixbuf
* pixbuf
= (GdkPixbuf
*) g_value_get_object( &value
);
213 g_object_ref( pixbuf
);
214 bitmap
.SetPixbuf( pixbuf
);
216 g_value_unset( &value
);
222 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
)
224 const int n
= wxComboBox::Append(item
);
225 if ( n
!= wxNOT_FOUND
)
226 SetItemBitmap(n
, bitmap
);
230 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
233 const int n
= wxComboBox::Append(item
, clientData
);
234 if ( n
!= wxNOT_FOUND
)
235 SetItemBitmap(n
, bitmap
);
239 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
240 wxClientData
*clientData
)
242 const int n
= wxComboBox::Append(item
, clientData
);
243 if ( n
!= wxNOT_FOUND
)
244 SetItemBitmap(n
, bitmap
);
248 int wxBitmapComboBox::Insert(const wxString
& item
,
249 const wxBitmap
& bitmap
,
252 const int n
= wxComboBox::Insert(item
, pos
);
253 if ( n
!= wxNOT_FOUND
)
254 SetItemBitmap(n
, bitmap
);
258 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
259 unsigned int pos
, wxClientData
*clientData
)
261 const int n
= wxComboBox::Insert(item
, pos
, clientData
);
262 if ( n
!= wxNOT_FOUND
)
263 SetItemBitmap(n
, bitmap
);
267 void wxBitmapComboBox::GTKInsertComboBoxTextItem( unsigned int n
, const wxString
& text
)
269 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
270 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
271 GtkListStore
*store
= GTK_LIST_STORE( model
);
274 gtk_list_store_insert( store
, &iter
, n
);
276 GValue value
= { 0, };
277 g_value_init( &value
, G_TYPE_STRING
);
278 g_value_set_string( &value
, wxGTK_CONV( text
) );
279 gtk_list_store_set_value( store
, &iter
, m_stringCellIndex
, &value
);
280 g_value_unset( &value
);
283 // ----------------------------------------------------------------------------
284 // wxTextEntry interface override
285 // ----------------------------------------------------------------------------
287 void wxBitmapComboBox::WriteText(const wxString
& value
)
290 wxComboBox::WriteText(value
);
292 SetStringSelection(value
);
295 wxString
wxBitmapComboBox::GetValue() const
298 return wxComboBox::GetValue();
300 return GetStringSelection();
303 void wxBitmapComboBox::Remove(long from
, long to
)
306 wxComboBox::Remove(from
, to
);
309 void wxBitmapComboBox::SetInsertionPoint(long pos
)
312 wxComboBox::SetInsertionPoint(pos
);
315 long wxBitmapComboBox::GetInsertionPoint() const
318 return wxComboBox::GetInsertionPoint();
322 long wxBitmapComboBox::GetLastPosition() const
325 return wxComboBox::GetLastPosition();
330 void wxBitmapComboBox::SetSelection(long from
, long to
)
333 wxComboBox::SetSelection(from
, to
);
336 void wxBitmapComboBox::GetSelection(long *from
, long *to
) const
339 wxComboBox::GetSelection(from
, to
);
342 bool wxBitmapComboBox::IsEditable() const
345 return wxTextEntry::IsEditable();
350 void wxBitmapComboBox::SetEditable(bool editable
)
353 wxComboBox::SetEditable(editable
);
356 #endif // wxUSE_BITMAPCOMBOBOX