1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * anjuta
4  * Copyright (C) James Liggett 2010 <jrliggett@cox.net>
5  *
6  * anjuta is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * anjuta is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "anjuta-column-text-view.h"
21 
22 struct _AnjutaColumnTextViewPriv
23 {
24 	GtkWidget *text_view;
25 	GtkWidget *column_label;
26 };
27 
28 G_DEFINE_TYPE (AnjutaColumnTextView, anjuta_column_text_view, GTK_TYPE_BOX);
29 
30 static void
set_text_view_column_label(GtkTextBuffer * buffer,GtkTextIter * location,GtkTextMark * mark,GtkLabel * column_label)31 set_text_view_column_label (GtkTextBuffer *buffer,
32                             GtkTextIter *location,
33                             GtkTextMark *mark,
34                             GtkLabel *column_label)
35 {
36 	gint column;
37 	gchar *text;
38 
39 	column = gtk_text_iter_get_line_offset (location) + 1;
40 	text = g_strdup_printf (_("Column %i"), column);
41 
42 	gtk_label_set_text (column_label, text);
43 
44 	g_free (text);
45 }
46 
47 static void
anjuta_column_text_view_init(AnjutaColumnTextView * self)48 anjuta_column_text_view_init (AnjutaColumnTextView *self)
49 {
50 	GtkWidget *scrolled_window;
51 	GtkTextBuffer *text_buffer;
52 
53 	/* Set properties */
54 	g_object_set (G_OBJECT (self), "orientation", GTK_ORIENTATION_VERTICAL,
55 	              NULL);
56 
57 	self->priv = g_new0 (AnjutaColumnTextViewPriv, 1);
58 
59 	/* Text view */
60 	scrolled_window = gtk_scrolled_window_new (NULL, NULL);
61 
62 	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
63 	                                GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
64 	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
65 	                                     GTK_SHADOW_IN);
66 
67 	self->priv->text_view = gtk_text_view_new ();
68 
69 	gtk_container_add (GTK_CONTAINER (scrolled_window), self->priv->text_view);
70 	gtk_box_pack_start (GTK_BOX (self), scrolled_window, TRUE, TRUE, 0);
71 
72 	/* Column label */
73 	self->priv->column_label = gtk_label_new (_("Column 1"));
74 
75 	/* Right justify the label text */
76 	g_object_set (G_OBJECT (self->priv->column_label), "xalign", 1.0, NULL);
77 
78 	/* Column change handling */
79 	text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->priv->text_view));
80 
81 	g_signal_connect (G_OBJECT (text_buffer), "mark-set",
82 	                  G_CALLBACK (set_text_view_column_label),
83 	                  self->priv->column_label);
84 
85 	gtk_box_pack_start (GTK_BOX (self), self->priv->column_label, FALSE, FALSE,
86 	                    0);
87 
88 	/* Allow focusing */
89 	gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
90 
91 	gtk_widget_show_all (GTK_WIDGET (self));
92 }
93 
94 static void
anjuta_column_text_view_finalize(GObject * object)95 anjuta_column_text_view_finalize (GObject *object)
96 {
97 	AnjutaColumnTextView *self;
98 
99 	self = ANJUTA_COLUMN_TEXT_VIEW (object);
100 
101 	g_free (self->priv);
102 
103 	G_OBJECT_CLASS (anjuta_column_text_view_parent_class)->finalize (object);
104 }
105 
106 static void
anjuta_column_text_view_grab_focus(GtkWidget * widget)107 anjuta_column_text_view_grab_focus (GtkWidget *widget)
108 {
109 	AnjutaColumnTextView *self;
110 
111 	self = ANJUTA_COLUMN_TEXT_VIEW (widget);
112 
113 	gtk_widget_grab_focus (self->priv->text_view);
114 }
115 
116 static void
anjuta_column_text_view_class_init(AnjutaColumnTextViewClass * klass)117 anjuta_column_text_view_class_init (AnjutaColumnTextViewClass *klass)
118 {
119 	GObjectClass* object_class = G_OBJECT_CLASS (klass);
120 	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
121 
122 	object_class->finalize = anjuta_column_text_view_finalize;
123 	widget_class->grab_focus = anjuta_column_text_view_grab_focus;
124 }
125 
126 
127 GtkWidget *
anjuta_column_text_view_new(void)128 anjuta_column_text_view_new (void)
129 {
130 	return g_object_new (ANJUTA_TYPE_COLUMN_TEXT_VIEW, NULL);
131 }
132 
133 gchar *
anjuta_column_text_view_get_text(AnjutaColumnTextView * self)134 anjuta_column_text_view_get_text (AnjutaColumnTextView *self)
135 {
136 	GtkTextBuffer *text_buffer;
137 	GtkTextIter start_iter;
138 	GtkTextIter end_iter;
139 	gchar *text;
140 
141 	text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->priv->text_view));
142 
143 	gtk_text_buffer_get_start_iter (text_buffer, &start_iter);
144 	gtk_text_buffer_get_end_iter (text_buffer, &end_iter) ;
145 
146 	text = gtk_text_buffer_get_text (text_buffer, &start_iter, &end_iter, FALSE);
147 
148 	return text;
149 }
150 
151 GtkTextBuffer *
anjuta_column_text_view_get_buffer(AnjutaColumnTextView * self)152 anjuta_column_text_view_get_buffer (AnjutaColumnTextView *self)
153 {
154 	return gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->priv->text_view));
155 }