• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Windows/Window.cpp
2 
3 #include "StdAfx.h"
4 
5 #ifndef _UNICODE
6 #include "../Common/StringConvert.h"
7 #endif
8 #include "Window.h"
9 
10 #ifndef _UNICODE
11 extern bool g_IsNT;
12 #endif
13 
14 namespace NWindows {
15 
16 #ifndef _UNICODE
MyRegisterClass(CONST WNDCLASSW * wndClass)17 ATOM MyRegisterClass(CONST WNDCLASSW *wndClass)
18 {
19   if (g_IsNT)
20     return RegisterClassW(wndClass);
21   WNDCLASSA wndClassA;
22   wndClassA.style = wndClass->style;
23   wndClassA.lpfnWndProc = wndClass->lpfnWndProc;
24   wndClassA.cbClsExtra = wndClass->cbClsExtra;
25   wndClassA.cbWndExtra = wndClass->cbWndExtra;
26   wndClassA.hInstance = wndClass->hInstance;
27   wndClassA.hIcon = wndClass->hIcon;
28   wndClassA.hCursor = wndClass->hCursor;
29   wndClassA.hbrBackground = wndClass->hbrBackground;
30   AString menuName;
31   AString className;
32   if (IS_INTRESOURCE(wndClass->lpszMenuName))
33     wndClassA.lpszMenuName = (LPCSTR)wndClass->lpszMenuName;
34   else
35   {
36     menuName = GetSystemString(wndClass->lpszMenuName);
37     wndClassA.lpszMenuName = menuName;
38   }
39   if (IS_INTRESOURCE(wndClass->lpszClassName))
40     wndClassA.lpszClassName = (LPCSTR)wndClass->lpszClassName;
41   else
42   {
43     className = GetSystemString(wndClass->lpszClassName);
44     wndClassA.lpszClassName = className;
45   }
46   return RegisterClassA(&wndClassA);
47 }
48 
Create(LPCWSTR className,LPCWSTR windowName,DWORD style,int x,int y,int width,int height,HWND parentWindow,HMENU idOrHMenu,HINSTANCE instance,LPVOID createParam)49 bool CWindow::Create(LPCWSTR className,
50       LPCWSTR windowName, DWORD style,
51       int x, int y, int width, int height,
52       HWND parentWindow, HMENU idOrHMenu,
53       HINSTANCE instance, LPVOID createParam)
54 {
55   if (g_IsNT)
56   {
57     _window = ::CreateWindowW(className, windowName,
58         style, x, y, width, height, parentWindow,
59         idOrHMenu, instance, createParam);
60      return (_window != NULL);
61   }
62   return Create(GetSystemString(className), GetSystemString(windowName),
63         style, x, y, width, height, parentWindow,
64         idOrHMenu, instance, createParam);
65 }
66 
CreateEx(DWORD exStyle,LPCWSTR className,LPCWSTR windowName,DWORD style,int x,int y,int width,int height,HWND parentWindow,HMENU idOrHMenu,HINSTANCE instance,LPVOID createParam)67 bool CWindow::CreateEx(DWORD exStyle, LPCWSTR className,
68       LPCWSTR windowName, DWORD style,
69       int x, int y, int width, int height,
70       HWND parentWindow, HMENU idOrHMenu,
71       HINSTANCE instance, LPVOID createParam)
72 {
73   if (g_IsNT)
74   {
75     _window = ::CreateWindowExW(exStyle, className, windowName,
76       style, x, y, width, height, parentWindow,
77       idOrHMenu, instance, createParam);
78      return (_window != NULL);
79   }
80   AString classNameA;
81   LPCSTR classNameP;
82   if (IS_INTRESOURCE(className))
83     classNameP = (LPCSTR)className;
84   else
85   {
86     classNameA = GetSystemString(className);
87     classNameP = classNameA;
88   }
89   AString windowNameA;
90   LPCSTR windowNameP;
91   if (IS_INTRESOURCE(windowName))
92     windowNameP = (LPCSTR)windowName;
93   else
94   {
95     windowNameA = GetSystemString(windowName);
96     windowNameP = windowNameA;
97   }
98   return CreateEx(exStyle, classNameP, windowNameP,
99       style, x, y, width, height, parentWindow,
100       idOrHMenu, instance, createParam);
101 }
102 
103 #endif
104 
105 #ifndef _UNICODE
MySetWindowText(HWND wnd,LPCWSTR s)106 bool MySetWindowText(HWND wnd, LPCWSTR s)
107 {
108   if (g_IsNT)
109     return BOOLToBool(::SetWindowTextW(wnd, s));
110   return BOOLToBool(::SetWindowTextA(wnd, UnicodeStringToMultiByte(s)));
111 }
112 #endif
113 
GetText(CSysString & s)114 bool CWindow::GetText(CSysString &s)
115 {
116   s.Empty();
117   int len = GetTextLength();
118   if (len == 0)
119     return (::GetLastError() == ERROR_SUCCESS);
120   TCHAR *p = s.GetBuf(len);
121   {
122     int len2 = GetText(p, len + 1);
123     if (len > len2)
124       len = len2;
125   }
126   s.ReleaseBuf_CalcLen(len);
127   if (len == 0)
128     return (::GetLastError() == ERROR_SUCCESS);
129   return true;
130 }
131 
132 #ifndef _UNICODE
GetText(UString & s)133 bool CWindow::GetText(UString &s)
134 {
135   if (g_IsNT)
136   {
137     s.Empty();
138     int len = GetWindowTextLengthW(_window);
139     if (len == 0)
140       return (::GetLastError() == ERROR_SUCCESS);
141     wchar_t *p = s.GetBuf(len);
142     {
143       int len2 = GetWindowTextW(_window, p, len + 1);
144       if (len > len2)
145         len = len2;
146     }
147     s.ReleaseBuf_CalcLen(len);
148     if (len == 0)
149       return (::GetLastError() == ERROR_SUCCESS);
150     return true;
151   }
152   CSysString sysString;
153   bool result = GetText(sysString);
154   MultiByteToUnicodeString2(s, sysString);
155   return result;
156 }
157 #endif
158 
159 
160 /*
161 bool CWindow::ModifyStyleBase(int styleOffset,
162   DWORD remove, DWORD add, UINT flags)
163 {
164   DWORD style = GetWindowLong(styleOffset);
165   DWORD newStyle = (style & ~remove) | add;
166   if (style == newStyle)
167     return false; // it is not good
168 
169   SetWindowLong(styleOffset, newStyle);
170   if (flags != 0)
171   {
172     ::SetWindowPos(_window, NULL, 0, 0, 0, 0,
173       SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | flags);
174   }
175   return TRUE;
176 }
177 */
178 
179 }
180