diff options
Diffstat (limited to 'generic/ttk/ttkLabel.c')
-rw-r--r-- | generic/ttk/ttkLabel.c | 149 |
1 files changed, 105 insertions, 44 deletions
diff --git a/generic/ttk/ttkLabel.c b/generic/ttk/ttkLabel.c index c34d479..f9698ed 100644 --- a/generic/ttk/ttkLabel.c +++ b/generic/ttk/ttkLabel.c @@ -46,25 +46,29 @@ typedef struct { /* Text element options table. * NB: Keep in sync with label element option table. */ -static Ttk_ElementOptionSpec TextElementOptions[] = { +static const Ttk_ElementOptionSpec TextElementOptions[] = { { "-text", TK_OPTION_STRING, - Tk_Offset(TextElement,textObj), "" }, + offsetof(TextElement,textObj), "" }, { "-font", TK_OPTION_FONT, - Tk_Offset(TextElement,fontObj), DEFAULT_FONT }, + offsetof(TextElement,fontObj), DEFAULT_FONT }, { "-foreground", TK_OPTION_COLOR, - Tk_Offset(TextElement,foregroundObj), "black" }, - { "-underline", TK_OPTION_INT, - Tk_Offset(TextElement,underlineObj), "-1"}, + offsetof(TextElement,foregroundObj), "black" }, + { "-underline", TK_OPTION_INDEX, +#if !defined(TK_NO_DEPRECATED) && (TCL_MAJOR_VERSION < 9) + offsetof(TextElement,underlineObj), "-1"}, +#else + offsetof(TextElement,underlineObj), NULL}, +#endif { "-width", TK_OPTION_INT, - Tk_Offset(TextElement,widthObj), "-1"}, + offsetof(TextElement,widthObj), "-1"}, { "-anchor", TK_OPTION_ANCHOR, - Tk_Offset(TextElement,anchorObj), "w"}, + offsetof(TextElement,anchorObj), "w"}, { "-justify", TK_OPTION_JUSTIFY, - Tk_Offset(TextElement,justifyObj), "left" }, + offsetof(TextElement,justifyObj), "left" }, { "-wraplength", TK_OPTION_PIXELS, - Tk_Offset(TextElement,wrapLengthObj), "0" }, + offsetof(TextElement,wrapLengthObj), "0" }, { "-embossed", TK_OPTION_INT, - Tk_Offset(TextElement,embossedObj), "0"}, + offsetof(TextElement,embossedObj), "0"}, { NULL, TK_OPTION_BOOLEAN, 0, NULL } }; @@ -126,7 +130,7 @@ static void TextCleanup(TextElement *text) static void TextDraw(TextElement *text, Tk_Window tkwin, Drawable d, Ttk_Box b) { XColor *color = Tk_GetColorFromObj(tkwin, text->foregroundObj); - int underline = -1; + Tcl_Size underline = INT_MIN; XGCValues gcValues; GC gc1, gc2; Tk_Anchor anchor = TK_ANCHOR_CENTER; @@ -170,14 +174,21 @@ static void TextDraw(TextElement *text, Tk_Window tkwin, Drawable d, Ttk_Box b) Tk_DrawTextLayout(Tk_Display(tkwin), d, gc1, text->textLayout, b.x, b.y, 0/*firstChar*/, -1/*lastChar*/); - Tcl_GetIntFromObj(NULL, text->underlineObj, &underline); - if (underline >= 0) { - if (text->embossed) { - Tk_UnderlineTextLayout(Tk_Display(tkwin), d, gc2, - text->textLayout, b.x+1, b.y+1, underline); + if (text->underlineObj != NULL) { + TkGetIntForIndex(text->underlineObj, TCL_INDEX_NONE, 0, &underline); + if (underline < INT_MIN) { + underline = INT_MIN; + } else if (underline > INT_MAX) { + underline = INT_MAX; + } + if (underline != INT_MIN) { + if (text->embossed) { + Tk_UnderlineTextLayout(Tk_Display(tkwin), d, gc2, + text->textLayout, b.x+1, b.y+1, underline); + } + Tk_UnderlineTextLayout(Tk_Display(tkwin), d, gc1, + text->textLayout, b.x, b.y, underline); } - Tk_UnderlineTextLayout(Tk_Display(tkwin), d, gc1, - text->textLayout, b.x, b.y, underline); } if (clipRegion != NULL) { @@ -229,7 +240,7 @@ static void TextElementDraw( } } -static Ttk_ElementSpec TextElementSpec = { +static const Ttk_ElementSpec TextElementSpec = { TK_STYLE_VERSION_2, sizeof(TextElement), TextElementOptions, @@ -238,6 +249,51 @@ static Ttk_ElementSpec TextElementSpec = { }; /*---------------------------------------------------------------------- + * +++ cText (collapsing text) element. + * + * This element is the same as the Text element, except its dimensions + * are 0,0 when the text to display is "". + */ + +static int cTextSetup(TextElement *text, Tk_Window tkwin) +{ + if (*Tcl_GetString(text->textObj) == '\0') { + return 0; + } else { + return TextSetup(text, tkwin); + } +} + +static void cTextElementSize( + TCL_UNUSED(void *), /* clientData */ + void *elementRecord, + Tk_Window tkwin, + int *widthPtr, + int *heightPtr, + TCL_UNUSED(Ttk_Padding *)) +{ + TextElement *text = (TextElement *)elementRecord; + + if (!cTextSetup(text, tkwin)) + return; + + *heightPtr = text->height; + *widthPtr = TextReqWidth(text); + + TextCleanup(text); + + return; +} + +static const Ttk_ElementSpec cTextElementSpec = { + TK_STYLE_VERSION_2, + sizeof(TextElement), + TextElementOptions, + cTextElementSize, + TextElementDraw +}; + +/*---------------------------------------------------------------------- * +++ Image element. * Draws an image. */ @@ -255,13 +311,13 @@ typedef struct { /* ===> NB: Keep in sync with label element option table. <=== */ -static Ttk_ElementOptionSpec ImageElementOptions[] = { +static const Ttk_ElementOptionSpec ImageElementOptions[] = { { "-image", TK_OPTION_STRING, - Tk_Offset(ImageElement,imageObj), "" }, + offsetof(ImageElement,imageObj), "" }, { "-stipple", TK_OPTION_STRING, /* Really: TK_OPTION_BITMAP */ - Tk_Offset(ImageElement,stippleObj), "gray50" }, + offsetof(ImageElement,stippleObj), "gray50" }, { "-background", TK_OPTION_COLOR, - Tk_Offset(ImageElement,backgroundObj), DEFAULT_BACKGROUND }, + offsetof(ImageElement,backgroundObj), DEFAULT_BACKGROUND }, { NULL, TK_OPTION_BOOLEAN, 0, NULL } }; @@ -401,7 +457,7 @@ static void ImageElementDraw( } } -static Ttk_ElementSpec ImageElementSpec = { +static const Ttk_ElementSpec ImageElementSpec = { TK_STYLE_VERSION_2, sizeof(ImageElement), ImageElementOptions, @@ -457,43 +513,47 @@ typedef struct { int totalWidth, totalHeight; } LabelElement; -static Ttk_ElementOptionSpec LabelElementOptions[] = { +static const Ttk_ElementOptionSpec LabelElementOptions[] = { { "-compound", TK_OPTION_ANY, - Tk_Offset(LabelElement,compoundObj), "none" }, + offsetof(LabelElement,compoundObj), "none" }, { "-space", TK_OPTION_PIXELS, - Tk_Offset(LabelElement,spaceObj), "4" }, + offsetof(LabelElement,spaceObj), "4" }, /* Text element part: * NB: Keep in sync with TextElementOptions. */ { "-text", TK_OPTION_STRING, - Tk_Offset(LabelElement,text.textObj), "" }, + offsetof(LabelElement,text.textObj), "" }, { "-font", TK_OPTION_FONT, - Tk_Offset(LabelElement,text.fontObj), DEFAULT_FONT }, + offsetof(LabelElement,text.fontObj), DEFAULT_FONT }, { "-foreground", TK_OPTION_COLOR, - Tk_Offset(LabelElement,text.foregroundObj), "black" }, - { "-underline", TK_OPTION_INT, - Tk_Offset(LabelElement,text.underlineObj), "-1"}, + offsetof(LabelElement,text.foregroundObj), "black" }, + { "-underline", TK_OPTION_INDEX, +#if !defined(TK_NO_DEPRECATED) && (TCL_MAJOR_VERSION < 9) + offsetof(LabelElement,text.underlineObj), "-1"}, +#else + offsetof(LabelElement,text.underlineObj), NULL}, +#endif { "-width", TK_OPTION_INT, - Tk_Offset(LabelElement,text.widthObj), ""}, + offsetof(LabelElement,text.widthObj), ""}, { "-anchor", TK_OPTION_ANCHOR, - Tk_Offset(LabelElement,text.anchorObj), "w"}, + offsetof(LabelElement,text.anchorObj), "w"}, { "-justify", TK_OPTION_JUSTIFY, - Tk_Offset(LabelElement,text.justifyObj), "left" }, + offsetof(LabelElement,text.justifyObj), "left" }, { "-wraplength", TK_OPTION_PIXELS, - Tk_Offset(LabelElement,text.wrapLengthObj), "0" }, + offsetof(LabelElement,text.wrapLengthObj), "0" }, { "-embossed", TK_OPTION_INT, - Tk_Offset(LabelElement,text.embossedObj), "0"}, + offsetof(LabelElement,text.embossedObj), "0"}, /* Image element part: * NB: Keep in sync with ImageElementOptions. */ { "-image", TK_OPTION_STRING, - Tk_Offset(LabelElement,image.imageObj), "" }, + offsetof(LabelElement,image.imageObj), "" }, { "-stipple", TK_OPTION_STRING, /* Really: TK_OPTION_BITMAP */ - Tk_Offset(LabelElement,image.stippleObj), "gray50" }, + offsetof(LabelElement,image.stippleObj), "gray50" }, { "-background", TK_OPTION_COLOR, - Tk_Offset(LabelElement,image.backgroundObj), DEFAULT_BACKGROUND }, + offsetof(LabelElement,image.backgroundObj), DEFAULT_BACKGROUND }, { NULL, TK_OPTION_BOOLEAN, 0, NULL } }; @@ -512,7 +572,7 @@ static void LabelSetup( Ttk_Compound *compoundPtr = &c->compound; Tk_GetPixelsFromObj(NULL, tkwin, c->spaceObj, &c->space); - Ttk_GetCompoundFromObj(NULL, c->compoundObj, (int *)compoundPtr); + Ttk_GetCompoundFromObj(NULL, c->compoundObj, compoundPtr); /* * Deal with TTK_COMPOUND_NONE. @@ -628,7 +688,7 @@ static void LabelElementSize( */ static void DrawCompound( LabelElement *l, Ttk_Box b, Tk_Window tkwin, Drawable d, Ttk_State state, - int imageSide, int textSide) + Ttk_Side imageSide, Ttk_Side textSide) { Ttk_Box imageBox = Ttk_PlaceBox(&b, l->image.width, l->image.height, imageSide, 0); @@ -698,7 +758,7 @@ static void LabelElementDraw( LabelCleanup(l); } -static Ttk_ElementSpec LabelElementSpec = { +static const Ttk_ElementSpec LabelElementSpec = { TK_STYLE_VERSION_2, sizeof(LabelElement), LabelElementOptions, @@ -716,6 +776,7 @@ TtkLabel_Init(Tcl_Interp *interp) Ttk_Theme theme = Ttk_GetDefaultTheme(interp); Ttk_RegisterElement(interp, theme, "text", &TextElementSpec, NULL); + Ttk_RegisterElement(interp, theme, "ctext", &cTextElementSpec, NULL); Ttk_RegisterElement(interp, theme, "image", &ImageElementSpec, NULL); Ttk_RegisterElement(interp, theme, "label", &LabelElementSpec, NULL); } |