home *** CD-ROM | disk | FTP | other *** search
Text File | 2002-01-01 | 6.0 KB | 223 lines | [TEXT/MPS ] |
- This is a collection of all the Key Script routines
- from KeyScript Sample Code.
-
- {
- The following Key Script routines are for Script Manager friendlyness.
- They are only active on non-Roman Script Systems.
-
- The objective is to synchronize the keyboard script (input) with the font script (output).
- For the Roman script this is easy - the key script is always Roman. But for other scripts,
- like Kanji, it can be either Roman or Japanese. The user normally selects the key
- script by clicking the script icon or tying command-space. The problem is that the user
- typically mixes Roman and Japanese characters together, and he is responsible for
- switching key scripts himself.
-
- Most applications do no special handling and this leads to garbage when a non-roman
- key script is used with a roman font. Other applications (ie HyperCard) try to prevent
- this by forcing the key script to the font script. This makes it very difficult to
- enter roman text when using a foreign script font - you have to keep resetting the key
- script.
-
- These routines use a combination of setting the script by context and by the previous
- state when there is no context. The result is that the user seldom has to set the key
- script and the application seems intelligent.
-
- An assumption is that there is a maximum of two scripts installed into the system
- (including Roman).
-
- Use find 'keyscript' to locate changes.
-
- These routines were written by Joel Cannon, Sept 14, 1989
- Apple Japan, Developer Technical Support
- }
-
- VAR
- scriptsInstalled : INTEGER;
- gKeyScriptState : INTEGER;
-
- {$S Main}
- { gets the state for the non-roman key script. (roman is always roman)}
- FUNCTION GetKeyScriptState : INTEGER;
- BEGIN
- GetKeyScriptState := gKeyScriptState;
- END;
-
-
- {$S Main}
- { update the non-roman key script state. }
- { the state is determined by context and user selection. }
- PROCEDURE SetKeyScriptState(txFont : INTEGER);
- BEGIN
- if scriptsInstalled > 1 then begin
- if Font2Script(txFont) <> smRoman then
- gKeyScriptState := GetEnvirons(smKeyScript);
- end;
- END;
-
-
- {$S Main}
- { calculates and sets the keyboard input script.}
- { priority:
- CONTEXT - [A] selection(from beginning), [B] paragraph (before/after)
- STATE - [C] fontscript, [D] last state, [E] system script. }
- PROCEDURE SetKeyScript(hTE : TEHandle);
- VAR
- determined : BOOLEAN;
- newScript : INTEGER;
- i : INTEGER;
- crFound : BOOLEAN;
-
- { disregard punctuation, numbers & symbols (single and double byte),
- detect CR (paragraph markers) and set newscript to the fontscript}
- PROCEDURE GetNewScript;
- VAR
- cType : INTEGER;
- BEGIN
- IF CharByte(hTE^^.hText^, i) <> smFirstByte THEN BEGIN
- cType:= CharType(hTE^^.hText^, i);
- CASE BitAnd(cType, smcTypeMask) OF
- smCharPunct : crFound := (CharsHandle(hTE^^.hText)^^[i] = chr(13));{drop out}
- smCharAscii : BEGIN
- determined := TRUE;
- newScript := smRoman;
- END;
- OTHERWISE BEGIN
- determined := TRUE;
- newScript := Font2Script(hTE^^.TxFont);
- END;
- END; {case}
- END;
- END;
-
- BEGIN
- IF scriptsInstalled > 1 THEN BEGIN
- determined := FALSE;
-
- {Search inside selection}
- i := hTE^^.SelStart;
- WHILE (i < hTE^^.selEnd) AND NOT determined DO BEGIN
- GetNewScript;
- i := i + 1;
- END;
-
- {Search to beginning of paragraph}
- i := hTE^^.SelStart-1;
- crFound := FALSE;
- WHILE (i >= 0) AND (NOT determined) AND (NOT crFound) DO BEGIN
- GetNewScript;
- i := i - 1;
- END;
-
- {Search to end of paragraph}
- i := hTE^^.SelEnd;
- {** special case - selecting CR includes next paragraph - no visual indication}
- IF (hTE^^.SelStart < hTE^^.SelEnd) then
- i := i-1;
- crFound := FALSE;
- WHILE (i < hTE^^.teLength) AND (NOT determined) AND (NOT crFound) DO BEGIN
- GetNewScript;
- i := i + 1;
- END;
-
- { when no text in paragraph, use fontscript then state}
- IF NOT determined THEN BEGIN
- IF Font2Script(hTE^^.txFont) = smRoman THEN
- newScript := smRoman
- ELSE
- newScript := GetKeyScriptState;
- END;
-
- { set the new keyscript and update the state}
- IF GetEnvirons(smKeyScript) <> newScript THEN BEGIN
- KeyScript(newScript);
- SetKeyScriptState(hTE^^.txFont);
- END;
- END;
- END; {SetKeyScript}
-
-
-
- {$S Main}
- { update the key script state }
- FUNCTION KeyScriptFilter(theDialog : DialogPtr; VAR theEvent : EventRecord;
- VAR itemHit : INTEGER) : Boolean;
- BEGIN
- SetKeyScriptState(DialogPeek(theDialog)^.textH^^.txFont);
- {
- setkeyscript cannot be called within the filter because
- it needs to be called AFTER the event has been handled.
- }
- KeyScriptFilter := FALSE;
- END;
-
-
- {$S Main}
- { Display the dialog box in response to the 'About TubeTest' menu item. }
- PROCEDURE ShowAboutMeDialog;
- Const
- DlgID = 1000;
- OK = 1;
- Text = 2;
- VAR
- theDialog: DialogPtr;
- itemHit: Integer;
- hTE : TEHandle;
- itemtype : Integer;
- box : Rect;
- BEGIN
- theDialog := GetNewDialog(DlgID, NIL, WindowPtr( - 1));
-
- { test for two different fonts }
- if button then DialogPeek(theDialog)^.textH^^.txFont := Monaco;
- SetKeyScript(DialogPeek(theDialog)^.textH);
-
- repeat
- ModalDialog(@KeyScriptFilter, itemHit);
- until itemHit = OK;
- DisposDialog(theDialog);
- END; { ShowAboutMeDialog }
-
-
- {$S Initialize}
- PROCEDURE InitKeyScript(VAR scriptsInstalled : INTEGER);
- CONST
- UnimplCoreRoutine = $9F;
- ScriptUtil = $BF;
- BEGIN
- {find out if we can use the Script Manager }
- scriptsInstalled := 0;
- IF GetTrapAddress(UnimplCoreRoutine) <> GetTrapAddress(ScriptUtil) THEN BEGIN
- scriptsInstalled := GetEnvirons(smEnabled);
- SetKeyScriptState(SystemFont);
- END;
- END;
-
-
- {Integration into application}
-
- Init
- InitKeyScript(scriptsInstalled);
-
- DoKey
- TEKey(key, te);
- { check for arrow keys }
- if ord(key) in [28,29,30,31] then SetKeyScript(te);
-
- DoContent
- TEClick(mouse, shiftDown, DocumentPeek(window)^.docTE);
- SetKeyScript(DocumentPeek(window)^.docTE); { JWC }
-
- DoActivate
- TEActivate(docTE); {let TE do its thing}
- SetKeyScript(docTE); { JWC }
-
- DoMenu
- SetFont(---);
- SetKeyScript(DocumentPeek(window)^.docTE); { JWC }
-
-
- DoIdle
- TEIdle(DocumentPeek(window)^.docTE);
- { user key script change events get trapped by system so check here}
- SetKeyScriptState(DocumentPeek(window)^.docTE^^.txFont);
-