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
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
/*
 * Copyright 2003, 2004, 2005 Martin Fuchs
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */


 //
 // Explorer clone
 //
 // traynotify.cpp
 //
 // Martin Fuchs, 22.08.2003
 //


#include <precomp.h>

#include "traynotify.h"

#include "../notifyhook/notifyhook.h"

NotifyHook::NotifyHook()
 :	WM_GETMODULEPATH(InstallNotifyHook())
{
}

NotifyHook::~NotifyHook()
{
	DeinstallNotifyHook();
}

void NotifyHook::GetModulePath(HWND hwnd, HWND hwndCallback)
{
	PostMessage(hwnd, WM_GETMODULEPATH, (WPARAM)hwndCallback, 0);
}

bool NotifyHook::ModulePathCopyData(LPARAM lparam, HWND* phwnd, String& path)
{
	char buffer[MAX_PATH];

	int l = GetWindowModulePathCopyData(lparam, phwnd, buffer, COUNTOF(buffer));

	if (l) {
		path.assign(buffer, l);
		return true;
	} else
		return false;
}


NotifyIconIndex::NotifyIconIndex(NOTIFYICONDATA* pnid)
{
	_hWnd = pnid->hWnd;
	_uID = pnid->uID;

	 // special handling for windows task manager
	if ((int)_uID < 0)
		_uID = 0;
}

NotifyIconIndex::NotifyIconIndex()
{
	_hWnd = 0;
	_uID = 0;
}


NotifyInfo::NotifyInfo()
{
	_idx = -1;
	_hIcon = 0;
	_dwState = 0;
	_uCallbackMessage = 0;
	_version = 0;

	_mode = NIM_AUTO;
	_lastChange = GetTickCount();
}


 // WCHAR versions von NOTIFYICONDATA
#define	NID_SIZE_W6	 sizeof(NOTIFYICONDATAW)										// _WIN32_IE = 0x600
#define	NID_SIZE_W5	(sizeof(NOTIFYICONDATAW)-sizeof(GUID))							// _WIN32_IE = 0x500
#define	NID_SIZE_W3	(sizeof(NOTIFYICONDATAW)-sizeof(GUID)-(128-64)*sizeof(WCHAR))	// _WIN32_IE < 0x500

 // CHAR versions von NOTIFYICONDATA
#define	NID_SIZE_A6	 sizeof(NOTIFYICONDATAA)
#define	NID_SIZE_A5	(sizeof(NOTIFYICONDATAA)-sizeof(GUID))
#define	NID_SIZE_A3	(sizeof(NOTIFYICONDATAA)-sizeof(GUID)-(128-64)*sizeof(CHAR))

bool NotifyInfo::modify(NOTIFYICONDATA* pnid)
{
	bool changes = false;

	if (_hWnd!=pnid->hWnd || _uID!=pnid->uID) {
		_hWnd = pnid->hWnd;
		_uID = pnid->uID;

		changes = true;
	}

	if (pnid->uFlags & NIF_MESSAGE) {
		if (_uCallbackMessage != pnid->uCallbackMessage) {
			_uCallbackMessage = pnid->uCallbackMessage;
			changes = true;
		}
	}

	if (pnid->uFlags & NIF_ICON) {
		 // Some applications destroy the icon immediatelly after completing the
		 // NIM_ADD/MODIFY message, so we have to make a copy of it.
		if (_hIcon)
			DestroyIcon(_hIcon);

		_hIcon = (HICON) CopyImage(pnid->hIcon, IMAGE_ICON, NOTIFYICON_SIZE, NOTIFYICON_SIZE, 0);

		changes = true;	///@todo compare icon
	}

	if (pnid->uFlags & NIF_STATE) {
		DWORD new_state = (_dwState&~pnid->dwStateMask) | (pnid->dwState&pnid->dwStateMask);

		if (_dwState != new_state) {
			_dwState = new_state;
			changes = true;
		}
	}

	 // store tool tip text
	if (pnid->uFlags & NIF_TIP) {
		String new_text;

		if (pnid->cbSize==NID_SIZE_W6 || pnid->cbSize==NID_SIZE_W5 || pnid->cbSize==NID_SIZE_W3) {
			 // UNICODE version of NOTIFYICONDATA structure
			LPCWSTR txt = (LPCWSTR)pnid->szTip;
			int max_len = pnid->cbSize==NID_SIZE_W3? 64: 128;

			 // get tooltip string length
			int l = 0;
			for(; l<max_len; ++l)
				if (!txt[l])
					break;

			new_text.assign(txt, l);

			if (new_text != _tipText) {
				_tipText = new_text;
				changes = true;
			}
		} else if (pnid->cbSize==NID_SIZE_A6 || pnid->cbSize==NID_SIZE_A5 || pnid->cbSize==NID_SIZE_A3) {
			LPCSTR txt = (LPCSTR)pnid->szTip;
			int max_len = pnid->cbSize==NID_SIZE_A3? 64: 128;

			int l = 0;
			for(int l=0; l<max_len; ++l)
				if (!txt[l])
					break;

			new_text.assign(txt, l);

			if (new_text != _tipText) {
				_tipText = new_text;
				changes = true;
			}
		}
	}

	TCHAR title[MAX_PATH];

	DWORD pid;
	GetWindowThreadProcessId(_hWnd, &pid);

	 // avoid to send WM_GETTEXT messages to the own process
	if (pid != GetCurrentProcessId())
		if (GetWindowText(_hWnd, title, COUNTOF(title))) {
			if (_windowTitle != title) {
				_windowTitle = title;
				changes = true;
			}
		}

	if (changes) {
		create_name();
		_lastChange = GetTickCount();
	}

	return changes;
}


NotifyArea::NotifyArea(HWND hwnd)
 :	super(hwnd),
	_tooltip(hwnd)
{
	_next_idx = 0;
	_clock_width = 0;
	_last_icon_count = 0;
	_show_hidden = false;
	_hide_inactive = true;
	_show_button = true;
}

NotifyArea::~NotifyArea()
{
	KillTimer(_hwnd, 0);

	write_config();
}

static bool get_hide_clock_from_registry()
{
	HKEY hkeyStuckRects = 0;
	DWORD buffer[10];
	DWORD len = sizeof(buffer);

	bool hide_clock = false;

	 // check if the clock should be hidden
	if (!RegOpenKey(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StuckRects2"), &hkeyStuckRects) &&
		!RegQueryValueEx(hkeyStuckRects, TEXT("Settings"), 0, NULL, (LPBYTE)buffer, &len) &&
		len==sizeof(buffer) && buffer[0]==sizeof(buffer))
		hide_clock = buffer[2] & 0x08? true: false;

	if (hkeyStuckRects)
		RegCloseKey(hkeyStuckRects);

	return hide_clock;
}

void NotifyArea::read_config()
{
	bool clock_visible = true;

	 // read notification icon settings from XML configuration
	XMLPos cfg_pos = g_Globals.get_cfg();

	if (!g_Globals._SHRestricted || !SHRestricted(REST_HIDECLOCK))
	{
		if (cfg_pos.go_down("desktopbar")) {
			clock_visible = XMLBoolRef(XMLPos(cfg_pos,"options"), "show-clock", !get_hide_clock_from_registry());
			cfg_pos.back();
		}
	}

	if (cfg_pos.go_down("notify-icons")) {
		XMLPos options(cfg_pos, "options");

		_hide_inactive = XMLBool(options, "hide-inactive", true);	///@todo read default setting from registry
		_show_hidden = XMLBool(options, "show-hidden", false);	///@todo read default setting from registry
		_show_button = XMLBool(options, "show-button", true);

		XMLChildrenFilter icons(cfg_pos, "icon");

		for(XMLChildrenFilter::iterator it=icons.begin(); it!=icons.end(); ++it) {
			const XMLNode& node = **it;

			NotifyIconConfig cfg;

			cfg._name = node.get("name").c_str();
			cfg._tipText = node.get("text").c_str();
			cfg._windowTitle = node.get("window").c_str();
			cfg._modulePath = node.get("module").c_str();
			const string& mode = node.get("show");

			if (mode == "show")
				cfg._mode = NIM_SHOW;
			else if (mode == "hide")
				cfg._mode = NIM_HIDE;
			else //if (mode == "auto")
				cfg._mode = NIM_HIDE;

			_cfg.push_back(cfg);
		}

		cfg_pos.back();
	}

	show_clock(clock_visible);
}

void NotifyArea::write_config()
{
	 // write notification icon settings to XML configuration file
	XMLPos cfg_pos = g_Globals.get_cfg();

	cfg_pos.smart_create("desktopbar");
	XMLBoolRef boolRef(XMLPos(cfg_pos,"options"), "show-clock");
    boolRef = _hwndClock!=0;
	cfg_pos.back();

	cfg_pos.smart_create("notify-icons");

	XMLPos options(cfg_pos, "options");
	XMLBoolRef(options, "hide-inactive") = _hide_inactive;
	XMLBoolRef(options, "show-hidden") = _show_hidden;
	XMLBoolRef(options, "show-button") = _show_button;

	for(NotifyIconCfgList::iterator it=_cfg.begin(); it!=_cfg.end(); ++it) {
		NotifyIconConfig& cfg = *it;

		 // search for the corresponding node using the original name
		cfg_pos.smart_create("icon", "name", cfg._name);

		 // refresh unique name
		cfg.create_name();

		cfg_pos["name"] = cfg._name.c_str();
		cfg_pos["text"] = cfg._tipText.c_str();
		cfg_pos["window"] = cfg._windowTitle.c_str();
		cfg_pos["module"] = cfg._modulePath.c_str();
		cfg_pos["show"] = string_from_mode(cfg._mode).c_str();

		cfg_pos.back();
	}

	cfg_pos.back();	// smart_create
}

void NotifyArea::show_clock(bool flag)
{
	bool vis = _hwndClock!=0;

	if (vis != flag) {
		if (flag) {
			 // create clock window
			_hwndClock = ClockWindow::Create(_hwnd);

			if (_hwndClock) {
				ClientRect clock_size(_hwndClock);
				_clock_width = clock_size.right;
			}
		} else {
			DestroyWindow(_hwndClock);
			_hwndClock = 0;
			_clock_width = 0;
		}

		SendMessage(GetParent(_hwnd), PM_RESIZE_CHILDREN, 0, 0);
	}
}

LRESULT NotifyArea::Init(LPCREATESTRUCT pcs)
{
	if (super::Init(pcs))
		return 1;

	read_config();

	SetTimer(_hwnd, 0, 1000, NULL);

	return 0;
}

HWND NotifyArea::Create(HWND hwndParent)
{
	static BtnWindowClass wcTrayNotify(CLASSNAME_TRAYNOTIFY, CS_DBLCLKS);

	ClientRect clnt(hwndParent);

	return Window::Create(WINDOW_CREATOR(NotifyArea), WS_EX_STATICEDGE,
							wcTrayNotify, TITLE_TRAYNOTIFY, WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN,
							clnt.right-(NOTIFYAREA_WIDTH_DEF+1), 1, NOTIFYAREA_WIDTH_DEF, clnt.bottom-2, hwndParent);
}

LRESULT NotifyArea::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
{
	switch(nmsg) {
	  case WM_PAINT:
		Paint();
		break;

	  case WM_TIMER: {
		Refresh();

		ClockWindow* clock_window = GET_WINDOW(ClockWindow, _hwndClock);

		if (clock_window)
			clock_window->TimerTick();
		break;}

	  case PM_REFRESH:
		Refresh(true);
		break;

	  case WM_SIZE: {
		int cx = LOWORD(lparam);
		SetWindowPos(_hwndClock, 0, cx-_clock_width, 0, 0, 0, SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
		break;}

	  case PM_GET_WIDTH: {
		int w = _sorted_icons.size()*NOTIFYICON_DIST + NOTIFYAREA_SPACE + _clock_width;
		if (_show_button)
			w += NOTIFYICON_DIST;
		return w;}

	  case PM_REFRESH_CONFIG:
		read_config();
		break;

	  case WM_CONTEXTMENU: {
		Point pt(lparam);
		POINTS p;
		p.x = (SHORT) pt.x;
		p.y = (SHORT) pt.y;
		ScreenToClient(_hwnd, &pt);

		if (IconHitTest(pt) == _sorted_icons.end()) { // display menu only when no icon clicked
			PopupMenu menu(IDM_NOTIFYAREA);
			SetMenuDefaultItem(menu, 0, MF_BYPOSITION);
			CheckMenuItem(menu, ID_SHOW_HIDDEN_ICONS, MF_BYCOMMAND|(_show_hidden?MF_CHECKED:MF_UNCHECKED));
			CheckMenuItem(menu, ID_SHOW_ICON_BUTTON, MF_BYCOMMAND|(_show_button?MF_CHECKED:MF_UNCHECKED));
			menu.TrackPopupMenu(_hwnd, p);
		}
		break;}

	  case WM_COPYDATA: {	// receive NotifyHook answers
		String path;
		HWND hwnd;

		if (_hook.ModulePathCopyData(lparam, &hwnd, path))
			_window_modules[hwnd] = path;
		break;}

	  default:
		if (nmsg>=WM_MOUSEFIRST && nmsg<=WM_MOUSELAST) {
			 // close startup menu and other popup menus
			 // This functionality is missing in MS Windows.
			if (nmsg==WM_LBUTTONDOWN || nmsg==WM_MBUTTONDOWN || nmsg==WM_RBUTTONDOWN
#ifdef WM_XBUTTONDOWN
				|| nmsg==WM_XBUTTONDOWN
#endif
				)

				CancelModes();

			Point pt(lparam);
			NotifyIconSet::const_iterator found = IconHitTest(pt);

			if (found != _sorted_icons.end()) {
				const NotifyInfo& entry = const_cast<NotifyInfo&>(*found);	// Why does GCC 3.3 need this additional const_cast ?!

				 // set activation time stamp
				if (nmsg == WM_LBUTTONDOWN ||	// Some programs need PostMessage() instead of SendMessage().
					nmsg == WM_MBUTTONDOWN ||	// So call SendMessage() only for BUTTONUP and BLCLK messages
#ifdef WM_XBUTTONDOWN
					nmsg == WM_XBUTTONDOWN ||
#endif
					nmsg == WM_RBUTTONDOWN) {
					_icon_map[entry]._lastChange = GetTickCount();
				}

				 // Notify the message if the owner is still alive
				if (IsWindow(entry._hWnd)) {
					if (nmsg == WM_MOUSEMOVE ||		// avoid to call blocking SendMessage() for merely moving the mouse over icons
						nmsg == WM_LBUTTONDOWN ||	// Some programs need PostMessage() instead of SendMessage().
						nmsg == WM_MBUTTONDOWN ||	// So call SendMessage() only for BUTTONUP and BLCLK messages
#ifdef WM_XBUTTONDOWN
						nmsg == WM_XBUTTONDOWN ||
#endif
						nmsg == WM_RBUTTONDOWN)
						PostMessage(entry._hWnd, entry._uCallbackMessage, entry._uID, nmsg);
					else {
						 // allow SetForegroundWindow() in client process
						DWORD pid;

						if (GetWindowThreadProcessId(entry._hWnd, &pid)) {
							 // bind dynamically to AllowSetForegroundWindow() to be compatible to WIN98
							static DynamicFct<BOOL(WINAPI*)(DWORD)> AllowSetForegroundWindow(TEXT("USER32"), "AllowSetForegroundWindow");

							if (AllowSetForegroundWindow)
								(*AllowSetForegroundWindow)(pid);
						}

						 // use PostMessage() for notifcation icons of Shell Service Objects in the own process
						if (pid == GetCurrentProcessId())
							PostMessage(entry._hWnd, entry._uCallbackMessage, entry._uID, nmsg);
						else
							SendMessage(entry._hWnd, entry._uCallbackMessage, entry._uID, nmsg);
					}
				}
				else if (_icon_map.erase(entry))	// delete icons without valid owner window
					UpdateIcons();
			} else
				 // handle clicks on notification area button "show hidden icons"
				if (_show_button)
					if (nmsg == WM_LBUTTONDOWN)
						if (pt.x>=NOTIFYICON_X && pt.x<NOTIFYICON_X+NOTIFYICON_SIZE &&
							pt.y>=NOTIFYICON_Y && pt.y<NOTIFYICON_Y+NOTIFYICON_SIZE)
							PostMessage(_hwnd, WM_COMMAND, MAKEWPARAM(ID_SHOW_HIDDEN_ICONS,0), 0);
		}

		return super::WndProc(nmsg, wparam, lparam);
	}

	return 0;
}

int NotifyArea::Command(int id, int code)
{
	switch(id) {
	  case ID_SHOW_HIDDEN_ICONS:
		_show_hidden = !_show_hidden;
		UpdateIcons();
		break;

	  case ID_SHOW_ICON_BUTTON:
		_show_button = !_show_button;
		UpdateIcons();
		break;

	  case ID_CONFIG_NOTIFYAREA:
		Dialog::DoModal(IDD_NOTIFYAREA, WINDOW_CREATOR(TrayNotifyDlg), GetParent(_hwnd));
		break;

	  case ID_CONFIG_TIME:
		launch_cpanel(_hwnd, TEXT("timedate.cpl"));
		break;

	  default:
		SendParent(WM_COMMAND, MAKELONG(id,code), 0);
	}

	return 0;
}

int NotifyArea::Notify(int id, NMHDR* pnmh)
{
	if (pnmh->code == TTN_GETDISPINFO) {
		LPNMTTDISPINFO pdi = (LPNMTTDISPINFO)pnmh;

		Point pt(GetMessagePos());
		ScreenToClient(_hwnd, &pt);

		if (_show_button &&
			pt.x>=NOTIFYICON_X && pt.x<NOTIFYICON_X+NOTIFYICON_SIZE &&
			pt.y>=NOTIFYICON_Y && pt.y<NOTIFYICON_Y+NOTIFYICON_SIZE)
		{
			static ResString sShowIcons(IDS_SHOW_HIDDEN_ICONS);
			static ResString sHideIcons(IDS_HIDE_ICONS);

			pdi->lpszText = (_show_hidden? sHideIcons: sShowIcons).str();
		} else {
			NotifyIconSet::iterator found = IconHitTest(pt);

			if (found != _sorted_icons.end()) {
				NotifyInfo& entry = const_cast<NotifyInfo&>(*found);	// Why does GCC 3.3 need this additional const_cast ?!

				 // enable multiline tooltips (break at CR/LF and for very long one-line strings)
				SendMessage(pnmh->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 400);

				pdi->lpszText = entry._tipText.str();
			}
		}
	}

	return 0;
}

void NotifyArea::CancelModes()
{
	PostMessage(HWND_BROADCAST, WM_CANCELMODE, 0, 0);

	for(NotifyIconSet::const_iterator it=_sorted_icons.begin(); it!=_sorted_icons.end(); ++it)
		PostMessage(it->_hWnd, WM_CANCELMODE, 0, 0);
}

LRESULT NotifyArea::ProcessTrayNotification(int notify_code, NOTIFYICONDATA* pnid)
{
	switch(notify_code) {
	  case NIM_ADD:
	  case NIM_MODIFY:
		if ((int)pnid->uID >= 0) {	///@todo This is a fix for Windows Task Manager.
			NotifyInfo& entry = _icon_map[pnid];

			 // a new entry?
			if (entry._idx == -1)
				entry._idx = ++_next_idx;
		/* equivalent code using iterator::find();
			NotifyIconMap::iterator found = _icon_map.find(pnid);
			NotifyInfo* pentry;
			 // a new entry?
			if (found == _icon_map.end()) {
				pentry = &_icon_map[pnid];
				pentry->_idx = ++_next_idx;
			} else {
				pentry = &found->second;
			}
			NotifyInfo& entry = *pentry;
		*/
			bool changes = entry.modify(pnid);

			if (DetermineHideState(entry) && entry._mode==NIM_HIDE) {
				entry._dwState |= NIS_HIDDEN;
				changes = true;
			}

			if (changes)
				UpdateIcons();	///@todo call only if really changes occurred

			return TRUE;
		}
		break;

	  case NIM_DELETE: {
		NotifyIconMap::iterator found = _icon_map.find(pnid);

		if (found != _icon_map.end()) {
			if (found->second._hIcon)
				DestroyIcon(found->second._hIcon);
			_icon_map.erase(found);
			UpdateIcons();
			return TRUE;
		}
		break;}

	  case NIM_SETFOCUS:
		SetForegroundWindow(_hwnd);
		return TRUE;

	  case NIM_SETVERSION:
		NotifyIconMap::iterator found = _icon_map.find(pnid);

		if (found != _icon_map.end()) {
			found->second._version = pnid->UNION_MEMBER(uVersion);
			return TRUE;
		} else
			return FALSE;
	}

	return FALSE;
}

void NotifyArea::UpdateIcons()
{
	_sorted_icons.clear();

	 // sort icon infos by display index
	for(NotifyIconMap::const_iterator it=_icon_map.begin(); it!=_icon_map.end(); ++it) {
		const NotifyInfo& entry = it->second;

		if (_show_hidden || !(entry._dwState & NIS_HIDDEN))
			_sorted_icons.insert(entry);
	}

	 // sync tooltip areas to current icon number
	if (_sorted_icons.size() != _last_icon_count) {
		RECT rect = {NOTIFYICON_X, NOTIFYICON_Y, NOTIFYICON_X+NOTIFYICON_SIZE, NOTIFYICON_Y+NOTIFYICON_SIZE};

		size_t tt_idx = 0;

		if (_show_button) {
			_tooltip.add(_hwnd, tt_idx++, rect);

			rect.left += NOTIFYICON_DIST;
			rect.right += NOTIFYICON_DIST;
		}

		size_t icon_cnt = _sorted_icons.size();
		while(tt_idx < icon_cnt) {
			_tooltip.add(_hwnd, tt_idx++, rect);

			rect.left += NOTIFYICON_DIST;
			rect.right += NOTIFYICON_DIST;
		}

		while(tt_idx < _last_icon_count)
			_tooltip.remove(_hwnd, tt_idx++);

		_last_icon_count = _sorted_icons.size();
	}

	SendMessage(GetParent(_hwnd), PM_RESIZE_CHILDREN, 0, 0);

	InvalidateRect(_hwnd, NULL, FALSE);	// refresh icon display
	UpdateWindow(_hwnd);
}

#ifndef _NO_ALPHABLEND
#ifdef _MSC_VER
#pragma comment(lib, "msimg32")	// for AlphaBlend()
#endif
#endif

void NotifyArea::Paint()
{
	BufferedPaintCanvas canvas(_hwnd);

	 // first fill with the background color
	FillRect(canvas, &canvas.rcPaint, GetSysColorBrush(COLOR_BTNFACE));

	 // draw icons
	int x = NOTIFYICON_X;
	int y = NOTIFYICON_Y;

	if (_show_button) {
		static SmallIcon leftArrowIcon(IDI_NOTIFY_L);
		static SmallIcon rightArrowIcon(IDI_NOTIFY_R);

		DrawIconEx(canvas, x, y, _show_hidden?rightArrowIcon:leftArrowIcon, NOTIFYICON_SIZE, NOTIFYICON_SIZE, 0, 0, DI_NORMAL);
		x += NOTIFYICON_DIST;
	}

#ifndef _NO_ALPHABLEND
	MemCanvas mem_dc;
	SelectedBitmap bmp(mem_dc, CreateCompatibleBitmap(canvas, NOTIFYICON_SIZE, NOTIFYICON_SIZE));
	RECT rect = {0, 0, NOTIFYICON_SIZE, NOTIFYICON_SIZE};
	BLENDFUNCTION blend = {AC_SRC_OVER, 0, 128, 0};	// 50 % visible
#endif

	for(NotifyIconSet::const_iterator it=_sorted_icons.begin(); it!=_sorted_icons.end(); ++it) {
#ifndef _NO_ALPHABLEND
		if (it->_dwState & NIS_HIDDEN) {
			FillRect(mem_dc, &rect, GetSysColorBrush(COLOR_BTNFACE));
			DrawIconEx(mem_dc, 0, 0, it->_hIcon, NOTIFYICON_SIZE, NOTIFYICON_SIZE, 0, 0, DI_NORMAL);
			AlphaBlend(canvas, x, y, NOTIFYICON_SIZE, NOTIFYICON_SIZE, mem_dc, 0, 0, NOTIFYICON_SIZE, NOTIFYICON_SIZE, blend);
		} else
#endif
			DrawIconEx(canvas, x, y, it->_hIcon, NOTIFYICON_SIZE, NOTIFYICON_SIZE, 0, 0, DI_NORMAL);

		x += NOTIFYICON_DIST;
	}
}

void NotifyArea::Refresh(bool update)
{
	 // Look for task icons without valid owner window.
	 // This is an extended feature missing in MS Windows.
	for(NotifyIconSet::const_iterator it=_sorted_icons.begin(); it!=_sorted_icons.end(); ++it) {
		const NotifyInfo& entry = *it;

		if (!IsWindow(entry._hWnd))
			if (_icon_map.erase(entry))	// delete icons without valid owner window
				++update;
	}

	DWORD now = GetTickCount();

	 // handle icon hiding
	for(NotifyIconMap::iterator it=_icon_map.begin(); it!=_icon_map.end(); ++it) {
		NotifyInfo& entry = it->second;

		DetermineHideState(entry);

		switch(entry._mode) {
		  case NIM_HIDE:
			if (!(entry._dwState & NIS_HIDDEN)) {
				entry._dwState |= NIS_HIDDEN;
				++update;
			}
			break;

		  case NIM_SHOW:
			if (entry._dwState&NIS_HIDDEN) {
				entry._dwState &= ~NIS_HIDDEN;
				++update;
			}
			break;

		  case NIM_AUTO:
			 // automatically hide icons after long periods of inactivity
			if (_hide_inactive)
				if (!(entry._dwState & NIS_HIDDEN))
					if (now-entry._lastChange > ICON_AUTOHIDE_SECONDS*1000) {
						entry._dwState |= NIS_HIDDEN;
						++update;
					}
			break;
		}
	}

	if (update)
		UpdateIcons();
}

 /// search for a icon at a given client coordinate position
NotifyIconSet::iterator NotifyArea::IconHitTest(const POINT& pos)
{
	if (pos.y<NOTIFYICON_Y || pos.y>=NOTIFYICON_Y+NOTIFYICON_SIZE)
		return _sorted_icons.end();

	NotifyIconSet::iterator it = _sorted_icons.begin();

	int x = NOTIFYICON_X;

	if (_show_button)
		x += NOTIFYICON_DIST;

	for(; it!=_sorted_icons.end(); ++it) {
		//NotifyInfo& entry = const_cast<NotifyInfo&>(*it);	// Why does GCC 3.3 need this additional const_cast ?!

		if (pos.x>=x && pos.x<x+NOTIFYICON_SIZE)
			break;

		x += NOTIFYICON_DIST;
	}

	return it;
}


void NotifyIconConfig::create_name()
{
	_name = FmtString(TEXT("'%s' - '%s' - '%s'"), _tipText.c_str(), _windowTitle.c_str(), _modulePath.c_str());
}


bool NotifyIconConfig::match(const NotifyIconConfig& props) const
{
	if (!_tipText.empty() && !props._tipText.empty())
		if (props._tipText == _tipText)
			return true;

	if (!_windowTitle.empty() && !props._windowTitle.empty())
		if (_tcsstr(props._windowTitle, _windowTitle))
			return true;

	if (!_modulePath.empty() && !props._modulePath.empty())
		if (!_tcsicmp(props._modulePath, _modulePath))
			return true;

	return false;
}

bool NotifyArea::DetermineHideState(NotifyInfo& entry)
{
	if (entry._modulePath.empty()) {
		const String& modulePath = _window_modules[entry._hWnd];

		 // request module path for new windows (We will get an asynchronous answer by a WM_COPYDATA message.)
		if (!modulePath.empty())
			entry._modulePath = modulePath;
		else
			_hook.GetModulePath(entry._hWnd, _hwnd);
	}

	for(NotifyIconCfgList::const_iterator it=_cfg.begin(); it!=_cfg.end(); ++it) {
		const NotifyIconConfig& cfg = *it;

		if (cfg.match(entry)) {
			entry._mode = cfg._mode;
			return true;
		}
	}

	return false;
}



String string_from_mode(NOTIFYICONMODE mode)
{
	switch(mode) {
	  case NIM_SHOW:
		return ResString(IDS_NOTIFY_SHOW);

	  case NIM_HIDE:
		return ResString(IDS_NOTIFY_HIDE);

	  default:	//case NIM_AUTO
		return ResString(IDS_NOTIFY_AUTOHIDE);
	}
}


TrayNotifyDlg::TrayNotifyDlg(HWND hwnd)
 :	super(hwnd),
	_tree_ctrl(GetDlgItem(hwnd, IDC_NOTIFY_ICONS)),
	_himl(ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), ILC_COLOR24, 3, 0)),
	_pNotifyArea(static_cast<NotifyArea*>(Window::get_window((HWND)SendMessage(g_Globals._hwndDesktopBar, PM_GET_NOTIFYAREA, 0, 0))))
{
	_selectedItem = 0;

	if (_pNotifyArea) {
		 // save original icon states and configuration data
		for(NotifyIconMap::const_iterator it=_pNotifyArea->_icon_map.begin(); it!=_pNotifyArea->_icon_map.end(); ++it)
			_icon_states_org[it->first] = IconStatePair(it->second._mode, it->second._dwState);

		_cfg_org = _pNotifyArea->_cfg;
		_show_hidden_org = _pNotifyArea->_show_hidden;
	}

	SetWindowIcon(hwnd, IDI_REACTOS);

	_haccel = LoadAccelerators(g_Globals._hInstance, MAKEINTRESOURCE(IDA_TRAYNOTIFY));

	{
	WindowCanvas canvas(_hwnd);
	HBRUSH hbkgnd = GetStockBrush(WHITE_BRUSH);

	ImageList_AddAlphaIcon(_himl, SmallIcon(IDI_DOT), hbkgnd, canvas);
	ImageList_AddAlphaIcon(_himl, SmallIcon(IDI_DOT_TRANS), hbkgnd, canvas);
	ImageList_AddAlphaIcon(_himl, SmallIcon(IDI_DOT_RED), hbkgnd, canvas);
	}

	(void)TreeView_SetImageList(_tree_ctrl, _himl, TVSIL_NORMAL);

	_resize_mgr.Add(IDC_NOTIFY_ICONS,	RESIZE);
	_resize_mgr.Add(IDC_LABEL1,			MOVE_Y);
	_resize_mgr.Add(IDC_NOTIFY_TOOLTIP,	RESIZE_X|MOVE_Y);
	_resize_mgr.Add(IDC_LABEL2,			MOVE_Y);
	_resize_mgr.Add(IDC_NOTIFY_TITLE,	RESIZE_X|MOVE_Y);
	_resize_mgr.Add(IDC_LABEL3,			MOVE_Y);
	_resize_mgr.Add(IDC_NOTIFY_MODULE,	RESIZE_X|MOVE_Y);

	_resize_mgr.Add(IDC_LABEL4,			MOVE_Y);
	_resize_mgr.Add(IDC_NOTIFY_SHOW,	MOVE_Y);
	_resize_mgr.Add(IDC_NOTIFY_HIDE,	MOVE_Y);
	_resize_mgr.Add(IDC_NOTIFY_AUTOHIDE,MOVE_Y);

	_resize_mgr.Add(IDC_PICTURE,		MOVE);
	_resize_mgr.Add(ID_SHOW_HIDDEN_ICONS,MOVE_Y);

	_resize_mgr.Add(IDC_LABEL6,			MOVE_Y);
	_resize_mgr.Add(IDC_LAST_CHANGE,	MOVE_Y);

	_resize_mgr.Add(IDOK,				MOVE);
	_resize_mgr.Add(IDCANCEL,			MOVE);

	_resize_mgr.Resize(+150, +200);

	Refresh();

	SetTimer(_hwnd, 0, 3000, NULL);
	register_pretranslate(hwnd);
}

TrayNotifyDlg::~TrayNotifyDlg()
{
	KillTimer(_hwnd, 0);
	unregister_pretranslate(_hwnd);
	ImageList_Destroy(_himl);
}

void TrayNotifyDlg::Refresh()
{
	///@todo refresh incrementally

	HiddenWindow hide(_tree_ctrl);

	TreeView_DeleteAllItems(_tree_ctrl);

	TV_INSERTSTRUCT tvi;

	tvi.hParent = 0;
	tvi.hInsertAfter = TVI_LAST;

	TV_ITEM& tv = tvi.item;
	tv.mask = TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;

	ResString str_cur(IDS_ITEMS_CUR);
	tv.pszText = str_cur.str();
	tv.iSelectedImage = tv.iImage = 0;	// IDI_DOT
	_hitemCurrent = TreeView_InsertItem(_tree_ctrl, &tvi);

	ResString str_conf(IDS_ITEMS_CONFIGURED);
	tv.pszText = str_conf.str();
	tv.iSelectedImage = tv.iImage = 2;	// IDI_DOT_RED
	_hitemConfig = TreeView_InsertItem(_tree_ctrl, &tvi);

	tvi.hParent = _hitemCurrent;

	ResString str_visible(IDS_ITEMS_VISIBLE);
	tv.pszText = str_visible.str();
	tv.iSelectedImage = tv.iImage = 0;	// IDI_DOT
	_hitemCurrent_visible = TreeView_InsertItem(_tree_ctrl, &tvi);

	ResString str_hidden(IDS_ITEMS_HIDDEN);
	tv.pszText = str_hidden.str();
	tv.iSelectedImage = tv.iImage = 1;	// IDI_DOT_TRANS
	_hitemCurrent_hidden = TreeView_InsertItem(_tree_ctrl, &tvi);

	if (_pNotifyArea) {
		_info.clear();

		tv.mask |= TVIF_PARAM;

		WindowCanvas canvas(_hwnd);

		 // insert current (visible and hidden) items
		for(NotifyIconMap::const_iterator it=_pNotifyArea->_icon_map.begin(); it!=_pNotifyArea->_icon_map.end(); ++it) {
			const NotifyInfo& entry = it->second;

			InsertItem(entry._dwState&NIS_HIDDEN? _hitemCurrent_hidden: _hitemCurrent_visible, TVI_LAST, entry, canvas);
		}

		 // insert configured items in tree view
		const NotifyIconCfgList& cfg = _pNotifyArea->_cfg;
		for(NotifyIconCfgList::const_iterator it=cfg.begin(); it!=cfg.end(); ++it) {
			const NotifyIconConfig& cfg_entry = *it;

			HICON hicon = 0;

			if (!cfg_entry._modulePath.empty()) {
				if ((int)ExtractIconEx(cfg_entry._modulePath, 0, NULL, &hicon, 1) <= 0)
					hicon = 0;

				if (!hicon) {
					SHFILEINFO sfi;

					if (SHGetFileInfo(cfg_entry._modulePath, 0, &sfi, sizeof(sfi), SHGFI_ICON|SHGFI_SMALLICON))
						hicon = sfi.hIcon;
				}
			}

			InsertItem(_hitemConfig, TVI_SORT, cfg_entry, canvas, hicon, cfg_entry._mode);

			if (hicon)
				DestroyIcon(hicon);
		}

		CheckDlgButton(_hwnd, ID_SHOW_HIDDEN_ICONS, _pNotifyArea->_show_hidden? BST_CHECKED: BST_UNCHECKED);
	}

	TreeView_Expand(_tree_ctrl, _hitemCurrent_visible, TVE_EXPAND);
	TreeView_Expand(_tree_ctrl, _hitemCurrent_hidden, TVE_EXPAND);
	TreeView_Expand(_tree_ctrl, _hitemCurrent, TVE_EXPAND);
	TreeView_Expand(_tree_ctrl, _hitemConfig, TVE_EXPAND);

	TreeView_EnsureVisible(_tree_ctrl, _hitemCurrent_visible);
}

void TrayNotifyDlg::InsertItem(HTREEITEM hparent, HTREEITEM after, const NotifyInfo& entry, HDC hdc)
{
	InsertItem(hparent, after, entry, hdc, entry._hIcon, entry._mode);
}

void TrayNotifyDlg::InsertItem(HTREEITEM hparent, HTREEITEM after, const NotifyIconDlgInfo& entry,
								HDC hdc, HICON hicon, NOTIFYICONMODE mode)
{
	int idx = _info.size() + 1;
	_info[idx] = entry;

	String mode_str = string_from_mode(mode);

	switch(mode) {
	  case NIM_SHOW:	mode_str = ResString(IDS_NOTIFY_SHOW);		break;
	  case NIM_HIDE:	mode_str = ResString(IDS_NOTIFY_HIDE);		break;
	  case NIM_AUTO:	mode_str = ResString(IDS_NOTIFY_AUTOHIDE);
	}

	FmtString txt(TEXT("%s  -  %s  [%s]"), entry._tipText.c_str(), entry._windowTitle.c_str(), mode_str.c_str());

	TV_INSERTSTRUCT tvi;

	tvi.hParent = hparent;
	tvi.hInsertAfter = after;

	TV_ITEM& tv = tvi.item;
	tv.mask = TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_PARAM;

	tv.lParam = (LPARAM)idx;
	tv.pszText = txt.str();
	tv.iSelectedImage = tv.iImage = ImageList_AddAlphaIcon(_himl, hicon, GetStockBrush(WHITE_BRUSH), hdc);
	(void)TreeView_InsertItem(_tree_ctrl, &tvi);
}

LRESULT TrayNotifyDlg::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
{
	switch(nmsg) {
	  case PM_TRANSLATE_MSG: {
		MSG* pmsg = (MSG*) lparam;

		if (TranslateAccelerator(_hwnd, _haccel, pmsg))
			return TRUE;

		return FALSE;}

	  case WM_TIMER:
		Refresh();
		break;

	  default:
		return super::WndProc(nmsg, wparam, lparam);
	}

	return 0;
}

int TrayNotifyDlg::Command(int id, int code)
{
	if (code == BN_CLICKED) {
		switch(id) {
		  case ID_REFRESH:
			Refresh();
			break;

		  case IDC_NOTIFY_SHOW:
			SetIconMode(NIM_SHOW);
			break;

		  case IDC_NOTIFY_HIDE:
			SetIconMode(NIM_HIDE);
			break;

		  case IDC_NOTIFY_AUTOHIDE:
			SetIconMode(NIM_AUTO);
			break;

		  case ID_SHOW_HIDDEN_ICONS:
			if (_pNotifyArea)
				SendMessage(*_pNotifyArea, WM_COMMAND, MAKEWPARAM(id,code), 0);
			break;

		  case IDOK:
			EndDialog(_hwnd, id);
			break;

		  case IDCANCEL:
			 // rollback changes
			if (_pNotifyArea) {
				 // restore original icon states and configuration data
				_pNotifyArea->_cfg = _cfg_org;
				_pNotifyArea->_show_hidden = _show_hidden_org;

				for(IconStateMap::const_iterator it=_icon_states_org.begin(); it!=_icon_states_org.end(); ++it) {
					NotifyInfo& info = _pNotifyArea->_icon_map[it->first];

					info._mode = it->second.first;
					info._dwState = it->second.second;
				}

				SendMessage(*_pNotifyArea, PM_REFRESH, 0, 0);
			}

			EndDialog(_hwnd, id);
			break;
		}

		return 0;
	}

	return 1;
}

int TrayNotifyDlg::Notify(int id, NMHDR* pnmh)
{
	switch(pnmh->code) {
	  case TVN_SELCHANGED: {
		NMTREEVIEW* pnmtv = (NMTREEVIEW*)pnmh;
		int idx = pnmtv->itemNew.lParam;

		if (idx) {
			RefreshProperties(_info[idx]);
			_selectedItem = pnmtv->itemNew.hItem;
		} else {
			/*
			SetDlgItemText(_hwnd, IDC_NOTIFY_TOOLTIP, NULL);
			SetDlgItemText(_hwnd, IDC_NOTIFY_TITLE, NULL);
			SetDlgItemText(_hwnd, IDC_NOTIFY_MODULE, NULL);
			*/
			CheckRadioButton(_hwnd, IDC_NOTIFY_SHOW, IDC_NOTIFY_AUTOHIDE, 0);
		}
		break;}
	}

	return 0;
}

void TrayNotifyDlg::RefreshProperties(const NotifyIconDlgInfo& entry)
{
	SetDlgItemText(_hwnd, IDC_NOTIFY_TOOLTIP, entry._tipText);
	SetDlgItemText(_hwnd, IDC_NOTIFY_TITLE, entry._windowTitle);
	SetDlgItemText(_hwnd, IDC_NOTIFY_MODULE, entry._modulePath);

	CheckRadioButton(_hwnd, IDC_NOTIFY_SHOW, IDC_NOTIFY_AUTOHIDE, IDC_NOTIFY_SHOW+entry._mode);

	String change_str;
	if (entry._lastChange)
		change_str.printf(TEXT("before %d s"), (GetTickCount()-entry._lastChange+500)/1000);
	SetDlgItemText(_hwnd, IDC_LAST_CHANGE, change_str);

	HICON hicon = 0; //get_window_icon_big(entry._hWnd, false);<--- Assignment 'hicon=0', assigned value is 0

	 // If we could not find an icon associated with the owner window, try to load one from the owning module.
	if (!hicon && !entry._modulePath.empty()) {<--- Condition '!hicon' is always true
		hicon = ExtractIcon(g_Globals._hInstance, entry._modulePath, 0);

		if (!hicon) {
			SHFILEINFO sfi;

			if (SHGetFileInfo(entry._modulePath, 0, &sfi, sizeof(sfi), SHGFI_ICON|SHGFI_LARGEICON))
				hicon = sfi.hIcon;
		}
	}

	if (hicon) {
		SendMessage(GetDlgItem(_hwnd, IDC_PICTURE), STM_SETICON, (LPARAM)hicon, 0);
		DestroyIcon(hicon);
	} else
		SendMessage(GetDlgItem(_hwnd, IDC_PICTURE), STM_SETICON, 0, 0);
}

void TrayNotifyDlg::SetIconMode(NOTIFYICONMODE mode)
{
	int idx = TreeView_GetItemData(_tree_ctrl, _selectedItem);

	if (!idx)
		return;

	NotifyIconConfig& entry = _info[idx];

	if (entry._mode != mode) {
		entry._mode = mode;

		 // trigger refresh in notify area and this dialog
		if (_pNotifyArea)
			SendMessage(*_pNotifyArea, PM_REFRESH, 0, 0);
	}

	if (_pNotifyArea) {
		bool found = false;

		NotifyIconCfgList& cfg = _pNotifyArea->_cfg;
		for(NotifyIconCfgList::iterator it=cfg.begin(); it!=cfg.end(); ++it) {
			NotifyIconConfig& cfg_entry = *it;

			if (cfg_entry.match(entry)) {
				cfg_entry._mode = mode;
				++found;
				break;
			}
		}

		if (!found) {
			 // insert new configuration entry
			NotifyIconConfig cfg_entry = entry;

			cfg_entry._mode = mode;

			_pNotifyArea->_cfg.push_back(cfg_entry);
		}
	}

	Refresh();
	///@todo select treeview item at new position in tree view -> refresh HTREEITEM in _selectedItem
}


ClockWindow::ClockWindow(HWND hwnd)
 :	super(hwnd),
	_tooltip(hwnd)
{
	*_time = TEXT('\0');
	FormatTime();

	_tooltip.add(_hwnd, _hwnd);
}

HWND ClockWindow::Create(HWND hwndParent)
{
	static BtnWindowClass wcClock(CLASSNAME_CLOCKWINDOW, CS_DBLCLKS);

	ClientRect clnt(hwndParent);

	WindowCanvas canvas(hwndParent);
	FontSelection font(canvas, GetStockFont(ANSI_VAR_FONT));

	RECT rect = {0, 0, 0, 0};
	TCHAR buffer[16];
	// Arbitrary high time so that the created clock window is big enough
	SYSTEMTIME st = { 1601, 1, 0, 1, 23, 59, 59, 999 };

	if (!GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, NULL, buffer, sizeof(buffer)/sizeof(TCHAR)))
		_tcscpy(buffer, TEXT("00:00"));

	// Calculate the rectangle needed to draw the time (without actually drawing it)
	DrawText(canvas, buffer, -1, &rect, DT_SINGLELINE|DT_NOPREFIX|DT_CALCRECT);
	int clockwindowWidth = rect.right-rect.left + 4;

	return Window::Create(WINDOW_CREATOR(ClockWindow), 0,
							wcClock, NULL, WS_CHILD|WS_VISIBLE,
							clnt.right-(clockwindowWidth), 1, clockwindowWidth, clnt.bottom-2, hwndParent);
}

LRESULT ClockWindow::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam)
{
	switch(nmsg) {
	  case WM_PAINT:
		Paint();
		break;

	  case WM_LBUTTONDBLCLK:
		launch_cpanel(_hwnd, TEXT("timedate.cpl"));
		break;

	  default:
		return super::WndProc(nmsg, wparam, lparam);
	}

	return 0;
}

int ClockWindow::Notify(int id, NMHDR* pnmh)
{
	if (pnmh->code == TTN_GETDISPINFO) {
		LPNMTTDISPINFO pdi = (LPNMTTDISPINFO)pnmh;

		SYSTEMTIME systime;
		TCHAR buffer[64];

		GetLocalTime(&systime);

		if (GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &systime, NULL, buffer, 64))
			_tcscpy(pdi->szText, buffer);
		else
			pdi->szText[0] = '\0';
	}

	return 0;
}

void ClockWindow::TimerTick()
{
	if (FormatTime())
		InvalidateRect(_hwnd, NULL, TRUE);	// refresh displayed time
}

bool ClockWindow::FormatTime()
{
	TCHAR buffer[16];

	if (GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, NULL, NULL, buffer, sizeof(buffer)/sizeof(TCHAR)))
		if (_tcscmp(buffer, _time)) {
			_tcscpy(_time, buffer);
			return true;	// The text to display has changed.
		}

	return false;	// no change
}

void ClockWindow::Paint()
{
	PaintCanvas canvas(_hwnd);

	FillRect(canvas, &canvas.rcPaint, GetSysColorBrush(COLOR_BTNFACE));

	BkMode bkmode(canvas, TRANSPARENT);
	FontSelection font(canvas, GetStockFont(ANSI_VAR_FONT));

	DrawText(canvas, _time, -1, ClientRect(_hwnd), DT_SINGLELINE|DT_VCENTER|DT_NOPREFIX);
}