home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / dreamscape / source / Dreamscape / Sources / objects / c / task
Encoding:
Text File  |  1996-09-27  |  3.9 KB  |  157 lines

  1.  
  2. /* objects.c.task
  3.  *
  4.  * Dreamscape - C++ class library for RISC OS
  5.  * Copyright (c) 1996 Mark Seaborn <mseaborn@argonet.co.uk>
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Library General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2 of the License, or (at your option) any later version.
  11.  * See the Dreamscape documentation for more information.
  12.  */
  13.  
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "OS:toolbox.h"
  17. #include "OS:messagetrans.h"
  18. #include "OS:hourglass.h"
  19. #include "OS:wimp.h"
  20.  
  21. #include "task.h"
  22. #include "tboxevent.h"
  23. #include "wimpevent.h"
  24. #include "wimpmsg.h"
  25. #include "x.h"
  26.  
  27.  
  28. toolbox_block dscape_task_ids_;
  29. messagetrans_control_block dscape_task_messages_;
  30. osspriteop_area *dscape_task_sprites_;
  31. wimp_t dscape_task_handle_ = 0;
  32.  
  33. static char *temp_message = 0;
  34.  
  35. static void error_handler(const toolbox_action *event,
  36.     const toolbox_block *ids);
  37. static bool quit_handler(const wimp_message *message, void *handle);
  38. static void error_reporter(const char *error);
  39.  
  40. void dscape_task_poll(void)
  41. {
  42.   wimp_block event;
  43.   wimp_event_no id;
  44.   x_declare(error);
  45.   dscape_task_ensure();
  46.  
  47.   id = wimp_poll(0, &event, 0);
  48.   x_try {
  49.     hourglass_on();
  50.     dscape_wimpevent_dispatch_event(&event, &dscape_task_ids_, id);
  51.     hourglass_off();
  52.   }
  53.   x_catch(error) {
  54.     hourglass_smash();
  55.     dscape_task_report_error(error->errmess);
  56.   }
  57. }
  58.  
  59. void dscape_task_poll_forever(void)
  60. {
  61.   wimp_block event;
  62.   wimp_event_no id;
  63.   x_declare(error);
  64.   dscape_task_ensure();
  65.  
  66.   while(1) {
  67.     id = wimp_poll(0, &event, 0);
  68.     x_try {
  69.       hourglass_on();
  70.       dscape_wimpevent_dispatch_event(&event, &dscape_task_ids_, id);
  71.       hourglass_off();
  72.     }
  73.     x_catch(error) {
  74.       hourglass_smash();
  75.       dscape_task_report_error(error->errmess);
  76.     }
  77.   }
  78. }
  79.  
  80. void dscape_task_ensure(void)
  81. {
  82.   if(!dscape_task_handle_) {
  83.     int all_events = 0;
  84.     dscape_task_handle_ = toolbox_initialise(0, 310, (wimp_message_list *)
  85.     &all_events, (toolbox_action_list *) &all_events,
  86.     dscape_task_directory_, &dscape_task_messages_, &dscape_task_ids_, 0,
  87.     &dscape_task_sprites_);
  88.     dscape_tboxevent_register_s_handler(action_ERROR, error_handler);
  89.     dscape_wimpmsg_register_handler(message_QUIT, quit_handler, 0);
  90.     x_error_reporter = error_reporter;
  91.   }
  92. }
  93.  
  94. void dscape_task_quit(void)
  95. {
  96.   hourglass_off();
  97.   exit(0);
  98. }
  99.  
  100. void dscape_task_report_error(const char *error)
  101. {
  102.   x_report_error(error);
  103. }
  104.  
  105. static void error_reporter(const char *error)
  106. {
  107.   os_error block;
  108.   const char *task_name = messagetrans_lookup(&dscape_task_messages_,
  109.                 "_TaskName", 0, 0, 0, 0, 0, 0, 0),
  110.          *task_sprite = messagetrans_lookup(&dscape_task_messages_,
  111.                 "_TaskSprite", 0, 0, 0, 0, 0, 0, 0);
  112.   block.errnum = 0;
  113.   strncpy(block.errmess, error, 250)[250] = 0;
  114.   wimp_report_error_by_category(&block, wimp_ERROR_BOX_OK_ICON |
  115.     wimp_ERROR_BOX_GIVEN_CATEGORY | (wimp_ERROR_BOX_CATEGORY_ERROR <<
  116.     wimp_ERROR_BOX_CATEGORY_SHIFT), task_name, task_sprite,
  117.     (osspriteop_area *) 1, 0);
  118. }
  119.  
  120. int dscape_task_msgcpy4(const char *token, const char *arg0,
  121.     const char *arg1, const char *arg2, const char *arg3,
  122.     char *buffer, int size)
  123. {
  124.   if(buffer) {
  125.     int used;
  126.     messagetrans_lookup(&dscape_task_messages_, token, buffer, size,
  127.     arg0, arg1, arg2, arg3, &used);
  128.     return used;
  129.   }
  130.   else {
  131.     return 256;
  132.   }
  133. }
  134.  
  135. const char *dscape_task_msg4(const char *token, const char *arg0,
  136.     const char *arg1, const char *arg2, const char *arg3)
  137. {
  138.   int used;
  139.   if(temp_message) free(temp_message);
  140.   temp_message = malloc(256);
  141.   messagetrans_lookup(&dscape_task_messages_, token, temp_message, 256,
  142.     arg0, arg1, arg2, arg3, &used);
  143.   return temp_message = realloc(temp_message, used+1);
  144. }
  145.  
  146. static void error_handler(const toolbox_action *event,
  147.     const toolbox_block *ids)
  148. {
  149.   dscape_task_report_error(event->data.error.errmess);
  150. }
  151.  
  152. static bool quit_handler(const wimp_message *message, void *handle)
  153. {
  154.   dscape_task_quit();
  155.   return 1;
  156. }
  157.