diff options
author | Matthieu Herrb <matthieu@cvs.openbsd.org> | 2022-07-15 10:10:13 +0000 |
---|---|---|
committer | Matthieu Herrb <matthieu@cvs.openbsd.org> | 2022-07-15 10:10:13 +0000 |
commit | 5bfce82a5085e9f980ce86e756a155b2628bf4c8 (patch) | |
tree | dacd2d4dce33ab121e85b23221f8dbcec9686d23 /app/xfontsel/xfontsel.c | |
parent | c3cfff21222f0e6511048aad1ae2d7b0739aad1f (diff) |
Update xfontsel to vesion 1.1.0
Diffstat (limited to 'app/xfontsel/xfontsel.c')
-rw-r--r-- | app/xfontsel/xfontsel.c | 129 |
1 files changed, 101 insertions, 28 deletions
diff --git a/app/xfontsel/xfontsel.c b/app/xfontsel/xfontsel.c index 9ab33d11d..540fade0f 100644 --- a/app/xfontsel/xfontsel.c +++ b/app/xfontsel/xfontsel.c @@ -50,6 +50,7 @@ Modified: Mark Leisher <mleisher@crl.nmsu.edu> to deal with UCS sample text. #include <X11/Xmu/Atoms.h> #include <X11/Xmu/StdSel.h> #include <X11/Xfuncs.h> +#include <X11/Xlib.h> #include "ULabel.h" #define MIN_APP_DEFAULTS_VERSION 1 @@ -67,7 +68,8 @@ Modified: Mark Leisher <mleisher@crl.nmsu.edu> to deal with UCS sample text. void GetFontNames(XtPointer closure); Boolean Matches(String pattern, String fontName, Boolean fields[], int *maxfields); Boolean DoWorkPiece(XtPointer closure); -void Quit(Widget w, XtPointer closure, XtPointer callData); +void Quit(Widget w, XtPointer closure, XtPointer callData) _X_NORETURN; +void Reset(Widget w, XtPointer closure, XtPointer callData); void OwnSelection(Widget w, XtPointer closure, XtPointer callData); void SelectField(Widget w, XtPointer closure, XtPointer callData); void ParseFontNames(XtPointer closure); @@ -79,7 +81,8 @@ void AnyValue(Widget w, XtPointer closure, XtPointer callData); void EnableOtherValues(Widget w, XtPointer closure, XtPointer callData); void EnableMenu(XtPointer closure); void SetCurrentFont(XtPointer closure); -void QuitAction(Widget w, XEvent *event, String *params, Cardinal *num_params); +void QuitAction(Widget w, XEvent *event, String *params, Cardinal *num_params) + _X_NORETURN; static XtActionsRec xfontsel_actions[] = { {"Quit", QuitAction} @@ -95,8 +98,8 @@ static struct _appRes { int app_defaults_version; Cursor cursor; String pattern; - String pixelSizeList; - String pointSizeList; + char *pixelSizeList; + char *pointSizeList; Boolean print_on_quit; String sample_text; String sample_text16; @@ -159,7 +162,8 @@ static void Syntax(const char *call) " -sample string sample text to use for 1-byte fonts\n" " -sample16 string sample text to use for 2-byte fonts\n" " -sampleUCS string sample text to use for ISO10646 fonts\n" - " -scaled use scaled instances of fonts\n"); + " -scaled use scaled instances of fonts\n" + "plus any standard toolkit options\n"); exit (1); } @@ -222,6 +226,7 @@ static void ScheduleWork(XtProc proc, XtPointer closure, int priority); static void SetCurrentFontCount(void); static void SetNoFonts(void); static void SetParsingFontCount(int count); +static void reset_currentFontNameString(void); static XtAppContext appCtx; static int numFonts; @@ -233,11 +238,12 @@ static FieldValueList *fieldValues[FIELD_COUNT]; static FontValues currentFont; static int matchingFontCount; static Boolean anyDisabled = False; +static Widget resetButton; static Widget ownButton; static Widget fieldBox; static Widget countLabel; static Widget currentFontName; -static String currentFontNameString; +static char *currentFontNameString; static int currentFontNameSize; static Widget sampleText; static int textEncoding = -1; @@ -274,7 +280,7 @@ see 'xfontsel' manual page." ); } - ScheduleWork(GetFontNames, (XtPointer)XtDisplay(topLevel), 0); + ScheduleWork(GetFontNames, (XtPointer)topLevel, 0); pane = XtCreateManagedWidget("pane",panedWidgetClass,topLevel,NZ); { @@ -282,11 +288,14 @@ see 'xfontsel' manual page." commandBox = XtCreateManagedWidget("commandBox",formWidgetClass,pane,NZ); { - Widget quitButton /*, ownButton , countLabel*/; + Widget quitButton /*, resetButton, ownButton , countLabel*/; quitButton = XtCreateManagedWidget("quitButton",commandWidgetClass,commandBox,NZ); + resetButton = + XtCreateManagedWidget("resetButton",commandWidgetClass,commandBox,NZ); + ownButton = XtCreateManagedWidget("ownButton",toggleWidgetClass,commandBox,NZ); @@ -294,6 +303,7 @@ see 'xfontsel' manual page." XtCreateManagedWidget("countLabel",labelWidgetClass,commandBox,NZ); XtAddCallback(quitButton, XtNcallback, Quit, NULL); + XtAddCallback(resetButton, XtNcallback, Reset, NULL); XtAddCallback(ownButton,XtNcallback,OwnSelection,(XtPointer)True); } @@ -321,10 +331,7 @@ see 'xfontsel' manual page." /* currentFontName = */ { Arg args[1]; - currentFontNameSize = strlen(AppRes.pattern); - if (currentFontNameSize < 128) currentFontNameSize = 128; - currentFontNameString = (String)XtMalloc(currentFontNameSize); - strcpy(currentFontNameString, AppRes.pattern); + reset_currentFontNameString(); XtSetArg(args[0], XtNlabel, currentFontNameString); currentFontName = XtCreateManagedWidget("fontName",labelWidgetClass,pane,args,ONE); @@ -348,7 +355,7 @@ see 'xfontsel' manual page." &wm_delete_window, 1); XtAppMainLoop(appCtx); - return 0; + exit(0); } @@ -441,19 +448,24 @@ struct ParseRec { void GetFontNames(XtPointer closure) { - Display *dpy = (Display*)closure; + Widget topLevel = (Widget)closure; + Display *dpy = XtDisplay(topLevel); ParseRec *parseRec; - int f, field, count; - String *fontNames; - Boolean *b; + int count; + char **fontNames; int work_priority = 0; fontNames = XListFonts(dpy, AppRes.pattern, 32767, &numFonts); fonts = (FontValues*)XtMalloc( numFonts*sizeof(FontValues) ); fontInSet = (Boolean*)XtMalloc( numFonts*sizeof(Boolean) ); - for (f = numFonts, b = fontInSet; f; f--, b++) *b = True; - for (field = 0; field < FIELD_COUNT; field++) { + { + int f; + Boolean *b; + for (f = numFonts, b = fontInSet; f; f--, b++) + *b = True; + } + for (int field = 0; field < FIELD_COUNT; field++) { fieldValues[field] = (FieldValueList*)XtMalloc(sizeof(FieldValueList)); fieldValues[field]->allocated = 1; fieldValues[field]->count = 0; @@ -491,7 +503,7 @@ void GetFontNames(XtPointer closure) ScheduleWork((XtProc)XFreeFontNames,(XtPointer)fontNames,work_priority); ScheduleWork((XtProc)XtFree, (XtPointer)parseRec, work_priority); if (AppRes.scaled_fonts) - ScheduleWork(FixScalables,(XtPointer)0,work_priority); + ScheduleWork(FixScalables,(XtPointer)topLevel, work_priority); ScheduleWork(SortFields,(XtPointer)0,work_priority); SetParsingFontCount(matchingFontCount); if (strcmp(AppRes.pattern, DEFAULTPATTERN)) { @@ -574,9 +586,10 @@ void ParseFontNames(XtPointer closure) if (len == 0) v->string = NULL; else { - v->string = (String)XtMalloc( len+1 ); - strncpy( v->string, fieldP, len ); - v->string[len] = '\0'; + char *s = XtMalloc(len + 1); + strncpy( s, fieldP, len ); + s[len] = '\0'; + v->string = (String) s; } v->font = (int*)XtMalloc( 10*sizeof(int) ); v->allocated = 10; @@ -700,13 +713,50 @@ static void NewScalables(int f, char *slist) /* Find all scalable fonts, defined as the set matching "0" in the pixel * size field (field 6). Augment the match-lists for all other fields * that are scalable. Add in new scalable pixel and point sizes given - * in resources. + * in resources, along with the current Screen's actual resX and resY + * values. */ /*ARGSUSED*/ void FixScalables(XtPointer closure) { int i; FieldValue *fval = fieldValues[6]->value; + Widget topLevel = (Widget) closure; + Display *dpy = XtDisplay(topLevel); + int scr = XScreenNumberOfScreen(XtScreenOfObject(topLevel)); + double xres, yres; + static char xreslist[21]; /* log10(UINT64_MAX) == 19 */ + static char yreslist[21]; + + /* from xdpyinfo.c: + * there are 2.54 centimeters to an inch; so there are 25.4 millimeters. + * + * dpi = N pixels / (M millimeters / (25.4 millimeters / 1 inch)) + * = N pixels / (M inch / 25.4) + * = N * 25.4 pixels / M inch + */ + xres = ((((double) DisplayWidth(dpy, scr)) * 25.4) / + ((double) DisplayWidthMM(dpy, scr))); + yres = ((((double) DisplayHeight(dpy, scr)) * 25.4) / + ((double) DisplayHeightMM(dpy, scr))); + + /* + * xxx the "0" element is always added, so we can't force these here.... + * + * However, what's interesting is that if the pattern contains '*' for these + * fields (i.e. instead of '0') then we end up with the menu containing "0, + * 100, xres", which makes for a really good demonstration of how scaling + * fonts without knowing the true screen resolution leads to very wonky + * results. + * + * xxx obviously these are static and related only to the screen of the + * Widget at the time this code executes and so you can't drag the Xfontsel + * winto to another screen with a different resolution and see things change + * dynamically -- you have to instantiate a new Xfontsel process on each + * different screen as desired. + */ + sprintf(xreslist, "%d", (int) (xres + 0.5)); + sprintf(yreslist, "%d", (int) (yres + 0.5)); for (i = fieldValues[6]->count; --i >= 0; fval++) { if (fval->string && !strcmp(fval->string, "0")) { @@ -716,8 +766,8 @@ void FixScalables(XtPointer closure) NewScalables(6, AppRes.pixelSizeList); AddScalables(7); NewScalables(7, AppRes.pointSizeList); - AddScalables(8); - AddScalables(9); + NewScalables(8, xreslist); + NewScalables(9, yreslist); AddScalables(11); break; } @@ -904,6 +954,7 @@ static void SetNoFonts(void) matchingFontCount = 0; SetCurrentFontCount(); XtSetSensitive(fieldBox, False); + XtSetSensitive(resetButton, False); XtSetSensitive(ownButton, False); if (AppRes.app_defaults_version >= MIN_APP_DEFAULTS_VERSION) { XtUnmapWidget(sampleText); @@ -1088,7 +1139,7 @@ void SetCurrentFont(XtPointer closure) len = 1; } if (len+1 > --bytesLeft) { - currentFontNameString = (String) + currentFontNameString = XtRealloc(currentFontNameString, currentFontNameSize+=128); bytesLeft += 128; } @@ -1284,7 +1335,7 @@ void EnableOtherValues(Widget w, XtPointer closure, XtPointer callData) if (scaledFonts) { /* Check for 2 out of 3 scalable y fields being set */ - char *str; + const char *str; Bool specificPxl, specificPt, specificY; f = currentFont.value_index[6]; @@ -1392,6 +1443,28 @@ void Quit(Widget w, XtPointer closure, XtPointer callData) } +void Reset(Widget w, XtPointer closure, XtPointer callData) { + Arg args[1]; + reset_currentFontNameString(); + XtSetArg(args[0], XtNlabel, currentFontNameString); + XtSetValues(currentFontName, args, ONE); + + for (int f = 0; f < FIELD_COUNT; f++) + currentFont.value_index[f] = patternFieldSpecified[f] ? 0 : -1; + + SetCurrentFont(NULL); + EnableRemainingItems(SkipCurrentField); /* menu */ +} + +static void reset_currentFontNameString(void) { + currentFontNameSize = strlen(AppRes.pattern); + if (currentFontNameSize < 128) currentFontNameSize = 128; + XtFree(currentFontNameString); + currentFontNameString = XtMalloc(currentFontNameSize); + strcpy(currentFontNameString, AppRes.pattern); +} + + static Boolean ConvertSelection(Widget w, Atom *selection, Atom *target, Atom *type, XtPointer *value, unsigned long *length, int *format) |