From 83cb7db35174e5a739f31c79b104e0fe7ac2e1a0 Mon Sep 17 00:00:00 2001 From: perry.werneck@gmail.com Date: Tue, 28 May 2013 19:32:45 +0000 Subject: [PATCH] Conteúdo do clipboard passa a ficar guardado no widget no formato original da lib3270 para permitir a futura implementação de cópia com formatação --- private.h | 6 +++++- selection.c | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------------- widget.c | 6 +----- 3 files changed, 85 insertions(+), 87 deletions(-) diff --git a/private.h b/private.h index 724d5bf..28ef92d 100644 --- a/private.h +++ b/private.h @@ -152,7 +152,11 @@ G_BEGIN_DECLS GSource * timer; GtkIMContext * input_method; unsigned short keyflags; - gchar * clipboard; /**< Clipboard contents (text only) */ + + struct + { + char * text; /**< Clipboard contents (lib3270 charset) */ + } selection; LIB3270_CURSOR pointer_id; unsigned char pointer; /**< Mouse pointer ID */ diff --git a/selection.c b/selection.c index db56456..6028296 100644 --- a/selection.c +++ b/selection.c @@ -63,11 +63,17 @@ static void clipboard_get(GtkClipboard *clipboard, GtkSelectionData *selection, switch(target) { - case CLIPBOARD_TYPE_TEXT: - if(!widget->clipboard) + case CLIPBOARD_TYPE_TEXT: /* Get clipboard contents as text */ + if(!widget->selection.text) + { lib3270_ring_bell(widget->host); + } else - gtk_selection_data_set_text(selection,widget->clipboard,-1); + { + gchar * text = g_convert(widget->selection.text, -1, "UTF-8", lib3270_get_charset(widget->host), NULL, NULL, NULL); + gtk_selection_data_set_text(selection,text,-1); + g_free(text); + } break; default: @@ -96,46 +102,11 @@ gchar * v3270_get_text(GtkWidget *widget, int offset, int len) return text; } -/** - * Get lib3270 selection as a g_malloc buffer. - * - * @param widget Widget containing the desired section. - * - * @return NULL if error, otherwise the selected buffer contents (release with g_free). - * - */ /* -static gchar * v3270_get_selected(v3270 *widget) -{ - gchar *text = lib3270_get_selected(widget->host); - if(text) - { - gchar *str = g_strdup(text); - lib3270_free(text); - return str; - } - return NULL; -} - -static gchar * v3270_cut_selected(v3270 *widget) -{ - gchar *text = lib3270_cut_selected(widget->host); - if(text) - { - gchar *str = g_strdup(text); - lib3270_free(text); - return str; - } - return NULL; -} -*/ - -gchar * v3270_get_selected(GtkWidget *widget, gboolean cut) +static const char * update_selected_text(GtkWidget *widget, gboolean cut) { v3270 * terminal; char * text; - g_return_val_if_fail(GTK_IS_V3270(widget),NULL); - terminal = GTK_V3270(widget); v3270_clear_clipboard(terminal); @@ -227,32 +198,77 @@ gchar * v3270_get_selected(GtkWidget *widget, gboolean cut) text = g_string_free(buffer,FALSE); } - terminal->clipboard = g_convert(text, -1, "UTF-8", lib3270_get_charset(terminal->host), NULL, NULL, NULL); + return terminal->selection.text = text; - lib3270_free(text); +} +/** + * Get lib3270 selection as a g_malloc buffer. + * + * @param widget Widget containing the desired section. + * + * @return NULL if error, otherwise the selected buffer contents (release with g_free). + * + */ +gchar * v3270_get_selected(GtkWidget *widget, gboolean cut) +{ + const char *text; + + g_return_val_if_fail(GTK_IS_V3270(widget),NULL); - return g_strdup(terminal->clipboard); + text = update_selected_text(widget,cut); + + if(text) + return g_convert(text, -1, "UTF-8", lib3270_get_charset(GTK_V3270(widget)->host), NULL, NULL, NULL); + + return NULL; } gchar * v3270_get_copy(GtkWidget *widget) { + const char *text; g_return_val_if_fail(GTK_IS_V3270(widget),NULL); - return g_strdup(GTK_V3270(widget)->clipboard); + + text = update_selected_text(widget,FALSE); + + if(text) + return g_convert(text, -1, "UTF-8", lib3270_get_charset(GTK_V3270(widget)->host), NULL, NULL, NULL); + + return NULL; +} + +static void update_system_clipboard(GtkWidget *widget) +{ + if(GTK_V3270(widget)->selection.text) + { + GtkClipboard * clipboard = gtk_widget_get_clipboard(widget,GDK_SELECTION_CLIPBOARD); + + if(gtk_clipboard_set_with_owner( clipboard, + targets, + G_N_ELEMENTS(targets), + (GtkClipboardGetFunc) clipboard_get, + (GtkClipboardClearFunc) clipboard_clear, + G_OBJECT(widget) + )) + { + gtk_clipboard_set_can_store(clipboard,targets,1); + } + + g_signal_emit(widget,v3270_widget_signal[SIGNAL_CLIPBOARD], 0, TRUE); + } } + void v3270_copy_append(GtkWidget *widget) { v3270 * terminal; char * str; - gchar * text; - gchar * clip; g_return_if_fail(GTK_IS_V3270(widget)); terminal = GTK_V3270(widget); - if(!terminal->clipboard) + if(!terminal->selection.text) { // Clipboard is empty, do a single copy v3270_copy(widget, V3270_SELECT_TEXT, FALSE); @@ -261,54 +277,36 @@ void v3270_copy_append(GtkWidget *widget) str = lib3270_get_selected(terminal->host); - if(!str) - return; - - text = g_convert(str, -1, "UTF-8", lib3270_get_charset(terminal->host), NULL, NULL, NULL); - - lib3270_free(str); + if(str) + { + size_t len = strlen(terminal->selection.text)+strlen(str)+2; - clip = g_strconcat(terminal->clipboard,"\n",text,NULL); + terminal->selection.text = lib3270_realloc(terminal->selection.text,len); - g_free(text); - v3270_clear_clipboard(terminal); + strncat(terminal->selection.text,"\n",len); + strncat(terminal->selection.text,str,len); - terminal->clipboard = clip; + lib3270_free(str); + } - gtk_clipboard_set_text(gtk_widget_get_clipboard(widget,GDK_SELECTION_CLIPBOARD),terminal->clipboard,-1); +/* + text = g_convert(terminal->selection.text, -1, "UTF-8", lib3270_get_charset(terminal->host), NULL, NULL, NULL); + gtk_clipboard_set_text(gtk_widget_get_clipboard(widget,GDK_SELECTION_CLIPBOARD),text,-1); + g_free(text); g_signal_emit(widget,v3270_widget_signal[SIGNAL_CLIPBOARD], 0, TRUE); +*/ + + update_system_clipboard(widget); } void v3270_copy(GtkWidget *widget, V3270_SELECT_FORMAT mode, gboolean cut) { - gchar * text; - GtkClipboard * clipboard = gtk_widget_get_clipboard(widget,GDK_SELECTION_CLIPBOARD); - g_return_if_fail(GTK_IS_V3270(widget)); - GTK_V3270(widget)->table = (mode == V3270_SELECT_TABLE ? 1 : 0); - - text = v3270_get_selected(widget,cut); - - if(text) - { - if(gtk_clipboard_set_with_owner( clipboard, - targets, - G_N_ELEMENTS(targets), - (GtkClipboardGetFunc) clipboard_get, - (GtkClipboardClearFunc) clipboard_clear, - G_OBJECT(widget) - )) - { - gtk_clipboard_set_can_store(clipboard,targets,1); - } - - g_signal_emit(widget,v3270_widget_signal[SIGNAL_CLIPBOARD], 0, TRUE); - g_free(text); - } - + update_selected_text(widget,cut); + update_system_clipboard(widget); } void v3270_paste_string(GtkWidget *widget, const gchar *text, const gchar *encoding) diff --git a/widget.c b/widget.c index ed79e14..02aa1f6 100644 --- a/widget.c +++ b/widget.c @@ -897,11 +897,7 @@ GtkWidget * v3270_new(void) void v3270_clear_clipboard(v3270 *terminal) { - if(terminal->clipboard) - { - g_free(terminal->clipboard); - terminal->clipboard = NULL; - } + terminal->selection.text = lib3270_free(terminal->selection.text); } #if GTK_CHECK_VERSION(3,0,0) -- libgit2 0.21.2