libframe: auto scale tick for retina

R=rsc
http://codereview.appspot.com/6850102
This commit is contained in:
Russ Cox 2012-11-25 23:48:19 -05:00
parent ffaaaf9dae
commit cc9547960e
3 changed files with 24 additions and 10 deletions

View file

@ -19,7 +19,6 @@ enum{
}; };
#define FRTICKW 3 #define FRTICKW 3
struct Frbox struct Frbox
{ {
long wid; /* in pixels */ long wid; /* in pixels */
@ -51,6 +50,7 @@ struct Frame
Image *tickback; /* saved image under tick */ Image *tickback; /* saved image under tick */
int ticked; /* flag: is tick onscreen? */ int ticked; /* flag: is tick onscreen? */
int noredraw; /* don't draw on the screen */ int noredraw; /* don't draw on the screen */
int tickscale; /* tick scaling factor */
}; };
ulong frcharofpt(Frame*, Point); ulong frcharofpt(Frame*, Point);

View file

@ -137,15 +137,15 @@ frredraw(Frame *f)
pt = frdrawsel0(f, pt, f->p1, f->nchars, f->cols[BACK], f->cols[TEXT]); pt = frdrawsel0(f, pt, f->p1, f->nchars, f->cols[BACK], f->cols[TEXT]);
} }
void static void
frtick(Frame *f, Point pt, int ticked) _frtick(Frame *f, Point pt, int ticked)
{ {
Rectangle r; Rectangle r;
if(f->ticked==ticked || f->tick==0 || !ptinrect(pt, f->r)) if(f->ticked==ticked || f->tick==0 || !ptinrect(pt, f->r))
return; return;
pt.x--; /* looks best just left of where requested */ pt.x -= f->tickscale; /* looks best just left of where requested */
r = Rect(pt.x, pt.y, pt.x+FRTICKW, pt.y+f->font->height); r = Rect(pt.x, pt.y, pt.x+FRTICKW*f->tickscale, pt.y+f->font->height);
/* can go into left border but not right */ /* can go into left border but not right */
if(r.max.x > f->r.max.x) if(r.max.x > f->r.max.x)
r.max.x = f->r.max.x; r.max.x = f->r.max.x;
@ -157,6 +157,17 @@ frtick(Frame *f, Point pt, int ticked)
f->ticked = ticked; f->ticked = ticked;
} }
void
frtick(Frame *f, Point pt, int ticked)
{
if(f->tickscale != scalesize(f->display, 1)) {
if(f->ticked)
_frtick(f, pt, 0);
frinittick(f);
}
_frtick(f, pt, ticked);
}
Point Point
_frdraw(Frame *f, Point pt) _frdraw(Frame *f, Point pt)
{ {

View file

@ -31,11 +31,14 @@ frinittick(Frame *f)
Image *b; Image *b;
Font *ft; Font *ft;
if(f->cols[BACK] == nil || f->display == nil)
return;
f->tickscale = scalesize(f->display, 1);
b = f->display->screenimage; b = f->display->screenimage;
ft = f->font; ft = f->font;
if(f->tick) if(f->tick)
freeimage(f->tick); freeimage(f->tick);
f->tick = allocimage(f->display, Rect(0, 0, FRTICKW, ft->height), b->chan, 0, DWhite); f->tick = allocimage(f->display, Rect(0, 0, f->tickscale*FRTICKW, ft->height), b->chan, 0, DWhite);
if(f->tick == nil) if(f->tick == nil)
return; return;
if(f->tickback) if(f->tickback)
@ -49,10 +52,10 @@ frinittick(Frame *f)
/* background color */ /* background color */
draw(f->tick, f->tick->r, f->cols[BACK], nil, ZP); draw(f->tick, f->tick->r, f->cols[BACK], nil, ZP);
/* vertical line */ /* vertical line */
draw(f->tick, Rect(FRTICKW/2, 0, FRTICKW/2+1, ft->height), f->display->black, nil, ZP); draw(f->tick, Rect(f->tickscale*(FRTICKW/2), 0, f->tickscale*(FRTICKW/2+1), ft->height), f->display->black, nil, ZP);
/* box on each end */ /* box on each end */
draw(f->tick, Rect(0, 0, FRTICKW, FRTICKW), f->cols[TEXT], nil, ZP); draw(f->tick, Rect(0, 0, f->tickscale*FRTICKW, f->tickscale*FRTICKW), f->cols[TEXT], nil, ZP);
draw(f->tick, Rect(0, ft->height-FRTICKW, FRTICKW, ft->height), f->cols[TEXT], nil, ZP); draw(f->tick, Rect(0, ft->height-f->tickscale*FRTICKW, f->tickscale*FRTICKW, ft->height), f->cols[TEXT], nil, ZP);
} }
void void