Skip to content

Commit

Permalink
Add external clipboard support
Browse files Browse the repository at this point in the history
  • Loading branch information
magiblot committed Nov 23, 2020
1 parent bf7d225 commit 469e279
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "deps/fmt"]
path = deps/fmt
url = https://github.com/fmtlib/fmt
[submodule "libclipboard"]
path = deps/libclipboard
url = https://github.com/jtanx/libclipboard.git
7 changes: 3 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ file(GLOB_RECURSE TURBO_SRC "${CMAKE_CURRENT_LIST_DIR}/src/*.cc")
add_executable(${PROJECT_NAME} ${TURBO_SRC})

add_subdirectory(deps/fmt)

add_subdirectory(deps/libclipboard)
add_subdirectory(deps/tvision)

target_include_directories(${PROJECT_NAME} PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/include"
"${CMAKE_CURRENT_LIST_DIR}/src"
"${CMAKE_CURRENT_LIST_DIR}/src/platform"
"${CMAKE_CURRENT_LIST_DIR}/deps/libclipboard/include"
)

if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0")
Expand All @@ -86,6 +87,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE
scintilla
tvision
fmt
clipboard
)

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9)
Expand All @@ -98,6 +100,3 @@ if (MAGIC)
target_link_libraries(${PROJECT_NAME} PRIVATE ${MAGIC})
target_compile_definitions(${PROJECT_NAME} PRIVATE HAVE_MAGIC)
endif()

target_link_libraries(turbo ${LIBS})

1 change: 1 addition & 0 deletions deps/libclipboard
Submodule libclipboard added at 3d2cb0
2 changes: 1 addition & 1 deletion scintilla/src/Editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class SelectionText {
codePage = 0;
characterSet = 0;
}
void Copy(const std::string &s_, int codePage_, int characterSet_, bool rectangular_, bool lineCopy_) {
void Copy(std::string_view s_, int codePage_, int characterSet_, bool rectangular_, bool lineCopy_) {
s = s_;
codePage = codePage_;
characterSet = characterSet_;
Expand Down
3 changes: 2 additions & 1 deletion src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <vector>
#include "util.h"
#include "doctree.h"
#include "clipboard.h"

struct EditorWindow;
class TClockView;
Expand Down Expand Up @@ -43,7 +44,7 @@ struct TurboApp : public TApplication {
bool argsParsed {false};
int argc;
const char **argv;
Scintilla::SelectionText clipboard;
Clipboard clipboard;

static TurboApp *app;

Expand Down
25 changes: 25 additions & 0 deletions src/clipboard.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "clipboard.h"
#include <libclipboard.h>

Clipboard::Clipboard()
{
cb = clipboard_new(nullptr);
}

Clipboard::~Clipboard()
{
clipboard_free(cb);
}

void Clipboard::syncSetText(std::string_view text)
{
if (cb)
clipboard_set_text_ex(cb, text.data(), (int) text.size(), LCB_CLIPBOARD);
}

const char* Clipboard::syncGetText()
{
if (cb)
return clipboard_text(cb);
return nullptr;
}
43 changes: 43 additions & 0 deletions src/clipboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef TURBO_CLIPBOARD_H
#define TURBO_CLIPBOARD_H

#include <ScintillaHeaders.h>
#include <string_view>
#include <cstdlib>

class Clipboard {

Scintilla::SelectionText selText;
struct clipboard_c *cb;

void syncSetText(std::string_view);
const char* syncGetText();

public:

Clipboard();
~Clipboard();

template <class Func>
void copy(Func &&fillSel)
{
fillSel(selText);
syncSetText({selText.Data(), selText.Length()});
}

template <class Func>
void paste(Func &&fillSel)
{
auto *data = syncGetText();
std::string_view text;
if (data)
text = data;
else
text = {selText.Data(), selText.Length()};
fillSel(selText, text);
::free((void *) data);
}

};

#endif // TURBO_CLIPBOARD_H
34 changes: 25 additions & 9 deletions src/tscintilla.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,35 @@ bool TScintillaEditor::ModifyScrollBars(Sci::Line nMax, Sci::Line nPage)

void TScintillaEditor::Copy()
{
if (clipboard && !sel.Empty())
CopySelectionRange(clipboard);
if (clipboard && !sel.Empty()) {
clipboard->copy(
[this] (auto &selText) {
CopySelectionRange(&selText);
}
);
}
}

void TScintillaEditor::Paste()
{
if (clipboard && !clipboard->Empty()) {
ClearSelection(multiPasteMode == SC_MULTIPASTE_EACH);
InsertPasteShape( clipboard->Data(),
(int) clipboard->Length(),
clipboard->rectangular ? pasteRectangular
: pasteStream );
EnsureCaretVisible();
if (clipboard) {
clipboard->paste(
[this] (auto &selText, auto text) {
if (text.size()) {
ClearSelection(multiPasteMode == SC_MULTIPASTE_EACH);
selText.Copy( text,
pdoc->dbcsCodePage,
vs.styles[STYLE_DEFAULT].characterSet,
false,
true );
InsertPasteShape( selText.Data(),
selText.Length(),
selText.rectangular ? pasteRectangular
: pasteStream );
EnsureCaretVisible();
}
}
);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/tscintilla.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <ScintillaHeaders.h>
#include <string_view>

#include "clipboard.h"

struct DocumentView;
class TDrawSurface;

Expand All @@ -21,7 +23,7 @@ struct TScintillaEditor : public ScintillaBase {
friend struct DocumentView;

TScintillaWindow *parent {0};
SelectionText *clipboard;
Clipboard *clipboard;

TScintillaEditor();

Expand Down

0 comments on commit 469e279

Please sign in to comment.