summaryrefslogtreecommitdiff
path: root/riscos/history.c
blob: 79f2665d5bb880e5e0efcb7d7111ba017514b7f2 (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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2004 James Bursa <bursa@users.sourceforge.net>
 */

/** \file
 * Browser history tree and window (implementation).
 */

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <swis.h>
#include "oslib/colourtrans.h"
#include "oslib/font.h"
#include "oslib/wimp.h"
#include "netsurf/riscos/options.h"
#include "netsurf/riscos/gui.h"
#include "netsurf/riscos/thumbnail.h"
#include "netsurf/riscos/tinct.h"
#include "netsurf/riscos/wimp.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/utils.h"

#define SIZE 10
#define WIDTH 200
#define HEIGHT 152
#define MARGIN 32
#define FULL_WIDTH (WIDTH + MARGIN + MARGIN)
#define FULL_HEIGHT (HEIGHT + MARGIN + MARGIN)
#define SPRITE_SIZE (16 + 44 + ((WIDTH / 2 + 3) & ~3) * HEIGHT / 2)

/** A node in the history tree. */
struct history_entry {
	char *url;    /**< Page URL. */
	char *frag_id; /** Fragment identifier */
	char *title;  /**< Page title. */
	struct history_entry *back;  /**< Parent. */
	struct history_entry *next;  /**< Next sibling. */
	struct history_entry *forward;  /**< First child. */
	struct history_entry *forward_pref;  /**< Child in direction of
	                                          current entry. */
	struct history_entry *forward_last;  /**< Last child. */
	unsigned int children;  /**< Number of children. */
	int x, y, width;
	osspriteop_area *sprite_area;  /**< Thumbnail sprite area, or 0. */
};

/** History tree for a window. */
struct history {
	/** First page in tree (page that window opened with). */
	struct history_entry *start;
	/** Current position in tree. */
	struct history_entry *current;
};

static struct browser_window *history_bw;
static struct history *history_current = 0;
/* Position of mouse in window */
static int mouse_x = 0, mouse_y = 0;
wimp_w history_window;
font_f history_font;

static void history_free_entry(struct history_entry *entry);
static void ro_gui_history_redraw_tree(struct history_entry *he,
		int x0, int y0);
static struct history_entry * ro_gui_history_click_find(
		struct history_entry *he,
		int x, int y);
static void history_go(struct browser_window *bw,
		struct history_entry *entry, bool new_window);


/**
 * Create a new history tree for a window.
 *
 * \return  pointer to an opaque history structure, 0 on failure.
 */

struct history *history_create(void)
{
	struct history *history;

	history = malloc(sizeof *history);
	if (!history) {
		warn_user("NoMemory", 0);
		return 0;
	}

	history->start = 0;
	history->current = 0;

	return history;
}


/**
 * Insert a url into the history tree.
 *
 * \param  history  opaque history structure, as returned by history_create()
 * \param  content  content to add to history
 * \param  frag_id  fragment identifier
 *
 * The page is added after the current entry and becomes current.
 */

void history_add(struct history *history, struct content *content, char *frag_id)
{
	struct history_entry *entry;
	char *url;
	char *title;
	char *split;
	int width;
	osspriteop_area *area;
//	os_error *error;

	if (!history)
		return;

	/* allocate space */
	entry = malloc(sizeof *entry);
	url = strdup(content->url);
	title = strdup(content->title ? content->title : url);
	if (!entry || !url || !title) {
		warn_user("NoMemory", 0);
		free(entry);
		free(url);
		free(title);
		return;
	}

	/* truncate title to available width */
	font_scan_string(history_font, title, font_GIVEN_FONT | font_KERN,
			WIDTH * 400, 0x7fffffff,
			0, 0, 0, &split, &width, 0, 0);
	if (title[split - title]) {
		title[split - title - 2] = 0x8c;  /* ellipsis */
		title[split - title - 1] = 0;
	}

	entry->url = url;
	entry->frag_id = frag_id ? strdup(frag_id) : 0;
	entry->title = title;
	entry->back = history->current;
	entry->next = 0;
	entry->forward = entry->forward_pref = entry->forward_last = 0;
	entry->children = 0;
	entry->width = width / 400;
	entry->sprite_area = 0;
	if (history->current) {
		if (history->current->forward_last)
			history->current->forward_last->next = entry;
		else
			history->current->forward = entry;
		history->current->forward_pref = entry;
		history->current->forward_last = entry;
		history->current->children++;
	} else {
		history->start = entry;
	}
	history->current = entry;

/*	area = malloc(SPRITE_SIZE);
	if (!area) {
		LOG(("malloc failed"));
		return;
	}

	area->size = SPRITE_SIZE;
	area->sprite_count = 0;
	area->first = 16;
	area->used = 16;

	error = xosspriteop_create_sprite(osspriteop_NAME,
			area, "thumbnail", false,
			WIDTH / 2, HEIGHT / 2, os_MODE8BPP90X90);
	if (error) {
		LOG(("0x%x: %s", error->errnum, error->errmess));
		return;
	}
  */
  	area = thumbnail_initialise(WIDTH / 2, HEIGHT / 2, (os_mode)0x301680b5);
  	if (!area) {
		LOG(("Thumbnail initialisation failed."));
		return;
	}
	thumbnail_create(content, area,
			(osspriteop_header *) (area + 1),
			WIDTH / 2, HEIGHT / 2);
/*	xosspriteop_save_sprite_file(osspriteop_NAME,
			area, "thumbnail");*/

	entry->sprite_area = area;
}


/**
 * Update the thumbnail for the current entry.
 *
 * \param  history  opaque history structure, as returned by history_create()
 * \param  content  content for current entry
 */

void history_update(struct history *history, struct content *content)
{
	if (!history || !history->current || !history->current->sprite_area)
		return;

	thumbnail_create(content, history->current->sprite_area,
			(osspriteop_header *)
				(history->current->sprite_area + 1),
			WIDTH / 2, HEIGHT / 2);
}


/**
 * Free a history structure.
 *
 * \param  history  opaque history structure, as returned by history_create()
 */

void history_destroy(struct history *history)
{
	if (!history)
		return;
	if (history_current == history) {
		wimp_close_window(history_window);
		history_current = 0;
	}
	history_free_entry(history->start);
	free(history);
}


/**
 * Free an entry in the tree recursively.
 */

void history_free_entry(struct history_entry *entry)
{
	if (!entry)
		return;
	history_free_entry(entry->forward);
	history_free_entry(entry->next);
	free(entry->url);
	if (entry->frag_id)
		free(entry->frag_id);
	free(entry->title);
	free(entry->sprite_area);
	free(entry);
}


/**
 * Create history window.
 */

void ro_gui_history_init(void)
{
	history_window = ro_gui_dialog_create("history");
	history_font = font_find_font("Homerton.Medium", 112, 128, 0, 0, 0, 0);
}


/**
 * Free history window resources.
 */

void ro_gui_history_quit(void)
{
	font_lose_font(history_font);
}


/**
 * Update resources folowing a mode change
 */
void ro_gui_history_mode_change(void)
{
	font_lose_font(history_font);
	history_font = font_find_font("Homerton.Medium", 112, 128, 0, 0, 0, 0);
}

/**
 * Open history window.
 *
 * \param pointer  whether to open the window at the pointer
 */

void ro_gui_history_open(struct browser_window *bw,
		struct history *history, bool pointer)
{
	bool done = false;
	unsigned int i, j, max_y = 0;
	int x;
	int width, height;
	struct history_entry *row[SIZE], *row2[SIZE];
	struct history_entry *he;
	os_box box = {0, 0, 0, 0};
	wimp_window_state state;

	if (!history || !history->start)
		return;

	history_bw = bw;
	history_current = history;

	/* calculate layout */
	for (i = 0; i != SIZE; i++)
		row[i] = row2[i] = 0;
	row[0] = history->start;
	history->start->x = 0;
	history->start->y = 0;
	for (x = 1; !done; x++) {
		for (i = 0; i != SIZE; i++) {
			if (row[i]) {
				for (j = i; j != SIZE && row2[j]; j++)
					;
				if (j == SIZE) {
					if (row[i]->forward)
						row[i]->forward->x = -1;
					break;
				}
				for (he = row[i]->forward; he; he = he->next) {
					row2[j++] = he;
					if (j == SIZE) {
						if (he->next)
							he->next->x = -1;
						break;
					}
				}
				if (j == SIZE)
					break;
			}
		}
		done = true;
		for (i = 0; i != SIZE; i++) {
			row[i] = row2[i];
			if (row[i]) {
				row[i]->x = x;
				row[i]->y = i;
				if (max_y < i)
					max_y = i;
				done = false;
			}
			row2[i] = 0;
		}
	}

	width = FULL_WIDTH * (x - 1);
	height = FULL_HEIGHT * (max_y + 1);

	box.x1 = width;
	box.y0 = -height;
	wimp_set_extent(history_window, &box);
	state.w = history_window;
	wimp_get_window_state(&state);
	state.visible.x0 = 0;
	state.visible.y0 = 0;
	state.visible.x1 = width;
	state.visible.y1 = height;
	state.next = wimp_HIDDEN;
	wimp_open_window((wimp_open *) &state);
	ro_gui_dialog_open_persistant(bw->window->window, history_window, pointer);
}


/**
 * Redraw history window.
 */

void ro_gui_history_redraw(wimp_draw *redraw)
{
	osbool more;

	more = wimp_redraw_window(redraw);

	while (more) {
		ro_gui_history_redraw_tree(history_current->start,
				redraw->box.x0 - redraw->xscroll,
				redraw->box.y1 - redraw->yscroll);
		more = wimp_get_rectangle(redraw);
	}
}


/**
 * Redraw history tree recursively.
 */

void ro_gui_history_redraw_tree(struct history_entry *he,
		int x0, int y0)
{
	struct history_entry *c;

/*	os_plot(os_MOVE_TO, x0 + he->x * FULL_WIDTH + MARGIN,
			y0 - he->y * FULL_HEIGHT - MARGIN);
	os_plot(os_PLOT_RECTANGLE | os_PLOT_BY, WIDTH, -HEIGHT);*/

	if (he == history_current->current)
		colourtrans_set_gcol(os_COLOUR_RED, 0, os_ACTION_OVERWRITE, 0);
	else
		colourtrans_set_gcol(os_COLOUR_MID_DARK_GREY, 0,
				os_ACTION_OVERWRITE, 0);

	os_plot(os_MOVE_TO, x0 + he->x * FULL_WIDTH + MARGIN - 1,
			y0 - he->y * FULL_HEIGHT - MARGIN);
	os_plot(os_PLOT_SOLID | os_PLOT_BY, WIDTH + 1, 0);
	os_plot(os_PLOT_SOLID | os_PLOT_BY, 0, -HEIGHT - 1);
	os_plot(os_PLOT_SOLID | os_PLOT_BY, -WIDTH - 1, 0);
	os_plot(os_PLOT_SOLID | os_PLOT_BY, 0, HEIGHT + 1);

	if (he->sprite_area) {
		osspriteop_area *area = he->sprite_area;
		osspriteop_header *header = (osspriteop_header *)(area + 1);
		osspriteop_trans_tab *table;

		/* 	Because we're supporting people with OS3.1 we need to check if the
			sprite we have is a legacy 256 colour one
		*/
		if (header->mode == (os_mode)tinct_SPRITE_MODE) {

			/*	We plot with no mask and no scaling as any EIG factors are
				handled internally by Tinct
			*/
			_swix(Tinct_Plot, _IN(2) | _IN(3) | _IN(4) | _IN(7),
				(char *)(header),
				x0 + he->x * FULL_WIDTH + MARGIN,
				y0 - he->y * FULL_HEIGHT - FULL_HEIGHT + MARGIN,
				tinct_ERROR_DIFFUSE | tinct_BILINEAR_FILTER);
		} else {
		        unsigned int size;
			os_factors factors;

			xcolourtrans_generate_table_for_sprite(
					area, (osspriteop_id)header,
					colourtrans_CURRENT_MODE, colourtrans_CURRENT_PALETTE,
					0, colourtrans_GIVEN_SPRITE, 0, 0, &size);
			table = calloc(size, 1);
			if (!table) {
				LOG(("Insufficient memory for calloc"));
				warn_user("NoMemory", 0);
			} else {
				xcolourtrans_generate_table_for_sprite(
						area, (osspriteop_id)header,
						colourtrans_CURRENT_MODE, colourtrans_CURRENT_PALETTE,
						table, colourtrans_GIVEN_SPRITE, 0, 0, 0);

				factors.xmul = 1;
				factors.ymul = 1;
				factors.xdiv = 1;
				factors.ydiv = 1;

				xosspriteop_put_sprite_scaled(osspriteop_PTR,
						area, (osspriteop_id)header,
						x0 + he->x * FULL_WIDTH + MARGIN,
						y0 - he->y * FULL_HEIGHT - FULL_HEIGHT + MARGIN,
						osspriteop_USE_MASK | osspriteop_USE_PALETTE,
						&factors, table);
				free(table);
			}
		}
	}


	if (he == history_current->current)
		wimp_set_font_colours(wimp_COLOUR_WHITE, wimp_COLOUR_RED);
	else
		wimp_set_font_colours(wimp_COLOUR_WHITE, wimp_COLOUR_BLACK);

	xfont_paint(history_font, he->title,
			font_OS_UNITS | font_GIVEN_FONT | font_KERN,
			x0 + he->x * FULL_WIDTH + (FULL_WIDTH - he->width) / 2,
			y0 - he->y * FULL_HEIGHT - HEIGHT - MARGIN - 24,
			NULL, NULL, 0);

	colourtrans_set_gcol(os_COLOUR_MID_DARK_GREY, 0,
			os_ACTION_OVERWRITE, 0);

	for (c = he->forward; c; c = c->next) {
		if (c->x == -1)
			continue;
		os_plot(os_MOVE_TO, x0 + c->x * FULL_WIDTH - MARGIN,
				y0 - he->y * FULL_HEIGHT - FULL_HEIGHT / 2);
		os_plot(os_PLOT_SOLID | os_PLOT_TO,
				x0 + c->x * FULL_WIDTH + MARGIN,
				y0 - c->y * FULL_HEIGHT - FULL_HEIGHT / 2);
		ro_gui_history_redraw_tree(c, x0, y0);
	}
}

/**
 * Handle mouse movements over the history window.
 */
void ro_gui_history_mouse_at(wimp_pointer *pointer)
{
	int x, y;
	long width;
	struct history_entry *he;
	wimp_window_state state;
	wimp_icon_state ic;
	os_box box = {0, 0, 0, 0};

	/* If the mouse hasn't moved, or if we don't want tooltips, exit */
	if ((mouse_x == pointer->pos.x && mouse_y == pointer->pos.y) ||
			!option_history_tooltip)
		return;

	/* Update mouse position */
	mouse_x = pointer->pos.x;
	mouse_y = pointer->pos.y;

	/* Find history tree entry under mouse */
	state.w = history_window;
	wimp_get_window_state(&state);

	x = (pointer->pos.x - (state.visible.x0 - state.xscroll)) / FULL_WIDTH;
	y = -(pointer->pos.y - (state.visible.y1 - state.yscroll)) / FULL_HEIGHT;
	he = ro_gui_history_click_find(history_current->start, x, y);
	if (he) {
		/* get width of string */
		xwimptextop_string_width(he->url,
			strlen(he->url) > 256 ? 256 : strlen(he->url),
				(int*)&width);

		ro_gui_set_icon_string(dialog_tooltip, 0, he->url);

		/* resize icon appropriately */
		ic.w = dialog_tooltip;
		ic.i = 0;
		wimp_get_icon_state(&ic);
		wimp_resize_icon(dialog_tooltip, 0,
				ic.icon.extent.x0, ic.icon.extent.y0,
				width + 16, ic.icon.extent.y1);

		state.w = dialog_tooltip;
		wimp_get_window_state(&state);

		/* update window extent */
		box.x1 = width + 16;
		box.y0 = -36;
		xwimp_set_extent(dialog_tooltip, &box);

		/* set visible area */
		state.visible.x0 = pointer->pos.x + 24;
		state.visible.y0 = pointer->pos.y - 22 - 36;
		state.visible.x1 = pointer->pos.x + 24 + width + 16;
		state.visible.y1 = pointer->pos.y - 22;
		state.next = wimp_TOP;
		/* open window */
		wimp_open_window((wimp_open *) &state);
	}
	else {
	        /* not over a tree entry => close tooltip window. */
	        wimp_close_window(dialog_tooltip);
	}
}

/**
 * Handle mouse clicks in the history window.
 */

void ro_gui_history_click(wimp_pointer *pointer)
{
	int x, y;
	struct history_entry *he;
	wimp_window_state state;

	if (pointer->buttons != wimp_CLICK_SELECT &&
			pointer->buttons != wimp_CLICK_ADJUST)
		/* return if not select or adjust click */
		return;

	state.w = history_window;
	wimp_get_window_state(&state);

	x = (pointer->pos.x - (state.visible.x0 - state.xscroll)) / FULL_WIDTH;
	y = -(pointer->pos.y - (state.visible.y1 - state.yscroll)) / FULL_HEIGHT;
	he = ro_gui_history_click_find(history_current->start, x, y);
	if (he) {
		if (pointer->buttons == wimp_CLICK_SELECT)
			history_current->current = he;
		wimp_close_window(history_window);
		history_current = 0;
		history_go(history_bw, he,
				pointer->buttons == wimp_CLICK_ADJUST);
	}
}


/**
 * Search for an entry with the specified coordinates.
 */

struct history_entry * ro_gui_history_click_find(struct history_entry *he,
		int x, int y)
{
	struct history_entry *c, *d;
	if (he->x == x && he->y == y)
		return he;
	for (c = he->forward; c; c = c->next) {
		d = ro_gui_history_click_find(c, x, y);
		if (d)
			return d;
	}
	return 0;
}


/**
 * Go back in the history.
 *
 * \param  bw       browser window
 * \param  history  history of the window
 */

void history_back(struct browser_window *bw, struct history *history)
{
	if (!history || !history->current || !history->current->back)
		return;
	history->current = history->current->back;
	history_go(bw, history->current, false);
}


/**
 * Go forward in the history.
 *
 * \param  bw       browser window
 * \param  history  history of the window
 */

void history_forward(struct browser_window *bw, struct history *history)
{
	if (!history || !history->current || !history->current->forward_pref)
		return;
	history->current = history->current->forward_pref;
	history_go(bw, history->current, false);
}


/**
 * Check whether it is pssible to go back in the history.
 *
 * \param  history  history of the window
 * \return true if the history can go back, false otherwise
 */

bool history_back_available(struct history *history) {
	return (history && history->current && history->current->back);
}


/**
 * Check whether it is pssible to go forwards in the history.
 *
 * \param  history  history of the window
 * \return true if the history can go forwards, false otherwise
 */

bool history_forward_available(struct history *history) {
	return (history && history->current && history->current->forward_pref);
}

/**
 * Open a history entry in the specified browser window
 *
 * \param bw          browser window
 * \param entry       entry to open
 * \param new_window  open entry in new window
 */
void history_go(struct browser_window *bw, struct history_entry *entry,
		bool new_window)
{
	char *url;

	if (entry->frag_id) {
		url = calloc(strlen(entry->url) + strlen(entry->frag_id) + 5,
							sizeof(char));
		if (!url) {
			warn_user("NoMemory", 0);
			return;
		}
		sprintf(url, "%s#%s", entry->url, entry->frag_id);
	}
	else
		url = entry->url;

	if (new_window)
		browser_window_create(url, bw, 0);
	else
		browser_window_go_post(bw, url, 0, 0, false, 0, false);

	if (entry->frag_id)
		free(url);
}