add link context menu to gtk and add link copying and bookmarking

This commit is contained in:
Vincent Sanders 2014-07-07 23:29:06 +01:00
parent 07f1ae7461
commit 5ce4f11eac
7 changed files with 251 additions and 109 deletions

View File

@ -53,6 +53,13 @@ static bool nsgtk_menu_add_image_item(GtkMenu *menu,
return true;
}
#define NEW_MENU(n, m) \
n = malloc(sizeof(*n)); \
if (n == NULL) { \
return NULL; \
} \
n->m##_menu = GTK_MENU(gtk_menu_new())
#define IMAGE_ITEM(p, q, r, s, t)\
nsgtk_menu_add_image_item(s->p##_menu, &(s->q##_menuitem), #r,\
#r "Accel", t)
@ -66,7 +73,7 @@ static bool nsgtk_menu_add_image_item(GtkMenu *menu,
GTK_WIDGET(s->q##_menuitem));\
gtk_widget_show(GTK_WIDGET(s->q##_menuitem));\
}
#define SET_SUBMENU(q, r) \
do { \
r->q##_submenu = nsgtk_menu_##q##_submenu(group); \
@ -76,7 +83,7 @@ static bool nsgtk_menu_add_image_item(GtkMenu *menu,
gtk_menu_item_set_submenu(GTK_MENU_ITEM(r->q##_menuitem), \
GTK_WIDGET(r->q##_submenu->q##_menu)); \
} \
} while(0)
} while(0)
#define ADD_NAMED_SEP(q, r, s) \
do { \
@ -85,7 +92,7 @@ static bool nsgtk_menu_add_image_item(GtkMenu *menu,
gtk_menu_shell_append(GTK_MENU_SHELL(s->q##_menu), s->r##_separator); \
gtk_widget_show(s->r##_separator); \
} \
} while(0)
} while(0)
#define ADD_SEP(q, r) \
do { \
@ -94,7 +101,7 @@ static bool nsgtk_menu_add_image_item(GtkMenu *menu,
gtk_menu_shell_append(GTK_MENU_SHELL(r->q##_menu), w); \
gtk_widget_show(w); \
} \
} while(0)
} while(0)
#define ATTACH_PARENT(parent, msgname, menuv, group) \
do { \
@ -107,9 +114,9 @@ static bool nsgtk_menu_add_image_item(GtkMenu *menu,
gtk_menu_item_set_submenu(menuv, GTK_WIDGET(menuv##_menu)); \
gtk_menu_set_accel_group(menuv##_menu, group); \
} \
} while(0)
} while(0)
/**
/**
* creates an export submenu
* \param group the 'global' in a gtk sense accelerator reference
*/
@ -135,7 +142,7 @@ static struct nsgtk_export_submenu *nsgtk_menu_export_submenu(GtkAccelGroup *gro
return ret;
}
/**
/**
* creates a scaleview submenu
* \param group the 'global' in a gtk sense accelerator reference
*/
@ -143,7 +150,7 @@ static struct nsgtk_export_submenu *nsgtk_menu_export_submenu(GtkAccelGroup *gro
static struct nsgtk_scaleview_submenu *nsgtk_menu_scaleview_submenu(
GtkAccelGroup *group)
{
struct nsgtk_scaleview_submenu *ret =
struct nsgtk_scaleview_submenu *ret =
malloc(sizeof(struct nsgtk_scaleview_submenu));
if (ret == NULL) {
warn_user(messages_get("NoMemory"), 0);
@ -161,7 +168,7 @@ static struct nsgtk_scaleview_submenu *nsgtk_menu_scaleview_submenu(
return ret;
}
/**
/**
* creates a tab navigation submenu
* \param group the 'global' in a gtk sense accelerator reference
*/
@ -186,7 +193,7 @@ static struct nsgtk_tabs_submenu *nsgtk_menu_tabs_submenu(GtkAccelGroup *group)
return ret;
}
/**
/**
* creates an images submenu
* \param group the 'global' in a gtk sense accelerator reference
*/
@ -210,7 +217,7 @@ static struct nsgtk_images_submenu *nsgtk_menu_images_submenu(GtkAccelGroup *gro
return ret;
}
/**
/**
* creates a toolbars submenu
* \param group the 'global' in a gtk sense accelerator reference
*/
@ -234,12 +241,12 @@ static struct nsgtk_toolbars_submenu *nsgtk_menu_toolbars_submenu(
if (ret->menubar_menuitem != NULL)
gtk_check_menu_item_set_active(ret->menubar_menuitem, TRUE);
CHECK_ITEM(toolbars, toolbar, gtkToolBar, ret)
if (ret->toolbar_menuitem != NULL)
if (ret->toolbar_menuitem != NULL)
gtk_check_menu_item_set_active(ret->toolbar_menuitem, TRUE);
return ret;
}
/**
/**
* creates a debugging submenu
* \param group the 'global' in a gtk sense accelerator reference
*/
@ -264,8 +271,8 @@ static struct nsgtk_debugging_submenu *nsgtk_menu_debugging_submenu(
IMAGE_ITEM(debugging, savedomtree, gtkSaveDomTree, ret, group);
return ret;
}
/**
/**
* creates the file menu
* \param group The gtk 'global' accelerator reference
* \param parent The parent menu to attach to or NULL
@ -304,7 +311,7 @@ static struct nsgtk_file_menu *nsgtk_menu_file_submenu(GtkAccelGroup *group)
return fmenu;
}
/**
/**
* creates an edit menu
* \param group the 'global' in a gtk sense accelerator reference
*/
@ -336,7 +343,7 @@ static struct nsgtk_edit_menu *nsgtk_menu_edit_submenu(GtkAccelGroup *group)
return ret;
}
/**
/**
* creates a view menu
* \param group the 'global' in a gtk sense accelerator reference
*/
@ -378,7 +385,7 @@ static struct nsgtk_view_menu *nsgtk_menu_view_submenu(GtkAccelGroup *group)
return ret;
}
/**
/**
* creates a nav menu
* \param group the 'global' in a gtk sense accelerator reference
*/
@ -415,7 +422,7 @@ static struct nsgtk_nav_menu *nsgtk_menu_nav_submenu(GtkAccelGroup *group)
return ret;
}
/**
/**
* creates a help menu
* \param group the 'global' in a gtk sense accelerator reference
*/
@ -448,9 +455,9 @@ static struct nsgtk_help_menu *nsgtk_menu_help_submenu(GtkAccelGroup *group)
*
* Generate the main menu structure and attach it to a menubar widget.
*/
struct nsgtk_bar_submenu *nsgtk_menu_bar_create(GtkMenuShell *menubar, GtkAccelGroup *group)
struct nsgtk_bar_submenu *
nsgtk_menu_bar_create(GtkMenuShell *menubar, GtkAccelGroup *group)
{
;
struct nsgtk_bar_submenu *nmenu;
nmenu = malloc(sizeof(struct nsgtk_bar_submenu));
@ -479,22 +486,13 @@ struct nsgtk_bar_submenu *nsgtk_menu_bar_create(GtkMenuShell *menubar, GtkAccelG
return nmenu;
}
/**
* Generate right click menu menu.
*
*/
struct nsgtk_popup_submenu *nsgtk_menu_popup_create(GtkAccelGroup *group)
/* exported function documented in gtk/menu.h */
struct nsgtk_popup_menu *nsgtk_popup_menu_create(GtkAccelGroup *group)
{
struct nsgtk_popup_submenu *nmenu;
struct nsgtk_popup_menu *nmenu;
nmenu = malloc(sizeof(struct nsgtk_popup_submenu));
if (nmenu == NULL) {
warn_user(messages_get("NoMemory"), 0);
return NULL;
}
NEW_MENU(nmenu, popup);
nmenu->popup_menu = GTK_MENU(gtk_menu_new());
IMAGE_ITEM(popup, file, gtkFile, nmenu, group);
SET_SUBMENU(file, nmenu);
@ -530,6 +528,26 @@ struct nsgtk_popup_submenu *nsgtk_menu_popup_create(GtkAccelGroup *group)
IMAGE_ITEM(popup, paste, gtkPaste, nmenu, group);
IMAGE_ITEM(popup, customize, gtkCustomize, nmenu, group);
return nmenu;
}
/* exported function documented in gtk/menu.h */
struct nsgtk_link_menu *
nsgtk_link_menu_create(GtkAccelGroup *group)
{
struct nsgtk_link_menu *nmenu;
NEW_MENU(nmenu, link);
IMAGE_ITEM(link, opentab, gtkOpentab, nmenu, group);
IMAGE_ITEM(link, openwin, gtkOpenwin, nmenu, group);
ADD_SEP(link, nmenu);
IMAGE_ITEM(link, save, gtkSavelink, nmenu, group);
IMAGE_ITEM(link, bookmark, gtkBookmarklink, nmenu, group);
IMAGE_ITEM(link, copy, gtkCopylink, nmenu, group);
return nmenu;
}

View File

@ -143,7 +143,7 @@ struct nsgtk_bar_submenu {
struct nsgtk_help_menu *help_submenu;
};
struct nsgtk_popup_submenu {
struct nsgtk_popup_menu {
GtkMenu *popup_menu;
GtkImageMenuItem *file_menuitem;
@ -189,7 +189,33 @@ struct nsgtk_popup_submenu {
};
struct nsgtk_link_menu {
GtkMenu *link_menu;
GtkImageMenuItem *opentab_menuitem;
GtkImageMenuItem *openwin_menuitem;
GtkImageMenuItem *save_menuitem;
GtkImageMenuItem *bookmark_menuitem;
GtkImageMenuItem *copy_menuitem;
};
/**
* Create main menu bar.
*/
struct nsgtk_bar_submenu *nsgtk_menu_bar_create(GtkMenuShell *menubar, GtkAccelGroup *group);
struct nsgtk_popup_submenu *nsgtk_menu_popup_create(GtkAccelGroup *group);
/**
* Generate right click menu menu.
*
*/
struct nsgtk_popup_menu *nsgtk_popup_menu_create(GtkAccelGroup *group);
/**
* Generate context sensitive popup menu for link.
*
*/
struct nsgtk_link_menu *nsgtk_link_menu_create(GtkAccelGroup *group);
#endif

View File

@ -107,7 +107,10 @@ struct gtk_scaffolding {
struct nsgtk_bar_submenu *menu_bar;
/** right click popup menu hierarchy */
struct nsgtk_popup_submenu *menu_popup;
struct nsgtk_popup_menu *menu_popup;
/** link popup menu */
struct nsgtk_link_menu *link_menu;
GtkToolbar *tool_bar;
struct nsgtk_button_connect *buttons[PLACEHOLDER_BUTTON];
@ -147,7 +150,7 @@ static struct contextual_content current_menu_ctx;
/**
* Helper to hide popup menu entries by grouping
*/
static void popup_menu_hide(struct nsgtk_popup_submenu *menu, bool submenu,
static void popup_menu_hide(struct nsgtk_popup_menu *menu, bool submenu,
bool link, bool nav, bool cnp, bool custom)
{
if (submenu){
@ -190,7 +193,7 @@ static void popup_menu_hide(struct nsgtk_popup_submenu *menu, bool submenu,
/**
* Helper to show popup menu entries by grouping
*/
static void popup_menu_show(struct nsgtk_popup_submenu *menu, bool submenu,
static void popup_menu_show(struct nsgtk_popup_menu *menu, bool submenu,
bool link, bool nav, bool cnp, bool custom)
{
if (submenu){
@ -1023,6 +1026,44 @@ MENUHANDLER(link_opentab)
return TRUE;
}
/**
* Handler for bookmarking a link. attached to the popup menu.
*/
MENUHANDLER(link_bookmark)
{
nsurl *url;
nserror error;
if (current_menu_ctx.link_url == NULL)
return FALSE;
error = nsurl_create(current_menu_ctx.link_url, &url);
if (error == NSERROR_OK) {
hotlist_add_url(url);
nsurl_unref(url);
}
if (error != NSERROR_OK) {
warn_user(messages_get_errorcode(error), 0);
}
return TRUE;
}
/**
* Handler for copying a link. attached to the popup menu.
*/
MENUHANDLER(link_copy)
{
GtkClipboard *clipboard;
if (current_menu_ctx.link_url == NULL)
return FALSE;
clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
gtk_clipboard_set_text(clipboard, current_menu_ctx.link_url, -1);
return TRUE;
}
MULTIHANDLER(cut)
{
@ -1755,23 +1796,21 @@ static void nsgtk_attach_menu_handlers(struct gtk_scaffolding *g)
* Create and connect handlers to popup menu.
*
* \param g scaffoliding to attach popup menu to.
* \return true on success or false on error.
* \return menu structure on sucess or NULL on error.
*/
static bool nsgtk_new_scaffolding_popup(struct gtk_scaffolding *g, GtkAccelGroup *group)
static struct nsgtk_popup_menu *
nsgtk_new_scaffolding_popup(struct gtk_scaffolding *g, GtkAccelGroup *group)
{
struct nsgtk_popup_submenu *nmenu;
struct nsgtk_popup_menu *nmenu;
nmenu = nsgtk_menu_popup_create(group);
nmenu = nsgtk_popup_menu_create(group);
if (nmenu == NULL)
return false;
if (nmenu == NULL) {
return NULL;
}
/** Connect a GTK signal handler to a widget */
#define SIG_CONNECT(obj, sig, callback, ptr) \
g_signal_connect(G_OBJECT(obj), (sig), G_CALLBACK(callback), (ptr))
SIG_CONNECT(nmenu->popup_menu, "hide",
nsgtk_window_popup_menu_hidden, g);
g_signal_connect(nmenu->popup_menu, "hide",
G_CALLBACK(nsgtk_window_popup_menu_hidden), g);
g_signal_connect(nmenu->savelink_menuitem, "activate",
G_CALLBACK(nsgtk_on_savelink_activate_menu), g);
@ -1797,9 +1836,42 @@ static bool nsgtk_new_scaffolding_popup(struct gtk_scaffolding *g, GtkAccelGroup
/* set initial popup menu visibility */
popup_menu_hide(nmenu, true, false, false, false, true);
g->menu_popup = nmenu;
return nmenu;
}
return true;
/**
* Create and connect handlers to link popup menu.
*
* \param g scaffoliding to attach popup menu to.
* \return true on success or false on error.
*/
static struct nsgtk_link_menu *
nsgtk_new_scaffolding_link_popup(struct gtk_scaffolding *g, GtkAccelGroup *group)
{
struct nsgtk_link_menu *nmenu;
nmenu = nsgtk_link_menu_create(group);
if (nmenu == NULL) {
return NULL;
}
g_signal_connect(nmenu->save_menuitem, "activate",
G_CALLBACK(nsgtk_on_savelink_activate_menu), g);
g_signal_connect(nmenu->opentab_menuitem, "activate",
G_CALLBACK(nsgtk_on_link_opentab_activate_menu), g);
g_signal_connect(nmenu->openwin_menuitem, "activate",
G_CALLBACK(nsgtk_on_link_openwin_activate_menu), g);
g_signal_connect(nmenu->bookmark_menuitem, "activate",
G_CALLBACK(nsgtk_on_link_bookmark_activate_menu), g);
g_signal_connect(nmenu->copy_menuitem, "activate",
G_CALLBACK(nsgtk_on_link_copy_activate_menu), g);
return nmenu;
}
nsgtk_scaffolding *nsgtk_new_scaffolding(struct gui_window *toplevel)
@ -2061,7 +2133,9 @@ nsgtk_scaffolding *nsgtk_new_scaffolding(struct gui_window *toplevel)
nsgtk_window_tool_bar_clicked, g);
/* create popup menu */
nsgtk_new_scaffolding_popup(g, group);
g->menu_popup = nsgtk_new_scaffolding_popup(g, group);
g->link_menu = nsgtk_new_scaffolding_link_popup(g, group);
/* set up the menu signal handlers */
nsgtk_scaffolding_toolbar_init(g);
@ -2509,53 +2583,53 @@ void nsgtk_scaffolding_initial_sensitivity(struct gtk_scaffolding *g)
gtk_widget_set_sensitive(GTK_WIDGET(g->menu_bar->view_submenu->images_menuitem), FALSE);
}
/**
* Checks if a location is over a link.
*
* Side effect of this function is to set the global current_menu_ctx
*/
static bool is_menu_over_link(struct gtk_scaffolding *g, gdouble x, gdouble y)
/* exported interface documented in gtk/scaffolding.h */
void nsgtk_scaffolding_context_menu(struct gtk_scaffolding *g,
gdouble x,
gdouble y)
{
GtkMenu *gtkmenu;
/* update the global current_menu_ctx */
browser_window_get_contextual_content(
nsgtk_get_browser_window(g->top_level),
x, y, &current_menu_ctx);
if (current_menu_ctx.link_url == NULL)
return false;
return true;
}
if (current_menu_ctx.link_url != NULL) {
/* menu is opening over a link */
gtkmenu = g->link_menu->link_menu;
void nsgtk_scaffolding_popup_menu(struct gtk_scaffolding *g, gdouble x, gdouble y)
{
if (is_menu_over_link(g, x, y)) {
popup_menu_show(g->menu_popup, false, true, false, false, false);
} else {
gtkmenu = g->menu_popup->popup_menu;
popup_menu_hide(g->menu_popup, false, true, false, false, false);
nsgtk_scaffolding_update_edit_actions_sensitivity(g);
if (!(g->buttons[COPY_BUTTON]->sensitivity))
gtk_widget_hide(GTK_WIDGET(g->menu_popup->copy_menuitem));
else
gtk_widget_show(GTK_WIDGET(g->menu_popup->copy_menuitem));
if (!(g->buttons[CUT_BUTTON]->sensitivity))
gtk_widget_hide(GTK_WIDGET(g->menu_popup->cut_menuitem));
else
gtk_widget_show(GTK_WIDGET(g->menu_popup->cut_menuitem));
if (!(g->buttons[PASTE_BUTTON]->sensitivity))
gtk_widget_hide(GTK_WIDGET(g->menu_popup->paste_menuitem));
else
gtk_widget_show(GTK_WIDGET(g->menu_popup->paste_menuitem));
/* hide customize */
popup_menu_hide(g->menu_popup, false, false, false, false, true);
}
nsgtk_scaffolding_update_edit_actions_sensitivity(g);
if (!(g->buttons[COPY_BUTTON]->sensitivity))
gtk_widget_hide(GTK_WIDGET(g->menu_popup->copy_menuitem));
else
gtk_widget_show(GTK_WIDGET(g->menu_popup->copy_menuitem));
if (!(g->buttons[CUT_BUTTON]->sensitivity))
gtk_widget_hide(GTK_WIDGET(g->menu_popup->cut_menuitem));
else
gtk_widget_show(GTK_WIDGET(g->menu_popup->cut_menuitem));
if (!(g->buttons[PASTE_BUTTON]->sensitivity))
gtk_widget_hide(GTK_WIDGET(g->menu_popup->paste_menuitem));
else
gtk_widget_show(GTK_WIDGET(g->menu_popup->paste_menuitem));
/* hide customize */
popup_menu_hide(g->menu_popup, false, false, false, false, true);
gtk_menu_popup(g->menu_popup->popup_menu, NULL, NULL, NULL, NULL, 0,
gtk_get_current_event_time());
gtk_menu_popup(gtkmenu, NULL, NULL, NULL, NULL, 0,
gtk_get_current_event_time());
}
/**

View File

@ -166,8 +166,15 @@ void nsgtk_scaffolding_destroy(nsgtk_scaffolding *g);
void nsgtk_scaffolding_set_sensitivity(struct gtk_scaffolding *g);
void nsgtk_scaffolding_initial_sensitivity(struct gtk_scaffolding *g);
void nsgtk_scaffolding_popup_menu(struct gtk_scaffolding *g, gdouble x,
gdouble y);
/**
* Open a context sensitive menu.
*
* \param g the scaffolding containing the browser window.
* \param x The x co-ordinate.
* \param y The y co-ordinate.
*/
void nsgtk_scaffolding_context_menu(struct gtk_scaffolding *g, gdouble x, gdouble y);
void nsgtk_scaffolding_toolbar_size_allocate(GtkWidget *widget,
GtkAllocation *alloc, gpointer data);
void nsgtk_scaffolding_set_icon(struct gui_window *gw);

View File

@ -338,8 +338,9 @@ static gboolean nsgtk_window_button_press_event(GtkWidget *widget,
case 3: /* Right button, usually. Action button, context menu. */
browser_window_remove_caret(g->bw, true);
nsgtk_scaffolding_popup_menu(g->scaffold, g->mouse.pressed_x,
g->mouse.pressed_y);
nsgtk_scaffolding_context_menu(g->scaffold,
g->mouse.pressed_x,
g->mouse.pressed_y);
return TRUE;
default:

View File

@ -2222,26 +2222,38 @@ fr.gtk.gtkAbout:_A propos...
it.gtk.gtkAbout:_Informazioni
nl.gtk.gtkAbout:_About
en.gtk.gtkCustomize:Customise…
de.gtk.gtkCustomize:Customise..
fr.gtk.gtkCustomize:Customise…
it.gtk.gtkCustomize:Personalizza...
nl.gtk.gtkCustomize:Customise…
en.gtk.gtkOpentab:Open Link in _New Tab
de.gtk.gtkOpentab:Link in _neuem Tab öffnen
fr.gtk.gtkOpentab:Open Link in _New Tab
it.gtk.gtkOpentab:Apri in una _nuova scheda
nl.gtk.gtkOpentab:Open Link in _New Tab
en.gtk.gtkOpenwin:Open Link in New Window
de.gtk.gtkOpenwin:Link in neuem Fenster öffnen
fr.gtk.gtkOpenwin:Open Link in New Window
it.gtk.gtkOpenwin:Apri in una nuova finestra
nl.gtk.gtkOpenwin:Open Link in New Window
en.gtk.gtkSavelink:Save Link
de.gtk.gtkSavelink:Link speichern..
fr.gtk.gtkSavelink:Save Link
it.gtk.gtkSavelink:Salva Link
nl.gtk.gtkSavelink:Save Link
en.gtk.gtkOpentab:Open Link in New _Tab
de.gtk.gtkOpentab:Link in neuem _Tab öffnen
fr.gtk.gtkOpentab:Ouvrir dans un nouvel _Onglet
it.gtk.gtkOpentab:Apri in una nuova _scheda
nl.gtk.gtkOpentab:Open Link in New _Tab
en.gtk.gtkOpenwin:Open Link in New _Window
de.gtk.gtkOpenwin:Link in neuem _Fenster öffnen
fr.gtk.gtkOpenwin:Ouvrir dans une nouvelle _Fenêtre
it.gtk.gtkOpenwin:Apri in una nuova _finestra
nl.gtk.gtkOpenwin:Open Link in New _Window
en.gtk.gtkSavelink:Save Lin_k
de.gtk.gtkSavelink:Lin_k speichern..
fr.gtk.gtkSavelink:Save Lin_k
it.gtk.gtkSavelink:Salva Lin_k
nl.gtk.gtkSavelink:Save Lin_k
en.gtk.gtkBookmarklink:Bookmark _Link
de.gtk.gtkBookmarklink:Bookmark _Link
fr.gtk.gtkBookmarklink:Bookmark _Link
it.gtk.gtkBookmarklink:Bookmark _Link
nl.gtk.gtkBookmarklink:Bookmark _Link
en.gtk.gtkCopylink:Copy link loc_ation
de.gtk.gtkCopylink:Copy link loc_ation
fr.gtk.gtkCopylink:Copy link loc_ation
it.gtk.gtkCopylink:Copy link loc_ation
nl.gtk.gtkCopylink:Copy link loc_ation
en.gtk.gtkToolBarTitle:Toolbar custom button store
de.gtk.gtkToolBarTitle:Benutzerdefinierter Ort für Toolbar-Icons

View File

@ -49,7 +49,11 @@ struct redraw_context {
};
/* Content located at a specific spatial location */
/**
* Content located at a specific spatial location.
*
* \todo This structure should contain a nsurl not a string.
*/
struct contextual_content {
const char *link_url;
struct hlcache_handle *object;