Add all the files

This commit is contained in:
Exzap 2022-08-22 22:21:23 +02:00
parent e3db07a16a
commit d60742f52b
1445 changed files with 430238 additions and 0 deletions

View file

@ -0,0 +1,44 @@
#pragma once
#include <wx/control.h>
class wxControlObject : public wxObject
{
public:
wxControlObject(wxControl* control)
: m_control(control) {}
template<typename T>
T* GetControl() const
{
static_assert(std::is_base_of<wxControl, T>::value, "T must inherit from wxControl");
return dynamic_cast<T*>(m_control);
}
wxControl* GetControl() const { return m_control; }
private:
wxControl* m_control;
};
class wxControlObjects : public wxObject
{
public:
wxControlObjects(std::vector<wxControl*> controls)
: m_controls(std::move(controls)) {}
template<typename T = wxControl>
T* GetControl(int index) const
{
static_assert(std::is_base_of<wxControl, T>::value, "T must inherit from wxControl");
if (index < 0 || index >= m_controls.size())
return nullptr;
return dynamic_cast<T*>(m_controls[index]);
}
const std::vector<wxControl*>& GetControls() const { return m_controls; }
private:
std::vector<wxControl*> m_controls;
};

View file

@ -0,0 +1,49 @@
#pragma once
#include <wx/clntdata.h>
template <typename T>
class wxCustomData : public wxClientData
{
public:
wxCustomData()
: m_data({}) {}
wxCustomData(T data)
: m_data(std::move(data)) { }
const T& GetData() const { return m_data; }
/// <summary>
/// obtains ownership of the data
/// </summary>
/// <returns>returns the stored data</returns>
T get() { return std::move(m_data); }
/// <summary>
/// access to the data without obtaining ownership
/// </summary>
/// <returns>returns a reference to the stored data</returns>
T& ref() { return m_data; }
bool operator==(const T& o) const { return m_data == o.m_data; }
bool operator!=(const T& o) const { return !(*this == o); }
private:
T m_data;
};
//template <typename T>
//class wxCustomObject : public wxObject
//{
//public:
// wxCustomObject(T data)
// : m_data(std::move(data)) { }
//
// const T& GetData() const { return m_data; }
//
// bool operator==(const T& o) const { return m_data == o.m_data; }
// bool operator!=(const T& o) const { return !(*this == o); }
//private:
// T m_data;
//};

View file

@ -0,0 +1,7 @@
#include "gui/helpers/wxCustomEvents.h"
wxDEFINE_EVENT(wxEVT_SET_STATUS_BAR_TEXT, wxSetStatusBarTextEvent);
wxDEFINE_EVENT(wxEVT_SET_GAUGE_VALUE, wxSetGaugeValue);
wxDEFINE_EVENT(wxEVT_REMOVE_ITEM, wxCommandEvent);
wxDEFINE_EVENT(wxEVT_SET_TEXT, wxCommandEvent);
wxDEFINE_EVENT(wxEVT_ENABLE, wxCommandEvent);

View file

@ -0,0 +1,74 @@
#pragma once
#include <wx/event.h>
#include <wx/gauge.h>
class wxStatusBar;
class wxControl;
wxDECLARE_EVENT(wxEVT_REMOVE_ITEM, wxCommandEvent);
wxDECLARE_EVENT(wxEVT_SET_TEXT, wxCommandEvent);
wxDECLARE_EVENT(wxEVT_ENABLE, wxCommandEvent);
class wxSetStatusBarTextEvent;
wxDECLARE_EVENT(wxEVT_SET_STATUS_BAR_TEXT, wxSetStatusBarTextEvent);
class wxSetStatusBarTextEvent : public wxCommandEvent
{
public:
wxSetStatusBarTextEvent(const wxString& text, int number = 0, wxEventType type = wxEVT_SET_STATUS_BAR_TEXT, int id = 0)
: wxCommandEvent(type, id), m_text(text), m_number(number) {}
wxSetStatusBarTextEvent(const wxSetStatusBarTextEvent& event)
: wxCommandEvent(event), m_text(event.GetText()), m_number(event.GetNumber()) {}
[[nodiscard]] const wxString& GetText() const { return m_text; }
[[nodiscard]] int GetNumber() const { return m_number; }
wxEvent* Clone() const override
{
return new wxSetStatusBarTextEvent(*this);
}
private:
const wxString m_text;
const int m_number;
};
class wxSetGaugeValue;
wxDECLARE_EVENT(wxEVT_SET_GAUGE_VALUE, wxSetGaugeValue);
class wxSetGaugeValue : public wxCommandEvent
{
public:
wxSetGaugeValue(int value, wxGauge* gauge, wxControl* text_ctrl = nullptr, const wxString& text = wxEmptyString, wxEventType type = wxEVT_SET_GAUGE_VALUE, int id = 0)
: wxCommandEvent(type, id), m_gauge_value(value), m_gauge_range(0), m_text_ctrl(text_ctrl), m_text(text)
{
SetEventObject(gauge);
}
wxSetGaugeValue(int value, int range, wxGauge* gauge, wxControl* text_ctrl = nullptr, const wxString& text = wxEmptyString, wxEventType type = wxEVT_SET_GAUGE_VALUE, int id = 0)
: wxCommandEvent(type, id), m_gauge_value(value), m_gauge_range(range), m_text_ctrl(text_ctrl), m_text(text)
{
SetEventObject(gauge);
}
wxSetGaugeValue(const wxSetGaugeValue& event)
: wxCommandEvent(event), m_gauge_value(event.GetValue()), m_gauge_range(event.GetRange()), m_text_ctrl(event.GetTextCtrl()), m_text(event.GetText())
{}
[[nodiscard]] int GetValue() const { return m_gauge_value; }
[[nodiscard]] int GetRange() const { return m_gauge_range; }
[[nodiscard]] wxGauge* GetGauge() const { return (wxGauge*)GetEventObject(); }
[[nodiscard]] const wxString& GetText() const { return m_text; }
[[nodiscard]] wxControl* GetTextCtrl() const { return m_text_ctrl; }
wxEvent* Clone() const override
{
return new wxSetGaugeValue(*this);
}
private:
const int m_gauge_value, m_gauge_range;
wxControl* m_text_ctrl;
const wxString m_text;
};

View file

@ -0,0 +1,66 @@
#include "gui/helpers/wxHelpers.h"
#include <wx/wupdlock.h>
#include <wx/stattext.h>
#include <wx/slider.h>
#include <wx/dirdlg.h>
#include "gui/helpers/wxControlObject.h"
void wxAutosizeColumn(wxListCtrlBase* ctrl, int col)
{
ctrl->SetColumnWidth(col, wxLIST_AUTOSIZE_USEHEADER);
int wh = ctrl->GetColumnWidth(col);
ctrl->SetColumnWidth(col, wxLIST_AUTOSIZE);
int wc = ctrl->GetColumnWidth(col);
if (wh > wc)
ctrl->SetColumnWidth(col, wxLIST_AUTOSIZE_USEHEADER);
}
void wxAutosizeColumns(wxListCtrlBase* ctrl, int col_start, int col_end)
{
wxWindowUpdateLocker lock(ctrl);
for (int i = col_start; i <= col_end; ++i)
wxAutosizeColumn(ctrl, i);
}
void update_slider_text(wxCommandEvent& event, const wxFormatString& format /*= "%d%%"*/)
{
const auto slider = dynamic_cast<wxSlider*>(event.GetEventObject());
wxASSERT(slider);
auto slider_text = dynamic_cast<wxControlObject*>(event.GetEventUserData())->GetControl<wxStaticText>();
wxASSERT(slider_text);
slider_text->SetLabel(wxString::Format(format, slider->GetValue()));
}
uint32 fix_raw_keycode(uint32 keycode, uint32 raw_flags)
{
#if BOOST_OS_WINDOWS
const auto flags = (HIWORD(raw_flags) & 0xFFF);
if(keycode == VK_SHIFT)
{
if(flags == 0x2A)
return 160;
else if (flags == 0x36)
return 161;
}
else if (keycode == VK_CONTROL)
{
if (flags == 0x1d)
return 162;
else if (flags == 0x11d)
return 163;
}
else if (keycode == VK_MENU)
{
if ((flags & 0xFF) == 0x38)
return 164;
else if ((flags & 0xFF) == 0x38)
return 165;
}
#endif
return keycode;
}

126
src/gui/helpers/wxHelpers.h Normal file
View file

@ -0,0 +1,126 @@
#pragma once
#include <wx/control.h>
#include <wx/listbase.h>
#include <wx/string.h>
template <>
struct fmt::formatter<wxString> : formatter<string_view>
{
template <typename FormatContext>
auto format(const wxString& str, FormatContext& ctx)
{
return formatter<string_view>::format(str.c_str().AsChar(), ctx);
}
};
class wxTempEnable
{
public:
wxTempEnable(wxControl* control, bool state = true)
: m_control(control), m_state(state)
{
m_control->Enable(m_state);
}
wxTempEnable(const wxTempEnable&) = delete;
wxTempEnable(wxTempEnable&&) noexcept = default;
~wxTempEnable()
{
if(m_control)
m_control->Enable(!m_state);
}
private:
wxControl* m_control;
const bool m_state;
};
class wxTempDisable : wxTempEnable
{
public:
wxTempDisable(wxControl* control)
: wxTempEnable(control, false) {}
};
template<typename ...TArgs>
wxString wxStringFormat2(const wxString& format, TArgs&&...args)
{
// ignores locale?
return fmt::format(format.ToStdString(), std::forward<TArgs>(args)...);
}
template<typename ...TArgs>
wxString wxStringFormat2W(const wxString& format, TArgs&&...args)
{
return fmt::format(format.ToStdWstring(), std::forward<TArgs>(args)...);
}
// executes a function when destroying the obj
template<typename TFunction, typename ...TArgs>
class wxDTorFunc
{
public:
wxDTorFunc(TFunction&& func, TArgs&&... args)
{
auto bound = std::bind(std::forward<TFunction>(func), std::forward<TArgs>(args)...);
// m_function = [bound] { bound(); };
m_function = [bound{ std::move(bound) }]{ bound(); };
}
~wxDTorFunc()
{
m_function();
}
private:
std::function<void()> m_function;
};
void wxAutosizeColumn(wxListCtrlBase* ctrl, int col);
void wxAutosizeColumns(wxListCtrlBase* ctrl, int col_start, int col_end);
// creates wxString from utf8 string
inline wxString to_wxString(std::string_view str)
{
return wxString::FromUTF8(str.data(), str.size());
}
// creates utf8 std::string from wxString
inline std::string from_wxString(const wxString& str)
{
const auto tmp = str.ToUTF8();
return std::string{ tmp.data(), tmp.length() };
}
template <typename T>
T get_next_sibling(const T element)
{
static_assert(std::is_pointer_v<T> && std::is_base_of_v<wxControl, std::remove_pointer_t<T>>, "element must be a pointer and inherit from wxControl");
for (auto sibling = element->GetNextSibling(); sibling; sibling = sibling->GetNextSibling())
{
if (auto result = dynamic_cast<T>(sibling))
return result;
}
return nullptr;
}
template <typename T>
T get_prev_sibling(const T element)
{
static_assert(std::is_pointer_v<T> && std::is_base_of_v<wxControl, std::remove_pointer_t<T>>, "element must be a pointer and inherit from wxControl");
for (auto sibling = element->GetPrevSibling(); sibling; sibling = sibling->GetPrevSibling())
{
auto result = dynamic_cast<T>(sibling);
if (result)
return result;
}
return nullptr;
}
void update_slider_text(wxCommandEvent& event, const wxFormatString& format = "%d%%");
uint32 fix_raw_keycode(uint32 keycode, uint32 raw_flags);

View file

@ -0,0 +1,32 @@
#pragma once
#include <wx/event.h>
class wxLogEvent;
wxDECLARE_EVENT(EVT_LOG, wxLogEvent);
class wxLogEvent : public wxCommandEvent
{
public:
wxLogEvent(const wxString& filter, const wxString& message)
: wxCommandEvent(EVT_LOG), m_filter(filter), m_message(message) { }
wxLogEvent(const wxLogEvent& event)
: wxCommandEvent(event), m_filter(event.m_filter), m_message(event.m_message) { }
wxEvent* Clone() const { return new wxLogEvent(*this); }
[[nodiscard]] const wxString& GetFilter() const
{
return m_filter;
}
[[nodiscard]] const wxString& GetMessage() const
{
return m_message;
}
private:
wxString m_filter;
wxString m_message;
};