lllineeditor.h

Go to the documentation of this file.
00001 
00032 // Text editor widget to let users enter/edit a single line.
00033 //
00034 //
00035 // Features: 
00036 //              Text entry of a single line (text, delete, left and right arrow, insert, return).
00037 //              Callbacks either on every keystroke or just on the return key.
00038 //              Focus (allow multiple text entry widgets)
00039 //              Clipboard (cut, copy, and paste)
00040 //              Horizontal scrolling to allow strings longer than widget size allows 
00041 //              Pre-validation (limit which keys can be used)
00042 //              Optional line history so previous entries can be recalled by CTRL UP/DOWN
00043 
00044 
00045 #ifndef LL_LLLINEEDITOR_H
00046 #define LL_LLLINEEDITOR_H
00047 
00048 #include "v4color.h"
00049 #include "llframetimer.h"
00050 
00051 #include "lleditmenuhandler.h"
00052 #include "lluictrl.h"
00053 #include "lluistring.h"
00054 #include "llviewborder.h"
00055 
00056 class LLFontGL;
00057 class LLLineEditorRollback;
00058 class LLButton;
00059 
00060 typedef BOOL (*LLLinePrevalidateFunc)(const LLWString &wstr);
00061 
00062 // 
00063 // Classes
00064 //
00065 class LLLineEditor
00066 : public LLUICtrl, public LLEditMenuHandler
00067 {
00068         friend class LLLineEditorRollback;
00069 
00070 public:
00071         LLLineEditor(const LLString& name, 
00072                                  const LLRect& rect,
00073                                  const LLString& default_text = LLString::null,
00074                                  const LLFontGL* glfont = NULL,
00075                                  S32 max_length_bytes = 254,
00076                                  void (*commit_callback)(LLUICtrl* caller, void* user_data) = NULL,
00077                                  void (*keystroke_callback)(LLLineEditor* caller, void* user_data) = NULL,
00078                                  void (*focus_lost_callback)(LLUICtrl* caller, void* user_data) = NULL,
00079                                  void* userdata = NULL,
00080                                  LLLinePrevalidateFunc prevalidate_func = NULL,
00081                                  LLViewBorder::EBevel border_bevel = LLViewBorder::BEVEL_IN,
00082                                  LLViewBorder::EStyle border_style = LLViewBorder::STYLE_LINE,
00083                                  S32 border_thickness = 1);
00084 
00085         virtual ~LLLineEditor();
00086         virtual EWidgetType getWidgetType() const;
00087         virtual LLString getWidgetTag() const;
00088         virtual LLXMLNodePtr getXML(bool save_children = true) const;
00089         void setColorParameters(LLXMLNodePtr node);
00090         static LLView* fromXML(LLXMLNodePtr node, LLView *parent, LLUICtrlFactory *factory);
00091 
00092         // mousehandler overrides
00093         /*virtual*/ BOOL        handleMouseDown(S32 x, S32 y, MASK mask);
00094         /*virtual*/ BOOL        handleMouseUp(S32 x, S32 y, MASK mask);
00095         /*virtual*/ BOOL        handleHover(S32 x, S32 y, MASK mask);
00096         /*virtual*/ BOOL        handleDoubleClick(S32 x,S32 y,MASK mask);
00097         /*virtual*/ BOOL        handleKeyHere(KEY key, MASK mask, BOOL called_from_parent );
00098         /*virtual*/ BOOL        handleUnicodeCharHere(llwchar uni_char, BOOL called_from_parent);
00099         /*virtual*/ void        onMouseCaptureLost();
00100 
00101         // LLEditMenuHandler overrides
00102         virtual void    cut();
00103         virtual BOOL    canCut();
00104 
00105         virtual void    copy();
00106         virtual BOOL    canCopy();
00107 
00108         virtual void    paste();
00109         virtual BOOL    canPaste();
00110         
00111         virtual void    doDelete();
00112         virtual BOOL    canDoDelete();
00113 
00114         virtual void    selectAll();
00115         virtual BOOL    canSelectAll();
00116 
00117         virtual void    deselect();
00118         virtual BOOL    canDeselect();
00119 
00120         // view overrides
00121         virtual void    draw();
00122         virtual void    reshape(S32 width,S32 height,BOOL called_from_parent=TRUE);
00123         virtual void    onFocusLost();
00124         virtual void    setEnabled(BOOL enabled);
00125 
00126         // UI control overrides
00127         virtual void    clear();
00128         virtual void    onTabInto();
00129         virtual void    setFocus( BOOL b );
00130         virtual void    setRect(const LLRect& rect);
00131         virtual BOOL    acceptsTextInput() const;
00132         virtual void    onCommit();
00133         virtual BOOL    isDirty() const;        // Returns TRUE if the user has changed value at all
00134         virtual void    resetDirty();           // Clear dirty state
00135 
00136         // assumes UTF8 text
00137         virtual void    setValue(const LLSD& value );
00138         virtual LLSD    getValue() const;
00139         virtual BOOL    setTextArg( const LLString& key, const LLStringExplicit& text );
00140         virtual BOOL    setLabelArg( const LLString& key, const LLStringExplicit& text );
00141 
00142         void                    setLabel(const LLStringExplicit &new_label);
00143         void                    setText(const LLStringExplicit &new_text);
00144 
00145         const LLString& getText() const         { return mText.getString(); }
00146         const LLWString& getWText() const       { return mText.getWString(); }
00147         S32                             getLength() const       { return mText.length(); }
00148 
00149         S32                             getCursor()             { return mCursorPos; }
00150         void                    setCursor( S32 pos );
00151         void                    setCursorToEnd();
00152 
00153         // Selects characters 'start' to 'end'.
00154         void                    setSelection(S32 start, S32 end);
00155         
00156         void                    setCommitOnFocusLost( BOOL b )  { mCommitOnFocusLost = b; }
00157         void                    setRevertOnEsc( BOOL b )                { mRevertOnEsc = b; }
00158 
00159         void setCursorColor(const LLColor4& c)                  { mCursorColor = c; }
00160         const LLColor4& getCursorColor() const                  { return mCursorColor; }
00161 
00162         void setFgColor( const LLColor4& c )                    { mFgColor = c; }
00163         void setReadOnlyFgColor( const LLColor4& c )    { mReadOnlyFgColor = c; }
00164         void setTentativeFgColor(const LLColor4& c)             { mTentativeFgColor = c; }
00165         void setWriteableBgColor( const LLColor4& c )   { mWriteableBgColor = c; }
00166         void setReadOnlyBgColor( const LLColor4& c )    { mReadOnlyBgColor = c; }
00167         void setFocusBgColor(const LLColor4& c)                 { mFocusBgColor = c; }
00168 
00169         const LLColor4& getFgColor() const                      { return mFgColor; }
00170         const LLColor4& getReadOnlyFgColor() const      { return mReadOnlyFgColor; }
00171         const LLColor4& getTentativeFgColor() const { return mTentativeFgColor; }
00172         const LLColor4& getWriteableBgColor() const     { return mWriteableBgColor; }
00173         const LLColor4& getReadOnlyBgColor() const      { return mReadOnlyBgColor; }
00174         const LLColor4& getFocusBgColor() const         { return mFocusBgColor; }
00175 
00176         void                    setIgnoreArrowKeys(BOOL b)              { mIgnoreArrowKeys = b; }
00177         void                    setIgnoreTab(BOOL b)                    { mIgnoreTab = b; }
00178         void                    setPassDelete(BOOL b)                   { mPassDelete = b; }
00179 
00180         void                    setDrawAsterixes(BOOL b)                { mDrawAsterixes = b; }
00181 
00182         // get the cursor position of the beginning/end of the prev/next word in the text
00183         S32                             prevWordPos(S32 cursorPos) const;
00184         S32                             nextWordPos(S32 cursorPos) const;
00185 
00186         BOOL                    hasSelection() { return (mSelectionStart != mSelectionEnd); }
00187         void                    startSelection();
00188         void                    endSelection();
00189         void                    extendSelection(S32 new_cursor_pos);
00190         void                    deleteSelection();
00191 
00192         void                    setHandleEditKeysDirectly( BOOL b ) { mHandleEditKeysDirectly = b; }
00193         void                    setSelectAllonFocusReceived(BOOL b);
00194 
00195         void                    setKeystrokeCallback(void (*keystroke_callback)(LLLineEditor* caller, void* user_data));
00196 
00197         void                    setMaxTextLength(S32 max_text_length); 
00198         void                    setBorderWidth(S32 left, S32 right);
00199 
00200         static BOOL             isPartOfWord(llwchar c);
00201         // Prevalidation controls which keystrokes can affect the editor
00202         void                    setPrevalidate( BOOL (*func)(const LLWString &) ) { mPrevalidateFunc = func; }
00203         static BOOL             prevalidateFloat(const LLWString &str );
00204         static BOOL             prevalidateInt(const LLWString &str );
00205         static BOOL             prevalidatePositiveS32(const LLWString &str);
00206         static BOOL             prevalidateNonNegativeS32(const LLWString &str);
00207         static BOOL             prevalidateAlphaNum(const LLWString &str );
00208         static BOOL             prevalidateAlphaNumSpace(const LLWString &str );
00209         static BOOL             prevalidatePrintableNotPipe(const LLWString &str); 
00210         static BOOL             prevalidatePrintableNoSpace(const LLWString &str);
00211         static BOOL             prevalidateASCII(const LLWString &str);
00212 
00213         static BOOL             postvalidateFloat(const LLString &str);
00214 
00215         // line history support:
00216         void                    setEnableLineHistory( BOOL enabled ); // switches line history on or off 
00217         void                    updateHistory(); // stores current line in history
00218 
00219 protected:
00220         void                    removeChar();
00221         void                    addChar(const llwchar c);
00222         void                    setCursorAtLocalPos(S32 local_mouse_x);
00223 
00224         S32                             findPixelNearestPos(S32 cursor_offset = 0);
00225         void                    reportBadKeystroke();
00226 
00227         BOOL                    handleSpecialKey(KEY key, MASK mask);
00228         BOOL                    handleSelectionKey(KEY key, MASK mask);
00229         BOOL                    handleControlKey(KEY key, MASK mask);
00230         S32                             handleCommitKey(KEY key, MASK mask);
00231 
00232 protected:
00233         LLUIString              mText;                                  // The string being edited.
00234         LLString                mPrevText;                              // Saved string for 'ESC' revert
00235         LLUIString              mLabel;                                 // text label that is visible when no user text provided
00236 
00237         // line history support:
00238         BOOL            mHaveHistory;                           // flag for enabled line history
00239         std::vector<LLString> mLineHistory;             // line history storage
00240         U32                     mCurrentHistoryLine;            // currently browsed history line
00241 
00242         LLViewBorder* mBorder;
00243         const LLFontGL* mGLFont;
00244         S32                     mMaxLengthChars;                        // Max number of characters
00245         S32                     mMaxLengthBytes;                        // Max length of the UTF8 string.
00246         S32                     mCursorPos;                                     // I-beam is just after the mCursorPos-th character.
00247         S32                     mScrollHPos;                            // Horizontal offset from the start of mText.  Used for scrolling.
00248         LLFrameTimer mScrollTimer;
00249         S32                     mMinHPixels;
00250         S32                     mMaxHPixels;
00251         S32                     mBorderLeft;
00252         S32                     mBorderRight;
00253 
00254         BOOL            mCommitOnFocusLost;
00255         BOOL            mRevertOnEsc;
00256 
00257         void            (*mKeystrokeCallback)( LLLineEditor* caller, void* userdata );
00258 
00259         BOOL            mIsSelecting;                           // Selection for clipboard operations
00260         S32                     mSelectionStart;
00261         S32                     mSelectionEnd;
00262         S32                     mLastSelectionX;
00263         S32                     mLastSelectionY;
00264         S32                     mLastSelectionStart;
00265         S32                     mLastSelectionEnd;
00266 
00267         S32                     (*mPrevalidateFunc)(const LLWString &str);
00268 
00269         LLFrameTimer mKeystrokeTimer;
00270 
00271         LLColor4        mCursorColor;
00272 
00273         LLColor4        mFgColor;
00274         LLColor4        mReadOnlyFgColor;
00275         LLColor4        mTentativeFgColor;
00276         LLColor4        mWriteableBgColor;
00277         LLColor4        mReadOnlyBgColor;
00278         LLColor4        mFocusBgColor;
00279 
00280         S32                     mBorderThickness;
00281 
00282         LLCoordGL       mLastIMEPosition;               // Last screen position used for the IME editor
00283 
00284         BOOL            mIgnoreArrowKeys;
00285         BOOL            mIgnoreTab;
00286         BOOL            mDrawAsterixes;
00287 
00288         BOOL            mHandleEditKeysDirectly;  // If true, the standard edit keys (Ctrl-X, Delete, etc,) are handled here instead of routed by the menu system
00289         BOOL            mSelectAllonFocusReceived;
00290         BOOL            mPassDelete;
00291 
00292         BOOL            mReadOnly;
00293 };
00294 
00295 
00296 class LLSearchEditor : public LLUICtrl
00297 {
00298 friend class LLLineEditorRollback;
00299 
00300 public:
00301         LLSearchEditor(const LLString& name, 
00302                 const LLRect& rect,
00303                 S32 max_length_bytes,
00304                 void (*search_callback)(const LLString& search_string, void* user_data),
00305                 void* userdata);
00306 
00307         virtual ~LLSearchEditor();
00308 
00309         /*virtual*/ void        draw();
00310 
00311         virtual EWidgetType getWidgetType() const;
00312         virtual LLString getWidgetTag() const;
00313         static LLView* fromXML(LLXMLNodePtr node, LLView *parent, LLUICtrlFactory *factory);
00314 
00315         void setText(const LLStringExplicit &new_text);
00316 
00317         void setSearchCallback(void (*search_callback)(const LLString& search_string, void* user_data), void* data) { mSearchCallback = search_callback; mCallbackUserData = data; }
00318 
00319         // LLUICtrl interface
00320         virtual void    setValue(const LLSD& value );
00321         virtual LLSD    getValue() const;
00322         virtual BOOL    setTextArg( const LLString& key, const LLStringExplicit& text );
00323         virtual BOOL    setLabelArg( const LLString& key, const LLStringExplicit& text );
00324         virtual void    clear();
00325 
00326 
00327 protected:
00328         LLLineEditor* mSearchEdit;
00329         LLButton* mClearSearchButton;
00330 
00331         void (*mSearchCallback)(const LLString& search_string, void* user_data);
00332 
00333         static void onSearchEdit(LLLineEditor* caller, void* user_data );
00334         static void onClearSearch(void* user_data);
00335 };
00336 
00337 #endif  // LL_LINEEDITOR_

Generated on Thu Jul 1 06:08:49 2010 for Second Life Viewer by  doxygen 1.4.7