1 /*   vibutils.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:  vibutils.c
27 *
28 * Author:  Jonathan Kans
29 *
30 * Version Creation Date:   7/1/91
31 *
32 * $Revision: 6.82 $
33 *
34 * File Description:
35 *       Vibrant miscellaneous functions
36 *
37 * Modifications:
38 * --------------------------------------------------------------------------
39 *
40 * ==========================================================================
41 */
42 
43 #include <ncbilcl.h>
44 
45 #ifdef WIN_MAC
46 #ifdef __MWERKS__
47 # if TARGET_API_MAC_CARBON
48 /* Use non-session APIs of the Carbon Printing Manager, for easy porting */
49 #  include <PMApplication.h>
50 # ifndef OS_UNIX_DARWIN
51 #  include <FullPath.h>
52 #endif
53 # endif
54 # ifdef OS_UNIX_DARWIN
55 #  include <LaunchServices.h>
56 #  include <Files.h>
57 # endif
58 # include <Navigation.h>
59 #endif
60 #endif
61 
62 #include <vibtypes.h>
63 #include <vibprocs.h>
64 #include <vibincld.h>
65 #include <ncbiport.h>
66 
67 
68 #ifdef WIN_MOTIF
69 #include <sys/times.h>
70 #include <limits.h>
71 #endif
72 
73 #ifdef WIN_MAC
74 Nlm_Int2     Nlm_nextIdNumber = 2;
75 #endif
76 #ifdef WIN_MSWIN
77 #include <CommCtrl.h>
78 Nlm_Int2     Nlm_nextIdNumber = 102;
79 #endif
80 #ifdef WIN_MOTIF
81 Nlm_Int2     Nlm_nextIdNumber = 1;
82 #endif
83 
84 Nlm_PoinT    Nlm_globalMouse;
85 Nlm_PoinT    Nlm_localMouse;
86 
87 Nlm_Int2     Nlm_hScrollBarHeight;
88 Nlm_Int2     Nlm_vScrollBarWidth;
89 
90 Nlm_Int2     Nlm_dialogTextHeight;
91 Nlm_Int2     Nlm_popupMenuHeight;
92 
93 Nlm_Char     Nlm_currentKey = '\0';
94 
95 Nlm_Boolean  Nlm_cmmdKey = FALSE;
96 Nlm_Boolean  Nlm_ctrlKey = FALSE;
97 Nlm_Boolean  Nlm_optKey = FALSE;
98 Nlm_Boolean  Nlm_shftKey = FALSE;
99 Nlm_Boolean  Nlm_dblClick = FALSE;
100 
101 #ifndef WIN_MSWIN
102 static Nlm_Boolean      errorBoxUp = FALSE;
103 static Nlm_Int2         errorBoxRsult = 0;
104 #endif
105 
106 static Nlm_GraphiC      recentGraphic = NULL;
107 static Nlm_GraphicData  recentGraphicData;
108 
109 static Nlm_BoX          recentBox = NULL;
110 static Nlm_BoxData      recentBoxData;
111 
112 #ifdef WIN_MAC
113 # if TARGET_API_MAC_CARBON
114 static PMPrintSession  printSession;
115 
116 static PMPageFormat    pageFormat = kPMNoPageFormat;
117 static PMPrintSettings printSettings = kPMNoPrintSettings;
118 static PMPrintContext  thePrintingPort = kPMNoReference;
119 # else
120 static THPrint   prHdl = NULL;
121 static TPPrPort  prPort = NULL;
122 static Nlm_Int2  prerr;
123 static Nlm_Char  fileTypes [32] = {0};
124 # endif
125 #endif
126 
127 #ifdef WIN_MSWIN
128 static Nlm_Boolean   abortPrint;
129 static HDC           hPr = NULL;
130 static int           prerr;
131 /*
132 static int           vibrant_disabled = 0;
133 */
134 static int           disabled_count = 0;
135 static PRINTDLG      pd;
136 static OPENFILENAME  ofn;
137 #endif
138 
139 #ifdef WIN_MOTIF
140 static Widget       fileDialog = NULL;
141 static Nlm_Boolean  fileBoxUp;
142 static Nlm_Boolean  fileBoxRsult;
143 static Nlm_Char     filePath [256];
144 #endif
145 
146 extern Nlm_Boolean Nlm_usesMacNavServices;
147 
Nlm_StrngPrintable(const Nlm_Char PNTR str)148 extern Nlm_CharPtr Nlm_StrngPrintable(const Nlm_Char PNTR str)
149 {
150 #ifdef WIN_MAC
151   Nlm_CharPtr s, x_str, tmp;
152 
153   if (str == NULL) return NULL;
154   tmp = Nlm_StringSave (str);
155   for (s = tmp;  *s;  s++) {
156     if (*s == '\r') {
157       *s = '\n'; /* convert 015 in text object to newline for StringPrintable */
158     }
159   }
160   x_str = Nlm_StringPrintable(tmp, FALSE);
161   Nlm_MemFree (tmp);
162   if ( !x_str )
163     return NULL;
164 
165   for (s = x_str;  *s;  s++) {
166     if (*s == '\n' || *s == '\r') {
167       *s = '\015'; /* convert back for text object */
168     }
169   }
170   return x_str;
171 #else
172   return Nlm_StringPrintable(str, TRUE);
173 #endif
174 }
175 
176 
Nlm_StrngLen(Nlm_CharPtr string)177 extern Nlm_Uint4 Nlm_StrngLen (Nlm_CharPtr string)
178 {
179   Nlm_Uint4  len;
180 
181   len = 0;
182   if (string != NULL) {
183     while (*string != '\0') {
184       string++;
185       len++;
186     }
187   }
188   return len;
189 }
190 
Nlm_StrngCat(Nlm_CharPtr dest,Nlm_CharPtr source,size_t maxsize)191 extern void Nlm_StrngCat (Nlm_CharPtr dest, Nlm_CharPtr source, size_t maxsize)
192 
193 {
194   Nlm_Uint4  count;
195   Nlm_Uint4  i;
196 
197   if (dest != NULL && source != NULL && maxsize > 0) {
198     count = Nlm_StrngLen (source);
199     i = Nlm_StrngLen (dest);
200     if (count + i >= maxsize) {
201       count = maxsize - i - 1;
202     }
203     dest += i;
204     while (count > 0) {
205       *dest = *source;
206       dest++;
207       source++;
208       count--;
209     }
210     *dest = '\0';
211   }
212 }
213 
Nlm_StrngCpy(Nlm_CharPtr dest,Nlm_CharPtr source,size_t maxsize)214 extern void Nlm_StrngCpy (Nlm_CharPtr dest, Nlm_CharPtr source, size_t maxsize)
215 
216 {
217   Nlm_Uint4  count;
218 
219   if (dest != NULL && maxsize > 0) {
220     count = Nlm_StrngLen (source);
221     if (count >= maxsize) {
222       count = maxsize - 1;
223     }
224     while (count > 0) {
225       *dest = *source;
226       dest++;
227       source++;
228       count--;
229     }
230     *dest = '\0';
231   }
232 }
233 
Nlm_InNumber(Nlm_Char ch)234 static Nlm_Boolean Nlm_InNumber (Nlm_Char ch)
235 
236 {
237   return (Nlm_Boolean) (ch >= '0' && ch <= '9') ;
238 }
239 
Nlm_Cap(Nlm_Char ch,Nlm_Boolean caseCounts)240 static Nlm_Char Nlm_Cap (Nlm_Char ch, Nlm_Boolean caseCounts)
241 
242 {
243   if (caseCounts) {
244     return ch;
245   } else if (ch >= 'a' && ch <= 'z') {
246     return (Nlm_Char) (ch - ' ');
247   } else {
248     return ch;
249   }
250 }
251 
Nlm_StrngPos(Nlm_CharPtr str,Nlm_CharPtr sub,Nlm_Uint4 start,Nlm_Boolean caseCounts,Nlm_Uint4Ptr match)252 extern Nlm_Boolean Nlm_StrngPos (Nlm_CharPtr str, Nlm_CharPtr sub,
253                                  Nlm_Uint4 start, Nlm_Boolean caseCounts,
254                                  Nlm_Uint4Ptr match)
255 
256 {
257   Nlm_Char     ch1;
258   Nlm_Char     ch2;
259   Nlm_Uint4    count;
260   Nlm_Boolean  found;
261   Nlm_Uint4    i;
262   Nlm_Uint4    j;
263   Nlm_Uint4    len;
264   Nlm_Uint4    max;
265   Nlm_Uint4    sublen;
266 
267   found = FALSE;
268   if (str != NULL && sub != NULL) {
269     len = Nlm_StrngLen (str);
270     sublen = Nlm_StrngLen (sub);
271     if (len < sublen) {
272       sublen = len;
273     }
274     max = len - sublen;
275     i = start;
276     if (match != NULL) {
277       *match = 0;
278     }
279     if (start < len) {
280       do {
281         found = TRUE;
282         j = 0;
283         count = sublen;
284         while (found && count > 0) {
285           ch1 = Nlm_Cap (str [i + j], caseCounts);
286           ch2 = Nlm_Cap (sub [j], caseCounts);
287           if (ch1 != ch2) {
288             found = FALSE;
289           }
290           j++;
291           count--;
292         }
293         if (found && match != NULL) {
294           *match = i;
295         }
296         i++;
297       } while ((! found) && (i <= max));
298     }
299     else {
300       found = FALSE;
301     }
302   }
303   return found;
304 }
305 
Nlm_StrngSeg(Nlm_CharPtr dest,Nlm_CharPtr source,Nlm_Uint4 start,Nlm_Uint4 length,size_t maxsize)306 extern void Nlm_StrngSeg (Nlm_CharPtr dest, Nlm_CharPtr source,
307                           Nlm_Uint4 start, Nlm_Uint4 length,
308                           size_t maxsize)
309 
310 {
311   Nlm_Uint4  count;
312   Nlm_Uint4  len;
313 
314   if (dest != NULL && source != NULL && maxsize > 0) {
315     count = length;
316     len = Nlm_StrngLen (source);
317     if (start + length > len) {
318       count = len - start;
319     }
320     if (count >= maxsize) {
321       count = maxsize - 1;
322     }
323     source += start;
324     while (count > 0) {
325       *dest = *source;
326       dest++;
327       source++;
328       count--;
329     }
330     *dest = '\0';
331   }
332 }
333 
Nlm_StrngRep(Nlm_CharPtr dest,Nlm_CharPtr source,Nlm_Uint4 start,size_t maxsize)334 extern void Nlm_StrngRep (Nlm_CharPtr dest, Nlm_CharPtr source,
335                           Nlm_Uint4 start, size_t maxsize)
336 
337 {
338   Nlm_Uint4  count;
339 
340   if (dest != NULL && source != NULL && maxsize > 0) {
341     count = Nlm_StrngLen (source);
342     if (count + start >= maxsize) {
343       count = maxsize - start - 1;
344     }
345     dest += start;
346     while (count > 0) {
347       *dest = *source;
348       dest++;
349       source++;
350       count--;
351     }
352   }
353 }
354 
Nlm_StrngEql(Nlm_CharPtr str1,Nlm_CharPtr str2,Nlm_Boolean caseCounts)355 extern Nlm_Boolean Nlm_StrngEql (Nlm_CharPtr str1, Nlm_CharPtr str2,
356                                  Nlm_Boolean caseCounts)
357 
358 {
359   return (Nlm_Boolean) (Nlm_StrngCmp (str1, str2, caseCounts) == 0);
360 }
361 
Nlm_StrngCmp(Nlm_CharPtr str1,Nlm_CharPtr str2,Nlm_Boolean caseCounts)362 extern Nlm_Int2 Nlm_StrngCmp (Nlm_CharPtr str1, Nlm_CharPtr str2,
363                               Nlm_Boolean caseCounts)
364 
365 {
366   Nlm_Uint4  count;
367   Nlm_Uint4  i;
368   Nlm_Uint4  len1;
369   Nlm_Uint4  len2;
370   Nlm_Int2   rsult;
371 
372   rsult = 0;
373   if (str1 != NULL && str2 != NULL) {
374     len1 = Nlm_StrngLen (str1);
375     len2 = Nlm_StrngLen (str2);
376     if (len1 > len2) {
377       count = len2;
378     } else {
379       count = len1;
380     }
381     i = 0;
382     while (count > 0 && Nlm_Cap (str1 [i], caseCounts) == Nlm_Cap (str2 [i], caseCounts)) {
383       i++;
384       count--;
385     }
386     if (count > 0) {
387       if (Nlm_Cap (str1 [i], caseCounts) > Nlm_Cap (str2 [i], caseCounts)) {
388         rsult = 1;
389       } else {
390         rsult = -1;
391       }
392     } else {
393       if (len1 > len2) {
394         rsult = 1;
395       } else if (len1 < len2) {
396         rsult = -1;
397       } else {
398         rsult = 0;
399       }
400     }
401   }
402   return rsult;
403 }
404 
Nlm_SymblCmp(Nlm_CharPtr str1,Nlm_CharPtr str2,Nlm_Boolean caseCounts)405 extern Nlm_Int2 Nlm_SymblCmp (Nlm_CharPtr str1, Nlm_CharPtr str2,
406                               Nlm_Boolean caseCounts)
407 
408 {
409   Nlm_Boolean  cont;
410   Nlm_Boolean  done;
411   Nlm_Uint4    i;
412   Nlm_Uint4    j;
413   Nlm_Uint4    len1;
414   Nlm_Uint4    len2;
415   Nlm_Uint4    num1;
416   Nlm_Uint4    num2;
417   Nlm_Int2     rsult;
418 
419   rsult = 0;
420   if (str1 != NULL && str2 != NULL) {
421     done = FALSE;
422     len1 = Nlm_StrngLen (str1);
423     len2 = Nlm_StrngLen (str2);
424     i = 0;
425     j = 0;
426     cont = (Nlm_Boolean) (len1 > 0 && len2 > 0);
427     while (cont) {
428       if (Nlm_InNumber (str1 [i]) && Nlm_InNumber (str2 [j])) {
429         num1 = 0;
430         while (i < len1 && Nlm_InNumber (str1 [i]) && (num1 < 429496729)) {
431           num1 *= 10;
432           num1 += (str1 [i] - '0');
433           i++;
434         }
435         num2 = 0;
436         while (j < len2 && Nlm_InNumber (str2 [j]) && (num2 < 429496729)) {
437           num2 *= 10;
438           num2 += (str2 [j] - '0');
439           j++;
440         }
441         if (num1 > num2) {
442           rsult = 1;
443           done = TRUE;
444           cont = FALSE;
445         } else if (num1 < num2) {
446           rsult = -1;
447           done = TRUE;
448           cont = FALSE;
449         } else {
450           cont = (Nlm_Boolean) (i < len1 && j < len2);
451         }
452       } else if (Nlm_Cap (str1 [i], caseCounts) > Nlm_Cap (str2 [j], caseCounts)) {
453         rsult = 1;
454         done = TRUE;
455         cont = FALSE;
456       } else if (Nlm_Cap (str1 [i], caseCounts) < Nlm_Cap (str2 [j], caseCounts)) {
457         rsult = -1;
458         done = TRUE;
459         cont = FALSE;
460       } else {
461         i++;
462         j++;
463         cont = (Nlm_Boolean) (i < len1 && j < len2);
464       }
465     }
466     if (done) {
467     } else if (i < len1 && j == len2) {
468       rsult = 1;
469     } else if (i == len1 && j < len2) {
470       rsult = -1;
471     } else {
472       rsult = 0;
473     }
474   }
475   return rsult;
476 }
477 
Nlm_SetString(Nlm_Handle h,Nlm_CharPtr str)478 extern Nlm_Handle Nlm_SetString (Nlm_Handle h, Nlm_CharPtr str)
479 
480 {
481   size_t       len;
482   Nlm_CharPtr  pp;
483 
484   len = 0;
485   if (str != NULL) {
486     len = Nlm_StringLen (str);
487     if (len > 0) {
488       if (h != NULL) {
489         h = Nlm_HandMore (h, len + 1);
490       } else {
491         h = Nlm_HandNew (len + 1);
492       }
493     } else if (h != NULL) {
494       Nlm_HandFree (h);
495       h = NULL;
496     }
497   } else if (h != NULL) {
498     Nlm_HandFree (h);
499     h = NULL;
500   }
501   if (h != NULL) {
502     pp = (Nlm_CharPtr) Nlm_HandLock (h);
503     if (pp != NULL) {
504       Nlm_StringNCpy (pp, str, len + 1); /* remains StringNCpy, not _0 */
505     }
506     Nlm_HandUnlock (h);
507   }
508   return h;
509 }
510 
511 
Nlm_GetString(Nlm_Handle h,Nlm_CharPtr str,size_t maxsize)512 extern void Nlm_GetString (Nlm_Handle h, Nlm_CharPtr str, size_t maxsize)
513 {
514   if (str == NULL  ||  maxsize <= 0)
515     return;
516 
517   str[0] = '\0';
518 
519   if (h == NULL)
520     return;
521 
522   {{
523     Nlm_CharPtr pp = (Nlm_CharPtr)Nlm_HandLock( h );
524     if (pp != NULL)
525       Nlm_StringNCpy_0 (str, pp, maxsize);
526     Nlm_HandUnlock( h );
527   }}
528 }
529 
530 #ifndef WIN_MSWIN
Nlm_MssgErrorProc(Nlm_ButtoN b)531 static void Nlm_MssgErrorProc (Nlm_ButtoN b)
532 {
533   errorBoxUp = FALSE;
534   errorBoxRsult = ANS_NO;
535 }
536 
Nlm_MssgFatalProc(Nlm_ButtoN b)537 static void Nlm_MssgFatalProc (Nlm_ButtoN b)
538 {
539   errorBoxUp = FALSE;
540   errorBoxRsult = ANS_NO;
541 }
542 
Nlm_MssgNoProc(Nlm_ButtoN b)543 static void Nlm_MssgNoProc (Nlm_ButtoN b)
544 {
545   errorBoxUp = FALSE;
546   errorBoxRsult = ANS_NO;
547 }
548 
Nlm_MssgYesProc(Nlm_ButtoN b)549 static void Nlm_MssgYesProc (Nlm_ButtoN b)
550 {
551   errorBoxUp = FALSE;
552   errorBoxRsult = ANS_YES;
553 }
554 
Nlm_MssgOkayProc(Nlm_ButtoN b)555 static void Nlm_MssgOkayProc (Nlm_ButtoN b)
556 {
557   errorBoxUp = FALSE;
558   errorBoxRsult = ANS_OK;
559 }
560 
Nlm_MssgRetryProc(Nlm_ButtoN b)561 static void Nlm_MssgRetryProc (Nlm_ButtoN b)
562 {
563   errorBoxUp = FALSE;
564   errorBoxRsult = ANS_RETRY;
565 }
566 
Nlm_MssgAbortProc(Nlm_ButtoN b)567 static void Nlm_MssgAbortProc (Nlm_ButtoN b)
568 {
569   errorBoxUp = FALSE;
570   errorBoxRsult = ANS_ABORT;
571 }
572 
Nlm_MssgCancelProc(Nlm_ButtoN b)573 static void Nlm_MssgCancelProc (Nlm_ButtoN b)
574 {
575   errorBoxUp = FALSE;
576   errorBoxRsult = ANS_CANCEL;
577 }
578 
Nlm_MssgIgnoreProc(Nlm_ButtoN b)579 static void Nlm_MssgIgnoreProc (Nlm_ButtoN b)
580 {
581   errorBoxUp = FALSE;
582   errorBoxRsult = ANS_IGNORE;
583 }
584 #endif /* ndef WIN_MSWIN */
585 
586 
s_CloseAWindow(Nlm_WindoW w)587 static void s_CloseAWindow(Nlm_WindoW w)
588 {
589   Nlm_Reset((Nlm_TexT) Nlm_GetObjectExtra(w));
590   Nlm_Hide(w);
591 }
592 
593 
594 static Nlm_Boolean s_FatalModalWindowUp;
s_CloseFatalModalWindow_W(Nlm_WindoW w)595 static void s_CloseFatalModalWindow_W(Nlm_WindoW w)
596 {
597   s_FatalModalWindowUp = FALSE;
598 }
599 /*
600 static void s_CloseFatalModalWindow_B(Nlm_ButtoN b)
601 {
602   s_CloseFatalModalWindow_W( Nlm_Parent(b) );
603 }
604 */
605 
606 
Nlm_VibMessageHook(MsgKey key,ErrSev severity,const char * caption,const char * message)607 static MsgAnswer LIBCALLBACK Nlm_VibMessageHook (MsgKey key, ErrSev severity,
608                                                  const char * caption,
609                                                  const char * message)
610 {
611   Nlm_CharPtr  buf = NULL;
612   Nlm_Int2     rsult = 0;
613 #if defined(WIN_MAC) || defined(WIN_MOTIF)
614   size_t       len;
615   Nlm_ButtoN   b[3];
616   Nlm_Char     ch;
617   Nlm_Int2     delta;
618   Nlm_GrouP    g;
619   Nlm_Int2     i;
620   Nlm_Int2     j;
621   Nlm_Int2     k;
622   Nlm_Int2     margin;
623   Nlm_Int2     maxWidth;
624   Nlm_PoinT    npt;
625   Nlm_Int4     percent;
626   Nlm_RecT     r;
627   Nlm_WindoW   w;
628   Nlm_Int2     width;
629 #endif
630 #ifdef WIN_MSWIN
631   Nlm_Int4     answer;
632   Nlm_Int2     wtype;
633 #endif
634 
635   const char* x_caption = caption ? caption : "Message";
636 
637   /* Use a window (or a modal dialog) to accumulate & post the message */
638   if (key == KEY_NONE  ||  severity >= ErrGetFatalLevel()) {
639     static Nlm_WindoW postWindow = 0;
640     static Nlm_TexT   postText   = 0;
641     Nlm_WindoW tempPort = Nlm_CurrentWindow();
642 
643     /* Prepare the message text (concat with the previous text, if any) */
644     char* x_text = NULL;
645     const char* add_mess = message ? message : "<no message posted>";
646     if ( postText ) {
647       size_t curr_len = Nlm_TextLength(postText);
648       size_t add_len  = Nlm_StringLen( message );
649       if (curr_len < 1000000 && add_len < 1000000) {
650         buf = (char*) Nlm_MemNew(add_len+1 + curr_len + 1);
651         Nlm_MemCpy(buf, add_mess, add_len);
652         buf[add_len++] = '\n';
653         Nlm_GetTitle(postText, buf+add_len, curr_len+1);
654         x_text = Nlm_StrngPrintable(buf);
655         Nlm_MemFree(buf);
656       } else {
657         x_text = Nlm_StrngPrintable ("VibMessageHook allocation failure\n");
658       }
659     } else {
660       x_text = Nlm_StrngPrintable(add_mess);
661     }
662 
663     /* Fatal or non-fatal error severity (modal or non-modal dialog) */
664     if (severity >= ErrGetFatalLevel()) {
665       /* Fatal error -- use modal dialog window */
666       Nlm_WindoW modalWindow = Nlm_ModalWindow(-50, -90, -20, -20,
667                                                s_CloseFatalModalWindow_W);
668       Nlm_TexT   modalText = Nlm_ScrollText(modalWindow, 64, 16,
669                                             Nlm_systemFont, TRUE, NULL);
670       Nlm_SetObjectExtra(modalWindow, modalText, NULL);
671       Nlm_SetTextEditable(modalText, FALSE);
672       Nlm_SetTitle(modalText, x_text);
673 
674       /* hide the regular message posting window */
675       if (postWindow  &&  Nlm_Visible(postWindow))
676         Nlm_Hide(postWindow);
677 
678       /* show the modal dialog */
679       Nlm_Show(modalWindow);
680       Nlm_PopupParentWindow(modalWindow);
681       s_FatalModalWindowUp = TRUE;
682       Nlm_WaitForCondition(s_FatalModalWindowUp);
683       Nlm_ProcessAnEvent();
684       Nlm_Remove(modalWindow);
685     } else {
686       /* Non-Fatal error -- use normal window */
687       if ( !postWindow ) {
688         /* create normal message posting window */
689         postWindow = Nlm_FixedWindow(-50, -90, -20, -20, (char*)x_caption,
690                                      s_CloseAWindow);
691         postText = Nlm_ScrollText(postWindow,  (Nlm_Int2)32, (Nlm_Int2)5,
692                                   Nlm_systemFont, TRUE, NULL);
693         Nlm_SetObjectExtra(postWindow, postText, NULL);
694         if ( postText )
695           Nlm_SetTextEditable(postText, FALSE);
696       }
697 
698       /* set the message text and popup the window */
699       if ( postText )
700           Nlm_SetTitle(postText, x_text);
701       Nlm_PopupParentWindow( postWindow );
702     }
703 
704     Nlm_MemFree(x_text);
705     Nlm_RestorePort( tempPort );
706     return (MsgAnswer)ANS_NONE;
707   }
708 
709 #ifdef WIN_MSWIN
710   wtype = MB_OK;
711   switch (key) {
712     case KEY_OK:
713       if (severity == SEV_ERROR) {
714         wtype = MB_OK;
715       } else if (severity == SEV_FATAL) {
716         wtype = MB_OK;
717       } else {
718         wtype = MB_OK;
719       }
720       break;
721     case KEY_RC:
722       wtype = MB_RETRYCANCEL;
723       break;
724     case KEY_ARI:
725       wtype = MB_ABORTRETRYIGNORE;
726       break;
727     case KEY_YN:
728       wtype = MB_YESNO;
729       break;
730     case KEY_YNC:
731       wtype = MB_YESNOCANCEL;
732       break;
733     case KEY_OKC:
734       wtype = MB_OKCANCEL;
735       break;
736     case KEY_NONE:
737       wtype = MB_OK;
738       break;
739     default:
740       break;
741   }
742 
743   answer = MessageBox(NULL, message, x_caption, wtype | MB_TASKMODAL);
744   switch (answer) {
745     case IDNO:
746       rsult = ANS_NO;
747       break;
748     case IDYES:
749       rsult = ANS_YES;
750       break;
751     case IDOK:
752       rsult = ANS_OK;
753       break;
754     case IDRETRY:
755       rsult = ANS_RETRY;
756       break;
757     case IDABORT:
758       rsult = ANS_ABORT;
759       break;
760     case IDCANCEL:
761       rsult = ANS_CANCEL;
762       break;
763     case IDIGNORE:
764       rsult = ANS_IGNORE;
765       break;
766     default:
767       break;
768   }
769 #endif /* WIN_MSWIN */
770 
771 #if defined(WIN_MOTIF) || defined(WIN_MAC)
772   len = MIN(Nlm_StringLen(message) + 1, 4096);
773   buf = (Nlm_CharPtr) Nlm_MemNew(len);
774   Nlm_StringNCpy_0(buf, message, len);
775 
776   percent = (Nlm_Int4)Nlm_GetAppProperty( "VibrantMessageWidthMax" );
777   if (percent <= 0  ||  95 < percent)
778     percent = 95;
779   else if (percent < 25)
780     percent = 25;
781 
782   maxWidth = (Nlm_Int2)(percent *
783                       (Nlm_screenRect.right - Nlm_screenRect.left) / 100 - 40);
784   maxWidth = MAX (maxWidth, 100);
785 
786   w = Nlm_MovableModalWindow (-50, -20, -20, -20, (char*)x_caption, NULL);
787   g = Nlm_HiddenGroup (w, 0, 10, NULL);
788   Nlm_GetNextPosition (g, &npt);
789   npt.x += 6;
790   Nlm_SetNextPosition (g, npt);
791 
792   i = 0;
793   while (buf[i] != '\0') {
794     width = 0;
795     j = 0;
796     while (buf [i + j] == ' ') {
797       i++;
798     }
799     ch = buf [i + j];
800     width += Nlm_CharWidth (ch);
801     while (ch != '\0' && ch != '\n' && ch != '\r' && width <= maxWidth && j < 125) {
802       j++;
803       ch = buf [i + j];
804       width += Nlm_CharWidth (ch);
805     }
806     if (width > maxWidth) {
807       ch = buf [i + j];
808       while (j > 0 && ch != ' ' && ch != '-') {
809         j--;
810         ch = buf [i + j];
811       }
812     } else if (j >= 125) {
813       k = j;
814       ch = buf [i + k];
815       while (k > 0 && ch != ' ' && ch != '-') {
816         k--;
817         ch = buf [i + k];
818       }
819       if (k > 80) {
820         j = k;
821       }
822       ch = buf [i + j];
823     }
824     if (ch == '\n' || ch == '\r') {
825       buf [i + j] = '\0';
826       Nlm_StaticPrompt (g, buf + i, 0, 0, Nlm_systemFont, 'l');
827       i += j + 1;
828     } else {
829       buf [i + j] = '\0';
830       Nlm_StaticPrompt (g, buf + i, 0, 0, Nlm_systemFont, 'l');
831       buf [i + j] = ch;
832       i += j;
833     }
834   }
835   Nlm_MemFree( buf );
836 
837   Nlm_GetPosition (g, &r);
838   margin = r.right;
839   Nlm_Break ((Nlm_GraphiC) w);
840   b [0] = NULL;
841   b [1] = NULL;
842   b [2] = NULL;
843   switch (key) {
844     case KEY_OK:
845       if (severity == SEV_ERROR) {
846         b [0] = Nlm_DefaultButton (w, "OK", Nlm_MssgErrorProc);
847       } else if (severity == SEV_FATAL) {
848         b [0] = Nlm_DefaultButton (w, "OK", Nlm_MssgFatalProc);
849       } else {
850         b [0] = Nlm_DefaultButton (w, "OK", Nlm_MssgOkayProc);
851       }
852       break;
853     case KEY_RC:
854       b [0] = Nlm_PushButton (w, "Retry", Nlm_MssgRetryProc);
855       Nlm_Advance ((Nlm_GraphiC) w);
856       b [1] = Nlm_DefaultButton (w, "Cancel", Nlm_MssgCancelProc);
857       break;
858     case KEY_ARI:
859       b [0] = Nlm_PushButton (w, "Abort", Nlm_MssgAbortProc);
860       Nlm_Advance ((Nlm_GraphiC) w);
861       b [1] = Nlm_PushButton (w, "Retry", Nlm_MssgRetryProc);
862       Nlm_Advance ((Nlm_GraphiC) w);
863       b [2] = Nlm_DefaultButton (w, "Ignore", Nlm_MssgIgnoreProc);
864       break;
865     case KEY_YN:
866       b [0] = Nlm_DefaultButton (w, "Yes", Nlm_MssgYesProc);
867       Nlm_Advance ((Nlm_GraphiC) w);
868       b [1] = Nlm_PushButton (w, "No", Nlm_MssgNoProc);
869       break;
870     case KEY_YNC:
871       b [0] = Nlm_DefaultButton (w, "Yes", Nlm_MssgYesProc);
872       Nlm_Advance ((Nlm_GraphiC) w);
873       b [1] = Nlm_PushButton (w, "No", Nlm_MssgNoProc);
874       Nlm_Advance ((Nlm_GraphiC) w);
875       b [2] = Nlm_PushButton (w, "Cancel", Nlm_MssgCancelProc);
876       break;
877     case KEY_OKC:
878       b [1] = Nlm_DefaultButton (w, "OK", Nlm_MssgOkayProc);
879       Nlm_Advance ((Nlm_GraphiC) w);
880       b [2] = Nlm_PushButton (w, "Cancel", Nlm_MssgCancelProc);
881       break;
882     case KEY_NONE:
883       b [0] = Nlm_DefaultButton (w, "OK", Nlm_MssgOkayProc);
884       break;
885     default:
886       b [0] = Nlm_DefaultButton (w, "OK", Nlm_MssgOkayProc);
887       break;
888   }
889   i = 2;
890   while (i >= 0 && b [i] == NULL) {
891     i--;
892   }
893   if (i >= 0) {
894     Nlm_GetPosition (b [i], &r);
895     delta = (margin - r.right) / 2;
896     if (delta > 0) {
897       while (i >= 0) {
898         Nlm_GetPosition (b [i], &r);
899         Nlm_OffsetRect (&r, delta, 0);
900         Nlm_SetPosition (b [i], &r);
901         i--;
902       }
903     } else if (delta < 0) {
904       Nlm_GetPosition (g, &r);
905       Nlm_OffsetRect (&r, -delta, 0);
906       Nlm_SetPosition (g, &r);
907     }
908   }
909   Nlm_DoShow ((Nlm_GraphiC) w, TRUE, TRUE);
910   errorBoxUp = TRUE;
911   errorBoxRsult = 0;
912 
913 #ifdef WIN_MAC
914   while (errorBoxUp) {
915     Nlm_ProcessExternalEvent ();
916     Nlm_Update ();
917   }
918 #else /* i.e. WIN_MOTIF */
919   Nlm_WaitForCondition(errorBoxUp);
920 #endif
921 
922   Nlm_ProcessAnEvent ();
923   Nlm_DoRemove ((Nlm_GraphiC) w, TRUE);
924   rsult = errorBoxRsult;
925 #endif /* WIN_MOTIF || WIN_MAC */
926 
927   return (MsgAnswer) rsult;
928 }
929 
930 
931 typedef struct monitextra {
932   Nlm_WindoW   wind;
933   Nlm_PaneL    pnl;
934   Nlm_Boolean  cancel;
935 } Nlm_VibMonExtra, PNTR Nlm_VibMonPtr;
936 
Nlm_SelectMonitor(Nlm_WindoW w)937 static void Nlm_SelectMonitor (Nlm_WindoW w)
938 {
939   Nlm_WindowTool wptr = Nlm_ParentWindowPtr ((Nlm_GraphiC) w);
940 #ifdef WIN_MAC
941   /* SelectWindow (wptr); */
942   SetPortWindowPort(wptr);
943   Nlm_SetUpdateRegion (wptr);
944   Nlm_ResetDrawingTools ();
945 #endif
946 #ifdef WIN_MSWIN
947   /* BringWindowToTop (wptr); */
948   Nlm_currentHDC = Nlm_ParentWindowPort ((Nlm_GraphiC) w);
949   Nlm_currentHWnd = wptr;
950 #endif
951 #ifdef WIN_MOTIF
952   /* Nlm_Select ( w ); */
953 #endif
954   Nlm_currentWindowTool = wptr;
955 }
956 
Nlm_VibMonStrValue(Nlm_MonitorPtr mon)957 static int Nlm_VibMonStrValue (Nlm_MonitorPtr mon)
958 
959 {
960   Nlm_VibMonPtr  vmp;
961   Nlm_PaneL      pnl;
962   Nlm_RecT       r;
963   Nlm_WindoW     tempPort;
964   Nlm_WindoW     wind;
965 #ifdef WIN_MOTIF
966   XEvent         event;
967   Nlm_Uint4      tempBackColor;
968   Nlm_Uint4      tempForeColor;
969   Nlm_Int2       tempXOffset;
970   Nlm_Int2       tempYOffset;
971   Window         tempXWind;
972   GC             tempXGC;
973 #endif
974 
975   if (mon == NULL || mon->type != MonType_Str || mon->extra == NULL) {
976     return 0;
977   }
978   vmp = (Nlm_VibMonPtr) mon->extra;
979   pnl = vmp->pnl;
980   wind = vmp->wind;
981   tempPort = Nlm_CurrentWindow ();
982 #ifdef WIN_MOTIF
983   tempXWind = Nlm_currentXWindow;
984   tempXGC   = Nlm_currentXGC;
985   tempBackColor = Nlm_XbackColor;
986   tempForeColor = Nlm_XforeColor;
987   tempXOffset = Nlm_XOffset;
988   tempYOffset = Nlm_YOffset;
989 #endif
990   Nlm_UseWindow (wind);
991   Nlm_SelectMonitor (wind);
992   Nlm_Select (pnl);
993   Nlm_GetRect ((Nlm_GraphiC) pnl, &r);
994   Nlm_InsetRect (&r, 2, 2);
995   Nlm_DrawString (&r, (Nlm_CharPtr) mon->strValue, 'l', FALSE);
996   Nlm_UseWindow (tempPort);
997 #ifdef WIN_MOTIF
998   Nlm_currentXWindow = tempXWind;
999   Nlm_currentXGC = tempXGC;
1000   Nlm_XbackColor = tempBackColor;
1001   Nlm_XforeColor = tempForeColor;
1002   Nlm_XOffset = tempXOffset;
1003   Nlm_YOffset = tempYOffset;
1004 #endif
1005 
1006   Nlm_Update ();
1007 
1008 #if defined(WIN_MAC) ||  defined(WIN_MSWIN)
1009   while (Nlm_MouseButton ()) {
1010     Nlm_ProcessExternalEvent ();
1011   }
1012 #endif
1013 #ifdef WIN_MOTIF
1014   while (XCheckTypedEvent (Nlm_currentXDisplay, ButtonPress, &event) ||
1015 	 XCheckTypedEvent (Nlm_currentXDisplay, ButtonRelease, &event))
1016     {
1017       XtDispatchEvent( &event );
1018     }
1019 #endif
1020   Nlm_ProcessAnEvent ();
1021 
1022   mon->cancel = vmp->cancel;
1023   return 0;
1024 }
1025 
Nlm_VibMonIntValue(Nlm_MonitorPtr mon)1026 static int Nlm_VibMonIntValue (Nlm_MonitorPtr mon)
1027 
1028 {
1029   Nlm_Int4       from;
1030   Nlm_VibMonPtr  vmp;
1031   Nlm_PaneL      pnl;
1032   Nlm_RecT       r;
1033   Nlm_Int4       range;
1034   Nlm_Int2       right;
1035   Nlm_WindoW     tempPort;
1036   Nlm_Int4       to;
1037   Nlm_Int4       value;
1038   Nlm_Int4       width;
1039   Nlm_WindoW     wind;
1040 #ifdef WIN_MOTIF
1041   XEvent         event;
1042   Nlm_Uint4      tempBackColor;
1043   Nlm_Uint4      tempForeColor;
1044   Nlm_Int2       tempXOffset;
1045   Nlm_Int2       tempYOffset;
1046   Window         tempXWind;
1047   GC             tempXGC;
1048 #endif
1049 
1050   if (mon == NULL || mon->type != MonType_Int || mon->extra == NULL) {
1051     return 0;
1052   }
1053   vmp = (Nlm_VibMonPtr) mon->extra;
1054   from = MIN (mon->num1, mon->num2);
1055   to = MAX (mon->num1, mon->num2);
1056   range = to - from;
1057   value = mon->intValue;
1058   value = MAX (value, from);
1059   value = MIN (value, to);
1060   value -= from;
1061   pnl = vmp->pnl;
1062   wind = vmp->wind;
1063   tempPort = Nlm_CurrentWindow ();
1064 #ifdef WIN_MOTIF
1065   tempXWind = Nlm_currentXWindow;
1066   tempXGC   = Nlm_currentXGC;
1067   tempBackColor = Nlm_XbackColor;
1068   tempForeColor = Nlm_XforeColor;
1069   tempXOffset = Nlm_XOffset;
1070   tempYOffset = Nlm_YOffset;
1071 #endif
1072   Nlm_UseWindow (wind);
1073   Nlm_SelectMonitor (wind);
1074   Nlm_Select (pnl);
1075   Nlm_GetRect ((Nlm_GraphiC) pnl, &r);
1076   Nlm_FrameRect (&r);
1077   Nlm_InsetRect (&r, 2, 2);
1078   value = MAX (value, 0);
1079   value = MIN (value, range);
1080   width = (Nlm_Int4) (r.right - r.left);
1081   right = r.right;
1082   r.right = (Nlm_Int2)(r.left + (width * value / range));
1083   Nlm_Black ();
1084   Nlm_PaintRect (&r);
1085   r.left = r.right;
1086   r.right = right;
1087   Nlm_EraseRect (&r);
1088   Nlm_UseWindow (tempPort);
1089 #ifdef WIN_MOTIF
1090   Nlm_currentXWindow = tempXWind;
1091   Nlm_currentXGC = tempXGC;
1092   Nlm_XbackColor = tempBackColor;
1093   Nlm_XforeColor = tempForeColor;
1094   Nlm_XOffset = tempXOffset;
1095   Nlm_YOffset = tempYOffset;
1096 #endif
1097 
1098   Nlm_Update ();
1099 
1100 #if defined(WIN_MAC) || defined(WIN_MSWIN)
1101   while (Nlm_MouseButton ()) {
1102     Nlm_ProcessExternalEvent ();
1103   }
1104 #endif
1105 #ifdef WIN_MOTIF
1106   while (XCheckTypedEvent (Nlm_currentXDisplay, ButtonPress, &event) ||
1107 	 XCheckTypedEvent (Nlm_currentXDisplay, ButtonRelease, &event))
1108     {
1109       XtDispatchEvent( &event );
1110     }
1111 #endif
1112   Nlm_ProcessAnEvent ();
1113 
1114   mon->cancel = vmp->cancel;
1115   return 0;
1116 }
1117 
Nlm_CancelMonitorProc(Nlm_ButtoN b)1118 static void Nlm_CancelMonitorProc (Nlm_ButtoN b)
1119 
1120 {
1121   Nlm_VibMonPtr  vmp;
1122 
1123   vmp = (Nlm_VibMonPtr) Nlm_GetObjectExtra (b);
1124   if (vmp != NULL) {
1125     vmp->cancel = TRUE;
1126   }
1127 }
1128 
Nlm_VibMonCreate(Nlm_MonitorPtr mon)1129 static int Nlm_VibMonCreate (Nlm_MonitorPtr mon)
1130 
1131 {
1132   Nlm_ButtoN     b;
1133   Nlm_VibMonPtr  vmp;
1134   Nlm_PaneL      pnl;
1135   Nlm_WindoW     wind;
1136 
1137   if (mon != NULL) {
1138     vmp = (Nlm_VibMonPtr) Nlm_MemNew (sizeof (Nlm_VibMonExtra));
1139     mon->extra = (Nlm_VoidPtr) vmp;
1140     if (vmp != NULL) {
1141       wind = Nlm_FixedWindow (-50, -90, -10, -10, (Nlm_CharPtr) mon->strTitle, NULL);
1142       if (mon->type == MonType_Int) {
1143         pnl = Nlm_SimplePanel (wind, 200, 20, NULL);
1144       } else if (mon->type == MonType_Str) {
1145         pnl = Nlm_SimplePanel(wind, (Nlm_Int2)(Nlm_stdCharWidth*mon->num1+6),
1146                               (Nlm_Int2)(Nlm_stdLineHeight + 4), NULL);
1147       } else {
1148         pnl = Nlm_SimplePanel (wind, 200, 20, NULL);
1149       }
1150       Nlm_Advance (wind);
1151       b = NULL;
1152       if ((Nlm_Boolean) mon->hasCancelBtn) {
1153         b = Nlm_DefaultButton (wind, "Cancel", Nlm_CancelMonitorProc);
1154         Nlm_SetObjectExtra (b, (Nlm_VoidPtr) vmp, NULL);
1155       }
1156       Nlm_AlignObjects (ALIGN_MIDDLE, (Nlm_HANDLE) pnl, (Nlm_HANDLE) b, NULL);
1157       vmp->wind = wind;
1158       vmp->pnl = pnl;
1159       vmp->cancel = FALSE;
1160       Nlm_Show (wind);
1161 #ifndef WIN_MOTIF
1162       Nlm_Select (wind);
1163 #endif
1164       Nlm_Update ();
1165     }
1166   }
1167   return TRUE;
1168 }
1169 
Nlm_VibMonDestroy(Nlm_MonitorPtr mon)1170 static int Nlm_VibMonDestroy (Nlm_MonitorPtr mon)
1171 
1172 {
1173   Nlm_VibMonPtr  vmp;
1174 
1175   if (mon != NULL) {
1176     vmp = (Nlm_VibMonPtr) mon->extra;
1177     if (vmp != NULL) {
1178       Nlm_Remove (vmp->wind);
1179       Nlm_MemFree (vmp);
1180     }
1181   }
1182   return 0;
1183 }
1184 
1185 
Nlm_VibMonitorHook(Nlm_MonitorPtr mon,MonCode code)1186 static int LIBCALLBACK Nlm_VibMonitorHook(Nlm_MonitorPtr mon, MonCode code)
1187 {
1188   switch ( code )
1189     {
1190     case MonCode_Create:
1191       return Nlm_VibMonCreate( mon );
1192     case MonCode_Destroy:
1193       return Nlm_VibMonDestroy( mon );
1194     case MonCode_IntValue:
1195       return Nlm_VibMonIntValue( mon );
1196     case MonCode_StrValue:
1197       return Nlm_VibMonStrValue( mon );
1198     }
1199 
1200   return 0;
1201 }
1202 
Nlm_VibBeepHook(void)1203 static void LIBCALLBACK Nlm_VibBeepHook (void)
1204 
1205 {
1206 #ifdef WIN_MAC
1207   SysBeep (60);
1208 #endif
1209 #ifdef WIN_MSWIN
1210   MessageBeep (0);
1211 #endif
1212 #ifdef WIN_MOTIF
1213   if (Nlm_currentXDisplay != NULL) {
1214     XBell (Nlm_currentXDisplay, 0);
1215   }
1216 #endif
1217 }
1218 
Nlm_InitVibrantHooks(void)1219 extern void Nlm_InitVibrantHooks (void)
1220 
1221 {
1222   Nlm_SetBeepHook (Nlm_VibBeepHook);
1223   Nlm_SetMonitorHook (Nlm_VibMonitorHook);
1224   Nlm_SetMessageHook (Nlm_VibMessageHook);
1225 }
1226 
Nlm_MousePosition(Nlm_PointPtr pt)1227 extern void Nlm_MousePosition (Nlm_PointPtr pt)
1228 
1229 {
1230 #ifdef WIN_MAC
1231   Nlm_PointTool  ptool;
1232 
1233   GetMouse (&ptool);
1234   if (pt != NULL) {
1235     Nlm_PointToolToPoinT (ptool, pt);
1236   }
1237 #endif
1238 #ifdef WIN_MSWIN
1239   Nlm_PointTool  ptool;
1240 
1241   if (pt != NULL && Nlm_currentHWnd != NULL) {
1242     GetCursorPos (&ptool);
1243     ScreenToClient (Nlm_currentHWnd, &ptool);
1244     Nlm_PointToolToPoinT (ptool, pt);
1245   }
1246 #endif
1247 #ifdef WIN_MOTIF
1248 #endif
1249 }
1250 
Nlm_MouseButton(void)1251 extern Nlm_Boolean Nlm_MouseButton (void)
1252 
1253 {
1254 #ifdef WIN_MAC
1255   return (Button ());
1256 #endif
1257 #ifdef WIN_MSWIN
1258   return (Nlm_Boolean) ((GetAsyncKeyState (VK_LBUTTON) & 0x8000) != 0);
1259 #endif
1260 #ifdef WIN_MOTIF
1261   return FALSE;
1262 #endif
1263 }
1264 
Nlm_ComputerTime(void)1265 extern Nlm_Int4 Nlm_ComputerTime (void)
1266 
1267 {
1268 #ifdef WIN_MAC
1269   return (TickCount ());
1270 #endif
1271 #ifdef WIN_MSWIN
1272   return (GetCurrentTime ());
1273 #endif
1274 #ifdef WIN_MOTIF
1275   struct tms buffer;
1276   return (Nlm_Int4) times (&buffer);
1277 #endif
1278 }
1279 
1280 
Nlm_Version(Nlm_CharPtr vsn,size_t maxsize)1281 extern void Nlm_Version (Nlm_CharPtr vsn, size_t maxsize)
1282 {
1283   Nlm_StringNCpy_0 (vsn, "NCBI VIBRANT Version 2.0 (29 Jan 97)", maxsize);
1284 }
1285 
1286 
Nlm_Advance(Nlm_Handle a)1287 extern void Nlm_Advance (Nlm_Handle a)
1288 
1289 {
1290   Nlm_BoxData  bdata;
1291 
1292   if (a != NULL) {
1293     Nlm_GetBoxData ((Nlm_BoX) a, &bdata);
1294     bdata.nextPoint.y = bdata.topRow;
1295     bdata.nextPoint.x = bdata.nextCol;
1296     Nlm_SetBoxData ((Nlm_BoX) a, &bdata);
1297   }
1298 }
1299 
Nlm_Break(Nlm_Handle a)1300 extern void Nlm_Break (Nlm_Handle a)
1301 
1302 {
1303   Nlm_BoxData  bdata;
1304 
1305   if (a != NULL) {
1306     Nlm_GetBoxData ((Nlm_BoX) a, &bdata);
1307     bdata.topRow = (Nlm_Int2)(bdata.limitPoint.y + bdata.ySpacing);
1308     bdata.nextCol = bdata.resetPoint.x;
1309     bdata.nextPoint.y = bdata.topRow;
1310     bdata.nextPoint.x = bdata.nextCol;
1311     Nlm_SetBoxData ((Nlm_BoX) a, &bdata);
1312   }
1313 }
1314 
Nlm_RecordRect(Nlm_GraphiC a,Nlm_RectPtr r)1315 extern void Nlm_RecordRect (Nlm_GraphiC a, Nlm_RectPtr r)
1316 
1317 {
1318   Nlm_BoxData  bdata;
1319   Nlm_Int2     nc;
1320   Nlm_Int2     nr;
1321 
1322   if (a != NULL && r != NULL) {
1323     Nlm_GetBoxData ((Nlm_BoX) a, &bdata);
1324     nr = r->bottom;
1325     if (nr > bdata.limitPoint.y) {
1326       bdata.limitPoint.y = nr;
1327     }
1328     nc = r->right;
1329     if (nc > bdata.limitPoint.x) {
1330       bdata.limitPoint.x = nc;
1331     }
1332     nc = (Nlm_Int2)(r->right + bdata.xSpacing);
1333     if (nc > bdata.nextCol) {
1334       bdata.nextCol = nc;
1335     }
1336     Nlm_SetBoxData ((Nlm_BoX) a, &bdata);
1337   }
1338 }
1339 
Nlm_NextPosition(Nlm_GraphiC a,Nlm_RectPtr r)1340 extern void Nlm_NextPosition (Nlm_GraphiC a, Nlm_RectPtr r)
1341 
1342 {
1343   Nlm_BoxData  bdata;
1344 
1345   if (a != NULL && r != NULL) {
1346     Nlm_GetBoxData ((Nlm_BoX) a, &bdata);
1347     bdata.nextPoint.y = (Nlm_Int2)(r->bottom + bdata.ySpacing);
1348     Nlm_SetBoxData ((Nlm_BoX) a, &bdata);
1349   }
1350 }
1351 
Nlm_SetNextPosition(Nlm_Handle a,Nlm_PoinT nps)1352 extern void Nlm_SetNextPosition (Nlm_Handle a, Nlm_PoinT nps)
1353 
1354 {
1355   Nlm_BoxData  bdata;
1356 
1357   if (a != NULL) {
1358     Nlm_GetBoxData ((Nlm_BoX) a, &bdata);
1359     bdata.nextPoint = nps;
1360     bdata.topRow = nps.y;
1361     Nlm_SetBoxData ((Nlm_BoX) a, &bdata);
1362   }
1363 }
1364 
Nlm_GetNextPosition(Nlm_Handle a,Nlm_PointPtr nps)1365 extern void Nlm_GetNextPosition (Nlm_Handle a, Nlm_PointPtr nps)
1366 
1367 {
1368   Nlm_BoxData  bdata;
1369 
1370   if (a != NULL && nps != NULL) {
1371     Nlm_GetBoxData ((Nlm_BoX) a, &bdata);
1372     *nps = bdata.nextPoint;
1373   }
1374 }
1375 
Nlm_GetClassPtr(Nlm_GraphiC a)1376 extern Nlm_GphPrcsPtr Nlm_GetClassPtr (Nlm_GraphiC a)
1377 
1378 {
1379   Nlm_GphPrcsPtr   classPtr;
1380   Nlm_GraphicData  gdata;
1381 
1382   classPtr = NULL;
1383   if (a != NULL) {
1384     Nlm_GetGraphicData (a, &gdata);
1385     classPtr = gdata.classptr;
1386   }
1387   return classPtr;
1388 }
1389 
1390 #ifdef WIN_MAC
Nlm_DoClick(Nlm_GraphiC a,Nlm_PoinT pt)1391 extern Nlm_Boolean Nlm_DoClick (Nlm_GraphiC a, Nlm_PoinT pt)
1392 
1393 {
1394   Nlm_GphPrcsPtr  classPtr;
1395   Nlm_Boolean     cont;
1396   Nlm_Boolean     (*clk) PROTO((Nlm_GraphiC, Nlm_PoinT));
1397   Nlm_Boolean     rsult;
1398 
1399   rsult = FALSE;
1400   cont = TRUE;
1401   classPtr = Nlm_GetClassPtr (a);
1402   while (classPtr != NULL && cont) {
1403     clk = classPtr->click;
1404     if (clk != NULL) {
1405       rsult = clk (a, pt);
1406       cont = FALSE;
1407     } else {
1408       classPtr = classPtr->ancestor;
1409     }
1410   }
1411   return rsult;
1412 }
1413 
Nlm_DoKey(Nlm_GraphiC a,Nlm_Char ch)1414 extern Nlm_Boolean Nlm_DoKey (Nlm_GraphiC a, Nlm_Char ch)
1415 
1416 {
1417   Nlm_GphPrcsPtr  classPtr;
1418   Nlm_Boolean     cont;
1419   Nlm_Boolean     (*ky) PROTO((Nlm_GraphiC, Nlm_Char));
1420   Nlm_Boolean     rsult;
1421 
1422   rsult = FALSE;
1423   cont = TRUE;
1424   classPtr = Nlm_GetClassPtr (a);
1425   while (classPtr != NULL && cont) {
1426     ky = classPtr->key;
1427     if (ky != NULL) {
1428       rsult = ky (a, ch);
1429       cont = FALSE;
1430     } else {
1431       classPtr = classPtr->ancestor;
1432     }
1433   }
1434   return rsult;
1435 }
1436 
Nlm_DoDraw(Nlm_GraphiC a)1437 extern void Nlm_DoDraw (Nlm_GraphiC a)
1438 
1439 {
1440   Nlm_GphPrcsPtr  classPtr;
1441   Nlm_Boolean     cont;
1442   void            (*drw) PROTO((Nlm_GraphiC));
1443 
1444   cont = TRUE;
1445   classPtr = Nlm_GetClassPtr (a);
1446   while (classPtr != NULL && cont) {
1447     drw = classPtr->draw;
1448     if (drw != NULL) {
1449       drw (a);
1450       cont = FALSE;
1451     } else {
1452       classPtr = classPtr->ancestor;
1453     }
1454   }
1455 }
1456 
Nlm_DoIdle(Nlm_GraphiC a,Nlm_PoinT pt)1457 extern Nlm_Boolean Nlm_DoIdle (Nlm_GraphiC a, Nlm_PoinT pt)
1458 
1459 {
1460   Nlm_GphPrcsPtr  classPtr;
1461   Nlm_Boolean     cont;
1462   Nlm_Boolean     (*idl) PROTO((Nlm_GraphiC, Nlm_PoinT));
1463   Nlm_Boolean     rsult;
1464 
1465   rsult = FALSE;
1466   cont = TRUE;
1467   classPtr = Nlm_GetClassPtr (a);
1468   while (classPtr != NULL && cont) {
1469     idl = classPtr->idle;
1470     if (idl != NULL) {
1471       rsult = idl (a, pt);
1472       cont = FALSE;
1473     } else {
1474       classPtr = classPtr->ancestor;
1475     }
1476   }
1477   return rsult;
1478 }
1479 #endif
1480 
1481 #ifdef WIN_MSWIN
Nlm_DoCommand(Nlm_GraphiC a)1482 extern Nlm_Boolean Nlm_DoCommand (Nlm_GraphiC a)
1483 
1484 {
1485   Nlm_GphPrcsPtr  classPtr;
1486   Nlm_Boolean     cont;
1487   Nlm_Boolean     (*cmd) PROTO((Nlm_GraphiC));
1488   Nlm_Boolean     rsult;
1489 
1490   rsult = FALSE;
1491   cont = TRUE;
1492   classPtr = Nlm_GetClassPtr (a);
1493   while (classPtr != NULL && cont) {
1494     cmd = classPtr->command;
1495     if (cmd != NULL) {
1496       rsult = cmd (a);
1497       cont = FALSE;
1498     } else {
1499       classPtr = classPtr->ancestor;
1500     }
1501   }
1502   return rsult;
1503 }
1504 #endif
1505 
1506 #ifdef WIN_MOTIF
Nlm_DoCallback(Nlm_GraphiC a)1507 extern void Nlm_DoCallback (Nlm_GraphiC a)
1508 
1509 {
1510   Nlm_GphPrcsPtr  classPtr;
1511   Nlm_Boolean     cont;
1512   void            (*clb) PROTO((Nlm_GraphiC));
1513 
1514   cont = TRUE;
1515   classPtr = Nlm_GetClassPtr (a);
1516   while (classPtr != NULL && cont) {
1517     clb = classPtr->callback;
1518     if (clb != NULL) {
1519       clb (a);
1520       cont = FALSE;
1521     } else {
1522       classPtr = classPtr->ancestor;
1523     }
1524   }
1525 }
1526 #endif
1527 
Nlm_DoShow(Nlm_GraphiC a,Nlm_Boolean setFlag,Nlm_Boolean savePort)1528 extern void Nlm_DoShow (Nlm_GraphiC a, Nlm_Boolean setFlag, Nlm_Boolean savePort)
1529 
1530 {
1531   Nlm_GphPrcsPtr  classPtr;
1532   Nlm_Boolean     cont;
1533   void            (*shw) PROTO((Nlm_GraphiC, Nlm_Boolean, Nlm_Boolean));
1534 
1535   cont = TRUE;
1536   classPtr = Nlm_GetClassPtr (a);
1537   while (classPtr != NULL && cont) {
1538     shw = classPtr->show;
1539     if (shw != NULL) {
1540       shw (a, setFlag, savePort);
1541       cont = FALSE;
1542     } else {
1543       classPtr = classPtr->ancestor;
1544     }
1545   }
1546 }
1547 
Nlm_DoHide(Nlm_GraphiC a,Nlm_Boolean setFlag,Nlm_Boolean savePort)1548 extern void Nlm_DoHide (Nlm_GraphiC a, Nlm_Boolean setFlag, Nlm_Boolean savePort)
1549 
1550 {
1551   Nlm_GphPrcsPtr  classPtr;
1552   Nlm_Boolean     cont;
1553   void            (*hid) PROTO((Nlm_GraphiC, Nlm_Boolean, Nlm_Boolean));
1554 
1555   cont = TRUE;
1556   classPtr = Nlm_GetClassPtr (a);
1557   while (classPtr != NULL && cont) {
1558     hid = classPtr->hide;
1559     if (hid != NULL) {
1560       hid (a, setFlag, savePort);
1561       cont = FALSE;
1562     } else {
1563       classPtr = classPtr->ancestor;
1564     }
1565   }
1566 }
1567 
Nlm_DoEnable(Nlm_GraphiC a,Nlm_Boolean setFlag,Nlm_Boolean savePort)1568 extern void Nlm_DoEnable (Nlm_GraphiC a, Nlm_Boolean setFlag, Nlm_Boolean savePort)
1569 
1570 {
1571   Nlm_GphPrcsPtr  classPtr;
1572   Nlm_Boolean     cont;
1573   void            (*enbl) PROTO((Nlm_GraphiC, Nlm_Boolean, Nlm_Boolean));
1574 
1575   cont = TRUE;
1576   classPtr = Nlm_GetClassPtr (a);
1577   while (classPtr != NULL && cont) {
1578     enbl = classPtr->enable;
1579     if (enbl != NULL) {
1580       enbl (a, setFlag, savePort);
1581       cont = FALSE;
1582     } else {
1583       classPtr = classPtr->ancestor;
1584     }
1585   }
1586 }
1587 
Nlm_DoDisable(Nlm_GraphiC a,Nlm_Boolean setFlag,Nlm_Boolean savePort)1588 extern void Nlm_DoDisable (Nlm_GraphiC a, Nlm_Boolean setFlag, Nlm_Boolean savePort)
1589 
1590 {
1591   Nlm_GphPrcsPtr  classPtr;
1592   Nlm_Boolean     cont;
1593   void            (*dsbl) PROTO((Nlm_GraphiC, Nlm_Boolean, Nlm_Boolean));
1594 
1595   cont = TRUE;
1596   classPtr = Nlm_GetClassPtr (a);
1597   while (classPtr != NULL && cont) {
1598     dsbl = classPtr->disable;
1599     if (dsbl != NULL) {
1600       dsbl (a, setFlag, savePort);
1601       cont = FALSE;
1602     } else {
1603       classPtr = classPtr->ancestor;
1604     }
1605   }
1606 }
1607 
Nlm_DoActivate(Nlm_GraphiC a,Nlm_Boolean savePort)1608 extern void Nlm_DoActivate (Nlm_GraphiC a, Nlm_Boolean savePort)
1609 
1610 {
1611   void            (*actvate) PROTO((Nlm_GraphiC, Nlm_Boolean));
1612   Nlm_Boolean     cont;
1613   Nlm_GphPrcsPtr  classPtr;
1614 
1615   cont = TRUE;
1616   classPtr = Nlm_GetClassPtr (a);
1617   while (classPtr != NULL && cont) {
1618     actvate = classPtr->activate;
1619     if (actvate != NULL) {
1620       actvate (a, savePort);
1621       cont = FALSE;
1622     } else {
1623       classPtr = classPtr->ancestor;
1624     }
1625   }
1626 }
1627 
Nlm_DoDeactivate(Nlm_GraphiC a,Nlm_Boolean savePort)1628 extern void Nlm_DoDeactivate (Nlm_GraphiC a, Nlm_Boolean savePort)
1629 
1630 {
1631   Nlm_GphPrcsPtr  classPtr;
1632   Nlm_Boolean     cont;
1633   void            (*deactvate) PROTO((Nlm_GraphiC, Nlm_Boolean));
1634 
1635   cont = TRUE;
1636   classPtr = Nlm_GetClassPtr (a);
1637   while (classPtr != NULL && cont) {
1638     deactvate = classPtr->deactivate;
1639     if (deactvate != NULL) {
1640       deactvate (a, savePort);
1641       cont = FALSE;
1642     } else {
1643       classPtr = classPtr->ancestor;
1644     }
1645   }
1646 }
1647 
Nlm_DoRemove(Nlm_GraphiC a,Nlm_Boolean savePort)1648 extern Nlm_Handle Nlm_DoRemove (Nlm_GraphiC a, Nlm_Boolean savePort)
1649 
1650 {
1651   Nlm_GphPrcsPtr  classPtr;
1652   Nlm_Boolean     cont;
1653   void            (*rmv) PROTO((Nlm_GraphiC, Nlm_Boolean));
1654 
1655   cont = TRUE;
1656 
1657 #if !defined(WIN_MOTIF) || !defined(LESSTIF_VERSION)
1658   /* using LessTif (on Linux), this eventually causes a segfault down the
1659      line when quitting the application - is this really necessary? (thiessen) */
1660   Nlm_DoHide (a, TRUE, savePort);
1661 #endif
1662 
1663   classPtr = Nlm_GetClassPtr (a);
1664   while (classPtr != NULL && cont) {
1665     rmv = classPtr->remove;
1666     if (rmv != NULL) {
1667       rmv (a, savePort);
1668       cont = FALSE;
1669     } else {
1670       classPtr = classPtr->ancestor;
1671     }
1672     recentGraphic = NULL;
1673     recentBox = NULL;
1674   }
1675   return NULL;
1676 }
1677 
Nlm_DoReset(Nlm_GraphiC a,Nlm_Boolean savePort)1678 extern void Nlm_DoReset (Nlm_GraphiC a, Nlm_Boolean savePort)
1679 
1680 {
1681   Nlm_GphPrcsPtr  classPtr;
1682   Nlm_Boolean     cont;
1683   void            (*rst) PROTO((Nlm_GraphiC, Nlm_Boolean));
1684 
1685   cont = TRUE;
1686   classPtr = Nlm_GetClassPtr (a);
1687   while (classPtr != NULL && cont) {
1688     rst = classPtr->reset;
1689     if (rst != NULL) {
1690       rst (a, TRUE);
1691       cont = FALSE;
1692     } else {
1693       classPtr = classPtr->ancestor;
1694     }
1695   }
1696 }
1697 
Nlm_DoSelect(Nlm_GraphiC a,Nlm_Boolean savePort)1698 extern void Nlm_DoSelect (Nlm_GraphiC a, Nlm_Boolean savePort)
1699 
1700 {
1701   Nlm_GphPrcsPtr  classPtr;
1702   Nlm_Boolean     cont;
1703   void            (*sel) PROTO((Nlm_GraphiC, Nlm_Boolean));
1704 
1705   cont = TRUE;
1706   classPtr = Nlm_GetClassPtr (a);
1707   while (classPtr != NULL && cont) {
1708     sel = classPtr->select;
1709     if (sel != NULL) {
1710       sel (a, savePort);
1711       cont = FALSE;
1712     } else {
1713       classPtr = classPtr->ancestor;
1714     }
1715   }
1716 }
1717 
Nlm_CountItems(Nlm_Handle a)1718 extern Nlm_Int2 Nlm_CountItems (Nlm_Handle a)
1719 
1720 {
1721   Nlm_GphPrcsPtr  classPtr;
1722   Nlm_Boolean     cont;
1723   Nlm_Int2        (*cntitm) PROTO((Nlm_GraphiC));
1724   Nlm_Int2        len;
1725 
1726   cont = TRUE;
1727   len = 0;
1728   classPtr = Nlm_GetClassPtr ((Nlm_GraphiC)a);
1729   while (classPtr != NULL && cont) {
1730     cntitm = classPtr->countItems;
1731     if (cntitm != NULL) {
1732       len = cntitm ((Nlm_GraphiC)a);
1733       cont = FALSE;
1734     } else {
1735       classPtr = classPtr->ancestor;
1736     }
1737   }
1738   return len;
1739 }
1740 
Nlm_DoLinkIn(Nlm_GraphiC a,Nlm_GraphiC prnt)1741 extern Nlm_GraphiC Nlm_DoLinkIn (Nlm_GraphiC a, Nlm_GraphiC prnt)
1742 
1743 {
1744   Nlm_GphPrcsPtr  prntClassPtr;
1745   Nlm_Boolean     cont;
1746   Nlm_GraphiC     (*lnkIn) PROTO((Nlm_GraphiC, Nlm_GraphiC));
1747   Nlm_GraphiC     rsult;
1748 
1749   cont = TRUE;
1750   rsult = NULL;
1751   if (a != NULL && prnt != NULL) {
1752     cont = TRUE;
1753     prntClassPtr = Nlm_GetClassPtr (prnt);
1754     while (prntClassPtr != NULL && cont) {
1755       lnkIn = prntClassPtr->linkIn;
1756       if (lnkIn != NULL) {
1757         rsult = lnkIn (a, prnt);
1758         cont = FALSE;
1759       } else {
1760         prntClassPtr = prntClassPtr->ancestor;
1761       }
1762     }
1763   }
1764   return rsult;
1765 }
1766 
Nlm_DoAdjustPrnt(Nlm_GraphiC a,Nlm_RectPtr r,Nlm_Boolean align,Nlm_Boolean savePort)1767 extern void Nlm_DoAdjustPrnt (Nlm_GraphiC a, Nlm_RectPtr r,
1768                               Nlm_Boolean align, Nlm_Boolean savePort)
1769 
1770 {
1771   void            (*adjst) PROTO((Nlm_GraphiC, Nlm_RectPtr, Nlm_Boolean, Nlm_Boolean));
1772   Nlm_Boolean     cont;
1773   Nlm_GraphiC     p;
1774   Nlm_GphPrcsPtr  prntClassPtr;
1775 
1776   cont = TRUE;
1777   if (a != NULL) {
1778     p = Nlm_GetParent (a);
1779     prntClassPtr = Nlm_GetClassPtr (p);
1780     while (prntClassPtr != NULL && cont) {
1781       adjst = prntClassPtr->adjustPrnt;
1782       if (adjst != NULL) {
1783         adjst (a, r, align, savePort);
1784         cont = FALSE;
1785       } else {
1786         prntClassPtr = prntClassPtr->ancestor;
1787       }
1788     }
1789   }
1790 }
1791 
1792 
Nlm_DoSetTitle(Nlm_GraphiC a,Nlm_Int2 item,Nlm_CharPtr title,Nlm_Boolean savePort)1793 extern void Nlm_DoSetTitle (Nlm_GraphiC a, Nlm_Int2 item,
1794                             Nlm_CharPtr title, Nlm_Boolean savePort)
1795 
1796 {
1797   Nlm_GphPrcsPtr  classPtr;
1798   Nlm_Boolean     cont;
1799   void            (*stttl) PROTO((Nlm_GraphiC, Nlm_Int2, Nlm_CharPtr, Nlm_Boolean));
1800 
1801   cont = TRUE;
1802   classPtr = Nlm_GetClassPtr (a);
1803   while (classPtr != NULL && cont) {
1804     stttl = classPtr->setTitle;
1805     if (stttl != NULL) {
1806       stttl (a, item, title, savePort);
1807       cont = FALSE;
1808     } else {
1809       classPtr = classPtr->ancestor;
1810     }
1811   }
1812 }
1813 
Nlm_DoGetTitle(Nlm_GraphiC a,Nlm_Int2 item,Nlm_CharPtr title,size_t maxsize)1814 extern void Nlm_DoGetTitle (Nlm_GraphiC a, Nlm_Int2 item,
1815                             Nlm_CharPtr title, size_t maxsize)
1816 
1817 {
1818   Nlm_GphPrcsPtr  classPtr;
1819   Nlm_Boolean     cont;
1820   Nlm_Boolean     failed;
1821   void            (*gtttl) PROTO((Nlm_GraphiC, Nlm_Int2, Nlm_CharPtr, size_t));
1822 
1823   cont = TRUE;
1824   failed = TRUE;
1825   classPtr = Nlm_GetClassPtr (a);
1826   while (classPtr != NULL && cont) {
1827     gtttl = classPtr->getTitle;
1828     if (gtttl != NULL) {
1829       gtttl (a, item, title, maxsize);
1830       failed = FALSE;
1831       cont = FALSE;
1832     } else {
1833       classPtr = classPtr->ancestor;
1834     }
1835   }
1836 
1837   if (failed  &&  maxsize > 0)
1838     title[0] = '\0';
1839 }
1840 
Nlm_DoSetValue(Nlm_GraphiC a,Nlm_Int2 value,Nlm_Boolean savePort)1841 extern void Nlm_DoSetValue (Nlm_GraphiC a, Nlm_Int2 value, Nlm_Boolean savePort)
1842 
1843 {
1844   Nlm_GphPrcsPtr  classPtr;
1845   Nlm_Boolean     cont;
1846   void            (*stval) PROTO((Nlm_GraphiC, Nlm_Int2, Nlm_Boolean));
1847 
1848   cont = TRUE;
1849   classPtr = Nlm_GetClassPtr (a);
1850   while (classPtr != NULL && cont) {
1851     stval = classPtr->setValue;
1852     if (stval != NULL) {
1853       stval (a, value, savePort);
1854       cont = FALSE;
1855     } else {
1856       classPtr = classPtr->ancestor;
1857     }
1858   }
1859 }
1860 
Nlm_DoGetValue(Nlm_GraphiC a)1861 extern Nlm_Int2 Nlm_DoGetValue (Nlm_GraphiC a)
1862 
1863 {
1864   Nlm_GphPrcsPtr  classPtr;
1865   Nlm_Boolean     cont;
1866   Nlm_Int2        (*gtval) PROTO((Nlm_GraphiC));
1867   Nlm_Int2        value;
1868 
1869   cont = TRUE;
1870   value = 0;
1871   classPtr = Nlm_GetClassPtr (a);
1872   while (classPtr != NULL && cont) {
1873     gtval = classPtr->getValue;
1874     if (gtval != NULL) {
1875       value = gtval (a);
1876       cont = FALSE;
1877     } else {
1878       classPtr = classPtr->ancestor;
1879     }
1880   }
1881   return value;
1882 }
1883 
Nlm_DoSetStatus(Nlm_GraphiC a,Nlm_Int2 item,Nlm_Boolean status,Nlm_Boolean savePort)1884 extern void Nlm_DoSetStatus (Nlm_GraphiC a, Nlm_Int2 item,
1885                              Nlm_Boolean status, Nlm_Boolean savePort)
1886 
1887 {
1888   Nlm_GphPrcsPtr  classPtr;
1889   Nlm_Boolean     cont;
1890   void            (*ststs) PROTO((Nlm_GraphiC, Nlm_Int2, Nlm_Boolean, Nlm_Boolean));
1891 
1892   cont = TRUE;
1893   classPtr = Nlm_GetClassPtr (a);
1894   while (classPtr != NULL && cont) {
1895     ststs = classPtr->setStatus;
1896     if (ststs != NULL) {
1897       ststs (a, item, status, savePort);
1898       cont = FALSE;
1899     } else {
1900       classPtr = classPtr->ancestor;
1901     }
1902   }
1903 }
1904 
Nlm_DoGetStatus(Nlm_GraphiC a,Nlm_Int2 item)1905 extern Nlm_Boolean Nlm_DoGetStatus (Nlm_GraphiC a, Nlm_Int2 item)
1906 
1907 {
1908   Nlm_GphPrcsPtr  classPtr;
1909   Nlm_Boolean     cont;
1910   Nlm_Boolean     (*gtsts) PROTO((Nlm_GraphiC, Nlm_Int2));
1911   Nlm_Boolean     val;
1912 
1913   cont = TRUE;
1914   val = FALSE;
1915   classPtr = Nlm_GetClassPtr (a);
1916   while (classPtr != NULL && cont) {
1917     gtsts = classPtr->getStatus;
1918     if (gtsts != NULL) {
1919       val = gtsts (a, item);
1920       cont = FALSE;
1921     } else {
1922       classPtr = classPtr->ancestor;
1923     }
1924   }
1925   return val;
1926 }
1927 
Nlm_DoSetOffset(Nlm_GraphiC a,Nlm_Int2 horiz,Nlm_Int2 vert,Nlm_Boolean savePort)1928 extern void Nlm_DoSetOffset (Nlm_GraphiC a, Nlm_Int2 horiz,
1929                              Nlm_Int2 vert,Nlm_Boolean savePort)
1930 
1931 {
1932   Nlm_GphPrcsPtr  classPtr;
1933   Nlm_Boolean     cont;
1934   void            (*stoff) PROTO((Nlm_GraphiC, Nlm_Int2, Nlm_Int2, Nlm_Boolean));
1935 
1936   cont = TRUE;
1937   classPtr = Nlm_GetClassPtr (a);
1938   while (classPtr != NULL && cont) {
1939     stoff = classPtr->setOffset;
1940     if (stoff != NULL) {
1941       stoff (a, horiz, vert, savePort);
1942       cont = FALSE;
1943     } else {
1944       classPtr = classPtr->ancestor;
1945     }
1946   }
1947 }
1948 
Nlm_DoGetOffset(Nlm_GraphiC a,Nlm_Int2Ptr horiz,Nlm_Int2Ptr vert)1949 extern void Nlm_DoGetOffset (Nlm_GraphiC a, Nlm_Int2Ptr horiz, Nlm_Int2Ptr vert)
1950 
1951 {
1952   Nlm_GphPrcsPtr  classPtr;
1953   Nlm_Boolean     cont;
1954   Nlm_Boolean     failed;
1955   void            (*gtoff) PROTO((Nlm_GraphiC, Nlm_Int2Ptr, Nlm_Int2Ptr));
1956 
1957   cont = TRUE;
1958   failed = TRUE;
1959   classPtr = Nlm_GetClassPtr (a);
1960   while (classPtr != NULL && cont) {
1961     gtoff = classPtr->getOffset;
1962     if (gtoff != NULL) {
1963       gtoff (a, horiz, vert);
1964       failed = FALSE;
1965       cont = FALSE;
1966     } else {
1967       classPtr = classPtr->ancestor;
1968     }
1969   }
1970   if (failed) {
1971     if (horiz != NULL) {
1972       *horiz = 0;
1973     }
1974     if (vert != NULL) {
1975       *vert = 0;
1976     }
1977   }
1978 }
1979 
Nlm_DoSetPosition(Nlm_GraphiC a,Nlm_RectPtr r,Nlm_Boolean savePort,Nlm_Boolean force)1980 extern void Nlm_DoSetPosition (Nlm_GraphiC a, Nlm_RectPtr r, Nlm_Boolean savePort, Nlm_Boolean force)
1981 
1982 {
1983   Nlm_GphPrcsPtr  classPtr;
1984 
1985   classPtr = Nlm_GetClassPtr( a );
1986   while (classPtr != NULL)
1987     {
1988       if ( classPtr->setPosition )
1989 	{
1990 	  (*classPtr->setPosition)(a, r, savePort, force);
1991 	  break;
1992 	}
1993       else
1994 	{
1995 	  classPtr = classPtr->ancestor;
1996 	}
1997     }
1998 }
1999 
Nlm_DoGetPosition(Nlm_GraphiC a,Nlm_RectPtr r)2000 extern void Nlm_DoGetPosition (Nlm_GraphiC a, Nlm_RectPtr r)
2001 
2002 {
2003   Nlm_GphPrcsPtr  classPtr;
2004   Nlm_Boolean     cont;
2005   Nlm_Boolean     failed;
2006   void            (*gtpos) PROTO((Nlm_GraphiC, Nlm_RectPtr));
2007 
2008   cont = TRUE;
2009   failed = TRUE;
2010   classPtr = Nlm_GetClassPtr (a);
2011   while (classPtr != NULL && cont) {
2012     gtpos = classPtr->getPosition;
2013     if (gtpos != NULL) {
2014       gtpos (a, r);
2015       failed = FALSE;
2016       cont = FALSE;
2017     } else {
2018       classPtr = classPtr->ancestor;
2019     }
2020   }
2021   if (failed) {
2022     if (r != NULL) {
2023       Nlm_LoadRect (r, 0, 0, 0, 0);
2024     }
2025   }
2026 }
2027 
Nlm_DoSetRange(Nlm_GraphiC a,Nlm_Int2 pgUp,Nlm_Int2 pgDn,Nlm_Int2 max,Nlm_Boolean savePort)2028 extern void Nlm_DoSetRange (Nlm_GraphiC a, Nlm_Int2 pgUp,
2029                             Nlm_Int2 pgDn, Nlm_Int2 max,
2030                             Nlm_Boolean savePort)
2031 
2032 {
2033   Nlm_GphPrcsPtr  classPtr;
2034   Nlm_Boolean     cont;
2035   void            (*rng) PROTO((Nlm_GraphiC, Nlm_Int2, Nlm_Int2, Nlm_Int2, Nlm_Boolean));
2036 
2037 
2038   cont = TRUE;
2039   classPtr = Nlm_GetClassPtr (a);
2040   while (classPtr != NULL && cont) {
2041     rng = classPtr->setRange;
2042     if (rng != NULL) {
2043       rng (a, pgUp, pgDn, max, savePort);
2044       cont = FALSE;
2045     } else {
2046       classPtr = classPtr->ancestor;
2047     }
2048   }
2049 }
2050 
Nlm_DoGainFocus(Nlm_GraphiC a,Nlm_Char ch,Nlm_Boolean savePort)2051 extern Nlm_GraphiC Nlm_DoGainFocus (Nlm_GraphiC a, Nlm_Char ch, Nlm_Boolean savePort)
2052 
2053 {
2054   Nlm_GphPrcsPtr  classPtr;
2055   Nlm_Boolean     cont;
2056   Nlm_GraphiC     rsult;
2057   Nlm_GraphiC     (*gainFcs) PROTO((Nlm_GraphiC, Nlm_Char, Nlm_Boolean));
2058 
2059   cont = TRUE;
2060   rsult = NULL;
2061   classPtr = Nlm_GetClassPtr (a);
2062   while (classPtr != NULL && cont) {
2063     gainFcs = classPtr->gainFocus;
2064     if (gainFcs != NULL) {
2065       rsult = gainFcs (a, ch, savePort);
2066       cont = FALSE;
2067     } else {
2068       classPtr = classPtr->ancestor;
2069     }
2070   }
2071   return rsult;
2072 }
2073 
Nlm_DoLoseFocus(Nlm_GraphiC a,Nlm_GraphiC excpt,Nlm_Boolean savePort)2074 extern void Nlm_DoLoseFocus (Nlm_GraphiC a, Nlm_GraphiC excpt, Nlm_Boolean savePort)
2075 
2076 {
2077   Nlm_GphPrcsPtr  classPtr;
2078   Nlm_Boolean     cont;
2079   void            (*loseFcs) PROTO((Nlm_GraphiC, Nlm_GraphiC, Nlm_Boolean));
2080 
2081   cont = TRUE;
2082   classPtr = Nlm_GetClassPtr (a);
2083   while (classPtr != NULL && cont) {
2084     loseFcs = classPtr->loseFocus;
2085     if (loseFcs != NULL) {
2086       loseFcs (a, excpt, savePort);
2087       cont = FALSE;
2088     } else {
2089       classPtr = classPtr->ancestor;
2090     }
2091   }
2092 }
2093 
Nlm_DoSendChar(Nlm_GraphiC a,Nlm_Char ch,Nlm_Boolean savePort)2094 extern void Nlm_DoSendChar (Nlm_GraphiC a, Nlm_Char ch, Nlm_Boolean savePort)
2095 
2096 {
2097   Nlm_GphPrcsPtr  classPtr;
2098   Nlm_Boolean     cont;
2099   void            (*sendCh) PROTO((Nlm_GraphiC, Nlm_Char, Nlm_Boolean));
2100 
2101   cont = TRUE;
2102   classPtr = Nlm_GetClassPtr (a);
2103   while (classPtr != NULL && cont) {
2104     sendCh = classPtr->sendChar;
2105     if (sendCh != NULL) {
2106       sendCh (a, ch, savePort);
2107       cont = FALSE;
2108     } else {
2109       classPtr = classPtr->ancestor;
2110     }
2111   }
2112 }
2113 
Nlm_CheckThisLevel(Nlm_GraphiC a,Nlm_Char ch)2114 static Nlm_GraphiC Nlm_CheckThisLevel (Nlm_GraphiC a, Nlm_Char ch)
2115 
2116 {
2117   Nlm_GraphiC  p;
2118   Nlm_GraphiC  q;
2119   Nlm_GraphiC  n;
2120 
2121   q = NULL;
2122   p = a;
2123   while (p != NULL && q == NULL) {
2124     n = Nlm_GetNext (p);
2125     q = Nlm_DoGainFocus (p, ch, FALSE);
2126     p = n;
2127   }
2128   return q;
2129 }
2130 
Nlm_DoSendFocus(Nlm_GraphiC a,Nlm_Char ch)2131 extern void Nlm_DoSendFocus (Nlm_GraphiC a, Nlm_Char ch)
2132 
2133 {
2134   Nlm_GraphiC   cur;
2135   Nlm_GraphiC   nxt;
2136   Nlm_GraphiC   q;
2137 
2138   q = NULL;
2139   nxt = (Nlm_GraphiC) a;
2140   while (q == NULL && nxt != NULL) {
2141     cur = nxt;
2142     nxt = Nlm_GetParent (cur);
2143     if (nxt != NULL) {
2144       cur = Nlm_GetNext (cur);
2145     }
2146     q = Nlm_CheckThisLevel (cur, ch);
2147   }
2148 }
2149 
Nlm_DoAction(Nlm_GraphiC a)2150 extern void Nlm_DoAction (Nlm_GraphiC a)
2151 
2152 {
2153   void             (*act) PROTO((Nlm_GraphiC));
2154   Nlm_GraphicData  gdata;
2155 
2156   if (a != NULL) {
2157     Nlm_GetGraphicData (a, &gdata);
2158     act = gdata.action;
2159     if (act != NULL) {
2160       act (a);
2161     }
2162   }
2163 }
2164 
Nlm_Show(Nlm_Handle a)2165 extern void Nlm_Show (Nlm_Handle a)
2166 
2167 {
2168   Nlm_DoShow ((Nlm_GraphiC) a, TRUE, TRUE);
2169 }
2170 
Nlm_Hide(Nlm_Handle a)2171 extern void Nlm_Hide (Nlm_Handle a)
2172 
2173 {
2174   Nlm_DoHide ((Nlm_GraphiC) a, TRUE, TRUE);
2175 }
2176 
Nlm_Enable(Nlm_Handle a)2177 extern void Nlm_Enable (Nlm_Handle a)
2178 
2179 {
2180   Nlm_DoEnable ((Nlm_GraphiC) a, TRUE, TRUE);
2181 }
2182 
Nlm_Disable(Nlm_Handle a)2183 extern void Nlm_Disable (Nlm_Handle a)
2184 
2185 {
2186   Nlm_DoDisable ((Nlm_GraphiC) a, TRUE, TRUE);
2187 }
2188 
Nlm_Remove(Nlm_Handle a)2189 extern Nlm_Handle Nlm_Remove (Nlm_Handle a)
2190 
2191 {
2192   return Nlm_DoRemove ((Nlm_GraphiC) a, TRUE);
2193 }
2194 
Nlm_Reset(Nlm_Handle a)2195 extern void Nlm_Reset (Nlm_Handle a)
2196 
2197 {
2198   Nlm_DoReset ((Nlm_GraphiC) a, TRUE);
2199 }
2200 
Nlm_Select(Nlm_Handle a)2201 extern void Nlm_Select (Nlm_Handle a)
2202 
2203 {
2204   Nlm_DoSelect ((Nlm_GraphiC) a, TRUE);
2205 }
2206 
Nlm_SetTitle(Nlm_Handle a,Nlm_CharPtr title)2207 extern void Nlm_SetTitle (Nlm_Handle a, Nlm_CharPtr title)
2208 
2209 {
2210   Nlm_DoSetTitle ((Nlm_GraphiC) a, 0, title, TRUE);
2211 }
2212 
Nlm_GetTitle(Nlm_Handle a,Nlm_CharPtr title,size_t maxsize)2213 extern void Nlm_GetTitle (Nlm_Handle a, Nlm_CharPtr title, size_t maxsize)
2214 
2215 {
2216   Nlm_DoGetTitle ((Nlm_GraphiC) a, 0, title, maxsize);
2217 }
2218 
Nlm_SetValue(Nlm_Handle a,Nlm_Int2 value)2219 extern void Nlm_SetValue (Nlm_Handle a, Nlm_Int2 value)
2220 
2221 {
2222   Nlm_DoSetValue ((Nlm_GraphiC) a, value, TRUE);
2223 }
2224 
Nlm_GetValue(Nlm_Handle a)2225 extern Nlm_Int2 Nlm_GetValue (Nlm_Handle a)
2226 
2227 {
2228   return (Nlm_DoGetValue ((Nlm_GraphiC) a));
2229 }
2230 
Nlm_SetStatus(Nlm_Handle a,Nlm_Boolean status)2231 extern void Nlm_SetStatus (Nlm_Handle a, Nlm_Boolean status)
2232 
2233 {
2234   Nlm_DoSetStatus ((Nlm_GraphiC) a, 0, status, TRUE);
2235 }
2236 
Nlm_GetStatus(Nlm_Handle a)2237 extern Nlm_Boolean Nlm_GetStatus (Nlm_Handle a)
2238 
2239 {
2240   return (Nlm_DoGetStatus ((Nlm_GraphiC) a, 0));
2241 }
2242 
Nlm_SetOffset(Nlm_Handle a,Nlm_Int2 horiz,Nlm_Int2 vert)2243 extern void Nlm_SetOffset (Nlm_Handle a, Nlm_Int2 horiz, Nlm_Int2 vert)
2244 
2245 {
2246   Nlm_DoSetOffset ((Nlm_GraphiC) a, horiz, vert, TRUE);
2247 }
2248 
Nlm_GetOffset(Nlm_Handle a,Nlm_Int2Ptr horiz,Nlm_Int2Ptr vert)2249 extern void Nlm_GetOffset (Nlm_Handle a, Nlm_Int2Ptr horiz, Nlm_Int2Ptr vert)
2250 
2251 {
2252   Nlm_DoGetOffset ((Nlm_GraphiC) a, horiz, vert);
2253 }
2254 
Nlm_SetPosition(Nlm_Handle a,Nlm_RectPtr r)2255 extern void Nlm_SetPosition (Nlm_Handle a, Nlm_RectPtr r)
2256 
2257 {
2258   Nlm_DoSetPosition ((Nlm_GraphiC) a, r, TRUE, FALSE);
2259 }
2260 
Nlm_GetPosition(Nlm_Handle a,Nlm_RectPtr r)2261 extern void Nlm_GetPosition (Nlm_Handle a, Nlm_RectPtr r)
2262 
2263 {
2264   Nlm_DoGetPosition ((Nlm_GraphiC) a, r);
2265 }
2266 
Nlm_SetRange(Nlm_Handle a,Nlm_Int2 pgUp,Nlm_Int2 pgDn,Nlm_Int2 max)2267 extern void Nlm_SetRange (Nlm_Handle a, Nlm_Int2 pgUp,
2268                           Nlm_Int2 pgDn, Nlm_Int2 max)
2269 
2270 {
2271   Nlm_DoSetRange ((Nlm_GraphiC) a, pgUp, pgDn, max, TRUE);
2272 }
2273 
Nlm_AdjustPrnt(Nlm_Handle a,Nlm_RectPtr r,Nlm_Boolean align)2274 extern void Nlm_AdjustPrnt (Nlm_Handle a, Nlm_RectPtr r, Nlm_Boolean align)
2275 
2276 {
2277   Nlm_DoAdjustPrnt ((Nlm_GraphiC) a, r, align, TRUE);
2278 }
2279 
Nlm_SetItemTitle(Nlm_Handle a,Nlm_Int2 item,Nlm_CharPtr title)2280 extern void Nlm_SetItemTitle (Nlm_Handle a, Nlm_Int2 item, Nlm_CharPtr title)
2281 
2282 {
2283   Nlm_DoSetTitle ((Nlm_GraphiC) a, item, title, TRUE);
2284 }
2285 
Nlm_GetItemTitle(Nlm_Handle a,Nlm_Int2 item,Nlm_CharPtr title,size_t maxsize)2286 extern void Nlm_GetItemTitle (Nlm_Handle a, Nlm_Int2 item, Nlm_CharPtr title, size_t maxsize)
2287 
2288 {
2289   Nlm_DoGetTitle ((Nlm_GraphiC) a, item, title, maxsize);
2290 }
2291 
Nlm_SetItemStatus(Nlm_Handle a,Nlm_Int2 item,Nlm_Boolean status)2292 extern void Nlm_SetItemStatus (Nlm_Handle a, Nlm_Int2 item, Nlm_Boolean status)
2293 
2294 {
2295   Nlm_DoSetStatus ((Nlm_GraphiC) a, item, status, TRUE);
2296 }
2297 
Nlm_GetItemStatus(Nlm_Handle a,Nlm_Int2 item)2298 extern Nlm_Boolean Nlm_GetItemStatus (Nlm_Handle a, Nlm_Int2 item)
2299 
2300 {
2301   return (Nlm_DoGetStatus ((Nlm_GraphiC) a, item));
2302 }
2303 
Nlm_GetNextItem(Nlm_Handle a,Nlm_Int2 prev)2304 extern Nlm_Int2 Nlm_GetNextItem (Nlm_Handle a, Nlm_Int2 prev)
2305 
2306 {
2307   Nlm_GphPrcsPtr  classPtr;
2308   Nlm_Int2        (*cntitm) PROTO((Nlm_GraphiC));
2309   Nlm_Int2        count;
2310   Nlm_Boolean     (*gtsts) PROTO((Nlm_GraphiC, Nlm_Int2));
2311   Nlm_Boolean     found;
2312   Nlm_Int2        rsult;
2313 
2314   rsult = 0;
2315   if (a != NULL && prev >= 0) {
2316     classPtr = Nlm_GetClassPtr ((Nlm_GraphiC) a);
2317     if (classPtr != NULL) {
2318       cntitm = classPtr->countItems;
2319       gtsts = classPtr->getStatus;
2320       if (cntitm != NULL && gtsts != NULL) {
2321         count = cntitm ((Nlm_GraphiC) a);
2322         found = FALSE;
2323         while (prev < count && (! found)) {
2324           prev++;
2325           found = gtsts ((Nlm_GraphiC) a, prev);
2326         }
2327         if (found) {
2328           rsult = prev;
2329         }
2330       }
2331     }
2332   }
2333   return rsult;
2334 }
2335 
Nlm_ClearItemsInGroup(Nlm_GraphiC a,Nlm_GraphiC excpt,Nlm_Boolean savePort)2336 extern void Nlm_ClearItemsInGroup (Nlm_GraphiC a, Nlm_GraphiC excpt, Nlm_Boolean savePort)
2337 
2338 {
2339   Nlm_GraphiC  g;
2340 
2341   if (a != NULL) {
2342     g = Nlm_GetChild (a);
2343     while (g != NULL) {
2344       if (g != excpt) {
2345         Nlm_DoSetStatus (g, 0, FALSE, savePort);
2346       }
2347       g = Nlm_GetNext (g);
2348     }
2349   }
2350 }
2351 
Nlm_CountGroupItems(Nlm_GraphiC a)2352 extern Nlm_Int2 Nlm_CountGroupItems (Nlm_GraphiC a)
2353 
2354 {
2355   Nlm_GraphiC  g;
2356   Nlm_Int2     i;
2357 
2358   i = 0;
2359   if (a != NULL) {
2360     g = Nlm_GetChild (a);
2361     while (g != NULL) {
2362       i++;
2363       g = Nlm_GetNext (g);
2364     }
2365   }
2366   return i;
2367 }
2368 
Nlm_LinkIn(Nlm_GraphiC a,Nlm_GraphiC prnt)2369 extern Nlm_GraphiC Nlm_LinkIn (Nlm_GraphiC a, Nlm_GraphiC prnt)
2370 
2371 {
2372   Nlm_GraphiC  first;
2373   Nlm_GraphiC  g;
2374   Nlm_GphPtr   gp;
2375   Nlm_GraphiC  last;
2376   Nlm_GphPtr   lastp;
2377   Nlm_GraphiC  p;
2378   Nlm_GphPtr   prntp;
2379 
2380   if (prnt != NULL) {
2381     g = (Nlm_GraphiC) a;
2382     prntp = (Nlm_GphPtr) Nlm_HandLock (prnt);
2383     first = prntp->children;
2384     last = prntp->lastChild;
2385     Nlm_HandUnlock (prnt);
2386     if (last != NULL) {
2387       lastp = (Nlm_GphPtr) Nlm_HandLock (last);
2388       lastp->next = g;
2389       Nlm_HandUnlock (last);
2390       prntp = (Nlm_GphPtr) Nlm_HandLock (prnt);
2391       prntp->lastChild = g;
2392       Nlm_HandUnlock (prnt);
2393     } else if (first != NULL) {
2394       p = first;
2395       while (p != NULL) {
2396         last = p;
2397         p = Nlm_GetNext (p);
2398       }
2399       lastp = (Nlm_GphPtr) Nlm_HandLock (last);
2400       lastp->next = g;
2401       Nlm_HandUnlock (last);
2402       prntp = (Nlm_GphPtr) Nlm_HandLock (prnt);
2403       prntp->lastChild = g;
2404       Nlm_HandUnlock (prnt);
2405     } else {
2406       prntp = (Nlm_GphPtr) Nlm_HandLock (prnt);
2407       prntp->children = g;
2408       prntp->lastChild = g;
2409       Nlm_HandUnlock (prnt);
2410     }
2411     gp = (Nlm_GphPtr) Nlm_HandLock (g);
2412     gp->next = NULL;
2413     gp->parent = prnt;
2414     gp->children = NULL;
2415     gp->lastChild = NULL;
2416     Nlm_HandUnlock (g);
2417   }
2418   recentGraphic = NULL;
2419   recentBox = NULL;
2420   return a;
2421 }
2422 
Nlm_LoadAction(Nlm_GraphiC a,Nlm_ActnProc actnProc)2423 extern void Nlm_LoadAction (Nlm_GraphiC a, Nlm_ActnProc actnProc)
2424 
2425 {
2426   Nlm_GraphicData  gdata;
2427 
2428   if (a != NULL) {
2429     Nlm_GetGraphicData (a, &gdata);
2430     gdata.action = actnProc;
2431     Nlm_SetGraphicData (a, &gdata);
2432   }
2433 }
2434 
Nlm_LoadGraphicData(Nlm_GraphiC a,Nlm_GraphiC nxt,Nlm_GraphiC prnt,Nlm_GraphiC chld,Nlm_GraphiC lstchd,Nlm_GphPrcs PNTR classPtr,Nlm_ActnProc actnProc,Nlm_RectPtr r,Nlm_Boolean enabl,Nlm_Boolean vis,Nlm_VoidPtr dat,Nlm_FreeProc cln)2435 extern void Nlm_LoadGraphicData (Nlm_GraphiC a, Nlm_GraphiC nxt,
2436                                  Nlm_GraphiC prnt, Nlm_GraphiC chld,
2437                                  Nlm_GraphiC lstchd, Nlm_GphPrcs PNTR classPtr,
2438                                  Nlm_ActnProc actnProc, Nlm_RectPtr r,
2439                                  Nlm_Boolean enabl, Nlm_Boolean vis,
2440                                  Nlm_VoidPtr dat, Nlm_FreeProc cln)
2441 {
2442   Nlm_GphPtr  ptr;
2443 
2444   if (a != NULL) {
2445     ptr = (Nlm_GphPtr) Nlm_HandLock (a);
2446     ptr->next = nxt;
2447     ptr->parent = prnt;
2448     ptr->children = chld;
2449     ptr->lastChild = lstchd;
2450     ptr->classptr = classPtr;
2451     ptr->action = actnProc;
2452     if (r != NULL) {
2453       ptr->rect = *r;
2454     } else {
2455       Nlm_LoadRect (&(ptr->rect), 0, 0, 0, 0);
2456     }
2457     ptr->enabled = enabl;
2458     ptr->visible = vis;
2459     ptr->data = dat;
2460     ptr->cleanup = cln;
2461     ptr->realized = (Nlm_Boolean)(prnt ? Nlm_GetRealized(prnt) : FALSE);
2462     Nlm_HandUnlock (a);
2463     recentGraphic = NULL;
2464   }
2465 }
2466 
Nlm_LoadBoxData(Nlm_BoX a,Nlm_PoinT nxt,Nlm_PoinT lmt,Nlm_PoinT rst,Nlm_Int2 top,Nlm_Int2 ncol,Nlm_Int2 xMrg,Nlm_Int2 yMrg,Nlm_Int2 xSpc,Nlm_Int2 ySpc,Nlm_Int2 wid,Nlm_Int2 hgt)2467 extern void Nlm_LoadBoxData (Nlm_BoX a, Nlm_PoinT nxt,
2468                              Nlm_PoinT lmt, Nlm_PoinT rst,
2469                              Nlm_Int2 top, Nlm_Int2 ncol,
2470                              Nlm_Int2 xMrg, Nlm_Int2 yMrg,
2471                              Nlm_Int2 xSpc, Nlm_Int2 ySpc,
2472                              Nlm_Int2 wid, Nlm_Int2 hgt)
2473 
2474 {
2475   Nlm_BoxPtr   bp;
2476   Nlm_BoxData  PNTR bptr;
2477 
2478   if (a != NULL) {
2479     bp = (Nlm_BoxPtr) Nlm_HandLock (a);
2480     bptr = &(bp->box);
2481     bptr->nextPoint = nxt;
2482     bptr->limitPoint = lmt;
2483     bptr->resetPoint = rst;
2484     bptr->topRow = top;
2485     bptr->nextCol = ncol;
2486     bptr->xMargin = xMrg;
2487     bptr->yMargin = yMrg;
2488     bptr->xSpacing = xSpc;
2489     bptr->ySpacing = ySpc;
2490     bptr->boxWidth = wid;
2491     bptr->boxHeight = hgt;
2492     Nlm_HandUnlock (a);
2493     recentBox = NULL;
2494   }
2495 }
2496 
Nlm_SetAction(Nlm_Handle a,Nlm_ActnProc actn)2497 extern void Nlm_SetAction (Nlm_Handle a, Nlm_ActnProc actn)
2498 
2499 {
2500   Nlm_GraphiC      g;
2501   Nlm_GraphicData  gdata;
2502 
2503   if (a != NULL) {
2504     g = (Nlm_GraphiC) a;
2505     if (g != NULL) {
2506       Nlm_GetGraphicData (g, &gdata);
2507       gdata.action = actn;
2508       Nlm_SetGraphicData (g, &gdata);
2509     }
2510   }
2511 }
2512 
Nlm_CreateLink(Nlm_GraphiC prnt,Nlm_RectPtr r,Nlm_Int2 recordSize,Nlm_GphPrcs PNTR classPtr)2513 extern Nlm_GraphiC Nlm_CreateLink (Nlm_GraphiC prnt, Nlm_RectPtr r,
2514                                    Nlm_Int2 recordSize, Nlm_GphPrcs PNTR classPtr)
2515 
2516 {
2517   Nlm_GraphiC  a;
2518   Nlm_GraphiC  rsult;
2519 
2520   rsult = NULL;
2521   a = NULL;
2522   if (prnt != NULL) {
2523     a = (Nlm_GraphiC) Nlm_HandNew (recordSize);
2524     if (a != NULL) {
2525       Nlm_LoadGraphicData (a, NULL, prnt, NULL, NULL, classPtr,
2526                            NULL, r, TRUE, FALSE, NULL, NULL);
2527       rsult = Nlm_DoLinkIn (a, prnt);
2528       if (rsult != NULL) {
2529         if (Nlm_nextIdNumber < 32767) {
2530           Nlm_nextIdNumber++;
2531         }
2532       } else {
2533         Nlm_HandFree (a);
2534       }
2535     }
2536   }
2537   return rsult;
2538 }
2539 
Nlm_RemoveLink(Nlm_GraphiC g)2540 extern void Nlm_RemoveLink (Nlm_GraphiC g)
2541 
2542 {
2543   Nlm_GraphiC      prev;
2544   Nlm_GraphicData  gdata;
2545   Nlm_GraphiC      prnt;
2546   Nlm_GphPtr       prntp;
2547 
2548   if (g == NULL)
2549     return;
2550 
2551   /* exclude from the parent's children list */
2552   prnt  = Nlm_GetParent( g );
2553   prntp = (Nlm_GphPtr)Nlm_HandLock( prnt );
2554   prev  = Nlm_GetChild( prnt );
2555   if (g == prev)
2556     {
2557       prntp->children = Nlm_GetNext( g );
2558       if (prntp->lastChild == g)
2559         {
2560           ASSERT ( prntp->children == NULL );
2561           prntp->lastChild = NULL;
2562         }
2563     }
2564   else if (prev != NULL)
2565     {
2566       Nlm_GraphiC next = Nlm_GetNext( prev );
2567       while (next != NULL  &&  next != g)
2568         {
2569           prev = next;
2570           next = Nlm_GetNext( prev );
2571         }
2572       if (next == g)
2573         {
2574           Nlm_GphPtr prev_ptr;
2575           next = Nlm_GetNext( g );
2576           prev_ptr = (Nlm_GphPtr)Nlm_HandLock( prev );
2577           prev_ptr->next = next;
2578           Nlm_HandUnlock( prev );
2579           if (prntp->lastChild == g)
2580             {
2581               ASSERT ( prev != NULL  &&  prev_ptr->next == NULL );
2582               prntp->lastChild = prev;
2583             }
2584         }
2585     }
2586   Nlm_HandUnlock( prnt );
2587 
2588   /* call cleanup procedure;  then dispose the object */
2589   Nlm_GetGraphicData(g, &gdata);
2590   if (gdata.data != NULL  &&  gdata.cleanup != NULL)
2591     gdata.cleanup((Nlm_GraphiC)g, gdata.data);
2592   Nlm_HandFree( g );
2593 
2594   recentGraphic = NULL;
2595   recentBox = NULL;
2596 }
2597 
Nlm_FindItem(Nlm_GraphiC a,Nlm_Int2 item)2598 extern Nlm_GraphiC Nlm_FindItem (Nlm_GraphiC a, Nlm_Int2 item)
2599 
2600 {
2601   Nlm_GraphiC  g;
2602 
2603   g = NULL;
2604   if (a != NULL && item > 0) {
2605     g = Nlm_GetChild (a);
2606     while (g != NULL && item > 1) {
2607       item--;
2608       g = Nlm_GetNext (g);
2609     }
2610   }
2611   return g;
2612 }
2613 
Nlm_GetItemIndex(Nlm_GraphiC a)2614 extern Nlm_Int2 Nlm_GetItemIndex (Nlm_GraphiC a)
2615 
2616 {
2617   Nlm_GraphiC  g;
2618   Nlm_Int2     index;
2619   Nlm_Int2     rsult;
2620 
2621   rsult = 0;
2622   if (a != NULL) {
2623     index = 1;
2624     g = Nlm_GetChild (a);
2625     while (g != NULL && g != a) {
2626       index++;
2627       g = Nlm_GetNext (g);
2628     }
2629     if (g == a) {
2630       rsult = index;
2631     } else {
2632       rsult = 0;
2633     }
2634   }
2635   return rsult;
2636 }
2637 
Nlm_GetAllParentsEnabled(Nlm_GraphiC a)2638 extern Nlm_Boolean Nlm_GetAllParentsEnabled (Nlm_GraphiC a)
2639 
2640 {
2641   Nlm_GraphiC  p;
2642 
2643   p = NULL;
2644   if (a != NULL) {
2645     p = Nlm_GetParent (a);
2646     while (p != NULL && Nlm_GetEnabled (p)) {
2647       p = Nlm_GetParent (p);
2648     }
2649   }
2650   return (Nlm_Boolean) (p == NULL);
2651 }
2652 
Nlm_AllParentsEnabled(Nlm_Handle a)2653 extern Nlm_Boolean Nlm_AllParentsEnabled (Nlm_Handle a)
2654 
2655 {
2656   return Nlm_GetAllParentsEnabled ((Nlm_GraphiC) a);
2657 }
2658 
Nlm_AllParentsButWindowVisible(Nlm_GraphiC a)2659 extern Nlm_Boolean Nlm_AllParentsButWindowVisible (Nlm_GraphiC a)
2660 
2661 {
2662   Nlm_GraphiC  p;
2663 
2664   p = NULL;
2665   if (a != NULL) {
2666     p = Nlm_GetParent (a);
2667     while (p != NULL && Nlm_GetVisible (p)) {
2668       p = Nlm_GetParent (p);
2669     }
2670   }
2671   return (Nlm_Boolean) (p == NULL || Nlm_GetParent (p) == NULL);
2672 }
2673 
Nlm_GetAllParentsVisible(Nlm_GraphiC a)2674 extern Nlm_Boolean Nlm_GetAllParentsVisible (Nlm_GraphiC a)
2675 
2676 {
2677   Nlm_GraphiC  p;
2678 
2679   p = NULL;
2680   if (a != NULL) {
2681     p = Nlm_GetParent (a);
2682     while (p != NULL && Nlm_GetVisible (p)) {
2683       p = Nlm_GetParent (p);
2684     }
2685   }
2686   return (Nlm_Boolean) (p == NULL);
2687 }
2688 
Nlm_AllParentsVisible(Nlm_Handle a)2689 extern Nlm_Boolean Nlm_AllParentsVisible (Nlm_Handle a)
2690 
2691 {
2692   return Nlm_GetAllParentsVisible ((Nlm_GraphiC) a);
2693 }
2694 
Nlm_SetNext(Nlm_GraphiC a,Nlm_GraphiC nxt)2695 extern void Nlm_SetNext (Nlm_GraphiC a, Nlm_GraphiC nxt)
2696 
2697 {
2698   Nlm_GraphicData  gdata;
2699 
2700   if (a != NULL) {
2701     Nlm_GetGraphicData (a, &gdata);
2702     gdata.next = nxt;
2703     Nlm_SetGraphicData (a, &gdata);
2704   }
2705 }
2706 
Nlm_GetNext(Nlm_GraphiC a)2707 extern Nlm_GraphiC Nlm_GetNext (Nlm_GraphiC a)
2708 
2709 {
2710   Nlm_GraphicData  gdata;
2711   Nlm_GraphiC      rsult;
2712 
2713   rsult = NULL;
2714   if (a != NULL) {
2715     Nlm_GetGraphicData (a, &gdata);
2716     rsult = gdata.next;
2717   }
2718   return rsult;
2719 }
2720 
Nlm_SetParent(Nlm_GraphiC a,Nlm_GraphiC prnt)2721 extern void Nlm_SetParent (Nlm_GraphiC a, Nlm_GraphiC prnt)
2722 
2723 {
2724   Nlm_GraphicData  gdata;
2725 
2726   if (a != NULL) {
2727     Nlm_GetGraphicData (a, &gdata);
2728     gdata.parent = prnt;
2729     Nlm_SetGraphicData (a, &gdata);
2730   }
2731 }
2732 
Nlm_GetParent(Nlm_GraphiC a)2733 extern Nlm_GraphiC Nlm_GetParent (Nlm_GraphiC a)
2734 
2735 {
2736   Nlm_GraphicData  gdata;
2737   Nlm_GraphiC      rsult;
2738 
2739   rsult = NULL;
2740   if (a != NULL) {
2741     Nlm_GetGraphicData (a, &gdata);
2742     rsult = gdata.parent;
2743   }
2744   return rsult;
2745 }
2746 
Nlm_Parent(Nlm_Handle a)2747 extern Nlm_Handle Nlm_Parent (Nlm_Handle a)
2748 
2749 {
2750   return Nlm_GetParent ((Nlm_GraphiC) a);
2751 }
2752 
Nlm_SetChild(Nlm_GraphiC a,Nlm_GraphiC chld)2753 extern void Nlm_SetChild (Nlm_GraphiC a, Nlm_GraphiC chld)
2754 
2755 {
2756   Nlm_GraphicData  gdata;
2757 
2758   if (a != NULL) {
2759     Nlm_GetGraphicData (a, &gdata);
2760     gdata.children = chld;
2761     Nlm_SetGraphicData (a, &gdata);
2762   }
2763 }
2764 
Nlm_GetChild(Nlm_GraphiC a)2765 extern Nlm_GraphiC Nlm_GetChild (Nlm_GraphiC a)
2766 
2767 {
2768   Nlm_GraphicData  gdata;
2769   Nlm_GraphiC      rsult;
2770 
2771   rsult = NULL;
2772   if (a != NULL) {
2773     Nlm_GetGraphicData (a, &gdata);
2774     rsult = gdata.children;
2775   }
2776   return rsult;
2777 }
2778 
Nlm_SetRect(Nlm_GraphiC a,Nlm_RectPtr r)2779 extern void Nlm_SetRect (Nlm_GraphiC a, Nlm_RectPtr r)
2780 
2781 {
2782   Nlm_GraphicData  gdata;
2783 
2784   if (a != NULL && r != NULL) {
2785     Nlm_GetGraphicData (a, &gdata);
2786     gdata.rect = *r;
2787     Nlm_SetGraphicData (a, &gdata);
2788   }
2789 }
2790 
Nlm_GetRect(Nlm_GraphiC a,Nlm_RectPtr r)2791 extern void Nlm_GetRect (Nlm_GraphiC a, Nlm_RectPtr r)
2792 
2793 {
2794   Nlm_GraphicData  gdata;
2795 
2796   if (a != NULL && r != NULL) {
2797     Nlm_GetGraphicData (a, &gdata);
2798     *r = gdata.rect;
2799   }
2800 }
2801 
Nlm_ObjectRect(Nlm_Handle a,Nlm_RectPtr r)2802 extern void Nlm_ObjectRect (Nlm_Handle a, Nlm_RectPtr r)
2803 
2804 {
2805   Nlm_GetRect ((Nlm_GraphiC) a, r);
2806 }
2807 
Nlm_InvalObject(Nlm_Handle a)2808 extern void Nlm_InvalObject (Nlm_Handle a)
2809 
2810 {
2811   Nlm_RecT  r;
2812   Nlm_WindoW   tempPort;                     /* M.I */
2813 
2814   if (a != NULL) {
2815     tempPort = Nlm_CurrentWindow ();         /* M.I */
2816     Nlm_GetRect ((Nlm_GraphiC) a, &r);
2817     Nlm_InsetRect (&r, -1, -1);
2818     Nlm_UseWindow( Nlm_ParentWindow( a ) );  /* M.I */
2819     Nlm_InvalRect (&r);
2820     Nlm_RestorePort (tempPort);              /* M.I */
2821   }
2822 }
2823 
Nlm_SetEnabled(Nlm_GraphiC a,Nlm_Boolean enabld)2824 extern void Nlm_SetEnabled (Nlm_GraphiC a, Nlm_Boolean enabld)
2825 
2826 {
2827   Nlm_GraphicData  gdata;
2828 
2829   if (a != NULL) {
2830     Nlm_GetGraphicData (a, &gdata);
2831     gdata.enabled = enabld;
2832     Nlm_SetGraphicData (a, &gdata);
2833   }
2834 }
2835 
Nlm_GetEnabled(Nlm_GraphiC a)2836 extern Nlm_Boolean Nlm_GetEnabled (Nlm_GraphiC a)
2837 
2838 {
2839   Nlm_GraphicData  gdata;
2840   Nlm_Boolean      rsult;
2841 
2842   rsult = FALSE;
2843   if (a != NULL) {
2844     Nlm_GetGraphicData (a, &gdata);
2845     rsult = gdata.enabled;
2846   }
2847   return rsult;
2848 }
2849 
Nlm_Enabled(Nlm_Handle a)2850 extern Nlm_Boolean Nlm_Enabled (Nlm_Handle a)
2851 
2852 {
2853   return Nlm_GetEnabled ((Nlm_GraphiC) a);
2854 }
2855 
Nlm_SetVisible(Nlm_GraphiC a,Nlm_Boolean visibl)2856 extern void Nlm_SetVisible (Nlm_GraphiC a, Nlm_Boolean visibl)
2857 
2858 {
2859   Nlm_GraphicData  gdata;
2860 
2861   if (a != NULL) {
2862     Nlm_GetGraphicData (a, &gdata);
2863     gdata.visible = visibl;
2864     Nlm_SetGraphicData (a, &gdata);
2865   }
2866 }
2867 
Nlm_GetVisible(Nlm_GraphiC a)2868 extern Nlm_Boolean Nlm_GetVisible (Nlm_GraphiC a)
2869 
2870 {
2871   Nlm_GraphicData  gdata;
2872   Nlm_Boolean      rsult;
2873 
2874   rsult = FALSE;
2875   if (a != NULL) {
2876     Nlm_GetGraphicData (a, &gdata);
2877     rsult = gdata.visible;
2878   }
2879   return rsult;
2880 }
2881 
Nlm_Visible(Nlm_Handle a)2882 extern Nlm_Boolean Nlm_Visible (Nlm_Handle a)
2883 
2884 {
2885   return Nlm_GetVisible ((Nlm_GraphiC) a);
2886 }
2887 
Nlm_SetObjectExtra(Nlm_Handle a,Nlm_VoidPtr data,Nlm_FreeProc cleanup)2888 extern void Nlm_SetObjectExtra (Nlm_Handle a, Nlm_VoidPtr data, Nlm_FreeProc cleanup)
2889 
2890 {
2891   Nlm_GraphicData  gdata;
2892 
2893   if (a != NULL) {
2894     Nlm_GetGraphicData ((Nlm_GraphiC) a, &gdata);
2895     gdata.data = data;
2896     gdata.cleanup = cleanup;
2897     Nlm_SetGraphicData ((Nlm_GraphiC) a, &gdata);
2898   }
2899 }
2900 
Nlm_GetObjectExtra(Nlm_Handle a)2901 extern Nlm_VoidPtr Nlm_GetObjectExtra (Nlm_Handle a)
2902 
2903 {
2904   Nlm_GraphicData  gdata;
2905 
2906   if (a != NULL) {
2907     Nlm_GetGraphicData ((Nlm_GraphiC) a, &gdata);
2908     return gdata.data;
2909   } else {
2910     return NULL;
2911   }
2912 }
2913 
Nlm_SetGraphicData(Nlm_GraphiC a,Nlm_GraphicData PNTR gdata)2914 extern void Nlm_SetGraphicData (Nlm_GraphiC a, Nlm_GraphicData PNTR gdata)
2915 
2916 {
2917   Nlm_GphPtr  gp;
2918 
2919   if (a != NULL && gdata != NULL) {
2920     gp = (Nlm_GphPtr) Nlm_HandLock (a);
2921     *gp = *gdata;
2922     Nlm_HandUnlock (a);
2923     recentGraphic = a;
2924     recentGraphicData = *gdata;
2925   }
2926 }
2927 
Nlm_GetGraphicData(Nlm_GraphiC a,Nlm_GraphicData PNTR gdata)2928 extern void Nlm_GetGraphicData (Nlm_GraphiC a, Nlm_GraphicData PNTR gdata)
2929 
2930 {
2931   Nlm_GphPtr  gp;
2932 
2933   if (a != NULL && gdata != NULL) {
2934     if (a == recentGraphic && NLM_RISKY) {
2935       *gdata = recentGraphicData;
2936     } else {
2937       gp = (Nlm_GphPtr) Nlm_HandLock (a);
2938       *gdata = *gp;
2939       Nlm_HandUnlock (a);
2940       recentGraphic = a;
2941       recentGraphicData = *gdata;
2942     }
2943   }
2944 }
2945 
Nlm_SetBoxData(Nlm_BoX a,Nlm_BoxData PNTR bdata)2946 extern void Nlm_SetBoxData (Nlm_BoX a, Nlm_BoxData PNTR bdata)
2947 
2948 {
2949   Nlm_BoxPtr  bp;
2950 
2951   if (a != NULL && bdata != NULL) {
2952     bp = (Nlm_BoxPtr) Nlm_HandLock (a);
2953     bp->box = *bdata;
2954     Nlm_HandUnlock (a);
2955     recentBox = a;
2956     recentBoxData = *bdata;
2957   }
2958 }
2959 
Nlm_GetBoxData(Nlm_BoX a,Nlm_BoxData PNTR bdata)2960 extern void Nlm_GetBoxData (Nlm_BoX a, Nlm_BoxData PNTR bdata)
2961 
2962 {
2963   Nlm_BoxPtr  bp;
2964 
2965   if (a != NULL && bdata != NULL) {
2966     if (a == recentBox && NLM_RISKY) {
2967       *bdata = recentBoxData;
2968     } else {
2969       bp = (Nlm_BoxPtr) Nlm_HandLock (a);
2970       *bdata = bp->box;
2971       Nlm_HandUnlock (a);
2972       recentBox = a;
2973       recentBoxData = *bdata;
2974     }
2975   }
2976 }
2977 
Nlm_SetExtraData(Nlm_GraphiC a,Nlm_VoidPtr dptr,Nlm_Int2 start,Nlm_Int2 extra)2978 extern void Nlm_SetExtraData (Nlm_GraphiC a, Nlm_VoidPtr dptr,
2979                               Nlm_Int2 start, Nlm_Int2 extra)
2980 
2981 {
2982   Nlm_BytePtr  dst;
2983   Nlm_GphPtr   gp;
2984   Nlm_BytePtr  src;
2985 
2986   if (a != NULL && dptr != NULL) {
2987     gp = (Nlm_GphPtr) Nlm_HandLock (a);
2988     dst = (Nlm_BytePtr) gp + start;
2989     src = (Nlm_BytePtr) dptr;
2990     while (extra > 0) {
2991       *dst = *src;
2992       dst++;
2993       src++;
2994       extra--;
2995     }
2996     Nlm_HandUnlock (a);
2997   }
2998 }
2999 
Nlm_GetExtraData(Nlm_GraphiC a,Nlm_VoidPtr dptr,Nlm_Int2 start,Nlm_Int2 extra)3000 extern void Nlm_GetExtraData (Nlm_GraphiC a, Nlm_VoidPtr dptr,
3001                               Nlm_Int2 start, Nlm_Int2 extra)
3002 
3003 {
3004   Nlm_BytePtr  dst;
3005   Nlm_GphPtr   gp;
3006   Nlm_BytePtr  src;
3007 
3008   if (a != NULL && dptr != NULL) {
3009     gp = (Nlm_GphPtr) Nlm_HandLock (a);
3010     src = (Nlm_BytePtr) gp + start;
3011     dst = (Nlm_BytePtr) dptr;
3012     while (extra > 0) {
3013       *dst = *src;
3014       dst++;
3015       src++;
3016       extra--;
3017     }
3018     Nlm_HandUnlock (a);
3019   }
3020 }
3021 
Nlm_PointToolToPoinT(Nlm_PointTool src,Nlm_PointPtr dst)3022 extern void Nlm_PointToolToPoinT (Nlm_PointTool src, Nlm_PointPtr dst)
3023 
3024 {
3025   if (dst != NULL) {
3026 #ifdef WIN_MAC
3027     dst->x = src.h;
3028     dst->y = src.v;
3029 #endif
3030 #ifdef WIN_MSWIN
3031     dst->x = (Nlm_Int2) src.x;
3032     dst->y = (Nlm_Int2) src.y;
3033 #endif
3034 #ifdef WIN_MOTIF
3035     dst->x = src.x;
3036     dst->y = src.y;
3037 #endif
3038   }
3039 }
3040 
Nlm_PoinTToPointTool(Nlm_PoinT src,Nlm_PointTool PNTR dst)3041 extern void Nlm_PoinTToPointTool (Nlm_PoinT src, Nlm_PointTool PNTR dst)
3042 
3043 {
3044   if (dst != NULL) {
3045 #ifdef WIN_MAC
3046     dst->h = src.x;
3047     dst->v = src.y;
3048 #endif
3049 #ifdef WIN_MSWIN
3050     dst->x = src.x;
3051     dst->y = src.y;
3052 #endif
3053 #ifdef WIN_MOTIF
3054     dst->x = src.x;
3055     dst->y = src.y;
3056 #endif
3057   }
3058 }
3059 
Nlm_RectToolToRecT(Nlm_RectTool PNTR src,Nlm_RectPtr dst)3060 extern void Nlm_RectToolToRecT (Nlm_RectTool PNTR src, Nlm_RectPtr dst)
3061 
3062 {
3063   if (dst != NULL && src != NULL) {
3064 #ifdef WIN_MAC
3065     dst->left = src->left;
3066     dst->top = src->top;
3067     dst->right = src->right;
3068     dst->bottom = src->bottom;
3069 #endif
3070 #ifdef WIN_MSWIN
3071     dst->left = (Nlm_Int2) src->left;
3072     dst->top = (Nlm_Int2) src->top;
3073     dst->right = (Nlm_Int2) src->right;
3074     dst->bottom = (Nlm_Int2) src->bottom;
3075 #endif
3076 #ifdef WIN_MOTIF
3077     dst->left = src->x;
3078     dst->top = src->y;
3079     dst->right = src->x + src->width;
3080     dst->bottom = src->y + src->height;
3081 #endif
3082   }
3083 }
3084 
Nlm_RecTToRectTool(Nlm_RectPtr src,Nlm_RectTool PNTR dst)3085 extern void Nlm_RecTToRectTool (Nlm_RectPtr src, Nlm_RectTool PNTR dst)
3086 
3087 {
3088   if (dst != NULL && src != NULL) {
3089 #ifdef WIN_MAC
3090     dst->left = MIN (src->left, src->right);
3091     dst->top = MIN (src->top, src->bottom);
3092     dst->right = MAX (src->left, src->right);
3093     dst->bottom = MAX (src->top, src->bottom);
3094 #endif
3095 #ifdef WIN_MSWIN
3096     dst->left = MIN (src->left, src->right);
3097     dst->top = MIN (src->top, src->bottom);
3098     dst->right = MAX (src->left, src->right);
3099     dst->bottom = MAX (src->top, src->bottom);
3100 #endif
3101 #ifdef WIN_MOTIF
3102     dst->x = MIN (src->left, src->right);
3103     dst->y = MIN (src->top, src->bottom);
3104     dst->width = ABS (src->right - src->left);
3105     dst->height = ABS (src->bottom - src->top);
3106 #endif
3107   }
3108 }
3109 
3110 #ifdef WIN_MAC_QUARTZ
Nlm_RecTToCGRect(Nlm_RecT r)3111 extern CGRect Nlm_RecTToCGRect(Nlm_RecT r)
3112 {
3113     CGRect cgr;
3114 
3115     cgr.origin.x = r.left;
3116     cgr.origin.y = r.top;
3117     cgr.size.width = r.right - r.left;
3118     cgr.size.height = r.bottom - r.top;
3119 
3120     return cgr;
3121 }
3122 
Nlm_CGRectToRecT(CGRect cgr)3123 extern Nlm_RecT Nlm_CGRectToRecT(CGRect cgr)
3124 {
3125     Nlm_RecT r;
3126     r.left   = cgr.origin.x;
3127     r.top    = cgr.origin.y;
3128     r.right  = cgr.size.width  + cgr.origin.x;
3129     r.bottom = cgr.size.height + cgr.origin.y;
3130     return r;
3131 }
3132 
Nlm_RectQDToCG(Rect r)3133 CGRect Nlm_RectQDToCG(Rect r)
3134 {
3135     Nlm_RecT rt;
3136     Nlm_RectToolToRecT (&r, &rt);
3137     return Nlm_RecTToCGRect (rt);
3138 }
3139 
Nlm_RectCGToQD(CGRect r)3140 Rect Nlm_RectCGToQD(CGRect r)
3141 {
3142     Nlm_RecT rt = Nlm_CGRectToRecT (r);
3143     Rect rq;
3144     Nlm_RecTToRectTool (&rt, &rq);
3145     return rq;
3146 }
3147 
Nlm_PoinTToCGPoint(Nlm_PoinT np)3148 extern CGPoint Nlm_PoinTToCGPoint(Nlm_PoinT np)
3149 {
3150     CGPoint qp;
3151     qp.x = (float) np.x;
3152     qp.y = (float) np.y;
3153     return qp;
3154 }
3155 
Nlm_CGPointToPoinT(CGPoint qp)3156 extern Nlm_PoinT Nlm_CGPointToPoinT(CGPoint qp)
3157 {
3158     Nlm_PoinT np;
3159     np.x = (float) qp.x;
3160     np.y = (float) qp.y;
3161     return np;
3162 }
3163 #endif
3164 
3165 
Nlm_LocalToGlobal(Nlm_PointPtr pt)3166 extern void Nlm_LocalToGlobal (Nlm_PointPtr pt)
3167 
3168 {
3169 #ifdef WIN_MAC
3170   Nlm_PointTool  ptool;
3171 
3172   if (pt != NULL) {
3173     Nlm_PoinTToPointTool (*pt, &ptool);
3174 #ifdef WIN_MAC_QUARTZ
3175 /* This function is deprecated on 10.4+ but there is no replacement
3176    available until 10.5, so we have to use it anyway */
3177     QDLocalToGlobalPoint (GetWindowPort(Nlm_GetQuartzCurrentWindow()), &ptool);
3178 #else
3179     LocalToGlobal (&ptool);
3180 #endif
3181     Nlm_PointToolToPoinT (ptool, pt);
3182   }
3183 #endif
3184 #ifdef WIN_MSWIN
3185   Nlm_PointTool  ptool;
3186 
3187   if (pt != NULL && Nlm_currentHWnd != NULL) {
3188     Nlm_PoinTToPointTool (*pt, &ptool);
3189     ClientToScreen (Nlm_currentHWnd, &ptool);
3190     Nlm_PointToolToPoinT (ptool, pt);
3191   }
3192 #endif
3193 #ifdef WIN_MOTIF
3194 #endif
3195 }
3196 
Nlm_GlobalToLocal(Nlm_PointPtr pt)3197 extern void Nlm_GlobalToLocal (Nlm_PointPtr pt)
3198 
3199 {
3200 #ifdef WIN_MAC
3201   Nlm_PointTool  ptool;
3202 
3203   if (pt != NULL) {
3204     Nlm_PoinTToPointTool (*pt, &ptool);
3205 #ifdef WIN_MAC_QUARTZ
3206 /* This function is deprecated on 10.4+ but there is no replacement
3207    available until 10.5, so we have to use it anyway */
3208     QDGlobalToLocalPoint (GetWindowPort(Nlm_GetQuartzCurrentWindow()), &ptool);
3209 #else
3210     GlobalToLocal (&ptool);
3211 #endif
3212     Nlm_PointToolToPoinT (ptool, pt);
3213   }
3214 #endif
3215 #ifdef WIN_MSWIN
3216   Nlm_PointTool  ptool;
3217 
3218   if (pt != NULL && Nlm_currentHWnd != NULL) {
3219     Nlm_PoinTToPointTool (*pt, &ptool);
3220     ScreenToClient (Nlm_currentHWnd, &ptool);
3221     Nlm_PointToolToPoinT (ptool, pt);
3222   }
3223 #endif
3224 #ifdef WIN_MOTIF
3225 #endif
3226 }
3227 
3228 #ifdef WIN_MSWIN
Nlm_VibrantDisabled(void)3229 extern Nlm_Boolean Nlm_VibrantDisabled (void)
3230 
3231 {
3232   if (Nlm_GetAppProperty("disable_vibrant") != NULL) {
3233     disabled_count++;
3234     return TRUE;
3235   }
3236   return FALSE;
3237 }
3238 #endif
3239 
3240 #ifdef WIN_MAC
3241 static PicHandle  picHdl = NULL;
3242 #endif
3243 
3244 #ifdef WIN_MSWIN
3245 static HDC       picHDC = NULL;
3246 static HDC       picWinHDC = NULL;
3247 static HWND      picHWND = NULL;
3248 static Nlm_RecT  picRect;
3249 #endif
3250 
Nlm_CopyWindowImage(void)3251 extern void Nlm_CopyWindowImage (void)
3252 
3253 {
3254   Nlm_RecT    r;
3255   Nlm_WindoW  w;
3256 #ifdef WIN_MSWIN
3257   Nlm_RecT    realr;
3258   HBITMAP     winBitMap, oldBitMap;
3259   HDC         hdcMemory;
3260   HDC         hdcWindow;
3261   int         fStatus = 0;
3262 #endif
3263 
3264   w = Nlm_CurrentWindow ();
3265   if (w != NULL) {
3266     Nlm_ObjectRect (w, &r);
3267 #ifdef WIN_MSWIN
3268     Nlm_SectRect ( &r, &Nlm_screenRect, &realr );
3269     hdcWindow = GetDC(NULL) ;
3270     if ( hdcWindow != NULL ) {
3271       fStatus = 1;
3272       hdcMemory = CreateCompatibleDC(hdcWindow);
3273       if ( hdcMemory != NULL ){
3274         fStatus = 2;
3275         winBitMap = CreateCompatibleBitmap ( hdcWindow,
3276           realr.right - realr.left, realr.bottom - realr.top );
3277         if ( winBitMap != NULL ){
3278           fStatus = 3;
3279           oldBitMap = SelectObject ( hdcMemory, winBitMap );
3280           BitBlt ( hdcMemory, 0, 0, realr.right - realr.left,
3281                    realr.bottom - realr.top,  hdcWindow,
3282                    realr.left, realr.top, SRCCOPY );
3283           SelectObject ( hdcMemory, oldBitMap );
3284           if ( OpenClipboard (Nlm_currentHWnd) ){
3285             EmptyClipboard ();
3286             if ( SetClipboardData (CF_BITMAP, winBitMap) != NULL ){
3287               fStatus = 2;
3288             }
3289             CloseClipboard ();
3290           }
3291         }
3292       }
3293     }
3294     switch ( fStatus ) {
3295       case 3:
3296         DeleteObject ( (HGDIOBJ)winBitMap );
3297       case 2:
3298         DeleteDC (hdcMemory);
3299       case 1:
3300         ReleaseDC (NULL,hdcWindow);
3301     }
3302 #else
3303     Nlm_OffsetRect (&r, -r.left, -r.top);
3304     Nlm_InvalRect (&r);
3305     w = Nlm_StartPicture (&r);
3306     Nlm_Update ();
3307     Nlm_EndPicture (w);
3308 #endif
3309   }
3310 }
3311 
3312 #ifdef WIN_MSWIN
Nlm_GetPicWinHDC(void)3313 extern HDC Nlm_GetPicWinHDC ( void )
3314 
3315 {
3316   return picWinHDC;
3317 }
3318 #endif
3319 
3320 
3321 #ifdef WIN_MAC_QUARTZ
3322 static CFMutableDataRef Nlm_PictureData = 0;
3323 #endif
3324 
Nlm_StartPicture(Nlm_RectPtr r)3325 extern Nlm_WindoW Nlm_StartPicture (Nlm_RectPtr r)
3326 {
3327   Nlm_WindoW    w;
3328 #ifdef WIN_MAC
3329   Nlm_RectTool  rtool;
3330 #endif
3331 
3332   w = Nlm_CurrentWindow ();
3333   if (r == NULL)
3334     return w;
3335 
3336 #ifdef WIN_MAC
3337 #ifdef WIN_MAC_QUARTZ
3338   CGRect cgr = CGRectMake (r->left, r->top, r->right - r->left, r->bottom - r->top);
3339   Nlm_PictureData = CFDataCreateMutable (NULL, 0);
3340 
3341   CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData (Nlm_PictureData);
3342   CGContextRef ctx = CGPDFContextCreate (consumer, &cgr, NULL);
3343   CGContextBeginPage (ctx, &cgr);
3344   Nlm_PushPort (ctx);
3345   CGDataConsumerRelease (consumer);
3346 #else
3347   Nlm_RecTToRectTool (r, &rtool);
3348   picHdl = OpenPicture (&rtool);
3349 #endif
3350 #endif
3351 #ifdef WIN_MSWIN
3352   picRect = *r;
3353   picHWND = Nlm_currentHWnd;
3354   picWinHDC = GetWindowDC(picHWND);
3355   picHDC    = CreateMetaFile (NULL);
3356   if (picHDC != NULL)
3357     {
3358       SetWindowOrgEx(picHDC, picRect.left, picRect.top, NULL);
3359       SetWindowExtEx(picHDC, picRect.right - picRect.left,
3360                      picRect.bottom - picRect.top, NULL);
3361       Nlm_SetPort((HWND) NULL, picHDC);
3362     }
3363 #endif
3364 #ifdef WIN_MOTIF
3365 #endif
3366 
3367   return w;
3368 }
3369 
3370 
Nlm_EndPicture(Nlm_WindoW w)3371 extern void Nlm_EndPicture (Nlm_WindoW w)
3372 
3373 {
3374 #ifdef WIN_MAC
3375 #ifdef WIN_MAC_QUARTZ
3376   CGContextRef ctx = Nlm_PopPort();
3377   CGContextEndPage (ctx);
3378   CGContextRelease (ctx);
3379 
3380   PasteboardRef pasteboard = 0;
3381   PasteboardCreate (kPasteboardClipboard, &pasteboard);
3382   PasteboardPutItemFlavor (pasteboard, (PasteboardItemID)1, CFSTR("com.adobe.pdf"), Nlm_PictureData, 0);
3383   CFRelease (pasteboard);
3384   CFRelease (Nlm_PictureData);
3385 #else
3386   long    len;
3387   PicPtr  picPtr;
3388 
3389   ClosePicture ();
3390 #if TARGET_API_MAC_CARBON
3391   ClearCurrentScrap();
3392 #else
3393   ZeroScrap ();
3394 #endif
3395   Nlm_textScrapFull = FALSE;
3396   if (picHdl != NULL) {
3397     len = GetHandleSize ((Handle) picHdl);
3398     picPtr = (PicPtr) Nlm_HandLock (picHdl);
3399 #if TARGET_API_MAC_CARBON
3400     {
3401       OSStatus status;
3402       ScrapRef scrap;
3403       ScrapFlavorFlags flags = 0;
3404       status = GetCurrentScrap(&scrap);
3405       status = PutScrapFlavor(scrap, kScrapFlavorTypePicture, flags, len, picPtr);
3406     }
3407 #else
3408     { OSErr err = PutScrap (len, 'PICT', (Ptr) picPtr); }
3409 #endif
3410     Nlm_HandUnlock (picHdl);
3411     KillPicture (picHdl);
3412   }
3413   picHdl = NULL;
3414 #endif
3415 #endif
3416 #ifdef WIN_MSWIN
3417   GLOBALHANDLE    hGMem = NULL;
3418   HMETAFILE       hmf = NULL;
3419   LPMETAFILEPICT  lpMFP;
3420   int             fileSend = 0;
3421 
3422   if (picWinHDC != NULL) {
3423     ReleaseDC(picHWND,picWinHDC);
3424     picWinHDC = NULL;
3425   }
3426   if (picHDC != NULL) {
3427     hmf = CloseMetaFile (picHDC);
3428     if ( hmf != NULL ) {
3429       hGMem = GlobalAlloc (GHND, (DWORD) sizeof (METAFILEPICT));
3430       if ( hGMem != NULL ) {
3431         lpMFP = (LPMETAFILEPICT) GlobalLock (hGMem);
3432         if ( lpMFP != NULL ){
3433           lpMFP->mm = MM_ANISOTROPIC;
3434           lpMFP->xExt = (picRect.right - picRect.left) * 25;
3435           lpMFP->yExt = (picRect.bottom - picRect.top) * 25;
3436           lpMFP->hMF = hmf;
3437           GlobalUnlock (hGMem);
3438           if ( OpenClipboard (picHWND) ){
3439             EmptyClipboard ();
3440             if ( SetClipboardData (CF_METAFILEPICT, hGMem) != NULL ){
3441               fileSend = 1;
3442             }
3443             CloseClipboard ();
3444           }
3445         }
3446       }
3447     }
3448   }
3449   if ( !fileSend ){
3450     if ( hGMem != NULL ) GlobalFree ( hGMem );
3451     if ( hmf != NULL ) DeleteMetaFile ( hmf );
3452   }
3453   picHDC = NULL;
3454   picHWND = NULL;
3455 #endif
3456 #ifdef WIN_MOTIF
3457 #endif
3458   if (w != NULL) {
3459     Nlm_UseWindow (w);
3460   }
3461 }
3462 
3463 #ifdef WIN_MOTIF
Nlm_GetAnyX11Window(void)3464 static Window Nlm_GetAnyX11Window (void)
3465 {
3466   Window         wX11;
3467   Nlm_WindoW     wVibrant;
3468   Nlm_ShellTool  wShell;
3469 
3470   wX11   = (Window) 0;
3471   wShell = (Nlm_ShellTool) 0;
3472   wVibrant = Nlm_desktopWindow;
3473   while ( wVibrant != NULL ){
3474     wShell = Nlm_GetWindowShell ( wVibrant );
3475     if ( wShell )
3476       break;
3477     wVibrant = (Nlm_WindoW) Nlm_GetNext ((Nlm_GraphiC)wVibrant);
3478   }
3479   if ( wShell ){
3480     wX11 = XtWindow ( wShell );
3481   }
3482   return wX11;
3483 }
3484 #endif
3485 
Nlm_StringToClipboard(Nlm_CharPtr str)3486 extern void Nlm_StringToClipboard (Nlm_CharPtr str)
3487 
3488 {
3489 #ifdef WIN_MAC
3490 #ifdef WIN_MAC_QUARTZ
3491   PasteboardRef pasteboard = 0;
3492   PasteboardCreate (kPasteboardClipboard, &pasteboard);
3493   PasteboardClear (pasteboard);
3494 
3495   CFDataRef asciidata = CFDataCreate (NULL, (UInt8 *)str, (CFIndex)strlen (str));
3496   CFStringRef cfstr = CFStringCreateFromExternalRepresentation (NULL, asciidata, kCFStringEncodingMacRoman);
3497   CFDataRef data = CFStringCreateExternalRepresentation (NULL, cfstr, kCFStringEncodingUnicode, '?');
3498   PasteboardPutItemFlavor (pasteboard, (PasteboardItemID)1, CFSTR("public.utf16-plain-text"), data, 0);
3499 
3500   CFRelease (data);
3501   CFRelease (cfstr);
3502   CFRelease (asciidata);
3503   CFRelease (pasteboard);
3504 #else
3505   long    len;
3506   OSErr err;
3507 
3508 # if TARGET_API_MAC_CARBON
3509   ClearCurrentScrap();
3510 # else
3511   ZeroScrap ();
3512 # endif
3513   Nlm_textScrapFull = FALSE;
3514   len = (long) Nlm_StringLen (str);
3515   if (len > 0) {
3516 # if TARGET_API_MAC_CARBON
3517     OSStatus status;
3518     ScrapRef scrap;
3519     ScrapFlavorFlags flags = 0;
3520     status = GetCurrentScrap(&scrap);
3521     status = PutScrapFlavor(scrap, kScrapFlavorTypeText, flags, len, str);
3522 # else
3523     PutScrap (len, 'TEXT', (Ptr) str);
3524 # endif
3525     err = TEFromScrap ();
3526   }
3527 #endif
3528 #endif
3529 #ifdef WIN_MSWIN
3530   HGLOBAL  hMem;
3531   DWORD    len;
3532   DWORD    n;
3533   LPSTR    pMem;
3534   LPSTR    pStr;
3535 
3536   if (Nlm_currentHWnd != NULL) {
3537     if (OpenClipboard (Nlm_currentHWnd)) {
3538       EmptyClipboard ();
3539       len = (long) Nlm_StringLen (str);
3540       if (len > 0) {
3541         hMem = GlobalAlloc (GHND, (DWORD) len + 1);
3542         if (hMem != NULL) {
3543           pMem = GlobalLock (hMem);
3544           if (pMem != NULL) {
3545             pStr = (LPSTR) str;
3546             for (n = 0; n < len; n++) {
3547               *pMem++ = *pStr++;
3548             }
3549           }
3550           GlobalUnlock (hMem);
3551           SetClipboardData (CF_TEXT, hMem);
3552         }
3553       }
3554       CloseClipboard ();
3555     }
3556   }
3557 #endif
3558 #ifdef WIN_MOTIF
3559   XmString  clip_label;
3560   long      item_id;
3561   Window    w;
3562   int       status;
3563   char      nchar = 0;
3564 
3565   if (Nlm_currentXDisplay != NULL  &&
3566       (w = Nlm_GetAnyX11Window()) != (Window)0)  {
3567     clip_label = XmStringCreateSimple ( "vibrant_to_clibboard" );
3568     do {
3569       status = XmClipboardStartCopy ( Nlm_currentXDisplay, w,
3570                                       clip_label, CurrentTime, NULL, NULL,
3571                                       &item_id );
3572     } while ( status == ClipboardLocked );
3573     XmStringFree ( clip_label );
3574     do {
3575       if ( str == NULL ){
3576         status = XmClipboardCopy ( Nlm_currentXDisplay, w,
3577                                    item_id, "STRING", &nchar,
3578                                    (long)1,
3579                                    0, NULL );
3580       } else {
3581         status = XmClipboardCopy ( Nlm_currentXDisplay, w,
3582                                    item_id, "STRING", str,
3583                                    (long)Nlm_StringLen(str),
3584                                    0, NULL );
3585       }
3586     } while ( status == ClipboardLocked );
3587     do {
3588       status = XmClipboardEndCopy ( Nlm_currentXDisplay, w,
3589                                     item_id);
3590     } while ( status == ClipboardLocked );
3591   }
3592 #endif
3593 }
3594 
3595 #ifdef WIN_MAC_QUARTZ
Nlm_CopyClipboardTextData(void)3596 static CFDataRef Nlm_CopyClipboardTextData (void)
3597 {
3598   PasteboardRef pasteboard = 0;
3599   PasteboardCreate (kPasteboardClipboard, &pasteboard);
3600   PasteboardSynchronize (pasteboard);
3601 
3602   ItemCount itemCount = 0;
3603   PasteboardGetItemCount (pasteboard, &itemCount);
3604 
3605   CFDataRef flavorData = 0;
3606 
3607   UInt32 index;
3608   for (index = 1; index <= itemCount && !flavorData; index++)
3609   {
3610     PasteboardItemID itemID = 0;
3611     CFArrayRef flavorTypeArray = 0;
3612     CFIndex flavorCount = 0;
3613 
3614     PasteboardGetItemIdentifier (pasteboard, index, &itemID);
3615     PasteboardCopyItemFlavors (pasteboard, itemID, &flavorTypeArray);
3616     flavorCount = CFArrayGetCount (flavorTypeArray);
3617     CFIndex i;
3618     for (i = 0; i < flavorCount; i++)
3619     {
3620       CFStringRef flavorType = (CFStringRef)CFArrayGetValueAtIndex (flavorTypeArray, i);
3621       if (UTTypeConformsTo (flavorType, CFSTR("public.utf16-plain-text")))
3622       {
3623         PasteboardCopyItemFlavorData (pasteboard, itemID, flavorType, &flavorData);
3624         break;
3625       }
3626     }
3627   }
3628 
3629   if (flavorData)
3630   {
3631     CFStringRef cfstr = CFStringCreateFromExternalRepresentation (NULL, flavorData, kCFStringEncodingUnicode);
3632     CFDataRef data = CFStringCreateExternalRepresentation (NULL, cfstr, kCFStringEncodingMacRoman, '?');
3633     CFRelease (flavorData);
3634     CFRelease (cfstr);
3635     return data;
3636   }
3637 
3638   return NULL;
3639 }
3640 #endif
3641 
Nlm_ClipboardHasString(void)3642 extern Nlm_Boolean Nlm_ClipboardHasString (void)
3643 
3644 {
3645 #ifdef WIN_MAC
3646 #ifdef WIN_MAC_QUARTZ
3647   CFDataRef data = Nlm_CopyClipboardTextData();
3648   if (data)
3649     CFRelease (data);
3650   return data != 0;
3651 #else
3652   long  len;
3653   OSErr err;
3654 
3655 # if TARGET_API_MAC_CARBON
3656   OSStatus status;
3657   ScrapRef scrap;
3658   status = GetCurrentScrap(&scrap);
3659 
3660   err = TEFromScrap();
3661   len = TEGetScrapLength();
3662   return (Nlm_Boolean) (len > 0);
3663 #endif
3664 #endif
3665 #endif
3666 
3667 #ifdef WIN_MSWIN
3668   return (Nlm_Boolean) (IsClipboardFormatAvailable (CF_TEXT));
3669 #endif
3670 
3671 #ifdef WIN_MOTIF
3672   Nlm_Boolean   result = FALSE;
3673   Nlm_Char      str[2];
3674   unsigned long len;
3675   unsigned long rlen;
3676   Window        w;
3677   int           status;
3678 
3679   if (Nlm_currentXDisplay != NULL  &&
3680       (w = Nlm_GetAnyX11Window()) != (Window)0)  {
3681     do {
3682       status = XmClipboardLock ( Nlm_currentXDisplay, w );
3683     } while ( status == ClipboardLocked );
3684     status = XmClipboardInquireLength ( Nlm_currentXDisplay, w,
3685                                         "STRING", &len );
3686     if ( (status != ClipboardSuccess) || (len==0) ) {
3687       XmClipboardUnlock ( Nlm_currentXDisplay, w, FALSE );
3688       return result;
3689     }
3690     str[0] = 0;
3691     XmClipboardRetrieve ( Nlm_currentXDisplay, w,
3692                           "STRING", str, 1, &rlen, NULL );
3693     XmClipboardUnlock ( Nlm_currentXDisplay, w, FALSE );
3694     if ( str[0] != 0 ) result = TRUE;
3695   }
3696   return result;
3697 #endif
3698 }
3699 
Nlm_ClipboardToString(void)3700 extern Nlm_CharPtr Nlm_ClipboardToString (void)
3701 
3702 {
3703 #ifdef WIN_MAC
3704 #ifdef WIN_MAC_QUARTZ
3705   Nlm_CharPtr str = 0;
3706 
3707   CFDataRef data = Nlm_CopyClipboardTextData();
3708   if (data)
3709   {
3710     str = Nlm_MemNew (CFDataGetLength (data));
3711     CFDataGetBytes (data, CFRangeMake (0, CFDataGetLength (data)), (UInt8 *)str);
3712     CFRelease (data);
3713   }
3714   return str;
3715 #else
3716   long         len;
3717   Nlm_CharPtr  str = NULL;
3718   Handle hdl;
3719 
3720 # if TARGET_API_MAC_CARBON
3721   TEFromScrap();
3722 # endif
3723   hdl = TEScrapHandle();
3724   len = TEGetScrapLength();
3725   if (len > 0 && len < 32766) {
3726     str = (Nlm_CharPtr) Nlm_MemNew ((size_t) len + 2);
3727     if (str != NULL) {
3728       str [len] = '\0';
3729       if (hdl != NULL) {
3730         Nlm_CharPtr  ptr;
3731         ptr = (Nlm_CharPtr) *hdl;
3732         Nlm_MemCpy (str, ptr, (size_t) len);
3733       }
3734     }
3735   }
3736   return str;
3737 #endif
3738 #endif
3739 #ifdef WIN_MSWIN
3740   HANDLE       hClip;
3741   LPSTR        pStr;
3742   Nlm_CharPtr  str;
3743 
3744   str = NULL;
3745   if (Nlm_currentHWnd != NULL && IsClipboardFormatAvailable (CF_TEXT)) {
3746     if (OpenClipboard (Nlm_currentHWnd)) {
3747       hClip = GetClipboardData (CF_TEXT);
3748       if (hClip != NULL) {
3749         pStr = GlobalLock (hClip);
3750         if (pStr != NULL) {
3751           str = Nlm_StringSave (pStr);
3752         }
3753         GlobalUnlock (hClip);
3754       }
3755       CloseClipboard ();
3756     }
3757   }
3758   return str;
3759 #endif
3760 #ifdef WIN_MOTIF
3761   Nlm_CharPtr   str;
3762   unsigned long len;
3763   unsigned long rlen;
3764   Window        w;
3765   int           status;
3766 
3767   if (Nlm_currentXDisplay != NULL  &&
3768       (w=Nlm_GetAnyX11Window()) != (Window)0)  {
3769     do {
3770       status = XmClipboardLock ( Nlm_currentXDisplay, w );
3771     } while ( status == ClipboardLocked );
3772     status = XmClipboardInquireLength ( Nlm_currentXDisplay, w,
3773                                         "STRING", &len );
3774     if ( status != ClipboardSuccess ) {
3775       XmClipboardUnlock ( Nlm_currentXDisplay, w, FALSE );
3776       return NULL;
3777     }
3778     str = (Nlm_CharPtr)MemNew(len+1);
3779     XmClipboardRetrieve ( Nlm_currentXDisplay, w,
3780                           "STRING", str, len+1, &rlen, NULL );
3781     XmClipboardUnlock ( Nlm_currentXDisplay, w, FALSE );
3782     return str;
3783   }
3784   return NULL;
3785 #endif
3786 }
3787 
3788 #ifdef WIN_MSWIN
Nlm_SetupPrinterDeviceContext(HDC prHDC)3789 static void Nlm_SetupPrinterDeviceContext(HDC prHDC)
3790 {
3791   if ( !prHDC )
3792     return;
3793 
3794   Nlm_SetPort((HWND)0, prHDC);
3795   SetMapMode(prHDC, MM_ANISOTROPIC);
3796 
3797   {{
3798     HWND screenHwnd = GetDesktopWindow();
3799     HDC  screenDC   = GetDC( screenHwnd );
3800 
3801     /* NOTE:  for video devices, the logical inch IS NOT == physical inch! */
3802     SetWindowExtEx(prHDC, GetDeviceCaps(screenDC, LOGPIXELSX),
3803                    GetDeviceCaps(screenDC, LOGPIXELSY), NULL);
3804     SetViewportExtEx(prHDC, GetDeviceCaps(prHDC, LOGPIXELSX),
3805                      GetDeviceCaps(prHDC, LOGPIXELSY), NULL);
3806 
3807     ReleaseDC(screenHwnd, screenDC);
3808   }}
3809 }
3810 #endif
3811 
3812 #ifdef WIN_MAC
3813 # if TARGET_API_MAC_CARBON
3814 
Nlm_StartPrinting(void)3815 extern Nlm_WindoW Nlm_StartPrinting (void)
3816 {
3817   OSStatus status;
3818   Boolean accepted;
3819 
3820 /*  status = PMBegin(); */
3821   status = PMCreateSession(&printSession);
3822   if (status != noErr) return NULL;
3823 /*  status = PMNewPageFormat(&pageFormat); */
3824   status = PMCreatePageFormat(&pageFormat);
3825   if (status != noErr || pageFormat == kPMNoPageFormat) return NULL;
3826 /*  status = PMDefaultPageFormat(pageFormat); */
3827   status = PMSessionDefaultPageFormat(printSession, pageFormat);
3828   if (status != noErr) return NULL;
3829 /*  status = PMPageSetupDialog(pageFormat, &accepted); */
3830   status = PMSessionPageSetupDialog(printSession, pageFormat, &accepted);
3831   if (status != noErr) return NULL;
3832 /*  status = PMNewPrintSettings(&printSettings); */
3833   status = PMCreatePrintSettings(&printSettings);
3834   if (status != noErr || printSettings == kPMNoPrintSettings) return NULL;
3835 /*  status = PMDefaultPrintSettings(printSettings); */
3836   status = PMSessionDefaultPrintSettings (printSession, printSettings);
3837   if (status != noErr) return NULL;
3838 /*  status = PMPrintDialog(printSettings, pageFormat, &accepted); */
3839   status = PMSessionPrintDialog(printSession, printSettings, pageFormat, &accepted);
3840   if (!accepted) status = kPMCancel;
3841   if (status != noErr) return NULL;
3842 
3843 /*  status = PMBeginDocument(printSettings, pageFormat, &thePrintingPort); */
3844   status = PMSessionBeginDocument(printSession, printSettings, pageFormat);
3845   if (status != noErr) return NULL;
3846 
3847   return Nlm_CurrentWindow();
3848 }
3849 
3850 # else  /* not TARGET_API_MAC_CARBON */
3851 
Nlm_StartPrinting(void)3852 extern Nlm_WindoW Nlm_StartPrinting (void)
3853 {
3854   Nlm_WindoW  w = Nlm_CurrentWindow ();
3855 
3856   PrOpen ();
3857   if (prHdl == NULL) {
3858     prHdl = (THPrint) Nlm_HandNew (sizeof (TPrint));
3859     if (prHdl != NULL) {
3860       PrintDefault (prHdl);
3861       prerr = PrError ();
3862       if (prerr != noErr) {
3863         Nlm_Message (MSG_ERROR, "PrintDefault error %d", (int) prerr);
3864       }
3865       if (! PrStlDialog (prHdl)) {
3866         prHdl = Nlm_HandFree (prHdl);
3867         w = NULL;
3868       }
3869     } else {
3870       Nlm_Message (MSG_ERROR, "Unable to create print handle");
3871     }
3872   }
3873   if (prHdl != NULL) {
3874     if (PrJobDialog (prHdl)) {
3875       prPort = PrOpenDoc (prHdl, NULL, NULL);
3876       Nlm_SetPort ((GrafPtr) prPort);
3877       prerr = PrError ();
3878       if (prerr != noErr) {
3879         Nlm_Message (MSG_ERROR, "PrOpenDoc error %d", prerr);
3880       } else {
3881         Nlm_nowPrinting = TRUE;
3882       }
3883     } else {
3884       w = NULL;
3885     }
3886   } else {
3887     w = NULL;
3888   }
3889   return w;
3890 }
3891 # endif  /* not TARGET_API_MAC_CARBON */
3892 #endif  /* WIN_MAC */
3893 
3894 #ifdef WIN_MSWIN
Nlm_StartPrinting(void)3895 extern Nlm_WindoW Nlm_StartPrinting (void)
3896 {
3897   Nlm_WindoW  w = Nlm_CurrentWindow ();
3898   DWORD       commdlgerr;
3899   DOCINFO     di;
3900   char        docName [256];
3901 
3902   abortPrint = FALSE;
3903   memset (&pd, 0, sizeof (PRINTDLG));
3904   pd.lStructSize = sizeof (PRINTDLG);
3905   pd.hwndOwner = Nlm_currentHWnd;
3906   pd.Flags = PD_RETURNDC;
3907   if (PrintDlg (&pd) != 0) {
3908     hPr = pd.hDC;
3909     if (hPr != NULL) {
3910       Nlm_SetupPrinterDeviceContext (hPr);
3911       Nlm_StringCpy (docName, "Vibrant");
3912       Nlm_MemSet(&di, '\0', sizeof(DOCINFO));
3913       di.cbSize = sizeof (DOCINFO);
3914       di.lpszDocName = (LPCSTR) docName;
3915       prerr = StartDoc (hPr, &di);
3916       if (prerr < 1) {
3917         abortPrint = TRUE;
3918         Nlm_Message (MSG_ERROR, "StartDoc error %d", prerr);
3919         w = NULL;
3920       } else {
3921         Nlm_nowPrinting = TRUE;
3922       }
3923     } else {
3924       commdlgerr = CommDlgExtendedError ();
3925       Nlm_Message (MSG_ERROR, "Unable to create print context, error %lu",
3926                    (unsigned long) commdlgerr);
3927       w = NULL;
3928     }
3929   } else {
3930     w = NULL;
3931   }
3932   return w;
3933 }
3934 #endif  /* WIN_MSWIN */
3935 
3936 #ifdef WIN_MOTIF
Nlm_StartPrinting(void)3937 extern Nlm_WindoW Nlm_StartPrinting (void)
3938 {
3939   return NULL;
3940 }
3941 #endif
3942 
3943 
3944 #ifdef WIN_MAC
3945 # if TARGET_API_MAC_CARBON
3946 
Nlm_EndPrinting(Nlm_WindoW w)3947 extern void Nlm_EndPrinting (Nlm_WindoW w)
3948 {
3949   OSStatus status;
3950 
3951   Nlm_nowPrinting = FALSE;
3952   if (w != NULL) {
3953     status = PMSessionError(printSession);
3954     if (status != noErr) {
3955       Nlm_Message (MSG_ERROR, "PM Session error %d", status);
3956     }
3957     /* (void)PMEndDocument(thePrintingPort); */
3958     PMSessionEndDocument (printSession);
3959   }
3960   if (pageFormat != kPMNoPageFormat) {
3961    /* (void)PMDisposePageFormat(pageFormat); */
3962     PMRelease(pageFormat);
3963     pageFormat = kPMNoPageFormat;
3964   }
3965   if (printSettings != kPMNoPrintSettings) {
3966   /*  (void)PMDisposePrintSettings(printSettings); */
3967     PMRelease(printSettings);
3968     printSettings = kPMNoPrintSettings;
3969   }
3970   /* (void)PMEnd(); */
3971   PMRelease(printSession);
3972 }
3973 
3974 # else  /* not TARGET_API_MAC_CARBON */
3975 
Nlm_EndPrinting(Nlm_WindoW w)3976 extern void Nlm_EndPrinting (Nlm_WindoW w)
3977 {
3978   TPrStatus  prStat;
3979 
3980   Nlm_nowPrinting = FALSE;
3981   if (w != NULL) {
3982     PrCloseDoc (prPort);
3983     prerr = PrError ();
3984     if (prerr != noErr) {
3985       Nlm_Message (MSG_ERROR, "PrCloseDoc error %d", prerr);
3986     }
3987     /* This call is not supported under Carbon, need to figure out
3988     how Apple wants us to deal with this ...  churchill 12/28/99 */
3989     PrPicFile (prHdl, 0L, 0L, 0L, &prStat);
3990     prerr = PrError ();
3991     if (prerr != noErr) {
3992       Nlm_Message (MSG_ERROR, "PrPicFile error %d", prerr);
3993     }
3994     prPort = NULL;
3995     Nlm_UseWindow (w);
3996   }
3997   PrClose ();
3998 }
3999 
4000 # endif  /* not TARGET_API_MAC_CARBON */
4001 #endif
4002 
4003 
4004 #ifdef WIN_MSWIN
Nlm_EndPrinting(Nlm_WindoW w)4005 extern void Nlm_EndPrinting (Nlm_WindoW w)
4006 {
4007   Nlm_nowPrinting = FALSE;
4008   if (w != NULL) {
4009     if (hPr != NULL) {
4010       if (! abortPrint) {
4011         prerr = EndDoc (hPr);
4012         if (prerr < 0) {
4013           Nlm_Message (MSG_ERROR, "EndDoc error %d", prerr);
4014         }
4015       }
4016       DeleteDC (hPr);
4017       if (pd.hDevMode != NULL) {
4018         GlobalFree (pd.hDevMode);
4019       }
4020       if (pd.hDevNames != NULL) {
4021         GlobalFree (pd.hDevNames);
4022       }
4023     }
4024     Nlm_UseWindow (w);
4025   }
4026 }
4027 #endif
4028 
4029 #ifdef WIN_MOTIF
Nlm_EndPrinting(Nlm_WindoW w)4030 extern void Nlm_EndPrinting (Nlm_WindoW w)
4031 {
4032   Nlm_nowPrinting = FALSE;
4033 }
4034 #endif
4035 
4036 
4037 #ifdef WIN_MAC
4038 
Nlm_ClipPrintingRect(const Nlm_RecT PNTR rpt)4039 extern Nlm_Boolean Nlm_ClipPrintingRect(const Nlm_RecT PNTR rpt)
4040 {
4041 	Nlm_RecT r;
4042 
4043 	if (rpt == NULL ||
4044 #if TARGET_API_MAC_CARBON
4045 		thePrintingPort == kPMNoReference)
4046 #else
4047 		prHdl == NULL || *prHdl == NULL)
4048 #endif
4049 	{
4050 		return FALSE;
4051 	}
4052 
4053 	r = *rpt;
4054 	Nlm_InsetRect(&r, -1, -1);
4055 
4056 	Nlm_ClipRect( &r );
4057 	return TRUE;
4058 }
4059 
4060 #endif  /* WIN_MAC */
4061 
4062 
4063 #ifdef WIN_MSWIN
4064 
Nlm_ClipPrintingRect(const Nlm_RecT PNTR rpt)4065 extern Nlm_Boolean Nlm_ClipPrintingRect(const Nlm_RecT PNTR rpt)
4066 {
4067   Nlm_RecT r;
4068 
4069   POINT    pt;
4070 
4071   if (rpt == NULL)
4072     return FALSE;
4073 
4074   r = *rpt;
4075   Nlm_InsetRect(&r, -1, -1);
4076 
4077   if (hPr == NULL)
4078     return FALSE;
4079 
4080   pt.x = r.left;
4081   pt.y = r.bottom;
4082   LPtoDP(hPr, &pt, 1);
4083   r.left   = (Nlm_Int2)pt.x;
4084   r.bottom = (Nlm_Int2)pt.y;
4085 
4086   pt.x = r.right;
4087   pt.y = r.top;
4088   LPtoDP(hPr, &pt, 1);
4089   r.right = (Nlm_Int2)pt.x;
4090   r.top   = (Nlm_Int2)pt.y;
4091 
4092   Nlm_ClipRect( &r );
4093   return TRUE;
4094 }
4095 
4096 #endif
4097 
4098 #if !defined(WIN_MAC) && !defined(WIN_MSWIN)
4099 
Nlm_ClipPrintingRect(const Nlm_RecT PNTR rpt)4100 extern Nlm_Boolean Nlm_ClipPrintingRect(const Nlm_RecT PNTR rpt)
4101 {
4102   return FALSE;
4103 }
4104 
4105 #endif
4106 
4107 
4108 #ifdef WIN_MAC
4109 # if TARGET_API_MAC_CARBON
4110 
Nlm_PrintingRect(Nlm_RectPtr rpt)4111 extern Nlm_Boolean Nlm_PrintingRect(Nlm_RectPtr rpt)
4112 {
4113 	OSStatus status;
4114 	PMRect pmRect;
4115 	SInt16 left, top, right, bottom;
4116 
4117 	if (rpt == NULL)
4118 		return FALSE;
4119 
4120 	Nlm_LoadRect (rpt, 0, 0, 0, 0);
4121 
4122 	status = PMGetAdjustedPageRect(pageFormat, &pmRect);
4123 	if (status != noErr) return FALSE;
4124 
4125 	left   = pmRect.left;
4126 	top    = pmRect.top;
4127 	right  = pmRect.right;
4128 	bottom = pmRect.bottom;
4129 
4130 	Nlm_LoadRect (rpt, left, top, right, bottom);
4131 
4132 	return TRUE;
4133 }
4134 
4135 # else  /* not TARGET_API_MAC_CARBON */
4136 
Nlm_PrintingRect(Nlm_RectPtr rpt)4137 extern Nlm_Boolean Nlm_PrintingRect(Nlm_RectPtr rpt)
4138 {
4139   if (rpt == NULL)
4140     return FALSE;
4141 
4142   Nlm_LoadRect (rpt, 0, 0, 0, 0);
4143 
4144   if (prHdl == NULL) {
4145     return FALSE;
4146   } else {
4147     TPPrint       prPtr;
4148     Nlm_RectTool  rtool;
4149     prPtr = (TPPrint)Nlm_HandLock( prHdl );
4150     if (prPtr == NULL)
4151       return FALSE;
4152 
4153     rtool = prPtr->prInfo.rPage;
4154     Nlm_RectToolToRecT(&rtool, rpt);
4155     Nlm_InsetRect(rpt, 10, 10);
4156     Nlm_HandUnlock (prHdl);
4157   }
4158 
4159   return TRUE;
4160 }
4161 
4162 # endif  /* not TARGET_API_MAC_CARBON */
4163 #endif  /* WIN_MAC */
4164 
4165 #ifdef WIN_MSWIN
4166 
Nlm_PrintingRect(Nlm_RectPtr rpt)4167 extern Nlm_Boolean Nlm_PrintingRect(Nlm_RectPtr rpt)
4168 {
4169   POINT         physPageSize;
4170   POINT         pixelsPerInch;
4171   Nlm_PoinT     pt;
4172 
4173   if (rpt == NULL)
4174     return FALSE;
4175 
4176   Nlm_LoadRect (rpt, 0, 0, 0, 0);
4177 
4178   if (hPr == NULL)
4179     return FALSE;
4180 
4181   physPageSize.x = GetDeviceCaps(hPr, HORZRES);
4182   physPageSize.y = GetDeviceCaps(hPr, VERTRES);
4183   DPtoLP(hPr, &physPageSize, 1);
4184   Nlm_PointToolToPoinT(physPageSize, &pt);
4185   rpt->right  = pt.x;
4186   rpt->bottom = pt.y;
4187   pixelsPerInch.x = GetDeviceCaps(hPr, LOGPIXELSX);
4188   pixelsPerInch.y = GetDeviceCaps(hPr, LOGPIXELSY);
4189   DPtoLP(hPr, &pixelsPerInch, 1);
4190   Nlm_InsetRect(rpt, (Nlm_Int2)(pixelsPerInch.x / 4),
4191                      (Nlm_Int2)(pixelsPerInch.y / 4));
4192 
4193   return TRUE;
4194 }
4195 
4196 #endif  /* WIN_MSWIN */
4197 
4198 #ifdef WIN_MOTIF
4199 
Nlm_PrintingRect(Nlm_RectPtr rpt)4200 extern Nlm_Boolean Nlm_PrintingRect(Nlm_RectPtr rpt)
4201 {
4202   if (rpt != NULL)
4203     Nlm_LoadRect (rpt, 0, 0, 0, 0);
4204 
4205   return FALSE;
4206 }
4207 
4208 #endif  /* WIN_MOTIF */
4209 
4210 
4211 #ifdef WIN_MAC
4212 # if TARGET_API_MAC_CARBON
4213 
Nlm_StartPage(void)4214 extern Nlm_Boolean Nlm_StartPage (void)
4215 {
4216   OSStatus status;
4217 
4218 /*  status = PMBeginPage(thePrintingPort, NULL); */
4219   status = PMSessionBeginPage(printSession, pageFormat, NULL);
4220   if (status != noErr) return false;  /* ?? */
4221 
4222   return true;
4223 }
4224 
4225 # else
4226 
Nlm_StartPage(void)4227 extern Nlm_Boolean Nlm_StartPage (void)
4228 {
4229   Nlm_Boolean  rsult;
4230 
4231   rsult = TRUE;
4232   if (prPort != NULL) {
4233     PrOpenPage (prPort, NULL);
4234     prerr = PrError ();
4235     if (prerr != noErr) {
4236       Nlm_Message (MSG_ERROR, "PrOpenPage error %d", prerr);
4237       rsult = FALSE;
4238     }
4239   } else {
4240     rsult = FALSE;
4241   }
4242   return rsult;
4243 }
4244 
4245 # endif
4246 #endif
4247 
4248 
4249 #ifdef WIN_MSWIN
4250 
Nlm_StartPage(void)4251 extern Nlm_Boolean Nlm_StartPage (void)
4252 {
4253   Nlm_Boolean  rsult;
4254 
4255   rsult = TRUE;
4256   if (hPr != NULL) {
4257     Nlm_SetupPrinterDeviceContext (hPr);
4258     prerr = StartPage (hPr);
4259     if (prerr < 0) {
4260       Nlm_Message (MSG_ERROR, "StartPage error %d", prerr);
4261       rsult = FALSE;
4262     }
4263     Nlm_SetPort ((HWND) NULL, (HDC) hPr);
4264   }
4265   return rsult;
4266 }
4267 
4268 #endif
4269 
4270 
4271 #ifdef WIN_MOTIF
4272 
Nlm_StartPage(void)4273 extern Nlm_Boolean Nlm_StartPage (void)
4274 {
4275   return FALSE;
4276 }
4277 
4278 #endif
4279 
4280 
4281 #ifdef WIN_MAC
4282 # if TARGET_API_MAC_CARBON
4283 
Nlm_EndPage(void)4284 extern Nlm_Boolean Nlm_EndPage (void)
4285 {
4286   OSStatus status;
4287 
4288 /*  status = PMEndPage(thePrintingPort); */
4289   status = PMSessionEndPage(printSession);
4290   if (status != noErr) return false;  /* ?? */
4291 
4292   return true;
4293 }
4294 
4295 # else
4296 
Nlm_EndPage(void)4297 extern Nlm_Boolean Nlm_EndPage (void)
4298 {
4299   Nlm_Boolean  rsult;
4300 
4301   rsult = TRUE;
4302   if (prPort != NULL) {
4303     PrClosePage (prPort);
4304     prerr = PrError ();
4305     if (prerr != noErr) {
4306       Nlm_Message (MSG_ERROR, "PrClosePage error %d", prerr);
4307       rsult = FALSE;
4308     }
4309   } else {
4310     rsult = FALSE;
4311   }
4312   return rsult;
4313 }
4314 
4315 # endif
4316 #endif
4317 
4318 
4319 #ifdef WIN_MSWIN
Nlm_EndPage(void)4320 extern Nlm_Boolean Nlm_EndPage (void)
4321 {
4322   Nlm_Boolean  rsult;
4323 
4324   rsult = TRUE;
4325   if (hPr != NULL) {
4326     prerr = EndPage (hPr);
4327     if (prerr < 0) {
4328       abortPrint = TRUE;
4329       Nlm_Message (MSG_ERROR, "EndPage error %d", prerr);
4330       rsult = FALSE;
4331     }
4332   } else {
4333     rsult = FALSE;
4334   }
4335   return rsult;
4336 }
4337 #endif
4338 
4339 #ifdef WIN_MOTIF
Nlm_EndPage(void)4340 extern Nlm_Boolean Nlm_EndPage (void)
4341 {
4342   return FALSE;
4343 }
4344 #endif
4345 
4346 #ifdef WIN_MAC
4347 # if !TARGET_API_MAC_CARBON
Nlm_GetOSType(Nlm_CharPtr str,OSType dfault)4348 static OSType Nlm_GetOSType (Nlm_CharPtr str, OSType dfault)
4349 
4350 {
4351   OSType  rsult;
4352 
4353   rsult = dfault;
4354   if (str != NULL && str [0] != '\0') {
4355     rsult = *(OSType*) str;
4356   }
4357   return rsult;
4358 }
4359 #endif
4360 
4361 /*
4362  2001-03-22:  Joshua Juran
4363  Working directory records are gone in Carbon.
4364  However, so is Standard File.  The code which calls Nav Services instead of SF
4365  doesn't need this function, so we don't define it.
4366 */
4367 # if !TARGET_API_MAC_CARBON
Nlm_GetFilePath(Nlm_Int2 currentVol,Nlm_CharPtr path,size_t maxsize)4368 static void Nlm_GetFilePath (Nlm_Int2 currentVol, Nlm_CharPtr path, size_t maxsize)
4369 
4370 {
4371   WDPBRec     block;
4372   Nlm_Char    directory [256];
4373   Nlm_Int4    dirID;
4374   OSErr       err;
4375   CInfoPBRec  params;
4376   Nlm_Char    temp [256];
4377   Nlm_Int2    vRefNum;
4378 
4379   block.ioNamePtr = NULL;
4380   block.ioVRefNum = currentVol;
4381   block.ioWDIndex = 0;
4382   block.ioWDProcID = 0;
4383   PBGetWDInfoSync(&block);
4384   dirID = block.ioWDDirID;
4385   vRefNum = block.ioWDVRefNum;
4386   temp [0] = '\0';
4387   params.dirInfo.ioNamePtr = (StringPtr) directory;
4388   params.dirInfo.ioDrParID = dirID;
4389   do {
4390     params.dirInfo.ioVRefNum = vRefNum;
4391     params.dirInfo.ioFDirIndex = -1;
4392     params.dirInfo.ioDrDirID = params.dirInfo.ioDrParID;
4393     err = PBGetCatInfo (&params, FALSE);
4394     Nlm_PtoCstr (directory);
4395     Nlm_StrngCat (directory, ":", sizeof (directory));
4396     Nlm_StrngCat (directory, temp, sizeof (directory));
4397     Nlm_StrngCpy (temp, directory, sizeof (temp));
4398   } while (params.dirInfo.ioDrDirID != fsRtDirID);
4399   Nlm_StringNCpy_0 (path, temp, maxsize);
4400 }
4401 # endif  /* !TARGET_API_MAC_CARBON */
4402 #endif
4403 
4404 #ifdef WIN_MOTIF
Nlm_CreateFileDialogShell(void)4405 static void Nlm_CreateFileDialogShell (void)
4406 {
4407   Cardinal  n = 0;
4408   Arg       wargs[8];
4409 
4410   if (Nlm_fileDialogShell != NULL)
4411     return;
4412 
4413   XtSetArg (wargs[n], XmNdefaultFontList, Nlm_XfontList); n++;
4414   XtSetArg (wargs[n], XmNdeleteResponse,  XmDO_NOTHING);  n++;
4415   Nlm_fileDialogShell = XtAppCreateShell((String) NULL, (String) "Vibrant",
4416                                          applicationShellWidgetClass,
4417                                          Nlm_currentXDisplay, wargs, n);
4418 }
4419 
4420 
Nlm_FileCancelCallback(Widget fs,XtPointer client_data,XtPointer call_data)4421 static void Nlm_FileCancelCallback (Widget fs, XtPointer client_data, XtPointer call_data)
4422 
4423 {
4424   fileBoxUp = FALSE;
4425   fileBoxRsult = FALSE;
4426 }
4427 
Nlm_FileOkCallback(Widget fs,XtPointer client_data,XtPointer call_data)4428 static void Nlm_FileOkCallback (Widget fs, XtPointer client_data, XtPointer call_data)
4429 
4430 {
4431   XmFileSelectionBoxCallbackStruct  *cbs;
4432   char                              *filename;
4433 
4434   cbs = (XmFileSelectionBoxCallbackStruct *) call_data;
4435   fileBoxUp = FALSE;
4436   if (! XmStringGetLtoR (cbs->value, XmSTRING_DEFAULT_CHARSET, &filename)) {
4437     return;
4438   }
4439   if (! *filename) {
4440     XtFree (filename);
4441     fileBoxRsult = FALSE;
4442     return;
4443   }
4444   Nlm_StringNCpy_0(filePath, filename, sizeof(filePath));
4445   XtFree (filename);
4446   fileBoxRsult = TRUE;
4447 }
4448 
Nlm_FileNoMatchCallback(Widget fs,XtPointer client_data,XtPointer call_data)4449 static void Nlm_FileNoMatchCallback (Widget fs, XtPointer client_data, XtPointer call_data)
4450 
4451 {
4452   fileBoxUp = FALSE;
4453   fileBoxRsult = FALSE;
4454 }
4455 
Nlm_FileMapCallback(Widget fs,XtPointer client_data,XtPointer call_data)4456 static void Nlm_FileMapCallback (Widget fs, XtPointer client_data, XtPointer call_data)
4457 
4458 {
4459   Position   x, y;
4460   Dimension  w, h;
4461 
4462   XtVaGetValues (fs, XmNwidth, &w, XmNheight, &h, NULL);
4463   x = (Position) (Nlm_screenRect.right - w) / (Position) 2;
4464   y = (Position) (Nlm_screenRect.bottom - h) / (Position) 3;
4465   XtVaSetValues (fs, XmNx, x, XmNy, y, NULL);
4466 }
4467 #endif
4468 
4469 #ifdef WIN_MAC
4470 extern void Nlm_ConvertFilename ( FSSpec *fss, Nlm_CharPtr filename );
4471 
4472 #if TARGET_API_MAC_CARBON
4473 
4474 /* new code calling MoreFiles */
Nlm_ConvertFilename(FSSpec * fss,Nlm_CharPtr filename)4475 extern void Nlm_ConvertFilename ( FSSpec *fss, Nlm_CharPtr filename )
4476 {
4477   OSErr        err;
4478 #ifdef OS_UNIX_DARWIN
4479   const Nlm_Int4    maxPathLen = 256;
4480   FSRef             fsref;
4481 
4482   err = FSpMakeFSRef(fss, &fsref);
4483   if (err == noErr) {
4484     /* if the file exists we can use this. */
4485     err = FSRefMakePath(&fsref, (Nlm_UcharPtr) filename, maxPathLen);
4486   } else if (err == fnfErr) {
4487     /* if the file does not exist we make the path of the parent directory
4488       and tack on the name of the file when done. */
4489     FSSpec parentFSS = *fss;
4490     parentFSS.name[0] =  '\0';
4491     err = FSpMakeFSRef(&parentFSS, &fsref);
4492     if (err == noErr) {
4493       err = FSRefMakePath(&fsref, (Nlm_UcharPtr) filename, maxPathLen);
4494       if (err == noErr) {
4495         Nlm_StrCat(filename, "/");
4496         /* fss->name is a pascal string. */
4497         Nlm_StrNCat(filename, (char *) &fss->name[1], fss->name[0]);
4498       }
4499     }
4500   }
4501 
4502   /* if there were any errors make sure the filename is blank. */
4503   if (err != noErr) {
4504       filename[0] = '\0';
4505   }
4506 
4507 #else // OS_UNIX_DARWIN
4508 
4509   short        fullPathLen;
4510   Handle       fullPath = NULL;
4511   Nlm_CharPtr  str;
4512 
4513   if (fss == NULL || filename == NULL) return;
4514   *filename = '\0';
4515   /*
4516   FSMakePath (fss->vRefNum, fss->parID, (ConstStr255Param) fss->name, (UInt8 *) filename, 255);
4517   */
4518   err = GetFullPath (fss->vRefNum, fss->parID, (ConstStr255Param) fss->name, &fullPathLen, &fullPath);
4519   if (fullPath == NULL || fullPathLen < 1) return;
4520   HLock ((Ptr *) fullPath);
4521   str = (Nlm_CharPtr) *((Ptr *) fullPath);
4522   /*
4523   str = Nlm_HandLock (fullPath);
4524   */
4525   if (str != NULL) {
4526     if (fullPathLen > 0 && fullPathLen < 255) {
4527       str [(int) fullPathLen] = '\0';
4528     }
4529     Nlm_StringCpy (filename, str);
4530   }
4531   HUnlock ((Ptr *) fullPath);
4532   DisposeHandle ((Ptr *) fullPath);
4533   /*
4534   Nlm_HandUnlock (fullPath);
4535   Nlm_HandFree (fullPath);
4536   */
4537 #endif // OS_UNIX_DARWIN
4538 }
4539 
4540 
4541 #else
4542 
4543 /* AppleEvent handlers modified from Roger Sayle's RasMol code */
Nlm_ConvertFilename(FSSpec * fss,Nlm_CharPtr filename)4544 extern void Nlm_ConvertFilename ( FSSpec *fss, Nlm_CharPtr filename )
4545 
4546 {
4547   register char *src;
4548   register char *dst;
4549   register int i;
4550   char buffer [256];
4551 
4552   Str255 dirname;
4553   DirInfo dinfo;
4554 
4555   src = buffer;
4556   dinfo.ioDrParID = fss->parID;
4557   dinfo.ioNamePtr = dirname;
4558   do {
4559     dinfo.ioVRefNum = fss->vRefNum;
4560     dinfo.ioFDirIndex = -1;
4561     dinfo.ioDrDirID = dinfo.ioDrParID;
4562     PBGetCatInfo ((CInfoPBPtr) &dinfo, 0);
4563 
4564     *src++ = ':';
4565     for ( i=dirname[0]; i; i-- )
4566       *src++ = dirname [i];
4567   } while ( dinfo.ioDrDirID != 2 );
4568 
4569   /* Reverse the file path! */
4570   dst = filename;
4571   while ( src != buffer )
4572     *dst++ = *(--src);
4573   for( i = 1; i <= fss->name [0]; i++ )
4574     *dst++ = fss->name [i];
4575   *dst = '\0';
4576 }
4577 
4578 #endif
4579 #endif
4580 
4581 #ifdef WIN_MAC
4582 #if defined(PROC_PPC) || defined(OS_UNIX_DARWIN)
MyNavEventProc(NavEventCallbackMessage callBackSelector,NavCBRecPtr callBackParms,NavCallBackUserData callBackUD)4583 static pascal void MyNavEventProc (NavEventCallbackMessage callBackSelector,
4584                                    NavCBRecPtr callBackParms,
4585                                    NavCallBackUserData callBackUD)
4586 
4587 {
4588 }
4589 
MyNavTextFilterProc(AEDesc * theItem,void * info,NavCallBackUserData callBackUD,NavFilterModes filterMode)4590 static pascal Boolean MyNavTextFilterProc (AEDesc* theItem, void* info,
4591                                            NavCallBackUserData callBackUD,
4592                                            NavFilterModes filterMode)
4593 
4594 {
4595     Boolean display = true;
4596     NavFileOrFolderInfo* theInfo = (NavFileOrFolderInfo*)info;
4597     OSType fdType;
4598 
4599     if (theItem->descriptorType == typeFSS) {
4600         if (!theInfo->isFolder) {
4601             fdType = theInfo->fileAndFolder.fileInfo.finderInfo.fdType;
4602 #ifdef OS_UNIX_DARWIN
4603             /* on Darwin, desired files may not have type set, so also allow type 0 */
4604             if (fdType != 'TEXT' && fdType != 0) {
4605                 display = false;
4606             }
4607 #else
4608             if (fdType != 'TEXT') {
4609                 display = false;
4610             }
4611 #endif
4612         }
4613     }
4614     return display;
4615 }
4616 
MyNavFilterProc(AEDesc * theItem,void * info,NavCallBackUserData callBackUD,NavFilterModes filterMode)4617 static pascal Boolean MyNavFilterProc (AEDesc* theItem, void* info,
4618                                        NavCallBackUserData callBackUD,
4619                                        NavFilterModes filterMode)
4620 
4621 {
4622   return true;
4623 }
4624 #endif
4625 #endif
4626 
4627 #ifdef WIN_MAC
4628 #if defined(PROC_PPC) || defined(OS_UNIX_DARWIN)
Nlm_NavServGetInputFileName(Nlm_CharPtr fileName,size_t maxsize,Nlm_CharPtr extType,Nlm_CharPtr macType)4629 static Nlm_Boolean Nlm_NavServGetInputFileName (Nlm_CharPtr fileName, size_t maxsize,
4630                                                 Nlm_CharPtr extType, Nlm_CharPtr macType)
4631 
4632 {
4633     NavDialogOptions    dialogOptions;
4634     AEDesc              defaultLocation;
4635     NavEventUPP         eventProc = NewNavEventUPP(MyNavEventProc);
4636     NavObjectFilterUPP  filterProc;
4637     OSErr               anErr = noErr;
4638     FSSpec              fss;
4639 	char                filename [256];
4640 	Nlm_Boolean         rsult = FALSE;
4641 
4642     if (StringCmp (macType, "TEXT") == 0) {
4643         filterProc = NewNavObjectFilterUPP(MyNavTextFilterProc);
4644     } else {
4645         filterProc = NewNavObjectFilterUPP(MyNavFilterProc);
4646     }
4647 
4648     /*  Specify default options for dialog box */
4649     anErr = NavGetDefaultDialogOptions(&dialogOptions);
4650     if (anErr == noErr)
4651     {
4652         /*  Adjust the options to fit our needs */
4653         /*  Set this option */
4654         dialogOptions.dialogOptionFlags |= kNavSelectDefaultLocation;
4655         /*  Clear this one */
4656         dialogOptions.dialogOptionFlags ^= kNavAllowPreviews;
4657 
4658         anErr = AECreateDesc(typeFSS, &fss,
4659                              sizeof(fss),
4660                              &defaultLocation );
4661         if (anErr == noErr)
4662         {
4663             /* Get 'open' resource. A nil handle being returned is OK,
4664                this simply means no automatic file filtering. */
4665             NavTypeListHandle typeList = (NavTypeListHandle)GetResource(
4666                                         'open', 128);
4667             NavReplyRecord reply;
4668 
4669             anErr = NavChooseFile (&defaultLocation, &reply, &dialogOptions,
4670                                    eventProc, nil, filterProc,
4671                                    typeList, 0);
4672             if (anErr == noErr && reply.validRecord)
4673             {
4674                 /*  Deal with multiple file selection */
4675                 long    count;
4676 
4677                 anErr = AECountItems(&(reply.selection), &count);
4678                 if (count > 1) {
4679                   count = 1;  /* force to 1 */
4680                 }
4681                 if (anErr == noErr)
4682                 {
4683                     long index;
4684 
4685                     for (index = 1; index <= count; index++)
4686                     {
4687                         AEKeyword   theKeyword;
4688                         DescType    actualType;
4689                         Size        actualSize;
4690                         FSSpec      documentFSSpec;
4691 
4692                         anErr = AEGetNthPtr(&(reply.selection), index,
4693                                             typeFSS, &theKeyword,
4694                                             &actualType,&documentFSSpec,
4695                                             sizeof(documentFSSpec),
4696                                             &actualSize);
4697                         if (anErr == noErr)
4698                         {
4699                             Nlm_ConvertFilename (&documentFSSpec, filename);
4700                             Nlm_StringNCpy_0 (fileName, filename, maxsize);
4701                             rsult = TRUE;
4702                         }
4703                     }
4704                 }
4705                 /*  Dispose of NavReplyRecord, resources, descriptors */
4706                 anErr = NavDisposeReply(&reply);
4707             }
4708             if (typeList != NULL)
4709             {
4710                 ReleaseResource( (Handle)typeList);
4711             }
4712             (void) AEDisposeDesc(&defaultLocation);
4713         }
4714     }
4715     DisposeNavEventUPP(eventProc);
4716     DisposeNavObjectFilterUPP(filterProc);
4717     return rsult;
4718 }
4719 #endif
4720 #endif
4721 
4722 #ifdef WIN_MSWIN
4723 
4724 #ifndef TTM_POP
4725 #define TTM_POP                 (WM_USER + 28)
4726 #endif
4727 
StopToolTips(int nCode,WPARAM wParam,LPARAM lParam)4728 static LRESULT CALLBACK StopToolTips(int nCode,
4729     WPARAM wParam,
4730     LPARAM lParam
4731 )
4732 {
4733 	PCWPSTRUCT msg;
4734 
4735 	msg = (PCWPSTRUCT) lParam;
4736 
4737 	if (nCode < 0) {
4738 		return CallNextHookEx (NULL, nCode, wParam, lParam);
4739 	}
4740 	if (msg != NULL) {
4741 
4742 		if (msg->message > TTM_ACTIVATE
4743 #ifdef TTM_GETTITLE
4744 			&& msg->message <= TTM_GETTITLE
4745 #elif defined(TTM_SETTITLEW)
4746 			&& msg->message <= TTM_SETTITLEW
4747 #elif defined(TTM_UPDATE)
4748 			&& msg->message <= TTM_UPDATE
4749 #else
4750 			&& msg->message <= TTM_POP
4751 #endif
4752 			&& msg->message != TTM_RELAYEVENT
4753 			&& msg->message != TTM_GETTEXTA) {
4754 			SendMessage (msg->hwnd, TTM_ACTIVATE, 0, 0);
4755 			return 1;
4756 		}
4757 	}
4758     return 0;
4759 }
4760 
4761 #endif
4762 
Nlm_GetInputFileName(Nlm_CharPtr fileName,size_t maxsize,Nlm_CharPtr extType,Nlm_CharPtr macType)4763 extern Nlm_Boolean Nlm_GetInputFileName (Nlm_CharPtr fileName, size_t maxsize,
4764                                          Nlm_CharPtr extType, Nlm_CharPtr macType)
4765 
4766 {
4767 #ifdef WIN_MAC
4768 # if TARGET_API_MAC_CARBON
4769     return Nlm_NavServGetInputFileName (fileName, maxsize, extType, macType);
4770 # else
4771   Nlm_Char       currentFileName [64];
4772   Nlm_Char       currentPath [256];
4773   SFTypeList     fTypeList;
4774   Nlm_Int2       i;
4775   Nlm_Int2       lengthTypes;
4776   Nlm_Int2       numTypes;
4777   Nlm_PointTool  ptool;
4778   Nlm_RecT       r;
4779   SFReply        reply;
4780   Nlm_Boolean    rsult;
4781   Nlm_RectTool   rtool;
4782   GrafPtr        tempPort;
4783   PenState       state;
4784   Nlm_Char       str [5];
4785   Nlm_PoinT      where;
4786 
4787   if (Nlm_usesMacNavServices) {
4788     return Nlm_NavServGetInputFileName (fileName, maxsize, extType, macType);
4789   }
4790   where.x = 90;
4791   where.y = 100;
4792   lengthTypes = sizeof (fileTypes);
4793   for (i = 0; i < lengthTypes; i++) {
4794     fileTypes [i] = '\0';
4795   }
4796   if (macType != NULL && macType [0] != '\0') {
4797     Nlm_StringNCpy (fileTypes, macType, 6); /* remains StringNCpy, not _0 */
4798     fileTypes [4] = '\0';
4799   }
4800   if (fileTypes [0] != '\0') {
4801     numTypes = 0;
4802     i = 0;
4803     while (numTypes < 4 && i <= lengthTypes) {
4804       Nlm_StrngSeg (str, fileTypes, i, 4, sizeof (str));
4805       fTypeList [numTypes] = Nlm_GetOSType (str, '    ');
4806       numTypes++;
4807       i += 4;
4808     }
4809   } else {
4810     numTypes = -1;
4811   }
4812   GetPenState (&state);
4813   GetPort (&tempPort);
4814   Nlm_PoinTToPointTool (where, &ptool);
4815   SFGetFile (ptool, NULL, NULL, numTypes, fTypeList, NULL, &reply);
4816   SetPort (tempPort);
4817   SetPenState (&state);
4818   Nlm_GetRect ((Nlm_GraphiC) Nlm_desktopWindow, &r);
4819   Nlm_RecTToRectTool (&r, &rtool);
4820   ClipRect (&rtool);
4821   if (reply.good != 0 && fileName != NULL  &&  maxsize > 0) {
4822     Nlm_StringNCpy_0(currentFileName, (Nlm_CharPtr) &(reply.fName),
4823                      sizeof(currentFileName));
4824     Nlm_PtoCstr (currentFileName);
4825     currentPath[0] = '\0';
4826     Nlm_GetFilePath (reply.vRefNum, currentPath, sizeof (currentPath));
4827     Nlm_StringNCat(currentPath, currentFileName,
4828                    sizeof(currentPath) - Nlm_StringLen(currentPath) - 1);
4829     Nlm_StringNCpy_0(fileName, currentPath, maxsize);
4830     rsult = TRUE;
4831   } else {
4832     rsult = FALSE;
4833   }
4834   Nlm_Update ();
4835   return rsult;
4836 # endif
4837 #endif	/* WIN_MAC */
4838 
4839 #ifdef WIN_MSWIN
4840   char  szDirName [256];
4841   char  szFile [256];
4842   char  szFileTitle [256];
4843   UINT  i;
4844   UINT  cbString;
4845   char  chReplace;
4846   char  szFilter [256];
4847   Nlm_Boolean rval;
4848   HHOOK   hook;
4849   DWORD thread_id;
4850 
4851   /* Get the current working directory: */
4852   szDirName[0] = '\0';
4853   _getcwd(szDirName, sizeof (szDirName));
4854   szFile [0] = '\0';
4855   memset (szFilter, 0, sizeof(szFilter));
4856   if (extType != NULL && extType [0] != '\0') {
4857     Nlm_StringCpy (szFilter, "Filtered Files (*");
4858     if (extType [0] != '.') {
4859       Nlm_StringCat (szFilter, ".");
4860     }
4861     Nlm_StringNCat (szFilter, extType, 5);
4862     Nlm_StringCat (szFilter, ")|*");
4863     if (extType [0] != '.') {
4864       Nlm_StringCat (szFilter, ".");
4865     }
4866     Nlm_StringNCat (szFilter, extType, 5);
4867     Nlm_StringCat (szFilter, "|");
4868   } else {
4869     Nlm_StringCpy (szFilter, "All Files (*.*)|*.*|");
4870   }
4871   cbString = (UINT) Nlm_StringLen (szFilter);
4872   chReplace = szFilter [cbString - 1];
4873   for (i = 0; szFilter [i] != '\0'; i++) {
4874     if (szFilter [i] == chReplace) {
4875       szFilter [i] = '\0';
4876     }
4877   }
4878   memset (&ofn, 0, sizeof(OPENFILENAME));
4879   ofn.lStructSize = sizeof(OPENFILENAME);
4880   ofn.hwndOwner = Nlm_currentHWnd;
4881   ofn.lpstrFilter = szFilter;
4882   ofn.nFilterIndex = 1;
4883   ofn.lpstrFile = szFile;
4884   ofn.nMaxFile = sizeof (szFile);
4885   ofn.lpstrFileTitle = szFileTitle;
4886   ofn.nMaxFileTitle = sizeof (szFileTitle);
4887   ofn.lpstrInitialDir = szDirName;
4888   ofn.Flags =
4889     OFN_SHOWHELP | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
4890   /* turn off tooltips, because they were causing Windows 2000 to crash when opening desktop files */
4891   thread_id = GetCurrentThreadId();
4892   hook = SetWindowsHookEx (WH_CALLWNDPROC, StopToolTips, NULL, thread_id);
4893   if (GetOpenFileName (&ofn) && fileName != NULL  &&  maxsize > 0) {
4894     Nlm_StringNCpy_0(fileName, ofn.lpstrFile, maxsize);
4895     AnsiToOemBuff (fileName, fileName, maxsize);
4896 
4897     rval = TRUE;
4898   } else {
4899     rval = FALSE;
4900   }
4901   UnhookWindowsHookEx (hook);
4902   return rval;
4903 #endif
4904 
4905 #ifdef WIN_MOTIF
4906   XmString  dirmask;
4907   char      *lastSlash;
4908   char      str [256];
4909   char      *text;
4910   Widget    txt;
4911 
4912   Nlm_CreateFileDialogShell ();
4913   if (Nlm_fileDialogShell != NULL && fileName != NULL) {
4914     if (fileDialog == NULL) {
4915       Cardinal  n = 0;
4916       Arg       wargs[4];
4917       XtSetArg (wargs[n], XmNcolormap, Nlm_VibrantDefaultColormap());  n++;
4918       XtSetArg (wargs[n], XmNvisual,   Nlm_VibrantDefaultVisual  ());  n++;
4919       XtSetArg (wargs[n], XmNdepth,    Nlm_VibrantDefaultDepth   ());  n++;
4920       fileDialog = XmCreateFileSelectionDialog (Nlm_fileDialogShell,
4921                                                 (String) "file_selection", wargs, n);
4922       XtAddCallback (fileDialog, XmNcancelCallback, Nlm_FileCancelCallback, NULL);
4923       XtAddCallback (fileDialog, XmNokCallback, Nlm_FileOkCallback, NULL);
4924       XtAddCallback (fileDialog, XmNnoMatchCallback, Nlm_FileNoMatchCallback, NULL);
4925       XtAddCallback (fileDialog, XmNmapCallback, Nlm_FileMapCallback, NULL);
4926       XtVaSetValues (fileDialog, XmNdefaultPosition, FALSE,
4927                      XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL, NULL);
4928     }
4929     if (fileDialog != NULL) {
4930       txt = XmFileSelectionBoxGetChild (fileDialog, XmDIALOG_FILTER_TEXT);
4931       text = XmTextGetString (txt);
4932       Nlm_StringNCpy_0(str, text, sizeof(str));
4933       lastSlash = Nlm_StringRChr (str, DIRDELIMCHR);
4934       if (lastSlash != NULL) {
4935         lastSlash [1] = '\0';
4936       } else {
4937         lastSlash = &(str [Nlm_StringLen (str) - 1]);
4938       }
4939       if (extType != NULL && extType [0] != '\0') {
4940         Nlm_StringCat (str, "*");
4941         if (extType [0] != '.') {
4942           Nlm_StringCat (str, ".");
4943         }
4944         Nlm_StringNCat (str, extType, 5);
4945         dirmask = XmStringCreateLtoR (str, XmSTRING_DEFAULT_CHARSET);
4946         XmFileSelectionDoSearch (fileDialog, dirmask);
4947         XmStringFree (dirmask);
4948         if (lastSlash != NULL) {
4949           lastSlash [1] = '\0';
4950         }
4951       }
4952       XtFree (text);
4953       txt = XmFileSelectionBoxGetChild (fileDialog, XmDIALOG_TEXT);
4954       XmTextSetString (txt, str);
4955       XtVaSetValues (fileDialog, XmNmustMatch, TRUE, NULL);
4956       fileBoxUp = TRUE;
4957       fileBoxRsult = FALSE;
4958       XtManageChild (fileDialog);
4959       XSync (Nlm_currentXDisplay, FALSE);
4960       while (fileBoxUp) {
4961         XEvent event;
4962         XPeekEvent(Nlm_currentXDisplay, &event);
4963         Nlm_ProcessAnEvent ();
4964       }
4965       if (fileBoxRsult) {
4966         Nlm_StringNCpy_0(fileName, filePath, maxsize);
4967       }
4968       XtUnmanageChild (fileDialog);
4969       XSync (Nlm_currentXDisplay, FALSE);
4970     }
4971   }
4972   return fileBoxRsult;
4973 #endif
4974 }
4975 
Nlm_GetInputFileNameEx(Nlm_CharPtr fileName,size_t maxsize,Nlm_CharPtr extType,Nlm_CharPtr macType,Nlm_CharPtr initialDir)4976 extern Nlm_Boolean Nlm_GetInputFileNameEx(Nlm_CharPtr fileName, size_t maxsize,
4977 	Nlm_CharPtr extType, Nlm_CharPtr macType,
4978 	Nlm_CharPtr initialDir)
4979 
4980 {
4981 	/* if initialDir is NULL, use cwd */
4982 
4983 #ifdef WIN_MAC
4984 # if TARGET_API_MAC_CARBON
4985 	return Nlm_NavServGetInputFileName(fileName, maxsize, extType, macType);
4986 # else
4987 	Nlm_Char       currentFileName[64];
4988 	Nlm_Char       currentPath[256];
4989 	SFTypeList     fTypeList;
4990 	Nlm_Int2       i;
4991 	Nlm_Int2       lengthTypes;
4992 	Nlm_Int2       numTypes;
4993 	Nlm_PointTool  ptool;
4994 	Nlm_RecT       r;
4995 	SFReply        reply;
4996 	Nlm_Boolean    rsult;
4997 	Nlm_RectTool   rtool;
4998 	GrafPtr        tempPort;
4999 	PenState       state;
5000 	Nlm_Char       str[5];
5001 	Nlm_PoinT      where;
5002 
5003 	if (Nlm_usesMacNavServices) {
5004 		return Nlm_NavServGetInputFileName(fileName, maxsize, extType, macType);
5005 	}
5006 	where.x = 90;
5007 	where.y = 100;
5008 	lengthTypes = sizeof(fileTypes);
5009 	for (i = 0; i < lengthTypes; i++) {
5010 		fileTypes[i] = '\0';
5011 	}
5012 	if (macType != NULL && macType[0] != '\0') {
5013 		Nlm_StringNCpy(fileTypes, macType, 6); /* remains StringNCpy, not _0 */
5014 		fileTypes[4] = '\0';
5015 	}
5016 	if (fileTypes[0] != '\0') {
5017 		numTypes = 0;
5018 		i = 0;
5019 		while (numTypes < 4 && i <= lengthTypes) {
5020 			Nlm_StrngSeg(str, fileTypes, i, 4, sizeof(str));
5021 			fTypeList[numTypes] = Nlm_GetOSType(str, '    ');
5022 			numTypes++;
5023 			i += 4;
5024 		}
5025 	}
5026 	else {
5027 		numTypes = -1;
5028 	}
5029 	GetPenState(&state);
5030 	GetPort(&tempPort);
5031 	Nlm_PoinTToPointTool(where, &ptool);
5032 	SFGetFile(ptool, NULL, NULL, numTypes, fTypeList, NULL, &reply);
5033 	SetPort(tempPort);
5034 	SetPenState(&state);
5035 	Nlm_GetRect((Nlm_GraphiC)Nlm_desktopWindow, &r);
5036 	Nlm_RecTToRectTool(&r, &rtool);
5037 	ClipRect(&rtool);
5038 	if (reply.good != 0 && fileName != NULL  &&  maxsize > 0) {
5039 		Nlm_StringNCpy_0(currentFileName, (Nlm_CharPtr)&(reply.fName),
5040 			sizeof(currentFileName));
5041 		Nlm_PtoCstr(currentFileName);
5042 		currentPath[0] = '\0';
5043 		Nlm_GetFilePath(reply.vRefNum, currentPath, sizeof(currentPath));
5044 		Nlm_StringNCat(currentPath, currentFileName,
5045 			sizeof(currentPath) - Nlm_StringLen(currentPath) - 1);
5046 		Nlm_StringNCpy_0(fileName, currentPath, maxsize);
5047 		rsult = TRUE;
5048 	}
5049 	else {
5050 		rsult = FALSE;
5051 	}
5052 	Nlm_Update();
5053 	return rsult;
5054 # endif
5055 #endif	/* WIN_MAC */
5056 
5057 #ifdef WIN_MSWIN
5058 	char  szDirName[256];
5059 	char  szFile[256];
5060 	char  szFileTitle[256];
5061 	UINT  i;
5062 	UINT  cbString;
5063 	char  chReplace;
5064 	char  szFilter[256];
5065 
5066 	/* Get the current working directory: */
5067 	szDirName[0] = '\0';
5068 	_getcwd(szDirName, sizeof(szDirName));
5069 	szFile[0] = '\0';
5070 	if (extType != NULL && extType[0] != '\0') {
5071 		Nlm_StringCpy(szFilter, "Filtered Files (*");
5072 		if (extType[0] != '.') {
5073 			Nlm_StringCat(szFilter, ".");
5074 		}
5075 		Nlm_StringNCat(szFilter, extType, 5);
5076 		Nlm_StringCat(szFilter, ")|*");
5077 		if (extType[0] != '.') {
5078 			Nlm_StringCat(szFilter, ".");
5079 		}
5080 		Nlm_StringNCat(szFilter, extType, 5);
5081 		Nlm_StringCat(szFilter, "|");
5082 	}
5083 	else {
5084 		Nlm_StringCpy(szFilter, "All Files (*.*)|*.*|");
5085 	}
5086 	cbString = (UINT)Nlm_StringLen(szFilter);
5087 	chReplace = szFilter[cbString - 1];
5088 	for (i = 0; szFilter[i] != '\0'; i++) {
5089 		if (szFilter[i] == chReplace) {
5090 			szFilter[i] = '\0';
5091 		}
5092 	}
5093 	memset(&ofn, 0, sizeof(OPENFILENAME));
5094 	ofn.lStructSize = sizeof(OPENFILENAME);
5095 	ofn.hwndOwner = Nlm_currentHWnd;
5096 	ofn.lpstrFilter = szFilter;
5097 	ofn.nFilterIndex = 1;
5098 	ofn.lpstrFile = szFile;
5099 	ofn.nMaxFile = sizeof(szFile);
5100 	ofn.lpstrFileTitle = szFileTitle;
5101 	ofn.nMaxFileTitle = sizeof(szFileTitle);
5102 
5103 	if (initialDir != NULL) {
5104 		ofn.lpstrInitialDir = initialDir;
5105 	}
5106 	else {
5107 		ofn.lpstrInitialDir = szDirName;
5108 	}
5109 
5110 	ofn.Flags =
5111 		OFN_SHOWHELP | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
5112 	if (GetOpenFileName(&ofn) && fileName != NULL  &&  maxsize > 0) {
5113 		Nlm_StringNCpy_0(fileName, ofn.lpstrFile, maxsize);
5114 		AnsiToOemBuff(fileName, fileName, maxsize);
5115 		return TRUE;
5116 	}
5117 	else {
5118 		return FALSE;
5119 	}
5120 #endif
5121 
5122 #ifdef WIN_MOTIF
5123 	XmString  dirmask;
5124 	char      *lastSlash;
5125 	char      str[256];
5126 	char      *text;
5127 	Widget    txt;
5128 
5129 	Nlm_CreateFileDialogShell();
5130 	if (Nlm_fileDialogShell != NULL && fileName != NULL) {
5131 		if (fileDialog == NULL) {
5132 			Cardinal  n = 0;
5133 			Arg       wargs[4];
5134 			XtSetArg(wargs[n], XmNcolormap, Nlm_VibrantDefaultColormap());  n++;
5135 			XtSetArg(wargs[n], XmNvisual, Nlm_VibrantDefaultVisual());  n++;
5136 			XtSetArg(wargs[n], XmNdepth, Nlm_VibrantDefaultDepth());  n++;
5137 
5138 			if (initialDir != NULL) {
5139 				XtSetArg(wargs[n], XmNdirectory, XmStringCreateLtoR(initialDir, XmSTRING_DEFAULT_CHARSET));  n++;
5140 			}
5141 
5142 			fileDialog = XmCreateFileSelectionDialog(Nlm_fileDialogShell,
5143 				(String) "file_selection", wargs, n);
5144 			XtAddCallback(fileDialog, XmNcancelCallback, Nlm_FileCancelCallback, NULL);
5145 			XtAddCallback(fileDialog, XmNokCallback, Nlm_FileOkCallback, NULL);
5146 			XtAddCallback(fileDialog, XmNnoMatchCallback, Nlm_FileNoMatchCallback, NULL);
5147 			XtAddCallback(fileDialog, XmNmapCallback, Nlm_FileMapCallback, NULL);
5148 			XtVaSetValues(fileDialog, XmNdefaultPosition, FALSE,
5149 				XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL, NULL);
5150 		}
5151 		if (fileDialog != NULL) {
5152 			txt = XmFileSelectionBoxGetChild(fileDialog, XmDIALOG_FILTER_TEXT);
5153 			text = XmTextGetString(txt);
5154 			Nlm_StringNCpy_0(str, text, sizeof(str));
5155 			lastSlash = Nlm_StringRChr(str, DIRDELIMCHR);
5156 			if (lastSlash != NULL) {
5157 				lastSlash[1] = '\0';
5158 			}
5159 			else {
5160 				lastSlash = &(str[Nlm_StringLen(str) - 1]);
5161 			}
5162 			if (extType != NULL && extType[0] != '\0') {
5163 				Nlm_StringCat(str, "*");
5164 				if (extType[0] != '.') {
5165 					Nlm_StringCat(str, ".");
5166 				}
5167 				Nlm_StringNCat(str, extType, 5);
5168 				dirmask = XmStringCreateLtoR(str, XmSTRING_DEFAULT_CHARSET);
5169 				XmFileSelectionDoSearch(fileDialog, dirmask);
5170 				XmStringFree(dirmask);
5171 				if (lastSlash != NULL) {
5172 					lastSlash[1] = '\0';
5173 				}
5174 			}
5175 			XtFree(text);
5176 			txt = XmFileSelectionBoxGetChild(fileDialog, XmDIALOG_TEXT);
5177 			XmTextSetString(txt, str);
5178 			XtVaSetValues(fileDialog, XmNmustMatch, TRUE, NULL);
5179 			fileBoxUp = TRUE;
5180 			fileBoxRsult = FALSE;
5181 			XtManageChild(fileDialog);
5182 			XSync(Nlm_currentXDisplay, FALSE);
5183 			while (fileBoxUp) {
5184 				XEvent event;
5185 				XPeekEvent(Nlm_currentXDisplay, &event);
5186 				Nlm_ProcessAnEvent();
5187 			}
5188 			if (fileBoxRsult) {
5189 				Nlm_StringNCpy_0(fileName, filePath, maxsize);
5190 			}
5191 			XtUnmanageChild(fileDialog);
5192 			XSync(Nlm_currentXDisplay, FALSE);
5193 		}
5194 	}
5195 	return fileBoxRsult;
5196 #endif
5197 }
5198 
5199 #ifdef WIN_MSWIN
Nlm_CopyDefaultName(Nlm_CharPtr dst,Nlm_CharPtr src)5200 static void Nlm_CopyDefaultName (Nlm_CharPtr dst, Nlm_CharPtr src)
5201 {
5202   /*
5203   Nlm_Char  ch;
5204   Nlm_Int2  i;
5205   Nlm_Int2  j;
5206   Nlm_Int2  k;
5207   */
5208 
5209   if (dst == NULL  ||  src == NULL)
5210     return;
5211   Nlm_StringCpy(dst, src);
5212 
5213   /*
5214   i = 0;
5215   j = 0;
5216   k = 0;
5217   ch = src[i];
5218   while (k < 8 && ch != '\0' && (IS_ALPHANUM(ch) || ch == '_'))
5219     {
5220       dst [j] = ch;
5221       i++;  j++;  k++;
5222       ch = src [i];
5223     }
5224   while (ch != '\0' && ch != '.')
5225     {
5226       i++;
5227       ch = src [i];
5228     }
5229   if (ch == '.')
5230     {
5231       dst[j] = ch;
5232       i++;
5233       j++;
5234       ch = src[i];
5235       k = 0;
5236       while (k < 3 && ch != '\0' && (IS_ALPHANUM(ch) || ch == '_'))
5237         {
5238           dst [j] = ch;
5239           i++; j++; k++;
5240           ch = src[i];
5241         }
5242       if (k == 0)
5243         j--;
5244     }
5245 
5246   dst[j] = '\0';
5247   */
5248 }
5249 #endif
5250 
5251 #ifdef WIN_MAC
5252 #if defined(PROC_PPC) || defined(OS_UNIX_DARWIN)
Nlm_NavServGetOutputFileName(Nlm_CharPtr fileName,size_t maxsize,Nlm_CharPtr dfault)5253 static Nlm_Boolean Nlm_NavServGetOutputFileName (Nlm_CharPtr fileName, size_t maxsize,
5254                                                  Nlm_CharPtr dfault)
5255 
5256 {
5257     OSErr               anErr = noErr;
5258     NavReplyRecord      reply;
5259     NavDialogOptions    dialogOptions;
5260     AEDesc              defaultLocation;
5261     FSSpec              fss;
5262     OSType              fileTypeToSave = 'TEXT';
5263     OSType              creatorType;
5264     NavEventUPP         eventProc = NewNavEventUPP (MyNavEventProc);
5265 	char                filename [256];
5266 	Nlm_Boolean         rsult = FALSE;
5267 
5268     anErr = NavGetDefaultDialogOptions (&dialogOptions);
5269     if (anErr == noErr)
5270     {
5271         /*  Adjust the options to fit our needs */
5272         /*  Set this option */
5273         dialogOptions.dialogOptionFlags |= kNavNoTypePopup;
5274         /*  Clear this one */
5275         dialogOptions.dialogOptionFlags ^= kNavAllowStationery;
5276 
5277         anErr = AECreateDesc(typeFSS, &fss,
5278                              sizeof(fss),
5279                              &defaultLocation );
5280         if (anErr == noErr) {
5281 
5282             /*  One way to get the name for the file to be saved. */
5283             Nlm_StringNCpy_0 ((Nlm_CharPtr) dialogOptions.savedFileName, dfault, 255);
5284             Nlm_CtoPstr ((Nlm_CharPtr) dialogOptions.savedFileName);
5285 
5286             Nlm_MemSet ((void *) &creatorType, '?', 4);
5287             anErr = NavPutFile( &defaultLocation, &reply, &dialogOptions, eventProc,
5288                                 fileTypeToSave, creatorType, 0 );
5289             if (anErr == noErr && reply.validRecord)
5290             {
5291                 AEKeyword   theKeyword;
5292                 DescType    actualType;
5293                 Size        actualSize;
5294                 FSSpec      documentFSSpec;
5295 
5296                 anErr = AEGetNthPtr(&(reply.selection), 1, typeFSS,
5297                                     &theKeyword, &actualType,
5298                                     &documentFSSpec, sizeof(documentFSSpec),
5299                                     &actualSize );
5300                 if (anErr == noErr)
5301                 {
5302                     if (reply.replacing)
5303                     {
5304                         /*
5305                          Make sure you save a temporary file
5306                          so you can check for problems before replacing
5307                          an existing file. Once the save is confirmed,
5308                          swap the files and delete the original.
5309                         */
5310                         Nlm_ConvertFilename (&documentFSSpec, filename);
5311                         Nlm_StringNCpy_0 (fileName, filename, maxsize);
5312                         rsult = TRUE;
5313                     }
5314                     else
5315                     {
5316                         Nlm_ConvertFilename (&documentFSSpec, filename);
5317                         Nlm_StringNCpy_0 (fileName, filename, maxsize);
5318                         rsult = TRUE;
5319                     }
5320 
5321                     if ( anErr == noErr)
5322                     {
5323                         /* DO NOT call NavCompleteSave() to complete */
5324                         /* anErr = NavCompleteSave(&reply,
5325                                                 kNavTranslateInPlace); */
5326                     }
5327                 }
5328                 (void) NavDisposeReply(&reply);
5329             }
5330         }
5331         (void) AEDisposeDesc(&defaultLocation);
5332         DisposeNavEventUPP(eventProc);
5333     }
5334     return rsult;
5335 }
5336 #endif
5337 #endif
5338 
Nlm_GetOutputFileName(Nlm_CharPtr fileName,size_t maxsize,Nlm_CharPtr dfault)5339 extern Nlm_Boolean Nlm_GetOutputFileName (Nlm_CharPtr fileName, size_t maxsize,
5340                                           Nlm_CharPtr dfault)
5341 
5342 {
5343 #ifdef WIN_MAC
5344 # if TARGET_API_MAC_CARBON
5345     return Nlm_NavServGetOutputFileName (fileName, maxsize, dfault);
5346 # else  /* not TARGET_API_MAC_CARBON */
5347   Nlm_Char       currentFileName [64];
5348   Nlm_Char       currentPath [256];
5349   unsigned char  original [256];
5350   unsigned char  promptStr [256];
5351   Nlm_PointTool  ptool;
5352   Nlm_RecT       r;
5353   SFReply        reply;
5354   Nlm_Boolean    rsult;
5355   Nlm_RectTool   rtool;
5356   GrafPtr        tempPort;
5357   PenState       state;
5358   Nlm_PoinT      where;
5359 
5360   if (Nlm_usesMacNavServices) {
5361     return Nlm_NavServGetOutputFileName (fileName, maxsize, dfault);
5362   }
5363 
5364   where.x = 90;
5365   where.y = 100;
5366   GetPenState (&state);
5367   GetPort (&tempPort);
5368   Nlm_PoinTToPointTool (where, &ptool);
5369   Nlm_StringNCpy_0((Nlm_CharPtr)original, dfault, sizeof(original));
5370   Nlm_CtoPstr ((Nlm_CharPtr) original);
5371   Nlm_StringNCpy_0((Nlm_CharPtr)promptStr, "Save File As:", sizeof(promptStr));
5372   Nlm_CtoPstr ((Nlm_CharPtr) promptStr);
5373   SFPutFile (ptool, promptStr, original, NULL, &reply);
5374   SetPort (tempPort);
5375   SetPenState (&state);
5376   Nlm_GetRect ((Nlm_GraphiC) Nlm_desktopWindow, &r);
5377   Nlm_RecTToRectTool (&r, &rtool);
5378   ClipRect (&rtool);
5379   if (reply.good != 0 && fileName != NULL  &&  maxsize > 0) {
5380     Nlm_StringNCpy_0(currentFileName, (Nlm_CharPtr) &(reply.fName),
5381                      sizeof (currentFileName));
5382     Nlm_PtoCstr (currentFileName);
5383     currentPath[0] = '\0';
5384     Nlm_GetFilePath (reply.vRefNum, currentPath, sizeof (currentPath));
5385     Nlm_StringNCat(currentPath, currentFileName,
5386                    sizeof(currentPath) - Nlm_StringLen(currentPath) - 1);
5387     Nlm_StringNCpy_0(fileName, currentPath, maxsize);
5388     rsult = TRUE;
5389   } else {
5390     rsult = FALSE;
5391   }
5392   Nlm_Update ();
5393   return rsult;
5394 # endif  /* not TARGET_API_MAC_CARBON */
5395 #endif /* ifdef WIN_MAC */
5396 
5397 #ifdef WIN_MSWIN
5398   char  szDirName [256];
5399   char  szFile [256];
5400   char  szFileTitle [256];
5401   UINT  i;
5402   UINT  cbString;
5403   char  chReplace;
5404   char  szFilter [256];
5405   Nlm_Boolean rval;
5406   HHOOK   hook;
5407   DWORD thread_id;
5408 
5409   /* Get the current working directory: */
5410   szDirName[0] = '\0';
5411   _getcwd(szDirName, sizeof (szDirName));
5412   szFile [0] = '\0';
5413   Nlm_CopyDefaultName ((Nlm_CharPtr) szFile, dfault);
5414   Nlm_StringCpy (szFilter, "All Files (*.*)|*.*|");
5415   cbString = (UINT) Nlm_StringLen (szFilter);
5416   chReplace = szFilter [cbString - 1];
5417   for (i = 0; szFilter [i] != '\0'; i++) {
5418     if (szFilter [i] == chReplace) {
5419       szFilter [i] = '\0';
5420     }
5421   }
5422   memset (&ofn, 0, sizeof (OPENFILENAME));
5423   ofn.lStructSize = sizeof (OPENFILENAME);
5424   ofn.hwndOwner = Nlm_currentHWnd;
5425   ofn.lpstrFilter = szFilter;
5426   ofn.lpstrFile = szFile;
5427   ofn.nMaxFile = sizeof (szFile);
5428   ofn.lpstrFileTitle = szFileTitle;
5429   ofn.nMaxFileTitle = sizeof (szFileTitle);
5430   ofn.lpstrInitialDir = szDirName;
5431   /*
5432   ofn.Flags = OFN_SHOWHELP | OFN_OVERWRITEPROMPT;
5433   */
5434   /* OFN_OVERWRITEPROMPT causes crashes for some unknown reason */
5435   ofn.Flags = OFN_SHOWHELP | OFN_NOCHANGEDIR;
5436 
5437   /* turn off tooltips, because they were causing Windows 2000 to crash when opening desktop files */
5438   thread_id = GetCurrentThreadId();
5439   hook = SetWindowsHookEx (WH_CALLWNDPROC, StopToolTips, NULL, thread_id);
5440 
5441   if (GetSaveFileName (&ofn) && fileName != NULL) {
5442     Nlm_StringNCpy_0(fileName, ofn.lpstrFile, maxsize);
5443     AnsiToOemBuff (fileName, fileName, maxsize);
5444     if (Nlm_FileLengthEx(fileName) != -1) {
5445       if (Nlm_Message (MSG_YN, "Replace existing file?") == ANS_NO) {
5446         rval = FALSE;
5447 	  } else {
5448 	    rval = TRUE;
5449 	  }
5450 	} else {
5451       rval = TRUE;
5452 	}
5453   } else {
5454     rval = FALSE;
5455   }
5456   UnhookWindowsHookEx (hook);
5457   return rval;
5458 
5459 #endif
5460 
5461 #ifdef WIN_MOTIF
5462   char   *lastSlash;
5463   char    str [256];
5464   char    *text;
5465   Widget  txt;
5466   char    *lastDot;
5467   XmString dirmask;
5468 
5469   Nlm_CreateFileDialogShell ();
5470   if (Nlm_fileDialogShell != NULL && fileName != NULL) {
5471     if (fileDialog == NULL) {
5472       Cardinal  n = 0;
5473       Arg       wargs[4];
5474       XtSetArg (wargs[n], XmNcolormap, Nlm_VibrantDefaultColormap());  n++;
5475       XtSetArg (wargs[n], XmNvisual,   Nlm_VibrantDefaultVisual  ());  n++;
5476       XtSetArg (wargs[n], XmNdepth,    Nlm_VibrantDefaultDepth   ());  n++;
5477       fileDialog = XmCreateFileSelectionDialog (Nlm_fileDialogShell,
5478                                                 (String) "file_selection", wargs, n);
5479       XtAddCallback (fileDialog, XmNcancelCallback, Nlm_FileCancelCallback, NULL);
5480       XtAddCallback (fileDialog, XmNokCallback, Nlm_FileOkCallback, NULL);
5481       XtAddCallback (fileDialog, XmNnoMatchCallback, Nlm_FileNoMatchCallback, NULL);
5482       XtAddCallback (fileDialog, XmNmapCallback, Nlm_FileMapCallback, NULL);
5483       XtVaSetValues (fileDialog, XmNdefaultPosition, FALSE,
5484                      XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL, NULL);
5485     }
5486     if (fileDialog != NULL) {
5487       txt = XmFileSelectionBoxGetChild (fileDialog, XmDIALOG_FILTER_TEXT);
5488       text = XmTextGetString (txt);
5489       Nlm_StringNCpy_0(str, text, sizeof(str));
5490       lastSlash = Nlm_StringRChr (str, DIRDELIMCHR);
5491       if (lastSlash != NULL) {
5492         lastSlash [1] = '\0';
5493       }
5494 
5495       /* V.L. */
5496       lastDot=dfault ? Nlm_StringRChr(dfault,'.') : (Nlm_CharPtr)NULL;
5497       Nlm_StringNCat(str, "*", sizeof(str) - Nlm_StringLen(str) - 1);
5498       if (lastDot)
5499         Nlm_StringNCat(str, lastDot, sizeof(str) - Nlm_StringLen(str) - 1);
5500       dirmask = XmStringCreateLtoR (str, XmSTRING_DEFAULT_CHARSET);
5501       XmFileSelectionDoSearch (fileDialog, dirmask);
5502       XmStringFree (dirmask);
5503       if (lastSlash != NULL) {
5504         lastSlash [1] = '\0';
5505       }
5506 
5507       XtFree (text);
5508       if (dfault != NULL) {
5509         Nlm_StringNCat(str, dfault, sizeof(str) - Nlm_StringLen(str) - 1);
5510       }
5511       txt = XmFileSelectionBoxGetChild (fileDialog, XmDIALOG_TEXT);
5512       XmTextSetString (txt, str);
5513       XtVaSetValues (fileDialog, XmNmustMatch, FALSE, NULL);
5514       fileBoxUp = TRUE;
5515       fileBoxRsult = FALSE;
5516       XtManageChild (fileDialog);
5517       while (fileBoxUp) {
5518         XEvent event;
5519         XPeekEvent(Nlm_currentXDisplay, &event);
5520         Nlm_ProcessAnEvent ();
5521       }
5522       if (fileBoxRsult) {
5523         Nlm_StringNCpy_0(fileName, filePath, maxsize);
5524       }
5525       XtUnmanageChild (fileDialog);
5526     }
5527   }
5528   return fileBoxRsult;
5529 #endif
5530 }
5531 
5532 extern Nlm_Boolean Nlm_LaunchAppEx (Nlm_CharPtr fileName, Nlm_VoidPtr serialNumPtr, Nlm_CharPtr sig);
Nlm_LaunchAppEx(Nlm_CharPtr fileName,Nlm_VoidPtr serialNumPtr,Nlm_CharPtr sig)5533 extern Nlm_Boolean Nlm_LaunchAppEx (Nlm_CharPtr fileName, Nlm_VoidPtr serialNumPtr, Nlm_CharPtr sig)
5534 
5535 {
5536 #ifdef WIN_MAC
5537 #ifdef OS_UNIX_DARWIN
5538   OSErr                   err;
5539   Nlm_Boolean             rsult = FALSE;
5540   OSType                  appCreator = 0;
5541   CFStringRef             appName = NULL;
5542   FSRef                   appRef;
5543 
5544   if (fileName != NULL && fileName [0] != '\0') {
5545     appName = CFStringCreateWithCString(NULL, fileName, kCFStringEncodingMacRoman);
5546   }
5547   if (sig != NULL && sig [0] != '\0') {
5548     appCreator = *(OSType*) sig;
5549   }
5550   if (appCreator == 0 && appName == NULL)
5551     return rsult;
5552 
5553   err = LSFindApplicationForInfo( appCreator, NULL, appName, &appRef, NULL);
5554   if (err == noErr) {
5555     err = LSOpenFSRef(&appRef, NULL);
5556     rsult = (err == noErr);
5557   }
5558   return rsult;
5559 
5560 #else
5561   OSErr                   err;
5562   Nlm_Boolean             rsult = FALSE;
5563   FSSpec                  fsspec;
5564   long                    gval;
5565   LaunchParamBlockRec     myLaunchParams;
5566   Nlm_Boolean             okay = FALSE;
5567   unsigned char           pathname [256];
5568   ProcessSerialNumberPtr  psnp;
5569   Nlm_CharPtr             ptr;
5570   OSType                  theSignature;
5571   ProcessSerialNumber     psn;
5572   ProcessInfoRec          pir;
5573   Nlm_Boolean             found;
5574   DTPBRec                 dtpb;
5575   short                   ioDTRefNum;
5576   long                    dirID;
5577   short                   vRefNum;
5578   long                    ioAPPLParID;
5579 
5580   if (Gestalt (gestaltSystemVersion, &gval) == noErr && (short) gval >= 7 * 256) {
5581     if (fileName != NULL && fileName [0] != '\0') {
5582       if (Nlm_StringChr (fileName, DIRDELIMCHR) != NULL) {
5583         Nlm_StringNCpy_0((Nlm_CharPtr)pathname, fileName, sizeof(pathname));
5584       } else {
5585         Nlm_ProgramPath ((Nlm_CharPtr) pathname, sizeof (pathname));
5586         ptr = Nlm_StringRChr ((Nlm_CharPtr) pathname, DIRDELIMCHR);
5587         if (ptr != NULL) {
5588           *ptr = '\0';
5589         }
5590         Nlm_FileBuildPath ((Nlm_CharPtr) pathname, NULL, fileName);
5591       }
5592       Nlm_CtoPstr ((Nlm_CharPtr) pathname);
5593       err = FSMakeFSSpec (0, 0, pathname, &(fsspec));
5594       okay = (Nlm_Boolean) (err == noErr);
5595     } else if (sig != NULL && sig [0] != '\0') {
5596       theSignature = *(OSType*) sig;
5597       psn.highLongOfPSN = 0;
5598       psn.lowLongOfPSN = kNoProcess;
5599       found = FALSE;
5600       while (GetNextProcess (&psn) != noErr) {
5601         if (GetProcessInformation (&psn, &pir) != noErr) {
5602           if (pir.processSignature == theSignature) {
5603             found = TRUE;
5604           }
5605         }
5606       }
5607       if (found) return TRUE; /* no need to launch if already running */
5608       if (! found) {
5609         err = FindFolder (kOnSystemDisk, kTemporaryFolderType,
5610                           kCreateFolder, &vRefNum, &dirID);
5611         if (err == noErr) {
5612           memset (&dtpb, 0, sizeof (DTPBRec));
5613           dtpb.ioNamePtr = NULL;
5614           dtpb.ioVRefNum = vRefNum;
5615           err = PBDTGetPath (&dtpb);
5616           if (err == noErr) {
5617             ioDTRefNum = dtpb.ioDTRefNum;
5618             if (ioDTRefNum != 0) {
5619               memset (&dtpb, 0, sizeof (DTPBRec));
5620               dtpb.ioDTRefNum = ioDTRefNum;
5621               dtpb.ioFileCreator = theSignature;
5622               dtpb.ioNamePtr = (StringPtr) pathname;
5623               dtpb.ioIndex = 0;
5624               memset (&pathname, 0, sizeof (pathname));
5625               err = PBDTGetAPPL (&dtpb, FALSE);
5626               if (err == noErr) {
5627                 ioAPPLParID = dtpb.ioAPPLParID;
5628                 if (pathname [0] != '\0') {
5629                   err = FSMakeFSSpec (vRefNum, ioAPPLParID,
5630                                       (ConstStr255Param) pathname, &fsspec);
5631                   okay = (Nlm_Boolean) (err == noErr);
5632                 }
5633               }
5634             }
5635           }
5636         }
5637       }
5638     }
5639     if (okay) {
5640       myLaunchParams.launchBlockID = extendedBlock;
5641       myLaunchParams.launchEPBLength = extendedBlockLen;
5642       myLaunchParams.launchFileFlags = 0;
5643       myLaunchParams.launchControlFlags = launchContinue + launchNoFileFlags;
5644       myLaunchParams.launchAppParameters = NULL;
5645       myLaunchParams.launchAppSpec = &fsspec;
5646       rsult = (Nlm_Boolean) (LaunchApplication (&myLaunchParams) == noErr);
5647       if (serialNumPtr != NULL) {
5648         psnp = (ProcessSerialNumberPtr) serialNumPtr;
5649         *psnp = myLaunchParams.launchProcessSN;
5650       }
5651       SetFrontProcess (&myLaunchParams.launchProcessSN);
5652     }
5653   }
5654   return rsult;
5655 #endif /* OS_UNIX_DARWIN */
5656 #endif /* WIN_MAC */
5657 #ifdef WIN_MSWIN
5658   Nlm_Char     ch;
5659   Nlm_Int2     i;
5660   Nlm_Int2     j;
5661   Nlm_Int2     k;
5662   char         lpszCmdLine [256];
5663   Nlm_Boolean  rsult;
5664 
5665   rsult = FALSE;
5666   if (fileName != NULL && fileName [0] != '\0') {
5667     Nlm_StringNCpy_0((Nlm_CharPtr)lpszCmdLine, fileName, sizeof(lpszCmdLine));
5668     i = 0;
5669     j = 0;
5670     k = 0;
5671     ch = fileName [j];
5672     while (i < 8 && ch != '\0') {
5673       if (ch == '_' || IS_ALPHANUM (ch)) {
5674         lpszCmdLine [k] = ch;
5675         j++;
5676         k++;
5677         ch = fileName [j];
5678       }
5679       i++;
5680     }
5681     while (ch != '\0' && ch != '.') {
5682       j++;
5683       ch = fileName [j];
5684     }
5685     if (ch == '.') {
5686       lpszCmdLine [k] = ch;
5687       j++;
5688       k++;
5689       ch = fileName [j];
5690       i = 0;
5691       while (i < 3 && ch != '\0') {
5692         if (ch == '_' || IS_ALPHANUM (ch)) {
5693           lpszCmdLine [k] = ch;
5694           j++;
5695           k++;
5696           ch = fileName [j];
5697         }
5698         i++;
5699       }
5700     }
5701     if (k > 0 && lpszCmdLine [k - 1] == '.') {
5702       lpszCmdLine [k - 1] = '\0';
5703     }
5704     lpszCmdLine [k] = '\0';
5705     rsult = (Nlm_Boolean) (WinExec (lpszCmdLine, SW_SHOW) >= 32);
5706   }
5707   return rsult;
5708 #endif
5709 #ifdef WIN_MOTIF
5710   Nlm_Boolean  rsult;
5711   int          status;
5712 
5713   rsult = FALSE;
5714   if (fileName != NULL && fileName [0] != '\0') {
5715 #ifdef OS_UNIX
5716     status = (int) fork ();
5717     if (status == 0) {
5718       execlp (fileName, fileName, (char *) 0);
5719       Nlm_Message (MSG_FATAL, "Application launch failed");
5720     } else if (status != -1) {
5721       rsult = TRUE;
5722     }
5723 #endif
5724 #ifdef OS_VMS
5725     status = execl (fileName, fileName, (char *) 0);
5726     if ( status == -1 ) {
5727       Nlm_Message (MSG_FATAL, "Application launch failed");
5728     } else {
5729       rsult = TRUE;
5730     }
5731 #endif
5732   }
5733   return rsult;
5734 #endif
5735 }
5736 
Nlm_LaunchApp(Nlm_CharPtr fileName)5737 extern Nlm_Boolean Nlm_LaunchApp (Nlm_CharPtr fileName)
5738 
5739 {
5740   return Nlm_LaunchAppEx (fileName, NULL, NULL);
5741 }
5742 
5743 
Nlm_Execv(const Nlm_Char * path,Nlm_Char * const * argv)5744 extern Nlm_Boolean Nlm_Execv(const Nlm_Char* path, Nlm_Char *const *argv)
5745 {
5746   if (!path  ||  !*path)
5747     return FALSE;
5748 
5749 #if defined(WIN_MSWIN)
5750   {{
5751     int argc;
5752     char cmdline[256];
5753 
5754     size_t len = Nlm_StrLen(path);
5755     for (argc = 0;  argv[argc];  argc++) {
5756       len += 1 + Nlm_StrLen(argv[argc]);
5757     }
5758     if (len >= sizeof(cmdline)) {
5759       Nlm_Message(MSG_ERROR, "Nlm_Execv() failed: command line is too long");
5760       return FALSE;
5761     }
5762 
5763     Nlm_StrCpy(cmdline, path);
5764     for (argc = 0;  argv[argc];  argc++) {
5765       Nlm_StrCat(cmdline, " ");
5766       Nlm_StrCat(cmdline, argv[argc]);
5767     }
5768     ASSERT( StrLen(cmdline) < sizeof(cmdline) );
5769 
5770     return (Nlm_Boolean)(WinExec(cmdline, SW_SHOW) > 31);
5771   }}
5772 
5773 #elif defined(OS_UNIX)
5774   {{
5775     int fork_code;
5776     /* Attention!
5777      * -- do not move this code to the "child part";
5778      *    it's tempting, but dangerous in the case of MT applications!
5779      */
5780     int x_argc;
5781     char **x_argv;
5782     for (x_argc = 0;  argv[x_argc];  x_argc++) continue;
5783     x_argv = (char**)Nlm_MemNew((1 + x_argc + 1) * sizeof(char*));
5784     x_argv[0] = (char *)path;
5785     Nlm_MemCpy(&x_argv[1], &argv[0], (x_argc + 1) * sizeof(char*));
5786 
5787     /* Fork the process
5788      */
5789 #ifdef SOLARIS_THREADS_AVAIL
5790     fork_code = fork1();
5791 #else
5792     fork_code = fork();
5793 #endif
5794 
5795     /* Child
5796      */
5797     if (fork_code == 0) {
5798       execv(path, x_argv);
5799       Nlm_Message(MSG_FATAL, "Nlm_Execv() failed: cannot execv()");
5800       ASSERT( 0 );
5801       return FALSE;
5802     }
5803 
5804     /* Parent
5805      */
5806     Nlm_MemFree(x_argv);
5807     if (fork_code == -1) {
5808       Nlm_Message(MSG_ERROR, "Nlm_Execv() failed: cannot fork()");
5809       return FALSE;
5810     }
5811     return TRUE;
5812   }}
5813 
5814 #else
5815   return FALSE;
5816 #endif
5817 }
5818 
5819 
5820 /* ++dgg -- added by d.gilbert, dec'93, for use w/ DCLAP c++ library */
5821 
Nlm_SetObject(Nlm_GraphiC a,Nlm_VoidPtr thisobject)5822 extern void Nlm_SetObject (Nlm_GraphiC a, Nlm_VoidPtr thisobject)
5823 {
5824   Nlm_GraphicData  gdata;
5825   if (a != NULL && thisobject != NULL) {
5826     Nlm_GetGraphicData (a, &gdata);
5827     gdata.thisobject = thisobject;
5828     Nlm_SetGraphicData (a, &gdata);
5829   }
5830 }
5831 
Nlm_GetObject(Nlm_GraphiC a)5832 extern Nlm_VoidPtr Nlm_GetObject (Nlm_GraphiC a)
5833 {
5834   Nlm_GraphicData  gdata;
5835   if (a != NULL) {
5836     Nlm_GetGraphicData (a, &gdata);
5837     return gdata.thisobject;
5838   } else {
5839     return NULL;
5840   }
5841 }
5842 
5843 #ifdef WIN_MAC
Nlm_SendOpenDocAppleEventEx(Nlm_CharPtr datafile,Nlm_CharPtr sig,Nlm_CharPtr prog,Nlm_Boolean wantReply)5844 extern void Nlm_SendOpenDocAppleEventEx (Nlm_CharPtr datafile, Nlm_CharPtr sig, Nlm_CharPtr prog, Nlm_Boolean wantReply)
5845 
5846 {
5847   long gval;
5848   OSErr theErr = noErr;
5849   AEAddressDesc theAddress;
5850   AEDesc docDesc;
5851   AppleEvent theEvent;
5852   AppleEvent theReply;
5853   OSType theSignature;
5854   AEDescList theList;
5855   CFURLRef cfurl;
5856   ProcessSerialNumber  psn;
5857   Nlm_Boolean  okay = FALSE;
5858 
5859   if (Gestalt (gestaltSystemVersion, &gval) == noErr && (short) gval >= 7 * 256) {
5860     theSignature = 'RSML';
5861     if (sig != NULL && sig [0] != '\0') {
5862       theSignature = *(OSType*) sig;
5863     }
5864     if (prog != NULL && prog [0] != '\0') {
5865       if (Nlm_LaunchAppEx (prog, (Nlm_VoidPtr) &psn, NULL)) {
5866         theErr = AECreateDesc(typeProcessSerialNumber, (Ptr)&psn,
5867                               sizeof(psn), &theAddress);
5868         okay = (Nlm_Boolean) (theErr == noErr);
5869       }
5870     } else if (sig != NULL && sig [0] != '\0') {
5871       if (Nlm_LaunchAppEx (prog, NULL, sig)) {
5872         theSignature = *(OSType*) sig;
5873         theErr = AECreateDesc(typeApplSignature, (Ptr)&theSignature,
5874                               sizeof(theSignature), &theAddress);
5875         okay = (Nlm_Boolean) (theErr == noErr);
5876       }
5877     }
5878     if (okay) {
5879       theErr = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &theAddress,
5880                                   kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
5881       if (theErr == noErr) {
5882         theErr = AECreateList(NULL, 0, FALSE, &theList);
5883         if (theErr == noErr) {
5884           cfurl = CFURLCreateFromFileSystemRepresentation (NULL, (const UInt8 *)datafile, strlen (datafile), 0);
5885           if (cfurl) {
5886             CFDataRef cfdata = CFURLCreateData (NULL, cfurl, kCFStringEncodingUTF8, 1);
5887             theErr = AECreateDesc(typeFileURL, CFDataGetBytePtr (cfdata), CFDataGetLength (cfdata), &docDesc);
5888             CFRelease (cfdata);
5889             if (theErr == noErr) {
5890               theErr = AEPutDesc(&theList, 0, &docDesc);
5891               if (theErr == noErr) {
5892                 theErr = AEPutParamDesc(&theEvent, keyDirectObject, &theList);
5893                 if (theErr == noErr) {
5894                   if (wantReply) {
5895                     AESend (&theEvent, &theReply, kAEQueueReply, kAENormalPriority, 120, NULL, NULL);
5896                   } else {
5897                     AESend (&theEvent, &theReply, kAENoReply, kAENormalPriority, 120, NULL, NULL);
5898                   }
5899                 }
5900               }
5901               AEDisposeDesc(&docDesc);
5902             }
5903           }
5904           AEDisposeDesc(&theList);
5905         }
5906         AEDisposeDesc(&theEvent);
5907       }
5908       AEDisposeDesc(&theAddress);
5909     }
5910   }
5911 }
5912 
Nlm_SendOpenDocAppleEvent(Nlm_CharPtr datafile,Nlm_CharPtr sig)5913 extern void Nlm_SendOpenDocAppleEvent (Nlm_CharPtr datafile, Nlm_CharPtr sig)
5914 
5915 {
5916   Nlm_SendOpenDocAppleEventEx (datafile, sig, NULL, FALSE);
5917 }
5918 
Nlm_SendURLAppleEvent(Nlm_CharPtr urlString,Nlm_CharPtr sig,Nlm_CharPtr prog)5919 extern void Nlm_SendURLAppleEvent (Nlm_CharPtr urlString, Nlm_CharPtr sig, Nlm_CharPtr prog)
5920 
5921 {
5922 #ifdef OS_UNIX_DARWIN
5923   OSStatus  err;
5924   CFURLRef  urlRef;
5925 
5926   /* on OS X ignore the sig and prog and open the url with the default browser */
5927   urlRef = CFURLCreateWithBytes (NULL, (const UInt8 *) urlString, Nlm_StrLen( urlString),  kCFStringEncodingMacRoman, NULL);
5928   err = LSOpenCFURLRef(urlRef, NULL);
5929 
5930 #else
5931   long gval;
5932   OSErr theErr = noErr;
5933   AEAddressDesc theAddress;
5934   AEDesc urlEvent;
5935   AppleEvent theEvent;
5936   AppleEvent theReply;
5937   OSType theSignature;
5938   ProcessSerialNumber  psn;
5939   Nlm_Boolean  okay = FALSE;
5940 
5941   if (Gestalt (gestaltSystemVersion, &gval) == noErr && (short) gval >= 7 * 256) {
5942     theSignature = 'RSML';
5943     if (sig != NULL && sig [0] != '\0') {
5944       theSignature = *(OSType*) sig;
5945     }
5946     if (prog != NULL && prog [0] != '\0') {
5947       if (Nlm_LaunchAppEx (prog, (Nlm_VoidPtr) &psn, NULL)) {
5948         theErr = AECreateDesc(typeProcessSerialNumber, (Ptr)&psn,
5949                               sizeof(psn), &theAddress);
5950         okay = (Nlm_Boolean) (theErr == noErr);
5951       }
5952     } else if (sig != NULL && sig [0] != '\0') {
5953       if (Nlm_LaunchAppEx (prog, NULL, sig)) {
5954         theSignature = *(OSType*) sig;
5955         theErr = AECreateDesc(typeApplSignature, (Ptr)&theSignature,
5956                               sizeof(theSignature), &theAddress);
5957         okay = (Nlm_Boolean) (theErr == noErr);
5958       }
5959     }
5960     if (okay) {
5961       theErr = AECreateAppleEvent('WWW!', 'OURL', &theAddress,
5962                                   kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
5963       if (theErr == noErr) {
5964         theErr = AECreateDesc(typeChar, urlString, Nlm_StringLen (urlString), &urlEvent);
5965         if (theErr == noErr) {
5966           theErr = AEPutParamDesc(&theEvent, keyDirectObject, &urlEvent);
5967           if (theErr == noErr) {
5968             AESend (&theEvent, &theReply, kAEWaitReply, kAEHighPriority, 500, NULL, NULL);
5969             AEDisposeDesc(&theReply);
5970           }
5971           AEDisposeDesc(&urlEvent);
5972         }
5973         AEDisposeDesc(&theEvent);
5974       }
5975       AEDisposeDesc(&theAddress);
5976     }
5977   }
5978 #endif
5979 }
5980 
5981 
5982 /* Modified from http://developer.apple.com/qa/qa2001/qa1026.html */
5983 
5984 #if TARGET_API_MAC_CARBON
5985 #ifdef __APPLE_CC__
5986 #include <Carbon/Carbon.h>
5987 #else
5988 #include <Carbon.h>
5989 #endif
5990 #else
5991 #include <Types.h>
5992 #include <OSA.h>
5993 #include <AppleScript.h>
5994 #endif
5995 #include <string.h>
5996     /* LowRunAppleScript compiles and runs an AppleScript
5997     provided as text in the buffer pointed to by text.  textLength
5998     bytes will be compiled from this buffer and run as an AppleScript
5999     using all of the default environment and execution settings.  If
6000     resultData is not NULL, then the result returned by the execution
6001     command will be returned as typeChar in this descriptor record
6002     (or typeNull if there is no result information).  If the function
6003     returns errOSAScriptError, then resultData will be set to a
6004     descriptive error message describing the error (if one is
6005     available).  */
6006 
LowRunAppleScript(const void * text,long textLength,AEDesc * resultData)6007 static OSStatus LowRunAppleScript (
6008   const void* text,
6009   long textLength,
6010   AEDesc *resultData
6011 )
6012 
6013 {
6014   ComponentInstance theComponent;
6015   AEDesc scriptTextDesc;
6016   OSStatus err;
6017   OSAID scriptID, resultID;
6018 
6019   /* set up locals to a known state */
6020   theComponent = NULL;
6021   AECreateDesc (typeNull, NULL, 0, &scriptTextDesc);
6022   scriptID = kOSANullScript;
6023   resultID = kOSANullScript;
6024 
6025   /* open the scripting component */
6026   theComponent = OpenDefaultComponent (kOSAComponentType, typeAppleScript);
6027   if (theComponent == NULL) {
6028     err = paramErr;
6029     goto bail;
6030   }
6031 
6032   /* put the script text into an aedesc */
6033   err = AECreateDesc (typeChar, text, textLength, &scriptTextDesc);
6034   if (err != noErr) goto bail;
6035 
6036   /* compile the script */
6037   err = OSACompile (theComponent, &scriptTextDesc, kOSAModeNull, &scriptID);
6038   if (err != noErr) goto bail;
6039 
6040   /* run the script */
6041   err = OSAExecute (theComponent, scriptID, kOSANullScript, kOSAModeNull, &resultID);
6042 
6043   /* collect the results - if any */
6044   if (resultData != NULL) {
6045     AECreateDesc (typeNull, NULL, 0, resultData);
6046     if (err == errOSAScriptError) {
6047       OSAScriptError (theComponent, kOSAErrorMessage, typeChar, resultData);
6048     } else if (err == noErr && resultID != kOSANullScript) {
6049       OSADisplay (theComponent, resultID, typeChar, kOSAModeNull, resultData);
6050     }
6051   }
6052 bail:
6053   AEDisposeDesc (&scriptTextDesc);
6054   if (scriptID != kOSANullScript) OSADispose (theComponent, scriptID);
6055   if (resultID != kOSANullScript) OSADispose (theComponent, resultID);
6056   if (theComponent != NULL) CloseComponent (theComponent);
6057   return err;
6058 }
6059 
6060 
Nlm_SendAppleScriptString(Nlm_CharPtr script)6061 extern void Nlm_SendAppleScriptString (Nlm_CharPtr script)
6062 
6063 {
6064   if (Nlm_StringHasNoText (script)) return;
6065   LowRunAppleScript ((const char*) script, Nlm_StringLen (script), NULL);
6066 }
6067 
6068 
6069 extern void Nlm_GetFileTypeAndCreator (Nlm_CharPtr filename, Nlm_CharPtr type, Nlm_CharPtr creator);
6070 #if TARGET_API_MAC_CARBON
Nlm_GetFileTypeAndCreator(Nlm_CharPtr filename,Nlm_CharPtr type,Nlm_CharPtr creator)6071 extern void Nlm_GetFileTypeAndCreator (Nlm_CharPtr filename, Nlm_CharPtr type, Nlm_CharPtr creator)
6072 {
6073   OSType    fCreator;
6074   Nlm_Int2  fError;
6075   FInfo     fInfo;
6076   OSType    fType;
6077   Nlm_Char  temp [256];
6078 
6079   if (type != NULL) {
6080     *type = '\0';
6081   }
6082   if (creator != NULL) {
6083     *creator = '\0';
6084   }
6085   Nlm_StringNCpy_0 (temp, filename, sizeof(temp));
6086   Nlm_CtoPstr ((Nlm_CharPtr) temp);
6087   fError = HGetFInfo ( 0, 0, (StringPtr) temp, &fInfo);
6088   if (fError == 0) {
6089     fType = fInfo.fdType;
6090     fCreator = fInfo.fdCreator;
6091     StringNCpy_0 (type, (Nlm_CharPtr) (&fType), 5);
6092     StringNCpy_0 (creator, (Nlm_CharPtr) (&fCreator), 5);
6093   }
6094 }
6095 
6096 #else
Nlm_GetFileTypeAndCreator(Nlm_CharPtr filename,Nlm_CharPtr type,Nlm_CharPtr creator)6097 extern void Nlm_GetFileTypeAndCreator (Nlm_CharPtr filename, Nlm_CharPtr type, Nlm_CharPtr creator)
6098 {
6099   OSStatus err;
6100   FSRef fsref;
6101   FSCatalogInfo catinfo;
6102 
6103   if (type != NULL) {
6104     *type = '\0';
6105   }
6106   if (creator != NULL) {
6107     *creator = '\0';
6108   }
6109 
6110   err = FSPathMakeRef ((const UInt8 *)filename, &fsref, NULL);
6111   if (err)
6112     return;
6113 
6114   err = FSGetCatalogInfo (&fsref, kFSCatInfoGettableInfo, &catinfo, NULL, NULL, NULL);
6115 
6116   if (err == noErr && (catinfo.nodeFlags & kFSNodeIsDirectoryMask) == 0)
6117   {
6118     FileInfo finfo;
6119     memcpy(&finfo, &catinfo.finderInfo, sizeof(finfo));
6120 
6121     if (type)
6122     {
6123       *type = htonl (finfo.fileType);
6124       type[4] = 0;
6125     }
6126     if (creator)
6127     {
6128       *(Nlm_Int4 *)creator = htonl (finfo.fileCreator);
6129       creator[4] = 0;
6130     }
6131   }
6132 }
6133 #endif
6134 #endif
6135 
6136 
6137 #ifdef WIN_MSWIN
Nlm_MSWin_OpenDocument(const Nlm_Char * doc_name)6138 extern Nlm_Boolean Nlm_MSWin_OpenDocument(const Nlm_Char* doc_name)
6139 {
6140   int status = (int)ShellExecute(0, "open", doc_name,
6141                                  NULL, NULL, SW_SHOWNORMAL);
6142   if (status <= 32) {
6143     Nlm_ErrPostEx(SEV_WARNING, 0, 0,
6144                   "Unable to open document \"%s\", error=%d",
6145                   doc_name, status);
6146     return FALSE;
6147   }
6148   return TRUE;
6149 }
6150 
Nlm_MSWin_OpenApplication(const Nlm_Char * program,const Nlm_Char * parameters)6151 extern Nlm_Boolean Nlm_MSWin_OpenApplication(const Nlm_Char* program, const Nlm_Char* parameters)
6152 {
6153   int status = (int)ShellExecute(0, "open", program,
6154                                  parameters, NULL, SW_SHOWNORMAL);
6155   if (status <= 32) {
6156     Nlm_ErrPostEx(SEV_WARNING, 0, 0,
6157                   "Unable to open document, error=%d", status);
6158     return FALSE;
6159   }
6160   return TRUE;
6161 }
6162 
6163 /*****************************************************************************
6164 
6165 Function: Nlm_GetExecPath()
6166 
6167 Purpose: Gets the path for an executable under Windows
6168 
6169 Parameters: filetype: the name of the filetype (e.g. "valfile" for Cn3D)
6170             buf: the buffer to put the path into
6171             buflen: the length of buf
6172 
6173 
6174 *****************************************************************************/
6175 
Nlm_GetExecPath(char * filetype,char * buf,int buflen)6176 extern void Nlm_GetExecPath(char *filetype, char *buf, int buflen)
6177 {
6178     HKEY hkResult;
6179     int i;
6180     char key[256];
6181     DWORD len;
6182 
6183     if(buf == NULL || filetype == NULL || buflen < 1) return;
6184     if(strlen(filetype)>220) return;
6185     key[0] = '\0';
6186     strcat(key, filetype);
6187     strcat(key, "\\Shell\\open\\command");
6188     RegOpenKeyEx(HKEY_CLASSES_ROOT, key, 0, KEY_READ,
6189         &hkResult);
6190     if(hkResult == NULL) {
6191         buf[0] = '\0';
6192         return;
6193     }
6194 
6195     len = (DWORD) buflen;
6196     RegQueryValueEx(hkResult,"", NULL, NULL, (LPBYTE) buf, (LPDWORD) &len);
6197     buflen = (int) len;
6198     RegCloseKey(hkResult);
6199 
6200     for (i=1; i<buflen && buf[i] != '"'; i++) {}
6201     buf[i-1] = '\0';
6202 }
6203 
6204 #endif
6205 
6206 
6207 /* esl: ChooseFont implementation */
6208 
6209 #ifdef WIN_MSWIN
6210 
Nlm_LOGFONTToFontSpec(LOGFONT * lfp,Nlm_FontSpecPtr fsp)6211 static void Nlm_LOGFONTToFontSpec (LOGFONT *lfp, Nlm_FontSpecPtr fsp)
6212 {
6213   if (fsp == NULL || lfp == NULL) return;
6214 
6215   /* size */
6216   if (lfp->lfHeight == 0) { /* lfHeight is not specified */
6217     fsp->size = 12; /* use default height */
6218   } else {
6219     int sizelp; /* size in logical points */
6220     if (lfp->lfHeight < 0) { /* -lfHeight is character height */
6221       sizelp = -lfp->lfHeight;
6222     } else { /* lfHeight is cell height */
6223       sizelp = lfp->lfHeight;
6224       sizelp -= sizelp / 10;  /* adhoc: 10% for internal leading */
6225     }
6226     { /* determine size in typographical points */
6227       HDC hDC = GetDC (NULL);
6228       fsp->size = (Nlm_Int2)MulDiv(sizelp, 72, GetDeviceCaps(hDC, LOGPIXELSY));
6229       ReleaseDC (NULL, hDC);
6230     }
6231   }
6232 
6233   { /* style */
6234     int style = 0;
6235     if (lfp->lfWeight > 600) style |= STYLE_BOLD;
6236     if (lfp->lfItalic != 0) style |= STYLE_ITALIC;
6237     if (lfp->lfUnderline != 0) style |= STYLE_UNDERLINE;
6238     if (lfp->lfStrikeOut != 0) style |= 128; /* Windows-specific */
6239     fsp->style = (Nlm_Uint1)style;
6240   }
6241 
6242   /* character set */
6243   switch (lfp->lfCharSet) {
6244     case SYMBOL_CHARSET: fsp->charset = CHARSET_SYMBOL; break;
6245     case ANSI_CHARSET: fsp->charset = CHARSET_ANSI; break;
6246     default: fsp->charset = CHARSET_NULL;
6247   }
6248 
6249   /* pitch */
6250   switch (lfp->lfPitchAndFamily & 0x0F) {
6251     case FIXED_PITCH: fsp->pitch = PITCH_FIXED; break;
6252     case VARIABLE_PITCH: fsp->pitch = PITCH_VARIABLE; break;
6253     default: fsp->pitch = PITCH_NULL;
6254   }
6255 
6256   /* family */
6257   switch (lfp->lfPitchAndFamily & 0xF0) {
6258     case FF_ROMAN: fsp->family = FAMILY_ROMAN; break;
6259     case FF_SWISS: fsp->family = FAMILY_SWISS; break;
6260     case FF_MODERN: fsp->family = FAMILY_MODERN; break;
6261     case FF_SCRIPT: fsp->family = FAMILY_SCRIPT; break;
6262     case FF_DECORATIVE: fsp->family = FAMILY_DECORATIVE; break;
6263     default: fsp->family = FAMILY_NULL;
6264   }
6265 
6266   /* name */
6267   Nlm_StringNCpy_0(fsp->name, lfp->lfFaceName, FONT_NAME_SIZE);
6268 }
6269 
Nlm_ChooseFont(Nlm_FontSpecPtr fsp,Nlm_Uint2 flags,Nlm_VoidPtr null)6270 extern Nlm_Boolean Nlm_ChooseFont (Nlm_FontSpecPtr fsp,
6271                                    Nlm_Uint2 flags,
6272                                    Nlm_VoidPtr null)
6273 {
6274   Nlm_Boolean initToFsp = (Nlm_Boolean)((flags & CFF_READ_FSP) != 0);
6275   Nlm_Boolean fixedOnly = (Nlm_Boolean)((flags & CFF_MONOSPACE) != 0);
6276   LOGFONT lf;
6277   CHOOSEFONT cf;
6278   if (fsp == NULL) { /* ERROR */  return FALSE; }
6279   if (initToFsp) Nlm_FontSpecToLOGFONT (fsp, &lf);
6280   memset (&cf, 0, sizeof (CHOOSEFONT));
6281   cf.lStructSize = sizeof (CHOOSEFONT);
6282   cf.hwndOwner = Nlm_currentHWnd;
6283   cf.hDC = NULL;
6284   cf.lpLogFont = &lf;
6285   cf.Flags = CF_SCREENFONTS | CF_NOVECTORFONTS | CF_EFFECTS;
6286   if (initToFsp) cf.Flags |= CF_INITTOLOGFONTSTRUCT;
6287   if (fixedOnly) cf.Flags |= CF_FIXEDPITCHONLY;
6288   if (ChooseFont (&cf)) {
6289     Nlm_LOGFONTToFontSpec (&lf, fsp);
6290     return TRUE;
6291   } else
6292     return FALSE;
6293 }
6294 
6295 #else /* !WIN_MSWIN */
6296 
6297 static Nlm_FontSpec     fntDlgFontSpec;
6298 static Nlm_Boolean      fntDlgUp;
6299 static Nlm_Boolean      fntDlgResult;
6300 #ifdef WIN_MAC
6301 static Nlm_PopuP        fntDlgFontList = NULL;
6302 static Nlm_PopuP        fntDlgSizeList = NULL;
6303 #endif
6304 #ifdef WIN_MOTIF
6305 static Nlm_LisT         fntDlgFontList = NULL;
6306 static Nlm_LisT         fntDlgSizeList = NULL;
6307 #endif
6308 static Nlm_ButtoN       fntDlgBoldBox = NULL;
6309 static Nlm_ButtoN       fntDlgItalicBox = NULL;
6310 static Nlm_ButtoN       fntDlgUnderlineBox = NULL;
6311 static Nlm_PaneL        fntDlgSample = NULL;
6312 
6313 #define NLM_STYLE_BOLD_ON        0x1
6314 #define NLM_STYLE_BOLD_OFF       0x2
6315 #define NLM_STYLE_ITALIC_ON      0x4
6316 #define NLM_STYLE_ITALIC_OFF     0x8
6317 #define NLM_STYLE_UNDERLINE_ON   0x10
6318 #define NLM_STYLE_UNDERLINE_OFF  0x20
6319 
Nlm_FntDlgDrawSampleProc(Nlm_PaneL p)6320 static void Nlm_FntDlgDrawSampleProc (Nlm_PaneL p)
6321 {
6322   Nlm_FonT font = Nlm_CreateFont (&fntDlgFontSpec);
6323   Nlm_RecT r;
6324   Nlm_ObjectRect (p, &r);
6325   Nlm_SelectFont (font);
6326   Nlm_DrawString (&r, "The Quick Brown Fox", 'c', FALSE);
6327   Nlm_SelectFont (Nlm_systemFont);
6328   Nlm_DeleteFont (font);
6329 }
6330 
Nlm_FntDlgRedrawSample(void)6331 static void Nlm_FntDlgRedrawSample (void)
6332 {
6333   Nlm_RecT r;
6334   Nlm_WindoW tmpPort;
6335 
6336   Nlm_ObjectRect (fntDlgSample, &r);
6337   tmpPort = Nlm_SavePort (fntDlgSample);
6338   Nlm_Select (fntDlgSample);
6339   Nlm_InvalRect (&r);
6340   Nlm_RestorePort (tmpPort);
6341 }
6342 
6343 /* simple font name/size enum interface (platform-dependent) */
6344 /* alexs (01-16-95): add platform-dependent for MAC */
6345 #ifdef WIN_MAC
6346 
6347 MenuHandle menuHforFontList;
6348 
Nlm_FntDlgGetSysFontList(Nlm_Boolean monoSpace)6349 static Nlm_Int2 Nlm_FntDlgGetSysFontList (Nlm_Boolean monoSpace)
6350 {
6351   CreateNewMenu (100, 0, &menuHforFontList);
6352   CreateStandardFontMenu (menuHforFontList, 0, 0, 0, NULL);
6353 
6354   if (monoSpace)
6355   {
6356     int item;
6357     int size;
6358     char *name;
6359     Nlm_FonT font;
6360     int i;
6361     int M;
6362     int s;
6363 
6364     for (item = CountMenuItems (menuHforFontList); item > 0; item--)
6365     {
6366       CFStringRef cfname = NULL;
6367       CopyMenuItemTextAsCFString (menuHforFontList, item, &cfname);
6368 
6369       size = CFStringGetMaximumSizeForEncoding (CFStringGetLength (cfname), kCFStringEncodingUTF8);
6370       name = malloc (size + 1);
6371       CFStringGetCString (cfname, name, size + 1, kCFStringEncodingUTF8);
6372 
6373       font = Nlm_GetFont (name, 12, 0, 0, 0, "");
6374 
6375       i = Nlm_CharWidth ('i');
6376       M = Nlm_CharWidth ('M');
6377       s = Nlm_CharWidth (' ');
6378       if( i != M || i != s )
6379         DeleteMenuItem (menuHforFontList, item);
6380 
6381       free (name);
6382       CFRelease (cfname);
6383     }
6384   }
6385 
6386   return CountMenuItems (menuHforFontList);
6387 }
6388 
Nlm_FntDlgGetSysFontName(Nlm_Int2 n,Nlm_CharPtr buf,Nlm_Int2 blen)6389 static void Nlm_FntDlgGetSysFontName (Nlm_Int2 n, Nlm_CharPtr buf,
6390                                       Nlm_Int2 blen)
6391 {
6392   char fontName[256];
6393 
6394   GetMenuItemText ( menuHforFontList, n+1, (StringPtr) fontName );
6395   Nlm_PtoCstr ( fontName );
6396   Nlm_StringNCpy_0(buf, fontName, blen);
6397 }
6398 
6399 static Nlm_Byte fontSizes [] = {9, 10, 12, 14, 18, 20, 24, 36};
6400 static Nlm_Int2 fontSizesCnt = sizeof(fontSizes) / sizeof(fontSizes[0]);
6401 
Nlm_FntDlgGetSysFontSize(Nlm_Int2 n,Nlm_BytePtr PNTR sizePtr)6402 static Nlm_Int2 Nlm_FntDlgGetSysFontSize (Nlm_Int2 n,
6403                                           Nlm_BytePtr PNTR sizePtr )
6404 {
6405   n = n;
6406   *sizePtr = &(fontSizes[0]);
6407   return fontSizesCnt;
6408 }
6409 
Nlm_FntDlgGetSysFontStyle(Nlm_Int2 n)6410 static Nlm_Byte Nlm_FntDlgGetSysFontStyle (Nlm_Int2 n)
6411 {
6412   return ( NLM_STYLE_BOLD_ON |
6413            NLM_STYLE_BOLD_OFF |
6414            NLM_STYLE_ITALIC_ON |
6415            NLM_STYLE_ITALIC_OFF |
6416            NLM_STYLE_UNDERLINE_ON |
6417            NLM_STYLE_UNDERLINE_OFF );
6418 }
6419 
Nlm_FntDlgFreeSysFontList()6420 static void Nlm_FntDlgFreeSysFontList ()
6421 {
6422   DisposeMenu (menuHforFontList);
6423 }
6424 
6425 #endif
6426 #ifdef WIN_MOTIF
6427 
6428 /* alexs (01-16-95): add platform-dependent for X_WIN */
6429 #define NLM_X11_MAXSIZECOUNT   64
6430 
6431 typedef struct {
6432   Nlm_Char  fontName[FONT_NAME_SIZE];
6433   Nlm_Byte  styleInter;
6434   Nlm_Byte  sizeFound;
6435   Nlm_Byte  sizes[NLM_X11_MAXSIZECOUNT];
6436 } Nlm_X11_FontDsc, * Nlm_X11_FontDscPtr;
6437 
6438 static Nlm_X11_FontDscPtr fontNamesArray = NULL;
6439 
Nlm_FntGetFontStyle(char * fontSpec)6440 static Nlm_Byte Nlm_FntGetFontStyle ( char * fontSpec )
6441 {
6442   Nlm_Byte fStyle = 0;
6443 
6444   fontSpec = Nlm_StringChr ( fontSpec, '-' );
6445   if ( fontSpec == NULL ) return 0;
6446   fontSpec = Nlm_StringChr ( fontSpec+1, '-');
6447   if ( fontSpec == NULL ) return 0;
6448   fontSpec = Nlm_StringChr ( fontSpec+1, '-');
6449   if ( fontSpec == NULL ) return 0;
6450   fontSpec++;
6451   if ( *fontSpec == 'b' ) fStyle |= NLM_STYLE_BOLD_ON;
6452   else fStyle |= NLM_STYLE_BOLD_OFF;
6453   fontSpec = Nlm_StringChr ( fontSpec, '-');
6454   if ( fontSpec == NULL ) return fStyle;
6455   fontSpec++;
6456   /* VL */
6457   if ( *fontSpec == 'i' || *fontSpec == 'o' ) fStyle |= NLM_STYLE_ITALIC_ON;
6458   else fStyle |= NLM_STYLE_ITALIC_OFF;
6459   return fStyle;
6460 }
6461 
Nlm_FntAddSize(Nlm_X11_FontDscPtr fontDsc,char * fontSpec)6462 static void Nlm_FntAddSize ( Nlm_X11_FontDscPtr fontDsc,
6463                              char * fontSpec )
6464 {
6465   int      i;
6466   int      curSize = 12;
6467   char     sizeStr[16];
6468 
6469   if ( fontDsc->sizeFound == NLM_X11_MAXSIZECOUNT ) return;
6470   for ( i=0; i<8; i++ ){
6471     fontSpec = Nlm_StringChr ( fontSpec, '-' );
6472     if ( fontSpec == NULL ) break;
6473     fontSpec++;
6474   }
6475   if ( fontSpec != NULL ){
6476     Nlm_StringNCpy_0(sizeStr, fontSpec, sizeof(sizeStr));
6477     fontSpec = Nlm_StringChr ( sizeStr, '-' );
6478     if ( fontSpec != NULL ) *fontSpec = '\0';
6479     curSize = atoi ( sizeStr ) / 10 ;
6480     if ( (curSize <= 0) || (curSize > 128) ) curSize = 12;
6481   }
6482   for ( i=0; i < (int)fontDsc->sizeFound; i++ ){
6483      if ( curSize == (int)fontDsc->sizes[i] ) break;
6484      if ( curSize < (int)fontDsc->sizes[i] ) {
6485         Nlm_MemMove ( &(fontDsc->sizes[i+1]), &(fontDsc->sizes[i]),
6486                       fontDsc->sizeFound - i );
6487         fontDsc->sizes[i] = (Nlm_Byte)curSize;
6488         fontDsc->sizeFound++;
6489         break;
6490      }
6491   }
6492   if ( i == fontDsc->sizeFound ){
6493     fontDsc->sizes[fontDsc->sizeFound] = (Nlm_Byte)curSize;
6494     fontDsc->sizeFound++;
6495   }
6496 }
6497 
Nlm_FntDlgGetSysFontList(Nlm_Boolean mono)6498 static Nlm_Int2 Nlm_FntDlgGetSysFontList (Nlm_Boolean mono)
6499 {
6500   char ** fontNames;
6501   char *  curFontPtr;
6502   Nlm_X11_FontDscPtr  curFontDsc;
6503   int     fontNamesCount;
6504   int     fontsFound = 0;
6505   int     i, ifound;
6506   char    curFontName[FONT_NAME_SIZE];
6507 
6508   if (mono) {
6509     fontNames = XListFonts ( Nlm_currentXDisplay,
6510 			     "-*-*-*-*-*-*-*-*-*-M-*",
6511 			     4096, &fontNamesCount );
6512   } else {
6513     fontNames = XListFonts ( Nlm_currentXDisplay,
6514 			     "-*-*-*-*-*-*-*-*-*-*-*",
6515 			     4096, &fontNamesCount );
6516   }
6517   if ( fontNames == NULL ) return 0;
6518   fontNamesArray = (Nlm_X11_FontDscPtr)Nlm_MemNew (
6519       sizeof(Nlm_X11_FontDsc) * fontNamesCount );
6520   for ( i=0; i<fontNamesCount; i++ ){
6521     curFontPtr = Nlm_StringChr (fontNames[i], '-' );
6522     if ( curFontPtr == NULL ) continue;
6523     curFontPtr++;
6524     curFontPtr = Nlm_StringChr (curFontPtr, '-' );
6525     if ( curFontPtr == NULL ) continue;
6526     curFontPtr++;
6527     Nlm_StringNCpy_0(curFontName, curFontPtr, FONT_NAME_SIZE);
6528     curFontPtr = Nlm_StringChr ( curFontName, '-' );
6529     if ( curFontPtr != NULL ) *curFontPtr = '\0';
6530     if ( Nlm_StringLen(curFontName) == 0 ) continue;
6531     for ( ifound = 0, curFontDsc=fontNamesArray;
6532           ifound < fontsFound;
6533           ifound++, curFontDsc ++ ){
6534       if ( Nlm_StrCmp ( curFontDsc->fontName, curFontName ) == 0 ) {
6535         curFontDsc->styleInter |= Nlm_FntGetFontStyle ( (char*)fontNames[i]) ;
6536         Nlm_FntAddSize ( curFontDsc, (char*)fontNames[i] );
6537         break;
6538       }
6539     }
6540     if ( ifound == fontsFound ){
6541       Nlm_StrCpy ( curFontDsc->fontName, curFontName );
6542       curFontDsc->styleInter = Nlm_FntGetFontStyle ( (char*)fontNames[i]) ;
6543       Nlm_FntAddSize ( curFontDsc, (char*)fontNames[i] );
6544       fontsFound++;
6545     }
6546   }
6547   XFreeFontNames ( fontNames );
6548   return fontsFound;
6549 }
6550 
Nlm_FntDlgGetSysFontName(Nlm_Int2 n,Nlm_CharPtr buf,Nlm_Int2 blen)6551 static void Nlm_FntDlgGetSysFontName (Nlm_Int2 n, Nlm_CharPtr buf,
6552                                       Nlm_Int2 blen)
6553 {
6554   Nlm_StringNCpy_0(buf, fontNamesArray[n].fontName, blen);
6555 }
6556 
Nlm_FntDlgGetSysFontSize(Nlm_Int2 n,Nlm_BytePtr PNTR sizePtr)6557 static Nlm_Int2 Nlm_FntDlgGetSysFontSize (Nlm_Int2 n,
6558                                           Nlm_BytePtr PNTR sizePtr )
6559 {
6560   *sizePtr = fontNamesArray[n].sizes;
6561   return (fontNamesArray[n].sizeFound );
6562 }
6563 
Nlm_FntDlgGetSysFontStyle(Nlm_Int2 n)6564 static Nlm_Byte Nlm_FntDlgGetSysFontStyle (Nlm_Int2 n)
6565 {
6566   return ( fontNamesArray[n].styleInter );
6567 }
6568 
Nlm_FntDlgFreeSysFontList(void)6569 static void Nlm_FntDlgFreeSysFontList(void)
6570 {
6571   if ( fontNamesArray != NULL ){
6572     Nlm_MemFree ( fontNamesArray );
6573     fontNamesArray = NULL;
6574   }
6575 }
6576 #endif
6577 
6578 /* simple font name/size enum interface (platform-independent) */
6579 static Nlm_Int2 sysFontCnt;
6580 
6581 
6582 /* init FontList */
Nlm_FntDlgInitFontList(void)6583 static void Nlm_FntDlgInitFontList (void)
6584 {
6585   Nlm_Int2 i;
6586   Nlm_Char buf [FONT_NAME_SIZE];
6587   for (i = 0; i < sysFontCnt; i++) {
6588     Nlm_FntDlgGetSysFontName (i, buf, FONT_NAME_SIZE);
6589 #ifdef WIN_MAC
6590     Nlm_PopupItem (fntDlgFontList, buf);
6591 #endif
6592 #ifdef WIN_MOTIF
6593     Nlm_ListItem (fntDlgFontList, buf);
6594 #endif
6595   }
6596 }
6597 
Nlm_FntDlgInitSizeList(Nlm_Int2 n,Nlm_Int2 oldSize)6598 static void Nlm_FntDlgInitSizeList ( Nlm_Int2 n, Nlm_Int2 oldSize )
6599 {
6600   Nlm_BytePtr sizePtr;
6601   Nlm_Int2 i;
6602   Nlm_Int2 setThis;
6603   Nlm_Int2 fontCnt;
6604   Nlm_Char buf [10];
6605 
6606   fontCnt = Nlm_FntDlgGetSysFontSize ( n, &sizePtr );
6607   setThis = 0;
6608   for (i = 0; i < fontCnt; i++) {
6609     if ( !setThis ){
6610       if ( (Nlm_Int2)sizePtr[i] >= oldSize ) setThis = i+1;
6611     }
6612     sprintf (buf, "%d", (int)sizePtr[i]);
6613 #ifdef WIN_MAC
6614     Nlm_PopupItem (fntDlgSizeList, buf);
6615 #endif
6616 #ifdef WIN_MOTIF
6617     Nlm_ListItem (fntDlgSizeList, buf);
6618 #endif
6619   }
6620   if ( !setThis ) setThis = fontCnt;
6621   Nlm_SetValue (fntDlgSizeList, setThis);
6622 }
6623 
Nlm_FntDlgInitStyles(Nlm_Int2 n,Nlm_Int2 oldStyle)6624 static void Nlm_FntDlgInitStyles ( Nlm_Int2 n, Nlm_Int2 oldStyle )
6625 {
6626   Nlm_Byte styles;
6627 
6628   styles = Nlm_FntDlgGetSysFontStyle (n);
6629   if ( (styles & NLM_STYLE_BOLD_ON) && (styles & NLM_STYLE_BOLD_OFF) ){
6630     Nlm_Enable (fntDlgBoldBox);
6631     if ( oldStyle & STYLE_BOLD ){
6632       Nlm_SetStatus (fntDlgBoldBox, TRUE);
6633     } else {
6634       Nlm_SetStatus (fntDlgBoldBox, FALSE);
6635     }
6636   }else{
6637     Nlm_Disable (fntDlgBoldBox);
6638     if ( styles & NLM_STYLE_BOLD_ON ){
6639       Nlm_SetStatus (fntDlgBoldBox, TRUE);
6640     } else {
6641       Nlm_SetStatus (fntDlgBoldBox, FALSE);
6642     }
6643   }
6644   if ( (styles & NLM_STYLE_ITALIC_ON) && (styles & NLM_STYLE_ITALIC_OFF) ){
6645     Nlm_Enable (fntDlgItalicBox);
6646     if ( oldStyle & STYLE_ITALIC ){
6647       Nlm_SetStatus (fntDlgItalicBox, TRUE);
6648     } else {
6649       Nlm_SetStatus (fntDlgItalicBox, FALSE);
6650     }
6651   }else{
6652     Nlm_Disable (fntDlgItalicBox);
6653     if ( styles & NLM_STYLE_ITALIC_ON ){
6654       Nlm_SetStatus (fntDlgItalicBox, TRUE);
6655     } else {
6656       Nlm_SetStatus (fntDlgItalicBox, FALSE);
6657     }
6658   }
6659   if ( (styles & NLM_STYLE_UNDERLINE_ON) &&
6660        (styles & NLM_STYLE_UNDERLINE_OFF) ){
6661     Nlm_Enable (fntDlgUnderlineBox);
6662     if ( oldStyle & STYLE_UNDERLINE ){
6663       Nlm_SetStatus (fntDlgUnderlineBox, TRUE);
6664     } else {
6665       Nlm_SetStatus (fntDlgUnderlineBox, FALSE);
6666     }
6667   }else{
6668     Nlm_Disable (fntDlgUnderlineBox);
6669     if ( styles & NLM_STYLE_UNDERLINE_ON ){
6670       Nlm_SetStatus (fntDlgUnderlineBox, TRUE);
6671     } else {
6672       Nlm_SetStatus (fntDlgUnderlineBox, FALSE);
6673     }
6674   }
6675 }
6676 
Nlm_FntDlgUpdate(Nlm_Boolean writep)6677 static void Nlm_FntDlgUpdate (Nlm_Boolean writep)
6678 {
6679   if (writep) {
6680     Nlm_BytePtr sizes;
6681     Nlm_Int2    i;
6682     Nlm_Int2    sizeCount;
6683 
6684     /* name and family/pitch/charset */
6685     i = Nlm_GetValue (fntDlgFontList);
6686     if (i > 0 && i <= sysFontCnt) {
6687       Nlm_MemSet (fntDlgFontSpec.name, 0, FONT_NAME_SIZE);
6688       Nlm_FntDlgGetSysFontName (i - 1, fntDlgFontSpec.name, FONT_NAME_SIZE);
6689       /* name is set! clear family/pitch/charset fields */
6690       fntDlgFontSpec.family = FAMILY_NULL;
6691       fntDlgFontSpec.pitch = PITCH_NULL;
6692       fntDlgFontSpec.charset = CHARSET_NULL;
6693       /* size */
6694       sizeCount = Nlm_FntDlgGetSysFontSize (i-1, &sizes );
6695       i = Nlm_GetValue (fntDlgSizeList);
6696       if (i > 0 && i <=  sizeCount ){
6697         fntDlgFontSpec.size = sizes[i - 1];
6698       }
6699     }
6700     /* style */
6701     i = 0;
6702     if (Nlm_GetStatus (fntDlgBoldBox)) i |= STYLE_BOLD;
6703     if (Nlm_GetStatus (fntDlgItalicBox)) i |= STYLE_ITALIC;
6704     if (Nlm_GetStatus (fntDlgUnderlineBox)) i |= STYLE_UNDERLINE;
6705     fntDlgFontSpec.style = (Nlm_Uint1) i;
6706   } else {
6707     Nlm_Int2 i = 0;
6708     Nlm_Int2 valSet = 0;
6709     Nlm_Char buf[FONT_NAME_SIZE];
6710     /* name */
6711     if (fntDlgFontSpec.name [0] != '\0'){
6712       for (i = 0; i < sysFontCnt; i++) {
6713         Nlm_FntDlgGetSysFontName (i, buf, FONT_NAME_SIZE);
6714         if (Nlm_StrNICmp (buf, fntDlgFontSpec.name, FONT_NAME_SIZE) == 0) {
6715           Nlm_SetValue (fntDlgFontList, i + 1);
6716           valSet = 1;
6717           break;
6718         }
6719       }
6720     }
6721     if ( !valSet ){
6722       Nlm_SetValue (fntDlgFontList,1);
6723       i = 0;
6724     }
6725     Nlm_FntDlgInitSizeList ( i, fntDlgFontSpec.size );
6726     Nlm_FntDlgInitStyles ( i, fntDlgFontSpec.style );
6727   }
6728 }
6729 
Nlm_FntDlgOKProc(Nlm_ButtoN b)6730 static void Nlm_FntDlgOKProc (Nlm_ButtoN b)
6731 {
6732   fntDlgUp = FALSE; fntDlgResult = TRUE;
6733 }
6734 
Nlm_FntDlgCancelProc(Nlm_ButtoN b)6735 static void Nlm_FntDlgCancelProc (Nlm_ButtoN b)
6736 {
6737   fntDlgUp = FALSE; fntDlgResult = FALSE;
6738 }
6739 
Nlm_FntDlgCheckBoxProc(Nlm_ButtoN b)6740 static void Nlm_FntDlgCheckBoxProc (Nlm_ButtoN b)
6741 {
6742   Nlm_FntDlgUpdate (TRUE);
6743   Nlm_FntDlgRedrawSample ();
6744 }
6745 
6746 #ifdef WIN_MAC
Nlm_FntDlgPopupProc(Nlm_PopuP p)6747 static void Nlm_FntDlgPopupProc (Nlm_PopuP p)
6748 {
6749   Nlm_FntDlgUpdate (TRUE);
6750   Nlm_FntDlgRedrawSample ();
6751 }
6752 #endif
6753 
6754 #ifdef WIN_MOTIF
Nlm_FntDlgListProc(Nlm_LisT l)6755 static void Nlm_FntDlgListProc (Nlm_LisT l)
6756 {
6757   Nlm_Int2 i;
6758 
6759   i = Nlm_GetValue (fntDlgFontList);
6760   if (i > 0 && i <= sysFontCnt) {
6761     Nlm_Hide ( fntDlgSizeList );
6762     Nlm_Reset ( fntDlgSizeList );
6763     Nlm_FntDlgInitSizeList ( i-1, fntDlgFontSpec.size );
6764     Nlm_Show ( fntDlgSizeList );
6765     Nlm_FntDlgInitStyles ( i-1, fntDlgFontSpec.style );
6766   }
6767   Nlm_FntDlgUpdate (TRUE);
6768   Nlm_FntDlgRedrawSample ();
6769 }
6770 
Nlm_FntDlgListSizeProc(Nlm_LisT l)6771 static void Nlm_FntDlgListSizeProc (Nlm_LisT l)
6772 {
6773   Nlm_FntDlgUpdate (TRUE);
6774   Nlm_FntDlgRedrawSample ();
6775 }
6776 #endif
6777 
Nlm_ChooseFont(Nlm_FontSpecPtr fsp,Nlm_Uint2 flags,Nlm_VoidPtr null)6778 extern Nlm_Boolean Nlm_ChooseFont (Nlm_FontSpecPtr fsp,
6779                                    Nlm_Uint2 flags,
6780                                    Nlm_VoidPtr null)
6781 {
6782   Nlm_Boolean initToFsp = (Nlm_Boolean)((flags & CFF_READ_FSP) != 0);
6783   Nlm_Boolean fixedOnly = (Nlm_Boolean)((flags & CFF_MONOSPACE) != 0);
6784   Nlm_WindoW w = Nlm_MovableModalWindow (-50, -20, -20, -20, "Font", NULL);
6785   Nlm_GrouP gfontsize, gstysamp, gbuttons;
6786 
6787   sysFontCnt = Nlm_FntDlgGetSysFontList (fixedOnly);
6788   { Nlm_GrouP g = Nlm_HiddenGroup (w, 10, 0, NULL);
6789     gfontsize = g;
6790     Nlm_SetGroupSpacing (g, 10, 5);
6791     Nlm_StaticPrompt (g, "Font:", 0, 0, Nlm_systemFont, 'r');
6792 #ifdef WIN_MAC
6793     fntDlgFontList = Nlm_PopupList (g, TRUE, Nlm_FntDlgPopupProc);
6794 #endif
6795 #ifdef WIN_MOTIF
6796     fntDlgFontList = Nlm_SingleList (g, 16, 6, Nlm_FntDlgListProc);
6797 #endif
6798     Nlm_FntDlgInitFontList ();
6799     Nlm_StaticPrompt (g, "Size:", 0, 0, Nlm_systemFont, 'r');
6800 #ifdef WIN_MAC
6801     fntDlgSizeList = Nlm_PopupList (g, TRUE, Nlm_FntDlgPopupProc);
6802 #endif
6803 #ifdef WIN_MOTIF
6804     fntDlgSizeList = Nlm_SingleList (g, 3, 5, Nlm_FntDlgListSizeProc);
6805 #endif
6806   }
6807   { Nlm_GrouP g = Nlm_HiddenGroup (w, 10, 0, NULL);
6808     Nlm_GrouP gstyle, gsample;
6809     gstysamp = g;
6810     Nlm_SetGroupSpacing (g, 10, 5);
6811     { Nlm_GrouP s = Nlm_NormalGroup (g, 0, 3, "Style:", NULL, NULL);
6812       gstyle = s;
6813       Nlm_SetGroupMargins (s, 10, 5);
6814       Nlm_SetGroupSpacing (s, 10, 5);
6815       fntDlgBoldBox = Nlm_CheckBox (s, "Bold", Nlm_FntDlgCheckBoxProc);
6816       fntDlgItalicBox = Nlm_CheckBox (s, "Italic", Nlm_FntDlgCheckBoxProc);
6817       fntDlgUnderlineBox = Nlm_CheckBox (s, "Underline", Nlm_FntDlgCheckBoxProc);
6818     }
6819     { Nlm_GrouP s = Nlm_NormalGroup (g, 0, 10, "Sample", NULL, NULL);
6820       Nlm_Int2 pixwidth = Nlm_stdCharWidth * 15;
6821       Nlm_Int2 pixheight = Nlm_stdLineHeight * 5;
6822       gsample = s;
6823       Nlm_SetGroupSpacing (s, 10, 5);
6824       fntDlgSample = Nlm_SimplePanel (s, pixwidth, pixheight,
6825                                       Nlm_FntDlgDrawSampleProc);
6826     }
6827     /*
6828     Nlm_AlignObjects (ALIGN_RIGHT, (Nlm_HANDLE) fntDlgSizeList,
6829                       (Nlm_HANDLE) gsample, NULL);
6830     */
6831     Nlm_AlignObjects (ALIGN_LOWER, (Nlm_HANDLE) gstyle,
6832                                     (Nlm_HANDLE) gsample, NULL);
6833   }
6834   { Nlm_GrouP g = Nlm_HiddenGroup (w, 10, 0, NULL);
6835     gbuttons = g;
6836     Nlm_SetGroupSpacing (g, 10, 5);
6837     Nlm_DefaultButton (g, "  OK  ", Nlm_FntDlgOKProc);
6838     Nlm_PushButton (g, "Cancel", Nlm_FntDlgCancelProc);
6839   }
6840   Nlm_AlignObjects (ALIGN_CENTER, (Nlm_HANDLE) gfontsize,
6841                                   (Nlm_HANDLE) gstysamp,
6842                                   (Nlm_HANDLE) gbuttons, NULL);
6843   /* init fontspec */
6844   if (initToFsp && fsp != NULL) {
6845     fntDlgFontSpec = *fsp;
6846   } else {
6847     Nlm_GetFontSpec (fixedOnly ? Nlm_programFont : Nlm_systemFont,
6848                      &fntDlgFontSpec);
6849   }
6850   /* update controls */
6851   Nlm_FntDlgUpdate (FALSE);
6852   /* show dialog */
6853   Nlm_DoShow ((Nlm_GraphiC) w, TRUE, TRUE);
6854   Nlm_ArrowCursor ();
6855   fntDlgUp = TRUE;
6856   fntDlgResult = FALSE;
6857   Nlm_WaitForCondition( fntDlgUp );
6858   Nlm_ProcessAnEvent ();
6859   /* update FontSpec */
6860   Nlm_FntDlgUpdate (TRUE);
6861   Nlm_DoRemove ((Nlm_GraphiC) w, TRUE);
6862   if (fntDlgResult == TRUE && fsp != NULL) *fsp = fntDlgFontSpec;
6863   Nlm_FntDlgFreeSysFontList ();
6864   return fntDlgResult;
6865 }
6866 
6867 #endif /* !WIN_MSWIN */
6868 
6869 /* FontSpec/string conversions */
6870 
Nlm_StrToFontSpec(Nlm_CharPtr str,Nlm_FontSpecPtr fsp)6871 extern Nlm_Boolean Nlm_StrToFontSpec (Nlm_CharPtr str, Nlm_FontSpecPtr fsp)
6872 {
6873   Nlm_CharPtr cp;
6874 
6875   if (fsp == NULL  ||  str == NULL)
6876     return FALSE;
6877 
6878   /* get name */
6879   if ((cp = Nlm_StrChr(str, ',')) == NULL)
6880     return FALSE;
6881 
6882   Nlm_StringNCpy_0(fsp->name, str, MIN(cp - str + 1, FONT_NAME_SIZE));
6883 
6884   {{ /* get other parameters */
6885     int size, style, charset, pitch, family;
6886     int n = sscanf (cp, ",%d,%d,%d,%d,%d",
6887                     &size, &style, &charset, &pitch, &family);
6888     fsp->size = (Nlm_Int2) size;
6889     fsp->style = (Nlm_Uint1) style;
6890     fsp->charset = (Nlm_Uint1) charset;
6891     fsp->pitch = (Nlm_Uint1) pitch;
6892     fsp->family = (Nlm_Uint1) family;
6893     return (Nlm_Boolean)(n == 5);
6894   }}
6895 }
6896 
Nlm_FontSpecToStr(Nlm_FontSpecPtr fsp,Nlm_CharPtr str,size_t maxsize)6897 extern void Nlm_FontSpecToStr (Nlm_FontSpecPtr fsp, Nlm_CharPtr str, size_t maxsize)
6898 {
6899   Nlm_Char  temp [80];
6900   if (fsp == NULL) {
6901     temp[0] = '\0';
6902   } else {
6903     sprintf (temp, "%s,%d,%d,%d,%d,%d", fsp->name,
6904                    (int) fsp->size, (int) fsp->style,
6905                    (int) fsp->charset, (int) fsp->pitch,
6906                    (int) fsp->family);
6907   }
6908   Nlm_StringNCpy_0(str, temp, maxsize);
6909 }
6910 
6911 
6912 
6913 #ifdef OS_UNIX
Nlm_FineGranularitySleep(Nlm_Int4 seconds,Nlm_Int4 microseconds)6914 extern void Nlm_FineGranularitySleep (Nlm_Int4 seconds, Nlm_Int4 microseconds)
6915 {
6916       struct timeval tv;
6917 
6918       tv.tv_sec = seconds;
6919       tv.tv_usec = microseconds;
6920       select(1, NULL, NULL, NULL, &tv); /* fine granularity sleep */
6921 }
6922 #endif /* OS_UNIX */
6923 
6924 #ifdef WIN_MOTIF
Nlm_WaitForXEvent(void)6925 extern void Nlm_WaitForXEvent( void )
6926 {
6927   XEvent event;
6928   XPeekEvent(Nlm_currentXDisplay, &event);
6929 }
6930 #endif
6931 
Nlm_SetRealized(Nlm_GraphiC a,Nlm_Boolean realizd)6932 extern void Nlm_SetRealized(Nlm_GraphiC a, Nlm_Boolean realizd)
6933 {
6934   Nlm_Boolean  switched;
6935 
6936   if ( !a )  return;
6937 
6938   {{
6939     Nlm_GraphicData  gdata;
6940     Nlm_GetGraphicData (a, &gdata);
6941     switched = (Nlm_Boolean)(gdata.realized != realizd);
6942     gdata.realized = realizd;
6943     Nlm_SetGraphicData (a, &gdata);
6944   }}
6945 
6946   if (switched  &&  realizd)
6947     {
6948       Nlm_RecT r;
6949       Nlm_GetPosition(a, &r);
6950       Nlm_DoSetPosition(a, &r, TRUE, TRUE);
6951     }
6952 
6953   for (a = Nlm_GetChild( a );  a;  a = Nlm_GetNext( a ))
6954     {
6955       Nlm_SetRealized(a, realizd);
6956     }
6957 }
6958 
6959 
Nlm_GetRealized(Nlm_GraphiC a)6960 extern Nlm_Boolean Nlm_GetRealized(Nlm_GraphiC a)
6961 {
6962   Nlm_GraphicData  gdata;
6963 
6964   if ( a )
6965     {
6966       Nlm_GetGraphicData(a, &gdata);
6967       return gdata.realized;
6968     }
6969 
6970   return FALSE;
6971 }
6972 
6973 #ifdef WIN_MSWIN
6974 #ifdef WIN16
6975 extern Nlm_Uint2 Nlm_GetDriveType (int driveNumber);
Nlm_GetDriveType(int driveNumber)6976 extern Nlm_Uint2 Nlm_GetDriveType (int driveNumber)
6977 
6978 {
6979   return (Nlm_Uint2) GetDriveType (driveNumber);
6980 }
6981 #else
6982 extern Nlm_Uint2 Nlm_GetDriveType (char* path);
Nlm_GetDriveType(char * path)6983 extern Nlm_Uint2 Nlm_GetDriveType (char* path)
6984 
6985 {
6986   return (Nlm_Uint2) GetDriveType (path);
6987 }
6988 #endif
6989 #endif
6990 
6991 /*
6992  * Navigation and special key translation and rendering
6993  */
6994 
6995 
6996 #ifdef WIN_MOTIF
Nlm_GetInputChar(XKeyEvent * event)6997 extern Nlm_Char Nlm_GetInputChar(XKeyEvent *event)
6998 {
6999   KeySym keysym;
7000   char   keystr[16];
7001   int    charcount = XLookupString(event, keystr, sizeof(keystr),
7002                                    &keysym, NULL);
7003   switch ( keysym )
7004     {
7005     case XK_Left:
7006       return NLM_LEFT;
7007     case XK_Right:
7008       return NLM_RIGHT;
7009     case XK_Up:
7010       return NLM_UP;
7011     case XK_Down:
7012       return NLM_DOWN;
7013     case XK_Prior:
7014       return NLM_PREV;
7015     case XK_Next:
7016       return NLM_NEXT;
7017     case XK_End:
7018       return NLM_END;
7019     case XK_Home:
7020       return NLM_HOME;
7021     case XK_Delete:
7022       return NLM_DEL;
7023     case XK_Insert:
7024       return NLM_INS;
7025     case XK_Escape:
7026       return NLM_ESC;
7027     }
7028 
7029   return (charcount == 1) ? keystr[0] : '\0';
7030 }
7031 
7032 
Nlm_StdTranslationCB(Widget w,XEvent * event,String * args,Cardinal * num_args)7033 static void Nlm_StdTranslationCB(Widget w, XEvent *event,
7034                                  String *args, Cardinal *num_args)
7035 {
7036   Nlm_Char ch;
7037   XtPointer ptr;
7038 
7039   XtVaGetValues(w, XmNuserData, &ptr, NULL);
7040   ASSERT ( ptr );
7041 
7042   ch = Nlm_GetInputChar( &event->xkey );
7043   ASSERT ( ch );
7044 
7045   Nlm_DoSendFocus((Nlm_GraphiC)ptr, ch);
7046 }
7047 
7048 
Nlm_RegisterStdTranslations(void)7049 extern void Nlm_RegisterStdTranslations( void )
7050 {
7051   XtActionsRec actions;
7052 
7053   actions.string = "Nlm_StdTranslation";
7054   actions.proc   = Nlm_StdTranslationCB;
7055   XtAppAddActions(Nlm_appContext, &actions, 1);
7056 }
7057 
7058 
Nlm_OverrideStdTranslations(Nlm_GraphiC g,Widget w,Nlm_Int4 filter)7059 extern void Nlm_OverrideStdTranslations(Nlm_GraphiC g, Widget w,
7060                                         Nlm_Int4 filter)
7061 {
7062   if ( !(filter & (VERT_PAGE | VERT_ARROW | HORIZ_PAGE | HORIZ_ARROW)) )
7063     return;
7064 
7065 #ifdef _DEBUG
7066   {{
7067     XtPointer ptr;
7068     XtVaGetValues(w, XmNuserData, &ptr, NULL);
7069     ASSERT ( !ptr );
7070   }}
7071 #endif
7072   XtVaSetValues(w, XmNuserData, (XtPointer)g, NULL);
7073 
7074   if (filter & VERT_PAGE)
7075     {
7076       XtOverrideTranslations(w, XtParseTranslationTable( "\
7077 <Key>osfPageUp:    Nlm_StdTranslation() \n\
7078 <Key>osfPageDown:  Nlm_StdTranslation()" ));
7079     }
7080 
7081   if (filter & VERT_ARROW)
7082     {
7083       XtOverrideTranslations(w, XtParseTranslationTable( "\
7084 <Key>osfUp:    Nlm_StdTranslation() \n\
7085 <Key>osfDown:  Nlm_StdTranslation()" ));
7086     }
7087 
7088   if (filter & HORIZ_PAGE)
7089     {
7090       XtOverrideTranslations(w, XtParseTranslationTable( "\
7091 <Key>osfBeginLine:  Nlm_StdTranslation() \n\
7092 <Key>osfEndLine:    Nlm_StdTranslation()" ));
7093     }
7094 
7095   if (filter & HORIZ_ARROW)
7096     {
7097       XtOverrideTranslations(w, XtParseTranslationTable( "\
7098 <Key>osfLeft:   Nlm_StdTranslation() \n\
7099 <Key>osfRight:  Nlm_StdTranslation()" ));
7100     }
7101 }
7102 #endif
7103 
7104 
7105 #ifdef WIN_MSWIN
Nlm_KeydownToChar(WPARAM wParam)7106 extern Nlm_Char Nlm_KeydownToChar(WPARAM wParam)
7107 {
7108   switch ( wParam )
7109     {
7110     case VK_LEFT:
7111       return NLM_LEFT;
7112     case VK_RIGHT:
7113       return NLM_RIGHT;
7114     case VK_UP:
7115       return NLM_UP;
7116     case VK_DOWN:
7117       return NLM_DOWN;
7118     case VK_PRIOR:
7119       return NLM_PREV;
7120     case VK_NEXT:
7121       return NLM_NEXT;
7122     case VK_HOME:
7123       return NLM_HOME;
7124     case VK_END:
7125       return NLM_END;
7126     case VK_INSERT:
7127       return NLM_INS;
7128     case VK_DELETE:
7129       return NLM_DEL;
7130     case VK_ESCAPE:
7131       return NLM_ESC;
7132     case VK_OEM_PLUS:
7133       return '=';
7134     }
7135 
7136   return '\0';
7137 }
7138 
7139 
Nlm_ProcessKeydown(Nlm_GraphiC g,WPARAM wParam,Nlm_Int4 filter)7140 extern Nlm_Boolean Nlm_ProcessKeydown(Nlm_GraphiC g, WPARAM wParam,
7141                                       Nlm_Int4 filter)
7142 {
7143   Nlm_Char ch = Nlm_KeydownToChar( wParam );
7144 
7145   switch ( ch )
7146     {
7147     case '\0':
7148       return FALSE;
7149 
7150     case NLM_LEFT:
7151     case NLM_RIGHT:
7152       if (filter & HORIZ_ARROW)
7153         break;
7154       return FALSE;
7155 
7156     case NLM_UP:
7157     case NLM_DOWN:
7158       if (filter & VERT_ARROW)
7159         break;
7160       return FALSE;
7161 
7162 
7163     case NLM_HOME:
7164     case NLM_END:
7165       if (filter & HORIZ_PAGE)
7166         break;
7167       return FALSE;
7168 
7169     case NLM_PREV:
7170     case NLM_NEXT:
7171       if (filter & VERT_PAGE)
7172         break;
7173       return FALSE;
7174 
7175     default:
7176       return FALSE;
7177     }
7178 
7179   Nlm_DoSendFocus(g, ch);
7180   return TRUE;
7181 }
7182 #endif
7183 
7184 
7185 #ifdef WIN_MOTIF
7186 typedef struct Nlm_IOstruct
7187 {
7188   int            fd;
7189   Nlm_IOtype     type;
7190   XtInputId      id;
7191   Nlm_IOcallback ufunc;
7192   Nlm_VoidPtr    udata;
7193 } Nlm_IOstruct;
7194 
7195 
io_callback(XtPointer client_data,int * source,XtInputId * id)7196 static void io_callback(XtPointer client_data, int *source, XtInputId *id)
7197 {
7198   Nlm_IO io = (Nlm_IO)client_data;
7199   (*io->ufunc)(io, io->fd, io->type, io->udata);
7200 }
7201 
7202 /* register IO callback "ufunc";  return IO handler.
7203  * NOTE:  "type" cannot be IO_CLEANUP!
7204  */
Nlm_RegisterIO(int fd,Nlm_IOcallback ufunc,Nlm_VoidPtr udata,Nlm_IOtype type)7205 extern Nlm_IO Nlm_RegisterIO(int fd,
7206                              Nlm_IOcallback ufunc, Nlm_VoidPtr udata,
7207                              Nlm_IOtype type)
7208 {
7209   Nlm_IO    io;
7210   XtPointer cond;
7211 
7212   if ( !ufunc )
7213     return NULL;
7214 
7215   switch ( type )
7216     {
7217     case Nlm_IO_READ:
7218       cond = (XtPointer)XtInputReadMask;
7219       break;
7220     case Nlm_IO_WRITE:
7221       cond = (XtPointer)XtInputWriteMask;
7222       break;
7223     case Nlm_IO_ERROR:
7224       cond = (XtPointer)XtInputExceptMask;
7225       break;
7226     default:
7227       ASSERT ( FALSE );
7228       return NULL;
7229     }
7230 
7231   io = (Nlm_IO)Nlm_MemNew( sizeof(Nlm_IOstruct) );
7232   io->fd    = fd;
7233   io->type  = type;
7234   io->ufunc = ufunc;
7235   io->udata = udata;
7236   io->id = XtAppAddInput(Nlm_appContext, fd, cond, io_callback, io);
7237   return io;
7238 }
7239 
Nlm_UnregisterIO(Nlm_IO io)7240 extern void Nlm_UnregisterIO(Nlm_IO io)
7241 {
7242   if ( !io )
7243     return;
7244 
7245   if ( io->ufunc )
7246     (*io->ufunc)(io, io->fd, Nlm_IO_CLEANUP, io->udata);
7247 
7248   XtRemoveInput( io->id );
7249   Nlm_MemFree( io );
7250 }
7251 #endif
7252