/*
* Main menu and button
*
* Copyright (C) 2017 Patrick McDermott
*
* This file is part of Marquee.
*
* Marquee 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, either version 3 of the License, or
* (at your option) any later version.
*
* Marquee 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 Marquee. If not, see .
*/
#include "main-menu.h"
#include
#include
#include
#include "../../notebook.h"
#include "../../tab-page.h"
#include "../../web-view.h"
#include "../find-toolbar.h"
struct _MqMainMenu {
GtkToolButton parent_instance;
MqTabPage *tab_page;
MqFindToolbar *find_toolbar;
MqWebView *web_view;
GtkWidget *popover;
};
enum {
PROP_TAB_PAGE = 1,
PROP_FIND_TOOLBAR,
PROP_WEB_VIEW,
N_PROPERTIES
};
static GParamSpec *obj_properties[N_PROPERTIES] = {NULL,};
struct _MqMainMenuClass {
GtkToolButtonClass parent_class;
};
G_DEFINE_TYPE(MqMainMenu, mq_main_menu, GTK_TYPE_TOOL_BUTTON)
static void
can_edit_cb(WebKitWebView *web_view, GAsyncResult *result, GtkWidget *button)
{
gtk_widget_set_sensitive(button,
webkit_web_view_can_execute_editing_command_finish(web_view,
result, NULL));
}
static void
cut_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqMainMenu *main_menu)
{
webkit_web_view_execute_editing_command(
WEBKIT_WEB_VIEW(main_menu->web_view),
WEBKIT_EDITING_COMMAND_CUT);
}
static void
copy_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqMainMenu *main_menu)
{
webkit_web_view_execute_editing_command(
WEBKIT_WEB_VIEW(main_menu->web_view),
WEBKIT_EDITING_COMMAND_COPY);
}
static void
paste_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqMainMenu *main_menu)
{
webkit_web_view_execute_editing_command(
WEBKIT_WEB_VIEW(main_menu->web_view),
WEBKIT_EDITING_COMMAND_PASTE);
}
static void
open_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqMainMenu *main_menu)
{
gtk_widget_hide(main_menu->popover);
mq_web_view_open(main_menu->web_view);
}
static void
save_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqMainMenu *main_menu)
{
gtk_widget_hide(main_menu->popover);
mq_web_view_save(main_menu->web_view);
}
static void
zoom_out_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqMainMenu *main_menu)
{
mq_web_view_zoom_out(main_menu->web_view);
}
static void
zoom_reset_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqMainMenu *main_menu)
{
mq_web_view_zoom_reset(main_menu->web_view);
}
static void
zoom_in_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqMainMenu *main_menu)
{
mq_web_view_zoom_in(main_menu->web_view);
}
static void
new_window_clicked_cb(GtkWidget G_GNUC_UNUSED *button, MqMainMenu *main_menu)
{
mq_application_add_window(
mq_tab_page_get_application(main_menu->tab_page), NULL);
gtk_widget_hide(main_menu->popover);
}
static void
find_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqMainMenu *main_menu)
{
gtk_widget_hide(main_menu->popover);
mq_find_toolbar_reveal(main_menu->find_toolbar);
}
static void
fullscreen_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqMainMenu *main_menu)
{
gtk_widget_hide(main_menu->popover);
mq_window_toggle_fullscreen(mq_tab_page_get_window(main_menu->tab_page));
}
static void
inspector_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqMainMenu *main_menu)
{
gtk_widget_hide(main_menu->popover);
webkit_web_inspector_show(webkit_web_view_get_inspector(
WEBKIT_WEB_VIEW(main_menu->web_view)));
}
static void
preferences_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqMainMenu *main_menu)
{
gtk_widget_hide(main_menu->popover);
mq_notebook_insert_sibling(
MQ_NOTEBOOK(gtk_widget_get_parent(
GTK_WIDGET(main_menu->tab_page))),
"about:preferences",
main_menu->tab_page,
TRUE);
}
static void
about_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqMainMenu *main_menu)
{
gtk_widget_hide(main_menu->popover);
mq_notebook_insert_sibling(
MQ_NOTEBOOK(gtk_widget_get_parent(
GTK_WIDGET(main_menu->tab_page))),
"about:",
main_menu->tab_page,
TRUE);
}
static void
quit_clicked_cb(GtkButton G_GNUC_UNUSED *button, MqMainMenu *main_menu)
{
/* mq_tab_page_quit() just calls mq_window_quit(), which just calls
* mq_application_quit(), which is asynchronous. So close the menu. */
gtk_widget_hide(main_menu->popover);
mq_tab_page_quit(main_menu->tab_page);
}
#define NEW_BUTTON(ID, ICON, TOOLTIP) \
G_STMT_START { \
ID##_button = gtk_button_new_from_icon_name(ICON, \
GTK_ICON_SIZE_BUTTON); \
gtk_widget_set_tooltip_text(ID##_button, TOOLTIP); \
gtk_box_pack_start(GTK_BOX(box), ID##_button, \
TRUE, TRUE, 0); \
g_signal_connect(ID##_button, "clicked", \
G_CALLBACK(ID##_clicked_cb), main_menu); \
} G_STMT_END
static GtkWidget *
create_edit_buttons(MqMainMenu *main_menu)
{
GtkWidget *box;
GtkWidget *cut_button;
GtkWidget *copy_button;
GtkWidget *paste_button;
box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_box_set_homogeneous(GTK_BOX(box), TRUE);
gtk_style_context_add_class(gtk_widget_get_style_context(box),
"linked");
NEW_BUTTON(cut, "edit-cut", "Cut");
gtk_widget_set_sensitive(cut_button, FALSE);
webkit_web_view_can_execute_editing_command(
WEBKIT_WEB_VIEW(main_menu->web_view),
WEBKIT_EDITING_COMMAND_CUT,
NULL, (GAsyncReadyCallback) can_edit_cb, cut_button);
NEW_BUTTON(copy, "edit-copy", "Copy");
gtk_widget_set_sensitive(copy_button, FALSE);
webkit_web_view_can_execute_editing_command(
WEBKIT_WEB_VIEW(main_menu->web_view),
WEBKIT_EDITING_COMMAND_COPY,
NULL, (GAsyncReadyCallback) can_edit_cb, copy_button);
NEW_BUTTON(paste, "edit-paste", "Paste");
gtk_widget_set_sensitive(paste_button, FALSE);
webkit_web_view_can_execute_editing_command(
WEBKIT_WEB_VIEW(main_menu->web_view),
WEBKIT_EDITING_COMMAND_PASTE,
NULL, (GAsyncReadyCallback) can_edit_cb, paste_button);
return box;
}
static GtkWidget *
create_zoom_buttons(MqMainMenu *main_menu)
{
GtkWidget *box;
GtkWidget *zoom_out_button;
GtkWidget *zoom_reset_button;
GtkWidget *zoom_in_button;
box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_box_set_homogeneous(GTK_BOX(box), TRUE);
gtk_style_context_add_class(gtk_widget_get_style_context(box),
"linked");
NEW_BUTTON(zoom_out, "zoom-out", "Zoom out");
NEW_BUTTON(zoom_reset, "zoom-original", "Reset zoom");
NEW_BUTTON(zoom_in, "zoom-in", "Zoom in");
return box;
}
static GtkWidget *
create_application_buttons(MqMainMenu *main_menu)
{
GtkWidget *box;
GtkWidget *preferences_button;
GtkWidget *about_button;
GtkWidget *quit_button;
box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_box_set_homogeneous(GTK_BOX(box), TRUE);
gtk_style_context_add_class(gtk_widget_get_style_context(box),
"linked");
NEW_BUTTON(preferences, "preferences-desktop", "Preferences");
NEW_BUTTON(about, "help-about", "About Marquee");
NEW_BUTTON(quit, "application-exit", "Quit");
return box;
}
#undef NEW_BUTTON
#define BUTTON_ROWS 3
#define BUTTON_COLS 3
#define NEW_BUTTON(Y, X, ICON, TOOLTIP) \
G_STMT_START { \
buttons[Y * BUTTON_COLS + X] = gtk_button_new_from_icon_name(\
ICON, GTK_ICON_SIZE_BUTTON); \
gtk_button_set_relief(GTK_BUTTON(buttons[Y * BUTTON_COLS + X]),\
GTK_RELIEF_NONE); \
gtk_button_set_label(GTK_BUTTON(buttons[Y * BUTTON_COLS + X]), \
TOOLTIP); \
gtk_button_set_image_position(\
GTK_BUTTON(buttons[Y * BUTTON_COLS + X]), GTK_POS_TOP);\
gtk_button_set_always_show_image(\
GTK_BUTTON(buttons[Y * BUTTON_COLS + X]), TRUE); \
gtk_widget_set_tooltip_text(buttons[Y * BUTTON_COLS + X], \
TOOLTIP); \
gtk_grid_attach(GTK_GRID(grid), buttons[Y * BUTTON_COLS + X], \
X, Y, 1, 1); \
} G_STMT_END
#define CLICKED_CB(Y, X, CB) \
g_signal_connect(buttons[Y * BUTTON_COLS + X], "clicked", \
G_CALLBACK(CB), main_menu)
static GtkWidget *
create_main_grid(MqMainMenu *main_menu)
{
GtkWidget *grid;
GtkWidget *buttons[BUTTON_ROWS * BUTTON_COLS];
/* Set up the grid. */
grid = gtk_grid_new();
gtk_grid_set_row_homogeneous(GTK_GRID(grid), TRUE);
gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE);
NEW_BUTTON(0, 0, "window-new", "New window");
NEW_BUTTON(0, 1, "edit-find", "Find");
NEW_BUTTON(0, 2, "view-fullscreen", "Full screen");
NEW_BUTTON(1, 0, "document-open", "Open file");
NEW_BUTTON(1, 1, "document-save-as", "Save page");
NEW_BUTTON(1, 2, "mail-message-new", "E-mail link");
NEW_BUTTON(2, 0, "document-print-preview", "Print preview");
NEW_BUTTON(2, 1, "document-print", "Print");
NEW_BUTTON(2, 2, "preferences-system", "Inspector");
CLICKED_CB(0, 0, new_window_clicked_cb);
CLICKED_CB(0, 1, find_clicked_cb);
CLICKED_CB(0, 2, fullscreen_clicked_cb);
CLICKED_CB(1, 0, open_clicked_cb);
CLICKED_CB(1, 1, save_clicked_cb);
/* TODO: 1, 2: E-mail link */
gtk_widget_set_sensitive(buttons[1 * BUTTON_COLS + 2], FALSE);
/* TODO: 2, 0: Print preview */
gtk_widget_set_sensitive(buttons[2 * BUTTON_COLS + 0], FALSE);
/* TODO: 2, 1: Print */
gtk_widget_set_sensitive(buttons[2 * BUTTON_COLS + 1], FALSE);
CLICKED_CB(2, 2, inspector_clicked_cb);
return grid;
}
#undef BUTTON_ROWS
#undef BUTTON_COLS
#undef NEW_BUTTON
#undef CLICKED_CB
static void
menu_clicked_cb(MqMainMenu *main_menu)
{
GtkWidget *box;
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_box_pack_start(GTK_BOX(box), create_edit_buttons(main_menu),
TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(box), create_zoom_buttons(main_menu),
TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(box), create_main_grid(main_menu),
TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(box), create_application_buttons(main_menu),
TRUE, TRUE, 0);
/* Set up the popover. */
main_menu->popover = gtk_popover_new(GTK_WIDGET(main_menu));
gtk_container_add(GTK_CONTAINER(main_menu->popover), box);
/* NB: gtk_popover_popup() is new in GTK+ 3.22. */
gtk_widget_show_all(main_menu->popover);
}
static void
get_property(GObject *object, guint property_id, GValue *value,
GParamSpec *param_spec)
{
MqMainMenu *main_menu;
main_menu = MQ_MAIN_MENU(object);
switch (property_id) {
case PROP_TAB_PAGE:
g_value_set_object(value, main_menu->tab_page);
break;
case PROP_FIND_TOOLBAR:
g_value_set_object(value, main_menu->find_toolbar);
break;
case PROP_WEB_VIEW:
g_value_set_object(value, main_menu->web_view);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id,
param_spec);
break;
}
}
static void
set_property(GObject *object, guint property_id, const GValue *value,
GParamSpec *param_spec)
{
MqMainMenu *main_menu;
main_menu = MQ_MAIN_MENU(object);
switch (property_id) {
case PROP_TAB_PAGE:
main_menu->tab_page = g_value_get_object(value);
break;
case PROP_FIND_TOOLBAR:
main_menu->find_toolbar = g_value_get_object(value);
break;
case PROP_WEB_VIEW:
main_menu->web_view = g_value_get_object(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id,
param_spec);
break;
}
}
static void
mq_main_menu_class_init(MqMainMenuClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->get_property = get_property;
object_class->set_property = set_property;
obj_properties[PROP_TAB_PAGE] = g_param_spec_object(
"tab-page",
"MqTabPage",
"The ancestral MqTabPage instance",
MQ_TYPE_TAB_PAGE,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB);
obj_properties[PROP_FIND_TOOLBAR] = g_param_spec_object(
"find-toolbar",
"MqFindToolbar",
"The associated MqFindToolbar instance",
MQ_TYPE_FIND_TOOLBAR,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB);
obj_properties[PROP_WEB_VIEW] = g_param_spec_object(
"web-view",
"MqWebView",
"The associated MqWebView instance",
MQ_TYPE_WEB_VIEW,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB);
g_object_class_install_properties(object_class, N_PROPERTIES,
obj_properties);
}
static void
mq_main_menu_init(MqMainMenu *main_menu)
{
gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(main_menu),
gtk_image_new_from_icon_name("open-menu-symbolic",
GTK_ICON_SIZE_SMALL_TOOLBAR));
gtk_tool_button_set_label(GTK_TOOL_BUTTON(main_menu), "Menu");
gtk_widget_set_tooltip_text(GTK_WIDGET(main_menu), "Open menu");
g_signal_connect(main_menu, "clicked",
G_CALLBACK(menu_clicked_cb), NULL);
}
GtkToolItem *
mq_main_menu_new(MqTabPage *tab_page, MqFindToolbar *find_toolbar,
MqWebView *web_view)
{
return g_object_new(MQ_TYPE_MAIN_MENU,
"tab-page", tab_page,
"find-toolbar", find_toolbar,
"web-view", web_view,
NULL);
}