Data update

This commit is contained in:
Ingy döt Net 2025-02-27 18:35:13 -05:00
parent 8e4e15fa56
commit 72eb4943cb
1853 changed files with 35514 additions and 9441 deletions

View file

@ -1,82 +1,97 @@
import strutils
import gintro/[glib, gobject, gtk, gio]
import std/strformat
import gtk2, glib2
type MainWindow = ref object of ApplicationWindow
strEntry: Entry
intEntry: SpinButton
###############################################################################
# Missing declaration.
#---------------------------------------------------------------------------------------------------
when defined(win32):
const lib = "libgtk-win32-2.0-0.dll"
elif defined(macosx):
const lib = "(libgtk-quartz-2.0.0.dylib|libgtk-x11-2.0.dylib)"
else:
const lib = "libgtk-x11-2.0.so(|.0)"
proc displayValues(strval: string; intval: int) =
proc getContentArea(dialog: PDialog): PVBox {.cdecl,
importc: "gtk_dialog_get_content_area", dynlib: lib.}
###############################################################################
type App = object
window: PWindow
strEntry: PEntry
intEntry: PSpinButton
proc displayValues(app: App; strval: cstring; intval: int) =
## Display a dialog window with the values entered by the user.
let dialog = newDialog()
dialog.setModal(true)
let label1 = newLabel(" String value is “$1”.".format(strval))
label1.setHalign(Align.start)
dialog.contentArea.packStart(label1, true, true, 5)
let msg = " Integer value is $1 which is ".format(intval) &
(if intval == 75000: "right. " else: "wrong (expected 75000). ")
let label2 = newLabel(msg)
dialog.contentArea.packStart(label2, true, true, 5)
discard dialog.addButton("OK", ord(ResponseType.ok))
let dialog = dialogNewWithButtons("user_input_graphical", app.window,
DIALOG_MODAL or DIALOG_DESTROY_WITH_PARENT, "OK")
let label1 = labelNew(cstring(&" String value is “{strval}”."))
let contentArea = dialog.getContentArea()
contentArea.packStart(label1, true, true, 5)
let text = if intval == 75000: "right. " else: "wrong (expected 75000). "
let msg = &" Integer value is {intval} which is {text}"
let label2 = labelNew(msg.cstring)
contentArea.packStart(label2, true, true, 5)
dialog.showAll()
discard dialog.run()
dialog.destroy()
#---------------------------------------------------------------------------------------------------
proc onOk(button: Button; window: MainWindow) =
proc onOk(button: PButton; app: var App) =
## Callback executed when the OK button has been clicked.
let strval = window.strEntry.text()
let intval = window.intEntry.value().toInt
displayValues(strval, intval)
let strval = app.strEntry.getText()
let intval = app.intEntry.getValue().toInt
app.displayValues(strval, intval)
if intval == 75_000:
window.destroy()
app.window.destroy()
#---------------------------------------------------------------------------------------------------
proc activate(app: Application) =
## Activate the application.
proc onDestroyEvent(widget: PWidget; data: pointer): gboolean {.cdecl.} =
## Quit the application.
mainQuit()
let window = newApplicationWindow(MainWindow, app)
window.setTitle("User input")
let content = newBox(Orientation.vertical, 10)
content.setHomogeneous(true)
let grid = newGrid()
grid.setColumnSpacing(30)
let bbox = newButtonBox(Orientation.horizontal)
bbox.setLayout(ButtonBoxStyle.spread)
var app: App
let strLabel = newLabel("Enter some text")
strLabel.setHalign(Align.start)
window.strEntry = newEntry()
grid.attach(strLabel, 0, 0, 1, 1)
grid.attach(window.strEntry, 1, 0, 1, 1)
nimInit()
let intLabel = newLabel("Enter 75000")
intLabel.setHalign(Align.start)
window.intEntry = newSpinButtonWithRange(0, 80_000, 1)
grid.attach(intLabel, 0, 1, 1, 1)
grid.attach(window.intEntry, 1, 1, 1, 1)
app.window = windowNew(WINDOW_TOPLEVEL)
app.window.setTitle("User input")
discard app.window.signalConnect("destroy", SIGNAL_FUNC(onDestroyEvent), nil)
let btnOk = newButton("OK")
let content = vboxNew(false, 10)
content.setHomogeneous(true)
let grid = tableNew(2, 2, false)
grid.setColSpacings(30)
bbox.add(btnOk)
let hbox1 = hboxNew(false, 0)
let strLabel = labelNew("Enter some text")
app.strEntry = entryNew()
hbox1.packStart(strLabel, false, false, 0)
grid.attach(hbox1, 0, 1, 0, 1, constFILL, 0, 0, 0)
grid.attach(app.strEntry, 1, 2, 0, 1, constFILL, 0, 0, 0)
content.packStart(grid, true, true, 0)
content.packEnd(bbox, true, true, 0)
let hbox2 = hboxNew(false, 0)
let intLabel = labelNew("Enter 75000")
app.intEntry = spinButtonNew(0, 80_000, 1)
hbox2.packStart(intLabel, false, false, 0)
grid.attach(hbox2, 0, 1, 1, 2, constFILL, 0, 0, 0)
grid.attach(app.intEntry, 1, 2, 1, 2, constFILL, 0, 0, 0)
window.setBorderWidth(5)
window.add(content)
let btnOk = buttonNew("OK")
btnOk.setSizeRequest(100, 40)
let hbox3 = hboxNew(false, 20)
hbox3.packEnd(btnOk, false, true, 10)
content.packStart(grid, true, true, 0)
content.packEnd(hbox3, false, false, 0)
discard btnOk.connect("clicked", onOk, window)
app.window.setBorderWidth(5)
app.window.add content
window.showAll()
discard btnOk.signalConnect("clicked", SIGNAL_FUNC(onOk), app.addr)
#———————————————————————————————————————————————————————————————————————————————————————————————————
let app = newApplication(Application, "Rosetta.UserInput")
discard app.connect("activate", activate)
discard app.run()
app.window.showAll()
main()

View file

@ -4,11 +4,6 @@ import ui
struct App {
mut:
win &ui.Window = unsafe {nil}
txt_1 &ui.TextBox = unsafe {nil}
txt_2 &ui.TextBox = unsafe {nil}
lbl_1 &ui.Label = unsafe {nil}
lbl_2 &ui.Label = unsafe {nil}
btn_1 &ui.Button = unsafe {nil}
instr string = "Enter some text, the number 75000, and press 'Validate'"
txt string
num string
@ -16,44 +11,37 @@ struct App {
fn main() {
mut app := &App{}
// widgets defined and placed here for clarity
app.lbl_1 = ui.label(text: &app.instr, id: "lbl_1", justify: [0.5, 0.0]) // [x, y] for text placement
app.lbl_2 = ui.label(text: "", id: "lbl_2", text_align: .center, justify: [0.5, 0.0])
app.txt_1 = ui.textbox(placeholder: "String", text: &app.txt, id: "txt_1")
app.txt_2 = ui.textbox(placeholder: "Number", text: &app.num, id: "txt_2")
app.btn_1 = ui.button(width: 5, text: "Validate", on_click: app.btn_click)
app.win = ui.window(
height: 150
width: 450
title: "Input Info"
mode: .resizable
// widgets defined and placed first for clarity
lbl_1 := ui.label(text: &app.instr, id: "lbl_1", justify: [0.5, 0.0]) // [x, y] for text placement
lbl_2 := ui.label(text: "", id: "lbl_2", text_align: .center, justify: [0.5, 0.0])
txt_1 := ui.textbox(placeholder: "String", text: &app.txt, id: "txt_1")
txt_2 := ui.textbox(placeholder: "Number", text: &app.num, id: "txt_2")
btn_1 := ui.button(width: 5, text: "Validate", on_click: app.btn_click)
// column can be spread vertically and for visualization
col := ui.column(
alignment: .center // button's center, relative to UI
spacing: 5 // button's distance from other widgets
widths: [ui.stretch, 150] // button's width relative to UI
margin: ui.Margin{0, 5, 0, 5} // widgets relative to UI's edges, {y, right, x, left}
children: [
ui.column(
alignment: .center // button's center, relative to UI
spacing: 5 // button's distance from other widgets
widths: [ui.stretch, 150] // button's width relative to UI
margin: ui.Margin{0, 5, 0, 5} // widgets relative to UI's edges, {y, right, x, left}
children: [
ui.column(
alignment: .center
spacing: 5 // distance between widgets in same column
children: [
app.lbl_1
app.lbl_2
app.txt_1
app.txt_2
]
),
// button in and controlled from separate column
app.btn_1
]
),
alignment: .center
spacing: 5 // distance between widgets in same column
children: [
lbl_1
lbl_2
txt_1
txt_2
]
),
btn_1 // button in and controlled from separate column
]
)
app.win = ui.window(height: 150, width: 450, title: "Input Info", mode: .resizable, children: [col])
ui.run(app.win)
}
fn (mut app App) btn_click(btn &ui.Button) {
app.lbl_2.set_text("${app.txt} ${app.num}")
mut lbl_2 := app.win.get_or_panic[ui.Label]("lbl_2") // id: "lbl_2" (used above)
lbl_2.set_text("${app.txt} ${app.num}")
}

View file

@ -1,95 +1,91 @@
\12345678901234567890123456789012345
\User input/Graphical.......... X .
\ Please enter a string and 75000: .
\ String: Hello, World!___ .
\ Number: 75000___________ .
\012345678901234567890123456789012345
\ User input/Graphical.......... X .
\ Please enter a string and 75000: .
\ String: Hello, World!___. .
\ Number: 75000___________. .
def X0=20, Y0=10; \upper-left corner of window's position (chars)
def X0=20, Y0=10; \position of upper-left corner of window (chars)
int Mouse, Button, X, Y, Ch, I, SN; \SN = string number = 0 or 1
def StrMax = 16; \maximum number of characters in strings
char String(2, StrMax); \string arrays (includes Number string)
char String(2, StrMax); \2 string arrays (including Number string)
int StrInx(2); \index to character to be added to String
int Mouse, Button, C, I, SN;
func GetButton; \Return soft button number at mouse pointer
int X, Y;
[Mouse:= GetMouse;
X:= Mouse(0)/8 - X0; \convert pixels to char cells
X:= Mouse(0)/8 - X0; \convert pixels to 8x16-pixel character cells
Y:= Mouse(1)/16 - Y0;
if X>=32 & X<=34 & Y=0 then return 0; \exit [X]
if X>=32 & X<=34 & Y=0 then return 0; \exit [X]
if X>=10 & X<=10+StrMax & Y=4 then return 1; \line 1
if X>=10 & X<=10+StrMax & Y=6 then return 2; \line 2
return -1; \mouse not on any soft button
];
proc ShowCursor(Flag); \Turn cursor at insertion point on or off
proc ShowCursor(Flag); \Turn cursor at end of active String on or off
int Flag;
[Cursor(10+StrInx(SN)+X0, 4+SN*2+Y0);
ChOut(6, if Flag then ^_ else ^ );
];
[SetVid($12); \640x480 graphics
TrapC(true); \disable Ctrl+C
Attrib($70); \black on gray
SetWind(0+X0, 0+Y0, 35+X0, 8+Y0, 0, \fill\true);
Cursor(33+X0, 0+Y0); Text(6, "X");
[SetVid($12); \set 640x480 graphics
TrapC(true); \prevent Ctrl+C from aborting the program
Attrib($70); \set black-on-gray color attribute
SetWind(0+X0, 0+Y0, 35+X0, 8+Y0, 0, \fill\true); \draw gray rectangle
Cursor(33+X0, 0+Y0); Text(6, "X"); \draw exit button
Cursor(2+X0, 2+Y0); Text(6, "Please enter a string and 75000:");
Cursor(2+X0, 4+Y0); Text(6, "String:");
Cursor(2+X0, 6+Y0); Text(6, "Number:");
Attrib($1F); \bright white on blue, for title
Attrib($9F); \set bright white on light blue, for title bar
Cursor(0+X0, 0+Y0); Text(6, " User input/Graphical ");
Attrib($F0); \black on bright white
Attrib($F0); \set black on bright white
for SN:= 0 to 1 do \initialize Strings
[StrInx(SN):= 0;
Cursor(10+X0, 4+SN*2+Y0);
[Cursor(10+X0, 4+SN*2+Y0);
for I:= 0 to StrMax-1 do
[String(SN, I):= $20; ChOut(6, ^ )];
[String(SN, I):= ^ ; ChOut(6, ^ )];
ChOut(6, ^ ); \add one more for cursor underline at StrMax
StrInx(SN):= 0;
];
SN:= 0; \select first, topmost String
SN:= 0; \select first (topmost) String
ShowCursor(true);
ShowMouse(true);
ShowMouse(true); \turn on mouse pointer
loop [MoveMouse; \make pointer track mouse movements
Mouse:= GetMouse;
Mouse:= GetMouse; \get pointer to mouse array information
if Mouse(2) then \a left or right mouse button is down
[Button:= GetButton; \get soft button at mouse pointer
while Mouse(2) do \wait for mouse button's release
while Mouse(2) do \wait for mouse button(s) to be released
[MoveMouse;
Mouse:= GetMouse;
];
if Button = GetButton then \if down Button = release button
[if Button = 0 then quit;
if Button # -1 then \move cursor to active String
[ShowCursor(false);
if Button = GetButton then \if down Button = release button and it
[if Button = 0 then quit; \is the exit [X] button then quit loop
if Button # -1 then \move cursor underline to active String
[ShowMouse(false); \don't overwrite mouse pointer
ShowCursor(false); \turn off cursor at old String position
SN:= Button-1;
ShowCursor(true);
ShowCursor(true); \turn on cursor for selected String
ShowMouse(true); \mouse pointer is normally displayed
];
];
];
if KeyHit then
[ShowMouse(false); \don't overwrite mouse pointer
C:= ChIn(1); \get character from non-echoed keyboard
if SN = 0 and C >= $20 and C <= $7E or \all printable ASCIIs
SN = 1 and C >= $30 and C <= $39 then \only numeric digits
[String(SN, StrInx(SN)):= C;
if StrInx(SN) < StrMax-1 then StrInx(SN):= StrInx(SN)+1;
ShowCursor(false); \remove cursor underline cuz it moves
Ch:= ChIn(1); \get character from non-echoed keyboard
if SN=0 & Ch>=$20 & Ch<=$7E or \allow all printable chars
SN=1 & Ch>=$30 & Ch<=$39 then \allow only numeric digits
[if StrInx(SN) < StrMax then
[String(SN, StrInx(SN)):= Ch; StrInx(SN):= StrInx(SN)+1];
]
else if C = \BS\$08 then \delete back a character
[ShowCursor(false);
if StrInx(SN) > 0 then StrInx(SN):= StrInx(SN)-1;
]
else if C = \tab\$09 then \select next string
[ShowCursor(false);
SN:= rem((SN+1)/2);
]
else if C = \Esc\$1B then quit;
else if Ch = \BS\$08 then \delete back a character
[if StrInx(SN) > 0 then StrInx(SN):= StrInx(SN)-1]
else if Ch = \tab\$09 then \select next string
SN:= rem((SN+1)/2)
else if Ch = \Esc\$1B then quit;
Cursor(10+X0, 4+SN*2+Y0); \show active String
for I:= 0 to StrInx(SN)-1 do ChOut(6, String(SN, I));
ChOut(6, ^_);
ChOut(6, ^_); \show cursor underline at end of String
ShowMouse(true);
];
];
]; \loop
SetVid(3); \restore normal text mode immediately
]