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