Hotlist 227 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
/* 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   : Hotlist.c                              */
/*                                                 */
/* Purpose: Managing a hotlist in the browser.     */
/*                                                 */
/* Author : D.T.A.Brown                            */
/*                                                 */
/* History: 06-Aug-97: Created.                    */
/*          22-Aug-97: (ADH/DTAB) Integrated into  */
/*                     main browser code.          */
/***************************************************/


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

#include "swis.h"

#include "toolbox.h"
#include "wimp.h"
#include "wimplib.h"
#include "menu.h"
#include "event.h"
#include "gadgets.h"

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

47
#include "ChoiceDefs.h"
48
#include "FetchPage.h"
49
#include "Filetypes.h"
50
#include "Menus.h"
51
#include "Mouse.h"
52
#include "Multiuser.h"
53
#include "Protocols.h"
54 55
#include "Save.h"
#include "Toolbars.h"
56
#include "URLUtils.h"
57 58 59 60
#include "Windows.h"

#include "Hotlist.h"

61
/* Internationalisation support */
62

63
#ifdef UNIFONT
64
  #define CHARSET_SPECIFIER "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
65
#else
66
  #define CHARSET_SPECIFIER "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\n"
67 68
#endif

69 70
/* Local definitions */

71
#define HotlistWrite(fn) {written = (fn); if (written < 0) goto hotlist_save_error;}
72 73 74

/* Local statics */

75 76 77
static int            autoopen_oldtime;                            /* Base time for autoopen directory */
static hotlist_item * hotlist_root                = NULL;          /* Pointer to the hotlist root directory item */
static int            hotlist_windowid            = NULL_ObjectId; /* Object ID of the hotlist window */
78

79
static hotlist_item * hotlist_newitem;                             /* When ever a new item is created this points to it */
80

81
static int            menu_itemno                 = 0;             /* Item over which menu was pressed */
82

83 84
static int            menu_select                 = 0;             /* 1 if an item had to be selected when the menu was opened */
static unsigned int   last_selected_item          = 0xffffffff;    /* The last item which was selected */
85

86 87 88
static unsigned int   highlighted_itemno;                          /* During dragging, the number of a directory item using the '...+' sprite */
static hotlist_item * hotlist_current_highlighted = NULL;          /* To save scanning lists, pointer to the highlighted item */
static int            hl_show_urls;                                /* 0 when url descriptions to be shown, 1 to show urls */
89

90 91 92
static unsigned int   hotlist_bbar_size           = 0;             /* Height in OS units of the button bar */
static unsigned int   alter_new;                                   /* Remembers if the edit dialogue is Alter or New; see   */
                                                                   /* HOTLIST_MENUSECTION_NEW and HOTLIST_MENUSECTION_ALTER */
93 94 95 96
/* Local structures */

typedef struct hl_dragging_details
{
97 98 99
  char         drag_type;       /* One of HOTIST_NOT_DRAGGING, HOTLIST_BOX_DRAG, HOTLIST_SOLID_DRAG_OBJECT, */
                                /* HOTLIST_SOLID_DRAG_SPRITE, or HOTLIST_BOX_DRAG_SELECTION - See Hotlist.h */
  unsigned int using_adjust :1;
100 101 102 103 104
}
hl_dragging_details;

static hl_dragging_details hotlist_dragging;

105 106
/* Event handler prototypes */

107 108 109 110
// Write these out in full, to clearly state exactly what parameters are needed?
// Or is this in fact more robust, as the compiler will fault mismatched functions
// in this source as opposed to wherever a registration of the handler occurs?

111 112 113 114 115 116 117 118 119 120
static _kernel_oserror     * hotlist_selection_box_start(void);

static WimpEventHandler      hotlist_redraw_handler;
static WimpEventHandler      hotlist_mouse_click_handler;
static WimpEventHandler      hotlist_drag_completed_handler;
static WimpEventHandler      hotlist_null_handler;
static WimpEventHandler      hotlist_null_drag_select_handler;

static ToolboxEventHandler   hotlist_menuopen_handler;
static ToolboxEventHandler   hotlist_menuclose_handler;
121 122
static ToolboxEventHandler   hotlist_select_all_handler;
static ToolboxEventHandler   hotlist_clear_selection_handler;
123 124 125
static ToolboxEventHandler   hotlist_menu_openall_handler;
static ToolboxEventHandler   hotlist_menu_closeall_handler;
static ToolboxEventHandler   hotlist_menu_delete_handler;
126
static ToolboxEventHandler   hotlist_save_to_server_handler;
127 128 129 130 131 132 133 134
static ToolboxEventHandler   hotlist_show_editurl_handler;
static ToolboxEventHandler   hotlist_show_rendirectory_handler;
static ToolboxEventHandler   hotlist_show_newurl_handler;
static ToolboxEventHandler   hotlist_show_newdirectory_handler;
static ToolboxEventHandler   hotlist_newedit_url_handler;
static ToolboxEventHandler   hotlist_newren_directory_handler;
static ToolboxEventHandler   hotlist_reset_url_handler;
static ToolboxEventHandler   hotlist_reset_directory_handler;
135 136
static ToolboxEventHandler   hotlist_show_descriptions_handler;
static ToolboxEventHandler   hotlist_show_urls_handler;
David Brown's avatar
David Brown committed
137
static ToolboxEventHandler   hotlist_drag_stop_handler;
138
static ToolboxEventHandler   hotlist_close_handler;
139 140 141 142 143 144 145 146 147 148

/* Debug functions */

#ifdef TRACE

  static void hotlist_display_item(hotlist_item * item);
  static void hotlist_display_tree(hotlist_item * list, int indent);

#endif

149 150 151 152 153
/* List enquiry and manupulation */

static _kernel_oserror * hotlist_link                    (hotlist_item * item, hotlist_item * target, unsigned int position);
static void              hotlist_unlink                  (hotlist_item * item);
static _kernel_oserror * hotlist_new_directory           (hotlist_item * parent, char * directory_name, unsigned int position, hotlist_item ** new);
154
static _kernel_oserror * hotlist_new_url                 (hotlist_item * parent, unsigned int position, char * url_desc, char * url);
155 156 157 158 159
static void              hotlist_delete_item             (hotlist_item * item);
static _kernel_oserror * hotlist_move_item               (hotlist_item * source, hotlist_item * target, unsigned int position);
static _kernel_oserror * hotlist_copy_item               (hotlist_item * source, hotlist_item * target, unsigned int position, hotlist_item ** new_item);

/* Item enquiry and manipulation */
160 161

static _kernel_oserror * hotlist_get_entry_sizes         (unsigned int * item_height, unsigned int * item_dir_width, unsigned int * item_url_width);
162 163 164 165
static int               hotlist_set_flags               (hotlist_item * list, hotlist_type type, unsigned int flags);
static int               hotlist_clear_flags             (hotlist_item * list, hotlist_type type, unsigned int flags);

/* Finding items */
166 167

static hotlist_item    * hotlist_find_item               (hotlist_item * list, unsigned int item_no);
168
static hotlist_item    * hotlist_find_item_r             (hotlist_item * list, unsigned int item_no, int * curr_item);
169
static int               hotlist_find_no_from_item       (hotlist_item * item);
170 171 172 173 174 175 176 177 178 179 180 181 182
static int               hotlist_find_no_from_item_r     (hotlist_item * list, hotlist_item * item, int * curr_item);
static hotlist_item    * hotlist_find_selected_item_r    (hotlist_item * list);

/* Counting items */

static void              hotlist_count_selected_items_r  (hotlist_item * list, int * count);
static unsigned int      hotlist_count_displayed_items   (hotlist_item * list);
static void              hotlist_count_displayed_items_r (hotlist_item * list, int * count);

// Categorisation work in progress...

static _kernel_oserror * hotlist_draw                    (hotlist_item * list, unsigned int first_item, unsigned int last_item);
static _kernel_oserror * hotlist_draw_r                  (hotlist_item *list, unsigned int first_item, unsigned int last_item, int * curr_item, unsigned int indent, unsigned int item_height, unsigned int item_dir_width, unsigned int item_url_width);
183 184
static unsigned int      hotlist_contents_selected       (hotlist_item * item);
static unsigned int      hotlist_no_contents_selected    (hotlist_item * item);
185
static unsigned int      hotlist_get_max_width           (hotlist_item * list);
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
186
static _kernel_oserror * hotlist_get_max_width_r         (hotlist_item *list, unsigned int indent, int * max_width, unsigned int item_height, unsigned int item_dir_width, unsigned int item_url_width);
187 188
static _kernel_oserror * hotlist_redraw_now              (void);
static _kernel_oserror * hotlist_redraw_now_r            (hotlist_item * list, int * curr_item);
189
static _kernel_oserror * hotlist_get_shape               (int * xmin, int * xmax, hotlist_item * item);
190 191 192
static _kernel_oserror * hotlist_directory_open_close    (hotlist_item * item, unsigned int itemno);
static _kernel_oserror * hotlist_redraw_items            (unsigned int firstitem, unsigned int lastitem);
static _kernel_oserror * hotlist_launch_url              (hotlist_item * item);
193 194
static _kernel_oserror * hotlist_process_click_on_item   (unsigned int itemno, hotlist_item * item, int buttons, int x, int y);
static _kernel_oserror * hotlist_process_click           (int x, int y, int buttons);
195
static int               hotlist_preopen                 (void);
196
static _kernel_oserror * hotlist_set_menu_details        (ObjectId menuid);
197
static _kernel_oserror * hotlist_save_entries            (FILE * fileptr, hotlist_item * list, int type, int save_read_only);
198 199
static void              hotlist_lower_tags              (char * string);
static void              hotlist_drag_renderer           (hotlist_item * item, unsigned int item_height, unsigned int item_dir_width, unsigned int item_url_width);
200 201
static _kernel_oserror * hotlist_start_drag              (void);
static _kernel_oserror * hotlist_start_drag_backend      (void);
202
static _kernel_oserror * hotlist_modified                (unsigned int type);
203
static void              hotlist_convert_drag_selection  (hotlist_item * item);
204 205
static _kernel_oserror * hotlist_autoscroll              (int window);
static _kernel_oserror * hotlist_load_directory          (FILE * fileptr, hotlist_item * target);
206

207
static int               hotlist_get_selected_shape      (BBox * box);
208 209 210 211
static _kernel_oserror * hotlist_get_selected_shape_r    (hotlist_item * list, BBox * box, unsigned int * itemno, int * found, unsigned int item_height);

static _kernel_oserror * hotlist_select_box              (unsigned int first_item, unsigned int last_item, int minx, int maxx);
static _kernel_oserror * hotlist_select_box_r            (unsigned int first_item, unsigned int last_item, hotlist_item *item, unsigned int *itemno, int minx, int maxx);
212

213
static void              hotlist_find_match_r            (char * buffer, int buffer_size, hotlist_item * item, hotlist_item ** lowest_item, int * lowest_offset, int * lowest_diff);
214

215 216
/* Save Protocol */

217 218
static _kernel_oserror * hotlist_initiate_uri_save       (hotlist_item *sourceitem);
static _kernel_oserror * hotlist_initiate_html_save      (char *filename);
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
#ifdef TRACE

  /*************************************************/
  /* hotlist_display_item()                        */
  /*                                               */
  /* This function display the data held by a      */
  /* single hotlist_item                           */
  /*                                               */
  /* Parameters: Pointer to a hotlist_item         */
  /*************************************************/

  static void hotlist_display_item(hotlist_item * item)
  {
    Printf("\nhotlist_display_item for %p\n",item);
    Printf("--------------------\n");
    Printf("type              = %d\n", item->type);
    Printf("name              = %s\n", item->name);
    Printf("flags             = %d\n", item->flags);
    Printf("parent            = %p\n", item->parent);
    Printf("previous          = %p\n", item->previous);
    Printf("next              = %p\n", item->next);
    Printf("data.generic_data = %p ",  item->data.generic_data);

    switch(item->type)
    {
      case hl_url:
      {
        Printf("URL(%s)\n", item->data.url);
      }
      break;

      case hl_directory:
      {
        Printf("sub directory pointer\n");
      }
      break;

      default:
      {
        Printf("\n");
      }
      break;
    }
  }

  /*************************************************/
  /* hotlist_display_tree()                        */
  /*                                               */
  /* Recursivly display the hotlist tree starting  */
  /* from the passed item                          */
  /*                                               */
  /* Parameters: Pointer to a hotlist_item         */
  /*             value to start indent at          */
  /*             recommended 0                     */
  /*************************************************/

  static void hotlist_display_tree(hotlist_item * list, int indent)
  {
    int count;

    Printf("\nhotlist_display_tree for %p, indent %d\n", list, indent);
    Printf("--------------------\n\n");

    while (list)
    {
      for (count = 0; count < indent; count++) Printf("| ");

      switch (list->type)
      {
        case hl_url:
        {
          Printf("%s:URL(%s)\n", list->name, list->data.url);
        }
        break;

        case hl_directory:
        {
          Printf("%s:DIRECTORY", list->name);
        }

        if (list->flags & HOTLIST_D_IS_OPEN)
        {
          Printf(" (Open)\n");
          hotlist_display_tree(list->data.directory_content, indent + 1);
        }
        else
        {
          Printf(" (Closed)\n");
          hotlist_display_tree(list->data.directory_content, indent + 1);
        }
        break;

        default:
        {
          Printf("%s:UNRECOGNISED TYPE\n", list->name);
        }
        break;
      }
      list = list->next;
    }
  }

#endif

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
// /*************************************************/
// /* hotlist_is_inside()                           */
// /*                                               */
// /* Checks if one hotlist_item is held inside     */
// /* the directory structure of another            */
// /* hotlist_item                                  */
// /*                                               */
// /* Parameters: Pointer to the hotlist_item that  */
// /*             may be inside the parent;         */
// /*                                               */
// /*             Pointer to the parent             */
// /*             hotlist_item.                     */
// /*                                               */
// /* Returns:    1 if item is inside, else 0.      */
// /*************************************************/
//
// static int hotlist_is_inside(hotlist_item * inside, hotlist_item * outside)
// {
//   while (inside)
//   {
//     if (inside == outside) return 1;
//     inside = inside->parent;
//   }
//
//   return 0;
// }

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
/*************************************************/
/* hotlist_link()                                */
/*                                               */
/* This function links the passed item to the    */
/* passed target.                                */
/*                                               */
/* It can link the item in four different ways;  */
/* before or after the target, or if the target  */
/* is a directory, at the beginning or end of    */
/* that directory's contents.                    */
/*                                               */
/* Parameters: Pointer to the hotlist_item       */
/*             struct to link in;                */
/*                                               */
/*             Pointer to the hotlist_item       */
/*             struct to link to;                */
/*                                               */
/*             Position to link to -             */
/*             HOTLIST_POSITION_BEGINNING        */
/*             HOTLIST_POSITION_END              */
/*             HOTLIST_POSITION_BEFORE or        */
/*             HOTLIST_POSITION_AFTER.           */
/*                                               */
/* Assumes:    The item and target pointers are  */
/*             not NULL and are valid;           */
/*                                               */
/*             That if adding to the beginning   */
/*             or end, the target is a directory */
/*             (in both cases, an error will be  */
/*             raised in TRACE builds if the     */
/*             assumptions are violated).        */
/*************************************************/

static _kernel_oserror * hotlist_link(hotlist_item * item, hotlist_item * target, unsigned int position)
{
  #ifdef TRACE

    /* Test certain basic assumptions are not violated */

    if (!item || !target)
    {
      erb.errnum = Utils_Error_Custom_Normal;
      strcpy(erb.errmess, "NULL hotlist item or target pointer in hotlist_link");
      show_error_ret(&erb);
      return NULL;
    }

    if (
         (
           position == HOTLIST_POSITION_BEGINNING ||
           position == HOTLIST_POSITION_END
         )
         && target->type != hl_directory
       )
    {
      erb.errnum = Utils_Error_Custom_Normal;
      strcpy(erb.errmess, "Cannot insert at item at the beginning or end when the target is not a directory, in hotlist_link");
      show_error_ret(&erb);
      return NULL;
    }

  #endif

  switch (position)
  {
    case HOTLIST_POSITION_BEFORE:
    {
      /* Simple insertion above the target item */

      item->next     = target;
      item->previous = target->previous;
      item->parent   = target->parent;

      target->previous = item;

      if (item->previous) item->previous->next = item;
      if (item->parent && item->parent->data.directory_content == target) item->parent->data.directory_content = item;
    }
    break;

    default: /* We'll allow a default to adding after the target item */

    case HOTLIST_POSITION_AFTER:
    {
      /* Again, a simple insertion after the target item */

      item->previous = target;
      item->next     = target->next;
      item->parent   = target->parent;

      target->next = item;

      if (item->next) item->next->previous = item;
    }
    break;

    case HOTLIST_POSITION_BEGINNING:
    {
      /* A bit harder - insert at the top of a directory. */
      /* First a quick sanity check - TRACE builds will   */
      /* already have faulted this, but For non-TRACE     */
      /* builds, at least this will stop things dying.    */

      if (target->type != hl_directory) return NULL;

      /* This item is going to be at the beginning of a */
      /* directory, so there is never a previous item.  */

      item->previous = NULL;

      /* The next item is the one that used to be at the top */
      /* of the directory, and the parent for this item will */
      /* obviously be the target.                            */

      item->next   = target->data.directory_content;
      item->parent = target;

      /* If we have a next item, make sure its previous field */
      /* now points to the new item at the top.               */

      if (item->next) item->next->previous = item;

      /* The target should point to the new top item, too. */

      target->data.directory_content = item;
    }
    break;

    case HOTLIST_POSITION_END:
    {
      hotlist_item * last;

      /* Similarly, add to the end of the directory */

      if (target->type != hl_directory) return NULL;

      /* As before the parent must be the target item */

      item->parent = target;

      /* Now look from the start of the directory downwards for */
      /* the last item currently present.                       */

      last = target->data.directory_content;

      while(last && last->next) last = last->next;

      /* The new item must point to that one as in its previous field, */
      /* and has no next item to point to.                             */

      item->previous = last;
      item->next     = NULL;

      if (last)
      {
        /* If there were any items in the directory, make sure that the */
        /* last one points to the new bottom entry.                     */

        last->next = item;
      }
      else
      {
        /* Otherwise, the parent directory should point to this item */

        target->data.directory_content = item;
      }
    }
    break;
  }

  /* Finished... */

  return NULL;
}

526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
/*************************************************/
/* hotlist_unlink()                              */
/*                                               */
/* This function unlinks the passed item from    */
/* the items linked in before and after it.      */
/*                                               */
/* Parameters: Pointer to the hotlist_item       */
/*             struct to remove.                 */
/*************************************************/

static void hotlist_unlink(hotlist_item *item)
{
  if (item->parent && item->parent->data.directory_content == item) item->parent->data.directory_content = item->next;
  if (item->previous) item->previous->next = item->next;
  if (item->next) item->next->previous = item->previous;

  /* Not strictly needed, but added to ensure robustness */

  item->next     = NULL;
  item->previous = NULL;
  item->parent   = NULL;
}

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
/*************************************************/
/* hotlist_new_directory()                       */
/*                                               */
/* This function creates a new directory         */
/* at the beginning of the given parent.         */
/*                                               */
/* Parameters: Pointer to a hotlist_item of type */
/*             directory that represents the     */
/*             parent item for this new entry;   */
/*                                               */
/*             Name of new directory to create;  */
/*                                               */
/*             Position to create the new item   */
/*             relative to the parent (as for    */
/*             hotlist_link);                    */
/*                                               */
/*             Pointer to a pointer to a         */
/*             hotlist_item struct, which will   */
/*             be filled in with the address of  */
/*             the new item (unless there is an  */
/*             error returned).                  */
/*                                               */
/* Assumes:    That the pointer to a pointer to  */
/*             a hotlist_item struct is not NULL */
/*             (it would make little sense to    */
/*             allow this...).                   */
/*************************************************/

static _kernel_oserror * hotlist_new_directory(hotlist_item * parent, char * directory_name, unsigned int position, hotlist_item ** new)
{
  char             * perm_dirname;
  hotlist_item     * item;
  _kernel_oserror  * e;
582
  int                is_untitled = 0;
583

584
  if (!directory_name) is_untitled = 1;
585 586 587 588
  else
  {
    while (*directory_name && *directory_name <= ' ') directory_name ++;

589
    if (!*directory_name) is_untitled = 1;
590 591
    else
    {
592
      int len = strlen(directory_name);
593

594
      while (len && directory_name[len - 1] <= ' ') len--;
595

596
      if (!len) is_untitled = 1;
597
      else directory_name[len] = '\0';
598 599
    }
  }
600

601 602
  if (is_untitled) directory_name = lookup_token("HotlistUntitled:(Untitled)",0,0);

603 604 605 606
  /* Allocate space for the item and its name */

  item = malloc(sizeof(hotlist_item));

607
  if (!item) return make_no_memory_error(4);
608 609 610 611 612 613 614

  perm_dirname = malloc(strlen(directory_name) + 1);

  if (!perm_dirname)
  {
    free(item);

615
    return make_no_memory_error(5);
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
  }

  /* Copy the name into the new buffer */

  strcpy(perm_dirname, directory_name);

  /* Initialise the new hotlist item */

  item->type                   = hl_directory;
  item->flags                  = DIRECTORY_FLAGS;
  item->name                   = perm_dirname;
  item->data.directory_content = NULL;

  if (parent)
  {
    /* If we have been given a parent item, link this new */
    /* directory to it.                                   */

    e = hotlist_link(item, parent, position);

    /* If there's an error, free the item structure and name, */
    /* and return that error.                                 */

    if (e)
    {
      free(item);
      free(perm_dirname);

      return e;
    }
  }
  else
  {
    /* If there's no parent item, fill the various pointers */
    /* to other things in the list with NULL.               */

    item->next     = NULL;
    item->previous = NULL;
    item->parent   = NULL;
  }

  /* Record the new item in the hotlist_newitem static */

  hotlist_newitem = item;

  /* Return the address of the new item and exit */

  *new = item;

  return NULL;
}

/*************************************************/
/* hotlist_new_url()                             */
/*                                               */
/* This function creates a new url               */
/* at the end of the passed directory            */
/*                                               */
/* Parameters: Pointer to a hotlist_item struct  */
/*             representing a directory to add   */
/*             the URL to;                       */
/*                                               */
/*             Position within that directory    */
/*             to link to (number of items from  */
/*             the start);                       */
/*                                               */
/*             Description of the URL (e.g. from */
/*             the page title);                  */
/*                                               */
/*             Pointer to the URL itself.        */
/*************************************************/

688
static _kernel_oserror * hotlist_new_url(hotlist_item * parent, unsigned int position, char * url_desc, char * url)
689 690 691 692 693 694
{
  char            * perm_url_desc;
  char            * perm_url;
  hotlist_item    * item;
  _kernel_oserror * e;

695 696 697 698 699 700 701 702 703 704
  /* Do we have a URL string? */

  if (!url) return NULL;

  while (*url && *url <= ' ') url++;

  if (!*url) return NULL;

  /* Do we have a useful title string? */

705
  if (!url_desc) url_desc = url;
706 707
  else
  {
708
    while (*url_desc && *url_desc <= ' ') url_desc ++;
709

710
    if (!*url_desc) url_desc = url;
711 712
    else
    {
713
      int len = strlen(url_desc) - 1;
714

715
      while (len && url_desc[len] <= ' ') len--;
716

717 718
      if (!len) url_desc = url;
      else url_desc[len + 1] = '\0';
719 720 721
    }
  }

722 723 724 725 726
  /* Allocate a new hotlist_item structure */

  if ((item = malloc(sizeof(hotlist_item))) == NULL)
  {
    #ifdef TRACE
727
        if (tl & (1<<25)) Printf("hotlist_new_url: Could not allocate room for new URL item\n");
728 729 730 731 732 733 734
    #endif

    goto hotlist_new_url_no_memory; /* See bottom of function */
  }

  /* Allocate space for the description string */

735
  perm_url_desc = malloc(strlen(url_desc) + 1);
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750

  if (perm_url_desc == NULL) goto hotlist_new_url_no_memory; /* See bottom of function */

  /* Allocate space for the URL */

  perm_url = malloc(strlen(url) + 1);

  if (perm_url == NULL)
  {
    free(perm_url_desc);
    goto hotlist_new_url_no_memory; /* See bottom of function */
  }

  /* Copy the given description and URL to the allocated space */

751
  strcpy(perm_url_desc, url_desc);
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 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 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 849 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 878 879 880 881 882 883
  strcpy(perm_url, url);

  /* Fill in miscellaneous parts of the hotlist_item structure */

  item->type     = hl_url;
  item->flags    = URL_FLAGS;
  item->name     = perm_url_desc;
  item->data.url = perm_url;

  /* Link the item to the rest of the hotlist */

  e = hotlist_link(item, parent, position);

  if (e)
  {
    free(item);
    free(perm_url_desc);
    free(perm_url);

    return e;
  }

  /* Remember which item was added in the hotlist_newitem static */
  /* and exit with no error.                                     */

  hotlist_newitem = item;

  return NULL;

  /* Code for a common error case */

hotlist_new_url_no_memory:

  erb.errnum = Utils_Error_Custom_Normal;

  StrNCpy0(erb.errmess,
           lookup_token("NoMemURL:There is not enough free memory to add another URL to the hotlist.",
           0,
           0));

  return &erb;
}

/*************************************************/
/* hotlist_delete_item()                         */
/*                                               */
/* This function deletes an item from the        */
/* hotlist structure; it will recursively delete */
/* directories.                                  */
/*                                               */
/* Parameters: Pointer to the hotlist_item       */
/*             struct to delete.                 */
/*************************************************/

static void hotlist_delete_item(hotlist_item * item)
{
  if (item)
  {
    switch (item->type)
    {
      /* For a URL item, unlink it from the list */
      /* and free associated memory.             */

      case hl_url:
      {
        hotlist_unlink(item);
        free(item->name);
        free(item->data.url);
        free(item);
      }
      break;

      case hl_directory:
      {
        /* Recursively delete all items in the directory */

        while (item->data.directory_content)
        {
          hotlist_delete_item(item->data.directory_content);
        }

        /* Now unlink the item */

        hotlist_unlink(item);

        /* Free associated memory */

        free(item);
        free(item->name);

      }
      break;

      default:
      {
        #ifdef TRACE

          /* A 'should never happen' case! */

          erb.errnum = Utils_Error_Custom_Message;
          strcpy(erb.errmess, "Unrecognised item type in hotlist_delete_item - possibly memory corruption?");
          show_error_ret(&erb);

        #endif
      }
      break;
    }
  }
}

/*************************************************/
/* hotlist_move_item()                           */
/*                                               */
/* This function takes an item and moves its     */
/* position within the directory tree.           */
/*                                               */
/* Parameters: Pointer to the hotlist_item       */
/*             struct to move;                   */
/*                                               */
/*             Pointer to the hotlist_item       */
/*             structure to move to;             */
/*                                               */
/*             Position relative to that struct  */
/*             for the item to go to, as for     */
/*             hotlist_link.                     */
/*                                               */
/* Assumes:    It is assumed that if the object  */
/*             is a directory it is not being    */
/*             moved into it self or one of its  */
/*             children. If this is not the case */
/*             then both it and its children     */
/*             will become unlinked from the     */
884
/*             hotlist structure (nasty...!).    */
885 886 887 888
/*************************************************/

static _kernel_oserror * hotlist_move_item(hotlist_item * source, hotlist_item * target, unsigned int position)
{
889
  hotlist_item * newdir, * tempptr;
890

891 892 893 894 895 896 897 898 899
  /* If the item is read only, don't move it; copy instead. */

  if (source->flags & HOTLIST_G_IS_READ_ONLY)
  {
    return hotlist_copy_item(source, target, position, NULL);
  }

  /* Otherwise, moving it is OK */

David Brown's avatar
David Brown committed
900 901 902
  if (!(source->type == hl_directory && !hotlist_contents_selected(source->data.directory_content)))
  {
    /* Unlink item from directory structure */
903

David Brown's avatar
David Brown committed
904
    hotlist_unlink(source);
905

David Brown's avatar
David Brown committed
906
    /* Link into new position in directory structure */
907

David Brown's avatar
David Brown committed
908 909 910 911
    return hotlist_link(source, target, position);
  }
  else
  {
912 913 914 915 916 917
    /* Special case - moving a directory whose contents are only partially selected. */
    /* We can't move a directory whose contents are only partially to be moved,      */
    /* there would be nowhere to leave the items which were not moved with it.       */

    /* So, create a new directory based on the old */

David Brown's avatar
David Brown committed
918
    RetError(hotlist_new_directory(target, source->name, position, &newdir));
919 920 921

    /* Move the contents recursively into the new directory */

David Brown's avatar
David Brown committed
922
    source = source->data.directory_content;
923

924
    while (source)
David Brown's avatar
David Brown committed
925 926
    {
      tempptr = source->next;
927

David Brown's avatar
David Brown committed
928 929
      if (source->flags & HOTLIST_G_IS_SELECTED)
      {
930
        hotlist_move_item(source, newdir, HOTLIST_POSITION_END);
David Brown's avatar
David Brown committed
931 932
        source->flags &= ~HOTLIST_G_IS_SELECTED;
      }
933

David Brown's avatar
David Brown committed
934 935 936
      source = tempptr;
    }
  }
937

David Brown's avatar
David Brown committed
938
  return NULL;
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
}

/*************************************************/
/* hotlist_copy_item()                           */
/*                                               */
/* This function copies an item, and in the case */
/* of it being a directory, its children, to the */
/* specified place.                              */
/*                                               */
/* Parameters: Pointer to the hotlist_item       */
/*             struct to copy;                   */
/*                                               */
/*             Pointer to the hotlist_item       */
/*             struct to copy to;                */
/*                                               */
/*             Position relative to that item to */
/*             copy to, as for hotlist_link;     */
/*                                               */
/*             Pointer to a pointer to a         */
/*             hotlist_item struct, in which the */
/*             address of the new item is        */
/*             returned.                         */
/*                                               */
/* Assumes:    It is assumed that if the object  */
/*             is a directory it is not being    */
/*             copied into it self or one of its */
/*             children - if it is the function  */
/*             will keep recursing and           */
/*             eventually run out of stack;      */
/*                                               */
/*             The pointer to the pointer to the */
/*             hotlist_item struct may be NULL.  */
/*************************************************/

static _kernel_oserror * hotlist_copy_item(hotlist_item * source, hotlist_item * target,
                                           unsigned int position, hotlist_item ** new_item)
{
976
  hotlist_item * newdir;
977 978 979 980 981 982 983 984 985

  newdir = NULL;

  switch(source->type)
  {
    /* For a URL, create a new item based on the source one */

    case hl_url:
    {
986
      RetError(hotlist_new_url(target, position, source->name, source->data.url));
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
    }
    break;

    /* For a directory, first create a new item based on the source one */

    case hl_directory:
    {
      hotlist_item * content;

      RetError(hotlist_new_directory(target, source->name, position, &newdir));

      /* Now copy the contents recursively */

      content = source->data.directory_content;

      while (content)
      {
David Brown's avatar
David Brown committed
1004 1005 1006 1007
        if (content->flags & HOTLIST_G_IS_SELECTED)
        {
          RetError(hotlist_copy_item(content, newdir, HOTLIST_POSITION_END, NULL));
        }
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025

        content = content->next;
      }
    }
    break;
  }

  /* Fill in the new_item return value */

  if (new_item)
  {
    if (newdir) *new_item = newdir;
    else        *new_item = hotlist_newitem;
  }

  return NULL;
}

1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
/*************************************************/
/* hotlist_get_entry_sizes()                     */
/*                                               */
/* This function reads the size of the sprites   */
/* to be used by the hotlist and from them       */
/* determines the size of the hotlist entries.   */
/*                                               */
/* Parameters: Pointer to an int, in which the   */
/*             height of an item is placed in    */
/*             OS units;                         */
/*                                               */
/*             Pointer to an int, in which the   */
/*             minimum width of a directory item */
/*             is returned, in OS units;         */
/*                                               */
/*             Pointer to an int, in which the   */
/*             minimum width of a URL item is    */
/*             returned, in OS units.            */
/*                                               */
/* Assumes:    Any of the pointers may be NULL.  */
/*************************************************/

static _kernel_oserror * hotlist_get_entry_sizes(unsigned int * item_height, unsigned int * item_dir_width, unsigned int * item_url_width)
{
1050
  int width, height;
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087

  /* Get the open directory sprite size */

  RetError(read_sprite_size(OPEN_DIRECTORY_SPRITE, &width, &height));

  /* Use this for the minimum width of a directory entry */
  /* and the height of an item.                          */

  if (item_dir_width) *item_dir_width = width;
  if (item_height)    *item_height    = height;

  /* Now read the closed sprite; if the size is greater */
  /* than the width or height found above, use the new  */
  /* sizes instead.                                     */

  RetError(read_sprite_size(CLOSED_DIRECTORY_SPRITE, &width, &height));

  if (item_height    && height > *item_height)    *item_height    = height;
  if (item_dir_width && width  > *item_dir_width) *item_dir_width = width;

  /* Similarly for the insert item sprite. */

  RetError(read_sprite_size(INSERT_DIRECTORY_SPRITE, &width, &height));

  if (item_height    && height > *item_height)    *item_height    = height;
  if (item_dir_width && width  > *item_dir_width) *item_dir_width = width;

  /* Find the URL sprite size, and if required increase the */
  /* minimum entry height again based on this.              */

  RetError(read_sprite_size(URL_SPRITE, &width, &height));

  if (item_height && height > *item_height) *item_height= height;

  /* Set the URL width to the value found above and add 8 to */
  /* all of them for aesthetics.                             */

Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1088
  if (item_url_width) *item_url_width = width + 8;
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 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
  if (item_dir_width) *item_dir_width += 8;
  if (item_height)    *item_height    += 8;

  return NULL;
}

/*************************************************/
/* hotlist_set_flags()                           */
/*                                               */
/* This function will recursively set flags for  */
/* either a specified type of hotlist_item or    */
/* all hotlist_items, to the given value. All    */
/* items which are changed will have their       */
/* HOTLIST_G_REDRAW_NOW bit set.                 */
/*                                               */
/* Parameters: Pointer to a hotlist_item struct  */
/*             to start on;                      */
/*                                               */
/*             Type of hotlist_item to set flags */
/*             for, or hl_ALL for all types;     */
/*                                               */
/*             Flags to set.                     */
/*                                               */
/* Returns:    1 if any flags were set, else 0.  */
/*************************************************/

static int hotlist_set_flags(hotlist_item * list, hotlist_type type, unsigned int flags)
{
  int changed = 0;

  while (list)
  {
    /* Alter all items, or those of the correct type */

    if (type == hl_ALL || type == list->type)
    {
      if (list->flags | flags != list->flags)
      {
        list->flags |= HOTLIST_G_REDRAW_NOW;
        changed = 1;
      }

      list->flags |= flags;
    }

    /* Recursive call for directories */

    if (list->type == hl_directory)
    {
      if (hotlist_set_flags(list->data.directory_content, type, flags)) changed = 1;
    }

    list = list->next;
  }

  return changed;
}

/*************************************************/
/* hotlist_clear_flags()                         */
/*                                               */
/* This function will recursively clear flags    */
/* for either a specified type of hotlist_item   */
/* or all hotlist_items. All items changed will  */
/* have their HOTLIST_G_REDRAW_NOW bit set,      */
/* unless, of course, the routine is called to   */
/* clear that bit.                               */
/*                                               */
/* Parameters: Pointer to a hotlist_item struct  */
/*             to start on;                      */
/*                                               */
/*             Type of hotlist_item to set flags */
/*             for, or hl_ALL for all types;     */
/*                                               */
/*             Flags clear word (any bits set in */
/*             this word are cleared in the item */
/*             flags).                           */
/*                                               */
/* Returns:    1 if flags were cleared, else 0.  */
/*************************************************/

static int hotlist_clear_flags(hotlist_item * list, hotlist_type type, unsigned int flags)
{
  int changed = 0;

  while (list)
  {
    /* Only alter the requested item types */

    if (type == hl_ALL || type == list->type)
    {
David Brown's avatar
David Brown committed
1180
      if ((list->flags & ~flags) != list->flags)
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
      {
        list->flags |= HOTLIST_G_REDRAW_NOW;
        changed = 1;
      }

      list->flags &= ~flags;
    }

    /* Recursive call for directories */

    if (list->type == hl_directory)
    {
      if (hotlist_clear_flags(list->data.directory_content, type, flags)) changed = 1;
    }

    list = list->next;
  }

  return changed;
}

1202 1203 1204
/*************************************************/
/* hotlist_find_item()                           */
/*                                               */
1205 1206 1207 1208 1209 1210 1211 1212 1213
/* This function will recursivly scan through    */
/* a hotlist structure and return a pointer to   */
/* the n'th item.  It will only recurse through  */
/* open directories.                             */
/*                                               */
/* Parameters: Pointer to a hotlist_item at the  */
/*             top of the directory to scan;     */
/*                                               */
/*             The nth item to return within it. */
1214 1215
/*                                               */
/* Returns:    Pointer to the requested item or  */
1216
/*             NULL if it does not exist.        */
1217 1218
/*************************************************/

1219
static hotlist_item * hotlist_find_item(hotlist_item * list, unsigned int item_no)
1220
{
1221
  int curr_item = 0;
1222

1223
  return hotlist_find_item_r(list, item_no, &curr_item);
1224 1225 1226
}

/*************************************************/
1227
/* hotlist_find_item_r()                         */
1228
/*                                               */
1229
/* Recursive back-end to hotlist_find_item.      */
1230
/*                                               */
1231 1232 1233 1234 1235 1236 1237 1238 1239
/* Parameters: Pointer to a hotlist_item at the  */
/*             top of the directory to scan;     */
/*                                               */
/*             The nth item to return within it; */
/*                                               */
/*             Pointer to an int, in which the   */
/*             number of the current item is     */
/*             accumulated (initialised to an    */
/*             appropriate value, usually 0).    */
1240
/*                                               */
1241
/* Returns:    As hotlist_find_item.             */
1242 1243 1244
/*                                               */
/* Assumes:    The pointer to the int may *not*  */
/*             be NULL.                          */
1245 1246
/*************************************************/

1247
static hotlist_item * hotlist_find_item_r(hotlist_item * list, unsigned int item_no, int * curr_item)
1248
{
1249 1250 1251 1252
  hotlist_item * temp;

  while (list)
  {
1253
    if (*curr_item == item_no) return list; /* Found the list item */
1254 1255 1256

    /* Increment the item counter */

1257
    *curr_item += 1;
1258 1259 1260 1261 1262

    /* Recursively scan open directories */

    if (list->type == hl_directory && list->flags & HOTLIST_D_IS_OPEN)
    {
1263
      temp = hotlist_find_item_r(list->data.directory_content, item_no, curr_item);
1264 1265 1266 1267 1268 1269 1270 1271 1272
      if (temp) return temp;
    }

    /* Move to the next list item */

    list = list->next;
  }

  return NULL; /* List does not extend far enough */
1273 1274 1275 1276 1277
}

/*************************************************/
/* hotlist_find_no_from_item()                   */
/*                                               */
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
/* This function will recursivly scan through    */
/* a hotlist structure and return the position   */
/* of the specified item. It will only recurse   */
/* through open directories.                     */
/*                                               */
/* Parameters: Pointer to a hotlist_item to      */
/*             start at;                         */
/*                                               */
/*             Pointer to the item whos position */
/*             is to be returned.                */
1288
/*                                               */
1289 1290
/* Returns:    The item position or -1 if it is  */
/*             not found.                        */
1291 1292
/*************************************************/

1293
static int hotlist_find_no_from_item(hotlist_item * item)
1294
{
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
  int curr_item = 0;

  return hotlist_find_no_from_item_r(hotlist_root->data.directory_content, item, &curr_item);
}

/*************************************************/
/* hotlist_find_no_from_item_r()                 */
/*                                               */
/* Recursive back-end to                         */
/* hotlist_find_no_from_item.                    */
/*                                               */
/* Parameters: Pointer to a hotlist_item to      */
/*             start at;                         */
/*                                               */
/*             Pointer to the item whos position */
/*             is to be returned;                */
/*                                               */
/*             Pointer to an int, in which the   */
/*             number of the current item is     */
/*             accumulated (initialised to an    */
/*             appropriate value, usually 0).    */
/*                                               */
/* Returns:    As hotlist_find_no_from_item.     */
/*                                               */
/* Assumes:    The pointer to the int may *not*  */
/*             be NULL.                          */
/*************************************************/

static int hotlist_find_no_from_item_r(hotlist_item * list, hotlist_item * item, int * curr_item)
{
  /* Start the search at the given item */

  while (list)
1328
  {
1329 1330 1331 1332 1333 1334
    if (item == list) return *curr_item; /* Found the list item */

    *curr_item += 1;

    /* Recursively scan open directories */

1335 1336
    if (list->type == hl_directory && list->flags & HOTLIST_D_IS_OPEN)
    {
1337 1338 1339 1340
      int found;

      found = hotlist_find_no_from_item_r(list->data.directory_content, item, curr_item);
      if (found >= 0) return found;
1341 1342
    }

1343 1344 1345
    /* Move to the next item */

    list = list->next;
1346
  }
1347 1348 1349

  /* Didn't find it */

1350 1351 1352 1353
  return -1;
}

/*************************************************/
1354
/* hotlist_find_selected_item()                  */
1355
/*                                               */
1356 1357
/* This function returns a pointer to the first  */
/* selected item found.                          */
1358
/*                                               */
1359 1360 1361 1362 1363
/* Returns:    Pointer to a hotlist_item struct  */
/*             which was the first selected item */
/*             found in a search starting        */
/*             from the root, or NULL if there   */
/*             is nothing selected.              */
1364 1365
/*************************************************/

1366
hotlist_item * hotlist_find_selected_item(void)
1367
{
1368
  return hotlist_find_selected_item_r(hotlist_root);
1369 1370
}

1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
/*************************************************/
/* hotlist_find_selected_item_r()                */
/*                                               */
/* Recursive back-end to                         */
/* hotlist_find_selected_item.                   */
/*                                               */
/* Parameters: Pointer to a hotlist_item         */
/*             struct to start at.               */
/*                                               */
/* Returns:    As hotlist_find_selected_item.    */
/*************************************************/

1383
static hotlist_item * hotlist_find_selected_item_r(hotlist_item * list)
1384
{
1385
  while (list)
1386
  {
1387 1388 1389 1390
    if (list->flags & HOTLIST_G_IS_SELECTED) return list; /* Found it */

    /* Recursively scan all directories */

1391 1392
    if (list->type == hl_directory)
    {
1393 1394 1395 1396
      hotlist_item * found;

      found = hotlist_find_selected_item_r(list->data.directory_content);
      if (found) return found;
1397
    }
1398 1399 1400

    /* Move to the next item */

1401 1402
    list = list->next;
  }
1403 1404 1405

  /* Didn't find it */

1406 1407 1408 1409
  return NULL;
}

/*************************************************/
1410
/* hotlist_count_selected_items()                */
1411
/*                                               */
1412 1413 1414
/* Count the number of items that are currently  */
/* selected in the hotlist window.               */
/*                                               */
1415 1416 1417 1418 1419 1420 1421 1422 1423
/* If you want to know if all items are selected */
/* use hotlist_contents_selected. If you want to */
/* know if no items are selected, it is faster   */
/* to use hotlist_no_contents_selected than      */
/* compare the return value of this function     */
/* against zero (this function *must* scan all   */
/* hotlist items, whereas the other can exit as  */
/* soon as a selected item is found).            */
/*                                               */
1424
/* Returns:    The number of selected items.     */
1425 1426
/*************************************************/

1427
unsigned int hotlist_count_selected_items(void)
1428
{
1429 1430 1431 1432 1433
  int count = 0;

  hotlist_count_selected_items_r(hotlist_root->data.directory_content, &count);

  return count;
1434 1435 1436
}

/*************************************************/
1437
/* hotlist_count_selected_items_r()              */
1438
/*                                               */
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
/* Recursive back-end to                         */
/* hotlist_count_selected_items.                 */
/*                                               */
/* Parameters: Pointer to a hotlist_item struct  */
/*             representing the first item in a  */
/*             directory to count;               */
/*                                               */
/*             Pointer to an int, in which the   */
/*             total is accumulated.             */
/*                                               */
/* Assumes:    The pointer to the int may *not*  */
/*             be NULL.                          */
1451 1452
/*************************************************/

1453
static void hotlist_count_selected_items_r(hotlist_item * list, int * count)
1454
{
1455
  while (list)
1456
  {
1457 1458 1459 1460 1461
    if (list->flags & HOTLIST_G_IS_SELECTED)
    {
      *count += 1;
    }
    else
1462
    {
1463 1464 1465 1466 1467 1468
      /* Only recurse through directories which are not selected */

      if (list->type == hl_directory)
      {
        hotlist_count_selected_items_r(list->data.directory_content, count);
      }
1469
    }
1470

1471 1472 1473 1474 1475 1476 1477
    list = list->next;
  }
}

/*************************************************/
/* hotlist_count_displayed_items()               */
/*                                               */
1478 1479
/* This routine counts the number of items       */
/* displayed in the hotlist window.              */
1480
/*                                               */
1481 1482
/* Parameters: Pointer to a hotlist_item struct  */
/*             to start at.                      */
1483
/*                                               */
1484
/* Returns:    The number of displayed items.    */
1485 1486
/*************************************************/

1487
static unsigned int hotlist_count_displayed_items(hotlist_item * list)
1488
{
1489 1490 1491 1492 1493
  int count = 0;

  hotlist_count_displayed_items_r(list, &count);

  return count;
1494 1495 1496
}

/*************************************************/
1497
/* hotlist_count_displayed_items_r()             */
1498
/*                                               */
1499 1500
/* Recursive back-end to                         */
/* hotlist_count_displayed_items.                */
1501
/*                                               */
1502 1503 1504 1505 1506
/* Parameters: Pointer to a hotlist_item struct  */
/*             to start at;                      */
/*                                               */
/*             Pointer to an int, in which the   */
/*             total is accumulated.             */
1507
/*                                               */
1508 1509
/* Assumes:    The pointer to the int may *not*  */
/*             be NULL.                          */
1510 1511
/*************************************************/

1512
static void hotlist_count_displayed_items_r(hotlist_item *list, int * count)
1513
{
1514
  while (list)
1515
  {
1516 1517 1518
    /* Recursive scan for open directories */

    if (list->type == hl_directory && list->flags & HOTLIST_D_IS_OPEN)
1519
    {
1520
      hotlist_count_displayed_items_r(list->data.directory_content, count);
1521
    }
1522

1523
    list = list->next;
1524 1525

    *count += 1;
1526 1527 1528 1529
  }
}

/*************************************************/
1530
/* hotlist_draw()                                */
1531
/*                                               */
1532 1533 1534 1535
/* Redraws a specified region of the hotlist.    */
/* Assumes graphics rectangles are set up        */
/* appropriately (e.g. the function is called    */
/* during a Wimp redraw session).                */
1536
/*                                               */
1537 1538
/* Parameters: Pointer to a hotlist_item giving  */
/*             the list that we're to draw;      */
1539
/*                                               */
1540 1541 1542 1543
/*             First item number to draw (count  */
/*             the visible items from the top of */
/*             the window downwards starting at  */
/*             an item number of zero);          */
1544 1545
/*                                               */
/*             Last item number to draw.         */
1546 1547
/*************************************************/

1548
static _kernel_oserror * hotlist_draw(hotlist_item * list, unsigned int first_item, unsigned int last_item)
1549
{
1550 1551
  int          curr_item = 0;
  unsigned int item_height, item_dir_width, item_url_width;
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562

  RetError(hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width));

  return hotlist_draw_r(list,
                        first_item,
                        last_item,
                        &curr_item,
                        0,
                        item_height,
                        item_dir_width,
                        item_url_width);
1563 1564 1565
}

/*************************************************/
1566
/* hotlist_draw_r()                              */
1567
/*                                               */
1568
/* Recursive back-end to hotlist_draw.           */
1569
/*                                               */
1570 1571 1572
/* Parameters: Pointer to a hotlist_item giving  */
/*             the list that we're to draw;      */
/*                                               */
1573 1574 1575 1576
/*             First item number to draw (count  */
/*             the visible items from the top of */
/*             the window downwards starting at  */
/*             an item number of zero);          */
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
/*                                               */
/*             Last item number to draw;         */
/*                                               */
/*             Pointer to an int, which is used  */
/*             to accumulate the current item    */
/*             number - the contents should be   */
/*             initialised to an appropriate     */
/*             value for the first three         */
/*             parameters (usually, zero);       */
/*                                               */
/*             Level of indentation (OS units);  */
/*                                               */
/*             Height of an item (OS units);     */
/*                                               */
/*             Width of a directory item (OS     */
/*             units);                           */
/*                                               */
/*             Width of a URL item (OS units).   */
/*                                               */
/* Assumes:    The pointer to the int may *not*  */
/*             be NULL.                          */
1598 1599
/*************************************************/

1600 1601 1602
static _kernel_oserror * hotlist_draw_r(hotlist_item *list, unsigned int first_item, unsigned int last_item,
                                        int * curr_item, unsigned int indent, unsigned int item_height,
                                        unsigned int item_dir_width, unsigned int item_url_width)
1603
{
1604 1605 1606
  WimpIconBlock icon;
  unsigned int  temp_width;
  int           text_width;
1607

1608
  while (list)
1609
  {
1610 1611 1612 1613 1614 1615 1616
    /* Break out if done all items to be displayed */

    if (*curr_item > last_item) return NULL;

    /* Don't draw until we reach the required first item */

    if (*curr_item >= first_item)
1617
    {
1618
      /* Construct an icon block for Wimp_PlotIcon. First, icon flags. */
1619

1620
      icon.flags = HOTLIST_SPRITE_ICON_FLAGS;
1621 1622 1623 1624

      if (((list->flags & HOTLIST_G_IS_SELECTED) ? 1 : 0) ^
          ((list->flags & HOTLIST_G_DRAG_SELECTED) ? 1 : 0)
         ) icon.flags |= WimpIcon_Selected;
1625

1626
      /* Point to the main sprite pool */
1627

1628 1629 1630 1631 1632
      icon.data.is.sprite_area = (void*) sprite_block;

      /* Set type-dependent characteristics */

      switch(list->type)
1633 1634
      {
        case hl_url:
1635 1636 1637
        {
          /* A URL item; set the sprite name and item width accordingly */

1638
          icon.data.is.sprite             = !(list->flags & HOTLIST_G_IS_READ_ONLY) ? URL_SPRITE : RESOURCES_URL_SPRITE;
1639 1640 1641
          icon.data.is.sprite_name_length = strlen(URL_SPRITE);
          temp_width = item_url_width;
        }
1642 1643 1644 1645
        break;

        case hl_directory:
        {
1646 1647 1648 1649 1650 1651 1652
          /* Directories are a bit more complex, as they can be open, */
          /* closed, or showing a '+' if a dragged item is hovering   */
          /* over it and would drop 'into' the directory if released. */

          temp_width = item_dir_width; /* Width is based on the widest sprite of the three */

          if (list->flags & HOTLIST_D_IS_HIGHLIGHTED)
1653
          {
1654 1655 1656 1657
            /* The item is either selected (something is 'hovering' over it)... */

            icon.data.is.sprite             = INSERT_DIRECTORY_SPRITE;
            icon.data.is.sprite_name_length = strlen(INSERT_DIRECTORY_SPRITE);
1658 1659 1660
          }
          else
          {
1661 1662 1663 1664
            /* ...or unselected. In that case it is either open or closed. */

            if (list->flags & HOTLIST_D_IS_OPEN)
            {
1665
              icon.data.is.sprite = !(list->flags & HOTLIST_G_IS_READ_ONLY) ? OPEN_DIRECTORY_SPRITE : OPEN_RESOURCES_DIRECTORY_SPRITE;
1666 1667 1668
            }
            else
            {
1669
              icon.data.is.sprite = !(list->flags & HOTLIST_G_IS_READ_ONLY) ? CLOSED_DIRECTORY_SPRITE : CLOSED_RESOURCES_DIRECTORY_SPRITE;
1670
            }
1671 1672

            icon.data.is.sprite_name_length = strlen(icon.data.is.sprite);
1673 1674 1675 1676 1677
          }
        }
        break;

        default:
1678 1679 1680 1681
        {
          temp_width = 0; /* Should never happen... */
        }
        break;
1682 1683
      }

1684
      /* Set the item bounding box appropriately */
1685

1686 1687
      icon.bbox.xmin = indent;
      icon.bbox.xmax = indent + temp_width;
1688

1689 1690
      icon.bbox.ymin = -item_height * (*curr_item) - item_height;
      icon.bbox.ymax = -item_height * (*curr_item);
1691

1692
      /* Plot the item */
1693

1694
      RetError(wimp_plot_icon(&icon));
1695

1696 1697 1698
      /* We now need to plot the item text. First, get the width, */
      /* taking the opportunity to point the icon that we'll plot */
      /* to the appropriate text, too.                            */
1699 1700 1701

      if (list->type == hl_url && hl_show_urls)
      {
1702 1703 1704 1705 1706 1707 1708 1709 1710
        /* If this is a URL item and we are to show URLs, then find */
        /* the width of the URL rather than the description.        */

        RetError(utils_text_width(list->data.url, &text_width, 0));

        /* Point to the URL text */

        icon.data.it.buffer      = list->data.url;
        icon.data.it.buffer_size = strlen(list->data.url);
1711
        icon.data.it.validation  = NULL;
1712 1713 1714
      }
      else
      {
1715 1716 1717 1718 1719 1720 1721 1722 1723
        /* Otherwise (any other item, or a URL item when we're showing */
        /* descriptions) just find the name's width.                   */

        RetError(utils_text_width(list->name, &text_width, 0));

        /* Point to the description */

        icon.data.it.buffer      = list->name;
        icon.data.it.buffer_size = strlen(list->name);
1724
        icon.data.it.validation  = NULL;
1725
      }
1726

1727 1728
      /* Set the bounding box for the text. The hard coded */
      /* constants are nothing critical - just aesthetics. */
1729

1730 1731 1732 1733 1734 1735 1736 1737
      icon.bbox.xmin = indent + temp_width + 2;
      icon.bbox.xmax = indent + temp_width + 2 + text_width + 12;

      icon.bbox.ymin = -item_height * (*curr_item) - item_height + 2;
      icon.bbox.ymax = -item_height * (*curr_item) - 2;

      /* Set the flags accordingly if the text is selected or unselected */

1738 1739 1740
      if (((list->flags & HOTLIST_G_IS_SELECTED) ? 1 : 0) ^
          ((list->flags & HOTLIST_G_DRAG_SELECTED) ? 1 : 0)) icon.flags = HOTLIST_TEXT_ICON_FLAGS_SELECTED;
      else                                                   icon.flags = HOTLIST_TEXT_ICON_FLAGS_UNSELECTED;
1741 1742 1743 1744

      /* Finally, plot the item. */

      RetError(wimp_plot_icon(&icon));
1745 1746
    }

1747 1748 1749 1750 1751
    /* Increment the item number */

    *curr_item += 1;

    /* Recursive redraw for directories */
1752 1753 1754

    if (list->type == hl_directory && list->flags & HOTLIST_D_IS_OPEN)
    {
1755 1756 1757 1758 1759 1760 1761 1762
      RetError(hotlist_draw_r(list->data.directory_content,
                              first_item,
                              last_item,
                              curr_item,
                              indent + item_dir_width,
                              item_height,
                              item_dir_width,
                              item_url_width));
1763
    }
1764 1765 1766

    /* Move on down the list */

1767 1768 1769
    list = list->next;
  }

1770
  return NULL;
1771 1772 1773
}

/*************************************************/
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
/* hotlist_get_max_width_r                       */
/*                                               */
/* Recursive back-end to hotlist_get_max_width.  */
/*                                               */
/* Parameters: Pointer to a hotlist_item struct  */
/*             to start at;                      */
/*                                               */
/*             An indent in OS units, as for     */
/*             redrawing the hotlist;            */
/*                                               */
/*             Pointer to an int, in which the   */
/*             width of the widest item so far   */
/*             is accumulated;                   */
/*                                               */
/*             Height of a hotlist item in OS    */
/*             units;                            */
/*                                               */
/*             Width of a hotlist directory      */
/*             sprite in OS units;               */
/*                                               */
/*             Width of a hotlist URL sprite in  */
/*             OS units.                         */
1796
/*                                               */
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1797 1798
/* Assumes:    The pointer to the int may *not*  */
/*             be NULL.                          */
1799 1800
/*************************************************/

Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1801 1802
static _kernel_oserror * hotlist_get_max_width_r(hotlist_item *list, unsigned int indent, int * max_width,
                                                 unsigned int item_height, unsigned int item_dir_width, unsigned int item_url_width)
1803
{
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1804 1805 1806
  _kernel_oserror * e;
  unsigned int      item_width;
  int               text_width;
1807

Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1808
  while (list)
1809
  {
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1810 1811 1812
    /* Get width of the icon */

    switch (list->type)
1813
    {
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1814 1815 1816 1817
      case hl_directory: item_width = item_dir_width; break;
      case hl_url:       item_width = item_url_width; break;
      default:           item_width = 0;              break;
    }
1818

Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1819
    /* Work out width of the text */
1820

Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1821 1822 1823 1824 1825 1826
    if (list->type == hl_url && hl_show_urls) e = utils_text_width(list->data.url, &text_width, 0);
    else                                      e = utils_text_width(list->name,     &text_width, 0);

    if (e) return e;

    /* Account for the indent and spacers (aesthetics) */
1827

Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1828
    item_width += indent + 2 + text_width + 12;
1829

Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1830
    /* If this is wider than so far recorded, store the new value */
1831

Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1832 1833 1834
    if (item_width > *max_width) *max_width = item_width;

    /* Recursive call for open directories */
1835 1836 1837

    if (list->type == hl_directory && list->flags & HOTLIST_D_IS_OPEN)
    {
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1838 1839 1840 1841 1842 1843
      RetError(hotlist_get_max_width_r(list->data.directory_content,
                                       indent + item_dir_width,
                                       max_width,
                                       item_height,
                                       item_dir_width,
                                       item_url_width));
1844
    }
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1845 1846 1847

    /* Move on down the list */

1848 1849
    list = list->next;
  }
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1850 1851

  return NULL;
1852 1853 1854 1855 1856 1857
}

/*************************************************/
/* hotlist_get_max_width()                       */
/*                                               */
/* This function returns the maximum width of    */
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1858
/* of the displayed hotlist entries.             */
1859
/*                                               */
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1860 1861 1862 1863
/* Parameters: Pointer to a hotlist_item struct  */
/*             to start at. This is assumed to   */
/*             be at zero indent from the left   */
/*             hand side.                        */
1864
/*                                               */
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1865 1866
/* Returns:    Of all visible entries, the width */
/*             of the widest, in OS units.       */
1867 1868
/*************************************************/

Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1869
static unsigned int hotlist_get_max_width(hotlist_item * list)
1870
{
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
  unsigned int item_height, item_dir_width, item_url_width;
  int          widest = 0;

  /* Find basic item size information */

  if (hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width)) return 0;

  /* Scan the directory and all open directories with in it for */
  /* the widest item                                            */

  if (hotlist_get_max_width_r(list,
                              0,
                              &widest,
                              item_height,
                              item_dir_width,
                              item_url_width)) return 0;

  /* Return the result */

  return widest;
1891 1892 1893 1894
}

/*************************************************/
/* hotlist_redraw_now()                          */
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1895 1896 1897
/*                                               */
/* This function redraws all visible items with  */
/* the HOTLIST_G_REDRAW_NOW bit set.             */
1898 1899
/*************************************************/

1900
static _kernel_oserror * hotlist_redraw_now(void)
1901
{
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1902 1903
  int curr_item = 0;

1904
  return hotlist_redraw_now_r(hotlist_root->data.directory_content, &curr_item);
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
}

/*************************************************/
/* hotlist_redraw_now_r()                        */
/*                                               */
/* Recursive back-end to hotlist_redraw_now.     */
/*                                               */
/* Parameters: Pointer to a hotlist_item struct  */
/*             to start on;                      */
/*                                               */
/*             Pointer to an int, in which the   */
/*             current item number being redrawn */
/*             is accumulated.                   */
/*                                               */
/* Assumes:    The pointer to the int may *not*  */
/*             be NULL.                          */
/*************************************************/

1923
static _kernel_oserror * hotlist_redraw_now_r(hotlist_item * list, int * curr_item)
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1924 1925
{
  while (list)
1926 1927 1928
  {
    if (list->flags & HOTLIST_G_REDRAW_NOW)
    {
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1929 1930
      /* Redraw just the one item */

1931
      RetError(hotlist_redraw_items(*curr_item, *curr_item));
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1932 1933 1934

      /* Clear the flag */

1935 1936
      list->flags &= ~HOTLIST_G_REDRAW_NOW;
    }
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1937 1938 1939

    *curr_item += 1;

1940 1941
    if (list->type == hl_directory && list->flags & HOTLIST_D_IS_OPEN)
    {
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1942 1943
      /* Recursive call for open directories */

1944
      RetError(hotlist_redraw_now_r(list->data.directory_content, curr_item));
1945
    }
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
1946

1947 1948
    list = list->next;
  }
1949 1950

  return NULL;
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
}

/*************************************************/
/* hotlist_add()                                 */
/*                                               */
/* Add a new URL to the hotlist.                 */
/*                                               */
/* Parameters: Description of the URL (e.g. from */
/*             the page title);                  */
/*                                               */
/*             Pointer to the URL itself;        */
/*                                               */
/*             0 to add to the top, 1 to add to  */
/*             the bottom.                       */
/*************************************************/

_kernel_oserror * hotlist_add(char * description, char * url, int at_bottom)
{
  _kernel_oserror * e = NULL;
  int               position;
1971 1972
  hotlist_item    * target;
  int               type;
1973

1974 1975 1976 1977 1978
  target = hotlist_find_selected_item();

  if (!target)
  {
    position = at_bottom ? HOTLIST_POSITION_END : HOTLIST_POSITION_BEGINNING;
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
    target   = hotlist_root;
    type     = 1;

    /* If adding to the beginning of the hotlist, skip any */
    /* read-only items at the top first.                   */

    if (position == HOTLIST_POSITION_BEGINNING)
    {
      hotlist_item * check = target->data.directory_content;

      /* Look for something not read-only */

      while (check && check->next)
      {
        if (check->next->flags & HOTLIST_G_IS_READ_ONLY) check = check->next;
        else break;
      }

      if (check && (check->flags & HOTLIST_G_IS_READ_ONLY))
      {
        position = HOTLIST_POSITION_AFTER;
        target   = check;
      }
    }
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
  }
  else
  {
    if (target->type == hl_directory)
    {
      position = HOTLIST_POSITION_BEGINNING;
    }
    else
    {
      position = HOTLIST_POSITION_AFTER;
    }
2014

2015 2016
    type = 2;
  }
2017 2018 2019

  /* Add the item and ensure the window extent etc. is correct */

2020
  e = hotlist_new_url(target,
2021 2022 2023 2024
                      position,
                      description,
                      url);

2025
  if (!e) hotlist_preopen();
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040

  /* Optimise the redraw to do as little as possible depending */
  /* upon where in the list the item was added.                */

  switch (position)
  {
    default:
    case HOTLIST_POSITION_END:
    {
      hotlist_redraw_now();
    }
    break;

    case HOTLIST_POSITION_BEGINNING:
    {
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
      if (type == 1)
      {
        hotlist_redraw_items(0,
                             hotlist_count_displayed_items(hotlist_root->data.directory_content));
      }
      else
      {
        if (target->flags & HOTLIST_D_IS_OPEN)
        {
          hotlist_redraw_items(hotlist_find_no_from_item(target),
                               hotlist_count_displayed_items(hotlist_root->data.directory_content));
        }
      }
2054 2055
    }
    break;
2056

2057
    case HOTLIST_POSITION_BEFORE:
2058 2059 2060 2061 2062
    case HOTLIST_POSITION_AFTER:
    {
      hotlist_redraw_items(hotlist_find_no_from_item(hotlist_newitem),
                           hotlist_count_displayed_items(hotlist_root->data.directory_content));
    }
2063 2064
  }

2065 2066
  if (!e) return hotlist_modified(HL_MODIFIED_ADD);

2067 2068 2069 2070 2071 2072 2073
  return e;
}

/*************************************************/
/* hotlist_get_shape()                           */
/*                                               */
/* This function calculates the xmin and xmax    */
2074 2075 2076 2077 2078
/* of the passed hotlist_item.                   */
/*                                               */
/* Parameters: Pointer to an int, in which the   */
/*             xmin offset (in OS units) from    */
/*             the left hand edge is returned;   */
2079
/*                                               */
2080 2081 2082 2083 2084 2085 2086
/*             Similarly, a pointer to an int to */
/*             take the xmax offset;             */
/*                                               */
/*             Pointer to the hotlist_item to    */
/*             examine.                          */
/*                                               */
/* Assumes:    Any pointer may be NULL.          */
2087 2088
/*************************************************/

2089
static _kernel_oserror * hotlist_get_shape(int * xmin, int * xmax, hotlist_item * item)
2090
{
2091 2092 2093 2094 2095
  hotlist_item * tempitem;
  unsigned int   item_height, item_dir_width, item_url_width;
  int            icon_width;
  int            text_width;
  int            count = 0;
2096

2097 2098
  if (xmin) *xmin = 0;
  if (xmax) *xmax = 0;
2099

2100 2101 2102 2103 2104 2105 2106 2107
  if (!item) return NULL;

  /* Find entry sizes */

  RetError(hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width));

  /* Count how many parent items we have. This allows */
  /* the left hand indent to be calculated.           */
2108 2109

  tempitem = item;
2110 2111

  while (tempitem)
2112 2113 2114 2115 2116 2117 2118
  {
    tempitem = tempitem->parent;
    count++;
  }

  count -= 2;

2119 2120
  /* Find the text width */

2121 2122
  if (item->type == hl_url && hl_show_urls) utils_text_width(item->data.url, &text_width, 0);
  else                                      utils_text_width(item->name,     &text_width, 0);
2123

2124 2125 2126
  /* Add in the sprite width */

  switch (item->type)
2127
  {
2128
    case hl_directory: icon_width = item_dir_width;
2129 2130
    break;

2131
    case hl_url:       icon_width = item_url_width;
2132 2133
    break;

2134
    default:           icon_width = 0;
2135 2136 2137
    break;
  }

2138 2139 2140 2141 2142 2143
  /* Add up the total and exit */

  if (xmin) *xmin = count * item_dir_width;
  if (xmax) *xmax = count * item_dir_width + icon_width + 2 + text_width + 12;

  return NULL;
2144 2145 2146 2147 2148
}

/*************************************************/
/* hotlist_directory_open_close()                */
/*                                               */
2149 2150 2151 2152 2153 2154
/* This function opens or closes a directory,    */
/* dealing with all required redrawing.          */
/*                                               */
/* Parameters: Pointer to the hotlist_item       */
/*             struct representing the directory */
/*             to open or close;                 */
2155
/*                                               */
2156 2157 2158 2159
/*             Number of that item (counting the */
/*             visible hotlist items from the    */
/*             top of the window downwards,      */
/*             starting at zero).                */
2160 2161
/*************************************************/

2162
static _kernel_oserror * hotlist_directory_open_close(hotlist_item * item, unsigned int itemno)
2163
{
2164 2165 2166
  unsigned int item_height, item_dir_width, item_url_width;
  int          top, window_handle;
  BBox         bbox;
2167

2168
  RetError(hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width));
2169

2170
  /* Swap the flag saying whether the directory is open or not */
2171 2172

  item->flags ^= HOTLIST_D_IS_OPEN;
David Brown's avatar
David Brown committed
2173

2174 2175
  /* Clear all selected items within the directory */

2176
  hotlist_clear_flags(item->data.directory_content, hl_ALL, HOTLIST_G_IS_SELECTED | HOTLIST_G_REDRAW_NOW);
David Brown's avatar
David Brown committed
2177

2178 2179
  /* Do the appropriate redrawing */

2180
  hotlist_preopen();
2181

2182 2183
  RetError(window_get_wimp_handle(0, hotlist_windowid, &window_handle));
  RetError(window_get_extent(0, hotlist_windowid, &bbox));
2184

2185
  top = -itemno * item_height;
2186 2187 2188

  if (item->data.directory_content)
  {
2189 2190 2191 2192 2193
    RetError(wimp_force_redraw(window_handle,
                               bbox.xmin,
                               bbox.ymin,
                               bbox.xmax,
                               top));
2194 2195 2196
  }
  else
  {
2197 2198 2199 2200 2201
    RetError(wimp_force_redraw(window_handle,
                               bbox.xmin,
                               top-item_height,
                               bbox.xmax,
                               top));
2202
  }
2203 2204

  return NULL;
2205 2206 2207 2208 2209 2210
}

/*************************************************/
/* hotlist_redraw_items()                        */
/*                                               */
/* This function forces the redraw of a set of   */
2211 2212 2213 2214 2215 2216 2217
/* items.                                        */
/*                                               */
/* Parameters: Item number of the first item to  */
/*             redraw (counting the visible      */
/*             items from the top of the window  */
/*             downwards, starting at zero) -    */
/*             this is inclusive;                */
2218
/*                                               */
2219 2220
/*             Item number of the last item to   */
/*             redraw (also inclusive).          */
2221 2222
/*************************************************/

2223
static _kernel_oserror * hotlist_redraw_items(unsigned int firstitem, unsigned int lastitem)
2224
{
2225 2226 2227
  unsigned int item_height, item_dir_width, item_url_width;
  BBox         bbox;
  int          window_handle;
2228

2229
  /* Get the entry sizes */
2230

2231 2232 2233
  RetError(hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width));

  /* Find the window extent */
2234

2235 2236
  RetError(window_get_wimp_handle(0, hotlist_windowid, &window_handle));
  RetError(window_get_extent(0, hotlist_windowid, &bbox));
2237

2238 2239 2240 2241 2242 2243 2244 2245
  /* Force a redraw for the whole extent width, over a vertical */
  /* span determined by the given item numbers.                 */

  return wimp_force_redraw(window_handle,
                           bbox.xmin,
                           - (lastitem + 1) * item_height,
                           bbox.xmax,
                           - firstitem * item_height);
2246 2247 2248 2249 2250
}

/*************************************************/
/* hotlist_clear_selection()                     */
/*                                               */
2251 2252
/* This function unselects all items, redrawing  */
/* as required.                                  */
2253 2254
/*************************************************/

2255
_kernel_oserror * hotlist_clear_selection(void)
2256
{
2257 2258 2259 2260 2261
  if (
       hotlist_clear_flags(hotlist_root->data.directory_content,
                           hl_ALL,
                           HOTLIST_G_IS_SELECTED)
     )
2262
     return hotlist_redraw_now();
2263 2264

  return NULL;
2265 2266 2267 2268 2269 2270
}

/*************************************************/
/* hotlist_launch_url()                          */
/*                                               */
/* This function launches the url in the passed  */
2271
/* item.                                         */
2272
/*                                               */
2273 2274
/* Parameters: Pointer to a hotlist_item struct  */
/*             holding the URL to launch.        */
2275 2276
/*************************************************/

2277
static _kernel_oserror * hotlist_launch_url(hotlist_item * item)
2278 2279 2280 2281 2282
{
  #ifdef TRACE
    if (tl & (1u<<25)) hotlist_display_item(item);
  #endif

2283
  return windows_create_browser(item->data.url, NULL, NULL, NULL, Windows_CreateBrowser_Normal);
2284 2285 2286 2287 2288
}

/*************************************************/
/* hotlist_process_click_on_item()               */
/*                                               */
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
/* Deal with clicks on hotlist items.            */
/*                                               */
/* Parameters: Item number that was clicked upon */
/*             (counting visible items from the  */
/*             top of the window downwards,      */
/*             starting with item number 0);     */
/*                                               */
/*             Pointer to a hotlist_item struct  */
/*             representing the item clicked on; */
/*                                               */
/*             State of the mouse buttons, as    */
/*             Wimp_GetPointerInfo would return  */
/*             for clicks on a window of button  */
/*             type 10 (Double/Click/Drag);      */
/*                                               */
/*             Screen x coordinate of the click; */
/*                                               */
/*             Screen y coordinate of the click. */
2307 2308
/*************************************************/

2309
static _kernel_oserror * hotlist_process_click_on_item(unsigned int itemno, hotlist_item * item, int buttons, int x, int y)
2310
{
2311 2312
  _kernel_oserror * e = NULL;

2313
  switch (buttons)
2314
  {
2315 2316 2317
    /* The window button type is Double/Click/Drag, so */
    /* this represents a double click with Select.     */

2318 2319
    case Wimp_MouseButtonSelect:
    {
2320 2321 2322
      /* Open or close directories, open URLs in a new window */

      if (last_selected_item == itemno)
2323
      {
2324 2325
        switch(item->type)
        {
2326
          case hl_directory: e = hotlist_directory_open_close(item, itemno);
2327
          break;
2328

2329
          case hl_url:       e = hotlist_launch_url(item);
2330 2331 2332
          break;
        }

2333 2334
        if (e) show_error_ret(e);

2335 2336 2337
        /* Deselect the item that was double-clicked upon */

        item->flags &= ~HOTLIST_G_IS_SELECTED;
2338
        e = hotlist_redraw_items(itemno, itemno);
2339 2340
      }
    }
2341 2342 2343
    break;

    /* Double-click with Adjust */
2344 2345 2346

    case Wimp_MouseButtonAdjust:
    {
2347 2348 2349 2350
      /* Open or close directories, but for URLs, open them in */
      /* a new window and close the hotlist.                   */

      if (last_selected_item == itemno)
2351
      {
2352 2353
        switch(item->type)
        {
2354
          case hl_directory: e = hotlist_directory_open_close(item, itemno);
2355
          break;
2356

2357 2358
          case hl_url:
          {
2359 2360 2361
            e = hotlist_launch_url(item);

            if (!e) toolbox_hide_object(0, hotlist_windowid);
2362 2363 2364 2365
          }
          break;
        }

2366 2367
        if (e) show_error_ret(e);

2368 2369 2370
        /* Deselect the item that was double-clicked upon */

        item->flags &= ~HOTLIST_G_IS_SELECTED;
2371
        e = hotlist_redraw_items(itemno, itemno);
2372 2373
      }
    }
2374
    break;
2375

2376
    /* Drag with Select */
2377

2378 2379 2380 2381
    case 64:
    {
      e = hotlist_start_drag();
    }
2382 2383 2384 2385 2386
    break;

    /* Drag with Adjust */

    case 16:
2387
    {
2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
      /* If the item clicked upon wasn't selected, select it */

      if (!(item->flags & HOTLIST_G_IS_SELECTED))
      {
        item->flags |= HOTLIST_G_IS_SELECTED;

        last_selected_item = itemno;
      }

      /* Start the drag and redraw the item clicked upon */
2398

2399 2400 2401
      e = hotlist_start_drag();

      if (!e) e = hotlist_redraw_items(itemno, itemno);
2402 2403 2404
    }
    break;

2405 2406 2407
    /* Click with Select */

    case 1024:
2408
    {
2409
      if (!(item->flags & HOTLIST_G_IS_SELECTED)) /* (Do nothing when selected) */
David Brown's avatar
David Brown committed
2410
      {
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
        /* Clear the selected flags of everything else */

        e = hotlist_clear_selection();

        /* Select this item and if a directory, all items within it */

        item->flags |= HOTLIST_G_IS_SELECTED | HOTLIST_G_REDRAW_NOW;

        if (item->type == hl_directory) hotlist_set_flags(item->data.directory_content,
                                                          hl_ALL,
                                                          HOTLIST_G_IS_SELECTED);

        /* Redraw to reflect the selection */

        if (!e) e = hotlist_redraw_now();
David Brown's avatar
David Brown committed
2426
      }
2427 2428

      last_selected_item = itemno;
2429 2430 2431
    }
    break;

2432 2433 2434
    /* Click with Adjust */

    case 256:
2435
    {
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445
      /* Swap the selected state of the item */

      item->flags ^= HOTLIST_G_IS_SELECTED;
      item->flags |= HOTLIST_G_REDRAW_NOW;

      last_selected_item = itemno;

      /* If a directory, select or deselect the contents */

      if (item->type == hl_directory)
David Brown's avatar
David Brown committed
2446
      {
2447 2448 2449 2450 2451 2452 2453 2454
        if (item->flags & HOTLIST_G_IS_SELECTED)
        {
          hotlist_set_flags(item->data.directory_content, hl_ALL, HOTLIST_G_IS_SELECTED);
        }
        else
        {
          hotlist_clear_flags(item->data.directory_content, hl_ALL, HOTLIST_G_IS_SELECTED);
        }
David Brown's avatar
David Brown committed
2455
      }
2456 2457 2458 2459

      /* Redraw to reflect the selection */

      e = hotlist_redraw_now();
2460 2461 2462 2463
    }
    break;
  }

2464 2465 2466
  /* Return any errors that may have been generated */

  return e;
2467 2468 2469 2470 2471
}

/*************************************************/
/* hotlist_process_click()                       */
/*                                               */
2472 2473 2474 2475 2476 2477 2478 2479
/* This function deals with mouse clicks on the  */
/* hotlist window.                               */
/*                                               */
/* Parameters: X position of click, in window    */
/*             coords;                           */
/*                                               */
/*             Y position of click, in window    */
/*             coords;                           */
2480
/*                                               */
2481 2482
/*             Button state (as returned by      */
/*             Wimp_GetPointerInfo).             */
2483 2484 2485 2486
/*************************************************/

static _kernel_oserror * hotlist_process_click(int x, int y, int buttons)
{
2487 2488 2489 2490 2491 2492 2493
  _kernel_oserror * e = NULL;
  hotlist_item    * item;
  unsigned int      item_height, item_dir_width, item_url_width;
  unsigned int      itemno;
  int               xmin, xmax;

  /* Get information on what was clicked upon */
2494

2495
  RetError(hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width));
2496 2497

  itemno = -y / item_height;
2498 2499 2500
  item   = hotlist_find_item(hotlist_root->data.directory_content, itemno);

  if (item) RetError(hotlist_get_shape(&xmin, &xmax, item));
2501 2502 2503

  if (item && x >= xmin && x <= xmax)
  {
2504 2505
    /* If we have a specific item, process the click on it */

2506 2507 2508 2509
    return hotlist_process_click_on_item(itemno, item, buttons, x, y);
  }
  else
  {
2510 2511
    /* Otherwise, actions depend on the button type */

2512 2513
    switch(buttons)
    {
2514
      /* Drag with Select */
2515

2516 2517 2518
      case 64:
      {
        RetError(hotlist_clear_selection());
2519

2520 2521
        e = hotlist_selection_box_start();
      }
2522 2523
      break;

2524 2525 2526 2527 2528 2529
      /* Drag with Adjust */

      case 16:
      {
        e = hotlist_selection_box_start();
      }
2530 2531
      break;

2532 2533
      /* Click with Select */

2534
      case 1024:
2535 2536 2537 2538 2539 2540 2541 2542
      {
        e = hotlist_clear_selection();

        last_selected_item = 0xffffffff;
      }
      break;

      /* Click with Adjust */
2543 2544

      case 256:
2545 2546 2547 2548
      {
        last_selected_item = 0xffffffff;
      }
      break;
2549 2550
    }
  }
2551 2552

  return e;
2553 2554 2555
}

/*************************************************/
2556
/* hotlist_preopen()                             */
2557 2558
/*                                               */
/* This function should be called before opening */
2559 2560 2561 2562 2563 2564 2565
/* the hotlist window. If the window is already  */
/* showing, then the extent may be altered but   */
/* it will never shrink the visible area of the  */
/* window. If the window is currently closed, it */
/* will set the extent to the minimum possible   */
/* value, which may well drag the visible area   */
/* down too.                                     */
2566
/*                                               */
2567
/* Returns:    1 if window was already open,     */
2568
/*             or 0 if it was closed.            */
2569 2570
/*************************************************/

2571
static int hotlist_preopen(void)
2572
{
2573 2574 2575 2576 2577 2578 2579 2580 2581
  _kernel_oserror         * e;
  WimpGetWindowStateBlock   state;
  BBox                      bbox;
  ObjectId                  parent_id;
  ComponentId               parent_component;
  unsigned int              item_height, item_dir_width, item_url_width;
  unsigned int              number, maxlen;
  unsigned int              objectstate;
  int                       height, width;
2582

2583 2584 2585 2586
  #define HotlistPreopen_ShowError(e) {if(e){show_error_ret(e);return 0;}}

  /* Is the window open or closed? */

2587
  e = toolbox_get_object_state(0, hotlist_windowid, &objectstate);
2588
  HotlistPreopen_ShowError(e);
2589

2590
  /* Get entry sizes */
2591

2592
  e = hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width);
2593
  HotlistPreopen_ShowError(e);
2594

2595
  /* General information */
2596 2597 2598 2599

  number = hotlist_count_displayed_items(hotlist_root->data.directory_content);
  maxlen = hotlist_get_max_width(hotlist_root->data.directory_content) + 4;

2600
  /* Find out window details */
2601

2602 2603
  e = window_get_wimp_handle(0, hotlist_windowid, &state.window_handle);
  HotlistPreopen_ShowError(e);
2604 2605

  e = wimp_get_window_state(&state);
2606 2607 2608
  HotlistPreopen_ShowError(e);

  /* Sanity check... */
2609 2610 2611 2612

  if (number < HOTLIST_WINDOW_MIN_HEIGHT) number = HOTLIST_WINDOW_MIN_HEIGHT;
  if (maxlen < HOTLIST_WINDOW_MIN_WIDTH)  maxlen = HOTLIST_WINDOW_MIN_WIDTH;

2613 2614
  /* If the window is open, get its extent */

2615
  if (objectstate & Toolbox_GetObjectState_Showing)
2616 2617
  {
    e = window_get_extent(0, hotlist_windowid, &bbox);
2618
    HotlistPreopen_ShowError(e);
2619
  }
2620

2621 2622 2623
  /* Work out the y extent and x extent required for showing */
  /* as much of the visible items in the hotlist as possible */

2624 2625 2626
  bbox.ymin = -number * item_height;
  bbox.xmax = maxlen;

2627 2628 2629 2630
  /* If the window was already open, don't let the visible */
  /* area change - otherwise go for a best fit to the      */
  /* items, as worked out above.                           */

2631
  if (objectstate & Toolbox_GetObjectState_Showing)
2632 2633 2634 2635 2636 2637 2638 2639
  {
    width  = state.visible_area.xmax - state.visible_area.xmin;
    height = state.visible_area.ymax - state.visible_area.ymin + hotlist_bbar_size;

    if (bbox.ymin > -height) bbox.ymin = -height;
    if (bbox.xmax < width) bbox.xmax = width;
  }

2640 2641
  /* Account for a button bar, if one were present. */

2642 2643 2644
  bbox.ymax = hotlist_bbar_size;
  bbox.xmin = 0;

2645 2646
  /* Set the extent */

2647
  e = window_set_extent(0, hotlist_windowid, &bbox);
2648 2649 2650
  HotlistPreopen_ShowError(e);

  /* (Re)open the window */
2651

2652
  if (objectstate & Toolbox_GetObjectState_Showing)
2653
  {
2654
    e = window_get_wimp_handle(0, hotlist_windowid, &state.window_handle);
2655

2656 2657 2658 2659
    if (!e) e = wimp_get_window_state(&state);
    if (!e) e = toolbox_get_parent(0, hotlist_windowid, &parent_id, &parent_component);
    if (!e) e = toolbox_show_object(0, hotlist_windowid, Toolbox_ShowObject_FullSpec, &(state.visible_area), parent_id, parent_component);

2660
    HotlistPreopen_ShowError(e);
2661 2662
  }

2663
  return !!(objectstate & Toolbox_GetObjectState_Showing);
2664 2665 2666 2667 2668
}

/*************************************************/
/* hotlist_redraw_handler()                      */
/*                                               */
2669 2670 2671 2672 2673
/* This handles redraw events from the Wimp, for */
/* the hotlist window.                           */
/*                                               */
/* Parameters are as standard for a Wimp event   */
/* handler.                                      */
2674 2675
/*************************************************/

2676
static int hotlist_redraw_handler(int event_code, WimpPollBlock * event, IdBlock * id_block, void * handle)
2677
{
2678 2679 2680 2681 2682
  _kernel_oserror       * e = NULL;
  WimpRedrawWindowBlock   block;
  unsigned int            item_height, item_dir_width, item_url_width;
  unsigned int            first_item, last_item;
  int                     more;
2683

2684 2685 2686
  /* Get entry sizes */

  ChkError(hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width));
2687 2688 2689

  block.window_handle = event->redraw_window_request.window_handle;

2690 2691
  /* Start the redraw loop */

2692
  wimp_redraw_window(&block, &more);
2693 2694

  while (more && !e)
2695
  {
2696 2697 2698
    /* Work out which bits to redraw... */

    first_item =  -(block.redraw_area.ymax - (block.visible_area.ymax - block.yscroll)) / item_height;
2699 2700
    last_item  = (-(block.redraw_area.ymin - (block.visible_area.ymax - block.yscroll)) / item_height) + 1;

2701 2702 2703 2704 2705 2706 2707 2708 2709
    /* ...and redraw them */

    e = hotlist_draw(hotlist_root->data.directory_content,
                     first_item,
                     last_item);

    /* Get the next redraw rectangle */

    if (!e) e = wimp_get_rectangle(&block, &more);
2710
  }
2711 2712

  return 1;
2713 2714 2715 2716 2717
}

/*************************************************/
/* hotlist_mouse_click_handler()                 */
/*                                               */
2718 2719 2720 2721 2722 2723 2724
/* Event handler to deal with mouse clicks in    */
/* the hotlist window; converts coordinates from */
/* screen to window, and runs the result through */
/* hotlist_process_click.                        */
/*                                               */
/* Parameters are as standard for a Wimp event   */
/* handler.                                      */
2725 2726
/*************************************************/

2727
static int hotlist_mouse_click_handler(int event_code, WimpPollBlock * event, IdBlock * id_block, void * handle)
2728 2729 2730 2731 2732 2733 2734 2735
{
  WimpGetWindowStateBlock state;

  state.window_handle = event->mouse_click.window_handle;
  wimp_get_window_state(&state);

  wimp_set_caret_position(state.window_handle, -1, 0, 0, -1, -1);

2736 2737 2738 2739
  ChkError(hotlist_process_click(event->mouse_click.mouse_x + (state.xscroll - state.visible_area.xmin),
                                 event->mouse_click.mouse_y + (state.yscroll - state.visible_area.ymax),
                                 event->mouse_click.buttons));
  return 1;
2740 2741 2742 2743 2744
}

/*************************************************/
/* hotlist_menuclose_handler()                   */
/*                                               */
2745 2746 2747 2748
/* Called when menus close.                      */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
2749 2750
/*************************************************/

2751
static int hotlist_menuclose_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
2752 2753
{
  ObjectId submenu_id;
2754 2755 2756

  /* Deselect any items selected by the menu */

David Brown's avatar
David Brown committed
2757
  if (menu_select)
2758 2759
  {
    hotlist_clear_selection();
David Brown's avatar
David Brown committed
2760
    menu_select = 0;
2761 2762
  }

2763 2764 2765 2766 2767 2768 2769
  /* Get rid of any submenus */

  ChkError(menu_get_sub_menu_show(0, id_block->self_id, HOTLIST_URL_MENUITEM, &submenu_id));

  if (submenu_id) ChkError(toolbox_delete_object(0, submenu_id));

  ChkError(menu_set_sub_menu_show(0, id_block->self_id, HOTLIST_URL_MENUITEM, 0));
2770

2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784
  return 1;
}

/*************************************************/
/* hotlist_set_menu_details()                    */
/*                                               */
/* When a menu is to be opened or the hotlist    */
/* state changes in a way that can affect open   */
/* menus, this can be used to update the menu    */
/* contents.                                     */
/*                                               */
/* Parameters: Object ID of the hotlist root     */
/*             menu.                             */
/*************************************************/
2785

2786
static _kernel_oserror * hotlist_set_menu_details(ObjectId menuid)
2787
{
2788 2789 2790
  hotlist_item * item;
  char           entrytext[Limits_Hotlist_ItemName];
  ObjectId       submenu_id;
2791

2792
  switch (hotlist_count_selected_items())
2793
  {
2794 2795 2796 2797
    /* No selected items - set the URL/Directory name string to */
    /* "URL ''" and grey it out, along with the Clear Selection */
    /* entry.                                                   */

2798
    case 0:
2799
    {
2800 2801 2802 2803
      RetError(menu_set_entry_text(0, menuid, HOTLIST_URL_MENUITEM, "URL ''"));

      RetError(menu_set_fade(0, menuid, HOTLIST_URL_MENUITEM, 1));
      RetError(menu_set_fade(0, menuid, HOTLIST_CLEARSELECTION_MENUITEM, 1));
2804
    }
2805 2806
    break;

2807 2808
    /* Only 1 item selected */

2809 2810
    case 1:
    {
2811 2812 2813
      int name_len;

      item = hotlist_find_selected_item();
2814

2815 2816
      switch(item->type)
      {
2817 2818 2819 2820
        /* One directory selected - set the URL/Directory name */
        /* string to a relevant value, and create and attach   */
        /* the relevant submenu.                               */

2821
        case hl_directory:
2822 2823 2824 2825 2826
        {
          StrNCpy0(entrytext, "Dir. '");

          RetError(toolbox_create_object(0, "HLDirmenu", &submenu_id));
          RetError(menu_set_sub_menu_show(0, menuid, HOTLIST_URL_MENUITEM, submenu_id));
2827 2828 2829 2830 2831

          /* If the item is read-only, grey out the 'delete' entry */

          if (item->flags & HOTLIST_G_IS_READ_ONLY) menu_set_fade(0, submenu_id, HOTLIST_DELETE_SUBMENUITEM, 1);
          else                                      menu_set_fade(0, submenu_id, HOTLIST_DELETE_SUBMENUITEM, 0);
2832
        }
2833
        break;
2834

2835 2836
        /* Similarly, deal with a single URL being selected */

2837
        case hl_url:
2838 2839 2840 2841 2842
        {
          StrNCpy0(entrytext, "URL '");

          RetError(toolbox_create_object(0, "HLURLmenu", &submenu_id));
          RetError(menu_set_sub_menu_show(0, menuid, HOTLIST_URL_MENUITEM, submenu_id));
2843 2844 2845

          if (item->flags & HOTLIST_G_IS_READ_ONLY) menu_set_fade(0, submenu_id, HOTLIST_DELETE_SUBMENUITEM, 1);
          else                                      menu_set_fade(0, submenu_id, HOTLIST_DELETE_SUBMENUITEM, 0);
2846
        }
2847
        break;
2848
      }
2849

2850 2851 2852 2853 2854 2855 2856 2857 2858 2859
      name_len = strlen(item->name);

      /* Add the item name in. If the whole item will not fit, put as much as */
      /* will do, followed by '...'.                                          */

      if (name_len < sizeof(entrytext) - strlen(entrytext) - 1) strcat(entrytext, item->name); /* ('<' accounts for terminator, -1 accounts for closing "'") */
      else if (sizeof(entrytext) - strlen(entrytext) > 5)
      {
        strncat(entrytext, item->name, sizeof(entrytext) - strlen(entrytext) - 5);             /* (-5 accounts for closing "'", "..." and terminator)        */
        strcat(entrytext, "...");
2860
      }
2861

2862 2863 2864 2865 2866 2867 2868 2869
      if (strlen(entrytext) < sizeof(entrytext) - 1) strcat(entrytext, "'");

      /* Set the text, unfade the item, and unfade the Clear Selection item */

      RetError(menu_set_entry_text(0, menuid, HOTLIST_URL_MENUITEM, entrytext));

      RetError(menu_set_fade(0, menuid, HOTLIST_URL_MENUITEM, 0));
      RetError(menu_set_fade(0, menuid, HOTLIST_CLEARSELECTION_MENUITEM, 0));
2870
    }
2871 2872
    break;

2873 2874
    /* Many items selected */

2875
    default:
2876
    {
2877 2878 2879 2880 2881 2882 2883 2884 2885 2886
      /* Set the URL/Directory name string, ungrey that item and Clear Selection, */
      /* and create and attach an appropriate submenu.                            */

      RetError(menu_set_entry_text(0, menuid, HOTLIST_URL_MENUITEM, "Selection"));

      RetError(menu_set_fade(0, menuid, HOTLIST_URL_MENUITEM, 0));
      RetError(menu_set_fade(0, menuid, HOTLIST_CLEARSELECTION_MENUITEM, 0));

      RetError(toolbox_create_object(0, "HLSlctmenu", &submenu_id));
      RetError(menu_set_sub_menu_show(0, menuid, HOTLIST_URL_MENUITEM, submenu_id));
2887
    }
2888 2889 2890
    break;

  }
2891

2892 2893 2894
  /* If *everything* is selected, want to grey out Select All; */
  /* else ungrey it.                                           */

2895 2896
  if (hotlist_contents_selected(hotlist_root)) RetError(menu_set_fade(0, menuid, HOTLIST_SELECTALL_MENUITEM, 1))
  else                                         RetError(menu_set_fade(0, menuid, HOTLIST_SELECTALL_MENUITEM, 0))
2897

2898 2899 2900 2901
  /* Set the ticks on 'Show Descriptions' or 'Show URLs' in the */
  /* Display submenu.                                           */

  RetError(menu_get_sub_menu_show(0, menuid, HOTLIST_DISPLAY_MENUITEM, &submenu_id));
2902 2903 2904

  if (hl_show_urls)
  {
2905 2906
    RetError(menu_set_tick(0, submenu_id, HOTLIST_MENU_SHOWDESCRIPTIONS, 0));
    RetError(menu_set_tick(0, submenu_id, HOTLIST_MENU_SHOWURLS, 1));
2907 2908 2909
  }
  else
  {
2910 2911
    RetError(menu_set_tick(0, submenu_id, HOTLIST_MENU_SHOWDESCRIPTIONS, 1));
    RetError(menu_set_tick(0, submenu_id, HOTLIST_MENU_SHOWURLS, 0));
2912
  }
2913 2914 2915 2916

  /* Finished */

  return NULL;
2917 2918 2919 2920 2921
}

/*************************************************/
/* hotlist_menuopen_handler()                    */
/*                                               */
2922 2923 2924 2925 2926
/* Handles events raised when menus are about to */
/* be shown.                                     */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
2927 2928 2929 2930 2931 2932 2933 2934
/*************************************************/

static int hotlist_menuopen_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
{
  WimpGetWindowStateBlock   state;
  WimpGetPointerInfoBlock   pointerblock;
  hotlist_item            * item;
  unsigned int              item_height, item_dir_width, item_url_width;
2935
  int                       xmin, xmax, window_handle;
2936
  ObjectId                  sub_menu;
2937

2938 2939 2940
  /* Get entry sizes. */

  ChkError(hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width));
2941

2942
  if (event_code == EHotlistToBeShown)
2943
  {
2944 2945 2946 2947
    /* Work out which item the pointer was over. */

    ChkError(window_get_wimp_handle(0, hotlist_windowid, &window_handle));

2948
    state.window_handle = window_handle;
2949 2950 2951
    ChkError(wimp_get_window_state(&state));

    ChkError(wimp_get_pointer_info(&pointerblock));
2952 2953 2954 2955 2956 2957 2958

    pointerblock.x = pointerblock.x + (state.xscroll - state.visible_area.xmin);
    pointerblock.y = pointerblock.y + (state.yscroll - state.visible_area.ymax);

    menu_itemno = -pointerblock.y / item_height;
  }

2959 2960 2961 2962
  ChkError(menu_get_sub_menu_show(0, id_block->self_id, HOTLIST_DISPLAY_MENUITEM, &sub_menu));

  /* If there are no selected items, select the one the menu */
  /* was opened over.                                        */
2963

David Brown's avatar
David Brown committed
2964
  if (hotlist_count_selected_items() == 0)
2965 2966 2967
  {
    item = hotlist_find_item(hotlist_root->data.directory_content, menu_itemno);

2968
    if (item) ChkError(hotlist_get_shape(&xmin, &xmax, item));
2969 2970 2971

    if (item && pointerblock.x >= xmin && pointerblock.x <= xmax)
    {
2972 2973
      item->flags |= HOTLIST_G_IS_SELECTED | HOTLIST_G_REDRAW_NOW;
      if (item->type == hl_directory) hotlist_set_flags(item->data.directory_content, hl_ALL, HOTLIST_G_IS_SELECTED);
2974

David Brown's avatar
David Brown committed
2975
      menu_select = 1;
2976 2977

      ChkError(hotlist_redraw_now());
2978 2979 2980
    }
  }

2981 2982 2983
  /* Update the menus */

  ChkError(hotlist_set_menu_details(id_block->self_id));
2984 2985 2986

  return 0;
}
2987

2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000
/*************************************************/
/* hotlist_save_entries()                        */
/*                                               */
/* This function recurses through the hotlist    */
/* directory structure saving all directories    */
/* and entries as it goes.                       */
/*                                               */
/* Parameters: Pointer to a FILE struct for the  */
/*             file to write to;                 */
/*                                               */
/*             Pointer to a hotlist_item struct  */
/*             representing the first item in    */
/*             the directory to save (which may  */
3001
/*             itself be a directory);           */
3002
/*                                               */
3003 3004 3005 3006 3007 3008 3009 3010
/*             0 - save all of hotlist,          */
/*             1 - only save selection portions, */
/*             but in both cases obey the next   */
/*             parameter;                        */
/*                                               */
/*             0 - don't save read-only items,   */
/*             1 - allow saving of read-only     */
/*             items.                            */
3011
/*                                               */
3012 3013 3014 3015 3016
/* Assumes:    The FILE pointer must not be NULL */
/*             however the hotlist_item pointer  */
/*             can be (e.g. an empty directory). */
/*************************************************/

3017
static _kernel_oserror * hotlist_save_entries(FILE * fileptr, hotlist_item * list, int type, int save_read_only)
3018
{
3019
  int written;
3020 3021 3022 3023 3024

  /* Follow the directory list */

  while (list)
  {
3025
    /* Don't save read-only entries unless told to */
3026

3027 3028 3029
    if (save_read_only || !(list->flags & HOTLIST_G_IS_READ_ONLY))
    {
      switch (list->type)
3030
      {
3031 3032 3033
        /* Write a link for URLs */

        case hl_url:
3034
        {
3035 3036 3037 3038
          if (type == 0 || (type == 1 && list->flags & HOTLIST_G_IS_SELECTED))
          {
            HotlistWrite(fprintf(fileptr, "<li><a href=\"%s\">%s</a>\n", list->data.url, list->name));
          }
3039
        }
3040
        break;
3041

3042
        /* Write a heading for directories */
3043

3044
        case hl_directory:
3045
        {
3046
          if (type == 0 || !hotlist_no_contents_selected(list->data.directory_content))
3047
          {
3048
            if (type == 0 || (type == 1 && list->flags & HOTLIST_G_IS_SELECTED))
3049
            {
3050 3051 3052 3053 3054 3055 3056 3057 3058 3059
              HotlistWrite(fprintf(fileptr, "<h4>%s</h4>\n", list->name));

              if (list->flags & HOTLIST_D_IS_OPEN)
              {
                HotlistWrite(fprintf(fileptr, "<ul><!--open-->\n"));
              }
              else
              {
                HotlistWrite(fprintf(fileptr, "<ul>\n"));
              }
3060
            }
3061 3062
            /* Recursive call for the directory contents First, */
            /* write the entry header.                          */
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
3063

3064
            /* Do the contents */
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
3065

3066 3067 3068 3069 3070
            RetError(hotlist_save_entries(fileptr, list->data.directory_content, type, save_read_only));
            if (type == 0 || (type == 1 && list->flags & HOTLIST_G_IS_SELECTED))
            {
              HotlistWrite(fprintf(fileptr, "</ul>\n"));
            }
3071
          }
3072
        }
3073
        break;
3074 3075 3076 3077 3078 3079 3080 3081 3082
      }
    }

    /* Continue down the list */

    list = list->next;
  }

  return NULL;
3083 3084 3085 3086 3087 3088

  /* Error condition exit */

hotlist_save_error:

  RetLastE;
3089 3090 3091
}

/*************************************************/
3092
/* hotlist_save_hotlist()                        */
3093 3094 3095 3096
/*                                               */
/* This function saves the hotlist as an HTML    */
/* file.                                         */
/*                                               */
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
3097
/* Parameters: Pointer to the filename to save   */
3098 3099 3100 3101 3102 3103
/*             to (null terminated);             */
/*                                               */
/*             Pointer to a buffer containing    */
/*             any extra information to put in   */
/*             the top of the file (intended for */
/*             multiuser builds);                */
3104
/*                                               */
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
3105 3106
/*             0 to save all of hotlist, 1 to    */
/*             save only the selected portions.  */
3107
/*************************************************/
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
3108

3109
_kernel_oserror * hotlist_save_hotlist(char * filename, char * extradata, int type)
3110 3111
{
  _kernel_oserror * e;
3112 3113
  FILE            * fileptr;
  static char     * local_path = NULL;
3114 3115
  int               written;

3116 3117
  if (!filename || !*filename) return NULL;

3118
  /* Canonicalise the path */
3119

3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130
  RetError(utils_canonicalise_path(filename, &local_path));

  /* Ensure it is present */

  e = utils_build_tree(local_path);

  if (e)
  {
    free(local_path);
    return e;
  }
3131

3132 3133 3134 3135
  /* Could take a while... */

  _swix(Hourglass_On, 0);

3136 3137
  /* Open the file for writng */

3138
  fileptr = fopen(local_path, "wb");
3139 3140 3141

  /* Complain if it fails */

3142 3143 3144 3145 3146
  if (fileptr == NULL)
  {
    free(local_path);
    RetLastE;
  }
3147

3148 3149 3150 3151
  /* Write the extra data */

  if (extradata) HotlistWrite(fprintf(fileptr, "%s", extradata));

3152 3153
  /* Write the file header */

3154 3155 3156
  HotlistWrite(fprintf(fileptr, "<html>\n"
                                "<head>\n"
                                CHARSET_SPECIFIER
3157 3158 3159 3160 3161
                                "<title>"));

  HotlistWrite(fprintf(fileptr, "%s", lookup_token("HotlistHTMLTitle:Hotlist",0,0)));

  HotlistWrite(fprintf(fileptr, "</title>\n"
3162 3163 3164
                                "</head>\n"
                                "<body>\n"
                                "<ul>\n"));
3165 3166 3167

  /* Fill in the body */

3168 3169 3170 3171
  e = hotlist_save_entries(fileptr,
                           hotlist_root->data.directory_content,
                           type,
                           type == 1 ? 1 : 0); /* If saving part of the hotlist, allow saving of read-only components */
3172 3173 3174

  if (e)
  {
3175 3176
    _swix(Hourglass_Off, 0);

3177
    fclose(fileptr);
3178
    free(local_path);
3179 3180 3181 3182 3183 3184

    return e;
  }

  /* Write the footer and close the file */

3185
  HotlistWrite(fprintf(fileptr, "</ul>\n"));
3186 3187 3188 3189 3190
  HotlistWrite(fprintf(fileptr, "</body>\n"));
  HotlistWrite(fprintf(fileptr, "</html>\n"));

  fclose(fileptr);

3191 3192
  _swix(Hourglass_Off, 0);

3193 3194
  /* Set the filetype to HTML (0xfaf) */

3195 3196 3197 3198 3199 3200
  e = _swix(OS_File,
            _INR(0,2),

            18,
            local_path,
            FileType_HTML);
3201

3202 3203 3204 3205 3206 3207 3208 3209
  free(local_path);

  return e;

  /* Error condition exit */

hotlist_save_error:

3210 3211 3212 3213 3214 3215
  if (fileptr)
  {
    fclose(fileptr);

    _swix(Hourglass_Off, 0);
  }
3216 3217 3218 3219

  free(local_path);

  RetLastE;
3220 3221
}

3222 3223 3224 3225
/*************************************************/
/* hotlist_save()                                */
/*                                               */
/* Veneer onto hotlist_save_hotlist - saves all  */
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
3226
/* of the hotlist, created to preserve API.      */
3227
/*                                               */
Andrew Hodgkinson's avatar
Andrew Hodgkinson committed
3228
/* Parameters: Pointer to the filename to save   */
3229
/*             to (null terminated).             */
3230 3231 3232 3233
/*************************************************/

_kernel_oserror * hotlist_save(char * filename)
{
3234
  return hotlist_save_hotlist(filename, NULL, 0); /* Save entire hotlist */
3235 3236
}

3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315
/*************************************************/
/* hotlist_lower_tags()                          */
/*                                               */
/* This function processes the passed string     */
/* turning all characters within a tag to lower  */
/* case. It will detect characters within quotes */
/* and leave their case the same; this will      */
/* preserve the case of URLs.                    */
/*                                               */
/* Obviously, this assumes that the HTML file    */
/* being fed in is not broken; tags and quoted   */
/* text must always be correctly closed, and     */
/* in both cases must not span multiple lines.   */
/*                                               */
/* Parameters: Pointer to the string to process. */
/*************************************************/

static void hotlist_lower_tags(char *string)
{
  int intag = 0, inquotes = 0;

  while (*string)
  {
    if (intag)
    {
      if (inquotes)
      {
        if (*string == '"') inquotes = 0;
      }
      else
      {
        if (*string == '"') inquotes = 1;
        if (*string == '>') intag --;

        *string = tolower(*string);
      }
    }
    else
    {
      if (*string == '<') intag++;
    }

    string++;
  }
}

/*************************************************/
/* hotlist_load_directory()                      */
/*                                               */
/* This function loads the directory contents of */
/* a hotlist HTML file previously saved by       */
/* hotlist_save or a compatible source. For      */
/* example, at the time of creation this can     */
/* correctly load and understand hotlist files   */
/* from at least one other popular browser.      */
/*                                               */
/* Parameters: Pointer to a FILE struct through  */
/*             which data will be read;          */
/*                                               */
/*             Pointer to a hotlist_item; new    */
/*             data structures generated from    */
/*             the file contents are added to    */
/*             the linked list that this struct  */
/*             lies in.                          */
/*************************************************/

static _kernel_oserror * hotlist_load_directory(FILE * fileptr, hotlist_item * target)
{
  _kernel_oserror * e = NULL;

  static char     * next_directory_name = NULL;
  static char     * string_buffer       = NULL;
  static char     * str_ptr;

  char            * url;
  hotlist_item    * new_dir;
  unsigned int      unfollowed_uls = 0;
  long int          file_position;
  int               character, count;
3316
  int               target_type;
3317

3318 3319
  /* Go through the file in chunks */

3320 3321
  target_type = 0; /* At end of directory */

3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368
  while (!feof(fileptr)) /* In theory the code below means you'll never get this; but just to be safe... */
  {
    file_position = ftell(fileptr);

    /* Scan ahead to find the end of line - marked by any */
    /* control character, in this case; need to include   */
    /* as many consecutive control chars as are present   */
    /* in the file, in the count.                         */

    do
    {
      /* First, get to either a control char or EOF */

      character = fgetc(fileptr);
    }
    while (character > 31 && character != EOF);

    while (character < 32 && character != EOF)
    {
      /* If we're not on EOF, continue until we're no longer */
      /* on a control char or hit EOF.                       */

      character = fgetc(fileptr);
    }

    /* Work out how many bytes we've read */

    count = (int) (ftell(fileptr) - file_position);

    /* If count is zero, we're at the end of the file */

    if (!count) break;

    /* If we're not on EOF and don't have a control char, then */
    /* we overshot by one; so we would want to subtract one    */
    /* from count. However, to ensure string manipulation      */
    /* works OK, we'll need one char to null terminate the     */
    /* string. So in that case, we need to *add* one to count  */
    /* if the above condition isn't true.                      */

    if (!(character > 31 && character != EOF)) count ++;

    /* Right, after all that messing around rewind to the stored */
    /* file position and allocate a buffer for this string.      */

    if (fseek(fileptr, file_position, SEEK_SET))
    {
3369 3370 3371
      erb = *_kernel_last_oserror();
      e   = &erb;

3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385
      goto hotlist_load_directory_exit; /* (See near end of function) */
    }

    /* Note that string_buffer is a static, as this way only one */
    /* of these buffers ever exists, even for recursive calls.   */

    if (string_buffer) free(string_buffer);

    string_buffer = malloc(count);

    /* Complain if the allocation fails */

    if (!string_buffer)
    {
3386 3387
      erb = *_kernel_last_oserror();
      e   = &erb;
3388 3389 3390 3391 3392 3393 3394 3395 3396

      goto hotlist_load_directory_exit; /* (See near end of function) */
    }

    /* Read the data and force a terminator at the end of the buffer, */
    /* just to be safe.                                               */

    if (fread(string_buffer, sizeof(char), count - 1, fileptr) != count - 1)
    {
3397 3398 3399
      erb = *_kernel_last_oserror();
      e   = &erb;

3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433
      goto hotlist_load_directory_exit; /* (See near end of function) */
    }

    string_buffer[count - 1] = 0;

    /* Convert tags to lower case */

    hotlist_lower_tags(string_buffer);

    /* Treat any opening '<h...>' tag (header) as the title to a directory */

    str_ptr = strstr(string_buffer, "<h");

    if (
         str_ptr           &&
         str_ptr[2] >= '1' &&
         str_ptr[2] <= '6' &&
         str_ptr[3] == '>'
       )
    {
      /* Read the directory name (up to the closing '</h...>') */

      str_ptr = strtok(str_ptr + 4, "<");

      /* Allocate space for it */

      if (next_directory_name) free(next_directory_name);

      next_directory_name = malloc(strlen(str_ptr) + 1);

      /* Complain if the allocation fails */

      if (!next_directory_name)
      {
3434
        e = make_no_memory_error(3);
3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466

        goto hotlist_load_directory_exit; /* (See near end of function) */
      }

      /* Otherwise, copy the name in */

      strcpy(next_directory_name, str_ptr);
    }

    /* Treat any '<a href=...>' attribute contents (link) as a URL for the hotlist */

    else if ((str_ptr = strstr(string_buffer, "<a href=\"")) != NULL) /* Using '!= NULL' stops a compiler warning... */
    {
      /* First extract the URL */

      str_ptr += 9; /* Derived from strlen("<a href=\"") */

      str_ptr = strtok(str_ptr, "\"");

      /* Because we're about to use strtok() to extract the title, it'll */
      /* put a convenient terminator into the string_buffer block at the */
      /* end of the URL. So all we need to do is record the current      */
      /* pointer in the block, str_ptr, for use when we finally add the  */
      /* item to the hotlist.                                            */

      url = str_ptr;

      /* Extract the title - between the closing '>' of the '<a href=...' */
      /* and the opening '<' of the '</a>'.                               */

      str_ptr = strtok(NULL, "><");

3467
      /* Get ready to add this item, provided that the URL string has */
3468
      /* ':/' in - i.e. looks fully specified                        */
3469

3470
      if (strstr(url, ":/"))
3471
      {
3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483
        if (target_type == 0)
        {
          e = hotlist_new_url(target, HOTLIST_POSITION_END, str_ptr, url);
          target_type = 1;
        }
        else
        {
          e = hotlist_new_url(target, HOTLIST_POSITION_AFTER, str_ptr, url);
        }
        if (e) goto hotlist_load_directory_exit; /* (See near end of function) */

        target = hotlist_newitem;
3484
      }
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502
    }

    /* Treat any '<ul>' tags as the start of a new directory. The name comes */
    /* from a preceeding '<h...>' tag (see above).                           */

    else if ((str_ptr = strstr(string_buffer, "<ul>")) != NULL) /* New directory */
    {
      if (!next_directory_name)
      {
        /* If we don't have a directory name for this one, apparently, flag it */
        /* by incrementing the unmatched '<ul>' counter.                       */

        unfollowed_uls ++;
      }
      else
      {
        /* Otherwise, add the directory */

3503 3504 3505 3506 3507 3508 3509 3510 3511
        if (target_type == 0)
        {
          e = hotlist_new_directory(target, next_directory_name, HOTLIST_POSITION_END, &new_dir);
          target_type = 1;
        }
        else
        {
          e = hotlist_new_directory(target, next_directory_name, HOTLIST_POSITION_AFTER, &new_dir);
        }
3512 3513
        if (e) goto hotlist_load_directory_exit;

3514 3515
        target = hotlist_newitem;

3516 3517 3518 3519 3520
        if (strstr(string_buffer, "<!--open-->") != NULL)
        {
          new_dir->flags |= HOTLIST_D_IS_OPEN;
        }

3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583
        free(next_directory_name);
        next_directory_name = NULL;

        /* If the directory was added, recursively load the contents */

        if (new_dir)
        {
          /* Note this will invalidate string_buffer. The contents must */
          /* not be used after the call!                                */

          e = hotlist_load_directory(fileptr, new_dir);
          if (e) goto hotlist_load_directory_exit; /* (See near end of function) */
        }
        else
        {
          /* If the directory was not added, flag it */

          unfollowed_uls ++;
        }
      }
    }

    /* Treat any '</ul>' tags as the end of a directory. */

    else if ((str_ptr = strstr(string_buffer, "</ul>")) != NULL) /* Close directory */
    {
      /* If we have unfollowed '<ul>' tags, decrement the counter; */
      /* otherwise, exit quietly.                                  */

      if (unfollowed_uls) unfollowed_uls--;
      else goto hotlist_load_directory_exit; /* (See near end of function) */
    }
  }

  /* This section is not necessarily an error exit condition, so */
  /* the code falls through to it in normal running. If 'e' is   */
  /* NULL there's still no error returned in the end. Note       */
  /* though how the various buffers are freed, and thus          */
  /* invalidated, at this point; so during recursive routines,   */
  /* they must not be accessed after the recursive call has      */
  /* been made (unless, of course, they are reallocated).        */

hotlist_load_directory_exit:

  /* Free up the temporary buffers */

  if (string_buffer)
  {
    free(string_buffer);
    string_buffer = NULL;
  }

  if (next_directory_name)
  {
    free(next_directory_name);
    next_directory_name = NULL;
  }

  // We could at this point give a warning if unfollowed_uls is non-zero.

  return e;
}

3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600
/*************************************************/
/* hotlist_discard()                             */
/*                                               */
/* Empties the hotlist. The hotlist window is    */
/* not updated (expected usage is to close the   */
/* window shotly before or after discarding the  */
/* contents).                                    */
/*************************************************/

void hotlist_discard(void)
{
  while (hotlist_root->data.directory_content)
  {
    hotlist_delete_item(hotlist_root->data.directory_content);
  }
}

3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613
/*************************************************/
/* hotlist_load()                                */
/*                                               */
/* This function loads an HTML file previously   */
/* saved by hotlist_save as the new hotlist.     */
/*                                               */
/* Parameters: Pointer to the filename to load   */
/*             (null terminated).                */
/*************************************************/

_kernel_oserror * hotlist_load(char * filename)
{
  _kernel_oserror * e;
3614
  static char     * local_path = NULL;
3615 3616
  FILE            * fileptr;

3617 3618 3619 3620 3621 3622 3623
  if (!filename || !*filename) return NULL;

  local_path = malloc(strlen(filename) + 1);
  if (!local_path) return NULL;

  strcpy(local_path, filename);

3624 3625
  /* First delete all the existing hotlist items */

3626
  hotlist_discard();
3627 3628 3629

  /* Open the file */

3630
  fileptr = fopen(local_path, "r");
3631 3632 3633

  if (fileptr == NULL)
  {
3634 3635
    free(local_path);

3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665
    erb.errnum = Utils_Error_Custom_Normal;
    StrNCpy0(erb.errmess,
             lookup_token("HlCantLoad:Hotlist file could not be loaded",
                          0,
                          0));
    return &erb;
  }

  /* Load it (any errors are returned right at the end) */

  e = hotlist_load_directory(fileptr, hotlist_root);

  fclose(fileptr);

  /* Clear all of the various flags for redraw, */
  /* selection etc. now that we have a new      */
  /* hotlist.                                   */

  hotlist_clear_flags(hotlist_root, hl_ALL, HOTLIST_G_REDRAW_NOW);

  #ifdef TRACE

    /* Show the tree now the file is loaded */

    if (tl & (1u<<25)) hotlist_display_tree(hotlist_root, 0);

  #endif

  /* Finished; redraw issues are left to the caller */

3666 3667 3668
  if (!e) e = hotlist_modified(HL_MODIFIED_LOAD);

  free(local_path);
3669

3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682
  return e;
}

/*************************************************/
/* hotlist_initialise()                          */
/*                                               */
/* This function initialises the hotlist library */
/* routines, and must be called before any other */
/* hotlist functions.                            */
/*************************************************/

_kernel_oserror * hotlist_initialise(void)
{
3683 3684 3685 3686 3687
  ObjectId     toolbar;
  ObjectId     menu_id;
  BBox         bbox;
  unsigned int item_height, item_dir_width, item_url_width;
  char         root[] = "Root";
3688

3689 3690 3691 3692 3693
  /* Set some initial flags */

  hotlist_dragging.drag_type    = HOTLIST_NOT_DRAGGING;
  hotlist_dragging.using_adjust = 0;

3694 3695
  /* Create root directory item */

3696
  RetError(hotlist_new_directory(NULL, root, 0, &hotlist_root));
3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720

  /* Create the hotlist window */

  RetError(toolbox_create_object(0, "HotlistWind", &hotlist_windowid));

  /* Is there a toolbar? */

  RetError(window_get_tool_bars(InternalTopLeft,
                                hotlist_windowid,
                                NULL,
                                &toolbar,
                                NULL,
                                NULL));

  /* If so, read the size */

  if (toolbar != 0)
  {
    RetError(window_get_extent(0, toolbar, &bbox));

    hotlist_bbar_size = bbox.ymax - bbox.ymin;
  }
  else hotlist_bbar_size = 0;

3721 3722 3723 3724
  /* Read sprite sizes to see if the sprites actually exist */

  RetError(hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width));

3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747
  /* Register event handlers for redraw, clicks and drags in the */
  /* main hotlist window.                                        */

  RetError(event_register_wimp_handler(hotlist_windowid,
                                       Wimp_ERedrawWindow,
                                       hotlist_redraw_handler,
                                       NULL));

  RetError(event_register_wimp_handler(hotlist_windowid,
                                       Wimp_EMouseClick,
                                       hotlist_mouse_click_handler,
                                       NULL));

  RetError(event_register_wimp_handler(-1,
                                       Wimp_EUserDrag,
                                       hotlist_drag_completed_handler,
                                       NULL));

  /* Menu handlers */

  RetError(window_get_menu(0, hotlist_windowid, &menu_id));

  RetError(event_register_toolbox_handler(menu_id,
3748
                                          EHotlistToBeShown,
3749 3750 3751 3752
                                          hotlist_menuopen_handler,
                                          NULL));

  RetError(event_register_toolbox_handler(menu_id,
3753
                                          EHotlistHidden,
3754 3755 3756 3757 3758 3759
                                          hotlist_menuclose_handler,
                                          NULL));

  /* Main menu items */

  RetError(event_register_toolbox_handler(-1,
3760 3761
                                          EHotlistSelectAll,
                                          hotlist_select_all_handler,
3762 3763 3764
                                          NULL));

  RetError(event_register_toolbox_handler(-1,
3765 3766
                                          EHotlistClearSelect,
                                          hotlist_clear_selection_handler,
3767 3768 3769
                                          NULL));

  RetError(event_register_toolbox_handler(-1,
3770
                                          EHotlistOpenAll,
3771 3772 3773 3774
                                          hotlist_menu_openall_handler,
                                          NULL));

  RetError(event_register_toolbox_handler(-1,
3775
                                          EHotlistCloseAll,
3776 3777 3778 3779
                                          hotlist_menu_closeall_handler,
                                          NULL));

  RetError(event_register_toolbox_handler(-1,
3780
                                          EHotlistDelete,
3781 3782 3783
                                          hotlist_menu_delete_handler,
                                          NULL));

3784 3785 3786 3787 3788
  RetError(event_register_toolbox_handler(-1,
                                          EHotlistSaveToServer,
                                          hotlist_save_to_server_handler, /* a.k.a. Save As Default */
                                          NULL));

3789 3790 3791
  /* Submenu warning events */

  RetError(event_register_toolbox_handler(-1,
3792
                                          EHotlistShowEditURL,
3793 3794 3795 3796
                                          hotlist_show_editurl_handler,
                                          NULL));

  RetError(event_register_toolbox_handler(-1,
3797
                                          EHotlistShowRenameDirectory,
3798 3799 3800 3801
                                          hotlist_show_rendirectory_handler,
                                          NULL));

  RetError(event_register_toolbox_handler(-1,
3802
                                          EHotlistShowNewURL,
3803 3804 3805 3806
                                          hotlist_show_newurl_handler,
                                          NULL));

  RetError(event_register_toolbox_handler(-1,
3807
                                          EHotlistShowNewDirectory,
3808 3809 3810 3811 3812
                                          hotlist_show_newdirectory_handler,
                                          NULL));

  /* Hotlist related dialogue events */

3813 3814 3815 3816 3817
  RetError(event_register_toolbox_handler(-1,
                                          EHotlistClose,
                                          hotlist_close_handler,
                                          NULL));

3818
  RetError(event_register_toolbox_handler(-1,
3819
                                          EHotlistNewEditURLOK,
3820 3821 3822 3823
                                          hotlist_newedit_url_handler,
                                          NULL));

  RetError(event_register_toolbox_handler(-1,
3824 3825
                                          EHotlistNewEditURLCancel,
                                          hotlist_reset_url_handler,
3826 3827 3828
                                          NULL));

  RetError(event_register_toolbox_handler(-1,
3829 3830
                                          EHotlistNewRenameDirectoryOK,
                                          hotlist_newren_directory_handler,
3831 3832 3833
                                          NULL));

  RetError(event_register_toolbox_handler(-1,
3834
                                          EHotlistNewRenameDirectoryCancel,
3835 3836
                                          hotlist_reset_directory_handler,
                                          NULL));
David Brown's avatar
David Brown committed
3837

3838
  RetError(event_register_toolbox_handler(-1,
3839
                                          EHotlistShowDescriptions,
3840 3841 3842 3843
                                          hotlist_show_descriptions_handler,
                                          NULL));

  RetError(event_register_toolbox_handler(-1,
3844
                                          EHotlistShowURLs,
3845 3846 3847
                                          hotlist_show_urls_handler,
                                          NULL));

David Brown's avatar
David Brown committed
3848
  RetError(event_register_toolbox_handler(-1,
3849
                                          EHotlistCancelDrag,
David Brown's avatar
David Brown committed
3850 3851 3852
                                          hotlist_drag_stop_handler,
                                          NULL));

3853 3854 3855 3856 3857 3858
  return NULL;
}

/*************************************************/
/* hotlist_open()                                */
/*                                               */
3859 3860 3861 3862
/* Opens the hotlist window. If already open,    */
/* all this does is bring it to the front - it   */
/* doesn't move it. The display type is changed, */
/* if required, in any case.                     */
3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875
/*                                               */
/* Parameters: Show type (as for a call to       */
/*             Toolbox_ShowObject);              */
/*                                               */
/*             Show block (as for a call to      */
/*             Toolbox_ShowObject);              */
/*                                               */
/*             0 to open showing descriptions,   */
/*             or 1 to open showing URLs.        */
/*************************************************/

_kernel_oserror * hotlist_open(int show_type, void * type, int show_urls)
{
3876 3877
  int               open, old_show_urls;
  _kernel_oserror * e;
3878 3879 3880

  old_show_urls = hl_show_urls;

3881
  /* Sets show descriptions / show URLs */
3882

3883
  hl_show_urls = show_urls;
3884 3885 3886

  /* Set the size of the window etc. */

3887
  open = hotlist_preopen();
3888

3889 3890 3891 3892 3893 3894
  /* Show the hotlist. We have to do this twice; if we try to centre */
  /* it just the once, then that call seems to read the visible area */
  /* as it was *before* hotlist_preopen changed the extent.          */
  /*                                                                 */
  /* This Is Bad. In any case, for opening centred, we want to open  */
  /* full size, if not already open.                                 */
3895

3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911
  if (open)
  {
    /* Open window once to let the wimp/toolbox see what shape it really is,  */
    /* but don't fiddle with the visible area as the window was already open. */

    e = toolbox_show_object(0,
                            hotlist_windowid,
                            Toolbox_ShowObject_Default,
                            0,
                            0,
                            -1);
  }
  else
  {
    BBox                  extent;
    WindowShowObjectBlock show;
3912

3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935
    RetError(window_get_extent(0, hotlist_windowid, &extent));

    show.visible_area = extent;
    show.behind       = -3;

    e = toolbox_show_object(0,
                            hotlist_windowid,
                            Toolbox_ShowObject_FullSpec,
                            &show,
                            0,
                            -1);

    /* Open it once again in the place the window is really wanted */

    e = toolbox_show_object(0,
                            hotlist_windowid,
                            show_type,
                            type,
                            0,
                            -1);
  }

  /* Redraw the window if the display type has changed */
3936 3937 3938 3939 3940 3941 3942

  if (!e && open && hl_show_urls != old_show_urls)
  {
    hotlist_redraw_items(0, hotlist_count_displayed_items(hotlist_root->data.directory_content));
  }

  return e;
3943 3944 3945 3946 3947 3948 3949 3950 3951 3952
}

/*************************************************/
/* hotlist_close()                               */
/*                                               */
/* Closes the hotlist window.                    */
/*************************************************/

_kernel_oserror * hotlist_close(void)
{
3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967
  if (hotlist_windowid) return toolbox_hide_object(0, hotlist_windowid);
  else                  return NULL;
}

/*************************************************/
/* hotlist_close_handler()                       */
/*                                               */
/* Closes the hotlist window.             .      */
/*************************************************/

static int hotlist_close_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
{
  ChkError(hotlist_close());

  return 1;
3968 3969 3970
}

/*************************************************/
3971
/* hotlist_select_all_handler()                  */
3972
/*                                               */
3973 3974 3975 3976 3977
/* This function handles the 'select all' menu   */
/* item.                                         */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
3978 3979
/*************************************************/

3980
static int hotlist_select_all_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
3981
{
3982
  ObjectId main_menu;
3983

3984
  menu_select = 0;
3985

3986
  /* Select everything */
3987

3988 3989
  hotlist_set_flags(hotlist_root, hl_ALL, HOTLIST_G_IS_SELECTED);
  hotlist_root->flags &= ~HOTLIST_G_IS_SELECTED;
3990

3991
  hotlist_redraw_now();
3992

3993
  /* Get the main menu ID */
3994

3995
  ChkError(window_get_menu(0, hotlist_windowid, &main_menu));
3996

3997
  /* Update the main menu contents to reflect the new state */
3998

3999
  hotlist_set_menu_details(main_menu);
4000

4001
  return 1;
4002 4003 4004
}

/*************************************************/
4005
/* hotlist_clear_selection_handler()             */
4006 4007 4008 4009 4010
/*                                               */
/* This function handles the clear selection     */
/* menu item                                     */
/*************************************************/

4011
static int hotlist_clear_selection_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
4012
{
4013 4014 4015 4016 4017 4018 4019 4020 4021 4022
  ObjectId main_menu;

  menu_select = 0;

  /* Get the main menu ID */

  ChkError(window_get_menu(0, hotlist_windowid, &main_menu));

  /* Clear the selection */

4023
  hotlist_clear_selection();
4024 4025 4026 4027 4028

  /* Update the main menu */

  hotlist_set_menu_details(main_menu);

4029
  return 1;
4030 4031 4032 4033 4034
}

/*************************************************/
/* hotlist_menu_openall_handler()                */
/*                                               */
4035 4036 4037 4038
/* Deal with the Open All menu item.             */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
4039 4040
/*************************************************/

4041
static int hotlist_menu_openall_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
4042 4043 4044 4045
{
  if (hotlist_root->data.directory_content)
  {
    menu_itemno = 0;
4046 4047 4048 4049

    /* Set all Open flags, clear all Redraw Now flags, and force a redraw of everything */

    hotlist_set_flags  (hotlist_root->data.directory_content, hl_directory, HOTLIST_D_IS_OPEN);
4050
    hotlist_clear_flags(hotlist_root->data.directory_content, hl_directory, HOTLIST_G_REDRAW_NOW);
4051 4052 4053

    ChkError(hotlist_redraw_items(0, hotlist_count_displayed_items(hotlist_root->data.directory_content)));

4054
    hotlist_preopen();
4055
  }
4056 4057

  return 1;
4058 4059 4060 4061 4062
}

/*************************************************/
/* hotlist_menu_closeall_handler()               */
/*                                               */
4063 4064 4065 4066
/* Deal with the Close All menu item.            */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
4067 4068
/*************************************************/

4069
static int hotlist_menu_closeall_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
4070 4071
{
  unsigned int noitems;
4072 4073
  ObjectId     main_menu;

4074 4075 4076
  if (hotlist_root->data.directory_content)
  {
    menu_itemno = 0;
4077 4078 4079

    /* Clear all Open and Redraw Now flags, and deselect all items */

4080 4081 4082 4083 4084
    noitems = hotlist_count_displayed_items(hotlist_root->data.directory_content);

    hotlist_clear_flags(hotlist_root->data.directory_content,
                        hl_directory,
                        HOTLIST_D_IS_OPEN | HOTLIST_G_REDRAW_NOW);
4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096

    ChkError(hotlist_clear_selection());
    ChkError(hotlist_redraw_items(0, noitems));

    /* Get the main menu ID */

    ChkError(window_get_menu(0, hotlist_windowid, &main_menu));

    /* Update the menus to reflect there's nothing selected */

    ChkError(hotlist_set_menu_details(main_menu));

4097
    hotlist_preopen();
4098
  }
4099 4100

  return 1;
4101 4102 4103 4104 4105
}

/*************************************************/
/* hotlist_menu_delete_handler()                 */
/*                                               */
4106 4107 4108 4109
/* Deal with the Delete menu item.               */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
4110 4111
/*************************************************/

4112
static int hotlist_menu_delete_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
4113
{
4114 4115 4116
  hotlist_item * item;
  unsigned int   noitems;
  ObjectId       hotlist_window;
4117
  int            deleted = 0;
4118 4119 4120 4121 4122

  if (!id_block->ancestor_id) hotlist_window = id_block->self_id;
  else                        hotlist_window = id_block->ancestor_id;

  /* If there are no selected items, can't do anything */
4123 4124 4125

  noitems = hotlist_count_displayed_items(hotlist_root->data.directory_content);

David Brown's avatar
David Brown committed
4126
  if (!hotlist_count_selected_items()) return 0;
4127

4128 4129 4130
  /* Delete all selected items */

  while ((item = hotlist_find_selected_item()) != NULL)
4131
  {
4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145
    if (item->flags & HOTLIST_G_IS_READ_ONLY)
    {
      /* Just clear selection on read only items */

      item->flags &= ~HOTLIST_G_IS_SELECTED;
    }
    else
    {
      /* Delete read/write items */

      deleted ++;

      hotlist_delete_item(item);
    }
4146
  }
4147 4148 4149

  /* Redraw */

4150
  hotlist_redraw_items(0, noitems);
4151

4152 4153
  /* We may have no more work to do if all of the selected items */
  /* were read-only; otherwise, 'deleted' will be non-zero.      */
4154

4155 4156 4157
  if (deleted)
  {
    /* Update the window extent */
4158

4159
    hotlist_preopen();
4160

4161 4162 4163 4164
    /* The hotlist has been modified... */

    hotlist_modified(HL_MODIFIED_DELETE);
  }
4165

4166 4167 4168
  /* Finished */

  return 1;
4169 4170
}

4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195
/*************************************************/
/* hotlist_save_to_server_handler()              */
/*                                               */
/* Deal with the 'Save to server' menu item      */
/* (also known as 'Save as default').            */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

static int hotlist_save_to_server_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
{
  #ifdef SINGLE_USER

    show_error_ret(hotlist_save(lookup_choice("HotlistSave:Browse:User.Hotlist",0,0)));

  #else

    show_error_ret(multiuser_save_hotlist());

  #endif

  return 1;
}

4196
/*************************************************/
4197 4198 4199 4200
/* hotlist_show_newurl_handler()                 */
/*                                               */
/* This function fills in the edit / create URL  */
/* dialogue to be a New URL object.              */
4201
/*                                               */
4202 4203
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
4204 4205
/*************************************************/

4206
static int hotlist_show_newurl_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
4207 4208 4209
{
  ObjectId dboxid;

4210
  #ifdef TRACE
4211
    if (tl & (1u<<25)) Printf("hotlist_show_newurl_handler: Called\n");
4212
  #endif
4213

4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229
  ChkError(menu_get_sub_menu_show(0, id_block->self_id, id_block->self_component, &dboxid));

  alter_new = HOTLIST_MENUSECTION_NEW;

  /* Set the title, clear the description writable and URL writable, */
  /* and set the default action button text. 'Cancel' stays as       */
  /* 'Cancel'.                                                       */

  ChkError(window_set_title(0, dboxid, lookup_token("HotlistCreateURLTitle:Create new URL",0,0)));

  ChkError(writablefield_set_value(0, dboxid, HOTLIST_NEWURL_NAME, ""));
  ChkError(writablefield_set_value(0, dboxid, HOTLIST_NEWURL_URL,  ""));

  ChkError(actionbutton_set_text  (0, dboxid, HOTLIST_NEWURL_NEW,  lookup_token("HotlistCreateURLAction:Create",0,0)));

  return 1;
4230 4231 4232
}

/*************************************************/
4233
/* hotlist_show_editurl_handler()                */
4234
/*                                               */
4235 4236 4237 4238 4239
/* This function fills in the edit / create URL  */
/* dialogue to be an Edit URL object.            */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
4240 4241
/*************************************************/

4242
static int hotlist_show_editurl_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
4243
{
4244 4245
  hotlist_item * item;
  ObjectId       dboxid;
4246

4247
  #ifdef TRACE
4248
    if (tl & (1u<<25)) Printf("hotlist_show_editurl_handler: Called\n");
4249
  #endif
4250

4251 4252
  /* Must have a selected item... */

4253 4254 4255
  item = hotlist_find_selected_item();
  if (!item) return 0;

4256
  ChkError(menu_get_sub_menu_show(0, id_block->self_id, id_block->self_component, &dboxid));
4257 4258

  alter_new = HOTLIST_MENUSECTION_ALTER;
4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269

  /* Set the title, description writable, URL writable, and  */
  /* default action button text. 'Cancel' stays as 'Cancel'. */

  ChkError(window_set_title(0, dboxid, lookup_token("HotlistEditURLTitle:Edit URL",0,0)));

  ChkError(writablefield_set_value(0, dboxid, HOTLIST_NEWURL_NAME, item->name));
  ChkError(writablefield_set_value(0, dboxid, HOTLIST_NEWURL_URL,  item->data.url));

  ChkError(actionbutton_set_text  (0, dboxid, HOTLIST_NEWURL_NEW,  lookup_token("HotlistEditURLAction:Alter",0,0)));

4270 4271 4272 4273 4274
  /* If the item's flags show it is read-only, it can't be altered */

  if (item->flags & HOTLIST_G_IS_READ_ONLY) set_gadget_state(dboxid, HOTLIST_NEWURL_NEW, 1);
  else                                      set_gadget_state(dboxid, HOTLIST_NEWURL_NEW, 0);

4275
  return 1;
4276 4277 4278
}

/*************************************************/
4279 4280 4281 4282 4283
/* hotlist_show_newdirectory_handler()           */
/*                                               */
/* This function fills in the edit / create      */
/* directory dialogue to be a New Directory      */
/* object.                                       */
4284
/*                                               */
4285 4286
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
4287 4288
/*************************************************/

4289
static int hotlist_show_newdirectory_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
4290 4291
{
  ObjectId dboxid;
4292 4293

  #ifdef TRACE
4294
    if (tl & (1u<<25)) Printf("hotlist_show_newdirectory_handler: Called\n");
4295
  #endif
4296

4297
  ChkError(menu_get_sub_menu_show(0, id_block->self_id, id_block->self_component, &dboxid));
4298 4299

  alter_new = HOTLIST_MENUSECTION_NEW;
4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310

  /* Set the title, clear the writable contents, and set */
  /* the default action button text. 'Cancel' stays as   */
  /* 'Cancel'.                                           */

  ChkError(window_set_title(0, dboxid, lookup_token("HotlistCreateDirTitle:Create new directory",0,0)));

  ChkError(writablefield_set_value(0, dboxid, HOTLIST_NEWDIRECTORY_NAME, ""));
  ChkError(actionbutton_set_text  (0, dboxid, HOTLIST_NEWDIRECTORY_NEW,  lookup_token("HotlistCreateDirAction:Create",0,0)));

  return 1;
4311 4312 4313
}

/*************************************************/
4314 4315 4316 4317 4318
/* hotlist_show_rendirectory_handler()           */
/*                                               */
/* This function fills in the edit / create      */
/* directory dialogue to be a Rename Directory   */
/* object.                                       */
4319
/*                                               */
4320 4321
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
4322 4323
/*************************************************/

4324
static int hotlist_show_rendirectory_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
4325
{
4326 4327
  hotlist_item * item;
  ObjectId       dboxid;
4328 4329

  #ifdef TRACE
4330
    if (tl & (1u<<25)) Printf("hotlist_show_rendirectory_handler: Called\n");
4331
  #endif
4332

4333
  /* Must have a selected item... */
4334

4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349
  item = hotlist_find_selected_item();
  if (!item) return 0;

  ChkError(menu_get_sub_menu_show(0, id_block->self_id, id_block->self_component, &dboxid));

  alter_new = HOTLIST_MENUSECTION_ALTER;

  /* Set the title, writable contents, and default action */
  /* button text. 'Cancel' stays as 'Cancel'.             */

  ChkError(window_set_title(0, dboxid, lookup_token("HotlistRenameDirTitle:Rename directory",0,0)));

  ChkError(writablefield_set_value(0, dboxid, HOTLIST_NEWDIRECTORY_NAME, item->name));
  ChkError(actionbutton_set_text  (0, dboxid, HOTLIST_NEWDIRECTORY_NEW,  lookup_token("HotlistRenameDirAction:Rename",0,0)));

4350 4351 4352 4353 4354
  /* If the item's flags show it is read-only, it can't be renamed */

  if (item->flags & HOTLIST_G_IS_READ_ONLY) set_gadget_state(dboxid, HOTLIST_NEWDIRECTORY_NEW, 1);
  else                                      set_gadget_state(dboxid, HOTLIST_NEWDIRECTORY_NEW, 0);

4355
  return 1;
4356 4357 4358 4359 4360
}

/*************************************************/
/* hotlist_newedit_url_handler()                 */
/*                                               */
4361 4362 4363 4364 4365 4366 4367
/* This function either alters the name and URL  */
/* fields of a selected URL, or creates a new    */
/* URL item in the directory that the pointer    */
/* was over when the main menu was opened.       */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
4368 4369
/*************************************************/

4370
static int hotlist_newedit_url_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
4371
{
4372 4373 4374 4375 4376 4377 4378 4379
  _kernel_oserror * e = NULL;
  hotlist_item    * item;
  hotlist_item    * tempitem;
  char            * tempdesc;
  char            * tempurl;
  int               size;

  /* Is there a selected item? */
4380 4381

  item = hotlist_find_selected_item();
4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421

  /* Get the length of the description (name) text */

  ChkError(writablefield_get_value(0,
                                   id_block->self_id,
                                   HOTLIST_NEWURL_NAME,
                                   NULL,
                                   0,
                                   &size));

  /* Try to allocate space for it */

  tempdesc = malloc(size + 1); /* '+1' to be safe (above call *should* return 'size of buffer required'...) */

  if (!tempdesc)
  {
    show_error_cont(make_no_memory_error(15));
    return 1;
  }

  /* Get the value, making absolutely sure it's terminated */

  ChkError(writablefield_get_value(0,
                                   id_block->self_id,
                                   HOTLIST_NEWURL_NAME,
                                   tempdesc,
                                   size,
                                   NULL));
  tempdesc[size] = 0;

  /* Do the same for the URL */

  ChkError(writablefield_get_value(0,
                                   id_block->self_id,
                                   HOTLIST_NEWURL_URL,
                                   NULL,
                                   0,
                                   &size));
  tempurl = malloc(size + 1);

4422 4423 4424
  if (!tempurl)
  {
    free(tempdesc);
4425 4426 4427

    show_error_cont(make_no_memory_error(15));
    return 1;
4428
  }
4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441

  ChkError(writablefield_get_value(0,
                                   id_block->self_id,
                                   HOTLIST_NEWURL_URL,
                                   tempurl,
                                   size,
                                   NULL));
  tempurl[size] = 0;

  /* 'alter_new' is set by the menu handling functions to */
  /* reflect whether we should create or alter items.     */

  switch (alter_new)
4442 4443
  {
    case HOTLIST_MENUSECTION_NEW:
4444
    {
4445 4446
      /* Create a new item. First, find the one the menu was */
      /* opened over.                                        */
4447

4448
      tempitem = hotlist_find_item(hotlist_root->data.directory_content, menu_itemno);
4449

4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461
      /* If we can't, add to the start of the root directory */

      if (!tempitem)
      {
        menu_itemno = 0;

        e = hotlist_new_url(hotlist_root,
                            HOTLIST_POSITION_BEGINNING,
                            tempdesc,
                            tempurl);
      }

4462
      /* Otherwise add it after the item, unless the parent is read-only */
4463 4464 4465

      else
      {
4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482
        if (tempitem->parent && (tempitem->parent->flags & HOTLIST_G_IS_READ_ONLY))
        {
          erb.errnum = Utils_Error_Custom_Message;

          StrNCpy0(erb.errmess,
                   lookup_token("HLNewRO:You can't create an item here (the directory is read-only).",
                                0,0));

          e = &erb;
        }
        else
        {
          e = hotlist_new_url(tempitem,
                              HOTLIST_POSITION_AFTER,
                              tempdesc,
                              tempurl);
        }
4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508
      }

      if (!e)
      {
        /* Ensure the window extent is up to date */

        hotlist_preopen();

        /* Deal with redraw issues */

        e = hotlist_redraw_items(menu_itemno,
                                 hotlist_count_displayed_items(hotlist_root->data.directory_content));

        hotlist_clear_flags(hotlist_root, hl_ALL, HOTLIST_G_REDRAW_NOW);

        /* Call the 'hotlist has changed' function */

        hotlist_modified(HL_MODIFIED_ADD);
      }

      /* Free temporary data and report any errors */

      free(tempdesc);
      free(tempurl);

      ChkError(e);
4509
    }
4510 4511 4512
    break;

    case HOTLIST_MENUSECTION_ALTER:
4513
    {
4514 4515
      /* Alter an existing item. If it is read-only, complain; */
      /* otherwise, free its URL and description text.         */
4516

4517 4518 4519
      if (item->flags & HOTLIST_G_IS_READ_ONLY)
      {
        erb.errnum = Utils_Error_Custom_Message;
4520

4521 4522 4523
        StrNCpy0(erb.errmess,
                 lookup_token("NLAlterRO:You can't alter this item (it is read-only).",
                              0,0));
4524

4525
        e = &erb;
4526

4527 4528 4529 4530 4531 4532 4533
        free(tempdesc);
        free(tempurl);
      }
      else
      {
        free(item->name);
        free(item->data.url);
4534

4535
        /* Point to the new URL and description */
4536

4537 4538
        item->name     = tempdesc;
        item->data.url = tempurl;
4539

4540 4541 4542 4543 4544
        /* Deal with redraw issues */

        item->flags |= HOTLIST_G_REDRAW_NOW;

        hotlist_preopen();
4545

4546
        e = hotlist_redraw_now();
4547

4548 4549 4550 4551
        /* Call the 'hotlist has changed' function */

        hotlist_modified(HL_MODIFIED_ALTER);
      }
4552 4553

      /* Report any errors */
4554

4555 4556 4557
      ChkError(e);
    }
    break;
4558
  }
4559 4560

  return 1;
4561 4562 4563 4564 4565
}

/*************************************************/
/* hotlist_newren_directory_handler()            */
/*                                               */
4566 4567 4568 4569 4570
/* As hotlist_newedit_url_handler, but for       */
/* directory items.                              */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
4571 4572
/*************************************************/

4573
static int hotlist_newren_directory_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
4574
{
4575 4576 4577 4578 4579 4580 4581
  _kernel_oserror * e = NULL;
  hotlist_item    * item;
  hotlist_item    * tempitem;
  char            * tempname;
  int               size;

  /* This bit is similar to the start of hotlist_newedit_url_handler */
4582 4583

  item = hotlist_find_selected_item();
4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594

  ChkError(writablefield_get_value(0,
                                   id_block->self_id,
                                   HOTLIST_NEWDIRECTORY_NAME,
                                   NULL,
                                   0,
                                   &size));

  tempname = malloc(size + 1);

  if (!tempname)
4595
  {
4596 4597 4598
    show_error_cont(make_no_memory_error(16));
    return 1;
  }
4599

4600 4601 4602 4603 4604 4605 4606 4607 4608 4609
  ChkError(writablefield_get_value(0,
                                   id_block->self_id,
                                   HOTLIST_NEWDIRECTORY_NAME,
                                   tempname,
                                   size,
                                   NULL));

  switch (alter_new)
  {
    case HOTLIST_MENUSECTION_NEW:
4610
    {
4611
      tempitem = hotlist_find_item(hotlist_root->data.directory_content, menu_itemno);
4612

4613 4614
      /* If we can't find the item the pointer was near, at at the */
      /* start of the root; else add near the item.                */
4615

4616 4617 4618 4619 4620 4621
      if (!tempitem)
      {
        menu_itemno = 0;

        e = hotlist_new_directory(hotlist_root,
                                  tempname,
4622
                                  HOTLIST_POSITION_END,
4623 4624 4625 4626
                                  &tempitem);
      }
      else
      {
4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645
        /* The parent may be read-only */

        if (tempitem->parent && (tempitem->parent->flags & HOTLIST_G_IS_READ_ONLY))
        {
          erb.errnum = Utils_Error_Custom_Message;

          StrNCpy0(erb.errmess,
                   lookup_token("HLNewRO:You can't create an item here (the directory is read-only).",
                                0,0));

          e = &erb;
        }
        else
        {
          e = hotlist_new_directory(tempitem,
                                    tempname,
                                    HOTLIST_POSITION_AFTER,
                                    &tempitem);
        }
4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663
      }

      if (!e)
      {
        hotlist_preopen();

        e = hotlist_redraw_items(menu_itemno,
                                 hotlist_count_displayed_items(hotlist_root->data.directory_content));

        hotlist_clear_flags(hotlist_root, hl_ALL, HOTLIST_G_REDRAW_NOW);
        hotlist_modified(HL_MODIFIED_ADD);
      }

      /* Free the temporary block and report any errors */

      free(tempname);

      ChkError(e);
4664
    }
4665 4666 4667
    break;

    case HOTLIST_MENUSECTION_ALTER:
4668
    {
4669
      /* Alter an existing directory. If it is read-only, complain. */
4670

4671 4672 4673
      if (item->flags & HOTLIST_G_IS_READ_ONLY)
      {
        erb.errnum = Utils_Error_Custom_Message;
4674

4675 4676 4677
        StrNCpy0(erb.errmess,
                 lookup_token("NLAlterRO:You can't alter this item (it is read-only).",
                              0,0));
4678

4679
        e = &erb;
4680

4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694
        free(tempname);
      }
      else
      {
        /* Free the item's existing name, link to the new */
        /* name, and do the relevant redraws / hotlist    */
        /* modified calls.                                */

        free(item->name);

        item->name = tempname;
        item->flags |= HOTLIST_G_REDRAW_NOW;

        hotlist_preopen();
4695

4696 4697 4698 4699
        e = hotlist_redraw_now();

        hotlist_modified(HL_MODIFIED_ALTER);
      }
4700 4701 4702 4703 4704 4705

      /* Report any errors */

      ChkError(e);
    }
    break;
4706
  }
4707 4708

  return 1;
4709 4710 4711
}

/*************************************************/
4712
/* hotlist_reset_url_handler()                   */
4713 4714
/*                                               */
/* This function is called when Cancel is        */
4715 4716 4717 4718 4719 4720
/* clicked on in the New (aka Edit) URL dialogue */
/* box. It resets the contents to their previous */
/* state.                                        */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
4721 4722
/*************************************************/

4723
static int hotlist_reset_url_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
4724
{
4725 4726 4727
  /* Action depends on whether we were altering, or creating an item */

  switch (alter_new)
4728 4729
  {
    case HOTLIST_MENUSECTION_NEW:
4730 4731 4732 4733
    {
      ChkError(writablefield_set_value(0, id_block->self_id, HOTLIST_NEWURL_NAME, ""));
      ChkError(writablefield_set_value(0, id_block->self_id, HOTLIST_NEWURL_URL,  ""));
    }
4734 4735 4736
    break;

    case HOTLIST_MENUSECTION_ALTER:
4737 4738
    {
      hotlist_item * item = hotlist_find_selected_item();
4739

4740 4741 4742 4743 4744 4745 4746
      if (item)
      {
        ChkError(writablefield_set_value(0, id_block->self_id, HOTLIST_NEWURL_NAME, item->name));
        ChkError(writablefield_set_value(0, id_block->self_id, HOTLIST_NEWURL_URL,  item->data.url));
      }
    }
    break;
4747
  }
4748 4749

  return 1;
4750 4751 4752 4753 4754
}

/*************************************************/
/* hotlist_reset_directory_handler()             */
/*                                               */
4755 4756 4757 4758 4759 4760
/* Similar to hotlist_reset_url_handler, but for */
/* resetting the New (aka Rename) Directory      */
/* dialogue.                                     */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
4761 4762
/*************************************************/

4763
static int hotlist_reset_directory_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
4764
{
4765
  switch (alter_new)
4766 4767
  {
    case HOTLIST_MENUSECTION_NEW:
4768 4769 4770 4771 4772 4773
    {
      ChkError(writablefield_set_value(0,
                                       id_block->self_id,
                                       HOTLIST_NEWDIRECTORY_NAME,
                                       ""));
    }
4774 4775 4776
    break;

    case HOTLIST_MENUSECTION_ALTER:
4777 4778
    {
      hotlist_item * item = hotlist_find_selected_item();
4779

4780 4781 4782 4783 4784 4785
      if (item) ChkError(writablefield_set_value(0,
                                                 id_block->self_id,
                                                 HOTLIST_NEWDIRECTORY_NAME,
                                                 item->name));
    }
    break;
4786
  }
4787 4788

  return 1;
4789 4790 4791 4792 4793
}

/*************************************************/
/* hotlist_drag_renderer()                       */
/*                                               */
4794 4795 4796 4797 4798 4799 4800 4801 4802 4803
/* Renders a single hotlist item at 0,0 for use  */
/* with DragAnObject.                            */
/*                                               */
/* Parameters: Pointer to the item to render;    */
/*                                               */
/*             Height of a hotlist item in OS    */
/*             units;                            */
/*                                               */
/*             Width of a hotlist directory      */
/*             sprite in OS units;               */
4804
/*                                               */
4805 4806
/*             Width of a hotlist URL sprite in  */
/*             OS units.                         */
4807 4808
/*************************************************/

4809 4810
static void hotlist_drag_renderer(hotlist_item * item, unsigned int item_height,
                                  unsigned int item_dir_width, unsigned int item_url_width)
4811
{
4812 4813
  /* DON'T put Printf's in here... Doesn't work, at least, */
  /* not with PipeFS output methods.                       */
4814

4815
  WimpIconBlock hotlist_iconblock;
4816 4817
  int           temp_width;
  int           text_width;
4818

4819
  /* Create an icon block for the sprite component */
4820

4821 4822
  hotlist_iconblock.flags               = HOTLIST_SPRITE_ICON_FLAGS;
  hotlist_iconblock.data.is.sprite_area = (void *) sprite_block;
4823

4824 4825 4826
  /* Set appropriate sprite and width of sprite */

  switch(item->type)
4827 4828
  {
    case hl_url:
4829
    {
4830 4831
      hotlist_iconblock.data.is.sprite             = !(item->flags & HOTLIST_G_IS_READ_ONLY) ? URL_SPRITE : RESOURCES_URL_SPRITE;
      hotlist_iconblock.data.is.sprite_name_length = strlen(hotlist_iconblock.data.is.sprite);
4832 4833 4834

      temp_width = item_url_width;
    }
4835 4836 4837
    break;

    case hl_directory:
4838
    {
4839 4840
      hotlist_iconblock.data.is.sprite             = !(item->flags & HOTLIST_G_IS_READ_ONLY) ? CLOSED_DIRECTORY_SPRITE : CLOSED_RESOURCES_DIRECTORY_SPRITE;
      hotlist_iconblock.data.is.sprite_name_length = strlen(hotlist_iconblock.data.is.sprite);
4841 4842 4843

      temp_width = item_dir_width;
    }
4844 4845 4846
    break;

    default:
4847 4848 4849
    {
      temp_width = 0;
    }
4850 4851 4852
    break;
  }

4853 4854
  /* Set the bounding box */

4855 4856 4857 4858 4859
  hotlist_iconblock.bbox.xmin = 2;
  hotlist_iconblock.bbox.xmax = 2 + temp_width;
  hotlist_iconblock.bbox.ymax = 2 + item_height;
  hotlist_iconblock.bbox.ymin = 2;

4860 4861 4862 4863 4864
  /* Plot the item */

  if (wimp_plot_icon(&hotlist_iconblock)) return; /* Bail out on error! */

  /* Create an icon block for the text component */
4865

4866 4867
  if (item->type == hl_url && hl_show_urls) utils_text_width(item->data.url, &text_width, 0);
  else                                      utils_text_width(item->name,     &text_width, 0);
4868

4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879
  hotlist_iconblock.flags = HOTLIST_TEXT_ICON_FLAGS_DRAG;

  /* Set the bounding box */

  hotlist_iconblock.bbox.xmin = temp_width + 4;
  hotlist_iconblock.bbox.xmax = temp_width + text_width + 16;
  hotlist_iconblock.bbox.ymax = item_height;
  hotlist_iconblock.bbox.ymin = 4;

  /* Set the text according to the item type */

4880 4881 4882 4883
  if (item->type == hl_url && hl_show_urls)
  {
    hotlist_iconblock.data.it.buffer      = item->data.url;
    hotlist_iconblock.data.it.buffer_size = strlen(item->data.url);
4884
    hotlist_iconblock.data.it.validation  = NULL;
4885 4886 4887 4888 4889
  }
  else
  {
    hotlist_iconblock.data.it.buffer      = item->name;
    hotlist_iconblock.data.it.buffer_size = strlen(item->name);
4890
    hotlist_iconblock.data.it.validation  = NULL;
4891
  }
4892

4893 4894 4895 4896 4897 4898 4899
  /* Plot the icon */

  wimp_plot_icon(&hotlist_iconblock);

  /* Finished */

  return;
4900 4901 4902 4903 4904
}

/*************************************************/
/* hotlist_start_drag()                          */
/*                                               */
4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934
/* Start a new drag operation inside the hotlist */
/* window.                                       */
/*************************************************/

static _kernel_oserror * hotlist_start_drag(void)
{
  /* Do the hard work elsewhere... */

  RetError(hotlist_start_drag_backend());

  /* Reset autoscroll */

  RetError(hotlist_autoscroll(0));

  /* Register NULL handler */

  register_null_claimant(Wimp_ENull, hotlist_null_handler, NULL);

  /* Finished */

  return NULL;
}

/*************************************************/
/* hotlist_start_drag_backend()                  */
/*                                               */
/* This function is called by hotlist_start_drag */
/* to do most of the work for starting a drag    */
/* operation from within the hotlist window. A   */
/* drag box is created, bounding all selected    */
4935 4936 4937
/* items.                                        */
/*************************************************/

4938
static _kernel_oserror * hotlist_start_drag_backend(void)
4939
{
4940 4941 4942 4943 4944
  WimpGetWindowStateBlock   state;
  WimpGetPointerInfoBlock   pointerblock;
  WimpDragBox               box;
  hotlist_item            * item;
  unsigned int              item_height, item_dir_width, item_url_width;
4945
  BBox                      bbox;
4946 4947
  int                       xorigin,     yorigin;
  int                       screenwidth, screenheight;
4948

4949
  /* Read the pointer position and get the hotlist item sizes */
4950

4951 4952
  RetError(wimp_get_pointer_info(&pointerblock));
  RetError(hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width));
4953

4954 4955 4956 4957
  /* Record whether ot not Adjust (and only Adjust) was used */

  hotlist_dragging.using_adjust = (pointerblock.button_state == Wimp_MouseButtonAdjust);

4958
  /* Get the shape of the selected items */
4959

4960
  hotlist_get_selected_shape(&bbox);
4961

4962 4963 4964 4965 4966
  /* Now get information on the hotlist window itself, */
  /* and the current screen shape.                     */

  RetError(window_get_wimp_handle(0, hotlist_windowid, &state.window_handle));
  RetError(wimp_get_window_state(&state));
4967 4968 4969 4970

  xorigin = state.xscroll - state.visible_area.xmin;
  yorigin = state.yscroll - state.visible_area.ymax;

4971 4972 4973 4974
  /* Screen size, pixels */

  screenwidth  = bbc_modevar(-1, BBC_XWindLimit);
  screenheight = bbc_modevar(-1, BBC_YWindLimit);
4975

4976 4977 4978 4979 4980 4981 4982 4983
  /* Convert to OS units */

  screenwidth  = (screenwidth  + 1) << (bbc_modevar(-1, BBC_XEigFactor));
  screenheight = (screenheight + 1) << (bbc_modevar(-1, BBC_YEigFactor));

  /* Read CMOS - solid drags, or a dashed outline? */

  if (_kernel_osbyte(161, 28, 0) & (1 << (8 + 1)))
4984
  {
4985 4986 4987 4988 4989
    _kernel_swi_regs regs;
    int              redraw_params[4];

    /* Solid drags */

David Brown's avatar
David Brown committed
4990 4991
    item = hotlist_find_selected_item();

4992 4993
    /* A single item, or several? */

David Brown's avatar
David Brown committed
4994 4995 4996 4997 4998 4999 5000
    if (
         (hotlist_count_selected_items() == 1 && item->type == hl_url) ||
         (
           hotlist_count_selected_items() == 1 &&
           item->type == hl_directory &&
           (
             !(item->flags & HOTLIST_D_IS_OPEN) ||
5001
             hotlist_no_contents_selected(item->data.directory_content) ||
David Brown's avatar
David Brown committed
5002 5003 5004 5005
             item->data.directory_content == NULL
           )
         )
       )
5006
    {
5007 5008
      /* A single item. Set up the box shape */

5009 5010 5011 5012
      box.dragging_box.xmin = bbox.xmin - xorigin;
      box.dragging_box.ymin = bbox.ymin - yorigin;
      box.dragging_box.xmax = bbox.xmax - xorigin;
      box.dragging_box.ymax = bbox.ymax - yorigin;
5013 5014

      /* Store details of the redraw */
5015

5016 5017 5018 5019 5020 5021
      redraw_params[0] = (int) hotlist_find_selected_item();
      redraw_params[1] = (int) item_height;
      redraw_params[2] = (int) item_dir_width;
      redraw_params[3] = (int) item_url_width;

      /* Fill in the register block */
5022 5023

      regs.r[0] = (2<<0) | (2<<2) | (1<<6) | (1<<7) | (1<<16);
5024 5025 5026
      regs.r[1] = (int) hotlist_drag_renderer;
      regs.r[2] = (int) redraw_params;
      regs.r[3] = (int) &(box.dragging_box);
5027

5028 5029 5030 5031 5032 5033 5034 5035
      /* Start the drag - use DragAnObject, as then we can show */
      /* the item's text, as seen in the hotlist window, during */
      /* the drag.                                              */

      RetError(_kernel_swi(DragAnObject_Start, &regs, &regs));

      /* Finished; flag that we're solid-dragging an object and exit */

5036 5037
      hotlist_dragging.drag_type = HOTLIST_SOLID_DRAG_OBJECT;

5038
      return NULL;
5039 5040 5041
    }
    else
    {
5042
      int width, height;
5043

5044
      /* Several items. */
5045

5046
      RetError(read_sprite_size(SELECTION_SPRITE, &width, &height));
5047

5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068
      /* Set the box shape, based around the mouse pointer coordinates  */

      box.dragging_box.xmin = pointerblock.x - (width  / 2 + 10); /* '+ 10' = aesthetics */
      box.dragging_box.ymin = pointerblock.y - (height / 2 + 10);
      box.dragging_box.xmax = pointerblock.x + (width  / 2 + 10);
      box.dragging_box.ymax = pointerblock.y + (height / 2 + 10);

      /* Fill in the register block */

      regs.r[0] = (1<<0) | (1<<2) | (1<<6) | (1<<7);
      regs.r[1] = (int) sprite_block;
      regs.r[2] = (int) "package";
      regs.r[3] = (int) &(box.dragging_box);

      /* Start the drag - use DragASprite as there's no text to */
      /* show with the 'package' icon.                          */

      RetError(_kernel_swi(DragASprite_Start, &regs, &regs));

      /* Finished; flag that we're solid-dragging an object and exit */

5069 5070
      hotlist_dragging.drag_type = HOTLIST_SOLID_DRAG_SPRITE;

5071 5072
      return NULL;
    }
5073 5074
  }

5075 5076 5077 5078 5079 5080
  /* CMOS said - use a dashed outline drag. */

  box.drag_type         = Wimp_DragBox_DragFixedDash;

  /* Set the box shape */

5081 5082 5083 5084
  box.dragging_box.xmin = bbox.xmin - xorigin;
  box.dragging_box.xmax = bbox.xmax - xorigin;
  box.dragging_box.ymin = bbox.ymin - yorigin;
  box.dragging_box.ymax = bbox.ymax - yorigin;
5085

5086
  /* Constrain the drag to the screen size */
5087

5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098
  box.parent_box.xmin   = box.dragging_box.xmin - pointerblock.x;
  box.parent_box.xmax   = box.dragging_box.xmax - pointerblock.x + screenwidth;
  box.parent_box.ymin   = box.dragging_box.ymin - pointerblock.y;
  box.parent_box.ymax   = box.dragging_box.ymax - pointerblock.y + screenheight;

  /* Start the drag */

  RetError(wimp_drag_box(&box));

  /* Finished; flag that we're dragging an object with a dashed outline and exit */

5099 5100
  hotlist_dragging.drag_type = HOTLIST_BOX_DRAG;

5101 5102
  return NULL;
}
5103

David Brown's avatar
David Brown committed
5104 5105 5106
/*************************************************/
/* hotlist_drag_stop_handler()                   */
/*                                               */
5107
/* Terminates any hotlist drag in progress.      */
David Brown's avatar
David Brown committed
5108 5109
/*************************************************/

5110
static int hotlist_drag_stop_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
David Brown's avatar
David Brown committed
5111
{
5112 5113
  _kernel_oserror * e;
  int               dragging = hotlist_dragging.drag_type;
5114 5115 5116 5117 5118 5119 5120 5121

  #ifdef TRACE
    if (tl & (1u<<25)) Printf("hotlist_drag_stop_handler: Called\n");
  #endif

  /* If we're not dragging in the hotlist, something weird is */
  /* going on! So definitely, ignore the event...             */

5122
  if (hotlist_dragging.drag_type == HOTLIST_NOT_DRAGGING) return 0;
5123 5124 5125 5126 5127

  /* Make sure we clear the dragging flag as soon as possible. */
  /* If it gets left switched on, then drags in other parts of */
  /* the browser could get misinterpreted by the hotlist code. */

5128 5129
  dragging                   = hotlist_dragging.drag_type;
  hotlist_dragging.drag_type = HOTLIST_NOT_DRAGGING;
5130

David Brown's avatar
David Brown committed
5131 5132 5133
  if (hotlist_current_highlighted)
  {
    hotlist_current_highlighted->flags &= ~HOTLIST_D_IS_HIGHLIGHTED;
5134 5135 5136

    ChkError(hotlist_redraw_items(highlighted_itemno, highlighted_itemno));

David Brown's avatar
David Brown committed
5137 5138 5139
    hotlist_current_highlighted = NULL;
  }

5140 5141 5142
  /* Deal with each dragging type */

  switch (dragging)
David Brown's avatar
David Brown committed
5143
  {
5144 5145
    /* Complain if we don't understand it */

David Brown's avatar
David Brown committed
5146
    default:
5147
    {
5148
      hotlist_dragging.drag_type = HOTLIST_NOT_DRAGGING;
5149 5150 5151 5152 5153

      #ifdef TRACE

        erb.errnum = Utils_Error_Custom_Normal;
        sprintf(erb.errmess,
5154 5155
                "Value %d of hotlist_dragging.drag_type not understood in hotlist_drag_stop_handler",
                hotlist_dragging.drag_type);
5156 5157 5158 5159 5160 5161 5162

        show_error_ret(&erb);

      #endif

      return 0;
    }
David Brown's avatar
David Brown committed
5163 5164 5165
    break;

    case HOTLIST_SOLID_DRAG_OBJECT:
5166 5167 5168
    {
      /* Stop drag an object */

5169
      e = _swix(DragAnObject_Stop, 0);
5170 5171

      deregister_null_claimant(Wimp_ENull, hotlist_null_handler, NULL);
5172 5173

      ChkError(e);
5174
    }
David Brown's avatar
David Brown committed
5175 5176 5177
    break;

    case HOTLIST_SOLID_DRAG_SPRITE:
5178 5179 5180
    {
      /* Stop drag a sprite */

5181 5182
      e = _swix(DragASprite_Stop, 0);

5183
      deregister_null_claimant(Wimp_ENull, hotlist_null_handler, NULL);
5184 5185

      ChkError(e);
5186
    }
David Brown's avatar
David Brown committed
5187 5188 5189
    break;

    case HOTLIST_BOX_DRAG_SELECTION:
5190 5191 5192
    {
      /* Stop drag box */

5193
      e = wimp_drag_box(NULL);
5194 5195 5196 5197 5198 5199

      deregister_null_claimant(Wimp_ENull, hotlist_null_drag_select_handler, NULL);

      /* Deselect anything that is currently selected */

      hotlist_clear_flags(hotlist_root->data.directory_content, hl_ALL, HOTLIST_G_DRAG_SELECTED);
5200 5201 5202
      ChkError(hotlist_redraw_now());

      ChkError(e);
5203
    }
David Brown's avatar
David Brown committed
5204
    break;
5205 5206 5207

    case HOTLIST_BOX_DRAG:
    {
5208 5209
      e = wimp_drag_box(NULL);

5210
      deregister_null_claimant(Wimp_ENull, hotlist_null_handler, NULL);
5211 5212

      ChkError(e);
5213 5214
    }
    break;
David Brown's avatar
David Brown committed
5215
  }
5216

David Brown's avatar
David Brown committed
5217 5218 5219
  return 1;
}

5220 5221 5222 5223
/*************************************************/
/* hotlist_drag_completed_handler()              */
/*                                               */
/* This function is called when a user_drag      */
5224 5225 5226 5227 5228 5229
/* completes. If the drag is one started by the  */
/* hotlist section it is processed. Dropping the */
/* drag in the hotlist window will move or copy  */
/* the items being dragged; dropping in any      */
/* other window will try to save the relevant    */
/* datatype (URI or HTML) to that window.        */
5230 5231
/*************************************************/

5232
static int hotlist_drag_completed_handler(int event_code, WimpPollBlock * event, IdBlock * id_block, void * handle)
5233
{
5234 5235
  WimpGetWindowStateBlock   state;
  WimpGetPointerInfoBlock   pointerblock;
5236 5237 5238
  hotlist_item            * targetitem;
  hotlist_item            * sourceitem;
  int                       window_handle;
5239
  unsigned int              item_height, item_dir_width, item_url_width;
5240
  unsigned int              top,         bottom;
5241
  int                       xmin,        xmax;
5242
  int                       winx,        winy;
5243
  int                       position;
5244
  int                       dragging;
5245
  int                       shift = 0;
5246 5247 5248 5249

  #ifdef TRACE
    if (tl & (1u<<25)) Printf("hotlist_drag_completed_handler: Called\n");
  #endif
5250

5251 5252 5253
  /* If we're not dragging in the hotlist, the Wimp returned */
  /* this because someone else completed a drag (e.g. a save */
  /* dialogue). In that case, ignore the event.              */
5254

5255
  if (hotlist_dragging.drag_type == HOTLIST_NOT_DRAGGING) return 0;
5256

5257 5258 5259
  /* Make sure we clear the dragging flag as soon as possible. */
  /* If it gets left switched on, then drags in other parts of */
  /* the browser could get misinterpreted by the hotlist code. */
5260

5261 5262
  dragging                   = hotlist_dragging.drag_type;
  hotlist_dragging.drag_type = HOTLIST_NOT_DRAGGING;
5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280

  /* Check if SHIFT is pressed */

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

        121,
        128,

        &shift);

  /* Reset autoscrolling (To restore pointer if necessary) */

  show_error_ret(hotlist_autoscroll(0));

  /* Deal with each dragging type */

  switch (dragging)
5281
  {
5282 5283 5284 5285 5286 5287 5288 5289
    /* Complain if we don't understand it */

    default:
    {
      #ifdef TRACE

        erb.errnum = Utils_Error_Custom_Normal;
        sprintf(erb.errmess,
5290 5291
                "Value %d of hotlist_dragging.drag_type not understood in hotlist_drag_completed_handler",
                hotlist_dragging.drag_type);
5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302

        show_error_ret(&erb);

      #endif

      return 0;
    }
    break;

    /* For e.g. dragging a single URL or directory item */

5303
    case HOTLIST_SOLID_DRAG_OBJECT:
5304 5305 5306
    {
      _swix(DragAnObject_Stop, 0);
    }
5307 5308
    break;

5309 5310
    /* For e.g. dragging a selection under the 'package' icon */

5311
    case HOTLIST_SOLID_DRAG_SPRITE:
5312 5313 5314
    {
      _swix(DragASprite_Stop, 0);
    }
5315 5316
    break;

5317 5318 5319 5320 5321 5322 5323 5324
    /* For e.g. dragging a single URL or selection without solid drags emables */

    case HOTLIST_BOX_DRAG:
    {
      /* No special action required in this case */
    }
    break;

5325
    /* When dragging out a box to dynamically select hotlist items */
5326

5327 5328 5329 5330
    case HOTLIST_BOX_DRAG_SELECTION:
    {
      deregister_null_claimant(Wimp_ENull, hotlist_null_drag_select_handler, NULL);
      hotlist_convert_drag_selection(hotlist_root->data.directory_content);
5331

5332 5333
      return 0;
    }
5334 5335 5336
    break;
  }

5337 5338 5339
  /* If there is a currently highlighted directory (i.e. something */
  /* was to be dropped into it) unhighlight that item.             */

5340 5341 5342 5343
  if (hotlist_current_highlighted)
  {
    hotlist_current_highlighted->flags &= ~HOTLIST_D_IS_HIGHLIGHTED;
    hotlist_redraw_items(highlighted_itemno, highlighted_itemno);
5344

5345 5346 5347
    hotlist_current_highlighted = NULL;
  }

5348 5349
  /* Remove the drag null handler */

5350 5351
  deregister_null_claimant(Wimp_ENull, hotlist_null_handler, NULL);

5352 5353 5354 5355 5356 5357
  /* Remember the first selected item in the hotlist */

  sourceitem = hotlist_find_selected_item();

  /* Get information on the pointer */

5358 5359
  wimp_get_pointer_info(&pointerblock);

5360 5361 5362 5363 5364
  /* Was the drag ended over the hotlist window? */

  ChkError(window_get_wimp_handle(0,
                                  hotlist_windowid,
                                  &window_handle));
5365

5366
  if (window_handle == pointerblock.window_handle)
5367
  {
5368 5369 5370 5371 5372 5373 5374 5375
    /* Yes, the drag ended over the hotlist window. */

    if (pointerblock.icon_handle != -1) return 0; /* (Only understand drops on workspace) */

    /* Find item sizes, the window x and y coordinate of the  */
    /* drop point, and from this, the targetted hotlist item. */

    ChkError(hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width));
5376 5377

    state.window_handle = pointerblock.window_handle;
5378 5379 5380 5381 5382 5383 5384
    ChkError(wimp_get_window_state(&state));

    winx       = pointerblock.x + (state.xscroll - state.visible_area.xmin);
    winy       = pointerblock.y + (state.yscroll - state.visible_area.ymax);

    top        = (-winy) / item_height;
    targetitem = hotlist_find_item(hotlist_root->data.directory_content, top);
5385 5386

    /* Decide where to put the items */
5387

5388 5389
    if (targetitem)
    {
5390
      /* If we've dropped on a selected or read-only item, do nothing */
5391

5392
      if (targetitem->flags & (HOTLIST_G_IS_SELECTED | HOTLIST_G_IS_READ_ONLY)) return 0;
5393 5394 5395

      /* Otherwise, find the x coordinate span of the target item... */

5396
      hotlist_get_shape(&xmin, &xmax, targetitem);
5397 5398

      if (targetitem->type == hl_directory && winx >= xmin && winx <= xmin + item_dir_width)
5399
      {
5400 5401 5402 5403 5404 5405 5406
        /* ...and if the drop point was within the x span of the */
        /* directory icon part of a directory, place in that     */
        /* directory (directories will only show the '+' icon,   */
        /* i.e. highlighted, when the pointer hovers over the    */
        /* sprite component).                                    */

        position = HOTLIST_POSITION_BEGINNING;
5407 5408 5409 5410
      }
      else
      {
        if ((-winy % item_height) > item_height / 2)
5411
        {
5412
          /* If we're over half way past the item, place after it, unless */
5413 5414 5415
          /* it is an open directory. Again, the null handler dealing     */
          /* with highlighting directory sprites reflects this situation  */
          /* correctly.                                                   */
5416

5417 5418
          if (
               targetitem->type == hl_directory      &&
5419
               targetitem->flags & HOTLIST_D_IS_OPEN
5420
             )
5421 5422 5423 5424 5425 5426 5427
          {
            position = HOTLIST_POSITION_BEGINNING;
          }
          else
          {
            position = HOTLIST_POSITION_AFTER;
          }
5428
        }
5429
        else
5430 5431 5432 5433 5434
        {
          /* Otherwise, place before */

          position = HOTLIST_POSITION_BEFORE;
        }
5435 5436 5437 5438
      }
    }
    else
    {
5439 5440
      /* If there is no target item, add to the end of the hotlist root */

5441
      targetitem = hotlist_root;
5442
      position   = HOTLIST_POSITION_END;
5443 5444
    }

5445 5446
    /* Find the selected item. If its number is less than the one */
    /* we estimated from the drop y coordinate, use that instead. */
5447

5448 5449
    {
      int selected;
5450

5451 5452 5453
      selected = hotlist_find_no_from_item(hotlist_find_selected_item());
      if (selected < top) top = selected;
    }
5454

5455 5456 5457
    /* Count how many items are displayed to get the last hotlist item */

    bottom = hotlist_count_displayed_items(hotlist_root->data.directory_content);
5458 5459 5460

    if (!shift)
    {
5461
      /* If shift isn't pressed, move selected items */
David Brown's avatar
David Brown committed
5462

5463 5464 5465
      do
      {
        /* Ensure the source item is deselected */
David Brown's avatar
David Brown committed
5466

5467
        sourceitem->flags &= ~HOTLIST_G_IS_SELECTED;
David Brown's avatar
David Brown committed
5468

5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482
        /* Move it */

        ChkError(hotlist_move_item(sourceitem,
                                   targetitem,
                                   position));

        /* If this is a directory, deselect its contents */

        if (sourceitem->type == hl_directory)
        {
          hotlist_clear_flags(sourceitem->data.directory_content,
                              hl_ALL,
                              HOTLIST_G_IS_SELECTED);
        }
5483 5484

        targetitem = sourceitem;
5485 5486 5487 5488 5489 5490 5491
        sourceitem = hotlist_find_selected_item();

        /* Loop round moving all following items for as long as */
        /* anything is still selected - making sure we copy     */
        /* them all after what we just added.                   */

        position = HOTLIST_POSITION_AFTER;
5492
      }
5493 5494 5495
      while (sourceitem);

      ChkError(hotlist_modified(HL_MODIFIED_MOVE));
5496 5497 5498
    }
    else
    {
5499 5500
      /* Shift is pressed, so copy the items. The code is very */
      /* similar in structure to the move stuff above.         */
David Brown's avatar
David Brown committed
5501

5502
      do
5503 5504
      {
        sourceitem->flags &= ~HOTLIST_G_IS_SELECTED;
5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519

        ChkError(hotlist_copy_item(sourceitem,
                                   targetitem,
                                   position,
                                   &targetitem));

        if (sourceitem->type == hl_directory)
        {
          hotlist_clear_flags(sourceitem->data.directory_content,
                              hl_ALL,
                              HOTLIST_G_IS_SELECTED);
        }

        sourceitem = hotlist_find_selected_item();
        position   = HOTLIST_POSITION_AFTER;
5520
      }
5521 5522 5523
      while (sourceitem);

      ChkError(hotlist_modified(HL_MODIFIED_COPY));
5524 5525
    }

5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536
    /* The displayed item count may have changed, so take the greatest */
    /* value of this as the bottom item to redraw between.             */

    {
      int new_bottom = hotlist_count_displayed_items(hotlist_root->data.directory_content);

      if (new_bottom > bottom) bottom = new_bottom;
    }

    /* Ensure the window extent etc. is up to date */

5537
    hotlist_preopen();
5538 5539 5540 5541

    /* Redraw the relevant regions of the window */

    ChkError(hotlist_redraw_items(top, bottom));
5542 5543 5544
  }
  else
  {
5545 5546 5547 5548 5549 5550 5551
    /* No - the drag did not end over the hotlist window */

    if (
         sourceitem                          &&
         hotlist_count_selected_items() == 1 &&
         sourceitem->type == hl_url
       )
5552
    {
5553 5554 5555
      /* There is a single selected item which is a URL. So, save */
      /* it as a URI file.                                        */

5556
      hotlist_initiate_uri_save(sourceitem);
5557 5558 5559
    }
    else
    {
5560 5561 5562 5563
      /* Can't save URI file when saving more than one URL, */
      /* so save an HTML file instead.                      */

      hotlist_initiate_html_save(lookup_token("HotlistLeafname:Hotlist",0,0));
5564
    }
5565 5566
  }

5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578
  /* If we were using Adjust, close the window */

  if (hotlist_dragging.using_adjust)
  {
    hotlist_dragging.using_adjust = 0;

    ChkError(toolbox_hide_object(0, hotlist_windowid));
  }

  /* Finished */

  return 1;
5579 5580
}

5581 5582 5583
/*************************************************/
/* hotlist_autoscroll()                          */
/*                                               */
5584
/* Auto-scrolls a window.                        */
5585
/*                                               */
5586 5587
/* Parameters: Object ID of the window to scroll */
/*             or 0 to reset autoscrolling.      */
5588 5589
/*************************************************/

5590
static _kernel_oserror * hotlist_autoscroll(int window)
5591
{
5592 5593 5594 5595 5596 5597 5598 5599
  WimpGetWindowStateBlock state;
  ObjectId                over_window;
  ObjectId                parent;
  ComponentId             component;
  BBox                    extent;
  int                     scroll_changed;
  int                     x, y, position;
  int                     autoscroll_newtime;
5600

5601 5602
  static unsigned int     scrolling, autoscroll_oldtime;
  static unsigned int     mouse_shape = Mouse_Shape_Normal;
5603

5604
  /* Reset autoscroll handling if requested */
5605

5606
  if (!window)
5607
  {
5608
    #ifdef TRACE
5609
      if (tl & (1<<25)) Printf("hotlist_autoscroll: Resetting\n");
5610
    #endif
5611

5612
    scrolling = 0;
5613

5614 5615 5616
    mouse_set_pointer_shape(Mouse_Shape_Normal);
    mouse_shape = Mouse_Shape_Normal;

5617 5618 5619 5620
    return _swix(OS_ReadMonotonicTime,
                 _OUT(0),

                 &autoscroll_oldtime);
5621 5622
  }

5623
  /* What window is the pointer over? */
5624

5625
  RetError(window_get_pointer_info(0, &x, &y, NULL, &over_window, NULL));
5626

5627 5628
  /* Get information on the window given to the function */

5629 5630 5631
  RetError(window_get_wimp_handle(0, window, &state.window_handle));
  RetError(wimp_get_window_state(&state));
  RetError(window_get_extent(0, window, &extent));
5632

5633
  /* Find location of pointer relative to the given window */
5634

5635 5636 5637 5638 5639 5640
  if (
       x < state.visible_area.xmin ||
       x > state.visible_area.xmax ||
       y < state.visible_area.ymin ||
       y > state.visible_area.ymax
     )
5641
  {
5642 5643 5644
    /* Outside the visible area */

    position = 0;
5645
  }
5646 5647 5648 5649 5650 5651
  else if (
            x > state.visible_area.xmin + choices.auto_scroll_margin &&
            x < state.visible_area.xmax - choices.auto_scroll_margin &&
            y > state.visible_area.ymin + choices.auto_scroll_margin &&
            y < state.visible_area.ymax - choices.auto_scroll_margin
          )
5652
  {
5653 5654 5655
    /* Pointer is inside the non-scrolling region of the given window */

    position = 1;
5656 5657 5658
  }
  else
  {
5659 5660 5661
    /* Pointer is inside the scrolling region of the given window */

    position = 2;
5662 5663
  }

5664 5665 5666
  /* Work out what scroll settings to give, flagging if there is */
  /* a change in 'scroll_changed'.                               */

5667
  scroll_changed = 0;
5668

5669
  if (position == 0 || position == 2)
5670
  {
5671 5672
    /* Check position relative to the top/bottom of the window */

5673
    if (y > state.visible_area.ymax - choices.auto_scroll_margin)
5674
    {
5675 5676
      /* At or over the top of the window */

5677
      if (state.yscroll < extent.ymax)
5678
      {
5679
        scroll_changed = 1;
5680
        state.yscroll += (y - (state.visible_area.ymax - choices.auto_scroll_margin));
5681 5682 5683 5684
      }
    }
    else
    {
5685
      if (y < state.visible_area.ymin + choices.auto_scroll_margin)
5686
      {
5687
        /* At or under the bottom of the window */
5688 5689

        if (state.yscroll > extent.ymin + (state.visible_area.ymax - state.visible_area.ymin))
5690
        {
5691
          scroll_changed = 1;
5692
          state.yscroll -= ((state.visible_area.ymin + choices.auto_scroll_margin) - y);
5693 5694
        }
      }
5695 5696
    }

5697 5698
    /* Check the position relative to the left/right of the window, too */

5699
    if (x > state.visible_area.xmax - choices.auto_scroll_margin)
5700
    {
5701 5702
      /* At or to the right of the right hand edge of the window */

5703
      if (state.xscroll < extent.xmax - (state.visible_area.xmax - state.visible_area.xmin))
5704
      {
5705
        scroll_changed = 1;
5706
        state.xscroll += (x - (state.visible_area.xmax - choices.auto_scroll_margin));
5707 5708 5709 5710
      }
    }
    else
    {
5711
      if (x < state.visible_area.xmin + choices.auto_scroll_margin)
5712
      {
5713 5714
        /* At or to the left of the left hand edge of the window */

5715
        if (state.xscroll > extent.xmin)
5716
        {
5717
          scroll_changed = 1;
5718
          state.xscroll -= ((state.visible_area.xmin + choices.auto_scroll_margin) - x);
5719
        }
5720
      }
5721
    }
5722 5723
  }

5724 5725 5726
  /* Deal with flags and the mouse pointer */

  switch (position)
5727
  {
5728 5729 5730
    /* If we're outside the visible area of the window, and not scrolling, */
    /* set the pointer back to a normal shape (else leave it alone).       */

5731
    case 0:
5732
    {
5733
      if (!scrolling)
5734
      {
5735 5736 5737 5738 5739 5740 5741 5742 5743 5744
        RetError(_swix(OS_ReadMonotonicTime,
                       _OUT(0),

                       &autoscroll_oldtime));

        if (mouse_shape != Mouse_Shape_Normal)
        {
          mouse_set_pointer_shape(Mouse_Shape_Normal);
          mouse_shape = Mouse_Shape_Normal;
        }
5745
      }
5746
    }
5747 5748
    break;

5749 5750 5751
    /* If we're in the non-scrolling region, unset the 'scrolling' flag */
    /* and set the mouse pointer back to a normal shape.                */

5752
    case 1:
5753
    {
5754
      scrolling = 0;
5755 5756 5757 5758 5759 5760

      RetError(_swix(OS_ReadMonotonicTime,
                     _OUT(0),

                     &autoscroll_oldtime));

5761
      if (mouse_shape != Mouse_Shape_Normal)
5762
      {
5763 5764
        mouse_set_pointer_shape(Mouse_Shape_Normal);
        mouse_shape = Mouse_Shape_Normal;
5765
      }
5766
    }
5767
    break;
5768

5769 5770 5771
    /* If inside the scrolling region, wait a while on the 'ToScroll' */
    /* pointer, then start scrolling.                                 */

5772 5773
    case 2:
    {
5774
      if (!scrolling && scroll_changed)
5775
      {
5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790
        RetError(_swix(OS_ReadMonotonicTime,
                       _OUT(0),

                       &autoscroll_newtime));

        /* If we exceed the autoscroll delay, set the scrolling flag */

        if (autoscroll_newtime - autoscroll_oldtime > choices.auto_scroll_delay)
        {
          scrolling = 1;
        }

        /* Otherwise, set the 'ToScroll' pointer */

        else
5791
        {
5792 5793 5794 5795 5796
          if (mouse_shape != Mouse_Shape_ToScroll)
          {
            mouse_shape = Mouse_Shape_ToScroll;
            mouse_set_pointer_shape(Mouse_Shape_ToScroll);
          }
5797 5798
        }
      }
5799 5800 5801 5802 5803 5804 5805 5806 5807 5808

      /* If the scroll position hasn't changed, update oldtime */

      if (!scroll_changed)
      {
        RetError(_swix(OS_ReadMonotonicTime,
                       _OUT(0),

                       &autoscroll_oldtime));
      }
5809 5810 5811
    }
    break;
  }
5812

5813 5814
  /* Closing actions to take if scrolling is in progress. */

5815 5816 5817 5818
  if (scrolling)
  {
    if (scroll_changed)
    {
5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834
      /* If the scroll position has changed, update the window */

      RetError(toolbox_get_parent(0,
                                  window,
                                  &parent,
                                  &component));

      RetError(toolbox_show_object(0,
                                   window,
                                   Toolbox_ShowObject_FullSpec,
                                   &state.visible_area,
                                   parent,
                                   component));

      /* Ensure the pointer shape is correct */

5835
      if (mouse_shape != Mouse_Shape_Scrolling)
5836
      {
5837 5838
        mouse_shape = Mouse_Shape_Scrolling;
        mouse_set_pointer_shape(Mouse_Shape_Scrolling);
5839
      }
5840 5841 5842
    }
    else
    {
5843 5844 5845 5846
      /* If the scroll position has not changed, set the pointer */
      /* shape to normal, and unset the scrolling flag - we've   */
      /* scrolled to the limit of the window extent.             */

5847
      if (mouse_shape != Mouse_Shape_Normal)
5848
      {
5849 5850
        mouse_shape = Mouse_Shape_Normal;
        mouse_set_pointer_shape(Mouse_Shape_Normal);
5851

5852
        scrolling = 0;
5853 5854
      }
    }
5855
  }
5856

5857 5858
  /* Finished */

5859
  return NULL;
5860 5861 5862 5863 5864
}

/*************************************************/
/* hotlist_null_handler()                        */
/*                                               */
5865 5866 5867 5868 5869
/* Called every null event while a drag is in    */
/* operation.                                    */
/*                                               */
/* Parameters are as standard for a Wimp event   */
/* handler.                                      */
5870 5871
/*************************************************/

5872
static int hotlist_null_handler(int event_code, WimpPollBlock * event, IdBlock * id_block, void * handle)
5873
{
5874 5875 5876 5877 5878 5879 5880 5881 5882 5883
  WimpGetWindowStateBlock   state;
  hotlist_item            * item;
  ObjectId                  window;
  unsigned int              item_height, item_dir_width, item_url_width;
  unsigned int              itemno;
  int                       x, y, buttons;
  int                       xmin, xmax;
  int                       remove_highlight = 0;

  /* Get information on the pointer position */
5884

5885
  ChkError(window_get_pointer_info(0, &x, &y, &buttons, &window, NULL));
5886

5887 5888 5889
  /* Deal with autoscroll */

  ChkError(hotlist_autoscroll(hotlist_windowid));
5890

5891 5892
  if (window == hotlist_windowid)
  {
5893 5894 5895
    /* The pointer is over the hotlist window */

    ChkError(window_get_wimp_handle(0, window, &state.window_handle));
5896 5897

    /* Display arrow pointing into directory to say that dropping */
5898 5899 5900 5901 5902 5903 5904 5905
    /* here will put in directory rather than next to it. First,  */
    /* get the window state.                                      */

    ChkError(wimp_get_window_state(&state));

    /* Work out the item that the pointer is over */

    ChkError(hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width));
5906 5907 5908 5909 5910

    x += (state.xscroll - state.visible_area.xmin);
    y += (state.yscroll - state.visible_area.ymax);

    itemno = -y / item_height;
5911 5912 5913 5914 5915
    item   = hotlist_find_item(hotlist_root->data.directory_content, itemno);

    /* Is this a directory item which is already not selected? (The */
    /* selected check is so that we don't end up highlighting the   */
    /* item we're dragging if this item is a directory).            */
5916 5917 5918

    if (item && item->type == hl_directory && !(item->flags & HOTLIST_G_IS_SELECTED))
    {
5919 5920 5921
      ChkError(hotlist_get_shape(&xmin, &xmax, item));

      /* If we're over the directory sprite, or half way under the item height when */
5922
      /* the directory is open.                                                     */
5923 5924 5925 5926 5927 5928 5929 5930 5931

      if (
           (
             x >= xmin &&
             x <= xmin + item_dir_width
           )
           ||
           (
             ((-y % item_height) > item_height / 2) &&
5932
             item->flags & HOTLIST_D_IS_OPEN
5933 5934
           )
         )
5935
      {
5936 5937
        /* ...and the currently highlighted item isn't the one we're over, and */
        /* it isn't read only...                                               */
5938

5939 5940 5941 5942
        if (
             hotlist_current_highlighted != item     &&
             !(item->flags & HOTLIST_G_IS_READ_ONLY)
           )
5943
        {
5944 5945
          /* ...then first, clear any highlighted item */

5946 5947 5948
          if (hotlist_current_highlighted)
          {
            hotlist_current_highlighted->flags &= ~HOTLIST_D_IS_HIGHLIGHTED;
5949 5950

            ChkError(hotlist_redraw_items(highlighted_itemno, highlighted_itemno));
5951
          }
5952 5953 5954

          /* Now highlight the item we're over */

5955
          item->flags |= HOTLIST_D_IS_HIGHLIGHTED;
5956

5957
          hotlist_current_highlighted = item;
5958 5959 5960 5961 5962 5963 5964 5965 5966 5967
          highlighted_itemno          = itemno;

          ChkError(hotlist_redraw_items(highlighted_itemno, highlighted_itemno));

          /* Reset the auto-open timer */

          ChkError(_swix(OS_ReadMonotonicTime,
                         _OUT(0),

                         &autoopen_oldtime));
5968
        }
5969
        else if (hotlist_current_highlighted == item)
5970
        {
5971 5972 5973 5974 5975 5976 5977 5978
          int new_time;

          /* If we've stayed over the same highlighted item, */
          /* keep a count of for how long, and if required,  */
          /* automatically open the directory.               */

          ChkError(_swix(OS_ReadMonotonicTime,
                         _OUT(0),
5979

5980
                         &new_time));
5981 5982

          if (
5983 5984 5985
               choices.auto_open_delay                               &&
               !(item->flags & HOTLIST_D_IS_OPEN)                    &&
               new_time - autoopen_oldtime > choices.auto_open_delay
5986
             )
5987
          {
5988 5989
            /* Auto-open the directory */

5990
            item->flags |= HOTLIST_D_IS_OPEN;
5991

5992
            hotlist_preopen();
5993 5994 5995

            ChkError(hotlist_redraw_items(highlighted_itemno,
                                          hotlist_count_displayed_items(hotlist_root->data.directory_content)));
5996
          }
5997 5998 5999 6000
        }
      }
      else
      {
6001 6002 6003 6004
        /* If we're not over a directory sprite, clear any existing */
        /* highlight.                                               */

        remove_highlight = 1;
6005 6006
      }
    }
6007 6008 6009 6010 6011

    /* If we're not over a directory at all, never mind it's directory */
    /* sprite component, again, clear any existing highlight.          */

    else remove_highlight = 1;
6012
  }
6013 6014 6015

  /* The following executes if the pointer is not over the hotlist window */

6016 6017
  else
  {
6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032
    /* Again, clear any highlighted directory sprite. */

    remove_highlight = 1;
  }

  /* If we're not over a directory sprite, clear any existing */
  /* highlight.                                               */

  if (remove_highlight && hotlist_current_highlighted)
  {
    hotlist_current_highlighted->flags &= ~HOTLIST_D_IS_HIGHLIGHTED;

    ChkError(hotlist_redraw_items(highlighted_itemno, highlighted_itemno));

    hotlist_current_highlighted = NULL;
6033
  }
6034 6035 6036

  /* Finished */

6037 6038 6039
  return 0;
}

6040 6041 6042 6043
//
// Simple non-scrolling selection box, to be improved later...
// Workarea relative corner of selection box may not be needed.
//
6044
static int selection_x, selection_y;
6045 6046 6047 6048 6049 6050 6051

/*************************************************/
/* hotlist_start_drag()                          */
/*                                               */
/* This function is called to start a selection  */
/* box operation in the hotlist window.          */
/*************************************************/
6052

6053
static _kernel_oserror * hotlist_selection_box_start(void)
6054
{
6055 6056 6057
  WimpGetPointerInfoBlock pointerblock;
  WimpGetWindowStateBlock state;
  WimpDragBox             box;
6058

6059
  /* Get the pointer position and info on the hotlist window */
6060

6061 6062 6063 6064 6065 6066
  RetError(wimp_get_pointer_info(&pointerblock));

  RetError(window_get_wimp_handle(0, hotlist_windowid, &state.window_handle));
  RetError(wimp_get_window_state(&state));

  /* Remember the pointer position in window coordinates */
6067 6068 6069 6070

  selection_x = pointerblock.x + (state.xscroll - state.visible_area.xmin);
  selection_y = pointerblock.y + (state.yscroll - state.visible_area.ymax);

6071 6072
  /* Set up the drag box structure */

6073
  box.drag_type         = Wimp_DragBox_DragRubberDash;
6074 6075

  box.dragging_box.xmin = pointerblock.x;
6076 6077 6078
  box.dragging_box.xmax = pointerblock.x;
  box.dragging_box.ymin = pointerblock.y;
  box.dragging_box.ymax = pointerblock.y;
6079 6080

  box.parent_box.xmin   = state.visible_area.xmin;
6081 6082 6083
  box.parent_box.xmax   = state.visible_area.xmax;
  box.parent_box.ymin   = state.visible_area.ymin;
  box.parent_box.ymax   = state.visible_area.ymax;
6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094

  /* Start the drag */

  RetError(wimp_drag_box(&box));

  /* Set global variable saying we are currently dragging */
  /* so we know whether to process a user_drag_box event  */

  hotlist_dragging.drag_type = HOTLIST_BOX_DRAG_SELECTION;

  /* Install the null handler */
6095 6096

  register_null_claimant(Wimp_ENull, hotlist_null_drag_select_handler, NULL);
6097

6098 6099 6100
  return NULL;
}

David Brown's avatar
David Brown committed
6101 6102 6103
/*************************************************/
/* hotlist_null_drag_select_handler()            */
/*                                               */
6104 6105 6106 6107 6108 6109 6110 6111
/* This function is called as a null handler;    */
/* it is responsible for selecting and           */
/* deselecting items within and outside the      */
/* rubber drag box started by                    */
/* hotlist_selection_box_start.                  */
/*                                               */
/* Parameters are as standard for a Wimp event   */
/* handler.                                      */
David Brown's avatar
David Brown committed
6112 6113
/*************************************************/

6114
static int hotlist_null_drag_select_handler(int event_code, WimpPollBlock * event, IdBlock * id_block, void * handle)
6115 6116 6117
{
  WimpGetPointerInfoBlock pointerblock;
  WimpGetWindowStateBlock state;
6118 6119 6120 6121
  unsigned int            item_height, item_dir_width, item_url_width;
  unsigned int            item_min,    item_max;
  int                     workx,       worky;
  int                     minx,        maxx;
6122

6123
  /* Find the pointer position and get information on the hotlist window */
6124

6125
  ChkError(wimp_get_pointer_info(&pointerblock));
6126

6127 6128 6129 6130 6131 6132
  ChkError(window_get_wimp_handle(0, hotlist_windowid, &state.window_handle));
  ChkError(wimp_get_window_state(&state));

  ChkError(hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width));

  /* Get the mouse position in window coordinates */
6133 6134 6135 6136

  workx = pointerblock.x + (state.xscroll - state.visible_area.xmin);
  worky = pointerblock.y + (state.yscroll - state.visible_area.ymax);

6137 6138
  /* Work out the items spanned by the drag box */

6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154
  item_min = -worky / item_height;
  if (-selection_y / item_height < item_min) item_min = -selection_y / item_height;
  item_max = (-worky / item_height);
  if ((-selection_y / item_height) > item_max) item_max = (-selection_y / item_height);

  if (selection_x < workx)
  {
    minx = selection_x;
    maxx = workx;
  }
  else
  {
    minx = workx;
    maxx = selection_x;
  }

6155 6156 6157
  /* Select them */

  ChkError(hotlist_select_box(item_min, item_max, minx, maxx));
6158 6159 6160

  return 0;
}
6161

6162 6163 6164 6165 6166 6167 6168
/*************************************************/
/* hotlist_select_box()                          */
/*                                               */
/* Sets the HOTLIST_G_DRAG_SELECTED flag for all */
/* visible items in the range first_item to      */
/* last_item where their horizontal visible area */
/* intersects with the a line draw from minx to  */
6169 6170 6171 6172 6173 6174
/* maxx. Clears the HOTLIST_G_DRAG_SELECTED      */
/* flag for all other items.                     */
/*                                               */
/* Parameters: Number of the first visible item; */
/*                                               */
/*             Number of the last visible item;  */
6175
/*                                               */
6176 6177 6178
/*             minx (window coords);             */
/*                                               */
/*             maxx (window coords).             */
6179 6180
/*************************************************/

6181
_kernel_oserror * hotlist_select_box(unsigned int first_item, unsigned int last_item, int minx, int maxx)
6182
{
6183 6184 6185 6186 6187 6188 6189 6190
  unsigned int itemno = 0;

  return hotlist_select_box_r(first_item,
                              last_item,
                              hotlist_root->data.directory_content,
                              &itemno,
                              minx,
                              maxx);
6191 6192 6193 6194 6195
}

/*************************************************/
/* hotlist_select_box_r()                        */
/*                                               */
6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211
/* Recursive backend to hotlist_select_box.      */
/*                                               */
/* Parameters: Number of the first visible item; */
/*                                               */
/*             Number of the last visible item;  */
/*                                               */
/*             First item in the directory to    */
/*             scan;                             */
/*                                               */
/*             Pointer to an int, in which the   */
/*             current item number is kept (for  */
/*             internal use only);               */
/*                                               */
/*             minx (window coords);             */
/*                                               */
/*             maxx (window coords).             */
6212 6213
/*************************************************/

6214 6215
_kernel_oserror * hotlist_select_box_r(unsigned int first_item, unsigned int last_item, hotlist_item * item,
                                       unsigned int * itemno, int minx, int maxx)
6216
{
6217 6218
  _kernel_oserror * e;
  int               itemxmin, itemxmax;
6219

6220 6221
  while (item)
  {
6222
    if (*itemno >= first_item && *itemno <= last_item)
6223
    {
6224 6225 6226 6227 6228 6229 6230
      /* As long as we're within the item number specified, deal with items */

      RetError(hotlist_get_shape(&itemxmin, &itemxmax, item));

      /* Select items within the horizontal range */

      if (!(maxx < itemxmin || minx > itemxmax))
6231
      {
6232
        if (!(item->flags & HOTLIST_G_DRAG_SELECTED))
6233
        {
6234
          item->flags |= HOTLIST_G_DRAG_SELECTED;
6235 6236 6237 6238 6239 6240 6241

          e = hotlist_redraw_items(*itemno, *itemno);

          /* If directory is closed, select everything in it (if it is open, */
          /* we should only select items the drag box covers).               */

          if (item->type == hl_directory && !(item->flags & HOTLIST_D_IS_OPEN))
6242
          {
6243
            hotlist_set_flags  (item->data.directory_content, hl_ALL, HOTLIST_G_DRAG_SELECTED);
6244
            hotlist_clear_flags(item->data.directory_content, hl_ALL, HOTLIST_G_REDRAW_NOW);
6245
          }
6246 6247

          if (e) return e;
6248 6249
        }
      }
6250 6251 6252

      /* Deselect items outside of the horizontal range */

6253 6254
      else
      {
6255
        if (item->flags & HOTLIST_G_DRAG_SELECTED)
6256
        {
6257
          item->flags &= ~HOTLIST_G_DRAG_SELECTED;
6258 6259 6260 6261 6262 6263

          e = hotlist_redraw_items(*itemno, *itemno);

          /* If directory is closed unselect everything in it */

          if (item->type == hl_directory && !(item->flags & HOTLIST_D_IS_OPEN))
David Brown's avatar
David Brown committed
6264
          {
6265 6266 6267
            hotlist_clear_flags(item->data.directory_content,
                                hl_ALL,
                                HOTLIST_G_DRAG_SELECTED | HOTLIST_G_REDRAW_NOW);
David Brown's avatar
David Brown committed
6268
          }
6269 6270

          if (e) return e;
6271 6272 6273
        }
      }
    }
6274 6275
    else
    {
6276 6277
      /* If we're not within the item numbers specified, then deselect things */

6278 6279 6280
      if (item->flags & HOTLIST_G_DRAG_SELECTED)
      {
        item->flags &= ~HOTLIST_G_DRAG_SELECTED;
6281 6282 6283 6284 6285 6286

        e = hotlist_redraw_items(*itemno, *itemno);

        /* If directory is closed unselect everything in it */

        if (item->type == hl_directory && !(item->flags & HOTLIST_D_IS_OPEN))
6287
        {
6288 6289 6290
          hotlist_clear_flags(item->data.directory_content,
                              hl_ALL,
                              HOTLIST_G_DRAG_SELECTED | HOTLIST_G_REDRAW_NOW);
6291
        }
6292 6293

        if (e) return e;
6294 6295
      }
    }
6296

6297 6298
    /* Increment the item number character */

6299 6300
    *itemno += 1;

6301 6302
    /* Recurse for any open directory items */

6303 6304
    if (item->type == hl_directory && item->flags & HOTLIST_D_IS_OPEN)
    {
6305 6306 6307 6308 6309 6310
      RetError(hotlist_select_box_r(first_item,
                                    last_item,
                                    item->data.directory_content,
                                    itemno,
                                    minx,
                                    maxx));
6311
    }
6312 6313 6314

    /* Get the next item */

6315 6316
    item = item->next;
  }
6317 6318 6319 6320

  /* Finished */

  return NULL;
6321
}
David Brown's avatar
David Brown committed
6322 6323 6324 6325 6326

/*************************************************/
/* hotlist_contents_selected()                   */
/*                                               */
/* Checks if all items and subdirectories are    */
6327
/* selected.                                     */
David Brown's avatar
David Brown committed
6328 6329
/*                                               */
/* Parameters: Pointer to the first hotlist_item */
6330
/*             to check.                         */
David Brown's avatar
David Brown committed
6331
/*                                               */
6332 6333
/* Returns:    1 if all items are selected, else */
/*             0 (there are unselected items).   */
David Brown's avatar
David Brown committed
6334 6335
/*************************************************/

6336
static unsigned int hotlist_contents_selected(hotlist_item * item)
David Brown's avatar
David Brown committed
6337
{
6338
  while (item)
David Brown's avatar
David Brown committed
6339
  {
6340 6341 6342
    /* Keep trying to find an unselected item and */
    /* immediately return 0 if found.             */

David Brown's avatar
David Brown committed
6343
    if (!item->flags & HOTLIST_G_IS_SELECTED) return 0;
6344

David Brown's avatar
David Brown committed
6345 6346 6347 6348
    if (item->type == hl_directory)
    {
      if (!hotlist_contents_selected(item->data.directory_content)) return 0;
    }
6349

David Brown's avatar
David Brown committed
6350 6351 6352 6353 6354 6355 6356 6357 6358
    item = item->next;
  }
  return 1;
}

/*************************************************/
/* hotlist_no_contents_selected()                */
/*                                               */
/* Checks if all items and subdirectories are    */
6359
/* unselected.                                   */
David Brown's avatar
David Brown committed
6360 6361
/*                                               */
/* Parameters: Pointer to the first hotlist_item */
6362
/*             to check.                         */
David Brown's avatar
David Brown committed
6363
/*                                               */
6364 6365
/* Returns:    1 if all items are unselected,    */
/*             or 0 (there are selected items).  */
David Brown's avatar
David Brown committed
6366 6367
/*************************************************/

6368
static unsigned int hotlist_no_contents_selected(hotlist_item * item)
David Brown's avatar
David Brown committed
6369
{
6370
  while (item)
David Brown's avatar
David Brown committed
6371
  {
6372 6373 6374
    /* Keep trying to find a selected item and */
    /* immediately return 0 if found.          */

David Brown's avatar
David Brown committed
6375
    if (item->flags & HOTLIST_G_IS_SELECTED) return 0;
6376

David Brown's avatar
David Brown committed
6377 6378 6379 6380
    if (item->type == hl_directory)
    {
      if (!hotlist_no_contents_selected(item->data.directory_content)) return 0;
    }
6381

David Brown's avatar
David Brown committed
6382 6383
    item = item->next;
  }
6384

David Brown's avatar
David Brown committed
6385 6386
  return 1;
}
6387 6388 6389 6390

/*************************************************/
/* hotlist_show_descriptions_handler()           */
/*                                               */
6391 6392 6393 6394
/* Selects Show Descriptions and redraws the     */
/* hotlist window and relevant menus to reflect  */
/* this. Parameters are as standard for a        */
/* Toolbox event handler.                        */
6395 6396
/*************************************************/

6397
static int hotlist_show_descriptions_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
6398
{
6399 6400
  ObjectId main_menu;

6401
  hl_show_urls = 0;
6402 6403 6404 6405 6406 6407 6408 6409

  /* Update the menus */

  ChkError(window_get_menu(0, hotlist_windowid, &main_menu));
  hotlist_set_menu_details(main_menu);

  /* Ensure the window is up to date */

6410
  hotlist_preopen();
6411 6412 6413 6414 6415 6416

  /* Redraw as required */

  ChkError(hotlist_redraw_items(0,
                                hotlist_count_displayed_items(hotlist_root->data.directory_content)));

6417 6418 6419 6420 6421 6422
  return 1;
}

/*************************************************/
/* hotlist_show_urls_handler()                   */
/*                                               */
6423 6424 6425 6426
/* Selects Show URLs and redraws the hotlist     */
/* window and relevant menus to reflect this.    */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
6427 6428
/*************************************************/

6429
static int hotlist_show_urls_handler(int event_code, ToolboxEvent * event, IdBlock * id_block, void * handle)
6430
{
6431 6432
  ObjectId main_menu;

6433
  hl_show_urls = 1;
6434 6435 6436 6437 6438 6439 6440 6441

  /* Update the menus */

  ChkError(window_get_menu(0, hotlist_windowid, &main_menu));
  hotlist_set_menu_details(main_menu);

  /* Ensure the window is up to date */

6442
  hotlist_preopen();
6443 6444 6445 6446 6447

  /* Redraw as required */

  ChkError(hotlist_redraw_items(0,
                                hotlist_count_displayed_items(hotlist_root->data.directory_content)));
6448 6449 6450
  return 1;
}

6451 6452 6453 6454 6455 6456
/*************************************************/
/* hotlist_convert_drag_selection()              */
/*                                               */
/* Recurses through the hotlist_item structure   */
/* altering all 'HOTLIST_G_DRAG_SELECTED' items  */
/* to have 'HOTLIST_G_IS_SELECTED' set or unset  */
6457 6458
/* as is appropriate.                            */
/*                                               */
6459 6460 6461 6462 6463
/* dragselected and selected = unselected        */
/* dragselected              = selected          */
/* selected                  = selected          */
/* neither                   = unselected        */
/*                                               */
6464 6465
/* Parameters: Pointer to a hotlist_item to      */
/*             start on.                         */
6466
/*************************************************/
6467

6468 6469
static void hotlist_convert_drag_selection(hotlist_item *item)
{
6470
  while (item)
6471 6472 6473 6474
  {
    if (item->flags & HOTLIST_G_DRAG_SELECTED)
    {
      item->flags &= ~HOTLIST_G_DRAG_SELECTED;
6475

6476 6477 6478 6479
      if (item->flags & HOTLIST_G_IS_SELECTED) item->flags &= ~HOTLIST_G_IS_SELECTED;
      else                                     item->flags |= HOTLIST_G_IS_SELECTED;
    }

6480 6481
    /* Recursive call for directories */

6482 6483 6484 6485 6486 6487 6488 6489 6490
    if (item->type == hl_directory)
    {
      hotlist_convert_drag_selection(item->data.directory_content);
    }

    item = item->next;
  }
}

6491 6492 6493
/*************************************************/
/* hotlist_modified()                            */
/*                                               */
6494 6495 6496 6497 6498 6499
/* Called whenever the hotlist is modified.      */
/*                                               */
/* Parameters: The operaton that modified the    */
/*             hotlist, as a number; see the     */
/*             HL_MODIFIED_... definitions in    */
/*             Hotlist.h.                        */
6500 6501
/*************************************************/

6502
static _kernel_oserror * hotlist_modified(unsigned int type)
6503
{
6504 6505 6506 6507
  #ifdef TRACE
    if (tl & (1u<<25)) Printf("hotlist_modified: Called\n");
  #endif

6508 6509
  if (choices.save_hotlist == Choices_SaveHotlist_Always)
  {
6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526
    /* If we've loaded a resources file, there's no point  */
    /* saving the hotlist because the read-only parts will */
    /* not get saved anyway.                               */

    if (type != HL_MODIFIED_LOADED_RESOURCES)
    {
      #ifdef SINGLE_USER

        return hotlist_save(lookup_choice("HotlistSave:Browse:User.Hotlist",0,0));

      #else

        multiuser_save_hotlist();
        return NULL;

      #endif
    }
6527
  }
6528

6529 6530
  return NULL;
}
6531

6532 6533
/*************************************************/
/* hotlist_initiate_uri_save()                   */
6534 6535 6536 6537 6538 6539
/*                                               */
/* Starts file transfer of a URI file to another */
/* application.                                  */
/*                                               */
/* Parameters: Pointer to the hotlist item to    */
/*             save as a URI file.               */
6540 6541
/*************************************************/

6542
static _kernel_oserror * hotlist_initiate_uri_save(hotlist_item * item)
6543
{
6544 6545
  WimpGetPointerInfoBlock block;
  WimpMessage             message;
6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558
  int                     ctrl;

  /* Is Control being pressed? */

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

        121,
        129,

        &ctrl);

  /* Find out where the pointer is */
6559

6560
  RetError(wimp_get_pointer_info(&block));
6561

6562 6563 6564
  /* Get the leafname. Use a message block for the buffer because  */
  /* there's no point generating anything longer than can actually */
  /* be sent out in a message.                                     */
6565

6566 6567 6568
  urlutils_leafname_from_url(item->data.url,
                             message.data.data_save.leaf_name,
                             sizeof(message.data.data_save.leaf_name));
6569

6570 6571
  /* Send Message_DataSave to start file transfer - a URI file, */
  /* or if Control is held down, a URL file.                    */
6572

6573 6574 6575
  return protocols_atats_send_data_save(NULL,
                                        item,
                                        message.data.data_save.leaf_name,
6576
                                        save_uri_size(item->data.url, item->name, 0),
6577
                                        ctrl ? FileType_URL : FileType_URI,
6578 6579
                                        protocols_saving_hotlist_entry,
                                        &block);
6580 6581
}

6582 6583
/*************************************************/
/* hotlist_initiate_html_save()                  */
6584 6585 6586 6587 6588 6589
/*                                               */
/* Starts file transfer of a selection in the    */
/* hotlist as an HTML file.                      */
/*                                               */
/* Parameters: Pointer to a null-terminated      */
/*             leafname to save as.              */
6590 6591
/*************************************************/

6592
static _kernel_oserror * hotlist_initiate_html_save(char * filename)
6593
{
6594 6595
  WimpGetPointerInfoBlock block;
  WimpMessage             message;
6596

6597
  RetError(wimp_get_pointer_info(&block));
6598

6599
  /* Ensure the leafname isn't too long for a message */
6600

6601
  StrNCpy0(message.data.data_save.leaf_name, filename);
6602

6603
  /* Send Message_DataSave to start file transfer */
6604

6605 6606 6607 6608 6609 6610 6611
  return protocols_atats_send_data_save(NULL,
                                        NULL,
                                        message.data.data_save.leaf_name,
                                        -1,
                                        FileType_HTML,
                                        protocols_saving_hotlist_selection,
                                        &block);
6612 6613
}

6614
/*************************************************/
6615
/* hotlist_return_window_id()                    */
6616
/*                                               */
6617 6618
/* Returns:    The toolbox object ID of the      */
/*             hotlist window.                   */
6619 6620
/*************************************************/

6621
ObjectId hotlist_return_window_id(void)
6622 6623 6624 6625 6626 6627 6628 6629 6630 6631
{
  return hotlist_windowid;
}

/*************************************************/
/* hotlist_add_position()                        */
/*                                               */
/* Add a new URL to the hotlist at a specified   */
/* position.                                     */
/*                                               */
6632
/* Parameters: Screen relative x position;       */
6633
/*                                               */
6634
/*             Screen relative y position;       */
6635 6636 6637 6638
/*                                               */
/*             Description of the URL (e.g. from */
/*             the page title);                  */
/*                                               */
6639
/*             Pointer to the URL itself.        */
6640 6641
/*************************************************/

6642
_kernel_oserror * hotlist_add_position(int x, int y, char * description, char * url)
6643
{
6644
  WimpGetWindowStateBlock   state;
6645
  hotlist_item            * targetitem;
6646 6647 6648 6649
  unsigned int              item_height, item_dir_width, item_url_width;
  int                       winx, winy;
  int                       top, position;

6650
  /* Calculate window relative coordinates */
6651 6652

  RetError(window_get_wimp_handle(0,
6653
                                  hotlist_windowid,
6654 6655
                                  &state.window_handle));

6656
  RetError(wimp_get_window_state(&state));
6657 6658 6659

  winx = coords_x_toworkarea(x, (WimpRedrawWindowBlock *) &state);
  winy = coords_y_toworkarea(y, (WimpRedrawWindowBlock *) &state);
6660 6661

  /* Calculate which item add is over */
6662

6663
  hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width);
6664 6665 6666

  top        = (-winy) / item_height;
  targetitem = hotlist_find_item(hotlist_root->data.directory_content, top);
6667 6668

  /* Decide where to put the new item */
6669

6670 6671
  if (targetitem)
  {
6672
    if ((-winy % item_height) > item_height / 2)
6673
    {
6674 6675 6676
      /* Put item after the target if we're over */
      /* half way past its height                */

6677
      position = HOTLIST_POSITION_AFTER;
6678 6679 6680
    }
    else
    {
6681 6682
      /* Otherwise, put item before the target */

6683
      position = HOTLIST_POSITION_BEFORE;
6684 6685 6686 6687 6688
    }
  }
  else
  {
    /* Put item at end of root directory */
6689 6690

    position   = HOTLIST_POSITION_END;
6691 6692 6693 6694 6695
    targetitem = hotlist_root;
  }

  /* Create new item in appropriate place */

6696 6697 6698 6699 6700 6701
  RetError(hotlist_new_url(targetitem,
                           position,
                           description,
                           url));

  hotlist_preopen();
6702 6703 6704 6705 6706 6707 6708 6709

  /* Optimise the redraw to do as little as possible depending */
  /* upon where in the list the item was added.                */

  switch (position)
  {
    case HOTLIST_POSITION_END:
    {
6710
      RetError(hotlist_redraw_now());
6711 6712 6713 6714 6715 6716
    }
    break;

    default:
    {
      hotlist_clear_flags(hotlist_root, hl_ALL, HOTLIST_G_REDRAW_NOW);
6717 6718 6719

      RetError(hotlist_redraw_items(top - 1,
                                    hotlist_count_displayed_items(hotlist_root->data.directory_content)));
6720 6721 6722 6723
    }
    break;
  }

6724 6725 6726
  /* Exit by calling the routine that we must call every */
  /* time the hotlist changes (so that it can, for       */
  /* example, be saved - should the Choices require it.  */
6727

6728
  return hotlist_modified(HL_MODIFIED_ADD);
6729
}
6730

6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753
/*************************************************/
/* hotlist_add_html_file()                       */
/*                                               */
/* Read in an HTML file, adding items starting   */
/* at the specified position.                    */
/*                                               */
/* Parameters: Screen relative x position;       */
/*                                               */
/*             Screen relative y position;       */
/*                                               */
/*             Pathname of the HTML file.        */
/*************************************************/

_kernel_oserror * hotlist_add_html_file(int x, int y, char * path)
{
  _kernel_oserror         * e;
  hotlist_item            * targetitem;
  hotlist_item            * newitem;
  FILE                    * fileptr;
  WimpGetWindowStateBlock   state;
  unsigned int              item_height, item_dir_width, item_url_width;
  int                       winx, winy;
  int                       top, position;
6754
  char                    * dirname;
6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799

  /* Calculate window relative coordinates */

  RetError(window_get_wimp_handle(0,
                                  hotlist_windowid,
                                  &state.window_handle));

  RetError(wimp_get_window_state(&state));

  winx = coords_x_toworkarea(x, (WimpRedrawWindowBlock *) &state);
  winy = coords_y_toworkarea(y, (WimpRedrawWindowBlock *) &state);

  /* Calculate which item add is over */

  hotlist_get_entry_sizes(&item_height, &item_dir_width, &item_url_width);

  top        = (-winy) / item_height;
  targetitem = hotlist_find_item(hotlist_root->data.directory_content, top);

  /* Decide where to put the new item */

  if (targetitem)
  {
    if ((-winy % item_height) > item_height / 2)
    {
      /* Put item after the target if we're over */
      /* half way past its height                */

      position = HOTLIST_POSITION_AFTER;
    }
    else
    {
      /* Otherwise, put item before the target */

      position = HOTLIST_POSITION_BEFORE;
    }
  }
  else
  {
    /* Put item at end of root directory */

    position   = HOTLIST_POSITION_END;
    targetitem = hotlist_root;
  }

6800 6801 6802 6803 6804 6805
  dirname = strrchr(path, '.');
  if (dirname) dirname ++;
  if (!dirname || !*dirname) dirname = lookup_token("HotlistUntitled:(Untitled)",0,0);

  RetError(hotlist_new_directory(targetitem, dirname, position, &newitem));

6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821
  /* Open the file */

  fileptr = fopen(path, "r");

  if (fileptr == NULL)
  {
    erb.errnum = Utils_Error_Custom_Normal;

    StrNCpy0(erb.errmess,
             lookup_token("HlCantLoad:Hotlist file could not be loaded",
                          0,
                          0));

    return &erb;
  }

6822
  /* Load it, adding to the new directory */
6823

6824
  e = hotlist_load_directory(fileptr, newitem);
6825 6826 6827 6828 6829 6830 6831 6832 6833

  fclose(fileptr);

  if (e) return e;

  /* Handle redraw */

  hotlist_preopen();

6834
  hotlist_set_flags(hotlist_root, hl_ALL, HOTLIST_G_REDRAW_NOW);
6835

6836
  RetError(hotlist_redraw_now());
6837 6838 6839 6840 6841 6842

  /* Exit through the 'has modified' routine */

  return hotlist_modified(HL_MODIFIED_ADD);
}

6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910
/*************************************************/
/* hotlist_load_resources()                      */
/*                                               */
/* Read in a read-only 'Resources' hotlist, at   */
/* the top of the hotlist window. This will not  */
/* be saved when the rest of the hotlist is.     */
/*                                               */
/* Errors are raised if there is not enough      */
/* memory etc., but not if the file doesn't      */
/* exist.                                        */
/*                                               */
/* Parameters: Pathname of the HTML file.        */
/*************************************************/

_kernel_oserror * hotlist_load_resources(char * path)
{
  _kernel_oserror * e;
  hotlist_item    * newitem;
  FILE            * fileptr;

  RetError(hotlist_new_directory(hotlist_root,
                                 lookup_token("HotlistResDir:Resources",0,0),
                                 HOTLIST_POSITION_BEGINNING,
                                 &newitem));

  /* Open the file */

  fileptr = fopen(path, "r");

  /* Fail silently if there's an error opening it */

  if (fileptr == NULL) return NULL;

  /* Load it, adding to the new directory */

  e = hotlist_load_directory(fileptr, newitem);

  fclose(fileptr);

  if (e) return e;

  /* Set all contained items as read-only, and mark the top-level */
  /* Resources directory as both read-only and, if the Choices    */
  /* say so, open.                                                */

  newitem->flags |= (HOTLIST_G_IS_READ_ONLY);

  hotlist_set_flags(newitem->data.directory_content,
                    hl_ALL,
                    HOTLIST_G_IS_READ_ONLY);

  if (!strcmp(lookup_choice("OpenResources:no",0,0), "yes"))
  {
    newitem->flags |= (HOTLIST_D_IS_OPEN);
  }

  /* Handle redraw */

  hotlist_preopen();
  hotlist_set_flags(hotlist_root, hl_ALL, HOTLIST_G_REDRAW_NOW);

  RetError(hotlist_redraw_now());

  /* Exit through the 'has modified' routine */

  return hotlist_modified(HL_MODIFIED_LOADED_RESOURCES);
}

6911 6912 6913 6914 6915 6916 6917
/*************************************************/
/* hotlist_get_selected_shape()                  */
/*                                               */
/* This routine gets the coordinates required to */
/* bound every selected item in the hotlist      */
/* window (window relative coordinated)          */
/*                                               */
6918 6919
/* Parameters: Pointer to window relative        */
/*             bounding box.                     */
6920
/*                                               */
6921 6922
/* Returns:    1 if there were selected item(s), */
/*             0 if there weren't.               */
6923 6924 6925 6926
/*************************************************/

static int hotlist_get_selected_shape(BBox * box)
{
6927 6928 6929
  _kernel_oserror * e;
  unsigned int      itemno, item_height;
  int               found;
6930 6931 6932

  found  = 0;
  itemno = 0;
6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955

  /* Get item sizes */

  e = hotlist_get_entry_sizes(&item_height, NULL, NULL);

  if (e)
  {
    show_error_ret(e);
    return 0;
  }

  /* Enter the recursive routine */

  e = hotlist_get_selected_shape_r(hotlist_root->data.directory_content,
                                   box,
                                   &itemno,
                                   &found,
                                   item_height);
  if (e)
  {
    show_error_ret(e);
    return 0;
  }
6956 6957 6958 6959 6960 6961 6962 6963

  return found;
}

/*************************************************/
/* hotlist_get_selected_shape_r()                */
/*                                               */
/* Recursive back-end to                         */
6964
/* hotlist_get_selected_shape().                 */
6965
/*                                               */
6966
/* Parameters: Pointer to a hotlist_item;        */
6967
/*                                               */
6968 6969
/*             Pointer to window relative        */
/*             bounding box;                     */
6970
/*                                               */
6971 6972
/*             Pointer to the current item       */
/*             number (for internal use);        */
6973
/*                                               */
6974
/*             Pointer to found flag;            */
6975 6976
/*                                               */
/*             The height of a single entry in   */
6977
/*             OS units.                         */
6978 6979
/*************************************************/

6980
static _kernel_oserror * hotlist_get_selected_shape_r(hotlist_item * list, BBox * box, unsigned int * itemno, int * found, unsigned int item_height)
6981
{
6982
  int newxmin, newxmax;
6983 6984

  while (list)
6985 6986 6987
  {
    if (list->flags & HOTLIST_G_IS_SELECTED)
    {
6988 6989 6990 6991
      RetError(hotlist_get_shape(&newxmin, &newxmax, list));

      /* If we've already found an open item, then only extend the */
      /* bounding box in appropriate directions.                   */
6992 6993 6994

      if (*found)
      {
6995 6996
        if (newxmin < box->xmin) box->xmin = newxmin;
        if (newxmax > box->xmax) box->xmax = newxmax;
6997 6998

        box->ymin = -((*itemno) + 1) * item_height;
6999 7000 7001
      }
      else
      {
7002 7003 7004 7005
        /* This section run for first selected item found only; */
        /* set the selected shape box to match the item's       */
        /* bounding box.                                        */

7006 7007
        box->xmin = newxmin;
        box->xmax = newxmax;
7008 7009 7010 7011 7012
        box->ymax = -(*itemno)       * item_height;
        box->ymin = -((*itemno) + 1) * item_height;

        /* Flag that we've found a selected item */

7013 7014 7015 7016
        *found = 1;
      }
    }

7017
    *itemno += 1;
7018 7019 7020 7021 7022

    /* Recursively scan all open directories */

    if (list->type == hl_directory && (list->flags & HOTLIST_D_IS_OPEN))
    {
7023 7024 7025 7026 7027
      RetError(hotlist_get_selected_shape_r(list->data.directory_content,
                                            box,
                                            itemno,
                                            found,
                                            item_height));
7028
    }
7029 7030 7031

    /* Follow the list */

7032 7033
    list = list->next;
  }
7034 7035 7036 7037

  /* Finished */

  return NULL;
7038
}
7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179

/*************************************************/
/* hotlist_find_match()                          */
/*                                               */
/* Takes a string from the given buffer and sees */
/* if there's something in the Hotlist that      */
/* matches it in some way.                       */
/*                                               */
/* If it finds something, it writes it back to   */
/* the buffer and returns 1.                     */
/*                                               */
/* Parameters: Pointer to the buffer holding the */
/*             string to try and match;          */
/*                                               */
/*             Size of the buffer in bytes.      */
/*                                               */
/* Returns:    1 if the buffer is updated with a */
/*             match string, else 0 (buffer      */
/*             contents will be unaltered).      */
/*************************************************/

int hotlist_find_match(char * buffer, int buffer_size)
{
  hotlist_item * lowest_item   = NULL;
  int            lowest_offset = -1;
  int            lowest_diff   = 0;

  if (!buffer || !*buffer) return 0;

  hotlist_find_match_r(buffer,
                       buffer_size,
                       hotlist_root,
                       &lowest_item,
                       &lowest_offset,
                       &lowest_diff);

  if (lowest_item && lowest_offset >= 0)
  {
    strncpy(buffer, lowest_item->data.url, buffer_size);
    buffer[buffer_size - 1] = 0;

    return 1;
  }

  return 0;
}

/*************************************************/
/* hotlist_find_match_r()                        */
/*                                               */
/* Recursive back-end to hotlist_find_match.     */
/*                                               */
/* Parameters: Pointer to the buffer holding the */
/*             string to try and match;          */
/*                                               */
/*             Size of the buffer in bytes;      */
/*                                               */
/*             Pointer to a hotlist_item at the  */
/*             top of the directory to scan;     */
/*                                               */
/*             Pointer to a hotlist_item * in    */
/*             which the best match hotlist_item */
/*             is written, if any;               */
/*                                               */
/*             Pointer to an int, in which the   */
/*             lowest offset into a string so    */
/*             far is written;                   */
/*                                               */
/*             Pointer to an int, in which the   */
/*             lowest difference in lengths      */
/*             between the buffer and match      */
/*             string is written.               */
/*************************************************/

static void hotlist_find_match_r(char * buffer, int buffer_size, hotlist_item * item,
                                 hotlist_item ** lowest_item, int * lowest_offset,
                                 int * lowest_diff)
{
  while (item)
  {
    if (item->type == hl_directory)
    {
      hotlist_find_match_r(buffer,
                           buffer_size,
                           item->data.directory_content,
                           lowest_item,
                           lowest_offset,
                           lowest_diff);
    }
    else
    {
      int          this_offset = 0;
      int          got_one     = 0;
      const char * found;

      if (item->data.url)
      {
        found = strstr(item->data.url, buffer);

        if (found)
        {
          this_offset = found - item->data.url;
          got_one     = 1;
        }
      }

      if (!got_one && item->name)
      {
        found = strstr(item->name, buffer);

        if (found)
        {
          this_offset = found - item->name;
          got_one     = 1;
        }
      }

      if (got_one)
      {
        if (*lowest_offset < 0 || this_offset <= *lowest_offset)
        {
          int this_diff = strlen(item->data.url) - strlen(buffer);

          *lowest_offset = this_offset;

          if (this_offset < *lowest_offset) *lowest_diff = 0;

          if (!*lowest_diff || (this_diff && this_diff < *lowest_diff))
          {
            *lowest_diff = this_diff;
            *lowest_item = item;
          }
        }
      }
    }

    item = item->next;
  }

  return;
}
7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198

/*************************************************/
/* hotlist_empty()                               */
/*                                               */
/* See if the hotlist is empty or not.           */
/*                                               */
/* Returns:    1 if the hotlist is empty else 0. */
/*************************************************/

int hotlist_empty(void)
{
  if (
       !hotlist_root                         ||
       !hotlist_root->data.directory_content
     )
     return 1;

  return 0;
}