source: trunk/dll/notify.c@ 1211

Last change on this file since 1211 was 1211, checked in by John Small, 17 years ago

Ticket 187: Move data declarations/definitions out of fm3dll.h

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.6 KB
RevLine 
[356]1/***********************************************************************
2
3 $Id: notify.c 1211 2008-09-13 06:52:08Z jbs $
4
[1002]5 Thread notes window and popup notification status line
[356]6
7 Copyright (c) 1993-98 M. Kimes
[1002]8 Copyright (c) 2006, 2008 Steven H.Levine
[356]9
10 17 Jul 06 SHL Use Win_Error
11 22 Jul 06 SHL Check more run time errors
[593]12 30 Mar 07 GKY Remove GetPString for window class names
[775]13 06 Aug 07 GKY Reduce DosSleep times (ticket 148)
[793]14 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
[1002]15 16 Apr 08 SHL Comment and clean up logic
[356]16
17***********************************************************************/
18
[2]19#include <stdlib.h>
20#include <string.h>
21#include <ctype.h>
[1163]22#include <stddef.h> // _threadid
23#include <process.h> // _beginthread
[356]24
[907]25#define INCL_DOS
26#define INCL_WIN
27#define INCL_GPI
[1163]28#define INCL_LONGLONG // dircnrs.h
[907]29
[1211]30#include "fm3dll.h"
31#include "collect.h" // Data declaration(s)
32#include "grep.h" // Data declaration(s)
33#include "notebook.h" // Data declaration(s)
34#include "init.h" // Data declaration(s)
[2]35#include "fm3dlg.h"
36#include "fm3str.h"
[1163]37#include "errutil.h" // Dos_Error...
38#include "strutil.h" // GetPString
39#include "notify.h"
[1188]40#include "presparm.h" // SetPresParams
[1211]41#include "mainwnd.h" // Data declaration(s)
[1188]42#include "wrappers.h" // xmalloc
43#include "misc.h" // PostMsg
[1038]44#include "fortify.h"
[2]45
46#pragma data_seg(DATA1)
[356]47
[1211]48// Data definitions
[356]49static PSZ pszSrcFile = __FILE__;
[1163]50static volatile HWND hwndNotify; // 16 Apr 08 SHL
[2]51
[1211]52#pragma data_seg(GLOBAL1)
53BOOL fThreadNotes;
54
[1163]55VOID StartNotes(CHAR * s);
56
[1002]57/**
58 * Popup notification message window procedure
59 * Display timed message over status line
60 */
61
[551]62MRESULT EXPENTRY NotifyWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
[356]63{
[2]64 static ULONG showing = 0;
65
[551]66 switch (msg) {
67 case WM_CREATE:
68 showing++;
69 {
[1002]70 MRESULT rc = PFNWPStatic(hwnd, msg, mp1, mp2);
[2]71
[551]72 if (!WinStartTimer(WinQueryAnchorBlock(hwnd), hwnd, ID_TIMER2, 5000)) {
[1163]73 Win_Error(hwnd, hwnd, pszSrcFile, __LINE__, "WinStartTimer");
74 WinDestroyWindow(hwnd);
[551]75 }
76 else {
[2]77
[1163]78 RGB2 rgb2F, rgb2;
[2]79
[1163]80 memset(&rgb2F, 0, sizeof(RGB2));
81 rgb2F.bRed = (BYTE)65;
82 rgb2.bRed = rgb2.bGreen = rgb2.bBlue = (BYTE)255;
83 rgb2.fcOptions = 0;
84 SetPresParams(hwnd, &rgb2, &rgb2F, &rgb2, GetPString(IDS_8HELVTEXT));
85 if (hwndMain) {
86 if (hwndStatus)
87 WinShowWindow(hwndStatus, FALSE);
88 if (hwndStatus2)
89 WinShowWindow(hwndStatus2, FALSE);
90 }
91 PostMsg(hwnd, UM_SETUP, MPVOID, MPVOID);
[2]92 }
[551]93 return rc;
94 }
[2]95
[551]96 case UM_SETUP:
97 WinSetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_ZORDER | SWP_SHOW);
98 WinInvalidateRect(hwnd, NULL, FALSE);
99 return 0;
[2]100
[551]101 case WM_SETFOCUS:
102 if (mp2)
103 PostMsg(hwnd, UM_REPLACEFOCUS, mp1, MPVOID);
104 break;
[2]105
[551]106 case UM_REPLACEFOCUS:
107 if (mp1)
108 WinSetFocus(HWND_DESKTOP, (HWND) mp1);
109 PostMsg(hwnd, UM_SETUP, MPVOID, MPVOID);
110 return 0;
[2]111
[551]112 case WM_PAINT:
113 {
114 SWP swp;
115 POINTL ptl;
[2]116
[1002]117 MRESULT mr = PFNWPStatic(hwnd, msg, mp1, mp2);
118 HPS hps = WinGetPS(hwnd);
[551]119 if (hps) {
[1163]120 if (WinQueryWindowPos(hwnd, &swp)) {
121 ptl.x = 0;
122 ptl.y = 0;
123 GpiMove(hps, &ptl);
124 GpiSetColor(hps, CLR_RED);
125 ptl.x = swp.cx - 1;
126 ptl.y = swp.cy - 1;
127 GpiBox(hps, DRO_OUTLINE, &ptl, 2, 2);
128 }
129 WinReleasePS(hwnd);
[2]130 }
[551]131 return mr;
132 }
[2]133
[551]134 case WM_BUTTON1DOWN:
135 case WM_BUTTON2DOWN:
136 case WM_BUTTON3DOWN:
137 case WM_TIMER:
138 case WM_CLOSE:
139 WinStopTimer(WinQueryAnchorBlock(hwnd), hwnd, ID_TIMER2);
140 WinDestroyWindow(hwnd);
141 return 0;
[2]142
[551]143 case WM_DESTROY:
144 showing--;
145 if (!showing && hwndMain) {
146 if (hwndStatus)
[1163]147 WinShowWindow(hwndStatus, TRUE);
[551]148 if (hwndStatus2)
[1163]149 WinShowWindow(hwndStatus2, TRUE);
[551]150 }
151 break;
[2]152 }
[551]153 return PFNWPStatic(hwnd, msg, mp1, mp2);
[2]154}
155
[1002]156/**
157 * Display timed notification window over status line
158 */
159
[551]160HWND DoNotify(char *str)
[356]161{
[551]162 char *p;
163 HWND hwnd = (HWND) 0, hwndP;
164 LONG x, y, cx, cy;
165 SWP swp, swpS, swpS2;
[2]166 static USHORT id = NOTE_FRAME;
167
[551]168 if (str && *str) {
[1002]169 // figure out what size the window should be and where it should be
170 hwndP = hwndMain ? WinQueryWindow(hwndMain, QW_PARENT) : HWND_DESKTOP;
[551]171 WinQueryWindowPos(hwndP, &swp);
172 if (hwndStatus)
173 WinQueryWindowPos(hwndStatus, &swpS);
174 if (hwndStatus2)
175 WinQueryWindowPos(hwndStatus2, &swpS2);
[1002]176 x = hwndMain ? (hwndStatus ? swpS.x - 1 :
[1163]177 WinQuerySysValue(HWND_DESKTOP, SV_CXSIZEBORDER)) : 0;
[1002]178 y = hwndMain ? (hwndStatus ? swpS.y - 1 :
[1163]179 WinQuerySysValue(HWND_DESKTOP, SV_CYSIZEBORDER)) : 0;
[551]180 if (hwndMain && hwndStatus) {
181 if (hwndStatus2)
[1163]182 cx = swpS2.cx + swpS.cx + 8;
[2]183 else
[1163]184 cx = swpS.cx + 6;
[2]185 }
186 else
187 cx = (swp.cx - ((x * 2) + 4));
[1002]188 cy = hwndMain ? (hwndStatus ? swpS.cy + 2 : 28) : 28;
[2]189
[1002]190 // pretty-up the note by putting on a leading space
[551]191 if (*str != ' ') {
192 p = xmalloc(strlen(str) + 2, pszSrcFile, __LINE__);
193 if (!p)
[1163]194 p = str;
[2]195 else {
[1163]196 strcpy(p + 1, str);
197 *p = ' ';
[2]198 }
199 }
200 else
201 p = str;
202
203 hwnd = WinCreateWindow(hwndP,
[1163]204 WC_ERRORWND,
205 p,
206 SS_TEXT | DT_LEFT | DT_VCENTER | WS_VISIBLE,
207 x, y, cx, cy, hwndP, HWND_TOP, id++, NULL, NULL);
[377]208 if (!hwndP)
[551]209 Win_Error2(hwndP, hwndP, pszSrcFile, __LINE__, IDS_WINCREATEWINDOW);
[377]210
[551]211 if (p != str)
[1039]212 free(p);
[551]213 if (id > NOTE_MAX)
[2]214 id = NOTE_FRAME;
215 }
216
217 AddNote(str);
218
219 return hwnd;
220}
221
[1002]222/**
223 * Add message to thread notes window
224 */
225
[551]226HWND Notify(char *str)
[356]227{
[1002]228 return (HWND)WinSendMsg(MainObjectHwnd, UM_NOTIFY, MPFROMP(str), MPVOID);
[2]229}
230
[1002]231/**
232 * Add error message to thread notes window
233 */
234
[551]235VOID NotifyError(CHAR * filename, APIRET status)
[356]236{
[2]237 CHAR errortext[512];
238
[551]239 if (!filename)
[2]240 return;
[551]241 sprintf(errortext, GetPString(IDS_ERRORACCESSTEXT), status, filename);
242 if (toupper(*filename) > 'B') {
243 if (status == 21)
244 strcat(errortext, GetPString(IDS_EMPTYREMOVETEXT));
245 else if (status == 27)
[2]246 strcat(errortext, GetPString(IDS_CDMUSICTEXT));
[551]247 else if (status == 19)
248 strcat(errortext, GetPString(IDS_WRITEPROTECTTEXT));
249 else if (status == 108)
250 strcat(errortext, GetPString(IDS_DRIVELOCKEDTEXT));
[2]251 }
252 else {
[551]253 if (status == 21)
254 strcat(errortext, GetPString(IDS_EMPTYFLOPPYTEXT));
255 else if (status == 27)
256 strcat(errortext, GetPString(IDS_UNFORMATEDTEXT));
257 else if (status == 107)
[2]258 sprintf(&errortext[strlen(errortext)],
[1163]259 GetPString(IDS_PHANTOMTEXT), toupper(*filename));
[551]260 else if (status == 19)
261 strcat(errortext, GetPString(IDS_DISKWRITEPROTEXTTEXT));
262 else if (status == 108)
263 strcat(errortext, GetPString(IDS_DISKLOCKEDTEXT));
[2]264 }
[551]265 DosBeep(250, 10);
266 DosBeep(500, 10);
267 DosBeep(250, 10);
268 DosBeep(500, 10);
[2]269 Notify(errortext);
270}
271
[1002]272/**
273 * Thread notes dialog window dialog procedure
274 */
275
[551]276MRESULT EXPENTRY NoteWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
[356]277{
[551]278 static HPOINTER hptrIcon = (HPOINTER) 0;
[2]279
[551]280 switch (msg) {
281 case WM_INITDLG:
[1002]282 if (hwndNotify != (HWND)0) {
283 // Already have notes dialog - pass message on
[551]284 if (mp2) {
[1163]285 WinSendDlgItemMsg(hwndNotify,
286 NOTE_LISTBOX,
287 LM_INSERTITEM, MPFROM2SHORT(LIT_END, 0), mp2);
288 PostMsg(hwndNotify, UM_NOTIFY, MPVOID, MPVOID);
289 free((CHAR *)mp2);
[2]290 }
[551]291 WinDismissDlg(hwnd, 0);
292 break;
293 }
294 hwndNotify = hwnd;
295 fThreadNotes = FALSE;
[1002]296 // Remember showing
[551]297 {
298 BOOL dummy = TRUE;
299 PrfWriteProfileData(fmprof,
[1163]300 FM3Str, "ThreadNotes", &dummy, sizeof(BOOL));
[551]301 }
302 if (mp2) {
303 WinSendDlgItemMsg(hwnd,
[1163]304 NOTE_LISTBOX,
305 LM_INSERTITEM, MPFROM2SHORT(LIT_END, 0), mp2);
[1039]306 free((CHAR *)mp2);
[551]307 }
[1002]308
[551]309 {
[1002]310 // Return focus
311 HWND hwndActive = WinQueryActiveWindow(HWND_DESKTOP);
[551]312 PostMsg(hwnd, UM_FOCUSME, MPFROMLONG(hwndActive), MPVOID);
313 }
[1002]314
[551]315 hptrIcon = WinLoadPointer(HWND_DESKTOP, FM3ModHandle, NOTE_FRAME);
316 if (hptrIcon)
317 WinDefDlgProc(hwnd, WM_SETICON, MPFROMLONG(hptrIcon), MPFROMLONG(0L));
318 break;
[2]319
[551]320 case UM_FOCUSME:
321 {
322 ULONG size = sizeof(SWP),
[1163]323 fl = SWP_ZORDER | SWP_FOCUSDEACTIVATE | SWP_SHOW;
[551]324 SWP swp;
[2]325
[551]326 if (PrfQueryProfileData(fmprof,
[1163]327 FM3Str, "NoteWndSwp", (PVOID) & swp, &size)) {
328 if (swp.fl & (SWP_HIDE | SWP_MINIMIZE)) {
329 fl |= SWP_MINIMIZE;
330 fl &= (~SWP_SHOW);
331 }
332 else
333 fl |= (SWP_MOVE | SWP_SIZE);
[2]334 }
[551]335 WinSetWindowPos(hwnd, HWND_BOTTOM, swp.x, swp.y, swp.cx, swp.cy, fl);
336 if (fl & SWP_MINIMIZE) {
[1163]337 WinSetWindowUShort(hwnd, QWS_XRESTORE, (USHORT) swp.x);
338 WinSetWindowUShort(hwnd, QWS_CXRESTORE, (USHORT) swp.cx);
339 WinSetWindowUShort(hwnd, QWS_YRESTORE, (USHORT) swp.y);
340 WinSetWindowUShort(hwnd, QWS_CYRESTORE, (USHORT) swp.cy);
[551]341 }
342 }
343 if (mp1)
344 WinSetActiveWindow(HWND_DESKTOP, (HWND) mp1);
345 return 0;
[2]346
[551]347 case UM_STRETCH:
348 {
349 SWP swp;
350 LONG titl, szbx, szby;
[2]351
[551]352 WinQueryWindowPos(hwnd, &swp);
353 if (!(swp.fl & (SWP_MINIMIZE | SWP_HIDE))) {
[1163]354 szbx = SysVal(SV_CXSIZEBORDER);
355 szby = SysVal(SV_CYSIZEBORDER);
356 titl = SysVal(SV_CYTITLEBAR);
357 WinSetWindowPos(WinWindowFromID(hwnd, NOTE_LISTBOX),
358 HWND_TOP,
359 szbx,
360 szby,
361 swp.cx - (szbx * 2L),
362 (swp.cy - titl) - (szby * 2L), SWP_MOVE | SWP_SIZE);
[2]363 }
[551]364 if (!(swp.fl & SWP_MAXIMIZE)) {
[1163]365 if (swp.fl & (SWP_MINIMIZE | SWP_HIDE)) {
366 swp.x = WinQueryWindowUShort(hwnd, QWS_XRESTORE);
367 swp.y = WinQueryWindowUShort(hwnd, QWS_YRESTORE);
368 swp.cx = WinQueryWindowUShort(hwnd, QWS_CXRESTORE);
369 swp.cy = WinQueryWindowUShort(hwnd, QWS_CYRESTORE);
370 }
371 PrfWriteProfileData(fmprof,
372 FM3Str, "NoteWndSwp", (PVOID) & swp, sizeof(SWP));
[551]373 }
374 }
375 return 0;
[2]376
[551]377 case WM_ADJUSTWINDOWPOS:
378 PostMsg(hwnd, UM_STRETCH, MPVOID, MPVOID);
379 break;
[2]380
[551]381 case UM_CONTAINER_FILLED:
382 {
[1002]383 SHORT y;
384 SHORT x = (SHORT)WinSendDlgItemMsg(hwnd,
[1163]385 NOTE_LISTBOX,
386 LM_QUERYITEMCOUNT, MPVOID, MPVOID);
[551]387 if (x > 60) {
[1163]388 for (y = 0; y < x - 50; y++) {
389 WinSendDlgItemMsg(hwnd,
390 NOTE_LISTBOX,
391 LM_DELETEITEM, MPFROMSHORT(y), MPVOID);
392 }
[2]393 }
[551]394 }
395 return 0;
[2]396
[551]397 case UM_NOTIFY:
398 {
[1002]399 SHORT x = (SHORT) WinSendDlgItemMsg(hwnd,
[1163]400 NOTE_LISTBOX,
401 LM_QUERYITEMCOUNT, MPVOID, MPVOID);
[551]402 if (x > 0)
[1163]403 WinSendDlgItemMsg(hwnd,
404 NOTE_LISTBOX,
405 LM_SETTOPINDEX, MPFROMSHORT(x), MPVOID);
[551]406 }
407 return 0;
[2]408
[551]409 case UM_SHOWME:
410 WinSetWindowPos(hwnd,
[1163]411 HWND_TOP,
412 0,
413 0,
414 0, 0, SWP_SHOW | SWP_RESTORE | SWP_ZORDER | SWP_ACTIVATE);
[551]415 return 0;
[2]416
[551]417 case WM_COMMAND:
418 return 0;
[2]419
[551]420 case WM_CLOSE:
421 WinDismissDlg(hwnd, 0);
422 return 0;
[2]423
[551]424 case WM_DESTROY:
425 if (hwndNotify == hwnd) {
426 fThreadNotes = FALSE;
427 PrfWriteProfileData(fmprof,
[1163]428 FM3Str, "ThreadNotes", &fThreadNotes, sizeof(BOOL));
[551]429 hwndNotify = (HWND) 0;
430 }
431 if (hptrIcon)
432 WinDestroyPointer(hptrIcon);
433 if (!PostMsg((HWND) 0, WM_QUIT, MPVOID, MPVOID))
434 WinSendMsg((HWND) 0, WM_QUIT, MPVOID, MPVOID);
435 break;
[2]436 }
[551]437 return WinDefDlgProc(hwnd, msg, mp1, mp2);
[2]438}
439
[1002]440/**
441 * Thread notes dialog window thread
442 */
443
[551]444static VOID NoteThread(VOID * args)
[356]445{
[1002]446 HAB hab = WinInitialize(0);
[1038]447# ifdef FORTIFY
448 Fortify_EnterScope();
[1063]449# endif
[1002]450 if (hab) {
451 HMQ hmq = WinCreateMsgQueue(hab, 0);
452 if (hmq) {
[551]453 if (!hwndNotify)
[1163]454 WinDlgBox(HWND_DESKTOP,
455 HWND_DESKTOP,
456 NoteWndProc, FM3ModHandle, NOTE_FRAME, (CHAR *)args);
[1002]457 WinDestroyMsgQueue(hmq);
[2]458 }
[1002]459 WinTerminate(hab);
[2]460 }
[1038]461# ifdef FORTIFY
[1063]462 Fortify_LeaveScope();
463# endif
[2]464}
465
[1002]466/**
467 * Start thread notes dialog window thread
468 */
469
[551]470VOID StartNotes(CHAR * note)
[356]471{
472 if (!hwndNotify) {
[551]473 if (_beginthread(NoteThread, NULL, 65536, (PVOID) note) == -1)
[1002]474 Runtime_Error2(pszSrcFile, __LINE__, IDS_COULDNTSTARTTHREADTEXT);
475 else {
476 USHORT i;
477 for (i = 0; !hwndNotify && i < 10; i++)
[1163]478 DosSleep(10);
[1002]479 if (!hwndNotify)
[1163]480 Runtime_Error(pszSrcFile, __LINE__, "Can not create Notify window");
[1002]481 }
[2]482 }
483}
484
[1002]485/**
486 * Add note to thread notes window or popup status window
487 */
488
[551]489BOOL AddNote(CHAR * note)
[356]490{
[551]491 CHAR *s, *p;
492 BOOL once = FALSE, ret = FALSE;
[2]493
[551]494 if ((fThreadNotes || hwndNotify) && note && *note) {
[2]495 p = note;
[551]496 while (*p == ' ')
[2]497 p++;
[551]498 if (*p) {
499 if (!hwndNotify) {
[1163]500 fThreadNotes = FALSE;
501 StartNotes(NULL);
[2]502 }
[551]503 if (hwndNotify) {
[1163]504 s = xmalloc(strlen(p) + 14, pszSrcFile, __LINE__);
505 if (s) {
506 sprintf(s, "%08lx %s", _threadid, p);
507 while (!once) {
508 if ((SHORT) WinSendDlgItemMsg(hwndNotify,
509 NOTE_LISTBOX,
510 LM_INSERTITEM,
511 MPFROM2SHORT(LIT_END, 0),
512 MPFROMP(s)) >= 0) {
513 ret = TRUE;
514 PostMsg(hwndNotify, UM_NOTIFY, MPVOID, MPVOID);
515 break;
516 }
517 PostMsg(hwndNotify, UM_CONTAINER_FILLED, MPVOID, MPVOID);
518 once = TRUE;
[1039]519 }
520 free(s);
[1163]521 }
[2]522 }
523 }
524 }
525 return ret;
526}
527
[1002]528/**
529 * Close thread notes window
530 */
531
[551]532VOID EndNote(VOID)
[356]533{
[551]534 if (hwndNotify)
535 if (!PostMsg(hwndNotify, WM_CLOSE, MPVOID, MPVOID))
536 WinSendMsg(hwndNotify, WM_CLOSE, MPVOID, MPVOID);
[2]537}
538
[1002]539/**
540 * Pop up thread notes window
541 */
542
[551]543VOID ShowNote(VOID)
[356]544{
[551]545 if (!hwndNotify)
[2]546 StartNotes(NULL);
[551]547 if (hwndNotify)
548 PostMsg(hwndNotify, UM_SHOWME, MPVOID, MPVOID);
[2]549}
550
[1002]551/**
552 * Hide thread notes window
553 */
554
[551]555VOID HideNote(VOID)
[356]556{
[551]557 if (hwndNotify)
[2]558 WinSetWindowPos(hwndNotify,
[1163]559 HWND_BOTTOM,
560 0,
561 0, 0, 0, SWP_MINIMIZE | SWP_ZORDER | SWP_FOCUSDEACTIVATE);
[2]562}
[793]563
[1038]564#pragma alloc_text(NOTIFY,Notify,NotifyWndProc,StartNotify,NotifyError)
[793]565#pragma alloc_text(NOTIFY2,AddNote,NoteThread,NoteWndProc)
566#pragma alloc_text(NOTIFY3,StartNotes,EndNote,HideNote,ShowNote)
Note: See TracBrowser for help on using the repository browser.