NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
utils.cpp
Go to the documentation of this file.
1// Copyright Timothy Miller, 1999
2
3#include "gterm.hpp"
4#include "../../kernel/core/ui/language.hpp"
5#include "../../kernel/core/utils/tools.hpp"
6#include <wx/log.h>
7
8extern Language _guilang;
9
10using namespace std;
11
12
22int GenericTerminal::calc_color( int fg, int bg, int flags )
23{
24 return (flags & 15) | (fg << 4) | (bg << 8);
25}
26
27
38{
39 int c;
40 int x;
41
42 // prevent recursion for scrolls which cause exposures
43 if (doing_update)
44 return;
45#ifdef DO_LOG
46 wxLogDebug("Updating Terminal");
47#endif
48
49 doing_update = 1;
50
51 // Go through the current height (number of lines) of the terminal
52 for (int i = 0; i < height; i++)
53 {
54 // Get the rendered string and the rendered color bitlists for
55 // the current line
56 string line = tm.getRenderedString(i);
57 vector<unsigned short> colors = tm.getRenderedColors(i);
58 size_t lastPos = 0;
59
60 // Print all characters together, which have the same color
61 // (= a token in this context)
62 for (size_t j = 1; j < line.length(); j++)
63 {
64 if (colors[j] != colors[j - 1])
65 {
66 string sSubstr = line.substr(lastPos, j - lastPos);
67 c = colors[j - 1];
68 DrawText((c >> 4) & 0xf, (c >> 8) & 0xf, c, lastPos, i, sSubstr);
69 lastPos = j;
70 }
71 }
72
73 // Manually print the last token
74 if (line.length())
75 {
76 string sSubstr = line.substr(lastPos);
77 c = colors[lastPos];
78 DrawText((c >> 4) & 0xf, (c >> 8) & 0xf, c, lastPos, i, sSubstr);
79 }
80
81 // Clear the remaining line (otherwise one would read old text here)
82 ClearChars(bg_color, line.length(), i, width - line.length(), 1);
83 }
84
85 // Draw the cursor
86 if ( !(mode_flags & CURSORINVISIBLE) )
87 {
88 // Get its x coordinate and ensure that it is valid
89 x = termCursor.x;
90 if ( x >= width )
91 x = width - 1;
92
93 // Get color and the character at the position of the cursor
95 unsigned char cursorChar = tm.GetCharAdjusted(termCursor.y, x);
96
97 // Draw the cursor
98 DrawCursor((c >> 4) & 0xf, (c >> 8) & 0xf, c & 15, x, termCursor.y, cursorChar);
99 }
100 doing_update = 0;
101}
102
103
117void GenericTerminal::clear_area( int start_x, int start_y, int end_x, int end_y )
118{
119 tm.clearRange(ViewCursor(start_x, start_y), ViewCursor(end_x, end_y));
120}
121
122
135{
136 if ( termCursor.x >= (size_t)width )
137 termCursor.x = width - 1;
138
139 termCursor.move(x, y);
140
143
145}
146
147
158static inline bool hasContextToolTip(short int c, bool inParens)
159{
161 || (c == NumeReSyntax::SYNTAX_FUNCTION && inParens)
162 || (c == NumeReSyntax::SYNTAX_METHODS && inParens)
163 || (c == NumeReSyntax::SYNTAX_PROCEDURE && inParens);
164}
165
166
176static inline bool hasNonContextToolTip(short int c)
177{
180}
181
182
194{
195 // Do nothing, if the terminal is scrolled up
196 if (IsScrolledUp())
197 {
199 return;
200 }
201 // if the position is on a relevant syntax element
202 // create a calltip for this element, otherwise
203 // dismiss it
204 std::string sLine = tm.getRenderedString(y);
205 std::vector<unsigned short> vColors = tm.getRenderedColors(y);
206
207 if ((int)vColors.size() <= x || x < 0)
208 {
210 return;
211 }
212
213 // If we're not directly on a tool-tip specific element,
214 // then we'll search a context tooltip left of the current
215 // position.
216 if (!hasContextToolTip((vColors[x] >> 4) & 0xf, true) // to enable actual detection, we set us to be in a parenthesis
217 && !hasNonContextToolTip((vColors[x] >> 4) & 0xf))
218 {
219 bool isInParens = false;
220 size_t cursor_x = x;
221
222 while (x >= 0)
223 {
224 if (((vColors[x] >> 4) & 0xf) == NumeReSyntax::SYNTAX_OPERATOR && (sLine[x] == '(' || sLine[x] =='{'))
225 isInParens = getMatchingParenthesis(StringView(sLine, x)) >= cursor_x-x;
226
227 // Functions and commands
228 if (hasContextToolTip((vColors[x] >> 4) & 0xf, isInParens))
229 break;
230
231 // options and constants (only near cursor)
232 if (cursor_x-x <= 2
233 && hasNonContextToolTip((vColors[x] >> 4) & 0xf)
234 && (sLine[cursor_x] == ' ' || ((vColors[cursor_x] >> 4) & 0xf) == NumeReSyntax::SYNTAX_OPERATOR))
235 break;
236
237 x--;
238 }
239 }
240
241 // If we found nothing, return
242 if (x < 0)
243 {
245 return;
246 }
247
248 int posStart = x;
249 size_t posEnd = x;
250
251 while (posStart > 0 && vColors[posStart-1] == vColors[x])
252 posStart--;
253
254 while (posEnd < vColors.size() && vColors[posEnd] == vColors[x])
255 posEnd++;
256
257 std::string sSyntaxElement = sLine.substr(posStart, posEnd - posStart);
258 NumeRe::CallTip _cTip;
259
260 // Determine the type of the color
261 switch ((vColors[x] >> 4) & 0xf)
262 {
264 _cTip = m_tipProvider.getCommand(sSyntaxElement);
265 break;
267 _cTip = m_tipProvider.getFunction(sSyntaxElement);
268 break;
270 _cTip = m_tipProvider.getProcedure(sSyntaxElement);
271 break;
273 _cTip = m_tipProvider.getMethod(sSyntaxElement);
274 break;
276 _cTip = m_tipProvider.getConstant(sSyntaxElement);
277 break;
279 _cTip = m_tipProvider.getOption(sSyntaxElement);
280 break;
281 default:
283 return;
284 }
285
286 // Do not display empty calltips
287 if (!_cTip.sDefinition.length())
288 {
290 return;
291 }
292
293 Calltip(posStart, y, _cTip);
294}
295
306{
307 if (tm.IsEditable(y, x))
308 move_cursor(x, y);
309}
310
311
321{
322 mode_flags |= flag;
323
325}
326
327
337{
338 mode_flags &= ~flag;
339
341}
void clear_mode_flag(int flag)
Clears a mode flag (mainly used to make the cursor visible again).
Definition: utils.cpp:336
int calc_color(int fg, int bg, int flags)
Returns the encoded color bitlist.
Definition: utils.cpp:22
virtual void ClearChars(int bg_color, int x, int y, int w, int h)
Definition: gterm.hpp:192
void set_mode_flag(int flag)
Sets a mode flag (only used to make the cursor invisble).
Definition: utils.cpp:320
ViewCursor termCursor
Definition: gterm.hpp:85
virtual void CalltipCancel()
Definition: gterm.hpp:189
void move_cursor(int x, int y)
Definition: utils.cpp:134
virtual void ModeChange(int state)
Definition: gterm.hpp:198
void update_changes()
Definition: utils.cpp:37
int doing_update
Definition: gterm.hpp:103
virtual void DrawCursor(int fg_color, int bg_color, int flags, int x, int y, unsigned char c)=0
virtual void Calltip(int x, int y, NumeRe::CallTip &_cTip)
Definition: gterm.hpp:188
TextManager tm
Definition: gterm.hpp:80
NumeRe::CallTipProvider m_tipProvider
Definition: gterm.hpp:82
void handle_calltip(int x, int y)
Check, whether a calltip is needed and select the corresponding text from the CallTipProvider.
Definition: utils.cpp:193
void clear_area(int start_x, int start_y, int end_x, int end_y)
Definition: utils.cpp:117
void move_cursor_editable_area(int x, int y)
Moves the cursor to a location, if this location is editable.
Definition: utils.cpp:305
bool IsScrolledUp()
Determine, whether the terminal is scrolled up.
Definition: gterm.cpp:298
virtual void DrawText(int fg_color, int bg_color, int flags, int x, int y, const std::string &sText)=0
int mode_flags
Definition: gterm.hpp:87
This class handles the internal language system and returns the language strings of the selected lang...
Definition: language.hpp:38
CallTip getProcedure(std::string sToken) const
Get the calltip for the selected (global) procedure.
CallTip getOption(std::string sToken) const
Get the calltip for the selected option.
CallTip getConstant(std::string sToken) const
Get the calltip for the selected constant.
CallTip getMethod(std::string sToken) const
Get the calltip for the selected method.
CallTip getFunction(std::string sToken) const
Get the calltip for the selected (built-in) function.
CallTip getCommand(std::string sToken) const
Get the calltip for the selected command.
@ SYNTAX_OPTION
Definition: syntax.hpp:92
@ SYNTAX_FUNCTION
Definition: syntax.hpp:93
@ SYNTAX_METHODS
Definition: syntax.hpp:102
@ SYNTAX_PROCEDURE
Definition: syntax.hpp:99
@ SYNTAX_CONSTANT
Definition: syntax.hpp:94
@ SYNTAX_COMMAND
Definition: syntax.hpp:91
@ SYNTAX_OPERATOR
Definition: syntax.hpp:98
This class is the immutable (const) version of a string view. It can be constructed from a MutableStr...
bool clearRange(const ViewCursor &cursor1, const ViewCursor &cursor2)
Clears the range between two view cursors.
unsigned short GetColorAdjusted(int y, int x) const
std::string getRenderedString(size_t viewLine) const
Return the rendered line for the current viewport setting.
std::vector< unsigned short > getRenderedColors(size_t viewLine) const
Return the rendered colors for the selected viewport line.
char GetCharAdjusted(int y, int x) const
ViewCursor getCurrentViewPos() const
Returns the current cursor position as view cursor.
bool IsEditable(int y, int x) const
Determines, whether the character at (x,y) is editable text.
LogicalCursor toLogicalCursor(const ViewCursor &viewCursor) const
Convert a view cursor into a logical cursor.
unsigned int getMatchingParenthesis(const StringView &)
Returns the position of the closing parenthesis.
Definition: tools.cpp:414
This structure contains the data for a single calltip, which might be shown in the editor or the term...
std::string sDefinition
Cursor, which is used in the terminal. The TextManager is able to convert this cursor into a LogicalC...
Definition: TextManager.h:124
size_t y
Definition: TextManager.h:126
size_t x
Definition: TextManager.h:125
void move(size_t _x, size_t _y)
Definition: TextManager.h:131
static bool hasContextToolTip(short int c, bool inParens)
Returns true, if a color corresponds to a syntax element, which has a (context, e....
Definition: utils.cpp:158
Language _guilang
static bool hasNonContextToolTip(short int c)
Returns true, if a color corresponds to a syntax element, which doesn't have a (context,...
Definition: utils.cpp:176