Implemented the INSERT command.
[pspp-builds.git] / src / ui / gui / syntax-editor-source.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2006  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
18 #include <config.h>
19
20 #include <libpspp/getl.h>
21 #include <libpspp/alloc.h>
22 #include <libpspp/compiler.h>
23 #include <libpspp/str.h>
24
25 #include <stdlib.h>
26
27 #include <gtk/gtk.h>
28
29 #include "syntax-editor-source.h"
30 #include "syntax-editor.h"
31
32 struct syntax_editor_source
33   {
34     struct getl_interface parent;
35     const struct syntax_editor *se;
36     GtkTextIter i;
37     GtkTextIter end;
38   };
39
40
41 static bool
42 always_false (const struct getl_interface *i UNUSED)
43 {
44   return false;
45 }
46
47 /* Returns the name of the source */
48 static const char *
49 name (const struct getl_interface *i)
50 {
51   const struct syntax_editor_source *ses =
52     (const struct syntax_editor_source *) i;
53
54   return window_name ((const struct editor_window *) ses->se);
55 }
56
57
58 /* Returns the location within the source */
59 static int
60 location (const struct getl_interface *i)
61 {
62   const struct syntax_editor_source *ses = (const struct syntax_editor_source *) i;
63
64   return gtk_text_iter_get_line (&ses->i);
65 }
66
67
68 static bool
69 read_line_from_buffer (struct getl_interface *i,
70                        struct string *line)
71 {
72   gchar *text;
73   GtkTextIter next_line;
74
75   struct syntax_editor_source *ses = (struct syntax_editor_source *) i;
76
77   if ( gtk_text_iter_compare (&ses->i, &ses->end) >= 0)
78     return false;
79
80   next_line = ses->i;
81   gtk_text_iter_forward_line (&next_line);
82
83   text = gtk_text_buffer_get_text (ses->se->buffer,
84                                    &ses->i, &next_line,
85                                    FALSE);
86   g_strchomp (text);
87
88   ds_assign_cstr (line, text);
89
90   g_free (text);
91
92   gtk_text_iter_forward_line (&ses->i);
93
94   return true;
95 }
96
97
98 static void
99 do_close (struct getl_interface *i )
100 {
101   free (i);
102 }
103
104 struct getl_interface *
105 create_syntax_editor_source (const struct syntax_editor *se,
106                              GtkTextIter start,
107                              GtkTextIter stop
108                              )
109 {
110   struct syntax_editor_source *ses = xzalloc (sizeof *ses);
111
112   ses->se = se;
113   ses->i = start;
114   ses->end = stop;
115
116
117   ses->parent.interactive = always_false;
118   ses->parent.read = read_line_from_buffer;
119   ses->parent.close = do_close;
120
121   ses->parent.name = name;
122   ses->parent.location = location;
123
124
125   return (struct getl_interface *) ses;
126 }