2008-08-24 14:31:38 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Michael Lester <element3260@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 <glade/glade.h>
|
2009-04-21 00:31:13 +04:00
|
|
|
#include <string.h>
|
2008-08-24 14:31:38 +04:00
|
|
|
#include "gtk/gtk_window.h"
|
|
|
|
#include "gtk/gtk_gui.h"
|
|
|
|
#include "desktop/browser.h"
|
|
|
|
#include "content/content.h"
|
2008-09-16 17:32:12 +04:00
|
|
|
#include "desktop/options.h"
|
2009-12-18 02:55:02 +03:00
|
|
|
#include "desktop/search.h"
|
2009-04-21 00:31:13 +04:00
|
|
|
#include "utils/utils.h"
|
2008-09-16 17:32:12 +04:00
|
|
|
#include "gtk/options.h"
|
2009-12-18 02:55:02 +03:00
|
|
|
#include "gtk/gtk_search.h"
|
2008-10-10 03:14:00 +04:00
|
|
|
#include "gtk/gtk_tabs.h"
|
2008-08-24 14:31:38 +04:00
|
|
|
|
|
|
|
#define TAB_WIDTH_N_CHARS 15
|
|
|
|
|
|
|
|
static GtkWidget *nsgtk_tab_label_setup(struct gui_window *window);
|
|
|
|
static void nsgtk_tab_visibility_update(GtkNotebook *notebook, GtkWidget *child,
|
|
|
|
guint page);
|
|
|
|
static void nsgtk_tab_update_size(GtkWidget *hbox, GtkStyle *previous_style,
|
|
|
|
GtkWidget *close_button);
|
|
|
|
|
2010-10-23 17:53:11 +04:00
|
|
|
static void nsgtk_tab_page_changed(GtkNotebook *notebook, gpointer *page,
|
2008-08-24 14:31:38 +04:00
|
|
|
gint page_num);
|
|
|
|
|
2008-09-16 17:32:12 +04:00
|
|
|
void nsgtk_tab_options_changed(GtkWidget *tabs)
|
|
|
|
{
|
|
|
|
nsgtk_tab_visibility_update(GTK_NOTEBOOK(tabs), NULL, 0);
|
|
|
|
}
|
|
|
|
|
2008-08-24 14:31:38 +04:00
|
|
|
void nsgtk_tab_init(GtkWidget *tabs)
|
|
|
|
{
|
|
|
|
g_signal_connect(tabs, "switch-page",
|
2008-09-16 17:32:12 +04:00
|
|
|
G_CALLBACK(nsgtk_tab_page_changed), NULL);
|
2008-08-24 14:31:38 +04:00
|
|
|
|
|
|
|
g_signal_connect(tabs, "page-removed",
|
2008-09-16 17:32:12 +04:00
|
|
|
G_CALLBACK(nsgtk_tab_visibility_update), NULL);
|
2008-08-24 14:31:38 +04:00
|
|
|
g_signal_connect(tabs, "page-added",
|
2008-10-11 17:52:03 +04:00
|
|
|
G_CALLBACK(nsgtk_tab_visibility_update), NULL);
|
2008-09-16 17:32:12 +04:00
|
|
|
nsgtk_tab_options_changed(tabs);
|
2008-08-24 14:31:38 +04:00
|
|
|
}
|
|
|
|
|
2010-04-07 20:52:45 +04:00
|
|
|
void nsgtk_tab_add(struct gui_window *window, GtkWidget *tab_contents, bool background)
|
2008-08-24 14:31:38 +04:00
|
|
|
{
|
2009-12-18 02:55:02 +03:00
|
|
|
GtkWidget *tabs = GTK_WIDGET(nsgtk_scaffolding_notebook(
|
|
|
|
nsgtk_get_scaffold(window)));
|
2008-08-24 14:31:38 +04:00
|
|
|
GtkWidget *tabBox = nsgtk_tab_label_setup(window);
|
2009-04-21 00:31:13 +04:00
|
|
|
gint remember = gtk_notebook_get_current_page(GTK_NOTEBOOK(tabs));
|
2010-04-07 20:52:45 +04:00
|
|
|
gtk_notebook_append_page(GTK_NOTEBOOK(tabs), tab_contents, tabBox);
|
2009-12-18 02:55:02 +03:00
|
|
|
/*causes gtk errors can't set a parent */
|
2009-04-21 00:31:13 +04:00
|
|
|
gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(tabs),
|
2010-04-07 20:52:45 +04:00
|
|
|
tab_contents,
|
2009-12-18 02:55:02 +03:00
|
|
|
true);
|
2010-04-07 20:52:45 +04:00
|
|
|
gtk_widget_show_all(tab_contents);
|
2009-04-21 00:31:13 +04:00
|
|
|
gtk_notebook_set_current_page(GTK_NOTEBOOK(tabs),
|
|
|
|
gtk_notebook_get_n_pages(GTK_NOTEBOOK(tabs)) - 1);
|
|
|
|
if (option_new_blank) {
|
|
|
|
/*char *blankpage = malloc(strlen(res_dir_location) +
|
|
|
|
SLEN("file:///blankpage") + 1);
|
|
|
|
blankpage = g_strconcat("file:///", res_dir_location,
|
|
|
|
"blankpage", NULL); */
|
|
|
|
/* segfaults
|
2009-12-18 02:55:02 +03:00
|
|
|
struct browser_window *bw =
|
|
|
|
gui_window_get_browser_window(window);
|
2009-04-21 00:31:13 +04:00
|
|
|
browser_window_go(bw, blankpage, 0, true); */
|
|
|
|
/* free(blankpage); */
|
|
|
|
}
|
|
|
|
if (background)
|
|
|
|
gtk_notebook_set_current_page(GTK_NOTEBOOK(tabs), remember);
|
2009-12-18 02:55:02 +03:00
|
|
|
gtk_widget_grab_focus(GTK_WIDGET(nsgtk_scaffolding_urlbar(
|
|
|
|
nsgtk_get_scaffold(window))));
|
2008-08-24 14:31:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsgtk_tab_visibility_update(GtkNotebook *notebook, GtkWidget *child,
|
|
|
|
guint page)
|
|
|
|
{
|
|
|
|
gint num_pages = gtk_notebook_get_n_pages(notebook);
|
2008-09-16 17:32:12 +04:00
|
|
|
if (option_show_single_tab == true || num_pages > 1)
|
|
|
|
gtk_notebook_set_show_tabs(notebook, TRUE);
|
|
|
|
else
|
|
|
|
gtk_notebook_set_show_tabs(notebook, FALSE);
|
2008-08-24 14:31:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsgtk_tab_set_title(struct gui_window *g, const char *title)
|
|
|
|
{
|
|
|
|
GtkWidget *label;
|
2009-12-18 02:55:02 +03:00
|
|
|
GtkWidget *tab;
|
|
|
|
tab = nsgtk_window_get_tab(g);
|
|
|
|
gboolean is_top_level = (tab != NULL);
|
2008-08-24 14:31:38 +04:00
|
|
|
|
|
|
|
if (is_top_level) {
|
2009-12-18 02:55:02 +03:00
|
|
|
label = g_object_get_data(G_OBJECT(tab), "label");
|
2008-08-24 14:31:38 +04:00
|
|
|
gtk_label_set_text(GTK_LABEL(label), title);
|
2009-12-18 02:55:02 +03:00
|
|
|
gtk_widget_set_tooltip_text(tab, title);
|
2008-10-11 17:52:03 +04:00
|
|
|
}
|
2008-08-24 14:31:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *nsgtk_tab_label_setup(struct gui_window *window)
|
|
|
|
{
|
|
|
|
GtkWidget *hbox, *label, *button, *close;
|
2008-10-11 17:52:03 +04:00
|
|
|
GtkRcStyle *rcstyle;
|
2008-08-24 14:31:38 +04:00
|
|
|
|
|
|
|
hbox = gtk_hbox_new(FALSE, 2);
|
2008-10-11 17:52:03 +04:00
|
|
|
|
2009-04-21 00:31:13 +04:00
|
|
|
if (option_new_blank == true)
|
|
|
|
label = gtk_label_new("New Tab");
|
|
|
|
else
|
|
|
|
label = gtk_label_new("Loading...");
|
2008-10-11 19:12:42 +04:00
|
|
|
gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END);
|
|
|
|
gtk_label_set_single_line_mode(GTK_LABEL(label), TRUE);
|
|
|
|
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
|
|
|
|
gtk_misc_set_padding(GTK_MISC(label), 0, 0);
|
|
|
|
gtk_widget_show(label);
|
2008-10-11 17:52:03 +04:00
|
|
|
|
2008-08-24 14:31:38 +04:00
|
|
|
button = gtk_button_new();
|
2008-10-11 19:12:42 +04:00
|
|
|
|
2008-10-11 17:52:03 +04:00
|
|
|
close = gtk_image_new_from_stock("gtk-close", GTK_ICON_SIZE_MENU);
|
|
|
|
gtk_container_add(GTK_CONTAINER(button), close);
|
|
|
|
gtk_button_set_focus_on_click(GTK_BUTTON(button), FALSE);
|
|
|
|
gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
|
|
|
|
gtk_widget_set_tooltip_text(button, "Close this tab.");
|
|
|
|
|
|
|
|
rcstyle = gtk_rc_style_new();
|
|
|
|
rcstyle->xthickness = rcstyle->ythickness = 0;
|
|
|
|
gtk_widget_modify_style(button, rcstyle);
|
|
|
|
g_object_unref(rcstyle);
|
2008-08-24 14:31:38 +04:00
|
|
|
|
|
|
|
g_signal_connect_swapped(button, "clicked",
|
|
|
|
G_CALLBACK(nsgtk_window_destroy_browser), window);
|
|
|
|
g_signal_connect(hbox, "style-set",
|
|
|
|
G_CALLBACK(nsgtk_tab_update_size), button);
|
|
|
|
|
|
|
|
gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
|
|
|
|
gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
|
|
|
|
|
2008-10-11 19:12:42 +04:00
|
|
|
g_object_set_data(G_OBJECT(hbox), "label", label);
|
|
|
|
g_object_set_data(G_OBJECT(hbox), "close-button", button);
|
2008-08-24 14:31:38 +04:00
|
|
|
|
2009-12-18 02:55:02 +03:00
|
|
|
nsgtk_window_set_tab(window, hbox);
|
2008-08-24 14:31:38 +04:00
|
|
|
|
|
|
|
gtk_widget_show_all(hbox);
|
|
|
|
return hbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsgtk_tab_update_size(GtkWidget *hbox, GtkStyle *previous_style,
|
|
|
|
GtkWidget *close_button)
|
|
|
|
{
|
|
|
|
PangoFontMetrics *metrics;
|
|
|
|
PangoContext *context;
|
|
|
|
int char_width, h, w;
|
|
|
|
|
2008-10-11 19:12:42 +04:00
|
|
|
context = gtk_widget_get_pango_context(hbox);
|
|
|
|
metrics = pango_context_get_metrics(context, hbox->style->font_desc,
|
|
|
|
pango_context_get_language(context));
|
2008-08-24 14:31:38 +04:00
|
|
|
|
2008-10-11 19:12:42 +04:00
|
|
|
char_width = pango_font_metrics_get_approximate_digit_width(metrics);
|
|
|
|
pango_font_metrics_unref(metrics);
|
2008-08-24 14:31:38 +04:00
|
|
|
|
2008-10-11 19:12:42 +04:00
|
|
|
gtk_icon_size_lookup_for_settings(gtk_widget_get_settings (hbox),
|
|
|
|
GTK_ICON_SIZE_MENU, &w, &h);
|
2008-08-24 14:31:38 +04:00
|
|
|
|
|
|
|
gtk_widget_set_size_request(hbox,
|
|
|
|
TAB_WIDTH_N_CHARS * PANGO_PIXELS(char_width) + 2 * w,
|
|
|
|
-1);
|
|
|
|
|
|
|
|
gtk_widget_set_size_request(close_button, w + 4, h + 4);
|
|
|
|
}
|
|
|
|
|
2010-10-23 17:53:11 +04:00
|
|
|
void nsgtk_tab_page_changed(GtkNotebook *notebook, gpointer *page,
|
2008-08-24 14:31:38 +04:00
|
|
|
gint page_num)
|
|
|
|
{
|
|
|
|
GtkWidget *window = gtk_notebook_get_nth_page(notebook, page_num);
|
|
|
|
struct gui_window *gw = g_object_get_data(G_OBJECT(window),
|
|
|
|
"gui_window");
|
2009-12-18 23:15:50 +03:00
|
|
|
if (gw == NULL)
|
|
|
|
return;
|
2009-12-18 02:55:02 +03:00
|
|
|
struct browser_window *bw = gui_window_get_browser_window(gw);
|
2009-12-18 23:15:50 +03:00
|
|
|
if (bw == NULL)
|
|
|
|
return;
|
|
|
|
if (bw->search_context != NULL)
|
2009-12-18 02:55:02 +03:00
|
|
|
search_destroy_context(bw->search_context);
|
|
|
|
nsgtk_search_set_forward_state(true, bw);
|
|
|
|
nsgtk_search_set_back_state(true, bw);
|
2009-12-18 23:15:50 +03:00
|
|
|
nsgtk_scaffolding_set_top_level(gw);
|
2008-08-24 14:31:38 +04:00
|
|
|
}
|
2008-10-18 18:37:29 +04:00
|
|
|
|
|
|
|
void nsgtk_tab_close_current(GtkNotebook *notebook)
|
|
|
|
{
|
|
|
|
gint curr_page = gtk_notebook_get_current_page(notebook);
|
|
|
|
GtkWidget *window = gtk_notebook_get_nth_page(notebook, curr_page);
|
|
|
|
struct gui_window *gw = g_object_get_data(G_OBJECT(window),
|
|
|
|
"gui_window");
|
2008-11-29 03:24:09 +03:00
|
|
|
|
2008-10-18 18:37:29 +04:00
|
|
|
if (gtk_notebook_get_n_pages(notebook) < 2)
|
|
|
|
return; /* wicked things happen if we close the last tab */
|
2008-11-29 03:24:09 +03:00
|
|
|
|
2008-10-18 18:37:29 +04:00
|
|
|
nsgtk_window_destroy_browser(gw);
|
2009-04-21 00:31:13 +04:00
|
|
|
/* deletes 2 notebook tabs at a time!
|
|
|
|
gtk_notebook_remove_page(notebook, curr_page); */
|
2008-10-18 18:37:29 +04:00
|
|
|
}
|