📄 multislice.c
字号:
# include <stdio.h># include <X11/IntrinsicP.h># include <X11/StringDefs.h># include "MultiSliceP.h"# include <X11/Xaw/Form.h># include <X11/Xaw/Command.h># include <X11/Xaw/Scrollbar.h>static char sccsid[] = "@(#)MultiSlice.c 1.5 8/14/95";static char *sccs_h_id = MSLICE_H_SCCS_ID;static char *sccs_ph_id = MSLICE_PH_SCCS_ID;static XtResource resources[] = {#define offset(field) XtOffset(MultiSliceWidget, slicetext.field) /* {name, class, type, size, offset, default_type, default_addr}, */ { XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *), offset(font), XtRString, XtDefaultFont }, { XtNaltBackground, XtCForeground, XtRPixel, sizeof(Pixel ), offset(alt_background), XtRString, XtDefaultForeground }, { XtNaltForeground, XtCBackground, XtRPixel, sizeof(Pixel ), offset(alt_foreground), XtRString, XtDefaultBackground }, { XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel ), offset(foreground), XtRString, XtDefaultForeground }, { XtNsideForeground, XtCBackground, XtRPixel, sizeof(Pixel ), offset(side_fg), XtRString, XtDefaultBackground }, { XtNsideBackground, XtCForeground, XtRPixel, sizeof(Pixel ), offset(side_bg), XtRString, XtDefaultForeground }, { XtNsideWidth, XtCWidth, XtRInt, sizeof(int ), offset(side_width), XtRString, "15" }, { XtNtabStop, XtCTabStop, XtRInt, sizeof(int ), offset(tabstop), XtRString, "4" }, { XtNcallback, XtCCallback, XtRCallback, sizeof(XtCallbackList), offset(select_callback), XtRCallback, NULL },#undef offset};# define Max_line_length 1000static void doexpose(); int DEBUG = 0;static void SliceResize ();void dojump (w,x,y) Widget w; XtPointer x,y;{ float *p; int at_line; p = (float *)y; if(DEBUG) printf ("Do jump %6.2f\n",*p); SliceSetTop (x,*p); /* at_line = (*p)*Lines_in_file + 1; if (DEBUG)printf (" at %d\n",at_line); */}static fakeexpose (w) MultiSliceWidget w;{ XAnyEvent e; e.display = XtDisplay(w); e.window = XtWindow (w); doexpose (w,&e,NULL);}void doscroll (x,w,y) MultiSliceWidget w; XtPointer x,y;{ int i; int page=1; i = (int )y; if(DEBUG)printf ("Do scroll %d\n",i); page = 0.5 + ( w->core.height)/ (float)( w->slicetext.font->ascent + w->slicetext.font->descent); page --; if(DEBUG)printf ("page %d\n",page); if (i < 0) w->slicetext.top -= page; else w->slicetext.top += page; if (w->slicetext.top < 1) w->slicetext.top = 1; else if (w->slicetext.top + page > w->slicetext.slicesrc.n_lines) w->slicetext.top = w->slicetext.slicesrc.n_lines - page; if(DEBUG)printf ("Set top to %d\n",w->slicetext.top); SliceResize(w); fakeexpose(w);}static SliceSetThumb (w,sb) MultiSliceWidget w; Widget sb;{ w->slicetext.scrollbar = sb;}static reset_top_line (w,delta) MultiSliceWidget w; int delta;{ int page,top; top = w->slicetext.top + delta; page = 0.5 + ( w->core.height)/ (float)( w->slicetext.font->ascent + w->slicetext.font->descent); page--; if (top < 1) top = 1; else if (top + page > w->slicetext.slicesrc.n_lines) top = w->slicetext.top; if (top == w->slicetext.top) return; w->slicetext.top = top; SliceResize(w); fakeexpose(w);}static void up_a_line (x,w,y) MultiSliceWidget w; XtPointer x,y;{ reset_top_line (w,-1);}static void down_a_line (x,w,y) MultiSliceWidget w; XtPointer x,y;{ reset_top_line (w,1);}# include "down.xbm"# include "up.xbm"Widget CreateSliceBox(name,parent) char *name; Widget parent;{ Widget bar; Widget frame; Widget slicewin; Widget up,dn; Arg args[9]; int na; int box_height; int sb_left = 0; Pixmap up_bitmap,dn_bitmap; Display *d; d = XtDisplay(parent); up_bitmap = XCreateBitmapFromData (XtDisplay(parent), RootWindow(d,DefaultScreen(d)),up_bits, up_width,up_height); dn_bitmap = XCreateBitmapFromData (XtDisplay(parent), RootWindow(d,DefaultScreen(d)),down_bits, down_width,down_height); frame = XtCreateManagedWidget(name, formWidgetClass, parent, NULL, 0); if (sb_left){ na = 0; XtSetArg (args[na],XtNlength,600); na++; XtSetArg (args[na],XtNtop,XtChainTop); na++; XtSetArg (args[na],XtNbottom,XtChainBottom); na++; XtSetArg (args[na],XtNleft,XtChainLeft); na++; XtSetArg (args[na],XtNright,XtChainLeft); na++; bar = XtCreateWidget("bar", scrollbarWidgetClass, frame, args, na); XtManageChild(bar); na = 0; XtSetArg (args[na],XtNresizable,True); na++; XtSetArg (args[na],XtNtop,XtChainTop); na++; XtSetArg (args[na],XtNbottom,XtChainBottom); na++; XtSetArg (args[na],XtNleft,XtChainLeft); na++; XtSetArg (args[na],XtNright,XtChainRight); na++; slicewin = XtCreateManagedWidget("slicewin", multisliceWidgetClass,frame, args, na); } else { na = 0; XtSetArg (args[na],XtNresizable,True); na++; XtSetArg (args[na],XtNtop,XtChainTop); na++; XtSetArg (args[na],XtNbottom,XtChainBottom); na++; XtSetArg (args[na],XtNleft,XtChainLeft); na++; XtSetArg (args[na],XtNright,XtChainRight); na++; slicewin = XtCreateManagedWidget("slicewin", multisliceWidgetClass,frame, args, na); up = XtVaCreateManagedWidget ("up",commandWidgetClass, frame, XtNborderWidth, 0, XtNbitmap, up_bitmap, XtNshapeStyle, XmuShapeRectangle, XtNfromHoriz, slicewin, XtNtop, XtChainTop, XtNbottom, XtChainTop, XtNleft, XtChainRight, XtNright, XtChainRight, NULL); na = 0; XtSetArg (args[na],XtNlength,600 - 36); na++; XtSetArg (args[na],XtNtop,XtChainTop); na++; XtSetArg (args[na],XtNbottom,XtChainBottom); na++; XtSetArg (args[na],XtNleft,XtChainRight); na++; XtSetArg (args[na],XtNright,XtChainRight); na++; XtSetArg (args[na],XtNfromVert,up); na++; XtSetArg (args[na],XtNfromHoriz,slicewin); na++; bar = XtCreateWidget("bar", scrollbarWidgetClass, frame, args, na); XtManageChild(bar); dn = XtVaCreateManagedWidget ("dn",commandWidgetClass, frame, XtNborderWidth, 0, XtNbitmap, dn_bitmap, XtNshapeStyle, XmuShapeRectangle, XtNfromHoriz, slicewin, XtNfromVert, bar, XtNtop, XtChainBottom, XtNbottom, XtChainBottom, XtNleft, XtChainRight, XtNright, XtChainRight, NULL); XtAddCallback (up,XtNcallback,up_a_line,slicewin); XtAddCallback (dn,XtNcallback,down_a_line,slicewin); } XtAddCallback (bar,XtNscrollProc,doscroll,slicewin); XtAddCallback (bar,XtNjumpProc,dojump,slicewin); SliceSetThumb(slicewin,bar); return slicewin;}static void doinit(r,nw,a,n) MultiSliceWidget r,nw; ArgList a; Cardinal *n;{ XGCValues values; Window root = RootWindowOfScreen(XtScreen(nw)); if(DEBUG) printf ("Init WxH (%dx%d)\n",nw->core.width,nw->core.height); /* printf (" FG %d ",nw->core.border_pixel); */ if(DEBUG) printf (" FG %d ",nw->slicetext.foreground); if(DEBUG) printf (" BG %d\n",nw->core.background_pixel); if(DEBUG) printf ("Alt FG %d ",nw->slicetext.alt_foreground); if(DEBUG) printf ("Alt BG %d\n",nw->slicetext.alt_background); if (DEBUG)fflush(stdout); if (nw->core.height == 0) nw->core.height = 600; if (nw->core.width == 0) nw->core.width = 800; values.foreground = nw->slicetext.foreground; values.background = nw->core.background_pixel; values.font = nw->slicetext.font->fid; nw->slicetext.gc = XtGetGC ((Widget)nw, GCForeground | GCBackground | GCFont, &values); values.foreground = nw->slicetext.alt_foreground; values.background = nw->slicetext.alt_background; values.font = nw->slicetext.font->fid; nw->slicetext.alt_gc = XtGetGC ((Widget)nw, GCForeground | GCBackground | GCFont, &values); nw->slicetext.slicesrc.n_lines = 0; nw->slicetext.top = 1; nw->slicetext.scrollbar = NULL; nw->slicetext.log = 0; nw->slicetext.sidebar = XCreatePixmap (XtDisplay(nw), root,nw->slicetext.side_width,DisplayHeight(XtDisplay(nw), DefaultScreen(XtDisplay(nw))), DefaultDepthOfScreen(XtScreen(nw))); values.foreground = nw->slicetext.side_fg; values.background = nw->slicetext.side_bg; nw->slicetext.side_gc = XtGetGC ((Widget)nw, GCForeground | GCBackground, &values); values.foreground = nw->slicetext.side_bg; values.background = nw->slicetext.side_fg; nw->slicetext.side_clear_gc = XtGetGC ((Widget)nw, GCForeground | GCBackground, &values); XFillRectangle (XtDisplay(nw),nw->slicetext.sidebar, nw->slicetext.side_clear_gc,0,0,nw->slicetext.side_width, DisplayHeight(XtDisplay(nw), DefaultScreen(XtDisplay(nw)))); nw->slicetext.hook = NULL; if (DEBUG)printf ("Tabstop %d\n",nw->slicetext.tabstop);}static void buildline (buff,line,n,tabstop,linemap) char *buff,*line; int *linemap; int n,tabstop;{ int in_at,out_at,add_tabs,n_spaces; int i; if(DEBUG)printf ("buildline:\n"); if(DEBUG)for (i = 0; i < n; i++) printf ("%c",line[i]); if(DEBUG)printf ("[%d,%d]\n",n,strlen(line)); out_at = 0; for (in_at = 0; in_at < n; in_at++){ linemap[in_at] = out_at; if (line[in_at] == '\t'){ n_spaces = tabstop - (out_at % tabstop); for (add_tabs = 0; add_tabs < n_spaces; add_tabs++) buff[out_at++] = ' '; } else buff[out_at++] = line[in_at]; } buff[out_at] = '\0'; linemap[n] = out_at; if(DEBUG)printf ("%s[%d,%d]\n",buff,out_at,strlen(buff)); for (i = 0; i <= n; i++) if(DEBUG)printf ("%3d",linemap[i]); if(DEBUG)printf ("\n");}/* drawline (e->display,e->window, w, line_buff, lineoffset,base,at );*/static void drawline (d,win,w,buff,offset,base,at,linemap) Display *d; Window win; MultiSliceWidget w; char *buff; int offset,base,at,*linemap;{ int highlight[Max_line_length]; int i,chunk,from,to,pos,last; GC gc,gcx,gc_to_use; XFontStruct font,fontx; for (i = 0; i <= strlen (buff) ; i++) highlight [i] = 0; if (DEBUG)printf ("chunks(%d) ", w->slicetext.slicesrc.line[at].n_highlight); for (chunk = 0 ; chunk < w->slicetext.slicesrc.line[at].n_highlight; chunk++){ if (DEBUG)printf ("[%d,%d] ", w->slicetext.slicesrc.line[at].start[chunk], w->slicetext.slicesrc.line[at].stop[chunk]); from = w->slicetext.slicesrc.line[at].start[chunk] ? w->slicetext.slicesrc.line[at].start[chunk] : 1; to = w->slicetext.slicesrc.line[at].stop[chunk] ? w->slicetext.slicesrc.line[at].stop[chunk] : w->slicetext.slicesrc.line[at].length; for (i = from; i <= to ; i ++) highlight[i] = 1; } pos = 1; last = w->slicetext.slicesrc.line[at].length; while (pos <= last){ to = pos + 1; while ((to <= last) && (highlight[pos] == highlight[to])) to++; gc_to_use = highlight[pos] ? w->slicetext.alt_gc : w->slicetext.gc; if (DEBUG)printf ("gc (%d,%d,%d)\n",gc_to_use,gc,gcx); if (DEBUG)printf ("pos %d\n",pos); fflush(stdout); if(DEBUG)printf ("line %d (%d,%d) -> (%d,%d)\n",at,pos,to, linemap[pos-1],linemap[to-1]); XDrawImageString (d,win,gc_to_use,offset,base, &buff[linemap[pos - 1]],linemap[to-1]-linemap[pos-1]); /* &buff[pos - 1],to-pos); */ offset += XTextWidth (highlight[pos] ? w->slicetext.font : w->slicetext.font, &buff[linemap[pos - 1]],linemap[to-1]-linemap[pos-1]); /* &buff[pos - 1],to-pos); */ pos = to; } for (i = 0; i <w->slicetext.slicesrc.line[at].length ; i++) if (DEBUG)printf ("%c",w->slicetext.slicesrc.line[at].text[i]); if (DEBUG)printf ("\n"); for (i = 0; i <w->slicetext.slicesrc.line[at].length ; i++) if (DEBUG)printf ("%1d",highlight[i]); if (DEBUG)printf ("\n");}int adjust_line (w,line) int line; MultiSliceWidget w;{ int *off,i; off = w->slicetext.slicesrc.offset; for (i = 1; i < w->slicetext.slicesrc.n_files ; i++){ if (off[i] >= line) return line - off[i-1]; } return line - off[w->slicetext.slicesrc.n_files-1];}static void doexpose(w,e,r) MultiSliceWidget w; XAnyEvent *e; Region r;{ int base,ascent,descent,h,line,linemap[Max_line_length]; GC gc_to_use; int LastLine,Nvisible,FirstLine; int lineoffset; char line_no[10],format[20],line_buff[Max_line_length]; base = w->slicetext.font->ascent; descent = w->slicetext.font->descent; ascent = base; h = ascent+descent; /* if (turn)gc_to_use = w->slicetext.gc; else gc_to_use = w->slicetext.alt_gc;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -