-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUIGTK.cxx
202 lines (172 loc) · 4.76 KB
/
GUIGTK.cxx
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
// SciTE - Scintilla based Text Editor
/** @file GUIGTK.cxx
** Interface to platform GUI facilities.
** Split off from Scintilla's Platform.h to avoid SciTE depending on implementation of Scintilla.
**/
// Copyright 1998-2010 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
#include <ctime>
#include <string>
#include <chrono>
#include <sstream>
#include <gtk/gtk.h>
#include "Scintilla.h"
#include "ScintillaWidget.h"
#include "GUI.h"
namespace GUI {
gui_string StringFromUTF8(const char *s) {
if (s)
return gui_string(s);
else
return gui_string("");
}
gui_string StringFromUTF8(const std::string &s) {
return s;
}
std::string UTF8FromString(const gui_string &s) {
return s;
}
gui_string StringFromInteger(long i) {
char number[32];
sprintf(number, "%0ld", i);
return gui_string(number);
}
gui_string StringFromLongLong(long long i) {
try {
std::ostringstream strstrm;
strstrm << i;
return StringFromUTF8(strstrm.str());
} catch (std::exception &) {
// Exceptions not enabled on stream but still causes diagnostic in Coverity.
// Simply swallow the failure and return the default value.
}
return gui_string();
}
std::string LowerCaseUTF8(std::string_view sv) {
gchar *lower = g_utf8_strdown(sv.data(), sv.length());
const std::string sLower(lower);
g_free(lower);
return sLower;
}
static GtkWidget *PWidget(WindowID wid) {
return static_cast<GtkWidget *>(wid);
}
void Window::Destroy() {
if (wid)
gtk_widget_destroy(GTK_WIDGET(wid));
wid = 0;
}
bool Window::HasFocus() {
return gtk_widget_has_focus(GTK_WIDGET(wid));
}
Rectangle Window::GetPosition() {
// Before any size allocated pretend its 1000 wide so not scrolled
Rectangle rc(0, 0, 1000, 1000);
if (wid) {
GtkAllocation allocation;
gtk_widget_get_allocation(PWidget(wid), &allocation);
rc.left = allocation.x;
rc.top = allocation.y;
if (allocation.width > 20) {
rc.right = rc.left + allocation.width;
rc.bottom = rc.top + allocation.height;
}
}
return rc;
}
void Window::SetPosition(Rectangle rc) {
GtkAllocation alloc;
alloc.x = rc.left;
alloc.y = rc.top;
alloc.width = rc.Width();
alloc.height = rc.Height();
gtk_widget_size_allocate(PWidget(wid), &alloc);
}
Rectangle Window::GetClientPosition() {
// On GTK, the client position is the window position
return GetPosition();
}
void Window::Show(bool show) {
if (show)
gtk_widget_show(PWidget(wid));
}
void Window::InvalidateAll() {
if (wid) {
gtk_widget_queue_draw(PWidget(wid));
}
}
void Window::SetTitle(const char *s) {
gtk_window_set_title(GTK_WINDOW(wid), s);
}
void Window::SetRedraw(bool /* redraw */) {
// Could call gdk_window_freeze_updates / gdk_window_thaw_updates here
// but unsure what the side effects will be.
}
void Menu::CreatePopUp() {
Destroy();
mid = gtk_menu_new();
g_object_ref_sink(G_OBJECT(mid));
}
void Menu::Destroy() noexcept {
if (mid)
g_object_unref(mid);
mid = 0;
}
#if !GTK_CHECK_VERSION(3,22,0)
static void MenuPositionFunc(GtkMenu *, gint *x, gint *y, gboolean *, gpointer userData) {
sptr_t intFromPointer = GPOINTER_TO_INT(userData);
*x = intFromPointer & 0xffff;
*y = intFromPointer >> 16;
}
#endif
void Menu::Show(Point pt G_GNUC_UNUSED, Window &) {
GtkMenu *widget = static_cast<GtkMenu *>(mid);
gtk_widget_show_all(GTK_WIDGET(widget));
#if GTK_CHECK_VERSION(3,22,0)
// Rely on GTK to do the right thing with positioning
gtk_menu_popup_at_pointer(widget, NULL);
#else
int screenHeight = gdk_screen_height();
int screenWidth = gdk_screen_width();
GtkRequisition requisition;
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_get_preferred_size(GTK_WIDGET(widget), NULL, &requisition);
#else
gtk_widget_size_request(GTK_WIDGET(widget), &requisition);
#endif
if ((pt.x + requisition.width) > screenWidth) {
pt.x = screenWidth - requisition.width;
}
if ((pt.y + requisition.height) > screenHeight) {
pt.y = screenHeight - requisition.height;
}
gtk_menu_popup(widget, NULL, NULL, MenuPositionFunc,
GINT_TO_POINTER((pt.y << 16) | pt.x), 0,
gtk_get_current_event_time());
#endif
}
sptr_t ScintillaPrimitive::Send(unsigned int msg, uptr_t wParam, sptr_t lParam) {
return scintilla_send_message(SCINTILLA(GetID()), msg, wParam, lParam);
}
bool IsDBCSLeadByte(int codePage, char ch) {
// Byte ranges found in Wikipedia articles with relevant search strings in each case
unsigned char uch = static_cast<unsigned char>(ch);
switch (codePage) {
case 932:
// Shift_jis
return ((uch >= 0x81) && (uch <= 0x9F)) ||
((uch >= 0xE0) && (uch <= 0xEF));
case 936:
// GBK
return (uch >= 0x81) && (uch <= 0xFE);
case 950:
// Big5
return (uch >= 0x81) && (uch <= 0xFE);
// Korean EUC-KR may be code page 949.
}
return false;
}
void SleepMilliseconds(int sleepTime) {
g_usleep(sleepTime * 1000);
}
}