/**
* erwin - really simple html editor
* Copyright (C) 1999 David Vogler
* Copyright (C) 1999-2005 Adrian Reber
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: erwin.c,v 1.34 2005/01/19 21:03:23 adrian Exp $
*
* $Author: adrian $
*
* Description: main startup file
*
* $Log: erwin.c,v $
* Revision 1.34 2005/01/19 21:03:23 adrian
* save_file has been changed to be a real G_CALLBACK function
*
* Revision 1.33 2004/12/29 20:01:38 adrian
* * re-indent-ed
* * our document changed handler should be the last
*
* Revision 1.32 2004/10/03 20:18:01 erwin
* more searchbar related changes
*
* Revision 1.31 2004/09/26 21:07:00 erwin
* - more work on the searchbar
*
* Revision 1.30 2004/09/23 20:22:46 erwin
* attempt to clone the searchbar from firefox. so far so good.
*
* Revision 1.29 2004/07/03 20:14:25 erwin
* commit
*
* Revision 1.28 2004/06/30 19:50:38 erwin
* * removed more global variables
* * fix save_file() to save the correct file
*
* Revision 1.27 2004/06/27 17:44:37 erwin
* * bug fixes
* * changed clipboard to use the globals struct
* * added drag'n'drop support
*
* Revision 1.26 2004/06/21 21:45:19 erwin
* big commit
* it has been a long time without a checkout
* we are close to version 0.7
*
* Revision 1.25 2004/03/11 21:05:01 erwin
* too many changes without commits
*
* Revision 1.24 2003/10/30 20:54:06 erwin
* syntax highlighting implementation
*
* Revision 1.23 2003/10/22 15:21:06 erwin
* include file mess cleaned up
* changed format to indent -kr -i8 -l100
* gtksourceview support
*
*/
#ifndef lint
static const char vcid[] = "$Id: erwin.c,v 1.34 2005/01/19 21:03:23 adrian Exp $";
#endif /* lint */
#include
#include
#include "erwin.h"
#include "erwinnotebook.h"
#include "erwintoolbar.h"
#include "startup.h"
#include "configuration.h"
#include "erwinfunctions.h"
#include "erwinmenu.h"
#include "recent.h"
#include
#include
#include
#include
#include "xpm/logo_new1.xpm"
GtkWidget *main_window;
GtkWidget *status_bar;
extern gint close_notebook;
int _debug_ = 0;
struct global globals;
void save_file(GtkToolButton *toolbutton, struct document *document)
{
FILE *fd;
gchar *erwin;
GtkTextBuffer *buffer;
GtkTextIter start, end;
gint page_num = 0;
if (close_notebook == -1)
page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook_main));
if (!document)
document = (g_list_nth(documents, page_num))->data;
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(document->text_view));
gtk_text_buffer_get_start_iter(buffer, &start);
gtk_text_buffer_get_end_iter(buffer, &end);
if (strcmp(document->filename->str, "Untitled")) {
_DEBUG_ fprintf(stderr, "%s:now saving file %s\n", PACKAGE,
document->filename->str);
//is a empty document?
if (gtk_text_iter_compare(&start, &end)) {
if (!is_file_readonly(document->filename->str)) {
_DEBUG_ fprintf(stderr, "%s:now writing to file %s\n", PACKAGE,
document->filename->str);
fd = fopen(document->filename->str, "w");
erwin = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
_DEBUG_ fprintf(stderr, "%s:saving:\n%s\n", PACKAGE, erwin);
fprintf(fd, "%s", erwin);
g_free(erwin);
fclose(fd);
gtk_text_buffer_set_modified(buffer, FALSE);
document->changed = FALSE;
document->saved = TRUE;
gtk_label_set_text(GTK_LABEL(document->notebook_label),
g_basename(document->filename->str));
set_title_name();
add_to_recent(document->filename->str);
update_recent(NULL);
} else {
display_dialog("Error",
"Insufficient permissions\n to write that file");
}
} else {
if (!is_file_readonly(document->filename->str)) {
fd = fopen(document->filename->str, "w");
fclose(fd);
set_title_name();
gtk_text_buffer_set_modified(buffer, FALSE);
document->changed = FALSE;
gtk_label_set_text(GTK_LABEL(document->notebook_label),
g_basename(document->filename->str));
}
}
} else {
save_file_dlg(document);
}
if (document->close_file && strcmp(document->filename->str, "Untitled")) {
document->close_file = FALSE;
really_close_file();
}
if (QUIT && strcmp(document->filename->str, "Untitled"))
really_quit_program();
}
void display_main_window(gboolean open, int width, int height)
{
GtkWidget *hbox;
GtkWidget *vbox;
GtkWidget *notebook_box;
GtkWidget *main_vbox;
GtkWidget *menubar, *recent_menu;
GtkWidget *toolbar;
GtkWidget *toolbox;
gchar erwin[1024] = "";
FILE *fd;
struct document *document;
GdkPixbuf *icon;
GtkTextBuffer *buffer;
GtkTextIter end;
char *error;
int x, y;
/* drag'n'drop ' */
static GtkTargetEntry drop_types[] = {
{"text/uri-list", 0, 0}
};
static gint n_drop_types = sizeof(drop_types) / sizeof(drop_types[0]);
QUIT = FALSE;
document = g_malloc(sizeof(struct document));
memset(document, 0, sizeof(struct document));
main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(main_window), PACKAGE " - Untitled");
globals.main_window = main_window;
gtk_window_position(GTK_WINDOW(main_window), GTK_WIN_POS_CENTER);
gtk_container_border_width(GTK_CONTAINER(main_window), 5);
x = config_get_int("xpos", &error);
y = config_get_int("ypos", &error);
if (width == -1)
width = (gint) (0.7 * gdk_screen_width());
if (height == -1)
height = (gint) (0.6 * gdk_screen_height());
gtk_window_set_default_size(GTK_WINDOW(main_window), width, height);
if (x != -1 && y != -1)
gtk_window_move(GTK_WINDOW(main_window), x, y);
g_signal_connect(G_OBJECT(main_window), "delete_event", G_CALLBACK(quit_program), NULL);
gtk_window_set_wmclass(GTK_WINDOW(main_window), "erwin", "erwin_main");
gtk_widget_realize(main_window);
/*---The icon for the windomanager---------------*/
icon = gdk_pixbuf_new_from_xpm_data((const char **) logo_new1_xpm);
gtk_window_set_icon(GTK_WINDOW(main_window), icon);
main_vbox = gtk_vbox_new(FALSE, 5);
gtk_container_add(GTK_CONTAINER(main_window), main_vbox);
document->text_view = GTK_WIDGET(gtk_source_view_new());
set_view_font(GTK_TEXT_VIEW(document->text_view));
vbox = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(main_vbox), vbox, FALSE, FALSE, 0);
/*--- the menubar ---*/
main_menu(&menubar, &recent_menu);
gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
gtk_widget_show(menubar);
update_recent(recent_menu);
/*--- the end of menubar ---*/
/*---html-toolbar ---*/
toolbox = gtk_hbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), toolbox, FALSE, FALSE, 5);
notebook_html = GTK_WIDGET(html_notebook(main_window, toolbox));
gtk_widget_show(notebook_html);
gtk_widget_show(toolbox);
/*---toolbar-end--*/
gtk_widget_show(vbox);
hbox = gtk_hbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(main_vbox), hbox, TRUE, TRUE, 0);
/*---toolbar ---*/
toolbox = gtk_hbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), toolbox, FALSE, FALSE, 0);
toolbar = main_toolbar(main_window);
gtk_box_pack_start(GTK_BOX(toolbox), toolbar, FALSE, FALSE, 0);
gtk_widget_show(toolbar);
gtk_widget_show(toolbox);
/*---toolbar-end--*/
notebook_box = gtk_vbox_new(FALSE, 0);
notebook_main = text_notebook(notebook_box, document);
gtk_container_add(GTK_CONTAINER(hbox), notebook_box);
globals.notebook_box = notebook_box;
globals.searchbar_visible = 0;
gtk_widget_show(notebook_box);
gtk_widget_show(notebook_main);
_DEBUG_ fprintf(stderr, "%s:%s:%d\n", PACKAGE, __FILE__, __LINE__);
gtk_widget_show(document->text_view);
g_signal_connect(G_OBJECT(notebook_main), "switch_page", G_CALLBACK(switch_page), menubar);
if (open)
document->filename = g_string_new(globals.file);
else
document->filename = g_string_new("Untitled");
document->saved = TRUE;
document->is_rendered = FALSE;
document->new_document = TRUE;
if (open)
gtk_label_set_text(GTK_LABEL(document->notebook_label),
g_basename(document->filename->str));
documents = g_list_append(documents, document);
gtk_widget_show(hbox);
gtk_widget_show(main_vbox);
gtk_widget_show(main_window);
buffer = get_text_buffer();
gtk_text_buffer_create_tag(buffer, "red_background", "background", "red", NULL);
gtk_text_buffer_create_tag(buffer, "yellow_background", "background", "yellow", NULL);
//status bar
status_bar = gtk_statusbar_new();
gtk_box_pack_start(GTK_BOX(main_vbox), status_bar, FALSE, FALSE, 0);
gtk_widget_show(status_bar);
add_to_statusbar(NULL, FALSE);
//highlighting stuff
make_highlighted_text_view(document, GTK_TEXT_BUFFER(buffer));
populate_highlighting_menu(menubar);
//drag'n'drop
gtk_drag_dest_set(GTK_WIDGET(main_window),
GTK_DEST_DEFAULT_ALL, drop_types, n_drop_types, GDK_ACTION_COPY);
gtk_signal_connect(GTK_OBJECT(main_window), "drag_data_received",
GTK_SIGNAL_FUNC(drop_file), NULL);
make_dropable(GTK_TEXT_VIEW(document->text_view));
if ((fd = fopen(document->filename->str, "r")) && open) {
gtk_text_buffer_get_start_iter(buffer, &end);
while (fgets(erwin, 1024, fd) != NULL) {
_DEBUG_ fprintf(stderr, "%s:reading %s\n", PACKAGE, erwin);
gtk_text_buffer_insert(buffer, &end, erwin, -1);
gtk_text_buffer_get_end_iter(buffer, &end);
}
gtk_text_buffer_get_start_iter(buffer, &end);
gtk_text_buffer_place_cursor(buffer, &end);
gtk_text_buffer_set_modified(buffer, FALSE);
fclose(fd);
document->new_document = FALSE;
set_title_name();
add_to_recent(document->filename->str);
update_recent(NULL);
activate_highlighting_from_filename(document);
update_statistic(buffer);
}
globals.clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
g_signal_connect_after(G_OBJECT(buffer), "changed", G_CALLBACK(buffer_changed), NULL);
gtk_window_set_focus(GTK_WINDOW(main_window), document->text_view);
}
int main(int argc, char *argv[])
{
GString *bla;
gboolean open;
char *temp;
char *error;
int width, height;
signal(SIGTERM, sighandler);
signal(SIGINT, sighandler);
gtk_init(&argc, &argv);
fprintf(stderr, "%s Release %s - Copyright (C) 1999 David Vogler\n", PACKAGE, VERSION);
fprintf(stderr, " \"%s\" Copyright (C) 1999-2005 by Adrian Reber\n", RELEASE_NAME);
fprintf(stderr, "%s comes with ABSOLUTELY NO WARRANTY - for details read the license.\n",
PACKAGE);
temp = parse_args(&argc, argv);
config_init(PACKAGE);
width = config_get_int("width", &error);
if (width == -1) {
_DEBUG_ fprintf(stderr, "%s:width: %d %s\n", PACKAGE, width, error);
width = -1;
}
height = config_get_int("height", &error);
if (height == -1) {
_DEBUG_ fprintf(stderr, "%s:height: %d %s\n", PACKAGE, height, error);
height = -1;
}
globals.display_statistic = config_get_int("display_statistic", &error);
if (globals.display_statistic == -1) {
_DEBUG_ fprintf(stderr, "%s:display_statistic: %s\n", PACKAGE, error);
globals.display_statistic = 1;
}
globals.show_quickstart = config_get_int("show_quickstart", &error);
if (globals.show_quickstart == -1) {
_DEBUG_ fprintf(stderr, "%s:show_quickstart: %s\n", PACKAGE, error);
globals.show_quickstart = 1;
}
if (config_get_int("number_of_recent", &error) > 0)
globals.number_of_recent = config_get_int("number_of_recent", &error);
else
globals.number_of_recent = 5;
populate_recent();
globals.default_highlighting = config_get_string("default_highlighting", &error);
if (!globals.default_highlighting)
globals.default_highlighting = strdup("HTML");
config_sync();
_DEBUG_ fprintf(stderr, "This is %s %s compiled with %s\n", PACKAGE, VERSION, __VERSION__);
if (temp) {
strcpy(globals.file, temp);
open = TRUE;
_DEBUG_ fprintf(stderr, "%s:is path absolute? %d\n", PACKAGE,
g_path_is_absolute(globals.file));
if (!g_path_is_absolute(globals.file)) {
bla = g_string_new(g_get_current_dir());
bla = g_string_append(bla, "/");
bla = g_string_append(bla, globals.file);
strcpy(globals.file, bla->str);
g_string_free(bla, TRUE);
}
_DEBUG_ fprintf(stderr, "%s:g_basename %s\n", PACKAGE, g_basename(globals.file));
_DEBUG_ fprintf(stderr, "%s:is path absolute? %d\n", PACKAGE,
g_path_is_absolute(globals.file));
_DEBUG_ fprintf(stderr, "%s:g_get_current_dir %s\n", PACKAGE, g_get_current_dir());
_DEBUG_ fprintf(stderr, "%s:g_dirname %s\n", PACKAGE, g_dirname(globals.file));
_DEBUG_ fprintf(stderr, "%s:Now opening %s\n", PACKAGE, globals.file);
} else {
_DEBUG_ fprintf(stderr, "%s:%s:%d:now opening without file...\n",
PACKAGE, __FILE__, __LINE__);
open = FALSE;
globals.file[0] = 0;
}
/* Default action enabled */
_DEBUG_ signal(SIGSEGV, SIG_DFL);
display_main_window(open, width, height);
gtk_main();
the_end();
return EXIT_SUCCESS;
}