Don't quit if getpwuid() fails. It can fail if some nameservices are not
[nedit.git] / util / fontsel.c
blob1d92d3e631602ba47f1473401d37eeb7f3904261
1 static const char CVSID[] = "$Id: fontsel.c,v 1.24 2003/04/07 22:51:41 yooden Exp $";
2 /*******************************************************************************
3 * *
4 * fontsel.c -- Nirvana Font Selector *
5 * *
6 * Copyright (C) 1999 Mark Edel *
7 * *
8 * This is free software; you can redistribute it and/or modify it under the *
9 * terms of the GNU General Public License as published by the Free Software *
10 * Foundation; either version 2 of the License, or (at your option) any later *
11 * version. *
12 * *
13 * This software is distributed in the hope that it will be useful, but WITHOUT *
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
16 * for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License along with *
19 * software; if not, write to the Free Software Foundation, Inc., 59 Temple *
20 * Place, Suite 330, Boston, MA 02111-1307 USA *
21 * *
22 * Nirvana Text Editor *
23 * June 2, 1993 *
24 * *
25 * Written by Suresh Ravoor (assisted by Mark Edel) *
26 * *
27 *******************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 #include "../config.h"
31 #endif
33 #include "fontsel.h"
34 #include "misc.h"
35 #include "DialogF.h"
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <math.h>
42 #include <X11/Intrinsic.h>
43 #include <Xm/Xm.h>
44 #include <Xm/Form.h>
45 #include <Xm/PushB.h>
46 #include <Xm/List.h>
47 #include <Xm/Label.h>
48 #include <Xm/Text.h>
49 #include <Xm/TextF.h>
50 #include <Xm/ToggleB.h>
51 #include <Xm/MessageB.h>
52 #include <Xm/DialogS.h>
54 #ifdef HAVE_DEBUG_H
55 #include "../debug.h"
56 #endif
58 #define MAX_ARGS 20
59 #define MAX_NUM_FONTS 32767
60 #define MAX_FONT_NAME_LEN 256
61 #define MAX_ENTRIES_IN_LIST 5000
62 #define MAX_DISPLAY_SIZE 150
63 #define DELIM '-'
64 #define NUM_COMPONENTS_FONT_NAME 14
65 #define TEMP_BUF_SIZE 256
66 #define DISPLAY_HEIGHT 90
68 enum listSpecifier { NONE, FONT, STYLE, SIZE };
71 /* local data structures and types */
73 typedef struct
75 Widget form; /* widget id */
76 Widget okButton; /* widget id */
77 Widget cancelButton; /* widget id */
78 Widget fontList; /* widget id */
79 Widget styleList; /* widget id */
80 Widget sizeList; /* widget id */
81 Widget fontNameField; /* widget id */
82 Widget sizeToggle; /* widget id */
83 Widget propFontToggle; /* widget id */
84 Widget dispField; /* widget id */
85 char **fontData; /* font name info */
86 int numFonts; /* number of fonts */
87 char *sel1; /* selection from list 1 */
88 char *sel2; /* selection from list 2 */
89 char *sel3; /* selection from list 3 */
90 int showPropFonts; /* toggle state - show prop fonts */
91 int showSizeInPixels;/* toggle state - size in pixels */
92 char *fontName; /* current font name */
93 XFontStruct *oldFont; /* font data structure for dispSample */
94 XmFontList oldFontList; /* font data structure for dispSample */
95 int exitFlag; /* used for program exit control */
96 int destroyedFlag; /* used to prevent double destruction */
97 Pixel sampleFG; /* Colors for the sample field */
98 Pixel sampleBG;
99 } xfselControlBlkType;
102 /* local function prototypes */
104 static void getStringComponent(const char *inStr, int pos, char *outStr);
105 static void setupScrollLists(int dontChange, xfselControlBlkType ctrlBlk);
106 static int notPropFont(const char *font);
107 static int styleMatch(xfselControlBlkType *ctrlBlk, const char *font);
108 static int sizeMatch(xfselControlBlkType *ctrlBlk, const char *font);
109 static int fontMatch(xfselControlBlkType *ctrlBlk, const char *font);
110 static void addItemToList(char **buf, const char *item, int *count);
111 static void getFontPart(const char *font, char *buff1);
112 static void getStylePart(const char *font, char *buff1);
113 static void getSizePart(const char *font, char *buff1, int inPixels);
114 static void propFontToggleAction(Widget widget,
115 xfselControlBlkType *ctrlBlk,
116 XmToggleButtonCallbackStruct *call_data);
117 static void sizeToggleAction(Widget widget,
118 xfselControlBlkType *ctrlBlk,
119 XmToggleButtonCallbackStruct *call_data);
120 static void fontAction(Widget widget, xfselControlBlkType *ctrlBlk,
121 XmListCallbackStruct *call_data);
122 static void styleAction(Widget widget, xfselControlBlkType *ctrlBlk,
123 XmListCallbackStruct *call_data);
124 static void sizeAction(Widget widget, xfselControlBlkType *ctrlBlk,
125 XmListCallbackStruct *call_data);
126 static void choiceMade(xfselControlBlkType *ctrlBlk);
127 static void dispSample(xfselControlBlkType *ctrlBlk);
128 static void destroyCB(Widget widget, xfselControlBlkType *ctrlBlk,
129 XmListCallbackStruct *call_data);
130 static void cancelAction(Widget widget, xfselControlBlkType *ctrlBlk,
131 XmListCallbackStruct *call_data);
132 static void okAction(Widget widget, xfselControlBlkType *ctrlBlk,
133 XmPushButtonCallbackStruct *call_data);
134 static void startupFont(xfselControlBlkType *ctrlBlk, const char *font);
135 static void setFocus(Widget w, xfselControlBlkType *ctrlBlk, XEvent *event,
136 Boolean *continueToDispatch);
137 static void FindBigFont(xfselControlBlkType *ctrlBlk, char *bigFont);
138 static void enableSample(xfselControlBlkType *ctrlBlk, Bool turn_on,
139 XmFontList *fontList);
141 /*******************************************************************************
143 * FontSel () *
146 * Function to put up a modal font selection dialog box. The purpose *
147 * of this routine is to allow the user to interactively view sample *
148 * fonts and to choose a font for current use. *
150 * Arguments: *
152 * Widget parent - parent widget ID *
154 * int showPropFont - ONLY_FIXED : shows only fixed fonts *
155 * doesn't show prop font *
156 * toggle button also. *
157 * PREF_FIXED : can select either fixed *
158 * or proportional fonts; *
159 * but starting option is *
160 * Fixed fonts. *
161 * PREF_PROP : can select either fixed *
162 * or proportional fonts; *
163 * but starting option is *
164 * proportional fonts. *
166 * char * currFont - ASCII string that contains the name *
167 * of the currently selected font. *
169 * Pixel sampleFG, sampleBG - Foreground/Background colors in *
170 * which to display the sample *
171 * text. *
174 * Returns: *
176 * pointer to an ASCII character string that contains the name of *
177 * the selected font (in X format for naming fonts); it is the users *
178 * responsibility to free the space allocated to this string. *
180 * Comments: *
182 * The calling function has to call the appropriate routines to set *
183 * the current font to the one represented by the returned string. *
185 *******************************************************************************/
187 char *FontSel(Widget parent, int showPropFonts, const char *currFont,
188 Pixel sampleFG, Pixel sampleBG)
190 Widget dialog, form, okButton, cancelButton;
191 Widget styleList, sizeList, fontName, fontList;
192 Widget sizeToggle, propFontToggle = NULL, dispField;
193 Widget nameLabel, sampleLabel;
194 Arg args[MAX_ARGS];
195 int n;
196 XmString tempStr;
197 char bigFont[MAX_FONT_NAME_LEN];
198 xfselControlBlkType ctrlBlk;
199 Display *theDisplay;
201 ctrlBlk.fontData = XListFonts(XtDisplay(parent),
202 "-*-*-*-*-*-*-*-*-*-*-*-*-*-*",
203 MAX_NUM_FONTS, &ctrlBlk.numFonts);
204 FindBigFont(&ctrlBlk, bigFont);
205 ctrlBlk.oldFont = XLoadQueryFont(XtDisplay(parent), bigFont);
206 ctrlBlk.oldFontList = XmFontListCreate(ctrlBlk.oldFont,
207 XmSTRING_DEFAULT_CHARSET);
208 ctrlBlk.sampleFG = sampleFG;
209 ctrlBlk.sampleBG = sampleBG;
211 dialog = CreateDialogShell(parent, "Font Selector", args, 0);
213 /* Set up window sizes for form widget */
215 n = 0;
216 XtSetArg(args[n], XmNautoUnmanage, FALSE); n++;
217 XtSetArg(args[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++;
219 /* Create form popup dialog widget */
221 form = XtCreateWidget ("Font Selector", xmFormWidgetClass, dialog,
222 args, n);
224 /* Create pushbutton widgets */
226 n = 0;
227 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
228 XtSetArg(args[n], XmNrightAttachment, XmATTACH_POSITION); n++;
229 XtSetArg(args[n], XmNbottomOffset, 4); n++;
230 XtSetArg(args[n], XmNtopOffset, 1); n++;
231 XtSetArg(args[n], XmNrightPosition, 45); n++;
232 XtSetArg(args[n], XmNwidth, 110); n++;
233 XtSetArg(args[n], XmNheight, 28); n++;
234 XtSetArg(args[n], XmNshowAsDefault, TRUE); n++;
235 okButton = XtCreateManagedWidget("OK", xmPushButtonWidgetClass, form,
236 args, n);
238 n = 0;
239 XtSetArg(args[n], XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
240 XtSetArg(args[n], XmNleftAttachment, XmATTACH_POSITION); n++;
241 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
242 XtSetArg(args[n], XmNtopWidget, okButton); n++;
243 XtSetArg(args[n], XmNbottomWidget, okButton); n++;
244 XtSetArg(args[n], XmNleftPosition, 55); n++;
245 XtSetArg(args[n], XmNwidth, 110); n++;
246 XtSetArg(args[n], XmNheight, 28); n++;
247 cancelButton = XtCreateManagedWidget("Cancel", xmPushButtonWidgetClass,
248 form, args, n);
250 /* create font name text widget and the corresponding label */
252 n = 0;
253 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
254 XtSetArg(args[n], XmNleftAttachment, XmATTACH_POSITION); n++;
255 XtSetArg(args[n], XmNrightAttachment, XmATTACH_POSITION); n++;
256 XtSetArg(args[n], XmNbottomWidget, okButton); n++;
257 XtSetArg(args[n], XmNleftPosition, 1); n++;
258 XtSetArg(args[n], XmNrightPosition, 99); n++;
259 XtSetArg(args[n], XmNeditable, True); n++;
260 XtSetArg(args[n], XmNeditMode, XmSINGLE_LINE_EDIT); n++;
261 XtSetArg(args[n], XmNmaxLength, MAX_FONT_NAME_LEN); n++;
262 fontName = XtCreateManagedWidget("fontname", xmTextWidgetClass, form,
263 args, n);
264 RemapDeleteKey(fontName); /* kludge to handle delete and BS */
266 n = 0;
267 tempStr = XmStringCreate("Font Name:", XmSTRING_DEFAULT_CHARSET);
268 XtSetArg(args[n], XmNlabelString, tempStr); n++;
269 XtSetArg(args[n], XmNmnemonic, 'N'); n++;
270 XtSetArg(args[n], XmNuserData, fontName); n++;
271 XtSetArg(args[n], XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
272 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
273 XtSetArg(args[n], XmNleftWidget, fontName); n++;
274 XtSetArg(args[n], XmNbottomWidget, fontName); n++;
275 XtSetArg(args[n], XmNtopOffset, 1); n++;
276 nameLabel = XtCreateManagedWidget("Font Name:", xmLabelWidgetClass,
277 form, args, n);
278 XmStringFree(tempStr);
280 /* create sample display text field widget */
282 n = 0;
283 XtSetArg(args[n], XmNleftAttachment, XmATTACH_POSITION); n++;
284 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
285 XtSetArg(args[n], XmNrightAttachment, XmATTACH_POSITION); n++;
286 XtSetArg(args[n], XmNrightPosition, 99); n++;
287 XtSetArg(args[n], XmNbottomWidget, nameLabel); n++;
288 XtSetArg(args[n], XmNleftPosition, 1); n++;
289 XtSetArg(args[n], XmNalignment, XmALIGNMENT_BEGINNING); n++;
290 XtSetArg(args[n], XmNvalue,
291 "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789");
292 n++;
293 XtSetArg(args[n], XmNforeground, sampleFG); n++;
294 XtSetArg(args[n], XmNbackground, sampleBG); n++;
295 dispField = XtCreateManagedWidget(" ", xmTextFieldWidgetClass, form,
296 args, n);
298 n = 0;
299 tempStr = XmStringCreate("Sample:", XmSTRING_DEFAULT_CHARSET);
300 XtSetArg(args[n], XmNlabelString, tempStr); n++;
301 XtSetArg(args[n], XmNmnemonic, 'S'); n++;
302 XtSetArg(args[n], XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
303 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
304 XtSetArg(args[n], XmNleftWidget, dispField); n++;
305 XtSetArg(args[n], XmNbottomWidget, dispField); n++;
306 XtSetArg(args[n], XmNtopOffset, 1); n++;
307 sampleLabel = XtCreateManagedWidget("Font Name:", xmLabelWidgetClass,
308 form, args, n);
309 XmStringFree(tempStr);
311 /* create toggle buttons */
313 n = 0;
314 tempStr = XmStringCreate("Show Size in Points",
315 XmSTRING_DEFAULT_CHARSET);
316 XtSetArg(args[n], XmNlabelString, tempStr); n++;
317 XtSetArg(args[n], XmNmnemonic, 'P'); n++;
318 XtSetArg(args[n], XmNleftAttachment, XmATTACH_POSITION); n++;
319 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
320 XtSetArg(args[n], XmNleftPosition, 2); n++;
321 XtSetArg(args[n], XmNtopOffset, 1); n++;
322 XtSetArg(args[n], XmNbottomWidget, sampleLabel); n++;
323 sizeToggle = XtCreateManagedWidget("sizetoggle",
324 xmToggleButtonWidgetClass, form, args, n);
325 XmStringFree(tempStr);
327 if (showPropFonts != ONLY_FIXED)
329 n = 0;
330 tempStr = XmStringCreate("Show Proportional Width Fonts",
331 XmSTRING_DEFAULT_CHARSET);
332 XtSetArg(args[n], XmNlabelString, tempStr); n++;
333 XtSetArg(args[n], XmNmnemonic, 'W'); n++;
334 XtSetArg(args[n], XmNrightAttachment, XmATTACH_POSITION); n++;
335 XtSetArg(args[n], XmNtopAttachment,
336 XmATTACH_OPPOSITE_WIDGET); n++;
337 XtSetArg(args[n], XmNbottomAttachment,
338 XmATTACH_OPPOSITE_WIDGET); n++;
339 XtSetArg(args[n], XmNrightPosition, 98); n++;
340 XtSetArg(args[n], XmNtopWidget, sizeToggle); n++;
341 XtSetArg(args[n], XmNbottomWidget, sizeToggle); n++;
342 XtSetArg(args[n], XmNtopOffset, 1); n++;
343 propFontToggle = XtCreateManagedWidget("propfonttoggle",
344 xmToggleButtonWidgetClass, form, args, n);
345 XmStringFree(tempStr);
348 /* create scroll list widgets */
349 /* "Font" list */
351 n = 0;
352 tempStr = XmStringCreate("Font:", XmSTRING_DEFAULT_CHARSET);
353 XtSetArg(args[n], XmNlabelString, tempStr); n++;
354 XtSetArg(args[n], XmNmnemonic, 'F'); n++;
355 XtSetArg(args[n], XmNtopOffset, 2); n++;
356 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
357 XtSetArg(args[n], XmNleftAttachment, XmATTACH_POSITION); n++;
358 XtSetArg(args[n], XmNleftPosition, 1); n++;
359 nameLabel = XtCreateManagedWidget("Font:", xmLabelWidgetClass, form,
360 args, n);
361 XmStringFree(tempStr);
363 n = 0;
364 XtSetArg(args[n], XmNvisibleItemCount, 15); n++;
365 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
366 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
367 XtSetArg(args[n], XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
368 XtSetArg(args[n], XmNrightAttachment, XmATTACH_POSITION); n++;
369 XtSetArg(args[n], XmNbottomWidget, sizeToggle); n++;
370 XtSetArg(args[n], XmNtopWidget, nameLabel); n++;
371 XtSetArg(args[n], XmNleftWidget, nameLabel); n++;
372 XtSetArg(args[n], XmNrightPosition, 52); n++;
373 fontList = XmCreateScrolledList(form, "fontlist", args, n);
374 XtManageChild(fontList);
375 XtVaSetValues(nameLabel, XmNuserData, fontList, NULL);
377 /* "Style" list */
379 n = 0;
380 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
381 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
382 XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
383 XtSetArg(args[n], XmNrightAttachment, XmATTACH_POSITION); n++;
384 XtSetArg(args[n], XmNtopWidget, nameLabel); n++;
385 XtSetArg(args[n], XmNleftOffset, 5); n++;
386 XtSetArg(args[n], XmNleftWidget, XtParent(fontList)); n++;
387 XtSetArg(args[n], XmNbottomWidget, XtParent(fontList)); n++;
388 XtSetArg(args[n], XmNrightPosition, 85); n++;
389 styleList = XmCreateScrolledList(form, "stylelist", args, n);
390 XtManageChild(styleList);
392 n = 0;
393 tempStr = XmStringCreate("Style:", XmSTRING_DEFAULT_CHARSET);
394 XtSetArg(args[n], XmNmnemonic, 'y'); n++;
395 XtSetArg(args[n], XmNuserData, styleList); n++;
396 XtSetArg(args[n], XmNlabelString, tempStr); n++;
397 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
398 XtSetArg(args[n], XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
399 XtSetArg(args[n], XmNbottomWidget, XtParent(styleList)); n++;
400 XtSetArg(args[n], XmNleftWidget, XtParent(styleList)); n++;
401 XtCreateManagedWidget("Style:", xmLabelWidgetClass, form, args, n);
402 XmStringFree(tempStr);
404 /* "Size" list */
406 n = 0;
407 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
408 XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
409 XtSetArg(args[n], XmNrightAttachment, XmATTACH_POSITION); n++;
410 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
411 XtSetArg(args[n], XmNtopWidget, nameLabel); n++;
412 XtSetArg(args[n], XmNleftWidget, XtParent(styleList)); n++;
413 XtSetArg(args[n], XmNbottomWidget, XtParent(fontList)); n++;
414 XtSetArg(args[n], XmNleftOffset, 5); n++;
415 XtSetArg(args[n], XmNrightPosition, 99); n++;
416 sizeList = XmCreateScrolledList(form, "sizelist", args, n);
417 XtManageChild(sizeList);
419 n = 0;
420 tempStr = XmStringCreate("Size:", XmSTRING_DEFAULT_CHARSET);
421 XtSetArg(args[n], XmNlabelString, tempStr); n++;
422 XtSetArg(args[n], XmNmnemonic, 'z'); n++;
423 XtSetArg(args[n], XmNuserData, sizeList); n++;
424 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
425 XtSetArg(args[n], XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
426 XtSetArg(args[n], XmNbottomWidget, XtParent(sizeList)); n++;
427 XtSetArg(args[n], XmNleftWidget, XtParent(sizeList)); n++;
428 XtCreateManagedWidget("Size:", xmLabelWidgetClass, form, args, n);
429 XmStringFree(tempStr);
431 /* update form widgets cancel button */
433 n = 0;
434 XtSetArg(args[n], XmNcancelButton, cancelButton); n++;
435 XtSetValues(form, args, n);
438 /* update application's control block structure */
440 ctrlBlk.form = form;
441 ctrlBlk.okButton = okButton;
442 ctrlBlk.cancelButton = cancelButton;
443 ctrlBlk.fontList = fontList;
444 ctrlBlk.styleList = styleList;
445 ctrlBlk.sizeList = sizeList;
446 ctrlBlk.fontNameField = fontName;
447 ctrlBlk.sizeToggle = sizeToggle;
448 if (showPropFonts != ONLY_FIXED)
449 ctrlBlk.propFontToggle = propFontToggle;
450 ctrlBlk.dispField = dispField;
451 ctrlBlk.exitFlag = FALSE;
452 ctrlBlk.destroyedFlag = FALSE;
453 ctrlBlk.showPropFonts = showPropFonts;
454 ctrlBlk.showSizeInPixels = TRUE;
455 ctrlBlk.sel1 = NULL;
456 ctrlBlk.sel2 = NULL;
457 ctrlBlk.sel3 = NULL;
458 ctrlBlk.fontName = NULL;
460 setupScrollLists(NONE, ctrlBlk); /* update scroll lists */
462 if (showPropFonts == PREF_PROP)
463 XmToggleButtonSetState(propFontToggle, TRUE, FALSE);
465 /* Register callback functions */
467 if (showPropFonts != ONLY_FIXED)
468 XtAddCallback(propFontToggle, XmNvalueChangedCallback,
469 (XtCallbackProc)propFontToggleAction, (char *)&ctrlBlk);
470 XtAddCallback(sizeToggle, XmNvalueChangedCallback,
471 (XtCallbackProc)sizeToggleAction, (char *)&ctrlBlk);
472 XtAddCallback(fontList, XmNbrowseSelectionCallback,
473 (XtCallbackProc)fontAction, (char *)&ctrlBlk);
474 XtAddCallback(styleList, XmNbrowseSelectionCallback,
475 (XtCallbackProc)styleAction, (char *)&ctrlBlk);
476 XtAddCallback(sizeList, XmNbrowseSelectionCallback,
477 (XtCallbackProc)sizeAction, (char *)&ctrlBlk);
478 XtAddCallback(okButton, XmNactivateCallback,
479 (XtCallbackProc)okAction, (char *)&ctrlBlk);
480 XtAddCallback(cancelButton, XmNactivateCallback,
481 (XtCallbackProc)cancelAction, (char *)&ctrlBlk);
483 /* add event handler to setup input focus at start to scroll list */
485 XtAddEventHandler(fontList, FocusChangeMask, FALSE,
486 (XtEventHandler)setFocus, (char *)&ctrlBlk);
487 XmProcessTraversal(fontList, XmTRAVERSE_CURRENT);
489 /* setup tabgroups */
491 XmAddTabGroup(fontList);
492 XmAddTabGroup(styleList);
493 XmAddTabGroup(sizeList);
494 XmAddTabGroup(sizeToggle);
495 if (showPropFonts != ONLY_FIXED)
496 XmAddTabGroup(propFontToggle);
497 XmAddTabGroup(fontName);
498 XmAddTabGroup(okButton);
499 XmAddTabGroup(cancelButton);
501 /* Make sure that we don't try to access the dialog if the user
502 destroyed it (possibly indirectly, by destroying the parent). */
503 XtAddCallback(dialog, XmNdestroyCallback,
504 (XtCallbackProc)destroyCB, (char *)&ctrlBlk);
506 /* Link Motif Close option to cancel action */
508 AddMotifCloseCallback(dialog, (XtCallbackProc)cancelAction, &ctrlBlk);
510 /* Handle dialog mnemonics */
512 AddDialogMnemonicHandler(form, FALSE);
514 /* Realize Widgets */
516 ManageDialogCenteredOnPointer(form);
518 /* set up current font parameters */
520 if (currFont[0] != '\0')
521 startupFont(&ctrlBlk, currFont);
523 /* Make sure that we can still access the display in case the form
524 gets destroyed */
525 theDisplay = XtDisplay(form);
527 /* enter event loop */
529 while (! ctrlBlk.exitFlag && ! ctrlBlk.destroyedFlag)
530 XtAppProcessEvent(XtWidgetToApplicationContext(form), XtIMAll);
532 if (! ctrlBlk.destroyedFlag) {
533 /* Don't let the callback destroy the font name */
534 XtRemoveCallback(dialog, XmNdestroyCallback,
535 (XtCallbackProc)destroyCB, (char *)&ctrlBlk);
536 XtDestroyWidget(dialog);
539 if (ctrlBlk.oldFont != NULL)
541 XFreeFont(theDisplay, ctrlBlk.oldFont);
542 XmFontListFree(ctrlBlk.oldFontList);
545 return(ctrlBlk.fontName);
549 /* gets a specific substring from a string */
551 static void getStringComponent(const char *inStr, int pos, char *outStr)
553 int i, j;
555 *outStr = '\0';
557 if (pos > NUM_COMPONENTS_FONT_NAME)
559 fprintf(stderr, "Warning: getStringComponent being used for ");
560 fprintf(stderr, "pos > %d\nIf such ", NUM_COMPONENTS_FONT_NAME);
561 fprintf(stderr, "use is intended remove these warning lines\n");
564 for (i = 0; (pos > 0) && (inStr[i] != '\0'); i++)
565 if (inStr[i] == DELIM)
566 pos--;
568 if (inStr[i] == '\0')
569 return;
571 for (j = 0; (inStr[i] != DELIM) && (inStr[i] != '\0'); i++, j++)
572 outStr[j] = inStr[i];
573 outStr[j] = '\0';
577 /* parse through the fontlist data and set up the three scroll lists */
579 static void setupScrollLists(int dontChange, xfselControlBlkType ctrlBlk)
581 char *itemBuf1[MAX_ENTRIES_IN_LIST];
582 char *itemBuf2[MAX_ENTRIES_IN_LIST];
583 char *itemBuf3[MAX_ENTRIES_IN_LIST];
584 int itemCount1, itemCount2, itemCount3;
585 char buff1[TEMP_BUF_SIZE];
586 XmString items[MAX_ENTRIES_IN_LIST];
587 int i;
589 itemCount1 = 0;
590 itemCount2 = 0;
591 itemCount3 = 0;
593 for (i = 0; i < ctrlBlk.numFonts && i < MAX_ENTRIES_IN_LIST; i++)
595 if ((dontChange != FONT) &&
596 (styleMatch(&ctrlBlk, ctrlBlk.fontData[i])) &&
597 (sizeMatch (&ctrlBlk, ctrlBlk.fontData[i])) &&
598 ((ctrlBlk.showPropFonts == PREF_PROP) ||
599 (notPropFont(ctrlBlk.fontData[i]))))
601 getFontPart(ctrlBlk.fontData[i], buff1);
602 addItemToList(itemBuf1, buff1, &itemCount1);
605 if ((dontChange != STYLE) &&
606 (fontMatch(&ctrlBlk, ctrlBlk.fontData[i])) &&
607 (sizeMatch (&ctrlBlk, ctrlBlk.fontData[i])) &&
608 ((ctrlBlk.showPropFonts == PREF_PROP) ||
609 (notPropFont(ctrlBlk.fontData[i]))))
611 getStylePart(ctrlBlk.fontData[i], buff1);
612 addItemToList(itemBuf2, buff1, &itemCount2);
615 if ((dontChange != SIZE) &&
616 (fontMatch(&ctrlBlk, ctrlBlk.fontData[i])) &&
617 (styleMatch (&ctrlBlk, ctrlBlk.fontData[i])) &&
618 ((ctrlBlk.showPropFonts == PREF_PROP) ||
619 (notPropFont(ctrlBlk.fontData[i]))))
621 getSizePart(ctrlBlk.fontData[i], buff1, ctrlBlk.showSizeInPixels);
622 addItemToList(itemBuf3, buff1, &itemCount3);
624 } /* end - for (i = 0; i < ctrlBlk.numFonts; i++) */
626 /* recreate all three scroll lists where necessary */
627 if (dontChange != FONT)
629 for (i = 0; i < itemCount1; i++)
631 items[i] = XmStringCreate(itemBuf1[i], XmSTRING_DEFAULT_CHARSET);
632 XtFree(itemBuf1[i]);
634 XmListDeleteAllItems(ctrlBlk.fontList);
635 XmListAddItems(ctrlBlk.fontList, items, itemCount1, 1);
636 if (ctrlBlk.sel1 != NULL)
638 XmStringFree(items[0]);
639 items[0] = XmStringCreate(ctrlBlk.sel1, XmSTRING_DEFAULT_CHARSET);
640 XmListSelectItem(ctrlBlk.fontList, items[0], FALSE);
641 XmListSetBottomItem(ctrlBlk.fontList, items[0]);
643 for (i = 0; i < itemCount1; i++)
644 XmStringFree(items[i]);
647 if (dontChange != STYLE)
649 for (i = 0; i < itemCount2; i++)
651 items[i] = XmStringCreate(itemBuf2[i], XmSTRING_DEFAULT_CHARSET);
652 XtFree(itemBuf2[i]);
654 XmListDeleteAllItems(ctrlBlk.styleList);
655 XmListAddItems(ctrlBlk.styleList, items, itemCount2, 1);
656 if (ctrlBlk.sel2 != NULL)
658 XmStringFree(items[0]);
659 items[0] = XmStringCreate(ctrlBlk.sel2, XmSTRING_DEFAULT_CHARSET);
660 XmListSelectItem(ctrlBlk.styleList, items[0], FALSE);
661 XmListSetBottomItem(ctrlBlk.styleList, items[0]);
663 for (i = 0; i < itemCount2; i++)
664 XmStringFree(items[i]);
667 if (dontChange != SIZE)
669 for (i = 0; i < itemCount3; i++)
671 items[i] = XmStringCreate(itemBuf3[i],
672 XmSTRING_DEFAULT_CHARSET);
673 XtFree(itemBuf3[i]);
675 XmListDeleteAllItems(ctrlBlk.sizeList);
676 XmListAddItems(ctrlBlk.sizeList, items, itemCount3, 1);
677 if (ctrlBlk.sel3 != NULL)
679 XmStringFree(items[0]);
680 items[0] = XmStringCreate(ctrlBlk.sel3, XmSTRING_DEFAULT_CHARSET);
681 XmListSelectItem(ctrlBlk.sizeList, items[0], FALSE);
682 XmListSetBottomItem(ctrlBlk.sizeList, items[0]);
684 for (i = 0; i < itemCount3; i++)
685 XmStringFree(items[i]);
690 /* returns TRUE if argument is not name of a proportional font */
692 static int notPropFont(const char *font)
694 char buff1[TEMP_BUF_SIZE];
696 getStringComponent(font, 11, buff1);
697 if ((strcmp(buff1, "p") == 0) || (strcmp(buff1, "P") == 0))
698 return(FALSE);
699 else
700 return(TRUE);
704 /* returns TRUE if the style portion of the font matches the currently
705 selected style */
707 static int styleMatch(xfselControlBlkType *ctrlBlk, const char *font)
709 char buff[TEMP_BUF_SIZE];
711 if (ctrlBlk->sel2 == NULL)
712 return(TRUE);
714 getStylePart(font, buff);
716 if (strcmp(buff, ctrlBlk->sel2) == 0)
717 return(TRUE);
718 else
719 return(FALSE);
723 /* returns TRUE if the size portion of the font matches the currently
724 selected size */
726 static int sizeMatch(xfselControlBlkType *ctrlBlk, const char *font)
728 char buff[TEMP_BUF_SIZE];
730 if (ctrlBlk->sel3 == NULL)
731 return(TRUE);
733 getSizePart(font, buff, ctrlBlk->showSizeInPixels);
734 if (strcmp(buff, ctrlBlk->sel3) == 0)
735 return(TRUE);
736 else
737 return(FALSE);
741 /* returns TRUE if the font portion of the font matches the currently
742 selected font */
744 static int fontMatch(xfselControlBlkType *ctrlBlk, const char *font)
746 char buff[TEMP_BUF_SIZE];
748 if (ctrlBlk->sel1 == NULL)
749 return(TRUE);
751 getFontPart(font, buff);
752 if (strcmp(buff, ctrlBlk->sel1) == 0)
753 return(TRUE);
754 else
755 return(FALSE);
759 /* inserts a string into correct sorted position in a list */
761 static void addItemToList(char **buf, const char *item, int *count)
763 int i, j;
765 if (*count == MAX_ENTRIES_IN_LIST)
767 fprintf(stderr, "Trying to add more than MAX_ENTRIES_IN_LIST ");
768 fprintf(stderr, "(%d) entries to array\n", MAX_ENTRIES_IN_LIST);
769 return;
772 for (i = 0; i < *count; i++)
774 if (strcmp(buf[i], item) == 0)
775 return;
776 if (strcmp(buf[i], item) > 0)
777 break;
780 for (j = *count; j > i; j--)
781 buf[j] = buf[j-1];
782 buf[i] = XtMalloc(strlen(item) + 1);
783 strcpy(buf[i], item);
784 (*count)++;
788 /* given a font name this function returns the part used in the first
789 scroll list */
791 static void getFontPart(const char *font, char *buff1)
793 char buff2[TEMP_BUF_SIZE], buff3[TEMP_BUF_SIZE];
794 char buff4[TEMP_BUF_SIZE];
796 getStringComponent(font, 2, buff1);
797 getStringComponent(font, 1, buff2);
799 sprintf(buff3, "%s (%s", buff1, buff2);
801 getStringComponent(font, 13, buff1);
802 getStringComponent(font, 14, buff4);
804 if (((strncmp(buff1, "iso8859", 7) == 0) ||
805 (strncmp(buff1, "ISO8859", 7) == 0)) && (strcmp(buff4, "1") == 0))
806 sprintf(buff1, "%s)", buff3);
807 else
809 sprintf(buff2, "%s, %s,", buff3, buff1);
810 sprintf(buff1, "%s %s)", buff2, buff4);
815 /* given a font name this function returns the part used in the second
816 scroll list */
818 static void getStylePart(const char *font, char *buff1)
820 char buff2[TEMP_BUF_SIZE], buff3[TEMP_BUF_SIZE];
822 getStringComponent(font, 3, buff3);
823 getStringComponent(font, 5, buff2);
825 if ((strcmp(buff2, "normal") != 0) && (strcmp(buff2, "Normal") != 0) &&
826 (strcmp(buff2, "NORMAL") != 0))
827 sprintf(buff1, "%s %s", buff3, buff2);
828 else
829 strcpy(buff1, buff3);
831 getStringComponent(font, 6, buff2);
833 if (buff2[0] != '\0')
834 sprintf(buff3, "%s %s", buff1, buff2);
835 else
836 strcpy(buff3, buff1);
838 getStringComponent(font, 4, buff2);
840 if ((strcmp(buff2, "o") == 0) || (strcmp(buff2, "O") == 0))
841 sprintf(buff1, "%s oblique", buff3);
842 else if ((strcmp(buff2, "i") == 0) || (strcmp(buff2, "I") == 0))
843 sprintf(buff1, "%s italic", buff3);
845 if (strcmp(buff1, " ") == 0)
846 strcpy(buff1, "-");
850 /* given a font name this function returns the part used in the third
851 scroll list */
853 static void getSizePart(const char *font, char *buff1, int inPixels)
855 int size;
857 if (inPixels)
859 getStringComponent(font, 7, buff1);
860 size = atoi(buff1);
861 sprintf(buff1, "%2d", size);
863 else
865 double temp;
867 getStringComponent(font, 8, buff1);
868 size = atoi(buff1);
869 temp = (double)size / 10.0;
870 if (buff1[strlen(buff1) - 1] == '0')
872 size = (int)floor(temp+0.5);
873 sprintf(buff1, "%2d", size);
875 else
876 sprintf(buff1, "%4.1f", temp);
881 /* Call back functions start from here - suffix Action in the function name
882 is for the callback function for the corresponding widget */
884 static void propFontToggleAction(Widget widget,
885 xfselControlBlkType *ctrlBlk,
886 XmToggleButtonCallbackStruct *call_data)
888 if (call_data->reason == XmCR_VALUE_CHANGED)
890 if (ctrlBlk->showPropFonts == PREF_FIXED)
891 ctrlBlk->showPropFonts = PREF_PROP;
892 else
893 ctrlBlk->showPropFonts = PREF_FIXED;
895 if (ctrlBlk->sel1 != NULL)
896 XtFree(ctrlBlk->sel1);
897 ctrlBlk->sel1 = NULL;
899 if (ctrlBlk->sel2 != NULL)
900 XtFree(ctrlBlk->sel2);
901 ctrlBlk->sel2 = NULL;
903 if (ctrlBlk->sel3 != NULL)
904 XtFree(ctrlBlk->sel3);
905 ctrlBlk->sel3 = NULL;
907 setupScrollLists(NONE, *ctrlBlk);
909 XmTextSetString(ctrlBlk->fontNameField, "");
910 enableSample(ctrlBlk, False, NULL);
915 static void sizeToggleAction(Widget widget,
916 xfselControlBlkType *ctrlBlk,
917 XmToggleButtonCallbackStruct *call_data)
919 int i, makeSelection;
920 char newSize[10];
921 XmString str;
923 if (call_data->reason == XmCR_VALUE_CHANGED)
925 makeSelection = (ctrlBlk->sel3 != NULL);
927 for (i = 0; (makeSelection) && (i < ctrlBlk->numFonts); i++)
928 if ((fontMatch(ctrlBlk, ctrlBlk->fontData[i])) &&
929 (styleMatch(ctrlBlk, ctrlBlk->fontData[i])) &&
930 (sizeMatch(ctrlBlk, ctrlBlk->fontData[i])))
932 getSizePart(ctrlBlk->fontData[i], newSize,
933 !ctrlBlk->showSizeInPixels);
934 break;
937 if (ctrlBlk->showSizeInPixels)
938 ctrlBlk->showSizeInPixels = FALSE;
939 else
940 ctrlBlk->showSizeInPixels = TRUE;
942 if (ctrlBlk->sel3 != NULL)
943 XtFree(ctrlBlk->sel3);
945 ctrlBlk->sel3 = NULL;
946 setupScrollLists(NONE, *ctrlBlk);
948 if (makeSelection)
950 str = XmStringCreate(newSize, XmSTRING_DEFAULT_CHARSET);
951 XmListSelectItem(ctrlBlk->sizeList, str, TRUE);
952 XmListSetBottomItem(ctrlBlk->sizeList, str);
953 XmStringFree(str);
959 static void enableSample(xfselControlBlkType *ctrlBlk, Bool turn_on,
960 XmFontList *fontList)
962 int n=0;
963 Arg args[4];
965 XtSetArg(args[n], XmNeditable, turn_on); n++;
966 XtSetArg(args[n], XmNcursorPositionVisible, turn_on); n++;
967 if( turn_on ) {
968 if( !fontList ) {
969 fprintf(stderr, "nedit: Internal error in fontsel.c, line %i\n", \
970 __LINE__);
971 } else {
972 XtSetArg(args[n], XmNfontList, *fontList); n++;
974 XtSetArg(args[n], XmNforeground, ctrlBlk->sampleFG); n++;
975 } else {
976 XtSetArg(args[n], XmNforeground, ctrlBlk->sampleBG); n++;
978 XtSetValues(ctrlBlk->dispField, args, n);
979 /* Make sure the sample area gets resized if the font size changes */
980 XtUnmanageChild(ctrlBlk->dispField);
981 XtManageChild(ctrlBlk->dispField);
985 static void fontAction(Widget widget, xfselControlBlkType *ctrlBlk,
986 XmListCallbackStruct *call_data)
988 char *sel;
990 XmStringGetLtoR(call_data->item, XmSTRING_DEFAULT_CHARSET, &sel);
992 if (ctrlBlk->sel1 == NULL)
994 ctrlBlk->sel1 = XtMalloc(strlen(sel) + 1);
995 strcpy(ctrlBlk->sel1, sel);
997 else
999 if (strcmp(ctrlBlk->sel1, sel) == 0)
1000 { /* Unselecting current selection */
1001 XtFree(ctrlBlk->sel1);
1002 ctrlBlk->sel1 = NULL;
1003 XmListDeselectItem(widget, call_data->item);
1005 else
1007 XtFree(ctrlBlk->sel1);
1008 ctrlBlk->sel1 = XtMalloc(strlen(sel) + 1);
1009 strcpy(ctrlBlk->sel1, sel);
1013 XtFree(sel);
1014 setupScrollLists(FONT, *ctrlBlk);
1015 if ((ctrlBlk->sel1 != NULL) && (ctrlBlk->sel2 != NULL) &&
1016 (ctrlBlk->sel3 != NULL))
1017 choiceMade(ctrlBlk);
1018 else
1020 enableSample(ctrlBlk, False, NULL);
1021 XmTextSetString(ctrlBlk->fontNameField, "");
1026 static void styleAction(Widget widget, xfselControlBlkType *ctrlBlk,
1027 XmListCallbackStruct *call_data)
1029 char *sel;
1031 XmStringGetLtoR(call_data->item, XmSTRING_DEFAULT_CHARSET, &sel);
1033 if (ctrlBlk->sel2 == NULL)
1035 ctrlBlk->sel2 = XtMalloc(strlen(sel) + 1);
1036 strcpy(ctrlBlk->sel2, sel);
1038 else
1040 if (strcmp(ctrlBlk->sel2, sel) == 0)
1041 { /* unselecting current selection */
1042 XtFree(ctrlBlk->sel2);
1043 ctrlBlk->sel2 = NULL;
1044 XmListDeselectItem(widget, call_data->item);
1046 else
1048 XtFree(ctrlBlk->sel2);
1049 ctrlBlk->sel2 = XtMalloc(strlen(sel) + 1);
1050 strcpy(ctrlBlk->sel2, sel);
1054 XtFree(sel);
1055 setupScrollLists(STYLE, *ctrlBlk);
1056 if ((ctrlBlk->sel1 != NULL) && (ctrlBlk->sel2 != NULL) &&
1057 (ctrlBlk->sel3 != NULL))
1058 choiceMade(ctrlBlk);
1059 else
1061 enableSample(ctrlBlk, False, NULL);
1062 XmTextSetString(ctrlBlk->fontNameField, "");
1066 static void sizeAction(Widget widget, xfselControlBlkType *ctrlBlk,
1067 XmListCallbackStruct *call_data)
1069 char *sel;
1071 XmStringGetLtoR(call_data->item, XmSTRING_DEFAULT_CHARSET, &sel);
1073 if (ctrlBlk->sel3 == NULL)
1075 ctrlBlk->sel3 = XtMalloc(strlen(sel) + 1);
1076 strcpy(ctrlBlk->sel3, sel);
1078 else
1080 if (strcmp(ctrlBlk->sel3, sel) == 0)
1081 { /* unselecting current selection */
1082 XtFree(ctrlBlk->sel3);
1083 ctrlBlk->sel3 = NULL;
1084 XmListDeselectItem(widget, call_data->item);
1086 else
1088 XtFree(ctrlBlk->sel3);
1089 ctrlBlk->sel3 = XtMalloc(strlen(sel) + 1);
1090 strcpy(ctrlBlk->sel3, sel);
1094 XtFree(sel);
1095 setupScrollLists(SIZE, *ctrlBlk);
1096 if ((ctrlBlk->sel1 != NULL) && (ctrlBlk->sel2 != NULL) &&
1097 (ctrlBlk->sel3 != NULL))
1098 choiceMade(ctrlBlk);
1099 else
1101 enableSample(ctrlBlk, False, NULL);
1102 XmTextSetString(ctrlBlk->fontNameField, "");
1106 /* function called when all three choices have been made; sets up font
1107 name and displays sample font */
1109 static void choiceMade(xfselControlBlkType *ctrlBlk)
1111 int i;
1113 if (ctrlBlk->fontName != NULL)
1114 XtFree(ctrlBlk->fontName);
1115 ctrlBlk->fontName = NULL;
1117 for (i = 0; i < ctrlBlk->numFonts; i++)
1119 if ((fontMatch(ctrlBlk, ctrlBlk->fontData[i])) &&
1120 (styleMatch(ctrlBlk, ctrlBlk->fontData[i])) &&
1121 (sizeMatch (ctrlBlk, ctrlBlk->fontData[i])))
1123 ctrlBlk->fontName = XtMalloc(
1124 strlen(ctrlBlk->fontData[i]) + 1);
1125 strcpy(ctrlBlk->fontName, ctrlBlk->fontData[i]);
1126 break;
1130 if (ctrlBlk->fontName != NULL)
1132 XmTextSetString(ctrlBlk->fontNameField, ctrlBlk->fontName);
1133 dispSample(ctrlBlk);
1135 else
1137 DialogF (DF_ERR, ctrlBlk->form, 1, "Font Specification",
1138 "Invalid Font Specification", "Dismiss");
1143 /* loads selected font and displays sample text in that font */
1145 static void dispSample(xfselControlBlkType *ctrlBlk)
1147 XFontStruct *font;
1148 XmFontList fontList;
1149 Display *display;
1151 display = XtDisplay(ctrlBlk->form);
1152 font = XLoadQueryFont(display, ctrlBlk->fontName);
1153 fontList = XmFontListCreate(font, XmSTRING_DEFAULT_CHARSET);
1155 enableSample(ctrlBlk, True, &fontList);
1157 if (ctrlBlk->oldFont != NULL)
1159 XFreeFont(display, ctrlBlk->oldFont);
1160 XmFontListFree(ctrlBlk->oldFontList);
1162 ctrlBlk->oldFont = font;
1163 ctrlBlk->oldFontList = fontList;
1167 static void destroyCB(Widget widget, xfselControlBlkType *ctrlBlk,
1168 XmListCallbackStruct *call_data)
1170 /* Prevent double destruction of the font selection dialog */
1171 ctrlBlk->destroyedFlag = TRUE;
1172 cancelAction(widget, ctrlBlk, call_data);
1175 static void cancelAction(Widget widget, xfselControlBlkType *ctrlBlk,
1176 XmListCallbackStruct *call_data)
1178 if (ctrlBlk->sel1 != NULL)
1179 XtFree(ctrlBlk->sel1);
1180 if (ctrlBlk->sel2 != NULL)
1181 XtFree(ctrlBlk->sel2);
1182 if (ctrlBlk->sel3 != NULL)
1183 XtFree(ctrlBlk->sel3);
1184 if (ctrlBlk->fontName != NULL)
1185 XtFree(ctrlBlk->fontName);
1187 ctrlBlk->fontName = NULL;
1188 XFreeFontNames(ctrlBlk->fontData);
1190 ctrlBlk->exitFlag = TRUE;
1194 static void okAction(Widget widget, xfselControlBlkType *ctrlBlk,
1195 XmPushButtonCallbackStruct *call_data)
1197 char *fontPattern;
1198 char **fontName;
1199 int i;
1201 fontPattern = XmTextGetString(ctrlBlk->fontNameField);
1202 fontName = XListFonts(XtDisplay(ctrlBlk->form), fontPattern, 1, &i);
1203 XtFree(fontPattern);
1205 if (i != 1)
1207 DialogF (DF_ERR, ctrlBlk->okButton, 1, "Font Specification",
1208 "Invalid Font Specification", "Dismiss");
1209 XFreeFontNames(fontName);
1211 else
1213 if (ctrlBlk->fontName != NULL)
1214 XtFree(ctrlBlk->fontName);
1215 ctrlBlk->fontName = XtMalloc(strlen(fontName[0]) + 1);
1216 strcpy(ctrlBlk->fontName, fontName[0]);
1218 if (ctrlBlk->sel1 != NULL)
1219 XtFree(ctrlBlk->sel1);
1220 if (ctrlBlk->sel2 != NULL)
1221 XtFree(ctrlBlk->sel2);
1222 if (ctrlBlk->sel3 != NULL)
1223 XtFree(ctrlBlk->sel3);
1225 XFreeFontNames(fontName);
1226 XFreeFontNames(ctrlBlk->fontData);
1228 ctrlBlk->exitFlag = TRUE;
1233 /* if current font is passed as an argument then this function is
1234 invoked and sets up initial entries */
1236 static void startupFont(xfselControlBlkType *ctrlBlk, const char *font)
1238 int i;
1239 char **fontName;
1240 char part[TEMP_BUF_SIZE];
1241 XmString str;
1243 fontName = XListFonts(XtDisplay(ctrlBlk->form), font, 1, &i);
1245 if (i == 0)
1246 { /* invalid font passed in at startup */
1247 XFreeFontNames(fontName);
1248 return;
1251 ctrlBlk->fontName = XtMalloc(strlen(fontName[0]) + 1);
1252 strcpy(ctrlBlk->fontName, fontName[0]);
1254 getFontPart(fontName[0], part);
1255 XFreeFontNames(fontName);
1256 str = XmStringCreate(part, XmSTRING_DEFAULT_CHARSET);
1257 XmListSetBottomItem(ctrlBlk->fontList, str);
1258 XmListSelectItem(ctrlBlk->fontList, str, TRUE);
1259 XmListSelectItem(ctrlBlk->fontList, str, TRUE);
1260 XmStringFree(str);
1262 dispSample(ctrlBlk);
1263 XmTextSetString(ctrlBlk->fontNameField, ctrlBlk->fontName);
1267 /* hacked code to move initial input focus to first scroll list and at the
1268 same time have the OK button as the default button */
1270 static void setFocus(Widget w, xfselControlBlkType *ctrlBlk, XEvent *event,
1271 Boolean *continueToDispatch)
1273 int n;
1274 Arg args[2];
1276 *continueToDispatch = TRUE;
1278 n = 0;
1279 XtSetArg(args[n], XmNdefaultButton, ctrlBlk->okButton); n++;
1280 XtSetValues(ctrlBlk->form, args, n);
1284 /* finds the name of the biggest font less than the given limit
1285 MAX_DISPLAY_SIZE used to set up the initial height of the display widget
1288 static void FindBigFont(xfselControlBlkType *ctrlBlk, char *bigFont)
1290 int i, maxSize, ind = -1, size;
1291 char sizeStr[10];
1293 for (i = 0, maxSize = 0; i < ctrlBlk->numFonts; i++)
1295 getStringComponent(ctrlBlk->fontData[i], 7, sizeStr);
1296 size = atoi(sizeStr);
1297 if ((size > maxSize) && (size < MAX_DISPLAY_SIZE))
1299 ind = i;
1300 maxSize = size;
1303 if (ind >= 0) {
1304 strcpy(bigFont, ctrlBlk->fontData[ind]);
1306 else {
1307 bigFont[0] = 0;