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_
) const = 0;
43 /// @see Screen::activeWindow()
44 virtual NC::Window
*activeWindow() = 0;
45 virtual const NC::Window
*activeWindow() const = 0;
47 /// @see Screen::refresh()
48 virtual void refresh() = 0;
50 /// @see Screen::refreshWindow()
51 virtual void refreshWindow() = 0;
53 /// @see Screen::scroll()
54 virtual void scroll(NC::Scroll where
) = 0;
56 /// Method used for switching to screen
57 virtual void switchTo() = 0;
59 /// Method that should resize screen
60 /// if requested by hasToBeResized
61 virtual void resize() = 0;
63 /// @return ncurses timeout parameter for the screen
64 virtual int windowTimeout() = 0;
66 /// @return title of the screen
67 virtual std::wstring
title() = 0;
69 /// @return type of the screen
70 virtual ScreenType
type() = 0;
72 /// If the screen contantly has to update itself
73 /// somehow, it should be called by this function.
74 virtual void update() = 0;
76 /// Invoked after Enter was pressed
77 virtual void enterPressed() = 0;
79 /// Invoked after Space was pressed
80 virtual void spacePressed() = 0;
82 /// @see Screen::mouseButtonPressed()
83 virtual void mouseButtonPressed(MEVENT me
) = 0;
85 /// @return true if screen can be locked. Note that returning
86 /// false here doesn't neccesarily mean that screen is also not
87 /// mergable (eg. lyrics screen is not lockable since that wouldn't
88 /// make much sense, but it's perfectly fine to merge it).
89 virtual bool isLockable() = 0;
91 /// @return true if screen is mergable, ie. can be "proper" subwindow
92 /// if one of the screens is locked. Screens that somehow resemble popups
93 /// will want to return false here.
94 virtual bool isMergable() = 0;
96 /// Locks current screen.
97 /// @return true if lock was successful, false otherwise. Note that
98 /// if there is already locked screen, it'll not overwrite it.
101 /// Should be set to true each time screen needs resize
104 /// Unlocks a screen, ie. hides merged window (if there is one set).
105 static void unlock();
108 /// Gets X offset and width of current screen to be used eg. in resize() function.
109 /// @param adjust_locked_screen indicates whether this function should
110 /// automatically adjust locked screen's dimensions (if there is one set)
111 /// if current screen is going to be subwindow.
112 void getWindowResizeParams(size_t &x_offset
, size_t &width
, bool adjust_locked_screen
= true);
115 void applyToVisibleWindows(std::function
<void(BaseScreen
*)> f
);
116 void updateInactiveScreen(BaseScreen
*screen_to_be_set
);
117 bool isVisible(BaseScreen
*screen
);
119 /// Class that all screens should derive from. It provides basic interface
120 /// for the screen to be working properly and assumes that we didn't forget
121 /// about anything vital.
123 template <typename WindowT
> struct Screen
: public BaseScreen
125 typedef WindowT WindowType
;
126 typedef typename
std::add_lvalue_reference
<WindowType
>::type WindowReference
;
127 typedef typename
std::add_lvalue_reference
<
128 typename
std::add_const
<WindowType
>::type
129 >::type ConstWindowReference
;
132 template <bool IsPointer
, typename Result
, typename ConstResult
>
134 static Result
&apply(WindowReference w
) { return w
; }
135 static ConstResult
&constApply(ConstWindowReference w
) { return w
; }
137 template <typename Result
, typename ConstResult
>
138 struct getObject
<true, Result
, ConstResult
> {
139 static Result
&apply(WindowType w
) { return *w
; }
140 static ConstResult
&constApply(const WindowType w
) { return *w
; }
144 std::is_pointer
<WindowT
>::value
,
145 typename
std::remove_pointer
<WindowT
>::type
,
146 typename
std::add_const
<
147 typename
std::remove_pointer
<WindowT
>::type
153 Screen(WindowT w_
) : w(w_
) { }
155 virtual ~Screen() { }
157 virtual bool isActiveWindow(const NC::Window
&w_
) const OVERRIDE
{
158 return &Accessor::constApply(w
) == &w_
;
161 /// Since some screens contain more that one window
162 /// it's useful to determine the one that is being
164 /// @return address to window object cast to void *
165 virtual NC::Window
*activeWindow() OVERRIDE
{
166 return &Accessor::apply(w
);
168 virtual const NC::Window
*activeWindow() const OVERRIDE
{
169 return &Accessor::constApply(w
);
172 /// Refreshes whole screen
173 virtual void refresh() OVERRIDE
{
174 Accessor::apply(w
).display();
177 /// Refreshes active window of the screen
178 virtual void refreshWindow() OVERRIDE
{
179 Accessor::apply(w
).display();
182 /// Scrolls the screen by given amount of lines and
183 /// if fancy scrolling feature is disabled, enters the
184 /// loop that holds main loop until user releases the key
185 /// @param where indicates where one wants to scroll
186 virtual void scroll(NC::Scroll where
) OVERRIDE
{
187 Accessor::apply(w
).scroll(where
);
190 /// @return timeout parameter used for the screen (in ms)
192 virtual int windowTimeout() OVERRIDE
{
196 /// Invoked after there was one of mouse buttons pressed
197 /// @param me struct that contains coords of where the click
198 /// had its place and button actions
199 virtual void mouseButtonPressed(MEVENT me
) OVERRIDE
{
200 genericMouseButtonPressed(Accessor::apply(w
), me
);
203 /// @return currently active window
204 WindowReference
main() {
209 /// Template parameter that should indicate the main type
210 /// of window used by the screen. What is more, it should
211 /// always be assigned to the currently active window (if
212 /// acreen contains more that one)
216 /// Specialization for Screen<Scrollpad>::mouseButtonPressed that should
217 /// not scroll whole page, but rather a few lines (the number of them is
218 /// defined in the config)
219 template <> inline void Screen
<NC::Scrollpad
>::mouseButtonPressed(MEVENT me
) {
220 scrollpadMouseButtonPressed(w
, me
);
223 #endif // NCMPCPP_SCREEN_H