Window creation: Difference between revisions

Updated to work with Nim 1.4 and Nim-Gtk2-1.3. Corrected wrong and inconsistent case of identifiers. Added a context to avoid access to global var in callback.
m (→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics)
(Updated to work with Nim 1.4 and Nim-Gtk2-1.3. Corrected wrong and inconsistent case of identifiers. Added a context to avoid access to global var in callback.)
Line 1,188:
=== gtk2 ===
This is example 9 from the Araq/Nim github repository (modified to include a quit button)
<lang nim>import</lang>
import gdk2, glib2, gtk2
 
proc thisDestroy(widget: pWidget, data: pgpointer){.cdecl.} =
main_quit()
 
const
Inside: cstring = "Mouse is over label"
OutSide: cstring = "Mouse is not over label"
 
# Context transmitted to callback.
var
type Context = object
OverButton: bool
label: PLabel
OverButtonoverButton: bool
 
 
proc ChangeLabelchangeLabel(Pp: PWidget,; Eventevent: gdk2.PEventCrossing,; context: var Context) {.cdecl.} =
context.label.set_text(if context.overButton: OutSide else: Inside)
context.overButton = not context.overButton
 
proc thisDestroy(widget: pWidgetPWidget, data: pgpointerPgpointer) {.cdecl.} =
main_quit()
 
 
var context: Context
nim_init()
var window = window_new(gtk2.WINDOW_TOPLEVEL)
var stackbox = vbox_new(TRUE, 10)
var button1 = button_new("Move mouse over button")
var buttonstyle = copy(get_style(Button1))
ButtonStyle.bg[STATE_PRELIGHT].pixel = 0
ButtonStyle.bg[STATE_PRELIGHT].red = -1'i16
ButtonStyle.bg[STATE_PRELIGHT].blue = 0'i16
ButtonStyle.bg[STATE_PRELIGHT].green = 0'i16
set_style(button1, buttonstyle)
var button2 = button_new()
var ALabel = label_new(Outside)
var button3 = button_new("Quit")
 
varlet window = window_new(gtk2.WINDOW_TOPLEVEL)
varlet stackbox = vbox_new(TRUEtrue, 10)
varlet button1 = button_new("Move mouse over button")
varlet buttonstyle = copy(button1.get_style(Button1))
buttonstyle.bg[STATE_PRELIGHT] = TColor(pixel: 0, red: 255, green: 0, blue: 0)
button1.set_style(button1, buttonstyle)
varlet button2 = button_new()
context = Context(label: label_new(Outside), overButton: false)
varlet button3 = button_new("Quit")
 
button2.add(context.label)
stackbox.pack_start(stackbox, button1, TRUEtrue, TRUEtrue, 0)
stackbox.pack_start(stackbox, button2, TRUEtrue, TRUEtrue, 0)
stackbox.pack_start(stackbox, button3, TRUEtrue, TRUEtrue, 0)
window.set_border_width(Window, 5)
window.add(window, stackbox)
 
discard window.signal_connect("destroy", SIGNAL_FUNC(thisDestroy), nil)
proc ChangeLabel(P: PWidget, Event: gdk2.PEventCrossing,
discard button1.signal_connect(button1, "enter_notify_event", SIGNAL_FUNC(changeLabel), addr(context))
Data: var bool){.cdecl.} =
discard button1.signal_connect(button1, "leave_notify_event", SIGNAL_FUNC(changeLabel), addr(context))
if Not Data: set_text(ALabel, Inside)
discard button3.signal_connect("clicked", SIGNAL_FUNC(thisDestroy), nil)
else: set_text(ALabel, Outside)
Data = Not Data
 
window.show_all(window)
add(button2, ALAbel)
pack_start(stackbox, button1, TRUE, TRUE, 0)
pack_start(stackbox, button2, TRUE, TRUE, 0)
pack_start(stackbox, button3, TRUE, TRUE, 0)
set_border_width(Window, 5)
add(window, stackbox)
discard signal_connect(window, "destroy",
SIGNAL_FUNC(thisDestroy), nil)
overbutton = False
discard signal_connect(button1, "enter_notify_event",
SIGNAL_FUNC(ChangeLabel), addr(OverButton))
discard signal_connect(button1, "leave_notify_event",
SIGNAL_FUNC(ChangeLabel), addr(OverButton))
discard signal_connect(button3, "clicked",
SIGNAL_FUNC(thisDestroy), nil)
show_all(window)
main()</lang>
 
Anonymous user