1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 Copyright (C) 2006 Sebastien Granjoux
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * It is a GtkTextView with an associated GtkAjustment, allowing to put in the
23 * GtkTextView only a part of a big amount of text. Cursor movements are not
24 * limited by the GtkTextView content but by the GtkAjustment.
25 *---------------------------------------------------------------------------*/
27 #include "chunk_view.h"
33 GtkAdjustment
*vadjustment
;
36 struct _DmaChunkViewClass
38 GtkTextViewClass parent_class
;
41 /* Used in dispose and finalize */
42 static GtkWidgetClass
*parent_class
= NULL
;
45 *---------------------------------------------------------------------------*/
48 set_adjustment_clamped (GtkAdjustment
*adj
, gdouble value
)
50 gdouble lower
, upper
, page_size
;
52 lower
= gtk_adjustment_get_lower (adj
);
53 upper
= gtk_adjustment_get_upper (adj
);
54 page_size
= gtk_adjustment_get_page_size (adj
);
60 if (value
> (upper
- page_size
))
62 value
= upper
- page_size
;
64 gtk_adjustment_set_value (adj
, value
);
68 *---------------------------------------------------------------------------*/
71 dma_chunk_view_move_cursor (GtkTextView
*text_view
,
74 gboolean extend_selection
)
76 DmaChunkView
*view
= DMA_CHUNK_VIEW (text_view
);
78 GtkTextBuffer
*buffer
;
81 gdouble value
= 0, step_increment
;
85 case GTK_MOVEMENT_LOGICAL_POSITIONS
:
86 case GTK_MOVEMENT_VISUAL_POSITIONS
:
87 case GTK_MOVEMENT_WORDS
:
88 case GTK_MOVEMENT_DISPLAY_LINE_ENDS
:
89 case GTK_MOVEMENT_HORIZONTAL_PAGES
:
91 case GTK_MOVEMENT_DISPLAY_LINES
:
92 case GTK_MOVEMENT_PARAGRAPHS
:
93 case GTK_MOVEMENT_PARAGRAPH_ENDS
:
94 buffer
= gtk_text_view_get_buffer (text_view
);
95 mark
= gtk_text_buffer_get_insert (buffer
);
96 gtk_text_buffer_get_iter_at_mark (buffer
, &cur
, mark
);
97 line
= gtk_text_iter_get_line (&cur
);
98 step_increment
= gtk_adjustment_get_step_increment (view
->vadjustment
);
100 if ((count
< 0) && (line
== 0))
102 value
+= count
* step_increment
;
103 set_adjustment_clamped (view
->vadjustment
, value
);
106 else if ((count
> 0) && (line
== gtk_text_buffer_get_line_count(buffer
) - 1))
108 value
+= count
* step_increment
;
109 set_adjustment_clamped (view
->vadjustment
, value
);
113 case GTK_MOVEMENT_PAGES
:
114 value
+= count
* gtk_adjustment_get_page_increment (view
->vadjustment
);
115 set_adjustment_clamped (view
->vadjustment
, value
);
117 case GTK_MOVEMENT_BUFFER_ENDS
:
118 set_adjustment_clamped (view
->vadjustment
,
119 count
< 0 ? gtk_adjustment_get_lower (view
->vadjustment
) : gtk_adjustment_get_upper (view
->vadjustment
));
125 GTK_TEXT_VIEW_CLASS (parent_class
)->move_cursor (text_view
,
131 *---------------------------------------------------------------------------*/
133 void dma_chunk_view_set_scroll_adjustment (DmaChunkView
*this, GtkAdjustment
* vadjustment
)
135 this->vadjustment
= vadjustment
;
139 *---------------------------------------------------------------------------*/
141 /* dispose is the first destruction step. It is used to unref object created
142 * with instance_init in order to break reference counting cycles. This
143 * function could be called several times. All function should still work
144 * after this call. It has to called its parents.*/
147 dma_chunk_view_dispose (GObject
*object
)
149 /*DmaChunkView *this = DMA_CHUNK_VIEW (obj);*/
151 G_OBJECT_CLASS (parent_class
)->dispose (object
);
154 /* finalize is the last destruction step. It must free all memory allocated
155 * with instance_init. It is called only one time just before releasing all
159 dma_chunk_view_finalize (GObject
*object
)
161 /*DmaChunkView *view = DMA_CHUNK_VIEW (object);*/
163 G_OBJECT_CLASS (parent_class
)->finalize (object
);
166 /* instance_init is the constructor. All functions should work after this
170 dma_chunk_view_instance_init (DmaChunkView
*view
)
172 // GtkAdjustment *adj;
174 // PangoFontDescription *font_desc;
178 /* class_init intialize the class itself not the instance */
181 dma_chunk_view_class_init (DmaChunkViewClass
* klass
)
183 GObjectClass
*gobject_class
;
184 GtkTextViewClass
*text_view_class
;
186 g_return_if_fail (klass
!= NULL
);
188 gobject_class
= G_OBJECT_CLASS (klass
);
189 text_view_class
= GTK_TEXT_VIEW_CLASS (klass
);
190 parent_class
= GTK_WIDGET_CLASS (g_type_class_peek_parent (klass
));
192 gobject_class
->dispose
= dma_chunk_view_dispose
;
193 gobject_class
->finalize
= dma_chunk_view_finalize
;
195 text_view_class
->move_cursor
= dma_chunk_view_move_cursor
;
199 dma_chunk_view_get_type (void)
201 static GType type
= 0;
205 static const GTypeInfo type_info
=
207 sizeof (DmaChunkViewClass
),
208 (GBaseInitFunc
) NULL
,
209 (GBaseFinalizeFunc
) NULL
,
210 (GClassInitFunc
) dma_chunk_view_class_init
,
211 (GClassFinalizeFunc
) NULL
,
212 NULL
, /* class_data */
213 sizeof (DmaChunkView
),
215 (GInstanceInitFunc
) dma_chunk_view_instance_init
,
216 NULL
/* value_table */
219 type
= g_type_register_static (GTK_TYPE_TEXT_VIEW
,
220 "DmaChunkView", &type_info
, 0);
226 /* Creation and Destruction
227 *---------------------------------------------------------------------------*/
230 dma_chunk_view_new (void)
234 this = g_object_new (DMA_CHUNK_VIEW_TYPE
, NULL
);
235 g_assert (this != NULL
);
241 dma_chunk_view_free (DmaChunkView
*this)
243 g_object_unref (this);