自作ダイアログ

概要

ダイアログを自作する。

Gtk::Dialog を継承して自作ダイアログを作成すれば良い。

ソース

dialog.cpp

#include <gtkmm.h>

class MyDialog : public Gtk::Dialog
{
    Gtk::Label m_label;

public:
    MyDialog( const Glib::ustring& title, Gtk::Window& parent, bool modal = false, bool use_separator = false );
};


MyDialog::MyDialog( const Glib::ustring& title, Gtk::Window& parent, bool modal, bool use_separator )
    : Gtk::Dialog( title, parent, modal, use_separator )
    , m_label( "テスト" )
{

    // widgetをaddする時は直接 add せずに
    // vboxを取得してからそれにaddする
    get_vbox()->add( m_label );

    // ボタン追加
    add_button( Gtk::Stock::OK, Gtk::RESPONSE_OK );
    add_button( Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL );

    show_all_children();
}


/////////////////////////////////////////////


class MainWin : public Gtk::Window
{
    Gtk::Button m_bt;

public:
    MainWin();

private:
    void on_bt_clicked();
};

MainWin::MainWin()
    : m_bt( "自作ダイアログ表示" )
{
    m_bt.signal_clicked().connect( 
                sigc::mem_fun( *this, &MainWin::on_bt_clicked ) );
    add( m_bt );
    show_all_children();
}

void MainWin::on_bt_clicked()
{
    MyDialog diag( "自作ダイアログ", *this );
    diag.run();
}

int main( int argc, char *argv[] )
{
    Gtk::Main kit( argc, argv );
    MainWin mainwin;
    Gtk::Main::run( mainwin );

    return 0;
}

コンパイル

必要なコンパイルオプションは pkg-config を使って取得する。

g++ dialog.cpp -o dialog `pkg-config gtkmm-2.4 --cflags --libs`

結果