summaryrefslogtreecommitdiff
path: root/plugins/SmileyAdd/src/options.cpp
blob: fa987d9dcaa080991d83cc731bd7df43afcdd6d4 (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
/*
Miranda NG SmileyAdd Plugin
Copyright (C) 2012 - 2016 Miranda NG project (http://miranda-ng.org)
Copyright (C) 2005 - 2011 Boris Krasnovskiy All Rights Reserved
Copyright (C) 2003 - 2004 Rein-Peter de Boer

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"

OptionsType opt;

#define UM_CHECKSTATECHANGE (WM_USER + 100)

class OptionsDialogType
{
private:
	HWND m_hwndDialog;
	SmileyCategoryListType tmpsmcat;
	SmileyPackType smPack;

	void InitDialog(void);
	void DestroyDialog(void);
	void AddCategory(void);
	void ApplyChanges(void);
	void UpdateControls(bool force = false);
	void SetChanged(void);
	bool BrowseForSmileyPacks(int item);
	void FilenameChanged(void);
	void ShowSmileyPreview(void);
	void PopulateSmPackList(void);
	void UpdateVisibleSmPackList(void);
	void UserAction(HTREEITEM hItem);
	long GetSelProto(HTREEITEM hItem = NULL);

public:
	OptionsDialogType(HWND hWnd) { m_hwndDialog = hWnd; }
	BOOL DialogProcedure(UINT msg, WPARAM wParam, LPARAM lParam);
};

//OptionsDialog class functions
BOOL OptionsDialogType::DialogProcedure(UINT msg, WPARAM wParam, LPARAM lParam)
{
	BOOL Result = FALSE;

	switch (msg) {
	case WM_INITDIALOG:
		InitDialog();
		Result = TRUE;
		break;

	case WM_DESTROY:
		DestroyDialog();
		break;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDC_FILENAME:
			switch (HIWORD(wParam)) {
			case EN_KILLFOCUS:
				FilenameChanged();
				break;

			case EN_CHANGE:
				if (GetFocus() == (HWND)lParam) SetChanged();
				break;
			}
			break;

		case IDC_BROWSE:
			if (HIWORD(wParam) == BN_CLICKED)
				if (BrowseForSmileyPacks(GetSelProto())) {
					UpdateControls(true);
					SetChanged();
				}
			break;

		case IDC_SMLOPTBUTTON:
			if (HIWORD(wParam) == BN_CLICKED)
				ShowSmileyPreview();
			break;

		case IDC_USESTDPACK:
			if (HIWORD(wParam) == BN_CLICKED) {
				BOOL en = IsDlgButtonChecked(m_hwndDialog, IDC_USESTDPACK) == BST_CHECKED;
				EnableWindow(GetDlgItem(m_hwndDialog, IDC_USEPHYSPROTO), en);
			} // no break!
		case IDC_USEPHYSPROTO:
			if (HIWORD(wParam) == BN_CLICKED) {
				PopulateSmPackList();
				SetChanged();
			}
			break;

		case IDC_PLUGENABLED:
			if (HIWORD(wParam) == BN_CLICKED) {
				BOOL en = IsDlgButtonChecked(m_hwndDialog, IDC_PLUGENABLED) == BST_CHECKED;
				EnableWindow(GetDlgItem(m_hwndDialog, IDC_SMLBUT), en);
				SetChanged();
			}
			break;

		case IDC_ADDCATEGORY:
			if (HIWORD(wParam) == BN_CLICKED)
				AddCategory();
			break;

		case IDC_DELETECATEGORY:
			if (HIWORD(wParam) == BN_CLICKED)
				if (tmpsmcat.DeleteCustomCategory(GetSelProto())) {
					PopulateSmPackList();
					SetChanged();
				}
			break;

		case IDC_SPACES:
		case IDC_SCALETOTEXTHEIGHT:
		case IDC_APPENDSPACES:
		case IDC_SMLBUT:
		case IDC_SCALEALLSMILEYS:
		case IDC_IEVIEWSTYLE:
		case IDC_ANIMATESEL:
		case IDC_ANIMATEDLG:
		case IDC_INPUTSMILEYS:
		case IDC_DCURSORSMILEY:
		case IDC_DISABLECUSTOM:
		case IDC_HQSCALING:
		case IDC_SORTING_HORIZONTAL:
			if (HIWORD(wParam) == BN_CLICKED)
				SetChanged();
			break;

		case IDC_MAXCUSTSMSZ:
		case IDC_MINSMSZ:
			if (HIWORD(wParam) == EN_CHANGE && GetFocus() == (HWND)lParam)
				SetChanged();
			break;
		}
		break;

	case UM_CHECKSTATECHANGE:
		UserAction((HTREEITEM)lParam);
		break;

	case WM_NOTIFY:
		switch (((LPNMHDR)lParam)->idFrom) {
		case 0:
			switch (((LPNMHDR)lParam)->code) {
			case PSN_APPLY:
				ApplyChanges();
				break;
			}
			break;

		case IDC_CATEGORYLIST:
			switch (((LPNMHDR)lParam)->code) {
			case NM_CLICK:
				{
					TVHITTESTINFO ht = { 0 };

					DWORD dwpos = GetMessagePos();
					POINTSTOPOINT(ht.pt, MAKEPOINTS(dwpos));
					MapWindowPoints(HWND_DESKTOP, ((LPNMHDR)lParam)->hwndFrom, &ht.pt, 1);

					TreeView_HitTest(((LPNMHDR)lParam)->hwndFrom, &ht);
					if (TVHT_ONITEM & ht.flags)
						FilenameChanged();
					if (TVHT_ONITEMSTATEICON & ht.flags)
						PostMessage(m_hwndDialog, UM_CHECKSTATECHANGE, 0, (LPARAM)ht.hItem);
				}

			case TVN_KEYDOWN:
				if (((LPNMTVKEYDOWN)lParam)->wVKey == VK_SPACE)
					PostMessage(m_hwndDialog, UM_CHECKSTATECHANGE, 0,
						(LPARAM)TreeView_GetSelection(((LPNMHDR)lParam)->hwndFrom));
				break;

			case TVN_SELCHANGED:
				LPNMTREEVIEW pnmtv = (LPNMTREEVIEW)lParam;
				if (pnmtv->itemNew.state & TVIS_SELECTED)
					UpdateControls();
			}
		}
		break;
	}

	return Result;
}

void OptionsDialogType::AddCategory(void)
{
	TCHAR cat[30];
	GetDlgItemText(m_hwndDialog, IDC_NEWCATEGORY, cat, _countof(cat));
	CMString catd = cat;

	if (!catd.IsEmpty()) {
		tmpsmcat.AddCategory(cat, catd, smcCustom);

		PopulateSmPackList();
		SetChanged();
	}
}

void OptionsDialogType::UserAction(HTREEITEM hItem)
{
	HWND hLstView = GetDlgItem(m_hwndDialog, IDC_CATEGORYLIST);

	if (TreeView_GetCheckState(hLstView, hItem)) {
		if (!BrowseForSmileyPacks(GetSelProto(hItem)))
			TreeView_SetCheckState(hLstView, hItem, TRUE)
	}
	else
		tmpsmcat.GetSmileyCategory(GetSelProto(hItem))->ClearFilename();

	if (hItem == TreeView_GetSelection(hLstView))
		UpdateControls();
	else
		TreeView_SelectItem(hLstView, hItem);

	SetChanged();
	PopulateSmPackList();
}


void OptionsDialogType::SetChanged(void)
{
	SendMessage(GetParent(m_hwndDialog), PSM_CHANGED, 0, 0);
}

void OptionsDialogType::UpdateControls(bool force)
{
	const SmileyCategoryType *smc = tmpsmcat.GetSmileyCategory(GetSelProto());
	if (smc == NULL)
		return;

	const CMString &smf = smc->GetFilename();
	SetDlgItemText(m_hwndDialog, IDC_FILENAME, smf);

	if (smPack.GetFilename() != smf || force)
		smPack.LoadSmileyFile(smf, smPack.GetName(), false, true);

	HWND hLstView = GetDlgItem(m_hwndDialog, IDC_CATEGORYLIST);
	TreeView_SetCheckState(hLstView, TreeView_GetSelection(hLstView), smPack.SmileyCount() != 0);

	SetDlgItemText(m_hwndDialog, IDC_LIBAUTHOR, smPack.GetAuthor().c_str());
	SetDlgItemText(m_hwndDialog, IDC_LIBVERSION, smPack.GetVersion().c_str());
	SetDlgItemText(m_hwndDialog, IDC_LIBNAME, TranslateTS(smPack.GetName().c_str()));
}

long OptionsDialogType::GetSelProto(HTREEITEM hItem)
{
	HWND hLstView = GetDlgItem(m_hwndDialog, IDC_CATEGORYLIST);
	TVITEM tvi = { 0 };

	tvi.mask = TVIF_PARAM;
	tvi.hItem = hItem == NULL ? TreeView_GetSelection(hLstView) : hItem;

	TreeView_GetItem(hLstView, &tvi);

	return (long)tvi.lParam;
}

void OptionsDialogType::UpdateVisibleSmPackList(void)
{
	bool useOne = IsDlgButtonChecked(m_hwndDialog, IDC_USESTDPACK) == BST_UNCHECKED;
	bool usePhysProto = IsDlgButtonChecked(m_hwndDialog, IDC_USEPHYSPROTO) == BST_CHECKED;

	SmileyCategoryListType::SmileyCategoryVectorType &smc = *tmpsmcat.GetSmileyCategoryList();
	for (int i = 0; i < smc.getCount(); i++) {
		bool visiblecat = usePhysProto ? !smc[i].IsAcc() : !smc[i].IsPhysProto();
		bool visible = useOne ? !smc[i].IsProto() : visiblecat;

		if (!visible && smc[i].IsAcc() && !useOne) {
			CMString PhysProtoName = _T("AllProto");
			CMString ProtoName = smc[i].GetName();
			DBVARIANT dbv;
			if (db_get_ts(NULL, _T2A(ProtoName.GetBuffer()), "AM_BaseProto", &dbv) == 0) {
				ProtoName = dbv.ptszVal;
				db_free(&dbv);
			}
			else
				ProtoName.Empty();

			CMString FileName;
			if (!ProtoName.IsEmpty()) {
				PhysProtoName += ProtoName;
				SmileyCategoryType *scm = tmpsmcat.GetSmileyCategory(PhysProtoName);
				if (scm == NULL)
					visible = false;
				else if (scm->GetFilename().IsEmpty())
					visible = true;
			}
		}

		smc[i].SetVisible(visible);
	}
}

void OptionsDialogType::PopulateSmPackList(void)
{
	HWND hLstView = GetDlgItem(m_hwndDialog, IDC_CATEGORYLIST);

	TreeView_SelectItem(hLstView, NULL);
	TreeView_DeleteAllItems(hLstView);

	TVINSERTSTRUCT tvi = { 0 };
	tvi.hParent = TVI_ROOT;
	tvi.hInsertAfter = TVI_LAST;
	tvi.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_STATE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
	tvi.item.stateMask = TVIS_STATEIMAGEMASK | TVIS_SELECTED;
	UpdateVisibleSmPackList();
	SmileyCategoryListType::SmileyCategoryVectorType &smc = *tmpsmcat.GetSmileyCategoryList();
	for (int i = 0; i < smc.getCount(); i++) {
		if (smc[i].IsVisible()) {
			tvi.item.pszText = (TCHAR*)smc[i].GetDisplayName().c_str();
			if (!smc[i].IsProto()) {
				tvi.item.iImage = 0;
				tvi.item.iSelectedImage = 0;
			}
			else {
				tvi.item.iImage = i;
				tvi.item.iSelectedImage = i;
			}
			tvi.item.lParam = i;
			tvi.item.state = INDEXTOSTATEIMAGEMASK(smPack.LoadSmileyFile(smc[i].GetFilename(), smc[i].GetDisplayName(), true, true) ? 2 : 1);
			TreeView_InsertItem(hLstView, &tvi);

			smPack.Clear();
		}
	}
	TreeView_SelectItem(hLstView, TreeView_GetRoot(hLstView));
}

void OptionsDialogType::InitDialog(void)
{
	TranslateDialogDefault(m_hwndDialog);

	CheckDlgButton(m_hwndDialog, IDC_PLUGENABLED, opt.PluginSupportEnabled ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(m_hwndDialog, IDC_SPACES, opt.EnforceSpaces ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(m_hwndDialog, IDC_SCALETOTEXTHEIGHT, opt.ScaleToTextheight ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(m_hwndDialog, IDC_USESTDPACK, opt.UseOneForAll ? BST_UNCHECKED : BST_CHECKED);
	CheckDlgButton(m_hwndDialog, IDC_USEPHYSPROTO, opt.UsePhysProto ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(m_hwndDialog, IDC_APPENDSPACES, opt.SurroundSmileyWithSpaces ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(m_hwndDialog, IDC_SCALEALLSMILEYS, opt.ScaleAllSmileys ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(m_hwndDialog, IDC_IEVIEWSTYLE, opt.IEViewStyle ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(m_hwndDialog, IDC_ANIMATESEL, opt.AnimateSel ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(m_hwndDialog, IDC_ANIMATEDLG, opt.AnimateDlg ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(m_hwndDialog, IDC_INPUTSMILEYS, opt.InputSmileys ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(m_hwndDialog, IDC_DCURSORSMILEY, opt.DCursorSmiley ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(m_hwndDialog, IDC_DISABLECUSTOM, opt.DisableCustom ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(m_hwndDialog, IDC_HQSCALING, opt.HQScaling ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(m_hwndDialog, IDC_SORTING_HORIZONTAL, opt.HorizontalSorting ? BST_CHECKED : BST_UNCHECKED);

	SendDlgItemMessage(m_hwndDialog, IDC_SMLBUT, CB_ADDSTRING, 0, (LPARAM)TranslateT("Hide"));
	SendDlgItemMessage(m_hwndDialog, IDC_SMLBUT, CB_ADDSTRING, 0, (LPARAM)TranslateT("Show"));

	SendDlgItemMessage(m_hwndDialog, IDC_SMLBUT, CB_SETCURSEL, opt.ButtonStatus, 0);
	EnableWindow(GetDlgItem(m_hwndDialog, IDC_SMLBUT), opt.PluginSupportEnabled);
	EnableWindow(GetDlgItem(m_hwndDialog, IDC_USEPHYSPROTO), !opt.UseOneForAll);

	SendDlgItemMessage(m_hwndDialog, IDC_MAXCUSTSPIN, UDM_SETRANGE32, 0, 99);
	SendDlgItemMessage(m_hwndDialog, IDC_MAXCUSTSPIN, UDM_SETPOS, 0, opt.MaxCustomSmileySize);
	SendDlgItemMessage(m_hwndDialog, IDC_MAXCUSTSMSZ, EM_LIMITTEXT, 2, 0);

	SendDlgItemMessage(m_hwndDialog, IDC_MINSPIN, UDM_SETRANGE32, 0, 99);
	SendDlgItemMessage(m_hwndDialog, IDC_MINSPIN, UDM_SETPOS, 0, opt.MinSmileySize);
	SendDlgItemMessage(m_hwndDialog, IDC_MINSMSZ, EM_LIMITTEXT, 2, 0);

	// Create and populate image list
	HIMAGELIST hImList = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
		ILC_MASK | ILC_COLOR32, g_SmileyCategories.NumberOfSmileyCategories(), 0);

	tmpsmcat = g_SmileyCategories;

	SmileyCategoryListType::SmileyCategoryVectorType &smc = *g_SmileyCategories.GetSmileyCategoryList();
	for (int i = 0; i < smc.getCount(); i++) {
		HICON hIcon = NULL;
		if (smc[i].IsProto()) {
			hIcon = (HICON)CallProtoService(_T2A(smc[i].GetName().c_str()), PS_LOADICON, PLI_PROTOCOL | PLIF_SMALL, 0);
			if (hIcon == NULL || (INT_PTR)hIcon == CALLSERVICE_NOTFOUND)
				hIcon = (HICON)CallProtoService(_T2A(smc[i].GetName().c_str()), PS_LOADICON, PLI_PROTOCOL, 0);
		}
		if (hIcon == NULL || hIcon == (HICON)CALLSERVICE_NOTFOUND)
			hIcon = GetDefaultIcon();

		ImageList_AddIcon(hImList, hIcon);
		DestroyIcon(hIcon);
	}

	HWND hLstView = GetDlgItem(m_hwndDialog, IDC_CATEGORYLIST);
	TreeView_SetImageList(hLstView, hImList, TVSIL_NORMAL);

	PopulateSmPackList();
}

void OptionsDialogType::DestroyDialog(void)
{
	HWND hLstView = GetDlgItem(m_hwndDialog, IDC_CATEGORYLIST);
	HIMAGELIST hImList = TreeView_SetImageList(hLstView, NULL, TVSIL_NORMAL);
	ImageList_Destroy(hImList);
}

void OptionsDialogType::ApplyChanges(void)
{
	ProcessAllInputAreas(true);
	CloseSmileys();

	opt.PluginSupportEnabled = IsDlgButtonChecked(m_hwndDialog, IDC_PLUGENABLED) == BST_CHECKED;
	opt.EnforceSpaces = IsDlgButtonChecked(m_hwndDialog, IDC_SPACES) == BST_CHECKED;
	opt.ScaleToTextheight = IsDlgButtonChecked(m_hwndDialog, IDC_SCALETOTEXTHEIGHT) == BST_CHECKED;
	opt.UseOneForAll = IsDlgButtonChecked(m_hwndDialog, IDC_USESTDPACK) == BST_UNCHECKED;
	opt.UsePhysProto = IsDlgButtonChecked(m_hwndDialog, IDC_USEPHYSPROTO) == BST_CHECKED;
	opt.SurroundSmileyWithSpaces = IsDlgButtonChecked(m_hwndDialog, IDC_APPENDSPACES) == BST_CHECKED;
	opt.ScaleAllSmileys = IsDlgButtonChecked(m_hwndDialog, IDC_SCALEALLSMILEYS) == BST_CHECKED;
	opt.IEViewStyle = IsDlgButtonChecked(m_hwndDialog, IDC_IEVIEWSTYLE) == BST_CHECKED;
	opt.AnimateSel = IsDlgButtonChecked(m_hwndDialog, IDC_ANIMATESEL) == BST_CHECKED;
	opt.AnimateDlg = IsDlgButtonChecked(m_hwndDialog, IDC_ANIMATEDLG) == BST_CHECKED;
	opt.InputSmileys = IsDlgButtonChecked(m_hwndDialog, IDC_INPUTSMILEYS) == BST_CHECKED;
	opt.DCursorSmiley = IsDlgButtonChecked(m_hwndDialog, IDC_DCURSORSMILEY) == BST_CHECKED;
	opt.DisableCustom = IsDlgButtonChecked(m_hwndDialog, IDC_DISABLECUSTOM) == BST_CHECKED;
	opt.HQScaling = IsDlgButtonChecked(m_hwndDialog, IDC_HQSCALING) == BST_CHECKED;
	opt.HorizontalSorting = IsDlgButtonChecked(m_hwndDialog, IDC_SORTING_HORIZONTAL) == BST_CHECKED;

	opt.ButtonStatus = (unsigned)SendDlgItemMessage(m_hwndDialog, IDC_SMLBUT, CB_GETCURSEL, 0, 0);
	opt.MaxCustomSmileySize = GetDlgItemInt(m_hwndDialog, IDC_MAXCUSTSMSZ, NULL, FALSE);
	opt.MinSmileySize = GetDlgItemInt(m_hwndDialog, IDC_MINSMSZ, NULL, FALSE);

	opt.Save();

	// Cleanup database
	CMString empty;
	SmileyCategoryListType::SmileyCategoryVectorType &smc = *g_SmileyCategories.GetSmileyCategoryList();
	for (int i = 0; i < smc.getCount(); i++)
		if (tmpsmcat.GetSmileyCategory(smc[i].GetName()) == NULL)
			opt.WritePackFileName(empty, smc[i].GetName());

	g_SmileyCategories = tmpsmcat;
	g_SmileyCategories.SaveSettings();
	g_SmileyCategories.ClearAndLoadAll();

	smPack.LoadSmileyFile(tmpsmcat.GetSmileyCategory(GetSelProto())->GetFilename(), tmpsmcat.GetSmileyCategory(GetSelProto())->GetDisplayName(), false, true);

	NotifyEventHooks(hEvent1, 0, 0);
	ProcessAllInputAreas(false);
}

bool OptionsDialogType::BrowseForSmileyPacks(int item)
{
	OPENFILENAME ofn = { 0 };

	TCHAR filename[MAX_PATH] = _T("");
	ofn.lpstrFile = filename;
	ofn.nMaxFile = _countof(filename);

	CMString inidir;
	SmileyCategoryType *smc = tmpsmcat.GetSmileyCategory(item);
	if (smc->GetFilename().IsEmpty())
		pathToAbsolute(_T("Smileys"), inidir);
	else {
		pathToAbsolute(smc->GetFilename(), inidir);
		inidir.Truncate(inidir.ReverseFind('\\'));
	}

	ofn.lpstrInitialDir = inidir.c_str();
	ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
	ofn.hwndOwner = m_hwndDialog;

	TCHAR filter[512], *pfilter;
	mir_tstrcpy(filter, TranslateT("Smiley packs"));
	mir_tstrcat(filter, _T(" (*.msl;*.asl;*.xep)"));
	pfilter = filter + mir_tstrlen(filter) + 1;
	mir_tstrcpy(pfilter, _T("*.msl;*.asl;*.xep"));
	pfilter = pfilter + mir_tstrlen(pfilter) + 1;
	mir_tstrcpy(pfilter, TranslateT("All files"));
	mir_tstrcat(pfilter, _T(" (*.*)"));
	pfilter = pfilter + mir_tstrlen(pfilter) + 1;
	mir_tstrcpy(pfilter, _T("*.*"));
	pfilter = pfilter + mir_tstrlen(pfilter) + 1;
	*pfilter = '\0';
	ofn.lpstrFilter = filter;

	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_READONLY |
		OFN_EXPLORER | OFN_LONGNAMES | OFN_NOCHANGEDIR;
	ofn.lpstrDefExt = _T("msl");

	if (GetOpenFileName(&ofn)) {
		CMString relpath;
		pathToRelative(filename, relpath);
		smc->SetFilename(relpath);

		return true;
	}

	return false;
}

void OptionsDialogType::FilenameChanged(void)
{
	TCHAR str[MAX_PATH];
	GetDlgItemText(m_hwndDialog, IDC_FILENAME, str, _countof(str));

	SmileyCategoryType *smc = tmpsmcat.GetSmileyCategory(GetSelProto());
	if (smc->GetFilename() != str) {
		CMString temp(str);
		smc->SetFilename(temp);
		UpdateControls();
	}
}

void OptionsDialogType::ShowSmileyPreview(void)
{
	RECT rect;
	GetWindowRect(GetDlgItem(m_hwndDialog, IDC_SMLOPTBUTTON), &rect);

	SmileyToolWindowParam *stwp = new SmileyToolWindowParam;
	stwp->pSmileyPack = &smPack;
	stwp->hWndParent = m_hwndDialog;
	stwp->hWndTarget = NULL;
	stwp->targetMessage = 0;
	stwp->targetWParam = 0;
	stwp->xPosition = rect.left;
	stwp->yPosition = rect.bottom + 4;
	stwp->direction = 1;
	stwp->hContact = NULL;

	mir_forkthread(SmileyToolThread, stwp);
}

void OptionsType::Save(void)
{
	db_set_b(NULL, "SmileyAdd", "PluginSupportEnabled", PluginSupportEnabled);
	db_set_b(NULL, "SmileyAdd", "EnforceSpaces", EnforceSpaces);
	db_set_b(NULL, "SmileyAdd", "ScaleToTextheight", ScaleToTextheight);
	db_set_b(NULL, "SmileyAdd", "UseOneForAll", UseOneForAll);
	db_set_b(NULL, "SmileyAdd", "UsePhysProto", UsePhysProto);
	db_set_b(NULL, "SmileyAdd", "SurroundSmileyWithSpaces", SurroundSmileyWithSpaces);
	db_set_b(NULL, "SmileyAdd", "ScaleAllSmileys", ScaleAllSmileys);
	db_set_b(NULL, "SmileyAdd", "IEViewStyle", IEViewStyle);
	db_set_b(NULL, "SmileyAdd", "AnimateSel", AnimateSel);
	db_set_b(NULL, "SmileyAdd", "AnimateDlg", AnimateDlg);
	db_set_b(NULL, "SmileyAdd", "InputSmileys", InputSmileys);
	db_set_b(NULL, "SmileyAdd", "DCursorSmiley", DCursorSmiley);
	db_set_b(NULL, "SmileyAdd", "DisableCustom", DisableCustom);
	db_set_b(NULL, "SmileyAdd", "HQScaling", HQScaling);
	db_set_b(NULL, "SmileyAdd", "ButtonStatus", (BYTE)ButtonStatus);
	db_set_dw(NULL, "SmileyAdd", "MaxCustomSmileySize", MaxCustomSmileySize);
	db_set_dw(NULL, "SmileyAdd", "MinSmileySize", MinSmileySize);
	db_set_b(NULL, "SmileyAdd", "HorizontalSorting", HorizontalSorting);
}

void OptionsType::Load(void)
{
	PluginSupportEnabled = db_get_b(NULL, "SmileyAdd", "PluginSupportEnabled", TRUE) != 0;
	EnforceSpaces = db_get_b(NULL, "SmileyAdd", "EnforceSpaces", FALSE) != 0;
	ScaleToTextheight = db_get_b(NULL, "SmileyAdd", "ScaleToTextheight", FALSE) != 0;
	UseOneForAll = db_get_b(NULL, "SmileyAdd", "UseOneForAll", TRUE) != 0;
	UsePhysProto = db_get_b(NULL, "SmileyAdd", "UsePhysProto", FALSE) != 0;
	SurroundSmileyWithSpaces =
		db_get_b(NULL, "SmileyAdd", "SurroundSmileyWithSpaces", FALSE) != 0;
	ScaleAllSmileys = db_get_b(NULL, "SmileyAdd", "ScaleAllSmileys", FALSE) != 0;
	IEViewStyle = db_get_b(NULL, "SmileyAdd", "IEViewStyle", FALSE) != 0;
	AnimateSel = db_get_b(NULL, "SmileyAdd", "AnimateSel", TRUE) != 0;
	AnimateDlg = db_get_b(NULL, "SmileyAdd", "AnimateDlg", TRUE) != 0;
	InputSmileys = db_get_b(NULL, "SmileyAdd", "InputSmileys", TRUE) != 0;
	DCursorSmiley = db_get_b(NULL, "SmileyAdd", "DCursorSmiley", FALSE) != 0;
	DisableCustom = db_get_b(NULL, "SmileyAdd", "DisableCustom", FALSE) != 0;
	HQScaling = db_get_b(NULL, "SmileyAdd", "HQScaling", FALSE) != 0;

	ButtonStatus = db_get_b(NULL, "SmileyAdd", "ButtonStatus", 1);
	SelWndBkgClr = db_get_dw(NULL, "SmileyAdd", "SelWndBkgClr", GetSysColor(COLOR_WINDOW));
	MaxCustomSmileySize = db_get_dw(NULL, "SmileyAdd", "MaxCustomSmileySize", 0);
	MinSmileySize = db_get_dw(NULL, "SmileyAdd", "MinSmileySize", 0);
	HorizontalSorting = db_get_b(NULL, "SmileyAdd", "HorizontalSorting", 1) != 0;
}

void OptionsType::ReadPackFileName(CMString &filename, const CMString &name, const CMString &defaultFilename)
{
	CMString settingKey = name + _T("-filename");

	ptrT tszValue(db_get_tsa(NULL, "SmileyAdd", _T2A(settingKey.c_str())));
	filename = (tszValue != NULL) ? (TCHAR*)tszValue : defaultFilename;
}

void OptionsType::WritePackFileName(const CMString &filename, const CMString &name)
{
	CMString settingKey = name + _T("-filename");
	db_set_ts(NULL, "SmileyAdd", _T2A(settingKey.c_str()), filename.c_str());
}

void OptionsType::ReadCustomCategories(CMString &cats)
{
	ptrT tszValue(db_get_tsa(NULL, "SmileyAdd", "CustomCategories"));
	if (tszValue != NULL)
		cats = tszValue;
}

void OptionsType::WriteCustomCategories(const CMString &cats)
{
	if (cats.IsEmpty())
		db_unset(NULL, "SmileyAdd", "CustomCategories");
	else
		db_set_ts(NULL, "SmileyAdd", "CustomCategories", cats.c_str());
}

void OptionsType::ReadContactCategory(MCONTACT hContact, CMString &cats)
{
	ptrT tszValue(db_get_tsa(hContact, "SmileyAdd", "CustomCategory"));
	if (tszValue != NULL)
		cats = tszValue;
}

void OptionsType::WriteContactCategory(MCONTACT hContact, const CMString &cats)
{
	if (cats.IsEmpty())
		db_unset(hContact, "SmileyAdd", "CustomCategory");
	else
		db_set_ts(hContact, "SmileyAdd", "CustomCategory", cats.c_str());
}

/////////////////////////////////////////////////////////////////////////////////////////
// Init and de-init functions, called from main

static INT_PTR CALLBACK DlgProcSmileysOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	OptionsDialogType *pOD = (OptionsDialogType*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
	if (pOD == NULL) {
		pOD = new OptionsDialogType(hwndDlg);
		SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)pOD);
	}

	INT_PTR Result = pOD->DialogProcedure(msg, wParam, lParam);
	SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, Result);

	if (msg == WM_NCDESTROY)
		delete pOD;

	return Result;
}

int SmileysOptionsInitialize(WPARAM addInfo, LPARAM)
{
	OPTIONSDIALOGPAGE odp = { 0 };
	odp.position = 910000000;
	odp.hInstance = g_hInst;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_SMILEYS);
	odp.pszTitle = LPGEN("Smileys");
	odp.pszGroup = LPGEN("Customize");
	odp.pfnDlgProc = DlgProcSmileysOptions;
	odp.flags = ODPF_BOLDGROUPS;
	Options_AddPage(addInfo, &odp);
	return 0;
}