From 5e7ea614539be0015edcd1bbb1651db2f436e86c Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Thu, 25 Jul 2019 15:26:47 -0300 Subject: [PATCH] Copying the new clipboard methods used on linux to windows. --- src/selection/windows/copy.c | 47 ++++++++++++++++++++++++++++++++++------------- src/selection/windows/paste.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------ src/testprogram/testprogram.c | 4 ++++ 3 files changed, 187 insertions(+), 31 deletions(-) diff --git a/src/selection/windows/copy.c b/src/selection/windows/copy.c index 69db5a6..045ee10 100644 --- a/src/selection/windows/copy.c +++ b/src/selection/windows/copy.c @@ -18,7 +18,7 @@ * 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. + * Este programa está nomeado como - possui - linhas de código. * * Contatos: * @@ -31,6 +31,7 @@ #include /*--[ Implement ]------------------------------------------------------------------------------------*/ + static void clipboard_clear(G_GNUC_UNUSED GtkClipboard *clipboard, G_GNUC_UNUSED GObject *obj) { v3270 * terminal = GTK_V3270(obj); @@ -56,18 +57,8 @@ static void clipboard_get(G_GNUC_UNUSED GtkClipboard *clipboard, GtkSelectionDa { case CLIPBOARD_TYPE_TEXT: // Get clipboard contents as text { - gchar *text; - - if(terminal->selection.format == V3270_SELECT_TABLE) - { - text = v3270_get_copy_as_table(terminal,"\t"); - } - else - { - text = v3270_get_copy_as_text(terminal); - } + g_autofree gchar *text = v3270_get_copy_as_text(terminal); gtk_selection_data_set_text(selection,text,-1); - g_free(text); } break; @@ -85,6 +76,33 @@ static void clipboard_get(G_GNUC_UNUSED GtkClipboard *clipboard, GtkSelectionDa } break; + case CLIPBOARD_TYPE_HTML: + { + g_autofree gchar *text = v3270_get_copy_as_html(terminal); + //debug("Selection:\n%s",text); + gtk_selection_data_set( + selection, + gdk_atom_intern_static_string("text/html"), + 8, + (guchar *) text, + strlen(text) + ); + } + break; + + case CLIPBOARD_TYPE_V3270_UNPROTECTED: + { + g_autofree gchar *data = v3270_get_copy_as_data_block(terminal); + gtk_selection_data_set( + selection, + GTK_V3270_GET_CLASS(obj)->clipboard_formatted, + 8, + (guchar *) data, + ((struct SelectionHeader *) data)->length + ); + } + break; + default: g_warning("Unexpected clipboard type %d\n",target); } @@ -109,7 +127,9 @@ void v3270_update_system_clipboard(GtkWidget *widget) // Reference: https://cpp.hotexamples.com/examples/-/-/g_list_insert_sorted/cpp-g_list_insert_sorted-function-examples.html // static const GtkTargetEntry internal_targets[] = { - { "text/csv", 0, CLIPBOARD_TYPE_CSV } + { "text/csv", 0, CLIPBOARD_TYPE_CSV }, + { "text/html", 0, CLIPBOARD_TYPE_HTML }, + { "application/x-v3270-unprotected", 0, CLIPBOARD_TYPE_V3270_UNPROTECTED }, }; GtkTargetList * list = gtk_target_list_new(internal_targets, G_N_ELEMENTS(internal_targets)); @@ -147,3 +167,4 @@ void v3270_update_system_clipboard(GtkWidget *widget) g_signal_emit(widget,v3270_widget_signal[V3270_SIGNAL_CLIPBOARD], 0, TRUE); } + diff --git a/src/selection/windows/paste.c b/src/selection/windows/paste.c index 689adc9..2d01eae 100644 --- a/src/selection/windows/paste.c +++ b/src/selection/windows/paste.c @@ -18,7 +18,7 @@ * 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. + * Este programa está nomeado como selection.c e possui - linhas de código. * * Contatos: * @@ -27,40 +27,171 @@ * */ - #include #include /*--[ Implement ]------------------------------------------------------------------------------------*/ -LIB3270_EXPORT void v3270_paste_text(GtkWidget *widget) +static void text_received(G_GNUC_UNUSED GtkClipboard *clipboard, const gchar *text, GtkWidget *widget) { - g_return_if_fail(GTK_IS_V3270(widget)); + v3270_input_text(widget,text,"UTF-8"); +} + +static gboolean has_target(const GdkAtom atom, const GdkAtom *atoms, const gint n_atoms) +{ + gint ix; + + for(ix = 0; ix < n_atoms; ix++) + { + if(atom == atoms[ix]) + return TRUE; + } + + return FALSE; +} + +static void formatted_received(GtkClipboard *clipboard, GtkSelectionData *selection_data, GtkWidget *widget) +{ + const struct SelectionHeader *selection = (const struct SelectionHeader *) gtk_selection_data_get_data(selection_data); + + v3270 * terminal = GTK_V3270(widget); + + debug( + "Received formatted data with %u bytes: Build=%u rows=%u cols=%u", + selection->length, + selection->build, + selection->rows, + selection->cols + ); + + if(selection->cols != lib3270_get_width(terminal->host) || selection->rows != lib3270_get_height(terminal->host)) + { + GtkWidget * dialog = + gtk_message_dialog_new( + GTK_WINDOW(gtk_widget_get_toplevel(widget)), + GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_INFO, + GTK_BUTTONS_NONE, + _("Not the same terminal type") + ); + + + gtk_window_set_title(GTK_WINDOW(dialog),_("Can't paste")); + + gtk_dialog_add_buttons( + GTK_DIALOG (dialog), + _("_Cancel"), GTK_RESPONSE_CANCEL, + _("_Paste as text"), GTK_RESPONSE_APPLY, + NULL + ); + + gtk_dialog_set_default_response(GTK_DIALOG (dialog),GTK_RESPONSE_APPLY); + + gint response = gtk_dialog_run(GTK_DIALOG(dialog)); + + gtk_widget_destroy(dialog); + + if(response == GTK_RESPONSE_APPLY) + { + gtk_clipboard_request_text( + clipboard, + (GtkClipboardTextReceivedFunc) text_received, + (gpointer) widget + ); + } - if (!OpenClipboard(NULL)) return; - if (IsClipboardFormatAvailable(CF_TEXT)) + } + + if(!v3270_set_from_data_block(terminal, selection)) { - // Got text formatted clipboard. - HGLOBAL hglb; + GtkWidget * dialog = + gtk_message_dialog_new( + GTK_WINDOW(gtk_widget_get_toplevel(widget)), + GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_INFO, + GTK_BUTTONS_NONE, + _("Unable to paste formatted data") + ); + + + gtk_window_set_title(GTK_WINDOW(dialog),_("Can't paste")); + + gtk_dialog_add_buttons( + GTK_DIALOG (dialog), + _("_Cancel"), GTK_RESPONSE_CANCEL, + _("_Paste as text"), GTK_RESPONSE_APPLY, + NULL + ); - hglb = GetClipboardData(CF_TEXT); - if (hglb != NULL) + gtk_dialog_set_default_response(GTK_DIALOG (dialog),GTK_RESPONSE_APPLY); + + gint response = gtk_dialog_run(GTK_DIALOG(dialog)); + + gtk_widget_destroy(dialog); + + if(response == GTK_RESPONSE_APPLY) { - LPTSTR lptstr = GlobalLock(hglb); - if (lptstr != NULL) - { - v3270_input_text(widget,lptstr,"CP1252"); - GlobalUnlock(hglb); - } + gtk_clipboard_request_text( + clipboard, + (GtkClipboardTextReceivedFunc) text_received, + (gpointer) widget + ); } + + return; + + } - CloseClipboard(); + +} + +static void targets_received(GtkClipboard *clipboard, GdkAtom *atoms, gint n_atoms, GtkWidget *widget) +{ + if(has_target(GTK_V3270_GET_CLASS(widget)->clipboard_formatted,atoms,n_atoms)) + { + debug("Clipboard as TN3270 \"%s\" data",gdk_atom_name(GTK_V3270_GET_CLASS(widget)->clipboard_formatted)); + + gtk_clipboard_request_contents( + clipboard, + GTK_V3270_GET_CLASS(widget)->clipboard_formatted, + (GtkClipboardReceivedFunc) formatted_received, + (gpointer) widget + ); + + return; + } + + // No special format available, request it as text. + gtk_clipboard_request_text( + clipboard, + (GtkClipboardTextReceivedFunc) text_received, + (gpointer) widget + ); } LIB3270_EXPORT void v3270_paste(GtkWidget *widget) { - v3270_paste(widget); + g_return_if_fail(GTK_IS_V3270(widget)); + + GtkClipboard * clipboard = gtk_widget_get_clipboard(widget,GTK_V3270(widget)->selection.target); + + gtk_clipboard_request_targets(clipboard, (GtkClipboardTargetsReceivedFunc) targets_received, (gpointer) widget); + +} + +LIB3270_EXPORT void v3270_paste_text(GtkWidget *widget) +{ + g_return_if_fail(GTK_IS_V3270(widget)); + GtkClipboard * clipboard = gtk_widget_get_clipboard(widget,GTK_V3270(widget)->selection.target); + + gtk_clipboard_request_text( + clipboard, + (GtkClipboardTextReceivedFunc) text_received, + (gpointer) widget + ); + } + diff --git a/src/testprogram/testprogram.c b/src/testprogram/testprogram.c index 3d3fffb..4c2a682 100644 --- a/src/testprogram/testprogram.c +++ b/src/testprogram/testprogram.c @@ -92,6 +92,10 @@ static void activate(GtkApplication* app, G_GNUC_UNUSED gpointer user_data) { gtk_notebook_append_page(GTK_NOTEBOOK(notebook),terminal,gtk_label_new("Terminal")); +#ifdef _WIN32 + v3270_set_font_family(terminal,"Droid Sans Mono"); +#endif // _WIN32 + } // Create trace window -- libgit2 0.21.2