GUI enabling/disabling of controls

From Rosetta Code
Revision as of 22:47, 23 August 2010 by Vera (talk | contribs) (→‎{{header|PureBasic}}: enlarged window-width to fit gadgets)
Task
GUI enabling/disabling of controls
You are encouraged to solve this task according to the task description, using any language you may know.

In addition to fundamental GUI component interaction, an application should dynamically enable and disable GUI components, to give some guidance to the user, and prohibit (inter)actions which are inappropriate in the current state of the application.

The task: Similar to the task GUI component interaction write a program that presents a form with three components to the user: A numeric input field ("Value") and two buttons ("increment" and "decrement").

The field is initialized to zero. The user may manually enter a new value into the field, increment its value with the "increment" button, or decrement the value with the "decrement" button.

The input field should be enabled only when its value is zero. The "increment" button only as long as the field's value is less then 10: When the value 10 is reached, the button should go into a disabled state. Analogously, the "decrement" button should be enabled only as long as the value is greater than zero.

Effectively, the user can now either increment up to 10, or down to zero. Manually entering values outside that range is still legal, but the buttons should reflect that and enable/disable accordingly.

PicoLisp

The standard PicoLisp GUI is HTTP based. Connect your browser to http://localhost:8080 after starting the following script. <lang PicoLisp>#!/usr/bin/picolisp /usr/lib/picolisp/lib.l

(load "@ext.l" "@lib/http.l" "@lib/xhtml.l" "@lib/form.l")

(de start ()

  (and (app) (zero *Number))
  (action
     (html 0 "Enable/Disable" "lib.css" NIL
        (form NIL
           (gui '(+Var +Able +NumField) '*Number '(=0 *Number) 20 "Value")
           (gui '(+Able +JS +Button) '(> 10 *Number) "increment"
              '(inc '*Number) )
           (gui '(+Able +JS +Button) '(gt0 *Number) "decrement"
              '(dec '*Number) ) ) ) ) )

(server 8080 "@start") (wait)</lang>

PureBasic

<lang PureBasic>Enumeration

 #TextGadget
 #AddButton
 #SubButton

EndEnumeration

Procedure UpdateGadgets(Value,UpdateValue=0)

 Overmax=0: UnderMin=0
 If Value>=10
   Overmax=1
 ElseIf Value<=0
   UnderMin=1
 EndIf
 DisableGadget(#AddButton,Overmax)
 DisableGadget(#SubButton,UnderMin)
 If UpdateValue
   SetGadgetText(#TextGadget,Str(Value))
 EndIf

EndProcedure

If OpenWindow(0,#PB_Ignore,#PB_Ignore,110,70,"PB-GUI",#PB_Window_SystemMenu)

 StringGadget(#TextGadget,10,10,90,20,"")
 ButtonGadget(#AddButton,10,40,30,20,"+")
 ButtonGadget(#SubButton,70,40,30,20,"-")
 UpdateGadgets(Value,1)
 Repeat 
   Event=WaitWindowEvent()
   If Event=#PB_Event_Gadget
     Gadget=EventGadget()
     Select Gadget
       Case #AddButton
         Value+1
         UpdateGadgets(Value,1)
       Case #SubButton
         Value-1
         UpdateGadgets(Value,1)
       Default
         EType=EventType()
         If EType=#PB_EventType_Change
           Value=Val(GetGadgetText(#TextGadget))
           UpdateGadgets(Value)
         EndIf
     EndSelect
   EndIf
   Until Event=#PB_Event_CloseWindow

EndIf</lang>