1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) Naba Kumar <naba@gnome.org>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * SECTION:anjuta-shell
24 * @short_description: Application shell interface
26 * @stability: Unstable
27 * @include: libanjuta/anjuta-shell.h
29 * Shell is the playground where plugins are loaded and their UI
30 * widgets shown. It is also a place where plugins export objects for letting
31 * other pluings to use. Plugins are loaded into shell on demand, but some
32 * plugins are loaded on startup (such as help and text editor plugin).
33 * Demand to load a plugin can be made by requesting for a primary inferface
34 * using anjuta_shell_get_interface() or anjuta_shell_get_object().
36 * Plugins can add widgets in shell with
37 * anjuta_shell_add_widget() and remove with anjuta_shell_remove_widget()
40 * In Anjuta, shell is implemented using an advanced widget docking system,
41 * allowing plugin widgets to dock, undock and layout in any fashion. Dock
42 * layout is also maintained internally and is transparent to plugin
45 * #AnjutaShell allows plugins to export arbitrary objects as <emphasis>
46 * values</emphasis> in its <emphasis>Values System</emphasis>. "value_added"
47 * and "value_removed" signals are emitted when a value is added to or
48 * removed from the <emphasis>Values System</emphasis>, hence notifying
49 * plugins of its state. However, plugins should really not connect directly
50 * to these signals, because they are emitted for all values
51 * and not just for the values the plugin is interested in. Instead,
52 * to monitor specific <emphasis>Values</emphasis>, plugins should
53 * setup watches using anjuta_plugin_add_watch().
55 * <emphasis>Values</emphasis> are added, get or removed with
56 * anjuta_shell_add_value() and anjuta_shell_get_value() or
57 * anjuta_shell_remove_value(). There multi-valued equivalent functions
58 * can be used to manipulate multiple values at once.
60 * <emphasis>Values</emphasis> are identified with names. Since <emphasis>
61 * Values</emphasis> are effectively variables, their names should follow
62 * the standard GNOME variable naming convention and should be as descriptive
63 * as possible (e.g project_root_directory, project_name etc.). It is also
64 * essential that meaningful prefix be given to names so that <emphasis>
65 * Values</emphasis> are easily grouped (e.g all values exported by a
66 * project manager should start with project_ prefix).
68 * Plugins can find other plugins with anjuta_shell_get_object() or
69 * anjuta_shell_get_interface() based on their primary interfaces.
74 #include <gobject/gvaluecollector.h>
75 #include "anjuta-shell.h"
76 #include "anjuta-marshal.h"
77 #include "anjuta-debug.h"
84 AnjutaShellPlacement placement
;
88 on_widget_data_free (WidgetQueueData
*data
)
90 g_object_unref (data
->widget
);
93 g_free (data
->stock_id
);
98 on_widget_data_add (WidgetQueueData
*data
, AnjutaShell
*shell
)
100 ANJUTA_SHELL_GET_IFACE (shell
)->add_widget (shell
, data
->widget
,
109 on_destroy_widget_queue (gpointer data
)
111 GQueue
*queue
= (GQueue
*)data
;
112 g_queue_foreach (queue
, (GFunc
)on_widget_data_free
, NULL
);
113 g_queue_free (queue
);
117 anjuta_shell_error_quark (void)
119 static GQuark quark
= 0;
122 quark
= g_quark_from_static_string ("anjuta-shell-quark");
129 * anjuta_shell_freeze:
130 * @shell: A #AnjutaShell interface.
131 * @error: Error propagation object.
133 * Freezes addition of any UI elements (widgets) in the shell. All widget
134 * additions are queued for later additions when freeze count reaches 0.
135 * Any number of this function can be called and each call will increase
136 * the freeze count. anjuta_shell_thaw() will reduce the freeze count by
137 * 1 and real thawing happens when the count reaches 0.
140 anjuta_shell_freeze (AnjutaShell
*shell
, GError
**error
)
144 g_return_if_fail (shell
!= NULL
);
145 freeze_count
= GPOINTER_TO_INT (g_object_get_data (G_OBJECT (shell
),
148 g_object_set_data (G_OBJECT (shell
), "__freeze_count",
149 GINT_TO_POINTER (freeze_count
));
154 * @shell: A #AnjutaShell interface.
155 * @error: Error propagation object.
157 * Reduces the freeze count by one and performs pending widget additions
158 * when the count reaches 0.
161 anjuta_shell_thaw (AnjutaShell
*shell
, GError
**error
)
165 g_return_if_fail (shell
!= NULL
);
166 freeze_count
= GPOINTER_TO_INT (g_object_get_data (G_OBJECT (shell
),
169 if (freeze_count
< 0)
171 g_object_set_data (G_OBJECT (shell
), "__freeze_count",
172 GINT_TO_POINTER (freeze_count
));
174 if (freeze_count
<= 0)
176 /* Add all pending widgets */
177 /* DEBUG_PRINT ("Thawing shell ..."); */
180 queue
= g_object_get_data (G_OBJECT (shell
), "__widget_queue");
183 g_queue_reverse (queue
);
184 g_queue_foreach (queue
, (GFunc
)on_widget_data_add
, shell
);
185 g_object_set_data (G_OBJECT (shell
), "__widget_queue", NULL
);
191 * anjuta_shell_add_widget:
192 * @shell: A #AnjutaShell interface.
193 * @widget: Then widget to add
194 * @name: Name of the widget. None translated string used to identify it in
196 * @stock_id: Icon stock ID. Could be null.
197 * @title: Translated string which is displayed along side the widget when
198 * required (eg. as window title or notebook tab label).
199 * @placement: Placement of the widget in shell.
200 * @error: Error propagation object.
202 * Adds @widget in the shell. The @placement tells where the widget should
203 * appear, but generally it will be overridden by the container
204 * (dock, notebook, GtkContainer etc.) saved layout.
207 anjuta_shell_add_widget (AnjutaShell
*shell
,
211 const char *stock_id
,
212 AnjutaShellPlacement placement
,
215 GQueue
*widget_queue
;
218 g_return_if_fail (shell
!= NULL
);
219 g_return_if_fail (ANJUTA_IS_SHELL (shell
));
220 g_return_if_fail (widget
!= NULL
);
221 g_return_if_fail (GTK_IS_WIDGET (widget
));
222 g_return_if_fail (name
!= NULL
);
223 g_return_if_fail (title
!= NULL
);
225 freeze_count
= GPOINTER_TO_INT (g_object_get_data (G_OBJECT (shell
),
227 if (freeze_count
<= 0)
229 ANJUTA_SHELL_GET_IFACE (shell
)->add_widget (shell
, widget
, name
,
235 /* Queue the operation */
238 widget_queue
= g_object_get_data (G_OBJECT (shell
), "__widget_queue");
241 widget_queue
= g_queue_new ();
242 g_object_set_data_full (G_OBJECT (shell
), "__widget_queue",
243 widget_queue
, on_destroy_widget_queue
);
245 qd
= g_new0(WidgetQueueData
, 1);
246 g_object_ref (G_OBJECT (widget
));
248 qd
->name
= g_strdup (name
);
249 qd
->title
= g_strdup (title
);
251 qd
->stock_id
= g_strdup (stock_id
);
252 qd
->placement
= placement
;
254 g_queue_push_head (widget_queue
, qd
);
259 * anjuta_shell_remove_widget:
260 * @shell: A #AnjutaShell interface
261 * @widget: The widget to remove
262 * @error: Error propagation object
264 * Removes the widget from shell. The widget should have been added before
265 * with #anjuta_shell_add_widget.
268 anjuta_shell_remove_widget (AnjutaShell
*shell
,
273 gboolean found_in_queue
;
275 g_return_if_fail (shell
!= NULL
);
276 g_return_if_fail (ANJUTA_IS_SHELL (shell
));
277 g_return_if_fail (widget
!= NULL
);
278 g_return_if_fail (GTK_IS_WIDGET (widget
));
280 /* If there is a queue, remove widgets from it */
281 found_in_queue
= FALSE
;
282 queue
= g_object_get_data (G_OBJECT (shell
), "__widget_queue");
286 for (i
= g_queue_get_length(queue
) - 1; i
>= 0; i
--)
290 qd
= g_queue_peek_nth (queue
, i
);
291 if (qd
->widget
== widget
)
293 g_queue_remove (queue
, qd
);
294 on_widget_data_free (qd
);
295 found_in_queue
= TRUE
;
301 ANJUTA_SHELL_GET_IFACE (shell
)->remove_widget (shell
, widget
, error
);
305 * anjuta_shell_present_widget:
306 * @shell: A #AnjutaShell interface
307 * @widget: The widget to present
308 * @error: Error propagation object
310 * Make sure the widget is visible to user. If the widget is hidden, it will
311 * be shown. If it is not visible to user, it will be made visible.
314 anjuta_shell_present_widget (AnjutaShell
*shell
,
319 gboolean found_in_queue
;
321 g_return_if_fail (shell
!= NULL
);
322 g_return_if_fail (ANJUTA_IS_SHELL (shell
));
323 g_return_if_fail (widget
!= NULL
);
324 g_return_if_fail (GTK_IS_WIDGET (widget
));
326 /* If there is a queue and the widget is in the queue, there is no
327 * way we can 'present' the widget */
328 found_in_queue
= FALSE
;
329 queue
= g_object_get_data (G_OBJECT (shell
), "__widget_queue");
333 for (i
= g_queue_get_length(queue
) - 1; i
>= 0; i
--)
337 qd
= g_queue_peek_nth (queue
, i
);
338 if (qd
->widget
== widget
)
340 found_in_queue
= TRUE
;
346 ANJUTA_SHELL_GET_IFACE (shell
)->present_widget (shell
, widget
, error
);
350 * anjuta_shell_add_value:
351 * @shell: A #AnjutaShell interface
352 * @name: Name of the value
353 * @value: Value to add
354 * @error: Error propagation object
356 * Sets a value in the shell with the given name. Any previous value will
357 * be overridden. "value_added" signal will be emitted. Objects connecting
358 * to this signal can then update their data according to the new value.
361 anjuta_shell_add_value (AnjutaShell
*shell
,
366 g_return_if_fail (shell
!= NULL
);
367 g_return_if_fail (ANJUTA_IS_SHELL (shell
));
368 g_return_if_fail (name
!= NULL
);
369 g_return_if_fail (value
!= NULL
);
371 ANJUTA_SHELL_GET_IFACE (shell
)->add_value (shell
, name
, value
, error
);
375 * anjuta_shell_add_valist:
376 * @shell: A #AnjutaShell interface
377 * @first_name: First value name
378 * @first_type: First value type
379 * @var_args: First value, Second value name, Second value type ....
381 * Adds a valist of values in the shell. The valist should be in the order -
382 * value1, name2, type2, value2,... "value_added" signal will be emitted
383 * for each of the value.
386 anjuta_shell_add_valist (AnjutaShell
*shell
,
387 const char *first_name
,
394 g_return_if_fail (shell
!= NULL
);
395 g_return_if_fail (ANJUTA_IS_SHELL (shell
));
396 g_return_if_fail (first_name
!= NULL
);
402 GValue value
= {0, };
406 g_value_init (&value
, type
);
408 G_VALUE_COLLECT (&value
, var_args
, 0, &error
);
411 g_warning ("%s: %s", G_STRLOC
, error
);
416 anjuta_shell_add_value (shell
, name
, &value
, &err
);
418 g_value_unset (&value
);
421 g_warning ("Could not set value: %s\n", err
->message
);
426 name
= va_arg (var_args
, char *);
428 type
= va_arg (var_args
, GType
);
435 * @shell: A #AnjutaShell interface
436 * @first_name: First value name
437 * @first_type: First value type
438 * @...: First value, Second value name, Second value type .... NULL
440 * Adds a list of values in the shell. The list should be NULL terminated
441 * and should be in the order - name1, type1, value1, name2, type2, value2,
442 * ..., NULL. "value_added" signal will be emitted for each of the value.
445 anjuta_shell_add (AnjutaShell
*shell
,
446 const char *first_name
,
452 g_return_if_fail (shell
!= NULL
);
453 g_return_if_fail (ANJUTA_IS_SHELL (shell
));
454 g_return_if_fail (first_name
!= NULL
);
456 va_start (var_args
, first_type
);
457 anjuta_shell_add_valist (shell
, first_name
, first_type
, var_args
);
462 * anjuta_shell_get_value:
463 * @shell: A #AnjutaShell interface
464 * @name: Name of the value to get
465 * @value: Value to get
466 * @error: Error propagation object
468 * Gets a value from the shell with the given name. The value will be set
469 * in the passed value pointer.
472 anjuta_shell_get_value (AnjutaShell
*shell
,
477 g_return_if_fail (shell
!= NULL
);
478 g_return_if_fail (ANJUTA_IS_SHELL (shell
));
479 g_return_if_fail (name
!= NULL
);
480 g_return_if_fail (value
!= NULL
);
482 ANJUTA_SHELL_GET_IFACE (shell
)->get_value (shell
, name
, value
, error
);
486 * anjuta_shell_get_valist:
487 * @shell: A #AnjutaShell interface
488 * @first_name: First value name
489 * @first_type: First value type
490 * @var_args: First value holder, Second value name, Second value type ....
492 * Gets a valist of values from the shell. The valist should be in the order -
493 * value1, name2, type2, value2,...
496 anjuta_shell_get_valist (AnjutaShell
*shell
,
497 const char *first_name
,
504 g_return_if_fail (shell
!= NULL
);
505 g_return_if_fail (ANJUTA_IS_SHELL (shell
));
506 g_return_if_fail (first_name
!= NULL
);
512 GValue value
= {0, };
516 g_value_init (&value
, type
);
518 anjuta_shell_get_value (shell
, name
, &value
, &err
);
521 g_warning ("Could not get value: %s", err
->message
);
526 G_VALUE_LCOPY (&value
, var_args
, 0, &error
);
529 g_warning ("%s: %s", G_STRLOC
, error
);
534 g_value_unset (&value
);
536 name
= va_arg (var_args
, char *);
538 type
= va_arg (var_args
, GType
);
545 * @shell: A #AnjutaShell interface
546 * @first_name: First value name
547 * @first_type: First value type
548 * @...: First value holder, Second value name, Second value type .... NULL
550 * Gets a list of values in the shell. The list should be NULL terminated
551 * and should be in the order - name1, type1, value1, name2, type2, value2,
555 anjuta_shell_get (AnjutaShell
*shell
,
556 const char *first_name
,
562 g_return_if_fail (shell
!= NULL
);
563 g_return_if_fail (ANJUTA_IS_SHELL (shell
));
564 g_return_if_fail (first_name
!= NULL
);
566 va_start (var_args
, first_type
);
567 anjuta_shell_get_valist (shell
, first_name
, first_type
, var_args
);
572 * anjuta_shell_remove_value:
573 * @shell: A #AnjutaShell interface
574 * @name: Name of the value to remove
575 * @error: Error propagation object
577 * Removes a value from the shell with the given name. "value_removed" signal
578 * will be emitted. Objects connecting to this signal can then update their
579 * data/internal-state accordingly.
582 anjuta_shell_remove_value (AnjutaShell
*shell
,
586 g_return_if_fail (shell
!= NULL
);
587 g_return_if_fail (ANJUTA_IS_SHELL (shell
));
588 g_return_if_fail (name
!= NULL
);
590 ANJUTA_SHELL_GET_IFACE (shell
)->remove_value (shell
, name
, error
);
594 * anjuta_shell_get_object:
595 * @shell: A #AnjutaShell interface
596 * @iface_name: The interface implemented by the object to be found
597 * @error: Error propagation.
599 * Searches the currently available plugins to find the one which
600 * implements the given interface as primary interface and returns it. If
601 * the plugin is not yet loaded, it will be loaded and activated.
602 * The returned object is garanteed to be an implementor of the
603 * interface (as exported by the plugin metafile). It only searches
604 * from the pool of plugin objects loaded in this shell and can only search
605 * by primary interface. If there are more objects implementing this primary
606 * interface, user might be prompted to select one from them (and might give
607 * the option to use it as default for future queries). A typical usage of this
611 * anjuta_plugins_get_object (shell, "IAnjutaDocumentManager", error);
613 * Notice that this function takes the interface name string as string, unlike
614 * anjuta_plugins_get_interface() which takes the type directly.
616 * Return value: A plugin object implementing the primary interface or NULL.
619 anjuta_shell_get_object (AnjutaShell
*shell
, const gchar
*iface_name
,
622 g_return_val_if_fail (shell
!= NULL
, NULL
);
623 g_return_val_if_fail (iface_name
!= NULL
, NULL
);
624 g_return_val_if_fail (ANJUTA_IS_SHELL (shell
), NULL
);
626 return ANJUTA_SHELL_GET_IFACE (shell
)->get_object (shell
, iface_name
, error
);
630 * anjuta_shell_get_status:
631 * @shell: A #AnjutaShell interface
632 * @error: Error propagation object
634 * Retrieves the #AnjutaStatus object associated with the shell.
636 * Return value: The #AnjutaStatus object.
639 anjuta_shell_get_status (AnjutaShell
*shell
, GError
**error
)
641 g_return_val_if_fail (shell
!= NULL
, NULL
);
642 g_return_val_if_fail (ANJUTA_IS_SHELL (shell
), NULL
);
644 return ANJUTA_SHELL_GET_IFACE (shell
)->get_status (shell
, error
);
648 * anjuta_shell_get_ui:
649 * @shell: A #AnjutaShell interface
650 * @error: Error propagation object
652 * Retrieves the #AnjutaUI object associated with the shell.
654 * Return value: The #AnjutaUI object.
657 anjuta_shell_get_ui (AnjutaShell
*shell
, GError
**error
)
659 g_return_val_if_fail (shell
!= NULL
, NULL
);
660 g_return_val_if_fail (ANJUTA_IS_SHELL (shell
), NULL
);
662 return ANJUTA_SHELL_GET_IFACE (shell
)->get_ui (shell
, error
);
666 * anjuta_shell_get_preferences:
667 * @shell: A #AnjutaShell interface
668 * @error: Error propagation object
670 * Retrieves the #AnjutaPreferences object associated with the shell.
672 * Return value: The #AnjutaPreferences object.
675 anjuta_shell_get_preferences (AnjutaShell
*shell
, GError
**error
)
677 g_return_val_if_fail (shell
!= NULL
, NULL
);
678 g_return_val_if_fail (ANJUTA_IS_SHELL (shell
), NULL
);
680 return ANJUTA_SHELL_GET_IFACE (shell
)->get_preferences (shell
, error
);
684 * anjuta_shell_get_plugin_manager:
685 * @shell: A #AnjutaShell interface
686 * @error: Error propagation object
688 * Retrieves the #AnjutaPluginManager object associated with the shell.
690 * Return value: The #AnjutaPluginManager object.
693 anjuta_shell_get_plugin_manager (AnjutaShell
*shell
, GError
**error
)
695 g_return_val_if_fail (shell
!= NULL
, NULL
);
696 g_return_val_if_fail (ANJUTA_IS_SHELL (shell
), NULL
);
698 return ANJUTA_SHELL_GET_IFACE (shell
)->get_plugin_manager (shell
, error
);
702 * anjuta_shell_get_profile_manager:
703 * @shell: A #AnjutaShell interface
704 * @error: Error propagation object
706 * Retrieves the #AnjutaProfileManager object associated with the shell.
708 * Return value: The #AnjutaProfileManager object.
710 AnjutaProfileManager
*
711 anjuta_shell_get_profile_manager (AnjutaShell
*shell
, GError
**error
)
713 g_return_val_if_fail (shell
!= NULL
, NULL
);
714 g_return_val_if_fail (ANJUTA_IS_SHELL (shell
), NULL
);
716 return ANJUTA_SHELL_GET_IFACE (shell
)->get_profile_manager (shell
, error
);
720 anjuta_shell_session_save (AnjutaShell
*shell
, const gchar
*session_directory
,
723 AnjutaSession
*session
;
725 g_return_if_fail (ANJUTA_IS_SHELL (shell
));
726 g_return_if_fail (session_directory
!= NULL
);
728 session
= anjuta_session_new (session_directory
);
729 anjuta_session_clear (session
);
730 g_signal_emit_by_name (G_OBJECT (shell
), "save_session",
731 ANJUTA_SESSION_PHASE_FIRST
, session
);
732 g_signal_emit_by_name (G_OBJECT (shell
), "save_session",
733 ANJUTA_SESSION_PHASE_NORMAL
, session
);
734 g_signal_emit_by_name (G_OBJECT (shell
), "save_session",
735 ANJUTA_SESSION_PHASE_LAST
, session
);
736 anjuta_session_sync (session
);
737 g_object_unref (session
);
741 anjuta_shell_session_load (AnjutaShell
*shell
, const gchar
*session_directory
,
744 AnjutaSession
*session
;
746 g_return_if_fail (ANJUTA_IS_SHELL (shell
));
747 g_return_if_fail (session_directory
!= NULL
);
749 /* Do not allow multiple session loadings at once. Could be a trouble */
750 if (g_object_get_data (G_OBJECT (shell
), "__session_loading"))
752 /* DEBUG_PRINT ("A session load is requested in the middle of another!!"); */
755 g_object_set_data (G_OBJECT (shell
), "__session_loading", "1");
757 session
= anjuta_session_new (session_directory
);
758 g_signal_emit_by_name (G_OBJECT (shell
), "load_session",
759 ANJUTA_SESSION_PHASE_FIRST
, session
);
760 g_signal_emit_by_name (G_OBJECT (shell
), "load_session",
761 ANJUTA_SESSION_PHASE_NORMAL
, session
);
762 g_signal_emit_by_name (G_OBJECT (shell
), "load_session",
763 ANJUTA_SESSION_PHASE_LAST
, session
);
764 g_object_unref (session
);
766 g_object_set_data (G_OBJECT (shell
), "__session_loading", NULL
);
770 anjuta_shell_save_prompt (AnjutaShell
*shell
,
771 AnjutaSavePrompt
*save_prompt
,
774 g_return_if_fail (ANJUTA_IS_SHELL (shell
));
775 g_return_if_fail (ANJUTA_IS_SAVE_PROMPT (save_prompt
));
776 g_signal_emit_by_name (shell
, "save-prompt", save_prompt
);
780 anjuta_shell_notify_exit (AnjutaShell
*shell
,
783 g_return_if_fail (ANJUTA_IS_SHELL (shell
));
784 g_signal_emit_by_name (shell
, "exiting");
788 anjuta_shell_base_init (gpointer gclass
)
790 static gboolean initialized
= FALSE
;
793 g_signal_new ("value-added",
796 G_STRUCT_OFFSET (AnjutaShellIface
, value_added
),
798 anjuta_cclosure_marshal_VOID__STRING_BOXED
,
800 G_TYPE_STRING
, G_TYPE_VALUE
);
802 g_signal_new ("value-removed",
805 G_STRUCT_OFFSET (AnjutaShellIface
, value_removed
),
807 anjuta_cclosure_marshal_VOID__STRING
,
810 g_signal_new ("save-session",
813 G_STRUCT_OFFSET (AnjutaShellIface
, save_session
),
815 anjuta_cclosure_marshal_VOID__INT_OBJECT
,
819 g_signal_new ("load-session",
822 G_STRUCT_OFFSET (AnjutaShellIface
, load_session
),
824 anjuta_cclosure_marshal_VOID__INT_OBJECT
,
828 g_signal_new ("save-prompt",
831 G_STRUCT_OFFSET (AnjutaShellIface
, save_prompt
),
833 anjuta_cclosure_marshal_VOID__OBJECT
,
836 g_signal_new ("exiting",
839 G_STRUCT_OFFSET (AnjutaShellIface
, exiting
),
841 anjuta_cclosure_marshal_VOID__VOID
,
848 anjuta_shell_get_type (void)
850 static GType type
= 0;
853 static const GTypeInfo info
= {
854 sizeof (AnjutaShellIface
),
855 anjuta_shell_base_init
,
865 type
= g_type_register_static (G_TYPE_INTERFACE
,
870 g_type_interface_add_prerequisite (type
, G_TYPE_OBJECT
);