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