summaryrefslogtreecommitdiff
path: root/NSCmpts/SourceProp.cpp
blob: 40cb0bf7c1ed8693cc058e6f2a16c6569998cb85 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// SourceProp.cpp : Implementation of CSourceProp
#include "stdafx.h"
#include "NSCmpts.h"
#include "SourceProp.h"
#include <path.h>

/////////////////////////////////////////////////////////////////////////////
// CSourceProp

LRESULT CSourceProp::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	// Load the System Image list
	m_imgList.Load(true);

	// Set the List
	m_ctlList = GetDlgItem(IDC_SOURCE_LIST);
	m_ctlList.SetImageList(m_imgList, LVSIL_SMALL);

	// We need at least one bogus column since we're using
	// report view
	LV_COLUMN lvC;      		// list view column structure

	// Now initialize the columns you will need.
	// Initialize the LV_COLUMN structure.
	// The mask specifies that the fmt, width, pszText, and subitem members
	// of the structure are valid.
	lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_SUBITEM;
	lvC.fmt = LVCFMT_LEFT;  // left-align column
	lvC.cx = 200;            // width of column in pixels
	lvC.iSubItem = 0;
		
	if(m_ctlList.InsertColumn(0, &lvC) == -1)
		return true;

	bHandled = true;
	return TRUE;
}

LRESULT CSourceProp::OnChange(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	// Make sure the column is the width of control
	RECT rect;
	m_ctlList.GetClientRect(&rect);
	m_ctlList.SetColumnWidth(0, rect.right);

	return 0;
}


LRESULT CSourceProp::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	UpdateData(true);
	return 0; 
}

LRESULT CSourceProp::OnShowWindow(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	HWND hwndParent = GetParent();

	// Sets the DropTarget for the Parent Window and removes
	// it when hidden. For some reason if we set it for the 
	// property page itself it doesn't work.
	//
	// Since there could be multiple property pages fiddling
	// with the parent windows Drag Drop Status, we use a 
	// window property to keep track of the current owner.

	if(wParam)
	{	
		IDropTarget* pTarget = NULL;
		QueryInterface(IID_IDropTarget, (void**)&pTarget);
		ASSERT(pTarget);

		// Not mission critical (used only for drag drop)
		RevokeDragDrop(hwndParent);
		HRESULT hr = RegisterDragDrop(hwndParent, pTarget);

		// Register Drag Drop Addrefs
		pTarget->Release();

		// Set the window property to a pointer to us
		if(SUCCEEDED(hr))
			SetProp(hwndParent, _T("DropTarget"), (HANDLE)this);
	}
	else
	{
		// If window property is pointer to us then ...
		if((CSourceProp*)GetProp(hwndParent, _T("DropTarget")) == this)
		{
			// Remove Drag Drop
			RevokeDragDrop(hwndParent);
			RemoveProp(hwndParent, _T("DropTarget"));
		}
	}

	return 0;
}

LRESULT CSourceProp::OnEndLabelEdit(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	LVITEM* pItem = &((NMLVDISPINFO*)pnmh)->item;
	
	// Only Change if there is Text
	if(pItem->pszText == NULL)
		return 0;

	file_path path(pItem->pszText);
	if(path.exists())
	{
		pItem->iImage = m_imgList.GetFileIndex(pItem->pszText);
		// WINBUG: Windows 95 (original version) doesn't set LVIF_TEXT
		// have to set it here.
		pItem->mask |= (LVIF_IMAGE | LVIF_TEXT);

		// Set Item text
		m_ctlList.SetItem(pItem);
	}
	else
	{
		string sTemp;
		sTemp.load_string(IDS_BACKUP_VALID_PATH);

		MessageBox(sTemp, _T("Night Security"), MB_OK | MB_ICONWARNING);
	}
			
	return 0;
}

LRESULT CSourceProp::OnListSetFocus(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	// Disclaimer: This is a kludge fix
	// SysListView32 wouldn't redraw itself when it had focus 
	// and app was activated 
	m_ctlList.Invalidate();
	m_ctlList.RedrawWindow(NULL, NULL, RDW_FRAME | RDW_INVALIDATE);
	return 0;
}

LRESULT CSourceProp::OnKeyDown(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	NMLVKEYDOWN* pLVKeyDow = (NMLVKEYDOWN*)pnmh;

	// if F2 then Edit
	if(pLVKeyDow->wVKey == VK_F2) 
	{
		m_ctlList.SetFocus(); // Needs to have the Focus in order to edit
		m_ctlList.EditLabel(m_ctlList.GetNextItem(-1, LVNI_ALL | LVNI_FOCUSED));
	}

	// If Delete then call Delete Handler
	if(pLVKeyDow->wVKey == VK_DELETE) 
	{
		OnRemove(0, 0, 0, bHandled);
	}

	return 0;
}

LRESULT CSourceProp::OnRemove(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	int iItem = -1;

	// Loop through selected items
	while((iItem = m_ctlList.GetNextItem(-1, LVNI_ALL | LVNI_SELECTED)) != -1) 
		// Take it out of List Box
		m_ctlList.DeleteItem(iItem);

	return 0;
}

LRESULT CSourceProp::OnAdd(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	LPMALLOC pMalloc;
	if(FAILED(::SHGetMalloc(&pMalloc)))
		return 0;

	TCHAR buff[MAX_PATH];
	BROWSEINFO bi;
	bi.hwndOwner = m_hWnd;
	bi.pidlRoot = NULL;
	bi.pszDisplayName = buff;
	bi.lpszTitle = _T("Choose a folder to add.");
	bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
	bi.lpfn = NULL;
    bi.lParam = 0;

	LPITEMIDLIST pidl = SHBrowseForFolder(&bi);

	if(pidl == NULL)
		return 0;

	if(::SHGetPathFromIDList(pidl, buff))
		AddFile(buff);

	pMalloc->Free(pidl);
	pMalloc->Release();

	bHandled = true;
	return 0;
}

bool CSourceProp::AddFile(string sFileName)
{
	int nIndex = m_imgList.GetFileIndex(sFileName);
	
	LVITEM lv;
	lv.mask = LVIF_TEXT | LVIF_IMAGE;
	lv.iItem = 0;
	lv.iSubItem = 0;
	lv.pszText = sFileName.get_buffer(sFileName.size() + 1);
	lv.iImage = nIndex;

	return ListView_InsertItem(GetDlgItem(IDC_SOURCE_LIST), &lv) >= 0;
}

STDMETHODIMP CSourceProp::DragEnter(IDataObject* pDataObject, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect)
{
    HRESULT hr = E_FAIL;
    FORMATETC fmte = 
    {
       CF_HDROP, 		   // use CF_HDROP format
       NULL, 			   // no specific device required
       DVASPECT_CONTENT,   // embedded object
       -1, 				   // must be -1 for DVASPECT_CONTENT
       TYMED_HGLOBAL	   // how to transfer data
    };
    STGMEDIUM medium;

    // No data object
    if (pDataObject == NULL)
	    return E_FAIL;

    // Use the given IDataObject to get a list of filenames (CF_HDROP).
    hr = pDataObject->GetData(&fmte, &medium);

	if(SUCCEEDED(hr))
	{
		ReleaseStgMedium(&medium);
		*pdwEffect = DROPEFFECT_LINK;
	}
	else
		*pdwEffect = DROPEFFECT_NONE;

	return hr;
}

STDMETHODIMP CSourceProp::Drop(IDataObject* pDataObject, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect)
{
    HRESULT hr = E_FAIL;
    FORMATETC fmte = 
    {
       CF_HDROP, 		   // use CF_HDROP format
       NULL, 			   // no specific device required
       DVASPECT_CONTENT,   // embedded object
       -1, 				   // must be -1 for DVASPECT_CONTENT
       TYMED_HGLOBAL	   // how to transfer data
    };
    STGMEDIUM medium;

    // No data object
    if(!pDataObject)
	    return hr;

    // Use the given IDataObject to get a list of filenames (CF_HDROP).
    hr = pDataObject->GetData(&fmte, &medium);

    if(FAILED(hr))
	    return hr;

	// Get Number of Files
	int nFiles = DragQueryFile((HDROP)medium.hGlobal, (UINT) -1, NULL, 0);

	string sFile;
	int nTotLength = 0;

	// Okay now Circle through and get all the files
	for (int nCnt = 0; nCnt < nFiles; nCnt++)
	{
		if(DragQueryFile((HDROP) medium.hGlobal, nCnt, sFile.get_buffer(MAX_PATH), MAX_PATH))
			AddFile(sFile);
	}

    // Release the data.
    ReleaseStgMedium(&medium);

    return hr;
}

void CSourceProp::UpdateData(bool bSave)
{
	if(bSave)
	{
		m_aPaths.clear();
		TCHAR buff[MAX_PATH];
		int nItem = 0;
		while(m_ctlList.GetItemText(nItem++, 0, buff, MAX_PATH))
			m_aPaths.push_back(buff);
	}
	else
	{
		string_array::const_iterator iter = m_aPaths.begin();
		for(; iter != m_aPaths.end(); iter++)
			AddFile(*iter);
	}

}