Initial German translation of the build tutorial
[anjuta.git] / plugins / devhelp / plugin.c
blob6978382a879098f944d4ef63cef606733e13a4fc
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * plugin.c
4 * Copyright (C) Johannes Schmid 2006 <jhs@cvs.gnome.org>
5 *
6 * plugin.c is free software.
7 *
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)
11 * any later version.
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.
25 #include <config.h>
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>
33 #include "plugin.h"
35 #ifndef DISABLE_EMBEDDED_DEVHELP
37 #ifdef HAVE_WEBKIT2
38 #include <webkit2/webkit2.h>
39 #else
40 #include <webkit/webkit.h>
41 #endif
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"
66 static void
67 register_stock_icons (AnjutaPlugin *plugin)
69 static gboolean registered = FALSE;
71 if (registered)
72 return;
73 registered = TRUE;
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);
80 END_REGISTER_ICON
83 static void
84 devhelp_sidebar_link_selected_cb (GObject *ignored, DhLink *link,
85 AnjutaDevhelp *widget)
87 gchar *uri;
89 anjuta_shell_present_widget (ANJUTA_PLUGIN (widget)->shell,
90 widget->present_widget, NULL);
92 uri = dh_link_get_uri (link);
93 #ifdef HAVE_WEBKIT2
94 webkit_web_view_load_uri (WEBKIT_WEB_VIEW (widget->view), uri);
95 #else
96 webkit_web_view_open (WEBKIT_WEB_VIEW (widget->view), uri);
97 #endif
98 g_free (uri);
100 anjuta_devhelp_check_history (widget);
103 static void
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);
114 static void
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);
125 static void
126 on_online_clicked (GtkWidget* widget, AnjutaDevhelp* plugin)
128 anjuta_shell_present_widget (ANJUTA_PLUGIN (plugin)->shell,
129 plugin->present_widget, NULL);
130 #ifdef HAVE_WEBKIT2
131 webkit_web_view_load_uri (WEBKIT_WEB_VIEW (plugin->view),
132 ONLINE_API_DOCS);
133 #else
134 webkit_web_view_open (WEBKIT_WEB_VIEW (plugin->view),
135 ONLINE_API_DOCS);
136 #endif
137 anjuta_devhelp_check_history (plugin);
140 static gboolean
141 api_reference_idle (AnjutaDevhelp* plugin)
143 anjuta_shell_present_widget (ANJUTA_PLUGIN (plugin)->shell,
144 plugin->sidebar, NULL);
145 return FALSE;
148 #else /* DISABLE_EMBEDDED_DEVHELP */
150 static gboolean
151 api_reference_idle (AnjutaDevhelp* plugin)
153 ianjuta_help_search (IANJUTA_HELP (plugin), "", NULL);
154 return FALSE;
157 #endif
159 static void
160 on_api_reference_activate (GtkAction *action, AnjutaDevhelp *plugin)
162 g_idle_add((GSourceFunc) api_reference_idle, plugin);
165 static gboolean
166 context_idle (AnjutaDevhelp* plugin)
168 IAnjutaEditor *editor;
169 gchar *current_word;
171 if(plugin->editor == NULL)
172 return FALSE;
174 editor = IANJUTA_EDITOR (plugin->editor);
175 current_word = ianjuta_editor_get_current_word (editor, NULL);
176 if (current_word)
178 ianjuta_help_search (IANJUTA_HELP (plugin), current_word, NULL);
179 g_free (current_word);
181 return FALSE;
184 static void
185 on_context_help_activate (GtkAction *action, AnjutaDevhelp *plugin)
187 g_idle_add((GSourceFunc) context_idle, plugin);
190 static void
191 on_search_help_activate (GtkAction *action, AnjutaDevhelp *plugin)
193 GtkWindow *parent;
194 gboolean status;
195 gchar *search_term = NULL;
197 parent = GTK_WINDOW (ANJUTA_PLUGIN (plugin)->shell);
198 status =
199 anjuta_util_dialog_input (parent, _("Search Help:"),
200 NULL, &search_term);
201 if (status)
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
216 /* Go menu */
218 "ActionMenuGoto",
219 NULL,
220 N_("_Go to"),
221 NULL,
222 NULL,
223 NULL
225 #endif /* DISABLE_EMBEDDED_DEVHELP */
227 "ActionHelpApi",
228 NULL,
229 N_("_API Reference"),
230 NULL,
231 N_("Browse API Pages"),
232 G_CALLBACK (on_api_reference_activate)
235 "ActionHelpContext",
236 #ifndef DISABLE_EMBEDDED_DEVHELP
237 ANJUTA_STOCK_DEVHELP_VIEW,
238 #else
239 NULL,
240 #endif /* DISABLE_EMBEDDED_DEVHELP */
241 N_("_Context Help"),
242 "<shift>F1",
243 N_("Search help for the current word in the editor"),
244 G_CALLBACK (on_context_help_activate)
247 "ActionHelpSearch",
248 #ifndef DISABLE_EMBEDDED_DEVHELP
249 ANJUTA_STOCK_DEVHELP_SEARCH,
250 #else
251 NULL,
252 #endif
253 N_("_Search Help"),
254 NULL,
255 N_("Search for a term in help"),
256 G_CALLBACK (on_search_help_activate)
260 /* Watches callbacks */
262 static void
263 value_added_current_editor (AnjutaPlugin *plugin, const gchar *name,
264 const GValue *value, gpointer data)
266 GtkAction *action;
267 GObject *editor;
268 AnjutaDevhelp* devhelp = ANJUTA_PLUGIN_DEVHELP (data);
270 editor = g_value_get_object (value);
271 if (!IANJUTA_IS_EDITOR (editor))
272 return;
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);
279 static void
280 value_removed_current_editor (AnjutaPlugin *plugin,
281 const gchar *name, gpointer data)
283 GtkAction *action;
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
294 #ifdef HAVE_WEBKIT2
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);
303 #else
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);
309 #endif
311 #endif
313 static gboolean
314 devhelp_activate (AnjutaPlugin *plugin)
316 AnjutaUI *ui;
317 AnjutaDevhelp *devhelp;
319 #ifndef DISABLE_EMBEDDED_DEVHELP
320 static gboolean init = FALSE;
321 GtkWidget *label;
323 if (!init)
325 register_stock_icons (plugin);
326 init = TRUE;
328 #endif
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"),
338 actions,
339 G_N_ELEMENTS (actions),
340 GETTEXT_PACKAGE, TRUE,
341 plugin);
342 devhelp->uiid = anjuta_ui_merge (ui, UI_FILE);
344 #ifndef DISABLE_EMBEDDED_DEVHELP
345 /* Tabs in grip */
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,
350 GTK_ICON_SIZE_MENU),
351 FALSE, FALSE, 0);
352 label = gtk_label_new (_("API Browser"));
353 gtk_box_pack_start (GTK_BOX (devhelp->sidebar_header),
354 label,
355 FALSE, FALSE, 0);
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,
367 GTK_ICON_SIZE_MENU),
368 FALSE, FALSE, 0);
369 gtk_box_pack_start (GTK_BOX (devhelp->custom_label), gtk_label_new (_("API")),
370 FALSE, FALSE, 0);
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,
391 FALSE, FALSE, 0);
392 gtk_box_pack_start (GTK_BOX (devhelp->custom_label), devhelp->go_forward,
393 FALSE, FALSE, 0);
394 gtk_box_pack_start (GTK_BOX (devhelp->custom_label), devhelp->online,
395 FALSE, FALSE, 0);
396 gtk_widget_show_all (devhelp->custom_label);
399 * Notebook
401 devhelp->sidebar = dh_sidebar_new (devhelp->book_manager);
403 g_signal_connect (devhelp->sidebar,
404 "link-selected",
405 G_CALLBACK (devhelp_sidebar_link_selected_cb),
406 devhelp);
408 gtk_widget_show_all (devhelp->sidebar);
410 /* View */
411 devhelp->view = webkit_web_view_new ();
412 gtk_widget_show (devhelp->view);
414 // TODO: Show some good start page
415 #ifdef HAVE_WEBKIT2
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;
421 #else
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;
435 #endif
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 */
450 /* Add watches */
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,
455 devhelp);
457 return TRUE;
460 static gboolean
461 devhelp_deactivate (AnjutaPlugin *plugin)
463 AnjutaUI *ui;
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
474 /* Remove widgets */
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);
483 /* Remove watch */
484 anjuta_plugin_remove_watch (plugin, devhelp->editor_watch_id, TRUE);
486 return TRUE;
489 #ifndef DISABLE_EMBEDDED_DEVHELP
491 void
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 */
501 static void
502 devhelp_finalize (GObject *obj)
504 DEBUG_PRINT ("%s", "Finalising Devhelp plugin");
506 /* Finalization codes here */
507 G_OBJECT_CLASS (parent_class)->finalize (obj);
510 static void
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);
525 static void
526 devhelp_instance_init (GObject *obj)
528 AnjutaDevhelp *plugin = ANJUTA_PLUGIN_DEVHELP (obj);
530 #ifndef DISABLE_EMBEDDED_DEVHELP
532 /* Initialize Devhelp support */
533 dh_init ();
535 /* Create devhelp */
536 plugin->book_manager = dh_book_manager_new ();
537 dh_book_manager_populate (plugin->book_manager);
539 #endif /* DISABLE_EMBEDDED_DEVHELP */
541 plugin->uiid = 0;
544 static void
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
560 static void
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.
579 static void
580 set_environment (gpointer display)
582 g_setenv ("DISPLAY", display, TRUE);
586 static void
587 ihelp_search (IAnjutaHelp *help, const gchar *query, GError **err)
589 gchar *cmd[4];
590 gchar *display;
592 if (!anjuta_util_prog_is_installed ("devhelp", TRUE))
594 return;
597 cmd[0] = "devhelp";
599 if (query && strlen (query) > 0)
601 cmd[1] = "-s";
602 cmd[2] = (gchar *)query;
603 cmd[3] = NULL;
605 else
607 cmd[1] = NULL;
610 display = gdk_screen_make_display_name (gdk_screen_get_default ());
612 g_spawn_async (NULL, /* working directory */
613 cmd,
614 NULL, /* envp */
615 G_SPAWN_SEARCH_PATH,
616 set_environment,
617 &display,
618 NULL,
619 NULL);
621 g_free (display);
624 #endif /* DISABLE_EMBEDDED_DEVHELP */
626 static void
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);
634 ANJUTA_PLUGIN_END;
636 ANJUTA_SIMPLE_PLUGIN (AnjutaDevhelp, devhelp);