Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement/text clipboard #1

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 42 additions & 40 deletions src/g_editor.c
Expand Up @@ -38,11 +38,6 @@ struct _instanceeditor

/* positional offset for duplicated items */
#define PASTE_OFFSET 10
#define CLIPBOARD_PATCH_TEXT_START 0
#define CLIPBOARD_PATCH_TEXT_LINE_END 1
#define CLIPBOARD_PATCH_TEXT_END 2
#define CLIPBOARD_PATCH_TEXT_LINE_PARTIAL 3
#define CLIPBOARD_PATCH_TEXT_LINE_END_APPENDIX 4

void glist_readfrombinbuf(t_glist *x, const t_binbuf *b, const char *filename,
int selectem);
Expand Down Expand Up @@ -4140,45 +4135,53 @@ static void canvas_paste(t_canvas *x)
}
}

static t_binbuf *clipboard_patch_bb = NULL;
static char* clipboard_patch_text = NULL;
static size_t clipboard_patch_len = 0;

void canvas_got_clipboard_contents(t_canvas *x, t_floatarg flagf, t_symbol *s) {
int flag = flagf;
void canvas_got_clipboard_contents(t_canvas *x, t_symbol*s, int argc, t_atom*argv) {
t_symbol*sflag = (argc>0)?atom_getsymbol(argv):0;
int flag = 0;
int i;
if(sflag == gensym("begin")) {
flag = 0;
} else if(sflag == gensym("end")) {
flag = 1;
} else if(sflag == gensym("addbytes")) {
flag = 2;
} else {
flag = 0;
}

switch (flag) {
case CLIPBOARD_PATCH_TEXT_START:
if (clipboard_patch_bb) {
binbuf_free(clipboard_patch_bb);
case 0:
if (clipboard_patch_text) {
freebytes(clipboard_patch_text, clipboard_patch_len);
}
clipboard_patch_bb = binbuf_new();
clipboard_patch_text = NULL;
clipboard_patch_len = 0;
break;
case CLIPBOARD_PATCH_TEXT_LINE_PARTIAL:
case CLIPBOARD_PATCH_TEXT_LINE_END:
case CLIPBOARD_PATCH_TEXT_LINE_END_APPENDIX:
if (clipboard_patch_bb) {
t_binbuf *line_bb = binbuf_new();
case 1:
if (clipboard_patch_text) {
t_binbuf *temp_bb = binbuf_new();
if (flag == CLIPBOARD_PATCH_TEXT_LINE_END_APPENDIX) {
t_atom a;
SETCOMMA(&a);
binbuf_add(line_bb, 1, &a);
}
binbuf_text(temp_bb, s->s_name, strlen(s->s_name));
if (flag == CLIPBOARD_PATCH_TEXT_LINE_END || flag == CLIPBOARD_PATCH_TEXT_LINE_END_APPENDIX) {
binbuf_addsemi(temp_bb);
}
binbuf_add(line_bb, binbuf_getnatom(temp_bb), binbuf_getvec(temp_bb));
binbuf_add(clipboard_patch_bb, binbuf_getnatom(line_bb), binbuf_getvec(line_bb));
binbuf_clear(line_bb);
binbuf_clear(temp_bb);
binbuf_free(line_bb);
binbuf_free(temp_bb);
binbuf_text(temp_bb, clipboard_patch_text, clipboard_patch_len);
canvas_dopaste(x, temp_bb);
freebytes(clipboard_patch_text, clipboard_patch_len);
}
clipboard_patch_text = NULL;
clipboard_patch_len = 0;
break;
case CLIPBOARD_PATCH_TEXT_END:
if (clipboard_patch_bb) {
canvas_dopaste(x, clipboard_patch_bb);
binbuf_free(clipboard_patch_bb);
clipboard_patch_bb = NULL;

case 2:
if (clipboard_patch_text) {
clipboard_patch_text = resizebytes(clipboard_patch_text, clipboard_patch_len, clipboard_patch_len + argc);
} else {
clipboard_patch_text = getbytes(argc);
clipboard_patch_len = 0;
}
for(i=1; i<argc; i++) {
int v = (int)atom_getfloat(argv+i);
clipboard_patch_text[clipboard_patch_len] = (char)v;
clipboard_patch_len++;
}
break;
default:
Expand Down Expand Up @@ -4980,7 +4983,8 @@ void g_editor_setup(void)
A_GIMME, A_NULL);
class_addmethod(canvas_class, (t_method)canvas_motion, gensym("motion"),
A_FLOAT, A_FLOAT, A_FLOAT, A_NULL);

class_addmethod(canvas_class, (t_method)canvas_got_clipboard_contents,
gensym("got-clipboard-contents"), A_GIMME, A_NULL);
/* ------------------------ menu actions ---------------------------- */
class_addmethod(canvas_class, (t_method)canvas_menuclose,
gensym("menuclose"), A_DEFFLOAT, 0);
Expand All @@ -4990,8 +4994,6 @@ void g_editor_setup(void)
gensym("copy"), A_NULL);
class_addmethod(canvas_class, (t_method)canvas_copy_to_clipboard_as_text,
gensym("copy-to-clipboard-as-text"), A_NULL);
class_addmethod(canvas_class, (t_method)canvas_got_clipboard_contents,
gensym("got-clipboard-contents"), A_FLOAT, A_DEFSYMBOL, A_NULL);
class_addmethod(canvas_class, (t_method)canvas_paste,
gensym("paste"), A_NULL);
class_addmethod(canvas_class, (t_method)canvas_paste_replace,
Expand Down
39 changes: 8 additions & 31 deletions tcl/pdtk_canvas.tcl
Expand Up @@ -251,12 +251,8 @@ proc pdtk_canvas_clickpaste {tkcanvas x y b} {
}

proc ::pdtk_canvas::pdtk_get_clipboard_text {tkcanvas} {
set CLIPBOARD_PATCH_TEXT_START 0;
set CLIPBOARD_PATCH_TEXT_LINE_END 1;
set CLIPBOARD_PATCH_TEXT_END 2;
set CLIPBOARD_PATCH_TEXT_LINE_PARTIAL 3;
set CLIPBOARD_PATCH_TEXT_LINE_END_APPENDIX 4;
set MAX_CHUNK_SIZE 960;
set MAX_CHUNK_SIZE 960
set toplevel [winfo toplevel $tkcanvas]
set clipboard_data [clipboard get]
if {[string length $clipboard_data] == 0} {
::pdwindow::post "Clipboard is empty.\n"
Expand All @@ -268,32 +264,13 @@ proc ::pdtk_canvas::pdtk_get_clipboard_text {tkcanvas} {
::pdwindow::post $clipboard_data
return
}
pdsend "[winfo toplevel $tkcanvas] got-clipboard-contents $CLIPBOARD_PATCH_TEXT_START"
foreach line [split $clipboard_data \n] {
if {[string length $line] > 0} {
set escaped_line [string range $line 0 end-1]
set escaped_line [string map {" " {\ } ";" {\\;} "," {\\,} "\$" {\\$}} $escaped_line]
# split each line into chunks, and make sure atoms don't get split
while {[string length $escaped_line] > $MAX_CHUNK_SIZE} {
set boundary [string last { } [string range $escaped_line 0 [expr $MAX_CHUNK_SIZE - 1]]]
set chunk [string range $escaped_line 0 $boundary]
pdsend "[winfo toplevel $tkcanvas] got-clipboard-contents $CLIPBOARD_PATCH_TEXT_LINE_PARTIAL $chunk"
set escaped_line [string range $escaped_line [expr $boundary + 1] end]
}
set appendix_part ""
# find appendix part, i.e. ", f 12" denoting object width in chars, extract and remove
if {[regexp {\,\\\s(f\\\s\d+)$} $escaped_line -> appendix_part]} {
set escaped_line [regsub {\,\\\sf\\\s\d+$} $escaped_line ""]
# remove trailing slash
set escaped_line [string range $escaped_line 0 end-2]
pdsend "[winfo toplevel $tkcanvas] got-clipboard-contents $CLIPBOARD_PATCH_TEXT_LINE_PARTIAL $escaped_line"
pdsend "[winfo toplevel $tkcanvas] got-clipboard-contents $CLIPBOARD_PATCH_TEXT_LINE_END_APPENDIX $appendix_part"
} else {
pdsend "[winfo toplevel $tkcanvas] got-clipboard-contents $CLIPBOARD_PATCH_TEXT_LINE_END $escaped_line"
}
}
pdsend "$toplevel got-clipboard-contents begin"
set output {}
foreach char [split $clipboard_data ""] {
lappend output [scan $char %c]
}
pdsend "[winfo toplevel $tkcanvas] got-clipboard-contents $CLIPBOARD_PATCH_TEXT_END"
pdsend "$toplevel got-clipboard-contents addbytes $output"
pdsend "$toplevel got-clipboard-contents end"
}

proc pdtk_copy_to_clipboard_as_text {tkcanvas args} {
Expand Down