summaryrefslogtreecommitdiff
path: root/common/persistwinpos.h
blob: 8fcef0689ebb6b8523461f5b33b05b5d29db3a14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// 
// AUTHOR
// Stef Walter
//
// LICENSE
// This software is in the public domain.
//
// The software is provided "as is", without warranty of any kind,
// express or implied, including but not limited to the warranties
// of merchantability, fitness for a particular purpose, and
// noninfringement. In no event shall the author(s) be liable for any
// claim, damages, or other liability, whether in an action of
// contract, tort, or otherwise, arising from, out of, or in connection
// with the software or the use or other dealings in the software.
// 
// SUPPORT
// Send bug reports to: <stef@memberwebs.com>
//

#ifndef __PERSISTPOSWINDOW_H__
#define __PERSISTPOSWINDOW_H__

#include "regsettings.h"

template<class T>
class CPersistPosWindow  
{
public:
	CPersistPosWindow(LPCTSTR szPrefix)
		{ m_szPrefix = szPrefix; };

	bool LoadPosition(CRegSettings& settings);
	bool SavePosition(CRegSettings& settings);

protected:
	LPCTSTR m_szPrefix;		// Prefix for Registry Entries
};

template <class T>
bool CPersistPosWindow<T>::LoadPosition(CRegSettings& settings)
{
	// Pointer to the Window Class
	T* pThis = (T*)this;

	// Init WINDOWPLACEMENT Structure
	WINDOWPLACEMENT wp;
	wp.length =	sizeof(WINDOWPLACEMENT);
	wp.flags = 0;
	wp.showCmd = SW_SHOWNA;
	// Not worrying about these for now
	// Probably okay
	wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
	wp.ptMinPosition.x = wp.ptMinPosition.y = 0;


	// This prefix is used before each Registry Entry
	TCHAR sPrefix[MAX_PATH];
	_tcsncpy(sPrefix, m_szPrefix, MAX_PATH);

	LPTSTR sEnd = sPrefix + _tcslen(sPrefix);

	// Load the Window Position from the registry
	_tcsncat(sPrefix, _T("_top"), MAX_PATH);
	wp.rcNormalPosition.top = settings.GetInt(sPrefix, -1);
	*sEnd = 0; _tcsncat(sPrefix, _T("_left"), MAX_PATH);
	wp.rcNormalPosition.left = settings.GetInt(sPrefix, -1);
	*sEnd = 0; _tcsncat(sPrefix, _T("_bottom"), MAX_PATH);
	wp.rcNormalPosition.bottom = settings.GetInt(sPrefix, -1);
	*sEnd = 0; _tcsncat(sPrefix, _T("_right"), MAX_PATH);
	wp.rcNormalPosition.right = settings.GetInt(sPrefix, -1);


	// Make sure each of them got a value from the Registry
	// If not don't proceed
	if(wp.rcNormalPosition.top != -1 &&
	   wp.rcNormalPosition.left != -1 &&
	   wp.rcNormalPosition.bottom != -1 &&
	   wp.rcNormalPosition.right != -1)
	{

		// Check if on screen or not
		WINDOWPLACEMENT wpScreen;
		wpScreen.length = sizeof(WINDOWPLACEMENT);
		wpScreen.flags = 0;

		GetWindowPlacement(GetDesktopWindow(), &wpScreen);
		
		// See if Desktop Window and our Window Intersect
		// If not don't proceed
		RECT rcTemp;
		if(IntersectRect(&rcTemp, &wpScreen.rcNormalPosition, 
					  &wp.rcNormalPosition))
		{

			// Move and Size the actual Window
			SetWindowPlacement(*pThis, &wp);
			return true;

		}
	}

	return false;
}

template <class T>
bool CPersistPosWindow<T>::SavePosition(CRegSettings& settings)
{
	// Pointer to our Window Class
	T* pThis = (T*)this;
	
	WINDOWPLACEMENT wp;
	wp.length =	sizeof(WINDOWPLACEMENT);
	wp.flags = 0;

	// Get the Current Placement
	GetWindowPlacement(*pThis, &wp);

	// This prefix is used before each Registry Entry
	TCHAR sPrefix[MAX_PATH];
	_tcsncpy(sPrefix, m_szPrefix, MAX_PATH);

	LPTSTR sEnd = sPrefix + _tcslen(sPrefix);


	_tcsncat(sPrefix, _T("_top"), MAX_PATH);
	settings.WriteInt(sPrefix, wp.rcNormalPosition.top);
	*sEnd = 0; _tcsncat(sPrefix, _T("_left"), MAX_PATH);
	settings.WriteInt(sPrefix, wp.rcNormalPosition.left);
	*sEnd = 0; _tcsncat(sPrefix, _T("_bottom"), MAX_PATH);
	settings.WriteInt(sPrefix, wp.rcNormalPosition.bottom);
	*sEnd = 0; _tcsncat(sPrefix, _T("_right"), MAX_PATH);
	settings.WriteInt(sPrefix, wp.rcNormalPosition.right);

	return true;
}

#endif