/*ProfiPacket - packet radio terminal program
  Copyright (C) 1999  Alexander Feigl

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  Author:

  Alexander Feigl
  Burachstraße 51

  D-88250 Weingarten

  Mail : Alexander.Feigl@gmx.de
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#define MonWrap 80

#include "GUIProtos.h"
#include "global.h"
#include "QSO.h"
#include "User.h"
#include "Preferences.h"
#include "Mail.h"
#include "GUIMain.h"
#include "Audio.h"

#include <libraries/mui.h>

#include <devices/timer.h>

#include <exec/lists.h>
#include <exec/memory.h>

#include <dos/exall.h>
#include <dos/dos.h>

#include <graphics/text.h>
#include <graphics/scale.h>


#include <clib/muimaster_protos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/alib_protos.h>
#include <clib/intuition_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>
#include <clib/locale_protos.h>
#include <clib/diskfont_protos.h>

#ifdef __GNUC__


extern void *DOSBase;
extern void *SysBase;
extern void *IntuitionBase;
extern void *UtilityBase;
extern void *LocaleBase;
extern void *GfxBase;
extern void *DiskfontBase;
extern void *MUIMasterBase;

#define NO_INLINE_STDARG
#include <inline/muimaster.h>
#include <inline/dos.h>
#include <inline/exec.h>
#include <inline/intuition.h>
#include <inline/graphics.h>
#include <inline/utility.h>
#include <inline/locale.h>
#include <inline/diskfont.h>
#endif

extern struct Locale *Locale;
extern struct Catalog *Catalog;
extern const char ProfiPacketCatalogName[];
extern struct TextFont *Topaz8;
extern struct TextAttr *Topaz8Attr;

#include "Editor.h"

#ifndef MAKE_ID
#define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
#endif

extern const unsigned char Ver2_Export[];

extern struct Library *KeymapBase;
int MCCEditor_Init(void);
int MCCPEditor_Init(void);


void PACK_DoPoll(void);
void QSO_DoPoll(void);
struct MsgPort *GUI_GetUserPort(void);

int GUI_InitApplication(void);
void GUI_DeleteApplication(void);

void GUI_BuildHardwarePages(void);
void GUI_RefreshMonitorHead(void);



void FillHookA4(void);
void FillHook(struct Hook *MyHook,void (*Function)(),void *FunctionData);

/* Hookfunc: struct Hook *MyHook,void *Param,void *Object,void *Data) */

void GUIMAIN_MenuTrigger(struct Hook *MyHook,void **Param,void *Object,
                         void **Data);


void GUIMAIN_ChannelButton(struct Hook *MyHook,unsigned long *Param,void *Object,
                           unsigned long *Data);

void GUIMAIN_MainKeyHook(struct Hook *MyHook,unsigned long *Param,void *Object,
                           unsigned long *Data);


unsigned long GUIMAIN_HListConstruct(struct Hook *MyHook,unsigned long *Param,void *Object,
                           unsigned long *Data);
unsigned long GUIMAIN_HListDestruct(struct Hook *MyHook,unsigned long *Param,void *Object,
                           unsigned long *Data);

unsigned long GUIMAIN_MainInputStream(struct Hook *MyHook,unsigned long *Param,void *Object,
                           unsigned long *Data);


signed long GUI_ButtonToChannel(unsigned long button);
signed long GUI_ButtonJumpChannel(unsigned long button);


void GUI_CloseMessageWindowReq(void);
void GUI_CloseMessageWindow(void);



void GUI_LoadBuffers(void);
void GUI_SaveBuffers(unsigned long leavemode);

void GUI_DisplayLaunchWindow(void);

void GUIMISC_EncryptionStatus(void);


void *GUI_ObtainWindowPtr(void)
 {
  void *stori;
  if (MainWindow==NULL) return(NULL);

  GetAttr(MUIA_Window_Window,MainWindow,(unsigned long *) &stori);
  return (stori);
 }

void GUI_LowMemory(void)
 {
  DisplayBeep(NULL);
 }


int GUI_InitLowLevel(void)
{
 if (LocaleBase!=NULL)
  {
   Locale=OpenLocale(NULL);
   if (Locale!=NULL)
    {
     Catalog=OpenCatalog(NULL,(char *) ProfiPacketCatalogName,TAG_DONE);    
    }
  }

 InitStandardLanguage();
 if (MUIMasterBase==NULL)
  {
   MUIMasterBase=OpenLibrary("muimaster.library",19);
   if (MUIMasterBase==NULL)
    {
     GUI_Cleanup();
     GUI_GeneralError(374);
     return(-1);
    }
   }
 if (GUI_InitStringSpeedup())
  {
   GUI_Cleanup();
   GUI_GeneralError(188);
   return(-1);
  }
 if (PrepareMenu())
    {
     GUI_Cleanup();
     GUI_GeneralError(125);
     return(-1);
    }
   
 if (HooksP==NULL)
  {
   HooksP=AllocVec(sizeof(*HooksP),MEMF_PUBLIC);
   if (HooksP==NULL) return(-1);
  }

 if (GlobalArraysP==NULL)
  {
   GlobalArraysP=AllocVec(sizeof(*GlobalArraysP),MEMF_PUBLIC);
   if (GlobalArraysP==NULL) return(-1);
  }


 FillHookA4();

 Application=ApplicationObject,
        MUIA_Application_Author,"Alexander Feigl",
        MUIA_Application_Base,"PROFIPACKET",
        /*MUIA_Application_Commands,NULL,        ARexx Commands */
        MUIA_Application_Copyright,"© 1996-1997 by Alexander Feigl",
        MUIA_Application_Description,GUI_String(377),
        /*MUIA_DiskObject,NULL,                  Iconify Object */
        MUIA_Application_HelpFile,GUI_String(474),
        MUIA_Application_Menustrip,MUI_MakeObject(MUIO_MenustripNM,MenuBuild,0),
        /*MUIA_Application_RexxHook,NULL,          RexxHook  */
        MUIA_Application_Title,"ProfiPacket",
        MUIA_Application_Version,Ver2_Export,
        MUIA_HelpNode,"Main",
        End;

 if (Application==NULL) return (-1);

 PrefDataspace=MUI_NewObject(MUIC_Dataspace,End;
 if (PrefDataspace==NULL) return (-1);
 return(0);
}


int GUI_Init(void)
 {
  struct TextAttr t8tf;
  struct TextAttr p8tf;
  
  p8tf.ta_Flags=FPF_ROMFONT|FPF_DISKFONT;
  p8tf.ta_Style=0;
  p8tf.ta_YSize=8;
  p8tf.ta_Name="data/PCFont.font";
  
  t8tf.ta_Style=0;
  t8tf.ta_Flags=FPF_TALLDOT;
  t8tf.ta_YSize=8;
  t8tf.ta_Name="topaz.font";

  if (Topaz8==NULL) 
   {
    Topaz8=OpenFont(&t8tf);
    if (Topaz8==NULL)
     {
      GUI_Cleanup();
      GUI_GeneralError(5);
      return(-1);
     }
   }  
   
  if (PcFont8==NULL)
   {
    BPTR fonlck;
    int ret;
    fonlck=Lock("data/PCFont.font",SHARED_LOCK);
    if (fonlck!=NULL) /*NoPCFont*/
     {
      char filename[512];
      ret=NameFromLock(fonlck,filename,512);
      UnLock(fonlck);
      if (ret==-1)
       {
        p8tf.ta_Name=filename;
        PcFont8=OpenDiskFont(&p8tf);       
       }           
     }
    if (PcFont8==NULL) 
     {
      GUI_GeneralError(39);
      PcFont8=Topaz8;
     }
   }
   {
    int i;
    for (i=0;i<8;++i) HardwareInfo[i]=4;   
   }  
  if (GUI_InitApplication()==0)
   {
    GUI_Cleanup();
    GUI_GeneralError(375);
    return(-1);
   }
  GUI_RefreshWindowTitle();
  GUI_RefreshPacketHead();
  GUI_RefreshMonitorHead();
  return(0);
 }

void GUI_Cleanup(void)

 {
  GUI_DeleteApplication();
  if (MenuBuild!=NULL)
   {
    FreeVec(MenuBuild);
    MenuBuild=NULL;
   }
  if (StringSpeedup!=NULL)
   {
    FreeVec(StringSpeedup);
    StringSpeedup=NULL;
   }
  if (Catalog!=NULL)
   {
    CloseCatalog(Catalog);
    Catalog=NULL;
   }
  if (Locale!=NULL)
   {
    CloseLocale(Locale);
    Locale=NULL;
   }
  if ( (PcFont8!=NULL) && (PcFont8!=Topaz8) )
   {
    CloseFont(PcFont8);
   }
  PcFont8=NULL;
  if (Topaz8!=NULL)
   {
    CloseFont(Topaz8);
   }
  if (MUIMasterBase!=NULL)
   {
    CloseLibrary(MUIMasterBase);
   }
 }

int GUI_InitApplication(void)
{

 FillHook(&HooksP->AppMenuHook,(void *) GUIMAIN_MenuTrigger,0);
 FillHook(&HooksP->MainChannelBut,(void *) GUIMAIN_ChannelButton,0);
 FillHook(&HooksP->HListConstruct,(void *) GUIMAIN_HListConstruct,0);
 FillHook(&HooksP->HListDestruct,(void *) GUIMAIN_HListDestruct,0);
 FillHook(&HooksP->MainInputStream,(void *) GUIMAIN_MainInputStream,0);
 FillHook(&HooksP->MainKeyHook,(void *) GUIMAIN_MainKeyHook,0);

 if (!MCCEditor_Init()) return(FALSE);
 if (!MCCPEditor_Init()) return(FALSE);

 GUI_BuildHardwarePages();

 MainWindow =
                 WindowObject,
                     MUIA_Window_ID   , MAKE_ID('M','A','I','N'),

                     WindowContents,

                       GlobalArraysP->MainObjects[29]=VGroup,

                         Child,GlobalArraysP->MainObjects[31]=VGroup,
                               Child,GlobalArraysP->MainObjects[0]=HGroup,

                               Child,GlobalArraysP->MainObjects[3]=TextObject,
                                     MUIA_Text_SetMin,FALSE,
                                     MUIA_Background,MUII_ButtonBack,
                                     MUIA_Frame,MUIV_Frame_Button,
                                     MUIA_InputMode,MUIV_InputMode_Toggle,
                                     End,
                               Child,GlobalArraysP->MainObjects[4]=TextObject,
                                     MUIA_Text_SetMin,FALSE,
                                     MUIA_Background,MUII_ButtonBack,
                                     MUIA_Frame,MUIV_Frame_Button,
                                     MUIA_InputMode,MUIV_InputMode_Toggle,
                                     End,
                               Child,GlobalArraysP->MainObjects[5]=TextObject,
                                     MUIA_Text_SetMin,FALSE,
                                     MUIA_Background,MUII_ButtonBack,
                                     MUIA_Frame,MUIV_Frame_Button,
                                     MUIA_InputMode,MUIV_InputMode_Toggle,
                                     End,
                               Child,GlobalArraysP->MainObjects[6]=TextObject,
                                     MUIA_Text_SetMin,FALSE,
                                     MUIA_Background,MUII_ButtonBack,
                                     MUIA_Frame,MUIV_Frame_Button,
                                     MUIA_InputMode,MUIV_InputMode_Toggle,
                                     End,
                               Child,GlobalArraysP->MainObjects[7]=TextObject,
                                     MUIA_Text_SetMin,FALSE,
                                     MUIA_Background,MUII_ButtonBack,
                                     MUIA_Frame,MUIV_Frame_Button,
                                     MUIA_InputMode,MUIV_InputMode_Toggle,
                                     End,
                               Child,GlobalArraysP->MainObjects[8]=TextObject,
                                     MUIA_Text_SetMin,FALSE,
                                     MUIA_Background,MUII_ButtonBack,
                                     MUIA_Frame,MUIV_Frame_Button,
                                     MUIA_InputMode,MUIV_InputMode_Toggle,
                                     End,
                               Child,GlobalArraysP->MainObjects[9]=TextObject,
                                     MUIA_Text_SetMin,FALSE,
                                     MUIA_Background,MUII_ButtonBack,
                                     MUIA_Frame,MUIV_Frame_Button,
                                     MUIA_InputMode,MUIV_InputMode_Toggle,
                                     End,
                               Child,GlobalArraysP->MainObjects[10]=TextObject,
                                     MUIA_Text_SetMin,FALSE,
                                     MUIA_Background,MUII_ButtonBack,
                                     MUIA_Frame,MUIV_Frame_Button,
                                     MUIA_InputMode,MUIV_InputMode_Toggle,
                                     End,
                               Child,GlobalArraysP->MainObjects[11]=TextObject,
                                     MUIA_Text_SetMin,FALSE,
                                     MUIA_Background,MUII_ButtonBack,
                                     MUIA_Frame,MUIV_Frame_Button,
                                     MUIA_InputMode,MUIV_InputMode_Toggle,
                                     End,
                               Child,GlobalArraysP->MainObjects[12]=TextObject,
                                     MUIA_Text_SetMin,FALSE,
                                     MUIA_Background,MUII_ButtonBack,
                                     MUIA_Frame,MUIV_Frame_Button,
                                     MUIA_InputMode,MUIV_InputMode_Toggle,
                                     End,
                               Child,GlobalArraysP->MainObjects[16]=PopobjectObject,
                                     MUIA_Weight,50,
                                     MUIA_Popstring_Button,PopButton(MUII_PopUp),

                                     MUIA_Popobject_Object,GlobalArraysP->MainObjects[17]=
                                                           ListviewObject,
                                                           MUIA_Listview_List,GlobalArraysP->MainObjects[18]=
                                                                ListObject,
                                                              InputListFrame,
                                                              MUIA_List_SourceArray,GlobalArraysP->HardwarePages,
                                                              MUIA_List_AdjustWidth,TRUE,
                                                              MUIA_List_AutoVisible,TRUE,
                                                              End,


                                                           End,
                                     MUIA_Popobject_StrObjHook,&HooksP->HListConstruct,
                                     MUIA_Popobject_ObjStrHook,&HooksP->HListDestruct,
                                     End,

                               End,

                         Child,GlobalArraysP->MainObjects[13]=EditorObject,
                               MUIA_Frame,MUIV_Frame_String,
                               MUIA_Font,PcFont8,
                               MUIA_Editor_EditorType,0,
                               MUIA_Editor_InputMask,1801+2048,
                               MUIA_Editor_FreeMoving,TRUE,
                               MUIA_Editor_GarbageEditor,TRUE,
                               MUIA_Editor_AskInputStream,TRUE,
                               MUIA_Editor_WordWrap,79,
                               MUIA_Editor_LineWrap,FALSE,
                               MUIA_Editor_PCFontKeymap,TRUE,
                               MUIA_Editor_Pages,MaxChannel+1,
                               MUIA_Editor_BufferSize,Preferences.EditBuffersize*1024,
                               MUIA_Editor_NumLockStatus,!Preferences.NumLock,
                               MUIA_Editor_InsertMode,!Preferences.InsertMode,
                               MUIA_Editor_NotifyPrPa,TRUE,
                               MUIA_Editor_Unprintable,0x0c000000,
                               End,

                           End,

                         /* QSO section */

                         Child,GlobalArraysP->MainObjects[30]=VGroup,
                           MUIA_ShowMe,FALSE,
                         Child,GlobalArraysP->MainObjects[1]=TextObject,
                               MUIA_Text_SetMin,FALSE,
                               MUIA_Background,MUII_TextBack,
                               MUIA_Frame,MUIV_Frame_Text,
                               MUIA_Font,MUIV_Font_Fixed,
                               End,
                         Child,VGroup,
                                 Child,GlobalArraysP->MainObjects[19]=ColGroup(2),
                                         /* QSO filetransfer */
                                         MUIA_ShowMe,FALSE,
                                         MUIA_Frame,MUIV_Frame_Text,
                                         MUIA_Background,MUII_TextBack,
                                         Child,GlobalArraysP->MainObjects[20]=TextObject,
                                                 MUIA_Font,MUIV_Font_Fixed,
                                                 MUIA_Text_SetMin,FALSE,
                                                 End,
                                         Child,GlobalArraysP->MainObjects[21]=TextObject,
                                                 MUIA_Font,MUIV_Font_Fixed,
                                                 MUIA_Text_SetMin,FALSE,
                                                 End,
                                         Child,GlobalArraysP->MainObjects[22]=TextObject,
                                                 MUIA_Font,MUIV_Font_Fixed,
                                                 MUIA_Text_SetMin,FALSE,
                                                 End,
                                         Child,GlobalArraysP->MainObjects[23]=TextObject,
                                                 MUIA_Font,MUIV_Font_Fixed,
                                                 MUIA_Text_SetMin,FALSE,
                                                 End,
                                         Child,GlobalArraysP->MainObjects[24]=TextObject,
                                                 MUIA_Font,MUIV_Font_Fixed,
                                                 MUIA_Text_SetMin,FALSE,
                                                 End,
                                         Child,GlobalArraysP->MainObjects[25]=TextObject,
                                                 MUIA_Font,MUIV_Font_Fixed,
                                                 MUIA_Text_SetMin,FALSE,
                                                 End,
                                         Child,GlobalArraysP->MainObjects[26]=GaugeObject,
                                                       MUIA_Frame,MUIV_Frame_Gauge,
                                                       MUIA_Font,MUIV_Font_Fixed,
                                                       MUIA_Gauge_InfoText,"%ld %%",
                                                       MUIA_Gauge_Horiz,TRUE,
                                                       End,
                                         Child,GlobalArraysP->MainObjects[27]=TextObject,
                                                 MUIA_Font,MUIV_Font_Fixed,
                                                 MUIA_Text_SetMin,FALSE,
                                                 End,

                                         End,
                                 Child,GlobalArraysP->MainObjects[14]=EditorObject,
                                       MUIA_Frame,MUIV_Frame_ReadList,
                                       MUIA_Font,PcFont8,
                                       MUIA_Editor_EditorType,1,
                                       MUIA_Editor_InputMask,14+2048,
                                       MUIA_Editor_GarbageEditor,TRUE,
                                       MUIA_Editor_Pages,MaxChannel+MaxSpy,
                                       MUIA_Editor_BufferSize,Preferences.QSOBuffersize*1024,
                                       MUIA_Editor_NotifyPrPa,TRUE,
                                       MUIA_Editor_PCFontKeymap,TRUE,
                                       MUIA_Editor_Unprintable,0x0c000000,
                                       End,
                                 End,

                           End,
                         Child,GlobalArraysP->MainObjects[32]=VGroup,
                         Child,GlobalArraysP->MainObjects[2]=TextObject,
                               MUIA_Font,MUIV_Font_Fixed,
                               MUIA_Text_SetMin,FALSE,
                               MUIA_Background,MUII_TextBack,
                               MUIA_Frame,MUIV_Frame_Text,
                               End,

                         Child,GlobalArraysP->MainObjects[15]=EditorObject,
                               MUIA_Frame,MUIV_Frame_ReadList,
                               MUIA_Font,PcFont8,
                               MUIA_Editor_EditorType,2,
                               MUIA_Editor_InputMask,14+2048,
                               MUIA_Editor_GarbageEditor,TRUE,
                               MUIA_Editor_Pages,1,
                               MUIA_Editor_BufferSize,Preferences.MonitorBuffersize*1024,
                               MUIA_Editor_NotifyPrPa,TRUE,
                               MUIA_Editor_PCFontKeymap,TRUE,
                               MUIA_Editor_Unprintable,0x0,
                               End,
                         End,
                       End,
                     End;

 if (MainWindow==NULL)
     {
      GUI_LowMemory();
      return(FALSE);
     }


 DoMethod(Application,OM_ADDMEMBER,MainWindow);


 DoMethod(MainWindow,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
             Application,3,MUIM_Set,MUIA_Application_Iconified,TRUE);


 DoMethod(Application,MUIM_Notify,MUIA_Application_MenuAction,MUIV_EveryTime,
             Application,3,MUIM_CallHook,&HooksP->AppMenuHook,MUIV_TriggerValue);


 DoMethod(GlobalArraysP->MainObjects[18],MUIM_Notify,MUIA_List_Active,
          MUIV_EveryTime,GlobalArraysP->MainObjects[16],
             2,MUIM_Popstring_Close,TRUE);

 DoMethod(GlobalArraysP->MainObjects[13],MUIM_Notify,MUIA_Editor_NotifyStream,
          MUIV_EveryTime,GlobalArraysP->MainObjects[13],
             3,MUIM_CallHook,&HooksP->MainInputStream,MUIV_TriggerValue);


 DoMethod(GlobalArraysP->MainObjects[13],MUIM_Notify,
          MUIA_Editor_NumLockStatus,MUIV_EveryTime,
          GlobalArraysP->MainObjects[13],4,MUIM_CallHook,&HooksP->MainKeyHook,
          100,MUIV_TriggerValue);
 DoMethod(GlobalArraysP->MainObjects[14],MUIM_Notify,
          MUIA_Editor_NumLockStatus,MUIV_EveryTime,
          GlobalArraysP->MainObjects[14],4,MUIM_CallHook,&HooksP->MainKeyHook,
          100,MUIV_TriggerValue);
 DoMethod(GlobalArraysP->MainObjects[15],MUIM_Notify,
          MUIA_Editor_NumLockStatus,MUIV_EveryTime,
          GlobalArraysP->MainObjects[15],4,MUIM_CallHook,&HooksP->MainKeyHook,
          100,MUIV_TriggerValue);

 DoMethod(GlobalArraysP->MainObjects[13],MUIM_Notify,
          MUIA_Editor_NotifyFKey,MUIV_EveryTime,
          GlobalArraysP->MainObjects[13],4,MUIM_CallHook,&HooksP->MainKeyHook,
          103,MUIV_TriggerValue);
 DoMethod(GlobalArraysP->MainObjects[14],MUIM_Notify,
          MUIA_Editor_NotifyFKey,MUIV_EveryTime,
          GlobalArraysP->MainObjects[14],4,MUIM_CallHook,&HooksP->MainKeyHook,
          103,MUIV_TriggerValue);
 DoMethod(GlobalArraysP->MainObjects[15],MUIM_Notify,
          MUIA_Editor_NotifyFKey,MUIV_EveryTime,
          GlobalArraysP->MainObjects[15],4,MUIM_CallHook,&HooksP->MainKeyHook,
          103,MUIV_TriggerValue);

 DoMethod(GlobalArraysP->MainObjects[13],MUIM_Notify,
          MUIA_Editor_NotifyPrPa,MUIV_EveryTime,
          GlobalArraysP->MainObjects[13],4,MUIM_CallHook,&HooksP->MainKeyHook,
          104,MUIV_TriggerValue);
 DoMethod(GlobalArraysP->MainObjects[14],MUIM_Notify,
          MUIA_Editor_NotifyPrPa,MUIV_EveryTime,
          GlobalArraysP->MainObjects[14],4,MUIM_CallHook,&HooksP->MainKeyHook,
          104,MUIV_TriggerValue);
 DoMethod(GlobalArraysP->MainObjects[15],MUIM_Notify,
          MUIA_Editor_NotifyPrPa,MUIV_EveryTime,
          GlobalArraysP->MainObjects[15],4,MUIM_CallHook,&HooksP->MainKeyHook,
          104,MUIV_TriggerValue);


 DoMethod(GlobalArraysP->MainObjects[13],MUIM_Notify,
          MUIA_Editor_InsertMode,MUIV_EveryTime,
          GlobalArraysP->MainObjects[13],4,MUIM_CallHook,&HooksP->MainKeyHook,
          101,MUIV_TriggerValue);

 DoMethod(GlobalArraysP->MainObjects[13],MUIM_Notify,
          MUIA_Editor_AltAsciiMode,MUIV_EveryTime,
          GlobalArraysP->MainObjects[13],4,MUIM_CallHook,&HooksP->MainKeyHook,
          102,MUIV_TriggerValue);


 {
  unsigned long i;
  for (i=0;i<10;++i)
   {
    DoMethod(GlobalArraysP->MainObjects[3+i],MUIM_Notify,MUIA_Selected,
             MUIV_EveryTime,GlobalArraysP->MainObjects[3+i],4,
             MUIM_CallHook,&HooksP->MainChannelBut,MUIV_TriggerValue,i);
   }
 }

 AltASCII=FALSE;

 set (GlobalArraysP->MainObjects[13],MUIA_Editor_NumLockStatus,Preferences.NumLock);
 set (GlobalArraysP->MainObjects[13],MUIA_Editor_InsertMode,Preferences.InsertMode);


 GUI_LoadBuffers();

 TimerPort = CreateMsgPort();
 if (TimerPort==NULL)
  {
   TimerPort=NULL;
   TimerRequest=NULL;
   MUI_DisposeObject(Application);
   Application=NULL;
   return(FALSE);
  }
 TimerRequest = CreateIORequest(TimerPort,sizeof(struct timerequest));
 if (TimerRequest==NULL)
  {
   DeleteMsgPort(TimerPort);
   TimerPort=NULL;
   TimerRequest=NULL;
   MUI_DisposeObject(Application);
   Application=NULL;
   return (FALSE);
  }
 if (OpenDevice("timer.device",UNIT_VBLANK,
                (struct IORequest *)TimerRequest,0)!=0)
  {
   DeleteIORequest(TimerRequest);
   DeleteMsgPort(TimerPort);
   TimerPort=NULL;
   TimerRequest=NULL;
   MUI_DisposeObject(Application);
   Application=NULL;
   return (FALSE);
  }

 TimerRequest->tr_node.io_Command = TR_ADDREQUEST;
 TimerRequest->tr_time.tv_secs    = 0;
 TimerRequest->tr_time.tv_micro   = 40;
 SendIO((struct IORequest *)TimerRequest);

 set (MainWindow,MUIA_Window_Open,TRUE);

 GUI_DisplayLaunchWindow();
 
 return (TRUE);
}

void GUI_DeleteApplication(void)
{
 if (TimerRequest!=NULL)
  {
   if (!CheckIO((struct IORequest *) TimerRequest)) 
       AbortIO((struct IORequest *) TimerRequest);
   WaitIO((struct IORequest *) TimerRequest);
   CloseDevice((struct IORequest *)TimerRequest);
   DeleteIORequest(TimerRequest);
   DeleteMsgPort(TimerPort);
   TimerPort=NULL;
   TimerRequest=NULL;
  }

 if (Application)
    {
     GUI_SaveBuffers(TRUE);
     MUI_DisposeObject(Application);
     Application=NULL;
     MainWindow=NULL;
    }

 if (PrefDataspace)
    {
     MUI_DisposeObject(PrefDataspace);
     PrefDataspace=NULL;
    }

 if (EditorClass!=NULL)
    {
     MUI_DeleteCustomClass(EditorClass);
     EditorClass=NULL;
    }
 if (EditorPrefClass!=NULL)
    {
     MUI_DeleteCustomClass(EditorPrefClass);
     EditorPrefClass=NULL;
    }

 if (HooksP!=NULL)
  {
   FreeVec(HooksP);
   HooksP=NULL;
  }
 if (GlobalArraysP!=NULL)
  {
   FreeVec(GlobalArraysP);
   GlobalArraysP=NULL;
  }

}


void GUI_ExitPP(int forceleave)
{
 int servers;
 unsigned long i;

 servers=FALSE;
 if (!forceleave) servers=GUI_CheckLeaveProg();
 if (servers) return;


 MAIN_ExecuteARexxScript("ProfiPacket_Stop",NULL,FALSE);
 for (i=1;i<=MaxChannel;++i)
  {
   QSO_ForceTransmit(i);
  }
 QSO_SaveHoldings();
 Clean_Up();
}


void GUI_DoPoll(void)
 {
  if (CurrentDateStamp.ds_Minute!=OldTimeMins)
   {
    OldTimeMins=CurrentDateStamp.ds_Minute;
    if (CurrentDateStamp.ds_Minute==0)
     {
      PACK_UpdateTNCTime();
      MAIL_DailyHousekeeping();
     }
    if ((MAIL_TestForMail(NULL) )/65536 )
     {
      int period,percode;

      percode=Preferences.SysopCallPeriod;
      if (percode!=0)
       {
        period=15<<(percode-1);
        if (CurrentDateStamp.ds_Minute%period==0)
         {
          AUDIO_Effect(10,NULL);
         }
       }
     }
    MAIN_NewMinute();
    GUI_RefreshWindowTitle();
   }
  if (QSO_UpdateTickerLine(TickerLine))
   {
    GUI_RefreshMonitorHead();
   }
  if (MessageWindow!=NULL)
   {
    if ( (CurrentDateStamp.ds_Tick/50)!=MessageWindowSecs)
     {
      MessageWindowSecs=CurrentDateStamp.ds_Tick/50;
      MessageWindowTime--;
      if (MessageWindowTime<=0)
       {
        GUI_CloseMessageWindow();
       }
     }
   }
 }

int GUI_MainPoll(unsigned long disablegui)
 {
  unsigned long sig_wait;
  unsigned long sig_receive;
  unsigned long mui_signal;
  unsigned long timeddone;
  unsigned long MUISig;


  timeddone=FALSE;

  mui_signal=MUISignal;
  
  sig_wait= mui_signal | 1<<TimerPort->mp_SigBit |
           SIGBREAKF_CTRL_C;  
 
  sig_receive=Wait(sig_wait);
  
  if ( (sig_receive & mui_signal) || ( (mui_signal==0) && (disablegui==FALSE) ) )
   {
    do {
        unsigned long rc;
        MUISig=sig_receive & mui_signal;
        rc=DoMethod(Application,MUIM_Application_NewInput,&MUISig);
        MUISignal=MUISig;

        if (rc==MUIV_Application_ReturnID_Quit)
         {
          unsigned long forceme;
          GetAttr(MUIA_Application_ForceQuit,Application,&forceme);
          GUI_ExitPP(forceme);
         }
       } while (MUISignal==0);
    
   }
  
   
  if (sig_receive & SIGBREAKF_CTRL_C )
   {
    GUI_ExitPP(TRUE);
   }
  if (CheckIO((struct IORequest *) TimerRequest))
   {
    DateStamp(&CurrentDateStamp);
    WaitIO((struct IORequest *) TimerRequest);
    SendIO((struct IORequest *) TimerRequest);
    ++GUIPollCounter;
    ++PacketPollCounter;
    ++QSOPollCounter;
    ++MailPollCounter;
    timeddone=TRUE;
    if (GUIPollCounter>=25)
     {
      GUIPollCounter=0;
      GUI_DoPoll();
     }
    if (PacketPollCounter>=2)
     {
      PacketPollCounter=0;
      PACK_DoPoll();
     }
    if (QSOPollCounter>=5)
     {
      QSOPollCounter=0;
      QSO_DoPoll();
     }
    if (MailPollCounter>=25)
     {
      MailPollCounter=0;
      MAIL_DoPoll();
     }
   }
                   
           

 return (timeddone);
}


void GUIMAIN_MenuTrigger(struct Hook *MyHook,void **Param,void *Object,
                         void **Data)
{
 void *MyFunction;

 MyFunction=*Param;
 if (MyFunction==NULL) return;

 CallMenu(MyFunction);

 return;

}

void GUIMAIN_ChannelButton(struct Hook *MyHook,unsigned long *Param,void *Object,
                           unsigned long *Data)
{
 signed long i;

 if (*(Param)==FALSE)
  {
   SetAttrs(Object,MUIA_Selected,TRUE,MUIA_NoNotify,TRUE,TAG_DONE);
  }

 i=GUI_ButtonToChannel(*(Param+1));

 if (i==ActiveChannel) i=GUI_ButtonJumpChannel(*(Param+1));


 GUI_ChangeChannel(i);

}

void GUIMAIN_MainKeyHook(struct Hook *MyHook,unsigned long *Param,void *Object,
                           unsigned long *Data)
{
 signed long par,par2;
 par=*(Param);
 par2=*(Param+1);
 switch (par)
  {
   case 100:
     NumLock=par2;
     nnset (GlobalArraysP->MainObjects[13],MUIA_Editor_NumLockStatus,NumLock);
     nnset (GlobalArraysP->MainObjects[14],MUIA_Editor_NumLockStatus,NumLock);
     nnset (GlobalArraysP->MainObjects[15],MUIA_Editor_NumLockStatus,NumLock);
     GUI_RefreshWindowTitle();
     break;
   case 101:
     InsertMode=par2;
     GUI_RefreshWindowTitle();
     break;
   case 102:
     AltASCII=par2;
     GUI_RefreshWindowTitle();
     break;
   case 103:
     {
      signed long i;
      i=GUI_ButtonToChannel(par2);
      if (i==ActiveChannel) i=GUI_ButtonJumpChannel(par2);
      GUI_ChangeChannel(i);
      break;
     }
   case 104:
     {
      switch (par2)
       {
        case 1:
          {
           signed long StartChannel;
           struct QSO *ThisQSO;
           struct Spy *ThisSpy;
           StartChannel=ActiveChannel;

           while (1)
            {
             if (StartChannel>0)
              {
               ThisQSO=(QSOs+(StartChannel-1));	
               if ( (ThisQSO->SkipChannel==FALSE) &&
                    (ThisQSO->IncomingMessage!=FALSE) ) break;
               StartChannel++;
               if (StartChannel>MaxChannel) StartChannel=-1;
               if ( (StartChannel==-1) && (MaxSpy==0) ) StartChannel=1;
               if (StartChannel==ActiveChannel) break;
               if ( (ActiveChannel==0) && (StartChannel==1) ) break;
              }
              else
              {
               if (StartChannel<0)
                {

                 ThisSpy=(struct Spy *)(((struct QSO *) Spys)+(-StartChannel-1));
                 if ((ThisSpy->SkipChannel==FALSE) &&
                     (ThisSpy->IncomingMessage!=FALSE) ) break;

                 StartChannel--;
                 if (StartChannel< (-MaxSpy) ) StartChannel=1;
                 if (StartChannel==ActiveChannel) break;
                 if ( (ActiveChannel==0) && (StartChannel==1) ) break;
                }
                else
                {
                 StartChannel=1;
                }
              }

            }
           if (StartChannel!=ActiveChannel) GUI_ChangeChannel(StartChannel);
           break;
          }
        case 2:
          if (ActiveChannel!=0)
            {
             GUI_ChangeChannel(0);
            }
            else
            {
             GUI_ChangeChannel(MonitorSaveChannel);
            }
          break;
       }
      break;
     }
  }
}


unsigned long GUIMAIN_HListConstruct(struct Hook *MyHook,unsigned long *Param,void *Object,
                           unsigned long *Data)

{
 signed long cass;
 signed long group,i;
 cass=0;

 group=0;
 if (ActiveChannel>0)
  {
   group=(((ActiveChannel-1)/10)*10)+1;

  }
 if (ActiveChannel<0)
  {
   group=-(((-ActiveChannel-1)/10)*10)-1;
  }

 for (i=0;i<40;++i)
  {
   if (group==GlobalArraysP->HardAssigns[i])
    {
     cass=i;
     break;
    }
   if (GlobalArraysP->HardAssigns[i]==0) break;
  }

 SetAttrs(GlobalArraysP->MainObjects[18],MUIA_List_Active,cass,
          MUIA_NoNotify,TRUE,TAG_DONE);
 return (TRUE);
}

unsigned long GUIMAIN_HListDestruct(struct Hook *MyHook,unsigned long *Param,void *Object,
                           unsigned long *Data)
{
 signed long cass,sass;
 signed long group,i;
 cass=0;

 group=0;
 if (ActiveChannel>0)
  {
   group=(((ActiveChannel-1)/10)*10)+1;

  }
 if (ActiveChannel<0)
  {
   group=-(((-ActiveChannel-1)/10)*10)-1;
  }

 for (i=0;i<40;++i)
  {
   if (group==GlobalArraysP->HardAssigns[i])
    {
     cass=i;
     break;
    }
   if (GlobalArraysP->HardAssigns[i]==0) break;
  }

 GetAttr(MUIA_List_Active,GlobalArraysP->MainObjects[18],(ULONG *) &sass);
 if (sass==cass) return (TRUE);

 GUI_ChangeChannel(GlobalArraysP->HardMemory[sass]);
 return (TRUE);
}


void GUI_ExitProg(void)
{
 DoMethod(Application,MUIM_Application_ReturnID,
          MUIV_Application_ReturnID_Quit);
 return;
}

void GUI_ChangeChannel(signed long Channel)
{

 unsigned short redofile;
 unsigned short changestate;

 signed long oldchannel;

 oldchannel=ActiveChannel;

 redofile=FALSE;
 changestate=FALSE;
 if (Channel==ActiveChannel) return;

 if ( (Channel>0) && (Channel>MaxChannel) ) return;
 if ( (Channel<0) && ((-Channel)>MaxSpy) ) return;

 if (ActiveChannel!=0)
  {
   signed long cass;
   signed long group,i;
   cass=0;
   group=0;

   if (ActiveChannel>0)
    {
     group=(((ActiveChannel-1)/10)*10)+1;
    }

   if (ActiveChannel<0)
    {
     group=-(((-ActiveChannel-1)/10)*10)-1;
    }

   for (i=0;i<40;++i)
    {
     if (group==GlobalArraysP->HardAssigns[i])
      {
       cass=i;
       break;
      }
     if (GlobalArraysP->HardAssigns[i]==0) break;
    }

   GlobalArraysP->HardMemory[cass]=ActiveChannel;
  }
 if ( (Channel==0) && (ActiveChannel!=0) )
  {
   MonitorSaveChannel=ActiveChannel;
  }

 ActiveChannel=Channel;




 if ( ( (ActiveChannel>0) && (oldchannel<=0) ) ||
        (ActiveChannel==0) ||
        ((ActiveChannel<0) && (oldchannel>=0) )  )

   {
    DoMethod(GlobalArraysP->MainObjects[29],MUIM_Group_InitChange);
    changestate=TRUE;
    if (ActiveChannel==0)
     {
      SetAttrs(GlobalArraysP->MainObjects[13],MUIA_ShowMe,TRUE,TAG_DONE);
      SetAttrs(GlobalArraysP->MainObjects[31],MUIA_VertWeight,Preferences.MonEditWeight,TAG_DONE);
      SetAttrs(GlobalArraysP->MainObjects[30],MUIA_ShowMe,FALSE,TAG_DONE);
      SetAttrs(GlobalArraysP->MainObjects[15],MUIA_ShowMe,TRUE,TAG_DONE);
      SetAttrs(GlobalArraysP->MainObjects[32],MUIA_VertWeight,Preferences.MonMonitorWeight,TAG_DONE);
      GUIMISC_EncryptionStatus();
     }

    if ( (ActiveChannel>0) && (!(ActiveChannel<0)))
     {
      SetAttrs(GlobalArraysP->MainObjects[13],MUIA_ShowMe,TRUE,TAG_DONE);
      SetAttrs(GlobalArraysP->MainObjects[31],MUIA_VertWeight,Preferences.EditWeight,TAG_DONE);
      SetAttrs(GlobalArraysP->MainObjects[30],MUIA_ShowMe,TRUE,TAG_DONE);
      SetAttrs(GlobalArraysP->MainObjects[15],MUIA_ShowMe,Preferences.MonitorMode,
               TAG_DONE);
      SetAttrs(GlobalArraysP->MainObjects[32],MUIA_VertWeight,Preferences.MonitorWeight,TAG_DONE);
     }

    if (ActiveChannel<0)
     {
      SetAttrs(GlobalArraysP->MainObjects[13],MUIA_ShowMe,FALSE,TAG_DONE);
      SetAttrs(GlobalArraysP->MainObjects[31],MUIA_VertWeight,Preferences.EditWeight,TAG_DONE);
      SetAttrs(GlobalArraysP->MainObjects[30],MUIA_ShowMe,TRUE,TAG_DONE);
      SetAttrs(GlobalArraysP->MainObjects[15],MUIA_ShowMe,Preferences.MonitorMode,
               TAG_DONE);
      SetAttrs(GlobalArraysP->MainObjects[32],MUIA_VertWeight,Preferences.MonitorWeight,TAG_DONE);
      SetAttrs(GlobalArraysP->MainObjects[19],MUIA_ShowMe,FALSE,TAG_DONE);
      GUIMISC_EncryptionStatus();
     }

    SetAttrs(GlobalArraysP->MainObjects[13],MUIA_Editor_DisableRedraw,TRUE,TAG_DONE);
    SetAttrs(GlobalArraysP->MainObjects[14],MUIA_Editor_DisableRedraw,TRUE,TAG_DONE);
    SetAttrs(GlobalArraysP->MainObjects[15],MUIA_Editor_DisableRedraw,TRUE,TAG_DONE);
   }

 if ( (ActiveChannel>0) && (!(ActiveChannel<0)))
  {
   struct QSO *ThisQSO;
   ThisQSO=QSOs+(Channel-1);
   SetAttrs(GlobalArraysP->MainObjects[19],MUIA_ShowMe,ThisQSO->TransferMode!=0,
               TAG_DONE);
   ThisQSO->IncomingMessage=FALSE;
   if (ThisQSO->TransferMode!=0) redofile=TRUE;
  }


 if (ActiveChannel<0)
  {
   struct QSO *ThisXQSO;
   struct Spy *ThisSpy;
   ThisXQSO=((struct QSO *) Spys)+ (-Channel-1);
   ThisSpy=((struct Spy *) ThisXQSO);
   ThisSpy->IncomingMessage=FALSE;
   ThisSpy->Unused1=FALSE; /*Optimizer error buxfix*/

  }



 if (changestate) DoMethod(GlobalArraysP->MainObjects[29],MUIM_Group_ExitChange);

 if (ActiveChannel==0)
  {
   SetAttrs(GlobalArraysP->MainObjects[13],MUIA_Editor_ActivePage,MaxChannel,TAG_DONE);
  }

 if ( (ActiveChannel>0) && (!(ActiveChannel<0)) )
  {
   SetAttrs(GlobalArraysP->MainObjects[13],MUIA_Editor_ActivePage,Channel-1,TAG_DONE);
   SetAttrs(GlobalArraysP->MainObjects[14],MUIA_Editor_ActivePage,Channel-1,TAG_DONE);
  }



 if (ActiveChannel<0)
  {
   SetAttrs(GlobalArraysP->MainObjects[14],MUIA_Editor_ActivePage,MaxChannel-1-
                         Channel,TAG_DONE);
  }


 if (changestate)
   {
    SetAttrs(GlobalArraysP->MainObjects[13],MUIA_Editor_DisableRedraw,FALSE,TAG_DONE);
    SetAttrs(GlobalArraysP->MainObjects[14],MUIA_Editor_DisableRedraw,FALSE,TAG_DONE);
    SetAttrs(GlobalArraysP->MainObjects[15],MUIA_Editor_DisableRedraw,FALSE,TAG_DONE);
   }

 GUI_RefreshPacketHead();
 GUI_RefreshConnectHead();

 if (redofile==TRUE) GUI_RedisplayFileTransfer(ActiveChannel);
}


void GUI_RefreshMonitorHead(void)
{
 unsigned char XTickerLine[256];
 unsigned char *Source;
 unsigned char *Dest;
 
 if (MainWindow==NULL) return;
 
 Source=GUI_String(20);
 Dest=XTickerLine;
 while ( *(Source)!='\0')
  {
   *(Dest++)=*(Source++);
  }
 Source=TickerLine;
 while ( *(Source)!='\0')
  {
   *(Dest++)=*(Source++);
  }

 *(Dest)='\0';

 SetAttrs(GlobalArraysP->MainObjects[2],MUIA_Text_Contents,(ULONG) XTickerLine,
                                     TAG_DONE);
}



void GUI_RefreshConnectHead(void)
 {
  unsigned char CHeadLine[256];
  struct QSO *ThisQSO;
  struct Spy *ThisSpy;

  unsigned char *Source,*Dest;

  if (MainWindow==NULL) return;
  
  Dest=CHeadLine;

  if (ActiveChannel==0) return;

  if (ActiveChannel>0)
   {
    ThisQSO=QSOs+ActiveChannel-1;

    /* Display channel number */

    Dest=GUI_LongToString(Dest,ActiveChannel,10,1);
    *(Dest++)=':';

    /* Display channel call */

    if (ThisQSO->Call[0]!='-')
     {
      signed long i;
      i=6;
      Source=ThisQSO->Call;
      while ( (*(Source)!='\0') && (*(Source)!='-') )
              {
               --i;
               *(Dest++)=*(Source++);
              }
      while ( i>0)
       {
        --i;
        *(Dest++)=' ';
       }
     }
     else
     {
      *(Dest++)='D';
      *(Dest++)='i';
      *(Dest++)='s';
      *(Dest++)='c';
      *(Dest++)='o';
      *(Dest++)='n';
     }
    *(Dest++)=' ';

    /* Display channel name */

    {
     unsigned long i;
     i=21;
     Source=ThisQSO->Name;
     while ( (*(Source)!='\0') && (i!=0) )
      {
       --i;
       *(Dest++)=*(Source++);
      }
     while (i>0)
             {
              *(Dest++)=' ';
              --i;
             }


    }

    /* Display channel huffman/crypt */

    if ( ( ((ThisQSO->CompressedMode &1)<<1) ^
           (ThisQSO->CompressedMode &2) ) !=0 )
     {
      *(Dest++)='h';
     }
     else
     {
      *(Dest++)=((ThisQSO->CompressedMode&1)==0)?' ':'H';
     }

    if ( ( ((ThisQSO->CompressedMode &4)<<1) ^
           (ThisQSO->CompressedMode &8) ) !=0)
     {
      *(Dest++)='c';
     }
     else
     {
      *(Dest++)=((ThisQSO->CompressedMode&4)==0)?' ':'C';
     }

    if ( ( ((ThisQSO->CompressedMode &16)<<1) ^
           (ThisQSO->CompressedMode &32) ) !=0)
     {
      *(Dest++)='x';
     }
     else
     {
      *(Dest++)=((ThisQSO->CompressedMode&16)==0)?' ':'X';
     }


    *(Dest++)=' ';

    /* Display N U T*/

    *(Dest++)='N';
    Dest=GUI_LongToString(Dest,ThisQSO->NotSent,100,2);
    *(Dest++)=' ';
    *(Dest++)='U';
    Dest=GUI_LongToString(Dest,ThisQSO->Unack,100,2);
    *(Dest++)=' ';
    *(Dest++)='T';
    Dest=GUI_LongToString(Dest,ThisQSO->Tries,100,2);
    Source=GUI_String(23+ThisQSO->LinkStatus);
    *(Dest++)=' ';

    /* Display link status */

    while ( *(Source)!='\0')
     {
      *(Dest++)=*(Source++);
     }

   }

  if (ActiveChannel<0)
   {
    signed long i;
    *(Dest++)='S';
    ThisSpy=(struct Spy *)
            (((unsigned char *) Spys)-sizeof(struct QSO)*(ActiveChannel+1));
    Dest=GUI_LongToString(Dest,-ActiveChannel,10,0);
    *(Dest++)=':';
    i=6;
    if (ThisSpy->MyCall[0]!='-')
     {
      Source=ThisSpy->MyCall;
      while ( ( (*Source)!='-') && ( *(Source)!='\0') )
       {
        i--;
        *(Dest++)=*(Source++);
       }
      while (i>0)
       {
        i--;
        *(Dest++)=' ';
       }
     }
     else
     {
      *(Dest++)='-';
      *(Dest++)='-';
      *(Dest++)='-';
      *(Dest++)='-';
      *(Dest++)='-';
      *(Dest++)='-';
     }
    *(Dest++)=' ';
    *(Dest++)='<';
    *(Dest++)='-';
    *(Dest++)='-';
    *(Dest++)='>';
    *(Dest++)=' ';


    i=6;
    if (ThisSpy->Call[0]!='-')
     {
      Source=ThisSpy->Call;
      while ( ( (*Source)!='-') && ( *(Source)!='\0') )
       {
        i--;
        *(Dest++)=*(Source++);
       }
      while (i>0)
       {
        i--;
        *(Dest++)=' ';
       }
     }
     else
     {
      *(Dest++)='-';
      *(Dest++)='-';
      *(Dest++)='-';
      *(Dest++)='-';
      *(Dest++)='-';
      *(Dest++)='-';
     }

    *(Dest++)=' ';
    *(Dest++)=' ';
    *(Dest++)=' ';
    *(Dest++)=' ';
    *(Dest++)=' ';
    if (ThisSpy->LinkStatus!=0)
     {

#define OutSpy(var) ( (var)<0)?'?':'0'+(var)

      *(Dest++)='R';
      *(Dest++)=OutSpy(ThisSpy->ReceiveCount1);
      *(Dest++)=' ';
      *(Dest++)='C';
      *(Dest++)=OutSpy(ThisSpy->InCollector1);
      *(Dest++)=' ';
      *(Dest++)='A';
      *(Dest++)=OutSpy(ThisSpy->OtherReceive1);
      *(Dest++)=' ';
      *(Dest++)=' ';
      *(Dest++)='<';
      *(Dest++)='-';
      *(Dest++)='-';
      *(Dest++)='>';
      *(Dest++)=' ';
      *(Dest++)=' ';
      *(Dest++)='R';
      *(Dest++)=OutSpy(ThisSpy->ReceiveCount2);
      *(Dest++)=' ';
      *(Dest++)='A';
      *(Dest++)=OutSpy(ThisSpy->InCollector2);
      *(Dest++)=' ';
      *(Dest++)='A';
      *(Dest++)=OutSpy(ThisSpy->OtherReceive2);

#undef OutSpy

     }
   }
  *(Dest)='\0';
  SetAttrs(GlobalArraysP->MainObjects[1],MUIA_Text_Contents,(ULONG) CHeadLine,
                                     TAG_DONE);

  GUIMISC_EncryptionStatus();
 }


void GUI_RefreshWindowTitle(void)
{
 signed long i,j,TNC,Cond,Data;


 /* Check for special TNC conditions */

 unsigned char *CString;

 TNC=-1;

 while (1)
  {
   if (ActiveChannel>0)
    {
     i=(QSOs+ActiveChannel-1)->HardwareUnit;
     j=HardwareInfo[i];
     if (j!=4)
      {
       TNC=i;
       if ( (j<5) || (j>7) ) break;
      }
    }
   for (i=0;i<8;++i)
    {
     j=HardwareInfo[i];
     if ( (j<4) || (j>7) )
      {
       TNC=i;
       break;
      }
    }
   break;
  }
 if (TNC==-1)
  {

   /* Print standard window title */

   unsigned char *Source,*Dest;

   unsigned long t;

   Dest=WindowTitle;
   Source=GUI_String(0);
   while ( *(Source)!='\0') *(Dest++)=*(Source++);
   *(Dest++)=' ';
   *(Dest++)=' ';
   *(Dest++)='«';

    {
     struct ClockData Clock;

     OldTimeMins=CurrentDateStamp.ds_Minute;
     Amiga2Date(((1440*CurrentDateStamp.ds_Days)+CurrentDateStamp.ds_Minute)*60,&Clock);
     Dest=GUI_LongToString(Dest,Clock.mday,10,0);
     *(Dest++)='.';
     Dest=GUI_LongToString(Dest,Clock.month,10,0);
     *(Dest++)='.';
     Dest=GUI_LongToString(Dest,Clock.year%100,10,0);
     *(Dest++)=' ';
     Dest=GUI_LongToString(Dest,Clock.hour,10,0);
     *(Dest++)=':';
     Dest=GUI_LongToString(Dest,Clock.min,10,0);
    }

   *(Dest++)='»';
   *(Dest++)=' ';
   *(Dest++)=' ';
   if (NumLock!=FALSE)
    {
     *(Dest++)='N';
     *(Dest++)='u';
     *(Dest++)='m';
     *(Dest++)='L';
    }
    else
    {
     *(Dest++)=' ';
     *(Dest++)=' ';
     *(Dest++)=' ';
     *(Dest++)=' ';
    }
   *(Dest++)=' ';

   if (InsertMode!=FALSE)
    {
     *(Dest++)='I';
     *(Dest++)='n';
     *(Dest++)='s';
     *(Dest++)=' ';
    }
    else
    {
     *(Dest++)=' ';
     *(Dest++)=' ';
     *(Dest++)=' ';
     *(Dest++)=' ';
    }
   *(Dest++)=' ';

   if (AltASCII!=FALSE)
    {
     *(Dest++)='I';
     *(Dest++)='A';
     *(Dest++)='s';
     *(Dest++)='c';
    }
    else
    {
     *(Dest++)=' ';
     *(Dest++)=' ';
     *(Dest++)=' ';
     *(Dest++)=' ';
    }

   *(Dest++)=' ';

   t=MAIN_GetConnectMsgNr();
   if (t!=0)

    {
     *(Dest++)='C';
     *(Dest++)='M';
     *(Dest++)='s';
     *(Dest++)='g';
     *(Dest++)='0'+t/10;
     *(Dest++)='0'+t%10;
    }
    else
    {
     *(Dest++)=' ';
     *(Dest++)=' ';
     *(Dest++)=' ';
     *(Dest++)=' ';
     *(Dest++)=' ';
    }
   *(Dest++)=' ';

   t=MAIL_TestForMail(NULL);
   if ((t&(65535<<16))!=0)

    {
     *(Dest++)='M';
     *(Dest++)='a';
     *(Dest++)='i';
     *(Dest++)='l';
    }
    else
    {
     *(Dest++)=' ';
     *(Dest++)=' ';
     *(Dest++)=' ';
     *(Dest++)=' ';
    }
   *(Dest++)=' ';


   *(Dest)='\0';
   Dest=WindowTitle;
   SetAttrs(MainWindow,MUIA_Window_Title,(ULONG) Dest,TAG_DONE);
   return;
  }

 /* Print TNC Special window title */

 Cond=HardwareInfo[TNC];
 if (Cond>=4) Cond--;
 Data=HardwareInfoData[TNC];
 TNC++;
 CString=GUI_String(240+Cond);

  {
   unsigned short XArray[2];
   XArray[0]=TNC;
   XArray[1]=Data;
   RawDoFmt(CString,XArray,(PutRawDoFmt),WindowTitle);
  }
 SetAttrs(MainWindow,MUIA_Window_Title,(ULONG) WindowTitle,TAG_DONE);
 OldTimeMins=CurrentDateStamp.ds_Minute;
 return;
}


signed long GUI_ButtonJumpChannel(unsigned long button)
 {
  signed long effchannel;
  signed long modulo;
  signed long base;
  signed long spytype;
  signed long ahang;

  effchannel=ActiveChannel;
  if (effchannel==0) effchannel=MonitorSaveChannel;
  if (effchannel==0) return(0);

  if (effchannel>0)
   {
    modulo=(effchannel-1)%10;
    base=(effchannel-1)/10;
    base*=10;
    spytype=FALSE;
   }
   else
   {
    modulo=(-effchannel-1)%10;
    base=(-effchannel-1)/10;
    base*=10;
    spytype=TRUE;
   }

  base+=10;
  ahang=40;
  while (ahang!=0)
   {
    ahang--;
    if (spytype==FALSE)
     {
      if (base+modulo+1<=MaxChannel)
       {
        struct QSO *ThisQSO;
        ThisQSO=QSOs+(base+modulo);
        if (ThisQSO->SkipChannel==FALSE) return(base+modulo+1);
        base+=10;
        continue;
       }
      base=0;
      spytype=TRUE;
      continue;
     }
    if (spytype==TRUE)
     {
      if (base+modulo+1<=MaxSpy) return(-base-modulo-1);
      base=0;
      spytype=FALSE;
      continue;
     }
    if (modulo>=MaxChannel) return(0);
   }
  return(0);
 }



signed long GUI_ButtonToChannel(unsigned long button)
 {
  signed long effchannel;
  signed long modulo;
  signed long base;
  signed long xchannel;
  struct QSO *ThisQSO;
  struct Spy *ThisSpy;
  effchannel=ActiveChannel;
  if (effchannel==0) effchannel=MonitorSaveChannel;
  if (effchannel==0) return(0);

  if (effchannel>0)
   {
    modulo=(effchannel-1)%10;
    base=(effchannel-1)/10;
    base*=10;
    xchannel=base+1+button;

    if (xchannel<=MaxChannel)
     {
      ThisQSO=(QSOs+xchannel-1);
      if ( (ThisQSO->SkipChannel==FALSE) &&
        (ThisQSO->IncomingMessage!=FALSE) ) return (xchannel);
     }

    for (xchannel=button+1;xchannel<=MaxChannel;xchannel+=10)
     {
      ThisQSO=(QSOs+xchannel-1);
      if (ThisQSO->SkipChannel==TRUE) continue;
      if (ThisQSO->IncomingMessage!=FALSE) return (xchannel);
     }

    xchannel=base+1+button;
    if (xchannel<=MaxChannel)
     {
      ThisQSO=(QSOs+xchannel-1);
      if (ThisQSO->SkipChannel==FALSE) return(xchannel);
      return (0);
     }
     else
     {
      return(0);
     }
   }
   else
   {
    modulo=(-effchannel-1)%10;
    base=(-effchannel-1)/10;
    base*=10;
    xchannel=base+1+button;

    if (xchannel<=MaxSpy)
     {
      ThisSpy=(struct Spy *) ( ((struct QSO *) Spys)+
                                (xchannel-1) );
      if (ThisSpy->IncomingMessage!=FALSE) return (-xchannel);
     }

    for (xchannel=button+1;xchannel<=MaxSpy;xchannel+=10)
     {
      ThisSpy=(struct Spy *) ( ((struct QSO *) Spys)+
                                (xchannel-1) );
      if (ThisSpy->IncomingMessage!=FALSE) return (-xchannel);
     }
    xchannel=base+1+button;
    if (xchannel<=MaxSpy)
     {
      return (-xchannel);
     }
     else
     {
      return(0);
     }
   }

 }

void GUI_RefreshPacketHead(void)
{
 unsigned long i;
 signed long thischan,dischan;

 struct QSO *ThisQSO;
 struct Spy *ThisSpy;

 if (MainWindow==NULL) return;
 

 thischan=ActiveChannel;

 if (thischan==0) thischan=MonitorSaveChannel;
 if ( (thischan>0) && (thischan>MaxChannel) ) return;
 if ( (thischan<0) && (-thischan>MaxSpy) ) return;

 if ( thischan==0) return;



 for (i=0;i<10;++i)
  {

   dischan=GUI_ButtonToChannel(i);

   if (dischan==0)
    {
     SetAttrs(GlobalArraysP->MainObjects[3+i],MUIA_Text_PreParse,(ULONG) NULL,
                                            MUIA_Text_Contents,(ULONG) "------",
                                            MUIA_Disabled,TRUE,
                                            TAG_DONE);
     /* Please disable */

     continue;
    }

   set(GlobalArraysP->MainObjects[3+i],MUIA_Disabled,FALSE);
   SetAttrs(GlobalArraysP->MainObjects[3+i],MUIA_Selected,(dischan==ActiveChannel)?
            TRUE:FALSE,MUIA_NoNotify,TRUE,TAG_DONE);

   if (dischan>0)
    {
     signed short strangegroup;
     ThisQSO=(QSOs+dischan-1);
     strangegroup=(((dischan-1)/10)==((thischan-1)/10))?FALSE:TRUE;
     if ( (thischan>0) && (dischan<0) ) strangegroup=TRUE;
     if ( (thischan<0) && (dischan>0) ) strangegroup=TRUE;
     /* Display it*/

     SetAttrs(GlobalArraysP->MainObjects[3+i],MUIA_Text_PreParse,
                       !strangegroup?(ThisQSO->IncomingMessage?
                       (ULONG) "\0333":(ULONG) NULL):(ULONG) "\033i\0333",
                       MUIA_Text_Contents,(ULONG) ThisQSO->Call,
                       TAG_DONE);

    }

   if (dischan<0)
    {
     signed short strangegroup;
     ThisSpy=(struct Spy *) ( ((struct QSO *) Spys)+
                                (-dischan-1) );
     strangegroup=(((dischan+1)/10)==((thischan+1)/10))?FALSE:TRUE;
     if ( (thischan>0) && (dischan<0) ) strangegroup=TRUE;
     if ( (thischan<0) && (dischan>0) ) strangegroup=TRUE;
     /* Display it*/

     SetAttrs(GlobalArraysP->MainObjects[3+i],MUIA_Text_PreParse,
                       !strangegroup?(ThisSpy->IncomingMessage?
                         (ULONG) "\0333":(ULONG) NULL):(ULONG) "\033i\0333",
                       MUIA_Text_Contents,(ULONG) ThisSpy->Call,
                       TAG_DONE);


    }

  }

}

unsigned long GUI_LaunchWindowPoll(void)
{
 if ( (Application!=NULL) && (LaunchWindow!=NULL) )
  {
   unsigned long xx;
   DoMethod(Application,MUIM_Application_InputBuffered);
   GetAttr(MUIA_Application_Iconified,Application,&xx);
   if (xx==TRUE) return (FALSE);
   GetAttr(MUIA_Window_Open,LaunchWindow,&xx);
   if (xx==FALSE) return(TRUE);
  }
 return(FALSE);
}

void GUI_DisplayLaunchWindow(void)
{
 Object *xb;

 if (LaunchWindow!=NULL)
  {
   return;
  }

 set(MainWindow,MUIA_Window_Sleep,TRUE);

 LaunchWindow =
                 WindowObject,
                 MUIA_Window_LeftEdge,MUIV_Window_LeftEdge_Centered,
                 MUIA_Window_TopEdge,MUIV_Window_TopEdge_Centered,
                 MUIA_Window_Title,GUI_String(0),
                 MUIA_Window_RefWindow,MainWindow,
                     WindowContents,


                       VGroup,Child,TextObject,
                                     MUIA_Background,MUII_TextBack,
                                     MUIA_Frame,MUIV_Frame_Text,
                                     MUIA_Text_PreParse,"\033c",
                                     MUIA_Text_Contents,GUI_String(106),
                                     End,

                              Child,MUI_NewObject("Busy.mcc",
                                     End,
                              Child,xb=KeyButton(GUI_String(427),'a'),

                       End,
                     End;

   if (LaunchWindow==NULL)
      {
       GUI_LowMemory();
       return;
      }

 DoMethod(Application,OM_ADDMEMBER,LaunchWindow);


 DoMethod(LaunchWindow,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
             LaunchWindow,3,MUIM_Set,MUIA_Window_Open,FALSE);
 DoMethod(xb,MUIM_Notify,MUIA_Pressed,FALSE,
             LaunchWindow,3,MUIM_Set,MUIA_Window_Open,FALSE);


 set(LaunchWindow,MUIA_Window_Open,TRUE);

 return;
}

void GUI_GetUserData(void);

void GUI_StartupDone(void)
{
 struct MUI_Command *ptr;
 MAIN_ExecuteARexxScript("ProfiPacket_Start",NULL,TRUE);
 set(MainWindow,MUIA_Window_Sleep,FALSE);
 GetAttr(MUIA_Application_Commands,Application,(ULONG *) &ptr);
 if (ptr==NULL)
  {
   ptr=MAIN_CreateRexx();
   set(Application,MUIA_Application_Commands,(ULONG) ptr);
  }
}



void GUI_CloseLaunchWindow(void)
{
 if (LaunchWindow==NULL) return;

 set(LaunchWindow,MUIA_Window_Open,FALSE);
 DoMethod(Application,OM_REMMEMBER,LaunchWindow);
 MUI_DisposeObject(LaunchWindow);
 LaunchWindow=NULL;
 if ( (UserBaseHead->ownerfirstname[0]!='\0') &&
      (UserBaseHead->ownerqth[0]!='\0') &&
      (UserBaseHead->ownerlocator[0]!='\0') )
  {
   GUI_StartupDone();
  }
  else
  {
   GUI_GetUserData();
  }
}


void GUI_DisplayEventWindow(unsigned char *event)
{
 Object *NewText;


 unsigned char *EventCopy;
 unsigned long EventLength;
 unsigned short firstop;


 firstop=FALSE;

 if (event==NULL)
  {
   return;
  }

 EventCopy=event;
 EventLength=1;
 while (*(EventCopy++)!='\0') ++EventLength;

 if ((EventCopy=AllocVec(EventLength,0x1))==NULL)
  {
   return;
  }

 {
  unsigned char *es,*ed;
  es=event;
  ed=EventCopy;
  while (*(es)!='\0')
   {
    if (*(es)!='\015')
     {
      *(ed++)=*(es++);
     }
     else
     {
      *(ed++)='\012';
      ++es;
     }
   }
  *(ed)='\0';
 }

 if (MessageWindow!=NULL)
  {
   unsigned long winopen;
   get(MessageWindow,MUIA_Window_Open,&winopen);
   if (winopen==FALSE)
    {
     GUI_CloseMessageWindow();
    }
  }
 if (MessageWindow==NULL)
  {
   firstop=TRUE;
   MessageWindow =
                 WindowObject,
                 MUIA_Window_LeftEdge,MUIV_Window_LeftEdge_Centered,
                 MUIA_Window_TopEdge,MUIV_Window_TopEdge_Centered,
                 MUIA_Window_NoMenus,TRUE,
                 MUIA_Window_Activate,FALSE,
                 MUIA_Window_Title,GUI_String(0),
                 MUIA_Window_RefWindow,MainWindow,
                     WindowContents,


                       MessageGroup=VGroup,

                         

                       End,
                     End;

   if (MessageWindow==NULL)
      {
       GUI_LowMemory();
       FreeVec(EventCopy);
       return;
      }

   DoMethod(Application,OM_ADDMEMBER,MessageWindow);
   FillHook(&HooksP->MessageWindowClose,(void *) GUI_CloseMessageWindowReq,0);


   DoMethod(MessageWindow,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
             MessageWindow,2,MUIM_CallHook,&HooksP->MessageWindowClose);

  }

 NewText=TextObject,
                   MUIA_Background,MUII_TextBack,
                   MUIA_Frame,MUIV_Frame_Text,
                   MUIA_Text_PreParse,"\033c",
                   MUIA_Text_Contents,EventCopy,
                   End;

 if (NewText==NULL)
  {
   FreeVec(EventCopy);
   return;
  }
 DoMethod(MessageGroup,MUIM_Group_InitChange);

 DoMethod(MessageGroup,OM_ADDMEMBER,NewText);

 DoMethod(MessageGroup,MUIM_Group_ExitChange);

 if (firstop==TRUE) set(MessageWindow,MUIA_Window_Open,TRUE);

 MessageWindowSecs=CurrentDateStamp.ds_Tick/50;
 MessageWindowTime=Preferences.MessageWindowSecs;

 FreeVec(EventCopy);

 return;
}


void GUI_CloseMessageWindowReq(void)
{
 if (MessageWindow==NULL) return;

 set(MessageWindow,MUIA_Window_Open,FALSE);
}

void GUI_CloseMessageWindow(void)
{

 if (MessageWindow==NULL) return;

 set(MessageWindow,MUIA_Window_Open,FALSE);
 DoMethod(Application,OM_REMMEMBER,MessageWindow);
 MUI_DisposeObject(MessageWindow);
 MessageWindow=NULL;

}



void GUI_BuildHardwarePages(void)
 {
  unsigned long i,j;
  unsigned char *base,*appendor,*appends;
  unsigned char **hlist;
  unsigned long hardinc[8];
  signed long *hass;
  struct QSO *ThisQSO;

  for (i=0;i<8;++i)
   {
    base=Preferences.Devices[i].Description;

    for (j=0;j<4;++j)
     {
      appends=base;
      appendor=GlobalArraysP->HardwareNames[i][j];
      while (*(appends)!='\0') *(appendor++)=*(appends++);
      *(appendor++)=' ';
      *(appendor++)='/';
      *(appendor++)=' ';
      *(appendor++)='1'+j;
      *(appendor)='\0';
     }
   }

  hlist=GlobalArraysP->HardwarePages;
  hass=GlobalArraysP->HardAssigns;
  hardinc[0]=0;
  hardinc[1]=0;
  hardinc[2]=0;
  hardinc[3]=0;
  hardinc[4]=0;
  hardinc[5]=0;
  hardinc[6]=0;
  hardinc[7]=0;

  for (i=1;i<=MaxChannel;i+=10)
   {
    ThisQSO=QSOs+i-1;
    if ( (ThisQSO->HardwareUnit<0) || (ThisQSO->HardwareUnit>7) ) continue;
    *(hlist++)=GlobalArraysP->HardwareNames[ThisQSO->HardwareUnit]
                  [hardinc[ThisQSO->HardwareUnit]];
    *(hass++)=i;
    hardinc[ThisQSO->HardwareUnit]++;
   }

  if (MaxSpy>0)
   {
    *(hlist++)=GUI_String(394);
    *(hass++)=-1;
   }
  if (MaxSpy>10)
   {
    *(hlist++)=GUI_String(395);
    *(hass++)=-11;
   }
  *(hlist++)=GUI_String(396);
  *(hass++)=0;
  *(hlist)=NULL;
  for (i=0;i<40;++i)
   {
    GlobalArraysP->HardMemory[i]=GlobalArraysP->HardAssigns[i];
   }
 }


void GUI_RedisplayFileTransfer_ftrdt(unsigned char *xdf,unsigned long time);

void GUI_RedisplayFileTransfer_ftrdt(unsigned char *xdf,unsigned long time)
  {
   unsigned char *xd;
   unsigned long hour,min,sec;
   hour=time % (3600*100);
   min=hour % (3600);
   sec=min % 60;
   hour=hour / 3600;
   min=min / 60;
   xd=xdf;
   *(xd++)=' ';
   *(xd++)='0'+(hour/10);
   *(xd++)='0'+(hour%10);
   *(xd++)=':';
   *(xd++)='0'+(min/10);
   *(xd++)='0'+(min%10);
   *(xd++)=':';
   *(xd++)='0'+(sec/10);
   *(xd++)='0'+(sec%10);
   *(xd)='\0';
  }


void GUI_RedisplayFileTransfer(signed long Channel)
 {
  struct QSO *ThisQSO;
  unsigned char xstring[80];
  unsigned char *xs,*xd;
  if (MainWindow==NULL) return;
  if (Channel!=ActiveChannel) return;
  if (Channel<=0) return;
  if (Channel>MaxChannel) return;

  ThisQSO=QSOs+(Channel-1);


  SetAttrs(GlobalArraysP->MainObjects[19],MUIA_ShowMe,
        ThisQSO->TransferMode!=0,TAG_DONE);


  if (ThisQSO->TransferMode==0)
   {
    /* Disable transfer showing*/
    return;
   }

  SetAttrs(GlobalArraysP->MainObjects[20],MUIA_Text_Contents,
           (ULONG) GUI_String(90+  ((ThisQSO->TransferMode-1) & 0xf)),TAG_DONE);
  SetAttrs(GlobalArraysP->MainObjects[21],MUIA_Text_Contents,
           (ULONG) ThisQSO->FileName,TAG_DONE);
  xd=xstring;
  xs=GUI_String(85);
  while (*(xs)!='\0') *(xd++)=*(xs++);
  *(xd++)=' ';
  xd=GUI_LongToString(xd,ThisQSO->TransferRBytes,1000000,1);

  if (ThisQSO->TransferPercent!=-1)
   {
    *(xd++)='/';
    xd=GUI_LongToString(xd,ThisQSO->TransferTotBytes,1000000,1);
   }
  *(xd)='\0';
  SetAttrs(GlobalArraysP->MainObjects[22],MUIA_Text_Contents,
           (ULONG) xstring,TAG_DONE);

  xd=xstring;
  xs=GUI_String(86);
  while (*(xs)!='\0') *(xd++)=*(xs++);
  GUI_RedisplayFileTransfer_ftrdt(xd,ThisQSO->TransferElapsed);
  SetAttrs(GlobalArraysP->MainObjects[23],MUIA_Text_Contents,
           (ULONG) xstring,TAG_DONE);

  xd=xstring;
  xs=GUI_String(87);
  while (*(xs)!='\0') *(xd++)=*(xs++);
  *(xd++)=' ';
  xd=GUI_LongToString(xd,ThisQSO->TransferBaud,1000000,1);
  *(xd)='\0';
  SetAttrs(GlobalArraysP->MainObjects[24],MUIA_Text_Contents,
           (ULONG) xstring,TAG_DONE);

  if (ThisQSO->TransferPercent!=-1)
   {
    xd=xstring;
    xs=GUI_String(88);
    while (*(xs)!='\0') *(xd++)=*(xs++);
    GUI_RedisplayFileTransfer_ftrdt(xd,ThisQSO->TransferEstimated);
    SetAttrs(GlobalArraysP->MainObjects[25],MUIA_Text_Contents,
             (ULONG) xstring,TAG_DONE);
    xd=xstring;
    xs=GUI_String(89);
    while (*(xs)!='\0') *(xd++)=*(xs++);
    GUI_RedisplayFileTransfer_ftrdt(xd,ThisQSO->TransferTotal);
    SetAttrs(GlobalArraysP->MainObjects[27],MUIA_Text_Contents,
             (ULONG) xstring,TAG_DONE);

    SetAttrs(GlobalArraysP->MainObjects[26],MUIA_Gauge_Current,
             ThisQSO->TransferPercent,MUIA_Gauge_InfoText,
             (ULONG) "%ld %%",TAG_DONE);
   }
   else
   {
    SetAttrs(GlobalArraysP->MainObjects[25],MUIA_Text_Contents,NULL,
             TAG_DONE);
    SetAttrs(GlobalArraysP->MainObjects[26],MUIA_Gauge_Current,0,
             MUIA_Gauge_InfoText,(ULONG) "????",TAG_DONE);
    SetAttrs(GlobalArraysP->MainObjects[27],MUIA_Text_Contents,NULL,
             TAG_DONE);
   }

 }




void GUIMAIN_PutMonitor(unsigned char *addch,unsigned long addlen,
                         unsigned long addstyle)
 {
  struct StyleInfo stinf;
  unsigned char *lineptr;
  unsigned char *restptr;
  unsigned long linelen;
  unsigned long restlen;
  unsigned long boolcr;

  restptr=addch;
  restlen=addlen;



  stinf.BackgroundColor=0;
  stinf.Pad1=0;
  stinf.StartColumn=0;

  switch (addstyle)
   {
    case 1:
     {
      stinf.ForegroundColor=Preferences.UserANSIPen;
      stinf.TextStyle=Preferences.UserANSIStyle;
      break;
     }
    case 2:
     {
      stinf.ForegroundColor=Preferences.SysopANSIPen;
      stinf.TextStyle=Preferences.SysopANSIStyle;
      break;
     }
    case 66:
     {
      stinf.ForegroundColor=Preferences.LinkANSIPen;
      stinf.TextStyle=Preferences.LinkANSIStyle;
      break;
     }
    default:
     {
      stinf.ForegroundColor=Preferences.SpecialANSIPen;
      stinf.TextStyle=Preferences.SpecialANSIStyle;
      break;
     }

   }

  while (restlen>0)
   {
    boolcr=FALSE;
    lineptr=restptr;
    linelen=0;
    while ( ( restlen>0 ) && (*(restptr)!='\015') && (linelen<MonWrap) )
     {
      linelen++;
      restlen--;
      restptr++;
     }
    if ( (restlen>0) && (*(restptr)=='\015') )
     {
      restlen--;
      restptr++;
      boolcr=TRUE;
     }

    if (linelen==MonWrap) boolcr=TRUE;


    DoMethod(GlobalArraysP->MainObjects[15],MUIM_Editor_WriteStream,
             0,lineptr,linelen,&stinf,1,boolcr);

   }

 }


void GUIMAIN_PutQSO(const unsigned char *addch,unsigned long addlen,
                    signed long channel,unsigned long addstyle)
 {
  unsigned long page;
  const unsigned char *lineptr;
  const unsigned char *restptr;
  unsigned long linelen;
  unsigned long restlen;
  unsigned long boolcr;


  if (channel==0) return;
  if ( (channel<0) && ((-channel)>MaxSpy) ) return;
  if ( (channel>0) && (channel>MaxChannel) ) return;

  if (channel>0)
   {
    page=channel-1;
   }
   else
   {
    page=MaxChannel+(-channel-1);
   }

  if ( ( (addstyle!=1) && (addstyle!=2) ) || (channel<0)  )
   {
    /*Do conventional writing mechanism (no incoming ANSI stream) */
    /*No style 1/2 or spy writing                                 */

    struct StyleInfo stinf;

    restptr=addch;
    restlen=addlen;


    stinf.BackgroundColor=0;
    stinf.Pad1=0;
    stinf.StartColumn=0;
    switch (addstyle)
     {
      case 1:
       {
        stinf.ForegroundColor=Preferences.UserANSIPen;
        stinf.TextStyle=Preferences.UserANSIStyle;
        break;
       }
      case 2:
       {
        stinf.ForegroundColor=Preferences.SysopANSIPen;
        stinf.TextStyle=Preferences.SysopANSIStyle;
        break;
       }
      case 66:
       {
        stinf.ForegroundColor=Preferences.LinkANSIPen;
        stinf.TextStyle=Preferences.LinkANSIStyle;
        break;
       }
      default:
       {
        stinf.ForegroundColor=Preferences.SpecialANSIPen;
        stinf.TextStyle=Preferences.SpecialANSIStyle;
        break;
       }

     }



    while (restlen>0)
     {
      boolcr=FALSE;
      lineptr=restptr;
      linelen=0;
      while ( ( restlen>0 ) && (*(restptr)!='\015'))
       {
        linelen++;
        restlen--;
        restptr++;
       }
      if ( (restlen>0) && (*(restptr)=='\015') )
       {
        restlen--;
        restptr++;
        boolcr=TRUE;
       }
      DoMethod(GlobalArraysP->MainObjects[14],MUIM_Editor_WriteStream,
               page,lineptr,linelen,&stinf,1,boolcr);
     }
   }
   else
   {
    /* Do new style ANSI parsing packet reading */

    struct ANSIStream *mystream;
    if (addstyle==2)
     {
      mystream=&(QSOs+(channel-1))->EditOutStream;
     }
     else
     {
      mystream=&(QSOs+(channel-1))->PacketOutStream;
     }

    restptr=addch;
    restlen=addlen;

    while (restlen>0)
     {
      boolcr=FALSE;
      lineptr=restptr;
      linelen=0;
      while ( ( restlen>0 ) && (*(restptr)!='\015'))
       {
        linelen++;
        restlen--;
        restptr++;
       }
      if ( (restlen>0) && (*(restptr)=='\015') )
       {
        restlen--;
        restptr++;
        boolcr=TRUE;
       }

      mystream->CharBuffer=GlobalArraysP->StreamCharBuffer;
      mystream->CharBufferSize=4096;
      mystream->Styles=GlobalArraysP->StreamStyleBuffer;
      mystream->StyleSize=128;
      mystream->CurrentColumn=0;
      mystream->TextBuffer=(unsigned char *)lineptr;
      mystream->TextLength=linelen;

      if (DoMethod(GlobalArraysP->MainObjects[14],MUIM_Editor_ANSIToStream,
                   mystream)!=MUIV_Editor_Return_OK)
       {
        return;
       }

      {
       struct StyleInfo *ms;
       unsigned long msl;

       msl=mystream->UsedStyles;
       ms=mystream->Styles;
       while (msl!=0)
        {
         if ( (ms->ForegroundColor==1) && (ms->BackgroundColor==0) &&
              (ms->TextStyle==0) )
          {
           if (addstyle==2)
            {
             ms->ForegroundColor=Preferences.SysopANSIPen;
             ms->TextStyle=Preferences.SysopANSIStyle;
            }
            else
            {
             ms->ForegroundColor=Preferences.UserANSIPen;
             ms->TextStyle=Preferences.UserANSIStyle;
            }
          }
         msl--;
         ms++;
        }
      }

      DoMethod(GlobalArraysP->MainObjects[14],MUIM_Editor_WriteStream,
               page,
               mystream->CharBuffer,
               mystream->CharLength,
               mystream->Styles,
               mystream->UsedStyles,
               boolcr);
     }


   }
 }



unsigned long GUIMAIN_MainInputStream(struct Hook *MyHook,unsigned long *Param,void *Object,
                           unsigned long *Data)
{
 unsigned char *inchar;
 struct StyleInfo *instyle;
 unsigned long incharl,instylel;
 unsigned long *para;
 unsigned long docr,i;
 unsigned char outansi[128];
 unsigned short doagain;
 struct QSO *ThisQSO;


 LastSysopActivity=CurrentDateStamp;

 ThisQSO=QSOs;

 para=(void *) *(Param);

 inchar=(void *) (*(para));
 incharl=*(para+1);
 instyle=(void *) (*(para+2));
 instylel=*(para+3);
 docr=*(para+4);


 if ( (incharl!=0) && (*(inchar)=='\033') )
  {
   incharl-=1;
   inchar++;
   QSO_SendCommandUser(incharl,ActiveChannel,inchar);
  }
  else
  {
   doagain=TRUE;
   ThisQSO->EditInStream.CharBuffer=inchar;
   ThisQSO->EditInStream.CharLength=incharl;
   ThisQSO->EditInStream.Styles=instyle;
   ThisQSO->EditInStream.UsedStyles=instylel;
   ThisQSO->EditInStream.CurrentColumn=0;
   ThisQSO->EditInStream.StreamCR=docr;
   while (doagain)
    {
     ThisQSO->EditInStream.TextBuffer=outansi;
     ThisQSO->EditInStream.TextBufferSize=128;
     if (DoMethod (GlobalArraysP->MainObjects[13],MUIM_Editor_StreamToANSI,
               &ThisQSO->EditInStream)!=MUIV_Editor_Return_ANSI_NotFinished)
      {
       doagain=FALSE;
      }
     for (i=0;i<ThisQSO->EditInStream.TextLength;++i)
      {
       if (outansi[i]=='\012') outansi[i]='\015';
      }
     QSO_SendDataUser(ThisQSO->EditInStream.TextLength,
                      ActiveChannel,outansi);
    }
  }
 return(0);
}

void GUIMAIN_ResetStyles(struct QSO *qso)
{
 qso->EditInStream.ForegroundColor=1;
 qso->EditInStream.BackgroundColor=0;
 qso->EditInStream.TextStyle=0;
 qso->EditInStream.Pad1=0;
 qso->EditOutStream.ForegroundColor=1;
 qso->EditOutStream.BackgroundColor=0;
 qso->EditOutStream.TextStyle=0;
 qso->EditOutStream.Pad1=0;
 qso->PacketOutStream.ForegroundColor=1;
 qso->PacketOutStream.BackgroundColor=0;
 qso->PacketOutStream.TextStyle=0;
 qso->PacketOutStream.Pad1=0;
 qso->EditInStream.ANSICmdState=0;
 qso->EditOutStream.ANSICmdState=0;
 qso->PacketOutStream.ANSICmdState=0;

}

void GUI_LoadBuffers(void)
{
 BPTR FileHand;
 BPTR MyLock;
 struct FileInfoBlock *Myfib;
 unsigned long i,err;
 unsigned char *cptr,*dptr;

 if (MainWindow==NULL) return;

 if (Preferences.BufferSave==FALSE) return;

 Myfib=AllocDosObject(DOS_FIB,NULL);
 if (Myfib==NULL) return;
 MyLock=Lock("data/buffers.dat",SHARED_LOCK);
 if (MyLock==NULL)
  {
   FreeDosObject(DOS_FIB,Myfib);
   return;
  }

 if ((Examine(MyLock,Myfib))==FALSE)
  {
   FreeDosObject(DOS_FIB,Myfib);
   UnLock(MyLock);
   return;
  }

 i=TRUE;

 cptr=Myfib->fib_Comment;

 dptr="LATEST BUFFERS";

 while ( *(dptr)!='\0') if ( *(cptr++) != *(dptr++) ) i=FALSE;

 UnLock(MyLock);
 FreeDosObject(DOS_FIB,Myfib);

 if ( (i==FALSE) &&
      (GUI_HandleRequest(0,428,346,NULL)==FALSE)
    ) return;


 if ( (FileHand=Open("data/buffers.dat",1005))==NULL) return;

  {
   unsigned long Header[4];
   if ( (Read(FileHand,Header,16))!=16)
    {
     Close(FileHand);
     DeleteFile("data/buffers.dat");
     return;
    }

   if ( (Header[0]!=0x50504246) ||  (Header[1]!=1) ||
        (Header[2]!=MaxChannel) ||  (Header[3]!=MaxSpy) )
    {
     Close(FileHand);
     DeleteFile("data/buffers.dat");
     return;
    }
  }
 for (i=13;i<16;++i)
  {
   err=DoMethod(GlobalArraysP->MainObjects[i],MUIM_Editor_GetStorage,FileHand);
   if (err!=MUIV_Editor_Return_OK)
    {
     if (err==MUIV_Editor_Return_Disk_BuffersTooSmall)
      {
       continue;
      }

     GUI_HandleRequest(0,430,16,NULL);
     Close(FileHand);
     SetComment("data/buffers.dat","");
     return;
    }
  }
 Close(FileHand);
 SetComment("data/buffers.dat","");
 return;
}

void GUI_SaveBuffers(unsigned long leavemode)
{
 BPTR FileHand;
 unsigned long i;

 if (MainWindow==NULL) return;

 if (Preferences.BufferSave==FALSE) return;
 if ( (FileHand=Open("data/buffers.dat",1006))==NULL)
  {
   return;
  }
  {
   unsigned long Header[4];
   Header[0]=0x50504246;
   Header[1]=1;
   Header[2]=MaxChannel;
   Header[3]=MaxSpy;
   if ( (Write(FileHand,Header,16))!=16)
    {
     Close(FileHand);
     DeleteFile("data/buffers.dat");
     GUI_HandleRequest(0,429,16,NULL);
     return;
    }
  }

 for (i=13;i<16;++i)
  {
   if ( (DoMethod(GlobalArraysP->MainObjects[i],MUIM_Editor_PutStorage,FileHand))!=
         MUIV_Editor_Return_OK)
    {
     Close(FileHand);
     DeleteFile("data/buffers.dat");
     GUI_HandleRequest(0,429,16,NULL);
    }
  }

 Close(FileHand);
 SetComment("data/buffers.dat",leavemode?"LATEST BUFFERS":"");
 return;
}

void GUI_GetLastActivity(struct DateStamp *stamp)
 {
  *stamp=LastSysopActivity;
 }

void GUI_GeneralError(unsigned long strnum)
 {
  char *errstr;
  errstr=GUI_String(strnum);
  if (Application!=NULL)
   {
    MUI_Request(Application,NULL,0,GUI_String(0),GUI_String(16),errstr);
   }
   else
   {
    struct EasyStruct eas;
    
    eas.es_StructSize=sizeof(eas);
    eas.es_Flags=0;
    eas.es_Title=GUI_String(0);
    eas.es_TextFormat=errstr;
    eas.es_GadgetFormat=GUI_String(16);
    EasyRequest(NULL,&eas,NULL);
   }
 }
 
void GUI_WriteToQSO(const unsigned char *data,int channel,int length,int color)
 {
  if (MainWindow==NULL) return;
  if (channel==0)
   {
    GUIMAIN_PutMonitor((unsigned char *)data,length,color);
    return;
   }
  if (channel>0) 
   {
    /* scan bell*/
    if (color==2)
     {
      GUIMAIN_PutQSO(data,length,channel,color);
      return;
     }
    if (color==1)
     {
      const unsigned char *sptr;
      int i,f=0;
      sptr=data;
      for (i=0;i<length;++i)
       {
        if ( *sptr==7) 
         {
          f=1;
          break;
         }
        ++sptr;
       }
      if (f==1) AUDIO_Effect_C(6,channel);
     }
   }  
     
  if (ActiveChannel!=channel)
   {
    struct QSO *mqso;
    struct Spy *mspy;
    if (channel<0)
     {
      AUDIO_Effect_C(9,channel);
     }
     else
     {
      // why triggered effect 31  for color !=1 
      if (color==1)
       {
        AUDIO_Effect_C(2,channel);
       }
     }
    if (channel>0)
     {
      mqso=(QSOs+(channel-1));	
      if (mqso->IncomingMessage==0)
       {
        mqso->IncomingMessage=1;
        GUI_RefreshPacketHead();
       }
     }
     else
     {
      mspy=(struct Spy *)(((struct QSO *) Spys)+(-channel-1));
      if (mspy->IncomingMessage==0)
       {
        mspy->IncomingMessage=1;
        GUI_RefreshPacketHead();        
       }
     }
   }
   else
   {
    if ( (channel<0) ||(color==1) )
     {
      AUDIO_Effect_C(3,channel);
     }
   }
  GUIMAIN_PutQSO(data,length,channel,color); 
 }
