source: trunk/gotcha.cpp@ 55

Last change on this file since 55 was 55, checked in by Gregg Young, 7 years ago

Update resources to get Gotcha! working in all languages; Reformat and update language tab in settings and about dialog; Clean up settings notebook formatting; Optimize turning PM print screen on and off. Fix screen region capture of its confirmation dialog. Move additional strings from code to the STRINGTABLE.

  • Property svn:eol-style set to native
File size: 11.8 KB
Line 
1/***
2 Main source of the Gotcha! screencapture program.
3 Copyright (C) 1998-2002 Thorsten Thielen <thth@c2226.de>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***/
19
20#define INCL_WINHOOKS
21
22#include "gotcha.h"
23#include "dll/gotchdll.h"
24#include "settings.h"
25#include "string.h"
26
27#include "io.h"
28#define INCL_EXCEPTQ_CLASS
29#define INCL_LOADEXCEPTQ
30#include "exceptq.h"
31
32HAB hab;
33HWND hwndFrame, hwndSnapshot;
34PFNWP OldFrameWP, wpOldButton;
35PSETTINGS pset;
36BOOL g_fIdle = FALSE, g_fSetPathName = FALSE;
37HWND g_hwndMenuSSW;
38HMODULE g_hmod = NULLHANDLE;
39Helper *g_phelp = NULL;
40
41#include "bitmap.cpp"
42#include "mainwin.cpp"
43#include "snapshot.cpp"
44#include "savebmp.cpp"
45
46// ** CheckCmdlineArgs **************************************************** /*FOLD00*/
47
48BOOL CheckCmdlineArgs (int argc, char *argv[])
49{
50 BOOL fAuto = FALSE;
51
52 for (USHORT i = 1; i < argc; i++)
53 {
54 // batch mode
55 if ((stricmp (argv[i], "-a") == 0))
56 {
57 if (pset->QuerySaveStyle () != FSS_FORCEFILE)
58 pset->SetFileSaveStyle (FSS_NUMFILES);
59 fAuto = TRUE;
60
61 if (i < argc-1)
62 if (argv[i+1][0] != '-')
63 {
64 i++;
65 pset->SetFileSaveStyle (FSS_NUMFILES);
66 pset->SetNumSaveDir (argv[i]);
67 }
68 }
69 // force saving to the given file name
70 else if ((stricmp (argv[i], "-f") == 0) && (i < argc-1))
71 {
72 if (argv[i+1][0] != '-')
73 {
74 i++;
75 pset->SetFileSaveStyle (FSS_FORCEFILE);
76 pset->SetForceSaveFile (argv[i]);
77 g_fSetPathName = TRUE;
78 }
79 }
80 // set to idle priority
81 else if (stricmp (argv[i], "-i") == 0)
82 {
83 g_fIdle = TRUE;
84 pset->SetFlag (SEI_IDLEPRIORITY, TRUE);
85 }
86 }
87
88 return fAuto;
89}
90
91// ** main **************************************************************** /*FOLD00*/
92
93int main (int argc, PSZ argv[])
94{
95 ScopedExceptqLoader sel;
96#ifdef _DOLOGDEBUG_
97 LogDebug( "Gotcha! start" );
98#endif
99#ifdef _DOLOGMEM_
100 LogMem("main", TRUE);
101#endif
102
103 // init system and msg queue
104 hab = WinInitialize (0);
105 HMQ hmq = WinCreateMsgQueue (hab, 0);
106
107 //DisplayError("DEBUG", "%d", Version());
108#ifdef _QUIET_
109 if ((Version() < 2))
110 {
111 DisplayError("GOTCHDLL.DLL Outdated",
112 "The file gotchdll.dll is is outdated. You should have "
113 "received a new version with the program, check for an "
114 "older version of gotchdll.dll in your LIBPATH. Is the "
115 "new gotchdll.dll in a directory in your LIBPATH?");
116 exit (0);
117 }
118 int rc;
119 HMTX hmtx = NULLHANDLE;
120 // running multiple instances orphans all but the first closed hook unloaded
121 rc = DosOpenMutexSem("\\SEM32\\GOTCHA", &hmtx);
122 if (!rc) {
123 DisplayError("GOTCHA QUIET Already running",
124 "Another instance of Gotcha Quiet is running. You can "
125 "open Gotcha Quiet's settings using the Alt+PrintScreen "
126 "key combination");
127 exit (0);
128 }
129 rc = DosCreateMutexSem("\\SEM32\\GOTCHA", &hmtx, 0, FALSE);
130 if (rc) {
131 DisplayError("Semaphore creation failed",
132 "Try restarting Gotcha Quiet");
133 exit (0);
134 }
135#endif
136
137 // register our window classes
138 WinRegisterClass (hab, "thth.wc.gotcha.main", WindowProcedure, 0L,
139 sizeof (ULONG)*2L);
140 WinRegisterClass (hab, "thth.wc.gotcha.snapshot", wpSnapshot,
141 CS_SIZEREDRAW, sizeof (ULONG)*2L);
142
143 // load the settings
144 pset = new SETTINGS;
145
146 pset->idleSetInIni = pset->QueryFlag(SEI_IDLEPRIORITY);
147 pset->saveStyle = pset->QuerySaveStyle ();
148 pset->pNumSaveDir = pset->QueryNumSaveDir ();
149 pset->pForceSaveFile = pset->QueryForceSaveFile();
150 pset->bSerialCapture = pset->SerialCapture ();
151
152#ifdef _QUIET_
153 bool fPrtScr = FALSE;
154 // always turn it off while running
155 // WinSet does not effect the ini setting
156 WinSetSysValue(HWND_DESKTOP, SV_PRINTSCREEN , fPrtScr);
157#endif
158 // check cmd line args and if "-a" found take screenshot and exit
159 if (CheckCmdlineArgs (argc, argv))
160 {
161 CaptureWindow (HWND_DESKTOP, HWND_DESKTOP, NULL, TRUE);
162 pset->SetFileSaveStyle(pset->saveStyle);
163 pset->SetNumSaveDir (pset->pNumSaveDir);
164 delete pset;
165 WinDestroyMsgQueue (hmq);
166 WinTerminate (hab);
167 exit (0);
168 }
169
170 SetIdlePriority(pset->QueryFlag(SEI_IDLEPRIORITY));
171
172 // create the windows
173 hwndFrame = CreateMainWindow ();
174 hwndSnapshot = CreateSnapshotWindow ();
175
176 SWP swp;
177 USHORT us[7];
178
179 // position main window
180 pset->QueryWindowData (&swp, us, FALSE);
181 WinSetWindowPos (hwndFrame, HWND_DESKTOP, swp.x,swp.y, 0,0,
182 SWP_SHOW | SWP_MOVE);
183
184
185#ifdef _QUIET_
186 InitDLL (hab, hwndFrame);
187 StartInputHook ();
188#endif
189
190 // position snapshot window
191 pset->QueryWindowData (&swp, us);
192
193 // size, activate & show window
194 WinSetWindowPos (hwndSnapshot, HWND_DESKTOP, swp.x,swp.y, swp.cx,swp.cy,
195 SWP_SHOW | SWP_SIZE | SWP_MOVE);
196 if (! pset->SnapshotWindow ())
197 WinShowWindow (hwndSnapshot, FALSE);
198 else
199 WinShowWindow (hwndSnapshot, TRUE);
200
201 WinSetWindowPos (hwndFrame, NULLHANDLE, 0,0, 0,0, SWP_SHOW);
202 WinSetWindowPos (WinWindowFromID (hwndFrame, FID_CLIENT), NULLHANDLE,
203 0,0, 0,0, SWP_SHOW);
204
205 g_phelp = new Helper(hwndFrame);
206
207
208 // do the main msg loop
209 QMSG qmsg;
210 while (WinGetMsg (hab, &qmsg, 0L, 0, 0))
211 WinDispatchMsg (hab, &qmsg);
212
213
214 // Set priorty to unless user set regular since running -q sets it to idle in the ini
215 if (g_fIdle) {
216 pset->SetFlag (SEI_IDLEPRIORITY, pset->idleSetInIni);
217 }
218 // Don't change the ini stored paths if the paths were set from the command line
219 if (g_fSetPathName) {
220 pset->SetFileSaveStyle(pset->saveStyle);
221 pset->SetForceSaveFile (pset->pForceSaveFile);
222 pset->SetNumSaveDir (pset->pNumSaveDir);
223 }
224 // save size, etc. of snapshot window
225 WinQueryWindowPos (hwndSnapshot, &swp);
226 pset->SetWindowData (&swp);
227
228 // save size, etc. of main window
229 WinQueryWindowPos (hwndFrame, &swp);
230 pset->SetWindowData (&swp, FALSE);
231
232 // goodbye windows!
233 WinDestroyWindow (hwndSnapshot);
234 WinDestroyWindow (hwndFrame);
235
236#ifdef _QUIET_
237 // Reset to user PM print screen choice
238 ULONG ulDataSize = 0;
239 rc = PrfQueryProfileSize(HINI_USERPROFILE, "PM_ControlPanel", "PrintScreen",
240 &ulDataSize );
241 rc = PrfQueryProfileData(HINI_USERPROFILE, "PM_ControlPanel", "PrintScreen",
242 &fPrtScr, &ulDataSize);
243 if (!rc) // Print screen is on by default (no ini entry)
244 fPrtScr = TRUE;
245 WinSetSysValue(HWND_DESKTOP, SV_PRINTSCREEN , fPrtScr);
246 StopInputHook ();
247 DosCloseMutexSem(hmtx);
248#endif
249
250 delete g_phelp;
251 delete pset;
252
253 WinDestroyMsgQueue (hmq);
254 WinTerminate (hab);
255
256#ifdef _DOLOGMEM_
257 LogMem("main", FALSE);
258#endif
259#ifdef _DOLOGDEBUG_
260 LogDebug( "Gotcha! end" );
261#endif
262}
263
264// ** DisplayError ******************************************************** /*FOLD00*/
265
266VOID DisplayError (PSZ pszTitle, PSZ psz, ...)
267{
268 CHAR dstring[401];
269 va_list valst;
270
271 va_start (valst, psz);
272 vsnprintf (dstring, 401, psz, valst);
273 va_end (valst);
274
275 WinMessageBox (HWND_DESKTOP, WinQueryActiveWindow (HWND_DESKTOP), dstring,
276 pszTitle, 0, MB_OK | MB_ERROR | MB_APPLMODAL | MB_MOVEABLE);
277}
278
279// saymsg2 was adapted from code in FM/2
280APIRET saymsg2(int DefaultButton, HWND hwnd, PCSZ pszTitle, PCSZ pszFmt, ...)
281{
282 ULONG i;
283 APIRET rc;
284 CHAR szMsg[4096];
285 va_list va;
286 MB2INFO *pmbInfo;
287 MB2D mb2dBut[3];
288 ULONG ulInfoSize = (sizeof(MB2INFO) + (sizeof(MB2D) * 2));
289
290 va_start(va, pszFmt);
291 szMsg[sizeof(szMsg) - 1] = 0;
292 vsprintf(szMsg, pszFmt, va);
293 va_end(va);
294
295 if (szMsg[sizeof(szMsg) - 1]) {
296 fprintf(stderr, "Buffer overflow in saymsg2 - need %u bytes\n", strlen(szMsg) + 1);
297 fflush(stderr);
298 }
299
300 memset(mb2dBut, 0, sizeof(MB2D) * 3);
301 strcpy(mb2dBut[0].achText,RSTR(IDS_OK));
302 strcpy(mb2dBut[1].achText,RSTR(IDS_CANCEL));
303 strcpy(mb2dBut[2].achText,RSTR(IDS_SETTINGS));
304 mb2dBut[0].idButton = 1;
305 mb2dBut[1].idButton = 2;
306 mb2dBut[2].idButton = 3;
307 if (DefaultButton)
308 mb2dBut[DefaultButton - 1].flStyle = BS_DEFAULT;
309 pmbInfo = (MB2INFO *) malloc(ulInfoSize);
310 memset(pmbInfo, 0, ulInfoSize);
311 if (pmbInfo) {
312 pmbInfo->cb = ulInfoSize;
313 pmbInfo->hIcon = 0;
314 pmbInfo->cButtons = 3;
315 pmbInfo->flStyle = MB_MOVEABLE | MB_ICONQUESTION ;
316 pmbInfo->hwndNotify = NULLHANDLE;
317 for (i = 0; i < 3; i++) {
318 memcpy( pmbInfo->mb2d+i , mb2dBut+i , sizeof(MB2D));
319 }
320 rc = WinMessageBox2(HWND_DESKTOP, hwnd,
321 szMsg, pszTitle, SM2_DIALOG,
322 pmbInfo);
323 WinSetFocus(HWND_DESKTOP, SM2_DIALOG);
324 free(pmbInfo);
325 return rc;
326 }
327 return MBID_ERROR;
328}
329// ** AddSysMenuItem ****************************************************** /*FOLD00*/
330
331VOID AddSysMenuItem (HWND hwndFrame, MENUITEM *Item, PSZ Text)
332{
333 HWND hwndSysMenu = WinWindowFromID (hwndFrame, FID_SYSMENU);
334 USHORT idSysMenu = SHORT1FROMMR (WinSendMsg (hwndSysMenu,
335 MM_ITEMIDFROMPOSITION, NULL,
336 NULL ));
337 MENUITEM miSysMenu;
338 WinSendMsg (hwndSysMenu, MM_QUERYITEM,
339 MPFROM2SHORT (idSysMenu,FALSE), MPFROMP(&miSysMenu));
340
341 HWND hwndSysSubMenu = miSysMenu.hwndSubMenu;
342
343 WinSendMsg (hwndSysSubMenu, MM_INSERTITEM, MPFROMP(Item), MPFROMP(Text));
344}
345
346// ** DoCountdown ********************************************************* /*fold00*/
347
348VOID DoCountdown (ULONG ul)
349{
350 if (ul > 10)
351 DosBeep (4000L-3000L, 20);
352 else
353 DosBeep (4000L-ul*300L, 20);
354}
355
356// ** SetIdlePriority **************************************************** /*FOLD00*/
357
358VOID SetIdlePriority (BOOL f)
359{
360#ifndef _QUEIT_
361 if (f)
362 DosSetPriority(PRTYS_PROCESS, PRTYC_IDLETIME, 0, 0);
363 else
364 DosSetPriority(PRTYS_PROCESS, PRTYC_REGULAR, 0, 0);
365#else
366 DosSetPriority(PRTYS_PROCESS, PRTYC_FOREGROUNDSERVER, 0, 0);
367#endif
368}
369
370// ***********************************************************************
371
372#ifdef _DOLOGMEM_
373VOID LogMem (PSZ psz, BOOL f)
374{
375 FILE *pf = fopen("gotcha.mem","ab");
376 static ULONG TotalPhysicalMemory, ul = 0;
377 ULONG i;
378 if (!f)
379 ul--;
380 DosQuerySysInfo(QSV_TOTAVAILMEM, QSV_TOTAVAILMEM, &TotalPhysicalMemory,
381 sizeof(TotalPhysicalMemory));
382 for (i = 0; i < ul; i++)
383 fputs(" ", pf);
384 fprintf(pf,"%10ld (%s)\n", TotalPhysicalMemory, psz);
385 if (f)
386 ul++;
387 fclose(pf);
388}
389#endif
390
391#ifdef _DOLOGDEBUG_
392VOID LogDebug( PSZ psz, ... )
393{
394 CHAR dstring[401];
395 va_list valst;
396
397 va_start (valst, psz);
398 vsnprintf (dstring, 401, psz, valst);
399 va_end (valst);
400
401 FILE *pf = fopen( "gotcha.log", "ab" );
402 fprintf( pf, "%s\n", dstring );
403 fclose( pf );
404}
405#endif
406
407// ***********************************************************************
Note: See TracBrowser for help on using the repository browser.