Skip to content

Commit

Permalink
Add KeyEvents and ProcessEvent examples
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Apr 26, 2020
1 parent fd6e67a commit adbd395
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Events/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ project(Events)
add_subdirectory(CustomEvent)
add_subdirectory(FrameClick)
add_subdirectory(FrameAndEvents)
add_subdirectory(KeyEvents)
add_subdirectory(ProcessEvent)
11 changes: 11 additions & 0 deletions src/Events/KeyEvents/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.0)
project(KeyEvents)

find_package(wxWidgets REQUIRED)
include(${wxWidgets_USE_FILE})
link_libraries(${wxWidgets_LIBRARIES})

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(${PROJECT_NAME} WIN32 MACOSX_BUNDLE KeyEvents.cpp)
set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "Events")
51 changes: 51 additions & 0 deletions src/Events/KeyEvents/KeyEvents.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <wx/wx.h>
#include <wx/display.h>

namespace Examples {
class Frame : public wxFrame {
public:
Frame() : wxFrame(nullptr, wxID_ANY, "KeyEvents", wxDefaultPosition, {300, 300}) {
logWindow->GetFrame()->SetPosition({wxDisplay(wxDisplay::GetFromWindow(this)).GetClientArea().GetLeft() + wxDisplay(wxDisplay::GetFromWindow(this)).GetClientArea().GetWidth() - wxDisplay(wxDisplay::GetFromWindow(this)).GetClientArea().GetWidth() / 4, wxDisplay(wxDisplay::GetFromWindow(this)).GetClientArea().GetTop()});
logWindow->GetFrame()->SetSize(wxDisplay(wxDisplay::GetFromWindow(this)).GetClientArea().GetWidth() / 4, wxDisplay(wxDisplay::GetFromWindow(this)).GetClientArea().GetHeight());

panel->Bind(wxEVT_KEY_DOWN, [&](wxKeyEvent& event) {
logWindow->LogTextAtLevel(0, wxString::Format("KeyDown={KeyCode=0x%04x, Modifiers=[%s]}", event.GetKeyCode(), ModiiersToString(event.GetModifiers())));
event.Skip();
});

panel->Bind(wxEVT_CHAR, [&](wxKeyEvent& event) {
logWindow->LogTextAtLevel(0, wxString::Format("Char={UnicodeKey=%s}", event.GetUnicodeKey()== WXK_NONE ? "[None]" : wxString::Format("'%c'", event.GetUnicodeKey())));
});

panel->Bind(wxEVT_KEY_UP, [&](wxKeyEvent& event) {
logWindow->LogTextAtLevel(0, wxString::Format("KeyUp={KeyCode=0x%04x, Modifiers=[%s]}%s", event.GetKeyCode(), ModiiersToString(event.GetModifiers()), event.GetModifiers() == WXK_NONE ? "\n" : ""));
});
}

private:
static std::string ModiiersToString(int modifiers) {
std::string result;
if ((modifiers & wxMOD_SHIFT) == wxMOD_SHIFT) result += "Shift, ";
if ((modifiers & wxMOD_RAW_CONTROL) == wxMOD_RAW_CONTROL) result += "Control, ";
if ((modifiers & wxMOD_ALT) == wxMOD_ALT) result += "Alt, ";
#if defined(__WXOSX__)
if ((modifiers & wxMOD_CONTROL) == wxMOD_CONTROL) result += "Command, ";
#endif
if ((modifiers & wxMOD_META) == wxMOD_META) result += "Meta, ";
if (result.size() > 1) result.resize(result.size() - 2);
return result;
}

wxPanel* panel = new wxPanel(this);
wxLogWindow* logWindow = new wxLogWindow(this, "Debug");
};

class Application : public wxApp {
bool OnInit() override {
(new Frame())->Show();
return true;
}
};
}

wxIMPLEMENT_APP(Examples::Application);
11 changes: 11 additions & 0 deletions src/Events/ProcessEvent/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.0)
project(ProcessEvent)

find_package(wxWidgets REQUIRED)
include(${wxWidgets_USE_FILE})
link_libraries(${wxWidgets_LIBRARIES})

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(${PROJECT_NAME} WIN32 MACOSX_BUNDLE ProcessEvent.cpp)
set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "Events")
55 changes: 55 additions & 0 deletions src/Events/ProcessEvent/ProcessEvent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <wx/wx.h>
#include <wx/msgout.h>

namespace Examples {
class Frame1 : public wxFrame {
public:
Frame1() : wxFrame(nullptr, wxID_ANY, "Frame1") {
SetClientSize(300, 300);
Bind(wxEVT_PAINT, [&](wxPaintEvent& event) {
wxPaintDC dc(this);
if (AppActive()) {
dc.SetBrush({wxSystemSettings::GetColour(wxSystemColour::wxSYS_COLOUR_MENUHILIGHT)});
dc.SetPen(*wxTRANSPARENT_PEN);
dc.DrawRectangle({10, 10, 280, 50});
dc.DrawText("Application is active", 10, 10);
} else {
dc.SetBrush({wxSystemSettings::GetColour(wxSystemColour::wxSYS_COLOUR_BTNFACE)});
dc.SetPen(*wxTRANSPARENT_PEN);
dc.DrawRectangle({10, 10, 280, 50});
dc.DrawText("Application is inactive", 10, 10);
}
});

}

bool AppActive() const {return appActive;}
void AppActive(bool value) {appActive = value;}

private:
wxPanel* panel = new wxPanel(this);
bool appActive = true;
};

class Application : public wxApp {
bool ProcessEvent (wxEvent &event) override {
if (event.GetEventType() == wxEVT_ACTIVATE_APP) {
frame1->AppActive(dynamic_cast<wxActivateEvent&>(event).GetActive());
wxMessageOutputDebug().Printf("wxEVT_ACTIVATE_APP [active=%s]", frame1->AppActive() ? "true" : "false");
frame1->Refresh();
}

return wxApp::ProcessEvent(event);
}

bool OnInit() override {
frame1 = new Frame1();
frame1->Show();
return true;
}

Frame1* frame1 = nullptr;
};
}

wxIMPLEMENT_APP(Examples::Application);

0 comments on commit adbd395

Please sign in to comment.