1 /*
2 * ===========================
3 * VDK Visual Develeopment Kit
4 * Version 2.0.4
5 * March 2004
6 * ===========================
7 *
8 * Copyright (C) 1998 - 2004 Mario Motta
9 * Developed by Mario Motta <mmotta@guest.net>
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
20 *
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 * 02111-1307, USA.
25 */
26 #include <vdk/vdkcomboentry.h>
27 #include <vdk/forms.h>
28 #include <string.h>
29 #include <vdk/colors.h>
FocusOutEvent(GtkWidget *,GdkEventFocus *,gpointer wid)30 int VDKComboEntry::FocusOutEvent(GtkWidget *,
31 GdkEventFocus*,
32 gpointer wid)
33 {
34 g_return_val_if_fail(wid != NULL,FALSE);
35 VDKComboEntry* obj = reinterpret_cast<VDKComboEntry*>(wid);
36 obj->SignalEmit(focus_out_signal);
37 return FALSE;
38 }
39 /*
40 */
FocusInEvent(GtkWidget *,GdkEventFocus *,gpointer wid)41 int VDKComboEntry::FocusInEvent(GtkWidget *,
42 GdkEventFocus *,
43 gpointer wid)
44 {
45 g_return_val_if_fail(wid != NULL, FALSE);
46 VDKComboEntry* obj = reinterpret_cast<VDKComboEntry*>(wid);
47 obj->SignalEmit(focus_in_signal);
48 return FALSE;
49 }
50
51 /*
52 */
VDKComboEntry(VDKForm * owner,char ** selections)53 VDKComboEntry::VDKComboEntry(VDKForm* owner, char** selections):
54 VDKObject(owner),
55 Editable("Editable", this, true, &VDKComboEntry::SetEditable),
56 Selected("Selected",this, -1, &VDKComboEntry::SetSelected,&VDKComboEntry::GetSelected),
57 Text("Text", this, NULL, &VDKComboEntry::SetText, &VDKComboEntry::GetText),
58 Selections("Selections", this, NULL, &VDKComboEntry::SetSelections, &VDKComboEntry::GetSelections),
59 StoreModel("StoreModel",this,NULL,&VDKComboEntry::SetStoreModel),
60 Sorted("Sorted",this,false,&VDKComboEntry::SetSorted),
61 SortingOrder("SortingOrder",this,GTK_SORT_ASCENDING)
62 {
63 GtkListStore* store = gtk_list_store_new (1, G_TYPE_STRING);
64 StoreModel(store);
65 widget = gtk_combo_box_entry_new_with_model (GTK_TREE_MODEL (store), 0);
66 // answering vdk signals is embedded entry widget
67 sigwid = GTK_WIDGET(GTK_ENTRY (GTK_BIN (widget)->child));
68 s_activated.obj = this;
69 s_activated.signal = activate_signal;
70 s_changed.obj = this;
71 s_changed.signal = changed_signal;
72 if(selections)
73 {
74 GtkTreeIter iter;
75 for(int t = 0; selections[t];t++)
76 {
77 gtk_list_store_append (store, &iter);
78 gtk_list_store_set (store, &iter, 0, selections[t], -1);
79 }
80 }
81
82 g_signal_connect_data (GTK_ENTRY (GTK_BIN (widget)->child), "activate",
83 G_CALLBACK (VDKObject::VDKSignalPipe), (gpointer) &s_activated,
84 NULL,(GConnectFlags) 0);
85 g_signal_connect_data (widget, "changed",
86 G_CALLBACK (VDKObject::VDKSignalPipe), (gpointer) &s_changed,
87 NULL,(GConnectFlags) 0);
88 gtk_signal_connect(GTK_OBJECT(GTK_ENTRY (GTK_BIN (widget)->child)),
89 "focus_out_event",
90 GTK_SIGNAL_FUNC(VDKComboEntry::FocusOutEvent),
91 (gpointer) this);
92 gtk_signal_connect(GTK_OBJECT(GTK_ENTRY (GTK_BIN (widget)->child)),
93 "focus_in_event",
94 GTK_SIGNAL_FUNC(VDKComboEntry::FocusInEvent),
95 (gpointer) this);
96 ConnectDefaultSignals();
97 }
98
99 /*
100 */
~VDKComboEntry()101 VDKComboEntry::~VDKComboEntry()
102 {
103
104 }
105
106 /*
107 */
SetSelections(StringList * selections)108 void VDKComboEntry::SetSelections(StringList* selections)
109 {
110 // clear the StoreModel
111 GtkTreeIter iter;
112 gtk_list_store_clear(StoreModel);
113 if(selections)
114 {
115 gtk_list_store_clear(StoreModel);
116 StringListIterator li(*selections);
117 for(;li;li++)
118 {
119 char* local = (char*) li.current();
120 gtk_list_store_append (StoreModel, &iter);
121 gtk_list_store_set (StoreModel, &iter, 0, local, -1);
122 }
123 }
124 }
125
126 StringList*
GetSelections(void)127 VDKComboEntry::GetSelections(void)
128 {
129 StringList* list = new StringList;
130 GtkTreeIter iter;
131 gchar *item = NULL;
132 GtkListStore* store = StoreModel;
133 for(bool flag = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store),&iter);
134 flag; flag = gtk_tree_model_iter_next(GTK_TREE_MODEL(store),&iter))
135 {
136 gtk_tree_model_get (GTK_TREE_MODEL(store), &iter, 0, &item, -1);
137 list->add(VDKUString(item));
138 }
139 return list;
140 }
141 /*
142 */
SetText(char * s)143 void VDKComboEntry::SetText(char* s)
144 {
145 gtk_entry_set_text(GTK_ENTRY(GTK_BIN (widget)->child),s);
146 }
147 /*
148 returned char* should be deleted by caller;
149 */
150 char*
GetText()151 VDKComboEntry::GetText()
152 {
153 const char* local = gtk_entry_get_text (GTK_ENTRY (GTK_BIN (widget)->child));
154 char* buffer = NULL;
155 if(local)
156 {
157 buffer = new char[strlen(local)+1];
158 strcpy(buffer,local);
159 }
160 return buffer;
161 }
162
163 void
SetStoreModel(GtkListStore * store_model)164 VDKComboEntry::SetStoreModel(GtkListStore* store_model)
165 {
166 GtkListStore* store = StoreModel;
167 g_object_unref (store);
168 gtk_combo_box_set_model (GTK_COMBO_BOX(widget),GTK_TREE_MODEL(store_model));
169 return;
170 }
171
172 void
SetSorted(bool flag)173 VDKComboEntry::SetSorted(bool flag)
174 {
175 GtkListStore* store = StoreModel;
176 GtkSortType stype = SortingOrder;
177 if(!Sorted && store && flag)
178 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(store),0,stype);
179 }
180
181 /*
182 */
SetBackground(VDKRgb rgb,GtkStateType state)183 void VDKComboEntry::SetBackground(VDKRgb rgb, GtkStateType state)
184 {
185 VDKColor *color = new VDKColor(Owner(),rgb.red,rgb.green,rgb.blue);
186 gtk_widget_modify_base (GTK_WIDGET(GTK_ENTRY(GTK_BIN (widget)->child)), state, color->Color());
187 }
188 /*
189 */
SetForeground(VDKRgb rgb,GtkStateType state)190 void VDKComboEntry::SetForeground(VDKRgb rgb, GtkStateType state)
191 {
192 VDKColor *color = new VDKColor(Owner(),rgb.red,rgb.green,rgb.blue);
193 gtk_widget_modify_text (GTK_WIDGET(GTK_ENTRY(GTK_BIN (widget)->child)), state, color->Color());
194 }
195
196 void
SetFont(VDKFont * font)197 VDKComboEntry::SetFont(VDKFont* font)
198 {
199 if(!GTK_WIDGET_REALIZED(GTK_WIDGET(widget)))
200 gtk_widget_realize(widget);
201 GtkRcStyle *rc_style;
202 rc_style = gtk_rc_style_new ();
203 rc_style->font_desc =
204 pango_font_description_copy (font->AsPangoFontDescription());
205 gtk_widget_modify_style (GTK_WIDGET(GTK_ENTRY(GTK_BIN (widget)->child)), rc_style);
206 gtk_rc_style_unref (rc_style);
207 gtk_widget_size_request (GTK_WIDGET(GTK_ENTRY(GTK_BIN (widget)->child)), NULL);
208 }
209