// Copyright 1998 Acorn Computers Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "palchoose.h"
#include "display.h"

void
PalchWin::DispatchEvent( ToolboxEvent *event, IdBlock *idb )
{
  if ((event->hdr.event_code == ActionButton_Selected)
     &&(idb->self_component == 0x02))
  		edit();
  else
      Window::DispatchEvent(event, idb);
}

void
PalchWin::Attach( ObjectId oid )
{
  Window::Attach(oid);

  sprintf(st_file, "0");
  WR_file.Attach(mOID, 0x04);
  AB_edit.Attach(mOID, 0x02);
}

int
PalchWin::read(void)
{
  int size;
  WR_file.GetValue(0, st_file, FILE_NAME_LENGTH,  &size);
  update = FALSE;
  return TRUE;
}

int
PalchWin::write(void)
{
   WR_file.SetValue(0, st_file);
   update = FALSE;
   return TRUE;
}

PalchWin::PalchWin(void)
{
  sprintf(st_file, "0");
}

void
PalchWin::edit(void)
{
  char string[256];

  read();	// get the value from the toolbox
  if (!readvar("Printers$Path", string, 256)||
      !readvar("PrinterChoices$Path", string, 256))
      {
  	  Error(error_construct("Err_PPath"));
  	  return;
      }
  else
      new SpalWin(st_file);
}

int
PalchWin::readvar(char *name, char *value, int valsize)
{
  _kernel_oserror *error;
  _kernel_swi_regs r;

  r.r[0] = (int)name;
  r.r[1] = (int)value;
  r.r[2] = valsize;
  r.r[3] = 0;
  r.r[4] = 0;
  error =  _kernel_swi(OS_ReadVarVal, &r, &r);

  if (error) value[0] = '\0'; else value[r.r[2]] = '\0';

  return (value[0] != '\0');	// return if the string is defined
}
