GUI enabling/disabling of controls: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Tcl: Added implementation)
No edit summary
Line 22: Line 22:
Manually entering values outside that range is still legal, but the buttons
Manually entering values outside that range is still legal, but the buttons
should reflect that and enable/disable accordingly.
should reflect that and enable/disable accordingly.

=={{header|Liberty BASIC}}==
<lang lb>nomainwin
textbox #demo.val, 20, 50, 90, 24
button #demo.dec, "Decrement", [btnDecrement], UL, 20, 90, 90, 24
button #demo.inc, "Increment", [btnIncrement], UL, 20, 120, 90, 24
statictext #demo.txt, "Positive or negative whole numbers only.", 20, 170, 240, 24
open "Rosetta Task: GUI enabling/disabling of controls" for window as #demo
#demo "trapclose [quit]"
#demo.val 0
#demo.dec "!disable"
wait

[quit]
close #demo
end

[btnDecrement]
validNum = validNum()
if validNum = 0 then
#demo.val "!contents? nVal$"
notice nVal$;" does not appear to be a valid whole number."
else
#demo.val "!contents? nVal"
if nVal > 0 then
nVal = nVal - 1
end if
end if
#demo.val nVal
call disEnableControls nVal
wait

[btnIncrement]
validNum = validNum()
if validNum = 0 then
#demo.val "!contents? nVal$"
notice nVal$;" does not appear to be a valid whole number."
else
#demo.val "!contents? nVal"
if nVal < 10 then
nVal = nVal + 1
end if
end if
#demo.val nVal
call disEnableControls nVal
wait

Function validNum()
validNum$ = "0123456789"
#demo.val "!contents? nVal$"
nVal$ = trim$(nVal$)
if left$(nVal$, 1) = "-" then
neg = 1
nVal$ = mid$(nVal$, 2)
else
neg = 0
end if
validNum = 1
for i = 1 to len(nVal$)
if instr(validNum$, mid$(nVal$, i, 1)) = 0 then
validNum = 0
end if
next i
End Function

Sub disEnableControls nVal
if nVal > 9 then
#demo.inc "!disable"
else
#demo.inc "!enable"
end if
if nVal < 1 then
#demo.dec "!disable"
else
#demo.dec "!enable"
end if
if nVal = 0 then
#demo.val "!enable"
else
#demo.val "!disable"
end if
End Sub</lang>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==

Revision as of 03:34, 25 August 2010

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.

Liberty BASIC

<lang lb>nomainwin

   textbox #demo.val, 20, 50, 90, 24
   button #demo.dec, "Decrement", [btnDecrement], UL, 20, 90, 90, 24
   button #demo.inc, "Increment", [btnIncrement], UL, 20, 120, 90, 24
   statictext #demo.txt, "Positive or negative whole numbers only.", 20, 170, 240, 24
   open "Rosetta Task: GUI enabling/disabling of controls" for window as #demo
   #demo "trapclose [quit]"
   #demo.val 0
   #demo.dec "!disable"

wait

[quit]

   close #demo

end

[btnDecrement]

   validNum = validNum()
   if validNum = 0 then
       #demo.val "!contents? nVal$"
       notice nVal$;" does not appear to be a valid whole number."
   else
       #demo.val "!contents? nVal"
       if nVal > 0 then
           nVal = nVal - 1
       end if
   end if
   #demo.val nVal
   call disEnableControls nVal

wait

[btnIncrement]

   validNum = validNum()
   if validNum = 0 then
       #demo.val "!contents? nVal$"
       notice nVal$;" does not appear to be a valid whole number."
   else
       #demo.val "!contents? nVal"
       if nVal < 10 then
           nVal = nVal + 1
       end if
   end if
   #demo.val nVal
   call disEnableControls nVal

wait

Function validNum()

   validNum$ = "0123456789"
   #demo.val "!contents? nVal$"
   nVal$ = trim$(nVal$)
   if left$(nVal$, 1) = "-" then
       neg = 1
       nVal$ = mid$(nVal$, 2)
   else
       neg = 0
   end if
   validNum = 1
   for i = 1 to len(nVal$)
       if instr(validNum$, mid$(nVal$, i, 1)) = 0 then
           validNum = 0
       end if
   next i

End Function

Sub disEnableControls nVal

   if nVal > 9 then
       #demo.inc "!disable"
   else
       #demo.inc "!enable"
   end if
   if nVal < 1 then
       #demo.dec "!disable"
   else
       #demo.dec "!enable"
   end if
   if nVal = 0 then
       #demo.val "!enable"
   else
       #demo.val "!disable"
   end if

End Sub</lang>

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>

Tcl

Library: Tk

<lang tcl>package require Tk

  1. Model

set field 0

  1. View

place [ttk::frame .bg] -relwidth 1 -relheight 1; # Hack to make things look nice pack [ttk::labelframe .val -text "Value"] pack [ttk::entry .val.ue -textvariable field \ -validate key -invalidcommand bell \ -validatecommand {string is integer %P}] pack [ttk::button .inc -text "increment" -command up] pack [ttk::button .dec -text "decrement" -command down]

  1. Controller

proc up {} {

   global field
   incr field

} proc down {} {

   global field
   incr field -1

}

  1. Attach this controller to the Model; easier than manual calling

trace add variable field write updateEnables proc updateEnables {args} {

   global field
   .inc state [expr {$field < 10 ? "!disabled" : "disabled"}]
   .dec state [expr {$field > 0 ? "!disabled" : "disabled"}]

} updateEnables; # Force initial state of buttons</lang>