CPatch: New memory management
[TortoiseGit.git] / src / TortoisePlink / LoginDialog.cpp
blob0d7acf95827e7adb25e63db0f82eeea30afc5d39
1 // TortoiseSVN - a Windows shell extension for easy version control
3 // Copyright (C) 2003, 2013, 2018 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #include "LoginDialog.h"
20 #include "TortoisePlinkRes.h"
21 #include <string>
23 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
25 HINSTANCE g_hmodThisDll;
26 HWND g_hwndMain;
28 class LoginDialog
30 public:
31 LoginDialog(const std::string& prompt);
33 static bool DoLoginDialog(std::string& password, const std::string& prompt);
35 private:
36 bool myOK;
37 HWND _hdlg;
39 std::string myPassword;
40 std::string myPrompt;
42 void CreateModule(void);
43 void RetrieveValues();
45 std::string GetPassword();
47 friend BOOL CALLBACK LoginDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
51 BOOL DoLoginDialog(char* password, int maxlen, const char* prompt)
53 g_hmodThisDll = GetModuleHandle(0);
54 g_hwndMain = GetParentHwnd();
55 std::string passwordstr;
56 BOOL res = LoginDialog::DoLoginDialog(passwordstr, prompt);
57 if (res)
58 strncpy(password, passwordstr.c_str(), maxlen);
59 return res;
63 BOOL CALLBACK LoginDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
65 if (uMsg == WM_INITDIALOG)
67 LoginDialog* pDlg = (LoginDialog*) lParam;
68 pDlg->_hdlg = hwndDlg;
69 SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);
70 // Set prompt text
71 SendDlgItemMessage(hwndDlg, IDC_LOGIN_PROMPT, WM_SETTEXT,
72 pDlg->myPrompt.length(), (LPARAM) pDlg->myPrompt.c_str());
73 // Make sure edit control has the focus
74 //SendDlgItemMessage(hwndDlg, IDC_LOGIN_PASSWORD, WM_SETFOCUS, 0, 0);
75 if (GetDlgCtrlID((HWND) wParam) != IDC_LOGIN_PASSWORD)
77 SetFocus(GetDlgItem(hwndDlg, IDC_LOGIN_PASSWORD));
78 return FALSE;
80 return TRUE;
82 else if (uMsg == WM_COMMAND && LOWORD(wParam) == IDCANCEL && HIWORD(wParam) == BN_CLICKED)
84 LoginDialog* pDlg = (LoginDialog*) GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
85 pDlg->myOK = false;
86 EndDialog(hwndDlg, IDCANCEL);
87 return 1;
89 else if (uMsg == WM_COMMAND && LOWORD(wParam) == IDOK && HIWORD(wParam) == BN_CLICKED)
91 LoginDialog* pDlg = (LoginDialog*) GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
92 pDlg->myOK = true;
93 pDlg->RetrieveValues();
94 EndDialog(hwndDlg, IDOK);
95 return 1;
97 return 0;
100 LoginDialog::LoginDialog(const std::string& prompt)
102 myPrompt = prompt;
105 void LoginDialog::CreateModule(void)
107 DialogBoxParam(g_hmodThisDll, MAKEINTRESOURCE(IDD_LOGIN), g_hwndMain,
108 (DLGPROC)(LoginDialogProc), (LPARAM)this);
112 bool LoginDialog::DoLoginDialog(std::string& password, const std::string& prompt)
114 LoginDialog *pDlg = new LoginDialog(prompt);
116 pDlg->CreateModule();
118 bool ret = pDlg->myOK;
119 password = pDlg->myPassword;
121 delete pDlg;
123 return ret;
127 std::string LoginDialog::GetPassword()
129 char szTxt[256];
130 SendDlgItemMessage(_hdlg, IDC_LOGIN_PASSWORD, WM_GETTEXT, sizeof(szTxt), (LPARAM)szTxt);
131 std::string strText = szTxt;
132 return strText;
135 void LoginDialog::RetrieveValues()
137 myPassword = GetPassword();
141 BOOL IsWinNT()
143 OSVERSIONINFO vi;
144 vi.dwOSVersionInfoSize = sizeof(vi);
145 if (GetVersionEx(&vi))
147 if (vi.dwPlatformId == VER_PLATFORM_WIN32_NT)
149 return TRUE;
152 return FALSE;
155 HWND GetParentHwnd()
157 if (IsWinNT())
159 return GetDesktopWindow();
161 else
163 return GetForegroundWindow();