📄 editor.c
字号:
if ( l > longes ) { /* it's longer that longest for now */
longes = l;
};
nlines++; /* inc max number of lines */
} while ( t++ );
if ( longest ) (*longest) = longes;
return lmax(0, nlines-1);
};
/*
find pointer to line from editor->text string
- return pointer to line "line"
, lines are spaced by dos-enter char = '10' ascii code
*/
l_text editor_lnptr ( p_editor o, l_long line )
{
l_text from = o->text;
l_long i = 0;
if ( o->line_ptr && o->line_from <= line ) {
i = o->line_from;
from = o->line_ptr;
};
if ( from && i < line )
do {
from = strchr(from, EDITOR_CHAR_ENTER); /* find end of line = '10' ascii code */
if ( from ) from++;
} while ( from && ++i < line );
return from;
};
/*
return pointer to text, that start in line (line) and delta position
from this line (pos). It something like (y, x) position in text.
*/
l_text editor_posptr ( p_editor o, l_long line, l_long pos )
{
l_text p = o->lnptr(o, line); /* get pointer of line (line) */
if ( p ) { /* exists this line */
l_long len = o->lnlenptr(o, p); /* get length of this line */
return &(o->text[(p-o->text)+lmin(pos, len)]);
};
return NULL;
};
/*
- return number of chars (bytes) that are from pointer (from),
to delta line from this pointer (deltaln) and delta pos in this line
(deltapos). Something like (y, x) from pointer from.
*/
l_long editor_sizeto ( l_text from, l_long deltaln, l_long deltapos )
{
if ( from ) {
l_long size = 0;
if ( *from )
while ( *from && deltaln > 0 ) {
if ( (*from) == EDITOR_CHAR_ENTER ) deltaln--;
else size++;
from++;
};
if ( !deltaln && *from && deltapos )
while ( *from && deltapos > 0 ) {
from++;
size++;
deltapos--;
};
return size;
};
return 0;
};
/*
- return length of line line.
*/
l_long editor_lnlen ( p_editor o, l_long line )
{
l_long l = 0;
l_text p = o->lnptr(o, line);
if ( p )
for (;*p && *p != EDITOR_CHAR_ENTER; p++, l++)
;
return l;
};
/*
- return length of line from ptr.
*/
l_long editor_lnlenptr ( p_editor o, l_text ptr )
{
l_long l = 0;
l_text p = ptr;
if ( p )
for (;*p && *p != EDITOR_CHAR_ENTER; p++, l++)
;
return l;
};
/*
- return size of line (line) in pixels to max chars (max).
*/
l_long editor_xsize_of_line ( p_editor o, l_long line, l_long max )
{
if ( line ) {
l_long lnlen = 0;
l_text text = o->lntxtlen(o, line, &lnlen);
l_long fw = 0;
if ( lnlen > 0 ) {
l_long size = lnlen;
if ( max < 0 ) max = size;
fw = FONT_GETSTRWIDTH(VIEW(o)->font, (l_byte*)text, lmin(max, size)); /* width of line */
} else fw = FONT_GETSTRWIDTH(VIEW(o)->font, (l_byte*)text, lmin(lnlen, (max<0?lnlen:max))); /* width of line */
return fw;
};
return 0;
};
/*
- return size of lines (from line (line)) in pixels.
line - pos of line
h - number of lines we want to get their height
p - pointer to integer where we want to get info about y
position from line (line) to (delta)+(line).
*/
l_long editor_ysize_of_line ( p_editor o, l_long line, l_long h, l_long (*p) )
{
l_long fh = 0;
if ( p ) (*p) = 0;
if ( line < 0 || line > o->line_num )
return 0;
fh = FONT_GETSTRHEIGHT(VIEW(o)->font)+EDITOR_LINE_SIZE; /* height of line */
if ( p ) (*p) = (line*fh);
return fh * h;
};
t_rect editor_size_limits ( p_view o )
{
return rect_assign(2, 2, rect_sizex(o->bounds)-2, rect_sizey(o->bounds)-2);
};
void editor_draw ( p_view o )
{
t_rect r = o->get_local_extent(o);
t_point p;
BITMAP *out = o->begin_paint(o, &p, r);
l_long dy = EDITOR(o)->ysize_of_line(EDITOR(o), 0, 1, NULL);
SCROLLER(o)->deltax = FONT_GETWIDTH(VIEW(o)->font, 'w');
SCROLLER(o)->deltay = dy;
if ( out ) {
button3d(o,out,p.x+r.a.x, p.y+r.a.y, p.x+r.b.x, p.y+r.b.y,1);
EDITOR(o)->draw_box(EDITOR(o));
};
o->end_of_paint(o, r);
};
/*
- redraw line (line) in editor
*/
void editor_draw_line ( p_editor o, l_long line )
{
p_view v = VIEW(o);
p_scroller sc = SCROLLER(o);
t_rect r = o->line_rect(o, line);
t_rect t;
t_point p;
BITMAP *out = v->begin_paint(v, &p, r);
if ( out ) {
l_color fcol = color_flat_text;
l_color fselcol = color_selected_text;
l_color bselcol = color_selected_face;
l_rect sx = -1;
l_rect ex = -1;
l_long len;
l_text text = o->lntxtlen(o, line, &len); /* get text from line "line"
and pos "pos" + len will
contains length of this
line */
l_rect dx = FONT_GETSTRWIDTH(v->font, text, len);;
t = rect_move(r, p.x, p.y);
v->background(v, out, t);
if ( text && line >= o->sel_from.a.y && line <= o->sel_from.b.y ) {
l_int l = len;
sx = 0;
if ( line == o->sel_from.a.y ) sx = lmax(0, lmin(l, o->sel_from.a.x));
if ( line == o->sel_from.b.y ) ex = lmax(0, lmin(l, o->sel_from.b.x));
};
if ( len > 0 )
draw_selected_text(out, v->font, text, len, sx, ex,
r.a.x+p.x-sc->scrollx, r.a.y+p.y, r.a.x+p.x+dx-sc->scrollx, r.b.y+p.y, TX_ALIGN_DEFAULT,
fcol, TX_NOCOLOR, fselcol, bselcol, 0);
else if ( text && *text ) {
l_char c[2] = {' ', '\0'};
draw_selected_text(out, v->font, (l_text)c, 1, sx, ex,
r.a.x+p.x-sc->scrollx, r.a.y+p.y, r.a.x+p.x+dx-sc->scrollx, r.b.y+p.y, TX_ALIGN_DEFAULT,
fcol, TX_NOCOLOR, fselcol, bselcol, 0);
};
};
v->end_of_paint(v, r);
};
/*
- draw box of editor, redraw background and all lines from (linefrom)
in visible box
*/
void editor_draw_box_ex ( p_editor o, l_long linefrom )
{
p_view v = VIEW(o);
t_rect r = v->size_limits(v);
t_rect x = o->line_rect(o, linefrom);
t_point p;
BITMAP *out;
r = rect_assign(r.a.x, lmax(r.a.y, x.a.y), r.b.x, r.b.y);
out = v->begin_paint(v, &p, r);
if ( out ) {
l_long line = linefrom;
l_int pgsz = o->lines_inpage(o);
v->background(v, out, rect_move(r, p.x, p.y));
while ( line <= o->line_num && line <= o->line_from + pgsz ) { /* draw all lines */
if ( line >= o->line_from ) {
x = o->line_rect(o, line);
o->draw_line(o, line);
};
line++;
};
};
v->end_of_paint(v, r);
EDITOR(o)->draw_cursor(EDITOR(o), -1, -1);
};
void editor_draw_box ( p_editor o )
{
o->draw_box_ex(o, o->line_from);
};
void editor_timer ( p_object o ) {
if ( !ed_is_wable(EDITOR(o)) )
return;
if ( o->is_state(o, OB_SF_FOCUSED) ) {
if ( EDITOR(o)->cursor_visible ) {
EDITOR(o)->cursor_visible = 0;
EDITOR(o)->draw_cursor_ex ( EDITOR(o), EDITOR(o)->line, EDITOR(o)->line_pos, 0);
} else {
EDITOR(o)->cursor_visible = 1;
EDITOR(o)->draw_cursor_ex ( EDITOR(o), EDITOR(o)->line, EDITOR(o)->line_pos, 1);
};
} else {
if ( EDITOR(o)->cursor_visible ) {
EDITOR(o)->cursor_visible = 0;
EDITOR(o)->draw_cursor_ex ( EDITOR(o), EDITOR(o)->line, EDITOR(o)->line_pos, 0);
};
};
};
/*
draw cursor to line (line) and pos (pos)
if active is non-zero it show cursor, otherwise it redraw background
*/
void editor_draw_cursor_ex ( p_editor o, l_long line, l_long pos, l_int active )
{
p_view vo = VIEW(o);
t_rect r = o->line_rect(o, line);
t_rect s = vo->size_limits(vo);
t_rect safe;
l_long len;
l_text text = o->lntxtlen(o, line, &len); /* get text from line "line"
and pos "pos" + len will
contains length of this
line */
l_rect rcpos = FONT_GETSTRWIDTH(vo->font, text, lmax(0, lmin(len, pos)));
//l_rect ecpos = FONT_GETSTRWIDTH(vo->font, text, lmax(0, lmin(len, pos+1)));
//l_color fcol = active?COLOR(CO_WHITE):color_flat_text;
//l_color bcolor = COLOR(CO_BLACK);
//l_color fselcol = color_selected_text;
//l_color bselcol = color_selected_face;
t_point p;
BITMAP *out;
//l_rect sx = -1;
//l_rect ex = -1;
l_rect dx = FONT_GETSTRWIDTH(vo->font, text, len);
//if ( ecpos == rcpos ) ecpos = rcpos+1+FONT_GETWIDTH(vo->font, ' ');
r = rect_assign(r.a.x+rcpos, r.a.y, r.a.x+rcpos, r.b.y);
r = rect_cliped(rect_move(r, -SCROLLER(o)->scrollx, 0), s);
safe = r;
out = vo->begin_paint(vo, &p, safe);
if ( out ) {
r = rect_move(r, p.x, p.y);
if ( active ) {
rectfill(out, r.a.x, r.a.y, r.b.x, r.b.y, color_flat_text);
/*textout_draw_rect(out, vo->font, &(text[pos]), 1,
r.a.x, r.a.y, r.a.x, r.b.y+dx, TX_ALIGN_DEFAULT,
fcol, TX_NOCOLOR, 0);*/
} else {
o->draw_line ( o, line );
};
};
vo->end_of_paint(vo, safe);
};
l_long editor_charsin ( p_editor o, l_long line, l_long pos, l_int plus )
{
return o->charsin_size(o, rect_sizex(VIEW(o)->bounds)-(4+FONT_GETWIDTH(VIEW(o)->font, ' ')), line, pos, plus);
};
l_long editor_charsin_size ( p_editor o, l_rect size, l_long line, l_long pos, l_int plus )
{
l_rect sizex = size;
l_long chars = 0;
l_long tsize;
l_text text = o->lntxtlen(o, line, &tsize); /* get text from line "line" */
if ( text ) {
pos += plus;
while ( sizex > 0 && pos >= 0 && pos <= tsize ) {
sizex -= FONT_GETWIDTH(VIEW(o)->font, text[pos]);
pos += plus;
chars++;
};
if ( sizex < 0 ) chars--;
return lmax(0, chars);
};
return 0;
};
l_long editor_linesin ( p_editor o, l_long line, l_int plus )
{
l_long dy = o->ysize_of_line(o, 0, 1, NULL);
return o->linesin_size(o, rect_sizey(VIEW(o)->bounds)-(dy+4), line, plus);
};
l_long editor_linesin_size ( p_editor o, l_rect size, l_long line, l_int plus )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -