>From fef2779bc52603137c6a6a7326c3738117572349 Mon Sep 17 00:00:00 2001
From: Johann Haarhoff <[email protected]>
Date: Mon, 29 Mar 2010 21:58:14 +0200
Subject: [PATCH] WINGs buttons and labels now have text
Running WINGs/Tests/wtest now draws a label and some buttons with
text on them.
---
WINGs/Tests/wtest.c | 5 +-
WINGs/WINGs/WINGs.h | 2 +-
WINGs/wbutton.c | 4 +-
WINGs/wfont.c | 2 +-
WINGs/wmisc.c | 9 ++--
test/cairo/draw.c | 105 +++++++++++++++++++++++++++++++++++++++++++++-----
6 files changed, 106 insertions(+), 21 deletions(-)
diff --git a/WINGs/Tests/wtest.c b/WINGs/Tests/wtest.c
index b1ba52b..b3bb54f 100644
--- a/WINGs/Tests/wtest.c
+++ b/WINGs/Tests/wtest.c
@@ -253,6 +253,7 @@ void testList(WMScreen * scr)
WMMapSubwidgets(win);
WMMapWidget(win);
}
+#endif
void testButton(WMScreen * scr)
{
@@ -289,7 +290,6 @@ void testButton(WMScreen * scr)
WMMapSubwidgets(win);
WMMapWidget(win);
}
-#endif
#ifdef obsolete
void testGradientButtons(WMScreen * scr)
@@ -540,7 +540,7 @@ void testStuff(WMScreen *scr)
button= WMCreateCommandButton(win);
WMSetButtonText(button, "OK");
WMResizeWidget(button, 80, 24);
- WMMoveWidget(button, 400-80-12, 300-24-12);
+ WMMoveWidget(button, 400-80-12, 200-24-12);
button= WMCreateCommandButton(win);
WMSetButtonText(button, "Cancel");
@@ -1008,6 +1008,7 @@ int main(int argc, char **argv)
* Put the testSomething() function you want to test here.
*/
testStuff(scr);
+ testButton(scr);
#if 0
testText(scr);
diff --git a/WINGs/WINGs/WINGs.h b/WINGs/WINGs/WINGs.h
index cad8d9d..43e4b7a 100644
--- a/WINGs/WINGs/WINGs.h
+++ b/WINGs/WINGs/WINGs.h
@@ -866,7 +866,7 @@ WMColorSpec WMDarkGrayColorSpec();
void WMDrawString(cairo_t *cairo, WMColorSpec *color, WMFont *font,
int x, int y, char *text);
-int WMWidthOfString(WMFont *font, char *text);
+int WMWidthOfString(WMFont *font, const char *text);
diff --git a/WINGs/wbutton.c b/WINGs/wbutton.c
index 00bdb72..77e4cdd 100644
--- a/WINGs/wbutton.c
+++ b/WINGs/wbutton.c
@@ -497,7 +497,7 @@ static void paintButton(Button * bPtr)
caption = bPtr->altCaption;
if (bPtr->altImage)
image = bPtr->altImage;
- textColor = bPtr->altTextColor;
+ textColor = bPtr->altTextColor;
}
if (bPtr->flags.statePush && bPtr->flags.bordered) {
@@ -521,7 +521,7 @@ static void paintButton(Button * bPtr)
caption = bPtr->altCaption;
if (bPtr->altImage)
image = bPtr->altImage;
- textColor = bPtr->altTextColor;
+ textColor = bPtr->altTextColor;
}
}
diff --git a/WINGs/wfont.c b/WINGs/wfont.c
index d6f616a..775ef88 100644
--- a/WINGs/wfont.c
+++ b/WINGs/wfont.c
@@ -290,7 +290,7 @@ WMFont *WMBoldSystemFontOfSize(WMScreen * scrPtr, int size)
return font;
}
-int WMWidthOfString(WMFont *font, char *text)
+int WMWidthOfString(WMFont *font, const char *text)
{
cairo_text_extents_t extents;
diff --git a/WINGs/wmisc.c b/WINGs/wmisc.c
index b5efbb0..0413654 100644
--- a/WINGs/wmisc.c
+++ b/WINGs/wmisc.c
@@ -331,14 +331,15 @@ void W_PaintText(cairo_t *cairo, WMFont *font, int x,
int y,
int count;
int fheight = WMFontHeight(font);
- line_x= x + (width - WMWidthOfString(font, ptr))/2;
- WMDrawString(cairo, color, font, line_x, y, ptr);
- return;
+ //line_x= x + (width - WMWidthOfString(font, ptr))/2;
+ //WMDrawString(cairo, color, font, line_x, y, ptr);
+ //return;
while (length > 0) {
count = fitText(ptr, font, width, wrap);
- //XXX line_width = WMWidthOfString(font, ptr, count);
+ line_width = WMWidthOfString(font, text);
+
if (alignment == WALeft)
line_x = x;
else if (alignment == WARight)
diff --git a/test/cairo/draw.c b/test/cairo/draw.c
index aded698..515d4b6 100644
--- a/test/cairo/draw.c
+++ b/test/cairo/draw.c
@@ -11,27 +11,110 @@
* cairo code you want to try in draw and very quickly see the results
* -- JH
*/
+static void curve_rectangle(cairo_t *cr,
+ double x0, double y0, double rect_width, double rect_height,
+ double radius)
+{
+ double x1,y1;
+
+ x1=x0+rect_width;
+ y1=y0+rect_height;
+ if (!rect_width || !rect_height)
+ return;
+ if (rect_width/2<radius) {
+ if (rect_height/2<radius) {
+ cairo_move_to (cr, x0, (y0 + y1)/2);
+ cairo_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0);
+ cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2);
+ cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1);
+ cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2);
+ } else {
+ cairo_move_to (cr, x0, y0 + radius);
+ cairo_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0);
+ cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
+ cairo_line_to (cr, x1 , y1 - radius);
+ cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1);
+ cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- radius);
+ }
+ } else {
+ if (rect_height/2<radius) {
+ cairo_move_to (cr, x0, (y0 + y1)/2);
+ cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0);
+ cairo_line_to (cr, x1 - radius, y0);
+ cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2);
+ cairo_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
+ cairo_line_to (cr, x0 + radius, y1);
+ cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2);
+ } else {
+ cairo_move_to (cr, x0, y0 + radius);
+ cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0);
+ cairo_line_to (cr, x1 - radius, y0);
+ cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
+ cairo_line_to (cr, x1 , y1 - radius);
+ cairo_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
+ cairo_line_to (cr, x0 + radius, y1);
+ cairo_curve_to (cr, x0, y1, x0, y1, x0, y1- radius);
+ }
+ }
+ cairo_close_path (cr);
+}
void draw(Display *dpy, Screen *scr, Visual *vis, Window win) {
cairo_surface_t *surf;
cairo_t *ct;
- surf = (cairo_surface_t *) cairo_xlib_surface_create(dpy, win, vis,
200, 200);
- ct = cairo_create(surf);
-
- cairo_pattern_t *linpat, *radpat;
- linpat = cairo_pattern_create_linear (0, 0, 100, 100);
- cairo_pattern_add_color_stop_rgb (linpat, 0, 0, 0.3, 0.8);
- cairo_pattern_add_color_stop_rgb (linpat, 1, 0, 0.8, 0.3);
+ int width=200;
+ int height = 50;
+ int x = 10;
+ int y = 10;
- radpat = cairo_pattern_create_radial (100, 100, 50, 100, 100, 75);
- cairo_pattern_add_color_stop_rgba (radpat, 0, 0, 0, 0, 1);
- cairo_pattern_add_color_stop_rgba (radpat, 0.5, 0, 0, 0, 0);
+ unsigned char border[4]= {0x00, 0x00, 0x00, 0x70};
+ //unsigned char color1[4]= {0x8c, 0xb1, 0xbc, 0xff};
+ //unsigned char color2[4]= {0xcb, 0xf3, 0xff, 0xff};
+ unsigned char color1[4]= {0x0, 0x0, 0x0, 0xff};
+ unsigned char color2[4]= {0xcf, 0xcf, 0xcf, 0xff};
+ unsigned char scolor1[4]= {0xff, 0xff, 0xff, 0xe5};
+ unsigned char scolor2[4]= {0xff, 0xff, 0xff, 0x70};
- cairo_set_source (ct, linpat);
- cairo_mask (ct, radpat);
+ surf = (cairo_surface_t *) cairo_xlib_surface_create(dpy, win, vis,
500, 500);
+ ct = cairo_create(surf);
- cairo_stroke (ct);
+ cairo_pattern_t *shine;
+ cairo_pattern_t *base;
+
+ shine= cairo_pattern_create_linear(0, 0, 0, height*2/5);
+ cairo_pattern_add_color_stop_rgba(shine, 0,
+ scolor1[0]/255.0, scolor1[1]/255.0, scolor1[2]/255.0,
+ scolor1[3]/255.0);
+ cairo_pattern_add_color_stop_rgba(shine, 1,
+ scolor2[0]/255.0, scolor2[1]/255.0, scolor2[2]/255.0,
+ scolor2[3]/255.0);
+
+ base= cairo_pattern_create_linear(0, 0, 0, height-1);
+ cairo_pattern_add_color_stop_rgba(base, 0,
+ color1[0]/255.0, color1[1]/255.0, color1[2]/255.0,
+ color1[3]/255.0);
+ cairo_pattern_add_color_stop_rgba(base, 1,
+ color2[0]/255.0, color2[1]/255.0, color2[2]/255.0,
+ color2[3]/255.0);
+
+
+ curve_rectangle(ct, x, y, width-1, height-1, height*2/3);
+ cairo_set_source(ct, base);
+ cairo_fill_preserve(ct);
+ cairo_clip(ct);
+
+ curve_rectangle(ct, x, y, width-1, height*2/5, width);
+ cairo_set_source(ct, shine);
+ cairo_fill(ct);
+
+ curve_rectangle(ct, x, y, width-1, height-1, height*2/3);
+ cairo_set_source_rgba(ct, border[0]/255.0, border[1]/255.0,
border[2]/255.0, border[3]/255.0);
+ cairo_set_line_width(ct, 2.0);
+ cairo_stroke(ct);
+
+ cairo_pattern_destroy(shine);
+ cairo_pattern_destroy(base);
cairo_destroy(ct);
cairo_surface_destroy(surf);
--
1.6.0.3