[Y-devel] About widget implementation

Casey Winans casey at cwinans.com
Fri Mar 12 19:23:04 GMT 2004


On Fri, 2004-03-12 at 13:09, Daniele wrote:
> Hi,
> i try to implement a ruler widget (like kruler) but i have a question.
> I see the label implementation (label.c)
> 
> static void
> labelPaint (struct Widget *self_w, struct Painter *painter)
> {
>   painterSaveState(painter);
>   themeDrawLabel (painter, castBack (self_w));
>   painterRestoreState(painter);
> }
> 
> And basic theme implementation (draw.c)
> 
> void
> basicDrawLabel(struct Painter *painter, struct Label *label)
> {
>   int offset, width, ascender, descender;
>   struct Font *font;
>   const char *text =
>                objectGetPropertyAsString (labelToObject (label), "text");
>   const char *alignment =
>                objectGetPropertyAsString (labelToObject (label), "alignment");
>   struct Rectangle *rect = widgetGetRectangle (labelToWidget (label));
>   enum WidgetState state = widgetGetState (labelToWidget (label));
> 
>   int xp, yp;
> 
>   painterSaveState (painter);     <------------------------------ Why do this 
> 
>   painterSetPenColour (painter, 0xFF000000);
> 
>   font = basicGetDefaultFont ();
> 
>   fontGetMetrics (font, &ascender, &descender, NULL); 
>   fontMeasureString (font, text, &offset, &width, NULL);
> 
>   yp = (rect->h - ascender - descender ) / 2 + ascender;
>   if (alignment && strcasecmp (alignment, "right") == 0)
>     xp = (rect->w - width);
>   else if (alignment && strcasecmp (alignment, "center") == 0)
>     xp = (rect->w - width + offset) / 2 - offset;
>   else
>     xp = - offset;
> 
>   fontRenderString (font, painter, text, xp, yp);
> 
>   painterRestoreState (painter);   <------------------------------ Why do this
> 
>   rectangleDestroy(rect);
> }
> 
> my question is why do painter(Save/Restore)State if i do the same in 
> labelPaint?
> 
> Thanks for the answer,
> 
> Daniele 

I was quick to respond before.. I now see your point. I agree that this
is redundant. I would be in favor of removing it from basicDrawLabel
(and all other [theme_name]Draw[widget_name] functions) so that it's
done in one place and each theme doesn't have to worry about the
painter's state... just its current job.

-- 
Casey Winans <casey at cwinans.com>




More information about the Y-devel mailing list









2026 © www.Y-Windows.Org - All Rights Reserved