home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OWLINC.PAK / EDIT.H < prev    next >
C/C++ Source or Header  |  1995-08-29  |  10KB  |  325 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1991, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Definition of class TEdit.  This defines the basic behavior
  6. //   of all edit controls.
  7. //----------------------------------------------------------------------------
  8. #if !defined(OWL_EDIT_H)
  9. #define OWL_EDIT_H
  10.  
  11. #if !defined(OWL_STATIC_H)
  12. # include <owl/static.h>
  13. #endif
  14. #include <owl/edit.rh>
  15.  
  16. class _OWLCLASS TValidator;
  17.  
  18. //
  19. //  class TEdit
  20. //  ----- -----
  21. //
  22. class _OWLCLASS TEdit : public TStatic {
  23.   public:
  24.     TEdit(TWindow*        parent,
  25.           int             id,
  26.           const char far* text,
  27.           int x, int y, int w, int h,
  28.           uint            textLen = 0,
  29.           bool            multiline = false,
  30.           TModule*        module = 0);
  31.  
  32.     TEdit(TWindow*   parent,
  33.           int        resourceId,
  34.           uint       textLen = 0,
  35.           TModule*   module = 0);
  36.  
  37.    ~TEdit();
  38.  
  39.     //
  40.     // Accessing
  41.     //
  42.     int     GetNumLines() const;
  43.     int     GetLineLength(int lineNumber) const;
  44.     bool    GetLine(char far* str, int strSize, int lineNumber) const;
  45.     void    GetSubText(char far* str, uint startPos, uint endPos) const;
  46.     void    GetSelection(uint& startPos, uint& endPos) const;
  47.  
  48.     bool    IsModified() const;
  49.     void    ClearModify() {HandleMessage(EM_SETMODIFY);}
  50.  
  51.     int     GetLineFromPos(uint charPos) const;
  52.     uint    GetLineIndex(int lineNumber) const;
  53.  
  54.     uint    Transfer(void* buffer, TTransferDirection direction);
  55.  
  56.     //
  57.     // Lock and unlock this edit control's buffer. Allows direct access to the
  58.     // text in the edit control.
  59.     //
  60.     char far* LockBuffer(uint newSize = 0);
  61.     void    UnlockBuffer(const char far* buffer, bool updateHandle = false);
  62.  
  63.     //
  64.     // operations
  65.     //
  66.     bool    DeleteSubText(uint startPos, uint endPos);
  67.     bool    DeleteLine(int lineNumber);
  68.     bool    DeleteSelection();
  69.     bool    SetSelection(uint startPos, uint endPos);
  70.  
  71.     void    Scroll(int horizontalUnit, int verticalUnit);
  72.     void    Insert(const char far* str);
  73.     int     Search(uint startPos, const char far* text,
  74.                    bool caseSensitive=false, bool wholeWord=false,
  75.                    bool up=false);
  76.  
  77.     void    GetRect(TRect& frmtRect) const;
  78.     void    SetRect(const TRect& frmtRect);
  79.     void    SetRectNP(const TRect& frmtRect);
  80.     void    FormatLines(bool addEOL);
  81.     void    SetTabStops(int numTabs, const int far* tabs);
  82.  
  83.     HLOCAL  GetHandle() const;
  84.     void    SetHandle(HLOCAL localMem);
  85.  
  86.     void    SetPasswordChar(uint ch) {HandleMessage(EM_SETPASSWORDCHAR, ch);}
  87.  
  88.     int     GetFirstVisibleLine() const;
  89.     void    SetReadOnly(bool readOnly);
  90.     uint    GetPasswordChar() const;
  91.  
  92.     EDITWORDBREAKPROC GetWordBreakProc() const;
  93.     void    SetWordBreakProc(EDITWORDBREAKPROC proc);
  94.  
  95.     //
  96.     // clipboard operations
  97.     //
  98.     bool    CanUndo() const;
  99.     void    EmptyUndoBuffer() {HandleMessage(EM_EMPTYUNDOBUFFER);}
  100.     void    Undo() {HandleMessage(WM_UNDO);}
  101.     void    Paste() {HandleMessage(WM_PASTE);}
  102.     void    Copy() {HandleMessage(WM_COPY);}
  103.     void    Cut() {HandleMessage(WM_CUT);}
  104.     
  105.     bool    IsValid(bool reportErr = false);
  106.     void    SetValidator(TValidator* validator);
  107.     void    ValidatorError();
  108.  
  109.     //
  110.     // Override TStatic virtual member functions
  111.     //
  112.     void Clear();
  113.  
  114.   protected:
  115.     //
  116.     // command response functions
  117.     //
  118.     void    CmEditCut() {Cut();}                  // CM_EDITCUT
  119.     void    CmEditCopy() {Copy();}                // CM_EDITCOPY
  120.     void    CmEditPaste() {Paste();}              // CM_EDITPASTE
  121.     void    CmEditDelete() {DeleteSelection();}   // CM_EDITDELETE
  122.     void    CmEditClear() {Clear();}              // CM_EDITCLEAR
  123.     void    CmEditUndo() {Undo();}                // CM_EDITUNDO
  124.  
  125.     //
  126.     // command enabler functions
  127.     //
  128.     void CmSelectEnable(TCommandEnabler& commandHandler);
  129.     void CmPasteEnable(TCommandEnabler& commandHandler);
  130.     void CmCharsEnable(TCommandEnabler& commandHandler);
  131.     void CmModEnable(TCommandEnabler& commandHandler);
  132.  
  133.     //
  134.     // child id notification handled at the child
  135.     //
  136.     void    ENErrSpace();  // EN_ERRSPACE
  137.  
  138.     //
  139.     // Override TWindow virtual member functions
  140.     //
  141.     char far* GetClassName();
  142.     void      SetupWindow();
  143.  
  144.     //
  145.     // Input validation object
  146.     //
  147.     TValidator*  Validator;
  148.  
  149.     void         EvChar(uint key, uint repeatCount, uint flags);
  150.     void         EvKeyDown(uint key, uint repeatCount, uint flags);
  151.     uint         EvGetDlgCode(MSG far*);
  152.     void         EvSetFocus(HWND hWndLostFocus);
  153.     void         EvKillFocus(HWND hWndGetFocus);
  154.     bool         CanClose();
  155.  
  156.     //
  157.     // handler for input validation message sent by parent
  158.     //
  159.     void         EvChildInvalid(HWND);
  160.  
  161.   private:
  162.     //
  163.     // hidden to prevent accidental copying or assignment
  164.     //
  165.     TEdit(const TEdit&);
  166.     TEdit& operator =(const TEdit&);
  167.  
  168.  
  169.     // Used to prevent 'oscillation' when a validated window with invalid
  170.     // input is losing focus to another validated window with invalid input
  171.     // Without this flag, the two windows will fight for focus
  172.     static TEdit* ValidatorReFocus;
  173.  
  174.   DECLARE_RESPONSE_TABLE(TEdit);
  175.   DECLARE_STREAMABLE(_OWLCLASS, TEdit, 1);
  176. };
  177.  
  178. //
  179. // edit control notification macros. methods are: void method()
  180. //
  181. // EV_EN_CHANGE(id, method)
  182. // EV_EN_ERRSPACE(id, method)
  183. // EV_EN_HSCROLL(id, method)
  184. // EV_EN_KILLFOCUS(id, method)
  185. // EV_EN_MAXTEXT(id, method)
  186. // EV_EN_SETFOCUS(id, method)
  187. // EV_EN_UPDATE(id, method)
  188. // EV_EN_VSCROLL(id, method)
  189.  
  190. //----------------------------------------------------------------------------
  191. // Inlines
  192. //----------------------------------------------------------------------------
  193.  
  194. //
  195. // returns the number of lines in the associated edit control
  196. //
  197. // note that GetNumLines returns 1 when the edit control has no text (i.e.
  198. // it has one line with no text in it). Returns zero if an error occurs
  199. //
  200. inline int TEdit::GetNumLines() const {
  201.   return (int)CONST_CAST(TEdit*,this)->HandleMessage(EM_GETLINECOUNT);
  202. }
  203.  
  204. inline bool TEdit::IsModified() const {
  205.   return (bool)CONST_CAST(TEdit*,this)->HandleMessage(EM_GETMODIFY);
  206. }
  207.  
  208. //
  209. // selects the characters in the range "startPos .. endPos"
  210. //
  211. inline bool TEdit::SetSelection(uint startPos, uint endPos) {
  212. #if defined(BI_PLAT_WIN32)
  213.   return HandleMessage(EM_SETSEL, startPos, endPos) != 0;
  214. #else
  215.   return HandleMessage(EM_SETSEL, 0, MAKELPARAM(startPos, endPos)) != 0;
  216. #endif
  217. }
  218.  
  219. // returns the starting and ending positions of the selected text
  220. //
  221. inline void TEdit::GetSelection(uint& startPos, uint& endPos) const {
  222. #if defined(BI_PLAT_WIN32)
  223.   CONST_CAST(TEdit*,this)->HandleMessage(EM_GETSEL, (WPARAM)&startPos, (LPARAM)&endPos);
  224. #else
  225.   LRESULT  retValue = CONST_CAST(TEdit*,this)->HandleMessage(EM_GETSEL);
  226.   startPos = LOWORD(retValue);
  227.   endPos   = HIWORD(retValue);
  228. #endif
  229. }
  230.  
  231. //
  232. // returns the line number associated with character index "CharPos"
  233. //
  234. // if "CharPos" is greater than the number of characters, the last line is
  235. // returned
  236. // if "CharPos" is -1, the line containing the beginning of the selection (or
  237. // the line containing the caret if no selection) is returned
  238. //
  239. inline int TEdit::GetLineFromPos(uint charPos) const {
  240.   return (int)CONST_CAST(TEdit*,this)->HandleMessage(EM_LINEFROMCHAR, charPos);
  241. }
  242.  
  243. //
  244. // returns the character index of line number "LineNumber" or -1 if
  245. // "LineNumber" is greater than the number of lines
  246. //
  247. // if "LineNumber" is -1, the line containing the caret is used
  248. //
  249. inline uint TEdit::GetLineIndex(int lineNumber) const {
  250.   return (int)CONST_CAST(TEdit*,this)->HandleMessage(EM_LINEINDEX, lineNumber);
  251. }
  252.  
  253. //
  254. // scrolls the text by the specified horizontal and vertical amounts
  255. //
  256. inline void TEdit::Scroll(int horizUnit, int vertUnit) {
  257. #if defined(BI_PLAT_WIN32)
  258.   HandleMessage(EM_LINESCROLL, horizUnit, vertUnit);
  259. #else
  260.   HandleMessage(EM_LINESCROLL, 0, MAKELONG(vertUnit, horizUnit));
  261. #endif
  262. }
  263.  
  264. //
  265. // sets the selection to the "str" (does a "paste" type of action
  266. // without affecting the clipboard)
  267. //
  268. inline void TEdit::Insert(const char far* str) {
  269.   HandleMessage(EM_REPLACESEL, 0, (LPARAM)str);
  270. }
  271.  
  272. inline void TEdit::GetRect(TRect& frmtRect) const {
  273.   CONST_CAST(TEdit*,this)->HandleMessage(EM_GETRECT, 0, (LPARAM)(TRect FAR*)&frmtRect);
  274. }
  275.  
  276. inline void TEdit::SetRect(const TRect& frmtRect) {
  277.   HandleMessage(EM_SETRECT, 0, (LPARAM)(const TRect FAR*)&frmtRect);
  278. }
  279.  
  280. inline void TEdit::SetRectNP(const TRect& frmtRect) {
  281.   HandleMessage(EM_SETRECTNP, 0, (LPARAM)(const TRect FAR*)&frmtRect);
  282. }
  283.  
  284. inline void TEdit::FormatLines(bool addEOL) {
  285.   HandleMessage(EM_FMTLINES, addEOL);
  286. }
  287.  
  288. inline void TEdit::SetTabStops(int numTabs, const int far* tabs) {
  289.   HandleMessage(EM_SETTABSTOPS, numTabs, (LPARAM)tabs);
  290. }
  291.  
  292. inline HLOCAL TEdit::GetHandle() const {
  293.   return (HLOCAL)CONST_CAST(TEdit*,this)->HandleMessage(EM_GETHANDLE);
  294. }
  295.  
  296. inline void TEdit::SetHandle(HLOCAL localMem) {
  297.   HandleMessage(EM_SETHANDLE, (WPARAM)localMem);
  298. }
  299.  
  300. inline int TEdit::GetFirstVisibleLine() const {
  301.   return (int)CONST_CAST(TEdit*,this)->HandleMessage(EM_GETFIRSTVISIBLELINE);
  302. }
  303.  
  304. inline void TEdit::SetReadOnly(bool readOnly) {
  305.   HandleMessage(EM_SETREADONLY, readOnly);
  306. }
  307.  
  308. inline uint TEdit::GetPasswordChar() const {
  309.   return (uint)CONST_CAST(TEdit*,this)->HandleMessage(EM_GETPASSWORDCHAR);
  310. }
  311.  
  312. inline EDITWORDBREAKPROC TEdit::GetWordBreakProc() const {
  313.   return (EDITWORDBREAKPROC)CONST_CAST(TEdit*,this)->HandleMessage(EM_GETWORDBREAKPROC);
  314. }
  315.  
  316. inline void TEdit::SetWordBreakProc(EDITWORDBREAKPROC proc) {
  317.   HandleMessage(EM_SETWORDBREAKPROC, 0, (LPARAM)proc);
  318. }
  319.  
  320. inline bool TEdit::CanUndo() const {
  321.   return (bool)CONST_CAST(TEdit*,this)->HandleMessage(EM_CANUNDO);
  322. }
  323.  
  324. #endif  // OWL_EDIT_H
  325.