ItemInfo 24 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 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 232 233 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 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 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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 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 550 551 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 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 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 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 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 787 788 789 790 791 792 793
/* Copyright 1998 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   : ItemInfo.c                             */
/*                                                 */
/* Purpose: Functions relating to the Item         */
/*          Information window.                    */
/*                                                 */
/* Author : A.D.Hodgkinson                         */
/*                                                 */
/* History: 11-Mar-98: Created.                    */
/***************************************************/

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

#include "swis.h"
#include "kernel.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 "window.h"

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

#include "Browser.h"
#include "Fetch.h" /* For ISLINK and ISOBJECT */
#include "Forms.h"
#include "Images.h"
#include "Menus.h"
#include "Object.h"
#include "Reformat.h"
#include "Windows.h"

#include "ItemInfo.h"

/* Local statics */

static char * src_url = NULL;
static char * dst_url = NULL;

/* Static function prototypes */

static void iteminfo_info_not_available      (ObjectId o, ComponentId c);
static void iteminfo_info_none               (ObjectId o, ComponentId c);

static void iteminfo_store_source            (ObjectId o, const char * source);
static void iteminfo_store_destination       (ObjectId o, const char * destination);

static void iteminfo_send_to_keyboard_buffer (const char * s);
static void iteminfo_fill_right              (char * buffer, int buffer_size, const char * s);

/*************************************************/
/* iteminfo_info_not_available()                 */
/*                                               */
/* Fill in a display field with a string to show */
/* the information is not available.             */
/*                                               */
/* Parameters: Object ID of the object the       */
/*             display field lies in;            */
/*                                               */
/*             Component ID of the display       */
/*             field.                            */
/*************************************************/

static void iteminfo_info_not_available(ObjectId o, ComponentId c)
{
  displayfield_set_value(0,
                         o,
                         c,
                         lookup_token("IINA:N/A",0,0));

  return;
}

/*************************************************/
/* iteminfo_info_none()                          */
/*                                               */
/* Fill in a display field with a string to show */
/* no information is available / relevant.       */
/*                                               */
/* Parameters: Object ID of the object the       */
/*             display field lies in;            */
/*                                               */
/*             Component ID of the display       */
/*             field.                            */
/*************************************************/

static void iteminfo_info_none(ObjectId o, ComponentId c)
{
  displayfield_set_value(0,
                         o,
                         c,
                         lookup_token("IINone:(None)",0,0));

  return;
}

/*************************************************/
/* iteminfo_store_source()                       */
/*                                               */
/* Remember a URL as a source URL for some item, */
/* so the 'Export item' button can be used if    */
/* present. Any URL already stored is discarded. */
/*                                               */
/* Parameters: Object ID of the Item Information */
/*             dialogue;                         */
/*                                               */
/*             Pointer to the URL to remember.   */
/*************************************************/

static void iteminfo_store_source(ObjectId o, const char * source)
{
  free(src_url);
  src_url = NULL;

  /* Remember the URL */

  if (source && *source)
  {
    src_url = malloc(strlen(source) + 1);

    if (src_url) strcpy(src_url, source);
  }

  /* If we have a stored URL, ungrey the 'Export item' button, */
  /* else grey it out.                                         */

  if (src_url) set_gadget_state(o, IIExportItem, 0);
  else         set_gadget_state(o, IIExportItem, 1);

  return;
}

/*************************************************/
/* iteminfo_store_destination                    */
/*                                               */
/* Remember a URL as a destination URL for some  */
/* item, so the 'Follow link' button can be used */
/* if present. Any URL already stored is         */
/* discarded.                                    */
/*                                               */
/* Parameters: Object ID of the Item Information */
/*             dialogue;                         */
/*                                               */
/*             Pointer to the URL to remember.   */
/*************************************************/

static void iteminfo_store_destination(ObjectId o, const char * destination)
{
  free(dst_url);
  dst_url = NULL;

  /* Remember the URL */

  if (destination && *destination)
  {
    dst_url = malloc(strlen(destination) + 1);

    if (dst_url) strcpy(dst_url, destination);
  }

  /* If we have a stored URL, ungrey the 'Follow Link' button, */
  /* else grey it out.                                         */

  if (dst_url) set_gadget_state(o, IIFollowLink, 0);
  else         set_gadget_state(o, IIFollowLink, 1);

  return;
}

/*************************************************/
/* iteminfo_send_to_keyboard_buffer()            */
/*                                               */
/* Little routine to send a stream of chars to   */
/* the keyboard buffer.                          */
/*                                               */
/* Parameters: Pointer to the string to send     */
/*             (terminator is not included).     */
/*************************************************/

static void iteminfo_send_to_keyboard_buffer(const char * s)
{
  if (!s) return;

  while (*s)
  {
    _swix(OS_Byte,
          _INR(0,2),

          138,   /* Insert char into buffer                  */
          0,     /* Magic number alert - 0 = keyboard buffer */
          *s++); /* Char to insert                           */
  }
}

/*************************************************/
/* iteminfo_fill_right()                         */
/*                                               */
/* Fills in the given buffer with either the     */
/* whole of the given string, or, if it doesn't  */
/* fit, the rightmost part of it.                */
/*                                               */
/* Parameters: Pointer to the buffer;            */
/*                                               */
/*             Size of the buffer;               */
/*                                               */
/*             Pointer to the string to fill in. */
/*************************************************/

static void iteminfo_fill_right(char * buffer, int buffer_size, const char * s)
{
  int len;

  if (!s || !buffer || buffer_size < 1) return;

  len = strlen(s);

  if (len < buffer_size) strcpy(buffer, s);
  else strcpy(buffer, s + len - buffer_size + 1);

  return;
}

/*************************************************/
/* iteminfo_to_be_shown()                        */
/*                                               */
/* Called before the Item Information window is  */
/* shown - fills in the contents, based on the   */
/* HStream and browser_data pointer returned by  */
/* calls to menus_document_opened_over and       */
/* menus_document_over_browser.                  */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int iteminfo_to_be_shown(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  ChkError(iteminfo_fill_in_contents(menus_document_over_browser(),
                                     menus_document_opened_over(),
                                     idb->self_id));

  return 1;
}

/*************************************************/
/* iteminfo_follow_link()                        */
/*                                               */
/* Called when the 'Export item' button is       */
/* clicked on (or rather, the EIIFollowLink      */
/* event is raised).                             */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int iteminfo_follow_link(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  if (dst_url && *dst_url)
  {
    int ctrl;

    _swix(OS_Byte,
          _INR(0,1) | _OUT(1),

          121,
          129,

          &ctrl);

    /* If Control is not held down, follow the link */

    if (!ctrl)
    {
      ChkError(windows_create_browser(dst_url,
                                      NULL,
                                      NULL,
                                      NULL,
                                      Windows_CreateBrowser_Normal));
    }

    /* Otherwise, insert '<a href="<url>"></a>' into the keyboard buffer... */

    else
    {
      char back[5];

      iteminfo_send_to_keyboard_buffer("<a href=\"");
      iteminfo_send_to_keyboard_buffer(dst_url);
      iteminfo_send_to_keyboard_buffer("\"></a>");

      /* Move cursor back before '</a>' */

      back[0] = back[1] = back[2] = back[3] = 140;
      back[4] = 0;

      iteminfo_send_to_keyboard_buffer(back);
    }

    free(dst_url);
    dst_url = NULL;

    return 1;
  }
  else return 0;
}

/*************************************************/
/* iteminfo_export_item()                        */
/*                                               */
/* Called when the 'Export item' button is       */
/* clicked on (or rather, the EIIExportItem      */
/* event is raised).                             */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int iteminfo_export_item(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  if (src_url && *src_url)
  {
    int ctrl;

    _swix(OS_Byte,
          _INR(0,1) | _OUT(1),

          121,
          129,

          &ctrl);

    /* If Control is not held down, save the object */

    if (!ctrl)
    {
      ChkError(windows_create_browser(src_url,
                                      NULL,
                                      NULL,
                                      NULL,
                                      Windows_CreateBrowser_SaveToFile));
    }

    /* Otherwise, use the text in the display fields to construct */
    /* part of what could be an IMG or OBJECT tag, or similar     */

    else
    {
      char number[32];

      /* 'src="..."' */

      iteminfo_send_to_keyboard_buffer("src=\"");
      iteminfo_send_to_keyboard_buffer(src_url);
      iteminfo_send_to_keyboard_buffer("\"");

      /* ' width="..."' */

      if (!(displayfield_get_value(0,
                                   idb->self_id,
                                   IIActualSizeWidthDisplay,
                                   number,
                                   sizeof(number),
                                   NULL)))
      {
        number[sizeof(number) - 1] = 0;

        if (atoi(number))
        {
          iteminfo_send_to_keyboard_buffer(" width=\"");
          iteminfo_send_to_keyboard_buffer(number);
          iteminfo_send_to_keyboard_buffer("\"");
        }
      }

      /* ' height="..."' */

      if (!(displayfield_get_value(0,
                                   idb->self_id,
                                   IIActualSizeHeightDisplay,
                                   number,
                                   sizeof(number),
                                   NULL)))
      {
        number[sizeof(number) - 1] = 0;

        if (atoi(number))
        {
          iteminfo_send_to_keyboard_buffer(" height=\"");
          iteminfo_send_to_keyboard_buffer(number);
          iteminfo_send_to_keyboard_buffer("\"");
        }
      }
    }

    free(src_url);
    src_url = NULL;

    return 1;
  }
  else return 0;
}

/*************************************************/
/* iteminfo_fill_in_contents()                   */
/*                                               */
/* Fill in the Item Information window based on  */
/* the given token.                              */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the item;             */
/*                                               */
/*             Pointer to the HStream to use as  */
/*             a source of information;          */
/*                                               */
/*             Object ID of the Item Information */
/*             dialogue to fill in.              */
/*************************************************/

_kernel_oserror * iteminfo_fill_in_contents(browser_data * b, HStream * t, ObjectId o)
{
  char url[Limits_ShortURL];

  /* Invalidate the source and destination URLs */

  iteminfo_store_source      (o, NULL);
  iteminfo_store_destination (o, NULL);

  if (!t)
  {
    /* No token - so fill in fields with 'none' or 'not available' */

    iteminfo_info_none(o, IIItemTypeDisplay);

    iteminfo_info_not_available(o, IILinkToDisplay);
    iteminfo_info_not_available(o, IIFetchedFromDisplay);

    iteminfo_info_not_available(o, IIActualSizeWidthDisplay);
    iteminfo_info_not_available(o, IIActualSizeHeightDisplay);
    iteminfo_info_not_available(o, IIScaledToWidthDisplay);
    iteminfo_info_not_available(o, IIScaledToHeightDisplay);
  }
  else
  {
    /* Deal with anchors at a generic level - other special */
    /* types can override this if they really want to.      */

    if (ISLINK(t) && *t->anchor)
    {
      iteminfo_fill_right(url, sizeof(url), t->anchor);

      displayfield_set_value(0,
                             o,
                             IILinkToDisplay,
                             url);

      iteminfo_store_destination(o, t->anchor);
    }
    else iteminfo_info_none(o, IILinkToDisplay);

    /* Now go through known token types */

    if (t->tagno == TAG_INPUT || t->tagno == TAG_TEXTAREA || t->tagno == TAG_SELECT)
    {
      const char * sub;
      int          post = 2;

      /* Forms item - for the 'link to', give the form submission URL, */
      /* if available.                                                 */

      sub = form_submission_information(b, t, &post);

      if (sub && *sub)
      {
        switch (post)
        {
          case 0:
          {
            iteminfo_fill_right(url, sizeof(url) - sizeof(" (GET)") + 1, sub);
            strcat(url, " (GET)");
          }
          break;

          case 1:
          {
            iteminfo_fill_right(url, sizeof(url) - sizeof(" (POST)") + 1, sub);
            strcat(url, " (POST)");
          }
          break;

          default:
          {
            iteminfo_fill_right(url, sizeof(url), sub);
          }
          break;
        }

        displayfield_set_value(0,
                               o,
                               IILinkToDisplay,
                               url);

        iteminfo_store_destination(o, sub);
      }
      else iteminfo_info_none(o, IILinkToDisplay);

      /* INPUT TYPE=IMAGE is a special case. */

      if (HtmlINPUTtype(t) == inputtype_IMAGE)
      {
        BBox size;
        char num[32];

        displayfield_set_value(0,
                               o,
                               IIItemTypeDisplay,
                               lookup_token("IIFrmImg:Form element (image)",0,0));

        /* Output the image source URL */

        if (HtmlINPUTsrc(t))
        {
          iteminfo_fill_right(url, sizeof(url), HtmlINPUTsrc(t));

          displayfield_set_value(0,
                                 o,
                                 IIFetchedFromDisplay,
                                 url);

          iteminfo_store_source(o, HtmlINPUTsrc(t));
        }
        else iteminfo_info_not_available(o, IIFetchedFromDisplay);

        /* Get the scaled size */

        reformat_get_image_size(b, t, &size);

        sprintf(num, "%d", size.xmax - size.xmin);
        displayfield_set_value(0, o, IIScaledToWidthDisplay, num);

        sprintf(num, "%d", size.ymax - size.ymin);
        displayfield_set_value(0, o, IIScaledToHeightDisplay, num);

        /* Get the actual size */

        image_get_token_actual_size(b, t, &size.xmin, &size.ymin);

        if (!size.xmin && !size.ymin)
        {
          iteminfo_info_not_available(o, IIActualSizeWidthDisplay);
          iteminfo_info_not_available(o, IIActualSizeHeightDisplay);
        }
        else
        {
          sprintf(num, "%d", size.xmin);
          displayfield_set_value(0, o, IIActualSizeWidthDisplay, num);

          sprintf(num, "%d", size.ymin);
          displayfield_set_value(0, o, IIActualSizeHeightDisplay, num);
        }
      }
      else
      {
        displayfield_set_value(0,
                               o,
                               IIItemTypeDisplay,
                               lookup_token("IIFrmGen:Form element (miscellaneous)",0,0));

        goto do_unknown;
      }
    }
    else if (t->style & IMG)
    {
      BBox size;
      char num[32];

      /* Output the image source URL */

      if (t->src)
      {
        iteminfo_fill_right(url, sizeof(url), t->src);

        displayfield_set_value(0,
                               o,
                               IIFetchedFromDisplay,
                               url);

        iteminfo_store_source(o, t->src);
      }
      else iteminfo_info_not_available(o, IIFetchedFromDisplay);

      /* Get the scaled size */

      reformat_get_image_size(b, t, &size);

      sprintf(num, "%d", size.xmax - size.xmin);
      displayfield_set_value(0, o, IIScaledToWidthDisplay, num);

      sprintf(num, "%d", size.ymax - size.ymin);
      displayfield_set_value(0, o, IIScaledToHeightDisplay, num);

      /* Get the actual size */

      image_get_token_actual_size(b, t, &size.xmin, &size.ymin);

      if (!size.xmin && !size.ymin)
      {
        iteminfo_info_not_available(o, IIActualSizeWidthDisplay);
        iteminfo_info_not_available(o, IIActualSizeHeightDisplay);

        displayfield_set_value(0,
                               o,
                               IIItemTypeDisplay,
                               lookup_token("IIImgNA:Image (size unknown)",0,0));
      }
      else
      {
        sprintf(num, "%d", size.xmin);
        displayfield_set_value(0, o, IIActualSizeWidthDisplay, num);

        sprintf(num, "%d", size.ymin);
        displayfield_set_value(0, o, IIActualSizeHeightDisplay, num);

        displayfield_set_value(0,
                               o,
                               IIItemTypeDisplay,
                               lookup_token("IIImage:Image (size known)",0,0));
      }
    }
    else if (ISOBJECT(t))
    {
      const char * data    = HtmlOBJECTdata(t);
      const char * current = browser_fetch_url(b);
      const char * newdata;

      /* For OBJECTs, need to jump a few hoops to get the source URL out */

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

      if (current)
      {
        int known = 0;

        newdata = HtmlRelativiseURL(current, data, t);

        if (newdata)
        {
          if (object_token_is_image(b, t))
          {
            BBox size;
            char num[32];

            displayfield_set_value(0,
                                   o,
                                   IIItemTypeDisplay,
                                   lookup_token("IIObjImg:Object (image)",0,0));

            /* Output the object's source URL */

            iteminfo_fill_right(url, sizeof(url), newdata);

            displayfield_set_value(0,
                                   o,
                                   IIFetchedFromDisplay,
                                   (char *) newdata);

            iteminfo_store_source(o, newdata);

            /* Will not need the relativised source URL from HtmlRelativiseURL */
            /* again, so free it. If we forgot to do this, since it was        */
            /* allocated in the context of the token it'd get freed sooner or  */
            /* later with the rest of the stream.                              */

            HtmlFree((void *) newdata);

            /* Get the scaled size */

            object_get_token_object_size(b, t, &size);

            sprintf(num, "%d", size.xmax - size.xmin);
            displayfield_set_value(0, o, IIScaledToWidthDisplay, num);

            sprintf(num, "%d", size.ymax - size.ymin);
            displayfield_set_value(0, o, IIScaledToHeightDisplay, num);

            /* Get the actual size */

            image_get_token_actual_size(b, t, &size.xmin, &size.ymin);

            if (!size.xmin && !size.ymin)
            {
              iteminfo_info_not_available(o, IIActualSizeWidthDisplay);
              iteminfo_info_not_available(o, IIActualSizeHeightDisplay);
            }
            else
            {
              sprintf(num, "%d", size.xmin);
              displayfield_set_value(0, o, IIActualSizeWidthDisplay, num);

              sprintf(num, "%d", size.ymin);
              displayfield_set_value(0, o, IIActualSizeHeightDisplay, num);
            }

            known = 1;

          /* Closure of 'if (object_is_image(...))' */
          }

        /* Closure of 'if (newdata)' */
        }

        /* If the object type was not dealt with (not known), use */
        /* a generic string instead                               */

        if (!known)
        {
          displayfield_set_value(0,
                                 o,
                                 IIItemTypeDisplay,
                                 lookup_token("IIObjGen:Object (miscellaneous)",0,0));

          goto do_unknown;
        }
      }
    }
    else if (t->tagno == 0 && (t->style & PCDATA))
    {
      /* Some text */

      if (ISLINK(t) && *t->anchor)
      {
        displayfield_set_value(0,
                               o,
                               IIItemTypeDisplay,
                               lookup_token("IITxtLnk:Piece of text (link)",0,0));
      }
      else
      {
        displayfield_set_value(0,
                               o,
                               IIItemTypeDisplay,
                               lookup_token("IITxtPln:Piece of text (not a link)",0,0));
      }

      goto do_unknown;
    }
    else
    {
      /* This isn't an item type we know about [yet] */

      displayfield_set_value(0,
                             o,
                             IIItemTypeDisplay,
                             lookup_token("IIUnknown:(Unknown)",0,0));

do_unknown: /* Jumped to for things which know the general */
            /* item type, but can't fill in the details.   */

      /* Information is not available */

      iteminfo_info_not_available(o, IIFetchedFromDisplay);

      iteminfo_info_not_available(o, IIActualSizeWidthDisplay);
      iteminfo_info_not_available(o, IIActualSizeHeightDisplay);
      iteminfo_info_not_available(o, IIScaledToWidthDisplay);
      iteminfo_info_not_available(o, IIScaledToHeightDisplay);
    }
  }

  return NULL;
}