📄 fontchooserc.c
字号:
val += charset2; } val += ')'; return val;} // End GetFamily/*----------------------------------------------------------------------- * Get the style string from a font name */StringCFontChooserC::GetStyle(char *cs){ CharC weight = GetNamePart(cs, F_WEIGHT); CharC width = GetNamePart(cs, F_WIDTH); CharC serif = GetNamePart(cs, F_SERIF); CharC slant = GetNamePart(cs, F_SLANT); StringC val = weight; if ( !width.Equals("normal", IGNORE_CASE) ) { val += ' '; val += width; } if ( serif.Length() > 0 ) { val += ' '; val += serif; } if ( slant.Equals('o', IGNORE_CASE) ) { val += ' '; val += "oblique"; } else if ( slant.Equals('i', IGNORE_CASE) ) { val += ' '; val += "italic"; } val.Trim(); if ( val.size() == 0 ) val = "-"; return val;} // End GetStyle/*----------------------------------------------------------------------- * Get the point size component from a font name */StringCFontChooserC::GetPointSize(char *cs){ StringC val = GetNamePart(cs, F_POINTS); int size = atoi(val); int smaj = size / 10; int smin = size % 10; val.Clear(); val += smaj; if ( smin != 0 ) { val += '.'; val += smin; } return val;} // End GetPointSize/*----------------------------------------------------------------------- * Get a font name component from the specified position in the name */CharCFontChooserC::GetNamePart(char *cs, int pos){ CharC val; if ( pos < 0 || pos > MAX_NAME_COMPONENT ) return val;//// Look for '-' between components in name. Decrement 'pos' for each one found// while (pos > 0 && *cs) { if ( *cs == '-') pos--; cs++; } if ( *cs ) { char *start = cs; int len = 0; while ( *cs && *cs != '-' ) { cs++; len++; } val.Set(start, len); } return val;} // End GetNamePart/*----------------------------------------------------------------------- * Get the font name component from the specified position in the name */voidFontChooserC::UpdateLists(Boolean changeFamily, Boolean changeStyle, Boolean changeSize){ StringListC familyNames; StringListC styleNames; StringListC sizeNames; familyNames.SetSorted(FALSE); styleNames.SetSorted(FALSE); sizeNames.SetSorted(FALSE); familyNames.AllowDuplicates(FALSE); styleNames.AllowDuplicates(FALSE); sizeNames.AllowDuplicates(FALSE); StringC family; StringC style; StringC size; Boolean showFixed = XmToggleButtonGetState(showFixedTB); Boolean showProp = XmToggleButtonGetState(showPropTB); Boolean usePixels = XmToggleButtonGetState(sizePixelsTB);//// Loop through font names// for (int i=0; i<fontCount; i++) { FontRecT *rec = &fontData[i]; if ( (rec->fixed && showFixed) || (!rec->fixed && showProp) ) { StringC& size = usePixels ? rec->pixelSize : rec->pointSize; Boolean familyOk = FamilyOk(rec->family); Boolean styleOk = StyleOk(rec->style); Boolean sizeOk = SizeOk(size); if ( changeFamily && styleOk && sizeOk ) familyNames.add(rec->family); if ( changeStyle && familyOk && sizeOk ) styleNames.add(rec->style); if ( changeSize && familyOk && styleOk) sizeNames.add(size); } // End if this font passes } // End for each font//// Sort and display the lists// if ( changeFamily ) { familyNames.sort(); SetList(familyListW, familyNames); } if ( changeStyle ) { styleNames.sort(); SetList(styleListW, styleNames); } if ( changeSize ) { sizeNames.sort((int (*)(const void*, const void*))CompareSizes); SetList(sizeListW, sizeNames); }} // End UpdateLists/*----------------------------------------------------------------------- * Compare function for sizes */intFontChooserC::CompareSizes(const StringC *a, const StringC *b){ int ia = atoi(*a); int ib = atoi(*b); if ( ia > ib ) return 1; if ( ib > ia ) return -1; return 0;}/*----------------------------------------------------------------------- * Method to set an XmList using a StringList */voidFontChooserC::SetList(Widget wlist, StringListC& slist){ int count = slist.size(); XmString *strList = new XmString[count]; int i; for (i=0; i<count; i++) { char *cs = *slist[i]; strList[i] = XmStringCreateLtoR(cs, XmFONTLIST_DEFAULT_TAG); } XtVaSetValues(wlist, XmNitems, strList, XmNitemCount, count, NULL); for (i=0; i<count; i++) XmStringFree(strList[i]); delete strList;}/*----------------------------------------------------------------------- * Display the sample font */voidFontChooserC::UpdateSample(){ if ( selectedFamily.size() == 0 || selectedStyle.size() == 0 || selectedSize.size() == 0 ) {//// Save current sample in case user changed it// char *cs = XmTextGetString(sampText); if ( strlen(cs) > 0 ) sampleStr = cs; XtFree(cs);//// Clear sample and name since no complete font is selected// XmTextSetString(sampText, ""); XmTextFieldSetString(nameTF, ""); return; }//// Loop through the fonts until we find one that matches all selections// StringC family; StringC style; StringC size; Boolean showFixed = XmToggleButtonGetState(showFixedTB); Boolean showProp = XmToggleButtonGetState(showPropTB); Boolean usePixels = XmToggleButtonGetState(sizePixelsTB); for (int i=0; i<fontCount; i++) { FontRecT *rec = &fontData[i]; if ( (rec->fixed && showFixed) || (!rec->fixed && showProp) ) { StringC& size = usePixels ? rec->pixelSize : rec->pointSize; if ( FamilyOk(rec->family) && StyleOk(rec->style) && SizeOk(size) ) {//// Save current string// char *cs = XmTextGetString(sampText); if ( strlen(cs) > 0 ) sampleStr = cs; XtFree(cs); XmTextSetString(sampText, "");//// Load new font// XFontStruct *newFont = XLoadQueryFont(halApp->display, rec->name); XmFontList newFontList = XmFontListCreate(newFont, XmSTRING_DEFAULT_CHARSET);//// Display sample text using new font// XtVaSetValues(sampText, XmNfontList, newFontList, NULL); XmTextSetString(sampText, sampleStr);//// Delete previous font// XFreeFont(halApp->display, sampleFont); XmFontListFree(sampleFontList); sampleFont = newFont; sampleFontList = newFontList;//// Display font name// XmTextFieldSetString(nameTF, rec->name); } // End if we have a complete font definition } // End if this font passes } // End for each font} // End UpdateSample/*----------------------------------------------------------------------- * Handle toggle of a proportional/fixed flag */voidFontChooserC::ToggleProp(Widget tb, FontChooserC *This, XmToggleButtonCallbackStruct *data){//// If there is a selected family that is going away, clear the name now.// if ( !data->set && This->selectedFamily.size() > 0 ) { Boolean fixed = (tb == This->showFixedTB); for (int i=0; i<This->fontCount; i++) { FontRecT *rec = &This->fontData[i]; if ( rec->fixed == fixed && This->selectedFamily == rec->family ) { XmListDeselectAllItems(This->familyListW); This->selectedFamily.Clear(); i = This->fontCount; } } // End for each font } // End if a selected name could go away This->UpdateLists();} // End ToggleProp/*----------------------------------------------------------------------- * Handle toggle of a pixels/points flag */voidFontChooserC::ToggleSize(Widget, FontChooserC *This, XmToggleButtonCallbackStruct *tb){ if ( !tb->set ) return; This->UpdateLists(False, False, True);}/*----------------------------------------------------------------------- * Handle selection of a value in the family list */voidFontChooserC::SelectFamily(Widget, FontChooserC *This, XmListCallbackStruct *l){ WXmString wstr = l->item; char *val = wstr; if ( val == This->selectedFamily ) This->selectedFamily.Clear(); else This->selectedFamily = val; XtFree(val); This->UpdateLists(False, True, True); This->UpdateSample();} // End SelectFamily/*----------------------------------------------------------------------- * Handle selection of a value in the style list */voidFontChooserC::SelectStyle(Widget, FontChooserC *This, XmListCallbackStruct *l){ WXmString wstr = l->item; char *val = wstr; if ( val == This->selectedStyle ) This->selectedStyle.Clear(); else This->selectedStyle = val; XtFree(val); This->UpdateLists(True, False, True); This->UpdateSample();} // End SelectStyle/*----------------------------------------------------------------------- * Handle selection of a value in the size list */voidFontChooserC::SelectSize(Widget, FontChooserC *This, XmListCallbackStruct *l){ WXmString wstr = l->item; char *val = wstr; if ( val == This->selectedSize ) This->selectedSize.Clear(); else This->selectedSize = val; XtFree(val); This->UpdateLists(True, True, False); This->UpdateSample();} // End SelectSize/*----------------------------------------------------------------------- * Handle press of ok button */voidFontChooserC::DoOk(Widget, FontChooserC *This, XtPointer){ if ( XmTextFieldGetLastPosition(This->nameTF) == 0 ) { set_invalid(This->nameTF, True, True); StringC errmsg("Please enter a font name."); This->PopupMessage(errmsg); return; } char *cs = XmTextFieldGetString(This->nameTF); StringC name(cs); XtFree(cs); name.Trim(); if ( name.size() == 0 ) { set_invalid(This->nameTF, True, True); StringC errmsg("Please enter a font name."); This->PopupMessage(errmsg); return; } CallCallbacks(This->okCalls, (char*)name); This->Hide();} // End DoOk/*----------------------------------------------------------------------- * Methods to display certain fonts */voidFontChooserC::ShowProp(Boolean val){ XmToggleButtonSetState(showPropTB, val, True);}voidFontChooserC::ShowFixed(Boolean val){ XmToggleButtonSetState(showFixedTB, val, True);}voidFontChooserC::AllowProp(Boolean val){ if ( !val ) XmToggleButtonSetState(showPropTB, False, True); XtSetSensitive(showPropTB, val);}voidFontChooserC::AllowFixed(Boolean val){ if ( !val ) XmToggleButtonSetState(showFixedTB, False, True); XtSetSensitive(showFixedTB, val);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -