make gtk source viewer less explody. fixes #2159

This commit is contained in:
Vincent Sanders 2014-07-21 01:13:10 +01:00
parent a05b5fcff4
commit 698b7c61f0
3 changed files with 105 additions and 82 deletions

View File

@ -2014,6 +2014,11 @@ bool browser_window_has_content(struct browser_window *bw)
return true; return true;
} }
/* Exported interface, documented in browser.h */
struct hlcache_handle *browser_window_get_content(struct browser_window *bw)
{
return bw->current_content;
}
/* Exported interface, documented in browser.h */ /* Exported interface, documented in browser.h */
nserror browser_window_get_extents(struct browser_window *bw, bool scaled, nserror browser_window_get_extents(struct browser_window *bw, bool scaled,

View File

@ -197,6 +197,11 @@ nserror browser_window_get_extents(struct browser_window *bw, bool scaled,
*/ */
bool browser_window_has_content(struct browser_window *bw); bool browser_window_has_content(struct browser_window *bw);
/**
* Get a cache handle for the content within a browser window.
*/
struct hlcache_handle *browser_window_get_content(struct browser_window *bw);
void browser_window_get_dimensions(struct browser_window *bw, void browser_window_get_dimensions(struct browser_window *bw,
int *width, int *height, bool scaled); int *width, int *height, bool scaled);
void browser_window_set_dimensions(struct browser_window *bw, void browser_window_set_dimensions(struct browser_window *bw,

View File

@ -59,8 +59,6 @@ static GtkBuilder *glade_File;
static struct nsgtk_source_window *nsgtk_source_list = 0; static struct nsgtk_source_window *nsgtk_source_list = 0;
static char source_zoomlevel = 10; static char source_zoomlevel = 10;
void nsgtk_source_tab_init(GtkWindow *parent, struct browser_window *bw);
#define MENUEVENT(x) { #x, G_CALLBACK(nsgtk_on_##x##_activate) } #define MENUEVENT(x) { #x, G_CALLBACK(nsgtk_on_##x##_activate) }
#define MENUPROTO(x) static gboolean nsgtk_on_##x##_activate( \ #define MENUPROTO(x) static gboolean nsgtk_on_##x##_activate( \
GtkMenuItem *widget, gpointer g) GtkMenuItem *widget, gpointer g)
@ -130,82 +128,81 @@ static gboolean nsgtk_source_delete_event(GtkWindow * window, gpointer g)
return FALSE; return FALSE;
} }
void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw)
{
char glade_Location[strlen(res_dir_location) + SLEN("source.gtk2.ui")
+ 1];
if (content_get_type(bw->current_content) != CONTENT_HTML)
return;
if (nsoption_bool(source_tab)) { static nserror
nsgtk_source_tab_init(parent, bw); nsgtk_source_win_init(GtkWindow *parent, struct browser_window *bw)
return; {
char glade_Location[strlen(res_dir_location) + SLEN("source.gtk2.ui") + 1];
struct hlcache_handle *hlcontent;
GError* error = NULL;
const char *source_data;
unsigned long source_size;
char *data = NULL;
size_t data_len;
nserror ret;
GtkWindow *wndSource;
GtkWidget *cutbutton;
GtkWidget *pastebutton;
GtkWidget *deletebutton;
GtkWidget *printbutton;
GtkTextView *sourceview;
PangoFontDescription *fontdesc;
GtkTextBuffer *tb;
struct nsgtk_source_window *thiswindow;
hlcontent = browser_window_get_content(bw);
if (hlcontent == NULL) {
return NSERROR_OK;
}
if (content_get_type(hlcontent) != CONTENT_HTML) {
return NSERROR_OK;
} }
sprintf(glade_Location, "%ssource.gtk2.ui", res_dir_location); sprintf(glade_Location, "%ssource.gtk2.ui", res_dir_location);
GError* error = NULL;
glade_File = gtk_builder_new(); glade_File = gtk_builder_new();
if (!gtk_builder_add_from_file(glade_File, glade_Location, &error)) { if (!gtk_builder_add_from_file(glade_File, glade_Location, &error)) {
g_warning ("Couldn't load builder file: %s", error->message); g_warning ("Couldn't load builder file: %s", error->message);
g_error_free (error); g_error_free (error);
LOG(("error loading glade tree")); LOG(("error loading glade tree"));
return; return NSERROR_OK;
} }
source_data = content_get_source_data(hlcontent, &source_size);
const char *source_data; ret = utf8_from_enc(source_data,
unsigned long source_size; html_get_encoding(hlcontent),
char *data = NULL;
size_t data_len;
source_data = content_get_source_data(bw->current_content,
&source_size);
nserror r = utf8_from_enc(
source_data,
html_get_encoding(bw->current_content),
source_size, source_size,
&data, &data,
&data_len); &data_len);
if (r == NSERROR_NOMEM) { if (ret != NSERROR_OK) {
warn_user("NoMemory",0); return ret;
return;
} else if (r == NSERROR_BAD_ENCODING) {
warn_user("EncNotRec",0);
return;
} }
GtkWindow *wndSource = GTK_WINDOW(gtk_builder_get_object( wndSource = GTK_WINDOW(gtk_builder_get_object(glade_File, "wndSource"));
glade_File, "wndSource")); cutbutton = GTK_WIDGET(gtk_builder_get_object(glade_File, "source_cut"));
GtkWidget *cutbutton = GTK_WIDGET(gtk_builder_get_object( pastebutton = GTK_WIDGET(gtk_builder_get_object(glade_File, "source_paste"));
glade_File, "source_cut")); deletebutton = GTK_WIDGET(gtk_builder_get_object(glade_File, "source_delete"));
GtkWidget *pastebutton = GTK_WIDGET(gtk_builder_get_object( printbutton = GTK_WIDGET(gtk_builder_get_object(glade_File, "source_print"));
glade_File, "source_paste"));
GtkWidget *deletebutton = GTK_WIDGET(gtk_builder_get_object(
glade_File, "source_delete"));
GtkWidget *printbutton = GTK_WIDGET(gtk_builder_get_object(
glade_File, "source_print"));
gtk_widget_set_sensitive(cutbutton, FALSE); gtk_widget_set_sensitive(cutbutton, FALSE);
gtk_widget_set_sensitive(pastebutton, FALSE); gtk_widget_set_sensitive(pastebutton, FALSE);
gtk_widget_set_sensitive(deletebutton, FALSE); gtk_widget_set_sensitive(deletebutton, FALSE);
/* for now */ /* for now */
gtk_widget_set_sensitive(printbutton, FALSE); gtk_widget_set_sensitive(printbutton, FALSE);
struct nsgtk_source_window *thiswindow = thiswindow = malloc(sizeof(struct nsgtk_source_window));
malloc(sizeof(struct nsgtk_source_window));
if (thiswindow == NULL) { if (thiswindow == NULL) {
free(data); free(data);
warn_user("NoMemory", 0); return NSERROR_NOMEM;
return;
} }
thiswindow->url = strdup(nsurl_access(browser_window_get_url(bw))); thiswindow->url = strdup(nsurl_access(browser_window_get_url(bw)));
if (thiswindow->url == NULL) { if (thiswindow->url == NULL) {
free(thiswindow); free(thiswindow);
free(data); free(data);
warn_user("NoMemory", 0); return NSERROR_NOMEM;
return;
} }
thiswindow->data = data; thiswindow->data = data;
@ -219,8 +216,9 @@ void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw)
thiswindow->next = nsgtk_source_list; thiswindow->next = nsgtk_source_list;
thiswindow->prev = NULL; thiswindow->prev = NULL;
if (nsgtk_source_list != NULL) if (nsgtk_source_list != NULL) {
nsgtk_source_list->prev = thiswindow; nsgtk_source_list->prev = thiswindow;
}
nsgtk_source_list = thiswindow; nsgtk_source_list = thiswindow;
nsgtk_attach_source_menu_handlers(glade_File, thiswindow); nsgtk_attach_source_menu_handlers(glade_File, thiswindow);
@ -234,87 +232,102 @@ void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw)
G_CALLBACK(nsgtk_source_delete_event), G_CALLBACK(nsgtk_source_delete_event),
thiswindow); thiswindow);
GtkTextView *sourceview = GTK_TEXT_VIEW( sourceview = GTK_TEXT_VIEW(
gtk_builder_get_object(glade_File, gtk_builder_get_object(glade_File,
"source_view")); "source_view"));
PangoFontDescription *fontdesc = fontdesc = pango_font_description_from_string("Monospace 8");
pango_font_description_from_string("Monospace 8");
thiswindow->gv = sourceview; thiswindow->gv = sourceview;
nsgtk_widget_modify_font(GTK_WIDGET(sourceview), fontdesc); nsgtk_widget_modify_font(GTK_WIDGET(sourceview), fontdesc);
GtkTextBuffer *tb = gtk_text_view_get_buffer(sourceview); tb = gtk_text_view_get_buffer(sourceview);
gtk_text_buffer_set_text(tb, thiswindow->data, -1); gtk_text_buffer_set_text(tb, thiswindow->data, -1);
gtk_widget_show(GTK_WIDGET(wndSource)); gtk_widget_show(GTK_WIDGET(wndSource));
return NSERROR_OK;
} }
/** /**
* create a new tab with page source * create a new tab with page source
*/ */
void nsgtk_source_tab_init(GtkWindow *parent, struct browser_window *bw) static nserror
nsgtk_source_tab_init(GtkWindow *parent, struct browser_window *bw)
{ {
const char *source_data; const char *source_data;
unsigned long source_size; unsigned long source_size;
char *ndata = 0; char *ndata = NULL;
size_t ndata_len; size_t ndata_len;
nsurl *url; nsurl *url;
nserror error; nserror ret;
nserror r;
gchar *filename; gchar *filename;
gint handle; gint handle;
struct hlcache_handle *hlcontent;
FILE *f;
source_data = content_get_source_data(bw->current_content, hlcontent = browser_window_get_content(bw);
&source_size); if (hlcontent == NULL) {
return NSERROR_OK;
}
r = utf8_from_enc(source_data, if (content_get_type(hlcontent) != CONTENT_HTML) {
html_get_encoding(bw->current_content), return NSERROR_OK;
}
source_data = content_get_source_data(hlcontent, &source_size);
ret = utf8_from_enc(source_data,
html_get_encoding(hlcontent),
source_size, source_size,
&ndata, &ndata,
&ndata_len); &ndata_len);
if (r == NSERROR_NOMEM) { if (ret != NSERROR_OK) {
warn_user("NoMemory",0); return ret;
return;
} else if (r == NSERROR_BAD_ENCODING) {
warn_user("EncNotRec",0);
return;
} }
handle = g_file_open_tmp("nsgtksourceXXXXXX", &filename, NULL); handle = g_file_open_tmp("nsgtksourceXXXXXX", &filename, NULL);
if ((handle == -1) || (filename == NULL)) { if ((handle == -1) || (filename == NULL)) {
warn_user(messages_get("gtkSourceTabError"), 0); warn_user(messages_get("gtkSourceTabError"), 0);
free(ndata); free(ndata);
return; return NSERROR_SAVE_FAILED;
} }
close (handle); /* in case it was binary mode */ close(handle); /* in case it was binary mode */
FILE *f = fopen(filename, "w"); f = fopen(filename, "w");
if (f == NULL) { if (f == NULL) {
warn_user(messages_get("gtkSourceTabError"), 0); warn_user(messages_get("gtkSourceTabError"), 0);
g_free(filename); g_free(filename);
free(ndata); free(ndata);
return; return NSERROR_SAVE_FAILED;
} }
fprintf(f, "%s", ndata); fprintf(f, "%s", ndata);
fclose(f); fclose(f);
free(ndata); free(ndata);
/* Open tab */ /* Open tab */
error = netsurf_path_to_nsurl(filename, &url); ret = netsurf_path_to_nsurl(filename, &url);
g_free(filename); g_free(filename);
if (error == NSERROR_OK) { if (ret == NSERROR_OK) {
error = browser_window_create(BW_CREATE_TAB, ret = browser_window_create(BW_CREATE_TAB | BW_CREATE_CLONE,
url, url, NULL, bw, NULL);
NULL,
bw,
NULL);
nsurl_unref(url); nsurl_unref(url);
} }
if (error != NSERROR_OK) {
warn_user(messages_get_errorcode(error), 0); return ret;
}
void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw)
{
nserror ret;
if (nsoption_bool(source_tab)) {
ret = nsgtk_source_tab_init(parent, bw);
} else {
ret = nsgtk_source_win_init(parent, bw);
}
if (ret != NSERROR_OK) {
warn_user(messages_get_errorcode(ret), 0);
} }
} }