Created
June 5, 2015 12:54
-
-
Save glurp/86f837372a31f6afcf04 to your computer and use it in GitHub Desktop.
DSL for GTK2 in Nim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import | |
| re, os, gtk2, gdk2, glib2, strutils, sequtils, math, typeinfo, macros | |
| ###################################### Engine | |
| ############ Stack layout | |
| var | |
| gstack : seq[PBox] = @[] | |
| glastWidget : PWidget | |
| proc last[T](st: seq[T]) : T = | |
| result=st[st.high] | |
| proc push[T](st: seq[T], e: T) : T = | |
| st.add(e) | |
| result=e | |
| # pop already defined ... | |
| proc append(w: PWidget) : void = | |
| glastWidget=w | |
| gstack.last.pack_start(w,true,true,0) | |
| proc appendi(w: PWidget): void = | |
| glastWidget=w | |
| gstack.last.pack_start(w,false,false,0) | |
| proc option() = | |
| echo("coucou") | |
| ############ | |
| template gtk_app(title: string,width,height: int,code: stmt): stmt {.immediate.} = | |
| nim_init() | |
| let win = window_new(gtk2.WINDOW_TOPLEVEL) | |
| let content = vbox_new(true,10) | |
| win.set_title("Gtk: " & title) | |
| win.set_default_size(width,height) | |
| gstack.add(content) | |
| code | |
| win.add(content) | |
| win.show_all() | |
| discard signal_connect(win, "destroy", SIGNAL_FUNC(mainQuit), nil) | |
| main() | |
| proc mainQuit(widget: PWidget, data: Pgpointer) {.cdecl.} = | |
| main_quit() | |
| ######################################## Layout | |
| template stack(code: stmt): stmt {.immediate.} = | |
| let content = vbox_new(false,0) | |
| append(content) | |
| apply(content,code) | |
| template flow(code: stmt): stmt {.immediate.} = | |
| let content = hbox_new(false,0) | |
| append(content) | |
| apply(content,code) | |
| template stacki(code: stmt): stmt {.immediate.} = | |
| let content = vbox_new(false,0) | |
| appendi(content) | |
| apply(content,code) | |
| template flowi(code: stmt): stmt {.immediate.} = | |
| let content = hbox_new(false,30) | |
| appendi(content) | |
| apply(content,code) | |
| template apply(w:PBox,code: stmt) : stmt {.immediate.} = | |
| gstack.add(w) | |
| code | |
| discard gstack.pop() | |
| ################################################## Widgets | |
| proc nulproc() = | |
| echo("e") | |
| proc button(label: string, f = nulproc) = | |
| var btn = button_new() | |
| if (label.existsFile()) or (label.find(re"^gtk-") > -1): | |
| var w= if existsFile(label): | |
| image_new_from_file(label) | |
| else: | |
| imageNewFromStock(label, ICON_SIZE_BUTTON) | |
| btn.set_image(w) | |
| else: | |
| btn= button_new(label) | |
| if f!=nulproc: | |
| discard btn.signal_connect("pressed", SIGNAL_FUNC(f), btn) | |
| append( btn ) | |
| proc label(label: string) = | |
| append( label_new(label) ) | |
| proc image(name: string) = | |
| var w= if existsFile(name): | |
| image_new_from_file(name) | |
| else: | |
| imageNewFromStock(name, ICON_SIZE_BUTTON) | |
| append( w ) | |
| proc buttoni(label: string, f = nulproc) = | |
| let btn = button_new(label) | |
| if f!=nulproc: | |
| discard btn.signal_connect("pressed", SIGNAL_FUNC(f), btn) | |
| appendi( btn ) | |
| proc labeli(label: string) = | |
| appendi(label_new(label)) | |
| ################################################################ | |
| # M A I N | |
| ################################################################ | |
| "Test gui".gtk_app(0,200): | |
| stack: | |
| button "Aeeee ",proc () = | |
| echo("eee") | |
| echo(".") | |
| flowi: | |
| button "gtk-open",proc () = (echo "button image!!") | |
| image "gtk-close" | |
| image "gtk-help" | |
| labeli "RRRRR " | |
| flowi: | |
| labeli "A " | |
| labeli "B " | |
| flowi: | |
| button "e",proc () = (echo("eee") ; echo("aaa")) | |
| button "r" | |
| label "C---- " | |
| buttoni "\nExit\n",proc () = (main_quit()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment