support for GTK 3.98.3

This commit is contained in:
Stefan Salewski 2020-05-19 14:19:38 +02:00
commit a725770ff0
15 changed files with 386 additions and 193 deletions

View file

@ -1,6 +1,6 @@
= High level GTK3 (and GTK4) bindings for the Nim programming language
(c) Stefan Salewski
//Version 0.7.3
//Version 0.7.4
:experimental:
:imagesdir: http://ssalewski.de/tmp
:source-highlighter: pygments
@ -25,7 +25,9 @@ with code inserted from files directly.
NOTE: This work is partly based on earlier works of J. Mansour and has been supported by A. Rumpf, E. Bassi and other _Nim_ and _GTK/Gnome_ developers.
The `combinatorics` module was kindly provided by R. Behrends.
NOTE: Starting with current version v0.7.3 we allow passing a type descriptor as first parameter to the
NOTE: With current version 0.7.4 we support latest GTK 3.98.3 which may become GTK 4 at the end of 2020.
NOTE: Starting with version v0.7.3 we allow passing a type descriptor as first parameter to the
new() procs like `newButton(MyButtonSubclass)` to support subclassing and extending Widgets in OOP style.
See <<Extending or sub-classing Widgets>> for an complete example. The init() procs which were used
before for this task are now deprecated and will be removed later. This new approach generally saves
@ -2723,6 +2725,7 @@ proc main =
## Wait until error or EOS
bus = gst.getBus(pipeline)
msg = gst.timedPopFiltered(bus, gst.Clock_Time_None, {gst.MessageFlag.error, gst.MessageFlag.eos})
echo msg.getType
discard gst.setState(pipeline, gst.State.null) # is this necessary?
main()
@ -2752,8 +2755,10 @@ button, but called after gtk4.main(). A bit strange indeed, see forum note of Mr
.headerbar.nim
----
# https://github.com/GNOME/gtk/blob/mainline/tests/testheaderbar.c
# this is still based on the original testheaderbar.c, which was recently replaced by testheaderbar2.c
# and testheaderbar.c is not a very good Nim GTK4 example unfortunately -- too comlicated and strange code.
# nim c headerbar.nim
import gintro/[gtk4, glib, gobject, gio]
import gintro/[gtk4, glib, gobject]
const
Css = """
@ -2775,7 +2780,7 @@ const
# we try to avoid use of global header variable as done in C code
type
MyWindow = ref object of gtk4.Window
header: gtk4.Headerbar
header: gtk4.Widget
proc response(d: gtk4.FileChooserDialog; responseID: int) = gtk4.destroy(d)
@ -2787,11 +2792,11 @@ proc onBookmarkClicked(button: Button; data: MyWindow) =
chooser.connect("response", response)
chooser.show
proc changeSubtitle(button: Button; w: MyWindow) =
if w.header.subtitle == "":
w.header.setSubtitle("(subtle subtitle)")
else:
w.header.setSubtitle("") # can we pass nil?
#proc changeSubtitle(button: Button; w: MyWindow) =
# if w.header.subtitle == "":
# w.header.setSubtitle("(subtle subtitle)")
# else:
# w.header.setSubtitle("") # can we pass nil?
proc toggleFullscreen(button: Button; window: MyWindow) =
var fullscreen {.global.}: bool
@ -2807,68 +2812,85 @@ proc toIntVal(i: int): Value =
discard init(result, gtype)
setInt(result, i)
proc quit(b: Button) = gtk4.mainQuit()
var done = false
proc quit_cb(b: Button) = # we can not pass a var parameter
#gtk4.mainQuit()
done = true
wakeup(defaultMainContext()) # g_main_context_wakeup (NULL);
proc changeHeader(button: ToggleButton; window: MyWindow) =
if button != nil and button.getActive:
window.header = cast[HeaderBar](newBox(gtk4.Orientation.horizontal,
10)) # intended cast, see C code!
addClass(getStyleContext(window.header), "titlebar")
addClass(getStyleContext(window.header), "header-bar")
window.header.setProperty("margin", toIntVal(10))
window.header = (newBox(gtk4.Orientation.horizontal,
10))
addCssClass(window.header, "titlebar")
addCssClass(window.header, "header-bar")
#window.header.setProperty("margin_start", toIntVal(10))
#window.header.setProperty("margin_end", toIntVal(10))
#window.header.setProperty("margin_top", toIntVal(10))
#window.header.setProperty("margin_bottom", toIntVal(10))
window.header.setMarginStart(10)
window.header.setMarginEnd(10)
window.header.setMarginTop(10)
window.header.setMarginBottom(10)
let label = newLabel("Label")
window.header.add(label)
gtk4.Box(window.header).append(label)
let levelBar = newLevelBar()
levelBar.setValue(0.4)
levelBar.setHexpand
window.header.add(levelBar)
gtk4.Box(window.header).append(levelBar)
else:
window.header = newHeaderBar()
addClass(getStyleContext(window.header), "titlebar")
window.header.setTitle("Example header")
#addClass(getStyleContext(window.header), "titlebar")
addCssClass(window.header, "titlebar")
#window.header.setTitle("Example header")
var button = newButton("_Close")
button.setUseUnderline
addClass(getStyleContext(button), "suggested-action")
button.connect("clicked", quit)
window.header.packEnd(button)
addCssClass(button, "suggested-action")
button.connect("clicked", quit_cb)
gtk4.HeaderBar(window.header).packEnd(button)
button = newButton()
let image = newImageFromIconName("bookmark-new-symbolic")
button.connect("clicked", onBookmarkClicked, window)
button.add(image)
window.header.packStart(button)
button.setChild(image)
gtk4.HeaderBar(window.header).packStart(button)
window.setTitlebar(window.header)
proc main =
gtk4.init()
var window: MyWindow
initWindow(window)
addClass(getStyleContext(window), "main")
#var window: MyWindow
let window = newWindow(MyWindow)
addCssClass(window, "main") # gtk_widget_add_css_class (window, "main");
let provider = newCssProvider()
provider.loadFromData(Css)
addProviderForDisplay(getDisplay(window), provider, STYLE_PROVIDER_PRIORITY_USER)
changeHeader(nil, window)
let box = newBox(Orientation.vertical, 0)
window.add(box)
window.setChild(box) # gtk_window_set_child (GTK_WINDOW (window), box);
#window.add(box)
let content = newImageFromIconName("start-here-symbolic")
content.setPixelSize(512)
box.add(content)
content.setVexpand
box.append(content)
let footer = newActionBar()
footer.setCenterWidget(newCheckButtonWithLabel("Middle"))
let button = newToggleButtonWithLabel("Custom")
button.connect("clicked", changeHeader, window)
footer.packStart(button)
var button1 = newButton("Subtitle")
button1.connect("clicked", changeSubtitle, window)
footer.packEnd(button1)
button1 = newButton("Fullscreen")
#var button1 = newButton("Subtitle")
#button1.connect("clicked", changeSubtitle, window)
#footer.packEnd(button1)
var button1 = newButton("Fullscreen")
footer.packEnd(button1)
button1.connect("clicked", toggleFullscreen, window)
box.add(footer)
box.append(footer)
window.show
gtk4.main()
#gtk4.main()
while not done:
discard iteration(defaultMainContext(), true) # g_main_context_iteration (NULL, TRUE);
destroy(window) # this is special for this example, see https://discourse.gnome.org/t/tests-testgaction-c/2232/6
main() # 118 lines
main() # 137 lines
----
NOTE: Related work: https://github.com/jdmansour/nim-smartgi