~ubuntu-branches/ubuntu/vivid/winefish/vivid

« back to all changes in this revision

Viewing changes to src/winefish.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Leidert (dale)
  • Date: 2006-04-14 14:41:11 UTC
  • Revision ID: james.westby@ubuntu.com-20060414144111-wi90w6pr70ifwxtw
Tags: upstream-1.3.3
ImportĀ upstreamĀ versionĀ 1.3.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: winefish.c 1984 2006-02-17 13:32:56Z kyanh $ */
 
2
 
 
3
/* Winefish LaTeX Editor (based on Bluefish HTML Editor)
 
4
 *
 
5
 * Original File: bluefish.c - the main function
 
6
 *
 
7
 * Copyright (C) 1998 Olivier Sessink and Chris Mazuc
 
8
 * Copyright (C) 1999-2004 Olivier Sessink
 
9
 * Modified for Winefish (C) 2005 Ky Anh <kyanh@o2.pl> 
 
10
 *
 
11
 * This program is free software; you can redistribute it and/or modify
 
12
 * it under the terms of the GNU General Public License as published by
 
13
 * the Free Software Foundation; either version 2 of the License, or
 
14
 * (at your option) any later version.
 
15
 *
 
16
 * This program is distributed in the hope that it will be useful,
 
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 * GNU General Public License for more details.
 
20
 *
 
21
 * You should have received a copy of the GNU General Public License
 
22
 * along with this program; if not, write to the Free Software
 
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
24
 */
 
25
/* #define DEBUG */
 
26
 
 
27
#include <gtk/gtk.h>
 
28
#include <unistd.h> /* getopt() */
 
29
#include <stdlib.h> /* getopt() exit() and abort() on Solaris */
 
30
#include <time.h> /* nanosleep */
 
31
#include <string.h> /* strcmp */
 
32
 
 
33
#include "bluefish.h"
 
34
 
 
35
#ifdef HAVE_ATLEAST_GNOMEUI_2_6
 
36
#include <libgnomeui/libgnomeui.h>
 
37
#endif
 
38
 
 
39
#ifdef ENABLE_NLS
 
40
#include <locale.h>
 
41
#endif
 
42
 
 
43
#include "document.h" /*  */
 
44
#include "gui.h" /* gui_create_main() */
 
45
#include "fref.h" /* fref_init() */
 
46
#include "bookmark.h"  /* bmark_init() */
 
47
#include "rcfile.h" /* rcfile_parse_main() */
 
48
#include "bf_lib.h" /* create_full_path() */
 
49
#include "highlight.h" /* hl_init() */
 
50
#include "msg_queue.h" /* msg_queue_start()*/
 
51
#include "stringlist.h" /* put_stringlist(), get_stringlist() */
 
52
#include "gtk_easy.h" /* flush_queue() */
 
53
#include "filebrowser.h" /* filters_rebuild() */
 
54
#include "project.h"
 
55
/* kyanh, removed, 20050309 */
 
56
/*#include "authen.h" *//* set_authen_callbacks() */
 
57
#include "autox.h" /* kyanh, completion */
 
58
#include "snooper.h" /* snooper_install() */
 
59
 
 
60
/*********************************************/
 
61
/* this var is global for all bluefish files */
 
62
/*********************************************/
 
63
Tmain *main_v;
 
64
 
 
65
/********************************/
 
66
/* functions used in bluefish.c */
 
67
/********************************/
 
68
#ifndef __GNUC__
 
69
void g_none(gchar *first, ...) {
 
70
        return;
 
71
}
 
72
#endif
 
73
 
 
74
static gint parse_commandline(int argc, char **argv
 
75
                , gboolean *root_override
 
76
                , GList **load_filenames
 
77
                , GList **load_projects
 
78
                , gboolean *open_in_new_win
 
79
                , gint *linenumber
 
80
                             ) {
 
81
        int c;
 
82
        gchar *tmpname;
 
83
 
 
84
        opterr = 0;
 
85
        DEBUG_MSG("parse_commandline, started\n");
 
86
        while ((c = getopt(argc, argv, "hsvn:l:p:?")) != -1) {
 
87
                switch (c) {
 
88
                case 's':
 
89
                        *root_override = 1;
 
90
                        break;
 
91
                case 'v':
 
92
                        g_print(CURRENT_VERSION_NAME);
 
93
                        g_print("\n");
 
94
                        exit(1);
 
95
                        break;
 
96
                case 'p':
 
97
                        tmpname = create_full_path(optarg, NULL);
 
98
                        *load_projects = g_list_append(*load_projects, tmpname);
 
99
                        break;
 
100
                case 'l':
 
101
                        *linenumber = strtoul(optarg, NULL,10);
 
102
                        break;
 
103
                case 'h':
 
104
                case '?':
 
105
                        g_print(CURRENT_VERSION_NAME);
 
106
                        g_print(_("\nUsage: %s [options] [filenames ...]\n"), argv[0]);
 
107
                        g_print(_("\nCurrently accepted options are:\n"));
 
108
                        g_print(_("-s           skip root check\n"));
 
109
                        g_print(_("-v           current version\n"));
 
110
                        g_print(_("-n 0|1       open new window (1) or not (0)\n"));
 
111
                        g_print(_("-p filename  open project\n"));
 
112
                        g_print(_("-l number    set line<number>. Negative value takes no effect.\n"));
 
113
                        g_print(_("-h           this help screen\n"));
 
114
                        exit(1);
 
115
                        break;
 
116
                case 'n':
 
117
                        if (strncmp(optarg,"0",1)==0) {
 
118
                                *open_in_new_win = 0;
 
119
                        }else{  
 
120
                                *open_in_new_win = 1;
 
121
                        }
 
122
                        break;
 
123
                default:
 
124
                        DEBUG_MSG("parse_commandline, abort ?!?\n");
 
125
                        abort();
 
126
                }
 
127
        }
 
128
        DEBUG_MSG("parse_commandline, optind=%d, argc=%d\n", optind, argc);
 
129
        while (optind < argc) {
 
130
                /* related to BUG#93 */
 
131
                /*if (file_exists_and_readable(argv[optind])){*/
 
132
                        tmpname = create_full_path(argv[optind], NULL);
 
133
                        DEBUG_MSG("parse_commandline, argv[%d]=%s, tmpname=%s\n", optind, argv[optind], tmpname);
 
134
                        *load_filenames = g_list_append(*load_filenames, tmpname);
 
135
                /*}else{
 
136
                        g_print("winefish: file '%s' is unreadable\n", argv[optind]);
 
137
                }*/
 
138
                optind++;
 
139
        }
 
140
        DEBUG_MSG("parse_commandline, finished, num files=%d, num projects=%d\n"
 
141
                , g_list_length(*load_filenames), g_list_length(*load_projects));
 
142
        return 0;
 
143
}
 
144
 
 
145
 
 
146
/*********************/
 
147
/* the main function */
 
148
/*********************/
 
149
 
 
150
int main(int argc, char *argv[])
 
151
{
 
152
        gboolean root_override=FALSE, open_in_new_window = FALSE;
 
153
        GList *filenames = NULL, *projectfiles=NULL;
 
154
        gint linenumber = -1;
 
155
 
 
156
        Tbfwin *firstbfwin;
 
157
#ifndef NOSPLASH
 
158
        GtkWidget *splash_window;
 
159
#endif /* #ifndef NOSPLASH */
 
160
 
 
161
#ifdef ENABLE_NLS
 
162
        setlocale(LC_ALL,"");
 
163
        bindtextdomain(PACKAGE,LOCALEDIR);
 
164
        DEBUG_MSG("set bindtextdomain for %s to %s\n",PACKAGE,LOCALEDIR);
 
165
        bind_textdomain_codeset(PACKAGE, "UTF-8");
 
166
        textdomain(PACKAGE);
 
167
#endif
 
168
#ifdef HAVE_ATLEAST_GNOMEUI_2_6
 
169
        gnome_init(PACKAGE, VERSION, argc, argv);
 
170
#else
 
171
        gtk_init(&argc, &argv);
 
172
#endif /* HAVE_ATLEAST_GNOMEUI_2_6
 
173
 */
 
174
        main_v = g_new0(Tmain, 1);
 
175
        main_v->session = g_new0(Tsessionvars,1);
 
176
        DEBUG_MSG("main, main_v is at %p\n", main_v);
 
177
 
 
178
        rcfile_check_directory();
 
179
        rcfile_parse_main();
 
180
 
 
181
        parse_commandline(argc, argv, &root_override, &filenames, &projectfiles, &open_in_new_window, &linenumber);
 
182
#ifdef WITH_MSG_QUEUE
 
183
        if (((filenames || projectfiles) && (main_v->props.view_bars & MODE_REUSE_WINDOW)) || open_in_new_window) {
 
184
                msg_queue_start(filenames, projectfiles, linenumber, open_in_new_window);
 
185
        }
 
186
#endif /* WITH_MSG_QUEUE */
 
187
#ifndef NOSPLASH
 
188
        /* start splash screen somewhere here */
 
189
        splash_window = start_splash_screen();
 
190
        splash_screen_set_label(_("parsing highlighting file..."));
 
191
#endif /* #ifndef NOSPLASH */
 
192
 
 
193
        {
 
194
                gchar *filename = g_strconcat(g_get_home_dir(), "/.winefish/dir_history", NULL);
 
195
                main_v->recent_directories = get_stringlist(filename, NULL);
 
196
                g_free(filename);
 
197
        }
 
198
        rcfile_parse_global_session();
 
199
        rcfile_parse_highlighting();
 
200
#ifndef NOSPLASH
 
201
        splash_screen_set_label(_("compiling highlighting patterns..."));
 
202
#endif /* #ifndef NOSPLASH */
 
203
        hl_init();
 
204
#ifndef NOSPLASH
 
205
        splash_screen_set_label(_("initialize some other things..."));
 
206
#endif /* #ifndef NOSPLASH */
 
207
        filebrowserconfig_init();
 
208
        filebrowser_filters_rebuild();
 
209
        autoclosing_init();
 
210
#ifndef NOSPLASH
 
211
        splash_screen_set_label(_("parsing autotext and words file..."));
 
212
#endif /* #ifndef NOSPLASH */
 
213
        autotext_init();
 
214
        completion_init();
 
215
#ifndef NOSPLASH
 
216
        splash_screen_set_label(_("parsing custom menu file..."));
 
217
#endif /* #ifndef NOSPLASH */
 
218
        rcfile_parse_custom_menu(FALSE,FALSE);
 
219
        main_v->tooltips = gtk_tooltips_new();
 
220
        /* initialize the completion window */
 
221
        main_v->completion.window = NULL;
 
222
        fref_init();
 
223
        bmark_init();
 
224
#ifdef WITH_MSG_QUEUE
 
225
        if (!filenames && !projectfiles && (main_v->props.view_bars & MODE_REUSE_WINDOW)) {
 
226
                msg_queue_start(NULL, NULL, -1, open_in_new_window);
 
227
        }
 
228
#endif /* WITH_MSG_QUEUE */
 
229
#ifndef NOSPLASH
 
230
        splash_screen_set_label(_("creating main gui..."));
 
231
#endif /* #ifndef NOSPLASH */
 
232
 
 
233
        /* create the first window */
 
234
        firstbfwin = g_new0(Tbfwin,1);
 
235
        firstbfwin->session = main_v->session;
 
236
        firstbfwin->bookmarkstore = main_v->bookmarkstore;
 
237
        main_v->bfwinlist = g_list_append(NULL, firstbfwin);
 
238
        gui_create_main(firstbfwin, filenames, linenumber);
 
239
        bmark_reload(firstbfwin);
 
240
#ifndef NOSPLASH
 
241
        splash_screen_set_label(_("showing main gui..."));
 
242
#endif /* #ifndef NOSPLASH */
 
243
 
 
244
        /* set GTK settings, must be AFTER the menu is created */
 
245
        {
 
246
                gchar *shortcutfilename;
 
247
                GtkSettings* gtksettings = gtk_settings_get_default();
 
248
                g_object_set(G_OBJECT(gtksettings), "gtk-can-change-accels", TRUE, NULL); 
 
249
                shortcutfilename = g_strconcat(g_get_home_dir(), "/.winefish/menudump_2", NULL);
 
250
                gtk_accel_map_load(shortcutfilename);
 
251
                g_free(shortcutfilename);
 
252
        }
 
253
 
 
254
        gui_show_main(firstbfwin);
 
255
        /*
 
256
        if (main_v->props.view_html_toolbar && main_v->globses.quickbar_items == NULL) {
 
257
                info_dialog(firstbfwin->main_window, _("Winefish tip:"), _("This message is shown since you DONOT have any items in the QuickBar.\n\nIf you right-click a button in the Standard toolbars you can add buttons to the Quickbar."));
 
258
        }
 
259
        */
 
260
        if (projectfiles) {
 
261
                GList *tmplist = g_list_first(projectfiles);
 
262
                while (tmplist) {
 
263
                        project_open_from_file(firstbfwin, tmplist->data, linenumber);
 
264
                        tmplist = g_list_next(tmplist);
 
265
                }
 
266
        }
 
267
 
 
268
#ifndef NOSPLASH
 
269
        DEBUG_MSG("destroy splash\n");
 
270
        flush_queue();
 
271
        {
 
272
                static struct timespec const req = { 0, 10000000};
 
273
                nanosleep(&req, NULL);
 
274
        }
 
275
        gtk_widget_destroy(splash_window);
 
276
#endif /* #ifndef NOSPLASH */
 
277
 
 
278
        /* snooper must be installed after the main gui has shown;
 
279
        otherwise the program may be aborted */
 
280
        snooper_install();
 
281
 
 
282
        DEBUG_MSG("main, before gtk_main()\n");
 
283
        gtk_main();
 
284
        DEBUG_MSG("main, after gtk_main()\n");
 
285
#ifdef WITH_MSG_QUEUE   
 
286
        /* do the cleanup */
 
287
        msg_queue_cleanup();
 
288
#endif /* WITH_MSG_QUEUE */
 
289
        DEBUG_MSG("Winefish: exiting cleanly\n");
 
290
        return 0;
 
291
}
 
292
 
 
293
void bluefish_exit_request() {
 
294
 
 
295
        GList *tmplist;
 
296
        
 
297
        gboolean tmpb;
 
298
        DEBUG_MSG("winefish_exit_request, started\n");
 
299
        /* if we have modified documents we have to do something, file_close_all_cb()
 
300
        does exactly want we want to do */
 
301
        tmplist = return_allwindows_documentlist();
 
302
        tmpb = (tmplist && test_docs_modified(tmplist));
 
303
        g_list_free(tmplist);
 
304
        tmplist = g_list_first(main_v->bfwinlist);
 
305
        while (tmplist) {
 
306
                /* if there is a project, we anyway want to save & close the project */
 
307
                if (BFWIN(tmplist->data)->project) {
 
308
                        if (!project_save_and_close(BFWIN(tmplist->data))) {
 
309
                                /* cancelled or error! */
 
310
                                DEBUG_MSG("winefish_exit_request, project_save_and_close returned FALSE\n");
 
311
                                return;
 
312
                        }
 
313
                }
 
314
                if (tmpb) {
 
315
                        file_close_all_cb(NULL, BFWIN(tmplist->data));
 
316
                }
 
317
                tmplist = g_list_next(tmplist);
 
318
        }
 
319
        /* if we still have modified documents we don't do a thing,
 
320
         if we don't have them we can quit */
 
321
        if (tmpb) {
 
322
                tmplist = return_allwindows_documentlist();
 
323
                tmpb = (tmplist && test_docs_modified(tmplist));
 
324
                g_list_free(tmplist);
 
325
                if (tmpb) {
 
326
                        return;
 
327
                }
 
328
        }
 
329
/*      gtk_widget_hide(main_v->main_window);*/
 
330
        
 
331
        tmplist = g_list_first(gtk_window_list_toplevels());
 
332
        gchar *role=NULL;
 
333
        while (tmplist) {
 
334
                /* BUG#38 */
 
335
                if (GTK_IS_WIDGET(tmplist->data)) {
 
336
                        role = g_strdup(gtk_window_get_role ((GtkWindow*)tmplist->data));
 
337
                        gtk_widget_hide(GTK_WIDGET(tmplist->data));
 
338
                        if (role && strncmp(role,"html_dialog",11) ==0) {
 
339
                                window_destroy(GTK_WIDGET(tmplist->data));
 
340
                        }
 
341
                }
 
342
                /* g_print("type = %s, role=%s\n", GTK_OBJECT_TYPE_NAME((GtkObject*) tmplist->data), role); */
 
343
                tmplist = g_list_next(tmplist);
 
344
        }
 
345
        g_free(role);
 
346
 
 
347
        flush_queue();
 
348
 
 
349
        rcfile_save_all();
 
350
        {
 
351
                gchar *filename = g_strconcat(g_get_home_dir(), "/.winefish/dir_history", NULL);
 
352
                put_stringlist_limited(filename, main_v->recent_directories, main_v->props.max_dir_history);
 
353
                g_free(filename);
 
354
        }
 
355
        
 
356
        gtk_main_quit();
 
357
}