| 1 |
#include "BmpTextSelectorDlg.h" |
| 2 |
#include "SingleBitmapAndTextDlg.h" |
| 3 |
#include <json_node.h> |
| 4 |
#include <windowattrmanager.h> |
| 5 |
|
| 6 |
BmpTextSelectorDlg::BmpTextSelectorDlg(wxWindow* parent, const wxString& initialValue) |
| 7 |
: BmpTextSelectorDlgBase(parent) |
| 8 |
{ |
| 9 |
BmpTextVec_t vec = FromString(initialValue); |
| 10 |
for(size_t i = 0; i < vec.size(); ++i) { |
| 11 |
wxVector<wxVariant> cols; |
| 12 |
cols.push_back(vec.at(i).first); |
| 13 |
cols.push_back(vec.at(i).second); |
| 14 |
m_dvListCtrl->AppendItem(cols); |
| 15 |
} |
| 16 |
SetName("BmpTextSelectorDlg"); |
| 17 |
WindowAttrManager::Load(this); |
| 18 |
} |
| 19 |
|
| 20 |
BmpTextSelectorDlg::~BmpTextSelectorDlg() {} |
| 21 |
|
| 22 |
void BmpTextSelectorDlg::OnDelete(wxCommandEvent& event) |
| 23 |
{ |
| 24 |
wxDataViewItem item = m_dvListCtrl->GetSelection(); |
| 25 |
if(item.IsOk()) { m_dvListCtrl->DeleteItem(m_dvListCtrl->ItemToRow(item)); } |
| 26 |
} |
| 27 |
|
| 28 |
void BmpTextSelectorDlg::OnEdit(wxCommandEvent& event) |
| 29 |
{ |
| 30 |
wxDataViewItem item = m_dvListCtrl->GetSelection(); |
| 31 |
if(item.IsOk()) { |
| 32 |
int row = m_dvListCtrl->ItemToRow(item); |
| 33 |
wxVariant value; |
| 34 |
wxString bmp, label; |
| 35 |
m_dvListCtrl->GetValue(value, row, 0); |
| 36 |
bmp = value.GetString(); |
| 37 |
m_dvListCtrl->GetValue(value, row, 1); |
| 38 |
label = value.GetString(); |
| 39 |
SingleBitmapAndTextDlg dlg(this, bmp, label); |
| 40 |
if(dlg.ShowModal() == wxID_OK) { |
| 41 |
m_dvListCtrl->SetValue(dlg.GetTextCtrlBmp()->GetValue(), row, 0); |
| 42 |
m_dvListCtrl->SetValue(dlg.GetTextCtrlText()->GetValue(), row, 1); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
void BmpTextSelectorDlg::OnNew(wxCommandEvent& event) |
| 48 |
{ |
| 49 |
SingleBitmapAndTextDlg dlg(this, "", ""); |
| 50 |
if(dlg.ShowModal() == wxID_OK) { |
| 51 |
wxVector<wxVariant> cols; |
| 52 |
cols.push_back(dlg.GetTextCtrlBmp()->GetValue()); |
| 53 |
cols.push_back(dlg.GetTextCtrlText()->GetValue()); |
| 54 |
m_dvListCtrl->AppendItem(cols); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
BmpTextVec_t BmpTextSelectorDlg::FromString(const wxString& text) |
| 59 |
{ |
| 60 |
BmpTextVec_t vec; |
| 61 |
JSONRoot root(text); |
| 62 |
int size = root.toElement().arraySize(); |
| 63 |
for(int i = 0; i < size; ++i) { |
| 64 |
JSONElement item = root.toElement().arrayItem(i); |
| 65 |
wxString bitmap = item.namedObject("bmp").toString(); |
| 66 |
wxString label = item.namedObject("label").toString(); |
| 67 |
vec.push_back(std::make_pair(bitmap, label)); |
| 68 |
} |
| 69 |
return vec; |
| 70 |
} |
| 71 |
|
| 72 |
wxString BmpTextSelectorDlg::ToString(const BmpTextVec_t& vec) |
| 73 |
{ |
| 74 |
JSONRoot root(cJSON_Array); |
| 75 |
for(size_t i = 0; i < vec.size(); ++i) { |
| 76 |
JSONElement element = JSONElement::createObject(); |
| 77 |
element.addProperty("bmp", vec.at(i).first); |
| 78 |
element.addProperty("label", vec.at(i).second); |
| 79 |
root.toElement().arrayAppend(element); |
| 80 |
} |
| 81 |
wxString asString(root.toElement().format()); |
| 82 |
asString.Replace("\n", ""); |
| 83 |
return asString; |
| 84 |
} |
| 85 |
|
| 86 |
wxString BmpTextSelectorDlg::GetValue() |
| 87 |
{ |
| 88 |
BmpTextVec_t v; |
| 89 |
int count = m_dvListCtrl->GetItemCount(); |
| 90 |
for(int i = 0; i < count; ++i) { |
| 91 |
wxVariant value; |
| 92 |
wxString bmp, label; |
| 93 |
m_dvListCtrl->GetValue(value, i, 0); |
| 94 |
bmp = value.GetString(); |
| 95 |
m_dvListCtrl->GetValue(value, i, 1); |
| 96 |
label = value.GetString(); |
| 97 |
v.push_back(std::make_pair(bmp, label)); |
| 98 |
} |
| 99 |
return ToString(v); |
| 100 |
} |
| 101 |
void BmpTextSelectorDlg::OnDeleteUI(wxUpdateUIEvent& event) { event.Enable(m_dvListCtrl->GetSelection().IsOk()); } |
| 102 |
void BmpTextSelectorDlg::OnEditUI(wxUpdateUIEvent& event) { event.Enable(m_dvListCtrl->GetSelection().IsOk()); } |
| 103 |
void BmpTextSelectorDlg::OnItemActivated(wxDataViewEvent& event) |
| 104 |
{ |
| 105 |
wxCommandEvent dummy; |
| 106 |
if(m_dvListCtrl->GetSelection().IsOk()) { OnEdit(dummy); } |
| 107 |
} |