home *** CD-ROM | disk | FTP | other *** search
- /* main.c - wimp interfacing routines for backthrow
- * using desklib */
- /* GTK 06-Feb-1993 */
-
- /* standard ANSI C includes.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- /* desklib includes
- */
- #include "Event.h" /* Event despatcher */
- #include "EventMsg.h" /* Wimp Message event dispatcher */
- #include "Error.h" /* Error despatcher */
- #include "Resource.h" /* Handles finding resource files in home dir. */
- #include "Template.h" /* Template loading and cacheing */
- #include "Window.h" /* Window handling automation */
- #include "Icon.h" /* Icon handling automation */
- #include "Hourglass.h" /* Hourglass module interfaces */
- #include "GFX.h" /* Graphics routines (GFX_Wait) */
- #include "Msgs.h" /* Message translation code */
- #include "Handler.h" /* Default event handlers */
- #include "menus.h" /* Menu utilities */
- #include "WimpSwis.h"
-
- /* backthrow includes
- */
- #include "backthrow.h"
-
- /* Misc defines.
- */
- #define BUFFERSIZE 4096
- #define APPNAME_MAX 64
- #define VERSION_MAX 10
- #define NUM_FILES 32
-
- /* defines for the icons in the main window
- */
- #define OK_ICN 0
- #define CANCEL_ICN 1
- #define TEXT_ICN 3
-
- /* define for the version writable icon in the program info dbox.
- */
- #define DBOX_VSN_ICN 4
-
- /* defines for the menu entries.
- */
- #define MNU_INFO 0
- #define MNU_QUIT 1
-
- /* Well... I'll think up something to make this one local to main...
- * until then it has to be global because the iconbar mouse click
- * handler needs to access it.
- */
- menu mnu;
-
- /* Handle icon bar clicks: open window if select is clicked, or
- * open menu if the menu button is clicked.
- */
- BOOL barclickproc(event_pollblock *event, void *reference)
- {
- if (event->data.mouse.button.data.select)
- Window_Show(*(window_handle*)reference, open_WHEREVER);
- if (event->data.mouse.button.data.menu)
- Menus_Show(mnu, event->data.mouse.pos, TRUE);
-
- return TRUE;
- }
-
- /* Close window if cancel clicked, just wrapped into a separate procedure
- * because it has to be an event handler.
- */
- BOOL cancelproc(event_pollblock *event, void *reference)
- {
- Window_Hide(event->data.mouse.window);
- return TRUE;
- }
-
- /* add filename of dragged files to our list.
- */
- BOOL dragproc(event_pollblock *event, void *reference)
- {
- char buf[BUFFERSIZE];
- static BOOL first=TRUE;
- window_handle mywindow;
-
- mywindow = *(window_handle*)reference;
-
- /* if the file was dragged to the window, then insert it
- */
- if (event->data.message.data.dataload.window == mywindow)
- {
- Icon_GetText(mywindow, TEXT_ICN, buf);
- if(first || strlen(buf) == 0)
- {
- Icon_SetText(mywindow, TEXT_ICN,
- event->data.message.data.dataload.filename);
- first = FALSE;
- }
- else
- Icon_printf(mywindow, TEXT_ICN, "%s,%s",buf,
- event->data.message.data.dataload.filename);
- }
- /* otherwise replace the contents of the icon with the filename,
- * and open the main window.
- */
- else
- {
- Icon_SetText(mywindow, TEXT_ICN,
- event->data.message.data.dataload.filename);
- if (first)
- first = FALSE;
- Window_Show(*(window_handle*)reference, open_WHEREVER);
- }
-
- return TRUE;
- }
-
- /* This one is called when the OK button is clicked. It decomposes the
- * string in the icon into a string array, then calls backthrow.
- */
- BOOL okproc(event_pollblock *event, void *reference)
- {
- char *buf, *end, *sav;
- char **pass;
- int num;
- BOOL retval;
-
- /* init a buffer for the contents of the writable icon, and pointers
- * and the array for backthrow().
- */
- buf = malloc(BUFFERSIZE);
- sav = buf;
- num = 0;
- pass = (char **) calloc(NUM_FILES, sizeof(int *));
-
- /* read the icons contents, and init the end pointer for the
- * resulting string.
- */
- Icon_GetText(event->data.mouse.window, TEXT_ICN, buf);
- end = buf + strlen(buf);
-
- /* this comparison does not work properly... it should inhibit
- * the start of the backthrower with a zero filename argument.
- */
- if (end > buf)
- {
- /* init first array argument.
- */
- pass[num++] = buf++;
- /* replace every ',' in the string with a '\0', then set the next
- * array element to point to the next char after the ',' unless
- * there is no next char. Increment element counter as appropriate.
- */
- while ((num < NUM_FILES) && (buf < end))
- if (*buf == ',')
- {
- *buf++ = NULL;
- if (buf < end)
- pass[num++] = buf;
- }
- else
- buf++;
- }
-
- /* call backthrow, and store its return value for the caller.
- */
- retval = backthrow(num, pass);
-
- /* clean up and close the main window after processing.
- */
- free(sav);
- Window_Hide(event->data.mouse.window);
-
- /* return retval frombackthrow to the caller.
- */
- return retval;
- }
-
- /* handle menu selections
- */
- void mnuproc(void *reference, int *hit)
- {
- switch(*hit)
- {
- case MNU_QUIT: Event_CloseDown();
- break;
- case MNU_INFO: ; /* Nothing happens! */
- }
- }
-
- /* This is the Menuselection handler procedure for the iconbar menu.
- * Doesn't really do a lot... just calls the mnuproc
- */
- BOOL mnuhandlerproc(event_pollblock *event, void *reference)
- {
- mnuproc(NULL, event->data.selection);
-
- return TRUE;
- }
-
- /* the programs main: just do some initialisations, then go into an
- * infinite event_poll() loop.
- */
- int main()
- {
- window_handle window;
- window_handle proginfo;
- icon_handle baricon;
- char appname[APPNAME_MAX];
- char version[VERSION_MAX];
-
- /* Tell Resource (and thereby Template, Msgs, etc) where our resource
- * directory is: "<Backthrow$Dir>"
- */
- Resource_Initialise("Backthrow"); /* resources in <Tester$Dir> */
-
- /* Load and cache the messages. Find out the application name
- * and version number
- */
- Msgs_LoadFile("messages");
- Msgs_Lookup("app.name:Backthrow", appname, APPNAME_MAX);
- Msgs_Lookup("app.version:unset", version, VERSION_MAX);
-
- /* Initialise the Wimp and Event Manager.
- */
- Event_Initialise(appname);
-
- /* Put an icon onto the iconbar
- */
- baricon = Icon_BarIcon("!Backthrow", iconbar_RIGHT);
-
- /* Place the Handler_ module skeleton default handlers on all
- * Redraw and Open-window request events (Application-wide defaults)
- */
- Event_Claim(event_REDRAW, event_ANY, event_ANY, Handler_NullRedraw, NULL);
- Event_Claim(event_OPEN, event_ANY, event_ANY, Handler_OpenWindow, NULL);
-
- /* Load in and cache our window templates from the file
- * "<Backthrow$Dir>.Templates" (Templates utilise the "Resource Directory")
- */
- Template_Initialise();
- Template_LoadFile("Templates");
-
- /* Create and open our main windows from the template "mainwindow".
- * Centered on screen (screen-mode independent)
- */
- window = Window_Create("mainwindow", 0);
- proginfo = Window_Create("proginfo",20);
- Icon_SetText(proginfo, DBOX_VSN_ICN, version);
-
- /* Claim events:
- * window close
- */
- Event_Claim(event_CLOSE, window, event_ANY, Handler_CloseWindow, NULL);
- /* Click to the OK icon
- */
- Event_Claim(event_CLICK, window, OK_ICN, okproc, NULL);
- /* Click to the cancel icon
- */
- Event_Claim(event_CLICK, window, CANCEL_ICN, cancelproc, NULL);
- /* Click to the iconbar icon (also does the iconbar menu handling)
- */
- Event_Claim(event_CLICK, window_ICONBAR, baricon, barclickproc, &window);
- /* Iconbar menu selection event handler
- */
- Event_Claim(event_MENU, event_ANY, baricon, mnuhandlerproc, NULL);
-
- /* Add the Window_HelpHandler message handler so that help requests
- * are handeled automatically.
- */
- Window_AutoHelp(event_ANY, event_ANY);
-
- /* Initialize the eventmsg system, and claim the dataload message
- * (for handling the filedrags to the main window)
- */
- EventMsg_Initialise();
- EventMsg_Claim(message_DATALOAD, event_ANY, dragproc, &window);
-
- /* Setup application menu
- */
- mnu = Menus_New("Backthrow", "Info ,Quit");
- Menus_AddDialogBox(mnu, 1, proginfo);
-
- /* attach menu to the main window, so that it is handeled automatically.
- * the menu that is opened on the iconbar is handeled separately...
- * (mainly because I did not find out to handle it automatically and
- * correctly!)
- */
- Menus_AttachMenu(mnu, window, event_ANY, mnuproc, NULL);
-
- /* Main event handling loop. Let Event_* do all the work for us!
- */
- while (TRUE)
- Event_Poll();
- }
-