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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
|
/*
Copyright (C) 2009 Ricardo Pescuma Domenecci
This is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this file; see the file license.txt. If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include "commons.h"
#define ICON_SIZE 16
// Prototypes /////////////////////////////////////////////////////////////////////////////////////
HANDLE hOptHook = NULL;
// Functions //////////////////////////////////////////////////////////////////////////////////////
BOOL ScreenToClient(HWND hWnd, LPRECT lpRect)
{
BOOL ret;
POINT pt;
pt.x = lpRect->left;
pt.y = lpRect->top;
ret = ScreenToClient(hWnd, &pt);
if (!ret)
return ret;
lpRect->left = pt.x;
lpRect->top = pt.y;
pt.x = lpRect->right;
pt.y = lpRect->bottom;
ret = ScreenToClient(hWnd, &pt);
lpRect->right = pt.x;
lpRect->bottom = pt.y;
return ret;
}
static void RemoveExtraIcons(int slot)
{
HANDLE hContact = db_find_first();
while (hContact != NULL)
{
Clist_SetExtraIcon(hContact, slot, INVALID_HANDLE_VALUE);
hContact = db_find_next(hContact);
}
}
#ifndef TVIS_FOCUSED
#define TVIS_FOCUSED 1
#endif
WNDPROC origTreeProc;
static bool IsSelected(HWND tree, HTREEITEM hItem)
{
return (TVIS_SELECTED & TreeView_GetItemState(tree, hItem, TVIS_SELECTED)) == TVIS_SELECTED;
}
static void Tree_Select(HWND tree, HTREEITEM hItem)
{
TreeView_SetItemState(tree, hItem, TVIS_SELECTED, TVIS_SELECTED);
}
static void Tree_Unselect(HWND tree, HTREEITEM hItem)
{
TreeView_SetItemState(tree, hItem, 0, TVIS_SELECTED);
}
static void Tree_DropHilite(HWND tree, HTREEITEM hItem)
{
TreeView_SetItemState(tree, hItem, TVIS_DROPHILITED, TVIS_DROPHILITED);
}
static void Tree_DropUnhilite(HWND tree, HTREEITEM hItem)
{
TreeView_SetItemState(tree, hItem, 0, TVIS_DROPHILITED);
}
static void UnselectAll(HWND tree)
{
TreeView_SelectItem(tree, NULL);
HTREEITEM hItem = TreeView_GetRoot(tree);
while (hItem)
{
Tree_Unselect(tree, hItem);
hItem = TreeView_GetNextSibling(tree, hItem);
}
}
static void Tree_SelectRange(HWND tree, HTREEITEM hStart, HTREEITEM hEnd)
{
int start = 0;
int end = 0;
int i = 0;
HTREEITEM hItem = TreeView_GetRoot(tree);
while (hItem)
{
if (hItem == hStart)
start = i;
if (hItem == hEnd)
end = i;
i++;
hItem = TreeView_GetNextSibling(tree, hItem);
}
if (end < start)
{
int tmp = start;
start = end;
end = tmp;
}
i = 0;
hItem = TreeView_GetRoot(tree);
while (hItem)
{
if (i >= start)
Tree_Select(tree, hItem);
if (i == end)
break;
i++;
hItem = TreeView_GetNextSibling(tree, hItem);
}
}
static int GetNumSelected(HWND tree)
{
int ret = 0;
HTREEITEM hItem = TreeView_GetRoot(tree);
while (hItem)
{
if (IsSelected(tree, hItem))
ret++;
hItem = TreeView_GetNextSibling(tree, hItem);
}
return ret;
}
static void Tree_GetSelected(HWND tree, vector<HTREEITEM> &selected)
{
HTREEITEM hItem = TreeView_GetRoot(tree);
while (hItem)
{
if (IsSelected(tree, hItem))
selected.push_back(hItem);
hItem = TreeView_GetNextSibling(tree, hItem);
}
}
static void Tree_Select(HWND tree, vector<HTREEITEM> &selected)
{
for (unsigned int i = 0; i < selected.size(); i++)
if (selected[i] != NULL)
Tree_Select(tree, selected[i]);
}
LRESULT CALLBACK TreeProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_LBUTTONDOWN:
{
DWORD pos = (DWORD) lParam;
TVHITTESTINFO hti;
hti.pt.x = (short) LOWORD(pos);
hti.pt.y = (short) HIWORD(pos);
if (!TreeView_HitTest(hwndDlg, &hti))
{
UnselectAll(hwndDlg);
break;
}
if (!(wParam & (MK_CONTROL | MK_SHIFT)) || !(hti.flags & (TVHT_ONITEMICON | TVHT_ONITEMLABEL
| TVHT_ONITEMRIGHT)))
{
UnselectAll(hwndDlg);
TreeView_SelectItem(hwndDlg, hti.hItem);
break;
}
if (wParam & MK_CONTROL)
{
vector<HTREEITEM> selected;
Tree_GetSelected(hwndDlg, selected);
// Check if have to deselect it
for (unsigned int i = 0; i < selected.size(); i++)
{
if (selected[i] == hti.hItem)
{
// Deselect it
UnselectAll(hwndDlg);
selected[i] = NULL;
if (i > 0)
hti.hItem = selected[0];
else if (i + 1 < selected.size())
hti.hItem = selected[i + 1];
else
hti.hItem = NULL;
break;
}
}
TreeView_SelectItem(hwndDlg, hti.hItem);
Tree_Select(hwndDlg, selected);
}
else if (wParam & MK_SHIFT)
{
HTREEITEM hItem = TreeView_GetSelection(hwndDlg);
if (hItem == NULL)
break;
vector<HTREEITEM> selected;
Tree_GetSelected(hwndDlg, selected);
TreeView_SelectItem(hwndDlg, hti.hItem);
Tree_Select(hwndDlg, selected);
Tree_SelectRange(hwndDlg, hItem, hti.hItem);
}
return 0;
}
}
return CallWindowProc(origTreeProc, hwndDlg, msg, wParam, lParam);
}
static vector<int> * Tree_GetIDs(HWND tree, HTREEITEM hItem)
{
TVITEM tvi = { 0 };
tvi.mask = TVIF_HANDLE | TVIF_PARAM;
tvi.hItem = hItem;
TreeView_GetItem(tree, &tvi);
return (vector<int> *) tvi.lParam;
}
static HTREEITEM Tree_AddExtraIcon(HWND tree, BaseExtraIcon *extra, bool selected, HTREEITEM hAfter = TVI_LAST)
{
vector<int> *ids = new vector<int> ;
ids->push_back(extra->getID());
TVINSERTSTRUCT tvis = { 0 };
tvis.hParent = NULL;
tvis.hInsertAfter = hAfter;
tvis.item.mask = TVIF_PARAM | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_STATE;
tvis.item.stateMask = TVIS_STATEIMAGEMASK;
tvis.item.iSelectedImage = tvis.item.iImage = extra->getID();
tvis.item.lParam = (LPARAM) ids;
tvis.item.pszText = (char *) extra->getDescription();
tvis.item.state = INDEXTOSTATEIMAGEMASK(selected ? 2 : 1);
return TreeView_InsertItem(tree, &tvis);
}
static HTREEITEM Tree_AddExtraIconGroup(HWND tree, vector<int> &group, bool selected, HTREEITEM hAfter = TVI_LAST)
{
vector<int> *ids = new vector<int> ;
string desc;
int img = 0;
for (unsigned int i = 0; i < group.size(); ++i)
{
BaseExtraIcon *extra = registeredExtraIcons[group[i] - 1];
ids->push_back(extra->getID());
if (img == 0 && !IsEmpty(extra->getDescIcon()))
img = extra->getID();
if (i > 0)
desc += " / ";
desc += extra->getDescription();
}
TVINSERTSTRUCT tvis = { 0 };
tvis.hParent = NULL;
tvis.hInsertAfter = hAfter;
tvis.item.mask = TVIF_PARAM | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_STATE;
tvis.item.stateMask = TVIS_STATEIMAGEMASK;
tvis.item.iSelectedImage = tvis.item.iImage = img;
tvis.item.lParam = (LPARAM) ids;
tvis.item.pszText = (char *) desc.c_str();
tvis.item.state = INDEXTOSTATEIMAGEMASK(selected ? 2 : 1);
return TreeView_InsertItem(tree, &tvis);
}
static void GroupSelectedItems(HWND tree)
{
vector<HTREEITEM> toRemove;
vector<int> ids;
bool selected = false;
HTREEITEM hPlace = NULL;
// Find items
HTREEITEM hItem = TreeView_GetRoot(tree);
TVITEM tvi = { 0 };
tvi.mask = TVIF_HANDLE | TVIF_PARAM | TVIF_TEXT | TVIF_STATE;
while (hItem)
{
if (IsSelected(tree, hItem))
{
if (hPlace == NULL)
hPlace = hItem;
tvi.hItem = hItem;
TreeView_GetItem(tree, &tvi);
vector<int> *iids = (vector<int> *) tvi.lParam;
ids.insert(ids.end(), iids->begin(), iids->end());
if ((tvi.state & INDEXTOSTATEIMAGEMASK(3)) == INDEXTOSTATEIMAGEMASK(2))
selected = true;
toRemove.push_back(hItem);
}
hItem = TreeView_GetNextSibling(tree, hItem);
}
if (hPlace == NULL)
return; // None selected
// Add new
int ii = ids.at(0);
ii = ids.at(1);
HTREEITEM hNew = Tree_AddExtraIconGroup(tree, ids, selected, hPlace);
// Remove old
for (unsigned int i = 0; i < toRemove.size(); ++i)
{
delete Tree_GetIDs(tree, toRemove[i]);
TreeView_DeleteItem(tree, toRemove[i]);
}
// Select
UnselectAll(tree);
TreeView_SelectItem(tree, hNew);
}
static void UngroupSelectedItems(HWND tree)
{
HTREEITEM hItem = TreeView_GetSelection(tree);
if (hItem == NULL)
return;
vector<int> *ids = Tree_GetIDs(tree, hItem);
if (ids->size() < 2)
return;
bool selected = IsSelected(tree, hItem);
for (size_t i = ids->size(); i > 0; --i)
{
BaseExtraIcon *extra = registeredExtraIcons[ids->at(i - 1) - 1];
Tree_AddExtraIcon(tree, extra, selected, hItem);
}
delete Tree_GetIDs(tree, hItem);
TreeView_DeleteItem(tree, hItem);
UnselectAll(tree);
}
static int ShowPopup(HWND hwndDlg, int popup)
{
// Fix selection
HWND tree = GetDlgItem(hwndDlg, IDC_EXTRAORDER);
HTREEITEM hSelected = (HTREEITEM) SendMessage(tree, TVM_GETNEXTITEM, TVGN_DROPHILITE, 0);
HTREEITEM hItem = TreeView_GetRoot(tree);
while (hItem)
{
if (hItem != hSelected && IsSelected(tree, hItem))
Tree_DropHilite(tree, hItem);
hItem = TreeView_GetNextSibling(tree, hItem);
}
// InvalidateRect(tree, NULL, FALSE);
HMENU menu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_OPT_POPUP));
HMENU submenu = GetSubMenu(menu, popup);
TranslateMenu(submenu);
DWORD pos = GetMessagePos();
int ret = TrackPopupMenu(submenu, TPM_TOPALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_LEFTALIGN, LOWORD(pos),
HIWORD(pos), 0, hwndDlg, NULL);
DestroyMenu(menu);
// Revert selection
hItem = TreeView_GetRoot(tree);
while (hItem)
{
if (hItem != hSelected && IsSelected(tree, hItem))
Tree_DropUnhilite(tree, hItem);
hItem = TreeView_GetNextSibling(tree, hItem);
}
return ret;
}
static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
vector<int> *a = (vector<int> *) lParam1;
vector<int> *b = (vector<int> *) lParam2;
return registeredExtraIcons[a->at(0) - 1]->compare(registeredExtraIcons[b->at(0) - 1]);
}
static INT_PTR CALLBACK OptionsDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
static int dragging = 0;
static HANDLE hDragItem = NULL;
switch (msg)
{
case WM_INITDIALOG:
{
TranslateDialogDefault(hwndDlg);
int numSlots = GetNumberOfSlots();
if (numSlots < (int) registeredExtraIcons.size())
{
char txt[512];
mir_snprintf(txt, MAX_REGS(txt), Translate("* only the first %d icons will be shown"), numSlots);
HWND label = GetDlgItem(hwndDlg, IDC_MAX_ICONS_L);
SetWindowText(label, txt);
ShowWindow(label, SW_SHOW);
}
HWND tree = GetDlgItem(hwndDlg, IDC_EXTRAORDER);
SetWindowLongPtr(tree, GWL_STYLE, GetWindowLongPtr(tree, GWL_STYLE) | TVS_NOHSCROLL);
int cx = GetSystemMetrics(SM_CXSMICON);
HIMAGELIST hImageList = ImageList_Create(cx, cx, ILC_COLOR32 | ILC_MASK, 2, 2);
HICON hDefaultIcon = (HICON) LoadImage(hInst, MAKEINTRESOURCE(IDI_EMPTY), IMAGE_ICON, cx, cx,
LR_DEFAULTCOLOR | LR_SHARED);
ImageList_AddIcon(hImageList, hDefaultIcon);
DestroyIcon(hDefaultIcon);
unsigned int i;
for (i = 0; i < registeredExtraIcons.size(); ++i)
{
ExtraIcon *extra = registeredExtraIcons[i];
HICON hIcon = IcoLib_LoadIcon(extra->getDescIcon());
if (hIcon == NULL)
{
HICON hDefaultIcon = (HICON) LoadImage(hInst, MAKEINTRESOURCE(IDI_EMPTY), IMAGE_ICON, cx, cx,
LR_DEFAULTCOLOR | LR_SHARED);
ImageList_AddIcon(hImageList, hDefaultIcon);
DestroyIcon(hDefaultIcon);
}
else
{
ImageList_AddIcon(hImageList, hIcon);
Skin_ReleaseIcon(hIcon);
}
}
TreeView_SetImageList(tree, hImageList, TVSIL_NORMAL);
for (i = 0; i < extraIconsBySlot.size(); ++i)
{
ExtraIcon *extra = extraIconsBySlot[i];
if (extra->getType() == EXTRAICON_TYPE_GROUP)
{
ExtraIconGroup *group = (ExtraIconGroup *) extra;
vector<int> ids;
for (unsigned int j = 0; j < group->items.size(); ++j)
ids.push_back(group->items[j]->getID());
Tree_AddExtraIconGroup(tree, ids, extra->isEnabled());
}
else
{
Tree_AddExtraIcon(tree, (BaseExtraIcon *) extra, extra->isEnabled());
}
}
TVSORTCB sort = { 0 };
sort.hParent = NULL;
sort.lParam = 0;
sort.lpfnCompare = CompareFunc;
TreeView_SortChildrenCB(tree, &sort, 0);
origTreeProc = (WNDPROC) SetWindowLongPtr(tree, GWLP_WNDPROC, (INT_PTR)TreeProc);
return TRUE;
}
case WM_NOTIFY:
{
LPNMHDR lpnmhdr = (LPNMHDR) lParam;
if (lpnmhdr->idFrom == 0)
{
if (lpnmhdr->code == (UINT) PSN_APPLY)
{
unsigned int i;
HWND tree = GetDlgItem(hwndDlg, IDC_EXTRAORDER);
// Store old slots
int *oldSlots = new int[registeredExtraIcons.size()];
int lastUsedSlot = -1;
for (i = 0; i < registeredExtraIcons.size(); ++i)
{
if (extraIconsByHandle[i] == registeredExtraIcons[i])
oldSlots[i] = registeredExtraIcons[i]->getSlot();
else
// Remove old slot for groups to re-set images
oldSlots[i] = -1;
lastUsedSlot = MAX(lastUsedSlot, registeredExtraIcons[i]->getSlot());
}
lastUsedSlot = MIN(lastUsedSlot, GetNumberOfSlots());
// Get user data and create new groups
vector<ExtraIconGroup *> groups;
BYTE pos = 0;
int firstEmptySlot = 0;
HTREEITEM ht = TreeView_GetRoot(tree);
TVITEM tvi = { 0 };
tvi.mask = TVIF_HANDLE | TVIF_PARAM | TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
while (ht)
{
tvi.hItem = ht;
TreeView_GetItem(tree, &tvi);
vector<int> *ids = (vector<int> *) tvi.lParam;
if (ids == NULL || ids->size() < 1)
continue; // ???
bool enabled = ((tvi.state & INDEXTOSTATEIMAGEMASK(3)) == INDEXTOSTATEIMAGEMASK(2));
int slot = (enabled ? firstEmptySlot++ : -1);
if (slot >= GetNumberOfSlots())
slot = -1;
if (ids->size() == 1)
{
BaseExtraIcon *extra = registeredExtraIcons[ids->at(0) - 1];
extra->setPosition(pos++);
extra->setSlot(slot);
}
else
{
char name[128];
mir_snprintf(name, MAX_REGS(name), "__group_%d", groups.size());
ExtraIconGroup *group = new ExtraIconGroup(name);
for (i = 0; i < ids->size(); ++i)
{
BaseExtraIcon *extra = registeredExtraIcons[ids->at(i) - 1];
extra->setPosition(pos++);
group->addExtraIcon(extra);
}
group->setSlot(slot);
groups.push_back(group);
}
ht = TreeView_GetNextSibling(tree, ht);
}
// Store data
for (i = 0; i < registeredExtraIcons.size(); ++i)
{
BaseExtraIcon *extra = registeredExtraIcons[i];
char setting[512];
mir_snprintf(setting, MAX_REGS(setting), "Position_%s", extra->getName());
DBWriteContactSettingWord(NULL, MODULE_NAME, setting, extra->getPosition());
mir_snprintf(setting, MAX_REGS(setting), "Slot_%s", extra->getName());
DBWriteContactSettingWord(NULL, MODULE_NAME, setting, extra->getSlot());
}
CallService(MS_DB_MODULE_DELETE, 0, (LPARAM) MODULE_NAME "Groups");
DBWriteContactSettingWord(NULL, MODULE_NAME "Groups", "Count", (WORD)groups.size());
for (i = 0; i < groups.size(); ++i)
{
ExtraIconGroup *group = groups[i];
char setting[512];
mir_snprintf(setting, MAX_REGS(setting), "%d_count", i);
DBWriteContactSettingWord(NULL, MODULE_NAME "Groups", setting, (WORD)group->items.size());
for (unsigned int j = 0; j < group->items.size(); ++j)
{
BaseExtraIcon *extra = group->items[j];
mir_snprintf(setting, MAX_REGS(setting), "%d_%d", i, j);
DBWriteContactSettingString(NULL, MODULE_NAME "Groups", setting, extra->getName());
}
}
// Clean removed slots
for (int j = firstEmptySlot; j <= lastUsedSlot; ++j)
RemoveExtraIcons(j);
// Apply icons to new slots
RebuildListsBasedOnGroups(groups);
for (i = 0; i < extraIconsBySlot.size(); ++i)
{
ExtraIcon *extra = extraIconsBySlot[i];
if (extra->getType() != EXTRAICON_TYPE_GROUP)
{
if (oldSlots[((BaseExtraIcon *) extra)->getID() - 1] == extra->getSlot())
continue;
}
extra->applyIcons();
}
delete[] oldSlots;
return TRUE;
}
}
else if (lpnmhdr->idFrom == IDC_EXTRAORDER)
{
HWND tree = GetDlgItem(hwndDlg, IDC_EXTRAORDER);
switch (lpnmhdr->code)
{
case TVN_BEGINDRAG:
{
SetCapture(hwndDlg);
dragging = 1;
hDragItem = ((LPNMTREEVIEWA) lParam)->itemNew.hItem;
TreeView_SelectItem(tree, hDragItem);
break;
}
case NM_CLICK:
{
DWORD pos = GetMessagePos();
TVHITTESTINFO hti;
hti.pt.x = (short) LOWORD(pos);
hti.pt.y = (short) HIWORD(pos);
ScreenToClient(lpnmhdr->hwndFrom, &hti.pt);
if (TreeView_HitTest(lpnmhdr->hwndFrom, &hti))
{
if (hti.flags & TVHT_ONITEMSTATEICON)
{
TreeView_SelectItem(tree, hti.hItem);
SendMessage(GetParent(hwndDlg), PSM_CHANGED, (WPARAM) hwndDlg, 0);
}
}
break;
}
case TVN_KEYDOWN:
{
TV_KEYDOWN *nmkd = (TV_KEYDOWN *) lpnmhdr;
if (nmkd->wVKey == VK_SPACE)
{
// Determine the selected tree item.
HTREEITEM hItem = TreeView_GetSelection(tree);
if (hItem != NULL)
SendMessage(GetParent(hwndDlg), PSM_CHANGED, (WPARAM) hwndDlg, 0);
}
break;
}
case NM_RCLICK:
{
HTREEITEM hSelected = (HTREEITEM) SendMessage(tree, TVM_GETNEXTITEM, TVGN_DROPHILITE, 0);
if (hSelected != NULL && !IsSelected(tree, hSelected))
{
UnselectAll(tree);
TreeView_SelectItem(tree, hSelected);
}
int sels = GetNumSelected(tree);
if (sels > 1)
{
if (ShowPopup(hwndDlg, 0) == ID_GROUP)
{
GroupSelectedItems(tree);
SendMessage(GetParent(hwndDlg), PSM_CHANGED, (WPARAM) hwndDlg, 0);
}
}
else if (sels == 1)
{
HTREEITEM hItem = TreeView_GetSelection(tree);
vector<int> *ids = Tree_GetIDs(tree, hItem);
if (ids->size() > 1)
{
if (ShowPopup(hwndDlg, 1) == ID_UNGROUP)
{
UngroupSelectedItems(tree);
SendMessage(GetParent(hwndDlg), PSM_CHANGED, (WPARAM) hwndDlg, 0);
}
}
}
break;
}
}
}
break;
}
case WM_MOUSEMOVE:
{
if (!dragging)
break;
HWND tree = GetDlgItem(hwndDlg, IDC_EXTRAORDER);
TVHITTESTINFO hti;
hti.pt.x = (short) LOWORD(lParam);
hti.pt.y = (short) HIWORD(lParam);
ClientToScreen(hwndDlg, &hti.pt);
ScreenToClient(tree, &hti.pt);
TreeView_HitTest(tree, &hti);
if (hti.flags & (TVHT_ONITEM | TVHT_ONITEMRIGHT))
{
HTREEITEM it = hti.hItem;
hti.pt.y -= TreeView_GetItemHeight(tree) / 2;
TreeView_HitTest(tree, &hti);
if (!(hti.flags & TVHT_ABOVE))
TreeView_SetInsertMark(tree, hti.hItem, 1);
else
TreeView_SetInsertMark(tree, it, 0);
}
else
{
if (hti.flags & TVHT_ABOVE)
SendDlgItemMessage(hwndDlg, IDC_EXTRAORDER, WM_VSCROLL, MAKEWPARAM(SB_LINEUP,0), 0);
if (hti.flags & TVHT_BELOW)
SendDlgItemMessage(hwndDlg, IDC_EXTRAORDER, WM_VSCROLL, MAKEWPARAM(SB_LINEDOWN,0), 0);
TreeView_SetInsertMark(tree, NULL, 0);
}
break;
}
case WM_LBUTTONUP:
{
if (!dragging)
break;
HWND tree = GetDlgItem(hwndDlg, IDC_EXTRAORDER);
TreeView_SetInsertMark(tree, NULL, 0);
dragging = 0;
ReleaseCapture();
TVHITTESTINFO hti;
hti.pt.x = (short) LOWORD(lParam);
hti.pt.y = (short) HIWORD(lParam);
ClientToScreen(hwndDlg, &hti.pt);
ScreenToClient(tree, &hti.pt);
hti.pt.y -= TreeView_GetItemHeight(tree) / 2;
TreeView_HitTest(tree,&hti);
if (hDragItem == hti.hItem)
break;
if (!(hti.flags & (TVHT_ONITEM | TVHT_ONITEMRIGHT | TVHT_ABOVE | TVHT_BELOW)))
break;
if (hti.flags & TVHT_ABOVE)
hti.hItem = TVI_FIRST;
else if (hti.flags & TVHT_BELOW)
hti.hItem = TVI_LAST;
TVINSERTSTRUCT tvis;
TCHAR name[512];
tvis.item.mask = TVIF_HANDLE | TVIF_PARAM | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_STATE;
tvis.item.stateMask = 0xFFFFFFFF;
tvis.item.pszText = name;
tvis.item.cchTextMax = MAX_REGS(name);
tvis.item.hItem = (HTREEITEM) hDragItem;
TreeView_GetItem(tree, &tvis.item);
TreeView_DeleteItem(tree, hDragItem);
tvis.hParent = NULL;
tvis.hInsertAfter = hti.hItem;
TreeView_SelectItem(tree, TreeView_InsertItem(tree, &tvis));
SendMessage(GetParent(hwndDlg), PSM_CHANGED, (WPARAM) hwndDlg, 0);
break;
}
case WM_DESTROY:
{
HWND tree = GetDlgItem(hwndDlg, IDC_EXTRAORDER);
HTREEITEM hItem = TreeView_GetRoot(tree);
while (hItem)
{
delete Tree_GetIDs(tree, hItem);
hItem = TreeView_GetNextSibling(tree, hItem);
}
break;
}
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
int InitOptionsCallback(WPARAM wParam, LPARAM lParam)
{
if (GetNumberOfSlots() < 1)
return 0;
OPTIONSDIALOGPAGE odp = { 0 };
odp.cbSize = sizeof(odp);
odp.hInstance = hInst;
odp.pszGroup = LPGEN("Contact List");
odp.pszTitle = LPGEN("Extra icons");
odp.pszTab = LPGEN("General");
odp.pfnDlgProc = OptionsDlgProc;
odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPTIONS);
odp.flags = ODPF_BOLDGROUPS;
Options_AddPage(wParam, &odp);
return 0;
}
void InitOptions()
{
hOptHook = HookEvent(ME_OPT_INITIALISE, InitOptionsCallback);
}
void DeInitOptions()
{
UnhookEvent(hOptHook);
}
|