Created
June 5, 2015 17:50
-
-
Save glurp/9e2d51dbdf3f6754752f to your computer and use it in GitHub Desktop.
gol 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() | |
| proc CPWIDGET(obj: pointer): PWidget = | |
| result = cast[PWidget](obj) | |
| proc CPBOX(obj: pointer): PBox= | |
| result = cast[PBox](obj) | |
| ######################################## 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 frame(title: string,code: stmt): stmt {.immediate.} = | |
| let content = CPBOX(frame_new(title)) | |
| 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() | |
| ################################################## Canvas | |
| var gcurrentCanvas : PDrawingArea | |
| template canvas(width,height: int,code: stmt) : stmt {.immediate.} = | |
| let cv= drawing_area_new() | |
| #cv.set_size_request*(width, height) | |
| cv.add_events(BUTTON_PRESS_MASK + BUTTON_MOTION_MASK + KEY_PRESS_MASK) | |
| #cv.can_focus = true | |
| gcurrentCanvas=cv | |
| code | |
| #gcurrentCanvas=0 | |
| append( cv ) | |
| template handle_expose(exposeProc: proc) : stmt {.immediate.} = | |
| discard gcurrentCanvas.signal_connect( "draw", SIGNAL_FUNC(exposeProc), gcurrentCanvas) | |
| template handle_button(clickProc: proc) : stmt {.immediate.} = | |
| discard gcurrentCanvas.signal_connect( "click", SIGNAL_FUNC(clickProc), gcurrentCanvas) | |
| ################################################## Widgets | |
| proc nulproc() = | |
| echo("e") | |
| proc bbutton(label: string, f = nulproc,sloti: bool) = | |
| 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) | |
| if sloti: appendi(btn) else: append( btn ) | |
| proc button(label: string, f = nulproc) = | |
| bbutton(label,f,false) | |
| proc buttoni(label: string, f = nulproc) = | |
| bbutton(label,f,true) | |
| proc blabel(label: string,sloti: bool) = | |
| var btn =if (label.existsFile()) or (label.find(re"^gtk-") > -1): | |
| if existsFile(label): | |
| image_new_from_file(label) | |
| else: | |
| imageNewFromStock(label, ICON_SIZE_BUTTON) | |
| else: | |
| label_new(label) | |
| if sloti: appendi(btn) else: append( btn ) | |
| proc label(label: string) = | |
| blabel(label,false) | |
| proc labeli(label: string) = | |
| blabel(label,true) | |
| proc image(name: string) = | |
| var w= if existsFile(name): | |
| image_new_from_file(name) | |
| else: | |
| imageNewFromStock(name, ICON_SIZE_BUTTON) | |
| append( w ) | |
| proc pass(n: int = 1) = | |
| label( " " ) | |
| ################################################################ | |
| # M A I N | |
| ################################################################ | |
| "Test Canvas".gtk_app(200,230): | |
| stack: | |
| flowi: | |
| pass() | |
| labeli("gtk-help") | |
| canvas(200,200): | |
| handle_expose proc() = (echo("canvas expose event")) | |
| handle_button proc() = (echo("button on canvas")) | |
| flowi: | |
| button "gtk-clear",proc () = (echo "button clear !!") | |
| button "gtk-help",proc () = (echo "button random!!") | |
| button "gtk-open",proc () = (echo "button run!!") | |
| button "gtk-close",proc () = ( main_quit() ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment