]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/window.cpp
Include the matching header first in .cpp files
[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 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         if(options.fullscreen && !options.fullscreen_monitor)
51                 options.fullscreen_monitor = display.get_desktop_mode().monitor;
52
53         platform_init();
54
55         display.add_window(*this);
56         display.check_error();
57
58         const string &app_name = Application::get_name();
59         if(!app_name.empty())
60                 set_title(app_name);
61 }
62
63 Window::~Window()
64 {
65         platform_cleanup();
66
67         display.remove_window(*this);
68
69         if(options.fullscreen && visible)
70                 display.restore_mode();
71
72         delete priv;
73 }
74
75 void Window::reconfigure(const WindowOptions &opts)
76 {
77         bool fullscreen_changed = (opts.fullscreen!=options.fullscreen);
78         if(opts.width!=options.width || opts.height!=options.height)
79                 resizing = true;
80         if(opts.x!=options.x || opts.y!=options.y)
81                 moving = true;
82
83         options = opts;
84
85         if(visible)
86         {
87                 if(options.fullscreen)
88                         set_fullscreen_mode();
89                 else if(fullscreen_changed)
90                         display.restore_mode();
91         }
92
93         platform_reconfigure(fullscreen_changed);
94 }
95
96 void Window::set_fullscreen_mode()
97 {
98         if(!options.fullscreen_monitor)
99                 options.fullscreen_monitor = display.get_desktop_mode().monitor;
100         VideoMode mode(options.width, options.height);
101         mode.monitor = options.fullscreen_monitor;
102         mode.rotation = mode.monitor->desktop_settings.rotation;
103         if(mode.rotation==ROTATE_LEFT || mode.rotation==ROTATE_RIGHT)
104                 swap(mode.width, mode.height);
105         display.set_mode(mode, options.fullscreen_exclusive);
106 }
107
108 void Window::set_keyboard_autorepeat(bool r)
109 {
110         kbd_autorepeat = r;
111 }
112
113 void Window::set_touch_input(bool t)
114 {
115         touch_input = t;
116         platform_set_touch_input();
117 }
118
119 void Window::show()
120 {
121         platform_show();
122         visible = true;
123
124         if(options.fullscreen)
125         {
126                 set_fullscreen_mode();
127                 warp_pointer(options.width/2, options.height/2);
128         }
129 }
130
131 void Window::hide()
132 {
133         platform_hide();
134         visible = false;
135
136         if(options.fullscreen)
137                 display.restore_mode();
138 }
139
140 } // namespace Graphics
141 } // namespace Msp