wxWidgets integration module example
Below you can find source code of a wxwidgets_integration
example that
showcases usage of the libbase
integration module for wxWidgets
library.
This example is located in the /examples/wxwidgets_integration/
directory in
the repository and - if enabled -
will be built along the libbase
library itself.
CMakeLists.txt
1find_package(wxWidgets CONFIG REQUIRED)
2
3add_executable(wxwidgets_integration_example WIN32 "")
4
5target_compile_options(wxwidgets_integration_example
6 PRIVATE
7 ${LIBBASE_COMPILE_FLAGS}
8)
9
10target_link_libraries(wxwidgets_integration_example
11 PRIVATE
12 libbase
13 libbase_wx
14 wxWidgets::wxWidgets
15)
16
17target_sources(wxwidgets_integration_example
18 PRIVATE
19 app.cc
20 app.h
21 main_frame.cc
22 main_frame.h
23 main.cc
24)
main.cc
1#include "app.h"
2
3#include "wx/app.h"
4
5wxIMPLEMENT_APP(IntegrationExample);
app.h
1#pragma once
2
3#include <memory>
4
5#include "base/message_loop/wx/wx_message_loop_attachment.h"
6#include "base/threading/thread.h"
7
8#include "wx/app.h"
9
10class IntegrationExample : public wxApp {
11 public:
12 bool OnInit() override;
13 int OnExit() override;
14
15 private:
16 std::unique_ptr<base::wx::WxMessageLoopAttachment> message_loop_attachment_;
17 base::Thread worker_thread_;
18};
app.cc
1#include "app.h"
2#include "main_frame.h"
3
4bool IntegrationExample::OnInit() {
5 message_loop_attachment_ =
6 std::make_unique<base::wx::WxMessageLoopAttachment>(this);
7 worker_thread_.Start();
8
9 auto* frame = new MainFrame(worker_thread_.TaskRunner());
10 frame->Show(true);
11 return true;
12}
13
14int IntegrationExample::OnExit() {
15 worker_thread_.Stop();
16 message_loop_attachment_.reset();
17 return 0;
18}
main_frame.h
1#pragma once
2
3#include <memory>
4
5#include "base/sequenced_task_runner.h"
6#include "base/synchronization/waitable_event.h"
7
8#include "wx/button.h"
9#include "wx/frame.h"
10
11class MainFrame : public wxFrame {
12 public:
13 MainFrame(std::shared_ptr<base::SequencedTaskRunner> task_runner);
14
15 private:
16 void OnButtonClicked(wxCommandEvent& event);
17 void ShowMessageBox();
18
19 std::shared_ptr<base::SequencedTaskRunner> task_runner_;
20 wxButton* m_button;
21
22 base::WeakPtr<MainFrame> weak_this_;
23 base::WeakPtrFactory<MainFrame> weak_factory_{this};
24};
main_frame.cc
1#include "main_frame.h"
2
3#include "wx/msgdlg.h"
4#include "wx/panel.h"
5
6MainFrame::MainFrame(std::shared_ptr<base::SequencedTaskRunner> task_runner)
7 : wxFrame(nullptr,
8 wxID_ANY,
9 "wxWidgets integration example",
10 wxDefaultPosition,
11 wxSize(400, 200)),
12 task_runner_(std::move(task_runner)),
13 weak_factory_(this) {
14 weak_this_ = weak_factory_.GetWeakPtr();
15
16 auto* panel = new wxPanel(this, wxID_ANY);
17 m_button = new wxButton(panel, wxID_ANY, "Click Me", wxPoint(150, 70),
18 wxDefaultSize);
19
20 m_button->Bind(wxEVT_BUTTON, &MainFrame::OnButtonClicked, this);
21}
22
23void MainFrame::OnButtonClicked(wxCommandEvent&) {
24 DCHECK(!task_runner_->RunsTasksInCurrentSequence());
25
26 task_runner_->PostTaskAndReply(
27 FROM_HERE, base::BindOnce([]() {
28 // some work on worker thread
29 }),
30 base::BindOnce(&MainFrame::ShowMessageBox, weak_this_));
31}
32
33void MainFrame::ShowMessageBox() {
34 DCHECK(!task_runner_->RunsTasksInCurrentSequence());
35
36 wxMessageBox("Hello world!", "Message", wxOK | wxICON_INFORMATION);
37}