This repository has been archived by the owner on Aug 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
gtk_helpers.jl
471 lines (343 loc) · 15.3 KB
/
gtk_helpers.jl
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
eventMouse = Union{Gtk.GdkEventButton, Gtk.GdkEventMotion, Gtk.GdkEventCrossing}
eventKey = Gtk.GdkEventKey
macro progress(e::Expr, msg, dialog)
return quote
if !disableLoadingScreen && $dialog !== nothing
set_gtk_property!($dialog, :secondary_text, $msg)
LoadingSpinner.animate($dialog)
waitForIdle()
end
$e
end |> esc
end
# Stupid hack to make @gtktype macro work from here
function GtkType(name::Symbol)
expr = Meta.parse("@gtktype $name")
Base.eval(Gtk, expr)
return getfield(Gtk, name)
end
# Radio Menu Item
GtkType(:GtkRadioMenuItem)
function RadioMenuItem(name::String, group::Any=C_NULL)
return Gtk.GtkRadioMenuItem(
ccall((:gtk_radio_menu_item_new_with_label, Gtk.libgtk), Ptr{GObject}, (Ptr{Nothing}, Ptr{UInt8}), group, name)
)
end
function RadioMenuItem(name::String, widget::Gtk.GtkRadioMenuItem)
return Gtk.GtkRadioMenuItem(
ccall((:gtk_radio_menu_item_new_with_label_from_widget, Gtk.libgtk), Ptr{GObject}, (Ptr{GObject}, Ptr{UInt8}), widget, name)
)
end
function connectRadioGroupSignal(func::Function, radios::Array{Gtk.GtkRadioMenuItem, 1}; onlyCallbackActive::Bool=true)
for radio in radios
@guarded signal_connect(radio, "toggled") do widget
active = get_gtk_property(widget, :active, Bool)
index = findfirst(isequal(widget), radios)
if active && onlyCallbackActive || !onlyCallbackActive
func(index)
end
end
end
end
function connectRadioGroupSignal(funcs::Array{Function, 1}, radios::Array{Gtk.GtkRadioMenuItem, 1}; onlyCallbackActive::Bool=true)
for radio in radios
@guarded signal_connect(radio, "toggled") do widget
active = get_gtk_property(widget, :active, Bool)
index = findfirst(isequal(widget), radios)
if active && onlyCallbackActive || !onlyCallbackActive
funcs[index]()
end
end
end
end
# Image Menu Item
GtkType(:GtkImageMenuItem)
function setImageMenuItemIcon!(item::Gtk.GtkImageMenuItem, image::Gtk.GtkImage)
ccall((:gtk_image_menu_item_set_image, Gtk.libgtk), Ptr{Nothing}, (Ptr{GObject}, Ptr{GObject}), item, image)
end
function ImageMenuItem(name::String)
return Gtk.GtkImageMenuItem(
ccall((:gtk_image_menu_item_new_with_label , Gtk.libgtk), Ptr{GObject}, (Ptr{UInt8},), name)
)
end
function ImageMenuItem(name::String, image::Gtk.GtkImage)
item = ImageMenuItem(name)
setImageMenuItemIcon!(item, image)
return item
end
# Check Menu Item
GtkType(:GtkCheckMenuItem)
function CheckMenuItem(name::String, group::Any=C_NULL)
return Gtk.GtkCheckMenuItem(
ccall((:gtk_check_menu_item_new_with_label, Gtk.libgtk), Ptr{GObject}, (Ptr{UInt8},), name)
)
end
# Window Cursor
# Since our objects are kinda badly thrown together we need to cache them to prevent duplication errors
cursorCache = Dict{Tuple{Gtk.GtkWindow, Ptr{GObject}}, Gtk.GObject}()
# https://developer.gnome.org/gdk3/stable/gdk3-Cursors.html#gdk-cursor-new-from-name Name list
function newCursorFromName(window::Gtk.GtkWindow, name::String)
ptr = ccall((:gdk_cursor_new_from_name, Gtk.libgdk), Ptr{GObject}, (Ptr{GObject}, Ptr{UInt8}), Gtk.GAccessor.display(window), name)
if ptr != C_NULL && !haskey(cursorCache, (window, ptr))
cursorCache[(window, ptr)] = Gtk.GLib.GObjectLeaf(ptr)
end
return get(cursorCache, (window, ptr), nothing)
end
function setWindowCursor!(window::Gtk.GtkWindow, cursor::Gtk.GObject)
return ccall((:gdk_window_set_cursor, Gtk.libgdk), Ptr{GObject}, (Ptr{GObject}, Ptr{GObject}), get_gtk_property(window, :window, Gtk.GObject), cursor)
end
setWindowCursor!(window::Gtk.GtkWindow, name::String) = setWindowCursor!(window, newCursorFromName(window, name))
function getWindowCursor(window::Gtk.GtkWindow)
ptr = ccall((:gdk_window_get_cursor, Gtk.libgdk), Ptr{GObject}, (Ptr{GObject},), get_gtk_property(window, :window, Gtk.GObject))
if ptr != C_NULL && !haskey(cursorCache, (window, ptr))
cursorCache[(window, ptr)] = Gtk.GLib.GObjectLeaf(ptr)
end
return get(cursorCache, (window, ptr), newCursorFromName(window, "default"))
end
function changeCursor(func::Function, window::Gtk.GtkWindow, startName::String, stopName::String="")
startCursor = newCursorFromName(window, startName)
stopCursor = isempty(stopName) ? getWindowCursor(window) : newCursorFromName(window, stopName)
setWindowCursor!(window, startCursor)
func()
setWindowCursor!(window, stopCursor)
end
# General
function setDefaultDirection(direction::Integer)
return ccall((:gtk_widget_set_default_direction, Gtk.libgtk), Ptr{}, (Cint,), direction)
end
function initComboBox!(widget::Gtk.GtkComboBoxText, choices::Array{String, 1})
append!(widget, choices)
set_gtk_property!(widget, :active, 0)
end
function dropdownString(value::Any)
return value === nothing ? "nothing" : string(value)
end
function setComboIndex!(widget::Gtk.GtkComboBoxText, choices::Array{T, 1}, item::H; allowCustom::Bool=true) where {T, H}
if !allowCustom && !(item in choices)
throw(ArgumentError("The selected value does not exist for the given choices"))
end
item = dropdownString(item)
choices = dropdownString.(choices)
if !(item in choices)
push!(choices, item)
push!(widget, item)
end
index = findfirst(isequal(item), choices)
Gtk.GLib.@sigatom set_gtk_property!(widget, :active, something(index, 1) - 1)
return index
end
function setEntryText!(entry::Gtk.GtkEntryLeaf, value::Any; updatePlaceholder::Bool=true)
text = string(value)
Gtk.GLib.@sigatom GAccessor.text(entry, text)
if updatePlaceholder
Gtk.GLib.@sigatom GAccessor.placeholder_text(entry, text)
end
end
function convertString(str::String, as::Type=String)
if as == String
return str
elseif as == Nothing
return nothing
elseif as == Char
if length(str) == 1
return str[1]
else
error("Expected string with length 1, got $(length(str))")
end
elseif as == Bool
return parse(as, str)
elseif as <: Integer
return parse(Int, str)
elseif as <: AbstractFloat
return parse(Float64, str)
else
error("Unsupported return type")
end
end
function getEntryText(entry::Gtk.GtkEntryLeaf, as::Type=String)
str = Gtk.GLib.@sigatom Gtk.bytestring(GAccessor.text(entry))
return convertString(str, as)
end
function setTextViewText!(textView::Gtk.GtkTextViewLeaf, value::String="")
GAccessor.buffer(textView, TextBuffer(text=value))
end
function getTextViewText(textView::Gtk.GtkTextViewLeaf)
return join(collect(GAccessor.buffer(textView)))
end
function addProviderForScreen(screen::Any, provider::Gtk.GtkCssProviderLeaf)
return ccall((:gtk_style_context_add_provider_for_screen, Gtk.libgtk), Nothing, (Ptr{Nothing}, Ptr{GObject}, Cuint), screen, provider, 1)
end
function setDialogFolder(dialog::GObject, path::String)
return ccall((:gtk_file_chooser_set_current_folder, Gtk.libgtk), Cint, (Ptr{GObject}, Ptr{UInt8}), dialog, path)
end
function setDialogFilename(dialog::GObject, path::String)
return ccall((:gtk_file_chooser_set_current_name, Gtk.libgtk), Cint, (Ptr{GObject}, Ptr{UInt8}), dialog, path)
end
function getProgressDialog(; title::String="", description::String="", filename::String="", stylesheet::String="", parent::Any=GtkNullContainer(), buttons::Tuple{}=())
pixbuf = isempty(filename) ? Pixbuf(width=1, height=1, has_alpha=true) : Pixbuf(filename=filename, preserve_aspect_ratio=true)
image = Image(pixbuf, name="dialog_image")
dlg = GtkMessageDialog(title, (),
Gtk.GtkDialogFlags.DESTROY_WITH_PARENT, Gtk.GtkMessageType.INFO, parent,
secondary_text=description, image=image
)
if !isempty(stylesheet)
screen = Gtk.GAccessor.screen(dlg)
provider = CssProviderLeaf(filename=stylesheet)
addProviderForScreen(screen, provider)
end
return dlg
end
function setProgressDialogPixbuf!(dlg::Gtk.GObject, pixbuf::Union{Gtk.GdkPixbuf, Ptr{GObject}})
image = get_gtk_property(dlg, :image, GtkImage)
set_gtk_property!(image, :visible, true)
set_gtk_property!(image, :pixbuf, pixbuf)
end
useNativeFileDialogs = isdefined(Gtk, :save_dialog_native) && Gtk.libgtk_version >= v"3.20.0"
# Copy of Gtk.jl definition, but calls a function before running the widget
function infoDialog(func::Function, message::String, parent::Gtk.GtkWindow=Ahorn.window)
w = GtkMessageDialogLeaf(ccall((:gtk_message_dialog_new, Gtk.libgtk), Ptr{Gtk.GObject},
(Ptr{Gtk.GObject}, Cint, Cint, Cint, Ptr{UInt8}),
parent, Gtk.GtkDialogFlags.DESTROY_WITH_PARENT,
Gtk.GtkMessageType.INFO, Gtk.GtkButtonsType.CLOSE, C_NULL))
set_gtk_property!(w, :text, message)
func(w)
run(w)
Gtk.destroy(w)
end
infoDialog(message::String, parent::Gtk.GtkWindow=Ahorn.window) = infoDialog(w -> (), message, parent)
topMostInfoDialog(message::String, parent::Gtk.GtkWindow=Ahorn.window) = infoDialog(w -> GAccessor.keep_above(w, true), message, parent)
# Copy of Gtk.jl definition, but calls a function before running the widget
askDialog(message::String, parent::Gtk.GtkWindow=Ahorn.window) = askDialog(message, "No", "Yes", parent)
askDialog(func::Function, message::String, parent::Gtk.GtkWindow=Ahorn.window) = askDialog(func, message, "No", "Yes", parent)
function askDialog(func::Function, message::String, noText::String, yesText::String, parent::Gtk.GtkWindow=Ahorn.window)
dlg = GtkMessageDialog(message, ((noText, 0), (yesText, 1)),
Gtk.GtkDialogFlags.DESTROY_WITH_PARENT, Gtk.GtkMessageType.QUESTION, parent)
func(dlg)
response = run(dlg)
Gtk.destroy(dlg)
return response == 1
end
askDialog(message::String, noText::String, yesText::String, parent::Gtk.GtkWindow=Ahorn.window) = askDialog(dlg -> (), message, noText, yesText, parent)
topMostAskDialog(message::String, parent::Gtk.GtkWindow=Ahorn.window) = askDialog(dlg -> GAccessor.keep_above(dlg, true), message, parent)
# Modified version of standard open dialog
function openDialog(title::AbstractString, parent=GtkNullContainer(), filters::Union{AbstractVector, Tuple}=String[]; folder::String="", native::Bool=useNativeFileDialogs, kwargs...)
local dlg
if native
dlg = GtkFileChooserNative(title, parent, GConstants.GtkFileChooserAction.OPEN, "_Open", "_Cancel"; kwargs...)
else
dlg = GtkFileChooserDialog(title, parent, GConstants.GtkFileChooserAction.OPEN, (
("_Cancel", GConstants.GtkResponseType.CANCEL),
("_Open", GConstants.GtkResponseType.ACCEPT)
); kwargs...)
end
dlgp = GtkFileChooser(dlg)
if !isempty(filters)
Gtk.makefilters!(dlgp, filters)
end
if !isempty(folder)
setDialogFolder(dlgp, folder)
end
response = run(dlg)
multiple = get_gtk_property(dlg, :select_multiple, Bool)
local selection
if response == GConstants.GtkResponseType.ACCEPT
if multiple
filename_list = ccall((:gtk_file_chooser_get_filenames, Gtk.libgtk), Ptr{Gtk._GSList{String}}, (Ptr{GObject},), dlgp)
selection = String[f for f in Gtk.GList(filename_list, true)]
else
selection = Gtk.bytestring(GAccessor.filename(dlgp))
end
else
if multiple
selection = String[]
else
selection = ""
end
end
native ? Gtk.GLib.gc_unref(dlg) : Gtk.destroy(dlg)
return selection
end
# Modified version of standard save dialog
function saveDialog(title::AbstractString, parent=GtkNullContainer(), filters::Union{AbstractVector, Tuple}=String[]; folder::String="", filename::String="", warnOverwrite::Bool=true, native::Bool=useNativeFileDialogs, kwargs...)
local dlg
if native
dlg = GtkFileChooserNative(title, parent, GConstants.GtkFileChooserAction.SAVE, "_Save", "_Cancel"; kwargs...)
else
dlg = GtkFileChooserDialog(title, parent, GConstants.GtkFileChooserAction.SAVE, (
("_Cancel", GConstants.GtkResponseType.CANCEL),
("_Save", GConstants.GtkResponseType.ACCEPT)
); kwargs...)
end
dlgp = GtkFileChooser(dlg)
if !isempty(folder)
setDialogFolder(dlgp, folder)
end
if !isempty(filename)
setDialogFilename(dlgp, filename)
end
if !isempty(filters)
Gtk.makefilters!(dlgp, filters)
end
ccall((:gtk_file_chooser_set_do_overwrite_confirmation, Gtk.libgtk), Nothing, (Ptr{GObject}, Cint), dlg, warnOverwrite)
response = run(dlg)
if response == GConstants.GtkResponseType.ACCEPT
selection = Gtk.bytestring(GAccessor.filename(dlgp))
else
selection = ""
end
native ? Gtk.GLib.gc_unref(dlg) : Gtk.destroy(dlg)
return selection
end
function scalePixbufSimple(pixbuf::Gtk.GdkPixbuf, width::Integer=width(pixbuf), height::Integer=height(pixbuf), method::Int32=GdkInterpType.NEAREST)
return ccall((:gdk_pixbuf_scale_simple, Gtk.libgdkpixbuf), Ptr{GObject}, (Ptr{GObject}, Cint, Cint, Cint), pixbuf, width, height, method)
end
function pixbufFromSurface(surface::Cairo.CairoSurface, alpha::Bool=true)
return Pixbuf(data=[Ahorn.ARGB(Ahorn.argb32ToRGBATuple(c)...) for c in getSurfaceData(surface)], has_alpha=alpha)
end
getSurfaceData(surface::Cairo.CairoSurfaceImage) = surface.data
function getSurfaceData(surface::Cairo.CairoSurface)
imageSurface = CairoImageSurface(zeros(UInt32, floor(Int, height(surface)), floor(Int, width(surface))), Cairo.FORMAT_ARGB32)
imageSurfaceCtx = CairoContext(imageSurface)
drawImage(imageSurfaceCtx, surface, 0, 0)
return imageSurface.data
end
cairoGCCache = Dict{Int, Cairo.CairoContext}()
function getSurfaceContext(surface::Cairo.CairoSurface)
key = Int(surface.ptr)
if get(cairoGCCache, key, C_NULL) == C_NULL
delete!(cairoGCCache, surface)
end
get!(cairoGCCache, key) do
creategc(surface)
end
end
function deleteSurface(surface::Cairo.CairoSurface)
key = Int(surface.ptr)
if haskey(cairoGCCache, key)
ctx = cairoGCCache[key]
delete!(cairoGCCache, key)
Cairo.destroy(ctx)
Cairo.destroy(surface)
end
end
# Borrowed from Gtk.jl repo, build is not installable yet
function g_timeout_add(interval::Integer, cb::Function, user_data::CT) where CT
callback = @cfunction($cb, Cint, (Ref{CT},) )
ref, deref = Gtk.GLib.gc_ref_closure(user_data)
return ccall((:g_timeout_add_full, Gtk.libglib), Cint, (Cint, UInt32, Ptr{Nothing}, Ptr{Nothing}, Ptr{Nothing}), 0, UInt32(interval), callback, ref, deref)
end
# Borrowed from Gtk.jl repo, build is not installable yet
function g_idle_add(cb::Function, user_data::CT) where CT
callback = @cfunction($cb, Cint, (Ref{CT},) )
ref, deref = Gtk.GLib.gc_ref_closure(user_data)
return ccall((:g_idle_add_full, Gtk.libglib), Cint, (Cint, Ptr{Nothing}, Ptr{Nothing}, Ptr{Nothing}), 0, callback, ref, deref)
end
function addIdleCallback(cb::Function, data::CT, timeout::Number=-1) where CT
if timeout == -1
g_idle_add(data -> Cint(cb(data)), data)
else
g_timeout_add(round(Int, timeout * 1000), data -> Cint(cb(data)), data)
end
end
waitForIdle() = addIdleCallback(data -> false, nothing)
MenuItemsTypes = Union{Gtk.GtkMenuItem, Gtk.GtkImageMenuItem}