1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
/// MessageList.cpp
/**
*/
#if defined(__WXGTK__) || defined(__WXMOTIF__)
#include <wx/wx.h>
#endif
#include <fstream>
#include <wx/wx.h>
#include <wx/wxprec.h>
#include <wx/listctrl.h>
#include <wx/types.h>
#include "MessageList.h"
using namespace jcs;
BEGIN_EVENT_TABLE(MessageListDlg, wxDialog)
EVT_BUTTON(wxID_SAVE, MessageListDlg::OnSave)
END_EVENT_TABLE()
///
/**
*/
MessageListDlg::MessageListDlg(const wxString& title)
: wxDialog(NULL, -1, title, wxDefaultPosition, wxDefaultSize,
wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
{
wxBoxSizer* dlgSizer = new wxBoxSizer(wxVERTICAL);
mpListView = new wxListView(this, -1,
wxDefaultPosition, wxSize(600, 400), wxLC_REPORT);
dlgSizer->Add(mpListView, 1, wxEXPAND|wxALL, 20);
wxBoxSizer* buttonSizer = new wxBoxSizer(wxHORIZONTAL);
// buttonSizer->Add(new wxButton(this, wxID_SAVE, _T("Save")), 0, wxRIGHT, 20);
buttonSizer->Add(new wxButton(this, wxID_OK, _T("Okay")), 0);
dlgSizer->Add(buttonSizer, 0, wxRIGHT | wxBOTTOM | wxALIGN_RIGHT, 20);
SetSizer(dlgSizer);
dlgSizer->Fit(this);
}
///
/**
*/
void
MessageListDlg::SetColumns(const Message& columns)
{
for (unsigned int i = 0; i < columns.size(); ++i)
mpListView->InsertColumn(i, columns[i].c_str());
}
///
/**
*/
void
MessageListDlg::AddMessages(const MessageList& messages)
{
MessageList::const_iterator list_it = messages.begin();
MessageList::const_iterator list_end = messages.end();
while (list_it != list_end) {
int row = mpListView->GetItemCount();
int column = 0;
Message::const_iterator it = list_it->begin();
Message::const_iterator it_end = list_it->end();
if (mpListView->GetColumnCount() <= column) {
mpListView->InsertColumn(column, _T(""));
}
mpListView->InsertItem(row, it->c_str());
++column;
++it;
while (it != it_end) {
if (mpListView->GetColumnCount() <= column) {
mpListView->InsertColumn(column, _T(""));
}
mpListView->SetItem(row, column, it->c_str());
++column;
++it;
}
++list_it;
}
for (int i = 0; i < mpListView->GetColumnCount(); ++i) {
mpListView->SetColumnWidth(i, wxLIST_AUTOSIZE);
if (mpListView->GetColumnWidth(i) > 450) {
mpListView->SetColumnWidth(i, 450);
}
}
}
///
/**
*/
void
MessageListDlg::OnSave (wxCommandEvent& event)
{
wxFileDialog* dlg = new wxFileDialog(this,
_T("Choose a file"), _T(""), _T(""), _T("*.txt"),
wxFD_SAVE);
if (dlg->ShowModal() == wxID_OK) {
wxString path = dlg->GetPath();
std::ofstream output(path.mb_str(wxConvLocal));
for (int i = 0; i < mpListView->GetItemCount(); ++i) {
for (int j = 0; j < mpListView->GetColumnCount(); ++j) {
output << mGetItemText(i, j).mb_str(wxConvLocal);
output << '\t';
}
output << std::endl;
}
output.close();
}
dlg->Destroy();
}
///
/**
*/
wxString
MessageListDlg::mGetItemText(long index, int column) const
{
wxListItem item;
item.SetId(index);
item.SetColumn(column);
item.SetMask(wxLIST_MASK_TEXT);
mpListView->GetItem(item);
return item.GetText();
}
|