summary refs log tree commit diff stats
path: root/examples/gtk
diff options
context:
space:
mode:
Diffstat (limited to 'examples/gtk')
-rw-r--r--examples/gtk/ex1.nim14
-rw-r--r--examples/gtk/ex2.nim21
-rw-r--r--examples/gtk/ex3.nim39
-rw-r--r--examples/gtk/ex4.nim31
-rw-r--r--examples/gtk/ex5.nim24
-rw-r--r--examples/gtk/ex6.nim52
-rw-r--r--examples/gtk/ex7.nim43
-rw-r--r--examples/gtk/ex8.nim32
-rw-r--r--examples/gtk/ex9.nim47
9 files changed, 303 insertions, 0 deletions
diff --git a/examples/gtk/ex1.nim b/examples/gtk/ex1.nim
new file mode 100644
index 000000000..aa3ed2f66
--- /dev/null
+++ b/examples/gtk/ex1.nim
@@ -0,0 +1,14 @@
+import 

+  cairo, glib2, gtk2

+

+proc destroy(widget: pGtkWidget, data: pgpointer) {.cdecl.} =

+  gtk_main_quit()

+

+var

+  window: pGtkWidget

+gtk_nimrod_init()

+window = gtk_window_new(GTK_WINDOW_TOPLEVEL)

+discard gtk_signal_connect(GTKOBJECT(window), "destroy",

+                   GTK_SIGNAL_FUNC(destroy), nil)

+gtk_widget_show(window)

+gtk_main()

diff --git a/examples/gtk/ex2.nim b/examples/gtk/ex2.nim
new file mode 100644
index 000000000..80ff6f104
--- /dev/null
+++ b/examples/gtk/ex2.nim
@@ -0,0 +1,21 @@
+
+import 
+  glib2, gtk2
+
+proc destroy(widget: pGtkWidget, data: pgpointer){.cdecl.} = 
+  gtk_main_quit()
+
+var 
+  window: PGtkWidget
+  button: PGtkWidget
+
+gtk_nimrod_init()
+window = gtk_window_new(GTK_WINDOW_TOPLEVEL)
+button = gtk_button_new_with_label("Click me")
+gtk_container_set_border_width(GTK_CONTAINER(Window), 5)
+gtk_container_add(GTK_Container(window), button)
+discard gtk_signal_connect(GTKOBJECT(window), "destroy", 
+                           GTK_SIGNAL_FUNC(destroy), nil)
+gtk_widget_show(button)
+gtk_widget_show(window)
+gtk_main()
diff --git a/examples/gtk/ex3.nim b/examples/gtk/ex3.nim
new file mode 100644
index 000000000..460b1e4dc
--- /dev/null
+++ b/examples/gtk/ex3.nim
@@ -0,0 +1,39 @@
+
+import 
+  glib2, gtk2
+
+proc newbutton(ALabel: cstring): PGtkWidget = 
+  Result = gtk_button_new_with_label(ALabel)
+  gtk_widget_show(result)
+
+proc destroy(widget: pGtkWidget, data: pgpointer){.cdecl.} = 
+  gtk_main_quit()
+
+var 
+  window, totalbox, hbox, vbox: PgtkWidget
+
+gtk_nimrod_init()
+window = gtk_window_new(GTK_WINDOW_TOPLEVEL) # Box to divide window in 2 halves:
+totalbox = gtk_vbox_new(true, 10)
+gtk_widget_show(totalbox)   # A box for each half of the screen:
+hbox = gtk_hbox_new(false, 5)
+gtk_widget_show(hbox)
+vbox = gtk_vbox_new(true, 5)
+gtk_widget_show(vbox)       # Put boxes in their halves
+gtk_box_pack_start(GTK_BOX(totalbox), hbox, true, true, 0)
+gtk_box_pack_start(GTK_BOX(totalbox), vbox, true, true, 0) # Now fill boxes with buttons.
+                                                           # Horizontal box
+gtk_box_pack_start(GTK_BOX(hbox), newbutton("Button 1"), false, false, 0)
+gtk_box_pack_start(GTK_BOX(hbox), newbutton("Button 2"), false, false, 0)
+gtk_box_pack_start(GTK_BOX(hbox), newbutton("Button 3"), false, false, 0) # 
+                                                                          # Vertical box
+gtk_box_pack_start(GTK_BOX(vbox), newbutton("Button A"), true, true, 0)
+gtk_box_pack_start(GTK_BOX(vbox), newbutton("Button B"), true, true, 0)
+gtk_box_pack_start(GTK_BOX(vbox), newbutton("Button C"), true, true, 0) # Put 
+                                                                        # totalbox in window
+gtk_container_set_border_width(GTK_CONTAINER(Window), 5)
+gtk_container_add(GTK_Container(window), totalbox)
+discard gtk_signal_connect(GTKOBJECT(window), "destroy", 
+                           GTK_SIGNAL_FUNC(destroy), nil)
+gtk_widget_show(window)
+gtk_main()
diff --git a/examples/gtk/ex4.nim b/examples/gtk/ex4.nim
new file mode 100644
index 000000000..a387da972
--- /dev/null
+++ b/examples/gtk/ex4.nim
@@ -0,0 +1,31 @@
+
+import 
+  glib2, gtk2
+
+proc newbutton(ALabel: cstring): PGtkWidget = 
+  Result = gtk_button_new_with_label(ALabel)
+  gtk_widget_show(result)
+
+proc destroy(widget: pGtkWidget, data: pgpointer){.cdecl.} = 
+  gtk_main_quit()
+
+var 
+  window, maintable: PgtkWidget
+
+proc AddToTable(Widget: PGtkWidget, Left, Right, Top, Bottom: guint) = 
+  gtk_table_attach_defaults(GTK_TABLE(MainTable), Widget, Left, right, top, 
+                            bottom)
+
+gtk_nimrod_init()
+window = gtk_window_new(GTK_WINDOW_TOPLEVEL)
+Maintable = gtk_table_new(6, 6, True)
+gtk_widget_show(MainTable)
+AddToTable(newbutton("1,1 At 1,1"), 1, 2, 1, 2)
+AddToTable(newbutton("2,2 At 3,1"), 3, 5, 1, 3)
+AddToTable(newbutton("4,1 At 4,1"), 1, 5, 4, 5) # Put all in window
+gtk_container_set_border_width(GTK_CONTAINER(Window), 5)
+gtk_container_add(GTK_Container(window), maintable)
+discard gtk_signal_connect(GTKOBJECT(window), "destroy", 
+                           GTK_SIGNAL_FUNC(destroy), nil)
+gtk_widget_show(window)
+gtk_main()
diff --git a/examples/gtk/ex5.nim b/examples/gtk/ex5.nim
new file mode 100644
index 000000000..3a5b076c6
--- /dev/null
+++ b/examples/gtk/ex5.nim
@@ -0,0 +1,24 @@
+
+import 
+  glib2, gtk2
+
+proc destroy(widget: pGtkWidget, data: pgpointer){.cdecl.} = 
+  gtk_main_quit()
+
+var 
+  window: PGtkWidget
+  button: PGtkWidget
+
+gtk_nimrod_init()
+window = gtk_window_new(GTK_WINDOW_TOPLEVEL)
+button = gtk_button_new_with_label("Click me")
+gtk_container_set_border_width(GTK_CONTAINER(Window), 5)
+gtk_container_add(GTK_Container(window), button)
+discard gtk_signal_connect(GTKOBJECT(window), "destroy", 
+                           GTK_SIGNAL_FUNC(destroy), nil)
+discard gtk_signal_connect_object(GTKOBJECT(button), "clicked", 
+                                  GTK_SIGNAL_FUNC(gtk_widget_destroy), 
+                                  GTKOBJECT(window))
+gtk_widget_show(button)
+gtk_widget_show(window)
+gtk_main()
diff --git a/examples/gtk/ex6.nim b/examples/gtk/ex6.nim
new file mode 100644
index 000000000..5f18786fe
--- /dev/null
+++ b/examples/gtk/ex6.nim
@@ -0,0 +1,52 @@
+
+import 
+  glib2, gtk2
+
+type 
+  TButtonSignalState = record 
+    Obj: PgtkObject
+    SignalID: int32
+    Disable: bool
+
+  PButtonSignalState = ptr TButtonSignalState
+
+proc destroy(widget: pGtkWidget, data: pgpointer){.cdecl.} = 
+  gtk_main_quit()
+
+proc disablesignal(widget: pGtkWidget, data: pgpointer){.cdecl.} = 
+  if PButtonSignalState(Data).Disable: 
+    gtk_signal_handler_block(PButtonSignalState(Data).Obj, SignalID)
+  else: 
+    gtk_signal_handler_unblock(PButtonSignalState(Data).Obj, SignalID)
+  PButtonSignalState(Data).disable = not PButtonSignalState(Data).disable
+
+var 
+  window: PGtkWidget
+  quitbutton: PGtkWidget
+  disablebutton: PGTKWidget
+  windowbox: PGTKWidget
+  quitsignal: guint
+  QuitState: TButtonSignalState
+
+gtk_nimrod_init()
+window = gtk_window_new(GTK_WINDOW_TOPLEVEL)
+quitbutton = gtk_button_new_with_label("Quit program")
+disablebutton = gtk_button_new_with_label("Disable button")
+windowbox = gtk_vbox_new(TRUE, 10)
+gtk_box_pack_start(GTK_BOX(windowbox), disablebutton, True, false, 0)
+gtk_box_pack_start(GTK_BOX(windowbox), quitbutton, True, false, 0)
+gtk_container_set_border_width(GTK_CONTAINER(Window), 10)
+gtk_container_add(GTK_Container(window), windowbox)
+gtk_signal_connect(GTKOBJECT(window), "destroy", 
+                   GTK_SIGNAL_FUNC(destroy), nil)
+QuitState.Obj = GTKObject(QuitButton)
+SignalID = gtk_signal_connect_object(QuitState.Obj, "clicked", GTK_SIGNAL_FUNC(
+              gtk_widget_destroy), GTKOBJECT(window))
+QuitState.Disable = True
+discard gtk_signal_connect(GTKOBJECT(disablebutton), "clicked", 
+                   GTK_SIGNAL_FUNC(disablesignal), addr(QuitState))
+gtk_widget_show(quitbutton)
+gtk_widget_show(disablebutton)
+gtk_widget_show(windowbox)
+gtk_widget_show(window)
+gtk_main()
diff --git a/examples/gtk/ex7.nim b/examples/gtk/ex7.nim
new file mode 100644
index 000000000..53890d1e1
--- /dev/null
+++ b/examples/gtk/ex7.nim
@@ -0,0 +1,43 @@
+
+import 
+  gdk2, glib2, gtk2
+
+proc destroy(widget: pGtkWidget, data: pgpointer){.cdecl.} = 
+  gtk_main_quit()
+
+const 
+  Inside: cstring = "Mouse is over label"
+  OutSide: cstring = "Mouse is not over label"
+
+var 
+  OverLabel: bool
+  window, box1, box2, stackbox, label1, Label2: PGtkWidget
+
+proc ChangeLabel(P: PGtkWidget, Event: PGdkEventCrossing, 
+                Data: var bool){.cdecl.} = 
+  if not Data: gtk_label_set_text(GTKLABEL(Label2), Inside)
+  else: gtk_label_set_text(GTKLABEL(Label2), Outside)
+  Data = not Data
+
+gtk_nimrod_init()
+window = gtk_window_new(GTK_WINDOW_TOPLEVEL)
+stackbox = gtk_vbox_new(TRUE, 10)
+box1 = gtk_event_box_new()
+label1 = gtk_label_new("Move mouse over label")
+gtk_container_add(GTK_CONTAINER(box1), label1)
+box2 = gtk_event_box_new()
+label2 = gtk_label_new(OutSide)
+gtk_container_add(GTK_CONTAINER(box2), label2)
+gtk_box_pack_start(GTK_BOX(stackbox), box1, TRUE, TRUE, 0)
+gtk_box_pack_start(GTK_BOX(stackbox), box2, TRUE, TRUE, 0)
+gtk_container_set_border_width(GTK_CONTAINER(Window), 5)
+gtk_container_add(GTK_Container(window), stackbox)
+discard gtk_signal_connect(GTKOBJECT(window), "destroy", 
+                   GTK_SIGNAL_FUNC(destroy), nil)
+overlabel = False
+discard gtk_signal_connect(GTKOBJECT(box1), "enter_notify_event", 
+                   GTK_SIGNAL_FUNC(ChangeLabel), addr(Overlabel))
+discard gtk_signal_connect(GTKOBJECT(box1), "leave_notify_event", 
+                   GTK_SIGNAL_FUNC(ChangeLabel), addr(Overlabel))
+gtk_widget_show_all(window)
+gtk_main()
diff --git a/examples/gtk/ex8.nim b/examples/gtk/ex8.nim
new file mode 100644
index 000000000..acbba2258
--- /dev/null
+++ b/examples/gtk/ex8.nim
@@ -0,0 +1,32 @@
+
+import 
+  glib2, gtk2
+
+proc destroy(widget: pGtkWidget, data: pgpointer){.cdecl.} = 
+  gtk_main_quit()
+
+var 
+  window, stackbox, label1, Label2: PGtkWidget
+  labelstyle: pgtkstyle
+
+gtk_nimrod_init()
+window = gtk_window_new(GTK_WINDOW_TOPLEVEL)
+stackbox = gtk_vbox_new(TRUE, 10)
+label1 = gtk_label_new("Red label text")
+labelstyle = gtk_style_copy(gtk_widget_get_style(label1))
+LabelStyle.fg[GTK_STATE_NORMAL].pixel = 0
+LabelStyle.fg[GTK_STATE_NORMAL].red = 0x0000FFFF
+LabelStyle.fg[GTK_STATE_NORMAL].blue = 0
+LabelStyle.fg[GTK_STATE_NORMAL].green = 0
+gtk_widget_set_style(label1, labelstyle) # Uncomment this to see the effect of setting the default style.
+                                         # 
+                                         # gtk_widget_set_default_style(labelstyle)
+label2 = gtk_label_new("Black label text")
+gtk_box_pack_start(GTK_BOX(stackbox), label1, TRUE, TRUE, 0)
+gtk_box_pack_start(GTK_BOX(stackbox), label2, TRUE, TRUE, 0)
+gtk_container_set_border_width(GTK_CONTAINER(Window), 5)
+gtk_container_add(GTK_Container(window), stackbox)
+discard gtk_signal_connect(GTKOBJECT(window), "destroy", 
+                   GTK_SIGNAL_FUNC(destroy), nil)
+gtk_widget_show_all(window)
+gtk_main()
diff --git a/examples/gtk/ex9.nim b/examples/gtk/ex9.nim
new file mode 100644
index 000000000..ce2f73862
--- /dev/null
+++ b/examples/gtk/ex9.nim
@@ -0,0 +1,47 @@
+
+import 
+  gdk2, glib2, gtk2
+
+proc destroy(widget: pGtkWidget, data: pgpointer){.cdecl.} = 
+  gtk_main_quit()
+
+const 
+  Inside: cstring = "Mouse is over label"
+  OutSide: cstring = "Mouse is not over label"
+
+var 
+  window, button1, Button2, Alabel, stackbox: PGtkWidget
+  buttonstyle: pgtkstyle
+  OverButton: bool
+
+proc ChangeLabel(P: PGtkWidget, Event: PGdkEventCrossing, Data: var bool){.cdecl.} = 
+  if Not Data: gtk_label_set_text(GTKLABEL(ALabel), Inside)
+  else: gtk_label_set_text(GTKLABEL(ALabel), Outside)
+  Data = Not Data
+
+gtk_nimrod_init()
+window = gtk_window_new(GTK_WINDOW_TOPLEVEL)
+stackbox = gtk_vbox_new(TRUE, 10)
+button1 = gtk_button_new_with_label("Move mouse over button")
+buttonstyle = gtk_style_copy(gtk_widget_get_style(Button1))
+ButtonStyle.bg[GTK_STATE_PRELIGHT].pixel = 0
+ButtonStyle.bg[GTK_STATE_PRELIGHT].red = 0x0000FFFF'i16
+ButtonStyle.bg[GTK_STATE_PRELIGHT].blue = 0'i16
+ButtonStyle.bg[GTK_STATE_PRELIGHT].green = 0'i16
+gtk_widget_set_style(button1, buttonstyle)
+button2 = gtk_button_new()
+ALabel = gtk_label_new(Outside)
+gtk_container_add(GTK_CONTAINER(button2), ALAbel)
+gtk_box_pack_start(GTK_BOX(stackbox), button1, TRUE, TRUE, 0)
+gtk_box_pack_start(GTK_BOX(stackbox), button2, TRUE, TRUE, 0)
+gtk_container_set_border_width(GTK_CONTAINER(Window), 5)
+gtk_container_add(GTK_Container(window), stackbox)
+discard gtk_signal_connect(GTKOBJECT(window), "destroy", 
+                   GTK_SIGNAL_FUNC(destroy), nil)
+overbutton = False
+discard gtk_signal_connect(GTKOBJECT(button1), "enter_notify_event", 
+                   GTK_SIGNAL_FUNC(ChangeLabel), addr(OverButton))
+discard gtk_signal_connect(GTKOBJECT(button1), "leave_notify_event", 
+                   GTK_SIGNAL_FUNC(ChangeLabel), addr(OverButton))
+gtk_widget_show_all(window)
+gtk_main()