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