/* draw_gui.c proj5 Jon Squire */ /* Started from O'reilly example */ /* used Young's rubber band, proj4 */ /* change history: 12/1/95 JSS added menus to proj4 12/4/95 JSS added more callbacks 12/8/95 JSS added pan and zoom (the objects will never know of this change) 2/11/01 JSS added circle, text, rectangle-center 2/17/01 JSS added symbols mux, alu, and, or, xor, not, comp, arrow, arc 1/17/02 JSS added offset selection to draw text 1/17/02 JSS added input of file name to save files 1/20/02 JSS added file selection box 1/23/02 JSS added jpeg BW and COLOR -DXBITS=16 1/21/03 JSS fixed ARC, ARROW, new_file_name 3/11/03 JSS fixed AND, OR, XOR for y1>y0 missing arc 8/01/05 JSS adder RLC 2/06/08 JSS compensate for XWindows change XMSTRINGDEFINES */ #define XMSTRINGDEFINES 1 #include #include "draw_gui.h" #undef dabs #define dabs(x) ((x)<0?(-(x)):(x)) #undef iabs #define iabs(x) ((x)<0?(-(x)):(x)) #ifdef XBITS long int background = 0xFFFF; /* user must use white background 16 bit */ /* 0xffff on PC, FFFFFF on Sun */ #else long int background = 0xFFFFFF; /* user must use white background 24 bit */ #endif static void write_gif_data(unsigned char datablk[], unsigned char *byte_count, int * nbit, FILE * FpGif); static void write_gif_file(char filename[]); static void write_xbm_file(char filename[]); static void write_bwjpg_file(char filename[]); static void write_coljpg_file(char filename[]); /* draw_gui - sets up all windows and widgets then performs initialization then sends and receives messages from Object Oriented part */ void draw_gui(int argc, char *argv[]) { /* callback functions, just dummies */ void file_CB(Widget w, int index, XmAnyCallbackStruct *call_data); void ReallyQuitCB(); void edit_CB(); void arrange_CB(); void pen_CB(Widget w, int index, XmAnyCallbackStruct *call_data); void color_CB(); void action_CB(); void expose_CB(); void font_size_CB(); void DoText_string(); void DoSetFile_name(); void DoOpen(); void grid_CB(); void symbol_CB(); printf("Draw starting, be patient if over network \n"); /* Initialize toolkit, open display and create application shell. */ toplevel = XtVaAppInitialize(&app_context, "Draw", NULL, 0, &argc, argv, NULL, NULL); /* Create MainWindow. */ main_w = XtVaCreateManagedWidget("main", xmMainWindowWidgetClass, toplevel, XmNshadowThickness, 0, NULL); #define XMS(string) XmStringCreateSimple(string) /* Create a simple MenuBar that contains six menus */ menu_bar = XmVaCreateSimpleMenuBar(main_w, "menubar", XmVaCASCADEBUTTON, XMS("File"), 'F', XmVaCASCADEBUTTON, XMS("Edit"), 'E', XmVaCASCADEBUTTON, XMS("Arrange"), 'A', XmVaCASCADEBUTTON, XMS("FontSize"), 'S', XmVaCASCADEBUTTON, XMS("Grid"), 'G', /* XmVaCASCADEBUTTON, XMS("Pen"), 'P', done below! */ NULL); /* Create first menu, the "File" menu -- callback is file_CB() */ XmVaCreateSimplePulldownMenu(menu_bar, "file_menu", 0, (XtCallbackProc)(file_CB), XmVaPUSHBUTTON, XMS("Clear"), 'C', NULL, NULL, XmVaPUSHBUTTON, XMS("Open .draw"), 'O', NULL, NULL, XmVaPUSHBUTTON, XMS("New file name"), 'f', NULL, NULL, XmVaPUSHBUTTON, XMS("Save .draw"), 'S', NULL, NULL, XmVaPUSHBUTTON, XMS("Save .gif"), 'g', NULL, NULL, XmVaPUSHBUTTON, XMS("Save .xbm"), 'x', NULL, NULL, XmVaPUSHBUTTON, XMS("Save bw .jpg"), 'j', NULL, NULL, XmVaPUSHBUTTON, XMS("Save col .jpg"), 'j', NULL, NULL, XmVaPUSHBUTTON, XMS("Quit"), 'Q', "Ctrlc", XMS("Ctrl-C"), NULL); /* Create second menu, the "Edit" menu -- callback is edit_CB() */ XmVaCreateSimplePulldownMenu(menu_bar, "edit_menu", 1, (XtCallbackProc)(edit_CB), XmVaPUSHBUTTON, XMS("Select All"), 'S', NULL, NULL, XmVaPUSHBUTTON, XMS("Deselect All"), 'e', NULL, NULL, XmVaPUSHBUTTON, XMS("Delete"), 'D', NULL, NULL, XmVaPUSHBUTTON, XMS("Cut"), 'C', NULL, NULL, XmVaPUSHBUTTON, XMS("Copy"), 'o', NULL, NULL, XmVaPUSHBUTTON, XMS("Paste"), 'P', NULL, NULL, NULL); /* Create third menu, the "Arrange" menu -- callback is arrange_CB() */ XmVaCreateSimplePulldownMenu(menu_bar, "arrange_menu", 2, (XtCallbackProc)(arrange_CB), XmVaPUSHBUTTON, XMS("Group"), 'G', NULL, NULL, XmVaPUSHBUTTON, XMS("Ungroup"), 'U', NULL, NULL, XmVaPUSHBUTTON, XMS("Move to Front"), 'F', NULL, NULL, XmVaPUSHBUTTON, XMS("Move to Back"), 'B', NULL, NULL, NULL); /* Create fourth menu, the "font size" menu -- callback is font_size_CB() */ XmVaCreateSimplePulldownMenu(menu_bar, "font_size_menu", 3, (XtCallbackProc)(font_size_CB), XmVaPUSHBUTTON, XMS("courier 12"), 'f', NULL, NULL, XmVaPUSHBUTTON, XMS("courier 14"), 's', NULL, NULL, XmVaPUSHBUTTON, XMS("courier 18"), 'm', NULL, NULL, XmVaPUSHBUTTON, XMS("courier 24"), 'l', NULL, NULL, NULL); /* Create fifth menu, the "Grid" menu -- callback is grid_CB() */ XmVaCreateSimplePulldownMenu(menu_bar, "Grid", 4, (XtCallbackProc)(grid_CB), XmVaPUSHBUTTON, XMS("grid off"), 'o', NULL, NULL, XmVaPUSHBUTTON, XMS("display grid"), 'd', NULL, NULL, XmVaPUSHBUTTON, XMS("snap to grid"), 's', NULL, NULL, XmVaPUSHBUTTON, XMS("no snap"), 'n', NULL, NULL, XmVaPUSHBUTTON, XMS("spacing 5"), '5', NULL, NULL, XmVaPUSHBUTTON, XMS("spacing 10"), '0', NULL, NULL, XmVaPUSHBUTTON, XMS("spacing 15"), '1', NULL, NULL, XmVaPUSHBUTTON, XMS("spacing 20"), '2', NULL, NULL, XmVaPUSHBUTTON, XMS("spacing 30"), '3', NULL, NULL, XmVaPUSHBUTTON, XMS("spacing 40"), '4', NULL, NULL, NULL); /* Create sixth menu, the "Pen" menu -- callback is pen_CB() */ /* "Pen" menu full expanded with bitmaps */ pen_pulldown = XmCreatePulldownMenu(menu_bar, "Pen", NULL, 0); pen_b = XmCreateCascadeButton(menu_bar, "Pen", NULL, 0); XtVaSetValues(pen_b, XmNsubMenuId, pen_pulldown, NULL); XtManageChild(pen_b); XtVaGetValues(pen_b, XmNforeground, &fg, XmNbackground, &bg, NULL); pixmap = XmGetPixmap( XtScreen(toplevel), "pixel1.xbm", fg, bg); pixel1_b = XtVaCreateManagedWidget ("button", xmCascadeButtonWidgetClass, pen_pulldown, XmNlabelType, XmPIXMAP, XmNlabelPixmap, pixmap, NULL); pixmap = XmGetPixmap( XtScreen(toplevel), "pixel2.xbm", fg, bg); pixel2_b = XtVaCreateManagedWidget ("button", xmCascadeButtonWidgetClass, pen_pulldown, XmNlabelType, XmPIXMAP, XmNlabelPixmap, pixmap, NULL); pixmap = XmGetPixmap( XtScreen(toplevel), "pixel3.xbm", fg, bg); pixel3_b = XtVaCreateManagedWidget ("button", xmCascadeButtonWidgetClass, pen_pulldown, XmNlabelType, XmPIXMAP, XmNlabelPixmap, pixmap, NULL); pixmap = XmGetPixmap( XtScreen(toplevel), "pixel4.xbm", fg, bg); pixel4_b = XtVaCreateManagedWidget ("button", xmCascadeButtonWidgetClass, pen_pulldown, XmNlabelType, XmPIXMAP, XmNlabelPixmap, pixmap, NULL); pixmap = XmGetPixmap( XtScreen(toplevel), "pixelinh.xbm", fg, bg); pixelinh_b = XtVaCreateManagedWidget ("button", xmCascadeButtonWidgetClass, pen_pulldown, XmNlabelType, XmPIXMAP, XmNlabelPixmap, pixmap, NULL); XtAddCallback(pixel1_b, XmNactivateCallback, (XtCallbackProc)(pen_CB), (XtPointer)0); XtAddCallback(pixel2_b, XmNactivateCallback, (XtCallbackProc)(pen_CB), (XtPointer)1); XtAddCallback(pixel3_b, XmNactivateCallback, (XtCallbackProc)(pen_CB), (XtPointer)2); XtAddCallback(pixel4_b, XmNactivateCallback, (XtCallbackProc)(pen_CB), (XtPointer)3); XtAddCallback(pixelinh_b, XmNactivateCallback, (XtCallbackProc)(pen_CB), (XtPointer)4); XtManageChild(menu_bar); q_dialog = XmCreateQuestionDialog(toplevel, "Want to quit? ", NULL, 0); XtUnmanageChild(XmMessageBoxGetChild(q_dialog, XmDIALOG_HELP_BUTTON)); XtVaSetValues(q_dialog, XmNmessageString, XMS("Really want to QUIT ?"), NULL); XtAddCallback(q_dialog, XmNokCallback, (XtCallbackProc)(ReallyQuitCB), NULL); f_dialog = XmCreateQuestionDialog(toplevel, "Unable to write file.", NULL, 0); XtUnmanageChild(XmMessageBoxGetChild(f_dialog, XmDIALOG_HELP_BUTTON)); XtVaSetValues(f_dialog, XmNmessageString, XMS("Unable to write file."), NULL); text_name_dia = XmCreatePromptDialog(toplevel, "Enter text " , NULL, 0); XtAddCallback(text_name_dia, XmNokCallback, (XtCallbackProc)(DoText_string), (XtPointer)OK); XtAddCallback(text_name_dia, XmNcancelCallback, (XtCallbackProc)(DoText_string), (XtPointer)CANCEL); /*XtUnmanageChild(XmMessageBoxGetChild(text_name_dia, XmDIALOG_HELP_BUTTON));*/ file_name_dia = XmCreatePromptDialog(toplevel, "New root name" , NULL, 0); XtAddCallback(file_name_dia, XmNokCallback, (XtCallbackProc)(DoSetFile_name), (XtPointer)OK); XtAddCallback(file_name_dia, XmNcancelCallback, (XtCallbackProc)(DoSetFile_name), (XtPointer)CANCEL); /* ignore help button */ open_dialog = XmCreateFileSelectionDialog(toplevel, "select file to open" , NULL, 0); XtAddCallback(open_dialog, XmNokCallback, (XtCallbackProc)(DoOpen), (XtPointer)OK); XtAddCallback(open_dialog, XmNcancelCallback, (XtCallbackProc)(DoOpen), (XtPointer)CANCEL); XtVaSetValues(open_dialog, XmNpattern, XMS("*.draw"), NULL); /* Create paint_area */ paint_area = XtVaCreateManagedWidget ("form", xmFormWidgetClass, main_w, XmNresizePolicy, XmRESIZE_NONE, NULL); printf("create side_row \n"); side_row = XtVaCreateManagedWidget ("actions", xmRowColumnWidgetClass, paint_area, XmNorientation, XmVERTICAL, XmNpacking, XmPACK_TIGHT, XmNleftAttachment, XmATTACH_FORM, XmNtopAttachment, XmATTACH_FORM, XmNbottomAttachment, XmATTACH_FORM, NULL); printf("create side_row2 \n"); side_row2 = XtVaCreateManagedWidget ("symbols", xmRowColumnWidgetClass, paint_area, XmNorientation, XmVERTICAL, XmNpacking, XmPACK_TIGHT, XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, side_row, XmNtopAttachment, XmATTACH_FORM, XmNbottomAttachment, XmATTACH_FORM, NULL); printf("create top_row \n"); top_row = XtVaCreateManagedWidget ("colors", xmRowColumnWidgetClass, paint_area, XmNorientation, XmHORIZONTAL, XmNpacking, XmPACK_COLUMN, XmNtopAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, side_row2, NULL); printf("create scrolled_w \n"); scrolled_w = XtVaCreateManagedWidget ("scroll", xmScrolledWindowWidgetClass, paint_area, XmNrightAttachment, XmATTACH_FORM, XmNbottomAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, side_row2, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, top_row, XmNscrollBarDisplayPolicy, XmSTATIC, XmNscrollingPolicy, XmAPPLICATION_DEFINED, XmNresizePolicy, XmRESIZE_NONE, XmNwidth, xmid+xmid+20, XmNheight, ymid+ymid+20, NULL); printf("create draw_a \n"); draw_a = XtVaCreateManagedWidget ("canvas", xmDrawingAreaWidgetClass, scrolled_w, XmNwidth, xmid+xmid, XmNheight, ymid+ymid, NULL); XtAddEventHandler ( draw_a, ButtonPressMask, FALSE, StartRubberBand, NULL ); XtAddEventHandler ( draw_a, ButtonMotionMask, FALSE, TrackRubberBand, NULL ); XtAddEventHandler ( draw_a, ButtonReleaseMask, FALSE, EndRubberBand, NULL ); XtAddCallback(draw_a, XmNexposeCallback, (XtCallbackProc)(expose_CB), NULL); printf("create vertical_bar \n"); vertical_bar = XtVaCreateManagedWidget("vsb", xmScrollBarWidgetClass, scrolled_w, XmNorientation, XmVERTICAL, XmNsliderSize, 20, XmNmaximum, 100, XmNminimum, 0, NULL); XtAddCallback(vertical_bar, XmNvalueChangedCallback, (XtCallbackProc)(action_CB), (XtPointer)YOFF); printf("create horizontal \n"); horizontal_bar = XtVaCreateManagedWidget("hsb", xmScrollBarWidgetClass, scrolled_w, XmNorientation, XmHORIZONTAL, XmNsliderSize, 20, XmNmaximum, 100, NULL); XtAddCallback(horizontal_bar, XmNvalueChangedCallback, (XtCallbackProc)(action_CB), (XtPointer)XOFF); XmScrolledWindowSetAreas(scrolled_w, horizontal_bar, vertical_bar, draw_a); printf("create white_b \n"); white_b = XtVaCreateManagedWidget ("white", xmPushButtonWidgetClass, top_row, NULL); XtAddCallback(white_b, XmNactivateCallback, (XtCallbackProc)(color_CB), (XtPointer)WHITE); black_b = XtVaCreateManagedWidget ("black", xmPushButtonWidgetClass, top_row, NULL); XtAddCallback(black_b, XmNactivateCallback, (XtCallbackProc)(color_CB), (XtPointer)BLACK); red_b = XtVaCreateManagedWidget ("red", xmPushButtonWidgetClass, top_row, NULL); XtAddCallback(red_b, XmNactivateCallback, (XtCallbackProc)(color_CB), (XtPointer)RED); green_b = XtVaCreateManagedWidget ("green", xmPushButtonWidgetClass, top_row, NULL); XtAddCallback(green_b, XmNactivateCallback, (XtCallbackProc)(color_CB), (XtPointer)GREEN); blue_b = XtVaCreateManagedWidget ("blue", xmPushButtonWidgetClass, top_row, NULL); XtAddCallback(blue_b, XmNactivateCallback, (XtCallbackProc)(color_CB), (XtPointer)BLUE); orange_b = XtVaCreateManagedWidget ("orange", xmPushButtonWidgetClass, top_row, NULL); XtAddCallback(orange_b, XmNactivateCallback, (XtCallbackProc)(color_CB), (XtPointer)ORANGE); violet_b = XtVaCreateManagedWidget ("violet", xmPushButtonWidgetClass, top_row, NULL); XtAddCallback(violet_b, XmNactivateCallback, (XtCallbackProc)(color_CB), (XtPointer)VIOLET); yellow_b = XtVaCreateManagedWidget ("yellow", xmPushButtonWidgetClass, top_row, NULL); XtAddCallback(yellow_b, XmNactivateCallback, (XtCallbackProc)(color_CB), (XtPointer)YELLOW); gray_b = XtVaCreateManagedWidget ("gray", xmPushButtonWidgetClass, top_row, NULL); XtAddCallback(gray_b, XmNactivateCallback, (XtCallbackProc)(color_CB), (XtPointer)GRAY); ltgray_b = XtVaCreateManagedWidget ("lt gray", xmPushButtonWidgetClass, top_row, NULL); XtAddCallback(ltgray_b, XmNactivateCallback, (XtCallbackProc)(color_CB), (XtPointer)LTGRAY); printf("create draw_name on menu_bar \n"); draw_name = XtVaCreateManagedWidget ("root file name", xmListWidgetClass, menu_bar, /* top_row, */ XmNwidth, 200, NULL); printf("create frame_b \n"); frame_b = XtVaCreateManagedWidget ("frame", xmPushButtonWidgetClass, side_row, NULL); XtAddCallback(frame_b, XmNactivateCallback, (XtCallbackProc)(action_CB), (XtPointer)FRAME); select_b = XtVaCreateManagedWidget ("select", xmPushButtonWidgetClass, side_row, NULL); XtAddCallback(select_b, XmNactivateCallback, (XtCallbackProc)(action_CB), (XtPointer)SELECT); selecta_b = XtVaCreateManagedWidget ("select area", xmPushButtonWidgetClass, side_row, NULL); XtAddCallback(selecta_b, XmNactivateCallback, (XtCallbackProc)(action_CB), (XtPointer)SELECTAREA); text_b = XtVaCreateManagedWidget ("text", xmPushButtonWidgetClass, side_row, NULL); XtAddCallback(text_b, XmNactivateCallback, (XtCallbackProc)(action_CB), (XtPointer)TEXT); line_b = XtVaCreateManagedWidget ("line", xmPushButtonWidgetClass, side_row, NULL); XtAddCallback(line_b, XmNactivateCallback, (XtCallbackProc)(action_CB), (XtPointer)LINE); ellipse_b = XtVaCreateManagedWidget ("ellipse", xmPushButtonWidgetClass, side_row, NULL); XtAddCallback(ellipse_b, XmNactivateCallback, (XtCallbackProc)(action_CB), (XtPointer)ELLIPSE); circle_b = XtVaCreateManagedWidget ("circle", xmPushButtonWidgetClass, side_row, NULL); XtAddCallback(circle_b, XmNactivateCallback, (XtCallbackProc)(action_CB), (XtPointer)CIRCLE); rect_b = XtVaCreateManagedWidget ("rectangle", xmPushButtonWidgetClass, side_row, NULL); XtAddCallback(rect_b, XmNactivateCallback, (XtCallbackProc)(action_CB), (XtPointer)RECTANGLE); rectc_b = XtVaCreateManagedWidget ("rectanglec", xmPushButtonWidgetClass, side_row, NULL); XtAddCallback(rectc_b, XmNactivateCallback, (XtCallbackProc)(action_CB), (XtPointer)RECTANGLEC); blank2_b = XtVaCreateManagedWidget ("separator", xmSeparatorWidgetClass, side_row, NULL); line_color_b = XtVaCreateManagedWidget ("line color", xmPushButtonWidgetClass, side_row, NULL); XtAddCallback(line_color_b, XmNactivateCallback, (XtCallbackProc)(action_CB), (XtPointer)LINECOLOR); fill_color_b = XtVaCreateManagedWidget ("fill color", xmPushButtonWidgetClass, side_row, NULL); XtAddCallback(fill_color_b, XmNactivateCallback, (XtCallbackProc)(action_CB), (XtPointer)FILLCOLOR); back_color_b = XtVaCreateManagedWidget ("back color", xmPushButtonWidgetClass, side_row, NULL); XtAddCallback(back_color_b, XmNactivateCallback, (XtCallbackProc)(action_CB), (XtPointer)BACKCOLOR); blank3_b = XtVaCreateManagedWidget ("separator", xmSeparatorWidgetClass, side_row, NULL); filled_b = XtVaCreateManagedWidget ("filled", xmPushButtonWidgetClass, side_row, NULL); XtAddCallback(filled_b, XmNactivateCallback, (XtCallbackProc)(action_CB), (XtPointer)FILLED); unfilled_b = XtVaCreateManagedWidget ("unfilled", xmPushButtonWidgetClass, side_row, NULL); XtAddCallback(unfilled_b, XmNactivateCallback, (XtCallbackProc)(action_CB), (XtPointer)UNFILLED); printf("create scale_w \n"); scale_w = XtVaCreateManagedWidget ("Zoom", xmScaleWidgetClass, side_row, XmNtitleString, XMS("Zoom"), XmNdecimalPoints, 2, XmNmaximum, 200, XmNminimum, 25, XmNvalue, 100, XmNorientation, XmVERTICAL, XmNshowValue, True, XmNscaleHeight, 150, NULL); XtAddCallback(scale_w, XmNvalueChangedCallback, (XtCallbackProc)(action_CB), (XtPointer)ZOOM); vmux_b = XtVaCreateManagedWidget ("V mux", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(vmux_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)VMUX); hmux_b = XtVaCreateManagedWidget ("H mux", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(hmux_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)HMUX); alu_b = XtVaCreateManagedWidget ("alu", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(alu_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)ALU); and_b = XtVaCreateManagedWidget ("and", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(and_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)AND); or_b = XtVaCreateManagedWidget ("or", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(or_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)OR); xor_b = XtVaCreateManagedWidget ("xor", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(xor_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)XOR); not_b = XtVaCreateManagedWidget ("driver", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(not_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)NOT); comp_b = XtVaCreateManagedWidget ("comp dot", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(comp_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)COMP); arrow_b = XtVaCreateManagedWidget ("arrow <", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(arrow_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)ARROW); arc_b = XtVaCreateManagedWidget ("3pt arc", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(arc_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)ARC); dot_b = XtVaCreateManagedWidget ("dot", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(dot_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)DOT); hres_b = XtVaCreateManagedWidget ("H resist", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(hres_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)HRES); vres_b = XtVaCreateManagedWidget ("V resist", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(vres_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)VRES); hcap_b = XtVaCreateManagedWidget ("H capaci", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(hcap_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)HCAP); vcap_b = XtVaCreateManagedWidget ("V capaci", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(vcap_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)VCAP); hind_b = XtVaCreateManagedWidget ("H induct", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(hind_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)HIND); vind_b = XtVaCreateManagedWidget ("V induct", xmPushButtonWidgetClass, side_row2, NULL); XtAddCallback(vind_b, XmNactivateCallback, (XtCallbackProc)(symbol_CB), (XtPointer)VIND); XtManageChild(top_row); XtManageChild(side_row); XtManageChild(side_row2); XtManageChild(paint_area); XtVaSetValues(toplevel, XmNmenuBar, menu_bar, XmNworkWindow, paint_area, NULL); XtRealizeWidget(toplevel); display = XtDisplay(toplevel); screen_num = XDefaultScreen(display); screen = XDefaultScreenOfDisplay(display); cmap = XDefaultColormap(display, screen_num); status = XAllocNamedColor(display, cmap, "white", &white_c, &exact_c); if (!status) printf("no white \n"); status = XAllocNamedColor(display, cmap, "black", &black_c, &exact_c); if (!status) printf("no black \n"); status = XAllocNamedColor(display, cmap, "red", &red_c, &exact_c); if (!status) printf("no red \n"); status = XAllocNamedColor(display, cmap, "green", &green_c, &exact_c); if (!status) printf("no green \n"); status = XAllocNamedColor(display, cmap, "blue", &blue_c, &exact_c); if (!status) printf("no blue \n"); status = XAllocNamedColor(display, cmap, "violet", &violet_c, &exact_c); if (!status) printf("no violet \n"); status = XAllocNamedColor(display, cmap, "orange", &orange_c, &exact_c); if (!status) printf("no orange \n"); status = XAllocNamedColor(display, cmap, "yellow", &yellow_c, &exact_c); if (!status) printf("no yellow \n"); status = XAllocNamedColor(display, cmap, "gray", &gray_c, &exact_c); if (!status) printf("no gray \n"); status = XAllocNamedColor(display, cmap, "light gray", <gray_c, &exact_c); if (!status) printf("no light gray \n"); XtVaSetValues(white_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); XtVaSetValues(black_b, XmNforeground, white_c.pixel, XmNbackground, black_c.pixel, NULL); XtVaSetValues(red_b, XmNforeground, white_c.pixel, XmNbackground, red_c.pixel, NULL); XtVaSetValues(green_b, XmNforeground, black_c.pixel, XmNbackground, green_c.pixel, NULL); XtVaSetValues(blue_b, XmNforeground, white_c.pixel, XmNbackground, blue_c.pixel, NULL); XtVaSetValues(violet_b, XmNforeground, black_c.pixel, XmNbackground, violet_c.pixel, NULL); XtVaSetValues(orange_b, XmNforeground, black_c.pixel, XmNbackground, orange_c.pixel, NULL); XtVaSetValues(yellow_b, XmNforeground, black_c.pixel, XmNbackground, yellow_c.pixel, NULL); XtVaSetValues(gray_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(draw_a, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); XtVaSetValues(ltgray_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); /* have set color pallett */ printf("color pallett set \n"); gc_fill = XCreateGC(XtDisplay(draw_a), XtWindow(draw_a), 0, &xgcvalues); gc_line = XCreateGC(XtDisplay(draw_a), XtWindow(draw_a), 0, &xgcvalues); gc_grid = XCreateGC(XtDisplay(draw_a), XtWindow(draw_a), 0, &xgcvalues); gc_rubber = XDefaultGC(display, screen_num); gc_frame = XDefaultGC(display, screen_num); gc_move = XDefaultGC(display, screen_num); fillback = FILLCOLOR; /* Establish a passive grab, for any button press. Force the mouse cursor to stay within the canvas window, and change the mouse cursor to a cross_hair. */ XGrabButton ( XtDisplay ( draw_a ), AnyButton, AnyModifier, XtWindow ( draw_a ), TRUE, ButtonPressMask | ButtonMotionMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, XtWindow ( draw_a ), XCreateFontCursor (XtDisplay(draw_a), XC_crosshair)); /* Create the GC used by the rubber banding functions. */ XtVaGetValues ( draw_a, XmNforeground, &xgcvalues.foreground, XmNbackground, &xgcvalues.background, NULL ); /* Set the foreground color to the XOR of the foreground and background colors, so that if an image is drawn on the background using this GC, it will be displayed as the foreground color, and vice-versa. */ xgcvalues.foreground = xgcvalues.foreground ^ xgcvalues.background; xgcvalues.background = xgcvalues.foreground; /* Set the rubber band gc to use XOR mode and draw dashed line. */ xgcvalues.line_style = LineOnOffDash; xgcvalues.function = GXxor; gc_rubber = XtGetGC ( draw_a, GCForeground | GCBackground | GCFunction | GCLineStyle, &xgcvalues ); gc_frame = XtGetGC ( draw_a, GCForeground | GCBackground | GCFunction | GCLineStyle, &xgcvalues ); gc_move = XtGetGC ( draw_a, GCForeground | GCBackground | GCFunction | GCLineStyle, &xgcvalues ); /* initial conditions */ printf("setting initial conditions \n"); draw_a_bg = white_c.pixel; /* send message to OODE */ set_background_color(white_c); fillback = FILLCOLOR; XtVaSetValues(back_color_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); XSetBackground(display, gc_fill, white_c.pixel); XSetLineAttributes(display, gc_line, 1, 0, 2, 1); XSetBackground(display, gc_line, white_c.pixel); XSetBackground(display, gc_grid, white_c.pixel); XSetForeground(display, gc_grid, black_c.pixel); XtVaSetValues(draw_a, XmNbackground, white_c.pixel, NULL); gc_text = XCreateGC(XtDisplay(draw_a), XtWindow(draw_a), 0, &xgcvalues); XSetBackground(display, gc_text, white_c.pixel); XSetForeground(display, gc_text, black_c.pixel); font12 = XLoadFont(display, "-*-courier-medium-r-normal-*-*-120-75-*"); font14 = XLoadFont(display, "-*-courier-medium-r-normal-*-*-140-75-*"); font18 = XLoadFont(display, "-*-courier-medium-r-normal-*-*-180-75-*"); font24 = XLoadFont(display, "-*-courier-medium-r-normal-*-*-240-75-*"); XtVaSetValues(fill_color_b, XmNforeground, black_c.pixel, XmNbackground, green_c.pixel, NULL); XSetForeground(display, gc_fill, green_c.pixel); /* send message to OODE */ set_fill_color(green_c); XtVaSetValues(line_color_b, XmNforeground, white_c.pixel, XmNbackground, black_c.pixel, NULL); XSetForeground(display, gc_line, black_c.pixel); /* send message to OODE */ set_line_color(black_c); action = RECTANGLE; /* send message to OODE */ set_action(RECTANGLE); clear_color(); XtVaSetValues(rect_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); filled_flag = False; /* send message to OODE */ set_filled(0); XtVaSetValues(unfilled_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); XtVaSetValues(filled_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XmScaleSetValue(scale_w, 100); { int vvalue, vssize, vinc, vpinc; XmScrollBarGetValues(vertical_bar, &vvalue, &vssize, &vinc, &vpinc); XmScrollBarSetValues(vertical_bar, 40, vssize, vinc, vpinc, False); XmScrollBarSetValues(horizontal_bar, 40, vssize, vinc, vpinc, False); } frame_auto = 1; /* over ridden by user frame action */ grid_spacing=20; strcpy(root_file_name, "draw"); XmListAddItem(draw_name, XMS(root_file_name), 1); sel_area_xmin=0; sel_area_ymin=0; sel_area_xmax=0; sel_area_xmax=0; printf("draw_gui waiting for quit \n"); XtAppMainLoop(app_context); } /* end draw_gui */ /* callbacks */ /* secondary callback from Quit dialog box, q_dialog */ void ReallyQuitCB(Widget w, XtPointer clientdata, XtPointer calldata) { exit(EXIT_SUCCESS); } /* end ReallyQuitCB */ /* prompt callback from "enter text" box, text_name_dia */ void DoText_string(Widget w, enum Choice client_data, XmAnyCallbackStruct * call_data) { char * s; XmSelectionBoxCallbackStruct * selection; if(client_data == OK) { selection=(XmSelectionBoxCallbackStruct *)call_data; XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &s); printf("user enetered text =%s \n", s); strcpy(msg, s); /* global */ XtFree(s); create_text(xg0, yg0, xg1, yg1, msg, font_size); } } /* end DoText_string */ void DoSetFile_name(Widget w, enum Choice client_data, XmAnyCallbackStruct * call_data) { char * s; XmSelectionBoxCallbackStruct * selection; if(client_data == OK) { selection=(XmSelectionBoxCallbackStruct *)call_data; XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &s); printf("user enetered file name =%s \n", s); strcpy(root_file_name, s); /* global */ XtFree(s); XmListAddItem(draw_name, XMS(root_file_name), 1); } } /* end DoSetFile_name */ void DoOpen(Widget w, enum Choice client_data, XmAnyCallbackStruct * call_data) { char * s; char * p; XmFileSelectionBoxCallbackStruct * selection; if(client_data == OK) { selection=(XmFileSelectionBoxCallbackStruct *)call_data; XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &s); printf("user enetered file name =%s \n", s); strcpy(save_file_name, s); /* global */ XtFree(s); strcpy(root_file_name, save_file_name); p = strchr(root_file_name, '.'); if(p != NULL) *p = (char)0; printf("root file name =%s \n", root_file_name); XmListAddItem(draw_name, XMS(root_file_name), 1); set_action(RESTORE); } XtUnmanageChild(open_dialog); } /* end DoOpen */ /* file_CB - callback sends message Clear, Save or Quit*/ void file_CB(Widget w, int index, XmAnyCallbackStruct *call_data) { enum {Clear, Open_draw, New_file, Save_draw, Save_gif, Save_xbm, Save_bwjpeg, Save_coljpeg, Quit}; printf("file_cb index=%d \n", index); switch (index) { case Clear: set_action(CLEAR); strcpy(root_file_name, "draw"); XmListAddItem(draw_name, XMS(root_file_name), 1); break; case Open_draw: XtManageChild(open_dialog); /* activate prompt box */ break; case New_file: printf("managing file_name_dia \n"); XtManageChild(file_name_dia); /* activate prompt box */ break; case Save_draw: strcpy(save_file_name, root_file_name); strcat(save_file_name, ".draw"); set_action(SAVE); break; case Save_gif: strcpy(save_file_name, root_file_name); strcat(save_file_name, ".gif"); write_gif_file(save_file_name); break; case Save_xbm: strcpy(save_file_name, root_file_name); strcat(save_file_name, ".xbm"); write_xbm_file(save_file_name); break; case Save_bwjpeg: /* still to implement */ strcpy(save_file_name, root_file_name); strcat(save_file_name, ".jpg"); write_bwjpg_file(save_file_name); break; case Save_coljpeg: /* still to implement */ strcpy(save_file_name, root_file_name); strcat(save_file_name, ".jpg"); write_coljpg_file(save_file_name); break; case Quit: XtManageChild(q_dialog); break; } } /* end file_CB */ /* edit_CB - callback sends message SelectAll, DeselectAll, Delete*/ void edit_CB(Widget w, int index, XmAnyCallbackStruct *call_data) { enum {SelectAll, DeSelectAll, Delete, Cut, Copy, Paste}; switch (index) { case SelectAll: set_action(SELECTALL); break; case DeSelectAll: set_action(DESELECTALL); break; case Delete: set_action(DELETE); break; case Cut: set_action(CUT); break; case Copy: set_action(COPY); break; case Paste: set_action(PASTE); break; } } /* end edit_CB */ /* arrange_CB - callback sends message Group, UnGroup, MoveToFront, MoveToBack*/ void arrange_CB(Widget w, int index, XmAnyCallbackStruct *call_data) { enum {Group, UnGroup, MoveToFront, MoveToBack}; switch (index) { case Group: set_action(GROUP); break; case UnGroup: set_action(UNGROUP); break; case MoveToFront: set_action(MOVETOFRONT); break; case MoveToBack: set_action(MOVETOBACK); break; } } /* end arrange_CB */ /* pen_CB - callback sends message Pixel1, Pixel2, Pixel3, Pixel4*/ void pen_CB(Widget w, int index, XmAnyCallbackStruct *call_data) { enum {Pixel1, Pixel2, Pixel3, Pixel4, Pixelinh}; unsigned int line_width; int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ switch (index) { case Pixel1: set_action(PIXEL1); line_width = 1; XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); break; case Pixel2: set_action(PIXEL2); line_width = 2; XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); break; case Pixel3: set_action(PIXEL3); line_width = 3; XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); break; case Pixel4: set_action(PIXEL4); line_width = 4; XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); case Pixelinh: set_action(PIXELI); line_width = 10; /* fix?? must look up from parent */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); break; } } /* end pen_CB */ /* font_size_CB - callback sends message ? */ void font_size_CB(Widget w, int index, XmAnyCallbackStruct *call_data) { printf("font_size_CB index = %d \n", index); switch (index) { case 0: font_size = 12; XSetFont(display, gc_text, font12); break; case 1: font_size = 14; XSetFont(display, gc_text, font14); break; case 2: font_size = 18; XSetFont(display, gc_text, font18); break; case 3: font_size = 24; XSetFont(display, gc_text, font24); break; } } /* end font_size_CB */ /* color_CB - callback sets one of LINECOLOR, FILLCOLOR or BACKCOLOR */ void color_CB(Widget w, int index, XmAnyCallbackStruct *call_data) { switch (index) { case WHITE: set_color( white_c, black_c); break; case BLACK: set_color( black_c, white_c); break; case RED: set_color( red_c, white_c); break; case GREEN: set_color( green_c, black_c); break; case BLUE: set_color( blue_c, white_c); break; case VIOLET: set_color( violet_c, black_c); break; case ORANGE: set_color( orange_c, black_c); break; case YELLOW: set_color( yellow_c, black_c); break; case GRAY: set_color( gray_c, black_c); break; case LTGRAY: set_color( gray_c, black_c); break; } } /* action_CB - callback sets flags or performs actions (side_row) */ void action_CB(Widget w, int index, XmAnyCallbackStruct *call_data) { int slide_size, incr, page_incr; switch (index) { case CLEAR: /* send message to OODE */ set_action(CLEAR); break; case EXIT: exit(EXIT_SUCCESS); break; case DELETE: /* send message to OODE */ set_action(DELETE); break; case FRAME: action = FRAME; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(frame_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case SELECT: action = SELECT; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(select_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case SELECTAREA: action = SELECTAREA; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(selecta_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case TEXT: action = TEXT; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(text_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case LINE: action = LINE; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(line_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case ELLIPSE: action = ELLIPSE; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(ellipse_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case CIRCLE: action = CIRCLE; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(circle_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case RECTANGLE: action = RECTANGLE; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(rect_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case RECTANGLEC: action = RECTANGLEC; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(rectc_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case LINECOLOR: fillback = LINECOLOR; break; case FILLCOLOR: fillback = FILLCOLOR; break; case BACKCOLOR: fillback = BACKCOLOR; break; case FILLED: filled_flag = True; /* send message to OODE */ set_filled(1); XtVaSetValues(filled_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); XtVaSetValues(unfilled_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); break; case UNFILLED: filled_flag = False; /* send message to OODE */ set_filled(0); XtVaSetValues(unfilled_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); XtVaSetValues(filled_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); break; case ZOOM: XmScaleGetValue(w, &scale_at); scale = (float)scale_at/100.0; /* 1/4 to 2 */ set_action(ZOOM); redraw(); break; case XOFF: XmScrollBarGetValues(w, &xoff_at, &slide_size, &incr, &page_incr); xoff_at +=10; /* center at 50 when width of bar = 20 */ xoff_at =(int)( (((xoff_at - 50)*xmid)/20.0)*scale ); /* printf("final xoff_at=%d\n", xoff_at); */ set_action(XOFF); /* xmid typically 400 */ redraw(); /* scale goes 1/4 to 2 */ break; /* xoff_at goes -2 to +2 times xmid*scale */ case YOFF: XmScrollBarGetValues(w, &yoff_at, &slide_size, &incr, &page_incr); yoff_at +=10; /* center at 50 when slide_size = 20 */ yoff_at = (int)( (((yoff_at - 50)*ymid)/20.0)*scale ); /* printf("final yoff_at=%d\n", yoff_at); */ set_action(YOFF); redraw(); break; } } /* expose_CB - redraw contents of drawing area when exposed */ void expose_CB(Widget w, XtPointer users, XmAnyCallbackStruct *call_data) { /* send expose message to draw object */ redraw(); } /* set_color - save color for modal buttons and fix color on buttons */ static void set_color(XColor fore, XColor back) { if (fillback == BACKCOLOR) { XtVaSetValues(back_color_b, XmNforeground, back.pixel, XmNbackground, fore.pixel, NULL); XSetBackground(display, gc_fill, fore.pixel); XSetBackground(display, gc_line, fore.pixel); draw_a_bg = fore.pixel; XtVaSetValues(draw_a, XmNbackground, draw_a_bg, NULL); /* send expose message to draw object */ set_background_color(fore); redraw(); } else if (fillback == FILLCOLOR) { XtVaSetValues(fill_color_b, XmNforeground, back.pixel, XmNbackground, fore.pixel, NULL); XSetForeground(display, gc_fill, fore.pixel); /* send message to OODE */ set_fill_color(fore); /* may be "parent color" */ redraw(); } else if (fillback == LINECOLOR) { XtVaSetValues(line_color_b, XmNforeground, back.pixel, XmNbackground, fore.pixel, NULL); XSetForeground(display, gc_line, fore.pixel); /* send message to OODE */ set_line_color(fore); /* may be "parent color" */ redraw(); } } /* utility functions */ /* StartRubberBand - from Young's book, modified for project 2, then proj 3 */ /* then proj 4, then proj 5 */ /* reacts to button press in drawing area */ static void StartRubberBand ( Widget w, XtPointer clientData, XEvent *event, Boolean *flag ) { midX = startX; /* for 3 point symbols, will be x2, y2 */ midY = startY; startX = event->xbutton.x; startY = event->xbutton.y; if(grid_snap_on) grid_snap(&startX, &startY); lastX = startX; lastY = startY; last_click((int)((startX-xmid)/scale+xoff_at), (int)((startY-ymid)/scale+yoff_at), gray_c); obj_kind = 99; now_button = event->xbutton.button; /* get back objects kind */ if (event->xbutton.button == 1) { /* doing resize, return obj_kind only id of handle */ find_kind((int)((startX-xmid)/scale+xoff_at), (int)((startY-ymid)/scale+yoff_at), &obj_kind); } printf("button down B,startX,startY = %d %d,%d \n", event->xbutton.button, startX, startY); } /* TrackRubberBand - from Young's book, modified for project 2 */ /* reacts to mouse motion in drawing area */ static void TrackRubberBand ( Widget w, XtPointer clientData, XEvent *event, Boolean *flag ) { /* not button 1 is move object */ if (event->xbutton.button == 2 || event->xbutton.button == 3 || obj_kind == TEXT || obj_kind == LINE || obj_kind == RECTANGLE || obj_kind == RECTANGLEC || obj_kind == ELLIPSE || obj_kind == CIRCLE || obj_kind == GROUP || obj_kind == SELECTAREA) { /* rubber banding of moving object */ if (lastX == event->xbutton.x && lastY == event->xbutton.y) return; XDrawLine ( XtDisplay ( w ), XtWindow ( w ), gc_rubber, startX, startY, lastX, lastY ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); XDrawLine ( XtDisplay ( w ), XtWindow ( w ), gc_rubber, startX, startY, lastX, lastY ); XDrawLine ( XtDisplay ( w ), XtWindow ( w ), gc_rubber, startX, startY, lastX, lastY ); XFlush(XtDisplay ( w )); /* printf("track button %d %d,%d %d,%d \n", (int)(event->xbutton.button), startX, startY, lastX, lastY); */ return; } /* button 1 motion thus outline obj_kind object */ if (action == TEXT || /* text and all symbols, no rubber shape */ action == VMUX || action == HMUX || action == ALU || action == AND || action == OR || action == XOR || action == NOT || action == COMP || action == ARROW || action == ARC || action == DOT || action == HRES || action == VRES || action == HCAP || action == VCAP || action == HIND || action == VIND) { /* Draw once to clear the previous rectangle. */ XDrawRectangle( XtDisplay ( w ), XtWindow ( w ), gc_rubber, min(startX,lastX), min(startY,lastY), abs(lastX-startX), abs(lastY-startY) ); /* Update the endpoints. */ lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); XDrawRectangle( XtDisplay ( w ), XtWindow ( w ), gc_rubber, min(startX,lastX), min(startY,lastY), abs(lastX-startX), abs(lastY-startY) ); } if (action == LINE) { /* Draw once to clear the previous line. */ XDrawLine ( XtDisplay ( w ), XtWindow ( w ), gc_rubber, startX, startY, lastX, lastY ); /* Update the endpoints. */ lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); /* Draw the new line. */ XDrawLine ( XtDisplay ( w ), XtWindow ( w ), gc_rubber, startX, startY, lastX, lastY ); } else if (action == ELLIPSE) { XDrawArc( XtDisplay ( w ), XtWindow ( w ), gc_rubber, min(startX,lastX), min(startY,lastY), abs(lastX-startX), abs(lastY-startY), 0, 64*360 ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); XDrawArc( XtDisplay ( w ), XtWindow ( w ), gc_rubber, min(startX,lastX), min(startY,lastY), abs(lastX-startX), abs(lastY-startY), 0, 64*360 ); } else if (action == CIRCLE) { dr=(int)sqrt((double)((startX-lastX)*(startX-lastX)+ (startY-lastY)*(startY-lastY))); XDrawArc( XtDisplay ( w ), XtWindow ( w ), gc_rubber, startX-dr, startY-dr, 2*dr, 2*dr, 0, 64*360 ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); dr=(int)sqrt((double)((startX-lastX)*(startX-lastX)+ (startY-lastY)*(startY-lastY))); XDrawArc( XtDisplay ( w ), XtWindow ( w ), gc_rubber, startX-dr, startY-dr, 2*dr, 2*dr, 0, 64*360 ); } else if (action == RECTANGLE || action == FRAME || action == SELECTAREA ) { XDrawRectangle( XtDisplay ( w ), XtWindow ( w ), gc_rubber, min(startX,lastX), min(startY,lastY), abs(lastX-startX), abs(lastY-startY) ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); XDrawRectangle( XtDisplay ( w ), XtWindow ( w ), gc_rubber, min(startX,lastX), min(startY,lastY), abs(lastX-startX), abs(lastY-startY) ); } else if (action == RECTANGLEC) { dx = abs(startX-lastX); dy = abs(startY-lastY); XDrawRectangle( XtDisplay ( w ), XtWindow ( w ), gc_rubber, startX-dx, startY-dy, 2*dx, 2*dy ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); dx = abs(startX-lastX); dy = abs(startY-lastY); XDrawRectangle( XtDisplay ( w ), XtWindow ( w ), gc_rubber, startX-dx, startY-dy, 2*dx, 2*dy ); } } /* EndRubberBand - from Young's book, modified for project 2 */ /* reacts to button release in drawing area */ static void EndRubberBand ( Widget w, XtPointer clientData, XEvent *event, Boolean *flag ) { /* move object, no select required, all objects under first point move */ if (event->xbutton.button == 2 || event->xbutton.button == 3) { /* object move end point */ XDrawLine ( XtDisplay ( w ), XtWindow ( w ), gc_rubber, startX, startY, lastX, lastY ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); move_to((int)((startX-xmid)/scale+xoff_at), /* runs display list */ (int)((startY-ymid)/scale+yoff_at), (int)((lastX -xmid)/scale+xoff_at), (int)((lastY -ymid)/scale+yoff_at)); return; } if (obj_kind == TEXT || obj_kind == LINE || obj_kind == RECTANGLE || obj_kind == RECTANGLEC || obj_kind == ELLIPSE || obj_kind == CIRCLE || obj_kind == GROUP || obj_kind == SELECTAREA) { /* selected resize on handle */ XDrawLine ( XtDisplay ( w ), XtWindow ( w ), gc_rubber, startX, startY, lastX, lastY ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); do_resize((int)((startX-xmid)/scale+xoff_at), /* runs display list */ (int)((startY-ymid)/scale+yoff_at), (int)((lastX -xmid)/scale+xoff_at), (int)((lastY -ymid)/scale+yoff_at)); return; } if (action == SELECTAREA) { XDrawRectangle( XtDisplay ( w ), XtWindow ( w ), gc_rubber, min(startX,lastX), min(startY,lastY), abs(lastX-startX), abs(lastY-startY) ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); xg0 = (int)((startX-xmid)/scale+xoff_at); yg0 = (int)((startY-ymid)/scale+yoff_at); xg1 = (int)((lastX -xmid)/scale+xoff_at); yg1 = (int)((lastY -ymid)/scale+yoff_at); sel_area_xmin=min(xg0,xg1); sel_area_xmax=max(xg0,xg1); sel_area_ymin=min(yg0,yg1); sel_area_ymax=max(yg0,yg1); printf("sel_area_xmin=%d, sel_area_ymin=%d, sel_area_xmax=%d, sel_area_ymax=%d\n", sel_area_xmin, sel_area_ymin, sel_area_xmax, sel_area_ymax); selectarea(); redraw(); sel_area_xmin=0; sel_area_ymin=0; sel_area_xmax=0; sel_area_xmax=0; } else if (action == FRAME) { XDrawRectangle( XtDisplay ( w ), XtWindow ( w ), gc_rubber, min(startX,lastX), min(startY,lastY), abs(lastX-startX), abs(lastY-startY) ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); frame_xmin=min(startX,lastX); frame_xmax=max(startX,lastX); frame_ymin=min(startY,lastY); frame_ymax=max(startY,lastY); printf("frame_xmin=%d, frame_ymin=%d, frame_xmax=%d, frame_ymax=%d\n", frame_xmin, frame_ymin, frame_xmax, frame_ymax); frame_auto=0; xg0 = (int)((startX-xmid)/scale+xoff_at); yg0 = (int)((startY-ymid)/scale+yoff_at); xg1 = (int)((lastX -xmid)/scale+xoff_at); yg1 = (int)((lastY -ymid)/scale+yoff_at); xg2 = (int)((midX -xmid)/scale+xoff_at); yg2 = (int)((midY -ymid)/scale+yoff_at); create_symbol(xg0, yg0, xg1, yg1, xg2, yg2, grid_spacing, (int)FRAME); } else if (action == SELECT) { lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); aselect((int)((lastX -xmid)/scale+xoff_at), (int)((lastY -ymid)/scale+yoff_at)); } else if (action == TEXT) { /* Clear the current rubber band box */ XDrawRectangle( XtDisplay ( w ), XtWindow ( w ), gc_rubber, min(startX,lastX), min(startY,lastY), abs(lastX-startX), abs(lastY-startY) ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); xg0 = (int)((startX-xmid)/scale+xoff_at); yg0 = (int)((startY-ymid)/scale+yoff_at); xg1 = (int)((lastX -xmid)/scale+xoff_at); yg1 = (int)((lastY -ymid)/scale+yoff_at); XtManageChild(text_name_dia); /* activate prompt box */ /* create_text done in prompt callback when data available */ } else if (action == LINE) { /* Clear the current line and update the endpoint info. */ XDrawLine ( XtDisplay ( w ), XtWindow ( w ), gc_rubber, startX, startY, lastX, lastY ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); xg0 = (int)((startX-xmid)/scale+xoff_at); yg0 = (int)((startY-ymid)/scale+yoff_at); xg1 = (int)((lastX -xmid)/scale+xoff_at); yg1 = (int)((lastY -ymid)/scale+yoff_at); create_line(xg0, yg0, xg1, yg1); } else if (action == ELLIPSE) { XDrawArc( XtDisplay ( w ), XtWindow ( w ), gc_rubber, min(startX,lastX), min(startY,lastY), abs(lastX-startX), abs(lastY-startY), 0, 64*360 ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); xg0 = (int)((startX-xmid)/scale+xoff_at); yg0 = (int)((startY-ymid)/scale+yoff_at); xg1 = (int)((lastX -xmid)/scale+xoff_at); yg1 = (int)((lastY -ymid)/scale+yoff_at); create_ellipse(min(xg0,xg1), min(yg0,yg1), (int)(abs((lastX-startX)/scale)), (int)(abs((lastY-startY)/scale))); } else if (action == CIRCLE) { dr=(int)sqrt((double)((startX-lastX)*(startX-lastX)+ (startY-lastY)*(startY-lastY))); XDrawArc( XtDisplay ( w ), XtWindow ( w ), gc_rubber, startX-dr, startY-dr, 2*dr, 2*dr, 0, 64*360 ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); xg0 = (int)((startX-xmid)/scale+xoff_at); yg0 = (int)((startY-ymid)/scale+yoff_at); xgr = (int)(sqrt(pow((double)((lastX-startX)/scale),2.0)+ pow((double)((lastY-startY)/scale),2.0))); create_circle(xg0, yg0, xgr); } else if (action == RECTANGLE) { XDrawRectangle( XtDisplay ( w ), XtWindow ( w ), gc_rubber, min(startX,lastX), min(startY,lastY), abs(lastX-startX), abs(lastY-startY) ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); xg0 = (int)((startX-xmid)/scale+xoff_at); yg0 = (int)((startY-ymid)/scale+yoff_at); xg1 = (int)((lastX -xmid)/scale+xoff_at); yg1 = (int)((lastY -ymid)/scale+yoff_at); create_rectangle(min(xg0,xg1), min(yg0,yg1), (int)(abs((lastX-startX)/scale)), (int)(abs((lastY-startY)/scale))); } else if (action == RECTANGLEC) { dx = abs(startX-lastX)/2; dy = abs(startY-lastY)/2; XDrawRectangle( XtDisplay ( w ), XtWindow ( w ), gc_rubber, min(startX,lastX)-dx, min(startY,lastY)-dy, abs(lastX-startX), abs(lastY-startY) ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); xg0 = (int)((startX-xmid)/scale+xoff_at); yg0 = (int)((startY-ymid)/scale+yoff_at); create_rectanglec(xg0, yg0, 2*(int)(abs((lastX-startX)/scale)), 2*(int)(abs((lastY-startY)/scale))); } else if (action == VMUX || action == HMUX || action == ALU || action == AND || action == OR || action == XOR || action == NOT || action == COMP || action == ARROW || action == ARC || action == DOT || action == HRES || action == VRES || action == HCAP || action == VCAP || action == HIND || action == VIND) { XDrawRectangle( XtDisplay ( w ), XtWindow ( w ), gc_rubber, min(startX,lastX), min(startY,lastY), abs(lastX-startX), abs(lastY-startY) ); lastX = event->xbutton.x; lastY = event->xbutton.y; if(grid_snap_on) grid_snap(&lastX, &lastY); xg0 = (int)((startX-xmid)/scale+xoff_at); yg0 = (int)((startY-ymid)/scale+yoff_at); xg1 = (int)((lastX -xmid)/scale+xoff_at); yg1 = (int)((lastY -ymid)/scale+yoff_at); xg2 = (int)((midX -xmid)/scale+xoff_at); yg2 = (int)((midY -ymid)/scale+yoff_at); /* create_symbol(min(xg0,xg1), min(yg0,yg1), max(xg0,xg1), max(yg0,yg1), xg2, yg2, grid_spacing, (int)action); */ create_symbol(xg0, yg0, xg1, yg1, xg2, yg2, grid_spacing, (int)action); } } /* end EndRubberBand callback */ void grid_CB(Widget w, int index, XmAnyCallbackStruct *call_data) { switch (index) { case 0: /* grid off */ grid_displayed=0; /* redraw happends because of menu pop */ grid_snap_on = 0; break; case 1: /* display grid */ grid_displayed = 1; break; case 2: /* snap on */ grid_snap_on = 1; grid_displayed=1; break; case 3: /* snap off */ grid_snap_on = 0; break; case 4: /* spacing 5 */ grid_spacing = 5; break; case 5: /* spacing 10 */ grid_spacing = 10; break; case 6: /* spacing 15 */ grid_spacing = 15; break; case 7: /* spacing 20 */ grid_spacing = 20; break; case 8: /* spacing 30 */ grid_spacing = 30; break; case 9: /* spacing 40 */ grid_spacing = 40; break; } redraw(); } /* end grid_CB */ void symbol_CB(Widget w, int index, XmAnyCallbackStruct *call_data) { switch (index) { case VMUX: action = VMUX; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(vmux_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case HMUX: action = HMUX; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(hmux_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case ALU: action = ALU; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(alu_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case AND: action = AND; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(and_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case OR: action = OR; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(or_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case XOR: action = XOR; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(xor_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case NOT: /* draw inverter */ action = NOT; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(not_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case COMP: /* complement bubble for input or output */ action = COMP; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(comp_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case ARROW: /* rotates */ action = ARROW; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(arrow_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case ARC: /* 3pt arc */ action = ARC; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(arc_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case DOT: /* connection dot */ action = DOT; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(dot_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case HRES: /* horizontal resistor */ action = HRES; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(hres_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case VRES: /* vertical resistor */ action = VRES; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(vres_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case HCAP: /* horizontal capacitor */ action = HCAP; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(hcap_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case VCAP: /* vertical capacitor */ action = VCAP; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(vcap_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case HIND: /* horizontal inductor */ action = HIND; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(hind_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; case VIND: /* vertical inductor */ action = VIND; /* send message to OODE */ set_action(action); clear_color(); XtVaSetValues(vind_b, XmNforeground, black_c.pixel, XmNbackground, white_c.pixel, NULL); break; } } /* end symbol_CB */ /* functions callable from object oriented C++ Draw Editor */ void draw_clear(XColor background_color) { XClearArea(XtDisplay(draw_a), XtWindow(draw_a), 0, 0, 0, 0, False); XtVaSetValues(draw_a, XmNbackground, background_color.pixel, NULL); } void draw_text(int xg0, int yg0, int xg1, int yg1, char * msg, XColor line_color, int siz) { XSetForeground(display, gc_text, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); /* may show box */ y1 = (int)((yg1-yoff_at)*scale+ymid); switch(siz) { case 12: XSetFont(display, gc_text, font12); break; case 14: XSetFont(display, gc_text, font14); break; case 18: XSetFont(display, gc_text, font18); break; case 24: XSetFont(display, gc_text, font24); break; } XDrawImageString(display, XtWindow ( draw_a ), gc_text, x0, y0, msg, strlen(msg)); } /* end draw_text */ void draw_vmux(int xg0, int yg0, int xg1, int yg1, int hg, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ int h; /* half height (width) */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); y1 = (int)((yg1-yoff_at)*scale+ymid); h = (int)(hg*scale); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-h, y0, x0-h, y1); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+h, y0, x0+h, y1); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-h, y0-h, 2*h, 2*h, 64*0, 64*180 ); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-h, y1-h, 2*h, 2*h, 64*0, -64*180 ); } /* end draw_vmux */ void draw_hmux(int xg0, int yg0, int xg1, int yg1, int hg, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ int h; /* half height */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); y1 = (int)((yg1-yoff_at)*scale+ymid); h = (int)(hg*scale); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0-h, x1, y0-h); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0+h, x1, y0+h); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-h, y0-h, 2*h, 2*h, 64*90, 64*180 ); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-h, y0-h, 2*h, 2*h, -64*90, -64*180 ); XDrawArc(display, XtWindow ( draw_a ), gc_line, x1-h, y0-h, 2*h, 2*h, 64*0, -64*90 ); XDrawArc(display, XtWindow ( draw_a ), gc_line, x1-h, y0-h, 2*h, 2*h, 64*0, 64*90 ); } /* end draw_hmux */ void draw_alu(int xg0, int yg0, int xg1, int yg1, int sg, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ int s; /* side unit */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); y1 = (int)((yg1-yoff_at)*scale+ymid); s = (int)(sg*scale); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-s, y0-3*s, x0+s, y0-s); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+s, y0-s, x0+s, y0+s); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+s, y0+s, x0-s, y0+3*s); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-s, y0+3*s, x0-s, y0+s); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-s, y0+s, x0, y0); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0-s, y0-s); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-s, y0-s, x0-s, y0-3*s); } /* end draw_alu */ void draw_and(int xg0, int yg0, int xg1, int yg1, int hg, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ int h; /* half height */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); y1 = (int)((yg1-yoff_at)*scale+ymid); h = (int)(hg*scale); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0+h, y0); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y1, x0+h, y1); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0, y1); if(y0y0) { dr = y1-y0; XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-dr/15, y0, x0+h-dr/15, y0); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-dr/15, y1, x0+h-dr/15, y1); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-(60*dr)/15, (y0+y1)/2-2*dr, 4*dr, 4*dr, -64*14, 64*28 ); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0+h-(16*dr)/15, y0, 2*dr, 2*dr, 64*30, 64*60 ); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0+h-(16*dr)/15, y0-dr, 2*dr, 2*dr, -64*30, -64*60 ); } else { dr = y0-y1; XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-dr/15, y0, x0+h-dr/15, y0); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-dr/15, y1, x0+h-dr/15, y1); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-(60*dr)/15, (y0+y1)/2-2*dr, 4*dr, 4*dr, -64*14, 64*28 ); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0+h-(16*dr)/15, y1, 2*dr, 2*dr, 64*30, 64*60 ); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0+h-(16*dr)/15, y1-dr, 2*dr, 2*dr, -64*30, -64*60 ); } } /* end draw_or */ void draw_xor(int xg0, int yg0, int xg1, int yg1, int hg, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ int h; /* half height */ int dr; XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); y1 = (int)((yg1-yoff_at)*scale+ymid); h = (int)(hg*scale); if(y1>y0) { dr = y1-y0; XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-dr/15, y0, x0+h-dr/15, y0); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-dr/15, y1, x0+h-dr/15, y1); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-(60*dr)/15, (y0+y1)/2-2*dr, 4*dr, 4*dr, -64*14, 64*28 ); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-(60*dr)/15-h, (y0+y1)/2-2*dr, 4*dr, 4*dr, -64*14, 64*28 ); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0+h-(16*dr)/15, y0, 2*dr, 2*dr, 64*30, 64*60 ); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0+h-(16*dr)/15, y0-dr, 2*dr, 2*dr, -64*30, -64*60 ); } else { dr = y0-y1; XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-dr/15, y0, x0+h-dr/15, y0); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-dr/15, y1, x0+h-dr/15, y1); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-(60*dr)/15, (y0+y1)/2-2*dr, 4*dr, 4*dr, -64*14, 64*28 ); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-(60*dr)/15-h, (y0+y1)/2-2*dr, 4*dr, 4*dr, -64*14, 64*28 ); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0+h-(16*dr)/15, y1, 2*dr, 2*dr, 64*30, 64*60 ); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0+h-(16*dr)/15, y1-dr, 2*dr, 2*dr, -64*30, -64*60 ); } } /* end draw_xor */ void draw_not(int xg0, int yg0, int xg1, int yg1, int hg, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ int h; /* half height */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); y1 = (int)((yg1-yoff_at)*scale+ymid); h = (int)(hg*scale); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0, y1); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x1, (y0+y1)/2); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y1, x1, (y0+y1)/2); } /* end draw_not */ void draw_comp(int xg0, int yg0, int xg1, int yg1, int hg, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ int h; /* half height */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); y1 = (int)((yg1-yoff_at)*scale+ymid); h = (int)(hg*scale); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0, y0-h/2, h, h, 64*0, 64*360 ); } /* end draw_comp */ void draw_arrow(int xg0, int yg0, int xg1, int yg1, int hg, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ int h; /* half height */ int dx, dy, usex; /* use biggest to get direction */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); /* "point" start */ y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); /* "back" direction */ y1 = (int)((yg1-yoff_at)*scale+ymid); h = (int)(hg*scale); dx = x0-x1; dy = y0-y1; usex=1; if(iabs(dx)<=iabs(dy))usex=0; if(usex) /* horizontal */ { if(dx>0) /* +x pointing */ { XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0-(81*h)/100, y0+h/4); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0-(81*h)/100, y0-h/4); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-h/4, y0, x0-(81*h)/100, y0+h/4); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-h/4, y0, x0-(81*h)/100, y0-h/4); } else /* -x pointing */ { XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0+(81*h)/100, y0+h/4); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0+(81*h)/100, y0-h/4); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+h/4, y0, x0+(81*h)/100, y0+h/4); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+h/4, y0, x0+(81*h)/100, y0-h/4); } } else /* vertical */ { if(dy>0) /* +y pointing */ { XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0+h/4, y0-(81*h)/100); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0-h/4, y0-(81*h)/100); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0-h/4, x0+h/4, y0-(81*h)/100); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0-h/4, x0-h/4, y0-(81*h)/100); } else /* -y pointing */ { XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0+h/4, y0+(81*h)/100); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0-h/4, y0+(81*h)/100); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0+h/4, x0+h/4, y0+(81*h)/100); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0+h/4, x0-h/4, y0+(81*h)/100); } } } /* end draw_arrow */ void draw_hres(int xg0, int yg0, int xg1, int yg1, int hg, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ int dx, h; XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); y1 = (int)((yg1-yoff_at)*scale+ymid); dx = (x1-x0)/11; h = 2*dx; XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0+3*dx/2, y0); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+3*dx/2, y0, x0+2*dx, y0+h); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+2*dx, y0+h, x0+3*dx, y0-h); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+3*dx, y0-h, x0+4*dx, y0+h); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+4*dx, y0+h, x0+5*dx, y0-h); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+5*dx, y0-h, x0+6*dx, y0+h); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+6*dx, y0+h, x0+7*dx, y0-h); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+7*dx, y0-h, x0+8*dx, y0+h); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+8*dx, y0+h, x0+9*dx, y0-h); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+9*dx, y0-h, x0+10*dx, y0); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+10*dx, y0, x1, y0); } /* end draw_hres */ void draw_vres(int xg0, int yg0, int xg1, int yg1, int hg, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ int dy, h; XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); y1 = (int)((yg1-yoff_at)*scale+ymid); dy = (y1-y0)/11; h = 2*dy; XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0, y0+3*dy/2); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0+3*dy/2, x0+h, y0+2*dy); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+h, y0+2*dy, x0-h, y0+3*dy); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-h, y0+3*dy, x0+h, y0+4*dy); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+h, y0+4*dy, x0-h, y0+5*dy); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-h, y0+5*dy, x0+h, y0+6*dy); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+h, y0+6*dy, x0-h, y0+7*dy); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-h, y0+7*dy, x0+h, y0+8*dy); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+h, y0+8*dy, x0-h, y0+9*dy); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-h, y0+9*dy, x0, y0+10*dy); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0+10*dy, x0, y1); } /* end draw_vres */ void draw_hcap(int xg0, int yg0, int xg1, int yg1, int hg, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ int dx; XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); y1 = (int)((yg1-yoff_at)*scale+ymid); dx = (x1-x0)/9; XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0+4*dx, y0); XDrawLine(display, XtWindow ( draw_a ), gc_line, x1-4*dx, y0, x1, y0); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+4*dx, y0+dx, x0+4*dx, y0-dx); XDrawLine(display, XtWindow ( draw_a ), gc_line, x1-4*dx, y0+dx, x1-4*dx, y0-dx); } /* end draw_hcap */ void draw_vcap(int xg0, int yg0, int xg1, int yg1, int hg, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ int dy; XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); y1 = (int)((yg1-yoff_at)*scale+ymid); dy = (y1-y0)/9; XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0, y0+4*dy); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y1-4*dy, x0, y1); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-dy, y0+4*dy, x0+dy, y0+4*dy); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-dy, y1-4*dy, x0+dy, y1-4*dy); } /* end draw_vcap */ void draw_hind(int xg0, int yg0, int xg1, int yg1, int hg, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ int dx; double x=0.0; double y=0.0; double xn, yn, xs, a, b; double t=0.0; double PI = 3.1415926535897932; double k = 5.31; a = 1.0/(4.0*PI); b = 2.0*a; XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); y1 = (int)((yg1-yoff_at)*scale+ymid); dx = (x1-x0)/10; XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0+dx, y0); XDrawLine(display, XtWindow ( draw_a ), gc_line, x1-dx, y0, x1, y0); while(t<(8.0*PI-PI/8.0)) { t=t+PI/8.0; yn = b-b*cos(t); xs = b*sin(t); xn = 0.75*a*t + xs; XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+dx+(int)(k*dx*x), y0+(int)(k*dx*(y)), x0+dx+(int)(k*dx*xn), y0+(int)(k*dx*(yn))); x = xn; y = yn; } } /* end draw_hind */ void draw_vind(int xg0, int yg0, int xg1, int yg1, int hg, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ int dy; double x=0.0; double y=0.0; double xn, yn, ys, a, b; double t=0.0; double PI = 3.1415926535897932; double k = 5.31; a = 1.0/(4.0*PI); b = 2.0*a; XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); y1 = (int)((yg1-yoff_at)*scale+ymid); dy = (y1-y0)/10; XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x0, y0+dy); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y1-dy, x0, y1); while(t<(8.0*PI-PI/8.0)) { t=t+PI/8.0; xn = b-b*cos(t); ys = b*sin(t); yn = 0.75*a*t + ys; XDrawLine(display, XtWindow ( draw_a ), gc_line, x0+(int)(k*dy*x), y0+dy+(int)(k*dy*y), x0+(int)(k*dy*xn), y0+dy+(int)(k*dy*yn)); x = xn; y = yn; } } /* end draw_vind */ void draw_arc(int xg0, int yg0, int xg1, int yg1, int xg2, int yg2, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ double PI = 3.1415926535897932; double ax0,ay0, ax1,ay1, ax2,ay2; /* three points define the arc */ double aa, ab, ac, ad, ae, af; /* temp values in equation */ double axc, ayc, ar; /* center and radius of circle */ double ax, ay, angle0, angle1, angle2; /* for XDrawArc */ double adet; /* determinant that must be non zero */ double rangle0, rangle1, rangle2; XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); ax0 = (double)((xg0-xoff_at)*scale+xmid); ay0 = (double)((yg0-yoff_at)*scale+ymid); ax2 = (double)((xg1-xoff_at)*scale+xmid); ay2 = (double)((yg1-yoff_at)*scale+ymid); ax1 = (double)((xg2-xoff_at)*scale+xmid); /* mid point is from previous click */ ay1 = (double)((yg2-yoff_at)*scale+ymid); printf("3pt arc raw int %d,%d %d,%d %d,%d\n", xg0, yg0, xg1, yg1, xg2, yg2); printf("3pt arc sca flt %g,%g %g,%g %g,%g\n", ax0, ay0, ax1, ay1, ax2, ay2); /* draw_arc given three points, get center and radius */ /* then x,y, r,r angle1 angle2 for XDrawArc */ /* (x0-xc)^2 + (y0-yc)^2 = (x1-xc)^2 + (y1-yc)^2 */ /* (x1-xc)^2 + (y1-yc)^2 = (x2-xc)^2 + (y2-yc)^2 */ /* solve for xc, solve for yc */ /* r = sqrt( (x1-xc)^2 + (y1-yc)^2 ) */ adet = ax0*ay1+ax1*ay2+ax2*ay0-ax0*ay2-ax1*ay0-ax2*ay1; if(dabs(adet)<0.001) { printf("NOT AN ARC, drawing straight line\n"); XDrawLine(display, XtWindow ( draw_a ), gc_line, (int)ax0, (int)ay0, (int)ax2, (int)ay2); return; } aa = ax0*ax0+ay0*ay0-ax1*ax1-ay1*ay1; ab = 2.0*(ax0-ax1); ac = 2.0*(ay0-ay1); ad = ax1*ax1+ay1*ay1-ax2*ax2-ay2*ay2; ae = 2.0*(ax1-ax2); af = 2.0*(ay1-ay2); printf("aa=%g, ab=%g, ac=%g, ad=%g, ae=%g, af=%g \n", aa, ab, ac, ad, ae, af); /* solve for center and radius */ axc = (af*aa-ac*ad)/(af*ab-ac*ae); ayc = (ae*aa-ab*ad)/(ae*ac-ab*af); ar = sqrt((axc-ax1)*(axc-ax1)+(ayc-ay1)*(ayc-ay1)); printf("axc=%g, ayc=%g, ar=%g \n", axc, ayc, ar); /* convert to XDrawArc system */ ax = axc - ar; ay = ayc - ar; /* compute angle from center to point */ rangle0 = atan2(ay0-ayc,ax0-axc) / PI; /* range -1.0 to 1.0 */ rangle1 = atan2(ay1-ayc,ax1-axc) / PI; rangle2 = atan2(ay2-ayc,ax2-axc) / PI; angle0 = -64.0 * 180.0 * rangle0; angle1 = -64.0 * 180.0 * rangle1; angle2 = -64.0 * 180.0 * rangle2; printf("rangle0=%g, angle0=%g \n", rangle0, angle0/64.0); printf("rangle1=%g, angle2=%g \n", rangle1, angle1/64.0); printf("rangle2=%g, angle2=%g \n", rangle2, angle2/64.0); if(angle0<0.0) angle0=64.0*360.0+angle0; if(angle1<0.0) angle1=64.0*360.0+angle1; if(angle2<0.0) angle2=64.0*360.0+angle2; if(angle01) XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-2, y1-2, 4, 4, 64*0, 64*360 ); /* line width applies */ if(line_width>2) XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-3, y1-3, 6, 6, 64*0, 64*360 ); if(line_width>3) XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-3, y1-3, 6, 6, 64*0, 64*360 ); /* line width applies */ /* bigger than this, use filled_circle */ } /* end draw_dot */ void draw_line_segment(int xg0, int yg0, int xg1, int yg1, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); y1 = (int)((yg1-yoff_at)*scale+ymid); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0, x1, y1); } /* end draw_line_segment */ void draw_filled_ellipse(int xg0, int yg0, int gwidth, int gheight, XColor line_color, XColor fill_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_fill, fill_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); width = (int)(gwidth*scale); height = (int)(gheight*scale); XFillArc(display, XtWindow ( draw_a ), gc_fill, x0, y0, width, height, 0, 64*360 ); XSetForeground(display, gc_line, line_color.pixel); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0, y0, width, height, 0, 64*360 ); } /* end draw_filled_ellipse */ void draw_line_ellipse(int xg0, int yg0, int gwidth, int gheight, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); width = (int)(gwidth*scale); height = (int)(gheight*scale); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0, y0, width, height, 0, 64*360 ); } /* end draw_line_ellipse */ void draw_filled_circle(int xg0, int yg0, int gradius, XColor line_color, XColor fill_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_fill, fill_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); radius = (int)(gradius*scale); XFillArc(display, XtWindow ( draw_a ), gc_fill, x0-radius, y0-radius, 2*radius, 2*radius, 0, 64*360 ); XSetForeground(display, gc_line, line_color.pixel); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-radius, y0-radius, 2*radius, 2*radius, 0, 64*360 ); } /* end draw_filled_circle */ void draw_line_circle(int xg0, int yg0, int gradius, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); radius = (int)(gradius*scale); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-radius, y0-radius, 2*radius, 2*radius, 0, 64*360 ); } /* end draw_line_circle */ void draw_line_circlec(int xg0, int yg0, int gradius, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); radius = (int)(gradius*scale); XDrawArc(display, XtWindow ( draw_a ), gc_line, x0-radius, y0-radius, 2*radius, 2*radius, 0, 64*360 ); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0-2, y0, x0+2, y0); XDrawLine(display, XtWindow ( draw_a ), gc_line, x0, y0-2, x0, y0+2); } /* end draw_line_circlec */ void draw_filled_rectangle(int xg0, int yg0, int gwidth, int gheight, XColor line_color, XColor fill_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_fill, fill_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); width = (int)(gwidth*scale); height = (int)(gheight*scale); XFillRectangle(display, XtWindow ( draw_a ), gc_fill, x0, y0, width+1, height+1 ); XSetForeground(display, gc_line, line_color.pixel); XDrawRectangle(display, XtWindow ( draw_a ), gc_line, x0, y0, width, height); } /* end draw_filled_rectangle */ void draw_line_rectangle(int xg0, int yg0, int gwidth, int gheight, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); width = (int)(gwidth*scale); height = (int)(gheight*scale); XDrawRectangle(display, XtWindow ( draw_a ), gc_line, x0, y0, width, height); } /* end draw_line_rectangle */ void draw_filled_rectanglec(int xg0, int yg0, int gwidth, int gheight, XColor line_color, XColor fill_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_fill, fill_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); width = (int)(gwidth*scale); height = (int)(gheight*scale); XFillRectangle(display, XtWindow ( draw_a ), gc_fill, x0-width/2, y0-height/2, width+1, height+1 ); XSetForeground(display, gc_line, line_color.pixel); XDrawRectangle(display, XtWindow ( draw_a ), gc_line, x0-width/2, y0-height/2, width, height); } /* end draw_filled_rectanglec */ void draw_line_rectanglec(int xg0, int yg0, int gwidth, int gheight, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); width = (int)(gwidth*scale); height = (int)(gheight*scale); XDrawRectangle(display, XtWindow ( draw_a ), gc_line, x0-width/2, y0-height/2, width, height); } /* end draw_line_rectanglec */ void draw_symbol(int xg0, int yg0, int xg1, int yg1, int xg2, int yg2, int side, int which, XColor line_color, int line_width) { int line_style = 0; /* LineSolid */ int cap_style = 2; /* CapRound */ int join_style = 1; /* JoinRound */ int x2, y2; XSetLineAttributes(display, gc_line, line_width, line_style, cap_style, join_style); XSetForeground(display, gc_line, line_color.pixel); x0 = (int)((xg0-xoff_at)*scale+xmid); y0 = (int)((yg0-yoff_at)*scale+ymid); x1 = (int)((xg1-xoff_at)*scale+xmid); y1 = (int)((yg1-yoff_at)*scale+ymid); x2 = (int)((xg2-xoff_at)*scale+xmid); y2 = (int)((yg2-yoff_at)*scale+ymid); if(which0) xg = ((xg+grid_spacing/2)/grid_spacing)*grid_spacing; if(xg<0) xg = ((xg-grid_spacing/2)/grid_spacing)*grid_spacing; if(yg>0) yg = ((yg+grid_spacing/2)/grid_spacing)*grid_spacing; if(yg<0) yg = ((yg-grid_spacing/2)/grid_spacing)*grid_spacing; *x = (int)((xg-xoff_at)*scale+xmid); *y = (int)((yg-yoff_at)*scale+ymid); } void draw_grid() { int i, j, x, y; /* screen */ int xg, yg; /* real world where snapped */ /* printf("draw_grid called, grid_displayed=%d, grid_spacing=%d, snap=%d \n", grid_displayed, grid_spacing, grid_snap_on); */ if(!grid_displayed) return; for(i=0; i0) xg = ((xg+grid_spacing/2)/grid_spacing)*grid_spacing; if(xg<0) xg = ((xg-grid_spacing/2)/grid_spacing)*grid_spacing; if(yg>0) yg = ((yg+grid_spacing/2)/grid_spacing)*grid_spacing; if(yg<0) yg = ((yg-grid_spacing/2)/grid_spacing)*grid_spacing; x = (int)((xg-xoff_at)*scale+xmid); y = (int)((yg-yoff_at)*scale+ymid); XDrawPoint(display, XtWindow ( draw_a ), gc_grid, x, y); } } } /* end draw_gui of CS635 project 5 by Jon Squire */ static void clear_color() /* on action and symbol buttons */ { XtVaSetValues(frame_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(selecta_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(select_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(text_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(line_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(ellipse_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(circle_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(rect_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(rectc_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(vmux_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(hmux_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(alu_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(and_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(or_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(xor_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(not_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(comp_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(arrow_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(arc_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(dot_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(hres_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(vres_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(hcap_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(vcap_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(hind_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); XtVaSetValues(vind_b, XmNforeground, black_c.pixel, XmNbackground, gray_c.pixel, NULL); } /* end clear_color */ static void write_gif_file(char filename[]) { /* frame_xmin, ... needed first */ FILE * FpGif; int i, j, this_bit; long int my_pixel; XImage * my_image; unsigned char byte_count; unsigned char datablk[1280]; /* [257] */ int nbit; int dbit, jbit; int dbyte; int ncode = 3; /* smallest for 1 bit */ int width, height; /* from frame */ int xoffs, yoffs; /* from frame */ unsigned char head[13]={ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00}; /* head[6]=low width, head[7]=high width, head[8]=low height, head[9]=high height */ unsigned char coltab[6]= {0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF}; unsigned char imgblk[11]= {0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}; /* imgblk[5]=low width, imgblk[6]=high width, imgblk[7]=low height, imgblk[8]=high height */ printf("gif_out stating to write %s \n", filename); FpGif=fopen(filename,"wb"); if(FpGif==NULL) { printf("could not open %s for write\n", filename); XtManageChild(f_dialog); return; } width = frame_xmax-frame_xmin-1; /* omit frame line */ height = frame_ymax-frame_ymin-1; my_image = XGetImage(XtDisplay(draw_a), XtWindow(draw_a), frame_xmin+1, frame_ymin+1, frame_xmax-frame_xmin-1, frame_ymax-frame_ymin-1, 0xFFFFFF, XYPixmap); if(my_image == NULL) { printf("can't get my_image\n"); fclose(FpGif); XtManageChild(f_dialog); return; } head[6]=width&0xFF; head[7]=width>>8; head[8]=height&0xFF; head[9]=height>>8; for(i=0; i<13; i++) putc(head[i], FpGif); /* header */ for(i=0; i<6; i++) putc(coltab[i], FpGif); /* color table */ imgblk[5]=width&0xFF; imgblk[6]=width>>8; imgblk[7]=height&0xFF; imgblk[8]=height>>8; for(i=0; i<11; i++) putc(imgblk[i], FpGif); /* image desc */ /* read pixels and do non LZW encode */ /* pack bits in datablk */ for(i=0; i<1280; i++)datablk[i]=0; /* zero image data */ nbit = 0; for(i=0; i7) datablk[dbyte+1] = dbit>>(8-jbit); nbit = nbit + ncode; dbit = this_bit; /* index into color table to write out */ dbyte = nbit/8; /* byte index */ jbit = nbit-dbyte*8; /* bit index in byte */ datablk[dbyte] = (datablk[dbyte]+(dbit<7) datablk[dbyte+1] = dbit>>(8-jbit); nbit = nbit + ncode; byte_count = nbit/8; if(byte_count >=253) write_gif_data(datablk, &byte_count, &nbit, FpGif); } /* end j loop */ } /* end i loop */ dbit = 0x05; /* end code */ dbyte = nbit/8; /* byte index */ jbit = nbit-dbyte*8; /* bit index in byte */ datablk[dbyte] = (datablk[dbyte]+(dbit<7) datablk[dbyte+1] = dbit>>(8-jbit); nbit = nbit + ncode; byte_count = (nbit+7)/8; write_gif_data(datablk, &byte_count, &nbit, FpGif); putc(0x00, FpGif); /* byte count of zero, indicating last data */ putc(0x3b, FpGif); /* file terminator */ fclose(FpGif); } /* end write_gif_file */ static void write_gif_data(unsigned char datablk[], unsigned char *byte_count, int * nbit, FILE * FpGif) { int j; /* write byte count of 254 or less, doing a data block */ putc(*byte_count, FpGif); /* image data byte count */ /* write packed bits as bytes */ for(j=0; j<(*byte_count); j++) { putc(datablk[j], FpGif); /* image data */ } datablk[0] = datablk[*byte_count]; for(j=1; j<1280; j++)datablk[j]=0; /* zero image data */ *nbit = (*nbit)-(*byte_count)*8; *byte_count = 0; } /* end write_gif_data */ static void write_xbm_file(char filename[]) { /* needs frame_xmin, ... */ FILE * output; int i, j, wcount, hex_bit, hex_val, hex_dig, this_bit; long int my_pixel; XImage * my_image; output=fopen(filename,"w"); if(output==NULL) { printf("could not open %s for write\n", filename); XtManageChild(f_dialog); return; } fprintf(output,"#define draw_width %d\n", frame_xmax-frame_xmin-1); fprintf(output,"#define draw_height %d\n", frame_ymax-frame_ymin-1); fprintf(output,"static char draw_bits[] = {\n"); /* "-1" to avoid drawing dashed frame */ my_image = XGetImage(XtDisplay(draw_a), XtWindow(draw_a), frame_xmin+1, frame_ymin+1, frame_xmax-frame_xmin-1, frame_ymax-frame_ymin-1, 0xFFFFFF, XYPixmap); if(my_image == NULL) { printf("can't get my_image\n"); XtManageChild(f_dialog); return; } printf("writing xbm to %s\n", filename); for(i=frame_ymax-frame_ymin-2; i>=0; i--) { wcount=0; hex_val=0; /* write as 0x%X */ hex_bit=0; /* pack eight bits */ for(j=0; j static void write_bwjpg_file(char filename[]) { XImage * my_image; int this_bit; long int my_pixel; unsigned char my_buffer[1280]; /* one scan line in gray scale */ JSAMPLE * image_buffer=(JSAMPLE *)my_buffer; int image_width=frame_xmax-frame_xmin-1; /* Number of columns in image */ int image_height=frame_ymax-frame_ymin-1; /* Number of rows in image */ int input_components=1; /* grey scale = 1 */ int in_color_space=JCS_GRAYSCALE; /* grey scalr */ int quality=95; int i, j; struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; FILE * outfile; /* target file */ JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); if ((outfile = fopen(filename, "wb")) == NULL) { fprintf(stderr, "can't open %s\n", filename ); XtManageChild(f_dialog); return; } my_image = XGetImage(XtDisplay(draw_a), XtWindow(draw_a), frame_xmin+1, frame_ymin+1, frame_xmax-frame_xmin-1, frame_ymax-frame_ymin-1, 0xFFFFFF, XYPixmap); if(my_image == NULL) { printf("can't get my_image\n"); XtManageChild(f_dialog); return; } jpeg_stdio_dest(&cinfo, outfile); cinfo.image_width = image_width; /* image width and height, in pixels */ cinfo.image_height = image_height; cinfo.input_components = input_components; /* # of color components per pixel */ cinfo.in_color_space = in_color_space; /* colorspace of input image */ jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); jpeg_start_compress(&cinfo, TRUE); row_pointer[0] = & my_buffer[0]; /* keep refilling */ i=0; while (cinfo.next_scanline < cinfo.image_height) { for(j=0; j>11)<<3; /* R */ my_buffer[3*j+1]=((my_pixel>>4)&0x3F)<<2 ; /* G */ my_buffer[3*j+2]=((my_pixel)&0x1F)<<3 ; /* B */ printf("16bit i=%d, j=%d, R=%X G=%X B=%X \n", i, j, my_buffer[3*j], my_buffer[3*j+1], my_buffer[3*j+2]); #else my_buffer[3*j]= my_pixel>>16; /* R */ my_buffer[3*j+1]=(my_pixel>>8)&0xFF ; /* G */ my_buffer[3*j+2]=(my_pixel)&0xFF ; /* B */ #endif } (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); i++; } jpeg_finish_compress(&cinfo); fclose(outfile); jpeg_destroy_compress(&cinfo); } /* end write_coljpg_file */