-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauto-scroll-width.c
175 lines (142 loc) · 4.21 KB
/
auto-scroll-width.c
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
/*
* Copyright 2016 Colomban Wendling <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* TODO:
* * optimize! (how?)
* * add a setting whether to hide the bottom scrollbar automatically
* * fix width on startup?
*/
#define AUTOHIDE 1
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <glib.h>
#include <geanyplugin.h>
#define SSM(s, m, w, l) scintilla_send_message (s, m, (uptr_t)(w), (sptr_t)(l))
#if AUTOHIDE
static void
get_text_child (GtkWidget *widget,
gpointer data)
{
if (! GTK_IS_SCROLLBAR (widget)) {
*((GtkWidget **) data) = widget;
}
}
static GtkWidget *
get_text_widget (ScintillaObject *sci)
{
GtkWidget *child = NULL;
gtk_container_forall (GTK_CONTAINER (sci), get_text_child, &child);
return child ? child : (GtkWidget *) sci;
}
static gint
get_margins_width (ScintillaObject *sci)
{
gint width;
gint i;
width = (gint) SSM (sci, SCI_GETMARGINLEFT, 0, 0);
width += (gint) SSM (sci, SCI_GETMARGINRIGHT, 0, 0);
for (i = 0; i < SC_MAX_MARGIN; i++) {
width += (gint) SSM (sci, SCI_GETMARGINWIDTHN, i, 0);
}
return width;
}
#endif /* AUTOHIDE */
static gint
get_longest_line_width (ScintillaObject *sci)
{
const gint lines = sci_get_line_count (sci);
gint x = 0;
gint line;
for (line = 0; line < lines; line++) {
gint end = sci_get_line_end_position (sci, line);
gint line_x = (gint) SSM (sci, SCI_POINTXFROMPOSITION, 0, end);
if (line_x > x) {
x = line_x;
}
}
return x - (gint) SSM (sci, SCI_POINTXFROMPOSITION, 0, 0);
}
static void
update_hscrollbar (ScintillaObject *sci)
{
gint width = get_longest_line_width (sci);
SSM (sci, SCI_SETSCROLLWIDTH, MAX (1, width), 0);
}
#if AUTOHIDE
static void
update_hscrollbar_visibility (ScintillaObject *sci)
{
GtkWidget *widget = get_text_widget (sci);
gint alloc_w = gtk_widget_get_allocated_width (widget);
gint margin = get_margins_width (sci);
gint width = (gint) SSM (sci, SCI_GETSCROLLWIDTH, 0, 0);
if (margin + width > alloc_w) {
SSM (sci, SCI_SETHSCROLLBAR, 1, 0);
} else if (SSM (sci, SCI_GETHSCROLLBAR, 0, 0)) {
SSM (sci, SCI_SETHSCROLLBAR, 0, 0);
SSM (sci, SCI_SETXOFFSET, 0, 0);
}
}
#endif /* AUTOHIDE */
static gboolean
on_editor_notify (GObject *obj,
GeanyEditor *editor,
SCNotification *nt,
gpointer user_data)
{
if (nt->nmhdr.code == SCN_UPDATEUI) {
update_hscrollbar (editor->sci);
}
#if AUTOHIDE
else if (nt->nmhdr.code == SCN_PAINTED) {
/* also update scrollbar visibility when editor size changes */
update_hscrollbar_visibility (editor->sci);
}
#endif
return FALSE;
}
static gboolean
asw_init (GeanyPlugin *plugin,
gpointer data)
{
GeanyDocument *doc = document_get_current ();
plugin_signal_connect (plugin, NULL, "editor-notify", FALSE,
G_CALLBACK (on_editor_notify), plugin);
/* in case we're loaded during the session, update the current document */
if (doc) {
update_hscrollbar (doc->editor->sci);
}
return TRUE;
}
static void
asw_cleanup (GeanyPlugin *plugin,
gpointer data)
{
}
G_MODULE_EXPORT void
geany_load_module (GeanyPlugin *plugin)
{
main_locale_init (LOCALEDIR, GETTEXT_PACKAGE);
plugin->info->name = _("Automatic Scroll Width");
plugin->info->description = _("Automatically updates the bottom scrollbar "
"width to account for actual contents");
plugin->info->version = "0.0";
plugin->info->author = "Colomban Wendling <[email protected]>";
plugin->funcs->init = asw_init;
plugin->funcs->cleanup = asw_cleanup;
GEANY_PLUGIN_REGISTER (plugin, 225);
}