]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/window.cpp
Use nullptr in place of 0 or NULL
[libs/gui.git] / source / graphics / window.cpp
1 #include "window.h"
2 #include "window_private.h"
3 #include <msp/core/application.h>
4 #include "display.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace Graphics {
10
11 Window::Window(Display &dpy, unsigned w, unsigned h, bool fs):
12         display(dpy)
13 {
14         options.width = w;
15         options.height = h;
16         options.fullscreen = fs;
17
18         init();
19 }
20
21 Window::Window(Display &dpy, const WindowOptions &opts):
22         display(dpy),
23         options(opts)
24 {
25         init();
26 }
27
28 void Window::init()
29 {
30         priv = new Private;
31
32         if(options.fullscreen && !options.fullscreen_monitor)
33                 options.fullscreen_monitor = display.get_desktop_mode().monitor;
34
35         platform_init();
36
37         display.add_window(*this);
38         display.check_error();
39
40         const string &app_name = Application::get_name();
41         if(!app_name.empty())
42                 set_title(app_name);
43 }
44
45 Window::~Window()
46 {
47         platform_cleanup();
48
49         display.remove_window(*this);
50
51         if(options.fullscreen && visible)
52                 display.restore_mode();
53
54         delete priv;
55 }
56
57 void Window::reconfigure(const WindowOptions &opts)
58 {
59         bool fullscreen_changed = (opts.fullscreen!=options.fullscreen);
60         if(opts.width!=options.width || opts.height!=options.height)
61                 resizing = true;
62         if(opts.x!=options.x || opts.y!=options.y)
63                 moving = true;
64
65         options = opts;
66
67         if(visible)
68         {
69                 if(options.fullscreen)
70                         set_fullscreen_mode();
71                 else if(fullscreen_changed)
72                         display.restore_mode();
73         }
74
75         platform_reconfigure(fullscreen_changed);
76 }
77
78 void Window::set_fullscreen_mode()
79 {
80         if(!options.fullscreen_monitor)
81                 options.fullscreen_monitor = display.get_desktop_mode().monitor;
82         VideoMode mode(options.width, options.height);
83         mode.monitor = options.fullscreen_monitor;
84         mode.rotation = mode.monitor->desktop_settings.rotation;
85         if(mode.rotation==ROTATE_LEFT || mode.rotation==ROTATE_RIGHT)
86                 swap(mode.width, mode.height);
87         display.set_mode(mode, options.fullscreen_exclusive);
88 }
89
90 void Window::set_keyboard_autorepeat(bool r)
91 {
92         kbd_autorepeat = r;
93 }
94
95 void Window::set_touch_input(bool t)
96 {
97         touch_input = t;
98         platform_set_touch_input();
99 }
100
101 void Window::show()
102 {
103         platform_show();
104         visible = true;
105
106         if(options.fullscreen)
107         {
108                 set_fullscreen_mode();
109                 warp_pointer(options.width/2, options.height/2);
110         }
111 }
112
113 void Window::hide()
114 {
115         platform_hide();
116         visible = false;
117
118         if(options.fullscreen)
119                 display.restore_mode();
120 }
121
122 } // namespace Graphics
123 } // namespace Msp