From 864a22da64e402b0e8adc5584025e7ec8549e1ad Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Mon, 15 Jul 2019 15:39:53 -0300 Subject: [PATCH] Moving selection management methods to protocol library. --- src/clipboard/copy.c | 26 +++++++++++++------------- src/clipboard/selection.c | 84 +++++------------------------------------------------------------------------------- src/clipboard/text.c | 111 ++++++++++++++++++++++++++------------------------------------------------------------------------------------- src/include/clipboard.h | 16 ++-------------- src/include/internals.h | 2 +- src/include/terminal.h | 3 +-- src/terminal/widget.c | 2 +- 7 files changed, 49 insertions(+), 195 deletions(-) diff --git a/src/clipboard/copy.c b/src/clipboard/copy.c index c190e93..5bd99f4 100644 --- a/src/clipboard/copy.c +++ b/src/clipboard/copy.c @@ -30,8 +30,16 @@ #include #include - static void do_copy(v3270 *terminal) + static void do_copy(v3270 *terminal, gboolean cut) { + lib3270_selection * selection = lib3270_get_selection(terminal->host,cut); + + if(selection) + { + terminal->selection.blocks = g_list_append(terminal->selection.blocks,selection); + } + + /* // Get selection bounds. unsigned int row; unsigned int col; @@ -80,7 +88,7 @@ } terminal->selection.blocks = g_list_append(terminal->selection.blocks,selection); - + */ } LIB3270_EXPORT void v3270_copy_selection(GtkWidget *widget, V3270_SELECT_FORMAT format, gboolean cut) @@ -90,17 +98,13 @@ v3270 * terminal = GTK_V3270(widget); // Have data? Clear it? - v3270_clear_clipboard(terminal); + v3270_clear_selection(terminal); terminal->selection.format = format; - do_copy(terminal); + do_copy(terminal,cut); v3270_update_system_clipboard(widget); - if(cut) - { - lib3270_erase_selected(terminal->host); - } } LIB3270_EXPORT void v3270_append_selection(GtkWidget *widget, gboolean cut) @@ -109,13 +113,9 @@ v3270 * terminal = GTK_V3270(widget); - do_copy(terminal); + do_copy(terminal,cut); v3270_update_system_clipboard(widget); - if(cut) - { - lib3270_erase_selected(terminal->host); - } } diff --git a/src/clipboard/selection.c b/src/clipboard/selection.c index e59fcab..5db24c9 100644 --- a/src/clipboard/selection.c +++ b/src/clipboard/selection.c @@ -28,6 +28,7 @@ */ #include + #include /*--[ Implement ]------------------------------------------------------------------------------------*/ @@ -38,43 +39,11 @@ static void clipboard_clear(G_GNUC_UNUSED GtkClipboard *clipboard, G_GNUC_UNUSED if(!lib3270_get_toggle(terminal->host,LIB3270_TOGGLE_KEEP_SELECTED)) { v3270_unselect(GTK_WIDGET(obj)); - v3270_clear_clipboard(terminal); + v3270_clear_selection(terminal); } } -/// @brief Get formatted contents as single text. -static gchar * get_copy_as_text(v3270 * terminal) -{ - GList * element = terminal->selection.blocks; - GString * string = g_string_new(""); - - while(element) - { - struct selection * block = ((struct selection *) element->data); - unsigned int row, col, src = 0; - - for(row=0; row < block->bounds.height; row++) - { - for(col=0; colbounds.width; col++) - { - if(block->contents[src].attr & LIB3270_ATTR_SELECTED) - g_string_append_c(string,block->contents[src].chr); - - src++; - - } - g_string_append_c(string,'\n'); - } - - element = g_list_next(element); - } - - g_autofree char * text = g_string_free(string,FALSE); - return g_convert(text, -1, "UTF-8", lib3270_get_display_charset(terminal->host), NULL, NULL, NULL); - -} - static void clipboard_get(G_GNUC_UNUSED GtkClipboard *clipboard, GtkSelectionData *selection, guint target, GObject *obj) { v3270 * terminal = GTK_V3270(obj); @@ -85,7 +54,7 @@ static void clipboard_get(G_GNUC_UNUSED GtkClipboard *clipboard, GtkSelectionDa if(terminal->selection.blocks) { - g_autofree gchar * converted = get_copy_as_text(terminal); + g_autofree gchar * converted = v3270_get_copy_as_text(terminal); gtk_selection_data_set_text(selection,converted,-1); } @@ -102,11 +71,11 @@ static void clipboard_get(G_GNUC_UNUSED GtkClipboard *clipboard, GtkSelectionDa * @param terminal Pointer to the terminal Widget. * */ -void v3270_clear_clipboard(v3270 *terminal) +void v3270_clear_selection(v3270 *terminal) { if(terminal->selection.blocks) { - g_list_free_full(terminal->selection.blocks,g_free); + g_list_free_full(terminal->selection.blocks,(GDestroyNotify) lib3270_free); terminal->selection.blocks = NULL; } } @@ -136,49 +105,6 @@ LIB3270_EXPORT gchar * v3270_get_selected(GtkWidget *widget, gboolean cut) return NULL; } -LIB3270_EXPORT gchar * v3270_get_copy(GtkWidget *widget) -{ - g_return_val_if_fail(GTK_IS_V3270(widget),NULL); - return get_copy_as_text(GTK_V3270(widget)); -} - - /* -LIB3270_EXPORT void v3270_set_copy(GtkWidget *widget, const gchar *text) -{ - v3270 * terminal; - gchar * isotext; - - g_return_if_fail(GTK_IS_V3270(widget)); - - terminal = GTK_V3270(widget); - v3270_clear_clipboard(terminal); - - if(!text) - { - // No string, signal clipboard clear and return - g_signal_emit(widget,v3270_widget_signal[V3270_SIGNAL_CLIPBOARD], 0, FALSE); - return; - } - - // Received text, replace the selection buffer - terminal->selection.format = V3270_SELECT_TEXT; - isotext = g_convert(text, -1, lib3270_get_display_charset(terminal->host), "UTF-8", NULL, NULL, NULL); - - if(!isotext) - { - // No string, signal clipboard clear and return - g_signal_emit(widget,v3270_widget_signal[V3270_SIGNAL_CLIPBOARD], 0, FALSE); - return; - } - - terminal->selection.text = lib3270_strdup(isotext); - - g_free(isotext); - - g_signal_emit(widget,v3270_widget_signal[V3270_SIGNAL_CLIPBOARD], 0, TRUE); -} - */ - void v3270_update_system_clipboard(GtkWidget *widget) { v3270 * terminal = GTK_V3270(widget); diff --git a/src/clipboard/text.c b/src/clipboard/text.c index e195419..8f75c3d 100644 --- a/src/clipboard/text.c +++ b/src/clipboard/text.c @@ -28,109 +28,43 @@ */ #include + #include +/*--[ Implement ]------------------------------------------------------------------------------------*/ -const char * v3270_update_selected_text(GtkWidget *widget, gboolean cut) +/// @brief Get formatted contents as single text. +gchar * v3270_get_copy_as_text(v3270 * terminal) { - /* - char * text; - v3270 * terminal = GTK_V3270(widget); + GList * element = terminal->selection.blocks; + GString * string = g_string_new(""); - v3270_clear_clipboard(terminal); - - if(cut) - text = lib3270_cut_selected(terminal->host); - else - text = lib3270_get_selected(terminal->host); - - if(!text) - { - g_signal_emit(widget,v3270_widget_signal[V3270_SIGNAL_CLIPBOARD], 0, FALSE); - lib3270_ring_bell(terminal->host); - return NULL; - } - - if(terminal->selection.format == V3270_SELECT_TABLE) + while(element) { - // Convert text to table - gchar **ln = g_strsplit(text,"\n",-1); - int width = lib3270_get_width(terminal->host); - gboolean cols[width]; - int l; - GString * buffer; + lib3270_selection * block = ((lib3270_selection *) element->data); + unsigned int row, col, src = 0; - memset(cols,0,sizeof(gboolean)*width); - - // Find column delimiters - for(l=0;ln[l];l++) + for(row=0; row < block->bounds.height; row++) { - int c; - gchar * ptr = ln[l]; - - for(c=0;cbounds.width; col++) { - if(!g_ascii_isspace(*ptr)) - cols[c] = TRUE; + if(block->contents[src].flags & LIB3270_ATTR_SELECTED) + g_string_append_c(string,block->contents[src].chr); - ptr++; - } - - } - - // Read screen contents - buffer = g_string_sized_new(strlen(text)); - for(l=0;ln[l];l++) - { - int col = 0; - gchar * src = ln[l]; - - while(col < width && *src) - { - if(col) - g_string_append_c(buffer,'\t'); - - // Find column start - while(!cols[col] && col < width && *src) - { - col++; - src++; - } - - if(col < width && *src) - { - gchar tmp[width+1]; - gchar * dst = tmp; - - // Copy column content - while(cols[col] && col < width && *src) - { - *dst = *src; - col++; - dst++; - src++; - } - *dst = 0; - g_string_append(buffer,g_strstrip(tmp)); - } + src++; } - g_string_append_c(buffer,'\n'); - + g_string_append_c(string,'\n'); } - g_strfreev(ln); - g_free(text); - - text = g_string_free(buffer,FALSE); + element = g_list_next(element); } - return terminal->selection.text = text; - */ - - return NULL; + g_autofree char * text = g_string_free(string,FALSE); + return g_convert(text, -1, "UTF-8", lib3270_get_display_charset(terminal->host), NULL, NULL, NULL); } + LIB3270_EXPORT void v3270_paste_text(GtkWidget *widget, const gchar *text, const gchar *encoding) { gchar * buffer = NULL; @@ -254,3 +188,10 @@ LIB3270_EXPORT void v3270_paste_text(GtkWidget *widget, const gchar *text, const g_signal_emit(widget,v3270_widget_signal[V3270_SIGNAL_PASTENEXT], 0, next); } + +LIB3270_EXPORT gchar * v3270_get_copy(GtkWidget *widget) +{ + g_return_val_if_fail(GTK_IS_V3270(widget),NULL); + return v3270_get_copy_as_text(GTK_V3270(widget)); +} + diff --git a/src/include/clipboard.h b/src/include/clipboard.h index 4c5e1ac..3c5dd1b 100644 --- a/src/include/clipboard.h +++ b/src/include/clipboard.h @@ -45,23 +45,11 @@ CLIPBOARD_TYPE_TEXT, }; - struct selection - { - struct { - unsigned int row; - unsigned int col; - unsigned int width; - unsigned int height; - - } bounds; ///< @brief Clipboard rectangle. - - struct v3270_character contents[1]; - - }; - G_GNUC_INTERNAL void v3270_update_system_clipboard(GtkWidget *widget); G_GNUC_INTERNAL const char * v3270_update_selected_text(GtkWidget *widget, gboolean cut); + /// @brief Get formatted contents as single text. + G_GNUC_INTERNAL gchar * v3270_get_copy_as_text(v3270 * terminal); #endif // V3270_CLIPBOARD_H_INCLUDED diff --git a/src/include/internals.h b/src/include/internals.h index 2ab5983..2f91781 100644 --- a/src/include/internals.h +++ b/src/include/internals.h @@ -174,7 +174,7 @@ G_GNUC_INTERNAL void v3270_draw_alt_status(v3270 *terminal); G_GNUC_INTERNAL void v3270_draw_ins_status(v3270 *terminal); - G_GNUC_INTERNAL void v3270_clear_clipboard(v3270 *terminal); + G_GNUC_INTERNAL void v3270_clear_selection(v3270 *terminal); G_GNUC_INTERNAL void v3270_update_cursor_surface(v3270 *widget,unsigned char chr,unsigned short attr); diff --git a/src/include/terminal.h b/src/include/terminal.h index 7841508..5921fa7 100644 --- a/src/include/terminal.h +++ b/src/include/terminal.h @@ -119,8 +119,7 @@ G_BEGIN_DECLS int baddr; ///< @brief Selection address. V3270_SELECT_FORMAT format; ///< @brief Copy format. - - GList * blocks; ///< @brief Selection blocks. + GList * blocks; ///< @brief Selection blocks. } selection; diff --git a/src/terminal/widget.c b/src/terminal/widget.c index c7900a3..a28fe83 100644 --- a/src/terminal/widget.c +++ b/src/terminal/widget.c @@ -660,7 +660,7 @@ static void v3270_destroy(GtkWidget *widget) terminal->input_method = NULL; } - v3270_clear_clipboard(terminal); + v3270_clear_selection(terminal); if(terminal->session_name) { -- libgit2 0.21.2