1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) Johannes Schmid 2008 <jhs@gnome.org>
6 * anjuta-trunk is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * anjuta-trunk is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "anjuta-bookmarks.h"
23 #include "anjuta-docman.h"
24 #include <libanjuta/interfaces/ianjuta-markable.h>
25 #include <libanjuta/interfaces/ianjuta-file.h>
26 #include <libanjuta/interfaces/ianjuta-editor-selection.h>
27 #include <libanjuta/interfaces/ianjuta-symbol-manager.h>
28 #include <libanjuta/anjuta-debug.h>
31 #include <libxml/encoding.h>
32 #include <libxml/xmlwriter.h>
33 #include <libxml/parser.h>
35 #define BOOKMARKS_GET_PRIVATE(o) \
36 (G_TYPE_INSTANCE_GET_PRIVATE ((o), ANJUTA_TYPE_BOOKMARKS, AnjutaBookmarksPrivate))
38 #define ANJUTA_STOCK_BOOKMARK_TOGGLE "anjuta-bookmark-toggle"
40 #define ENCODING "UTF-8"
43 typedef struct _AnjutaBookmarksPrivate AnjutaBookmarksPrivate
;
45 struct _AnjutaBookmarksPrivate
50 GtkCellRenderer
* renderer
;
51 GtkTreeViewColumn
* column
;
53 GtkWidget
* button_add
;
54 GtkWidget
* button_remove
;
57 IAnjutaSymbolQuery
* query_scope
;
71 G_DEFINE_TYPE (AnjutaBookmarks
, anjuta_bookmarks
, G_TYPE_OBJECT
);
74 anjuta_bookmarks_get_text (AnjutaBookmarks
* bookmarks
, IAnjutaEditor
* editor
, gint line
, gboolean use_selection
);
77 on_document_changed (AnjutaDocman
*docman
, IAnjutaDocument
*doc
,
78 AnjutaBookmarks
*bookmarks
)
80 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
81 gboolean status
= IANJUTA_IS_EDITOR(doc
);
82 gtk_widget_set_sensitive (GTK_WIDGET(priv
->button_add
), status
);
86 on_add_clicked (GtkWidget
* button
, AnjutaBookmarks
* bookmarks
)
88 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
89 IAnjutaDocument
* doc
=
90 anjuta_docman_get_current_document (ANJUTA_DOCMAN(priv
->docman
->docman
));
91 g_return_if_fail (IANJUTA_IS_EDITOR(doc
));
92 IAnjutaEditor
* editor
= IANJUTA_EDITOR(doc
);
93 anjuta_bookmarks_add (bookmarks
, editor
,
94 ianjuta_editor_get_lineno (editor
, NULL
), NULL
,TRUE
);
98 on_remove_clicked (GtkWidget
* button
, AnjutaBookmarks
* bookmarks
)
100 anjuta_bookmarks_remove (bookmarks
);
104 on_rename (GtkWidget
* menuitem
, AnjutaBookmarks
* bookmarks
)
106 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
107 GtkTreeSelection
* selection
=
108 gtk_tree_view_get_selection (GTK_TREE_VIEW (priv
->tree
));
111 gtk_tree_selection_get_selected_rows (selection
, NULL
);
115 // We shouldn't get here otherwise
116 g_assert (g_list_length(rows
) == 1);
118 g_object_set (G_OBJECT(priv
->renderer
), "editable", TRUE
, NULL
);
120 path
= rows
->data
; // First and only row
121 anjuta_shell_present_widget (ANJUTA_PLUGIN(priv
->docman
)->shell
,
123 gtk_widget_grab_focus (priv
->tree
);
124 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv
->tree
), path
,
125 priv
->column
, FALSE
, 0.0, 0.0);
127 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (priv
->tree
), path
,
131 g_list_foreach (rows
, (GFunc
)gtk_tree_path_free
, NULL
);
136 on_row_activate (GtkTreeView
* view
, GtkTreePath
* path
,
137 GtkTreeViewColumn
* column
, AnjutaBookmarks
* bookmarks
)
143 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
144 IAnjutaEditor
* editor
;
145 gtk_tree_model_get_iter (priv
->model
, &iter
, path
);
146 gtk_tree_model_get (priv
->model
, &iter
,
149 COLUMN_HANDLE
, &handle
,
152 editor
= anjuta_docman_goto_file_line (ANJUTA_DOCMAN(priv
->docman
->docman
), file
, line
);
155 line
= ianjuta_markable_location_from_handle (IANJUTA_MARKABLE (editor
),
159 gchar
* new_title
= anjuta_bookmarks_get_text (bookmarks
,
163 anjuta_docman_goto_file_line (ANJUTA_DOCMAN(priv
->docman
->docman
), file
, line
);
164 gtk_list_store_set (GTK_LIST_STORE (priv
->model
), &iter
,
165 COLUMN_TEXT
, new_title
,
166 COLUMN_LINE
, line
, -1);
170 g_object_unref (file
);
174 on_button_press_event (GtkWidget
*widget
, GdkEventButton
*event
, AnjutaBookmarks
* bookmarks
)
176 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
177 /* Ignore double-clicks and triple-clicks */
178 if (event
->button
== 3 && event
->type
== GDK_BUTTON_PRESS
)
183 GtkTreeSelection
* selection
=
184 gtk_tree_view_get_selection (GTK_TREE_VIEW (widget
));
186 if (gtk_tree_selection_count_selected_rows (selection
) == 1)
188 /*g_signal_connect (menu, "deactivate",
189 G_CALLBACK (gtk_widget_destroy), NULL);*/
193 button
= event
->button
;
194 event_time
= event
->time
;
199 event_time
= gtk_get_current_event_time ();
201 gtk_menu_popup (GTK_MENU(priv
->menu
), NULL
, NULL
, NULL
, NULL
, button
, event_time
);
211 on_document_added (AnjutaDocman
* docman
, IAnjutaDocument
* doc
,
212 AnjutaBookmarks
* bookmarks
)
214 IAnjutaMarkable
* markable
;
217 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
219 if (!IANJUTA_IS_MARKABLE(doc
))
222 markable
= IANJUTA_MARKABLE(doc
);
223 if (!gtk_tree_model_get_iter_first (priv
->model
, &iter
))
226 editor_file
= ianjuta_file_get_file (IANJUTA_FILE(doc
), NULL
);
227 if (editor_file
== NULL
)
235 gtk_tree_model_get (priv
->model
, &iter
,
239 if (g_file_equal (file
, editor_file
))
241 if (!ianjuta_markable_is_marker_set (markable
,
243 IANJUTA_MARKABLE_BOOKMARK
,
246 int handle
= ianjuta_markable_mark (markable
, line
,
247 IANJUTA_MARKABLE_BOOKMARK
,
249 gtk_list_store_set (GTK_LIST_STORE(priv
->model
),
255 g_object_unref (file
);
257 while (gtk_tree_model_iter_next (priv
->model
, &iter
));
258 g_object_unref (editor_file
);
262 on_selection_changed (GtkTreeSelection
* selection
, AnjutaBookmarks
* bookmarks
)
264 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
265 gboolean status
= (gtk_tree_selection_count_selected_rows (selection
) > 0);
266 gtk_widget_set_sensitive (priv
->button_remove
, status
);
270 on_title_edited (GtkCellRendererText
*cell
,
273 AnjutaBookmarks
* bookmarks
)
275 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
276 GtkTreeModel
*model
= priv
->model
;
278 GtkTreePath
*path
= gtk_tree_path_new_from_string (path_string
);
280 gtk_tree_model_get_iter (model
, &iter
, path
);
281 gtk_list_store_set (GTK_LIST_STORE (model
), &iter
, COLUMN_TEXT
, new_text
, -1);
283 g_object_set (G_OBJECT(priv
->renderer
), "editable", FALSE
, NULL
);
284 gtk_tree_path_free (path
);
288 anjuta_bookmarks_init (AnjutaBookmarks
*bookmarks
)
290 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
291 GtkWidget
* scrolled_window
;
292 GtkTreeSelection
* selection
;
294 GtkWidget
* item_rename
;
295 GtkWidget
* item_remove
;
297 priv
->window
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, 5);
298 scrolled_window
= gtk_scrolled_window_new (NULL
, NULL
);
299 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW(scrolled_window
),
300 GTK_SHADOW_ETCHED_IN
);
301 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window
),
302 GTK_POLICY_AUTOMATIC
,
303 GTK_POLICY_AUTOMATIC
);
304 gtk_box_pack_start (GTK_BOX (priv
->window
),
308 priv
->model
= GTK_TREE_MODEL(gtk_list_store_new (N_COLUMNS
, G_TYPE_STRING
,
309 G_TYPE_OBJECT
, G_TYPE_INT
, G_TYPE_INT
));
310 priv
->tree
= gtk_tree_view_new_with_model (priv
->model
);
311 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(priv
->tree
), FALSE
);
312 priv
->renderer
= gtk_cell_renderer_text_new();
313 g_signal_connect (G_OBJECT(priv
->renderer
), "edited", G_CALLBACK(on_title_edited
),
315 priv
->column
= gtk_tree_view_column_new_with_attributes ("Bookmark", priv
->renderer
,
316 "text", COLUMN_TEXT
, NULL
);
317 gtk_tree_view_append_column (GTK_TREE_VIEW(priv
->tree
), priv
->column
);
318 gtk_container_add (GTK_CONTAINER(scrolled_window
),
321 g_signal_connect (G_OBJECT(priv
->tree
), "row-activated", G_CALLBACK(on_row_activate
),
323 g_signal_connect (G_OBJECT(priv
->tree
), "button-press-event", G_CALLBACK(on_button_press_event
),
326 selection
= gtk_tree_view_get_selection (GTK_TREE_VIEW(priv
->tree
));
327 gtk_tree_selection_set_mode (selection
, GTK_SELECTION_BROWSE
);
328 g_signal_connect (G_OBJECT(selection
), "changed", G_CALLBACK(on_selection_changed
),
331 priv
->grip
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 0);
332 priv
->button_add
= gtk_button_new();
333 gtk_widget_set_tooltip_text (priv
->button_add
,
335 gtk_container_add (GTK_CONTAINER (priv
->button_add
),
336 gtk_image_new_from_stock (GTK_STOCK_ADD
,
337 GTK_ICON_SIZE_MENU
));
338 priv
->button_remove
= gtk_button_new();
339 gtk_widget_set_tooltip_text (priv
->button_remove
,
340 _("Remove bookmark"));
341 gtk_container_add (GTK_CONTAINER (priv
->button_remove
),
342 gtk_image_new_from_stock (GTK_STOCK_REMOVE
,
343 GTK_ICON_SIZE_MENU
));
344 g_signal_connect (G_OBJECT(priv
->button_add
), "clicked", G_CALLBACK(on_add_clicked
), bookmarks
);
345 g_signal_connect (G_OBJECT(priv
->button_remove
), "clicked", G_CALLBACK(on_remove_clicked
), bookmarks
);
346 gtk_widget_set_sensitive (GTK_WIDGET(priv
->button_add
), FALSE
);
347 gtk_widget_set_sensitive (GTK_WIDGET(priv
->button_remove
), FALSE
);
349 gtk_box_pack_start (GTK_BOX(priv
->grip
),
350 gtk_image_new_from_stock (ANJUTA_STOCK_BOOKMARK_TOGGLE
,
353 gtk_box_pack_start (GTK_BOX(priv
->grip
),
354 gtk_label_new (_("Bookmarks")),
356 gtk_box_pack_start (GTK_BOX(priv
->grip
), priv
->button_add
, FALSE
, FALSE
, 0);
357 gtk_box_pack_start (GTK_BOX(priv
->grip
), priv
->button_remove
, FALSE
, FALSE
, 0);
358 gtk_widget_show_all (priv
->grip
);
360 /* Create popup menu */
361 priv
->menu
= gtk_menu_new ();
362 item_rename
= gtk_menu_item_new_with_label (_("Rename"));
363 item_remove
= gtk_menu_item_new_with_label (_("Remove"));
365 g_signal_connect (item_rename
, "activate", G_CALLBACK(on_rename
), bookmarks
);
366 g_signal_connect (item_remove
, "activate", G_CALLBACK(on_remove_clicked
), bookmarks
);
368 gtk_menu_shell_append (GTK_MENU_SHELL(priv
->menu
), item_rename
);
369 gtk_menu_shell_append (GTK_MENU_SHELL(priv
->menu
), item_remove
);
371 gtk_widget_show_all (priv
->menu
);
372 gtk_menu_attach_to_widget (GTK_MENU (priv
->menu
), priv
->tree
, NULL
);
374 gtk_widget_show_all (priv
->window
);
378 anjuta_bookmarks_finalize (GObject
*object
)
380 AnjutaBookmarks
* bookmarks
= ANJUTA_BOOKMARKS (object
);
381 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
383 gtk_widget_destroy (priv
->menu
);
385 anjuta_shell_remove_widget (ANJUTA_PLUGIN(priv
->docman
)->shell
,
389 G_OBJECT_CLASS (anjuta_bookmarks_parent_class
)->finalize (object
);
393 anjuta_bookmarks_class_init (AnjutaBookmarksClass
*klass
)
395 GObjectClass
* object_class
= G_OBJECT_CLASS (klass
);
397 object_class
->finalize
= anjuta_bookmarks_finalize
;
399 g_type_class_add_private (klass
, sizeof (AnjutaBookmarksPrivate
));
404 anjuta_bookmarks_new (DocmanPlugin
* docman
)
406 AnjutaBookmarks
* bookmarks
= ANJUTA_BOOKMARKS (g_object_new (ANJUTA_TYPE_BOOKMARKS
,
408 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
409 priv
->docman
= docman
;
411 anjuta_shell_add_widget_custom (ANJUTA_PLUGIN(docman
)->shell
,
415 ANJUTA_STOCK_BOOKMARK_TOGGLE
,
417 ANJUTA_SHELL_PLACEMENT_RIGHT
,
420 g_signal_connect (G_OBJECT(docman
->docman
), "document-changed",
421 G_CALLBACK(on_document_changed
), bookmarks
);
422 g_signal_connect (G_OBJECT(docman
->docman
), "document-added",
423 G_CALLBACK(on_document_added
), bookmarks
);
427 static IAnjutaSymbolQuery
*
428 anjuta_bookmarks_create_query (AnjutaBookmarks
* bookmarks
)
430 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
431 IAnjutaSymbolManager
* sym_manager
;
432 IAnjutaSymbolQuery
* query_scope
= NULL
;
434 sym_manager
= anjuta_shell_get_interface (ANJUTA_PLUGIN(priv
->docman
)->shell
,
435 IAnjutaSymbolManager
, NULL
);
436 if (sym_manager
!= NULL
)
438 static IAnjutaSymbolField fields
[] = {IANJUTA_SYMBOL_FIELD_NAME
};
441 ianjuta_symbol_manager_create_query (sym_manager
,
442 IANJUTA_SYMBOL_QUERY_SEARCH_SCOPE
,
443 IANJUTA_SYMBOL_QUERY_DB_PROJECT
,
445 ianjuta_symbol_query_set_fields (query_scope
, 1, fields
, NULL
);
452 anjuta_bookmarks_get_text_from_file (AnjutaBookmarks
* bookmarks
, GFile
* file
, gint line
)
454 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
458 if (priv
->query_scope
!= NULL
)
460 gchar
* path
= g_file_get_path (file
);
461 IAnjutaIterable
* iter
=
462 ianjuta_symbol_query_search_scope (priv
->query_scope
,
467 const gchar
* symbol_name
;
468 symbol_name
= ianjuta_symbol_get_string (IANJUTA_SYMBOL(iter
),
469 IANJUTA_SYMBOL_FIELD_NAME
,
471 text
= g_strdup_printf ("%s:%d", symbol_name
,
473 g_object_unref (iter
);
479 /* As last chance, take file + line */
480 info
= g_file_query_info (file
,
481 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
,
482 G_FILE_QUERY_INFO_NONE
,
485 text
= g_strdup_printf ("%s:%d", g_file_info_get_display_name (info
),
487 g_object_unref (info
);
493 anjuta_bookmarks_get_text (AnjutaBookmarks
* bookmarks
, IAnjutaEditor
* editor
, gint line
, gboolean use_selection
)
495 /* If we have a (short) selection, take this */
496 if (IANJUTA_IS_EDITOR_SELECTION(editor
) && use_selection
)
498 IAnjutaEditorSelection
* selection
= IANJUTA_EDITOR_SELECTION(editor
);
499 if (ianjuta_editor_selection_has_selection (selection
, NULL
))
501 gchar
* text
= ianjuta_editor_selection_get (selection
, NULL
);
502 if (strlen (text
) < 100)
508 GFile
* file
= ianjuta_file_get_file (IANJUTA_FILE(editor
), NULL
);
509 gchar
* text
= anjuta_bookmarks_get_text_from_file (bookmarks
,
512 g_object_unref (file
);
518 anjuta_bookmarks_check_selection (AnjutaBookmarks
* bookmarks
,
521 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
522 GtkTreeSelection
* selection
= gtk_tree_view_get_selection (GTK_TREE_VIEW(priv
->tree
));
523 GList
* selected
= gtk_tree_selection_get_selected_rows (selection
,
528 if (!path
|| !gtk_tree_model_get_iter (priv
->model
, &iter
, path
))
530 if (!gtk_tree_model_get_iter_first (priv
->model
, &iter
))
533 gtk_tree_selection_select_iter (selection
, &iter
);
538 anjuta_bookmarks_add (AnjutaBookmarks
* bookmarks
, IAnjutaEditor
* editor
, gint line
,
539 const gchar
* title
, gboolean use_selection
)
541 g_return_if_fail (IANJUTA_IS_MARKABLE(editor
));
542 IAnjutaMarkable
* markable
= IANJUTA_MARKABLE(editor
);
546 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
549 /* If there is already a marker -> do nothing */
550 if (ianjuta_markable_is_marker_set (markable
, line
, IANJUTA_MARKABLE_BOOKMARK
, NULL
))
553 handle
= ianjuta_markable_mark (markable
, line
, IANJUTA_MARKABLE_BOOKMARK
, NULL
, NULL
);
555 gtk_list_store_append (GTK_LIST_STORE(priv
->model
), &iter
);
557 text
= anjuta_bookmarks_get_text (bookmarks
, editor
, line
, use_selection
);
559 text
= g_strdup(title
);
560 file
= ianjuta_file_get_file(IANJUTA_FILE(editor
), NULL
);
561 /* The buffer is not saved yet -> do nothing */
564 gtk_list_store_set (GTK_LIST_STORE(priv
->model
), &iter
,
568 COLUMN_HANDLE
, handle
,
571 g_object_unref (file
);
575 anjuta_bookmarks_remove (AnjutaBookmarks
* bookmarks
)
577 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
578 GtkTreeSelection
* selection
= gtk_tree_view_get_selection (GTK_TREE_VIEW(priv
->tree
));
579 GList
* selected
= gtk_tree_selection_get_selected_rows (selection
,
583 GtkTreePath
* first_path
= NULL
;
584 for (node
= selected
; node
!= NULL
; node
= g_list_next (node
))
586 GtkTreeRowReference
* ref
= gtk_tree_row_reference_new (priv
->model
,
588 refs
= g_list_append (refs
, ref
);
590 g_list_foreach (selected
, (GFunc
)gtk_tree_path_free
, NULL
);
591 g_list_free (selected
);
592 for (node
= refs
; node
!= NULL
; node
= g_list_next (node
))
596 IAnjutaEditor
* editor
;
598 GtkTreeRowReference
* ref
= node
->data
;
599 GtkTreePath
* path
= gtk_tree_row_reference_get_path(ref
);
600 gtk_tree_model_get_iter (priv
->model
,
609 gtk_tree_path_free (path
);
611 gtk_tree_model_get (priv
->model
, &iter
,
615 editor
= IANJUTA_EDITOR(anjuta_docman_get_document_for_file (ANJUTA_DOCMAN(priv
->docman
->docman
),
619 if (ianjuta_markable_is_marker_set (IANJUTA_MARKABLE(editor
),
620 line
, IANJUTA_MARKABLE_BOOKMARK
, NULL
))
622 ianjuta_markable_unmark (IANJUTA_MARKABLE(editor
), line
,
623 IANJUTA_MARKABLE_BOOKMARK
, NULL
);
626 g_object_unref (file
);
628 gtk_list_store_remove (GTK_LIST_STORE (priv
->model
), &iter
);
631 anjuta_bookmarks_check_selection (bookmarks
, first_path
);
633 gtk_tree_path_free (first_path
);
635 g_list_foreach (refs
, (GFunc
)gtk_tree_row_reference_free
, NULL
);
640 anjuta_bookmarks_add_file (AnjutaBookmarks
* bookmarks
,
645 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
646 IAnjutaDocument
* doc
;
649 if ((doc
= anjuta_docman_get_document_for_file (ANJUTA_DOCMAN(priv
->docman
->docman
), file
)))
651 anjuta_bookmarks_add (bookmarks
, IANJUTA_EDITOR(doc
), line
, title
, FALSE
);
656 gtk_list_store_append (GTK_LIST_STORE(priv
->model
), &iter
);
658 text
= anjuta_bookmarks_get_text_from_file (bookmarks
, file
, line
);
660 text
= g_strdup(title
);
661 gtk_list_store_set (GTK_LIST_STORE(priv
->model
), &iter
,
672 anjuta_bookmarks_session_save (AnjutaBookmarks
* bookmarks
, AnjutaSession
* session
)
676 xmlTextWriterPtr writer
;
680 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
682 /* Create a new XML buffer, to which the XML document will be
684 buf
= xmlBufferCreate();
687 DEBUG_PRINT ("%s", "XmlwriterMemory: Error creating the xml buffer\n");
691 /* Create a new XmlWriter for memory, with no compression.
692 * Remark: there is no compression for this kind of xmlTextWriter */
693 writer
= xmlNewTextWriterMemory(buf
, 0);
696 DEBUG_PRINT ("%s", "XmlwriterMemory: Error creating the xml writer\n");
700 rc
= xmlTextWriterStartDocument(writer
, NULL
, ENCODING
, NULL
);
703 "XmlwriterMemory: Error at xmlTextWriterStartDocument\n");
706 rc
= xmlTextWriterStartElement(writer
, BAD_CAST
"bookmarks");
709 "XmlwriterMemory: Error at xmlTextWriterStartElement\n");
713 if (gtk_tree_model_get_iter_first (priv
->model
,
724 gtk_tree_model_get (priv
->model
,
730 uri
= g_file_get_uri (file
);
731 g_object_unref (file
);
733 rc
= xmlTextWriterStartElement(writer
, BAD_CAST
"bookmark");
736 "XmlwriterMemory: Error at xmlTextWriterStartElement\n");
739 rc
= xmlTextWriterWriteAttribute(writer
, BAD_CAST
"title",
744 "XmlwriterMemory: Error at xmlTextWriterWriteAttribute\n");
747 rc
= xmlTextWriterWriteAttribute(writer
, BAD_CAST
"uri",
752 "XmlwriterMemory: Error at xmlTextWriterWriteAttribute\n");
755 line_text
= g_strdup_printf ("%d", line
);
756 rc
= xmlTextWriterWriteAttribute(writer
, BAD_CAST
"line",
761 "XmlwriterMemory: Error at xmlTextWriterWriteAttribute\n");
764 /* Close the element named bookmark. */
765 rc
= xmlTextWriterEndElement(writer
);
767 DEBUG_PRINT ("%s", "XmlwriterMemory: Error at xmlTextWriterEndElement\n");
771 while (gtk_tree_model_iter_next (priv
->model
, &iter
));
773 rc
= xmlTextWriterEndDocument(writer
);
775 DEBUG_PRINT ("%s", "testXmlwriterMemory: Error at xmlTextWriterEndDocument\n");
778 xmlFreeTextWriter(writer
);
780 anjuta_session_set_string (session
,
783 (const gchar
*) buf
->content
);
786 /* Clear the model */
787 gtk_list_store_clear (GTK_LIST_STORE (priv
->model
));
789 if (priv
->query_scope
)
791 g_object_unref (priv
->query_scope
);
792 priv
->query_scope
= NULL
;
797 anjuta_bookmarks_toggle (AnjutaBookmarks
* bookmarks
,
798 IAnjutaEditor
* editor
,
801 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
803 g_return_if_fail (bookmarks
!= NULL
);
804 g_return_if_fail (editor
!= NULL
);
806 /* Check if there is a bookmark in that line already */
807 if (ianjuta_markable_is_marker_set (IANJUTA_MARKABLE (editor
),
808 line
, IANJUTA_MARKABLE_BOOKMARK
, NULL
))
811 if (gtk_tree_model_get_iter_first (priv
->model
, &iter
))
817 gtk_tree_model_get (priv
->model
, &iter
,
818 COLUMN_HANDLE
, &handle
, -1);
820 /* Update location if necessary */
821 location
= ianjuta_markable_location_from_handle (IANJUTA_MARKABLE (editor
),
823 gtk_list_store_set (GTK_LIST_STORE (priv
->model
), &iter
,
824 COLUMN_LINE
, location
, -1 );
825 if (line
== location
)
827 GtkTreeSelection
* selection
= gtk_tree_view_get_selection (GTK_TREE_VIEW (priv
->tree
));
828 gtk_tree_selection_select_iter (selection
, &iter
);
829 anjuta_bookmarks_remove (bookmarks
);
832 while (gtk_tree_model_iter_next (priv
->model
, &iter
));
837 anjuta_bookmarks_add (bookmarks
, editor
, line
, NULL
, TRUE
);
842 read_bookmarks (AnjutaBookmarks
* bookmarks
, xmlNodePtr marks
)
845 for (cur
= marks
; cur
!= NULL
; cur
= cur
->next
)
847 DEBUG_PRINT ("Reading bookmark: %s", cur
->name
);
848 if (xmlStrcmp (cur
->name
, BAD_CAST
"bookmark") == 0)
856 title
= (gchar
*) xmlGetProp(cur
, BAD_CAST
"title");
857 uri
= (gchar
*) xmlGetProp(cur
, BAD_CAST
"uri");
858 line_text
= (gchar
*) xmlGetProp(cur
, BAD_CAST
"line");
860 DEBUG_PRINT ("Reading bookmark real: %s", title
);
863 line
= atoi(line_text
);
864 file
= g_file_new_for_uri (uri
);
866 anjuta_bookmarks_add_file (bookmarks
, file
, line
, title
);
874 anjuta_bookmarks_session_load (AnjutaBookmarks
* bookmarks
, AnjutaSession
* session
)
876 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
877 gchar
* xml_string
= anjuta_session_get_string (session
,
880 DEBUG_PRINT("Session load");
882 if (!xml_string
|| !strlen(xml_string
))
884 xmlDocPtr doc
= xmlParseMemory (xml_string
,
885 strlen (xml_string
));
888 xmlNodePtr cur
= xmlDocGetRootElement (doc
);
896 if (xmlStrcmp (cur
->name
, BAD_CAST
"bookmarks") == 0)
897 read_bookmarks (bookmarks
, cur
->children
);
901 priv
->query_scope
= anjuta_bookmarks_create_query (bookmarks
);
905 line_compare (gconstpointer line1
, gconstpointer line2
)
907 gint l1
= GPOINTER_TO_INT(line1
);
908 gint l2
= GPOINTER_TO_INT(line2
);
913 get_bookmarks_for_editor (AnjutaBookmarks
* bookmarks
, IAnjutaEditor
* editor
)
915 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
919 if (!gtk_tree_model_get_iter_first (priv
->model
, &iter
))
921 file
= ianjuta_file_get_file (IANJUTA_FILE(editor
), NULL
);
926 GFile
* bookmark_file
;
928 gtk_tree_model_get (priv
->model
,
930 COLUMN_FILE
, &bookmark_file
,
933 if (g_file_equal (file
, bookmark_file
))
934 marks
= g_list_insert_sorted (marks
, GINT_TO_POINTER(line
), line_compare
);
935 g_object_unref (bookmark_file
);
937 while (gtk_tree_model_iter_next (priv
->model
, &iter
));
938 g_object_unref (file
);
944 anjuta_bookmarks_next (AnjutaBookmarks
* bookmarks
, IAnjutaEditor
* editor
,
947 GList
* marks
= get_bookmarks_for_editor (bookmarks
, editor
);
950 for (node
= marks
; node
!= NULL
; node
= g_list_next (node
))
952 gint node_line
= GPOINTER_TO_INT (node
->data
);
953 if (node_line
> line
)
955 ianjuta_editor_goto_line (editor
, node_line
, NULL
);
962 anjuta_bookmarks_prev (AnjutaBookmarks
* bookmarks
, IAnjutaEditor
* editor
,
965 GList
* marks
= get_bookmarks_for_editor (bookmarks
, editor
);
968 marks
= g_list_reverse (marks
);
970 for (node
= marks
; node
!= NULL
; node
= g_list_next (node
))
972 gint node_line
= GPOINTER_TO_INT (node
->data
);
973 if (node_line
< line
)
975 ianjuta_editor_goto_line (editor
, node_line
, NULL
);
981 void anjuta_bookmarks_clear (AnjutaBookmarks
* bookmarks
)
983 AnjutaBookmarksPrivate
* priv
= BOOKMARKS_GET_PRIVATE(bookmarks
);
984 GtkTreeSelection
* selection
= gtk_tree_view_get_selection (GTK_TREE_VIEW(priv
->tree
));
985 gtk_tree_selection_select_all (selection
);
986 anjuta_bookmarks_remove (bookmarks
);