/**
 * Dialog
 *
 * Demonstrates implementation of a GUI dialog.
 */

import("gui")

class SampleDialog
{
    // inject GUI module for convenience
    inject(System.GUI)

    
    /**
     * Create label control
     *
     * params:  
     * return:  label (Label)
     */
    method create_label ()
    {
        // label attributes
        {Attribute.FONT     : {"typeface":"Verdana","size":18}}^attr

        // create label
        Label.new("Sample Label", attr)
    }

    
    /**
     * Create label image
     *
     * params:  
     * return:  label (Label)
     */
    method create_label_image ()
    {
        image = Image.load_image("c:\\nexus\\samples\\images\\price.jpg")

        // label attributes
        attr = {Attribute.IMAGE     : image}

        // create label
        Label.new("", attr)
    }

    
    /**
     * Button event handler
     *
     * params:  button (Button)
     * return:  result (Number)
     */
    method button1_action (button)
    {
        print("Button1 Action")
    }

    
    /**
     * Create button control
     *
     * params:  
     * return:  button (Button)
     */
    method create_button1 ()
    {
        button = Button.new("Button1")

        image = Image.load_image("c:\\nexus\\samples\\images\\screen.png")

        button.set_image(image)
        button.set_fgcolor({"red":255, "green":100, "blue":200})
        button.set_font({"typeface":"Verdana","bold":true,"size":20})

        handler = EventHandler.new(get_method("button1_action"), self)

        button.set_event(Event.ACTION, handler)

        button
    }

    
    /**
     * Button event handler
     *
     * params:  button (Button)
     * return:  result (Number)
     */
    method button2_action (button)
    {
        print("Button2 Action")
    }

    
    /**
     * Create button control
     *
     * params:  
     * return:  button (Button)
     */
    method create_button2 ()
    {
        button = Button.new("Button2")

        button.set_fgcolor({"red":255, "green":100, "blue":20})

        handler = EventHandler.new(get_method("button2_action"), self)

        button.set_event(Event.ACTION, handler)

        button
    }

    
    /**
     * Create dialog content
     *
     * params:  
     * return:  child (Container)
     */
    method create_child ()
    {
        // container
        VBox.new({HBox.new({create_label(), create_button1(), create_button2()}),
                 Fill.new(),
                 create_label_image()})
    }

    
    /**
     * Show dialog
     *
     * params:  
     * return:  
     */
    public method show ()
    {
        @dialog.show()

        System.GUI.main_loop()
    }

    
    /**
     * Initialize
     *
     * params:  
     * return:  dialog (SampleDialog)
     */
    method initialize ()
    {
        field @dialog

        // dialog attributes
        attr =
          {Attribute.TITLE    : "Sample Dialog",
           Attribute.SIZE     : {"width":300, "height":200},
           Attribute.OPACITY  : 255}

        // create dialog
        @dialog = Dialog.new(create_child(), attr)
        @dialog.set_bgcolor({"red":255, "green":100, "blue":20})
    }
}


/**
 * Entry point
 *
 * params:  
 * return:  
 */
method main ()
{
    dialog = SampleDialog.new()
    dialog.show()
}

main()