📄 cadddlg.cpp
字号:
//PK 2006/10/11 - 2007/03/10
#include "stdafx.h"
#include "resource.h"
#include "cadddlg.h"
#include "utility.h"
#include "wtl_tools.h"
#include "configurations.h"
#include "Repository.h"
#include "Book.h"
#include "Item.h"
#include "CreateBookDlg.h"
#include "MainDlg.h"
LRESULT CAddDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{ //PK 2006/10/11 - 2007/03/08
CenterWindow(GetParent());
HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME),
IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
SetIcon(hIconSmall, FALSE);
_edit_fn = GetDlgItem(IDC_EDT_DLGADD_NAME);
_day = GetDlgItem(IDC_DTP_DLGADD_DAY);
_edit_times = GetDlgItem(IDC_EDT_DLGADD_TIMES);
_cmb_books = GetDlgItem(IDC_CMB_DLGADD_CLASS);
_edit_content = GetDlgItem(IDC_EDT_DLGADD_CONTENT);
_text_total = GetDlgItem(IDC_TXT_DLGADD_TOTAL);
int amount = _repository.size();
for (int i = 0; i < amount; ++i) {
CBook * book = _repository.get_book(i);
_add_book_to_list(book);
}
_cmb_books.SetCurSel(0);
CButton btn = GetDlgItem(IDC_BTN_DLGADD_OK);
switch(_status)
{
case ADD:
{
_day.SetSystemTime(GDT_NONE, NULL);
_day.EnableWindow(FALSE);
_edit_times.SetWindowText("0");
_edit_times.EnableWindow(FALSE);
SetWindowText("Recite - Add Items");
btn.SetWindowText("&Add");
}
break;
case MODIFY:
{
assert(_item != NULL);
_edit_fn.SetWindowText(_item->file_name().string().c_str());
_edit_fn.EnableWindow(FALSE);
date day = _item->access_date();
if (day.is_not_a_date()) _day.SetSystemTime(GDT_NONE, NULL);
else {
SYSTEMTIME st;
memset(&st, 0, sizeof(SYSTEMTIME));
st.wYear = day.year();
st.wMonth = day.month();
st.wDay = day.day();
_day.SetSystemTime(GDT_VALID, &st);
}
_edit_times.SetWindowText(ulong2c(_item->access_times()).c_str());
int amount = _cmb_books.GetCount();
CBook * book = _item->book();
for (int i = 0; i < amount; ++i) {
if ((CBook*)_cmb_books.GetItemData(i) == book) {
_cmb_books.SetCurSel(i);
_cmb_books.EnableWindow(FALSE);
}
}
_article = _item->get_article();
_edit_content.SetWindowText(_article.c_str());
SetWindowText("Recite - Modify Item");
btn.SetWindowText("OK");
CButton btn_book = GetDlgItem(IDC_BTN_DLGADD_BOOK);
btn_book.EnableWindow(FALSE);
}
break;
default:
assert(false); //PK the logic can't reach here
}
OnBookChange(0, 0, 0);
return TRUE;
}
LRESULT CAddDlg::OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{ //PK 2006/10/11 - 2007/03/10
fs::path file_path;
string content, times;
//PK check if the contents of controls is valid
bool is_valid = true;
stringstream promot;
//PK file name
if (ADD == _status) {
assert(NULL == _item);
_item = new CItem();
string fn;
get_window_text(_edit_fn, fn);
if (!iends_with(fn, ".txt")) fn = fn + ".txt";
try {
file_path = fs::path(fn);
} catch (...) {
is_valid = false;
promot << "File name must be a valid file name!" << std::endl;
}
}
//PK studied times
get_window_text(_edit_times, times);
if (times.empty()) {
is_valid = false;
promot << "\"Times\" can't be empty!" << std::endl;
}
string::iterator itr = times.begin(), itr_end = times.end();
for (; itr != itr_end; ++itr) {
if (0 == isdigit(*itr)) {
is_valid = false;
promot << "\"Times\" must be number!" << std::endl;
break;
}
}
//PK article
get_window_text(_edit_content, content);
if (content.empty()) {
is_valid = false;
promot << "\"Content\" can't be empty!" << std::endl;
}
//PK promot the error
if (!is_valid) {
string caption;
if (ADD == _status) caption = "Add - ";
if (MODIFY == _status) caption = "Modify - ";
caption = caption + "Error!";
MessageBox(promot.str().c_str(), caption.c_str(), MB_OK | MB_ICONWARNING);
delete _item;
_item = NULL;
return 0;
}
//PK set properties of item
CBook * book = (CBook*)_cmb_books.GetItemDataPtr(_cmb_books.GetCurSel());
if (ADD == _status) {
_item->book(book);
_item->file_name(file_path);
if (fs::exists(_item->file_name())) {
promot << ("File \"") << file_path.string() << "\" has existed!" << std::endl;
MessageBox(promot.str().c_str(), "Add - Error!", MB_OK | MB_ICONWARNING);
delete _item;
_item = NULL;
return 0;
}
_item->attach_article(content);
book->add_item(_item);
} else {
if (content != _article) _item->attach_article(content);
}
_item->access_times(atoi(times.c_str()));
SYSTEMTIME st;
DWORD res = _day.GetSystemTime(&st);
if (GDT_VALID == res) {
_item->access_date(date(st.wYear, st.wMonth, st.wDay));
}
//PK let item be displayed in list control
if (ADD == _status && book->is_active()) {
::PostMessage(GetParent(), WM_ITEM_ADD, (WPARAM)_item, 0);
}
//PK clear controls for adding next item
_edit_fn.SetWindowText("");
_edit_times.SetWindowText("0");
_edit_content.SetWindowText("");
if (ADD == _status) _item = NULL;
else EndDialog(wID);
OnBookChange(0, 0, 0);
return 0;
}
LRESULT CAddDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{ //PK 2006/10/11
EndDialog(wID);
return 0;
}
LRESULT CAddDlg::OnCreateBook(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{ //PK 2007/03/05
CAddBookDlg dlg;
if (dlg.DoModal() == IDOK) {
CBook * book = _repository.add_book(dlg._name, dlg._path);
_add_book_to_list(book);
}
return 0;
}
LRESULT CAddDlg::OnBookChange(UINT, int, HWND)
{ //PK 2007/02/23
int index = _cmb_books.GetCurSel();
if (index == -1) return 0;
CBook * book = (CBook*)_cmb_books.GetItemDataPtr(index);
int amout = book->size();
stringstream str;
str << "Total Items: " << amout << std::ends;
_text_total.SetWindowText(str.str().c_str());
return 0;
}
void CAddDlg::_add_book_to_list(CBook * book)
{ //PK 2007/03/05
int idx = _cmb_books.InsertString(_cmb_books.GetCount(), book->book_name().c_str());
_cmb_books.SetItemDataPtr(idx, (void*)book);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -