'2024/02/11'에 해당되는 글 2건

  1. 2024.02.11 [wxWidgets] 새 글 기능 구현하기.
  2. 2024.02.11 [wxWidgets] 체크 할 수 있는 메뉴 추가하기. 1

 notepad을 따라한다고 했을때, notepad을 구현하는 방법은 다음과 같이 한다.

 wxWidgets.h

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
#pragma once
#include "wx/wx.h"
#include <wx/filedlg.h>
#include <wx/textctrl.h>
#include <wx/splitter.h>
 
// Font Dialog
#include <wx/fontdlg.h>
 
// 파일을 읽어 들이기 위한 용도.
#include <fstream>
#include <sstream>
 
#include "wxOptionsDialog.h"
 
enum
{
    ID_QUIT,
    ID_WORD_WRAP,
    ID_FontSetting,
};
 
enum {
    MY_EVENT_ID = 10001,
};
 
// ID 값 정의
enum
{
    ID_Settings_Menu = wxID_HIGHEST + 1 // 사용자 정의 ID
};
 
class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};
 
class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString& title);
 
    void OnQuit(wxCommandEvent& event);
 
private:
    wxTextCtrl* textControl;
    wxOptionDialog* dialog;
    wxFont* font;
 
    // 메뉴바 및 메뉴 변수.
    wxMenuBar* menuBar;
    wxMenu* menuFile;
    wxMenu* menuFormat;
 
    void OnNew(wxCommandEvent& event);
    void OnOpen(wxCommandEvent& event);
    void OnSave(wxCommandEvent& event);
    void OnButtonClick(wxCommandEvent& event);
 
    // 세팅 창을 띄우기 위한 메소드
    void OnToggleWordWrap(wxCommandEvent& event);
    void OnFontSetting(wxCommandEvent& event);
 
    // 이벤트를 받기 위한 메소드
    void OnMyCustomEvent(MyCustomEvent& event);
};
 
cs

 

wxMain.cpp

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "wxMain.h"
 
wxIMPLEMENT_APP(MyApp);
 
bool MyApp::OnInit()
{
    MyFrame* frame = new MyFrame("No Title - Notepad");
    frame->Show(true);
    return true;
}
 
MyFrame::MyFrame(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title)
{
    menuFile = new wxMenu;
    menuFile->Append(wxID_NEW, "&New\tCtrl-N""New a Notepad");
    menuFile->Append(wxID_OPEN, "&Open\tCtrl-O""Open a file");
    menuFile->Append(wxID_SAVE, "&Save\tCtrl-S""Save the file");
    menuFile->AppendSeparator();
    menuFile->Append(ID_QUIT, "E&xit\tAlt-X""프로그램 종료");
 
    menuFormat = new wxMenu;
    menuFormat->AppendCheckItem(ID_WORD_WRAP, "Word &Wrap\tCtrl+W""Toggle word wrapping.");
    menuFormat->Append(ID_FontSetting, "&Font""Font Set Menu");
    
 
    menuBar = new wxMenuBar;
    menuBar->Append(menuFile, "&File");
    menuBar->Append(menuFormat, "&F&omat");
 
    SetMenuBar(menuBar);
 
    textControl = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
 
    // sizer를 생성하여 텍스트 컨트롤의 크기를 조정합니다.
    wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
    sizer->Add(textControl, 1, wxEXPAND | wxALL, 0); // wxEXPAND는 컨트롤이 sizer의 가능한 모든 공간을 차지하도록 합니다. 1은 비율을 의미하며, 이 경우 다른 컨트롤이 없으므로 전체 크기를 차지합니다.
 
    // 프레임에 sizer를 설정합니다.
    this->SetSizer(sizer);
    this->Layout(); // sizer를 강제로 다시 계산하여 적용합니다.
 
    // 폰트 설정
    wxFont font(16, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
    textControl->SetFont(font);
 
    CreateStatusBar();
    SetStatusText("Ready");
 
    // 이벤트 핸들러 연결
    Bind(wxEVT_MENU, &MyFrame::OnNew, this, wxID_NEW);
    Bind(wxEVT_MENU, &MyFrame::OnOpen, this, wxID_OPEN);
    Bind(wxEVT_MENU, &MyFrame::OnSave, this, wxID_SAVE);
 
    Bind(wxEVT_MENU, &MyFrame::OnQuit, this, ID_QUIT);
 
    // 메뉴 Format에 관련된 이벤트 핸들러
    Bind(wxEVT_MENU, &MyFrame::OnToggleWordWrap, this, ID_WORD_WRAP);
    Bind(wxEVT_MENU, &MyFrame::OnFontSetting, this, ID_FontSetting);
 
    // 이벤트 처리기 등록
    Bind(MY_CUSTOM_EVENT, &MyFrame::OnMyCustomEvent, this);
}
 
void MyFrame::OnQuit(wxCommandEvent& event)
{
    Close(true);
}
 
void MyFrame::OnNew(wxCommandEvent& event) {
    // 텍스트 컨트롤의 내용을 비웁니다.
    textControl->Clear();
 
    // 타이틀에 메시지를 새로 작성합니다.
    wxString titleNames = "No Title";
    titleNames += " - Notepad";
    SetTitle(titleNames);
 
    // 상태 표시줄에 메시지를 표시합니다.
    SetStatusText("New document created.");
}
 
void MyFrame::OnOpen(wxCommandEvent& event)
{
    wxFileDialog openFileDialog(this, _("Open TXT file"), """",
        "TXT files (*.txt)|*.txt", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    if (openFileDialog.ShowModal() == wxID_CANCEL)
        return// 사용자가 취소했을 때
 
    std::ifstream file(openFileDialog.GetPath().ToStdString());
    // 파일을 열고 텍스트 컨트롤에 내용을 로드합니다.
    if (textControl->LoadFile(openFileDialog.GetPath())) {
        std::stringstream buffer;
        buffer << file.rdbuf(); // 파일의 내용을 buffer에 읽어 들입니다.
        file.close(); // 파일을 닫습니다.
 
        // textControl의 내용을 갱신합니다.
        textControl->SetValue(buffer.str());
        //textControl->SetLabelText(buffer.str());
        
        wxString titleNames = openFileDialog.GetFilename();
        titleNames += " - Notepad";
        // 타이틀을 열린 파일의 이름으로 설정합니다.
        SetTitle(titleNames);
 
    }
    else {
        wxMessageBox("Cannot open File!""Error", wxOK | wxICON_ERROR);
    }
}
 
void MyFrame::OnSave(wxCommandEvent& event)
{
    wxFileDialog saveFileDialog(this, _("Save TXT file"), """",
        "TXT files (*.txt)|*.txt", wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
    if (saveFileDialog.ShowModal() == wxID_CANCEL)
        return// 사용자가 취소했을 때
 
    // 현재 텍스트 컨트롤의 내용을 파일에 저장합니다.
    textControl->SaveFile(saveFileDialog.GetPath());
 
    wxString titleNames = saveFileDialog.GetFilename();
    titleNames += " - Notepad";
    // 타이틀을 열린 파일의 이름으로 설정합니다.
    SetTitle(titleNames);
}
 
void MyFrame::OnToggleWordWrap(wxCommandEvent& event)
{
    bool isChecked = menuFormat->IsChecked(ID_WORD_WRAP);
    textControl->SetWindowStyleFlag(isChecked ? (textControl->GetWindowStyleFlag() | wxTE_WORDWRAP) : (textControl->GetWindowStyleFlag() & ~wxTE_WORDWRAP));
    textControl->Refresh(); // 화면 갱신
}
 
void MyFrame::OnFontSetting(wxCommandEvent& event)
{
    /*dialog = new wxOptionDialog(this, wxID_ANY, "Settings");
    dialog->ShowModal();*/
 
    wxFontData fontData;
    fontData.SetInitialFont(textControl->GetFont());
    fontData.SetColour(textControl->GetForegroundColour());
 
    wxFontDialog fontDialog(this, fontData);
    if (fontDialog.ShowModal() == wxID_OK)
    {
        wxFontData retData = fontDialog.GetFontData();
        wxFont font = retData.GetChosenFont();
        wxColour colour = retData.GetColour();
 
        textControl->SetFont(font);
        textControl->SetForegroundColour(colour);
    }
}
 
// 이벤트 처리 함수 구현
void MyFrame::OnMyCustomEvent(MyCustomEvent& event)
{
    const wxFont info = event.GetwxFont();
 
    int fontSize = info.GetPointSize();
    // 이벤트와 함께 전달된 정보 처리
    textControl->SetFont(info);
 
    //textControl->SetFont(font);
    dialog->Destroy(); // dialog를 안전하게 삭제
    delete dialog;
}
cs

 

 이렇게 하면, New을 클릭했을 경우에 Notepad의 값을 Clear되면서 초기화 할 수 있게 된다.

 

 

Posted by JunkMam
,

 wxWidgets에서 설정한 상태를 확인하거나 적용해서 출력하는 부분을 구현하고자 한다.

 

 다음과 같은 방식을 wxWidgets에선 다음과 같은 메소드를 제공한다.

1
2
menuOptions->AppendCheckItem(ID_WORD_WRAP, "Word &Wrap\tCtrl+W""Toggle word wrapping.");
 
cs

 

 그리고, 이걸 토글해주는 이벤트 핸들러를 적용해야된다.

1
2
3
4
5
6
void MyFrame::OnToggleWordWrap(wxCommandEvent& event)
{
    bool isChecked = menuOptions->IsChecked(ID_WORD_WRAP);
    textControl->SetWindowStyleFlag(isChecked ? (textControl->GetWindowStyleFlag() | wxTE_WORDWRAP) : (textControl->GetWindowStyleFlag() & ~wxTE_WORDWRAP));
    textControl->Refresh(); // 화면 갱신
}
cs

 

 그리고, 이 핸들러를 연결하기 위해서 Bind를 이용해서 연결해준다.

1
2
Bind(wxEVT_MENU, &MyFrame::OnToggleWordWrap, this, ID_WORD_WRAP);
 
cs

 

 전체적으로 적용하면,

 wxMain.cpp와 wxMain.h에 다음과 같이 적용한다.

 

wxMain.h

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
#pragma once
#include "wx/wx.h"
#include <wx/filedlg.h>
#include <wx/textctrl.h>
#include <wx/splitter.h>
 
// Font Dialog
#include <wx/fontdlg.h>
 
// 파일을 읽어 들이기 위한 용도.
#include <fstream>
#include <sstream>
 
#include "wxOptionsDialog.h"
 
enum
{
    ID_QUIT,
    ID_WORD_WRAP,
    ID_FontSetting,
};
 
enum {
    MY_EVENT_ID = 10001,
};
 
// ID 값 정의
enum
{
    ID_Settings_Menu = wxID_HIGHEST + 1 // 사용자 정의 ID
};
 
class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};
 
class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString& title);
 
    void OnQuit(wxCommandEvent& event);
 
private:
    wxTextCtrl* textControl;
    wxOptionDialog* dialog;
    wxFont* font;
 
    // 메뉴바 및 메뉴 변수.
    wxMenuBar* menuBar;
    wxMenu* menuFile;
    wxMenu* menuFormat;
 
    void OnOpen(wxCommandEvent& event);
    void OnSave(wxCommandEvent& event);
    void OnButtonClick(wxCommandEvent& event);
 
    // 세팅 창을 띄우기 위한 메소드
    void OnToggleWordWrap(wxCommandEvent& event);
    void OnFontSetting(wxCommandEvent& event);
 
    // 이벤트를 받기 위한 메소드
    void OnMyCustomEvent(MyCustomEvent& event);
};
 
cs

 

wxMain.cpp

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "wxMain.h"
 
wxIMPLEMENT_APP(MyApp);
 
bool MyApp::OnInit()
{
    MyFrame* frame = new MyFrame("Serial Graph");
    frame->Show(true);
    return true;
}
 
MyFrame::MyFrame(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title)
{
    menuFile = new wxMenu;
    menuFile->Append(wxID_OPEN, "&Open\tCtrl-O""Open a file");
    menuFile->Append(wxID_SAVE, "&Save\tCtrl-S""Save the file");
    menuFile->AppendSeparator();
    menuFile->Append(ID_QUIT, "E&xit\tAlt-X""프로그램 종료");
 
    menuFormat = new wxMenu;
    menuFormat->AppendCheckItem(ID_WORD_WRAP, "Word &Wrap\tCtrl+W""Toggle word wrapping.");
    menuFormat->Append(ID_FontSetting, "&Font""Font Set Menu");
    
 
    menuBar = new wxMenuBar;
    menuBar->Append(menuFile, "&File");
    menuBar->Append(menuFormat, "&F&omat");
 
    SetMenuBar(menuBar);
 
    textControl = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
 
    // sizer를 생성하여 텍스트 컨트롤의 크기를 조정합니다.
    wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
    sizer->Add(textControl, 1, wxEXPAND | wxALL, 0); // wxEXPAND는 컨트롤이 sizer의 가능한 모든 공간을 차지하도록 합니다. 1은 비율을 의미하며, 이 경우 다른 컨트롤이 없으므로 전체 크기를 차지합니다.
 
    // 프레임에 sizer를 설정합니다.
    this->SetSizer(sizer);
    this->Layout(); // sizer를 강제로 다시 계산하여 적용합니다.
 
    // 폰트 설정
    wxFont font(16, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
    textControl->SetFont(font);
 
    CreateStatusBar();
    SetStatusText("Ready");
 
    // 이벤트 핸들러 연결
    Bind(wxEVT_MENU, &MyFrame::OnQuit, this, ID_QUIT);
    Bind(wxEVT_MENU, &MyFrame::OnOpen, this, wxID_OPEN);
    Bind(wxEVT_MENU, &MyFrame::OnSave, this, wxID_SAVE);
 
    // 메뉴 Format에 관련된 이벤트 핸들러
    Bind(wxEVT_MENU, &MyFrame::OnToggleWordWrap, this, ID_WORD_WRAP);
    Bind(wxEVT_MENU, &MyFrame::OnFontSetting, this, ID_FontSetting);
 
    // 이벤트 처리기 등록
    Bind(MY_CUSTOM_EVENT, &MyFrame::OnMyCustomEvent, this);
}
 
void MyFrame::OnQuit(wxCommandEvent& event)
{
    Close(true);
}
 
void MyFrame::OnOpen(wxCommandEvent& event)
{
    wxFileDialog openFileDialog(this, _("Open TXT file"), """",
        "TXT files (*.txt)|*.txt", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    if (openFileDialog.ShowModal() == wxID_CANCEL)
        return// 사용자가 취소했을 때
 
    std::ifstream file(openFileDialog.GetPath().ToStdString());
    // 파일을 열고 텍스트 컨트롤에 내용을 로드합니다.
    if (textControl->LoadFile(openFileDialog.GetPath())) {
        std::stringstream buffer;
        buffer << file.rdbuf(); // 파일의 내용을 buffer에 읽어 들입니다.
        file.close(); // 파일을 닫습니다.
 
        // textControl의 내용을 갱신합니다.
        textControl->SetValue(buffer.str());
        //textControl->SetLabelText(buffer.str());
        // 타이틀을 열린 파일의 이름으로 설정합니다.
        SetTitle(openFileDialog.GetFilename());
 
    }
    else {
        wxMessageBox("Cannot open File!""Error", wxOK | wxICON_ERROR);
    }
}
 
void MyFrame::OnSave(wxCommandEvent& event)
{
    wxFileDialog saveFileDialog(this, _("Save TXT file"), """",
        "TXT files (*.txt)|*.txt", wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
    if (saveFileDialog.ShowModal() == wxID_CANCEL)
        return// 사용자가 취소했을 때
 
    // 현재 텍스트 컨트롤의 내용을 파일에 저장합니다.
    textControl->SaveFile(saveFileDialog.GetPath());
}
 
void MyFrame::OnToggleWordWrap(wxCommandEvent& event)
{
    bool isChecked = menuFormat->IsChecked(ID_WORD_WRAP);
    textControl->SetWindowStyleFlag(isChecked ? (textControl->GetWindowStyleFlag() | wxTE_WORDWRAP) : (textControl->GetWindowStyleFlag() & ~wxTE_WORDWRAP));
    textControl->Refresh(); // 화면 갱신
}
 
void MyFrame::OnFontSetting(wxCommandEvent& event)
{
    /*dialog = new wxOptionDialog(this, wxID_ANY, "Settings");
    dialog->ShowModal();*/
 
    wxFontData fontData;
    fontData.SetInitialFont(textControl->GetFont());
    fontData.SetColour(textControl->GetForegroundColour());
 
    wxFontDialog fontDialog(this, fontData);
    if (fontDialog.ShowModal() == wxID_OK)
    {
        wxFontData retData = fontDialog.GetFontData();
        wxFont font = retData.GetChosenFont();
        wxColour colour = retData.GetColour();
 
        textControl->SetFont(font);
        textControl->SetForegroundColour(colour);
    }
 
    //if (dialog.ShowModal() == wxID_OK)
    //{
    //    // 사용자가 설정을 변경하고 OK를 클릭했을 때의 처리
    //    SetStatusText("Settings Updated");
    //}
}
 
// 이벤트 처리 함수 구현
void MyFrame::OnMyCustomEvent(MyCustomEvent& event)
{
    const wxFont info = event.GetwxFont();
 
    int fontSize = info.GetPointSize();
    // 이벤트와 함께 전달된 정보 처리
    textControl->SetFont(info);
 
    //textControl->SetFont(font);
    dialog->Destroy(); // dialog를 안전하게 삭제
    delete dialog;
}
cs

 

 

 적용하면, 다음과 같은 창이 완성된다.

 

 

 

Posted by JunkMam
,