887f14f3eb927867f7fe22e026fa2025f518f982
[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 <config.h>
22
23 #include <libpspp/getl.h>
24 #include <libpspp/alloc.h>
25 #include <libpspp/compiler.h>
26 #include <libpspp/str.h>
27
28 #include <stdlib.h>
29
30 #include <gtk/gtk.h>
31
32 #include "syntax-editor-source.h"
33 #include "syntax-editor.h"
34
35 struct syntax_editor_source
36   {
37     struct getl_interface parent;
38     const struct syntax_editor *se;
39     GtkTextIter i;
40     GtkTextIter end;
41   };
42
43
44 static bool
45 always_false (const struct getl_interface *i UNUSED)
46 {
47   return false;
48 }
49
50 /* Returns the name of the source */
51 static const char *
52 name (const struct getl_interface *i)
53 {
54   const struct syntax_editor_source *ses =
55     (const struct syntax_editor_source *) i;
56
57   return window_name ((const struct editor_window *) ses->se);
58 }
59
60
61 /* Returns the location within the source */
62 static int
63 location (const struct getl_interface *i)
64 {
65   const struct syntax_editor_source *ses = (const struct syntax_editor_source *) i;
66
67   return gtk_text_iter_get_line (&ses->i);
68 }
69
70
71 static bool
72 read_line_from_buffer (struct getl_interface *i,
73                        struct string *line,
74                        enum getl_syntax *syntax_rules)
75 {
76   gchar *text;
77   GtkTextIter next_line;
78
79   struct syntax_editor_source *ses = (struct syntax_editor_source *) i;
80
81   if ( gtk_text_iter_compare (&ses->i, &ses->end) >= 0)
82     return false;
83
84   next_line = ses->i;
85   gtk_text_iter_forward_line (&next_line);
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 do_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 = do_close;
124
125   ses->parent.name = name;
126   ses->parent.location = location;
127
128
129   return (struct getl_interface *) ses;
130 }