1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include <gtksheet/gtksheet.h>
20 #include "psppire-dialog.h"
22 #include "data-editor.h"
23 #include <language/syntax-string-source.h>
24 #include "syntax-editor.h"
25 #include "psppire-var-store.h"
26 #include <libpspp/syntax-gen.h>
28 #include "comments-dialog.h"
30 #include "dialog-common.h"
33 #include <glade/glade.h>
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
47 static void refresh (PsppireDialog *dialog, const struct comment_dialog *);
48 static char *generate_syntax (const struct comment_dialog *);
51 set_column_number (GtkTextBuffer *textbuffer,
56 GtkLabel *label = data;
59 text = g_strdup_printf ( _("Column Number: %d"),
60 1 + gtk_text_iter_get_line_offset (iter));
62 gtk_label_set_text (label, text);
68 wrap_line (GtkTextBuffer *buffer,
74 gint chars = gtk_text_iter_get_chars_in_line (iter);
76 if ( chars > DOC_LINE_LENGTH )
78 GtkTextIter line_fold = *iter;
81 gtk_text_iter_set_line_offset (&line_fold, DOC_LINE_LENGTH);
83 gtk_text_buffer_insert (buffer, &line_fold, "\r\n", 2);
86 gtk_text_iter_forward_to_line_end (&cursor);
88 gtk_text_buffer_place_cursor (buffer, &cursor);
95 comments_dialog (GObject *o, gpointer data)
99 struct data_editor *de = data;
100 struct comment_dialog cd;
102 GladeXML *xml = XML_NEW ("psppire.glade");
104 GtkWidget *dialog = get_widget_assert (xml, "comments-dialog");
105 GtkWidget *textview = get_widget_assert (xml, "comments-textview1");
106 GtkWidget *label = get_widget_assert (xml, "column-number-label");
107 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));
109 GtkSheet *var_sheet =
110 GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
112 PsppireVarStore *vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
114 gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
117 PangoContext * context ;
118 PangoLayout * layout ;
121 /* Since we're going to truncate lines to 80 chars,
122 we need a monospaced font otherwise it'll look silly */
123 PangoFontDescription *font_desc =
124 pango_font_description_from_string ("monospace");
126 gtk_widget_modify_font (textview, font_desc);
129 /* and let's just make sure that a complete line fits into the
131 context = gtk_widget_create_pango_context (textview);
132 layout = pango_layout_new (context);
134 pango_layout_set_text (layout, "M", 1);
136 pango_layout_set_font_description (layout, font_desc);
138 pango_layout_get_extents (layout, NULL, &rect);
140 g_object_set (textview, "width-request",
141 PANGO_PIXELS (rect.width) * DOC_LINE_LENGTH + 20, NULL);
143 g_object_unref (G_OBJECT (layout));
144 g_object_unref (G_OBJECT (context));
146 pango_font_description_free (font_desc);
152 g_signal_connect (buffer, "mark-set",
153 G_CALLBACK (set_column_number), label);
155 g_signal_connect_after (buffer, "insert-text",
156 G_CALLBACK (wrap_line), NULL);
158 gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
159 gtk_text_buffer_place_cursor (buffer, &iter);
162 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &cd);
165 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
169 case GTK_RESPONSE_OK:
171 gchar *syntax = generate_syntax (&cd);
172 struct getl_interface *sss = create_syntax_string_source (syntax);
173 execute_syntax (sss);
178 case PSPPIRE_RESPONSE_PASTE:
180 gchar *syntax = generate_syntax (&cd);
182 struct syntax_editor *se =
183 (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
185 gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
195 g_object_unref (xml);
200 add_line_to_buffer (GtkTextBuffer *buffer, const char *line)
202 gtk_text_buffer_insert_at_cursor (buffer, line, -1);
204 gtk_text_buffer_insert_at_cursor (buffer, "\n", 1);
208 refresh (PsppireDialog *dialog, const struct comment_dialog *cd)
211 GtkWidget *tv = get_widget_assert (cd->xml, "comments-textview1");
212 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
214 gtk_text_buffer_set_text (buffer, "", 0);
216 for ( i = 0 ; i < dict_get_document_line_cnt (cd->dict->dict); ++i )
219 ds_init_empty (&str);
220 dict_get_document_line (cd->dict->dict, i, &str);
221 add_line_to_buffer (buffer, ds_cstr (&str));
229 generate_syntax (const struct comment_dialog *cd)
235 GtkWidget *tv = get_widget_assert (cd->xml, "comments-textview1");
236 GtkWidget *check = get_widget_assert (cd->xml, "comments-checkbutton1");
237 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
238 const char *existing_docs = dict_get_documents (cd->dict->dict);
240 str = g_string_new ("\n* Data File Comments.\n\n");
242 if ( NULL != existing_docs)
243 g_string_append (str, "DROP DOCUMENTS.\n");
245 g_string_append (str, "ADD DOCUMENT\n");
247 for (i = 0 ; i < gtk_text_buffer_get_line_count (buffer) ; ++i )
250 GtkTextIter start, end;
251 gtk_text_buffer_get_iter_at_line (buffer, &start, i);
255 gtk_text_iter_forward_to_line_end (&end);
257 if ( gtk_text_iter_ends_line (&start))
258 ds_init_cstr (&line, "");
261 gtk_text_buffer_get_text (buffer,
265 gen_quoted_string (&line);
267 g_string_append_printf (str, " %s\n", ds_cstr (&line));
271 g_string_append (str, " .\n");
275 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check)))
276 g_string_append (str, "DISPLAY DOCUMENTS.\n");
280 g_string_free (str, FALSE);