summaryrefslogtreecommitdiff
path: root/frontends/amiga/history_local.c
blob: 34d6b03c4aff2b0a64d3f6a1295f934200ae9be1 (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
/*
 * Copyright 2009, 2010 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/>.
 */

/** \file
 * Browser history window (AmigaOS implementation).
 *
 * There is only one history window, not one per browser window.
 */

#include "amiga/os3support.h"

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <proto/intuition.h>
#include <proto/exec.h>
#include <proto/graphics.h>
#include <intuition/icclass.h>
#include <proto/utility.h>
#include <proto/window.h>
#include <proto/space.h>
#include <proto/layout.h>
#include <classes/window.h>
#include <gadgets/space.h>
#include <gadgets/scroller.h>
#include <reaction/reaction.h>
#include <reaction/reaction_macros.h>

#include "utils/log.h"
#include "utils/utils.h"
#include "utils/messages.h"
#include "desktop/browser_history.h"
#include "netsurf/browser_window.h"
#include "netsurf/plotters.h"
#include "netsurf/window.h"
#include "graphics/rpattr.h"

#include "amiga/libs.h"
#include "amiga/misc.h"
#include "amiga/object.h"
#include "amiga/plotters.h"
#include "amiga/gui.h"
#include "amiga/history_local.h"

struct history_window {
	struct ami_generic_window w;
	struct Window *win;
	Object *objects[GID_LAST];
	struct gui_window *gw;
	struct Hook scrollerhook;
	struct gui_globals *gg;
};

static void ami_history_update_extent(struct history_window *hw);
HOOKF(void, ami_history_scroller_hook, Object *, object, struct IntuiMessage *);

static BOOL ami_history_event(void *w);

static const struct ami_win_event_table ami_localhistory_table = {
	ami_history_event,
	NULL, /* we don't explicitly close the local history window on quit */
};

/**
 * Redraw history window.
 */

static void ami_history_redraw(struct history_window *hw)
{
	struct IBox *bbox;
	ULONG xs,ys;
	struct redraw_context ctx = {
		.interactive = true,
		.background_images = true,
		.plot = &amiplot,
		.priv = hw->gg
	};

	GetAttr(SCROLLER_Top,hw->objects[OID_HSCROLL],(ULONG *)&xs);
	GetAttr(SCROLLER_Top,hw->objects[OID_VSCROLL],(ULONG *)&ys);
	if(ami_gui_get_space_box(hw->objects[GID_BROWSER], &bbox) != NSERROR_OK) {
		amiga_warn_user("NoMemory", "");
		return;
	}

/*	core should clear this area for us
	SetRPAttrs(glob->rp, RPTAG_APenColor, 0xffffffff, TAG_DONE);
	RectFill(glob->rp, 0, 0, bbox->Width - 1, bbox->Height - 1);
*/

	browser_window_history_redraw_rectangle(hw->gw->bw, xs, ys,
			bbox->Width + xs, bbox->Height + ys, 0, 0, &ctx);

	ami_clearclipreg(hw->gg);
	ami_history_update_extent(hw);

	BltBitMapRastPort(hw->gg->bm, 0, 0, hw->win->RPort,
				bbox->Left, bbox->Top, bbox->Width, bbox->Height, 0x0C0);

	ami_gui_free_space_box(bbox);
}


/* exported interface documented in amiga/history_local.h */
void ami_history_open(struct gui_window *gw)
{
	struct history *history;
	int width, height;

	if (gw->bw == NULL)
		return;

	history = browser_window_get_history(gw->bw);
	if (history == NULL)
		return;

	if(!gw->hw)
	{
		gw->hw = calloc(1, sizeof(struct history_window));
		gw->hw->gg = ami_plot_ra_alloc(scrn->Width, scrn->Height, false, true);

		gw->hw->gw = gw;
		browser_window_history_size(gw->bw, &width, &height);

		gw->hw->scrollerhook.h_Entry = (void *)ami_history_scroller_hook;
		gw->hw->scrollerhook.h_Data = gw->hw;

		gw->hw->objects[OID_MAIN] = WindowObj,
			WA_ScreenTitle, ami_gui_get_screen_title(),
			WA_Title, messages_get("History"),
			WA_Activate, TRUE,
			WA_DepthGadget, TRUE,
			WA_DragBar, TRUE,
			WA_CloseGadget, TRUE,
			WA_SizeGadget, TRUE,
			WA_PubScreen,scrn,
			WA_InnerWidth,width,
			WA_InnerHeight,height + 10,
			WINDOW_SharedPort,sport,
			WINDOW_UserData,gw->hw,
			WINDOW_IconifyGadget, FALSE,
			WINDOW_GadgetHelp, TRUE,
			WINDOW_Position, WPOS_CENTERSCREEN,
			WINDOW_HorizProp,1,
			WINDOW_VertProp,1,
			WINDOW_IDCMPHook,&gw->hw->scrollerhook,
			WINDOW_IDCMPHookBits,IDCMP_IDCMPUPDATE,
//			WA_ReportMouse,TRUE,
			WA_IDCMP,IDCMP_MOUSEBUTTONS | IDCMP_NEWSIZE, // | IDCMP_MOUSEMOVE,
			WINDOW_ParentGroup, gw->hw->objects[GID_MAIN] = LayoutVObj,
				LAYOUT_AddChild, gw->hw->objects[GID_BROWSER] = SpaceObj,
					GA_ID,GID_BROWSER,
//					SPACE_MinWidth,width,
//					SPACE_MinHeight,height,
				SpaceEnd,
			EndGroup,
		EndWindow;

		gw->hw->win = (struct Window *)RA_OpenWindow(gw->hw->objects[OID_MAIN]);
		ami_gui_win_list_add(gw->hw, AMINS_HISTORYWINDOW, &ami_localhistory_table);

		GetAttr(WINDOW_HorizObject,gw->hw->objects[OID_MAIN],(ULONG *)&gw->hw->objects[OID_HSCROLL]);
		GetAttr(WINDOW_VertObject,gw->hw->objects[OID_MAIN],(ULONG *)&gw->hw->objects[OID_VSCROLL]);

		RefreshSetGadgetAttrs((APTR)gw->hw->objects[OID_VSCROLL],gw->hw->win,NULL,
			GA_ID,OID_VSCROLL,
			SCROLLER_Top,0,
			ICA_TARGET,ICTARGET_IDCMP,
			TAG_DONE);

		RefreshSetGadgetAttrs((APTR)gw->hw->objects[OID_HSCROLL],gw->hw->win,NULL,
			GA_ID,OID_HSCROLL,
			SCROLLER_Top,0,
			ICA_TARGET,ICTARGET_IDCMP,
			TAG_DONE);
	}

	ami_history_redraw(gw->hw);
}


/**
 * Handle mouse clicks in the history window.
 *
 * \return true if the event was handled, false to pass it on
 */

static bool ami_history_click(struct history_window *hw, uint16 code)
{
	int x, y;
	struct IBox *bbox;
	ULONG xs, ys;

	if(ami_gui_get_space_box(hw->objects[GID_BROWSER], &bbox) != NSERROR_OK) {
		amiga_warn_user("NoMemory", "");
		return false;
	}

	GetAttr(SCROLLER_Top,hw->objects[OID_HSCROLL],(ULONG *)&xs);
	x = hw->win->MouseX - bbox->Left +xs;
	GetAttr(SCROLLER_Top,hw->objects[OID_VSCROLL],(ULONG *)&ys);
	y = hw->win->MouseY - bbox->Top + ys;

	ami_gui_free_space_box(bbox);

	switch(code)
	{
		case SELECTUP:
			browser_window_history_click(hw->gw->bw, x, y, false);
			ami_history_redraw(hw);
			ami_schedule_redraw(hw->gw->shared, true);
		break;

		case MIDDLEUP:
			browser_window_history_click(hw->gw->bw, x, y, true);
			ami_history_redraw(hw);
		break;
	}

	return true;
}

void ami_history_close(struct history_window *hw)
{
	ami_plot_ra_free(hw->gg);
	hw->gw->hw = NULL;
	DisposeObject(hw->objects[OID_MAIN]);
	ami_gui_win_list_remove(hw);
}

static BOOL ami_history_event(void *w)
{
	/* return TRUE if window destroyed */
	struct history_window *hw = (struct history_window *)w;
	ULONG result = 0;
	uint16 code;
	const char *url;
	struct IBox *bbox;
	ULONG xs, ys;

	while((result = RA_HandleInput(hw->objects[OID_MAIN],&code)) != WMHI_LASTMSG)
	{
       	switch(result & WMHI_CLASSMASK) // class
   		{
/* no menus yet, copied in as will probably need it later
			case WMHI_MENUPICK:
				item = ItemAddress(gwin->win->MenuStrip,code);
				while (code != MENUNULL)
				{
					ami_menupick(code,gwin);
					if(win_destroyed) break;
					code = item->NextSelect;
				}
			break;
*/

			case WMHI_MOUSEMOVE:
				GetAttr(SCROLLER_Top, hw->objects[OID_HSCROLL], (ULONG *)&xs);
				GetAttr(SCROLLER_Top, hw->objects[OID_VSCROLL], (ULONG *)&ys);

				if(ami_gui_get_space_box(hw->objects[GID_BROWSER], &bbox) != NSERROR_OK) {
					amiga_warn_user("NoMemory", "");
					break;
				}

				url = browser_window_history_position_url(hw->gw->bw,
					hw->win->MouseX - bbox->Left + xs,
					hw->win->MouseY - bbox->Top + ys);

				ami_gui_free_space_box(bbox);

				RefreshSetGadgetAttrs((APTR)hw->objects[GID_BROWSER],
					hw->win, NULL,
					GA_HintInfo, url,
					TAG_DONE);
			break;

			case WMHI_NEWSIZE:
				ami_history_redraw(hw);
			break;

			case WMHI_MOUSEBUTTONS:
				ami_history_click(hw,code);
			break;

			case WMHI_CLOSEWINDOW:
				ami_history_close(hw);
				return TRUE;
			break;
		}
	}
	return FALSE;
}

static void ami_history_update_extent(struct history_window *hw)
{
	struct IBox *bbox;
	int width, height;

	browser_window_history_size(hw->gw->bw, &width, &height);
	if(ami_gui_get_space_box(hw->objects[GID_BROWSER], &bbox) != NSERROR_OK) {
		amiga_warn_user("NoMemory", "");
		return;
	}

	RefreshSetGadgetAttrs((APTR)hw->objects[OID_VSCROLL], hw->win, NULL,
		GA_ID, OID_VSCROLL,
		SCROLLER_Total, height,
		SCROLLER_Visible, bbox->Height,
		ICA_TARGET, ICTARGET_IDCMP,
		TAG_DONE);

	RefreshSetGadgetAttrs((APTR)hw->objects[OID_HSCROLL], hw->win, NULL,
		GA_ID, OID_HSCROLL,
		SCROLLER_Total, width,
		SCROLLER_Visible, bbox->Width,
		ICA_TARGET, ICTARGET_IDCMP,
		TAG_DONE);

	ami_gui_free_space_box(bbox);
}

HOOKF(void, ami_history_scroller_hook, Object *, object, struct IntuiMessage *)
{
	ULONG gid;
	struct history_window *hw = hook->h_Data;

	if (msg->Class == IDCMP_IDCMPUPDATE) 
	{ 
		gid = GetTagData( GA_ID, 0, msg->IAddress ); 

		switch( gid ) 
		{ 
 			case OID_HSCROLL: 
 			case OID_VSCROLL: 
				ami_history_redraw(hw);
 			break; 
		} 
	}
//	ReplyMsg((struct Message *)msg);
}