fontsel.c
来自「nedit 是一款linux下的开发源码的功能强大的编辑器」· C语言 代码 · 共 1,313 行 · 第 1/4 页
C
1,313 行
static void fontAction(Widget widget, xfselControlBlkType *ctrlBlk, XmListCallbackStruct *call_data){ char *sel; XmStringGetLtoR(call_data->item, XmSTRING_DEFAULT_CHARSET, &sel); if (ctrlBlk->sel1 == NULL) { ctrlBlk->sel1 = XtMalloc(strlen(sel) + 1); strcpy(ctrlBlk->sel1, sel); } else { if (strcmp(ctrlBlk->sel1, sel) == 0) { /* Unselecting current selection */ XtFree(ctrlBlk->sel1); ctrlBlk->sel1 = NULL; XmListDeselectItem(widget, call_data->item); } else { XtFree(ctrlBlk->sel1); ctrlBlk->sel1 = XtMalloc(strlen(sel) + 1); strcpy(ctrlBlk->sel1, sel); } } XtFree(sel); setupScrollLists(FONT, *ctrlBlk); if ((ctrlBlk->sel1 != NULL) && (ctrlBlk->sel2 != NULL) && (ctrlBlk->sel3 != NULL)) choiceMade(ctrlBlk); else { enableSample(ctrlBlk, False, NULL); XmTextSetString(ctrlBlk->fontNameField, ""); }}static void styleAction(Widget widget, xfselControlBlkType *ctrlBlk, XmListCallbackStruct *call_data){ char *sel; XmStringGetLtoR(call_data->item, XmSTRING_DEFAULT_CHARSET, &sel); if (ctrlBlk->sel2 == NULL) { ctrlBlk->sel2 = XtMalloc(strlen(sel) + 1); strcpy(ctrlBlk->sel2, sel); } else { if (strcmp(ctrlBlk->sel2, sel) == 0) { /* unselecting current selection */ XtFree(ctrlBlk->sel2); ctrlBlk->sel2 = NULL; XmListDeselectItem(widget, call_data->item); } else { XtFree(ctrlBlk->sel2); ctrlBlk->sel2 = XtMalloc(strlen(sel) + 1); strcpy(ctrlBlk->sel2, sel); } } XtFree(sel); setupScrollLists(STYLE, *ctrlBlk); if ((ctrlBlk->sel1 != NULL) && (ctrlBlk->sel2 != NULL) && (ctrlBlk->sel3 != NULL)) choiceMade(ctrlBlk); else { enableSample(ctrlBlk, False, NULL); XmTextSetString(ctrlBlk->fontNameField, ""); }}static void sizeAction(Widget widget, xfselControlBlkType *ctrlBlk, XmListCallbackStruct *call_data){ char *sel; XmStringGetLtoR(call_data->item, XmSTRING_DEFAULT_CHARSET, &sel); if (ctrlBlk->sel3 == NULL) { ctrlBlk->sel3 = XtMalloc(strlen(sel) + 1); strcpy(ctrlBlk->sel3, sel); } else { if (strcmp(ctrlBlk->sel3, sel) == 0) { /* unselecting current selection */ XtFree(ctrlBlk->sel3); ctrlBlk->sel3 = NULL; XmListDeselectItem(widget, call_data->item); } else { XtFree(ctrlBlk->sel3); ctrlBlk->sel3 = XtMalloc(strlen(sel) + 1); strcpy(ctrlBlk->sel3, sel); } } XtFree(sel); setupScrollLists(SIZE, *ctrlBlk); if ((ctrlBlk->sel1 != NULL) && (ctrlBlk->sel2 != NULL) && (ctrlBlk->sel3 != NULL)) choiceMade(ctrlBlk); else { enableSample(ctrlBlk, False, NULL); XmTextSetString(ctrlBlk->fontNameField, ""); }}/* function called when all three choices have been made; sets up font name and displays sample font */static void choiceMade(xfselControlBlkType *ctrlBlk){ int i; if (ctrlBlk->fontName != NULL) XtFree(ctrlBlk->fontName); ctrlBlk->fontName = NULL; for (i = 0; i < ctrlBlk->numFonts; i++) { if ((fontMatch(ctrlBlk, ctrlBlk->fontData[i])) && (styleMatch(ctrlBlk, ctrlBlk->fontData[i])) && (sizeMatch (ctrlBlk, ctrlBlk->fontData[i]))) { ctrlBlk->fontName = XtMalloc( strlen(ctrlBlk->fontData[i]) + 1); strcpy(ctrlBlk->fontName, ctrlBlk->fontData[i]); break; } } if (ctrlBlk->fontName != NULL) { XmTextSetString(ctrlBlk->fontNameField, ctrlBlk->fontName); dispSample(ctrlBlk); } else { DialogF (DF_ERR, ctrlBlk->form, 1, "Font Specification", "Invalid Font Specification", "Dismiss"); }}/* loads selected font and displays sample text in that font */static void dispSample(xfselControlBlkType *ctrlBlk){ XFontStruct *font; XmFontList fontList; Display *display; display = XtDisplay(ctrlBlk->form); font = XLoadQueryFont(display, ctrlBlk->fontName); fontList = XmFontListCreate(font, XmSTRING_DEFAULT_CHARSET); enableSample(ctrlBlk, True, &fontList); if (ctrlBlk->oldFont != NULL) { XFreeFont(display, ctrlBlk->oldFont); XmFontListFree(ctrlBlk->oldFontList); } ctrlBlk->oldFont = font; ctrlBlk->oldFontList = fontList;}static void destroyCB(Widget widget, xfselControlBlkType *ctrlBlk, XmListCallbackStruct *call_data){ /* Prevent double destruction of the font selection dialog */ ctrlBlk->destroyedFlag = TRUE; cancelAction(widget, ctrlBlk, call_data);}static void cancelAction(Widget widget, xfselControlBlkType *ctrlBlk, XmListCallbackStruct *call_data){ if (ctrlBlk->sel1 != NULL) XtFree(ctrlBlk->sel1); if (ctrlBlk->sel2 != NULL) XtFree(ctrlBlk->sel2); if (ctrlBlk->sel3 != NULL) XtFree(ctrlBlk->sel3); if (ctrlBlk->fontName != NULL) XtFree(ctrlBlk->fontName); ctrlBlk->fontName = NULL; XFreeFontNames(ctrlBlk->fontData); ctrlBlk->exitFlag = TRUE;}static void okAction(Widget widget, xfselControlBlkType *ctrlBlk, XmPushButtonCallbackStruct *call_data){ char *fontPattern; char **fontName; int i; fontPattern = XmTextGetString(ctrlBlk->fontNameField); fontName = XListFonts(XtDisplay(ctrlBlk->form), fontPattern, 1, &i); XtFree(fontPattern); if (i != 1) { DialogF (DF_ERR, ctrlBlk->okButton, 1, "Font Specification", "Invalid Font Specification", "Dismiss"); XFreeFontNames(fontName); } else { if (ctrlBlk->fontName != NULL) XtFree(ctrlBlk->fontName); ctrlBlk->fontName = XtMalloc(strlen(fontName[0]) + 1); strcpy(ctrlBlk->fontName, fontName[0]); if (ctrlBlk->sel1 != NULL) XtFree(ctrlBlk->sel1); if (ctrlBlk->sel2 != NULL) XtFree(ctrlBlk->sel2); if (ctrlBlk->sel3 != NULL) XtFree(ctrlBlk->sel3); XFreeFontNames(fontName); XFreeFontNames(ctrlBlk->fontData); ctrlBlk->exitFlag = TRUE; }}/* if current font is passed as an argument then this function is invoked and sets up initial entries */static void startupFont(xfselControlBlkType *ctrlBlk, const char *font){ int i; char **fontName; char part[TEMP_BUF_SIZE]; XmString str; fontName = XListFonts(XtDisplay(ctrlBlk->form), font, 1, &i); if (i == 0) { /* invalid font passed in at startup */ XFreeFontNames(fontName); return; } ctrlBlk->fontName = XtMalloc(strlen(fontName[0]) + 1); strcpy(ctrlBlk->fontName, fontName[0]); getFontPart(fontName[0], part); XFreeFontNames(fontName); str = XmStringCreate(part, XmSTRING_DEFAULT_CHARSET); XmListSetBottomItem(ctrlBlk->fontList, str); XmListSelectItem(ctrlBlk->fontList, str, TRUE); XmListSelectItem(ctrlBlk->fontList, str, TRUE); XmStringFree(str); dispSample(ctrlBlk); XmTextSetString(ctrlBlk->fontNameField, ctrlBlk->fontName);}/* hacked code to move initial input focus to first scroll list and at the same time have the OK button as the default button */static void setFocus(Widget w, xfselControlBlkType *ctrlBlk, XEvent *event, Boolean *continueToDispatch){ int n; Arg args[2]; *continueToDispatch = TRUE; n = 0; XtSetArg(args[n], XmNdefaultButton, ctrlBlk->okButton); n++; XtSetValues(ctrlBlk->form, args, n);}/* finds the name of the biggest font less than the given limit MAX_DISPLAY_SIZE used to set up the initial height of the display widget*/static void FindBigFont(xfselControlBlkType *ctrlBlk, char *bigFont){ int i, maxSize, ind = -1, size; char sizeStr[10]; for (i = 0, maxSize = 0; i < ctrlBlk->numFonts; i++) { getStringComponent(ctrlBlk->fontData[i], 7, sizeStr); size = atoi(sizeStr); if ((size > maxSize) && (size < MAX_DISPLAY_SIZE)) { ind = i; maxSize = size; } } if (ind >= 0) { strcpy(bigFont, ctrlBlk->fontData[ind]); } else { bigFont[0] = 0; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?