1 /*   vibwndws.c
2 * ===========================================================================
3 *
4 *                            PUBLIC DOMAIN NOTICE
5 *            National Center for Biotechnology Information (NCBI)
6 *
7 *  This software/database is a "United States Government Work" under the
8 *  terms of the United States Copyright Act.  It was written as part of
9 *  the author's official duties as a United States Government employee and
10 *  thus cannot be copyrighted.  This software/database is freely available
11 *  to the public for use. The National Library of Medicine and the U.S.
12 *  Government do not place any restriction on its use or reproduction.
13 *  We would, however, appreciate having the NCBI and the author cited in
14 *  any work or product based on this material
15 *
16 *  Although all reasonable efforts have been taken to ensure the accuracy
17 *  and reliability of the software and data, the NLM and the U.S.
18 *  Government do not and cannot warrant the performance or results that
19 *  may be obtained by using this software or data. The NLM and the U.S.
20 *  Government disclaim all warranties, express or implied, including
21 *  warranties of performance, merchantability or fitness for any particular
22 *  purpose.
23 *
24 * ===========================================================================
25 *
26 * File Name:  vibwndws.c
27 *
28 * Author:  Jonathan Kans
29 *
30 * Version Creation Date:   7/1/91
31 *
32 * $Revision: 6.93 $
33 *
34 * File Description:
35 *       Vibrant main, event loop, and window functions
36 *
37 * Modifications:
38 * --------------------------------------------------------------------------
39 *
40 * ==========================================================================
41 */
42 
43 #include <vibtypes.h>
44 #include <vibprocs.h>
45 #include <vibincld.h>
46 #include <vibmain.h>
47 
48 #include <connect/ncbi_core_c.h>
49 
50 #ifdef WIN_MAC
51 #ifdef __MWERKS__
52 #include <Appearance.h>
53 #include <Navigation.h>
54 #if __profile__
55 #include <Profiler.h>
56 #endif
57 #include "MoreCarbonAccessors.h"
58 #endif
59 #endif
60 
61 #if defined(WIN_MOTIF) && defined(_DEBUG) && !defined(__hpux)
62 #include <X11/Xmu/Editres.h>
63 #endif
64 
65 #ifdef WIN_X
66 #include <X11/Xatom.h>
67 #ifdef OS_AXP_VMS
68 #include <Xm/StdCmap.h>
69 #else
70 #if !defined(__hpux)
71 #include <X11/Xmu/StdCmap.h>
72 #endif /* !defined(__hpux) */
73 #endif
74 #endif
75 
76 #ifdef WIN_MAC
77 /* imported from vibutils.c, not in the header files. rsmith */
78 extern void Nlm_ConvertFilename ( FSSpec *fss, Nlm_CharPtr filename );
79 #endif
80 
81 /* remove this define to remove Quartz/Cocoa cursor handling
82    cursor calls will become no-ops if removed */
83 /*
84 #define WIN_MAC_QUARTZ_COCOA_CURSORS
85 */
86 
87 #ifdef WIN_MAC_QUARTZ
88 #ifdef WIN_MAC_QUARTZ_COCOA_CURSORS
89 /* needed to be able to access a bit of Cocoa */
90 #include <CoreFoundation/CoreFoundation.h>
91 char NSApplicationLoad(void);
92 void *NSClassFromString(CFStringRef aClassName);
93 void *NSSelectorFromString(CFStringRef aSelectorName);
94 void *objc_msgSend(void *self, void *_cmd, ...);
95 #endif
96 #endif
97 
98 #ifdef WIN_MAC
99 #ifndef Nlm_WindowTool
100 #define Nlm_WindowTool  WindowPtr
101 #endif
102 #ifndef Nlm_PortTool
103 #define Nlm_PortTool    GrafPtr
104 #endif
105 #ifndef Nlm_ShellTool
106 #define Nlm_ShellTool   Nlm_Handle
107 #endif
108 #ifndef Nlm_MainTool
109 #define Nlm_MainTool    Nlm_Handle
110 #endif
111 #endif
112 
113 #ifdef WIN_MSWIN
114 #ifndef Nlm_WindowTool
115 #define Nlm_WindowTool  HWND
116 #endif
117 #ifndef Nlm_PortTool
118 #define Nlm_PortTool    HDC
119 #endif
120 #ifndef Nlm_ShellTool
121 #define Nlm_ShellTool   Nlm_Handle
122 #endif
123 #ifndef Nlm_MainTool
124 #define Nlm_MainTool    Nlm_Handle
125 #endif
126 #endif
127 
128 #ifdef WIN_MOTIF
129 #ifndef Nlm_WindowTool
130 #define Nlm_WindowTool  Widget
131 #endif
132 #ifndef Nlm_PortTool
133 #define Nlm_PortTool    GC
134 #endif
135 #ifndef Nlm_ShellTool
136 #define Nlm_ShellTool   Widget
137 #endif
138 #ifndef Nlm_MainTool
139 #define Nlm_MainTool    Widget
140 #endif
141 #endif
142 
143 typedef enum {
144   USE_FULL_SCREEN = 1,
145   USE_LEFT_SCREEN,
146   USE_RIGHT_SCREEN,
147   USE_PRIMARY_MONITOR
148 } Nlm_ScreenMode;
149 
150 static Nlm_ScreenMode Nlm_screenMode = USE_FULL_SCREEN;
151 
152 typedef  struct  Nlm_shelldata {
153   struct Nlm_shelldata PNTR next;
154   Nlm_ShellTool             shell;
155 } Nlm_ShellData, PNTR Nlm_ShellDataPtr;
156 
157 typedef  struct  Nlm_windowdata {
158   Nlm_WindowTool   handle;
159   Nlm_ShellTool    shell;
160   Nlm_PortTool     port;
161   Nlm_MainTool     main;
162   Nlm_ColorMTool   cMap;
163   Nlm_ShellDataPtr allShells;
164   Nlm_Int2         cMapStatus;
165   Nlm_RecT         dragArea;
166   Nlm_RecT         growArea;
167   Nlm_RecT         zoomArea;
168   Nlm_WndActnProc  close;
169   Nlm_WndActnProc  activate;
170   Nlm_WndActnProc  deactivate;
171   Nlm_WndActnProc  resize;
172   Nlm_DisplaY      charDisplay;
173   Nlm_MenuBaR      menuBar;
174   Nlm_ButtoN       defaultButton;
175   Nlm_Boolean      notYetShown;
176   Nlm_Boolean      dying;
177   Nlm_CharPtr      title;
178   Nlm_VoidPtr      data;
179   Nlm_WndFreeProc  cleanup;
180   Nlm_WndActnProc  timer;
181   Nlm_WindoW       modalOwner; /* esl++ to support nested modal dialogs */
182   Nlm_ScreenMode   screenMode;
183 #ifdef WIN_MOTIF
184   Visual      *visual;
185   Nlm_Boolean cMap_fixed;
186 #endif
187 #ifdef WIN_MSWIN
188   Nlm_Uint4        style;
189   Nlm_Uint4        ex_style;
190   Nlm_Handle       haccel;
191 #endif
192 } Nlm_WindowData;
193 
194 typedef  struct  Nlm_windowrec {
195   Nlm_BoxRec      boxR;
196   Nlm_WindowData  window;
197 } Nlm_WindowRec, PNTR Nlm_WndPtr;
198 
199 Nlm_WindoW   Nlm_desktopWindow;
200 Nlm_WindoW   Nlm_systemWindow;
201 
202 Nlm_RecT     Nlm_screenRect;
203 
204 Nlm_WindoW   Nlm_theWindow;
205 
206 extern Nlm_Boolean okayToDrawContents;
207 Nlm_Boolean okayToDrawContents = TRUE; /* set to false by CleanUpWindows */
208 
209 extern Nlm_Boolean Nlm_usesMacNavServices;
210 Nlm_Boolean Nlm_usesMacNavServices = FALSE;
211 
212 #ifdef WIN_MAC
213 EventRecord  Nlm_currentEvent;
214 #ifdef WIN_MAC_QUARTZ
215 static void Nlm_DrawWindow (Nlm_GraphiC w, Nlm_Boolean drawGrowIcon);
216 #endif
217 #ifdef __CONDITIONALMACROS__
218 /* Global RoutineDescriptors - from RasMol */
219 AEEventHandlerUPP HandleAEIgnorePtr;
220 AEEventHandlerUPP HandleAEOpenDocPtr;
221 AEEventHandlerUPP HandleAEQuitAppPtr;
222 AEEventHandlerUPP HandleAEAnswerPtr;
223 #endif
224 typedef SInt32 AERefCon;
225 /* typedef UInt32 AERefCon; */
226 #endif
227 
228 #ifdef WIN_MSWIN
229 WPARAM       Nlm_currentWParam;
230 HINSTANCE    Nlm_currentHInst;
231 LPARAM       Nlm_currentLParam;
232 MSG          Nlm_currentMssg;
233 int          Nlm_currentId;
234 HWND         Nlm_currentHwndCtl;
235 UINT         Nlm_currentCode;
236 int          Nlm_currentPos;
237 #endif
238 
239 #ifdef WIN_MOTIF
240 XEvent        Nlm_currentEvent;
241 XtAppContext  Nlm_appContext;
242 XmFontList    Nlm_XfontList;
243 Widget        Nlm_fileDialogShell;
244 
245 static const char Nlm_VibName[]            = "vibrant";
246 static const char Nlm_VibClass[]           = "Vibrant";
247 static       char Nlm_AppName[PATH_MAX+1]  = "vibrant";
248 #endif
249 
250 Nlm_WindowTool  Nlm_currentWindowTool;
251 Nlm_Boolean     Nlm_processUpdatesFirstVal = TRUE;
252 
253 static Nlm_Boolean     Nlm_showGetArgTag = TRUE;
254 
255 static Nlm_GphPrcsPtr  gphprcsptr = NULL;
256 
257 static Nlm_GphPrcsPtr  documentProcs;
258 static Nlm_GphPrcsPtr  fixedProcs;
259 static Nlm_GphPrcsPtr  frozenProcs;
260 static Nlm_GphPrcsPtr  roundProcs;
261 static Nlm_GphPrcsPtr  alertProcs;
262 static Nlm_GphPrcsPtr  modalProcs;
263 static Nlm_GphPrcsPtr  movableModalProcs;
264 static Nlm_GphPrcsPtr  floatingProcs;
265 static Nlm_GphPrcsPtr  shadowProcs;
266 static Nlm_GphPrcsPtr  plainProcs;
267 static Nlm_GphPrcsPtr  desktopProcs;
268 static Nlm_GphPrcsPtr  systemProcs;
269 
270 static Nlm_Boolean     quitProgram;
271 static Nlm_Boolean     getArgsBoxUp;
272 static Nlm_Boolean     getArgsOk;
273 
274 static Nlm_WindoW      recentWindow = NULL;
275 static Nlm_WindowData  recentWindowData;
276 
277 #ifdef WIN_MAC
278 static Nlm_WindoW      chosenWindow;
279 static Nlm_WindoW      frontWindow;
280 static Nlm_WindoW      theActiveWindow;
281 #endif
282 
283 static Nlm_WindoW      dyingWindow;
284 static Nlm_RecT        screenBitBounds;
285 
286 /*
287 #ifndef WIN_MOTIF
288 static Nlm_Boolean     inNotice;
289 #endif
290 */
291 
292 static Nlm_VoidProc    timerAction = NULL;
293 static Nlm_KeyProc     keyAction = NULL;
294 static Nlm_ClckProc    mouseMovedAction = NULL;
295 
296 static Nlm_WndActnProc  appWndAct = NULL;
297 static Nlm_WndActnProc  appWndDeact = NULL;
298 
299 static Nlm_DropProc        registeredDropProc = NULL;
300 static Nlm_ServiceProc  registeredServiceProc = NULL;
301 static Nlm_ResultProc   registeredResultProc = NULL;
302 
303 #ifdef WIN_MAC
304 static Cursor       cross;
305 static Cursor       iBeam;
306 static Cursor       plus;
307 static Cursor       watch;
308 static Nlm_Int4     lastTimerTime;
309 static Nlm_Boolean  hasColorQD = FALSE;
310 #endif
311 
312 #ifdef WIN_MSWIN
313 HCURSOR       Nlm_currentCursor;
314 static UINT          timerID;
315 static Nlm_Char      windowclass [32];
316 static int           discard_count = 0;
317 static Nlm_Boolean   handlechar;
318 BOOLEAN Nlm_hasBackColor = FALSE;
319 COLORREF Nlm_crBackColor = 0;
320 HBRUSH Nlm_hbrWindowBackground = NULL;
321 #endif
322 
323 #ifdef WIN_MOTIF
324 Cursor        Nlm_currentCursor;
325 static Cursor        arrow;
326 static Cursor        cross;
327 static Cursor        iBeam;
328 static Cursor        plus;
329 static Cursor        watch;
330 static XFontStruct   *font;
331 static XtIntervalId  windowTimer;
332 static Widget        selectShell;
333 #endif
334 
335 #ifdef WIN_MOTIF
336 #ifdef MISSING_X_SYMBOLS_BUG
get_wmShellWidgetClass(void)337 int get_wmShellWidgetClass (void)
338 
339 {
340 }
341 
get_applicationShellWidgetClass(void)342 int get_applicationShellWidgetClass (void)
343 
344 {
345 }
346 
XShapeCombineMask(void)347 int XShapeCombineMask (void)
348 
349 {
350 }
351 #endif
352 #endif
353 
Nlm_LoadWindowData(Nlm_WindoW w,Nlm_WindowTool hdl,Nlm_ShellTool shl,Nlm_PortTool prt,Nlm_MainTool man,Nlm_RectPtr drag,Nlm_RectPtr grow,Nlm_RectPtr zoom,Nlm_WndActnProc closeProc,Nlm_WndActnProc actProc,Nlm_WndActnProc deactProc,Nlm_WndActnProc resiz,Nlm_DisplaY charDisp,Nlm_MenuBaR mnuBar,Nlm_ButtoN dfltBtn,Nlm_Boolean notYet,Nlm_Boolean die,Nlm_CharPtr ttl,Nlm_VoidPtr dat,Nlm_WndFreeProc cln,Nlm_WndActnProc tmr)354 static void Nlm_LoadWindowData (Nlm_WindoW w, Nlm_WindowTool hdl,
355                                 Nlm_ShellTool shl, Nlm_PortTool prt,
356                                 Nlm_MainTool man, Nlm_RectPtr drag,
357                                 Nlm_RectPtr grow, Nlm_RectPtr zoom,
358                                 Nlm_WndActnProc closeProc, Nlm_WndActnProc actProc,
359                                 Nlm_WndActnProc deactProc, Nlm_WndActnProc resiz,
360                                 Nlm_DisplaY charDisp, Nlm_MenuBaR mnuBar,
361                                 Nlm_ButtoN dfltBtn, Nlm_Boolean notYet,
362                                 Nlm_Boolean die, Nlm_CharPtr ttl,
363                                 Nlm_VoidPtr dat, Nlm_WndFreeProc cln,
364                                 Nlm_WndActnProc tmr)
365 
366 {
367   Nlm_WindowData  PNTR wdptr;
368   Nlm_WndPtr      wp;
369 
370   if (w != NULL) {
371     wp = (Nlm_WndPtr) Nlm_HandLock (w);
372     wdptr = &(wp->window);
373     wdptr->handle = hdl;
374     wdptr->shell = shl;
375     wdptr->port = prt;
376     wdptr->main = man;
377     if (drag != NULL) {
378       wdptr->dragArea = *drag;
379     } else {
380       Nlm_LoadRect (&(wdptr->dragArea), 0, 0, 0, 0);
381     }
382     if (grow != NULL) {
383       wdptr->growArea = *grow;
384     } else {
385       Nlm_LoadRect (&(wdptr->growArea), 0, 0, 0, 0);
386     }
387     if (zoom != NULL) {
388       wdptr->zoomArea = *zoom;
389     } else {
390       Nlm_LoadRect (&(wdptr->zoomArea), 0, 0, 0, 0);
391     }
392     wdptr->close = closeProc;
393     wdptr->activate = actProc;
394     wdptr->deactivate = deactProc;
395     wdptr->resize = resiz;
396     wdptr->charDisplay = charDisp;
397     wdptr->menuBar = mnuBar;
398     wdptr->defaultButton = dfltBtn;
399     wdptr->notYetShown = notYet;
400     wdptr->dying = die;
401     wdptr->title = ttl;
402     wdptr->data = dat;
403     wdptr->cleanup = cln;
404     wdptr->timer = tmr;
405     wdptr->modalOwner = NULL; /* esl++ */
406     wdptr->screenMode = Nlm_screenMode; /* jk */
407 #ifdef WIN_MOTIF
408     wdptr->visual = NULL;
409     wdptr->cMap_fixed = FALSE;
410 #endif
411     Nlm_HandUnlock (w);
412     recentWindow = NULL;
413   }
414 }
415 
Nlm_SetWindowData(Nlm_WindoW w,Nlm_WindowData * wdata)416 static void Nlm_SetWindowData (Nlm_WindoW w, Nlm_WindowData * wdata)
417 
418 {
419   Nlm_WndPtr  wp;
420 
421   if (w != NULL && wdata != NULL) {
422     wp = (Nlm_WndPtr) Nlm_HandLock (w);
423     wp->window = *wdata;
424     Nlm_HandUnlock (w);
425     recentWindow = w;
426     recentWindowData = *wdata;
427   }
428 }
429 
Nlm_GetWindowData(Nlm_WindoW w,Nlm_WindowData * wdata)430 static void Nlm_GetWindowData (Nlm_WindoW w, Nlm_WindowData * wdata)
431 
432 {
433   Nlm_WndPtr  wp;
434 
435   if (w != NULL && wdata != NULL) {
436     if (w == recentWindow && NLM_RISKY) {
437       *wdata = recentWindowData;
438     } else {
439       wp = (Nlm_WndPtr) Nlm_HandLock (w);
440       *wdata = wp->window;
441       Nlm_HandUnlock (w);
442       recentWindow = w;
443       recentWindowData = *wdata;
444     }
445   }
446 }
447 
Nlm_GetWindowPtr(Nlm_WindoW w)448 static Nlm_WindowTool Nlm_GetWindowPtr (Nlm_WindoW w)
449 
450 {
451   Nlm_WindowTool  rsult;
452   Nlm_WindowData  wdata;
453 
454   rsult = (Nlm_WindowTool) 0;
455   if (w != NULL) {
456     Nlm_GetWindowData (w, &wdata);
457     rsult = wdata.handle;
458   }
459   return rsult;
460 }
461 
Nlm_GetWindowShell(Nlm_WindoW w)462 extern Nlm_ShellTool Nlm_GetWindowShell (Nlm_WindoW w)
463 
464 {
465   Nlm_ShellTool   rsult;
466   Nlm_WindowData  wdata;
467 
468   rsult = (Nlm_ShellTool) 0;
469   if (w != NULL) {
470     Nlm_GetWindowData (w, &wdata);
471     rsult = wdata.shell;
472   }
473   return rsult;
474 }
475 
476 
477 extern void Nlm_AddSubwindowShell (Nlm_WindoW w, Nlm_ShellTool shell );
Nlm_AddSubwindowShell(Nlm_WindoW w,Nlm_ShellTool shell)478 extern void Nlm_AddSubwindowShell (Nlm_WindoW w, Nlm_ShellTool shell )
479 {
480   Nlm_WindowData   wdata;
481   Nlm_ShellDataPtr sptr;
482 
483   sptr = (Nlm_ShellDataPtr)MemNew( sizeof(Nlm_ShellData) );
484   sptr->shell = shell;
485 
486   Nlm_GetWindowData(w, &wdata);
487   sptr->next = wdata.allShells;
488   wdata.allShells = sptr;
489   Nlm_SetWindowData(w, &wdata);
490 }
491 
492 
493 extern void Nlm_DelSubwindowShell (Nlm_WindoW w, Nlm_ShellTool shell );
Nlm_DelSubwindowShell(Nlm_WindoW w,Nlm_ShellTool shell)494 extern void Nlm_DelSubwindowShell (Nlm_WindoW w, Nlm_ShellTool shell )
495 {
496   Nlm_WindowData   wdata;
497   Nlm_ShellDataPtr sptr;
498   Nlm_ShellDataPtr sPrevptr = NULL;
499 
500   Nlm_GetWindowData(w, &wdata);
501   for (sptr = wdata.allShells;
502        sptr != NULL  &&  sptr->shell != shell;
503        sPrevptr = sptr, sptr = sptr->next) continue;
504   if (sptr == NULL)
505     return;
506 
507   if (sPrevptr == NULL)
508     wdata.allShells = sptr->next;
509   else
510     sPrevptr->next = sptr->next;
511 
512   MemFree( sptr );
513   Nlm_SetWindowData(w, &wdata);
514 }
515 
516 
Nlm_GetWindowPort(Nlm_WindoW w)517 static Nlm_PortTool Nlm_GetWindowPort (Nlm_WindoW w)
518 
519 {
520   Nlm_PortTool    rsult;
521   Nlm_WindowData  wdata;
522 
523   rsult = NULL;
524   if (w != NULL) {
525     Nlm_GetWindowData (w, &wdata);
526     rsult = wdata.port;
527   }
528   return rsult;
529 }
530 
Nlm_GetWindowMain(Nlm_WindoW w)531 static Nlm_MainTool Nlm_GetWindowMain (Nlm_WindoW w)
532 
533 {
534   Nlm_MainTool    rsult;
535   Nlm_WindowData  wdata;
536 
537   rsult = (Nlm_MainTool) 0;
538   if (w != NULL) {
539     Nlm_GetWindowData (w, &wdata);
540     rsult = wdata.main;
541   }
542   return rsult;
543 }
544 
Nlm_SetWindowCharDisplay(Nlm_WindoW w,Nlm_DisplaY d)545 extern void Nlm_SetWindowCharDisplay (Nlm_WindoW w, Nlm_DisplaY d)
546 
547 {
548   Nlm_WindowData  wdata;
549 
550   if (w != NULL) {
551     Nlm_GetWindowData (w, &wdata);
552     wdata.charDisplay = d;
553     Nlm_SetWindowData (w, &wdata);
554   }
555 }
556 
Nlm_GetWindowCharDisplay(Nlm_WindoW w)557 extern Nlm_DisplaY Nlm_GetWindowCharDisplay (Nlm_WindoW w)
558 
559 {
560   Nlm_DisplaY     rsult;
561   Nlm_WindowData  wdata;
562 
563   rsult = NULL;
564   if (w != NULL) {
565     Nlm_GetWindowData (w, &wdata);
566     rsult = wdata.charDisplay;
567   }
568   return rsult;
569 }
570 
Nlm_SetWindowMenuBar(Nlm_WindoW w,Nlm_MenuBaR mb)571 extern void Nlm_SetWindowMenuBar (Nlm_WindoW w, Nlm_MenuBaR mb)
572 
573 {
574   Nlm_WindowData  wdata;
575 
576   if (w != NULL) {
577     Nlm_GetWindowData (w, &wdata);
578     wdata.menuBar = mb;
579     Nlm_SetWindowData (w, &wdata);
580   }
581 }
582 
Nlm_GetWindowMenuBar(Nlm_WindoW w)583 extern Nlm_MenuBaR Nlm_GetWindowMenuBar (Nlm_WindoW w)
584 
585 {
586   Nlm_MenuBaR     rsult;
587   Nlm_WindowData  wdata;
588 
589   rsult = NULL;
590   if (w != NULL) {
591     Nlm_GetWindowData (w, &wdata);
592     rsult = wdata.menuBar;
593   }
594   return rsult;
595 }
596 
597 extern void Nlm_SetButtonDefault (Nlm_ButtoN b, Nlm_Boolean dflt);
Nlm_SetWindowDefaultButton(Nlm_WindoW w,Nlm_ButtoN b)598 extern void Nlm_SetWindowDefaultButton (Nlm_WindoW w, Nlm_ButtoN b)
599 {
600   Nlm_WindowData  wdata;
601 
602   if (w != NULL) {
603     Nlm_GetWindowData (w, &wdata);
604     if (wdata.defaultButton != NULL)
605       Nlm_SetButtonDefault(wdata.defaultButton, FALSE);
606     wdata.defaultButton = b;
607     if (wdata.defaultButton != NULL)
608       Nlm_SetButtonDefault(wdata.defaultButton, TRUE);
609     Nlm_SetWindowData (w, &wdata);
610   }
611 }
612 
Nlm_GetWindowDefaultButton(Nlm_WindoW w)613 extern Nlm_ButtoN Nlm_GetWindowDefaultButton (Nlm_WindoW w)
614 
615 {
616   Nlm_ButtoN      rsult;
617   Nlm_WindowData  wdata;
618 
619   rsult = NULL;
620   if (w != NULL) {
621     Nlm_GetWindowData (w, &wdata);
622     rsult = wdata.defaultButton;
623   }
624   return rsult;
625 }
626 
Nlm_SetNotYetShown(Nlm_WindoW w,Nlm_Boolean notYet)627 static void Nlm_SetNotYetShown (Nlm_WindoW w, Nlm_Boolean notYet)
628 
629 {
630   Nlm_WindowData  wdata;
631 
632   if (w != NULL) {
633     Nlm_GetWindowData (w, &wdata);
634     wdata.notYetShown = notYet;
635     Nlm_SetWindowData (w, &wdata);
636   }
637 }
638 
Nlm_WindowHasBeenShown(Nlm_WindoW w)639 extern Nlm_Boolean Nlm_WindowHasBeenShown (Nlm_WindoW w)
640 
641 {
642   Nlm_Boolean     rsult;
643   Nlm_WindowData  wdata;
644 
645   rsult = FALSE;
646   if (w != NULL) {
647     Nlm_GetWindowData (w, &wdata);
648     rsult = (Nlm_Boolean) (! wdata.notYetShown);
649   }
650   return rsult;
651 }
652 
Nlm_SetWindowDying(Nlm_WindoW w,Nlm_Boolean die)653 static void Nlm_SetWindowDying (Nlm_WindoW w, Nlm_Boolean die)
654 
655 {
656   Nlm_WindowData  wdata;
657 
658   if (w != NULL) {
659     Nlm_GetWindowData (w, &wdata);
660     wdata.dying = die;
661     Nlm_SetWindowData (w, &wdata);
662   }
663 }
664 
Nlm_IsWindowDying(Nlm_WindoW w)665 extern Nlm_Boolean Nlm_IsWindowDying (Nlm_WindoW w)
666 
667 {
668   Nlm_Boolean     rsult;
669   Nlm_WindowData  wdata;
670 
671   rsult = FALSE;
672   if (w != NULL) {
673     Nlm_GetWindowData (w, &wdata);
674     rsult = wdata.dying;
675   }
676   return rsult;
677 }
678 
679 /*
680 static void Nlm_SetScreenMode (Nlm_WindoW w, Nlm_ScreenMode screenMode)
681 
682 {
683   Nlm_WindowData  wdata;
684 
685   if (w != NULL) {
686     Nlm_GetWindowData (w, &wdata);
687     wdata.screenMode = screenMode;
688     Nlm_SetWindowData (w, &wdata);
689   }
690 }
691 */
692 
Nlm_GetScreenMode(Nlm_WindoW w)693 static Nlm_ScreenMode Nlm_GetScreenMode (Nlm_WindoW w)
694 
695 {
696   Nlm_ScreenMode  rsult;
697   Nlm_WindowData  wdata;
698 
699   rsult = USE_FULL_SCREEN;
700   if (w != NULL) {
701     Nlm_GetWindowData (w, &wdata);
702     rsult = wdata.screenMode;
703   }
704   return rsult;
705 }
706 
Nlm_GetParentWindow(Nlm_GraphiC a)707 extern Nlm_WindoW Nlm_GetParentWindow (Nlm_GraphiC a)
708 
709 {
710   Nlm_GraphiC  g;
711   Nlm_GraphiC  p;
712 
713   g = a;
714   if (a != NULL) {
715     p = Nlm_GetParent (g);
716     while (p != NULL) {
717       g = p;
718       p = Nlm_GetParent (g);
719     }
720   }
721   return (Nlm_WindoW) g;
722 }
723 
Nlm_ParentWindow(Nlm_Handle a)724 extern Nlm_WindoW Nlm_ParentWindow (Nlm_Handle a)
725 
726 {
727   return Nlm_GetParentWindow ((Nlm_GraphiC) a);
728 }
729 
Nlm_ParentWindowPtr(Nlm_GraphiC a)730 extern Nlm_WindowTool Nlm_ParentWindowPtr (Nlm_GraphiC a)
731 
732 {
733   Nlm_WindowTool  rsult;
734   Nlm_WindoW      w;
735 
736   rsult = (Nlm_WindowTool) 0;
737   if (a != NULL) {
738     w = Nlm_GetParentWindow (a);
739     rsult = Nlm_GetWindowPtr (w);
740   }
741   return rsult;
742 }
743 
Nlm_ParentWindowPort(Nlm_GraphiC a)744 extern Nlm_PortTool Nlm_ParentWindowPort (Nlm_GraphiC a)
745 
746 {
747   Nlm_PortTool  rsult;
748   Nlm_WindoW    w;
749 
750   rsult = NULL;
751   if (a != NULL) {
752     w = Nlm_GetParentWindow (a);
753     rsult = Nlm_GetWindowPort (w);
754   }
755   return rsult;
756 }
757 
Nlm_ParentWindowMain(Nlm_GraphiC a)758 extern Nlm_MainTool Nlm_ParentWindowMain (Nlm_GraphiC a)
759 
760 {
761   Nlm_MainTool  rsult;
762   Nlm_WindoW    w;
763 
764   rsult = (Nlm_MainTool) 0;
765   if (a != NULL) {
766     w = Nlm_GetParentWindow (a);
767     rsult = Nlm_GetWindowMain (w);
768   }
769   return rsult;
770 }
771 
Nlm_ParentWindowShell(Nlm_GraphiC a)772 extern Nlm_ShellTool Nlm_ParentWindowShell (Nlm_GraphiC a)
773 
774 {
775   Nlm_ShellTool  rsult;
776   Nlm_WindoW     w;
777 
778   rsult = (Nlm_ShellTool) 0;
779   if (a != NULL) {
780     w = Nlm_GetParentWindow (a);
781     rsult = Nlm_GetWindowShell (w);
782   }
783   return rsult;
784 }
785 
Nlm_SavePortIfNeeded(Nlm_GraphiC a,Nlm_Boolean savePort)786 extern Nlm_WindoW Nlm_SavePortIfNeeded (Nlm_GraphiC a, Nlm_Boolean savePort)
787 
788 {
789   Nlm_WindoW  newwindow;
790   Nlm_WindoW  oldwindow;
791   Nlm_WindoW  rsult;
792 
793   rsult = NULL;
794   if (a != NULL && savePort) {
795     savePort = Nlm_GetAllParentsVisible (a);
796     if (savePort) {
797       oldwindow = Nlm_CurrentWindow ();
798       newwindow = Nlm_GetParentWindow (a);
799       savePort = (Nlm_Boolean) (oldwindow != newwindow);
800       if (savePort) {
801         Nlm_UseWindow (newwindow);
802         rsult = oldwindow;
803       }
804     }
805   }
806   return rsult;
807 }
808 
Nlm_SavePort(Nlm_Handle a)809 extern Nlm_WindoW Nlm_SavePort (Nlm_Handle a)
810 
811 {
812   return Nlm_SavePortIfNeeded ((Nlm_GraphiC) a, TRUE);
813 }
814 
Nlm_RestorePort(Nlm_WindoW w)815 extern void Nlm_RestorePort (Nlm_WindoW w)
816 
817 {
818   if (Nlm_processUpdatesFirstVal) {
819     Nlm_Update ();
820   }
821   if (w != NULL) {
822     Nlm_UseWindow (w);
823   }
824 }
825 
Nlm_SetClose(Nlm_WindoW w,Nlm_WndActnProc cls)826 extern void Nlm_SetClose (Nlm_WindoW w, Nlm_WndActnProc cls)
827 
828 {
829   Nlm_WindowData  wdata;
830 
831   if (w != NULL) {
832     Nlm_GetWindowData (w, &wdata);
833     wdata.close = cls;
834     Nlm_SetWindowData (w, &wdata);
835   }
836 }
837 
Nlm_SetActivate(Nlm_WindoW w,Nlm_WndActnProc act)838 extern void Nlm_SetActivate (Nlm_WindoW w, Nlm_WndActnProc act)
839 
840 {
841   Nlm_WindowData  wdata;
842 
843   if (w != NULL) {
844     Nlm_GetWindowData (w, &wdata);
845     wdata.activate = act;
846     Nlm_SetWindowData (w, &wdata);
847   } else {
848     appWndAct = act;
849   }
850 }
851 
Nlm_SetDeactivate(Nlm_WindoW w,Nlm_WndActnProc deact)852 extern void Nlm_SetDeactivate (Nlm_WindoW w, Nlm_WndActnProc deact)
853 
854 {
855   Nlm_WindowData  wdata;
856 
857   if (w != NULL) {
858     Nlm_GetWindowData (w, &wdata);
859     wdata.deactivate = deact;
860     Nlm_SetWindowData (w, &wdata);
861   } else {
862     appWndDeact = deact;
863   }
864 }
865 
866 /*
867  * Test X windows to see what kind of color buffer is available.
868  * Necessary because Linux supports only the type available
869  * from the hardware.  Returns TRUE if match and visinfo.
870  */
871 
872 #ifdef WIN_MOTIF
873 
874 typedef struct _TNlm_MatchListX {
875     int class;
876     unsigned int depth;
877 } TNlm_MatchListX;
878 
879 #define XMATCHLENGTH 6
880 
881 /* list of display classes and depths to match, in order.
882  * we prefer 8 bit for Cn3D for speed.
883  */
884 static const TNlm_MatchListX Nlm_MatchListX[] = {
885     { PseudoColor, 8  },
886     { GrayScale, 8 },
887     { PseudoColor, 16 },
888     { TrueColor, 24 },
889     { GrayScale, 16 },
890     { TrueColor, 16 } };
891 
Nlm_CheckX(XVisualInfo * visinfo)892 extern Nlm_Boolean Nlm_CheckX(XVisualInfo * visinfo)
893 {
894     int visualsmatched, i, j, defaultdepth;
895     XVisualInfo *VisualList, VisualTemplate;
896 
897     /* check display modes in order of preference */
898 
899     VisualTemplate.screen = DefaultScreen(Nlm_currentXDisplay);
900 
901     /* enforce vibrant convention of using default depth for windows */
902     defaultdepth = DefaultDepth(Nlm_currentXDisplay, VisualTemplate.screen);
903 
904     VisualList = XGetVisualInfo(Nlm_currentXDisplay, VisualScreenMask,
905             &VisualTemplate, &visualsmatched);
906 
907     for(j = 0; j < XMATCHLENGTH; j++) {
908         for(i = 0; i < visualsmatched; i++) {
909          if (VisualList[i].depth == Nlm_MatchListX[j].depth
910                  && VisualList[i].class == Nlm_MatchListX[j].class
911                  && VisualList[i].depth == defaultdepth) {
912                 MemCpy(visinfo, &VisualList[i], sizeof(XVisualInfo));
913                 XFree(VisualList);
914                 return TRUE;
915             }
916         }
917     }
918     return FALSE;
919 }
920 #endif /* WIN_MOTIF */
921 
Nlm_SetResize(Nlm_WindoW w,Nlm_WndActnProc resiz)922 extern void Nlm_SetResize (Nlm_WindoW w, Nlm_WndActnProc resiz)
923 
924 {
925   Nlm_WindowData  wdata;
926 
927   if (w != NULL) {
928     Nlm_GetWindowData (w, &wdata);
929     wdata.resize = resiz;
930     Nlm_SetWindowData (w, &wdata);
931   }
932 }
933 
Nlm_SetColorMap(Nlm_WindoW w,Nlm_Uint2 totalColors,Nlm_Uint1Ptr red,Nlm_Uint1Ptr green,Nlm_Uint1Ptr blue)934 extern void Nlm_SetColorMap (Nlm_WindoW w, Nlm_Uint2 totalColors,
935                              Nlm_Uint1Ptr red, Nlm_Uint1Ptr green,
936                              Nlm_Uint1Ptr blue)
937 {
938   Nlm_WindowData  wdata;
939 
940 #ifdef WIN_MSWIN
941   Nlm_Uint2       i;
942   LOGPALETTE    * palette;
943   PALETTEENTRY  * p;
944 #endif
945 #ifdef WIN_MAC
946   Nlm_Int2        i;
947   RGBColor        col;
948 #endif
949 
950 
951   if (w == NULL  ||  totalColors > 256)
952     return;
953 
954   Nlm_GetWindowData (w, &wdata);
955 
956 #ifdef WIN_MSWIN
957   if ( GetDeviceCaps (Nlm_currentHDC, RASTERCAPS) &  RC_PALETTE ){
958     if ( wdata.cMap != NULL ){
959       DeleteObject( wdata.cMap );
960       wdata.cMap = NULL;
961     }
962     if ( totalColors!=0 ) {
963       palette = (LOGPALETTE*)MemNew (sizeof(LOGPALETTE) +
964                                      totalColors*sizeof(PALETTEENTRY));
965       if ( palette != NULL ) {
966         palette->palVersion = 0x300;
967         palette->palNumEntries = totalColors;
968         for( i=0; i<totalColors; i++ ) {
969           p = &(palette->palPalEntry[i]);
970           p->peFlags = (BYTE)((i != 0) ? 0 : PC_RESERVED);
971           p->peRed   = red[i];
972           p->peGreen = green[i];
973           p->peBlue  = blue[i];
974         }
975         wdata.cMap = CreatePalette(palette);
976         MemFree (palette);
977         if ( wdata.cMap != NULL ) {
978           SelectPalette ( Nlm_currentHDC,wdata.cMap,FALSE );
979           RealizePalette ( Nlm_currentHDC );
980 
981         }
982       }
983     }
984   }
985 #endif
986 
987 #ifdef WIN_MAC
988 // QUARTZ_FIXME: there's no replacement for this in CG, does anything
989 // need to be done?
990 #ifndef WIN_MAC_QUARTZ
991   if ( wdata.cMap != NULL ){
992     if ( wdata.cMapStatus ){
993       SetPalette( wdata.handle, NULL, FALSE);
994     }
995     DisposePalette ( wdata.cMap );
996     wdata.cMap = NULL;
997   }
998   wdata.cMapStatus = 0;
999   if ( totalColors!=0 ) {
1000     wdata.cMap = NewPalette(totalColors,(CTabHandle)0,pmTolerant,0);
1001     if ( wdata.cMap != NULL ) {
1002       for( i=0; i<totalColors; i++ ) {
1003         col.red   = (Nlm_Uint2)red[i]<<8 | (Nlm_Uint2)red[i];
1004         col.green = (Nlm_Uint2)green[i]<<8 | (Nlm_Uint2)green[i];
1005         col.blue  = (Nlm_Uint2)blue[i]<<8 | (Nlm_Uint2)blue[i];
1006         SetEntryColor(wdata.cMap,i,&col);
1007       }
1008       SetPalette( wdata.handle, wdata.cMap, FALSE);
1009       ActivatePalette ( wdata.handle );
1010       wdata.cMapStatus = 1;
1011     }
1012   }
1013 #endif
1014 #endif
1015 
1016 #ifdef WIN_MOTIF
1017   do { /* TRY */
1018     unsigned long  pixel;
1019     int            n_savedColors = 0;
1020     XColor         colorCells[256];
1021 
1022     /* Uninstall, store first several colors and free current
1023      * colormap -- if necessary */
1024     if ( wdata.cMap )
1025       {
1026         n_savedColors = 32;
1027         if (n_savedColors > (int)totalColors)
1028           return;
1029 
1030         if ( wdata.cMapStatus )
1031           {
1032             XUninstallColormap(Nlm_currentXDisplay, wdata.cMap);
1033             wdata.cMapStatus = 0;
1034           }
1035 
1036         if ( !wdata.cMap_fixed )
1037           {
1038             if (totalColors != 0)
1039               {
1040                 for (pixel = 0;  pixel < n_savedColors;  pixel++)
1041                   colorCells[pixel].pixel = pixel;
1042                 XQueryColors(Nlm_currentXDisplay, wdata.cMap,
1043                              colorCells, n_savedColors);
1044               }
1045 
1046             XFreeColormap(Nlm_currentXDisplay, wdata.cMap);
1047             wdata.cMap = (Nlm_ColorMTool) 0;
1048           }
1049       }
1050 
1051     if (totalColors == 0)
1052       break;    /* no colors specified for the new colormap */
1053 
1054     /* Create new colormap, if necessary */
1055     if ( !wdata.cMap )
1056       {
1057         XVisualInfo    visinfo;
1058         unsigned long  plane_m[1];
1059         unsigned long  pixels[256];
1060         int defaultdepth;
1061 
1062         defaultdepth = DefaultDepth(Nlm_currentXDisplay,
1063                   DefaultScreen(Nlm_currentXDisplay));
1064 #ifdef OS_UNIX_LINUX
1065         if(!Nlm_CheckX(&visinfo))
1066 #else /* OS_UNIX_LINUX */
1067         if( !(XMatchVisualInfo(Nlm_currentXDisplay,
1068                          DefaultScreen(Nlm_currentXDisplay),
1069                          8,PseudoColor,&visinfo) ||
1070             XMatchVisualInfo(Nlm_currentXDisplay,
1071                          DefaultScreen(Nlm_currentXDisplay),
1072                          8,GrayScale,&visinfo)) )
1073 #endif /* else OS_UNIX_LINUX */
1074         break;  /* no matching visuals found */
1075 
1076 #ifdef OS_UNIX_LINUX
1077         if(visinfo.class > PseudoColor) break;  /* no palette needed */
1078 #endif
1079         wdata.cMap = XCreateColormap(Nlm_currentXDisplay,
1080                                      RootWindow(Nlm_currentXDisplay,
1081                                                 Nlm_currentXScreen),
1082                                      visinfo.visual, AllocNone);
1083         if (wdata.cMap == DefaultColormap(Nlm_currentXDisplay,
1084                                           Nlm_currentXScreen))
1085           {
1086             wdata.cMap = (Nlm_ColorMTool) 0;
1087             break;  /* hardware colormap is immutable */
1088           }
1089 
1090         if ( !XAllocColorCells ( Nlm_currentXDisplay, wdata.cMap, FALSE,
1091                                  (unsigned long*) plane_m, 0,
1092                                  (unsigned long*) pixels, totalColors) )
1093           {
1094             XFreeColormap (Nlm_currentXDisplay,wdata.cMap);
1095             wdata.cMap = (Nlm_ColorMTool) 0;
1096             break;  /* cannot allocate color cells for the new colormap */
1097           }
1098       }
1099 
1100     for (pixel = n_savedColors;  pixel < totalColors;  pixel++)
1101       {
1102         colorCells[pixel].red   = (((Nlm_Uint2)red  [pixel]) << 8) |
1103           (Nlm_Uint2)red  [pixel];
1104         colorCells[pixel].green = (((Nlm_Uint2)green[pixel]) << 8) |
1105           (Nlm_Uint2)green[pixel];
1106         colorCells[pixel].blue  = (((Nlm_Uint2)blue [pixel]) << 8) |
1107           (Nlm_Uint2)blue [pixel];
1108 
1109         colorCells[pixel].flags = DoRed | DoGreen | DoBlue;
1110         colorCells[pixel].pixel = pixel;
1111         colorCells[pixel].pad = 0;
1112       }
1113     XStoreColors(Nlm_currentXDisplay, wdata.cMap,
1114                  colorCells + n_savedColors,
1115                  (int)(pixel - n_savedColors));
1116 
1117     XInstallColormap(Nlm_currentXDisplay, wdata.cMap);
1118     XSetWindowColormap (Nlm_currentXDisplay, XtWindow( wdata.shell ),
1119                         wdata.cMap);
1120     {{
1121       Nlm_ShellDataPtr sptr = wdata.allShells;
1122       while (sptr != NULL)
1123         {
1124           XSetWindowColormap (Nlm_currentXDisplay, XtWindow( sptr->shell ),
1125                               wdata.cMap);
1126           sptr = sptr->next;
1127         }
1128     }}
1129 
1130     wdata.cMapStatus = 1;
1131   }  while ( 0 );
1132 #endif
1133 
1134   Nlm_SetWindowData (w, &wdata);
1135 }
1136 
1137 
Nlm_SetColorCell(Nlm_GraphiC gr,Nlm_Uint1 pixel,Nlm_Uint1 red,Nlm_Uint1 green,Nlm_Uint1 blue)1138 extern void Nlm_SetColorCell(Nlm_GraphiC gr, Nlm_Uint1 pixel,
1139                              Nlm_Uint1 red, Nlm_Uint1 green, Nlm_Uint1 blue)
1140 {
1141 #ifdef WIN_MOTIF
1142   Colormap cMap;
1143   XColor colorCell;
1144 
1145   /* get colormap, if fixed */
1146   if ( !Nlm_GetFixedColormap(gr, &cMap, NULL) )
1147     return;
1148 
1149   /* compose and store the new color cell */
1150   colorCell.red   = ((Nlm_Uint2)red   << 8) | (Nlm_Uint2)red;
1151   colorCell.green = ((Nlm_Uint2)green << 8) | (Nlm_Uint2)green;
1152   colorCell.blue  = ((Nlm_Uint2)blue  << 8) | (Nlm_Uint2)blue;
1153   colorCell.flags = DoRed | DoGreen | DoBlue;
1154   colorCell.pixel = pixel;
1155   colorCell.pad   = 0;
1156   XStoreColor(Nlm_currentXDisplay, cMap, &colorCell);
1157 #endif
1158 
1159 #ifdef WIN_MSWIN_obsolete
1160   Nlm_WindowData  wdata;
1161   PALETTEENTRY    colorCell;
1162   Nlm_WindoW      w = Nlm_GetParentWindow( gr );
1163 
1164   if (w == NULL  ||
1165       (GetDeviceCaps(Nlm_currentHDC, RASTERCAPS) &  RC_PALETTE) == 0)
1166     return;
1167 
1168   Nlm_GetWindowData (w, &wdata);
1169   if (wdata.cMap == NULL)
1170     return;
1171 
1172   colorCell.peFlags = PC_RESERVED;
1173   colorCell.peRed   = red;
1174   colorCell.peGreen = green;
1175   colorCell.peBlue  = blue;
1176 
1177   AnimatePalette(wdata.cMap, pixel, 1, &colorCell);
1178 #endif
1179 
1180 #if defined(WIN_MAC)  || defined(WIN_MSWIN)
1181   Nlm_InvalObject ((Nlm_Handle) gr);
1182 #endif
1183 }
1184 
1185 
1186 
1187 #ifdef WIN_MOTIF
1188 static Nlm_CharPtr resource_nm = NULL;
Nlm_SetMotifWindowName(Nlm_CharPtr nm)1189 extern void Nlm_SetMotifWindowName (Nlm_CharPtr nm)
1190 {
1191   resource_nm = (Nlm_CharPtr) Nlm_MemFree (resource_nm);
1192   if (nm != NULL && *nm != '\0') {
1193     resource_nm = StringSave (nm);
1194   }
1195 }
1196 
1197 static Nlm_Uchar motif_n_colorcells = (Nlm_Uchar)0;
1198 
Nlm_RestrictMotifColorsTo(Nlm_Uchar n_colorcells)1199 extern void Nlm_RestrictMotifColorsTo (Nlm_Uchar n_colorcells)
1200 {
1201   motif_n_colorcells = n_colorcells;
1202 }
1203 
Nlm_GetFixedColormap(Nlm_GraphiC w,Colormap * cMap,Visual ** visual)1204 extern Nlm_Boolean Nlm_GetFixedColormap(Nlm_GraphiC w,
1205                                         Colormap *cMap, Visual **visual)
1206 {
1207   Nlm_WindowData wdata;
1208   Nlm_GetWindowData(Nlm_ParentWindow( w ), &wdata);
1209   if ( !wdata.cMap_fixed )
1210     return FALSE;
1211 
1212   if ( cMap )
1213     *cMap = wdata.cMap;
1214   if ( visual )
1215     *visual = wdata.visual;
1216 
1217   return TRUE;
1218 }
1219 
Nlm_BusyColormap(unsigned long fromColor,unsigned long toColor,Colormap * cMap,Visual ** visual,int * depth)1220 static Nlm_Boolean Nlm_BusyColormap(unsigned long fromColor,
1221                                     unsigned long toColor,
1222                                     Colormap *cMap,
1223                                     Visual  **visual,
1224                                     int      *depth)
1225 {
1226 #define MAX_toColor 256
1227 #define N_DEF_COL   2
1228   unsigned long nInherit = N_DEF_COL;
1229 
1230   if (!Nlm_hasColor  ||
1231       fromColor < N_DEF_COL  || fromColor > toColor  || toColor > MAX_toColor)
1232     return FALSE;
1233 
1234   if (fromColor == toColor)
1235     { /* Special case:  create new colormap and inherit
1236          first "toColor" color from the system colormap */
1237       nInherit = toColor;
1238     }
1239 
1240   {{ /* Create new colormap, if possible */
1241     XVisualInfo visinfo;
1242     Window      rootwin = RootWindow(Nlm_currentXDisplay, Nlm_currentXScreen);
1243     if ( !XMatchVisualInfo(Nlm_currentXDisplay,Nlm_currentXScreen,
1244                            8,PseudoColor,&visinfo)  &&
1245          !XMatchVisualInfo(Nlm_currentXDisplay,Nlm_currentXScreen,
1246                            8,GrayScale,&visinfo) )
1247       return FALSE;
1248 
1249     *cMap = XCreateColormap(Nlm_currentXDisplay, rootwin, visinfo.visual,
1250                             AllocNone);
1251     if ( visual )
1252       *visual = visinfo.visual;
1253     if ( depth )
1254       *depth = 8;
1255   }}
1256 
1257   {{ /* Allocate colors */
1258     unsigned long  plane_m;
1259     unsigned long  pixels[MAX_toColor];
1260     if ( !XAllocColorCells(Nlm_currentXDisplay, *cMap,
1261                            FALSE, &plane_m, 0, pixels, toColor) )
1262       {
1263         if (*cMap != DefaultColormap(Nlm_currentXDisplay,
1264                                      Nlm_currentXScreen) )
1265           XFreeColormap(Nlm_currentXDisplay, *cMap);
1266         return FALSE;
1267       }
1268   }}
1269 
1270   {{ /* Inherit first "nInherit" colors from the system default colormap */
1271     XColor defColors[MAX_toColor];
1272     unsigned long pixel;
1273     for (pixel = 0;  pixel < nInherit;  pixel++)
1274       defColors[pixel].pixel = pixel;
1275     XQueryColors(Nlm_currentXDisplay,
1276                  DefaultColormap(Nlm_currentXDisplay, Nlm_currentXScreen),
1277                  defColors, nInherit);
1278     XStoreColors(Nlm_currentXDisplay, *cMap, defColors, nInherit);
1279   }}
1280 
1281   {{ /* Free first "fromColor" but the very first "N_DEF_COL" colorcells */
1282     unsigned long pixel;
1283     unsigned long pixels[256];
1284     for(pixel = nInherit;  pixel < fromColor;  pixel++)
1285       {
1286         pixels[pixel-nInherit] = pixel;
1287       }
1288     XFreeColors(Nlm_currentXDisplay, *cMap, pixels, fromColor-nInherit, 0);
1289   }}
1290 
1291   return TRUE;
1292 }
1293 
1294 
Nlm_XGetRGBColormap(VisualID visualid,XStandardColormap * xcmap)1295 static Boolean Nlm_XGetRGBColormap(VisualID visualid, XStandardColormap *xcmap)
1296 { /* Look for an already existing XA_RGB_DEFAULT_MAP */
1297     XStandardColormap *xcmaps;
1298     int i, n_xcmaps;
1299     if ( !XGetRGBColormaps(Nlm_currentXDisplay,
1300                            RootWindow(Nlm_currentXDisplay,
1301                                       Nlm_currentXScreen),
1302                            &xcmaps, &n_xcmaps, XA_RGB_DEFAULT_MAP))
1303       return FALSE;
1304 
1305     for (i = 0;  i < n_xcmaps  &&  xcmaps[i].visualid != visualid;  i++);
1306     if (i == n_xcmaps  ||  !xcmaps[i].red_max  ||  !xcmaps [i].visualid)
1307       return FALSE;
1308 
1309     *xcmap = xcmaps[i];
1310     XFree( xcmaps );
1311     return TRUE;
1312 }
1313 
1314 
1315 static XStandardColormap Nlm_stdColormapRGB;
1316 static Boolean           Nlm_stdGrayScale = False;
1317 
Nlm_StandardColormap(Colormap * cMap,Visual ** visual,int * depth)1318 static Boolean Nlm_StandardColormap(Colormap *cMap,
1319                                     Visual **visual, int *depth)
1320 { /* Try use the XA_RGB_DEFAULT_MAP standard colormap of 8-bit depth */
1321   XVisualInfo visinfo;
1322 
1323   ASSERT ( !Nlm_stdColormapRGB.visualid );
1324 
1325   /* The relevant 8-bit visual */
1326   if ( !XMatchVisualInfo(Nlm_currentXDisplay, Nlm_currentXScreen,
1327                         8, PseudoColor, &visinfo) )
1328     {
1329       if ( !XMatchVisualInfo(Nlm_currentXDisplay, Nlm_currentXScreen,
1330                              8, GrayScale, &visinfo) )
1331         return FALSE;
1332       Nlm_stdGrayScale = True;
1333     }
1334 
1335   /* Use already existing or create new default standard colormap*/
1336   if (Nlm_XGetRGBColormap(visinfo.visualid, &Nlm_stdColormapRGB)  ||
1337       (XmuLookupStandardColormap(Nlm_currentXDisplay, Nlm_currentXScreen,
1338                                  visinfo.visualid, visinfo.depth,
1339                                  XA_RGB_DEFAULT_MAP, False, True)  &&
1340        Nlm_XGetRGBColormap(visinfo.visualid, &Nlm_stdColormapRGB)))
1341     {
1342       *cMap = Nlm_stdColormapRGB.colormap;
1343       if ( visual )
1344         *visual = visinfo.visual;
1345       if ( depth )
1346         *depth = visinfo.depth;
1347       return TRUE;
1348     }
1349 
1350   return FALSE;
1351 }
1352 
1353 
1354 static Boolean Nlm_XrmGetResource2 PROTO((const char *_basename,
1355                                           const char *_resource,
1356                                           XrmValuePtr value));
1357 
1358 extern XVisualInfo * Nlm_GetBestOGLVisual(void); /* in vibslate.c */
1359 
Nlm_VibrantDefaultColormapEx(Visual ** visual,int * depth)1360 static Colormap Nlm_VibrantDefaultColormapEx(Visual **visual, int *depth)
1361 {
1362   static Nlm_Boolean vibrant_cMap_ready = FALSE;
1363   static Colormap    vibrant_cMap;
1364   static Visual     *vibrant_visual = NULL;
1365   static int         vibrant_depth = -1;
1366 
1367   Boolean  privateColormap;
1368   int      nInheritColors = 64;  /* default */
1369   char    *value;
1370 
1371   while ( !vibrant_cMap_ready )
1372     {{ /* THE FIRST TIME ONLY */
1373       vibrant_cMap_ready = TRUE;
1374 
1375       value = Nlm_XrmGetResource( "privateColormap" );
1376       privateColormap = (value != NULL  &&
1377                          (strcmp(value, "true") == 0  ||
1378                           strcmp(value, "yes" ) == 0  ||
1379                           strcmp(value, "on"  ) == 0  ||
1380                           strcmp(value, "1"   ) == 0));
1381       Nlm_MemFree( value );
1382 
1383       if (privateColormap  &&
1384           (value = Nlm_XrmGetResource( "nInheritColors" )) != NULL )
1385         {
1386           int int_value = atoi( value );
1387           Nlm_MemFree( value );
1388 
1389           if (1 < int_value ||  int_value < 256)
1390             nInheritColors = int_value;
1391         }
1392 
1393       /* Compose private colormap, if specified */
1394       if (privateColormap  &&
1395           Nlm_BusyColormap(nInheritColors, nInheritColors,
1396                            &vibrant_cMap, &vibrant_visual, &vibrant_depth))
1397         break;
1398 
1399 #ifdef _OPENGL
1400       /* we want better-than-8-bit color for OpenGL, if possible, so force
1401          vibrant to try those first, using glX selection of a visual/depth
1402          appropriate for OpenGL */
1403       {
1404         XVisualInfo *visinfo = Nlm_GetBestOGLVisual();
1405         if (!visinfo) {
1406           puts("Can't find OpenGL-appropriate visual!");
1407           exit(1);
1408         }
1409         vibrant_visual = visinfo->visual;
1410         vibrant_depth = visinfo->depth;
1411         /* create an X colormap since probably not using default visual */
1412         vibrant_cMap = XCreateColormap(
1413           Nlm_currentXDisplay,
1414           RootWindow(Nlm_currentXDisplay, visinfo->screen), \
1415           visinfo->visual, AllocNone);
1416       }
1417 #else
1418       /* Try use the XA_RGB_DEFAULT_MAP standard colormap of 8-bit depth */
1419       if ( Nlm_StandardColormap(&vibrant_cMap,&vibrant_visual,&vibrant_depth) )
1420         break;
1421 
1422       /* By default, use default colormap and visual */
1423       vibrant_cMap  = DefaultColormap(Nlm_currentXDisplay, Nlm_currentXScreen);
1424       vibrant_visual= DefaultVisual  (Nlm_currentXDisplay, Nlm_currentXScreen);
1425       vibrant_depth = DefaultDepth   (Nlm_currentXDisplay, Nlm_currentXScreen);
1426 #endif /* ! _OPENGL */
1427 
1428     }}
1429 
1430   if ( visual )
1431     *visual = vibrant_visual;
1432   if ( depth )
1433     *depth = vibrant_depth;
1434   return vibrant_cMap;
1435 }
1436 
Nlm_VibrantDefaultColormap(void)1437 extern Colormap Nlm_VibrantDefaultColormap( void )
1438 {
1439   return Nlm_VibrantDefaultColormapEx(NULL, NULL);
1440 }
1441 
Nlm_VibrantDefaultVisual(void)1442 extern Visual *Nlm_VibrantDefaultVisual( void )
1443 {
1444   Visual *visual;
1445   Nlm_VibrantDefaultColormapEx(&visual, NULL);
1446   return visual;
1447 }
1448 
Nlm_VibrantDefaultDepth(void)1449 extern int Nlm_VibrantDefaultDepth( void )
1450 {
1451   int depth;
1452   Nlm_VibrantDefaultColormapEx(NULL, &depth);
1453   return depth;
1454 }
1455 
1456 
Nlm_XAllocColor(XColor * xcolor,Colormap colormap,Nlm_Uint1 red,Nlm_Uint1 green,Nlm_Uint1 blue)1457 extern void Nlm_XAllocColor(XColor *xcolor, Colormap colormap,
1458                             Nlm_Uint1 red, Nlm_Uint1 green, Nlm_Uint1 blue)
1459 {
1460   if (Nlm_currentXDisplay == NULL)
1461     return;
1462 
1463   if (Nlm_stdColormapRGB.visualid  &&  Nlm_stdColormapRGB.colormap == colormap)
1464     { /* Use standard colormap */
1465       if ( Nlm_stdGrayScale )
1466         xcolor->pixel = (30 * red + 59 * green + 11 * blue) *
1467           Nlm_stdColormapRGB.red_max / 25500 * Nlm_stdColormapRGB.red_mult;
1468       else
1469         xcolor->pixel =
1470           ((red   * Nlm_stdColormapRGB.red_max   + 128) / 255) *
1471            Nlm_stdColormapRGB.red_mult   +
1472           ((green * Nlm_stdColormapRGB.green_max + 128) / 255) *
1473           Nlm_stdColormapRGB.green_mult +
1474           ((blue  * Nlm_stdColormapRGB.blue_max  + 128) / 255) *
1475           Nlm_stdColormapRGB.blue_mult;
1476 
1477       xcolor->pixel += Nlm_stdColormapRGB.base_pixel;
1478       XQueryColor(Nlm_currentXDisplay, colormap, xcolor);
1479       return;
1480     }
1481 
1482   xcolor->red   = red   << 8 | red;
1483   xcolor->green = green << 8 | green;
1484   xcolor->blue  = blue  << 8 | blue;
1485 
1486   if (!Nlm_hasColor  ||
1487       !XAllocColor(Nlm_currentXDisplay,
1488                    (colormap != 0) ? colormap : Nlm_VibrantDefaultColormap(),
1489                    xcolor))
1490     {
1491       xcolor->pixel =
1492         (30 * (int)red + 59 * (int)green + 11 * (int)blue < 12800) ?
1493         BlackPixel(Nlm_currentXDisplay, Nlm_currentXScreen) :
1494         WhitePixel(Nlm_currentXDisplay, Nlm_currentXScreen);
1495     }
1496 }
1497 #endif /* WIN_MOTIF */
1498 
1499 
Nlm_SetWindowExtra(Nlm_WindoW w,Nlm_VoidPtr data,Nlm_WndFreeProc cleanup)1500 extern void Nlm_SetWindowExtra (Nlm_WindoW w, Nlm_VoidPtr data, Nlm_WndFreeProc cleanup)
1501 
1502 {
1503   Nlm_WindowData  wdata;
1504 
1505   if (w != NULL) {
1506     Nlm_GetWindowData (w, &wdata);
1507     wdata.data = data;
1508     wdata.cleanup = cleanup;
1509     Nlm_SetWindowData (w, &wdata);
1510   }
1511 }
1512 
Nlm_GetWindowExtra(Nlm_WindoW w)1513 extern Nlm_VoidPtr Nlm_GetWindowExtra (Nlm_WindoW w)
1514 
1515 {
1516   Nlm_WindowData  wdata;
1517 
1518   if (w != NULL) {
1519     Nlm_GetWindowData (w, &wdata);
1520     return wdata.data;
1521   } else {
1522     return NULL;
1523   }
1524 }
1525 
1526 #ifdef WIN_MAC
Nlm_ClearKeys(void)1527 static void Nlm_ClearKeys (void)
1528 {
1529   Nlm_cmmdKey = FALSE;
1530   Nlm_ctrlKey = FALSE;
1531   Nlm_optKey = FALSE;
1532   Nlm_shftKey = FALSE;
1533   Nlm_dblClick = FALSE;
1534 }
1535 #endif
1536 
1537 
Nlm_FindWindowRec(Nlm_WindowTool wptr)1538 static Nlm_WindoW Nlm_FindWindowRec (Nlm_WindowTool wptr)
1539 
1540 {
1541   Nlm_WindoW      w;
1542 #ifdef WIN_MAC
1543   Nlm_WindowData  wdata;
1544 #endif
1545 
1546 #ifdef WIN_MAC
1547   Nlm_GetWindowData (Nlm_systemWindow, &wdata);
1548   wdata.handle = wptr;
1549   Nlm_SetWindowData (Nlm_systemWindow, &wdata);
1550 #endif
1551   w = Nlm_desktopWindow;
1552   while (w != NULL && Nlm_ParentWindowPtr ((Nlm_GraphiC) w) != wptr) {
1553     w = (Nlm_WindoW) Nlm_GetNext ((Nlm_GraphiC) w);
1554   }
1555   if (w == NULL) {
1556     w = Nlm_desktopWindow;
1557   }
1558   return w;
1559 }
1560 
Nlm_SetWindowTitle(Nlm_GraphiC w,Nlm_Int2 item,Nlm_CharPtr title,Nlm_Boolean savePort)1561 static void Nlm_SetWindowTitle (Nlm_GraphiC w, Nlm_Int2 item,
1562                                 Nlm_CharPtr title, Nlm_Boolean savePort)
1563 
1564 
1565 {
1566   Nlm_Char        temp [256];
1567   Nlm_WindowTool  wptr;
1568 #ifdef WIN_MOTIF
1569   Nlm_ShellTool   shl;
1570   Nlm_WindowData  wdata;
1571 #endif
1572 
1573   wptr = Nlm_ParentWindowPtr (w);
1574   Nlm_StringNCpy_0(temp, title, sizeof(temp));
1575 #ifdef WIN_MAC
1576   Nlm_CtoPstr (temp);
1577   SetWTitle (wptr, (StringPtr) temp);
1578 #endif
1579 #ifdef WIN_MSWIN
1580   SetWindowText (wptr, temp);
1581 #endif
1582 #ifdef WIN_MOTIF
1583   shl = Nlm_GetWindowShell ((Nlm_WindoW) w);
1584   if (temp [0] != '\0') {
1585       if(getenv("VIBRANT_USE_APPLICATION_NAME") != NULL && Nlm_StringCmp(getenv("VIBRANT_USE_APPLICATION_NAME"),"0")) {
1586           Nlm_ProgramPath(temp, sizeof(temp));
1587           Nlm_StringNCpy_0(temp,Nlm_StringRChr(temp,'/')+1,sizeof(temp));
1588           Nlm_StringNCat(temp, ": ", sizeof(temp));
1589           Nlm_StringNCat(temp, title, sizeof(temp));
1590       }
1591     XtVaSetValues (shl, XmNtitle, temp, NULL);
1592   } else {
1593     XtVaSetValues (shl, XmNtitle, " ", NULL);
1594   }
1595   XFlush( Nlm_currentXDisplay );
1596   Nlm_GetWindowData ((Nlm_WindoW) w, &wdata);
1597   if (wdata.title != NULL) {
1598     wdata.title = (Nlm_CharPtr) Nlm_MemFree (wdata.title);
1599   }
1600   wdata.title = Nlm_StringSave (title);
1601   Nlm_SetWindowData ((Nlm_WindoW) w, &wdata);
1602 #endif
1603 }
1604 
Nlm_GetWindowTitle(Nlm_GraphiC w,Nlm_Int2 item,Nlm_CharPtr title,size_t maxsize)1605 static void Nlm_GetWindowTitle (Nlm_GraphiC w, Nlm_Int2 item,
1606                                 Nlm_CharPtr title, size_t maxsize)
1607 
1608 {
1609   Nlm_Char        temp [256];
1610   Nlm_WindowTool  wptr;
1611 #ifdef WIN_MOTIF
1612   Nlm_WindowData  wdata;
1613 #endif
1614 
1615   wptr = Nlm_ParentWindowPtr (w);
1616   temp [0] = '\0';
1617 #ifdef WIN_MAC
1618   GetWTitle (wptr, (StringPtr) temp);
1619   Nlm_PtoCstr (temp);
1620 #endif
1621 #ifdef WIN_MSWIN
1622   GetWindowText (wptr, temp, sizeof (temp));
1623 #endif
1624 #ifdef WIN_MOTIF
1625   Nlm_GetWindowData ((Nlm_WindoW) w, &wdata);
1626   Nlm_StringNCpy_0(temp, wdata.title, sizeof(temp));
1627 #endif
1628   Nlm_StringNCpy_0(title, temp, maxsize);
1629 }
1630 
Nlm_MakeWindowLink(Nlm_RectPtr r,Nlm_Int2 recordSize,Nlm_GphPrcs PNTR classPtr)1631 static Nlm_WindoW Nlm_MakeWindowLink (Nlm_RectPtr r, Nlm_Int2 recordSize,
1632                                       Nlm_GphPrcs PNTR classPtr)
1633 
1634 {
1635   Nlm_GraphiC  n;
1636   Nlm_WindoW   w;
1637 
1638   w = (Nlm_WindoW) Nlm_HandNew (recordSize);
1639   if (w != NULL) {
1640     n = Nlm_GetNext ((Nlm_GraphiC) Nlm_desktopWindow);
1641     Nlm_LoadGraphicData ((Nlm_GraphiC) w, n, NULL, NULL, NULL,
1642                          classPtr, NULL, r, TRUE, FALSE, NULL, NULL);
1643     if (Nlm_nextIdNumber < 32767) {
1644       Nlm_nextIdNumber++;
1645     }
1646     Nlm_SetNext ((Nlm_GraphiC) Nlm_desktopWindow, (Nlm_GraphiC) w);
1647   }
1648   return w;
1649 }
1650 
1651 #ifdef WIN_MAC
Nlm_SetUpdateRegion(WindowPtr wptr)1652 extern void Nlm_SetUpdateRegion (WindowPtr wptr)
1653 
1654 {
1655 // QUARTZ_FIXME: what does this do? does it need to do anything?
1656 #ifndef WIN_MAC_QUARTZ
1657   Rect bounds;
1658 
1659   if (wptr != NULL) {
1660     GetPortVisibleRegion(GetWindowPort(wptr), Nlm_updateRgn);
1661     /* HLock ((Handle) Nlm_updateRgn); */
1662     GetRegionBounds(Nlm_updateRgn, &bounds);
1663     Nlm_RectToolToRecT (&bounds, &Nlm_updateRect);
1664     /* HUnlock ((Handle) Nlm_updateRgn); */
1665   }
1666 #endif
1667 }
1668 #endif
1669 
1670 extern Nlm_Int2  Nlm_internalMenuBarHeight;
1671 
1672 #ifdef WIN_MOTIF
WindowCloseCallback(Widget wd,XtPointer client_data,XtPointer call_data)1673 static void WindowCloseCallback(Widget wd,
1674                                 XtPointer client_data, XtPointer call_data)
1675 {
1676   Nlm_WndActnProc  cls;
1677   Nlm_WindoW       w;
1678   Nlm_WindowData   wdata;
1679 
1680   w = (Nlm_WindoW) client_data;
1681   Nlm_GetWindowData (w, &wdata);
1682   cls = wdata.close;
1683   if (cls != NULL) {
1684     cls (w);
1685   }
1686 }
1687 
1688 
ignoreXerror_handler(Display * dpy,XErrorEvent * xerr)1689 static int ignoreXerror_handler(Display *dpy, XErrorEvent *xerr)
1690 {
1691   return 0;
1692 }
1693 
defaultXerror_handler(Display * dpy,XErrorEvent * event)1694 static int defaultXerror_handler(Display *dpy, XErrorEvent *event)
1695 {
1696   char buffer[BUFSIZ];
1697   char mesg[BUFSIZ];
1698   char number[32];
1699   char *mtype = "XlibMessage";
1700 
1701   XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
1702   XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
1703   fprintf(stderr, "%s:  %s\n  ", mesg, buffer);
1704   XGetErrorDatabaseText(dpy, mtype, "MajorCode", "Request Major code %d",
1705                         mesg, BUFSIZ);
1706   fprintf(stderr, mesg, event->request_code);
1707   if (event->request_code < 128) {
1708     sprintf(number, "%d", event->request_code);
1709     XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
1710   } else {
1711     buffer[0] = '\0';
1712   }
1713   fprintf(stderr, " (%s)\n", buffer);
1714 
1715   if (event->request_code >= 128) {
1716     XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
1717                           mesg, BUFSIZ);
1718     fputs("  ", stderr);
1719     fprintf(stderr, mesg, event->minor_code);
1720     fputs("\n", stderr);
1721 
1722     strcpy(buffer, "Value");
1723     XGetErrorDatabaseText(dpy, mtype, buffer, "", mesg, BUFSIZ);
1724     if (mesg[0]) {
1725       fputs("  ", stderr);
1726       fprintf(stderr, mesg, event->resourceid);
1727       fputs("\n", stderr);
1728     }
1729   } else if ((event->error_code == BadWindow) ||
1730              (event->error_code == BadPixmap) ||
1731              (event->error_code == BadCursor) ||
1732              (event->error_code == BadFont) ||
1733              (event->error_code == BadDrawable) ||
1734              (event->error_code == BadColor) ||
1735              (event->error_code == BadGC) ||
1736              (event->error_code == BadIDChoice) ||
1737              (event->error_code == BadValue) ||
1738              (event->error_code == BadAtom)) {
1739     if (event->error_code == BadValue)
1740       XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
1741                             mesg, BUFSIZ);
1742     else if (event->error_code == BadAtom)
1743       XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
1744                             mesg, BUFSIZ);
1745     else
1746       XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
1747                             mesg, BUFSIZ);
1748     fputs("  ", stderr);
1749     fprintf(stderr, mesg, event->resourceid);
1750     fputs("\n", stderr);
1751   }
1752 
1753   XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d",
1754                         mesg, BUFSIZ);
1755   fputs("  ", stderr);
1756   fprintf(stderr, mesg, event->serial);
1757   XGetErrorDatabaseText(dpy, mtype, "CurrentSerial", "Current Serial #%d",
1758                         mesg, BUFSIZ);
1759   fputs("\n  ", stderr);
1760   fprintf(stderr, mesg, (XNextRequest(dpy) - 1));
1761   fputs("\n", stderr);
1762 
1763   return (event->error_code == BadImplementation) ? 0 : 1;
1764 }
1765 
1766 
WindowStructFocusCallback(Widget wd,XtPointer client_data,XEvent * event,Boolean * contin)1767 static void WindowStructFocusCallback (Widget wd, XtPointer client_data,
1768                                        XEvent *event, Boolean *contin)
1769 {
1770   Nlm_WindoW w = (Nlm_WindoW)client_data;
1771 
1772   switch ( event->type )
1773     {
1774     case FocusOut:
1775       {
1776         Nlm_DoDeactivate((Nlm_GraphiC)w, FALSE);
1777         break;
1778       }
1779 
1780     case FocusIn:
1781       {
1782         Nlm_DoActivate((Nlm_GraphiC)w, FALSE);
1783         break;
1784       }
1785 
1786     case MapNotify:
1787       {
1788         if (wd == selectShell)
1789           { /* Set input focus */
1790             XWindowAttributes attr;
1791             Window selectWin = XtWindow( selectShell );
1792 
1793             XGetWindowAttributes(Nlm_currentXDisplay, selectWin, &attr);
1794             if (attr.map_state != IsViewable) {
1795               Nlm_SetVisible((Nlm_GraphiC)w, TRUE);
1796               return;
1797             }
1798 
1799             XSetInputFocus(Nlm_currentXDisplay, selectWin,
1800                            RevertToPointerRoot, CurrentTime);
1801             selectShell = NULL;
1802           }
1803         Nlm_SetVisible((Nlm_GraphiC)w, TRUE);
1804         XtVaSetValues(wd, XtNiconic, (Boolean)False, NULL);
1805         break;
1806       }
1807 
1808     case UnmapNotify:
1809       {
1810         Nlm_SetVisible((Nlm_GraphiC)w, FALSE);
1811         break;
1812       }
1813 
1814     case ConfigureNotify:
1815       { /* Move & Resize */
1816         Nlm_RecT         oldrect;
1817         Nlm_RecT         r;
1818         Nlm_RectTool     rtool;
1819         Window           xw, root, parent, child, *children = NULL;
1820         unsigned int     nchildren;
1821         int              x, y;
1822         Nlm_WindowData   wdata;
1823 
1824         XSetErrorHandler( ignoreXerror_handler );
1825         for (xw = XtWindow( wd );
1826              xw  &&  XQueryTree(Nlm_currentXDisplay, xw, &root, &parent,
1827                                 &children, &nchildren)  &&
1828                root != parent;
1829              xw = parent)
1830           {
1831             XFree( (char *)children );
1832             children = NULL;
1833           }
1834         XSetErrorHandler( defaultXerror_handler );
1835         if ( children )
1836           XFree( (char *)children );
1837 
1838         if (root != parent)
1839           break;
1840 
1841         XTranslateCoordinates(Nlm_currentXDisplay, xw, root,
1842                               (Position)0, (Position)0,
1843                               &x, &y,  &child);
1844 
1845         rtool.x = x;
1846         rtool.y = y;
1847         rtool.width  = event->xconfigure.width;
1848         rtool.height = event->xconfigure.height;
1849         if (Nlm_GetWindowMenuBar( w ) != NULL)
1850           rtool.height -= Nlm_internalMenuBarHeight;
1851 
1852         Nlm_RectToolToRecT(&rtool, &r);
1853         Nlm_GetRect((Nlm_GraphiC)w, &oldrect);
1854         Nlm_SetRect((Nlm_GraphiC)w, &r);
1855         Nlm_GetWindowData(w, &wdata);
1856 
1857         if (wdata.resize != NULL  &&
1858             (oldrect.right  - oldrect.left != r.right  - r.left ||
1859              oldrect.bottom - oldrect.top  != r.bottom - r.top) &&
1860             Nlm_WindowHasBeenShown( w )  &&
1861             Nlm_GetVisible((Nlm_GraphiC) w))
1862           {
1863             wdata.resize( w );
1864           }
1865         break;
1866       }
1867     }
1868 }
1869 
1870 
WindowEnterCallback(Widget wd,XtPointer client_data,XEvent * ev,Boolean * contin)1871 static void WindowEnterCallback(Widget wd, XtPointer client_data,
1872                                 XEvent *ev, Boolean *contin)
1873 {
1874   XCrossingEvent  *event;
1875   Nlm_WindoW       w;
1876   Nlm_WindowData   wdata;
1877 
1878   event = (XCrossingEvent*) ev;
1879   if (event->type != EnterNotify) return;
1880   w = (Nlm_WindoW) client_data;
1881   Nlm_GetWindowData (w, &wdata);
1882   if ( wdata.cMapStatus ) return;
1883   if ( !wdata.cMap )  return;
1884   XInstallColormap(Nlm_currentXDisplay, wdata.cMap);
1885   wdata.cMapStatus = 1;
1886   Nlm_SetWindowData (w, &wdata);
1887 }
1888 
WindowLeaveCallback(Widget wd,XtPointer client_data,XEvent * ev,Boolean * contin)1889 static void WindowLeaveCallback(Widget wd, XtPointer client_data,
1890                                 XEvent *ev, Boolean *contin)
1891 {
1892   XCrossingEvent  *event;
1893   Nlm_WindoW       w;
1894   Nlm_WindowData   wdata;
1895 
1896   event = (XCrossingEvent*) ev;
1897   if (event->type != LeaveNotify) return;
1898   w = (Nlm_WindoW) client_data;
1899   Nlm_GetWindowData (w, &wdata);
1900   if ( wdata.cMapStatus == 0 ) return;
1901   XUninstallColormap(Nlm_currentXDisplay, wdata.cMap);
1902   wdata.cMapStatus = 0;
1903   Nlm_SetWindowData (w, &wdata);
1904 }
1905 
1906 
1907 static GC vibrant_GC[25];
1908 
Nlm_GetGC(int depth)1909 static GC Nlm_GetGC(int depth)
1910 {
1911   ASSERT ( 0 <= depth  &&  depth < sizeof(vibrant_GC)/sizeof(GC) );
1912   if ( !vibrant_GC[depth] )
1913     {
1914       Pixmap fake_pixmap = XCreatePixmap(Nlm_currentXDisplay,
1915                                          RootWindow(Nlm_currentXDisplay,
1916                                                     Nlm_currentXScreen),
1917                                          4, 4, depth);
1918       vibrant_GC[depth] = XCreateGC(Nlm_currentXDisplay, fake_pixmap, 0, NULL);
1919       XFreePixmap(Nlm_currentXDisplay, fake_pixmap);
1920     }
1921 
1922   return vibrant_GC[depth];
1923 }
1924 
Nlm_FreeGCs(void)1925 static void Nlm_FreeGCs( void )
1926 {
1927   size_t depth;
1928   for (depth = 0;  depth < sizeof(vibrant_GC)/sizeof(GC);  depth++)
1929     if ( vibrant_GC[depth] )
1930       {
1931         XFreeGC(Nlm_currentXDisplay, vibrant_GC[depth]);
1932         vibrant_GC[depth] = NULL;
1933       }
1934 }
1935 #endif
1936 
1937 
1938 
1939 #ifdef WIN_MSWIN
client2win(Nlm_WindoW w,Nlm_RectPtr client_rect,LPRECT win_rect)1940 static void client2win(Nlm_WindoW w,
1941                        Nlm_RectPtr client_rect, LPRECT win_rect)
1942 {
1943   if (client_rect == NULL  ||  win_rect == NULL)
1944     return;
1945 
1946   win_rect->left   = client_rect->left;
1947   win_rect->top    = client_rect->top;
1948   win_rect->right  = client_rect->right;
1949   win_rect->bottom = client_rect->bottom;
1950 
1951   if (w == NULL)
1952     return;
1953 
1954   {{
1955   Nlm_Int2 dXleft, dXright, dYtop, dYbottom;
1956   Nlm_Int2 dX = 0, dY = 0;
1957 
1958   Nlm_WindowData wdata;
1959   Nlm_GetWindowData(w, &wdata);
1960 
1961   AdjustWindowRectEx(win_rect, wdata.style, (wdata.menuBar != NULL),
1962                      wdata.ex_style);
1963 
1964   dXleft  = (Nlm_Int2)(screenBitBounds.left  - win_rect->left );
1965   dXright = (Nlm_Int2)(screenBitBounds.right - win_rect->right);
1966   if (dXleft > 0)
1967     dX = dXleft;
1968   else if (dXright < 0)
1969     dX = dXright;
1970 
1971   dYtop    = (Nlm_Int2)(screenBitBounds.top    - win_rect->top   );
1972   dYbottom = (Nlm_Int2)(screenBitBounds.bottom - win_rect->bottom);
1973   if (dYtop > 0)
1974     dY = dYtop;
1975   else if (dYbottom < 0)
1976     dY = dYbottom;
1977 
1978   if (dX == 0  &&  dY == 0)
1979     return;
1980 
1981   Nlm_OffsetRect(client_rect, dX, dY);
1982   win_rect->left   += dX;
1983   win_rect->top    += dY;
1984   win_rect->right  += dX;
1985   win_rect->bottom += dY;
1986   }}
1987 }
1988 
1989 
win2client(Nlm_WindoW w,LPRECT win_rect,Nlm_RectPtr client_rect)1990 static void win2client(Nlm_WindoW w,
1991                        LPRECT win_rect, Nlm_RectPtr client_rect)
1992 {
1993   RECT xclient_rect;
1994 
1995   ASSERT ( w           != NULL );
1996   ASSERT ( client_rect != NULL );
1997   ASSERT ( win_rect    != NULL );
1998 
1999   xclient_rect.left   = (Nlm_Int2)
2000     ((screenBitBounds.right - screenBitBounds.left) / 2 - 2);
2001   xclient_rect.right  = (Nlm_Int2)(xclient_rect.left + 4);
2002   xclient_rect.top    = (Nlm_Int2)
2003     ((screenBitBounds.bottom - screenBitBounds.top) / 2 - 2);
2004   xclient_rect.bottom = (Nlm_Int2)(xclient_rect.top + 4);
2005 
2006 
2007   {{
2008   RECT xwin_rect = xclient_rect;
2009   int  dXleft, dXright, dYtop, dYbottom;
2010 
2011   Nlm_WindowData wdata;
2012   Nlm_GetWindowData(w, &wdata);
2013 
2014   AdjustWindowRectEx(&xwin_rect, wdata.style, (wdata.menuBar != NULL),
2015                      wdata.ex_style);
2016 
2017   dXleft   = xclient_rect.left - xwin_rect.left;
2018   dXright  = (xwin_rect.right - xwin_rect.left) -
2019              (xclient_rect.right - xclient_rect.left) - dXleft;
2020   dYtop    = xclient_rect.top  - xwin_rect.top;
2021   dYbottom = (xwin_rect.bottom - xwin_rect.top) -
2022              (xclient_rect.bottom - xclient_rect.top) - dYtop;
2023 
2024   client_rect->left   = (Nlm_Int2)(win_rect->left   + dXleft  );
2025   client_rect->right  = (Nlm_Int2)(win_rect->right  - dXright );
2026   client_rect->top    = (Nlm_Int2)(win_rect->top    + dYtop   );
2027   client_rect->bottom = (Nlm_Int2)(win_rect->bottom - dYbottom);
2028   }}
2029 }
2030 #endif
2031 
2032 #ifdef WIN_MAC_QUARTZ
2033 // QUARTZ_FIXME: this stuff is probably not needed
2034 //static void Nlm_WindowSetProperty (WindowRef wptr, PropertyTag tag, void *ptr)
2035 //{
2036 //  SetWindowProperty (wptr, 'NCBI', tag, sizeof(ptr), &ptr);
2037 //}
2038 //
2039 //static void *Nlm_WindowGetProperty (WindowRef wptr, PropertyTag tag)
2040 //{
2041 //  void *ret = 0;
2042 //  GetWindowProperty (wptr, 'NCBI', tag, sizeof(ret), 0, &ret);
2043 //  return ret;
2044 //}
2045 //
2046 //static void Nlm_EnsureQuartzPort (CGContextRef *ctxPtr, HIViewRef imageView)
2047 //{
2048 //  /* note: the context must actually be a bitmap context, this
2049 //      is guaranteed since this function is the only one that
2050 //      gets to make it anyway */
2051 //  HIRect bounds;
2052 //  HIViewGetBounds (imageView, &bounds);
2053 //  int good = *ctxPtr != 0;
2054 //  if (good)
2055 //  {
2056 //
2057 //    if (bounds.size.width != CGBitmapContextGetWidth (*ctxPtr))
2058 //      good = 0;
2059 //    if (bounds.size.height != CGBitmapContextGetHeight (*ctxPtr))
2060 //      good = 0;
2061 //  }
2062 //  if (!good)
2063 //  {
2064 //    CGContextRelease (*ctxPtr);
2065 //
2066 //    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
2067 //    *ctxPtr = CGBitmapContextCreate (0, bounds.size.width, bounds.size.height, 32, 0, colorSpace, kCGImageAlphaNoneSkipLast);
2068 //    CGColorSpaceRelease (colorSpace);
2069 //  }
2070 //}
2071 //
2072 //CGContextRef Nlm_GetQuartzWindowPort (WindowRef wptr)
2073 //{
2074 //  HIViewRef imageView = Nlm_WindowGetProperty (wptr, 'imgv');
2075 //  CGContextRef ref = Nlm_WindowGetProperty (wptr, 'cntx');
2076 //  CGContextRef old = ref;
2077 //  Nlm_EnsureQuartzPort (&ref, imageView);
2078 //  if (ref != old)
2079 //    Nlm_WindowSetProperty (wptr, 'cntx', ref);
2080 //  return ref;
2081 //}
2082 
Nlm_RootHIViewRedrawHandler(EventHandlerCallRef handler,EventRef event,void * userData)2083 static OSStatus Nlm_RootHIViewRedrawHandler (EventHandlerCallRef handler, EventRef event, void *userData)
2084 {
2085   Nlm_WindoW w = userData;
2086 
2087   CGContextRef ctx;
2088   GetEventParameter (event,
2089                      kEventParamCGContextRef,
2090                      typeCGContextRef,
2091                      NULL,
2092                      sizeof (CGContextRef),
2093                      NULL,
2094                      &ctx);
2095   Nlm_SetPort (ctx);
2096   Nlm_SetPortWindowPort (Nlm_ParentWindowPtr ((Nlm_GraphiC)w));
2097   Nlm_DrawWindow ((Nlm_GraphiC)w, 0);
2098   Nlm_SetPortWindowPort (NULL);
2099   Nlm_SetPort (NULL);
2100 
2101   return noErr;
2102 }
2103 
2104 #endif
2105 
2106 #define DOCUMENT_STYLE 1
2107 #define FIXED_STYLE    2
2108 #define FROZEN_STYLE   3
2109 #define ROUND_STYLE    4
2110 #define ALERT_STYLE    5
2111 #define MODAL_STYLE    6
2112 #define FLOATING_STYLE 7
2113 #define SHADOW_STYLE   8
2114 #define PLAIN_STYLE    9
2115 
Nlm_NewWindow(Nlm_WindoW w,Nlm_Int2 type,Nlm_Int2 procID,Nlm_Boolean goAway,Nlm_CharPtr windowClass,Nlm_Uint4 style,Nlm_CharPtr title,Nlm_WndActnProc close,Nlm_WndActnProc resize)2116 static void Nlm_NewWindow (Nlm_WindoW w, Nlm_Int2 type, Nlm_Int2 procID,
2117                            Nlm_Boolean goAway, Nlm_CharPtr windowClass,
2118                            Nlm_Uint4 style, Nlm_CharPtr title,
2119                            Nlm_WndActnProc close, Nlm_WndActnProc resize)
2120 {
2121   Nlm_ShellTool   dlg;
2122   Nlm_RecT        drag;
2123   Nlm_RecT        grow;
2124   Nlm_Int2        hsb;
2125   Nlm_MainTool    man;
2126   Nlm_Int2        margin;
2127   Nlm_RecT        p;
2128   Nlm_PortTool    prt;
2129   Nlm_PoinT       pt;
2130   Nlm_RecT        r;
2131   Nlm_ShellTool   shl;
2132   Nlm_Int2        spacing;
2133   Nlm_Char        temp [256];
2134   Nlm_CharPtr     ttl;
2135   Nlm_Int2        vsb;
2136   Nlm_WindowTool  wptr;
2137   Nlm_RecT        zoom;
2138 #ifdef WIN_MAC
2139   Nlm_WindowTool  behindNone;
2140   CCTabHandle     colorHandle;
2141   CCTabPtr        colorPtr;
2142   Nlm_RectTool    rtool;
2143   WindowClass     winClass = 0;
2144 #if TARGET_API_MAC_CARBON
2145   WindowAttributes winAttrs = kWindowStandardHandlerAttribute;
2146 #else
2147   WindowAttributes winAttrs = kWindowCompositingAttribute; /* kWindowStandardHandlerAttribute; */
2148 #endif
2149   OSStatus        osErr;
2150 #endif
2151 #ifdef WIN_MOTIF
2152   Atom            atom;
2153   Cardinal        n;
2154   Arg             wargs[32];
2155   int             screen_num;
2156   int             depth = -1;
2157   Visual         *visual;
2158   Colormap        cMap;
2159   Nlm_Boolean     is_cMap = FALSE;
2160 #endif
2161 #ifdef WIN_MSWIN
2162   Nlm_Uint4       ex_style = 0;
2163 #endif
2164 
2165   Nlm_StringNCpy_0(temp, title, sizeof(temp));
2166 
2167 #ifdef WIN_MAC
2168   Nlm_CtoPstr (temp);
2169 #endif
2170 
2171   Nlm_LoadRect (&r, 50, 50, 150, 150);
2172 
2173 #ifdef WIN_MOTIF
2174   r = Nlm_screenRect;
2175   Nlm_InsetRect (&r, 50, 50);
2176 #endif
2177 
2178   Nlm_GetRect ((Nlm_GraphiC) w, &p);
2179   margin = 10;
2180   spacing = 10;
2181   if (p.right < 0)
2182     margin  = (Nlm_Int2)(-p.right);
2183   if (p.bottom < 0)
2184     spacing = (Nlm_Int2)(-p.bottom);
2185   if (p.right >= 0 || p.bottom >= 0)
2186     {
2187       r = p;
2188       r.bottom = (Nlm_Int2)(r.top  + r.bottom);
2189       r.right  = (Nlm_Int2)(r.left + r.right );
2190     }
2191   Nlm_LoadPt (&pt, margin, margin);
2192   shl  = (Nlm_ShellTool) 0;
2193   dlg  = (Nlm_ShellTool) 0;
2194   wptr = (Nlm_WindowTool) 0;
2195   prt  = NULL;
2196   man  = (Nlm_MainTool) 0;
2197   ttl  = NULL;
2198   hsb  = 0;
2199   vsb  = 0;
2200 
2201 #ifdef WIN_MAC
2202     switch (type) {
2203       case  DOCUMENT_STYLE:
2204         winClass = kDocumentWindowClass;
2205         winAttrs |= kWindowStandardDocumentAttributes;
2206         break;
2207       case  FIXED_STYLE:
2208       case  ROUND_STYLE:
2209         winClass = kDocumentWindowClass;
2210         winAttrs |= kWindowCloseBoxAttribute | kWindowCollapseBoxAttribute;
2211         break;
2212       case  MODAL_STYLE:
2213       case  ALERT_STYLE:
2214         winClass = kMovableModalWindowClass;
2215         winAttrs = kWindowNoAttributes;
2216         break;
2217       case  FROZEN_STYLE:
2218         winClass = kMovableModalWindowClass;
2219         winAttrs |= kWindowCloseBoxAttribute;
2220         break;
2221       case  FLOATING_STYLE:
2222         winClass = kFloatingWindowClass;
2223         winAttrs |= kWindowStandardFloatingAttributes;
2224         break;
2225       case  PLAIN_STYLE:
2226       case  SHADOW_STYLE:
2227         winClass = kPlainWindowClass;
2228         winAttrs |= kWindowNoAttributes;
2229         break;
2230     }
2231 
2232   behindNone = (Nlm_WindowTool) (-1);
2233 #ifndef WIN_MAC_QUARTZ
2234   TextFont (0);
2235   TextSize (0);
2236 #endif
2237   Nlm_RecTToRectTool (&r, &rtool);
2238   if (close == NULL) {
2239     goAway = FALSE;
2240   }
2241 #if 1 /* CreateNewWindow does not set window title */
2242   if (hasColorQD) {
2243     wptr = (WindowPtr) NewCWindow (NULL, &rtool, (StringPtr) temp,
2244                                    FALSE, procID,(Nlm_WindowTool) behindNone,
2245                                    goAway, 0);
2246   } else {
2247     wptr = NewWindow (NULL, &rtool, (StringPtr) temp, FALSE, procID,
2248                       (Nlm_WindowTool) behindNone, goAway, 0);
2249   }
2250 #else
2251   if (! goAway) {
2252     winAttrs &= ~kWindowCloseBoxAttribute;
2253   }
2254   osErr = CreateNewWindow(winClass, winAttrs, &rtool, &wptr);
2255   if (osErr != noErr) {
2256     wptr = NULL;
2257     return;
2258   }
2259 #endif
2260 #ifdef WIN_MAC_QUARTZ
2261   HIViewRef root = HIViewGetRoot (wptr);
2262   HIViewRef content;
2263   HIViewFindByID (root, kHIViewWindowContentID, &content);
2264 
2265   EventTypeSpec updateEventSpec[] = { kEventClassControl, kEventControlDraw };
2266   InstallEventHandler (GetControlEventTarget (content),
2267                        Nlm_RootHIViewRedrawHandler,
2268                        GetEventTypeCount (updateEventSpec),
2269                        updateEventSpec,
2270                        w,
2271                        NULL);
2272 
2273 // QUARTZ_FIXME: unneeded unless we really do need the triple buffer
2274 //  HIViewRef imageView;
2275 //  HIImageViewCreate (0, &imageView);
2276 //
2277 //  HIViewAddSubview (root, imageView);
2278 //  HIRect bounds;
2279 //  HIViewGetBounds (root, &bounds);
2280 //  HIViewSetFrame (imageView, &bounds);
2281 //
2282 //  HILayoutInfo layout;
2283 //  layout.version = kHILayoutInfoVersionZero;
2284 //
2285 //  HIViewGetLayoutInfo (imageView, &layout);
2286 //  layout.binding.top.toView = root;
2287 //  layout.binding.top.kind = kHILayoutBindTop;
2288 //  layout.binding.bottom.toView = root;
2289 //  layout.binding.bottom.kind = kHILayoutBindBottom;
2290 //  layout.binding.left.toView = root;
2291 //  layout.binding.left.kind = kHILayoutBindLeft;
2292 //  layout.binding.right.toView = root;
2293 //  layout.binding.right.kind = kHILayoutBindRight;
2294 //  HIViewSetLayoutInfo (imageView, &layout);
2295 //  HIViewApplyLayout (imageView);
2296 //  HIViewSetVisible (imageView, 1);
2297 //
2298 //  char *path = "/Users/mikeash/bob_eggleton_adeepnessinthesky.jpg";
2299 //  CFURLRef url = CFURLCreateFromFileSystemRepresentation (NULL, (const UInt8 *)path, strlen (path), 0);
2300 //  CGDataProviderRef provider = CGDataProviderCreateWithURL (url);
2301 //  CGImageRef img = CGImageCreateWithJPEGDataProvider (provider, NULL, 1, kCGRenderingIntentDefault);
2302 //  HIImageViewSetImage (imageView, img);
2303 
2304 //  Nlm_WindowSetProperty (wptr, 'imgv', imageView);
2305 
2306 #else
2307   Nlm_SetPortWindowPort(wptr);
2308   Nlm_currentWindowTool = wptr;
2309   prt = (Nlm_PortTool) wptr;
2310   Nlm_SetUpdateRegion (wptr);
2311   PenNormal ();
2312   Nlm_MoveTo (2, 10);
2313   if (hasColorQD) {
2314     colorHandle = (CCTabHandle) GetResource ('cctb', 0);
2315     if (colorHandle != NULL) {
2316       LoadResource ((Handle) colorHandle);
2317       if (ResError () == noErr) {
2318         HLock ((Handle) colorHandle);
2319         colorPtr = *(colorHandle);
2320         if (colorPtr != NULL) {
2321           RGBForeColor (&(colorPtr->ctTable [2].rgb));
2322         }
2323         HUnlock ((Handle) colorHandle);
2324       }
2325     }
2326     GetForeColor (&Nlm_RGBforeColor);
2327     GetBackColor (&Nlm_RGBbackColor);
2328   }
2329 #endif
2330 #endif
2331 
2332 #ifdef WIN_MSWIN
2333   if (close == NULL)
2334     style &= ~WS_SYSMENU;
2335 
2336   switch ( type )
2337     {
2338     case FLOATING_STYLE:
2339       ex_style = WS_EX_TOPMOST;
2340       break;
2341     case MODAL_STYLE:
2342       ex_style = WS_EX_DLGMODALFRAME;
2343       break;
2344     default:
2345       ex_style = 0;
2346     }
2347 
2348   wptr = CreateWindowEx(ex_style, windowClass, temp, style,
2349                         r.left, r.top, (r.right - r.left),
2350                         (r.bottom - r.top), NULL, NULL,
2351                         Nlm_currentHInst, NULL);
2352 
2353   if ( wptr )
2354     SetProp(wptr, (LPSTR)"Nlm_VibrantProp", (Nlm_HandleTool)w);
2355 
2356   prt = GetDC( wptr );
2357   ReleaseDC(wptr, prt);
2358   Nlm_currentHDC  = prt;
2359   Nlm_currentHWnd = wptr;
2360 #endif
2361 
2362 #ifdef WIN_MOTIF
2363   n = 0;
2364   if (temp [0] != '\0') {
2365     if(getenv("VIBRANT_USE_APPLICATION_NAME") != NULL && Nlm_StringCmp(getenv("VIBRANT_USE_APPLICATION_NAME"),"0")) {
2366       Nlm_ProgramPath(temp, sizeof(temp));
2367       Nlm_StringNCpy_0(temp,Nlm_StringRChr(temp,'/')+1,sizeof(temp));
2368       Nlm_StringNCat(temp, ": ", sizeof(temp));
2369       Nlm_StringNCat(temp, title, sizeof(temp));
2370     }
2371     XtSetArg (wargs[n], XmNtitle, temp); n++;
2372   } else {
2373     XtSetArg (wargs[n], XmNtitle, " "); n++;
2374   }
2375   XtSetArg (wargs[n], XmNx, (Position) r.left); n++;
2376   XtSetArg (wargs[n], XmNy, (Position) r.top); n++;
2377   XtSetArg (wargs[n], XmNdefaultFontList, Nlm_XfontList); n++;
2378   XtSetArg (wargs[n], XmNdeleteResponse,   XmDO_NOTHING); n++;
2379   XtSetArg (wargs[n], XmNallowShellResize, (Boolean)FALSE); n++;
2380 
2381 #ifndef _OPENGL
2382   if (motif_n_colorcells != 0  &&
2383       Nlm_BusyColormap(motif_n_colorcells, 256, &cMap, &visual, &depth))
2384     is_cMap = TRUE;
2385   else
2386 #endif
2387     cMap = Nlm_VibrantDefaultColormapEx(&visual, &depth);
2388   motif_n_colorcells = 0;
2389 
2390   XtSetArg(wargs[n], XtNdepth,    depth );  n++;
2391   XtSetArg(wargs[n], XtNvisual,   visual);  n++;
2392   XtSetArg(wargs[n], XtNcolormap, cMap  );  n++;
2393 
2394   shl = XtAppCreateShell ((String) resource_nm, (String) "Vibrant",
2395                           applicationShellWidgetClass,
2396                           Nlm_currentXDisplay, wargs, n);
2397 
2398   dlg = shl;
2399   if ( dlg ) {
2400     XtSetMappedWhenManaged (shl, FALSE);
2401 #ifdef _DEBUG
2402     XtAddEventHandler(shl,
2403                       (EventMask) 0, True,
2404                       (XtEventHandler) _XEditResCheckMessages, NULL);
2405 #endif
2406     XtAddEventHandler (shl, StructureNotifyMask|FocusChangeMask, FALSE,
2407                        WindowStructFocusCallback, (XtPointer) w);
2408     XtAddEventHandler (shl, EnterWindowMask, FALSE,
2409                        WindowEnterCallback, (XtPointer) w);
2410     XtAddEventHandler (shl, LeaveWindowMask, FALSE,
2411                        WindowLeaveCallback, (XtPointer) w);
2412 
2413     n = 0;
2414     XtSetArg (wargs[n], XmNwidth,  (Dimension) (r.right - r.left)); n++;
2415     XtSetArg (wargs[n], XmNheight, (Dimension) (r.bottom - r.top)); n++;
2416     XtSetArg (wargs[n], XmNmarginHeight, (Dimension)0); n++;
2417     XtSetArg (wargs[n], XmNmarginWidth,  (Dimension)0); n++;
2418     XtSetArg (wargs[n], XmNborderWidth,  (Dimension)0); n++;
2419     man = XmCreateMainWindow (dlg, (String) "", wargs, n);
2420 
2421     n = 0;
2422     XtSetArg (wargs[n], XmNwidth,  (Dimension) (r.right - r.left)); n++;
2423     XtSetArg (wargs[n], XmNheight, (Dimension) (r.bottom - r.top)); n++;
2424     XtSetArg (wargs[n], XmNbuttonFontList, Nlm_XfontList); n++;
2425     XtSetArg (wargs[n], XmNlabelFontList,  Nlm_XfontList); n++;
2426     XtSetArg (wargs[n], XmNtextFontList,   Nlm_XfontList); n++;
2427     XtSetArg (wargs[n], XmNmarginHeight, (Dimension)0); n++;
2428     XtSetArg (wargs[n], XmNmarginWidth,  (Dimension)0); n++;
2429     XtSetArg (wargs[n], XmNborderWidth,  (Dimension)0); n++;
2430     wptr = XmCreateBulletinBoard (man, (String) "", wargs, n);
2431 
2432     screen_num = DefaultScreen (Nlm_currentXDisplay);
2433     prt = Nlm_GetGC( depth );
2434     XtManageChild (man);
2435     XtManageChild (wptr);
2436 
2437     atom = XmInternAtom (Nlm_currentXDisplay, "WM_DELETE_WINDOW", TRUE);
2438     if (atom != 0) {
2439       XmAddWMProtocolCallback (shl, atom, WindowCloseCallback, (XtPointer) w);
2440     }
2441   }
2442 
2443   if (temp [0] != '\0') {
2444     ttl = Nlm_StringSave (temp);
2445   }
2446 #endif
2447 
2448   Nlm_currentWindowTool = wptr;
2449   r = screenBitBounds;
2450   Nlm_LoadRect (&drag, (Nlm_Int2)(r.left  + 4), (Nlm_Int2)(r.top    + 24),
2451                        (Nlm_Int2)(r.right - 4), (Nlm_Int2)(r.bottom -  4));
2452   Nlm_LoadRect (&grow, r.left, (Nlm_Int2)(r.top + 24), r.right, r.bottom);
2453   Nlm_LoadRect (&zoom, (Nlm_Int2)(r.left  + 4), (Nlm_Int2)(r.top    + 40),
2454                        (Nlm_Int2)(r.right - 4), (Nlm_Int2)(r.bottom -  4));
2455   Nlm_LoadRect (&r, 0, 0, 0, 0);
2456 
2457 #ifdef DCLAP
2458 #ifdef WIN_MSWIN
2459   hsb = 6; vsb = 4;  /* dgg -- patch for mswin sizebar space */
2460 #endif
2461 #endif
2462 
2463   Nlm_LoadBoxData ((Nlm_BoX) w, pt, pt, pt, margin, margin,
2464                    (Nlm_Int2)(margin + hsb), (Nlm_Int2)(margin + vsb),
2465                    spacing, spacing, 0, 0);
2466   Nlm_LoadWindowData (w, wptr, shl, prt, man, &drag, &grow, &zoom,
2467                       close, NULL, NULL, resize, NULL, NULL, NULL,
2468                       TRUE, FALSE, ttl, NULL, NULL, NULL);
2469 
2470 
2471 #ifdef WIN_MSWIN
2472   {{
2473   Nlm_WindowData wdata;
2474   Nlm_GetWindowData(w, &wdata);
2475   wdata.style    = style;
2476   wdata.ex_style = ex_style;
2477   wdata.haccel   = 0;
2478   Nlm_SetWindowData(w, &wdata);
2479   }}
2480 #endif
2481 #ifdef WIN_MOTIF
2482   if ( is_cMap )
2483     {
2484       Nlm_WindowData wdata;
2485       Nlm_GetWindowData(w, &wdata);
2486       wdata.cMap       = cMap;
2487       wdata.visual     = visual;
2488       wdata.cMap_fixed = TRUE;
2489       Nlm_SetWindowData(w, &wdata);
2490     }
2491   Nlm_UseWindow( w );
2492 #endif
2493 }
2494 
2495 
2496 #ifdef WIN_MAC
Nlm_NextVisWindow(Nlm_WindoW w)2497 static Nlm_WindoW Nlm_NextVisWindow (Nlm_WindoW w)
2498 
2499 {
2500   WindowPtr  p;
2501   WindowPtr  q = NULL;
2502 
2503   if (w == Nlm_desktopWindow) {
2504     return Nlm_desktopWindow;
2505   } else {
2506     p = Nlm_ParentWindowPtr((Nlm_GraphiC) w);
2507     while (p != NULL) {
2508       q = GetNextWindow(p);
2509       if (q != NULL) {
2510         if (IsWindowVisible(q)) {
2511           p = NULL;
2512         } else {
2513           p = q;
2514         }
2515       } else {
2516         p = q;
2517       }
2518     }
2519     return Nlm_FindWindowRec ((Nlm_WindowTool) q);
2520   }
2521 }
2522 #endif
2523 
Nlm_HideWindow(Nlm_GraphiC w,Nlm_Boolean setFlag,Nlm_Boolean savePort)2524 static void Nlm_HideWindow (Nlm_GraphiC w, Nlm_Boolean setFlag, Nlm_Boolean savePort)
2525 {
2526   Nlm_WindowTool  wptr;
2527 #ifdef WIN_MOTIF
2528   Nlm_ShellTool   shl;
2529 #endif
2530 
2531   wptr = Nlm_ParentWindowPtr (w);
2532   Nlm_SetVisible (w, FALSE);
2533 #ifdef WIN_MAC
2534   HideWindow (wptr);
2535   if (appWndDeact != NULL) {
2536     appWndDeact ((Nlm_WindoW) w);
2537   }
2538   Nlm_Update ();
2539 #endif
2540 #ifdef WIN_MSWIN
2541   ShowWindow (wptr, SW_HIDE);
2542   if (appWndDeact != NULL) {
2543     appWndDeact ((Nlm_WindoW) w);
2544   }
2545   Nlm_Update ();
2546 #endif
2547 #ifdef WIN_MOTIF
2548   shl = Nlm_GetWindowShell ((Nlm_WindoW) w);
2549   if (!NLM_QUIET  ||  Nlm_WindowHasBeenShown( (Nlm_WindoW)w ))
2550     {
2551       Window focus_win;
2552       int revert_to;
2553       XGetInputFocus(Nlm_currentXDisplay, &focus_win, &revert_to);
2554       if (focus_win == XtWindow( shl ))
2555         {
2556           XSetInputFocus(Nlm_currentXDisplay,
2557                          PointerRoot, RevertToNone, CurrentTime);
2558           XSync(Nlm_currentXDisplay, FALSE);
2559         }
2560       if ( Nlm_IconicWindow( (Nlm_WindoW)w ) )
2561         {
2562           XtVaSetValues(shl, XtNiconic, (Boolean)True,  NULL);
2563           XtVaSetValues(shl, XtNiconic, (Boolean)False, NULL);
2564           Nlm_Update();
2565         }
2566       XtUnmapWidget( shl );
2567     }
2568   Nlm_Update ();
2569 #endif
2570 }
2571 
2572 /* Thanks to Mike Isaev for suggesting this solution to modal windows. */
2573 
Nlm_EnableOneWindow(Nlm_WindoW w,Nlm_Boolean onOffFlag)2574 static void Nlm_EnableOneWindow (Nlm_WindoW w, Nlm_Boolean onOffFlag)
2575 
2576 {
2577 #ifdef WIN_MSWIN
2578   Nlm_WindowTool  wptr;
2579 #endif
2580 #ifdef WIN_MOTIF
2581   Nlm_ShellTool   shl;
2582 #endif
2583 
2584   if (w != NULL && w != Nlm_desktopWindow && w != Nlm_systemWindow) {
2585 #ifdef WIN_MSWIN
2586     wptr = Nlm_GetWindowPtr (w);
2587     EnableWindow (wptr, onOffFlag);
2588 #endif
2589 #ifdef WIN_MOTIF
2590     shl = Nlm_GetWindowShell (w);
2591     XtSetSensitive(shl, onOffFlag);
2592 #endif
2593   }
2594 }
2595 
Nlm_EnableOtherWindows(Nlm_WindoW notThisWindow,Nlm_Boolean onOffFlag)2596 static void Nlm_EnableOtherWindows (Nlm_WindoW notThisWindow, Nlm_Boolean onOffFlag)
2597 
2598 {
2599   Nlm_WindoW  w;
2600 
2601   w = (Nlm_WindoW) Nlm_GetNext ((Nlm_GraphiC) Nlm_desktopWindow);
2602   while (w != NULL && w != Nlm_systemWindow) {
2603     if (w != notThisWindow) {
2604       Nlm_EnableOneWindow (w, onOffFlag);
2605     }
2606     w = (Nlm_WindoW) Nlm_GetNext ((Nlm_GraphiC) w);
2607   }
2608 }
2609 
2610 /* esl++ */
Nlm_IsWindowModal(Nlm_WindoW w)2611 extern Nlm_Boolean Nlm_IsWindowModal (Nlm_WindoW w)
2612 {
2613   Nlm_GphPrcsPtr  classPtr;
2614 
2615   if (w != NULL) {
2616     classPtr = Nlm_GetClassPtr ((Nlm_GraphiC) w);
2617     if (classPtr == modalProcs || classPtr == movableModalProcs) return TRUE;
2618   }
2619   return FALSE;
2620 }
2621 
2622 /* esl++ */
Nlm_HideAndActivateOwner(Nlm_WindoW w,Nlm_WindoW owner)2623 static void Nlm_HideAndActivateOwner (Nlm_WindoW w, Nlm_WindoW owner)
2624 {
2625   /* first of all, select owner window */
2626   Nlm_Select ((Nlm_GraphiC) owner);
2627   /* then hide window */
2628   if (w != NULL && w != Nlm_desktopWindow) {
2629     Nlm_WindowTool wptr = Nlm_ParentWindowPtr ((Nlm_GraphiC) w);
2630     if (wptr == Nlm_currentWindowTool) {
2631       Nlm_HideWindow ((Nlm_GraphiC) w, FALSE, FALSE);
2632       return;
2633     }
2634     if (wptr  &&  !Nlm_IsWindowDying((Nlm_WindoW) w)) {
2635       Nlm_SetVisible ((Nlm_GraphiC) w, FALSE);
2636 #ifdef WIN_MAC
2637       ShowHide (wptr, FALSE);
2638       Nlm_Update ();
2639 #endif
2640 #ifdef WIN_MSWIN
2641       SetWindowPos (wptr, NULL, 0, 0, 0, 0,
2642                     SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE|
2643                     SWP_NOZORDER);
2644       Nlm_Update ();
2645 #endif
2646 #ifdef WIN_MOTIF
2647       {
2648         Nlm_ShellTool shl = Nlm_GetWindowShell ((Nlm_WindoW) w);
2649         if (!NLM_QUIET  ||  Nlm_WindowHasBeenShown( (Nlm_WindoW)w ))
2650           XtUnmapWidget (shl);
2651         Nlm_Update ();
2652       }
2653 #endif
2654     }
2655   }
2656 }
2657 
Nlm_HideModal(Nlm_GraphiC w,Nlm_Boolean setFlag,Nlm_Boolean savePort)2658 static void Nlm_HideModal (Nlm_GraphiC w, Nlm_Boolean setFlag, Nlm_Boolean savePort)
2659 {
2660   /* esl: handle nested modal dialogs */
2661   if (w != NULL) {
2662     Nlm_WindowData wdata;
2663     Nlm_GetWindowData ((Nlm_WindoW) w, &wdata);
2664     Nlm_HideAndActivateOwner ((Nlm_WindoW) w, wdata.modalOwner);
2665     if (Nlm_IsWindowModal (wdata.modalOwner)) {
2666       /* enable previous modal dialog only */
2667       Nlm_EnableOneWindow (wdata.modalOwner, TRUE);
2668     } else {
2669       /* enable all windows */
2670       Nlm_EnableOtherWindows ((Nlm_WindoW) w, TRUE);
2671     }
2672   }
2673 }
2674 
2675 
Nlm_RemoveWindow(Nlm_GraphiC w,Nlm_Boolean savePort)2676 static void Nlm_RemoveWindow (Nlm_GraphiC w, Nlm_Boolean savePort)
2677 
2678 {
2679   Nlm_WindoW      nxt;
2680   Nlm_WindoW      p;
2681   Nlm_WindoW      q;
2682   Nlm_WindowData  wdata;
2683 #ifdef WIN_MOTIF
2684   Nlm_ShellTool   shl;
2685 #endif
2686 
2687   if ((Nlm_WindoW) w != Nlm_desktopWindow && (Nlm_WindoW) w != Nlm_systemWindow) {
2688     q = Nlm_desktopWindow;
2689     p = (Nlm_WindoW) Nlm_GetNext ((Nlm_GraphiC) Nlm_desktopWindow);
2690     while (p != NULL && p != (Nlm_WindoW) w) {
2691       q = p;
2692       p = (Nlm_WindoW) Nlm_GetNext ((Nlm_GraphiC) p);
2693     }
2694     if (p != NULL) {
2695       Nlm_GetWindowData ((Nlm_WindoW) w, &wdata);
2696       wdata.timer = NULL;
2697       Nlm_SetWindowData ((Nlm_WindoW) w, &wdata);
2698 #ifdef WIN_MAC
2699       HideWindow (Nlm_GetWindowPtr ((Nlm_WindoW) w));
2700       Nlm_Update ();
2701 #endif
2702 #ifdef WIN_MSWIN
2703       ShowWindow (Nlm_GetWindowPtr ((Nlm_WindoW) w), SW_HIDE);
2704 #endif
2705 #ifdef WIN_MOTIF
2706       shl = Nlm_GetWindowShell ((Nlm_WindoW) w);
2707       if (NLM_QUIET) {
2708         if (Nlm_WindowHasBeenShown ((Nlm_WindoW) w)) {
2709           XtUnmapWidget (shl);
2710         }
2711       } else {
2712         XtUnmapWidget (shl);
2713       }
2714 #endif
2715       Nlm_SetWindowDying ((Nlm_WindoW) w, TRUE);
2716       nxt = (Nlm_WindoW) Nlm_GetNext (w);
2717       Nlm_SetNext ((Nlm_GraphiC) q, (Nlm_GraphiC) nxt);
2718       Nlm_SetNext (w, (Nlm_GraphiC) dyingWindow);
2719       dyingWindow = (Nlm_WindoW) w;
2720       /* prevents parent lockout under Linux/LessTif, maybe others (thiessen) */
2721       if (Nlm_IsWindowModal((Nlm_WindoW) w))
2722         Nlm_HideModal(w, FALSE, FALSE);
2723     }
2724   }
2725   Nlm_Update ();
2726 }
2727 
Nlm_UseFullScreen(void)2728 NLM_EXTERN void Nlm_UseFullScreen (void)
2729 
2730 {
2731   Nlm_screenMode = USE_FULL_SCREEN;
2732 }
2733 
Nlm_UseLeftScreen(void)2734 NLM_EXTERN void Nlm_UseLeftScreen (void)
2735 
2736 {
2737   Nlm_screenMode = USE_LEFT_SCREEN;
2738 }
2739 
Nlm_UseRightScreen(void)2740 NLM_EXTERN void Nlm_UseRightScreen (void)
2741 
2742 {
2743   Nlm_screenMode = USE_RIGHT_SCREEN;
2744 }
2745 
Nlm_UsePrimaryMonitor(void)2746 NLM_EXTERN void Nlm_UsePrimaryMonitor (void)
2747 
2748 {
2749   Nlm_screenMode = USE_PRIMARY_MONITOR;
2750 }
2751 
Nlm_HasDualScreen(void)2752 NLM_EXTERN Nlm_Boolean Nlm_HasDualScreen (void)
2753 
2754 {
2755 #if(WINVER >= 0x0500)
2756   int num_monitors = GetSystemMetrics (SM_CMONITORS);
2757   if (num_monitors == 2) {
2758     return TRUE;
2759   }
2760   return FALSE;
2761 #else
2762   if (screenBitBounds.right > 2000 && screenBitBounds.bottom < 800) {
2763     return TRUE;
2764   } else if (screenBitBounds.right > 2500 && screenBitBounds.bottom < 1100) {
2765     return TRUE;
2766   }
2767   return FALSE;
2768 #endif
2769 }
2770 
Nlm_IsRemoteDesktop(void)2771 NLM_EXTERN Nlm_Boolean Nlm_IsRemoteDesktop (void)
2772 
2773 {
2774 #if(WINVER >= 0x0500)
2775   int iRemoteSession = GetSystemMetrics (SM_REMOTESESSION);
2776   if (iRemoteSession) {
2777     return TRUE;
2778   }
2779   return FALSE;
2780 #else
2781   return FALSE;
2782 #endif
2783 }
2784 
2785 /* esl: extraWidth parameter added */
Nlm_ResizeWindow(Nlm_GraphiC w,Nlm_Int2 dragHeight,Nlm_Int2 scrollWidth,Nlm_Int2 minWidth,Nlm_Int2 extraWidth,Nlm_Int2 extraHeight)2786 static void Nlm_ResizeWindow (Nlm_GraphiC w, Nlm_Int2 dragHeight,
2787                               Nlm_Int2 scrollWidth, Nlm_Int2 minWidth,
2788                               Nlm_Int2 extraWidth, Nlm_Int2 extraHeight)
2789 
2790 {
2791   Nlm_BoxData     bdata;
2792   Nlm_Int4        free;
2793   Nlm_Int4        height, width;
2794   Nlm_Int4        leftpix;
2795   Nlm_PoinT       lpt;
2796   Nlm_Int2        menuHeight = 0;
2797   Nlm_RecT        r;
2798   Nlm_Int4        rleft;
2799   Nlm_Int4        rtop;
2800   Nlm_ScreenMode  screenMode;
2801   Nlm_Int4        toppix;
2802   Nlm_WindowTool  wptr;
2803 #ifdef WIN_MOTIF
2804   Nlm_MainTool    man;
2805   Nlm_ShellTool   shl;
2806   Window          xtw;
2807 #endif
2808 
2809   if ( Nlm_WindowHasBeenShown((Nlm_WindoW) w) )  return;
2810 
2811   Nlm_SetRealized(w, TRUE);
2812 
2813 #ifdef WIN_MAC
2814   menuHeight = 21;
2815 #ifndef WIN_MAC_QUARTZ
2816   TextFont (0);
2817   TextSize (0);
2818 #endif
2819 #endif
2820 #ifdef WIN_MOTIF
2821   if (Nlm_GetWindowMenuBar( (Nlm_WindoW)w ) != NULL)
2822     menuHeight = Nlm_internalMenuBarHeight;
2823 #endif
2824 
2825   Nlm_GetRect (w, &r);
2826   if (r.left < 0 || r.right < 0 || r.top < 0 || r.bottom < 0) {
2827     Nlm_GetBoxData ((Nlm_BoX) w, &bdata);
2828     lpt = bdata.limitPoint;
2829     if (r.right < 0)
2830       {
2831         width = lpt.x + scrollWidth + bdata.xMargin;
2832         if (width < minWidth)
2833           width = minWidth;
2834       }
2835     else
2836       width = r.right;
2837     width += extraWidth; /* esl */
2838 
2839     if (r.bottom < 0)
2840       height = lpt.y + scrollWidth + extraHeight + bdata.yMargin;
2841     else
2842       height = r.bottom + extraHeight;
2843     if (r.left < 0)
2844       {
2845 #if(WINVER >= 0x0500)
2846         if (Nlm_HasDualScreen ()) {
2847           screenMode = Nlm_GetScreenMode ((Nlm_WindoW) w);
2848           if (screenMode == USE_PRIMARY_MONITOR) {
2849             free = (screenBitBounds.right - screenBitBounds.left) / 2 - width;
2850             rleft = (Nlm_Int4)r.left;
2851             leftpix = free * (-rleft) / 100;
2852             r.left = (Nlm_Int2)leftpix;
2853       } else if (screenMode == USE_LEFT_SCREEN) {
2854             free = (screenBitBounds.right - screenBitBounds.left) / 2 - width;
2855             rleft = (Nlm_Int4)r.left;
2856             leftpix = free * (-rleft) / 100;
2857             r.left = (Nlm_Int2)leftpix + screenBitBounds.left;
2858           } else if (screenMode == USE_RIGHT_SCREEN) {
2859             free = (screenBitBounds.right - screenBitBounds.left) / 2 - width;
2860             rleft = (Nlm_Int4)r.left;
2861             leftpix = free * (-rleft) / 100;
2862             r.left = (Nlm_Int2)leftpix + screenBitBounds.left;
2863             r.left += (screenBitBounds.right - screenBitBounds.left) / 2;
2864           } else {
2865             free = (screenBitBounds.right - screenBitBounds.left) - width;
2866             rleft = (Nlm_Int4)r.left;
2867             leftpix = free * (-rleft) / 100;
2868             r.left = (Nlm_Int2)leftpix + screenBitBounds.left;
2869           }
2870         } else {
2871           free = screenBitBounds.right - width;
2872           rleft = (Nlm_Int4)r.left;
2873           leftpix = free * (-rleft) / 100;
2874           r.left = (Nlm_Int2)leftpix;
2875         }
2876 #else
2877         if (Nlm_HasDualScreen ()) {
2878           screenMode = Nlm_GetScreenMode ((Nlm_WindoW) w);
2879           if (screenMode == USE_LEFT_SCREEN) {
2880             free = screenBitBounds.right / 2 - width;
2881             rleft = (Nlm_Int4)r.left;
2882             leftpix = free * (-rleft) / 100;
2883             r.left = (Nlm_Int2)leftpix;
2884           } else if (screenMode == USE_RIGHT_SCREEN) {
2885             free = screenBitBounds.right / 2 - width;
2886             rleft = (Nlm_Int4)r.left;
2887             leftpix = free * (-rleft) / 100;
2888             r.left = (Nlm_Int2)leftpix;
2889             r.left += screenBitBounds.right / 2;
2890           } else {
2891             free = screenBitBounds.right - width;
2892             rleft = (Nlm_Int4)r.left;
2893             leftpix = free * (-rleft) / 100;
2894             r.left = (Nlm_Int2)leftpix;
2895           }
2896         } else {
2897           free = screenBitBounds.right - width;
2898           rleft = (Nlm_Int4)r.left;
2899           leftpix = free * (-rleft) / 100;
2900           r.left = (Nlm_Int2)leftpix;
2901         }
2902 #endif
2903       }
2904     if (r.top < 0)
2905       {
2906         free = screenBitBounds.bottom - height - menuHeight - dragHeight;
2907         rtop = (Nlm_Int4)r.top;
2908         toppix = free * (-rtop) / 100;
2909         r.top = (Nlm_Int2)(toppix + menuHeight + dragHeight);
2910       }
2911     r.right  = (Nlm_Int2)(r.left + width);
2912     r.bottom = (Nlm_Int2)(r.top  + height);
2913 #ifdef WIN_MOTIF
2914     r.bottom -= menuHeight;
2915 #endif
2916     Nlm_SetRect(w, &r);
2917 
2918     wptr = Nlm_GetWindowPtr( (Nlm_WindoW)w );
2919 #ifdef WIN_MAC
2920     SizeWindow (wptr, width, height, FALSE);
2921     MoveWindow (wptr, r.left, r.top, FALSE);
2922 #endif
2923 #ifdef WIN_MSWIN
2924     {{
2925     RECT wr;
2926     client2win((Nlm_WindoW)w, &r, &wr);
2927     Nlm_SetRect(w, &r);
2928     MoveWindow(wptr, wr.left, wr.top,
2929                (wr.right - wr.left), (wr.bottom - wr.top), FALSE);
2930     }}
2931 #endif
2932 #ifdef WIN_MOTIF
2933     shl = Nlm_GetWindowShell ((Nlm_WindoW) w);
2934     man = Nlm_GetWindowMain ((Nlm_WindoW) w);
2935     XtVaSetValues (shl,
2936                    XmNx, (Position) r.left,
2937                    XmNy, (Position) r.top,
2938                    XmNwidth,  (Dimension) width,
2939                    XmNheight, (Dimension) height,
2940                    NULL);
2941     XtVaSetValues (man,
2942                    XmNwidth,  (Dimension) width,
2943                    XmNheight, (Dimension) height,
2944                    NULL);
2945     XtVaSetValues (wptr,
2946                    XmNwidth,  (Dimension) width,
2947                    XmNheight, (Dimension) height,
2948                    NULL);
2949 #endif
2950   }
2951 
2952 #ifdef WIN_MOTIF
2953   if (NLM_QUIET  &&  !Nlm_IsWindowDying((Nlm_WindoW) w))  {
2954     shl = Nlm_GetWindowShell ((Nlm_WindoW) w);
2955     XtRealizeWidget (shl);
2956     man = Nlm_GetWindowMain ((Nlm_WindoW) w);
2957     xtw = XtWindow( man );
2958     if ( xtw )
2959       XDefineCursor(Nlm_currentXDisplay, xtw, Nlm_currentCursor);
2960   }
2961 #endif
2962 }
2963 
2964 
Nlm_ShowNormal(Nlm_GraphiC w,Nlm_Boolean setFlag,Nlm_Boolean savePort)2965 static void Nlm_ShowNormal (Nlm_GraphiC w, Nlm_Boolean setFlag, Nlm_Boolean savePort)
2966 
2967 {
2968   Nlm_Char        title [256];
2969   Nlm_WindowTool  wptr;
2970 #ifdef WIN_MOTIF
2971   Nlm_Int2        extrah;
2972   Nlm_ShellTool   shl;
2973 #endif
2974 
2975   wptr = Nlm_ParentWindowPtr (w);
2976   Nlm_GetWindowTitle (w, 0, title, sizeof (title));
2977 #ifdef WIN_MAC
2978   Nlm_ResizeWindow (w, 20, 0, Nlm_StringWidth (title) + 70, 0, 0);
2979   Nlm_SetNotYetShown ((Nlm_WindoW) w, FALSE);
2980   if (setFlag) {
2981     Nlm_SetVisible (w, TRUE);
2982     ShowWindow (wptr);
2983   }
2984 #endif
2985 #ifdef WIN_MSWIN
2986   Nlm_ResizeWindow (w, 0, 0, (Nlm_Int2)(Nlm_StringWidth(title) + 80), 0, 0);
2987   Nlm_SetNotYetShown ((Nlm_WindoW) w, FALSE);
2988   if (setFlag) {
2989     Nlm_SetVisible (w, TRUE);
2990     ShowWindow (wptr, SW_SHOWNORMAL);
2991     UpdateWindow (wptr);
2992   }
2993 #endif
2994 #ifdef WIN_MOTIF
2995   extrah = 0;
2996   if (Nlm_GetWindowMenuBar ((Nlm_WindoW) w) != NULL) {
2997     extrah += 31;
2998   }
2999   Nlm_ResizeWindow (w, 0, 0, Nlm_StringWidth (title) + 65, 0, extrah);
3000   Nlm_SetNotYetShown ((Nlm_WindoW) w, FALSE);
3001   if (setFlag) {
3002     Nlm_SetVisible (w, TRUE);
3003     shl = Nlm_GetWindowShell ((Nlm_WindoW) w);
3004     XtMapWidget (shl);
3005   }
3006 #endif
3007   Nlm_Update ();
3008 }
3009 
Nlm_ShowModal(Nlm_GraphiC w,Nlm_Boolean setFlag,Nlm_Boolean savePort)3010 static void Nlm_ShowModal (Nlm_GraphiC w, Nlm_Boolean setFlag, Nlm_Boolean savePort)
3011 
3012 {
3013   Nlm_Char        title [256];
3014   Nlm_WindowTool  wptr;
3015 #ifdef WIN_MOTIF
3016   Nlm_Int2        extrah;
3017   Nlm_ShellTool   shl;
3018 #endif
3019 
3020   wptr = Nlm_ParentWindowPtr (w);
3021   Nlm_GetWindowTitle (w, 0, title, sizeof (title));
3022 #ifdef WIN_MAC
3023   Nlm_ResizeWindow (w, 20, 0, Nlm_StringWidth (title) + 70, 0, 0);
3024   Nlm_SetNotYetShown ((Nlm_WindoW) w, FALSE);
3025   if (setFlag) {
3026     Nlm_SetVisible (w, TRUE);
3027     ShowWindow (wptr);
3028   }
3029 #endif
3030 #ifdef WIN_MSWIN
3031   Nlm_ResizeWindow (w, 0, 0, (Nlm_Int2)(Nlm_StringWidth(title) + 80), 0, 0);
3032   Nlm_SetNotYetShown ((Nlm_WindoW) w, FALSE);
3033   if (setFlag) {
3034     Nlm_SetVisible (w, TRUE);
3035     ShowWindow (wptr, SW_SHOWNORMAL);
3036     UpdateWindow (wptr);
3037   }
3038 #endif
3039 #ifdef WIN_MOTIF
3040   extrah = 0;
3041   if (Nlm_GetWindowMenuBar ((Nlm_WindoW) w) != NULL) {
3042     extrah += 31;
3043   }
3044   Nlm_ResizeWindow (w, 0, 0, Nlm_StringWidth (title) + 65, 0, extrah);
3045   Nlm_SetNotYetShown ((Nlm_WindoW) w, FALSE);
3046   if (setFlag) {
3047     Nlm_SetVisible (w, TRUE);
3048     shl = Nlm_GetWindowShell ((Nlm_WindoW) w);
3049     XtMapWidget (shl);
3050   }
3051 #endif
3052   Nlm_Update ();
3053   Nlm_EnableOtherWindows ((Nlm_WindoW ) w, FALSE);
3054 }
3055 
Nlm_ShowDocument(Nlm_GraphiC w,Nlm_Boolean setFlag,Nlm_Boolean savePort)3056 static void Nlm_ShowDocument (Nlm_GraphiC w, Nlm_Boolean setFlag, Nlm_Boolean savePort)
3057 
3058 {
3059   Nlm_Char        title [256];
3060   Nlm_WindowTool  wptr;
3061 #ifdef WIN_MOTIF
3062   Nlm_Int2        extrah;
3063   Nlm_ShellTool   shl;
3064 #endif
3065 
3066   wptr = Nlm_ParentWindowPtr (w);
3067   Nlm_GetWindowTitle (w, 0, title, sizeof (title));
3068 #ifdef WIN_MAC
3069   Nlm_ResizeWindow (w, 20, 0, Nlm_StringWidth (title) + 70, 0, 0);
3070   Nlm_SetNotYetShown ((Nlm_WindoW) w, FALSE);
3071   if (setFlag) {
3072     Nlm_SetVisible (w, TRUE);
3073     ShowWindow (wptr);
3074   }
3075 #endif
3076 #ifdef WIN_MSWIN
3077   Nlm_ResizeWindow (w, 0, 0, (Nlm_Int2)(Nlm_StringWidth(title) + 80), 0, 0);
3078   Nlm_SetNotYetShown ((Nlm_WindoW) w, FALSE);
3079   if (setFlag) {
3080     Nlm_SetVisible (w, TRUE);
3081     ShowWindow (wptr, SW_SHOWNORMAL);
3082     UpdateWindow (wptr);
3083   }
3084 #endif
3085 #ifdef WIN_MOTIF
3086   extrah = 0;
3087   if (Nlm_GetWindowMenuBar ((Nlm_WindoW) w) != NULL) {
3088     extrah += 31;
3089   }
3090   Nlm_ResizeWindow (w, 0, 0, Nlm_StringWidth (title) + 65, 0, extrah);
3091   Nlm_SetNotYetShown ((Nlm_WindoW) w, FALSE);
3092   if (setFlag) {
3093     Nlm_SetVisible (w, TRUE);
3094     shl = Nlm_GetWindowShell ((Nlm_WindoW) w);
3095     XtMapWidget (shl);
3096   }
3097 #endif
3098   Nlm_Update ();
3099 }
3100 
3101 
Nlm_SetWindowPosition(Nlm_GraphiC w,Nlm_RectPtr r,Nlm_Boolean savePort,Nlm_Boolean force)3102 static void Nlm_SetWindowPosition (Nlm_GraphiC w, Nlm_RectPtr r,
3103                                    Nlm_Boolean savePort, Nlm_Boolean force)
3104 {
3105   Nlm_Int2        height;
3106   Nlm_RecT        rct;
3107   Nlm_Int2        width;
3108   Nlm_WindowTool  wptr;
3109 #ifdef WIN_MOTIF
3110   Nlm_MainTool    man;
3111   Nlm_ShellTool   shl;
3112 #endif
3113 
3114   if (r == NULL)  return;
3115 
3116   rct = *r;
3117   width  = (Nlm_Int2)(rct.right  - rct.left);
3118   height = (Nlm_Int2)(rct.bottom - rct.top );
3119   if (Nlm_GetWindowMenuBar( (Nlm_WindoW)w ) != NULL)
3120     rct.bottom -= Nlm_internalMenuBarHeight;
3121   Nlm_SetRect (w, &rct);
3122 
3123   if ( !Nlm_GetRealized( w ) )  return;
3124 
3125   wptr = Nlm_GetWindowPtr ((Nlm_WindoW) w);
3126 #ifdef WIN_MAC
3127     MoveWindow (wptr, rct.left, rct.top, FALSE);
3128     SizeWindow (wptr, width, height, TRUE);
3129 #endif
3130 #ifdef WIN_MSWIN
3131     {{
3132     RECT wr;
3133     client2win((Nlm_WindoW)w, &rct, &wr);
3134     MoveWindow(wptr, wr.left, wr.top,
3135                (wr.right - wr.left), (wr.bottom - wr.top), TRUE);
3136     }}
3137 #endif
3138 #ifdef WIN_MOTIF
3139     shl = Nlm_GetWindowShell ((Nlm_WindoW) w);
3140     man = Nlm_GetWindowMain  ((Nlm_WindoW) w);
3141     XtVaSetValues (shl,
3142                    XmNx, (Position) rct.left,
3143                    XmNy, (Position) rct.top,
3144                    XmNwidth,  (Dimension) width,
3145                    XmNheight, (Dimension) height,
3146                    NULL);
3147     XtVaSetValues (man,
3148                    XmNwidth,  (Dimension) width,
3149                    XmNheight, (Dimension) height,
3150                    NULL);
3151     XtVaSetValues (wptr,
3152                    XmNwidth,  (Dimension) width,
3153                    XmNheight, (Dimension) height,
3154                    NULL);
3155 #endif
3156 }
3157 
3158 
Nlm_GetWindowPosition(Nlm_GraphiC w,Nlm_RectPtr r)3159 static void Nlm_GetWindowPosition (Nlm_GraphiC w, Nlm_RectPtr r)
3160 
3161 {
3162   if (r != NULL) {
3163     Nlm_GetRect (w, r);
3164     if (Nlm_GetWindowMenuBar ((Nlm_WindoW) w) != NULL)
3165       r->bottom += Nlm_internalMenuBarHeight;
3166   }
3167 }
3168 
3169 #if defined(LESSTIF_VERSION)
3170 
FixVisibilityIssues(Nlm_GraphiC g)3171 static void FixVisibilityIssues (Nlm_GraphiC g)
3172 {
3173   Nlm_GraphiC child;
3174 
3175   for (child = Nlm_GetChild (g);  child != NULL; child = Nlm_GetNext (child))
3176   {
3177     if (!Nlm_GetVisible (child))
3178     {
3179       Nlm_Show (child);
3180       FixVisibilityIssues (child);
3181       Nlm_Hide (child);
3182     }
3183     else
3184     {
3185       FixVisibilityIssues (child);
3186     }
3187   }
3188 }
3189 #endif
3190 
Nlm_RealizeWindow(Nlm_WindoW w)3191 extern void Nlm_RealizeWindow (Nlm_WindoW w)
3192 
3193 {
3194   if (w != NULL) {
3195     Nlm_DoShow ((Nlm_GraphiC) w, FALSE, TRUE);
3196   }
3197 
3198 #if defined(LESSTIF_VERSION)
3199   FixVisibilityIssues ((Nlm_GraphiC) w);
3200 #endif
3201 }
3202 
Nlm_IconifyWindow(Nlm_WindoW w)3203 extern void Nlm_IconifyWindow(Nlm_WindoW w)
3204 {
3205 #if defined(WIN_MOTIF)
3206   XtVaSetValues(Nlm_GetWindowShell(w), XtNiconic, (Boolean)True, NULL);
3207   Nlm_Update();
3208 #elif defined(WIN_MSWIN)
3209   ShowWindow(Nlm_ParentWindowPtr((Nlm_GraphiC)w), SW_MINIMIZE);
3210   Nlm_SetVisible((Nlm_GraphiC)w, FALSE);
3211 #endif
3212 }
3213 
3214 
Nlm_IconicWindow(Nlm_WindoW w)3215 extern Nlm_Boolean Nlm_IconicWindow(Nlm_WindoW w)
3216 {
3217 #if defined(WIN_MOTIF)
3218   /* (X-technique from Ken Sall's function CheckWinMgrState() */
3219   unsigned long *property = NULL;
3220   unsigned long nitems;
3221   unsigned long leftover;
3222   Atom          xa_WM_STATE, actual_type;
3223   int           actual_format;
3224   int           status;
3225 
3226   Widget   shl = Nlm_GetWindowShell( w );
3227   Display *dpy = XtDisplay( shl );
3228   Window   win = XtWindow ( shl );
3229 
3230   if ( !win )
3231     return FALSE;
3232 
3233   xa_WM_STATE = XInternAtom(dpy, "WM_STATE", False);
3234   status  = XGetWindowProperty(dpy, win, xa_WM_STATE, 0L, 1L,
3235                                False, xa_WM_STATE,
3236                                &actual_type, &actual_format,
3237                                &nitems, &leftover,
3238                                (unsigned char **)&property);
3239 
3240   status = (status == Success  &&  actual_type == xa_WM_STATE  &&
3241             nitems == 1  &&  property  &&  *property == IconicState);
3242 
3243   if ( property )
3244     XFree( (char *)property );
3245   return (Nlm_Boolean)status;
3246 #elif defined(WIN_MSWIN)
3247   return (Nlm_Boolean)IsIconic( Nlm_ParentWindowPtr((Nlm_GraphiC)w) );
3248 #else
3249   return FALSE;
3250 #endif
3251 }
3252 
3253 
Nlm_WhichWindow(Nlm_PoinT mouseLoc)3254 extern Nlm_WindoW Nlm_WhichWindow (Nlm_PoinT mouseLoc)
3255 {
3256 #ifdef WIN_MAC
3257   Nlm_PoinT       point;
3258   Nlm_PointTool   ptool;
3259   Nlm_Int2        windowLoc;
3260   Nlm_WindowTool  wptr;
3261 
3262   point = mouseLoc;
3263   Nlm_LocalToGlobal (&point);
3264   Nlm_PoinTToPointTool (point, &ptool);
3265   windowLoc = FindWindow (ptool, &wptr);
3266   return Nlm_FindWindowRec (wptr);
3267 #endif
3268 #ifdef WIN_MSWIN
3269   Nlm_PoinT       point;
3270   Nlm_PointTool   ptool;
3271   Nlm_WindoW      rsult;
3272   Nlm_WindowTool  wptr;
3273 
3274   rsult = NULL;
3275   point = mouseLoc;
3276   Nlm_LocalToGlobal (&point);
3277   Nlm_PoinTToPointTool (point, &ptool);
3278   wptr = WindowFromPoint (ptool);
3279   if ( wptr ) {
3280     rsult = (Nlm_WindoW) GetProp (wptr, (LPSTR) "Nlm_VibrantProp");
3281   }
3282   return rsult;
3283 #endif
3284 #ifdef WIN_MOTIF
3285   return NULL;
3286 #endif
3287 }
3288 
Nlm_InWindow(Nlm_PoinT mouseLoc)3289 extern Nlm_Boolean Nlm_InWindow (Nlm_PoinT mouseLoc)
3290 
3291 {
3292   return (Nlm_Boolean) (Nlm_WhichWindow (mouseLoc) != Nlm_desktopWindow);
3293 }
3294 
Nlm_FrontWindow(void)3295 extern Nlm_WindoW Nlm_FrontWindow(void)
3296 
3297 {
3298 #ifdef WIN_MAC
3299   return (Nlm_FindWindowRec (FrontWindow ()));
3300 #endif
3301 #ifdef WIN_MSWIN
3302   return (Nlm_FindWindowRec (Nlm_currentHWnd));
3303 #endif
3304 #ifdef WIN_MOTIF
3305   return NULL;
3306 #endif
3307 }
3308 
Nlm_InFront(Nlm_WindoW w)3309 extern Nlm_Boolean Nlm_InFront (Nlm_WindoW w)
3310 
3311 {
3312 #ifdef WIN_MAC
3313   return (w == Nlm_FrontWindow ());
3314 #endif
3315 #ifdef WIN_MSWIN
3316   return FALSE;
3317 #endif
3318 #ifdef WIN_MOTIF
3319   return FALSE;
3320 #endif
3321 }
3322 
Nlm_UseWindow(Nlm_WindoW w)3323 extern void Nlm_UseWindow (Nlm_WindoW w)
3324 
3325 {
3326   Nlm_WindowTool  wptr;
3327 #ifdef WIN_MOTIF
3328   Nlm_ShellTool   shl;
3329 #endif
3330 
3331   if (w != NULL && w != Nlm_desktopWindow) {
3332     wptr = Nlm_ParentWindowPtr ((Nlm_GraphiC) w);
3333     if (wptr  &&  !Nlm_IsWindowDying( w )) {
3334 #ifdef WIN_MAC
3335 #ifndef WIN_MAC_QUARTZ
3336       Nlm_SetPortWindowPort(wptr);
3337       Nlm_SetUpdateRegion (wptr);
3338       Nlm_ResetDrawingTools();
3339 #endif
3340       Nlm_theWindow = w;
3341 #endif
3342 #ifdef WIN_MSWIN
3343       Nlm_currentHDC = Nlm_GetWindowPort (w);
3344       Nlm_currentHWnd = wptr;
3345 #endif
3346 #ifdef WIN_MOTIF
3347       shl = Nlm_GetWindowShell (w);
3348       Nlm_currentXWindow = XtWindow (shl);
3349       Nlm_currentXGC = Nlm_GetWindowPort (w);
3350 #endif
3351       Nlm_currentWindowTool = wptr;
3352     }
3353   }
3354 }
3355 
Nlm_CurrentWindow(void)3356 extern Nlm_WindoW Nlm_CurrentWindow(void)
3357 
3358 {
3359   Nlm_WindoW  rsult;
3360 
3361   rsult = Nlm_FindWindowRec (Nlm_currentWindowTool);
3362   return rsult;
3363 }
3364 
Nlm_UsingWindow(Nlm_WindoW w)3365 extern Nlm_Boolean Nlm_UsingWindow (Nlm_WindoW w)
3366 
3367 {
3368   return (Nlm_Boolean) (w == Nlm_CurrentWindow ());
3369 }
3370 
Nlm_ActiveWindow(void)3371 extern Nlm_WindoW Nlm_ActiveWindow (void)
3372 
3373 {
3374   Nlm_WindoW      rsult;
3375 #ifdef WIN_MSWIN
3376   Nlm_WindowTool  wptr;
3377 #endif
3378 
3379   rsult = NULL;
3380 #ifdef WIN_MAC
3381   rsult = Nlm_theWindow;
3382 #endif
3383 #ifdef WIN_MSWIN
3384   wptr = GetActiveWindow ();
3385   if (wptr != NULL) {
3386     rsult = (Nlm_WindoW) GetProp (wptr, (LPSTR) "Nlm_VibrantProp");
3387   }
3388 #endif
3389 #ifdef WIN_MOTIF
3390 #endif
3391   return rsult;
3392 }
3393 
3394 #ifdef WIN_MAC
3395 /*
3396 static Nlm_WindoW Nlm_PrevVisWindow (Nlm_WindoW w)
3397 
3398 {
3399   WindowPtr  p;
3400   WindowPtr  q;
3401   WindowPtr  t;
3402 
3403   if (frontWindow == Nlm_desktopWindow) {
3404     return Nlm_desktopWindow;
3405   } else if (frontWindow == w) {
3406     return frontWindow;
3407   } else {
3408     p = Nlm_ParentWindowPtr ((Nlm_GraphiC) frontWindow);
3409     q = p;
3410     t = Nlm_ParentWindowPtr ((Nlm_GraphiC) w);
3411     while (p != NULL) {
3412       if (p == t) {
3413         p = NULL;
3414       } else if (IsWindowVisible(p)) {
3415         q = p;
3416         p = GetNextWindow(p);
3417       } else {
3418         p = GetNextWindow(p);
3419       }
3420     }
3421     return (Nlm_FindWindowRec ((Nlm_WindowTool) q));
3422   }
3423 }
3424 */
3425 #endif
3426 
3427 
Nlm_SelectWindow(Nlm_GraphiC w,Nlm_Boolean savePort)3428 static void Nlm_SelectWindow(Nlm_GraphiC w, Nlm_Boolean savePort)
3429 
3430 {
3431   Nlm_WindowTool  wptr;
3432 
3433   if (w == NULL  ||  (Nlm_WindoW)w == Nlm_desktopWindow)
3434     return;
3435 
3436   wptr = Nlm_ParentWindowPtr( w );
3437   if (wptr == NULL  ||  Nlm_IsWindowDying( (Nlm_WindoW)w ))
3438     return;
3439 
3440 #ifdef WIN_MAC
3441   SelectWindow (wptr);
3442 #ifndef WIN_MAC_QUARTZ
3443   Nlm_SetPortWindowPort(wptr);
3444   Nlm_SetUpdateRegion(wptr);
3445   Nlm_ResetDrawingTools();
3446 #endif
3447 #endif
3448 #ifdef WIN_MSWIN
3449   BringWindowToTop (wptr);
3450 /* M.I */
3451   if( IsIconic( wptr ) )
3452     ShowWindow( wptr, SW_RESTORE );  /* de-Iconize window */
3453 /* M.I */
3454   Nlm_currentHDC = Nlm_GetWindowPort ((Nlm_WindoW) w);
3455   Nlm_currentHWnd = wptr;
3456 #endif
3457 #ifdef WIN_MOTIF
3458   selectShell = Nlm_GetWindowShell( (Nlm_WindoW)w );
3459   if (Nlm_currentXDisplay == NULL  ||  !XtIsRealized( selectShell ))
3460     return;
3461 
3462   {{
3463     Window selectWin = XtWindow( selectShell );
3464     Window focus_win;
3465     int revert_to;
3466 
3467     XMapRaised(Nlm_currentXDisplay, selectWin);
3468     XGetInputFocus(Nlm_currentXDisplay, &focus_win, &revert_to);
3469     if (focus_win != selectWin)
3470       {
3471         XWindowAttributes attr;
3472         XGetWindowAttributes(Nlm_currentXDisplay, selectWin, &attr);
3473         if (attr.map_state != IsViewable)
3474           return;
3475 
3476         XSetInputFocus(Nlm_currentXDisplay, selectWin,
3477                        RevertToPointerRoot, CurrentTime);
3478         selectShell = NULL;
3479       }
3480   }}
3481   Nlm_UseWindow( (Nlm_WindoW)w );
3482 #endif
3483 
3484   Nlm_currentWindowTool = wptr;
3485   Nlm_Update ();
3486 }
3487 
3488 
Nlm_PopupParentWindow(Nlm_Handle a)3489 extern void Nlm_PopupParentWindow(Nlm_Handle a)
3490 {
3491   Nlm_WindoW     w = Nlm_ParentWindow( a );
3492   Nlm_WindowTool wptr;
3493 
3494   if (w == NULL  ||  w == Nlm_desktopWindow  ||  Nlm_IsWindowDying( w ))
3495     return;
3496 
3497   if ( !Nlm_Visible( w ) )
3498     {
3499       Nlm_Show( w );
3500       return;
3501     }
3502 
3503   wptr = Nlm_ParentWindowPtr( (Nlm_GraphiC)w );
3504   if ( !wptr )
3505     return;
3506 
3507 #ifdef WIN_MAC
3508   SelectWindow (wptr);
3509   Nlm_SetUpdateRegion (wptr);
3510   Nlm_ResetDrawingTools ();
3511 #endif
3512 #ifdef WIN_MSWIN
3513   if( IsIconic(wptr) )
3514     ShowWindow(wptr, SW_RESTORE);
3515   else
3516     SetWindowPos(wptr, HWND_TOP, 0, 0, 0, 0,
3517                  SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_SHOWWINDOW);
3518 #endif
3519 #ifdef WIN_MOTIF
3520   {{
3521     Widget selShell = Nlm_GetWindowShell( w );
3522     if (Nlm_currentXDisplay == NULL  ||  !XtIsRealized( selShell ))
3523       return;
3524 
3525     XMapRaised(Nlm_currentXDisplay, XtWindow( selShell ));
3526   }}
3527 #endif
3528 
3529   Nlm_Update ();
3530 }
3531 
3532 
Nlm_EraseWindow(Nlm_WindoW w)3533 extern void Nlm_EraseWindow (Nlm_WindoW w)
3534 
3535 {
3536 #ifdef WIN_MAC
3537   Nlm_RecT        r;
3538   Nlm_PortTool    temp;
3539   Nlm_WindowTool  wptr;
3540   Rect bounds;
3541 
3542   if (w != NULL) {
3543     wptr = Nlm_ParentWindowPtr ((Nlm_GraphiC) w);
3544 #ifdef WIN_MAC_QUARTZ
3545     HIViewSetNeedsDisplay (HIViewGetRoot (wptr), 1);
3546 #else
3547     GetPort(&temp);
3548     Nlm_SetPortWindowPort(wptr);
3549     Nlm_ResetDrawingTools();
3550     Nlm_currentWindowTool = wptr;
3551     GetPortBounds(GetWindowPort(wptr), &bounds);
3552     Nlm_RectToolToRecT (&bounds, &r);
3553     Nlm_EraseRect(&r);
3554     Nlm_SetPort(temp);
3555     Nlm_currentWindowTool = GetWindowFromPort(temp);
3556     Nlm_Update();
3557 #endif
3558   }
3559 #endif
3560 #ifdef WIN_MSWIN
3561 #endif
3562 #ifdef WIN_MOTIF
3563 #endif
3564 }
3565 
3566 #ifdef WIN_MAC
Nlm_DragClick(Nlm_GraphiC w,Nlm_PoinT pt)3567 static Nlm_Boolean Nlm_DragClick (Nlm_GraphiC w, Nlm_PoinT pt)
3568 
3569 {
3570   Nlm_PointTool   ptool;
3571   Nlm_RecT        r;
3572   Nlm_RectTool    rtool;
3573   Nlm_Boolean     rsult;
3574   Nlm_Int2        windowLoc;
3575   Nlm_WindowData  wdata;
3576   Nlm_WindowTool  wptr;
3577   Rect bounds;
3578 
3579   rsult = FALSE;
3580   Nlm_PoinTToPointTool (Nlm_globalMouse, &ptool);
3581   windowLoc = FindWindow (ptool, &wptr);
3582   if (windowLoc == inDrag) {
3583     Nlm_GetWindowData ((Nlm_WindoW) w, &wdata);
3584     Nlm_RecTToRectTool (&(wdata.dragArea), &rtool);
3585     DragWindow (wptr, ptool, &rtool);
3586     GetPortBounds(GetWindowPort(wptr), &bounds);
3587     Nlm_RectToolToRecT (&bounds, &r);
3588     Nlm_LocalToGlobal ((Nlm_PointPtr) &(r.left));
3589     Nlm_LocalToGlobal ((Nlm_PointPtr) &(r.right));
3590     Nlm_SetRect (w, &r);
3591     rsult = TRUE;
3592   }
3593   return rsult;
3594 }
3595 
Nlm_UpdateScrollBar(Nlm_GraphiC w)3596 static void Nlm_UpdateScrollBar (Nlm_GraphiC w)
3597 
3598 {
3599   Nlm_RecT        barArea;
3600   Nlm_WindowTool  wptr;
3601   Rect bounds;
3602 
3603   wptr = Nlm_ParentWindowPtr (w);
3604   GetPortBounds(GetWindowPort(wptr), &bounds);
3605   Nlm_RectToolToRecT (&bounds, &barArea);
3606   barArea.left = barArea.right - 16;
3607   if (Nlm_GetWindowMenuBar ((Nlm_WindoW) w) != NULL) {
3608     barArea.top = barArea.top + 21;
3609   }
3610   Nlm_InvalRect (&barArea);
3611   Nlm_RectToolToRecT (&bounds, &barArea);
3612   barArea.top = barArea.bottom - 16;
3613   Nlm_InvalRect (&barArea);
3614 }
3615 
Nlm_GrowClick(Nlm_GraphiC w,Nlm_PoinT pt)3616 static Nlm_Boolean Nlm_GrowClick (Nlm_GraphiC w, Nlm_PoinT pt)
3617 
3618 {
3619   Nlm_Int2         ht;
3620   Nlm_Int4         newSize;
3621   Nlm_PointTool    ptool;
3622   Nlm_RecT         r;
3623   Nlm_WndActnProc  resize;
3624   Nlm_RectTool     rtool;
3625   Nlm_Boolean      rsult;
3626   Nlm_Int2         wd;
3627   Nlm_WindowData   wdata;
3628   Nlm_Int2         windowLoc;
3629   Nlm_WindowTool   wptr;
3630   Rect bounds;
3631 
3632   rsult = FALSE;
3633   Nlm_PoinTToPointTool (Nlm_globalMouse, &ptool);
3634   windowLoc = FindWindow (ptool, &wptr);
3635   if (windowLoc == inGrow) {
3636     Nlm_LoadRect (&r, -32768, -32768, 32767, 32767);
3637     Nlm_RecTToRectTool (&r, &rtool);
3638 #ifndef WIN_MAC_QUARTZ
3639     ClipRect (&rtool);
3640 #endif
3641     Nlm_GetWindowData ((Nlm_WindoW) w, &wdata);
3642     Nlm_RecTToRectTool (&(wdata.growArea), &rtool);
3643     newSize = GrowWindow (wptr, ptool, &rtool);
3644     wd = LoWord (newSize);
3645     ht = HiWord (newSize);
3646  /* dgg -- bug fix: simple click on grow box caused it to resize to 0 --
3647     should check here for 0 == no change.  Also make minsize bigger.
3648  */
3649     GetPortBounds(GetWindowPort(wptr), &bounds);
3650     if (! newSize) {
3651 #ifndef WIN_MAC_QUARTZ
3652       ClipRect (&bounds);
3653 #endif
3654       return rsult;
3655     }
3656     if (wd < 50) {
3657       wd = 50;
3658     }
3659     if (ht < 32) {
3660       ht = 32;
3661     }
3662     bounds.right = bounds.left + wd;
3663     bounds.bottom = bounds.top + ht;
3664     Nlm_UpdateScrollBar (w);
3665     SizeWindow (wptr, wd, ht, TRUE);
3666     Nlm_UpdateScrollBar (w);
3667     wptr = Nlm_GetWindowPtr ((Nlm_WindoW) w);
3668     Nlm_RectToolToRecT (&bounds, &r);
3669 #ifndef WIN_MAC_QUARTZ
3670     ClipRect (&bounds);
3671 #endif
3672     Nlm_LocalToGlobal ((Nlm_PointPtr) &(r.left));
3673     Nlm_LocalToGlobal ((Nlm_PointPtr) &(r.right));
3674     Nlm_SetRect (w, &r);
3675     resize = wdata.resize;
3676     if (resize != NULL) {
3677       resize ((Nlm_WindoW) w);
3678     }
3679     rsult = TRUE;
3680   }
3681   return rsult;
3682 }
3683 
Nlm_DrawGrowIcon(Nlm_GraphiC w,Nlm_Boolean drawgrow,Nlm_Boolean drawbar)3684 static void Nlm_DrawGrowIcon (Nlm_GraphiC w, Nlm_Boolean drawgrow, Nlm_Boolean drawbar)
3685 
3686 {
3687   Nlm_RecT        r;
3688   Nlm_RectTool    rtool;
3689   PenState        state;
3690   Nlm_PortTool    temp;
3691   Nlm_WindowTool  wptr;
3692   Rect bounds;
3693 
3694 #ifndef WIN_MAC_QUARTZ
3695   GetPort (&temp);
3696   GetPenState (&state);
3697 #endif
3698   wptr = Nlm_ParentWindowPtr (w);
3699   GetPortBounds(GetWindowPort(wptr), &bounds);
3700   Nlm_RectToolToRecT (&bounds, &r);
3701   if (drawbar && Nlm_GetWindowMenuBar ((Nlm_WindoW) w) != NULL) {
3702     Nlm_RecTToRectTool (&r, &rtool);
3703 #ifndef WIN_MAC_QUARTZ
3704     ClipRect (&rtool);
3705 #endif
3706     Nlm_MoveTo (r.right - 16, 20);
3707     Nlm_LineTo (r.right, 20);
3708     r.top = r.top + 21;
3709   }
3710   if (drawgrow) {
3711     Nlm_RecTToRectTool (&r, &rtool);
3712     rtool.left = rtool.right - 15;
3713     rtool.top = rtool.bottom - 15;
3714 #ifndef WIN_MAC_QUARTZ
3715     ClipRect (&rtool);
3716 #endif
3717     DrawGrowIcon (wptr);
3718   }
3719   Nlm_RecTToRectTool (&r, &rtool);
3720   r.top = bounds.top;
3721 #ifdef DCLAP
3722   /* dgg- this cliprect is the culprit for preventing scrollbar updates by subviews */
3723   /* try clipping a region that excludes just the growicon?? */
3724 #else
3725   /*
3726   r.right = r.right - 16;
3727   r.bottom = r.bottom - 16;
3728   */
3729 #endif
3730   Nlm_RecTToRectTool (&r, &rtool);
3731 #ifndef WIN_MAC_QUARTZ
3732   ClipRect (&rtool);
3733   Nlm_SetPort(temp);
3734   Nlm_currentWindowTool = GetWindowFromPort(temp);
3735   Nlm_SetUpdateRegion (GetWindowFromPort(temp));
3736   SetPenState (&state);
3737 #endif
3738 }
3739 
Nlm_ZoomClick(Nlm_GraphiC w,Nlm_PoinT pt)3740 static Nlm_Boolean Nlm_ZoomClick (Nlm_GraphiC w, Nlm_PoinT pt)
3741 
3742 {
3743   Nlm_PointTool    ptool;
3744   Nlm_RecT         r;
3745   Nlm_WndActnProc  resize;
3746   Nlm_Boolean      rsult;
3747   Nlm_WindowData   wdata;
3748   Nlm_Int2         windowLoc;
3749   Nlm_WindowTool   wptr;
3750 
3751   rsult = FALSE;
3752   Nlm_PoinTToPointTool (Nlm_globalMouse, &ptool);
3753   windowLoc = FindWindow (ptool, &wptr);
3754   if (windowLoc == inZoomIn || windowLoc == inZoomOut) {
3755 #if OPAQUE_TOOLBOX_STRUCTS
3756     Point idealSize;
3757     int part;
3758 #else
3759     Rect stdRect;
3760     WStateDataHandle wshdl;
3761 #endif
3762     Nlm_GetWindowData ((Nlm_WindoW) w, &wdata);
3763     r = wdata.zoomArea;
3764 #if OPAQUE_TOOLBOX_STRUCTS
3765     idealSize.v = r.bottom - r.top;
3766     idealSize.h = r.right - r.left;
3767 
3768     part = IsWindowInStandardState(wptr, &idealSize, NULL) ? inZoomIn : inZoomOut;
3769     windowLoc = part;
3770 #endif
3771 
3772     if (TrackBox (wptr, ptool, windowLoc)) {
3773       Rect bounds;
3774       /* WindowPeek::dataHandle is not supported under Carbon. */
3775 #if OPAQUE_TOOLBOX_STRUCTS
3776       ZoomWindowIdeal(wptr, part, &idealSize);
3777 #else
3778       wshdl = (WStateDataHandle) ((WindowPeek)wptr)->dataHandle;
3779       Nlm_RecTToRectTool (&r, &stdRect);
3780       (*wshdl)->stdState = stdRect;
3781       GetPortBounds(GetWindowPort(wptr), &bounds);
3782       Nlm_RectToolToRecT (&bounds, &r);
3783       ClipRect (&bounds);
3784       Nlm_EraseRect (&r);
3785       ZoomWindow (wptr, windowLoc, FALSE);
3786 #endif
3787       GetPortBounds(GetWindowPort(wptr), &bounds);
3788       Nlm_UpdateScrollBar (w);
3789       wptr = Nlm_GetWindowPtr ((Nlm_WindoW) w);
3790       Nlm_RectToolToRecT (&bounds, &r);
3791 #ifndef WIN_MAC_QUARTZ
3792       ClipRect (&bounds);
3793 #endif
3794       Nlm_LocalToGlobal ((Nlm_PointPtr) &(r.left));
3795       Nlm_LocalToGlobal ((Nlm_PointPtr) &(r.right));
3796       Nlm_SetRect (w, &r);
3797       resize = wdata.resize;
3798       if (resize != NULL) {
3799         resize ((Nlm_WindoW) w);
3800       }
3801     }
3802     rsult = TRUE;
3803   }
3804   return rsult;
3805 }
3806 
Nlm_CloseClick(Nlm_GraphiC w,Nlm_PoinT pt)3807 static Nlm_Boolean Nlm_CloseClick (Nlm_GraphiC w, Nlm_PoinT pt)
3808 
3809 {
3810   Nlm_WndActnProc  cls;
3811   Nlm_PointTool    ptool;
3812   Nlm_Boolean      rsult;
3813   Nlm_WindowData   wdata;
3814   Nlm_Int2         windowLoc;
3815   Nlm_WindowTool   wptr;
3816 
3817   rsult = FALSE;
3818   Nlm_PoinTToPointTool (Nlm_globalMouse, &ptool);
3819   windowLoc = FindWindow (ptool, &wptr);
3820   if (windowLoc == inGoAway) {
3821     if (TrackGoAway (wptr, ptool)) {
3822       Nlm_GetWindowData ((Nlm_WindoW) w, &wdata);
3823       cls = wdata.close;
3824       if (cls != NULL) {
3825         cls ((Nlm_WindoW) w);
3826       }
3827     }
3828     rsult = TRUE;
3829   }
3830   return rsult;
3831 }
3832 
3833 typedef struct revItem {
3834   Nlm_GraphiC          graphic;
3835   struct revItem  PNTR next;
3836 } Nlm_RevItem, PNTR Nlm_RevPtr;
3837 
Nlm_ContentClick(Nlm_GraphiC w,Nlm_PoinT pt)3838 static Nlm_Boolean Nlm_ContentClick (Nlm_GraphiC w, Nlm_PoinT pt)
3839 
3840 {
3841   Nlm_GraphiC    g;
3842   Nlm_MenuBaR    mb;
3843   Nlm_RevPtr     next;
3844   Nlm_Boolean    notInside;
3845   Nlm_PointTool  ptool;
3846   Nlm_RevPtr     this;
3847   Nlm_RevPtr     top;
3848 
3849   Nlm_localMouse = Nlm_globalMouse;
3850   Nlm_SetPortWindowPort (Nlm_ParentWindowPtr (w));
3851   Nlm_GlobalToLocal (&Nlm_localMouse);
3852   g = Nlm_GetChild (w);
3853   notInside = TRUE;
3854   mb = Nlm_GetWindowMenuBar ((Nlm_WindoW) w);
3855   if (mb != NULL) {
3856     if (Nlm_GetEnabled ((Nlm_GraphiC) mb) && Nlm_GetVisible ((Nlm_GraphiC) mb) &&
3857         Nlm_DoClick ((Nlm_GraphiC) mb, Nlm_localMouse)) {
3858       notInside = FALSE;
3859     }
3860   }
3861   /*
3862   while (g != NULL && notInside) {
3863     n = Nlm_GetNext (g);
3864     if (Nlm_GetEnabled (g) && Nlm_GetVisible (g) && Nlm_DoClick (g, Nlm_localMouse)) {
3865       notInside = FALSE;
3866     }
3867     g = n;
3868   }
3869   */
3870   if (g != NULL && notInside) {
3871     top = NULL;
3872     while (g != NULL) {
3873       this = Nlm_MemNew (sizeof (Nlm_RevItem));
3874       if (this != NULL) {
3875         this->graphic = g;
3876         this->next = top;
3877         top = this;
3878       }
3879       g = Nlm_GetNext (g);
3880     }
3881     this = top;
3882     while (this != NULL && notInside) {
3883       next = this->next;
3884       g = this->graphic;
3885       if (Nlm_GetEnabled (g) && Nlm_GetVisible (g) && Nlm_DoClick (g, Nlm_localMouse)) {
3886         notInside = FALSE;
3887       }
3888       this = next;
3889     }
3890     this = top;
3891     while (this != NULL) {
3892       next = this->next;
3893       Nlm_MemFree (this);
3894       this = next;
3895     }
3896   }
3897   if (notInside) {
3898     GetMouse (&ptool);
3899     Nlm_PointToolToPoinT (ptool, &Nlm_localMouse);
3900     if ((Nlm_WindoW) w != Nlm_WhichWindow (Nlm_localMouse)) {
3901       notInside = FALSE;
3902     }
3903   }
3904   return (! notInside);
3905 }
3906 
Nlm_CommonClick(Nlm_GraphiC w,Nlm_PoinT pt,Nlm_Boolean close,Nlm_Boolean drag,Nlm_Boolean grow,Nlm_Boolean zoom)3907 static Nlm_Boolean Nlm_CommonClick (Nlm_GraphiC w, Nlm_PoinT pt,
3908                                     Nlm_Boolean close, Nlm_Boolean drag,
3909                                     Nlm_Boolean grow, Nlm_Boolean zoom)
3910 
3911 {
3912   Nlm_Boolean     rsult;
3913   PenState        state;
3914   Nlm_PortTool    temp;
3915   Nlm_WindowTool  wptr;
3916 
3917   rsult = FALSE;
3918   if (chosenWindow == (Nlm_WindoW) w) {
3919     wptr = Nlm_ParentWindowPtr (w);
3920 #ifndef WIN_MAC_QUARTZ
3921     GetPort (&temp);
3922     GetPenState (&state);
3923 #endif
3924 #ifndef WIN_MAC_QUARTZ
3925     Nlm_SetPortWindowPort(wptr);
3926     Nlm_currentWindowTool = wptr;
3927     Nlm_SetUpdateRegion (wptr);
3928     Nlm_ResetDrawingTools ();
3929 #endif
3930     if ((close && Nlm_CloseClick (w, pt)) ||
3931        (drag && Nlm_DragClick (w, pt)) ||
3932        (grow && Nlm_GrowClick (w, pt)) ||
3933        (zoom && Nlm_ZoomClick (w, pt))) {
3934       rsult = TRUE;
3935     } else {
3936       rsult = Nlm_ContentClick (w, pt);
3937     }
3938 #ifndef WIN_MAC_QUARTZ
3939     Nlm_SetPort(temp);
3940     Nlm_currentWindowTool = GetWindowFromPort(temp);
3941     Nlm_SetUpdateRegion (GetWindowFromPort(temp));
3942     SetPenState (&state);
3943 #endif
3944   } else if (chosenWindow != NULL) {
3945     Nlm_DoSelect ((Nlm_GraphiC) chosenWindow, TRUE);
3946   }
3947   return rsult;
3948 }
3949 
Nlm_DocumentClick(Nlm_GraphiC w,Nlm_PoinT pt)3950 static Nlm_Boolean Nlm_DocumentClick (Nlm_GraphiC w, Nlm_PoinT pt)
3951 
3952 {
3953   return Nlm_CommonClick (w, pt, TRUE, TRUE, TRUE, TRUE);
3954 }
3955 
Nlm_DialogClick(Nlm_GraphiC w,Nlm_PoinT pt)3956 static Nlm_Boolean Nlm_DialogClick (Nlm_GraphiC w, Nlm_PoinT pt)
3957 
3958 {
3959   return Nlm_CommonClick (w, pt, TRUE, TRUE, FALSE, FALSE);
3960 }
3961 
Nlm_FrozenClick(Nlm_GraphiC w,Nlm_PoinT pt)3962 static Nlm_Boolean Nlm_FrozenClick (Nlm_GraphiC w, Nlm_PoinT pt)
3963 
3964 {
3965   return Nlm_CommonClick (w, pt, TRUE, FALSE, FALSE, FALSE);
3966 }
3967 
Nlm_PlainClick(Nlm_GraphiC w,Nlm_PoinT pt)3968 static Nlm_Boolean Nlm_PlainClick (Nlm_GraphiC w, Nlm_PoinT pt)
3969 
3970 {
3971   return Nlm_CommonClick (w, pt, FALSE, FALSE, FALSE, FALSE);
3972 }
3973 
Nlm_ModalClick(Nlm_GraphiC w,Nlm_PoinT pt)3974 static Nlm_Boolean Nlm_ModalClick (Nlm_GraphiC w, Nlm_PoinT pt)
3975 
3976 {
3977   Nlm_WndActnProc  cls;
3978   Nlm_Boolean      rsult;
3979   PenState         state;
3980   Nlm_PortTool     temp;
3981   Nlm_WindowData   wdata;
3982   Nlm_WindowTool   wptr;
3983 
3984   rsult = FALSE;
3985   if (chosenWindow == (Nlm_WindoW) w) {
3986     rsult = Nlm_CommonClick (w, pt, FALSE, FALSE, FALSE, FALSE);
3987     if (! rsult) {
3988       Nlm_GetWindowData ((Nlm_WindoW) w, &wdata);
3989       cls = wdata.close;
3990       if (cls != NULL) {
3991         wptr = Nlm_ParentWindowPtr (w);
3992 #ifndef WIN_MAC_QUARTZ
3993         GetPort (&temp);
3994         GetPenState (&state);
3995 #endif
3996         Nlm_SetPortWindowPort(wptr);
3997         Nlm_currentWindowTool = wptr;
3998         Nlm_SetUpdateRegion (wptr);
3999         Nlm_ResetDrawingTools ();
4000         cls ((Nlm_WindoW) w);
4001 #ifndef WIN_MAC_QUARTZ
4002         Nlm_SetPort(temp);
4003         Nlm_currentWindowTool = GetWindowFromPort(temp);
4004         Nlm_SetUpdateRegion (GetWindowFromPort(temp));
4005         SetPenState (&state);
4006 #endif
4007       }
4008     }
4009   } else {
4010     Nlm_Beep ();
4011   }
4012   return rsult;
4013 }
4014 
Nlm_MovableModalClick(Nlm_GraphiC w,Nlm_PoinT pt)4015 static Nlm_Boolean Nlm_MovableModalClick (Nlm_GraphiC w, Nlm_PoinT pt)
4016 
4017 {
4018   Nlm_WndActnProc  cls;
4019   Nlm_Boolean      rsult;
4020   PenState         state;
4021   Nlm_PortTool     temp;
4022   Nlm_WindowData   wdata;
4023   Nlm_WindowTool   wptr;
4024 
4025   rsult = FALSE;
4026   if (chosenWindow == (Nlm_WindoW) w) {
4027     rsult = Nlm_CommonClick (w, pt, FALSE, TRUE, FALSE, FALSE);
4028     if (! rsult) {
4029       Nlm_GetWindowData ((Nlm_WindoW) w, &wdata);
4030       cls = wdata.close;
4031       if (cls != NULL) {
4032         wptr = Nlm_ParentWindowPtr (w);
4033 #ifndef WIN_MAC_QUARTZ
4034         GetPort (&temp);
4035         GetPenState (&state);
4036 #endif
4037         Nlm_SetPortWindowPort(wptr);
4038         Nlm_currentWindowTool = wptr;
4039         Nlm_SetUpdateRegion (wptr);
4040         Nlm_ResetDrawingTools ();
4041         cls ((Nlm_WindoW) w);
4042 #ifndef WIN_MAC_QUARTZ
4043         Nlm_SetPort(temp);
4044         Nlm_currentWindowTool = GetWindowFromPort(temp);
4045         Nlm_SetUpdateRegion(GetWindowFromPort(temp));
4046         SetPenState (&state);
4047 #endif
4048       }
4049     }
4050   } else {
4051     Nlm_Beep ();
4052   }
4053   return rsult;
4054 }
4055 
Nlm_FloatingClick(Nlm_GraphiC w,Nlm_PoinT pt)4056 static Nlm_Boolean Nlm_FloatingClick (Nlm_GraphiC w, Nlm_PoinT pt)
4057 
4058 {
4059   Nlm_WindoW  nw;
4060 
4061   if (chosenWindow == (Nlm_WindoW) w) {
4062     Nlm_CommonClick (w, Nlm_globalMouse, TRUE, TRUE, FALSE, FALSE);
4063   } else {
4064     nw = Nlm_NextVisWindow ((Nlm_WindoW) w);
4065     Nlm_DoClick ((Nlm_GraphiC) nw, Nlm_globalMouse);
4066   }
4067   return TRUE;
4068 }
4069 
4070 /* 2001-03-22:  Joshua Juran
4071  * SystemClick() is not supported in Carbon.  It's unnecessary.
4072  */
4073 #if TARGET_API_MAC_CARBON
4074 # define SystemClick(event, window)
4075 #endif
Nlm_SystemClick(Nlm_GraphiC w,Nlm_PoinT pt)4076 static Nlm_Boolean Nlm_SystemClick (Nlm_GraphiC w, Nlm_PoinT pt)
4077 {
4078   Nlm_WindowTool  wptr;
4079 
4080   if (chosenWindow == (Nlm_WindoW) w) {
4081     wptr = Nlm_ParentWindowPtr (w);
4082     SystemClick (&Nlm_currentEvent, wptr);
4083   } else {
4084     Nlm_DoSelect ((Nlm_GraphiC) chosenWindow, TRUE);
4085   }
4086   return TRUE;
4087 }
4088 
Nlm_DesktopClick(Nlm_GraphiC w,Nlm_PoinT pt)4089 static Nlm_Boolean Nlm_DesktopClick (Nlm_GraphiC w, Nlm_PoinT pt)
4090 
4091 {
4092   Nlm_MenuBaR  mb;
4093 
4094   mb = Nlm_GetWindowMenuBar ((Nlm_WindoW) w);
4095   Nlm_localMouse = Nlm_globalMouse;
4096   Nlm_GlobalToLocal (&Nlm_localMouse);
4097   (void) (Nlm_GetEnabled ((Nlm_GraphiC) mb) && Nlm_GetVisible ((Nlm_GraphiC) mb) &&
4098           Nlm_DoClick ((Nlm_GraphiC) mb, Nlm_localMouse));
4099   return TRUE;
4100 }
4101 #endif
4102 
Nlm_NormalSelect(Nlm_GraphiC w,Nlm_Boolean savePort)4103 static void Nlm_NormalSelect (Nlm_GraphiC w, Nlm_Boolean savePort)
4104 
4105 {
4106 #ifdef WIN_MAC
4107   if ((! Nlm_DragClick (w, Nlm_localMouse)) || (! Nlm_cmmdKey)) {
4108     Nlm_SelectWindow (w, FALSE);
4109   }
4110 #endif
4111 #ifdef WIN_MSWIN
4112   Nlm_SelectWindow (w, FALSE);
4113 #endif
4114 #ifdef WIN_MOTIF
4115   Nlm_SelectWindow (w, FALSE);
4116 #endif
4117 }
4118 
Nlm_PlainSelect(Nlm_GraphiC w,Nlm_Boolean savePort)4119 static void Nlm_PlainSelect (Nlm_GraphiC w, Nlm_Boolean savePort)
4120 
4121 {
4122 #ifdef WIN_MAC
4123   Nlm_SelectWindow (w, FALSE);
4124 #endif
4125 #ifdef WIN_MSWIN
4126   Nlm_SelectWindow (w, FALSE);
4127 #endif
4128 #ifdef WIN_MOTIF
4129   Nlm_SelectWindow (w, FALSE);
4130 #endif
4131 }
4132 
Nlm_FloatingSelect(Nlm_GraphiC w,Nlm_Boolean savePort)4133 static void Nlm_FloatingSelect (Nlm_GraphiC w, Nlm_Boolean savePort)
4134 
4135 {
4136 #ifdef WIN_MAC
4137   PenState        state;
4138   Nlm_PortTool    temp;
4139   Nlm_WindowTool  wptr;
4140 
4141   wptr = Nlm_ParentWindowPtr (w);
4142 #ifndef WIN_MAC_QUARTZ
4143   GetPort (&temp);
4144   GetPenState (&state);
4145 #endif
4146   Nlm_SetPortWindowPort(wptr);
4147   Nlm_currentWindowTool = wptr;
4148   Nlm_SetUpdateRegion (wptr);
4149   Nlm_ResetDrawingTools ();
4150   Nlm_localMouse = Nlm_globalMouse;
4151   Nlm_GlobalToLocal (&Nlm_localMouse);
4152   if (Nlm_DragClick (w, Nlm_globalMouse) || Nlm_CloseClick (w, Nlm_globalMouse)) {
4153   } else {
4154     Nlm_ContentClick (w, Nlm_globalMouse);
4155   }
4156 #ifndef WIN_MAC_QUARTZ
4157   Nlm_SetPort(temp);
4158   Nlm_currentWindowTool = GetWindowFromPort(temp);
4159   Nlm_SetUpdateRegion (GetWindowFromPort(temp));
4160   SetPenState (&state);
4161 #endif
4162   Nlm_localMouse = Nlm_globalMouse;
4163   Nlm_GlobalToLocal (&Nlm_localMouse);
4164 #endif
4165 #ifdef WIN_MSWIN
4166   Nlm_SelectWindow (w, FALSE);
4167 #endif
4168 #ifdef WIN_MOTIF
4169   Nlm_SelectWindow (w, FALSE);
4170 #endif
4171 }
4172 
Nlm_DesktopSelect(Nlm_GraphiC w,Nlm_Boolean savePort)4173 static void Nlm_DesktopSelect (Nlm_GraphiC w, Nlm_Boolean savePort)
4174 
4175 {
4176 #ifdef WIN_MAC
4177   PenState      state;
4178   Nlm_PortTool  temp;
4179 
4180 #ifndef WIN_MAC_QUARTZ
4181   GetPort (&temp);
4182   GetPenState (&state);
4183   PenNormal ();
4184 #endif
4185   Nlm_DesktopClick (w, Nlm_globalMouse);
4186 #ifndef WIN_MAC_QUARTZ
4187   Nlm_SetPort(temp);
4188   Nlm_currentWindowTool = GetWindowFromPort(temp);
4189   Nlm_SetUpdateRegion (GetWindowFromPort(temp));
4190   SetPenState (&state);
4191 #endif
4192 #endif
4193 }
4194 
4195 #ifdef WIN_MAC
Nlm_DesktopKey(Nlm_GraphiC w,Nlm_Char ch)4196 static Nlm_Boolean Nlm_DesktopKey (Nlm_GraphiC w, Nlm_Char ch)
4197 
4198 {
4199   Nlm_MenuBaR  mb;
4200 
4201   mb = Nlm_GetWindowMenuBar ((Nlm_WindoW) w);
4202   Nlm_localMouse = Nlm_globalMouse;
4203   Nlm_GlobalToLocal (&Nlm_localMouse);
4204   if (Nlm_currentKey != '\0') {
4205     (void) (Nlm_GetEnabled ((Nlm_GraphiC) mb) && Nlm_GetVisible ((Nlm_GraphiC) mb) &&
4206             Nlm_DoKey ((Nlm_GraphiC) mb, ch));
4207   }
4208   return TRUE;
4209 }
4210 
Nlm_NormalKey(Nlm_GraphiC w,Nlm_Char ch)4211 static Nlm_Boolean Nlm_NormalKey (Nlm_GraphiC w, Nlm_Char ch)
4212 
4213 {
4214   Nlm_GraphiC     g;
4215   Nlm_MenuBaR     mb;
4216   Nlm_GraphiC     n;
4217   Nlm_Boolean     notInside;
4218   PenState        state;
4219   Nlm_PortTool    temp;
4220   Nlm_WindowTool  wptr;
4221 
4222   wptr = Nlm_ParentWindowPtr (w);
4223 #ifndef WIN_MAC_QUARTZ
4224   GetPort (&temp);
4225   GetPenState (&state);
4226   Nlm_SetPortWindowPort(wptr);
4227   Nlm_currentWindowTool = wptr;
4228   Nlm_SetUpdateRegion (wptr);
4229   Nlm_ResetDrawingTools ();
4230 #endif
4231   Nlm_localMouse = Nlm_globalMouse;
4232   Nlm_GlobalToLocal (&Nlm_localMouse);
4233   if (Nlm_currentKey != '\0') {
4234     g = Nlm_GetChild (w);
4235     notInside = TRUE;
4236     mb = Nlm_GetWindowMenuBar ((Nlm_WindoW) w);
4237     if (mb != NULL) {
4238       if (Nlm_GetEnabled ((Nlm_GraphiC) mb) && Nlm_GetVisible ((Nlm_GraphiC) mb) &&
4239           Nlm_DoKey ((Nlm_GraphiC) mb, ch)) {
4240         notInside = FALSE;
4241       }
4242     }
4243     while (g != NULL && notInside) {
4244       n = Nlm_GetNext (g);
4245       if (Nlm_GetEnabled (g) && Nlm_GetVisible (g) && Nlm_DoKey (g, ch)) {
4246         notInside = FALSE;
4247       }
4248       g = n;
4249     }
4250     if (notInside && Nlm_cmmdKey) {
4251       Nlm_DesktopKey ((Nlm_GraphiC) Nlm_desktopWindow, ch);
4252     }
4253   }
4254 #ifndef WIN_MAC_QUARTZ
4255   Nlm_SetPort(temp);
4256   Nlm_currentWindowTool = GetWindowFromPort(temp);
4257   Nlm_SetUpdateRegion(GetWindowFromPort(temp));
4258   SetPenState (&state);
4259 #endif
4260   return TRUE;
4261 }
4262 
Nlm_FloatingKey(Nlm_GraphiC w,Nlm_Char ch)4263 static Nlm_Boolean Nlm_FloatingKey (Nlm_GraphiC w, Nlm_Char ch)
4264 
4265 {
4266   Nlm_GraphiC     g;
4267   Nlm_MenuBaR     mb;
4268   Nlm_GraphiC     n;
4269   Nlm_WindoW      nw;
4270   Nlm_Boolean     notInside;
4271   PenState        state;
4272   Nlm_PortTool    temp;
4273   Nlm_WindowTool  wptr;
4274 
4275   wptr = Nlm_ParentWindowPtr (w);
4276 #ifndef WIN_MAC_QUARTZ
4277   GetPort (&temp);
4278   GetPenState (&state);
4279 #endif
4280   Nlm_SetPortWindowPort(wptr);
4281   Nlm_currentWindowTool = wptr;
4282   Nlm_SetUpdateRegion (wptr);
4283   Nlm_ResetDrawingTools ();
4284   Nlm_localMouse = Nlm_globalMouse;
4285   Nlm_GlobalToLocal (&Nlm_localMouse);
4286   if (Nlm_currentKey != '\0') {
4287     g = Nlm_GetChild (w);
4288     notInside = TRUE;
4289     mb = Nlm_GetWindowMenuBar ((Nlm_WindoW) w);
4290     if (mb != NULL) {
4291       if (Nlm_GetEnabled ((Nlm_GraphiC) mb) && Nlm_GetVisible ((Nlm_GraphiC) mb) &&
4292           Nlm_DoKey ((Nlm_GraphiC) mb, ch)) {
4293         notInside = FALSE;
4294       }
4295     }
4296     while (g != NULL && notInside) {
4297       n = Nlm_GetNext (g);
4298       if (Nlm_GetEnabled (g) && Nlm_GetVisible (g) && Nlm_DoKey (g, ch)) {
4299         notInside = FALSE;
4300       }
4301       g = n;
4302     }
4303     if (notInside) {
4304       nw = Nlm_NextVisWindow ((Nlm_WindoW) w);
4305       Nlm_DoKey ((Nlm_GraphiC) nw, ch);
4306     }
4307   }
4308 #ifndef WIN_MAC_QUARTZ
4309   Nlm_SetPort(temp);
4310   Nlm_currentWindowTool = GetWindowFromPort(temp);
4311   Nlm_SetUpdateRegion(GetWindowFromPort(temp));
4312   SetPenState (&state);
4313 #endif
4314   return TRUE;
4315 }
4316 
Nlm_ModalKey(Nlm_GraphiC w,Nlm_Char ch)4317 static Nlm_Boolean Nlm_ModalKey (Nlm_GraphiC w, Nlm_Char ch)
4318 
4319 {
4320   if (Nlm_cmmdKey) {
4321     Nlm_Beep ();
4322   } else {
4323     Nlm_NormalKey (w, ch);
4324   }
4325   return TRUE;
4326 }
4327 
Nlm_DrawWindow(Nlm_GraphiC w,Nlm_Boolean drawGrowIcon)4328 static void Nlm_DrawWindow (Nlm_GraphiC w, Nlm_Boolean drawGrowIcon)
4329 
4330 {
4331   Nlm_GraphiC     g;
4332   Nlm_MenuBaR     mb;
4333   Nlm_GraphiC     n;
4334   Nlm_RecT        r;
4335   PenState        state;
4336   Nlm_PortTool    temp;
4337   Nlm_WindowTool  wptr;
4338   Rect bounds;
4339 
4340 #ifndef WIN_MAC_QUARTZ
4341   wptr = Nlm_ParentWindowPtr (w);
4342   GetPort (&temp);
4343   GetPenState (&state);
4344   Nlm_SetPortWindowPort(wptr);
4345   Nlm_currentWindowTool = wptr;
4346 
4347   Nlm_ResetDrawingTools ();
4348   Nlm_ResetClip ();
4349   PenNormal ();
4350 
4351   BeginUpdate (wptr);
4352   Nlm_SetUpdateRegion (wptr);
4353   GetPortBounds(GetWindowPort(wptr), &bounds);
4354   Nlm_RectToolToRecT (&bounds, &r);
4355   Nlm_EraseRect (&r);
4356 
4357 #endif
4358   if (okayToDrawContents) {
4359     if (drawGrowIcon) {
4360       Nlm_DrawGrowIcon (w, FALSE, TRUE);
4361     }
4362     mb = Nlm_GetWindowMenuBar ((Nlm_WindoW) w);
4363     if (mb != NULL) {
4364       Nlm_DoDraw ((Nlm_GraphiC) mb);
4365     }
4366     g = Nlm_GetChild (w);
4367     while (g != NULL) {
4368       n = Nlm_GetNext (g);
4369       Nlm_DoDraw (g);
4370       g = n;
4371     }
4372     if (drawGrowIcon) {
4373       Nlm_DrawGrowIcon (w, TRUE, FALSE);
4374     }
4375   }
4376 
4377 #ifndef WIN_MAC_QUARTZ
4378   EndUpdate (wptr);
4379 #endif
4380   Nlm_ResetDrawingTools ();
4381 #ifndef WIN_MAC_QUARTZ
4382   Nlm_ResetClip ();
4383   Nlm_SetPort(temp);
4384   Nlm_currentWindowTool = GetWindowFromPort(temp);
4385   Nlm_SetUpdateRegion(GetWindowFromPort(temp));
4386   SetPenState (&state);
4387 #endif
4388 }
4389 
Nlm_DocumentDraw(Nlm_GraphiC w)4390 static void Nlm_DocumentDraw (Nlm_GraphiC w)
4391 
4392 {
4393   Nlm_DrawWindow (w, TRUE);
4394 }
4395 
Nlm_NormalDraw(Nlm_GraphiC w)4396 static void Nlm_NormalDraw (Nlm_GraphiC w)
4397 
4398 {
4399   Nlm_DrawWindow (w, FALSE);
4400 }
4401 #endif
4402 
Nlm_ActivateWindow(Nlm_GraphiC w,Nlm_Boolean drawGrow)4403 static void Nlm_ActivateWindow (Nlm_GraphiC w, Nlm_Boolean drawGrow)
4404 
4405 {
4406   Nlm_WndActnProc  act;
4407   Nlm_GraphiC      g;
4408   Nlm_MenuBaR      mb;
4409   Nlm_GraphiC      n;
4410   Nlm_WindowData   wdata;
4411 
4412   Nlm_UseWindow ((Nlm_WindoW) w);
4413 #ifdef WIN_MAC
4414   if (drawGrow) {
4415     Nlm_DrawGrowIcon (w, TRUE, TRUE);
4416   }
4417 #endif
4418   mb = Nlm_GetWindowMenuBar ((Nlm_WindoW) w);
4419   if (mb != NULL) {
4420     Nlm_DoActivate ((Nlm_GraphiC) mb, FALSE);
4421   }
4422   g = Nlm_GetChild (w);
4423   while (g != NULL) {
4424     n = Nlm_GetNext (g);
4425     Nlm_DoActivate (g, FALSE);
4426     g = n;
4427   }
4428   Nlm_GetWindowData ((Nlm_WindoW) w, &wdata);
4429   act = wdata.activate;
4430   if (act != NULL) {
4431     act ((Nlm_WindoW) w);
4432   }
4433   if (appWndAct != NULL) {
4434     appWndAct ((Nlm_WindoW) w);
4435   }
4436 }
4437 
Nlm_DocumentActivate(Nlm_GraphiC w,Nlm_Boolean savePort)4438 static void Nlm_DocumentActivate (Nlm_GraphiC w, Nlm_Boolean savePort)
4439 
4440 {
4441   Nlm_ActivateWindow (w, TRUE);
4442 }
4443 
Nlm_NormalActivate(Nlm_GraphiC w,Nlm_Boolean savePort)4444 static void Nlm_NormalActivate (Nlm_GraphiC w, Nlm_Boolean savePort)
4445 
4446 {
4447   Nlm_ActivateWindow (w, FALSE);
4448 }
4449 
Nlm_DeactivateWindow(Nlm_GraphiC w,Nlm_Boolean drawGrow)4450 static void Nlm_DeactivateWindow (Nlm_GraphiC w, Nlm_Boolean drawGrow)
4451 
4452 {
4453   Nlm_WndActnProc  deact;
4454   Nlm_GraphiC      g;
4455   Nlm_MenuBaR      mb;
4456   Nlm_GraphiC      n;
4457   Nlm_WindowData   wdata;
4458 
4459   Nlm_UseWindow ((Nlm_WindoW) w);
4460 #ifdef WIN_MAC
4461   if (drawGrow) {
4462     Nlm_DrawGrowIcon (w, TRUE, TRUE);
4463   }
4464 #endif
4465   mb = Nlm_GetWindowMenuBar ((Nlm_WindoW) w);
4466   if (mb != NULL) {
4467     Nlm_DoDeactivate ((Nlm_GraphiC) mb, FALSE);
4468   }
4469   g = Nlm_GetChild (w);
4470   while (g != NULL) {
4471     n = Nlm_GetNext (g);
4472     Nlm_DoDeactivate (g, FALSE);
4473     g = n;
4474   }
4475   Nlm_GetWindowData ((Nlm_WindoW) w, &wdata);
4476   deact = wdata.deactivate;
4477   if (deact != NULL) {
4478     deact ((Nlm_WindoW) w);
4479   }
4480   if (appWndDeact != NULL) {
4481     appWndDeact ((Nlm_WindoW) w);
4482   }
4483 }
4484 
Nlm_DocumentDeactivate(Nlm_GraphiC w,Nlm_Boolean savePort)4485 static void Nlm_DocumentDeactivate (Nlm_GraphiC w, Nlm_Boolean savePort)
4486 
4487 {
4488   Nlm_DeactivateWindow (w, TRUE);
4489 }
4490 
Nlm_NormalDeactivate(Nlm_GraphiC w,Nlm_Boolean savePort)4491 static void Nlm_NormalDeactivate (Nlm_GraphiC w, Nlm_Boolean savePort)
4492 
4493 {
4494   Nlm_DeactivateWindow (w, FALSE);
4495 }
4496 
4497 #ifdef WIN_MAC
Nlm_NormalIdle(Nlm_GraphiC w,Nlm_PoinT pt)4498 static Nlm_Boolean Nlm_NormalIdle (Nlm_GraphiC w, Nlm_PoinT pt)
4499 
4500 {
4501   Nlm_GraphiC  g;
4502   Nlm_GraphiC  n;
4503 
4504   g = Nlm_GetChild (w);
4505   while (g != NULL) {
4506     n = Nlm_GetNext (g);
4507     Nlm_DoIdle (g, Nlm_localMouse);
4508     g = n;
4509   }
4510   return TRUE;
4511 }
4512 
Nlm_FloatingIdle(Nlm_GraphiC w,Nlm_PoinT pt)4513 static Nlm_Boolean Nlm_FloatingIdle (Nlm_GraphiC w, Nlm_PoinT pt)
4514 
4515 {
4516   Nlm_WindoW   nw;
4517 
4518   nw = Nlm_NextVisWindow ((Nlm_WindoW) w);
4519   Nlm_DoIdle ((Nlm_GraphiC) nw, pt);
4520   return TRUE;
4521 }
4522 #endif
4523 
Nlm_NormalDrawChar(Nlm_GraphiC w,Nlm_Char ch,Nlm_Boolean savePort)4524 static void Nlm_NormalDrawChar (Nlm_GraphiC w, Nlm_Char ch, Nlm_Boolean savePort)
4525 
4526 {
4527   Nlm_DisplaY     d;
4528   Nlm_WindowData  wdata;
4529 
4530   Nlm_GetWindowData ((Nlm_WindoW) w, &wdata);
4531   d = wdata.charDisplay;
4532   if (d != NULL) {
4533     Nlm_DoSendChar ((Nlm_GraphiC) d, ch, TRUE);
4534   }
4535 }
4536 
Nlm_FloatingDrawChar(Nlm_GraphiC w,Nlm_Char ch,Nlm_Boolean savePort)4537 static void Nlm_FloatingDrawChar (Nlm_GraphiC w, Nlm_Char ch, Nlm_Boolean savePort)
4538 
4539 {
4540   Nlm_DisplaY     d;
4541   Nlm_WindowData  wdata;
4542 #ifdef WIN_MAC
4543   Nlm_WindoW      nw;
4544 #endif
4545 
4546   Nlm_GetWindowData ((Nlm_WindoW) w, &wdata);
4547   d = wdata.charDisplay;
4548   if (d != NULL) {
4549     Nlm_DoSendChar ((Nlm_GraphiC) d, ch, TRUE);
4550   } else {
4551 #ifdef WIN_MAC
4552     nw = Nlm_NextVisWindow ((Nlm_WindoW) w);
4553     Nlm_DoSendChar ((Nlm_GraphiC) nw, ch, TRUE);
4554 #endif
4555 #ifdef WIN_MOTIF
4556 #endif
4557   }
4558 }
4559 
Nlm_AdjustWindow(Nlm_GraphiC w,Nlm_RectPtr r,Nlm_Boolean align,Nlm_Boolean savePort)4560 static void Nlm_AdjustWindow (Nlm_GraphiC w, Nlm_RectPtr r,
4561                               Nlm_Boolean align, Nlm_Boolean savePort)
4562 
4563 {
4564   Nlm_GraphiC  g;
4565   Nlm_WindoW   tempPort;
4566 
4567   tempPort = Nlm_SavePortIfNeeded (w, savePort);
4568   if (align) {
4569     g = Nlm_GetParent (w);
4570     Nlm_RecordRect (g, r);
4571     Nlm_NextPosition (g, r);
4572   }
4573   Nlm_RestorePort (tempPort);
4574 }
4575 
Nlm_WindowGainFocus(Nlm_GraphiC w,Nlm_Char ch,Nlm_Boolean savePort)4576 static Nlm_GraphiC Nlm_WindowGainFocus (Nlm_GraphiC w, Nlm_Char ch, Nlm_Boolean savePort)
4577 
4578 {
4579   Nlm_GraphiC  p;
4580   Nlm_GraphiC  q;
4581   Nlm_GraphiC  n;
4582 
4583   q = NULL;
4584   p = Nlm_GetChild (w);
4585   while (p != NULL && q == NULL) {
4586     n = Nlm_GetNext (p);
4587     q = Nlm_DoGainFocus (p, ch, savePort);
4588     p = n;
4589   }
4590   return q;
4591 }
4592 
Nlm_WindowLoseFocus(Nlm_GraphiC w,Nlm_GraphiC excpt,Nlm_Boolean savePort)4593 static void Nlm_WindowLoseFocus (Nlm_GraphiC w, Nlm_GraphiC excpt, Nlm_Boolean savePort)
4594 
4595 {
4596   Nlm_GraphiC  p;
4597 
4598   p = Nlm_GetChild (w);
4599   while (p != NULL) {
4600     Nlm_DoLoseFocus (p, excpt, savePort);
4601     p = Nlm_GetNext (p);
4602   }
4603 }
4604 
Nlm_DocumentWindow(Nlm_Int2 left,Nlm_Int2 top,Nlm_Int2 width,Nlm_Int2 height,Nlm_CharPtr title,Nlm_WndActnProc close,Nlm_WndActnProc resize)4605 extern Nlm_WindoW Nlm_DocumentWindow (Nlm_Int2 left, Nlm_Int2 top,
4606                                       Nlm_Int2 width, Nlm_Int2 height,
4607                                       Nlm_CharPtr title,
4608                                       Nlm_WndActnProc close,
4609                                       Nlm_WndActnProc resize)
4610 
4611 {
4612   Nlm_RecT    r;
4613   Nlm_WindoW  w;
4614 
4615   Nlm_LoadRect (&r, left, top, width, height);
4616   w = Nlm_MakeWindowLink (&r, sizeof (Nlm_WindowRec), documentProcs);
4617   if (w != NULL) {
4618 #ifdef WIN_MAC
4619     Nlm_NewWindow (w, DOCUMENT_STYLE, 8, TRUE, NULL, 0, title, close, resize);
4620 #endif
4621 #ifdef WIN_MSWIN
4622     Nlm_NewWindow (w, DOCUMENT_STYLE, 0, FALSE, windowclass, WS_OVERLAPPEDWINDOW, title, close, resize);
4623 #endif
4624 #ifdef WIN_MOTIF
4625     Nlm_NewWindow (w, DOCUMENT_STYLE, 0, FALSE, NULL, 0, title, close, resize);
4626 #endif
4627   }
4628   return w;
4629 }
4630 
Nlm_FixedWindow(Nlm_Int2 left,Nlm_Int2 top,Nlm_Int2 width,Nlm_Int2 height,Nlm_CharPtr title,Nlm_WndActnProc close)4631 extern Nlm_WindoW Nlm_FixedWindow (Nlm_Int2 left, Nlm_Int2 top,
4632                                    Nlm_Int2 width, Nlm_Int2 height,
4633                                    Nlm_CharPtr title, Nlm_WndActnProc close)
4634 
4635 {
4636   Nlm_RecT    r;
4637   Nlm_WindoW  w;
4638 
4639   Nlm_LoadRect (&r, left, top, width, height);
4640   w = Nlm_MakeWindowLink (&r, sizeof (Nlm_WindowRec), fixedProcs);
4641   if (w != NULL) {
4642 #ifdef WIN_MAC
4643     Nlm_NewWindow (w, FIXED_STYLE, 4, TRUE, NULL, 0, title, close, NULL);
4644 #endif
4645 #ifdef WIN_MSWIN
4646     Nlm_NewWindow (w, FIXED_STYLE, 0, FALSE, windowclass, WS_OVERLAPPED |
4647                    WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, title, close, NULL);
4648 #endif
4649 #ifdef WIN_MOTIF
4650     Nlm_NewWindow (w, FIXED_STYLE, 0, FALSE, NULL, 0, title, close, NULL);
4651 #endif
4652   }
4653   return w;
4654 }
4655 
Nlm_FrozenWindow(Nlm_Int2 left,Nlm_Int2 top,Nlm_Int2 width,Nlm_Int2 height,Nlm_CharPtr title,Nlm_WndActnProc close)4656 extern Nlm_WindoW Nlm_FrozenWindow (Nlm_Int2 left, Nlm_Int2 top,
4657                                     Nlm_Int2 width, Nlm_Int2 height,
4658                                     Nlm_CharPtr title, Nlm_WndActnProc close)
4659 
4660 {
4661   Nlm_RecT    r;
4662   Nlm_WindoW  w;
4663 
4664   Nlm_LoadRect (&r, left, top, width, height);
4665   w = Nlm_MakeWindowLink (&r, sizeof (Nlm_WindowRec), frozenProcs);
4666   if (w != NULL) {
4667 #ifdef WIN_MAC
4668     Nlm_NewWindow (w, FROZEN_STYLE, 4, TRUE, NULL, 0, title, close, NULL);
4669 #endif
4670 #ifdef WIN_MSWIN
4671     Nlm_NewWindow (w, FROZEN_STYLE, 0, FALSE, windowclass, WS_OVERLAPPED |
4672                    WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, title, close, NULL);
4673 #endif
4674 #ifdef WIN_MOTIF
4675     Nlm_NewWindow (w, FROZEN_STYLE, 0, FALSE, NULL, 0, title, close, NULL);
4676 #endif
4677   }
4678   return w;
4679 }
4680 
Nlm_RoundWindow(Nlm_Int2 left,Nlm_Int2 top,Nlm_Int2 width,Nlm_Int2 height,Nlm_CharPtr title,Nlm_WndActnProc close)4681 extern Nlm_WindoW Nlm_RoundWindow (Nlm_Int2 left, Nlm_Int2 top,
4682                                    Nlm_Int2 width, Nlm_Int2 height,
4683                                    Nlm_CharPtr title, Nlm_WndActnProc close)
4684 
4685 {
4686   Nlm_RecT    r;
4687   Nlm_WindoW  w;
4688 
4689   Nlm_LoadRect (&r, left, top, width, height);
4690   w = Nlm_MakeWindowLink (&r, sizeof (Nlm_WindowRec), roundProcs);
4691   if (w != NULL) {
4692 #ifdef WIN_MAC
4693     Nlm_NewWindow (w, ROUND_STYLE, 16, TRUE, NULL, 0, title, close, NULL);
4694 #endif
4695 #ifdef WIN_MSWIN
4696     Nlm_NewWindow (w, ROUND_STYLE, 0, FALSE, windowclass, WS_OVERLAPPED |
4697                    WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, title, close, NULL);
4698 #endif
4699 #ifdef WIN_MOTIF
4700     Nlm_NewWindow (w, ROUND_STYLE, 0, FALSE, NULL, 0, title, close, NULL);
4701 #endif
4702   }
4703   return w;
4704 }
4705 
Nlm_AlertWindow(Nlm_Int2 left,Nlm_Int2 top,Nlm_Int2 width,Nlm_Int2 height,Nlm_WndActnProc close)4706 extern Nlm_WindoW Nlm_AlertWindow (Nlm_Int2 left, Nlm_Int2 top,
4707                                    Nlm_Int2 width, Nlm_Int2 height,
4708                                    Nlm_WndActnProc close)
4709 
4710 {
4711   Nlm_RecT    r;
4712   Nlm_WindoW  w;
4713 
4714   Nlm_LoadRect (&r, left, top, width, height);
4715   w = Nlm_MakeWindowLink (&r, sizeof (Nlm_WindowRec), alertProcs);
4716   if (w != NULL) {
4717 #ifdef WIN_MAC
4718     Nlm_NewWindow (w, ALERT_STYLE, 1, FALSE, NULL, 0, NULL, close, NULL);
4719 #endif
4720 #ifdef WIN_MSWIN
4721     Nlm_NewWindow (w, ALERT_STYLE, 0, FALSE, windowclass, WS_OVERLAPPED, "", close, NULL);
4722 #endif
4723 #ifdef WIN_MOTIF
4724     Nlm_NewWindow (w, ALERT_STYLE, 0, FALSE, NULL, 0, "", close, NULL);
4725 #endif
4726   }
4727   return w;
4728 }
4729 
4730 /* esl: set the "owner" window of modal dialog */
Nlm_SetModalWindowOwner(Nlm_WindoW w,Nlm_WindoW owner)4731 extern void Nlm_SetModalWindowOwner (Nlm_WindoW w, Nlm_WindoW owner)
4732 {
4733   if (w != NULL) {
4734     Nlm_WindowData wdata;
4735     Nlm_GetWindowData (w, &wdata);
4736     wdata.modalOwner = owner;
4737     Nlm_SetWindowData (w, &wdata);
4738   }
4739 }
4740 
Nlm_ModalWindow(Nlm_Int2 left,Nlm_Int2 top,Nlm_Int2 width,Nlm_Int2 height,Nlm_WndActnProc close)4741 extern Nlm_WindoW Nlm_ModalWindow (Nlm_Int2 left, Nlm_Int2 top,
4742                                    Nlm_Int2 width, Nlm_Int2 height,
4743                                    Nlm_WndActnProc close)
4744 
4745 {
4746   Nlm_RecT    r;
4747   Nlm_WindoW  w;
4748   Nlm_WindoW  owner = Nlm_ActiveWindow (); /* esl: reasonable guess */
4749 
4750   Nlm_LoadRect (&r, left, top, width, height);
4751   w = Nlm_MakeWindowLink (&r, sizeof (Nlm_WindowRec), modalProcs);
4752   if (w != NULL) {
4753 #ifdef WIN_MAC
4754     Nlm_NewWindow (w, MODAL_STYLE, 1, FALSE, NULL, 0, NULL, close, NULL);
4755 #endif
4756 #ifdef WIN_MSWIN
4757     Nlm_NewWindow (w, MODAL_STYLE, 0, FALSE, windowclass, WS_POPUP |
4758                    WS_CAPTION | WS_SYSMENU, "", close, NULL);
4759 #endif
4760 #ifdef WIN_MOTIF
4761     Nlm_NewWindow (w, MODAL_STYLE, 0, FALSE, NULL, 0, "", close, NULL);
4762 #endif
4763   }
4764   Nlm_SetModalWindowOwner (w, owner); /* esl */
4765   return w;
4766 }
4767 
Nlm_ResizableModalWindow(Nlm_Int2 left,Nlm_Int2 top,Nlm_Int2 width,Nlm_Int2 height,Nlm_CharPtr title,Nlm_WndActnProc close,Nlm_WndActnProc resize)4768 extern Nlm_WindoW Nlm_ResizableModalWindow(Nlm_Int2 left, Nlm_Int2 top,
4769     Nlm_Int2 width, Nlm_Int2 height,
4770     Nlm_CharPtr title,
4771     Nlm_WndActnProc close,
4772     Nlm_WndActnProc resize)
4773 {
4774     Nlm_RecT    r;
4775     Nlm_WindoW  w = 0;
4776     Nlm_WindoW  owner = Nlm_ActiveWindow();
4777 
4778     Nlm_LoadRect(&r, left, top, width, height);
4779     w = Nlm_MakeWindowLink(&r, sizeof(Nlm_WindowRec), movableModalProcs);
4780     if (w != NULL) {
4781 #ifdef WIN_MAC
4782         Nlm_NewWindow(w, MODAL_STYLE, 5, FALSE, NULL, 0, title, close, resize);
4783 #endif
4784 #ifdef WIN_MSWIN
4785         Nlm_NewWindow(w, MODAL_STYLE, 0, FALSE, windowclass, WS_POPUP |
4786             WS_CAPTION | WS_SYSMENU | WS_THICKFRAME, title, close, resize);
4787 #endif
4788 #ifdef WIN_MOTIF
4789         Nlm_NewWindow(w, MODAL_STYLE, 0, FALSE, NULL, 0, title, close, resize);
4790 #endif
4791     }
4792     Nlm_SetModalWindowOwner(w, owner);
4793 
4794     return w;
4795 }
4796 
4797 /* esl: movable dialog with caption */
Nlm_MovableModalWindow(Nlm_Int2 left,Nlm_Int2 top,Nlm_Int2 width,Nlm_Int2 height,Nlm_CharPtr title,Nlm_WndActnProc close)4798 extern Nlm_WindoW Nlm_MovableModalWindow (Nlm_Int2 left, Nlm_Int2 top,
4799                                           Nlm_Int2 width, Nlm_Int2 height,
4800                                           Nlm_CharPtr title,
4801                                           Nlm_WndActnProc close)
4802 {
4803   Nlm_RecT    r;
4804   Nlm_WindoW  w;
4805   Nlm_WindoW  owner = Nlm_ActiveWindow (); /* esl: reasonable guess */
4806 
4807   Nlm_LoadRect (&r, left, top, width, height);
4808   w = Nlm_MakeWindowLink (&r, sizeof (Nlm_WindowRec), movableModalProcs);
4809   if (w != NULL) {
4810 #ifdef WIN_MAC
4811     Nlm_NewWindow (w, MODAL_STYLE, 5, FALSE, NULL, 0, title, close, NULL);
4812 #endif
4813 #ifdef WIN_MSWIN
4814     Nlm_NewWindow (w, MODAL_STYLE, 0, FALSE, windowclass, WS_POPUP |
4815                    WS_CAPTION | WS_SYSMENU, title, close, NULL);
4816 #endif
4817 #ifdef WIN_MOTIF
4818     Nlm_NewWindow (w, MODAL_STYLE, 0, FALSE, NULL, 0, title, close, NULL);
4819 #endif
4820   }
4821   Nlm_SetModalWindowOwner (w, owner); /* esl */
4822   return w;
4823 }
4824 
Nlm_FloatingWindow(Nlm_Int2 left,Nlm_Int2 top,Nlm_Int2 width,Nlm_Int2 height,Nlm_WndActnProc close)4825 extern Nlm_WindoW Nlm_FloatingWindow (Nlm_Int2 left, Nlm_Int2 top,
4826                                       Nlm_Int2 width, Nlm_Int2 height,
4827                                       Nlm_WndActnProc close)
4828 
4829 {
4830   Nlm_RecT    r;
4831   Nlm_WindoW  w;
4832 
4833   Nlm_LoadRect (&r, left, top, width, height);
4834   w = Nlm_MakeWindowLink (&r, sizeof (Nlm_WindowRec), floatingProcs);
4835   if (w != NULL) {
4836 #ifdef WIN_MAC
4837     Nlm_NewWindow (w, FLOATING_STYLE, 17, TRUE, NULL, 0, NULL, close, NULL);
4838 #endif
4839 #ifdef WIN_MSWIN
4840     Nlm_NewWindow (w, FLOATING_STYLE, 0, FALSE, windowclass, WS_POPUP |
4841                    WS_CAPTION | WS_SYSMENU, "", close, NULL);
4842 #endif
4843 #ifdef WIN_MOTIF
4844     Nlm_NewWindow (w, FLOATING_STYLE, 0, FALSE, NULL, 0, "", close, NULL);
4845 #endif
4846   }
4847   return w;
4848 }
4849 
Nlm_ShadowWindow(Nlm_Int2 left,Nlm_Int2 top,Nlm_Int2 width,Nlm_Int2 height,Nlm_WndActnProc close)4850 extern Nlm_WindoW Nlm_ShadowWindow (Nlm_Int2 left, Nlm_Int2 top,
4851                                     Nlm_Int2 width, Nlm_Int2 height,
4852                                     Nlm_WndActnProc close)
4853 
4854 {
4855   Nlm_RecT    r;
4856   Nlm_WindoW  w;
4857 
4858   Nlm_LoadRect (&r, left, top, width, height);
4859   w = Nlm_MakeWindowLink (&r, sizeof (Nlm_WindowRec), shadowProcs);
4860   if (w != NULL) {
4861 #ifdef WIN_MAC
4862     Nlm_NewWindow (w, SHADOW_STYLE, 3, FALSE, NULL, 0, NULL, close, NULL);
4863 #endif
4864 #ifdef WIN_MSWIN
4865     Nlm_NewWindow (w, SHADOW_STYLE, 0, FALSE, windowclass,
4866       WS_CAPTION|WS_BORDER, "", close, NULL);
4867 #endif
4868 #ifdef WIN_MOTIF
4869     Nlm_NewWindow (w, SHADOW_STYLE, 0, FALSE, NULL, 0, "", close, NULL);
4870 #endif
4871   }
4872   return w;
4873 }
4874 
Nlm_PlainWindow(Nlm_Int2 left,Nlm_Int2 top,Nlm_Int2 width,Nlm_Int2 height,Nlm_WndActnProc close)4875 extern Nlm_WindoW Nlm_PlainWindow (Nlm_Int2 left, Nlm_Int2 top,
4876                                    Nlm_Int2 width, Nlm_Int2 height,
4877                                    Nlm_WndActnProc close)
4878 
4879 {
4880   Nlm_RecT    r;
4881   Nlm_WindoW  w;
4882 
4883   Nlm_LoadRect (&r, left, top, width, height);
4884   w = Nlm_MakeWindowLink (&r, sizeof (Nlm_WindowRec), plainProcs);
4885   if (w != NULL) {
4886 #ifdef WIN_MAC
4887     Nlm_NewWindow (w, PLAIN_STYLE, 2, FALSE, NULL, 0, NULL, close, NULL);
4888 #endif
4889 #ifdef WIN_MSWIN
4890     Nlm_NewWindow (w, PLAIN_STYLE, 0, FALSE, windowclass, WS_OVERLAPPED, "", close, NULL);
4891 #endif
4892 #ifdef WIN_MOTIF
4893     Nlm_NewWindow (w, PLAIN_STYLE, 0, FALSE, NULL, 0, "", close, NULL);
4894 #endif
4895   }
4896   return w;
4897 }
4898 
Nlm_Metronome(Nlm_VoidProc actn)4899 extern void Nlm_Metronome (Nlm_VoidProc actn)
4900 
4901 {
4902   timerAction = actn;
4903 }
4904 
Nlm_SetWindowTimer(Nlm_WindoW w,Nlm_WndActnProc actn)4905 extern void Nlm_SetWindowTimer (Nlm_WindoW w, Nlm_WndActnProc actn)
4906 
4907 {
4908   Nlm_WindowData  wdata;
4909 
4910   if (w != NULL) {
4911     Nlm_GetWindowData (w, &wdata);
4912     wdata.timer = actn;
4913     Nlm_SetWindowData (w, &wdata);
4914   }
4915 }
4916 
Nlm_CallWindowTimers(void)4917 static void Nlm_CallWindowTimers (void)
4918 
4919 {
4920   Nlm_WindoW      w;
4921   Nlm_WindowData  wdata;
4922 
4923   w = (Nlm_WindoW) Nlm_GetNext ((Nlm_GraphiC) Nlm_desktopWindow);
4924   while (w != NULL && w != Nlm_systemWindow) {
4925     Nlm_GetWindowData (w, &wdata);
4926     if (wdata.timer != NULL) {
4927       wdata.timer (w);
4928     }
4929     w = (Nlm_WindoW) Nlm_GetNext ((Nlm_GraphiC) w);
4930   }
4931 }
4932 
4933 
4934 #ifdef WIN_MOTIF
Nlm_SetCursor(Cursor cursor)4935 static void Nlm_SetCursor (Cursor cursor)
4936 
4937 {
4938   Nlm_MainTool  man;
4939   Nlm_WindoW    w;
4940   Window        xtw;
4941 
4942   if (Nlm_currentXDisplay != NULL) {
4943     w = (Nlm_WindoW) Nlm_GetNext ((Nlm_GraphiC) Nlm_desktopWindow);
4944     while (w != NULL) {
4945       if (! Nlm_IsWindowDying (w)) {
4946         man = Nlm_GetWindowMain (w);
4947         xtw = XtWindow (man);
4948         if ( xtw ) {
4949           XDefineCursor (Nlm_currentXDisplay, xtw, cursor);
4950         }
4951       }
4952       w = (Nlm_WindoW) Nlm_GetNext ((Nlm_GraphiC) w);
4953     }
4954     Nlm_currentCursor = cursor;
4955     XFlush( Nlm_currentXDisplay );
4956   }
4957 }
4958 #endif
4959 
4960 #ifdef WIN_MAC_QUARTZ
4961 #ifdef WIN_MAC_QUARTZ_COCOA_CURSORS
4962 /* these support functions make life a bit easier and centralize the
4963    ugly Cocoa access */
4964 
4965 /* objc_msgSend is actually a trampoline which is effectively has no real prototype
4966    but actually just takes on the prototype of the target method;
4967    in order to shut the compiler up about methods which return char and generate
4968    the correct machine code at the call site, we define this global function pointer
4969    which points to the same function but with a different prototype */
4970 static char (*objc_msgSend_charRetFptr)(void *, void *, ...) = (void *)objc_msgSend;
4971 
4972 /* handy shortcut macros for invoking NSCursor methods */
4973 #define COCOA_SELECTOR(name) NSSelectorFromString(CFSTR(name))
4974 
4975 /* NSCursor does not define all of the methods we use when running
4976    on 10.2, so we check to ensure it responds before calling anything.
4977    If it does not respond, we'll return 0 (nil) and the following
4978    set operation will be a no-op. */
4979 #define COCOA_NSCURSOR_CLASS_RESPONDS(name) objc_msgSend_charRetFptr(Nlm_NSCursorClass(), COCOA_SELECTOR("respondsToSelector:"), COCOA_SELECTOR(name))
4980 
4981 #define COCOA_NAMED_CURSOR(name) (COCOA_NSCURSOR_CLASS_RESPONDS(name "Cursor") \
4982                                   ? objc_msgSend(Nlm_NSCursorClass(), COCOA_SELECTOR(name "Cursor")) \
4983                                   : 0)
4984 
Nlm_NSCursorClass(void)4985 static void *Nlm_NSCursorClass (void)
4986 {
4987   static int didLoadApplication = 0;
4988   if (!didLoadApplication)
4989   {
4990     didLoadApplication = 1;
4991     NSApplicationLoad();
4992   }
4993 
4994   return NSClassFromString(CFSTR("NSCursor"));
4995 }
4996 
Nlm_SetCursor(void * cursor)4997 static void Nlm_SetCursor (void *cursor)
4998 {
4999   objc_msgSend(cursor, NSSelectorFromString(CFSTR("set")));
5000 }
5001 
5002 #endif
5003 #endif
5004 
Nlm_ArrowCursor(void)5005 extern void Nlm_ArrowCursor (void)
5006 
5007 {
5008 #ifdef WIN_MAC
5009 #ifdef WIN_MAC_QUARTZ
5010 #ifdef WIN_MAC_QUARTZ_COCOA_CURSORS
5011   Nlm_SetCursor(COCOA_NAMED_CURSOR("arrow"));
5012 #endif
5013 #else
5014   Cursor cursor;
5015   GetQDGlobalsArrow(&cursor);
5016   SetCursor (&cursor);
5017 #endif
5018 #endif
5019 #ifdef WIN_MSWIN
5020   Nlm_currentCursor = LoadCursor (NULL, IDC_ARROW);
5021   SetCursor (Nlm_currentCursor);
5022 #endif
5023 #ifdef WIN_MOTIF
5024   Nlm_SetCursor (arrow);
5025 #endif
5026 }
5027 
Nlm_CrossCursor(void)5028 extern void Nlm_CrossCursor (void)
5029 
5030 {
5031 #ifdef WIN_MAC
5032 #ifdef WIN_MAC_QUARTZ
5033 #ifdef WIN_MAC_QUARTZ_COCOA_CURSORS
5034   Nlm_SetCursor(COCOA_NAMED_CURSOR("crosshair"));
5035 #endif
5036 #else
5037   SetCursor (&cross);
5038 #endif
5039 #endif
5040 #ifdef WIN_MSWIN
5041   Nlm_currentCursor = LoadCursor (NULL, IDC_CROSS);
5042   SetCursor (Nlm_currentCursor);
5043 #endif
5044 #ifdef WIN_MOTIF
5045   Nlm_SetCursor (cross);
5046 #endif
5047 }
5048 
Nlm_IBeamCursor(void)5049 extern void Nlm_IBeamCursor (void)
5050 
5051 {
5052 #ifdef WIN_MAC
5053 #ifdef WIN_MAC_QUARTZ
5054 #ifdef WIN_MAC_QUARTZ_COCOA_CURSORS
5055   Nlm_SetCursor(COCOA_NAMED_CURSOR("IBeam"));
5056 #endif
5057 #else
5058   SetCursor (&iBeam);
5059 #endif
5060 #endif
5061 #ifdef WIN_MSWIN
5062   Nlm_currentCursor = LoadCursor (NULL, IDC_IBEAM);
5063   SetCursor (Nlm_currentCursor);
5064 #endif
5065 #ifdef WIN_MOTIF
5066   Nlm_SetCursor (iBeam);
5067 #endif
5068 }
5069 
Nlm_PlusCursor(void)5070 extern void Nlm_PlusCursor (void)
5071 
5072 {
5073 #ifdef WIN_MAC
5074 #ifdef WIN_MAC_QUARTZ
5075 #ifdef WIN_MAC_QUARTZ_COCOA_CURSORS
5076   Nlm_SetCursor(COCOA_NAMED_CURSOR("crosshair"));
5077 #endif
5078 #else
5079   SetCursor (&plus);
5080 #endif
5081 #endif
5082 #ifdef WIN_MSWIN
5083   Nlm_currentCursor = LoadCursor (NULL, IDC_CROSS);
5084   SetCursor (Nlm_currentCursor);
5085 #endif
5086 #ifdef WIN_MOTIF
5087   Nlm_SetCursor (plus);
5088 #endif
5089 }
5090 
Nlm_WatchCursor(void)5091 extern void Nlm_WatchCursor (void)
5092 
5093 {
5094 #ifdef WIN_MAC
5095 #ifdef WIN_MAC_QUARTZ
5096 #ifdef WIN_MAC_QUARTZ_COCOA_CURSORS
5097 // QUARTZ_FIXME: do we even need or want a watch cursor?
5098 #endif
5099 #else
5100   SetCursor (&watch);
5101 #endif
5102 #endif
5103 #ifdef WIN_MSWIN
5104   Nlm_currentCursor = LoadCursor (NULL, IDC_WAIT);
5105   SetCursor (Nlm_currentCursor);
5106 #endif
5107 #ifdef WIN_MOTIF
5108   Nlm_SetCursor (watch);
5109 #endif
5110 }
5111 
5112 
5113 typedef void (*Nlm_SetCursorFuncPtr)( void );
5114 
5115 static enumCursorShape curr_cursor_shape = CURSOR_DEFAULT;
5116 static Nlm_SetCursorFuncPtr Nlm_CursorArray[ N_CURSORS ];
5117 
5118 
Nlm_AddCursorShape(enumCursorShape cursor_shape,Nlm_SetCursorFuncPtr set_function)5119 static Nlm_Boolean Nlm_AddCursorShape(enumCursorShape      cursor_shape,
5120                                       Nlm_SetCursorFuncPtr set_function)
5121 {
5122   if (cursor_shape < 0  ||  N_CURSORS <= cursor_shape  ||
5123       Nlm_CursorArray[cursor_shape] != NULL  ||
5124       set_function == NULL)
5125     return FALSE;
5126 
5127   Nlm_CursorArray[cursor_shape] = set_function;
5128   return TRUE;
5129 }
5130 
5131 
Nlm_InitCursorShapes(void)5132 Nlm_Boolean Nlm_InitCursorShapes( void )
5133 {
5134   int i;
5135   for (i = 0;  i < N_CURSORS;  i++)
5136     Nlm_CursorArray[i] = NULL;
5137 
5138   {{
5139     Nlm_Boolean ok = (Nlm_Boolean)(
5140       Nlm_AddCursorShape (CURSOR_ARROW, Nlm_ArrowCursor)  &&
5141       Nlm_AddCursorShape (CURSOR_CROSS, Nlm_CrossCursor)  &&
5142       Nlm_AddCursorShape (CURSOR_IBEAM, Nlm_IBeamCursor)  &&
5143       Nlm_AddCursorShape (CURSOR_PLUS,  Nlm_PlusCursor )  &&
5144       Nlm_AddCursorShape (CURSOR_WATCH, Nlm_WatchCursor));
5145     ASSERT ( ok );
5146     if ( !ok )  return FALSE;
5147   }}
5148 
5149   for (i = 0;  i < N_CURSORS;  i++)
5150     if (Nlm_CursorArray[i] == NULL)
5151       {
5152         ASSERT ( FALSE );
5153         return FALSE;
5154       }
5155 
5156   return  (Nlm_Boolean)(SetCursorShape( curr_cursor_shape )  !=  BAD_CURSOR);
5157 }
5158 
5159 
Nlm_SetCursorShape(enumCursorShape cursor_shape)5160 enumCursorShape Nlm_SetCursorShape(enumCursorShape cursor_shape)
5161 {
5162   if (cursor_shape == WHAT_CURSOR)
5163     return curr_cursor_shape;
5164 
5165   if (0 <= cursor_shape  &&  cursor_shape < N_CURSORS  &&
5166       Nlm_CursorArray[cursor_shape] != NULL)
5167     {
5168       enumCursorShape prev_cursor_shape = curr_cursor_shape;
5169       (*Nlm_CursorArray[curr_cursor_shape = cursor_shape])();
5170       return prev_cursor_shape;
5171     }
5172   else
5173     {
5174       ASSERT ( FALSE );
5175       return BAD_CURSOR;
5176     }
5177 }
5178 
5179 
5180 extern void LIBCALL
Nlm_RegisterDropProc(Nlm_DropProc dropProc)5181 Nlm_RegisterDropProc (Nlm_DropProc dropProc)
5182 {
5183   registeredDropProc = dropProc;
5184 }
5185 
5186 extern void LIBCALL
Nlm_RegisterServiceProc(Nlm_ServiceProc serviceProc)5187 Nlm_RegisterServiceProc (Nlm_ServiceProc serviceProc)
5188 {
5189   registeredServiceProc = serviceProc;
5190 }
5191 
5192 extern void LIBCALL
Nlm_RegisterResultProc(Nlm_ResultProc resultProc)5193 Nlm_RegisterResultProc (Nlm_ResultProc resultProc)
5194 {
5195   registeredResultProc = resultProc;
5196 }
5197 
5198 #ifdef WIN_MAC
HandleAEQuitApp(const AppleEvent * event,AppleEvent * reply,AERefCon ref)5199 static pascal OSErr HandleAEQuitApp (const AppleEvent *event, AppleEvent *reply, AERefCon ref)
5200 
5201 {
5202   Nlm_QuitProgram ();
5203   return noErr;
5204 }
5205 
HandleAEIgnore(const AppleEvent * event,AppleEvent * reply,AERefCon ref)5206 static pascal OSErr HandleAEIgnore (const AppleEvent *event, AppleEvent *reply, AERefCon ref)
5207 
5208 {
5209   return noErr;
5210 }
5211 
5212 #if 0
5213 /* AppleEvent handlers modified from Roger Sayle's RasMol code */
5214 static void ConvertFilename ( FSSpec *fss, Nlm_CharPtr filename )
5215 
5216 {
5217   register char *src;
5218   register char *dst;
5219   register int i;
5220   char buffer [256];
5221 
5222   Str255 dirname;
5223   DirInfo dinfo;
5224 
5225   src = buffer;
5226   dinfo.ioDrParID = fss->parID;
5227   dinfo.ioNamePtr = dirname;
5228   do {
5229     dinfo.ioVRefNum = fss->vRefNum;
5230     dinfo.ioFDirIndex = -1;
5231     dinfo.ioDrDirID = dinfo.ioDrParID;
5232     PBGetCatInfo ((CInfoPBPtr) &dinfo, 0);
5233 
5234     *src++ = ':';
5235     for ( i=dirname[0]; i; i-- )
5236       *src++ = dirname [i];
5237   } while ( dinfo.ioDrDirID != 2 );
5238 
5239   /* Reverse the file path! */
5240   dst = filename;
5241   while ( src != buffer )
5242     *dst++ = *(--src);
5243   for( i = 1; i <= fss->name [0]; i++ )
5244     *dst++ = fss->name [i];
5245   *dst = '\0';
5246 }
5247 #endif
5248 
HandleAEOpenDoc(const AppleEvent * event,AppleEvent * reply,AERefCon ref)5249 static pascal OSErr HandleAEOpenDoc (const AppleEvent *event, AppleEvent *reply, AERefCon ref)
5250 
5251 {
5252   register OSErr stat;
5253   register long i;
5254   AEDescList list;
5255   AEKeyword keywd;
5256   DescType dtype;
5257   FSSpec fss;
5258   long count;
5259   Size size;
5260   char filename [256];
5261   char tempfile [PATH_MAX];
5262   AEDesc docDesc;
5263   AEDescList theList;
5264   CFURLRef cfurl;
5265   OSErr theErr;
5266 
5267   stat = AEGetParamDesc (event, keyDirectObject, typeAEList, &list);
5268   if ( stat ) return ( stat );
5269 
5270   stat = AEGetAttributePtr (event, keyMissedKeywordAttr, typeWildCard,
5271                             &dtype, 0, 0, &size );
5272   if ( stat != errAEDescNotFound ) {
5273     AEDisposeDesc( &list );
5274     return ( stat? stat : errAEEventNotHandled );
5275   }
5276 
5277   AECountItems ( &list, &count );
5278   for ( i = 1; i <= count; i++ ) {
5279     stat = AEGetNthPtr (&list, i, typeFSS, &keywd,
5280                         &dtype, (Ptr) &fss, sizeof (fss),
5281                         &size);
5282     if ( !stat ) {
5283       Nlm_ConvertFilename (&fss, filename);
5284 
5285       if (registeredDropProc != NULL) {
5286         registeredDropProc (filename);
5287       }
5288 
5289       if (registeredServiceProc != NULL) {
5290         Nlm_TmpNam (tempfile);
5291         registeredServiceProc (filename, tempfile);
5292 
5293         theErr = AECreateList(NULL, 0, FALSE, &theList);
5294         if (theErr == noErr) {
5295           cfurl = CFURLCreateFromFileSystemRepresentation (NULL, (const UInt8 *)tempfile, strlen (tempfile), 0);
5296           if (cfurl) {
5297             CFDataRef cfdata = CFURLCreateData (NULL, cfurl, kCFStringEncodingUTF8, 1);
5298             theErr = AECreateDesc(typeFileURL, CFDataGetBytePtr (cfdata), CFDataGetLength (cfdata), &docDesc);
5299             CFRelease (cfdata);
5300             if (theErr == noErr) {
5301               theErr = AEPutDesc(&theList, 0, &docDesc);
5302               if (theErr == noErr) {
5303                 theErr = AEPutParamDesc(reply, keyDirectObject, &theList);
5304               }
5305             }
5306           }
5307         }
5308 
5309       }
5310     }
5311   }
5312   AEDisposeDesc ( &list );
5313   return noErr;
5314 }
5315 
HandleAEAnswer(const AppleEvent * event,AppleEvent * reply,AERefCon ref)5316 static pascal OSErr HandleAEAnswer (const AppleEvent *event, AppleEvent *reply, AERefCon ref)
5317 
5318 {
5319   register OSErr stat;
5320   register long i;
5321   AEDescList list;
5322   AEKeyword keywd;
5323   DescType dtype;
5324   FSSpec fss;
5325   long count;
5326   Size size;
5327   char filename [256];
5328 
5329   stat = AEGetParamDesc (event, keyDirectObject, typeAEList, &list);
5330   if ( stat ) return ( stat );
5331 
5332   stat = AEGetAttributePtr (event, keyMissedKeywordAttr, typeWildCard,
5333                             &dtype, 0, 0, &size );
5334   if ( stat != errAEDescNotFound ) {
5335     AEDisposeDesc( &list );
5336     return ( stat? stat : errAEEventNotHandled );
5337   }
5338 
5339   AECountItems ( &list, &count );
5340   for ( i = 1; i <= count; i++ ) {
5341     stat = AEGetNthPtr (&list, i, typeFSS, &keywd,
5342                         &dtype, (Ptr) &fss, sizeof (fss),
5343                         &size);
5344     if ( !stat ) {
5345       Nlm_ConvertFilename (&fss, filename);
5346       if (registeredResultProc != NULL) {
5347         registeredResultProc (filename);
5348         Nlm_FileRemove (filename); /* ? */
5349       } else if (registeredDropProc != NULL) {
5350         registeredDropProc (filename);
5351       }
5352     }
5353   }
5354   AEDisposeDesc ( &list );
5355   return noErr;
5356 }
5357 
5358 
Nlm_HandleEvent(void)5359 static void Nlm_HandleEvent (void)
5360 
5361 {
5362   OSErr           err;
5363   Nlm_Int2        key;
5364   Nlm_Uint4       mess;
5365   Nlm_PointTool   ptool;
5366   Nlm_Int2        windowLoc;
5367   Nlm_WindowTool  wptr;
5368 
5369   Nlm_PointToolToPoinT (Nlm_currentEvent.where, &Nlm_globalMouse);
5370   Nlm_localMouse = Nlm_globalMouse;
5371   Nlm_GlobalToLocal (&Nlm_localMouse);
5372   Nlm_currentKey = '\0';
5373   key = Nlm_currentEvent.modifiers;
5374   Nlm_cmmdKey = ((key & 256) != 0);
5375   Nlm_ctrlKey = ((key & 4096) != 0);
5376   Nlm_shftKey = ((key & 512) != 0);
5377   Nlm_optKey = ((key & 2048) != 0);
5378   Nlm_dblClick = FALSE;
5379   frontWindow = Nlm_FindWindowRec (FrontWindow ());
5380   Nlm_theWindow = Nlm_FindWindowRec ((Nlm_WindowTool) Nlm_currentEvent.message);
5381   chosenWindow = Nlm_theWindow;
5382   switch (Nlm_currentEvent.what) {
5383     case mouseDown:
5384       Nlm_PoinTToPointTool (Nlm_globalMouse, &ptool);
5385       windowLoc = FindWindow (ptool, &wptr);
5386       chosenWindow = Nlm_FindWindowRec (wptr);
5387       Nlm_DoClick ((Nlm_GraphiC) frontWindow, Nlm_globalMouse);
5388       break;
5389     case keyDown:
5390       Nlm_currentKey = (Nlm_Char) (Nlm_currentEvent.message % 256);
5391       if (keyAction != NULL) {
5392         keyAction (Nlm_currentKey);
5393       }
5394       Nlm_DoKey ((Nlm_GraphiC) frontWindow, Nlm_currentKey);
5395       break;
5396     case autoKey:
5397       if (! Nlm_cmmdKey) {
5398         Nlm_currentKey = (Nlm_Char) (Nlm_currentEvent.message % 256);
5399         Nlm_DoKey ((Nlm_GraphiC) frontWindow, Nlm_currentKey);
5400       }
5401       break;
5402     case updateEvt:
5403 #ifndef WIN_MAC_QUARTZ
5404 /* this gets handled by the HIView infrastructure behind our backs in Quartz */
5405       Nlm_DoDraw ((Nlm_GraphiC) Nlm_theWindow);
5406 #endif
5407       break;
5408     case osEvt:
5409       mess = (Nlm_currentEvent.message & osEvtMessageMask) >> 24;
5410       if (mess == suspendResumeMessage) {
5411         if (Nlm_currentEvent.message & resumeFlag) {
5412           /* Resume */
5413 #if !TARGET_API_MAC_CARBON
5414           if (Nlm_currentEvent.message & convertClipboardFlag) {
5415             /* 2001-05-14:  JDJ
5416              * We always convert the clipboard.
5417              * If it turns out that pasting is noticeably slow, I'll change it.
5418              */
5419           }
5420 #endif
5421         } else {
5422           /* Suspend
5423            */
5424         }
5425       } else if (mess == mouseMovedMessage) {
5426         if (mouseMovedAction != NULL) {
5427           Nlm_PoinTToPointTool (Nlm_globalMouse, &ptool);
5428           windowLoc = FindWindow (ptool, &wptr);
5429           chosenWindow = Nlm_FindWindowRec (wptr);
5430           mouseMovedAction ((Nlm_GraphiC) frontWindow, Nlm_globalMouse);
5431         }
5432       }
5433       break;
5434     case activateEvt:
5435       if (Nlm_currentEvent.modifiers & 01) {
5436         Nlm_DoActivate ((Nlm_GraphiC) Nlm_theWindow, FALSE);
5437       } else {
5438         Nlm_DoDeactivate ((Nlm_GraphiC) Nlm_theWindow, FALSE);
5439       }
5440       break;
5441 /* 2001-03-22:  Joshua Juran
5442  * Carbon doesn't support DIBadMount() and will not send diskEvt in the first place.
5443  */
5444 #if !TARGET_API_MAC_CARBON
5445     case diskEvt:
5446       if (HiWord (Nlm_currentEvent.message) != 0) {
5447         Nlm_PoinT where;
5448         where.x = 90;
5449         where.y = 100;
5450         Nlm_PoinTToPointTool (where, &ptool);
5451         DIBadMount (ptool, Nlm_currentEvent.message);
5452       }
5453       break;
5454 #endif
5455     case kHighLevelEvent:
5456       err = AEProcessAppleEvent (&Nlm_currentEvent);
5457       break;
5458     default:
5459       break;
5460   }
5461 }
5462 
Nlm_RegisterWindows(void)5463 extern Nlm_Boolean Nlm_RegisterWindows (void)
5464 
5465 {
5466   return TRUE;
5467 }
5468 
5469 #ifndef WIN_MAC_QUARTZ
Nlm_ReturnCursor(Cursor * cursor,Nlm_Int2 cursorID)5470 static void Nlm_ReturnCursor (Cursor *cursor, Nlm_Int2 cursorID)
5471 
5472 {
5473   CursHandle  hCurs;
5474   CursPtr     pCurs;
5475 
5476   hCurs = GetCursor (cursorID);
5477   if (hCurs != NULL) {
5478     HLock ((Handle) hCurs);
5479     pCurs = (CursPtr) *((Handle) hCurs);
5480     *cursor = *pCurs;
5481     HUnlock ((Handle) hCurs);
5482   } else {
5483     GetQDGlobalsArrow(cursor);
5484   }
5485 }
5486 #endif
5487 
Nlm_SetupWindows(void)5488 static Nlm_Boolean Nlm_SetupWindows (void)
5489 
5490 {
5491   Nlm_PoinT  pt;
5492   Nlm_RecT   r;
5493   long  gval;
5494 #ifdef WIN_MAC_QUARTZ
5495   CGDirectDisplayID display;
5496   CGDisplayCount displayCount;
5497 #else
5498   BitMap myScreenBits;
5499 
5500   Nlm_ReturnCursor (&cross, 2);
5501   Nlm_ReturnCursor (&iBeam, 1);
5502   Nlm_ReturnCursor (&plus, 3);
5503   Nlm_ReturnCursor (&watch, 4);
5504 #endif
5505   Nlm_WatchCursor ();
5506   Nlm_ClearKeys ();
5507   Nlm_LoadPt (&pt, 0, 0);
5508 #ifdef WIN_MAC_QUARTZ
5509   CGGetActiveDisplayList (1, &display, &displayCount);
5510   screenBitBounds = Nlm_CGRectToRecT (CGDisplayBounds (display));
5511 #else
5512   GetQDGlobalsScreenBits(&myScreenBits);
5513   Nlm_RectToolToRecT (&(myScreenBits.bounds), &screenBitBounds);
5514 #endif
5515   r = screenBitBounds;
5516   Nlm_screenRect = screenBitBounds;
5517   Nlm_desktopWindow = (Nlm_WindoW) Nlm_HandNew (sizeof (Nlm_WindowRec));
5518   Nlm_systemWindow = (Nlm_WindoW) Nlm_HandNew (sizeof (Nlm_WindowRec));
5519   Nlm_LoadGraphicData ((Nlm_GraphiC) Nlm_desktopWindow, (Nlm_GraphiC) Nlm_systemWindow,
5520                        NULL, NULL, NULL, desktopProcs, NULL, &r, TRUE, TRUE, NULL, NULL);
5521   Nlm_LoadBoxData ((Nlm_BoX) Nlm_desktopWindow, pt, pt, pt, 0, 0, 0, 0, 0, 0, 0, 0);
5522   Nlm_LoadWindowData (Nlm_desktopWindow, NULL, NULL, NULL, NULL, &r, &r, &r, NULL, NULL,
5523                       NULL, NULL, NULL, NULL, NULL, TRUE, FALSE, NULL, NULL, NULL, NULL);
5524   Nlm_LoadRect (&r, 0, 0, 0, 0);
5525   Nlm_LoadGraphicData ((Nlm_GraphiC) Nlm_systemWindow, NULL, NULL,
5526                        NULL, NULL, systemProcs, NULL, &r, TRUE, TRUE, NULL, NULL);
5527   Nlm_LoadBoxData ((Nlm_BoX) Nlm_systemWindow, pt, pt, pt, 0, 0, 0, 0, 0, 0, 0, 0);
5528   Nlm_LoadWindowData (Nlm_systemWindow, NULL, NULL, NULL, NULL, &r, &r, &r, NULL, NULL,
5529                       NULL, NULL, NULL, NULL, NULL, TRUE, FALSE, NULL, NULL, NULL, NULL);
5530   dyingWindow = NULL;
5531   frontWindow = Nlm_desktopWindow;
5532   theActiveWindow = Nlm_desktopWindow;
5533   chosenWindow = Nlm_desktopWindow;
5534   Nlm_theWindow = Nlm_desktopWindow;
5535   Nlm_currentWindowTool = (Nlm_WindowTool) 0;
5536   quitProgram = FALSE;
5537 
5538   Nlm_SetUpDrawingTools ();
5539 
5540   Nlm_hScrollBarHeight = 16;
5541   Nlm_vScrollBarWidth = 16;
5542 
5543   Nlm_popupMenuHeight = Nlm_stdLineHeight + 4;
5544   Nlm_dialogTextHeight = Nlm_stdLineHeight + 4;
5545 
5546   lastTimerTime = Nlm_ComputerTime ();
5547   timerAction = NULL;
5548   keyAction = NULL;
5549   mouseMovedAction = NULL;
5550   registeredDropProc = NULL;
5551   registeredServiceProc = NULL;
5552   registeredResultProc = NULL;
5553 
5554     /* gestalt for quickdraw features are defined as bits in a bitfield
5555        for example gestaltHasColor = 0, thus we need to test for lsb set
5556      */
5557   if( Gestalt( gestaltQuickdrawFeatures, &gval) == noErr){
5558       hasColorQD = (gval && (1 << gestaltHasColor));
5559   }
5560   return TRUE;
5561 }
5562 #endif
5563 
5564 #ifdef WIN_MSWIN
5565 
5566 /* Message cracker functions */
5567 
MyCls_OnMouseMove(HWND hwnd,int x,int y,UINT keyFlags)5568 static void MyCls_OnMouseMove (HWND hwnd, int x, int y, UINT keyFlags)
5569 
5570 {
5571   SetCursor (Nlm_currentCursor);
5572 }
5573 
MyCls_OnShowWindow(HWND hwnd,BOOL fShow,UINT status)5574 static void MyCls_OnShowWindow(HWND hwnd, BOOL fShow, UINT status)
5575 {
5576   Nlm_SetVisible((Nlm_GraphiC)Nlm_theWindow, (Nlm_Boolean)
5577                  (fShow ? (status != SW_PARENTCLOSING) : FALSE));
5578 }
5579 
MyCls_OnCommand(HWND hwnd,int id,HWND hwndCtl,UINT codeNotify)5580 static void MyCls_OnCommand (HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
5581 
5582 {
5583   Nlm_GraphiC  g;
5584   Nlm_MenuBaR  mb;
5585 
5586   Nlm_currentId = id;
5587   Nlm_currentHwndCtl = hwndCtl;
5588   Nlm_currentCode = codeNotify;
5589 
5590   if (hwndCtl == NULL) {
5591     mb = Nlm_GetWindowMenuBar (Nlm_theWindow);
5592     Nlm_DoCommand ((Nlm_GraphiC) mb);
5593   } else {
5594     g = (Nlm_GraphiC) GetProp (hwndCtl, (LPSTR) "Nlm_VibrantProp");
5595     (void) (Nlm_GetEnabled (g) &&
5596             Nlm_GetVisible (g) &&
5597             Nlm_DoCommand (g));
5598   }
5599 }
5600 
MyCls_OnHVScroll(HWND hwnd,HWND hwndCtl,UINT code,int pos)5601 static void MyCls_OnHVScroll (HWND hwnd, HWND hwndCtl, UINT code, int pos)
5602 
5603 {
5604   Nlm_GraphiC  g;
5605 
5606   Nlm_currentHwndCtl = hwndCtl;
5607   Nlm_currentCode = code;
5608   Nlm_currentPos = pos;
5609 
5610   if (hwndCtl != NULL) {
5611     g = (Nlm_GraphiC) GetProp (hwndCtl, (LPSTR) "Nlm_VibrantProp");
5612     (void) (Nlm_GetEnabled (g) &&
5613             Nlm_GetVisible (g) &&
5614             Nlm_DoCommand (g));
5615   }
5616 }
5617 
MyCls_OnClose(HWND hwnd)5618 static void MyCls_OnClose (HWND hwnd)
5619 
5620 {
5621   Nlm_WndActnProc  cls;
5622   Nlm_WindowData   wdata;
5623 
5624   Nlm_GetWindowData (Nlm_theWindow, &wdata);
5625   cls = wdata.close;
5626   if (cls != NULL) {
5627     cls (Nlm_theWindow);
5628   }
5629 }
5630 
MyCls_OnActivate(HWND hwnd,UINT state,HWND hwndActDeact,BOOL fMinimized)5631 static void MyCls_OnActivate(HWND hwnd, UINT state, HWND hwndActDeact, BOOL fMinimized)
5632 {
5633   switch ( state )
5634     {
5635     case WA_ACTIVE:
5636     case WA_CLICKACTIVE:
5637       {
5638         Nlm_WindowData  wdata;
5639         Nlm_GetWindowData(Nlm_theWindow, &wdata);
5640         if ( wdata.cMap ) {
5641           SelectPalette(Nlm_currentHDC, wdata.cMap, FALSE);
5642           RealizePalette( Nlm_currentHDC );
5643         }
5644         Nlm_SetVisible((Nlm_GraphiC)Nlm_theWindow,
5645                        (Nlm_Boolean)(!fMinimized));
5646         Nlm_DoActivate((Nlm_GraphiC)Nlm_theWindow, FALSE);
5647         break;
5648       }
5649     case WA_INACTIVE:
5650       {
5651         Nlm_DoDeactivate((Nlm_GraphiC)Nlm_theWindow, FALSE);
5652         break;
5653       }
5654     }
5655 }
5656 
5657 
OnSizeMove(HWND hwnd,Nlm_Boolean resized)5658 static void OnSizeMove(HWND hwnd, Nlm_Boolean resized)
5659 {
5660   Nlm_RectTool rtool;
5661 
5662   if (!Nlm_WindowHasBeenShown( Nlm_theWindow )  ||
5663       !Nlm_GetVisible( (Nlm_GraphiC)Nlm_theWindow ))
5664     return;
5665 
5666   {{
5667   Nlm_RecT r;
5668   GetWindowRect(hwnd, &rtool);
5669   win2client(Nlm_theWindow, &rtool, &r);
5670   Nlm_SetRect((Nlm_GraphiC)Nlm_theWindow, &r);
5671   }}
5672 
5673   if ( resized )
5674     {
5675       Nlm_WindowData wdata;
5676       Nlm_GetWindowData(Nlm_theWindow, &wdata);
5677       if (wdata.resize == NULL)
5678         return;
5679       (*wdata.resize)( Nlm_theWindow );
5680       InvalidateRect(hwnd, &rtool, FALSE);
5681     }
5682 }
5683 
MyCls_OnSize(HWND hwnd,UINT state,int cx,int cy)5684 static void MyCls_OnSize(HWND hwnd, UINT state, int cx, int cy)
5685 {
5686   if ( !Nlm_GetVisible( (Nlm_GraphiC)Nlm_theWindow ) )
5687     return;
5688 
5689   Nlm_SetVisible((Nlm_GraphiC)Nlm_theWindow,
5690                  (Nlm_Boolean)(state != SIZE_MINIMIZED));
5691   OnSizeMove(hwnd, TRUE);
5692 }
5693 
MyCls_OnMove(HWND hwnd,int x,int y)5694 static void MyCls_OnMove(HWND hwnd, int x, int y)
5695 {
5696   OnSizeMove(hwnd, FALSE);
5697 }
5698 
5699 
MyCls_OnChar(HWND hwnd,UINT ch,int cRepeat)5700 static void MyCls_OnChar (HWND hwnd, UINT ch, int cRepeat)
5701 
5702 {
5703   handlechar = FALSE;
5704   if (ch == '\t') {
5705     Nlm_DoSendFocus ((Nlm_GraphiC) Nlm_theWindow, (Nlm_Char) ch);
5706   } else if (ch == '\n' || ch == '\r') {
5707     Nlm_DoSendFocus ((Nlm_GraphiC) Nlm_theWindow, (Nlm_Char) ch);
5708   } else {
5709     handlechar = TRUE;
5710   }
5711 }
5712 
5713 /*
5714 *  Note that the WM_SIZE message will trigger the resize callback whenever
5715 *  a visible window is resized, including the initial sizing when a window
5716 *  is first displayed.
5717 */
5718 
MainProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)5719 static LRESULT CALLBACK EXPORT MainProc (HWND hwnd, UINT message,
5720                                   WPARAM wParam, LPARAM lParam)
5721 
5722 {
5723   MINMAXINFO FAR*  lpmmi;
5724   HDC              tempHDC;
5725   HWND             tempHWnd;
5726   LRESULT          mainwndrsult = 0;
5727 
5728   if (Nlm_VibrantDisabled ()) {
5729     return DefWindowProc (hwnd, message, wParam, lParam);
5730   }
5731 
5732   tempHWnd = Nlm_currentHWnd;
5733   tempHDC = Nlm_currentHDC;
5734   Nlm_theWindow = (Nlm_WindoW) GetProp (hwnd, (LPSTR) "Nlm_VibrantProp");
5735   Nlm_currentHWnd = hwnd;
5736   Nlm_currentHDC = Nlm_GetWindowPort (Nlm_theWindow);
5737   Nlm_currentWindowTool = hwnd;
5738   Nlm_currentKey = '\0';
5739   Nlm_currentWParam = wParam;
5740   Nlm_currentLParam = lParam;
5741   Nlm_cmmdKey = FALSE;
5742   Nlm_optKey = FALSE;
5743   Nlm_ctrlKey = (Nlm_Boolean) ((GetKeyState (VK_CONTROL) & 0x8000) != 0);
5744   Nlm_shftKey = (Nlm_Boolean) ((GetKeyState (VK_SHIFT)   & 0x8000) != 0);
5745   Nlm_dblClick = FALSE;
5746 
5747   switch ( message )
5748     {
5749     case WM_MOUSEMOVE:
5750       HANDLE_WM_MOUSEMOVE (hwnd, wParam, lParam, MyCls_OnMouseMove);
5751       break;
5752     case WM_SHOWWINDOW:
5753       HANDLE_WM_SHOWWINDOW (hwnd, wParam, lParam, MyCls_OnShowWindow);
5754       break;
5755     case WM_COMMAND:
5756       HANDLE_WM_COMMAND (hwnd, wParam, lParam, MyCls_OnCommand);
5757       break;
5758     case WM_HSCROLL:
5759       HANDLE_WM_HSCROLL (hwnd, wParam, lParam, MyCls_OnHVScroll);
5760       break;
5761     case WM_VSCROLL:
5762       HANDLE_WM_VSCROLL (hwnd, wParam, lParam, MyCls_OnHVScroll);
5763       break;
5764     case WM_CLOSE:
5765       HANDLE_WM_CLOSE (hwnd, wParam, lParam, MyCls_OnClose);
5766       break;
5767     case WM_ACTIVATE:
5768       mainwndrsult = DefWindowProc (hwnd, message, wParam, lParam);
5769       HANDLE_WM_ACTIVATE (hwnd, wParam, lParam, MyCls_OnActivate);
5770       break;
5771     case WM_SIZE:
5772       HANDLE_WM_SIZE (hwnd, wParam, lParam, MyCls_OnSize);
5773       break;
5774     case WM_MOVE:
5775       HANDLE_WM_MOVE (hwnd, wParam, lParam, MyCls_OnMove);
5776       break;
5777     case WM_GETMINMAXINFO:
5778       lpmmi = (MINMAXINFO FAR*) lParam;
5779       lpmmi->ptMaxTrackSize.x = 30000;
5780       lpmmi->ptMaxTrackSize.y = 30000;
5781       break;
5782     case WM_DESTROY:
5783       break;
5784 
5785 #ifdef WIN32
5786     case WM_CTLCOLORSTATIC:
5787       {
5788               HDC hdc = GetDC( hwnd );
5789               SetBkMode((HDC)wParam, TRANSPARENT);
5790               ReleaseDC(hwnd, hdc);
5791               mainwndrsult = GetClassLongPtr(hwnd, GCLP_HBRBACKGROUND);
5792               break;
5793       }
5794     case WM_CTLCOLORLISTBOX:
5795     case WM_CTLCOLOREDIT:
5796       {
5797           if(Nlm_hasBackColor)
5798           {
5799         SetBkColor((HDC)wParam, Nlm_crBackColor);
5800         mainwndrsult = (long)Nlm_hbrWindowBackground;
5801           }
5802           else
5803           {
5804         SetBkColor((HDC)wParam, RGB(255, 255, 255));
5805         mainwndrsult = (long)GetStockObject( WHITE_BRUSH );
5806           }
5807               break;
5808       }
5809     case WM_CTLCOLORDLG:
5810     case WM_CTLCOLORBTN:
5811       {
5812           if(Nlm_hasBackColor)
5813           {
5814         SetBkColor((HDC)wParam, Nlm_crBackColor);
5815         mainwndrsult = (long)Nlm_hbrWindowBackground;
5816           }
5817           else
5818         mainwndrsult = DefWindowProc (hwnd, message, wParam, lParam);
5819               break;
5820       }
5821 #else
5822     case WM_CTLCOLOR:
5823       switch ( lParam )
5824             {
5825             case CTLCOLOR_STATIC:
5826               {
5827                 HDC hdc = GetDC( hwnd );
5828                 SetBkMode((HDC)wParam, TRANSPARENT);
5829                 ReleaseDC(hwnd, hdc);
5830                 mainwndrsult = GetClassLongPtr(hwnd, GCLP_HBRBACKGROUND);
5831                 break;
5832               }
5833             case CTLCOLOR_LISTBOX:
5834             case CTLCOLOR_EDIT:
5835               {
5836                 SetBkColor((HDC)wParam, RGB(255, 255, 255));
5837                 mainwndrsult = (long)(int)GetStockObject( WHITE_BRUSH );
5838                 break;
5839               }
5840             default:
5841               mainwndrsult = DefWindowProc(hwnd, message, wParam, lParam);
5842             }
5843       break;
5844 #endif
5845 
5846     case WM_KEYDOWN:
5847       if ( !Nlm_ProcessKeydown((Nlm_GraphiC)Nlm_theWindow, wParam,
5848                                 VERT_PAGE|VERT_ARROW|HORIZ_PAGE|HORIZ_ARROW) )
5849         mainwndrsult = DefWindowProc(hwnd, message, wParam, lParam);
5850       break;
5851     case WM_CHAR:
5852       HANDLE_WM_CHAR (hwnd, wParam, lParam, MyCls_OnChar);
5853       if (handlechar) {
5854         mainwndrsult = DefWindowProc (hwnd, message, wParam, lParam);
5855       }
5856       break;
5857     case WM_DROPFILES:
5858       {
5859         Nlm_Char fnamebuf[PATH_MAX];
5860 
5861         DragQueryFile((HDROP)wParam,0,fnamebuf,sizeof(fnamebuf));
5862         if (registeredDropProc != NULL)
5863         {
5864           registeredDropProc(fnamebuf);
5865         }
5866         DragFinish((HDROP)wParam);
5867       }
5868       break;
5869     default:
5870       mainwndrsult = DefWindowProc (hwnd, message, wParam, lParam);
5871       break;
5872   }
5873   Nlm_currentHWnd = tempHWnd;
5874   Nlm_currentHDC = tempHDC;
5875   Nlm_currentWindowTool = tempHWnd;
5876   return mainwndrsult;
5877 }
5878 
MetronomeProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)5879 static void FAR PASCAL EXPORT MetronomeProc (HWND hwnd, UINT message,
5880                                       WPARAM wParam, LPARAM lParam)
5881 
5882 {
5883   if (Nlm_VibrantDisabled ()) {
5884     return;
5885   }
5886   if (message == WM_TIMER) {
5887     if (timerAction != NULL) {
5888       timerAction ();
5889     }
5890     Nlm_CallWindowTimers ();
5891   }
5892 }
5893 
Nlm_RegisterWindows(void)5894 extern Nlm_Boolean Nlm_RegisterWindows (void)
5895 {
5896   WNDCLASS     wc;
5897 
5898   wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
5899   wc.lpfnWndProc = MainProc;
5900   wc.cbClsExtra = 0;
5901   wc.cbWndExtra = 0;
5902   wc.hInstance = Nlm_currentHInst;
5903   wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
5904   wc.hCursor = NULL;
5905   wc.hbrBackground = Nlm_hasBackColor ?
5906                    CreateSolidBrush( Nlm_crBackColor ) :
5907                        CreateSolidBrush( GetSysColor(COLOR_ACTIVEBORDER) );
5908   wc.lpszMenuName = NULL;
5909   sprintf (windowclass, "Nlm_WindowClass%ld", (long) (int) Nlm_currentHInst);
5910   wc.lpszClassName = windowclass;
5911 
5912   return (Nlm_Boolean)(RegisterClass(&wc) != 0);
5913 }
5914 
5915 
5916 #include "vibrgb.h"
5917 
5918 
Nlm_FindRGBName(const Nlm_Char * name)5919 static Nlm_RGBName* Nlm_FindRGBName(const Nlm_Char* name)
5920 
5921 {
5922   Nlm_RGBName* p;
5923   for(p = RGBNames; p->name; ++p) {
5924     if (StringICmp(p->name, name) == 0)
5925       return p;
5926   }
5927 
5928   return NULL;
5929 }
5930 
5931 
Nlm_ParseXArguments(int * argc,char ** argv)5932 static void Nlm_ParseXArguments(int* argc, char** argv)
5933 
5934 {
5935   int i, j;
5936 
5937   if (argc == 0 || argv == NULL)
5938     return;
5939 
5940   for(i = 0, j = 0; i < *argc; ) {
5941     if (StringCmp(argv[i], "-bg") == 0 && argv[i+1] != NULL) {
5942       Nlm_RGBName* p = Nlm_FindRGBName(argv[i+1]);
5943       if (p != NULL) {
5944     /* Set user-defined background color */
5945     Nlm_hasBackColor = TRUE;
5946     Nlm_crBackColor = RGB(p->red, p->green, p->blue);
5947     Nlm_hbrWindowBackground = CreateSolidBrush( Nlm_crBackColor );
5948     i += 2;
5949     continue;
5950       }
5951     }
5952     argv[j++] = argv[i++];
5953   }
5954   *argc = j;
5955 }
5956 
5957 
Nlm_SetupWindows(void)5958 static Nlm_Boolean Nlm_SetupWindows (void)
5959 
5960 {
5961   Nlm_Int2   height;
5962   WNDPROC    lpfnTimerProc;
5963   Nlm_PoinT  pt;
5964   Nlm_RecT   r;
5965   Nlm_Int2   width;
5966 
5967   int        xx_argc = (int)Nlm_GetArgc();
5968   char     **xx_argv =      Nlm_GetArgv();
5969 
5970   /*
5971     Using SM_CXVIRTUALSCREEN to accomodate dual monitors effectively limits target platform
5972     to Windows2000 or later (i.e. Win95/98/ME are NOT supported).
5973     In order to use these identifiers, must define WINVER=0x0500.
5974   */
5975 #if(WINVER >= 0x0500)
5976   int x = (Nlm_Int2)GetSystemMetrics (SM_XVIRTUALSCREEN);
5977   int y = (Nlm_Int2)GetSystemMetrics (SM_YVIRTUALSCREEN);
5978   width  = (Nlm_Int2)GetSystemMetrics (SM_CXVIRTUALSCREEN);
5979   height = (Nlm_Int2)GetSystemMetrics (SM_CYVIRTUALSCREEN);
5980   Nlm_LoadRect (&screenBitBounds, x, y, x+width, y+height);
5981   Nlm_LoadPt (&pt, x, y);
5982 #else
5983   width  = (Nlm_Int2)GetSystemMetrics (SM_CXSCREEN);
5984   height = (Nlm_Int2)GetSystemMetrics (SM_CYSCREEN);
5985   Nlm_LoadRect (&screenBitBounds, 0, 0, width, height);
5986   Nlm_LoadPt (&pt, 0, 0);
5987 #endif
5988   r = screenBitBounds;
5989   Nlm_screenRect = screenBitBounds;
5990   Nlm_desktopWindow = (Nlm_WindoW) Nlm_HandNew (sizeof (Nlm_WindowRec));
5991   Nlm_LoadGraphicData ((Nlm_GraphiC) Nlm_desktopWindow, NULL, NULL,
5992                        NULL, NULL, desktopProcs, NULL, &r, TRUE, TRUE, NULL, NULL);
5993   Nlm_LoadBoxData ((Nlm_BoX) Nlm_desktopWindow, pt, pt, pt, 0, 0, 0, 0, 0, 0, 0, 0);
5994   Nlm_LoadWindowData (Nlm_desktopWindow, NULL, NULL, NULL, NULL, &r, &r, &r, NULL, NULL,
5995                       NULL, NULL, NULL, NULL, NULL, TRUE, FALSE, NULL, NULL, NULL, NULL);
5996   Nlm_systemWindow = NULL;
5997   Nlm_currentHWnd = NULL;
5998   Nlm_currentHDC = NULL;
5999   Nlm_currentWindowTool = (Nlm_WindowTool) 0;
6000   quitProgram = FALSE;
6001 
6002   Nlm_ParseXArguments(&xx_argc, xx_argv);
6003   Nlm_SetupArguments(xx_argc, xx_argv);
6004 
6005   Nlm_SetUpDrawingTools ();
6006 
6007   Nlm_hScrollBarHeight = (Nlm_Int2)GetSystemMetrics (SM_CYHSCROLL);
6008   Nlm_vScrollBarWidth  = (Nlm_Int2)GetSystemMetrics (SM_CXVSCROLL);
6009 
6010   Nlm_popupMenuHeight  = (Nlm_Int2)(Nlm_stdLineHeight + 8);
6011   Nlm_dialogTextHeight = (Nlm_Int2)(Nlm_stdFontHeight * 3 / 2);
6012 
6013   Nlm_currentCursor = LoadCursor (NULL, IDC_WAIT);
6014 
6015   lpfnTimerProc = (WNDPROC) MakeProcInstance ((FARPROC) MetronomeProc, Nlm_currentHInst);
6016   if (lpfnTimerProc != NULL) {
6017     timerID = SetTimer (NULL, 1, 50, (TIMERPROC) lpfnTimerProc);
6018   }
6019   timerAction = NULL;
6020   keyAction = NULL;
6021   mouseMovedAction = NULL;
6022   registeredDropProc = NULL;
6023   registeredServiceProc = NULL;
6024   registeredResultProc = NULL;
6025   return TRUE;
6026 }
6027 #endif  /* WIN_MSWIN */
6028 
6029 
6030 #ifdef WIN_MOTIF
Nlm_WindowTimer(XtPointer client_data,XtIntervalId * id)6031 static void Nlm_WindowTimer (XtPointer client_data, XtIntervalId *id)
6032 
6033 {
6034   if (! quitProgram) {
6035     windowTimer = XtAppAddTimeOut (Nlm_appContext, 50, Nlm_WindowTimer, NULL);
6036   }
6037   if (timerAction != NULL) {
6038     timerAction ();
6039   }
6040   Nlm_CallWindowTimers ();
6041 }
6042 
Nlm_RegisterWindows(void)6043 extern Nlm_Boolean Nlm_RegisterWindows (void)
6044 
6045 {
6046   windowTimer = XtAppAddTimeOut (Nlm_appContext, 50, Nlm_WindowTimer, NULL);
6047   return TRUE;
6048 }
6049 
6050 char * defFallbackRes[] =
6051 {
6052   "Vibrant*background:                      light grey",
6053   "Vibrant*foreground:                      black",
6054   "Vibrant*XmPushButton.background:         grey",
6055   "Vibrant*XmToggleButton.foreground:       blue",
6056   "Vibrant*XmText.background:               white",
6057   "Vibrant*XmTextField.background:          white",
6058   "Vibrant*XmScrolledWindow.background:     white",
6059   "Vibrant*XmList.background:               white",
6060   "Vibrant*XmDrawingArea.background:        white",
6061   "Vibrant*XmOptionButtonGadget.background: grey",
6062 
6063   NULL
6064 };
6065 
6066 
Nlm_XrmGetResource1(const char * _basename,const char * _resource,XrmValuePtr value)6067 static Boolean Nlm_XrmGetResource1(const char *_basename,
6068                                    const char *_resource,
6069                                    XrmValuePtr value)
6070 {
6071   XrmDatabase rdb = XtDatabase( Nlm_currentXDisplay );
6072   XrmString type_str;
6073   Boolean ok;
6074 
6075   size_t basename_len = Nlm_StringLen( _basename );
6076   size_t resource_len = Nlm_StringLen( _resource );
6077   size_t len = basename_len + resource_len + 2;
6078 
6079   char *xx_name  = XtMalloc( len );
6080   char *xx_class = XtMalloc( len );
6081 
6082   Nlm_StrCpy(xx_name, _basename);
6083   xx_name[basename_len] = '.';
6084   Nlm_StrCpy(xx_name + basename_len + 1, _resource);
6085 
6086   Nlm_StrCpy(xx_class, xx_name);
6087   xx_class[0]                = TO_UPPER( _basename[0] );
6088   xx_class[basename_len + 1] = TO_UPPER( _resource[0] );
6089 
6090   ok = XrmGetResource(rdb, xx_name, xx_class, &type_str, value);
6091 
6092   XtFree( xx_class );
6093   XtFree( xx_name  );
6094 
6095   return ok;
6096 }
6097 
6098 
Nlm_XrmGetResource2(const char * _basename,const char * _resource,XrmValuePtr value)6099 static Boolean Nlm_XrmGetResource2(const char *_basename,
6100                                    const char *_resource,
6101                                    XrmValuePtr value)
6102 {
6103   ASSERT ( _basename  &&  *_basename );
6104   ASSERT ( _resource  &&  *_resource );
6105   ASSERT ( value );
6106 
6107   return
6108     Nlm_XrmGetResource1(_basename,   _resource, value)  ||
6109     Nlm_XrmGetResource1(Nlm_VibName, _resource, value);
6110 }
6111 
6112 
Nlm_XrmGetResource(const Nlm_Char PNTR _resource)6113 extern Nlm_CharPtr Nlm_XrmGetResource(const Nlm_Char PNTR _resource)
6114 {
6115   XrmValue value;
6116   if (_resource  && Nlm_XrmGetResource2(Nlm_AppName, _resource, &value))
6117     return Nlm_StringSave( (Nlm_CharPtr)value.addr );
6118 
6119   return NULL;
6120 }
6121 
6122 
6123 extern XFontStruct *Nlm_XLoadStandardFont(void); /* <-- see in "ncbidraw.c" */
6124 
Nlm_SetupWindows(void)6125 static Nlm_Boolean Nlm_SetupWindows (void)
6126 {
6127   Nlm_Int4   height;
6128   Nlm_PoinT  pt;
6129   Nlm_RecT   r;
6130   Nlm_Int4   width;
6131   int        xx_argc = (int)Nlm_GetArgc();
6132   char     **xx_argv =      Nlm_GetArgv();
6133   XmFontListEntry font_entry;
6134 
6135   Nlm_desktopWindow = NULL;
6136   Nlm_systemWindow = NULL;
6137   Nlm_currentXDisplay = NULL;
6138   Nlm_currentXScreen = 0;
6139   Nlm_currentXWindow = 0;
6140   Nlm_currentXGC = NULL;
6141   Nlm_currentWindowTool = (Nlm_WindowTool) 0;
6142   quitProgram = FALSE;
6143   timerAction = NULL;
6144   keyAction = NULL;
6145   mouseMovedAction = NULL;
6146   registeredDropProc = NULL;
6147   registeredServiceProc = NULL;
6148   registeredResultProc = NULL;
6149   XtToolkitInitialize ();
6150   Nlm_appContext = XtCreateApplicationContext ();
6151   if (Nlm_appContext == NULL) {
6152     return FALSE;
6153   }
6154 
6155   if (xx_argc  &&  *xx_argv[0])
6156     {/* Get the application basename(cut off path and extention(s), locase) */
6157       const char *s     = xx_argv[0];
6158       const char *start = s;
6159 
6160       while ( *s )
6161         if (*s++ == '/') {
6162           while (*s  &&  (*s == '/'  ||  *s == '.'))
6163             s++;
6164           start = s;
6165         }
6166 
6167       for (s = start;  *s && *s != '.';  s++);
6168       ASSERT ( s  >=  start );
6169 
6170       if (s != start) {
6171         size_t len = s - start;
6172         ASSERT ( len  <  sizeof(Nlm_AppName) );
6173         Nlm_AppName[len] = '\0';
6174         while (len-- > 0)
6175           Nlm_AppName[len] = TO_LOWER( start[len] );
6176       }
6177     }
6178 
6179   {{
6180 #define disp_opTable_SIZE 2
6181     static XrmOptionDescRec disp_opTable[disp_opTable_SIZE] = {
6182       {"-display", ".display", XrmoptionSepArg, 0},
6183       {"-dpy",     ".display", XrmoptionSepArg, 0}
6184     };
6185     XrmString type_str;
6186     XrmDatabase disp_db = NULL;
6187     XrmValue value;
6188     XrmInitialize();
6189     XrmParseCommand(&disp_db, disp_opTable, disp_opTable_SIZE,
6190                     "vibrant", &xx_argc, xx_argv);
6191     if ( !XrmGetResource(disp_db,  "vibrant.display", "Vibrant.Display",
6192                          &type_str, &value) )
6193       value.addr = NULL;
6194     Nlm_currentXDisplay = XOpenDisplay( (char *)value.addr );
6195     XrmDestroyDatabase( disp_db );
6196   }}
6197 
6198   if (Nlm_currentXDisplay == NULL) {
6199     fprintf (stderr, "Vibrant applications require X Windows\n");
6200     return FALSE;
6201   }
6202 
6203   XSetErrorHandler( defaultXerror_handler );
6204 
6205   Nlm_currentXScreen = DefaultScreen (Nlm_currentXDisplay);
6206   width =  DisplayWidth (Nlm_currentXDisplay, Nlm_currentXScreen);
6207   height = DisplayHeight (Nlm_currentXDisplay, Nlm_currentXScreen);
6208 
6209   Nlm_LoadRect (&screenBitBounds, 0, 0, (Nlm_Int2) width, (Nlm_Int2) height);
6210   Nlm_LoadPt (&pt, 0, 0);
6211   r = screenBitBounds;
6212   Nlm_screenRect = screenBitBounds;
6213   Nlm_desktopWindow = (Nlm_WindoW) Nlm_HandNew (sizeof (Nlm_WindowRec));
6214   Nlm_LoadGraphicData((Nlm_GraphiC)Nlm_desktopWindow, NULL, NULL,
6215                       NULL, NULL, desktopProcs, NULL, &r,
6216                       TRUE, TRUE, NULL, NULL);
6217   Nlm_LoadBoxData((Nlm_BoX)Nlm_desktopWindow,
6218                   pt, pt, pt, 0, 0, 0, 0, 0, 0, 0, 0);
6219   Nlm_LoadWindowData(Nlm_desktopWindow,
6220                      NULL, NULL, NULL, NULL, &r, &r, &r, NULL, NULL,
6221                      NULL, NULL, NULL, NULL, NULL, TRUE, FALSE, NULL,
6222                      NULL, NULL, NULL);
6223 
6224   if (DisplayPlanes(Nlm_currentXDisplay, Nlm_currentXScreen) != 1)
6225     XtAppSetFallbackResources (Nlm_appContext, (String*)defFallbackRes );
6226 
6227   XtDisplayInitialize (Nlm_appContext, Nlm_currentXDisplay,
6228                        (String)Nlm_AppName, (String)Nlm_VibClass,
6229                        (XrmOptionDescRec *)NULL, (Cardinal)0,
6230                        &xx_argc, xx_argv);
6231   Nlm_SetupArguments(xx_argc, xx_argv);
6232 
6233   Nlm_currentXGC = Nlm_GetGC(DefaultDepth(Nlm_currentXDisplay,
6234                                           Nlm_currentXScreen));
6235 
6236   Nlm_SetUpDrawingTools();
6237 
6238   Nlm_RegisterStdTranslations();
6239 
6240   Nlm_hScrollBarHeight = 15;
6241   Nlm_vScrollBarWidth  = 15;
6242 
6243   Nlm_popupMenuHeight  = Nlm_stdLineHeight + 18;
6244   Nlm_dialogTextHeight = Nlm_stdLineHeight + 6;
6245 
6246   arrow = XCreateFontCursor (Nlm_currentXDisplay, XC_top_left_arrow);
6247   cross = XCreateFontCursor (Nlm_currentXDisplay, XC_cross);
6248   iBeam = XCreateFontCursor (Nlm_currentXDisplay, XC_xterm);
6249   plus  = XCreateFontCursor (Nlm_currentXDisplay, XC_crosshair);
6250   watch = XCreateFontCursor (Nlm_currentXDisplay, XC_watch);
6251 
6252   Nlm_currentCursor = arrow;
6253 
6254   font = Nlm_XLoadStandardFont();
6255 
6256   font_entry = XmFontListEntryCreate  ("dummy", XmFONT_IS_FONT, font);
6257   Nlm_XfontList = XmFontListAppendEntry(NULL, font_entry);
6258 
6259   Nlm_fileDialogShell = NULL;
6260 
6261   return TRUE;
6262 }
6263 #endif  /* WIN_MOTIF */
6264 
6265 
6266 /*
6267 #ifndef WIN_MOTIF
6268 static void Nlm_WindowGo (Nlm_WindoW w)
6269 {
6270   inNotice = FALSE;
6271   Nlm_DoRemove ((Nlm_GraphiC) w, TRUE);
6272 }
6273 
6274 static void Nlm_PanelGo (Nlm_PaneL p, Nlm_PoinT pt)
6275 {
6276   Nlm_WindoW  w;
6277 
6278   inNotice = FALSE;
6279   w = Nlm_ParentWindow (p);
6280   Nlm_DoRemove ((Nlm_GraphiC) w, TRUE);
6281 }
6282 
6283 
6284 static void Nlm_CenterLine (Nlm_RectPtr rptr, Nlm_CharPtr text, Nlm_FonT fnt)
6285 {
6286   if (fnt != NULL) {
6287     Nlm_SelectFont (fnt);
6288   }
6289   rptr->bottom = (Nlm_Int2)(rptr->top + Nlm_LineHeight());
6290   Nlm_DrawString (rptr, text, 'c', FALSE);
6291   rptr->top = rptr->bottom;
6292 }
6293 
6294 
6295 static void Nlm_DrawAbout (Nlm_PaneL p)
6296 
6297 {
6298   Nlm_RecT  r;
6299 
6300   Nlm_ObjectRect (p, &r);
6301   Nlm_InsetRect (&r, 4, 4);
6302   r.top += 10;
6303   Nlm_Blue ();
6304   Nlm_CenterLine (&r, "VIBRANT", Nlm_systemFont);
6305   r.top += 10;
6306   Nlm_Red ();
6307   Nlm_CenterLine (&r, "National Center for Biotechnology Information", Nlm_systemFont);
6308   r.top += 5;
6309   Nlm_CenterLine (&r, "National Library of Medicine", Nlm_systemFont);
6310   r.top += 5;
6311   Nlm_CenterLine (&r, "National Institutes of Health", Nlm_systemFont);
6312   r.top += 10;
6313   Nlm_CenterLine (&r, "(301) 496-2475", Nlm_systemFont);
6314   r.top += 5;
6315   Nlm_CenterLine (&r, "info@ncbi.nlm.nih.gov", Nlm_systemFont);
6316 }
6317 
6318 
6319 static void Nlm_GetSet (void)
6320 {
6321   Nlm_PaneL   p;
6322   Nlm_WindoW  w;
6323 
6324   w = Nlm_ModalWindow (-50, -33, -1, -1, Nlm_WindowGo);
6325   p = Nlm_SimplePanel (w, (Nlm_Int2)(28 * Nlm_stdCharWidth),
6326                           (Nlm_Int2)(10 * Nlm_stdLineHeight), Nlm_DrawAbout);
6327   Nlm_SetPanelClick (p, NULL, NULL, NULL, Nlm_PanelGo);
6328   Nlm_DoShow ((Nlm_GraphiC) w, TRUE, TRUE);
6329   inNotice = TRUE;
6330   while (inNotice) {
6331     Nlm_ProcessAnEvent ();
6332   }
6333 }
6334 #endif
6335 */
6336 
6337 #ifdef WIN_MSWIN
ParseSetupArguments(HINSTANCE hInstance,Nlm_CharPtr lpszCmdLine)6338 static void ParseSetupArguments(HINSTANCE hInstance, Nlm_CharPtr lpszCmdLine)
6339 {
6340   int    xx_argc;
6341   char **xx_argv;
6342 
6343   static char winfile[128];
6344   char *p;
6345 
6346   GetModuleFileName(hInstance, winfile, sizeof(winfile));
6347 
6348   if ((p = strrchr(winfile, DIRDELIMCHR)) != NULL)
6349     p++;
6350   else
6351     p = winfile;
6352   SetProgramName(p);
6353 
6354   if (!lpszCmdLine  ||  !*lpszCmdLine)
6355     {
6356       xx_argc = 1;
6357       xx_argv = (char **)Nlm_MemNew( sizeof(char*) );
6358       xx_argv[0] = winfile;
6359       Nlm_SetupArguments(xx_argc, xx_argv);
6360       return;
6361     }
6362 
6363   /* Count command-line arguments and separate them by '\0' */
6364   xx_argc = 1;
6365   for (p = lpszCmdLine;  *p; )
6366     {
6367       if ( isspace(*p) )
6368         {
6369           *p++ = '\0';
6370           continue;
6371         }
6372 
6373       if (*p == '\''  ||  *p == '"')
6374         {
6375           char quote = *p;
6376           xx_argc++;
6377           while (*(++p)  &&  *p != quote) continue;
6378           if ( *p )
6379             *p++ = '\0';
6380           continue;
6381         }
6382 
6383       xx_argc++;
6384       while (*p  &&  !isspace(*p))
6385         p++;
6386     }
6387 
6388   /* Allocate and fill out "xx_argv" */
6389   {{
6390     int   n = 0;
6391     char *s = lpszCmdLine;
6392     xx_argv = (char **)Nlm_MemNew(xx_argc * sizeof(char*));
6393     xx_argv[n++] = winfile;
6394     while (n < xx_argc)
6395       {
6396         while ( !*s )
6397           s++;
6398         if (*s == '\''  ||  *s == '"')
6399           s++; /* -- skip the leading quote */
6400         xx_argv[n++] = s;
6401         while ( *s )
6402           s++;
6403       }
6404 #ifdef _DEBUG
6405     while (s < p  &&  !*s)
6406       s++;
6407     ASSERT ( s == p );
6408 #endif
6409   }}
6410 
6411   Nlm_SetupArguments(xx_argc, xx_argv);
6412 }
6413 #endif
6414 
6415 
Nlm_GetReady(void)6416 static void Nlm_GetReady (void)
6417 {
6418   Nlm_VibrantSetGUI();
6419   Nlm_InitCursorShapes();
6420   Nlm_textScrapFull = FALSE;
6421   Nlm_processUpdatesFirstVal = TRUE;
6422 }
6423 
Nlm_CleanUpWindows(void)6424 static void Nlm_CleanUpWindows (void)
6425 
6426 /*
6427 *  Since submenus goes into the Macintosh desktop menu bar, to allow for
6428 *  command key equivalents, it can be very dangerous to remove the menu
6429 *  bar.  The submenu linked into the menu bar now no longer removes its
6430 *  children, leaving that up to the submenu item.
6431 */
6432 
6433 {
6434   Nlm_GraphiC  g;
6435   Nlm_MenuBaR  mb;
6436   Nlm_GraphiC  n;
6437   Nlm_WindoW   w;
6438 
6439   okayToDrawContents = FALSE; /* suppress all further slate content drawing */
6440 
6441   Nlm_RemoveDyingWindows();
6442 
6443 #ifdef WIN_MSWIN
6444   if (timerID != 0) {
6445     KillTimer (NULL, timerID);
6446   }
6447 #endif
6448 #ifdef WIN_MOTIF
6449   XtRemoveTimeOut (windowTimer);
6450 #endif
6451   w = (Nlm_WindoW) Nlm_GetNext ((Nlm_GraphiC) Nlm_desktopWindow);
6452   while (w != NULL && w != Nlm_systemWindow) {
6453     Nlm_UseWindow (w);
6454     Nlm_DoHide ((Nlm_GraphiC) w, TRUE, FALSE);
6455     g = Nlm_GetChild ((Nlm_GraphiC) w);
6456     while (g != NULL) {
6457       n = Nlm_GetNext (g);
6458       Nlm_DoRemove (g, FALSE);
6459       g = n;
6460     }
6461     mb = Nlm_GetWindowMenuBar (w);
6462     if (mb != NULL) {
6463       Nlm_DoRemove ((Nlm_GraphiC) mb, FALSE);
6464       Nlm_SetWindowMenuBar (w, NULL);
6465     }
6466     w = (Nlm_WindoW) Nlm_GetNext ((Nlm_GraphiC) w);
6467   }
6468   w = Nlm_desktopWindow;
6469   mb = Nlm_GetWindowMenuBar (w);
6470   if (mb != NULL) {
6471     Nlm_DoRemove ((Nlm_GraphiC) mb, FALSE);
6472     Nlm_SetWindowMenuBar (w, NULL);
6473   }
6474 #ifdef WIN_MOTIF
6475   resource_nm = (Nlm_CharPtr) Nlm_MemFree (resource_nm);
6476 #endif
6477 }
6478 
6479 
6480 #ifdef WIN_MSWIN
6481 #ifdef WIN32
6482 #ifndef __MINGW32__
Nlm_HandleException(DWORD code)6483 static int Nlm_HandleException (DWORD code)
6484 
6485 {
6486   Nlm_Message (MSG_OK, "WIN32 exception %ld", (long)code);
6487   return EXCEPTION_CONTINUE_SEARCH;
6488 }
6489 #endif
6490 #endif
6491 #endif
6492 
6493 #ifdef WIN_MOTIF
6494 #if defined(OS_UNIX) && defined(COMP_SUNPRO)
6495 static char **tenvp = NULL;
6496 
6497 extern void Nlm_DisplayEnvironmentVariables (void);
Nlm_DisplayEnvironmentVariables(void)6498 extern void Nlm_DisplayEnvironmentVariables (void)
6499 
6500 {
6501   FILE  *f;
6502   int   i;
6503 
6504   if (tenvp == NULL) return;
6505   f = Nlm_FileOpen ("EnvLogFile", "a");
6506   if (f == NULL) return;
6507   for (i = 0; tenvp [i] != NULL; i++) {
6508     fprintf (f, "%s\n", tenvp [i]);
6509   }
6510   Nlm_FileClose (f);
6511 }
6512 #endif
6513 #endif
6514 
6515 
6516 
6517 
6518 #ifdef WIN_MAC
6519 #ifdef OS_UNIX_DARWIN
Nlm_VibMainPrelude(int argc,char * argv[])6520 extern int Nlm_VibMainPrelude (int argc, char *argv[])
6521 {
6522   long   gval;
6523   OSStatus  err;
6524 
6525   Nlm_SetupArguments (argc, argv);
6526 # else /* ! OS_UNIX_DARWIN */
6527 extern void Nlm_VibMainPrelude ()
6528 {
6529   long   gval;
6530   OSErr  err;
6531 # endif
6532 
6533 #if __profile__
6534   ProfilerInit (collectDetailed, bestTimeBase, 1000, 50);
6535   ProfilerSetStatus (FALSE);
6536 #endif
6537 
6538 #if TARGET_API_MAC_CARBON >= 1
6539   /*
6540    carbon changes the API: pass the number of master pointers to allocate
6541    MoreMasters (1280);
6542    2001-03-22:  Joshua Juran
6543    CarbonDater report says to use MoreMasterPointers() instead of MoreMasters().
6544    Universal Interfaces 3.3.2 declares MoreMasters(void) under Carbon.
6545   */
6546   /* MoreMasterPointers(1280); Don't need this under OSX. */
6547   FlushEvents (everyEvent, 0);
6548   /* the rest of the toolbox is done for us can't init them...
6549    */
6550 #else
6551   MaxApplZone ();
6552   if (1) {
6553     Nlm_Int2 i;
6554     for (i = 0; i < 20; i++) {
6555       MoreMasters ();
6556     }
6557   }
6558   InitGraf (&qd.thePort);
6559   InitFonts ();
6560   FlushEvents (everyEvent, 0);
6561   InitWindows ();
6562   InitMenus ();
6563   TEInit ();
6564   InitDialogs (0);
6565 #endif
6566 #ifdef WIN_MAC_QUARTZ
6567   err = TXNInitTextension (NULL, 0, 0);
6568   if(err) abort();
6569 #else
6570   InitCursor ();
6571 #endif
6572 
6573   err = Gestalt (gestaltAppleEventsAttr, &gval);
6574   if (err == noErr && ((short) gval & (1 << gestaltAppleEventsPresent)) != 0) {
6575     /* Create Routine Descriptors - from RasMol */
6576     HandleAEIgnorePtr = NewAEEventHandlerUPP(HandleAEIgnore);
6577     HandleAEOpenDocPtr = NewAEEventHandlerUPP(HandleAEOpenDoc);
6578     HandleAEQuitAppPtr = NewAEEventHandlerUPP(HandleAEQuitApp);
6579     HandleAEAnswerPtr = NewAEEventHandlerUPP(HandleAEAnswer);
6580 
6581     /* Install Required Event Handlers */
6582     AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
6583                           HandleAEIgnorePtr, 0, FALSE);
6584     AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
6585                           HandleAEOpenDocPtr, 0, FALSE);
6586     AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,
6587                           HandleAEIgnorePtr, 1, FALSE);
6588     AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
6589                           HandleAEQuitAppPtr, 0, FALSE);
6590     AEInstallEventHandler(kCoreEventClass,kAEAnswer,
6591                           HandleAEAnswerPtr, 0, FALSE);
6592   }
6593 
6594   Nlm_usesMacNavServices = FALSE;
6595   err = Gestalt (gestaltAppleEventsAttr, &gval);
6596   if (err == noErr && ((short) gval & (1 << gestaltAppearanceExists)) != 0) {
6597     if (NavServicesAvailable ()) {
6598       Nlm_usesMacNavServices = TRUE;
6599     }
6600   }
6601   if (Nlm_usesMacNavServices) {
6602     NavLoad ();
6603   }
6604 
6605   Nlm_InitVibrantHooks ();
6606 
6607   Nlm_InitWindows ();
6608   Nlm_InitBars ();
6609   Nlm_InitButtons ();
6610   Nlm_InitExtras ();
6611   Nlm_InitForms ();
6612   Nlm_InitGroup ();
6613   Nlm_InitLists ();
6614   Nlm_InitMenus ();
6615   Nlm_InitPrompt ();
6616   Nlm_InitSlate ();
6617   Nlm_InitTexts ();
6618   Nlm_SetupWindows ();
6619   Nlm_RegisterWindows ();
6620   Nlm_RegisterTexts ();
6621   Nlm_RegisterSlates ();
6622 #ifndef OS_UNIX_DARWIN
6623   Nlm_SetupArguments(0, NULL);
6624 #endif
6625   Nlm_GetReady ();
6626 
6627   /* Initialize connection library's logger, registry and lock */
6628   CONNECT_Init(0);
6629 
6630   /* There is no need to initialize the TE private scrap because
6631    * we never assume it's current.
6632    */
6633 }
6634 
6635 
6636 
6637 
6638 #ifdef OS_UNIX_DARWIN
6639 extern int Nlm_VibMainFinale (int argc, char *argv[])
6640 {
6641 # else /* ! OS_UNIX_DARWIN */
6642 extern void Nlm_VibMainFinale ()
6643 {
6644 # endif
6645 
6646   Nlm_CleanUpWindows ();
6647   Nlm_CleanUpDrawingTools ();
6648   Nlm_FreeWindows ();
6649   Nlm_FreeBars ();
6650   Nlm_FreeButtons ();
6651   Nlm_FreeExtras ();
6652   Nlm_FreeForms ();
6653   Nlm_FreeGroup ();
6654   Nlm_FreeLists ();
6655   Nlm_FreeMenus ();
6656   Nlm_FreePrompt ();
6657   Nlm_FreeSlate ();
6658   Nlm_FreeTexts ();
6659   Nlm_FreeConfigStruct ();
6660   Nlm_ErrSetLogfile (NULL,0);
6661   if (Nlm_usesMacNavServices) {
6662     NavUnload ();
6663   }
6664 
6665 #if __profile__
6666   ProfilerDump ("\pvibrant.prof");
6667   ProfilerTerm ();
6668 #endif
6669 }
6670 #endif
6671 
6672 
6673 
6674 
6675 #ifdef WIN_MSWIN
6676 extern int CALLBACK Nlm_VibMainPrelude (
6677   HINSTANCE hInstance,
6678   HINSTANCE hPrevInstance,
6679   LPSTR lpszCmdLine,
6680   int nCmdShow
6681 )
6682 {
6683   Nlm_Char  str [32];
6684 
6685 #ifdef WIN32
6686 #ifndef __MINGW32__
6687   __try {
6688 #endif
6689 #endif
6690 
6691   Nlm_currentHInst = hInstance;
6692   ParseSetupArguments(Nlm_currentHInst, StringSave(lpszCmdLine));
6693 
6694   Nlm_InitVibrantHooks ();
6695 
6696   Nlm_InitWindows ();
6697   Nlm_InitBars ();
6698   Nlm_InitButtons ();
6699   Nlm_InitExtras ();
6700   Nlm_InitForms ();
6701   Nlm_InitGroup ();
6702   Nlm_InitLists ();
6703   Nlm_InitMenus ();
6704   Nlm_InitPrompt ();
6705   Nlm_InitSlate ();
6706   Nlm_InitTexts ();
6707   Nlm_SetupWindows ();
6708   if (! (Nlm_RegisterWindows ())) {
6709     return FALSE;
6710   }
6711   if (! (Nlm_RegisterTexts ())) {
6712     return FALSE;
6713   }
6714 
6715   if (! (Nlm_RegisterSlates ())) {
6716     sprintf (str, "Nlm_WindowClass%ld", (long) (int) Nlm_currentHInst);
6717     UnregisterClass (str, Nlm_currentHInst);
6718     return FALSE;
6719   }
6720 
6721 
6722   Nlm_GetReady ();
6723 
6724   /* Initialize connection library's logger, registry and lock */
6725   CONNECT_Init(0);
6726 
6727 #ifdef WIN32
6728 #ifndef __MINGW32__
6729   }
6730   __except ( Nlm_HandleException( GetExceptionCode() ) )  { }
6731 #endif
6732 #endif
6733 
6734   return TRUE;
6735 }
6736 
6737 
6738 extern int CALLBACK Nlm_VibMainFinale (
6739   HINSTANCE hInstance,
6740   HINSTANCE hPrevInstance,
6741   LPSTR lpszCmdLine,
6742   int nCmdShow
6743 )
6744 {
6745   Nlm_Char  str [32];
6746   WNDCLASS  wc;
6747 
6748 #ifdef WIN32
6749 #ifndef __MINGW32__
6750   __try {
6751 #endif
6752 #endif
6753 
6754   Nlm_CleanUpWindows ();
6755   Nlm_CleanUpDrawingTools ();
6756   Nlm_FreeWindows ();
6757   Nlm_FreeBars ();
6758   Nlm_FreeButtons ();
6759   Nlm_FreeExtras ();
6760   Nlm_FreeForms ();
6761   Nlm_FreeGroup ();
6762   Nlm_FreeLists ();
6763   Nlm_FreeMenus ();
6764   Nlm_FreePrompt ();
6765   Nlm_FreeSlate ();
6766   Nlm_FreeTexts ();
6767   Nlm_FreeConfigStruct ();
6768   Nlm_ErrSetLogfile (NULL,0);
6769 
6770   sprintf (str, "Nlm_WindowClass%ld", (long) (int) Nlm_currentHInst);
6771   if (GetClassInfo (Nlm_currentHInst, str, &wc)) {
6772     /* WNDCLASS description says application should not destroy background brush */
6773     /* DeleteObject (wc.hbrBackground); */
6774   }
6775   UnregisterClass (str, Nlm_currentHInst);
6776   sprintf (str, "Nlm_SlateClass%ld", (long) (int) Nlm_currentHInst);
6777   UnregisterClass (str, Nlm_currentHInst);
6778   Nlm_MemFree( Nlm_GetArgv() );
6779 
6780 #ifdef WIN32
6781 #ifndef __MINGW32__
6782   }
6783   __except ( Nlm_HandleException( GetExceptionCode() ) )  { }
6784 #endif
6785 #endif
6786 
6787   return TRUE;
6788 }
6789 #endif
6790 
6791 
6792 
6793 #ifdef WIN_MOTIF
6794 #if defined(OS_UNIX) && defined(COMP_SUNPRO)
6795 extern void Nlm_VibMainPrelude (int argc, char *argv[], char *envp[])
6796 #else
6797 extern void Nlm_VibMainPrelude (int argc, char *argv[])
6798 #endif
6799 {
6800   Nlm_Int2  retval;
6801 
6802   Nlm_SetupArguments(argc, argv);
6803 
6804 #if defined(OS_UNIX) && defined(COMP_SUNPRO)
6805   tenvp = envp;
6806 #endif
6807 
6808   Nlm_InitVibrantHooks ();
6809 
6810   Nlm_InitWindows ();
6811   Nlm_InitButtons ();
6812   Nlm_InitExtras ();
6813   Nlm_InitForms ();
6814   Nlm_InitGroup ();
6815   Nlm_InitBars ();
6816   Nlm_InitLists ();
6817   Nlm_InitMenus ();
6818   Nlm_InitPrompt ();
6819   Nlm_InitSlate ();
6820   Nlm_InitTexts ();
6821   if (! Nlm_SetupWindows ()) return;
6822   Nlm_RegisterWindows ();
6823   Nlm_RegisterTexts ();
6824   Nlm_RegisterSlates ();
6825 
6826   Nlm_GetReady ();
6827 
6828   /* Initialize connection library's logger, registry and lock */
6829   CONNECT_Init(0);
6830 }
6831 
6832 
6833 
6834 #if defined(OS_UNIX) && defined(COMP_SUNPRO)
6835 extern void Nlm_VibMainFinale (int argc, char *argv[], char *envp[])
6836 #else
6837 extern void Nlm_VibMainFinale (int argc, char *argv[])
6838 #endif
6839 {
6840   Nlm_CleanUpWindows ();
6841   Nlm_CleanUpDrawingTools ();
6842   Nlm_FreeWindows ();
6843   Nlm_FreeBars ();
6844   Nlm_FreeButtons ();
6845   Nlm_FreeExtras ();
6846   Nlm_FreeForms ();
6847   Nlm_FreeGroup ();
6848   Nlm_FreeLists ();
6849   Nlm_FreeMenus ();
6850   Nlm_FreePrompt ();
6851   Nlm_FreeSlate ();
6852   Nlm_FreeTexts ();
6853   Nlm_FreeConfigStruct ();
6854   Nlm_currentWindowTool = (Nlm_WindowTool) 0;
6855   Nlm_currentXWindow = 0;
6856   Nlm_currentXGC = NULL;
6857   if (Nlm_currentXDisplay != NULL) {
6858     if (Nlm_fileDialogShell != NULL) {
6859       XtDestroyWidget (Nlm_fileDialogShell);
6860     }
6861     if (Nlm_XfontList != NULL) {
6862       XmFontListFree (Nlm_XfontList);
6863     }
6864     XFreeCursor (Nlm_currentXDisplay, arrow);
6865     XFreeCursor (Nlm_currentXDisplay, cross);
6866     XFreeCursor (Nlm_currentXDisplay, iBeam);
6867     XFreeCursor (Nlm_currentXDisplay, plus);
6868     XFreeCursor (Nlm_currentXDisplay, watch);
6869     Nlm_FreeGCs ();
6870     XCloseDisplay (Nlm_currentXDisplay);
6871   }
6872   Nlm_ErrSetLogfile (NULL,0);
6873 }
6874 #endif
6875 
6876 
6877 
6878 
6879 #ifdef WIN_MOTIF
6880 static Nlm_Boolean IsXParentOf(Window is_parent, Window w)
6881 {
6882   Window root, parent, *children;
6883   unsigned int nchildren, i;
6884 
6885   if (is_parent == (Window)0  ||  w == (Window)0)
6886     return FALSE;
6887   else if (is_parent == w)
6888     return TRUE;
6889 
6890   if ( !XQueryTree(Nlm_currentXDisplay, is_parent, &root, &parent,
6891                    &children, &nchildren) )
6892     return FALSE;
6893 
6894   for (i = 0;  i < nchildren  &&  !IsXParentOf(children[i], w);  i++);
6895   if ( children )
6896     XFree( children );
6897 
6898   return (i != nchildren);
6899 }
6900 #endif
6901 
6902 
6903 extern void Nlm_RemoveDyingWindows (void)
6904 
6905 /*
6906 *  Since submenus goes into the Macintosh desktop menu bar, to allow for
6907 *  command key equivalents, it can be very dangerous to remove the menu
6908 *  bar.  The submenu linked into the menu bar now no longer removes its
6909 *  children, leaving that up to the submenu item.
6910 */
6911 
6912 {
6913   Nlm_GraphiC      g;
6914   Nlm_GraphicData  gdata;
6915   Nlm_MenuBaR      mb;
6916   Nlm_GraphiC      n;
6917 #ifndef WIN_MOTIF
6918   Nlm_PortTool     tempPort;
6919 #endif
6920   Nlm_WindoW       w;
6921   Nlm_WindowData   wdata;
6922   Nlm_WindowTool   wptr;
6923 #ifdef WIN_MAC
6924   PenState         state;
6925 #endif
6926 
6927   while (dyingWindow != NULL) {
6928     w = dyingWindow;
6929     wptr = Nlm_ParentWindowPtr ((Nlm_GraphiC) w);
6930 #ifdef WIN_MAC
6931 #ifndef WIN_MAC_QUARTZ
6932     GetPort (&tempPort);
6933     GetPenState (&state);
6934 #endif
6935     Nlm_SetPortWindowPort(wptr);
6936     Nlm_currentWindowTool = wptr;
6937     Nlm_SetUpdateRegion (wptr);
6938 #endif
6939 #ifdef WIN_MSWIN
6940     tempPort = Nlm_currentHDC;
6941     Nlm_currentHDC = Nlm_ParentWindowPort ((Nlm_GraphiC) w);
6942     Nlm_currentWindowTool = wptr;
6943 #endif
6944 #ifdef WIN_MOTIF
6945     XSync (Nlm_currentXDisplay, FALSE);
6946     {{
6947       Window xw = XtWindow(  Nlm_GetWindowShell(w) );
6948       if ( IsXParentOf(xw, Nlm_currentXWindow) )
6949         {
6950           Nlm_currentWindowTool = (Nlm_WindowTool)0;
6951           Nlm_currentXWindow = 0;
6952         }
6953     }}
6954 #endif
6955     g = Nlm_GetChild ((Nlm_GraphiC) w);
6956     while (g != NULL) {
6957       n = Nlm_GetNext (g);
6958       Nlm_DoRemove (g, FALSE);
6959       g = n;
6960     }
6961 
6962     mb = Nlm_GetWindowMenuBar (w);
6963     if (mb != NULL) {
6964       Nlm_DoRemove ((Nlm_GraphiC) mb, FALSE);
6965       Nlm_SetWindowMenuBar (w, NULL);
6966     }
6967 
6968 #if !defined(WIN_MOTIF) && !defined(WIN_MAC_QUARTZ)
6969     Nlm_ResetDrawingTools ();
6970 #endif
6971 
6972     Nlm_GetWindowData (w, &wdata);
6973     if (wdata.data != NULL && wdata.cleanup != NULL) {
6974       wdata.cleanup (w, wdata.data);
6975     }
6976 #ifdef WIN_MAC
6977 #ifndef WIN_MAC_QUARTZ
6978     if ( wdata.cMap != NULL ){
6979       DisposePalette ( wdata.cMap );
6980       wdata.cMap = NULL;
6981     }
6982     Nlm_SetPort(tempPort);
6983     Nlm_currentWindowTool = GetWindowFromPort(tempPort);
6984     Nlm_SetUpdateRegion (GetWindowFromPort(tempPort));
6985     SetPenState (&state);
6986 #endif
6987     /* 2001-03-22:  Joshua Juran
6988        Carbon does not support application-supplied storage for windows. */
6989     DisposeWindow(wptr);
6990 #endif
6991 #ifdef WIN_MSWIN
6992     if ( wdata.cMap != NULL ){
6993       DeleteObject( wdata.cMap );
6994       wdata.cMap = NULL;
6995     }
6996     if ( wdata.haccel != NULL ){
6997       DestroyAcceleratorTable(wdata.haccel);
6998       wdata.haccel = NULL;
6999     }
7000     Nlm_currentHDC = tempPort;
7001     RemoveProp (wptr, (LPSTR) "Nlm_VibrantProp");
7002     DestroyWindow (wptr);
7003 #endif
7004 #ifdef WIN_MOTIF
7005     if ( wdata.cMap )  {
7006       if ( wdata.cMapStatus ) {
7007         XUninstallColormap(Nlm_currentXDisplay,wdata.cMap);
7008         wdata.cMapStatus = 0;
7009       }
7010       XFreeColormap (Nlm_currentXDisplay,wdata.cMap);
7011       wdata.cMap = (Nlm_ColorMTool) 0;
7012     }
7013     if (wdata.title != NULL) {
7014       Nlm_MemFree (wdata.title);
7015     }
7016 
7017     {{
7018       Nlm_ShellTool shl = Nlm_GetWindowShell( w );
7019       XtUnrealizeWidget(shl);
7020       if (
7021 #ifdef NLM_MOTIF_CASCADEB_BUG
7022           !mb  &&
7023 #endif
7024           (!NLM_QUIET  ||  Nlm_WindowHasBeenShown( w ))) {
7025         XtDestroyWidget( shl );
7026       }
7027     }}
7028     XSync (Nlm_currentXDisplay, FALSE);
7029 #endif
7030     dyingWindow = (Nlm_WindoW) Nlm_GetNext ((Nlm_GraphiC) w);
7031     Nlm_GetGraphicData ((Nlm_GraphiC) w, &gdata);
7032     if (gdata.data != NULL && gdata.cleanup != NULL) {
7033       gdata.cleanup ((Nlm_GraphiC) w, gdata.data);
7034     }
7035     Nlm_HandFree (w);
7036     recentWindow = NULL;
7037     Nlm_Update ();
7038   }
7039 }
7040 
7041 
7042 #ifdef WIN_X
7043 static Boolean Nlm_XCheckUpdateEvent(XEvent *event)
7044 {
7045   return XCheckMaskEvent(Nlm_currentXDisplay, ExposureMask|StructureNotifyMask,
7046                          event);
7047 }
7048 #endif
7049 
7050 
7051 #ifdef WIN_MAC
7052 static Nlm_RegioN macMouseRgn = NULL;
7053 #endif
7054 
7055 extern void Nlm_SetMouseMoveCallback (Nlm_ClckProc actn)
7056 
7057 {
7058   mouseMovedAction = actn;
7059 }
7060 
7061 extern void Nlm_SetMouseMoveRegion (Nlm_RegioN rgn)
7062 
7063 {
7064 #ifdef WIN_MAC
7065   if (macMouseRgn != NULL) {
7066     macMouseRgn = Nlm_DestroyRgn (macMouseRgn);
7067   }
7068   if (rgn == NULL) return;
7069   macMouseRgn = Nlm_CreateRgn ();
7070   Nlm_UnionRgn (macMouseRgn, rgn, macMouseRgn);
7071 #endif
7072 }
7073 
7074 extern void Nlm_Update (void)
7075 {
7076 #ifdef WIN_MAC
7077   while (EventAvail (updateMask, &Nlm_currentEvent)) {
7078     if (WaitNextEvent (updateMask, &Nlm_currentEvent, 0, (Nlm_RgnTool) macMouseRgn)) {
7079       Nlm_HandleEvent ();
7080     }
7081   }
7082 #endif
7083 #ifdef WIN_MSWIN
7084   while (PeekMessage (&Nlm_currentMssg, NULL, WM_PAINT, WM_PAINT, PM_NOREMOVE)) {
7085     if (GetMessage (&Nlm_currentMssg, NULL, WM_PAINT, WM_PAINT)) {
7086       if (Nlm_VibrantDisabled ()) {
7087         discard_count++; /* really just a place to set a breakpoint */
7088       }
7089       TranslateMessage (&Nlm_currentMssg);
7090       DispatchMessage (&Nlm_currentMssg);
7091     }
7092   }
7093 #endif
7094 #ifdef WIN_MOTIF
7095   XEvent event;
7096   XSync (Nlm_currentXDisplay, FALSE);
7097   while ( Nlm_XCheckUpdateEvent(&event) )
7098     {
7099       XtDispatchEvent (&event);
7100     }
7101   XFlush( Nlm_currentXDisplay );
7102 #endif
7103 }
7104 
7105 extern void Nlm_KeyboardView (Nlm_KeyProc key)
7106 
7107 {
7108   keyAction = key;
7109 }
7110 
7111 #ifdef WIN_MSWIN
7112 static void Nlm_ProcessKeyPress (LPMSG lpMsg)
7113 
7114 {
7115   Nlm_Char  ch, sp_ch;
7116 
7117   if (keyAction == NULL) return;
7118 
7119   ch = (Nlm_Char) lpMsg->wParam;
7120   if (lpMsg->message == WM_CHAR)
7121   {
7122     keyAction (ch);
7123   }
7124   else if (lpMsg->message == WM_KEYDOWN)
7125   {
7126     sp_ch = Nlm_KeydownToChar ((Nlm_Uchar)ch);
7127     if (sp_ch != 0)
7128     {
7129       keyAction (sp_ch);
7130     }
7131   }
7132 }
7133 #endif
7134 
7135 #ifdef WIN_MOTIF
7136 static void Nlm_ProcessKeyPress (XEvent *event)
7137 
7138 {
7139   Nlm_Char ch;
7140 
7141   if (event->type == KeyPress  &&  keyAction != NULL)
7142   {
7143     ch = Nlm_GetInputChar (&event->xkey);
7144     Nlm_ctrlKey  = ((event->xkey.state & ControlMask) != 0);
7145     Nlm_shftKey  = ((event->xkey.state & ShiftMask  ) != 0);
7146     Nlm_cmmdKey = FALSE;
7147     Nlm_optKey = FALSE;
7148     keyAction( ch );
7149   }
7150 }
7151 #endif
7152 
7153 #ifdef WIN_MSWIN
7154 
7155 typedef void (LIBCALLBACK *CallbackProc)(void* data);
7156 static UINT idWMVibrantCallback = 0;
7157 
7158 static void Nlm_InitVibrantCallback ()
7159 
7160 {
7161   idWMVibrantCallback = RegisterWindowMessage("VibrantCallback");
7162 }
7163 
7164 
7165 static Nlm_Boolean Nlm_ProcessVibrantCallback (LPMSG lpMsg)
7166 
7167 {
7168   if(idWMVibrantCallback && idWMVibrantCallback == lpMsg->message)
7169   {
7170     CallbackProc proc = (CallbackProc)lpMsg->wParam;
7171     proc((void*)(lpMsg->lParam));
7172     return TRUE;
7173   }
7174   else
7175     return FALSE;
7176 }
7177 #endif
7178 
7179 #ifdef WIN_MSWIN
7180 
7181 extern Nlm_Handle Nlm_GetWindowHAccel (Nlm_WindoW w);
7182 extern Nlm_Handle Nlm_GetWindowHAccel (Nlm_WindoW w)
7183 
7184 {
7185   Nlm_Handle      rsult;
7186   Nlm_WindowData  wdata;
7187 
7188   rsult = (Nlm_Handle) 0;
7189   if (w != NULL) {
7190     Nlm_GetWindowData (w, &wdata);
7191     rsult = wdata.haccel;
7192   }
7193   return rsult;
7194 }
7195 
7196 
7197 extern void Nlm_SetWindowHAccel (Nlm_WindoW w, Nlm_Handle h);
7198 extern void Nlm_SetWindowHAccel (Nlm_WindoW w, Nlm_Handle h)
7199 
7200 {
7201   Nlm_WindowData  wdata;
7202 
7203   if (w != NULL) {
7204     Nlm_GetWindowData (w, &wdata);
7205     wdata.haccel = h;
7206     Nlm_SetWindowData (w, &wdata);
7207   }
7208 }
7209 
7210 
7211 static Nlm_Boolean Nlm_ProcessAccelerator (LPMSG lpMsg)
7212 
7213 {
7214   if(lpMsg->hwnd != NULL &&
7215      (lpMsg->message == WM_KEYDOWN ||
7216       lpMsg->message == WM_KEYUP ||
7217       lpMsg->message == WM_SYSKEYDOWN ||
7218       lpMsg->message == WM_SYSKEYUP ||
7219       lpMsg->message == WM_CHAR))
7220   {
7221     Nlm_WindoW w = (Nlm_WindoW) GetProp (lpMsg->hwnd, (LPSTR) "Nlm_VibrantProp");
7222     if(w != NULL)
7223     {
7224       Nlm_WindoW p = Nlm_GetParentWindow((Nlm_GraphiC)w);
7225       if(p != NULL)
7226       {
7227         Nlm_WindowData wdata;
7228         Nlm_GetWindowData(p, &wdata);
7229         if(wdata.haccel != NULL)
7230         {
7231           int ret = TranslateAccelerator(wdata.handle, wdata.haccel, lpMsg);
7232           return ret != 0;
7233         }
7234       }
7235     }
7236   }
7237 
7238   return FALSE;
7239 }
7240 #endif
7241 
7242 extern void Nlm_ProcessAnEvent (void)
7243 
7244 {
7245 #ifdef WIN_MAC
7246   if (EventAvail (everyEvent, &Nlm_currentEvent)) {
7247     if (WaitNextEvent (everyEvent, &Nlm_currentEvent, 0, (Nlm_RgnTool) macMouseRgn)) {
7248       Nlm_HandleEvent ();
7249     }
7250   }
7251 #endif
7252 #ifdef WIN_MSWIN
7253   if (PeekMessage (&Nlm_currentMssg, NULL, 0, 0, PM_NOREMOVE)) {
7254     if (GetMessage (&Nlm_currentMssg, NULL, 0, 0)) {
7255       TranslateMessage (&Nlm_currentMssg);
7256       Nlm_ProcessKeyPress (&Nlm_currentMssg);
7257       Nlm_ProcessVibrantCallback(&Nlm_currentMssg);
7258       DispatchMessage (&Nlm_currentMssg);
7259     }
7260   }
7261 #endif
7262 #ifdef WIN_MOTIF
7263   XEvent  event;
7264 
7265   if (XtAppPending (Nlm_appContext) & XtIMXEvent) {
7266     XtAppNextEvent (Nlm_appContext, &event);
7267     Nlm_ProcessKeyPress (&event);
7268     XtDispatchEvent (&event);
7269   }
7270 #endif
7271 }
7272 
7273 extern void Nlm_ProcessTimerEvent (void)
7274 
7275 {
7276 #ifdef WIN_MAC
7277   Nlm_Int4  delta;
7278   Nlm_Int4  ticks;
7279 
7280   ticks = Nlm_ComputerTime ();
7281   delta = ticks - lastTimerTime;
7282   if (delta >= 3) {
7283     lastTimerTime = ticks;
7284     if (timerAction != NULL) {
7285       timerAction ();
7286     }
7287     Nlm_CallWindowTimers ();
7288   }
7289 #endif
7290 }
7291 
7292 #ifdef WIN_MAC
7293 static void Nlm_ProcessIdle (void)
7294 
7295 {
7296   Nlm_PointTool  ptool;
7297 
7298   Nlm_ProcessTimerEvent ();
7299   GetMouse (&ptool);
7300   Nlm_PointToolToPoinT (ptool, &Nlm_globalMouse);
7301   Nlm_LocalToGlobal (&Nlm_globalMouse);
7302   frontWindow = Nlm_FindWindowRec (FrontWindow ());
7303   Nlm_DoIdle ((Nlm_GraphiC) frontWindow, Nlm_globalMouse);
7304   Nlm_ClearKeys ();
7305 }
7306 #endif
7307 
7308 extern void Nlm_ProcessEventOrIdle (void)
7309 
7310 {
7311 #ifdef WIN_MAC
7312   if (EventAvail (everyEvent, &Nlm_currentEvent)) {
7313     if (WaitNextEvent (everyEvent, &Nlm_currentEvent, 0, (Nlm_RgnTool) macMouseRgn)) {
7314       Nlm_HandleEvent ();
7315     }
7316   } else {
7317     Nlm_ProcessIdle ();
7318   }
7319 #endif
7320 #ifdef WIN_MSWIN
7321   if (Nlm_processUpdatesFirstVal &&
7322       PeekMessage (&Nlm_currentMssg, NULL, WM_PAINT, WM_PAINT, PM_NOREMOVE)) {
7323     if (GetMessage (&Nlm_currentMssg, NULL, WM_PAINT, WM_PAINT)) {
7324       TranslateMessage (&Nlm_currentMssg);
7325       DispatchMessage (&Nlm_currentMssg);
7326     }
7327   } else if (PeekMessage (&Nlm_currentMssg, NULL, 0, 0, PM_NOREMOVE)) {
7328     if (GetMessage (&Nlm_currentMssg, NULL, 0, 0)) {
7329       TranslateMessage (&Nlm_currentMssg);
7330       Nlm_ProcessKeyPress (&Nlm_currentMssg);
7331       Nlm_ProcessVibrantCallback(&Nlm_currentMssg);
7332       DispatchMessage (&Nlm_currentMssg);
7333     }
7334   }
7335 #endif
7336 #ifdef WIN_MOTIF
7337   XEvent  event;
7338 
7339   if (XtAppPending (Nlm_appContext) & XtIMXEvent) {
7340     if (Nlm_processUpdatesFirstVal &&
7341         Nlm_XCheckUpdateEvent(&event)) {
7342       XtDispatchEvent (&event);
7343     } else {
7344       XtAppNextEvent (Nlm_appContext, &event);
7345       Nlm_ProcessKeyPress (&event);
7346       XtDispatchEvent (&event);
7347     }
7348   }
7349 #endif
7350 }
7351 
7352 #if 1
7353 extern void Nlm_ProcessExternalEvent (void)
7354 
7355 {
7356 #ifdef WIN_MAC
7357   if (WaitNextEvent (everyEvent, &Nlm_currentEvent, 10, (Nlm_RgnTool) macMouseRgn)) {
7358     Nlm_HandleEvent ();
7359   }
7360 #endif
7361 #ifdef WIN_MSWIN
7362   /* Yield to other processes and threads */
7363   MsgWaitForMultipleObjectsEx(0, NULL, 10, QS_ALLEVENTS, MWMO_INPUTAVAILABLE);
7364 
7365   if (Nlm_processUpdatesFirstVal &&
7366       PeekMessage (&Nlm_currentMssg, NULL, WM_PAINT, WM_PAINT, PM_NOREMOVE)) {
7367     if (GetMessage (&Nlm_currentMssg, NULL, WM_PAINT, WM_PAINT)) {
7368       TranslateMessage (&Nlm_currentMssg);
7369       DispatchMessage (&Nlm_currentMssg);
7370     }
7371   } else if (PeekMessage (&Nlm_currentMssg, NULL, 0, 0, PM_NOREMOVE)) {
7372     if (GetMessage (&Nlm_currentMssg, NULL, 0, 0)) {
7373       TranslateMessage (&Nlm_currentMssg);
7374       Nlm_ProcessKeyPress (&Nlm_currentMssg);
7375       Nlm_ProcessVibrantCallback(&Nlm_currentMssg);
7376       DispatchMessage (&Nlm_currentMssg);
7377     }
7378   }
7379 #endif
7380 #ifdef WIN_MOTIF
7381   XEvent  event;
7382 
7383   if (XtAppPending (Nlm_appContext) & XtIMXEvent) {
7384     if (Nlm_processUpdatesFirstVal &&
7385         Nlm_XCheckUpdateEvent(&event)) {
7386       XtDispatchEvent (&event);
7387     } else {
7388       XtAppNextEvent (Nlm_appContext, &event);
7389       Nlm_ProcessKeyPress (&event);
7390       XtDispatchEvent (&event);
7391     }
7392   }
7393 #endif
7394 }
7395 #else
7396 extern void Nlm_ProcessExternalEvent (void)
7397 
7398 {
7399 #ifdef WIN_MAC
7400   if (WaitNextEvent (everyEvent, &Nlm_currentEvent, 10, (Nlm_RgnTool) macMouseRgn)) {
7401     Nlm_HandleEvent ();
7402   }
7403 #endif
7404 #ifdef WIN_MSWIN
7405   if (Nlm_processUpdatesFirstVal &&
7406       PeekMessage (&Nlm_currentMssg, NULL, WM_PAINT, WM_PAINT, PM_NOREMOVE)) {
7407     if (GetMessage (&Nlm_currentMssg, NULL, WM_PAINT, WM_PAINT)) {
7408       TranslateMessage (&Nlm_currentMssg);
7409       DispatchMessage (&Nlm_currentMssg);
7410     }
7411   } else if (PeekMessage (&Nlm_currentMssg, NULL, 0, 0, PM_NOREMOVE)) {
7412     if (GetMessage (&Nlm_currentMssg, NULL, 0, 0)) {
7413       TranslateMessage (&Nlm_currentMssg);
7414       Nlm_ProcessKeyPress (&Nlm_currentMssg);
7415       Nlm_ProcessVibrantCallback(&Nlm_currentMssg);
7416       DispatchMessage (&Nlm_currentMssg);
7417     }
7418   }
7419 #endif
7420 #ifdef WIN_MOTIF
7421   XEvent  event;
7422 
7423   if (XtAppPending (Nlm_appContext) & XtIMXEvent) {
7424     if (Nlm_processUpdatesFirstVal &&
7425         Nlm_XCheckUpdateEvent(&event)) {
7426       XtDispatchEvent (&event);
7427     } else {
7428       XtAppNextEvent (Nlm_appContext, &event);
7429       Nlm_ProcessKeyPress (&event);
7430       XtDispatchEvent (&event);
7431     }
7432   }
7433 #endif
7434 }
7435 #endif
7436 
7437 extern void Nlm_ProcessEvents (void)
7438 
7439 {
7440 #ifdef WIN_MAC
7441   while (! quitProgram) {
7442     if (! EventAvail (everyEvent, &Nlm_currentEvent)) {
7443       Nlm_ProcessIdle ();
7444     }
7445     while (WaitNextEvent (everyEvent, &Nlm_currentEvent, 1, (Nlm_RgnTool) macMouseRgn)) {
7446       Nlm_HandleEvent ();
7447       Nlm_RemoveDyingWindows ();
7448     }
7449   }
7450 #endif
7451 #ifdef WIN_MSWIN
7452   Nlm_InitVibrantCallback ();
7453   if (registeredDropProc != NULL) {
7454     DragAcceptFiles(Nlm_currentHWnd, TRUE);
7455   }
7456   while (! quitProgram) {
7457     while (Nlm_processUpdatesFirstVal &&
7458            PeekMessage (&Nlm_currentMssg, NULL, WM_PAINT, WM_PAINT, PM_NOREMOVE)) {
7459       if (GetMessage (&Nlm_currentMssg, NULL, WM_PAINT, WM_PAINT)) {
7460         TranslateMessage (&Nlm_currentMssg);
7461         DispatchMessage (&Nlm_currentMssg);
7462       }
7463     }
7464     if (GetMessage (&Nlm_currentMssg, NULL, 0, 0)) {
7465       if (!Nlm_ProcessAccelerator(&Nlm_currentMssg)) {
7466         TranslateMessage (&Nlm_currentMssg);
7467         Nlm_ProcessKeyPress (&Nlm_currentMssg);
7468         Nlm_ProcessVibrantCallback(&Nlm_currentMssg);
7469         DispatchMessage (&Nlm_currentMssg);
7470       }
7471       Nlm_RemoveDyingWindows ();
7472     }
7473   }
7474 #endif
7475 #ifdef WIN_MOTIF
7476   XEvent       event;
7477   XtInputMask  mask;
7478 
7479   while (! quitProgram) {
7480     mask = XtAppPending (Nlm_appContext);
7481     if (mask != 0) {
7482       while (Nlm_processUpdatesFirstVal &&
7483              Nlm_XCheckUpdateEvent(&event)) {
7484         XtDispatchEvent (&event);
7485       }
7486     }
7487     XtAppNextEvent (Nlm_appContext, &event);
7488     Nlm_ProcessKeyPress (&event);
7489     XtDispatchEvent (&event);
7490     Nlm_RemoveDyingWindows ();
7491   }
7492   if (Nlm_currentXDisplay != NULL) {
7493     XSync (Nlm_currentXDisplay, TRUE);
7494   }
7495 #endif
7496   Nlm_RemoveDyingWindows ();
7497 }
7498 
7499 extern Nlm_Boolean Nlm_EventAvail (void)
7500 
7501 {
7502 #ifdef WIN_MAC
7503   return (EventAvail (everyEvent, &Nlm_currentEvent));
7504 #endif
7505 #ifdef WIN_MSWIN
7506   return (Nlm_Boolean) (PeekMessage (&Nlm_currentMssg, NULL, 0, 0, PM_NOREMOVE | PM_NOYIELD));
7507 #endif
7508 #ifdef WIN_MOTIF
7509   XtInputMask  mask;
7510   mask = XtAppPending (Nlm_appContext);
7511   return ((mask & XtIMXEvent) != 0); /* M.Gouy */
7512 #endif
7513 }
7514 
7515 extern void Nlm_FlushEvents (void)
7516 
7517 {
7518 #ifdef WIN_MAC
7519   FlushEvents (everyEvent - updateMask, 0);
7520 #endif
7521 #ifdef WIN_MSWIN
7522   Nlm_Boolean  goOn;
7523 
7524   goOn = TRUE;
7525   while (goOn) {
7526     goOn = (Nlm_Boolean) PeekMessage (&Nlm_currentMssg, NULL, 0, 0, PM_REMOVE | PM_NOYIELD);
7527     if (goOn && Nlm_currentMssg.message == WM_PAINT) {
7528       goOn = FALSE;
7529     }
7530   }
7531 #endif
7532 #ifdef WIN_MOTIF
7533   if (Nlm_currentXDisplay != NULL) {
7534     XSync (Nlm_currentXDisplay, FALSE);
7535   }
7536 #endif
7537 }
7538 
7539 extern void Nlm_ProcessUpdatesFirst (Nlm_Boolean val)
7540 
7541 {
7542   Nlm_processUpdatesFirstVal = val;
7543 }
7544 
7545 
7546 extern void Nlm_QuitProgram (void)
7547 {
7548   quitProgram = TRUE;
7549 #ifdef WIN_MSWIN
7550   PostQuitMessage (0);
7551 #endif
7552 }
7553 
7554 extern Nlm_Boolean Nlm_QuittingProgram (void)
7555 {
7556   return quitProgram;
7557 }
7558 
7559 
7560 extern void Nlm_UnloadSegment (Nlm_VoidProc routineAddr)
7561 
7562 {
7563 #if defined(WIN_MAC) && !defined(OS_UNIX_DARWIN)
7564   if (routineAddr != NULL) {
7565     UnloadSeg ((void *) routineAddr);
7566   }
7567 #endif
7568 }
7569 
7570 static void Nlm_GetArgsOkProc (Nlm_ButtoN b)
7571 
7572 {
7573   getArgsBoxUp = FALSE;
7574   getArgsOk = TRUE;
7575 }
7576 
7577 static void Nlm_GetArgsCancelProc (Nlm_ButtoN b)
7578 
7579 {
7580   getArgsBoxUp = FALSE;
7581 }
7582 
7583 static Nlm_GraphiC Nlm_GetPrevObject (Nlm_GraphiC g)
7584 
7585 {
7586   Nlm_GraphiC  prev = NULL;
7587   if (g != NULL) {
7588     Nlm_GraphiC prnt = Nlm_GetParent (g);
7589     Nlm_GraphiC chld = Nlm_GetChild (prnt);
7590     while (chld  &&  chld != g) {
7591       prev = chld;
7592       chld = Nlm_GetNext(chld);
7593     }
7594   }
7595   return prev;
7596 }
7597 
7598 static void Nlm_GetArgsFileInProc (Nlm_ButtoN b)
7599 
7600 {
7601   Nlm_Char  path [PATH_MAX];
7602   Nlm_TexT  t;
7603 
7604   t = (Nlm_TexT) Nlm_GetPrevObject ((Nlm_GraphiC) b);
7605   if (t != NULL) {
7606     if (Nlm_GetInputFileName (path, sizeof (path), "", "")) {
7607       Nlm_SetTitle ((Nlm_Handle) t, path);
7608     }
7609   }
7610 }
7611 
7612 static void Nlm_GetArgsFileOutProc (Nlm_ButtoN b)
7613 
7614 {
7615   Nlm_Char  path [PATH_MAX];
7616   Nlm_TexT  t;
7617 
7618   t = (Nlm_TexT) Nlm_GetPrevObject ((Nlm_GraphiC) b);
7619   if (t != NULL) {
7620     if (Nlm_GetOutputFileName (path, sizeof (path), "")) {
7621       Nlm_SetTitle ((Nlm_Handle) t, path);
7622     }
7623   }
7624 }
7625 
7626 
7627 typedef Nlm_Handle PNTR Nlm_HandlePtr;
7628 
7629 static void Nlm_MultiLinePrompt (Nlm_GrouP prnt, Nlm_CharPtr str, Nlm_Int2 width,
7630                                  Nlm_Int2 height, Nlm_FonT fnt, Nlm_Char just)
7631 
7632 {
7633   Nlm_CharPtr    buf;
7634   Nlm_Char       ch;
7635   Nlm_Boolean    go_on;
7636   Nlm_Int2       k;
7637   Nlm_Int2       l;
7638   Nlm_Int2       num;
7639   Nlm_GrouP      pg;
7640 
7641   if (prnt != NULL && str != NULL) {
7642     pg = Nlm_HiddenGroup (prnt, 0, 10, NULL);
7643     Nlm_SetGroupMargins (pg, 1, 1);
7644     Nlm_SetGroupSpacing (pg, 1, 1);
7645     buf = StringSave (str);
7646     k = 0;
7647     num = 0;
7648     go_on = TRUE;
7649     while (Nlm_StringLen (buf + k) > 0 && go_on) {
7650       l = 0;
7651       ch = buf [k + l];
7652       while (ch != '\0' && ch != '\n' && ch != '\r') {
7653         l++;
7654         ch = buf [k + l];
7655       }
7656       num++;
7657       if (num >= 20) {
7658         go_on = FALSE;
7659         Nlm_StaticPrompt (pg, "...", width, height, fnt, just);
7660       } else if (ch == '\n' || ch == '\r') {
7661         buf [k + l] = '\0';
7662         if (Nlm_StringLen (buf + k) > 0) {
7663           Nlm_StaticPrompt (pg, buf + k, width, height, fnt, just);
7664         }
7665         k += l + 1;
7666       } else {
7667         if (Nlm_StringLen (buf + k) > 0) {
7668           Nlm_StaticPrompt (pg, buf + k, width, height, fnt, just);
7669         }
7670         k += l;
7671       }
7672     }
7673     MemFree (buf);
7674   }
7675 }
7676 
7677 
7678 typedef enum
7679 {
7680   FetchArg_SILENT,
7681   FetchArg_ERRPOST,
7682   FetchArg_MESSAGE
7683 }  enumFetchArgDiag;
7684 
7685 static Nlm_Boolean FetchArg(Nlm_ArgPtr arg, const char *str,
7686                             enumFetchArgDiag diag, Nlm_Boolean check_range)
7687 {
7688   static char *xxx = "...";
7689   char *min_str = arg->from ? arg->from : xxx;
7690   char *max_str = arg->to   ? arg->to   : xxx;
7691   Nlm_Boolean valid = TRUE;
7692 
7693   switch ( arg->type )
7694     {
7695     case ARG_BOOLEAN:
7696       {
7697         arg->intvalue =
7698           (str == NULL  ||  *str == '\0'  ||  TO_UPPER(*str) == 'T') ? 1 : 0;
7699         break;
7700       }
7701 
7702     case ARG_INT:
7703       {
7704         long value;
7705         if (str == NULL  ||  sscanf(str, "%ld", &value) != 1)
7706           valid = FALSE;
7707 
7708         if (valid  &&  !check_range) {
7709           arg->intvalue = value;
7710           break;
7711         }
7712 
7713         /* range checking */
7714         if (valid  &&  arg->from != NULL)
7715           {
7716             long min_value;
7717             if (sscanf(arg->from, "%ld", &min_value) != 1  ||
7718                 value < min_value)
7719               valid = FALSE;
7720           }
7721         if (valid  &&  arg->to != NULL)
7722           {
7723             long max_value;
7724             if (sscanf(arg->to,   "%ld", &max_value) != 1  ||
7725                 value > max_value)
7726               valid = FALSE;
7727           }
7728 
7729         if ( valid )
7730           arg->intvalue = value;
7731         break;
7732       }
7733 
7734     case ARG_FLOAT:
7735       {
7736         double value;
7737         if (str == NULL  ||  sscanf(str, "%lf", &value) != 1)
7738           valid = FALSE;
7739 
7740         if (valid  &&  !check_range) {
7741           arg->floatvalue = value;
7742           break;
7743         }
7744 
7745         /* range checking */
7746         if (valid  &&  arg->from != NULL)
7747           {
7748             double min_value;
7749             if (sscanf(arg->from, "%lg", &min_value) != 1  ||
7750                 value < min_value)
7751               valid = FALSE;
7752           }
7753         if (valid  &&  arg->to != NULL)
7754           {
7755             double max_value;
7756             if (sscanf(arg->to,   "%lg", &max_value) != 1  ||
7757                 value > max_value)
7758               valid = FALSE;
7759           }
7760 
7761         if ( valid )
7762           arg->floatvalue = value;
7763         break;
7764       }
7765 
7766     case ARG_STRING:
7767     case ARG_FILE_IN:
7768     case ARG_FILE_OUT:
7769     case ARG_DATA_IN:
7770     case ARG_DATA_OUT:
7771       if (str == NULL  ||  *str == '\0')
7772         arg->strvalue = NULL;
7773       else
7774         arg->strvalue = StringSave( str );
7775       break;
7776 
7777     default:
7778       valid = FALSE;
7779     }
7780 
7781   if ( !valid )
7782     {
7783       switch ( diag )
7784         {
7785         case FetchArg_SILENT:
7786           break;
7787         case FetchArg_ERRPOST:
7788           ErrPostEx(SEV_ERROR, 0,0,
7789                     "%s ::  '%s' is bad or out of range [%s to %s]",
7790                     arg->prompt, str, min_str, max_str);
7791           break;
7792         case FetchArg_MESSAGE:
7793           Message(MSG_ERROR,
7794                   "%s ::  '%s' is bad or out of range [%s to %s]",
7795                   arg->prompt, str, min_str, max_str);
7796           break;
7797         }
7798       return FALSE;
7799     }
7800 
7801   return TRUE;
7802 }
7803 
7804 
7805 static Nlm_Int2 NumReturnsIn (Nlm_CharPtr prompt)
7806 
7807 {
7808   Nlm_Char  ch;
7809   Nlm_Int2  count = 0;
7810 
7811   if (StringHasNoText (prompt)) return 0;
7812 
7813   ch = *prompt;
7814   while (ch != '\0') {
7815     if (ch == '\r' || ch == '\n') {
7816       count++;
7817     }
7818     prompt++;
7819     ch = *prompt;
7820   }
7821 
7822   return count;
7823 }
7824 
7825 
7826 static Nlm_Boolean GetArgs_ST(const char* progname,
7827                               Nlm_Int2 numargs, Nlm_ArgPtr ap,
7828                               Nlm_Boolean silent)
7829 {
7830   static const char* s_TypeStrings[] = {
7831     "",
7832     "",
7833     "Integer: ",
7834     "Float: ",
7835     "String: ",
7836     "File In: ",
7837     "File Out: ",
7838     "Data In: ",
7839     "Data Out: "
7840   };
7841 
7842 #ifdef WIN_MOTIF
7843 static const char* s_ValueStrings[] = {
7844     "",
7845     "T/F",
7846     "Integer",
7847     "Float",
7848     "String",
7849     "Input-File",
7850     "Output-File",
7851     "Input-Data",
7852     "Output-Data"
7853   };
7854 #endif
7855 
7856   Nlm_Int2       i, j;
7857   Nlm_ArgPtr     curarg;
7858   Nlm_Boolean   *resolved;
7859   Nlm_Char       arg[512];
7860   Nlm_Int2       delta;
7861   Nlm_GrouP      g;
7862   Nlm_GrouP      h;
7863   Nlm_HandlePtr  hp;
7864   Nlm_RecT       r1;
7865   Nlm_RecT       r2;
7866   Nlm_Boolean    new_line;
7867   Nlm_Boolean    largeScreen;
7868   Nlm_Boolean    smallScreen;
7869   Nlm_Char       tag [16];
7870   Nlm_WindoW     w;
7871   Nlm_TexT       firstText;
7872   Nlm_CharPtr    tmp;
7873 
7874   int    xx_argc = (int)Nlm_GetArgc();
7875   char **xx_argv =      Nlm_GetArgv();
7876 
7877   if (ap == NULL  &&  numargs == 0)
7878     return TRUE;
7879 
7880   if (ap == NULL  ||  numargs <= 0)
7881     return FALSE;
7882 
7883   resolved = (Nlm_Boolean*) Nlm_MemNew(numargs * sizeof(Nlm_Boolean));
7884   if ( !resolved )
7885     return FALSE;
7886 
7887   /* Initialize arguments using default value */
7888   for (i = 0, curarg = ap;  i < numargs;  i++, curarg++)
7889     {
7890       curarg->intvalue   = 0;
7891       curarg->floatvalue = 0.0;
7892       curarg->strvalue   = NULL;
7893 
7894       if (curarg->defaultvalue == NULL)
7895         continue;
7896 
7897       switch ( curarg->type )
7898         {
7899         case ARG_BOOLEAN:
7900         case ARG_INT:
7901         case ARG_FLOAT:
7902         case ARG_STRING:
7903         case ARG_FILE_IN:
7904         case ARG_FILE_OUT:
7905         case ARG_DATA_IN:
7906         case ARG_DATA_OUT:
7907           resolved[i] = FetchArg(curarg, curarg->defaultvalue,
7908                                  FetchArg_ERRPOST, FALSE);
7909           break;
7910         default:
7911           ErrPostEx(SEV_ERROR, 0,0, "Invalid Arg->type in %s", curarg->prompt);
7912           Nlm_MemFree( resolved );
7913           return FALSE;
7914       }
7915     }
7916 
7917 
7918   /* Print the usage info */
7919 #ifdef WIN_MOTIF
7920   if (xx_argc == 2
7921       &&  (xx_argv[1][1] == '\0'  ||  !strcmp(xx_argv[1], "--help")))
7922     {
7923       printf("\n%s   arguments:\n\n", progname);
7924       curarg = ap;
7925 
7926       for (i = 0, j = 0;  i < numargs;  i++, j++, curarg++)
7927         {
7928           printf("  -%c  %s [%s]",
7929                  curarg->tag, curarg->prompt, s_ValueStrings[curarg->type]);
7930           if (curarg->optional )
7931             printf( "  Optional" );
7932           printf("\n");
7933           if (curarg->defaultvalue != NULL)
7934             printf("    default = %s\n", curarg->defaultvalue);
7935           if (curarg->from != NULL  ||  curarg->to != NULL)
7936             {
7937               if (curarg->type == ARG_DATA_IN  || curarg->type == ARG_DATA_OUT)
7938                 printf("    Data Type = %s\n",
7939                        (curarg->from ? curarg->from : "<NULL>"));
7940               else
7941                 printf("    range from %s to %s\n",
7942                        (curarg->from ? curarg->from : "<NULL>"),
7943                        (curarg->to   ? curarg->to   : "<NULL>"));
7944             }
7945         }
7946 
7947       printf("\n");
7948       Nlm_MemFree( resolved );
7949       return FALSE;
7950     }
7951 #endif
7952 
7953 
7954   /* Parse command-line arguments */
7955   for (i = 1;  i < xx_argc;  i++)
7956     {
7957       Nlm_CharPtr arg_str = xx_argv[i];
7958 
7959 #ifdef WIN_MAC
7960       if (Nlm_StringCmp (arg_str, "-NSDocumentRevisionsDebugMode") == 0) {
7961         ++i;
7962         if (i < xx_argc) {
7963           arg_str = xx_argv[i];
7964           if (Nlm_StringCmp (arg_str, "YES") == 0) continue;
7965           if (Nlm_StringCmp (arg_str, "NO") == 0) continue;
7966         }
7967       }
7968 #endif
7969 
7970       if (*arg_str != '-') {
7971         ErrPostEx(SEV_ERROR, 0, 0,
7972                   "\n%s\n(offending argument #%d was: '%s')",
7973                   "Vibrant command line arguments must start with '-'",
7974                   (int)i, arg_str);
7975         Nlm_MemFree( resolved );
7976         return FALSE;
7977       }
7978 
7979       arg_str++;
7980       curarg = ap;
7981       for (j = 0;  j < numargs  &&  *arg_str != curarg->tag;  j++, curarg++) continue;
7982       if (j == numargs) {
7983         ErrPostEx(SEV_ERROR, 0, 0, "Invalid argument: %s", xx_argv[i]);
7984         Nlm_MemFree( resolved );
7985         return FALSE;
7986       }
7987 
7988       arg_str++;
7989       if (*arg_str == '\0'  &&  curarg->type != ARG_BOOLEAN)
7990         {
7991           if (++i == xx_argc)  {
7992             ErrPostEx(SEV_ERROR, 0, 0,
7993                       "No argument given for %s", curarg->prompt);
7994             Nlm_MemFree( resolved );
7995             return FALSE;
7996           }
7997           arg_str = xx_argv[i];
7998           ASSERT( arg_str  &&  *arg_str );
7999       }
8000 
8001       if ( !FetchArg(curarg, arg_str, FetchArg_ERRPOST, TRUE) )
8002         {
8003           Nlm_MemFree( resolved );
8004           return FALSE;
8005         }
8006       resolved[j] = TRUE;
8007     }
8008 
8009 /*
8010   silent = (Nlm_Boolean)(silent  ||  xx_argc > 1);
8011 */
8012 
8013   if ( silent )
8014     {
8015       for (i = 0;
8016            i < numargs  &&  (ap[i].optional  ||  resolved[i]);
8017            i++) continue;
8018       if (i == numargs) {
8019         Nlm_MemFree( resolved );
8020         return TRUE; /* all args resolved from command line or by default */
8021       }
8022     }
8023 
8024 
8025   /* Arg-Query Dialog Box */
8026   w = Nlm_FixedWindow (-50, -33, -10, -10, (Nlm_CharPtr) progname, NULL);
8027   smallScreen = FALSE;
8028   largeScreen = FALSE;
8029 #ifdef WIN_MAC
8030   if (Nlm_screenRect.right < 513  ||  Nlm_screenRect.bottom < 343) {
8031     smallScreen = TRUE;
8032   }
8033   if (Nlm_screenRect.right > 1200  &&  Nlm_screenRect.bottom > 800) {
8034     largeScreen = TRUE;
8035   }
8036 #endif
8037 #ifdef WIN_MSWIN
8038   if (Nlm_screenRect.bottom < 352) {
8039     smallScreen = TRUE;
8040   }
8041   if (Nlm_screenRect.bottom > 800) {
8042     largeScreen = TRUE;
8043   }
8044 #endif
8045   g = Nlm_HiddenGroup (w, 5, 0, NULL);
8046   hp = (Nlm_HandlePtr) Nlm_MemNew (numargs * sizeof (Nlm_Handle));
8047 
8048   firstText = NULL;
8049   curarg = ap;
8050   for (i = 0, j = 0; i < numargs; i++, curarg++) {
8051     new_line = FALSE;
8052     if (smallScreen) {
8053       if (j >= 10) {
8054         new_line = TRUE;
8055       }
8056     } else if (largeScreen) {
8057       if (j >= 30) {
8058         new_line = TRUE;
8059       }
8060     } else {
8061       if (j >= 15) {
8062         new_line = TRUE;
8063       }
8064     }
8065     if (new_line) {
8066       j = 0;
8067       Nlm_Advance (w);
8068       g = Nlm_HiddenGroup (w, 5, 0, NULL);
8069     }
8070     j++;
8071     j += NumReturnsIn ((Nlm_CharPtr) curarg->prompt);
8072 
8073     Nlm_StaticPrompt(g, (char*)s_TypeStrings[curarg->type], 0,
8074                      Nlm_dialogTextHeight, Nlm_systemFont, 'l');
8075 
8076 
8077     tag [0] = '\0';
8078     if (Nlm_showGetArgTag) {
8079       sprintf (tag, " -%c", (char) curarg->tag);
8080     }
8081 
8082     /* Populate the Arg-Query Dialog Box's input controls and
8083        initialize these by default values */
8084 
8085     switch (curarg->type) {
8086       case ARG_BOOLEAN:
8087         hp[i] = (Nlm_Handle)
8088           Nlm_CheckBox(g, (Nlm_CharPtr) curarg->prompt, NULL);
8089         Nlm_StaticPrompt(g, tag, 0, Nlm_dialogTextHeight, Nlm_systemFont, 'l');
8090         Nlm_StaticPrompt (g, "", 0, 0, Nlm_systemFont, 'l');
8091         if (curarg->intvalue == 1) {
8092           Nlm_SetStatus (hp [i], TRUE);
8093         }
8094         break;
8095       case ARG_INT:
8096       case ARG_FLOAT:
8097       case ARG_STRING:
8098       case ARG_FILE_IN:
8099       case ARG_FILE_OUT:
8100       case ARG_DATA_IN:
8101       case ARG_DATA_OUT:
8102         Nlm_MultiLinePrompt (g, (Nlm_CharPtr) curarg->prompt, 0, Nlm_dialogTextHeight, Nlm_systemFont, 'l');
8103         Nlm_StaticPrompt(g, tag, 0, Nlm_dialogTextHeight, Nlm_systemFont, 'l');
8104         hp[i] = (Nlm_Handle) Nlm_DialogText(g, (Nlm_CharPtr) curarg->defaultvalue, 10, NULL);
8105         if (firstText == NULL) {
8106           firstText = (Nlm_TexT) hp[i];
8107         }
8108         break;
8109     }
8110 
8111     arg[0] = '\0';
8112     if ((curarg->from != NULL) || (curarg->to != NULL)) {
8113       tmp = Nlm_StringMove(arg, " (");
8114       tmp = Nlm_StringMove(tmp, ((curarg->from) != NULL ? (curarg->from): "<NULL>"));
8115       if ((curarg->type == ARG_INT) || (curarg->type == ARG_FLOAT)) {
8116         tmp = Nlm_StringMove(tmp, " to ");
8117         tmp = Nlm_StringMove(tmp, ((curarg->to) != NULL ? ( curarg->to): "<NULL>"));
8118       }
8119       Nlm_StringMove(tmp, ")");
8120     }
8121 
8122     if (curarg->type == ARG_FILE_IN) {
8123       Nlm_PushButton (g, "File IN", Nlm_GetArgsFileInProc);
8124     } else if (curarg->type == ARG_FILE_OUT) {
8125       Nlm_PushButton (g, "File OUT", Nlm_GetArgsFileOutProc);
8126     } else {
8127       Nlm_StaticPrompt(g, arg, 0,  Nlm_dialogTextHeight, Nlm_systemFont, 'l');
8128     }
8129   }
8130 
8131   Nlm_Break (w);
8132   h = Nlm_HiddenGroup (w, 10, 0, NULL);
8133   Nlm_PushButton (h, "OK", Nlm_GetArgsOkProc);
8134   Nlm_PushButton (h, "Cancel", Nlm_GetArgsCancelProc);
8135   Nlm_GetRect ((Nlm_GraphiC) g, &r1);
8136   Nlm_DoGetPosition ((Nlm_GraphiC) h, &r2);
8137   delta = (Nlm_Int2)(r1.right - r2.right);
8138   if (delta > 0) {
8139     Nlm_OffsetRect (&r2, (Nlm_Int2)(delta / 2), 0);
8140     Nlm_DoSetPosition ((Nlm_GraphiC) h, &r2, TRUE, FALSE);
8141   }
8142   Nlm_DoShow ((Nlm_GraphiC) w, TRUE, TRUE);
8143   if (firstText != NULL)
8144     Nlm_DoSelect ((Nlm_GraphiC) firstText, TRUE);
8145 
8146   /* Get & handle user input */
8147   getArgsBoxUp = TRUE;
8148   while ( getArgsBoxUp )
8149     {
8150       getArgsOk = FALSE;
8151       while (getArgsBoxUp  &&  !getArgsOk)
8152         Nlm_ProcessEventOrIdle();
8153 
8154       if ( !getArgsOk )
8155         continue;
8156 
8157       curarg = ap;
8158       for (i = 0;  i < numargs  &&  !getArgsBoxUp;  i++, curarg++)
8159         {
8160           resolved[i] = TRUE;
8161           switch ( curarg->type )
8162             {
8163             case ARG_BOOLEAN:
8164               {
8165                 if ( Nlm_GetStatus( hp[i] ) )
8166                   curarg->intvalue = 1;
8167                 else
8168                   curarg->intvalue = 0;
8169                 break;
8170               }
8171 
8172             case ARG_INT:
8173             case ARG_FLOAT:
8174             case ARG_STRING:
8175             case ARG_FILE_IN:
8176             case ARG_FILE_OUT:
8177             case ARG_DATA_IN:
8178             case ARG_DATA_OUT:
8179               {
8180                 Nlm_Char str_value[256];
8181                 Nlm_GetTitle(hp[i], str_value, sizeof(str_value));
8182                 if ( !FetchArg(curarg, str_value, FetchArg_MESSAGE, TRUE) )
8183                   {
8184                     Nlm_DoSelect((Nlm_GraphiC)hp[i], TRUE);
8185                     getArgsBoxUp = TRUE;
8186                   }
8187                 break;
8188               }
8189 
8190             default:
8191               ASSERT ( FALSE );
8192               Nlm_MemFree( resolved );
8193               return FALSE;
8194             }
8195         }
8196 
8197       curarg = ap;
8198       if ( getArgsBoxUp )
8199         continue;
8200 
8201       for (i = 0;  i < numargs;  i++, curarg++)
8202         if (!curarg->optional  &&  !resolved[i])
8203           {
8204             Message(MSG_ERROR, "%s was not given an argument", curarg->prompt);
8205             getArgsBoxUp = TRUE;
8206             Nlm_DoSelect((Nlm_GraphiC)hp[i], TRUE);
8207             break;
8208           }
8209     }
8210   Nlm_DoRemove ((Nlm_GraphiC) w, TRUE);
8211   Nlm_Update ();
8212 
8213   if (getArgsOk) {  /* leave up a little window with the program name */
8214     w = Nlm_FixedWindow (-50, -33, -10, -10, (Nlm_CharPtr) progname, NULL);
8215     Nlm_DoShow ((Nlm_GraphiC) w, TRUE, TRUE);
8216   }
8217 
8218   Nlm_MemFree( resolved );
8219   return getArgsOk;
8220 }
8221 
8222 
8223 extern Nlm_Boolean Nlm_GetArgs(const char* progname,
8224                                Nlm_Int2 numargs, Nlm_ArgPtr ap)
8225 {
8226   return GetArgs_ST(progname, numargs, ap, FALSE);
8227 }
8228 
8229 
8230 extern Nlm_Boolean Nlm_GetArgsSilent(const char* progname,
8231                                      Nlm_Int2 numargs, Nlm_ArgPtr ap)
8232 {
8233   return GetArgs_ST(progname, numargs, ap, TRUE);
8234 }
8235 
8236 
8237 extern void Nlm_FreeWindows (void)
8238 {
8239   gphprcsptr = (Nlm_GphPrcsPtr) Nlm_MemFree (gphprcsptr);
8240 }
8241 
8242 extern void Nlm_InitWindows (void)
8243 {
8244   gphprcsptr = (Nlm_GphPrcsPtr) Nlm_MemNew (sizeof (Nlm_GphPrcs) * 12);
8245 
8246   documentProcs = &(gphprcsptr [0]);
8247 #ifdef WIN_MAC
8248   documentProcs->click = Nlm_DocumentClick;
8249   documentProcs->key = Nlm_NormalKey;
8250   documentProcs->draw = Nlm_DocumentDraw;
8251   documentProcs->idle = Nlm_NormalIdle;
8252 #endif
8253   documentProcs->show = Nlm_ShowDocument;
8254   documentProcs->hide = Nlm_HideWindow;
8255   documentProcs->activate = Nlm_DocumentActivate;
8256   documentProcs->deactivate = Nlm_DocumentDeactivate;
8257   documentProcs->remove = Nlm_RemoveWindow;
8258   documentProcs->select = Nlm_NormalSelect;
8259   documentProcs->linkIn = Nlm_LinkIn;
8260   documentProcs->adjustPrnt = Nlm_AdjustWindow;
8261   documentProcs->setTitle = Nlm_SetWindowTitle;
8262   documentProcs->getTitle = Nlm_GetWindowTitle;
8263   documentProcs->setPosition = Nlm_SetWindowPosition;
8264   documentProcs->getPosition = Nlm_GetWindowPosition;
8265   documentProcs->gainFocus = Nlm_WindowGainFocus;
8266   documentProcs->loseFocus = Nlm_WindowLoseFocus;
8267   documentProcs->sendChar = Nlm_NormalDrawChar;
8268 
8269   fixedProcs = &(gphprcsptr [1]);
8270 #ifdef WIN_MAC
8271   fixedProcs->click = Nlm_DialogClick;
8272   fixedProcs->key = Nlm_NormalKey;
8273   fixedProcs->draw = Nlm_NormalDraw;
8274   fixedProcs->idle = Nlm_NormalIdle;
8275 #endif
8276   fixedProcs->show = Nlm_ShowNormal;
8277   fixedProcs->hide = Nlm_HideWindow;
8278   fixedProcs->activate = Nlm_NormalActivate;
8279   fixedProcs->deactivate = Nlm_NormalDeactivate;
8280   fixedProcs->remove = Nlm_RemoveWindow;
8281   fixedProcs->select = Nlm_NormalSelect;
8282   fixedProcs->countItems = NULL;
8283   fixedProcs->linkIn = Nlm_LinkIn;
8284   fixedProcs->adjustPrnt = Nlm_AdjustWindow;
8285   fixedProcs->setTitle = Nlm_SetWindowTitle;
8286   fixedProcs->getTitle = Nlm_GetWindowTitle;
8287   fixedProcs->setPosition = Nlm_SetWindowPosition;
8288   fixedProcs->getPosition = Nlm_GetWindowPosition;
8289   fixedProcs->gainFocus = Nlm_WindowGainFocus;
8290   fixedProcs->loseFocus = Nlm_WindowLoseFocus;
8291   fixedProcs->sendChar = Nlm_NormalDrawChar;
8292 
8293   frozenProcs = &(gphprcsptr [2]);
8294 #ifdef WIN_MAC
8295   frozenProcs->click = Nlm_FrozenClick;
8296   frozenProcs->key = Nlm_NormalKey;
8297   frozenProcs->draw = Nlm_NormalDraw;
8298   frozenProcs->idle = Nlm_NormalIdle;
8299 #endif
8300   frozenProcs->show = Nlm_ShowNormal;
8301   frozenProcs->hide = Nlm_HideWindow;
8302   frozenProcs->activate = Nlm_NormalActivate;
8303   frozenProcs->deactivate = Nlm_NormalDeactivate;
8304   frozenProcs->remove = Nlm_RemoveWindow;
8305   frozenProcs->select = Nlm_NormalSelect;
8306   frozenProcs->linkIn = Nlm_LinkIn;
8307   frozenProcs->adjustPrnt = Nlm_AdjustWindow;
8308   frozenProcs->setTitle = Nlm_SetWindowTitle;
8309   frozenProcs->getTitle = Nlm_GetWindowTitle;
8310   frozenProcs->setPosition = Nlm_SetWindowPosition;
8311   frozenProcs->getPosition = Nlm_GetWindowPosition;
8312   frozenProcs->gainFocus = Nlm_WindowGainFocus;
8313   frozenProcs->loseFocus = Nlm_WindowLoseFocus;
8314   frozenProcs->sendChar = Nlm_NormalDrawChar;
8315 
8316   roundProcs = &(gphprcsptr [3]);
8317 #ifdef WIN_MAC
8318   roundProcs->click = Nlm_DialogClick;
8319   roundProcs->key = Nlm_NormalKey;
8320   roundProcs->draw = Nlm_NormalDraw;
8321   roundProcs->idle = Nlm_NormalIdle;
8322 #endif
8323   roundProcs->show = Nlm_ShowNormal;
8324   roundProcs->hide = Nlm_HideWindow;
8325   roundProcs->activate = Nlm_NormalActivate;
8326   roundProcs->deactivate = Nlm_NormalDeactivate;
8327   roundProcs->remove = Nlm_RemoveWindow;
8328   roundProcs->select = Nlm_NormalSelect;
8329   roundProcs->linkIn = Nlm_LinkIn;
8330   roundProcs->adjustPrnt = Nlm_AdjustWindow;
8331   roundProcs->setTitle = Nlm_SetWindowTitle;
8332   roundProcs->getTitle = Nlm_GetWindowTitle;
8333   roundProcs->setPosition = Nlm_SetWindowPosition;
8334   roundProcs->getPosition = Nlm_GetWindowPosition;
8335   roundProcs->gainFocus = Nlm_WindowGainFocus;
8336   roundProcs->loseFocus = Nlm_WindowLoseFocus;
8337   roundProcs->sendChar = Nlm_NormalDrawChar;
8338 
8339   alertProcs = &(gphprcsptr [4]);
8340 #ifdef WIN_MAC
8341   alertProcs->click = Nlm_PlainClick;
8342   alertProcs->key = Nlm_NormalKey;
8343   alertProcs->draw = Nlm_NormalDraw;
8344   alertProcs->idle = Nlm_NormalIdle;
8345 #endif
8346   alertProcs->show = Nlm_ShowNormal;
8347   alertProcs->hide = Nlm_HideWindow;
8348   alertProcs->activate = Nlm_NormalActivate;
8349   alertProcs->deactivate = Nlm_NormalDeactivate;
8350   alertProcs->remove = Nlm_RemoveWindow;
8351   alertProcs->select = Nlm_PlainSelect;
8352   alertProcs->linkIn = Nlm_LinkIn;
8353   alertProcs->adjustPrnt = Nlm_AdjustWindow;
8354   alertProcs->setTitle = Nlm_SetWindowTitle;
8355   alertProcs->getTitle = Nlm_GetWindowTitle;
8356   alertProcs->setPosition = Nlm_SetWindowPosition;
8357   alertProcs->getPosition = Nlm_GetWindowPosition;
8358   alertProcs->gainFocus = Nlm_WindowGainFocus;
8359   alertProcs->loseFocus = Nlm_WindowLoseFocus;
8360   alertProcs->sendChar = Nlm_NormalDrawChar;
8361 
8362   modalProcs = &(gphprcsptr [5]);
8363 #ifdef WIN_MAC
8364   modalProcs->click = Nlm_ModalClick;
8365   modalProcs->key = Nlm_ModalKey;
8366   modalProcs->draw = Nlm_NormalDraw;
8367   modalProcs->idle = Nlm_NormalIdle;
8368 #endif
8369   modalProcs->show = Nlm_ShowModal;
8370   modalProcs->hide = Nlm_HideModal;
8371   modalProcs->activate = Nlm_NormalActivate;
8372   modalProcs->deactivate = Nlm_NormalDeactivate;
8373   modalProcs->remove = Nlm_RemoveWindow;
8374   modalProcs->select = Nlm_PlainSelect;
8375   modalProcs->linkIn = Nlm_LinkIn;
8376   modalProcs->adjustPrnt = Nlm_AdjustWindow;
8377   modalProcs->setTitle = Nlm_SetWindowTitle;
8378   modalProcs->getTitle = Nlm_GetWindowTitle;
8379   modalProcs->setPosition = Nlm_SetWindowPosition;
8380   modalProcs->getPosition = Nlm_GetWindowPosition;
8381   modalProcs->gainFocus = Nlm_WindowGainFocus;
8382   modalProcs->loseFocus = Nlm_WindowLoseFocus;
8383   modalProcs->sendChar = Nlm_NormalDrawChar;
8384 
8385   movableModalProcs = &(gphprcsptr [6]);
8386 #ifdef WIN_MAC
8387   movableModalProcs->click = Nlm_MovableModalClick;
8388   movableModalProcs->key = Nlm_ModalKey;
8389   movableModalProcs->draw = Nlm_NormalDraw;
8390   movableModalProcs->idle = Nlm_NormalIdle;
8391 #endif
8392   movableModalProcs->show = Nlm_ShowModal;
8393   movableModalProcs->hide = Nlm_HideModal;
8394   movableModalProcs->activate = Nlm_NormalActivate;
8395   movableModalProcs->deactivate = Nlm_NormalDeactivate;
8396   movableModalProcs->remove = Nlm_RemoveWindow;
8397   movableModalProcs->select = Nlm_PlainSelect;
8398   movableModalProcs->linkIn = Nlm_LinkIn;
8399   movableModalProcs->adjustPrnt = Nlm_AdjustWindow;
8400   movableModalProcs->setTitle = Nlm_SetWindowTitle;
8401   movableModalProcs->getTitle = Nlm_GetWindowTitle;
8402   movableModalProcs->setPosition = Nlm_SetWindowPosition;
8403   movableModalProcs->getPosition = Nlm_GetWindowPosition;
8404   movableModalProcs->gainFocus = Nlm_WindowGainFocus;
8405   movableModalProcs->loseFocus = Nlm_WindowLoseFocus;
8406   movableModalProcs->sendChar = Nlm_NormalDrawChar;
8407 
8408   floatingProcs = &(gphprcsptr [7]);
8409 #ifdef WIN_MAC
8410   floatingProcs->click = Nlm_FloatingClick;
8411   floatingProcs->key = Nlm_FloatingKey;
8412   floatingProcs->draw = Nlm_NormalDraw;
8413   floatingProcs->idle = Nlm_FloatingIdle;
8414 #endif
8415   floatingProcs->show = Nlm_ShowNormal;
8416   floatingProcs->hide = Nlm_HideWindow;
8417   floatingProcs->activate = Nlm_NormalActivate;
8418   floatingProcs->deactivate = Nlm_NormalDeactivate;
8419   floatingProcs->remove = Nlm_RemoveWindow;
8420   floatingProcs->select = Nlm_FloatingSelect;
8421   floatingProcs->linkIn = Nlm_LinkIn;
8422   floatingProcs->adjustPrnt = Nlm_AdjustWindow;
8423   floatingProcs->setTitle = Nlm_SetWindowTitle;
8424   floatingProcs->getTitle = Nlm_GetWindowTitle;
8425   floatingProcs->setPosition = Nlm_SetWindowPosition;
8426   floatingProcs->getPosition = Nlm_GetWindowPosition;
8427   floatingProcs->gainFocus = Nlm_WindowGainFocus;
8428   floatingProcs->loseFocus = Nlm_WindowLoseFocus;
8429   floatingProcs->sendChar = Nlm_FloatingDrawChar;
8430 
8431   shadowProcs = &(gphprcsptr [8]);
8432 #ifdef WIN_MAC
8433   shadowProcs->click = Nlm_PlainClick;
8434   shadowProcs->key = Nlm_NormalKey;
8435   shadowProcs->draw = Nlm_NormalDraw;
8436   shadowProcs->idle = Nlm_NormalIdle;
8437 #endif
8438   shadowProcs->show = Nlm_ShowNormal;
8439   shadowProcs->hide = Nlm_HideWindow;
8440   shadowProcs->activate = Nlm_NormalActivate;
8441   shadowProcs->deactivate = Nlm_NormalDeactivate;
8442   shadowProcs->remove = Nlm_RemoveWindow;
8443   shadowProcs->select = Nlm_PlainSelect;
8444   shadowProcs->linkIn = Nlm_LinkIn;
8445   shadowProcs->adjustPrnt = Nlm_AdjustWindow;
8446   shadowProcs->setTitle = Nlm_SetWindowTitle;
8447   shadowProcs->getTitle = Nlm_GetWindowTitle;
8448   shadowProcs->setPosition = Nlm_SetWindowPosition;
8449   shadowProcs->getPosition = Nlm_GetWindowPosition;
8450   shadowProcs->gainFocus = Nlm_WindowGainFocus;
8451   shadowProcs->loseFocus = Nlm_WindowLoseFocus;
8452   shadowProcs->sendChar = Nlm_NormalDrawChar;
8453 
8454   plainProcs = &(gphprcsptr [9]);
8455 #ifdef WIN_MAC
8456   plainProcs->click = Nlm_PlainClick;
8457   plainProcs->key = Nlm_NormalKey;
8458   plainProcs->draw = Nlm_NormalDraw;
8459   plainProcs->idle = Nlm_NormalIdle;
8460 #endif
8461   plainProcs->show = Nlm_ShowNormal;
8462   plainProcs->hide = Nlm_HideWindow;
8463   plainProcs->activate = Nlm_NormalActivate;
8464   plainProcs->deactivate = Nlm_NormalDeactivate;
8465   plainProcs->remove = Nlm_RemoveWindow;
8466   plainProcs->select = Nlm_PlainSelect;
8467   plainProcs->linkIn = Nlm_LinkIn;
8468   plainProcs->adjustPrnt = Nlm_AdjustWindow;
8469   plainProcs->setTitle = Nlm_SetWindowTitle;
8470   plainProcs->getTitle = Nlm_GetWindowTitle;
8471   plainProcs->setPosition = Nlm_SetWindowPosition;
8472   plainProcs->getPosition = Nlm_GetWindowPosition;
8473   plainProcs->gainFocus = Nlm_WindowGainFocus;
8474   plainProcs->loseFocus = Nlm_WindowLoseFocus;
8475   plainProcs->sendChar = Nlm_NormalDrawChar;
8476 
8477   desktopProcs = &(gphprcsptr [10]);
8478 #ifdef WIN_MAC
8479   desktopProcs->click = Nlm_DesktopClick;
8480   desktopProcs->key = Nlm_DesktopKey;
8481 #endif
8482   desktopProcs->show = Nlm_ShowNormal;
8483   desktopProcs->hide = Nlm_HideWindow;
8484   desktopProcs->remove = Nlm_RemoveWindow;
8485   desktopProcs->select = Nlm_DesktopSelect;
8486   desktopProcs->linkIn = Nlm_LinkIn;
8487 
8488   systemProcs = &(gphprcsptr [11]);
8489 #ifdef WIN_MAC
8490   systemProcs->click = Nlm_SystemClick;
8491   systemProcs->draw = Nlm_NormalDraw;
8492 #endif
8493   systemProcs->show = Nlm_ShowNormal;
8494   systemProcs->hide = Nlm_HideWindow;
8495   systemProcs->remove = Nlm_RemoveWindow;
8496   systemProcs->select = Nlm_NormalSelect;
8497   systemProcs->linkIn = Nlm_LinkIn;
8498 }
8499 
8500 #ifdef WIN_MOTIF
8501 /**********************************************************/
8502 static void WindowStructFocusCallbackEx(Widget wd, XtPointer client_data,
8503                                         XEvent *event, Boolean *contin)
8504 {
8505     Nlm_WindowData wdata;
8506     Nlm_RectTool   rtool;
8507     Nlm_WindoW     w;
8508     Nlm_RecT       r;
8509     Window         xw;
8510     Window         root;
8511     Window         parent;
8512     Window         child;
8513     Window         *children = NULL;
8514     unsigned int   nchildren;
8515     int            x;
8516     int            y;
8517 
8518     if(event->type != ConfigureNotify)
8519     {
8520         WindowStructFocusCallback(wd, client_data, event, contin);
8521         return;
8522     }
8523 
8524     XSetErrorHandler(ignoreXerror_handler);
8525     for(xw = XtWindow(wd); xw; xw = parent)
8526     {
8527         if(!XQueryTree(Nlm_currentXDisplay, xw, &root, &parent, &children,
8528                        &nchildren) || root == parent)
8529             break;
8530         XFree((char *) children);
8531         children = NULL;
8532     }
8533     XSetErrorHandler(defaultXerror_handler);
8534     if(children != NULL)
8535         XFree((char *) children);
8536 
8537     if(root != parent)
8538         return;
8539 
8540     w = (Nlm_WindoW) client_data;
8541 
8542     XTranslateCoordinates(Nlm_currentXDisplay, xw, root, (Position) 0,
8543                           (Position) 0, &x, &y, &child);
8544 
8545     rtool.x = x;
8546     rtool.y = y;
8547     rtool.width = event->xconfigure.width;
8548     rtool.height = event->xconfigure.height;
8549     if(Nlm_GetWindowMenuBar(w) != NULL)
8550         rtool.height -= Nlm_internalMenuBarHeight;
8551 
8552     Nlm_RectToolToRecT(&rtool, &r);
8553     Nlm_SetRect((Nlm_GraphiC) w, &r);
8554     Nlm_GetWindowData(w, &wdata);
8555 
8556     if(wdata.resize != NULL && Nlm_WindowHasBeenShown(w) &&
8557        Nlm_GetVisible((Nlm_GraphiC) w))
8558     {
8559         wdata.resize(w);
8560     }
8561 }
8562 
8563 #endif
8564 
8565 /**********************************************************/
8566 extern void Nlm_SetWindowConfigureCallback(Nlm_WindoW w)
8567 {
8568 #ifdef WIN_MOTIF
8569     Widget wd;
8570 
8571     if(w == NULL)
8572         return;
8573 
8574     wd = (Widget) Nlm_GetWindowShell(w);
8575     XtRemoveEventHandler(wd, (StructureNotifyMask | FocusChangeMask), FALSE,
8576                          WindowStructFocusCallback, (XtPointer) w);
8577     XtAddEventHandler(wd, (StructureNotifyMask | FocusChangeMask), FALSE,
8578                       WindowStructFocusCallbackEx, (XtPointer) w);
8579 #endif
8580     return;
8581 }
8582