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