VladimirMarkelov / clui

Command Line User Interface (Console UI inspired by TurboVision)
MIT License
670 stars 50 forks source link

Radio button example needed -- please #119

Closed rowemi closed 5 years ago

rowemi commented 5 years ago

Not a real issue other than with my inexperience.

I love clui but having a problem setting up radio buttons. Can anyone please provide a short example of creating a couple of buttons.

Thanks in advance

VladimirMarkelov commented 5 years ago

E.g, you can check dialog.go - search for CreateSelectDialog and look for else branch - between lines 228-241 a RadioGroup with a few items is created. At L254 the currently selected RadioButton is read on pressing button.

rowemi commented 5 years ago

Many thanks. The code helps a lot.

rowemi commented 5 years ago

How do I determine if a radioButton within a radioGroup has been selected. I've tried to use the func "(c *Radio) ProcessEvent(event Event) bool {" in the Radio package

I get the error "cannot use func literal (type func(clui.Event)) as type clui.Event in argument to r.ProcessEventgo" Where r. is a checkbox. There does not seem to be (RadioGroup *) ProcessEvent function which to me would the way to check if a Radio Button in a RadioGroup was selected.

Any help is greatly apprciated.

VladimirMarkelov commented 5 years ago

Do you want to get an event when the selection changes? It seems at this moment you can only subscribe to each event of all Radios(you can set the same callback for all cases) - OnChange. And inside this callback you can read RadioGroup.Selected() - it returns the number of the selected Radio or -1 if none is selected.

It is not very convenient but should work.

rowemi commented 5 years ago

I appreciate your fast reply. I'm more of a backend programmer and strugglying to add a UI to some code.

As you suggested, I tried the OnChange event for one of the radio buttons and it doesn't seem to work like it has for checkboxs or regular buttons. Here is the definition of my radio buttons/group: // Radio buttons to drive ui wRadioView := ui.AddWindow(0, 0, 25, 7, "Select Function") fRadio := ui.CreateFrame(wRadioView, 1, 1, ui.BorderNone, ui.Fixed) fRadio.SetPaddings(1, 1) fRadio.SetGaps(0, 0) fRadio.SetPack(ui.Vertical) dlgrg := ui.CreateRadioGroup() rPick := ui.CreateRadio(fRadio, ui.AutoSize, "Pick Symptoms", ui.Fixed) dlgrg.AddItem(rPick) rAddSymp := ui.CreateRadio(fRadio, ui.AutoSize, "Add Synmptom", ui.Fixed) dlgrg.AddItem(rAddSymp) rAddDiag := ui.CreateRadio(fRadio, ui.AutoSize, "Add Diagnosis", ui.Fixed) dlgrg.AddItem(rAddDiag)

Here is the event to monitor one of the radiobuttons: rAddDiag.OnChange(func(ev ui.Event) { if dlgrg.Selected() == 1 { fNewSymp.SetEnabled(true) } else { fNewSymp.SetEnabled(false) } })

I think this is what you recommended. But the OnChange wants a bool. Not sure how what else to try. Unless I'm doing something wrong, I can probably use a workaround with a group of three regular buttons. Upon the OnClick of any one of the buttons, unset the other buttons and perform actions related to that button.

I would be nice down the road to have an OnChange event for the RadioGroup.

VladimirMarkelov commented 5 years ago

This one works for me: the CheckBox fnewSymp is enabled only when rAddDiag is selected:

package main

import (
    ui "github.com/VladimirMarkelov/clui"
)

func main() {
    ui.InitLibrary()
    defer ui.DeinitLibrary()

    wRadioView := ui.AddWindow(0, 0, 25, 7, "Select Function")
    fRadio := ui.CreateFrame(wRadioView, 1, 1, ui.BorderNone, ui.Fixed)
    fRadio.SetPaddings(1, 1)
    fRadio.SetGaps(0, 0)
    fRadio.SetPack(ui.Vertical)

    dlgrg := ui.CreateRadioGroup()
    rPick := ui.CreateRadio(fRadio, ui.AutoSize, "Pick Symptoms", ui.Fixed)
    dlgrg.AddItem(rPick)
    rAddSymp := ui.CreateRadio(fRadio, ui.AutoSize, "Add Synmptom", ui.Fixed)
    dlgrg.AddItem(rAddSymp)
    rAddDiag := ui.CreateRadio(fRadio, ui.AutoSize, "Add Diagnosis", ui.Fixed)
    dlgrg.AddItem(rAddDiag)

    fNewSymp := ui.CreateCheckBox(fRadio, ui.AutoSize, "Test Checkbox", ui.Fixed)
    ui.ActivateControl(wRadioView, rPick) // to unselect all controls

    // declare common callback for all radio buttons
    rgCallBack := func(bool) {
        fNewSymp.SetEnabled(dlgrg.Selected() == 1)
    }
    // override onChange for all radio buttons
    rAddDiag.OnChange(rgCallBack)
    rPick.OnChange(rgCallBack)
    rAddSymp.OnChange(rgCallBack)

    ui.MainLoop()
}
rowemi commented 5 years ago

Thanks -- I'll give your example a try in a day or so. I appreciate your guidance with my UI adventure.

VladimirMarkelov commented 5 years ago

@rowemi If you need only to control a CheckBox availability by only rAddSymp state(in oter words, CheckBox is enabled only if rAddSymp is selected), you can even simplify my example. Replace:

    // declare common callback for all radio buttons
    rgCallBack := func(bool) {
        fNewSymp.SetEnabled(dlgrg.Selected() == 1)
    }
    // override onChange for all radio buttons
    rAddDiag.OnChange(rgCallBack)
    rPick.OnChange(rgCallBack)
    rAddSymp .OnChange(rgCallBack)

with

rAddSymp.OnChange(func(isOn bool) {
        fNewSymp.SetEnabled(isOn)
})
rowemi commented 5 years ago

Vladimir,

Thanks, I got my UI working last night using the the SetEnable()s suggested from your example.

Cheers,

Mike Rowe

Professor Emeritus, Software Engineering

rowemi@uwplatt.edumailto:rowemi@uwplatt.edu


From: Vladimir Markelov notifications@github.com Sent: Thursday, April 11, 2019 9:42 PM To: VladimirMarkelov/clui Cc: Michael Rowe; State change Subject: Re: [VladimirMarkelov/clui] Radio button example needed -- please (#119)

If you need only to control a CheckBox availability by only rAddSymp state(in oter words, CheckBox is enabled only if rAddSymp is selected), you can even simplify my example. Replace:

// declare common callback for all radio buttons
rgCallBack := func(bool) {
    fNewSymp.SetEnabled(dlgrg.Selected() == 1)
}
// override onChange for all radio buttons
    rAddDiag.OnChange(rgCallBack)
    rPick.OnChange(rgCallBack)
    rAddSymp .OnChange(rgCallBack)

with

rAddSymp .OnChange(func(isOn bool) { fNewSymp.SetEnabled(isOn) })

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHubhttps://github.com/VladimirMarkelov/clui/issues/119#issuecomment-482415136, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AhYh9lHB7qhRBtAkQKRdMaUykpDCmO3_ks5vf_KRgaJpZM4cMdmP.