]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bmpcbox.cpp
derive wxSTC from wxTextEntryBase to provide even more wxTextCtrl-like methods (see...
[wxWidgets.git] / src / gtk / bmpcbox.cpp
CommitLineData
e78c1d78
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/bmpcboxg.cpp
3// Purpose: wxBitmapComboBox
4// Author: Jaakko Salli
5// Created: 2008-05-19
6// RCS-ID: $Id:
7// Copyright: (c) 2008 Jaakko Salli
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19#include "wx/wxprec.h"
20
21#ifdef __BORLANDC__
22 #pragma hdrstop
23#endif
24
25#if wxUSE_BITMAPCOMBOBOX
26
27#include "wx/bmpcbox.h"
28
29#ifndef WX_PRECOMP
30 #include "wx/log.h"
31#endif
32
33#include "wx/gtk/private.h"
34
35#ifdef __WXGTK24__
36 #include "wx/gtk/win_gtk.h"
37
38 #include <gobject/gvaluecollector.h>
39 #include <gtk/gtktreemodel.h>
40#endif
41
42
43// ============================================================================
44// implementation
45// ============================================================================
46
47
48IMPLEMENT_DYNAMIC_CLASS(wxBitmapComboBox, wxComboBox)
49
50
51// ----------------------------------------------------------------------------
52// wxBitmapComboBox creation
53// ----------------------------------------------------------------------------
54
55void wxBitmapComboBox::Init()
56{
57 m_bitmapCellIndex = 0;
58 m_stringCellIndex = 1;
59 m_bitmapSize = wxSize(0, 0);
60}
61
62wxBitmapComboBox::wxBitmapComboBox(wxWindow *parent,
63 wxWindowID id,
64 const wxString& value,
65 const wxPoint& pos,
66 const wxSize& size,
67 const wxArrayString& choices,
68 long style,
69 const wxValidator& validator,
70 const wxString& name)
71 : wxComboBox(),
72 wxBitmapComboBoxBase()
73{
74 Init();
75
76 Create(parent,id,value,pos,size,choices,style,validator,name);
77}
78
79bool wxBitmapComboBox::Create(wxWindow *parent,
80 wxWindowID id,
81 const wxString& value,
82 const wxPoint& pos,
83 const wxSize& size,
84 const wxArrayString& choices,
85 long style,
86 const wxValidator& validator,
87 const wxString& name)
88{
89 wxCArrayString chs(choices);
90 return Create(parent, id, value, pos, size, chs.GetCount(),
91 chs.GetStrings(), style, validator, name);
92}
93
94bool wxBitmapComboBox::Create(wxWindow *parent,
95 wxWindowID id,
96 const wxString& value,
97 const wxPoint& pos,
98 const wxSize& size,
99 int n,
100 const wxString choices[],
101 long style,
102 const wxValidator& validator,
103 const wxString& name)
104{
105 if ( !wxComboBox::Create(parent, id, value, pos, size,
106 n, choices, style, validator, name) )
107 return false;
108
109 // Select 'value' in entry-less mode
110 if ( !GetEntry() )
111 {
112 int n = FindString(value);
113 if ( n != wxNOT_FOUND )
114 SetSelection(n);
115 }
116
117 return true;
118}
119
120void wxBitmapComboBox::GTKCreateComboBoxWidget()
121{
122 GtkListStore *store;
123
124 store = gtk_list_store_new( 2, G_TYPE_OBJECT, G_TYPE_STRING );
125
126 if ( HasFlag(wxCB_READONLY) )
127 {
128 m_widget = gtk_combo_box_new_with_model( GTK_TREE_MODEL(store) );
129 }
130 else
131 {
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 );
135 }
136
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) );
140
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);
146
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);
152}
153
154wxBitmapComboBox::~wxBitmapComboBox()
155{
156}
157
158GtkWidget* wxBitmapComboBox::GetConnectWidget()
159{
160 if ( GetEntry() )
161 return wxComboBox::GetConnectWidget();
162
163 return wxChoice::GetConnectWidget();
164}
165
166GdkWindow *wxBitmapComboBox::GTKGetWindow(wxArrayGdkWindows& windows) const
167{
168 if ( GetEntry() )
169 return wxComboBox::GTKGetWindow(windows);
170
171 return wxChoice::GTKGetWindow(windows);
172}
173
174// ----------------------------------------------------------------------------
175// Item manipulation
176// ----------------------------------------------------------------------------
177
178void wxBitmapComboBox::SetItemBitmap(unsigned int n, const wxBitmap& bitmap)
179{
180 if ( bitmap.IsOk() )
181 {
182 if ( m_bitmapSize.x == 0 )
183 {
184 m_bitmapSize.x = bitmap.GetWidth();
185 m_bitmapSize.y = bitmap.GetHeight();
186 }
187
188 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
189 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
190 GtkTreeIter iter;
191
192 if ( gtk_tree_model_iter_nth_child( model, &iter, NULL, n ) )
193 {
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 );
200 }
201 }
202}
203
204wxBitmap wxBitmapComboBox::GetItemBitmap(unsigned int n) const
205{
206 wxBitmap bitmap;
207
208 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
209 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
210 GtkTreeIter iter;
211
212 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
213 {
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 );
218 if ( pixbuf )
219 {
220 g_object_ref( pixbuf );
221 bitmap.SetPixbuf( pixbuf );
222 }
223 g_value_unset( &value );
224 }
225
226 return bitmap;
227}
228
229int wxBitmapComboBox::Append(const wxString& item, const wxBitmap& bitmap)
230{
231 const int n = wxComboBox::Append(item);
232 if ( n != wxNOT_FOUND )
233 SetItemBitmap(n, bitmap);
234 return n;
235}
236
237int wxBitmapComboBox::Append(const wxString& item, const wxBitmap& bitmap,
238 void *clientData)
239{
240 const int n = wxComboBox::Append(item, clientData);
241 if ( n != wxNOT_FOUND )
242 SetItemBitmap(n, bitmap);
243 return n;
244}
245
246int wxBitmapComboBox::Append(const wxString& item, const wxBitmap& bitmap,
247 wxClientData *clientData)
248{
249 const int n = wxComboBox::Append(item, clientData);
250 if ( n != wxNOT_FOUND )
251 SetItemBitmap(n, bitmap);
252 return n;
253}
254
255int wxBitmapComboBox::Insert(const wxString& item,
256 const wxBitmap& bitmap,
257 unsigned int pos)
258{
259 const int n = wxComboBox::Insert(item, pos);
260 if ( n != wxNOT_FOUND )
261 SetItemBitmap(n, bitmap);
262 return n;
263}
264
265int wxBitmapComboBox::Insert(const wxString& item, const wxBitmap& bitmap,
266 unsigned int pos, wxClientData *clientData)
267{
268 const int n = wxComboBox::Insert(item, pos, clientData);
269 if ( n != wxNOT_FOUND )
270 SetItemBitmap(n, bitmap);
271 return n;
272}
273
274void wxBitmapComboBox::GTKInsertComboBoxTextItem( unsigned int n, const wxString& text )
275{
276 GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
277 GtkTreeModel *model = gtk_combo_box_get_model( combobox );
278 GtkListStore *store = GTK_LIST_STORE( model );
279 GtkTreeIter iter;
280
281 gtk_list_store_insert( store, &iter, n );
282
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 );
288}
289
290// ----------------------------------------------------------------------------
291// wxTextEntry interface override
292// ----------------------------------------------------------------------------
293
294void wxBitmapComboBox::WriteText(const wxString& value)
295{
296 if ( GetEntry() )
297 wxComboBox::WriteText(value);
298}
299
300wxString wxBitmapComboBox::GetValue() const
301{
302 if ( GetEntry() )
303 return wxComboBox::GetValue();
304
305 return GetStringSelection();
306}
307
308void wxBitmapComboBox::Remove(long from, long to)
309{
310 if ( GetEntry() )
311 wxComboBox::Remove(from, to);
312}
313
314void wxBitmapComboBox::SetInsertionPoint(long pos)
315{
316 if ( GetEntry() )
317 wxComboBox::SetInsertionPoint(pos);
318}
319
320long wxBitmapComboBox::GetInsertionPoint() const
321{
322 if ( GetEntry() )
323 return wxComboBox::GetInsertionPoint();
324
325 return 0;
326 }
327long wxBitmapComboBox::GetLastPosition() const
328{
329 if ( GetEntry() )
330 return wxComboBox::GetLastPosition();
331
332 return 0;
333 }
334
335void wxBitmapComboBox::SetSelection(long from, long to)
336{
337 if ( GetEntry() )
338 wxComboBox::SetSelection(from, to);
339}
340
341void wxBitmapComboBox::GetSelection(long *from, long *to) const
342{
343 if ( GetEntry() )
344 wxComboBox::GetSelection(from, to);
345}
346
347bool wxBitmapComboBox::IsEditable() const
348{
349 if ( GetEntry() )
350 return wxTextEntry::IsEditable();
351
352 return false;
353}
354
355void wxBitmapComboBox::SetEditable(bool editable)
356{
357 if ( GetEntry() )
358 wxComboBox::SetEditable(editable);
359}
360
361#endif // wxUSE_BITMAPCOMBOBOX