YaST2 Developers Documentation: YCP UI Reference: Widgets



YCP UI Widget Reference

Back to the widget index

CheckBox Clickable on/off toggle button

Description

A checkbox widget has two states: Checked and not checked. It returns no user input but you can query and change its state via the Value property.

Arguments

string label the text describing the check box

Optional

boolean|nil checked whether the check box should start checked - nil means tristate condition, i.e. neither on nor off

Special Properties

None

Sample Usage

`CheckBox( `id( `cheese ), "& Extra cheese" )

Examples

Example 1: CheckBox1.ycp

{
    UI::OpenDialog(
	       `CheckBox("A &checked check box\nwith multi-line", true)
	       );
    UI::UserInput();
    UI::CloseDialog();
}
		

Example 2: CheckBox2.ycp

{
    UI::OpenDialog(
	       `VBox(
		     `Label("Select your extras"),
		     `Left(`CheckBox(`id(`cheese), "Extra Cheese")),
		     `Left(`CheckBox(`id(`pepr),   "Pepperoni", true)),
		     `PushButton("&OK")
		     )
	       );
    UI::UserInput();
    boolean cheese = (boolean) UI::QueryWidget(`id(`cheese), `Value);
    boolean pepr   = (boolean) UI::QueryWidget(`id(`pepr),   `Value);
    UI::CloseDialog();

    define string yesno(boolean b) ``{ if (b) return "yes"; else return "no"; };

    UI::OpenDialog(
	       `VBox(
		     `Left(`Label("Extra Cheese: " + yesno(cheese))),
		     `Left(`Label("Pepperoni: "    + yesno(pepr))),
		     `PushButton("&OK")
		     )
	       );
    UI::UserInput();
    UI::CloseDialog();
}

	       
		

Example 3: CheckBox3.ycp

{
    // Build dialog with one check box and buttons to set its state to
    // on, off or "don't care" (tri-state).
    
    UI::OpenDialog(
	       `VBox( 
		     `CheckBox(`id(`cb), "Format hard disk"),
		     `HBox(
			   `HWeight(1, `PushButton(`id(`setOn ),   "Set on"     ) ),
			   `HWeight(1, `PushButton(`id(`setOff),   "Set off"    ) ),
			   `HWeight(1, `PushButton(`id(`dontCare), "Don't care" ) )
			   ),
		     `PushButton(`id(`ok), "&OK")
		     )
	       );

    
    // Input loop. Will be left only after 'OK' is clicked.

    any button = nil;
    
    repeat
	{
	    button = UI::UserInput();

	    if      ( button == `setOn    ) UI::ChangeWidget ( `id(`cb), `Value, true  );
	    else if ( button == `setOff   ) UI::ChangeWidget ( `id(`cb), `Value, false );
	    else if ( button == `dontCare ) UI::ChangeWidget ( `id(`cb), `Value, nil   );
	} until ( button == `ok );
	

    // Get the check box's value.
    //
    // Notice: The return value of UI::UserInput() does NOT return this value!
    // Rather, it returns the ID of the widget (normally the PushButton)
    // that caused UI::UserInput() to return.
    
    boolean cb_val = (boolean) UI::QueryWidget(`id(`cb), `Value);
    
    // Close the dialog.
    // Remember to read values from the dialog's widgets BEFORE closing it!
    UI::CloseDialog();

    // Convert the check box value to string.
    string valStr = "Don't care";
    if ( cb_val == true  ) valStr = "Yes";
    if ( cb_val == false ) valStr = "No";
    
    // Pop up a new dialog to echo the input.
    UI::OpenDialog(
	       `VBox(
		     `Label("Your selection:"),
		     `Label(valStr),
		     `PushButton("&OK")
		     )
	       );
    UI::UserInput();
    UI::CloseDialog();
}
		

Back to the widget index


YaST2 Developers Documentation: YCP UI Reference: Widgets

Generated Fri May 21 12:39:01 2004