1 | #include <stdlib.h>
|
---|
2 | #include <string.h>
|
---|
3 |
|
---|
4 | #include "HelpInstance.h"
|
---|
5 | #include "Viewer.h"
|
---|
6 | #include "log.h"
|
---|
7 |
|
---|
8 | //--------------------------------------------------------------------------------
|
---|
9 | // Viewer handling
|
---|
10 | //--------------------------------------------------------------------------------
|
---|
11 |
|
---|
12 | // Start the viewer if it's not already running
|
---|
13 | //--------------------------------------------------------------------------------
|
---|
14 | void EnsureViewerRunning( TPHelpInstance pHelpInstance )
|
---|
15 | {
|
---|
16 | HWND FocusFrame;
|
---|
17 | if ( ! pHelpInstance -> FViewerStarted )
|
---|
18 | {
|
---|
19 | // get focus frame
|
---|
20 | FocusFrame = GetTopLevelWindow( WinQueryFocus( HWND_DESKTOP ) );
|
---|
21 | // associate viewer with that.
|
---|
22 | if ( ViewHelpFile( pHelpInstance -> FHelpFileNames,
|
---|
23 | pHelpInstance -> FHelpWindowTitle,
|
---|
24 | pHelpInstance -> FHandle,
|
---|
25 | FocusFrame,
|
---|
26 | pHelpInstance -> Fhab ) )
|
---|
27 | {
|
---|
28 | pHelpInstance -> FViewerStarted = TRUE;
|
---|
29 | }
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | // Send a message to the viewer. If it hasn't
|
---|
34 | // started yet, then queue the message.
|
---|
35 | //--------------------------------------------------------------------------------
|
---|
36 | void PostViewerMessage( TPHelpInstance pHelpInstance,
|
---|
37 | ULONG msg,
|
---|
38 | MPARAM mp1,
|
---|
39 | MPARAM mp2 )
|
---|
40 | {
|
---|
41 | TQueuedViewerMessage ViewerMessage;
|
---|
42 | BOOL ok;
|
---|
43 |
|
---|
44 | LogEvent( "PostViewerMessage: %d", msg );
|
---|
45 |
|
---|
46 | if ( ! pHelpInstance -> FViewerStarted )
|
---|
47 | {
|
---|
48 | // Presumably an error
|
---|
49 | LogEvent( " Viewer not started, discarding" );
|
---|
50 | return;
|
---|
51 | }
|
---|
52 |
|
---|
53 | if ( pHelpInstance -> FViewerWindow == NULLHANDLE )
|
---|
54 | {
|
---|
55 | // viewer isn't ready yet, so queue the message
|
---|
56 | LogEvent( " Queueing message" );
|
---|
57 |
|
---|
58 | if ( pHelpInstance -> FViewerStartupMessagesCount >= MAX_VIEWER_STARTUP_MESSAGES )
|
---|
59 | {
|
---|
60 | LogEvent( " Out of queue space!" );
|
---|
61 | return;
|
---|
62 | }
|
---|
63 |
|
---|
64 | ViewerMessage.msg = msg;
|
---|
65 | ViewerMessage.mp1 = mp1;
|
---|
66 | ViewerMessage.mp2 = mp2;
|
---|
67 |
|
---|
68 | pHelpInstance -> FViewerStartupMessages[
|
---|
69 | pHelpInstance -> FViewerStartupMessagesCount ] = ViewerMessage;
|
---|
70 | pHelpInstance -> FViewerStartupMessagesCount ++;
|
---|
71 | return;
|
---|
72 | }
|
---|
73 |
|
---|
74 | ok = WinPostMsg( pHelpInstance -> FViewerWindow,
|
---|
75 | msg,
|
---|
76 | mp1,
|
---|
77 | mp2 );
|
---|
78 | if ( ok )
|
---|
79 | LogEvent( " Posted OK" );
|
---|
80 | else
|
---|
81 | LogEvent( " WinPostMsg failed" );
|
---|
82 | }
|
---|
83 |
|
---|
84 | // Post messages to the viewer that have been
|
---|
85 | // queued up waiting for it to complete startup
|
---|
86 | //--------------------------------------------------------------------------------
|
---|
87 | void PostQueuedViewerMessages( TPHelpInstance pHelpInstance )
|
---|
88 | {
|
---|
89 | int i;
|
---|
90 | TQueuedViewerMessage ViewerMessage;
|
---|
91 |
|
---|
92 | for ( i = 0; i < pHelpInstance -> FViewerStartupMessagesCount; i ++ )
|
---|
93 | {
|
---|
94 | ViewerMessage = pHelpInstance -> FViewerStartupMessages[ i ];
|
---|
95 |
|
---|
96 | LogEvent( " Posting queued message" );
|
---|
97 | WinSendMsg( pHelpInstance -> FViewerWindow,
|
---|
98 | ViewerMessage.msg,
|
---|
99 | ViewerMessage.mp1,
|
---|
100 | ViewerMessage.mp2 );
|
---|
101 |
|
---|
102 | }
|
---|
103 |
|
---|
104 | pHelpInstance -> FViewerStartupMessagesCount = 0;
|
---|
105 | }
|
---|
106 |
|
---|
107 | void CloseViewer( TPHelpInstance pHelpInstance )
|
---|
108 | {
|
---|
109 | LogEvent( " Close viewer" );
|
---|
110 | if ( pHelpInstance -> FViewerStarted )
|
---|
111 | {
|
---|
112 | LogEvent( " Viewer started, closing" );
|
---|
113 | PostViewerMessage( pHelpInstance, WM_CLOSE, 0, 0 );
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | //--------------------------------------------------------------------------------
|
---|
118 | // Window Association
|
---|
119 | //--------------------------------------------------------------------------------
|
---|
120 |
|
---|
121 | // Add the given window to the list of associated
|
---|
122 | // windows for this help instance
|
---|
123 | //--------------------------------------------------------------------------------
|
---|
124 | void AssociateWindow( TPHelpInstance pHelpInstance,
|
---|
125 | HWND hwnd )
|
---|
126 | {
|
---|
127 | LogEvent( "AssociateWindow: %8x with instance %8x",
|
---|
128 | hwnd,
|
---|
129 | pHelpInstance );
|
---|
130 |
|
---|
131 | if ( IsWindowAssociated( pHelpInstance, hwnd ) )
|
---|
132 | {
|
---|
133 | LogEvent( "Already associated" );
|
---|
134 | return;
|
---|
135 | }
|
---|
136 |
|
---|
137 | if ( pHelpInstance -> FNumApplicationWindows == 0 )
|
---|
138 | {
|
---|
139 | LogEvent( " First window being associated" );
|
---|
140 |
|
---|
141 | // this is the first window to be associated
|
---|
142 | /*
|
---|
143 | if ( pHelpInstance -> FActiveWindow == NULLHANDLE )
|
---|
144 | {
|
---|
145 | // an "active" window has not yet been set. Use this one
|
---|
146 | LogEvent( " Setting active window" );
|
---|
147 | pHelpInstance -> FActiveWindow = hwnd;
|
---|
148 | }
|
---|
149 | */
|
---|
150 | }
|
---|
151 |
|
---|
152 | // see if it will fit.
|
---|
153 | if ( pHelpInstance -> FNumApplicationWindows
|
---|
154 | >= pHelpInstance -> FMaxApplicationWindows )
|
---|
155 | {
|
---|
156 | // need more space.
|
---|
157 | if ( pHelpInstance -> FMaxApplicationWindows == 0 )
|
---|
158 | // first time
|
---|
159 | pHelpInstance -> FMaxApplicationWindows = 4;
|
---|
160 | else
|
---|
161 | // double space
|
---|
162 | pHelpInstance -> FMaxApplicationWindows *= 2;
|
---|
163 |
|
---|
164 | LogEvent( "AssociateWIndow: allocating list space to %d",
|
---|
165 | pHelpInstance -> FMaxApplicationWindows );
|
---|
166 |
|
---|
167 | // reallocate memory.
|
---|
168 | pHelpInstance -> FApplicationWindows
|
---|
169 | = (HWND*) realloc( pHelpInstance -> FApplicationWindows,
|
---|
170 | pHelpInstance -> FMaxApplicationWindows
|
---|
171 | * sizeof( HWND ) );
|
---|
172 | }
|
---|
173 |
|
---|
174 | pHelpInstance ->
|
---|
175 | FApplicationWindows[ pHelpInstance -> FNumApplicationWindows ]
|
---|
176 | = hwnd;
|
---|
177 |
|
---|
178 | pHelpInstance -> FNumApplicationWindows ++;
|
---|
179 |
|
---|
180 | LogEvent( "AssociateWindow: Now have %d windows associated",
|
---|
181 | pHelpInstance -> FNumApplicationWindows );
|
---|
182 | }
|
---|
183 |
|
---|
184 | // Removes the given window from the list of associated
|
---|
185 | // windows for this help instance (if present)
|
---|
186 | //--------------------------------------------------------------------------------
|
---|
187 | void RemoveAssociatedWindow( TPHelpInstance pHelpInstance,
|
---|
188 | HWND hwnd )
|
---|
189 | {
|
---|
190 | int i;
|
---|
191 | int j;
|
---|
192 |
|
---|
193 | i = 0;
|
---|
194 | while ( i < pHelpInstance -> FNumApplicationWindows )
|
---|
195 | {
|
---|
196 | if ( pHelpInstance -> FApplicationWindows[ i ] == hwnd )
|
---|
197 | {
|
---|
198 | // found one, copy remaining elements down by one
|
---|
199 | j = i;
|
---|
200 | while( j < pHelpInstance -> FNumApplicationWindows - 1 )
|
---|
201 | {
|
---|
202 | pHelpInstance -> FApplicationWindows[ j ]
|
---|
203 | = pHelpInstance -> FApplicationWindows[ j + 1 ];
|
---|
204 | j ++;
|
---|
205 | }
|
---|
206 | pHelpInstance -> FNumApplicationWindows --;
|
---|
207 | }
|
---|
208 | else
|
---|
209 | {
|
---|
210 | // not a match - next please
|
---|
211 | i ++;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | // Returns true if the given window is associated with
|
---|
217 | // the given help instance
|
---|
218 | //--------------------------------------------------------------------------------
|
---|
219 | BOOL IsWindowAssociated( TPHelpInstance pHelpInstance,
|
---|
220 | HWND hwnd )
|
---|
221 | {
|
---|
222 | int i;
|
---|
223 |
|
---|
224 | for ( i = 0; i < pHelpInstance -> FNumApplicationWindows; i ++ )
|
---|
225 | {
|
---|
226 | if ( pHelpInstance -> FApplicationWindows[ i ] == hwnd )
|
---|
227 | // found
|
---|
228 | return TRUE;
|
---|
229 | }
|
---|
230 | // not found
|
---|
231 | return FALSE;
|
---|
232 | }
|
---|
233 |
|
---|
234 | //--------------------------------------------------------------------------------
|
---|
235 | // Help instance
|
---|
236 | //--------------------------------------------------------------------------------
|
---|
237 |
|
---|
238 | // return the HelpInstance with the given handle
|
---|
239 | //--------------------------------------------------------------------------------
|
---|
240 | TPHelpInstance GetHelpInstance( HWND hwnd )
|
---|
241 | {
|
---|
242 | ULONG ulInstance;
|
---|
243 | ULONG ulMagicNumber;
|
---|
244 |
|
---|
245 | ulMagicNumber = WinQueryWindowULong( hwnd, QWL_HELPINSTANCEMAGICNUMBER );
|
---|
246 |
|
---|
247 | if ( ulMagicNumber != HELPINSTANCEMAGICNUMBER )
|
---|
248 | return NULL;
|
---|
249 |
|
---|
250 | ulInstance = WinQueryWindowULong( hwnd, QWL_HELPINSTANCEPTR );
|
---|
251 |
|
---|
252 | return (TPHelpInstance) ulInstance;
|
---|
253 | }
|
---|
254 |
|
---|
255 | //--------------------------------------------------------------------------------
|
---|
256 | void ReleaseHelpTable( TPHelpInstance pHelpInstance )
|
---|
257 | {
|
---|
258 | if ( pHelpInstance -> pHelpTable )
|
---|
259 | // no table loaded
|
---|
260 | return;
|
---|
261 |
|
---|
262 | if ( pHelpInstance -> HelpTableFromResource )
|
---|
263 | // free copy of resource
|
---|
264 | FreeHelpTable( & ( pHelpInstance -> pHelpTable ) );
|
---|
265 | }
|
---|
266 |
|
---|
267 | //--------------------------------------------------------------------------------
|
---|
268 | TPHelpInstance MakeNewHelpInstance()
|
---|
269 | {
|
---|
270 | TPHelpInstance pHelpInstance;
|
---|
271 |
|
---|
272 | pHelpInstance = (TPHelpInstance) malloc( sizeof( THelpInstance ) );
|
---|
273 | memset( pHelpInstance, 0, sizeof( THelpInstance ) );
|
---|
274 |
|
---|
275 | // window list will be allocated on first association
|
---|
276 | pHelpInstance -> FNumApplicationWindows = 0;
|
---|
277 | pHelpInstance -> FMaxApplicationWindows = 0;
|
---|
278 | pHelpInstance -> FApplicationWindows = NULL;
|
---|
279 |
|
---|
280 | pHelpInstance -> FActiveWindow = NULLHANDLE;
|
---|
281 |
|
---|
282 | return pHelpInstance;
|
---|
283 | }
|
---|
284 |
|
---|