window: adjust indentation

master
Andrzej Rybczak 14 years ago
parent d47cad1d50
commit 337a27c366
  1. 942
      src/window.h

@ -123,497 +123,497 @@
/// ///
namespace NC namespace NC
{ {
/// Colors used by NCurses /// Colors used by NCurses
///
enum Color { clDefault, clBlack, clRed, clGreen, clYellow, clBlue, clMagenta, clCyan, clWhite, clEnd };
/// Format flags used by NCurses
///
enum Format {
fmtNone = clEnd+1,
fmtBold, fmtBoldEnd,
fmtUnderline, fmtUnderlineEnd,
fmtReverse, fmtReverseEnd,
fmtAltCharset, fmtAltCharsetEnd
};
/// Available border colors for window
///
enum Border { brNone, brBlack, brRed, brGreen, brYellow, brBlue, brMagenta, brCyan, brWhite };
/// This indicates how much the window has to be scrolled
///
enum Where { wUp, wDown, wPageUp, wPageDown, wHome, wEnd };
/// Helper function that is invoked each time one will want
/// to obtain string from Window::GetString() function
/// @see Window::GetString()
///
typedef std::function<void(const std::wstring &)> GetStringHelper;
/// Initializes curses screen and sets some additional attributes
/// @param window_title title of the window (has an effect only if pdcurses lib is used)
/// @param enable_colors enables colors
///
void InitScreen(const char *window_title, bool enable_colors);
/// Destroys the screen
///
void DestroyScreen();
/// Struct used to set color of both foreground and background of window
/// @see Window::operator<<()
///
struct Colors
{
Colors(Color one, Color two = clDefault) : fg(one), bg(two) { }
Color fg;
Color bg;
};
/// Struct used for going to given coordinates
/// @see Window::operator<<()
///
struct XY
{
XY(int xx, int yy) : x(xx), y(yy) { }
int x;
int y;
};
/// Main class of NCurses namespace, used as base for other specialized windows
///
struct Window
{
/// Constructs an empty window with given parameters
/// @param startx X position of left upper corner of constructed window
/// @param starty Y position of left upper corner of constructed window
/// @param width width of constructed window
/// @param height height of constructed window
/// @param title title of constructed window
/// @param color base color of constructed window
/// @param border border of constructed window
/// ///
enum Color { clDefault, clBlack, clRed, clGreen, clYellow, clBlue, clMagenta, clCyan, clWhite, clEnd }; Window(size_t startx, size_t starty, size_t width, size_t height,
const std::string &title, Color color, Border border);
/// Format flags used by NCurses /// Copies thw window
/// @param w copied window
/// ///
enum Format { Window(const Window &w);
fmtNone = clEnd+1,
fmtBold, fmtBoldEnd,
fmtUnderline, fmtUnderlineEnd,
fmtReverse, fmtReverseEnd,
fmtAltCharset, fmtAltCharsetEnd
};
/// Available border colors for window /// Destroys the window and frees memory
/// ///
enum Border { brNone, brBlack, brRed, brGreen, brYellow, brBlue, brMagenta, brCyan, brWhite }; virtual ~Window();
/// This indicates how much the window has to be scrolled /// Allows for direct access to internal WINDOW pointer in case there
/// is no wrapper for a function from curses library one may want to use
/// @return internal WINDOW pointer
/// ///
enum Where { wUp, wDown, wPageUp, wPageDown, wHome, wEnd }; WINDOW *Raw() const { return itsWindow; }
/// Helper function that is invoked each time one will want /// @return window's width
/// to obtain string from Window::GetString() function
/// @see Window::GetString()
/// ///
typedef std::function<void(const std::wstring &)> GetStringHelper; size_t GetWidth() const;
/// Initializes curses screen and sets some additional attributes /// @return window's height
/// @param window_title title of the window (has an effect only if pdcurses lib is used)
/// @param enable_colors enables colors
/// ///
void InitScreen(const char *window_title, bool enable_colors); size_t GetHeight() const;
/// Destroys the screen /// @return X position of left upper window's corner
/// ///
void DestroyScreen(); size_t GetStartX() const;
/// Struct used to set color of both foreground and background of window /// @return Y position of left upper window's corner
/// @see Window::operator<<()
/// ///
struct Colors size_t GetStartY() const;
{
Colors(Color one, Color two = clDefault) : fg(one), bg(two) { }
Color fg;
Color bg;
};
/// Struct used for going to given coordinates /// @return window's title
/// @see Window::operator<<()
/// ///
struct XY const std::string &getTitle() const;
{
XY(int xx, int yy) : x(xx), y(yy) { } /// @return current window's color
int x; ///
int y; Color GetColor() const;
};
/// Main class of NCurses namespace, used as base for other specialized windows /// @return current window's border
/// ///
class Window Border GetBorder() const;
/// @return current window's timeout
///
int GetTimeout() const;
/// Reads the string from standard input. Note that this is much more complex
/// function than getstr() from curses library. It allows for moving through
/// letters with arrows, supports scrolling if string's length is bigger than
/// given area, supports history of previous strings and each time it receives
/// an input from the keyboard or the timeout is reached, it calls helper function
/// (if it's set) that takes as an argument currently edited string.
/// @param base base string that has to be edited
/// @param length max length of string, unlimited by default
/// @param width width of area that entry field can take. if it's reached, string
/// will be scrolled. if value is 0, field will be from cursor position to the end
/// of current line wide.
/// @param encrypted if set to true, '*' characters will be displayed instead of
/// actual text.
/// @return edited string
///
/// @see SetGetStringHelper()
/// @see SetTimeout()
/// @see CreateHistory()
///
std::string GetString(const std::string &base, size_t length = -1,
size_t width = 0, bool encrypted = 0);
/// Wrapper for above function that doesn't take base string (it will be empty).
/// Taken parameters are the same as for above.
///
std::string GetString(size_t length = -1, size_t width = 0, bool encrypted = 0)
{ {
public: return GetString("", length, width, encrypted);
/// Constructs an empty window with given parameters }
/// @param startx X position of left upper corner of constructed window
/// @param starty Y position of left upper corner of constructed window /// Moves cursor to given coordinates
/// @param width width of constructed window /// @param x given X position
/// @param height height of constructed window /// @param y given Y position
/// @param title title of constructed window ///
/// @param color base color of constructed window void GotoXY(int x, int y);
/// @param border border of constructed window
/// /// @return x window coordinate
Window(size_t startx, size_t starty, size_t width, size_t height, /// @see GetXY()
const std::string &title, Color color, Border border); ///
int X();
/// Copies thw window
/// @param w copied window /// @return y windows coordinate
/// /// @see GetXY()
Window(const Window &w); ///
int Y();
/// Destroys the window and frees memory
/// /// Used to indicate whether given coordinates of main screen lies within
virtual ~Window(); /// window area or not and if they do, transform them into in-window coords.
/// Otherwise function doesn't modify its arguments.
/// Allows for direct access to internal WINDOW pointer in case there /// @param x X position of main screen to be checked
/// is no wrapper for a function from curses library one may want to use /// @param y Y position of main screen to be checked
/// @return internal WINDOW pointer /// @return true if it transformed variables, false otherwise
/// ///
WINDOW *Raw() const { return itsWindow; } bool hasCoords(int &x, int &y);
/// @return window's width /// Sets helper function used in GetString()
/// /// @param helper pointer to function that matches GetStringHelper prototype
size_t GetWidth() const; /// @see GetString()
///
/// @return window's height void SetGetStringHelper(GetStringHelper helper) { itsGetStringHelper = helper; }
///
size_t GetHeight() const; /// Sets window's base color
/// @param fg foregound base color
/// @return X position of left upper window's corner /// @param bg background base color
/// ///
size_t GetStartX() const; void SetBaseColor(Color fg, Color bg = clDefault);
/// @return Y position of left upper window's corner /// Sets window's border
/// /// @param border new window's border
size_t GetStartY() const; ///
void SetBorder(Border border);
/// @return window's title
/// /// Sets window's timeout
const std::string &getTitle() const; /// @param timeout window's timeout
///
/// @return current window's color void SetTimeout(int timeout);
///
Color GetColor() const; /// Sets window's title
/// @param new_title new title for window
/// @return current window's border ///
/// void SetTitle(const std::string &new_title);
Border GetBorder() const;
/// Creates internal container that stores all previous
/// @return current window's timeout /// strings that were edited using this window.
/// ///
int GetTimeout() const; void CreateHistory();
/// Reads the string from standard input. Note that this is much more complex /// Deletes container with all previous history entries
/// function than getstr() from curses library. It allows for moving through ///
/// letters with arrows, supports scrolling if string's length is bigger than void DeleteHistory();
/// given area, supports history of previous strings and each time it receives
/// an input from the keyboard or the timeout is reached, it calls helper function /// "Hides" the window by filling its area with given character
/// (if it's set) that takes as an argument currently edited string. /// @param ch character to fill the area
/// @param base base string that has to be edited /// @see Clear()
/// @param length max length of string, unlimited by default ///
/// @param width width of area that entry field can take. if it's reached, string void Hide(char ch = 32) const;
/// will be scrolled. if value is 0, field will be from cursor position to the end
/// of current line wide. /// Refreshed whole window and its border
/// @param encrypted if set to true, '*' characters will be displayed instead of /// @see Refresh()
/// actual text. ///
/// @return edited string void Display();
///
/// @see SetGetStringHelper() /// Refreshes whole window, but not the border
/// @see SetTimeout() /// @see Display()
/// @see CreateHistory() ///
/// virtual void Refresh();
std::string GetString(const std::string &base, size_t length = -1,
size_t width = 0, bool encrypted = 0); /// Moves the window to new coordinates
/// @param new_x new X position of left upper corner of window
/// Wrapper for above function that doesn't take base string (it will be empty). /// @param new_y new Y position of left upper corner of window
/// Taken parameters are the same as for above. ///
/// virtual void MoveTo(size_t new_x, size_t new_y);
std::string GetString(size_t length = -1, size_t width = 0, bool encrypted = 0)
{ /// Resizes the window
return GetString("", length, width, encrypted); /// @param new_width new window's width
} /// @param new_height new window's height
///
/// Moves cursor to given coordinates virtual void Resize(size_t new_width, size_t new_height);
/// @param x given X position
/// @param y given Y position /// Cleares the window
/// ///
void GotoXY(int x, int y); virtual void Clear();
/// @return x window coordinate /// Adds given file descriptor to the list that will be polled in
/// @see GetXY() /// ReadKey() along with stdin and callback that will be invoked
/// /// when there is data waiting for reading in it
int X(); /// @param fd file descriptor
/// @param callback callback
/// @return y windows coordinate ///
/// @see GetXY() void AddFDCallback(int fd, void (*callback)());
///
int Y(); /// Clears list of file descriptors and their callbacks
///
/// Used to indicate whether given coordinates of main screen lies within void ClearFDCallbacksList();
/// window area or not and if they do, transform them into in-window coords.
/// Otherwise function doesn't modify its arguments. /// Checks if list of file descriptors is empty
/// @param x X position of main screen to be checked /// @return true if list is empty, false otherwise
/// @param y Y position of main screen to be checked ///
/// @return true if it transformed variables, false otherwise bool FDCallbacksListEmpty() const;
///
bool hasCoords(int &x, int &y); /// Reads key from standard input (or takes it from input queue)
/// and writes it into read_key variable
/// Sets helper function used in GetString() ///
/// @param helper pointer to function that matches GetStringHelper prototype int ReadKey();
/// @see GetString()
/// /// Push single character into input queue, so it can get consumed by ReadKey
void SetGetStringHelper(GetStringHelper helper) { itsGetStringHelper = helper; } void PushChar(int ch);
/// Sets window's base color /// Scrolls the window by amount of lines given in its parameter
/// @param fg foregound base color /// @param where indicates how many lines it has to scroll
/// @param bg background base color ///
/// virtual void Scroll(Where where);
void SetBaseColor(Color fg, Color bg = clDefault);
/// Applies function of compatible prototype to internal WINDOW pointer
/// Sets window's border /// The mostly used function in this case seem to be wclrtoeol(), which
/// @param border new window's border /// clears the window from current cursor position to the end of line.
/// /// Note that delwin() also matches that prototype, but I wouldn't
void SetBorder(Border border); /// recommend anyone passing this pointer here ;)
/// @param f pointer to function to call with internal WINDOW pointer
/// Sets window's timeout /// @return reference to itself
/// @param timeout window's timeout ///
/// Window &operator<<(int (*f)(WINDOW *));
void SetTimeout(int timeout);
/// Applies foreground and background colors to window
/// Sets window's title /// @param colors struct that holds new colors information
/// @param new_title new title for window /// @return reference to itself
/// ///
void SetTitle(const std::string &new_title); Window &operator<<(Colors colors);
/// Creates internal container that stores all previous /// Applies foregound color to window. Note that colors applied
/// strings that were edited using this window. /// that way are stacked, i.e if you applied clRed, then clGreen
/// /// and clEnd, current color would be clRed. If you want to discard
void CreateHistory(); /// all colors and fall back to base one, pass clDefault.
/// @param color new color value
/// Deletes container with all previous history entries /// @return reference to itself
/// ///
void DeleteHistory(); Window &operator<<(Color color);
/// "Hides" the window by filling its area with given character /// Applies format flag to window. Note that these attributes are
/// @param ch character to fill the area /// also stacked, so if you applied fmtBold twice, to get rid of
/// @see Clear() /// it you have to pass fmtBoldEnd also twice.
/// /// @param format format flag
void Hide(char ch = 32) const; /// @return reference to itself
///
/// Refreshed whole window and its border Window &operator<<(Format format);
/// @see Refresh()
/// /// Moves current cursor position to given coordinates.
void Display(); /// @param coords struct that holds information about new coordinations
/// @return reference to itself
/// Refreshes whole window, but not the border ///
/// @see Display() Window &operator<<(XY coords);
///
virtual void Refresh(); /// Prints string to window
/// @param s const pointer to char array to be printed
/// Moves the window to new coordinates /// @return reference to itself
/// @param new_x new X position of left upper corner of window ///
/// @param new_y new Y position of left upper corner of window Window &operator<<(const char *s);
///
virtual void MoveTo(size_t new_x, size_t new_y); /// Prints single character to window
/// @param c character to be printed
/// Resizes the window /// @return reference to itself
/// @param new_width new window's width ///
/// @param new_height new window's height Window &operator<<(char c);
///
virtual void Resize(size_t new_width, size_t new_height); /// Prints wide string to window
/// @param ws const pointer to wchar_t array to be printed
/// Cleares the window /// @return reference to itself
/// ///
virtual void Clear(); Window &operator<<(const wchar_t *ws);
/// Adds given file descriptor to the list that will be polled in /// Prints single wide character to window
/// ReadKey() along with stdin and callback that will be invoked /// @param wc wide character to be printed
/// when there is data waiting for reading in it /// @return reference to itself
/// @param fd file descriptor ///
/// @param callback callback Window &operator<<(wchar_t wc);
///
void AddFDCallback(int fd, void (*callback)()); /// Prints int to window
/// @param i integer value to be printed
/// Clears list of file descriptors and their callbacks /// @return reference to itself
/// ///
void ClearFDCallbacksList(); Window &operator<<(int i);
/// Checks if list of file descriptors is empty /// Prints double to window
/// @return true if list is empty, false otherwise /// @param d double value to be printed
/// /// @return reference to itself
bool FDCallbacksListEmpty() const; ///
Window &operator<<(double d);
/// Reads key from standard input (or takes it from input queue)
/// and writes it into read_key variable /// Prints size_t to window
/// /// @param s size value to be printed
int ReadKey(); /// @return reference to itself
///
/// Push single character into input queue, so it can get consumed by ReadKey Window &operator<<(size_t s);
void PushChar(int ch);
/// Prints std::string to window
/// Scrolls the window by amount of lines given in its parameter /// @param s string to be printed
/// @param where indicates how many lines it has to scroll /// @return reference to itself
/// ///
virtual void Scroll(Where where); Window &operator<<(const std::string &s);
/// Applies function of compatible prototype to internal WINDOW pointer /// Prints std::wstring to window
/// The mostly used function in this case seem to be wclrtoeol(), which /// @param ws wide string to be printed
/// clears the window from current cursor position to the end of line. /// @return reference to itself
/// Note that delwin() also matches that prototype, but I wouldn't ///
/// recommend anyone passing this pointer here ;) Window &operator<<(const std::wstring &ws);
/// @param f pointer to function to call with internal WINDOW pointer
/// @return reference to itself /// Fallback for Length() for wide strings used if unicode support is disabled
/// /// @param s string that real length has to be measured
Window &operator<<(int (*f)(WINDOW *)); /// @return standard std::string::length() result since it's only fallback
///
/// Applies foreground and background colors to window static size_t Length(const std::string &s) { return s.length(); }
/// @param colors struct that holds new colors information
/// @return reference to itself /// Measures real length of wide string (required if e.g. asian characters are used)
/// /// @param ws wide string that real length has to be measured
Window &operator<<(Colors colors); /// @return real length of wide string
///
/// Applies foregound color to window. Note that colors applied static size_t Length(const std::wstring &ws);
/// that way are stacked, i.e if you applied clRed, then clGreen
/// and clEnd, current color would be clRed. If you want to discard /// Cuts string so it fits desired length on the screen. Note that it uses
/// all colors and fall back to base one, pass clDefault. /// wcwidth to check real width of all characters it contains. If string
/// @param color new color value /// fits requested length it's not modified at all.
/// @return reference to itself /// @param ws wide string to be cut
/// /// @param max_len maximal length of string
Window &operator<<(Color color); static void Cut(std::wstring &ws, size_t max_len);
/// Applies format flag to window. Note that these attributes are protected:
/// also stacked, so if you applied fmtBold twice, to get rid of /// Sets colors of window (interal use only)
/// it you have to pass fmtBoldEnd also twice. /// @param fg foregound color
/// @param format format flag /// @param bg background color
/// @return reference to itself ///
/// void SetColor(Color fg, Color bg = clDefault);
Window &operator<<(Format format);
/// Refreshes window's border
/// Moves current cursor position to given coordinates. ///
/// @param coords struct that holds information about new coordinations void ShowBorder() const;
/// @return reference to itself
/// /// Changes dimensions of window, called from Resize()
Window &operator<<(XY coords); /// @param width new window's width
/// @param height new window's height
/// Prints string to window /// @see Resize()
/// @param s const pointer to char array to be printed ///
/// @return reference to itself void AdjustDimensions(size_t width, size_t height);
///
Window &operator<<(const char *s); /// Deletes old window and creates new. It's called by Resize(),
/// SetBorder() or SetTitle() since internally windows are
/// Prints single character to window /// handled as curses pads and change in size requires to delete
/// @param c character to be printed /// them and create again, there is no way to change size of pad.
/// @return reference to itself /// @see SetBorder()
/// /// @see SetTitle()
Window &operator<<(char c); /// @see Resize()
///
/// Prints wide string to window virtual void Recreate(size_t width, size_t height);
/// @param ws const pointer to wchar_t array to be printed
/// @return reference to itself /// internal WINDOW pointers
/// WINDOW *itsWindow;
Window &operator<<(const wchar_t *ws); WINDOW *itsWinBorder;
/// Prints single wide character to window /// start points and dimensions
/// @param wc wide character to be printed size_t itsStartX;
/// @return reference to itself size_t itsStartY;
/// size_t itsWidth;
Window &operator<<(wchar_t wc); size_t itsHeight;
/// Prints int to window /// window timeout
/// @param i integer value to be printed int itsWindowTimeout;
/// @return reference to itself
/// /// current colors
Window &operator<<(int i); Color itsColor;
Color itsBgColor;
/// Prints double to window
/// @param d double value to be printed /// base colors
/// @return reference to itself Color itsBaseColor;
/// Color itsBaseBgColor;
Window &operator<<(double d);
/// current border
/// Prints size_t to window Border itsBorder;
/// @param s size value to be printed
/// @return reference to itself private:
/// /// Sets state of bold attribute (internal use only)
Window &operator<<(size_t s); /// @param bold_state state of bold attribute
///
/// Prints std::string to window void Bold(bool bold_state) const;
/// @param s string to be printed
/// @return reference to itself /// Sets state of underline attribute (internal use only)
/// /// @param underline_state state of underline attribute
Window &operator<<(const std::string &s); ///
void Underline(bool underline_state) const;
/// Prints std::wstring to window
/// @param ws wide string to be printed /// Sets state of reverse attribute (internal use only)
/// @return reference to itself /// @param reverse_state state of reverse attribute
/// ///
Window &operator<<(const std::wstring &ws); void Reverse(bool reverse_state) const;
/// Fallback for Length() for wide strings used if unicode support is disabled /// Sets state of altcharset attribute (internal use only)
/// @param s string that real length has to be measured /// @param altcharset_state state of altcharset attribute
/// @return standard std::string::length() result since it's only fallback ///
/// void AltCharset(bool altcharset_state) const;
static size_t Length(const std::string &s) { return s.length(); }
/// pointer to helper function used by GetString()
/// Measures real length of wide string (required if e.g. asian characters are used) /// @see GetString()
/// @param ws wide string that real length has to be measured ///
/// @return real length of wide string GetStringHelper itsGetStringHelper;
///
static size_t Length(const std::wstring &ws); /// window title
std::string itsTitle;
/// Cuts string so it fits desired length on the screen. Note that it uses
/// wcwidth to check real width of all characters it contains. If string /// stack of colors
/// fits requested length it's not modified at all. std::stack<Colors> itsColors;
/// @param ws wide string to be cut
/// @param max_len maximal length of string /// input queue of a window. you can put characters there using
static void Cut(std::wstring &ws, size_t max_len); /// PushChar and they will be immediately consumed and
/// returned by ReadKey
protected: std::queue<int> itsInputQueue;
/// Sets colors of window (interal use only)
/// @param fg foregound color /// containter used for additional file descriptors that have
/// @param bg background color /// to be polled in ReadKey() and correspondent callbacks that
/// /// are invoked if there is data available in them
void SetColor(Color fg, Color bg = clDefault); typedef std::vector< std::pair<int, void (*)()> > FDCallbacks;
FDCallbacks itsFDs;
/// Refreshes window's border
/// /// pointer to container used as history
void ShowBorder() const; std::list<std::wstring> *itsHistory;
/// Changes dimensions of window, called from Resize() /// counters for format flags
/// @param width new window's width int itsBoldCounter;
/// @param height new window's height int itsUnderlineCounter;
/// @see Resize() int itsReverseCounter;
/// int itsAltCharsetCounter;
void AdjustDimensions(size_t width, size_t height); };
/// Deletes old window and creates new. It's called by Resize(),
/// SetBorder() or SetTitle() since internally windows are
/// handled as curses pads and change in size requires to delete
/// them and create again, there is no way to change size of pad.
/// @see SetBorder()
/// @see SetTitle()
/// @see Resize()
///
virtual void Recreate(size_t width, size_t height);
/// internal WINDOW pointers
WINDOW *itsWindow;
WINDOW *itsWinBorder;
/// start points and dimensions
size_t itsStartX;
size_t itsStartY;
size_t itsWidth;
size_t itsHeight;
/// window timeout
int itsWindowTimeout;
/// current colors
Color itsColor;
Color itsBgColor;
/// base colors
Color itsBaseColor;
Color itsBaseBgColor;
/// current border
Border itsBorder;
private:
/// Sets state of bold attribute (internal use only)
/// @param bold_state state of bold attribute
///
void Bold(bool bold_state) const;
/// Sets state of underline attribute (internal use only)
/// @param underline_state state of underline attribute
///
void Underline(bool underline_state) const;
/// Sets state of reverse attribute (internal use only)
/// @param reverse_state state of reverse attribute
///
void Reverse(bool reverse_state) const;
/// Sets state of altcharset attribute (internal use only)
/// @param altcharset_state state of altcharset attribute
///
void AltCharset(bool altcharset_state) const;
/// pointer to helper function used by GetString()
/// @see GetString()
///
GetStringHelper itsGetStringHelper;
/// window title
std::string itsTitle;
/// stack of colors
std::stack<Colors> itsColors;
/// input queue of a window. you can put characters there using
/// PushChar and they will be immediately consumed and
/// returned by ReadKey
std::queue<int> itsInputQueue;
/// containter used for additional file descriptors that have
/// to be polled in ReadKey() and correspondent callbacks that
/// are invoked if there is data available in them
typedef std::vector< std::pair<int, void (*)()> > FDCallbacks;
FDCallbacks itsFDs;
/// pointer to container used as history
std::list<std::wstring> *itsHistory;
/// counters for format flags
int itsBoldCounter;
int itsUnderlineCounter;
int itsReverseCounter;
int itsAltCharsetCounter;
};
} }
#endif #endif

Loading…
Cancel
Save