Dynamic keyboard entries moved to wxZRColaKeyHandler

This commit is contained in:
Simon Rozman 2016-03-03 16:24:30 +01:00
parent 70990846e1
commit 87b80bb8c4
7 changed files with 159 additions and 96 deletions

View File

@ -84,6 +84,7 @@
<ClCompile Include="zrcolacomppnl.cpp" /> <ClCompile Include="zrcolacomppnl.cpp" />
<ClCompile Include="zrcolafrm.cpp" /> <ClCompile Include="zrcolafrm.cpp" />
<ClCompile Include="zrcolagui.cpp" /> <ClCompile Include="zrcolagui.cpp" />
<ClCompile Include="zrcolakeyhndlr.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="stdafx.h" /> <ClInclude Include="stdafx.h" />
@ -91,6 +92,7 @@
<ClInclude Include="zrcolacomppnl.h" /> <ClInclude Include="zrcolacomppnl.h" />
<ClInclude Include="zrcolafrm.h" /> <ClInclude Include="zrcolafrm.h" />
<ClInclude Include="zrcolagui.h" /> <ClInclude Include="zrcolagui.h" />
<ClInclude Include="zrcolakeyhndlr.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\lib\libZRColaUI\build\libZRColaUI.vcxproj"> <ProjectReference Include="..\lib\libZRColaUI\build\libZRColaUI.vcxproj">

View File

@ -34,6 +34,9 @@
<ClCompile Include="zrcolafrm.cpp"> <ClCompile Include="zrcolafrm.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="zrcolakeyhndlr.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="stdafx.h"> <ClInclude Include="stdafx.h">
@ -51,6 +54,9 @@
<ClInclude Include="zrcolafrm.h"> <ClInclude Include="zrcolafrm.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="zrcolakeyhndlr.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="res\send.ico"> <None Include="res\send.ico">

View File

@ -23,6 +23,7 @@
#include "zrcolaapp.h" #include "zrcolaapp.h"
#include "zrcolacomppnl.h" #include "zrcolacomppnl.h"
#include "zrcolafrm.h" #include "zrcolafrm.h"
#include "zrcolakeyhndlr.h"
#include <wx/msgdlg.h> #include <wx/msgdlg.h>
#include <wxex/common.h> #include <wxex/common.h>

View File

@ -20,32 +20,6 @@
#include "stdafx.h" #include "stdafx.h"
//////////////////////////////////////////////////////////////////////////
// wxZRColaComposerPanelEvtHandler
//////////////////////////////////////////////////////////////////////////
wxZRColaComposerPanelEvtHandler::wxZRColaComposerPanelEvtHandler(wxZRColaComposerPanel *target) :
m_target(target),
wxEvtHandler()
{
}
bool wxZRColaComposerPanelEvtHandler::ProcessEvent(wxEvent& event)
{
if (m_target && event.IsCommandEvent()) {
int id = event.GetId();
if (wxZRColaComposerPanel::wxID_ACCEL <= id && id < wxZRColaComposerPanel::wxID_ACCEL + m_target->m_ks_db.idxKey.size()) {
const ZRCola::keyseq_db::keyseq &ks = (const ZRCola::keyseq_db::keyseq&)m_target->m_ks_db.data[m_target->m_ks_db.idxKey[id - wxZRColaComposerPanel::wxID_ACCEL]];
m_target->m_decomposed->WriteText(ks.chr);
return true;
}
}
return wxEvtHandler::ProcessEvent(event);
}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// wxZRColaComposerPanel // wxZRColaComposerPanel
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@ -54,7 +28,6 @@ wxZRColaComposerPanel::wxZRColaComposerPanel(wxWindow* parent) :
m_progress(false), m_progress(false),
m_selDecomposed(0, 0), m_selDecomposed(0, 0),
m_selComposed(0, 0), m_selComposed(0, 0),
eh(this),
wxZRColaComposerPanelBase(parent) wxZRColaComposerPanelBase(parent)
{ {
wxString sPath(wxPathOnly(wxTheApp->argv[0])); wxString sPath(wxPathOnly(wxTheApp->argv[0]));
@ -66,63 +39,29 @@ wxZRColaComposerPanel::wxZRColaComposerPanel(wxWindow* parent) :
ZRCola::recordsize_t size; ZRCola::recordsize_t size;
dat.read((char*)&size, sizeof(ZRCola::recordsize_t)); dat.read((char*)&size, sizeof(ZRCola::recordsize_t));
if (dat.good()) { if (dat.good()) {
bool has_translation_data = false; ZRCola::translation_rec rec(m_t_db);
if (rec.find(dat, size)) {
for (;;) { dat >> rec;
ZRCola::recordid_t id; if (!dat.good()) {
if (!stdex::idrec::read_id(dat, id, size)) break;
if (id == ZRCola::translation_rec::id) {
dat >> ZRCola::translation_rec(m_t_db);
if (dat.good()) {
has_translation_data = true;
} else {
wxFAIL_MSG(wxT("Error reading translation data from ZRCola.zrcdb.")); wxFAIL_MSG(wxT("Error reading translation data from ZRCola.zrcdb."));
m_t_db.idxComp .clear(); m_t_db.idxComp .clear();
m_t_db.idxDecomp.clear(); m_t_db.idxDecomp.clear();
m_t_db.data .clear(); m_t_db.data .clear();
} }
} else if (id == ZRCola::keyseq_rec::id) { } else
dat >> ZRCola::keyseq_rec(m_ks_db);
if (!dat.good()) {
wxFAIL_MSG(wxT("Error reading key sequence data from ZRCola.zrcdb."));
m_ks_db.idxChr.clear();
m_ks_db.idxKey.clear();
m_ks_db.data .clear();
}
}
}
if (!has_translation_data)
wxFAIL_MSG(wxT("ZRCola.zrcdb has no translation data.")); wxFAIL_MSG(wxT("ZRCola.zrcdb has no translation data."));
} }
} else } else
wxFAIL_MSG(wxT("ZRCola.zrcdb is not a valid ZRCola database.")); wxFAIL_MSG(wxT("ZRCola.zrcdb is not a valid ZRCola database."));
} }
std::vector<unsigned __int32>::size_type n = m_ks_db.idxKey.size(); m_decomposed->PushEventHandler(&m_keyhandler);
std::vector<wxAcceleratorEntry> entries;
entries.reserve(n);
for (std::vector<unsigned __int32>::size_type i = 0; i < n; i++) {
const ZRCola::keyseq_db::keyseq &ks = (const ZRCola::keyseq_db::keyseq&)m_ks_db.data[m_ks_db.idxKey[i]];
if (ks.seq_len == 1) {
// The key sequence is trivial.
entries.push_back(wxAcceleratorEntry(
((ks.seq[0].modifiers & ZRCola::keyseq_db::keyseq::SHIFT) ? wxACCEL_SHIFT : 0) |
((ks.seq[0].modifiers & ZRCola::keyseq_db::keyseq::CTRL ) ? wxACCEL_CTRL : 0) |
((ks.seq[0].modifiers & ZRCola::keyseq_db::keyseq::ALT ) ? wxACCEL_ALT : 0), ks.seq[0].key, wxID_ACCEL + i));
}
}
wxAcceleratorTable accel(entries.size(), entries.data());
SetAcceleratorTable(accel);
PushEventHandler(&eh);
} }
wxZRColaComposerPanel::~wxZRColaComposerPanel() wxZRColaComposerPanel::~wxZRColaComposerPanel()
{ {
PopEventHandler(); m_decomposed->PopEventHandler();
} }

View File

@ -20,48 +20,27 @@
/// ///
/// Forward declarations /// Forward declarations
/// ///
class wxZRColaComposerPanelEvtHandler;
class wxZRColaComposerPanel; class wxZRColaComposerPanel;
#pragma once #pragma once
#include "zrcolagui.h" #include "zrcolagui.h"
#include "zrcolakeyhndlr.h"
#include <zrcola/translate.h> #include <zrcola/translate.h>
#include <zrcolaui/keyboard.h>
#include <utility> #include <utility>
///
/// ZRCola composer panel event handler
///
class wxZRColaComposerPanelEvtHandler : public wxEvtHandler
{
public:
wxZRColaComposerPanelEvtHandler(wxZRColaComposerPanel *target);
virtual bool ProcessEvent(wxEvent& event);
public:
wxZRColaComposerPanel *m_target; ///< Composer panel window
};
/// ///
/// ZRCola composer panel /// ZRCola composer panel
/// ///
class wxZRColaComposerPanel : public wxZRColaComposerPanelBase class wxZRColaComposerPanel : public wxZRColaComposerPanelBase
{ {
enum {
wxID_ACCEL = wxID_HIGHEST
};
public: public:
wxZRColaComposerPanel(wxWindow* parent); wxZRColaComposerPanel(wxWindow* parent);
virtual ~wxZRColaComposerPanel(); virtual ~wxZRColaComposerPanel();
friend class wxZRColaFrame; // Allow main frame direct access to our members. friend class wxZRColaFrame; // Allow main frame direct access to our members.
friend class wxZRColaComposerPanelEvtHandler; // Allow own event handler direct access to our members.
protected: protected:
virtual void OnDecomposedPaint(wxPaintEvent& event); virtual void OnDecomposedPaint(wxPaintEvent& event);
@ -71,11 +50,10 @@ protected:
protected: protected:
ZRCola::translation_db m_t_db; ///< Translation database ZRCola::translation_db m_t_db; ///< Translation database
ZRCola::keyseq_db m_ks_db; ///< Key sequence database
bool m_progress; ///< Boolean flag to avoid recursive updates of composed and decomposed text controls bool m_progress; ///< Boolean flag to avoid recursive updates of composed and decomposed text controls
ZRCola::mapping_vector m_mapping; ///< Character index mapping vector between composed and decomposed text ZRCola::mapping_vector m_mapping; ///< Character index mapping vector between composed and decomposed text
std::pair<long, long> std::pair<long, long>
m_selDecomposed, ///< Character index of selected text in decomposed text control m_selDecomposed, ///< Character index of selected text in decomposed text control
m_selComposed; ///< Character index of selected text in composed text control m_selComposed; ///< Character index of selected text in composed text control
wxZRColaComposerPanelEvtHandler eh; ///< Event handler wxZRColaKeyHandler m_keyhandler; ///< Key handler for decomposed window
}; };

93
ZRCola/zrcolakeyhndlr.cpp Normal file
View File

@ -0,0 +1,93 @@
/*
Copyright 2015-2016 Amebis
This file is part of ZRCola.
ZRCola is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ZRCola is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ZRCola. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
//////////////////////////////////////////////////////////////////////////
// wxZRColaKeyHandler
//////////////////////////////////////////////////////////////////////////
wxZRColaKeyHandler::wxZRColaKeyHandler() : wxEvtHandler()
{
wxString sPath(wxPathOnly(wxTheApp->argv[0]));
sPath << wxT("\\..\\data\\ZRCola.zrcdb"); // TODO: Make database file configurable
std::fstream dat((LPCTSTR)sPath, std::ios_base::in | std::ios_base::binary);
if (dat.good()) {
if (stdex::idrec::find<ZRCola::recordid_t, ZRCola::recordsize_t, ZRCOLA_RECORD_ALIGN>(dat, ZRCOLA_DB_ID, sizeof(ZRCola::recordid_t))) {
ZRCola::recordsize_t size;
dat.read((char*)&size, sizeof(ZRCola::recordsize_t));
if (dat.good()) {
ZRCola::keyseq_rec rec(m_ks_db);
if (rec.find(dat, size)) {
dat >> rec;
if (!dat.good()) {
wxFAIL_MSG(wxT("Error reading translation data from ZRCola.zrcdb."));
m_ks_db.idxChr.clear();
m_ks_db.idxKey.clear();
m_ks_db.data .clear();
}
} else
wxFAIL_MSG(wxT("ZRCola.zrcdb has no translation data."));
}
} else
wxFAIL_MSG(wxT("ZRCola.zrcdb is not a valid ZRCola database."));
}
}
bool wxZRColaKeyHandler::ProcessEvent(wxEvent& event)
{
if (event.GetEventType() == wxEVT_CHAR) {
// The character event occured.
ZRCola::keyseq_db::indexKey::size_type start, end;
bool found;
{
wxKeyEvent &e = (wxKeyEvent&)event;
ZRCola::keyseq_db::keyseq *ks = (ZRCola::keyseq_db::keyseq*)new char[sizeof(ZRCola::keyseq_db::keyseq) + sizeof(ZRCola::keyseq_db::keyseq::key_t)*1];
ks->chr = 0;
ks->seq_len = 1;
ks->seq[0].key = wxToupper(e.m_uniChar);
ks->seq[0].modifiers =
(e.ShiftDown() ? ZRCola::keyseq_db::keyseq::SHIFT : 0) |
(e.ControlDown() ? ZRCola::keyseq_db::keyseq::CTRL : 0) |
(e.AltDown() ? ZRCola::keyseq_db::keyseq::ALT : 0);
found = m_ks_db.idxKey.find((const unsigned __int16&)*ks, start, end);
delete ks;
}
if (found) {
const ZRCola::keyseq_db::keyseq &ks = (const ZRCola::keyseq_db::keyseq&)m_ks_db.data[m_ks_db.idxKey[start]];
wxObject *obj = event.GetEventObject();
if (obj && obj->IsKindOf(wxCLASSINFO(wxTextCtrl))) {
// Push text to source control.
((wxTextCtrl*)obj)->WriteText(ks.chr);
// Event is fully processed now.
event.StopPropagation();
return true;
}
}
}
return wxEvtHandler::ProcessEvent(event);
}

44
ZRCola/zrcolakeyhndlr.h Normal file
View File

@ -0,0 +1,44 @@
/*
Copyright 2015-2016 Amebis
This file is part of ZRCola.
ZRCola is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ZRCola is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ZRCola. If not, see <http://www.gnu.org/licenses/>.
*/
///
/// Forward declarations
///
class wxZRColaKeyHandler;
#pragma once
#include <zrcolaui/keyboard.h>
#include <wx/event.h>
///
/// ZRCola keyboard event handler
///
class wxZRColaKeyHandler : public wxEvtHandler
{
public:
wxZRColaKeyHandler();
virtual bool ProcessEvent(wxEvent& event);
protected:
ZRCola::keyseq_db m_ks_db; ///< Key sequence database
};