]> git.sesse.net Git - casparcg/blob - server/Window.cpp
1.8.1:
[casparcg] / server / Window.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20  \r
21 #include "stdafx.h"\r
22 \r
23 #include "window.h"\r
24 #include "resource.h"\r
25 \r
26 namespace caspar {\r
27 \r
28 using namespace utils;\r
29 \r
30 Window::Window() : _hwnd(NULL), _hinstance(NULL), _hdc(NULL)\r
31 {\r
32 }\r
33 \r
34 Window::~Window()\r
35 {\r
36         Destroy();\r
37 }\r
38 \r
39 bool Window::Initialize(HINSTANCE hinstance, const TCHAR* windowTitle, const TCHAR* className)\r
40 {\r
41         _hinstance = hinstance;\r
42         _classname = className;\r
43 \r
44     WNDCLASSEX wndClass;         // Window class\r
45     ZeroMemory(&wndClass, sizeof(wndClass)); // Clear the window class structure\r
46     wndClass.cbSize = sizeof(WNDCLASSEX); \r
47     wndClass.style          = CS_HREDRAW | CS_VREDRAW | CS_CLASSDC;\r
48     wndClass.lpfnWndProc    = (WNDPROC)WndProc;\r
49     wndClass.cbClsExtra     = 0;\r
50     wndClass.cbWndExtra     = 0;\r
51     wndClass.hInstance      = _hinstance;\r
52     wndClass.hIcon          = LoadIcon(_hinstance, MAKEINTRESOURCE(IDI_ICON1));\r
53     wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);\r
54     wndClass.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);\r
55     wndClass.lpszMenuName   = NULL;//MAKEINTRESOURCE(IDR_MAINMENU);\r
56     wndClass.lpszClassName  = _classname.c_str();\r
57     wndClass.hIconSm        = 0;\r
58 \r
59     if (RegisterClassEx(&wndClass) == 0)// Attemp to register the window class\r
60     {\r
61                 LOG << TEXT("WINDOW ERROR: Failed to register the window class!") << LogStream::Flush;\r
62         return false;\r
63     }\r
64     DWORD dwStyle;              // Window styles\r
65     DWORD dwExStyle;            // Extended window styles\r
66 \r
67     dwStyle = WS_OVERLAPPEDWINDOW |        // Creates an overlapping window\r
68               WS_CLIPCHILDREN |            // Doesn"t draw within child windows\r
69               WS_CLIPSIBLINGS;              // Doesn"t draw within sibling windows\r
70     dwExStyle = WS_EX_APPWINDOW |          // Top level window\r
71                 WS_EX_WINDOWEDGE;           // Border with a raised edge\r
72     \r
73     //adjust window size\r
74     RECT rMain;\r
75     rMain.left = 0;\r
76     rMain.right = 720;\r
77     rMain.top = 0;\r
78     rMain.bottom = 576;  \r
79 \r
80     AdjustWindowRect(&rMain, dwStyle, 0);\r
81 \r
82     // Attempt to create the actual window\r
83     _hwnd = CreateWindowEx( dwExStyle,       // Extended window styles\r
84                             _classname.c_str(),   // Class name\r
85                             windowTitle,       // Window title (caption)\r
86                             dwStyle,         // Window styles\r
87                             0, 0,            // Window position\r
88                             rMain.right - rMain.left,\r
89                             rMain.bottom - rMain.top,   // Size of window\r
90                             0,               // No parent window\r
91                             0,               // No menu\r
92                             _hinstance,      // Instance\r
93                             0);            // Pass nothing to WM_CREATE\r
94 \r
95     if(_hwnd == 0) \r
96     {\r
97         Destroy();\r
98         LOG << TEXT("WINDOW ERROR: Unable to create window!") << LogStream::Flush;\r
99         return false;\r
100     }\r
101 \r
102     ShowWindow(_hwnd, SW_SHOW);\r
103     SetForegroundWindow(_hwnd);\r
104     SetFocus(_hwnd);\r
105 \r
106 \r
107         //TEST: select a more appropriate pixelformat\r
108         _hdc = ::GetDC(_hwnd);\r
109 \r
110         PIXELFORMATDESCRIPTOR pfd = \r
111         {\r
112                 sizeof(PIXELFORMATDESCRIPTOR),          //size of struct\r
113                 1,                                                                      //version number\r
114                 //PFD_DRAW_TO_WINDOW |                          //Format must support draw to window\r
115                 PFD_DRAW_TO_BITMAP |                            //Format must support draw to bitmap\r
116                 PFD_DOUBLEBUFFER_DONTCARE |                     //Format does not have to support doublebuffer\r
117                 PFD_DEPTH_DONTCARE,                                     //Formet does not have to support depthbuffer\r
118                 PFD_TYPE_RGBA,                                          //Request RGBA format\r
119                 24,                                                                     //Color depth\r
120                 0,0,0,0,0,0,                                            //colorbits ignored\r
121                 8,                                                                      //8-bit alpha-buffer\r
122                 0,                                                                      //shift bit ignored\r
123                 0,                                                                      //no accumulation-buffer\r
124                 0,0,0,0,                                                        //Accumulation bits ignored\r
125                 0,                                                                      //no depth-buffer\r
126                 0,                                                                      //no stencil-buffer\r
127                 0,                                                                      //no auxiliary-buffer\r
128                 PFD_MAIN_PLANE,                                         //Main drawing layer\r
129                 0,                                                                      //RESERVED\r
130                 0,0,0                                                           //Layer masks ignored\r
131         };\r
132 \r
133         unsigned int nPixelFormat = ChoosePixelFormat(_hdc, &pfd);\r
134         if(nPixelFormat) {\r
135                 if(!SetPixelFormat(_hdc, nPixelFormat, &pfd)) {\r
136                         ;\r
137                 }\r
138         }\r
139         //END TEST: select a more appropriate pixelformat\r
140 /*\r
141         //TEST: give flash access to a directdraw device\r
142         IDirectDrawFactory* pDDF = NULL;\r
143         pDD_ = NULL;\r
144         CComBSTR ddfGUID(_T("{4FD2A832-86C8-11d0-8FCA-00C04FD9189D}"));\r
145         CLSID clsid;\r
146         HRESULT hr = CLSIDFromString((LPOLESTR)ddfGUID, &clsid);\r
147 \r
148         hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, GUID_DDFactory, (void **)&pDDF);\r
149         if(pDDF != 0) {\r
150                 pDDF->CreateDirectDraw(NULL, _hwnd, DDSCL_NORMAL, NULL, NULL, &pDD_);\r
151                 pDDF->Release();\r
152         }\r
153         //END TEST: give flash access to a directdraw device\r
154 */\r
155         return true;\r
156 }\r
157 \r
158 void Window::Destroy()\r
159 {\r
160 /*      if(pDD_ != 0) {\r
161                 pDD_->Release();\r
162                 pDD_ = 0;\r
163         }\r
164 */\r
165         // Attempts to destroy the window\r
166         if(_hwnd) {\r
167                 DestroyWindow(_hwnd);\r
168                 _hwnd = NULL;\r
169         }\r
170 \r
171     // Attempts to unregister the window class\r
172     if (!UnregisterClass(_classname.c_str(), _hinstance))\r
173     {\r
174         LOG << TEXT("WINDOW ERROR: Unable to unregister window class!") << LogStream::Flush;\r
175         _hinstance = NULL;\r
176     }\r
177 }\r
178 \r
179 }