There's probably a much more straightforword way
of doing this, but I couldn't find it!
Attached is some demonstration code for a dialog
box for asking Yes/No questions and getting a
response.
What makes this code interesting (he claims) is
that the size of dialog box is determined (within
bounds) by the size of the message. The hard
part is figuring out the size of each line of
text based upon the currently active font when
there isn't a window yet to ask about the font!
(The solution is to delay figuring anything out
until there is a window, computing the sizes,
and then rebuilding the window... [as I said,
there's probably a better way - please suggest
it if you know it!])
You can't run this without the class library
found at:
http://tapestry.tucson.az.us/unicon
(this file is part of that class library now)
though it really doesn't use much of that
library.) However, the technique is interesting
in itself.
-Steve
--
Steve Wampler <[EMAIL PROTECTED]>
#<p>
# YesNoDialog.icn -- provide a dialog box for a yes/no response.
#</p>
#<p>
# Resizes the dialog box appropriately.
#</p>
#<p>
# <b>Author:</b> Steve Wampler (<i>[EMAIL PROTECTED]</i>)
#</p>
#<p>
# This file is in the <i>public domain</i>.
#</p>
package GuiWidgets
import Utils
#<p>
# Provide a simple dialog box that allows you to ask a question and get
# a Yes/No response.
#</p>
class YesNoDialog : _Dialog(yesButton, noButton, messageArea, mesg, sizeInfo, value)
#<p>
# <i>Internal use only!</i>
#</p>
method handle_yesButton(ev)
value := "yes"
dispose()
end
#<p>
# <i>Internal use only!</i>
#</p>
method handle_noButton(ev)
value := "no"
dispose()
end
#<p>
# <i>Internal use only!</i>
#</p>
method dialog_event(ev)
case ev.get_component() of {
yesButton : handle_yesButton(ev)
noButton : handle_noButton(ev)
}
end
#<p>
# <i>Internal use only!</i>
#</p>
method final_setup()
local sizeField, sizeInfo
minArea := displayAreaSize(100,100)
maxArea := displayAreaSize(500,500)
sizeMInfo := textAreaSize(self.get_win(), mesg)
sizeDInfo := displayAreaSize(sizeMInfo.width+30, sizeMInfo.height+40)
sizeDInfo := forceAreaSize(sizeDInfo, minArea, maxArea)
sizeField := "size="||(sizeDInfo.width)||","||(sizeDInfo.height)
self.set_attribs(sizeField)
messageArea := TextList()
messageArea$set_pos("15", "10")
messageArea$set_size(sizeMInfo.width, sizeMInfo.height)
messageArea$set_contents(mesg)
self$add(messageArea)
yesButton := TextButton()
yesButton$set_pos(sizeDInfo.width-72, sizeDInfo.height-30)
yesButton$set_label("Yes")
yesButton$set_internal_alignment("c")
self$add(yesButton)
noButton := TextButton()
noButton$set_pos(sizeDInfo.width-32, sizeDInfo.height-30)
noButton$set_label("No")
noButton$set_internal_alignment("c")
self$add(noButton)
WClose(self.get_win())
self.open_win()
self$_Dialog.final_setup(self)
end
#<p>
# <i>Internal use only!</i>
#</p>
method open_win()
self.win := (WOpen ! (["inputmask=mc"] ||| self.attribs)) |
error("couldn't open window")
self.buffer_win := (WOpen ! (["canvas=hidden"] ||| self.attribs)) |
error("couldn't open window")
return
end
#<p>
# <i>Internal use only!</i>
#</p>
method end_dialog()
end
#<p>
# <i>Internal use only!</i>
#</p>
method setup()
/mesg := "Yes or No?"
end
#<p>
# <i>Internal use only!</i>
#</p>
method component_setup()
self.setup()
end
#<p>
# Succeed if the "Yes" button was pressed.
#</p>
method isYes()
return value == "yes"
end
#<p>
# Succeed if the "No" button was pressed.
#</p>
method isNo()
return value == "no"
end
#<p>
# Produce the response ("yes" or "no")
#</p>
method getResponse()
return value
end
#<p>
# If message is a string, split it vertically on embedded newlines.
# Otherwise, message should be a list of lines forming the question.
#</p>
initially (message)
self$_Dialog.initially()
/message := "Yes or No?"
if type(message) == "string" then {
mesg := stringToList(message)
}
else {
if *message = 0 then message := ["Yes or No?"]
mesg := message
}
write("mesg:")
every write("\t",!mesg)
end