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