~ubuntu-branches/ubuntu/hoary/gedit/hoary-security

« back to all changes in this revision

Viewing changes to src/document.c

  • Committer: Bazaar Package Importer
  • Author(s): Joe Drew
  • Date: 2002-01-13 02:13:27 UTC
  • Revision ID: james.westby@ubuntu.com-20020113021327-dukaa4n50oykvrjg
Tags: upstream-0.9.6
ImportĀ upstreamĀ versionĀ 0.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
2
/*
 
3
 * gedit
 
4
 *
 
5
 * Copyright (C) 1998, 1999, 2000  Alex Roberts, Evan Lawrence, Jason Leach, Jose M Celorio
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include <config.h>
 
23
#include <gnome.h>
 
24
#include <libgnomevfs/gnome-vfs.h>
 
25
 
 
26
#include "window.h"
 
27
#include "undo.h"
 
28
#include "file.h"
 
29
#include "utils.h"
 
30
#include "document.h"
 
31
#include "prefs.h"
 
32
#include "menus.h"
 
33
#include "commands.h"
 
34
 
 
35
GnomeMDI *mdi;
 
36
typedef void (*gedit_document_signal) (GtkObject *, gpointer, gpointer);
 
37
static GnomeMDIChildClass *parent_class = NULL;
 
38
 
 
39
static gchar*           gedit_document_get_config_string (GnomeMDIChild *child);
 
40
static gint             remove_child_cb (GnomeMDI *mdi, GeditDocument *doc);
 
41
 
 
42
static  GtkWidget*      gedit_document_create_view (GnomeMDIChild *child);
 
43
static  void            gedit_document_destroy (GtkObject *obj);
 
44
static  void            gedit_document_class_init (GeditDocumentClass *class);
 
45
static  void            gedit_document_init (GeditDocument *doc);
 
46
        GtkType         gedit_document_get_type (void);
 
47
 
 
48
/* ----- Local structs ------------ */
 
49
 
 
50
struct _GeditDocumentClass
 
51
{
 
52
        GnomeMDIChildClass parent_class;
 
53
 
 
54
        void (*document_changed)(GeditDocument *, gpointer);
 
55
        
 
56
};
 
57
 
 
58
 
 
59
 
 
60
 
 
61
 
 
62
 
 
63
 
 
64
 
 
65
 
 
66
 
 
67
 
 
68
/**
 
69
 * gedit_document_insert_text: Inserts text to a document and hadles all the details like
 
70
 *                             adding the data to the undo stack and adding it to multiple views
 
71
 *                             Provides an abstraction layer between gedit and the text widget.
 
72
 * @doc: document to insert text
 
73
 * @text: null terminated text to insert
 
74
 * @position: position to insert the text at
 
75
 * @undoable: if this insertion gets added to the undo stack or not.
 
76
 * 
 
77
 *
 
78
 **/
 
79
void
 
80
gedit_document_insert_text (GeditDocument *doc, const guchar *text, guint position, gint undoable)
 
81
{
 
82
        GeditView *view;
 
83
        GtkText *text_widget;
 
84
        GtkWidget *editable;
 
85
        gint length;
 
86
        gint exclude_this_view;
 
87
 
 
88
        if (doc->readonly)
 
89
             return;
 
90
        g_return_if_fail (text!=NULL);
 
91
        view = g_list_nth_data (doc->views, 0);
 
92
        g_return_if_fail (view!=NULL);
 
93
        text_widget = GTK_TEXT (view->text);
 
94
        g_return_if_fail (text_widget!=NULL);
 
95
        editable = GTK_WIDGET (text_widget);
 
96
        g_return_if_fail (editable!=NULL);
 
97
        length = strlen (text);
 
98
        exclude_this_view = FALSE;
 
99
 
 
100
        doc_insert_text_real_cb (editable, text, length, &position, view, exclude_this_view, undoable);
 
101
}
 
102
 
 
103
/**
 
104
 * gedit_document_delete_text: Deletes text from a document and hadles all the details like
 
105
 *                             adding the data to the undo stack and deleting it from multiple views
 
106
 *                             Provides an abstraction layer between gedit and the text widget.
 
107
 * @doc: document to delete text from
 
108
 * @position: position to delete the text from
 
109
 * @length: length of the text to delete
 
110
 * @undoable: if this deletion gets added to the undo stack or not.
 
111
 * 
 
112
 *
 
113
 **/
 
114
void
 
115
gedit_document_delete_text (GeditDocument *doc, guint position, gint length, gint undoable)
 
116
{
 
117
        GeditView *view;
 
118
        GtkText *text_widget;
 
119
        GtkWidget *editable;
 
120
        gint exclude_this_view;
 
121
 
 
122
        if (doc->readonly)
 
123
             return;
 
124
 
 
125
        view = g_list_nth_data (doc->views, 0);
 
126
        g_return_if_fail (view!=NULL);
 
127
        text_widget = GTK_TEXT (view->text);
 
128
        g_return_if_fail (text_widget!=NULL);
 
129
        editable = GTK_WIDGET (text_widget);
 
130
        g_return_if_fail (editable!=NULL);
 
131
        exclude_this_view = FALSE;
 
132
 
 
133
        doc_delete_text_real_cb (editable, position, position+length, view, exclude_this_view, undoable);
 
134
}
 
135
 
 
136
/**
 
137
 * gedit_document_replace_text:
 
138
 * @doc: 
 
139
 * @text: 
 
140
 * @position: 
 
141
 * @length: 
 
142
 * @undoable: 
 
143
 * 
 
144
 * is equivalent to delete_text + insert_text
 
145
 **/
 
146
void
 
147
gedit_document_replace_text (GeditDocument *doc, const guchar * text_to_insert,
 
148
                             gint length, guint position, gint undoable)
 
149
{
 
150
        gchar *text_to_delete;
 
151
 
 
152
        gedit_debug (DEBUG_DOCUMENT, "");
 
153
 
 
154
        g_return_if_fail (doc != NULL);
 
155
        g_return_if_fail (text_to_insert != NULL);
 
156
 
 
157
        text_to_delete = gedit_document_get_chars (doc, position, position + length);
 
158
 
 
159
        g_return_if_fail (text_to_delete != NULL);
 
160
 
 
161
        if (undoable)
 
162
        {
 
163
                gint   text_to_delete_length;
 
164
                gint   text_to_insert_length;
 
165
 
 
166
                text_to_delete_length = strlen (text_to_delete);
 
167
                text_to_insert_length = strlen (text_to_insert);
 
168
 
 
169
                gedit_undo_add (text_to_delete,
 
170
                                position,
 
171
                                position + length,
 
172
                                GEDIT_UNDO_ACTION_REPLACE_DELETE,
 
173
                                doc,
 
174
                                NULL);
 
175
 
 
176
                gedit_undo_add (text_to_insert,
 
177
                                position,
 
178
                                position + text_to_insert_length,
 
179
                                GEDIT_UNDO_ACTION_REPLACE_INSERT,
 
180
                                doc,
 
181
                                NULL);
 
182
        }
 
183
 
 
184
        gedit_document_delete_text (doc, position, length, FALSE);
 
185
        gedit_document_insert_text (doc, text_to_insert, position, FALSE);
 
186
 
 
187
        
 
188
        g_free (text_to_delete);
 
189
}
 
190
 
 
191
 
 
192
void
 
193
gedit_document_set_readonly (GeditDocument *doc, gint readonly)
 
194
{
 
195
        GeditView * nth_view;
 
196
        gint n;
 
197
        
 
198
        gedit_debug (DEBUG_DOCUMENT, "");
 
199
 
 
200
        g_return_if_fail(doc != NULL);
 
201
        
 
202
        doc->readonly = readonly;
 
203
        doc->changed = FALSE;
 
204
                
 
205
        for (n = 0; n < g_list_length (doc->views); n++)
 
206
        {
 
207
                nth_view = g_list_nth_data (doc->views, n);
 
208
                gedit_view_set_readonly (nth_view, doc->readonly);
 
209
        }
 
210
 
 
211
}
 
212
 
 
213
void
 
214
gedit_document_text_changed_signal_connect (GeditDocument *doc)
 
215
{
 
216
        GeditView *nth_view;
 
217
        gint n;
 
218
        
 
219
        for (n = 0; n < g_list_length (doc->views); n++)
 
220
        {
 
221
                nth_view = g_list_nth_data (doc->views, n);
 
222
 
 
223
                g_return_if_fail (nth_view != NULL);
 
224
 
 
225
                nth_view->view_text_changed_signal =
 
226
                        gtk_signal_connect (GTK_OBJECT(nth_view->text), "changed",
 
227
                                            GTK_SIGNAL_FUNC (gedit_view_text_changed_cb), nth_view);
 
228
        }  
 
229
 
 
230
}
 
231
 
 
232
 
 
233
 
 
234
/**
 
235
 * gedit_document_get_tab_name:
 
236
 * @doc: 
 
237
 * 
 
238
 * determines the correct tab name for doc and assings a untitled
 
239
 * number if necessary
 
240
 *
 
241
 * Return Value: a potiner to a newly allocated string 
 
242
 **/
 
243
gchar*
 
244
gedit_document_get_tab_name (GeditDocument *doc, gboolean star)
 
245
{
 
246
        GeditDocument *nth_doc;
 
247
        int max_number = 0;
 
248
        int i;
 
249
 
 
250
        gedit_debug (DEBUG_DOCUMENT, "");
 
251
        
 
252
        g_return_val_if_fail (doc != NULL, g_strdup ("?"));
 
253
        
 
254
        if (doc->filename != NULL)
 
255
        {
 
256
                gchar * tab_name = NULL;
 
257
                gchar * unescaped_str = gnome_vfs_unescape_string_for_display (doc->filename);
 
258
 
 
259
                if(unescaped_str == NULL) 
 
260
                {
 
261
                        g_warning ("unescaped_str == NULL in gedit_document_get_tab_name ()");
 
262
                        unescaped_str = g_strdup (doc->filename);
 
263
                }
 
264
                
 
265
                if (!doc->changed || !star)
 
266
                {
 
267
                        tab_name = g_strdup_printf ("%s%s", doc->readonly?_("RO - "):"", g_basename(unescaped_str));
 
268
                }
 
269
                else
 
270
                {                       
 
271
                        tab_name = g_strdup_printf ("%s%s*", doc->readonly?_("RO - "):"", g_basename(unescaped_str));
 
272
                }
 
273
 
 
274
                g_free (unescaped_str);
 
275
                
 
276
                return tab_name;
 
277
        }
 
278
        else
 
279
        {
 
280
                if (doc->untitled_number == 0)
 
281
                {
 
282
                        for (i = 0; i < g_list_length (mdi->children); i++)
 
283
                        {
 
284
                                nth_doc = (GeditDocument *)g_list_nth_data (mdi->children, i);
 
285
                                
 
286
                                if (nth_doc->untitled_number > max_number)
 
287
                                {
 
288
                                        max_number = nth_doc->untitled_number;
 
289
                                }
 
290
                        }
 
291
                        doc->untitled_number = max_number + 1;
 
292
                }
 
293
                if (!doc->changed || !star)
 
294
                {
 
295
                        return _(g_strdup_printf ("%s %d", _("Untitled"), doc->untitled_number));
 
296
                }
 
297
                else
 
298
                {
 
299
                        return _(g_strdup_printf ("%s %d*", _("Untitled"), doc->untitled_number));
 
300
                }
 
301
        }
 
302
}
 
303
 
 
304
guchar *
 
305
gedit_document_get_chars (GeditDocument *doc, guint start_pos, guint end_pos)
 
306
{
 
307
        guchar * buffer;
 
308
        GtkText * text;
 
309
        GeditView * view;
 
310
 
 
311
        gedit_debug (DEBUG_DOCUMENT, "");
 
312
 
 
313
        g_return_val_if_fail (doc!=NULL, NULL);
 
314
        g_return_val_if_fail (end_pos > start_pos, NULL);
 
315
        view = g_list_nth_data (doc->views, 0);
 
316
        g_return_val_if_fail (view!=NULL, NULL);
 
317
 
 
318
        text = GTK_TEXT (view->text);
 
319
 
 
320
        buffer = gtk_editable_get_chars ( GTK_EDITABLE ( text ),
 
321
                                          start_pos,
 
322
                                          end_pos);
 
323
        return buffer;
 
324
}
 
325
 
 
326
/**
 
327
 * gedit_document_get_buffer:
 
328
 * @doc: 
 
329
 * 
 
330
 * returns a newly allocated buffer containg the text inside doc
 
331
 * 
 
332
 * Return Value: 
 
333
 **/
 
334
guchar *
 
335
gedit_document_get_buffer (GeditDocument *doc)
 
336
{
 
337
        guchar * buffer;
 
338
        guint length;
 
339
        GtkText * text;
 
340
        GeditView * view;
 
341
 
 
342
        gedit_debug (DEBUG_DOCUMENT, "");
 
343
 
 
344
        g_return_val_if_fail (doc!=NULL, NULL);
 
345
        view = g_list_nth_data (doc->views, 0);
 
346
        g_return_val_if_fail (view!=NULL, NULL);
 
347
 
 
348
        text = GTK_TEXT (view->text);
 
349
 
 
350
        length = gtk_text_get_length (text);
 
351
        buffer = gtk_editable_get_chars ( GTK_EDITABLE ( text ),
 
352
                                          0,
 
353
                                          length);
 
354
        return buffer;
 
355
}
 
356
 
 
357
guint
 
358
gedit_document_get_buffer_length (GeditDocument *doc)
 
359
{
 
360
        guint length;
 
361
        GeditView * view;
 
362
 
 
363
        gedit_debug (DEBUG_DOCUMENT, "");
 
364
 
 
365
        g_return_val_if_fail (doc!=NULL, 0);
 
366
        view = g_list_nth_data (doc->views, 0);
 
367
        g_return_val_if_fail (view!=NULL, 0);
 
368
 
 
369
        length = gtk_text_get_length (GTK_TEXT (view->text));
 
370
        return length;
 
371
}
 
372
 
 
373
 
 
374
GeditDocument *
 
375
gedit_document_new (void)
 
376
{
 
377
        GeditDocument *doc;
 
378
 
 
379
        gedit_debug (DEBUG_DOCUMENT, "");
 
380
 
 
381
        doc = gtk_type_new (gedit_document_get_type ());
 
382
 
 
383
        g_return_val_if_fail (doc != NULL, NULL);
 
384
 
 
385
        gnome_mdi_add_child (mdi, GNOME_MDI_CHILD (doc));
 
386
        gnome_mdi_add_view (mdi, GNOME_MDI_CHILD (doc));
 
387
 
 
388
        gedit_window_set_widgets_sensitivity (TRUE);
 
389
        gedit_window_set_toolbar_labels (GEDIT_VIEW(doc->views->data)->app);
 
390
 
 
391
        return doc;
 
392
}
 
393
 
 
394
GeditDocument *
 
395
gedit_document_new_with_title (const gchar *title)
 
396
{
 
397
        GeditDocument *doc;
 
398
        
 
399
        gedit_debug (DEBUG_DOCUMENT, "");
 
400
 
 
401
        g_return_val_if_fail (title != NULL, NULL);
 
402
        doc = gtk_type_new (gedit_document_get_type ());
 
403
        g_return_val_if_fail (doc != NULL, NULL);
 
404
 
 
405
        doc->filename = g_strdup (title);
 
406
        
 
407
        gnome_mdi_add_child (mdi, GNOME_MDI_CHILD (doc));
 
408
        gnome_mdi_add_view (mdi, GNOME_MDI_CHILD (doc));
 
409
 
 
410
        gedit_window_set_widgets_sensitivity (TRUE);
 
411
 
 
412
        return doc;
 
413
}
 
414
 
 
415
gint
 
416
gedit_document_new_with_file (const gchar *file_name)
 
417
{
 
418
        GeditDocument *doc;
 
419
 
 
420
        gedit_debug (DEBUG_DOCUMENT, "");
 
421
 
 
422
        doc = gedit_document_current();
 
423
 
 
424
        if (doc!=NULL) 
 
425
                if (doc->changed || doc->filename)
 
426
                        doc = NULL;
 
427
 
 
428
        if (gedit_file_open (doc, file_name) == 1)
 
429
                return FALSE;
 
430
        
 
431
        gedit_window_set_widgets_sensitivity (TRUE);
 
432
 
 
433
        return TRUE;
 
434
 
 
435
}
 
436
 
 
437
GeditDocument *
 
438
gedit_document_current (void)
 
439
{
 
440
        GeditDocument *current_document = NULL;
 
441
 
 
442
        if (mdi->active_child)
 
443
                current_document = GEDIT_DOCUMENT (mdi->active_child);
 
444
 
 
445
        return current_document;
 
446
}
 
447
 
 
448
static gchar *
 
449
gedit_document_get_config_string (GnomeMDIChild *child)
 
450
{
 
451
        gedit_debug (DEBUG_DOCUMENT, "");
 
452
        return g_strdup_printf ("%d", GPOINTER_TO_INT (gtk_object_get_user_data (GTK_OBJECT (child))));
 
453
}
 
454
 
 
455
static gint
 
456
remove_child_cb (GnomeMDI *mdi, GeditDocument *doc)
 
457
{
 
458
        gedit_debug (DEBUG_DOCUMENT, "start");
 
459
        
 
460
        if (!gedit_view_active())
 
461
        {
 
462
                gedit_debug (DEBUG_DOCUMENT, "returning since views are NULL");
 
463
                return TRUE;
 
464
        }
 
465
 
 
466
        if (doc->changed)
 
467
        {
 
468
                GtkWidget *msgbox, *w;
 
469
                gchar *fname = NULL, *msg = NULL;
 
470
                gint ret;
 
471
 
 
472
                w = GTK_WIDGET (g_list_nth_data(doc->views, 0));
 
473
                        
 
474
                if(w != NULL)
 
475
                        gnome_mdi_set_active_view (mdi, w);
 
476
 
 
477
                fname = gedit_document_get_tab_name (doc, FALSE);
 
478
 
 
479
                msg = g_strdup_printf (_("``%s'' has been modified.  Do you wish to save it?"),
 
480
                                       fname);
 
481
                
 
482
                msgbox = gnome_message_box_new (msg,
 
483
                                                GNOME_MESSAGE_BOX_QUESTION,
 
484
                                                GNOME_STOCK_BUTTON_YES,
 
485
                                                GNOME_STOCK_BUTTON_NO,
 
486
                                                GNOME_STOCK_BUTTON_CANCEL,
 
487
                                                NULL);
 
488
                gnome_dialog_set_default (GNOME_DIALOG (msgbox), 2);
 
489
                ret = gnome_dialog_run_and_close (GNOME_DIALOG (msgbox));
 
490
 
 
491
                g_free (fname);
 
492
                g_free (msg);
 
493
                
 
494
                switch (ret)
 
495
                {
 
496
                case 0:
 
497
                        return file_save_document (doc);
 
498
                case 1:
 
499
                        return TRUE;
 
500
                default:
 
501
                        gedit_close_all_flag_clear();
 
502
                        return FALSE;
 
503
                }
 
504
        }
 
505
        
 
506
        gedit_debug (DEBUG_DOCUMENT, "end");
 
507
        
 
508
        return TRUE;
 
509
}
 
510
 
 
511
static GtkWidget *
 
512
gedit_document_create_view (GnomeMDIChild *child)
 
513
{
 
514
        GeditView  *new_view;
 
515
 
 
516
        gedit_debug (DEBUG_DOCUMENT, "");
 
517
 
 
518
        g_return_val_if_fail (child != NULL, NULL);
 
519
        g_return_val_if_fail (GNOME_IS_MDI_CHILD (child), NULL);
 
520
 
 
521
        new_view = GEDIT_VIEW (gedit_view_new (GEDIT_DOCUMENT (child)));
 
522
 
 
523
        gedit_view_set_font (new_view, settings->font);
 
524
        gedit_view_set_readonly (new_view, GEDIT_DOCUMENT (child)->readonly);
 
525
 
 
526
        return GTK_WIDGET (new_view);
 
527
}
 
528
 
 
529
static void
 
530
gedit_document_destroy (GtkObject *obj)
 
531
{
 
532
        GeditDocument *doc = GEDIT_DOCUMENT (obj);
 
533
 
 
534
        gedit_debug (DEBUG_DOCUMENT, "");
 
535
 
 
536
        g_list_free (doc->views);
 
537
        g_free (doc->filename);
 
538
        gedit_undo_free_list (&doc->undo);
 
539
        gedit_undo_free_list (&doc->redo);
 
540
        
 
541
        if (GTK_OBJECT_CLASS (parent_class)->destroy)
 
542
                (* GTK_OBJECT_CLASS (parent_class)->destroy)(GTK_OBJECT (doc));
 
543
 
 
544
}
 
545
 
 
546
static void
 
547
gedit_document_class_init (GeditDocumentClass *class)
 
548
{
 
549
        GtkObjectClass          *object_class;
 
550
        GnomeMDIChildClass      *child_class;
 
551
        
 
552
        object_class = (GtkObjectClass*)class;
 
553
        child_class = GNOME_MDI_CHILD_CLASS (class);
 
554
 
 
555
        gedit_debug (DEBUG_DOCUMENT, "");
 
556
        
 
557
        object_class->destroy = gedit_document_destroy;
 
558
        
 
559
        child_class->create_view = (GnomeMDIChildViewCreator)(gedit_document_create_view);
 
560
        child_class->get_config_string = (GnomeMDIChildConfigFunc)(gedit_document_get_config_string);
 
561
 
 
562
        parent_class = gtk_type_class (gnome_mdi_child_get_type ());
 
563
 
 
564
}
 
565
 
 
566
static void
 
567
gedit_document_init (GeditDocument *doc)
 
568
{
 
569
        gedit_debug (DEBUG_DOCUMENT, "");
 
570
 
 
571
        doc->filename = NULL;
 
572
        doc->changed = FALSE;
 
573
        doc->views = NULL;
 
574
        doc->undo = NULL;
 
575
        doc->redo = NULL;
 
576
                
 
577
        gnome_mdi_child_set_menu_template (GNOME_MDI_CHILD (doc), doc_menu);
 
578
}
 
579
 
 
580
GtkType
 
581
gedit_document_get_type (void)
 
582
{
 
583
        static GtkType doc_type = 0;
 
584
        
 
585
        if (!doc_type)
 
586
        {
 
587
                static const GtkTypeInfo doc_info =
 
588
                {
 
589
                        "Document",
 
590
                        sizeof (GeditDocument),
 
591
                        sizeof (GeditDocumentClass),
 
592
                        (GtkClassInitFunc) gedit_document_class_init,
 
593
                        (GtkObjectInitFunc) gedit_document_init,
 
594
                        (GtkArgSetFunc) NULL,
 
595
                        (GtkArgGetFunc) NULL,
 
596
                };
 
597
          
 
598
                doc_type = gtk_type_unique (gnome_mdi_child_get_type (),
 
599
                                            &doc_info);
 
600
        }
 
601
          
 
602
        return doc_type;
 
603
}
 
604
 
 
605
 
 
606
void
 
607
gedit_mdi_init (void)
 
608
{
 
609
        gedit_debug (DEBUG_DOCUMENT, "start");
 
610
 
 
611
        /*
 
612
        mdi = GNOME_MDI (gnome_mdi_new ("gedit", "gedit "VESION));
 
613
        */
 
614
        mdi = GNOME_MDI (gnome_mdi_new ("gedit", "gedit "));
 
615
 
 
616
        mdi->tab_pos = settings->tab_pos;
 
617
 
 
618
        gnome_mdi_set_menubar_template (mdi, gedit_menu);
 
619
        gnome_mdi_set_toolbar_template (mdi, toolbar_data);
 
620
        
 
621
        gnome_mdi_set_child_menu_path (mdi, GNOME_MENU_FILE_STRING);
 
622
        gnome_mdi_set_child_list_path (mdi, D_("_Documents/"));
 
623
 
 
624
        /* connect signals */
 
625
        gtk_signal_connect (GTK_OBJECT (mdi), "remove_child",
 
626
                            GTK_SIGNAL_FUNC (remove_child_cb), NULL);
 
627
        gedit_mdi_destroy_signal =
 
628
                gtk_signal_connect (GTK_OBJECT (mdi), "destroy",
 
629
                                    GTK_SIGNAL_FUNC (file_quit_cb), NULL);
 
630
        gtk_signal_connect (GTK_OBJECT (mdi), "view_changed",
 
631
                            GTK_SIGNAL_FUNC (gedit_view_changed_cb), NULL);
 
632
        gtk_signal_connect (GTK_OBJECT (mdi), "app_created",
 
633
                            GTK_SIGNAL_FUNC (gedit_window_new), NULL);
 
634
 
 
635
        gnome_mdi_set_mode (mdi, settings->mdi_mode);
 
636
        gnome_mdi_open_toplevel (mdi);
 
637
 
 
638
        /* Loads the structure gedit_toolbar with the widgets */
 
639
        gedit_window_set_toolbar_labels (mdi->active_window);
 
640
        gedit_window_set_view_menu_sensitivity (mdi->active_window);
 
641
 
 
642
        gedit_debug (DEBUG_DOCUMENT, "end");
 
643
}
 
644
 
 
645
 
 
646
gboolean
 
647
gedit_document_load (GList *file_list)
 
648
{
 
649
        gchar *file_name;
 
650
        gboolean can_be_created;
 
651
        
 
652
        gedit_debug (DEBUG_DOCUMENT, "");
 
653
 
 
654
        gedit_file_stdin (NULL);
 
655
 
 
656
        /* create a file for each document in the parameter list */
 
657
        for (;file_list; file_list = file_list->next)
 
658
        {
 
659
                
 
660
                const gchar* scheme; 
 
661
                GnomeVFSURI *uri = gnome_vfs_uri_new (file_list->data);
 
662
 
 
663
                if(uri == NULL) 
 
664
                {
 
665
                        g_print("Wrong URI: %s\n", (gchar*)file_list->data);
 
666
                        continue;
 
667
                }
 
668
 
 
669
                scheme = gnome_vfs_uri_get_scheme (uri);
 
670
 
 
671
                if ((scheme != NULL) && (strcmp (scheme, "file") == 0))
 
672
                {
 
673
                        gchar* tmp_str;
 
674
                        gchar* tmp_str2;
 
675
                        
 
676
                        can_be_created = TRUE;
 
677
                                                
 
678
                        tmp_str = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_TOPLEVEL_METHOD);                            
 
679
                        tmp_str2 = gnome_vfs_unescape_string_for_display (tmp_str);
 
680
 
 
681
                        file_name = gedit_file_convert_to_full_pathname (tmp_str);                      
 
682
                        
 
683
                        g_free (tmp_str);
 
684
                        g_free (tmp_str2);
 
685
 
 
686
                        gnome_vfs_uri_unref (uri);      
 
687
                        uri = gnome_vfs_uri_new (file_name);
 
688
                }
 
689
                else
 
690
                {
 
691
                        /* FIXME: hide password */
 
692
                        can_be_created = FALSE;                 
 
693
                        file_name = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);             
 
694
                }
 
695
 
 
696
                
 
697
                if (gnome_vfs_uri_exists (uri))
 
698
                {
 
699
                        gedit_document_new_with_file ((gchar *) file_name);
 
700
                }
 
701
                else    
 
702
                {                                                       
 
703
                        if (can_be_created)
 
704
                        {       
 
705
                                gedit_file_create_popup ((guchar *) file_name);
 
706
                        }
 
707
                        else
 
708
                        {       
 
709
                                /* If you try to open a non existant remote file */
 
710
                                gchar *errstr = g_strdup_printf (_("An error was encountered while opening the file: \n\n%s\n\n"
 
711
                                                        "Please make sure the file exists."), file_name);
 
712
        
 
713
                                gnome_app_warning (gedit_window_active_app(), errstr);
 
714
                                g_free (errstr);                                
 
715
                        }
 
716
                }
 
717
 
 
718
                g_free (file_name);
 
719
                gnome_vfs_uri_unref(uri);               
 
720
        }
 
721
 
 
722
        if (gedit_document_current() == NULL)
 
723
                gedit_document_new ();
 
724
        
 
725
        g_return_val_if_fail(gedit_window_active_app() != NULL, FALSE);
 
726
        g_return_val_if_fail(gedit_document_current() != NULL, FALSE);
 
727
 
 
728
        gedit_window_set_widgets_sensitivity_ro (gedit_window_active_app(), gedit_document_current()->readonly);
 
729
        
 
730
        return FALSE;
 
731
}
 
732
 
 
733
/**
 
734
 * gedit_set_title:
 
735
 * @docname: Document name in a string, the new title
 
736
 *
 
737
 * Set the title to "$docname - $gedit_ver" and if the document has
 
738
 * changed, lets show that it has. 
 
739
 **/
 
740
void
 
741
gedit_document_set_title (GeditDocument *doc)
 
742
{
 
743
        gchar *title;
 
744
        gchar *docname;
 
745
 
 
746
        gedit_debug (DEBUG_DOCUMENT, "");
 
747
 
 
748
        
 
749
        if (doc == NULL)
 
750
                return;
 
751
        
 
752
        if (doc->filename == NULL) {
 
753
                docname = g_strdup_printf (_("Untitled %i"), doc->untitled_number);
 
754
        } else {
 
755
#if 0
 
756
                docname = g_strdup (g_basename (doc->filename));
 
757
#endif
 
758
                docname = gnome_vfs_unescape_string_for_display (doc->filename);                
 
759
        }
 
760
 
 
761
#if 0
 
762
        if (doc->changed) {
 
763
                title = g_strdup_printf ("gedit: %s %s", docname, _("(modified)"));
 
764
        } else if (doc->readonly) {
 
765
                title = g_strdup_printf ("gedit: %s %s", docname, _("(readonly)"));
 
766
        } else {
 
767
                title = g_strdup_printf ("gedit: %s", docname);
 
768
        }
 
769
#endif
 
770
        if (doc->changed) {
 
771
                title = g_strdup_printf ("gedit - [%s] %s", docname, _("(modified)"));
 
772
        } else if (doc->readonly) {
 
773
                title = g_strdup_printf ("gedit - [%s] %s", docname, _("(readonly)"));
 
774
        } else {
 
775
                title = g_strdup_printf ("gedit - [%s]", docname);
 
776
        }
 
777
 
 
778
 
 
779
        gtk_window_set_title (gedit_window_active(), title);
 
780
        
 
781
        g_free (docname);
 
782
        
 
783
        g_free (title);
 
784
 
 
785
        gedit_debug (DEBUG_DOCUMENT, "end");
 
786
}
 
787
 
 
788
 
 
789
/**
 
790
 * gedit_document_swap_hc_cb:
 
791
 * 
 
792
 * if .c file is open, then open the related .h file and v.v.
 
793
 *
 
794
 * TODO: if a .h file is open, do we swap to a .c or a .cpp?  we
 
795
 * should put a check in there.  if both exist, then probably open
 
796
 * both files.
 
797
 **/
 
798
void
 
799
gedit_document_swap_hc_cb (GtkWidget *widget, gpointer data)
 
800
{
 
801
        size_t len;
 
802
        gchar *new_file_name;
 
803
        GeditDocument *doc, *nth_doc;
 
804
        gint n;
 
805
 
 
806
        gedit_debug (DEBUG_DOCUMENT, "");
 
807
        
 
808
        doc = gedit_document_current();
 
809
        if (!doc || !doc->filename)
 
810
                return;
 
811
 
 
812
        new_file_name = NULL;
 
813
        len = strlen (doc->filename);
 
814
 
 
815
        while (len)
 
816
        {
 
817
                if (doc->filename[len] == '.')
 
818
                        break;
 
819
                len--;
 
820
        }
 
821
 
 
822
        if (len == 0)
 
823
        {
 
824
                gchar *errstr = g_strdup (_("This file does not end with a valid extension\n"));
 
825
                gnome_app_error (gedit_window_active_app(), errstr);
 
826
                g_free (errstr);
 
827
                return;
 
828
        }
 
829
 
 
830
        len++;
 
831
        if (doc->filename[len] == 'h')
 
832
        {
 
833
                new_file_name = g_strdup (doc->filename);
 
834
                new_file_name[len] = 'c';
 
835
        }
 
836
        else if (doc->filename[len] == 'H')
 
837
        {
 
838
                new_file_name = g_strdup (doc->filename);
 
839
                new_file_name[len] = 'C';
 
840
        }
 
841
        else if (doc->filename[len] == 'c')
 
842
        {
 
843
                new_file_name = g_strdup (doc->filename);
 
844
                new_file_name[len] = 'h';
 
845
 
 
846
                if (len < strlen(doc->filename) && strcmp(doc->filename + len, "cpp") == 0)
 
847
                        new_file_name[len+1] = '\0';
 
848
        }
 
849
        else if (doc->filename[len] == 'C')
 
850
        {
 
851
                new_file_name = g_strdup (doc->filename);
 
852
 
 
853
                if (len < strlen(doc->filename) && strcmp(doc->filename + len, "CPP") == 0)
 
854
                {
 
855
                        new_file_name[len] = 'H';
 
856
                        new_file_name[len+1] = '\0';
 
857
                }
 
858
                else
 
859
                        new_file_name[len] = 'H';
 
860
        }
 
861
 
 
862
        if (!new_file_name)
 
863
        {
 
864
                gchar *errstr = g_strdup (_("This file does not end with a valid extension\n"));
 
865
                gnome_app_error (gedit_window_active_app(), errstr);
 
866
                g_free (errstr);
 
867
                return;
 
868
        }
 
869
 
 
870
        if (!g_file_exists (new_file_name))
 
871
        {
 
872
                gchar *errstr = g_strdup_printf (_("The file %s was not found."), new_file_name);
 
873
                gnome_app_error (gedit_window_active_app(), errstr);
 
874
                g_free (errstr);
 
875
                return;
 
876
        }
 
877
 
 
878
        /* Scan the documents to see if the file we are looking for is allready open */
 
879
        for (n = 0; n < g_list_length (mdi->children); n++)
 
880
        {
 
881
                nth_doc = (GeditDocument *)g_list_nth_data (mdi->children, n);
 
882
                
 
883
                if (strcmp(nth_doc->filename, new_file_name) == 0)
 
884
                {
 
885
                        GeditView *view;
 
886
                        view = g_list_nth_data (nth_doc->views, 0);
 
887
                        g_return_if_fail (view != NULL);
 
888
                        gnome_mdi_set_active_view (mdi, GTK_WIDGET (view));
 
889
                        return;
 
890
                }
 
891
                
 
892
        }
 
893
        
 
894
        gedit_document_new_with_file (new_file_name);
 
895
}
 
896
 
 
897
void
 
898
gedit_document_set_undo (GeditDocument *doc, gint undo_state, gint redo_state)
 
899
{
 
900
        GeditView *nth_view;
 
901
        gint n;
 
902
        
 
903
        gedit_debug (DEBUG_DOCUMENT, "");
 
904
        
 
905
        g_return_if_fail (GEDIT_IS_DOCUMENT(doc));
 
906
 
 
907
        for ( n=0; n < g_list_length (doc->views); n++)
 
908
        {
 
909
                nth_view = GEDIT_VIEW (g_list_nth_data (doc->views, n));
 
910
                gedit_view_set_undo (nth_view, undo_state, redo_state);
 
911
        }
 
912
        
 
913
}
 
914