Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / November 2004



Tip: Looking for answers? Try searching our database.

Standard Font Dialog

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
vcmac - 22 Nov 2004 10:30 GMT
How to generate Mac Standard Font Dialog in Using Power Plant?

Thanks
-Hemant
David Phillip Oster - 24 Nov 2004 05:50 GMT
In article
<47de1176e7c7eb357518289b99fd796e@localhost.talkaboutmac.com>,

> How to generate Mac Standard Font Dialog in Using Power Plant?

FontPanel.h only exports 3 functions. Here is how you might
integrate them into PowerPlant:

This is much easier to do if you are using a Mach-O target. In
CFM, you'll need to load the framework and extract function
pointers from it at run time.

Here are some useful member functions (these fit into MLTEDoc,
on <http://www.TurboZen.com/SourceCode/>)

// ---------------------------------------------------------------------------
// € FindCommandStatus                          [public, virtual]
void CTextDocument::FindCommandStatus(
  CommandT    inCommand,
  Boolean&    outEnabled,
  Boolean&    outUsesMark,
  UInt16&        outMark,
  Str255         outName){

  switch(inCommand){
  case cmd_Revert:
     outEnabled = IsSpecified() and IsModified();
     break;
  case kHICommandShowHideFontPanel:
     GetIndString(outName, STRx_Fonts, CFontPanel::IsFontPanelVisible() ? str_HideFont : str_ShowFont);
     outEnabled = true;
     break;
  default: LSingleDoc::FindCommandStatus(inCommand, outEnabled, outUsesMark, outMark, outName);
     break;
  }
}

// ---------------------------------------------------------------------------
// € ObeyCommand                             [public, virtual]
Boolean CTextDocument::ObeyCommand(CommandT inCommand, void* ioParam){
  switch(inCommand){
  case kHICommandShowHideFontPanel:
     CFontPanel::ShowHide();
     return true;
  default: return LSingleDoc::ObeyCommand(inCommand, ioParam);
  }
}

================================================================

// ---------------------------------------------------------------------------------
// € HandleKeyPress                             [public, virtual]
// Note: in 10.3, the keypress is handled directly by Carbon Events, so this code isn't called.
Boolean CTextPane::HandleKeyPress(const EventRecord& inKeyEvent){
  Boolean val = inherited::HandleKeyPress(inKeyEvent);
  SetFontInfoForSelection();
  return val;
}

// ---------------------------------------------------------------------------------
// € IsDirty                              [public]
/// Note: TXNGetChangeCount() does not do what we want here. Use TXNGetActionChangeCount.
Boolean CTextPane::IsDirty() const{
  ItemCount count = 0;
  TXNGetActionChangeCount(const_cast<CTextPane*>(this)->GetTextObject(), kTXNAllCountMask, &count);
  return 0 < count;
}

// ---------------------------------------------------------------------------------
// € SetDirty                             [public]
void CTextPane::SetDirty(Boolean inDirty){
  if(not inDirty){  // caller wants to mark us as not dirty
     TXNClearActionChangeCount(GetTextObject(), kTXNAllCountMask);
  }
}

// ---------------------------------------------------------------------------------
// € UserChangedText                            [public, virtual]
// Called when a user action changes the text
void CTextPane::UserChangedText(){
  SetUpdateCommandStatus(true);
  SetFontInfoForSelection();
}

// ---------------------------------------------------------------------------------
// € SetFontInfoForSelection                             [private]
// tell the font panel about the state of the selection, and where the font panel
// will deliver font changed carbon events.
// Note: although I've looked inside the ATSUIStyle, and it does contain text RGBColor information,
// FontPanel is ignoring the text color info. I haven't checked CoreGraphics color yet.
void CTextPane::SetFontInfoForSelection(){
  if(IsTarget()){
     TXNOffset   start, finish;
     TXNDataType dataType = 0;
     TXNTypeAttributes attr[] = { {kTXNATSUIStyle, kTXNATSUIStyleSize, {0}} };
     OSStatus e;
     TXNGetSelection(GetTextObject(), &start, &finish);
     e = TXNGetIndexedRunInfoFromRange(GetTextObject(), 0, start, start, &start, &finish, &dataType, 1, attr);
     Assert_(noErr == e);
     if(noErr == e){
        switch(dataType){
        case kTXNTextData:
        case kTXNUnicodeTextData:
           CFontPanel::SetFontInfoForSelection((ATSUStyle) attr[0].data.dataPtr, GetWindowEventTarget(GetMacWindow()));
           break;
        }
     }
  }
}

I also added this code to CTextDocument's destructor:

     if(NULL != mWindow){
        mWindow->Hide();
        if(NULL == UDesktop::FetchTopRegular() and CFontPanel::IsVisible()){
           CFontPanel::ShowHide();
        }
     }

================================================================

CFontPanel looks like this:
// CFontPanel.h

class CFontPanel {
public:
  static bool IsFontPanelVisible();
  static void SetFontInfoForSelection(ATSUStyle style, EventTargetRef eventTarget);
  static void ShowHide();
};

// CFontPanel.cp
#include "CFontPanel.h"
#include <FontPanel.h>

// see http://developer.apple.com/qa/qa2004/qa1375.html for why the cast to HIObjectRef
// takes ownership of the style.
void CFontPanel::SetFontInfoForSelection(ATSUStyle style, EventTargetRef eventTarget){
  OSStatus e;
  e = ::SetFontInfoForSelection(kFontSelectionATSUIType, 1, &style, reinterpret_cast<HIObjectRef>(eventTarget));
  Assert_(noErr == e);
  e = ATSUDisposeStyle(style);
  Assert_(noErr == e);
}

bool CFontPanel::IsFontPanelVisible(){
  return FPIsFontPanelVisible();
}

void CFontPanel::ShowHide(){
  ::FPShowHideFontPanel();
}

In Finder, to be able to drag .rtf files onto your app, you'll need
an appropriate info.plist. This .plc file will do the job:

// I've previously posted this file. Search for it on <http://groups.google.com/>
#include "StandardInfoPlistKeys.h"

// Change this to your bundle identifier string
#define kBundleIdentifier "com.turbozen.mltedoc"
#define kBundleShortVersion __OUTPUT_FILENAME__ " version 1.0"

// displayed in the Finder's Get Info window
#define kBundleGetInfo kBundleShortVersion ", Copyright \U00A9 2004 by David Phillip Oster.  All rights reserved."

plist
{
  dictionary
  {
     key CFBundleIdentifier value string kBundleIdentifier
     
     key CFBundleName value string __OUTPUT_FILENAME__
     key CFBundleGetInfoString value string kBundleGetInfo
     key CFBundleShortVersionString value string kBundleShortVersion
     
     key CFBundlePackageType value string __OUTPUT_TYPE__
     key CFBundleSignature value string __OUTPUT_CREATOR__
     key CFBundleVersion value string "1.0"
     
     key CFBundleDevelopmentRegion value string "English"
     key CFBundleInfoDictionaryVersion value string "6.0"
     key LSPrefersCarbon value boolean true
     key CFBundleDocumentTypes value array [
        dictionary {
           key CFBundleTypeExtensions value array [
              string ".rtf"
              string ".txt"
              string ".text"
           ]
           key CFBundleTypeOSTypes value array [
              string "TEXT"
           ]
           key CFBundleTypeRole value string "Viewer"
        }
     ]
  }
}

See unicodetextedit on that same <http://www.TurboZen.com/SourceCode/>
for a possible workaround for the MLTE handling of keystrokes directly,
not through PowerPlant's HandleKeyPress();, but I am still looking in
to this.

DoRevert, which is where file reading takes place should be:

namespace {

FSRef ToFSRef(SInt16 inRefNum){
  FSRef ref;
  ThrowIfOSStatus_(FSGetForkCBInfo(inRefNum, 0, 0, NULL, NULL, &ref, NULL));
  return ref;
}

} // end anonymous namespace

// Read the entire file contents and close the file.
// Put the contents in the text view and clear the dirty flag.
// Note: TXNSetDataFromFile is deprecated.
// Incomplete: this should set the window title from the filename using
// mWindow->SetCFDescriptor(fileName);, to handle the case that the
// use renamed the file on disk.
void CTextDocument::DoRevert(){
  LCFURL cfurl(ToFSRef(mFile->GetDataForkRefNum()));
  ThrowIfOSStatus_(TXNSetDataFromCFURLRef(mTextView->GetTextObject(), cfurl, kTXNStartOffset, kTXNEndOffset));
  mFile->CloseDataFork();
  mFile->OpenDataFork(fsRdWrPerm);
  mTextView->SetDirty(false);
  mTextView->Refresh();
}

* Incomplete:
The missing piece in all of the above, is you'll need to install
CarbonEvent handlers on the owning Window to dispatch Carbon Events
from FontPanel to actually change the selection in the targeted
CTextPane. See FontPanel.h for more information.

* Bug:
TXNGetIndexedRunInfoFromRange() DOES return RGBColor text color
information in the ATSUStyle (I know, I looked.), but
FontPanel ignores it (I think this is a bug in 10.3's FontPanel.)

* Bug:
There is a pause of about a second the first time a FontPanel routine
is called. I tried to workaround this by moving a call to
CFontPanel::IsFontPanelVisible() early in the program, but then I
got error messages on the console saying that there was no
AutoReleasePool.

* Bug:
As mentioned in the code comments, MLTE grabs keystrokes directly,
without going through the PowerPlant LCommander system, so
SetFontInfoForSelection() is only being called when you click.

* Bug:
TXNGetChangeCount() and TXNClearChangeCount() do not appear to
work correctly. Use TXNGetActionChangeCount() and
TXNClearActionChangeCount()

Conclusion:

I must let you know, I'm pretty disgusted. MLTE has been out for
years, and FontPanel.h has been around for awhile. Where are other
people's code examples? I've searched using google.com and
groups.google.com, I've searched the source in (Codewarrior Examples)
and in /Developer/Examples/ and in the end I've had to stumble over
obvious bugs that anyone actually trying to use these subsystems
would have already tripped over.

Learning is endless. What should I have read?

David Phillip Oster
vcmac - 24 Nov 2004 11:33 GMT
Thanks David for font panel.
I have created my own font dialog.
But I have quite a few  problems about MLTE.
 I am using TXNSetDataFromFile() for getting RTF text.
 But In ur code u r using TXNSetDataFromCFURLRef().
My system doesn't support TXNSetDataFromCFURLRef().
So I think I am not getting color attribute.
Can u pls help?

Regards
-Hemant
David Phillip Oster - 24 Nov 2004 16:56 GMT
In article
<12a8973615146fff52b541aff5b67bc5@localhost.talkaboutmac.com>,

> Thanks David for font panel.
>  I have created my own font dialog.
>  But I have quite a few  problems about MLTE.
>   I am using TXNSetDataFromFile() for getting RTF text.
>   But In ur code u r using TXNSetDataFromCFURLRef().
>  My system doesn't support TXNSetDataFromCFURLRef().

You are probably looking at MacTextEditor.h from the Universal Headers,
which are now old. I was looking in /System/Library/Frameworks/

>  So I think I am not getting color attribute.

Are you saying that you downloaded my sample code from the Turbozen web
site, compiled it, pasted the sample RTF from my previous posting into a
file using CodeWarrior, named it with an .rtf extension, and read the
file in with the freshly compiled program, and it parsed the RTF
correctly, except that it did not interpret the color directives, but
Apple's TextEdit did?
David Dunham - 27 Nov 2004 21:46 GMT
In article
<oster-24C6D2.08592824112004@newssvr21-ext.news.prodigy.com>,

> >   I am using TXNSetDataFromFile() for getting RTF text.
> >   But In ur code u r using TXNSetDataFromCFURLRef().
> >  My system doesn't support TXNSetDataFromCFURLRef().
>
> You are probably looking at MacTextEditor.h from the Universal Headers,
> which are now old. I was looking in /System/Library/Frameworks/

Elsewhere he's said his using 10.1, which doesn't support the newer
flattening/unflattening APIs.

Signature

David Dunham    A Sharp, LLC
http://www.a-sharp.com/
   "I say we should listen to the customers and give them what they want."
   "What they want is better products for free." --Scott Adams

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.