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_line_cnt (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)
173 PsppireDialogActionComments *act = PSPPIRE_DIALOG_ACTION_COMMENTS (pda);
175 GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
176 GtkBuilder *xml = g_hash_table_lookup (thing, pda);
181 xml = builder_new ("comments.ui");
182 g_hash_table_insert (thing, pda, xml);
184 pda->dialog = get_widget_assert (xml, "comments-dialog");
185 act->textview = get_widget_assert (xml, "comments-textview1");
186 GtkWidget *label = get_widget_assert (xml, "column-number-label");
187 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (act->textview));
188 act->check = get_widget_assert (xml, "comments-checkbutton1");
190 g_signal_connect_swapped (pda->dialog, "show", G_CALLBACK (retrieve_comments), pda);
193 PangoContext * context ;
194 PangoLayout * layout ;
198 /* Since we're going to truncate lines to 80 chars,
199 we need a monospaced font otherwise it'll look silly */
200 PangoFontDescription *font_desc =
201 pango_font_description_from_string ("monospace");
203 GtkStyleContext *style = gtk_widget_get_style_context (GTK_WIDGET (act->textview));
204 GtkCssProvider *cssp = gtk_css_provider_new ();
206 gchar *str = pango_font_description_to_string (font_desc);
208 g_strdup_printf ("* {font: %s}", str);
212 gtk_css_provider_load_from_data (cssp, css, -1, &err);
215 g_warning ("Failed to load font css \"%s\": %s", css, err->message);
220 gtk_style_context_add_provider (style,
221 GTK_STYLE_PROVIDER (cssp),
222 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
223 g_object_unref (cssp);
226 /* And let's just make sure that a complete line fits into the
228 context = gtk_widget_create_pango_context (act->textview);
229 layout = pango_layout_new (context);
231 pango_layout_set_text (layout, "M", 1);
233 pango_layout_set_font_description (layout, font_desc);
235 pango_layout_get_extents (layout, NULL, &rect);
237 g_object_set (act->textview, "width-request",
238 PANGO_PIXELS (rect.width) * DOC_LINE_LENGTH + 20, NULL);
240 g_object_unref (G_OBJECT (layout));
241 g_object_unref (G_OBJECT (context));
243 pango_font_description_free (font_desc);
246 g_signal_connect (buffer, "mark-set",
247 G_CALLBACK (set_column_number), label);
249 g_signal_connect_after (buffer, "insert-text",
250 G_CALLBACK (wrap_line), NULL);
252 gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
253 gtk_text_buffer_place_cursor (buffer, &iter);
256 psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
257 psppire_dialog_action_set_refresh (pda, refresh);
261 psppire_dialog_action_comments_class_init (PsppireDialogActionCommentsClass *class)
263 psppire_dialog_action_set_activation (class, psppire_dialog_action_comments_activate);
264 PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
268 psppire_dialog_action_comments_init (PsppireDialogActionComments *act)