netsurf/amiga/menu.c

438 lines
13 KiB
C
Raw Normal View History

/*
* Copyright 2008 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/browser.h"
#include "amiga/menu.h"
#include "utils/messages.h"
#include "amiga/utf8.h"
#include <libraries/gadtools.h>
#include <proto/asl.h>
#include "amiga/options.h"
#include "desktop/gui.h"
#include "amiga/hotlist.h"
#include <proto/dos.h>
#include "amiga/gui.h"
#include "amiga/save_pdf.h"
#include "desktop/save_text.h"
#include "desktop/save_pdf/pdf_plotters.h"
#include <string.h>
#include "amiga/tree.h"
#include "amiga/history.h"
#include "amiga/cookies.h"
void ami_menu_scan(struct tree *tree,struct NewMenu *menu);
void ami_menu_scan_2(struct tree *tree,struct node *root,WORD *gen,ULONG *item,struct NewMenu *menu);
void ami_free_menulabs(void)
{
int i;
for(i=0;i<=AMI_MENU_MAX;i++)
{
if(menulab[i] && (menulab[i] != NM_BARLABEL)) ami_utf8_free(menulab[i]);
}
}
void ami_init_menulabs(void)
{
menulab[0] = ami_utf8_easy((char *)messages_get("Project"));
menulab[1] = ami_utf8_easy((char *)messages_get("NewWindowNS"));
menulab[2] = ami_utf8_easy((char *)messages_get("NewTab"));
menulab[3] = NM_BARLABEL;
menulab[4] = ami_utf8_easy((char *)messages_get("SaveAs"));
menulab[5] = ami_utf8_easy((char *)messages_get("Source"));
menulab[6] = ami_utf8_easy((char *)messages_get("TextNS"));
menulab[7] = ami_utf8_easy((char *)messages_get("PDF"));
menulab[8] = NM_BARLABEL;
menulab[9] = ami_utf8_easy((char *)messages_get("CloseTab"));
menulab[10] = ami_utf8_easy((char *)messages_get("CloseWindow"));
menulab[11] = NM_BARLABEL;
menulab[12] = ami_utf8_easy((char *)messages_get("Quit"));
menulab[13] = ami_utf8_easy((char *)messages_get("Edit"));
menulab[14] = ami_utf8_easy((char *)messages_get("CopyNS"));
menulab[15] = ami_utf8_easy((char *)messages_get("Paste"));
menulab[16] = ami_utf8_easy((char *)messages_get("SelectAllNS"));
menulab[17] = ami_utf8_easy((char *)messages_get("ClearNS"));
menulab[18] = ami_utf8_easy((char *)messages_get("Browser"));
menulab[19] = ami_utf8_easy((char *)messages_get("HistGlobalNS"));
menulab[20] = ami_utf8_easy((char *)messages_get("ShowCookies"));
menulab[21] = ami_utf8_easy((char *)messages_get("Hotlist"));
menulab[22] = ami_utf8_easy((char *)messages_get("HotlistAdd"));
menulab[23] = ami_utf8_easy((char *)messages_get("HotlistShowNS"));
menulab[24] = NM_BARLABEL;
menulab[65] = ami_utf8_easy((char *)messages_get("Settings"));
menulab[66] = ami_utf8_easy((char *)messages_get("SnapshotWindow"));
menulab[67] = ami_utf8_easy((char *)messages_get("SettingsSave"));
}
struct NewMenu *ami_create_menu(ULONG type)
{
int i;
ULONG menuflags = 0;
STATIC struct NewMenu menu[] = {
{NM_TITLE,0,0,0,0,0,}, // project
{ NM_ITEM,0,"N",0,0,0,}, // new window
{ NM_ITEM,0,"T",0,0,0,}, // new tab
{ NM_ITEM,NM_BARLABEL,0,0,0,0,},
{ NM_ITEM,0,0,0,0,0,}, // save
{ NM_SUB,0,"S",0,0,0,}, // save as source
{ NM_SUB,0,0,0,0,0,}, // save as text
{ NM_SUB,0,0,0,0,0,}, // save as pdf
{ NM_ITEM,NM_BARLABEL,0,0,0,0,},
{ NM_ITEM,0,"K",0,0,0,}, // close tab
{ NM_ITEM,0,0,0,0,0,}, // close window
{ NM_ITEM,NM_BARLABEL,0,0,0,0,},
{ NM_ITEM,0,"Q",0,0,0,}, // quit
{NM_TITLE,0,0,0,0,0,}, // edit
{ NM_ITEM,0,"C",0,0,0,}, // copy
{ NM_ITEM,0,"V",0,0,0,}, // paste
{ NM_ITEM,0,"A",0,0,0,}, // select all
{ NM_ITEM,0,"Z",0,0,0,}, // clear selection
{NM_TITLE,0,0,0,0,0,}, // browser
{ NM_ITEM,0,0,0,0,0,}, // global history
{ NM_ITEM,0,0,0,0,0,}, // cookies
{NM_TITLE,0,0,0,0,0,}, // hotlist
{ NM_ITEM,0,0,0,0,0,}, // add to hotlist
{ NM_ITEM,0,"H",0,0,0,}, // show hotlist (treeview)
{ NM_ITEM,NM_BARLABEL,0,0,0,0,},
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{ NM_IGNORE,0,0,0,0,0,}, // ** hotlist entry **
{NM_TITLE,0,0,0,0,0,}, // settings
{ NM_ITEM,0,0,0,0,0,}, // snapshot window
{ NM_ITEM,0,0,0,0,0,}, // save settings
{ NM_END,0,0,0,0,0,},
};
if(type != BROWSER_WINDOW_NORMAL)
{
menuflags = NM_ITEMDISABLED;
}
for(i=0;i<=AMI_MENU_MAX;i++)
{
menu[i].nm_Label = menulab[i];
}
menu[1].nm_Flags = menuflags;
menu[2].nm_Flags = menuflags;
menu[9].nm_Flags = menuflags;
menu[10].nm_Flags = menuflags;
#ifndef WITH_PDF_EXPORT
menu[7].nm_Flags = NM_ITEMDISABLED;
#endif
ami_menu_scan(hotlist,menu);
return(menu);
}
void ami_menu_scan(struct tree *tree,struct NewMenu *menu)
{
struct node *root = tree->root->child;
struct node_element *element=NULL;
struct node *node;
static WORD gen = 0;
static ULONG item;
item = AMI_MENU_HOTLIST;
for (node = root; node; node = node->next)
{
element = tree_find_element(node, TREE_ELEMENT_NAME);
if(!element) element = tree_find_element(node, TREE_ELEMENT_TITLE);
if(element && (strcmp(element->text,"Menu")==0))
{
// found menu
ami_menu_scan_2(tree,node,&gen,&item,menu);
}
}
}
void ami_menu_scan_2(struct tree *tree,struct node *root,WORD *gen,ULONG *item,struct NewMenu *menu)
{
struct node *tempnode;
struct node_element *element=NULL;
struct node *node;
*gen = *gen + 1;
for (node = root; node; node = node->next)
{
element = tree_find_element(node, TREE_ELEMENT_TITLE);
if((*gen > 1) && (*gen < 4))
{
if(*item >= AMI_MENU_HOTLIST_MAX) return;
if(*gen == 2) menu[*item].nm_Type = NM_ITEM;
if(*gen == 3) menu[*item].nm_Type = NM_SUB;
if(strcmp(element->text,"--"))
{
menulab[*item] = ami_utf8_easy(element->text);
}
else
{
menulab[*item] = NM_BARLABEL;
}
menu[*item].nm_Label = menulab[*item];
element = tree_find_element(node, TREE_ELEMENT_URL);
if(element && element->text) menu[*item].nm_UserData = element->text;
if(node->folder && (!node->child)) menu[*item].nm_Flags = NM_ITEMDISABLED;
*item = *item + 1;
}
if (node->child)
{
ami_menu_scan_2(tree,node->child,gen,item,menu);
}
}
*gen = *gen - 1;
}
void ami_menupick(ULONG code,struct gui_window_2 *gwin,struct MenuItem *item)
{
struct browser_window *bw;
struct gui_window tgw;
ULONG menunum=0,itemnum=0,subnum=0;
menunum = MENUNUM(code);
itemnum = ITEMNUM(code);
subnum = SUBNUM(code);
bool openwin=false;
bool opentab=true;
if(option_force_tabs)
{
openwin=true;
opentab=false;
}
tgw.tab_node = NULL;
tgw.tab = 0;
tgw.shared = gwin;
switch(menunum)
{
case 0: // project
switch(itemnum)
{
case 0: // new window
bw = browser_window_create(gwin->bw->current_content->url,gwin->bw, 0, true, openwin);
break;
case 1: // new tab
bw = browser_window_create(gwin->bw->current_content->url,gwin->bw, 0, true, opentab);
break;
case 3: // save
switch(subnum)
{
BPTR fh=0;
char fname[1024];
case 0:
if(AslRequestTags(filereq,
ASLFR_TitleText,messages_get("NetSurf"),
ASLFR_Screen,scrn,
ASLFR_DoSaveMode,TRUE,
ASLFR_InitialFile,FilePart(gwin->bw->current_content->url),
TAG_DONE))
{
strlcpy(&fname,filereq->fr_Drawer,1024);
AddPart(fname,filereq->fr_File,1024);
ami_update_pointer(gwin->win,GUI_POINTER_WAIT);
if(fh = FOpen(fname,MODE_NEWFILE,0))
{
FWrite(fh,gwin->bw->current_content->source_data,1,gwin->bw->current_content->source_size);
FClose(fh);
SetComment(fname,gwin->bw->current_content->url);
}
ami_update_pointer(gwin->win,GUI_POINTER_DEFAULT);
}
break;
case 1:
if(AslRequestTags(filereq,
ASLFR_TitleText,messages_get("NetSurf"),
ASLFR_Screen,scrn,
ASLFR_DoSaveMode,TRUE,
ASLFR_InitialFile,FilePart(gwin->bw->current_content->url),
TAG_DONE))
{
strlcpy(&fname,filereq->fr_Drawer,1024);
AddPart(fname,filereq->fr_File,1024);
ami_update_pointer(gwin->win,GUI_POINTER_WAIT);
save_as_text(gwin->bw->current_content,fname);
SetComment(fname,gwin->bw->current_content->url);
ami_update_pointer(gwin->win,GUI_POINTER_DEFAULT);
}
break;
case 2:
#ifdef WITH_PDF_EXPORT
if(AslRequestTags(filereq,
ASLFR_TitleText,messages_get("NetSurf"),
ASLFR_Screen,scrn,
ASLFR_DoSaveMode,TRUE,
ASLFR_InitialFile,FilePart(gwin->bw->current_content->url),
TAG_DONE))
{
strlcpy(&fname,filereq->fr_Drawer,1024);
AddPart(fname,filereq->fr_File,1024);
ami_update_pointer(gwin->win,GUI_POINTER_WAIT);
pdf_set_scale(DEFAULT_EXPORT_SCALE);
save_as_pdf(gwin->bw->current_content,fname);
SetComment(fname,gwin->bw->current_content->url);
ami_update_pointer(gwin->win,GUI_POINTER_DEFAULT);
}
#endif
break;
}
break;
case 5: // close tab
browser_window_destroy(gwin->bw);
break;
case 6: // close window
ami_close_all_tabs(gwin);
break;
case 8: // quit
ami_quit_netsurf();
break;
}
break;
case 1: // edit
switch(itemnum)
{
case 0: // copy
gui_copy_to_clipboard(gwin->bw->sel);
browser_window_key_press(gwin->bw, 26);
break;
case 1: // paste
gui_paste_from_clipboard(&tgw,0,0);
break;
case 2: // select all
browser_window_key_press(gwin->bw, 1);
break;
case 3: // clear selection
browser_window_key_press(gwin->bw, 26);
break;
}
break;
case 2:
switch(itemnum)
{
case 0: // global history
ami_open_tree(global_history_tree,AMI_TREE_HISTORY);
break;
case 1: // cookies tree
ami_open_tree(cookies_tree,AMI_TREE_COOKIES);
break;
}
break;
case 3: // hotlist
switch(itemnum)
{
case 0: // add
ami_hotlist_add(hotlist->root,gwin->bw->current_content);
options_save_tree(hotlist,"Resources/Hotlist","NetSurf hotlist");
break;
case 1: // show
ami_open_tree(hotlist,AMI_TREE_HOTLIST);
break;
default: // bookmarks
if(GTMENUITEM_USERDATA(item))
browser_window_go(gwin->bw,GTMENUITEM_USERDATA(item),NULL, true);
break;
}
break;
case 4: // settings
switch(itemnum)
{
case 0: // snapshot
option_window_x = gwin->win->LeftEdge;
option_window_y = gwin->win->TopEdge;
option_window_width = gwin->win->Width;
option_window_height = gwin->win->Height;
break;
case 1: // save settings
options_write("Resources/Options");
break;
}
break;
}
}