1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "NativeSubWindow.h"
17 
18 #include <stdio.h>
19 struct SubWindowUserData {
20     SubWindowRepaintCallback repaint_callback;
21     void* repaint_callback_param;
22 };
23 
subWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)24 static LRESULT CALLBACK subWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
25     if (uMsg == WM_PAINT) {
26         auto user_data =
27             (SubWindowUserData*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
28         if (user_data && user_data->repaint_callback) {
29             user_data->repaint_callback(user_data->repaint_callback_param);
30         }
31     } else if (uMsg == WM_NCDESTROY) {
32         SubWindowUserData* user_data =
33             (SubWindowUserData*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
34         delete user_data;
35     }
36     return DefWindowProcA(hwnd, uMsg, wParam, lParam);
37 }
38 
createSubWindow(FBNativeWindowType p_window,int x,int y,int width,int height,float dpr,SubWindowRepaintCallback repaint_callback,void * repaint_callback_param,int hideWindow)39 EGLNativeWindowType createSubWindow(FBNativeWindowType p_window,
40                                     int x, int y,int width, int height, float dpr,
41                                     SubWindowRepaintCallback repaint_callback,
42                                     void* repaint_callback_param, int hideWindow){
43     static const char className[] = "subWin";
44 
45     WNDCLASSA wc = {};
46     if (!GetClassInfoA(GetModuleHandle(NULL), className, &wc)) {
47         wc.style =  CS_OWNDC | CS_HREDRAW | CS_VREDRAW;// redraw if size changes
48         wc.lpfnWndProc = &subWindowProc;               // points to window procedure
49         wc.cbWndExtra = sizeof(void*) ;                // save extra window memory
50         wc.lpszClassName = className;                  // name of window class
51         RegisterClassA(&wc);
52     }
53 
54     // We assume size/pos are passed in as logical size/coordinates. Convert it to pixel
55     // coordinates.
56     x *= dpr;
57     y *= dpr;
58     width *= dpr;
59     height *= dpr;
60     EGLNativeWindowType ret = CreateWindowExA(
61                         WS_EX_NOPARENTNOTIFY,  // do not bother our parent window
62                         className,
63                         "sub",
64                         WS_CHILD|WS_DISABLED,
65                         x,y,width,height,
66                         p_window,
67                         NULL,
68                         NULL,
69                         NULL);
70 
71     auto user_data = new SubWindowUserData();
72     user_data->repaint_callback = repaint_callback;
73     user_data->repaint_callback_param = repaint_callback_param;
74 
75     SetWindowLongPtr(ret, GWLP_USERDATA, (LONG_PTR)user_data);
76     if (!hideWindow)
77         ShowWindow(ret, SW_SHOW);
78     return ret;
79 }
80 
destroySubWindow(EGLNativeWindowType win)81 void destroySubWindow(EGLNativeWindowType win){
82     PostMessage(win, WM_CLOSE, 0, 0);
83 }
84 
moveSubWindow(FBNativeWindowType p_parent_window,EGLNativeWindowType p_sub_window,int x,int y,int width,int height,float dpr)85 int moveSubWindow(FBNativeWindowType p_parent_window,
86                   EGLNativeWindowType p_sub_window,
87                   int x,
88                   int y,
89                   int width,
90                   int height,
91                   float dpr) {
92     BOOL ret = MoveWindow(p_sub_window,
93                           x * dpr,
94                           y * dpr,
95                           width * dpr,
96                           height * dpr,
97                           TRUE);
98     return ret;
99 }
100