| 1 |
/* |
| 2 |
* (C) 2020- TeraTerm Project |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in the |
| 13 |
* documentation and/or other materials provided with the distribution. |
| 14 |
* 3. The name of the author may not be used to endorse or promote products |
| 15 |
* derived from this software without specific prior written permission. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
| 18 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 |
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include <windows.h> |
| 30 |
#include <stdio.h> |
| 31 |
#define _CRTDBG_MAP_ALLOC |
| 32 |
#include <stdlib.h> |
| 33 |
#include <crtdbg.h> |
| 34 |
#include <wchar.h> |
| 35 |
#include <commctrl.h> |
| 36 |
|
| 37 |
#include "i18n.h" |
| 38 |
#include "tt_res.h" |
| 39 |
#include "ttlib.h" |
| 40 |
#include "dlglib.h" |
| 41 |
#include "tttypes.h" // for WM_USER_DLGHELP2 |
| 42 |
#include "helpid.h" |
| 43 |
#include "codeconv.h" |
| 44 |
#include "asprintf.h" |
| 45 |
#include "win32helper.h" |
| 46 |
#include "tipwin2.h" |
| 47 |
|
| 48 |
#include "sendfiledlg.h" |
| 49 |
|
| 50 |
typedef struct { |
| 51 |
sendfiledlgdata *create_param; |
| 52 |
// work |
| 53 |
TipWin2 *tip; |
| 54 |
UINT MsgDlgHelp; |
| 55 |
} SendFileDlgWork_t; |
| 56 |
|
| 57 |
static INT_PTR CALLBACK SendFileDlgProc(HWND hDlgWnd, UINT msg, WPARAM wp, LPARAM lp) |
| 58 |
{ |
| 59 |
static const DlgTextInfo TextInfos[] = { |
| 60 |
{0, "FILEDLG_TRANS_TITLE_SENDFILE"}, |
| 61 |
{IDC_SENDFILE_FILENAME_TITLE, "DLG_SENDFILE_FILENAME_TITLE"}, |
| 62 |
{IDC_SENDFILE_CHECK_BINARY, "DLG_FOPT_BINARY"}, |
| 63 |
{IDC_SENDFILE_DELAYTYPE_LABEL, "DLG_SENDFILE_DELAYTYPE_TITLE"}, |
| 64 |
{IDC_SENDFILE_SEND_SIZE_LABEL, "DLG_SENDFILE_SEND_SIZE_TITLE"}, |
| 65 |
{IDC_SENDFILE_DELAYTIME_LABEL, "DLG_SENDFILE_DELAYTIME_TITLE"}, |
| 66 |
{IDC_SENDFILE_CHECK_4, "DLG_SENDFILE_TERATERM4"}, |
| 67 |
{IDCANCEL, "BTN_CANCEL"}, |
| 68 |
{IDOK, "BTN_OK"}, |
| 69 |
}; |
| 70 |
static const I18nTextInfo delaytype_list[] = { |
| 71 |
{"DLG_SENDFILE_DELAYTYPE_NO_DELAY", L"no delay"}, |
| 72 |
{"DLG_SENDFILE_DELAYTYPE_PER_CHAR", L"per charactor"}, |
| 73 |
{"DLG_SENDFILE_DELAYTYPE_PER_LINE", L"per line"}, |
| 74 |
{"DLG_SENDFILE_DELAYTYPE_PER_SENDSIZE", L"per sendsize"}, |
| 75 |
}; |
| 76 |
static const int send_size_list[] = {16, 256, 4 * 1024}; |
| 77 |
SendFileDlgWork_t *work = (SendFileDlgWork_t *)GetWindowLongPtr(hDlgWnd, DWLP_USER); |
| 78 |
sendfiledlgdata *data = work != NULL ? work->create_param : NULL; |
| 79 |
|
| 80 |
switch (msg) { |
| 81 |
case WM_INITDIALOG: { |
| 82 |
work = (SendFileDlgWork_t *)calloc(sizeof(*work), 1); |
| 83 |
SetWindowLongPtr(hDlgWnd, DWLP_USER, (LONG_PTR)work); |
| 84 |
data = (sendfiledlgdata *)lp; |
| 85 |
work->create_param = data; |
| 86 |
work->MsgDlgHelp = RegisterWindowMessage(HELPMSGSTRING); |
| 87 |
::DragAcceptFiles(hDlgWnd, TRUE); |
| 88 |
SetDlgTextsW(hDlgWnd, TextInfos, _countof(TextInfos), data->UILanguageFileW); |
| 89 |
CenterWindow(hDlgWnd, GetParent(hDlgWnd)); |
| 90 |
|
| 91 |
SetI18nListW("Tera Term", hDlgWnd, IDC_SENDFILE_DELAYTYPE_DROPDOWN, delaytype_list, _countof(delaytype_list), |
| 92 |
data->UILanguageFileW, 0); |
| 93 |
|
| 94 |
for (size_t i = 0; i < _countof(send_size_list); i++) { |
| 95 |
char buf[32]; |
| 96 |
sprintf(buf, "%d", send_size_list[i]); |
| 97 |
SendDlgItemMessageA(hDlgWnd, IDC_SENDFILE_SEND_SIZE_DROPDOWN, CB_ADDSTRING, 0, (LPARAM)buf); |
| 98 |
} |
| 99 |
SendDlgItemMessage(hDlgWnd, IDC_SENDFILE_SEND_SIZE_DROPDOWN, CB_SETCURSEL, _countof(send_size_list) - 1, 0); |
| 100 |
|
| 101 |
SetDlgItemTextA(hDlgWnd, IDC_SENDFILE_DELAYTIME_EDIT, "1"); |
| 102 |
|
| 103 |
EnableWindow(GetDlgItem(hDlgWnd, IDC_SENDFILE_SEND_SIZE_DROPDOWN), FALSE); |
| 104 |
EnableWindow(GetDlgItem(hDlgWnd, IDC_SENDFILE_DELAYTIME_EDIT), FALSE); |
| 105 |
|
| 106 |
TipWin2 *tip = TipWin2Create(NULL, hDlgWnd); |
| 107 |
work->tip = tip; |
| 108 |
wchar_t *text; |
| 109 |
GetI18nStrWW("Tera Term", "DLG_SENDFILE_TERATERM4_TOOLTIP", NULL, data->UILanguageFileW, &text); |
| 110 |
if (text != NULL) { |
| 111 |
TipWin2SetTextW(tip, IDC_SENDFILE_CHECK_4, text); |
| 112 |
free(text); |
| 113 |
} |
| 114 |
//TipWin2SetTextW(tip, IDC_SENDFILE_FILENAME_EDIT, L"�t�@�C������������"); // test |
| 115 |
//TipWin2SetTextW(tip, IDC_SENDFILE_FILENAME_BUTTON, L"�t�@�C���I��"); // test |
| 116 |
|
| 117 |
return TRUE; |
| 118 |
} |
| 119 |
case WM_DESTROY: { |
| 120 |
TipWin2Destroy(work->tip); |
| 121 |
work->tip = NULL; |
| 122 |
free(work); |
| 123 |
return FALSE; |
| 124 |
} |
| 125 |
case WM_COMMAND: |
| 126 |
switch (wp) { |
| 127 |
case IDOK | (BN_CLICKED << 16): { |
| 128 |
wchar_t *filename; |
| 129 |
hGetWindowTextW(GetDlgItem(hDlgWnd, IDC_SENDFILE_FILENAME_EDIT), &filename); |
| 130 |
const DWORD attr = GetFileAttributesW(filename); |
| 131 |
if (attr == INVALID_FILE_ATTRIBUTES || attr & FILE_ATTRIBUTE_DIRECTORY) { |
| 132 |
static const TTMessageBoxInfoW mbinfo = { |
| 133 |
"Tera Term", |
| 134 |
"MSG_TT_ERROR", L"Tera Term: Error", |
| 135 |
"MSG_CANTOPEN_FILE_ERROR", L"Cannot open file", |
| 136 |
MB_TASKMODAL | MB_ICONEXCLAMATION }; |
| 137 |
TTMessageBoxW(hDlgWnd, &mbinfo, data->UILanguageFileW); |
| 138 |
|
| 139 |
free(filename); |
| 140 |
|
| 141 |
PostMessage(hDlgWnd, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlgWnd, IDC_SENDFILE_FILENAME_EDIT), |
| 142 |
TRUE); |
| 143 |
|
| 144 |
return TRUE; |
| 145 |
} |
| 146 |
|
| 147 |
data->filename = filename; |
| 148 |
data->binary = |
| 149 |
SendMessage(GetDlgItem(hDlgWnd, IDC_SENDFILE_CHECK_BINARY), BM_GETCHECK, 0, 0) == BST_CHECKED |
| 150 |
? TRUE |
| 151 |
: FALSE; |
| 152 |
data->delay_type = (int)SendDlgItemMessage(hDlgWnd, IDC_SENDFILE_DELAYTYPE_DROPDOWN, CB_GETCURSEL, 0, 0); |
| 153 |
data->delay_tick = GetDlgItemInt(hDlgWnd, IDC_SENDFILE_DELAYTIME_EDIT, NULL, FALSE); |
| 154 |
data->send_size = GetDlgItemInt(hDlgWnd, IDC_SENDFILE_SEND_SIZE_DROPDOWN, NULL, FALSE); |
| 155 |
data->method_4 = IsDlgButtonChecked(hDlgWnd, IDC_SENDFILE_CHECK_4); |
| 156 |
|
| 157 |
TTEndDialog(hDlgWnd, IDOK); |
| 158 |
return TRUE; |
| 159 |
} |
| 160 |
|
| 161 |
case IDHELP | (BN_CLICKED << 16): |
| 162 |
PostMessage(GetParent(hDlgWnd), WM_USER_DLGHELP2, HlpMenuFileSendfile, 0); |
| 163 |
return TRUE; |
| 164 |
|
| 165 |
case IDCANCEL | (BN_CLICKED << 16): |
| 166 |
data->filename = NULL; |
| 167 |
TTEndDialog(hDlgWnd, IDCANCEL); |
| 168 |
return TRUE; |
| 169 |
|
| 170 |
case IDC_SENDFILE_FILENAME_BUTTON | (BN_CLICKED << 16): { |
| 171 |
wchar_t TempDir[MAX_PATH]; |
| 172 |
GetCurrentDirectoryW(_countof(TempDir), TempDir); |
| 173 |
|
| 174 |
wchar_t *uimsg; |
| 175 |
GetI18nStrWW("Tera Term", "FILEDLG_TRANS_TITLE_SENDFILE", L"Send file", data->UILanguageFileW, &uimsg); |
| 176 |
wchar_t *title; |
| 177 |
aswprintf(&title, L"Tera Term: %s", uimsg); |
| 178 |
free(uimsg); |
| 179 |
uimsg = NULL; |
| 180 |
|
| 181 |
wchar_t *filterW = GetCommonDialogFilterWW(data->filesend_filter, data->UILanguageFileW); |
| 182 |
wchar_t filename[MAX_PATH]; |
| 183 |
filename[0] = 0; |
| 184 |
OPENFILENAMEW ofn = {}; |
| 185 |
ofn.lStructSize = get_OPENFILENAME_SIZEW(); |
| 186 |
ofn.hwndOwner = hDlgWnd; |
| 187 |
ofn.lpstrFile = filename; |
| 188 |
ofn.nMaxFile = MAX_PATH; |
| 189 |
ofn.lpstrFilter = filterW; |
| 190 |
ofn.nFilterIndex = 0; |
| 191 |
ofn.lpstrTitle = title; |
| 192 |
ofn.Flags = OFN_FILEMUSTEXIST | OFN_SHOWHELP | OFN_HIDEREADONLY; |
| 193 |
BOOL Ok = GetOpenFileNameW(&ofn); |
| 194 |
free(filterW); |
| 195 |
free(title); |
| 196 |
|
| 197 |
SetCurrentDirectoryW(TempDir); |
| 198 |
|
| 199 |
if (Ok) { |
| 200 |
SetDlgItemTextW(hDlgWnd, IDC_SENDFILE_FILENAME_EDIT, filename); |
| 201 |
PostMessage(hDlgWnd, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlgWnd, IDOK), TRUE); |
| 202 |
} |
| 203 |
|
| 204 |
return TRUE; |
| 205 |
} |
| 206 |
|
| 207 |
case IDC_SENDFILE_DELAYTYPE_DROPDOWN | (CBN_SELCHANGE << 16): { |
| 208 |
int sel = (int)SendDlgItemMessage(hDlgWnd, IDC_SENDFILE_DELAYTYPE_DROPDOWN, CB_GETCURSEL, 0, 0); |
| 209 |
EnableWindow(GetDlgItem(hDlgWnd, IDC_SENDFILE_SEND_SIZE_DROPDOWN), sel != 3 ? FALSE : TRUE); |
| 210 |
EnableWindow(GetDlgItem(hDlgWnd, IDC_SENDFILE_DELAYTIME_EDIT), sel == 0 ? FALSE : TRUE); |
| 211 |
return TRUE; |
| 212 |
} |
| 213 |
|
| 214 |
default: |
| 215 |
return FALSE; |
| 216 |
} |
| 217 |
|
| 218 |
case WM_DROPFILES: { |
| 219 |
// �����h���b�v��������������1������������ |
| 220 |
HDROP hDrop = (HDROP)wp; |
| 221 |
const UINT len = DragQueryFileW(hDrop, 0, NULL, 0); |
| 222 |
if (len == 0) { |
| 223 |
DragFinish(hDrop); |
| 224 |
return TRUE; |
| 225 |
} |
| 226 |
wchar_t *filename = (wchar_t *)malloc(sizeof(wchar_t) * (len + 1)); |
| 227 |
DragQueryFileW(hDrop, 0, filename, len + 1); |
| 228 |
filename[len] = '\0'; |
| 229 |
SetDlgItemTextW(hDlgWnd, IDC_SENDFILE_FILENAME_EDIT, filename); |
| 230 |
SendDlgItemMessage(hDlgWnd, IDC_SENDFILE_FILENAME_EDIT, EM_SETSEL, len, len); |
| 231 |
PostMessage(hDlgWnd, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlgWnd, IDOK), TRUE); |
| 232 |
|
| 233 |
free(filename); |
| 234 |
DragFinish(hDrop); |
| 235 |
return TRUE; |
| 236 |
} |
| 237 |
default: |
| 238 |
if (work != NULL && msg == work->MsgDlgHelp) { |
| 239 |
// �R�����_�C�A���O���w���v�{�^������������ |
| 240 |
PostMessage(GetParent(hDlgWnd), WM_USER_DLGHELP2, HlpMenuFileSendfile, 0); |
| 241 |
return TRUE; |
| 242 |
} |
| 243 |
return FALSE; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
INT_PTR sendfiledlg(HINSTANCE hInstance, HWND hWndParent, sendfiledlgdata *data) |
| 248 |
{ |
| 249 |
INT_PTR ret; |
| 250 |
data->method_4 = FALSE; |
| 251 |
ret = TTDialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_SENDFILEDLG), hWndParent, SendFileDlgProc, (LPARAM)data); |
| 252 |
return ret; |
| 253 |
} |