Looking into the printing, I discovered that it looks as though there is
a bug in the gtk printing logic for text/plain files, causing a segfault
so I've added a temporary patch to gtk_scaffolding.c; try for instance
pointing your browser at a local file:/// then printing it; aside from
that, I would say the source view window is ready
Best
Mark
http://www.halloit.com
Key ID 046B65CF
Index: gtk_scaffolding.c
===================================================================
--- gtk_scaffolding.c (revision 7048)
+++ gtk_scaffolding.c (working copy)
@@ -22,7 +22,6 @@
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
-#include <glade/glade.h>
#include <libxml/debugXML.h>
#include "content/content.h"
#include "desktop/browser.h"
@@ -41,6 +40,7 @@
#include "gtk/gtk_completion.h"
#include "gtk/dialogs/gtk_options.h"
#include "gtk/dialogs/gtk_about.h"
+#include "gtk/dialogs/gtk_source.h"
#include "gtk/gtk_download.h"
#include "gtk/gtk_gui.h"
#include "gtk/gtk_history.h"
@@ -65,42 +65,7 @@
struct gtk_history_window;
-struct gtk_scaffolding {
- GtkWindow *window;
- GtkNotebook *notebook;
- GtkEntry *url_bar;
- GtkEntryCompletion *url_bar_completion;
- GtkStatusbar *status_bar;
- GtkMenuItem *edit_menu;
- GtkMenuItem *tabs_menu;
- GtkToolbar *tool_bar;
- GtkToolButton *back_button;
- GtkToolButton *forward_button;
- GtkToolButton *stop_button;
- GtkToolButton *reload_button;
- GtkMenuBar *menu_bar;
- GtkMenuItem *back_menu;
- GtkMenuItem *forward_menu;
- GtkMenuItem *stop_menu;
- GtkMenuItem *reload_menu;
- GtkImage *throbber;
- GtkPaned *status_pane;
- GladeXML *xml;
-
- GladeXML *popup_xml;
- GtkMenu *popup_menu;
-
- struct gtk_history_window *history_window;
- GtkDialog *preferences_dialog;
-
- int throb_frame;
- struct gui_window *top_level;
- int being_destroyed;
-
- bool fullscreen;
-};
-
struct gtk_history_window {
struct gtk_scaffolding *g;
GtkWindow *window;
@@ -187,6 +152,7 @@
MENUPROTO(normal_size);
MENUPROTO(zoom_out);
MENUPROTO(full_screen);
+MENUPROTO(view_source);
MENUPROTO(menu_bar);
MENUPROTO(tool_bar);
MENUPROTO(status_bar);
@@ -244,6 +210,7 @@
MENUEVENT(normal_size),
MENUEVENT(zoom_out),
MENUEVENT(full_screen),
+ MENUEVENT(view_source),
MENUEVENT(menu_bar),
MENUEVENT(tool_bar),
MENUEVENT(status_bar),
@@ -305,7 +272,7 @@
LOG(("Being Destroyed = %d", g->being_destroyed));
gtk_widget_destroy(GTK_WIDGET(g->history_window->window));
gtk_widget_destroy(GTK_WIDGET(g->window));
-
+
if (--open_windows == 0)
netsurf_quit = true;
@@ -659,7 +626,7 @@
}
content_to_print = bw->current_content;
-
+
page_setup = gtk_print_run_page_setup_dialog(gw->window, NULL, NULL);
if (page_setup == NULL) {
warn_user(messages_get("NoMemory"), 0);
@@ -676,8 +643,8 @@
G_CALLBACK(gtk_print_signal_draw_page), NULL);
g_signal_connect(print_op, "end_print",
G_CALLBACK(gtk_print_signal_end_print), settings);
-
- res = gtk_print_operation_run(print_op,
+ if (bw->current_content->type != CONTENT_TEXTPLAIN)
+ res = gtk_print_operation_run(print_op,
GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
gw->window,
NULL);
@@ -691,7 +658,6 @@
gtk_print_settings_to_file(gtk_print_settings,
print_options_file_location, NULL);
}
-
/* Our print_settings object is destroyed by the end print handler */
g_object_unref(page_setup);
g_object_unref(print_op);
@@ -852,6 +818,14 @@
return TRUE;
}
+MENUHANDLER(view_source)
+{
+ struct gtk_scaffolding *gw = (struct gtk_scaffolding *)g;
+ nsgtk_source_dialog_init(gw->window,
+ nsgtk_get_browser_for_gui(gw->top_level));
+ return TRUE;
+}
+
MENUHANDLER(menu_bar)
{
struct gtk_scaffolding *gw = (struct gtk_scaffolding *)g;
@@ -1167,7 +1141,7 @@
LOG(("Constructing a scaffold of %p for gui_window %p", g, toplevel));
g->top_level = toplevel;
-
+
open_windows++;
/* load the window template from the glade xml file, and extract
@@ -1209,6 +1183,53 @@
} else {
gtk_window_set_default_size(g->window, 600, 600);
}
+
+ if (option_button_type == 0) {
+ GtkSettings *settings = gtk_settings_get_default();
+ GtkIconSize tooliconsize;
+ GtkToolbarStyle toolbarstyle;
+ g_object_get(settings, "gtk-toolbar-icon-size", &tooliconsize,
+ "gtk-toolbar-style", &toolbarstyle, NULL);
+ switch (toolbarstyle) {
+ case GTK_TOOLBAR_ICONS :
+ option_button_type = (tooliconsize ==
+ GTK_ICON_SIZE_SMALL_TOOLBAR) ? 1 : 2;
+ break;
+ case GTK_TOOLBAR_TEXT :
+ option_button_type = 4;
+ break;
+ case GTK_TOOLBAR_BOTH :
+ case GTK_TOOLBAR_BOTH_HORIZ :
+ default :
+ option_button_type = 3;
+ break;
+ }
+ }
+ switch(option_button_type) {
+ case 1:
+ gtk_toolbar_set_style(GTK_TOOLBAR(g->tool_bar),
+ GTK_TOOLBAR_ICONS);
+ gtk_toolbar_set_icon_size(GTK_TOOLBAR(g->tool_bar),
+ GTK_ICON_SIZE_SMALL_TOOLBAR);
+ break;
+ case 2:
+ gtk_toolbar_set_style(GTK_TOOLBAR(g->tool_bar),
+ GTK_TOOLBAR_ICONS);
+ gtk_toolbar_set_icon_size(GTK_TOOLBAR(g->tool_bar),
+ GTK_ICON_SIZE_LARGE_TOOLBAR);
+ break;
+ case 3:
+ gtk_toolbar_set_style(GTK_TOOLBAR(g->tool_bar),
+ GTK_TOOLBAR_BOTH);
+ gtk_toolbar_set_icon_size(GTK_TOOLBAR(g->tool_bar),
+ GTK_ICON_SIZE_LARGE_TOOLBAR);
+ break;
+ case 4:
+ gtk_toolbar_set_style(GTK_TOOLBAR(g->tool_bar),
+ GTK_TOOLBAR_TEXT);
+ default:
+ break;
+ }
nsgtk_tab_init(GTK_WIDGET(g->notebook));
@@ -1572,4 +1593,3 @@
widget = glade_xml_get_widget(xml, "separator");
gtk_widget_show(widget);
}
-
#include <gtk/gtk.h>
#include "desktop/browser.h"
struct nsgtk_source_window {
gchar *url;
gchar *data;
GtkWindow *sourcewindow;
GtkTextView *gv;
struct browser_window *bw;
struct nsgtk_source_window *next;
struct nsgtk_source_window *prev;
};
void nsgtk_source_dialog_init(GtkWindow * parent, struct browser_window * bw);
void nsgtk_source_file_save(GtkWindow * parent, const char * filename, const char * data);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include "gtk/dialogs/gtk_source.h"
#include "gtk/dialogs/gtk_about.h"
#include "gtk/gtk_window.h"
#include "gtk/gtk_gui.h"
#include "gtk/gtk_print.h"
#include "gtk/gtk_selection.h"
#include "desktop/netsurf.h"
#include "desktop/print.h"
#include "utils/messages.h"
#include "utils/utils.h"
#include "utils/utf8.h"
#include "render/font.h"
#include "content/content.h"
#include "content/content_type.h"
#include "render/textplain.h"
struct menu_events {
const char *widget;
GCallback handler;
};
static GladeXML * glade_File;
static gchar * glade_Location;
static struct nsgtk_source_window *nsgtk_source_list = 0;
static char source_zoomlevel = 10;
void nsgtk_attach_source_menu_handlers(GladeXML *xml, gpointer g);
gboolean nsgtk_source_delete_event(GtkWindow * window, gpointer g);
gboolean nsgtk_source_destroy_event(GtkWindow * window, gpointer g);
void nsgtk_source_update_zoomlevel(gpointer g);
#define MENUEVENT(x) { #x, G_CALLBACK(nsgtk_on_##x##_activate) }
#define MENUPROTO(x) static gboolean nsgtk_on_##x##_activate( \
GtkMenuItem *widget, gpointer g)
MENUPROTO(source_save_as);
MENUPROTO(source_print);
MENUPROTO(source_close);
MENUPROTO(source_select_all);
MENUPROTO(source_cut);
MENUPROTO(source_copy);
MENUPROTO(source_paste);
MENUPROTO(source_delete);
MENUPROTO(source_zoom_in);
MENUPROTO(source_zoom_out);
MENUPROTO(source_zoom_normal);
MENUPROTO(source_about);
struct menu_events source_menu_events[] = {
MENUEVENT(source_save_as),
MENUEVENT(source_print),
MENUEVENT(source_close),
MENUEVENT(source_select_all),
MENUEVENT(source_cut),
MENUEVENT(source_copy),
MENUEVENT(source_paste),
MENUEVENT(source_delete),
MENUEVENT(source_zoom_in),
MENUEVENT(source_zoom_out),
MENUEVENT(source_zoom_normal),
MENUEVENT(source_about),
{NULL, NULL}
};
void nsgtk_source_dialog_init(GtkWindow * parent, struct browser_window * bw)
{
if (bw->current_content->type == CONTENT_HTML) {
glade_Location = g_strconcat(res_dir_location, "source.glade", NULL);
glade_File = glade_xml_new(glade_Location, NULL, NULL);
if (glade_File) {
GtkWindow * wndSource;
wndSource = malloc(sizeof(GtkWindow));
wndSource = GTK_WINDOW(glade_xml_get_widget(glade_File,
"wndSource"));
GtkWidget *cutbutton = glade_xml_get_widget(glade_File,
"source_cut");
GtkWidget *pastebutton = glade_xml_get_widget(glade_File,
"source_paste");
GtkWidget *deletebutton = glade_xml_get_widget(glade_File,
"source_delete");
GtkWidget *printbutton = glade_xml_get_widget(glade_File,
"source_print");
gtk_widget_set_sensitive(cutbutton, FALSE);
gtk_widget_set_sensitive(pastebutton, FALSE);
gtk_widget_set_sensitive(deletebutton, FALSE);
/* for now */
gtk_widget_set_sensitive(printbutton, FALSE);
struct nsgtk_source_window *thiswindow = (struct
nsgtk_source_window *)malloc(sizeof(struct
nsgtk_source_window));
int len = strlen(bw->current_content->url);
thiswindow->url = (char *)malloc(len + 1);
strcpy(thiswindow->url,bw->current_content->url);
*(thiswindow->url + len) = '\0';
char * ndata = 0;
char ** data = &ndata;
utf8_from_enc(bw->current_content->source_data,
bw->current_content->data.html.encoding,
bw->current_content->source_size, data);
thiswindow->data = *data;
thiswindow->sourcewindow = wndSource;
thiswindow->bw = bw;
if (nsgtk_source_list) {
thiswindow->next = nsgtk_source_list;
thiswindow->prev = NULL;
nsgtk_source_list->prev = thiswindow;
} else {
thiswindow->next = NULL;
thiswindow->prev = NULL;
}
nsgtk_source_list = thiswindow;
char *title;
title = malloc(strlen(bw->current_content->url) + SLEN("Source of ") + 1);
sprintf(title, "Source of %s", bw->current_content->url);
nsgtk_attach_source_menu_handlers(glade_File, thiswindow);
gtk_window_set_title(wndSource, title);
free(title);
g_signal_connect(G_OBJECT(wndSource), "destroy",
G_CALLBACK(nsgtk_source_destroy_event),
thiswindow);
g_signal_connect(G_OBJECT(wndSource), "delete-event",
G_CALLBACK(nsgtk_source_delete_event),
thiswindow);
GtkTextView * sourceview = GTK_TEXT_VIEW(glade_xml_get_widget(
glade_File, "source_view"));
thiswindow->gv = sourceview;
GtkTextBuffer * tb = gtk_text_view_get_buffer(sourceview);
gtk_text_buffer_set_text(tb, thiswindow->data, -1);
gtk_widget_show(GTK_WIDGET(wndSource));
} else {
printf("error loading glade tree\n");
}
}
}
void nsgtk_attach_source_menu_handlers(GladeXML *xml, gpointer g)
{
struct menu_events *event = source_menu_events;
while (event->widget != NULL)
{
GtkWidget *w = glade_xml_get_widget(xml, event->widget);
g_signal_connect(G_OBJECT(w), "activate", event->handler, g);
event++;
}
}
gboolean nsgtk_on_source_save_as_activate( GtkMenuItem *widget, gpointer g)
{
struct nsgtk_source_window * nsg = (struct nsgtk_source_window *)g;
GtkWidget * fc = gtk_file_chooser_dialog_new("save source",
nsg->sourcewindow, GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL,
GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
char *filename, *temp;
temp = strrchr(nsg->url, '/');
int len = strlen(nsg->url);
filename = malloc(len + 1);
strcpy(filename, nsg->url);
filename[len] = '\0';
if (temp == nsg->url + len - 1) {
filename[len-1] = '\0';
}
temp = strrchr(filename, '/') + 1;
free(filename);
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(fc), temp);
if (gtk_dialog_run(GTK_DIALOG(fc)) == GTK_RESPONSE_ACCEPT) {
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fc));
nsgtk_source_file_save(nsg->sourcewindow, filename, nsg->data);
g_free(filename);
}
gtk_widget_destroy(fc);
return TRUE;
}
void nsgtk_source_file_save(GtkWindow * parent, const char * filename, const char * data)
{
FILE *f;
bool auth = true;
char temp[15];
GtkWidget * notif, *label;
if (!(access(filename, F_OK))) {
auth = false;
GtkWidget * confd = gtk_dialog_new_with_buttons("file exists",
parent,
GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL);
label = gtk_label_new("\n do you want to confirm overwrite? \n");
gtk_container_add(GTK_CONTAINER(GTK_DIALOG(confd)->vbox), label);
gtk_widget_show(label);
if (gtk_dialog_run(GTK_DIALOG(confd)) == GTK_RESPONSE_ACCEPT) {
auth = true;
}
gtk_widget_destroy(confd);
}
if (auth) {
f = fopen(filename, "w+");
fprintf(f, "%s", data);
fclose(f);
label = gtk_label_new(
"\n file saved \n");
strcpy(temp, "file saved");
} else {
label = gtk_label_new(
"\n file not saved \n");
strcpy(temp, "file not saved");
}
notif = gtk_dialog_new_with_buttons(temp,
parent, GTK_DIALOG_MODAL, GTK_STOCK_OK,
GTK_RESPONSE_NONE, NULL);
g_signal_connect_swapped(notif, "response",
G_CALLBACK(gtk_widget_destroy), notif);
gtk_container_add(GTK_CONTAINER(GTK_DIALOG(notif)->vbox), label);
gtk_widget_show_all(notif);
}
gboolean nsgtk_on_source_print_activate( GtkMenuItem *widget, gpointer g)
{
/* TODO correct printing */
struct nsgtk_source_window * nsg = (struct nsgtk_source_window *)g;
GtkPrintOperation *print_op;
GtkPageSetup *page_setup;
GtkPrintSettings *gtk_print_settings;
GtkPrintOperationResult res;
struct print_settings *settings;
print_op = gtk_print_operation_new();
if (print_op == NULL) {
warn_user(messages_get("NoMemory"), 0);
return TRUE;
}
/* use previously saved settings if any */
gtk_print_settings = gtk_print_settings_new_from_file(
print_options_file_location, NULL);
if (gtk_print_settings != NULL) {
gtk_print_operation_set_print_settings(print_op,
gtk_print_settings);
/* We're not interested in the settings any more */
g_object_unref(gtk_print_settings);
}
/* this kind of action could also be adapted to allow opening a source
view in a new tab, with appropriate preference settings */
struct content *tempcontent = content_create(nsg->url);
unsigned int len = (unsigned int)strlen(nsg->data);
const char * params[2] = {"encoding", "utf-8"};
textplain_create(tempcontent, params);
textplain_process_data(tempcontent, nsg->data, len);
content_to_print = tempcontent;
page_setup = gtk_print_run_page_setup_dialog(nsg->sourcewindow, NULL,
NULL);
if (page_setup == NULL) {
warn_user(messages_get("NoMemory"), 0);
g_object_unref(print_op);
return TRUE;
}
gtk_print_operation_set_default_page_setup(print_op, page_setup);
printf("print_make_settings\n");
settings = print_make_settings(PRINT_DEFAULT, NULL, &nsfont);
g_signal_connect(print_op, "begin_print",
G_CALLBACK(gtk_print_signal_begin_print), settings);
g_signal_connect(print_op, "draw_page",
G_CALLBACK(gtk_print_signal_draw_page), NULL);
g_signal_connect(print_op, "end_print",
G_CALLBACK(gtk_print_signal_end_print), settings);
printf("gtk_print\n");
res = gtk_print_operation_run(print_op,
GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
nsg->sourcewindow,
NULL);
/* if the settings were used save them for future use */
if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
printf("gtk_get_print_settings\n");
/* Don't ref the settings, as we don't want to own them */
gtk_print_settings = gtk_print_operation_get_print_settings(
print_op);
printf("gtk_print_settings_to_file\n");
gtk_print_settings_to_file(gtk_print_settings,
print_options_file_location, NULL);
}
printf("g_object_unref\n");
/* Our print_settings object is destroyed by the end print handler */
g_object_unref(page_setup);
g_object_unref(print_op);
return TRUE;
}
gboolean nsgtk_on_source_close_activate( GtkMenuItem *widget, gpointer g)
{
struct nsgtk_source_window * nsg = (struct nsgtk_source_window *)g;
gtk_widget_destroy(GTK_WIDGET(nsg->sourcewindow));
return TRUE;
}
gboolean nsgtk_source_delete_event(GtkWindow * window, gpointer g)
{
return FALSE;
}
gboolean nsgtk_source_destroy_event(GtkWindow * window, gpointer g)
{
struct nsgtk_source_window * nsg = (struct nsgtk_source_window *)g;
if (nsg->next) {
if (nsg != nsgtk_source_list) { /* in the middle */
nsg->prev->next = nsg->next;
nsg->next->prev = nsg->prev;
} else { /* last */
nsg->next->prev = NULL;
nsgtk_source_list = nsg->next;
}
} else if (nsg->prev) { /* first */
nsg->prev->next = NULL;
} else { /* unique */
nsgtk_source_list = NULL;
}
free(nsg->data);
free(nsg->url);
free(g);
return FALSE;
}
gboolean nsgtk_on_source_select_all_activate (GtkMenuItem *widget, gpointer g) {
struct nsgtk_source_window * nsg = (struct nsgtk_source_window *)g;
GtkTextBuffer *buf = gtk_text_view_get_buffer(nsg->gv);
GtkTextIter start, end;
gtk_text_buffer_get_bounds(buf, &start, &end);
gtk_text_buffer_select_range(buf, &start, &end);
return TRUE;
}
gboolean nsgtk_on_source_cut_activate( GtkMenuItem *widget, gpointer g)
{
return TRUE;
}
gboolean nsgtk_on_source_copy_activate( GtkMenuItem *widget, gpointer g)
{
struct nsgtk_source_window * nsg = (struct nsgtk_source_window *)g;
GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(nsg->gv));
gtk_text_buffer_copy_clipboard(buf, gtk_clipboard_get
(GDK_SELECTION_CLIPBOARD));
return TRUE;
}
gboolean nsgtk_on_source_paste_activate( GtkMenuItem *widget, gpointer g)
{
return TRUE;
}
gboolean nsgtk_on_source_delete_activate( GtkMenuItem *widget, gpointer g)
{
return TRUE;
}
gboolean nsgtk_on_source_zoom_in_activate( GtkMenuItem *widget, gpointer g)
{
source_zoomlevel++;
nsgtk_source_update_zoomlevel(g);
return TRUE;
}
gboolean nsgtk_on_source_zoom_out_activate( GtkMenuItem *widget, gpointer g)
{
if (source_zoomlevel > 1) {
source_zoomlevel--;
nsgtk_source_update_zoomlevel(g);
}
return TRUE;
}
gboolean nsgtk_on_source_zoom_normal_activate( GtkMenuItem *widget, gpointer
g)
{
source_zoomlevel = 10;
nsgtk_source_update_zoomlevel(g);
return TRUE;
}
gboolean nsgtk_on_source_about_activate( GtkMenuItem *widget, gpointer g)
{
struct nsgtk_source_window *nsg = (struct nsgtk_source_window *)g;
nsgtk_about_dialog_init(nsg->sourcewindow, nsg->bw, netsurf_version);
return TRUE;
}
void nsgtk_source_update_zoomlevel(gpointer g)
{
struct nsgtk_source_window *nsg;
GtkTextBuffer *buf;
GtkTextTagTable * tab;
GtkTextTag *tag;
nsg = nsgtk_source_list;
while (nsg) {
if (nsg->gv) {
buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(nsg->gv));
tab = gtk_text_buffer_get_tag_table(GTK_TEXT_BUFFER(buf));
tag = gtk_text_tag_table_lookup(tab, "zoomlevel");
if (!tag) {
tag = gtk_text_tag_new("zoomlevel");
gtk_text_tag_table_add(tab, GTK_TEXT_TAG(tag));
}
gdouble fscale = ((gdouble)source_zoomlevel) / 10;
g_object_set(GTK_TEXT_TAG(tag), "scale", fscale, NULL);
GtkTextIter start, end;
gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(buf), &start, &end);
gtk_text_buffer_remove_all_tags(GTK_TEXT_BUFFER(buf), &start,
&end);
gtk_text_buffer_apply_tag(GTK_TEXT_BUFFER(buf), GTK_TEXT_TAG(tag),
&start, &end);
}
nsg = nsg->next;
}
}