Minor cleanup
[TortoiseGit.git] / src / Utils / MiscUI / BaseDialog.cpp
blob65446418c0903cbc441ce6e9f9de7163c8eec13b
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2007 - Stefan Kueng
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 Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "BaseDialog.h"
23 INT_PTR CDialog::DoModal(HINSTANCE hInstance, int resID, HWND hWndParent)
25 hResource = hInstance;
26 return DialogBoxParam(hInstance, MAKEINTRESOURCE(resID), hWndParent, &CDialog::stDlgFunc, (LPARAM)this);
29 HWND CDialog::Create(HINSTANCE hInstance, int resID, HWND hWndParent)
31 hResource = hInstance;
32 m_hwnd = CreateDialogParam(hInstance, MAKEINTRESOURCE(resID), hWndParent, &CDialog::stDlgFunc, (LPARAM)this);
33 return m_hwnd;
36 void CDialog::InitDialog(HWND hwndDlg, UINT iconID)
38 HWND hwndOwner;
39 RECT rc, rcDlg, rcOwner;
41 hwndOwner = ::GetParent(hwndDlg);
42 if (hwndOwner == NULL)
43 hwndOwner = ::GetDesktopWindow();
45 GetWindowRect(hwndOwner, &rcOwner);
46 GetWindowRect(hwndDlg, &rcDlg);
47 CopyRect(&rc, &rcOwner);
49 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
50 OffsetRect(&rc, -rc.left, -rc.top);
51 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
53 SetWindowPos(hwndDlg, HWND_TOP, rcOwner.left + (rc.right / 2), rcOwner.top + (rc.bottom / 2), 0, 0, SWP_NOSIZE);
54 HICON hIcon = (HICON)::LoadImage(hResource, MAKEINTRESOURCE(iconID), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE|LR_SHARED);
55 ::SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
56 ::SendMessage(hwndDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
59 INT_PTR CALLBACK CDialog::stDlgFunc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
61 CDialog* pWnd;
62 if (uMsg == WM_INITDIALOG)
64 // get the pointer to the window from lpCreateParams
65 SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);
66 pWnd = (CDialog*)lParam;
67 pWnd->m_hwnd = hwndDlg;
69 // get the pointer to the window
70 pWnd = GetObjectFromWindow(hwndDlg);
72 // if we have the pointer, go to the message handler of the window
73 // else, use DefWindowProc
74 if (pWnd)
76 LRESULT lRes = pWnd->DlgFunc(hwndDlg, uMsg, wParam, lParam);
77 SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, lRes);
78 return lRes;
80 else
81 return DefWindowProc(hwndDlg, uMsg, wParam, lParam);