summaryrefslogtreecommitdiff
path: root/plugins/NewStory/src/templates.cpp
blob: 199b0094585259e2dcdcf23274d567b4dc86c6eb (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
#include "stdafx.h"

void TplInitVars(TemplateVars *vars);
void TplCleanVars(TemplateVars *vars);
__forceinline TCHAR *TplGetVar(TemplateVars *vars, char id);
__forceinline void TplSetVar(TemplateVars *vars, char id, TCHAR *v, bool d);
int TplMeasureVars(TemplateVars *vars, TCHAR *str);

void vfGlobal(int mode, TemplateVars *vars, MCONTACT hContact, HistoryArray::ItemData *item);
void vfContact(int mode, TemplateVars *vars, MCONTACT hContact, HistoryArray::ItemData *item);
void vfSystem(int mode, TemplateVars *vars, MCONTACT hContact, HistoryArray::ItemData *item);
void vfEvent(int mode, TemplateVars *vars, MCONTACT hContact, HistoryArray::ItemData *item);
void vfMessage(int mode, TemplateVars *vars, MCONTACT hContact, HistoryArray::ItemData *item);
void vfFile(int mode, TemplateVars *vars, MCONTACT hContact, HistoryArray::ItemData *item);
void vfUrl(int mode, TemplateVars *vars, MCONTACT hContact, HistoryArray::ItemData *item);
void vfSign(int mode, TemplateVars *vars, MCONTACT hContact, HistoryArray::ItemData *item);
void vfAuth(int mode, TemplateVars *vars, MCONTACT hContact, HistoryArray::ItemData *item);
void vfAdded(int mode, TemplateVars *vars, MCONTACT hContact, HistoryArray::ItemData *item);
void vfDeleted(int mode, TemplateVars *vars, MCONTACT hContact, HistoryArray::ItemData *item);
void vfOther(int mode, TemplateVars *vars, MCONTACT hContact, HistoryArray::ItemData *item);

TemplateInfo templates[TPL_COUNT] =
{
	{ "tpl/interface/title", _T("Interface"), ICO_NEWSTORY, _T("Window Title"),
		_T("%N's Newstory [%c messages total]"), 0, 0,
		{ vfGlobal, vfContact, 0, 0, 0 } },
	{ "tpl/interface/passwordq", _T("Interface"), ICO_NOPASSWORD, _T("Password Query"),
		_T("Ooops, %N's history seems to be password protected, so type your password here:"), 0, 0,
		{ vfGlobal, vfContact, 0, 0, 0 } },

	{ "tpl/msglog/msg", _T("Message Log"), ICO_SENDMSG, _T("Messages"),
		_T("%I%i[b]%N, %t:[/b]\x0d\x0a%M"), 0, 0,
		{ vfGlobal, vfContact, vfEvent, vfMessage, 0 } },
	{ "tpl/msglog/file", _T("Message Log"), ICO_FILE, _T("Files"),
		_T("%I%i[b]%N, %t:[/b]%n%M"), 0, 0,
		{ vfGlobal, vfContact, vfEvent, vfFile, 0 } },
	{ "tpl/msglog/url", _T("Message Log"), ICO_URL, _T("URLs"),
		_T("%I%i[b]%N, %t:[/b]%n%M"), 0, 0,
		{ vfGlobal, vfContact, vfEvent, vfUrl, 0 } },
	{ "tpl/msglog/status", _T("Message Log"), ICO_SIGNIN, _T("Status Changes"),
		_T("%I%i[b]%N, %t:[/b]%n%M"), 0, 0,
		{ vfGlobal, vfContact, vfEvent, vfSign, 0 } },
	{ "tpl/msglog/other", _T("Message Log"), ICO_UNKNOWN, _T("Other Events"),
		_T("%I%i[b]%N, %t:[/b]%n%M"), 0, 0,
		{ vfGlobal, vfContact, vfEvent, vfOther, 0 } },

	{ "tpl/msglog/authrq", _T("Message Log"), ICO_UNKNOWN, _T("Authorization Requests"),
		_T("%I%i[b]%N, %t:[/b]%n%M"), 0, 0,
		{ vfGlobal, vfEvent, vfSystem, vfAuth, 0 } },
	{ "tpl/msglog/added", _T("Message Log"), ICO_UNKNOWN, _T("'You were added' events"),
		_T("%I%i[b]%N, %t:[/b]%n%M"), 0, 0,
		{ vfGlobal, vfEvent, vfSystem, vfAdded, 0 } },
	{ "tpl/msglog/deleted", _T("Message Log"), ICO_UNKNOWN, _T("'You were deleted' events"),
		_T("%I%i[b]%N, %t:[/b]%n%M"), 0, 0,
		{ vfGlobal, vfEvent, vfSystem, vfDeleted, 0 } },

	{ "tpl/copy/msg", _T("Clipboard"), ICO_SENDMSG, _T("Messages"),
		_T("%N, %t:\x0d\x0a%M%n"), 0, 0,
		{ vfGlobal, vfContact, vfEvent, vfMessage, 0 } },
	{ "tpl/copy/file", _T("Clipboard"), ICO_FILE, _T("Files"),
		_T("%N, %t:\x0d\x0a%M%n"), 0, 0,
		{ vfGlobal, vfContact, vfEvent, vfFile, 0 } },
	{ "tpl/copy/url", _T("Clipboard"), ICO_URL, _T("URLs"),
		_T("%N, %t:\x0d\x0a%M%n"), 0, 0,
		{ vfGlobal, vfContact, vfEvent, vfUrl, 0 } },
	{ "tpl/copy/status", _T("Clipboard"), ICO_SIGNIN, _T("Status Changes"),
		_T("%N, %t:\x0d\x0a%M%n"), 0, 0,
		{ vfGlobal, vfContact, vfEvent, vfSign, 0 } },
	{ "tpl/copy/other", _T("Clipboard"), ICO_UNKNOWN, _T("Other Events"),
		_T("%N, %t:\x0d\x0a%M%n"), 0, 0,
		{ vfGlobal, vfContact, vfEvent, vfOther, 0 } },

	{ "tpl/copy/authrq", _T("Clipboard"), ICO_UNKNOWN, _T("Authorization Requests"),
		_T("%N, %t:\x0d\x0a%M%n"), 0, 0,
		{ vfGlobal, vfEvent, vfSystem, vfAuth, 0 } },
	{ "tpl/copy/added", _T("Clipboard"), ICO_UNKNOWN, _T("'You were added' events"),
		_T("%N, %t:\x0d\x0a%M%n"), 0, 0,
		{ vfGlobal, vfEvent, vfSystem, vfAdded, 0 } },
	{ "tpl/copy/deleted", _T("Clipboard"), ICO_UNKNOWN, _T("'You were deleted' events"),
		_T("%N, %t:\x0d\x0a%M%n"), 0, 0,
		{ vfGlobal, vfEvent, vfSystem, vfDeleted, 0 } }
};

void LoadTemplates()
{
	for (int i = 0; i < TPL_COUNT; i++)
	{
		DBVARIANT dbv = { 0 };
		db_get_ws(0, MODULENAME, templates[i].setting, &dbv);
		if (templates[i].value)
			free(templates[i].value);
		if (dbv.pwszVal)
		{
			templates[i].value = _tcsdup(dbv.pwszVal);
		}
		else
		{
			templates[i].value = 0;
		}
		db_free(&dbv);
	}
}

void SaveTemplates()
{
	for (int i = 0; i < TPL_COUNT; i++)
		if (templates[i].value)
			db_set_ws(0, MODULENAME, templates[i].setting, templates[i].value);
}

TCHAR *TplFormatStringEx(int tpl, TCHAR *sztpl, MCONTACT hContact, HistoryArray::ItemData *item)
{
	if ((tpl < 0) || (tpl >= TPL_COUNT) || !sztpl)
		return _tcsdup(_T(""));

	int i;
	TemplateVars vars;
	for (i = 0; i < 256; i++)
	{
		vars.del[i] = false;
		vars.val[i] = 0;
	}
	for (i = 0; i < TemplateInfo::VF_COUNT; i++)
		if (templates[tpl].vf[i])
			templates[tpl].vf[i](VFM_VARS, &vars, hContact, item);

	TCHAR *buf = (TCHAR *)malloc(sizeof(TCHAR)*(TplMeasureVars(&vars, sztpl) + 1));
	TCHAR *bufptr = buf;
	for (TCHAR *p = sztpl; *p; p++)
	{
		if (*p == '%')
		{
			TCHAR *var = TplGetVar(&vars, (char)(p[1] & 0xff));
			if (var)
			{
				lstrcpy(bufptr, var);
				bufptr += lstrlen(var);
			}
			p++;
		}
		else
		{
			*bufptr++ = *p;
		}
	}
	*bufptr = 0;
	return buf;
}

TCHAR *TplFormatString(int tpl, MCONTACT hContact, HistoryArray::ItemData *item)
{
	if ((tpl < 0) || (tpl >= TPL_COUNT))
		return _tcsdup(_T(""));

	if (!templates[tpl].value)
		templates[tpl].value = _tcsdup(templates[tpl].defvalue);

	int i;
	TemplateVars vars;
	for (i = 0; i < 256; i++)
	{
		vars.del[i] = false;
		vars.val[i] = 0;
	}
	for (i = 0; i < TemplateInfo::VF_COUNT; i++)
		if (templates[tpl].vf[i])
			templates[tpl].vf[i](VFM_VARS, &vars, hContact, item);

	TCHAR *buf = (TCHAR *)malloc(sizeof(TCHAR)*(TplMeasureVars(&vars, templates[tpl].value) + 1));
	TCHAR *bufptr = buf;
	for (TCHAR *p = templates[tpl].value; *p; p++)
	{
		if (*p == '%')
		{
			TCHAR *var = TplGetVar(&vars, (char)(p[1] & 0xff));
			if (var)
			{
				lstrcpy(bufptr, var);
				bufptr += lstrlen(var);
			}
			p++;
		}
		else
		{
			*bufptr++ = *p;
		}
	}
	*bufptr = 0;
	return buf;
}

// Variable management
void TplInitVars(TemplateVars *vars)
{
	for (int i = 0; i < 256; i++)
	{
		vars->val[i] = 0;
		vars->del[i] = false;
	}
}

void TplCleanVars(TemplateVars *vars)
{
	for (int i = 0; i < 256; i++)
		if (vars->val[i] && vars->del[i])
		{
			free(vars->val[i]);
			vars->val[i] = 0;
			vars->del[i] = false;
		}
}

__forceinline TCHAR *TplGetVar(TemplateVars *vars, char id)
{
	return vars->val[id];
}

__forceinline void TplSetVar(TemplateVars *vars, char id, TCHAR *v, bool d)
{
	if (vars->val[id] && vars->del[id])
		free(vars->val[id]);
	vars->val[id] = v;
	vars->del[id] = d;
}

int TplMeasureVars(TemplateVars *vars, TCHAR *str)
{
	int res = 0;
	for (TCHAR *p = str; *p; p++)
	{
		if (*p == '%')
		{
			TCHAR *var = TplGetVar(vars, (char)(p[1] & 0xff));
			if (var) res += lstrlen(var);
			p++;
		}
		else
		{
			res++;
		}
	}
	return res;
}

// Loading variables
void vfGlobal(int mode, TemplateVars *vars, HANDLE hContact, HistoryArray::ItemData *item)
{
	//  %%: simply % character
	TplSetVar(vars, '%', _T("%"), false);

	//  %n: line break
	TplSetVar(vars, 'n', _T("\x0d\x0a"), false);

	// %M: my nick (not for messages)
	wchar_t *buf = Clist_GetContactDisplayName(0, 0);
	TplSetVar(vars, 'M', buf, false);
}

void vfContact(int mode, TemplateVars *vars, MCONTACT hContact, HistoryArray::ItemData *item)
{
	// %N: buddy's nick (not for messages)
	wchar_t *buff = Clist_GetContactDisplayName(hContact, 0);
	TplSetVar(vars, 'N', buff, false);

	// %c: event count
	TCHAR *buf = new TCHAR[20];
	wsprintf(buf, _T("%d"), db_event_count(hContact));
	TplSetVar(vars, 'c', buf, false);
}

void vfSystem(int mode, TemplateVars *vars, MCONTACT hContact, HistoryArray::ItemData *item)
{
	// %N: buddy's nick (not for messages)
	TplSetVar(vars, 'N', /*TranslateTS*/_T("System Event"), false);

	// %c: event count
	TCHAR *buf = new TCHAR[20];
	wsprintf(buf, _T("%d"), db_event_count(hContact));
	TplSetVar(vars, 'c', buf, false);
}

void vfEvent(int mode, TemplateVars *vars, MCONTACT hContact, HistoryArray::ItemData *item)
{
	HICON hIcon;
	TCHAR *s;

	//  %U: UIN (contextual, own uin for sent, buddys UIN for received messages)

	//  %N: Nickname
	wchar_t *buff = Clist_GetContactDisplayName(item->dbe.flags&DBEF_SENT ? 0 : (WPARAM)item->hContact, 0);
	TplSetVar(vars, 'N', buff, false);

	//  %I: Icon
	switch (item->dbe.eventType)
	{
	case EVENTTYPE_MESSAGE:
		hIcon = GetIcon(ICO_SENDMSG);
		break;
	case EVENTTYPE_FILE:
		hIcon = GetIcon(ICO_FILE);
		break;
	case EVENTTYPE_URL:
		hIcon = GetIcon(ICO_URL);
		break;
	case EVENTTYPE_STATUSCHANGE:
		hIcon = GetIcon(ICO_SIGNIN);
		break;
	default:
		hIcon = GetIcon(ICO_UNKNOWN);
		break;
	}
	s = (TCHAR *)calloc(64, sizeof(TCHAR));
	wsprintf(s, _T("[$hicon=%d$]"), hIcon);
	TplSetVar(vars, 'I', s, true);

	//  %i: Direction icon
	if (item->dbe.flags & DBEF_SENT)
	{
		hIcon = GetIcon(ICO_MSGOUT);
	}
	else
	{
		hIcon = GetIcon(ICO_MSGIN);
	}
	s = (TCHAR *)calloc(64, sizeof(TCHAR));
	wsprintf(s, _T("[$hicon=%d$]"), hIcon);
	TplSetVar(vars, 'i', s, true);

	// %D: direction symbol
	if (item->dbe.flags & DBEF_SENT)
	{
		TplSetVar(vars, 'D', _T("<<"), false);
	}
	else
	{
		TplSetVar(vars, 'D', _T(">>"), false);
	}

	//  %t: timestamp
	TCHAR *buf = (TCHAR *)calloc(100, sizeof(TCHAR));
	_tcsftime(buf, 100, _T("%d.%m.%Y, %H:%M"), localtime((time_t *)&item->dbe.timestamp));
	TplSetVar(vars, 't', buf, true);

	//	DBTIMETOSTRING tts;
	//	tts.cbDest = 100;
	//	tts.szFormat = "d, t";
	//	tts.szDest = (char *)calloc(100, 1);
	//	CallService(MS_DB_TIME_TIMESTAMPTOSTRING, item->dbe.timestamp, (LPARAM)&tts);
	//	TplSetVar(vars, 't', tts.szDest, true);

	//	tts.szFormat = "";
	//	tts.szDest = (char *)calloc(100, 1);
	//	CallService(MS_DB_TIME_TIMESTAMPTOSTRING, item->dbe.timestamp, (LPARAM)&tts);
	//	TplSetVar(vars, 't', tts.szDest, true);

	//  %h: hour (24 hour format, 0-23)
	buf = (TCHAR *)calloc(5, sizeof(TCHAR));
	_tcsftime(buf, 5, _T("%H"), localtime((time_t *)&item->dbe.timestamp));
	TplSetVar(vars, 'h', buf, true);

	//  %a: hour (12 hour format)
	buf = (TCHAR *)calloc(5, sizeof(TCHAR));
	_tcsftime(buf, 5, _T("%h"), localtime((time_t *)&item->dbe.timestamp));
	TplSetVar(vars, 'a', buf, true);

	//  %m: minute
	buf = (TCHAR *)calloc(5, sizeof(TCHAR));
	_tcsftime(buf, 5, _T("%M"), localtime((time_t *)&item->dbe.timestamp));
	TplSetVar(vars, 'm', buf, true);

	//  %s: second
	buf = (TCHAR *)calloc(5, sizeof(TCHAR));
	_tcsftime(buf, 5, _T("%S"), localtime((time_t *)&item->dbe.timestamp));
	TplSetVar(vars, 's', buf, true);

	//  %o: month
	buf = (TCHAR *)calloc(5, sizeof(TCHAR));
	_tcsftime(buf, 5, _T("%m"), localtime((time_t *)&item->dbe.timestamp));
	TplSetVar(vars, 'o', buf, true);

	//  %d: day of month
	buf = (TCHAR *)calloc(5, sizeof(TCHAR));
	_tcsftime(buf, 5, _T("%d"), localtime((time_t *)&item->dbe.timestamp));
	TplSetVar(vars, 'd', buf, true);

	//  %y: year
	buf = (TCHAR *)calloc(5, sizeof(TCHAR));
	_tcsftime(buf, 5, _T("%Y"), localtime((time_t *)&item->dbe.timestamp));
	TplSetVar(vars, 'y', buf, true);

	//  %w: day of week (Sunday, Monday.. translateable)
	buf = (TCHAR *)calloc(25, sizeof(TCHAR));
	_tcsftime(buf, 25, _T("%A"), localtime((time_t *)&item->dbe.timestamp));
	TplSetVar(vars, 'w', TranslateW(buf), false);

	//  %p: AM/PM symbol
	buf = (TCHAR *)calloc(5, sizeof(TCHAR));
	_tcsftime(buf, 5, _T("%p"), localtime((time_t *)&item->dbe.timestamp));
	TplSetVar(vars, 'p', buf, true);

	//  %O: Name of month, translateable
	buf = (TCHAR *)calloc(25, sizeof(TCHAR));
	_tcsftime(buf, 25, _T("%B"), localtime((time_t *)&item->dbe.timestamp));
	TplSetVar(vars, 'O', TranslateW(buf), false);
}

void vfMessage(int mode, TemplateVars *vars, HANDLE hContact, HistoryArray::ItemData *item)
{
	//  %M: the message string itself
	TplSetVar(vars, 'M', item->getTBuf(), false);
}

void vfFile(int mode, TemplateVars *vars, HANDLE hContact, HistoryArray::ItemData *item)
{
	//  %M: the message string itself
	TplSetVar(vars, 'M', item->getTBuf(), false);
	//	TplSetVar(vars, 'M', _T("File event"), false);
}

void vfUrl(int mode, TemplateVars *vars, HANDLE hContact, HistoryArray::ItemData *item)
{
	//  %M: the message string itself
	TplSetVar(vars, 'M', item->getTBuf(), false);
	//	TplSetVar(vars, 'M', _T("URL Event"), false);
}

void vfSign(int mode, TemplateVars *vars, HANDLE hContact, HistoryArray::ItemData *item)
{
	//  %M: the message string itself
	TplSetVar(vars, 'M', item->getTBuf(), false);
}

void vfAuth(int mode, TemplateVars *vars, HANDLE hContact, HistoryArray::ItemData *item)
{
	//  %M: the message string itself
	TplSetVar(vars, 'M', item->getTBuf(), false);
	//	TplSetVar(vars, 'M', _T("Auth Rq"), false);
}

void vfAdded(int mode, TemplateVars *vars, HANDLE hContact, HistoryArray::ItemData *item)
{
	//  %M: the message string itself
	TplSetVar(vars, 'M', item->getTBuf(), false);
	//	TplSetVar(vars, 'M', _T("Added"), false);
}

void vfDeleted(int mode, TemplateVars *vars, HANDLE hContact, HistoryArray::ItemData *item)
{
	//  %M: the message string itself
	TplSetVar(vars, 'M', item->getTBuf(), false);
	//	TplSetVar(vars, 'M', _T("Deleted"), false);
}

void vfOther(int mode, TemplateVars *vars, HANDLE hContact, HistoryArray::ItemData *item)
{
	//  %M: the message string itself
	TplSetVar(vars, 'M', _T("Unknown Event"), false);
}

// Option dialog

BOOL CALLBACK OptTemplatesDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static int CurrentTemplate;
	switch (msg)
	{
	case WM_INITDIALOG:
	{
		CurrentTemplate = -1;

		HTREEITEM hGroup = 0;
		HTREEITEM hFirst = 0;

		HIMAGELIST himgTree = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), ILC_COLOR32 | ILC_MASK, 1, 1);
		TreeView_SetImageList(GetDlgItem(hwnd, IDC_TEMPLATES), himgTree, TVSIL_NORMAL);

		ImageList_AddIcon(himgTree, GetIcon(ICO_TPLGROUP));

		for (int i = 0; i < TPL_COUNT; i++)
		{
			if (!i || lstrcmp(templates[i].group, templates[i - 1].group))
			{
				if (hGroup)
					TreeView_Expand(GetDlgItem(hwnd, IDC_TEMPLATES), hGroup, TVE_EXPAND);

				TVINSERTSTRUCT tvis;
				tvis.hParent = 0;
				tvis.hInsertAfter = TVI_LAST;
				tvis.item.mask = TVIF_TEXT | TVIF_STATE | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
				tvis.item.state = tvis.item.stateMask = TVIS_BOLD;
				tvis.item.iSelectedImage = tvis.item.iImage = 0;
				tvis.item.pszText = templates[i].group;
				tvis.item.lParam = -1;
				hGroup = TreeView_InsertItem(GetDlgItem(hwnd, IDC_TEMPLATES), &tvis);

				if (!hFirst) hFirst = hGroup;
			}

			TVINSERTSTRUCT tvis;
			tvis.hParent = hGroup;
			tvis.hInsertAfter = TVI_LAST;
			tvis.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
			tvis.item.pszText = templates[i].title;
			tvis.item.iSelectedImage = tvis.item.iImage =
				ImageList_AddIcon(himgTree, GetIcon(templates[i].icon));
			tvis.item.lParam = i;
			TreeView_InsertItem(GetDlgItem(hwnd, IDC_TEMPLATES), &tvis);
		}

		if (hGroup)
			TreeView_Expand(GetDlgItem(hwnd, IDC_TEMPLATES), hGroup, TVE_EXPAND);

		TreeView_SelectItem(GetDlgItem(hwnd, IDC_TEMPLATES), hFirst);
		TreeView_EnsureVisible(GetDlgItem(hwnd, IDC_TEMPLATES), hFirst);

		SendMessage(GetDlgItem(hwnd, IDC_DISCARD), BUTTONSETASFLATBTN, 0, 0);
		SendMessage(GetDlgItem(hwnd, IDC_UPDATEPREVIEW), BUTTONSETASFLATBTN, 0, 0);
		SendMessage(GetDlgItem(hwnd, IDC_VARHELP), BUTTONSETASFLATBTN, 0, 0);

		SendMessage(GetDlgItem(hwnd, IDC_DISCARD), BUTTONADDTOOLTIP, (WPARAM)Translate("Cancel Edit"), 0);
		SendMessage(GetDlgItem(hwnd, IDC_UPDATEPREVIEW), BUTTONADDTOOLTIP, (WPARAM)Translate("Update Preview"), 0);
		SendMessage(GetDlgItem(hwnd, IDC_VARHELP), BUTTONADDTOOLTIP, (WPARAM)Translate("Help on Variables"), 0);

		SendMessage(GetDlgItem(hwnd, IDC_DISCARD), BM_SETIMAGE, IMAGE_ICON, (LPARAM)GetIcon(ICO_RESET));
		SendMessage(GetDlgItem(hwnd, IDC_UPDATEPREVIEW), BM_SETIMAGE, IMAGE_ICON, (LPARAM)GetIcon(ICO_PREVIEW));
		SendMessage(GetDlgItem(hwnd, IDC_VARHELP), BM_SETIMAGE, IMAGE_ICON, (LPARAM)GetIcon(ICO_VARHELP));

		return TRUE;
	}

	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDC_EDITTEMPLATE:
		{
			SendMessage(GetParent(GetParent(hwnd)), PSM_CHANGED, 0, 0);
		}

		case IDC_UPDATEPREVIEW:
		{
			if (templates[CurrentTemplate].tmpValue)
				free(templates[CurrentTemplate].tmpValue);
			int length = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDITTEMPLATE)) + 1;
			templates[CurrentTemplate].tmpValue = (TCHAR *)malloc(length * sizeof(TCHAR));
			GetWindowText(GetDlgItem(hwnd, IDC_EDITTEMPLATE), templates[CurrentTemplate].tmpValue, length);

			HistoryArray::ItemData item;
			item.hContact = db_find_first();
			while (item.hContact && !item.hEvent)
			{
				item.hEvent = db_event_first(item.hContact);
				if (!item.hEvent)
					item.hContact = db_find_next(item.hContact);
			}
			if (item.hContact && item.hEvent)
			{
				item.load(ELM_DATA);
				TCHAR *preview = TplFormatStringEx(CurrentTemplate, templates[CurrentTemplate].tmpValue, item.hContact, &item);
				SetWindowText(GetDlgItem(hwnd, IDC_PREVIEW), preview);
				//						SetWindowText(GetDlgItem(hwnd, IDC_GPREVIEW), preview);
				SetWindowText(GetDlgItem(hwnd, IDC_GPREVIEW), _T("$hit :)"));
				free(preview);
			}
			else
			{
				SetWindowText(GetDlgItem(hwnd, IDC_PREVIEW), _T(""));
				SetWindowText(GetDlgItem(hwnd, IDC_GPREVIEW), _T(""));
			}
			break;
		}

		case IDC_DISCARD:
		{
			if (templates[CurrentTemplate].tmpValue)
				free(templates[CurrentTemplate].tmpValue);
			templates[CurrentTemplate].tmpValue = 0;
			if (templates[CurrentTemplate].value)
			{
				SetWindowText(GetDlgItem(hwnd, IDC_EDITTEMPLATE), templates[CurrentTemplate].value);
			}
			else
			{
				SetWindowText(GetDlgItem(hwnd, IDC_EDITTEMPLATE), templates[CurrentTemplate].defvalue);
			}
			PostMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDC_UPDATEPREVIEW, 0), 0);
			break;
		}
		}
		break;

	case WM_NOTIFY:
		switch (((LPNMHDR)lParam)->idFrom)
		{
		case 0:
		{
			switch (((LPNMHDR)lParam)->code)
			{
			case PSN_RESET:
			{
				for (int i = 0; i < TPL_COUNT; i++)
				{
					if (templates[i].tmpValue)
					{
						if (templates[i].tmpValue)
							free(templates[i].tmpValue);
					}
				}
				return TRUE;
			}

			case PSN_APPLY:
			{
				for (int i = 0; i < TPL_COUNT; i++)
				{
					if (templates[i].tmpValue)
					{
						if (templates[i].value)
							free(templates[i].value);
						templates[i].value = templates[i].tmpValue;
						templates[i].tmpValue = 0;
					}
				}
				SaveTemplates();
				return TRUE;
			}
			}
			break;
		}

		case IDC_TEMPLATES:
		{
			switch (((LPNMHDR)lParam)->code)
			{
			case TVN_SELCHANGED:
			case TVN_SELCHANGING:
			{
				LPNMTREEVIEW lpnmtv = (LPNMTREEVIEW)lParam;

				TVITEM tvi;
				tvi.hItem = TreeView_GetSelection(GetDlgItem(hwnd, IDC_TEMPLATES));
				tvi.mask = TVIF_PARAM;
				TreeView_GetItem(GetDlgItem(hwnd, IDC_TEMPLATES), &tvi);

				if ((tvi.lParam < 0) || (tvi.lParam >= TPL_COUNT))
				{
					EnableWindow(GetDlgItem(hwnd, IDC_EDITTEMPLATE), FALSE);
					EnableWindow(GetDlgItem(hwnd, IDC_GPREVIEW), FALSE);
					EnableWindow(GetDlgItem(hwnd, IDC_PREVIEW), FALSE);
					EnableWindow(GetDlgItem(hwnd, IDC_DISCARD), FALSE);
					EnableWindow(GetDlgItem(hwnd, IDC_UPDATEPREVIEW), FALSE);
					EnableWindow(GetDlgItem(hwnd, IDC_VARHELP), FALSE);
					/*								HTREEITEM hItem = TreeView_GetChild(GetDlgItem(hwnd, IDC_TEMPLATES), tvi.hItem);
													if (hItem)
													{
														TreeView_Expand(GetDlgItem(hwnd, IDC_TEMPLATES), tvi.hItem, TVE_EXPAND);
														TreeView_SelectItem(GetDlgItem(hwnd, IDC_TEMPLATES), hItem);
													}*/
					break;
				}
				else
				{
					EnableWindow(GetDlgItem(hwnd, IDC_EDITTEMPLATE), TRUE);
					EnableWindow(GetDlgItem(hwnd, IDC_GPREVIEW), TRUE);
					EnableWindow(GetDlgItem(hwnd, IDC_PREVIEW), TRUE);
					EnableWindow(GetDlgItem(hwnd, IDC_DISCARD), TRUE);
					EnableWindow(GetDlgItem(hwnd, IDC_UPDATEPREVIEW), TRUE);
					EnableWindow(GetDlgItem(hwnd, IDC_VARHELP), TRUE);
				}

				if ((lpnmtv->itemOld.mask&TVIF_HANDLE) && lpnmtv->itemOld.hItem && (lpnmtv->itemOld.hItem != lpnmtv->itemNew.hItem) && (lpnmtv->itemOld.lParam >= 0) && (lpnmtv->itemOld.lParam < TPL_COUNT))
				{
					if (templates[lpnmtv->itemOld.lParam].tmpValue)
						free(templates[lpnmtv->itemOld.lParam].tmpValue);
					int length = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDITTEMPLATE)) + 1;
					templates[lpnmtv->itemOld.lParam].tmpValue = (TCHAR *)malloc(length * sizeof(TCHAR));
					GetWindowText(GetDlgItem(hwnd, IDC_EDITTEMPLATE), templates[lpnmtv->itemOld.lParam].tmpValue, length);
				}

				CurrentTemplate = tvi.lParam;

				if (templates[CurrentTemplate].tmpValue)
				{
					SetWindowText(GetDlgItem(hwnd, IDC_EDITTEMPLATE), templates[CurrentTemplate].tmpValue);
				}
				else
					if (templates[CurrentTemplate].value)
					{
						SetWindowText(GetDlgItem(hwnd, IDC_EDITTEMPLATE), templates[CurrentTemplate].value);
					}
					else
					{
						SetWindowText(GetDlgItem(hwnd, IDC_EDITTEMPLATE), templates[CurrentTemplate].defvalue);
					}

				PostMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDC_UPDATEPREVIEW, 0), 0);

				break;
			}
			case TVN_KEYDOWN:
			case NM_CLICK:
			{
				SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
				break;
			}

			/*
									{
										TVHITTESTINFO hti;
										hti.pt.x = (short)LOWORD(GetMessagePos());
										hti.pt.y = (short)HIWORD(GetMessagePos());
										ScreenToClient(((LPNMHDR)lParam)->hwndFrom,&hti.pt);
										if(TreeView_HitTest(((LPNMHDR)lParam)->hwndFrom,&hti))
											if(hti.flags&TVHT_ONITEMSTATEICON)
												SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
									}
			*/

			}
			break;
		}
		}
		break;
	}
	return FALSE;
}