Fix a random crash when vim modelines are used
[anjuta.git] / libanjuta / anjuta-column-text-view.c
blob34943a4816b379c388fd26876bd5c39aedc4786f
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.
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.
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/>.
20 #include "anjuta-column-text-view.h"
22 struct _AnjutaColumnTextViewPriv
24 GtkWidget *text_view;
25 GtkWidget *column_label;
28 G_DEFINE_TYPE (AnjutaColumnTextView, anjuta_column_text_view, GTK_TYPE_BOX);
30 static void
31 set_text_view_column_label (GtkTextBuffer *buffer,
32 GtkTextIter *location,
33 GtkTextMark *mark,
34 GtkLabel *column_label)
36 gint column;
37 gchar *text;
39 column = gtk_text_iter_get_line_offset (location) + 1;
40 text = g_strdup_printf (_("Column %i"), column);
42 gtk_label_set_text (column_label, text);
44 g_free (text);
47 static void
48 anjuta_column_text_view_init (AnjutaColumnTextView *self)
50 GtkWidget *scrolled_window;
51 GtkTextBuffer *text_buffer;
53 /* Set properties */
54 g_object_set (G_OBJECT (self), "orientation", GTK_ORIENTATION_VERTICAL,
55 NULL);
57 self->priv = g_new0 (AnjutaColumnTextViewPriv, 1);
59 /* Text view */
60 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
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);
67 self->priv->text_view = gtk_text_view_new ();
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);
72 /* Column label */
73 self->priv->column_label = gtk_label_new (_("Column 1"));
75 /* Right justify the label text */
76 g_object_set (G_OBJECT (self->priv->column_label), "xalign", 1.0, NULL);
78 /* Column change handling */
79 text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->priv->text_view));
81 g_signal_connect (G_OBJECT (text_buffer), "mark-set",
82 G_CALLBACK (set_text_view_column_label),
83 self->priv->column_label);
85 gtk_box_pack_start (GTK_BOX (self), self->priv->column_label, FALSE, FALSE,
86 0);
88 /* Allow focusing */
89 gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
91 gtk_widget_show_all (GTK_WIDGET (self));
94 static void
95 anjuta_column_text_view_finalize (GObject *object)
97 AnjutaColumnTextView *self;
99 self = ANJUTA_COLUMN_TEXT_VIEW (object);
101 g_free (self->priv);
103 G_OBJECT_CLASS (anjuta_column_text_view_parent_class)->finalize (object);
106 static void
107 anjuta_column_text_view_grab_focus (GtkWidget *widget)
109 AnjutaColumnTextView *self;
111 self = ANJUTA_COLUMN_TEXT_VIEW (widget);
113 gtk_widget_grab_focus (self->priv->text_view);
116 static void
117 anjuta_column_text_view_class_init (AnjutaColumnTextViewClass *klass)
119 GObjectClass* object_class = G_OBJECT_CLASS (klass);
120 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
122 object_class->finalize = anjuta_column_text_view_finalize;
123 widget_class->grab_focus = anjuta_column_text_view_grab_focus;
127 GtkWidget *
128 anjuta_column_text_view_new (void)
130 return g_object_new (ANJUTA_TYPE_COLUMN_TEXT_VIEW, NULL);
133 gchar *
134 anjuta_column_text_view_get_text (AnjutaColumnTextView *self)
136 GtkTextBuffer *text_buffer;
137 GtkTextIter start_iter;
138 GtkTextIter end_iter;
139 gchar *text;
141 text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->priv->text_view));
143 gtk_text_buffer_get_start_iter (text_buffer, &start_iter);
144 gtk_text_buffer_get_end_iter (text_buffer, &end_iter) ;
146 text = gtk_text_buffer_get_text (text_buffer, &start_iter, &end_iter, FALSE);
148 return text;
151 GtkTextBuffer *
152 anjuta_column_text_view_get_buffer (AnjutaColumnTextView *self)
154 return gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->priv->text_view));