/indra/newview/llurllineeditorctrl.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 93 lines · 48 code · 13 blank · 32 comment · 7 complexity · 553b923650147c702558e022542e7a20 MD5 · raw file

  1. /**
  2. * @file llurllineeditorctrl.cpp
  3. * @brief LLURLLineEditor base class
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "llviewerprecompiledheaders.h"
  27. #include "llclipboard.h"
  28. #include "lluictrlfactory.h"
  29. #include "llurllineeditorctrl.h"
  30. #include "llweb.h"
  31. #include "llslurl.h"
  32. //Constructor
  33. LLURLLineEditor::LLURLLineEditor(const LLLineEditor::Params& p)
  34. : LLLineEditor(p){
  35. }
  36. // copy selection to clipboard
  37. void LLURLLineEditor::copy()
  38. {
  39. if( canCopy() )
  40. {
  41. copyEscapedURLToClipboard();
  42. }
  43. }
  44. // cut selection to clipboard
  45. void LLURLLineEditor::cut()
  46. {
  47. if( canCut() )
  48. {
  49. // Prepare for possible rollback
  50. LLURLLineEditorRollback rollback( this );
  51. copyEscapedURLToClipboard();
  52. deleteSelection();
  53. // Validate new string and rollback the if needed.
  54. BOOL need_to_rollback = ( mPrevalidateFunc && !mPrevalidateFunc( mText.getWString() ) );
  55. if( need_to_rollback )
  56. {
  57. rollback.doRollback( this );
  58. LLUI::reportBadKeystroke();
  59. }
  60. else
  61. if( mKeystrokeCallback )
  62. {
  63. mKeystrokeCallback( this );
  64. }
  65. }
  66. }
  67. // Copies escaped URL to clipboard
  68. void LLURLLineEditor::copyEscapedURLToClipboard()
  69. {
  70. S32 left_pos = llmin( mSelectionStart, mSelectionEnd );
  71. S32 length = llabs( mSelectionStart - mSelectionEnd );
  72. const std::string unescaped_text = wstring_to_utf8str(mText.getWString().substr(left_pos, length));
  73. LLWString text_to_copy;
  74. // *HACK: Because LLSLURL is currently broken we cannot use it to check if unescaped_text is a valid SLURL (see EXT-8335).
  75. if (LLStringUtil::startsWith(unescaped_text, "http://")) // SLURL
  76. text_to_copy = utf8str_to_wstring(LLWeb::escapeURL(unescaped_text));
  77. else // human-readable location
  78. text_to_copy = utf8str_to_wstring(unescaped_text);
  79. gClipboard.copyFromString( text_to_copy );
  80. }