home *** CD-ROM | disk | FTP | other *** search
- /* > c.Control
- *
- * Title: Control Purpose: Graphics Fun - show a display Works with Penrose
- *
- * Left-clicking on the icon will start the display */
-
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
-
- #include "wimp.h" /* access to WIMP SWIs */
- #include "wimpt.h" /* wimp task facilities */
- #include "win.h" /* registering window handlers */
- #include "event.h" /* poll loops, etc */
- #include "baricon.h" /* putting icon on icon bar */
- #include "res.h" /* access to resources */
- #include "resspr.h" /* sprite resources */
- #include "template.h" /* reading in template file */
- #include "dbox.h" /* dialogue box handling */
- #include "werr.h" /* error reporting */
-
- /* externals to display routines */
-
- #include "Display.h"
-
- /* --- Menu Entry Constants --- */
- #define iconmenu_MInfo 1
- #define iconmenu_MDisplay 2
- #define iconmenu_MPattern 3
- #define iconmenu_MQuit 4
-
- static int type = 0;
-
-
- /***************************** WINDOW HANDLING *****************************/
-
-
- /*--- Create the window, yielding its handle. Return TRUE if ok. ---*/
- static BOOL display_create_window(display_datas *display, wimp_w *handle)
- {
- wimp_wind *window; /* Pointer to window definition */
-
- /* Find template for the window */
- if ((window = template_syshandle("Display")) == 0)
- return FALSE;
-
- /* Create the window, dealing with errors */
- if (wimpt_complain(wimp_create_wind(window, handle)) != 0)
- return FALSE;
-
- display->w = *handle;
-
- return TRUE;
- }
-
-
- /*--- Individual event routines for the window ---*/
-
- static void display_redo_window(display_datas *display,
- wimp_redrawstr *r, BOOL more)
- {
- /* --- refresh the window's contents --- */
- while (more)
- {
- display_show(display, r);
-
- wimp_get_rectangle(r, &more);
- }
-
- /* We do not report any errors - see the manual for the reason why */
- }
-
-
- static void display_redraw_window(display_datas *display, wimp_w handle)
- {
- wimp_redrawstr r;
- BOOL more;
-
- /* --- do the redraw --- */
- r.w = handle;
- wimpt_noerr(wimp_redraw_wind(&r, &more));
-
- if (more)
- display_redo_window(display, &r, more);
- }
-
-
- static void display_open_window(display_datas *display, wimp_openstr *o)
- {
- /* Pass the open request on to the wimp */
- wimpt_noerr(wimp_open_wind(o));
- }
-
-
- /*--- Close the window. ---*/
- static void display_close_window(display_datas *display, wimp_w handle)
- {
- /* Close the window */
- wimpt_noerr(wimp_close_wind(handle));
-
- display->displaying = FALSE;
-
- /* Decrement active window count - this will lead to closedown */
- /* win_activedec(); */
- }
-
-
- /****************************** EVENT HANDLING *****************************/
-
-
- /*--- Event handler for window. 'handle' is a pointer to the display. ---*/
- static void display_handler(wimp_eventstr *e, void *handle)
- {
- display_datas *display = (display_datas *)handle;
-
- /* Deal with event */
- switch (e->e)
- {
- case wimp_EREDRAW:
- display_redraw_window(display, e->data.o.w);
- break;
-
- case wimp_EOPEN:
- display_open_window(display, &e->data.o);
- break;
-
- case wimp_ECLOSE:
- display_close_window(display, e->data.o.w);
- break;
-
- case wimp_ESEND:
- case wimp_ESENDWANTACK:
- switch (e->data.msg.hdr.action)
- {
- case wimp_MHELPREQUEST:
- e->data.msg.hdr.your_ref = e->data.msg.hdr.my_ref;
- e->data.msg.hdr.action = wimp_MHELPREPLY;
- e->data.msg.hdr.size = 256;
- if (e->data.msg.data.helprequest.m.i == -1) /* ie. not on our icon */
- sprintf(e->data.msg.data.helpreply.text,
- "This is the display window.|MOnly one can be active");
- else
- sprintf(e->data.msg.data.helpreply.text,
- "This is the display icon.|MClick SELECT to start display");
- wimpt_noerr(wimp_sendmessage(wimp_ESEND, &e->data.msg,
- e->data.msg.hdr.task));
- break;
-
- default:
- break;
- }
-
- default: /* we're not interested in any other events */
- break;
- }
- }
-
-
- /*--- Event handler called on a left click on the icon. ---*/
- static void control_leftclickproc(wimp_i icon)
- {
- wimp_wstate state;
- wimp_winfo info;
- wimp_redrawstr r;
- icon = icon;
-
- display_init(type++);
-
- /* --- open the window we created --- */
- wimpt_noerr(wimp_get_wind_state(display_data.w, &state));
-
- if (display_data.displaying == FALSE)
- {
- state.o.behind = -1; /* make sure it is opened in front */
- display_open_window(&display_data, &state.o);
-
- r.w = display_data.w;
- r.box.x0 = 0;
- r.box.x1 = 100;
- r.box.y0 = -100;
- r.box.y1 = 0;
- display_data.displaying = TRUE;
- }
- else
- {
- r.w = -1;
- r.box = state.o.box;
- r.w = display_data.w;
- r.box.x0 = state.o.x;
- r.box.x1 = state.o.x+state.o.box.x1-state.o.box.x0;
- r.box.y0 = state.o.y+state.o.box.y0-state.o.box.y1;
- r.box.y1 = state.o.y;
- }
-
- /* --- force a redraw of the whole window --- */
- wimpt_noerr(wimp_force_redraw(&r));
- }
-
-
- /*--- Event handler called for Pattern menu entry. ---*/
- static void control_pattern(wimp_i icon, int pattern)
- {
- wimp_wstate state;
- wimp_winfo info;
- wimp_redrawstr r;
- icon = icon;
-
- if (pattern != -1)
- {
- type = pattern;
- display_init(type);
-
- if (display_data.displaying != FALSE)
- {
- /* --- open the window we created --- */
- wimpt_noerr(wimp_get_wind_state(display_data.w, &state));
-
- r.w = -1;
- r.box = state.o.box;
- r.w = display_data.w;
- r.box.x0 = state.o.x;
- r.box.x1 = state.o.x+state.o.box.x1-state.o.box.x0;
- r.box.y0 = state.o.y+state.o.box.y0-state.o.box.y1;
- r.box.y1 = state.o.y;
-
- /* --- force a redraw of the whole window --- */
- wimpt_noerr(wimp_force_redraw(&r));
- }
- }
- }
-
-
- static void control_info_aboutprog(void)
- {
- dbox d; /* Dialogue box handle */
-
- /* --- display info about the program in a dialogue box --- */
- if ((d = dbox_new("ProgInfo")) != NULL)
- {
- dbox_showstatic(d);
-
- /* Keep it on the screen as long as needed */
- dbox_fillin(d);
-
- /* Dispose of the dialogue box */
- dbox_dispose(&d);
- }
- }
-
- /******************************* MENU HANDLING *****************************/
-
-
- static menu control_menumaker(void *handle)
- {
- menu temp, sub;
- handle = handle;
-
- /* --- create a menu for the icon on the icon bar --- */
- temp = menu_new(display_name, ">Info,Display,Pattern,Quit");
- sub = menu_new("Patterns", "Standard,Darts&Kites,Gingerbread,Roman,Curves,Rhombii");
- menu_submenu(temp, iconmenu_MPattern, sub);
-
- /* --- fade out "start" field if we already have a display --- */
- menu_setflags(temp, iconmenu_MDisplay, 0,
- (display_data.displaying == TRUE) ? 1 : 0);
-
- return temp;
- }
-
-
- /*--- Event handler for the menu. ---*/
- static void control_menuproc(void *handle, char *hit)
- {
- handle = handle;
-
- /* --- see which menu entry has been chosen --- */
- switch (hit[0])
- {
- case iconmenu_MInfo:
- control_info_aboutprog();
- break;
-
- case iconmenu_MDisplay:
- control_leftclickproc((wimp_i) 0);
- break;
-
- case iconmenu_MPattern:
- control_pattern((wimp_i) 0, hit[1]-1);
- break;
-
- case iconmenu_MQuit:
- exit(0);
- break;
-
- default: /* shouldn't get here */
- break;
- }
- }
-
- /******************************** INITIALISATION ***************************/
-
- static BOOL control_initialise(void)
- {
- char baricon_name[30];
- wimp_w handle;
-
- /* --- initialise wimp library modules --- */
- wimpt_init(display_name);
- res_init(display_name);
- resspr_init();
- template_init();
- dbox_init();
-
- /* Initialise display handler */
- type = 0;
- display_init(type);
-
- /* --- create a window for display --- */
- display_create_window(&display_data, &handle);
-
- /* --- attach an event handling function to window --- */
- win_register_event_handler(handle, display_handler, &display_data);
-
- /* --- put our icon on the icon bar --- */
- baricon_name[0] = '!';
- strcpy(baricon_name + 1, display_name);
- baricon(baricon_name, (int) resspr_area(), control_leftclickproc);
-
- /* --- attach a menu to the icon on the icon bar --- */
- event_attachmenumaker(win_ICONBAR, control_menumaker,
- control_menuproc, 0);
-
- event_attachmenumaker(handle, control_menumaker,
- control_menuproc, 0);
-
- /* --- activate saving of floating point registers on poll --- */
- wimp_save_fp_state_on_poll();
-
- /* All went ok */
- return TRUE;
- }
-
- /******************************* MAIN PROGRAM ******************************/
-
-
- int main(int argc, char *argv[])
- {
- /* --- initialise the environment --- */
- if (control_initialise())
- {
- /* --- mask off the events we're not interested in --- */
- event_setmask(wimp_EMPTRENTER | wimp_EMPTRLEAVE);
-
- /* --- the main event loop --- */
- while (TRUE)
- event_process();
- }
-
- return 0;
- }
-