1 /*
2    Vimpc
3    Copyright (C) 2010 - 2011 Nathan Sweetman
4 
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 
18    help.cpp - window to display help about commands
19    */
20 
21 #include "help.hpp"
22 
23 #ifndef HELP_DIRECTORY
24 #define HELP_DIRECTORY ""
25 #endif
26 
27 #include "algorithm.hpp"
28 #include "error.hpp"
29 #include "screen.hpp"
30 #include "settings.hpp"
31 #include "mode/search.hpp"
32 
33 #include <algorithm>
34 #include <fstream>
35 #include <string>
36 
37 char const * const Local    = "doc";
38 char const * const Specific = HELP_DIRECTORY;
39 char const * const HelpFile = "/help.txt";
40 
41 using namespace Ui;
42 
HelpWindow(Main::Settings const & settings,Ui::Screen & screen,Ui::Search const & search)43 HelpWindow::HelpWindow(Main::Settings const & settings, Ui::Screen & screen, Ui::Search const & search) :
44    SelectWindow     (settings, screen, "help"),
45    settings_        (settings),
46    search_          (search),
47    help_            ()
48 {
49    LoadHelpFile();
50 }
51 
~HelpWindow()52 HelpWindow::~HelpWindow()
53 {
54 }
55 
Redraw()56 void HelpWindow::Redraw()
57 {
58    Clear();
59    LoadHelpFile();
60 }
61 
Print(uint32_t line) const62 void HelpWindow::Print(uint32_t line) const
63 {
64    WINDOW * window = N_WINDOW();
65 
66    std::string const BlankLine(Columns(), ' ');
67    mvwprintw(window, line, 0, BlankLine.c_str());
68    wmove(window, line, 0);
69 
70    if ((FirstLine() + line) < help_.Size())
71    {
72       std::string currentLine = help_.Get(FirstLine() + line);
73 
74       if ((search_.LastSearchString() != "") && (settings_.Get(Setting::HighlightSearch) == true) &&
75           (search_.HighlightSearch() == true))
76       {
77          Regex::RE const expression(".*" + search_.LastSearchString() + ".*", search_.LastSearchOptions());
78 
79          if (expression.CompleteMatch(currentLine))
80          {
81             wattron(window, COLOR_PAIR(settings_.colours.SongMatch));
82          }
83       }
84 
85       if ((FirstLine() == 0) && (line == 0))
86       {
87          if (settings_.Get(Setting::ColourEnabled) == true)
88          {
89             wattron(window, COLOR_PAIR(settings_.colours.CurrentSong));
90          }
91 
92          wattron(window, A_BOLD);
93       }
94       else if (Algorithm::isUpper(currentLine) == true)
95       {
96          if (settings_.Get(Setting::ColourEnabled) == true)
97          {
98             wattron(window, COLOR_PAIR(settings_.colours.Directory));
99          }
100       }
101 
102       size_t const pos = currentLine.find('|');
103 
104       if ((pos != std::string::npos) &&
105          ((pos == 0) || (currentLine[pos - 1] != '\\')))
106       {
107          std::string firstHalf = currentLine.substr(0, currentLine.find_last_of('|') - 1);
108          std::string lastHalf = currentLine.substr(currentLine.find_last_of('|') + 1);
109 
110          mvwaddstr(window, line, 0, firstHalf.c_str());
111          waddstr(window, lastHalf.c_str());
112       }
113       else if ((pos != std::string::npos) &&
114               ((pos > 0) && (currentLine[pos - 1] == '\\')))
115       {
116          currentLine.erase(pos-1, 1);
117          mvwaddstr(window, line, 0, currentLine.c_str());
118       }
119       else
120       {
121          mvwaddstr(window, line, 0, currentLine.c_str());
122       }
123 
124       if ((search_.LastSearchString() != "") && (settings_.Get(Setting::HighlightSearch) == true) &&
125           (search_.HighlightSearch() == true))
126       {
127          Regex::RE const expression(".*" + search_.LastSearchString() + ".*", search_.LastSearchOptions());
128 
129          if (expression.CompleteMatch(currentLine))
130          {
131             wattroff(window, COLOR_PAIR(settings_.colours.SongMatch));
132          }
133       }
134    }
135 
136    if (settings_.Get(Setting::ColourEnabled) == true)
137    {
138       wattroff(window, COLOR_PAIR(settings_.colours.Directory) | COLOR_PAIR(settings_.colours.TabWindow));
139    }
140 
141    if ((FirstLine() == 0) && (line == 0))
142    {
143       wattroff(window, A_BOLD);
144    }
145 }
146 
Confirm()147 void HelpWindow::Confirm()
148 {
149 }
150 
Scroll(int32_t scrollCount)151 void HelpWindow::Scroll(int32_t scrollCount)
152 {
153    currentLine_ += scrollCount;
154    LimitCurrentSelection();
155    ScrollWindow::Scroll(scrollCount);
156 }
157 
ScrollTo(uint32_t scrollLine)158 void HelpWindow::ScrollTo(uint32_t scrollLine)
159 {
160    int64_t oldSelection = currentLine_;
161    currentLine_    = (static_cast<int64_t>(scrollLine));
162    LimitCurrentSelection();
163 
164    ScrollWindow::ScrollTo(scrollLine);
165 }
166 
167 
LoadHelpFile()168 void HelpWindow::LoadHelpFile()
169 {
170    std::string testFile(Local);
171 
172    testFile += HelpFile;
173 
174    std::ifstream testStream(testFile.c_str());
175    std::string   file;
176 
177    if (testStream.is_open() == true)
178    {
179       file = Local;
180    }
181    else
182    {
183       file = Specific;
184    }
185 
186    file += HelpFile;
187 
188    testStream.close();
189 
190    std::ifstream helpFile(file.c_str());
191 
192    if (helpFile.is_open() == true)
193    {
194       std::string nextLine;
195 
196       while (helpFile.eof() == false)
197       {
198          getline(helpFile, nextLine);
199 
200          if (helpFile.eof() == false)
201          {
202             help_.Add(nextLine);
203          }
204       }
205 
206       helpFile.close();
207    }
208    else
209    {
210       ErrorString(ErrorNumber::HelpFileNonexistant);
211    }
212 }
213 
Clear()214 void HelpWindow::Clear()
215 {
216    help_.Clear();
217 }
218 
219 /* vim: set sw=3 ts=3: */
220