Cleaned up GUI, by objectifying the data editor. Removed a number of global variables.
[pspp-builds.git] / src / ui / gui / syntax-editor-source.c
1 /*
2     PSPPIRE --- A Graphical User Interface for PSPP
3     Copyright (C) 2006  Free Software Foundation
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18     02110-1301, USA. */
19
20
21 #include <libpspp/getl.h>
22 #include <libpspp/alloc.h>
23 #include <libpspp/compiler.h>
24 #include <libpspp/str.h>
25
26 #include <stdlib.h>
27
28 #include <gtk/gtk.h>
29
30 #include "syntax-editor-source.h"
31 #include "syntax-editor.h"
32
33 struct syntax_editor_source
34   {
35     struct getl_interface parent;
36     const struct syntax_editor *se;
37     GtkTextIter i;
38     GtkTextIter end;
39   };
40
41
42 static bool
43 always_false (const struct getl_interface *i UNUSED)
44 {
45   return false;
46 }
47
48 /* Returns the name of the source */
49 static const char *
50 name (const struct getl_interface *i)
51 {
52   const struct syntax_editor_source *ses =
53     (const struct syntax_editor_source *) i;
54
55   return window_name ((struct window_editor *) ses->se);
56 }
57
58
59 /* Returns the location within the source */
60 static int
61 location (const struct getl_interface *i)
62 {
63   const struct syntax_editor_source *ses = (const struct syntax_editor_source *) i;
64
65   return gtk_text_iter_get_line (&ses->i);
66 }
67
68
69 static bool
70 read_line_from_buffer (struct getl_interface *i,
71                        struct string *line,
72                        enum getl_syntax *syntax_rules)
73 {
74   gchar *text;
75   GtkTextIter next_line;
76
77   struct syntax_editor_source *ses = (struct syntax_editor_source *) i;
78
79   if ( gtk_text_iter_compare (&ses->i, &ses->end) >= 0)
80     return false;
81
82   gtk_text_buffer_get_iter_at_line ( ses->se->buffer,
83                                      &next_line,
84                                      1 + gtk_text_iter_get_line (&ses->i)
85                                      );
86
87   text = gtk_text_buffer_get_text (ses->se->buffer,
88                                    &ses->i, &next_line,
89                                    FALSE);
90   g_strchomp (text);
91
92   ds_assign_cstr (line, text);
93
94   g_free (text);
95
96   gtk_text_iter_forward_line (&ses->i);
97
98   return true;
99 }
100
101
102 static void
103 close (struct getl_interface *i )
104 {
105   free (i);
106 }
107
108 struct getl_interface *
109 create_syntax_editor_source (const struct syntax_editor *se,
110                              GtkTextIter start,
111                              GtkTextIter stop
112                              )
113 {
114   struct syntax_editor_source *ses = xzalloc (sizeof *ses);
115
116   ses->se = se;
117   ses->i = start;
118   ses->end = stop;
119
120
121   ses->parent.interactive = always_false;
122   ses->parent.read = read_line_from_buffer;
123   ses->parent.close = close;
124
125   ses->parent.name = name;
126   ses->parent.location = location;
127
128
129   return (struct getl_interface *) ses;
130 }