gltk 0.3
a C++ widget library built on GLUT
spinb.cc
spinb.png

Use example of SpinButton widget.

/*
  This is adapted from the SpinButton example of gtkmm:
  http://developer.gnome.org/gtkmm-tutorial/3.0/sec-spinbutton.html.en
 */

#include <gltk.hh>
#include <iostream>
#include <cstdio>
using namespace gltk;

class ExampleWindow :
        public Window
{
public:
        ExampleWindow();
        virtual ~ExampleWindow();
protected:
        void on_checkbutton_snap();
        void on_checkbutton_numeric();
        void on_spinbutton_digits_changed();
        void on_button_close();

        enum enumValueFormats {
                VALUE_FORMAT_INT,
                VALUE_FORMAT_FLOAT
        };
        void on_button_getvalue(enumValueFormats display);

        Frame m_Frame_NotAccelerated, m_Frame_Accelerated;
        Box m_HBox_NotAccelerated, m_HBox_Accelerated, m_HBox_Buttons;
        Box m_VBox_Main, m_VBox, m_VBox_Day, m_VBox_Month, m_VBox_Year, m_VBox_Accelerated, m_VBox_Value, m_VBox_Digits;
        Label m_Label_Day, m_Label_Month, m_Label_Year, m_Label_Value, m_Label_Digits, m_Label_ShowValue;
        SpinButton m_SpinButton_Day, m_SpinButton_Month, m_SpinButton_Year, m_SpinButton_Value, m_SpinButton_Digits;
        CheckButton m_CheckButton_Snap, m_CheckButton_Numeric;
        Button m_Button_Int, m_Button_Float, m_Button_Close;
};


ExampleWindow::ExampleWindow() :
        m_Frame_NotAccelerated("Not accelerated"),
        m_Frame_Accelerated("Accelerated"),
        m_VBox_Main(ORIENTATION_VERTICAL, 5),
        m_VBox(ORIENTATION_VERTICAL),
        m_VBox_Day(ORIENTATION_VERTICAL),
        m_VBox_Month(ORIENTATION_VERTICAL),
        m_VBox_Year(ORIENTATION_VERTICAL),
        m_VBox_Accelerated(ORIENTATION_VERTICAL),
        m_VBox_Value(ORIENTATION_VERTICAL),
        m_VBox_Digits(ORIENTATION_VERTICAL),
        m_Label_Day("Day: "),
        m_Label_Month("Month: "),
        m_Label_Year("Year: "),
        m_Label_Value("Value: "),
        m_Label_Digits("Digits: "),
        m_SpinButton_Value(1.0, 2),
        m_CheckButton_Snap("Snap to 0.5-ticks"),
        m_CheckButton_Numeric("Numeric only input mode"),
        m_Button_Int("Value as Int"),
        m_Button_Float("Value as Float"),
        m_Button_Close("Close")
{
        m_SpinButton_Day.configure(1.0, 1.0, 31.0, 1.0, 5.0, 0.0);
        m_SpinButton_Month.configure(1.0, 1.0, 12.0, 1.0, 5.0, 0.0);
        m_SpinButton_Year.configure(1998.0, 0.0, 2100.0, 1.0, 100.0, 0.0);
        m_SpinButton_Value.configure(0.0, -10000.0, 10000.0, 0.5, 100.0, 0.0);
        m_SpinButton_Digits.configure(2.0, 1.0, 5.0, 1.0, 1.0, 0.0);

        set_title("SpinButton");

        m_VBox_Main.set_border_width(10);
        add(m_VBox_Main);

        m_VBox_Main.pack_start(m_Frame_NotAccelerated);

        m_VBox.set_border_width(5);
        m_Frame_NotAccelerated.add(m_VBox);

        /* Day, month, year spinners */
        m_VBox.pack_start(m_HBox_NotAccelerated);
        m_VBox_Day.pack_start(m_Label_Day);
        m_SpinButton_Day.set_wrap();
        m_VBox_Day.pack_start(m_SpinButton_Day);
        m_HBox_NotAccelerated.pack_start(m_VBox_Day);
        m_VBox_Month.pack_start(m_Label_Month);
        m_SpinButton_Month.set_wrap();
        m_VBox_Month.pack_start(m_SpinButton_Month);
        m_HBox_NotAccelerated.pack_start(m_VBox_Month);
        m_VBox_Year.pack_start(m_Label_Year);
        m_SpinButton_Year.set_wrap();
        m_VBox_Year.pack_start(m_SpinButton_Year);
        m_HBox_NotAccelerated.pack_start(m_VBox_Year);

        // Accelerated:
        m_VBox_Main.pack_start(m_Frame_Accelerated);
        m_VBox_Accelerated.set_border_width(5);
        m_Frame_Accelerated.add(m_VBox_Accelerated);
        m_VBox_Accelerated.pack_start(m_HBox_Accelerated);
        m_HBox_Accelerated.pack_start(m_VBox_Value);
        m_VBox_Value.pack_start(m_Label_Value);
        m_SpinButton_Value.set_wrap();
        m_VBox_Value.pack_start(m_SpinButton_Value);
        m_HBox_Accelerated.pack_start(m_VBox_Digits);
        m_VBox_Digits.pack_start(m_Label_Digits);
        m_SpinButton_Digits.set_wrap();
        m_SpinButton_Digits.signal_value_changed()
                .connect(sigc::mem_fun(this, & ExampleWindow::on_spinbutton_digits_changed));
        m_VBox_Digits.pack_start(m_SpinButton_Digits);

        // CheckButtons:
        m_VBox_Accelerated.pack_start(m_CheckButton_Snap);
        m_CheckButton_Snap.signal_toggled()
                .connect(sigc::mem_fun(this, & ExampleWindow::on_checkbutton_snap));
        m_VBox_Accelerated.pack_start(m_CheckButton_Numeric);
        m_CheckButton_Numeric.set_active();
        m_CheckButton_Numeric.signal_toggled()
                .connect(sigc::mem_fun(this, & ExampleWindow::on_checkbutton_numeric));

        // Buttons:
        m_VBox_Accelerated.pack_start(m_HBox_Buttons);
        m_Button_Int.signal_clicked()
                .connect(sigc::bind(sigc::mem_fun(this, & ExampleWindow::on_button_getvalue), VALUE_FORMAT_INT));
        m_HBox_Buttons.pack_start(m_Button_Int);
        m_Button_Float.signal_clicked()
                .connect(sigc::bind(sigc::mem_fun(this, & ExampleWindow::on_button_getvalue), VALUE_FORMAT_FLOAT));
        m_HBox_Buttons.pack_start(m_Button_Float);

        Bin * b = new Bin;
        b->add(m_Label_ShowValue);
        m_VBox_Accelerated.pack_start(b);
        m_Label_ShowValue.set_text("0");

        // Close button:
        m_Button_Close.signal_clicked()
                .connect(sigc::mem_fun(this, & ExampleWindow::on_button_close));
        m_VBox_Main.pack_start(& m_Button_Close);

        show_all_children();
}

ExampleWindow::~ExampleWindow()
{}


void ExampleWindow::on_button_close()
{
        hide();
}

void ExampleWindow::on_checkbutton_snap()
{
        m_SpinButton_Value.set_snap_to_ticks(m_CheckButton_Snap.get_active());
}

void ExampleWindow::on_checkbutton_numeric()
{
        //  m_SpinButton_Value.set_numeric(m_CheckButton_Numeric.get_active());
}

void ExampleWindow::on_spinbutton_digits_changed()
{
        m_SpinButton_Value.set_digits(m_SpinButton_Digits.get_value_as_int());
}

void ExampleWindow::on_button_getvalue(enumValueFormats display)
{
        char buf[32];

        if (display == VALUE_FORMAT_INT) sprintf(buf, "%d", m_SpinButton_Value.get_value_as_int());
        else sprintf(buf, "%0.*f", m_SpinButton_Value.get_digits(), m_SpinButton_Value.get_value());
        m_Label_ShowValue.set_text(buf);
}


int main(int argc, char *argv[])
{
        Main kit(argc, argv);
        ExampleWindow window;
        Main::run(window);
        return 0;
}
 All Classes Namespaces Files Functions Variables Enumerations Enumerator