home *** CD-ROM | disk | FTP | other *** search
- /*
- File: MyWindow.c
- Author: Copyright © 1993 Paul Vernon
- Version: 1.00 (19 Mar 1992)
- Purpose: My High-level window management functions:
- these do not clone the windows, so only one open at once!
- */
-
- #include "DeskLib:Wimp.h"
- #include "DeskLib:StringCR.h"
- #include "DeskLib:Error.h"
- #include "DeskLib:LinkList.h"
- #include "DeskLib:Template.h"
- #include "DeskLib:WimpSWIs.h"
- #include "DeskLib:Error.h"
- #include "DeskLib:Screen.h"
- #include "DeskLib:Event.h"
- #include "MyWindow.h"
-
- #include <stdlib.h>
- #include <string.h>
-
- static linklist_header mywindow_listanchor;
-
- typedef struct
- {
- linklist_header header;
- window_handle window;
- char templatename[wimp_MAXNAME +1];
- window_block *memory; /* template block */
- } windowrec;
-
- /* Tries to find window of name windowname, returns handle if exists else NULL*/
- extern window_handle MyWindow_Find(char *windowname)
- {
- windowrec *ptr;
-
- ptr = (windowrec *) mywindow_listanchor.next;
- while (ptr != NULL){
-
- if (strcmpcr(ptr->templatename, windowname)==0){
- break;
- }
- ptr = (windowrec *) ptr->header.next;
- }
- if (ptr != NULL){
- return(ptr->window); /* Window does exist */
- }
- return NULL; /* Does not exist */
- }
-
- #define ERRBASE 1
- #define ERR1 ERRBASE+0
- #define ERRMESS1 "Insufficient memory to open window"
-
- extern window_handle MyWindow_Create(char *windowname)
- {
- windowrec *record;
- window_block *windowptr;
- window_handle window;
-
- windowptr = Template_Find(windowname);
-
- Wimp_CreateWindow(windowptr, &window);
-
- record = (windowrec *) malloc(sizeof(windowrec));
- if (record == NULL) Error_ReportFatalInternal(ERR1, ERRMESS1);
-
- LinkList_AddToHead(&mywindow_listanchor, &(record->header));
-
- strncpy(record->templatename, windowname, wimp_MAXNAME);
- record->templatename[wimp_MAXNAME] = 0; /*Record of name for help */
-
- record->window = window; /*Remember window handle */
- record->memory = windowptr; /*template window block*/
-
- return(window);
- }
-
- extern void MyWindow_Delete(window_handle window)
- {
- windowrec *ptr;
-
- Event_ReleaseWindow(window); /* Release all handlers for this window */
-
- Wimp_CloseWindow(window); /* say bye bye! */
- Wimp_DeleteWindow(window);
-
- ptr = (windowrec *) mywindow_listanchor.next;
- while (ptr != NULL)
- {
- if (ptr->window == window)
- break;
- ptr = (windowrec *) ptr->header.next;
- }
-
- if (ptr == NULL)
- return;
-
- LinkList_Unlink(&mywindow_listanchor, &(ptr->header));
-
- free(ptr); /* ... and the window's list entry */
- }
-
-
- extern void MyWindow_DeleteAll(void)
- /* as Window_Delete changes the window list, enougth to repeatly
- delete the first element */
- {
- windowrec *ptr;
-
- ptr = (windowrec *) mywindow_listanchor.next;
-
- while (ptr != NULL)
- {
- Error_Report(0,"Deleting :%s",ptr->templatename);
- Event_ReleaseWindow(ptr->window);
- Error_Check(Wimp_CloseWindow(ptr->window));
- Error_Check(Wimp_DeleteWindow(ptr->window));
- LinkList_Unlink(&mywindow_listanchor, &(ptr->header));
- free(ptr);
- ptr = (windowrec *) mywindow_listanchor.next;
- }
- }
-
- static wimp_point lastopenpos = {-1, -1};
-
-
- extern void Window_Show(window_handle window, window_openpos openpos)
- {
- window_state wstate;
- wimp_point moveto = {0, 0};
- int w, h;
-
- Screen_CacheModeInfo(); /* Ensure got correct screen mode info. */
-
- Wimp_GetWindowState(window, &wstate);
- wstate.openblock.behind = -1; /* open in front */
-
- w = wstate.openblock.screenrect.max.x - wstate.openblock.screenrect.min.x;
- h = wstate.openblock.screenrect.max.y - wstate.openblock.screenrect.min.y;
-
- switch(openpos)
- {
- case open_CENTERED:
- moveto.x = (screen_size.x - w) / 2;
- moveto.y = (screen_size.y + h) / 2;
- break;
-
- case open_OVERCARET:
- {
- caret_block caret;
- window_state wstate;
-
- Wimp_GetCaretPosition(&caret);
-
- if (caret.window > 0)
- {
- Wimp_GetWindowState(caret.window, &wstate);
-
- moveto.x = wstate.openblock.screenrect.min.x +
- (caret.offset.x - wstate.openblock.scroll.x) - 64;
- moveto.y = wstate.openblock.screenrect.max.y -
- (caret.offset.y - wstate.openblock.scroll.y) + 64;
- }
- else
- {
- /* No caret, so just open centered on screen */
- moveto.x = (screen_size.x - w) / 2;
- moveto.y = (screen_size.y + h) / 2;
- }
- }
- break;
-
- case open_UNDERPOINTER:
- {
- mouse_block ptr;
-
- Wimp_GetPointerInfo(&ptr);
- moveto.x = ptr.pos.x - 64;
- moveto.y = ptr.pos.y + 64;
- }
- break;
-
- case open_NEARLAST:
- if (lastopenpos.x >= 0)
- {
- moveto.x = lastopenpos.x + 16;
- moveto.y = lastopenpos.y - 16;
- }
- else
- {
- moveto.x = (screen_size.x - w) / 2;
- moveto.y = (screen_size.y + h) / 2;
- }
-
- if (moveto.x > ((screen_size.x / 2) + 128))
- moveto.x = (screen_size.x / 2) - 128;
-
- if (moveto.y < ((screen_size.y / 2) - 128))
- moveto.y = (screen_size.y / 2) + 128;
- break;
-
- default:
- /* Open wherever it is defined in the template file. */
- moveto.x = wstate.openblock.screenrect.min.x;
- moveto.y = wstate.openblock.screenrect.max.y;
- break;
- }
-
- if (moveto.x < 0) moveto.x = 0;
- if (moveto.y < 64) moveto.y = 64;
- if (moveto.x > screen_size.x - 96) moveto.x = screen_size.x - 96;
- if (moveto.y > screen_size.y - 32) moveto.y = screen_size.y - 32;
-
- wstate.openblock.screenrect.min.x = moveto.x;
- wstate.openblock.screenrect.max.y = moveto.y;
-
- wstate.openblock.screenrect.max.x = wstate.openblock.screenrect.min.x + w;
- wstate.openblock.screenrect.min.y = wstate.openblock.screenrect.max.y - h;
-
- if (openpos == open_NEARLAST)
- {
- lastopenpos.x = wstate.openblock.screenrect.min.x; /* save last open pos*/
- lastopenpos.y = wstate.openblock.screenrect.max.y;
- }
-
- Wimp_OpenWindow(&wstate.openblock);
- }
-
- extern void Window_GetInfo(window_handle window, window_info *result)
- /* Returns just the window part of the window_info block (strips icon defn.s)
- * The real solution would be to find out how many icons were in the window
- * and then get a flex block big enough for the whole thing and then do it,
- * but you can't read the number of icons until you have read the window_info
- * so it's a bit difficult! As a result, I just use a 4kB block to hold
- * the window definition in.
- * NOTE THAT THIS WILL CRASH IF A WINDOW HAS TOO MANY ICONS!!!!!
- * (anything more than about 120 icons will cause undefined results)
- */
- {
- int block[1024]; /* 4096 bytes for winfo return block */
- window_info *winfo = (window_info *) &block[0];
-
- winfo->window = window;
- Wimp_GetWindowInfo(winfo);
- memcpy(result, winfo, sizeof(window_info));
- }
-
- extern BOOL Window_HelpHandler(event_pollblock *event, void *reference);
-
-
- /* NOTE:
- * By #defining "USE_EVENTMSG" on compilation, the HelpHandler will be
- * called ONLY for a HelpRequest message, but this will also pull in the
- * "EventMsg" code.
- *
- * This function is basically here to show you how to include the
- * Window_HelpHandler under your own steam, using either EventMsg or Event
- *
- * use: #define USE_EVENTMSG 1
- *
- */
-
- extern BOOL Window_AutoHelp(window_handle window, icon_handle icon)
- {
- #ifdef USE_EVENTMSG
- return(EventMsg_Claim(message_HELPREQUEST, window,
- Window_HelpHandler, NULL);
- #else
- return(Event_Claim(event_USERMESSAGERECORDED, window, icon,
- Window_HelpHandler, NULL));
- #endif
- }
-
-