]>
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"
36 #include "wx/gtk/win_gtk.h"
38 #include <gobject/gvaluecollector.h>
39 #include <gtk/gtktreemodel.h>
43 // ============================================================================
45 // ============================================================================
48 IMPLEMENT_DYNAMIC_CLASS(wxBitmapComboBox
, wxComboBox
)
51 // ----------------------------------------------------------------------------
52 // wxBitmapComboBox creation
53 // ----------------------------------------------------------------------------
55 void wxBitmapComboBox::Init()
57 m_bitmapCellIndex
= 0;
58 m_stringCellIndex
= 1;
59 m_bitmapSize
= wxSize(0, 0);
62 wxBitmapComboBox::wxBitmapComboBox(wxWindow
*parent
,
64 const wxString
& value
,
67 const wxArrayString
& choices
,
69 const wxValidator
& validator
,
72 wxBitmapComboBoxBase()
76 Create(parent
,id
,value
,pos
,size
,choices
,style
,validator
,name
);
79 bool wxBitmapComboBox::Create(wxWindow
*parent
,
81 const wxString
& value
,
84 const wxArrayString
& choices
,
86 const wxValidator
& validator
,
89 wxCArrayString
chs(choices
);
90 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
91 chs
.GetStrings(), style
, validator
, name
);
94 bool wxBitmapComboBox::Create(wxWindow
*parent
,
96 const wxString
& value
,
100 const wxString choices
[],
102 const wxValidator
& validator
,
103 const wxString
& name
)
105 if ( !wxComboBox::Create(parent
, id
, value
, pos
, size
,
106 n
, choices
, style
, validator
, name
) )
109 // Select 'value' in entry-less mode
112 int n
= FindString(value
);
113 if ( n
!= wxNOT_FOUND
)
120 void wxBitmapComboBox::GTKCreateComboBoxWidget()
124 store
= gtk_list_store_new( 2, G_TYPE_OBJECT
, G_TYPE_STRING
);
126 if ( HasFlag(wxCB_READONLY
) )
128 m_widget
= gtk_combo_box_new_with_model( GTK_TREE_MODEL(store
) );
132 m_widget
= gtk_combo_box_entry_new_with_model( GTK_TREE_MODEL(store
), m_stringCellIndex
);
133 m_entry
= GTK_ENTRY( GTK_BIN(m_widget
)->child
);
134 gtk_entry_set_editable( m_entry
, TRUE
);
137 // This must be called as gtk_combo_box_entry_new_with_model adds
138 // automatically adds one text column.
139 gtk_cell_layout_clear( GTK_CELL_LAYOUT(m_widget
) );
141 GtkCellRenderer
* imageRenderer
= gtk_cell_renderer_pixbuf_new();
142 gtk_cell_layout_pack_start( GTK_CELL_LAYOUT(m_widget
),
143 imageRenderer
, FALSE
);
144 gtk_cell_layout_add_attribute( GTK_CELL_LAYOUT(m_widget
),
145 imageRenderer
, "pixbuf", 0);
147 GtkCellRenderer
* textRenderer
= gtk_cell_renderer_text_new();
148 gtk_cell_layout_pack_end( GTK_CELL_LAYOUT(m_widget
),
149 textRenderer
, TRUE
);
150 gtk_cell_layout_add_attribute( GTK_CELL_LAYOUT(m_widget
),
151 textRenderer
, "text", 1);
154 wxBitmapComboBox::~wxBitmapComboBox()
158 GtkWidget
* wxBitmapComboBox::GetConnectWidget()
161 return wxComboBox::GetConnectWidget();
163 return wxChoice::GetConnectWidget();
166 GdkWindow
*wxBitmapComboBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
169 return wxComboBox::GTKGetWindow(windows
);
171 return wxChoice::GTKGetWindow(windows
);
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 void wxBitmapComboBox::SetItemBitmap(unsigned int n
, const wxBitmap
& bitmap
)
182 if ( m_bitmapSize
.x
== 0 )
184 m_bitmapSize
.x
= bitmap
.GetWidth();
185 m_bitmapSize
.y
= bitmap
.GetHeight();
188 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
189 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
192 if ( gtk_tree_model_iter_nth_child( model
, &iter
, NULL
, n
) )
194 GValue value0
= { 0, };
195 g_value_init( &value0
, G_TYPE_OBJECT
);
196 g_value_set_object( &value0
, bitmap
.GetPixbuf() );
197 gtk_list_store_set_value( GTK_LIST_STORE(model
), &iter
,
198 m_bitmapCellIndex
, &value0
);
199 g_value_unset( &value0
);
204 wxBitmap
wxBitmapComboBox::GetItemBitmap(unsigned int n
) const
208 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
209 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
212 if (gtk_tree_model_iter_nth_child (model
, &iter
, NULL
, n
))
214 GValue value
= { 0, };
215 gtk_tree_model_get_value( model
, &iter
,
216 m_bitmapCellIndex
, &value
);
217 GdkPixbuf
* pixbuf
= (GdkPixbuf
*) g_value_get_object( &value
);
220 g_object_ref( pixbuf
);
221 bitmap
.SetPixbuf( pixbuf
);
223 g_value_unset( &value
);
229 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
)
231 const int n
= wxComboBox::Append(item
);
232 if ( n
!= wxNOT_FOUND
)
233 SetItemBitmap(n
, bitmap
);
237 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
240 const int n
= wxComboBox::Append(item
, clientData
);
241 if ( n
!= wxNOT_FOUND
)
242 SetItemBitmap(n
, bitmap
);
246 int wxBitmapComboBox::Append(const wxString
& item
, const wxBitmap
& bitmap
,
247 wxClientData
*clientData
)
249 const int n
= wxComboBox::Append(item
, clientData
);
250 if ( n
!= wxNOT_FOUND
)
251 SetItemBitmap(n
, bitmap
);
255 int wxBitmapComboBox::Insert(const wxString
& item
,
256 const wxBitmap
& bitmap
,
259 const int n
= wxComboBox::Insert(item
, pos
);
260 if ( n
!= wxNOT_FOUND
)
261 SetItemBitmap(n
, bitmap
);
265 int wxBitmapComboBox::Insert(const wxString
& item
, const wxBitmap
& bitmap
,
266 unsigned int pos
, wxClientData
*clientData
)
268 const int n
= wxComboBox::Insert(item
, pos
, clientData
);
269 if ( n
!= wxNOT_FOUND
)
270 SetItemBitmap(n
, bitmap
);
274 void wxBitmapComboBox::GTKInsertComboBoxTextItem( unsigned int n
, const wxString
& text
)
276 GtkComboBox
* combobox
= GTK_COMBO_BOX( m_widget
);
277 GtkTreeModel
*model
= gtk_combo_box_get_model( combobox
);
278 GtkListStore
*store
= GTK_LIST_STORE( model
);
281 gtk_list_store_insert( store
, &iter
, n
);
283 GValue value
= { 0, };
284 g_value_init( &value
, G_TYPE_STRING
);
285 g_value_set_string( &value
, wxGTK_CONV( text
) );
286 gtk_list_store_set_value( store
, &iter
, m_stringCellIndex
, &value
);
287 g_value_unset( &value
);
290 // ----------------------------------------------------------------------------
291 // wxTextEntry interface override
292 // ----------------------------------------------------------------------------
294 void wxBitmapComboBox::WriteText(const wxString
& value
)
297 wxComboBox::WriteText(value
);
300 wxString
wxBitmapComboBox::GetValue() const
303 return wxComboBox::GetValue();
305 return GetStringSelection();
308 void wxBitmapComboBox::Remove(long from
, long to
)
311 wxComboBox::Remove(from
, to
);
314 void wxBitmapComboBox::SetInsertionPoint(long pos
)
317 wxComboBox::SetInsertionPoint(pos
);
320 long wxBitmapComboBox::GetInsertionPoint() const
323 return wxComboBox::GetInsertionPoint();
327 long wxBitmapComboBox::GetLastPosition() const
330 return wxComboBox::GetLastPosition();
335 void wxBitmapComboBox::SetSelection(long from
, long to
)
338 wxComboBox::SetSelection(from
, to
);
341 void wxBitmapComboBox::GetSelection(long *from
, long *to
) const
344 wxComboBox::GetSelection(from
, to
);
347 bool wxBitmapComboBox::IsEditable() const
350 return wxTextEntry::IsEditable();
355 void wxBitmapComboBox::SetEditable(bool editable
)
358 wxComboBox::SetEditable(editable
);
361 #endif // wxUSE_BITMAPCOMBOBOX