ManaPlus
helpwindow.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "gui/windows/helpwindow.h"
25 
26 #include "configuration.h"
27 
28 #include "enums/gui/layouttype.h"
29 
30 #include "fs/paths.h"
31 
32 #include "fs/virtfs/tools.h"
33 
34 #include "gui/gui.h"
35 
37 
38 #include "gui/widgets/button.h"
40 #include "gui/widgets/layout.h"
41 #include "gui/widgets/scrollarea.h"
42 
43 #include "input/inputmanager.h"
44 
45 #include "utils/foreach.h"
46 #include "utils/gettext.h"
47 #include "utils/process.h"
48 
51 
52 #include "debug.h"
53 
55 
57  // TRANSLATORS: help window name
58  Window(_("Help"), Modal_false, nullptr, "help.xml"),
59  LinkHandler(),
61  // TRANSLATORS: help window. button.
62  mDYKButton(new Button(this, _("Did you know..."), "DYK",
63  BUTTON_SKIN, this)),
64  mBrowserBox(new StaticBrowserBox(this, Opaque_true,
65  "browserbox.xml")),
66  mScrollArea(new ScrollArea(this, mBrowserBox,
67  Opaque_true, "help_background.xml")),
68  mTagFileMap()
69 {
70  setMinWidth(300);
71  setMinHeight(220);
72  setContentSize(455, 350);
73  setWindowName("Help");
74  setCloseButton(true);
75  setResizable(true);
76  setStickyButtonLock(true);
77 
78  if (setupWindow != nullptr)
80 
81  setDefaultSize(500, 400, ImagePosition::CENTER, 0, 0);
82 
84 
86  if (gui != nullptr)
92 
93  place(4, 3, mDYKButton, 1, 1);
94  place(0, 0, mScrollArea, 5, 3).setPadding(3);
95 
96  Layout &layout = getLayout();
97  layout.setRowHeight(0, LayoutType::SET);
98 
100  loadTags();
101  enableVisibleSound(true);
102  widgetResized(Event(nullptr));
103 }
104 
105 void HelpWindow::action(const ActionEvent &event)
106 {
107  if (event.getId() == "DYK")
109 }
110 
111 void HelpWindow::handleLink(const std::string &link,
112  MouseEvent *const event A_UNUSED)
113 {
114  if (!strStartWith(link, "http://") && !strStartWith(link, "https://"))
115  {
116  // need use separate variable
117  const std::string helpFile = link; // NOLINT
118  loadHelp(helpFile);
119  }
120  else
121  {
122  openBrowser(link);
123  }
124 }
125 
126 void HelpWindow::loadHelp(const std::string &helpFile)
127 {
128  if (!checkPath(helpFile))
129  return;
131  loadFile("header");
132  loadFile(helpFile);
133  loadFile("footer");
137 }
138 
139 void HelpWindow::loadHelpSimple(const std::string &helpFile)
140 {
141  if (!checkPath(helpFile))
142  return;
144  loadFile(helpFile);
148 }
149 
150 void HelpWindow::loadFile(std::string file)
151 {
152  trim(file);
153  std::string helpPath = branding.getStringValue("helpPath");
154  if (helpPath.empty())
155  helpPath = paths.getStringValue("help");
156 
157  StringVect lines;
158  TranslationManager::translateFile(pathJoin(helpPath, file).append(".txt"),
159  translator, lines);
160 
161  for (size_t i = 0, sz = lines.size(); i < sz; ++i)
162  mBrowserBox->addRow(lines[i], false);
163 }
164 
166 {
167  std::string helpPath = branding.getStringValue("tagsPath");
168  if (helpPath.empty())
169  helpPath = paths.getStringValue("tags");
170 
171  StringVect filesVect;
172  VirtFs::getFilesInDir(helpPath, filesVect, ".idx");
173  FOR_EACH (StringVectCIter, itVect, filesVect)
174  {
175  StringVect lines;
176  VirtFs::loadTextFile(*itVect, lines);
177  FOR_EACH (StringVectCIter, it, lines)
178  {
179  const std::string &str = *it;
180  const size_t idx = str.find('|');
181  if (idx != std::string::npos)
182  mTagFileMap[str.substr(idx + 1)].insert(str.substr(0, idx));
183  }
184  }
185 }
186 
187 void HelpWindow::search(const std::string &text0)
188 {
189  std::string text = text0;
190  trim(text);
191  toLower(text);
192  if (mTagFileMap.find(text) == mTagFileMap.end())
193  {
194  loadHelp("searchnotfound");
195  }
196  else
197  {
198  const HelpNames &names = mTagFileMap[text];
199  if (names.size() == 1)
200  {
201  loadHelp(*names.begin());
202  }
203  else
204  {
205  if (translator == nullptr)
206  return;
208  loadFile("header");
209  loadFile("searchmany");
210  FOR_EACH (HelpNamesCIter, it, names)
211  {
212  const char *const str = (*it).c_str();
213  mBrowserBox->addRow(strprintf(" -> @@%s|%[email protected]@", str,
214  translator->getChar(str)),
215  false);
216  }
217  loadFile("footer");
221  }
222  }
223 }
const std::string BUTTON_SKIN
Definition: button.h:89
const std::string & getId() const
Definition: actionevent.h:122
Definition: button.h:102
std::string getStringValue(const std::string &key) const
Definition: event.h:79
Font * getHelpFont() const
Definition: gui.h:166
StaticBrowserBox * mBrowserBox
Definition: helpwindow.h:86
void search(const std::string &text)
Definition: helpwindow.cpp:187
void loadFile(std::string file)
Definition: helpwindow.cpp:150
void action(const ActionEvent &event)
Definition: helpwindow.cpp:105
Button * mDYKButton
Definition: helpwindow.h:85
ScrollArea * mScrollArea
Definition: helpwindow.h:87
void loadHelp(const std::string &helpFile)
Definition: helpwindow.cpp:126
void loadTags()
Definition: helpwindow.cpp:165
HelpTagsMap mTagFileMap
Definition: helpwindow.h:88
void handleLink(const std::string &link, MouseEvent *const event)
Definition: helpwindow.cpp:111
void loadHelpSimple(const std::string &helpFile)
Definition: helpwindow.cpp:139
void executeAction(const InputActionT keyNum)
LayoutCell & setPadding(int p)
Definition: layoutcell.h:60
void setRowHeight(const int n, const int h)
Definition: layoutcell.cpp:128
Definition: layout.h:45
const char * getChar(const char *const str)
Definition: podict.cpp:52
void setVerticalScrollAmount(const int vScroll)
void registerWindowForReset(Window *const window)
void addRow(const std::string &row, const bool atTop)
void setOpaque(Opaque opaque)
void setEnableKeys(const bool n)
void setEnableImages(const bool n)
void setEnableTabs(const bool n)
void setProcessVars(const bool n)
void setLinkHandler(LinkHandler *linkHandler)
static bool translateFile(const std::string &fileName, PoDict *const dict, StringVect &lines)
void setFont(Font *const font)
Definition: widget.cpp:349
Definition: window.h:102
void setResizable(const bool resize)
Definition: window.cpp:627
virtual void setVisible(Visible visible)
Definition: window.cpp:778
void setContentSize(int width, int height)
Definition: window.cpp:492
Layout & getLayout()
Definition: window.cpp:1365
void setWindowName(const std::string &name)
Definition: window.h:355
void setMinHeight(const int height)
Definition: window.cpp:604
void setMinWidth(const int width)
Definition: window.cpp:591
void enableVisibleSound(bool b)
Definition: window.h:481
void widgetResized(const Event &event)
Definition: window.cpp:655
void setCloseButton(const bool flag)
Definition: window.cpp:749
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:772
LayoutCell & place(const int x, const int y, Widget *const wg, const int w, const int h)
Definition: window.cpp:1384
void setDefaultSize()
Definition: window.cpp:1198
void loadWindowState()
Definition: window.cpp:1087
Configuration paths
Configuration branding
#define new
Definition: debug_new.h:147
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define _(s)
Definition: gettext.h:35
Gui * gui
Definition: gui.cpp:111
HelpWindow * helpWindow
Definition: helpwindow.cpp:54
std::set< std::string > HelpNames
Definition: helpwindow.h:36
HelpNames::const_iterator HelpNamesCIter
Definition: helpwindow.h:39
InputManager inputManager
#define nullptr
Definition: localconsts.h:45
#define A_UNUSED
Definition: localconsts.h:160
const bool Modal_false
Definition: modal.h:30
std::string trim(std::string const &str)
std::string toLower(std::string const &s)
bool loadTextFile(const std::string &fileName, StringVect &lines)
Definition: tools.cpp:137
void getFilesInDir(const std::string &dir, StringVect &list, const std::string &ext)
Definition: tools.cpp:81
const bool Opaque_false
Definition: opaque.h:30
const bool Opaque_true
Definition: opaque.h:30
bool checkPath(const std::string &path)
Definition: paths.cpp:121
PoDict * translator
Definition: podict.cpp:28
bool openBrowser(std::string url)
Definition: process.cpp:310
SetupWindow * setupWindow
Definition: setupwindow.cpp:64
std::string strprintf(const char *const format,...)
std::string pathJoin(std::string str1, const std::string &str2)
bool strStartWith(const std::string &str1, const std::string &str2)
StringVect::const_iterator StringVectCIter
Definition: stringvector.h:31
std::vector< std::string > StringVect
Definition: stringvector.h:29
const bool Visible_true
Definition: visible.h:30