Welcome to mirror list, hosted at ThFree Co, Russian Federation.

textview.c « space_info « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 33333e4c99266e3a5fd1f770ee58867b54a45e52 (plain)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Contributor(s): Campbell Barton
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/editors/space_info/textview.c
 *  \ingroup spinfo
 */


#include <math.h>
#include <string.h>
#include <sys/stat.h>
#include <limits.h>
#include <assert.h>

#include "MEM_guardedalloc.h"

#include "BLF_api.h"

#include "BLI_math.h"
#include "BLI_utildefines.h"
#include "BLI_string_utf8.h"

#include "BIF_gl.h"

#include "BKE_text.h"

#include "textview.h"

static void console_font_begin(TextViewContext *sc)
{
	/* 0.875 is based on: 16 pixels lines get 14 pixel text */
	BLF_size(blf_mono_font, 0.875 * sc->lheight, 72);
}

typedef struct ConsoleDrawContext {
	int cwidth;
	int lheight;
	int lofs; /* text vertical offset */
	int console_width; /* number of characters that fit into the width of the console (fixed width) */
	int winx;
	int ymin, ymax;
	int *xy; // [2]
	int *sel; // [2]
	int *pos_pick; // bottom of view == 0, top of file == combine chars, end of line is lower then start. 
	const int *mval; // [2]
	int draw;
} ConsoleDrawContext;

BLI_INLINE void console_step_sel(ConsoleDrawContext *cdc, const int step)
{
	cdc->sel[0] += step;
	cdc->sel[1] += step;
}

static void console_draw_sel(const char *str, const int sel[2], const int xy[2], const int str_len_draw,
                             int cwidth, int lheight, const unsigned char bg_sel[4])
{
	if (sel[0] <= str_len_draw && sel[1] >= 0) {
		const int sta = txt_utf8_offset_to_column(str, max_ii(sel[0], 0));
		const int end = txt_utf8_offset_to_column(str, min_ii(sel[1], str_len_draw));

		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glColor4ubv(bg_sel);

		glRecti(xy[0] + (cwidth * sta), xy[1] - 2 + lheight, xy[0] + (cwidth * end), xy[1] - 2);

		glDisable(GL_BLEND);
	}
}

/* warning: allocated memory for 'offsets' must be freed by caller */
static int console_wrap_offsets(const char *str, int len, int width, int *lines, int **offsets)
{
	int i, end;  /* column */
	int j;       /* mem */

	*lines = 1;

	*offsets = MEM_callocN(sizeof(**offsets) * (len * BLI_UTF8_WIDTH_MAX / MAX2(1, width - (BLI_UTF8_WIDTH_MAX - 1)) + 1),
	                       "console_wrap_offsets");
	(*offsets)[0] = 0;

	for (i = 0, end = width, j = 0; j < len && str[j]; j += BLI_str_utf8_size_safe(str + j)) {
		int columns = BLI_str_utf8_char_width_safe(str + j);

		if (i + columns > end) {
			(*offsets)[*lines] = j;
			(*lines)++;

			end = i + width;
		}
		i += columns;
	}
	return j; /* return actual length */
}

/* return 0 if the last line is off the screen
 * should be able to use this for any string type */

static int console_draw_string(ConsoleDrawContext *cdc, const char *str, int str_len,
                               const unsigned char fg[3], const unsigned char bg[3], const unsigned char bg_sel[4])
{
	int tot_lines;            /* total number of lines for wrapping */
	int *offsets;             /* offsets of line beginnings for wrapping */
	int y_next;
	const int mono = blf_mono_font;

	str_len = console_wrap_offsets(str, str_len, cdc->console_width, &tot_lines, &offsets);
	y_next = cdc->xy[1] + cdc->lheight * tot_lines;

	/* just advance the height */
	if (cdc->draw == 0) {
		if (cdc->pos_pick && cdc->mval[1] != INT_MAX && cdc->xy[1] <= cdc->mval[1]) {
			if (y_next >= cdc->mval[1]) {
				int ofs = 0;

				/* wrap */
				if (tot_lines > 1) {
					int iofs = (int)((float)(y_next - cdc->mval[1]) / cdc->lheight);
					ofs += offsets[MIN2(iofs, tot_lines - 1)];
				}

				/* last part */
				ofs += txt_utf8_column_to_offset(str + ofs,
				                                 (int)floor((float)cdc->mval[0] / cdc->cwidth));

				CLAMP(ofs, 0, str_len);
				*cdc->pos_pick += str_len - ofs;
			}
			else
				*cdc->pos_pick += str_len + 1;
		}

		cdc->xy[1] = y_next;
		MEM_freeN(offsets);
		return 1;
	}
	else if (y_next - cdc->lheight < cdc->ymin) {
		/* have not reached the drawable area so don't break */
		cdc->xy[1] = y_next;

		/* adjust selection even if not drawing */
		if (cdc->sel[0] != cdc->sel[1]) {
			console_step_sel(cdc, -(str_len + 1));
		}

		MEM_freeN(offsets);
		return 1;
	}

	if (tot_lines > 1) { /* wrap? */
		const int initial_offset = offsets[tot_lines - 1];
		size_t len = str_len - initial_offset;
		const char *s = str + initial_offset;
		int i;
		
		int sel_orig[2];
		copy_v2_v2_int(sel_orig, cdc->sel);

		/* invert and swap for wrapping */
		cdc->sel[0] = str_len - sel_orig[1];
		cdc->sel[1] = str_len - sel_orig[0];
		
		if (bg) {
			glColor3ubv(bg);
			glRecti(0, cdc->xy[1], cdc->winx, (cdc->xy[1] + (cdc->lheight * tot_lines)));
		}

		glColor3ubv(fg);

		/* last part needs no clipping */
		BLF_position(mono, cdc->xy[0], cdc->lofs + cdc->xy[1], 0);
		BLF_draw_mono(mono, s, len, cdc->cwidth);

		if (cdc->sel[0] != cdc->sel[1]) {
			console_step_sel(cdc, -initial_offset);
			// glColor4ub(255, 0, 0, 96); // debug
			console_draw_sel(s, cdc->sel, cdc->xy, len, cdc->cwidth, cdc->lheight, bg_sel);
			glColor3ubv(fg);
		}

		cdc->xy[1] += cdc->lheight;

		for (i = tot_lines - 1; i > 0; i--) {
			len = offsets[i] - offsets[i - 1];
			s = str + offsets[i - 1];

			BLF_position(mono, cdc->xy[0], cdc->lofs + cdc->xy[1], 0);
			BLF_draw_mono(mono, s, len, cdc->cwidth);
			
			if (cdc->sel[0] != cdc->sel[1]) {
				console_step_sel(cdc, len);
				// glColor4ub(0, 255, 0, 96); // debug
				console_draw_sel(s, cdc->sel, cdc->xy, len, cdc->cwidth, cdc->lheight, bg_sel);
				glColor3ubv(fg);
			}

			cdc->xy[1] += cdc->lheight;
			
			/* check if were out of view bounds */
			if (cdc->xy[1] > cdc->ymax) {
				MEM_freeN(offsets);
				return 0;
			}
		}

		copy_v2_v2_int(cdc->sel, sel_orig);
		console_step_sel(cdc, -(str_len + 1));
	}
	else { /* simple, no wrap */

		if (bg) {
			glColor3ubv(bg);
			glRecti(0, cdc->xy[1], cdc->winx, cdc->xy[1] + cdc->lheight);
		}

		glColor3ubv(fg);

		BLF_position(mono, cdc->xy[0], cdc->lofs + cdc->xy[1], 0);
		BLF_draw_mono(mono, str, str_len, cdc->cwidth);
		
		if (cdc->sel[0] != cdc->sel[1]) {
			int isel[2];

			isel[0] = str_len - cdc->sel[1];
			isel[1] = str_len - cdc->sel[0];

			// glColor4ub(255, 255, 0, 96); // debug
			console_draw_sel(str, isel, cdc->xy, str_len, cdc->cwidth, cdc->lheight, bg_sel);
			console_step_sel(cdc, -(str_len + 1));
		}

		cdc->xy[1] += cdc->lheight;

		if (cdc->xy[1] > cdc->ymax) {
			MEM_freeN(offsets);
			return 0;
		}
	}

	MEM_freeN(offsets);
	return 1;
}

#define CONSOLE_DRAW_MARGIN 4

int textview_draw(TextViewContext *tvc, const int draw, int mval[2], void **mouse_pick, int *pos_pick)
{
	ConsoleDrawContext cdc = {0};

	int x_orig = CONSOLE_DRAW_MARGIN, y_orig = CONSOLE_DRAW_MARGIN + tvc->lheight / 6;
	int xy[2], y_prev;
	int sel[2] = {-1, -1}; /* defaults disabled */
	unsigned char fg[3], bg[3];
	const int mono = blf_mono_font;

	console_font_begin(tvc);

	xy[0] = x_orig; xy[1] = y_orig;

	if (mval[1] != INT_MAX)
		mval[1] += (tvc->ymin + CONSOLE_DRAW_MARGIN);

	if (pos_pick)
		*pos_pick = 0;

	/* constants for the sequencer context */
	cdc.cwidth = (int)BLF_fixed_width(mono);
	assert(cdc.cwidth > 0);
	cdc.lheight = tvc->lheight;
	cdc.lofs = -BLF_descender(mono);
	/* note, scroll bar must be already subtracted () */
	cdc.console_width = (tvc->winx - (CONSOLE_DRAW_MARGIN * 2)) / cdc.cwidth;
	/* avoid divide by zero on small windows */
	if (cdc.console_width < 1)
		cdc.console_width = 1;
	cdc.winx = tvc->winx - CONSOLE_DRAW_MARGIN;
	cdc.ymin = tvc->ymin;
	cdc.ymax = tvc->ymax;
	cdc.xy = xy;
	cdc.sel = sel;
	cdc.pos_pick = pos_pick;
	cdc.mval = mval;
	cdc.draw = draw;

	/* shouldnt be needed */
	tvc->cwidth = cdc.cwidth;
	tvc->console_width = cdc.console_width;
	tvc->iter_index = 0;

	if (tvc->sel_start != tvc->sel_end) {
		sel[0] = tvc->sel_start;
		sel[1] = tvc->sel_end;
	}

	if (tvc->begin(tvc)) {
		unsigned char bg_sel[4] = {0};

		if (draw && tvc->const_colors) {
			tvc->const_colors(tvc, bg_sel);
		}

		do {
			const char *ext_line;
			int ext_len;
			int color_flag = 0;

			y_prev = xy[1];

			if (draw)
				color_flag = tvc->line_color(tvc, fg, bg);

			tvc->line_get(tvc, &ext_line, &ext_len);

			if (!console_draw_string(&cdc, ext_line, ext_len,
			                         (color_flag & TVC_LINE_FG) ? fg : NULL,
			                         (color_flag & TVC_LINE_BG) ? bg : NULL,
			                         bg_sel))
			{
				/* when drawing, if we pass v2d->cur.ymax, then quit */
				if (draw) {
					break; /* past the y limits */
				}
			}

			if ((mval[1] != INT_MAX) && (mval[1] >= y_prev && mval[1] <= xy[1])) {
				*mouse_pick = (void *)tvc->iter;
				break;
			}

			tvc->iter_index++;

		} while (tvc->step(tvc));
	}

	tvc->end(tvc);

	xy[1] += tvc->lheight * 2;

	return xy[1] - y_orig;
}