Choices 129 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
/* 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   : Choices.c                              */
/*                                                 */
/* Purpose: Functions relating to the choices      */
/*          dialogue box and associated sub        */
/*          windows                                */
/*                                                 */
/* Author : D.T.A.Brown                            */
/*                                                 */
/* History: 23-Sep-97: Created.                    */
/***************************************************/

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

#include "swis.h"

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

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

#include "toolbox.h"
#include "window.h"
#include "menu.h"
#include "ColourDbox.h"
42
#include "FontMenu.h"
43 44 45 46 47 48 49 50

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

#include "Encoding.h"
#include "FetchPage.h"
51
#include "FontManage.h"
52
#include "History.h"
53
#include "Limits.h"
54 55
#include "Menus.h"
#include "NestWimp.h"
56
#include "Redraw.h"
57
#include "Reformat.h"
58 59 60 61 62 63
#include "Save.h"
#include "URLutils.h"
#include "Windows.h"

#include "Choices.h"

64 65 66 67
/* Local compile-time options */

#define CREATE_ONLY_ONCE

68
/* Locals */
69

David Brown's avatar
David Brown committed
70 71 72
static char consonant[]                = "bcdfghjklmnpqrstvwxyz";
static char vowel[]                    = "aeiou";
static int  rubbish_seed;
73

74
/* Used to prevent rounding errors when converting to/from font 16ths of a point to decimal */
75

76 77 78
static unsigned char todecimal[]   = {0, 0, 1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 8, 9};
static unsigned char fromdecimal[] = {0, 2, 4, 5, 7, 8, 10, 12, 13, 15};

79 80 81
static ObjectId *    subwindows          = NULL;
static int           current_subwindow   = 0;
static ObjectId      colourdbox_id       = 0;
82

83 84 85
static int           save_ticked         = -1;
static int           disp_ticked         = -1;
static int           plug_ticked         = -1;
86 87 88
static int           time_ticked         = -1;
static int           bord_ticked         = -1;

89 90 91 92 93 94 95 96 97
static int           history_radio       = 0; /* 0 - page history, 1 - image history */

static int           expiry_age_greyed       = 0;
static int           max_size_greyed         = 0;
static int           image_expiry_age_greyed = 0;
static int           image_max_size_greyed   = 0;

static int           image_expiry_units   = -1;
static int           history_expiry_units = -1;
98

99
static int           choices_modechanged = 0;
100

101 102 103 104
global_choices  *    new_choices         = NULL;
ObjectId             choices_windowid    = 0;

static fm_typeface * new_typefaces       = NULL;
105

106
#ifdef CREATE_ONLY_ONCE
107
  static int         objects_created     = 0;
108 109
#endif

110
/* Static function prototypes */
111 112 113 114

static _kernel_oserror * choices_show_subwindow          (ObjectId choices_window, int subwindow);
static _kernel_oserror * choices_set_contents            (void);
static _kernel_oserror * choices_get_contents            (void);
115 116
_kernel_oserror        * choices_set_encoding_field      (void);
_kernel_oserror        * choices_set_plugin_field        (void);
117
static void              choices_set_expiry_age          (ObjectId window);
118
static void              choices_set_expiry_age_greyed   (ObjectId window, int state);
119 120
static void              choices_set_tables_greyed       (int state, ObjectId window);
static _kernel_oserror * choices_set_table_border_field  (ObjectId obj, ComponentId comp, int state);
121
static void              choices_set_fonts_greyed        (ObjectId window, int state);
122
static void              choices_set_max_size            (ObjectId window);
123
static void              choices_set_max_size_greyed     (ObjectId window, int state);
124 125
static void              choices_set_im_max_size         (ObjectId window);
static void              choices_set_im_max_size_greyed  (ObjectId window, int state);
126
int                      choices_clip_to_min_max         (int value, int min, int max);
127 128 129
static void              choices_set_im_expiry_age       (ObjectId window);
static void              choices_set_im_expiry_age_greyed(ObjectId window, int state);
_kernel_oserror        * choices_set_timetype_tick       (int timetype);
130

131
_kernel_oserror        * choices_set_timetype_field      (int timetype, ComponentId comp);
132 133

static void              choices_modified_font           (char * orig, char * mod, char * buffer);
134 135 136 137 138 139 140 141 142 143 144 145 146

static int               choices_radio_click_handler     (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_set_button_handler      (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_cancel_button_handler   (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_save_button_handler     (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_encoding_button_handler (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_colour_button_handler   (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_colour_closed_handler   (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_colour_selected_handler (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_display_m_button_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_save_m_button_handler   (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_display_m_click_handler (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_save_m_click_handler    (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
147
static int               choices_option_state_handler    (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
148 149 150
static int               choices_font_button_handler     (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_font_closed_handler     (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_font_selected_handler   (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
151 152
static int               choices_redraw_fakepage_handler (int eventcode, WimpPollBlock * event, IdBlock * id_block, void * handle);
static int               choices_open_choice_window      (int eventcode, WimpPollBlock * event, IdBlock * id_block, void * handle);
153 154
static int               choices_plug_m_button_handler   (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_plug_m_click_handler    (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
155 156 157 158
static int               choices_timetype_m_button_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_timetype_m_click_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_bord_m_button_handler   (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
static int               choices_bord_m_click_handler    (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
159
static int               choices_history_radio_handler   (int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle);
160

161 162 163
static _kernel_oserror * choices_colour_set_component    (ObjectId window, ComponentId component, int colour);
static _kernel_oserror * choices_set_save_field          (ObjectId obj, ComponentId comp, int state);
static _kernel_oserror * choices_set_display_field       (ObjectId obj, ComponentId comp, int state);
164
static ObjectId          choices_find_component          (ComponentId component);
165
_kernel_oserror        * choices_get_menu_entry_text     (char * menuname, ComponentId compid, char ** tempstring);
166

167 168 169 170 171
static _kernel_oserror * choices_displayfield_set_value  (unsigned int flags, ObjectId window, ComponentId writable, char *text);
static _kernel_oserror * choices_writablefield_set_value (unsigned int flags, ObjectId window, ComponentId writable, char *text);
static _kernel_oserror * choices_button_set_validation   (unsigned int flags, ObjectId window, ComponentId writable, char *text);
static _kernel_oserror * choices_numberrange_set_value   (unsigned int flags, ObjectId window, ComponentId writable, int value);

172 173 174 175 176 177
static int               choices_return_appropriate_timetype(int seconds);
static int               choices_seconds_to_typed_time   (int secs, int timetype);
static int               choices_typed_time_to_seconds   (int time, int timetype);
static int               choices_get_range_of_typed_time (int time);


178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
/*************************************************/
/* choices_show_subwindow()                      */
/*                                               */
/* Shows a choice subwindow in the main choices  */
/* window.                                       */
/*                                               */
/* Parameters: the ObjectId of the main choices  */
/*             window                            */
/*                                               */
/*             the number of the subwindow to    */
/*             show.  Defined in choices.h       */
/*             Use CDSubNone to remove           */
/*             the current subwindow.            */
/*                                               */
/* Returns:    pointer to _kernel_oserror        */
/*************************************************/

195
static _kernel_oserror * choices_show_subwindow(ObjectId choices_window, int subwindow)
196
{
197 198 199 200 201 202
  _kernel_oserror           * e;
  WindowShowObjectBlock       show_block;
  WimpGetWindowStateBlock     state;
  WimpGetWindowOutlineBlock   outline;
  int                         window_handle;
  int                         vwidth, hheight;
203

204
  if (current_subwindow != CDSubNone && current_subwindow != subwindow)
205 206
  {
    /* remove the current subwindow */
207

208
    RetError(toolbox_hide_object(0, subwindows[current_subwindow]));
209
    current_subwindow = CDSubNone;
210 211
  }

212
  if (subwindow == CDSubNone || !(subwindows[subwindow]))
213 214 215 216 217 218 219 220 221 222 223 224
  {
    /* Have not been asked to open a new subwindow */

    return NULL;
  }

  RetError(window_get_wimp_handle(0, choices_window, &window_handle));
  state.window_handle = window_handle;
  RetError(wimp_get_window_state(&state));
  RetError(gadget_get_bbox(0, choices_window, CDPlaceHolder, &show_block.visible_area));
  windows_return_tool_sizes(NULL, &hheight, &vwidth);

225 226 227 228 229
  show_block.visible_area.xmin = coords_x_toscreen(show_block.visible_area.xmin, (WimpRedrawWindowBlock *) &state);
  show_block.visible_area.xmax = coords_x_toscreen(show_block.visible_area.xmax, (WimpRedrawWindowBlock *) &state);
  show_block.visible_area.ymin = coords_y_toscreen(show_block.visible_area.ymin, (WimpRedrawWindowBlock *) &state);
  show_block.visible_area.ymax = coords_y_toscreen(show_block.visible_area.ymax, (WimpRedrawWindowBlock *) &state);

230 231 232
  /* Adjustments for different types of window */

  #ifdef TRACE
233
    if (tl & (1u<<29)) Printf("choices_show_subwindow: Wimp flags = %x\n", state.flags);
234 235
  #endif

236
  RetError(window_get_wimp_handle(0, subwindows[subwindow], &state.window_handle));
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  RetError(wimp_get_window_state(&state));

  if (state.flags & WimpWindow_VScroll) show_block.visible_area.xmax -= vwidth;
  else vwidth  = 0;

  if (state.flags & WimpWindow_HScroll) show_block.visible_area.ymin += hheight;
  else hheight = 0;

  show_block.xscroll = 0;
  show_block.yscroll = 0;
  show_block.behind  = -1;

  show_block.window_flags    = 0;
  show_block.alignment_flags = 0;

  /* Requires wimp handle just to be awkward */
  show_block.parent_window_handle = window_handle;

  RetError(toolbox_show_object(Toolbox_ShowObject_AsSubWindow,
256
                               subwindows[subwindow],
257 258 259 260 261 262 263 264 265
                               Toolbox_ShowObject_FullSpec,
                               &show_block,
                               choices_window,
                               -1));

  outline.window_handle = state.window_handle;
  wimp_get_window_outline(&outline);

  if (outline.outline.xmin < show_block.visible_area.xmin)
266
  {
267
    show_block.visible_area.xmin += (show_block.visible_area.xmin - outline.outline.xmin);
268
  }
269
  if (outline.outline.ymin < (show_block.visible_area.ymin - hheight))
270
  {
271
    show_block.visible_area.ymin += (show_block.visible_area.ymin - outline.outline.ymin);
272
  }
273
  if (outline.outline.xmax > (show_block.visible_area.xmax + vwidth))
274
  {
275
    show_block.visible_area.xmax -= (outline.outline.xmax - show_block.visible_area.xmax);
276
  }
277
  if (outline.outline.ymax > show_block.visible_area.ymax)
278
  {
279
    show_block.visible_area.ymax -= (outline.outline.ymax - show_block.visible_area.ymax);
280
  }
281 282

  e = toolbox_show_object(Toolbox_ShowObject_AsSubWindow,
283
                          subwindows[subwindow],
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
                          Toolbox_ShowObject_FullSpec,
                          &show_block,
                          choices_window,
                          -1);

  if (!e) current_subwindow = subwindow;

  return e;
}

/*************************************************/
/* choices_to_be_shown()                         */
/*                                               */
/* Called when the ECDToBeShown event is         */
/* generated.                                    */
/* Creates all the subwindow dialogues and fills */
/* them in with appropriate values.              */
/* Registers all additional event handlers used  */
/* while the choices dialogue is visible.        */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int choices_to_be_shown(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  _kernel_oserror       *e;
311
  ObjectId              window;
312
  int                   subwindow_tobeshown;
313 314

  #ifdef TRACE
315
    if (tl & (1u<<29)) Printf("choices_to_be_shown: Called\n");
316 317
  #endif

318 319
  if (choices_windowid) return 1;

David Brown's avatar
David Brown committed
320 321
  _swix(OS_ReadMonotonicTime, _OUT(0), &rubbish_seed);

322 323 324
  choices_windowid = idb->self_id;

  /* Allocate block for holding subwindows ObjectIds */
325

326 327 328 329
  if (!subwindows)
  {
    subwindows = malloc(sizeof(ObjectId)*CDNoSubwindows);

330
    /* If hide object doesn't work, things are really screwed */
331

332 333 334 335 336
    if (!subwindows)
    {
      ChkError(toolbox_hide_object(0, idb->self_id));
      return 1;
    }
337 338 339 340 341
  }

  if (!new_choices)
  {
    new_choices = malloc(sizeof(global_choices));
342

343 344
    if (!new_choices)
    {
345
      ChkError(toolbox_hide_object(0, idb->self_id));
346
      return 1;
347 348 349 350 351 352 353
    }
    else
    {
      memcpy(new_choices, &choices, sizeof(global_choices));
    }
  }

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
  if (!new_typefaces)
  {
    new_typefaces = calloc(sizeof(fm_typeface), 3);

    if (!new_typefaces)
    {
      free(new_choices);
      new_choices = NULL;
      free(subwindows);
      subwindows  = NULL;
    }
    else
    {
      fm_typeface * tfptr;

      tfptr = fm_find_typeface("serif");
      memcpy(&new_typefaces[0], tfptr, sizeof(fm_typeface));
      tfptr = fm_find_typeface("sans");
      memcpy(&new_typefaces[1], tfptr, sizeof(fm_typeface));
      tfptr = fm_find_typeface("fixed");
      memcpy(&new_typefaces[2], tfptr, sizeof(fm_typeface));
    }
  }

378 379
  /* Create all choices subwindows.  Set their ObjectIds to NULL if they cannot be created */

380 381
  /* Might be worth changing this to a loop in the future */

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  #ifdef CREATE_ONLY_ONCE
  if (!objects_created)
  {
  #endif
    e = toolbox_create_object(0, "ChSub0", &subwindows[0]); if (e) subwindows[0] = NULL;
    e = toolbox_create_object(0, "ChSub1", &subwindows[1]); if (e) subwindows[1] = NULL;
    e = toolbox_create_object(0, "ChSub2", &subwindows[2]); if (e) subwindows[2] = NULL;
    e = toolbox_create_object(0, "ChSub3", &subwindows[3]); if (e) subwindows[3] = NULL;
    e = toolbox_create_object(0, "ChSub4", &subwindows[4]); if (e) subwindows[4] = NULL;
    e = toolbox_create_object(0, "ChSub5", &subwindows[5]); if (e) subwindows[5] = NULL;
    e = toolbox_create_object(0, "ChSub6", &subwindows[6]); if (e) subwindows[6] = NULL;
    e = toolbox_create_object(0, "ChSub7", &subwindows[7]); if (e) subwindows[7] = NULL;
    e = toolbox_create_object(0, "ChSub8", &subwindows[8]); if (e) subwindows[8] = NULL;
  #ifdef CREATE_ONLY_ONCE
  }
  #endif
398

399 400 401 402
  subwindow_tobeshown = current_subwindow;
  current_subwindow   = CDSubNone;

  ChkError(choices_show_subwindow(idb->self_id, subwindow_tobeshown));
403

404
  ChkError(radiobutton_set_state(0, idb->self_id, CDFirstSubRadio + subwindow_tobeshown, 1));
405 406 407 408 409 410

  ChkError(choices_set_contents());

  /* Register choices event handlers. Make sure any additions here */
  /* are matched in the deregistration section below, and add to   */
  /* choices_hidden.                                               */
411 412
  #ifdef CREATE_ONLY_ONCE
  e = NULL;
413

414
  if (!objects_created)
415
  {
416 417 418 419 420 421 422 423 424 425 426 427 428
  #endif
    e         = event_register_toolbox_handler(-1, ECDRG2,                    choices_radio_click_handler,       NULL);
    if (!e) e = event_register_toolbox_handler(-1, CDSet,                     choices_set_button_handler,        NULL);
    if (!e) e = event_register_toolbox_handler(-1, CDCancel,                  choices_cancel_button_handler,     NULL);
    if (!e) e = event_register_toolbox_handler(-1, CDSaveButton,              choices_save_button_handler,       NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECD_EncodingMenuBt,        choices_encoding_button_handler,   NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECD_ColourButton,          choices_colour_button_handler,     NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECD_HlDispBt,              choices_display_m_button_handler,  NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECD_HiDispBt,              choices_display_m_button_handler,  NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECD_HlSaveBt,              choices_save_m_button_handler,     NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECD_HiSaveBt,              choices_save_m_button_handler,     NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECDSaveMenuClick,          choices_save_m_click_handler,      NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECDDispMenuClick,          choices_display_m_click_handler,   NULL);
429
    if (!e) e = event_register_toolbox_handler(-1, ECDHiRadioClick,           choices_history_radio_handler,     NULL);
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    if (!e) e = event_register_toolbox_handler(-1, OptionButton_StateChanged, choices_option_state_handler,      NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECD_FontButton,            choices_font_button_handler,       NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECD_ObjPlugBt,             choices_plug_m_button_handler,     NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECDPlugMenuClick,          choices_plug_m_click_handler,      NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECD_HiAgeTypeBt,           choices_timetype_m_button_handler, NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECDTimeMenuClick,          choices_timetype_m_click_handler,  NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECD_TabInnerBordBt,        choices_bord_m_button_handler,     NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECD_TabOuterBordBt,        choices_bord_m_button_handler,     NULL);
    if (!e) e = event_register_toolbox_handler(-1, ECDBordMenuClick,          choices_bord_m_click_handler,      NULL);
    if ((window = choices_find_component(CD_FakePage)) != 0)
    {
      if (!e) e = event_register_wimp_handler(window, Wimp_ERedrawWindow, choices_redraw_fakepage_handler, NULL);
    }
    if (!e) e = event_register_wimp_handler(idb->self_id, Wimp_EOpenWindow, choices_open_choice_window, NULL);
  #ifdef CREATE_ONLY_ONCE
445
  }
446
  #endif
447

448
  /* Deal with errors */
449 450 451

  if (e)
  {
452 453 454 455 456 457 458 459 460 461 462 463
    event_deregister_toolbox_handler(-1, ECDRG2,                    choices_radio_click_handler,      NULL);
    event_deregister_toolbox_handler(-1, CDSet,                     choices_set_button_handler,       NULL);
    event_deregister_toolbox_handler(-1, CDCancel,                  choices_cancel_button_handler,    NULL);
    event_deregister_toolbox_handler(-1, CDSaveButton,              choices_save_button_handler,      NULL);
    event_deregister_toolbox_handler(-1, ECD_EncodingMenuBt,        choices_encoding_button_handler,  NULL);
    event_deregister_toolbox_handler(-1, ECD_ColourButton,          choices_colour_button_handler,    NULL);
    event_deregister_toolbox_handler(-1, ECD_HlDispBt,              choices_display_m_button_handler, NULL);
    event_deregister_toolbox_handler(-1, ECD_HiDispBt,              choices_display_m_button_handler, NULL);
    event_deregister_toolbox_handler(-1, ECD_HlSaveBt,              choices_save_m_button_handler,    NULL);
    event_deregister_toolbox_handler(-1, ECD_HiSaveBt,              choices_save_m_button_handler,    NULL);
    event_deregister_toolbox_handler(-1, ECDSaveMenuClick,          choices_save_m_click_handler,     NULL);
    event_deregister_toolbox_handler(-1, ECDDispMenuClick,          choices_display_m_click_handler,  NULL);
464
    event_deregister_toolbox_handler(-1, ECDHiRadioClick,           choices_history_radio_handler,     NULL);
465 466 467 468
    event_deregister_toolbox_handler(-1, OptionButton_StateChanged, choices_option_state_handler,     NULL);
    event_deregister_toolbox_handler(-1, ECD_FontButton,            choices_font_button_handler,      NULL);
    event_deregister_toolbox_handler(-1, ECD_ObjPlugBt,             choices_plug_m_button_handler,    NULL);
    event_deregister_toolbox_handler(-1, ECDPlugMenuClick,          choices_plug_m_click_handler,     NULL);
469 470 471 472 473
    event_deregister_toolbox_handler(-1, ECD_HiAgeTypeBt,           choices_timetype_m_button_handler, NULL);
    event_deregister_toolbox_handler(-1, ECDTimeMenuClick,          choices_timetype_m_click_handler,  NULL);
    event_deregister_toolbox_handler(-1, ECD_TabInnerBordBt,        choices_bord_m_button_handler,     NULL);
    event_deregister_toolbox_handler(-1, ECD_TabOuterBordBt,        choices_bord_m_button_handler,     NULL);
    event_deregister_toolbox_handler(-1, ECDBordMenuClick,          choices_bord_m_click_handler,      NULL);
474 475 476 477 478

    if ((window = choices_find_component(CD_FakePage)) != 0)
    {
      event_deregister_wimp_handler(window, Wimp_ERedrawWindow, choices_redraw_fakepage_handler, NULL);
    }
479 480

    event_deregister_wimp_handler(idb->self_id, Wimp_EOpenWindow, choices_open_choice_window, NULL);
481 482

    ChkError(e);
483 484
  }

485 486 487 488
  #ifdef CREATE_ONLY_ONCE
  objects_created = 1;
  #endif

489 490 491
  return 1;
}

492 493 494 495 496 497 498 499
#ifndef CREATE_ONLY_ONCE
  /*************************************************/
  /* choices_delete_subwindows()                   */
  /*                                               */
  /* Deletes all subwindows, reports any and all   */
  /* errors occuring while deleting them.          */
  /* Frees the structure holding the subwindows.   */
  /*************************************************/
500

501
  static void choices_delete_subwindows(void)
502
  {
503 504 505 506
    _kernel_oserror *e;
    int count;

    for(count = 0; count < CDNoSubwindows; count++)
507
    {
508 509 510 511 512 513
      if (subwindows[count])
      {
        e = toolbox_delete_object(0, subwindows[count]);
        if (e) show_error_ret(e);
        subwindows[count] = 0;
      }
514
    }
515 516
    free(subwindows);
    subwindows = NULL;
517
  }
518
#endif
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533

/*************************************************/
/* choices_hidden()                              */
/*                                               */
/* Called when the ECDHidden event is generated. */
/* Deletes all the subwindow dialogues.          */
/* Deregisters all additional event handlers     */
/* used while the choices dialogue is visible.   */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

int choices_hidden(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
534
  _kernel_oserror * e;
535 536 537
  #ifndef CREATE_ONLY_ONCE
    ObjectId          window;
  #endif
538 539 540
  int               temp;

  temp = current_subwindow;
541 542 543 544 545 546

  /* Close subwindow */

  e = choices_show_subwindow(idb->self_id, CDSubNone);
  if (e) show_error_ret(e);

547 548 549 550 551 552 553 554 555 556 557
  #ifndef CREATE_ONLY_ONCE
    /* Delete all subwindow objects */

    choices_delete_subwindows();

    free(new_choices);
    new_choices = NULL;
    free(new_typefaces);
    new_typefaces = NULL;

    /* Deregister choices event handlers */
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
    event_deregister_toolbox_handler(-1, ECDRG2,                    choices_radio_click_handler,       NULL);
    event_deregister_toolbox_handler(-1, CDSet,                     choices_set_button_handler,        NULL);
    event_deregister_toolbox_handler(-1, CDCancel,                  choices_cancel_button_handler,     NULL);
    event_deregister_toolbox_handler(-1, CDSaveButton,              choices_save_button_handler,       NULL);
    event_deregister_toolbox_handler(-1, ECD_EncodingMenuBt,        choices_encoding_button_handler,   NULL);
    event_deregister_toolbox_handler(-1, ECD_ColourButton,          choices_colour_button_handler,     NULL);
    event_deregister_toolbox_handler(-1, ECD_HlDispBt,              choices_display_m_button_handler,  NULL);
    event_deregister_toolbox_handler(-1, ECD_HiDispBt,              choices_display_m_button_handler,  NULL);
    event_deregister_toolbox_handler(-1, ECD_HlSaveBt,              choices_save_m_button_handler,     NULL);
    event_deregister_toolbox_handler(-1, ECD_HiSaveBt,              choices_save_m_button_handler,     NULL);
    event_deregister_toolbox_handler(-1, ECDSaveMenuClick,          choices_save_m_click_handler,      NULL);
    event_deregister_toolbox_handler(-1, ECDDispMenuClick,          choices_display_m_click_handler,   NULL);
    event_deregister_toolbox_handler(-1, ECDHiRadioClick,           choices_history_radio_handler,     NULL);
    event_deregister_toolbox_handler(-1, OptionButton_StateChanged, choices_option_state_handler,      NULL);
    event_deregister_toolbox_handler(-1, ECD_FontButton,            choices_font_button_handler,       NULL);
    event_deregister_toolbox_handler(-1, ECD_ObjPlugBt,             choices_plug_m_button_handler,     NULL);
    event_deregister_toolbox_handler(-1, ECDPlugMenuClick,          choices_plug_m_click_handler,      NULL);
    event_deregister_toolbox_handler(-1, ECD_HiAgeTypeBt,           choices_timetype_m_button_handler, NULL);
    event_deregister_toolbox_handler(-1, ECDTimeMenuClick,          choices_timetype_m_click_handler,  NULL);
    event_deregister_toolbox_handler(-1, ECD_TabInnerBordBt,        choices_bord_m_button_handler,     NULL);
    event_deregister_toolbox_handler(-1, ECD_TabOuterBordBt,        choices_bord_m_button_handler,     NULL);
    event_deregister_toolbox_handler(-1, ECDBordMenuClick,          choices_bord_m_click_handler,      NULL);
580 581 582 583 584 585 586

    if ((window = choices_find_component(CD_FakePage)) != 0)
    {
      event_deregister_wimp_handler(window, Wimp_ERedrawWindow, choices_redraw_fakepage_handler, NULL);
    }
    event_deregister_wimp_handler(idb->self_id, Wimp_EOpenWindow, choices_open_choice_window, NULL);
  #endif
587 588

  choices_windowid = 0;
589
  current_subwindow = temp;
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605

  return 1;
}

/*************************************************/
/* choices_radio_click_handler()                 */
/*                                               */
/* Shows the appropriate subwindow when a radio  */
/* button in the choices dialogue is clicked on  */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

static int choices_radio_click_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
606
  int window, state;
607

608 609 610 611 612
  radiobutton_get_state(0, idb->self_id, idb->self_component, &state, NULL);

  /* Is this a radiobutton selected event? */

  if (state)
613
  {
614 615 616
    window = idb->self_component - CDFirstSubRadio;
    if (window > CDNoSubwindows) window = CDSubNone;
    choices_show_subwindow(idb->self_id, window);
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
  }

  return 1;
}

/*************************************************/
/* choices_set_contents()                        */
/*                                               */
/* Sets the contents of all choices subwindows   */
/* to reflect the current state of the global    */
/* choices.                                      */
/*                                               */
/* Returns:    pointer to _kernel_oserror.       */
/*************************************************/

632
static _kernel_oserror * choices_set_contents(void)
633
{
634
  ObjectId          window;
635
  unsigned int      temp, frac;
636

637
  /* No error handling here, as we want to allow items to be missing */
638

639
  if ((window = choices_find_component(CD_Homepage))       != 0) choices_writablefield_set_value(0, window, CD_Homepage,       new_choices->home_page);
640 641 642 643
  if ((window = choices_find_component(CD_UnderlineLinks)) != 0) optionbutton_set_state(0,  window, CD_UnderlineLinks, new_choices->underline_links);
  if ((window = choices_find_component(CD_UseDocColours))  != 0) optionbutton_set_state(0,  window, CD_UseDocColours,  new_choices->use_source_cols);
  if ((window = choices_find_component(CD_ShowForeground)) != 0) optionbutton_set_state(0,  window, CD_ShowForeground, new_choices->show_foreground);
  if ((window = choices_find_component(CD_ShowBackground)) != 0) optionbutton_set_state(0,  window, CD_ShowBackground, new_choices->show_background);
644 645

  /* Get encoding name */
646
  choices_set_encoding_field();
647 648 649 650 651 652 653 654 655 656 657 658 659

  if ((window = choices_find_component(CD_URLBar))         != 0) optionbutton_set_state(0, window, CD_URLBar,     new_choices->url_bar);
  if ((window = choices_find_component(CD_StatusBar))      != 0) optionbutton_set_state(0, window, CD_StatusBar,  new_choices->status_bar);
  if ((window = choices_find_component(CD_ButtonBar))      != 0) optionbutton_set_state(0, window, CD_ButtonBar , new_choices->button_bar);
  if ((window = choices_find_component(CD_FullScreen))     != 0) optionbutton_set_state(0, window, CD_FullScreen, new_choices->full_screen);

  if ((window = choices_find_component(CD_BackColour))     != 0) choices_colour_set_component(window, CD_BackColour, new_choices->background_colour);
  if ((window = choices_find_component(CD_TextColour))     != 0) choices_colour_set_component(window, CD_TextColour, new_choices->text_colour);
  if ((window = choices_find_component(CD_LinkColour))     != 0) choices_colour_set_component(window, CD_LinkColour, new_choices->link_colour);
  if ((window = choices_find_component(CD_UsedColour))     != 0) choices_colour_set_component(window, CD_UsedColour, new_choices->used_colour);
  if ((window = choices_find_component(CD_FolwColour))     != 0) choices_colour_set_component(window, CD_FolwColour, new_choices->followed_colour);
  if ((window = choices_find_component(CD_SlctColour))     != 0) choices_colour_set_component(window, CD_SlctColour, new_choices->selected_colour);

660 661
  if ((window = choices_find_component(CD_HlAutoOpen))     != 0) choices_numberrange_set_value(0,  window, CD_HlAutoOpen,   new_choices->auto_open_delay);
  if ((window = choices_find_component(CD_HlAutoScroll))   != 0) choices_numberrange_set_value(0,  window, CD_HlAutoScroll, new_choices->auto_scroll_delay);
662 663
  if ((window = choices_find_component(CD_HlDispDisp))     != 0) choices_set_display_field(window, CD_HlDispDisp,   new_choices->hotlist_show);
  if ((window = choices_find_component(CD_HlSaveDisp))     != 0) choices_set_save_field(   window, CD_HlSaveDisp,   new_choices->save_hotlist);
664

665 666
  if ((window = choices_find_component(CD_HiExpiryAge))    != 0) choices_set_expiry_age(window);
  if ((window = choices_find_component(CD_HiMaxSize))      != 0) choices_set_max_size(window);
667
  if ((window = choices_find_component(CD_HiDispDisp))     != 0) choices_set_display_field(window, CD_HiDispDisp, new_choices->show_urls);
668
  if ((window = choices_find_component(CD_HiSaveDisp))     != 0) choices_set_save_field(   window, CD_HiSaveDisp, new_choices->save_history);
669 670 671 672 673 674 675 676 677 678 679 680 681
  if ((window = choices_find_component(CD_HiImExpiryAge))  != 0) choices_set_im_expiry_age(window);
  if ((window = choices_find_component(CD_HiImMaxSize))    != 0) choices_set_im_max_size(window);

  if (history_radio)
  {
    if ((window = choices_find_component(CD_HiRadImage))    != 0)
    radiobutton_set_state(0, window, CD_HiRadImage, 1);
  }
  else
  {
    if ((window = choices_find_component(CD_HiRadPage))    != 0)
    radiobutton_set_state(0, window, CD_HiRadPage, 1);
  }
682 683 684 685

  if ((window = choices_find_component(CD_ClientPull))     != 0) optionbutton_set_state(0, window, CD_ClientPull,    new_choices->client_pull);
  if ((window = choices_find_component(CD_NetscapeEmu))    != 0) optionbutton_set_state(0, window, CD_NetscapeEmu,   new_choices->clone);
  if ((window = choices_find_component(CD_FramesSupport))  != 0) optionbutton_set_state(0, window, CD_FramesSupport, new_choices->support_frames);
686

687 688 689
  if ((window = choices_find_component(CD_FontsSystem))    != 0)
  {
    optionbutton_set_state(0, window, CD_FontsSystem, new_choices->system_font);
690
    choices_set_fonts_greyed(window, new_choices->system_font);
691 692
    /*set_gadget_state(window, CD_FontsGroup1,  new_choices->system_font);*/
  }
693 694 695
  if ((window = choices_find_component(CD_FontsTF1Disp))   != 0) choices_displayfield_set_value(0, window, CD_FontsTF1Disp, new_typefaces[0].fontnames[0]);
  if ((window = choices_find_component(CD_FontsTF2Disp))   != 0) choices_displayfield_set_value(0, window, CD_FontsTF2Disp, new_typefaces[1].fontnames[0]);
  if ((window = choices_find_component(CD_FontsTF3Disp))   != 0) choices_displayfield_set_value(0, window, CD_FontsTF3Disp, new_typefaces[2].fontnames[0]);
696

697 698
  temp = new_choices->font_size;
  frac = (temp & 0xf);
699 700
  temp = ((temp >> 4) * 10) + todecimal[frac];

701
  if ((window = choices_find_component(CD_FontsSize))      != 0) choices_numberrange_set_value(0,  window, CD_FontsSize, temp);
702
  if ((window = choices_find_component(CD_FontsAspect))    != 0) choices_numberrange_set_value(0,  window, CD_FontsAspect, new_choices->tt_aspect);
703

704
  if ((window = choices_find_component(CD_ObjHandle))      != 0) optionbutton_set_state(0,  window, CD_ObjHandle, new_choices->support_object);
705 706
  if ((window = choices_find_component(CD_ObjPlugDisp))    != 0)
  {
707 708 709 710
    set_gadget_state(window, CD_ObjPlugLabl, !new_choices->support_object);
    set_gadget_state(window, CD_ObjPlugBt,   !new_choices->support_object);
    set_gadget_state(window, CD_ObjPlugDisp, !new_choices->support_object);
    choices_set_plugin_field();
711 712
  }

713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
  if ((window = choices_find_component(CD_TabSupport))     != 0)
  {
    optionbutton_set_state(0,  window, CD_TabSupport, new_choices->support_tables);
    choices_set_tables_greyed(!new_choices->support_tables,window);
  }

  if ((window = choices_find_component(CD_TabInnerBordDisp)) != 0)
  {
    choices_set_table_border_field(window, CD_TabInnerBordDisp, new_choices->table_inner);
  }

  if ((window = choices_find_component(CD_TabOuterBordDisp)) != 0)
  {
    choices_set_table_border_field(window, CD_TabOuterBordDisp, new_choices->table_outer);
  }

David Brown's avatar
David Brown committed
729 730 731
  if ((window = choices_find_component(CD_NetUseProxy))     != 0) optionbutton_set_state(0,  window, CD_NetUseProxy, new_choices->use_proxy);
  if ((window = choices_find_component(CD_NetProxyAddr))    != 0)
  {
732 733 734
    set_gadget_state(window, CD_NetProxyAddr, !new_choices->use_proxy);
    set_gadget_state(window, CD_NetProxyLabl, !new_choices->use_proxy);
    choices_writablefield_set_value(0, window, CD_NetProxyAddr, new_choices->proxy_address);
David Brown's avatar
David Brown committed
735 736
  }

737 738 739 740 741 742 743 744 745 746 747
  return NULL;
}

/*************************************************/
/* choices_get_contents()                        */
/*                                               */
/* Sets the state of the global choices to       */
/* reflect the contents of all the choices       */
/* subwindows.                                   */
/*************************************************/

748
static _kernel_oserror * choices_get_contents(void)
749
{
750 751 752
  _kernel_oserror         * e;
  WimpGetWindowStateBlock   s;
  browser_data            * b;
753
  ObjectId                  window;
754 755
  char                    * tempstring;
  int                       reqsize;
756
  fm_typeface             * tfptr;
757 758 759
  int                       serif_changed = 0;
  int                       sans_changed  = 0;
  int                       fixed_changed = 0;
760
  int                       needs_redraw  = 0;
761
  int                       old_font_size, old_system_font, frac;
762
  int                       temp;
763

764 765
  /* The lack of error checking is to allow */
  /* items to be missing from the dialogue. */
766

767
  if ((window = choices_find_component(CD_Homepage)) != 0)
768
  {
769
    e = writablefield_get_value(0, window, CD_Homepage, NULL, 0, &reqsize);
770

771
    if (!e)
772
    {
773 774 775
      tempstring = malloc(reqsize + 1);

      if (tempstring)
776
      {
777
        e = writablefield_get_value(0, window, CD_Homepage, tempstring, reqsize, NULL);
778 779 780 781 782 783 784 785 786 787 788 789 790

        if (!e)
        {
          if (!strcmp(tempstring, new_choices->home_page))
          {
            free(tempstring);
          }
          else
          {
            free(new_choices->home_page);
            new_choices->home_page = tempstring;
          }
        }
791 792 793
      }
      else
      {
794
        show_error_ret(make_no_memory_error(17));
795 796
      }
    }
797 798
  }

799 800
  if ((window = choices_find_component(CD_HlAutoOpen))     != 0) numberrange_get_value(0, window, CD_HlAutoOpen,   &new_choices->auto_open_delay);
  if ((window = choices_find_component(CD_HlAutoScroll))   != 0) numberrange_get_value(0, window, CD_HlAutoScroll, &new_choices->auto_scroll_delay);
801

802 803
  if (expiry_age_greyed)
  {
804 805
    if (history_radio) new_choices->image_expiry_age = 0;
    else               new_choices->expiry_age = 0;
806 807 808 809 810 811
  }
  else
  {
    if ((window = choices_find_component(CD_HiExpiryAge))    != 0)
    {
      numberrange_get_value(0, window, CD_HiExpiryAge,  &temp);
812 813
      if (history_radio) new_choices->image_expiry_age = choices_typed_time_to_seconds(temp, history_expiry_units);
      else               new_choices->expiry_age = choices_typed_time_to_seconds(temp, history_expiry_units);
814 815 816 817 818
    }
  }

  if (max_size_greyed)
  {
819 820
    if (history_radio) new_choices->image_max_size = 0;
    else               new_choices->max_size = 0;
821 822 823 824 825 826
  }
  else
  {
    if ((window = choices_find_component(CD_HiMaxSize))      != 0)
    {
      numberrange_get_value(0, window, CD_HiMaxSize,    &temp);
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
      if (history_radio) new_choices->image_max_size = temp * 1024;
      else               new_choices->max_size = temp * 1024;
    }
  }

  if (image_expiry_age_greyed)
  {
    new_choices->image_expiry_age = 0;
  }
  else
  {
    if ((window = choices_find_component(CD_HiImExpiryAge))    != 0)
    {
      numberrange_get_value(0, window, CD_HiImExpiryAge,  &temp);
      new_choices->image_expiry_age = choices_typed_time_to_seconds(temp, image_expiry_units);
842 843
    }
  }
844

845 846 847 848 849 850 851 852 853 854 855 856 857 858
  if (image_max_size_greyed)
  {
    new_choices->image_max_size = 0;
  }
  else
  {
    if ((window = choices_find_component(CD_HiImMaxSize))      != 0)
    {
      numberrange_get_value(0, window, CD_HiImMaxSize,    &temp);
      new_choices->image_max_size = temp * 1024;
    }
  }


David Brown's avatar
David Brown committed
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 884 885 886 887 888 889 890
  if ((window = choices_find_component(CD_NetProxyAddr)) != 0)
  {
    e = writablefield_get_value(0, window, CD_NetProxyAddr, NULL, 0, &reqsize);

    if (!e)
    {
      tempstring = malloc(reqsize + 1);

      if (tempstring)
      {
        e = writablefield_get_value(0, window, CD_NetProxyAddr, tempstring, reqsize, NULL);

        if (!e)
        {
          if (!strcmp(tempstring, new_choices->proxy_address))
          {
            free(tempstring);
          }
          else
          {
            free(new_choices->proxy_address);
            new_choices->proxy_address = tempstring;
          }
        }
      }
      else
      {
        show_error_ret(make_no_memory_error(17));
      }
    }
  }

891
  old_font_size = choices.font_size;
892 893
  old_system_font = choices.system_font;
  if ((window = choices_find_component(CD_FontsSize)) != 0)
David Brown's avatar
David Brown committed
894 895 896 897 898
  {
    numberrange_get_value(0, window, CD_FontsSize, &new_choices->font_size);
    frac = new_choices->font_size % 10;
    new_choices->font_size = ((new_choices->font_size / 10)<<4) + fromdecimal[frac];
  }
899

900 901 902 903 904 905
  if ((window = choices_find_component(CD_FontsAspect)) != 0)
  {
    numberrange_get_value(0, window, CD_FontsAspect, &new_choices->tt_aspect);
    if (choices.tt_aspect != new_choices->tt_aspect) fixed_changed = 1;
  }

906 907 908 909 910 911 912 913 914 915
  /* Do we need to redraw because certain special items have changed? */

  if (
       choices.table_inner != new_choices->table_inner ||
       choices.table_outer != new_choices->table_outer
     )
     needs_redraw = 1;

  /* Now start using the dialogue's new settings */

916 917
  memcpy(&choices, new_choices, sizeof(global_choices));

918 919 920
  /* Before we change fonts, we need to forget the basic */
  /* typeface definitions. First find out which have     */
  /* changed, if any.                                    */
921 922

  tfptr = fm_find_typeface("serif");
923

924 925
  if (tfptr)
  {
926
    if (memcmp(tfptr, &new_typefaces[0], sizeof(fm_typeface))) serif_changed = 1;
927
  }
928

929
  tfptr = fm_find_typeface("sans");
930

931 932
  if (tfptr)
  {
933
    if (memcmp(tfptr, &new_typefaces[1], sizeof(fm_typeface))) sans_changed = 1;
934
  }
935

936
  tfptr = fm_find_typeface("fixed");
937

938 939
  if (tfptr)
  {
940
    if (memcmp(tfptr, &new_typefaces[2], sizeof(fm_typeface))) fixed_changed = 1;
941 942
  }

943 944 945
  if (choices.system_font) choices.font_size = FM_Standard_Size;

  if (choices.font_size != old_font_size || choices.system_font != old_system_font)
946 947 948 949
  {
    serif_changed = 1;
    sans_changed  = 1;
    fixed_changed = 1;
950
    fm_set_system_font(choices.system_font);
951 952 953
    fm_init_sizes(choices.font_size);
  }

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
  /* If fonts have changed, get ready to update them */

  if (serif_changed || sans_changed || fixed_changed)
  {
    /* Dump *all* current fonts */

    fm_shutdown();

    /* Write the new typeface details into the existing structures */

    tfptr = fm_find_typeface("serif");
    if (tfptr) memcpy(tfptr, &new_typefaces[0], sizeof(fm_typeface));

    tfptr = fm_find_typeface("sans");
    if (tfptr) memcpy(tfptr, &new_typefaces[1], sizeof(fm_typeface));

    tfptr = fm_find_typeface("fixed");
    if (tfptr) memcpy(tfptr, &new_typefaces[2], sizeof(fm_typeface));
  }

  /* Update browsers - *must* call fm_lose_fonts for all */
  /* browsers if the above code called fm_shutdown.      */
976 977 978 979 980

  b = last_browser;

  while (b)
  {
981
    if (serif_changed || sans_changed || fixed_changed)
982
    {
983 984 985
      /* Get rid of the currently used fonts for this browser */

      fm_lose_fonts(b);
986

987
      /* Only reformat page if it has no children */
988

989 990 991
      if (!b->children) reformat_format_from(b, -1, 1, -1);
    }
    else
992
    {
993 994 995 996
      if (b->background_colour == -1)
      {
        b->antialias_colour = redraw_backcol(b);
      }
997

998 999 1000 1001
      /* The Choices are only used directly for browsers which are */
      /* set to override document colours. Otherwise defaults will */
      /* have been read locally and possibly overridden by HTML,   */
      /* and a reload will be needed to reflect the changes.       */
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
      /*                                                           */
      /* Alternatively, it may be the case that all browsers need  */
      /* redrawing (e.g. if the table border types change).        */

      if (
           needs_redraw ||
           (
             !b->use_source_cols ||
             (
               b->background_colour == -1 &&
               b->background_image  == -1
             )
           )
         )
1016
      {
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
        s.window_handle = b->window_handle;
        RetError(wimp_get_window_state(&s));

        coords_box_toworkarea(&s.visible_area, (WimpRedrawWindowBlock *) &s);

        RetError(wimp_force_redraw(b->window_handle,
                                   s.visible_area.xmin,
                                   s.visible_area.ymin,
                                   s.visible_area.xmax,
                                   s.visible_area.ymax));

        if (b->background_colour == -1)
        {
          b->antialias_colour = choices.background_colour;
        }
1032
      }
1033 1034 1035 1036 1037
    }

    b = b->previous;
  }

1038 1039 1040 1041 1042 1043 1044 1045
  if (serif_changed || sans_changed || fixed_changed)
  {
    /* Reclaim basic typefaces - remember to claim at the *new* */
    /* font Choices size.                                       */

    fm_claim_basic_typefaces(choices.font_size);
  }

1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
  return NULL;
}

/*************************************************/
/* choices_set_button_handler()                  */
/*                                               */
/* Called when the set button in the main        */
/* choices dialogue is clicked.                  */
/* Uses choices_get_contents to set the global   */
/* choices to reflect the choices set in the     */
/* choices subwindows                            */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

static int choices_set_button_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  _kernel_oserror *e = NULL;
1065 1066 1067 1068
  int             buttons;

  ChkError(window_get_pointer_info(0, NULL, NULL, &buttons, NULL, NULL));

1069 1070 1071
  e = choices_get_contents();
  if (e) show_error_ret(e);

1072 1073 1074 1075 1076
  if (buttons != 1)
  {
    ChkError(toolbox_hide_object(0, choices_windowid));
  }

1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
  return 1;
}

/*************************************************/
/* choices_cancel_button_handler()               */
/*                                               */
/* Called when the cancel button in the main     */
/* choices dialogue is clicked.                  */
/* Uses choices_set_contents to restore the      */
/* subwindows to their previous state.           */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

static int choices_cancel_button_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
1094
  ObjectId        window;
1095
  int             update_fake;
1096
  fm_typeface   * tfptr;
1097
  int             buttons;
1098

1099 1100 1101
  ChkError(window_get_pointer_info(0, NULL, NULL, &buttons, NULL, NULL));

  if (buttons == 1)
1102
  {
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
    if ((choices.background_colour != new_choices->background_colour) ||
        (choices.text_colour       != new_choices->text_colour)       ||
        (choices.link_colour       != new_choices->link_colour)       ||
        (choices.used_colour       != new_choices->used_colour)       ||
        (choices.followed_colour   != new_choices->followed_colour)   ||
        (choices.selected_colour   != new_choices->selected_colour)   ||
        (choices.underline_links   != new_choices->underline_links))
    {
      update_fake = 1;
    }
    else
    {
      update_fake = 0;
    }
1117

1118
    memcpy(new_choices, &choices, sizeof(global_choices));
1119

1120 1121 1122 1123 1124 1125
    tfptr = fm_find_typeface("serif");
    memcpy(&new_typefaces[0], tfptr, sizeof(fm_typeface));
    tfptr = fm_find_typeface("sans");
    memcpy(&new_typefaces[1], tfptr, sizeof(fm_typeface));
    tfptr = fm_find_typeface("fixed");
    memcpy(&new_typefaces[2], tfptr, sizeof(fm_typeface));
1126

1127
    choices_set_contents();
1128

1129
    /* Attempt to redraw fake page display */
1130

1131 1132 1133 1134 1135 1136
    if (update_fake && (window = choices_find_component(CD_FakePage)) != 0) button_set_flags(0, window, CD_FakePage, 0, 0);
  }
  else
  {
    ChkError(toolbox_hide_object(0, choices_windowid));
  }
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157

  return 1;
}

/*************************************************/
/* choices_save_button_handler()                 */
/*                                               */
/* Called when the set button in the main        */
/* choices dialogue is clicked.                  */
/* Uses choices_get_contents to set the global   */
/* choices to reflect the choices set in the     */
/* choices subwindows and then saves the choices */
/* file.                                         */
/*                                               */
/* Parameters are as standard for a Toolbox      */
/* event handler.                                */
/*************************************************/

static int choices_save_button_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  _kernel_oserror *e = NULL;
1158
  ObjectId        window;
1159 1160 1161
  int             buttons;

  ChkError(window_get_pointer_info(0, NULL, NULL, &buttons, NULL, NULL));
1162

1163 1164 1165 1166 1167 1168 1169
  e = choices_get_contents();
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

1170 1171 1172 1173
  /* Attempt to redraw fake page display */

  if ((window = choices_find_component(CD_FakePage)) != 0) button_set_flags(0, window, CD_FakePage, 0, 0);

1174 1175 1176
  e = save_save_choices(NULL);
  if (e) show_error_ret(e);

1177 1178 1179 1180 1181
  if (buttons != 1)
  {
    ChkError(toolbox_hide_object(0, choices_windowid));
  }

1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
  return 1;
}

/*************************************************/
/* choices_set_encoding_field()                  */
/*                                               */
/* Sets the encoding display field appropriately */
/* to reflect the current state of               */
/* new_choices->encoding                         */
/*************************************************/

_kernel_oserror * choices_set_encoding_field(void)
{
  _kernel_oserror *e;
1196
  ObjectId    objid, destwind;
1197 1198 1199 1200
  ComponentId compid;
  char *textptr;
  int sizereqd;

1201 1202 1203 1204 1205
  /* Find which window the encoding display is in */

  destwind = choices_find_component(CD_EncodingDisply);
  if (!destwind) return NULL;

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
  /* Find menu item which contains encoding name */

  if (encoding_get_encoding_item(new_choices->encoding, &objid, &compid))
  {
    RetError(menu_get_entry_text(0, objid, compid, NULL, 0, &sizereqd));

    /* Add 1 to the buffer size just incase the sizereqd field is returned as string */
    /* length rather than the buffer size required.  Typical paranoid precautions    */
    /* when using the toolbox.                                                       */

    textptr = malloc(sizereqd+1);

    if (textptr)
    {
      e = menu_get_entry_text(0, objid, compid, textptr, sizereqd+1, NULL);
      if (e)
      {
        free(textptr);
        return e;
      }
1226 1227

      e = choices_displayfield_set_value(0, destwind, CD_EncodingDisply, textptr);
1228 1229

      free(textptr);
1230
      return e;
1231 1232 1233
    }
    else
    {
1234
      RetError(make_no_memory_error(18));
1235 1236 1237 1238 1239 1240
    }
  }
  else
  {
    /* The encoding name could not be found in the menu structure */

1241
    RetError(choices_displayfield_set_value(0, destwind, CD_EncodingDisply, "Unknown encoding")); //
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
  }

  return NULL;
}

/*************************************************/
/* choices_encoding_button_handler()             */
/*                                               */
/* Opens the encoding window with appropriate    */
/* values for the choices dialogue.              */
/*************************************************/

static int choices_encoding_button_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  _kernel_oserror *e;
  int  position[2];
  BBox box;
  WimpGetWindowStateBlock state;

  e = gadget_get_bbox(0, idb->self_id, idb->self_component, &box);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = window_get_wimp_handle(0, idb->self_id, &state.window_handle);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = wimp_get_window_state(&state);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  /* Set open coordinates of encoding menu to top left of encoding button */

  position[0] = coords_x_toscreen(box.xmax, (WimpRedrawWindowBlock*)&state);
  position[1] = coords_y_toscreen(box.ymax, (WimpRedrawWindowBlock*)&state);

  show_error_ret(toolbox_show_object(Toolbox_ShowObject_AsMenu,
                                     encoding_get_menuid(),
                                     Toolbox_ShowObject_TopLeft,
                                     position,
                                     choices_windowid,
                                     NULL));

  return 1;
}

/*************************************************/
/* choices_colour_button_handler()               */
/*                                               */
/* Opens a colour dialogue box with appropriate  */
/* settings for the current colour.              */
/*************************************************/

static int choices_colour_button_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  _kernel_oserror *e;
  int  position[2];
  BBox box;
  WimpGetWindowStateBlock state;
  int colour[2];

1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
  /* Is the ColourDbox module in RMA? */

  e = _swix(OS_Module, _INR(0,1), 18, "ColourDbox");

  /* No, well try and load it from system */

  if (e)
  {
    e = _swix(OS_Module, _INR(0,1), 1, "System:Modules.Toolbox.ColourDbox");
  }

  /* That didn't work so try reinitialising it */

  if (e)
  {
    e = _swix(OS_Module, _INR(0,1), 3, "ColourDbox");
  }

  /* That didn't work either!  So it's tough you can't change the colours */

  if (e)
  {
    show_error_ret(e);
    return 1;
  }

1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
  switch(idb->self_component)
  {
    case CD_BackColourBt: colour[0] = new_choices->background_colour; break;
    case CD_TextColourBt: colour[0] = new_choices->text_colour;       break;
    case CD_LinkColourBt: colour[0] = new_choices->link_colour;       break;
    case CD_UsedColourBt: colour[0] = new_choices->used_colour;       break;
    case CD_FolwColourBt: colour[0] = new_choices->followed_colour;   break;
    case CD_SlctColourBt: colour[0] = new_choices->selected_colour;   break;
    default:
    return 1;
    break;
  }

  colour[1] = 0;

  e = gadget_get_bbox(0, idb->self_id, idb->self_component, &box);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = window_get_wimp_handle(0, idb->self_id, &state.window_handle);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = wimp_get_window_state(&state);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  /* Set open coordinates of encoding menu to top left of encoding button */

  position[0] = coords_x_toscreen(box.xmax, (WimpRedrawWindowBlock*)&state);
  position[1] = coords_y_toscreen(box.ymax, (WimpRedrawWindowBlock*)&state);

  e = toolbox_create_object(0, "ColourDbox", &colourdbox_id);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = colourdbox_set_colour(0, colourdbox_id, colour);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  show_error_ret(toolbox_show_object(Toolbox_ShowObject_AsMenu,
                                     colourdbox_id,
                                     Toolbox_ShowObject_TopLeft,
                                     position,
                                     idb->self_id,
                                     idb->self_component));

  e = event_register_toolbox_handler(colourdbox_id, ColourDbox_DialogueCompleted, choices_colour_closed_handler, NULL);
  if (e) {show_error_ret(e); return 1;}

  e = event_register_toolbox_handler(colourdbox_id, ColourDbox_ColourSelected, choices_colour_selected_handler, NULL);
  if (e) {show_error_ret(e); return 1;}

  return 1;
}

/*************************************************/
/* choices_colour_closed_handler()               */
/*                                               */
/* Called when the colour dialogue box is closed */
/* deregisters all events attached to it and     */
/* deletes the dbox object.                      */
/*************************************************/

static int choices_colour_closed_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  _kernel_oserror *e;

  e = event_deregister_toolbox_handler(idb->self_id, ColourDbox_DialogueCompleted, choices_colour_closed_handler, NULL);
  if (e) {show_error_ret(e); return 1;}

  e = event_deregister_toolbox_handler(colourdbox_id, ColourDbox_ColourSelected, choices_colour_selected_handler, NULL);
  if (e) {show_error_ret(e); return 1;}

  e = toolbox_delete_object(0, idb->self_id);
  if (e) {show_error_ret(e); return 1;}

  #ifdef TRACE
1431
    if (tl & (1u<<29)) Printf("choices_colour_closed_handler: Colour DBox deleted\n");
1432 1433 1434 1435 1436 1437
  #endif

  return 1;
}

/*************************************************/
1438
/* choices_colour_selected_handler()             */
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
/*                                               */
/* Called when the a colour is selected in the   */
/* colour dialogue box.                          */
/* Sets appropriate fields in the new_choices    */
/* structure and updates the window to reflect   */
/* the new colour chosen.                        */
/*************************************************/

static int choices_colour_selected_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  ColourDboxColourSelectedEvent *cevent = (ColourDboxColourSelectedEvent*)event;

  switch(idb->parent_component)
  {
    case CD_BackColourBt: new_choices->background_colour = cevent->colour_block[0]; break;
    case CD_TextColourBt: new_choices->text_colour       = cevent->colour_block[0]; break;
    case CD_LinkColourBt: new_choices->link_colour       = cevent->colour_block[0]; break;
    case CD_UsedColourBt: new_choices->used_colour       = cevent->colour_block[0]; break;
    case CD_FolwColourBt: new_choices->followed_colour   = cevent->colour_block[0]; break;
    case CD_SlctColourBt: new_choices->selected_colour   = cevent->colour_block[0]; break;
    default:
    return 1;
    break;
  }

1464 1465 1466 1467 1468 1469 1470
  /* Don't bother with errors as colour display field might not exist */

  choices_colour_set_component(idb->parent_id, idb->parent_component-1, cevent->colour_block[0]);

  /* Don't bother with errors as fake page display might not exist */

  button_set_flags(0, idb->parent_id, CD_FakePage, 0, 0);
1471 1472 1473 1474 1475

  return 1;
}

/*************************************************/
1476
/* choices_colour_set_component()                */
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
/*                                               */
/* Sets the validation string of a button to be  */
/* slabbed in and have the background colour     */
/* specified.                                    */
/*                                               */
/* Parameters: window ObjectId                   */
/*                                               */
/*             button ComponentId                */
/*                                               */
/*             colour                            */
/*************************************************/

static _kernel_oserror *choices_colour_set_component(ObjectId window, ComponentId component, int colour)
{
1491 1492 1493
  char            newvalidation[32];
  unsigned char * newcol;

1494 1495
  newcol = (unsigned char*)&colour;

1496
  sprintf(newvalidation, "R2;C/%02.2x%02.2x%02.2x", *(newcol + 3), *(newcol + 2), *(newcol + 1));
1497

1498
  return choices_button_set_validation(0, window, component, newvalidation);
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
}

/*************************************************/
/* choices_display_button_handler()              */
/*                                               */
/* Opens the display menu with a value           */
/* appropriate to either the hotlist or history. */
/*************************************************/

static int choices_display_m_button_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  _kernel_oserror *e;
  int  position[2];
  BBox box;
  WimpGetWindowStateBlock state;
  ObjectId display;
  int new_tick;

  e = gadget_get_bbox(0, idb->self_id, idb->self_component, &box);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = window_get_wimp_handle(0, idb->self_id, &state.window_handle);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = wimp_get_window_state(&state);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  /* Set open coordinates of display menu to top left of display button */

  position[0] = coords_x_toscreen(box.xmax, (WimpRedrawWindowBlock*)&state);
  position[1] = coords_y_toscreen(box.ymax, (WimpRedrawWindowBlock*)&state);

  e = toolbox_create_object(0, "ChDisplay", &display);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  switch(idb->self_component)
  {
    case CD_HlDispBt: new_tick = new_choices->hotlist_show; break;
    case CD_HiDispBt: new_tick = new_choices->show_urls;    break;
    default: new_tick = 0; break;
  }

  if (disp_ticked != new_tick)
  {
    if (disp_ticked != -1) menu_set_tick(0, display, disp_ticked, 0);
    menu_set_tick(0, display, new_tick, 1);
    disp_ticked = new_tick;
  }

  show_error_ret(toolbox_show_object(Toolbox_ShowObject_AsMenu,
                                     display,
                                     Toolbox_ShowObject_TopLeft,
                                     position,
                                     idb->self_id,
                                     idb->self_component));

  return 1;
}

/*************************************************/
1575
/* choices_save_m_button_handler()               */
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
/*                                               */
/* Opens the save menu with a value appropriate  */
/* to either the hotlist or history.             */
/*************************************************/

static int choices_save_m_button_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  _kernel_oserror *e;
  int  position[2];
  BBox box;
  WimpGetWindowStateBlock state;
  ObjectId display;
  int new_tick;

  e = gadget_get_bbox(0, idb->self_id, idb->self_component, &box);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = window_get_wimp_handle(0, idb->self_id, &state.window_handle);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = wimp_get_window_state(&state);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  /* Set open coordinates of display menu to top left of display button */

  position[0] = coords_x_toscreen(box.xmax, (WimpRedrawWindowBlock*)&state);
  position[1] = coords_y_toscreen(box.ymax, (WimpRedrawWindowBlock*)&state);

  e = toolbox_create_object(0, "ChSave", &display);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  switch(idb->self_component)
  {
    case CD_HlSaveBt: new_tick = new_choices->save_hotlist; break;
    case CD_HiSaveBt: new_tick = new_choices->save_history; break;
    default: new_tick = 0; break;
  }

  if (save_ticked != new_tick)
  {
    if (save_ticked != -1) menu_set_tick(0, display, save_ticked, 0);
    menu_set_tick(0, display, new_tick, 1);
    save_ticked = new_tick;
  }

  show_error_ret(toolbox_show_object(Toolbox_ShowObject_AsMenu,
                                     display,
                                     Toolbox_ShowObject_TopLeft,
                                     position,
                                     idb->self_id,
                                     idb->self_component));

  return 1;
}

/*************************************************/
/* choices_set_save_field()                      */
/*                                               */
/* Sets the passed save display field            */
/* appropriately to reflect the passed state.    */
/*************************************************/

static _kernel_oserror * choices_set_save_field(ObjectId obj, ComponentId comp, int state)
{
  _kernel_oserror *e;
  char *tempstring;

  #ifdef TRACE
1660
    if (tl & (1u<<29)) Printf("choices_set_display_field: Called\n");
1661 1662
  #endif

1663
  RetError(choices_get_menu_entry_text("ChSave", state, &tempstring));
1664

1665
  e = choices_displayfield_set_value(0, obj, comp, tempstring);
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684

  free(tempstring);

  return e;
}

/*************************************************/
/* choices_set_display_field()                   */
/*                                               */
/* Sets the passed display display field         */
/* appropriately to reflect the passed state.    */
/*************************************************/

static _kernel_oserror * choices_set_display_field(ObjectId obj, ComponentId comp, int state)
{
  _kernel_oserror *e;
  char *tempstring;

  #ifdef TRACE
1685
    if (tl & (1u<<29)) Printf("choices_set_display_field: Called\n");
1686 1687
  #endif

1688
  RetError(choices_get_menu_entry_text("ChDisplay", state, &tempstring));
1689

1690
  e = choices_displayfield_set_value(0, obj, comp, tempstring);
1691 1692 1693 1694 1695 1696 1697 1698 1699

  free(tempstring);

  return e;
}

/*************************************************/
/* choices_display_m_click_handler()             */
/*                                               */
1700 1701 1702 1703 1704 1705 1706
/* Called when there is a selection in the       */
/* display menu.  Sets the selected menu item to */
/* be ticked, unticks the previously ticked      */
/* entry sets appropriate new_choices field and  */
/* updates the display field of the component    */
/* with component number 1 less than the menus   */
/* parent.                                       */
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
/*************************************************/

static int choices_display_m_click_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  if (disp_ticked != idb->self_component)
  {
    if (disp_ticked != -1) menu_set_tick(0, idb->self_id, disp_ticked, 0);
    menu_set_tick(0, idb->self_id, idb->self_component, 1);
    disp_ticked = idb->self_component;
  }

  switch(idb->parent_component)
  {
    case CD_HlDispBt:
    new_choices->hotlist_show = idb->self_component;
    break;
    case CD_HiDispBt:
    new_choices->show_urls    = idb->self_component;
    break;
  }

  choices_set_display_field(idb->parent_id, idb->parent_component - 1, idb->self_component);

  return 1;
}

/*************************************************/
/* choices_save_m_click_handler()                */
/*                                               */
1736 1737 1738 1739 1740 1741 1742
/* Called when there is a selection in the save  */
/* menu.  Sets the selected menu item to be      */
/* ticked, unticks the previously ticked entry   */
/* sets appropriate new_choices field and        */
/* updates the display field of the component    */
/* with component number 1 less than the menus   */
/* parent.                                       */
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
/*************************************************/

static int choices_save_m_click_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  if (save_ticked != idb->self_component)
  {
    if (save_ticked != -1) menu_set_tick(0, idb->self_id, save_ticked, 0);
    menu_set_tick(0, idb->self_id, idb->self_component, 1);
    save_ticked = idb->self_component;
  }

  switch(idb->parent_component)
  {
    case CD_HlSaveBt:
    new_choices->save_hotlist = idb->self_component;
    break;
    case CD_HiSaveBt:
    new_choices->save_history = idb->self_component;
    break;
  }

  choices_set_save_field(idb->parent_id, idb->parent_component - 1, idb->self_component);

  return 1;
}
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825

/*************************************************/
/* choices_draw_string()                         */
/*                                               */
/* Writes a string on the screen using the       */
/* passed font handle, it underlines the text if */
/* required.  It also returns the position to    */
/* paint the next string following this one.     */
/*                                               */
/* Parameters: font handle                       */
/*                                               */
/*             string to print                   */
/*                                               */
/*             xposition on screen (os coords)   */
/*                                               */
/*             yposition on screen (os coords)   */
/*                                               */
/*             0     - don't underline           */
/*             non 0 - underline                 */
/*                                               */
/*             foreground colour                 */
/*                                               */
/*             background colour                 */
/*                                               */
/*             pointer to int to return x        */
/*             position in                       */
/*************************************************/

static _kernel_oserror * choices_draw_string(fm_face h,
                                             char *string,
                                             int xpos,
                                             int ypos,
                                             int underline,
                                             int forecolour,
                                             int backcolour,
                                             int *newxpos)
{
  int width, nochars;
  fm_set_font_colour(h, forecolour, backcolour);
  fm_get_string_width(h, string, 0x1000000, strlen(string), -1, &nochars, &width);
  convert_to_os(width, &width);

  /* The next string must be plotted straight after this string */

  *newxpos = xpos + width;

  fm_puts(h, xpos, ypos, string, 1, 0);

  if (underline)
  {
    redraw_set_colour(forecolour);
    bbc_move(xpos, ypos - 7);
    bbc_draw(*newxpos, ypos - 7);
  }
  return NULL;
}

/*************************************************/
David Brown's avatar
David Brown committed
1826
/* choices_write_rubbish()                       */
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
/*                                               */
/* Writes a string of readable rubbish on the    */
/* screen using the passed font handle.  It also */
/* returns the position to paint the next string */
/* following this one.                           */
/*                                               */
/* Parameters: font handle                       */
/*                                               */
/*             xposition on screen (os coords)   */
/*                                               */
/*             yposition on screen (os coords)   */
/*                                               */
/*             Approximate x position to stop    */
/*             printing at (os coords)           */
/*                                               */
/*             foreground colour                 */
/*                                               */
/*             background colour                 */
/*                                               */
/*             pointer to int to return x        */
/*             position in                       */
/*                                               */
/*             Random seed for string to print   */
/*             (If you pass the same seed the    */
/*             string should be the same)        */
/*************************************************/

David Brown's avatar
David Brown committed
1854
static _kernel_oserror * choices_write_rubbish(fm_face h,
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
                                            int xpos,
                                            int ypos,
                                            int maxxish,
                                            int forecolour,
                                            int backcolour,
                                            int *newxpos,
                                            int seed)
{
  char ministring[5];
  int chars = 0;
  int spcwidth, width, nochars;

  fm_set_font_colour(h, forecolour, backcolour);

  srand(seed);

  fm_get_string_width(h, " ", 0x1000000, 1, -1, &nochars, &width);
  convert_to_os(width, &spcwidth);
  xpos += spcwidth;

  while(xpos < maxxish)
  {
1877 1878
    ministring[0] = consonant[rand()%(sizeof(consonant)-1)];
    ministring[1] = vowel[rand()%(sizeof(vowel)-1)];
1879 1880 1881

    if (rand()%2)
    {
1882 1883
      ministring[2] = consonant[rand()%(sizeof(consonant)-1)];
      ministring[3] = vowel[rand()%(sizeof(vowel)-1)];
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
      ministring[4] = 0;
    }
    else
    {
      ministring[2] = 0;
    }

    fm_puts(h, xpos, ypos, ministring, 1, 0);
    fm_get_string_width(h, ministring, 0x1000000, strlen(ministring), -1, &nochars, &width);
    convert_to_os(width, &width);
    xpos += width;

    chars++;
    if (chars > rand()%2 || xpos >= maxxish)
    {
      chars = 0;
      xpos += spcwidth;
    }
  }

  *newxpos = xpos;
  return NULL;
}

/*************************************************/
/* choices_redraw_fakepage_handler()             */
/*                                               */
/* Redraws the fake page in the colour selection */
/* dbox.                                         */
/*************************************************/

static int choices_redraw_fakepage_handler(int eventcode, WimpPollBlock * event, IdBlock * idb, void * handle)
{
  _kernel_oserror       * e = NULL;
  WimpRedrawWindowBlock   block;
  WimpGetWindowStateBlock state;
1920
  int                     more, ypos, xtarget, ptsize, gadsize, xstart, nolines;
1921 1922 1923 1924
  int                     fontheight = 0;
  int                     gotfont    = 0;
  BBox                    icon_coords, fbox;
  fm_face                 h = 0;
1925
  char                    display_this[4];
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950

  block.window_handle = event->redraw_window_request.window_handle;
  state.window_handle = event->redraw_window_request.window_handle;

  e = wimp_get_window_state(&state);
  show_error_ret(e);
  if (!e) e = gadget_get_bbox(0, idb->self_id, CD_FakePage, &icon_coords);

  gadsize = icon_coords.ymax - icon_coords.ymin;

  xstart = gadsize / 8;

  if (e)
  {
    /* Gadget doesn't exist or something strange happened trying */
    /* to get window state so just do a simple redraw loop to    */
    /* keep the wimp happy.                                      */

    ChkError(wimp_redraw_window(&block, &more));
    while (more && !e) e = wimp_get_rectangle(&block, &more);
    return 1;
  }

  coords_box_toscreen(&icon_coords, (WimpRedrawWindowBlock *) &state);

1951 1952 1953 1954 1955 1956
  /* Find out which lines to draw */

  for(nolines = 0; nolines < sizeof(display_this); nolines++) display_this[nolines] = 0;

  nolines = 1; /* Start with one line (half at top, half at bottom) */

1957
  if (choices_find_component(CD_LinkColourBt))
1958 1959 1960 1961
  {
    nolines ++;
    display_this[0] = 1;
  }
1962
  if (choices_find_component(CD_UsedColourBt))
1963 1964 1965 1966
  {
    nolines ++;
    display_this[1] = 1;
  }
1967
  if (choices_find_component(CD_FolwColourBt))
1968 1969 1970 1971
  {
    nolines ++;
    display_this[2] = 1;
  }
1972
  if (choices_find_component(CD_SlctColourBt))
1973 1974 1975 1976 1977
  {
    nolines ++;
    display_this[3] = 1;
  }

1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
  /* Start the redraw loop */

  ChkError(wimp_redraw_window(&block, &more));

  while (more && !e)
  {
    /* Clip the redraw area to only take in the gadget */

    if (set_graphics_intersection(&icon_coords, &block.redraw_area))
    {
      /* Only claim the font if it is required */

      if (!gotfont)
      {
        /* Find default browser font, the font manager will return system */
        /* font if it is configured.                                      */

        /* Find a 1000 subpoint (big) version of default browser font to scale against */

        h = fm_find_font(NULL, "serif", 1000, 1000, 0, 0);
1998
        e = fm_font_box(h, &fbox);
1999
        fm_lose_font(NULL, h);
2000
        if (e) show_error_cont(e);
2001 2002 2003 2004 2005 2006
        fontheight = fbox.ymax - fbox.ymin;

        /* Find a version of the default browser font with a */
        /* point size which will allow an appropriate number */
        /* of lines in the fake page display.                */

2007
        ptsize = ((gadsize * 1000 / nolines) / fontheight);
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026

        h = fm_find_font(NULL, "serif", ptsize, ptsize, 0, 0);
        ChkError(fm_font_box(h, &fbox));
        fontheight = fbox.ymax - fbox.ymin;
        gotfont = 1;
      }

      /* Fake browser drawing code is here */

      /* Fill background with background colour */

      redraw_set_colour(new_choices->background_colour);
      ChkError(bbc_rectanglefill(icon_coords.xmin, icon_coords.ymin, icon_coords.xmax - icon_coords.xmin, icon_coords.ymax - icon_coords.ymin));

      xtarget = icon_coords.xmin - 32;
      ypos = icon_coords.ymax - fontheight / 3;

      /* Display the top line of the fake page display */

David Brown's avatar
David Brown committed
2027
      ChkError(choices_write_rubbish(h, xtarget, ypos, icon_coords.xmax, new_choices->text_colour, new_choices->background_colour, &xtarget, rubbish_seed + 1));
2028 2029 2030 2031 2032 2033

      ypos -= fontheight;
      xtarget = icon_coords.xmin - 32;

      /* Display the new link line of the fake page display */

2034 2035
      if (display_this[0])
      {
David Brown's avatar
David Brown committed
2036
        ChkError(choices_write_rubbish(h, xtarget, ypos, icon_coords.xmin+xstart, new_choices->text_colour, new_choices->background_colour, &xtarget, rubbish_seed + 2));
2037
        ChkError(choices_draw_string(h, "new", xtarget, ypos, new_choices->underline_links, new_choices->link_colour, new_choices->background_colour, &xtarget));
David Brown's avatar
David Brown committed
2038
        ChkError(choices_write_rubbish(h, xtarget, ypos, icon_coords.xmax, new_choices->text_colour, new_choices->background_colour, &xtarget, rubbish_seed + 3));
2039

2040 2041 2042
        ypos -= fontheight;
        xtarget = icon_coords.xmin - 32;
      }
2043 2044 2045

      /* Display the new followed line of the fake page display */

2046 2047
      if (display_this[1])
      {
David Brown's avatar
David Brown committed
2048
        ChkError(choices_write_rubbish(h, xtarget, ypos, icon_coords.xmin+xstart, new_choices->text_colour, new_choices->background_colour, &xtarget, rubbish_seed + 4));
2049
        ChkError(choices_draw_string(h, "followed", xtarget, ypos, new_choices->underline_links, new_choices->used_colour, new_choices->background_colour, &xtarget));
David Brown's avatar
David Brown committed
2050
        ChkError(choices_write_rubbish(h, xtarget, ypos, icon_coords.xmax, new_choices->text_colour, new_choices->background_colour, &xtarget, rubbish_seed + 5));
2051

2052 2053 2054
        ypos -= fontheight;
        xtarget = icon_coords.xmin - 32;
      }
2055 2056 2057

      /* Display the highlighted link line of the fake page display */

2058 2059
      if (display_this[2])
      {
David Brown's avatar
David Brown committed
2060
        ChkError(choices_write_rubbish(h, xtarget, ypos, icon_coords.xmin+xstart, new_choices->text_colour, new_choices->background_colour, &xtarget, rubbish_seed + 6));
2061
        ChkError(choices_draw_string(h, "highlighted", xtarget, ypos, new_choices->underline_links, new_choices->followed_colour, new_choices->background_colour, &xtarget));
David Brown's avatar
David Brown committed
2062
        ChkError(choices_write_rubbish(h, xtarget, ypos, icon_coords.xmax, new_choices->text_colour, new_choices->background_colour, &xtarget, rubbish_seed + 7));
2063

2064 2065 2066
        ypos -= fontheight;
        xtarget = icon_coords.xmin - 32;
      }
2067 2068

      /* Display the selected link line of the fake page display */
2069 2070
      if (display_this[3])
      {
David Brown's avatar
David Brown committed
2071
        ChkError(choices_write_rubbish(h, xtarget, ypos, icon_coords.xmin+xstart, new_choices->text_colour, new_choices->background_colour, &xtarget, rubbish_seed + 8));
2072
        ChkError(choices_draw_string(h, "selected", xtarget, ypos, new_choices->underline_links, new_choices->selected_colour, new_choices->background_colour, &xtarget));
David Brown's avatar
David Brown committed
2073
        ChkError(choices_write_rubbish(h, xtarget, ypos, icon_coords.xmax, new_choices->text_colour, new_choices->background_colour, &xtarget, rubbish_seed + 9));
2074 2075 2076

        ypos -= fontheight;
        xtarget = icon_coords.xmin - 32;
2077
      }
2078 2079 2080

      /* Display the bottom line of the fake page display */

David Brown's avatar
David Brown committed
2081
      ChkError(choices_write_rubbish(h, xtarget, ypos, icon_coords.xmax, new_choices->text_colour, new_choices->background_colour, &xtarget, rubbish_seed + 10));
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
    }

    restore_graphics_intersection(&block.redraw_area);

    /* Get the next redraw rectangle */

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

  if (gotfont) fm_lose_font(NULL, h);

  return 1;
}

/*************************************************/
/* choices_option_state_handler()                */
/*                                               */
/* Called when an option button has its state    */
/* changed by being clicked on.  Sets or unsets  */
/* the relevant flag in new_choices and causes   */
/* redraws where necessary.                      */
/*************************************************/

static int choices_option_state_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
2107 2108 2109
  int               state;
  ObjectId          window;

2110 2111 2112 2113 2114 2115 2116 2117
  ChkError(optionbutton_get_state(0, idb->self_id, idb->self_component, &state));

  switch(idb->self_component)
  {
    case CD_UnderlineLinks:
    {
      new_choices->underline_links = !!state;

2118
      /* Attempt to redraw fake page display */
2119

2120
      if ((window = choices_find_component(CD_FakePage)) != 0) button_set_flags(0, window, CD_FakePage, 0, 0);
2121 2122 2123 2124 2125 2126
      break;
    }

    case CD_UseDocColours:  new_choices->use_source_cols = !!state; break;
    case CD_ShowForeground: new_choices->show_foreground = !!state; break;
    case CD_ShowBackground: new_choices->show_background = !!state; break;
2127 2128 2129 2130 2131 2132

    case CD_URLBar:         new_choices->url_bar         = !!state; break;
    case CD_StatusBar:      new_choices->status_bar      = !!state; break;
    case CD_ButtonBar:      new_choices->button_bar      = !!state; break;
    case CD_FullScreen:     new_choices->full_screen     = !!state; break;

2133 2134 2135 2136
    case CD_ClientPull:     new_choices->client_pull     = !!state; break;
    case CD_NetscapeEmu:    new_choices->clone           = !!state; break;
    case CD_FramesSupport:  new_choices->support_frames  = !!state; break;

2137 2138 2139
    case CD_FontsSystem:
    new_choices->system_font = !!state;
    if ((window = choices_find_component(CD_FontsTF1Labl)) != 0)
2140
    {
2141
      choices_set_fonts_greyed(window, new_choices->system_font);
2142 2143
      /*set_gadget_state(window, CD_FontsGroup1,  new_choices->system_font);*/
      if ((window = choices_find_component(CD_FontsSize)) != 0)
2144
      {
2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
        if (new_choices->system_font)
        {
          int frac, temp;
          numberrange_get_value(0, window, CD_FontsSize, &new_choices->font_size);
          frac = new_choices->font_size % 10;
          new_choices->font_size = ((new_choices->font_size / 10)<<4) + fromdecimal[frac];
          temp = FM_Standard_Size;
          frac = (temp & 0xf);
          temp = ((temp >> 4) * 10) + todecimal[frac];
          choices_numberrange_set_value(0, window, CD_FontsSize, temp);
2155
          choices_numberrange_set_value(0, window, CD_FontsAspect, 100);
2156 2157 2158 2159 2160 2161 2162 2163
        }
        else
        {
          int temp, frac;
          temp = new_choices->font_size;
          frac = (temp & 0xf);
          temp = ((temp >> 4) * 10) + todecimal[frac];
          choices_numberrange_set_value(0, window, CD_FontsSize, temp);
2164
          choices_numberrange_set_value(0, window, CD_FontsAspect, new_choices->tt_aspect);
2165
        }
2166
      }
2167 2168
    }
    break;
2169

2170 2171
    case CD_ObjHandle:
    new_choices->support_object = !!state;
2172

2173 2174 2175 2176 2177 2178
    if ((window = choices_find_component(CD_ObjPlugDisp)) != 0)
    {
      set_gadget_state(window, CD_ObjPlugLabl, !new_choices->support_object);
      set_gadget_state(window, CD_ObjPlugBt, !new_choices->support_object);
      set_gadget_state(window, CD_ObjPlugDisp, !new_choices->support_object);
      choices_set_plugin_field();
2179 2180 2181
    }
    break;

David Brown's avatar
David Brown committed
2182 2183 2184 2185
    case CD_NetUseProxy:
    new_choices->use_proxy = !!state;
    if ((window = choices_find_component(CD_NetProxyAddr))    != 0)
    {
2186 2187
      set_gadget_state(window, CD_NetProxyAddr, !new_choices->use_proxy);
      set_gadget_state(window, CD_NetProxyLabl, !new_choices->use_proxy);
David Brown's avatar
David Brown committed
2188 2189 2190
    }
    break;

2191
    case CD_HiDontAge:
2192
    choices_set_expiry_age_greyed(idb->self_id, !state);
2193
    expiry_age_greyed = !state;
2194 2195 2196
    break;

    case CD_HiDontSize:
2197
    choices_set_max_size_greyed(idb->self_id, !state);
2198
    max_size_greyed = !state;
2199 2200
    break;

2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
    case CD_HiImDontAge:
    choices_set_im_expiry_age_greyed(idb->self_id, !state);
    image_expiry_age_greyed = !state;
    break;

    case CD_HiImDontSize:
    choices_set_im_max_size_greyed(idb->self_id, !state);
    image_max_size_greyed = !state;
    break;

2211 2212 2213 2214 2215
    case CD_TabSupport:
    new_choices->support_tables = !!state;
    choices_set_tables_greyed(!new_choices->support_tables,idb->self_id);
    break;

2216 2217 2218
    /* Haven't recognised this option button event so pass it on */

    default: return 0;
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
  }

  return 1;
}

/*************************************************/
/* choices_mode_change()                         */
/*                                               */
/* Called on every mode change event.  Records   */
/* that a mode change has taken place for use    */
/* with the choices_open_choice_window function. */
/*************************************************/

_kernel_oserror * choices_mode_change(void)
{
  choices_modechanged = 1;

  return NULL;
}

/*************************************************/
/* choices_open_choice_window()                  */
/*                                               */
/* Called whenever the choices window needs to   */
/* be moved and specifically on mode changes so  */
/* the current sub window can be positioned      */
/* correctly dealing with rounding errors        */
/* between modes of different aspect ratio.      */
/*************************************************/

static int choices_open_choice_window(int eventcode, WimpPollBlock * event, IdBlock * idb, void * handle)
{
2251
  toolbox_show_object(0, idb->self_id, Toolbox_ShowObject_FullSpec, &(event->open_window_request.visible_area), idb->parent_id, idb->parent_component);
2252 2253 2254

  if (choices_modechanged)
  {
2255 2256
    /* Reshow the subwindow at possibly new location */

2257 2258 2259 2260 2261 2262
    choices_show_subwindow(idb->self_id, current_subwindow);
    choices_modechanged = 0;
  }

  return 1;
}
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281

/*************************************************/
/* choices_find_component()                      */
/*                                               */
/* Scans all the choices subwindows for the      */
/* requested component and returns the id of the */
/* first window in which it was seen.            */
/*                                               */
/* Parameters: The component to find.            */
/*                                               */
/* Returns:    Where to return the object id to. */
/*             0 if the component was not found  */
/*************************************************/

static ObjectId choices_find_component(ComponentId component)
{
  int          findwindow;
  unsigned int flags;

2282 2283 2284
  /* Uses lazy evaluation to not call gadget_get_flags unless */
  /* subwindows[findwindow] contains an objectid.             */

2285
  for(findwindow = 0; findwindow < CDNoSubwindows; findwindow++)
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
    if (subwindows[findwindow] && !gadget_get_flags(0, subwindows[findwindow], component, &flags)) return subwindows[findwindow];

  return 0;
}

/*************************************************/
/* choices_writablefield_set_value()             */
/*                                               */
/* Does as writablefield_set_value but will only */
/* update it if the text is different to that    */
/* currently in the display field.               */
/*************************************************/

static _kernel_oserror *choices_writablefield_set_value(unsigned int flags, ObjectId window, ComponentId writable, char *text)
{
  int               reqdsize;
  char            * oldtext;
2303
  _kernel_oserror * e = NULL;
2304 2305 2306 2307 2308 2309 2310 2311 2312

  RetError(writablefield_get_value(0, window, writable, NULL, 0, &reqdsize));

  oldtext = malloc(reqdsize+1);
  if (!oldtext) return make_no_memory_error(100);

  writablefield_get_value(0, window, writable, oldtext, reqdsize, NULL);

  if (strcmp(text, oldtext))
2313
  {
2314
    e = writablefield_set_value(flags, window, writable, text);
2315 2316
  }

2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
  free(oldtext);

  return e;
}

/*************************************************/
/* choices_displayfield_set_value()              */
/*                                               */
/* Does as displayfield_set_value but will only  */
/* update it if the text is different to that    */
/* currently in the display field.               */
/*************************************************/

static _kernel_oserror *choices_displayfield_set_value(unsigned int flags, ObjectId window, ComponentId writable, char *text)
{
  int               reqdsize;
  char            * oldtext;
2334
  _kernel_oserror * e = NULL;
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364

  RetError(displayfield_get_value(0, window, writable, NULL, 0, &reqdsize));

  oldtext = malloc(reqdsize+1);
  if (!oldtext) return make_no_memory_error(100);

  displayfield_get_value(0, window, writable, oldtext, reqdsize, NULL);

  if (strcmp(text, oldtext))
  {
    e = displayfield_set_value(flags, window, writable, text);
  }

  free(oldtext);

  return e;
}

/*************************************************/
/* choices_button_set_validation()               */
/*                                               */
/* Does as button_set_validation but will only   */
/* update it if the text is different to that    */
/* currently in the button.                      */
/*************************************************/

static _kernel_oserror *choices_button_set_validation(unsigned int flags, ObjectId window, ComponentId writable, char *text)
{
  int               reqdsize;
  char            * oldtext;
2365
  _kernel_oserror * e = NULL;
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394

  RetError(button_get_validation(0, window, writable, NULL, 0, &reqdsize));

  oldtext = malloc(reqdsize+1);
  if (!oldtext) return make_no_memory_error(100);

  button_get_validation(0, window, writable, oldtext, reqdsize, NULL);

  if (strcmp(text, oldtext))
  {
    e = button_set_validation(flags, window, writable, text);
  }

  free(oldtext);

  return e;
}

/*************************************************/
/* choices_numberrange_set_value()               */
/*                                               */
/* Does as numberrange_set_value but will only   */
/* update it if the number is different to that  */
/* currently in the display.                     */
/*************************************************/

static _kernel_oserror *choices_numberrange_set_value(unsigned int flags, ObjectId window, ComponentId writable, int value)
{
  int               oldvalue;
2395
  _kernel_oserror * e = NULL;
2396 2397 2398 2399 2400 2401 2402 2403 2404

  RetError(numberrange_get_value(0, window, writable, &oldvalue));

  if (oldvalue != value)
  {
    e = numberrange_set_value(flags, window, writable, value);
  }

  return e;
2405
}
2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495

/*************************************************/
/* choices_modified_font()                       */
/*                                               */
/* Finds a version of a font with a specified    */
/* modification eg. bold or italic.              */
/*                                               */
/* Parameters: pointer to the original font name */
/*                                               */
/*             pointer to a space separated and  */
/*             null terminated list of           */
/*             modifications in order of         */
/*             preference                        */
/*                                               */
/*             pointer to a buffer to contain    */
/*             the name of the new font.         */
/*************************************************/

static void choices_modified_font(char * orig, char * mod, char * buffer)
{
  _kernel_oserror * e = NULL;
  char              origname[Limits_FontName];
  int               f=-1;
  char            * p, * p2;
  char            * lastdot;
  BOOL              found;

  /* Find the fontname field */

  _swix(Font_FindField, _INR(1,2)|_OUTR(1,2), orig, 'F', &p, &found);

  /* If the field could not be found assume that the entire */
  /* string is the font name.                               */

  if (!found) p = orig;

  p2 = origname;

  /* Extract the font name.                                       */
  /* Copys the string pointed to by p to p2 until either a \ or a */
  /* character with an ascii value less than 32 is encountered.   */

  while (*p > 32 && *p != '\\') *p2++ = *p++;

  /* NULL terminate the new string */

  *p2 = '\0';

  lastdot = strrchr(origname, '.');

  /* Get first of possibly several possible modifications */

  p = strtok(mod, " ");

  while (p)
  {
    sprintf(buffer, "%s.%s", origname, p);
    e=_swix(Font_FindFont, _INR(1,5)|_OUT(0), buffer, 12*16, 12*16, 90, 90, &f);

    /* If there was no error then the font must have been found */

    if (!e) break;

    if (lastdot)
    {
      *lastdot = '\0';
      sprintf(buffer, "%s.%s", origname, p);
      *lastdot = '.';
      e=_swix(Font_FindFont, _INR(1,5)|_OUT(0), buffer, 12*16, 12*16, 90, 90, &f);
      if (e==NULL) break;
    }

    /* Get the next modification */

    p=strtok(NULL, " ");
  }

  /* If a font has been 'found' we need to lose it again */

  if (f >= 0) _swix(Font_LoseFont, _IN(0), f);

  /* If an error was encountered the best we can do is */
  /* to return the original font.                      */

  if (e) strcpy(buffer, orig);
}

/*************************************************/
/* choices_font_button_handler()                 */
/*                                               */
2496 2497
/* Opens a font menu box with appropriate        */
/* settings for the current font.                */
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
/*************************************************/

static int choices_font_button_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  _kernel_oserror *e;
  int  position[2];
  BBox box;
  WimpGetWindowStateBlock state;
  ObjectId                fontmenu_id;
  int colour[2];
2508
  char * fontname;
2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555

  switch(idb->self_component)
  {
    case CD_FontsTF1Bt: break;
    case CD_FontsTF2Bt: break;
    case CD_FontsTF3Bt: break;

    default:
    return 1;
    break;
  }

  colour[1] = 0;

  e = gadget_get_bbox(0, idb->self_id, idb->self_component, &box);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = window_get_wimp_handle(0, idb->self_id, &state.window_handle);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = wimp_get_window_state(&state);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  /* Set open coordinates of encoding menu to top left of encoding button */

  position[0] = coords_x_toscreen(box.xmax, (WimpRedrawWindowBlock*)&state);
  position[1] = coords_y_toscreen(box.ymax, (WimpRedrawWindowBlock*)&state);

  e = toolbox_create_object(0, "FontMenu", &fontmenu_id);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
  switch(idb->self_component)
  {
    case CD_FontsTF1Bt: fontname = new_typefaces[0].fontnames[0]; break;
    case CD_FontsTF2Bt: fontname = new_typefaces[1].fontnames[0]; break;
    case CD_FontsTF3Bt: fontname = new_typefaces[2].fontnames[0]; break;
    default: fontname = ""; break;
  }

  fontmenu_set_font(0, fontmenu_id, fontname);

2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618
  show_error_ret(toolbox_show_object(Toolbox_ShowObject_AsMenu,
                                     fontmenu_id,
                                     Toolbox_ShowObject_TopLeft,
                                     position,
                                     idb->self_id,
                                     idb->self_component));

  e = event_register_toolbox_handler(fontmenu_id, FontMenu_HasBeenHidden, choices_font_closed_handler, NULL);
  if (e) {show_error_ret(e); return 1;}

  e = event_register_toolbox_handler(fontmenu_id, FontMenu_Selection, choices_font_selected_handler, NULL);
  if (e) {show_error_ret(e); return 1;}

  return 1;
}

/*************************************************/
/* choices_font_closed_handler()                 */
/*                                               */
/* Called when the font menu is closed           */
/* deregisters all events attached to it and     */
/* deletes the menu object.                      */
/*************************************************/

static int choices_font_closed_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  _kernel_oserror *e;

  e = event_register_toolbox_handler(idb->self_id, FontMenu_HasBeenHidden, choices_font_closed_handler, NULL);
  if (e) {show_error_ret(e); return 1;}

  e = event_register_toolbox_handler(idb->self_id, FontMenu_Selection, choices_font_selected_handler, NULL);
  if (e) {show_error_ret(e); return 1;}

  e = toolbox_delete_object(0, idb->self_id);
  if (e) {show_error_ret(e); return 1;}

  #ifdef TRACE
    if (tl & (1u<<29)) Printf("choices_font_closed_handler: FontMenu deleted\n");
  #endif

  return 1;
}

/*************************************************/
/* choices_font_selected_handler()               */
/*                                               */
/* Called when the a font is selected in the     */
/* font menu.                                    */
/*************************************************/

static int choices_font_selected_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
2619
  char font[4] [Limits_FontName], *p, *p2;
2620
  int found;
2621
  fm_typeface * target;
2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664

  #ifdef TRACE
    if (tl & (1u<<29))
    {
      Printf("choices_font_selected_handler: called\n");
      Printf("Selected font = %s\n", ((FontMenuSelectionEvent*)event)->font_id);
    }
  #endif

  p2 = font[0];

  /* Assume that the F field will be found because the fontmenu */
  /* returns the font in the correct format.                    */

  ChkError(_swix(Font_FindField, _INR(1,2)|_OUTR(1,2), ((FontMenuSelectionEvent*)event)->font_id, 'F', &p, &found));

  /* Extract the font name */

  while (*p > 32 && *p != '\\') *p2++ = *p++;
  *p2 = '\0';

  choices_modified_font(font[0], getenv("Font$Italic"), font[1]); /* Get italic variant of base font */
  choices_modified_font(font[0], getenv("Font$Bold"),   font[2]); /* Get bold version of base font   */
  choices_modified_font(font[2], getenv("Font$Italic"), font[3]); /* Get italic version of bold font */

  #ifdef TRACE
    if (tl & (1u<<29))
    {
      Printf("Base font           = %s\n", font[0]);
      Printf("Italic version      = %s\n", font[1]);
      Printf("Bold version        = %s\n", font[2]);
      Printf("Bold-Italic version = %s\n", font[3]);
    }
  #endif

  /* Set display field to name of new font */
  /* Ignore error as the display isn't     */
  /* required.                             */

  choices_displayfield_set_value(0, idb->parent_id, idb->parent_component - 1, font[0]);

  switch(idb->parent_component)
  {
2665 2666 2667
    case CD_FontsTF1Bt: target = &new_typefaces[0]; break;
    case CD_FontsTF2Bt: target = &new_typefaces[1]; break;
    case CD_FontsTF3Bt: target = &new_typefaces[2]; break;
2668 2669 2670 2671 2672 2673

    default:
    return 1;
    break;
  }

2674 2675 2676 2677 2678
  strcpy(target->fontnames[0], font[0]);
  strcpy(target->fontnames[1], font[1]);
  strcpy(target->fontnames[2], font[2]);
  strcpy(target->fontnames[3], font[3]);

2679 2680 2681
  return 1;
}

2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
/*************************************************/
/* choices_set_fonts_greyed()                    */
/*                                               */
/* Sets the font name and font size gadgets to   */
/* greyed/ungreyed.                              */
/*                                               */
/* Parameters: ObjectId of the window which      */
/*             contains the icons.               */
/*                                               */
/*             0 to ungrey, any other value to   */
/*             to grey.                          */
/*************************************************/

static void choices_set_fonts_greyed(ObjectId window, int state)
{
2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
  set_gadget_state(window, CD_FontsTF1Labl,     !!state);
  set_gadget_state(window, CD_FontsTF1Disp,     !!state);
  set_gadget_state(window, CD_FontsTF1Bt,       !!state);
  set_gadget_state(window, CD_FontsTF2Labl,     !!state);
  set_gadget_state(window, CD_FontsTF2Disp,     !!state);
  set_gadget_state(window, CD_FontsTF2Bt,       !!state);
  set_gadget_state(window, CD_FontsTF3Labl,     !!state);
  set_gadget_state(window, CD_FontsTF3Disp,     !!state);
  set_gadget_state(window, CD_FontsTF3Bt,       !!state);
  set_gadget_state(window, CD_FontsSize,        !!state);
  set_gadget_state(window, CD_FontsSzLabl1,     !!state);
  set_gadget_state(window, CD_FontsSzLabl2,     !!state);
  set_gadget_state(window, CD_FontsAspect,      !!state);
  set_gadget_state(window, CD_FontsAspectLabl1, !!state);
  set_gadget_state(window, CD_FontsAspectLabl2, !!state);
2712 2713
}

2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
/*************************************************/
/* choices_set_plugin_field()                    */
/*                                               */
/* Sets the plugin field appropriately to        */
/* reflect the current state of                  */
/* new_choices->plugin_control                   */
/*************************************************/

_kernel_oserror * choices_set_plugin_field(void)
{
  _kernel_oserror * e;
  ObjectId          destwind;
  char            * tempstring;

  #ifdef TRACE
    if (tl & (1u<<29)) Printf("choices_set_plugin_field: Called\n");
  #endif

  /* Find which window the encoding display is in */

  destwind = choices_find_component(CD_ObjPlugDisp);
  if (!destwind) return NULL;

2737
  RetError(choices_get_menu_entry_text("ChPlugin", new_choices->plugin_control, &tempstring));
2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774

  e = choices_displayfield_set_value(0, destwind, CD_ObjPlugDisp, tempstring);

  free(tempstring);

  return NULL;
}

/*************************************************/
/* choices_plug_m_click_handler()                */
/*                                               */
/* Called when there is a selection in the       */
/* plugin menu.  Sets the selected menu item to  */
/* be ticked, unticks the previously ticked      */
/* entry sets appropriate new_choices field and  */
/* updates the display field of the component    */
/* with component number 1 less than the menus   */
/* parent.                                       */
/*************************************************/

static int choices_plug_m_click_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  if (plug_ticked != idb->self_component)
  {
    if (plug_ticked != -1) menu_set_tick(0, idb->self_id, plug_ticked, 0);
    menu_set_tick(0, idb->self_id, idb->self_component, 1);
    plug_ticked = idb->self_component;
  }

  new_choices->plugin_control = idb->self_component;

  choices_set_plugin_field();

  return 1;
}

/*************************************************/
2775
/* choices_plug_m_button_handler()               */
2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840
/*                                               */
/* Opens the save menu with a value appropriate  */
/* to either the hotlist or history.             */
/*************************************************/

static int choices_plug_m_button_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  _kernel_oserror         * e;
  int                       position[2];
  BBox                      box;
  WimpGetWindowStateBlock   state;
  ObjectId                  display;
  int                       new_tick;

  e = gadget_get_bbox(0, idb->self_id, idb->self_component, &box);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = window_get_wimp_handle(0, idb->self_id, &state.window_handle);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = wimp_get_window_state(&state);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  /* Set open coordinates of display menu to top left of display button */

  position[0] = coords_x_toscreen(box.xmax, (WimpRedrawWindowBlock*)&state);
  position[1] = coords_y_toscreen(box.ymax, (WimpRedrawWindowBlock*)&state);

  e = toolbox_create_object(0, "ChPlugin", &display);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  new_tick = new_choices->plugin_control;

  if (plug_ticked != new_tick)
  {
    if (plug_ticked != -1) menu_set_tick(0, display, plug_ticked, 0);
    menu_set_tick(0, display, new_tick, 1);
    plug_ticked = new_tick;
  }

  show_error_ret(toolbox_show_object(Toolbox_ShowObject_AsMenu,
                                     display,
                                     Toolbox_ShowObject_TopLeft,
                                     position,
                                     idb->self_id,
                                     idb->self_component));

  return 1;
}
2841

2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908
/*************************************************/
/* choices_history_radio_handler()               */
/*                                               */
/* Handles radio button state changes.  Swaps    */
/* between page and image history settings.      */
/*************************************************/

static int choices_history_radio_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  int state, temp;
  ObjectId window;

  radiobutton_get_state(0, idb->self_id, idb->self_component, &state, NULL);

  /* Is this a radiobutton selected event? */

  if (state)
  {
    if (expiry_age_greyed)
    {
      if (history_radio) new_choices->image_expiry_age = 0;
      else               new_choices->expiry_age = 0;
    }
    else
    {
      if ((window = choices_find_component(CD_HiExpiryAge))    != 0)
      {
        numberrange_get_value(0, window, CD_HiExpiryAge,  &temp);
        if (history_radio) new_choices->image_expiry_age = choices_typed_time_to_seconds(temp, history_expiry_units);
        else               new_choices->expiry_age = choices_typed_time_to_seconds(temp, history_expiry_units);
      }
    }

    if (max_size_greyed)
    {
      if (history_radio) new_choices->image_max_size = 0;
      else               new_choices->max_size = 0;
    }
    else
    {
      if ((window = choices_find_component(CD_HiMaxSize))      != 0)
      {
        numberrange_get_value(0, window, CD_HiMaxSize,    &temp);
        if (history_radio) new_choices->image_max_size = temp * 1024;
        else               new_choices->max_size = temp * 1024;
      }
    }

    switch(idb->self_component)
    {
      case CD_HiRadPage:
      history_radio = 0;
      if ((window = choices_find_component(CD_HiExpiryAge))    != 0) choices_set_expiry_age(window);
      if ((window = choices_find_component(CD_HiMaxSize))      != 0) choices_set_max_size(window);
      break;

      case CD_HiRadImage:
      history_radio = 1;
      if ((window = choices_find_component(CD_HiExpiryAge))    != 0) choices_set_expiry_age(window);
      if ((window = choices_find_component(CD_HiMaxSize))      != 0) choices_set_max_size(window);
      break;
    }
  }

  return 1;
}

2909 2910
/*************************************************/
/* choices_set_expiry_age()                      */
2911 2912 2913 2914 2915 2916
/*                                               */
/* Sets up the expiry age fields and greys/      */
/* ungreys them as appropriate.                  */
/*                                               */
/* Parameters: ObjectId of the window containing */
/*             the gadgets.                      */
2917 2918 2919 2920 2921 2922 2923
/*************************************************/

static void choices_set_expiry_age(ObjectId window)
{
  int temp;
  int rangetop;
  int time_scale;
2924 2925 2926
  int age;

  age = history_radio ? new_choices->image_expiry_age : new_choices->expiry_age;
2927

2928
  if (age == 0)
2929 2930 2931 2932 2933 2934 2935
  {
    temp       = 1;
    time_scale = CD_TimeDays;
    rangetop  = 7;
  }
  else
  {
2936 2937
    time_scale = choices_return_appropriate_timetype(age);
    temp       = choices_seconds_to_typed_time(age, time_scale);
2938 2939
    rangetop   = choices_get_range_of_typed_time(time_scale);
  }
2940

2941 2942 2943
  expiry_age_greyed = !age;
  choices_set_expiry_age_greyed(window, !age);
  optionbutton_set_state(0, window, CD_HiDontAge, !!age);
2944

2945
  choices_numberrange_set_value(0, window, CD_HiExpiryAge, temp);
2946 2947 2948 2949
  choices_set_timetype_field(time_scale, CD_HiAgeTypeDisp);

  history_expiry_units = time_scale;

2950 2951 2952 2953 2954
  numberrange_set_bounds(15, window, CD_HiExpiryAge, 1, rangetop, 1, 0);
}

/*************************************************/
/* choices_set_expiry_age_greyed()               */
2955 2956 2957 2958 2959 2960 2961 2962 2963
/*                                               */
/* Sets the expiry age gadgets to greyed/        */
/* ungreyed.                                     */
/*                                               */
/* Parameters: ObjectId of the window which      */
/*             contains the icons.               */
/*                                               */
/*             0 to ungrey, any other value to   */
/*             to grey.                          */
2964 2965
/*************************************************/

2966
static void choices_set_expiry_age_greyed(ObjectId window, int state)
2967
{
2968 2969 2970 2971
  set_gadget_state(window, CD_HiExpiryAgeLabl, !!state);
  set_gadget_state(window, CD_HiExpiryAge,     !!state);
  set_gadget_state(window, CD_HiAgeTypeDisp,   !!state);
  set_gadget_state(window, CD_HiAgeTypeBt,     !!state);
2972 2973
}

2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034
/*************************************************/
/* choices_set_im_expiry_age()                   */
/*                                               */
/* Sets up the image expiry age fields and greys */
/* /ungreys them as appropriate.                 */
/*                                               */
/* Parameters: ObjectId of the window containing */
/*             the gadgets.                      */
/*************************************************/

static void choices_set_im_expiry_age(ObjectId window)
{
  int temp;
  int rangetop;
  int time_scale;

  if (new_choices->image_expiry_age == 0)
  {
    temp       = 1;
    time_scale = CD_TimeDays;
    rangetop  = 7;
  }
  else
  {
    time_scale = choices_return_appropriate_timetype(new_choices->image_expiry_age);
    temp       = choices_seconds_to_typed_time(new_choices->image_expiry_age, time_scale);
    rangetop   = choices_get_range_of_typed_time(time_scale);
  }

  image_expiry_age_greyed = !new_choices->image_expiry_age;
  choices_set_im_expiry_age_greyed(window, !new_choices->image_expiry_age);
  optionbutton_set_state(0, window, CD_HiImDontAge, !!new_choices->image_expiry_age);
  choices_numberrange_set_value(0, window, CD_HiImExpiryAge, temp);
  choices_set_timetype_field(time_scale, CD_HiImAgeTypeDisp);

  image_expiry_units = time_scale;

  numberrange_set_bounds(15, window, CD_HiImExpiryAge, 1, rangetop, 1, 0);
}

/*************************************************/
/* choices_set_im_expiry_age_greyed()            */
/*                                               */
/* Sets the image expiry age gadgets to greyed/  */
/* ungreyed.                                     */
/*                                               */
/* Parameters: ObjectId of the window which      */
/*             contains the icons.               */
/*                                               */
/*             0 to ungrey, any other value to   */
/*             to grey.                          */
/*************************************************/

static void choices_set_im_expiry_age_greyed(ObjectId window, int state)
{
  set_gadget_state(window, CD_HiImExpiryAgeLabl, !!state);
  set_gadget_state(window, CD_HiImExpiryAge,     !!state);
  set_gadget_state(window, CD_HiImAgeTypeDisp,   !!state);
  set_gadget_state(window, CD_HiImAgeTypeBt,     !!state);
}

3035 3036
/*************************************************/
/* choices_set_max_size()                        */
3037 3038 3039 3040 3041 3042
/*                                               */
/* Sets up the max size field and greys or       */
/* ungreys it as appropriate.                    */
/*                                               */
/* Parameters: The ObjectId of the window which  */
/*             contains the gadgets.             */
3043 3044 3045 3046
/*************************************************/

static void choices_set_max_size(ObjectId window)
{
3047 3048 3049 3050 3051 3052
  int size;

  size = history_radio ? new_choices->image_max_size : new_choices->max_size;

  optionbutton_set_state(0, window, CD_HiDontSize, !!size);
  max_size_greyed = !size;
3053 3054 3055 3056 3057
  choices_set_max_size_greyed(window, max_size_greyed);

  choices_numberrange_set_value(0,
                                window,
                                CD_HiMaxSize,
3058
                                !size ? 32 : size/1024);
3059
}
3060

3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077
/*************************************************/
/* choices_set_max_size_greyed()                 */
/*                                               */
/* Sets the max size gadgets to greyed/ungreyed. */
/*                                               */
/* Parameters: ObjectId of the window which      */
/*             contains the icons.               */
/*                                               */
/*             0 to ungrey, any other value to   */
/*             to grey.                          */
/*************************************************/

static void choices_set_max_size_greyed(ObjectId window, int state)
{
  set_gadget_state(window, CD_HiMaxSizeLabl1, !!state);
  set_gadget_state(window, CD_HiMaxSizeLabl2, !!state);
  set_gadget_state(window, CD_HiMaxSize,      !!state);
3078 3079
}

3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121
/*************************************************/
/* choices_set_im_max_size()                     */
/*                                               */
/* Sets up the image max size field and greys or */
/* ungreys it as appropriate.                    */
/*                                               */
/* Parameters: The ObjectId of the window which  */
/*             contains the gadgets.             */
/*************************************************/

static void choices_set_im_max_size(ObjectId window)
{
  optionbutton_set_state(0, window, CD_HiImDontSize, !!new_choices->image_max_size);
  image_max_size_greyed = !new_choices->image_max_size;
  choices_set_im_max_size_greyed(window, image_max_size_greyed);

  choices_numberrange_set_value(0,
                                window,
                                CD_HiImMaxSize,
                                !new_choices->image_max_size ? 32 : new_choices->image_max_size/1024);
}

/*************************************************/
/* choices_set_im_max_size_greyed()              */
/*                                               */
/* Sets the image max size gadgets to greyed/    */
/* ungreyed.                                     */
/*                                               */
/* Parameters: ObjectId of the window which      */
/*             contains the icons.               */
/*                                               */
/*             0 to ungrey, any other value to   */
/*             to grey.                          */
/*************************************************/

static void choices_set_im_max_size_greyed(ObjectId window, int state)
{
  set_gadget_state(window, CD_HiImMaxSizeLabl1, !!state);
  set_gadget_state(window, CD_HiImMaxSizeLabl2, !!state);
  set_gadget_state(window, CD_HiImMaxSize,      !!state);
}

3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132
/*************************************************/
/* choices_set_timetype_field()                  */
/*                                               */
/* Sets the time field appropriately to reflect  */
/* the passed state.  Also ticks the appropriate */
/* item in the menu.                             */
/*                                               */
/* Parameters: timetype - see defined values in  */
/*                        choices.h              */
/*************************************************/

3133
_kernel_oserror * choices_set_timetype_field(int timetype, ComponentId comp)
3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145
{
  _kernel_oserror * e;
  ObjectId          destwind;
  ObjectId          menu_id;
  char            * tempstring;

  #ifdef TRACE
    if (tl & (1u<<29)) Printf("choices_set_plugin_field: Called\n");
  #endif

  /* Find which window the encoding display is in */

3146
  destwind = choices_find_component(comp);
3147 3148 3149 3150
  if (!destwind) return NULL;

  RetError(toolbox_create_object(0, "ChTime", &menu_id));

3151
  RetError(choices_get_menu_entry_text("ChTime", timetype, &tempstring));
3152

3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176
  RetError(choices_set_timetype_tick(timetype));

  e = choices_displayfield_set_value(0, destwind, comp, tempstring);

  free(tempstring);

  return NULL;
}

/*************************************************/
/* choices_set_timetype_tick()                   */
/*                                               */
/* Ticks the appropriate item in the ChTime menu */
/*                                               */
/* Parameters: timetype - see defined values in  */
/*                        choices.h              */
/*************************************************/

_kernel_oserror * choices_set_timetype_tick(int timetype)
{
  ObjectId menu_id;

  RetError(toolbox_create_object(0, "ChTime", &menu_id));

3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190
  if (time_ticked != -1)
  {
    RetError(menu_set_tick(0, menu_id, time_ticked, 0));
  }

  RetError(menu_set_tick(0, menu_id, timetype, 1));

  time_ticked = timetype;

  return NULL;
}

/*************************************************/
/* choices_timetype_m_button_handler()           */
3191 3192 3193
/*                                               */
/* Displays the time units menu when the         */
/* appropriate toolaction button is pressed.     */
3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229
/*************************************************/

static int choices_timetype_m_button_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  _kernel_oserror         * e;
  int                       position[2];
  BBox                      box;
  WimpGetWindowStateBlock   state;
  ObjectId                  display;

  e = gadget_get_bbox(0, idb->self_id, idb->self_component, &box);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = window_get_wimp_handle(0, idb->self_id, &state.window_handle);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = wimp_get_window_state(&state);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  /* Set open coordinates of display menu to top left of display button */

  position[0] = coords_x_toscreen(box.xmax, (WimpRedrawWindowBlock*)&state);
  position[1] = coords_y_toscreen(box.ymax, (WimpRedrawWindowBlock*)&state);

3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240
  switch(idb->self_component)
  {
    case CD_HiAgeTypeBt:
    choices_set_timetype_tick(history_expiry_units);
    break;

    case CD_HiImAgeTypeBt:
    choices_set_timetype_tick(image_expiry_units);
    break;
  }

3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259
  e = toolbox_create_object(0, "ChTime", &display);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  show_error_ret(toolbox_show_object(Toolbox_ShowObject_AsMenu,
                                     display,
                                     Toolbox_ShowObject_TopLeft,
                                     position,
                                     idb->self_id,
                                     idb->self_component));

  return 1;
}

/*************************************************/
/* choices_timetype_m_click_handler()            */
3260 3261 3262 3263 3264
/*                                               */
/* Handles clicks in the time units menu.        */
/* Translates the current expiry time to the new */
/* units and places the name of the units in the */
/* appropriate display field.                    */
3265 3266 3267 3268 3269 3270 3271
/*************************************************/

static int choices_timetype_m_click_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  ObjectId window;
  int temptime;

3272 3273 3274 3275 3276 3277 3278 3279 3280
  window = idb->parent_id;

  switch(idb->parent_component)
  {
    case CD_HiAgeTypeBt:
    {
      numberrange_get_value(0, window, CD_HiExpiryAge, &temptime);

      temptime = choices_typed_time_to_seconds(temptime, history_expiry_units);
3281

3282
      choices_set_timetype_field(idb->self_component, CD_HiAgeTypeDisp);
3283

3284
      history_expiry_units = idb->self_component;
3285

3286 3287 3288 3289 3290 3291 3292 3293 3294
      numberrange_set_value(0, window, CD_HiExpiryAge, choices_seconds_to_typed_time(temptime, history_expiry_units));
      numberrange_set_bounds(15, window, CD_HiExpiryAge, 1, choices_get_range_of_typed_time(history_expiry_units), 1, 0);

      break;
    }

    case CD_HiImAgeTypeBt:
    {
      numberrange_get_value(0, window, CD_HiImExpiryAge, &temptime);
3295

3296
      temptime = choices_typed_time_to_seconds(temptime, image_expiry_units);
3297

3298 3299 3300 3301 3302 3303 3304 3305 3306 3307
      choices_set_timetype_field(idb->self_component, CD_HiImAgeTypeDisp);

      image_expiry_units = idb->self_component;

      numberrange_set_value(0, window, CD_HiImExpiryAge, choices_seconds_to_typed_time(temptime, image_expiry_units));
      numberrange_set_bounds(15, window, CD_HiImExpiryAge, 1, choices_get_range_of_typed_time(image_expiry_units), 1, 0);

      break;
    }
  }
3308 3309 3310 3311 3312 3313

  return 1;
}

/*************************************************/
/* choices_typed_time_to_seconds()               */
3314 3315 3316 3317 3318 3319 3320 3321 3322 3323
/*                                               */
/* Converts a time in the passed units to        */
/* seconds.                                      */
/*                                               */
/* Parameters: Time in passed units;             */
/*                                               */
/*             units which the time was passed   */
/*             as (defined in choices.h);        */
/*                                               */
/* Returns:    time in seconds.                  */
3324 3325 3326 3327 3328 3329
/*************************************************/

static int choices_typed_time_to_seconds(int time, int timetype)
{
  switch(timetype)
  {
3330 3331 3332 3333 3334 3335
    case CD_TimeMinutes: return time * 60;
    case CD_TimeHours:   return time * 60*60;
    case CD_TimeDays:    return time * 60*60*24;
    case CD_TimeWeeks:   return time * 60*60*24*7;
    case CD_TimeMonths:  return time * 60*60*24*7*4;
    default:             return 0;
3336
  }
3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355
}

/*************************************************/
/* choices_clip_to_min_max()                     */
/*                                               */
/* Clips a number to a minimum and maximum value */
/*                                               */
/* Parameters: initial value;                    */
/*                                               */
/*             minimum;                          */
/*                                               */
/*             maximum.                          */
/*                                               */
/* Returns:    clipped value.                    */
/*************************************************/

int choices_clip_to_min_max(int value, int min, int max)
{
  return value > max ? max : (value < min ? min : value);
3356 3357 3358 3359
}

/*************************************************/
/* choices_seconds_to_typed_time()               */
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369
/*                                               */
/* Converts a time in seconds to a time in the   */
/* passed units.                                 */
/*                                               */
/* Parameters: time in seconds;                  */
/*                                               */
/*             the units to return time in as    */
/*             defined in choices.h;             */
/*                                               */
/* Returns:    the time in the passed units.     */
3370 3371 3372 3373 3374 3375
/*************************************************/

static int choices_seconds_to_typed_time(int secs, int timetype)
{
  switch(timetype)
  {
3376 3377 3378 3379 3380 3381
    case CD_TimeMinutes: return choices_clip_to_min_max(secs/(60),           1, 60);
    case CD_TimeHours:   return choices_clip_to_min_max(secs/(60*60),        1, 24);
    case CD_TimeDays:    return choices_clip_to_min_max(secs/(60*60*24),     1, 7);
    case CD_TimeWeeks:   return choices_clip_to_min_max(secs/(60*60*24*7),   1, 4);
    case CD_TimeMonths:  return choices_clip_to_min_max(secs/(60*60*24*7*4), 1, 12);
    default:             return 1;
3382 3383 3384 3385 3386
  }
}

/*************************************************/
/* choices_return_appropriate_timetype()         */
3387 3388 3389 3390 3391 3392 3393 3394 3395
/*                                               */
/* Given a time in seconds returns the most      */
/* appropriate set of units to display that time */
/* in.                                           */
/*                                               */
/* Parameters: time in seconds.                  */
/*                                               */
/* Returns:    the type of the appropriate units */
/*             as defined in choices.h           */
3396 3397 3398 3399
/*************************************************/

static int choices_return_appropriate_timetype(int seconds)
{
3400 3401 3402 3403 3404
  if (seconds <= 60*60)        return CD_TimeMinutes;
  if (seconds <= 60*60*24)     return CD_TimeHours;
  if (seconds <= 60*60*24*7)   return CD_TimeDays;
  if (seconds <= 60*60*24*7*4) return CD_TimeWeeks;

3405 3406 3407 3408 3409
  return CD_TimeMonths;
}

/*************************************************/
/* choices_get_range_of_typed_time()             */
3410 3411 3412 3413 3414 3415 3416 3417
/*                                               */
/* Returns the max acceptable value of the       */
/* passed time units type.                       */
/*                                               */
/* Parameters: the type of time units. (defined  */
/*             in choices.h)                     */
/*                                               */
/* Returns:    the maximum acceptable value.     */
3418 3419 3420 3421 3422 3423
/*************************************************/

static int choices_get_range_of_typed_time(int timetype)
{
  switch(timetype)
  {
3424 3425 3426 3427 3428 3429
    case CD_TimeMinutes: return 60;
    case CD_TimeHours:   return 24;
    case CD_TimeDays:    return 7;
    case CD_TimeWeeks:   return 4;
    case CD_TimeMonths:  return 12;
    default:             return 0;
3430 3431 3432 3433 3434
  }
}

/*************************************************/
/* choices_set_tables_greyed()                   */
3435 3436 3437 3438 3439 3440 3441 3442 3443
/*                                               */
/* Sets the table border gadgets to greyed/      */
/* ungreyed.                                     */
/*                                               */
/* Parameters: ObjectId of the window which      */
/*             contains the icons.               */
/*                                               */
/*             0 to ungrey, any other value to   */
/*             to grey.                          */
3444 3445 3446 3447
/*************************************************/

static void choices_set_tables_greyed(int state, ObjectId window)
{
3448 3449 3450 3451 3452 3453
  set_gadget_state(window, CD_TabInnerBordLabl, !!state);
  set_gadget_state(window, CD_TabInnerBordDisp, !!state);
  set_gadget_state(window, CD_TabInnerBordBt,   !!state);
  set_gadget_state(window, CD_TabOuterBordLabl, !!state);
  set_gadget_state(window, CD_TabOuterBordDisp, !!state);
  set_gadget_state(window, CD_TabOuterBordBt,   !!state);
3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471
}

/*************************************************/
/* choices_set_table_border_field()              */
/*                                               */
/* Sets the passed display display field         */
/* appropriately to reflect the passed state.    */
/*************************************************/

static _kernel_oserror * choices_set_table_border_field(ObjectId obj, ComponentId comp, int state)
{
  _kernel_oserror *e;
  char *tempstring;

  #ifdef TRACE
    if (tl & (1u<<29)) Printf("choices_set_table_border_field: Called\n");
  #endif

3472
  RetError(choices_get_menu_entry_text("ChTabBord", state, &tempstring));
3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 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

  e = choices_displayfield_set_value(0, obj, comp, tempstring);

  free(tempstring);

  return e;
}

/*************************************************/
/* choices_bord_m_button_handler()               */
/*                                               */
/* Opens the bord menu with a value appropriate  */
/* to either the inner or outer table border.    */
/*************************************************/

static int choices_bord_m_button_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  _kernel_oserror *e;
  int  position[2];
  BBox box;
  WimpGetWindowStateBlock state;
  ObjectId display;
  int new_tick;

  e = gadget_get_bbox(0, idb->self_id, idb->self_component, &box);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = window_get_wimp_handle(0, idb->self_id, &state.window_handle);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  e = wimp_get_window_state(&state);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  /* Set open coordinates of display menu to top left of display button */

  position[0] = coords_x_toscreen(box.xmax, (WimpRedrawWindowBlock*)&state);
  position[1] = coords_y_toscreen(box.ymax, (WimpRedrawWindowBlock*)&state);

  e = toolbox_create_object(0, "ChTabBord", &display);
  if (e)
  {
    show_error_ret(e);
    return 1;
  }

  switch(idb->self_component)
  {
    case CD_TabInnerBordBt: new_tick = new_choices->table_inner; break;
    case CD_TabOuterBordBt: new_tick = new_choices->table_outer; break;
    default: new_tick = 0; break;
  }

  if (bord_ticked != new_tick)
  {
    if (bord_ticked != -1) menu_set_tick(0, display, bord_ticked, 0);
    menu_set_tick(0, display, new_tick, 1);
    bord_ticked = new_tick;
  }

  show_error_ret(toolbox_show_object(Toolbox_ShowObject_AsMenu,
                                     display,
                                     Toolbox_ShowObject_TopLeft,
                                     position,
                                     idb->self_id,
                                     idb->self_component));

  return 1;
}

/*************************************************/
/* choices_bord_m_click_handler()                */
3556 3557 3558 3559
/*                                               */
/* Handles clicks in the border type menu.  Sets */
/* the associated display field and field in the */
/* new_choices structure.                        */
3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584
/*************************************************/

static int choices_bord_m_click_handler(int eventcode, ToolboxEvent * event, IdBlock * idb, void * handle)
{
  if (bord_ticked != idb->self_component)
  {
    if (bord_ticked != -1) menu_set_tick(0, idb->self_id, bord_ticked, 0);
    menu_set_tick(0, idb->self_id, idb->self_component, 1);
    bord_ticked = idb->self_component;
  }

  switch(idb->parent_component)
  {
    case CD_TabInnerBordBt:
    new_choices->table_inner = idb->self_component;
    break;
    case CD_TabOuterBordBt:
    new_choices->table_outer = idb->self_component;
    break;
  }

  choices_set_table_border_field(idb->parent_id, idb->parent_component - 1, idb->self_component);

  return 1;
}
3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604

/*************************************************/
/* choices_get_menu_entry_text()                 */
/*                                               */
/* Gets a pointer to a piece of text acquired    */
/* from a menu.  The pointer is to a malloc      */
/* block and so must be freed after use.         */
/*                                               */
/* Parameters: pointer to the menu name          */
/*                                               */
/*             menu componentid                  */
/*                                               */
/*             pointer to char* to use for       */
/*             the malloc block                  */
/*                                               */
/* Assumes:    the menu is a shared object.      */
/*************************************************/

_kernel_oserror * choices_get_menu_entry_text(char * menuname, ComponentId compid, char ** tempstring)
{
3605 3606
  ObjectId menu_id;
  int      reqdsize;
3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618

  RetError(toolbox_create_object(0, menuname, &menu_id));

  RetError(menu_get_entry_text(0, menu_id, compid, NULL, 0, &reqdsize));

  *tempstring = malloc(reqdsize+1);
  if (!*tempstring) return make_no_memory_error(20);

  RetError(menu_get_entry_text(0, menu_id, compid, *tempstring, reqdsize+1, NULL));

  return NULL;
}