[go: up one dir, main page]

Menu

[r377]: / trunk / tools / oogtk / ooui.h  Maximize  Restore  History

Download this file

285 lines (244 with data), 11.5 kB

  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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#ifndef OOUI_H
#define OOUI_H
namespace gtk {
class ActionGroup;
class Action : public Object {
friend class ActionGroup;
private:
AbstractCbk *cbk_;
public:
operator GtkAction *() const { return GTK_ACTION(obj_); }
Action(GObject *obj) : cbk_(NULL) { Init(obj); }
Action(const std::string &name,
const std::string &label,
const std::string &tooltip = "") : cbk_(NULL) {
const char *l = NULL, *s = NULL;
GtkStockItem item;
if (gtk_stock_lookup(label.c_str(), &item))
s = label.c_str();
else
l = label.c_str();
Init(gtk_action_new(name.c_str(), l, s, tooltip.empty() ? NULL : tooltip.c_str()));
Internal(true);
}
virtual ~Action() {
if (cbk_ && Obj()->ref_count < 2) {
delete cbk_;
cbk_ = NULL;
}
}
void Activate() { gtk_action_activate(*this); }
// get/set
bool Visible() const { return gtk_action_get_visible(*this); }
bool IsVisible() const { return gtk_action_is_visible(*this); }
void Visible(bool flag) { gtk_action_set_visible(*this, flag); }
bool Sensitive() const { return gtk_action_get_sensitive(*this); }
bool IsSensitive() const { return gtk_action_is_sensitive(*this); }
void Sensitive(bool flag) { gtk_action_set_sensitive(*this, flag); }
std::string Name() const { return gtk_action_get_name(*this); }
std::string AccelPath() const { return gtk_action_get_accel_path(*this); }
void AccelPath(const std::string &path) { gtk_action_set_accel_path(*this, path.c_str()); }
void AccelGroup(const gtk::AccelGroup &group) { gtk_action_set_accel_group(*this, group); }
// callback handling
template <typename A>
void Callback(void (A::*callback)(), A *base) {
if (cbk_) delete cbk_;
cbk_ = new CbkEvent<A,void>(base, callback);
}
template <typename A, typename B>
void Callback(void (A::*callback)(B), A *base, B data) {
if (cbk_) delete cbk_;
cbk_ = new CbkEvent<A,void,B>(base, callback, data);
}
};
struct ActionEntry {
std::string name;
std::string stock_id;
std::string label;
std::string tooltip;
std::string accel;
AbstractCbk *cbk;
void Tooltip(const std::string &tip) { tooltip = tip; }
void Accel(const std::string &acc) { accel = acc; }
ActionEntry(const std::string &nam, const std::string &lab,
const std::string &ttip = "", const char *acc = NULL) : name(nam), tooltip(ttip), cbk(NULL) {
GtkStockItem item;
if (gtk_stock_lookup(lab.c_str(), &item))
stock_id = lab;
else
label = lab;
if (acc) accel = acc;
}
void Label(const std::string &l) { label = l; }
void Icon(const std::string &icon) { stock_id = icon; }
template <typename A, typename B>
ActionEntry(const std::string &nam, const std::string &lab,
void (A::*callback)(B), A *base, B data,
const std::string &ttip = "", const char *acc = NULL) : name(nam), tooltip(ttip) {
cbk = new CbkEvent<A,void,B>(base, callback, data);
GtkStockItem item;
if (gtk_stock_lookup(lab.c_str(), &item))
stock_id = lab;
else
label = lab;
if (acc) accel = acc;
}
template <typename A>
ActionEntry(const std::string &nam, const std::string &lab,
void (A::*callback)(), A *base,
const std::string &ttip = "", const char *acc = NULL) : name(nam), tooltip(ttip) {
cbk = new CbkEvent<A,void>(base, callback);
GtkStockItem item;
if (gtk_stock_lookup(lab.c_str(), &item))
stock_id = lab;
else
label = lab;
if (acc) accel = acc;
}
};
typedef std::vector<ActionEntry> ActionList;
typedef std::vector<ActionEntry>::const_iterator ActionListIt;
class ActionGroup : public Object {
static void action_cbk(GtkAction *w, ActionGroup *group) {
if (gtk::Action *a = dynamic_cast<gtk::Action *>(Object::Find((GObject *)w)))
group->Dispatch(*a);
}
public:
operator GtkActionGroup *() const { return GTK_ACTION_GROUP(obj_); }
ActionGroup(GObject *obj) { Init(obj); }
ActionGroup(const std::string &name) {
Init(gtk_action_group_new(name.c_str()));
Internal(true);
}
void Dispatch(const gtk::Action &action) {
if (action.cbk_)
action.cbk_->notify();
}
void Add(const ActionList &actions) {
GtkActionEntry entries[actions.size()];
int i = 0;
memset(entries, 0, sizeof(GtkActionEntry) * actions.size());
for (ActionListIt it = actions.begin(); it != actions.end(); ++it, ++i) {
entries[i].name = it->name.c_str();
if (!it->label.empty())
entries[i].label = it->label.c_str();
if (!it->stock_id.empty())
entries[i].stock_id = it->stock_id.c_str();
if (!it->tooltip.empty())
entries[i].tooltip = it->tooltip.c_str();
if (!it->accel.empty())
entries[i].accelerator = it->accel.c_str();
entries[i].callback = GCallback(ActionGroup::action_cbk);
}
gtk_action_group_add_actions(*this, entries, actions.size(), this);
for (ActionListIt it = actions.begin(); it != actions.end(); ++it) {
if (gtk::Action *a = Action(it->name))
a->cbk_ = const_cast<AbstractCbk *>(it->cbk);
}
}
void Add(const gtk::Action &action, const char *accel = NULL) {
if (accel)
gtk_action_group_add_action_with_accel(*this, action, accel);
else
gtk_action_group_add_action(*this, action);
}
void Remove(const gtk::Action &action) {
gtk_action_group_remove_action(*this, action);
}
gtk::Action *Action(const std::string &actionname) {
return dynamic_cast<gtk::Action *>(Object::Find((GObject *)
gtk_action_group_get_action(*this, actionname.c_str())));
}
// get/set attributes
bool Visible() const { return gtk_action_group_get_visible(*this); }
void Visible(bool flag) { gtk_action_group_set_visible(*this, flag); }
bool Sensitive() const { return gtk_action_group_get_sensitive(*this); }
void Sensitive(bool flag) { gtk_action_group_set_sensitive(*this, flag); }
std::string Name() const { return gtk_action_group_get_name(*this); }
};
enum UIManagerItemType {
UIManagerAuto = GTK_UI_MANAGER_AUTO,
UIManagerMenuBar = GTK_UI_MANAGER_MENUBAR,
UIManagerMenu = GTK_UI_MANAGER_MENU,
UIManagerToolbar = GTK_UI_MANAGER_TOOLBAR,
UIManagerPlaceholder = GTK_UI_MANAGER_PLACEHOLDER,
UIManagerPopup =GTK_UI_MANAGER_POPUP,
UIManagerMenuItem =GTK_UI_MANAGER_MENUITEM,
UIManagerToolItem =GTK_UI_MANAGER_TOOLITEM,
UIManagerSeparator =GTK_UI_MANAGER_SEPARATOR,
UIManagerAccelerator =GTK_UI_MANAGER_ACCELERATOR
};
class UIManager : public Object {
public:
operator GtkUIManager *() const { return GTK_UI_MANAGER(obj_); }
// constructors
UIManager(GObject *obj) { Init(obj); }
UIManager() { Init(gtk_ui_manager_new()); Internal(true); }
UIManager(const ActionGroup &group, const std::string &desc = "") {
Init(gtk_ui_manager_new()); Internal(true);
Append(group);
if (!desc.empty()) FromString(desc);
}
// import datas
int FromString(const std::string &desc) {
GError *err = NULL;
int rc = gtk_ui_manager_add_ui_from_string(*this, desc.c_str(), desc.length(), &err);
if (rc == 0) {
std::string errorstring;
if (err)
errorstring = err->message;
else
errorstring = "FromString UIManager error";
g_error_free(err);
throw std::runtime_error(errorstring);
}
return rc;
}
int FromFile(const std::string &filename) {
GError *err = NULL;
int rc = gtk_ui_manager_add_ui_from_file(*this, filename.c_str(), &err);
if (rc == 0) {
std::string errorstring;
if (err)
errorstring = err->message;
else
errorstring = "FromString UIManager error";
g_error_free(err);
throw std::runtime_error(errorstring);
}
return rc;
}
std::string UI() {
std::string res;
if (gchar *ui = gtk_ui_manager_get_ui(*this)) {
res = ui;
g_free(ui);
}
return res;
}
void RemoveUI(int id) { gtk_ui_manager_remove_ui(*this, id); }
gtk::AccelGroup &AccelGroup() {
if (gtk::AccelGroup *a = dynamic_cast<gtk::AccelGroup *>(
Object::Find((GObject*)gtk_ui_manager_get_accel_group(*this))))
return *a;
else
throw std::runtime_error("UIManager without accelgroup!");
}
void Append(const ActionGroup &group) {
gtk_ui_manager_insert_action_group(*this, group, 1000); // to be sure we are at the end
}
void Insert(const ActionGroup &group, int position = 0) {
gtk_ui_manager_insert_action_group(*this, group, 0);
}
void Remove(const ActionGroup &group) { gtk_ui_manager_remove_action_group(*this, group); }
gtk::Widget *Widget(const std::string &path) {
return dynamic_cast<gtk::Widget *>(Object::Find((GObject*)
gtk_ui_manager_get_widget(*this, path.c_str())));
}
gtk::Action *Action(const std::string &path) {
return dynamic_cast<gtk::Action *>(Object::Find((GObject*)
gtk_ui_manager_get_action(*this, path.c_str())));
}
};
}
#endif