-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidget.cxx
More file actions
495 lines (421 loc) · 13 KB
/
Copy pathWidget.cxx
File metadata and controls
495 lines (421 loc) · 13 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// SciTE - Scintilla based Text Editor
// Widget.cxx - code for manipulating GTK widgets
// Copyright 2010 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
#include <cstdlib>
#include <cstring>
#include <tuple>
#include <string>
#include <string_view>
#include <vector>
#include <chrono>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include "Scintilla.h"
#include "GUI.h"
#include "StringHelpers.h"
#include "Widget.h"
// Key names are longer for GTK 3
#if GTK_CHECK_VERSION(3,0,0)
#define GKEY_Escape GDK_KEY_Escape
#define GKEY_Void GDK_KEY_VoidSymbol
#else
#define GKEY_Escape GDK_Escape
#define GKEY_Void GDK_VoidSymbol
#endif
WBase::operator GtkWidget*() const {
return GTK_WIDGET(GetID());
}
GtkWidget* WBase::Pointer() {
return GTK_WIDGET(GetID());
}
bool WBase::Sensitive() {
return gtk_widget_get_sensitive(GTK_WIDGET(Pointer()));
}
void WStatic::Create(GUI::gui_string text) {
SetID(gtk_label_new_with_mnemonic(text.c_str()));
}
bool WStatic::HasMnemonic() {
return gtk_label_get_mnemonic_keyval(GTK_LABEL(Pointer())) != GKEY_Void;
}
void WStatic::SetMnemonicFor(WBase &w) {
gtk_label_set_mnemonic_widget(GTK_LABEL(Pointer()), w);
}
void WEntry::Create(const GUI::gui_char *text) {
SetID(gtk_entry_new());
if (text)
gtk_entry_set_text(GTK_ENTRY(GetID()), text);
}
void WEntry::ActivatesDefault() {
gtk_entry_set_activates_default(GTK_ENTRY(GetID()), TRUE);
}
const GUI::gui_char *WEntry::Text() {
return gtk_entry_get_text(GTK_ENTRY(GetID()));
}
int WEntry::Value() {
return atoi(Text());
}
void WEntry::SetText(const GUI::gui_char *text) {
return gtk_entry_set_text(GTK_ENTRY(GetID()), text);
}
void WEntry::SetValid(GtkEntry *entry, bool valid) {
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_name(GTK_WIDGET(entry), valid ? "" : "entryInvalid");
#else
if (valid) {
// Reset widget's background color
// to the one given by the GTK theme
gtk_widget_modify_base(GTK_WIDGET(entry), GTK_STATE_NORMAL, NULL);
} else {
GdkColor red = { 0, 0xFFFF, 0x6666, 0x6666 };
gtk_widget_modify_base(GTK_WIDGET(entry), GTK_STATE_NORMAL, &red);
}
#endif
}
void WComboBoxEntry::Create() {
#if GTK_CHECK_VERSION(3,0,0)
SetID(gtk_combo_box_text_new_with_entry());
#else
SetID(gtk_combo_box_entry_new_text());
#endif
}
GtkEntry *WComboBoxEntry::Entry() {
return GTK_ENTRY(gtk_bin_get_child(GTK_BIN(GetID())));
}
void WComboBoxEntry::ActivatesDefault() {
gtk_entry_set_activates_default(Entry(), TRUE);
}
const GUI::gui_char *WComboBoxEntry::Text() {
return gtk_entry_get_text(Entry());
}
void WComboBoxEntry::SetText(const GUI::gui_char *text) {
return gtk_entry_set_text(Entry(), text);
}
bool WComboBoxEntry::HasFocusOnSelfOrChild() {
return HasFocus() || gtk_widget_has_focus(GTK_WIDGET(Entry()));
}
void WComboBoxEntry::RemoveText(int position) {
#if GTK_CHECK_VERSION(3,0,0)
gtk_combo_box_text_remove(GTK_COMBO_BOX_TEXT(GetID()), position);
#else
gtk_combo_box_remove_text(GTK_COMBO_BOX(GetID()), position);
#endif
}
void WComboBoxEntry::AppendText(const char *text) {
#if GTK_CHECK_VERSION(3,0,0)
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(GetID()), text);
#if GTK_CHECK_VERSION(3,14,0)
gtk_combo_box_set_button_sensitivity(GTK_COMBO_BOX(GetID()), GTK_SENSITIVITY_ON);
#endif
#else
gtk_combo_box_append_text(GTK_COMBO_BOX(GetID()), text);
#endif
}
void WComboBoxEntry::ClearList() {
#if GTK_CHECK_VERSION(3,0,0)
gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(GetID()));
#if GTK_CHECK_VERSION(3,14,0)
gtk_combo_box_set_button_sensitivity(GTK_COMBO_BOX(GetID()), GTK_SENSITIVITY_OFF);
#endif
#else
for (int i = 0; i < 10; i++) {
RemoveText(0);
}
#endif
}
void WComboBoxEntry::FillFromMemory(const std::vector<std::string> &mem, bool useTop) {
ClearList();
for (const std::string &s : mem) {
AppendText(s.c_str());
}
if (useTop) {
gtk_entry_set_text(Entry(), mem[0].c_str());
}
}
void WButton::Create(GUI::gui_string text, GCallback func, gpointer data) {
SetID(gtk_button_new_with_mnemonic(text.c_str()));
gtk_widget_set_can_default(GTK_WIDGET(GetID()), TRUE);
g_signal_connect(G_OBJECT(GetID()), "clicked", func, data);
}
void WButton::Create(GUI::gui_string text) {
SetID(gtk_button_new_with_mnemonic(text.c_str()));
gtk_widget_set_can_default(GTK_WIDGET(GetID()), TRUE);
}
void WToggle::Create(const GUI::gui_string &text) {
SetID(gtk_check_button_new_with_mnemonic(text.c_str()));
}
bool WToggle::Active() {
return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(GetID()));
}
void WToggle::SetActive(bool active) {
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(GetID()), active);
}
void WProgress::Create() {
SetID(gtk_progress_bar_new());
}
WCheckDraw::WCheckDraw() : cdfn(NULL), user(NULL) {
}
WCheckDraw::~WCheckDraw() {
}
static void GreyToAlpha(GdkPixbuf *ppb, GdkColor fore) {
guchar *pixels = gdk_pixbuf_get_pixels(ppb);
int rowStride = gdk_pixbuf_get_rowstride(ppb);
int width = gdk_pixbuf_get_width(ppb);
int height = gdk_pixbuf_get_height(ppb);
for (int y =0; y<height; y++) {
guchar *pixelsRow = pixels + rowStride * y;
for (int x =0; x<width; x++) {
guchar alpha = pixelsRow[0];
pixelsRow[3] = 255 - alpha;
pixelsRow[0] = fore.red / 256;
pixelsRow[1] = fore.green / 256;
pixelsRow[2] = fore.blue / 256;
pixelsRow += 4;
}
}
}
void WCheckDraw::Create(const char **xpmImage, const GUI::gui_string &toolTip, GtkStyle *pStyle_) {
GtkWidget *button = gtk_toggle_button_new();
gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), FALSE);
GdkPixbuf *pbGrey = gdk_pixbuf_new_from_xpm_data(xpmImage);
GdkPixbuf *pbAlpha = gdk_pixbuf_add_alpha(pbGrey, TRUE, 0xff, 0xff, 0);
g_object_unref(pbGrey);
#if GTK_CHECK_VERSION(3, 4, 0)
(void)pStyle_;
GdkRGBA rgbaFore;
GtkStyleContext *context = gtk_widget_get_style_context(button);
gtk_style_context_get_color(context, gtk_style_context_get_state(context), &rgbaFore);
GdkColor fore;
fore.red = rgbaFore.red * 65535;
fore.green = rgbaFore.green * 65535;
fore.blue = rgbaFore.blue * 65535;
fore.pixel = 0;
#else
GdkColor fore = pStyle_->fg[gtk_widget_get_state(button)];
#endif
// Convert the grey to alpha and make black
GreyToAlpha(pbAlpha, fore);
GtkWidget *image = gtk_image_new_from_pixbuf(pbAlpha);
g_object_unref(pbAlpha);
SetID(button);
gtk_container_add(GTK_CONTAINER(button), image);
g_signal_connect(button, "toggled", G_CALLBACK(Toggled), this);
gtk_widget_set_name(GTK_WIDGET(button), "toggler");
GUI::gui_string toolTipNoMnemonic = toolTip;
size_t posMnemonic = toolTipNoMnemonic.find("_");
if (posMnemonic != GUI::gui_string::npos)
toolTipNoMnemonic.replace(posMnemonic, 1, "");
gtk_widget_set_tooltip_text(Pointer(), toolTipNoMnemonic.c_str());
}
void WCheckDraw::Toggled(GtkWidget *, WCheckDraw *pcd) {
if (pcd->cdfn)
pcd->cdfn(pcd, pcd->user);
}
GtkToggleButton *WCheckDraw::ToggleButton() {
return reinterpret_cast<GtkToggleButton*>(GetID());
}
bool WCheckDraw::Active() {
return gtk_toggle_button_get_active(ToggleButton());
}
void WCheckDraw::SetActive(bool active) {
gtk_toggle_button_set_active(ToggleButton(), active);
}
void WCheckDraw::Toggle() {
SetActive(!Active());
}
void WCheckDraw::SetChangeFunction(ChangeFunction cdfn_, void *user_) {
cdfn = cdfn_;
user = user_;
}
#if GTK_CHECK_VERSION(3,4,0)
#define USE_GRID 1
#else
#define USE_GRID 0
#endif
WTable::WTable(int rows_, int columns_) :
rows(rows_), columns(columns_), next(0) {
#if USE_GRID
SetID(gtk_grid_new());
#else
SetID(gtk_table_new(rows, columns, FALSE));
#endif
}
void WTable::Add(GtkWidget *child, int width, bool expand, int xpadding, int ypadding) {
if (child) {
#if USE_GRID
gtk_widget_set_hexpand(child, expand);
#if GTK_CHECK_VERSION(3,14,0)
gtk_widget_set_margin_end(child, xpadding);
#else
gtk_widget_set_margin_right(child, xpadding);
#endif
gtk_widget_set_margin_bottom(child, ypadding);
gtk_grid_attach(GTK_GRID(GetID()), child,
next % columns, next / columns,
width, 1);
#else
GtkAttachOptions opts = static_cast<GtkAttachOptions>(
GTK_SHRINK | GTK_FILL);
GtkAttachOptions optsExpand = static_cast<GtkAttachOptions>(
GTK_SHRINK | GTK_FILL | GTK_EXPAND);
gtk_table_attach(GTK_TABLE(GetID()), child,
next % columns, next % columns + width,
next / columns, (next / columns) + 1,
expand ? optsExpand : opts, opts,
xpadding, ypadding);
#endif
}
next += width;
}
void WTable::Label(GtkWidget *child) {
#if GTK_CHECK_VERSION(3,14,0)
gtk_widget_set_halign(child, GTK_ALIGN_END);
gtk_widget_set_valign(child, GTK_ALIGN_BASELINE);
#else
gtk_misc_set_alignment(GTK_MISC(child), 1.0, 0.5);
#endif
Add(child);
}
void WTable::PackInto(GtkBox *box, gboolean expand) {
gtk_box_pack_start(box, Pointer(), expand, expand, 0);
}
void WTable::Resize(int rows_, int columns_) {
rows = rows_;
columns = columns_;
#if !USE_GRID
gtk_table_resize(GTK_TABLE(GetID()), rows, columns);
#endif
next = 0;
}
void WTable::NextLine() {
next = ((next - 1) / columns + 1) * columns;
}
GUI::gui_char KeyFromLabel(GUI::gui_string label) {
if (!label.empty()) {
size_t posMnemonic = label.find('_');
return MakeLowerCase(label[posMnemonic + 1]);
}
return 0;
}
void Dialog::Create(const GUI::gui_string &title) {
wid = gtk_dialog_new();
gtk_window_set_title(GTK_WINDOW(GetID()), title.c_str());
gtk_window_set_resizable(GTK_WINDOW(GetID()), TRUE);
}
void Dialog::Display(GtkWidget *parent, bool modal) {
// Mark it as a modal transient dialog
gtk_window_set_modal(GTK_WINDOW(GetID()), modal);
if (parent) {
gtk_window_set_transient_for(GTK_WINDOW(GetID()), GTK_WINDOW(parent));
}
g_signal_connect(G_OBJECT(GetID()), "destroy", G_CALLBACK(SignalDestroy), this);
gtk_widget_show_all(GTK_WIDGET(GetID()));
if (modal) {
while (Created()) {
gtk_main_iteration();
}
}
}
GtkWidget *Dialog::ResponseButton(const GUI::gui_string &text, int responseID) {
return gtk_dialog_add_button(GTK_DIALOG(GetID()), text.c_str(), responseID);
}
void Dialog::Present() {
gtk_window_present(GTK_WINDOW(GetID()));
}
GtkWidget *Dialog::ContentArea() {
#if GTK_CHECK_VERSION(3,0,0)
return gtk_dialog_get_content_area(GTK_DIALOG(GetID()));
#else
return GTK_DIALOG(GetID())->vbox;
#endif
}
void Dialog::SignalDestroy(GtkWidget *, Dialog *d) {
if (d) {
d->SetID(0);
}
}
void Strip::Creation(GtkWidget *) {
g_signal_connect(G_OBJECT(GetID()), "button-press-event", G_CALLBACK(ButtonsPress), this);
}
void Strip::Show(int) {
gtk_widget_show(Widget(*this));
visible = true;
}
void Strip::Close() {
gtk_widget_hide(Widget(*this));
visible = false;
}
bool Strip::KeyDown(GdkEventKey *event) {
bool retVal = false;
if (visible) {
if (event->keyval == GKEY_Escape) {
Close();
return true;
}
if (event->state & GDK_MOD1_MASK) {
GList *childWidgets = gtk_container_get_children(GTK_CONTAINER(GetID()));
for (GList *child = g_list_first(childWidgets); child; child = g_list_next(child)) {
GtkWidget **w = (GtkWidget **)child;
std::string name = gtk_widget_get_name(*w);
std::string label;
if (name == "GtkButton" || name == "GtkCheckButton") {
label = gtk_button_get_label(GTK_BUTTON(*w));
} else if (name == "GtkLabel") {
label = gtk_label_get_label(GTK_LABEL(*w));
}
char key = KeyFromLabel(label);
if (static_cast<unsigned int>(key) == event->keyval) {
//fprintf(stderr, "%p %s %s %c\n", *w, name.c_str(), label.c_str(), key);
if (name == "GtkButton" || name == "GtkCheckButton") {
gtk_button_clicked(GTK_BUTTON(*w));
} else if (name == "GtkLabel") {
// Only ever use labels to label ComboBoxEntry
GtkWidget *pwidgetSelect = gtk_label_get_mnemonic_widget(GTK_LABEL(*w));
if (pwidgetSelect) {
gtk_widget_grab_focus(pwidgetSelect);
}
}
retVal = true;
break;
}
}
g_list_free(childWidgets);
}
}
return retVal;
}
void Strip::MenuSignal(GtkMenuItem *menuItem, Strip *pStrip) {
sptr_t cmd = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menuItem), "CmdNum"));
pStrip->MenuAction(cmd);
}
void Strip::AddToPopUp(GUI::Menu &popup, const char *label, int cmd, bool checked) {
allowMenuActions = false;
GUI::gui_string localised = localiser->Text(label);
GtkWidget *menuItem = gtk_check_menu_item_new_with_mnemonic(localised.c_str());
gtk_menu_shell_append(GTK_MENU_SHELL(popup.GetID()), menuItem);
g_object_set_data(G_OBJECT(menuItem), "CmdNum", GINT_TO_POINTER(cmd));
g_signal_connect(G_OBJECT(menuItem),"activate", G_CALLBACK(MenuSignal), this);
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuItem), checked ? TRUE : FALSE);
allowMenuActions = true;
}
void Strip::ChildFocus(GtkWidget *widget) {
childHasFocus = widget != 0;
}
gboolean Strip::ChildFocusSignal(GtkContainer */*container*/, GtkWidget *widget, Strip *pStrip) {
pStrip->ChildFocus(widget);
return FALSE;
}
gboolean Strip::FocusSignal(GtkWidget */*widget*/, GtkDirectionType direction, Strip *pStrip) {
return pStrip->Focus(direction);
}
bool Strip::VisibleHasFocus() const {
return visible && childHasFocus;
}
gint Strip::ButtonsPress(GtkWidget *, GdkEventButton *event, Strip *pstrip) {
if (event->button == 3) {
pstrip->ShowPopup();
return TRUE;
}
return FALSE;
}