New options in utils.inputbox and utils.editbox

Posted by Nick Gammon on Fri 01 Oct 2010 07:26 AM — 9 posts, 39,781 views.

Australia Forum Administrator #0
I have had some queries about the functions utils.inputbox and utils.editbox which used to display a fixed-size dialog box. This tended to look a bit flaky if you just wanted to ask the user to enter a small amount of data, when the dialog was actually quite large.

Now those two functions have a sixth argument, which is a table of "extra" information. These fields are named:

  • box_width --> width of message box in pixels (min 180)
  • box_height --> height of message box in pixels (min 125)
  • prompt_width --> width of prompt text in pixels (min 10)
  • prompt_height --> height of prompt text in pixels (min 12)
  • reply_width --> width of reply in pixels (min 10)
  • reply_height --> height of reply in pixels (min 10)
  • max_length --> max characters they can type (min 1)


[EDIT] Also added (see post further down):

  • validate --> validation function
  • ok_button --> label for "OK" button
  • cancel_button --> label for "Cancel" button


[EDIT] Also added in version 4.73:

  • read_only --> text-entry area is read-only
  • ok_button_width --> width of "OK" button
  • cancel_button_width --> width of "Cancel" button



So for example:


result = utils.inputbox ( "Enter the room number",   -- prompt
                          "Room",   -- window title
                          "1234",   -- default answer
                          "Courier", 10,  -- font
         {
          box_width = 180,
          box_height = 125, 
          prompt_width = 150,
          prompt_height = 12,
          reply_width = 80,
          reply_height = 10,
          max_length = 5,
           } 
           )


Options which are omitted try to default to reasonable values.

The box width and height control the overall dialog box size.

The prompt width and height are the size of the field which prompts for the response. So if you have a short question you might make the prompt height small (like, 12).

The reply width and height are the size of the box into which you type the response.

The field "max_length" controls the maximum number of characters you can type into the field. So for example, if you are asking for a character name, and the maximum name is 12 characters, you could put 12 there.

This functionality is in version 4.64 onwards.
Amended on Tue 03 May 2011 11:28 AM by Nick Gammon
Australia Forum Administrator #1
Example:


result = utils.inputbox ( "Your name?", "Character creation", "Nick", nil, nil,
         {
         box_width = 180,
         box_height = 130, 
         prompt_height = 12,
         reply_width = 80,
         reply_height = 20,
         max_length = 12,
         } 
         )


Australia Forum Administrator #2
Another few useful extra options are:

  • validate --> validation function
  • ok_button --> label for "OK" button
  • cancel_button --> label for "Cancel" button


The two label options let you label the OK and Cancel buttons a bit more appropriately. For example, you might label the OK button "Find", and the cancel button "Close".

The "validate" function lets you do validation of the user-entered data before they are allowed to close the dialog box (this only applies if they hit the "OK" button, not the "Cancel" button).

The validate function is passed a single string which is what the user has typed. You can return nil or false to not accept the value. You can return a "true" value (basically everything except nil or false) to accept the entry.

The validation function can be used to check anything you wish. Examples might be:

  • That the entry is not empty
  • That it is a number
  • If a number, that it is in a certain range
  • If a thing (like a room, or spell) that this thing exists (eg. by table lookup)
  • That the entry is not the duplicate of something that already exists
  • That it conforms to some naming rules (eg. only alphabetic characters)


An example might be:


function age_check (s)

  -- check a number
  local n = tonumber (s)  

  if not n then
    utils.msgbox (s .. " is not numeric")
    return false
  end -- if

  -- check in range
  if n < 1 or n > 100 then
    utils.msgbox (s .. " is out of range 1 to 100")
    return false
  end -- if

  -- passes checks
  return true
end  -- function age_check


result = utils.inputbox ( "Your age?", 
       "Character creation", 
       "20",   -- default answer
       "Courier", 10,  -- font
         { validate = age_check,
           max_length = 3, } 
           )



This will simplify somewhat those plugins that ask questions (the mapper springs to mind) because previously you had to let the utils.inputbox dialog close, then validate the answer, and then display an error, which looked a bit clunky.
Amended on Fri 01 Oct 2010 10:42 PM by Nick Gammon
#3
Awesome adds, thanks for the quick response to a feature request.
USA Global Moderator #4
It looks like the OK and Cancel boxes do not resize if you try to put longer strings in, so there is currently an effective limit on button text length. Is this expected?
Australia Forum Administrator #5
Expected, yes. Useful, not really.

Altered in version 4.73 to allow you to specify your own widths. I did this rather than calculate the width based on the text, because you may prefer the buttons to be the same size, and that size being the width of the larger one.

https://github.com/nickgammon/mushclient/commit/d33099e4cf
USA Global Moderator #6
Nick Gammon said:

Expected, yes. Useful, not really.

Altered in version 4.73 to allow you to specify your own widths. I did this rather than calculate the width based on the text, because you may prefer the buttons to be the same size, and that size being the width of the larger one.

https://github.com/nickgammon/mushclient/commit/d33099e4cf


I wouldn't have thought that the system font sizes would be predictable enough for manual widths to work. Is there a way for us to make the appropriate width calculation calculation based on...I guess(?)...getting the system font information?
Amended on Wed 04 May 2011 03:58 AM by Fiendish
Australia Forum Administrator #7
Now you are pushing my knowledge limits. My experience is that button fonts tend to be fixed, but of course if you change themes, or something (like switch to "high readability mode") then the fonts probably get larger. But do the buttons too?

If you just want to change "OK" to "Equip my gear" or something, then some sort of trial-and-error probably would work for 99% of players.

A quick test shows that changing Display -> Appearance -> Font Size to "Extra Large Fonts" does not affect the font used in buttons (in MUSHclient at least) so you can probably just fudge it. Or work out what font the system uses, make a temporary miniwindow, and work out how many pixels you need.
#8
Hi.

I have a question about InputBox function in scripting.
I used to work on my mushclient scripts in VBscript, but lately i changed scripting language to Python and so the trouble began.
I used InputBox function in some of my scripts in vbscript but when i tried to use it in python it shows errors and it seems it does not work in this language.

Am i thinking rigt and if i'm wrong could i get an example how it supposed to look in python?

Gorertz