/**
 *
 * $Id: ui.ycp 15841 2004-03-09 18:47:30Z sh $
 *
 * File:
 *   ui.ycp
 *
 * Module:
 *   Sound
 *
 * Summary:
 *   user interface functions for sound module
 *
 * Authors:
 *   Dan Veselư <dan@suse.cz>
 *   Dan Meszaros <dmeszar@suse.cz>
 *
 */

{
    textdomain "sound";
    import "Sound";
    import "Joystick";
    import "Wizard";
    import "Popup";
    import "Label";

    /**
     *  dialog to be displayed when user presses 'Abort' button
     *	@return boolean yes/no
     */
    define boolean ReallyAbort() ``{
	return Popup::ReallyAbort (Sound::Changed() || Joystick::Changed());
    }

    /**
     * quick config dialog widget
     * @param name card model
     * @param cname alias for modules conf
     * @param cpos position of the card
     * @param has_joy If true checkbox for joystick configuration is added
     * @return term with widget
     */
    define term AutoconfDlg (string name, string cname, integer cpos,
				    boolean has_joy) ``{
	string clab = "";
	if (cpos < 3)
	{
	    clab = "\n" + sformat (Sound::STRINGS["soundCount", cpos]:"",cname);
	}
	else
	{
	    clab = "\n" + sformat(Sound::STRINGS["soundCount", 3]:"", cname,
		cpos + 1);
	}

	term jcheckbox = (!has_joy) ? `Empty() :
	    // push button label
	    `Left (`CheckBox(`id(`joystick), _("&Configure joystick")));

	term con = `VBox (
	    `VSpacing (1),
	    // this is the first part of message "The sound card 'cardname'
	    // will be configured as the first snd card"
	    `Label (`opt (`hstretch), _("The sound card\n")),
	    `HBox (`HSpacing (5), `Label (`opt (`hstretch), name)),
	    `Label (`opt (`hstretch), clab),
            `VSpacing (1),
	    `HBox (`HSpacing (5),
	      `RadioButtonGroup (`id (`action), `VBox (
		`RadioButton (`id (`quick), `opt (`hstretch, `notify),
		    // radio button label - type of setup
		    _("&Quick automatic setup"), true),
                `VSpacing (0.3),
                `RadioButton (`id (`normal), `opt (`hstretch, `notify),
		    // radio button label - type of setup
		    _("Normal &setup")),
                `VSpacing (0.3),
                `RadioButton (`id (`options), `opt (`hstretch, `notify),
		    // radio button label - type of setup
		    _("Advanced setup with possibility to change &options")),
		`VSpacing (3),
		jcheckbox
              ))
	    ),
	    `VStretch ()
        );
	return con;
    }

    /**
     *  DisplayName
     *
     * @param name card model
     * @param cname sound card alias for modules conf
     * @param cpos cards position
     * @param flags enable/disable radiobuttons accordingly
     * @param has_joy sound card supports joystick
     * @return map symbol of next dialog
     */
    define map DisplayName (string name, string cname, integer cpos,
				   integer flags, boolean has_joy) ``{

	string helptext	= Sound::STRINGS["DisplayName"]:"";
	term con	= AutoconfDlg (name, cname, cpos, has_joy);
	// dialog header
	Wizard::SetContents (_("Sound card configuration"), con,
	    helptext, true, true);

	// dialog title
	integer flagc	= 1;
	integer flagp	= 0;
	boolean selected = false;
	list flages	= [`quick, `normal, `intro];

	while (flagp < 3)
	{
	    if ((flagc & flags) == 0)
	    {
		UI::ChangeWidget(`id(flages[flagp]:`quick), `Enabled, false);
	    }
	    else
	    {
		if (!selected)
		{
		    selected = true;
		    UI::ChangeWidget(`id(`action), `CurrentButton,
			flages[flagp]:`quick);
		}
	    }
	    flagp = flagp + 1;
	    flagc = flagc * 2;
	}

	Wizard::RestoreNextButton();

	symbol input = `quick;

	do {
	    if (input == `cancel || input == `abort)
	    {
		if (ReallyAbort())
		{
		    return $["ui" : `abort];
		}
	    }
	    input = (symbol) UI::UserInput();

	} while (input != `next && input !=`back);

	symbol output = input;
	if (input == `next)
	{
	    output = (symbol) UI::QueryWidget(`id(`action), `CurrentButton);
	    if (output == `normal)
	    {
		output = `next;
	    }
	    if (has_joy == true)
	    {
		Sound::conf_joystick =
		    (boolean)UI::QueryWidget(`id(`joystick), `Value);
	    }
	    else
	    {
		Sound::conf_joystick = false;
	    }
	}
	return $["ui" : output];
    }

    /**
     * quick conf dialog widget
     * @param name card model
     * @param cname card alias for module.conf
     * @param cpos card position
     * @param has_joy If true checkbox for joystick configuration is added
     * @return term widget
     */
    define term QuickconfDlg (string name, string cname, integer cpos,
				     boolean has_joy) ``{
        string clab = "";
        if (cpos < 3)
        {
            clab = "\n" + sformat(Sound::STRINGS["soundCount", cpos]:"", cname);
        }
        else
        {
            clab = "\n" + sformat(Sound::STRINGS["soundCount", 3]:"", cname, cpos + 1);
        }

	term jcheckbox = (!has_joy) ? `Empty() :
	    // push button label
	    `Left (`CheckBox(`id(`joystick), _("&Configure joystick")));

        term con = `VBox (
	    `VSpacing (1),
            // this is the first part of message "The sound card 'cardname'
	    // will be configured as the first snd card"
            `Label (`opt (`hstretch), _("The sound card\n")),
            `HBox (`HSpacing (5), `Label (`opt (`hstretch), name)),
            `Label (`opt (`hstretch), clab),
            `VSpacing (1),
            `HBox (`HSpacing (5),
	      `RadioButtonGroup (`id (`action),`VBox (
		`RadioButton (`id (`quick), `opt (`hstretch, `notify),
		    // radio button label - type of setup
		    _("&Quick automatic setup"), true),
                `VSpacing (0.3),
                `RadioButton (`id (`normal), `opt (`hstretch, `notify),
		    // radio button label - type of setup
		    _("Normal &setup")),
		`VSpacing (0.3),
                `RadioButton (`id (`intro), `opt (`hstretch, `notify),
		    // radio button label - type of setup
		    _("&More detailed installation of sound cards")),
		`VSpacing(3),
		jcheckbox
              ))
	    ),
	    `VStretch ()
        );
        return con;
    }


    /**
     * quick config dialog (is shown when only one card has been detected0
     * @param name card model
     * @param cname alias for modules conf
     * @param cpos position of the soud card
     * @param has_joy set to true if configured card supports joystick
     * @return map symbol for next dialog
     */
    define map quickConfig(string name, string cname, integer cpos,
				  boolean has_joy) ``{

        string helptext = Sound::STRINGS["quickConfig"]:"";
        term con	= QuickconfDlg (name, cname, cpos, has_joy);
	// dialog header
        Wizard::SetContents (_("Sound card configuration"), con,
	    helptext, true, true);
        Wizard::RestoreNextButton();

	symbol input = `quick;

        do {
	    if (input == `cancel || input == `abort)
            {
                if (ReallyAbort())
                {
                    return $["ui" : `abort];
                }
            }
            input = (symbol) UI::UserInput();

        } while (input != `next && input != `back);

        symbol output = input;
        if (input == `next)
        {
	    if (has_joy == true)
	    {
		Sound::conf_joystick =
		    (boolean) UI::QueryWidget(`id(`joystick), `Value);
	    }
	    else
	    {
		Sound::conf_joystick = false;
	    }
            output = (symbol) UI::QueryWidget(`id(`action), `CurrentButton);
        }
        return $["ui" : output];
    }

    /**
     * dialog to be displayed in WhichDialog function
     *
     * @param nc list of not configured cards
     * @return term card type selection dialog contents
     * @see WhichDialog
     */
    define term WhichDlgCon (list nc) ``{

	return `HBox(
            `HSpacing (3),
	    // frame label
            `Frame (`id(`frame), _("Select"), `HBox (
		`HSpacing (1),
		`VBox (
		    `RadioButtonGroup (`id(`rg), `VBox (
			`VSpacing (1),
			`Left(`VBox (
			    `Left(`RadioButton(`id(`auto), `opt (`notify),
				// label of radiobutton
				_("&Autodetected"), (size (nc) > 0))
			    ),
			    `VSquash(`SelectionBox(`id(`sel),
				// label of list with autodetected cards
				_("&List of autodetected"), nc)
			    )
                        )),
			`VSpacing (1),
			`Left(`RadioButton (`id(`manual), `opt(`notify),
			    // label of radio button
			    _("&Select from list"), (size (nc) == 0))
			),
			`VSpacing (1)
                    ))
                ),
		`HSpacing (1)
            )),
            `HSpacing (3)
        );
    }

    /**
     * widget for complex dialog
     * @param cc list of configured cards
     * @param start_alsa_seq as previous
     * @param show_font_but show button for sound fonts installation
     * @return widget
     */
    define term ComplexDlgCon (list cc, boolean start_alsa_seq,
				      boolean show_font_but) ``{
	term butts = nil;
	term alsabuts = `Empty();
	if  (Sound::use_alsa)
	{
	    alsabuts = `HBox(
		`Left(`CheckBox(`id(`b_seq), `opt(`notify),
		    // check box label
		    _("&Start sequencer"), start_alsa_seq))
	    );
	}
	if (show_font_but)
	{
	    butts = `HBox(
		alsabuts,
		`VBox (`PushButton (`id(`b_fonts),`opt(`key_F7),
		    // push button label
		    _("&Install soundfonts..."))
		)
	    );
	}
	else
	{
	    butts =  alsabuts;
	}
	term con = `HBox (
            `HSpacing (3),
            `VBox (
		`Table (`id(`configured_table), `opt (`notify, `immediate),
		    // table header label
                    `header(_("Number"),
		    // table header label
		    _("Card model"),
		    // table header label
		    _("State")), cc),
                `VSpacing (0.5),
                `HBox (
		    `PushButton (`id(`b_conf2), `opt (`key_F3),
			// push button label
			_("Add sound &card")),
		    // push button label
		    `PushButton (`id(`b_options), `opt(`key_F4), _("&Options")),
		    // push button label
		    `PushButton (`id(`b_volume), `opt (`key_F6), _("&Volume")),
		    `HStretch (),
		    // push button label
		    `PushButton (`id(`b_delete), `opt (`key_F5), _("&Delete"))
                ),
		`VSpacing (0.5),
                `HBox (
		    `HStretch (),
		    butts
                ),
                `VSpacing (0.5)
            ),
	    `HSpacing (3)
        );
	return con;
    }

    /**
     * displays a dialog that offer to configure detected or
     * manually selected card
     * @param cards detected cards
     * @return `auto/`manual
     */
    define map WhichDialog (list<string> cards) ``{

        string helptext = Sound::STRINGS["WhichDialog"]:"";
        list nc		= [];
        integer i	= 0;

        maplist (string cardname, cards, ``{
            nc = add (nc, `item(`id(i), cardname));
            i = i + 1;
        });

        term con = WhichDlgCon(nc);

	// dialog title
        Wizard::SetContents (_("Configure a sound card"), con, helptext,
	    true, true);
        Wizard::RestoreNextButton();

        if (size (nc) == 0)
        {
            UI::ChangeWidget (`id(`sel), `Enabled, false);
            UI::ChangeWidget (`id(`auto), `Enabled, false);
        }
        else
        {
            UI::ChangeWidget(`id(`sel), `CurrentItem, 0);
        }

        symbol ui = nil;

	do
        {
            ui = (symbol) UI::UserInput ();

            if (ui == `manual)
            {
                UI::ChangeWidget (`id(`sel), `Enabled, false);
            }
            else if (ui == `auto)
            {
                UI::ChangeWidget (`id(`sel), `Enabled, true);
            }
            else if (ui == `abort || ui == `cancel)
            {
                if (ReallyAbort())
                {
                    break;
                }
            }
        } while (!contains([`back, `next, `cancel], ui));

        integer cid = 0;

        symbol way = (symbol) UI::QueryWidget (`id(`rg), `CurrentButton);
        if (way == `auto)
        {
            cid = (integer) UI::QueryWidget(`id(`sel), `CurrentItem);
        }
        else if (way == `manual)
        {
	    cid = -1;
	}

        return $[ "ui" : ui, "card_id"  : cid ];
    }

}//EOF
