1 /* Window.hpp
2  * Copyright (C) 2018, 2019  Sven Jähnichen
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef BWIDGETS_WINDOW_HPP_
19 #define BWIDGETS_WINDOW_HPP_
20 
21 // Default BWidgets::Window settings (Note: use non-transparent backgrounds only)
22 #define BWIDGETS_DEFAULT_WINDOW_BACKGROUND BStyles::blackFill
23 
24 #include <chrono>
25 #include <deque>
26 #include <list>
27 #include "Widget.hpp"
28 
29 namespace BWidgets
30 {
31 
32 /**
33  * Class BWidgets::Window
34  *
35  * Main window class of BWidgets. Add all other widgets (directly or
36  * indirectly) to this window.
37  * A BWidgets::Window is the BWidgets::Widget that is controlled by the host
38  * via Pugl, receives host events via Pugl and coordinates handling of all
39  * events. Configure, expose, and close events will be handled directly and
40  * exclusively by this widget.
41  */
42 class Window : public Widget
43 {
44 protected:
45 	BDevices::DeviceGrabStack<uint32_t> keyGrabStack_;
46 	BDevices::DeviceGrabStack<BDevices::MouseDevice> buttonGrabStack_;
47 
48 public:
49 	Window ();
50 	Window (const double width, const double height, const std::string& title,
51 		PuglNativeView nativeWindow, bool resizable = false,
52 		PuglWorldType worldType = PUGL_PROGRAM, int worldFlag = 0);
53 
54 	Window (const Window& that) = delete;			// Only one window in this version
55 
56 	~Window ();
57 
58 	Window& operator= (const Window& that) = delete;	// Only one Window in this version
59 
60 	virtual Widget* clone () = delete;			// Only one Window in this version
61 
62 	/**
63 	 * Gets in contact to the host system via Pugl
64 	 * @return Pointer to the PuglView
65 	 */
66 	PuglView* getPuglView ();
67 
68 	/**
69 	 * Gets the Cairo context provided by the host system via Pugl
70 	 * @return Pointer to the Cairo context
71 	 */
72 	cairo_t* getPuglContext ();
73 
74 	/**
75 	 * Runs the window until the close flag is set and thus it will be closed.
76 	 * For stand-alone applications.
77 	 */
78 	void run ();
79 
80 	/**
81 	 * Queues an event until the next call of the handleEvents method.
82 	 * @param event Event
83 	 */
84 	void addEventToQueue (BEvents::Event* event);
85 
86 	/**
87 	 * Main Event handler. Walks through the event queue and sorts the events
88 	 * to their respective onXXX handling methods
89 	 */
90 	void handleEvents ();
91 
92 	/**
93 	 * Executes an reexposure of the area given by the expose event.
94 	 * @param event Expose event containing the widget that emitted the event
95 	 * 		and the area that should be reexposed.
96 	 */
97 	virtual void onExposeRequest (BEvents::ExposeEvent* event) override;
98 
99 	/**
100 	 * Predefined empty method to handle a BEvents::EventType::CONFIGURE_EVENT.
101 	 * @param event Expose event containing the widget that emitted the event
102 	 * 		and the area that should be reexposed.
103 	 */
104 	virtual void onConfigureRequest (BEvents::ExposeEvent* event) override;
105 
106 	/**
107 	 * Sets the close flag and thus ends the run method.
108 	 * @param event Widget event containing the widget that emitted the event
109 	 */
110 	virtual void onCloseRequest (BEvents::WidgetEvent* event) override;
111 
112 	/* Gets (the pointer to) the keyGrabStack and thus enables access to the
113 	 * keyboard input.
114 	 * @return	Pointer to keyGrabStack_.
115 	 */
116 	BDevices::DeviceGrabStack<uint32_t>* getKeyGrabStack ();
117 
118 	/* Gets (the pointer to) the buttonGrabStack and thus enables access to
119 	 * the mouse buttons pressed.
120 	 * @return	Pointer to buttonGrabStack_.
121 	 */
122 	BDevices::DeviceGrabStack<BDevices::MouseDevice>* getButtonGrabStack ();
123 
124 	/*
125 	 * Removes events (emited by a given widget) from the event queue
126 	 * @param widget	Emitting widget (nullptr for all widgets)
127 	 */
128 	void purgeEventQueue (Widget* widget = nullptr);
129 
130 protected:
131 
132 	/**
133 	 * Communication interface to the host via Pugl. Translates PuglEvents to
134 	 * BEvents::Event derived objects.
135 	 */
136 	static PuglStatus translatePuglEvent (PuglView* view, const PuglEvent* event);
137 
138 	void translateTimeEvent ();
139 	void mergeEvents ();
140 
141 	void unfocus();
142 
143 	std::string title_;
144 	PuglWorld* world_;
145 	PuglView* view_;
146 	PuglNativeView nativeWindow_;
147 	bool quit_;
148 	bool focused_;
149 	BUtilities::Point pointer_;
150 
151 	std::deque<BEvents::Event*> eventQueue_;		// TODO: std::list ?
152 };
153 
154 }
155 
156 
157 
158 #endif /* BWIDGETS_WINDOW_HPP_ */
159