summaryrefslogtreecommitdiff
path: root/amiga/clipboard.c
blob: b092650036ce01c85fdad831c8ef5ea5272494db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
 * Copyright 2008-2012 Chris Young <chris@unsatisfactorysoftware.co.uk>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "desktop/gui.h"
#include "desktop/selection.h"
#include "desktop/textinput.h"
#include "render/box.h"
#include "utils/utf8.h"

#include "amiga/bitmap.h"
#include "amiga/clipboard.h"
#include "amiga/drag.h"
#include "amiga/filetype.h"
#include "amiga/gui.h"
#include "amiga/iff_cset.h"
#include "amiga/iff_dr2d.h"
#include "amiga/menu.h"
#include "desktop/options.h"
#include "amiga/utf8.h"

#include <proto/iffparse.h>
#include <proto/intuition.h>
#include <proto/exec.h>
#include <proto/datatypes.h>
#include <proto/diskfont.h>

#include <diskfont/diskfonttag.h>
#include <datatypes/textclass.h>
#include <datatypes/pictureclass.h>

#define ID_UTF8  MAKE_ID('U','T','F','8')

struct IFFHandle *iffh = NULL;

bool ami_add_to_clipboard(const char *text, size_t length, bool space);
static bool ami_copy_selection(const char *text, size_t length,
	struct box *box, void *handle, const char *whitespace_text,
	size_t whitespace_length);

static LONG ami_clipboard_iffp_do_nothing(struct Hook *hook, void *object, LONG *cmd)
{
	return 0;
}

static void ami_clipboard_iffp_clear_stopchunk(struct IFFHandle *iffh, ULONG iff_type, ULONG iff_chunk)
{
	static struct Hook entry_hook;

	entry_hook.h_Entry = (void *)ami_clipboard_iffp_do_nothing;
	entry_hook.h_Data = 0;

	EntryHandler(iffh, iff_type, iff_chunk, IFFSLI_TOP, &entry_hook, NULL);
}

struct IFFHandle *ami_clipboard_init_internal(int unit)
{
	struct IFFHandle *iffhandle = NULL;

	if(iffhandle = AllocIFF())
	{
		if(iffhandle->iff_Stream = (ULONG)OpenClipboard(unit))
		{
			InitIFFasClip(iffhandle);
		}
	}

	return iffhandle;
}

void ami_clipboard_init(void)
{
	iffh = ami_clipboard_init_internal(0);
}

void ami_clipboard_free_internal(struct IFFHandle *iffhandle)
{
	if(iffhandle->iff_Stream) CloseClipboard((struct ClipboardHandle *)iffhandle->iff_Stream);
	if(iffhandle) FreeIFF(iffhandle);
}

void ami_clipboard_free(void)
{
	ami_clipboard_free_internal(iffh);
}

void gui_start_selection(struct gui_window *g)
{
	if(!g) return;
	if(!g->shared->win) return;
	if(nsoption_bool(kiosk_mode) == true) return;

	OnMenu(g->shared->win, AMI_MENU_CLEAR);
	OnMenu(g->shared->win, AMI_MENU_COPY);

	if(selection_read_only(browser_window_get_selection(g->shared->bw)) == false)
		OnMenu(g->shared->win, AMI_MENU_CUT);
}

void gui_clear_selection(struct gui_window *g)
{
	if(!g) return;
	if(!g->shared->win) return;
	if(nsoption_bool(kiosk_mode) == true) return;

	OffMenu(g->shared->win, AMI_MENU_CLEAR);
	OffMenu(g->shared->win, AMI_MENU_CUT);
	OffMenu(g->shared->win, AMI_MENU_COPY);
}

bool ami_clipboard_check_for_utf8(struct IFFHandle *iffh) {
	struct ContextNode *cn;
	ULONG error;
	bool utf8_chunk = false;
	
	if(OpenIFF(iffh, IFFF_READ)) return false;
	
	ami_clipboard_iffp_clear_stopchunk(iffh, ID_FTXT, ID_CSET);
	ami_clipboard_iffp_clear_stopchunk(iffh, ID_FTXT, ID_CHRS);
	
	if(!StopChunk(iffh, ID_FTXT, ID_UTF8)) {
		error = ParseIFF(iffh, IFFPARSE_SCAN);
		if(error != IFFERR_EOF)
			utf8_chunk = true; /* or a real error, but that'll get caught later */
	}
	CloseIFF(iffh);

	return utf8_chunk;
}

void gui_paste_from_clipboard(struct gui_window *g, int x, int y)
{
	/* This and the other clipboard code is heavily based on the RKRM examples */
	struct ContextNode *cn;
	ULONG rlen=0,error;
	struct CSet cset;
	LONG codeset = 0;
	char *clip;
	bool utf8_chunks = false;
	STRPTR readbuf = AllocVec(1024,MEMF_PRIVATE | MEMF_CLEAR);

	cset.CodeSet = 0;

	if(ami_clipboard_check_for_utf8(iffh))
		utf8_chunks = true;
	
	if(OpenIFF(iffh,IFFF_READ)) return;
	
	if(utf8_chunks == false) {
		if(StopChunk(iffh,ID_FTXT,ID_CHRS)) return;
		if(StopChunk(iffh,ID_FTXT,ID_CSET)) return;
	} else {
		if(StopChunk(iffh,ID_FTXT,ID_UTF8)) return;
	}
	
	while(1)
	{
		error = ParseIFF(iffh,IFFPARSE_SCAN);
		if(error == IFFERR_EOC) continue;
		else if(error) break;

		cn = CurrentChunk(iffh);

		if((cn)&&(cn->cn_Type == ID_FTXT)&&(cn->cn_ID == ID_CSET)&&(utf8_chunks == false))
		{
			rlen = ReadChunkBytes(iffh,&cset,32);
			if(cset.CodeSet == 1) codeset = 106;
				else codeset = cset.CodeSet;
		}

		if((cn)&&(cn->cn_Type == ID_FTXT)&&(cn->cn_ID == ID_CHRS)&&(utf8_chunks == false))
		{
			while((rlen = ReadChunkBytes(iffh,readbuf,1024)) > 0)
			{
				if(codeset == 0)
				{
					utf8_from_local_encoding(readbuf,rlen,&clip);
				}
				else
				{
					utf8_from_enc(readbuf,
						(const char *)ObtainCharsetInfo(DFCS_NUMBER,
										codeset, DFCS_MIMENAME),
						rlen, &clip);
				}

				browser_window_paste_text(g->shared->bw,clip,rlen,true);
			}
			if(rlen < 0) error = rlen;
		}

		if((cn)&&(cn->cn_Type == ID_FTXT)&&(cn->cn_ID == ID_UTF8)&&(utf8_chunks == true))
		{
			while((rlen = ReadChunkBytes(iffh, readbuf, 1024)) > 0)
			{
				browser_window_paste_text(g->shared->bw, readbuf, rlen, true);
			}
			if(rlen < 0) error = rlen;
		}
	}
	CloseIFF(iffh);
}

bool gui_empty_clipboard(void)
{
	/* Put a half-completed FTXT on the clipboard and leave it open for more additions */

	struct CSet cset = {0};

	if(!(OpenIFF(iffh,IFFF_WRITE)))
	{
		if(!(PushChunk(iffh,ID_FTXT,ID_FORM,IFFSIZE_UNKNOWN)))
		{
			if(nsoption_bool(utf8_clipboard))
			{
				if(!(PushChunk(iffh,0,ID_CSET,32)))
				{
					cset.CodeSet = 106; // UTF-8
					WriteChunkBytes(iffh,&cset,32);
					PopChunk(iffh);
				}
			}
		}
		else
		{
			PopChunk(iffh);
			return false;
		}
		return true;
	}
	return false;
}

bool gui_add_to_clipboard(const char *text, size_t length, bool space)
{
	/* This might crash or at least not work if gui_empty_clipboard isn't called first,
	   and gui_commit_clipboard after.
	   These only seem to be called from desktop/textinput.c in this specific order, if they
	   are added elsewhere this might need a rewrite. */

	char *buffer;

	if(text == NULL) return true;
	
	if(!(PushChunk(iffh,0,ID_CHRS,IFFSIZE_UNKNOWN))) {
		if(nsoption_bool(utf8_clipboard)) {
			WriteChunkBytes(iffh,text,length);
		} else {
			buffer = ami_utf8_easy(text);

			if(buffer) {
				char *p;

				p = buffer;

				while(*p != '\0') {
					if(*p == 0xa0) *p = 0x20;
					p++;
				}
				WriteChunkBytes(iffh, buffer, strlen(buffer));
				ami_utf8_free(buffer);
			}
		}

		if(space) WriteChunkBytes(iffh," ",1);
		PopChunk(iffh);
	} else {
		PopChunk(iffh);
		return false;
	}

	if(!(PushChunk(iffh, 0, ID_UTF8, IFFSIZE_UNKNOWN))) {
		WriteChunkBytes(iffh, text, length);
		if(space) WriteChunkBytes(iffh, " ", 1);
		PopChunk(iffh);
	} else {
		PopChunk(iffh);
		return false;
	}
		
	return true;
}

bool gui_commit_clipboard(void)
{
	if(iffh) CloseIFF(iffh);

	return true;
}

bool gui_copy_to_clipboard(struct selection *s)
{
	bool success;

	if(s->defined == false) return false;
	if(!gui_empty_clipboard()) return false;

	success = selection_copy_to_clipboard(s);

	/* commit regardless, otherwise we leave the clipboard in an unusable state */
	gui_commit_clipboard();

	return success;
}

struct ami_text_selection *ami_selection_to_text(struct gui_window_2 *gwin)
{
	struct ami_text_selection *sel;

	sel = AllocVec(sizeof(struct ami_text_selection),
			MEMF_PRIVATE | MEMF_CLEAR);

	if(sel) selection_traverse(browser_window_get_selection(gwin->bw), ami_copy_selection, sel);

	return sel;
}

static bool ami_copy_selection(const char *text, size_t length,
	struct box *box, void *handle, const char *whitespace_text,
	size_t whitespace_length)
{
	struct ami_text_selection *sel = handle;
	int len = length;

	if((length + (sel->length)) > (sizeof(sel->text)))
		len = sizeof(sel->text) - (sel->length);

	if(len <= 0) return false;

	memcpy((sel->text) + (sel->length), text, len);
	sel->length += len;

	sel->text[sel->length] = '\0';

	return true;
}

void ami_drag_selection(struct selection *s)
{
	struct box *text_box;
	int x;
	int y;
	char *utf8text;
	struct ami_text_selection *sel;
	struct IFFHandle *old_iffh = iffh;
	struct gui_window_2 *gwin = ami_window_at_pointer(AMINS_WINDOW);

	if(!gwin)
	{
		DisplayBeep(scrn);
		return;
	}

	x = gwin->win->MouseX;
	y = gwin->win->MouseY;

	if(text_box = ami_text_box_at_point(gwin, (ULONG *)&x, (ULONG *)&y))
	{
		iffh = ami_clipboard_init_internal(1);

		if(gui_copy_to_clipboard(s))
		{
			browser_window_mouse_click(gwin->bw, BROWSER_MOUSE_PRESS_1, x, y);
			browser_window_key_press(gwin->bw, KEY_PASTE);
		}

		ami_clipboard_free_internal(iffh);
		iffh = old_iffh;
	}
	else
	{
		x = gwin->win->MouseX;
		y = gwin->win->MouseY;

		if(ami_gadget_hit(gwin->objects[GID_URL], x, y))
		{
			if(sel = ami_selection_to_text(gwin))
			{
				utf8text = ami_utf8_easy(sel->text);
				RefreshSetGadgetAttrs((struct Gadget *)gwin->objects[GID_URL],
					gwin->win, NULL, STRINGA_TextVal, utf8text, TAG_DONE);
				FreeVec(sel);
				ami_utf8_free(utf8text);
			}
		}
		else if(ami_gadget_hit(gwin->objects[GID_SEARCHSTRING], x, y))
		{
			if(sel = ami_selection_to_text(gwin))
			{
				utf8text = ami_utf8_easy(sel->text);
				RefreshSetGadgetAttrs((struct Gadget *)gwin->objects[GID_SEARCHSTRING],
					gwin->win, NULL, STRINGA_TextVal, utf8text, TAG_DONE);
				FreeVec(sel);
				ami_utf8_free(utf8text);
			}
		}
		else
		{
			DisplayBeep(scrn);
		}
	}
}

bool ami_easy_clipboard(char *text)
{
	if(!gui_empty_clipboard()) return false;
	if(!gui_add_to_clipboard(text,strlen(text),false)) return false;
	if(!gui_commit_clipboard()) return false;

	return true;
}

bool ami_easy_clipboard_bitmap(struct bitmap *bitmap)
{
	Object *dto = NULL;

	if(dto = ami_datatype_object_from_bitmap(bitmap))
	{
		DoDTMethod(dto,NULL,NULL,DTM_COPY,NULL);
		DisposeDTObject(dto);
	}
}

#ifdef WITH_NS_SVG
bool ami_easy_clipboard_svg(struct hlcache_handle *c)
{
	const char *source_data;
	ULONG source_size;

	if(ami_mime_compare(c, "svg") == false) return false;
	if((source_data = content_get_source_data(c, &source_size)) == NULL) return false;

	if(!(OpenIFF(iffh,IFFF_WRITE)))
	{
		ami_svg_to_dr2d(iffh, source_data, source_size, nsurl_access(hlcache_handle_get_url(c)));
		CloseIFF(iffh);
	}

	return true;
}
#endif