1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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 2 of the License, or *
8 * (at your option) any later version. *
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. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #ifndef NCMPCPP_SCREEN_H
22 #define NCMPCPP_SCREEN_H
25 #include "scrollpad.h"
26 #include "screen_type.h"
28 void drawSeparator(int x
);
29 void genericMouseButtonPressed(NC::Window
&w
, MEVENT me
);
30 void scrollpadMouseButtonPressed(NC::Scrollpad
&w
, MEVENT me
);
32 /// An interface for various instantiations of Screen template class. Since C++ doesn't like
33 /// comparison of two different instantiations of the same template class we need the most
34 /// basic class to be non-template to allow it.
37 BaseScreen() : hasToBeResized(false) { }
38 virtual ~BaseScreen() { }
40 /// @see Screen::isActiveWindow()
41 virtual bool isActiveWindow(const NC::Window
&w_
) = 0;
43 /// @see Screen::activeWindow()
44 virtual void *activeWindow() = 0;
46 /// @see Screen::refresh()
47 virtual void refresh() = 0;
49 /// @see Screen::refreshWindow()
50 virtual void refreshWindow() = 0;
52 /// @see Screen::scroll()
53 virtual void scroll(NC::Scroll where
) = 0;
55 /// Method used for switching to screen
56 virtual void switchTo() = 0;
58 /// Method that should resize screen
59 /// if requested by hasToBeResized
60 virtual void resize() = 0;
62 /// @return ncurses timeout parameter for the screen
63 virtual int windowTimeout() = 0;
65 /// @return title of the screen
66 virtual std::wstring
title() = 0;
68 /// @return type of the screen
69 virtual ScreenType
type() = 0;
71 /// If the screen contantly has to update itself
72 /// somehow, it should be called by this function.
73 virtual void update() = 0;
75 /// Invoked after Enter was pressed
76 virtual void enterPressed() = 0;
78 /// Invoked after Space was pressed
79 virtual void spacePressed() = 0;
81 /// @see Screen::mouseButtonPressed()
82 virtual void mouseButtonPressed(MEVENT me
) = 0;
84 /// @return true if screen is mergable, ie. can be "proper" subwindow
85 /// if one of the screens is locked. Screens that somehow resemble popups
86 /// will want to return false here.
87 virtual bool isMergable() = 0;
89 /// Locks current screen.
90 /// @return true if lock was successful, false otherwise. Note that
91 /// if there is already locked screen, it'll not overwrite it.
94 /// Should be set to true each time screen needs resize
97 /// Unlocks a screen, ie. hides merged window (if there is one set).
101 /// @return true if screen can be locked. Note that returning
102 /// false here doesn't neccesarily mean that screen is also not
103 /// mergable (eg. lyrics screen is not lockable since that wouldn't
104 /// make much sense, but it's perfectly fine to merge it).
105 virtual bool isLockable() = 0;
107 /// Gets X offset and width of current screen to be used eg. in resize() function.
108 /// @param adjust_locked_screen indicates whether this function should
109 /// automatically adjust locked screen's dimensions (if there is one set)
110 /// if current screen is going to be subwindow.
111 void getWindowResizeParams(size_t &x_offset
, size_t &width
, bool adjust_locked_screen
= true);
114 void applyToVisibleWindows(std::function
<void(BaseScreen
*)> f
);
115 void updateInactiveScreen(BaseScreen
*screen_to_be_set
);
116 bool isVisible(BaseScreen
*screen
);
118 /// Class that all screens should derive from. It provides basic interface
119 /// for the screen to be working properly and assumes that we didn't forget
120 /// about anything vital.
122 template <typename WindowT
> struct Screen
: public BaseScreen
124 typedef WindowT WindowType
;
125 typedef typename
std::add_lvalue_reference
<WindowType
>::type WindowReference
;
128 template <bool IsPointer
, typename Result
> struct getObject
{ };
129 template <typename Result
> struct getObject
<true, Result
> {
130 static Result
apply(WindowType w
) { return *w
; }
132 template <typename Result
> struct getObject
<false, Result
> {
133 static Result
apply(WindowReference w
) { return w
; }
137 std::is_pointer
<WindowT
>::value
,
138 typename
std::add_lvalue_reference
<
139 typename
std::remove_pointer
<WindowT
>::type
145 Screen(WindowT w_
) : w(w_
) { }
147 virtual ~Screen() { }
149 virtual bool isActiveWindow(const NC::Window
&w_
) OVERRIDE
{
150 return &Accessor::apply(w
) == &w_
;
153 /// Since some screens contain more that one window
154 /// it's useful to determine the one that is being
156 /// @return address to window object cast to void *
157 virtual void *activeWindow() OVERRIDE
{
158 return &Accessor::apply(w
);
161 /// Refreshes whole screen
162 virtual void refresh() OVERRIDE
{
163 Accessor::apply(w
).display();
166 /// Refreshes active window of the screen
167 virtual void refreshWindow() OVERRIDE
{
168 Accessor::apply(w
).display();
171 /// Scrolls the screen by given amount of lines and
172 /// if fancy scrolling feature is disabled, enters the
173 /// loop that holds main loop until user releases the key
174 /// @param where indicates where one wants to scroll
175 virtual void scroll(NC::Scroll where
) OVERRIDE
{
176 Accessor::apply(w
).scroll(where
);
179 /// @return timeout parameter used for the screen (in ms)
181 virtual int windowTimeout() OVERRIDE
{
185 /// Invoked after there was one of mouse buttons pressed
186 /// @param me struct that contains coords of where the click
187 /// had its place and button actions
188 virtual void mouseButtonPressed(MEVENT me
) OVERRIDE
{
189 genericMouseButtonPressed(Accessor::apply(w
), me
);
192 /// @return currently active window
193 WindowReference
main() {
198 /// Template parameter that should indicate the main type
199 /// of window used by the screen. What is more, it should
200 /// always be assigned to the currently active window (if
201 /// acreen contains more that one)
205 /// Specialization for Screen<Scrollpad>::mouseButtonPressed that should
206 /// not scroll whole page, but rather a few lines (the number of them is
207 /// defined in the config)
208 template <> inline void Screen
<NC::Scrollpad
>::mouseButtonPressed(MEVENT me
) {
209 scrollpadMouseButtonPressed(w
, me
);
212 #endif // NCMPCPP_SCREEN_H