// $Revision: 1.3 $ // Copyright (c) 1994-1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_STYLESNIPPETS #include "StyleSnippets.h" #endif // for TTextColorStyle #ifndef Taligent_LINELAYOUTSTYLES #include #endif // for TFontPointSizeStyle #ifndef Taligent_FONTSTYLES #include #endif // For TUniversalNumberFormatter. #ifndef Taligent_NUMBERFORMAT #include #endif // For TRGBColor. #ifndef Taligent_RGBCOLOR #include #endif // ----------------------------------------------------------------------------- TaligentTypeExtensionMacro(TStyleSnippets) TStyleSnippets::TStyleSnippets() : TSnippets() { SNIPPETINFO(QuerySet_1); SNIPPETINFO(QuerySet_2); SNIPPETINFO(QuerySet_3); SNIPPETINFO(ApplyStyleData); SNIPPETINFO(ModifyStyleData); SNIPPETINFO(IterateStyleSet); SNIPPETINFO(UseSharedStyles); } TStyleSnippets::~TStyleSnippets() { } // Display utilities. void TStyleSnippets::DisplayStyleSet(const TStyleSet& styleSet) { GetDisplay() << "Style set:" << endl << indent; TStyleSetIterator iter(styleSet); for (const TStyle* style = iter.First(); style; style = iter.Next()) { DisplayStyle(*style); } } void TStyleSnippets::DisplayStyle(const TStyle& style) { // This only knows how to display the value of TFontFamilyStyle, // TFontPointSizeStyle, and TTextColorStyle. TSnippetDisplay& display = GetDisplay(); static const int kBufferLength = 60; UniChar buffer[kBufferLength]; unsigned long length; TToken kindName = style.GetKind().GetName(); kindName.GetData(buffer, kBufferLength); length = Min(kBufferLength, kindName.GetLength()); display << "kind: " << TRepUniChars(buffer, length); TStyleCategory category = style.GetCategory(); category.GetData(buffer, kBufferLength); length = Min(kBufferLength, category.GetLength()); display << ", category: " << TRepUniChars(buffer, length); TStyleName name = style.GetName(); name.GetData(buffer, kBufferLength); length = Min(kBufferLength, name.GetLength()); display << ", name: " << TRepUniChars(buffer, length); TFontFamilyStyle* familyStyle; ::DynamicCastTo(familyStyle, &style); if (familyStyle) { TToken token; familyStyle->GetFamilyToken(token); token.GetData(buffer, kBufferLength); length = Min(kBufferLength, token.GetLength()); display << ", value: " << TRepUniChars(buffer, length) << endl; return; } TFontPointSizeStyle* pointSizeStyle; ::DynamicCastTo(pointSizeStyle, &style); if (pointSizeStyle) { double value = pointSizeStyle->GetValue(); display << ", value: " << value << " hoo" << endl; return; } TTextColorStyle* textColorStyle; ::DynamicCastTo(textColorStyle, &style); if (textColorStyle) { const struct { const char* name; const TTextColorStyle& style; } namedStyles[8] = { { "black", TTextColorStyle::GetBlack() }, { "blue", TTextColorStyle::GetBlue() }, { "cyan", TTextColorStyle::GetCyan() }, { "green", TTextColorStyle::GetGreen() }, { "magenta", TTextColorStyle::GetMagenta() }, { "red", TTextColorStyle::GetRed() }, { "white", TTextColorStyle::GetWhite() }, { "yellow", TTextColorStyle::GetYellow() } }; display << ", value: "; for (int i = 0; i < 8; i++) { if (*textColorStyle == namedStyles[i].style) { display << namedStyles[i].name; break; } } if (i == 8) { display << "unknown"; } display << endl; return; } } // Data utilities. const TStyleSet& TStyleSnippets::GetBlue24ptHelveticaStyleSet() const { static TStyleSet* gBlue24ptHelveticaStyleSet = NIL; if (!gBlue24ptHelveticaStyleSet) { gBlue24ptHelveticaStyleSet = new TStyleSet; gBlue24ptHelveticaStyleSet->Add(TTextColorStyle::GetBlue()); gBlue24ptHelveticaStyleSet->Add(TFontPointSizeStyle(24.0)); gBlue24ptHelveticaStyleSet->Add(TFontFamilyStyle(TToken("Helvetica"))); } return *gBlue24ptHelveticaStyleSet; } // Check to see if a style set contains a style of the same 'type' and with // the same value as a given style. void TStyleSnippets::QuerySet_1() { const TStyleSet styleSet = GetBlue24ptHelveticaStyleSet(); if (styleSet.MemberByValue(TTextColorStyle::GetBlue())) { GetDisplay() << "Style set contains blue color style." << endl; } } // Check to see if a style set contains a style of the same 'type' as a given // style, and find out the value. void TStyleSnippets::QuerySet_2() { const TStyleSet styleSet = GetBlue24ptHelveticaStyleSet(); const TStyle* style = NIL; if (styleSet.MemberByName(TFontPointSizeStyle(), style)) { GCoordinate pointSize = ((TFontPointSizeStyle*)style)->GetValue(); GetDisplay() << "Point size is: " << pointSize << endl; } } // Check to see if a style set contains a style with a given category and name // (which define the 'type' of a style). void TStyleSnippets::QuerySet_3() { const TStyleSet styleSet = GetBlue24ptHelveticaStyleSet(); TTextColorStyle colorStyle; TStyleCategory category = colorStyle.GetCategory(); TStyleName name = colorStyle.GetName(); if (styleSet.MemberByName(category, name)) { GetDisplay() << "Style set contains a color style." << endl; } } // Create styled text. This creates blue 24pt helvetica text. void TStyleSnippets::ApplyStyleData() { TStandardText text("Blue 24pt TaligentMono."); GetDisplay() << "Before: " << text << endl; TStyleSet styleSet; styleSet.Add(TTextColorStyle::GetBlue()); styleSet.Add(TFontPointSizeStyle(24.0)); styleSet.Add(TFontFamilyStyle(TToken("TaligentMono"))); text.AddStyles(styleSet); GetDisplay() << "After: " << text << endl; } // Modify styles over a range of styled text. Existing styles of the same // type are changed to the new value. Note that AddStyles and related // members are overloaded to take either a TStyleSet or a TStyle. void TStyleSnippets::ModifyStyleData() { TStandardText text("Red 24pt helvetica."); text.AddStyles(GetBlue24ptHelveticaStyleSet()); GetDisplay() << "Before: " << text << endl; TTextRange redRange(0, TTextCount(3)); // 'Red' text.AddStyles(TTextColorStyle::GetRed(), redRange); GetDisplay() << "After: " << text << endl; } // Iterate over a style set, printing out information on the styles. void TStyleSnippets::IterateStyleSet() { TStyleSet styleSet = GetBlue24ptHelveticaStyleSet(); GetDisplay() << indent; TStyleSetIterator iter(styleSet); for (const TStyle* style = iter.First(); style; style = iter.Next()) { DisplayStyle(*style); } GetDisplay() << outdent; GetDisplay() << "The set has " << styleSet.Count() << " styles." << endl; } #ifndef TaligentSamples_SHAREDCOLORSTYLE #include "SharedColorStyle.h" #endif // Illustrate use of the shared style defined in SharedColorStyle.[hC] // Note that line layout can't display styles other than those that descend // from the standard set. Since shared styles don't, they don't display, // but you can use them to tag data. void TStyleSnippets::UseSharedStyles() { TSharedColorStyle sharedStyle(TRGBColor(0.3, 0.8, 0.9)); TStandardText text("A color and a color and a color."); text.AddStyles(sharedStyle, TTextRange(2, TTextCount(5))); text.AddStyles(sharedStyle, TTextRange(14, TTextCount(5))); text.AddStyles(sharedStyle, TTextRange(26, TTextCount(5))); GetDisplay() << text << endl; }