futhark icon indicating copy to clipboard operation
futhark copied to clipboard

basic gtk3 code

Open vaguinerg opened this issue 6 months ago • 1 comments

hello, please could you provide the minimal needed to just open an gtk3 window?

vaguinerg avatar Aug 07 '25 04:08 vaguinerg

Not exactly minimal, but should be easy enough to strip away the things you don't need. This is what I use for a couple of programs I've written using Gtk with a webview:

{.passL: "`pkg-config gtk+-3.0 webkit2gtk-4.1 gtksourceview-4 pango gthread-2.0 --libs`".}
when defined(useFuthark) or defined(useFutharkForGtk):
  import futhark
  import strutils, os

  proc renameCallback(name, kind: string, partOf = ""): string =
    result = name
    result.removePrefix("gtk_")
    result.removePrefix("webkit_")
    if result != "web_view_new":
      result.removePrefix("web_view_")
    if result != "g_object_set":
      result.removePrefix("g_object_")
    result.removePrefix("jsc_")
    result.removePrefix("javascript_result_")
    result.removePrefix("box_")
    if result != "file_chooser_native_new":
      result.removePrefix("file_chooser_")
    if result != "file_filter_new":
      result.removePrefix("file_filter_")
    #if name.startsWith("gtk_"):
    #  result = name[4..^1]

  importc:
    path "/usr/include/gtksourceview-4/"
    path "/usr/include/webkitgtk-4.1/webkit"
    path "/usr/include/webkitgtk-4.1/webkit2"
    path "/usr/include/webkitgtk-4.1/webkitdom"
    path "/usr/include/webkitgtk-4.1/JavaScriptCore"
    path "/usr/include/webkitgtk-4.1/jsc"
    path "/usr/include/gtk-3.0"
    path "/usr/include/gio-2.0"
    path "/usr/include/glib-2.0"
    path "/usr/include/pango-1.0"
    compilerarg "`pkg-config gtk+-3.0 webkit2gtk-4.1 gtksourceview-4 pango --cflags`"
    renameCallback renameCallback
    "gdk/gdk.h"
    "gtk/gtk.h"
    "glib.h"
    "gobject/gsignal.h"
    "webkit2/webkit2.h"
    "webkitdom/webkitdom.h"
    "JavaScriptCore/JavaScript.h"
    "jsc/jsc.h"
    "gio/gcontenttype.h"
    "gtksourceview/gtksource.h"
    "pango/pango.h"
    outputPath currentSourcePath.parentDir / "futhark_gtk.nim"
else:
  include "futhark_gtk.nim"

converter cstringToPtrGchar*(str: cstring): ptr gchar = cast[ptr gchar](str)
converter toWidget*(p: ptr WebkitWebView): ptr GtkWidget = cast[ptr GtkWidget](p)
converter toGboolean*(b: bool): gboolean = b.gboolean

proc widget*(x: auto): ptr GtkWidget = cast[ptr GtkWidget](x) # {.importc: "GTK_WIDGET".}
proc container*(x: auto): ptr GtkContainer = cast[ptr GtkContainer](x) # {.importc: "GTK_WIDGET".}
proc bin*(x: auto): ptr GtkBin = cast[ptr GtkBin](x) # {.importc: "GTK_WIDGET".}
proc label*(x: auto): ptr GtkLabel = cast[ptr GtkLabel](x) # {.importc: "GTK_WIDGET".}
proc box*(x: auto): ptr GtkBox = cast[ptr GtkBox](x) # {.importc: "GTK_WIDGET".}

iterator children*(x: auto): ptr GtkWidget =
  let children = containerGetChildren(container(x))
  defer: gListFree(children)
  var iter = children
  while not iter.isNil:
    yield cast[ptr GtkWidget](iter.data)
    iter = iter.next

iterator childrenPairs*(x: auto): (int, ptr GtkWidget) =
  let children = containerGetChildren(container(x))
  defer: gListFree(children)
  var
    iter = children
    i = 0
  while not iter.isNil:
    yield (i, cast[ptr GtkWidget](iter.data))
    iter = iter.next
    inc i

proc nthChild*(x: auto, nth: int): ptr GtkWidget =
  let children = containerGetChildren(container(x))
  defer: gListFree(children)
  var
    iter = children
    i = 0
  while not iter.isNil and i < nth:
    iter = iter.next
    inc i
  if not iter.isNil:
    result = cast[ptr GtkWidget](iter.data)


let
  G_TYPE_INVALID* = GType(0 shl 2)
  G_TYPE_NONE* {.importc.}: GType
  #G_TYPE_INTERFACE* {.importc.}: GType
  G_TYPE_CHAR* {.importc.}: GType
  G_TYPE_UCHAR* {.importc.}: GType
  G_TYPE_BOOLEAN* {.importc.}: GType
  G_TYPE_INT* {.importc.}: GType
  G_TYPE_UINT* {.importc.}: GType
  G_TYPE_LONG* {.importc.}: GType
  G_TYPE_ULONG* {.importc.}: GType
  G_TYPE_INT64* {.importc.}: GType
  G_TYPE_UINT64* {.importc.}: GType
  G_TYPE_ENUM* {.importc.}: GType
  #G_TYPE_FLAGS* {.importc.}: GType
  G_TYPE_FLOAT* {.importc.}: GType
  G_TYPE_DOUBLE* {.importc.}: GType
  G_TYPE_STRING* = GType(16 shl 2)
  G_TYPE_POINTER* {.importc.}: GType
  G_TYPE_BOXED* {.importc.}: GType
  G_TYPE_PARAM* {.importc.}: GType
  G_TYPE_OBJECT* {.importc.}: GType

PMunch avatar Aug 11 '25 11:08 PMunch