#ifndef _c_Text_Navigation_Attachment_h #define _c_Text_Navigation_Attachment_h #include #include // =========================================================================== // cTextNavigationAttachment.cpp Version 1.2 ©1998 Joakim Braun All rights reserved. // =========================================================================== // // An highly customizable attachment that works with LEditText, // LTextEditView and LEditField and implements keyboard navigation within text fields. // The following navigations are included: // // * Go to previous word: Positions cursor at start of previous word. // * Go to next word: Positions cursor at start of next word. // * Select previous word: Extends selection backwards to start of previous word, // as defined by Go to previous word routine. // * Select next word: Extends selection forward to end of next word, // as defined by Go to next word routine. // * Delete previous word: Deletes previous word // * Delete next word: Deletes next word // * Select to start: Extends selection backwards to start of text. // * Select to end: Extends selection forward to end of text. // * Select current line: Selects entire line of text where the cursor // (more specifically, the selStart field of the TERecord) is. // * Smart delete: Normalizes spaces when deleting (no more than one space character, // whatever the context of the selection deleted). // // The key combinations that trigger the attachment may either be set in Constructor // (for each attachment), or saved in a single 'tNav' resource (Resorcerer TMPL is included). // In the last case, identical behavior is ensured for all cTextNavigationAttachments // in the entire application. Any of the routines may also be disabled. // The attachment calls FindWordBreaks() to determine where words start and end. // If you don't like the way it works, go complain to Apple - or write your own subclass. // Note also that several bottleneck routines are public and static, // and so may be used without messing around with attachments. // Templates for Constructor and Resorcerer are included. // cTextNavigationAttachment is free for any and all use. // Do not distribute modified source code under my name. // No support promised, no liability accepted. Provided "as is". // That said, I can be reached at braun@swipnet.se. // (Im new at Power Plant. If anyone knows how to implement this at the LWindow level // instead of at the individual edit field level, please tell me...) // Change history: // 1.0 April 5th, 1998 First release // // 1.1 April 7th, 1998 FindNextWordBreak() now advances to beginning of next word, // not end of current word. Slightly less annoying. // // When deleting words or doing SmartDelete (now renamed SelectSmartDelete()), // we don't call TEDelete() ourselves, since that messes up undo of TEActions. // Instead, change selection accordingly and put a kBackspaceCharCode // into the event record for the text object to process. // // 1.2 August 30, 1998 Removed LTextEdit class (now obsolete) // Added call to SetUpdateCommandStatus() when selection changed class cTextNavigationAttachment : public LAttachment{ public: enum { class_ID = 'txtA' }; cTextNavigationAttachment(MessageT inMessage = msg_AnyMessage, Boolean inExecuteHost = true); cTextNavigationAttachment(LStream *inStream); static void GoToPreviousWord(TEHandle macTEH); static void GoToNextWord(TEHandle macTEH); static void SelectPreviousWord(TEHandle macTEH); static void SelectNextWord(TEHandle macTEH); static void DeletePreviousWord(TEHandle macTEH); static void DeleteNextWord(TEHandle macTEH); static void SelectToStart(TEHandle macTEH); static void SelectToEnd(TEHandle macTEH); static void SelectCurrentLine(TEHandle macTEH); static void SelectSmartDelete(TEHandle macTEH); static void SelectSmartDelete(char* textStart, Uint16 textLength, Uint16& selStartIO, Uint16& selEndIO); static void GetPrecedingWordBreak(TEHandle macTEH, Uint16& selStartIO); static void GetNextWordBreak(TEHandle macTEH, Uint16& selEndIO); static void GetPrecedingWordBreak(char* textStart, Uint16 textLength, Uint16& selStartIO); static void GetNextWordBreak(char* textStart, Uint16 textLength, Uint16& selStartIO); protected: enum { eUseConstructorValues = 0, eUseResourceValues, eTextNavigationAttachmentResType = 'tNav', eNotOurKey = 0, eGotoPreviousWordKey, eGotoNextWordKey, eSelectPreviousWordKey, eSelectNextWordKey, eDeletePreviousWordKey, eDeleteNextWordKey, eSelectToStartKey, eSelectToEndKey, eSelectCurrentLineKey, eSmartDeleteKey }; Int16 mCmdCharPreviousWord, mCmdCharNextWord, mCmdCharSelectPreviousWord, mCmdCharSelectNextWord, mCmdCharDeletePreviousWord, mCmdCharDeleteNextWord, mCmdCharSelectToStart, mCmdCharSelectToEnd, mCmdCharSelectCurrentLine, mCmdCharSmartDelete; EventModifiers mCmdKeysPreviousWord, mCmdKeysNextWord, mCmdKeysSelectPreviousWord, mCmdKeysSelectNextWord, mCmdKeysDeletePreviousWord, mCmdKeysDeleteNextWord, mCmdKeysSelectToStart, mCmdKeysSelectToEnd, mCmdKeysSelectCurrentLine, mCmdKeysSmartDelete; virtual void ExecuteSelf(MessageT inMessage, void *ioParam); void InitFromResource(Int16 tNavResID); void InitFromStream(LStream *inStream); virtual void DefaultInit(void); virtual Uint16 IsWhatKey(Int16 key, EventModifiers mods); virtual TEHandle GetHostMacTEH(void); }; #endif