mirror of
https://github.com/clang-retro/mfc.git
synced 2026-11-28 21:29:33 -08:00
These were the last functions in port/src/stubs.txt. Nothing on Linux or macOS fails as unimplemented any more. WinHelp: there is no WinHelp viewer, so the port opens a document next to the program's .hlp file instead: the same name as .html, .htm, .pdf or .txt, or a folder with an index.html. It uses the desktop's opener (xdg-open or open), or the command WINPORT_HELP names. A help context becomes the HTML page's anchor (help.html#131072). MFC's Help commands work through it. Gopher: GopherCreateLocator, GopherGetLocatorType, GopherFindFirstFile (menus, submenus and index searches, then InternetFindNextFile), GopherOpenFile (text with its dot escaping undone, binary, and gopher+ views) and GopherGetAttribute (gopher+ attributes, all of them or one block). gopher:// URLs work through InternetOpenUrl. inettest runs a gopher server of its own and tests it on the port; Windows dropped gopher with IE 7, and Wine's is a stub. CoInternetCreateSecurityManager: zones without Windows' settings. Local files and res: are the local machine; a share or a host without a dot is the intranet; everything else is the internet. Each zone has a policy for ProcessUrlAction. There are security ids, and SetZoneMapping and GetZoneMappings for the process. A service provider's own manager is asked first. CDHtmlDialog uses it to decide window.external, and htmltest now tests it directly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRRw4kkFHrrHRhkcuLw Signed-off-by: Claude <noreply@anthropic.com>
2824 lines
109 KiB
C++
2824 lines
109 KiB
C++
// wintest.cpp - console tests for windows and drawing below the MFC
|
|
// classes: window classes and windows, the window tree and coordinates,
|
|
// sending and posting, the message loop, timers, messages between
|
|
// threads, hooks, ANSI and Unicode window procedures, focus, painting,
|
|
// device contexts, bitmaps and DIBs, raster operations, regions,
|
|
// clipping, mapping modes and scrolling, lines, shapes, paths and
|
|
// text, icons, accelerators and menus, dialogs and the standard
|
|
// controls, MDI, image lists, the common controls, the list view, the
|
|
// tree view and property sheets; printing and the print dialogs; window
|
|
// shapes (SetWindowRgn) and dropped files (WM_DROPFILES); and a CWnd
|
|
// with a message map on top.
|
|
//
|
|
// Part of clang-retro MFC, an independent implementation of the MFC API.
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
#include <afxwin.h>
|
|
#include <commctrl.h>
|
|
#include <commdlg.h>
|
|
#include <dlgs.h>
|
|
#include <prsht.h>
|
|
#include <shlobj.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
static int g_failures = 0;
|
|
#define CHECK(cond) \
|
|
do { \
|
|
if (!(cond)) { \
|
|
printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
g_failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
CWinApp theApp;
|
|
|
|
// ---------------------------------------------------------------------
|
|
// A window procedure that records what it is sent.
|
|
|
|
static std::vector<UINT> g_msgs;
|
|
static int g_paints = 0;
|
|
static int g_timers = 0;
|
|
static HWND g_focusFrom = NULL;
|
|
static BOOL g_inSend = FALSE;
|
|
|
|
static bool Seen(UINT msg) {
|
|
for (UINT m : g_msgs)
|
|
if (m == msg) return true;
|
|
return false;
|
|
}
|
|
|
|
static int IndexOf(UINT msg) {
|
|
for (size_t i = 0; i < g_msgs.size(); i++)
|
|
if (g_msgs[i] == msg) return (int)i;
|
|
return -1;
|
|
}
|
|
|
|
static LRESULT CALLBACK RecordProc(HWND h, UINT msg, WPARAM wp, LPARAM lp) {
|
|
g_msgs.push_back(msg);
|
|
switch (msg) {
|
|
case WM_PAINT: {
|
|
g_paints++;
|
|
PAINTSTRUCT ps;
|
|
HDC dc = ::BeginPaint(h, &ps);
|
|
HBRUSH br = ::CreateSolidBrush(RGB(255, 0, 0));
|
|
RECT rc;
|
|
::GetClientRect(h, &rc);
|
|
::FillRect(dc, &rc, br);
|
|
::DeleteObject(br);
|
|
::EndPaint(h, &ps);
|
|
return 0;
|
|
}
|
|
case WM_TIMER:
|
|
g_timers++;
|
|
return 0;
|
|
case WM_SETFOCUS:
|
|
g_focusFrom = (HWND)wp;
|
|
return 0;
|
|
case WM_APP:
|
|
g_inSend = ::InSendMessage();
|
|
return wp + lp;
|
|
case WM_APP + 1:
|
|
return ::GetCurrentThreadId();
|
|
}
|
|
return ::DefWindowProcW(h, msg, wp, lp);
|
|
}
|
|
|
|
// An ANSI procedure for checking text conversion.
|
|
static char g_ansiText[64];
|
|
static LRESULT CALLBACK AnsiProc(HWND h, UINT msg, WPARAM wp, LPARAM lp) {
|
|
if (msg == WM_SETTEXT && lp) lstrcpynA(g_ansiText, (LPCSTR)lp, sizeof g_ansiText);
|
|
return ::DefWindowProcA(h, msg, wp, lp);
|
|
}
|
|
|
|
static const wchar_t kClass[] = L"WinTestClass";
|
|
|
|
static void Pump() {
|
|
MSG m;
|
|
while (::PeekMessageW(&m, NULL, 0, 0, PM_REMOVE)) {
|
|
::TranslateMessage(&m);
|
|
::DispatchMessageW(&m);
|
|
}
|
|
}
|
|
|
|
static HWND NewTop(DWORD style, int x, int y, int cx, int cy) {
|
|
return ::CreateWindowExW(0, kClass, L"Test", style, x, y, cx, cy, NULL, NULL, ::GetModuleHandleW(NULL), NULL);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
static void TestClassesAndCreation() {
|
|
WNDCLASSW wc = {};
|
|
wc.style = CS_DBLCLKS;
|
|
wc.lpfnWndProc = RecordProc;
|
|
wc.hInstance = ::GetModuleHandleW(NULL);
|
|
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
|
|
wc.lpszClassName = kClass;
|
|
wc.cbWndExtra = 2 * sizeof(LONG_PTR);
|
|
CHECK(::RegisterClassW(&wc) != 0);
|
|
CHECK(::RegisterClassW(&wc) == 0 && ::GetLastError() == ERROR_CLASS_ALREADY_EXISTS);
|
|
WNDCLASSW got;
|
|
CHECK(::GetClassInfoW(::GetModuleHandleW(NULL), kClass, &got) && got.cbWndExtra == 2 * sizeof(LONG_PTR));
|
|
|
|
g_msgs.clear();
|
|
HWND h = NewTop(WS_OVERLAPPEDWINDOW, 10, 20, 300, 200);
|
|
CHECK(h != NULL);
|
|
// The creation messages, in order.
|
|
CHECK(IndexOf(WM_GETMINMAXINFO) >= 0 && IndexOf(WM_GETMINMAXINFO) < IndexOf(WM_NCCREATE));
|
|
CHECK(IndexOf(WM_NCCREATE) < IndexOf(WM_NCCALCSIZE) && IndexOf(WM_NCCALCSIZE) < IndexOf(WM_CREATE));
|
|
CHECK(!::IsWindowVisible(h));
|
|
wchar_t text[16];
|
|
CHECK(::GetWindowTextW(h, text, 16) == 4 && lstrcmpW(text, L"Test") == 0);
|
|
CHECK(::GetWindowTextLengthW(h) == 4);
|
|
char textA[16];
|
|
CHECK(::GetWindowTextA(h, textA, 16) == 4 && lstrcmpA(textA, "Test") == 0);
|
|
wchar_t cls[32];
|
|
CHECK(::GetClassNameW(h, cls, 32) && lstrcmpiW(cls, kClass) == 0);
|
|
CHECK(::IsWindowUnicode(h));
|
|
|
|
RECT wr, cr;
|
|
::GetWindowRect(h, &wr);
|
|
::GetClientRect(h, &cr);
|
|
CHECK(wr.left == 10 && wr.top == 20 && wr.right == 310 && wr.bottom == 220);
|
|
// The frame AdjustWindowRectEx adds is what the client area lost.
|
|
RECT adj = {0, 0, cr.right, cr.bottom};
|
|
::AdjustWindowRectEx(&adj, WS_OVERLAPPEDWINDOW, FALSE, 0);
|
|
CHECK(adj.right - adj.left == 300 && adj.bottom - adj.top == 200);
|
|
|
|
// Extra bytes and user data.
|
|
::SetWindowLongPtrW(h, sizeof(LONG_PTR), 1234);
|
|
CHECK(::GetWindowLongPtrW(h, sizeof(LONG_PTR)) == 1234);
|
|
::SetWindowLongPtrW(h, GWLP_USERDATA, 99);
|
|
CHECK(::GetWindowLongPtrW(h, GWLP_USERDATA) == 99);
|
|
CHECK((::GetWindowLongW(h, GWL_STYLE) & WS_CAPTION) == WS_CAPTION);
|
|
|
|
// Properties.
|
|
CHECK(::SetPropW(h, L"Prop", (HANDLE)42));
|
|
CHECK(::GetPropW(h, L"Prop") == (HANDLE)42);
|
|
CHECK(::RemovePropW(h, L"Prop") == (HANDLE)42 && ::GetPropW(h, L"Prop") == NULL);
|
|
|
|
g_msgs.clear();
|
|
CHECK(::DestroyWindow(h));
|
|
CHECK(Seen(WM_DESTROY) && Seen(WM_NCDESTROY) && IndexOf(WM_DESTROY) < IndexOf(WM_NCDESTROY));
|
|
CHECK(!::IsWindow(h));
|
|
}
|
|
|
|
static void TestTree() {
|
|
HWND top = NewTop(WS_OVERLAPPEDWINDOW, 50, 60, 400, 300);
|
|
HWND a = ::CreateWindowExW(0, kClass, L"a", WS_CHILD | WS_VISIBLE, 10, 10, 50, 40, top, (HMENU)101,
|
|
::GetModuleHandleW(NULL), NULL);
|
|
HWND b = ::CreateWindowExW(0, kClass, L"b", WS_CHILD | WS_VISIBLE, 70, 10, 50, 40, top, (HMENU)102,
|
|
::GetModuleHandleW(NULL), NULL);
|
|
CHECK(a && b);
|
|
CHECK(::GetParent(a) == top && ::IsChild(top, a) && !::IsChild(a, top));
|
|
CHECK(::GetDlgItem(top, 102) == b && ::GetDlgCtrlID(a) == 101);
|
|
// Children stay in the order they were made.
|
|
CHECK(::GetWindow(top, GW_CHILD) == a && ::GetWindow(a, GW_HWNDNEXT) == b);
|
|
::SetWindowPos(b, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
|
|
CHECK(::GetWindow(top, GW_CHILD) == b);
|
|
|
|
POINT p = {0, 0};
|
|
::ClientToScreen(a, &p);
|
|
POINT q = {0, 0};
|
|
::ClientToScreen(top, &q);
|
|
CHECK(p.x == q.x + 10 && p.y == q.y + 10);
|
|
POINT r = {5, 5};
|
|
::MapWindowPoints(a, b, &r, 1);
|
|
CHECK(r.x == 5 - 60 && r.y == 5);
|
|
POINT inside = {75, 15};
|
|
CHECK(::ChildWindowFromPoint(top, inside) == b);
|
|
|
|
::MoveWindow(a, 20, 30, 60, 50, TRUE);
|
|
RECT rc;
|
|
::GetWindowRect(a, &rc);
|
|
CHECK(rc.left == q.x + 20 && rc.top == q.y + 30 && rc.right - rc.left == 60);
|
|
::GetClientRect(a, &rc);
|
|
CHECK(rc.right == 60 && rc.bottom == 50);
|
|
|
|
// Destroying the parent destroys the children.
|
|
::DestroyWindow(top);
|
|
CHECK(!::IsWindow(a) && !::IsWindow(b));
|
|
}
|
|
|
|
static void TestMessages() {
|
|
HWND h = NewTop(WS_OVERLAPPEDWINDOW, 0, 0, 200, 100);
|
|
CHECK(::SendMessageW(h, WM_APP, 2, 3) == 5);
|
|
CHECK(!g_inSend);
|
|
|
|
// Posted messages come back in order, and filters pick among them.
|
|
Pump();
|
|
::PostMessageW(h, WM_APP + 10, 1, 0);
|
|
::PostMessageW(h, WM_APP + 11, 2, 0);
|
|
::PostMessageW(NULL, WM_APP + 12, 3, 0);
|
|
MSG m;
|
|
CHECK(::PeekMessageW(&m, NULL, WM_APP + 11, WM_APP + 11, PM_REMOVE) && m.wParam == 2);
|
|
CHECK(::PeekMessageW(&m, (HWND)-1, 0, 0, PM_REMOVE) && m.message == WM_APP + 12 && m.hwnd == NULL);
|
|
CHECK(::PeekMessageW(&m, h, 0, 0, PM_NOREMOVE) && m.message == WM_APP + 10);
|
|
CHECK(::PeekMessageW(&m, h, WM_APP, WM_APP + 20, PM_REMOVE) && m.message == WM_APP + 10);
|
|
CHECK(!::PeekMessageW(&m, h, WM_APP, WM_APP + 20, PM_REMOVE));
|
|
|
|
// WM_QUIT ends GetMessage.
|
|
::PostThreadMessageW(::GetCurrentThreadId(), WM_APP + 13, 0, 0);
|
|
::PostQuitMessage(7);
|
|
CHECK(::GetMessageW(&m, NULL, 0, 0) == TRUE && m.message == WM_APP + 13);
|
|
CHECK(::GetMessageW(&m, NULL, 0, 0) == FALSE && m.message == WM_QUIT && m.wParam == 7);
|
|
|
|
// TranslateMessage makes WM_CHAR from a key.
|
|
::PostMessageW(h, WM_KEYDOWN, 'A', 1);
|
|
CHECK(::PeekMessageW(&m, h, WM_KEYDOWN, WM_KEYDOWN, PM_REMOVE));
|
|
::TranslateMessage(&m);
|
|
CHECK(::PeekMessageW(&m, h, WM_CHAR, WM_CHAR, PM_REMOVE) && (m.wParam == 'a' || m.wParam == 'A'));
|
|
|
|
::DestroyWindow(h);
|
|
}
|
|
|
|
static int g_procTimers = 0;
|
|
static void CALLBACK OnThreadTimer(HWND, UINT, UINT_PTR, DWORD) { g_procTimers++; }
|
|
|
|
static void TestTimers() {
|
|
HWND h = NewTop(WS_OVERLAPPEDWINDOW, 0, 0, 200, 100);
|
|
g_timers = 0;
|
|
CHECK(::SetTimer(h, 1, 20, NULL) == 1);
|
|
UINT_PTR t = ::SetTimer(NULL, 0, 20, OnThreadTimer);
|
|
CHECK(t != 0);
|
|
DWORD start = ::GetTickCount();
|
|
MSG m;
|
|
while ((g_timers < 2 || g_procTimers < 2) && ::GetTickCount() - start < 3000) {
|
|
if (::GetMessageW(&m, NULL, 0, 0) <= 0) break;
|
|
::DispatchMessageW(&m);
|
|
}
|
|
CHECK(g_timers >= 2 && g_procTimers >= 2);
|
|
CHECK(::KillTimer(h, 1));
|
|
CHECK(::KillTimer(NULL, t));
|
|
CHECK(!::KillTimer(h, 1));
|
|
::DestroyWindow(h);
|
|
}
|
|
|
|
struct SendArgs {
|
|
HWND hwnd;
|
|
LRESULT result;
|
|
LRESULT tid;
|
|
};
|
|
static DWORD WINAPI SendFromThread(LPVOID p) {
|
|
SendArgs* a = (SendArgs*)p;
|
|
a->result = ::SendMessageW(a->hwnd, WM_APP, 20, 22);
|
|
a->tid = ::SendMessageW(a->hwnd, WM_APP + 1, 0, 0);
|
|
::PostMessageW(a->hwnd, WM_APP + 2, 0, 0);
|
|
return 0;
|
|
}
|
|
|
|
static void TestThreads() {
|
|
HWND h = NewTop(WS_OVERLAPPEDWINDOW, 0, 0, 200, 100);
|
|
SendArgs a = {h, 0, 0};
|
|
g_inSend = FALSE;
|
|
HANDLE t = ::CreateThread(NULL, 0, SendFromThread, &a, 0, NULL);
|
|
// The window's thread handles the sent messages while it waits.
|
|
MSG m;
|
|
DWORD start = ::GetTickCount();
|
|
bool posted = false;
|
|
while (!posted && ::GetTickCount() - start < 5000) {
|
|
if (::MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT) == WAIT_OBJECT_0) {
|
|
while (::PeekMessageW(&m, NULL, 0, 0, PM_REMOVE)) {
|
|
if (m.message == WM_APP + 2) posted = true;
|
|
::DispatchMessageW(&m);
|
|
}
|
|
}
|
|
}
|
|
::WaitForSingleObject(t, 5000);
|
|
::CloseHandle(t);
|
|
CHECK(posted);
|
|
CHECK(a.result == 42);
|
|
CHECK(g_inSend);
|
|
CHECK(a.tid == (LRESULT)::GetCurrentThreadId());
|
|
::DestroyWindow(h);
|
|
}
|
|
|
|
static HWND g_cbtCreated = NULL;
|
|
static bool g_cbtBeforeNcCreate = false;
|
|
static LRESULT CALLBACK CbtHook(int code, WPARAM wp, LPARAM lp) {
|
|
// Only this test's class counts; Wine makes windows of its own.
|
|
wchar_t cls[64] = L"";
|
|
if (code == HCBT_CREATEWND) ::GetClassNameW((HWND)wp, cls, 64);
|
|
if (code == HCBT_CREATEWND && !g_cbtCreated && lstrcmpW(cls, kClass) == 0) {
|
|
g_cbtCreated = (HWND)wp;
|
|
g_cbtBeforeNcCreate = !Seen(WM_NCCREATE);
|
|
}
|
|
return ::CallNextHookEx(NULL, code, wp, lp);
|
|
}
|
|
|
|
static void TestHooksAndAnsi() {
|
|
HHOOK hook = ::SetWindowsHookExW(WH_CBT, CbtHook, NULL, ::GetCurrentThreadId());
|
|
CHECK(hook != NULL);
|
|
g_msgs.clear();
|
|
g_cbtCreated = NULL;
|
|
HWND h = NewTop(WS_OVERLAPPEDWINDOW, 0, 0, 200, 100);
|
|
CHECK(g_cbtCreated == h && g_cbtBeforeNcCreate);
|
|
CHECK(::UnhookWindowsHookEx(hook));
|
|
|
|
// An ANSI procedure gets text as ANSI, and Unicode callers still see it.
|
|
WNDPROC old = (WNDPROC)::SetWindowLongPtrA(h, GWLP_WNDPROC, (LONG_PTR)AnsiProc);
|
|
CHECK(!::IsWindowUnicode(h));
|
|
CHECK(::SetWindowTextW(h, L"Hello"));
|
|
CHECK(lstrcmpA(g_ansiText, "Hello") == 0);
|
|
wchar_t buf[16];
|
|
CHECK(::GetWindowTextW(h, buf, 16) == 5 && lstrcmpW(buf, L"Hello") == 0);
|
|
// The old procedure, asked for by an ANSI caller, is a handle that
|
|
// CallWindowProc knows.
|
|
CHECK(::CallWindowProcA(old, h, WM_APP, 1, 1) == 2);
|
|
::SetWindowLongPtrA(h, GWLP_WNDPROC, (LONG_PTR)old);
|
|
CHECK(::IsWindowUnicode(h));
|
|
CHECK(::SendMessageW(h, WM_APP, 3, 4) == 7);
|
|
::DestroyWindow(h);
|
|
}
|
|
|
|
static void TestFocusAndPaint() {
|
|
HWND top = NewTop(WS_OVERLAPPEDWINDOW | WS_VISIBLE, 40, 40, 300, 200);
|
|
HWND child = ::CreateWindowExW(0, kClass, L"c", WS_CHILD | WS_VISIBLE, 10, 10, 50, 50, top, (HMENU)1,
|
|
::GetModuleHandleW(NULL), NULL);
|
|
::SetFocus(top);
|
|
::SetFocus(child);
|
|
CHECK(::GetFocus() == child);
|
|
CHECK(g_focusFrom == top);
|
|
|
|
g_paints = 0;
|
|
::UpdateWindow(top);
|
|
Pump();
|
|
CHECK(g_paints >= 1);
|
|
RECT rc;
|
|
CHECK(!::GetUpdateRect(top, &rc, FALSE));
|
|
::InvalidateRect(top, NULL, TRUE);
|
|
CHECK(::GetUpdateRect(top, &rc, FALSE));
|
|
::ValidateRect(top, NULL);
|
|
CHECK(!::GetUpdateRect(top, &rc, FALSE));
|
|
|
|
// What WM_PAINT drew is on the window.
|
|
::InvalidateRect(top, NULL, TRUE);
|
|
::UpdateWindow(top);
|
|
HDC dc = ::GetDC(top);
|
|
CHECK(::GetPixel(dc, 100, 100) == RGB(255, 0, 0));
|
|
::ReleaseDC(top, dc);
|
|
|
|
// Scrolling moves what is drawn.
|
|
dc = ::GetDC(top);
|
|
HBRUSH blue = ::CreateSolidBrush(RGB(0, 0, 255));
|
|
RECT left = {0, 0, 150, 100};
|
|
::FillRect(dc, &left, blue);
|
|
::DeleteObject(blue);
|
|
::ReleaseDC(top, dc);
|
|
RECT area = {0, 0, 280, 100};
|
|
::ScrollWindowEx(top, 20, 0, &area, &area, NULL, NULL, 0);
|
|
dc = ::GetDC(top);
|
|
CHECK(::GetPixel(dc, 160, 50) == RGB(0, 0, 255));
|
|
CHECK(::GetPixel(dc, 175, 50) == RGB(255, 0, 0));
|
|
::ReleaseDC(top, dc);
|
|
|
|
::DestroyWindow(top);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Drawing in memory
|
|
|
|
static void TestMemoryDC() {
|
|
HDC screen = ::GetDC(NULL);
|
|
HDC mem = ::CreateCompatibleDC(screen);
|
|
HBITMAP bm = ::CreateCompatibleBitmap(screen, 40, 30);
|
|
HGDIOBJ old = ::SelectObject(mem, bm);
|
|
CHECK(::GetObjectType(mem) == OBJ_MEMDC);
|
|
CHECK(::GetCurrentObject(mem, OBJ_BITMAP) == bm);
|
|
BITMAP info;
|
|
CHECK(::GetObjectW(bm, sizeof info, &info) && info.bmWidth == 40 && info.bmHeight == 30);
|
|
|
|
RECT all = {0, 0, 40, 30};
|
|
::FillRect(mem, &all, (HBRUSH)::GetStockObject(WHITE_BRUSH));
|
|
HBRUSH green = ::CreateSolidBrush(RGB(0, 255, 0));
|
|
RECT part = {5, 5, 15, 10};
|
|
::FillRect(mem, &part, green);
|
|
CHECK(::GetPixel(mem, 5, 5) == RGB(0, 255, 0));
|
|
CHECK(::GetPixel(mem, 14, 9) == RGB(0, 255, 0));
|
|
CHECK(::GetPixel(mem, 15, 10) == RGB(255, 255, 255));
|
|
::SetPixel(mem, 1, 1, RGB(10, 20, 30));
|
|
CHECK(::GetPixel(mem, 1, 1) == RGB(10, 20, 30));
|
|
|
|
// Raster operations.
|
|
::PatBlt(mem, 0, 0, 2, 2, DSTINVERT);
|
|
CHECK(::GetPixel(mem, 1, 1) == RGB(245, 235, 225));
|
|
CHECK(::GetPixel(mem, 0, 0) == RGB(0, 0, 0));
|
|
|
|
// Copies, stretched and not.
|
|
HDC mem2 = ::CreateCompatibleDC(screen);
|
|
HBITMAP bm2 = ::CreateCompatibleBitmap(screen, 80, 60);
|
|
HGDIOBJ old2 = ::SelectObject(mem2, bm2);
|
|
::BitBlt(mem2, 0, 0, 40, 30, mem, 0, 0, SRCCOPY);
|
|
CHECK(::GetPixel(mem2, 10, 7) == RGB(0, 255, 0));
|
|
::StretchBlt(mem2, 0, 0, 80, 60, mem, 0, 0, 40, 30, SRCCOPY);
|
|
CHECK(::GetPixel(mem2, 20, 14) == RGB(0, 255, 0));
|
|
CHECK(::GetPixel(mem2, 32, 22) == RGB(255, 255, 255));
|
|
|
|
// A monochrome bitmap: the source's background color becomes white,
|
|
// and going back, white is the destination's background color.
|
|
HDC monoDC = ::CreateCompatibleDC(screen);
|
|
HBITMAP mono = ::CreateBitmap(40, 30, 1, 1, NULL);
|
|
HGDIOBJ oldMono = ::SelectObject(monoDC, mono);
|
|
::SetBkColor(mem, RGB(255, 255, 255));
|
|
::BitBlt(monoDC, 0, 0, 40, 30, mem, 0, 0, SRCCOPY);
|
|
CHECK(::GetPixel(monoDC, 20, 20) == RGB(255, 255, 255));
|
|
CHECK(::GetPixel(monoDC, 6, 6) == RGB(0, 0, 0));
|
|
::SetBkColor(mem2, RGB(1, 2, 3));
|
|
::SetTextColor(mem2, RGB(4, 5, 6));
|
|
::BitBlt(mem2, 0, 0, 40, 30, monoDC, 0, 0, SRCCOPY);
|
|
CHECK(::GetPixel(mem2, 20, 20) == RGB(1, 2, 3));
|
|
CHECK(::GetPixel(mem2, 6, 6) == RGB(4, 5, 6));
|
|
::SelectObject(monoDC, oldMono);
|
|
::DeleteObject(mono);
|
|
::DeleteDC(monoDC);
|
|
|
|
// GetDIBits as 24 bits, bottom row first.
|
|
BITMAPINFO bi = {};
|
|
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
|
bi.bmiHeader.biWidth = 40;
|
|
bi.bmiHeader.biHeight = 30;
|
|
bi.bmiHeader.biPlanes = 1;
|
|
bi.bmiHeader.biBitCount = 24;
|
|
std::vector<BYTE> bits(((40 * 3 + 3) & ~3) * 30);
|
|
::SelectObject(mem, old);
|
|
CHECK(::GetDIBits(screen, bm, 0, 30, &bits[0], &bi, DIB_RGB_COLORS) == 30);
|
|
int stride = (40 * 3 + 3) & ~3;
|
|
BYTE* px = &bits[(29 - 5) * stride + 5 * 3]; // (5, 5)
|
|
CHECK(px[0] == 0 && px[1] == 255 && px[2] == 0);
|
|
|
|
::SelectObject(mem2, old2);
|
|
::DeleteObject(bm2);
|
|
::DeleteDC(mem2);
|
|
::DeleteObject(green);
|
|
::DeleteObject(bm);
|
|
::DeleteDC(mem);
|
|
::ReleaseDC(NULL, screen);
|
|
}
|
|
|
|
static void TestDIBSections() {
|
|
HDC screen = ::GetDC(NULL);
|
|
HDC mem = ::CreateCompatibleDC(screen);
|
|
// 32 bits, top-down: the bits are the pixels.
|
|
BITMAPINFO bi = {};
|
|
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
|
bi.bmiHeader.biWidth = 8;
|
|
bi.bmiHeader.biHeight = -4;
|
|
bi.bmiHeader.biPlanes = 1;
|
|
bi.bmiHeader.biBitCount = 32;
|
|
void* p = NULL;
|
|
HBITMAP dib = ::CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &p, NULL, 0);
|
|
CHECK(dib && p);
|
|
DWORD* px = (DWORD*)p;
|
|
px[1 * 8 + 2] = 0x00336699; // (2, 1)
|
|
HGDIOBJ old = ::SelectObject(mem, dib);
|
|
CHECK(::GetPixel(mem, 2, 1) == RGB(0x33, 0x66, 0x99));
|
|
::SetPixel(mem, 3, 2, RGB(1, 2, 3));
|
|
::GdiFlush();
|
|
CHECK((px[2 * 8 + 3] & 0xFFFFFF) == 0x010203);
|
|
DIBSECTION ds;
|
|
CHECK(::GetObjectW(dib, sizeof ds, &ds) == sizeof ds && ds.dsBm.bmBits == p && ds.dsBmih.biBitCount == 32);
|
|
::SelectObject(mem, old);
|
|
::DeleteObject(dib);
|
|
|
|
// 8 bits with a color table, bottom-up.
|
|
struct {
|
|
BITMAPINFOHEADER h;
|
|
RGBQUAD colors[2];
|
|
} b8 = {};
|
|
b8.h.biSize = sizeof(BITMAPINFOHEADER);
|
|
b8.h.biWidth = 4;
|
|
b8.h.biHeight = 2;
|
|
b8.h.biPlanes = 1;
|
|
b8.h.biBitCount = 8;
|
|
b8.h.biClrUsed = 2;
|
|
b8.colors[1].rgbRed = 200;
|
|
dib = ::CreateDIBSection(screen, (BITMAPINFO*)&b8, DIB_RGB_COLORS, &p, NULL, 0);
|
|
CHECK(dib && p);
|
|
((BYTE*)p)[0] = 1; // the bottom row's first pixel
|
|
old = ::SelectObject(mem, dib);
|
|
CHECK(::GetPixel(mem, 0, 1) == RGB(200, 0, 0));
|
|
CHECK(::GetPixel(mem, 0, 0) == RGB(0, 0, 0));
|
|
::SelectObject(mem, old);
|
|
::DeleteObject(dib);
|
|
::DeleteDC(mem);
|
|
::ReleaseDC(NULL, screen);
|
|
}
|
|
|
|
static void TestRegionsAndClipping() {
|
|
HRGN a = ::CreateRectRgn(0, 0, 10, 10);
|
|
HRGN b = ::CreateRectRgn(5, 5, 15, 15);
|
|
HRGN c = ::CreateRectRgn(0, 0, 0, 0);
|
|
CHECK(::CombineRgn(c, a, b, RGN_AND) == SIMPLEREGION);
|
|
RECT box;
|
|
::GetRgnBox(c, &box);
|
|
CHECK(box.left == 5 && box.top == 5 && box.right == 10 && box.bottom == 10);
|
|
CHECK(::CombineRgn(c, a, b, RGN_OR) == COMPLEXREGION);
|
|
CHECK(::PtInRegion(c, 12, 12) && !::PtInRegion(c, 12, 2));
|
|
CHECK(::CombineRgn(c, a, b, RGN_DIFF) == COMPLEXREGION && !::PtInRegion(c, 7, 7));
|
|
::OffsetRgn(a, 100, 0);
|
|
CHECK(::PtInRegion(a, 105, 5));
|
|
HRGN e = ::CreateEllipticRgn(0, 0, 20, 20);
|
|
CHECK(::PtInRegion(e, 10, 10) && !::PtInRegion(e, 1, 1));
|
|
|
|
HDC screen = ::GetDC(NULL);
|
|
HDC mem = ::CreateCompatibleDC(screen);
|
|
HBITMAP bm = ::CreateCompatibleBitmap(screen, 20, 20);
|
|
HGDIOBJ old = ::SelectObject(mem, bm);
|
|
RECT all = {0, 0, 20, 20};
|
|
::FillRect(mem, &all, (HBRUSH)::GetStockObject(BLACK_BRUSH));
|
|
HRGN clip = ::CreateRectRgn(5, 5, 10, 10);
|
|
::SelectClipRgn(mem, clip);
|
|
::FillRect(mem, &all, (HBRUSH)::GetStockObject(WHITE_BRUSH));
|
|
CHECK(::GetPixel(mem, 6, 6) == RGB(255, 255, 255));
|
|
CHECK(::GetPixel(mem, 2, 2) == RGB(0, 0, 0));
|
|
::GetClipBox(mem, &box);
|
|
CHECK(box.left == 5 && box.right == 10);
|
|
::ExcludeClipRect(mem, 5, 5, 7, 7);
|
|
CHECK(!::PtVisible(mem, 6, 6) && ::PtVisible(mem, 8, 8));
|
|
::SelectClipRgn(mem, NULL);
|
|
CHECK(::PtVisible(mem, 1, 1));
|
|
|
|
// SaveDC and RestoreDC bring back the attributes.
|
|
int saved = ::SaveDC(mem);
|
|
::SetTextColor(mem, RGB(1, 1, 1));
|
|
::IntersectClipRect(mem, 0, 0, 2, 2);
|
|
::RestoreDC(mem, saved);
|
|
CHECK(::GetTextColor(mem) == RGB(0, 0, 0));
|
|
CHECK(::PtVisible(mem, 15, 15));
|
|
|
|
// An anisotropic mapping doubles everything.
|
|
::SetMapMode(mem, MM_ANISOTROPIC);
|
|
::SetWindowExtEx(mem, 10, 10, NULL);
|
|
::SetViewportExtEx(mem, 20, 20, NULL);
|
|
POINT pt = {3, 4};
|
|
::LPtoDP(mem, &pt, 1);
|
|
CHECK(pt.x == 6 && pt.y == 8);
|
|
::SetWindowOrgEx(mem, 1, 1, NULL);
|
|
RECT r = {1, 1, 3, 3};
|
|
::FillRect(mem, &r, (HBRUSH)::GetStockObject(GRAY_BRUSH));
|
|
::SetMapMode(mem, MM_TEXT);
|
|
::SetWindowOrgEx(mem, 0, 0, NULL); // the origin outlives the mapping mode
|
|
CHECK(::GetPixel(mem, 3, 3) == RGB(128, 128, 128));
|
|
CHECK(::GetPixel(mem, 4, 4) == RGB(0, 0, 0));
|
|
|
|
::SelectObject(mem, old);
|
|
::DeleteObject(bm);
|
|
::DeleteDC(mem);
|
|
::ReleaseDC(NULL, screen);
|
|
::DeleteObject(a);
|
|
::DeleteObject(b);
|
|
::DeleteObject(c);
|
|
::DeleteObject(e);
|
|
::DeleteObject(clip);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Lines, shapes, paths and text
|
|
|
|
// A 32-bit top-down DIB section in a memory DC, filled white.
|
|
struct Canvas {
|
|
HDC dc;
|
|
HBITMAP bm;
|
|
HGDIOBJ old;
|
|
DWORD* bits;
|
|
int w, h;
|
|
Canvas(int cx, int cy) : w(cx), h(cy) {
|
|
dc = ::CreateCompatibleDC(NULL);
|
|
BITMAPINFO bi = {};
|
|
bi.bmiHeader.biSize = sizeof bi.bmiHeader;
|
|
bi.bmiHeader.biWidth = cx;
|
|
bi.bmiHeader.biHeight = -cy;
|
|
bi.bmiHeader.biPlanes = 1;
|
|
bi.bmiHeader.biBitCount = 32;
|
|
void* p = NULL;
|
|
bm = ::CreateDIBSection(dc, &bi, DIB_RGB_COLORS, &p, NULL, 0);
|
|
bits = (DWORD*)p;
|
|
old = ::SelectObject(dc, bm);
|
|
RECT all = {0, 0, cx, cy};
|
|
::FillRect(dc, &all, (HBRUSH)::GetStockObject(WHITE_BRUSH));
|
|
}
|
|
~Canvas() {
|
|
::SelectObject(dc, old);
|
|
::DeleteObject(bm);
|
|
::DeleteDC(dc);
|
|
}
|
|
DWORD At(int x, int y) {
|
|
::GdiFlush();
|
|
return bits[y * w + x] & 0xFFFFFF;
|
|
}
|
|
// Whether any pixel in the rectangle differs from white.
|
|
bool Inked(int l, int t, int r, int b) {
|
|
::GdiFlush();
|
|
for (int y = t; y < b; y++)
|
|
for (int x = l; x < r; x++)
|
|
if (x >= 0 && y >= 0 && x < w && y < h && (bits[y * w + x] & 0xFFFFFF) != 0xFFFFFF) return true;
|
|
return false;
|
|
}
|
|
};
|
|
|
|
static void TestShapes() {
|
|
Canvas c(120, 100);
|
|
const DWORD kWhite = 0xFFFFFF, kBlack = 0, kRed = 0xFF0000;
|
|
HBRUSH red = ::CreateSolidBrush(RGB(255, 0, 0));
|
|
HGDIOBJ oldBrush = ::SelectObject(c.dc, red);
|
|
// A rectangle leaves out its right and bottom edges; the pen draws the
|
|
// border and the brush the inside.
|
|
CHECK(::Rectangle(c.dc, 0, 0, 10, 10));
|
|
CHECK(c.At(0, 0) == kBlack && c.At(9, 9) == kBlack && c.At(9, 0) == kBlack);
|
|
CHECK(c.At(5, 5) == kRed && c.At(10, 10) == kWhite && c.At(10, 5) == kWhite);
|
|
// With no pen the filled area is a pixel smaller each way.
|
|
HGDIOBJ oldPen = ::SelectObject(c.dc, ::GetStockObject(NULL_PEN));
|
|
::Rectangle(c.dc, 20, 0, 30, 10);
|
|
CHECK(c.At(20, 0) == kRed && c.At(28, 8) == kRed && c.At(29, 9) == kWhite);
|
|
::SelectObject(c.dc, ::GetStockObject(BLACK_PEN));
|
|
// An ellipse: filled in the middle, not in the corners of its box.
|
|
::Ellipse(c.dc, 40, 0, 60, 20);
|
|
CHECK(c.At(50, 10) == kRed && c.At(40, 0) == kWhite && c.At(59, 19) == kWhite);
|
|
CHECK(c.At(40, 10) == kBlack || c.At(40, 9) == kBlack);
|
|
// LineTo leaves out the last pixel and moves the current position.
|
|
::MoveToEx(c.dc, 0, 30, NULL);
|
|
CHECK(::LineTo(c.dc, 10, 30));
|
|
CHECK(c.At(0, 30) == kBlack && c.At(9, 30) == kBlack && c.At(10, 30) == kWhite);
|
|
POINT cur;
|
|
CHECK(::GetCurrentPositionEx(c.dc, &cur) && cur.x == 10 && cur.y == 30);
|
|
// Drawn twice with XOR, a line leaves nothing.
|
|
int oldRop = ::SetROP2(c.dc, R2_XORPEN);
|
|
HPEN white = ::CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
|
|
::SelectObject(c.dc, white);
|
|
::MoveToEx(c.dc, 0, 35, NULL);
|
|
::LineTo(c.dc, 20, 45);
|
|
CHECK(c.At(0, 35) == kBlack);
|
|
::MoveToEx(c.dc, 0, 35, NULL);
|
|
::LineTo(c.dc, 20, 45);
|
|
CHECK(!c.Inked(0, 34, 21, 46));
|
|
::SetROP2(c.dc, oldRop);
|
|
// A wide pen is centered on the line.
|
|
HPEN wide = ::CreatePen(PS_SOLID, 5, RGB(0, 0, 0));
|
|
::SelectObject(c.dc, wide);
|
|
::MoveToEx(c.dc, 0, 55, NULL);
|
|
::LineTo(c.dc, 30, 55);
|
|
CHECK(c.At(15, 53) == kBlack && c.At(15, 57) == kBlack && c.At(15, 50) == kWhite && c.At(15, 60) == kWhite);
|
|
::SelectObject(c.dc, ::GetStockObject(BLACK_PEN));
|
|
// The fill mode decides whether a star's middle is filled.
|
|
POINT star[] = {{80, 30}, {92, 70}, {60, 45}, {100, 45}, {68, 70}};
|
|
::SetPolyFillMode(c.dc, ALTERNATE);
|
|
::Polygon(c.dc, star, 5);
|
|
CHECK(c.At(80, 52) == kWhite && c.At(80, 40) == kRed);
|
|
::SetPolyFillMode(c.dc, WINDING);
|
|
::Polygon(c.dc, star, 5);
|
|
CHECK(c.At(80, 52) == kRed);
|
|
// Flood fill stops at the border color.
|
|
::SelectObject(c.dc, ::GetStockObject(NULL_BRUSH));
|
|
::Rectangle(c.dc, 0, 70, 20, 90);
|
|
HBRUSH blue = ::CreateSolidBrush(RGB(0, 0, 255));
|
|
::SelectObject(c.dc, blue);
|
|
CHECK(::FloodFill(c.dc, 10, 80, RGB(0, 0, 0)));
|
|
CHECK(c.At(10, 80) == 0x0000FF && c.At(1, 71) == 0x0000FF && c.At(0, 70) == kBlack && c.At(25, 80) == kWhite);
|
|
::SelectObject(c.dc, oldBrush);
|
|
::SelectObject(c.dc, oldPen);
|
|
::DeleteObject(red);
|
|
::DeleteObject(blue);
|
|
::DeleteObject(white);
|
|
::DeleteObject(wide);
|
|
|
|
// Paths
|
|
Canvas p(60, 60);
|
|
CHECK(::BeginPath(p.dc));
|
|
::MoveToEx(p.dc, 10, 10, NULL);
|
|
::LineTo(p.dc, 40, 10);
|
|
::LineTo(p.dc, 40, 40);
|
|
CHECK(::CloseFigure(p.dc));
|
|
CHECK(::EndPath(p.dc));
|
|
CHECK(!p.Inked(0, 0, 60, 60));
|
|
int n = ::GetPath(p.dc, NULL, NULL, 0);
|
|
CHECK(n == 3);
|
|
POINT pts[3];
|
|
BYTE types[3];
|
|
CHECK(::GetPath(p.dc, pts, types, 3) == 3);
|
|
CHECK(types[0] == PT_MOVETO && types[1] == PT_LINETO && types[2] == (PT_LINETO | PT_CLOSEFIGURE));
|
|
CHECK(pts[2].x == 40 && pts[2].y == 40);
|
|
HRGN rgn = ::PathToRegion(p.dc);
|
|
CHECK(rgn != NULL);
|
|
RECT box;
|
|
CHECK(::GetRgnBox(rgn, &box) != ERROR && box.left >= 10 && box.right <= 41 && box.top >= 10 && box.bottom <= 41);
|
|
CHECK(::PtInRegion(rgn, 35, 15) && !::PtInRegion(rgn, 15, 35));
|
|
::DeleteObject(rgn);
|
|
CHECK(::GetPath(p.dc, NULL, NULL, 0) == -1); // PathToRegion used the path up
|
|
::BeginPath(p.dc);
|
|
::Rectangle(p.dc, 5, 5, 25, 25);
|
|
::EndPath(p.dc);
|
|
::SelectObject(p.dc, ::GetStockObject(BLACK_BRUSH));
|
|
CHECK(::FillPath(p.dc));
|
|
CHECK(p.At(15, 15) == kBlack && p.At(30, 30) == kWhite);
|
|
}
|
|
|
|
static void TestText() {
|
|
Canvas c(300, 120);
|
|
HFONT font = ::CreateFontW(-13, 0, 0, 0, FW_NORMAL, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, L"Arial");
|
|
HGDIOBJ oldFont = ::SelectObject(c.dc, font);
|
|
TEXTMETRICW tm;
|
|
CHECK(::GetTextMetricsW(c.dc, &tm));
|
|
CHECK(tm.tmHeight > 8 && tm.tmHeight < 30 && tm.tmAscent + tm.tmDescent == tm.tmHeight);
|
|
CHECK(tm.tmAveCharWidth > 0 && tm.tmMaxCharWidth >= tm.tmAveCharWidth);
|
|
TEXTMETRICA tma;
|
|
CHECK(::GetTextMetricsA(c.dc, &tma) && tma.tmHeight == tm.tmHeight);
|
|
wchar_t face[LF_FACESIZE];
|
|
CHECK(::GetTextFaceW(c.dc, LF_FACESIZE, face) > 0);
|
|
|
|
SIZE sz;
|
|
CHECK(::GetTextExtentPoint32W(c.dc, L"Hello", 5, &sz));
|
|
CHECK(sz.cx > 10 && sz.cy == tm.tmHeight);
|
|
SIZE sza;
|
|
CHECK(::GetTextExtentPoint32A(c.dc, "Hello", 5, &sza) && sza.cx == sz.cx);
|
|
SIZE wide, narrow;
|
|
::GetTextExtentPoint32W(c.dc, L"WWWW", 4, &wide);
|
|
::GetTextExtentPoint32W(c.dc, L"iiii", 4, &narrow);
|
|
CHECK(wide.cx >= narrow.cx);
|
|
int widths[5];
|
|
CHECK(::GetCharWidth32W(c.dc, 'a', 'e', widths) && widths[0] > 0);
|
|
int fit = 0, dx[5];
|
|
SIZE ext5;
|
|
CHECK(::GetTextExtentExPointW(c.dc, L"Hello", 5, sz.cx - 1, &fit, dx, &ext5));
|
|
CHECK(fit == 4 && dx[4] == sz.cx && dx[0] > 0 && dx[0] < dx[4]);
|
|
|
|
// Text draws inside its box, from the top-left by default.
|
|
CHECK(::TextOutW(c.dc, 10, 10, L"Hello", 5));
|
|
CHECK(c.Inked(10, 10, 10 + sz.cx, 10 + sz.cy));
|
|
CHECK(!c.Inked(0, 0, 300, 9) && !c.Inked(12 + sz.cx, 0, 300, 120) && !c.Inked(0, 11 + sz.cy, 300, 120));
|
|
// TA_UPDATECP moves the current position past the text.
|
|
::SetTextAlign(c.dc, TA_UPDATECP);
|
|
::MoveToEx(c.dc, 100, 10, NULL);
|
|
::TextOutW(c.dc, 0, 0, L"Hello", 5);
|
|
POINT cur;
|
|
::GetCurrentPositionEx(c.dc, &cur);
|
|
CHECK(cur.x == 100 + sz.cx && cur.y == 10);
|
|
::SetTextAlign(c.dc, TA_LEFT | TA_TOP);
|
|
// An opaque background fills the text's box in the background color.
|
|
::SetBkColor(c.dc, RGB(0, 255, 0));
|
|
::SetBkMode(c.dc, OPAQUE);
|
|
::TextOutW(c.dc, 200, 10, L" ", 1);
|
|
CHECK(c.At(200, 10) == 0x00FF00);
|
|
::SetBkMode(c.dc, TRANSPARENT);
|
|
|
|
// DrawText measures one line, or breaks words to fit a width.
|
|
RECT r = {0, 0, 0, 0};
|
|
int h = ::DrawTextW(c.dc, L"Hello", -1, &r, DT_CALCRECT | DT_SINGLELINE | DT_NOPREFIX);
|
|
CHECK(h == tm.tmHeight && r.right == sz.cx && r.bottom == tm.tmHeight);
|
|
RECT wrap = {0, 0, sz.cx + 5, 0};
|
|
h = ::DrawTextW(c.dc, L"Hello Hello Hello", -1, &wrap, DT_CALCRECT | DT_WORDBREAK);
|
|
CHECK(h == 3 * tm.tmHeight && wrap.bottom == h && wrap.right <= sz.cx + 5);
|
|
// A prefix character is not drawn.
|
|
RECT pr = {0, 0, 0, 0};
|
|
::DrawTextW(c.dc, L"&Hello", -1, &pr, DT_CALCRECT | DT_SINGLELINE);
|
|
CHECK(pr.right == sz.cx);
|
|
// Centered in a box, vertically too.
|
|
RECT box = {0, 50, 300, 110};
|
|
::DrawTextW(c.dc, L"Hello", -1, &box, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
|
|
CHECK(c.Inked(140 - sz.cx / 2, 50, 160 + sz.cx / 2, 110) && !c.Inked(0, 50, 145 - sz.cx / 2, 110));
|
|
// An ellipsis replaces what does not fit, and DT_MODIFYSTRING says so.
|
|
wchar_t buf[64] = L"Hello Hello Hello";
|
|
RECT el = {0, 0, sz.cx, 20};
|
|
::DrawTextExW(c.dc, buf, -1, &el, DT_SINGLELINE | DT_END_ELLIPSIS | DT_MODIFYSTRING, NULL);
|
|
size_t len = wcslen(buf);
|
|
CHECK(len < 17 && len >= 3 && wcscmp(buf + len - 3, L"...") == 0);
|
|
// Tabs
|
|
DWORD ext = ::GetTabbedTextExtentW(c.dc, L"a\tb", 3, 0, NULL);
|
|
CHECK(HIWORD(ext) == tm.tmHeight && LOWORD(ext) >= 8 * tm.tmAveCharWidth);
|
|
|
|
// There is at least one font family to enumerate.
|
|
struct Counter {
|
|
static int CALLBACK Proc(const LOGFONTW* lf, const TEXTMETRICW*, DWORD, LPARAM n) {
|
|
if (lf->lfFaceName[0]) ++*(int*)n;
|
|
return 1;
|
|
}
|
|
};
|
|
int families = 0;
|
|
LOGFONTW want = {};
|
|
want.lfCharSet = DEFAULT_CHARSET;
|
|
::EnumFontFamiliesExW(c.dc, &want, Counter::Proc, (LPARAM)&families, 0);
|
|
CHECK(families > 0);
|
|
::SelectObject(c.dc, oldFont);
|
|
::DeleteObject(font);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------
|
|
// Icons, cursors and accelerators
|
|
|
|
static WPARAM g_lastCommand = 0;
|
|
static std::vector<UINT> g_menuMsgs;
|
|
static int g_clickItem = -1; // a popup item to click at the next WM_ENTERIDLE
|
|
static HMENU g_clickMenu = NULL;
|
|
|
|
static LRESULT CALLBACK MenuProc(HWND h, UINT msg, WPARAM wp, LPARAM lp) {
|
|
switch (msg) {
|
|
case WM_ENTERMENULOOP:
|
|
case WM_EXITMENULOOP:
|
|
case WM_INITMENU:
|
|
case WM_INITMENUPOPUP:
|
|
case WM_UNINITMENUPOPUP:
|
|
case WM_MENUSELECT:
|
|
g_menuMsgs.push_back(msg);
|
|
break;
|
|
case WM_ENTERIDLE:
|
|
if (wp == MSGF_MENU && g_clickItem >= 0) {
|
|
RECT r;
|
|
if (::GetMenuItemRect(h, g_clickMenu, g_clickItem, &r)) {
|
|
POINT p = {(r.left + r.right) / 2, (r.top + r.bottom) / 2};
|
|
::ScreenToClient(h, &p);
|
|
::PostMessageW(h, WM_MOUSEMOVE, 0, MAKELPARAM(p.x, p.y));
|
|
::PostMessageW(h, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(p.x, p.y));
|
|
::PostMessageW(h, WM_LBUTTONUP, 0, MAKELPARAM(p.x, p.y));
|
|
}
|
|
g_clickItem = -1;
|
|
}
|
|
return 0;
|
|
case WM_COMMAND:
|
|
g_lastCommand = wp;
|
|
return 0;
|
|
}
|
|
return ::DefWindowProcW(h, msg, wp, lp);
|
|
}
|
|
|
|
static HWND NewMenuWindow(HMENU menu) {
|
|
static ATOM atom = 0;
|
|
if (!atom) {
|
|
WNDCLASSW wc = {};
|
|
wc.lpfnWndProc = MenuProc;
|
|
wc.hInstance = ::GetModuleHandleW(NULL);
|
|
wc.lpszClassName = L"WinTestMenu";
|
|
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
|
|
atom = ::RegisterClassW(&wc);
|
|
}
|
|
return ::CreateWindowExW(0, L"WinTestMenu", L"Menus", WS_OVERLAPPEDWINDOW, 20, 20, 320, 240, NULL, menu,
|
|
::GetModuleHandleW(NULL), NULL);
|
|
}
|
|
|
|
static void TestIconsAndAccelerators() {
|
|
HICON app = ::LoadIconW(NULL, (LPCWSTR)IDI_APPLICATION);
|
|
CHECK(app != NULL);
|
|
ICONINFO ii;
|
|
CHECK(::GetIconInfo(app, &ii) && ii.fIcon && ii.hbmMask != NULL);
|
|
if (ii.hbmMask) ::DeleteObject(ii.hbmMask);
|
|
if (ii.hbmColor) ::DeleteObject(ii.hbmColor);
|
|
CHECK(::LoadCursorW(NULL, (LPCWSTR)IDC_ARROW) != NULL);
|
|
|
|
// An icon made from two bitmaps draws its color where the mask is clear.
|
|
HDC screen = ::GetDC(NULL);
|
|
HBITMAP color = ::CreateCompatibleBitmap(screen, 16, 16);
|
|
HBITMAP mask = ::CreateBitmap(16, 16, 1, 1, NULL);
|
|
HDC mem = ::CreateCompatibleDC(screen);
|
|
HGDIOBJ old = ::SelectObject(mem, color);
|
|
RECT all = {0, 0, 16, 16};
|
|
HBRUSH red = ::CreateSolidBrush(RGB(255, 0, 0));
|
|
::FillRect(mem, &all, red);
|
|
::SelectObject(mem, mask);
|
|
::FillRect(mem, &all, (HBRUSH)::GetStockObject(BLACK_BRUSH));
|
|
::SelectObject(mem, old);
|
|
ICONINFO make = {TRUE, 0, 0, mask, color};
|
|
HICON icon = ::CreateIconIndirect(&make);
|
|
CHECK(icon != NULL);
|
|
HBITMAP target = ::CreateCompatibleBitmap(screen, 16, 16);
|
|
old = ::SelectObject(mem, target);
|
|
::FillRect(mem, &all, (HBRUSH)::GetStockObject(WHITE_BRUSH));
|
|
CHECK(::DrawIconEx(mem, 0, 0, icon, 16, 16, 0, NULL, DI_NORMAL));
|
|
CHECK(::GetPixel(mem, 8, 8) == RGB(255, 0, 0));
|
|
::SelectObject(mem, old);
|
|
CHECK(::DestroyIcon(icon));
|
|
::DeleteObject(target);
|
|
::DeleteObject(color);
|
|
::DeleteObject(mask);
|
|
::DeleteObject(red);
|
|
::DeleteDC(mem);
|
|
::ReleaseDC(NULL, screen);
|
|
|
|
ACCEL keys[2] = {{FVIRTKEY | FCONTROL, 'O', 100}, {FVIRTKEY, VK_F5, 101}};
|
|
HACCEL accel = ::CreateAcceleratorTableW(keys, 2);
|
|
CHECK(accel != NULL && ::CopyAcceleratorTableW(accel, NULL, 0) == 2);
|
|
HWND h = NewMenuWindow(NULL);
|
|
g_lastCommand = 0;
|
|
MSG m = {h, WM_KEYDOWN, VK_F5, 1};
|
|
CHECK(::TranslateAcceleratorW(h, accel, &m));
|
|
CHECK(LOWORD(g_lastCommand) == 101 && HIWORD(g_lastCommand) == 1);
|
|
m.wParam = 'O';
|
|
CHECK(!::TranslateAcceleratorW(h, accel, &m)); // Ctrl is not down
|
|
CHECK(::DestroyAcceleratorTable(accel));
|
|
::DestroyWindow(h);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Menus
|
|
|
|
static void TestMenus() {
|
|
HMENU bar = ::CreateMenu();
|
|
HMENU file = ::CreatePopupMenu();
|
|
CHECK(::IsMenu(bar) && ::IsMenu(file) && !::IsMenu(NULL));
|
|
CHECK(::AppendMenuW(file, MF_STRING, 100, L"&Open\tCtrl+O"));
|
|
CHECK(::AppendMenuW(file, MF_SEPARATOR, 0, NULL));
|
|
CHECK(::AppendMenuA(file, MF_STRING | MF_GRAYED, 101, "E&xit"));
|
|
CHECK(::AppendMenuW(bar, MF_POPUP, (UINT_PTR)file, L"&File"));
|
|
CHECK(::GetMenuItemCount(bar) == 1 && ::GetMenuItemCount(file) == 3);
|
|
CHECK(::GetSubMenu(bar, 0) == file);
|
|
CHECK(::GetMenuItemID(bar, 0) == (UINT)-1 && ::GetMenuItemID(file, 2) == 101);
|
|
CHECK(::GetMenuState(file, 1, MF_BYPOSITION) & MF_SEPARATOR);
|
|
// A search by command looks into submenus.
|
|
CHECK(::GetMenuState(bar, 101, MF_BYCOMMAND) & MF_GRAYED);
|
|
CHECK((::GetMenuState(bar, 0, MF_BYPOSITION) >> 8) == 3);
|
|
CHECK(::GetMenuState(bar, 999, MF_BYCOMMAND) == (UINT)-1);
|
|
wchar_t buf[64];
|
|
CHECK(::GetMenuStringW(bar, 100, buf, 64, MF_BYCOMMAND) == 12 && wcscmp(buf, L"&Open\tCtrl+O") == 0);
|
|
CHECK(::GetMenuStringW(file, 100, NULL, 0, MF_BYCOMMAND) == 12);
|
|
char abuf[8];
|
|
CHECK(::GetMenuStringA(file, 101, abuf, sizeof abuf, MF_BYCOMMAND) == 5 && strcmp(abuf, "E&xit") == 0);
|
|
CHECK(::EnableMenuItem(bar, 101, MF_BYCOMMAND | MF_ENABLED) == MF_GRAYED);
|
|
CHECK(::CheckMenuItem(file, 100, MF_BYCOMMAND | MF_CHECKED) == MF_UNCHECKED);
|
|
CHECK(::CheckMenuItem(file, 100, MF_BYCOMMAND | MF_UNCHECKED) == MF_CHECKED);
|
|
CHECK(::CheckMenuItem(file, 999, MF_BYCOMMAND) == (DWORD)-1);
|
|
CHECK(::ModifyMenuW(file, 100, MF_BYCOMMAND | MF_STRING, 102, L"&New"));
|
|
CHECK(::GetMenuItemID(file, 0) == 102);
|
|
CHECK(::InsertMenuW(file, 0, MF_BYPOSITION | MF_STRING, 103, L"First"));
|
|
CHECK(::GetMenuItemID(file, 0) == 103 && ::GetMenuItemCount(file) == 4);
|
|
CHECK(::DeleteMenu(file, 103, MF_BYCOMMAND) && ::GetMenuItemCount(file) == 3);
|
|
CHECK(!::RemoveMenu(file, 999, MF_BYCOMMAND));
|
|
|
|
MENUITEMINFOW mi = {sizeof mi};
|
|
mi.fMask = MIIM_STRING | MIIM_ID;
|
|
mi.dwTypeData = buf;
|
|
mi.cch = 64;
|
|
CHECK(::GetMenuItemInfoW(file, 0, TRUE, &mi));
|
|
CHECK(mi.wID == 102 && mi.cch == 4 && wcscmp(buf, L"&New") == 0);
|
|
mi.fMask = MIIM_DATA | MIIM_STATE;
|
|
mi.dwItemData = 1234;
|
|
mi.fState = MFS_CHECKED | MFS_DEFAULT;
|
|
CHECK(::SetMenuItemInfoW(file, 102, FALSE, &mi));
|
|
CHECK(::GetMenuDefaultItem(file, FALSE, 0) == 102 && ::GetMenuDefaultItem(file, TRUE, 0) == 0);
|
|
memset(&mi, 0, sizeof mi);
|
|
mi.cbSize = sizeof mi;
|
|
mi.fMask = MIIM_DATA | MIIM_STATE;
|
|
CHECK(::GetMenuItemInfoW(file, 102, FALSE, &mi) && mi.dwItemData == 1234 && (mi.fState & MFS_CHECKED));
|
|
::CheckMenuItem(file, 102, MF_BYCOMMAND | MF_UNCHECKED);
|
|
|
|
HMENU view = ::CreatePopupMenu();
|
|
for (UINT i = 0; i < 3; i++) ::AppendMenuW(view, MF_STRING, 200 + i, L"Mode");
|
|
CHECK(::CheckMenuRadioItem(view, 200, 202, 201, MF_BYCOMMAND));
|
|
CHECK(!(::GetMenuState(view, 200, MF_BYCOMMAND) & MF_CHECKED));
|
|
CHECK(::GetMenuState(view, 201, MF_BYCOMMAND) & MF_CHECKED);
|
|
MENUITEMINFOW ri = {sizeof ri};
|
|
ri.fMask = MIIM_FTYPE;
|
|
CHECK(::GetMenuItemInfoW(view, 201, FALSE, &ri) && (ri.fType & MFT_RADIOCHECK));
|
|
CHECK(::InsertMenuW(bar, (UINT)-1, MF_BYPOSITION | MF_POPUP, (UINT_PTR)view, L"&View"));
|
|
CHECK(::GetMenuItemCount(bar) == 2);
|
|
MENUINFO info = {sizeof info};
|
|
info.fMask = MIM_MENUDATA;
|
|
info.dwMenuData = 77;
|
|
CHECK(::SetMenuInfo(view, &info));
|
|
info.dwMenuData = 0;
|
|
CHECK(::GetMenuInfo(view, &info) && info.dwMenuData == 77);
|
|
|
|
// Templates: the standard form and the extended one.
|
|
static const WORD tmpl[] = {0, 0, MF_POPUP | MF_END, '&', 'F', 0, 0, 1, 'A', 0, MF_END, 2, 'B', 0};
|
|
HMENU loaded = ::LoadMenuIndirectW(tmpl);
|
|
CHECK(loaded && ::GetMenuItemCount(loaded) == 1);
|
|
HMENU sub = ::GetSubMenu(loaded, 0);
|
|
CHECK(sub && ::GetMenuItemCount(sub) == 2 && ::GetMenuItemID(sub, 1) == 2);
|
|
CHECK(::DestroyMenu(loaded) && !::IsMenu(sub));
|
|
BYTE ex[56] = {};
|
|
auto put16 = [&](int at, WORD v) { memcpy(ex + at, &v, 2); };
|
|
auto put32 = [&](int at, DWORD v) { memcpy(ex + at, &v, 4); };
|
|
// The header: version 1, then the offset past its help ID.
|
|
put16(0, 1);
|
|
put16(2, 4);
|
|
// A popup that is the last item: type, state, ID, flags, text, padding
|
|
// to a DWORD, and the submenu's help ID.
|
|
put16(20, 0x81);
|
|
put16(22, 'F');
|
|
// Its one item, checked, with ID 5.
|
|
put32(36, MFS_CHECKED);
|
|
put32(40, 5);
|
|
put16(44, 0x80);
|
|
put16(46, 'X');
|
|
loaded = ::LoadMenuIndirectW(ex);
|
|
CHECK(loaded && ::GetMenuItemCount(loaded) == 1);
|
|
sub = loaded ? ::GetSubMenu(loaded, 0) : NULL;
|
|
CHECK(sub && ::GetMenuItemID(sub, 0) == 5 && (::GetMenuState(sub, 0, MF_BYPOSITION) & MF_CHECKED));
|
|
if (loaded) ::DestroyMenu(loaded);
|
|
|
|
// A window's menu bar takes room from the client area.
|
|
HWND h = NewMenuWindow(bar);
|
|
CHECK(h && ::GetMenu(h) == bar);
|
|
RECT withBar, without;
|
|
::GetClientRect(h, &withBar);
|
|
CHECK(::SetMenu(h, NULL) && ::GetMenu(h) == NULL);
|
|
::GetClientRect(h, &without);
|
|
CHECK(without.bottom - withBar.bottom == ::GetSystemMetrics(SM_CYMENU));
|
|
CHECK(::SetMenu(h, bar));
|
|
RECT item, wr;
|
|
::GetWindowRect(h, &wr);
|
|
CHECK(::GetMenuItemRect(h, bar, 0, &item));
|
|
CHECK(item.left >= wr.left && item.top > wr.top && item.bottom < wr.bottom && item.right > item.left);
|
|
POINT mid = {(item.left + item.right) / 2, (item.top + item.bottom) / 2};
|
|
CHECK(::MenuItemFromPoint(h, bar, mid) == 0);
|
|
|
|
HMENU sys = ::GetSystemMenu(h, FALSE);
|
|
CHECK(sys && ::GetMenuItemCount(sys) >= 5);
|
|
CHECK(::GetMenuState(sys, SC_CLOSE, MF_BYCOMMAND) != (UINT)-1);
|
|
CHECK(::GetSystemMenu(h, TRUE) == NULL);
|
|
|
|
#ifdef _WINPORT
|
|
// Tracking, driven by posted input.
|
|
::ShowWindow(h, SW_SHOW);
|
|
Pump();
|
|
g_menuMsgs.clear();
|
|
::PostMessageW(h, WM_KEYDOWN, VK_DOWN, 0);
|
|
::PostMessageW(h, WM_KEYDOWN, VK_RETURN, 0);
|
|
CHECK(::TrackPopupMenu(file, TPM_RETURNCMD, 50, 50, 0, h, NULL) == 102);
|
|
CHECK(!g_menuMsgs.empty() && g_menuMsgs.front() == WM_ENTERMENULOOP && g_menuMsgs.back() == WM_EXITMENULOOP);
|
|
bool init = false, select = false;
|
|
for (UINT msg : g_menuMsgs) {
|
|
init |= msg == WM_INITMENUPOPUP;
|
|
select |= msg == WM_MENUSELECT;
|
|
}
|
|
CHECK(init && select);
|
|
::PostMessageW(h, WM_KEYDOWN, VK_ESCAPE, 0);
|
|
CHECK(::TrackPopupMenu(file, TPM_RETURNCMD, 50, 50, 0, h, NULL) == 0);
|
|
// A mnemonic chooses its item, and the command is posted.
|
|
g_lastCommand = 0;
|
|
::PostMessageW(h, WM_CHAR, 'x', 0);
|
|
CHECK(::TrackPopupMenu(file, 0, 50, 50, 0, h, NULL));
|
|
Pump();
|
|
CHECK(LOWORD(g_lastCommand) == 101);
|
|
// The mouse: a click on an item.
|
|
g_clickMenu = file;
|
|
g_clickItem = 0;
|
|
CHECK(::TrackPopupMenu(file, TPM_RETURNCMD, 50, 50, 0, h, NULL) == 102);
|
|
// The menu bar from the keyboard, as Alt+V then Down twice and Enter.
|
|
g_lastCommand = 0;
|
|
::PostMessageW(h, WM_KEYDOWN, VK_DOWN, 0);
|
|
::PostMessageW(h, WM_KEYDOWN, VK_DOWN, 0);
|
|
::PostMessageW(h, WM_KEYDOWN, VK_RETURN, 0);
|
|
::SendMessageW(h, WM_SYSCOMMAND, SC_KEYMENU, 'v');
|
|
Pump();
|
|
CHECK(LOWORD(g_lastCommand) == 202);
|
|
// The bar is drawn in the menu color past its items.
|
|
HDC dc = ::GetWindowDC(h);
|
|
RECT last;
|
|
::GetMenuItemRect(h, bar, 1, &last);
|
|
CHECK(::GetPixel(dc, last.right - wr.left + 8, (last.top + last.bottom) / 2 - wr.top) == ::GetSysColor(COLOR_MENU));
|
|
::ReleaseDC(h, dc);
|
|
#endif
|
|
|
|
// The window's menu goes with it.
|
|
::DestroyWindow(h);
|
|
CHECK(!::IsMenu(bar) && !::IsMenu(file) && !::IsMenu(view));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Dialogs from an in-memory template, and the standard controls in them.
|
|
|
|
// Builds a DLGTEMPLATE with its items, in UTF-16 words.
|
|
struct DlgBuilder {
|
|
std::vector<WORD> w;
|
|
WORD* count = NULL;
|
|
void Dword(DWORD v) {
|
|
w.push_back(LOWORD(v));
|
|
w.push_back(HIWORD(v));
|
|
}
|
|
void Str(const char* s) {
|
|
while (*s) w.push_back((WORD)*s++);
|
|
w.push_back(0);
|
|
}
|
|
void Align() {
|
|
if (w.size() % 2) w.push_back(0);
|
|
}
|
|
DlgBuilder(DWORD style, short cx, short cy, const char* title) {
|
|
Dword(style | DS_SETFONT);
|
|
Dword(0);
|
|
w.push_back(0); // the item count, filled in by Item
|
|
for (short v : {(short)10, (short)10, cx, cy}) w.push_back((WORD)v);
|
|
w.push_back(0); // no menu
|
|
w.push_back(0); // the dialog class
|
|
Str(title);
|
|
w.push_back(8);
|
|
Str("MS Shell Dlg");
|
|
}
|
|
void Item(WORD cls, DWORD style, short x, short y, short cx, short cy, WORD id, const char* text) {
|
|
Align();
|
|
w[4]++;
|
|
Dword(style | WS_CHILD | WS_VISIBLE);
|
|
Dword(0);
|
|
for (short v : {x, y, cx, cy}) w.push_back((WORD)v);
|
|
w.push_back(id);
|
|
w.push_back(0xFFFF);
|
|
w.push_back(cls);
|
|
Str(text);
|
|
w.push_back(0); // no creation data
|
|
}
|
|
LPCDLGTEMPLATEW Get() { return (LPCDLGTEMPLATEW)w.data(); }
|
|
};
|
|
|
|
static LPARAM g_initParam = 0;
|
|
static std::vector<WPARAM> g_dlgCommands;
|
|
|
|
static INT_PTR CALLBACK TestDlgProc(HWND h, UINT msg, WPARAM wp, LPARAM lp) {
|
|
switch (msg) {
|
|
case WM_INITDIALOG:
|
|
g_initParam = lp;
|
|
if (lp == 2) ::PostMessageW(h, WM_COMMAND, IDOK, 0);
|
|
return TRUE;
|
|
case WM_COMMAND:
|
|
g_dlgCommands.push_back(wp);
|
|
if (LOWORD(wp) == IDOK && g_initParam == 2) ::EndDialog(h, 7);
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
static bool SawCommand(WORD id, WORD code) {
|
|
for (WPARAM wp : g_dlgCommands)
|
|
if (LOWORD(wp) == id && HIWORD(wp) == code) return true;
|
|
return false;
|
|
}
|
|
|
|
static std::wstring DlgText(HWND dlg, int id) {
|
|
wchar_t buf[128] = {0};
|
|
::GetDlgItemTextW(dlg, id, buf, 128);
|
|
return buf;
|
|
}
|
|
|
|
static void TestDialogsAndControls() {
|
|
DlgBuilder b(WS_POPUP | WS_CAPTION | WS_SYSMENU | DS_MODALFRAME, 220, 140, "Dialog");
|
|
b.Item(0x0080, BS_DEFPUSHBUTTON | WS_TABSTOP | WS_GROUP, 160, 5, 50, 14, IDOK, "OK");
|
|
b.Item(0x0082, SS_LEFT, 5, 5, 30, 8, 16, "&Name:");
|
|
b.Item(0x0081, ES_AUTOHSCROLL | WS_BORDER | WS_TABSTOP | WS_GROUP, 40, 5, 100, 12, 10, "");
|
|
b.Item(0x0080, BS_AUTOCHECKBOX | WS_TABSTOP | WS_GROUP, 5, 22, 60, 10, 11, "&Check");
|
|
b.Item(0x0080, BS_AUTORADIOBUTTON | WS_TABSTOP | WS_GROUP, 5, 34, 60, 10, 12, "One");
|
|
b.Item(0x0080, BS_AUTORADIOBUTTON, 5, 46, 60, 10, 13, "Two");
|
|
b.Item(0x0083, LBS_STANDARD | WS_TABSTOP | WS_GROUP, 80, 22, 60, 50, 14, "");
|
|
b.Item(0x0085, CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP, 150, 22, 60, 60, 15, "");
|
|
|
|
g_dlgCommands.clear();
|
|
HWND dlg = ::CreateDialogIndirectParamW(::GetModuleHandleW(NULL), b.Get(), NULL, TestDlgProc, 1);
|
|
CHECK(dlg && g_initParam == 1);
|
|
wchar_t cls[32];
|
|
::GetClassNameW(dlg, cls, 32);
|
|
CHECK(wcscmp(cls, L"#32770") == 0);
|
|
HWND edit = ::GetDlgItem(dlg, 10);
|
|
CHECK(edit && ::GetDlgCtrlID(edit) == 10);
|
|
::GetClassNameW(edit, cls, 32);
|
|
CHECK(_wcsicmp(cls, L"Edit") == 0);
|
|
CHECK(LOWORD(::SendMessageW(dlg, DM_GETDEFID, 0, 0)) == IDOK);
|
|
RECT units = {0, 0, 4, 8};
|
|
CHECK(::MapDialogRect(dlg, &units) && units.right > 0 && units.bottom > 0);
|
|
|
|
// Text and numbers through the dialog helpers.
|
|
CHECK(::SetDlgItemTextW(dlg, 10, L"hello world"));
|
|
CHECK(DlgText(dlg, 10) == L"hello world");
|
|
CHECK(::SetDlgItemInt(dlg, 10, (UINT)-42, TRUE));
|
|
BOOL ok = FALSE;
|
|
CHECK((int)::GetDlgItemInt(dlg, 10, &ok, TRUE) == -42 && ok);
|
|
::SetDlgItemTextW(dlg, 10, L"abc");
|
|
::GetDlgItemInt(dlg, 10, &ok, TRUE);
|
|
CHECK(!ok);
|
|
|
|
// The edit control: selection, replacement and undo.
|
|
::SetWindowTextW(edit, L"hello world");
|
|
::SendMessageW(edit, EM_SETSEL, 0, 5);
|
|
DWORD s0 = 0, s1 = 0;
|
|
::SendMessageW(edit, EM_GETSEL, (WPARAM)&s0, (LPARAM)&s1);
|
|
CHECK(s0 == 0 && s1 == 5);
|
|
::SendMessageW(edit, EM_REPLACESEL, TRUE, (LPARAM)L"HELLO");
|
|
CHECK(DlgText(dlg, 10) == L"HELLO world");
|
|
CHECK(::SendMessageW(edit, EM_CANUNDO, 0, 0));
|
|
::SendMessageW(edit, EM_UNDO, 0, 0);
|
|
CHECK(DlgText(dlg, 10) == L"hello world");
|
|
::SendMessageW(edit, EM_LIMITTEXT, 5, 0);
|
|
::SendMessageW(edit, EM_SETSEL, 0, -1);
|
|
::SendMessageW(edit, EM_REPLACESEL, FALSE, (LPARAM)L"123456789");
|
|
CHECK(DlgText(dlg, 10) == L"12345");
|
|
::SendMessageW(edit, EM_LIMITTEXT, 0, 0);
|
|
CHECK(SawCommand(10, EN_CHANGE));
|
|
|
|
// Copy and paste through the clipboard, which converts text.
|
|
::SetWindowTextW(edit, L"copy me");
|
|
::SendMessageW(edit, EM_SETSEL, 0, 4);
|
|
::SendMessageW(edit, WM_COPY, 0, 0);
|
|
CHECK(::IsClipboardFormatAvailable(CF_UNICODETEXT) && ::IsClipboardFormatAvailable(CF_TEXT));
|
|
if (::OpenClipboard(dlg)) {
|
|
HANDLE ht = ::GetClipboardData(CF_TEXT);
|
|
const char* a = ht ? (const char*)::GlobalLock(ht) : NULL;
|
|
CHECK(a && strcmp(a, "copy") == 0);
|
|
if (a) ::GlobalUnlock(ht);
|
|
::CloseClipboard();
|
|
}
|
|
::SendMessageW(edit, EM_SETSEL, 7, 7);
|
|
::SendMessageW(edit, WM_PASTE, 0, 0);
|
|
CHECK(DlgText(dlg, 10) == L"copy mecopy");
|
|
|
|
// Buttons: check boxes and a radio group.
|
|
CHECK(::IsDlgButtonChecked(dlg, 11) == BST_UNCHECKED);
|
|
::SendDlgItemMessageW(dlg, 11, BM_CLICK, 0, 0);
|
|
CHECK(::IsDlgButtonChecked(dlg, 11) == BST_CHECKED);
|
|
CHECK(SawCommand(11, BN_CLICKED));
|
|
CHECK(::CheckDlgButton(dlg, 11, BST_UNCHECKED) && !::IsDlgButtonChecked(dlg, 11));
|
|
CHECK(::CheckRadioButton(dlg, 12, 13, 12));
|
|
CHECK(::IsDlgButtonChecked(dlg, 12) && !::IsDlgButtonChecked(dlg, 13));
|
|
::SendDlgItemMessageW(dlg, 13, BM_CLICK, 0, 0);
|
|
CHECK(!::IsDlgButtonChecked(dlg, 12) && ::IsDlgButtonChecked(dlg, 13));
|
|
CHECK(::GetNextDlgGroupItem(dlg, ::GetDlgItem(dlg, 12), FALSE) == ::GetDlgItem(dlg, 13));
|
|
|
|
// The list box, sorted.
|
|
HWND list = ::GetDlgItem(dlg, 14);
|
|
for (const wchar_t* s : {L"pear", L"apple", L"fig"}) ::SendMessageW(list, LB_ADDSTRING, 0, (LPARAM)s);
|
|
CHECK(::SendMessageW(list, LB_GETCOUNT, 0, 0) == 3);
|
|
wchar_t buf[64];
|
|
::SendMessageW(list, LB_GETTEXT, 0, (LPARAM)buf);
|
|
CHECK(wcscmp(buf, L"apple") == 0);
|
|
CHECK(::SendMessageW(list, LB_FINDSTRING, (WPARAM)-1, (LPARAM)L"pe") == 2);
|
|
CHECK(::SendMessageW(list, LB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)L"pe") == LB_ERR);
|
|
::SendMessageW(list, LB_SETCURSEL, 1, 0);
|
|
CHECK(::SendMessageW(list, LB_GETCURSEL, 0, 0) == 1);
|
|
::SendMessageW(list, LB_SETITEMDATA, 1, 1234);
|
|
CHECK(::SendMessageW(list, LB_GETITEMDATA, 1, 0) == 1234);
|
|
CHECK(::SendMessageW(list, LB_DELETESTRING, 0, 0) == 2);
|
|
CHECK(::SendMessageW(list, LB_GETCURSEL, 0, 0) == 0);
|
|
|
|
// The drop-down list combo box.
|
|
HWND combo = ::GetDlgItem(dlg, 15);
|
|
for (const wchar_t* s : {L"red", L"green", L"blue"}) ::SendMessageW(combo, CB_ADDSTRING, 0, (LPARAM)s);
|
|
CHECK(::SendMessageW(combo, CB_GETCOUNT, 0, 0) == 3);
|
|
::SendMessageW(combo, CB_SETCURSEL, 1, 0);
|
|
CHECK(::SendMessageW(combo, CB_GETCURSEL, 0, 0) == 1);
|
|
CHECK(DlgText(dlg, 15) == L"green");
|
|
CHECK(::SendMessageW(combo, CB_SELECTSTRING, (WPARAM)-1, (LPARAM)L"bl") == 2);
|
|
CHECK(DlgText(dlg, 15) == L"blue");
|
|
COMBOBOXINFO ci = {sizeof ci};
|
|
CHECK(::GetComboBoxInfo(combo, &ci) && ci.hwndList && ci.hwndCombo == combo);
|
|
CHECK(!::SendMessageW(combo, CB_GETDROPPEDSTATE, 0, 0));
|
|
|
|
// Keyboard navigation through IsDialogMessage.
|
|
::ShowWindow(dlg, SW_SHOW);
|
|
::SetFocus(edit);
|
|
MSG m = {};
|
|
m.hwnd = edit;
|
|
m.message = WM_KEYDOWN;
|
|
m.wParam = VK_TAB;
|
|
CHECK(::IsDialogMessageW(dlg, &m));
|
|
CHECK(::GetFocus() == ::GetDlgItem(dlg, 11));
|
|
CHECK(::GetNextDlgTabItem(dlg, edit, FALSE) == ::GetDlgItem(dlg, 11));
|
|
CHECK(::GetNextDlgTabItem(dlg, edit, TRUE) == ::GetDlgItem(dlg, IDOK));
|
|
m.hwnd = ::GetFocus();
|
|
m.wParam = VK_RETURN;
|
|
g_dlgCommands.clear();
|
|
CHECK(::IsDialogMessageW(dlg, &m));
|
|
Pump();
|
|
CHECK(SawCommand(IDOK, BN_CLICKED));
|
|
::DestroyWindow(dlg);
|
|
CHECK(!::IsWindow(dlg) && !::IsWindow(edit));
|
|
|
|
// A modal dialog, ended from its own procedure.
|
|
CHECK(::DialogBoxIndirectParamW(::GetModuleHandleW(NULL), b.Get(), NULL, TestDlgProc, 2) == 7);
|
|
CHECK(g_initParam == 2);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// MDI: a frame, its client and children, and the window menu.
|
|
|
|
static HWND g_mdiClient = NULL;
|
|
|
|
static LRESULT CALLBACK MdiFrameProc(HWND h, UINT msg, WPARAM wp, LPARAM lp) {
|
|
return ::DefFrameProcW(h, g_mdiClient, msg, wp, lp);
|
|
}
|
|
|
|
static int g_mdiActivations = 0;
|
|
static LRESULT CALLBACK MdiChildProc(HWND h, UINT msg, WPARAM wp, LPARAM lp) {
|
|
if (msg == WM_MDIACTIVATE && (HWND)lp == h) g_mdiActivations++;
|
|
return ::DefMDIChildProcW(h, msg, wp, lp);
|
|
}
|
|
|
|
static void TestMdi() {
|
|
HINSTANCE inst = ::GetModuleHandleW(NULL);
|
|
WNDCLASSW wc = {};
|
|
wc.lpfnWndProc = MdiFrameProc;
|
|
wc.hInstance = inst;
|
|
wc.lpszClassName = L"MdiFrame";
|
|
::RegisterClassW(&wc);
|
|
wc.lpfnWndProc = MdiChildProc;
|
|
wc.lpszClassName = L"MdiChild";
|
|
::RegisterClassW(&wc);
|
|
|
|
HMENU bar = ::CreateMenu(), win = ::CreatePopupMenu();
|
|
::AppendMenuW(win, MF_STRING, 300, L"&Cascade");
|
|
::AppendMenuW(bar, MF_POPUP, (UINT_PTR)win, L"&Window");
|
|
HWND frame = ::CreateWindowExW(0, L"MdiFrame", L"Frame", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 600, 400,
|
|
NULL, bar, inst, NULL);
|
|
CLIENTCREATESTRUCT ccs = {win, 0xFF00};
|
|
g_mdiClient = ::CreateWindowExW(0, L"MDICLIENT", NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN, 0, 0, 0, 0, frame,
|
|
NULL, inst, &ccs);
|
|
CHECK(g_mdiClient != NULL);
|
|
RECT fc, cc;
|
|
::GetClientRect(frame, &fc);
|
|
::SendMessageW(frame, WM_SIZE, SIZE_RESTORED, MAKELPARAM(fc.right, fc.bottom));
|
|
::GetClientRect(g_mdiClient, &cc);
|
|
CHECK(cc.right == fc.right && cc.bottom == fc.bottom);
|
|
|
|
MDICREATESTRUCTW mcs = {L"MdiChild", L"One", inst, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
|
|
WS_VISIBLE, 0};
|
|
HWND one = (HWND)::SendMessageW(g_mdiClient, WM_MDICREATE, 0, (LPARAM)&mcs);
|
|
mcs.szTitle = L"Two";
|
|
HWND two = (HWND)::SendMessageW(g_mdiClient, WM_MDICREATE, 0, (LPARAM)&mcs);
|
|
CHECK(one && two && ::GetParent(one) == g_mdiClient);
|
|
CHECK(::GetWindowLongW(one, GWL_EXSTYLE) & WS_EX_MDICHILD);
|
|
BOOL maxed = TRUE;
|
|
CHECK((HWND)::SendMessageW(g_mdiClient, WM_MDIGETACTIVE, 0, (LPARAM)&maxed) == two && !maxed);
|
|
CHECK(g_mdiActivations == 2);
|
|
// The window menu lists the children, the active one checked.
|
|
CHECK(::GetMenuItemCount(win) == 4);
|
|
CHECK(::GetMenuState(win, 0xFF01, MF_BYCOMMAND) & MF_CHECKED);
|
|
wchar_t text[64];
|
|
::GetMenuStringW(win, 0xFF00, text, 64, MF_BYCOMMAND);
|
|
CHECK(wcscmp(text, L"&1 One") == 0);
|
|
// Choosing one from the menu activates it.
|
|
::SendMessageW(frame, WM_COMMAND, 0xFF00, 0);
|
|
CHECK((HWND)::SendMessageW(g_mdiClient, WM_MDIGETACTIVE, 0, 0) == one);
|
|
::SendMessageW(g_mdiClient, WM_MDINEXT, 0, 0);
|
|
CHECK((HWND)::SendMessageW(g_mdiClient, WM_MDIGETACTIVE, 0, 0) == two);
|
|
|
|
// Maximized: the caption goes, the buttons and title go to the frame.
|
|
int items = ::GetMenuItemCount(bar);
|
|
::SendMessageW(g_mdiClient, WM_MDIMAXIMIZE, (WPARAM)two, 0);
|
|
CHECK(::IsZoomed(two));
|
|
CHECK((HWND)::SendMessageW(g_mdiClient, WM_MDIGETACTIVE, 0, (LPARAM)&maxed) == two && maxed);
|
|
CHECK(::GetMenuItemCount(bar) == items + 4);
|
|
::GetWindowTextW(frame, text, 64);
|
|
CHECK(wcscmp(text, L"Frame - [Two]") == 0);
|
|
RECT kc;
|
|
::GetClientRect(two, &kc);
|
|
CHECK(kc.right == cc.right && kc.bottom == cc.bottom);
|
|
// Activating another keeps the maximized state.
|
|
::SendMessageW(g_mdiClient, WM_MDIACTIVATE, (WPARAM)one, 0);
|
|
CHECK(::IsZoomed(one) && !::IsZoomed(two));
|
|
::GetWindowTextW(frame, text, 64);
|
|
CHECK(wcscmp(text, L"Frame - [One]") == 0);
|
|
// The restore button, through the frame.
|
|
::SendMessageW(frame, WM_COMMAND, SC_RESTORE, 0);
|
|
CHECK(!::IsZoomed(one) && ::GetMenuItemCount(bar) == items);
|
|
::GetWindowTextW(frame, text, 64);
|
|
CHECK(wcscmp(text, L"Frame") == 0);
|
|
|
|
// Tiling side by side.
|
|
::SendMessageW(g_mdiClient, WM_MDITILE, MDITILE_VERTICAL, 0);
|
|
RECT r1, r2;
|
|
::GetWindowRect(one, &r1);
|
|
::GetWindowRect(two, &r2);
|
|
CHECK(r1.top == r2.top && (r1.right == r2.left || r2.right == r1.left));
|
|
|
|
// Closing the active one activates the other and renumbers it.
|
|
::SendMessageW(one, WM_CLOSE, 0, 0);
|
|
CHECK(!::IsWindow(one));
|
|
CHECK((HWND)::SendMessageW(g_mdiClient, WM_MDIGETACTIVE, 0, 0) == two);
|
|
CHECK(::GetDlgCtrlID(two) == 0xFF00 && ::GetMenuItemCount(win) == 3);
|
|
::SendMessageW(g_mdiClient, WM_MDIDESTROY, (WPARAM)two, 0);
|
|
CHECK(!::IsWindow(two) && ::SendMessageW(g_mdiClient, WM_MDIGETACTIVE, 0, 0) == 0);
|
|
CHECK(::GetMenuItemCount(win) == 1);
|
|
::DestroyWindow(frame);
|
|
g_mdiClient = NULL;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Image lists.
|
|
|
|
static void TestImageLists() {
|
|
HDC screen = ::GetDC(NULL);
|
|
HDC mem = ::CreateCompatibleDC(screen);
|
|
// Two 8x8 images: a red square and a blue one, each on magenta with a
|
|
// 2-pixel border.
|
|
HBITMAP strip = ::CreateCompatibleBitmap(screen, 16, 8);
|
|
HGDIOBJ old = ::SelectObject(mem, strip);
|
|
RECT r = {0, 0, 16, 8};
|
|
HBRUSH magenta = ::CreateSolidBrush(RGB(255, 0, 255)), red = ::CreateSolidBrush(RGB(255, 0, 0)),
|
|
blue = ::CreateSolidBrush(RGB(0, 0, 255));
|
|
::FillRect(mem, &r, magenta);
|
|
RECT a = {2, 2, 6, 6}, b = {10, 2, 14, 6};
|
|
::FillRect(mem, &a, red);
|
|
::FillRect(mem, &b, blue);
|
|
::SelectObject(mem, old);
|
|
|
|
HIMAGELIST il = ImageList_Create(8, 8, ILC_MASK | ILC_COLOR24, 2, 2);
|
|
CHECK(il && ImageList_GetImageCount(il) == 0);
|
|
CHECK(ImageList_AddMasked(il, strip, RGB(255, 0, 255)) == 0);
|
|
CHECK(ImageList_GetImageCount(il) == 2);
|
|
int cx = 0, cy = 0;
|
|
CHECK(ImageList_GetIconSize(il, &cx, &cy) && cx == 8 && cy == 8);
|
|
|
|
// Drawn transparently on white, and over a background color.
|
|
HBITMAP canvas = ::CreateCompatibleBitmap(screen, 32, 8);
|
|
old = ::SelectObject(mem, canvas);
|
|
RECT all = {0, 0, 32, 8};
|
|
::FillRect(mem, &all, (HBRUSH)::GetStockObject(WHITE_BRUSH));
|
|
CHECK(ImageList_Draw(il, 0, mem, 0, 0, ILD_TRANSPARENT));
|
|
CHECK(::GetPixel(mem, 3, 3) == RGB(255, 0, 0) && ::GetPixel(mem, 0, 0) == RGB(255, 255, 255));
|
|
CHECK(ImageList_DrawEx(il, 1, mem, 8, 0, 0, 0, RGB(0, 255, 0), CLR_NONE, ILD_NORMAL));
|
|
CHECK(::GetPixel(mem, 11, 3) == RGB(0, 0, 255) && ::GetPixel(mem, 8, 0) == RGB(0, 255, 0));
|
|
// The mask alone, and a blend toward the highlight color.
|
|
CHECK(ImageList_Draw(il, 0, mem, 16, 0, ILD_MASK));
|
|
CHECK(::GetPixel(mem, 19, 3) == RGB(0, 0, 0) && ::GetPixel(mem, 16, 0) == RGB(255, 255, 255));
|
|
// Windows may dither the blend rather than mix each pixel, so a 2x2
|
|
// block is averaged.
|
|
ImageList_DrawEx(il, 0, mem, 24, 0, 0, 0, CLR_NONE, RGB(0, 0, 255), ILD_BLEND50);
|
|
int rsum = 0, bsum = 0;
|
|
for (int y = 2; y < 4; y++)
|
|
for (int x = 26; x < 28; x++) {
|
|
rsum += GetRValue(::GetPixel(mem, x, y));
|
|
bsum += GetBValue(::GetPixel(mem, x, y));
|
|
}
|
|
CHECK(rsum / 4 > 100 && rsum / 4 < 155 && bsum / 4 > 100);
|
|
// An overlay drawn on top.
|
|
CHECK(ImageList_SetOverlayImage(il, 1, 1));
|
|
::FillRect(mem, &all, (HBRUSH)::GetStockObject(WHITE_BRUSH));
|
|
ImageList_Draw(il, 0, mem, 0, 0, ILD_TRANSPARENT | INDEXTOOVERLAYMASK(1));
|
|
CHECK(::GetPixel(mem, 3, 3) == RGB(0, 0, 255));
|
|
::SelectObject(mem, old);
|
|
|
|
// Icons in and out.
|
|
HICON icon = ImageList_GetIcon(il, 0, ILD_NORMAL);
|
|
CHECK(icon != NULL);
|
|
CHECK(ImageList_AddIcon(il, icon) == 2);
|
|
::SelectObject(mem, canvas);
|
|
::FillRect(mem, &all, (HBRUSH)::GetStockObject(WHITE_BRUSH));
|
|
ImageList_Draw(il, 2, mem, 0, 0, ILD_TRANSPARENT);
|
|
CHECK(::GetPixel(mem, 3, 3) == RGB(255, 0, 0) && ::GetPixel(mem, 0, 0) == RGB(255, 255, 255));
|
|
::SelectObject(mem, old);
|
|
::DestroyIcon(icon);
|
|
|
|
IMAGEINFO info;
|
|
CHECK(ImageList_GetImageInfo(il, 1, &info) && info.hbmImage && info.hbmMask && info.rcImage.left == 8);
|
|
HIMAGELIST copy = ImageList_Duplicate(il);
|
|
CHECK(ImageList_GetImageCount(copy) == 3);
|
|
HIMAGELIST merged = ImageList_Merge(il, 0, il, 1, 4, 0);
|
|
CHECK(merged && ImageList_GetIconSize(merged, &cx, &cy) && cx == 12 && cy == 8);
|
|
CHECK(ImageList_Remove(il, 0) && ImageList_GetImageCount(il) == 2);
|
|
CHECK(ImageList_RemoveAll(il) && ImageList_GetImageCount(il) == 0);
|
|
CHECK(ImageList_Destroy(il) && ImageList_Destroy(copy) && ImageList_Destroy(merged));
|
|
CHECK(!ImageList_Destroy(il));
|
|
|
|
::DeleteObject(strip);
|
|
::DeleteObject(canvas);
|
|
::DeleteObject(magenta);
|
|
::DeleteObject(red);
|
|
::DeleteObject(blue);
|
|
::DeleteDC(mem);
|
|
::ReleaseDC(NULL, screen);
|
|
}
|
|
|
|
// A CWnd with a message map
|
|
|
|
class CTestWnd : public CWnd {
|
|
public:
|
|
int m_nCreate = 0, m_nSize = 0, m_nTimer = 0, m_nApp = 0;
|
|
bool m_bPostNcDestroy = false;
|
|
CSize m_size;
|
|
|
|
protected:
|
|
afx_msg int OnCreate(LPCREATESTRUCT cs) {
|
|
m_nCreate++;
|
|
return CWnd::OnCreate(cs);
|
|
}
|
|
afx_msg void OnSize(UINT type, int cx, int cy) {
|
|
m_nSize++;
|
|
m_size = CSize(cx, cy);
|
|
CWnd::OnSize(type, cx, cy);
|
|
}
|
|
afx_msg void OnTimer(UINT_PTR id) {
|
|
if (id == 7) m_nTimer++;
|
|
}
|
|
afx_msg LRESULT OnApp(WPARAM wp, LPARAM) {
|
|
m_nApp++;
|
|
return wp * 2;
|
|
}
|
|
virtual void PostNcDestroy() { m_bPostNcDestroy = true; }
|
|
DECLARE_MESSAGE_MAP()
|
|
};
|
|
|
|
BEGIN_MESSAGE_MAP(CTestWnd, CWnd)
|
|
ON_WM_CREATE()
|
|
ON_WM_SIZE()
|
|
ON_WM_TIMER()
|
|
ON_MESSAGE(WM_APP + 5, OnApp)
|
|
END_MESSAGE_MAP()
|
|
|
|
// A parent that records what its common controls tell it.
|
|
static std::vector<UINT> g_notifies;
|
|
static int g_hscrolls = 0, g_vscrolls = 0;
|
|
static LRESULT CALLBACK CtlHostProc(HWND h, UINT msg, WPARAM wp, LPARAM lp) {
|
|
if (msg == WM_NOTIFY) g_notifies.push_back(((NMHDR*)lp)->code);
|
|
if (msg == WM_HSCROLL) g_hscrolls++;
|
|
if (msg == WM_VSCROLL) g_vscrolls++;
|
|
return ::DefWindowProcW(h, msg, wp, lp);
|
|
}
|
|
|
|
static bool Notified(UINT code) {
|
|
for (UINT c : g_notifies)
|
|
if (c == code) return true;
|
|
return false;
|
|
}
|
|
|
|
static void TestCommonControls() {
|
|
INITCOMMONCONTROLSEX icc = {sizeof icc, ICC_WIN95_CLASSES | ICC_BAR_CLASSES};
|
|
CHECK(::InitCommonControlsEx(&icc));
|
|
WNDCLASSW wc = {};
|
|
wc.lpfnWndProc = CtlHostProc;
|
|
wc.hInstance = ::GetModuleHandleW(NULL);
|
|
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
|
|
wc.lpszClassName = L"CtlHost";
|
|
::RegisterClassW(&wc);
|
|
HWND host = ::CreateWindowW(L"CtlHost", L"Controls", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, 0, 0, 420, 320,
|
|
NULL, NULL, wc.hInstance, NULL);
|
|
CHECK(host != NULL);
|
|
HINSTANCE inst = wc.hInstance;
|
|
DWORD child = WS_CHILD | WS_VISIBLE;
|
|
|
|
// Status bar with parts.
|
|
HWND status = ::CreateWindowW(STATUSCLASSNAMEW, L"Ready", child | SBARS_SIZEGRIP, 0, 0, 0, 0, host,
|
|
(HMENU)1, inst, NULL);
|
|
CHECK(status != NULL);
|
|
int parts[] = {200, 300, -1};
|
|
CHECK(::SendMessageW(status, SB_SETPARTS, 3, (LPARAM)parts));
|
|
CHECK(::SendMessageW(status, SB_GETPARTS, 0, 0) == 3);
|
|
::SendMessageW(status, SB_SETTEXTW, 1, (LPARAM)L"NUM");
|
|
wchar_t buf[64] = L"";
|
|
CHECK(LOWORD(::SendMessageW(status, SB_GETTEXTW, 1, (LPARAM)buf)) == 3 && wcscmp(buf, L"NUM") == 0);
|
|
|
|
// Progress.
|
|
HWND prog = ::CreateWindowW(PROGRESS_CLASSW, L"", child, 10, 10, 180, 16, host, (HMENU)2, inst, NULL);
|
|
CHECK(prog != NULL);
|
|
::SendMessageW(prog, PBM_SETRANGE32, 0, 200);
|
|
CHECK(::SendMessageW(prog, PBM_GETRANGE, FALSE, 0) == 200);
|
|
::SendMessageW(prog, PBM_SETPOS, 50, 0);
|
|
CHECK(::SendMessageW(prog, PBM_DELTAPOS, 25, 0) == 50);
|
|
CHECK(::SendMessageW(prog, PBM_GETPOS, 0, 0) == 75);
|
|
::SendMessageW(prog, PBM_SETPOS, 500, 0);
|
|
CHECK(::SendMessageW(prog, PBM_GETPOS, 0, 0) == 200);
|
|
::SendMessageW(prog, PBM_SETPOS, 100, 0);
|
|
|
|
// Trackbar: range, position clamping, keys scroll the parent.
|
|
HWND track = ::CreateWindowW(TRACKBAR_CLASSW, L"", child | WS_TABSTOP | TBS_AUTOTICKS, 10, 34, 180, 30, host,
|
|
(HMENU)3, inst, NULL);
|
|
CHECK(track != NULL);
|
|
::SendMessageW(track, TBM_SETRANGE, TRUE, MAKELONG(0, 10));
|
|
CHECK(::SendMessageW(track, TBM_GETRANGEMIN, 0, 0) == 0 && ::SendMessageW(track, TBM_GETRANGEMAX, 0, 0) == 10);
|
|
::SendMessageW(track, TBM_SETPOS, TRUE, 4);
|
|
CHECK(::SendMessageW(track, TBM_GETPOS, 0, 0) == 4);
|
|
::SendMessageW(track, TBM_SETPOS, TRUE, 99);
|
|
CHECK(::SendMessageW(track, TBM_GETPOS, 0, 0) == 10);
|
|
::SendMessageW(track, TBM_SETPOS, TRUE, 4);
|
|
g_hscrolls = 0;
|
|
::SendMessageW(track, WM_KEYDOWN, VK_RIGHT, 0);
|
|
CHECK(::SendMessageW(track, TBM_GETPOS, 0, 0) == 5 && g_hscrolls > 0);
|
|
RECT thumb = {}, chan = {};
|
|
::SendMessageW(track, TBM_GETTHUMBRECT, 0, (LPARAM)&thumb);
|
|
::SendMessageW(track, TBM_GETCHANNELRECT, 0, (LPARAM)&chan);
|
|
CHECK(thumb.right > thumb.left && chan.right > chan.left);
|
|
|
|
// Up-down with an edit buddy.
|
|
HWND edit = ::CreateWindowExW(WS_EX_CLIENTEDGE, L"Edit", L"", child | ES_NUMBER, 200, 10, 60, 20, host,
|
|
(HMENU)4, inst, NULL);
|
|
HWND spin = ::CreateWindowW(UPDOWN_CLASSW, L"", child | UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_ARROWKEYS, 0, 0,
|
|
0, 0, host, (HMENU)5, inst, NULL);
|
|
CHECK(spin != NULL);
|
|
::SendMessageW(spin, UDM_SETBUDDY, (WPARAM)edit, 0);
|
|
CHECK((HWND)::SendMessageW(spin, UDM_GETBUDDY, 0, 0) == edit);
|
|
::SendMessageW(spin, UDM_SETRANGE32, 0, 20);
|
|
::SendMessageW(spin, UDM_SETPOS32, 0, 7);
|
|
CHECK(::SendMessageW(spin, UDM_GETPOS32, 0, NULL) == 7);
|
|
::GetWindowTextW(edit, buf, 64);
|
|
CHECK(wcscmp(buf, L"7") == 0);
|
|
g_notifies.clear();
|
|
g_vscrolls = 0;
|
|
::SendMessageW(edit, WM_KEYDOWN, VK_UP, 0);
|
|
CHECK(::SendMessageW(spin, UDM_GETPOS32, 0, NULL) == 8 && Notified(UDN_DELTAPOS) && g_vscrolls > 0);
|
|
::GetWindowTextW(edit, buf, 64);
|
|
CHECK(wcscmp(buf, L"8") == 0);
|
|
|
|
// Header: items, text, layout and hit testing.
|
|
HWND header = ::CreateWindowW(WC_HEADERW, L"", child | HDS_BUTTONS, 10, 70, 300, 20, host, (HMENU)6, inst, NULL);
|
|
CHECK(header != NULL);
|
|
HDITEMW hi = {};
|
|
hi.mask = HDI_TEXT | HDI_WIDTH | HDI_FORMAT;
|
|
hi.fmt = HDF_LEFT | HDF_STRING;
|
|
hi.cxy = 100;
|
|
hi.pszText = (LPWSTR)L"Name";
|
|
CHECK(::SendMessageW(header, HDM_INSERTITEMW, 0, (LPARAM)&hi) == 0);
|
|
hi.pszText = (LPWSTR)L"Size";
|
|
hi.cxy = 60;
|
|
CHECK(::SendMessageW(header, HDM_INSERTITEMW, 1, (LPARAM)&hi) == 1);
|
|
CHECK(::SendMessageW(header, HDM_GETITEMCOUNT, 0, 0) == 2);
|
|
RECT ir = {};
|
|
CHECK(::SendMessageW(header, HDM_GETITEMRECT, 1, (LPARAM)&ir) && ir.left == 100 && ir.right == 160);
|
|
HDITEMW got = {};
|
|
got.mask = HDI_TEXT | HDI_WIDTH;
|
|
got.pszText = buf;
|
|
got.cchTextMax = 64;
|
|
CHECK(::SendMessageW(header, HDM_GETITEMW, 1, (LPARAM)&got) && wcscmp(buf, L"Size") == 0 && got.cxy == 60);
|
|
HDHITTESTINFO ht = {};
|
|
ht.pt.x = 120;
|
|
ht.pt.y = 5;
|
|
CHECK(::SendMessageW(header, HDM_HITTEST, 0, (LPARAM)&ht) == 1 && (ht.flags & HHT_ONHEADER));
|
|
int order[2] = {};
|
|
CHECK(::SendMessageW(header, HDM_GETORDERARRAY, 2, (LPARAM)order) && order[0] == 0 && order[1] == 1);
|
|
|
|
// Tabs: items, selection by mouse notifies the parent.
|
|
HWND tabs = ::CreateWindowW(WC_TABCONTROLW, L"", child | WS_CLIPSIBLINGS, 10, 100, 300, 120, host, (HMENU)7,
|
|
inst, NULL);
|
|
CHECK(tabs != NULL);
|
|
TCITEMW ti = {};
|
|
ti.mask = TCIF_TEXT | TCIF_PARAM;
|
|
ti.pszText = (LPWSTR)L"General";
|
|
ti.lParam = 11;
|
|
CHECK(::SendMessageW(tabs, TCM_INSERTITEMW, 0, (LPARAM)&ti) == 0);
|
|
ti.pszText = (LPWSTR)L"Advanced";
|
|
ti.lParam = 22;
|
|
CHECK(::SendMessageW(tabs, TCM_INSERTITEMW, 1, (LPARAM)&ti) == 1);
|
|
CHECK(::SendMessageW(tabs, TCM_GETITEMCOUNT, 0, 0) == 2 && ::SendMessageW(tabs, TCM_GETCURSEL, 0, 0) == 0);
|
|
TCITEMW tg = {};
|
|
tg.mask = TCIF_TEXT | TCIF_PARAM;
|
|
tg.pszText = buf;
|
|
tg.cchTextMax = 64;
|
|
CHECK(::SendMessageW(tabs, TCM_GETITEMW, 1, (LPARAM)&tg) && wcscmp(buf, L"Advanced") == 0 && tg.lParam == 22);
|
|
RECT tr = {};
|
|
CHECK(::SendMessageW(tabs, TCM_GETITEMRECT, 1, (LPARAM)&tr) && tr.right > tr.left);
|
|
g_notifies.clear();
|
|
::SendMessageW(tabs, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM((tr.left + tr.right) / 2, (tr.top + tr.bottom) / 2));
|
|
::SendMessageW(tabs, WM_LBUTTONUP, 0, MAKELPARAM((tr.left + tr.right) / 2, (tr.top + tr.bottom) / 2));
|
|
CHECK(::SendMessageW(tabs, TCM_GETCURSEL, 0, 0) == 1 && Notified(TCN_SELCHANGING) && Notified(TCN_SELCHANGE));
|
|
RECT page = {0, 0, 300, 120};
|
|
::SendMessageW(tabs, TCM_ADJUSTRECT, FALSE, (LPARAM)&page);
|
|
CHECK(page.top > 0 && page.bottom < 120 && page.left > 0);
|
|
|
|
// Hotkey.
|
|
HWND hot = ::CreateWindowW(HOTKEY_CLASSW, L"", child | WS_BORDER, 200, 34, 120, 20, host, (HMENU)8, inst, NULL);
|
|
CHECK(hot != NULL);
|
|
::SendMessageW(hot, HKM_SETHOTKEY, MAKEWORD('K', HOTKEYF_CONTROL), 0);
|
|
CHECK(::SendMessageW(hot, HKM_GETHOTKEY, 0, 0) == MAKEWORD('K', HOTKEYF_CONTROL));
|
|
|
|
::ShowWindow(host, SW_SHOW);
|
|
::UpdateWindow(host);
|
|
Pump();
|
|
::DestroyWindow(host);
|
|
}
|
|
|
|
static std::vector<UINT> g_commands; // WM_COMMAND notification codes
|
|
|
|
static LRESULT CALLBACK MoreHostProc(HWND h, UINT msg, WPARAM wp, LPARAM lp) {
|
|
if (msg == WM_NOTIFY) g_notifies.push_back(((NMHDR*)lp)->code);
|
|
if (msg == WM_COMMAND && lp) g_commands.push_back(HIWORD(wp));
|
|
return ::DefWindowProcW(h, msg, wp, lp);
|
|
}
|
|
|
|
static bool Commanded(UINT code) {
|
|
for (UINT c : g_commands)
|
|
if (c == code) return true;
|
|
return false;
|
|
}
|
|
|
|
// Pumps messages for up to half a second until the code comes.
|
|
static bool WaitCommand(UINT code) {
|
|
for (int i = 0; i < 50 && !Commanded(code); i++) {
|
|
Pump();
|
|
if (!Commanded(code)) ::Sleep(10);
|
|
}
|
|
return Commanded(code);
|
|
}
|
|
|
|
static bool SameDate(const SYSTEMTIME& a, const SYSTEMTIME& b) {
|
|
return a.wYear == b.wYear && a.wMonth == b.wMonth && a.wDay == b.wDay;
|
|
}
|
|
|
|
// A two-frame, 16x16, 8-bit uncompressed AVI file.
|
|
static std::vector<BYTE> MakeAvi() {
|
|
std::vector<BYTE> v;
|
|
auto put32 = [&](DWORD d) {
|
|
for (int i = 0; i < 4; i++) v.push_back((BYTE)(d >> (8 * i)));
|
|
};
|
|
auto put16 = [&](WORD w) {
|
|
v.push_back((BYTE)w);
|
|
v.push_back((BYTE)(w >> 8));
|
|
};
|
|
auto fcc = [&](const char* s) { v.insert(v.end(), s, s + 4); };
|
|
auto patch = [&](size_t at) { // the size of the chunk whose size field is at `at`
|
|
DWORD n = (DWORD)(v.size() - at - 4);
|
|
for (int i = 0; i < 4; i++) v[at + i] = (BYTE)(n >> (8 * i));
|
|
};
|
|
const int w = 16, h = 16, frames = 2;
|
|
fcc("RIFF");
|
|
size_t riff = v.size();
|
|
put32(0);
|
|
fcc("AVI ");
|
|
fcc("LIST");
|
|
size_t hdrl = v.size();
|
|
put32(0);
|
|
fcc("hdrl");
|
|
fcc("avih");
|
|
put32(56);
|
|
put32(100000); // microseconds per frame
|
|
put32(0); // max bytes per second
|
|
put32(0); // padding
|
|
put32(0x10); // AVIF_HASINDEX
|
|
put32(frames); // total frames
|
|
put32(0); // initial frames
|
|
put32(1); // streams
|
|
put32(w * h); // suggested buffer size
|
|
put32(w);
|
|
put32(h);
|
|
for (int i = 0; i < 4; i++) put32(0);
|
|
fcc("LIST");
|
|
size_t strl = v.size();
|
|
put32(0);
|
|
fcc("strl");
|
|
fcc("strh");
|
|
put32(56);
|
|
fcc("vids");
|
|
fcc("DIB ");
|
|
put32(0); // flags
|
|
put16(0); // priority
|
|
put16(0); // language
|
|
put32(0); // initial frames
|
|
put32(1); // scale
|
|
put32(10); // rate
|
|
put32(0); // start
|
|
put32(frames); // length
|
|
put32(w * h); // suggested buffer size
|
|
put32((DWORD)-1); // quality
|
|
put32(0); // sample size
|
|
put16(0);
|
|
put16(0);
|
|
put16(w);
|
|
put16(h);
|
|
fcc("strf");
|
|
put32(40 + 2 * 4);
|
|
put32(40);
|
|
put32(w);
|
|
put32(h);
|
|
put16(1);
|
|
put16(8);
|
|
put32(BI_RGB);
|
|
put32(w * h);
|
|
put32(0);
|
|
put32(0);
|
|
put32(2); // colors used
|
|
put32(0);
|
|
put32(0x00ff0000); // blue... (RGBQUAD order: blue, green, red)
|
|
put32(0x000000ff);
|
|
patch(strl);
|
|
patch(hdrl);
|
|
fcc("LIST");
|
|
size_t movi = v.size();
|
|
put32(0);
|
|
fcc("movi");
|
|
DWORD offsets[frames];
|
|
for (int f = 0; f < frames; f++) {
|
|
offsets[f] = (DWORD)(v.size() - (movi + 4));
|
|
fcc("00db");
|
|
put32(w * h);
|
|
for (int i = 0; i < w * h; i++) v.push_back((BYTE)((i / w < h / 2) == (f == 0)));
|
|
}
|
|
patch(movi);
|
|
fcc("idx1");
|
|
put32(16 * frames);
|
|
for (int f = 0; f < frames; f++) {
|
|
fcc("00db");
|
|
put32(0x10); // AVIIF_KEYFRAME
|
|
put32(offsets[f]);
|
|
put32(w * h);
|
|
}
|
|
patch(riff);
|
|
return v;
|
|
}
|
|
|
|
// The rest of the common controls: IP address, ComboBoxEx, month
|
|
// calendar, date and time picker, animation.
|
|
static void TestMoreControls() {
|
|
INITCOMMONCONTROLSEX icc = {sizeof icc, ICC_INTERNET_CLASSES | ICC_USEREX_CLASSES | ICC_DATE_CLASSES |
|
|
ICC_ANIMATE_CLASS};
|
|
CHECK(::InitCommonControlsEx(&icc));
|
|
WNDCLASSW wc = {};
|
|
wc.lpfnWndProc = MoreHostProc;
|
|
wc.hInstance = ::GetModuleHandleW(NULL);
|
|
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
|
|
wc.lpszClassName = L"MoreHost";
|
|
::RegisterClassW(&wc);
|
|
HWND host = ::CreateWindowW(L"MoreHost", L"More controls", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, 0, 0, 520,
|
|
400, NULL, NULL, wc.hInstance, NULL);
|
|
CHECK(host != NULL);
|
|
HINSTANCE inst = wc.hInstance;
|
|
DWORD child = WS_CHILD | WS_VISIBLE;
|
|
wchar_t buf[64] = L"";
|
|
|
|
// IP address: the address in and out, as text too, and a blank field.
|
|
HWND ip = ::CreateWindowW(WC_IPADDRESSW, L"", child | WS_TABSTOP, 10, 10, 150, 22, host, (HMENU)1, inst, NULL);
|
|
CHECK(ip != NULL);
|
|
CHECK(::SendMessageW(ip, IPM_ISBLANK, 0, 0));
|
|
::SendMessageW(ip, IPM_SETADDRESS, 0, MAKEIPADDRESS(192, 168, 1, 20));
|
|
DWORD addr = 0;
|
|
CHECK(::SendMessageW(ip, IPM_GETADDRESS, 0, (LPARAM)&addr) == 4 && addr == MAKEIPADDRESS(192, 168, 1, 20));
|
|
CHECK(!::SendMessageW(ip, IPM_ISBLANK, 0, 0));
|
|
::GetWindowTextW(ip, buf, 64);
|
|
CHECK(wcscmp(buf, L"192.168.1.20") == 0);
|
|
::SendMessageW(ip, IPM_CLEARADDRESS, 0, 0);
|
|
CHECK(::SendMessageW(ip, IPM_ISBLANK, 0, 0));
|
|
CHECK(::SendMessageW(ip, IPM_GETADDRESS, 0, (LPARAM)&addr) == 0);
|
|
|
|
// ComboBoxEx: items with text and data through the inner combo box.
|
|
HWND cbx = ::CreateWindowW(WC_COMBOBOXEXW, L"", child | CBS_DROPDOWNLIST, 180, 10, 160, 200, host, (HMENU)2,
|
|
inst, NULL);
|
|
CHECK(cbx != NULL);
|
|
HWND inner = (HWND)::SendMessageW(cbx, CBEM_GETCOMBOCONTROL, 0, 0);
|
|
CHECK(inner != NULL && ::GetParent(inner) == cbx);
|
|
COMBOBOXEXITEMW cbi = {};
|
|
cbi.mask = CBEIF_TEXT | CBEIF_LPARAM | CBEIF_INDENT;
|
|
cbi.iItem = -1;
|
|
cbi.pszText = (LPWSTR)L"Alpha";
|
|
cbi.lParam = 101;
|
|
CHECK(::SendMessageW(cbx, CBEM_INSERTITEMW, 0, (LPARAM)&cbi) == 0);
|
|
cbi.pszText = (LPWSTR)L"Beta";
|
|
cbi.lParam = 102;
|
|
cbi.iIndent = 1;
|
|
CHECK(::SendMessageW(cbx, CBEM_INSERTITEMW, 0, (LPARAM)&cbi) == 1);
|
|
cbi.iItem = 0;
|
|
cbi.pszText = (LPWSTR)L"First";
|
|
cbi.lParam = 100;
|
|
cbi.iIndent = 0;
|
|
CHECK(::SendMessageW(cbx, CBEM_INSERTITEMW, 0, (LPARAM)&cbi) == 0);
|
|
CHECK(::SendMessageW(cbx, CB_GETCOUNT, 0, 0) == 3);
|
|
COMBOBOXEXITEMW gi = {};
|
|
gi.mask = CBEIF_TEXT | CBEIF_LPARAM | CBEIF_INDENT;
|
|
gi.iItem = 2;
|
|
gi.pszText = buf;
|
|
gi.cchTextMax = 64;
|
|
CHECK(::SendMessageW(cbx, CBEM_GETITEMW, 0, (LPARAM)&gi) && wcscmp(buf, L"Beta") == 0 && gi.lParam == 102 &&
|
|
gi.iIndent == 1);
|
|
::SendMessageW(cbx, CB_SETCURSEL, 1, 0);
|
|
CHECK(::SendMessageW(cbx, CB_GETCURSEL, 0, 0) == 1 && ::SendMessageW(cbx, CB_GETITEMDATA, 1, 0) == 101);
|
|
CHECK(::SendMessageW(cbx, CBEM_DELETEITEM, 0, 0) == 2 && ::SendMessageW(cbx, CB_GETCOUNT, 0, 0) == 2);
|
|
|
|
// Month calendar: the selection, the range and the first day.
|
|
HWND cal = ::CreateWindowW(MONTHCAL_CLASSW, L"", child, 10, 50, 220, 160, host, (HMENU)3, inst, NULL);
|
|
CHECK(cal != NULL);
|
|
SYSTEMTIME day = {};
|
|
day.wYear = 2026;
|
|
day.wMonth = 11;
|
|
day.wDay = 17;
|
|
day.wDayOfWeek = 2;
|
|
CHECK(::SendMessageW(cal, MCM_SETCURSEL, 0, (LPARAM)&day));
|
|
SYSTEMTIME got = {};
|
|
CHECK(::SendMessageW(cal, MCM_GETCURSEL, 0, (LPARAM)&got) && SameDate(got, day));
|
|
RECT need = {};
|
|
CHECK(::SendMessageW(cal, MCM_GETMINREQRECT, 0, (LPARAM)&need) && need.right > 100 && need.bottom > 80);
|
|
SYSTEMTIME range[2] = {};
|
|
range[0].wYear = 2026;
|
|
range[0].wMonth = 1;
|
|
range[0].wDay = 1;
|
|
range[1].wYear = 2026;
|
|
range[1].wMonth = 12;
|
|
range[1].wDay = 31;
|
|
CHECK(::SendMessageW(cal, MCM_SETRANGE, GDTR_MIN | GDTR_MAX, (LPARAM)range));
|
|
SYSTEMTIME gotRange[2] = {};
|
|
CHECK(::SendMessageW(cal, MCM_GETRANGE, 0, (LPARAM)gotRange) == (GDTR_MIN | GDTR_MAX) &&
|
|
SameDate(gotRange[0], range[0]) && SameDate(gotRange[1], range[1]));
|
|
SYSTEMTIME outside = day;
|
|
outside.wYear = 2027;
|
|
CHECK(!::SendMessageW(cal, MCM_SETCURSEL, 0, (LPARAM)&outside));
|
|
CHECK(::SendMessageW(cal, MCM_GETCURSEL, 0, (LPARAM)&got) && SameDate(got, day));
|
|
::SendMessageW(cal, MCM_SETFIRSTDAYOFWEEK, 0, 6); // Sunday
|
|
CHECK(LOWORD(::SendMessageW(cal, MCM_GETFIRSTDAYOFWEEK, 0, 0)) == 6);
|
|
::SendMessageW(cal, MCM_SETCOLOR, MCSC_TITLEBK, RGB(0, 0, 128));
|
|
CHECK(::SendMessageW(cal, MCM_GETCOLOR, MCSC_TITLEBK, 0) == RGB(0, 0, 128));
|
|
|
|
// Date and time picker: the date in and out, none, and the format.
|
|
HWND dtp = ::CreateWindowW(DATETIMEPICK_CLASSW, L"", child | WS_TABSTOP | DTS_SHOWNONE, 250, 50, 160, 22, host,
|
|
(HMENU)4, inst, NULL);
|
|
CHECK(dtp != NULL);
|
|
CHECK(::SendMessageW(dtp, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&day));
|
|
memset(&got, 0, sizeof got);
|
|
CHECK(::SendMessageW(dtp, DTM_GETSYSTEMTIME, 0, (LPARAM)&got) == GDT_VALID && SameDate(got, day));
|
|
CHECK(::SendMessageW(dtp, DTM_SETFORMATW, 0, (LPARAM)L"yyyy'-'MM'-'dd"));
|
|
CHECK(::SendMessageW(dtp, DTM_SETSYSTEMTIME, GDT_NONE, (LPARAM)&day));
|
|
CHECK(::SendMessageW(dtp, DTM_GETSYSTEMTIME, 0, (LPARAM)&got) == GDT_NONE);
|
|
CHECK(::SendMessageW(dtp, DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM)&day));
|
|
::SendMessageW(dtp, DTM_SETMCCOLOR, MCSC_TITLEBK, RGB(0, 128, 0));
|
|
CHECK(::SendMessageW(dtp, DTM_GETMCCOLOR, MCSC_TITLEBK, 0) == RGB(0, 128, 0));
|
|
::ShowWindow(host, SW_SHOW);
|
|
|
|
// Animation: a clip from a file, its first frame on show, playing and
|
|
// stopping.
|
|
std::vector<BYTE> avi = MakeAvi();
|
|
wchar_t dir[MAX_PATH], path[MAX_PATH];
|
|
::GetTempPathW(MAX_PATH, dir);
|
|
::GetTempFileNameW(dir, L"avi", 0, path);
|
|
HANDLE file = ::CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
|
|
DWORD wrote = 0;
|
|
CHECK(file != INVALID_HANDLE_VALUE && ::WriteFile(file, avi.data(), (DWORD)avi.size(), &wrote, NULL));
|
|
::CloseHandle(file);
|
|
HWND anim = ::CreateWindowW(ANIMATE_CLASSW, L"", child, 10, 230, 100, 100, host, (HMENU)5, inst, NULL);
|
|
CHECK(anim != NULL);
|
|
CHECK(!::SendMessageW(anim, ACM_OPENW, 0, (LPARAM)L"no such clip.avi"));
|
|
CHECK(::SendMessageW(anim, ACM_OPENW, 0, (LPARAM)path));
|
|
RECT ar = {};
|
|
::GetClientRect(anim, &ar);
|
|
CHECK(ar.right == 16 && ar.bottom == 16);
|
|
CHECK(::SendMessageW(anim, ACM_PLAY, 1, MAKELPARAM(0, 0)));
|
|
Pump();
|
|
::UpdateWindow(host);
|
|
HDC screen = ::GetDC(anim);
|
|
CHECK(::GetPixel(screen, 2, 2) == RGB(255, 0, 0) && ::GetPixel(screen, 2, 13) == RGB(0, 0, 255));
|
|
::ReleaseDC(anim, screen);
|
|
// The notifications may come posted.
|
|
g_commands.clear();
|
|
CHECK(::SendMessageW(anim, ACM_PLAY, (WPARAM)-1, MAKELPARAM(0, -1)));
|
|
CHECK(WaitCommand(ACN_START));
|
|
CHECK(::SendMessageW(anim, ACM_STOP, 0, 0));
|
|
CHECK(WaitCommand(ACN_STOP));
|
|
CHECK(::SendMessageW(anim, ACM_OPENW, 0, 0));
|
|
::DestroyWindow(anim);
|
|
::DeleteFileW(path);
|
|
|
|
Pump();
|
|
::DestroyWindow(host);
|
|
}
|
|
|
|
static int CALLBACK CompareDown(LPARAM a, LPARAM b, LPARAM) { return (int)(b - a); }
|
|
|
|
static void TestListView() {
|
|
HINSTANCE inst = ::GetModuleHandleW(NULL);
|
|
HWND host = ::CreateWindowW(L"CtlHost", L"List", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, 0, 0, 420, 320, NULL,
|
|
NULL, inst, NULL);
|
|
HWND lv = ::CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, L"", WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_EDITLABELS,
|
|
10, 10, 380, 250, host, (HMENU)20, inst, NULL);
|
|
CHECK(lv != NULL);
|
|
HWND header = (HWND)::SendMessageW(lv, LVM_GETHEADER, 0, 0);
|
|
CHECK(header != NULL);
|
|
|
|
// Columns go to the header.
|
|
LVCOLUMNW col = {};
|
|
col.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT;
|
|
col.fmt = LVCFMT_LEFT;
|
|
col.cx = 150;
|
|
col.pszText = (LPWSTR)L"Name";
|
|
CHECK(::SendMessageW(lv, LVM_INSERTCOLUMNW, 0, (LPARAM)&col) == 0);
|
|
col.fmt = LVCFMT_RIGHT;
|
|
col.cx = 80;
|
|
col.pszText = (LPWSTR)L"Size";
|
|
CHECK(::SendMessageW(lv, LVM_INSERTCOLUMNW, 1, (LPARAM)&col) == 1);
|
|
CHECK(::SendMessageW(header, HDM_GETITEMCOUNT, 0, 0) == 2);
|
|
CHECK(::SendMessageW(lv, LVM_GETCOLUMNWIDTH, 1, 0) == 80);
|
|
wchar_t buf[64] = L"";
|
|
LVCOLUMNW gc = {};
|
|
gc.mask = LVCF_TEXT | LVCF_FMT;
|
|
gc.pszText = buf;
|
|
gc.cchTextMax = 64;
|
|
CHECK(::SendMessageW(lv, LVM_GETCOLUMNW, 1, (LPARAM)&gc) && wcscmp(buf, L"Size") == 0 && gc.fmt == LVCFMT_RIGHT);
|
|
|
|
// Items and subitems.
|
|
g_notifies.clear();
|
|
const wchar_t* names[] = {L"apple", L"banana", L"cherry", L"date"};
|
|
for (int i = 0; i < 4; i++) {
|
|
LVITEMW it = {};
|
|
it.mask = LVIF_TEXT | LVIF_PARAM;
|
|
it.iItem = i;
|
|
it.pszText = (LPWSTR)names[i];
|
|
it.lParam = i * 10;
|
|
CHECK(::SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&it) == i);
|
|
wchar_t size[16];
|
|
swprintf(size, 16, L"%d KB", (i + 1) * 3);
|
|
it.mask = LVIF_TEXT;
|
|
it.iSubItem = 1;
|
|
it.pszText = size;
|
|
CHECK(::SendMessageW(lv, LVM_SETITEMTEXTW, i, (LPARAM)&it));
|
|
}
|
|
CHECK(Notified(LVN_INSERTITEM));
|
|
CHECK(::SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0) == 4);
|
|
LVITEMW get = {};
|
|
get.iSubItem = 1;
|
|
get.pszText = buf;
|
|
get.cchTextMax = 64;
|
|
CHECK(::SendMessageW(lv, LVM_GETITEMTEXTW, 2, (LPARAM)&get) == 4 && wcscmp(buf, L"9 KB") == 0);
|
|
get.mask = LVIF_TEXT | LVIF_PARAM;
|
|
get.iItem = 1;
|
|
get.iSubItem = 0;
|
|
CHECK(::SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&get) && wcscmp(buf, L"banana") == 0 && get.lParam == 10);
|
|
LVFINDINFOW fi = {};
|
|
fi.flags = LVFI_STRING;
|
|
fi.psz = L"Cherry";
|
|
CHECK(::SendMessageW(lv, LVM_FINDITEMW, (WPARAM)-1, (LPARAM)&fi) == 2);
|
|
fi.flags = LVFI_PARAM;
|
|
fi.lParam = 30;
|
|
CHECK(::SendMessageW(lv, LVM_FINDITEMW, (WPARAM)-1, (LPARAM)&fi) == 3);
|
|
|
|
// Selection through state, with notifications.
|
|
g_notifies.clear();
|
|
LVITEMW st = {};
|
|
st.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
|
|
st.state = LVIS_SELECTED | LVIS_FOCUSED;
|
|
CHECK(::SendMessageW(lv, LVM_SETITEMSTATE, 1, (LPARAM)&st));
|
|
CHECK(Notified(LVN_ITEMCHANGING) && Notified(LVN_ITEMCHANGED));
|
|
CHECK(::SendMessageW(lv, LVM_GETSELECTEDCOUNT, 0, 0) == 1);
|
|
CHECK(::SendMessageW(lv, LVM_GETNEXTITEM, (WPARAM)-1, LVNI_SELECTED) == 1);
|
|
CHECK(::SendMessageW(lv, LVM_GETNEXTITEM, (WPARAM)-1, LVNI_FOCUSED) == 1);
|
|
CHECK(::SendMessageW(lv, LVM_GETNEXTITEM, 1, LVNI_BELOW) == 2);
|
|
|
|
// Geometry and hit testing.
|
|
RECT ir = {LVIR_BOUNDS};
|
|
CHECK(::SendMessageW(lv, LVM_GETITEMRECT, 2, (LPARAM)&ir) && ir.right - ir.left == 230 && ir.bottom > ir.top);
|
|
LVHITTESTINFO ht = {};
|
|
ht.pt.x = ir.left + 20;
|
|
ht.pt.y = (ir.top + ir.bottom) / 2;
|
|
CHECK(::SendMessageW(lv, LVM_HITTEST, 0, (LPARAM)&ht) == 2 && (ht.flags & LVHT_ONITEM));
|
|
ht.pt.x = ir.left + 190;
|
|
CHECK(::SendMessageW(lv, LVM_SUBITEMHITTEST, 0, (LPARAM)&ht) == 2 && ht.iSubItem == 1);
|
|
|
|
// The keyboard moves the selection; a click selects and notifies.
|
|
::SetFocus(lv);
|
|
::SendMessageW(lv, WM_KEYDOWN, VK_DOWN, 0);
|
|
CHECK(::SendMessageW(lv, LVM_GETNEXTITEM, (WPARAM)-1, LVNI_SELECTED) == 2);
|
|
CHECK(::SendMessageW(lv, LVM_GETSELECTEDCOUNT, 0, 0) == 1);
|
|
g_notifies.clear();
|
|
RECT r0 = {LVIR_LABEL};
|
|
::SendMessageW(lv, LVM_GETITEMRECT, 0, (LPARAM)&r0);
|
|
LPARAM at = MAKELPARAM(r0.left + 5, (r0.top + r0.bottom) / 2);
|
|
::SendMessageW(lv, WM_LBUTTONDOWN, MK_LBUTTON, at);
|
|
::SendMessageW(lv, WM_LBUTTONUP, 0, at);
|
|
CHECK(::SendMessageW(lv, LVM_GETNEXTITEM, (WPARAM)-1, LVNI_SELECTED) == 0 && Notified(NM_CLICK));
|
|
::SendMessageW(lv, WM_KEYDOWN, VK_END, 0);
|
|
CHECK(::SendMessageW(lv, LVM_GETNEXTITEM, (WPARAM)-1, LVNI_FOCUSED) == 3);
|
|
g_notifies.clear();
|
|
::SendMessageW(lv, WM_CHAR, 'b', 1);
|
|
CHECK(::SendMessageW(lv, LVM_GETNEXTITEM, (WPARAM)-1, LVNI_FOCUSED) == 1);
|
|
|
|
// Label editing ends with the parent's say.
|
|
HWND edit = (HWND)::SendMessageW(lv, LVM_EDITLABELW, 1, 0);
|
|
CHECK(edit != NULL && Notified(LVN_BEGINLABELEDITW));
|
|
if (edit) {
|
|
::SetWindowTextW(edit, L"blueberry");
|
|
::SendMessageW(edit, WM_KEYDOWN, VK_RETURN, 0);
|
|
CHECK(Notified(LVN_ENDLABELEDITW) && ::SendMessageW(lv, LVM_GETEDITCONTROL, 0, 0) == 0);
|
|
}
|
|
|
|
// Sorting by lParam, descending.
|
|
CHECK(::SendMessageW(lv, LVM_SORTITEMS, 0, (LPARAM)CompareDown));
|
|
get.mask = LVIF_PARAM;
|
|
get.iItem = 0;
|
|
CHECK(::SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&get) && get.lParam == 30);
|
|
CHECK(::SendMessageW(lv, LVM_GETNEXTITEM, (WPARAM)-1, LVNI_FOCUSED) == 2);
|
|
|
|
// Check boxes are state images 1 and 2.
|
|
::SendMessageW(lv, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT,
|
|
LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
|
|
CHECK(::SendMessageW(lv, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0) == (LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT));
|
|
CHECK((::SendMessageW(lv, LVM_GETITEMSTATE, 0, LVIS_STATEIMAGEMASK) >> 12) == 1);
|
|
st.stateMask = LVIS_STATEIMAGEMASK;
|
|
st.state = INDEXTOSTATEIMAGEMASK(2);
|
|
::SendMessageW(lv, LVM_SETITEMSTATE, 0, (LPARAM)&st);
|
|
CHECK((::SendMessageW(lv, LVM_GETITEMSTATE, 0, LVIS_STATEIMAGEMASK) >> 12) == 2);
|
|
|
|
// Deleting.
|
|
g_notifies.clear();
|
|
CHECK(::SendMessageW(lv, LVM_DELETEITEM, 3, 0) && Notified(LVN_DELETEITEM));
|
|
CHECK(::SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0) == 3);
|
|
|
|
// The other views lay the items out on their own.
|
|
::SetWindowLongW(lv, GWL_STYLE, (::GetWindowLongW(lv, GWL_STYLE) & ~LVS_TYPEMASK) | LVS_ICON);
|
|
RECT a = {LVIR_BOUNDS}, b = {LVIR_BOUNDS};
|
|
::SendMessageW(lv, LVM_GETITEMRECT, 0, (LPARAM)&a);
|
|
::SendMessageW(lv, LVM_GETITEMRECT, 1, (LPARAM)&b);
|
|
CHECK(b.left > a.left && b.top == a.top && !(::GetWindowLongW(header, GWL_STYLE) & WS_VISIBLE));
|
|
::SetWindowLongW(lv, GWL_STYLE, (::GetWindowLongW(lv, GWL_STYLE) & ~LVS_TYPEMASK) | LVS_LIST);
|
|
::SendMessageW(lv, LVM_GETITEMRECT, 0, (LPARAM)&a);
|
|
// A row of text each (Wine keeps the second item's icon place).
|
|
CHECK(a.left == 0 && a.top == 0 && a.bottom - a.top < 30);
|
|
::SetWindowLongW(lv, GWL_STYLE, (::GetWindowLongW(lv, GWL_STYLE) & ~LVS_TYPEMASK) | LVS_REPORT);
|
|
CHECK(::GetWindowLongW(header, GWL_STYLE) & WS_VISIBLE);
|
|
|
|
// Owner data: the list view keeps only the count and the states.
|
|
HWND od = ::CreateWindowW(WC_LISTVIEWW, L"", WS_CHILD | LVS_REPORT | LVS_OWNERDATA, 0, 0, 100, 100, host,
|
|
(HMENU)21, inst, NULL);
|
|
::SendMessageW(od, LVM_SETITEMCOUNT, 1000, 0);
|
|
CHECK(::SendMessageW(od, LVM_GETITEMCOUNT, 0, 0) == 1000);
|
|
st.stateMask = LVIS_SELECTED;
|
|
st.state = LVIS_SELECTED;
|
|
::SendMessageW(od, LVM_SETITEMSTATE, 500, (LPARAM)&st);
|
|
CHECK(::SendMessageW(od, LVM_GETNEXTITEM, (WPARAM)-1, LVNI_SELECTED) == 500);
|
|
|
|
g_notifies.clear();
|
|
CHECK(::SendMessageW(lv, LVM_DELETEALLITEMS, 0, 0) && Notified(LVN_DELETEALLITEMS));
|
|
CHECK(::SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0) == 0);
|
|
::DestroyWindow(host);
|
|
}
|
|
|
|
static HTREEITEM TreeAdd(HWND tv, HTREEITEM parent, const wchar_t* text, LPARAM data,
|
|
HTREEITEM after = TVI_LAST) {
|
|
TVINSERTSTRUCTW ins = {};
|
|
ins.hParent = parent;
|
|
ins.hInsertAfter = after;
|
|
ins.item.mask = TVIF_TEXT | TVIF_PARAM;
|
|
ins.item.pszText = (LPWSTR)text;
|
|
ins.item.lParam = data;
|
|
return (HTREEITEM)::SendMessageW(tv, TVM_INSERTITEMW, 0, (LPARAM)&ins);
|
|
}
|
|
|
|
static HTREEITEM TreeNext(HWND tv, HTREEITEM h, UINT code) {
|
|
return (HTREEITEM)::SendMessageW(tv, TVM_GETNEXTITEM, code, (LPARAM)h);
|
|
}
|
|
|
|
static std::wstring TreeText(HWND tv, HTREEITEM h) {
|
|
wchar_t buf[64] = L"";
|
|
TVITEMW it = {};
|
|
it.mask = TVIF_TEXT;
|
|
it.hItem = h;
|
|
it.pszText = buf;
|
|
it.cchTextMax = 64;
|
|
::SendMessageW(tv, TVM_GETITEMW, 0, (LPARAM)&it);
|
|
return buf;
|
|
}
|
|
|
|
static int CALLBACK CompareParamDown(LPARAM a, LPARAM b, LPARAM) { return (int)(b - a); }
|
|
|
|
static void TestTreeView() {
|
|
HINSTANCE inst = ::GetModuleHandleW(NULL);
|
|
HWND host = ::CreateWindowW(L"CtlHost", L"Tree", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, 0, 0, 300, 320, NULL,
|
|
NULL, inst, NULL);
|
|
HWND tv = ::CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW, L"",
|
|
WS_CHILD | WS_VISIBLE | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT |
|
|
TVS_EDITLABELS | TVS_SHOWSELALWAYS,
|
|
10, 10, 260, 260, host, (HMENU)30, inst, NULL);
|
|
CHECK(tv != NULL);
|
|
|
|
HTREEITEM fruit = TreeAdd(tv, TVI_ROOT, L"Fruit", 1);
|
|
HTREEITEM veg = TreeAdd(tv, TVI_ROOT, L"Vegetables", 2);
|
|
HTREEITEM pear = TreeAdd(tv, fruit, L"Pear", 13);
|
|
HTREEITEM apple = TreeAdd(tv, fruit, L"Apple", 11, TVI_FIRST);
|
|
HTREEITEM fig = TreeAdd(tv, fruit, L"Fig", 12, TVI_SORT);
|
|
HTREEITEM leek = TreeAdd(tv, veg, L"Leek", 21);
|
|
CHECK(fruit && veg && pear && apple && fig && leek);
|
|
CHECK(::SendMessageW(tv, TVM_GETCOUNT, 0, 0) == 6);
|
|
CHECK(TreeNext(tv, NULL, TVGN_ROOT) == fruit && TreeNext(tv, fruit, TVGN_NEXT) == veg);
|
|
CHECK(TreeNext(tv, fruit, TVGN_CHILD) == apple && TreeNext(tv, apple, TVGN_NEXT) == fig &&
|
|
TreeNext(tv, fig, TVGN_NEXT) == pear);
|
|
CHECK(TreeNext(tv, pear, TVGN_PARENT) == fruit && TreeNext(tv, fruit, TVGN_PARENT) == NULL);
|
|
CHECK(TreeText(tv, fig) == L"Fig");
|
|
|
|
// Closed parents hide their children.
|
|
CHECK(TreeNext(tv, fruit, TVGN_NEXTVISIBLE) == veg);
|
|
RECT r;
|
|
*(HTREEITEM*)&r = apple;
|
|
CHECK(!::SendMessageW(tv, TVM_GETITEMRECT, TRUE, (LPARAM)&r));
|
|
g_notifies.clear();
|
|
CHECK(::SendMessageW(tv, TVM_EXPAND, TVE_EXPAND, (LPARAM)fruit));
|
|
CHECK(Notified(TVN_ITEMEXPANDINGW) && Notified(TVN_ITEMEXPANDEDW));
|
|
CHECK(::SendMessageW(tv, TVM_GETITEMSTATE, (WPARAM)fruit, TVIS_EXPANDED) == TVIS_EXPANDED);
|
|
CHECK(TreeNext(tv, fruit, TVGN_NEXTVISIBLE) == apple);
|
|
*(HTREEITEM*)&r = apple;
|
|
CHECK(::SendMessageW(tv, TVM_GETITEMRECT, TRUE, (LPARAM)&r) && r.right > r.left);
|
|
// Only the first TVM_EXPAND tells the parent.
|
|
::SendMessageW(tv, TVM_EXPAND, TVE_COLLAPSE, (LPARAM)fruit);
|
|
g_notifies.clear();
|
|
::SendMessageW(tv, TVM_EXPAND, TVE_EXPAND, (LPARAM)fruit);
|
|
CHECK(!Notified(TVN_ITEMEXPANDINGW));
|
|
|
|
// Selection.
|
|
g_notifies.clear();
|
|
CHECK(::SendMessageW(tv, TVM_SELECTITEM, TVGN_CARET, (LPARAM)fig));
|
|
CHECK(Notified(TVN_SELCHANGINGW) && Notified(TVN_SELCHANGEDW));
|
|
CHECK(TreeNext(tv, NULL, TVGN_CARET) == fig);
|
|
CHECK(::SendMessageW(tv, TVM_GETITEMSTATE, (WPARAM)fig, TVIS_SELECTED) == TVIS_SELECTED);
|
|
// Selecting a hidden item opens its parent.
|
|
::SendMessageW(tv, TVM_SELECTITEM, TVGN_CARET, (LPARAM)leek);
|
|
CHECK(::SendMessageW(tv, TVM_GETITEMSTATE, (WPARAM)veg, TVIS_EXPANDED) == TVIS_EXPANDED);
|
|
|
|
// The keyboard.
|
|
::SetFocus(tv);
|
|
::SendMessageW(tv, WM_KEYDOWN, VK_UP, 0);
|
|
CHECK(TreeNext(tv, NULL, TVGN_CARET) == veg);
|
|
::SendMessageW(tv, WM_KEYDOWN, VK_LEFT, 0);
|
|
CHECK(::SendMessageW(tv, TVM_GETITEMSTATE, (WPARAM)veg, TVIS_EXPANDED) == 0);
|
|
::SendMessageW(tv, WM_KEYDOWN, VK_HOME, 0);
|
|
CHECK(TreeNext(tv, NULL, TVGN_CARET) == fruit);
|
|
::SendMessageW(tv, WM_KEYDOWN, VK_RIGHT, 0);
|
|
::SendMessageW(tv, WM_KEYDOWN, VK_RIGHT, 0);
|
|
CHECK(TreeNext(tv, NULL, TVGN_CARET) == apple);
|
|
::SendMessageW(tv, WM_CHAR, 'p', 1);
|
|
CHECK(TreeNext(tv, NULL, TVGN_CARET) == pear);
|
|
|
|
// Hit testing and a click on the button.
|
|
*(HTREEITEM*)&r = veg;
|
|
::SendMessageW(tv, TVM_GETITEMRECT, TRUE, (LPARAM)&r);
|
|
TVHITTESTINFO ht = {};
|
|
ht.pt.x = r.left + 3;
|
|
ht.pt.y = (r.top + r.bottom) / 2;
|
|
CHECK((HTREEITEM)::SendMessageW(tv, TVM_HITTEST, 0, (LPARAM)&ht) == veg && (ht.flags & TVHT_ONITEMLABEL));
|
|
ht.pt.x = r.left - 10;
|
|
CHECK((HTREEITEM)::SendMessageW(tv, TVM_HITTEST, 0, (LPARAM)&ht) == veg && (ht.flags & TVHT_ONITEMBUTTON));
|
|
::SendMessageW(tv, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(ht.pt.x, ht.pt.y));
|
|
::SendMessageW(tv, WM_LBUTTONUP, 0, MAKELPARAM(ht.pt.x, ht.pt.y));
|
|
CHECK(::SendMessageW(tv, TVM_GETITEMSTATE, (WPARAM)veg, TVIS_EXPANDED) == TVIS_EXPANDED);
|
|
g_notifies.clear();
|
|
// The release is waiting in the queue: Windows watches for a drag
|
|
// until it comes.
|
|
::PostMessageW(tv, WM_LBUTTONUP, 0, MAKELPARAM(r.left + 3, ht.pt.y));
|
|
::SendMessageW(tv, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(r.left + 3, ht.pt.y));
|
|
Pump();
|
|
CHECK(TreeNext(tv, NULL, TVGN_CARET) == veg && Notified(NM_CLICK));
|
|
|
|
// Editing a label.
|
|
g_notifies.clear();
|
|
HWND edit = (HWND)::SendMessageW(tv, TVM_EDITLABELW, 0, (LPARAM)leek);
|
|
CHECK(edit && Notified(TVN_BEGINLABELEDITW));
|
|
if (edit) {
|
|
::SetWindowTextW(edit, L"Onion");
|
|
CHECK(::SendMessageW(tv, TVM_ENDEDITLABELNOW, FALSE, 0));
|
|
CHECK(Notified(TVN_ENDLABELEDITW) && ::SendMessageW(tv, TVM_GETEDITCONTROL, 0, 0) == 0);
|
|
}
|
|
|
|
// Sorting.
|
|
TVSORTCB sort = {fruit, CompareParamDown, 0};
|
|
CHECK(::SendMessageW(tv, TVM_SORTCHILDRENCB, 0, (LPARAM)&sort));
|
|
CHECK(TreeNext(tv, fruit, TVGN_CHILD) == pear && TreeNext(tv, pear, TVGN_NEXT) == fig);
|
|
CHECK(::SendMessageW(tv, TVM_SORTCHILDREN, 0, (LPARAM)fruit));
|
|
CHECK(TreeNext(tv, fruit, TVGN_CHILD) == apple);
|
|
|
|
// Deleting a branch with the caret in it moves the caret.
|
|
::SendMessageW(tv, TVM_SELECTITEM, TVGN_CARET, (LPARAM)fig);
|
|
g_notifies.clear();
|
|
CHECK(::SendMessageW(tv, TVM_DELETEITEM, 0, (LPARAM)fruit));
|
|
CHECK(Notified(TVN_DELETEITEMW) && ::SendMessageW(tv, TVM_GETCOUNT, 0, 0) == 2);
|
|
CHECK(TreeNext(tv, NULL, TVGN_CARET) == veg);
|
|
CHECK(::SendMessageW(tv, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT) && ::SendMessageW(tv, TVM_GETCOUNT, 0, 0) == 0);
|
|
::DestroyWindow(host);
|
|
}
|
|
|
|
// A wizard page that records what the sheet tells it and can refuse to
|
|
// be left.
|
|
static std::vector<UINT> g_pageCodes;
|
|
static bool g_pageRefuses = false;
|
|
static INT_PTR CALLBACK WizPageProc(HWND h, UINT msg, WPARAM, LPARAM lp) {
|
|
if (msg == WM_NOTIFY) {
|
|
NMHDR* nm = (NMHDR*)lp;
|
|
g_pageCodes.push_back(nm->code);
|
|
LRESULT r = 0;
|
|
if (nm->code == PSN_SETACTIVE) ::PostMessageW(::GetParent(h), PSM_SETWIZBUTTONS, 0, PSWIZB_BACK | PSWIZB_NEXT);
|
|
if (nm->code == PSN_KILLACTIVE && g_pageRefuses) r = TRUE;
|
|
::SetWindowLongPtrW(h, DWLP_MSGRESULT, r);
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
static bool PageCode(UINT code) {
|
|
for (UINT c : g_pageCodes)
|
|
if (c == code) return true;
|
|
return false;
|
|
}
|
|
|
|
static void TestPropertySheets() {
|
|
DlgBuilder one(WS_CHILD | WS_CAPTION, 180, 80, "One");
|
|
one.Item(0x0081, ES_AUTOHSCROLL | WS_BORDER | WS_TABSTOP, 7, 7, 100, 12, 100, "");
|
|
DlgBuilder two(WS_CHILD | WS_CAPTION, 200, 90, "Two");
|
|
two.Item(0x0080, BS_AUTOCHECKBOX | WS_TABSTOP, 7, 7, 80, 10, 101, "Check");
|
|
PROPSHEETPAGEW pages[2] = {};
|
|
for (int i = 0; i < 2; i++) {
|
|
pages[i].dwSize = sizeof(PROPSHEETPAGEW);
|
|
pages[i].dwFlags = PSP_DLGINDIRECT;
|
|
pages[i].pResource = (LPCDLGTEMPLATE)(i ? two.Get() : one.Get());
|
|
pages[i].pfnDlgProc = WizPageProc;
|
|
}
|
|
HPROPSHEETPAGE extra = ::CreatePropertySheetPageW(&pages[1]);
|
|
CHECK(extra != NULL);
|
|
|
|
// A modeless property sheet: tabs, pages and the Apply button.
|
|
PROPSHEETHEADERW psh = {};
|
|
psh.dwSize = sizeof psh;
|
|
psh.dwFlags = PSH_PROPSHEETPAGE | PSH_MODELESS;
|
|
psh.hInstance = ::GetModuleHandleW(NULL);
|
|
psh.pszCaption = L"Sheet";
|
|
psh.nPages = 2;
|
|
psh.ppsp = pages;
|
|
g_pageCodes.clear();
|
|
HWND sheet = (HWND)::PropertySheetW(&psh);
|
|
CHECK(sheet != NULL && ::IsWindow(sheet));
|
|
wchar_t title[64] = L"";
|
|
::GetWindowTextW(sheet, title, 64);
|
|
CHECK(wcscmp(title, L"Sheet") == 0);
|
|
HWND tab = (HWND)::SendMessageW(sheet, PSM_GETTABCONTROL, 0, 0);
|
|
CHECK(tab && ::SendMessageW(tab, TCM_GETITEMCOUNT, 0, 0) == 2);
|
|
HWND page0 = (HWND)::SendMessageW(sheet, PSM_GETCURRENTPAGEHWND, 0, 0);
|
|
CHECK(page0 && ::GetParent(page0) == sheet && ::GetDlgItem(page0, 100) && PageCode(PSN_SETACTIVE));
|
|
CHECK(::GetWindowLongW(page0, GWL_STYLE) & WS_CHILD);
|
|
HWND apply = ::GetDlgItem(sheet, 0x3021);
|
|
CHECK(apply && !::IsWindowEnabled(apply));
|
|
::SendMessageW(sheet, PSM_CHANGED, (WPARAM)page0, 0);
|
|
CHECK(::IsWindowEnabled(apply));
|
|
// A page can keep the focus.
|
|
g_pageRefuses = true;
|
|
CHECK(!::SendMessageW(sheet, PSM_SETCURSEL, 1, 0));
|
|
g_pageRefuses = false;
|
|
g_pageCodes.clear();
|
|
CHECK(::SendMessageW(sheet, PSM_SETCURSEL, 1, 0));
|
|
CHECK(PageCode(PSN_KILLACTIVE) && PageCode(PSN_SETACTIVE));
|
|
CHECK(::SendMessageW(tab, TCM_GETCURSEL, 0, 0) == 1);
|
|
CHECK(::SendMessageW(sheet, PSM_ADDPAGE, 0, (LPARAM)extra) && ::SendMessageW(tab, TCM_GETITEMCOUNT, 0, 0) == 3);
|
|
CHECK(::SendMessageW(sheet, PSM_INDEXTOPAGE, 2, 0) == (LRESULT)extra);
|
|
g_pageCodes.clear();
|
|
::SendMessageW(sheet, PSM_PRESSBUTTON, PSBTN_APPLYNOW, 0);
|
|
CHECK(PageCode(PSN_APPLY) && !::IsWindowEnabled(apply));
|
|
g_pageCodes.clear();
|
|
::SendMessageW(sheet, PSM_PRESSBUTTON, PSBTN_OK, 0);
|
|
CHECK(PageCode(PSN_APPLY) && ::SendMessageW(sheet, PSM_GETCURRENTPAGEHWND, 0, 0) == 0);
|
|
::DestroyWindow(sheet);
|
|
|
|
// A wizard: Next and Back move through the pages.
|
|
psh.dwFlags = PSH_PROPSHEETPAGE | PSH_MODELESS | PSH_WIZARD;
|
|
g_pageCodes.clear();
|
|
sheet = (HWND)::PropertySheetW(&psh);
|
|
CHECK(sheet != NULL);
|
|
Pump();
|
|
HWND next = ::GetDlgItem(sheet, 0x3024), back = ::GetDlgItem(sheet, 0x3023), fin = ::GetDlgItem(sheet, 0x3025);
|
|
CHECK(next && back && fin && ::IsWindowEnabled(next) && !(::GetWindowLongW(fin, GWL_STYLE) & WS_VISIBLE));
|
|
::SendMessageW(sheet, PSM_PRESSBUTTON, PSBTN_NEXT, 0);
|
|
CHECK(PageCode(PSN_WIZNEXT));
|
|
HWND page1 = (HWND)::SendMessageW(sheet, PSM_GETCURRENTPAGEHWND, 0, 0);
|
|
CHECK(page1 && ::GetDlgItem(page1, 101) && ::SendMessageW(sheet, PSM_HWNDTOINDEX, (WPARAM)page1, 0) == 1);
|
|
::SendMessageW(sheet, PSM_SETWIZBUTTONS, 0, PSWIZB_BACK | PSWIZB_FINISH);
|
|
CHECK((::GetWindowLongW(fin, GWL_STYLE) & WS_VISIBLE) && !(::GetWindowLongW(next, GWL_STYLE) & WS_VISIBLE));
|
|
::SendMessageW(sheet, PSM_PRESSBUTTON, PSBTN_BACK, 0);
|
|
HWND cur = (HWND)::SendMessageW(sheet, PSM_GETCURRENTPAGEHWND, 0, 0);
|
|
CHECK(PageCode(PSN_WIZBACK) && ::SendMessageW(sheet, PSM_HWNDTOINDEX, (WPARAM)cur, 0) == 0);
|
|
::SendMessageW(sheet, PSM_SETWIZBUTTONS, 0, PSWIZB_FINISH);
|
|
g_pageCodes.clear();
|
|
::SendMessageW(sheet, PSM_PRESSBUTTON, PSBTN_FINISH, 0);
|
|
CHECK(PageCode(PSN_WIZFINISH) && ::SendMessageW(sheet, PSM_GETCURRENTPAGEHWND, 0, 0) == 0);
|
|
::DestroyWindow(sheet);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Window shapes, and files dropped on a window.
|
|
|
|
static int g_drops = 0;
|
|
static std::vector<std::wstring> g_dropped;
|
|
static POINT g_dropPt;
|
|
static BOOL g_dropInClient;
|
|
static UINT g_dropShort;
|
|
static WCHAR g_dropShortText[8];
|
|
|
|
static LRESULT CALLBACK DropProc(HWND h, UINT msg, WPARAM wp, LPARAM lp) {
|
|
if (msg != WM_DROPFILES) return ::DefWindowProcW(h, msg, wp, lp);
|
|
HDROP drop = (HDROP)wp;
|
|
g_dropped.clear();
|
|
UINT n = ::DragQueryFileW(drop, 0xFFFFFFFF, NULL, 0);
|
|
for (UINT i = 0; i < n; i++) {
|
|
UINT len = ::DragQueryFileW(drop, i, NULL, 0);
|
|
std::vector<WCHAR> buf(len + 1);
|
|
::DragQueryFileW(drop, i, buf.data(), len + 1);
|
|
g_dropped.push_back(std::wstring(buf.data(), len));
|
|
}
|
|
// A buffer too short takes what fits.
|
|
g_dropShort = ::DragQueryFileW(drop, 0, g_dropShortText, 4);
|
|
g_dropInClient = ::DragQueryPoint(drop, &g_dropPt);
|
|
::DragFinish(drop);
|
|
g_drops++;
|
|
return 0;
|
|
}
|
|
|
|
// An HDROP as the shell makes one: a DROPFILES and the paths.
|
|
static HGLOBAL MakeDrop(const void* paths, size_t bytes, BOOL wide, POINT pt, BOOL nc) {
|
|
HGLOBAL h = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof(DROPFILES) + bytes);
|
|
DROPFILES* df = (DROPFILES*)::GlobalLock(h);
|
|
df->pFiles = sizeof(DROPFILES);
|
|
df->pt = pt;
|
|
df->fNC = nc;
|
|
df->fWide = wide;
|
|
memcpy(df + 1, paths, bytes);
|
|
::GlobalUnlock(h);
|
|
return h;
|
|
}
|
|
|
|
static void TestShapesAndDrops() {
|
|
WNDCLASSW wc;
|
|
memset(&wc, 0, sizeof(wc));
|
|
wc.lpfnWndProc = DropProc;
|
|
wc.hInstance = ::GetModuleHandleW(NULL);
|
|
wc.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
|
|
wc.lpszClassName = L"WinTestDrop";
|
|
CHECK(::RegisterClassW(&wc) != 0);
|
|
HWND top = ::CreateWindowExW(WS_EX_TOPMOST, L"WinTestDrop", L"drop", WS_POPUP | WS_VISIBLE, 100, 100, 300, 200,
|
|
NULL, NULL, wc.hInstance, NULL);
|
|
HWND child = ::CreateWindowExW(0, L"WinTestDrop", L"shaped", WS_CHILD | WS_VISIBLE, 0, 0, 100, 100, top, NULL,
|
|
wc.hInstance, NULL);
|
|
CHECK(top != NULL && child != NULL);
|
|
::UpdateWindow(top);
|
|
|
|
// The child keeps only its right half.
|
|
HRGN rgn = ::CreateRectRgn(0, 0, 0, 0);
|
|
CHECK(::GetWindowRgn(child, rgn) == ERROR);
|
|
CHECK(::SetWindowRgn(child, ::CreateRectRgn(50, 0, 100, 100), TRUE));
|
|
CHECK(::GetWindowRgn(child, rgn) == SIMPLEREGION);
|
|
RECT box;
|
|
::GetRgnBox(rgn, &box);
|
|
CHECK(box.left == 50 && box.top == 0 && box.right == 100 && box.bottom == 100);
|
|
POINT outside = {110, 110}, inside = {160, 110};
|
|
CHECK(::WindowFromPoint(outside) == top);
|
|
CHECK(::WindowFromPoint(inside) == child);
|
|
HDC dc = ::GetDC(child);
|
|
::GetClipBox(dc, &box);
|
|
CHECK(box.left == 50 && box.right == 100);
|
|
::ReleaseDC(child, dc);
|
|
CHECK(::SetWindowRgn(child, NULL, TRUE));
|
|
CHECK(::GetWindowRgn(child, rgn) == ERROR);
|
|
CHECK(::WindowFromPoint(outside) == child);
|
|
::DeleteObject(rgn);
|
|
|
|
// Files dropped on the window.
|
|
::DragAcceptFiles(top, TRUE);
|
|
CHECK((::GetWindowLongW(top, GWL_EXSTYLE) & WS_EX_ACCEPTFILES) != 0);
|
|
static const WCHAR wide[] = L"C:\\one.txt\0C:\\two words.txt\0";
|
|
POINT pt = {12, 34};
|
|
::SendMessageW(top, WM_DROPFILES, (WPARAM)MakeDrop(wide, sizeof(wide), TRUE, pt, FALSE), 0);
|
|
CHECK(g_drops == 1);
|
|
CHECK(g_dropped.size() == 2);
|
|
if (g_dropped.size() == 2) {
|
|
CHECK(g_dropped[0] == L"C:\\one.txt");
|
|
CHECK(g_dropped[1] == L"C:\\two words.txt");
|
|
}
|
|
CHECK(g_dropShort == 3 && lstrcmpW(g_dropShortText, L"C:\\") == 0);
|
|
CHECK(g_dropInClient && g_dropPt.x == 12 && g_dropPt.y == 34);
|
|
// The narrow form, read wide; a drop on the frame.
|
|
static const char narrow[] = "/tmp/three\0";
|
|
::SendMessageW(top, WM_DROPFILES, (WPARAM)MakeDrop(narrow, sizeof(narrow), FALSE, pt, TRUE), 0);
|
|
CHECK(g_drops == 2 && g_dropped.size() == 1 && g_dropped[0] == L"/tmp/three");
|
|
CHECK(!g_dropInClient);
|
|
char a[32] = "";
|
|
HGLOBAL h = MakeDrop(wide, sizeof(wide), TRUE, pt, FALSE);
|
|
CHECK(::DragQueryFileA((HDROP)h, 1, a, sizeof(a)) == 16 && strcmp(a, "C:\\two words.txt") == 0);
|
|
CHECK(::DragQueryFileA((HDROP)h, 2, a, sizeof(a)) == 0);
|
|
::DragFinish((HDROP)h);
|
|
::DragAcceptFiles(top, FALSE);
|
|
CHECK((::GetWindowLongW(top, GWL_EXSTYLE) & WS_EX_ACCEPTFILES) == 0);
|
|
::DestroyWindow(top);
|
|
}
|
|
|
|
static void TestCWnd() {
|
|
CTestWnd wnd;
|
|
CString cls = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW, NULL, (HBRUSH)(COLOR_WINDOW + 1));
|
|
CHECK(wnd.CreateEx(0, cls, _T("MFC window"), WS_OVERLAPPEDWINDOW, CRect(0, 0, 320, 240), NULL, 0));
|
|
CHECK(wnd.m_nCreate == 1);
|
|
CHECK(CWnd::FromHandlePermanent(wnd.m_hWnd) == &wnd);
|
|
CString title;
|
|
wnd.GetWindowText(title);
|
|
CHECK(title == _T("MFC window"));
|
|
CRect rc;
|
|
wnd.GetClientRect(&rc);
|
|
wnd.SetWindowPos(NULL, 0, 0, 400, 300, SWP_NOMOVE | SWP_NOZORDER);
|
|
CHECK(wnd.m_nSize >= 1);
|
|
wnd.GetClientRect(&rc);
|
|
CHECK(wnd.m_size == rc.Size());
|
|
CHECK(wnd.SendMessage(WM_APP + 5, 21) == 42 && wnd.m_nApp == 1);
|
|
wnd.SetTimer(7, 20, NULL);
|
|
DWORD start = ::GetTickCount();
|
|
while (wnd.m_nTimer < 1 && ::GetTickCount() - start < 3000) AfxPumpMessage();
|
|
CHECK(wnd.m_nTimer >= 1);
|
|
wnd.KillTimer(7);
|
|
wnd.DestroyWindow();
|
|
CHECK(wnd.m_bPostNcDestroy && wnd.m_hWnd == NULL);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Printing: the default printer, printer DCs and their settings, a job
|
|
// written to a file, and the Print and Page Setup dialogs driven by
|
|
// their hooks.
|
|
|
|
static int g_abortCalls = 0;
|
|
static BOOL CALLBACK CountingAbortProc(HDC, int) {
|
|
g_abortCalls++;
|
|
return TRUE;
|
|
}
|
|
|
|
static UINT_PTR CALLBACK PrintPagesHook(HWND h, UINT msg, WPARAM, LPARAM lp) {
|
|
if (msg == WM_INITDIALOG) {
|
|
CHECK(((PRINTDLGW*)lp)->lCustData == 77);
|
|
CHECK(::GetDlgItem(h, edt1) != NULL && ::GetDlgItem(h, edt2) != NULL && ::GetDlgItem(h, edt3) != NULL);
|
|
::CheckRadioButton(h, rad1, rad3, rad3);
|
|
::SetDlgItemInt(h, edt1, 2, FALSE);
|
|
::SetDlgItemInt(h, edt2, 3, FALSE);
|
|
::SetDlgItemInt(h, edt3, 2, FALSE);
|
|
::PostMessageW(h, WM_COMMAND, IDOK, 0);
|
|
return TRUE;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static UINT_PTR CALLBACK PrintCancelHook(HWND h, UINT msg, WPARAM, LPARAM) {
|
|
if (msg == WM_INITDIALOG) {
|
|
::PostMessageW(h, WM_COMMAND, IDCANCEL, 0);
|
|
return TRUE;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static UINT_PTR CALLBACK PageSetupHook(HWND h, UINT msg, WPARAM, LPARAM lp) {
|
|
if (msg == WM_INITDIALOG) {
|
|
CHECK(((PAGESETUPDLGW*)lp)->lCustData == 78);
|
|
::CheckRadioButton(h, rad1, rad2, rad2);
|
|
::SendMessageW(h, WM_COMMAND, MAKEWPARAM(rad2, BN_CLICKED), (LPARAM)::GetDlgItem(h, rad2));
|
|
::PostMessageW(h, WM_COMMAND, IDOK, 0);
|
|
return TRUE;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static std::wstring DevNamesDevice(HGLOBAL h) {
|
|
DEVNAMES* dn = (DEVNAMES*)::GlobalLock(h);
|
|
std::wstring s = dn ? (const wchar_t*)dn + dn->wDeviceOffset : L"";
|
|
::GlobalUnlock(h);
|
|
return s;
|
|
}
|
|
|
|
static void TestPrinting() {
|
|
// The default printer, with a DC.
|
|
PRINTDLGW pd;
|
|
memset(&pd, 0, sizeof pd);
|
|
pd.lStructSize = sizeof pd;
|
|
pd.Flags = PD_RETURNDEFAULT | PD_RETURNDC;
|
|
if (!::PrintDlgW(&pd)) {
|
|
printf("note: no printer (%lu), skipping printing\n", (unsigned long)::CommDlgExtendedError());
|
|
return;
|
|
}
|
|
CHECK(pd.hDevNames != NULL && pd.hDevMode != NULL && pd.hDC != NULL);
|
|
std::wstring device = DevNamesDevice(pd.hDevNames);
|
|
DEVMODEW* dm = (DEVMODEW*)::GlobalLock(pd.hDevMode);
|
|
CHECK(dm != NULL && device == dm->dmDeviceName);
|
|
CHECK(dm != NULL && (dm->dmFields & DM_ORIENTATION) && dm->dmOrientation == DMORIENT_PORTRAIT);
|
|
DEVMODEW portrait = *dm;
|
|
::GlobalUnlock(pd.hDevMode);
|
|
DEVNAMES* dn = (DEVNAMES*)::GlobalLock(pd.hDevNames);
|
|
CHECK(dn != NULL && (dn->wDefault & DN_DEFAULTPRN));
|
|
::GlobalUnlock(pd.hDevNames);
|
|
HDC hdc = pd.hDC;
|
|
CHECK(::GetDeviceCaps(hdc, TECHNOLOGY) == DT_RASPRINTER);
|
|
int dpi = ::GetDeviceCaps(hdc, LOGPIXELSX), cx = ::GetDeviceCaps(hdc, HORZRES), cy = ::GetDeviceCaps(hdc, VERTRES);
|
|
int pw = ::GetDeviceCaps(hdc, PHYSICALWIDTH), ph = ::GetDeviceCaps(hdc, PHYSICALHEIGHT);
|
|
int ox = ::GetDeviceCaps(hdc, PHYSICALOFFSETX), oy = ::GetDeviceCaps(hdc, PHYSICALOFFSETY);
|
|
printf("printer '%ls': %d dpi, %dx%d of %dx%d at %d,%d\n", device.c_str(), dpi, cx, cy, pw, ph, ox, oy);
|
|
CHECK(dpi >= 72 && cy > cx && cx > dpi * 4);
|
|
CHECK(pw >= cx + ox && ph >= cy + oy);
|
|
CHECK(::GetDeviceCaps(hdc, HORZSIZE) > 100 && ::GetDeviceCaps(hdc, HORZSIZE) < 300);
|
|
|
|
// The same printer turned: a landscape DC, and ResetDC back.
|
|
portrait.dmFields = DM_ORIENTATION;
|
|
DEVMODEW landscape = portrait;
|
|
landscape.dmOrientation = DMORIENT_LANDSCAPE;
|
|
HDC hdcLand = ::CreateDCW(L"WINSPOOL", device.c_str(), NULL, &landscape);
|
|
CHECK(hdcLand != NULL);
|
|
if (hdcLand) {
|
|
CHECK(::GetDeviceCaps(hdcLand, PHYSICALWIDTH) == ph && ::GetDeviceCaps(hdcLand, PHYSICALHEIGHT) == pw);
|
|
CHECK(::GetDeviceCaps(hdcLand, HORZRES) > ::GetDeviceCaps(hdcLand, VERTRES));
|
|
CHECK(::ResetDCW(hdcLand, &portrait) == hdcLand);
|
|
CHECK(::GetDeviceCaps(hdcLand, HORZRES) == cx && ::GetDeviceCaps(hdcLand, VERTRES) == cy);
|
|
::DeleteDC(hdcLand);
|
|
}
|
|
|
|
// A two-page job, to a file, on a DC made from the settings.
|
|
CHECK(::DeleteDC(hdc));
|
|
hdc = ::CreateDCW(L"WINSPOOL", device.c_str(), NULL, &portrait);
|
|
CHECK(hdc != NULL);
|
|
wchar_t dir[MAX_PATH], path[MAX_PATH];
|
|
::GetTempPathW(MAX_PATH, dir);
|
|
::GetTempFileNameW(dir, L"prt", 0, path);
|
|
g_abortCalls = 0;
|
|
CHECK(::SetAbortProc(hdc, CountingAbortProc) > 0);
|
|
DOCINFOW di;
|
|
memset(&di, 0, sizeof di);
|
|
di.cbSize = sizeof di;
|
|
di.lpszDocName = L"wintest";
|
|
di.lpszOutput = path;
|
|
int job = ::StartDocW(hdc, &di);
|
|
if (job <= 0) printf("StartDoc: error %lu\n", (unsigned long)::GetLastError());
|
|
CHECK(job > 0);
|
|
for (int page = 0; page < 2; page++) {
|
|
CHECK(::StartPage(hdc) > 0);
|
|
RECT r = {dpi, dpi, dpi * 3, dpi * 2};
|
|
HBRUSH br = ::CreateSolidBrush(RGB(0, 0, 255));
|
|
::FillRect(hdc, &r, br);
|
|
::DeleteObject(br);
|
|
::TextOutW(hdc, dpi, dpi * 3, L"Printed by wintest", 18);
|
|
CHECK(::EndPage(hdc) > 0);
|
|
}
|
|
CHECK(::EndDoc(hdc) > 0);
|
|
CHECK(g_abortCalls >= 1);
|
|
HANDLE f = ::CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
|
|
CHECK(f != INVALID_HANDLE_VALUE);
|
|
if (f != INVALID_HANDLE_VALUE) {
|
|
char head[8] = {};
|
|
DWORD got = 0;
|
|
::ReadFile(f, head, 4, &got, NULL);
|
|
DWORD size = ::GetFileSize(f, NULL);
|
|
::CloseHandle(f);
|
|
printf("print file: %lu bytes, '%.4s'\n", (unsigned long)size, head);
|
|
CHECK(got == 4 && size > 100);
|
|
CHECK(memcmp(head, "%PDF", 4) == 0 || memcmp(head, "%!PS", 4) == 0);
|
|
}
|
|
::DeleteFileW(path);
|
|
|
|
// A job abandoned part way.
|
|
CHECK(::StartDocW(hdc, &di) > 0);
|
|
CHECK(::StartPage(hdc) > 0);
|
|
CHECK(::AbortDoc(hdc) > 0);
|
|
::DeleteFileW(path);
|
|
CHECK(::DeleteDC(hdc));
|
|
::GlobalFree(pd.hDevNames);
|
|
::GlobalFree(pd.hDevMode);
|
|
|
|
// Errors: a default asked for with settings given; a wrong size.
|
|
memset(&pd, 0, sizeof pd);
|
|
pd.lStructSize = sizeof pd;
|
|
pd.Flags = PD_RETURNDEFAULT;
|
|
pd.hDevMode = ::GlobalAlloc(GMEM_MOVEABLE, sizeof(DEVMODEW));
|
|
CHECK(!::PrintDlgW(&pd) && ::CommDlgExtendedError() == PDERR_RETDEFFAILURE);
|
|
::GlobalFree(pd.hDevMode);
|
|
pd.hDevMode = NULL;
|
|
pd.lStructSize = 12;
|
|
CHECK(!::PrintDlgW(&pd) && ::CommDlgExtendedError() == CDERR_STRUCTSIZE);
|
|
|
|
// The ANSI form.
|
|
PRINTDLGA pda;
|
|
memset(&pda, 0, sizeof pda);
|
|
pda.lStructSize = sizeof pda;
|
|
pda.Flags = PD_RETURNDEFAULT;
|
|
CHECK(::PrintDlgA(&pda));
|
|
if (pda.hDevNames && pda.hDevMode) {
|
|
DEVNAMES* dna = (DEVNAMES*)::GlobalLock(pda.hDevNames);
|
|
DEVMODEA* dma = (DEVMODEA*)::GlobalLock(pda.hDevMode);
|
|
CHECK(strcmp((const char*)dna + dna->wDeviceOffset, (const char*)dma->dmDeviceName) == 0);
|
|
CHECK(*((const char*)dna + dna->wDriverOffset) != 0);
|
|
::GlobalUnlock(pda.hDevMode);
|
|
::GlobalUnlock(pda.hDevNames);
|
|
}
|
|
::GlobalFree(pda.hDevNames);
|
|
::GlobalFree(pda.hDevMode);
|
|
|
|
// The Print dialog: pages 2 to 3, twice.
|
|
memset(&pd, 0, sizeof pd);
|
|
pd.lStructSize = sizeof pd;
|
|
pd.Flags = PD_ENABLEPRINTHOOK | PD_NOSELECTION;
|
|
pd.lpfnPrintHook = PrintPagesHook;
|
|
pd.lCustData = 77;
|
|
pd.nFromPage = pd.nToPage = 1;
|
|
pd.nMinPage = 1;
|
|
pd.nMaxPage = 9;
|
|
pd.nCopies = 1;
|
|
CHECK(::PrintDlgW(&pd));
|
|
CHECK(pd.Flags & PD_PAGENUMS);
|
|
CHECK(pd.nFromPage == 2 && pd.nToPage == 3);
|
|
CHECK(pd.hDevNames != NULL && pd.hDevMode != NULL && pd.hDC == NULL);
|
|
int copies = pd.nCopies;
|
|
if (pd.hDevMode) {
|
|
DEVMODEW* m = (DEVMODEW*)::GlobalLock(pd.hDevMode);
|
|
if (copies == 1 && m) copies = m->dmCopies;
|
|
::GlobalUnlock(pd.hDevMode);
|
|
}
|
|
CHECK(copies == 2);
|
|
::GlobalFree(pd.hDevNames);
|
|
::GlobalFree(pd.hDevMode);
|
|
|
|
// Cancel leaves the structure.
|
|
memset(&pd, 0, sizeof pd);
|
|
pd.lStructSize = sizeof pd;
|
|
pd.Flags = PD_ENABLEPRINTHOOK;
|
|
pd.lpfnPrintHook = PrintCancelHook;
|
|
CHECK(!::PrintDlgW(&pd));
|
|
CHECK(pd.hDC == NULL && pd.hDevNames == NULL);
|
|
|
|
// Page Setup: the defaults, then landscape.
|
|
PAGESETUPDLGW psd;
|
|
memset(&psd, 0, sizeof psd);
|
|
psd.lStructSize = sizeof psd;
|
|
psd.Flags = PSD_RETURNDEFAULT | PSD_INTHOUSANDTHSOFINCHES;
|
|
CHECK(::PageSetupDlgW(&psd));
|
|
POINT paper = psd.ptPaperSize;
|
|
printf("paper %ldx%ld, margins %ld,%ld,%ld,%ld\n", (long)paper.x, (long)paper.y, (long)psd.rtMargin.left,
|
|
(long)psd.rtMargin.top, (long)psd.rtMargin.right, (long)psd.rtMargin.bottom);
|
|
CHECK(paper.y > paper.x && paper.x > 7000 && paper.x < 20000);
|
|
CHECK(psd.hDevNames != NULL && psd.hDevMode != NULL);
|
|
psd.Flags = PSD_ENABLEPAGESETUPHOOK | PSD_INTHOUSANDTHSOFINCHES;
|
|
psd.lpfnPageSetupHook = PageSetupHook;
|
|
psd.lCustData = 78;
|
|
CHECK(::PageSetupDlgW(&psd));
|
|
CHECK(psd.ptPaperSize.x == paper.y && psd.ptPaperSize.y == paper.x);
|
|
if (psd.hDevMode) {
|
|
DEVMODEW* m = (DEVMODEW*)::GlobalLock(psd.hDevMode);
|
|
CHECK(m && m->dmOrientation == DMORIENT_LANDSCAPE);
|
|
::GlobalUnlock(psd.hDevMode);
|
|
}
|
|
::GlobalFree(psd.hDevNames);
|
|
::GlobalFree(psd.hDevMode);
|
|
}
|
|
|
|
#ifdef _WINPORT
|
|
// WinHelp, which opens the help document next to the .hlp file: here
|
|
// through WINPORT_HELP, a command that notes what it was given.
|
|
static std::string WaitForLine(const std::string& log, size_t n) {
|
|
for (int i = 0; i < 500; i++) {
|
|
std::vector<std::string> lines;
|
|
if (FILE* f = fopen(log.c_str(), "r")) {
|
|
char buf[1024];
|
|
while (fgets(buf, sizeof(buf), f)) lines.push_back(buf);
|
|
fclose(f);
|
|
}
|
|
if (lines.size() >= n) {
|
|
std::string s = lines[n - 1];
|
|
while (!s.empty() && (s.back() == '\n' || s.back() == '\r')) s.pop_back();
|
|
return s;
|
|
}
|
|
::Sleep(10);
|
|
}
|
|
return std::string();
|
|
}
|
|
|
|
static void TestWinHelp() {
|
|
const char* out = getenv("MFC_TEST_OUT");
|
|
std::string dir = out && *out ? out : ".";
|
|
std::string log = dir + "/winhelp.log";
|
|
remove(log.c_str());
|
|
std::string html = dir + "/AppHelp.html";
|
|
if (FILE* f = fopen(html.c_str(), "w")) {
|
|
fputs("<html><body><h1 id=\"131072\">Help</h1></body></html>\n", f);
|
|
fclose(f);
|
|
}
|
|
std::string cmd = "echo >> '" + log + "'";
|
|
setenv("WINPORT_HELP", cmd.c_str(), 1);
|
|
std::wstring hlp = std::wstring(CStringW(dir.c_str())) + L"/AppHelp.HLP";
|
|
|
|
// The contents: the document itself.
|
|
CHECK(::WinHelpW(NULL, hlp.c_str(), HELP_FINDER, 0));
|
|
CHECK(WaitForLine(log, 1) == html);
|
|
// A context: the page's anchor; a window after the name is let go.
|
|
std::string hlpA = dir + "/AppHelp.HLP>main";
|
|
CHECK(::WinHelpA(NULL, hlpA.c_str(), HELP_CONTEXT, 131072));
|
|
CHECK(WaitForLine(log, 2) == "file://" + html + "#131072");
|
|
// Through CWnd, with the program's own help file.
|
|
CWinApp* pApp = AfxGetApp();
|
|
LPCTSTR pszOld = pApp->m_pszHelpFilePath;
|
|
CString strHlp(hlp.c_str());
|
|
pApp->m_pszHelpFilePath = strHlp;
|
|
CWnd wnd;
|
|
CHECK(wnd.CreateEx(0, _T("STATIC"), _T("help"), WS_POPUP, CRect(0, 0, 10, 10), NULL, 0));
|
|
wnd.WinHelp(0, HELP_INDEX);
|
|
wnd.DestroyWindow();
|
|
pApp->m_pszHelpFilePath = pszOld;
|
|
CHECK(WaitForLine(log, 3) == html);
|
|
|
|
// Nothing to close; no document.
|
|
CHECK(::WinHelpW(NULL, hlp.c_str(), HELP_QUIT, 0));
|
|
::SetLastError(0);
|
|
CHECK(!::WinHelpW(NULL, L"/nowhere/none.hlp", HELP_CONTENTS, 0));
|
|
CHECK(::GetLastError() == ERROR_FILE_NOT_FOUND);
|
|
unsetenv("WINPORT_HELP");
|
|
remove(html.c_str());
|
|
remove(log.c_str());
|
|
}
|
|
#endif
|
|
|
|
int main() {
|
|
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOW)) {
|
|
printf("FAIL: AfxWinInit\n");
|
|
return 1;
|
|
}
|
|
TestClassesAndCreation();
|
|
TestTree();
|
|
TestMessages();
|
|
TestTimers();
|
|
TestThreads();
|
|
TestHooksAndAnsi();
|
|
TestFocusAndPaint();
|
|
TestMemoryDC();
|
|
TestDIBSections();
|
|
TestRegionsAndClipping();
|
|
TestShapes();
|
|
TestText();
|
|
TestIconsAndAccelerators();
|
|
TestMenus();
|
|
TestDialogsAndControls();
|
|
TestMdi();
|
|
TestImageLists();
|
|
TestCommonControls();
|
|
TestMoreControls();
|
|
TestListView();
|
|
TestTreeView();
|
|
TestPropertySheets();
|
|
TestPrinting();
|
|
TestShapesAndDrops();
|
|
TestCWnd();
|
|
#ifdef _WINPORT
|
|
TestWinHelp();
|
|
#endif
|
|
printf("%s: %d failure(s)\n", g_failures ? "FAIL" : "PASS", g_failures);
|
|
return g_failures ? 1 : 0;
|
|
}
|