1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) Johannes Schmid 2006 <jhs@cvs.gnome.org>
6 * plugin.c is free software.
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
13 * plugin.c is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with plugin.c. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
26 #include <libanjuta/anjuta-shell.h>
27 #include <libanjuta/anjuta-debug.h>
28 #include <libanjuta/anjuta-tabber.h>
29 #include <libanjuta/interfaces/ianjuta-document-manager.h>
30 #include <libanjuta/interfaces/ianjuta-editor.h>
31 #include <libanjuta/interfaces/ianjuta-help.h>
35 #ifndef DISABLE_EMBEDDED_DEVHELP
38 #include <webkit2/webkit2.h>
40 #include <webkit/webkit.h>
43 #define ONLINE_API_DOCS "http://library.gnome.org/devel"
45 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-devhelp.ui"
47 #else /* DISABLE_EMBEDDED_DEVHELP */
49 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-devhelp-simple.ui"
51 #endif /* DISABLE_EMBEDDED_DEVHELP */
53 static gpointer parent_class
;
55 #ifndef DISABLE_EMBEDDED_DEVHELP
57 #define ANJUTA_PIXMAP_DEVHELP "anjuta-devhelp-plugin-48.png"
58 #define ANJUTA_PIXMAP_DEVHELP_SEARCH "anjuta-devhelp-search"
59 #define ANJUTA_PIXMAP_DEVHELP_VIEW "anjuta-devhelp-view"
62 #define ANJUTA_STOCK_DEVHELP "anjuta-devhelp"
63 #define ANJUTA_STOCK_DEVHELP_SEARCH "anjuta-devhelp-search"
64 #define ANJUTA_STOCK_DEVHELP_VIEW "anjuta-devhelp-view"
67 register_stock_icons (AnjutaPlugin
*plugin
)
69 static gboolean registered
= FALSE
;
75 /* Register stock icons */
76 BEGIN_REGISTER_ICON (plugin
);
77 REGISTER_ICON (ANJUTA_PIXMAP_DEVHELP
, ANJUTA_STOCK_DEVHELP
);
78 REGISTER_ICON_FULL (ANJUTA_PIXMAP_DEVHELP_VIEW
, ANJUTA_STOCK_DEVHELP_VIEW
);
79 REGISTER_ICON_FULL (ANJUTA_PIXMAP_DEVHELP_SEARCH
, ANJUTA_STOCK_DEVHELP_SEARCH
);
84 devhelp_sidebar_link_selected_cb (GObject
*ignored
, DhLink
*link
,
85 AnjutaDevhelp
*widget
)
89 anjuta_shell_present_widget (ANJUTA_PLUGIN (widget
)->shell
,
90 widget
->present_widget
, NULL
);
92 uri
= dh_link_get_uri (link
);
94 webkit_web_view_load_uri (WEBKIT_WEB_VIEW (widget
->view
), uri
);
96 webkit_web_view_open (WEBKIT_WEB_VIEW (widget
->view
), uri
);
100 anjuta_devhelp_check_history (widget
);
104 on_go_back_clicked (GtkWidget
*widget
, AnjutaDevhelp
*plugin
)
106 anjuta_shell_present_widget (ANJUTA_PLUGIN (plugin
)->shell
,
107 plugin
->present_widget
, NULL
);
109 webkit_web_view_go_back (WEBKIT_WEB_VIEW (plugin
->view
));
111 anjuta_devhelp_check_history (plugin
);
115 on_go_forward_clicked (GtkWidget
*widget
, AnjutaDevhelp
*plugin
)
117 anjuta_shell_present_widget (ANJUTA_PLUGIN (plugin
)->shell
,
118 plugin
->present_widget
, NULL
);
120 webkit_web_view_go_forward (WEBKIT_WEB_VIEW (plugin
->view
));
122 anjuta_devhelp_check_history (plugin
);
126 on_online_clicked (GtkWidget
* widget
, AnjutaDevhelp
* plugin
)
128 anjuta_shell_present_widget (ANJUTA_PLUGIN (plugin
)->shell
,
129 plugin
->present_widget
, NULL
);
131 webkit_web_view_load_uri (WEBKIT_WEB_VIEW (plugin
->view
),
134 webkit_web_view_open (WEBKIT_WEB_VIEW (plugin
->view
),
137 anjuta_devhelp_check_history (plugin
);
141 api_reference_idle (AnjutaDevhelp
* plugin
)
143 anjuta_shell_present_widget (ANJUTA_PLUGIN (plugin
)->shell
,
144 plugin
->sidebar
, NULL
);
148 #else /* DISABLE_EMBEDDED_DEVHELP */
151 api_reference_idle (AnjutaDevhelp
* plugin
)
153 ianjuta_help_search (IANJUTA_HELP (plugin
), "", NULL
);
160 on_api_reference_activate (GtkAction
*action
, AnjutaDevhelp
*plugin
)
162 g_idle_add((GSourceFunc
) api_reference_idle
, plugin
);
166 context_idle (AnjutaDevhelp
* plugin
)
168 IAnjutaEditor
*editor
;
171 if(plugin
->editor
== NULL
)
174 editor
= IANJUTA_EDITOR (plugin
->editor
);
175 current_word
= ianjuta_editor_get_current_word (editor
, NULL
);
178 ianjuta_help_search (IANJUTA_HELP (plugin
), current_word
, NULL
);
179 g_free (current_word
);
185 on_context_help_activate (GtkAction
*action
, AnjutaDevhelp
*plugin
)
187 g_idle_add((GSourceFunc
) context_idle
, plugin
);
191 on_search_help_activate (GtkAction
*action
, AnjutaDevhelp
*plugin
)
195 gchar
*search_term
= NULL
;
197 parent
= GTK_WINDOW (ANJUTA_PLUGIN (plugin
)->shell
);
199 anjuta_util_dialog_input (parent
, _("Search Help:"),
203 if (search_term
&& strlen (search_term
) > 0)
205 ianjuta_help_search (IANJUTA_HELP (plugin
), search_term
, NULL
);
208 g_free (search_term
);
211 /* Action definitions */
212 static GtkActionEntry actions
[] = {
214 #ifndef DISABLE_EMBEDDED_DEVHELP
225 #endif /* DISABLE_EMBEDDED_DEVHELP */
229 N_("_API Reference"),
231 N_("Browse API Pages"),
232 G_CALLBACK (on_api_reference_activate
)
236 #ifndef DISABLE_EMBEDDED_DEVHELP
237 ANJUTA_STOCK_DEVHELP_VIEW
,
240 #endif /* DISABLE_EMBEDDED_DEVHELP */
243 N_("Search help for the current word in the editor"),
244 G_CALLBACK (on_context_help_activate
)
248 #ifndef DISABLE_EMBEDDED_DEVHELP
249 ANJUTA_STOCK_DEVHELP_SEARCH
,
255 N_("Search for a term in help"),
256 G_CALLBACK (on_search_help_activate
)
260 /* Watches callbacks */
263 value_added_current_editor (AnjutaPlugin
*plugin
, const gchar
*name
,
264 const GValue
*value
, gpointer data
)
268 AnjutaDevhelp
* devhelp
= ANJUTA_PLUGIN_DEVHELP (data
);
270 editor
= g_value_get_object (value
);
271 if (!IANJUTA_IS_EDITOR (editor
))
273 devhelp
->editor
= IANJUTA_EDITOR(editor
);
274 action
= gtk_action_group_get_action (devhelp
->action_group
,
275 "ActionHelpContext");
276 g_object_set (action
, "sensitive", TRUE
, NULL
);
280 value_removed_current_editor (AnjutaPlugin
*plugin
,
281 const gchar
*name
, gpointer data
)
284 AnjutaDevhelp
* devhelp
= ANJUTA_PLUGIN_DEVHELP (data
);
286 devhelp
->editor
= NULL
;
287 action
= gtk_action_group_get_action (devhelp
->action_group
,
288 "ActionHelpContext");
289 g_object_set (action
, "sensitive", FALSE
, NULL
);
292 #ifndef DISABLE_EMBEDDED_DEVHELP
295 static void on_load_changed (WebKitWebView
* web_view
,
296 WebKitLoadEvent load_event
, gpointer user_data
)
298 AnjutaDevhelp
* devhelp
= ANJUTA_PLUGIN_DEVHELP(user_data
);
300 if (load_event
== WEBKIT_LOAD_FINISHED
)
301 anjuta_devhelp_check_history(devhelp
);
304 static void on_load_finished (GObject
* view
, GObject
* frame
, gpointer user_data
)
306 AnjutaDevhelp
* devhelp
= ANJUTA_PLUGIN_DEVHELP(user_data
);
307 anjuta_devhelp_check_history(devhelp
);
314 devhelp_activate (AnjutaPlugin
*plugin
)
317 AnjutaDevhelp
*devhelp
;
319 #ifndef DISABLE_EMBEDDED_DEVHELP
320 static gboolean init
= FALSE
;
325 register_stock_icons (plugin
);
330 DEBUG_PRINT ("%s", "AnjutaDevhelp: Activating AnjutaDevhelp plugin ...");
331 devhelp
= ANJUTA_PLUGIN_DEVHELP (plugin
);
333 /* Add all UI actions and merge UI */
334 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
335 devhelp
->action_group
=
336 anjuta_ui_add_action_group_entries (ui
, "ActionGroupDevhelp",
337 _("Help operations"),
339 G_N_ELEMENTS (actions
),
340 GETTEXT_PACKAGE
, TRUE
,
342 devhelp
->uiid
= anjuta_ui_merge (ui
, UI_FILE
);
344 #ifndef DISABLE_EMBEDDED_DEVHELP
346 devhelp
->sidebar_header
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 1);
348 gtk_box_pack_start (GTK_BOX (devhelp
->sidebar_header
),
349 gtk_image_new_from_stock (ANJUTA_STOCK_DEVHELP_SEARCH
,
352 label
= gtk_label_new (_("API Browser"));
353 gtk_box_pack_start (GTK_BOX (devhelp
->sidebar_header
),
357 gtk_widget_show_all (devhelp
->sidebar_header
);
360 * Forward/back buttons
362 devhelp
->custom_label
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 1);
363 gtk_widget_show (devhelp
->custom_label
);
365 gtk_box_pack_start (GTK_BOX (devhelp
->custom_label
),
366 gtk_image_new_from_stock (ANJUTA_STOCK_DEVHELP_VIEW
,
369 gtk_box_pack_start (GTK_BOX (devhelp
->custom_label
), gtk_label_new (_("API")),
372 devhelp
->go_back
= gtk_button_new ();
373 gtk_container_add (GTK_CONTAINER (devhelp
->go_back
),
374 gtk_image_new_from_stock(GTK_STOCK_GO_BACK
, GTK_ICON_SIZE_MENU
));
375 gtk_widget_set_sensitive (devhelp
->go_back
, FALSE
);
376 g_signal_connect (devhelp
->go_back
, "clicked",
377 G_CALLBACK (on_go_back_clicked
), devhelp
);
379 devhelp
->go_forward
= gtk_button_new ();
380 gtk_container_add (GTK_CONTAINER (devhelp
->go_forward
),
381 gtk_image_new_from_stock(GTK_STOCK_GO_FORWARD
, GTK_ICON_SIZE_MENU
));
382 gtk_widget_set_sensitive (devhelp
->go_forward
, FALSE
);
383 g_signal_connect (devhelp
->go_forward
, "clicked",
384 G_CALLBACK (on_go_forward_clicked
), devhelp
);
386 devhelp
->online
= gtk_button_new_with_label ("library.gnome.org");
387 g_signal_connect (devhelp
->online
, "clicked",
388 G_CALLBACK (on_online_clicked
), devhelp
);
390 gtk_box_pack_start (GTK_BOX (devhelp
->custom_label
), devhelp
->go_back
,
392 gtk_box_pack_start (GTK_BOX (devhelp
->custom_label
), devhelp
->go_forward
,
394 gtk_box_pack_start (GTK_BOX (devhelp
->custom_label
), devhelp
->online
,
396 gtk_widget_show_all (devhelp
->custom_label
);
401 devhelp
->sidebar
= dh_sidebar_new (devhelp
->book_manager
);
403 g_signal_connect (devhelp
->sidebar
,
405 G_CALLBACK (devhelp_sidebar_link_selected_cb
),
408 gtk_widget_show_all (devhelp
->sidebar
);
411 devhelp
->view
= webkit_web_view_new ();
412 gtk_widget_show (devhelp
->view
);
414 // TODO: Show some good start page
416 webkit_web_view_load_uri (WEBKIT_WEB_VIEW (devhelp
->view
), "about:blank");
417 g_signal_connect(G_OBJECT (devhelp
->view
), "load-changed",
418 G_CALLBACK (on_load_changed
), devhelp
);
420 devhelp
->present_widget
= devhelp
->view
;
422 webkit_web_view_open (WEBKIT_WEB_VIEW (devhelp
->view
), "about:blank");
423 g_signal_connect(G_OBJECT (devhelp
->view
), "load-finished",
424 G_CALLBACK (on_load_finished
), devhelp
);
426 devhelp
->view_sw
= gtk_scrolled_window_new (NULL
, NULL
);
427 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (devhelp
->view_sw
),
428 GTK_POLICY_AUTOMATIC
,
429 GTK_POLICY_AUTOMATIC
);
430 gtk_container_set_border_width (GTK_CONTAINER (devhelp
->view_sw
), 5);
431 gtk_widget_show (devhelp
->view_sw
);
432 gtk_container_add (GTK_CONTAINER (devhelp
->view_sw
), devhelp
->view
);
434 devhelp
->present_widget
= devhelp
->view_sw
;
437 /* This is the window that show the html help text */
438 anjuta_shell_add_widget_custom (plugin
->shell
, devhelp
->present_widget
,
439 "AnjutaDevhelpDisplay", _("API"),
440 ANJUTA_STOCK_DEVHELP_VIEW
, devhelp
->custom_label
,
441 ANJUTA_SHELL_PLACEMENT_CENTER
, NULL
);
443 anjuta_shell_add_widget_custom (plugin
->shell
, devhelp
->sidebar
,
444 "AnjutaDevhelpIndex", _("API Browser"), ANJUTA_STOCK_DEVHELP_SEARCH
,
445 devhelp
->sidebar_header
,
446 ANJUTA_SHELL_PLACEMENT_LEFT
, NULL
);
448 #endif /* DISABLE_EMBEDDED_DEVHELP */
451 devhelp
->editor_watch_id
=
452 anjuta_plugin_add_watch (plugin
, IANJUTA_DOCUMENT_MANAGER_CURRENT_DOCUMENT
,
453 value_added_current_editor
,
454 value_removed_current_editor
,
461 devhelp_deactivate (AnjutaPlugin
*plugin
)
464 AnjutaDevhelp
* devhelp
= ANJUTA_PLUGIN_DEVHELP (plugin
);
466 DEBUG_PRINT ("%s", "AnjutaDevhelp: Dectivating AnjutaDevhelp plugin ...");
469 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
470 anjuta_ui_unmerge (ui
, devhelp
->uiid
);
472 #ifndef DISABLE_EMBEDDED_DEVHELP
475 anjuta_shell_remove_widget(plugin
->shell
, devhelp
->present_widget
, NULL
);
476 anjuta_shell_remove_widget(plugin
->shell
, devhelp
->sidebar
, NULL
);
478 #endif /* DISABLE_EMBEDDED_DEVHELP */
480 /* Remove action group */
481 anjuta_ui_remove_action_group (ui
, devhelp
->action_group
);
484 anjuta_plugin_remove_watch (plugin
, devhelp
->editor_watch_id
, TRUE
);
489 #ifndef DISABLE_EMBEDDED_DEVHELP
492 anjuta_devhelp_check_history (AnjutaDevhelp
* devhelp
)
494 gtk_widget_set_sensitive (devhelp
->go_forward
, webkit_web_view_can_go_forward (WEBKIT_WEB_VIEW (devhelp
->view
)));
496 gtk_widget_set_sensitive (devhelp
->go_back
, webkit_web_view_can_go_back (WEBKIT_WEB_VIEW (devhelp
->view
)));
499 #endif /* DISABLE_EMBEDDED_DEVHELP */
502 devhelp_finalize (GObject
*obj
)
504 DEBUG_PRINT ("%s", "Finalising Devhelp plugin");
506 /* Finalization codes here */
507 G_OBJECT_CLASS (parent_class
)->finalize (obj
);
511 devhelp_dispose (GObject
*obj
)
513 DEBUG_PRINT ("%s", "Disposing Devhelp plugin");
515 #ifndef DISABLE_EMBEDDED_DEVHELP
516 AnjutaDevhelp
* devhelp
= ANJUTA_PLUGIN_DEVHELP (obj
);
518 g_clear_object (&devhelp
->book_manager
);
519 #endif /* DISABLE_EMBEDDED_DEVHELP */
521 /* Disposition codes */
522 G_OBJECT_CLASS (parent_class
)->dispose (obj
);
526 devhelp_instance_init (GObject
*obj
)
528 AnjutaDevhelp
*plugin
= ANJUTA_PLUGIN_DEVHELP (obj
);
530 #ifndef DISABLE_EMBEDDED_DEVHELP
532 /* Initialize Devhelp support */
536 plugin
->book_manager
= dh_book_manager_new ();
537 dh_book_manager_populate (plugin
->book_manager
);
539 #endif /* DISABLE_EMBEDDED_DEVHELP */
545 devhelp_class_init (GObjectClass
*klass
)
547 AnjutaPluginClass
*plugin_class
= ANJUTA_PLUGIN_CLASS (klass
);
549 parent_class
= g_type_class_peek_parent (klass
);
551 plugin_class
->activate
= devhelp_activate
;
552 plugin_class
->deactivate
= devhelp_deactivate
;
554 klass
->finalize
= devhelp_finalize
;
555 klass
->dispose
= devhelp_dispose
;
558 #ifndef DISABLE_EMBEDDED_DEVHELP
561 ihelp_search (IAnjutaHelp
*help
, const gchar
*query
, GError
**err
)
563 AnjutaDevhelp
*plugin
;
565 plugin
= ANJUTA_PLUGIN_DEVHELP (help
);
567 anjuta_shell_present_widget (ANJUTA_PLUGIN (plugin
)->shell
,
568 plugin
->sidebar
, NULL
);
570 dh_sidebar_set_search_string (DH_SIDEBAR (plugin
->sidebar
), query
);
573 #else /* DISABLE_EMBEDDED_DEVHELP */
577 * Set the DISPLAY variable, to be use by g_spawn_async.
580 set_environment (gpointer display
)
582 g_setenv ("DISPLAY", display
, TRUE
);
587 ihelp_search (IAnjutaHelp
*help
, const gchar
*query
, GError
**err
)
592 if (!anjuta_util_prog_is_installed ("devhelp", TRUE
))
599 if (query
&& strlen (query
) > 0)
602 cmd
[2] = (gchar
*)query
;
610 display
= gdk_screen_make_display_name (gdk_screen_get_default ());
612 g_spawn_async (NULL
, /* working directory */
624 #endif /* DISABLE_EMBEDDED_DEVHELP */
627 ihelp_iface_init(IAnjutaHelpIface
*iface
)
629 iface
->search
= ihelp_search
;
632 ANJUTA_PLUGIN_BEGIN (AnjutaDevhelp
, devhelp
);
633 ANJUTA_PLUGIN_ADD_INTERFACE (ihelp
, IANJUTA_TYPE_HELP
);
636 ANJUTA_SIMPLE_PLUGIN (AnjutaDevhelp
, devhelp
);