1 /**
2 * Copyright (c) 2005 PCMan <pcman.tw@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifdef __GNUG__
20 #pragma implementation "listbox.h"
21 #endif
22
23 #include "listbox.h"
24
CListBox()25 CListBox::CListBox()
26 : CWidget()
27 {
28
29 m_Widget = gtk_tree_view_new();
30 PostCreate();
31 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(m_Widget), false);
32
33 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
34 GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes("", renderer, "text", 0, NULL);
35 gtk_tree_view_insert_column( (GtkTreeView*)m_Widget, col, -1);
36
37 m_Store = gtk_list_store_new(1, G_TYPE_STRING );
38
39 gtk_tree_view_set_model(GTK_TREE_VIEW(m_Widget), GTK_TREE_MODEL(m_Store));
40 g_object_unref(m_Store); // destroy model automatically with view
41
42 gtk_tree_selection_set_mode( gtk_tree_view_get_selection(GTK_TREE_VIEW(m_Widget)), GTK_SELECTION_BROWSE);
43
44 Show();
45 }
46
47
Append(string text)48 void CListBox::Append(string text)
49 {
50 GtkTreeIter iter;
51 gtk_list_store_append(m_Store, &iter);
52 gtk_list_store_set(m_Store, &iter, 0, (GValue*)text.c_str(), -1);
53 }
54
55
Insert(int pos,string text)56 void CListBox::Insert(int pos, string text)
57 {
58 GtkTreeIter iter;
59 gtk_list_store_insert(m_Store, &iter, pos);
60 gtk_list_store_set(m_Store, &iter, 0, (GValue*)text.c_str(), -1);
61 }
62
63
Delete(int idx)64 void CListBox::Delete(int idx)
65 {
66 if( idx >= Count() || idx <0 )
67 return;
68 GtkTreeIter iter;
69 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_Store), &iter, NULL, idx);
70 gtk_list_store_remove(m_Store, &iter);
71 }
72
73
MoveUp(int idx)74 void CListBox::MoveUp(int idx)
75 {
76 if( idx <= 0 )
77 return;
78
79 GtkTreeIter iter;
80 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_Store), &iter, NULL, idx-1);
81
82 GtkTreeIter iter2 = iter;
83 gtk_tree_model_iter_next(GTK_TREE_MODEL(m_Store), &iter2 );
84
85 gtk_list_store_swap(m_Store, &iter, &iter2);
86 }
87
MoveDown(int idx)88 void CListBox::MoveDown(int idx)
89 {
90 if( idx + 1 >= Count() )
91 return;
92
93 GtkTreeIter iter;
94 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_Store), &iter, NULL, idx);
95
96 GtkTreeIter iter2 = iter;
97 gtk_tree_model_iter_next(GTK_TREE_MODEL(m_Store), &iter2 );
98
99 gtk_list_store_swap(m_Store, &iter, &iter2);
100 }
101
102
GetCurSel()103 int CListBox::GetCurSel()
104 {
105 GtkTreeSelection* sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(m_Widget));
106 GtkTreeModel* model;
107 GList* list = gtk_tree_selection_get_selected_rows( sel, &model);
108 if( list )
109 {
110 GtkTreePath* path = (GtkTreePath*)list->data;
111 int* pidx = gtk_tree_path_get_indices( path );
112 int idx = pidx ? *pidx : -1;
113 g_list_foreach(list, (GFunc)gtk_tree_path_free, NULL);
114 g_list_free(list);
115 return idx;
116 }
117 return -1;
118 }
119
120
SetItemText(int idx,string text)121 void CListBox::SetItemText(int idx, string text)
122 {
123 GtkTreeIter iter;
124 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_Store), &iter, NULL, idx);
125 gtk_list_store_set(m_Store, &iter, 0, (GValue*)text.c_str(), -1);
126 }
127
GetItemText(int idx)128 string CListBox::GetItemText(int idx)
129 {
130 gchar* ptext = NULL;
131 GtkTreeIter iter;
132 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_Store), &iter, NULL, idx);
133 gtk_tree_model_get( GTK_TREE_MODEL(m_Store), &iter, 0, &ptext, -1);
134 string text(ptext);
135 if(ptext)
136 g_free(ptext);
137 return text;
138 }
139
SetCurSel(int idx)140 void CListBox::SetCurSel(int idx)
141 {
142 GtkTreeSelection* sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(m_Widget));
143 GtkTreeIter iter;
144 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_Store), &iter, NULL, idx);
145 gtk_tree_selection_select_iter(sel, &iter);
146 }
147