1 // TortoiseSVN - a Windows shell extension for easy version control
3 // Copyright (C) 2003, 2013 - 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"
23 HINSTANCE g_hmodThisDll
;
29 LoginDialog(const std::string
& prompt
);
31 static bool DoLoginDialog(std::string
& password
, const std::string
& prompt
);
37 std::string myPassword
;
40 void CreateModule(void);
41 void RetrieveValues();
43 std::string
GetPassword();
45 friend BOOL CALLBACK
LoginDialogProc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
49 BOOL
DoLoginDialog(char* password
, int maxlen
, const char* prompt
)
51 g_hmodThisDll
= GetModuleHandle(0);
52 g_hwndMain
= GetParentHwnd();
53 std::string passwordstr
;
54 BOOL res
= LoginDialog::DoLoginDialog(passwordstr
, prompt
);
56 strncpy(password
, passwordstr
.c_str(), maxlen
);
61 BOOL CALLBACK
LoginDialogProc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
63 if (uMsg
== WM_INITDIALOG
)
65 LoginDialog
* pDlg
= (LoginDialog
*) lParam
;
66 pDlg
->_hdlg
= hwndDlg
;
67 SetWindowLongPtr(hwndDlg
, GWLP_USERDATA
, lParam
);
69 SendDlgItemMessage(hwndDlg
, IDC_LOGIN_PROMPT
, WM_SETTEXT
,
70 pDlg
->myPrompt
.length(), (LPARAM
) pDlg
->myPrompt
.c_str());
71 // Make sure edit control has the focus
72 //SendDlgItemMessage(hwndDlg, IDC_LOGIN_PASSWORD, WM_SETFOCUS, 0, 0);
73 if (GetDlgCtrlID((HWND
) wParam
) != IDC_LOGIN_PASSWORD
)
75 SetFocus(GetDlgItem(hwndDlg
, IDC_LOGIN_PASSWORD
));
80 else if (uMsg
== WM_COMMAND
&& LOWORD(wParam
) == IDCANCEL
&& HIWORD(wParam
) == BN_CLICKED
)
82 LoginDialog
* pDlg
= (LoginDialog
*) GetWindowLongPtr(hwndDlg
, GWLP_USERDATA
);
84 EndDialog(hwndDlg
, IDCANCEL
);
87 else if (uMsg
== WM_COMMAND
&& LOWORD(wParam
) == IDOK
&& HIWORD(wParam
) == BN_CLICKED
)
89 LoginDialog
* pDlg
= (LoginDialog
*) GetWindowLongPtr(hwndDlg
, GWLP_USERDATA
);
91 pDlg
->RetrieveValues();
92 EndDialog(hwndDlg
, IDOK
);
98 LoginDialog::LoginDialog(const std::string
& prompt
)
103 void LoginDialog::CreateModule(void)
105 DialogBoxParam(g_hmodThisDll
, MAKEINTRESOURCE(IDD_LOGIN
), g_hwndMain
,
106 (DLGPROC
)(LoginDialogProc
), (LPARAM
)this);
110 bool LoginDialog::DoLoginDialog(std::string
& password
, const std::string
& prompt
)
112 LoginDialog
*pDlg
= new LoginDialog(prompt
);
114 pDlg
->CreateModule();
116 bool ret
= pDlg
->myOK
;
117 password
= pDlg
->myPassword
;
125 std::string
LoginDialog::GetPassword()
128 SendDlgItemMessage(_hdlg
, IDC_LOGIN_PASSWORD
, WM_GETTEXT
, sizeof(szTxt
), (LPARAM
)szTxt
);
129 std::string strText
= szTxt
;
133 void LoginDialog::RetrieveValues()
135 myPassword
= GetPassword();
142 vi
.dwOSVersionInfoSize
= sizeof(vi
);
143 if (GetVersionEx(&vi
))
145 if (vi
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
157 return GetDesktopWindow();
161 return GetForegroundWindow();