1 
2 
3 #include <windows.h>
4 #include <assert.h>
5 #include "ttwain_win.h"
6 #include "ttwain_winPD.h"
7 #include "ttwain_state.h"
8 #include "ttwain_error.h"
9 
10 #define HINSTLIB0 0
11 static HWND Dummy = 0; /* proxy window */
12 
13 extern int TTWAIN_MessageHook(void *lpmsg);
14 
CreateDummyWindow(void)15 static HWND CreateDummyWindow(void) {
16   HWND hwnd;
17   hwnd = CreateWindow("STATIC",                      // class
18                       "Acquire Dummy",               // title
19                       WS_POPUPWINDOW,                // style
20                       CW_USEDEFAULT, CW_USEDEFAULT,  // x, y
21                       CW_USEDEFAULT, CW_USEDEFAULT,  // width, height
22                       HWND_DESKTOP,                  // parent window
23                       NULL,                          // hmenu
24                       HINSTLIB0,                     // hinst
25                       NULL);                         // lpvparam
26   return hwnd;
27 }
28 /*---------------------------------------------------------------------------*/
TTWAIN_GetValidHwndPD(void * _hwnd)29 void *TTWAIN_GetValidHwndPD(void *_hwnd)
30 // Returns a valid window handle as follows:
31 // If hwnd is a valid window handle, hwnd is returned.
32 // Otherwise a proxy window handle is created and returned.
33 // Once created, a proxy window handle is destroyed when
34 // the source manager is unloaded.
35 // If hwnd is an invalid window handle (other than NULL)
36 // an error box is displayed.
37 {
38   HWND hwnd = (HWND)_hwnd;
39   if (!IsWindow(hwnd)) {
40     if (hwnd != NULL) {
41       assert(!"Window handle is invalid");
42       hwnd = NULL;
43     }
44     if (!Dummy) {
45       Dummy = CreateDummyWindow();
46       if (!IsWindow(Dummy)) {
47         assert(!"Unable to create Dummy window");
48         Dummy = NULL;
49       }
50     }
51     hwnd = Dummy;
52   }
53   return (void *)hwnd;
54 }
55 /*---------------------------------------------------------------------------*/
TTWAIN_EmptyMessageQueuePD(void)56 void TTWAIN_EmptyMessageQueuePD(void) {
57   MSG msg;
58 #ifdef _DEBUG
59   OutputDebugString("EmptyMsgQ<");
60 #endif
61   while (PeekMessage((LPMSG)&msg, NULL, 0, 0, PM_REMOVE)) {
62     if (!TTWAIN_MessageHook((LPMSG)&msg)) {
63       TranslateMessage((LPMSG)&msg);
64       DispatchMessage((LPMSG)&msg);
65 #ifdef _DEBUG
66       OutputDebugString("-");
67 #endif
68     } else {
69 #ifdef _DEBUG
70       OutputDebugString("T");
71 #endif
72     }
73   }
74 #ifdef _DEBUG
75   OutputDebugString(">\n");
76 #endif
77 }
78 
TTWAIN_ModalEventLoopPD(void)79 void TTWAIN_ModalEventLoopPD(void) {
80   MSG msg;
81   // Clear global breakout flag
82   TTwainData.breakModalLoop = FALSE;
83 
84   while ((TTWAIN_GetState() >= TWAIN_SOURCE_ENABLED) &&
85          !TTwainData.breakModalLoop && GetMessage((LPMSG)&msg, NULL, 0, 0)) {
86     if (!TTWAIN_MessageHook((LPMSG)&msg)) {
87       TranslateMessage((LPMSG)&msg);
88       DispatchMessage((LPMSG)&msg);
89     }
90   }  // while
91   TTwainData.breakModalLoop = FALSE;
92 }
TTWAIN_EnableWindowPD(void * hwnd,int flag)93 int TTWAIN_EnableWindowPD(void *hwnd, int flag) {
94   return EnableWindow(hwnd, flag);
95 }
96