Merge pull request #2212 from unxed/ctrl_yo
[far2l.git] / far2l / src / ctrlobj.cpp
blobb73acb6f946cb18bb879e75fee89d5b34dac2ea5
1 /*
2 ctrlobj.cpp
4 Управление остальными объектами, раздача сообщений клавиатуры и мыши
5 */
6 /*
7 Copyright (c) 1996 Eugene Roshal
8 Copyright (c) 2000 Far Group
9 All rights reserved.
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions
13 are met:
14 1. Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
16 2. Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in the
18 documentation and/or other materials provided with the distribution.
19 3. The name of the authors may not be used to endorse or promote products
20 derived from this software without specific prior written permission.
22 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "headers.hpp"
36 #include "ctrlobj.hpp"
37 #include "lang.hpp"
38 #include "language.hpp"
39 #include "manager.hpp"
40 #include "cmdline.hpp"
41 #include "hilight.hpp"
42 #include "poscache.hpp"
43 #include "history.hpp"
44 #include "treelist.hpp"
45 #include "filefilter.hpp"
46 #include "filepanels.hpp"
47 #include "syslog.hpp"
48 #include "interf.hpp"
49 #include "config.hpp"
50 #include "ConfigOptSaveLoad.hpp"
51 #include "fileowner.hpp"
52 #include "dirmix.hpp"
53 #include "console.hpp"
55 #include "farversion.h"
57 ControlObject *CtrlObject;
59 ControlObject::ControlObject()
61 FPanels(0),
62 CmdLine(0),
63 CmdHistory(0),
64 FolderHistory(0),
65 ViewHistory(0),
66 MainKeyBar(0),
67 TopMenuBar(0),
68 HiFiles(0),
69 ViewerPosCache(0),
70 EditorPosCache(0)
72 _OT(SysLog(L"[%p] ControlObject::ControlObject()", this));
73 CtrlObject = this;
74 HiFiles = new HighlightFiles;
75 ViewerPosCache = new FilePositionCache(FPCK_VIEWER);
76 EditorPosCache = new FilePositionCache(FPCK_EDITOR);
77 FrameManager = new Manager;
78 // Macro.LoadMacros();
79 ApplyConfig();
80 CmdHistory = new History(HISTORYTYPE_CMD, Opt.HistoryCount, "SavedHistory", &Opt.SaveHistory, false);
81 FolderHistory = new History(HISTORYTYPE_FOLDER, Opt.FoldersHistoryCount, "SavedFolderHistory",
82 &Opt.SaveFoldersHistory, true);
83 ViewHistory = new History(HISTORYTYPE_VIEW, Opt.ViewHistoryCount, "SavedViewHistory",
84 &Opt.SaveViewHistory, true);
85 FolderHistory->SetAddMode(true, 2, true);
86 ViewHistory->SetAddMode(true, 1, true);
89 void ControlObject::Init()
91 TreeList::ClearCache(0);
92 SetColor(COL_COMMANDLINEUSERSCREEN);
93 GotoXY(0, ScrY - 3);
94 ShowStartupBanner();
95 GotoXY(0, ScrY - 2);
96 MoveCursor(0, ScrY - 1);
97 FPanels = new FilePanels();
98 CmdLine = new CommandLine();
99 CmdLine->SaveBackground(0, 0, ScrX, ScrY);
100 this->MainKeyBar = &(FPanels->MainKeyBar);
101 this->TopMenuBar = &(FPanels->TopMenuBar);
102 FPanels->Init();
103 FPanels->SetScreenPosition();
105 if (Opt.ShowMenuBar)
106 this->TopMenuBar->Show();
108 // FPanels->Redraw();
109 CmdLine->Show();
111 this->MainKeyBar->Refresh(Opt.ShowKeyBar);
113 FrameManager->InsertFrame(FPanels);
114 FrameManager->PluginCommit();
116 Cp()->LeftPanel->Update(0);
117 Cp()->RightPanel->Update(0);
119 Cp()->LeftPanel->GoToFile(Opt.strLeftCurFile);
120 Cp()->RightPanel->GoToFile(Opt.strRightCurFile);
122 FARString strStartCurDir;
123 Cp()->ActivePanel->GetCurDir(strStartCurDir);
124 FarChDir(strStartCurDir);
125 Cp()->ActivePanel->SetFocus();
127 FARString strOldTitle;
128 Console.GetTitle(strOldTitle);
129 FrameManager->PluginCommit();
130 Plugins.LoadPlugins();
131 Console.SetTitle(strOldTitle);
133 Macro.LoadMacros();
135 auto *CurFrame = FrameManager->GetCurrentFrame();
136 if (LIKELY(CurFrame))
137 CurFrame->Show(); // otherwise panels displayed empty on start sometimes
139 FarChDir(StartCurDir);
141 //_SVS(SysLog(L"ActivePanel->GetCurDir='%ls'",StartCurDir));
142 //_SVS(char PPP[NM];Cp()->GetAnotherPanel(Cp()->ActivePanel)->GetCurDir(PPP);SysLog(L"AnotherPanel->GetCurDir='%ls'",PPP));
145 void ControlObject::CreateFilePanels()
147 FPanels = new FilePanels();
150 ControlObject::~ControlObject()
152 if (CriticalInternalError)
153 return;
155 _OT(SysLog(L"[%p] ControlObject::~ControlObject()", this));
157 if (Cp() && Cp()->ActivePanel) {
158 if (Opt.AutoSaveSetup)
159 ConfigOptSave(false);
161 if (Cp()->ActivePanel->GetMode() != PLUGIN_PANEL) {
162 FARString strCurDir;
163 Cp()->ActivePanel->GetCurDir(strCurDir);
164 FolderHistory->AddToHistory(strCurDir);
168 FrameManager->CloseAll();
169 FPanels = nullptr;
170 FileFilter::CloseFilter();
171 delete CmdHistory;
172 delete FolderHistory;
173 delete ViewHistory;
174 delete CmdLine;
175 delete HiFiles;
177 delete ViewerPosCache;
178 delete EditorPosCache;
180 delete FrameManager;
181 TreeList::FlushCache();
182 Lang.Close();
183 CtrlObject = nullptr;
186 void ControlObject::ShowStartupBanner(LPCWSTR EmergencyMsg)
188 std::vector<FARString> Lines;
190 std::string tmp_mb;
191 for (const char *p = Copyright; *p; ++p) {
192 if (*p == '\n') {
193 Lines.emplace_back(tmp_mb);
194 tmp_mb.clear();
195 } else {
196 tmp_mb+= *p;
199 if (!tmp_mb.empty()) {
200 Lines.emplace_back(tmp_mb);
203 COORD Size{}, CursorPosition{};
204 WORD SavedAttr{};
205 Console.GetSize(Size);
206 Console.GetCursorPosition(CursorPosition);
207 Console.GetTextAttributes(SavedAttr);
209 if (EmergencyMsg) {
210 for (const auto &Line : Lines) {
211 Console.Write(Line, static_cast<DWORD>(Line.GetLength()));
212 CursorPosition.Y++;
213 Console.SetCursorPosition(CursorPosition);
216 Console.SetTextAttributes(F_YELLOW | B_BLACK);
217 Console.Write(EmergencyMsg, wcslen(EmergencyMsg));
218 CursorPosition.Y++;
219 Console.SetCursorPosition(CursorPosition);
220 Console.SetTextAttributes(SavedAttr);
222 } else {
223 Lines.emplace_back();
224 const size_t ConsoleHintsIndex = Lines.size();
225 Lines.reserve(ConsoleHintsIndex + 12);
226 Lines.emplace_back(Msg::VTStartTipNoCmdTitle);
227 Lines.emplace_back(Msg::VTStartTipNoCmdCtrlO);
228 Lines.emplace_back(Msg::VTStartTipNoCmdCtrlArrow);
229 Lines.emplace_back(Msg::VTStartTipNoCmdShiftTAB);
230 Lines.emplace_back(Msg::VTStartTipNoCmdFn);
231 Lines.emplace_back(Msg::VTStartTipNoCmdMouse);
232 Lines.emplace_back(Msg::VTStartTipPendCmdTitle);
233 Lines.emplace_back(Msg::VTStartTipPendCmdFn);
234 Lines.emplace_back(Msg::VTStartTipPendCmdCtrlAltC);
235 Lines.emplace_back(Msg::VTStartTipPendCmdCtrlAltZ);
236 Lines.emplace_back(Msg::VTStartTipPendCmdMouse);
237 Lines.emplace_back(Msg::VTStartTipMouseSelect);
239 const int FreeSpace = Size.Y - CursorPosition.Y - 1;
240 const int LineCount = 4 + Lines.size();
242 if (FreeSpace < LineCount)
243 ScrollScreen(LineCount - FreeSpace);
245 const auto SavedColor = GetColor();
246 for (size_t i = 0; i < Lines.size(); ++i) {
247 if (i >= ConsoleHintsIndex) {
248 SetColor(Lines[i].Begins(L' ') ? COL_HELPTEXT : COL_HELPTOPIC); // COL_HELPBOXTITLE
250 if (!Lines[i].IsEmpty()) {
251 GotoXY(0, ScrY - (Lines.size() - i + 2));
252 Text(Lines[i]);
255 SetRealColor(SavedColor);
259 FilePanels *ControlObject::Cp()
261 return FPanels;