?? frame.c
字號:
static char rcsid[] = "$Id: Frame.c,v 1.3 1998/12/16 03:10:59 falk Exp falk $" ;/* Frame.c - Put a decorative frame around any other widget. * * Author: Edward A. Falk * falk@falconer.vip.best.com * * Date: Sept 29, 1998 * * * $Log: Frame.c,v $ * Revision 1.3 1998/12/16 03:10:59 falk * fixed shadow_etched_out, shadow_etched_in * * Revision 1.2 1998/12/15 06:35:16 falk * label is now another child widget. Some names * changed for compatibility with Motif. * * Revision 1.1 1998/10/12 01:33:20 falk * Initial revision * * Revision 1.1 1998/10/09 17:11:12 falk * Initial revision * * */#include <stdio.h>#include <X11/Xlib.h>#include <X11/IntrinsicP.h>#include <X11/StringDefs.h>#include <X11/Xmu/Converters.h>#include <X11/Xmu/CharSet.h>#include <X11/Xaw/Label.h>#include "FrameP.h"#include "Gcs.h"#define MIN_SIZE 2 /* make sure we don't have zero-size widget */#define TTL_MARGIN 5 /* minimum margin for title */#ifndef max#define max(a,b) ((a)<(b)?(b):(a))#define min(a,b) ((a)>(b)?(b):(a))#endif/**************************************************************** * * Frame Resources * ****************************************************************/#define offset(field) XtOffsetOf(FrameRec, frame.field)static XtResource resources[] = { { XtNshadowType, XtCShadowType, XtRShadowType, sizeof(XtShadowType), offset(type), XtRImmediate, (XtPointer)Solid}, { XtNshadowWidth, XtCShadowWidth, XtRDimension, sizeof(Dimension), offset(shadowWidth), XtRImmediate, (XtPointer)2 }, { XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel), offset(foreground), XtRString, XtDefaultForeground}, { XtNtitle, XtCTitle, XtRWidget, sizeof(Widget), offset(title), XtRWidget, NULL}, { XtNjustify, XtCJustify, XtRJustify, sizeof(XtJustify), offset(justify), XtRString, XtJustifyLeft}, { XtNmarginWidth, XtCMargin, XtRDimension, sizeof(Dimension), offset(marginWidth), XtRImmediate, (XtPointer)0 }, { XtNmarginHeight, XtCMargin, XtRDimension, sizeof(Dimension), offset(marginHeight), XtRImmediate, (XtPointer)0 }, { XtNallowResize, XtCAllowResize, XtRBoolean, sizeof(Boolean), offset(allowResize), XtRImmediate, (XtPointer)True }, {XtNbeNiceToColormap, XtCBeNiceToColormap, XtRBoolean, sizeof(Boolean), offset(be_nice_to_cmap), XtRImmediate, (XtPointer) True}, {XtNtopShadowContrast, XtCTopShadowContrast, XtRInt, sizeof(int), offset(top_shadow_contrast), XtRImmediate, (XtPointer) 20}, {XtNbottomShadowContrast, XtCBottomShadowContrast, XtRInt, sizeof(int), offset(bot_shadow_contrast), XtRImmediate, (XtPointer) 40}, { XtNborderWidth, XtCBorderWidth, XtRDimension, sizeof(Dimension), XtOffsetOf(RectObjRec,rectangle.border_width), XtRImmediate, (XtPointer)0},};#undef offset#ifndef __STDC__ /* member functions */static void FrameClassInit();static void FrameInit();static void FrameResize();static void FrameExpose();static void FrameDestroy();static void FrameRealize();static Boolean FrameSetValues();static XtGeometryResult FrameQueryGeometry();static XtGeometryResult FrameGeometryManager();static void FrameChangeManaged();static void _CvtStringToShadowType() ; /* internal privates */static void FrameAllocGCs() ; /* get rendering GCs */static void FrameFreeGCs() ; /* return rendering GCs */static void DrawFrame() ; /* draw all frame */static void DrawFrame() ; /* draw frame around contents */static void DrawTrim() ; /* draw trim around a tab */static void DrawBorder() ; /* draw border */static void PreferredSize() ; /* compute preferred size */static void PreferredSize3() ; /* compute preferred size */static Widget FrameChild() ; /* find primary child widget */static void Draw3dBox() ;static Pixel AllocShadowPixel() ;#elsestatic void FrameClassInit() ;static void FrameInit( Widget req, Widget new, ArgList args, Cardinal *) ;static void FrameRealize( Widget w, Mask *, XSetWindowAttributes *) ;static void FrameDestroy( Widget w ) ;static void FrameResize( Widget w) ;static void FrameExpose( Widget w , XEvent *event , Region region ) ;static Boolean FrameSetValues( Widget, Widget, Widget, ArgList, Cardinal *) ;static XtGeometryResult FrameQueryGeometry( Widget, XtWidgetGeometry *, XtWidgetGeometry *) ;static XtGeometryResult FrameGeometryManager( Widget, XtWidgetGeometry *, XtWidgetGeometry *) ;static void FrameChangeManaged( Widget w) ;static void _CvtStringToShadowType( XrmValuePtr, Cardinal *, XrmValuePtr, XrmValuePtr) ;static void FrameAllocGCs( FrameWidget fw ) ;static void FrameFreeGCs( FrameWidget fw ) ;static void PreferredSize( FrameWidget fw, Dimension *w, Dimension *h) ;static void PreferredSize3(FrameWidget, int cw, int ch, int tw, int th, Dimension *, Dimension *) ;static Widget FrameChild(FrameWidget) ;#endif#if XtSpecificationRelease < 5static GC XtAllocateGC() ;#endif#define AddRect(i,xx,yy,w,h) \ do{rects[(i)].x=(xx); rects[i].y=(yy); \ rects[i].width=(w); rects[i].height=(h);}while(0)/****************************************************************** Full class record constant*****************************************************************/FrameClassRec frameClassRec = { {/* core_class fields */ /* superclass */ (WidgetClass) &compositeClassRec, /* class_name */ "Frame", /* widget_size */ sizeof(FrameRec), /* class_initialize */ FrameClassInit, /* class_part_init */ NULL, /* TODO? */ /* class_inited */ FALSE, /* initialize */ FrameInit, /* initialize_hook */ NULL, /* realize */ FrameRealize, /* actions */ NULL, /* num_actions */ 0, /* resources */ resources, /* num_resources */ XtNumber(resources), /* xrm_class */ NULLQUARK, /* compress_motion */ TRUE, /* compress_exposure */ TRUE, /* compress_enterleave*/ TRUE, /* visible_interest */ FALSE, /* destroy */ FrameDestroy, /* resize */ FrameResize, /* expose */ FrameExpose, /* set_values */ FrameSetValues, /* set_values_hook */ NULL, /* set_values_almost */ XtInheritSetValuesAlmost, /* get_values_hook */ NULL, /* accept_focus */ NULL, /* version */ XtVersion, /* callback_private */ NULL, /* tm_table */ XtInheritTranslations, /* query_geometry */ FrameQueryGeometry, /* display_accelerator*/ XtInheritDisplayAccelerator, /* extension */ NULL }, {/* composite_class fields */ /* geometry_manager */ FrameGeometryManager, /* change_managed */ FrameChangeManaged, /* insert_child */ XtInheritInsertChild, /* TODO? */ /* delete_child */ XtInheritDeleteChild, /* TODO? */ /* extension */ NULL }, {/* Frame class fields */ /* extension */ NULL, }};WidgetClass frameWidgetClass = (WidgetClass)&frameClassRec;#ifdef DEBUG#ifdef __STDC__#define assert(e) \ ((e) || fprintf(stderr,"yak! %s at %s:%d\n",#e,__FILE__,__LINE__))#else#define assert(e) \ ((e) || fprintf(stderr,"yak! e at %s:%d\n",__FILE__,__LINE__))#endif#else#define assert(e)#endif/**************************************************************** * * Member Procedures * ****************************************************************/static voidFrameClassInit(){ static XtConvertArgRec parentCvtArgs[] = { {XtBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.parent), sizeof(Widget)} }; XtAddConverter( XtRString, XtRShadowType, _CvtStringToShadowType, NULL, 0) ; XtAddConverter( XtRString, XtRJustify, XmuCvtStringToJustify, NULL, 0) ; XtSetTypeConverter (XtRString, XtRWidget, XmuNewCvtStringToWidget, parentCvtArgs, XtNumber(parentCvtArgs), XtCacheNone, (XtDestructor)NULL);} /* Init a newly created frame widget. Compute height of frame * and optionally compute size of widget. *//* ARGSUSED */static voidFrameInit(request, new, args, num_args) Widget request, new; ArgList args; Cardinal *num_args;{ FrameWidget newFw = (FrameWidget)new; /* if size not explicitly set, set it to our preferred size now. */ if( request->core.width == 0 || request->core.height == 0 ) { Dimension w,h ; PreferredSize(newFw, &w, &h) ; if( request->core.width == 0 ) new->core.width = w ; if( request->core.height == 0 ) new->core.height = h ; XtClass(new)->core_class.resize(new) ; } /* defer GC allocation, etc., until Realize() time. */ newFw->frame.foregroundGC = newFw->frame.topGC = newFw->frame.botGC = None ; newFw->frame.needs_layout = False ;} /* Called when frame widget first realized. Create the window * and allocate the GCs */static voidFrameRealize(w, valueMask, attributes) Widget w; Mask *valueMask; XSetWindowAttributes *attributes;{ FrameWidget fw = (FrameWidget) w; /* TODO: shouldn't this chain to the parent's realize instead? */ XtCreateWindow( w, (unsigned)InputOutput, (Visual *)CopyFromParent, *valueMask, attributes); FrameAllocGCs(fw) ;}static voidFrameDestroy(w) Widget w ;{ FrameFreeGCs((FrameWidget)w) ;} /* Parent has resized us. This will require that the frame be * laid out again. */static voidFrameResize(w) Widget w;{ FrameWidget fw = (FrameWidget) w; Widget child ; Dimension cw,ch,bw ; Dimension sw = fw->frame.shadowWidth ; Widget ttl = fw->frame.title ; int tw,th ; /* title width, height */ int cy ; /* child y */ if( ttl != NULL ) { tw = ttl->core.width ; th = ttl->core.height ; switch( fw->frame.justify ) { case XtJustifyLeft: fw->frame.tx = sw + max(fw->frame.marginWidth, TTL_MARGIN) ; break ; case XtJustifyCenter: fw->frame.tx = (w->core.width - tw) / 2 ; break ; case XtJustifyRight: fw->frame.tx = w->core.width - tw - sw - max(fw->frame.marginWidth, TTL_MARGIN ) ; break ; } fw->frame.ty = 0 ; cy = max(th,sw) + fw->frame.marginHeight ; fw->frame.sy = th > sw ? (th - sw)/2 : 0 ; fw->frame.sh = fw->core.height - fw->frame.sy ; XtConfigureWidget(ttl, fw->frame.tx, fw->frame.ty, tw,th, 0) ; } else { cy = sw + fw->frame.marginHeight ; fw->frame.sy = 0 ; fw->frame.sh = fw->core.height ; } if( (child = FrameChild(fw)) != NULL ) { /* assign child position & size. */ bw = child->core.border_width ; cw = fw->core.width - 2*(bw+sw+fw->frame.marginWidth) ; ch = fw->core.height - cy - sw - fw->frame.marginHeight - 2*bw ; /* ... except don't assign them when we don't know what they are! */ if (!cw) { cw = child->core.width; fw->core.width = cw + 2*(bw+sw+fw->frame.marginWidth) ; } if (!ch) { ch = child->core.height; fw->core.height = ch + cy + sw + fw->frame.marginHeight + 2*bw ; } XtConfigureWidget(child, sw+fw->frame.marginWidth, cy, cw,ch, bw) ; } fw->frame.needs_layout = False ;} /* Resize */ /* Redraw entire Frame widget *//* ARGSUSED */static voidFrameExpose(w, event, region) Widget w ; XEvent *event ; Region region ;{ FrameWidget fw = (FrameWidget) w; int s = fw->frame.shadowWidth ; Position sy = fw->frame.sy ; Dimension sw = fw->core.width ; Dimension sh = fw->frame.sh ; GC topGC = fw->frame.topGC ; GC botGC = fw->frame.botGC ; if( fw->frame.needs_layout ) XtClass(w)->core_class.resize(w) ; switch( fw->frame.type ) { case Blank: break ; case Solid: Draw3dBox(w, 0,sy, sw,sh, s, fw->frame.foregroundGC, fw->frame.foregroundGC) ; break ; case Raised: Draw3dBox(w, 0,sy, sw,sh, s, topGC, botGC) ; break ; case Lowered: Draw3dBox(w, 0,sy, sw,sh, s, botGC, topGC) ; break ; case Ridge: Draw3dBox(w, s/2, sy+s/2, sw-s, sh-s, s-s/2, botGC, topGC) ; Draw3dBox(w, 0,sy,sw,sh, s/2, topGC, botGC) ; break ; case Groove: Draw3dBox(w, s/2, sy+s/2, sw-s, sh-s, s-s/2, topGC, botGC) ; Draw3dBox(w, 0,sy, sw,sh, s/2, botGC, topGC) ; break ; case Plateau: Draw3dBox(w, 0,sy+0, sw, sh, 2, topGC, botGC) ; Draw3dBox(w, s-2, sy+s-2, sw-s*2+4, sh-s*2+4, 2, botGC, topGC) ;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -