summaryrefslogtreecommitdiff
path: root/ipl/gprocs/vquery.icn
blob: 8696153f1cdf0b9d7a2498acca7043f6d435ecfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
############################################################################
#
#	File:     vquery.icn
#
#	Subject:  Procedures for window queries
#
#	Author:   Jon Lipp
#
#	Date:     May 2, 2001
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  Utility procedures in this file: Vchoice(), Vinput()
#
############################################################################
#
#  Requires:  Version 9 graphics
#
############################################################################
#
#  Links:  vidgets, vbuttons, vtext
#
############################################################################

link vidgets
link vbuttons
link vtext

procedure Vchoice(str, buttons[])
local win, root, t, u, w, b, i, x, y, rv
local button_pos, def_button, old, event
static wpad, hwpad
static temp, PAD, WINX, WINY
initial {
    temp := open("vchoice", "g", "canvas=hidden")
    PAD := integer(WAttrib(temp, "fheight") + 10)
    WINX := integer(WAttrib(temp, "displaywidth") / 2)
    WINY := integer(WAttrib(temp, "displayheight") / 2)

    wpad := 30
    hwpad := wpad/2
}

    if *buttons = 0 then buttons := [" Yes ", " No  "]
    t := TextWidth(temp, str)
    u := 0
    every b := !buttons do
  	u +:= TextWidth(temp, \b) + 13
    w := ((u > t, u) | t) + wpad

    win := vquery_open_window("choose", WINX-w/2, WINY-PAD, w, 2*PAD+wpad)
    root := Vroot_frame(win)
    VResize(root)

    Vmessage(root, hwpad + (w-wpad-t)/2, hwpad, win, str)
    x := hwpad + (w-wpad-u)/2; y := -hwpad
    button_pos := table()
    every i := 1 to *buttons do {
	t := Vbutton(root, x, y, win, buttons[i], , i)
	x +:= t.aw+5
	button_pos[i] := xywh_rec(t.ax-2, t.ay-2, t.aw+4, t.ah+4)
    }
    VDraw(root)

    def_button := 1
    old := button_pos[def_button]
    DrawRectangle(win, old.x, old.y, old.w, old.h)

    repeat {
	rv := &null
	case event := Event(win) of {
	-10: next
	"\r": {
	    rv := def_button
	    break
	}
	"\t" : {
            WAttrib(win, "drawop=reverse")
            DrawRectangle(win, old.x, old.y, old.w, old.h)
            def_button +:= 1
	    def_button := (def_button > *buttons, 1)
            old := button_pos[def_button]
            WAttrib(win, "drawop=copy")
            DrawRectangle(win, old.x, old.y, old.w, old.h)
        }
	default : {
	    rv := VEvent(root, event, &x, &y)
	    (\rv, break)
	}
	} # end case
    }
    close(win)
    return rv

end
record xywh_rec(x, y, w, h)

procedure Vinput(str, def_value)
local win, root, t, u, w, b, i, x, y, rv
local buttons, v, input_vidget, ok, cancel
local button_pos, def_button, old, lrv, event
static temp, PAD, WINX, WINY, FW, VTEXT_W
static wpad, hwpad, ID_OK, ID_CANCEL
initial {
    temp := WOpen("canvas=hidden")
    PAD := integer(WAttrib(temp, "fheight") + 10)
    WINX := integer(WAttrib(temp, "displaywidth") / 2)
    WINY := integer(WAttrib(temp, "displayheight") / 2)
    FW := integer(WAttrib(temp, "fwidth"))

    wpad := 30
    hwpad := wpad/2
    ID_OK := -11
    ID_CANCEL := -12
    VTEXT_W := 20
}

    /str := ""
    /def_value := ""
    buttons := ["  Ok  ", "Cancel"]
    v := FW * VTEXT_W + 8
    t := TextWidth(temp, str)
    u := 0
    every b := !buttons do
  	u +:= TextWidth(temp, b) + 13
    w := vquery_maximum(t, u, v) + wpad

    win := vquery_open_window("choose", WINX-w/2, WINY-PAD, w, 3*PAD+wpad)
    root := Vroot_frame(win)
    VResize(root)

    t := Vmessage(root, hwpad + (w-wpad-t)/2, hwpad, win, str)
    input_vidget := Vtext(root, hwpad+(w-wpad-v)/2, hwpad+t.ah+5, win, "\\="||def_value , , , VTEXT_W)
    x := hwpad + (w-wpad-u)/2; y := -hwpad
    ok := Vbutton(root, x, y, win, buttons[1], , ID_OK)
    x +:= ok.aw+5
    cancel := Vbutton(root, x, y, win, buttons[2], , ID_CANCEL)

    button_pos := table()
    button_pos[ID_OK] := xywh_rec(ok.ax-2, ok.ay-2, ok.aw+4, ok.ah+4)
    button_pos[ID_CANCEL] := xywh_rec(cancel.ax-2, cancel.ay-2, cancel.aw+4, cancel.ah+4)

    VDraw(root)
    def_button := ID_OK
    old := button_pos[def_button]
    DrawRectangle(win, old.x, old.y, old.w, old.h)

    repeat {
	lrv := rv := &null
	case event := Event(win) of {
	-10 : next
	"\r" : {
	    rv := def_button
	    break
	}
	"\t": {
	    WAttrib(win, "drawop=reverse")
	    DrawRectangle(win, old.x, old.y, old.w, old.h)
	    def_button := (def_button = ID_OK, ID_CANCEL) | ID_OK
	    old := button_pos[def_button]
	    WAttrib(win, "drawop=copy")
	    DrawRectangle(win, old.x, old.y, old.w, old.h)
	}

	default: {
	    lrv := root.V.lookup(root, &x, &y)
	    /lrv  := input_vidget
	    rv := (lrv).V.event(lrv, event, &x, &y)
	    if rv === (ID_OK | ID_CANCEL) then break
	}
	} # end case
    }
    close(win)
    return (rv = ID_OK, input_vidget.data) | &null

end

procedure vquery_maximum(l[])
    return sort(l)[-1]
end
procedure vquery_open_window(title, x, y, w, h)
local win

    /x := 50; /y := 50; /w := 400; /h := 400
    win := open(title, "g", "pos="||x||","||y, "width="||w, "height="||h) |
	 _Vbomb("couldn't open window")

    return win
end