1 // Copyright 2014 Emil Velikov
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
8 // - Redistributions of source code must retain the above copyright notice, this
9 // list of conditions and the following disclaimer.
11 // - Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "wcore_error.h"
30 #include "wgl_config.h"
31 #include "wgl_platform.h"
32 #include "wgl_window.h"
35 wgl_window_destroy(struct wcore_window
*wc_self
)
37 struct wgl_window
*self
= wgl_window(wc_self
);
42 self
->created
= false;
43 ShowWindow(self
->hWnd
, SW_HIDE
);
48 wgl_window_priv_destroy(struct wcore_window
*wc_self
)
50 struct wgl_window
*self
= wgl_window(wc_self
);
58 ok
&= ReleaseDC(self
->hWnd
, self
->hDC
);
60 ok
&= DestroyWindow(self
->hWnd
);
63 ok
&= wcore_window_teardown(wc_self
);
69 wgl_window_create(struct wcore_platform
*wc_plat
,
70 struct wcore_config
*wc_config
,
74 struct wgl_config
*config
= wgl_config(wc_config
);
77 assert(config
->window
);
79 // Currently we do not allow multiple windows per config.
80 // Neither piglit nor the waffle examples do that yet, so just
81 // return NULL in case that ever changes.
82 assert(!config
->window
->created
);
83 if (config
->window
->created
)
86 config
->window
->created
= true;
88 ok
= wgl_window_resize(&config
->window
->wcore
, width
, height
);
92 return &config
->window
->wcore
;
96 wgl_window_priv_create(struct wcore_platform
*wc_plat
,
97 struct wcore_config
*wc_config
,
101 struct wgl_platform
*plat
= wgl_platform(wc_plat
);
102 struct wgl_window
*self
;
106 self
= wcore_calloc(sizeof(*self
));
110 ok
= wcore_window_init(&self
->wcore
, wc_config
);
116 rect
.right
= rect
.left
+ width
;
117 rect
.bottom
= rect
.top
+ height
;
119 ok
= AdjustWindowRect(&rect
, WS_POPUPWINDOW
, FALSE
);
123 self
->hWnd
= CreateWindow(plat
->class_name
, NULL
, WS_POPUPWINDOW
,
125 rect
.right
- rect
.left
, rect
.bottom
- rect
.top
,
126 NULL
, NULL
, NULL
, NULL
);
130 self
->hDC
= GetDC(self
->hWnd
);
137 wgl_window_priv_destroy(&self
->wcore
);
142 wgl_window_show(struct wcore_window
*wc_self
)
144 struct wgl_window
*self
= wgl_window(wc_self
);
149 // If the window was previously hidden the function returns zero,
150 // and non-zero otherwise.
151 // XXX: Use SW_SHOW or SW_SHOWDEFAULT, SW_SHOWNORMAL ?
152 ShowWindow(self
->hWnd
, SW_SHOW
);
157 wgl_window_resize(struct wcore_window
*wc_self
,
158 int32_t width
, int32_t height
)
160 struct wgl_window
*self
= wgl_window(wc_self
);
169 rect
.right
= rect
.left
+ width
;
170 rect
.bottom
= rect
.top
+ height
;
172 ok
= AdjustWindowRect(&rect
, WS_POPUPWINDOW
, FALSE
);
176 ok
= SetWindowPos(self
->hWnd
, 0, 0, 0,
177 rect
.right
- rect
.left
,
178 rect
.bottom
- rect
.top
,
179 SWP_NOMOVE
|SWP_NOZORDER
|SWP_NOACTIVATE
);
182 // Verify the client area size matches the required size.
184 GetClientRect(self
->hWnd
, &rect
);
185 assert(rect
.left
== 0);
186 assert(rect
.top
== 0);
187 assert(rect
.right
- rect
.left
== width
);
188 assert(rect
.bottom
- rect
.top
== height
);
194 wgl_window_swap_buffers(struct wcore_window
*wc_self
)
196 struct wgl_window
*self
= wgl_window(wc_self
);
201 return SwapBuffers(self
->hDC
);