#include #include #include #include #include #include #include #include XtAppContext app_con; Widget app_sh, StartButton, StopButton; Boolean Running = False; typedef struct { short mag, size; } ApplicationData, *ApplicationDataPtr; ApplicationData AppData; String fallback_resources[] = { "*FontList: *-helvetica-bold-r-*-140-75-*", "*.background: grey90", "*.field.width: 500", "*.field.height: 300", "*.monitor_draw.width: 400", "*.monitor_draw.height: 128", "*.monitor_draw.background: grey", "*.field.background: black", "*.field.foreground: white", "*.openFsb.*selectionLabelString: Open and Load from:", "*.saveFsb.*selectionLabelString: Save into:", "*.Quit.accelerator: Altq", "*.Quit.acceleratorText: Alt+Q", NULL }; Display *dpy = NULL; GC gc; Window win, bar_win; Pixmap pmap; Dimension width, height; Pixel fg, bg; static void setup_draw_info(bar, draw) Widget bar, draw; { unsigned int depth; dpy = XtDisplay(app_sh); win = XtWindow(draw); bar_win = XtWindow(bar); gc = DefaultGC(dpy,DefaultScreen(dpy)); XtVaGetValues(draw,XmNwidth,&width,XmNheight,&height, XmNdepth,&depth,XmNforeground,&fg,XmNbackground,&bg,NULL); pmap = XCreatePixmap(dpy,win,width,height,depth); XSetForeground(dpy,gc,bg); XFillRectangle(dpy,pmap,gc,0,0,width,height); draw_init(); } static void redisplay() { XCopyArea(dpy,pmap,win,gc,0,0,width,height,0,0); } static void reset() { world_reset(); redisplay(); } static void stop_it() { if (!Running) return; Running = False; XtVaSetValues(StopButton,XmNsensitive,False,NULL); XtVaSetValues(StartButton,XmNsensitive,True,NULL); } static Boolean work_one_step(cl) XtPointer cl; { int result; do result = one_step(); while (result == 2); if (result == 0) reset(); return !Running; } static void ClickCB(w,cl,cd) Widget w; XtPointer cl,cd; { XmDrawingAreaCallbackStruct *d = (XmDrawingAreaCallbackStruct *)cd; XButtonEvent *e = &(d->event->xbutton); if (e->type != ButtonPress) return; show_prog_m(xy_to_index(e->x,e->y),(e->button == Button2)); } static void RedrawCB(w,cl,cd) Widget w; XtPointer cl,cd; { XmDrawingAreaCallbackStruct *d = (XmDrawingAreaCallbackStruct *)cd; XExposeEvent *e = &(d->event->xexpose); XCopyArea(dpy,pmap,win,gc,e->x,e->y,e->width,e->height,e->x,e->y); } static void CancelCB(w,cl,cd) Widget w; XtPointer cl,cd; { XtDestroyWidget(w); } static Widget create_fsb(proc,name) XtCallbackProc proc; char *name; { Widget fsb; fsb = XmCreateFileSelectionDialog(app_sh,name,NULL,0); XtUnmanageChild(XmFileSelectionBoxGetChild(fsb,XmDIALOG_HELP_BUTTON)); XtAddCallback(fsb,XmNokCallback,proc,NULL); XtAddCallback(fsb,XmNcancelCallback,CancelCB,NULL); return fsb; } static void OpenOkCB(w,cl,cd) Widget w; XtPointer cl,cd; { char *path; path = XmTextGetString(XmFileSelectionBoxGetChild(w,XmDIALOG_TEXT)); open_file(path); XtDestroyWidget(w); } static void SaveOkCB(w,cl,cd) Widget w; XtPointer cl,cd; { char *path; path = XmTextGetString(XmFileSelectionBoxGetChild(w,XmDIALOG_TEXT)); save_file(path); XtDestroyWidget(w); } static void OpenCB(w,cl,cd) Widget w; XtPointer cl,cd; { static Widget fsb = NULL; if (!fsb) fsb = create_fsb(OpenOkCB,"openFsb"); XtManageChild(fsb); } static void SaveCB(w,cl,cd) Widget w; XtPointer cl,cd; { static Widget fsb = NULL; if (!fsb) fsb = create_fsb(SaveOkCB,"saveFsb"); XtManageChild(fsb); } static void ResetCB(w,cl,cd) Widget w; XtPointer cl,cd; { reset(); } static void QuitCB(w,cl,cd) Widget w; XtPointer cl,cd; { XtDestroyWidget(app_sh); exit(0);} static void StartCB(w,cl,cd) Widget w; XtPointer cl,cd; { Running = True; XtAppAddWorkProc(app_con,work_one_step,NULL); XtVaSetValues(StartButton,XmNsensitive,False,NULL); XtVaSetValues(StopButton,XmNsensitive,True,NULL); } static void StopCB(w,cl,cd) Widget w; XtPointer cl,cd; { stop_it(); } static void StepCB(w,cl,cd) Widget w; XtPointer cl,cd; { stop_it(); XtAppAddWorkProc(app_con,work_one_step,NULL); } static void SpeciesCB(w,cl,cd) Widget w; XtPointer cl,cd; { draw_specie(); } main(argc,argv) int argc; char *argv[]; { Widget main_win, menu_bar, menu, button, draw; Arg args[2]; extern void MonitorCB(); app_sh = XtAppInitialize(&app_con,"X Tierra",NULL,0, &argc,argv,fallback_resources,NULL,0); XtManageChild(main_win=XmCreateMainWindow(app_sh,"main",NULL,0)); XtManageChild(menu_bar=XmCreateMenuBar(main_win,"bar",NULL,0)); menu = XmCreatePulldownMenu(menu_bar,"FileMenu",NULL,0); XtManageChild(button=XmCreatePushButton(menu,"Open",NULL,0)); XtAddCallback(button,XmNactivateCallback,OpenCB,NULL); XtManageChild(button=XmCreatePushButton(menu,"Save",NULL,0)); XtAddCallback(button,XmNactivateCallback,SaveCB,NULL); XtManageChild(button=XmCreatePushButton(menu,"Reset",NULL,0)); XtAddCallback(button,XmNactivateCallback,ResetCB,NULL); XtManageChild(button=XmCreatePushButton(menu,"Quit",NULL,0)); XtAddCallback(button,XmNactivateCallback,QuitCB,NULL); XtSetArg(args[0],XmNsubMenuId,menu); XtManageChild(XmCreateCascadeButton(menu_bar,"File",args,1)); XtManageChild(button=XmCreateCascadeButton(menu_bar,"Monitor",NULL,0)); XtAddCallback(button,XmNactivateCallback,MonitorCB,NULL); XtManageChild(button=XmCreateCascadeButton(menu_bar,"Species",NULL,0)); XtAddCallback(button,XmNactivateCallback,SpeciesCB,NULL); XtManageChild(button=XmCreateCascadeButton(menu_bar,"Step",NULL,0)); XtAddCallback(button,XmNactivateCallback,StepCB,NULL); StartButton = XmCreateCascadeButton(menu_bar,"Start",NULL,0); XtManageChild(StartButton); XtAddCallback(StartButton,XmNactivateCallback,StartCB,NULL); XtSetArg(args[0],XmNsensitive,False); StopButton = XmCreateCascadeButton(menu_bar,"Stop",args,1); XtManageChild(StopButton); XtAddCallback(StopButton,XmNactivateCallback,StopCB,NULL); XtManageChild(draw=XmCreateDrawingArea(main_win,"field",NULL,0)); XtRealizeWidget(app_sh); setup_draw_info(menu_bar,draw); XtAddCallback(draw,XmNinputCallback,ClickCB,NULL); XtAddCallback(draw,XmNexposeCallback,RedrawCB,NULL); init_monitor(); reset(); XtAppMainLoop(app_con); }