Object 43.5 KB
Newer Older
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
/* Copyright 1997 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.
 */
/***************************************************/
/* File   : Object.c                               */
/*                                                 */
/* Purpose: Handling OBJECT, APPLET and EMBED.     */
/*                                                 */
/* Author : A.D.Hodgkinson                         */
/*                                                 */
/* History: 05-Oct-97: Created.                    */
/***************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "swis.h"
#include "flex.h"

#include "HTMLLib.h" /* HTML library API, Which will include html2_ext.h, tags.h and struct.h */

#include "wimp.h"
#include "wimplib.h"
#include "event.h"

#include "toolbox.h"

#include "svcprint.h"
#include "Global.h"
#include "FromROSLib.h"
#include "Utils.h"

#include "Browser.h"
46
#include "ChoiceDefs.h"
47 48
#include "Fetch.h" /* (Which itself includes URLstat.h) */
#include "FetchHTML.h"
49
#include "Filetypes.h"
50
#include "Images.h"
51
#include "Memory.h"
52
#include "MimeMap.h"
53
#include "MiscDefs.h"
54 55 56 57
#include "PlugIn.h"
#include "Redraw.h"
#include "Reformat.h"
#include "TokenUtils.h"
58
#include "Toolbars.h"
59 60 61
#include "URLveneer.h"

#include "Object.h"
62 63 64

/* Static function prototypes */

65 66
static int               object_get_token_object    (browser_data * b, HStream * t);
static _kernel_oserror * object_get_object_size     (browser_data * b, int object, BBox * size);
67
static _kernel_oserror * object_set_object_size     (browser_data * b, int object, BBox * size);
68 69 70
static int               object_get_object_position (browser_data * b, int object, int * x, int * y);
static _kernel_oserror * object_get_object_plugin   (browser_data * b, int object, unsigned int * plugin_instance, unsigned int * plugin_task);
static _kernel_oserror * object_set_object_plugin   (browser_data * b, int object, unsigned int plugin_instance, unsigned int plugin_task);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

/*************************************************/
/* object_new_object()                           */
/*                                               */
/* Adds a structure for a new Object to a        */
/* browser's array of Objects.                   */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             to add to;                        */
/*                                               */
/*             Pointer to a token representing   */
/*             the Object (OBJECT, EMBED or      */
/*             APPLET tag).                      */
/*************************************************/

_kernel_oserror * object_new_object(browser_data * b, HStream * t)
{
88 89
  const char * data;
  const char * type;
90 91 92 93

  /* If this isn't an Object token, do nothing (well, */
  /* complain about it in TRACE builds).              */

94
  if (!t || !ISOBJECT(t))
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
  {
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Token %08x passed to object_new_object does not represent an OBJECT, APPLET or EMBED tag",
              (int) t);

      return &erb;

    #endif

    return NULL;
  }

  /* Allocate memory for the item */

  RetError(memory_set_chunk_size(b,
                                 NULL,
                                 CK_OBJB,
                                 (b->nobjects + 1) * sizeof(object_info)));

  /* Fill in the new item */

120 121 122
  b->odata[b->nobjects].plugin_instance_handle = 0;
  b->odata[b->nobjects].plugin_task_handle     = 0;
  b->odata[b->nobjects].token                  = t;
123

124 125 126 127
  /* Until this is seen by the user, open it off-screen to avoid */
  /* having to send out lots of reposition calls during the main */
  /* page reformat.                                              */

128 129
  b->odata[b->nobjects].x                      = -1;
  b->odata[b->nobjects].y                      = -1;
130

131 132 133 134 135 136
  /* Initially set invalid widths / heights so that it must be */
  /* worked out properly later.                                */

  b->odata[b->nobjects].w                      = -1;
  b->odata[b->nobjects].h                      = -1;

137 138
  /* Set up initial flags values */

139 140 141
  b->odata[b->nobjects].isimage                = 0;
  b->odata[b->nobjects].isplugin               = 0;
  b->odata[b->nobjects].broadcast_sent         = 0;
142

143 144 145 146
  /* Finally, increment the objects counter */

  b->nobjects++;

147 148
  /* Now deal with data types we can handle inline */

149 150 151
  data = HtmlOBJECTdata(t);
  type = HtmlOBJECTtype(t);

152
  if (data && *data)
153
  {
154 155
    int filetype;

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    /* If we have a Mime type, use it */

    if (type && *type)
    {
      if (mimemap_mime_to_riscos(type, &filetype)) filetype = FileType_DATA;
    }

    /* Otherwise, try to work it out from the filename extension */

    else
    {
      const char * ext;

      ext = data + strlen(data) - 1;

      while (ext >= data && *ext != '.') ext--;

      if (*ext == '.')
      {
        if (mimemap_extension_to_riscos(ext, &filetype)) filetype = FileType_DATA;
      }
      else filetype = FileType_DATA;
    }
179 180

    /* Is it an image we can handle? */
181

182 183 184 185 186 187 188 189
    if (
         filetype == FileType_PNG  ||
         filetype == FileType_GIF  ||
         filetype == FileType_JPEG ||
         filetype == FileType_TIFF ||
         filetype == FileType_XBM  ||
         filetype == FileType_BMP
       )
190
    {
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
      const char * current;
      const char * newdata;

      /* Must relativise the data URL... */

      current = browser_fetch_url(b);
      if (!current) current = browser_current_url(b);

      if (current)
      {
        newdata = HtmlRelativiseURL(current, data, t);

        if (newdata)
        {
          /* If we've managed all that, call image_new_image */

          RetError(image_new_image(b,
                                   newdata,
                                   t,
                                   0));
211

212
          b->odata[b->nobjects - 1].isimage = 1;
213

214 215 216
          return NULL;
        }
      }
217 218 219
    }
  }

220 221
  /* Deal with external objects */

222 223 224
  else data = HtmlOBJECTclassid(t);

  if (data && *data)
225
  {
226 227 228 229 230 231 232 233 234 235 236
    int cannot_handle = 0;

    /* Well OK, this looks like a Plug-In. But can we handle it? */
    /* First, check for Active-X.                                */

    if (!strncmp(data, "clsid:", 6)) cannot_handle = 1;

    /* Otherwise, check a Plug-In exists to handle the data type */

    else
    {
237 238
      const char * type = HtmlOBJECTcodetype(t);
      const char * ext  = strrchr(data, '.');
239

240
      /* Can't handle it if there's no extension or code type */
241

242
      if (!ext && (!type || (type && !*type))) cannot_handle = 1;
243 244 245 246 247 248
      else
      {
        int filetype;

        /* Can't handle it if we don't have a Mime map entry for it */

249 250 251 252
        if (type)
        {
          if (mimemap_mime_to_riscos(type, &filetype)) cannot_handle = 1;
        }
253
        else
254 255 256 257 258
        {
          if (mimemap_extension_to_riscos(ext, &filetype)) cannot_handle = 1;
        }

        if (!cannot_handle)
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
        {
          char             var[32];
          _kernel_swi_regs r;

          /* Final check is to see if there's an Alias$@PlugInType_xxx variable */

          sprintf(var, "Alias$@PlugInType_%03X", filetype);

          r.r[0] = (int) var;
          r.r[1] = (int) NULL;
          r.r[2] = -1;
          r.r[3] = 0;
          r.r[4] = 0;

          /* *Not* _swix, we want the R2 value of 0 for 'unset'... */

          _kernel_swi(OS_ReadVarVal, &r, &r);

          if (!r.r[2]) cannot_handle = 1;
        }
      }
    }

    /* If we can handle it, flag this as a Plug-In */

    if (!cannot_handle) b->odata[b->nobjects - 1].isplugin = 1;

    /* Otherwise, use the alternative HTML stream here */

    else
    {
      int          depth, line, chunk;
      token_path * path = NULL;

      // For now - security blanket...

      /* Can't do anything if the next token has been preprocessed [somehow] */

      if (t->next && t->next->flags == HFlags_DealtWithToken)
      {
        #ifdef TRACE

          erb.errnum = Utils_Error_Custom_Normal;
302

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
          StrNCpy0(erb.errmess,
                   "Can't replace this object tag as later tokens have already been preprocessed, in object_new_object");

          show_error_ret(&erb);

        #else

          t = t;

        #endif
      }
      else
      {
        /* What line was the token in? */

        depth = tokenutils_line_range(b,
                                      t,
                                      &line,
                                      &chunk,
                                      NULL,
                                      NULL,
                                      &path);

        if (line >= 0)
        {
          /* If the line was found and we have a depth, the line */
          /* is in a table - find the base parent line of the    */
          /* table, so we can reformat it.                       */

          if (depth) line = path[depth - 1].line;
        }

        if (path) free(path);

        /* Right, only have to reformat if the token was in the */
        /* line list.                                           */

        if (line >= 0) reformat_format_from(b, line - 1, 0, -1);

        /* We now know that the object is no longer in any line */
        /* list, so it's safe to preprocess the alternate HTML  */
        /* stream and place that under the token.               */

        t->flags |= HFlags_IgnoreObject;

        /* Discard the new object */

        b->nobjects --;

        memory_set_chunk_size(b,
                              NULL,
                              CK_OBJB,
                              (b->nobjects + 1) * sizeof(object_info));

        if (HtmlOBJECTstream(t))
        {

// Eeek! Recursive calls back to this routine!
//
//            HStream * alt = HtmlOBJECTstream(t);
//
//            /* Preprocess the stream */
//
//            // Of course, this probably breaks forms if extra form fields
//            // are present as part of an ongoing FORM tag.
//
//            while (alt)
//            {
//              fetch_preprocess_token(b, alt);
//
//              alt = alt->next;
//            }
//
          /* Move it into the main token list */

          HtmlReplaceOBJECT(t);

          /* That's it; any subsequent reformatting takes care of the rest */

          return NULL;
        }
      }
    }
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
  }

  /* If this is a Plug-In, and we're supposed to launch them */
  /* as soon as possible, then launch it now.                */

  if (
       b->odata[b->nobjects - 1].isplugin &&
       choices.support_object    &&
       choices.plugin_control == Choices_PlugIns_ASAP
     )
  {
    BBox position;

    RetError(object_get_object_size(b, b->nobjects - 1, &position));

    /* Object position is currently invalid, so hide the Plug-In */
    /* by moving it off the top of the work area.                */

    position.xmax = (position.xmax - position.xmin);
    position.xmin = 0;
    position.ymax = (position.ymax - position.ymin) + 0x1000; /* 0x1000 to be (very) safe */
    position.ymin = 0x1000;

    b->odata[b->nobjects - 1].broadcast_sent = 1; /* Don't try over and over if the first gives an error */

    RetError(plugin_add_queue_item(b, t, &position));
412
  }
413

414 415 416 417 418 419 420 421 422 423 424 425 426 427
  return NULL;
}

/*************************************************/
/* object_discard()                              */
/*                                               */
/* Discards all Objects held by a given browser. */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the Objects.          */
/*************************************************/

_kernel_oserror * object_discard(browser_data * b)
{
428
  _kernel_oserror * e = NULL;
429
  _kernel_oserror * local;
430 431 432 433 434 435 436 437 438 439 440 441
  int               i;

  /* Close down any Plug-Ins */

  for (i = 0; i < b->nobjects; i++)
  {
    if (
         b->odata[i].isplugin       &&
         b->odata[i].broadcast_sent &&
         b->odata[i].plugin_task_handle
       )
    {
442 443 444 445 446 447 448 449 450 451
      unsigned int instance;

      e = plugin_obtain_instance(b, b->odata[i].token, &instance);

      if (!e)
      {
        e = plugin_send_close(instance,
                              b->odata[i].plugin_task_handle,
                              b->odata[i].plugin_instance_handle);
      }
452 453 454
    }
  }

455 456
  plugin_flush_instance_entries(b);

457 458 459 460 461 462 463
  /* Only set 'e' from the queue flush if the above call didn't */
  /* generate an error.                                         */

  local = plugin_flush_queue(b, 1);
  if (!e) e = local;

  /* Remove the Objects themselves. */
464

465 466
  b->nobjects = 0;

467 468 469
  memory_set_chunk_size(b, NULL, CK_OBJB, 0);

  return e;
470 471 472 473 474 475 476 477 478
}

/*************************************************/
/* object_get_token_object()                     */
/*                                               */
/* Return the number of the Object represented   */
/* in the given browser by the given token.      */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
479
/*             relevant to the Object;           */
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
/*                                               */
/*             Pointer to a token representing   */
/*             the Object (OBJECT, EMBED or      */
/*             APPLET tag).                      */
/*                                               */
/* Returns:    Number of the Object from 0 to    */
/*             number of Objects minus 1, or -1  */
/*             if the Object cannot be found.    */
/*************************************************/

static int object_get_token_object(browser_data * b, HStream * t)
{
  int found = -1;
  int i;

  /* If this isn't an Object token, do nothing (well, */
  /* complain about it in TRACE builds).              */

498
  if (!t || !ISOBJECT(t))
499 500 501
  {
    #ifdef TRACE

502
      if (!ISOBJECT(t))
503 504
      {
        erb.errnum = Utils_Error_Custom_Normal;
505

506 507 508
        sprintf(erb.errmess,
                "Token %08x passed to object_get_token_object does not represent an OBJECT, APPLET or EMBED tag",
                (int) t);
509

510 511
        show_error_ret(&erb);
      }
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549

    #endif

    return -1;
  }

  /* Otherwise, try to find the item */

  for (i = 0; i < b->nobjects; i++)
  {
    if (b->odata[i].token == t)
    {
      found = i;
      break;
    }
  }

  return found;
}

/*************************************************/
/* object_get_object_size()                      */
/*                                               */
/* Returns the size of a given Object.           */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             to add to;                        */
/*                                               */
/*             Number of the Object, from 0 to   */
/*             number of Objects minus 1;        */
/*                                               */
/*             Pointer to a BBox, in which the   */
/*             size of the Object will be        */
/*             written.                          */
/*************************************************/

static _kernel_oserror * object_get_object_size(browser_data * b, int object, BBox * size)
{
550 551
  HStream * tp;

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
  /* Can't do anything without a bounding box! */

  if (!size)
  {
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;
      strcpy(erb.errmess, "Null bounding box pointer passed to object_get_object_size");
      return &erb;

    #endif

    return NULL;
  }

  /* Fill in zeros to start with */

  size->xmin = size->ymin = 0;
  size->xmax = size->ymax = 0;

  /* Is this a valid object number? */

  if (object < 0 || object >= b->nobjects)
  {
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Invalid object number %d for passed to object_get_object_size (browser %08x, nobjects = %d)",
              object,
              (int) b,
              b->nobjects);

      return &erb;

    #endif

    return NULL;
  }

593 594
  /* If this is an image, return through the image sizing routines */

595 596 597 598 599 600
  if (b->odata[object].isimage)
  {
    return image_get_token_image_size(b,
                                      b->odata[object].token,
                                      size);
  }
601

602 603
  /* Find its size */

604 605
  tp = b->odata[object].token;

606 607 608
  /* If we haven't already, work this out */

  if (b->odata[object].w < 0 || b->odata[object].h < 0)
609
  {
610 611 612
    int w, h;
    int available_w = redraw_display_width (b, b->cell);
    int available_h = redraw_display_height(b, b->cell);
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639

    /* Deal with % specifiers as well as pixels */

    if (OBJECT_HAS_WIDTH(tp))
    {
      switch (OBJECT_WIDTH_UNITS(tp))
      {
        default:
        case UNITS_PIXELS:  w = OBJECT_WIDTH(tp) * 2; break;
        case UNITS_PERCENT: w = available_w * OBJECT_WIDTH(tp) / 100; break;
      }
    }
    else w = 0;

    if (OBJECT_HAS_HEIGHT(tp))
    {
      switch (OBJECT_HEIGHT_UNITS(tp))
      {
        default:
        case UNITS_PIXELS:  h = OBJECT_HEIGHT(tp) * 2; break;
        case UNITS_PERCENT: h = available_h * OBJECT_HEIGHT(tp) / 100; break;
      }
    }
    else h = 0;

    size->xmax = w;
    size->ymax = h;
640 641 642 643 644 645 646 647 648 649 650

    b->odata[object].w = w;
    b->odata[object].h = h;
  }

  /* Otherwise, get the prestored value */

  else
  {
    size->xmax = b->odata[object].w;
    size->ymax = b->odata[object].h;
651 652
  }

653 654 655 656
  /* Don't allow zero in any direction */

  if (size->xmax == 0 || size->ymax == 0)
  {
657 658 659
    const char * text = HtmlOBJECTstandby(b->odata[object].token);

    reformat_get_placeholder_size(b, b->odata[object].token, text, size);
660 661 662

    b->odata[object].w = size->xmax - size->xmin;
    b->odata[object].h = size->ymax - size->ymin;
663 664
  }

665 666 667 668 669 670 671 672 673
  return NULL;
}

/*************************************************/
/* object_get_token_object_size()                */
/*                                               */
/* Returns the size of a given Object.           */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
674
/*             relevant to the Object;           */
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
/*                                               */
/*             Pointer to a token representing   */
/*             the Object (OBJECT, EMBED or      */
/*             APPLET tag);                      */
/*                                               */
/*             Pointer to a BBox, in which the   */
/*             size of the Object will be        */
/*             written.                          */
/*************************************************/

_kernel_oserror * object_get_token_object_size(browser_data * b, HStream * t, BBox * size)
{
  return object_get_object_size(b, object_get_token_object(b, t), size);
}

690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
/*************************************************/
/* object_set_object_size()                      */
/*                                               */
/* Sets the size of a given Object.              */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the Object;           */
/*                                               */
/*             Number of the Object, from 0 to   */
/*             number of Objects minus 1;        */
/*                                               */
/*             Pointer to a BBox, from which the */
/*             xmax - xmin and ymax - ymin       */
/*             values are used to set the size.  */
/*************************************************/

static _kernel_oserror * object_set_object_size(browser_data * b, int object, BBox * size)
{
708
  int width, height;
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786

  /* Can't do anything without a bounding box! */

  if (!size)
  {
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;
      strcpy(erb.errmess, "Null bounding box pointer passed to object_get_object_size");
      return &erb;

    #endif

    return NULL;
  }

  /* Work out the width and height */

  width  = size->xmax - size->xmin;
  height = size->ymax - size->ymin;

  /* Is this a valid object number? */

  if (object < 0 || object >= b->nobjects)
  {
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Invalid object number %d for passed to object_get_object_size (browser %08x, nobjects = %d)",
              object,
              (int) b,
              b->nobjects);

      return &erb;

    #endif

    return NULL;
  }

  /* If this is an image, need to set the image's size */

  if (b->odata[object].isimage) RetError(image_set_token_image_size(b,
                                                                    b->odata[object].token,
                                                                    size));

  /* Set also the width and height values for the Object */

  b->odata[object].w = width;
  b->odata[object].h = height;

  return NULL;
}

/*************************************************/
/* object_set_token_object_size()                */
/*                                               */
/* Sets the size of a given Object.              */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the Object;           */
/*                                               */
/*             Pointer to a token representing   */
/*             the Object (OBJECT, EMBED or      */
/*             APPLET tag);                      */
/*                                               */
/*             Pointer to a BBox, from which the */
/*             xmax - xmin and ymax - ymin       */
/*             values are used to set the size.  */
/*************************************************/

_kernel_oserror * object_set_token_object_size(browser_data * b, HStream * t, BBox * size)
{
  return object_set_object_size(b, object_get_token_object(b, t), size);
}

787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
/*************************************************/
/* object_get_object_position()                  */
/*                                               */
/* Returns the x and y fields of the object_info */
/* structure for a given Object.                 */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the Object;           */
/*                                               */
/*             Number of the Object;             */
/*                                               */
/*             Pointer to an int, in which the   */
/*             X coordinate is returned;         */
/*                                               */
/*             Pointer to an int, in which the   */
/*             Y coordinate is returned.         */
/*                                               */
/* Returns:    1 if the object number is invalid */
/*             or 0 for success.                 */
/*                                               */
/* Assumes:    Neither pointer is NULL.          */
/*************************************************/

static int object_get_object_position(browser_data * b, int object, int * x, int * y)
{
  if (object < 0 || object >= b->nobjects) return 1;

  *x = b->odata[object].x;
  *y = b->odata[object].y;

  return 0;
}

820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
/*************************************************/
/* object_get_token_object_position()            */
/*                                               */
/* Returns the x and y fields of the object_info */
/* structure for a given Object.                 */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the Object;           */
/*                                               */
/*             Pointer to the token representing */
/*             the Object;                       */
/*                                               */
/*             Pointer to an int, in which the   */
/*             X coordinate is returned;         */
/*                                               */
/*             Pointer to an int, in which the   */
/*             Y coordinate is returned.         */
/*                                               */
/* Returns:    1 if the object could not be      */
/*             found from the given token, or 0  */
/*             for success.                      */
/*                                               */
/* Assumes:    Neither pointer is NULL.          */
/*************************************************/

int object_get_token_object_position(browser_data * b, HStream * t, int * x, int * y)
{
  int object = object_get_token_object(b, t);

849
  return object_get_object_position(b, object, x, y);
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
}

/*************************************************/
/* object_set_token_object_position()            */
/*                                               */
/* Sets the x and y fields of the object_info    */
/* structure for a given Object, so that it may  */
/* be [partially] plotted during a fetch.        */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the object;           */
/*                                               */
/*             Pointer to the token representing */
/*             the Object;                       */
/*                                               */
/*             X coordinate (window coords);     */
/*                                               */
/*             Y coordinate (window coords).     */
/*                                               */
/* Returns:    Number of the Object that was     */
/*             changed, or -1 if none could be   */
/*             found for the given token.        */
/*************************************************/

int object_set_token_object_position(browser_data * b, HStream * t, int x, int y)
{
  int object = object_get_token_object(b, t);

878 879 880 881 882 883 884 885 886
  #ifdef TRACE
    if (tl & (1u<<30))
    {
      char debugbuf[1024];
      sprintf(debugbuf,"object_set_token_object_positon: Called for %p, %p at %d, %d",b,t,x,y);
      Printf("%s\n",debugbuf);
    }
  #endif

887 888 889 890
  /* If an object was found, set the x and y coordinates */

  if (object >= 0)
  {
891 892 893 894 895
    int oldx, oldy;

    oldx = b->odata[object].x;
    oldy = b->odata[object].y;

896 897 898 899 900 901 902 903 904 905 906 907
    /* *Don't* invalidate x and y positions for Objects; this causes */
    /* too many reshape messages for some PlugIns and can lead to    */
    /* items left with out-of-date position information.             */
    /*                                                               */
    /* Objects are initialised to be in an invalidated position, of  */
    /* course, so still need to check for that later in the code.    */

    if (x != -1 && y != -1)
    {
      b->odata[object].x = x;
      b->odata[object].y = y;
    }
908 909 910 911 912

    /* If this is is acting as an image, need to update */
    /* the image structure too.                         */

    if (b->odata[object].isimage) image_set_token_image_position(b, t, x, y);
913 914 915 916 917 918 919 920 921

    /* If this item is a Plug-In, this could be the first time we've */
    /* known for sure what position the item should be plotted at;   */
    /* pr we may want to tell the Plug-In to move to a new position. */

    if (b->odata[object].isplugin)
    {
      BBox position;

922 923 924 925
      #ifdef TRACE
        if (tl & (1u<<30)) Printf("object_set_token_object_positon: Is Plug-In\n");
      #endif

926 927 928 929
      show_error_ret(object_get_object_size(b, object, &position));

      /* If this is the first time we've tried to set a position for */
      /* the Object and a broadcast to start up an associated        */
930 931
      /* Plug-In has not yet been sent, add the Plug-In to the       */
      /* 'ready to launch' queue.                                    */
932 933 934

      if (!b->odata[object].broadcast_sent)
      {
935 936
        int start_now = 0;

937
        /* Only do this for valid Object positions */
938

939
        if (x >= 0)
940 941 942 943 944 945 946
        {
          /* Object position is valid, we can place it in the right position initially */

          position.xmin += x;
          position.ymin += y;
          position.xmax += x;
          position.ymax += y;
947 948 949 950 951 952 953 954 955 956 957 958 959

          if (choices.plugin_control != Choices_PlugIns_Never && choices.support_object) start_now = 1;
        }

        if (start_now)
        {
          b->odata[object].broadcast_sent = 1; /* Don't try over and over if the first gives an error */

          #ifdef TRACE
            if (tl & (1u<<30)) Printf("object_set_token_object_positon: Starting Plug-In\n");
          #endif

          show_error_ret(plugin_add_queue_item(b, t, &position));
960 961
        }

962 963 964
        #ifdef TRACE

          else if (tl & (1u<<30)) Printf("object_set_token_object_position: Choices say not to start Plug-In yet\n");
965

966
        #endif
967 968 969 970 971 972 973 974 975 976 977 978 979
      }

      /* If the Plug-In has been started, and the position of the Object */
      /* has changed, tell the Plug-In to move.                          */

      else if (
                (
                  oldx != x ||
                  oldy != y
                )
                && b->odata[object].plugin_task_handle
              )
      {
980 981
        unsigned int instance;

982 983
        if (x < 0)
        {
984
          /* Object position is invalid */
985

986 987 988 989
          position.xmax = (position.xmax - position.xmin);
          position.xmin = 0;
          position.ymax = (position.ymax - position.ymin) + 0x1000; /* 0x1000 to be (very) safe */
          position.ymax = 0x1000;
990 991 992 993 994 995 996 997 998 999 1000 1001 1002
        }
        else
        {
          /* Object position has been updated */

          position.xmin += x;
          position.ymin += y;
          position.xmax += x;
          position.ymax += y;
        }

        /* Send the reshape message */

1003 1004 1005 1006
        #ifdef TRACE
          if (tl & (1u<<30)) Printf("object_set_token_object_positon: Moving Plug-In\n");
        #endif

1007 1008 1009 1010 1011 1012 1013
        if (!plugin_obtain_instance(b, b->odata[object].token, &instance))
        {
          show_error_ret(plugin_send_original_reshape(instance,
                                                      b->odata[object].plugin_task_handle,
                                                      b->odata[object].plugin_instance_handle,
                                                      &position));
        }
1014 1015
      }
    }
1016 1017
  }

1018 1019 1020 1021
  #ifdef TRACE
    if (tl & (1u<<30)) Printf("object_set_token_object_positon: Successful, returning Object number %d\n",object);
  #endif

1022 1023 1024
  return object;
}

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
/*************************************************/
/* object_return_info()                          */
/*                                               */
/* Return information on an Object identified by */
/* a Plug-In instance handle.                    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             holding the Objects to search;    */
/*                                               */
/*             Plug-In instance handle;          */
/*                                               */
/*             Pointer to an HStream *, in which */
/*             the token the Object is represen- */
/*             ting is written;                  */
/*                                               */
/*             Pointer to an int, in which the   */
/*             Plug-In task handle for this      */
/*             Object is written.                */
/*                                               */
/* Returns:    Returns 1 if the item was found,  */
/*             else 0. If 0, the contents of the */
/*             given pointers are not changed.   */
/*                                               */
/* Assumes:    Either of the last two pointers   */
/*             may be NULL.                      */
/*************************************************/

int object_return_info(browser_data * b, unsigned int plugin_instance_handle, HStream ** token, unsigned int * plugin_task)
{
  int i;

  for (i = 0; i < b->nobjects; i++)
  {
    if (b->odata[i].plugin_instance_handle == plugin_instance_handle)
    {
      /* Found it */

      if (token)       *token       = b->odata[i].token;
      if (plugin_task) *plugin_task = b->odata[i].plugin_task_handle;

      return 1;
    }
  }

  /* Found nothing */

  return 0;
}

1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
/*************************************************/
/* object_get_token_object_box()                 */
/*                                               */
/* Returns a bounding box filled in with parent  */
/* window work area coordinates for a given      */
/* Object.                                       */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the Object;           */
/*                                               */
/*             Pointer to the token representing */
/*             the Object;                       */
/*                                               */
/*             Pointer to a BBox to return the   */
/*             work area coordinates describing  */
/*             the Object's size.                */
/*                                               */
/* Returns:    1 for failure, 0 for success.     */
/*************************************************/

int object_get_token_object_box(browser_data * b, HStream * t, BBox * box)
{
  int object = object_get_token_object(b, t);
  int x, y;

  if (object < 0 || object >= b->nobjects || !box) return 1;

  /* Get the Object bounding box */

  if (object_get_object_size(b, object, box)) return 1;

  /* Get the Object position */

  if (object_get_object_position(b, object, &x, &y)) return 1;

  /* Return if the Object position is invalidated at the moment */

  if (x < 0) return 1;

  /* Otherwise add the coordinates */

  box->xmin += x;
  box->ymin += y;
  box->xmax += x;
  box->ymax += y;

  return 0;
}

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
/*************************************************/
/* object_get_object_plugin()                    */
/*                                               */
/* Returns details of any Plug-In associated     */
/* with a given Object (referenced by number).   */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the Object;           */
/*                                               */
/*             Number of the Object;             */
/*                                               */
/*             Pointer to an int, in which the   */
/*             Plug-In instance handle will be   */
/*             written;                          */
/*                                               */
/*             Pointer to an int, in which the   */
/*             Plug-In task handle will be       */
/*             written, or 0 if the Object       */
/*             does not have an associated       */
/*             Plug-In or can't be found.        */
/*                                               */
/* Assumes:    Either pointer may be NULL.       */
/*************************************************/

static _kernel_oserror * object_get_object_plugin(browser_data * b, int object, unsigned int * plugin_instance, unsigned int * plugin_task)
{
  /* Fill in zeros to start with */

  if (plugin_instance) *plugin_instance = 0;
  if (plugin_task)     *plugin_task     = 0;

  /* Is this a valid object number? */

  if (object < 0 || object >= b->nobjects)
  {
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Invalid object number %d passed to object_get_token_object_plugin (browser %08x, nobjects = %d)",
              object,
              (int) b,
              b->nobjects);

      return &erb;

    #endif

    return NULL;
  }

  /* If not associated with a Plug-In, exit */

  if (!b->odata[object].isplugin) return NULL;

  /* Otherwise, return the details */

  if (plugin_instance) *plugin_instance = b->odata[object].plugin_instance_handle;
  if (plugin_task)     *plugin_task     = b->odata[object].plugin_task_handle;

  /* Finished */

  return NULL;
}

/*************************************************/
/* object_get_token_object_plugin()              */
/*                                               */
/* Returns details of any Plug-In associated     */
/* with a given Object (referenced by token).    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the Object;           */
/*                                               */
/*             Pointer to the token representing */
/*             the Object;                       */
/*                                               */
/*             Pointer to an int, in which the   */
/*             Plug-In instance handle will be   */
/*             written;                          */
/*                                               */
/*             Pointer to an int, in which the   */
/*             Plug-In task handle will be       */
/*             written, or 0 if the Object       */
/*             does not have an associated       */
/*             Plug-In or can't be found.        */
/*                                               */
/* Assumes:    Either pointer may be NULL.       */
/*************************************************/

_kernel_oserror * object_get_token_object_plugin(browser_data * b, HStream * t, unsigned int * plugin_instance, unsigned int * plugin_task)
{
  return object_get_object_plugin(b,
                                  object_get_token_object(b, t),
                                  plugin_instance,
                                  plugin_task);
}

/*************************************************/
/* object_set_object_plugin()                    */
/*                                               */
/* Sets details of any Plug-In associated with   */
/* a given Object (referenced by number).        */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the Object;           */
/*                                               */
/*             Number of the Object;             */
/*                                               */
/*             Plug-In instance handle to set;   */
/*                                               */
/*             Plug-In task handle to set.       */
/*                                               */
/* Assumes:    Either pointer may be NULL.       */
/*************************************************/

static _kernel_oserror * object_set_object_plugin(browser_data * b, int object, unsigned int plugin_instance, unsigned int plugin_task)
{
  /* Is this a valid object number? */

  if (object < 0 || object >= b->nobjects)
  {
    #ifdef TRACE

      erb.errnum = Utils_Error_Custom_Normal;

      sprintf(erb.errmess,
              "Invalid object number %d for passed to object_set_token_object_plugin (browser %08x, nobjects = %d)",
              object,
              (int) b,
              b->nobjects);

      return &erb;

    #endif

    return NULL;
  }

  /* If not associated with a Plug-In, exit */

  if (!b->odata[object].isplugin) return NULL;

  /* Otherwise, set the details */

  b->odata[object].plugin_instance_handle = plugin_instance;
  b->odata[object].plugin_task_handle     = plugin_task;

  /* Finished */

  return NULL;
}

/*************************************************/
/* object_set_token_object_plugin()              */
/*                                               */
/* Sets details of any Plug-In associated with   */
/* a given Object (referenced by token).         */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
1284
/*             relevant to the Object;           */
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
/*                                               */
/*             Pointer to the token representing */
/*             the Object (usually have to       */
/*             remember this when the initial    */
/*             Message_PlugIn_Open is sent);     */
/*                                               */
/*             Plug-In instance handle to set;   */
/*                                               */
/*             Plug-In task handle to set.       */
/*                                               */
/* Assumes:    Either pointer may be NULL.       */
/*************************************************/

1298
_kernel_oserror * object_set_token_object_plugin(browser_data * b, HStream * t, unsigned int plugin_instance, unsigned int plugin_task)
1299 1300 1301 1302 1303 1304 1305
{
  return object_set_object_plugin(b,
                                  object_get_token_object(b, t),
                                  plugin_instance,
                                  plugin_task);
}

1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
/*************************************************/
/* object_redraw()                               */
/*                                               */
/* Does a high level redraw of an Object, using  */
/* an outline to show where the Object should be */
/* if it isn't plotted by some other method.     */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the Object;           */
/*                                               */
/*             Pointer to a RedrawWindowBlock    */
/*             struct which holds information    */
/*             about the current redraw session; */
/*                                               */
/*             Address of the token representing */
/*             the OBJECT, APPLET or EMBED tag;  */
/*                                               */
/*             The X offset in window coords (so */
/*             OS units) of the left hand edge   */
/*             of the Object;                    */
/*                                               */
/*             The Y offset in window coords (so */
/*             OS units) of the bottom edge of   */
/*             the Object.                       */
/*************************************************/

_kernel_oserror * object_redraw(browser_data * b, WimpRedrawWindowBlock * r, HStream * token, int x, int y)
{
1334 1335 1336
  _kernel_oserror * e       = NULL;
  int               object  = object_get_token_object(b, token);
  int               plotted = 0;
1337

1338 1339 1340 1341 1342
  if (b->odata[object].isimage)
  {
    e = image_redraw(b, r, token, x, y);
    if (!e) plotted = 1;
  }
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358

  if (!plotted)
  {
    /* Plot a placeholder */

    BBox box;

    object_get_object_size(b, object, &box);

    box.xmin += x;
    box.ymin += y;
    box.xmax += x - box.xmin;
    box.ymax += y - box.ymin;

    box.xmin &= ~(wimpt_dx() - 1);
    box.ymin &= ~(wimpt_dy() - 1);
1359

1360 1361 1362 1363 1364
    redraw_draw_placeholder(b,
                            r,
                            &box,
                            token,
                            HtmlOBJECTstandby(token));
1365 1366
  }

1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
  return e;
}

/*************************************************/
/* object_token_is_image()                       */
/*                                               */
/* Finds out whether an Object is acting as an   */
/* an inline image or not.                       */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the object;           */
/*                                               */
/*             Pointer to the token representing */
/*             the Object.                       */
/*                                               */
/* Returns:    1 if the Object is acting as an   */
/*             inline image, else 0.             */
/*************************************************/

int object_token_is_image(browser_data * b, HStream * t)
{
  int object = object_get_token_object(b, t);

  if (object < 0 || object >= b->nobjects) return 0;
  else                                     return !!b->odata[object].isimage;
1392
}