モーダルダイアログ

Fl_Window(およびその派生クラス)を派生した新しいウィンドウクラスを作成して他のウィンドウから呼び出すことでダイアログを生成できる. Fl_Windowクラスにはウィンドウをモーダルにする関数set_modal()が用意されている.

ダイアログクラス

ここでは例としてFl_Windowを派生した以下のrxFlTextDialogクラスを生成してみる. rxFlTextDialogクラスではテキストインプット1つとOK,Cancelボタンを定義している.

#include <string>

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Button.H>

using namespace std;

//-----------------------------------------------------------------------------
//! rxFlTextDialogクラス - fltkによるテキストダイアログ
//-----------------------------------------------------------------------------
class rxFlTextDialog : public Fl_Window
{
protected:
    Fl_Window *m_pParent;    //!< 親クラス
    Fl_Input *m_pInputText;    //!< テキスト入力
 
public:
    string m_strText;
 
public:
    //! コンストラクタ
    rxFlTextDialog(int w_, int h_, const char* title, const char* label, const char* text_label, void *parent)
        : Fl_Window(w_, h_, title), m_pInputText(0)
    {
        m_pParent = (Fl_Window*)parent;
        m_strText = "";
 
        int margin = 12;
        int xs, ys;
        int ws, hs;
 
        {
            Fl_Scroll *g = new Fl_Scroll(0, 0, w(), h()-40, 0);
            g->box(FL_FLAT_BOX);
 
            xs = margin;
            ys = margin;
            ws = w()-2.5*margin;
            hs = h()-(margin+50);
 
            //
            // テキスト入力
            //
            Fl_Group* sg = new Fl_Group(xs, ys, ws, hs, label);
            {
                sg->box(FL_DOWN_BOX);
                sg->labelsize(12);
                sg->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
 
                int dx = strlen(text_label)*10;
                ys += 25;
				m_pInputText = new Fl_Input(xs+dx, ys, ws-(dx+margin*2), 25, text_label);
            
                sg->resizable(NULL);
                sg->end();
            }
        
            g->end();
            Fl_Group::current()->resizable(g);
        }
        {
            ws = 80;
            hs = 25;
            xs = w()-2*(ws+margin);
            ys = h()-(margin+hs);
 
            Fl_Button *button;
                
            // OKボタン
            button = new Fl_Button(xs, ys, ws, hs, "OK");
            button->callback(OnButtonOK_s, this);
            button->down_box(FL_UP_BOX);
                
            // OKボタン
            button = new Fl_Button(xs+(ws+margin), ys, ws, hs, "Cancel");
            button->callback(OnButtonCancel_s, this);
            button->down_box(FL_UP_BOX);
        }
 
        resizable(this);
        end();
    }
 
    //! デストラクタ
    ~rxFlTextDialog()
    {
        if(m_pInputText) delete m_pInputText;
    }
 
public:
    // イベントハンドラ
    static void OnButtonOK_s(Fl_Widget *widget, void* x)
    {
        ((rxFlTextDialog*)x)->OnButtonOK();
    }
    inline void OnButtonOK(void)
    {
        m_strText = m_pInputText->value();
        hide();
    }
 
    static void OnButtonCancel_s(Fl_Widget *widget, void* x)
    {
        ((rxFlTextDialog*)x)->OnButtonCancel();
    }
    inline void OnButtonCancel(void)
    {
        hide();
    }
};

ダイアログの呼び出し

上記のダイアログクラスを呼び出す際の例を以下に示す.

rxFlTextDialog *dlg = new rxFlTextDialog(300, 150, "title", "group", "value", this);
	dlg->set_modal();
	dlg->show();
	while(dlg->visible()){
		Fl::wait();
	}

	if(!dlg->m_strText.empty()){
		cout << "Input : " << dlg->m_strText << endl;
	}

	delete dlg;

set_modal()でモーダルウィンドウにして, show()でダイアログを表示.

上記のコードでダイアログを表示した結果は以下(Windows10環境).

fltk_modal_dialog.jpg

添付ファイル: filefltk_modal_dialog.jpg 776件 [詳細]

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2024-03-08 (金) 18:06:10