Files
NeoandClaude dd357a6564 Build the window classes for 16-bit Windows (#104)
The Win16 library now has CWinApp, CWnd, the frames (SDI, MDI, mini
frames and splitters), CDialog with DDX, the common dialogs, the
controls, the GDI classes and document/view with printing.

- afxwin16u.h gives the Win32 USER, GDI and KERNEL declarations that
  the window classes use; src/win16u.cpp does them with the nearest
  16-bit calls, or fails as Win32 fails. include/win16 sends the OLE
  headers to the OLE 2 headers of the SDK; the common control types
  come from port/include, last on the include path.
- The ON_WM_ macros, OnCommand, the reflection and the MDI code read
  the 16-bit packing of WM_COMMAND, WM_ACTIVATE, the scroll, menu,
  list box and MDI messages, and WM_CTLCOLOR. GET_WM_COMMAND_* are in
  afxwin.h for each target.
- CAfxDlgTemplate writes the 16-bit dialog template. The File New type
  dialog keeps the chosen template in its state, not in the dialog
  result, which has no room for a far pointer on Win16.
- Registered message entries keep a DWORD_PTR, which holds a far
  pointer. The handle maps use a word key on Win16.
- The registry functions use the HKEY_CLASSES_ROOT tree of shell.dll;
  profiles go to the INI file. Events, mutexes and semaphores are task
  objects. The OLE virtuals are stubs (src/win16ole.cpp).
- tests/run-win16-tests.sh (make check) runs core, memtest and three
  window tests (frame and dialog, SDI, MDI) at -O0, -Os and -O2 in
  Wine: 15 of 15 pass.


Claude-Session: https://claude.ai/code/session_01B82eRRTV4u8c9f3gQUmqvq

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-24 20:44:18 -08:00

224 lines
6.7 KiB
C++

// win16doc.cpp - document/view on 16-bit Windows: a CSingleDocTemplate
// with a CDocument, a CView and a CFrameWnd from resources, File New,
// the view's OnDraw and OnUpdate, and the command routing from the frame
// to the view and the document. With -DTEST_MDI, a CMultiDocTemplate
// with a CMDIFrameWnd and two CMDIChildWnd windows, and the MDI menus.
// It prints ALL PASSED when each step worked.
#include <afxwin.h>
#include <stdio.h>
#include "win16wnd.h"
static int g_failures = 0;
static BOOL g_bDone = FALSE; // the last step ran
#define CHECK(cond) \
do { \
if (!(cond)) { \
printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
g_failures++; \
} \
} while (0)
#define ID_TEST_DOC 210
#define ID_TEST_VIEW 211
class CTestDoc : public CDocument {
DECLARE_DYNCREATE(CTestDoc)
public:
int m_nNew;
BOOL m_bDocCommand;
CString m_strText;
CTestDoc() : m_nNew(0), m_bDocCommand(FALSE) {}
/// Counts the new documents and sets the text.
virtual BOOL OnNewDocument() {
if (!CDocument::OnNewDocument()) return FALSE;
m_nNew++;
m_strText = "document text";
return TRUE;
}
protected:
/// A command that the document handles.
afx_msg void OnTestDoc() { m_bDocCommand = TRUE; }
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNCREATE(CTestDoc, CDocument)
BEGIN_MESSAGE_MAP(CTestDoc, CDocument)
ON_COMMAND(ID_TEST_DOC, OnTestDoc)
END_MESSAGE_MAP()
class CTestView : public CView {
DECLARE_DYNCREATE(CTestView)
public:
int m_nDraws;
int m_nUpdates;
BOOL m_bViewCommand;
CTestView() : m_nDraws(0), m_nUpdates(0), m_bViewCommand(FALSE) {}
/// Draws the text of the document.
virtual void OnDraw(CDC* pDC) {
CTestDoc* pDoc = (CTestDoc*)GetDocument();
pDC->TextOut(10, 10, pDoc->m_strText);
m_nDraws++;
}
/// Counts the updates, then redraws.
virtual void OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) {
m_nUpdates++;
CView::OnUpdate(pSender, lHint, pHint);
}
protected:
/// A command that the view handles.
afx_msg void OnTestView() { m_bViewCommand = TRUE; }
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNCREATE(CTestView, CView)
BEGIN_MESSAGE_MAP(CTestView, CView)
ON_COMMAND(ID_TEST_VIEW, OnTestView)
END_MESSAGE_MAP()
#ifdef TEST_MDI
class CTestChild : public CMDIChildWnd {
DECLARE_DYNCREATE(CTestChild)
protected:
/// Gives the child a position and a size. Wine puts a 16-bit MDI child
/// with CW_USEDEFAULT outside the client area, so it never paints.
virtual BOOL PreCreateWindow(CREATESTRUCT& cs) {
cs.x = cs.y = 10;
cs.cx = 200;
cs.cy = 120;
return CMDIChildWnd::PreCreateWindow(cs);
}
};
IMPLEMENT_DYNCREATE(CTestChild, CMDIChildWnd)
#define CTestFrameBase CMDIFrameWnd
#else
#define CTestFrameBase CFrameWnd
#endif
class CTestFrame : public CTestFrameBase {
DECLARE_DYNCREATE(CTestFrame)
public:
int m_nStep;
CTestFrame() : m_nStep(0) {}
protected:
/// Starts the timer that runs the steps.
afx_msg int OnCreate(LPCREATESTRUCT lpcs) {
if (CTestFrameBase::OnCreate(lpcs) == -1) return -1;
SetTimer(1, 50, NULL);
return 0;
}
/// Does one step of the test each time.
afx_msg void OnTimer(UINT_PTR) {
switch (m_nStep++) {
case 0:
#ifdef TEST_MDI
// A second document, in a second child window.
AfxGetApp()->OnFileNew();
break;
case 1:
#endif
// Commands from the menu go to the view and to the document.
PostMessage(WM_COMMAND, ID_TEST_VIEW, 0);
PostMessage(WM_COMMAND, ID_TEST_DOC, 0);
break;
#ifdef TEST_MDI
case 2: {
#else
case 1: {
#endif
KillTimer(1);
#ifdef TEST_MDI
BOOL bMaximized = TRUE;
CMDIChildWnd* pChild = MDIGetActive(&bMaximized);
CHECK(pChild != NULL && pChild->IsKindOf(RUNTIME_CLASS(CTestChild)));
CHECK(!bMaximized);
if (pChild == NULL) break;
CTestView* pView = (CTestView*)pChild->GetActiveView();
// The menu of the document type, with its Window menu.
CHECK(GetMenu() != NULL && GetMenu()->GetMenuItemCount() == 2);
// Activate the other child: the frame gets the other document.
MDINext();
CMDIChildWnd* pOther = MDIGetActive();
CHECK(pOther != NULL && pOther != pChild);
if (pOther != NULL)
CHECK(pOther->GetActiveDocument() != pChild->GetActiveDocument());
CHECK(GetActiveFrame() == pOther);
MDIActivate(pChild);
CHECK(MDIGetActive() == pChild);
#else
CTestView* pView = (CTestView*)GetActiveView();
#endif
CHECK(pView != NULL && pView->IsKindOf(RUNTIME_CLASS(CTestView)));
if (pView == NULL) break;
CTestDoc* pDoc = (CTestDoc*)pView->GetDocument();
CHECK(pDoc != NULL && pDoc->IsKindOf(RUNTIME_CLASS(CTestDoc)));
CHECK(pDoc->m_nNew == 1);
#ifdef TEST_MDI
CHECK(pDoc->GetTitle() == "TestDoc2");
#else
CHECK(pDoc->GetTitle() == "Untitled");
CHECK(GetMenu() != NULL && GetMenu()->GetMenuItemCount() == 1);
#endif
CHECK(pView->m_bViewCommand);
CHECK(pDoc->m_bDocCommand);
pView->UpdateWindow();
CHECK(pView->m_nDraws > 0);
CHECK(pView->m_nUpdates > 0);
g_bDone = TRUE;
PostMessage(WM_COMMAND, ID_APP_EXIT, 0);
break;
}
}
}
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNCREATE(CTestFrame, CTestFrameBase)
BEGIN_MESSAGE_MAP(CTestFrame, CTestFrameBase)
ON_WM_CREATE()
ON_WM_TIMER()
END_MESSAGE_MAP()
class CDocApp : public CWinApp {
public:
/// Adds the document template and makes the first document.
virtual BOOL InitInstance() {
#ifdef TEST_MDI
AddDocTemplate(new CMultiDocTemplate(IDR_DOCTYPE, RUNTIME_CLASS(CTestDoc),
RUNTIME_CLASS(CTestChild), RUNTIME_CLASS(CTestView)));
CTestFrame* pFrame = new CTestFrame;
if (!pFrame->LoadFrame(IDR_MAINFRAME)) {
printf("FAIL LoadFrame\n");
return FALSE;
}
m_pMainWnd = pFrame;
#else
AddDocTemplate(new CSingleDocTemplate(IDR_MAINFRAME, RUNTIME_CLASS(CTestDoc),
RUNTIME_CLASS(CTestFrame), RUNTIME_CLASS(CTestView)));
#endif
OnFileNew();
if (m_pMainWnd == NULL) {
printf("FAIL OnFileNew\n");
return FALSE;
}
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
/// Prints the result.
virtual int ExitInstance() {
CHECK(g_bDone);
printf(g_failures == 0 ? "ALL PASSED\n" : "FAILED %d\n", g_failures);
return CWinApp::ExitInstance();
}
};
CDocApp theApp;