1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2010, 2011, 2012, 2013, 2015 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/>. */
20 #include "psppire-dialog-action-comments.h"
21 #include "psppire-selector.h"
22 #include "psppire-var-view.h"
23 #include "dict-display.h"
25 #include "psppire-dialog.h"
26 #include "builder-wrapper.h"
27 #include <ui/syntax-gen.h>
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
33 static void psppire_dialog_action_comments_init (PsppireDialogActionComments *act);
34 static void psppire_dialog_action_comments_class_init (PsppireDialogActionCommentsClass *class);
36 G_DEFINE_TYPE (PsppireDialogActionComments, psppire_dialog_action_comments, PSPPIRE_TYPE_DIALOG_ACTION);
39 generate_syntax (const PsppireDialogAction *pda)
41 PsppireDialogActionComments *cd = PSPPIRE_DIALOG_ACTION_COMMENTS (pda);
46 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (cd->textview));
48 str = g_string_new ("\n* Data File Comments.\n\n");
50 if (dict_get_documents (pda->dict->dict) != NULL)
51 g_string_append (str, "DROP DOCUMENTS.\n");
53 g_string_append (str, "ADD DOCUMENT\n");
55 for (i = 0 ; i < gtk_text_buffer_get_line_count (buffer) ; ++i)
61 gtk_text_buffer_get_iter_at_line (buffer, &start, i);
62 if (gtk_text_iter_ends_line (&start))
66 GtkTextIter end = start;
67 gtk_text_iter_forward_to_line_end (&end);
68 line = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
72 syntax_gen_string (&tmp, ss_cstr (line));
75 g_string_append_printf (str, " %s\n", ds_cstr (&tmp));
79 g_string_append (str, " .\n");
82 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cd->check)))
83 g_string_append (str, "DISPLAY DOCUMENTS.\n");
87 g_string_free (str, FALSE);
94 dialog_state_valid (gpointer data)
100 add_line_to_buffer (GtkTextBuffer *buffer, const char *line)
102 gtk_text_buffer_insert_at_cursor (buffer, line, -1);
104 gtk_text_buffer_insert_at_cursor (buffer, "\n", 1);
108 retrieve_comments (PsppireDialogAction *pda)
110 PsppireDialogActionComments *wcd = PSPPIRE_DIALOG_ACTION_COMMENTS (pda);
113 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (wcd->textview));
115 gtk_text_buffer_set_text (buffer, "", 0);
117 for (i = 0 ; i < dict_get_document_n_lines (pda->dict->dict); ++i)
118 add_line_to_buffer (buffer, dict_get_document_line (pda->dict->dict, i));
123 refresh (PsppireDialogAction *pda)
125 PsppireDialogActionComments *act = PSPPIRE_DIALOG_ACTION_COMMENTS (pda);
127 retrieve_comments (pda);
129 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (act->check), FALSE);
134 set_column_number (GtkTextBuffer *textbuffer,
139 GtkLabel *label = data;
142 text = g_strdup_printf (_("Column Number: %d"),
143 1 + gtk_text_iter_get_line_offset (iter));
145 gtk_label_set_text (label, text);
151 wrap_line (GtkTextBuffer *buffer,
157 gint chars = gtk_text_iter_get_chars_in_line (iter);
159 if (chars > DOC_LINE_LENGTH)
161 GtkTextIter line_fold = *iter;
163 gtk_text_iter_set_line_offset (&line_fold, DOC_LINE_LENGTH);
165 gtk_text_buffer_insert (buffer, &line_fold, "\r\n", 2);
171 psppire_dialog_action_comments_activate (PsppireDialogAction *pda, GVariant *param)
173 PsppireDialogActionComments *act = PSPPIRE_DIALOG_ACTION_COMMENTS (pda);
175 GtkBuilder *xml = builder_new ("comments.ui");
177 pda->dialog = get_widget_assert (xml, "comments-dialog");
178 act->textview = get_widget_assert (xml, "comments-textview1");
179 GtkWidget *label = get_widget_assert (xml, "column-number-label");
180 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (act->textview));
181 act->check = get_widget_assert (xml, "comments-checkbutton1");
183 g_signal_connect_swapped (pda->dialog, "show", G_CALLBACK (retrieve_comments), pda);
186 /* Since we're going to truncate lines to 80 chars,
187 we need a monospaced font otherwise it'll look silly */
189 GtkStyleContext *style = gtk_widget_get_style_context (GTK_WIDGET (act->textview));
190 GtkCssProvider *cssp = gtk_css_provider_new ();
192 const gchar *css = "* {font-family: monospace}";
194 gtk_css_provider_load_from_data (cssp, css, -1, &err);
197 g_warning ("Failed to load font css \"%s\": %s", css, err->message);
201 gtk_style_context_add_provider (style,
202 GTK_STYLE_PROVIDER (cssp),
203 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
204 g_object_unref (cssp);
208 g_signal_connect (buffer, "mark-set",
209 G_CALLBACK (set_column_number), label);
211 g_signal_connect_after (buffer, "insert-text",
212 G_CALLBACK (wrap_line), NULL);
214 gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
215 gtk_text_buffer_place_cursor (buffer, &iter);
217 psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
218 psppire_dialog_action_set_refresh (pda, refresh);
224 psppire_dialog_action_comments_class_init (PsppireDialogActionCommentsClass *class)
226 PSPPIRE_DIALOG_ACTION_CLASS (class)->initial_activate = psppire_dialog_action_comments_activate;
227 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
231 psppire_dialog_action_comments_init (PsppireDialogActionComments *act)