325 lines
7.6 KiB
C++
325 lines
7.6 KiB
C++
|
// DlgIndicator.cpp: 实现文件
|
|||
|
//
|
|||
|
|
|||
|
#include "DlgSetting.h"
|
|||
|
#include "resource.h"
|
|||
|
#include "scanned_img.h" // for local_trans
|
|||
|
#include "DlgPage.h"
|
|||
|
|
|||
|
// CDlgIndicator 对话框
|
|||
|
#define WM_USB_PACKET_RECEIVED WM_USER + 1
|
|||
|
#define WM_IMAGE_RECEIVED WM_USER + 2
|
|||
|
#define WM_SCAN_FINISHED WM_USER + 3 // WPARAM: std::string* msg; LPARAM: boo err
|
|||
|
|
|||
|
|
|||
|
static IMPLEMENT_OPTION_STRING_COMPARE(cmp_sane_opt);
|
|||
|
|
|||
|
|
|||
|
dlg_setting::dlg_setting(HWND parent, LPSANEAPI api, SANE_Handle dev, bool with_scan, const wchar_t* name) : dlg_base(parent, IDD_SETTING)
|
|||
|
, sane_api_(*api), sane_dev_(dev), with_scan_(with_scan)
|
|||
|
, papers_(0), images_(0), notify_(NULL), notify_param_(NULL), err_(false), tab_(NULL)
|
|||
|
{
|
|||
|
create();
|
|||
|
SetWindowTextW(hwnd(), (std::wstring(name) + L" \u8bbe\u7f6e").c_str());
|
|||
|
}
|
|||
|
dlg_setting::~dlg_setting()
|
|||
|
{
|
|||
|
if (IsWindow(tab_))
|
|||
|
{
|
|||
|
for (int i = 0; i < get_tab_count(); ++i)
|
|||
|
{
|
|||
|
TCITEMW item = { 0 };
|
|||
|
|
|||
|
item.mask = TCIF_PARAM;
|
|||
|
TabCtrl_GetItem(tab_, i, &item);
|
|||
|
if (item.lParam)
|
|||
|
delete (dlg_page*)item.lParam;
|
|||
|
}
|
|||
|
DestroyWindow(tab_);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
BOOL dlg_setting::handle_message(UINT msg, WPARAM wp, LPARAM lp)
|
|||
|
{
|
|||
|
wchar_t text[40] = { 0 };
|
|||
|
BOOL ret = TRUE;
|
|||
|
|
|||
|
switch (msg)
|
|||
|
{
|
|||
|
case WM_INITDIALOG:
|
|||
|
on_init_dialog();
|
|||
|
break;
|
|||
|
case WM_COMMAND:
|
|||
|
handle_command(HIWORD(wp), LOWORD(wp), (HWND)lp);
|
|||
|
break;
|
|||
|
case WM_NOTIFY:
|
|||
|
on_notify((int)wp, (LPNMHDR)lp);
|
|||
|
break;
|
|||
|
case WM_REFRESH_OPTION:
|
|||
|
refresh_controls((int)wp);
|
|||
|
break;
|
|||
|
default:
|
|||
|
ret = FALSE;
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
return ret;
|
|||
|
}
|
|||
|
void dlg_setting::handle_command(WORD code, WORD id, HANDLE ctrl)
|
|||
|
{
|
|||
|
if (id == IDOK)
|
|||
|
{
|
|||
|
notify_over();
|
|||
|
}
|
|||
|
else if (id == IDC_BUTTON_HELP)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
else if (id == IDC_BUTTON_RESTORE)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
void dlg_setting::notify_over(void)
|
|||
|
{
|
|||
|
if (notify_)
|
|||
|
notify_(true, notify_param_);
|
|||
|
}
|
|||
|
void dlg_setting::on_init_dialog(void)
|
|||
|
{
|
|||
|
dlg_page* page = NULL;
|
|||
|
SANE_Int sn = 1;
|
|||
|
SIZE size = { 0 };
|
|||
|
RECT r = { 0 }, rme = { 0 };
|
|||
|
int y = 0;
|
|||
|
const SANE_Option_Descriptor* desc = sane_api_.sane_get_option_descriptor_api(sane_dev_, sn++);
|
|||
|
|
|||
|
while (desc)
|
|||
|
{
|
|||
|
if (desc->type == SANE_TYPE_GROUP)
|
|||
|
{
|
|||
|
if (page)
|
|||
|
dlg_base::get_max_size(size, page->desired_size());
|
|||
|
page = add_tab(desc->title);
|
|||
|
}
|
|||
|
else if (page)
|
|||
|
{
|
|||
|
char* buf = new char[desc->size + 4];
|
|||
|
SANE_Int info = 0;
|
|||
|
|
|||
|
memset(buf, 0, desc->size + 4);
|
|||
|
sane_api_.sane_control_option_api(sane_dev_, sn - 1, SANE_ACTION_GET_VALUE, buf, &info);
|
|||
|
page->add_control(sn - 1, desc, buf);
|
|||
|
delete[] buf;
|
|||
|
}
|
|||
|
else if(desc->type == SANE_TYPE_BUTTON)
|
|||
|
{
|
|||
|
if (cmp_sane_opt(OPTION_TITLE_BZ, desc->title))
|
|||
|
ShowWindow(GetDlgItem(hwnd_, IDC_BUTTON_HELP), SW_SHOW);
|
|||
|
if (cmp_sane_opt(OPTION_TITLE_HFMRSZ, desc->title))
|
|||
|
ShowWindow(GetDlgItem(hwnd_, IDC_BUTTON_RESTORE), SW_SHOW);
|
|||
|
}
|
|||
|
desc = sane_api_.sane_get_option_descriptor_api(sane_dev_, sn++);
|
|||
|
}
|
|||
|
if (page)
|
|||
|
dlg_base::get_max_size(size, page->desired_size());
|
|||
|
|
|||
|
if (size.cx || size.cy || IsWindow(tab_))
|
|||
|
{
|
|||
|
// resize ...
|
|||
|
GetClientRect(hwnd(), &rme);
|
|||
|
if (size.cx < rme.right - rme.left)
|
|||
|
size.cx = rme.right - rme.left;
|
|||
|
|
|||
|
if (IsWindow(tab_))
|
|||
|
{
|
|||
|
GetWindowRect(tab_, &r);
|
|||
|
y = r.bottom - r.top;
|
|||
|
size.cy += y;
|
|||
|
r.right = r.left + rme.right - rme.left;
|
|||
|
screen_2_client(&r);
|
|||
|
MoveWindow(tab_, r.left, r.top, r.right - r.left, y, TRUE);
|
|||
|
}
|
|||
|
|
|||
|
GetWindowRect(hwnd(), &r);
|
|||
|
r.right += size.cx - (rme.right - rme.left);
|
|||
|
r.bottom += size.cy;
|
|||
|
MoveWindow(hwnd(), r.left, r.top, r.right - r.left, r.bottom - r.top, TRUE);
|
|||
|
|
|||
|
for (int i = 0; page = get_page(i); ++i)
|
|||
|
{
|
|||
|
MoveWindow(page->hwnd(), 0, y, size.cx, size.cy - y, TRUE);
|
|||
|
}
|
|||
|
|
|||
|
GetWindowRect(get_item(IDC_BUTTON_SCAN), &r);
|
|||
|
OffsetRect(&r, 0, size.cy);
|
|||
|
screen_2_client(&r);
|
|||
|
MoveWindow(get_item(IDC_BUTTON_SCAN), r.left, r.top, r.right - r.left, r.bottom - r.top, TRUE);
|
|||
|
|
|||
|
GetWindowRect(get_item(IDC_BUTTON_HELP), &r);
|
|||
|
OffsetRect(&r, size.cx - (rme.right - rme.left), size.cy);
|
|||
|
screen_2_client(&r);
|
|||
|
MoveWindow(get_item(IDC_BUTTON_HELP), r.left, r.top, r.right - r.left, r.bottom - r.top, TRUE);
|
|||
|
|
|||
|
GetWindowRect(get_item(IDC_BUTTON_RESTORE), &r);
|
|||
|
OffsetRect(&r, size.cx - (rme.right - rme.left), size.cy);
|
|||
|
screen_2_client(&r);
|
|||
|
MoveWindow(get_item(IDC_BUTTON_RESTORE), r.left, r.top, r.right - r.left, r.bottom - r.top, TRUE);
|
|||
|
|
|||
|
GetWindowRect(get_item(IDOK), &r);
|
|||
|
OffsetRect(&r, size.cx - (rme.right - rme.left), size.cy);
|
|||
|
screen_2_client(&r);
|
|||
|
MoveWindow(get_item(IDOK), r.left, r.top, r.right - r.left, r.bottom - r.top, TRUE);
|
|||
|
}
|
|||
|
select_page(0);
|
|||
|
UpdateWindow(hwnd());
|
|||
|
}
|
|||
|
void dlg_setting::on_notify(int ctrl_id, LPNMHDR pnmh)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
int dlg_setting::get_tab_count(void)
|
|||
|
{
|
|||
|
int count = 0;
|
|||
|
|
|||
|
if (IsWindow(tab_))
|
|||
|
count = TabCtrl_GetItemCount(tab_);
|
|||
|
|
|||
|
return count;
|
|||
|
}
|
|||
|
dlg_page* dlg_setting::add_tab(const char* utf8_title)
|
|||
|
{
|
|||
|
std::wstring title(local_trans::a2u(utf8_title, CP_UTF8));
|
|||
|
dlg_page *page = new dlg_page(hwnd(), title.c_str());
|
|||
|
HFONT font = (HFONT)SendMessage(get_item(IDOK), WM_GETFONT, 0, 0);
|
|||
|
LOGFONTW lf = { 0 };
|
|||
|
|
|||
|
GetObjectW(font, sizeof(lf), &lf);
|
|||
|
page->set_font(font);
|
|||
|
if (!IsWindow(tab_))
|
|||
|
{
|
|||
|
tab_ = CreateWindowW(L"SysTabControl32", L"pages", WS_CHILD | WS_VISIBLE, 0, 0, 100, lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, hwnd(), NULL, g_my_inst, NULL);
|
|||
|
SendMessage(tab_, WM_SETFONT, (WPARAM)SendMessage(get_item(IDOK), WM_GETFONT, 0, 0), 1);
|
|||
|
SetWindowLong(tab_, GWL_ID, 1234);
|
|||
|
ShowWindow(tab_, SW_SHOW);
|
|||
|
}
|
|||
|
|
|||
|
TC_ITEMW item = { 0 };
|
|||
|
|
|||
|
item.mask = TCIF_PARAM | TCIF_TEXT;
|
|||
|
item.lParam = (LPARAM)page;
|
|||
|
item.pszText = &title[0];
|
|||
|
TabCtrl_InsertItem(tab_, get_tab_count(), &item);
|
|||
|
page->hide();
|
|||
|
|
|||
|
return page;
|
|||
|
}
|
|||
|
dlg_page* dlg_setting::get_page(int index)
|
|||
|
{
|
|||
|
dlg_page* page = NULL;
|
|||
|
|
|||
|
if (IsWindow(tab_) && index >= 0 && index < get_tab_count())
|
|||
|
{
|
|||
|
TCITEMW item = { 0 };
|
|||
|
|
|||
|
item.mask = TCIF_PARAM;
|
|||
|
TabCtrl_GetItem(tab_, index, &item);
|
|||
|
page = (dlg_page*)item.lParam;
|
|||
|
}
|
|||
|
|
|||
|
return page;
|
|||
|
}
|
|||
|
dlg_page* dlg_setting::get_page(const char* utf8_title)
|
|||
|
{
|
|||
|
dlg_page* page = NULL;
|
|||
|
std::wstring unic(local_trans::a2u(utf8_title, CP_UTF8));
|
|||
|
|
|||
|
for (int i = 0; i < get_tab_count(); ++i)
|
|||
|
{
|
|||
|
TCITEMW item = { 0 };
|
|||
|
wchar_t buf[80] = { 0 };
|
|||
|
|
|||
|
item.mask = TCIF_TEXT | TCIF_PARAM;
|
|||
|
item.pszText = buf;
|
|||
|
item.cchTextMax = _countof(buf) - 1;
|
|||
|
TabCtrl_GetItem(tab_, i, &item);
|
|||
|
if (unic == buf)
|
|||
|
{
|
|||
|
page = (dlg_page*)item.lParam;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return page;
|
|||
|
}
|
|||
|
dlg_page* dlg_setting::select_page(int index)
|
|||
|
{
|
|||
|
dlg_page* ret = NULL, *cur = NULL;
|
|||
|
|
|||
|
for (int i = 0; cur = get_page(i); ++i)
|
|||
|
{
|
|||
|
if (i == index)
|
|||
|
{
|
|||
|
ret = cur;
|
|||
|
cur->show();
|
|||
|
}
|
|||
|
else
|
|||
|
cur->hide();
|
|||
|
}
|
|||
|
|
|||
|
return ret;
|
|||
|
}
|
|||
|
void dlg_setting::refresh_controls(int src_sn)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
void dlg_setting::set_quit_notify(void(__stdcall* notify)(bool, void*), void* param)
|
|||
|
{
|
|||
|
notify_ = notify;
|
|||
|
notify_param_ = param;
|
|||
|
}
|
|||
|
HWND dlg_setting::window(void)
|
|||
|
{
|
|||
|
return hwnd_;
|
|||
|
}
|
|||
|
HWND dlg_setting::parent(void)
|
|||
|
{
|
|||
|
return parent_;
|
|||
|
}
|
|||
|
void dlg_setting::show(void)
|
|||
|
{
|
|||
|
RECT rp, r;
|
|||
|
|
|||
|
if (IsWindow(parent_))
|
|||
|
GetWindowRect(parent_, &rp);
|
|||
|
else
|
|||
|
GetWindowRect(GetDesktopWindow(), &rp);
|
|||
|
GetWindowRect(hwnd_, &r);
|
|||
|
|
|||
|
rp.left += (rp.right - rp.left - (r.right - r.left)) / 2;
|
|||
|
rp.top += (rp.bottom - rp.top - (r.bottom - r.top)) / 2;
|
|||
|
SetWindowPos(hwnd_, HWND_TOPMOST, rp.left, rp.top, r.right - r.left, r.bottom - r.top, SWP_NOSIZE | SWP_SHOWWINDOW);
|
|||
|
UpdateWindow(hwnd_);
|
|||
|
}
|
|||
|
void dlg_setting::hide(void)
|
|||
|
{
|
|||
|
ShowWindow(hwnd_, SW_HIDE);
|
|||
|
}
|
|||
|
void dlg_setting::notify_data_arrived(bool image)
|
|||
|
{
|
|||
|
PostMessage(hwnd_, image ? WM_IMAGE_RECEIVED : WM_USB_PACKET_RECEIVED, 0, 0);
|
|||
|
}
|
|||
|
void dlg_setting::notify_scan_over(const char* msg, bool err)
|
|||
|
{
|
|||
|
std::string* mstr(new std::string(msg ? msg : ""));
|
|||
|
|
|||
|
err_ = err;
|
|||
|
if (!PostMessage(hwnd_, WM_SCAN_FINISHED, (WPARAM)mstr, (LPARAM)err))
|
|||
|
{
|
|||
|
delete mstr;
|
|||
|
notify_over();
|
|||
|
}
|
|||
|
}
|
|||
|
// CDlgIndicator 消息处理程序
|