%%% %%% Chapter: Getting started %%% declare W={New Tk.toplevel tkInit(title:'Capitalization')} E={New Tk.entry tkInit(parent:W)} B={New Tk.button tkInit(parent: W text: 'Change Capitalization' action: proc {$} S={E tkReturn(get $)} in {E tk(delete 0 'end')} {E tk(insert 0 {Map S fun {$ I} case {Char.type I} of lower then {Char.toUpper I} [] upper then {Char.toLower I} else I end end})} end)} {Tk.send pack(E B fill:x padx:4 pady:4)} %%% %%% Chapter: Widgets %%% %% %% Toplevel widgets %% %% Creating a toplevel widget declare W={New Tk.toplevel tkInit(width:150 height:50)} %% Changing its backgound {W tk(configure background:purple)} %% Closing the toplevel widget {W tkClose} %% %% Frames %% declare W ={New Tk.toplevel tkInit} Fs={Map [groove ridge flat sunken raised] fun {$ R} {New Tk.frame tkInit(parent:W width:2#c height:1#c relief:R borderwidth:4)} end} {{Nth Fs 3} tk(configure background:black)} {Tk.send pack(b(Fs) side:left padx:4 pady:4)} {W tkClose} %% %% Label Widgets %% declare W ={New Tk.toplevel tkInit} L1={New Tk.label tkInit(parent:W bitmap:info)} L2={New Tk.label tkInit(parent:W text:'Labels: bitmaps and text')} {Tk.send pack(L1 L2 side:left padx:2#m pady:2#m)} {L1 tk(configure bitmap:warning)} %% %% Bitmap options %% {L2 tk(configure bitmap: '@'#{Property.get 'oz.home'}# '/doc/wp/queen.xbm' foreground: orange)} {W tkClose} declare W={New Tk.toplevel tkInit} {List.forAllInd [error gray75 gray50 gray25 gray12 hourglass info questhead question warning] proc {$ I D} R=(I-1) div 5 C=(I-1) mod 5 in {Tk.batch [grid(row:R*2 column:C {New Tk.label tkInit(parent:W bitmap:D)}) grid(row:R*2+1 column:C {New Tk.label tkInit(parent:W text:D)})]} end} {W tkClose} %% %% Font options %% declare W={New Tk.toplevel tkInit} {ForAll [times helvetica courier] proc {$ Family} {ForAll [normal bold] proc {$ Weight} F={New Tk.font tkInit(family: Family weight: Weight size: 12)} L={New Tk.label tkInit(parent: W text: 'A '#Weight#' '#Family#' font.' font: F)} in {Tk.send pack(L)} end} end} {W tkClose} %% %% Images %% declare W ={New Tk.toplevel tkInit} D ={Property.get 'oz.home'}#'/doc/wp/' I ={New Tk.image tkInit(type:photo format:ppm file:D#'truck-left.ppm')} L1={New Tk.label tkInit(parent:W image:I)} L2={New Tk.label tkInit(parent:W image:I)} L3={New Tk.label tkInit(parent:W image:I)} {Tk.send pack(L1 L2 L3 padx:1#m pady:1#m side:left)} {I tk(configure file:D#'truck-right.ppm')} {W tkClose} %% %% Messages %% declare W ={New Tk.toplevel tkInit(bg:white)} S ='Text extending over several lines.' Ms={Map [left#200 center#100 right#50] fun {$ J#A} {New Tk.message tkInit(parent:W text:S justify:J aspect:A)} end} {Tk.send pack(b(Ms) side:left padx:2#m pady:2#m)} {W tkClose} %%% %%% Chapter: Geometry Managers %%% %% %% The Packer %% declare fun {NewLabels} W={New Tk.toplevel tkInit(background:white)} in {Map ['label' 'Second label widget' '3rd label'] fun {$ A} {New Tk.label tkInit(parent:W text:A)} end} end declare [L1 L2 L3] = {NewLabels} {Tk.send pack(L1 L2 L3)} {Tk.send pack(b({NewLabels}))} %% Side Options {Tk.send pack(b({NewLabels}) side:left)} {Tk.send pack(b({NewLabels}) side:bottom)} %% Padding {Tk.send pack(b({NewLabels}) padx:1#m pady:1#m)} {Tk.send pack(b({NewLabels}) ipadx:2#m ipady:2#m)} %% Anchors {Tk.send pack(b({NewLabels}) anchor:w padx:1#m pady:1#m)} %% Filling and Expansion {Tk.send pack(b({NewLabels}) fill:x)} {Tk.send pack(b({NewLabels}) expand:true)} {Tk.send pack(b({NewLabels}) fill:both expand:true)} %% %% The Grid Geometry Manager %% declare W={New Tk.toplevel tkInit(bg:white)} proc {GL W R C S} L={New Tk.label tkInit(parent:W text:S)} in {Tk.send grid(L row:R column:C padx:4 pady:4)} end {GL W 1 1 nw} {GL W 1 2 north} {GL W 1 3 ne} {GL W 2 1 west} {GL W 2 3 east} {GL W 3 1 sw} {GL W 3 2 south} {GL W 3 3 sw} %% Span Options declare W={New Tk.toplevel tkInit(bg:white)} {Tk.send grid({New Tk.label tkInit(parent:W text:'Upper left')} row:1 rowspan:2 column:1 columnspan:2 padx:4 pady:4)} {GL W 1 3 ne} {GL W 2 3 east} {GL W 3 1 sw} {GL W 3 2 south} {GL W 3 3 sw} %% Sticky Options declare W={New Tk.toplevel tkInit(bg:white)} {Tk.send grid({New Tk.label tkInit(parent:W text:'Upper left')} row:1 rowspan:2 column:1 columnspan:2 sticky: nse padx:4 pady:4)} {GL W 1 3 ne} {GL W 2 3 east} {GL W 3 1 sw} {GL W 3 2 south} {GL W 3 3 sw} %% Weight Options {Tk.batch [grid(rowconfigure W 3 weight:1) grid(columnconfigure W 3 weight:1)]} %% %% Using Anchors for Widgets %% declare [L1 L2 L3]={NewLabels} {Tk.send pack(L1 L2 L3 fill:x)} {L1 tk(configure anchor:w)} {L3 tk(configure anchor:e)} %%% %%% Chapter: More Widgets %%% %% %% Buttons and Actions %% declare W ={New Tk.toplevel tkInit} B1={New Tk.button tkInit(parent: W text: 'Press me!' action: proc {$} {Browse pressed} end)} B2={New Tk.button tkInit(parent: W bitmap: error action: W#tkClose)} {Tk.send pack(B1 B2 fill:x padx:1#m pady:1#m)} {B1 tkAction} {B2 tkAction(action: B1 # tkClose)} {W tkClose} %% %% Checkbuttons, Radiobuttons, and Variables %% declare W ={New Tk.toplevel tkInit} V1={New Tk.variable tkInit(false)} C ={New Tk.checkbutton tkInit(parent:W variable:V1 text:'Bold' anchor:w)} V2={New Tk.variable tkInit('Helvetica')} Rs={Map ['Times' 'Helvetica' 'Courier'] fun {$ F} {New Tk.radiobutton tkInit(parent:W variable:V2 value:F text:F anchor:w)} end} {Tk.batch [grid(C padx:2#m columnspan:3) grid(b(Rs) padx:2#m)]} {Browse state(bold: {V1 tkReturnInt($)}==1 family: {V2 tkReturnAtom($)})} declare fun {GetWeight} if {V1 tkReturnInt($)}==1 then bold else normal end end F={New Tk.font tkInit(size:24 family: {V2 tkReturn($)} weight: {GetWeight})} L={New Tk.label tkInit(parent:W text:'A test text.' font:F)} {C tkAction(action: proc {$} {F tk(configure weight:{GetWeight})} {L tk(configure font:F)} end)} {List.forAllInd ['Times' 'Helvetica' 'Courier'] proc {$ I Family} {{Nth Rs I} tkAction(action: proc {$} {F tk(configure family:Family)} {L tk(configure font:F)} end)} end} {Tk.send grid(L padx:2#m pady:2#m sticky:ew columnspan:3)} {W tkClose} %% %% Menus, Menuitems and Menubuttons %% declare W ={New Tk.toplevel tkInit} Cs =['Wheat' 'Firebrick' 'Navy' 'Darkorange'] M1 ={New Tk.menu tkInit(parent:W tearoff:false)} M2 ={New Tk.menu tkInit(parent:M1 tearoff:false)} E1 ={New Tk.menuentry.cascade tkInit(parent:M1 label:'Background Color' menu:M2)} E2 ={New Tk.menuentry.separator tkInit(parent:M1)} E3 ={New Tk.menuentry.command tkInit(parent:M1 label:'Quit' action: W#tkClose)} V ={New Tk.variable tkInit(Cs.1)} CEs={Map Cs fun {$ C} {New Tk.menuentry.radiobutton tkInit(parent:M2 label:C var:V val:C action: W#tk(configure bg:C))} end} {M1 tk(post 0 0)} %% %% Events %% {W tkBind(event: '' args: [int(x) int(y)] action: proc {$ X Y} TX={Tk.returnInt winfo(rootx W)} TY={Tk.returnInt winfo(rooty W)} in {Tk.send tk_popup(M1 X+TX Y+TY)} end)} {W tkBind(event: '<1>' append: true action: proc {$} {Browse pop} end)} {W tkClose} %% %% More on Actions: Listeners %% declare W ={New Tk.toplevel tkInit} L ={New class $ from Tk.listener meth b1 {Browse b1} end meth b2 {Browse b2} end end tkInit} B1={New Tk.button tkInit(parent:W text:'One' action: L#b1)} B2={New Tk.button tkInit(parent:W text:'Two' action: L#b2)} {Tk.send pack(B1 B2 side:left)} {L tkServe(b1)} {W tkClose} %% %% Entries and Focus %% declare W={New Tk.toplevel tkInit} L={New Tk.label tkInit(parent:W text:'File name:')} E={New Tk.entry tkInit(parent:W width:20)} {Tk.batch [pack(L E side:left pady:1#m padx:1#m) focus(E)]} {Browse {E tkReturnAtom(get $)}} {W tkClose} %% %% Scales %% declare W ={New Tk.toplevel tkInit} L ={New class $ from Tk.listener attr red:0 green:0 blue:0 meth bg(C I) C := I {F tk(configure bg:c(@red @green @blue))} end end tkInit} F ={New Tk.frame tkInit(parent:W height:2#c)} Ss={Map [red green blue] fun {$ C} {New Tk.scale tkInit(parent:W orient:horizontal length:8#c label:C 'from':0 to:255 action: L # bg(C) args: [int])} end} {Tk.send pack(b(Ss) F fill:x)} {W tkClose} %% %% Listboxes and Scrollbars %% declare W={New Tk.toplevel tkInit} L={New Tk.listbox tkInit(parent:W height:6)} {L tkBind(event: '<1>' action: proc {$} I={L tkReturn(curselection $)} C={L tkReturn(get(I) $)} in {L tk(configure bg:C)} end)} S={New Tk.scrollbar tkInit(parent:W)} {ForAll [aliceblue antiquewhite aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse chocolate coral cornflowerblue cornsilk cyan darkblue darkcyan darkgoldenrod darkgray darkgreen darkgrey darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkslategrey darkturquoise darkviolet deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite forestgreen gainsboro ghostwhite gold goldenrod gray green greenyellow grey honeydew hotpink indianred ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrod lightgoldenrodyellow lightgray lightgreen lightgrey lightpink lightsalmon lightseagreen lightskyblue lightslateblue lightslategray lightslategrey lightsteelblue lightyellow limegreen linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy navyblue oldlace olivedrab orange orangered orchid palegoldenrod palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple red rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell sienna skyblue slateblue slategray slategrey snow springgreen steelblue tan thistle tomato turquoise violet violetred wheat white whitesmoke yellow yellowgreen] proc {$ C} {L tk(insert 'end' C)} end} {Tk.addYScrollbar L S} {Tk.send pack(L S fill:y side:left)} {W tkClose} %% %% Toplevel Widgets and Window Manager Commands %% declare W={New Tk.toplevel tkInit(title:'Something different')} {W tkClose} declare W={New Tk.toplevel tkInit(withdraw:true)} {Tk.send wm(deiconify W)} {W tkClose} %% %% Selecting Files %% case {Tk.return tk_getOpenFile} of nil then skip elseof S then {Browse file({String.toAtom S})} end %% %% Example: Help Popups %% declare local fun {MakePopup Parent Text} fun {$} [X Y H]={Map [rootx rooty height] fun {$ WI} {Tk.returnInt winfo(WI Parent)} end} W={New Tk.toplevel tkInit(withdraw:true bg:black)} M={New Tk.message tkInit(parent:W text:Text bg:khaki aspect:400)} in {Tk.batch [wm(overrideredirect W true) wm(geometry W '+'#X+10#'+'#Y+H) pack(M padx:2 pady:2) wm(deiconify W)]} W end end class HelpListener from Tk.listener attr cancel: unit popup: unit meth init(parent:P text:T) popup := {MakePopup P T} Tk.listener,tkInit end meth enter C in cancel := C thread A={Alarm 1000} in {WaitOr C A} if {IsDet A} then W={@popup} in {Wait C} {W tkClose} end end end meth leave @cancel=unit end end in proc {AttachHelp Widget Text} L={New HelpListener init(parent:Widget text:Text)} in {Widget tkBind(event:'' action:L#enter append:true)} {Widget tkBind(event:'' action:L#leave append:true)} {Widget tkBind(event:'