From ae623c8068145f968f09ccf9bbdd90e993ca4283 Mon Sep 17 00:00:00 2001 From: Leyla Farazha Date: Thu, 16 Sep 2010 09:38:05 -0700 Subject: Set display name field now clamps to 31 unicode characters Added ability to set max length in line editors by characters in addition to bytes left other widgets (comboboxes/spinners) using the bytes, but it can easily be changed over reviewed by Richard --- indra/llui/llcombobox.cpp | 2 +- indra/llui/lllineeditor.cpp | 37 ++++++++++++++++++++-- indra/llui/lllineeditor.h | 14 ++++++-- indra/llui/llmultisliderctrl.cpp | 2 +- indra/llui/llspinctrl.cpp | 2 +- indra/newview/llfolderview.cpp | 2 +- indra/newview/lllocationinputctrl.cpp | 2 +- indra/newview/lltoastalertpanel.cpp | 3 ++ .../newview/skins/default/xui/en/notifications.xml | 2 +- 9 files changed, 56 insertions(+), 10 deletions(-) diff --git a/indra/llui/llcombobox.cpp b/indra/llui/llcombobox.cpp index f91b4ad025..e749ff9daf 100644 --- a/indra/llui/llcombobox.cpp +++ b/indra/llui/llcombobox.cpp @@ -484,7 +484,7 @@ void LLComboBox::createLineEditor(const LLComboBox::Params& p) LLLineEditor::Params params = p.combo_editor; params.rect(text_entry_rect); params.default_text(LLStringUtil::null); - params.max_length_bytes(mMaxChars); + params.max_length.bytes(mMaxChars); params.commit_callback.function(boost::bind(&LLComboBox::onTextCommit, this, _2)); params.keystroke_callback(boost::bind(&LLComboBox::onTextEntry, this, _1)); params.commit_on_focus_lost(false); diff --git a/indra/llui/lllineeditor.cpp b/indra/llui/lllineeditor.cpp index 2759167d04..3646228097 100644 --- a/indra/llui/lllineeditor.cpp +++ b/indra/llui/lllineeditor.cpp @@ -78,7 +78,7 @@ template class LLLineEditor* LLView::getChild( // LLLineEditor::Params::Params() -: max_length_bytes("max_length", 254), +: max_length(""), keystroke_callback("keystroke_callback"), prevalidate_callback("prevalidate_callback"), background_image("background_image"), @@ -108,7 +108,8 @@ LLLineEditor::Params::Params() LLLineEditor::LLLineEditor(const LLLineEditor::Params& p) : LLUICtrl(p), - mMaxLengthBytes(p.max_length_bytes), + mMaxLengthBytes(p.max_length.bytes), + mMaxLengthChars(p.max_length.chars), mCursorPos( 0 ), mScrollHPos( 0 ), mTextPadLeft(p.text_pad_left), @@ -313,6 +314,12 @@ void LLLineEditor::setMaxTextLength(S32 max_text_length) mMaxLengthBytes = max_len; } +void LLLineEditor::setMaxTextChars(S32 max_text_chars) +{ + S32 max_chars = llmax(0, max_text_chars); + mMaxLengthChars = max_chars; +} + void LLLineEditor::getTextPadding(S32 *left, S32 *right) { *left = mTextPadLeft; @@ -358,6 +365,16 @@ void LLLineEditor::setText(const LLStringExplicit &new_text) } mText.assign(truncated_utf8); + if (mMaxLengthChars) + { + LLWString truncated_wstring = utf8str_to_wstring(truncated_utf8); + if (truncated_wstring.size() > (U32)mMaxLengthChars) + { + truncated_wstring = truncated_wstring.substr(0, mMaxLengthChars); + } + mText.assign(wstring_to_utf8str(truncated_wstring)); + } + if (all_selected) { // ...keep whole thing selected @@ -798,6 +815,7 @@ void LLLineEditor::addChar(const llwchar uni_char) } S32 cur_bytes = mText.getString().size(); + S32 new_bytes = wchar_utf8_length(new_c); BOOL allow_char = TRUE; @@ -807,6 +825,14 @@ void LLLineEditor::addChar(const llwchar uni_char) { allow_char = FALSE; } + else if (mMaxLengthChars) + { + S32 wide_chars = mText.getWString().size(); + if ((wide_chars + 1) > mMaxLengthChars) + { + allow_char = FALSE; + } + } if (allow_char) { @@ -1107,6 +1133,13 @@ void LLLineEditor::pasteHelper(bool is_primary) clean_string = clean_string.substr(0, wchars_that_fit); LLUI::reportBadKeystroke(); } + + U32 available_chars = mMaxLengthChars - mText.getWString().size(); + + if (available_chars < clean_string.size()) + { + clean_string = clean_string.substr(0, available_chars); + } mText.insert(getCursor(), clean_string); setCursor( getCursor() + (S32)clean_string.length() ); diff --git a/indra/llui/lllineeditor.h b/indra/llui/lllineeditor.h index 76d0187712..b988bc412e 100644 --- a/indra/llui/lllineeditor.h +++ b/indra/llui/lllineeditor.h @@ -59,11 +59,19 @@ public: typedef boost::function keystroke_callback_t; + struct MaxLength : public LLInitParam::Choice + { + Alternative bytes, chars; + + MaxLength() : bytes("max_length_bytes", 254), + chars("max_length_chars", 0) + {} + }; + struct Params : public LLInitParam::Block { Optional default_text; - Optional max_length_bytes; - + Optional max_length; Optional keystroke_callback; Optional prevalidate_callback; @@ -214,6 +222,7 @@ public: void setKeystrokeCallback(callback_t callback, void* user_data); void setMaxTextLength(S32 max_text_length); + void setMaxTextChars(S32 max_text_chars); // Manipulate left and right padding for text void getTextPadding(S32 *left, S32 *right); void setTextPadding(S32 left, S32 right); @@ -277,6 +286,7 @@ protected: LLViewBorder* mBorder; const LLFontGL* mGLFont; S32 mMaxLengthBytes; // Max length of the UTF8 string in bytes + S32 mMaxLengthChars; // Maximum number of characters in the string S32 mCursorPos; // I-beam is just after the mCursorPos-th character. S32 mScrollHPos; // Horizontal offset from the start of mText. Used for scrolling. LLFrameTimer mScrollTimer; diff --git a/indra/llui/llmultisliderctrl.cpp b/indra/llui/llmultisliderctrl.cpp index bd65625f53..91e5b6b9de 100644 --- a/indra/llui/llmultisliderctrl.cpp +++ b/indra/llui/llmultisliderctrl.cpp @@ -130,7 +130,7 @@ LLMultiSliderCtrl::LLMultiSliderCtrl(const LLMultiSliderCtrl::Params& p) params.name("MultiSliderCtrl Editor"); params.rect(text_rect); params.font(p.font); - params.max_length_bytes(MAX_STRING_LENGTH); + params.max_length.bytes(MAX_STRING_LENGTH); params.commit_callback.function(LLMultiSliderCtrl::onEditorCommit); params.prevalidate_callback(&LLTextValidate::validateFloat); params.follows.flags(FOLLOWS_LEFT | FOLLOWS_BOTTOM); diff --git a/indra/llui/llspinctrl.cpp b/indra/llui/llspinctrl.cpp index 9decfa0b25..6b4e9cf923 100644 --- a/indra/llui/llspinctrl.cpp +++ b/indra/llui/llspinctrl.cpp @@ -119,7 +119,7 @@ LLSpinCtrl::LLSpinCtrl(const LLSpinCtrl::Params& p) { params.font(p.font); } - params.max_length_bytes(MAX_STRING_LENGTH); + params.max_length.bytes(MAX_STRING_LENGTH); params.commit_callback.function((boost::bind(&LLSpinCtrl::onEditorCommit, this, _2))); if( mPrecision>0 )//should accept float numbers diff --git a/indra/newview/llfolderview.cpp b/indra/newview/llfolderview.cpp index 5d8e3f9ab9..21c6c98876 100644 --- a/indra/newview/llfolderview.cpp +++ b/indra/newview/llfolderview.cpp @@ -224,7 +224,7 @@ LLFolderView::LLFolderView(const Params& p) params.name("ren"); params.rect(rect); params.font(getLabelFontForStyle(LLFontGL::NORMAL)); - params.max_length_bytes(DB_INV_ITEM_NAME_STR_LEN); + params.max_length.bytes(DB_INV_ITEM_NAME_STR_LEN); params.commit_callback.function(boost::bind(&LLFolderView::commitRename, this, _2)); params.prevalidate_callback(&LLTextValidate::validateASCIIPrintableNoPipe); params.commit_on_focus_lost(true); diff --git a/indra/newview/lllocationinputctrl.cpp b/indra/newview/lllocationinputctrl.cpp index d714cae872..1527f8f4c9 100644 --- a/indra/newview/lllocationinputctrl.cpp +++ b/indra/newview/lllocationinputctrl.cpp @@ -224,7 +224,7 @@ LLLocationInputCtrl::LLLocationInputCtrl(const LLLocationInputCtrl::Params& p) LLLineEditor::Params params = p.combo_editor; params.rect(text_entry_rect); params.default_text(LLStringUtil::null); - params.max_length_bytes(p.max_chars); + params.max_length.bytes(p.max_chars); params.keystroke_callback(boost::bind(&LLLocationInputCtrl::onTextEntry, this, _1)); params.commit_on_focus_lost(false); params.follows.flags(FOLLOWS_ALL); diff --git a/indra/newview/lltoastalertpanel.cpp b/indra/newview/lltoastalertpanel.cpp index 2d0c360905..8b2f066d41 100644 --- a/indra/newview/lltoastalertpanel.cpp +++ b/indra/newview/lltoastalertpanel.cpp @@ -76,6 +76,7 @@ LLToastAlertPanel::LLToastAlertPanel( LLNotificationPtr notification, bool modal LLNotificationFormPtr form = mNotification->getForm(); std::string edit_text_name; std::string edit_text_contents; + S32 edit_text_max_chars = 0; bool is_password = false; LLToastPanel::setBackgroundVisible(FALSE); @@ -115,6 +116,7 @@ LLToastAlertPanel::LLToastAlertPanel( LLNotificationPtr notification, bool modal { edit_text_contents = (*it)["value"].asString(); edit_text_name = (*it)["name"].asString(); + edit_text_max_chars = (*it)["max_length_chars"].asInteger(); } else if (type == "password") { @@ -253,6 +255,7 @@ LLToastAlertPanel::LLToastAlertPanel( LLNotificationPtr notification, bool modal mLineEditor->setName(edit_text_name); mLineEditor->reshape(leditor_rect.getWidth(), leditor_rect.getHeight()); mLineEditor->setRect(leditor_rect); + mLineEditor->setMaxTextChars(edit_text_max_chars); mLineEditor->setText(edit_text_contents); // decrease limit of line editor of teleport offer dialog to avoid truncation of diff --git a/indra/newview/skins/default/xui/en/notifications.xml b/indra/newview/skins/default/xui/en/notifications.xml index 281f367345..5bd28e5b91 100644 --- a/indra/newview/skins/default/xui/en/notifications.xml +++ b/indra/newview/skins/default/xui/en/notifications.xml @@ -3197,7 +3197,7 @@ Press reset to make it the same as your username. Change your display name?
- + [DISPLAY_NAME]