1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2010, 2011, 2012, 2013 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 "psppire-dialog.h"
20 #include "builder-wrapper.h"
21 #include "psppire-data-window.h"
22 #include "psppire-data-editor.h"
25 #include <ui/syntax-gen.h>
27 #include "comments-dialog.h"
29 #include "dialog-common.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
45 static void refresh (PsppireDialog *dialog, const struct comment_dialog *);
46 static char *generate_syntax (const struct comment_dialog *);
49 set_column_number (GtkTextBuffer *textbuffer,
54 GtkLabel *label = data;
57 text = g_strdup_printf ( _("Column Number: %d"),
58 1 + gtk_text_iter_get_line_offset (iter));
60 gtk_label_set_text (label, text);
66 wrap_line (GtkTextBuffer *buffer,
72 gint chars = gtk_text_iter_get_chars_in_line (iter);
74 if ( chars > DOC_LINE_LENGTH )
76 GtkTextIter line_fold = *iter;
78 gtk_text_iter_set_line_offset (&line_fold, DOC_LINE_LENGTH);
80 gtk_text_buffer_insert (buffer, &line_fold, "\r\n", 2);
87 comments_dialog (PsppireDataWindow *de)
91 struct comment_dialog cd;
93 GtkBuilder *xml = builder_new ("psppire.ui");
95 GtkWidget *dialog = get_widget_assert (xml, "comments-dialog");
96 GtkWidget *textview = get_widget_assert (xml, "comments-textview1");
97 GtkWidget *label = get_widget_assert (xml, "column-number-label");
98 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));
101 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
104 PangoContext * context ;
105 PangoLayout * layout ;
108 /* Since we're going to truncate lines to 80 chars,
109 we need a monospaced font otherwise it'll look silly */
110 PangoFontDescription *font_desc =
111 pango_font_description_from_string ("monospace");
113 gtk_widget_modify_font (textview, font_desc);
116 /* and let's just make sure that a complete line fits into the
118 context = gtk_widget_create_pango_context (textview);
119 layout = pango_layout_new (context);
121 pango_layout_set_text (layout, "M", 1);
123 pango_layout_set_font_description (layout, font_desc);
125 pango_layout_get_extents (layout, NULL, &rect);
127 g_object_set (textview, "width-request",
128 PANGO_PIXELS (rect.width) * DOC_LINE_LENGTH + 20, NULL);
130 g_object_unref (G_OBJECT (layout));
131 g_object_unref (G_OBJECT (context));
133 pango_font_description_free (font_desc);
137 g_object_get (de->data_editor, "dictionary", &cd.dict, NULL);
139 g_signal_connect (buffer, "mark-set",
140 G_CALLBACK (set_column_number), label);
142 g_signal_connect_after (buffer, "insert-text",
143 G_CALLBACK (wrap_line), NULL);
145 gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
146 gtk_text_buffer_place_cursor (buffer, &iter);
149 g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &cd);
152 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
156 case GTK_RESPONSE_OK:
157 g_free (execute_syntax_string (de, generate_syntax (&cd)));
159 case PSPPIRE_RESPONSE_PASTE:
160 g_free (paste_syntax_to_window (generate_syntax (&cd)));
167 g_object_unref (xml);
172 add_line_to_buffer (GtkTextBuffer *buffer, const char *line)
174 gtk_text_buffer_insert_at_cursor (buffer, line, -1);
176 gtk_text_buffer_insert_at_cursor (buffer, "\n", 1);
180 refresh (PsppireDialog *dialog, const struct comment_dialog *cd)
183 GtkWidget *tv = get_widget_assert (cd->xml, "comments-textview1");
184 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
186 gtk_text_buffer_set_text (buffer, "", 0);
188 for ( i = 0 ; i < dict_get_document_line_cnt (cd->dict->dict); ++i )
189 add_line_to_buffer (buffer, dict_get_document_line (cd->dict->dict, i));
195 generate_syntax (const struct comment_dialog *cd)
201 GtkWidget *tv = get_widget_assert (cd->xml, "comments-textview1");
202 GtkWidget *check = get_widget_assert (cd->xml, "comments-checkbutton1");
203 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
205 str = g_string_new ("\n* Data File Comments.\n\n");
207 if (dict_get_documents (cd->dict->dict) != NULL)
208 g_string_append (str, "DROP DOCUMENTS.\n");
210 g_string_append (str, "ADD DOCUMENT\n");
212 for (i = 0 ; i < gtk_text_buffer_get_line_count (buffer) ; ++i )
218 gtk_text_buffer_get_iter_at_line (buffer, &start, i);
219 if (gtk_text_iter_ends_line (&start))
220 line = g_strdup ("");
223 GtkTextIter end = start;
224 gtk_text_iter_forward_to_line_end (&end);
225 line = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
228 ds_init_empty (&tmp);
229 syntax_gen_string (&tmp, ss_cstr (line));
232 g_string_append_printf (str, " %s\n", ds_cstr (&tmp));
236 g_string_append (str, " .\n");
240 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check)))
241 g_string_append (str, "DISPLAY DOCUMENTS.\n");
245 g_string_free (str, FALSE);