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 @@
# app0.nim -- minimal application style example
# nim c app0.nim
import gintro/[gtk, glib, gobject, gio]
import gintro/[gtk, gobject, gio]
proc appActivate(app: Application) =
let window = newApplicationWindow(app)

View file

@ -1,7 +1,7 @@
# https://developer.gnome.org/gtk3/stable/ch01s03.html
# builder.nim -- application style example using builder/glade xml file for user interface
# nim c builder.nim
import gintro/[gtk, glib, gobject, gio]
import gintro/[gtk, gobject, gio]
proc hello(b: Button; msg: string) =
echo "Hello", msg

View file

@ -1,6 +1,6 @@
# This example shows how to apply a CellDataFunc to a GtkTreeView
# C example code was provided by A.Krause in chapter 8 of his book
import gintro/[gtk, gobject, glib]
import gintro/[gtk, gobject]
const
Color = 0

View file

@ -0,0 +1,55 @@
# nim c button.nim
import gintro/[gtk, glib, gobject, gio, cairo]
from math import Tau
type
ColorPicker = ref object of Button
c: array[4, float]
proc newColorPicker(c: array[4, float]): ColorPicker =
result = newButton(ColorPicker)
result.c = c
proc buttonClicked (button: ColorPicker) =
#button.label = utf8Strreverse(button.label, -1)
echo "clicked"
proc draw(b: ColorPicker; cr: cairo.Context): bool =
echo "draw"
let x = getAllocatedWidth(b).float / 2
let y = getAllocatedHeight(b).float / 2
cr.moveTo(x, y)
cr.newPath
cr.arc(x, y, 10, 0, math.Tau)
cr.setSource(b.c)
cr.setLineWidth(5)
cr.stroke
proc appActivate (app: Application) =
let window = newApplicationWindow(app)
window.title = "GNOME Button"
window.defaultSize = (250, 50)
let box = newBox(Orientation.horizontal, 0)
var button = newColorPicker([1.0, 0, 0, 1])#newButton(ColorPicker)#"Click Me")
box.add(button)
button.connect("clicked", buttonClicked)
button.connectAfter("draw", draw)
button = newColorPicker([0.0, 0, 1, 1])#newButton(ColorPicker)#"Click Me")
box.add(button)
button.connect("clicked", buttonClicked)
button.connectAfter("draw", draw)
button = newColorPicker([0.0, 1, 0, 1])#newButton(ColorPicker)#"Click Me")
box.add(button)
button.connect("clicked", buttonClicked)
button.connectAfter("draw", draw)
window.add(box)
window.showAll
proc main =
let app = newApplication("org.gtk.example")
connect(app, "activate", appActivate)
discard app.run
main()

View file

@ -60,7 +60,7 @@ type
handlerID: uint64
proc newPosAdj: PosAdj =
initAdjustment(result, 0, 0, 1, 1, 10, 1)
newAdjustment(PosAdj, 0, 0, 1, 1, 10, 1)
type
PDA_Data* = object
@ -104,7 +104,7 @@ proc drawingAreaDrawCb(darea: DrawingArea; cr: Context; this: PDA): bool =
if this.selecting:
cr.rectangle(this.lastButtonDownPosX, this.lastButtonDownPosY,
this.zoomRectX1 - this.lastButtonDownPosX, this.zoomRectY1 - this.lastButtonDownPosY)
cr.setSource(0, 0, 1, 0.5) # SELECT_RECT_COL) # 0, 0, 1, 0.5
cr.setSource(SelectRectCol) # SELECT_RECT_COL) # 0, 0, 1, 0.5
cr.fillPreserve
cr.setSource(0, 0, 0)
cr.setLineWidth(2)
@ -256,7 +256,7 @@ proc onAdjustmentEvent(this: PosAdj; pda: PDA) =
pda.darea.queueDrawArea(0, 0, pda.darea.allocatedWidth, pda.darea.allocatedHeight)
proc newPDA: PDA =
initGrid(result)
result = newGrid(PDA)
let da = newDrawingArea()
result.darea = da
da.setHExpand

View file

@ -62,9 +62,9 @@ proc radio(action: SimpleAction; parameter: Variant; label: Label) =
let str: string = "From Radio menu item " & getString(newState, l)
label.setLabel(str)
proc bye(w: Window) =
mainQuit()
echo "Bye..."
#proc bye(w: Window) =
# mainQuit()
# echo "Bye..."
proc main =
gtk.init()

View file

@ -1,13 +1,13 @@
# gsettings.nim -- basic use of gsettings
# nim c gsettings.nim
# https://blog.gtk.org/2017/05/01/first-steps-with-gsettings/
import gintro/[gtk, glib, gobject, gio]
import gintro/[gtk, gobject, gio]
# unused
proc toggle(b: CheckButton) =
echo b.active
let s = newSettings("org.gnome.Recipes")
discard s.setBoolean("like-nim", b.active)
#proc toggle(b: CheckButton) =
# echo b.active
# let s = newSettings("org.gnome.Recipes")
# discard s.setBoolean("like-nim", b.active)
proc appActivate(app: Application) =
let window = newApplicationWindow(app)

View file

@ -15,6 +15,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()

View file

@ -53,7 +53,7 @@ type
handlerID: uint64
proc newPosAdj: PosAdj =
initAdjustment(result, 0, 0, 1, 1, 10, 1)
newAdjustment(PosAdj, 0, 0, 1, 1, 10, 1)
type
PDA_Data* = object
@ -98,7 +98,7 @@ proc drawingAreaDrawCb(darea: DrawingArea; cr: Context; this: PDA): bool =
if this.selecting:
cr.rectangle(this.lastButtonDownPosX, this.lastButtonDownPosY,
this.zoomRectX1 - this.lastButtonDownPosX, this.zoomRectY1 - this.lastButtonDownPosY)
cr.setSource(0, 0, 1, 0.5) # SELECT_RECT_COL) # 0, 0, 1, 0.5
cr.setSource(SelectRectCol) # SELECT_RECT_COL) # 0, 0, 1, 0.5
cr.fillPreserve
cr.setSource(0, 0, 0)
cr.setLineWidth(2)
@ -227,7 +227,7 @@ proc onAdjustmentEvent(this: PosAdj; pda: PDA) =
pda.darea.queueDrawArea(0, 0, pda.darea.allocatedWidth, pda.darea.allocatedHeight)
proc newPDA: PDA =
initGrid(result)
result = newGrid(PDA)
let da = newDrawingArea()
result.darea = da
da.setHExpand

View file

@ -1,4 +1,4 @@
import gintro/[gtk, glib, gobject, gio, gtksource]
import gintro/[gtk, gobject, gio, gtksource]
proc appActivate (app: Application) =
let window = newApplicationWindow(app)

View file

@ -1,6 +1,6 @@
# app0.nim -- minimal application style example
# nim c app0.nim
import gintro/[gtk4, glib, gobject, gio]
import gintro/[gtk4, gobject, gio]
proc appActivate(app: Application) =
let window = newApplicationWindow(app)

View file

@ -1,6 +1,8 @@
# 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 = """
@ -22,7 +24,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)
@ -34,11 +36,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
@ -54,65 +56,82 @@ 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