c6d8668c1facbe1596a89ed60bba5c9387288e3c
[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 ((const struct editor_window *) 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   next_line = ses->i;
83   gtk_text_iter_forward_line (&next_line);
84
85   text = gtk_text_buffer_get_text (ses->se->buffer,
86                                    &ses->i, &next_line,
87                                    FALSE);
88   g_strchomp (text);
89
90   ds_assign_cstr (line, text);
91
92   g_free (text);
93
94   gtk_text_iter_forward_line (&ses->i);
95
96   return true;
97 }
98
99
100 static void
101 close (struct getl_interface *i )
102 {
103   free (i);
104 }
105
106 struct getl_interface *
107 create_syntax_editor_source (const struct syntax_editor *se,
108                              GtkTextIter start,
109                              GtkTextIter stop
110                              )
111 {
112   struct syntax_editor_source *ses = xzalloc (sizeof *ses);
113
114   ses->se = se;
115   ses->i = start;
116   ses->end = stop;
117
118
119   ses->parent.interactive = always_false;
120   ses->parent.read = read_line_from_buffer;
121   ses->parent.close = close;
122
123   ses->parent.name = name;
124   ses->parent.location = location;
125
126
127   return (struct getl_interface *) ses;
128 }