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

Example for using Scales.

#include <gltk.hh>

namespace gltk {
        template<typename T>
        struct RefPtr
        {
                T * ptr;
                RefPtr(T * p) : ptr(p) {}
                T * operator->() {return ptr;}
                operator T * () {return ptr;}
        };
}

class ExampleWindow : public gltk::Window
{
public:
        ExampleWindow();
        virtual ~ExampleWindow();

protected:
        //Signal handlers:
        void on_checkbutton_toggled();
        void on_menu_position(gltk::PositionType type);
        void on_adjustment1_value_changed();
        void on_adjustment2_value_changed();
        void on_button_quit();

        //Child widgets:
        gltk::VBox m_VBox_Top, m_VBox2, m_VBox_HScale;
        gltk::HBox m_HBox_Scales, m_HBox_Digits, m_HBox_PageSize;

        gltk::RefPtr<gltk::Adjustment> m_adjustment, m_adjustment_digits, m_adjustment_pagesize;

        gltk::VScale m_VScale;
        gltk::HScale m_HScale, m_Scale_Digits, m_Scale_PageSize;

        gltk::HSeparator m_Separator;

        gltk::CheckButton m_CheckButton;

        gltk::HScrollbar m_Scrollbar;

        gltk::Button m_Button_Quit;
};

#include <iostream>

ExampleWindow::ExampleWindow() :
        m_VBox2(false, 20),
        m_VBox_HScale(false, 10),
        m_HBox_Scales(false, 10),
        m_HBox_Digits(false, 10),
        m_HBox_PageSize(false, 10),

        // Value, lower, upper, step_increment, page_increment, page_size:
        // Note that the page_size value only makes a difference for
        // scrollbar widgets, and the highest value you'll get is actually
        // (upper - page_size).
        m_adjustment( gltk::Adjustment::create(0.0, 0.0, 101.0, 0.1, 1.0, 1.0) ),
        m_adjustment_digits( gltk::Adjustment::create(1.0, 0.0, 5.0) ),
        m_adjustment_pagesize( gltk::Adjustment::create(1.0, 1.0, 101.0) ),

        m_VScale(m_adjustment),
        m_HScale(m_adjustment),
        m_Scale_Digits(m_adjustment_digits),
        m_Scale_PageSize(m_adjustment_pagesize),

        // A checkbutton to control whether the value is displayed or not:
        m_CheckButton("Display value on scale widgets", 0),

        // Reuse the same adjustment again.
        // Notice how this causes the scales to always be updated
        // continuously when the scrollbar is moved.
        m_Scrollbar(m_adjustment),

        m_Button_Quit("Quit")
{
        set_title("range controls");

        //VScale:
        m_VScale.set_digits(1);
        m_VScale.set_value_pos(gltk::POS_TOP);
        m_VScale.set_draw_value();

        //HScale:
        m_HScale.set_digits(1);
        m_HScale.set_value_pos(gltk::POS_TOP);
        m_HScale.set_draw_value();
        m_HScale.set_size_request(200, 30);

        add(m_VBox_Top);
        m_VBox_Top.pack_start(m_VBox2);
        m_VBox2.set_border_width(10);
        m_VBox2.pack_start(m_HBox_Scales);

        //Put VScale and HScale (above scrollbar) side-by-side.
        m_HBox_Scales.pack_start(m_VScale);
        m_HBox_Scales.pack_start(m_VBox_HScale);

        m_VBox_HScale.pack_start(m_HScale);

        //Scrollbar:
        m_VBox_HScale.pack_start(m_Scrollbar);

        //CheckButton:
        m_CheckButton.set_active();
        m_CheckButton.signal_toggled().connect( sigc::mem_fun(*this,
        &ExampleWindow::on_checkbutton_toggled) );
        m_VBox2.pack_start(m_CheckButton);

        //Digits:
        m_HBox_Digits.pack_start(
                *gltk::manage(new gltk::Label("Scale Digits:", 0)), gltk::PACK_SHRINK);
        m_Scale_Digits.set_digits(0);
        m_adjustment_digits->signal_value_changed().connect(sigc::mem_fun(*this,
        &ExampleWindow::on_adjustment1_value_changed));
        m_HBox_Digits.pack_start(m_Scale_Digits);

        //Page Size:
        m_HBox_PageSize.pack_start(
                *gltk::manage(new gltk::Label("Scrollbar Page Size:", 0)),
                gltk::PACK_SHRINK);
        m_Scale_PageSize.set_digits(0);
        m_adjustment_pagesize->signal_value_changed().connect(sigc::mem_fun(*this,
        &ExampleWindow::on_adjustment2_value_changed));
        m_HBox_PageSize.pack_start(m_Scale_PageSize);

        m_VBox2.pack_start(m_HBox_Digits);
        m_VBox2.pack_start(m_HBox_PageSize);
        m_VBox_Top.pack_start(m_Separator, gltk::PACK_SHRINK);
        m_VBox_Top.pack_start(m_Button_Quit, gltk::PACK_SHRINK);

        m_Button_Quit.set_can_default();
        m_Button_Quit.grab_default();
        m_Button_Quit.signal_clicked().connect(sigc::mem_fun(*this,
        &ExampleWindow::on_button_quit));
        m_Button_Quit.set_border_width(10);

        show_all_children();
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_checkbutton_toggled()
{
        m_VScale.set_draw_value(m_CheckButton.get_active());
        m_HScale.set_draw_value(m_CheckButton.get_active());
}

void ExampleWindow::on_menu_position(gltk::PositionType postype)
{
        m_VScale.set_value_pos(postype);
        m_HScale.set_value_pos(postype);
}

void ExampleWindow::on_adjustment1_value_changed()
{
        const double val = m_adjustment_digits->get_value();
        m_VScale.set_digits((int)val);
        m_HScale.set_digits((int)val);
}

void ExampleWindow::on_adjustment2_value_changed()
{
        const double val = m_adjustment_pagesize->get_value();
        m_adjustment->set_page_size(val);
        m_adjustment->set_page_increment(val);

        // Note that we don't have to emit the "changed" signal
        // because gtkmm does this for us.
}

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

int main(int argc, char *argv[])
{
        gltk::Main kit(argc, argv);

        ExampleWindow window;
        //Shows the window and returns when it is closed.
        gltk::Main::run(window);

        return 0;
}
 All Classes Namespaces Files Functions Variables Enumerations Enumerator