summaryrefslogtreecommitdiff
path: root/frontends/amiga/history.c
blob: fd33a67f3838108fa9ab14e51810dd7e048024bb (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
/*
 * Copyright 2017 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
 * Implementation of Amiga global history viewer using core windows.
 */

#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#include <proto/asl.h>
#include <proto/dos.h>
#include <proto/intuition.h>

#include <classes/window.h>
#include <gadgets/layout.h>
#include <gadgets/space.h>

#include <reaction/reaction_macros.h>

#include "desktop/global_history.h"
#include "netsurf/browser_window.h"
#include "netsurf/keypress.h"
#include "netsurf/plotters.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/nsoption.h"

#include "amiga/corewindow.h"
#include "amiga/drag.h"
#include "amiga/file.h"
#include "amiga/history.h"
#include "amiga/libs.h"
#include "amiga/menu.h"
#include "amiga/theme.h"
#include "amiga/utf8.h"

enum {
	/* Project menu */
	AMI_HISTORY_M_PROJECT = 0,
	 AMI_HISTORY_M_EXPORT,
	 AMI_HISTORY_M_BAR_P1,
	 AMI_HISTORY_M_EXPAND,
	  AMI_HISTORY_M_EXPAND_ALL,
	  AMI_HISTORY_M_EXPAND_FOLDERS,
	  AMI_HISTORY_M_EXPAND_LINKS,
	 AMI_HISTORY_M_COLLAPSE,
	  AMI_HISTORY_M_COLLAPSE_ALL,
	  AMI_HISTORY_M_COLLAPSE_FOLDERS,
	  AMI_HISTORY_M_COLLAPSE_LINKS,
	 AMI_HISTORY_M_BAR_P2,
	 AMI_HISTORY_M_SNAPSHOT,
	 AMI_HISTORY_M_BAR_P3,
	 AMI_HISTORY_M_CLOSE,
	/* Edit menu */
	AMI_HISTORY_M_EDIT,
	 AMI_HISTORY_M_SELECTALL,
	 AMI_HISTORY_M_CLEAR,
	 AMI_HISTORY_M_BAR_E1,
	 AMI_HISTORY_M_DELETE,
	AMI_HISTORY_M_LAST
};

/**
 * Amiga history viewer window context
 */
struct ami_history_global_window {
	/** Amiga core window context */
	struct ami_corewindow core;

	struct ami_menu_data *menu_data[AMI_HISTORY_M_LAST + 1];
	struct Menu *imenu; /* Intuition menu */
};

static struct ami_history_global_window *history_window = NULL;


static void
ami_history_global_menu_free(struct ami_history_global_window *history_win)
{
	SetAttrs(history_win->core.objects[GID_CW_WIN],
		WINDOW_MenuStrip, NULL,
	TAG_DONE);
	
	ami_menu_free_menu(history_win->menu_data, AMI_HISTORY_M_LAST, history_win->imenu);
}

/**
 * destroy a previously created history view
 */
static void
ami_history_global_destroy(struct ami_corewindow *ami_cw)
{
	nserror res;

	if(history_window == NULL)
		return;

	res = global_history_fini();
	if (res == NSERROR_OK) {
		ami_history_global_menu_free(history_window);
		res = ami_corewindow_fini(&history_window->core); /* closes the window for us, frees history_win */
		history_window = NULL;
	}
}


/**
 * callback for mouse action for history viewer on core window
 *
 * \param ami_cw The Amiga core window structure.
 * \param mouse_state netsurf mouse state on event
 * \param x location of event
 * \param y location of event
 * \return NSERROR_OK on success otherwise apropriate error code
 */
static nserror
ami_history_global_mouse(struct ami_corewindow *ami_cw,
					browser_mouse_state mouse_state,
					int x, int y)
{
	global_history_mouse_action(mouse_state, x, y);

	return NSERROR_OK;
}

/**
 * callback for keypress for history viewer on core window
 *
 * \param ami_cw The Amiga core window structure.
 * \param nskey The netsurf key code
 * \return NSERROR_OK on success otherwise apropriate error code
 */
static nserror
ami_history_global_key(struct ami_corewindow *ami_cw, uint32_t nskey)
{
	if (global_history_keypress(nskey)) {
			return NSERROR_OK;
	}
	return NSERROR_NOT_IMPLEMENTED;
}

/**
 * callback on draw event for history viewer on core window
 *
 * \param ami_cw The Amiga core window structure.
 * \param x The x coordinate of global history area to redraw
 * \param y The y coordinate of global history area to redraw
 * \param r The rectangle of the window that needs updating.
 * \param ctx The drawing context
 * \return NSERROR_OK on success otherwise apropriate error code
 */
static nserror
ami_history_global_draw(struct ami_corewindow *ami_cw,
			int x, int y, struct rect *r,
			struct redraw_context *ctx)
{
	global_history_redraw(x, y, r, ctx);

	return NSERROR_OK;
}

/**
 * callback on drag end for history viewer
 *
 * \param ami_cw The Amiga core window structure.
 * \param x mouse x co-ordinate
 * \param y mouse y co-ordinate
 * \return NSERROR_OK on success otherwise apropriate error code
 */
static nserror
ami_history_global_drag_end(struct ami_corewindow *ami_cw, int x, int y)
{
	struct nsurl *url = NULL;
	const char *title = NULL;
	bool ok = false;
	struct gui_window_2 *gwin;
	struct ami_corewindow *cw;

	if(global_history_has_selection()) {
		ok = global_history_get_selection(&url, &title);
	}
	
	if((ok == false) || (url == NULL)) {
		ami_gui_beep();
	} else if(url) {
		if((gwin = ami_window_at_pointer(AMINS_WINDOW))) {
			browser_window_navigate(ami_gui2_get_browser_window(gwin),
					url,
					NULL,
					BW_NAVIGATE_HISTORY,
					NULL,
					NULL,
					NULL);
		} else if((cw = (struct ami_corewindow *)ami_window_at_pointer(AMINS_COREWINDOW)) &&
			(ami_cw->icon_drop != NULL)) {
			cw->icon_drop(cw, url, title, x, y);
		}
	}
	return NSERROR_OK;
}

/**
 * menu stuff
 */

 /* menu hook functions */
HOOKF(void, ami_history_global_menu_item_project_export, APTR, window, struct IntuiMessage *)
{
	char fname[1024];
	struct ami_corewindow *ami_cw;
	GetAttr(WINDOW_UserData, (Object *)window, (ULONG *)&ami_cw);

	if(AslRequestTags(savereq,
		ASLFR_Window, ami_cw->win,
		ASLFR_SleepWindow, TRUE,
		ASLFR_TitleText, messages_get("NetSurf"),
		ASLFR_Screen, ami_gui_get_screen(),
		ASLFR_InitialFile, "history.html",
		TAG_DONE)) {
			strlcpy(fname, savereq->fr_Drawer, 1024);
			AddPart(fname, savereq->fr_File, 1024);
			ami_update_pointer(ami_cw->win, GUI_POINTER_WAIT);
			global_history_export(fname, NULL);
			ami_update_pointer(ami_cw->win, GUI_POINTER_DEFAULT);
	}
}
									
HOOKF(void, ami_history_global_menu_item_project_expand_all, APTR, window, struct IntuiMessage *)
{
	global_history_expand(false);
}
 
HOOKF(void, ami_history_global_menu_item_project_expand_folders, APTR, window, struct IntuiMessage *)
{
	global_history_expand(true);
}

HOOKF(void, ami_history_global_menu_item_project_expand_links, APTR, window, struct IntuiMessage *)
{
	global_history_expand(false);
}

HOOKF(void, ami_history_global_menu_item_project_collapse_all, APTR, window, struct IntuiMessage *)
{
	global_history_contract(true);
}
 
HOOKF(void, ami_history_global_menu_item_project_collapse_folders, APTR, window, struct IntuiMessage *)
{
	global_history_contract(true);
}

HOOKF(void, ami_history_global_menu_item_project_collapse_links, APTR, window, struct IntuiMessage *)
{
	global_history_contract(false);
}

HOOKF(void, ami_history_global_menu_item_project_snapshot, APTR, window, struct IntuiMessage *)
{
	struct ami_corewindow *ami_cw;
	GetAttr(WINDOW_UserData, (Object *)window, (ULONG *)&ami_cw);

	nsoption_set_int(history_window_ypos, ami_cw->win->TopEdge);
	nsoption_set_int(history_window_xpos, ami_cw->win->LeftEdge);
	nsoption_set_int(history_window_xsize, ami_cw->win->Width);
	nsoption_set_int(history_window_ysize, ami_cw->win->Height);
}

HOOKF(void, ami_history_global_menu_item_project_close, APTR, window, struct IntuiMessage *)
{
	struct ami_corewindow *ami_cw;
	GetAttr(WINDOW_UserData, (Object *)window, (ULONG *)&ami_cw);
	
	ami_cw->close_window = true;
}

HOOKF(void, ami_history_global_menu_item_edit_select_all, APTR, window, struct IntuiMessage *)
{
	global_history_keypress(NS_KEY_SELECT_ALL);
}

HOOKF(void, ami_history_global_menu_item_edit_clear, APTR, window, struct IntuiMessage *)
{
	global_history_keypress(NS_KEY_CLEAR_SELECTION);
}

HOOKF(void, ami_history_global_menu_item_edit_delete, APTR, window, struct IntuiMessage *)
{
	global_history_keypress(NS_KEY_DELETE_LEFT);
}


/* menu setup */

static void ami_history_global_menulabs(struct ami_menu_data **md)
{
	ami_menu_alloc_item(md, AMI_HISTORY_M_PROJECT, NM_TITLE, "Tree", NULL, NULL, NULL, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_EXPORT,   NM_ITEM, "TreeExport", "S", "TBImages:list_save",
		ami_history_global_menu_item_project_export, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_BAR_P1, NM_ITEM, NM_BARLABEL, NULL, NULL, NULL, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_EXPAND,   NM_ITEM, "Expand", NULL, "TBImages:list_folderunfold", NULL, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_EXPAND_ALL,   NM_SUB, "All", "+", NULL,
		ami_history_global_menu_item_project_expand_all, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_EXPAND_FOLDERS,   NM_SUB, "Folders", NULL, NULL,
		ami_history_global_menu_item_project_expand_folders, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_EXPAND_LINKS,   NM_SUB, "Links", NULL, NULL,
		ami_history_global_menu_item_project_expand_links, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_COLLAPSE,   NM_ITEM, "Collapse", NULL, "TBImages:list_folderfold", NULL, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_COLLAPSE_ALL,   NM_SUB, "All", "-", NULL,
		ami_history_global_menu_item_project_collapse_all, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_COLLAPSE_FOLDERS,   NM_SUB, "Folders", NULL, NULL,
		ami_history_global_menu_item_project_collapse_folders, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_COLLAPSE_LINKS,   NM_SUB, "Links", NULL, NULL,
		ami_history_global_menu_item_project_collapse_links, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_BAR_P2, NM_ITEM, NM_BARLABEL, NULL, NULL, NULL, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_SNAPSHOT,   NM_ITEM, "SnapshotWindow", NULL, "TBImages:list_hold",
		ami_history_global_menu_item_project_snapshot, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_BAR_P3, NM_ITEM, NM_BARLABEL, NULL, NULL, NULL, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_CLOSE,   NM_ITEM, "CloseWindow", "K", "TBImages:list_cancel",
		ami_history_global_menu_item_project_close, NULL, 0);

	ami_menu_alloc_item(md, AMI_HISTORY_M_EDIT, NM_TITLE, "Edit", NULL, NULL, NULL, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_SELECTALL,   NM_ITEM, "SelectAllNS", "A", NSA_SPACE,
		ami_history_global_menu_item_edit_select_all, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_CLEAR,   NM_ITEM, "ClearNS", NULL, NSA_SPACE,
		ami_history_global_menu_item_edit_clear, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_BAR_E1, NM_ITEM, NM_BARLABEL, NULL, NULL, NULL, NULL, 0);
	ami_menu_alloc_item(md, AMI_HISTORY_M_DELETE,   NM_ITEM, "TreeDelete", "Del", "TBImages:list_delete",
		ami_history_global_menu_item_edit_delete, NULL, 0);

	ami_menu_alloc_item(md, AMI_HISTORY_M_LAST,   NM_END, NULL, NULL, NULL, NULL, NULL, 0);
}

static struct Menu *
ami_history_global_menu_create(struct ami_history_global_window *history_win)
{
	ami_history_global_menulabs(history_win->menu_data);
	history_win->imenu = ami_menu_layout(history_win->menu_data, AMI_HISTORY_M_LAST);
	if(history_win->imenu == NULL) return NULL;

	return history_win->imenu;
}


static nserror
ami_history_global_create_window(struct ami_history_global_window *history_win)
{
	struct ami_corewindow *ami_cw = (struct ami_corewindow *)&history_win->core;
	ULONG refresh_mode = WA_SmartRefresh;

	if(nsoption_bool(window_simple_refresh) == true) {
		refresh_mode = WA_SimpleRefresh;
	}

	ami_cw->objects[GID_CW_WIN] = WindowObj,
  	    WA_ScreenTitle, ami_gui_get_screen_title(),
       	WA_Title, ami_cw->wintitle,
       	WA_Activate, TRUE,
       	WA_DepthGadget, TRUE,
       	WA_DragBar, TRUE,
       	WA_CloseGadget, TRUE,
       	WA_SizeGadget, TRUE,
		WA_SizeBRight, TRUE,
		WA_Top, nsoption_int(history_window_ypos),
		WA_Left, nsoption_int(history_window_xpos),
		WA_Width, nsoption_int(history_window_xsize),
		WA_Height, nsoption_int(history_window_ysize),
		WA_PubScreen, ami_gui_get_screen(),
		WA_ReportMouse, TRUE,
		refresh_mode, TRUE,
		WA_IDCMP, IDCMP_MOUSEMOVE | IDCMP_MOUSEBUTTONS | IDCMP_NEWSIZE |
				IDCMP_RAWKEY | IDCMP_GADGETUP | IDCMP_IDCMPUPDATE |
				IDCMP_EXTENDEDMOUSE | IDCMP_SIZEVERIFY | IDCMP_REFRESHWINDOW,
		WINDOW_IDCMPHook, &ami_cw->idcmp_hook,
		WINDOW_IDCMPHookBits, IDCMP_IDCMPUPDATE | IDCMP_EXTENDEDMOUSE |
				IDCMP_SIZEVERIFY | IDCMP_REFRESHWINDOW,
		WINDOW_SharedPort, ami_gui_get_shared_msgport(),
		WINDOW_HorizProp, 1,
		WINDOW_VertProp, 1,
		WINDOW_UserData, history_win,
		WINDOW_MenuStrip, ami_history_global_menu_create(history_win),
		WINDOW_MenuUserData, WGUD_HOOK,
		WINDOW_IconifyGadget, FALSE,
		WINDOW_Position, WPOS_CENTERSCREEN,
		WINDOW_ParentGroup, ami_cw->objects[GID_CW_MAIN] = LayoutVObj,
			LAYOUT_AddChild, ami_cw->objects[GID_CW_DRAW] = SpaceObj,
				GA_ID, GID_CW_DRAW,
				SPACE_Transparent, TRUE,
				SPACE_BevelStyle, BVS_DISPLAY,
				GA_RelVerify, TRUE,
   			SpaceEnd,
		EndGroup,
	EndWindow;

	if(ami_cw->objects[GID_CW_WIN] == NULL) {
		return NSERROR_NOMEM;
	}

	return NSERROR_OK;
}

/* exported interface documented in amiga/cookies.h */
nserror ami_history_global_present(void)
{
	struct ami_history_global_window *ncwin;
	nserror res;

	if(history_window != NULL) {
		//windowtofront()
		return NSERROR_OK;
	}

	ncwin = calloc(1, sizeof(struct ami_history_global_window));
	if (ncwin == NULL) {
		return NSERROR_NOMEM;
	}

	ncwin->core.wintitle = ami_utf8_easy((char *)messages_get("GlobalHistory"));

	res = ami_history_global_create_window(ncwin);
	if (res != NSERROR_OK) {
		NSLOG(netsurf, INFO, "SSL UI builder init failed");
		ami_utf8_free(ncwin->core.wintitle);
		free(ncwin);
		return res;
	}

	/* initialise Amiga core window */
	ncwin->core.draw = ami_history_global_draw;
	ncwin->core.key = ami_history_global_key;
	ncwin->core.mouse = ami_history_global_mouse;
	ncwin->core.close = ami_history_global_destroy;
	ncwin->core.event = NULL;
	ncwin->core.drag_end = ami_history_global_drag_end;
	ncwin->core.icon_drop = NULL;

	res = ami_corewindow_init(&ncwin->core);
	if (res != NSERROR_OK) {
		ami_utf8_free(ncwin->core.wintitle);
		DisposeObject(ncwin->core.objects[GID_CW_WIN]);
		free(ncwin);
		return res;
	}

	res = global_history_init(ncwin->core.cb_table, (struct core_window *)ncwin);
	if (res != NSERROR_OK) {
		ami_utf8_free(ncwin->core.wintitle);
		DisposeObject(ncwin->core.objects[GID_CW_WIN]);
		free(ncwin);
		return res;
	}

	history_window = ncwin;

	return NSERROR_OK;
}