forked from stephaniewm55/WinVi-Minimalist-Text-Hex-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterCom.c
206 lines (181 loc) · 5.55 KB
/
InterCom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/********************************************************\
|* *|
|* Program: WinVi *|
|* Author: Raphael Molle *|
|* Compiler: several *|
|* --------------------- -------------------------- *|
|* © Copyright 1994-2007 by Raphael Molle, Berlin *|
|* © Copyleft by GPL See file COPYING for more *|
|* info about terms of use *|
|* *|
\********************************************************/
/* revision log:
* 3-Dec-2000 first publication of source code
* 12-Dec-2000 missing initialization of DWORD i in EnumWindowsProc()
* 4-Apr-2004 return value of InterCommInit (actually not needed)
* 21-Jul-2007 window identifier can be included into window title
*/
#include <windows.h>
#include <string.h>
#include <wchar.h>
#include "winvi.h"
#include "intercom.h"
#define MAXDISPLAY 20
extern PCWSTR ClassName;
static UINT IcMessage, IcCount, IcUnassigned;
static BOOL IcLowest;
static UINT IcIndex = MAXDISPLAY;
static HWND IcWindows[MAXDISPLAY];
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lPar)
{ WCHAR Buf[20];
PARAM_NOT_USED(lPar);
Buf[19] = '\0';
if (GetClassNameW(hWnd, Buf, WSIZE(Buf)-1) && wcscmp(Buf, ClassName) == 0) {
DWORD i = MAXDISPLAY;
#if defined(WIN32)
#if !defined(SMTO_NORMAL)
#define SMTO_NORMAL 0x0000
#endif
SendMessageTimeout(hWnd, IcMessage, (WPARAM)hWnd, 0,
SMTO_NORMAL, 2000, &i);
#else
i = SendMessage(hWnd, IcMessage, (WPARAM)hWnd, 0);
#endif
if (i < MAXDISPLAY) {
if (!IcWindows[(int)i]) IcWindows[(int)i] = hWnd;
#if 0
// else OutputDebugString("Entry already in use!\n");
#endif
} else ++IcUnassigned;
}
return (TRUE);
}
static VOID RebuildWindowList(VOID)
{ static WNDENUMPROC EnumProc;
/*request window titles from all running WinVis*/
if (EnumProc == NULL)
EnumProc=(WNDENUMPROC)MakeProcInstance((FARPROC)EnumWindowsProc, hInst);
memset((VOID*)IcWindows, 0, sizeof(IcWindows));
EnumWindows(EnumProc, 0);
}
INT InterCommInit(HWND hWnd)
{ INT i;
PARAM_NOT_USED(hWnd);
IcMessage = RegisterWindowMessage("WinVi - Message");
for (;;) {
IcLowest = TRUE;
IcCount = IcUnassigned = 0;
RebuildWindowList();
if (IcCount <= 1 /*own window (might already be closed)*/) break;
if (!IcLowest && IcUnassigned > 1) {
IcCount = 0;
for (i=1000; i>0; --i) {
MessageLoop(FALSE);
if (IcCount && i > MAXDISPLAY) i = MAXDISPLAY;
}
}
}
/*search for an empty entry index to use for this WinVi instance...*/
for (i=0; i<MAXDISPLAY && IcWindows[i]; ++i) {}
IcIndex = i;
return (IcIndex);
}
BOOL InterCommCommand(WORD uCmd)
{ HWND hwndThis;
if ((uCmd -= 2000) < MAXDISPLAY && IsWindow(hwndThis = IcWindows[uCmd])) {
if (IsIconic(hwndThis)) ShowWindow(hwndThis, SW_RESTORE);
else {
#if defined(WIN32)
BringWindowToTop(hwndThis);
#else
SetActiveWindow(hwndThis);
#endif
}
return (TRUE);
}
return (FALSE);
}
#ifdef __LCC__
#define MENUITEMINFOW MENUITEMINFO
#define MIIRADIOMENU_DWTYPEDATA *(LPWSTR*)&mii.dwTypeData
#else
#define MIIRADIOMENU_DWTYPEDATA mii.dwTypeData
#endif
VOID InterCommMenu(HWND hWnd, HMENU hMenu)
{ INT i;
UINT LastItem = 0;
if (!IcMessage) return;
/*clear old list of menu entries*/
do; while (DeleteMenu(hMenu, 0, MF_BYPOSITION));
RebuildWindowList();
for (i=0; i<MAXDISPLAY; ++i) {
static WCHAR Buf[266];
PWSTR p, p2;
if (!IcWindows[i]) continue;
GetWindowTextW(IcWindows[i], Buf+4, WSIZE(Buf)-4);
if (!wcsncmp(Buf+4, L"WinVi - ", 8)) p = Buf + 8;
else if (wcscmp(Buf+wcslen(Buf+4)-4, L" - WinVi") == 0) {
Buf[wcslen(Buf+4)-4] = '\0';
p = Buf;
}
p2 = p+2;
while ((p2 = wcschr(p2+2, '&')) != NULL)
memmove(p2+1, p2, 2*wcslen(p2)+2);
p[0] = '&'; p[1] = i<9 ? '1'+i : 'a'-9+i; p[2] = ' '; p[3] = '\t';
#if defined(WIN32)
{ static MENUITEMINFOW mii = {sizeof(MENUITEMINFO),
MIIM_ID | MIIM_TYPE | MIIM_STATE,
MFT_RADIOCHECK | MFT_STRING};
MIIRADIOMENU_DWTYPEDATA = p;
mii.wID = 2000 + i;
mii.fState = IcWindows[i]==hWnd ? MFS_CHECKED : 0;
if (InsertMenuItemW(hMenu, LastItem, TRUE, &mii)) {
++LastItem;
continue;
}
}
#endif
InsertMenuW(hMenu, LastItem++,
IcWindows[i]!=hWnd ? MF_BYPOSITION | MF_STRING
: MF_CHECKED | MF_BYPOSITION | MF_STRING,
2000+i, p);
}
}
INT IcWindowID(VOID) {
if (IcIndex == MAXDISPLAY) return 0;
return IcIndex < '9' ? '1' + IcIndex : 'a' - 9 + IcIndex;
}
BOOL InterCommActivate(BOOL Backwards)
{ INT i, Start;
HWND Activate = 0;
if (!IcMessage) return (FALSE);
RebuildWindowList();
for (Start=0; IcWindows[Start]!=hwndMain && Start<MAXDISPLAY; ++Start);
if (Start >= MAXDISPLAY) return (FALSE);
i = Start;
while (++i != Start) {
if (i >= MAXDISPLAY) {
i = 0;
if (!Start) break;
}
if (IcWindows[i]) {
Activate = IcWindows[i];
if (!Backwards) break;
}
}
if (!Activate) return (FALSE);
#if defined(WIN32)
BringWindowToTop(Activate);
#else
SetActiveWindow(Activate);
#endif
return (TRUE);
}
LRESULT InterCommWindowProc(HWND hWnd, UINT uMsg, WPARAM wPar, LPARAM lPar)
{
PARAM_NOT_USED(lPar);
if (!uMsg || uMsg!=IcMessage) return ((LRESULT)-1);
++IcCount;
if ((HWND)wPar < hWnd) IcLowest = FALSE;
return (IcIndex);
}