1370b324cSopenharmony_ci// Windows/Window.cpp
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#include "StdAfx.h"
4370b324cSopenharmony_ci
5370b324cSopenharmony_ci#ifndef _UNICODE
6370b324cSopenharmony_ci#include "../Common/StringConvert.h"
7370b324cSopenharmony_ci#endif
8370b324cSopenharmony_ci#include "Window.h"
9370b324cSopenharmony_ci
10370b324cSopenharmony_ci#ifndef _UNICODE
11370b324cSopenharmony_ciextern bool g_IsNT;
12370b324cSopenharmony_ci#endif
13370b324cSopenharmony_ci
14370b324cSopenharmony_cinamespace NWindows {
15370b324cSopenharmony_ci
16370b324cSopenharmony_ci#ifndef _UNICODE
17370b324cSopenharmony_ciATOM MyRegisterClass(CONST WNDCLASSW *wndClass)
18370b324cSopenharmony_ci{
19370b324cSopenharmony_ci  if (g_IsNT)
20370b324cSopenharmony_ci    return RegisterClassW(wndClass);
21370b324cSopenharmony_ci  WNDCLASSA wndClassA;
22370b324cSopenharmony_ci  wndClassA.style = wndClass->style;
23370b324cSopenharmony_ci  wndClassA.lpfnWndProc = wndClass->lpfnWndProc;
24370b324cSopenharmony_ci  wndClassA.cbClsExtra = wndClass->cbClsExtra;
25370b324cSopenharmony_ci  wndClassA.cbWndExtra = wndClass->cbWndExtra;
26370b324cSopenharmony_ci  wndClassA.hInstance = wndClass->hInstance;
27370b324cSopenharmony_ci  wndClassA.hIcon = wndClass->hIcon;
28370b324cSopenharmony_ci  wndClassA.hCursor = wndClass->hCursor;
29370b324cSopenharmony_ci  wndClassA.hbrBackground = wndClass->hbrBackground;
30370b324cSopenharmony_ci  AString menuName;
31370b324cSopenharmony_ci  AString className;
32370b324cSopenharmony_ci  if (IS_INTRESOURCE(wndClass->lpszMenuName))
33370b324cSopenharmony_ci    wndClassA.lpszMenuName = (LPCSTR)wndClass->lpszMenuName;
34370b324cSopenharmony_ci  else
35370b324cSopenharmony_ci  {
36370b324cSopenharmony_ci    menuName = GetSystemString(wndClass->lpszMenuName);
37370b324cSopenharmony_ci    wndClassA.lpszMenuName = menuName;
38370b324cSopenharmony_ci  }
39370b324cSopenharmony_ci  if (IS_INTRESOURCE(wndClass->lpszClassName))
40370b324cSopenharmony_ci    wndClassA.lpszClassName = (LPCSTR)wndClass->lpszClassName;
41370b324cSopenharmony_ci  else
42370b324cSopenharmony_ci  {
43370b324cSopenharmony_ci    className = GetSystemString(wndClass->lpszClassName);
44370b324cSopenharmony_ci    wndClassA.lpszClassName = className;
45370b324cSopenharmony_ci  }
46370b324cSopenharmony_ci  return RegisterClassA(&wndClassA);
47370b324cSopenharmony_ci}
48370b324cSopenharmony_ci
49370b324cSopenharmony_cibool CWindow::Create(LPCWSTR className,
50370b324cSopenharmony_ci      LPCWSTR windowName, DWORD style,
51370b324cSopenharmony_ci      int x, int y, int width, int height,
52370b324cSopenharmony_ci      HWND parentWindow, HMENU idOrHMenu,
53370b324cSopenharmony_ci      HINSTANCE instance, LPVOID createParam)
54370b324cSopenharmony_ci{
55370b324cSopenharmony_ci  if (g_IsNT)
56370b324cSopenharmony_ci  {
57370b324cSopenharmony_ci    _window = ::CreateWindowW(className, windowName,
58370b324cSopenharmony_ci        style, x, y, width, height, parentWindow,
59370b324cSopenharmony_ci        idOrHMenu, instance, createParam);
60370b324cSopenharmony_ci     return (_window != NULL);
61370b324cSopenharmony_ci  }
62370b324cSopenharmony_ci  return Create(GetSystemString(className), GetSystemString(windowName),
63370b324cSopenharmony_ci        style, x, y, width, height, parentWindow,
64370b324cSopenharmony_ci        idOrHMenu, instance, createParam);
65370b324cSopenharmony_ci}
66370b324cSopenharmony_ci
67370b324cSopenharmony_cibool CWindow::CreateEx(DWORD exStyle, LPCWSTR className,
68370b324cSopenharmony_ci      LPCWSTR windowName, DWORD style,
69370b324cSopenharmony_ci      int x, int y, int width, int height,
70370b324cSopenharmony_ci      HWND parentWindow, HMENU idOrHMenu,
71370b324cSopenharmony_ci      HINSTANCE instance, LPVOID createParam)
72370b324cSopenharmony_ci{
73370b324cSopenharmony_ci  if (g_IsNT)
74370b324cSopenharmony_ci  {
75370b324cSopenharmony_ci    _window = ::CreateWindowExW(exStyle, className, windowName,
76370b324cSopenharmony_ci      style, x, y, width, height, parentWindow,
77370b324cSopenharmony_ci      idOrHMenu, instance, createParam);
78370b324cSopenharmony_ci     return (_window != NULL);
79370b324cSopenharmony_ci  }
80370b324cSopenharmony_ci  AString classNameA;
81370b324cSopenharmony_ci  LPCSTR classNameP;
82370b324cSopenharmony_ci  if (IS_INTRESOURCE(className))
83370b324cSopenharmony_ci    classNameP = (LPCSTR)className;
84370b324cSopenharmony_ci  else
85370b324cSopenharmony_ci  {
86370b324cSopenharmony_ci    classNameA = GetSystemString(className);
87370b324cSopenharmony_ci    classNameP = classNameA;
88370b324cSopenharmony_ci  }
89370b324cSopenharmony_ci  AString windowNameA;
90370b324cSopenharmony_ci  LPCSTR windowNameP;
91370b324cSopenharmony_ci  if (IS_INTRESOURCE(windowName))
92370b324cSopenharmony_ci    windowNameP = (LPCSTR)windowName;
93370b324cSopenharmony_ci  else
94370b324cSopenharmony_ci  {
95370b324cSopenharmony_ci    windowNameA = GetSystemString(windowName);
96370b324cSopenharmony_ci    windowNameP = windowNameA;
97370b324cSopenharmony_ci  }
98370b324cSopenharmony_ci  return CreateEx(exStyle, classNameP, windowNameP,
99370b324cSopenharmony_ci      style, x, y, width, height, parentWindow,
100370b324cSopenharmony_ci      idOrHMenu, instance, createParam);
101370b324cSopenharmony_ci}
102370b324cSopenharmony_ci
103370b324cSopenharmony_ci#endif
104370b324cSopenharmony_ci
105370b324cSopenharmony_ci#ifndef _UNICODE
106370b324cSopenharmony_cibool MySetWindowText(HWND wnd, LPCWSTR s)
107370b324cSopenharmony_ci{
108370b324cSopenharmony_ci  if (g_IsNT)
109370b324cSopenharmony_ci    return BOOLToBool(::SetWindowTextW(wnd, s));
110370b324cSopenharmony_ci  return BOOLToBool(::SetWindowTextA(wnd, UnicodeStringToMultiByte(s)));
111370b324cSopenharmony_ci}
112370b324cSopenharmony_ci#endif
113370b324cSopenharmony_ci
114370b324cSopenharmony_cibool CWindow::GetText(CSysString &s) const
115370b324cSopenharmony_ci{
116370b324cSopenharmony_ci  s.Empty();
117370b324cSopenharmony_ci  unsigned len = (unsigned)GetTextLength();
118370b324cSopenharmony_ci  if (len == 0)
119370b324cSopenharmony_ci    return (::GetLastError() == ERROR_SUCCESS);
120370b324cSopenharmony_ci  TCHAR *p = s.GetBuf(len);
121370b324cSopenharmony_ci  {
122370b324cSopenharmony_ci    const unsigned len2 = (unsigned)GetText(p, (int)(len + 1));
123370b324cSopenharmony_ci    if (len > len2)
124370b324cSopenharmony_ci      len = len2;
125370b324cSopenharmony_ci  }
126370b324cSopenharmony_ci  s.ReleaseBuf_CalcLen(len);
127370b324cSopenharmony_ci  if (len == 0)
128370b324cSopenharmony_ci    return (::GetLastError() == ERROR_SUCCESS);
129370b324cSopenharmony_ci  return true;
130370b324cSopenharmony_ci}
131370b324cSopenharmony_ci
132370b324cSopenharmony_ci#ifndef _UNICODE
133370b324cSopenharmony_cibool CWindow::GetText(UString &s) const
134370b324cSopenharmony_ci{
135370b324cSopenharmony_ci  if (g_IsNT)
136370b324cSopenharmony_ci  {
137370b324cSopenharmony_ci    s.Empty();
138370b324cSopenharmony_ci    unsigned len = (unsigned)GetWindowTextLengthW(_window);
139370b324cSopenharmony_ci    if (len == 0)
140370b324cSopenharmony_ci      return (::GetLastError() == ERROR_SUCCESS);
141370b324cSopenharmony_ci    wchar_t *p = s.GetBuf(len);
142370b324cSopenharmony_ci    {
143370b324cSopenharmony_ci      const unsigned len2 = (unsigned)GetWindowTextW(_window, p, (int)(len + 1));
144370b324cSopenharmony_ci      if (len > len2)
145370b324cSopenharmony_ci        len = len2;
146370b324cSopenharmony_ci    }
147370b324cSopenharmony_ci    s.ReleaseBuf_CalcLen(len);
148370b324cSopenharmony_ci    if (len == 0)
149370b324cSopenharmony_ci      return (::GetLastError() == ERROR_SUCCESS);
150370b324cSopenharmony_ci    return true;
151370b324cSopenharmony_ci  }
152370b324cSopenharmony_ci  CSysString sysString;
153370b324cSopenharmony_ci  const bool result = GetText(sysString);
154370b324cSopenharmony_ci  MultiByteToUnicodeString2(s, sysString);
155370b324cSopenharmony_ci  return result;
156370b324cSopenharmony_ci}
157370b324cSopenharmony_ci#endif
158370b324cSopenharmony_ci
159370b324cSopenharmony_ci
160370b324cSopenharmony_ci/*
161370b324cSopenharmony_cibool CWindow::ModifyStyleBase(int styleOffset,
162370b324cSopenharmony_ci  DWORD remove, DWORD add, UINT flags)
163370b324cSopenharmony_ci{
164370b324cSopenharmony_ci  DWORD style = GetWindowLong(styleOffset);
165370b324cSopenharmony_ci  DWORD newStyle = (style & ~remove) | add;
166370b324cSopenharmony_ci  if (style == newStyle)
167370b324cSopenharmony_ci    return false; // it is not good
168370b324cSopenharmony_ci
169370b324cSopenharmony_ci  SetWindowLong(styleOffset, newStyle);
170370b324cSopenharmony_ci  if (flags != 0)
171370b324cSopenharmony_ci  {
172370b324cSopenharmony_ci    ::SetWindowPos(_window, NULL, 0, 0, 0, 0,
173370b324cSopenharmony_ci      SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | flags);
174370b324cSopenharmony_ci  }
175370b324cSopenharmony_ci  return TRUE;
176370b324cSopenharmony_ci}
177370b324cSopenharmony_ci*/
178370b324cSopenharmony_ci
179370b324cSopenharmony_ci}
180