File: treeview.hh

package info (click to toggle)
linuxdcpp 1.0.2-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,392 kB
  • ctags: 5,102
  • sloc: cpp: 33,213; ansic: 472; makefile: 15
file content (131 lines) | stat: -rw-r--r-- 3,967 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright © 2004-2008 Jens Oknelid, paskharen@gmail.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * In addition, as a special exception, compiling, linking, and/or
 * using OpenSSL with this program is allowed.
 */

#ifndef WULFOR_TREE_VIEW_HH
#define WULFOR_TREE_VIEW_HH

#include <gtk/gtk.h>
#include <cassert>
#include <map>
#include <string>

class TreeView
{
	public:
		typedef enum
		{
			STRING,
			STRINGR,
			INT,
			BOOL,
			PIXBUF,
			PIXBUF_STRING,
			EDIT_STRING,
			PROGRESS
		} columnType;

		TreeView();
		~TreeView();
		void setView(GtkTreeView *view);
		void setView(GtkTreeView *view, bool padding, const std::string &name = "");
		GtkTreeView *get();
		void insertColumn(const std::string &title, const GType &gtype, const columnType type, const int width, const std::string &linkedCol = "");
		void insertHiddenColumn(const std::string &title, const GType &gtype);
		void finalize();
		int getColCount();
		int getRowCount();
		GType* getGTypes();
		void setSortColumn_gui(const std::string &column, const std::string &sortColumn);
		int col(const std::string &title);
		void saveSettings();
		std::string getString(GtkTreeIter *i, const std::string &column, GtkTreeModel *m = NULL);

		template<class T, class C>
		C getValue(GtkTreeIter *i, const std::string &column, GtkTreeModel *m = NULL)
		{
			if (m == NULL)
				m = gtk_tree_view_get_model(view);
			T value;
			assert(gtk_tree_model_get_column_type(m, col(column)) != G_TYPE_STRING);
			gtk_tree_model_get(m, i, col(column), &value, -1);
			return C(value);
		}
		template<class T>
		T getValue(GtkTreeIter *i, const std::string &column, GtkTreeModel *m = NULL)
		{
			if (m == NULL)
				m = gtk_tree_view_get_model(view);
			T value;
			assert(gtk_tree_model_get_column_type(m, col(column)) != G_TYPE_STRING);
			gtk_tree_model_get(m, i, col(column), &value, -1);
			return value;
		}

	private:
		class Column
		{
			public:
				Column() {};
				Column(const std::string &title, int id, GType gtype, TreeView::columnType type, int width, const std::string &linkedCol = "") :
					title(title), id(id), gtype(gtype), type(type), width(width), pos(id), linkedCol(linkedCol), visible(true) {};
				Column(const std::string &title, int id, GType gtype) :
					title(title), id(id), gtype(gtype) {};
				std::string title;
				int id;
				GType gtype;
				TreeView::columnType type;
				int width;
				int pos;
				std::string linkedCol;
				bool visible;
				bool operator<(const Column &right) const
				{
					return pos < right.pos;
				}
		};

		void addColumn_gui(Column column);
		void restoreSettings();
		static gboolean popupMenu_gui(GtkWidget *widget, GdkEventButton *event, gpointer data);
		static void toggleColumnVisibility(GtkMenuItem *item, gpointer data);

		GtkTreeView *view;
		std::string name; // Used to save settings
		bool padding;
		int count;
		int visibleColumns;
		GtkMenu *menu;
		GType *gtypes;
		std::map<std::string, GtkWidget*> colMenuItems;

		typedef std::map<std::string, Column> ColMap;
		typedef std::map<int, std::string> SortedColMap;
		typedef ColMap::iterator ColIter;
		typedef SortedColMap::iterator SortedColIter;
		ColMap columns;
		SortedColMap sortedColumns;
		ColMap hiddenColumns;
		SortedColMap sortedHiddenColumns;
};

#else
class TreeView;
#endif