Comments dialog: convert to PsppireDialogAction
[pspp] / src / ui / gui / psppire-dialog-action-comments.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C)  2007, 2010, 2011, 2012, 2013, 2015  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17
18 #include <config.h>
19
20 #include "psppire-dialog-action-comments.h"
21 #include "psppire-selector.h"
22 #include "psppire-var-view.h"
23 #include "dict-display.h"
24
25 #include "psppire-dialog.h"
26 #include "builder-wrapper.h"
27 #include <ui/syntax-gen.h>
28
29 #include <gettext.h>
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
32
33 static void psppire_dialog_action_comments_init            (PsppireDialogActionComments      *act);
34 static void psppire_dialog_action_comments_class_init      (PsppireDialogActionCommentsClass *class);
35
36 G_DEFINE_TYPE (PsppireDialogActionComments, psppire_dialog_action_comments, PSPPIRE_TYPE_DIALOG_ACTION);
37
38 static char *
39 generate_syntax (PsppireDialogAction *pda)
40 {
41   PsppireDialogActionComments *cd = PSPPIRE_DIALOG_ACTION_COMMENTS (pda);
42   gint i;
43
44   GString *str;
45   gchar *text;
46   GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (cd->textview));
47
48   str = g_string_new ("\n* Data File Comments.\n\n");
49
50   if (dict_get_documents (pda->dict->dict) != NULL)
51     g_string_append (str, "DROP DOCUMENTS.\n");
52
53   g_string_append (str, "ADD DOCUMENT\n");
54
55   for (i = 0 ; i < gtk_text_buffer_get_line_count (buffer) ; ++i )
56     {
57       struct string tmp;
58       GtkTextIter start;
59       char *line;
60
61       gtk_text_buffer_get_iter_at_line (buffer, &start, i);
62       if (gtk_text_iter_ends_line (&start))
63         line = g_strdup ("");
64       else
65         {
66           GtkTextIter end = start;
67           gtk_text_iter_forward_to_line_end (&end);
68           line = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
69         }
70
71       ds_init_empty (&tmp);
72       syntax_gen_string (&tmp, ss_cstr (line));
73       g_free (line);
74
75       g_string_append_printf (str, " %s\n", ds_cstr (&tmp));
76
77       ds_destroy (&tmp);
78     }
79   g_string_append (str, " .\n");
80
81
82   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cd->check)))
83     g_string_append (str, "DISPLAY DOCUMENTS.\n");
84
85   text = str->str;
86
87   g_string_free (str, FALSE);
88
89   return text;
90 }
91
92
93 static gboolean
94 dialog_state_valid (gpointer data)
95 {
96   return TRUE;
97 }
98
99 static void
100 add_line_to_buffer (GtkTextBuffer *buffer, const char *line)
101 {
102   gtk_text_buffer_insert_at_cursor (buffer, line, -1);
103
104   gtk_text_buffer_insert_at_cursor (buffer, "\n", 1);
105 }
106
107 static void
108 retrieve_comments (PsppireDialogAction *pda)
109 {
110   PsppireDialogActionComments *wcd = PSPPIRE_DIALOG_ACTION_COMMENTS (pda);
111   gint i;
112
113   GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (wcd->textview));
114
115   gtk_text_buffer_set_text (buffer, "", 0);
116
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));
119 }
120
121
122 static void
123 refresh (PsppireDialogAction *pda)
124 {
125   PsppireDialogActionComments *act = PSPPIRE_DIALOG_ACTION_COMMENTS (pda);
126
127   retrieve_comments (pda);
128
129   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (act->check), FALSE);
130 }
131
132
133 static void
134 set_column_number (GtkTextBuffer *textbuffer,
135      GtkTextIter   *iter,
136      GtkTextMark   *mark,
137      gpointer       data)
138 {
139   GtkLabel *label = data;
140   gchar *text ;
141
142   text = g_strdup_printf ( _("Column Number: %d"),
143                            1 + gtk_text_iter_get_line_offset (iter));
144
145   gtk_label_set_text (label, text);
146
147   g_free (text);
148 }
149
150 static void
151 wrap_line (GtkTextBuffer *buffer,
152      GtkTextIter   *iter,
153      gchar         *text,
154      gint           count,
155      gpointer       data)
156 {
157   gint chars = gtk_text_iter_get_chars_in_line (iter);
158
159   if ( chars > DOC_LINE_LENGTH )
160     {
161       GtkTextIter line_fold = *iter;
162
163       gtk_text_iter_set_line_offset (&line_fold, DOC_LINE_LENGTH);
164
165       gtk_text_buffer_insert (buffer, &line_fold, "\r\n", 2);
166     }
167 }
168
169
170 static void
171 psppire_dialog_action_comments_activate (PsppireDialogAction *pda)
172 {
173   PsppireDialogActionComments *act = PSPPIRE_DIALOG_ACTION_COMMENTS (pda);
174
175   GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
176   GtkBuilder *xml = g_hash_table_lookup (thing, pda);
177   if (!xml)
178     {
179       GtkTextIter iter;
180
181       xml = builder_new ("comments.ui");
182       g_hash_table_insert (thing, pda, xml);
183
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");
189
190       g_signal_connect_swapped (pda->dialog, "show", G_CALLBACK (retrieve_comments), pda);
191       
192       {
193         PangoContext * context ;
194         PangoLayout *  layout ;
195         PangoRectangle rect;
196
197         /* Since we're going to truncate lines to 80 chars,
198            we need a monospaced font otherwise it'll look silly */
199         PangoFontDescription *font_desc =
200           pango_font_description_from_string ("monospace");
201
202         gtk_widget_override_font (act->textview, font_desc);
203
204         /* and let's just make sure that a complete line fits into the
205            widget's width */
206         context = gtk_widget_create_pango_context (act->textview);
207         layout = pango_layout_new (context);
208
209         pango_layout_set_text (layout, "M", 1);
210
211         pango_layout_set_font_description (layout, font_desc);
212
213         pango_layout_get_extents (layout, NULL, &rect);
214
215         g_object_set (act->textview, "width-request",
216                       PANGO_PIXELS (rect.width) * DOC_LINE_LENGTH + 20, NULL);
217
218         g_object_unref (G_OBJECT (layout));
219         g_object_unref (G_OBJECT (context));
220
221         pango_font_description_free (font_desc);
222       }
223       
224       g_signal_connect (buffer, "mark-set",
225                         G_CALLBACK (set_column_number), label);
226
227       g_signal_connect_after (buffer, "insert-text",
228                               G_CALLBACK (wrap_line), NULL);
229
230       gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
231       gtk_text_buffer_place_cursor (buffer, &iter);
232     }
233   
234   psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
235   psppire_dialog_action_set_refresh (pda, refresh);
236
237
238
239   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_comments_parent_class)->activate)
240     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_comments_parent_class)->activate (pda);
241 }
242
243 static void
244 psppire_dialog_action_comments_class_init (PsppireDialogActionCommentsClass *class)
245 {
246   psppire_dialog_action_set_activation (class, psppire_dialog_action_comments_activate);
247   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
248 }
249
250 static void
251 psppire_dialog_action_comments_init (PsppireDialogActionComments *act)
252 {
253 }
254