summaryrefslogtreecommitdiff
path: root/yapp/popup_history.cpp
blob: b10062ff16ffeeaa1c5feb59536c7f0362ab538e (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#define STRICT
#define WIN32_LEAN_AND_MEAN

#include "common.h"
#include "resource.h"
#include "popup_history.h"
#include <time.h>

#define POPUPMENU_TITLE     100
#define POPUPMENU_MESSAGE   101
#define POPUPMENU_TIMESTAMP 102

HWND hHistoryWindow = 0; //the history window
PopupHistoryList lstPopupHistory; //defined in main.cpp

WNDPROC oldPopupsListProc = NULL;

PopupHistoryList::PopupHistoryList()
{
	size = HISTORY_SIZE; //fixed size (at least for now)
	historyData = (PopupHistoryData *) malloc(size * sizeof(PopupHistoryData)); //alloc space for data
	count = 0;
}

PopupHistoryList::~PopupHistoryList()
{
	Clear(); //clear the data strings
	free(historyData); //deallocate the data list
}

void PopupHistoryList::Clear()
{
	int i;
	for (i = 0; i < count; i++)
		{
			DeleteData(i);
		}
	count = 0;
}

int PopupHistoryList::Count()
{
	return count;
}

int PopupHistoryList::Size()
{
	return size;
}

void PopupHistoryList::RemoveItem(int index)
{
	int i;
	DeleteData(index); //free the mem for that particular item
	for (i = index + 1; i < count; i++)
		{
			historyData[i - 1] = historyData[i]; //shift all items to the left
		}
}

void PopupHistoryList::DeleteData(int index)
{
	PopupHistoryData *item = &historyData[index];
	if (item->flags && PHDF_UNICODE) //strings are dupped(), we need to free them
		{
			free(item->titleW);
			free(item->messageW);
		}
		else{
			free(item->title);
			free(item->message);
		}
	item->timestamp = 0; //invalidate item
	item->title = NULL;
	item->message = NULL;
	item->flags = 0;
}

void PopupHistoryList::AddItem(PopupHistoryData item)
{
	if (count >= size)
		{
			RemoveItem(0); //remove first element - the oldest
			count--; //it will be inc'ed later
		}
	historyData[count++] = item; //item has it's relevant strings dupped()
	RefreshPopupHistory(hHistoryWindow);
}

void PopupHistoryList::Add(char *title, char *message, time_t timestamp)
{
	PopupHistoryData item = {0}; //create a history item
	item.timestamp = timestamp;
	item.title = strdup(title);
	item.message = strdup(message);
	AddItem(item); //add it (flags = 0)
}

void PopupHistoryList::Add(wchar_t *title, wchar_t *message, time_t timestamp)
{
	PopupHistoryData item = {0}; //create an unicode history item
	item.flags = PHDF_UNICODE; //mark it as unicode
	item.timestamp = timestamp;
	item.titleW = _wcsdup(title);
	item.messageW = _wcsdup(message);
	AddItem(item); //add it
}

PopupHistoryData *PopupHistoryList::Get(int index)
{
	if ((index < 0) || (index >= count)) //a bit of sanity check
		{
			return NULL;
		}
	
	return &historyData[index];
}

//Stucture passed to list sort function
struct SortParams{
	HWND hList;
	int column;
};

static int lastColumn = -1; //last sort column

int CALLBACK PopupsCompare(LPARAM lParam1, LPARAM lParam2, LPARAM myParam)
{
	SortParams params = *(SortParams *) myParam;
	const int MAX_SIZE = 512;
	TCHAR text1[MAX_SIZE];
	TCHAR text2[MAX_SIZE];
	int res;
	
	ListView_GetItemText(params.hList, (int) lParam1, params.column, text1, MAX_SIZE);
	ListView_GetItemText(params.hList, (int) lParam2, params.column, text2, MAX_SIZE);
	
	res = _tcsicmp(text1, text2);
	
	res = (params.column == lastColumn) ? -res : res; //do reverse search on second click on same column
	
	return res;
}

void RefreshPopupHistory(HWND hWnd)
{
	if (!hWnd)
		{
			return;
		}
	HWND hHistoryList = GetDlgItem(hWnd, IDC_LST_HISTORY);
	ListView_DeleteAllItems(hHistoryList);
	
	int i;
	LVITEM item = {0};
	item.mask = LVIF_TEXT;
	TCHAR buffer[1024];
	struct tm *myTime;
	for (i = 0; i < lstPopupHistory.Count(); i++)
		{
			item.iItem = i;
			PopupHistoryData *popupItem = lstPopupHistory.Get(i);
			item.pszText = popupItem->titleT;
			ListView_InsertItem(hHistoryList, &item);
			ListView_SetItemText(hHistoryList, i, 1, popupItem->messageT);
			myTime = localtime(&popupItem->timestamp);
			_tcsftime(buffer, 1024, _T("%c"), myTime);
			ListView_SetItemText(hHistoryList, i, 2, buffer);
		}
	
	SortParams params = {0};
	params.hList = hHistoryList;
	params.column = lastColumn;
	
	ListView_SortItemsEx(hHistoryList, PopupsCompare, &params);
}

void CopyPopupDataToClipboard(HWND hList, int selection)
{
	if (!selection)
	{
		return;
	}
	
	if (!GetOpenClipboardWindow())
	{
		if (OpenClipboard(hList))
		{
			TCHAR buffer[2048];
			buffer[0] = _T('\0');
			TCHAR *clipboard;
			int i;
			int found = 0;
			int count = ListView_GetItemCount(hList);
			int textType;
#ifdef _UNICODE
			textType = CF_UNICODETEXT;
#else
			textType = CF_TEXT;
#endif
			
			for (i = 0; i < count; i++)
			{
				if (ListView_GetItemState(hList, i, LVIS_SELECTED))
				{
					ListView_GetItemText(hList, i, selection - 100, buffer, 2048);
					found = 1;
					break;
				}
			}
			if (found)
			{
				EmptyClipboard();
				int len = _tcslen(buffer);
				
				HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, (len + 2) * sizeof(TCHAR));
				clipboard = (TCHAR *) GlobalLock(hData);
				_tcsncpy(clipboard, buffer, len);
				clipboard[len] = _T('\0');
				GlobalUnlock(hData);
				if (!SetClipboardData(textType, hData))
				{
					PUShowMessage("Could not set clipboard data", SM_WARNING);
				}
			}
			CloseClipboard();
		}
		else{
			PUShowMessage("Could not open clipboard", SM_WARNING);
		}
	}
	else{
		PUShowMessage("The clipboard is not available", SM_WARNING);
	}
}

//subclass proc for the list view
BOOL CALLBACK PopupsListSubclassProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
		case WM_CONTEXTMENU:
		{
			int x = LOWORD(lParam);
			int y = HIWORD(lParam);
			int selection;		
			
			HMENU hMenu = CreatePopupMenu();
			AppendMenu(hMenu, MF_STRING, POPUPMENU_TITLE, TranslateT("Copy title to clipboard"));
			AppendMenu(hMenu, MF_STRING, POPUPMENU_MESSAGE, TranslateT("Copy message to clipboard"));
			AppendMenu(hMenu, MF_STRING, POPUPMENU_TIMESTAMP, TranslateT("Copy timestamp to clipboard"));
			selection = TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD, x, y, 0, hWnd, NULL);
			DestroyMenu(hMenu);
			if (selection)
			{
				CopyPopupDataToClipboard(hWnd, selection);
			}
		
			break;
		}
		
		case WM_KEYUP:
		{
			switch (wParam)
			{
				case 'C':
				{
					if (GetKeyState(VK_CONTROL))
					{
						CopyPopupDataToClipboard(hWnd, POPUPMENU_MESSAGE);
					}
					
					break;
				}
				
				case VK_ESCAPE:
				{
					SendMessage(GetParent(hWnd), WM_CLOSE, 0, 0);
					
					break;
				}
				
			}
			 
			break;
		}
		
		case WM_SYSKEYDOWN:
		{
			if (wParam == 'X')
			{
				SendMessage(GetParent(hWnd), WM_CLOSE, 0, 0);
			}
			
			break;
		}
	}
	
	return CallWindowProc(oldPopupsListProc, hWnd, msg, wParam, lParam);
}

//this is the history list window handler
BOOL CALLBACK DlgProcHistLst(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
		{
			case WM_INITDIALOG:
				{
					TranslateDialogDefault(hWnd);
					HWND hHistoryList = GetDlgItem(hWnd, IDC_LST_HISTORY);

					ListView_SetExtendedListViewStyleEx (hHistoryList, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);

			oldPopupsListProc = (WNDPROC) SetWindowLong(hHistoryList, GWL_WNDPROC, (LONG) PopupsListSubclassProc);
			
					LVCOLUMN col;
					col.mask = LVCF_TEXT | LVCF_WIDTH;
					col.pszText = _T("Title");
					col.cx = 100;
					ListView_InsertColumn(hHistoryList, 0, &col);
					col.pszText = _T("Message");
					col.cx = 450;
					ListView_InsertColumn(hHistoryList, 1, &col);
					col.pszText = _T("Timestamp");
					col.cx = 115;
					ListView_InsertColumn(hHistoryList, 2, &col);
					RefreshPopupHistory(hWnd);
			
					return TRUE;
				}
				
		case WM_DESTROY:
		{
			hHistoryWindow = NULL;
			
			break;
		}
			
			case WM_CLOSE:
				{
					DestroyWindow(hWnd);
			
					break;
				}
				
			case WM_COMMAND:
				{
					switch (LOWORD(wParam))
						{
							case IDC_CLOSE:
								{
									SendMessage(hWnd, WM_CLOSE, 0, 0);
					
									break;
								}
						}
			
					break;
				}
		
		case WM_NOTIFY:
		{
			switch(((LPNMHDR)lParam)->idFrom)
			{
				case IDC_LST_HISTORY:
				{
					switch (((LPNMHDR)lParam)->code)
					{
						case LVN_COLUMNCLICK:
						{
							LPNMLISTVIEW lv = (LPNMLISTVIEW) lParam;
							int column = lv->iSubItem;
							SortParams params = {0};
							params.hList = GetDlgItem(hWnd, IDC_LST_HISTORY);
							params.column = column;
							
							ListView_SortItemsEx(params.hList, PopupsCompare, (LPARAM) &params);
							lastColumn = (params.column == lastColumn) ? -1 : params.column;

							break;
						}
					}
				
					break;
		}
			}
				
			break;
		}
	}
	
	return 0;
}