From c72aba7bd7060dc52df82336081b43813945ca5c Mon Sep 17 00:00:00 2001 From: perry.werneck@gmail.com Date: Tue, 13 Mar 2012 11:07:53 +0000 Subject: [PATCH] Copia para clipboard funcionando; "text-only" por enquanto --- src/gtk/v3270/clipboard.c | 27 +++++++++++++++++++++++++-- src/gtk/v3270/keyboard.c | 4 ++-- src/gtk/v3270/v3270.h | 2 ++ src/gtk/v3270/widget.c | 6 ++++++ src/include/lib3270.h | 26 ++++++++++++++++++++++++-- src/include/lib3270/session.h | 1 + src/lib3270/charset.c | 39 ++++++++++++++++----------------------- src/lib3270/charsetc.h | 7 ++++--- src/lib3270/glue.c | 13 ++++++++++--- src/lib3270/macros.c | 2 +- src/lib3270/printer.c | 2 +- src/lib3270/screen.c | 5 ----- src/lib3270/selection.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++------------------- src/lib3270/trace_ds.c | 2 +- 14 files changed, 146 insertions(+), 62 deletions(-) diff --git a/src/gtk/v3270/clipboard.c b/src/gtk/v3270/clipboard.c index 30f7325..a1ec62e 100644 --- a/src/gtk/v3270/clipboard.c +++ b/src/gtk/v3270/clipboard.c @@ -29,6 +29,7 @@ #include #include + #include #include "v3270.h" #include "private.h" @@ -55,14 +56,17 @@ static void clipboard_clear(GtkClipboard *clipboard, GObject *obj) static void clipboard_get(GtkClipboard *clipboard, GtkSelectionData *selection, guint target, GObject *obj) { -// v3270 *widget = GTK_V3270(obj); + v3270 * widget = GTK_V3270(obj); trace("%s: widget=%p target=\"%s\"",__FUNCTION__,obj,targets[target].target); switch(target) { case CLIPBOARD_TYPE_TEXT: - gtk_selection_data_set_text(selection,"teste de clipboard",-1); + if(!widget->clipboard) + lib3270_ring_bell(widget->host); + else + gtk_selection_data_set_text(selection,widget->clipboard,-1); break; default: @@ -72,8 +76,27 @@ static void clipboard_get(GtkClipboard *clipboard, GtkSelectionData *selection, void v3270_copy_clipboard(v3270 *widget) { + char *text; GtkClipboard * clipboard = gtk_widget_get_clipboard(GTK_WIDGET(widget),GDK_SELECTION_CLIPBOARD); + if(widget->clipboard) + { + g_free(widget->clipboard); + widget->clipboard = NULL; + } + + text = lib3270_get_selected(widget->host); + + if(!text) + { + lib3270_ring_bell(widget->host); + return; + } + + widget->clipboard = g_convert(text, -1, "UTF-8", lib3270_get_charset(widget->host), NULL, NULL, NULL); + + free(text); + if(gtk_clipboard_set_with_owner( clipboard, targets, G_N_ELEMENTS(targets), diff --git a/src/gtk/v3270/keyboard.c b/src/gtk/v3270/keyboard.c index bf8b959..2f3bfcf 100644 --- a/src/gtk/v3270/keyboard.c +++ b/src/gtk/v3270/keyboard.c @@ -194,7 +194,7 @@ void v3270_key_commit(GtkIMContext *imcontext, gchar *str, v3270 *widget) { - gchar *utf = g_convert((char *) str, -1, "ISO-8859-1", "UTF-8", NULL, NULL, NULL); + gchar *utf = g_convert((char *) str, -1, lib3270_get_charset(widget->host), "UTF-8", NULL, NULL, NULL); if(utf) { @@ -203,7 +203,7 @@ } else { - gdk_beep(); + lib3270_ring_bell(widget->host); } } diff --git a/src/gtk/v3270/v3270.h b/src/gtk/v3270/v3270.h index 5210cfc..1bbcacd 100644 --- a/src/gtk/v3270/v3270.h +++ b/src/gtk/v3270/v3270.h @@ -144,6 +144,7 @@ /* private */ int selecting : 1; + #if GTK_CHECK_VERSION(3,0,0) #else @@ -154,6 +155,7 @@ GSource * timer; GtkIMContext * input_method; unsigned short keyflags; + gchar * clipboard; /**< Clipboard contents (text only) */ /* Font info */ gchar * font_family; diff --git a/src/gtk/v3270/widget.c b/src/gtk/v3270/widget.c index e5c64ca..5429ba7 100644 --- a/src/gtk/v3270/widget.c +++ b/src/gtk/v3270/widget.c @@ -554,6 +554,12 @@ static void v3270_destroy(GtkObject *widget) terminal->input_method = NULL; } + if(terminal->clipboard) + { + g_free(terminal->clipboard); + terminal->clipboard = NULL; + } + #if GTK_CHECK_VERSION(3,0,0) GTK_WIDGET_CLASS(v3270_parent_class)->destroy(widget); #else diff --git a/src/include/lib3270.h b/src/include/lib3270.h index 3a46ab1..6330580 100644 --- a/src/include/lib3270.h +++ b/src/include/lib3270.h @@ -596,7 +596,7 @@ * * Get the handle to the GtkWidget who's handling this session. * - * @param h Session handle + * @param h Session handle. * * @return Associated GtkWidget (can be null) * @@ -608,11 +608,33 @@ * * If available play a sound signal do alert user. * - * @param h Session handle + * @param h Session handle. * */ LIB3270_EXPORT void lib3270_ring_bell(H3270 *session); + + /** + * Get lib3270's charset. + * + * @param h Session handle. + * + * @return String with current encoding. + * + */ + LIB3270_EXPORT const char * lib3270_get_charset(H3270 *session); + + /** + * Get selected area. + * + * @param h Session Handle. + * + * @return selected text if available, or NULL. Release it with free() + * + */ + LIB3270_EXPORT char * lib3270_get_selected(H3270 *h); + + LIB3270_EXPORT int lib3270_set_model(H3270 *session, int model); LIB3270_EXPORT int lib3270_get_model(H3270 *session); diff --git a/src/include/lib3270/session.h b/src/include/lib3270/session.h index 91f75e1..cd4964e 100644 --- a/src/include/lib3270/session.h +++ b/src/include/lib3270/session.h @@ -95,6 +95,7 @@ unsigned short current_port; // screen info + char * charset; int ov_rows; int ov_cols; int maxROWS; diff --git a/src/lib3270/charset.c b/src/lib3270/charset.c index ff7d008..28d3ff2 100644 --- a/src/lib3270/charset.c +++ b/src/lib3270/charset.c @@ -178,8 +178,7 @@ wide_resource_init(char *csname) /* * Change character sets. */ -enum cs_result -charset_init(char *csname) +enum cs_result charset_init(H3270 *session, char *csname) { char *cs, *ftcs; enum cs_result rc; @@ -207,17 +206,7 @@ charset_init(char *csname) charset_defaults(); set_cgcsgids(CN); set_charset_name(CN); - set_display_charset("iso8859-1"); - -/* -#if defined(_WIN32) || defined(LIB3270) - set_display_charset("iso8859-1"); -#elif defined(X3270_DISPLAY) || (defined(C3270) && !defined(_WIN32)) - (void) screen_new_display_charsets(default_display_charset,"us"); -#else - utf8_set_display_charsets(default_display_charset, "us"); -#endif -*/ + set_display_charset(session, "ISO-8859-1"); return CS_OKAY; } @@ -373,8 +362,7 @@ set_charset_name(char *csname) } /* Define a charset from resources. */ -static enum cs_result -resource_charset(char *csname, char *cs, char *ftcs) +static enum cs_result resource_charset(char *csname, char *cs, char *ftcs) { enum cs_result rc; int ne = 0; @@ -454,10 +442,12 @@ resource_charset(char *csname, char *cs, char *ftcs) #if defined(_WIN32) /*[*/ /* See about changing the console output code page. */ dcs = get_fresource("%s.%s", ResDisplayCharset, csname); - if (dcs != NULL) { + if (dcs != NULL) + { set_display_charset(dcs); - } else { - set_display_charset("iso8859-1"); + } else + { + set_display_charset("ISO-8859-1"); } #endif /*]*/ @@ -744,10 +734,13 @@ check_charset(void) } #endif /*]*/ -/* Return the current character set name. */ -char * -get_charset_name(void) +void set_display_charset(H3270 *session, char *dcs) +{ + session->charset = strdup(dcs); +} + +LIB3270_EXPORT const char * lib3270_get_charset(H3270 *session) { - return (charset_name != CN)? charset_name: - ((appres.charset != CN)? appres.charset: "us"); + CHECK_SESSION_HANDLE(session); + return session->charset ? session->charset : "ISO-8859-1"; } diff --git a/src/lib3270/charsetc.h b/src/lib3270/charsetc.h index 002f912..d555a46 100644 --- a/src/lib3270/charsetc.h +++ b/src/lib3270/charsetc.h @@ -35,7 +35,8 @@ LIB3270_INTERNAL char *default_display_charset; enum cs_result { CS_OKAY, CS_NOTFOUND, CS_BAD, CS_PREREQ, CS_ILLEGAL }; -LIB3270_INTERNAL enum cs_result charset_init(char *csname); -LIB3270_INTERNAL char *get_charset_name(void); -LIB3270_INTERNAL void set_display_charset(char *dcs); +LIB3270_INTERNAL enum cs_result charset_init(H3270 *session, char *csname); +// LIB3270_INTERNAL char *get_charset_name(void); + +LIB3270_INTERNAL void set_display_charset(H3270 *session, char *dcs); diff --git a/src/lib3270/glue.c b/src/lib3270/glue.c index 5de2831..7e17b5f 100644 --- a/src/lib3270/glue.c +++ b/src/lib3270/glue.c @@ -138,6 +138,13 @@ void lib3270_session_free(H3270 *h) } } + // Release memory + if(h->charset) + { + free(h->charset); + h->charset = NULL; + } + } static void update_char(H3270 *session, int addr, unsigned char chr, unsigned short attr, unsigned char cursor) @@ -259,10 +266,10 @@ H3270 * lib3270_session_new(const char *model) return NULL; Trace("Charset: %s",appres.charset); - if (charset_init(appres.charset) != CS_OKAY) + if (charset_init(hSession,appres.charset) != CS_OKAY) { - Warning(NULL, _( "Cannot find charset \"%s\", using defaults" ), appres.charset); - (void) charset_init(CN); + Warning(hSession, _( "Cannot find charset \"%s\", using defaults" ), appres.charset); + (void) charset_init(hSession,CN); } kybd_init(); diff --git a/src/lib3270/macros.c b/src/lib3270/macros.c index af8bdd4..a757472 100644 --- a/src/lib3270/macros.c +++ b/src/lib3270/macros.c @@ -99,7 +99,7 @@ LIB3270_MACRO( encoding ) { - return strdup("ISO-8859-1"); + return strdup(lib3270_get_charset(hSession)); } LIB3270_MACRO( get ) diff --git a/src/lib3270/printer.c b/src/lib3270/printer.c index f1e6f81..1396aff 100644 --- a/src/lib3270/printer.c +++ b/src/lib3270/printer.c @@ -314,7 +314,7 @@ printer_start(const char *lu) "for pr3287\n"); (void) fprintf(f, "# Created by %s\n", build); (void) fprintf(f, "# Chararter set is '%s'\n", - get_charset_name()); + lib3270_get_charset(&h3270)); (void) fprintf(f, "cgcsgid=0x%08lx\n", cgcsgid); #if defined(X3270_DBCS) /*[*/ if (dbcs) { diff --git a/src/lib3270/screen.c b/src/lib3270/screen.c index d7cae08..4541e77 100644 --- a/src/lib3270/screen.c +++ b/src/lib3270/screen.c @@ -91,11 +91,6 @@ static void status_connect(H3270 *session, int ignored, void *dunno); static void status_3270_mode(H3270 *session, int ignored, void *dunno); static void status_printer(H3270 *session, int on, void *dunno); static unsigned short color_from_fa(unsigned char fa); -// static void relabel(H3270 *session, int ignored, void *dunno); - -void set_display_charset(char *dcs) -{ -} static void addch(H3270 *session, int baddr, unsigned char c, unsigned short attr) { diff --git a/src/lib3270/selection.c b/src/lib3270/selection.c index 9e67823..6e3f4fe 100644 --- a/src/lib3270/selection.c +++ b/src/lib3270/selection.c @@ -36,6 +36,20 @@ /*--[ Implement ]------------------------------------------------------------------------------------*/ +static void get_selected_addr(H3270 *session, int *begin, int *end) +{ + if(session->select.begin > session->select.end) + { + *end = session->select.begin; + *begin = session->select.end; + } + else + { + *begin = session->select.begin; + *end = session->select.end; + } +} + static void update_selected_rectangle(H3270 *session) { struct @@ -44,16 +58,10 @@ static void update_selected_rectangle(H3270 *session) int col; } p[2]; - int begin = session->select.begin; - int end = session->select.end; - int row, col, baddr; - if(begin > end) - { - baddr = end; - end = begin; - begin = baddr; - } + int begin, end, row, col, baddr; + + get_selected_addr(session,&begin,&end); // Get start & end posision p[0].row = (begin/session->cols); @@ -95,17 +103,10 @@ static void update_selected_rectangle(H3270 *session) static void update_selected_region(H3270 *session) { - int baddr; - int begin = session->select.begin; - int end = session->select.end; - int len = session->rows*session->cols; + int baddr,begin,end; + int len = session->rows*session->cols; - if(begin > end) - { - baddr = end; - end = begin; - begin = baddr; - } + get_selected_addr(session,&begin,&end); // First remove unselected areas for(baddr = 0; baddr < begin; baddr++) @@ -310,3 +311,36 @@ LIB3270_ACTION( reselect ) return 0; } +LIB3270_EXPORT char * lib3270_get_selected(H3270 *hSession) +{ + int row, col, baddr; + char *ret; + size_t sz = 0; + + if(!hSession->selected || hSession->select.begin == hSession->select.end) + return NULL; + + ret = malloc(hSession->rows * (hSession->cols+1)); + + baddr = 0; + for(row=0;row < hSession->rows;row++) + { + int cr = 0; + + for(col = 0; col < hSession->cols;col++) + { + if(hSession->ea_buf[baddr].attr & LIB3270_ATTR_SELECTED) + { + cr++; + ret[sz++] = hSession->ea_buf[baddr].chr; + } + baddr++; + } + + if(cr) + ret[sz++] = '\n'; + } + ret[sz] = 0; + + return realloc(ret,sz+1); +} diff --git a/src/lib3270/trace_ds.c b/src/lib3270/trace_ds.c index 717d075..c0704ae 100644 --- a/src/lib3270/trace_ds.c +++ b/src/lib3270/trace_ds.c @@ -429,7 +429,7 @@ create_tracefile_header(const char *mode) if (appres.extended) wtrace(", extended data stream"); wtrace(", %s emulation", appres.m3279 ? "color" : "monochrome"); - wtrace(", %s charset", get_charset_name()); + wtrace(", %s charset", lib3270_get_charset(&h3270)); if (appres.apl_mode) wtrace(", APL mode"); wtrace("\n"); -- libgit2 0.21.2