Updated Spanish translation
[anjuta.git] / plugins / debug-manager / info.c
blobb3c6118ec3f7a358db0654710415e60156ed8df9
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 gdb_info.h
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
30 #include <glib.h>
31 #include <gtk/gtk.h>
33 #include <libanjuta/resources.h>
34 #include <glib/gi18n.h>
36 #include "utilities.h"
37 #include "info.h"
39 static GtkWidget *
40 create_dialog_with_textview (GtkWindow *parent, gint width, gint height)
42 GtkWidget *dialog;
43 GtkWidget *textview;
44 GtkWidget *scrolledwindow;
46 if (height < 250)
47 height = 250;
49 if (width < 400)
50 width = 400;
52 dialog = gtk_dialog_new_with_buttons (_("Information"), parent,
53 GTK_DIALOG_DESTROY_WITH_PARENT,
54 GTK_STOCK_CLOSE, GTK_RESPONSE_NONE,
55 NULL);
56 gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
57 gtk_window_set_default_size (GTK_WINDOW (dialog), 400, 250);
58 gtk_window_set_wmclass (GTK_WINDOW (dialog), "infoless", "Anjuta");
59 gtk_widget_show (dialog);
60 /* Auto close */
61 g_signal_connect (G_OBJECT (dialog), "response",
62 G_CALLBACK (gtk_widget_destroy), NULL);
64 scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
65 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
66 scrolledwindow, TRUE, TRUE, 0);
67 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
68 GTK_POLICY_AUTOMATIC,
69 GTK_POLICY_AUTOMATIC);
70 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow),
71 GTK_SHADOW_IN);
72 gtk_widget_show (scrolledwindow);
74 textview = gtk_text_view_new ();
75 gtk_text_view_set_editable (GTK_TEXT_VIEW (textview), FALSE);
76 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (textview), GTK_WRAP_WORD);
77 gtk_container_add (GTK_CONTAINER (scrolledwindow), textview);
78 gtk_widget_show (textview);
80 gtk_widget_show (dialog);
82 return textview;
85 static GtkWidget *
86 create_dialog_with_treeview (GtkWindow *parent, gint width, gint height)
88 GtkWidget *dialog;
89 GtkWidget *scrolledwindow;
90 GtkTreeModel *model;
91 GtkCellRenderer *renderer;
92 GtkTreeViewColumn *column;
93 GtkWidget *treeview;
95 if (height < 250)
96 height = 250;
98 if (width < 400)
99 width = 400;
101 dialog = gtk_dialog_new_with_buttons (_("Information"), parent,
102 GTK_DIALOG_DESTROY_WITH_PARENT,
103 GTK_STOCK_CLOSE, GTK_RESPONSE_NONE,
104 NULL);
105 gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
106 gtk_window_set_default_size (GTK_WINDOW (dialog), 400, 250);
107 gtk_window_set_wmclass (GTK_WINDOW (dialog), "infoless", "Anjuta");
108 gtk_widget_show (dialog);
109 /* Auto close */
110 g_signal_connect (G_OBJECT (dialog), "response",
111 G_CALLBACK (gtk_widget_destroy), NULL);
113 scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
114 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
115 scrolledwindow, TRUE, TRUE, 0);
116 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
117 GTK_POLICY_AUTOMATIC,
118 GTK_POLICY_AUTOMATIC);
119 gtk_widget_show (scrolledwindow);
120 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow),
121 GTK_SHADOW_IN);
123 model = GTK_TREE_MODEL (gtk_list_store_new (1, G_TYPE_STRING));
125 treeview = gtk_tree_view_new_with_model (model);
126 renderer = gtk_cell_renderer_text_new ();
127 column = gtk_tree_view_column_new_with_attributes (_("Lines"), renderer,
128 "text", 0, NULL);
129 gtk_tree_view_append_column (GTK_TREE_VIEW(treeview), column);
130 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(treeview), FALSE);
131 gtk_container_add (GTK_CONTAINER (scrolledwindow), treeview);
132 gtk_widget_show (treeview);
133 g_object_unref (G_OBJECT (model));
135 return treeview;
138 gboolean
139 gdb_info_show_file (GtkWindow *parent, const gchar *path,
140 gint width, gint height)
142 FILE *f;
144 g_return_val_if_fail (path != NULL, FALSE);
146 if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
147 return FALSE;
149 if ((f = fopen (path, "r")) == NULL)
150 return FALSE;
152 if (!gdb_info_show_filestream (parent, f, width, height))
154 int errno_bak = errno;
156 fclose (f);
158 errno = errno_bak;
160 return FALSE;
163 return (fclose (f) != 0) ? FALSE : TRUE;
166 gboolean
167 gdb_info_show_command (GtkWindow *parent, const gchar *command_line,
168 gint width, gint height)
170 GError *err = NULL;
171 gchar *std_output = NULL;
172 gboolean ret;
174 g_return_val_if_fail (command_line != NULL, FALSE);
176 /* Note: we could use popen like gdb_info_show_file,
177 * but g_spawn is safier
179 if (!g_spawn_command_line_sync (command_line, &std_output,
180 NULL, NULL, &err))
182 g_warning ("%s", err->message);
183 g_error_free (err);
185 return FALSE;
188 if (!g_utf8_validate (std_output, strlen (std_output), NULL))
189 g_warning ("Invalid UTF-8 data encountered reading output of command '%s'",
190 command_line);
192 ret = gdb_info_show_string (parent, std_output, width, height);
194 g_free (std_output);
196 return ret;
199 gboolean
200 gdb_info_show_string (GtkWindow *parent, const gchar *s,
201 gint width, gint height)
203 GtkWidget *textview;
204 GtkTextBuffer *buffer;
206 g_return_val_if_fail (s != NULL, FALSE);
208 textview = create_dialog_with_textview (parent,
209 width, height);
210 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));
211 gtk_text_buffer_set_text (buffer, s, strlen (s));
213 return TRUE;
216 #define ANJUTA_INFO_TEXTVIEW_BUFSIZE 1024
218 gboolean
219 gdb_info_show_filestream (GtkWindow *parent, FILE *f,
220 gint width, gint height)
222 GtkWidget *textview;
223 GtkTextBuffer *buffer;
224 gchar buf[ANJUTA_INFO_TEXTVIEW_BUFSIZE];
226 g_return_val_if_fail (f != NULL, FALSE);
228 textview = create_dialog_with_textview (parent, width, height);
229 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));
231 errno = 0; /* Reset it to detect errors */
233 while (1) {
234 GtkTextIter iter;
235 gchar *s;
237 if ((s = fgets (buf, ANJUTA_INFO_TEXTVIEW_BUFSIZE, f)) == NULL)
238 break;
240 gtk_text_buffer_get_end_iter (buffer, &iter);
241 gtk_text_buffer_insert (buffer, &iter, buf, strlen (buf));
243 return errno ? FALSE : TRUE;
246 gboolean
247 gdb_info_show_fd (GtkWindow *parent, int file_descriptor,
248 gint width, gint height)
250 FILE *f;
252 if ((f = fdopen (file_descriptor, "r")) == NULL)
253 return FALSE;
255 if (!gdb_info_show_filestream (parent, f, width, height))
257 int errno_bak = errno;
259 fclose (f);
261 errno = errno_bak;
263 return FALSE;
266 return (fclose (f) != 0) ? FALSE : TRUE;
269 void
270 gdb_info_show_list (GtkWindow *parent, const GList* list,
271 gint width, gint height)
273 GtkWidget *treeview;
274 GtkTreeModel *model;
276 g_return_if_fail (list != NULL);
278 treeview = create_dialog_with_treeview (parent, width, height);
279 model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview));
281 for (; list; list = g_list_next (list))
283 GtkTreeIter iter;
284 gchar *tmp;
286 tmp = gdb_util_remove_white_spaces (list->data);
288 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
289 gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, tmp, -1);
291 g_free (tmp);