ergonis software - software that just works


The PopChar Pro Programming Interface

Contents

  1. Introduction
  2. General Guidelines
  3. How PopChar Pro Works
  4. The PopChar Pro Gestalt Record
  5. Accessing the PopChar Pro Gestalt Record
  6. Specifying the Current Font
  7. The PopChar Pro Floater
  8. The Black List

1. Introduction

This document describes the PopChar Pro Programming Interface. You will need this information only if you are an application developer and can't get PopChar Pro to work with your application using standard techniques. Before you take advantage of the Programming Interface, you should first try to solve the problem by following the General Guidelines. If that doesn't work, consult the next sections about How PopChar Pro Works and on the PopChar Pro Gestalt Record. If you need further help, please contact:

ergonis software gmbh
Softwarepark Hagenberg
Hauptstraße 119
A-4232 Hagenberg / Austria
Fax: +43 7236 3343-399
e-mail: popchar-support@ergonis.com

2. General Guidelines

If you are an application developer, you can make your application work well with PopChar Pro by following a few simple rules (see also the Black List):

  • Include a Font menu in your menu bar (or in a hierarchical submenu).
  • Use the standard techniques to create the Font menu. Use AppendResMenu (formerly AddResMenu) to fill the menu with all available fonts.
  • Make sure that the correct font has a check mark when the user pulls down the menu. You should at least update the check mark immediately before calling MenuSelect.
  • If you don't want or need a Font menu, use TextFont to set the font of your frontmost window to the font that will be used for the next typed character. Make sure the current font (that is, myWindow^.txFont) is set correctly before your event handling or drawing procedures return to the main event loop.

If you cannot follow these rules for some reason, read the next sections about how to control PopChar Pro from within your application.

3. How PopChar Pro Works

PopChar Pro patches a couple of traps to perform additional operations when the user selects a menu item from the menu bar. Some of these patches deal with special cases only (for example, to "read" the font name in the toolbar of Microsoft applications). There are only two patches that are important for you:

3.1. The MenuSelect Patch

When your application detects a click in the menu bar, it should first update its menus and then call MenuSelect. PopChar Pro uses a head patch to find the current font before the PopChar Pro menu is pulled down. Note that this must be done in every case (even when the user clicks the File or Edit menu) because the user might change his/her mind and move the pointer to the PopChar Pro menu.

The MenuSelect patch performs the following task:

  1. When the user holds down the shift key while clicking in the menu bar, the PopChar Pro menu is replaced with a font menu (for manual font selection). The menu handle is generated on demand when such a shift click appears for the first time in an application. During subsequent shift-clicks, this menu handle is reused. When planning the memory require-ments of your application, you should make sure that enough space is available in your application heap to store a menu handle with the names of all installed fonts. Note that hundreds of fonts may be installed. To play safe, you should make sure that about 4kB are always available in your application's heap.
  2. Next, PopChar Pro checks whether your application requested a specific font by storing a font ID in PopChar Pro's gestalt record. If that is the case, PopChar Pro uses this font and skips the steps 3 thru 8. Note that the request for a specific font is cleared during this step; it affects only a single invocation of MenuSelect.
  3. If the frontmost window is a dialog window with at least one editText item, PopChar Pro uses the font that's being used for the editText item that contains the selection or insertion mark, and the remaining steps 4 thru 8 are skipped.
  4. If the user has selected a specific font to be used by this application ("manual font selection", as described in the PopChar Pro User's Guide), PopChar Pro uses this font and skips the remaining steps 5 thru 8.
  5. PopChar Pro then checks for special cases (such as a Finder window displaying a font or the font name shown in a Microsoft toolbar). These checks are hard-coded for certain applications and will not affect your application. If any of the preprogrammed special cases is found, PopChar Pro uses special code to find the current font and skips the remaining steps 6 thru 8.
  6. PopChar Pro scans through all menus of the application to find a checked font name. If it finds a menu item that is both checked and matches the name of an installed font, that font is used and steps 7 and 8 are skipped.
  7. If at least one window is open (not counting floating windows maintained by the Text Services Manager), PopChar Pro uses the current font of the frontmost window (FrontWindow^.txFont) and skips step 8.
  8. If everything else fails, PopChar Pro uses the system font (which is not of much use, as the user can't type text, anyway).

Please note that a specific font requested by an application (step 2) currently overrides manual font selection. This may change in future releases.

3.2. The GetNextEvent Patch

When the user selects a character from the PopChar Pro menu, that character is placed in an internal data structure. When GetNextEvent (or WaitNextEvent, which calls GetNextEvent) is called the next time, PopChar Pro's GetNextEvent patch returns a keyDown event for that character. In this event record, the character code, when and where fields are correctly filled in. The modifiers field is set to 0, and the key code is set to 0.

If your application relies on key codes instead of character codes, it will not be able to process keyDown events generated by PopChar Pro.

If necessary, your application can check the key code to determine if a keyDown event was generated by keyboard input or by PopChar Pro. There is only one case in which this distinction will not work: When the user hits the "A" key on the keyboard without holding any modifier keys, an event with the character code "a" and the key code 0 will be generated. Please note that the key code 0 does not necessarily mean that PopChar Pro generated the keyDown event; other system extensions may use the same techniques, so the key code 0 just means "not a real keyboard event".

4. The PopChar Pro Gestalt Record

When PopChar Pro loads, it installs a gestalt selector "PopÇ" ($506F7082). The value returned by the Gestalt function is a locked handle (of type PopGestaltHnd) to a record with the following structure:

TYPE
  PopGestaltRecord = RECORD
    popGestaltVersion: Integer;
    popPublicVersion: Integer;
    popCharVersion: Integer;
    popCharVersionStr: Str15;
    popCharFileSpec: FSSpec;
    fontToUse:Integer;
    moreData: ARRAY [1..1] OF Integer;
  END;
PopGestaltPtr = ^ PopGestaltRecord;
PopGestaltHnd = ^ PopGestaltP

Note that the actual record contains more undocumented fields for internal use (as indicated by the moreData field).

popGestaltVersion contains a version number of the entire gestalt record (including the undocumented fields). This field is used internally to verify compatibility of the individual parts of the PopChar Pro package. Your application should not depend on a specific value stored in popGestaltVersion.

popPublicVersion contains a version number of the public fields (i.e., those listed in the above record declaration). In PopChar Pro 1.0.x, 1.1, and 1.1.1, popPublicVersion has the value 0. In future versions, this number will increase when additional fields are made available for public access. Such fields will be inserted after fontToUse (to preserve the order of fields published in previous versions). You can then check the value of popPublicVersion to test the availability of additional fields. For now, you can assume that the fields popGestaltVersion thru fontToUse are always available.

popCharVersion contains the PopChar Pro version number in hexadecimal format. Version 1.0 is identified by the value $0100, version 1.0.1 is denoted by $0101, and so forth.

popCharVersionStr contains the PopChar Pro version number in string format. Version 1.1.1 is identified by the value "1.1.1".

popCharFileSpec contains the file specification record of the PopChar Pro control panel.

fontToUse contains the font ID of the font to be used by PopChar Pro. This field normally has the value –1, indicating that the font determination algorithm outlined in section 3.1) is to be used. If PopChar Pro finds any other value in this field during a call of MenuSelect, it interprets that value as a font ID and displays the corresponding font.

IMPORTANT: Do not modify any fields except fontToUse.

5. Accessing the PopChar Pro Gestalt Record

To access the PopChar Pro gestalt record and to test whether PopChar Pro is installed, use the following procedure:

CONST
   popGestaltSelector = 'PopÇ';
VAR
   popPtr: PopGestaltPtr; {global variable}
FUNCTION PopGestaltPointer: PopGestaltPtr;
VAR
   pgh: PopGestaltHnd;
BEGIN
   IF Gestalt(popGestaltSelector, LongInt(pgh))=noErr THEN
      PopGestaltPointer := pgh^
   ELSE
      PopGestaltPointer := NIL {PopChar Pro not installed}
END;

BEGIN {startup code of your application}
   InitializeToolbox;
   popPtr := PopGestaltPointer;
   ...

PopGestaltPointer returns the address of the gestalt record if PopChar Pro is installed. If PopChar Pro is not installed, NIL is returned.

You can safely call PopGestaltPointer in the startup code of your application and store the returned value in a global variable for future use. The gestalt handle (pgh) is locked; the gestalt record will thus not move in memory.

6. Specifying the Current Font

When the General Guidelines and the hints in the Black List don't work for you, your application can explicitly tell PopChar Pro which font to use. Here is how you can achieve that.

When your application detects a click in the menu bar, it should first update its menus, then determine in which font the character generated by the next keystroke will appear, and finally tell PopChar Pro to use that font before calling MenuSelect. The following program fragment shows a typical sequence for specifying the font to be used by PopChar Pro:

PROCEDURE DoMouseDown (theEvent: EventRecord);
VAR
   theWindow: WindowPtr;
   global: Point;
   wLoc: Integer;
   menuResult: LongInt;
BEGIN
   global := theEvent.where;
   wLoc := FindWindow(global, theWindow);
   CASE wLoc OF
      inMenuBar: BEGIN
         UpdateMenuItems;
         SetPopCharFont;
         menuResult := MenuSelect(global);
         IF HiWord(menuResult)<>0 THEN
            HandleMenu(menuResult);
      END;
      ...
      ..

The procedure SetPopCharFont can be implemented in the following way:

PROCEDURE SetPopCharFont;
VAR
   currentFont: Integer;
BEGIN
   IF popPtr<>NIL THEN BEGIN {only if PopChar Pro is installed}
      currentFont := FontUsedForNextKeyStroke;
      popPtr^.fontToUse := currentFont
   END
END;

Important:

  • A single call of SetPopCharFont affects only a single invocation of MenuSelect. You must call the procedure again before you call MenuSelect the next time.
  • Balance every invocation of SetPopCharFont with a call to MenuSelect. If you set the fontToUse field without calling MenuSelect, the setting will remain stuck until MenuSelect is invoked the next time (which might happen from another application after a context switch).
  • To specify the current font for a modal dialog, you will need to write a filter procedure that calls SetPopCharFont and MenuSelect when the user clicks in the menu bar.

7. The PopChar Pro Floater

When PopChar Pro's floating window is open, the window should also update to font changes even when the user does not pull down a menu from the menu bar. To achieve that, the PopChar Pro Floater application generates fake clicks in the menu bar twice a second as long as the floating window is open and no other user events occured during the past 0.5 sec. Your application will receive these clicks automatically when it calls GetNextEvent or WaitNextEvent. These fake clicks cannot be distinguished from real clicks. Your application will process them as usual and update the check marks in its menus (or call SetPopCharFont) before calling MenuSelect. The call to MenuSelect will have no visual effect; it lets PopChar Pro recognize the current font and immediately returns with the function result 0 as if the user had released the mouse button outside the menu.

If PopChar Pro's floating window is open, update events can occur when the floating window is moved, collapsed, closed, or when the window's size changes as the result of a font change. Your application should therefore process update events whenever possible. Your application will handle such update events anyway when it goes through its main event loop. However, when a modal dialog is open, other windows may appear partially blank when the floating window goes away. You can avoid such "white spaces" by writing a filter procedure for all modal dialogs. If the filter procedure detects an update event that belongs to an open window of your application, it should call an update procedure that redraws the window's content between calls of BeginUpdate and EndUpdate.

8. The Black List

This section lists a couple of absolutely forbidden things that should not happen in a "PopChar-Pro-aware" application.

  • Don't cripple the Font menu. Use a regular Font menu and fill it with a call to AppendResMenu. Don't be clever with the menu items; in particular, don't modify the font names. Otherwise, PopChar Pro will be unable to recognize them.
  • Don't modify PopChar Pro's gestalt record (except for the fontToUse field as described above). PopChar Pro will strike back terribly if you do that.
  • Don't make assumptions about undocumented fields in PopChar Pro's gestalt record, even if you figure out what they mean. All items beyond fontToUse are subject to changes.
  • Don't HUnlock PopChar Pro's gestalt handle. PopChar Pro locks the handle once at startup and assumes that no evil creature will undo that.
  • Don't assume the presence of PopChar Pro. There are still Mac users out there who don't know what they are missing. Before you attempt to access PopChar Pro's gestalt record, check if the gestalt pointer is NIL (see the SetPopCharFont procedure above).
  • Don't mess around with PopChar Pro. PopChar Pro checks itself against modifications. It will detect virus attacks and all unauthorized changes.
  • Don't open the PopChar Pro Prefs file. PopChar Pro opens its preferences files whenever it needs to. The prefs file is closed again as soon as PopChar Pro has read or written preferences. If you open the prefs file, PopChar Pro will be unable to retrieve or store its preferences and will start to behave strangely.


webmaster

site map © ergonis software