... | ... |
@@ -19,6 +19,8 @@ |
19 | 19 |
|
20 | 20 |
#define DEFAULTFONTSIZE 16 |
21 | 21 |
#define DEFAULTTITLEPREFIX "re - " |
22 |
+#define DEFAULTWINDOWWIDTH 80 |
|
23 |
+#define DEFAULTWINDOWHEIGHT 66 |
|
22 | 24 |
|
23 | 25 |
#define RECTFILL(r,rx,ry,rw,rh) (r).x=(rx),(r).y=(ry),(r).w=(rw),(r).h=(rh) |
24 | 26 |
|
... | ... |
@@ -32,20 +34,12 @@ reui_init() |
32 | 34 |
memset(ui,0,sizeof(reui_t)); |
33 | 35 |
/* video */ |
34 | 36 |
if(SDL_Init(SDL_INIT_VIDEO)<0 |
35 |
- || SDL_GetDisplayBounds(0,&screenrect)!=0 |
|
36 |
- || (ui->win=SDL_CreateWindow( |
|
37 |
- DEFAULTTITLEPREFIX, |
|
38 |
- SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED, |
|
39 |
- screenrect.w,screenrect.h,0))==NULL |
|
40 |
- || (ui->renderer=SDL_CreateRenderer(ui->win,-1, |
|
41 |
- SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC))==NULL |
|
42 |
- || (ui->scr=SDL_CreateTexture(ui->renderer,SDL_PIXELFORMAT_ARGB8888, |
|
43 |
- SDL_TEXTUREACCESS_STREAMING,screenrect.w,screenrect.h))==NULL) { |
|
37 |
+ || SDL_GetDisplayBounds(0,&screenrect)!=0) { |
|
44 | 38 |
reui_free(ui),ui=NULL; |
45 | 39 |
return(NULL); |
46 | 40 |
} |
47 |
- ui->w=screenrect.w; |
|
48 |
- ui->h=screenrect.h; |
|
41 |
+ ui->screenw=screenrect.w; |
|
42 |
+ ui->screenh=screenrect.h; |
|
49 | 43 |
/* font */ |
50 | 44 |
ui->fontheight=DEFAULTFONTSIZE; |
51 | 45 |
ui->fontdata=SDL_RWFromConstMem(hack_regular,SIZE_HACK_REGULAR); |
... | ... |
@@ -67,6 +61,22 @@ reui_init() |
67 | 61 |
ui->fontwidth=s->w; |
68 | 62 |
SDL_FreeSurface(s),s=NULL; |
69 | 63 |
} |
64 |
+ /* main window */ |
|
65 |
+ ui->w=ui->fontwidth*DEFAULTWINDOWWIDTH; |
|
66 |
+ ui->w=(ui->w>ui->screenw)?ui->screenw:ui->w; |
|
67 |
+ ui->h=ui->fontheight*DEFAULTWINDOWHEIGHT; |
|
68 |
+ ui->h=(ui->h>ui->screenh)?ui->screenw:ui->h; |
|
69 |
+ if((ui->win=SDL_CreateWindow( |
|
70 |
+ DEFAULTTITLEPREFIX, |
|
71 |
+ SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED, |
|
72 |
+ ui->w,ui->h,0))==NULL |
|
73 |
+ || (ui->renderer=SDL_CreateRenderer(ui->win,-1, |
|
74 |
+ SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC))==NULL |
|
75 |
+ || (ui->scr=SDL_CreateTexture(ui->renderer,SDL_PIXELFORMAT_ARGB8888, |
|
76 |
+ SDL_TEXTUREACCESS_STREAMING,ui->w,ui->h))==NULL) { |
|
77 |
+ reui_free(ui),ui=NULL; |
|
78 |
+ return(NULL); |
|
79 |
+ } |
|
70 | 80 |
/* finished */ |
71 | 81 |
ui->scrdirty=1; |
72 | 82 |
ui->rendererdirty=1; |
... | ... |
@@ -18,6 +18,9 @@ |
18 | 18 |
#include "re_ui.h" |
19 | 19 |
#include "ext/socklib.h" |
20 | 20 |
|
21 |
+#define LINEFORCESCROLL 1 |
|
22 |
+#define COLFORCESCROLL 5 |
|
23 |
+ |
|
21 | 24 |
typedef struct re_t { |
22 | 25 |
redata_t *data; |
23 | 26 |
reui_t *ui; |
... | ... |
@@ -25,7 +28,8 @@ typedef struct re_t { |
25 | 28 |
char filename[PATH_MAX]; |
26 | 29 |
int x, y, w, h; // contents rect |
27 | 30 |
long cursorpos; |
28 |
- int lastcol,lastrow; |
|
31 |
+ int originline,origincol; |
|
32 |
+ int curline,curcol; |
|
29 | 33 |
int maxrow,maxcol; |
30 | 34 |
int contentsdirty; |
31 | 35 |
} re_t; |
... | ... |
@@ -209,8 +213,7 @@ re_processkey(re_t *re, SDL_Event *event) |
209 | 213 |
char *ptr,*ptr2; |
210 | 214 |
int len; |
211 | 215 |
int has_nl; |
212 |
- int oldcol,maxcol; |
|
213 |
- int linecols; |
|
216 |
+ int maxcol; |
|
214 | 217 |
int inc; |
215 | 218 |
if(re==NULL || event==NULL || event->type!=SDL_KEYDOWN) |
216 | 219 |
return(-1); /* sanity check failed */ |
... | ... |
@@ -219,23 +222,28 @@ re_processkey(re_t *re, SDL_Event *event) |
219 | 222 |
return(-1); /* couldn't get current line data */ |
220 | 223 |
if(event->key.keysym.sym==SDLK_UP && newpos==0) |
221 | 224 |
return(-1); /* going up but already at top */ |
225 |
+#if 0 |
|
222 | 226 |
maxcol=redata_generic_utf8len(ptr,len); |
223 |
- if(maxcol<re->lastcol) |
|
227 |
+ if(maxcol<re->curcol) |
|
224 | 228 |
re->cursorpos=newpos+len; |
225 | 229 |
else |
226 |
- re->cursorpos=newpos+(redata_generic_utf8col(ptr,len,re->lastcol)-ptr); |
|
230 |
+ re->cursorpos=newpos+(redata_generic_utf8col(ptr,len,re->curcol)-ptr); |
|
231 |
+#endif |
|
227 | 232 |
if(redata_line_info(re->data,(event->key.keysym.sym==SDLK_DOWN)?(newpos+len):(newpos-1),&newpos2,&ptr,&len)==-1) |
228 | 233 |
return(-1); /* couldn't get next line data */ |
229 | 234 |
has_nl=((len>0 && ptr[len-1]=='\n')?1:0); |
230 | 235 |
maxcol=redata_generic_utf8len(ptr,len)-has_nl; |
231 |
- if(maxcol<re->lastcol) |
|
236 |
+ if(maxcol<re->curcol) |
|
232 | 237 |
re->cursorpos=newpos2+len-has_nl; |
233 | 238 |
else |
234 |
- re->cursorpos=newpos2+(redata_generic_utf8col(ptr,len,re->lastcol)-ptr); |
|
235 |
- if(event->key.keysym.sym==SDLK_DOWN && re->lastrow<re->maxrow) |
|
236 |
- re->lastrow++; |
|
237 |
- else if(event->key.keysym.sym==SDLK_UP && re->lastrow>0) |
|
238 |
- re->lastrow--; |
|
239 |
+ re->cursorpos=newpos2+(redata_generic_utf8col(ptr,len,re->curcol)-ptr); |
|
240 |
+ re->curline+=(event->key.keysym.sym==SDLK_DOWN)?1:-1; |
|
241 |
+ if(re->curline<(re->originline+LINEFORCESCROLL)) { |
|
242 |
+ re->originline-=LINEFORCESCROLL; |
|
243 |
+ re->originline=(re->originline<0)?0:re->originline; |
|
244 |
+ } |
|
245 |
+ if(re->curline>(re->originline+re->maxrow-LINEFORCESCROLL)) |
|
246 |
+ re->originline+=LINEFORCESCROLL; |
|
239 | 247 |
re_drawheader(re); |
240 | 248 |
re->contentsdirty=1; |
241 | 249 |
} else if(event->key.keysym.sym==SDLK_LEFT || event->key.keysym.sym==SDLK_RIGHT) { |
... | ... |
@@ -243,20 +251,25 @@ re_processkey(re_t *re, SDL_Event *event) |
243 | 251 |
return(-1); /* couldn't get current line data */ |
244 | 252 |
if(event->key.keysym.sym==SDLK_LEFT && re->cursorpos==0) |
245 | 253 |
return(-1); /* going left but already at leftmost char */ |
246 |
- linecols=redata_generic_utf8len(ptr,len); |
|
247 |
- oldcol=redata_generic_utf8len(ptr,re->cursorpos-newpos); |
|
254 |
+ maxcol=redata_generic_utf8len(ptr,len); |
|
248 | 255 |
inc=(event->key.keysym.sym==SDLK_LEFT)?-1:1; |
249 | 256 |
has_nl=((len>0 && ptr[len-1]=='\n')?1:0); |
250 |
- if(re->lastcol<=linecols) { |
|
251 |
- ptr2=redata_generic_utf8col(ptr,len,re->lastcol+inc); |
|
252 |
- } else |
|
257 |
+ if(re->curcol<=maxcol) |
|
258 |
+ ptr2=redata_generic_utf8col(ptr,len,re->curcol+inc); |
|
259 |
+ else |
|
253 | 260 |
ptr2=NULL; |
254 | 261 |
if(ptr2!=NULL) |
255 | 262 |
re->cursorpos=newpos+(ptr2-ptr); |
256 | 263 |
else |
257 | 264 |
re->cursorpos=newpos+(len-has_nl); /* we're past the last col, set cursor to last pos in line */ |
258 |
- if((re->lastcol+inc)>=0) |
|
259 |
- re->lastcol+=inc; |
|
265 |
+ if((re->curcol+inc)>=0) |
|
266 |
+ re->curcol+=inc; |
|
267 |
+ if(re->curcol<(re->origincol+COLFORCESCROLL)) { |
|
268 |
+ re->origincol-=COLFORCESCROLL; |
|
269 |
+ re->origincol=(re->origincol<0)?0:re->origincol; |
|
270 |
+ } |
|
271 |
+ if(re->curcol>(re->origincol+re->maxcol-COLFORCESCROLL)) |
|
272 |
+ re->origincol+=COLFORCESCROLL; |
|
260 | 273 |
re_drawheader(re); |
261 | 274 |
re->contentsdirty=1; |
262 | 275 |
} else if(event->key.keysym.sym==SDLK_a) { |
... | ... |
@@ -275,7 +288,7 @@ re_drawheader(re_t *re) |
275 | 288 |
if(redata_pos2linecol(re->data,re->cursorpos,&line,&col)==-1) |
276 | 289 |
line=col=-1; |
277 | 290 |
reui_fill(re->ui,0,0,re->ui->w,re->ui->fontheight,"\x00\x00\xff\xff"); |
278 |
- reui_printf(re->ui,0,0,"\xff\xff\x00\xff","File: %s Line:%i (%i) Col:%i (%i) Pos:%li",re->filename,re->lastrow,line,re->lastcol,col,re->cursorpos); |
|
291 |
+ reui_printf(re->ui,0,0,"\xff\xff\x00\xff","File: %s Line:%i (%i) Col:%i (%i) Pos:%li",re->filename,re->curline,line,re->curcol,col,re->cursorpos); |
|
279 | 292 |
return(0); |
280 | 293 |
} |
281 | 294 |
|
... | ... |
@@ -283,7 +296,7 @@ int |
283 | 296 |
re_drawcontents(re_t *re) |
284 | 297 |
{ |
285 | 298 |
long pos,newpos; |
286 |
- char *ptr; |
|
299 |
+ char *ptr,*visibleptr; |
|
287 | 300 |
int len; |
288 | 301 |
int y,row; |
289 | 302 |
char *curptr; |
... | ... |
@@ -292,7 +305,7 @@ re_drawcontents(re_t *re) |
292 | 305 |
if(re==NULL) |
293 | 306 |
return(-1); |
294 | 307 |
reui_fill(re->ui,re->x,re->y,re->w,re->h,"\xdf\xdf\xdf\xff"); |
295 |
- row=re->lastrow; |
|
308 |
+ row=re->curline-re->originline; |
|
296 | 309 |
pos=re->cursorpos; |
297 | 310 |
while(row>0) { |
298 | 311 |
if(redata_line_info(re->data,pos,&newpos,NULL,NULL)==-1) |
... | ... |
@@ -301,20 +314,22 @@ re_drawcontents(re_t *re) |
301 | 314 |
row--; |
302 | 315 |
} |
303 | 316 |
/* highlight current line */ |
304 |
- reui_fill(re->ui,re->x,re->y+(re->lastrow)*re->ui->fontheight,re->w,re->ui->fontheight+1,"\xef\xef\xef\xff"); |
|
317 |
+ reui_fill(re->ui,re->x,re->y+(re->curline-re->originline)*re->ui->fontheight,re->w,re->ui->fontheight+1,"\xef\xef\xef\xff"); |
|
305 | 318 |
/* draw the lines */ |
306 | 319 |
for(y=re->y;y<(re->y+re->h);y+=re->ui->fontheight,row++) { |
307 | 320 |
if(redata_line_info(re->data,pos,&newpos,&ptr,&len)==-1) |
308 | 321 |
break; /* couldn't get line start pos */ |
309 | 322 |
has_nl=((len>0 && ptr[len-1]=='\n')?1:0); |
310 |
- reui_write(re->ui,re->x,y,"\x00\x00\x00\xff",(char *)ptr,len-has_nl); |
|
311 |
- if(row==re->lastrow) { |
|
323 |
+ visibleptr=redata_generic_utf8col((char *)ptr,len-has_nl,re->origincol); |
|
324 |
+ if(visibleptr!=NULL) |
|
325 |
+ reui_write(re->ui,re->x,y,"\x00\x00\x00\xff",visibleptr,len-has_nl-(visibleptr-ptr)); |
|
326 |
+ if(row==(re->curline-re->originline)) { |
|
312 | 327 |
#warning DEBUG write of current char |
313 |
- reui_fill(re->ui,re->x+re->ui->fontwidth*re->lastcol,y,re->ui->fontwidth,re->ui->fontheight+1,"\x00\x00\x00\xff"); |
|
328 |
+ reui_fill(re->ui,re->x+re->ui->fontwidth*(re->curcol-re->origincol),y,re->ui->fontwidth,re->ui->fontheight+1,"\x00\x00\x00\xff"); |
|
314 | 329 |
#warning TODO: consider tabs |
315 |
- curptr=redata_generic_utf8col(ptr,len-has_nl,re->lastcol); |
|
330 |
+ curptr=redata_generic_utf8col(ptr,len-has_nl,re->curcol); |
|
316 | 331 |
curptrlen=(curptr==NULL)?0:(len-has_nl)-(curptr-ptr); |
317 |
- reui_write(re->ui,re->x+re->ui->fontwidth*re->lastcol,y,"\xff\xff\xff\xff",curptr,redata_generic_utf8charlen(ptr,curptrlen)); |
|
332 |
+ reui_write(re->ui,re->x+re->ui->fontwidth*(re->curcol-re->origincol),y,"\xff\xff\xff\xff",curptr,redata_generic_utf8charlen(ptr,curptrlen)); |
|
318 | 333 |
#warning TODO: if it is one of '[','{','<','>','}',']', highlight the matching bracket/parens/anglebracket. |
319 | 334 |
} |
320 | 335 |
pos=newpos+len; |