Пример #1
0
/* Moves the view to the cursor location,
 * also used to make sure the view isn't outside the file */
void text_scroll_to_cursor(SpaceText *st, ARegion *ar, const bool center)
{
	Text *text;
	int i, x, winx = ar->winx;

	if (ELEM(NULL, st, st->text, st->text->curl)) return;

	text = st->text;

	text_update_character_width(st);

	i = txt_get_span(text->lines.first, text->sell);
	if (st->wordwrap) {
		int offl, offc;
		wrap_offset(st, ar, text->sell, text->selc, &offl, &offc);
		i += offl;
	}

	if (center) {
		if (st->top + st->viewlines <= i || st->top > i) {
			st->top = i - st->viewlines / 2;
		}
	}
	else {
		if (st->top + st->viewlines <= i) {
			st->top = i - (st->viewlines - 1);
		}
		else if (st->top > i) {
			st->top = i;
		}
	}
	
	if (st->wordwrap) {
		st->left = 0;
	}
	else {
		x = st->cwidth * (text_get_char_pos(st, text->sell->line, text->selc) - st->left);
		winx -= TXT_OFFSET + (st->showlinenrs ? TEXTXLOC : 0) + TXT_SCROLL_WIDTH;

		if (center) {
			if (x <= 0 || x > winx) {
				st->left += (x - winx / 2) / st->cwidth;
			}
		}
		else {
			if (x <= 0) {
				st->left += ((x + 1) / st->cwidth) - 1;
			}
			else if (x > winx) {
				st->left += ((x - (winx + 1)) / st->cwidth) + 1;
			}
		}
	}

	if (st->top < 0) st->top = 0;
	if (st->left < 0) st->left = 0;

	st->scroll_accum[0] = 0.0f;
	st->scroll_accum[1] = 0.0f;
}
Пример #2
0
int text_do_suggest_select(SpaceText *st, ARegion *ar)
{
	SuggItem *item, *first, *last /* , *sel */ /* UNUSED */;
	TextLine *tmp;
	int l, x, y, w, h, i;
	int tgti, *top;
	int mval[2] = {0, 0};

	if (!st || !st->text) return 0;
	if (!texttool_text_is_active(st->text)) return 0;

	first = texttool_suggest_first();
	last = texttool_suggest_last();
	/* sel = texttool_suggest_selected(); */ /* UNUSED */
	top = texttool_suggest_top();

	if (!last || !first)
		return 0;

	/* Count the visible lines to the cursor */
	for (tmp = st->text->curl, l = -st->top; tmp; tmp = tmp->prev, l++) ;
	if (l < 0) return 0;

	text_update_character_width(st);

	if (st->showlinenrs) {
		x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET + TEXTXLOC - 4;
	}
	else {
		x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET - 4;
	}
	y = ar->winy - st->lheight_dpi * l - 2;

	w = SUGG_LIST_WIDTH * st->cwidth + U.widget_unit;
	h = SUGG_LIST_SIZE * st->lheight_dpi + 0.4f * U.widget_unit;

	// XXX getmouseco_areawin(mval);

	if (mval[0] < x || x + w < mval[0] || mval[1] < y - h || y < mval[1])
		return 0;

	/* Work out which of the items is at the top of the visible list */
	for (i = 0, item = first; i < *top && item->next; i++, item = item->next) ;

	/* Work out the target item index in the visible list */
	tgti = (y - mval[1] - 4) / st->lheight_dpi;
	if (tgti < 0 || tgti > SUGG_LIST_SIZE)
		return 1;

	for (i = tgti; i > 0 && item->next; i--, item = item->next) ;
	if (item)
		texttool_suggest_select(item);
	return 1;
}
Пример #3
0
/* Moves the view to the cursor location,
  also used to make sure the view isnt outside the file */
void text_update_cursor_moved(bContext *C)
{
	ScrArea *sa= CTX_wm_area(C);
	SpaceText *st= CTX_wm_space_text(C);
	Text *text;
	ARegion *ar;
	int i, x, winx= 0;

	if(ELEM3(NULL, st, st->text, st->text->curl)) return;

	text= st->text;

	for(ar=sa->regionbase.first; ar; ar= ar->next)
		if(ar->regiontype==RGN_TYPE_WINDOW)
			winx= ar->winx;
	
	winx -= TXT_SCROLL_WIDTH;

	text_update_character_width(st);

	i= txt_get_span(text->lines.first, text->sell);
	if(st->wordwrap) {
		int offl, offc;
		wrap_offset(st, CTX_wm_region(C), text->sell, text->selc, &offl, &offc);
		i+= offl;
	}

	if(st->top+st->viewlines <= i || st->top > i)
		st->top= i - st->viewlines/2;
	
	if(st->wordwrap) {
		st->left= 0;
	}
	else {
		x= text_draw(st, text->sell->line, st->left, text->selc, 0, 0, 0, NULL);

		if(x==0 || x>winx)
			st->left= text->curc-0.5*winx/st->cwidth;
	}

	if(st->top < 0) st->top= 0;
	if(st->left <0) st->left= 0;
}
Пример #4
0
/* Moves the view to the cursor location,
 * also used to make sure the view isn't outside the file */
void text_scroll_to_cursor(SpaceText *st, ScrArea *sa)
{
	Text *text;
	ARegion *ar = NULL;
	int i, x, winx = 0;

	if (ELEM3(NULL, st, st->text, st->text->curl)) return;

	text = st->text;

	for (ar = sa->regionbase.first; ar; ar = ar->next)
		if (ar->regiontype == RGN_TYPE_WINDOW) {
			winx = ar->winx;
			break;
		}

	text_update_character_width(st);

	i = txt_get_span(text->lines.first, text->sell);
	if (st->wordwrap) {
		int offl, offc;
		wrap_offset(st, ar, text->sell, text->selc, &offl, &offc);
		i += offl;
	}

	if (st->top + st->viewlines <= i || st->top > i)
		st->top = i - st->viewlines / 2;
	
	if (st->wordwrap) {
		st->left = 0;
	}
	else {
		x = st->cwidth * (text_get_char_pos(st, text->sell->line, text->selc) - st->left);
		winx -= TXT_OFFSET + (st->showlinenrs ? TEXTXLOC : 0) + TXT_SCROLL_WIDTH;

		if (x <= 0 || x > winx)
			st->left += (x - winx / 2) / st->cwidth;
	}

	if (st->top < 0) st->top = 0;
	if (st->left < 0) st->left = 0;
}
Пример #5
0
static void text_update_drawcache(SpaceText *st, ARegion *ar)
{
	DrawCache *drawcache;
	int full_update = 0, nlines = 0;
	Text *txt = st->text;

	if (!st->drawcache) text_drawcache_init(st);

	text_update_character_width(st);

	drawcache = (DrawCache *)st->drawcache;
	nlines = drawcache->nlines;

	/* check if full cache update is needed */
	full_update |= drawcache->winx != ar->winx;               /* area was resized */
	full_update |= drawcache->wordwrap != st->wordwrap;       /* word-wrapping option was toggled */
	full_update |= drawcache->showlinenrs != st->showlinenrs; /* word-wrapping option was toggled */
	full_update |= drawcache->tabnumber != st->tabnumber;     /* word-wrapping option was toggled */
	full_update |= drawcache->lheight != st->lheight_dpi;         /* word-wrapping option was toggled */
	full_update |= drawcache->cwidth != st->cwidth;           /* word-wrapping option was toggled */
	full_update |= strncmp(drawcache->text_id, txt->id.name, MAX_ID_NAME); /* text datablock was changed */

	if (st->wordwrap) {
		/* update line heights */
		if (full_update || !drawcache->line_height) {
			drawcache->valid_head  = 0;
			drawcache->valid_tail  = 0;
			drawcache->update_flag = 1;
		}

		if (drawcache->update_flag) {
			TextLine *line = st->text->lines.first;
			int lineno = 0, size, lines_count;
			int *fp = drawcache->line_height, *new_tail, *old_tail;

			nlines = BLI_countlist(&txt->lines);
			size = sizeof(int) * nlines;

			if (fp) fp = MEM_reallocN(fp, size);
			else fp = MEM_callocN(size, "text drawcache line_height");

			drawcache->valid_tail = drawcache->valid_head = 0;
			old_tail = fp + drawcache->nlines - drawcache->valid_tail;
			new_tail = fp + nlines - drawcache->valid_tail;
			memmove(new_tail, old_tail, drawcache->valid_tail);

			drawcache->total_lines = 0;

			if (st->showlinenrs)
				st->linenrs_tot = (int)floor(log10((float)nlines)) + 1;

			while (line) {
				if (drawcache->valid_head) { /* we're inside valid head lines */
					lines_count = fp[lineno];
					drawcache->valid_head--;
				}
				else if (lineno > new_tail - fp) {  /* we-re inside valid tail lines */
					lines_count = fp[lineno];
				}
				else {
					lines_count = text_get_visible_lines(st, ar, line->line);
				}

				fp[lineno] = lines_count;

				line = line->next;
				lineno++;
				drawcache->total_lines += lines_count;
			}

			drawcache->line_height = fp;
		}
	}
	else {
		if (drawcache->line_height) {
			MEM_freeN(drawcache->line_height);
			drawcache->line_height = NULL;
		}

		if (full_update || drawcache->update_flag) {
			nlines = BLI_countlist(&txt->lines);

			if (st->showlinenrs)
				st->linenrs_tot = (int)floor(log10((float)nlines)) + 1;
		}

		drawcache->total_lines = nlines;
	}

	drawcache->nlines = nlines;

	/* store settings */
	drawcache->winx        = ar->winx;
	drawcache->wordwrap    = st->wordwrap;
	drawcache->lheight     = st->lheight_dpi;
	drawcache->cwidth      = st->cwidth;
	drawcache->showlinenrs = st->showlinenrs;
	drawcache->tabnumber   = st->tabnumber;

	strncpy(drawcache->text_id, txt->id.name, MAX_ID_NAME);

	/* clear update flag */
	drawcache->update_flag = 0;
	drawcache->valid_head  = 0;
	drawcache->valid_tail  = 0;
}