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