File: gtksourcecompletioncontainer.c

package info (click to toggle)
gtksourceview3 3.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,184 kB
  • ctags: 5,650
  • sloc: ansic: 42,764; sh: 11,441; xml: 2,198; makefile: 742; python: 557; perl: 52; yacc: 45; cobol: 20; objc: 19; fortran: 14; cpp: 8
file content (263 lines) | stat: -rw-r--r-- 7,296 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
/* gtksourcecompletioncontainer.c
 * This file is part of GtkSourceView
 *
 * Copyright (C) 2013, 2014 - Sébastien Wilmet <swilmet@gnome.org>
 *
 * GtkSourceView is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * GtkSourceView is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * Custom sizing of the scrolled window containing the GtkTreeView containing
 * the completion proposals. If the GtkTreeView is small enough, the scrolled
 * window returns the natural size of the GtkTreeView. If it exceeds a certain
 * size, the scrolled window returns a smaller size, with the height at a row
 * boundary of the GtkTreeView (plus the size of the scrollbar(s) if needed).
 *
 * The purpose is to have a compact completion window, with a certain size
 * limit.
 */

#include "gtksourcecompletioncontainer.h"

#define UNREALIZED_WIDTH  350
#define MAX_HEIGHT        180

G_DEFINE_TYPE (GtkSourceCompletionContainer,
	       _gtk_source_completion_container,
	       GTK_TYPE_SCROLLED_WINDOW);

static gint
get_max_width (GtkSourceCompletionContainer *container)
{
	if (gtk_widget_get_realized (GTK_WIDGET (container)))
	{
		GtkWidget *toplevel;
		GdkWindow *window;
		GdkScreen *screen;
		gint max_width;
		gint xorigin;

		toplevel = gtk_widget_get_toplevel (GTK_WIDGET (container));
		window = gtk_widget_get_window (toplevel);
		screen = gdk_window_get_screen (window);

		gdk_window_get_origin (window, &xorigin, NULL);
		max_width = gdk_screen_get_width (screen) - xorigin;

		return MAX (max_width, UNREALIZED_WIDTH);
	}

	return UNREALIZED_WIDTH;
}

static gint
get_vertical_scrollbar_width (void)
{
	gint width;
	GtkWidget *scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, NULL);
	g_object_ref_sink (scrollbar);
	gtk_widget_show (scrollbar);

	gtk_widget_get_preferred_width (scrollbar, NULL, &width);

	g_object_unref (scrollbar);
	return width;
}

static gint
get_horizontal_scrollbar_height (void)
{
	gint height;
	GtkWidget *scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, NULL);
	g_object_ref_sink (scrollbar);
	gtk_widget_show (scrollbar);

	gtk_widget_get_preferred_height (scrollbar, NULL, &height);

	g_object_unref (scrollbar);
	return height;
}

/* This condition is used at several places, and it is important that it is the
 * same condition. So a function is better.
 */
static gboolean
needs_vertical_scrollbar (gint child_natural_height)
{
	return MAX_HEIGHT < child_natural_height;
}

static void
get_width (GtkSourceCompletionContainer *container,
	   gint                         *container_width,
	   gint                         *child_available_width)
{
	GtkWidget *child;
	GtkRequisition nat_size;
	gint width;
	gint scrollbar_width = 0;

	child = gtk_bin_get_child (GTK_BIN (container));
	gtk_widget_get_preferred_size (child, NULL, &nat_size);

	width = nat_size.width;

	if (needs_vertical_scrollbar (nat_size.height))
	{
		scrollbar_width = get_vertical_scrollbar_width ();
		width += scrollbar_width;
	}

	width = MIN (width, get_max_width (container));

	if (container_width != NULL)
	{
		*container_width = width;
	}

	if (child_available_width != NULL)
	{
		*child_available_width = width - scrollbar_width;
	}
}

static void
_gtk_source_completion_container_get_preferred_width (GtkWidget *widget,
						      gint      *min_width,
						      gint      *nat_width)
{
	GtkSourceCompletionContainer *container = GTK_SOURCE_COMPLETION_CONTAINER (widget);
	gint width;

	get_width (container, &width, NULL);

	if (min_width != NULL)
	{
		*min_width = width;
	}

	if (nat_width != NULL)
	{
		*nat_width = width;
	}
}

static gint
get_row_height (GtkSourceCompletionContainer *container,
		gint                          tree_view_height)
{
	GtkWidget *tree_view;
	GtkTreeModel *model;
	gint nb_rows;

	/* For another possible implementation, see gtkentrycompletion.c in the
	 * GTK+ source code (the _gtk_entry_completion_resize_popup() function).
	 * It uses gtk_tree_view_column_cell_get_size() for retrieving the
	 * height, plus gtk_widget_style_get() to retrieve the
	 * "vertical-separator" height (note that the vertical separator must
	 * probably be counted one less time than the number or rows).
	 * But using that technique is buggy, it returns a smaller height (it's
	 * maybe a bug in GtkTreeView, or there are other missing parameters).
	 *
	 * Note that the following implementation doesn't take into account
	 * "vertical-separator". If there are some sizing bugs, it's maybe the
	 * source of the problem. (note that on my system the separator size was
	 * 0).
	 */

	tree_view = gtk_bin_get_child (GTK_BIN (container));
	model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_view));

	if (model == NULL)
	{
		return 0;
	}

	nb_rows = gtk_tree_model_iter_n_children (model, NULL);

	return tree_view_height / nb_rows;
}

/* Return a height at a row boundary of the GtkTreeView. */
static void
_gtk_source_completion_container_get_preferred_height (GtkWidget *widget,
						       gint	 *min_height,
						       gint	 *nat_height)
{
	GtkSourceCompletionContainer *container = GTK_SOURCE_COMPLETION_CONTAINER (widget);
	GtkWidget *child;
	GtkRequisition nat_size;
	gint total_height;
	gint scrollbar_height = 0;
	gint child_available_width;
	gint ret_height;

	child = gtk_bin_get_child (GTK_BIN (container));
	gtk_widget_get_preferred_size (child, NULL, &nat_size);

	total_height = nat_size.height;

	get_width (container, NULL, &child_available_width);

	/* Needs horizontal scrollbar */
	if (child_available_width < nat_size.width)
	{
		scrollbar_height = get_horizontal_scrollbar_height ();
		total_height += scrollbar_height;
	}

	if (needs_vertical_scrollbar (nat_size.height))
	{
		gint row_height = get_row_height (container, nat_size.height);
		gint nb_rows_allowed = MAX_HEIGHT / row_height;

		ret_height = nb_rows_allowed * row_height + scrollbar_height;
	}
	else
	{
		ret_height = total_height;
	}

	if (min_height != NULL)
	{
		*min_height = ret_height;
	}

	if (nat_height != NULL)
	{
		*nat_height = ret_height;
	}
}

static void
_gtk_source_completion_container_class_init (GtkSourceCompletionContainerClass *klass)
{
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

	widget_class->get_preferred_width = _gtk_source_completion_container_get_preferred_width;
	widget_class->get_preferred_height = _gtk_source_completion_container_get_preferred_height;
}

static void
_gtk_source_completion_container_init (GtkSourceCompletionContainer *container)
{
}

GtkSourceCompletionContainer *
_gtk_source_completion_container_new (void)
{
	return g_object_new (GTK_SOURCE_TYPE_COMPLETION_CONTAINER, NULL);
}