New objects psppire-window and psppire-syntax-window.
[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 "data-editor.h"
22 #include <language/syntax-string-source.h>
23 #include "psppire-syntax-window.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 #include <glade/glade.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   GladeXML *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 (GObject *o, gpointer data)
95 {
96   GtkTextIter iter;
97   gint response ;
98   struct data_editor *de = data;
99   struct comment_dialog cd;
100
101   GladeXML *xml = XML_NEW ("psppire.glade");
102
103   GtkWidget *dialog = get_widget_assert (xml, "comments-dialog");
104   GtkWidget *textview = get_widget_assert (xml, "comments-textview1");
105   GtkWidget *label = get_widget_assert (xml, "column-number-label");
106   GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));
107
108   PsppireVarStore *vs = NULL;
109
110   g_object_get (de->data_editor, "var-store", &vs, NULL);
111
112   gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
113
114   {
115     PangoContext * context ;
116     PangoLayout *  layout ;
117     PangoRectangle rect;
118
119     /* Since we're going to truncate lines to 80 chars,
120        we need a monospaced font otherwise it'll look silly */
121     PangoFontDescription *font_desc =
122       pango_font_description_from_string ("monospace");
123
124     gtk_widget_modify_font (textview, font_desc);
125
126
127     /* and let's just make sure that a complete line fits into the
128        widget's width */
129     context = gtk_widget_create_pango_context (textview);
130     layout = pango_layout_new (context);
131
132     pango_layout_set_text (layout, "M", 1);
133
134     pango_layout_set_font_description (layout, font_desc);
135
136     pango_layout_get_extents (layout, NULL, &rect);
137
138     g_object_set (textview, "width-request",
139                   PANGO_PIXELS (rect.width) * DOC_LINE_LENGTH + 20, NULL);
140
141     g_object_unref (G_OBJECT (layout));
142     g_object_unref (G_OBJECT (context));
143
144     pango_font_description_free (font_desc);
145   }
146
147   cd.xml = xml;
148   cd.dict = vs->dict;
149
150   g_signal_connect (buffer, "mark-set",
151                     G_CALLBACK (set_column_number), label);
152
153   g_signal_connect_after (buffer, "insert-text",
154                           G_CALLBACK (wrap_line), NULL);
155
156   gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
157   gtk_text_buffer_place_cursor (buffer, &iter);
158
159
160   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &cd);
161
162
163   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
164
165   switch (response)
166     {
167     case GTK_RESPONSE_OK:
168       {
169         gchar *syntax = generate_syntax (&cd);
170         struct getl_interface *sss = create_syntax_string_source (syntax);
171         execute_syntax (sss);
172
173         g_free (syntax);
174       }
175       break;
176     case PSPPIRE_RESPONSE_PASTE:
177       {
178         gchar *syntax = generate_syntax (&cd);
179
180         GtkWidget *se = psppire_syntax_window_new ();
181
182         gtk_text_buffer_insert_at_cursor (PSPPIRE_SYNTAX_WINDOW (se)->buffer, syntax, -1);
183
184         gtk_widget_show (se);
185
186         g_free (syntax);
187       }
188       break;
189     default:
190       break;
191     }
192
193
194   g_object_unref (xml);
195 }
196
197
198 static void
199 add_line_to_buffer (GtkTextBuffer *buffer, const char *line)
200 {
201   gtk_text_buffer_insert_at_cursor (buffer, line, -1);
202
203   gtk_text_buffer_insert_at_cursor (buffer, "\n", 1);
204 }
205
206 static void
207 refresh (PsppireDialog *dialog, const struct comment_dialog *cd)
208 {
209   gint i;
210   GtkWidget *tv = get_widget_assert (cd->xml, "comments-textview1");
211   GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
212
213   gtk_text_buffer_set_text (buffer, "", 0);
214
215   for ( i = 0 ; i < dict_get_document_line_cnt (cd->dict->dict); ++i )
216     {
217       struct string str;
218       ds_init_empty (&str);
219       dict_get_document_line (cd->dict->dict, i, &str);
220       add_line_to_buffer (buffer, ds_cstr (&str));
221       ds_destroy (&str);
222     }
223 }
224
225
226
227 static char *
228 generate_syntax (const struct comment_dialog *cd)
229 {
230   gint i;
231
232   GString *str;
233   gchar *text;
234   GtkWidget *tv = get_widget_assert (cd->xml, "comments-textview1");
235   GtkWidget *check = get_widget_assert (cd->xml, "comments-checkbutton1");
236   GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
237   const char *existing_docs = dict_get_documents (cd->dict->dict);
238
239   str = g_string_new ("\n* Data File Comments.\n\n");
240
241   if ( NULL != existing_docs)
242     g_string_append (str, "DROP DOCUMENTS.\n");
243
244   g_string_append (str, "ADD DOCUMENT\n");
245
246   for (i = 0 ; i < gtk_text_buffer_get_line_count (buffer) ; ++i )
247     {
248       struct string tmp;
249       GtkTextIter start;
250       char *line;
251
252       gtk_text_buffer_get_iter_at_line (buffer, &start, i);
253       if (gtk_text_iter_ends_line (&start))
254         line = g_strdup ("");
255       else
256         {
257           GtkTextIter end = start;
258           gtk_text_iter_forward_to_line_end (&end);
259           line = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
260         }
261
262       ds_init_empty (&tmp);
263       syntax_gen_string (&tmp, ss_cstr (line));
264       g_free (line);
265
266       g_string_append_printf (str, " %s\n", ds_cstr (&tmp));
267
268       ds_destroy (&tmp);
269     }
270   g_string_append (str, " .\n");
271
272
273
274   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check)))
275     g_string_append (str, "DISPLAY DOCUMENTS.\n");
276
277   text = str->str;
278
279   g_string_free (str, FALSE);
280
281   return text;
282 }