lllineeditor.h

Go to the documentation of this file.
00001 
00041 #ifndef LL_LLLINEEDITOR_H
00042 #define LL_LLLINEEDITOR_H
00043 
00044 #include "v4color.h"
00045 #include "llframetimer.h"
00046 
00047 #include "lleditmenuhandler.h"
00048 #include "lluictrl.h"
00049 #include "lluistring.h"
00050 #include "llviewborder.h"
00051 
00052 #include "llpreeditor.h"
00053 
00054 class LLFontGL;
00055 class LLLineEditorRollback;
00056 class LLButton;
00057 
00058 typedef BOOL (*LLLinePrevalidateFunc)(const LLWString &wstr);
00059 
00060 
00061 class LLLineEditor
00062 : public LLUICtrl, public LLEditMenuHandler, protected LLPreeditor
00063 {
00064 
00065 public:
00066         LLLineEditor(const LLString& name, 
00067                                  const LLRect& rect,
00068                                  const LLString& default_text = LLString::null,
00069                                  const LLFontGL* glfont = NULL,
00070                                  S32 max_length_bytes = 254,
00071                                  void (*commit_callback)(LLUICtrl* caller, void* user_data) = NULL,
00072                                  void (*keystroke_callback)(LLLineEditor* caller, void* user_data) = NULL,
00073                                  void (*focus_lost_callback)(LLFocusableElement* caller, void* user_data) = NULL,
00074                                  void* userdata = NULL,
00075                                  LLLinePrevalidateFunc prevalidate_func = NULL,
00076                                  LLViewBorder::EBevel border_bevel = LLViewBorder::BEVEL_IN,
00077                                  LLViewBorder::EStyle border_style = LLViewBorder::STYLE_LINE,
00078                                  S32 border_thickness = 1);
00079 
00080         virtual ~LLLineEditor();
00081 
00082         virtual LLXMLNodePtr getXML(bool save_children = true) const;
00083         void setColorParameters(LLXMLNodePtr node);
00084         static LLView* fromXML(LLXMLNodePtr node, LLView *parent, LLUICtrlFactory *factory);
00085         static void cleanupClass();
00086 
00087         // mousehandler overrides
00088         /*virtual*/ BOOL        handleMouseDown(S32 x, S32 y, MASK mask);
00089         /*virtual*/ BOOL        handleMouseUp(S32 x, S32 y, MASK mask);
00090         /*virtual*/ BOOL        handleHover(S32 x, S32 y, MASK mask);
00091         /*virtual*/ BOOL        handleDoubleClick(S32 x,S32 y,MASK mask);
00092         /*virtual*/ BOOL        handleKeyHere(KEY key, MASK mask );
00093         /*virtual*/ BOOL        handleUnicodeCharHere(llwchar uni_char);
00094         /*virtual*/ void        onMouseCaptureLost();
00095 
00096         // LLEditMenuHandler overrides
00097         virtual void    cut();
00098         virtual BOOL    canCut() const;
00099 
00100         virtual void    copy();
00101         virtual BOOL    canCopy() const;
00102 
00103         virtual void    paste();
00104         virtual BOOL    canPaste() const;
00105         
00106         virtual void    doDelete();
00107         virtual BOOL    canDoDelete() const;
00108 
00109         virtual void    selectAll();
00110         virtual BOOL    canSelectAll() const;
00111 
00112         virtual void    deselect();
00113         virtual BOOL    canDeselect() const;
00114 
00115         // view overrides
00116         virtual void    draw();
00117         virtual void    reshape(S32 width,S32 height,BOOL called_from_parent=TRUE);
00118         virtual void    onFocusReceived();
00119         virtual void    onFocusLost();
00120         virtual void    setEnabled(BOOL enabled);
00121 
00122         // UI control overrides
00123         virtual void    clear();
00124         virtual void    onTabInto();
00125         virtual void    setFocus( BOOL b );
00126         virtual void    setRect(const LLRect& rect);
00127         virtual BOOL    acceptsTextInput() const;
00128         virtual void    onCommit();
00129         virtual BOOL    isDirty() const { return mText.getString() != mPrevText; }      // Returns TRUE if user changed value at all
00130         virtual void    resetDirty() { mPrevText = mText.getString(); }         // Clear dirty state
00131 
00132         // assumes UTF8 text
00133         virtual void    setValue(const LLSD& value ) { setText(value.asString()); }
00134         virtual LLSD    getValue() const { return LLSD(getText()); }
00135         virtual BOOL    setTextArg( const LLString& key, const LLStringExplicit& text );
00136         virtual BOOL    setLabelArg( const LLString& key, const LLStringExplicit& text );
00137 
00138         void                    setLabel(const LLStringExplicit &new_label) { mLabel = new_label; }
00139         void                    setText(const LLStringExplicit &new_text);
00140 
00141         const LLString& getText() const         { return mText.getString(); }
00142         const LLWString& getWText() const       { return mText.getWString(); }
00143         S32                             getLength() const       { return mText.length(); }
00144 
00145         S32                             getCursor()     const   { return mCursorPos; }
00146         void                    setCursor( S32 pos );
00147         void                    setCursorToEnd();
00148 
00149         // Selects characters 'start' to 'end'.
00150         void                    setSelection(S32 start, S32 end);
00151         
00152         void                    setCommitOnFocusLost( BOOL b )  { mCommitOnFocusLost = b; }
00153         void                    setRevertOnEsc( BOOL b )                { mRevertOnEsc = b; }
00154 
00155         void setCursorColor(const LLColor4& c)                  { mCursorColor = c; }
00156         const LLColor4& getCursorColor() const                  { return mCursorColor; }
00157 
00158         void setFgColor( const LLColor4& c )                    { mFgColor = c; }
00159         void setReadOnlyFgColor( const LLColor4& c )    { mReadOnlyFgColor = c; }
00160         void setTentativeFgColor(const LLColor4& c)             { mTentativeFgColor = c; }
00161         void setWriteableBgColor( const LLColor4& c )   { mWriteableBgColor = c; }
00162         void setReadOnlyBgColor( const LLColor4& c )    { mReadOnlyBgColor = c; }
00163         void setFocusBgColor(const LLColor4& c)                 { mFocusBgColor = c; }
00164 
00165         const LLColor4& getFgColor() const                      { return mFgColor; }
00166         const LLColor4& getReadOnlyFgColor() const      { return mReadOnlyFgColor; }
00167         const LLColor4& getTentativeFgColor() const { return mTentativeFgColor; }
00168         const LLColor4& getWriteableBgColor() const     { return mWriteableBgColor; }
00169         const LLColor4& getReadOnlyBgColor() const      { return mReadOnlyBgColor; }
00170         const LLColor4& getFocusBgColor() const         { return mFocusBgColor; }
00171 
00172         void                    setIgnoreArrowKeys(BOOL b)              { mIgnoreArrowKeys = b; }
00173         void                    setIgnoreTab(BOOL b)                    { mIgnoreTab = b; }
00174         void                    setPassDelete(BOOL b)                   { mPassDelete = b; }
00175         void                    setDrawAsterixes(BOOL b);
00176 
00177         // get the cursor position of the beginning/end of the prev/next word in the text
00178         S32                             prevWordPos(S32 cursorPos) const;
00179         S32                             nextWordPos(S32 cursorPos) const;
00180 
00181         BOOL                    hasSelection() const { return (mSelectionStart != mSelectionEnd); }
00182         void                    startSelection();
00183         void                    endSelection();
00184         void                    extendSelection(S32 new_cursor_pos);
00185         void                    deleteSelection();
00186 
00187         void                    setHandleEditKeysDirectly( BOOL b ) { mHandleEditKeysDirectly = b; }
00188         void                    setSelectAllonFocusReceived(BOOL b);
00189 
00190         void                    setKeystrokeCallback(void (*keystroke_callback)(LLLineEditor* caller, void* user_data));
00191 
00192         void                    setMaxTextLength(S32 max_text_length);
00193         void                    setTextPadding(S32 left, S32 right); // Used to specify room for children before or after text.
00194 
00195         static BOOL             isPartOfWord(llwchar c);
00196         // Prevalidation controls which keystrokes can affect the editor
00197         void                    setPrevalidate( BOOL (*func)(const LLWString &) );
00198         static BOOL             prevalidateFloat(const LLWString &str );
00199         static BOOL             prevalidateInt(const LLWString &str );
00200         static BOOL             prevalidatePositiveS32(const LLWString &str);
00201         static BOOL             prevalidateNonNegativeS32(const LLWString &str);
00202         static BOOL             prevalidateAlphaNum(const LLWString &str );
00203         static BOOL             prevalidateAlphaNumSpace(const LLWString &str );
00204         static BOOL             prevalidatePrintableNotPipe(const LLWString &str); 
00205         static BOOL             prevalidatePrintableNoSpace(const LLWString &str);
00206         static BOOL             prevalidateASCII(const LLWString &str);
00207 
00208         static BOOL             postvalidateFloat(const LLString &str);
00209 
00210         // line history support:
00211         void                    setEnableLineHistory( BOOL enabled ) { mHaveHistory = enabled; } // switches line history on or off 
00212         void                    updateHistory(); // stores current line in history
00213 
00214         void                    setReplaceNewlinesWithSpaces(BOOL replace);
00215         
00216 private:
00217         // private helper methods
00218         void                    removeChar();
00219         void                    addChar(const llwchar c);
00220         void                    setCursorAtLocalPos(S32 local_mouse_x);
00221         S32                             findPixelNearestPos(S32 cursor_offset = 0) const;
00222         void                    reportBadKeystroke();
00223         BOOL                    handleSpecialKey(KEY key, MASK mask);
00224         BOOL                    handleSelectionKey(KEY key, MASK mask);
00225         BOOL                    handleControlKey(KEY key, MASK mask);
00226         S32                             handleCommitKey(KEY key, MASK mask);
00227 
00228         //
00229         // private data members
00230         //
00231         void                    updateAllowingLanguageInput();
00232         BOOL                    hasPreeditString() const;
00233         // Implementation (overrides) of LLPreeditor
00234         virtual void    resetPreedit();
00235         virtual void    updatePreedit(const LLWString &preedit_string,
00236                                                 const segment_lengths_t &preedit_segment_lengths, const standouts_t &preedit_standouts, S32 caret_position);
00237         virtual void    markAsPreedit(S32 position, S32 length);
00238         virtual void    getPreeditRange(S32 *position, S32 *length) const;
00239         virtual void    getSelectionRange(S32 *position, S32 *length) const;
00240         virtual BOOL    getPreeditLocation(S32 query_position, LLCoordGL *coord, LLRect *bounds, LLRect *control) const;
00241         virtual S32             getPreeditFontSize() const;
00242 
00243 protected:
00244         LLUIString              mText;                                  // The string being edited.
00245         LLString                mPrevText;                              // Saved string for 'ESC' revert
00246         LLUIString              mLabel;                                 // text label that is visible when no user text provided
00247 
00248         // line history support:
00249         BOOL            mHaveHistory;                           // flag for enabled line history
00250         std::vector<LLString> mLineHistory;             // line history storage
00251         U32                     mCurrentHistoryLine;            // currently browsed history line
00252 
00253         LLViewBorder* mBorder;
00254         const LLFontGL* mGLFont;
00255         S32                     mMaxLengthBytes;                        // Max length of the UTF8 string in bytes
00256         S32                     mCursorPos;                                     // I-beam is just after the mCursorPos-th character.
00257         S32                     mScrollHPos;                            // Horizontal offset from the start of mText.  Used for scrolling.
00258         LLFrameTimer mScrollTimer;
00259         S32                     mTextPadLeft;                           // Used to reserve space before the beginning of the text for children.
00260         S32                     mTextPadRight;                          // Used to reserve space after the end of the text for children.
00261         S32                     mMinHPixels;
00262         S32                     mMaxHPixels;
00263 
00264         BOOL            mCommitOnFocusLost;
00265         BOOL            mRevertOnEsc;
00266 
00267         void            (*mKeystrokeCallback)( LLLineEditor* caller, void* userdata );
00268 
00269         BOOL            mIsSelecting;                           // Selection for clipboard operations
00270         S32                     mSelectionStart;
00271         S32                     mSelectionEnd;
00272         S32                     mLastSelectionX;
00273         S32                     mLastSelectionY;
00274         S32                     mLastSelectionStart;
00275         S32                     mLastSelectionEnd;
00276 
00277         S32                     (*mPrevalidateFunc)(const LLWString &str);
00278 
00279         LLFrameTimer mKeystrokeTimer;
00280 
00281         LLColor4        mCursorColor;
00282 
00283         LLColor4        mFgColor;
00284         LLColor4        mReadOnlyFgColor;
00285         LLColor4        mTentativeFgColor;
00286         LLColor4        mWriteableBgColor;
00287         LLColor4        mReadOnlyBgColor;
00288         LLColor4        mFocusBgColor;
00289 
00290         S32                     mBorderThickness;
00291 
00292         BOOL            mIgnoreArrowKeys;
00293         BOOL            mIgnoreTab;
00294         BOOL            mDrawAsterixes;
00295 
00296         BOOL            mHandleEditKeysDirectly;  // If true, the standard edit keys (Ctrl-X, Delete, etc,) are handled here instead of routed by the menu system
00297         BOOL            mSelectAllonFocusReceived;
00298         BOOL            mPassDelete;
00299 
00300         BOOL            mReadOnly;
00301 
00302         LLWString       mPreeditWString;
00303         LLWString       mPreeditOverwrittenWString;
00304         std::vector<S32> mPreeditPositions;
00305         LLPreeditor::standouts_t mPreeditStandouts;
00306 
00307 private:
00308         // Utility on top of LLUI::getUIImage, looks up a named image in a given XML node and returns it if possible
00309         // or returns a given default image if anything in the process fails.
00310         static LLPointer<LLUIImage> parseImage(LLString name, LLXMLNodePtr from, LLPointer<LLUIImage> def);
00311         // Global instance used as default for member instance below.
00312         static LLPointer<LLUIImage> sImage;
00313         // Instances that by default point to the statics but can be overidden in XML.
00314         LLPointer<LLUIImage> mImage;
00315 
00316         BOOL        mReplaceNewlinesWithSpaces; // if false, will replace pasted newlines with paragraph symbol.
00317 
00318         // private helper class
00319         class LLLineEditorRollback
00320         {
00321         public:
00322                 LLLineEditorRollback( LLLineEditor* ed )
00323                         :
00324                         mCursorPos( ed->mCursorPos ),
00325                         mScrollHPos( ed->mScrollHPos ),
00326                         mIsSelecting( ed->mIsSelecting ),
00327                         mSelectionStart( ed->mSelectionStart ),
00328                         mSelectionEnd( ed->mSelectionEnd )
00329                 {
00330                         mText = ed->getText();
00331                 }
00332 
00333                 void doRollback( LLLineEditor* ed )
00334                 {
00335                         ed->mCursorPos = mCursorPos;
00336                         ed->mScrollHPos = mScrollHPos;
00337                         ed->mIsSelecting = mIsSelecting;
00338                         ed->mSelectionStart = mSelectionStart;
00339                         ed->mSelectionEnd = mSelectionEnd;
00340                         ed->mText = mText;
00341                         ed->mPrevText = mText;
00342                 }
00343 
00344                 LLString getText()   { return mText; }
00345 
00346         private:
00347                 LLString mText;
00348                 S32             mCursorPos;
00349                 S32             mScrollHPos;
00350                 BOOL    mIsSelecting;
00351                 S32             mSelectionStart;
00352                 S32             mSelectionEnd;
00353         }; // end class LLLineEditorRollback
00354 
00355 }; // end class LLLineEditor
00356 
00357 
00358 
00359 /*
00360  * @brief A line editor with a button to clear it and a callback to call on every edit event.
00361  */
00362 class LLSearchEditor : public LLUICtrl
00363 {
00364 public:
00365         LLSearchEditor(const LLString& name, 
00366                 const LLRect& rect,
00367                 S32 max_length_bytes,
00368                 void (*search_callback)(const LLString& search_string, void* user_data),
00369                 void* userdata);
00370 
00371         virtual ~LLSearchEditor() {}
00372 
00373         /*virtual*/ void        draw();
00374 
00375         static LLView* fromXML(LLXMLNodePtr node, LLView *parent, LLUICtrlFactory *factory);
00376 
00377         void setText(const LLStringExplicit &new_text) { mSearchEdit->setText(new_text); }
00378 
00379         void setSearchCallback(void (*search_callback)(const LLString& search_string, void* user_data), void* data) { mSearchCallback = search_callback; mCallbackUserData = data; }
00380 
00381         // LLUICtrl interface
00382         virtual void    setValue(const LLSD& value );
00383         virtual LLSD    getValue() const;
00384         virtual BOOL    setTextArg(  const LLString& key, const LLStringExplicit& text );
00385         virtual BOOL    setLabelArg( const LLString& key, const LLStringExplicit& text );
00386         virtual void    clear();
00387 
00388 private:
00389         static void onSearchEdit(LLLineEditor* caller, void* user_data );
00390         static void onClearSearch(void* user_data);
00391 
00392         LLLineEditor* mSearchEdit;
00393         class LLButton* mClearSearchButton;
00394         void (*mSearchCallback)(const LLString& search_string, void* user_data);
00395 
00396 };
00397 
00398 #endif  // LL_LINEEDITOR_

Generated on Fri May 16 08:32:53 2008 for SecondLife by  doxygen 1.5.5