RSS


[ Pobierz całość w formacie PDF ]
.The action procedures are executedsequentially in the thread as window events arrive.An exampleThe easiest way to see how this works is by means of an example.Here is a simpleuser interface description defined as a record:D=button(text:"Click this button")The record D defines a widget of button type and the content of the text fieldgives the initial text in the button.Other widgets follow the same conventions.The record label denotes the widget type, the field names denote widget param-eters, and the field contents denote either the parameters initial values or theprocedural parts of the interface (actions or handlers).Some of the widgets are able to contain other widgets.By using these, thecomplete user interface is a nested record that defines all the widgets and theirlogical organization on the screen.For example, here is a simple interface fordoing text entry (see Figure 10.2):D=td(lr(label(text:"Type your name:")entry(handle:H))button(text:"Ok" action:proc {$} {W close} end))Copyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved. 10.2 Using the declarative/procedural approach 695fun {GetText A}H T D W inD=td(lr(label(text:A) entry(handle:H))button(text:"Ok"action:proc {$} T={H get($)} {W close} end))W={QTk.build D}{W show} {W wait}TendFigure 10.3: Function for doing text entryFigure 10.4: Windows generated with the lr and td widgetsThe tdwidget organizes its member widgets in top-down fashion.The lr widgetis similar, but goes left to right.This example has one action, proc {$} {Wclose} end, and a handle, H, which we will explain later.At this point, bothH and W are still unbound variables.Create the window by passing D to theQTk.build procedure:W={QTk.build D}This creates a window, a window object Wthat represents it, and a handler objectH.Now we display the window:{W show}The user can type text in this window.At any time, the text in the window canbe read by calling the handler H:T={H get($)}This is usually done when the window is closed.To make sure it is done whenthe window is closed, we can put it inside the action procedure.To complete this example, let us encapsulate the whole user interface in afunction called GetText.Figure 10.3 shows the resulting code.Calling GetTextwill wait until the user types a line of text and then return the text:{Browse {GetText "Type your name:"}}Note that GetText does a {W wait} call to wait until the window is closedbefore returning.Leaving out this call will return immediately with an unboundvariable that is bound later, when the user clicks the button.Copyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved. 696 Graphical User Interface ProgrammingFigure 10.5: Window generated with newline and continue codes10.2.3 Declarative geometryIn addition to the widgets themselves, there are two other aspects of a windowthat are defined declaratively: the geometric arrangement of its widgets and thebehavior of its widgets when the window is resized.We describe each in turn.Thegeometric arrangement of widgets is defined by means of three special widgetsthat can contain other widgets:" Thelrandtdwidgets arrange their member widgets left-right or top-down.Figure 10.4 shows the two windows that are displayed with the followingtwo commands:D=lr(label(text:"left") E=td(label(text:"top")label(text:"center") label(text:"center")label(text:"right")) label(text:"down"))W1={QTk.build D} W2={QTk.build E}{W1 show} {W2 show}" Theplaceholderwidget defines a rectangular area in the window that cancontain any other widget as long as the window exists.The placeholder scontent can be changed at any time during execution.A placeholder may beput inside a placeholder, to any level of recursion.In the following example,the window alternatively contains a label and a pushbutton:placeholder(handle:P).{P set(label(text:"Hello"))}.{P set(button(text:"World"))}Calling {P set(D)} is almost the same as calling {QTk.build D}, i.e., itinterprets the nested record D and creates handler objects, but the visibleeffect is limited to the rectangular area of the placeholder widget.Copyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved. 10.2 Using the declarative/procedural approach 697nweWsFigure 10.6: Declarative resize behavior" The lr and td widgets support the special codes newline, empty, andcontinue, which allows to organize their member widgets in a grid struc-ture with aligned rows and columns of the same size (see Figure 10.5).Thecode newline makes the subsequent contained widgets jump to a new row(for lr) or column (for td).All the widgets in the new row or column arealigned with the widgets in the previous row or column.The empty spe-cial code leaves an empty box the size of a widget.The continue specialcode lets the previous widget span over an additional box.The followingdescription:lr(button(text:"One" glue:we)button(text:"Two" glue:we)button(text:"Three" glue:we) newlinebutton(text:"Four" glue:we)button(text:"Five" glue:we)button(text:"Six" glue:we) newlinebutton(text:"Seven" glue:we)button(text:"Eight" glue:we)button(text:"Nine" glue:we) newlineempty button(text:"Zero" glue:we) continue)gives the window of Figure 10.5.10.2.4 Declarative resize behaviorWhen the size of a window is changed, the interface has to define how the internalwidgets rearrange themselves.This is called the resize behavior.The resizebehavior is dynamic, i.e., it defines a behavior over time.But it is a sufficientlyrestricted kind of dynamic behavior that we can define it using a descriptivedeclarative model.Copyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved. 698 Graphical User Interface ProgrammingFigure 10.7: Window generated with the glue parameterWe define the resize behavior of a widget by an optional glue parameter, whosevalue is an atom made up of any combination of the letters n, s, w, and e.Theglue parameter places constraints on how the widget is placed and how it resizes.As Figure 10.6 illustrates, a widget W is always placed inside a rectangular areaand has a  natural size defined by its contents.One can choose for the widgetto occupy its natural size in either direction (horizontally or vertically) or to beexpanded to take as much space as possible in either direction.For the left-right direction, the w value, when present, will attach the widget to the left side( west ).The same for the e value ( east ) and the right side.If w and e arepresent simultaneously, then the widget is expanded.Otherwise, it takes up justits natural size.For the top-down direction, the n and s values play the sameroles ( north and  south ).For example, the description:lr(label(text:"Name" glue:w) entry(glue:we) glue:nwe)gives the window of Figure 10.7.10.2.5 Dynamic behavior of widgetsThe dynamic behavior of widgets is defined by means of action procedures andhandler objects.Look again at the example of Section 10.2.2:declare E D W inD=td(lr(label(text:"Type your name:")entry(handle:E))button(text:"Ok" action:toplevel#close))W={QTk.build D}{W show}The action toplevel#close is part of the button; when the button is clickedthen this causes the window to be closed.Generally, actions are zero-argumentprocedures, except that short-cuts are given for a few common actions such asclosing the window.The handle E is an object that allows to control the textentry widget.For example, here s how to set the value of the widget s text entryfield:Copyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved. 10 [ Pobierz caÅ‚ość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • wblaskucienia.xlx.pl