Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[c8d76d]: src / ui / Windows / MainMenu.cpp Maximize Restore History

Download this file

MainMenu.cpp    1472 lines (1248 with data), 53.1 kB

   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
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
/*
* Copyright (c) 2003-2014 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
// file MainMenu.cpp
// Menu-related methods of DboxMain
//-----------------------------------------------------------------------------
#include "PasswordSafe.h"
#include "ThisMfcApp.h"
#include "DboxMain.h"
#include "HKModifiers.h"
#include "core/PWSprefs.h"
#include "resource.h"
#include "resource2.h" // Menu, Toolbar & Accelerator resources
#include "resource3.h" // String resources
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// Functor for count_if
struct CountShortcuts {
bool operator()(const std::pair<const UINT, CMenuShortcut> &p) {
return (p.second.siVirtKey != 0);
}
};
// Functor for for_each
struct CreateAccelTable {
CreateAccelTable(ACCEL *pacceltbl, unsigned char ucAutotypeKey)
: m_pacceltbl(pacceltbl), m_ucAutotypeKey(ucAutotypeKey) {}
void operator()(const std::pair<UINT, CMenuShortcut> &p)
{
if (p.second.siVirtKey != 0 && p.second.siVirtKey != m_ucAutotypeKey) {
m_pacceltbl->fVirt = FVIRTKEY |
((p.second.cModifier & HOTKEYF_CONTROL) == HOTKEYF_CONTROL ? FCONTROL : 0) |
((p.second.cModifier & HOTKEYF_ALT) == HOTKEYF_ALT ? FALT : 0) |
((p.second.cModifier & HOTKEYF_SHIFT) == HOTKEYF_SHIFT ? FSHIFT : 0);
m_pacceltbl->key = (WORD)p.second.siVirtKey;
m_pacceltbl->cmd = (WORD)p.first;
m_pacceltbl++;
}
}
private:
ACCEL *m_pacceltbl;
unsigned char m_ucAutotypeKey; // AutoType shortcut key
};
static bool IsExtended(int code)
{
switch (code) {
case VK_PRIOR: // Page Up
case VK_NEXT: // Page Down
case VK_END: // End
case VK_HOME: // Home
case VK_LEFT: // Left arrow cursor
case VK_UP: // Up arrow cursor
case VK_RIGHT: // Right arrow cursor
case VK_DOWN: // Down arrow cursor
case VK_SNAPSHOT: // Print Screen
case VK_INSERT: // Insert
case VK_DELETE: // Delete
case VK_LWIN: // Left Windows Key
case VK_RWIN: // Right Windows Key
case VK_APPS: // Windows Application Key
case VK_DIVIDE: // Numeric pad '/'
case VK_NUMLOCK: // Numlock
return true;
default:
return false;
}
}
void InsertShortcuts(CMenu *pMenu, MapMenuShortcuts &mms,
const unsigned int parentID)
{
// if parentID == 0, we're processing a toplevel menu,
// else, we're passed the menu in which the desired
// submenu resides.
BOOL brc;
wchar_t tcMenuString[_MAX_PATH + 1];
CMenuShortcut mst;
MENUITEMINFO miteminfo;
CMenu *pSCTMenu = pMenu;
ASSERT_VALID(pSCTMenu);
static int iMenuPos(0);
if (parentID != 0) {
int isubmenu_pos = app.FindMenuItem(pSCTMenu, parentID);
ASSERT(isubmenu_pos != -1);
pSCTMenu = pSCTMenu->GetSubMenu(isubmenu_pos);
ASSERT_VALID(pSCTMenu);
}
mst.uiParentID = parentID;
UINT uiCount = pSCTMenu->GetMenuItemCount();
ASSERT((int)uiCount >= 0);
for (UINT ui = 0; ui < uiCount; ui++) {
SecureZeroMemory(tcMenuString, sizeof(tcMenuString));
SecureZeroMemory(&miteminfo, sizeof(miteminfo));
miteminfo.cbSize = sizeof(miteminfo);
miteminfo.fMask = MIIM_ID | MIIM_STRING;
miteminfo.cch = _MAX_PATH;
miteminfo.dwTypeData = tcMenuString;
brc = pSCTMenu->GetMenuItemInfo(ui, &miteminfo, TRUE);
ASSERT(brc != 0);
if (miteminfo.wID >= 1) {
std::pair< MapMenuShortcutsIter, bool > pr;
mst.name = tcMenuString;
mst.iMenuPosition = iMenuPos;
pr = mms.insert(MapMenuShortcutsPair(miteminfo.wID, mst));
ASSERT(pr.second == true);
iMenuPos++;
}
}
}
void DboxMain::SetUpInitialMenuStrings()
{
CMenu *pMainMenu, *pSubMenu;
int isubmenu_pos;
UINT uiCount;
CString sKeyName;
BOOL brc;
// Following are excluded from list of user-configurable
// shortcuts
UINT excludedMenuItems[] = {
// Add user Excluded Menu Items - anything that is a Popup Menu
ID_FILEMENU, ID_EXPORTMENU, ID_IMPORTMENU, ID_EDITMENU,
ID_VIEWMENU, ID_FILTERMENU, ID_CHANGEFONTMENU, ID_REPORTSMENU,
ID_MANAGEMENU, ID_LANGUAGEMENU, ID_HELPMENU, ID_FINDMENU, ID_EXPORTENTMENU,
// Plus Exit (2 shortcuts Ctrl+Q and Alt+F4) and Help (F1)
ID_MENUITEM_EXIT, ID_HELP,
// Plus Expand/Collapse group - function of TreeCtrl and is
// the Enter key, which cannot be assigned via the Shortcut
// Property Page
ID_MENUITEM_GROUPENTER,
// The following are only in the Menu to get the correct string
// for the action. Add here to stop them being in the Shortcut
// Options CListCtrl.
// User can alter the function using the base values
// ID_MENUITEM_EDIT, ID_MENUITEM_DELETE & ID_MENUITEM_RENAME
ID_MENUITEM_VIEW,
ID_MENUITEM_DELETEENTRY, ID_MENUITEM_DELETEGROUP,
ID_MENUITEM_RENAMEENTRY, ID_MENUITEM_RENAMEGROUP,
};
m_ExcludedMenuItems.clear();
m_ExcludedMenuItems.assign(excludedMenuItems,
excludedMenuItems + _countof(excludedMenuItems));
pMainMenu = new CMenu;
brc = pMainMenu->LoadMenu(IDR_MAINMENU);
ASSERT(brc != 0);
wchar_t tcMenuString[_MAX_PATH + 1];
MENUITEMINFO miteminfo = {0};
uiCount = pMainMenu->GetMenuItemCount();
ASSERT((int)uiCount >= 0);
// Add reserved shortcuts = Alt+'x' for main menu e.g. Alt+F, E, V, M, H
// Maybe different in other languages
m_ReservedShortcuts.clear();
st_MenuShortcut st_mst;
st_mst.siVirtKey;
st_mst.cModifier = HOTKEYF_ALT;
for (UINT ui = 0; ui < uiCount; ui++) {
SecureZeroMemory(tcMenuString, sizeof(tcMenuString));
SecureZeroMemory(&miteminfo, sizeof(miteminfo));
miteminfo.cbSize = sizeof(miteminfo);
miteminfo.fMask = MIIM_ID | MIIM_STRING;
miteminfo.dwTypeData = tcMenuString;
miteminfo.cch = _MAX_PATH;
brc = pMainMenu->GetMenuItemInfo(ui, &miteminfo, TRUE);
ASSERT(brc != 0);
if (miteminfo.wID >= 1) {
CString csMainMenuItem = tcMenuString;
int iamp = csMainMenuItem.Find(L'&');
if (iamp >= 0 && iamp < csMainMenuItem.GetLength() - 1) {
st_mst.nControlID = miteminfo.wID;
st_mst.siVirtKey = csMainMenuItem[iamp + 1];
m_ReservedShortcuts.push_back(st_mst);
}
}
}
// Add 3 special keys F1 (for Help), Ctrl+Q/Alt+F4 (Exit)
st_mst.nControlID = ID_MENUITEM_HELP;
st_mst.siVirtKey = VK_F1;
st_mst.cModifier = 0;
m_ReservedShortcuts.push_back(st_mst);
st_mst.nControlID = ID_MENUITEM_EXIT;
st_mst.siVirtKey = 'Q';
st_mst.cModifier = HOTKEYF_CONTROL;
m_ReservedShortcuts.push_back(st_mst);
st_mst.nControlID = ID_MENUITEM_EXIT;
st_mst.siVirtKey = VK_F4;
st_mst.cModifier = HOTKEYF_ALT;
m_ReservedShortcuts.push_back(st_mst);
// Now get all other Menu items
// (ronys) I think we can do the following properly via recursive descent. Later.
m_MapMenuShortcuts.clear();
// Do Main Menu (uiParentID == 0)
InsertShortcuts(pMainMenu, m_MapMenuShortcuts, 0);
// Do File Menu
InsertShortcuts(pMainMenu, m_MapMenuShortcuts, ID_FILEMENU);
isubmenu_pos = app.FindMenuItem(pMainMenu, ID_FILEMENU);
ASSERT(isubmenu_pos != -1);
pSubMenu = pMainMenu->GetSubMenu(isubmenu_pos);
// Do File Menu Export submenu
InsertShortcuts(pSubMenu, m_MapMenuShortcuts, ID_EXPORTMENU);
// Do File Menu Import submenu
InsertShortcuts(pSubMenu, m_MapMenuShortcuts, ID_IMPORTMENU);
// Do Edit Menu
InsertShortcuts(pMainMenu, m_MapMenuShortcuts, ID_EDITMENU);
// Do Export Entry submenu
// InsertShortcuts(pSubMenu, m_MapMenuShortcuts, ID_EXPORTENTMENU);
// Do View Menu
InsertShortcuts(pMainMenu, m_MapMenuShortcuts, ID_VIEWMENU);
isubmenu_pos = app.FindMenuItem(pMainMenu, ID_VIEWMENU);
ASSERT(isubmenu_pos != -1);
pSubMenu = pMainMenu->GetSubMenu(isubmenu_pos);
// Do View Menu Filter submenu
InsertShortcuts(pSubMenu, m_MapMenuShortcuts, ID_FILTERMENU);
// Do View Menu ChangeFont submenu
InsertShortcuts(pSubMenu, m_MapMenuShortcuts, ID_CHANGEFONTMENU);
// Do View Menu Reports submenu
InsertShortcuts(pSubMenu, m_MapMenuShortcuts, ID_REPORTSMENU);
// Do Manage Menu
InsertShortcuts(pMainMenu, m_MapMenuShortcuts, ID_MANAGEMENU);
// No need to do Manage Menu Langauges submenu as rebuilt every time!
// Do Help Menu
InsertShortcuts(pMainMenu, m_MapMenuShortcuts, ID_HELPMENU);
// Don't need main menu again here
brc = pMainMenu->DestroyMenu();
ASSERT(brc != 0);
// Do Find toolbar menu items not on a menu!
brc = pMainMenu->LoadMenu(IDR_POPFIND);
ASSERT(brc != 0);
// Again a parent menu (uiParentID == 0)
InsertShortcuts(pMainMenu, m_MapMenuShortcuts, 0);
InsertShortcuts(pMainMenu, m_MapMenuShortcuts, ID_FINDMENU);
// No longer need any menus
brc = pMainMenu->DestroyMenu();
ASSERT(brc != 0);
delete pMainMenu;
// Now that we have all menu strings - go get current accelerator strings
// and update Map
MapMenuShortcutsIter iter, iter_entry, inuse_iter;
HACCEL curacctbl = app.m_ghAccelTable;
//if (curacctbl != NULL) {
// DestroyAcceleratorTable(app.m_ghAccelTable);
// curacctbl = app.m_ghAccelTable = NULL;
//}
ACCEL *pacceltbl(NULL), *paccel(NULL);
int numaccels;
// Just in case user deletes all shortcuts!
if (app.m_ghAccelTable != NULL) {
numaccels = CopyAcceleratorTable(curacctbl, NULL, 0);
pacceltbl = (LPACCEL)LocalAlloc(LPTR, numaccels * sizeof(ACCEL));
ASSERT(pacceltbl != NULL);
CopyAcceleratorTable(curacctbl, pacceltbl, numaccels);
// Load our map with all the current info
paccel = pacceltbl;
for (int i = 0; i < numaccels; i++) {
/*
FALT - The ALT key must be held down when the accelerator key is pressed.
FCONTROL - The CTRL key must be held down when the accelerator key is pressed.
FSHIFT - The SHIFT key must be held down when the accelerator key is pressed.
FVIRTKEY - The key member specifies a virtual-key code. If this flag is not specified,
key is assumed to specify a character code.
*/
iter = m_MapMenuShortcuts.find((UINT)paccel->cmd);
if (iter != m_MapMenuShortcuts.end()) {
iter->second.siDefVirtKey = iter->second.siVirtKey =
(unsigned char)paccel->key;
iter->second.cDefModifier = iter->second.cModifier =
((paccel->fVirt & FCONTROL) == FCONTROL ? HOTKEYF_CONTROL : 0) |
((paccel->fVirt & FALT) == FALT ? HOTKEYF_ALT : 0) |
((paccel->fVirt & FSHIFT) == FSHIFT ? HOTKEYF_SHIFT : 0) |
(IsExtended((int)paccel->key) ? HOTKEYF_EXT : 0);
}
paccel++;
}
// Don't need copy any more
LocalFree(pacceltbl);
pacceltbl = paccel = NULL;
}
// Now go through the default table and see if the user has changed anything,
// change shortcuts as per preferences
std::vector<st_prefShortcut> vShortcuts(PWSprefs::GetInstance()->GetPrefShortcuts());
// We need to convert from PWS to Hotkey modifiers
for (size_t i = 0; i < vShortcuts.size(); i++) {
WORD wPWSModifiers = vShortcuts[i].cModifier;
// Translate from CHotKeyCtrl to PWS modifiers
WORD wHKModifiers = ConvertModifersPWS2MFC(wPWSModifiers);
vShortcuts[i].cModifier = (unsigned char)wHKModifiers;
}
size_t N = vShortcuts.size();
for (size_t i = 0; i < N; i++) {
const st_prefShortcut &stxst = vShortcuts[i];
// User should not have these sub-entries in their config file
if (stxst.id == ID_MENUITEM_GROUPENTER ||
stxst.id == ID_MENUITEM_VIEW ||
stxst.id == ID_MENUITEM_DELETEENTRY ||
stxst.id == ID_MENUITEM_DELETEGROUP ||
stxst.id == ID_MENUITEM_RENAME ||
stxst.id == ID_MENUITEM_RENAMEENTRY ||
stxst.id == ID_MENUITEM_RENAMEGROUP) {
continue;
}
iter = m_MapMenuShortcuts.find(stxst.id);
if (iter == m_MapMenuShortcuts.end()) {
// Unknown Control ID - ignore - maybe used by a later version of PWS
continue;
}
// Check not already in use (ignore if deleting current shortcut)
if (stxst.siVirtKey != 0) {
st_mst.siVirtKey = stxst.siVirtKey;
st_mst.cModifier = stxst.cModifier;
already_inuse inuse(st_mst);
inuse_iter = std::find_if(m_MapMenuShortcuts.begin(),
m_MapMenuShortcuts.end(),
inuse);
if (inuse_iter != m_MapMenuShortcuts.end() &&
inuse_iter->first != iter->first) {
// Shortcut in use - ignore entry - duplicates not allowed!
continue;
}
}
if ((iter->second.siVirtKey != stxst.siVirtKey ||
iter->second.cModifier != stxst.cModifier)) {
// User changed a added or shortcut
iter->second.siVirtKey = stxst.siVirtKey;
iter->second.cModifier = stxst.cModifier;
}
}
// Set up the shortcuts based on the main entry
// for View, Delete and Rename
iter = m_MapMenuShortcuts.find(ID_MENUITEM_EDIT);
ASSERT(iter != m_MapMenuShortcuts.end());
iter_entry = m_MapMenuShortcuts.find(ID_MENUITEM_VIEW);
ASSERT(iter_entry != m_MapMenuShortcuts.end());
iter_entry->second.SetKeyFlags(iter->second);
SetupSpecialShortcuts();
UpdateAccelTable();
}
void DboxMain::UpdateAccelTable()
{
ACCEL *pacceltbl, *pxatbl;
int numscs(0);
CountShortcuts cntscs;
// Add on space of 3 reserved shortcuts (Ctrl-Q, F4, F1)
numscs = (int)std::count_if(m_MapMenuShortcuts.begin(), m_MapMenuShortcuts.end(),
cntscs) + 3;
// But take off 1 if there is a shprtcut for AutoType
if (m_wpAutotypeKey != 0)
numscs--;
// Get new table space
pacceltbl = (LPACCEL)LocalAlloc(LPTR, numscs * sizeof(ACCEL));
ASSERT(pacceltbl != NULL);
// Populate it
CreateAccelTable create_accel_table(pacceltbl, (unsigned char)m_wpAutotypeKey);
for_each(m_MapMenuShortcuts.begin(), m_MapMenuShortcuts.end(), create_accel_table);
// Add back in the 3 reserved
pxatbl = pacceltbl + (numscs - 3);
pxatbl->fVirt = FVIRTKEY | FCONTROL;
pxatbl->key = (WORD)'Q';
pxatbl->cmd = (WORD)ID_MENUITEM_EXIT;
pxatbl++;
pxatbl->fVirt = FVIRTKEY | FALT;
pxatbl->key = (WORD)VK_F4;
pxatbl->cmd = (WORD)ID_MENUITEM_EXIT;
pxatbl++;
pxatbl->fVirt = FVIRTKEY;
pxatbl->key = (WORD)VK_F1;
pxatbl->cmd = (WORD)ID_HELP;
// Replace current one
DestroyAcceleratorTable(app.m_ghAccelTable);
app.m_ghAccelTable = CreateAcceleratorTable(pacceltbl, numscs);
app.SetACCELTableCreated();
}
void DboxMain::SetUpMenuStrings(CMenu *pPopupMenu)
{
// Can't use GetMenuItemID, as it does not understand that with the MENUEX
// format, Popup menus can have IDs
ASSERT_VALID(pPopupMenu);
BOOL brc;
MENUITEMINFO miteminfo = {0};
MapMenuShortcutsIter iter;
UINT uiCount = pPopupMenu->GetMenuItemCount();
ASSERT((int)uiCount >= 0);
for (UINT ui = 0; ui < uiCount; ui++) {
SecureZeroMemory(&miteminfo, sizeof(miteminfo));
miteminfo.cbSize = sizeof(miteminfo);
miteminfo.fMask = MIIM_ID | MIIM_STATE;
brc = pPopupMenu->GetMenuItemInfo(ui, &miteminfo, TRUE);
ASSERT(brc != 0);
// Exit & Help never changed and their shortcuts are in the menu text
if (miteminfo.wID >= 1 &&
miteminfo.wID != ID_MENUITEM_EXIT && miteminfo.wID != ID_HELP) {
iter = m_MapMenuShortcuts.find(miteminfo.wID);
if (iter != m_MapMenuShortcuts.end()) {
CString str;
if (iter->second.siVirtKey != 0) {
str.Format(L"%s\t%s", iter->second.name.c_str(),
CMenuShortcut::FormatShortcut(iter));
} else {
str = iter->second.name.c_str();
}
pPopupMenu->ModifyMenu(miteminfo.wID, MF_BYCOMMAND | miteminfo.fState, miteminfo.wID, str);
}
}
}
}
void DboxMain::CustomiseMenu(CMenu *pPopupMenu, const UINT uiMenuID,
const bool bDoShortcuts)
{
// Original OnInitMenu code
// All main menus are POPUPs (see PasswordSafe2.rc2)
// This routine changes the text in the menu via "ModifyMenu" and
// adds the "check" mark via CheckMenuRadioItem for view type and toolbar.
// It then tailors the Edit menu depending on whether a Group or Entry is selected.
// "EnableMenuItem" is handled by the UPDATE_UI routines
bool bGroupSelected(false);
const bool bTreeView = m_ctlItemTree.IsWindowVisible() == TRUE;
const bool bItemSelected = (SelItemOk() == TRUE);
const bool bReadOnly = m_core.IsReadOnly();
const CItemData *pci(NULL);
const wchar_t *tc_dummy = L" ";
ASSERT_VALID(pPopupMenu);
// Except for Edit & View menus, we only get here if the Shortcuts have changed and
// we need to rebuild menu item text
if (bDoShortcuts)
SetUpMenuStrings(pPopupMenu);
// Change the 'Change Mode' text as appropriate
if (uiMenuID == ID_FILEMENU) {
pPopupMenu->ModifyMenu(ID_MENUITEM_CHANGEMODE, MF_BYCOMMAND,
ID_MENUITEM_CHANGEMODE,
bReadOnly ? CS_READWRITE : CS_READONLY);
return;
}
// We have done for all except the Edit and View menus
if (uiMenuID != ID_EDITMENU && uiMenuID != ID_VIEWMENU && uiMenuID != ID_FILTERMENU)
return;
// If View menu selected (contains 'Flattened &List' menu item)
if (uiMenuID == ID_VIEWMENU) {
// Delete Show Find Toolbar menu item - don't know if there or previously deleted
pPopupMenu->RemoveMenu(ID_MENUITEM_SHOWFINDTOOLBAR, MF_BYCOMMAND);
if (!m_FindToolBar.IsVisible()) {
// Put it back if not visible - before the separator
int pos = app.FindMenuItem(pPopupMenu, ID_MENUITEM_SHOWHIDE_DRAGBAR);
pPopupMenu->InsertMenu(pos + 1,
MF_BYPOSITION | MF_ENABLED | MF_STRING,
ID_MENUITEM_SHOWFINDTOOLBAR, tc_dummy);
}
pPopupMenu->CheckMenuRadioItem(ID_MENUITEM_LIST_VIEW, ID_MENUITEM_TREE_VIEW,
bTreeView ? ID_MENUITEM_TREE_VIEW : ID_MENUITEM_LIST_VIEW,
MF_BYCOMMAND);
pPopupMenu->CheckMenuItem(ID_MENUITEM_SHOWHIDE_TOOLBAR, MF_BYCOMMAND |
m_MainToolBar.IsWindowVisible() ? MF_CHECKED : MF_UNCHECKED);
bool bDragBarState = PWSprefs::GetInstance()->GetPref(PWSprefs::ShowDragbar);
pPopupMenu->CheckMenuItem(ID_MENUITEM_SHOWHIDE_DRAGBAR, MF_BYCOMMAND |
bDragBarState ? MF_CHECKED : MF_UNCHECKED);
pPopupMenu->CheckMenuItem(ID_MENUITEM_SHOWHIDE_UNSAVED, MF_BYCOMMAND |
m_bUnsavedDisplayed ? MF_CHECKED : MF_UNCHECKED);
pPopupMenu->CheckMenuItem(ID_MENUITEM_SHOW_ALL_EXPIRY, MF_BYCOMMAND |
m_bExpireDisplayed ? MF_CHECKED : MF_UNCHECKED);
// Don't show filter menu if "internal" menu active
pPopupMenu->EnableMenuItem(ID_FILTERMENU, MF_BYCOMMAND |
(m_bUnsavedDisplayed || m_bExpireDisplayed) ? MF_GRAYED : MF_ENABLED);
pPopupMenu->CheckMenuRadioItem(ID_MENUITEM_NEW_TOOLBAR,
ID_MENUITEM_OLD_TOOLBAR,
m_toolbarMode, MF_BYCOMMAND);
// Disable View Reports menu if no database open as we need the full path
// of the database to get to its reports!
pPopupMenu->EnableMenuItem(ID_REPORTSMENU, MF_BYCOMMAND | (m_bOpen ? MF_ENABLED : MF_GRAYED));
goto exit;
} // View menu
if (uiMenuID == ID_FILTERMENU) {
pPopupMenu->ModifyMenu(1, MF_BYPOSITION,
m_bFilterActive ? ID_MENUITEM_CLEARFILTER : ID_MENUITEM_APPLYFILTER,
m_bFilterActive ? CS_CLEARFILTERS : CS_SETFILTERS);
goto exit;
}
if (bItemSelected) {
pci = getSelectedItem();
ASSERT(pci != NULL);
}
// Save original entry type before possibly changing pci
const CItemData::EntryType etype_original =
pci == NULL ? CItemData::ET_INVALID : pci->GetEntryType();
if (bItemSelected && pci->IsShortcut())
pci = m_core.GetBaseEntry(pci);
if (bTreeView) {
HTREEITEM hi = m_ctlItemTree.GetSelectedItem();
bGroupSelected = (hi != NULL && !m_ctlItemTree.IsLeaf(hi));
}
// If Edit menu selected
if (uiMenuID == ID_EDITMENU) {
// Delete all entries and rebuild depending on group/entry selected
// and, if entry, if fields are non-empty
UINT uiCount = pPopupMenu->GetMenuItemCount();
ASSERT((int)uiCount >= 0);
for (UINT ui = 0; ui < uiCount; ui++) {
pPopupMenu->RemoveMenu(0, MF_BYPOSITION);
}
/*
Three scenarios:
Note: If the database is read-only, anything modifying it is not added.
1. Nothing selected
Tree View - Add Entry, Sep, Add Group, Sep, Clear Clipboard
List View - Add Entry, Sep, Clear Clipboard
2. Group selected (Tree view only)
Add Entry, Sep, <Group Items>, Sep, Clear Clipboard
Add Group, Duplicate Group, (Un)Protect entries
3. Entry selected
Tree View - <Entry Items>, Sep, Add Group, Sep, Clear Clipboard, Sep
Entry functions (copy to clipboard, browse, run etc)
List View - <Entry Items>, Sep, Clear Clipboard, Sep
Entry functions (copy to clipboard, browse, run etc)
*/
// Scenario 1 - Nothing selected
// Tree View - Add Entry, Sep, Add Group, Sep, Clear Clipboard
// List View - Add Entry, Sep, Clear Clipboard
if (pci == NULL && !bGroupSelected) {
if (m_IsListView) {
if (!bReadOnly) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_ADD, tc_dummy);
pPopupMenu->InsertMenu((UINT)-1, MF_SEPARATOR);
}
} else {
if (!bReadOnly) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_ADD, tc_dummy);
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_ADDGROUP, tc_dummy);
pPopupMenu->InsertMenu((UINT)-1, MF_SEPARATOR);
}
}
if (m_core.AnyToUndo() || m_core.AnyToRedo()) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_UNDO, tc_dummy);
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_REDO, tc_dummy);
pPopupMenu->InsertMenu((UINT)-1, MF_SEPARATOR);
}
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_CLEARCLIPBOARD, tc_dummy);
goto exit;
}
// Scenario 2 - Group selected (Tree view only)
// Add Entry, Sep, <Group Items>, Sep, Clear Clipboard
if (!m_IsListView && bGroupSelected) {
if (!bReadOnly) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_ADD, tc_dummy);
pPopupMenu->InsertMenu((UINT)-1, MF_SEPARATOR);
}
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_GROUPENTER, tc_dummy);
if (!bReadOnly) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_DELETEGROUP, tc_dummy);
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_RENAMEGROUP, tc_dummy);
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_ADDGROUP, tc_dummy);
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_DUPLICATEGROUP, tc_dummy);
int numProtected, numUnprotected;
bool bProtect = GetSubtreeEntriesProtectedStatus(numProtected, numUnprotected);
if (bProtect) {
if (numUnprotected > 0)
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_PROTECTGROUP, tc_dummy);
if (numProtected > 0)
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_UNPROTECTGROUP, tc_dummy);
}
}
pPopupMenu->InsertMenu((UINT)-1, MF_SEPARATOR);
if (m_core.AnyToUndo() || m_core.AnyToRedo()) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_UNDO, tc_dummy);
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_REDO, tc_dummy);
pPopupMenu->InsertMenu((UINT)-1, MF_SEPARATOR);
}
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_CLEARCLIPBOARD, tc_dummy);
goto exit;
}
// Scenario 3 - Entry selected
// Tree View - <Entry Items>, Sep, Add Entry, Add Group, Sep, Clear Clipboard, Sep,
// Entry functions (copy to clipboard, browse, run etc)
// List View - <Entry Items>, Sep, Add Entry, Sep, Clear Clipboard, Sep,
// Entry functions (copy to clipboard, browse, run etc)
if (pci != NULL) {
// Entry is selected
// Deal with multi-selection
// More than 2 is meaningless in List view
if (m_IsListView && m_ctlItemList.GetSelectedCount() > 2)
return;
// If exactly 2 selected - show compare entries menu
if (m_IsListView && m_ctlItemList.GetSelectedCount() == 2) {
CString cs_txt(MAKEINTRESOURCE(ID_MENUITEM_COMPARE_ENTRIES));
cs_txt.TrimLeft(); // Remove leading newline
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_COMPARE_ENTRIES, cs_txt);
return;
}
if (!bReadOnly) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_ADD, tc_dummy);
}
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
m_core.IsReadOnly() ? ID_MENUITEM_VIEW : ID_MENUITEM_EDIT,
tc_dummy);
if (!bReadOnly) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_DELETEENTRY, tc_dummy);
if (!m_IsListView) {
// Rename not valid in List View
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_RENAMEENTRY, tc_dummy);
}
}
// Only have Find Next/Previous if find still active and entries were found
if (m_FindToolBar.IsVisible() && m_FindToolBar.EntriesFound()) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_FIND, tc_dummy);
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_FINDUP, tc_dummy);
} else { // otherwise just add "Find..."
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_FINDELLIPSIS, tc_dummy);
}
if (!bReadOnly) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_DUPLICATEENTRY, tc_dummy);
if (!m_IsListView) {
pPopupMenu->InsertMenu((UINT)-1, MF_SEPARATOR);
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_ADDGROUP, tc_dummy);
}
}
pPopupMenu->InsertMenu((UINT)-1, MF_SEPARATOR);
if (m_core.AnyToUndo() || m_core.AnyToRedo()) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_UNDO, tc_dummy);
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_REDO, tc_dummy);
pPopupMenu->InsertMenu((UINT)-1, MF_SEPARATOR);
}
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_CLEARCLIPBOARD, tc_dummy);
pPopupMenu->InsertMenu((UINT)-1, MF_SEPARATOR);
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_COPYPASSWORD, tc_dummy);
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_PASSWORDSUBSET, tc_dummy);
if (!pci->IsUserEmpty())
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_COPYUSERNAME, tc_dummy);
if (!pci->IsNotesEmpty())
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_COPYNOTESFLD, tc_dummy);
/*
* Rules:
* 1. If email field is not empty, add email menuitem.
* 2. If URL is not empty and is NOT an email address, add browse menuitem
* 3. If URL is not empty and is an email address, add email menuitem
* (if not already added)
*/
bool bAddCopyEmail = !pci->IsEmailEmpty();
bool bAddSendEmail = bAddCopyEmail || (!pci->IsURLEmpty() && pci->IsURLEmail());
bool bAddURL = !pci->IsURLEmpty();
// Add copies in order
if (bAddURL) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_COPYURL, tc_dummy);
}
if (bAddCopyEmail) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_COPYEMAIL, tc_dummy);
}
if (!pci->IsRunCommandEmpty())
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_COPYRUNCOMMAND, tc_dummy);
pPopupMenu->InsertMenu((UINT)-1, MF_SEPARATOR);
// Add actions in order
if (bAddURL && !pci->IsURLEmail()) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_BROWSEURL, tc_dummy);
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_BROWSEURLPLUS, tc_dummy);
}
if (bAddSendEmail) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_SENDEMAIL, tc_dummy);
}
if (!pci->IsRunCommandEmpty())
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_RUNCOMMAND, tc_dummy);
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_AUTOTYPE, tc_dummy);
switch (etype_original) {
case CItemData::ET_NORMAL:
case CItemData::ET_SHORTCUTBASE:
// Allow creation of a shortcut
if (!bReadOnly) {
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_CREATESHORTCUT, tc_dummy);
}
break;
case CItemData::ET_ALIASBASE:
// Can't have a shortcut to an AliasBase entry + can't goto base
break;
case CItemData::ET_ALIAS:
case CItemData::ET_SHORTCUT:
// Allow going to/editing the appropriate base entry
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_GOTOBASEENTRY, tc_dummy);
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_EDITBASEENTRY, tc_dummy);
break;
default:
ASSERT(0);
}
CMenu EEsubMenu;
CString EEstr;
EEsubMenu.CreatePopupMenu();
EEstr.LoadString(IDS_EXPORTENT2PLAINTEXT);
EEsubMenu.AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_EXPORTENT2PLAINTEXT, EEstr);
EEstr.LoadString(IDS_EXPORTENT2XML);
EEsubMenu.AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_EXPORTENT2XML, EEstr);
EEstr.LoadString(IDS_EXPORTENTMENU);
pPopupMenu->AppendMenu(MF_POPUP, (UINT)EEsubMenu.Detach(), EEstr);
if (!bReadOnly && etype_original != CItemData::ET_SHORTCUT)
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
pci->IsProtected() ? ID_MENUITEM_UNPROTECT : ID_MENUITEM_PROTECT,
tc_dummy);
// Tree view and command flag present only
if (!m_IsListView && m_bCompareEntries &&
etype_original != CItemData::ET_SHORTCUT)
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_COMPARE_ENTRIES, tc_dummy);
} else {
// Must be List view with no entry selected
pPopupMenu->InsertMenu((UINT)-1, MF_SEPARATOR);
pPopupMenu->AppendMenu(MF_ENABLED | MF_STRING,
ID_MENUITEM_CLEARCLIPBOARD, tc_dummy);
}
} // Edit menu
exit:
SetUpMenuStrings(pPopupMenu);
}
// Helps with MRU by allowing ON_UPDATE_COMMAND_UI
void DboxMain::OnInitMenuPopup(CMenu* pPopupMenu, UINT, BOOL)
{
// Original OnInitMenu code
// All main menus are POPUPs (see PasswordSafe2.rc2)
// Don't do the old OnInitMenu processing if the right-click context menu
// (IDR_POPEDITMENU or IDR_POPEDITGROUP) is being processed. Only for the Main Menu
// (ID_EDITMENU).
ASSERT_VALID(pPopupMenu);
BOOL brc;
MENUINFO minfo = {0};
minfo.cbSize = sizeof(MENUINFO);
minfo.fMask = MIM_MENUDATA;
brc = pPopupMenu->GetMenuInfo(&minfo);
ASSERT(brc != 0);
// Need to update the main menu if the shortcuts have been changed or
// if the Edit or View menu. The Edit menu is completely rebuilt each time
// based on the view and if anything is selected. The View menu has one item
// (ShowFindToolbar) that is removed if already displayed
bool bDoShortcuts(false);
switch (minfo.dwMenuData) {
case ID_FILEMENU:
case ID_EXPORTMENU:
case ID_IMPORTMENU:
case ID_EDITMENU:
case ID_VIEWMENU:
case ID_FILTERMENU:
case ID_EXPORTENTMENU:
case ID_CHANGEFONTMENU:
case ID_REPORTSMENU:
case ID_MANAGEMENU:
case ID_HELPMENU: //main menu items' shortcut should be always updated because keyboard layout could be changed
bDoShortcuts = true;
break;
default:
break;
}
if (bDoShortcuts ||
minfo.dwMenuData == ID_FILEMENU ||
minfo.dwMenuData == ID_EDITMENU ||
minfo.dwMenuData == ID_VIEWMENU)
CustomiseMenu(pPopupMenu, (UINT)minfo.dwMenuData, bDoShortcuts);
if (minfo.dwMenuData == ID_MANAGEMENU) {
// Process the Change Language sub-menu
// First get its position and then build it
// If no entries added (no language DLLs or back level language DLLs),
// disable the menu item.
int iLangPos = app.FindMenuItem(pPopupMenu, ID_LANGUAGEMENU);
if (iLangPos >= 0) {
CMenu *pSubMenu = pPopupMenu->GetSubMenu(iLangPos);
const bool brc = ProcessLanguageMenu(pSubMenu);
pPopupMenu->EnableMenuItem(iLangPos, MF_BYPOSITION | (brc ? MF_ENABLED : MF_GRAYED));
}
}
// http://www4.ncsu.edu:8030/~jgbishop/codetips/dialog/updatecommandui_menu.html
// This code comes from the MFC Documentation, and is adapted from
// CFrameWnd::OnInitMenuPopup() in WinFrm.cpp.
CCmdUI state; // Check the enabled state of various menu items
state.m_pMenu = pPopupMenu;
ASSERT(state.m_pOther == NULL);
ASSERT(state.m_pParentMenu == NULL);
// Is the menu in question a popup in the top-level menu? If so, set m_pOther
// to this menu. Note that m_pParentMenu == NULL indicates that the menu is a
// secondary popup.
CMenu *hParentMenu;
if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu) {
state.m_pParentMenu = pPopupMenu; // Parent == child for tracking popup.
} else
if ((hParentMenu = this->GetMenu()) != NULL) {
CWnd* pParent = this;
// Child windows don't have menus--need to go to the top!
if (pParent != NULL && (hParentMenu = pParent->GetMenu()) != NULL) {
int nIndexMax = hParentMenu->GetMenuItemCount();
for (int nIndex = 0; nIndex < nIndexMax; nIndex++) {
CMenu *submenu = hParentMenu->GetSubMenu(nIndex);
if (submenu != NULL && submenu->m_hMenu == pPopupMenu->m_hMenu) {
// When popup is found, m_pParentMenu is containing menu.
state.m_pParentMenu = CMenu::FromHandle(hParentMenu->GetSafeHmenu());
break;
}
}
}
}
state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax; state.m_nIndex++) {
state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
if (state.m_nID == 0)
continue; // Menu separator or invalid cmd - ignore it.
ASSERT(state.m_pOther == NULL);
ASSERT(state.m_pMenu != NULL);
if (state.m_nID == (UINT)-1) {
// Possibly a popup menu, route to first item of that popup.
state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
if (state.m_pSubMenu == NULL ||
(state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
state.m_nID == (UINT)-1) {
continue; // First item of popup can't be routed to.
}
state.DoUpdate(this, TRUE); // Popups are never auto disabled.
} else {
// Normal menu item.
// Auto enable/disable if frame window has m_bAutoMenuEnable
// set and command is _not_ a system command.
state.m_pSubMenu = NULL;
state.DoUpdate(this, FALSE);
}
// Adjust for menu deletions and additions.
UINT nCount = pPopupMenu->GetMenuItemCount();
if (nCount < state.m_nIndexMax) {
state.m_nIndex -= (state.m_nIndexMax - nCount);
while(state.m_nIndex < nCount &&
pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID) {
state.m_nIndex++;
}
}
state.m_nIndexMax = nCount;
}
// Ignore SystemTray popup menu
if (!m_bImageInLV ||
(minfo.dwMenuData != IDR_POPTRAY &&
minfo.dwMenuData != IDR_POPCOPYTOORIGINAL &&
minfo.dwMenuData != IDR_POPEDITVIEWORIGINAL))
return;
// System Tray Popup menu processing only
SecureZeroMemory(&minfo, sizeof(minfo));
minfo.cbSize = sizeof(minfo);
minfo.fMask = MIM_STYLE;
minfo.dwStyle = MNS_CHECKORBMP | MNS_AUTODISMISS;
brc = pPopupMenu->SetMenuInfo(&minfo);
ASSERT(brc != 0);
MENUITEMINFO miteminfo = {0};
CRUEItemData *pmd;
UINT uiCount = pPopupMenu->GetMenuItemCount();
ASSERT((int)uiCount >= 0);
for (UINT pos = 0; pos < uiCount; pos++) {
SecureZeroMemory(&miteminfo, sizeof(miteminfo));
miteminfo.cbSize = sizeof(miteminfo);
miteminfo.fMask = MIIM_FTYPE | MIIM_DATA;
brc = pPopupMenu->GetMenuItemInfo(pos, &miteminfo, TRUE);
ASSERT(brc != 0);
pmd = (CRUEItemData *)miteminfo.dwItemData;
if (pmd && pmd->IsRUEID() && !(miteminfo.fType & MFT_OWNERDRAW) &&
pmd->nImage >= 0) {
SecureZeroMemory(&miteminfo, sizeof(miteminfo));
miteminfo.cbSize = sizeof(miteminfo);
miteminfo.fMask = MIIM_FTYPE | MIIM_BITMAP;
miteminfo.hbmpItem = HBMMENU_CALLBACK;
miteminfo.fType = MFT_STRING;
brc = pPopupMenu->SetMenuItemInfo(pos, &miteminfo, TRUE);
ASSERT(brc != 0);
}
}
}
// Called when right-click is invoked in the client area of the window.
void DboxMain::OnContextMenu(CWnd * /* pWnd */, CPoint screen)
{
const DWORD dwTrackPopupFlags = TPM_LEFTALIGN | TPM_RIGHTBUTTON;
BOOL brc;
CPoint client;
int item = -1;
const CItemData *pci = NULL;
CMenu menu;
MENUINFO minfo ={0};
minfo.cbSize = sizeof(MENUINFO);
minfo.fMask = MIM_MENUDATA;
// Note if point = (-1, -1) then invoked via keyboard.
// Need coordinates of current selected itme instead on mouse position when message sent
bool bKeyboard = (screen.x == -1 && screen.y == -1);
CPoint mp; // Screen co-ords (from "message point" or via Shift+F10 selected item
CRect rect, appl_rect;
// Get client window position
if (bKeyboard) {
CRect r;
if (m_ctlItemList.IsWindowVisible()) {
POSITION pos = m_ctlItemList.GetFirstSelectedItemPosition();
if (pos == NULL)
return; // Nothing selected!
int nIndex = m_ctlItemList.GetNextSelectedItem(pos);
m_ctlItemList.GetItemRect(nIndex, &r, LVIR_LABEL);
m_ctlItemList.ClientToScreen(&r);
} else
if (m_ctlItemTree.IsWindowVisible()) {
HTREEITEM hItem = m_ctlItemTree.GetSelectedItem();
if (hItem == NULL)
return; // Nothing selected!
m_ctlItemTree.GetItemRect(hItem, &r, TRUE);
m_ctlItemTree.ClientToScreen(&r);
}
mp.x = (r.left + r.right) / 2;
mp.y = (r.top + r.bottom) / 2;
screen = mp; // In screen co-ords
} else {
mp = ::GetMessagePos();
}
GetWindowRect(&appl_rect);
m_MainToolBar.GetWindowRect(&rect);
// RClick over Main Toolbar - allow Customize Main toolbar
if (m_MainToolBar.IsVisible() &&
mp.x > appl_rect.left && mp.x < appl_rect.right &&
mp.y > rect.top && mp.y < rect.bottom) {
if (menu.LoadMenu(IDR_POPCUSTOMIZETOOLBAR)) {
minfo.dwMenuData = IDR_POPCUSTOMIZETOOLBAR;
brc = menu.SetMenuInfo(&minfo);
ASSERT(brc != 0);
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT_VALID(pPopup);
// Use this DboxMain for commands
pPopup->TrackPopupMenu(dwTrackPopupFlags, screen.x, screen.y, this);
}
return;
}
if (!m_bOpen)
return;
client = screen;
// RClick over ListView
if (m_ctlItemList.IsWindowVisible()) {
// currently in flattened list view.
m_ctlItemList.GetWindowRect(&rect);
if (mp.x < rect.left || mp.x > appl_rect.right ||
mp.y < rect.top || mp.y > rect.bottom) {
// But not in the window
return;
}
m_ctlItemList.ScreenToClient(&client);
item = m_ctlItemList.HitTest(client);
if (item < 0)
return; // right click on empty list
pci = (CItemData *)m_ctlItemList.GetItemData(item);
UpdateToolBarForSelectedItem(pci);
if (SelectEntry(item) == 0) {
return;
}
m_ctlItemList.SetFocus();
}
// RClick over TreeView
if (m_ctlItemTree.IsWindowVisible()) {
// currently in tree view
m_ctlItemTree.GetWindowRect(&rect);
if (mp.x < rect.left || mp.x > appl_rect.right ||
mp.y < rect.top || mp.y > rect.bottom) {
// But not in the window
return;
}
ASSERT(m_ctlItemTree.IsWindowVisible());
m_ctlItemTree.ScreenToClient(&client);
HTREEITEM ti = m_ctlItemTree.HitTest(client);
if (ti != NULL) {
pci = (CItemData *)m_ctlItemTree.GetItemData(ti);
UpdateToolBarForSelectedItem(pci);
if (pci != NULL) {
// right-click was on an item (LEAF of some kind: normal, alias, shortcut)
DisplayInfo *pdi = (DisplayInfo *)pci->GetDisplayInfo();
ASSERT(pdi != NULL);
ASSERT(pdi->tree_item == ti);
item = pdi->list_index;
m_ctlItemTree.SelectItem(ti); // So that OnEdit gets the right one
} else {
// right-click was on a group (GROUP)
m_ctlItemTree.SelectItem(ti);
if (menu.LoadMenu(IDR_POPEDITGROUP)) {
minfo.dwMenuData = IDR_POPEDITGROUP;
menu.SetMenuInfo(&minfo);
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT_VALID(pPopup);
m_TreeViewGroup = m_ctlItemTree.GetGroup(ti);
int numProtected, numUnprotected;
bool bProtect = GetSubtreeEntriesProtectedStatus(numProtected, numUnprotected);
if (!bProtect || numUnprotected == 0)
pPopup->RemoveMenu(ID_MENUITEM_PROTECTGROUP, MF_BYCOMMAND);
if (!bProtect || numProtected == 0)
pPopup->RemoveMenu(ID_MENUITEM_UNPROTECTGROUP, MF_BYCOMMAND);
// Use this DboxMain for commands
pPopup->TrackPopupMenu(dwTrackPopupFlags, screen.x, screen.y, this);
}
}
} else {
// not over anything
if (menu.LoadMenu(IDR_POPTREE)) { // "Add Group"
minfo.dwMenuData = IDR_POPTREE;
brc = menu.SetMenuInfo(&minfo);
ASSERT(brc != 0);
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT_VALID(pPopup);
ti = m_ctlItemTree.GetSelectedItem();
if (ti == NULL || (ti != NULL && (CItemData *)m_ctlItemTree.GetItemData(ti) != NULL))
pPopup->RemoveMenu(ID_MENUITEM_DUPLICATEGROUP, MF_BYCOMMAND);
// Use this DboxMain for commands
m_bWhitespaceRightClick = true;
pPopup->TrackPopupMenu(dwTrackPopupFlags, screen.x, screen.y, this);
}
}
m_ctlItemTree.SetFocus();
} // tree view handling
// RClick over an entry
if (item >= 0) {
CMenu *pPopup(NULL);
// More than 2 is meaningless in List view
if (m_IsListView && m_ctlItemList.GetSelectedCount() > 2)
return;
// Only compare entries if 2 entries are selected - List view only
if (m_IsListView && m_ctlItemList.GetSelectedCount() == 2) {
brc = menu.LoadMenu(IDR_POPCOMPAREENTRIES);
ASSERT(brc != 0);
minfo.dwMenuData = IDR_POPCOMPAREENTRIES;
brc = menu.SetMenuInfo(&minfo);
ASSERT(brc != 0);
pPopup = menu.GetSubMenu(0);
ASSERT_VALID(pPopup);
// Use this DboxMain for commands
pPopup->TrackPopupMenu(dwTrackPopupFlags, screen.x, screen.y, this);
return;
}
ASSERT(pci != NULL);
brc = menu.LoadMenu(IDR_POPEDITMENU);
ASSERT(brc != 0);
minfo.dwMenuData = IDR_POPEDITMENU;
brc = menu.SetMenuInfo(&minfo);
ASSERT(brc != 0);
pPopup = menu.GetSubMenu(0);
ASSERT_VALID(pPopup);
const CItemData::EntryType etype_original = pci->GetEntryType();
switch (etype_original) {
case CItemData::ET_NORMAL:
case CItemData::ET_SHORTCUTBASE:
pPopup->RemoveMenu(ID_MENUITEM_GOTOBASEENTRY, MF_BYCOMMAND);
pPopup->RemoveMenu(ID_MENUITEM_EDITBASEENTRY, MF_BYCOMMAND);
break;
case CItemData::ET_ALIASBASE:
pPopup->RemoveMenu(ID_MENUITEM_CREATESHORTCUT, MF_BYCOMMAND);
pPopup->RemoveMenu(ID_MENUITEM_GOTOBASEENTRY, MF_BYCOMMAND);
pPopup->RemoveMenu(ID_MENUITEM_EDITBASEENTRY, MF_BYCOMMAND);
break;
case CItemData::ET_ALIAS:
case CItemData::ET_SHORTCUT:
pPopup->RemoveMenu(ID_MENUITEM_CREATESHORTCUT, MF_BYCOMMAND);
break;
default:
ASSERT(0);
}
if (m_core.IsReadOnly() || pci->IsShortcut()) {
pPopup->RemoveMenu(ID_MENUITEM_PROTECT, MF_BYCOMMAND);
pPopup->RemoveMenu(ID_MENUITEM_UNPROTECT, MF_BYCOMMAND);
} else {
pPopup->RemoveMenu(pci->IsProtected() ? ID_MENUITEM_PROTECT : ID_MENUITEM_UNPROTECT, MF_BYCOMMAND);
}
// NOTE: after here, if an entry is a Shortcut, pci points to its base entry!
if (pci->IsShortcut()) {
pci = m_core.GetBaseEntry(pci);
}
bool bCopyEmail = !pci->IsEmailEmpty();
bool bSendEmail = bCopyEmail || (!pci->IsURLEmpty() && pci->IsURLEmail());
bool bUseURL = !pci->IsURLEmpty() && !pci->IsURLEmail();
if (pci->IsUserEmpty())
pPopup->RemoveMenu(ID_MENUITEM_COPYUSERNAME, MF_BYCOMMAND);
if (pci->IsNotesEmpty())
pPopup->RemoveMenu(ID_MENUITEM_COPYNOTESFLD, MF_BYCOMMAND);
if (!bCopyEmail)
pPopup->RemoveMenu(ID_MENUITEM_COPYEMAIL, MF_BYCOMMAND);
if (!bSendEmail)
pPopup->RemoveMenu(ID_MENUITEM_SENDEMAIL, MF_BYCOMMAND);
if (pci->IsURLEmpty())
pPopup->RemoveMenu(ID_MENUITEM_COPYURL, MF_BYCOMMAND);
if (!bUseURL) {
pPopup->RemoveMenu(ID_MENUITEM_BROWSEURL, MF_BYCOMMAND);
pPopup->RemoveMenu(ID_MENUITEM_BROWSEURLPLUS, MF_BYCOMMAND);
}
if (pci->IsRunCommandEmpty()) {
pPopup->RemoveMenu(ID_MENUITEM_COPYRUNCOMMAND, MF_BYCOMMAND);
pPopup->RemoveMenu(ID_MENUITEM_RUNCOMMAND, MF_BYCOMMAND);
}
if (m_IsListView) {
// Rename not valid in List View
pPopup->RemoveMenu(ID_MENUITEM_RENAMEENTRY, MF_BYCOMMAND);
}
// Since an entry - remove Duplicate Group
pPopup->RemoveMenu(ID_MENUITEM_DUPLICATEGROUP, MF_BYCOMMAND);
// Remove if in List View, not allowed in Tree view or a shortcut
if (m_IsListView || !m_bCompareEntries ||
etype_original == CItemData::ET_SHORTCUT)
pPopup->RemoveMenu(ID_MENUITEM_COMPARE_ENTRIES, MF_BYCOMMAND);
// Use this DboxMain for commands
pPopup->TrackPopupMenu(dwTrackPopupFlags, screen.x, screen.y, this);
} // if (item >= 0)
}
void DboxMain::SetupSpecialShortcuts()
{
MapMenuShortcutsIter iter, iter_entry, iter_group;
// Find Delete Shortcut
iter = m_MapMenuShortcuts.find(ID_MENUITEM_DELETE);
// Save for CTreeCtrl & CListCtrl PreTranslateMessage
if (iter != m_MapMenuShortcuts.end()) {
iter_entry = m_MapMenuShortcuts.find(ID_MENUITEM_DELETEENTRY);
iter_entry->second.SetKeyFlags(iter->second);
iter_group = m_MapMenuShortcuts.find(ID_MENUITEM_DELETEGROUP);
iter_group->second.SetKeyFlags(iter->second);
m_wpDeleteMsg = ((iter->second.cModifier & HOTKEYF_ALT) == HOTKEYF_ALT) ?
WM_SYSKEYDOWN : WM_KEYDOWN;
m_wpDeleteKey = iter->second.siVirtKey;
m_bDeleteCtrl = (iter->second.cModifier & HOTKEYF_CONTROL) == HOTKEYF_CONTROL;
m_bDeleteShift = (iter->second.cModifier & HOTKEYF_SHIFT) == HOTKEYF_SHIFT;
} else {
m_wpDeleteKey = 0;
}
// Find Rename Shortcut
iter = m_MapMenuShortcuts.find(ID_MENUITEM_RENAME);
// Save for CTreeCtrl::PreTranslateMessage (not CListCtrl)
if (iter != m_MapMenuShortcuts.end()) {
iter_entry = m_MapMenuShortcuts.find(ID_MENUITEM_RENAMEENTRY);
iter_entry->second.SetKeyFlags(iter->second);
iter_group = m_MapMenuShortcuts.find(ID_MENUITEM_RENAMEGROUP);
iter_group->second.SetKeyFlags(iter->second);
m_wpRenameMsg = ((iter->second.cModifier & HOTKEYF_ALT) == HOTKEYF_ALT) ?
WM_SYSKEYDOWN : WM_KEYDOWN;
m_wpRenameKey = iter->second.siVirtKey;
m_bRenameCtrl = (iter->second.cModifier & HOTKEYF_CONTROL) == HOTKEYF_CONTROL;
m_bRenameShift = (iter->second.cModifier & HOTKEYF_SHIFT) == HOTKEYF_SHIFT;
} else {
m_wpRenameKey = 0;
}
// Find Autotype Shortcut
iter = m_MapMenuShortcuts.find(ID_MENUITEM_AUTOTYPE);
// Save for CTreeCtrl & CListCtrl PreTranslateMessage
if (iter != m_MapMenuShortcuts.end()) {
m_wpAutotypeDNMsg = ((iter->second.cModifier & HOTKEYF_ALT) == HOTKEYF_ALT) ?
WM_SYSKEYDOWN : WM_KEYDOWN;
m_wpAutotypeUPMsg = ((iter->second.cModifier & HOTKEYF_ALT) == HOTKEYF_ALT) ?
WM_SYSKEYUP : WM_KEYUP;
m_wpAutotypeKey = iter->second.siVirtKey;
m_bAutotypeCtrl = (iter->second.cModifier & HOTKEYF_CONTROL) == HOTKEYF_CONTROL;
m_bAutotypeShift = (iter->second.cModifier & HOTKEYF_SHIFT) == HOTKEYF_SHIFT;
} else {
m_wpAutotypeKey = 0;
}
}
bool DboxMain::ProcessLanguageMenu(CMenu *pPopupMenu)
{
app.GetLanguageFiles();
// Should always be 1 entry (for the embedded resources)
// Can't do anything if no other DLLs available
// This can also happen if they are back-level resoure only DLLs who have
// a language ID the same as the application.
if (app.m_vlanguagefiles.size() < 2)
return false;
size_t i;
const UINT numItems = pPopupMenu->GetMenuItemCount();
// Remove any existing menu items in case user has done something!
for (i = 0; i < numItems; i++) {
pPopupMenu->RemoveMenu(0, MF_BYPOSITION);
}
// Add new ones
UINT nID = ID_LANGUAGES;
// Add languages
for (i = 0; i < app.m_vlanguagefiles.size(); i++) {
UINT uiFlags = MF_STRING | MF_ENABLED |
((app.m_vlanguagefiles[i].xFlags & 0x80) == 0x80) ? MF_CHECKED : MF_UNCHECKED;
pPopupMenu->AppendMenu(uiFlags, nID++, app.m_vlanguagefiles[i].wsLanguage.c_str());
}
return true;
}
const unsigned int DboxMain::GetMenuShortcut(const unsigned short int &siVirtKey,
const unsigned char &cModifier,
StringX &sxMenuItemName)
{
unsigned int nControlID(0);
sxMenuItemName.empty();
MapMenuShortcutsIter inuse_iter;
st_MenuShortcut st_mst;
st_mst.siVirtKey = siVirtKey;
st_mst.cModifier = cModifier;
inuse_iter = std::find_if(m_MapMenuShortcuts.begin(),
m_MapMenuShortcuts.end(),
already_inuse(st_mst));
if (inuse_iter != m_MapMenuShortcuts.end()) {
nControlID = inuse_iter->first;
sxMenuItemName = inuse_iter->second.name.c_str();
}
// std::vector<st_MenuShortcut> m_ReservedShortcuts
if (nControlID == 0) {
std::vector<st_MenuShortcut>::iterator iter;
iter = std::find_if(m_ReservedShortcuts.begin(), m_ReservedShortcuts.end(),
reserved(st_mst));
if (iter != m_ReservedShortcuts.end()) {
nControlID = iter->nControlID;
LoadAString(sxMenuItemName, nControlID);
}
}
if (!sxMenuItemName.empty()) {
// These may have the shortcut hardcoded after a tab character e.g. "\tF1" for Help
// Remove the tab and shortcut
size_t found = sxMenuItemName.find_first_of(_T("\t"));
if (found != StringX::npos)
sxMenuItemName.erase(found);
}
return nControlID;
}