Commit a3be1d7a30fca68a3d82274e939739bd658c2a30
1 parent
a7a3d00f
Exists in
master
and in
1 other branch
Adding HTML target to clipboard.
Showing
5 changed files
with
187 additions
and
1 deletions
Show diff stats
src/include/clipboard.h
... | ... | @@ -44,6 +44,7 @@ |
44 | 44 | { |
45 | 45 | CLIPBOARD_TYPE_TEXT, |
46 | 46 | CLIPBOARD_TYPE_CSV, |
47 | + CLIPBOARD_TYPE_HTML, | |
47 | 48 | }; |
48 | 49 | |
49 | 50 | G_GNUC_INTERNAL void v3270_update_system_clipboard(GtkWidget *widget); |
... | ... | @@ -51,6 +52,7 @@ |
51 | 52 | |
52 | 53 | /// @brief Get formatted contents as single text. |
53 | 54 | G_GNUC_INTERNAL gchar * v3270_get_copy_as_text(v3270 * terminal); |
55 | + G_GNUC_INTERNAL gchar * v3270_get_copy_as_html(v3270 * terminal); | |
54 | 56 | G_GNUC_INTERNAL gchar * v3270_get_copy_as_table(v3270 * terminal, const gchar *delimiter); |
55 | 57 | |
56 | 58 | #endif // V3270_CLIPBOARD_H_INCLUDED | ... | ... |
... | ... | @@ -0,0 +1,150 @@ |
1 | +/* | |
2 | + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 | |
3 | + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a | |
4 | + * aplicativos mainframe. Registro no INPI sob o nome G3270. | |
5 | + * | |
6 | + * Copyright (C) <2008> <Banco do Brasil S.A.> | |
7 | + * | |
8 | + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob | |
9 | + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela | |
10 | + * Free Software Foundation. | |
11 | + * | |
12 | + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER | |
13 | + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO | |
14 | + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para | |
15 | + * obter mais detalhes. | |
16 | + * | |
17 | + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este | |
18 | + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin | |
19 | + * St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | + * | |
21 | + * Este programa está nomeado como - e possui - linhas de código. | |
22 | + * | |
23 | + * Contatos: | |
24 | + * | |
25 | + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) | |
26 | + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) | |
27 | + * | |
28 | + */ | |
29 | + | |
30 | + #include <clipboard.h> | |
31 | + #include <lib3270/selection.h> | |
32 | + #include <ctype.h> | |
33 | + | |
34 | +/*--[ Implement ]------------------------------------------------------------------------------------*/ | |
35 | + | |
36 | +static void get_element_colors(v3270 * terminal, unsigned short attr, gchar **fgColor, gchar **bgColor) | |
37 | +{ | |
38 | + GdkRGBA *fg; | |
39 | + GdkRGBA *bg = terminal->color+((attr & 0x00F0) >> 4); | |
40 | + | |
41 | + if(attr & LIB3270_ATTR_FIELD) | |
42 | + fg = terminal->color+(attr & 0x0003)+V3270_COLOR_FIELD; | |
43 | + else | |
44 | + fg = terminal->color+(attr & 0x000F); | |
45 | + | |
46 | + *fgColor = gdk_rgba_to_string(fg); | |
47 | + *bgColor = gdk_rgba_to_string(bg); | |
48 | + | |
49 | +} | |
50 | + | |
51 | +/// @brief Get formatted contents as HTML DIV. | |
52 | +static gchar * get_as_div(v3270 * terminal) | |
53 | +{ | |
54 | + GList * element = terminal->selection.blocks; | |
55 | + GString * string = g_string_new(""); | |
56 | + gchar * bgColor = gdk_rgba_to_string(terminal->color+V3270_COLOR_BACKGROUND); | |
57 | + gchar * fgColor; | |
58 | + | |
59 | + g_string_append_printf( | |
60 | + string, | |
61 | + "<div style=\"font-family:%s,monospace;padding:1em;display:inline-block;background-color:%s\">", | |
62 | + terminal->font.family, | |
63 | + bgColor | |
64 | + ); | |
65 | + | |
66 | + g_free(bgColor); | |
67 | + | |
68 | + while(element) | |
69 | + { | |
70 | + lib3270_selection * block = ((lib3270_selection *) element->data); | |
71 | + unsigned int row, col, src = 0; | |
72 | + unsigned short flags = block->contents[0].flags; | |
73 | + | |
74 | + get_element_colors(terminal,flags,&fgColor,&bgColor); | |
75 | + | |
76 | + g_string_append_printf( | |
77 | + string, | |
78 | + "<span style=\"background-color:%s;color:%s\">", | |
79 | + bgColor, | |
80 | + fgColor | |
81 | + ); | |
82 | + | |
83 | + g_free(bgColor); | |
84 | + g_free(fgColor); | |
85 | + | |
86 | +#ifdef DEBUG | |
87 | + g_string_append_c(string,'\n'); | |
88 | +#endif // DEBUG | |
89 | + | |
90 | + for(row=0; row < block->bounds.height; row++) | |
91 | + { | |
92 | + for(col=0; col<block->bounds.width; col++) | |
93 | + { | |
94 | + if(flags != block->contents[src].flags) | |
95 | + { | |
96 | + flags = block->contents[src].flags; | |
97 | + | |
98 | + get_element_colors(terminal,flags,&fgColor,&bgColor); | |
99 | + | |
100 | + g_string_append_printf( | |
101 | + string, | |
102 | + "</span><span style=\"background-color:%s;color:%s\">", | |
103 | + bgColor, | |
104 | + fgColor | |
105 | + ); | |
106 | + | |
107 | + g_free(bgColor); | |
108 | + g_free(fgColor); | |
109 | + | |
110 | + | |
111 | + } | |
112 | + | |
113 | + if(block->contents[src].flags & LIB3270_ATTR_SELECTED && !isspace(block->contents[src].chr)) | |
114 | + { | |
115 | + g_string_append_c(string,block->contents[src].chr); | |
116 | + } | |
117 | + else | |
118 | + { | |
119 | + g_string_append(string," "); | |
120 | + } | |
121 | + | |
122 | + src++; | |
123 | + | |
124 | + } | |
125 | + g_string_append(string,"<br />"); | |
126 | +#ifdef DEBUG | |
127 | + g_string_append_c(string,'\n'); | |
128 | +#endif // DEBUG | |
129 | + } | |
130 | + | |
131 | + g_string_append(string,"</span>"); | |
132 | + | |
133 | + element = g_list_next(element); | |
134 | + } | |
135 | + | |
136 | +#ifdef DEBUG | |
137 | + g_string_append_c(string,'\n'); | |
138 | +#endif // DEBUG | |
139 | + | |
140 | + g_string_append(string,"</div>"); | |
141 | + | |
142 | + return g_string_free(string,FALSE); | |
143 | + | |
144 | +} | |
145 | + | |
146 | +gchar * v3270_get_copy_as_html(v3270 * terminal) | |
147 | +{ | |
148 | + g_autofree char * text = get_as_div(terminal); | |
149 | + return g_convert(text, -1, "UTF-8", lib3270_get_display_charset(terminal->host), NULL, NULL, NULL); | |
150 | +} | ... | ... |
src/selection/linux/copy.c
... | ... | @@ -65,7 +65,11 @@ static void clipboard_get(G_GNUC_UNUSED GtkClipboard *clipboard, GtkSelectionDa |
65 | 65 | } |
66 | 66 | else |
67 | 67 | { |
68 | +#ifdef DEBUG | |
69 | + text = v3270_get_copy_as_html(terminal); | |
70 | +#else | |
68 | 71 | text = v3270_get_copy_as_text(terminal); |
72 | +#endif // DEBUG | |
69 | 73 | } |
70 | 74 | gtk_selection_data_set_text(selection,text,-1); |
71 | 75 | g_free(text); |
... | ... | @@ -86,6 +90,20 @@ static void clipboard_get(G_GNUC_UNUSED GtkClipboard *clipboard, GtkSelectionDa |
86 | 90 | } |
87 | 91 | break; |
88 | 92 | |
93 | + case CLIPBOARD_TYPE_HTML: | |
94 | + { | |
95 | + g_autofree gchar *text = v3270_get_copy_as_html(terminal); | |
96 | + //debug("Selection:\n%s",text); | |
97 | + gtk_selection_data_set( | |
98 | + selection, | |
99 | + gdk_atom_intern_static_string("text/html"), | |
100 | + 8, | |
101 | + (guchar *) text, | |
102 | + strlen(text) | |
103 | + ); | |
104 | + } | |
105 | + break; | |
106 | + | |
89 | 107 | default: |
90 | 108 | g_warning("Unexpected clipboard type %d\n",target); |
91 | 109 | } |
... | ... | @@ -110,7 +128,8 @@ void v3270_update_system_clipboard(GtkWidget *widget) |
110 | 128 | // Reference: https://cpp.hotexamples.com/examples/-/-/g_list_insert_sorted/cpp-g_list_insert_sorted-function-examples.html |
111 | 129 | // |
112 | 130 | static const GtkTargetEntry internal_targets[] = { |
113 | - { "text/csv", 0, CLIPBOARD_TYPE_CSV } | |
131 | + { "text/csv", 0, CLIPBOARD_TYPE_CSV }, | |
132 | + { "text/html", 0, CLIPBOARD_TYPE_HTML } | |
114 | 133 | }; |
115 | 134 | |
116 | 135 | GtkTargetList * list = gtk_target_list_new(internal_targets, G_N_ELEMENTS(internal_targets)); | ... | ... |
src/testprogram/testprogram.c
... | ... | @@ -223,6 +223,11 @@ static void paste_clicked(GtkButton *button, GtkWidget *terminal) |
223 | 223 | v3270_paste(terminal); |
224 | 224 | } |
225 | 225 | |
226 | +static void copy_clicked(GtkButton *button, GtkWidget *terminal) | |
227 | +{ | |
228 | + v3270_copy_selection(terminal,V3270_SELECT_TEXT,FALSE); | |
229 | +} | |
230 | + | |
226 | 231 | static void color_clicked(GtkButton G_GNUC_UNUSED(*button), GtkWidget *terminal) |
227 | 232 | { |
228 | 233 | GtkWidget * dialog = v3270_dialog_new(terminal, _("Color setup"), _("_Save")); |
... | ... | @@ -302,6 +307,7 @@ static void activate(GtkApplication* app, G_GNUC_UNUSED gpointer user_data) { |
302 | 307 | { "gtk-home", G_CALLBACK(host_clicked), "Configure host" }, |
303 | 308 | { "gtk-print", G_CALLBACK(print_clicked), "Print screen contents" }, |
304 | 309 | { "gtk-harddisk", G_CALLBACK(ft_clicked), "Open file transfer dialog" }, |
310 | + { "gtk-copy", G_CALLBACK(copy_clicked), "Copy data" }, | |
305 | 311 | { "gtk-paste", G_CALLBACK(paste_clicked), "Paste data" } |
306 | 312 | }; |
307 | 313 | ... | ... |
v3270.cbp
... | ... | @@ -146,6 +146,12 @@ |
146 | 146 | <Unit filename="src/selection/copy.c"> |
147 | 147 | <Option compilerVar="CC" /> |
148 | 148 | </Unit> |
149 | + <Unit filename="src/selection/html.c"> | |
150 | + <Option compilerVar="CC" /> | |
151 | + </Unit> | |
152 | + <Unit filename="src/selection/linux/copy.c"> | |
153 | + <Option compilerVar="CC" /> | |
154 | + </Unit> | |
149 | 155 | <Unit filename="src/selection/linux/paste.c"> |
150 | 156 | <Option compilerVar="CC" /> |
151 | 157 | </Unit> |
... | ... | @@ -158,6 +164,9 @@ |
158 | 164 | <Unit filename="src/selection/text.c"> |
159 | 165 | <Option compilerVar="CC" /> |
160 | 166 | </Unit> |
167 | + <Unit filename="src/selection/windows/copy.c"> | |
168 | + <Option compilerVar="CC" /> | |
169 | + </Unit> | |
161 | 170 | <Unit filename="src/selection/windows/paste.c"> |
162 | 171 | <Option compilerVar="CC" /> |
163 | 172 | </Unit> | ... | ... |