From 3e37efd9cd7d03b7f3a8400ceeea507b4ee54b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Thu, 27 Jul 2023 01:27:24 +0200 Subject: xW: show a connect dialog when run without args --- xW/xW-resources.h | 5 ++++ xW/xW.cpp | 70 +++++++++++++++++++++++++++++++++++-------------------- xW/xW.rc | 21 +++++++++++++++++ 3 files changed, 71 insertions(+), 25 deletions(-) diff --git a/xW/xW-resources.h b/xW/xW-resources.h index caa37ad..47568b0 100644 --- a/xW/xW-resources.h +++ b/xW/xW-resources.h @@ -10,3 +10,8 @@ #define ID_GOTO_ACTIVITY 15 #define ID_TOGGLE_UNIMPORTANT 16 #define ID_DISPLAY_FULL_LOG 17 + +#define IDD_CONNECT 20 +#define IDC_STATIC 21 +#define IDC_HOST 22 +#define IDC_PORT 23 diff --git a/xW/xW.cpp b/xW/xW.cpp index f8e961f..cbc5b30 100644 --- a/xW/xW.cpp +++ b/xW/xW.cpp @@ -113,6 +113,9 @@ struct { // Networking: + std::wstring host; ///< Host as given by user + std::wstring port; ///< Port/service as given by user + addrinfoW *addresses; ///< GetAddrInfo() result addrinfoW *addresses_iterator; ///< Currently processed address SOCKET socket; ///< Relay socket @@ -153,6 +156,15 @@ format_error_message(int err) return copy; } +static std::wstring +window_get_text(HWND hWnd) +{ + int length = GetWindowTextLength(hWnd); + std::wstring buffer(length, {}); + GetWindowText(hWnd, buffer.data(), length + 1); + return buffer; +} + // --- Networking -------------------------------------------------------------- static bool @@ -411,10 +423,7 @@ refresh_status() } // Buffer scrolling would cause a ton of flickering redraws. - int length = GetWindowTextLength(g.hwndStatus); - std::wstring buffer(length, {}); - GetWindowText(g.hwndStatus, buffer.data(), length + 1); - if (buffer != status) + if (window_get_text(g.hwndStatus) != status) SetWindowText(g.hwndStatus, status.c_str()); } @@ -811,8 +820,6 @@ relay_process_callbacks(uint32_t command_seq, } } -static std::wstring input_get_contents(); - static void relay_process_message(const Relay::EventMessage &m) { @@ -941,7 +948,7 @@ relay_process_message(const Relay::EventMessage &m) old->new_unimportant_messages = 0; old->highlighted = false; - old->input = input_get_contents(); + old->input = window_get_text(g.hwndInput); SendMessage(g.hwndInput, EM_GETSEL, (WPARAM) &old->input_start, (LPARAM) &old->input_end); @@ -1183,15 +1190,6 @@ relay_process_socket_events(std::wstring &error) // --- Input line -------------------------------------------------------------- -static std::wstring -input_get_contents() -{ - int length = GetWindowTextLength(g.hwndInput); - std::wstring buffer(length, {}); - GetWindowText(g.hwndInput, buffer.data(), length + 1); - return buffer; -} - static void input_set_contents(const std::wstring &input) { @@ -1209,7 +1207,7 @@ input_submit() auto input = new Relay::CommandData_BufferInput(); input->buffer_name = b->buffer_name; - input->text = input_get_contents(); + input->text = window_get_text(g.hwndInput); // Buffer::history[Buffer::history.size()] is virtual, // and is represented either by edit contents when it's currently @@ -1233,7 +1231,7 @@ input_stamp() { DWORD start = {}, end = {}; SendMessage(g.hwndInput, EM_GETSEL, (WPARAM) &start, (LPARAM) &end); - return {start, end, input_get_contents()}; + return {start, end, window_get_text(g.hwndInput)}; } static void @@ -1340,7 +1338,7 @@ input_proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, if (b->history_at < 1) break; if (b->history_at == b->history.size()) - b->input = input_get_contents(); + b->input = window_get_text(g.hwndInput); input_set_contents(b->history.at(--b->history_at)); return 0; } @@ -1705,6 +1703,26 @@ window_proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) return DefWindowProc(hWnd, uMsg, wParam, lParam); } +static INT_PTR CALLBACK +connect_proc( + HWND hDlg, UINT uMsg, WPARAM wParam, [[maybe_unused]] LPARAM lParam) +{ + switch (uMsg) { + case WM_INITDIALOG: + return TRUE; + case WM_COMMAND: + switch (LOWORD(wParam)) { + case IDOK: + case IDCANCEL: + g.host = window_get_text(GetDlgItem(hDlg, IDC_HOST)); + g.port = window_get_text(GetDlgItem(hDlg, IDC_PORT)); + EndDialog(hDlg, LOWORD(wParam)); + return TRUE; + } + } + return FALSE; +} + static void get_font() { @@ -1883,11 +1901,14 @@ wWinMain(HINSTANCE hInstance, [[maybe_unused]] HINSTANCE hPrevInstance, int argc = 0; LPWSTR *argv = CommandLineToArgvW(pCmdLine, &argc); - if (argc < 2) { - show_error_message( - L"You must pass the relay address and port on the command line."); - return 1; + if (argc >= 2) { + g.host = argv[0]; + g.port = argv[1]; + } else if (DialogBox(hInstance, MAKEINTRESOURCE(IDD_CONNECT), + g.hwndMain, connect_proc) != IDOK) { + return 0; } + LocalFree(argv); // We have a few suboptimal asynchronous options: // a) WSAAsyncGetHostByName() requires us to distinguish hostnames @@ -1898,8 +1919,7 @@ wWinMain(HINSTANCE hInstance, [[maybe_unused]] HINSTANCE hPrevInstance, hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; - err = GetAddrInfo(argv[0], argv[1], &hints, &g.addresses); - LocalFree(argv); + err = GetAddrInfo(g.host.c_str(), g.port.c_str(), &hints, &g.addresses); if (err) { show_error_message(format_error_message(err).c_str()); return 1; diff --git a/xW/xW.rc b/xW/xW.rc index 2948a21..44d1aee 100644 --- a/xW/xW.rc +++ b/xW/xW.rc @@ -31,6 +31,27 @@ BEGIN #endif END +// https://devblogs.microsoft.com/oldnewthing/20050204-00/?p=36523 +// https://devblogs.microsoft.com/oldnewthing/20050207-00/?p=36513 +// +// Note that this is still not the right font to use in newest Windows, +// that would be 9pt Segoe UI, as described in: +// https://learn.microsoft.com/en-us/windows/win32/uxguide/vis-fonts +// or even better yet, NONCLIENTMETRICS::lfMessageFont. +IDD_CONNECT DIALOGEX 0, 0, 150, 64 +STYLE DS_SHELLFONT | DS_MODALFRAME | DS_CENTER \ + | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Connect to Relay" +FONT 8, "MS Shell Dlg", 400 /*FW_NORMAL*/, 0 /*FALSE*/, 0x1 /*DEFAULT_CHARSET*/ +BEGIN + LTEXT "&Host:", IDC_STATIC, 7, 10, 18, 8 + EDITTEXT IDC_HOST, 39, 7, 104, 14, ES_AUTOHSCROLL + LTEXT "&Port:", IDC_STATIC, 7, 28, 18, 8 + EDITTEXT IDC_PORT, 39, 25, 104, 14, ES_AUTOHSCROLL + DEFPUSHBUTTON "&Connect", IDOK, 39, 43, 50, 14 + PUSHBUTTON "E&xit", IDCANCEL, 93, 43, 50, 14 +END + VS_VERSION_INFO VERSIONINFO FILEVERSION PROJECT_MAJOR, PROJECT_MINOR, PROJECT_PATCH, PROJECT_TWEAK PRODUCTVERSION PROJECT_MAJOR, PROJECT_MINOR, PROJECT_PATCH, PROJECT_TWEAK -- cgit v1.2.3