From f463bf4bd969a9872daa99228b92cd152b2d5954 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Tue, 17 Jan 2012 02:47:25 +0000 Subject: [PATCH] Derive the custom hotkey renderers from wxDataViewCustomRenderer Deriving from the more specialized renderes reduces the amount of code required, but it depends on implementation details of the specific implementations, resulting in the custom renderers not working with wxGTK. In addition, this makes it possible to bypass the default handling for 'Enter' which made it impossible to set it as a hotkey. Originally committed to SVN as r6300. --- aegisub/src/preferences.cpp | 95 +++++++++++++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 8 deletions(-) diff --git a/aegisub/src/preferences.cpp b/aegisub/src/preferences.cpp index 2b7082fea..e2a85b84f 100644 --- a/aegisub/src/preferences.cpp +++ b/aegisub/src/preferences.cpp @@ -274,11 +274,13 @@ Interface_Colours::Interface_Colours(wxTreebook *book, Preferences *parent): Opt } /// wxDataViewIconTextRenderer with command name autocompletion -class CommandRenderer : public wxDataViewIconTextRenderer { +class CommandRenderer : public wxDataViewCustomRenderer { wxArrayString autocomplete; + wxDataViewIconText value; + public: CommandRenderer() - : wxDataViewIconTextRenderer("wxDataViewIconText", wxDATAVIEW_CELL_EDITABLE) + : wxDataViewCustomRenderer("wxDataViewIconText", wxDATAVIEW_CELL_EDITABLE) { std::vector cmd_names = cmd::get_registered_commands(); autocomplete.reserve(cmd_names.size()); @@ -286,19 +288,77 @@ public: } wxWindow *CreateEditorCtrl(wxWindow *parent, wxRect label_rect, wxVariant const& value) { - wxTextCtrl *ctrl = static_cast(wxDataViewIconTextRenderer::CreateEditorCtrl(parent, label_rect, value)); + wxDataViewIconText iconText; + iconText << value; + + wxString text = iconText.GetText(); + + // adjust the label rect to take the width of the icon into account + if (iconText.GetIcon().IsOk()) { + int w = iconText.GetIcon().GetWidth() + 4; + label_rect.x += w; + label_rect.width -= w; + } + + wxTextCtrl* ctrl = new wxTextCtrl(parent, -1, text, label_rect.GetPosition(), label_rect.GetSize(), wxTE_PROCESS_ENTER); + ctrl->SetInsertionPointEnd(); + ctrl->SelectAll(); ctrl->AutoComplete(autocomplete); return ctrl; } + + bool SetValue(wxVariant const& var) { + value << var; + return true; + } + + bool Render(wxRect rect, wxDC *dc, int state) { + int xoffset = 0; + + wxIcon const& icon = value.GetIcon(); + if (icon.IsOk()) { + dc->DrawIcon(icon, rect.x, rect.y + (rect.height - icon.GetHeight()) / 2); + xoffset = icon.GetWidth() + 4; + } + + RenderText(value.GetText(), xoffset, rect, dc, state); + + return true; + } + + wxSize GetSize() const { + if (!value.GetText().empty()) { + wxSize size = GetTextExtent(value.GetText()); + + if (value.GetIcon().IsOk()) + size.x += value.GetIcon().GetWidth() + 4; + return size; + } + return wxSize(80,20); + } + + bool GetValueFromEditorCtrl(wxWindow* editor, wxVariant &var) { + wxTextCtrl *text = static_cast(editor); + wxDataViewIconText iconText(text->GetValue(), value.GetIcon()); + var << iconText; + return true; + } + + bool GetValue(wxVariant &) const { return false; } + bool HasEditorCtrl() const { return true; } }; -class HotkeyRenderer : public wxDataViewTextRenderer { +class HotkeyRenderer : public wxDataViewCustomRenderer { + wxString value; wxTextCtrl *ctrl; -public: - HotkeyRenderer() : wxDataViewTextRenderer("string", wxDATAVIEW_CELL_EDITABLE) { } - wxWindow *CreateEditorCtrl(wxWindow *parent, wxRect label_rect, wxVariant const& value) { - ctrl = static_cast(wxDataViewTextRenderer::CreateEditorCtrl(parent, label_rect, value)); +public: + HotkeyRenderer() : wxDataViewCustomRenderer("string", wxDATAVIEW_CELL_EDITABLE) { } + + wxWindow *CreateEditorCtrl(wxWindow *parent, wxRect label_rect, wxVariant const& var) { + ctrl = new wxTextCtrl(parent, -1, var.GetString(), label_rect.GetPosition(), label_rect.GetSize(), wxTE_PROCESS_ENTER); + ctrl->SetInsertionPointEnd(); + ctrl->SelectAll(); ctrl->Bind(wxEVT_KEY_DOWN, &HotkeyRenderer::OnKeyDown, this); return ctrl; } @@ -306,6 +366,25 @@ public: void OnKeyDown(wxKeyEvent &evt) { ctrl->ChangeValue(lagi_wxString(hotkey::keypress_to_str(evt.GetKeyCode(), evt.GetUnicodeKey(), evt.GetModifiers()))); } + + bool SetValue(wxVariant const& var) { + value = var.GetString(); + return true; + } + + bool Render(wxRect rect, wxDC *dc, int state) { + RenderText(value, 0, rect, dc, state); + return true; + } + + bool GetValueFromEditorCtrl(wxWindow*, wxVariant &var) { + var = ctrl->GetValue(); + return true; + } + + bool GetValue(wxVariant &) const { return false; } + wxSize GetSize() const { return !value ? wxSize(80, 20) : GetTextExtent(value); } + bool HasEditorCtrl() const { return true; } }; /// Interface Hotkeys preferences subpage