#include #include typedef struct { FL_FORM * f1; FL_OBJECT * canvas; FL_OBJECT * quit; } FD_f1; /*************************************** * Function called when there's an Expose event for the canvas ***************************************/ static int draw( FL_OBJECT * obj FL_UNUSED_ARG, Window win, int win_width, int win_height, XEvent * xev FL_UNUSED_ARG, void * user_data FL_UNUSED_ARG ) { const char * text = "This uses Xft"; XGlyphInfo extents; XftFont * font; XftDraw * xftdraw; XRenderColor xrcolor; XftColor xftcolor; Display * display = fl_display; int screen = fl_screen; Visual * visual = fl_state[ fl_vmode ].xvinfo->visual; Colormap colormap = fl_state[ fl_vmode ].colormap; /* Try to get the exact same font as XForms uses (the inactive section should be equivalent) */ #if 1 font = XftFontOpenXlfd( display, screen, "-*-helvetica-medium-o-*-*-*-240-*-*-p-*-*-*" ); #else font = XftFontOpen ( display, screen, XFT_FAMILY, XftTypeString, "helvetica", XFT_SIZE, XftTypeDouble, 24.0, XFT_SLANT, XftTypeInteger, XFT_SLANT_OBLIQUE, NULL ); #endif /* Get structure with information needed for rendering with either XRender or the X11 core protocol */ xftdraw = XftDrawCreate( display, win, visual, colormap ); /* Prepare color (white) for drawing the background */ xrcolor.red = 0xffff; xrcolor.green = 0xffff; xrcolor.blue = 0xffff; xrcolor.alpha = 0xffff; XftColorAllocValue( display, visual, colormap, &xrcolor, &xftcolor ); /* Draw the background (could also done with X11 XDrawRectangle()) and afterwards release the color used */ XftDrawRect( xftdraw, &xftcolor, 0, 0, win_width, win_height ); XftColorFree( display, visual, colormap, &xftcolor ); /* Prepare color (black) for drawing the string */ xrcolor.red = 0x0; xrcolor.green = 0x0; xrcolor.blue = 0x0; xrcolor.alpha = 0xffff; XftColorAllocValue( display, visual, colormap, &xrcolor, &xftcolor ); /* Get the size of the text to be drawn (for centering) */ XftTextExtents8( display, font, ( XftChar8 const * ) text, strlen( text ), &extents ); /* Draw the text */ XftDrawString8( xftdraw, &xftcolor, font, ( win_width - extents.width ) / 2, ( win_height + extents.height ) / 2, ( XftChar8 const * ) text, strlen( text ) ); /* Free all allocated resources */ XftColorFree( display, visual, colormap, &xftcolor ); XftDrawDestroy( xftdraw ); XftFontClose( display, font ); return 1; } /*************************************** ***************************************/ FD_f1 * create_form_f1( void ) { FL_OBJECT *obj; FD_f1 *fdui = fl_malloc( sizeof *fdui ); fdui->f1 = fl_bgn_form( FL_NO_BOX, 280, 180 ); fl_add_box( FL_FLAT_BOX, 0, 0, 280, 180, "" ); obj = fl_add_text( FL_NORMAL_TEXT, 20, 20, 240, 40, "This uses X11" ); fl_set_object_lalign( obj, FL_ALIGN_CENTER ); fl_set_object_boxtype( obj, FL_DOWN_BOX ); fl_set_object_lsize( obj, FL_HUGE_SIZE ); fl_set_object_lstyle( obj, FL_ITALIC_STYLE ); fl_set_object_color( obj, FL_WHITE, FL_WHITE ); obj = fdui->canvas = fl_add_canvas( FL_NORMAL_CANVAS, 20, 70, 240, 40, "" ); fl_add_canvas_handler( obj, Expose, draw, NULL ); fdui->quit = fl_add_button( FL_NORMAL_BUTTON, 90, 130, 100, 30, "Exit" ); fl_end_form( ); return fdui->f1->fdui = fdui; } /*************************************** ***************************************/ int main( int argc, char * argv[ ] ) { FD_f1 *fd_f1; fl_initialize( &argc, argv, 0, 0, 0 ); fd_f1 = create_form_f1( ); fl_show_form( fd_f1->f1, FL_PLACE_CENTERFREE, FL_FULLBORDER, "X11 versus Xft" ); while ( fl_do_forms( ) != fd_f1->quit ) /* empty */ ; fl_finish( ); fl_free( fd_f1 ); return 0; }