home *** CD-ROM | disk | FTP | other *** search
- /*
- * Title : Lesson 5
- * System : New Risc-OS library. */
- #define Version "1.0"
- /* Copyright : (c) John Winters
- * Date : 28th November, 1993
- * Author : John H. Winters
- *
- * Function : Lesson 5 of the Spirit library tutorial.
- *
- * At last, a "Hello World" program.
- *
- * N.B. We use the high-level font opening routines in WinAppLib,
- * rather than the low-level ones in FontLib, so that they will be
- * closed automatically when the program exits.
- *
- *
- * Modification history.
- *
- * Version :
- * Date :
- * Author :
- * Changes :
- *
- */
-
- #include <stdlib.h>
- #include <string.h>
- #include "fontlib.h"
- #include "winapp.h"
-
- /*
- *==========================================================================
- *
- * Hash defines.
- *
- *==========================================================================
- */
-
- #define FONT_SIZE 50 /* Point size of font to use */
- #define MENU_INFO 0 /* Menu entry for information */
- #define MENU_QUIT 1 /* Menu entry to quit */
- #define TFN_VERSION 4 /* Field number for version string */
-
- /*
- *==========================================================================
- *
- * Local data.
- *
- *==========================================================================
- */
-
- /*
- * The following data items describe the font we are going
- * to use.
- */
- static const t_font_descriptor *FontDescriptor ;
- static char FontName [256] = "Trinity.Medium" ;
- static unsigned int FontResolution = 0 ;
- static unsigned int FontSize = FONT_SIZE * 16 ;
- /*
- * The text we propose to print.
- */
- static const char Text [] = "Hello World" ;
- /*
- * Handle on the dialogue box which we use to give process information.
- */
- static t_dbox InfoDbox ;
- /*
- * Name of our template file.
- */
- static const char TemplateFileName [] = "Templates" ;
- /*
- * String to place in our information dialogue box.
- */
- static char VersionString [] = Version " (28th November, 1993)" ;
- /*
- * Handle for our window.
- */
- static t_window_handle WindowHandle ;
-
- /*
- *==========================================================================
- *
- * Externally visible data.
- *
- *==========================================================================
- */
-
- /*
- * This data item gives the name of the directory to look in when loading
- * resources. It will make the library code look for an environment
- * variable <Lesson5$Dir> which points to the correct directory.
- */
- const char WA_resource_dir [] = "Lesson5" ;
- /*
- * This data item defines the task name.
- */
- const char WA_task_name [] = "Lesson 5" ;
-
- /*
- *==========================================================================
- *
- * Forward declarations.
- *
- *==========================================================================
- */
-
- static unsigned int ClickHandler (
- t_poll_block *poll_block,
- void *reference) ;
-
- static unsigned int FontHandler (
- void *reference,
- const u8 *name) ;
-
- static void MenuHandler (
- void *reference,
- const int *hit) ;
-
- static uint RedrawHandler (
- t_poll_block *poll_block,
- void *reference) ;
-
- /*
- *==========================================================================
- *
- * Externally visible routines.
- *
- *==========================================================================
- */
-
- unsigned int WA_BeginProcessing (void)
-
- /*
- * Function :
- * This routine is called after we have succesfully
- * called WIMP_Initialise and are ready to go ahead.
- *
- * Parameters :
- * None.
- *
- * Returns :
- * TRUE if we are happy to go on, FALSE otherwise.
- *
- */
-
- {
- t_menu_block *MainMenu ;
-
- /*
- * Create a menu to attach to our icon. The syntax for the menu
- * string is as for Acorn's product.
- */
- MainMenu = WA_BuildMenu (WA_resource_dir,
- ">Info, Quit") ;
- if (MainMenu == NULL)
- {
- LOG_Error ("Failed to create a menu.\n") ;
- }
- else
- {
- /*
- * Now create a dialogue box from the template we loaded
- * earlier. It's a non-persistent one (FALSE), and
- * doesn't require a handler or reference (NULL, NULL).
- */
- InfoDbox = WA_CreateDbox ("ProgInfo", FALSE, NULL, NULL) ;
- if (InfoDbox == NULL)
- {
- LOG_Error ("Can't create information dbox.\n") ;
- }
- else
- {
- /*
- * Inject our current version number into the dialogue
- * box.
- */
- WA_SetDboxField (InfoDbox,
- TFN_VERSION,
- VersionString) ;
- /*
- * Create a window. Note the use of WA_CreateWindow
- * instead of WIMP_CreateWindow. The former does more
- * work for us (cloning the template etc.) and registers
- * a default handler.
- */
- if ((WA_CreateWindow ("Window", NULL, &WindowHandle)) &&
- (WA_ClaimEvent (ET_RedrawRequest,
- WindowHandle,
- DONT_CARE,
- RedrawHandler,
- NULL)))
- {
- /*
- * Now pop our icon on the bar, and attach a menu to it.
- * If an error occurs it will be reported for us.
- */
- return ((WA_IconToBar ("!lesson5", TRUE)) &&
- (WA_AttachMenu (ICON_BAR,
- MainMenu,
- MenuHandler,
- NULL)) &&
- (WA_ClaimEvent (ET_MouseClick,
- ICON_BAR,
- DONT_CARE,
- ClickHandler,
- NULL)) &&
- (WA_AttachFontMenu (WindowHandle,
- FALSE,
- FontName,
- FontHandler,
- NULL))) ;
- }
- }
- }
- return (FALSE) ;
- }
-
-
- unsigned int WA_LoadResources (void)
-
- /*
- * Function :
- * This is our opportunity to load whatever resources
- * we require before WIMP_Initialise is called.
- *
- * Parameters :
- * None.
- *
- * Returns :
- * TRUE if we are happy to go on, FALSE otherwise.
- *
- */
-
- {
- t_LOG_HandlerMask mask ;
-
- /*
- * Arrange for any messages to appear in a pop-up box. A more
- * sophisticated application would probably arrange its own
- * message handling, but this easy method is provided for noddy
- * code.
- */
- mask.value = 0 ;
- mask.b.LHM_Debug = TRUE ;
- mask.b.LHM_Info = TRUE ;
- mask.b.LHM_Warning = TRUE ;
- mask.b.LHM_Error = TRUE ;
- mask.b.LHM_SevereError = TRUE ;
- WA_SetPopupErrors (mask) ;
- /*
- * Now we need a sprite to put on the icon bar, and a template
- * for our information box. Note that we are here loading our sprite
- * from the !Sprites file in our application directory. If we were
- * using more than one sprite, we would probably use a separate
- * sprite file.
- */
- return ((WA_LoadSprites ("!Sprites")) &&
- (WA_LoadTemplate (TemplateFileName, "ProgInfo")) &&
- (WA_LoadTemplate (TemplateFileName, "Window")) &&
- ((FontDescriptor = WA_OpenFont (FontName,
- FontSize,
- FontSize,
- FontResolution,
- FontResolution)) != NULL)) ;
- }
-
- /*
- *==========================================================================
- *
- * Local routines.
- *
- *==========================================================================
- */
-
- static unsigned int ClickHandler (
- t_poll_block *poll_block,
- void *reference)
-
- /*
- * Function :
- * Click handler.
- *
- * Parameters :
- * poll_block Pointer to block describing the event.
- * reference Ignored.
- *
- * Returns :
- * TRUE if we handled the event, FALSE otherwise.
- *
- */
-
- {
- t_state_block state ;
-
- reference = reference ;
- /*
- * Check for left or right.
- */
- if ((poll_block->data.mc.button_state.m.adjust) ||
- (poll_block->data.mc.button_state.m.select))
- {
- /*
- * The customer has clicked on our icon. Display our window.
- * If it is already on view, this sequence will have the effect
- * of bringing it to the front.
- */
- if (LOG_OSError (WIMP_GetWindowState (WindowHandle,
- &state)) == NULL)
- {
- state.open_block.behind = -1 ;
- LOG_OSError (WIMP_OpenWindow (&(state.open_block))) ;
- }
- return (TRUE) ;
- }
- else
- {
- return (FALSE) ;
- }
- }
-
-
- static unsigned int FontHandler (
- void *reference,
- const u8 *name)
-
- /*
- * Function :
- * Handles notifications of font changes.
- *
- * Parameters :
- * reference Ignored.
- * name New font name.
- *
- * Returns :
- * None.
- *
- */
-
- {
- const t_font_descriptor *descriptor ;
- unsigned int height ;
- t_redraw_block redraw_block ;
- unsigned int result ;
- t_state_block state_block ;
- unsigned int width ;
-
- reference = reference ;
- result = FALSE ;
- if (*name != '\0')
- {
- descriptor = WA_OpenFont (name,
- FontSize,
- FontSize,
- FontResolution,
- FontResolution) ;
- if (descriptor != NULL)
- {
- WA_CloseFont (FontDescriptor) ;
- strcpy (FontName, name) ;
- FontDescriptor = descriptor ;
- if (LOG_OSError (WIMP_GetWindowState (WindowHandle,
- &state_block)) == NULL)
- {
- if (state_block.flags.is_open)
- {
- /*
- * As a short cut here, we could just invalidate the
- * rectangular area of the screen which our visible work
- * area is occupying. Unfortunately, this would cause any
- * window overlaying it to flicker.
- */
- height = state_block.open_block.visible_area.max.y -
- state_block.open_block.visible_area.min.y ;
- width = state_block.open_block.visible_area.max.x -
- state_block.open_block.visible_area.min.x ;
- redraw_block.handle = WindowHandle ;
- redraw_block.visible_area.min.x =
- state_block.open_block.scroll_offset.x ;
- redraw_block.visible_area.min.y =
- state_block.open_block.scroll_offset.y - height ;
- redraw_block.visible_area.max.x =
- state_block.open_block.scroll_offset.x + width ;
- redraw_block.visible_area.max.y =
- state_block.open_block.scroll_offset.y ;
- LOG_OSError (WIMP_ForceRedraw (&redraw_block)) ;
- }
- }
- /*
- * We have changed font. Set result to TRUE so that our
- * menu will be updated with appropriate ticks.
- */
- result = TRUE ;
- }
- }
- return (result) ;
- }
-
-
- static void MenuHandler (
- void *reference,
- const int *hit)
-
- /*
- * Function :
- * Menu event handler.
- *
- * Parameters :
- * reference Ignored.
- * hit Indication of the menu hit.
- *
- * Returns :
- * None.
- *
- */
-
- {
- reference = reference ;
- /*
- * See which menu entry has been chosen.
- */
- switch (hit [0])
- {
- case MENU_INFO :
- WA_DisplayDbox (InfoDbox) ;
- break ;
-
- case MENU_QUIT :
- /*
- * This call causes our application to exit in an
- * orderly manner.
- */
- WA_WindUp () ;
- break ;
-
- }
- }
-
-
- static uint RedrawHandler (
- t_poll_block *poll_block,
- void *reference)
-
- /*
- * Function :
- * Handle incoming redraw requests.
- *
- * Parameters :
- * poll_block Poll block describing event.
- * reference Ignored.
- *
- * Returns :
- * TRUE if we handled it, FALSE if we didn't.
- *
- */
-
- {
- uint more ;
- static t_font_plot_type plot_type = {
- FALSE, /* Graphics justify */
- FALSE, /* Rubout */
- FALSE, /* 2 bits unused */
-
- TRUE, /* OS units */
- FALSE, /* Use Coordinate block */
- FALSE, /* Use transformation matrix */
- FALSE, /* Use length value */
-
- TRUE, /* Use font handle */
- FALSE, /* Do kerning */
- FALSE, /* Write from right to left */
- 0 /* The rest */
- } ;
- t_redraw_block rb ;
-
- reference = reference ;
- WIMP_SetFontColours (8, 0) ;
- rb.handle = poll_block->data.ob.window_handle ;
- if (LOG_OSError (WIMP_RedrawWindow (&rb, &more)) == NULL)
- {
- while (more)
- {
- FONT_Paint (FontDescriptor->handle,
- Text,
- plot_type,
- rb.visible_area.min.x - rb.scroll_offset.x + 25,
- rb.visible_area.max.y - rb.scroll_offset.y - 200,
- NULL,
- NULL,
- 0) ;
- WIMP_GetRectangle (&rb, &more) ;
- }
- }
- return (TRUE) ;
- }
-