Merge branch 'master' of ssh://git.code.sf.net/p/cdesktopenv/code
[oweals/cde.git] / cde / programs / dtstyle / Font.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /************************************<+>*************************************
24  ****************************************************************************
25  **
26  **   File:        Font.c
27  **
28  **   Project:     DT 3.0
29  **
30  **   Description: Controls the Dtstyle Font dialog
31  **
32  **
33  **  (c) Copyright Hewlett-Packard Company, 1990.  
34  **
35  **
36  **
37  ****************************************************************************
38  ************************************<+>*************************************/
39 /* $XConsortium: Font.c /main/7 1996/10/30 11:14:15 drk $ */
40
41 /*+++++++++++++++++++++++++++++++++++++++*/
42 /* include files                         */
43 /*+++++++++++++++++++++++++++++++++++++++*/
44
45 #include <X11/Xlib.h>
46 #include <Xm/MwmUtil.h>
47
48 #include <Xm/Xm.h>
49 #include <Xm/XmP.h>
50 #include <Xm/MessageB.h>
51 #include <Xm/Form.h>
52 #include <Xm/Frame.h>
53 #include <Xm/LabelG.h>
54 #include <Xm/List.h>
55 #include <Xm/Scale.h>
56 #include <Xm/Text.h>
57 #include <Xm/TextF.h>
58 #include <Xm/VendorSEP.h>
59
60 #include <Dt/DialogBox.h>
61 #include <Dt/Icon.h>
62 #include <Dt/TitleBox.h>
63
64 #include <Dt/Message.h>
65 #include <Dt/SessionM.h>
66 #include <Dt/HourGlass.h>
67
68 #include <string.h>
69 #include "Help.h"
70 #include "Main.h"
71 #include "SaveRestore.h"
72
73 /*+++++++++++++++++++++++++++++++++++++++*/
74 /* Local #defines                        */
75 /*+++++++++++++++++++++++++++++++++++++++*/
76
77 #define FONT_MSG   ((char *)GETMESSAGE(5, 23, "Style Manager - Font"))
78 #define PREVIEW    ((char *)GETMESSAGE(5, 17, "Preview"))
79 #define IMMEDIATE  ((char *)GETMESSAGE(5, 18, "The changes to fonts will show up in some\napplications the next time they are started.\nOther applications, such as file manager and\napplication manager, will not show the font\nchanges until you Exit the desktop and then log\nback in.")) 
80 #define LATER      ((char *)GETMESSAGE(5, 19, "The selected font will be used when\n you restart this session."))
81 #define INFO_MSG   ((char *)GETMESSAGE(5, 24, "The font that is currently used for your desktop is not\navailable in the Size list. If a new font is selected and\napplied, you will not be able to return to the current font\nusing the Style Manager - Font dialog."))
82 #define SYSTEM_MSG ((char *)GETMESSAGE(5, 20, "AaBbCcDdEeFfGg0123456789"))
83 #define USER_MSG   ((char *)GETMESSAGE(5, 21, "AaBbCcDdEeFfGg0123456789"))
84 #define BLANK_MSG  "                          "
85 #define SIZE       ((char *)GETMESSAGE(5, 22, "Size"))
86
87
88 /*+++++++++++++++++++++++++++++++++++++++*/
89 /* Internal Variables                    */
90 /*+++++++++++++++++++++++++++++++++++++++*/
91
92 typedef struct {
93     Widget fontWkarea;
94     Widget fontpictLabel;
95     Widget previewTB;
96     Widget previewForm;
97     Widget systemLabel;
98     Widget userText;
99     Widget sizeTB;
100     Widget sizeList;
101     int    originalFontIndex;
102     int    selectedFontIndex;
103     String selectedFontStr;
104     Boolean userTextChanged;
105 } FontData;
106 static FontData font;
107
108 static saveRestore save = {FALSE, 0, };
109
110 /*+++++++++++++++++++++++++++++++++++++++*/
111 /* Internal Functions                    */
112 /*+++++++++++++++++++++++++++++++++++++++*/
113
114
115 static void CreateFontDlg( 
116                         Widget parent) ;
117 static void _DtmapCB_fontBB( 
118                         Widget w,
119                         XtPointer client_data,
120                         XtPointer call_data) ;
121 static void ButtonCB( 
122                         Widget w,
123                         XtPointer client_data,
124                         XtPointer call_data) ;
125 static void changeSampleFontCB( 
126                         Widget w,
127                         XtPointer client_data,
128                         XtPointer call_data) ;
129 static void valueChangedCB( 
130                         Widget w,
131                         XtPointer client_data,
132                         XtPointer call_data) ;
133
134
135 /*+++++++++++++++++++++++++++++++++++++++*/
136 /* popup_fontBB                          */
137 /*+++++++++++++++++++++++++++++++++++++++*/
138 void 
139 popup_fontBB(
140         Widget shell )
141 {
142   if (style.fontDialog == NULL) {
143     _DtTurnOnHourGlass(shell);  
144     CreateFontDlg(shell); 
145     XtManageChild(style.fontDialog);
146     _DtTurnOffHourGlass(shell);  
147   } else { 
148     XtManageChild(style.fontDialog);
149     raiseWindow(XtWindow(XtParent(style.fontDialog)));
150   }
151
152   /* If no font is found to match current Desktop
153      font, pop up informative message */
154   if (font.selectedFontIndex < 0)
155      InfoDialog(INFO_MSG, style.shell, False);
156 }
157
158 /*+++++++++++++++++++++++++++++++++++++++*/
159 /* CreateFontDlg                         */
160 /*+++++++++++++++++++++++++++++++++++++++*/
161 static void 
162 CreateFontDlg(
163         Widget parent )
164 {
165
166     register int     n;
167     int              i;
168     Arg              args[MAX_ARGS];
169     Widget           appTBox;
170     Widget           sizeMenuPlDn;
171     XmString         button_string[NUM_LABELS];
172     XmString         string;
173     int              count = 0;
174     Widget           widget_list[6];
175     XmString         *sizeItems;
176     XmStringTable    selectedSize;
177     char             sizeStr[111];
178     Dimension        fontheight;
179
180
181     font.selectedFontStr = style.xrdb.systemFontStr;
182
183     /* Assume nothing is selected */
184     font.selectedFontIndex = -1;
185
186     /* 
187      * The following flag is used to determine if the user has 
188      * entered anything into the sample user font field.  If 
189      * he does, than when the font selection is changed, the 
190      * default message "aAbBcC..." won't be displayed overwriting
191      * the user's text, only the fontlist will be changed. 
192      * This flag will be set in the valueChanged callback for the
193      * font.sizeList widget.
194      */
195     font.userTextChanged = FALSE;
196
197     /* 
198      * Look for the selectedFont in the fontChoice array and set 
199      * selectedFontIndex to that entry
200      */
201     for (i=0; i<style.xrdb.numFonts; i++)
202         if (strcmp (font.selectedFontStr, 
203                     style.xrdb.fontChoice[i].sysStr) == 0)
204         {
205             font.selectedFontIndex = i;        
206             if (!style.xrdb.fontChoice[i].userFont)
207                 GetUserFontResource(i);
208             if (!style.xrdb.fontChoice[i].sysFont)
209                 GetSysFontResource(i);
210             break;    
211         }            
212
213     /* 
214      * Save the index of the originally selected font.  If no
215      * font is selected, this value will remain -1.
216      */
217     font.originalFontIndex = font.selectedFontIndex;
218
219     /* Set up button labels. */
220     button_string[0] = CMPSTR(_DtOkString);
221     button_string[1] = CMPSTR(_DtCancelString);
222     button_string[2] = CMPSTR(_DtHelpString);
223
224     /* Create toplevel DialogBox */
225     /* saveRestore
226      * Note that save.poscnt has been initialized elsewhere.  
227      * save.posArgs may contain information from restoreFont().
228      */
229
230     XtSetArg(save.posArgs[save.poscnt], XmNallowOverlap, False); save.poscnt++;
231     XtSetArg(save.posArgs[save.poscnt], XmNdefaultPosition, False); 
232     save.poscnt++;
233     XtSetArg(save.posArgs[save.poscnt], XmNbuttonCount, NUM_LABELS);  
234     save.poscnt++;
235     XtSetArg(save.posArgs[save.poscnt], XmNbuttonLabelStrings, button_string); 
236     save.poscnt++;
237     style.fontDialog = 
238         __DtCreateDialogBoxDialog(parent, "Fonts", save.posArgs, save.poscnt);
239     XtAddCallback(style.fontDialog, XmNcallback, ButtonCB, NULL);
240     XtAddCallback(style.fontDialog, XmNmapCallback, _DtmapCB_fontBB, 
241                             (XtPointer)parent);
242     XtAddCallback(style.fontDialog, XmNhelpCallback,
243             (XtCallbackProc)HelpRequestCB, (XtPointer)HELP_FONT_DIALOG);
244
245     XmStringFree(button_string[0]);
246     XmStringFree(button_string[1]);
247     XmStringFree(button_string[2]);
248
249     widget_list[0] = _DtDialogBoxGetButton(style.fontDialog,2);
250     n=0;
251     XtSetArg(args[n], XmNautoUnmanage, False); n++;
252     XtSetArg(args[n], XmNcancelButton, widget_list[0]); n++;
253     XtSetValues (style.fontDialog, args, n);
254
255     n=0;
256     XtSetArg(args[n], XmNtitle, FONT_MSG); n++;
257     XtSetArg (args[n], XmNuseAsyncGeometry, True); n++;
258     XtSetArg(args[n], XmNmwmFunctions, DIALOG_MWM_FUNC); n++;
259     XtSetValues (XtParent(style.fontDialog), args, n);
260
261     n = 0;
262     XtSetArg (args[n], XmNchildType, XmWORK_AREA);  n++;
263     XtSetArg(args[n], XmNhorizontalSpacing, style.horizontalSpacing); n++;
264     XtSetArg(args[n], XmNverticalSpacing, style.verticalSpacing); n++;
265     XtSetArg(args[n], XmNallowOverlap, False); n++;
266     font.fontWkarea = XmCreateForm(style.fontDialog, "fontWorkArea", args, n);
267
268     n = 0;
269     XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM);  n++;
270     XtSetArg(args[n], XmNtopOffset, style.verticalSpacing);  n++;
271     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM);  n++;
272
273     XtSetArg(args[n], XmNfillMode, XmFILL_SELF); n++;
274     XtSetArg(args[n], XmNbehavior, XmICON_LABEL); n++;
275     XtSetArg(args[n], XmNpixmapForeground, style.secBSCol); n++;
276     XtSetArg(args[n], XmNpixmapBackground, style.secTSCol); n++;
277     XtSetArg(args[n], XmNstring, NULL); n++;  
278     XtSetArg(args[n], XmNshadowThickness, 0); n++;  
279     XtSetArg(args[n], XmNimageName, FONT_ICON); n++;  
280     XtSetArg(args[n], XmNtraversalOn, False); n++;  
281     widget_list[count++] = font.fontpictLabel = 
282         _DtCreateIcon(font.fontWkarea, "fontpictLabel", args, n);
283
284     /* Create a TitleBox and Scale/List to choose the font size */
285
286     n = 0;
287     string = CMPSTR(SIZE);
288     XtSetArg(args[n], XmNtitleString, string);  n++;
289     XtSetArg(args[n], XmNtopAttachment,      XmATTACH_WIDGET);     n++;
290     XtSetArg(args[n], XmNtopWidget,          font.fontpictLabel);  n++;
291     XtSetArg(args[n], XmNtopOffset,          style.verticalSpacing+5);  n++;
292     XtSetArg(args[n], XmNleftAttachment,     XmATTACH_FORM);       n++;
293     XtSetArg(args[n], XmNleftOffset,         style.horizontalSpacing);  n++;
294     XtSetArg(args[n], XmNbottomAttachment,   XmATTACH_FORM);       n++;
295     widget_list[count++] = font.sizeTB =
296         _DtCreateTitleBox(font.fontWkarea, "sizeTB", args, n); 
297     XmStringFree(string);
298
299     /* calculate size for each of the fonts based on system font size */
300
301     sizeItems = (XmString *) XtMalloc(sizeof(XmString) * style.xrdb.numFonts);
302     for (n=0; n<style.xrdb.numFonts; n++)
303       {
304         sprintf(sizeStr, "%d", (int)(n+1));
305         sizeItems[n] = CMPSTR(sizeStr);
306         style.xrdb.fontChoice[n].pointSize = CMPSTR(sizeStr); 
307       }
308   
309
310     n=0;
311     XtSetArg (args[n], XmNselectionPolicy, XmBROWSE_SELECT); n++;
312     XtSetArg (args[n], XmNautomaticSelection, True); n++;
313     XtSetArg (args[n], XmNvisibleItemCount, 7); n++;
314     XtSetArg (args[n], XmNitemCount, style.xrdb.numFonts); n++;
315     XtSetArg (args[n], XmNitems, sizeItems); n++;
316
317     /* 
318      * If matching font was found for current selection,
319      * display it as selected.  Otherwise, don't select
320      * anything.
321      */
322     if (font.selectedFontIndex >=0) {
323       selectedSize = &(style.xrdb.fontChoice[font.selectedFontIndex].pointSize);
324       XtSetArg (args[n], XmNselectedItems, selectedSize); n++; 
325       XtSetArg (args[n], XmNselectedItemCount, 1); n++;
326     }
327     font.sizeList = XmCreateScrolledList(font.sizeTB,"sizeList",args,n);
328     XtAddCallback(font.sizeList, XmNbrowseSelectionCallback,
329                                          changeSampleFontCB, NULL);
330
331     /* If a font match was found and selected, then set it's point size. */
332     if (font.selectedFontIndex >=0)
333       XmListSetItem(font.sizeList,
334         style.xrdb.fontChoice[font.selectedFontIndex].pointSize);
335     XtFree((char *)sizeItems);
336
337     /* preview TitleBox */
338     n = 0;
339     XtSetArg(args[n], XmNtopAttachment,      XmATTACH_WIDGET);     n++;
340     XtSetArg(args[n], XmNtopWidget,          font.fontpictLabel);  n++;
341     XtSetArg(args[n], XmNtopOffset,          style.verticalSpacing+5);  n++;
342     XtSetArg(args[n], XmNleftAttachment,     XmATTACH_WIDGET);     n++; 
343     XtSetArg(args[n], XmNleftWidget,         font.sizeTB);         n++;
344     XtSetArg(args[n], XmNrightAttachment,    XmATTACH_FORM);       n++;
345     XtSetArg(args[n], XmNrightOffset,        style.horizontalSpacing);  n++;
346     XtSetArg(args[n], XmNbottomAttachment,   XmATTACH_FORM);       n++;
347     XtSetArg(args[n], XmNbottomOffset,       style.verticalSpacing);    n++;
348     string = CMPSTR(PREVIEW); 
349     XtSetArg(args[n], XmNtitleString, string); n++;
350     widget_list[count++] = font.previewTB =
351         _DtCreateTitleBox(font.fontWkarea, "systemSample", args, n);
352     XmStringFree(string);
353   
354     /*form to contain preview font area*/
355     n = 0;
356     font.previewForm = 
357         XmCreateForm(font.previewTB, "previewForm", args, n);
358
359     /* sample system font */
360     n = 0;
361
362     /* 
363      * If a font match was found and selected, then set the fontlist
364      * and the sample string.  Otherwise, output a blank message.
365      */
366     if (font.selectedFontIndex >=0) {
367       XtSetArg (args[n], XmNfontList, 
368         style.xrdb.fontChoice[font.selectedFontIndex].sysFont); n++; 
369       string = CMPSTR(SYSTEM_MSG); 
370     } else {
371       string = CMPSTR(BLANK_MSG);
372     }
373     XtSetArg (args[n], XmNlabelString, string);  n++;
374     XtSetArg (args[n], XmNalignment, XmALIGNMENT_BEGINNING);  n++;
375     XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM);  n++;
376     XtSetArg (args[n], XmNtopOffset, 2 * style.verticalSpacing);  n++;    
377     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM);  n++;
378     XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM);  n++;
379     XtSetArg (args[n], XmNbottomAttachment, XmATTACH_NONE);  n++;
380     font.systemLabel = 
381         XmCreateLabelGadget(font.previewForm, "systemSample", args, n);
382     XmStringFree(string);
383
384     /* sample user font */
385     n = 0;
386     /* 
387      * If a font match was found and selected, then set the fontlist
388      * and the sample string.   Otherwise output a blank message.
389      */
390     if (font.selectedFontIndex >=0) {
391       XtSetArg (args[n], XmNfontList, 
392               style.xrdb.fontChoice[font.selectedFontIndex].userFont); n++;
393       XtSetArg (args[n], XmNvalue, USER_MSG);  n++;
394     } else {
395       XtSetArg (args[n], XmNvalue, NULL);  n++;
396     }
397
398     XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET);  n++;
399     XtSetArg (args[n], XmNtopWidget, font.systemLabel);  n++;
400     XtSetArg (args[n], XmNtopOffset, 2 * style.verticalSpacing);  n++;    
401     XtSetArg (args[n], XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET);  n++;
402     XtSetArg (args[n], XmNleftWidget, font.systemLabel);  n++;
403     XtSetArg (args[n], XmNbottomAttachment, XmATTACH_NONE);  n++;
404     XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET);  n++;
405     XtSetArg (args[n], XmNrightWidget, font.systemLabel);  n++;
406     font.userText = 
407         XmCreateText(font.previewForm, "userText", args, n);
408     /* Add callback to determine if user changes text in sample field */
409     XtAddCallback(font.userText, XmNvalueChangedCallback, valueChangedCB, NULL);
410
411     XtManageChild(font.systemLabel);
412     XtManageChild(font.userText);
413     XtManageChild(font.previewForm);
414     XtManageChild(font.sizeList);
415     XtManageChildren(widget_list,count);
416     XtManageChild(font.fontWkarea);
417
418 }
419
420
421 /*+++++++++++++++++++++++++++++++++++++++*/
422 /* _DtmapCB_fontBB                          */
423 /*+++++++++++++++++++++++++++++++++++++++*/
424 static void 
425 _DtmapCB_fontBB(
426         Widget w,
427         XtPointer client_data,
428         XtPointer call_data )
429 {
430    
431     DtWsmRemoveWorkspaceFunctions(style.display, XtWindow(XtParent(w)));
432
433     if (!save.restoreFlag)
434         putDialog ((Widget)client_data, w);
435       
436     XmTextShowPosition(font.userText, 0);    
437
438     XtRemoveCallback(style.fontDialog, XmNmapCallback, _DtmapCB_fontBB, NULL);
439
440 }
441
442     
443 /*+++++++++++++++++++++++++++++++++++++++++++++++++*/
444 /* ButtonCB                                        */
445 /* Process callback from PushButtons in DialogBox. */
446 /*+++++++++++++++++++++++++++++++++++++++++++++++++*/
447 static void 
448 ButtonCB(
449         Widget w,
450         XtPointer client_data,
451         XtPointer call_data )
452 {
453   DtDialogBoxCallbackStruct *cb     
454            = (DtDialogBoxCallbackStruct *) call_data;
455   int      n, len, items;
456   char     *str, *fntstr, *fntsetstr;
457   Arg      args[MAX_ARGS];
458   char     fontres[8192];
459
460   switch (cb->button_position)
461     {
462         /* Set the xrdb or pass to dtsession and close the window */
463       case OK_BUTTON:
464          /* 
465           * Need to test for the case where the Font dialog maps without 
466           * any fonts selected, and the user presses OK.  Do this by 
467           * checking for number of items selected before changing 
468           * anything.
469           */
470          XtVaGetValues (font.sizeList, XmNselectedItemCount, &items, NULL);
471
472          /*   Post an info dialog explaining when the new fonts will appear */
473          if ((font.selectedFontIndex != font.originalFontIndex) && (items > 0))
474          {
475             XtUnmanageChild(style.fontDialog);  
476
477             if(style.xrdb.writeXrdbImmediate) 
478             { 
479               InfoDialog(IMMEDIATE, style.shell, False); 
480             }
481             else 
482             {
483               InfoDialog(LATER, style.shell, False); 
484             }
485
486             /* 
487                for *FontSet resource: find first font entry delimited by a ":" 
488                or an "=". 
489             */ 
490             len =strcspn(style.xrdb.fontChoice[font.selectedFontIndex].userStr,
491                          ":=");
492             fntsetstr = (char *) XtCalloc(1, len + 1);
493             memcpy(fntsetstr, 
494                    style.xrdb.fontChoice[font.selectedFontIndex].userStr,
495                    len);
496
497             /* 
498                Since the *Font and *FontSet resources may be used by old
499                X applications, a fontlist of multiple fonts must be converted 
500                to Xt font set format (';'s converted to ','s since many old X 
501                apps don't understand ';' syntax.)
502             */
503             str = strstr(fntsetstr,";");
504             while (str) {
505                 *str = ',';
506                 str = strstr(str,";");
507             }
508
509             /* 
510                for *Font resource: find first font entry delimited by a comma, 
511                a colon or an = 
512             */
513             len = strcspn(fntsetstr,",:=");
514             fntstr = (char *) XtCalloc(1, len + 1);
515             memcpy(fntstr, 
516                    style.xrdb.fontChoice[font.selectedFontIndex].userStr,
517                    len);
518
519             /*
520               for *FontSet resource: if we got a font (instead of a font set)
521               from the first entry, then wildcard its charset fields
522              */
523             len = strlen(fntsetstr);
524             if (style.xrdb.fontChoice[font.selectedFontIndex].userStr[len] 
525                 != ':') {
526                 str = strchr(fntsetstr, '-');
527                 for (n = 1; n < 13 && str; n++)
528                     str = strchr(str + 1, '-');
529                 if (str)
530                     strcpy(str + 1, "*-*");
531             }
532
533            /* create the font resource specs with the selected font for xrdb */
534             sprintf(fontres,
535                  "*systemFont: %s\n*userFont: %s\n*FontList: %s\n*buttonFontList: %s\n*labelFontList: %s\n*textFontList: %s\n*XmText*FontList: %s\n*XmTextField*FontList: %s\n*DtEditor*textFontList: %s\n*Font: %s\n*FontSet: %s\n",
536                  style.xrdb.fontChoice[font.selectedFontIndex].sysStr,
537                  style.xrdb.fontChoice[font.selectedFontIndex].userStr,
538                  style.xrdb.fontChoice[font.selectedFontIndex].sysStr,
539                  style.xrdb.fontChoice[font.selectedFontIndex].sysStr,
540                  style.xrdb.fontChoice[font.selectedFontIndex].sysStr,
541                  style.xrdb.fontChoice[font.selectedFontIndex].userStr,
542                  style.xrdb.fontChoice[font.selectedFontIndex].userStr,
543                  style.xrdb.fontChoice[font.selectedFontIndex].userStr,
544                  style.xrdb.fontChoice[font.selectedFontIndex].userStr,
545                  fntstr, fntsetstr);
546
547             XtFree(fntstr);
548             XtFree(fntsetstr);
549
550             /* if writeXrdbImmediate true write to Xrdb else send to session mgr */
551             if(style.xrdb.writeXrdbImmediate)
552                 _DtAddToResource(style.display,fontres);
553
554             SmNewFontSettings(fontres);
555
556             font.originalFontIndex = font.selectedFontIndex;
557             style.xrdb.systemFontStr = font.selectedFontStr;
558          }
559
560          else 
561                  XtUnmanageChild(style.fontDialog);  
562                  
563          break;
564
565     case CANCEL_BUTTON:
566
567       /* reset preview area fonts to original and close the window*/
568
569       XtUnmanageChild(style.fontDialog);
570
571       if (font.originalFontIndex >= 0)
572         XmListSelectPos(font.sizeList, font.originalFontIndex+1, True);
573       else { 
574         /* 
575          * if no font was originally selected, need to undo any results
576          * from selections that were made by user before pressing Cancel.
577          */
578         XtVaSetValues (font.sizeList, XmNselectedItemCount, 0, NULL);
579         XtVaSetValues (font.userText, 
580                        XmNvalue, BLANK_MSG, 
581                        XmNfontList, style.xrdb.userFont,
582                        NULL);
583         XtVaSetValues (font.systemLabel, 
584                        XmNlabelString, CMPSTR(BLANK_MSG), 
585                        XmNfontList, style.xrdb.systemFont,
586                        NULL);
587         font.userTextChanged = FALSE;
588         font.selectedFontIndex = -1;
589       }
590       break;
591
592     case HELP_BUTTON:
593       XtCallCallbacks(style.fontDialog, XmNhelpCallback, (XtPointer)NULL);
594       break;
595
596     default:
597       break;
598     }
599 }
600
601
602 /*+++++++++++++++++++++++++++++++++++++++*/
603 /* changSampleFontCB                     */
604 /*  Change the font in the sample areas  */
605 /*+++++++++++++++++++++++++++++++++++++++*/
606 static void 
607 changeSampleFontCB(
608         Widget w,
609         XtPointer client_data,
610         XtPointer call_data )
611 {
612     int       n;
613     int       pos;
614     int       hourGlassOn;
615     Arg       args[MAX_ARGS];
616     XmListCallbackStruct *cb = (XmListCallbackStruct *) call_data;
617
618     pos = cb->item_position-1;
619
620     font.selectedFontIndex = pos;
621     font.selectedFontStr = style.xrdb.fontChoice[pos].sysStr;
622
623     hourGlassOn = !style.xrdb.fontChoice[pos].userFont ||
624                 !style.xrdb.fontChoice[pos].sysFont;
625
626     if (hourGlassOn)
627       _DtTurnOnHourGlass(style.fontDialog);
628
629     if (!style.xrdb.fontChoice[pos].userFont)
630       GetUserFontResource(pos);
631     if (!style.xrdb.fontChoice[pos].sysFont)
632       GetSysFontResource(pos);
633
634     if (hourGlassOn)
635       _DtTurnOffHourGlass(style.fontDialog);
636
637     /* Set the sample System Font string to different Font */
638     n = 0;
639     XtSetArg(args[n], XmNfontList, style.xrdb.fontChoice[pos].sysFont); n++;
640     /* string_val = CMPSTR(SYSTEM_MSG);*/
641     XtSetArg (args[n], XmNlabelString, CMPSTR(SYSTEM_MSG));  n++;
642     XtSetValues (font.systemLabel, args, n); 
643
644     /* 
645      * If the user didn't change the text field, output standard user 
646      * text message.
647      */
648     n = 0;
649     if (!font.userTextChanged) 
650       XtSetArg (args[n], XmNvalue, USER_MSG);  n++;
651     XtSetArg(args[n], XmNfontList, style.xrdb.fontChoice[pos].userFont); n++;
652     XtSetValues (font.userText, args, n);
653     XmTextShowPosition(font.userText, 0);
654 }
655
656 /*+++++++++++++++++++++++++++++++++++++++*/
657 /* valueChangedCB                        */
658 /*  Set flag indicating that the user    */
659 /*  text field has been modified.        */
660 /*+++++++++++++++++++++++++++++++++++++++*/
661 static void 
662 valueChangedCB(
663         Widget w,
664         XtPointer client_data,
665         XtPointer call_data )
666
667
668   font.userTextChanged = TRUE; 
669 }
670
671 /************************************************************************
672  * restoreFonts()
673  *
674  * restore any state information saved with saveFonts.
675  * This is called from restoreSession with the application
676  * shell and the special xrm database retrieved for restore.
677  ************************************************************************/
678 void 
679 restoreFonts(
680         Widget shell,
681         XrmDatabase db )
682 {
683     XrmName xrm_name[5];
684     XrmRepresentation rep_type;
685     XrmValue value;
686
687     xrm_name [0] = XrmStringToQuark ("Fonts");
688     xrm_name [2] = 0;
689
690     /* get x position */
691     xrm_name [1] = XrmStringToQuark ("x");
692     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value)){
693       XtSetArg (save.posArgs[save.poscnt], XmNx, atoi((char *)value.addr)); 
694       save.poscnt++;
695       save.restoreFlag = True;
696     }
697
698     /* get y position */
699     xrm_name [1] = XrmStringToQuark ("y");
700     if (XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value)){
701       XtSetArg (save.posArgs[save.poscnt], XmNy, atoi((char *)value.addr)); 
702       save.poscnt++;
703     }
704
705     xrm_name [1] = XrmStringToQuark ("ismapped");
706     XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
707     /* Are we supposed to be mapped? */
708     if (strcmp(value.addr, "True") == 0) 
709       popup_fontBB(shell);
710 }
711
712 /************************************************************************
713  * saveFonts()
714  *
715  * This routine will write out to the passed file descriptor any state
716  * information this dialog needs.  It is called from saveSessionCB with the
717  * file already opened.
718  * All information is saved in xrm format.  There is no restriction
719  * on what can be saved.  It doesn't have to be defined or be part of any
720  * widget or Xt definition.  Just name and save it here and recover it in
721  * restoreFonts.  The suggested minimum is whether you are mapped, and your
722  * location.
723  ************************************************************************/
724 void 
725 saveFonts(
726         int fd )
727 {
728     Position x,y;
729     char *bufr = style.tmpBigStr;     /* size=[1024], make bigger if needed */
730     XmVendorShellExtObject  vendorExt;
731     XmWidgetExtData         extData;
732
733     if (style.fontDialog != NULL) {
734         if (XtIsManaged(style.fontDialog))
735           sprintf(bufr, "*Fonts.ismapped: True\n");
736         else
737           sprintf(bufr, "*Fonts.ismapped: False\n");
738
739         /* Get and write out the geometry info for our Window */
740
741         x = XtX(XtParent(style.fontDialog));
742         y = XtY(XtParent(style.fontDialog));
743
744         /* Modify x & y to take into account window mgr frames
745          * This is pretty bogus, but I don't know a better way to do it.
746          */
747         extData = _XmGetWidgetExtData(style.shell, XmSHELL_EXTENSION);
748         vendorExt = (XmVendorShellExtObject)extData->widget;
749         x -= vendorExt->vendor.xOffset;
750         y -= vendorExt->vendor.yOffset;
751
752         sprintf(bufr, "%s*Fonts.x: %d\n", bufr, x);
753         sprintf(bufr, "%s*Fonts.y: %d\n", bufr, y);
754
755         write (fd, bufr, strlen(bufr));
756     }
757 }
758
759
760