モーダルダイアログ

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

ダイアログクラス

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

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#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;
 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
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;
                
                        button = new Fl_Button(xs, ys, ws, hs, "OK");
            button->callback(OnButtonOK_s, this);
            button->down_box(FL_UP_BOX);
                
                        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();
    }
};

ダイアログの呼び出し

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

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
    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 723件 [詳細]

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2022-11-30 (水) 13:48:12