| 1 |
/* Tree View/List Store |
| 2 |
* |
| 3 |
* The Gtk::ListStore is used to store data in list form, to be used |
| 4 |
* later on by a Gtk::TreeView to display it. This demo builds a |
| 5 |
* simple Gtk::ListStore and displays it. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <gtkmm.h> |
| 9 |
|
| 10 |
class CellItem_Bug |
| 11 |
{ |
| 12 |
public: |
| 13 |
CellItem_Bug(); |
| 14 |
CellItem_Bug(bool fixed, guint number, const Glib::ustring& severity, const Glib::ustring& description); |
| 15 |
CellItem_Bug(const CellItem_Bug& src); |
| 16 |
~CellItem_Bug(); |
| 17 |
CellItem_Bug& operator=(const CellItem_Bug& src); |
| 18 |
|
| 19 |
bool m_fixed; |
| 20 |
guint m_number; |
| 21 |
Glib::ustring m_severity; |
| 22 |
Glib::ustring m_description; |
| 23 |
}; |
| 24 |
|
| 25 |
class Example_TreeView_ListStore : public Gtk::Window |
| 26 |
{ |
| 27 |
public: |
| 28 |
Example_TreeView_ListStore(); |
| 29 |
~Example_TreeView_ListStore() override; |
| 30 |
|
| 31 |
protected: |
| 32 |
virtual void create_model(); |
| 33 |
virtual void add_columns(); |
| 34 |
virtual void add_items(); |
| 35 |
virtual void liststore_add_item(const CellItem_Bug& foo); |
| 36 |
|
| 37 |
//Member widgets: |
| 38 |
Gtk::Box m_VBox; |
| 39 |
Gtk::ScrolledWindow m_ScrolledWindow; |
| 40 |
Gtk::Label m_Label; |
| 41 |
Gtk::TreeView m_TreeView; |
| 42 |
Glib::RefPtr<Gtk::ListStore> m_refListStore; |
| 43 |
|
| 44 |
typedef std::vector<CellItem_Bug> type_vecItems; |
| 45 |
type_vecItems m_vecItems; |
| 46 |
|
| 47 |
struct ModelColumns : public Gtk::TreeModelColumnRecord |
| 48 |
{ |
| 49 |
Gtk::TreeModelColumn<bool> fixed; |
| 50 |
Gtk::TreeModelColumn<unsigned int> number; |
| 51 |
Gtk::TreeModelColumn<Glib::ustring> severity; |
| 52 |
Gtk::TreeModelColumn<Glib::ustring> description; |
| 53 |
|
| 54 |
ModelColumns() { add(fixed); add(number); add(severity); add(description); } |
| 55 |
}; |
| 56 |
|
| 57 |
const ModelColumns m_columns; |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
CellItem_Bug::CellItem_Bug() |
| 62 |
{ |
| 63 |
m_fixed = false; |
| 64 |
m_number = 0; |
| 65 |
} |
| 66 |
|
| 67 |
CellItem_Bug::CellItem_Bug(bool fixed, guint number, const Glib::ustring& severity, const Glib::ustring& description) |
| 68 |
{ |
| 69 |
m_fixed = fixed; |
| 70 |
m_number = number; |
| 71 |
m_severity = severity; |
| 72 |
m_description = description; |
| 73 |
} |
| 74 |
|
| 75 |
CellItem_Bug::CellItem_Bug(const CellItem_Bug& src) |
| 76 |
{ |
| 77 |
operator=(src); |
| 78 |
} |
| 79 |
|
| 80 |
CellItem_Bug::~CellItem_Bug() |
| 81 |
{ |
| 82 |
} |
| 83 |
|
| 84 |
CellItem_Bug& CellItem_Bug::operator=(const CellItem_Bug& src) |
| 85 |
{ |
| 86 |
m_fixed = src.m_fixed; |
| 87 |
m_number = src.m_number; |
| 88 |
m_severity = src.m_severity; |
| 89 |
m_description = src.m_description; |
| 90 |
|
| 91 |
return *this; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
//Called by DemoWindow; |
| 96 |
Gtk::Window* do_treeview_liststore() |
| 97 |
{ |
| 98 |
return new Example_TreeView_ListStore(); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
Example_TreeView_ListStore::Example_TreeView_ListStore() |
| 103 |
: m_VBox(Gtk::ORIENTATION_VERTICAL, 8), |
| 104 |
m_Label("This is the bug list (note: not based on real data, it would be nice to have a nice ODBC interface to bugzilla or so, though).") |
| 105 |
{ |
| 106 |
set_title("Gtk::ListStore demo"); |
| 107 |
set_border_width(8); |
| 108 |
set_default_size(280, 250); |
| 109 |
|
| 110 |
add(m_VBox); |
| 111 |
m_VBox.pack_start(m_Label, Gtk::PACK_SHRINK); |
| 112 |
|
| 113 |
m_ScrolledWindow.set_shadow_type(Gtk::SHADOW_ETCHED_IN); |
| 114 |
m_ScrolledWindow.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC); |
| 115 |
m_VBox.pack_start(m_ScrolledWindow); |
| 116 |
|
| 117 |
/* create model */ |
| 118 |
create_model(); |
| 119 |
|
| 120 |
/* create tree view */ |
| 121 |
m_TreeView.set_model(m_refListStore); |
| 122 |
m_TreeView.set_search_column(m_columns.description.index()); |
| 123 |
|
| 124 |
add_columns(); |
| 125 |
m_ScrolledWindow.add(m_TreeView); |
| 126 |
|
| 127 |
show_all(); |
| 128 |
} |
| 129 |
|
| 130 |
Example_TreeView_ListStore::~Example_TreeView_ListStore() |
| 131 |
{ |
| 132 |
} |
| 133 |
|
| 134 |
void Example_TreeView_ListStore::add_items() |
| 135 |
{ |
| 136 |
m_vecItems.push_back( CellItem_Bug(false, 60482, "Normal", "scrollable notebooks and hidden tabs") ); |
| 137 |
m_vecItems.push_back( CellItem_Bug(false, 60620, "Critical", "gdk_window_clear_area (gdkwindow-win32.c) is not thread-safe") ); |
| 138 |
m_vecItems.push_back( CellItem_Bug(false, 50214, "Major", "Xft support does not clean up correctly") ); |
| 139 |
m_vecItems.push_back( CellItem_Bug(true, 52877, "Major", "GtkFileSelection needs a refresh method. ") ); |
| 140 |
m_vecItems.push_back( CellItem_Bug(false, 56070, "Normal", "Can not click button after setting insensitive") ); |
| 141 |
m_vecItems.push_back( CellItem_Bug(true, 56355, "Normal", "GtkLabel - Not all changes propagate correctly") ); |
| 142 |
m_vecItems.push_back( CellItem_Bug(false, 50055, "Normal", "Rework width/height computations for TreeView") ); |
| 143 |
m_vecItems.push_back( CellItem_Bug(false, 58278, "Normal", "gtk_dialog_set_response_sensitive () does nott work") ); |
| 144 |
m_vecItems.push_back( CellItem_Bug(false, 55767, "Normal", "Getters for all setters") ); |
| 145 |
m_vecItems.push_back( CellItem_Bug(false, 56925, "Normal", "Gtkcalender size") ); |
| 146 |
m_vecItems.push_back( CellItem_Bug(false, 56221, "Normal", "Selectable label needs right-click copy menu") ); |
| 147 |
m_vecItems.push_back( CellItem_Bug(true, 50939, "Normal", "Add shift clicking to GtkTextView") ); |
| 148 |
m_vecItems.push_back( CellItem_Bug(false, 6112, "Enhancement","netscape-like collapsable toolbars") ); |
| 149 |
m_vecItems.push_back( CellItem_Bug(false, 1, "Normal", "First bug :=)") ); |
| 150 |
} |
| 151 |
|
| 152 |
void Example_TreeView_ListStore::create_model() |
| 153 |
{ |
| 154 |
m_refListStore = Gtk::ListStore::create(m_columns); |
| 155 |
|
| 156 |
add_items(); |
| 157 |
|
| 158 |
std::for_each( |
| 159 |
m_vecItems.begin(), m_vecItems.end(), |
| 160 |
sigc::mem_fun(*this, &Example_TreeView_ListStore::liststore_add_item)); |
| 161 |
} |
| 162 |
|
| 163 |
void Example_TreeView_ListStore::liststore_add_item(const CellItem_Bug& foo) |
| 164 |
{ |
| 165 |
Gtk::TreeRow row = *(m_refListStore->append()); |
| 166 |
|
| 167 |
row[m_columns.fixed] = foo.m_fixed; |
| 168 |
row[m_columns.number] = foo.m_number; |
| 169 |
row[m_columns.severity] = foo.m_severity; |
| 170 |
row[m_columns.description] = foo.m_description; |
| 171 |
} |
| 172 |
|
| 173 |
void Example_TreeView_ListStore::add_columns() |
| 174 |
{ |
| 175 |
/* column for fixed toggles */ |
| 176 |
{ |
| 177 |
int cols_count = m_TreeView.append_column_editable("Fixed?", m_columns.fixed); |
| 178 |
Gtk::TreeViewColumn* pColumn = m_TreeView.get_column(cols_count-1); |
| 179 |
|
| 180 |
/* set this column to a fixed sizing (of 50 pixels) */ |
| 181 |
pColumn->set_sizing(Gtk::TREE_VIEW_COLUMN_FIXED); |
| 182 |
pColumn->set_fixed_width(50); |
| 183 |
pColumn->set_clickable(); |
| 184 |
} |
| 185 |
|
| 186 |
/* column for bug numbers */ |
| 187 |
m_TreeView.append_column("Bug number", m_columns.number); |
| 188 |
|
| 189 |
/* column for severities */ |
| 190 |
m_TreeView.append_column("Severity", m_columns.severity); |
| 191 |
|
| 192 |
/* column for descriptions */ |
| 193 |
m_TreeView.append_column("Description", m_columns.description); |
| 194 |
} |