Welcome to mirror list, hosted at ThFree Co, Russian Federation.

usermenu.cpp « src « far2l - github.com/elfmz/far2l.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6c8302d27b14139435872bfe6116f8117df89a4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
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
/*
usermenu.cpp

User menu и есть
*/
/*
Copyright (c) 1996 Eugene Roshal
Copyright (c) 2000 Far Group
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. The name of the authors may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "headers.hpp"


#include "lang.hpp"
#include "keys.hpp"
#include "filepanels.hpp"
#include "panel.hpp"
#include "cmdline.hpp"
#include "vmenu.hpp"
#include "dialog.hpp"
#include "fileedit.hpp"
#include "ctrlobj.hpp"
#include "manager.hpp"
#include "constitle.hpp"
#include "message.hpp"
#include "usermenu.hpp"
#include "filetype.hpp"
#include "fnparce.hpp"
#include "execute.hpp"
#include "pathmix.hpp"
#include "strmix.hpp"
#include "panelmix.hpp"
#include "filestr.hpp"
#include "mix.hpp"
#include "savescr.hpp"
#include "syslog.hpp"
#include "interf.hpp"
#include "cache.hpp"

#if defined(PROJECT_DI_MEMOEDIT)
/*
  Идея в следующем.
  1. Строки в реестре храняться как и раньше, т.к. CommandXXX
  2. Для DI_MEMOEDIT мы из только преобразовываем в один массив
*/
#endif

static std::unique_ptr<ConfigReader> s_cfg_reader;

// Коды выхода из меню (Exit codes)
enum
{
	EC_CLOSE_LEVEL      = -1, // Выйти из меню на один уровень вверх
	EC_CLOSE_MENU       = -2, // Выйти из меню по SHIFT+F10
	EC_PARENT_MENU      = -3, // Показать меню родительского каталога
	EC_MAIN_MENU        = -4, // Показать главное меню
	EC_COMMAND_SELECTED = -5, // Выбрана команда - закрыть меню и обновить папку
};

static int PrepareHotKey(FARString &strHotKey)
{
	int FuncNum=0;

	if (strHotKey.GetLength() > 1)
	{
		// если хоткей больше 1 символа, считаем это случаем "F?", причем при кривизне всегда будет "F1"
		FuncNum=_wtoi(strHotKey.CPtr()+1);

		if (FuncNum < 1 || FuncNum > 24)
		{
			FuncNum=1;
			strHotKey=L"F1";
		}
	}
	else
	{
		// при наличии "&" продублируем
		if (strHotKey.At(0) == L'&')
			strHotKey += L"&";
	}

	return FuncNum;
}

static void MenuRegToFile(const wchar_t *MenuKey, File& MenuFile, CachedWrite& CW, bool SingleItemMenu=false)
{
	for (int i=0;;i++)
	{
		const std::string &strItemKey = StrPrintf("%ls/Item%d", MenuKey, i);
		s_cfg_reader->SelectSection(strItemKey);
		FARString strLabel;
		if (!s_cfg_reader->GetString(strLabel, "Label", L"")) {
			break;
		}

		FARString strHotKey = s_cfg_reader->GetString("HotKey", L"");
		bool SubMenu = s_cfg_reader->GetInt("Submenu", 0) != 0;
		CW.Write(strHotKey.CPtr(), static_cast<DWORD>(strHotKey.GetLength()*sizeof(WCHAR)));
		CW.Write(L":  ", 3*sizeof(WCHAR));
		CW.Write(strLabel.CPtr(), static_cast<DWORD>(strLabel.GetLength()*sizeof(WCHAR)));
		CW.Write(L"\r\n", 2*sizeof(WCHAR));

		if (SubMenu)
		{
			CW.Write(L"{\r\n", 3*sizeof(WCHAR));
			MenuRegToFile(FARString(strItemKey), MenuFile, CW, false);
			CW.Write(L"}\r\n", 3*sizeof(WCHAR));
		}
		else
		{
			for (int i=0;; i++)
			{
				FARString strCommand;
				if (!s_cfg_reader->GetString(strCommand, StrPrintf("Command%d", i), L"")) {
					break;
				}

				CW.Write(L"    ", 4*sizeof(WCHAR));
				CW.Write(strCommand.CPtr(), static_cast<DWORD>(strCommand.GetLength()*sizeof(WCHAR)));
				CW.Write(L"\r\n", 2*sizeof(WCHAR));
			}
		}
	}
}

void MenuFileToReg(const wchar_t *MenuKey, File& MenuFile, GetFileString& GetStr, bool SingleItemMenu = false, UINT MenuCP = CP_WIDE_LE)
{
	INT64 Pos = 0;
	MenuFile.GetPointer(Pos);
	if(!Pos)
	{
		if (!GetFileFormat(MenuFile,MenuCP))
			MenuCP=CP_UTF8;
	}

	LPWSTR MenuStr = nullptr;
	int MenuStrLength = 0;
	int KeyNumber=-1,CommandNumber=0;

	while(GetStr.GetString(&MenuStr, MenuCP, MenuStrLength))
	{
		FARString strItemKey;

		if (!SingleItemMenu)
			strItemKey.Format(L"%ls/Item%d",MenuKey,KeyNumber);
		else
			strItemKey=MenuKey;

		RemoveTrailingSpaces(MenuStr);

		if (!*MenuStr)
			continue;

		if (*MenuStr==L'{' && KeyNumber>=0)
		{
			MenuFileToReg(strItemKey,MenuFile, GetStr, false,MenuCP);
			continue;
		}

		if (*MenuStr==L'}')
			break;

		if (!IsSpace(*MenuStr))
		{
			wchar_t *ChPtr=nullptr;

			if (!(ChPtr=wcschr(MenuStr,L':')))
				continue;

			if (!SingleItemMenu)
			{
				strItemKey.Format(L"%ls/Item%d",MenuKey,++KeyNumber);
			}
			else
			{
				strItemKey=MenuKey;
				++KeyNumber;
			}

			*ChPtr=0;
			FARString strHotKey=MenuStr;
			FARString strLabel=ChPtr+1;
			RemoveLeadingSpaces(strLabel);
			bool SubMenu=(GetStr.PeekString(&MenuStr, MenuCP, MenuStrLength) && *MenuStr==L'{');

			// Support for old 1.x separator format
			if(MenuCP==CP_OEMCP && strHotKey==L"-" && strLabel.IsEmpty())
			{
				strHotKey+=L"-";
			}

			ConfigWriter cfg_writer(strItemKey.GetMB());
			cfg_writer.SetString("HotKey", strHotKey);
			cfg_writer.SetString("Label", strLabel);
			cfg_writer.SetInt("Submenu", SubMenu);
			//CloseSameRegKey();
			CommandNumber=0;
		}
		else
		{
			if (KeyNumber>=0)
			{
				RemoveLeadingSpaces(MenuStr);
				const std::string &strLineName = StrPrintf("Command%d", CommandNumber);
				++CommandNumber;
				ConfigWriter(strItemKey.GetMB()).SetString(strLineName, MenuStr);
			}
		}

		ConfigReaderScope::Update(s_cfg_reader);

		SingleItemMenu=false;
	}
}

UserMenu::UserMenu(bool ChoiceMenuType)
	: grs(s_cfg_reader)
{
	ProcessUserMenu(ChoiceMenuType);
}

UserMenu::~UserMenu()
{
}

void UserMenu::ProcessUserMenu(bool ChoiceMenuType)
{
	// Путь к текущему каталогу с файлом LocalMenuFileName
	FARString strMenuFilePath;
	CtrlObject->CmdLine->GetCurDir(strMenuFilePath);
	// по умолчанию меню - это FarMenu.ini
	MenuMode=MM_LOCAL;
	FARString strLocalMenuKey;
	strLocalMenuKey.Format(L"UserMenu/LocalMenu%u",GetProcessUptimeMSec());
	{ ConfigWriter(strLocalMenuKey.GetMB()).RemoveSection(); }
	ConfigReaderScope::Update(s_cfg_reader);
	MenuModified=MenuNeedRefresh=false;

	if (ChoiceMenuType)
	{
		int EditChoice=Message(0,3,Msg::UserMenuTitle,Msg::ChooseMenuType,Msg::ChooseMenuMain,Msg::ChooseMenuLocal,Msg::Cancel);

		if (EditChoice<0 || EditChoice==2)
			return;

		if (!EditChoice)
		{
			MenuMode=MM_FAR;
			strMenuFilePath = g_strFarPath;
		}
	}

	// основной цикл обработки
	const wchar_t *LocalMenuFileName=L"FarMenu.ini";
	bool FirstRun=true;
	int ExitCode = 0;

	while ((ExitCode != EC_CLOSE_LEVEL) && (ExitCode != EC_CLOSE_MENU) && (ExitCode != EC_COMMAND_SELECTED))
	{
		FARString strMenuFileFullPath = strMenuFilePath;
		AddEndSlash(strMenuFileFullPath);
		strMenuFileFullPath += LocalMenuFileName;

		if (MenuMode != MM_MAIN)
		{
			// Пытаемся открыть файл на локальном диске
			File MenuFile;
			bool FileOpened = PathCanHoldRegularFile(strMenuFilePath) ? MenuFile.Open(strMenuFileFullPath,GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING) : false;
			if (FileOpened)
			{
				// сливаем содержимое в реестр "на запасной путь" и оттуда будем пользовать
				GetFileString GetStr(MenuFile);
				MenuFileToReg(strLocalMenuKey, MenuFile, GetStr);
				MenuFile.Close();
			}
			else
			{
				// Файл не открылся. Смотрим дальше.
				if (MenuMode == MM_FAR) // был в %FARHOME%?
				{
					MenuMode=MM_MAIN; // ...реестр
				}
				else
				{
					if (!ChoiceMenuType)
					{
						if (!FirstRun)
						{
							// подымаемся выше...
							size_t pos;

							if (FindLastSlash(pos,strMenuFilePath))
							{
								strMenuFilePath.Truncate(pos--);

								if (strMenuFilePath.At(pos) != L':')
									continue;
							}
						}

						FirstRun=false;
						strMenuFilePath = g_strFarPath;
						MenuMode=MM_FAR;
						continue;
					}
				}
			}
		}

		FARString strMenuRootKey = (MenuMode==MM_MAIN) ? L"UserMenu/MainMenu" : strLocalMenuKey;
		int PrevMacroMode=CtrlObject->Macro.GetMode();
		int _CurrentFrame=FrameManager->GetCurrentFrame()->GetType();
		CtrlObject->Macro.SetMode(MACRO_USERMENU);
		// вызываем меню
		ExitCode=ProcessSingleMenu(strMenuRootKey, 0,strMenuRootKey);

		if (_CurrentFrame == FrameManager->GetCurrentFrame()->GetType()) //???
			CtrlObject->Macro.SetMode(PrevMacroMode);

		// обработка локального меню...
		if (MenuMode != MM_MAIN)
		{
			// ...запишем изменения обратно в файл
			if (MenuModified)
			{
				DWORD FileAttr=apiGetFileAttributes(strMenuFileFullPath);

				if (FileAttr != INVALID_FILE_ATTRIBUTES)
				{
					if (FileAttr & FILE_ATTRIBUTE_READONLY)
					{
						int AskOverwrite;
						AskOverwrite=Message(MSG_WARNING,2,Msg::UserMenuTitle,LocalMenuFileName,Msg::EditRO,Msg::EditOvr,Msg::Yes,Msg::No);

						if (!AskOverwrite)
							apiSetFileAttributes(strMenuFileFullPath,FileAttr & ~FILE_ATTRIBUTE_READONLY);
					}

					if (FileAttr & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM))
						apiSetFileAttributes(strMenuFileFullPath,FILE_ATTRIBUTE_NORMAL);
				}

				File MenuFile;
				// Don't use CreationDisposition=CREATE_ALWAYS here - it's kills alternate streams
				if (MenuFile.Open(strMenuFileFullPath,GENERIC_WRITE, FILE_SHARE_READ, nullptr, FileAttr==INVALID_FILE_ATTRIBUTES?CREATE_NEW:TRUNCATE_EXISTING))
				{
					CachedWrite CW(MenuFile);
					WCHAR Data = SIGN_WIDE_LE;
					CW.Write(&Data, sizeof(WCHAR));
					MenuRegToFile(strLocalMenuKey,MenuFile, CW);
					CW.Flush();
					UINT64 Size = 0;
					MenuFile.GetSize(Size);
					MenuFile.Close();

					// если файл FarMenu.ini пуст, то удалим его
					if (Size<3) // 2 for BOM
					{
						apiDeleteFile(strMenuFileFullPath);
					}
					else if (FileAttr!=INVALID_FILE_ATTRIBUTES)
					{
						apiSetFileAttributes(strMenuFileFullPath,FileAttr);
					}
				}
			}

			// ...почистим реестр.
			{ ConfigWriter(strLocalMenuKey.GetMB()).RemoveSection(); }
			ConfigReaderScope::Update(s_cfg_reader);
		}

		// что было после вызова меню?
		switch (ExitCode)
		{
				// Показать меню родительского каталога
			case EC_PARENT_MENU:
			{
				if (MenuMode == MM_LOCAL)
				{
					size_t pos;

					if (FindLastSlash(pos,strMenuFilePath))
					{
						strMenuFilePath.Truncate(pos--);

						if (strMenuFilePath.At(pos)!=L':')
							continue;
					}

					strMenuFilePath = g_strFarPath;
					MenuMode=MM_FAR;
				}
				else
				{
					MenuMode=MM_MAIN;
				}

				break;
			}
			// Показать главное меню
			case EC_MAIN_MENU:
			{
				// $ 14.07.2000 VVM: Shift+F2 переключает Главное меню/локальное в цикле
				switch (MenuMode)
				{
					case MM_LOCAL:
					{
						strMenuFilePath = g_strFarPath;
						MenuMode=MM_FAR;
						break;
					}
					case MM_FAR:
					{
						MenuMode=MM_MAIN;
						break;
					}
					default: // MM_MAIN
					{
						CtrlObject->CmdLine->GetCurDir(strMenuFilePath);
						MenuMode=MM_LOCAL;
					}
				}

				break;
			}
		}
	}

	if (FrameManager->IsPanelsActive() && (ExitCode == EC_COMMAND_SELECTED || MenuModified))
		ShellUpdatePanels(CtrlObject->Cp()->ActivePanel,FALSE);
}

// заполнение меню
static int FillUserMenu(VMenu& UserMenu,const wchar_t *MenuKey,int MenuPos,int *FuncPos,const wchar_t *Name)
{
	UserMenu.DeleteItems();
	int NumLines=0;

	for (NumLines=0;; NumLines++)
	{
		s_cfg_reader->SelectSectionFmt("%ls/Item%d", MenuKey, NumLines);
		if (!s_cfg_reader->HasSection())
			break;

		MenuItemEx UserMenuItem;
		UserMenuItem.Clear();
		FARString strHotKey = s_cfg_reader->GetString("HotKey", L"");
		FARString strLabel = s_cfg_reader->GetString("Label", L"");
		int FuncNum=0;

		// сепаратором является случай, когда хоткей == "--"
		if (!StrCmp(strHotKey,L"--"))
		{
			UserMenuItem.Flags|=LIF_SEPARATOR;
			UserMenuItem.Flags&=~LIF_SELECTED;
			UserMenuItem.strName=strLabel;

			if (NumLines==MenuPos)
			{
				MenuPos++;
			}
		}
		else
		{
			SubstFileName(strLabel,Name,nullptr,nullptr,TRUE);
			apiExpandEnvironmentStrings(strLabel, strLabel);
			FuncNum=PrepareHotKey(strHotKey);
			int Offset=strHotKey.At(0)==L'&'?5:4;
			FormatString FString;
			FString<<((!strHotKey.IsEmpty() && !FuncNum)?L"&":L"")<<fmt::LeftAlign()<<fmt::Width(Offset)<<fmt::Precision(Offset)<<strHotKey;
			UserMenuItem.strName=std::move(FString.strValue());
			UserMenuItem.strName+=strLabel;

			if (s_cfg_reader->GetInt("Submenu", 0) != 0)
			{
				UserMenuItem.Flags|=MIF_SUBMENU;
			}

			UserMenuItem.SetSelect(NumLines==MenuPos);
			UserMenuItem.Flags &= ~LIF_SEPARATOR;
		}

		int ItemPos=UserMenu.AddItem(&UserMenuItem);

		if (FuncNum>0)
		{
			FuncPos[FuncNum-1]=ItemPos;
		}
	}

	MenuItemEx UserMenuItem;
	UserMenuItem.Clear();
	UserMenuItem.SetSelect(NumLines==MenuPos);
	UserMenu.AddItem(&UserMenuItem);
	return NumLines;
}

// обработка единичного меню
int UserMenu::ProcessSingleMenu(const wchar_t *MenuKey,int MenuPos,const wchar_t *MenuRootKey,const wchar_t *Title)
{
	MenuItemEx UserMenuItem;

	for (;;)
	{
		UserMenuItem.Clear();
		int NumLine=0,ExitCode,FuncPos[24];

		// очистка F-хоткеев
		for (size_t I=0 ; I < ARRAYSIZE(FuncPos) ; I++)
			FuncPos[I]=-1;

		FARString strName;
		CtrlObject->Cp()->ActivePanel->GetCurName(strName);
		/* $ 24.07.2000 VVM + При показе главного меню в заголовок добавляет тип - FAR/Registry */
		FARString strMenuTitle;

		if (Title && *Title)
			strMenuTitle = Title;
		else
		{
			switch (MenuMode)
			{
				case MM_LOCAL:
					strMenuTitle = Msg::LocalMenuTitle;
					break;
				case MM_FAR:
				{
					strMenuTitle=Msg::MainMenuTitle;
					strMenuTitle+=L" (";
					strMenuTitle+=Msg::MainMenuFAR;
					strMenuTitle+=L")";
				}
				break;
				default:
				{
					strMenuTitle=Msg::MainMenuTitle;
					const wchar_t *Ptr=Msg::MainMenuREG;

					if (*Ptr)
					{
						strMenuTitle+=L" (";
						strMenuTitle+=Ptr;
						strMenuTitle+=L")";
					}
				}
			}
		}

		{
			VMenu UserMenu(strMenuTitle,nullptr,0,ScrY-4);
			UserMenu.SetFlags(VMENU_WRAPMODE);
			UserMenu.SetHelp(L"UserMenu");
			UserMenu.SetPosition(-1,-1,0,0);
			UserMenu.SetBottomTitle(Msg::MainMenuBottomTitle);
			//NumLine=FillUserMenu(UserMenu,MenuKey,MenuPos,FuncPos,Name);
			MenuNeedRefresh=true;

			while (!UserMenu.Done())
			{
				if (MenuNeedRefresh)
				{
					UserMenu.Hide(); // спрячем
					// "изнасилуем" (перезаполним :-)
					NumLine=FillUserMenu(UserMenu,MenuKey,MenuPos,FuncPos,strName);
					// заставим манагер менюхи корректно отрисовать ширину и
					// высоту, а заодно и скорректировать вертикальные позиции
					UserMenu.SetPosition(-1,-1,-1,-1);
					UserMenu.Show();
					MenuNeedRefresh=false;
				}

				int Key=UserMenu.ReadInput();
				MenuPos=UserMenu.GetSelectPos();

				if ((unsigned int)Key>=KEY_F1 && (unsigned int)Key<=KEY_F24)
				{
					int FuncItemPos;

					if ((FuncItemPos=FuncPos[Key-KEY_F1])!=-1)
					{
						UserMenu.Modal::SetExitCode(FuncItemPos);
						continue;
					}
				}
				else if (Key == L' ') // исключаем пробел из "хоткеев"!
					continue;

				switch (Key)
				{
						/* $ 24.08.2001 VVM + Стрелки вправо/влево открывают/закрывают подменю соответственно */
					case KEY_RIGHT:
					case KEY_NUMPAD6:
					case KEY_MSWHEEL_RIGHT:
					{
						s_cfg_reader->SelectSectionFmt("%ls/Item%d", MenuKey, MenuPos);
						if (s_cfg_reader->GetInt("Submenu", 0) != 0)
							UserMenu.SetExitCode(MenuPos);

						break;
					}
					case KEY_LEFT:
					case KEY_NUMPAD4:
					case KEY_MSWHEEL_LEFT:

						if (Title && *Title)
							UserMenu.SetExitCode(-1);

						break;
					case KEY_NUMDEL:
					case KEY_DEL:

						if (MenuPos<NumLine)
							DeleteMenuRecord(MenuKey,MenuPos);

						break;
					case KEY_INS:
					case KEY_F4:
					case KEY_SHIFTF4:
					case KEY_NUMPAD0:

						if (Key != KEY_INS && Key != KEY_NUMPAD0 && MenuPos>=NumLine)
							break;

						EditMenu(MenuKey,MenuPos,NumLine,Key == KEY_INS || Key == KEY_NUMPAD0);
						break;
					case KEY_CTRLUP:
					case KEY_CTRLDOWN:
					{
						int Pos=UserMenu.GetSelectPos();

						if (Pos!=UserMenu.GetItemCount()-1)
						{
							if (!(Key==KEY_CTRLUP && !Pos) && !(Key==KEY_CTRLDOWN && Pos==UserMenu.GetItemCount()-2))
							{
								MenuPos=Pos+(Key==KEY_CTRLUP?-1:+1);
								MoveMenuItem(MenuKey,Pos,MenuPos);
							}
						}
					}
					break;
					//case KEY_ALTSHIFTF4:  // редактировать только текущий пункт (если субменю - то все субменю)
					case KEY_CTRLF4:       // редактировать все меню
					{
						(*FrameManager)[0]->Unlock();
						FARString strMenuFileName;
						File MenuFile;
						if (!FarMkTempEx(strMenuFileName) || (!MenuFile.Open(strMenuFileName, GENERIC_WRITE, FILE_SHARE_READ, nullptr, CREATE_ALWAYS)))
						{
							break;
						}

						FARString strCurrentKey;

						if (Key==KEY_ALTSHIFTF4)
							strCurrentKey.Format(L"%ls/Item%d",MenuKey,MenuPos);
						else
							strCurrentKey=MenuRootKey;
						CachedWrite CW(MenuFile);
						WCHAR Data = SIGN_WIDE_LE;
						CW.Write(&Data, sizeof(WCHAR));
						MenuRegToFile(strCurrentKey, MenuFile, CW, Key==KEY_ALTSHIFTF4);
						CW.Flush();
						MenuNeedRefresh=true;
						MenuFile.Close();
						{
							ConsoleTitle *OldTitle=new ConsoleTitle;
							FARString strFileName = strMenuFileName;
							FileEditor ShellEditor(strFileName,CP_WIDE_LE,FFILEEDIT_DISABLEHISTORY,-1,-1,nullptr);
							delete OldTitle;
							ShellEditor.SetDynamicallyBorn(false);
							FrameManager->EnterModalEV();
							FrameManager->ExecuteModal();
							FrameManager->ExitModalEV();

							if (!ShellEditor.IsFileChanged() || (!MenuFile.Open(strMenuFileName, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING)))
							{
								apiDeleteFile(strMenuFileName);

								if (Key == KEY_ALTSHIFTF4) // для тукущего пункта меню закрывать ненадо
									break;

								return 0;
							}
						}
						{ ConfigWriter(strCurrentKey.GetMB()).RemoveSection(); }
						ConfigReaderScope::Update(s_cfg_reader);
						GetFileString GetStr(MenuFile);
						MenuFileToReg(strCurrentKey, MenuFile, GetStr, Key==KEY_ALTSHIFTF4);
						MenuFile.Close();
						apiDeleteFile(strMenuFileName);
						MenuModified=true;
						UserMenu.Hide();

						if (Key == KEY_ALTSHIFTF4) // для тукущего пункта меню закрывать ненадо
							break;

						return 0; // Закрыть меню
					}
					/* $ 28.06.2000 tran
					выход из пользовательского меню по ShiftF10 из любого уровня
					вложенности просто задаем ExitCode -1, и возвращаем FALSE -
					по FALSE оно и выйдет откуда угодно */
					case KEY_SHIFTF10:
						//UserMenu.SetExitCode(-1);
						return(EC_CLOSE_MENU);
					case KEY_SHIFTF2: // Показать главное меню
						return(EC_MAIN_MENU);
					case KEY_BS: // Показать меню из родительского каталога только в MM_LOCAL режиме

						if (MenuMode != MM_MAIN)
							return(EC_PARENT_MENU);

					default:
						UserMenu.ProcessInput();

						if (Key == KEY_F1)
							MenuNeedRefresh=true;

						break;
				} // switch(Key)
			} // while (!UserMenu.Done())

			ExitCode=UserMenu.Modal::GetExitCode();
		}

		if (ExitCode<0 || ExitCode>=NumLine)
			return(EC_CLOSE_LEVEL); //  вверх на один уровень

		FARString strCurrentKey;
		strCurrentKey.Format(L"%ls/Item%d",MenuKey,ExitCode);
		s_cfg_reader->SelectSection(strCurrentKey);
		int SubMenu = s_cfg_reader->GetInt("Submenu", 0);

		if (SubMenu)
		{
			/* $ 20.08.2001 VVM + При вложенных меню показывает заголовки предыдущих */
			FARString strSubMenuKey, strSubMenuTitle, strSubMenuLabel;
			strSubMenuKey.Format(L"%ls/Item%d",MenuKey,ExitCode);
			s_cfg_reader->SelectSection(strSubMenuKey);
			if (s_cfg_reader->GetString(strSubMenuLabel, "Label", L""))
			{
				SubstFileName(strSubMenuLabel,strName,nullptr,nullptr,TRUE);
				apiExpandEnvironmentStrings(strSubMenuLabel, strSubMenuLabel);
				size_t pos;

				if (strSubMenuLabel.Pos(pos,L'&'))
					strSubMenuLabel.LShift(1,pos);

				if (Title && *Title)
				{
					strSubMenuTitle=Title;
					strSubMenuTitle+=L" -> ";
					strSubMenuTitle+=strSubMenuLabel;
				}
				else
					strSubMenuTitle = strSubMenuLabel;
			}

			/* $ 14.07.2000 VVM ! Если закрыли подменю, то остаться. Инече передать управление выше */
			MenuPos=ProcessSingleMenu(strSubMenuKey,0,MenuRootKey,strSubMenuTitle);

			if (MenuPos!=EC_CLOSE_LEVEL)
				return(MenuPos);

			MenuPos=ExitCode;
			continue;
		}

		/* $ 01.05.2001 IS Отключим до лучших времен */
		//int LeftVisible,RightVisible,PanelsHidden=0;
		int CurLine=0;
		FARString strCmdLineDir;
		CtrlObject->CmdLine->GetCurDir(strCmdLineDir);
		FARString strOldCmdLine;
		CtrlObject->CmdLine->GetString(strOldCmdLine);
		int OldCmdLineCurPos = CtrlObject->CmdLine->GetCurPos();
		int OldCmdLineLeftPos = CtrlObject->CmdLine->GetLeftPos();
		int OldCmdLineSelStart, OldCmdLineSelEnd;
		CtrlObject->CmdLine->GetSelection(OldCmdLineSelStart,OldCmdLineSelEnd);
		CtrlObject->CmdLine->LockUpdatePanel(TRUE);

		// Цикл исполнения команд меню (CommandX)
		for (;;)
		{
			FormatString strLineName;
			FARString strCommand, strListName, strAnotherListName;
			strLineName<<L"Command"<<CurLine;

			s_cfg_reader->SelectSection(strCurrentKey);
			if (!s_cfg_reader->GetString(strCommand, FARString(strLineName).GetMB(), L""))
				break;

			if (!((!StrCmpNI(strCommand,L"REM",3) && IsSpaceOrEos(strCommand.At(3))) || !StrCmpNI(strCommand,L"::",2)))
			{
				/*
				  Осталось корректно обработать ситуацию, например:
				  if exist !#!\!^!.! far:edit < diff -c -p !#!\!^!.! !\!.!
				  Т.е. сначала "вычислить" кусок "if exist !#!\!^!.!", ну а если
				  выполнится, то делать дальше.
				  Или еще пример,
				  if exist ..\a.bat D:\FAR\170\DIFF.MY\mkdiff.bat !?&Номер патча?!
				  ЭТО выполняется всегда, т.к. парсинг всей строки идет, а надо
				  проверить фазу "if exist ..\a.bat", а уж потом делать выводы...
				*/
				{
					/* $ 01.05.2001 IS Отключим до лучших времен */
					/*
					if (!PanelsHidden)
					{
						LeftVisible=CtrlObject->Cp()->LeftPanel->IsVisible();
						RightVisible=CtrlObject->Cp()->RightPanel->IsVisible();
						CtrlObject->Cp()->LeftPanel->Hide();
						CtrlObject->Cp()->RightPanel->Hide();
						CtrlObject->Cp()->LeftPanel->SetUpdateMode(FALSE);
						CtrlObject->Cp()->RightPanel->SetUpdateMode(FALSE);
						PanelsHidden=TRUE;
					}
					*/
					//;
					/*int PreserveLFN=*/SubstFileName(strCommand,strName,&strListName,&strAnotherListName, FALSE, strCmdLineDir);
					bool ListFileUsed=!strListName.IsEmpty()||!strAnotherListName.IsEmpty();

					{
						//PreserveLongName PreserveName(PreserveLFN);
						RemoveExternalSpaces(strCommand);

						if (!strCommand.IsEmpty())
						{
							bool isSilent=false;

							if (strCommand.At(0) == L'@')
							{
								strCommand.LShift(1);
								isSilent=true;
							}

							ProcessOSAliases(strCommand);
							// TODO: Ахтунг. В режиме isSilent имеем проблемы с командами, которые выводят что-то на экран
							//       Здесь необходимо переделка, например, перед исполнением подсунуть временный экранный буфер, а потом его содержимое подсунуть в ScreenBuf...

							if (!isSilent)
							{
								CtrlObject->CmdLine->ExecString(strCommand,FALSE, 0, 0, ListFileUsed);
							}
							else
							{
								SaveScreen SaveScr;
								CtrlObject->Cp()->LeftPanel->CloseFile();
								CtrlObject->Cp()->RightPanel->CloseFile();
								Execute(strCommand, 0, 0, 0, ListFileUsed, true);
							}
//							WaitForClose(strName);
						}
					}
				}
			} // strCommand != "REM"

			if (!strListName.IsEmpty())
				QueueDeleteOnClose(strListName);

			if (!strAnotherListName.IsEmpty())
				QueueDeleteOnClose(strAnotherListName);

			CurLine++;
		} // while (1)

		CtrlObject->CmdLine->LockUpdatePanel(FALSE);

		if (!strOldCmdLine.IsEmpty())  // восстановим сохраненную командную строку
		{
			CtrlObject->CmdLine->SetString(strOldCmdLine, FrameManager->IsPanelsActive());
			CtrlObject->CmdLine->SetCurPos(OldCmdLineCurPos, OldCmdLineLeftPos);
			CtrlObject->CmdLine->Select(OldCmdLineSelStart, OldCmdLineSelEnd);
		}

		/* $ 01.05.2001 IS Отключим до лучших времен */
		/*
		if (PanelsHidden)
		{
			CtrlObject->Cp()->LeftPanel->SetUpdateMode(TRUE);
			CtrlObject->Cp()->RightPanel->SetUpdateMode(TRUE);
			CtrlObject->Cp()->LeftPanel->Update(UPDATE_KEEP_SELECTION);
			CtrlObject->Cp()->RightPanel->Update(UPDATE_KEEP_SELECTION);
			if (RightVisible)
				CtrlObject->Cp()->RightPanel->Show();
			if (LeftVisible)
				CtrlObject->Cp()->LeftPanel->Show();
		}
		*/
		/* $ 14.07.2000 VVM ! Закрыть меню */
		/* $ 25.04.2001 DJ - сообщаем, что была выполнена команда (нужно перерисовать панели) */
		return(EC_COMMAND_SELECTED);
	}
}

enum EditMenuItems
{
	EM_DOUBLEBOX,
	EM_HOTKEY_TEXT,
	EM_HOTKEY_EDIT,
	EM_LABEL_TEXT,
	EM_LABEL_EDIT,
	EM_SEPARATOR1,
	EM_COMMANDS_TEXT,
#ifdef PROJECT_DI_MEMOEDIT
	EM_MEMOEDIT,
#else
	EM_EDITLINE_0,
	EM_EDITLINE_1,
	EM_EDITLINE_2,
	EM_EDITLINE_3,
	EM_EDITLINE_4,
	EM_EDITLINE_5,
	EM_EDITLINE_6,
	EM_EDITLINE_7,
	EM_EDITLINE_8,
	EM_EDITLINE_9,
#endif
	EM_SEPARATOR2,
	EM_BUTTON_OK,
	EM_BUTTON_CANCEL,
};

LONG_PTR WINAPI EditMenuDlgProc(HANDLE hDlg,int Msg,int Param1,LONG_PTR Param2)
{
#if defined(PROJECT_DI_MEMOEDIT)
	Dialog* Dlg=(Dialog*)hDlg;

	switch (Msg)
	{
		case DN_INITDIALOG:
		{
			break;
		}
	}

#endif

	switch (Msg)
	{
		case DN_CLOSE:

			if (Param1==EM_BUTTON_OK)
			{
				BOOL Result=TRUE;
				LPCWSTR HotKey=reinterpret_cast<LPCWSTR>(SendDlgMessage(hDlg,DM_GETCONSTTEXTPTR,EM_HOTKEY_EDIT,0));
				LPCWSTR Label=reinterpret_cast<LPCWSTR>(SendDlgMessage(hDlg,DM_GETCONSTTEXTPTR,EM_LABEL_EDIT,0));
				int FocusPos=-1;

				if(StrCmp(HotKey,L"--"))
				{
					if (!*Label)
					{
						FocusPos=EM_LABEL_EDIT;
					}
					else if (StrLength(HotKey)>1)
					{
						FocusPos=EM_HOTKEY_EDIT;

						if (Upper(*HotKey)==L'F')
						{
							int FuncNum=_wtoi(HotKey+1);

							if (FuncNum > 0 && FuncNum < 25)
								FocusPos=-1;
						}
					}
				}

				if (FocusPos!=-1)
				{
					Message(MSG_WARNING,1,Msg::UserMenuTitle,((*Label?Msg::UserMenuInvalidInputHotKey:Msg::UserMenuInvalidInputLabel)),Msg::Ok);
					SendDlgMessage(hDlg,DM_SETFOCUS,FocusPos,0);
					Result=FALSE;
				}

				return Result;
			}

			break;
	}

	return DefDlgProc(hDlg,Msg,Param1,Param2);
}


bool UserMenu::EditMenu(const wchar_t *MenuKey,int EditPos,int TotalRecords,bool Create)
{
	bool Result=false;
	FormatString strItemKey;
	strItemKey<<MenuKey<<L"/Item"<<EditPos;
	MenuNeedRefresh=true;
	bool SubMenu=false,Continue=true;

	s_cfg_reader->SelectSection(strItemKey);

	if (Create)
	{
		switch (Message(0,2,Msg::UserMenuTitle,Msg::AskInsertMenuOrCommand,Msg::MenuInsertCommand,Msg::MenuInsertMenu))
		{
			case -1:
			case -2:
				Continue=false;
			case 1:
				SubMenu=true;
		}
	}
	else
	{
		SubMenu = s_cfg_reader->GetInt("Submenu", 0) != 0;
	}

	if (Continue)
	{
		const int DLG_X=76, DLG_Y=SubMenu?10:22;
		DWORD State=SubMenu?DIF_HIDDEN|DIF_DISABLE:0;
		DialogDataEx EditDlgData[]=
		{
			{DI_DOUBLEBOX,3,1,DLG_X-4,(short)(DLG_Y-2),{},0,(SubMenu?Msg::EditSubmenuTitle:Msg::EditMenuTitle)},
			{DI_TEXT,5,2,0,2,{},0,Msg::EditMenuHotKey},
			{DI_FIXEDIT,5,3,7,3,{},DIF_FOCUS,L""},
			{DI_TEXT,5,4,0,4,{},0,Msg::EditMenuLabel},
			{DI_EDIT,5,5,DLG_X-6,5,{},0,L""},

			{DI_TEXT,3,6,0,6,{},DIF_SEPARATOR|State,L""},
			{DI_TEXT,5,7,0,7,{},State,Msg::EditMenuCommands},
#ifdef PROJECT_DI_MEMOEDIT
			{DI_MEMOEDIT,5, 8,DLG_X-6,17,{},DIF_EDITPATH,L""},
#else
			{DI_EDIT,5, 8,DLG_X-6,8,{},DIF_EDITPATH|DIF_EDITOR|State,L""},
			{DI_EDIT,5, 9,DLG_X-6,9,{},DIF_EDITPATH|DIF_EDITOR|State,L""},
			{DI_EDIT,5,10,DLG_X-6,10,{},DIF_EDITPATH|DIF_EDITOR|State,L""},
			{DI_EDIT,5,11,DLG_X-6,11,{},DIF_EDITPATH|DIF_EDITOR|State,L""},
			{DI_EDIT,5,12,DLG_X-6,12,{},DIF_EDITPATH|DIF_EDITOR|State,L""},
			{DI_EDIT,5,13,DLG_X-6,13,{},DIF_EDITPATH|DIF_EDITOR|State,L""},
			{DI_EDIT,5,14,DLG_X-6,14,{},DIF_EDITPATH|DIF_EDITOR|State,L""},
			{DI_EDIT,5,15,DLG_X-6,15,{},DIF_EDITPATH|DIF_EDITOR|State,L""},
			{DI_EDIT,5,16,DLG_X-6,16,{},DIF_EDITPATH|DIF_EDITOR|State,L""},
			{DI_EDIT,5,17,DLG_X-6,17,{},DIF_EDITPATH|DIF_EDITOR|State,L""},
#endif

			{DI_TEXT,3,(short)(DLG_Y-4),0,(short)(DLG_Y-4),{},DIF_SEPARATOR,L""},
			{DI_BUTTON,0,(short)(DLG_Y-3),0,(short)(DLG_Y-3),{},DIF_DEFAULT|DIF_CENTERGROUP,Msg::Ok},
			{DI_BUTTON,0,(short)(DLG_Y-3),0,(short)(DLG_Y-3),{},DIF_CENTERGROUP,Msg::Cancel}
		};
		MakeDialogItemsEx(EditDlgData,EditDlg);
#ifndef PROJECT_DI_MEMOEDIT
		enum {DI_EDIT_COUNT=EM_SEPARATOR2-EM_COMMANDS_TEXT-1};
#endif

		if (!Create)
		{
			EditDlg[EM_HOTKEY_EDIT].strData = s_cfg_reader->GetString("HotKey", L"");
			EditDlg[EM_LABEL_EDIT].strData = s_cfg_reader->GetString("Label", L"");
#if defined(PROJECT_DI_MEMOEDIT)
			/*
				...
				здесь добавка строк из "Command%d" в EMR_MEMOEDIT
				...
			*/
			FARString strBuffer, strCommand;
			int CommandNumber=0;

			while (s_cfg_reader->GetString(strCommand, StrPrintf("Command%d", CommandNumber), L""))
			{
				strBuffer+=strCommand;
				strBuffer+=L"\n";    //??? "\n\r"
				CommandNumber++;
			}

			EditDlg[EM_MEMOEDIT].strData = strBuffer; //???
#else
			int CommandNumber=0;

			while (CommandNumber < DI_EDIT_COUNT)
			{
				FARString strCommand;
				if (!s_cfg_reader->GetString(strCommand, StrPrintf("Command%d", CommandNumber), L""))
					break;

				EditDlg[EM_EDITLINE_0+CommandNumber].strData = strCommand;
				CommandNumber++;
			}

#endif
		}

		Dialog Dlg(EditDlg,ARRAYSIZE(EditDlg),EditMenuDlgProc);
		Dlg.SetHelp(L"UserMenu");
		Dlg.SetPosition(-1,-1,DLG_X,DLG_Y);
		Dlg.Process();

		if (Dlg.GetExitCode()==EM_BUTTON_OK)
		{
			MenuModified=true;
			{
				ConfigWriter cfg_writer;
				cfg_writer.SelectSectionFmt("%ls/Item%u", MenuKey, (unsigned int)EditPos);

				if (Create)
				{
					cfg_writer.ReserveIndexedSection(
						StrPrintf("%ls/Item", MenuKey).c_str(), (unsigned int)EditPos);
				}

				cfg_writer.SetString("HotKey", EditDlg[EM_HOTKEY_EDIT].strData.CPtr());
				cfg_writer.SetString("Label", EditDlg[EM_LABEL_EDIT].strData.CPtr());
				cfg_writer.SetInt("Submenu", SubMenu ? 1 : 0);

				if (!SubMenu)
				{
#if defined(PROJECT_DI_MEMOEDIT)
					/*
					...
					здесь преобразование содержимого итема EMR_MEMOEDIT в "Command%d"
					...
					*/
#else
					int CommandNumber=0;

					for (int i=0 ; i < DI_EDIT_COUNT ; i++)
						if (!EditDlg[i+EM_EDITLINE_0].strData.IsEmpty())
						CommandNumber=i+1;

					for (int i=0 ; i < DI_EDIT_COUNT ; i++)
					{
						const std::string &strCommandName = StrPrintf("Command%d", i);

						if (i>=CommandNumber)
							cfg_writer.RemoveKey(strCommandName);
						else
							cfg_writer.SetString(strCommandName, EditDlg[i+EM_EDITLINE_0].strData.CPtr());
					}
#endif
				}
			}

			ConfigReaderScope::Update(s_cfg_reader);
			Result=true;
		}
	}

	return Result;
}

int UserMenu::DeleteMenuRecord(const wchar_t *MenuKey,int DeletePos)
{
	FormatString strRegKey;
	strRegKey<<MenuKey<<L"/Item"<<DeletePos;
	s_cfg_reader->SelectSection(strRegKey);
	FARString strRecText = s_cfg_reader->GetString("Label", L"");
	int SubMenu = s_cfg_reader->GetInt("Submenu", 0);
	FARString strItemName=strRecText;
	InsertQuote(strItemName);

	if (Message(MSG_WARNING,2,Msg::UserMenuTitle,(!SubMenu?Msg::AskDeleteMenuItem:Msg::AskDeleteSubMenuItem),strItemName,Msg::Delete,Msg::Cancel))
		return FALSE;

	MenuModified=MenuNeedRefresh=true;
	strRegKey.Clear();
	strRegKey << MenuKey << L"/Item";
	FARString DefragPrefix(strRegKey);
	strRegKey << DeletePos;
	{
		ConfigWriter cfg_writer(FARString(strRegKey).GetMB());
		cfg_writer.RemoveSection();
		cfg_writer.DefragIndexedSections(DefragPrefix.GetMB().c_str());
	}
	ConfigReaderScope::Update(s_cfg_reader);
	return TRUE;
}

bool UserMenu::MoveMenuItem(const wchar_t *MenuKey,int Pos,int NewPos)
{
	if (Pos != NewPos)
	{
		ConfigWriter().MoveIndexedSection(
			StrPrintf("%ls/Item", MenuKey).c_str(), Pos, NewPos);
		MenuModified = MenuNeedRefresh=true;
	}
	ConfigReaderScope::Update(s_cfg_reader);
	return true;
}