gui: context menu for the world tree
[ladish.git] / gui / graph_view.c
blob1be7bf0cc06ad7cb98efd4d95873a9275a6298f2
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of the graph view object
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "common.h"
28 #include "graph_view.h"
29 #include "glade.h"
30 #include "world_tree.h"
31 #include "app_supervisor_proxy.h"
33 struct graph_view
35 struct list_head siblings;
36 char * name;
37 graph_canvas_handle graph_canvas;
38 graph_proxy_handle graph;
39 GtkWidget * canvas_widget;
40 ladish_app_supervisor_proxy_handle app_supervisor;
43 struct list_head g_views;
45 GtkScrolledWindow * g_main_scrolledwin;
46 static struct graph_view * g_current_view;
48 void view_init(void)
50 g_main_scrolledwin = GTK_SCROLLED_WINDOW(get_glade_widget("main_scrolledwin"));
51 INIT_LIST_HEAD(&g_views);
52 g_current_view = NULL;
55 static void app_added(void * context, uint64_t id, const char * name, bool running, bool terminal, uint8_t level)
57 //log_info("app added. id=%"PRIu64", name='%s', %srunning, %s, level %u", id, name, running ? "" : "not ", terminal ? "terminal" : "shell", (unsigned int)level);
58 world_tree_add_app(context, id, name, running, terminal, level);
61 static void app_state_changed(void * context, uint64_t id, const char * name, bool running, bool terminal, uint8_t level)
63 //log_info("app state changed. id=%"PRIu64", name='%s', %srunning, %s, level %u", id, name, running ? "" : "not ", terminal ? "terminal" : "shell", (unsigned int)level);
64 world_tree_app_state_changed(context, id, name, running, terminal, level);
67 static void app_removed(void * context, uint64_t id)
69 //log_info("app removed. id=%"PRIu64, id);
70 world_tree_remove_app(context, id);
73 bool
74 create_view(
75 const char * name,
76 const char * service,
77 const char * object,
78 bool graph_dict_supported,
79 bool app_supervisor_supported,
80 bool force_activate,
81 graph_view_handle * handle_ptr)
83 struct graph_view * view_ptr;
85 view_ptr = malloc(sizeof(struct graph_view));
86 if (view_ptr == NULL)
88 log_error("malloc() failed for struct graph_view");
89 goto fail;
92 view_ptr->name = strdup(name);
93 if (view_ptr->name == NULL)
95 log_error("strdup() failed for \"%s\"", name);
96 goto free_view;
99 view_ptr->app_supervisor = NULL;
101 if (!graph_proxy_create(service, object, graph_dict_supported, &view_ptr->graph))
103 goto free_name;
106 if (!graph_canvas_create(1600 * 2, 1200 * 2, &view_ptr->graph_canvas))
108 goto destroy_graph;
111 if (!graph_canvas_attach(view_ptr->graph_canvas, view_ptr->graph))
113 goto destroy_graph_canvas;
116 view_ptr->canvas_widget = canvas_get_widget(graph_canvas_get_canvas(view_ptr->graph_canvas));
118 list_add_tail(&view_ptr->siblings, &g_views);
120 gtk_widget_show(view_ptr->canvas_widget);
122 world_tree_add((graph_view_handle)view_ptr, force_activate);
124 if (app_supervisor_supported)
126 if (!ladish_app_supervisor_proxy_create(service, object, view_ptr, app_added, app_state_changed, app_removed, &view_ptr->app_supervisor))
128 goto detach_graph_canvas;
132 if (!graph_proxy_activate(view_ptr->graph))
134 goto free_app_supervisor;
137 *handle_ptr = (graph_view_handle)view_ptr;
139 return true;
141 free_app_supervisor:
142 if (view_ptr->app_supervisor != NULL)
144 ladish_app_supervisor_proxy_destroy(view_ptr->app_supervisor);
146 detach_graph_canvas:
147 graph_canvas_detach(view_ptr->graph_canvas);
148 destroy_graph_canvas:
149 graph_canvas_destroy(view_ptr->graph_canvas);
150 destroy_graph:
151 graph_proxy_destroy(view_ptr->graph);
152 free_name:
153 free(view_ptr->name);
154 free_view:
155 free(view_ptr);
156 fail:
157 return false;
160 static void attach_canvas(struct graph_view * view_ptr)
162 GtkWidget * child;
164 child = gtk_bin_get_child(GTK_BIN(g_main_scrolledwin));
166 if (child == view_ptr->canvas_widget)
168 return;
171 if (child != NULL)
173 gtk_container_remove(GTK_CONTAINER(g_main_scrolledwin), child);
176 g_current_view = view_ptr;
177 gtk_container_add(GTK_CONTAINER(g_main_scrolledwin), view_ptr->canvas_widget);
179 //_main_scrolledwin->property_hadjustment().get_value()->set_step_increment(10);
180 //_main_scrolledwin->property_vadjustment().get_value()->set_step_increment(10);
183 static void detach_canvas(struct graph_view * view_ptr)
185 GtkWidget * child;
187 child = gtk_bin_get_child(GTK_BIN(g_main_scrolledwin));
188 if (child == view_ptr->canvas_widget)
190 gtk_container_remove(GTK_CONTAINER(g_main_scrolledwin), view_ptr->canvas_widget);
191 g_current_view = NULL;
195 #define view_ptr ((struct graph_view *)view)
197 void destroy_view(graph_view_handle view)
199 list_del(&view_ptr->siblings);
200 if (!list_empty(&g_views))
202 world_tree_activate((graph_view_handle)list_entry(g_views.next, struct graph_view, siblings));
204 else
206 set_main_window_title(NULL);
209 detach_canvas(view_ptr);
211 world_tree_remove(view);
213 graph_canvas_detach(view_ptr->graph_canvas);
214 graph_canvas_destroy(view_ptr->graph_canvas);
215 graph_proxy_destroy(view_ptr->graph);
217 if (view_ptr->app_supervisor != NULL)
219 ladish_app_supervisor_proxy_destroy(view_ptr->app_supervisor);
222 free(view_ptr->name);
223 free(view_ptr);
226 void activate_view(graph_view_handle view)
228 attach_canvas(view_ptr);
229 set_main_window_title(view);
232 const char * get_view_name(graph_view_handle view)
234 return view_ptr->name;
237 bool set_view_name(graph_view_handle view, const char * cname)
239 char * name;
241 name = strdup(cname);
242 if (name == NULL)
244 log_error("strdup() failed for \"%s\"", name);
245 return false;
248 free(view_ptr->name);
249 view_ptr->name = name;
251 world_tree_name_changed(view);
253 if (g_current_view == view_ptr)
255 set_main_window_title(view);
258 return true;
261 canvas_handle get_current_canvas()
263 if (g_current_view == NULL)
265 return NULL;
268 return graph_canvas_get_canvas(g_current_view->graph_canvas);
271 bool app_run_custom(graph_view_handle view, const char * command, const char * name, bool run_in_terminal)
273 return ladish_app_supervisor_proxy_run_custom(view_ptr->app_supervisor, command, name, run_in_terminal);