Updated Spanish translation
[anjuta.git] / plugins / document-manager / plugin.c
blob50470ecd717ec8d00cd3e326cba493e9ab5d10a8
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 plugin.c
4 Copyright (C) 2000-2008 Naba Kumar
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <config.h>
22 #include <libanjuta/anjuta-shell.h>
23 #include <libanjuta/anjuta-debug.h>
24 #include <libanjuta/anjuta-encodings.h>
26 #include <libanjuta/interfaces/ianjuta-document-manager.h>
27 #include <libanjuta/interfaces/ianjuta-project-manager.h>
28 #include <libanjuta/interfaces/ianjuta-file.h>
29 #include <libanjuta/interfaces/ianjuta-editor.h>
30 #include <libanjuta/interfaces/ianjuta-editor-selection.h>
31 #include <libanjuta/interfaces/ianjuta-editor-convert.h>
32 #include <libanjuta/interfaces/ianjuta-editor-view.h>
33 #include <libanjuta/interfaces/ianjuta-editor-line-mode.h>
34 #include <libanjuta/interfaces/ianjuta-editor-factory.h>
35 #include <libanjuta/interfaces/ianjuta-editor-folds.h>
36 #include <libanjuta/interfaces/ianjuta-editor-comment.h>
37 #include <libanjuta/interfaces/ianjuta-editor-zoom.h>
38 #include <libanjuta/interfaces/ianjuta-editor-goto.h>
39 #include <libanjuta/interfaces/ianjuta-editor-search.h>
40 #include <libanjuta/interfaces/ianjuta-editor-assist.h>
41 #include <libanjuta/interfaces/ianjuta-file-savable.h>
42 #include <libanjuta/interfaces/ianjuta-editor-language.h>
43 #include <libanjuta/interfaces/ianjuta-language.h>
44 #include <libanjuta/interfaces/ianjuta-preferences.h>
45 #include <libanjuta/interfaces/ianjuta-document.h>
47 #include "anjuta-docman.h"
48 #include "action-callbacks.h"
49 #include "plugin.h"
50 #include "search-box.h"
51 #include "search-files.h"
52 #include "anjuta-bookmarks.h"
54 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-document-manager.xml"
55 #define PREFS_BUILDER PACKAGE_DATA_DIR"/glade/anjuta-document-manager.ui"
56 #define ICON_FILE "anjuta-document-manager-plugin-48.png"
58 #define PREF_SCHEMA "org.gnome.anjuta.document-manager"
60 #define ANJUTA_PIXMAP_BOOKMARK_TOGGLE "anjuta-bookmark-toggle"
61 #define ANJUTA_PIXMAP_BOOKMARK_PREV "anjuta-bookmark-prev"
62 #define ANJUTA_PIXMAP_BOOKMARK_NEXT "anjuta-bookmark-next"
63 #define ANJUTA_PIXMAP_BOOKMARK_CLEAR "anjuta-bookmark-clear"
65 #define ANJUTA_PIXMAP_FOLD_TOGGLE "anjuta-fold-toggle"
66 #define ANJUTA_PIXMAP_FOLD_CLOSE "anjuta-fold-close"
67 #define ANJUTA_PIXMAP_FOLD_OPEN "anjuta-fold-open"
69 #define ANJUTA_PIXMAP_BLOCK_SELECT "anjuta-block-select"
70 #define ANJUTA_PIXMAP_BLOCK_START "anjuta-block-start"
71 #define ANJUTA_PIXMAP_BLOCK_END "anjuta-block-end"
73 #define ANJUTA_PIXMAP_INDENT_INC "anjuta-indent-more"
74 #define ANJUTA_PIXMAP_INDENT_DCR "anjuta-indent-less"
76 #define ANJUTA_PIXMAP_GOTO_LINE "anjuta-go-line"
78 #define ANJUTA_PIXMAP_HISTORY_NEXT "anjuta-go-history-next"
79 #define ANJUTA_PIXMAP_HISTORY_PREV "anjuta-go-history-prev"
81 #define ANJUTA_PIXMAP_AUTOCOMPLETE "anjuta-complete-auto"
83 /* Stock icons */
84 #define ANJUTA_STOCK_FOLD_TOGGLE "anjuta-fold-toggle"
85 #define ANJUTA_STOCK_FOLD_OPEN "anjuta-fold-open"
86 #define ANJUTA_STOCK_FOLD_CLOSE "anjuta-fold-close"
87 #define ANJUTA_STOCK_BLOCK_SELECT "anjuta-block-select"
88 #define ANJUTA_STOCK_INDENT_INC "anjuta-indent-inc"
89 #define ANJUTA_STOCK_INDENT_DCR "anjuta-indect-dcr"
90 #define ANJUTA_STOCK_PREV_BRACE "anjuta-prev-brace"
91 #define ANJUTA_STOCK_NEXT_BRACE "anjuta-next-brace"
92 #define ANJUTA_STOCK_BLOCK_START "anjuta-block-start"
93 #define ANJUTA_STOCK_BLOCK_END "anjuta-block-end"
94 #define ANJUTA_STOCK_BOOKMARK_TOGGLE "anjuta-bookmark-toggle"
95 #define ANJUTA_STOCK_BOOKMARK_PREV "anjuta-bookmark-previous"
96 #define ANJUTA_STOCK_BOOKMARK_NEXT "anjuta-bookmark-next"
97 #define ANJUTA_STOCK_BOOKMARK_CLEAR "anjuta-bookmark-clear"
98 #define ANJUTA_STOCK_GOTO_LINE "anjuta-goto-line"
99 #define ANJUTA_STOCK_HISTORY_NEXT "anjuta-history-next"
100 #define ANJUTA_STOCK_HISTORY_PREV "anjuta-history-prev"
101 #define ANJUTA_STOCK_MATCH_NEXT "anjuta-match-next"
102 #define ANJUTA_STOCK_MATCH_PREV "anjuta-match-prev"
103 #define ANJUTA_STOCK_AUTOCOMPLETE "anjuta-autocomplete"
106 static gpointer parent_class;
108 /* Shortcuts implementation */
109 enum {
110 m___ = 0,
111 mS__ = GDK_SHIFT_MASK,
112 m_C_ = GDK_CONTROL_MASK,
113 m__M = GDK_MOD1_MASK,
114 mSC_ = GDK_SHIFT_MASK | GDK_CONTROL_MASK,
117 enum {
118 ID_FIRSTBUFFER = 1, /* Note: the value mustn't be 0 ! */
121 typedef struct {
122 gint modifiers;
123 guint gdk_key;
124 gint id;
125 } ShortcutMapping;
127 static ShortcutMapping global_keymap[] = {
128 { m__M, GDK_KEY_1, ID_FIRSTBUFFER },
129 { m__M, GDK_KEY_2, ID_FIRSTBUFFER + 1},
130 { m__M, GDK_KEY_3, ID_FIRSTBUFFER + 2},
131 { m__M, GDK_KEY_4, ID_FIRSTBUFFER + 3},
132 { m__M, GDK_KEY_5, ID_FIRSTBUFFER + 4},
133 { m__M, GDK_KEY_6, ID_FIRSTBUFFER + 5},
134 { m__M, GDK_KEY_7, ID_FIRSTBUFFER + 6},
135 { m__M, GDK_KEY_8, ID_FIRSTBUFFER + 7},
136 { m__M, GDK_KEY_9, ID_FIRSTBUFFER + 8},
137 { m__M, GDK_KEY_0, ID_FIRSTBUFFER + 9},
138 { 0, 0, 0 }
141 static GtkActionEntry actions_file[] = {
142 { "ActionFileSave", GTK_STOCK_SAVE, N_("_Save"), "<control>s",
143 N_("Save current file"), G_CALLBACK (on_save_activate)},
144 { "ActionFileSaveAs", GTK_STOCK_SAVE_AS, N_("Save _As…"), "<shift><control>s",
145 N_("Save the current file with a different name"),
146 G_CALLBACK (on_save_as_activate)},
147 { "ActionFileSaveAll", GTK_STOCK_SAVE, N_("Save A_ll"), "<shift><control>l",
148 N_("Save all currently open files, except new files"),
149 G_CALLBACK (on_save_all_activate)},
150 { "ActionFileClose", GTK_STOCK_CLOSE, N_("_Close File"), "<control>w",
151 N_("Close current file"),
152 G_CALLBACK (on_close_file_activate)},
153 { "ActionFileCloseAll", GTK_STOCK_CLOSE, N_("Close All"), "<shift><control>w",
154 N_("Close all files"),
155 G_CALLBACK (on_close_all_file_activate)},
156 { "ActionFileCloseOther", GTK_STOCK_CLOSE, N_("Close Others"), "<shift><control>o",
157 N_("Close other documents"),
158 G_CALLBACK (on_close_other_file_activate)},
159 { "ActionFileReload", GTK_STOCK_REVERT_TO_SAVED, N_("Reload F_ile"), NULL,
160 N_("Reload current file"),
161 G_CALLBACK (on_reload_file_activate)},
162 { "ActionMenuFileRecentFiles", NULL, N_("Recent _Files"), NULL, NULL, NULL}, /* menu title */
165 static GtkActionEntry actions_print[] = {
166 { "ActionPrintFile", GTK_STOCK_PRINT, N_("_Print…"), "<control>p",
167 N_("Print the current file"), G_CALLBACK (on_print_activate)},
168 { "ActionPrintPreview",
169 #ifdef GTK_STOCK_PRINT_PREVIEW
170 GTK_STOCK_PRINT_PREVIEW
171 #else
172 NULL
173 #endif
174 , N_("_Print Preview"), NULL,
175 N_("Preview the current file in print format"),
176 G_CALLBACK (on_print_preview_activate)},
179 static GtkActionEntry actions_transform[] = {
180 { "ActionMenuEditTransform", NULL, N_("_Transform"), NULL, NULL, NULL}, /* menu title */
181 { "ActionEditMakeSelectionUppercase", NULL, N_("_Make Selection Uppercase"), NULL,
182 N_("Make the selected text uppercase"),
183 G_CALLBACK (on_editor_command_upper_case_activate)},
184 { "ActionEditMakeSelectionLowercase", NULL, N_("Make Selection Lowercase"), NULL,
185 N_("Make the selected text lowercase"),
186 G_CALLBACK (on_editor_command_lower_case_activate)},
187 { "ActionEditConvertCRLF", NULL, N_("Convert EOL to CRLF"), NULL,
188 N_("Convert End Of Line characters to DOS EOL (CRLF)"),
189 G_CALLBACK (on_editor_command_eol_crlf_activate)},
190 { "ActionEditConvertLF", NULL, N_("Convert EOL to LF"), NULL,
191 N_("Convert End Of Line characters to Unix EOL (LF)"),
192 G_CALLBACK (on_editor_command_eol_lf_activate)},
193 { "ActionEditConvertCR", NULL, N_("Convert EOL to CR"), NULL,
194 N_("Convert End Of Line characters to Mac OS EOL (CR)"),
195 G_CALLBACK (on_editor_command_eol_cr_activate)},
196 { "ActionEditConvertEOL", NULL, N_("Convert EOL to Majority EOL"), NULL,
197 N_("Convert End Of Line characters to the most common EOL found in the file"),
198 G_CALLBACK (on_transform_eolchars1_activate)},
201 static GtkActionEntry actions_select[] = {
202 { "ActionMenuEditSelect", NULL, N_("_Select"), NULL, NULL, NULL}, /* menu title */
203 { "ActionEditSelectAll",
204 GTK_STOCK_SELECT_ALL, N_("Select _All"), "<control>a",
205 N_("Select all text in the editor"),
206 G_CALLBACK (on_editor_command_select_all_activate)},
207 { "ActionEditSelectBlock", ANJUTA_STOCK_BLOCK_SELECT, N_("Select _Code Block"),
208 "<shift><control>b", N_("Select the current code block"),
209 G_CALLBACK (on_editor_command_select_block_activate)}
212 static GtkActionEntry actions_comment[] = {
213 { "ActionMenuEditComment", NULL, N_("Co_mment"), NULL, NULL, NULL},
214 /* Block comment: Uses line-comment (comment that affects only single line
215 such as '//' or '#') and comments a block of lines. */
216 { "ActionEditCommentBlock", NULL, N_("_Block Comment/Uncomment"), NULL,
217 N_("Block comment the selected text"),
218 G_CALLBACK (on_comment_block)},
219 /* Box comment: Uses stream-comment to comment a block of lines, usually with
220 some decorations, to give an appearance of box. */
221 { "ActionEditCommentBox", NULL, N_("Bo_x Comment/Uncomment"), NULL,
222 N_("Box comment the selected text"),
223 G_CALLBACK (on_comment_box)},
224 /* Stream comment: Uses 'stream comment' (comment that affects a stream of
225 characters -- has start and end comment code) and comments any code from
226 arbitrary start position to arbitrary end position (can be in middle of
227 lines). */
228 { "ActionEditCommentStream", NULL, N_("_Stream Comment/Uncomment"), NULL,
229 N_("Stream comment the selected text"),
230 G_CALLBACK (on_comment_stream)},
233 static GtkActionEntry actions_navigation[] = {
234 { "ActionMenuGoto", NULL, N_("_Go to"), NULL, NULL, NULL},/* menu title */
235 { "ActionEditGotoLine", ANJUTA_STOCK_GOTO_LINE, N_("_Line Number…"),
236 "<control><alt>g", N_("Go to a particular line in the editor"),
237 G_CALLBACK (on_goto_line_no1_activate)},
238 { "ActionEditGotoMatchingBrace", GTK_STOCK_JUMP_TO, N_("Matching _Brace"),
239 "<control><alt>m", N_("Go to the matching brace in the editor"),
240 G_CALLBACK (on_editor_command_match_brace_activate)},
241 { "ActionEditGotoBlockStart", ANJUTA_STOCK_BLOCK_START, N_("_Start of Block"),
242 "<control><alt>s", N_("Go to the start of the current block"),
243 G_CALLBACK (on_goto_block_start1_activate)},
244 { "ActionEditGotoBlockEnd", ANJUTA_STOCK_BLOCK_END, N_("_End of Block"),
245 "<control><alt>e", N_("Go to the end of the current block"),
246 G_CALLBACK (on_goto_block_end1_activate)},
247 { "ActionEditGotoHistoryPrev", ANJUTA_STOCK_HISTORY_PREV, N_("Previous _History"),
248 "<alt>Left", N_("Go to previous history"),
249 G_CALLBACK (on_prev_history)},
250 { "ActionEditGotoHistoryNext", ANJUTA_STOCK_HISTORY_NEXT, N_("Next Histor_y"),
251 "<alt>Right", N_("Go to next history"),
252 G_CALLBACK (on_next_history)}
255 static GtkActionEntry actions_search[] = {
256 { "ActionMenuEditSearch", NULL, N_("_Search"), NULL, NULL, NULL},
257 { "ActionEditSearchQuickSearch", GTK_STOCK_FIND, N_("_Quick Search"),
258 "<control>f", N_("Quick editor embedded search"),
259 G_CALLBACK (on_show_search)},
260 { "ActionEditSearchQuickSearchAgain", GTK_STOCK_FIND, N_("Find _Next"),
261 "<control>g", N_("Search for next appearance of term."),
262 G_CALLBACK (on_repeat_quicksearch)},
263 { "ActionEditSearchReplace", GTK_STOCK_FIND, N_("Find and R_eplace…"),
264 "<control>h", N_("Search and replace"),
265 G_CALLBACK (on_search_and_replace)},
266 { "ActionEditSearchFindPrevious", GTK_STOCK_FIND, N_("Find _Previous"),
267 "<control><shift>g",
268 N_("Repeat the last Find command"),
269 G_CALLBACK (on_search_previous)},
270 { "ActionSearchboxPopupClearHighlight", GTK_STOCK_FIND, N_("Clear Highlight"),
271 NULL, N_("Clear all highlighted text"),
272 G_CALLBACK (on_search_popup_clear_highlight)},
273 { "ActionEditFindFiles", GTK_STOCK_FIND_AND_REPLACE, N_("Find in Files"),
274 NULL, N_("Search in project files"),
275 G_CALLBACK (on_search_find_in_files)}
278 static GtkToggleActionEntry actions_searchbox_popup[] = {
279 { "ActionSearchboxPopupCaseCheck", GTK_STOCK_FIND,
280 N_("Case Sensitive"), NULL,
281 N_("Match case in search results."),
282 G_CALLBACK (on_search_popup_case_sensitive_toggle)},
283 { "ActionSearchboxPopupHighlightAll", GTK_STOCK_FIND,
284 N_("Highlight All"), NULL,
285 N_("Highlight all occurrences"),
286 G_CALLBACK (on_search_popup_highlight_toggle)},
287 { "ActionSearchboxRegexSearch", GTK_STOCK_FIND,
288 N_("Regular Expression"), NULL,
289 N_("Search using regular expressions"),
290 G_CALLBACK (on_search_popup_regex_search)}
294 static GtkActionEntry actions_edit[] = {
295 { "ActionMenuEdit", NULL, N_("_Edit"), NULL, NULL, NULL},/* menu title */
296 { "ActionMenuViewEditor", NULL, N_("_Editor"), NULL, NULL, NULL},
297 { "ActionViewEditorAddView",
298 #ifdef GTK_STOCK_EDIT
299 GTK_STOCK_EDIT
300 #else
301 NULL
302 #endif
303 , N_("_Add Editor View"), NULL,
304 N_("Add one more view of current document"),
305 G_CALLBACK (on_editor_add_view_activate)},
306 { "ActionViewEditorRemoveView", NULL, N_("_Remove Editor View"), NULL,
307 N_("Remove current view of the document"),
308 G_CALLBACK (on_editor_remove_view_activate)},
309 { "ActionEditUndo", GTK_STOCK_UNDO, N_("U_ndo"), "<control>z",
310 N_("Undo the last action"),
311 G_CALLBACK (on_editor_command_undo_activate)},
312 { "ActionEditRedo", GTK_STOCK_REDO, N_("_Redo"), "<shift><control>z",
313 N_("Redo the last undone action"),
314 G_CALLBACK (on_editor_command_redo_activate)},
315 { "ActionEditCut", GTK_STOCK_CUT, N_("C_ut"), "<control>x",
316 N_("Cut the selected text from the editor to the clipboard"),
317 G_CALLBACK (on_editor_command_cut_activate)},
318 { "ActionEditCopy", GTK_STOCK_COPY, N_("_Copy"), "<control>c",
319 N_("Copy the selected text to the clipboard"),
320 G_CALLBACK (on_editor_command_copy_activate)},
321 { "ActionEditPaste", GTK_STOCK_PASTE, N_("_Paste"), "<control>v",
322 N_("Paste the content of clipboard at the current position"),
323 G_CALLBACK (on_editor_command_paste_activate)},
324 { "ActionEditClear",
325 #ifdef GTK_STOCK_DISCARD
326 GTK_STOCK_DISCARD
327 #else
328 GTK_STOCK_CLEAR
329 #endif
330 , N_("_Clear"), NULL,
331 N_("Delete the selected text from the editor"),
332 G_CALLBACK (on_editor_command_clear_activate)},
333 { "ActionEditAutocomplete", ANJUTA_STOCK_AUTOCOMPLETE,
334 N_("_Auto-Complete"), "<control>Return",
335 N_("Auto-complete the current word"), G_CALLBACK (on_autocomplete_activate)
340 static GtkActionEntry actions_zoom[] = {
341 { "ActionViewEditorZoomIn", GTK_STOCK_ZOOM_IN, N_("Zoom In"), "<control>KP_Add",
342 N_("Zoom in: Increase font size"),
343 G_CALLBACK (on_zoom_in_text_activate)},
344 { "ActionViewEditorZoomOut", GTK_STOCK_ZOOM_OUT, N_("Zoom Out"), "<control>KP_Subtract",
345 N_("Zoom out: Decrease font size"),
346 G_CALLBACK (on_zoom_out_text_activate)}
349 static GtkActionEntry actions_style[] = {
350 { "ActionMenuFormatStyle", NULL, N_("_Highlight Mode"), NULL, NULL, NULL}/* menu title */
353 static GtkActionEntry actions_format[] = {
354 { "ActionFormatFoldCloseAll", ANJUTA_STOCK_FOLD_CLOSE, N_("_Close All Folds"),
355 NULL, N_("Close all code folds in the editor"),
356 G_CALLBACK (on_editor_command_close_folds_all_activate)},
357 { "ActionFormatFoldOpenAll", ANJUTA_STOCK_FOLD_OPEN, N_("_Open All Folds"),
358 NULL, N_("Open all code folds in the editor"),
359 G_CALLBACK (on_editor_command_open_folds_all_activate)},
360 { "ActionFormatFoldToggle", ANJUTA_STOCK_FOLD_TOGGLE, N_("_Toggle Current Fold"),
361 NULL, N_("Toggle current code fold in the editor"),
362 G_CALLBACK (on_editor_command_toggle_fold_activate)},
365 static GtkActionEntry actions_documents[] = {
366 {"ActionMenuDocuments", NULL, N_("_Documents"), NULL, NULL, NULL},
367 { "ActionDocumentsPrevious", GTK_STOCK_GO_BACK, N_("Previous Document"),
368 "<control>Page_Up", N_("Switch to previous document"),
369 G_CALLBACK (on_previous_document)},
370 { "ActionDocumentsNext", GTK_STOCK_GO_FORWARD, N_("Next Document"),
371 "<control>Page_Down", N_("Switch to next document"),
372 G_CALLBACK (on_next_document)},
375 static GtkActionEntry actions_bookmarks[] = {
376 { "ActionMenuBookmark", NULL, N_("Bookmar_k"), NULL, NULL, NULL},
377 { "ActionBookmarkToggle", GTK_STOCK_ADD, N_("_Toggle Bookmark"),
378 "<control>k", N_("Toggle bookmark at the current line position"),
379 G_CALLBACK (on_bookmark_toggle_activate)},
380 { "ActionBookmarkPrev", ANJUTA_STOCK_BOOKMARK_PREV, N_("_Previous Bookmark"),
381 "<control>comma", N_("Jump to the previous bookmark in the file"),
382 G_CALLBACK (on_bookmark_prev_activate)},
383 { "ActionBookmarkNext", ANJUTA_STOCK_BOOKMARK_NEXT, N_("_Next Bookmark"),
384 "<control>period", N_("Jump to the next bookmark in the file"),
385 G_CALLBACK (on_bookmark_next_activate)},
386 { "ActionBookmarksClear", ANJUTA_STOCK_BOOKMARK_CLEAR, N_("_Clear All Bookmarks"),
387 NULL, N_("Clear bookmarks"),
388 G_CALLBACK (on_bookmarks_clear_activate)},
391 struct ActionGroupInfo {
392 GtkActionEntry *group;
393 gint size;
394 gchar *name;
395 gchar *label;
398 struct ActionToggleGroupInfo {
399 GtkToggleActionEntry *group;
400 gint size;
401 gchar *name;
402 gchar *label;
405 static struct ActionGroupInfo action_groups[] = {
406 { actions_file, G_N_ELEMENTS (actions_file), "ActionGroupEditorFile", N_("Editor file operations")},
407 { actions_print, G_N_ELEMENTS (actions_print), "ActionGroupEditorPrint", N_("Editor print operations")},
408 { actions_transform, G_N_ELEMENTS (actions_transform), "ActionGroupEditorTransform", N_("Editor text transformation") },
409 { actions_select, G_N_ELEMENTS (actions_select), "ActionGroupEditorSelect", N_("Editor text selection") },
410 // { actions_insert, G_N_ELEMENTS (actions_insert), "ActionGroupEditorInsert", N_("Editor text insertions") },
411 { actions_comment, G_N_ELEMENTS (actions_comment), "ActionGroupEditorComment", N_("Editor code commenting") },
412 { actions_navigation, G_N_ELEMENTS (actions_navigation), "ActionGroupEditorNavigate", N_("Editor navigations") },
413 { actions_edit, G_N_ELEMENTS (actions_edit), "ActionGroupEditorEdit", N_("Editor edit operations") },
414 { actions_zoom, G_N_ELEMENTS (actions_zoom), "ActionGroupEditorZoom", N_("Editor zoom operations") },
415 { actions_style, G_N_ELEMENTS (actions_style), "ActionGroupEditorStyle", N_("Editor syntax highlighting styles") },
416 { actions_format, G_N_ELEMENTS (actions_format), "ActionGroupEditorFormat", N_("Editor text formatting") },
417 { actions_search, G_N_ELEMENTS (actions_search), "ActionGroupEditorSearch", N_("Simple searching") },
418 { actions_documents, G_N_ELEMENTS (actions_documents), "ActionGroupDocuments", N_("Documents") },
419 { actions_bookmarks, G_N_ELEMENTS (actions_bookmarks), "ActionGroupBookmarks", N_("Bookmarks") }
422 static struct ActionToggleGroupInfo action_toggle_groups[] = {
423 { actions_searchbox_popup, G_N_ELEMENTS(actions_searchbox_popup),
424 "ActionGroupEditorSearchOptions", N_("Toggle search options") }
427 // void pref_set_style_combo(DocmanPlugin *plugin);
429 #define MAX_TITLE_LENGTH 80
431 static void
432 on_editor_lang_changed (IAnjutaEditor* editor, const gchar* language,
433 DocmanPlugin* plugin);
435 static gchar*
436 get_directory_display_name (DocmanPlugin* plugin,
437 GFile* file)
439 gchar* dir;
440 gchar* display_uri = g_file_get_parse_name (file);
441 gchar* display_dir;
442 dir = anjuta_util_uri_get_dirname (display_uri);
444 display_dir = anjuta_util_str_middle_truncate (dir,
445 MAX (20, MAX_TITLE_LENGTH));
446 g_free (display_uri);
447 g_free (dir);
448 return display_dir;
451 static void
452 update_title (DocmanPlugin* doc_plugin)
454 IAnjutaDocument *doc;
455 AnjutaStatus *status;
456 gchar *title;
458 doc = anjuta_docman_get_current_document (ANJUTA_DOCMAN (doc_plugin->docman));
460 if (doc)
462 gchar* real_filename;
463 gchar *dir;
464 const gchar *filename;
465 filename = ianjuta_document_get_filename (doc, NULL);
466 GFile* file;
467 file = ianjuta_file_get_file (IANJUTA_FILE (doc), NULL);
468 if (file)
470 dir = get_directory_display_name (doc_plugin, file);
471 g_object_unref (file);
473 else
475 dir = NULL;
477 if (ianjuta_file_savable_is_dirty(IANJUTA_FILE_SAVABLE (doc), NULL))
479 gchar* dirty_name = g_strconcat ("*", filename, NULL);
480 real_filename = dirty_name;
482 else
483 real_filename = g_strdup (filename);
485 if (doc_plugin->project_name)
487 if (dir)
488 title = g_strdup_printf ("%s (%s) - %s", real_filename, dir,
489 doc_plugin->project_name);
490 else
491 title = g_strdup_printf ("%s - %s", real_filename,
492 doc_plugin->project_name);
494 else
496 if (dir)
497 title = g_strdup_printf ("%s (%s)", real_filename, dir);
498 else
499 title = g_strdup_printf ("%s", real_filename);
501 g_free (real_filename);
502 g_free (dir);
504 else
506 title = g_strdup (doc_plugin->project_name);
508 status = anjuta_shell_get_status (ANJUTA_PLUGIN (doc_plugin)->shell, NULL);
509 /* NULL title is ok */
510 anjuta_status_set_title (status, title);
511 g_free (title);
514 static void
515 value_added_project_root_uri (AnjutaPlugin *plugin, const gchar *name,
516 const GValue *value, gpointer user_data)
518 DocmanPlugin *doc_plugin;
519 const gchar *root_uri;
521 doc_plugin = ANJUTA_PLUGIN_DOCMAN (plugin);
523 DEBUG_PRINT ("%s", "Project added");
525 g_free (doc_plugin->project_name);
526 g_free (doc_plugin->project_path);
527 doc_plugin->project_name = NULL;
528 doc_plugin->project_path = NULL;
530 if (doc_plugin->search_files)
531 search_files_update_project (SEARCH_FILES(doc_plugin->search_files));
533 root_uri = g_value_get_string (value);
534 if (root_uri)
536 GFile* file = g_file_new_for_uri (root_uri);
537 gchar* path = g_file_get_path (file);
539 doc_plugin->project_name = g_file_get_basename (file);
540 doc_plugin->project_path = path;
542 if (doc_plugin->project_name)
544 update_title (doc_plugin);
546 g_object_unref (file);
548 anjuta_docman_project_path_updated (ANJUTA_DOCMAN (doc_plugin->docman));
552 static void
553 value_removed_project_root_uri (AnjutaPlugin *plugin, const gchar *name,
554 gpointer user_data)
556 DocmanPlugin *doc_plugin;
558 doc_plugin = ANJUTA_PLUGIN_DOCMAN (plugin);
560 DEBUG_PRINT ("%s", "Project removed");
562 g_free (doc_plugin->project_name);
563 g_free (doc_plugin->project_path);
564 doc_plugin->project_name = NULL;
565 doc_plugin->project_path = NULL;
567 if (doc_plugin->search_files)
568 search_files_update_project (SEARCH_FILES(doc_plugin->search_files));
570 update_title(doc_plugin);
571 anjuta_docman_project_path_updated (ANJUTA_DOCMAN (doc_plugin->docman));
574 static void
575 ui_give_shorter_names (AnjutaPlugin *plugin)
577 AnjutaUI *ui;
578 GtkAction *action;
580 ui = anjuta_shell_get_ui (ANJUTA_PLUGIN (plugin)->shell, NULL);
581 action = anjuta_ui_get_action (ui, "ActionGroupEditorFile",
582 "ActionFileSave");
583 g_object_set (G_OBJECT (action), "short-label", _("Save"),
584 "is-important", TRUE, NULL);
585 action = anjuta_ui_get_action (ui, "ActionGroupEditorFile",
586 "ActionFileReload");
587 g_object_set (G_OBJECT (action), "short-label", _("Reload"), NULL);
588 action = anjuta_ui_get_action (ui, "ActionGroupEditorEdit",
589 "ActionEditUndo");
590 g_object_set (G_OBJECT (action), "is-important", TRUE, NULL);
591 action = anjuta_ui_get_action (ui, "ActionGroupEditorNavigate",
592 "ActionEditGotoLine");
593 g_object_set (G_OBJECT (action), "short-label", _("Go to"), NULL);
596 static void
597 update_document_ui_enable_all (AnjutaPlugin *plugin)
599 AnjutaUI *ui;
600 gint i, j;
601 GtkAction *action;
603 ui = anjuta_shell_get_ui (ANJUTA_PLUGIN (plugin)->shell, NULL);
604 for (i = 0; i < G_N_ELEMENTS (action_groups); i++)
606 for (j = 0; j < action_groups[i].size; j++)
608 action = anjuta_ui_get_action (ui, action_groups[i].name,
609 action_groups[i].group[j].name);
610 if (action_groups[i].group[j].callback)
612 g_object_set (G_OBJECT (action), "sensitive", TRUE, NULL);
618 static void
619 update_document_ui_disable_all (AnjutaPlugin *plugin)
621 AnjutaUI *ui;
622 gint i, j;
623 GtkAction *action;
625 ui = anjuta_shell_get_ui (ANJUTA_PLUGIN (plugin)->shell, NULL);
626 for (i = 0; i < G_N_ELEMENTS (action_groups); i++)
628 for (j = 0; j < action_groups[i].size; j++)
630 /* Never disable find in files. */
631 if (!strcmp (action_groups[i].group[j].name, "ActionEditFindFiles"))
632 continue;
634 action = anjuta_ui_get_action (ui, action_groups[i].name,
635 action_groups[i].group[j].name);
636 if (action_groups[i].group[j].callback)
638 g_object_set (G_OBJECT (action), "sensitive", FALSE, NULL);
644 static void
645 update_document_ui_undo_items (AnjutaPlugin *plugin, IAnjutaDocument* doc)
647 AnjutaUI *ui;
648 GtkAction *action;
650 ui = anjuta_shell_get_ui (plugin->shell, NULL);
652 action = anjuta_ui_get_action (ui, "ActionGroupEditorEdit",
653 "ActionEditUndo");
654 g_object_set (G_OBJECT (action), "sensitive",
655 ianjuta_document_can_undo (doc, NULL), NULL);
657 action = anjuta_ui_get_action (ui, "ActionGroupEditorEdit",
658 "ActionEditRedo");
659 g_object_set (G_OBJECT (action), "sensitive",
660 ianjuta_document_can_redo (doc, NULL), NULL);
663 static void
664 update_document_ui_save_items (AnjutaPlugin *plugin, IAnjutaDocument *doc)
666 AnjutaUI *ui;
667 GtkAction *action;
669 ui = anjuta_shell_get_ui (plugin->shell, NULL);
671 if (anjuta_docman_get_current_document (ANJUTA_DOCMAN(ANJUTA_PLUGIN_DOCMAN(plugin)->docman)) ==
672 doc)
674 action = anjuta_ui_get_action (ui, "ActionGroupEditorFile",
675 "ActionFileSave");
676 g_object_set (G_OBJECT (action), "sensitive",
677 ianjuta_file_savable_is_dirty(IANJUTA_FILE_SAVABLE(doc), NULL),
678 NULL);
681 static void
682 update_document_ui_interface_items (AnjutaPlugin *plugin, IAnjutaDocument *doc)
684 AnjutaUI *ui;
685 GtkAction *action;
686 gboolean flag;
688 ui = anjuta_shell_get_ui (plugin->shell, NULL);
690 /* IAnjutaEditorLanguage */
691 flag = IANJUTA_IS_EDITOR_LANGUAGE (doc);
693 action = anjuta_ui_get_action (ui, "ActionGroupEditorStyle",
694 "ActionMenuFormatStyle");
695 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
697 /* IAnjutaDocument */
698 flag = IANJUTA_IS_DOCUMENT (doc);
700 action = anjuta_ui_get_action (ui, "ActionGroupEditorEdit",
701 "ActionEditCut");
702 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
704 action = anjuta_ui_get_action (ui, "ActionGroupEditorEdit",
705 "ActionEditCopy");
706 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
708 action = anjuta_ui_get_action (ui, "ActionGroupEditorEdit",
709 "ActionEditPaste");
710 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
712 action = anjuta_ui_get_action (ui, "ActionGroupEditorEdit",
713 "ActionEditClear");
714 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
716 /* IAnjutaEditorSelection */
717 flag = IANJUTA_IS_EDITOR_SELECTION (doc);
719 action = anjuta_ui_get_action (ui, "ActionGroupEditorSelect",
720 "ActionEditSelectAll");
721 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
723 action = anjuta_ui_get_action (ui, "ActionGroupEditorSelect",
724 "ActionEditSelectBlock");
725 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
727 /* IAnjutaEditorConvert */
728 flag = IANJUTA_IS_EDITOR_CONVERT (doc);
729 action = anjuta_ui_get_action (ui, "ActionGroupEditorTransform",
730 "ActionEditMakeSelectionUppercase");
731 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
733 action = anjuta_ui_get_action (ui, "ActionGroupEditorTransform",
734 "ActionEditMakeSelectionLowercase");
735 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
737 /* IAnjutaEditorLineMode */
738 flag = IANJUTA_IS_EDITOR_LINE_MODE (doc);
740 action = anjuta_ui_get_action (ui, "ActionGroupEditorTransform",
741 "ActionEditConvertCRLF");
742 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
743 action = anjuta_ui_get_action (ui, "ActionGroupEditorTransform",
744 "ActionEditConvertLF");
745 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
746 action = anjuta_ui_get_action (ui, "ActionGroupEditorTransform",
747 "ActionEditConvertCR");
748 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
749 action = anjuta_ui_get_action (ui, "ActionGroupEditorTransform",
750 "ActionEditConvertEOL");
751 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
753 /* IAnjutaEditorView */
754 flag = IANJUTA_IS_EDITOR_VIEW (doc);
755 action = anjuta_ui_get_action (ui, "ActionGroupEditorEdit",
756 "ActionViewEditorAddView");
757 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
758 action = anjuta_ui_get_action (ui, "ActionGroupEditorEdit",
759 "ActionViewEditorRemoveView");
760 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
762 /* IAnjutaEditorFolds */
763 flag = IANJUTA_IS_EDITOR_FOLDS (doc);
764 action = anjuta_ui_get_action (ui, "ActionGroupEditorFormat",
765 "ActionFormatFoldCloseAll");
766 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
768 flag = IANJUTA_IS_EDITOR_FOLDS (doc);
769 action = anjuta_ui_get_action (ui, "ActionGroupEditorFormat",
770 "ActionFormatFoldOpenAll");
771 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
773 flag = IANJUTA_IS_EDITOR_FOLDS (doc);
774 action = anjuta_ui_get_action (ui, "ActionGroupEditorFormat",
775 "ActionFormatFoldToggle");
776 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
778 /* IAnjutaEditorComment */
779 flag = IANJUTA_IS_EDITOR_COMMENT (doc);
780 action = anjuta_ui_get_action (ui, "ActionGroupEditorComment",
781 "ActionMenuEditComment");
782 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
784 /* IAnjutaEditorZoom */
785 flag = IANJUTA_IS_EDITOR_ZOOM (doc);
786 action = anjuta_ui_get_action (ui, "ActionGroupEditorZoom",
787 "ActionViewEditorZoomIn");
788 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
789 action = anjuta_ui_get_action (ui, "ActionGroupEditorZoom",
790 "ActionViewEditorZoomOut");
791 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
793 /* IAnjutaEditorGoto */
794 flag = IANJUTA_IS_EDITOR_GOTO (doc);
795 action = anjuta_ui_get_action (ui, "ActionGroupEditorNavigate",
796 "ActionEditGotoBlockStart");
797 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
798 action = anjuta_ui_get_action (ui, "ActionGroupEditorNavigate",
799 "ActionEditGotoBlockEnd");
800 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
801 action = anjuta_ui_get_action (ui, "ActionGroupEditorNavigate",
802 "ActionEditGotoMatchingBrace");
803 g_object_set (G_OBJECT (action), "visible", flag, "sensitive", flag, NULL);
805 /* IAnjutaEditorSearch */
806 flag = IANJUTA_IS_EDITOR_SEARCH (doc);
807 action = anjuta_ui_get_action (ui, "ActionGroupEditorSearch",
808 "ActionEditSearchQuickSearch");
809 g_object_set (G_OBJECT (action), "sensitive", flag, NULL);
810 action = anjuta_ui_get_action (ui, "ActionGroupEditorSearch",
811 "ActionEditSearchQuickSearchAgain");
812 action = anjuta_ui_get_action (ui, "ActionGroupEditorSearch",
813 "ActionEditSearchFindPrevious");
814 g_object_set (G_OBJECT (action), "sensitive", flag, NULL);
815 action = anjuta_ui_get_action (ui, "ActionGroupEditorSearch",
816 "ActionEditSearchReplace");
817 g_object_set (G_OBJECT (action), "sensitive", flag, NULL);
818 action = anjuta_ui_get_action (ui, "ActionGroupEditorSearch",
819 "ActionSearchboxPopupClearHighlight");
820 g_object_set (G_OBJECT (action), "sensitive", flag, NULL);
821 action = anjuta_ui_get_action (ui, "ActionGroupEditorNavigate",
822 "ActionEditGotoLine");
823 g_object_set (G_OBJECT (action), "sensitive", flag, NULL);
824 action = anjuta_ui_get_action (ui, "ActionGroupEditorSearchOptions",
825 "ActionSearchboxPopupCaseCheck");
826 g_object_set (G_OBJECT (action), "sensitive", flag, NULL);
827 action = anjuta_ui_get_action (ui, "ActionGroupEditorSearchOptions",
828 "ActionSearchboxPopupHighlightAll");
829 g_object_set (G_OBJECT (action), "sensitive", flag, NULL);
830 action = anjuta_ui_get_action (ui, "ActionGroupEditorSearchOptions",
831 "ActionSearchboxRegexSearch");
832 g_object_set (G_OBJECT (action), "sensitive", flag, NULL);
834 /* IAnjutaEditorAssist */
835 flag = IANJUTA_IS_EDITOR_ASSIST (doc);
837 /* Enable autocompletion action */
838 action = anjuta_ui_get_action (ui,
839 "ActionGroupEditorEdit",
840 "ActionEditAutocomplete");
841 g_object_set (G_OBJECT (action), "visible", flag,
842 "sensitive", flag, NULL);
845 static void
846 update_document_ui (AnjutaPlugin *plugin, IAnjutaDocument *doc)
848 if (doc == NULL)
850 update_document_ui_disable_all (plugin);
851 return;
853 update_document_ui_enable_all (plugin);
854 update_document_ui_save_items (plugin, doc);
855 update_document_ui_interface_items (plugin, doc);
858 static void
859 on_document_update_save_ui (IAnjutaDocument *doc,
860 AnjutaPlugin *plugin)
862 update_document_ui_save_items (plugin, doc);
863 update_title(ANJUTA_PLUGIN_DOCMAN (plugin));
866 static void
867 register_stock_icons (AnjutaPlugin *plugin)
869 static gboolean registered = FALSE;
870 if (registered)
871 return;
872 registered = TRUE;
874 /* Register stock icons */
875 BEGIN_REGISTER_ICON (plugin);
876 REGISTER_ICON (ICON_FILE, "editor-plugin-icon");
877 REGISTER_ICON_FULL (ANJUTA_PIXMAP_FOLD_TOGGLE, ANJUTA_STOCK_FOLD_TOGGLE);
878 REGISTER_ICON_FULL (ANJUTA_PIXMAP_FOLD_OPEN, ANJUTA_STOCK_FOLD_OPEN);
879 REGISTER_ICON_FULL (ANJUTA_PIXMAP_FOLD_CLOSE, ANJUTA_STOCK_FOLD_CLOSE);
880 REGISTER_ICON_FULL (ANJUTA_PIXMAP_INDENT_DCR, ANJUTA_STOCK_INDENT_DCR);
881 REGISTER_ICON_FULL (ANJUTA_PIXMAP_INDENT_INC, ANJUTA_STOCK_INDENT_INC);
882 REGISTER_ICON_FULL (ANJUTA_PIXMAP_BLOCK_SELECT, ANJUTA_STOCK_BLOCK_SELECT);
883 REGISTER_ICON_FULL (ANJUTA_PIXMAP_BOOKMARK_TOGGLE, ANJUTA_STOCK_BOOKMARK_TOGGLE);
884 REGISTER_ICON_FULL (ANJUTA_PIXMAP_BOOKMARK_PREV, ANJUTA_STOCK_BOOKMARK_PREV);
885 REGISTER_ICON_FULL (ANJUTA_PIXMAP_BOOKMARK_NEXT, ANJUTA_STOCK_BOOKMARK_NEXT);
886 REGISTER_ICON_FULL (ANJUTA_PIXMAP_BOOKMARK_CLEAR, ANJUTA_STOCK_BOOKMARK_CLEAR);
887 REGISTER_ICON_FULL (ANJUTA_PIXMAP_BLOCK_START, ANJUTA_STOCK_BLOCK_START);
888 REGISTER_ICON_FULL (ANJUTA_PIXMAP_BLOCK_END, ANJUTA_STOCK_BLOCK_END);
889 REGISTER_ICON_FULL (ANJUTA_PIXMAP_GOTO_LINE, ANJUTA_STOCK_GOTO_LINE);
890 REGISTER_ICON_FULL (ANJUTA_PIXMAP_HISTORY_NEXT, ANJUTA_STOCK_HISTORY_NEXT);
891 REGISTER_ICON_FULL (ANJUTA_PIXMAP_HISTORY_PREV, ANJUTA_STOCK_HISTORY_PREV);
892 REGISTER_ICON_FULL (ANJUTA_PIXMAP_AUTOCOMPLETE, ANJUTA_STOCK_AUTOCOMPLETE);
893 END_REGISTER_ICON;
896 #define TEXT_ZOOM_FACTOR "text-zoom-factor"
898 static void
899 update_status (DocmanPlugin *plugin, IAnjutaEditor *te)
901 AnjutaStatus *status;
903 if (te)
905 gchar *edit /*, *mode*/;
906 guint line, col, zoom;
908 status = anjuta_shell_get_status (ANJUTA_PLUGIN (plugin)->shell, NULL);
909 if (status == NULL)
910 return;
912 line = ianjuta_editor_get_lineno (te, NULL);
913 col = ianjuta_editor_get_column (te, NULL);
915 if (ianjuta_editor_get_overwrite (te, NULL))
917 edit = g_strdup (_("OVR"));
919 else
921 edit = g_strdup (_("INS"));
924 if (IANJUTA_IS_EDITOR_ZOOM(te))
926 zoom = g_settings_get_int (plugin->settings, TEXT_ZOOM_FACTOR);
927 anjuta_status_set_default (status, _("Zoom"), "%d", zoom);
929 else
930 anjuta_status_set_default (status, _("Zoom"), NULL);
932 anjuta_status_set_default (status, _("Line"), "%04d", line);
933 anjuta_status_set_default (status, _("Col"), "%03d", col);
934 anjuta_status_set_default (status, _("Mode"), edit);
935 // anjuta_status_set_default (status, _("EOL"), mode);
937 g_free (edit);
938 /* g_free (mode); */
940 else
942 status = anjuta_shell_get_status (ANJUTA_PLUGIN (plugin)->shell, NULL);
943 if (status)
945 anjuta_status_set (status, "");
950 static void
951 on_document_update_ui (IAnjutaDocument *doc, DocmanPlugin *plugin)
953 IAnjutaDocument *curdoc;
955 curdoc = anjuta_docman_get_current_document (ANJUTA_DOCMAN (plugin->docman));
956 update_document_ui_undo_items (ANJUTA_PLUGIN(plugin), curdoc);
958 if (IANJUTA_IS_EDITOR (curdoc) && curdoc == doc)
960 update_status (plugin, IANJUTA_EDITOR (curdoc));
964 /* Remove all instances of c from the string s. */
965 static void remove_char(gchar *s, gchar c)
967 gchar *t = s;
968 for (; *s ; ++s)
969 if (*s != c)
970 *t++ = *s;
971 *t = '\0';
974 /* Compare two strings, ignoring _ characters which indicate mnemonics.
975 * Returns -1, 0, or 1, just like strcmp(). */
976 static gint
977 menu_name_compare(const gchar *s, const char *t)
979 gchar *s1 = g_utf8_strdown(s, -1);
980 gchar *t1 = g_utf8_strdown(t, -1);
981 remove_char(s1, '_');
982 remove_char(t1, '_');
983 gint result = g_utf8_collate (s1, t1);
984 g_free(s1);
985 g_free(t1);
986 return result;
989 static gint
990 compare_language_name(const gchar *lang1, const gchar *lang2, IAnjutaEditorLanguage *manager)
992 const gchar *fullname1 = ianjuta_editor_language_get_language_name (manager, lang1, NULL);
993 const gchar *fullname2 = ianjuta_editor_language_get_language_name (manager, lang2, NULL);
994 return menu_name_compare(fullname1, fullname2);
997 static GtkWidget*
998 create_highlight_submenu (DocmanPlugin *plugin, IAnjutaEditor *editor)
1000 const GList *languages, *node;
1001 GList *sorted_languages;
1002 GtkWidget *submenu;
1003 GtkWidget *auto_menuitem;
1004 submenu = gtk_menu_new ();
1006 if (!editor || !IANJUTA_IS_EDITOR_LANGUAGE (editor))
1007 return NULL;
1009 languages = ianjuta_editor_language_get_supported_languages (IANJUTA_EDITOR_LANGUAGE (editor), NULL);
1010 if (!languages)
1011 return NULL;
1013 /* Automatic highlight menu */
1014 auto_menuitem = gtk_radio_menu_item_new_with_mnemonic (NULL, _("Automatic"));
1015 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(auto_menuitem), TRUE);
1016 g_signal_connect (G_OBJECT (auto_menuitem), "activate",
1017 G_CALLBACK (on_force_hilite_activate),
1018 plugin);
1019 g_object_set_data(G_OBJECT(auto_menuitem), "language_code", "auto-detect");
1020 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), auto_menuitem);
1021 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), gtk_separator_menu_item_new());
1023 /* Sort languages so they appear alphabetically in the menu. */
1024 sorted_languages = g_list_copy((GList *) languages);
1025 sorted_languages = g_list_sort_with_data(sorted_languages, (GCompareDataFunc) compare_language_name,
1026 IANJUTA_EDITOR_LANGUAGE (editor));
1028 node = sorted_languages;
1029 while (node)
1031 const gchar *lang = node->data;
1032 const gchar *name = ianjuta_editor_language_get_language_name (IANJUTA_EDITOR_LANGUAGE (editor), lang, NULL);
1034 /* Should fix #493583 */
1035 if (name != NULL)
1037 GtkWidget *menuitem;
1038 menuitem = gtk_radio_menu_item_new_with_mnemonic_from_widget (GTK_RADIO_MENU_ITEM(auto_menuitem), name);
1039 g_object_set_data_full (G_OBJECT (menuitem), "language_code",
1040 g_strdup (lang),
1041 (GDestroyNotify)g_free);
1042 g_signal_connect (G_OBJECT (menuitem), "activate",
1043 G_CALLBACK (on_force_hilite_activate),
1044 plugin);
1045 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), menuitem);
1046 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
1048 node = g_list_next (node);
1050 g_list_free(sorted_languages);
1052 gtk_widget_show_all (submenu);
1053 return submenu;
1056 static void
1057 on_support_plugin_deactivated (AnjutaPlugin* plugin, DocmanPlugin* docman_plugin)
1059 docman_plugin->support_plugins = g_list_remove (docman_plugin->support_plugins, plugin);
1062 static GList*
1063 load_new_support_plugins (DocmanPlugin* docman_plugin, GList* new_plugin_handle,
1064 AnjutaPluginManager* plugin_manager)
1066 GList* node;
1067 GList* needed_plugins = NULL;
1068 for (node = new_plugin_handle; node != NULL; node = g_list_next (node))
1070 AnjutaPluginHandle *handle = (AnjutaPluginHandle *)node->data;
1071 GObject* new_plugin = anjuta_plugin_manager_get_plugin_by_handle (plugin_manager,
1072 handle);
1073 GList* item = g_list_find (docman_plugin->support_plugins, new_plugin);
1074 if (!item)
1076 DEBUG_PRINT ("Loading plugin: %s", anjuta_plugin_handle_get_id (handle));
1077 g_signal_connect (new_plugin, "deactivated",
1078 G_CALLBACK (on_support_plugin_deactivated), docman_plugin);
1080 needed_plugins = g_list_append (needed_plugins, new_plugin);
1082 return needed_plugins;
1085 static void
1086 unload_unused_support_plugins (DocmanPlugin* docman_plugin,
1087 GList* needed_plugins)
1089 GList* plugins = g_list_copy (docman_plugin->support_plugins);
1090 GList* node;
1091 DEBUG_PRINT ("Unloading plugins");
1092 for (node = plugins; node != NULL; node = g_list_next (node))
1094 AnjutaPlugin* plugin = ANJUTA_PLUGIN (node->data);
1095 DEBUG_PRINT ("Checking plugin: %p", plugin);
1096 if (g_list_find (needed_plugins, plugin) == NULL)
1098 DEBUG_PRINT ("%s", "Unloading plugin");
1099 anjuta_plugin_deactivate (plugin);
1102 g_list_free (plugins);
1105 static void
1106 update_language_plugin (AnjutaDocman *docman, IAnjutaDocument *doc,
1107 AnjutaPlugin *plugin)
1109 DocmanPlugin* docman_plugin = ANJUTA_PLUGIN_DOCMAN (plugin);
1111 DEBUG_PRINT("%s", "Beginning language support");
1112 if (doc && IANJUTA_IS_EDITOR_LANGUAGE (doc))
1114 AnjutaPluginManager *plugin_manager;
1115 IAnjutaLanguage *lang_manager;
1116 GList *new_support_plugins, *needed_plugins, *node;
1117 const gchar *language;
1119 lang_manager = anjuta_shell_get_interface (plugin->shell,
1120 IAnjutaLanguage,
1121 NULL);
1122 if (!lang_manager)
1124 g_warning ("Could not load language manager!");
1125 return;
1128 g_signal_handlers_block_by_func (doc, on_editor_lang_changed, plugin);
1129 language = ianjuta_language_get_name_from_editor (lang_manager,
1130 IANJUTA_EDITOR_LANGUAGE (doc),
1131 NULL);
1132 g_signal_handlers_unblock_by_func (doc, on_editor_lang_changed, plugin);
1134 if (!language)
1136 unload_unused_support_plugins (docman_plugin, NULL);
1137 return;
1140 /* Load current language editor support plugins */
1141 plugin_manager = anjuta_shell_get_plugin_manager (plugin->shell, NULL);
1142 new_support_plugins = anjuta_plugin_manager_query (plugin_manager,
1143 "Anjuta Plugin",
1144 "Interfaces",
1145 "IAnjutaLanguageSupport",
1146 "Language Support",
1147 "Languages",
1148 language, NULL);
1150 /* Load new plugins */
1151 needed_plugins =
1152 load_new_support_plugins (docman_plugin, new_support_plugins,
1153 plugin_manager);
1155 /* Unload unused plugins */
1156 unload_unused_support_plugins (docman_plugin, needed_plugins);
1158 /* Update list */
1159 g_list_free (docman_plugin->support_plugins);
1160 docman_plugin->support_plugins = needed_plugins;
1162 g_list_free (new_support_plugins);
1164 else
1166 unload_unused_support_plugins (docman_plugin, NULL);
1170 static void
1171 on_document_changed (AnjutaDocman *docman, IAnjutaDocument *doc,
1172 AnjutaPlugin *plugin)
1174 DocmanPlugin *docman_plugin;
1175 update_document_ui (plugin, doc);
1177 docman_plugin = ANJUTA_PLUGIN_DOCMAN (plugin);
1178 if (doc)
1180 GValue value = {0, };
1181 g_value_init (&value, G_TYPE_OBJECT);
1182 g_value_set_object (&value, doc);
1183 anjuta_shell_add_value (plugin->shell,
1184 IANJUTA_DOCUMENT_MANAGER_CURRENT_DOCUMENT,
1185 &value, NULL);
1186 g_value_unset(&value);
1188 else
1190 anjuta_shell_remove_value (plugin->shell, IANJUTA_DOCUMENT_MANAGER_CURRENT_DOCUMENT,
1191 NULL);
1195 if (doc && IANJUTA_IS_EDITOR (doc))
1197 update_status (docman_plugin, IANJUTA_EDITOR (doc));
1198 update_language_plugin (docman, doc, plugin);
1200 else
1202 update_status (docman_plugin, NULL);
1203 update_language_plugin (docman, NULL, plugin);
1205 update_title (ANJUTA_PLUGIN_DOCMAN (plugin));
1208 static void
1209 on_editor_lang_changed (IAnjutaEditor* editor,
1210 const gchar* language,
1211 DocmanPlugin* plugin)
1213 update_language_plugin (ANJUTA_DOCMAN (plugin->docman),
1214 IANJUTA_DOCUMENT (editor),
1215 ANJUTA_PLUGIN(plugin));
1218 #define EDITOR_TABS_RECENT_FIRST "docman-tabs-recent-first"
1219 #define EDITOR_TABS_POS "docman-tabs-pos"
1220 #define EDITOR_SHOW_DROP_DOWN "docman-show-drop-down"
1221 #define EDITOR_TABS_HIDE "docman-tabs-hide"
1222 #define EDITOR_TABS_ORDERING "docman-tabs-ordering"
1223 #define AUTOSAVE_TIMER "docman-autosave-timer"
1224 #define SAVE_AUTOMATIC "docman-automatic-save"
1226 static void
1227 docman_plugin_set_tab_pos (DocmanPlugin *ep)
1230 if (g_settings_get_boolean (ep->settings, EDITOR_SHOW_DROP_DOWN))
1232 anjuta_docman_set_open_documents_mode (ANJUTA_DOCMAN (ep->docman),
1233 ANJUTA_DOCMAN_OPEN_DOCUMENTS_MODE_COMBO);
1235 else if (g_settings_get_boolean (ep->settings, EDITOR_TABS_HIDE))
1237 anjuta_docman_set_open_documents_mode (ANJUTA_DOCMAN (ep->docman),
1238 ANJUTA_DOCMAN_OPEN_DOCUMENTS_MODE_NONE);
1240 else
1242 gchar *tab_pos;
1243 GtkPositionType pos;
1245 anjuta_docman_set_open_documents_mode (ANJUTA_DOCMAN (ep->docman),
1246 ANJUTA_DOCMAN_OPEN_DOCUMENTS_MODE_TABS);
1247 tab_pos = g_settings_get_string (ep->settings, EDITOR_TABS_POS);
1249 pos = GTK_POS_TOP;
1250 if (tab_pos)
1252 if (strcasecmp (tab_pos, "top") == 0)
1253 pos = GTK_POS_TOP;
1254 else if (strcasecmp (tab_pos, "left") == 0)
1255 pos = GTK_POS_LEFT;
1256 else if (strcasecmp (tab_pos, "right") == 0)
1257 pos = GTK_POS_RIGHT;
1258 else if (strcasecmp (tab_pos, "bottom") == 0)
1259 pos = GTK_POS_BOTTOM;
1260 g_free (tab_pos);
1262 anjuta_docman_set_tab_pos (ANJUTA_DOCMAN (ep->docman), pos);
1266 static void
1267 on_document_removed (AnjutaDocman *docman, IAnjutaDocument *doc,
1268 AnjutaPlugin *plugin)
1270 /* Emit document-removed signal */
1271 g_signal_emit_by_name (plugin, "document-removed", doc);
1274 static void
1275 on_document_added (AnjutaDocman *docman, IAnjutaDocument *doc,
1276 AnjutaPlugin *plugin)
1278 GtkWidget *highlight_submenu, *highlight_menu;
1279 DocmanPlugin *docman_plugin;
1281 docman_plugin = ANJUTA_PLUGIN_DOCMAN (plugin);
1282 g_signal_connect (G_OBJECT (doc), "update_ui",
1283 G_CALLBACK (on_document_update_ui),
1284 docman_plugin);
1285 g_signal_connect (G_OBJECT (doc), "update-save-ui",
1286 G_CALLBACK (on_document_update_save_ui),
1287 plugin);
1289 /* Present the vbox as this is the widget that was added to the shell */
1290 anjuta_shell_present_widget (plugin->shell,
1291 GTK_WIDGET (docman_plugin->vbox), NULL);
1293 if (IANJUTA_IS_EDITOR (doc))
1295 IAnjutaEditor *te;
1297 te = IANJUTA_EDITOR (doc);
1299 /* Check for language changes */
1300 g_signal_connect (G_OBJECT (doc), "language-changed",
1301 G_CALLBACK (on_editor_lang_changed),
1302 docman_plugin);
1305 /* Create Highlight submenu */
1306 highlight_submenu =
1307 create_highlight_submenu (docman_plugin, te);
1308 if (highlight_submenu)
1310 highlight_menu =
1311 gtk_ui_manager_get_widget (GTK_UI_MANAGER (docman_plugin->ui),
1312 "/MenuMain/MenuView/MenuViewEditor/MenuFormatStyle");
1313 gtk_menu_item_set_submenu (GTK_MENU_ITEM (highlight_menu),
1314 highlight_submenu);
1318 /* Emit document-added signal */
1319 g_signal_emit_by_name (docman_plugin, "document-added", doc);
1322 static gboolean
1323 on_window_key_press_event (AnjutaShell *shell,
1324 GdkEventKey *event,
1325 DocmanPlugin *plugin)
1327 gint i;
1329 g_return_val_if_fail (event != NULL, FALSE);
1331 for (i = 0; global_keymap[i].id; i++)
1332 if (event->keyval == global_keymap[i].gdk_key &&
1333 (event->state & global_keymap[i].modifiers) == global_keymap[i].modifiers)
1334 break;
1336 if (!global_keymap[i].id)
1337 return FALSE;
1339 if (global_keymap[i].id >= ID_FIRSTBUFFER &&
1340 global_keymap[i].id <= (ID_FIRSTBUFFER + 9))
1342 gint page_req = global_keymap[i].id - ID_FIRSTBUFFER;
1344 if (!anjuta_docman_set_page (ANJUTA_DOCMAN(plugin->docman), page_req))
1345 return FALSE;
1347 else
1348 return FALSE;
1350 /* Note: No reason for a shortcut to do more than one thing a time */
1351 g_signal_stop_emission_by_name (G_OBJECT (ANJUTA_PLUGIN(plugin)->shell),
1352 "key-press-event");
1354 return TRUE;
1357 static gboolean
1358 add_new_default_document (gpointer data)
1360 IAnjutaDocument *page;
1362 page = anjuta_docman_get_current_document (ANJUTA_DOCMAN (data));
1363 if (page == NULL)
1365 anjuta_docman_add_editor (ANJUTA_DOCMAN (data), NULL, NULL);
1368 return FALSE;
1371 static void
1372 on_session_load (AnjutaShell *shell, AnjutaSessionPhase phase,
1373 AnjutaSession *session, DocmanPlugin *plugin)
1375 if (phase == ANJUTA_SESSION_PHASE_END)
1377 /* Add a empty buffer if the document manager is used "Standalone" */
1378 AnjutaPluginManager *plugin_manager;
1379 AnjutaPluginHandle *handle;
1380 AnjutaPluginDescription *desc;
1381 gboolean standalone = FALSE;
1383 plugin_manager = anjuta_shell_get_plugin_manager (ANJUTA_PLUGIN (plugin)->shell, NULL);
1384 handle = anjuta_plugin_manager_get_plugin_handle (plugin_manager, G_OBJECT (plugin));
1385 desc = anjuta_plugin_handle_get_description (handle);
1387 anjuta_plugin_description_get_boolean (desc, "Configuration", "Standalone", &standalone);
1388 if (standalone)
1390 g_idle_add (add_new_default_document, plugin->docman);
1394 if (phase != ANJUTA_SESSION_PHASE_NORMAL)
1395 return;
1397 anjuta_bookmarks_session_load (ANJUTA_BOOKMARKS (plugin->bookmarks),
1398 session);
1400 search_box_session_load (SEARCH_BOX (plugin->search_box),
1401 session);
1404 static void
1405 on_session_save (AnjutaShell *shell, AnjutaSessionPhase phase,
1406 AnjutaSession *session, DocmanPlugin *plugin)
1408 GList *docwids, *node, *files;
1410 if (phase != ANJUTA_SESSION_PHASE_NORMAL)
1411 return;
1413 files = anjuta_session_get_string_list (session, "File Loader", "Files"); /* probably NULL */
1414 /* buffers list is ordered last-opened to first-opened */
1415 docwids = anjuta_docman_get_all_doc_widgets (ANJUTA_DOCMAN (plugin->docman));
1416 if (docwids)
1418 for (node = docwids; node != NULL; node = g_list_next (node))
1420 /* only editor-documents are logged here. glade files etc handled elsewhere */
1421 if (IANJUTA_IS_EDITOR (node->data))
1423 IAnjutaEditor *te;
1424 GFile* file;
1426 te = IANJUTA_EDITOR (node->data);
1427 file = ianjuta_file_get_file (IANJUTA_FILE (te), NULL);
1428 if (file)
1430 /* Save line locations also */
1431 gchar *line_number;
1433 line_number = g_strdup_printf ("%d", ianjuta_editor_get_lineno (te, NULL));
1434 files = g_list_prepend (files, anjuta_session_get_relative_uri_from_file (session, file, line_number));
1435 g_free (line_number);
1437 /* Close document */
1438 anjuta_docman_remove_document (ANJUTA_DOCMAN (plugin->docman), IANJUTA_DOCUMENT (node->data));
1441 g_list_free (docwids);
1443 if (files)
1445 anjuta_session_set_string_list (session, "File Loader", "Files", files);
1446 g_list_foreach (files, (GFunc)g_free, NULL);
1447 g_list_free (files);
1450 anjuta_bookmarks_session_save (ANJUTA_BOOKMARKS (plugin->bookmarks),
1451 session);
1453 search_box_session_save (SEARCH_BOX (plugin->search_box), session);
1456 static gboolean
1457 on_save_prompt_save_editor (AnjutaSavePrompt *save_prompt,
1458 gpointer item, gpointer user_data)
1460 DocmanPlugin *plugin;
1462 plugin = ANJUTA_PLUGIN_DOCMAN (user_data);
1463 return anjuta_docman_save_document (ANJUTA_DOCMAN (plugin->docman),
1464 IANJUTA_DOCUMENT (item),
1465 GTK_WIDGET (save_prompt));
1468 static void
1469 on_save_prompt (AnjutaShell *shell, AnjutaSavePrompt *save_prompt,
1470 DocmanPlugin *plugin)
1472 GList *buffers, *node;
1474 buffers = anjuta_docman_get_all_doc_widgets (ANJUTA_DOCMAN (plugin->docman));
1475 if (buffers)
1477 for (node = buffers; node; node = g_list_next (node))
1479 IAnjutaFileSavable *editor = IANJUTA_FILE_SAVABLE (node->data);
1480 if (ianjuta_file_savable_is_dirty (editor, NULL))
1482 const gchar *name;
1483 gchar *uri = NULL;
1484 GFile* file;
1486 name = ianjuta_document_get_filename (IANJUTA_DOCUMENT (editor), NULL);
1487 file = ianjuta_file_get_file (IANJUTA_FILE (editor), NULL);
1488 if (file)
1490 uri = g_file_get_uri (file);
1491 g_object_unref (file);
1493 anjuta_save_prompt_add_item (save_prompt, name, uri, editor,
1494 on_save_prompt_save_editor, plugin);
1495 g_free (uri);
1498 g_list_free (buffers);
1502 static void
1503 on_notify_prefs (AnjutaPreferences* prefs,
1504 const gchar* key, gpointer user_data)
1506 DocmanPlugin *ep = ANJUTA_PLUGIN_DOCMAN (user_data);
1507 docman_plugin_set_tab_pos (ep);
1510 static gboolean
1511 on_docman_auto_save (gpointer data)
1513 gboolean retval;
1514 DocmanPlugin *plugin;
1515 AnjutaDocman *docman;
1516 AnjutaStatus* status;
1517 IAnjutaDocument *doc;
1518 GList *buffers, *node;
1520 plugin = ANJUTA_PLUGIN_DOCMAN (data);
1521 docman = ANJUTA_DOCMAN (plugin->docman);
1522 if (!docman)
1523 return FALSE;
1525 if (g_settings_get_boolean (plugin->settings, SAVE_AUTOMATIC) == FALSE)
1527 plugin->autosave_on = FALSE;
1528 return FALSE;
1531 status = anjuta_shell_get_status (docman->shell, NULL);
1533 retval = TRUE;
1534 buffers = anjuta_docman_get_all_doc_widgets (docman);
1535 if (buffers)
1537 for (node = buffers; node != NULL; node = g_list_next (node))
1539 doc = IANJUTA_DOCUMENT (node->data);
1540 if (ianjuta_file_savable_is_dirty (IANJUTA_FILE_SAVABLE (doc), NULL) &&
1541 !ianjuta_file_savable_is_conflict (IANJUTA_FILE_SAVABLE (doc), NULL))
1543 GFile* file = ianjuta_file_get_file (IANJUTA_FILE (doc), NULL);
1544 if (file)
1546 GError *err = NULL;
1547 g_object_unref (file);
1549 ianjuta_file_savable_save (IANJUTA_FILE_SAVABLE (doc), &err);
1550 if (err)
1552 gchar *fullmsg;
1553 const gchar *filename = ianjuta_document_get_filename (doc, NULL); /* this may fail, too */
1554 fullmsg = g_strdup_printf (_("Autosave failed for %s"), filename);
1555 anjuta_status (status, fullmsg, 3);
1556 g_free (fullmsg);
1557 g_error_free (err);
1558 retval = FALSE;
1563 g_list_free (buffers);
1566 if (retval)
1568 anjuta_status (status, _("Autosave completed"), 3);
1570 return retval;
1573 static void
1574 on_notify_timer (GSettings* settings,
1575 const gchar* key,
1576 gpointer user_data)
1578 DocmanPlugin *plugin;
1579 gint auto_save_timer;
1580 gboolean auto_save;
1582 plugin = ANJUTA_PLUGIN_DOCMAN (user_data);
1584 auto_save_timer = g_settings_get_int(settings, AUTOSAVE_TIMER);
1585 auto_save = g_settings_get_boolean(settings, SAVE_AUTOMATIC);
1587 if (auto_save)
1589 if (plugin->autosave_on)
1591 if (auto_save_timer != plugin->autosave_it)
1593 g_source_remove (plugin->autosave_id);
1594 plugin->autosave_id =
1595 g_timeout_add_seconds (auto_save_timer * 60,
1596 on_docman_auto_save, plugin);
1599 else
1601 plugin->autosave_id =
1602 g_timeout_add_seconds (auto_save_timer * 60,
1603 on_docman_auto_save, plugin);
1605 plugin->autosave_it = auto_save_timer;
1606 plugin->autosave_on = TRUE;
1608 else if (plugin->autosave_on)
1610 g_source_remove (plugin->autosave_id);
1611 plugin->autosave_on = FALSE;
1615 static void
1616 prefs_init (DocmanPlugin *ep)
1618 docman_plugin_set_tab_pos (ep);
1620 g_signal_connect (ep->settings, "changed::" EDITOR_SHOW_DROP_DOWN,
1621 G_CALLBACK (on_notify_prefs), ep);
1622 g_signal_connect (ep->settings, "changed::" EDITOR_TABS_HIDE,
1623 G_CALLBACK (on_notify_prefs), ep);
1624 g_signal_connect (ep->settings, "changed::" EDITOR_TABS_POS,
1625 G_CALLBACK (on_notify_prefs), ep);
1626 g_signal_connect (ep->settings, "changed::" AUTOSAVE_TIMER,
1627 G_CALLBACK (on_notify_timer), ep);
1628 g_signal_connect (ep->settings, "changed::" SAVE_AUTOMATIC,
1629 G_CALLBACK (on_notify_timer), ep);
1631 on_notify_timer(ep->settings, NULL, ep);
1634 static gboolean
1635 activate_plugin (AnjutaPlugin *plugin)
1637 GtkWidget *docman, *popup_menu;
1638 AnjutaUI *ui;
1639 DocmanPlugin *dplugin;
1640 GtkActionGroup *group;
1641 gint i;
1642 static gboolean initialized = FALSE;
1643 GList *actions, *act;
1645 DEBUG_PRINT ("%s", "DocmanPlugin: Activating document manager plugin…");
1647 dplugin = ANJUTA_PLUGIN_DOCMAN (plugin);
1648 dplugin->ui = anjuta_shell_get_ui (plugin->shell, NULL);
1650 ui = dplugin->ui;
1651 docman = anjuta_docman_new (dplugin);
1652 gtk_widget_show (docman);
1653 dplugin->docman = docman;
1655 ANJUTA_DOCMAN(docman)->shell = anjuta_plugin_get_shell(plugin);
1656 g_signal_connect (G_OBJECT (docman), "document-added",
1657 G_CALLBACK (on_document_added), plugin);
1658 g_signal_connect (G_OBJECT (docman), "document-removed",
1659 G_CALLBACK (on_document_removed), plugin);
1660 g_signal_connect (G_OBJECT (docman), "document-changed",
1661 G_CALLBACK (on_document_changed), plugin);
1662 g_signal_connect (G_OBJECT (plugin->shell), "key-press-event",
1663 G_CALLBACK (on_window_key_press_event), plugin);
1665 if (!initialized)
1667 register_stock_icons (plugin);
1669 dplugin->action_groups = NULL;
1670 /* Add all our editor actions */
1671 for (i = 0; i < G_N_ELEMENTS (action_groups); i++)
1673 group = anjuta_ui_add_action_group_entries (ui,
1674 action_groups[i].name,
1675 _(action_groups[i].label),
1676 action_groups[i].group,
1677 action_groups[i].size,
1678 GETTEXT_PACKAGE, TRUE,
1679 plugin);
1680 dplugin->action_groups =
1681 g_list_prepend (dplugin->action_groups, group);
1682 actions = gtk_action_group_list_actions (group);
1683 act = actions;
1684 while (act) {
1685 g_object_set_data (G_OBJECT (act->data), "Plugin", dplugin);
1686 act = g_list_next (act);
1689 for (i = 0; i < G_N_ELEMENTS (action_toggle_groups); i++)
1691 group = anjuta_ui_add_toggle_action_group_entries (ui,
1692 action_toggle_groups[i].name,
1693 _(action_toggle_groups[i].label),
1694 action_toggle_groups[i].group,
1695 action_toggle_groups[i].size,
1696 GETTEXT_PACKAGE, TRUE, plugin);
1697 dplugin->action_groups =
1698 g_list_prepend (dplugin->action_groups, group);
1699 actions = gtk_action_group_list_actions (group);
1700 act = actions;
1701 while (act) {
1702 g_object_set_data (G_OBJECT (act->data), "Plugin", dplugin);
1703 act = g_list_next (act);
1707 /* Add UI */
1708 dplugin->uiid = anjuta_ui_merge (ui, UI_FILE);
1709 dplugin->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
1710 gtk_widget_show (dplugin->vbox);
1711 gtk_box_pack_start (GTK_BOX(dplugin->vbox), docman, TRUE, TRUE, 0);
1713 /* Create the default searchbox instance. */
1714 dplugin->search_box = search_box_new (ANJUTA_DOCMAN (docman));
1715 gtk_box_pack_end (GTK_BOX (dplugin->vbox), dplugin->search_box, FALSE, FALSE, 0);
1717 anjuta_shell_add_widget_full (plugin->shell, dplugin->vbox,
1718 "AnjutaDocumentManager", _("Documents"),
1719 "editor-plugin-icon",
1720 ANJUTA_SHELL_PLACEMENT_CENTER,
1721 TRUE, NULL);
1722 anjuta_shell_present_widget (plugin->shell, dplugin->vbox, NULL);
1724 ui_give_shorter_names (plugin);
1725 update_document_ui (plugin, NULL);
1727 /* Setup popup menu */
1728 popup_menu = gtk_ui_manager_get_widget (GTK_UI_MANAGER (ui),
1729 "/PopupDocumentManager");
1730 g_assert (popup_menu != NULL && GTK_IS_MENU (popup_menu));
1731 anjuta_docman_set_popup_menu (ANJUTA_DOCMAN (docman), popup_menu);
1734 /* Connect to save session */
1735 g_signal_connect (G_OBJECT (plugin->shell), "save-session",
1736 G_CALLBACK (on_session_save), plugin);
1737 /* Connect to load session */
1738 g_signal_connect (G_OBJECT (plugin->shell), "load-session",
1739 G_CALLBACK (on_session_load), plugin);
1741 /* Connect to save prompt */
1742 g_signal_connect (G_OBJECT (plugin->shell), "save-prompt",
1743 G_CALLBACK (on_save_prompt), plugin);
1745 /* Add bookmarks widget */
1746 dplugin->bookmarks = G_OBJECT(anjuta_bookmarks_new (dplugin));
1748 dplugin->project_watch_id =
1749 anjuta_plugin_add_watch (plugin, IANJUTA_PROJECT_MANAGER_PROJECT_ROOT_URI,
1750 value_added_project_root_uri,
1751 value_removed_project_root_uri, NULL);
1752 dplugin->project_name = NULL;
1754 prefs_init(ANJUTA_PLUGIN_DOCMAN (plugin));
1755 initialized = TRUE;
1757 return TRUE;
1760 static gboolean
1761 deactivate_plugin (AnjutaPlugin *plugin)
1763 // GtkIconFactory *icon_factory;
1764 DocmanPlugin *eplugin;
1765 AnjutaUI *ui;
1766 GList *node;
1768 DEBUG_PRINT ("%s", "DocmanPlugin: Deactivating document manager plugin…");
1770 eplugin = ANJUTA_PLUGIN_DOCMAN (plugin);
1772 g_signal_handlers_disconnect_by_func (G_OBJECT (plugin->shell),
1773 G_CALLBACK (on_session_save), plugin);
1774 g_signal_handlers_disconnect_by_func (G_OBJECT (plugin->shell),
1775 G_CALLBACK (on_save_prompt), plugin);
1777 ui = anjuta_shell_get_ui (plugin->shell, NULL);
1779 g_signal_handlers_disconnect_by_func (G_OBJECT (eplugin->docman),
1780 G_CALLBACK (on_document_changed),
1781 plugin);
1782 g_signal_handlers_disconnect_by_func (G_OBJECT (plugin->shell),
1783 G_CALLBACK (on_window_key_press_event),
1784 plugin);
1786 /* Remove autosave timeout if one is scheduled. */
1787 if (eplugin->autosave_on)
1789 g_source_remove (eplugin->autosave_id);
1790 eplugin->autosave_on = FALSE;
1793 on_document_changed (ANJUTA_DOCMAN (eplugin->docman), NULL, plugin);
1795 /* Widget is removed from the container when destroyed */
1796 gtk_widget_destroy (eplugin->docman);
1797 g_object_unref (eplugin->bookmarks);
1798 anjuta_ui_unmerge (ui, eplugin->uiid);
1799 node = eplugin->action_groups;
1800 while (node)
1802 GtkActionGroup *group = GTK_ACTION_GROUP (node->data);
1803 anjuta_ui_remove_action_group (ui, group);
1804 node = g_list_next (node);
1806 g_list_free (eplugin->action_groups);
1808 /* FIXME: */
1809 /* Unregister stock icons */
1810 /* Unregister preferences */
1812 eplugin->docman = NULL;
1813 eplugin->uiid = 0;
1814 eplugin->action_groups = NULL;
1816 return TRUE;
1819 static void
1820 dispose (GObject *obj)
1822 DocmanPlugin *eplugin = ANJUTA_PLUGIN_DOCMAN (obj);
1824 g_object_unref (eplugin->settings);
1826 G_OBJECT_CLASS (parent_class)->dispose (obj);
1829 static void
1830 finalize (GObject *obj)
1832 /* Finalization codes here */
1833 G_OBJECT_CLASS (parent_class)->finalize (obj);
1836 static void
1837 docman_plugin_instance_init (GObject *obj)
1839 DocmanPlugin *plugin = ANJUTA_PLUGIN_DOCMAN (obj);
1840 plugin->uiid = 0;
1841 plugin->notify_ids = NULL;
1842 plugin->support_plugins = NULL;
1843 plugin->settings = g_settings_new (PREF_SCHEMA);
1846 static void
1847 docman_plugin_class_init (GObjectClass *klass)
1849 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
1851 parent_class = g_type_class_peek_parent (klass);
1853 plugin_class->activate = activate_plugin;
1854 plugin_class->deactivate = deactivate_plugin;
1855 klass->dispose = dispose;
1856 klass->finalize = finalize;
1859 /* Implement IAnjutaDocumentManager interfaces */
1860 static GFile*
1861 ianjuta_docman_get_file (IAnjutaDocumentManager *plugin,
1862 const gchar *filename, GError **e)
1864 AnjutaDocman *docman;
1865 docman = ANJUTA_DOCMAN ((ANJUTA_PLUGIN_DOCMAN (plugin)->docman));
1866 return anjuta_docman_get_file (docman, filename);
1869 static IAnjutaDocument*
1870 ianjuta_docman_get_document_for_file (IAnjutaDocumentManager *plugin,
1871 GFile* file, GError **e)
1873 AnjutaDocman *docman;
1874 docman = ANJUTA_DOCMAN ((ANJUTA_PLUGIN_DOCMAN (plugin)->docman));
1875 return anjuta_docman_get_document_for_file (docman, file);
1878 static IAnjutaDocument*
1879 ianjuta_docman_get_current_document (IAnjutaDocumentManager *plugin, GError **e)
1881 AnjutaDocman *docman;
1882 docman = ANJUTA_DOCMAN ((ANJUTA_PLUGIN_DOCMAN (plugin)->docman));
1883 return anjuta_docman_get_current_document (docman);
1886 static void
1887 ianjuta_docman_set_current_document (IAnjutaDocumentManager *plugin,
1888 IAnjutaDocument *doc, GError **e)
1890 AnjutaDocman *docman;
1891 docman = ANJUTA_DOCMAN ((ANJUTA_PLUGIN_DOCMAN (plugin)->docman));
1892 anjuta_docman_set_current_document (docman, doc);
1895 static GList*
1896 ianjuta_docman_get_doc_widgets (IAnjutaDocumentManager *plugin, GError **e)
1898 AnjutaDocman *docman;
1899 docman = ANJUTA_DOCMAN ((ANJUTA_PLUGIN_DOCMAN (plugin)->docman));
1900 return anjuta_docman_get_all_doc_widgets (docman);
1903 static IAnjutaEditor*
1904 ianjuta_docman_goto_file_line (IAnjutaDocumentManager *plugin,
1905 GFile* file, gint linenum, GError **e)
1907 AnjutaDocman *docman;
1908 docman = ANJUTA_DOCMAN ((ANJUTA_PLUGIN_DOCMAN (plugin)->docman));
1909 return anjuta_docman_goto_file_line (docman, file, linenum);
1912 static IAnjutaEditor*
1913 ianjuta_docman_goto_file_line_mark (IAnjutaDocumentManager *plugin,
1914 GFile* file, gint linenum, gboolean mark, GError **e)
1916 AnjutaDocman *docman;
1917 docman = ANJUTA_DOCMAN ((ANJUTA_PLUGIN_DOCMAN (plugin)->docman));
1918 return anjuta_docman_goto_file_line_mark (docman, file, linenum, mark);
1922 * anjuta_docman_add_buffer:
1923 * @plugin:
1924 * @filename:
1925 * @content file text, a 0-terminated utf-8 string
1926 * @r: store for pointer to error data struct
1928 * Return value: the new editor
1930 static IAnjutaEditor*
1931 ianjuta_docman_add_buffer (IAnjutaDocumentManager *plugin,
1932 const gchar* filename, const gchar *content,
1933 GError **e)
1935 AnjutaDocman *docman;
1936 IAnjutaEditor *te;
1937 docman = ANJUTA_DOCMAN ((ANJUTA_PLUGIN_DOCMAN (plugin)->docman));
1938 te = anjuta_docman_add_editor (docman, NULL, filename);
1939 if (te)
1941 if (content != NULL && *content != '\0')
1942 ianjuta_editor_append (te, content, -1, NULL);
1944 return IANJUTA_EDITOR (te);
1946 return NULL;
1949 static void
1950 ianjuta_docman_add_document (IAnjutaDocumentManager *plugin,
1951 IAnjutaDocument* document,
1952 GError **e)
1954 AnjutaDocman *docman;
1955 docman = ANJUTA_DOCMAN ((ANJUTA_PLUGIN_DOCMAN (plugin)->docman));
1957 anjuta_docman_add_document(docman, document, NULL);
1960 static gboolean
1961 ianjuta_docman_remove_document(IAnjutaDocumentManager *plugin,
1962 IAnjutaDocument *doc,
1963 gboolean save_before, GError **e)
1965 gboolean ret_val;
1966 AnjutaDocman *docman;
1968 docman = ANJUTA_DOCMAN ((ANJUTA_PLUGIN_DOCMAN (plugin)->docman));
1970 if (save_before)
1972 ret_val = anjuta_docman_save_document(docman, doc,
1973 GTK_WIDGET (ANJUTA_PLUGIN (plugin)->shell));
1975 else
1976 ret_val = TRUE;
1978 if (ret_val)
1979 anjuta_docman_remove_document (docman, doc);
1981 return ret_val;
1984 static void
1985 ianjuta_docman_add_bookmark (IAnjutaDocumentManager* plugin,
1986 GFile* file,
1987 gint line,
1988 GError **e)
1990 AnjutaBookmarks* bookmarks = ANJUTA_BOOKMARKS (ANJUTA_PLUGIN_DOCMAN(plugin)->bookmarks);
1991 anjuta_bookmarks_add_file (bookmarks, file, line, NULL);
1994 static void
1995 ianjuta_document_manager_iface_init (IAnjutaDocumentManagerIface *iface)
1997 iface->add_buffer = ianjuta_docman_add_buffer;
1998 iface->add_document = ianjuta_docman_add_document;
1999 iface->find_document_with_file = ianjuta_docman_get_document_for_file;
2000 iface->get_current_document = ianjuta_docman_get_current_document;
2001 iface->get_doc_widgets = ianjuta_docman_get_doc_widgets;
2002 iface->get_file = ianjuta_docman_get_file;
2003 iface->goto_file_line = ianjuta_docman_goto_file_line;
2004 iface->goto_file_line_mark = ianjuta_docman_goto_file_line_mark;
2005 iface->remove_document = ianjuta_docman_remove_document;
2006 iface->set_current_document = ianjuta_docman_set_current_document;
2007 iface->add_bookmark = ianjuta_docman_add_bookmark;
2010 /* Implement IAnjutaFile interface */
2011 static void
2012 ifile_open (IAnjutaFile* plugin, GFile* file, GError** e)
2014 AnjutaDocman *docman;
2016 docman = ANJUTA_DOCMAN ((ANJUTA_PLUGIN_DOCMAN (plugin)->docman));
2017 anjuta_docman_goto_file_line (docman, file, -1);
2020 static GFile*
2021 ifile_get_file (IAnjutaFile* plugin, GError** e)
2023 AnjutaDocman *docman;
2024 IAnjutaDocument *doc;
2026 docman = ANJUTA_DOCMAN ((ANJUTA_PLUGIN_DOCMAN (plugin)->docman));
2027 doc = anjuta_docman_get_current_document (docman);
2028 if (doc != NULL)
2029 return ianjuta_file_get_file (IANJUTA_FILE (doc), NULL);
2030 else
2031 return NULL;
2034 static void
2035 ifile_iface_init (IAnjutaFileIface *iface)
2037 iface->open = ifile_open;
2038 iface->get_file = ifile_get_file;
2041 /* Implement IAnjutaFileSavable interface */
2042 static void
2043 isaveable_save (IAnjutaFileSavable* plugin, GError** e)
2045 /* Save all buffers whose path is known */
2046 AnjutaDocman *docman;
2047 GList *docwids, *node;
2049 docman = ANJUTA_DOCMAN ((ANJUTA_PLUGIN_DOCMAN (plugin)->docman));
2050 docwids = anjuta_docman_get_all_doc_widgets (docman);
2051 if (docwids)
2053 for (node = docwids; node != NULL; node = g_list_next (node))
2055 IAnjutaDocument *doc;
2056 doc = IANJUTA_DOCUMENT (node->data);
2057 /* known-uri test occurs downstream */
2058 /* CHECKME test here and perform save-as when appropriate ? */
2059 if (ianjuta_file_savable_is_dirty (IANJUTA_FILE_SAVABLE (doc), NULL))
2061 ianjuta_file_savable_save (IANJUTA_FILE_SAVABLE (doc), NULL);
2064 g_list_free (docwids);
2068 static void
2069 isavable_save_as (IAnjutaFileSavable* plugin, GFile* file, GError** e)
2071 DEBUG_PRINT("%s", "save_as: Not implemented in DocmanPlugin");
2074 static gboolean
2075 isavable_is_dirty (IAnjutaFileSavable* plugin, GError **e)
2077 /* Is any editor unsaved */
2078 AnjutaDocman *docman;
2079 IAnjutaDocument *doc;
2080 GList *buffers, *node;
2081 gboolean retval;
2083 retval = FALSE;
2084 docman = ANJUTA_DOCMAN ((ANJUTA_PLUGIN_DOCMAN (plugin)->docman));
2085 buffers = anjuta_docman_get_all_doc_widgets (docman);
2086 if (buffers)
2088 for (node = buffers; node; node = g_list_next (node))
2090 doc = IANJUTA_DOCUMENT (node->data);
2091 if (ianjuta_file_savable_is_dirty (IANJUTA_FILE_SAVABLE (doc), NULL))
2093 retval = TRUE;
2094 break;
2097 g_list_free (buffers);
2099 return retval;
2102 static void
2103 isavable_set_dirty(IAnjutaFileSavable* plugin, gboolean dirty, GError** e)
2105 DEBUG_PRINT("%s", "set_dirty: Not implemented in DocmanPlugin");
2108 static void
2109 isavable_iface_init (IAnjutaFileSavableIface *iface)
2111 iface->save = isaveable_save;
2112 iface->save_as = isavable_save_as;
2113 iface->set_dirty = isavable_set_dirty;
2114 iface->is_dirty = isavable_is_dirty;
2117 static void
2118 ipreferences_merge(IAnjutaPreferences* ipref, AnjutaPreferences* prefs, GError** e)
2120 GError* error = NULL;
2121 GtkBuilder* bxml = gtk_builder_new ();
2122 DocmanPlugin* doc_plugin = ANJUTA_PLUGIN_DOCMAN (ipref);
2123 GObject *show_tabs_radio, *tabs_settings_box;
2125 /* Add preferences */
2126 if (!gtk_builder_add_from_file (bxml, PREFS_BUILDER, &error))
2128 g_warning ("Couldn't load builder file: %s", error->message);
2129 g_error_free (error);
2132 show_tabs_radio = gtk_builder_get_object (bxml, "show-tabs-radio");
2133 tabs_settings_box = gtk_builder_get_object (bxml, "tabs-settings-box");
2134 g_object_bind_property (show_tabs_radio, "active", tabs_settings_box, "sensitive", 0);
2136 anjuta_preferences_add_from_builder (prefs, bxml,
2137 doc_plugin->settings,
2138 "Documents", _("Documents"), ICON_FILE);
2140 g_object_unref (G_OBJECT (bxml));
2143 static void
2144 ipreferences_unmerge(IAnjutaPreferences* ipref, AnjutaPreferences* prefs, GError** e)
2146 /* DocmanPlugin* plugin = ANJUTA_PLUGIN_DOCMAN (ipref);*/
2147 anjuta_preferences_remove_page(prefs, _("Documents"));
2150 static void
2151 ipreferences_iface_init(IAnjutaPreferencesIface* iface)
2153 iface->merge = ipreferences_merge;
2154 iface->unmerge = ipreferences_unmerge;
2157 ANJUTA_PLUGIN_BEGIN (DocmanPlugin, docman_plugin);
2158 ANJUTA_PLUGIN_ADD_INTERFACE(ianjuta_document_manager, IANJUTA_TYPE_DOCUMENT_MANAGER);
2159 ANJUTA_PLUGIN_ADD_INTERFACE(ifile, IANJUTA_TYPE_FILE);
2160 ANJUTA_PLUGIN_ADD_INTERFACE(isavable, IANJUTA_TYPE_FILE_SAVABLE);
2161 ANJUTA_PLUGIN_ADD_INTERFACE(ipreferences, IANJUTA_TYPE_PREFERENCES);
2162 ANJUTA_PLUGIN_END;
2164 ANJUTA_SIMPLE_PLUGIN (DocmanPlugin, docman_plugin);