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