2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2007 Free Software Foundation
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <gtksheet/gtksheet.h>
23 #include "psppire-dialog.h"
25 #include "data-editor.h"
26 #include <language/syntax-string-source.h>
27 #include "syntax-editor.h"
28 #include "psppire-var-store.h"
29 #include <libpspp/syntax-gen.h>
31 #include "comments-dialog.h"
33 #include "dialog-common.h"
36 #include <glade/glade.h>
40 #define _(msgid) gettext (msgid)
41 #define N_(msgid) msgid
50 static void refresh (PsppireDialog *dialog, const struct comment_dialog *);
51 static char *generate_syntax (const struct comment_dialog *);
54 set_column_number (GtkTextBuffer *textbuffer,
59 GtkLabel *label = data;
62 text = g_strdup_printf ( _("Column Number: %d"),
63 1 + gtk_text_iter_get_line_offset (iter));
65 gtk_label_set_text (label, text);
71 wrap_line (GtkTextBuffer *buffer,
77 gint chars = gtk_text_iter_get_chars_in_line (iter);
79 if ( chars > DOC_LINE_LENGTH )
81 GtkTextIter line_fold = *iter;
84 gtk_text_iter_set_line_offset (&line_fold, DOC_LINE_LENGTH);
86 gtk_text_buffer_insert (buffer, &line_fold, "\r\n", 2);
89 gtk_text_iter_forward_to_line_end (&cursor);
91 gtk_text_buffer_place_cursor (buffer, &cursor);
98 comments_dialog (GObject *o, gpointer data)
102 struct data_editor *de = data;
103 struct comment_dialog cd;
105 GladeXML *xml = XML_NEW ("psppire.glade");
107 GtkWidget *dialog = get_widget_assert (xml, "comments-dialog");
108 GtkWidget *textview = get_widget_assert (xml, "comments-textview1");
109 GtkWidget *label = get_widget_assert (xml, "column-number-label");
110 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));
112 GtkSheet *var_sheet =
113 GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
115 PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
117 gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
120 PangoContext * context ;
121 PangoLayout * layout ;
124 /* Since we're going to truncate lines to 80 chars,
125 we need a monospaced font otherwise it'll look silly */
126 PangoFontDescription *font_desc =
127 pango_font_description_from_string ("monospace");
129 gtk_widget_modify_font (textview, font_desc);
132 /* and let's just make sure that a complete line fits into the
134 context = gtk_widget_create_pango_context (textview);
135 layout = pango_layout_new (context);
137 pango_layout_set_text (layout, "M", 1);
139 pango_layout_set_font_description (layout, font_desc);
141 pango_layout_get_extents (layout, NULL, &rect);
143 g_object_set (textview, "width-request",
144 PANGO_PIXELS (rect.width) * DOC_LINE_LENGTH + 20, NULL);
146 g_object_unref (G_OBJECT (layout));
147 g_object_unref (G_OBJECT (context));
149 pango_font_description_free (font_desc);
155 g_signal_connect (buffer, "mark-set",
156 G_CALLBACK (set_column_number), label);
158 g_signal_connect_after (buffer, "insert-text",
159 G_CALLBACK (wrap_line), NULL);
161 gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
162 gtk_text_buffer_place_cursor (buffer, &iter);
165 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &cd);
168 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
172 case GTK_RESPONSE_OK:
174 gchar *syntax = generate_syntax (&cd);
175 struct getl_interface *sss = create_syntax_string_source (syntax);
176 execute_syntax (sss);
181 case PSPPIRE_RESPONSE_PASTE:
183 gchar *syntax = generate_syntax (&cd);
185 struct syntax_editor *se =
186 (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
188 gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
198 g_object_unref (xml);
203 add_line_to_buffer (GtkTextBuffer *buffer, const char *line)
205 gtk_text_buffer_insert_at_cursor (buffer, line, -1);
207 gtk_text_buffer_insert_at_cursor (buffer, "\n", 1);
211 refresh (PsppireDialog *dialog, const struct comment_dialog *cd)
214 GtkWidget *tv = get_widget_assert (cd->xml, "comments-textview1");
215 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
217 gtk_text_buffer_set_text (buffer, "", 0);
219 for ( i = 0 ; i < dict_get_document_line_cnt (cd->dict->dict); ++i )
222 ds_init_empty (&str);
223 dict_get_document_line (cd->dict->dict, i, &str);
224 add_line_to_buffer (buffer, ds_cstr (&str));
232 generate_syntax (const struct comment_dialog *cd)
238 GtkWidget *tv = get_widget_assert (cd->xml, "comments-textview1");
239 GtkWidget *check = get_widget_assert (cd->xml, "comments-checkbutton1");
240 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
241 const char *existing_docs = dict_get_documents (cd->dict->dict);
243 str = g_string_new ("\n* Data File Comments.\n\n");
245 if ( NULL != existing_docs)
246 g_string_append (str, "DROP DOCUMENTS.\n");
248 g_string_append (str, "ADD DOCUMENT\n");
250 for (i = 0 ; i < gtk_text_buffer_get_line_count (buffer) ; ++i )
253 GtkTextIter start, end;
254 gtk_text_buffer_get_iter_at_line (buffer, &start, i);
258 gtk_text_iter_forward_to_line_end (&end);
260 if ( gtk_text_iter_ends_line (&start))
261 ds_init_cstr (&line, "");
264 gtk_text_buffer_get_text (buffer,
268 gen_quoted_string (&line);
270 g_string_append_printf (str, " %s\n", ds_cstr (&line));
274 g_string_append (str, " .\n");
278 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check)))
279 g_string_append (str, "DISPLAY DOCUMENTS.\n");
283 g_string_free (str, FALSE);