create 11 KB
Newer Older
Neil Turton's avatar
Neil Turton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
/* Copyright 1996 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/* Title:   create.c
 * Purpose: create a Menu Object
 * Author:  TGR
 * History: 1-Nov-93: TGR
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include "kernel.h"
#include "swis.h"

#include "const.h"
#include "macros.h"
#include "string32.h"
#include "messages.h"
#include "objects.toolbox.h"
#include "objects.menu.h"
#include "services.h"
#include "debug.h"
#include "mem.h"
#include "twimp.h"
#include "style.h"

#include "Menu.h"
#include "auxiliary.h"
#include "object.h"
#include "task.h"

#include "create.h"

_kernel_oserror *create_menu_aux (MenuTemplate *menu_temp, ObjectID object_id, TaskDescriptor *t, MenuInternal *menu_int, MenuTemplateEntry *temp_entries);
extern _kernel_oserror *create_menu (_kernel_swi_regs *r, TaskDescriptor *t) {

    /*
     * request to create a menu
     * R0 = 0
     * R1 = Object ID
     * R2 = 0  (will be internal handle for other SWIs
     * R3 = wimp task handle of caller (use to identify task descriptor)
     * R4 -> user regs R0-R9
     *      R0 = flags
     *      R1 -> description block
     */

    /*
     * The Toolbox has already checked that this is not just a create
     * call for a shared Object which already exists.
     * We create a new Object, and add it to the list of Objects for this
     * task.
     * We need to remember the ObjectID passed to us by the Toolbox, so
     * that we can identify this Object if we are given an ID from the
     * client's "id block".
     * Note that if any template names are held in the Object, then we
     * create an Object from that template, and store its ID.
     * Note also that the Toolbox has changed the client's R1 to point
     * at an in-core template, if it wasn't already!
     */

   _kernel_swi_regs     *user_regs;
   MenuTemplate         *menu_temp,
                        *new_menu_temp = NULL;
   MenuTemplateEntry    *temp_entries;
   ObjectTemplateHeader *obj_temp_hdr;
   MenuInternal         *menu_int;
   ObjectID              object_id;
   int                   version_no;
   _kernel_oserror      *e;

   user_regs    = (_kernel_swi_regs *) r->r[4];
   obj_temp_hdr = (ObjectTemplateHeader *)user_regs->r[1];
   menu_temp    = (MenuTemplate *) obj_temp_hdr->body;
   object_id    = (ObjectID) r->r[1];
   version_no   = obj_temp_hdr->version;

   if ((menu_int=mem_alloc(sizeof(MenuInternal))) == NULL) {
      return make_error(Menu_AllocFailed,0);
   }
   /* Version 100 menus are not supported AT ALL - none should exist and if they do,
    * they'll crash the application */
   if (version_no == 101) { /* if more versions are added, change to switch statement */
      if ((new_menu_temp = mem_alloc (sizeof (MenuTemplate))) == NULL) {
         mem_freek (menu_int);
         return make_error (Menu_AllocFailed, 0);
      }
      DEBUG debug_output (
         "f",
         "Menu: old version, copying 0x%x (size 0x%x) to 0x%x and 0x%x (size 0x%x) to 0x%x\n",
         menu_temp,
         offsetof (MenuTemplateHeader,show_event),
         new_menu_temp,
         ((char *) menu_temp) + offsetof (MenuTemplateHeader101,num_entries),
         (sizeof (MenuTemplateHeader) - offsetof (MenuTemplateHeader, num_entries)),
         ((char *) new_menu_temp) + offsetof (MenuTemplateHeader,num_entries)
      );
      memcpy (new_menu_temp,
              menu_temp,
              offsetof (MenuTemplateHeader,show_event)
      );
      memcpy (((char *) new_menu_temp) + offsetof (MenuTemplateHeader   ,num_entries),
              ((char *) menu_temp)     + offsetof (MenuTemplateHeader101,num_entries),
              (sizeof (MenuTemplateHeader) - offsetof (MenuTemplateHeader, num_entries))
      );
      new_menu_temp->hdr.show_event = 0;
      new_menu_temp->hdr.hide_event = 0;
      temp_entries = (MenuTemplateEntry *) (((MenuTemplateHeader101 *) menu_temp)+1);
      menu_temp = new_menu_temp;
   } else {
      temp_entries = (MenuTemplateEntry *) (menu_temp + 1);
   }
   DEBUG debug_output ("f","Menu: using first template menu entry at 0x%x\n",temp_entries);

   if ((e = create_menu_aux (menu_temp, object_id, t, menu_int, temp_entries)) != NULL) {
      if (new_menu_temp) mem_freek (new_menu_temp);
      mem_freek(menu_int);
      return e;
   }
   user_regs->r[0] = r->r[1];
   r->r[0] = (int) menu_int;

   if (new_menu_temp) mem_freek (new_menu_temp);

   return NULL;
}


_kernel_oserror *create_menu_aux (MenuTemplate *menu_temp, ObjectID object_id, TaskDescriptor *t, MenuInternal *menu_int, MenuTemplateEntry *temp_entries) {

   _kernel_oserror   *e = NULL;
   _kernel_swi_regs   regs;
   MenuTemplateEntry *temp_entry;
   MenuInternalEntry *int_entry,*int_entries;
   wimp_Menu         *menu;
   wimp_MenuEntry    *menu_entry;
   int                num_entries;
   int                i;
   BOOL               generate_show_event = menu_temp->hdr.flags  & Menu_GenerateShowEvent;
   BOOL               generate_hide_event = menu_temp->hdr.flags  & Menu_GenerateHideEvent;

   num_entries  = menu_temp->hdr.num_entries;

   DEBUG debug_output ("M","Menu: auxiliary create, num_entries = %d\n",num_entries);

   if ((menu=mem_alloc(sizeof(wimp_Menu) + num_entries*sizeof(wimp_MenuEntry))) == NULL) {
      return make_error(Menu_AllocFailed,0);
   }
   menu_int->hdr.wimp_menu = menu;

   DEBUG debug_output ("M","Menu, max title_len = %d, title = '%s'\n",menu_temp->hdr.max_title,menu_temp->hdr.title);
   if (!menu_temp->hdr.title || !menu_temp->hdr.max_title) {
      menu->hdr.title.indirect_text.buffer = NULL;
      menu->hdr.title.indirect_text.buff_len = 0;
   } else if ((menu->hdr.title.indirect_text.buffer = mem_alloc(menu_temp->hdr.max_title)) == NULL) {
      e = make_error(Menu_AllocFailed,0);
      goto clearup5;
   } else if (!string_copy_chk(menu->hdr.title.indirect_text.buffer,menu_temp->hdr.title,menu_temp->hdr.max_title)) {
      e = make_error (Menu_ShortBuffer,0);
      goto clearup4;
   } else {
      menu->hdr.title.indirect_text.valid_string = NULL;
      menu->hdr.title.indirect_text.buff_len = strlen (menu_temp->hdr.title) + 1;
   }
   if (num_entries) {
      if ((int_entries=mem_alloc(num_entries*sizeof(MenuInternalEntry))) == NULL) {
         e = make_error(Menu_AllocFailed,0);
         goto clearup4;
      }
   }
   else
      int_entries = NULL;

   menu_int->hdr.entries      = int_entries;

   if (menu_temp->hdr.max_help) {
     if ((menu_int->hdr.help_message = mem_alloc(menu_temp->hdr.max_help)) == NULL) {
        e = make_error(Menu_AllocFailed,0);
        goto clearup3;
     } else string_copy_chk (menu_int->hdr.help_message,menu_temp->hdr.help_message,menu_temp->hdr.max_help);
   }
   else menu_int->hdr.help_message = NULL;

   menu->hdr.title_fgnd = style_MENU_TITLE_FORECOL;
   menu->hdr.title_bgnd = style_MENU_TITLE_BACKCOL;
   menu->hdr.worka_fgnd = style_MENU_ENTRY_FORECOL;
   menu->hdr.worka_bgnd = style_MENU_ENTRY_BACKCOL;
   menu->hdr.height     = style_MENU_ENTRY_HEIGHT;
   menu->hdr.vert_gap   = style_MENU_ENTRY_GAP;

   menu_int->hdr.object_id    = object_id;
   menu_int->hdr.flags        = (generate_show_event ? MENU_INT_FLAGS_GENERATE_SHOW_EVENT : 0)
                              | (generate_hide_event ? MENU_INT_FLAGS_GENERATE_HIDE_EVENT : 0);
   menu_int->hdr.num_entries  = num_entries;
   menu_int->hdr.title_len    = menu_temp->hdr.max_title;
   menu_int->hdr.help_len     = menu_temp->hdr.max_help;
   menu_int->hdr.show_event   = menu_temp->hdr.show_event;
   menu_int->hdr.hide_event   = menu_temp->hdr.hide_event;

   for (i=0; i<num_entries; i++) {

      DEBUG debug_output ("f", "Menu: entry no. %d\n",i);

      menu_entry  = wimp_menu_entry(menu,i);
      temp_entry  = &(temp_entries[i]); /*menu_template_entry(menu_temp,i);*/
      int_entry   = menu_internal_entry(menu_int,i);

      if ((e = create_menu_entry (menu_entry, temp_entry, int_entry)) != NULL) break;
   }
   if (e) {
      for (i--;i>=0;i--)
         remove_menu_entry(wimp_menu_entry(menu,i),menu_internal_entry(menu_int,i));
      goto clearup2;
   }

   if (num_entries>0) {
232 233
      if (menu->hdr.title.indirect_text.buffer)  /* SJM: 14/06/96: Only set indirect bit if there is a title */
         wimp_menu_entry(menu,0)->flags          |= wimp_MENUFLAGS_TITLE_INDIRECT;
Neil Turton's avatar
Neil Turton committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
      wimp_menu_entry(menu,num_entries-1)->flags |= wimp_MENUFLAGS_LAST_ITEM;
   }
   if ((e = menu_update_size(menu_int)) != NULL) {
      goto clearup1;
   }
   if (t->object_list) { /* If there are already menus attached to the task ... */

      menu_int->hdr.forward                        = t->object_list;
      menu_int->hdr.backward                       = t->object_list->hdr.backward;
      t->object_list->hdr.backward->hdr.forward    = menu_int;
      t->object_list->hdr.backward                 = menu_int;

   } else {              /* ... if not ... */

      /* register interest in toolbox events */

      regs.r[0] = 0;
      regs.r[1] = Menu_PostFilter;
      regs.r[2] = (int) t;
      regs.r[3] = Toolbox_RegisterPostFilter_ToolboxEvent;
      regs.r[4] = (int) toolbox_events_of_interest;

      if ((e = _kernel_swi (Toolbox_RegisterPostFilter, &regs, &regs)) != NULL) {
         goto clearup1;
      }
      /* register interest in wimp events */

      regs.r[0] = 0;
      regs.r[1] = Menu_PostFilter;
      regs.r[2] = (int) t;
      regs.r[3] = Toolbox_RegisterPostFilter_WimpEvent;
      regs.r[4] = (int) events_of_interest;

      if ((e = _kernel_swi (Toolbox_RegisterPostFilter, &regs, &regs)) != NULL) {
         goto clearup1;
      }

      /* register interest in wimp messages */

      regs.r[0] = 0;
      regs.r[1] = Menu_PostFilter;
      regs.r[2] = (int) t;
      regs.r[3] = Toolbox_RegisterPostFilter_WimpMessage;
      regs.r[4] = (int) messages_of_interest;

      if ((e = _kernel_swi (Toolbox_RegisterPostFilter, &regs, &regs)) != NULL) {
         goto clearup1;
      }

      /* register interest in pre-filter */

      regs.r[0] = 0;
      regs.r[1] = Menu_PreFilter;
      regs.r[2] = (int) t;

      if ((e = _kernel_swi (Toolbox_RegisterPreFilter, &regs, &regs)) != NULL) {
         goto clearup1;
      }
      t->object_list         = menu_int;
      menu_int->hdr.forward  = menu_int;
      menu_int->hdr.backward = menu_int;
   }
   return e;

   clearup1:
      for (i=0; i<num_entries; i++) {
         remove_menu_entry(wimp_menu_entry(menu,i),menu_internal_entry(menu_int,i));
      }
   clearup2:
      if (menu_int->hdr.help_message) mem_freek(menu_int->hdr.help_message);
   clearup3:
      mem_freek(int_entries);
   clearup4:
      if (menu->hdr.title.indirect_text.buffer) mem_freek(menu->hdr.title.indirect_text.buffer);
   clearup5:
      mem_freek(menu);
      return e;
}