/indra/newview/llurllineeditorctrl.cpp
C++ | 93 lines | 48 code | 13 blank | 32 comment | 7 complexity | 553b923650147c702558e022542e7a20 MD5 | raw file
Possible License(s): LGPL-2.1
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
27#include "llviewerprecompiledheaders.h"
28
29#include "llclipboard.h"
30#include "lluictrlfactory.h"
31
32#include "llurllineeditorctrl.h"
33
34#include "llweb.h"
35#include "llslurl.h"
36
37//Constructor
38LLURLLineEditor::LLURLLineEditor(const LLLineEditor::Params& p)
39: LLLineEditor(p){
40
41}
42
43// copy selection to clipboard
44void LLURLLineEditor::copy()
45{
46 if( canCopy() )
47 {
48 copyEscapedURLToClipboard();
49 }
50}
51
52// cut selection to clipboard
53void LLURLLineEditor::cut()
54{
55 if( canCut() )
56 {
57 // Prepare for possible rollback
58 LLURLLineEditorRollback rollback( this );
59
60 copyEscapedURLToClipboard();
61
62 deleteSelection();
63
64 // Validate new string and rollback the if needed.
65 BOOL need_to_rollback = ( mPrevalidateFunc && !mPrevalidateFunc( mText.getWString() ) );
66 if( need_to_rollback )
67 {
68 rollback.doRollback( this );
69 LLUI::reportBadKeystroke();
70 }
71 else
72 if( mKeystrokeCallback )
73 {
74 mKeystrokeCallback( this );
75 }
76 }
77}
78// Copies escaped URL to clipboard
79void LLURLLineEditor::copyEscapedURLToClipboard()
80{
81 S32 left_pos = llmin( mSelectionStart, mSelectionEnd );
82 S32 length = llabs( mSelectionStart - mSelectionEnd );
83
84 const std::string unescaped_text = wstring_to_utf8str(mText.getWString().substr(left_pos, length));
85 LLWString text_to_copy;
86 // *HACK: Because LLSLURL is currently broken we cannot use it to check if unescaped_text is a valid SLURL (see EXT-8335).
87 if (LLStringUtil::startsWith(unescaped_text, "http://")) // SLURL
88 text_to_copy = utf8str_to_wstring(LLWeb::escapeURL(unescaped_text));
89 else // human-readable location
90 text_to_copy = utf8str_to_wstring(unescaped_text);
91
92 gClipboard.copyFromString( text_to_copy );
93}