Browse Source

Add mechanism to copy the selected rows to clipboard. Use in console.
Closes #289.

Yao Wei Tjong 姚伟忠 11 years ago
parent
commit
6da0cb3995

+ 5 - 2
Bin/Data/UI/DefaultStyle.xml

@@ -218,8 +218,11 @@
         <attribute name="Color" value="0.15 0.15 0.15 0.8" />
         <attribute name="Layout Border" value="4 4 4 4" />
     </element>
-    <element type="ConsoleText" style="Text" auto="false" />
-    <element type="ConsoleHighlightedText" style="Text" auto="false">
+    <element type="ConsoleText" style="Text" auto="false">
+        <attribute name="Hover Color" value="0.3 0.4 0.7 1" />
+        <attribute name="Selection Color" value="0.2 0.225 0.35 1" />
+    </element>
+    <element type="ConsoleHighlightedText" style="ConsoleText" auto="false">
         <attribute name="Color" value="1 0 0 1" />
     </element>
     <element type="ConsoleLineEdit" style="LineEdit" auto="false">

+ 7 - 0
Source/Engine/Engine/Console.cpp

@@ -70,6 +70,8 @@ Console::Console(Context* context) :
     background_->SetLayout(LM_VERTICAL);
 
     rowContainer_ = new ListView(context_);
+    rowContainer_->SetHighlightMode(HM_ALWAYS);
+    rowContainer_->SetMultiselect(true);
     background_->AddChild(rowContainer_);
 
     lineEdit_ = new LineEdit(context_);
@@ -231,6 +233,11 @@ unsigned Console::GetNumBufferedRows() const
     return rowContainer_->GetNumItems();
 }
 
+void Console::CopySelectedRows() const
+{
+    rowContainer_->CopySelectedItemsToClipboard();
+}
+
 const String& Console::GetHistoryRow(unsigned index) const
 {
     return index < history_.Size() ? history_[index] : String::EMPTY;

+ 2 - 0
Source/Engine/Engine/Console.h

@@ -79,6 +79,8 @@ public:
     unsigned GetNumBufferedRows() const;
     /// Return number of displayed rows.
     unsigned GetNumRows() const { return displayedRows_; }
+    /// Copy selected rows to system clipboard.
+    void CopySelectedRows() const;
     /// Return history maximum size.
     unsigned GetNumHistoryRows() const { return historyRows_; }
     /// Return current history position.

+ 1 - 0
Source/Engine/LuaScript/pkgs/Engine/Console.pkg

@@ -19,6 +19,7 @@ class Console : public Object
     bool IsAutoVisibleOnError() const;
     unsigned GetNumBufferedRows() const;
     unsigned GetNumRows() const;
+    void CopySelectedRows() const;
     unsigned GetNumHistoryRows() const;
     unsigned GetHistoryPosition() const;
     const String GetHistoryRow(unsigned index) const;

+ 1 - 0
Source/Engine/LuaScript/pkgs/UI/ListView.pkg

@@ -45,6 +45,7 @@ class ListView : public ScrollView
     unsigned FindItem(UIElement* item) const;
     unsigned GetSelection() const;
     const PODVector<unsigned>& GetSelections() const;
+    void CopySelectedItemsToClipboard() const;
     UIElement* GetSelectedItem() const;
     
     // PODVector<UIElement*> GetSelectedItems() const;

+ 1 - 0
Source/Engine/Script/EngineAPI.cpp

@@ -39,6 +39,7 @@ static void RegisterConsole(asIScriptEngine* engine)
     RegisterObject<Console>(engine, "Console");
     engine->RegisterObjectMethod("Console", "void Toggle()", asMETHOD(Console, Toggle), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "void UpdateElements()", asMETHOD(Console, UpdateElements), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Console", "void CopySelectedRows() const", asMETHOD(Console, CopySelectedRows), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "void set_defaultStyle(XMLFile@+)", asMETHOD(Console, SetDefaultStyle), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "XMLFile@+ get_defaultStyle() const", asMETHOD(Console, GetDefaultStyle), asCALL_THISCALL);
     engine->RegisterObjectMethod("Console", "void set_visible(bool)", asMETHOD(Console, SetVisible), asCALL_THISCALL);

+ 1 - 0
Source/Engine/Script/UIAPI.cpp

@@ -280,6 +280,7 @@ static void RegisterListView(asIScriptEngine* engine)
     engine->RegisterObjectMethod("ListView", "void ChangeSelection(int, bool)", asMETHOD(ListView, ChangeSelection), asCALL_THISCALL);
     engine->RegisterObjectMethod("ListView", "void SetSelections(Array<uint>@+)", asFUNCTION(ListViewSetSelections), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("ListView", "void ClearSelection()", asMETHOD(ListView, ClearSelection), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ListView", "void CopySelectedItemsToClipboard()", asMETHOD(ListView, CopySelectedItemsToClipboard), asCALL_THISCALL);
     engine->RegisterObjectMethod("ListView", "void Expand(uint, bool, bool arg2 = false)", asMETHOD(ListView, Expand), asCALL_THISCALL);
     engine->RegisterObjectMethod("ListView", "void ToggleExpand(uint, bool arg1 = false)", asMETHOD(ListView, ToggleExpand), asCALL_THISCALL);
     engine->RegisterObjectMethod("ListView", "bool IsSelected(uint) const", asMETHOD(ListView, IsSelected), asCALL_THISCALL);

+ 17 - 0
Source/Engine/UI/ListView.cpp

@@ -27,6 +27,8 @@
 #include "ListView.h"
 #include "Log.h"
 #include "Sort.h"
+#include "Text.h"
+#include "UI.h"
 #include "UIEvents.h"
 
 #include "DebugNew.h"
@@ -858,6 +860,21 @@ PODVector<UIElement*> ListView::GetSelectedItems() const
     return ret;
 }
 
+void ListView::CopySelectedItemsToClipboard() const
+{
+    String selectedText;
+
+    for (PODVector<unsigned>::ConstIterator i = selections_.Begin(); i != selections_.End(); ++i)
+    {
+        // Only handle Text UI element
+        Text* text = dynamic_cast<Text*>(GetItem(*i));
+        if (text)
+            selectedText.Append(text->GetText()).Append("\n");
+    }
+
+    GetSubsystem<UI>()->SetClipboardText(selectedText);
+}
+
 bool ListView::IsSelected(unsigned index) const
 {
     return selections_.Contains(index);

+ 2 - 0
Source/Engine/UI/ListView.h

@@ -112,6 +112,8 @@ public:
     unsigned GetSelection() const;
     /// Return all selected indices.
     const PODVector<unsigned>& GetSelections() const { return selections_; }
+    /// Copy selected items to system clipboard. Currently only applicable to Text items.
+    void CopySelectedItemsToClipboard() const;
     /// Return first selected item, or null if none selected.
     UIElement* GetSelectedItem() const;
     /// Return all selected items.