From 9699327abfe05121306b1f3519786f056e791630 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Fri, 12 Jul 2019 17:04:59 -0300 Subject: [PATCH] Copy as text seens ok in the new clipboard system. --- src/clipboard/copy.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/clipboard/selection.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------- src/clipboard/text.c | 6 ++++++ src/include/internals.h | 10 +++++++++- src/include/terminal.h | 25 ++++++++++++++++++++++--- src/include/v3270.h | 6 ++++-- src/terminal/draw.c | 60 ++++++++++++++++++------------------------------------------ src/trace/exec.c | 5 +++-- v3270.cbp | 3 +++ 9 files changed, 255 insertions(+), 108 deletions(-) create mode 100644 src/clipboard/copy.c diff --git a/src/clipboard/copy.c b/src/clipboard/copy.c new file mode 100644 index 0000000..ffebbe9 --- /dev/null +++ b/src/clipboard/copy.c @@ -0,0 +1,86 @@ +/* + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a + * aplicativos mainframe. Registro no INPI sob o nome G3270. + * + * Copyright (C) <2008> + * + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela + * Free Software Foundation. + * + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para + * obter mais detalhes. + * + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin + * St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Este programa está nomeado como - e possui - linhas de código. + * + * Contatos: + * + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) + * + */ + + #include + #include + + LIB3270_EXPORT void v3270_copy(GtkWidget *widget, V3270_SELECT_FORMAT format, gboolean cut) + { + + g_return_if_fail(GTK_IS_V3270(widget)); + + v3270 * terminal = GTK_V3270(widget); + + terminal->selection.format = format; + + // Have data? Clear it? + v3270_clear_clipboard(terminal); + + // Get selection bounds. + + if(lib3270_get_selection_rectangle(terminal->host, &terminal->selection.bounds.row, &terminal->selection.bounds.col, &terminal->selection.bounds.width, &terminal->selection.bounds.height) != 0) + return; + + debug("Selecion rectangle starts on %u,%u with size of %ux%u", + terminal->selection.bounds.row, terminal->selection.bounds.col, + terminal->selection.bounds.width, terminal->selection.bounds.height + ); + + + // Copy terminal buffer + unsigned int r, c; + + terminal->selection.contents = g_new0(struct v3270_character,(terminal->selection.bounds.width * terminal->selection.bounds.height)); + + int pos = 0; + for(r=0;r < terminal->selection.bounds.height; r++) + { + // Get starting address. + int baddr = lib3270_translate_to_address(terminal->host, terminal->selection.bounds.row+r+1, terminal->selection.bounds.col+1); + if(baddr < 0) + { + g_message("Can't convert coordinate %u,%d",terminal->selection.bounds.row+r+1,terminal->selection.bounds.col+1); + gdk_display_beep(gdk_display_get_default()); + return; + } + + for(c=0;c < terminal->selection.bounds.width; c++) + { + lib3270_get_contents(terminal->host,baddr,baddr,&terminal->selection.contents[pos].chr,&terminal->selection.contents[pos].attr); + debug("pos=%d baddr=%u char=%c",pos,baddr,terminal->selection.contents[pos].chr); + pos++; + baddr++; + } + + } + + v3270_update_system_clipboard(widget); + + } + diff --git a/src/clipboard/selection.c b/src/clipboard/selection.c index d097d1a..a884b4a 100644 --- a/src/clipboard/selection.c +++ b/src/clipboard/selection.c @@ -29,41 +29,63 @@ #include -/*--[ Globals ]--------------------------------------------------------------------------------------*/ - -/* - static const GtkTargetEntry targets[] = - { - { "COMPOUND_TEXT", 0, CLIPBOARD_TYPE_TEXT }, - { "UTF8_STRING", 0, CLIPBOARD_TYPE_TEXT }, - }; -*/ - /*--[ Implement ]------------------------------------------------------------------------------------*/ static void clipboard_clear(G_GNUC_UNUSED GtkClipboard *clipboard, G_GNUC_UNUSED GObject *obj) { + v3270 * terminal = GTK_V3270(obj); + + if(!lib3270_get_toggle(terminal->host,LIB3270_TOGGLE_KEEP_SELECTED)) + { + v3270_unselect(GTK_WIDGET(obj)); + v3270_clear_clipboard(terminal); + } + +} + +/// @brief Get formatted contents as single text. +static gchar * get_selection_as_text(v3270 * terminal) +{ + // Has formatted clipboard, convert it to text. + unsigned int r, c, src = 0, dst = 0; + g_autofree char * text = g_malloc0( (terminal->selection.bounds.width * terminal->selection.bounds.height) + terminal->selection.bounds.height + 2); + + for(r=0;r < terminal->selection.bounds.height;r++) + { + for(c=0;cselection.bounds.width;c++) + { + if(terminal->selection.contents[src].attr & LIB3270_ATTR_SELECTED) + text[dst++] = terminal->selection.contents[src].chr; + + src++; + } + + text[dst++] = '\n'; + } + + 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 * widget = GTK_V3270(obj); + v3270 * terminal = GTK_V3270(obj); - debug("%s target=%u",__FUNCTION__,(unsigned int) target); + debug("%s target=%u selection from with %ux%u", + __FUNCTION__, + (unsigned int) target, + terminal->selection.bounds.width, terminal->selection.bounds.height + ); switch(target) { case CLIPBOARD_TYPE_TEXT: /* Get clipboard contents as text */ - if(!widget->selection.text) - { - lib3270_ring_bell(widget->host); - } - else - { - gchar * text = g_convert(widget->selection.text, -1, "UTF-8", lib3270_get_display_charset(widget->host), NULL, NULL, NULL); - gtk_selection_data_set_text(selection,text,-1); - g_free(text); - } + + if(terminal->selection.contents) + { + g_autofree gchar * converted = get_selection_as_text(terminal); + gtk_selection_data_set_text(selection,converted,-1); + } + break; default: @@ -79,7 +101,15 @@ static void clipboard_get(G_GNUC_UNUSED GtkClipboard *clipboard, GtkSelectionDa */ void v3270_clear_clipboard(v3270 *terminal) { - terminal->selection.text = lib3270_free(terminal->selection.text); + memset(&terminal->selection.bounds,0,sizeof(terminal->selection.bounds)); + + if(terminal->selection.contents) + { + g_free(terminal->selection.contents); + terminal->selection.contents = NULL; + } + +// terminal->selection.text = lib3270_free(terminal->selection.text); } /** @@ -106,6 +136,7 @@ LIB3270_EXPORT gchar * v3270_get_selected(GtkWidget *widget, gboolean cut) LIB3270_EXPORT gchar * v3270_get_copy(GtkWidget *widget) { + /* const char *text; g_return_val_if_fail(GTK_IS_V3270(widget),NULL); @@ -117,11 +148,14 @@ LIB3270_EXPORT gchar * v3270_get_copy(GtkWidget *widget) if(text) return g_convert(text, -1, "UTF-8", lib3270_get_display_charset(GTK_V3270(widget)->host), NULL, NULL, NULL); + */ + return NULL; } LIB3270_EXPORT void v3270_set_copy(GtkWidget *widget, const gchar *text) { + /* v3270 * terminal; gchar * isotext; @@ -132,18 +166,18 @@ LIB3270_EXPORT void v3270_set_copy(GtkWidget *widget, const gchar *text) if(!text) { - /* No string, signal clipboard clear and return */ + // 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 */ + // 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 */ + // No string, signal clipboard clear and return g_signal_emit(widget,v3270_widget_signal[V3270_SIGNAL_CLIPBOARD], 0, FALSE); return; } @@ -153,59 +187,71 @@ LIB3270_EXPORT void v3270_set_copy(GtkWidget *widget, const gchar *text) g_free(isotext); g_signal_emit(widget,v3270_widget_signal[V3270_SIGNAL_CLIPBOARD], 0, TRUE); + */ } void v3270_update_system_clipboard(GtkWidget *widget) { - if(GTK_V3270(widget)->selection.text) - { - GtkClipboard * clipboard = gtk_widget_get_clipboard(widget,GDK_SELECTION_CLIPBOARD); + v3270 * terminal = GTK_V3270(widget); + + if(!terminal->selection.bounds.width) + { + // No clipboard data, return. + g_signal_emit(widget,v3270_widget_signal[V3270_SIGNAL_CLIPBOARD], 0, FALSE); + return; + } + + // Has clipboard data, inform system. + GtkClipboard * clipboard = gtk_widget_get_clipboard(widget,GDK_SELECTION_CLIPBOARD); - // Create target list - // - // Reference: https://cpp.hotexamples.com/examples/-/-/g_list_insert_sorted/cpp-g_list_insert_sorted-function-examples.html - // - GtkTargetList * list = gtk_target_list_new(NULL, 0); - GtkTargetEntry * targets; - int n_targets; + // Create target list + // + // Reference: https://cpp.hotexamples.com/examples/-/-/g_list_insert_sorted/cpp-g_list_insert_sorted-function-examples.html + // + GtkTargetList * list = gtk_target_list_new(NULL, 0); + GtkTargetEntry * targets; + int n_targets; + + gtk_target_list_add_text_targets(list, CLIPBOARD_TYPE_TEXT); + targets = gtk_target_table_new_from_list(list, &n_targets); - gtk_target_list_add_text_targets(list, CLIPBOARD_TYPE_TEXT); - targets = gtk_target_table_new_from_list(list, &n_targets); #ifdef DEBUG - { - int ix; - for(ix = 0; ix < n_targets; ix++) { - debug("target(%d)=\"%s\"",ix,targets[ix].target); - } + { + int ix; + for(ix = 0; ix < n_targets; ix++) { + debug("target(%d)=\"%s\"",ix,targets[ix].target); } + } #endif // DEBUG - if(gtk_clipboard_set_with_owner( - clipboard, - targets, - n_targets, - (GtkClipboardGetFunc) clipboard_get, - (GtkClipboardClearFunc) clipboard_clear, - G_OBJECT(widget) - )) - { - gtk_clipboard_set_can_store(clipboard,targets,1); - } + if(gtk_clipboard_set_with_owner( + clipboard, + targets, + n_targets, + (GtkClipboardGetFunc) clipboard_get, + (GtkClipboardClearFunc) clipboard_clear, + G_OBJECT(widget) + )) + { + gtk_clipboard_set_can_store(clipboard,targets,1); + } - gtk_target_table_free(targets, n_targets); - gtk_target_list_unref(list); + gtk_target_table_free(targets, n_targets); + gtk_target_list_unref(list); + + g_signal_emit(widget,v3270_widget_signal[V3270_SIGNAL_CLIPBOARD], 0, TRUE); - g_signal_emit(widget,v3270_widget_signal[V3270_SIGNAL_CLIPBOARD], 0, TRUE); - } } LIB3270_EXPORT void v3270_copy_text(GtkWidget *widget, V3270_SELECT_FORMAT mode, gboolean cut) { + /* g_return_if_fail(GTK_IS_V3270(widget)); GTK_V3270(widget)->selection.format = mode; v3270_update_selected_text(widget,cut); v3270_update_system_clipboard(widget); + */ } LIB3270_EXPORT void v3270_unselect(GtkWidget *widget) diff --git a/src/clipboard/text.c b/src/clipboard/text.c index ad7b805..5351d8d 100644 --- a/src/clipboard/text.c +++ b/src/clipboard/text.c @@ -32,6 +32,7 @@ LIB3270_EXPORT void v3270_copy_text_append(GtkWidget *widget) { + /* v3270 * terminal; char * str; @@ -61,11 +62,13 @@ LIB3270_EXPORT void v3270_copy_text_append(GtkWidget *widget) } v3270_update_system_clipboard(widget); + */ } const char * v3270_update_selected_text(GtkWidget *widget, gboolean cut) { + /* char * text; v3270 * terminal = GTK_V3270(widget); @@ -158,6 +161,9 @@ const char * v3270_update_selected_text(GtkWidget *widget, gboolean cut) } return terminal->selection.text = text; + */ + + return NULL; } diff --git a/src/include/internals.h b/src/include/internals.h index 95c2cfe..2ab5983 100644 --- a/src/include/internals.h +++ b/src/include/internals.h @@ -53,6 +53,14 @@ G_BEGIN_DECLS +/*--[ Structures ]-----------------------------------------------------------------------------------*/ + + struct v3270_character + { + unsigned char chr; ///< @brief Character value. + unsigned short attr; ///< @brief Character attribute. + }; + /*--[ Signals ]--------------------------------------------------------------------------------------*/ /// @brief V3270 Signal list @@ -187,7 +195,7 @@ G_GNUC_INTERNAL void v3270_update_font_metrics(v3270 *terminal, cairo_t *cr, unsigned int width, unsigned int height); - G_GNUC_INTERNAL void v3270_update_cursor_rect(v3270 *widget, GdkRectangle *rect, unsigned char chr, unsigned short attr); + G_GNUC_INTERNAL void v3270_update_cursor_rect(v3270 *widget, GdkRectangle *rect, const struct v3270_character *element); G_GNUC_INTERNAL void v3270_update_message(v3270 *widget, LIB3270_MESSAGE id); G_GNUC_INTERNAL void v3270_update_cursor(H3270 *session, unsigned short row, unsigned short col, unsigned char c, unsigned short attr); diff --git a/src/include/terminal.h b/src/include/terminal.h index 0759344..bf4b52c 100644 --- a/src/include/terminal.h +++ b/src/include/terminal.h @@ -115,12 +115,31 @@ G_BEGIN_DECLS GtkIMContext * input_method; unsigned short keyflags; + struct { + + int baddr; ///< @brief Selection address. + V3270_SELECT_FORMAT format; ///< @brief Copy format. + + struct { + unsigned int row; + unsigned int col; + unsigned int width; + unsigned int height; + + } bounds; ///< @brief Clipboard rectangle. + + struct v3270_character * contents; + + } selection; + + /* struct { - V3270_SELECT_FORMAT format; /**< Copy format */ - char * text; /**< Clipboard contents (lib3270 charset) */ - int baddr; /**< Selection addr */ + V3270_SELECT_FORMAT format; ///< Copy format + char * text; ///< Clipboard contents (lib3270 charset) + int baddr; ///< Selection addr } selection; + */ LIB3270_POINTER pointer_id; unsigned char pointer; /**< Mouse pointer ID */ diff --git a/src/include/v3270.h b/src/include/v3270.h index e3de2a5..5b9e62e 100644 --- a/src/include/v3270.h +++ b/src/include/v3270.h @@ -183,8 +183,9 @@ // Clipboard typedef enum _v3270_select_format { - V3270_SELECT_TEXT, - V3270_SELECT_TABLE, + V3270_SELECT_NONE, ///< @brief No selected format, use default. + V3270_SELECT_TEXT, ///< @brief Single text format, don't process. + V3270_SELECT_TABLE, ///< @brief Parse contents as table (only for text formats). V3270_SELECT_MAX } V3270_SELECT_FORMAT; @@ -206,6 +207,7 @@ LIB3270_EXPORT void v3270_select_all(GtkWidget *widget); LIB3270_EXPORT void v3270_select_region(GtkWidget *widget, gint start, gint end); + LIB3270_EXPORT void v3270_copy(GtkWidget *widget, V3270_SELECT_FORMAT mode, gboolean cut); LIB3270_EXPORT void v3270_copy_text(GtkWidget *widget, V3270_SELECT_FORMAT mode, gboolean cut); LIB3270_EXPORT void v3270_copy_text_append(GtkWidget *widget); diff --git a/src/terminal/draw.c b/src/terminal/draw.c index 2d2b4f6..f6fb052 100644 --- a/src/terminal/draw.c +++ b/src/terminal/draw.c @@ -188,35 +188,7 @@ void v3270_draw_text_at(cairo_t *cr, int x, int y, v3270FontInfo *font, const ch } void v3270_draw_text(cairo_t *cr, const GdkRectangle *rect, v3270FontInfo *font, const char *str) { - v3270_draw_text_at(cr,rect->x,rect->y,font,str); - -/* - cairo_status_t status; - cairo_glyph_t * glyphs = NULL; - int num_glyphs = 0; - cairo_text_cluster_t * clusters = NULL; - int num_clusters = 0; - cairo_text_cluster_flags_t cluster_flags; - cairo_scaled_font_t * scaled_font = cairo_get_scaled_font (cr); - - status = cairo_scaled_font_text_to_glyphs( - scaled_font, - (double) rect->x, (double) (rect->y+font->height), - str, strlen(str), - &glyphs, &num_glyphs, - &clusters, &num_clusters, &cluster_flags ); - - if (status == CAIRO_STATUS_SUCCESS) { - cairo_show_text_glyphs(cr,str,strlen(str),glyphs, num_glyphs,clusters, num_clusters, cluster_flags); - } - - if(glyphs) - cairo_glyph_free(glyphs); - - if(clusters) - cairo_text_cluster_free(clusters); -*/ } void v3270_draw_char(cairo_t *cr, unsigned char chr, unsigned short attr, H3270 *session, v3270FontInfo *font, GdkRectangle *rect, GdkRGBA *fg, GdkRGBA *bg) @@ -419,15 +391,14 @@ LIB3270_EXPORT void v3270_reload(GtkWidget *widget) for(c=0;c < cols;c++) { - unsigned char chr = 0; - unsigned short attr; + struct v3270_character element = { 0, 0 }; - lib3270_get_contents(terminal->host,addr,addr,&chr,&attr); + lib3270_get_contents(terminal->host,addr,addr,&element.chr,&element.attr); if(addr == cursor) - v3270_update_cursor_rect(terminal,&rect,chr,attr); + v3270_update_cursor_rect(terminal,&rect,&element); - v3270_draw_element(cr,chr,attr,terminal->host,&terminal->font,&rect,terminal->color); + v3270_draw_element(cr,element.chr,element.attr,terminal->host,&terminal->font,&rect,terminal->color); addr++; rect.x += rect.width; @@ -446,10 +417,14 @@ LIB3270_EXPORT void v3270_reload(GtkWidget *widget) void v3270_update_char(H3270 *session, int addr, unsigned char chr, unsigned short attr, unsigned char cursor) { - v3270 * terminal = GTK_V3270(lib3270_get_user_data(session)); - cairo_t * cr; - GdkRectangle rect; - unsigned int rows,cols; + v3270 * terminal = GTK_V3270(lib3270_get_user_data(session)); + cairo_t * cr; + GdkRectangle rect; + unsigned int rows,cols; + struct v3270_character element; + + element.chr = chr; + element.attr = attr; if(!(gtk_widget_get_realized(GTK_WIDGET(terminal)) && terminal->drawing)) return; @@ -473,8 +448,9 @@ void v3270_update_char(H3270 *session, int addr, unsigned char chr, unsigned sho cairo_set_scaled_font(cr,terminal->font.scaled); v3270_draw_element(cr, chr, attr, terminal->host, &terminal->font, &rect,terminal->color); cairo_destroy(cr); + if(cursor) - v3270_update_cursor_rect(terminal,&rect,chr,attr); + v3270_update_cursor_rect(terminal,&rect,&element); v3270_queue_draw_area(GTK_WIDGET(terminal),rect.x,rect.y,rect.width,rect.height); @@ -503,13 +479,13 @@ void v3270_update_cursor_surface(v3270 *widget,unsigned char chr,unsigned short } -void v3270_update_cursor_rect(v3270 *widget, GdkRectangle *rect, unsigned char chr, unsigned short attr) +void v3270_update_cursor_rect(v3270 *widget, GdkRectangle *rect, const struct v3270_character *element) { - widget->cursor.chr = chr; + widget->cursor.chr = element->chr; widget->cursor.rect = *rect; - widget->cursor.attr = attr; + widget->cursor.attr = element->attr; widget->cursor.rect.height = widget->font.height + widget->font.descent; - v3270_update_cursor_surface(widget,chr,attr); + v3270_update_cursor_surface(widget,element->chr,element->attr); } void v3270_queue_draw_area(GtkWidget *widget, gint x, gint y, gint width, gint height) diff --git a/src/trace/exec.c b/src/trace/exec.c index 7fa217a..7cc39b9 100644 --- a/src/trace/exec.c +++ b/src/trace/exec.c @@ -176,17 +176,18 @@ if(g_str_has_prefix(cmdline,"copy")) { + gchar * arg = cmdline+4; g_strstrip(arg); if(!(*arg && g_ascii_strcasecmp(arg,"text"))) { // No argument or "text" copy text. - v3270_copy_text(widget, V3270_SELECT_TEXT, FALSE); + v3270_copy(widget, V3270_SELECT_TEXT, FALSE); } else if(!g_ascii_strcasecmp(arg,"table")) { - v3270_copy_text(widget, V3270_SELECT_TABLE, FALSE); + v3270_copy(widget, V3270_SELECT_TABLE, FALSE); } return 0; diff --git a/v3270.cbp b/v3270.cbp index 6c4950d..b5227f3 100644 --- a/v3270.cbp +++ b/v3270.cbp @@ -42,6 +42,9 @@ + + -- libgit2 0.21.2