gui: short instructions for first time users
[ladish.git] / gui / graph_view.c
blob7d7dfecf0b5589791345330db39d0053df6ac32b
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"
32 struct graph_view
34 struct list_head siblings;
35 char * name;
36 graph_canvas_handle graph_canvas;
37 graph_proxy_handle graph;
38 GtkWidget * canvas_widget;
39 ladish_app_supervisor_proxy_handle app_supervisor;
42 struct list_head g_views;
44 GtkScrolledWindow * g_main_scrolledwin;
45 static struct graph_view * g_current_view;
46 GtkWidget * g_view_label;
48 const char * g_view_lavel_text =
49 "If you've started ladish for the first time, you should:\n\n"
50 " 1. Create a new studio (in the menu, Studio -> New Studio)\n"
51 " 2. Configure JACK (in the menu, Tools -> Configure JACK)\n"
52 " 3. Start the studio (in the menu, Studio -> Start Studio)\n"
53 " 4. Start apps (in the menu, Application -> Run)\n"
54 " 5. Connect their ports by click & drag on canvas\n"
55 " 6. Save the studio (in the menu, Studio -> Save Studio)\n";
57 void view_init(void)
59 g_main_scrolledwin = GTK_SCROLLED_WINDOW(get_glade_widget("main_scrolledwin"));
60 INIT_LIST_HEAD(&g_views);
62 g_view_label = gtk_label_new(g_view_lavel_text);
63 g_object_ref(g_view_label);
64 gtk_widget_show(g_view_label);
66 g_current_view = NULL;
67 gtk_scrolled_window_add_with_viewport(g_main_scrolledwin, g_view_label);
70 static void app_added(void * context, uint64_t id, const char * name, bool running, bool terminal, uint8_t level)
72 //log_info("app added. id=%"PRIu64", name='%s', %srunning, %s, level %u", id, name, running ? "" : "not ", terminal ? "terminal" : "shell", (unsigned int)level);
73 world_tree_add_app(context, id, name, running, terminal, level);
76 static void app_state_changed(void * context, uint64_t id, const char * name, bool running, bool terminal, uint8_t level)
78 //log_info("app state changed. id=%"PRIu64", name='%s', %srunning, %s, level %u", id, name, running ? "" : "not ", terminal ? "terminal" : "shell", (unsigned int)level);
79 world_tree_app_state_changed(context, id, name, running, terminal, level);
82 static void app_removed(void * context, uint64_t id)
84 //log_info("app removed. id=%"PRIu64, id);
85 world_tree_remove_app(context, id);
88 bool
89 create_view(
90 const char * name,
91 const char * service,
92 const char * object,
93 bool graph_dict_supported,
94 bool app_supervisor_supported,
95 bool force_activate,
96 graph_view_handle * handle_ptr)
98 struct graph_view * view_ptr;
100 view_ptr = malloc(sizeof(struct graph_view));
101 if (view_ptr == NULL)
103 log_error("malloc() failed for struct graph_view");
104 goto fail;
107 view_ptr->name = strdup(name);
108 if (view_ptr->name == NULL)
110 log_error("strdup() failed for \"%s\"", name);
111 goto free_view;
114 view_ptr->app_supervisor = NULL;
116 if (!graph_proxy_create(service, object, graph_dict_supported, &view_ptr->graph))
118 goto free_name;
121 if (!graph_canvas_create(1600 * 2, 1200 * 2, &view_ptr->graph_canvas))
123 goto destroy_graph;
126 if (!graph_canvas_attach(view_ptr->graph_canvas, view_ptr->graph))
128 goto destroy_graph_canvas;
131 view_ptr->canvas_widget = canvas_get_widget(graph_canvas_get_canvas(view_ptr->graph_canvas));
133 list_add_tail(&view_ptr->siblings, &g_views);
135 gtk_widget_show(view_ptr->canvas_widget);
137 world_tree_add((graph_view_handle)view_ptr, force_activate);
139 if (app_supervisor_supported)
141 if (!ladish_app_supervisor_proxy_create(service, object, view_ptr, app_added, app_state_changed, app_removed, &view_ptr->app_supervisor))
143 goto detach_graph_canvas;
147 if (!graph_proxy_activate(view_ptr->graph))
149 goto free_app_supervisor;
152 *handle_ptr = (graph_view_handle)view_ptr;
154 return true;
156 free_app_supervisor:
157 if (view_ptr->app_supervisor != NULL)
159 ladish_app_supervisor_proxy_destroy(view_ptr->app_supervisor);
161 detach_graph_canvas:
162 graph_canvas_detach(view_ptr->graph_canvas);
163 destroy_graph_canvas:
164 graph_canvas_destroy(view_ptr->graph_canvas);
165 destroy_graph:
166 graph_proxy_destroy(view_ptr->graph);
167 free_name:
168 free(view_ptr->name);
169 free_view:
170 free(view_ptr);
171 fail:
172 return false;
175 static void attach_canvas(struct graph_view * view_ptr)
177 GtkWidget * child;
179 child = gtk_bin_get_child(GTK_BIN(g_main_scrolledwin));
181 if (child == view_ptr->canvas_widget)
183 return;
186 if (child != NULL)
188 gtk_container_remove(GTK_CONTAINER(g_main_scrolledwin), child);
191 g_current_view = view_ptr;
192 gtk_container_add(GTK_CONTAINER(g_main_scrolledwin), view_ptr->canvas_widget);
194 //_main_scrolledwin->property_hadjustment().get_value()->set_step_increment(10);
195 //_main_scrolledwin->property_vadjustment().get_value()->set_step_increment(10);
198 static void detach_canvas(struct graph_view * view_ptr)
200 GtkWidget * child;
202 child = gtk_bin_get_child(GTK_BIN(g_main_scrolledwin));
203 if (child == view_ptr->canvas_widget)
205 gtk_container_remove(GTK_CONTAINER(g_main_scrolledwin), view_ptr->canvas_widget);
206 g_current_view = NULL;
207 gtk_widget_show(g_view_label);
208 gtk_scrolled_window_add_with_viewport(g_main_scrolledwin, g_view_label);
212 #define view_ptr ((struct graph_view *)view)
214 void destroy_view(graph_view_handle view)
216 list_del(&view_ptr->siblings);
217 if (!list_empty(&g_views))
219 world_tree_activate((graph_view_handle)list_entry(g_views.next, struct graph_view, siblings));
221 else
223 set_main_window_title(NULL);
226 detach_canvas(view_ptr);
228 world_tree_remove(view);
230 graph_canvas_detach(view_ptr->graph_canvas);
231 graph_canvas_destroy(view_ptr->graph_canvas);
232 graph_proxy_destroy(view_ptr->graph);
234 if (view_ptr->app_supervisor != NULL)
236 ladish_app_supervisor_proxy_destroy(view_ptr->app_supervisor);
239 free(view_ptr->name);
240 free(view_ptr);
243 void activate_view(graph_view_handle view)
245 attach_canvas(view_ptr);
246 set_main_window_title(view);
249 const char * get_view_name(graph_view_handle view)
251 return view_ptr->name;
254 bool set_view_name(graph_view_handle view, const char * cname)
256 char * name;
258 name = strdup(cname);
259 if (name == NULL)
261 log_error("strdup() failed for \"%s\"", name);
262 return false;
265 free(view_ptr->name);
266 view_ptr->name = name;
268 world_tree_name_changed(view);
270 if (g_current_view == view_ptr)
272 set_main_window_title(view);
275 return true;
278 canvas_handle get_current_canvas()
280 if (g_current_view == NULL)
282 return NULL;
285 return graph_canvas_get_canvas(g_current_view->graph_canvas);
288 bool app_run_custom(graph_view_handle view, const char * command, const char * name, bool run_in_terminal, uint8_t level)
290 return ladish_app_supervisor_proxy_run_custom(view_ptr->app_supervisor, command, name, run_in_terminal, level);
293 ladish_app_supervisor_proxy_handle graph_view_get_app_supervisor(graph_view_handle view)
295 return view_ptr->app_supervisor;