/* 
    allwidgets for Bt848 frame grabber driver

    Copyright (C) 1996,97 Marcus Metzler (mocm@thp.uni-koeln.de)

    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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
 * allwidgets.cc 
 */

#include <X11/Xlib.h>
#include <Xm/Xm.h>
#include <Xm/List.h>
#include <Xm/TextF.h>
#include <Xm/RowColumn.h>
#include <Xm/MainW.h>
#include <Xm/Form.h>
#include <Xm/PanedW.h>
#include <Xm/Frame.h>
#include <Xm/PushB.h>
#include <Xm/DialogS.h>
#include <Xm/CascadeB.h>
#include <Xm/MessageB.h>
#include <Xm/Scale.h>
#include <Xm/ToggleBG.h>
#include <Xm/Protocols.h>
#include <Xm/Text.h>
#include <Xm/ArrowB.h>

#include "TVscreen.h"
#include "allwidgets.h"
#include "callbacks.h"
#include "images.h"
#include "sound.h"
#include "snap.h"

/*                          */
/* Ask the user             */
/*                          */
int Nandeska(Widget parent, char *toi)
{
  static Widget dialog;

  XmString t,hai,iie;
  static int kotae;
  
  extern   XtAppContext app_context;

  if (!dialog){
    dialog=XmCreateQuestionDialog(parent,"what",NULL,0);
    hai=XmStringCreateLocalized("Yes");
    iie=XmStringCreateLocalized("No");
    XtVaSetValues(dialog,XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL,
		  XmNokLabelString, hai,
		  XmNcancelLabelString, iie,
		  NULL);
    XtSetSensitive( XmMessageBoxGetChild(dialog,XmDIALOG_HELP_BUTTON),False);
    XtAddCallback(dialog,XmNokCallback,nandemo,(XtPointer) &kotae);
    XtAddCallback(dialog,XmNcancelCallback,nandemo,(XtPointer) &kotae);
    XmStringFree(hai);
    XmStringFree(iie);
  }
  kotae=0;
  t=XmStringCreateLocalized(toi);
  XtVaSetValues(dialog,XmNmessageString, t,NULL);
  XmStringFree(t);
  XtManageChild(dialog);
  XtPopup(XtParent(dialog),XtGrabNone);
  while(kotae==0) XtAppProcessEvent(app_context,XtIMAll);
  XtPopdown(XtParent(dialog));
  return kotae;
}



/*                          */
/* Get TopShell             */
/*                          */
static Widget GetTopShell(Widget w)
{
  while (w && !XtIsWMShell(w))
    w=XtParent(w);
  return w;
}

/*                          */
/* create channel arrows    */
/*                          */
void channel_arrows(Widget menu)
{ 
  Widget arrow;
  Pixel fg,bg;

  Pixmap up=XCreateBitmapFromData(XtDisplay(menu),
				  RootWindowOfScreen(XtScreen(menu)),
				  up_bits,up_width,up_height);
  arrow=XtVaCreateManagedWidget("<",xmPushButtonWidgetClass,menu,
#ifndef LesstifVersion
				XmNlabelType,XmPIXMAP,
				XmNlabelPixmap,up,
#endif
				NULL);

  XtAddCallback(arrow,XmNactivateCallback,chan_ud,XtPointer(CHANUP));
  XtAddEventHandler(arrow,EnterWindowMask,False,help_mess,XtPointer(CHANUP));
  
  Pixmap down=XCreateBitmapFromData(XtDisplay(menu),
				RootWindowOfScreen(XtScreen(menu)),
				down_bits,down_width,down_height);
  
  arrow=XtVaCreateManagedWidget(">",xmPushButtonWidgetClass,menu,
#ifndef LesstifVersion
				     XmNlabelType,XmPIXMAP,
				     XmNlabelPixmap,down,
#endif
				     NULL);

  XtAddCallback(arrow,XmNactivateCallback,chan_ud,XtPointer(CHANDOWN));
  XtAddEventHandler(arrow,EnterWindowMask,False,help_mess,XtPointer(CHANDOWN));

}
  
/*                          */
/* create input buttons     */
/*                          */
void input_buttons(Widget menu)
{ 
  Pixmap  tu=XCreateBitmapFromData(XtDisplay(menu),
				   RootWindowOfScreen(XtScreen(menu)),
				   t_bits,t_width,t_height);

  /* tuner input select */
  input1[TUNER]=XtVaCreateManagedWidget("T",xmPushButtonWidgetClass,menu,
#ifndef LesstifVersion
					XmNlabelType,XmPIXMAP,
					XmNlabelPixmap,tu,
#endif
					NULL);

  XtAddCallback(input1[0],XmNactivateCallback,input_sel,XtPointer(TUNER));
  XtAddEventHandler(input1[0],EnterWindowMask,False,help_mess,XtPointer(TUNERMESS));


  /* composit input 1 select */
  Pixmap  co1=XCreateBitmapFromData(XtDisplay(menu),
				   RootWindowOfScreen(XtScreen(menu)),
				   c1_bits,c1_width,c1_height);

  input1[COMP1]=XtVaCreateManagedWidget("1",xmPushButtonWidgetClass,menu,
#ifndef LesstifVersion
					XmNlabelType,XmPIXMAP,
					XmNlabelPixmap,co1,	       
#endif
					NULL);

  XtAddCallback(input1[1],XmNactivateCallback,input_sel,XtPointer(COMP1));
  XtAddEventHandler(input1[1],EnterWindowMask,False,help_mess,XtPointer(COMP1MESS));


  /* composit input 2 select */
  Pixmap  co2=XCreateBitmapFromData(XtDisplay(menu),
				   RootWindowOfScreen(XtScreen(menu)),
				   c2_bits,c2_width,c2_height);

  input1[COMP2]=XtVaCreateManagedWidget("2",xmPushButtonWidgetClass,menu,
#ifndef LesstifVersion
					XmNlabelType,XmPIXMAP,
					XmNlabelPixmap,co2,	       
#endif
					NULL);

  XtAddCallback(input1[2],XmNactivateCallback,input_sel,XtPointer(COMP2));
  XtAddEventHandler(input1[2],EnterWindowMask,False,help_mess,XtPointer(COMP2MESS));

  /* svhs input select */
  Pixmap  svhs=XCreateBitmapFromData(XtDisplay(menu),
				   RootWindowOfScreen(XtScreen(menu)),
				   svhs_bits,svhs_width,svhs_height);

  input1[SVHS]=XtVaCreateManagedWidget("S",xmPushButtonWidgetClass,menu,
#ifndef LesstifVersion
				       XmNlabelType,XmPIXMAP,
				       XmNlabelPixmap,svhs,	       
#endif
				       NULL);

  XtAddCallback(input1[3],XmNactivateCallback,input_sel,XtPointer(SVHS));
  XtAddEventHandler(input1[3],EnterWindowMask,False,help_mess,XtPointer(SVHSMESS));

  int in;
  XtVaGetValues(screen,XtNinputSelect,&in,NULL);
  XtVaSetValues(input1[in],XmNbottomShadowColor,0,NULL);
  XtVaSetValues(input1[in],
		XmNbottomShadowColor,
		BlackPixel(XtDisplay(input1[in]),
			   DefaultScreen(XtDisplay(input1[in]))),NULL);
}



/*                 */
/* Create Menu     */
/*                 */
void Menu(Widget &menu,Widget &win)
{
  Widget widget;
  XmString file,start,help,quit,capture,control,channel,fix,pbuttt;
  XmString audio,aauto,mono,stereo,bilang1,bilang2;
  XmString snap,seq,options;

  file=XmStringCreateLocalized("File");
  start=XmStringCreateLocalized("Start");
  audio=XmStringCreateLocalized("Audio");
  help=XmStringCreateLocalized("Help");
  menu = XmVaCreateSimpleMenuBar(win,"menubar",
			       XmVaCASCADEBUTTON,file,'F',
			       XmVaCASCADEBUTTON,start,'S',
			       XmVaCASCADEBUTTON,audio,'A',
			       XmVaCASCADEBUTTON,help,'H',
			       NULL);
  XmStringFree(file);
  XmStringFree(start);

  /* which one is help menu */
  if (widget =XtNameToWidget(menu,"button_3"))
    XtVaSetValues (menu,XmNmenuHelpWidget,widget,NULL);

  /* file menu point */
  quit=XmStringCreateLocalized("Quit");
  XmVaCreateSimplePulldownMenu(menu,"fileMenu",0,file_cb
			       ,XmVaPUSHBUTTON,quit,'Q',
			       "<Key>Q",XmStringCreateLocalized("Q"),
			       NULL);
  XmStringFree(quit);
  Atom WM_DELETE_WINDOW;
  
  WM_DELETE_WINDOW=XmInternAtom(XtDisplay(menu),"WM_DELETE_WINDOW",False);
  XmAddWMProtocolCallback(XtParent(win),WM_DELETE_WINDOW,file_cb,0);



  /* start menu point */
  capture=XmStringCreateLocalized("Cap on/off");
  control=XmStringCreateLocalized("Control");
  channel=XmStringCreateLocalized("Channel");
  options=XmStringCreateLocalized("Options");
  snap=XmStringCreateLocalized("Snapshot");
  seq=XmStringCreateLocalized("Sequence");
  
  if (app_data.dasp==A16_9)
    fix =XmStringCreateLocalized("Fix 16/9");
  else
    fix=XmStringCreateLocalized("Fix 4/3");

  XmVaCreateSimplePulldownMenu(menu,"startMenu",1,start_cb
			       ,XmVaPUSHBUTTON,capture,'C',
			       "<Key>C",XmStringCreateLocalized("C")
			       ,XmVaSEPARATOR
			       ,XmVaCHECKBUTTON,control,'o',
			       "<Key>O",XmStringCreateLocalized("O")
			       ,XmVaPUSHBUTTON,channel,'h',
			       "<Key>H",XmStringCreateLocalized("H")
			       ,XmVaCHECKBUTTON,options,'t',
			       "<Key>T",XmStringCreateLocalized("T")
			       ,XmVaSEPARATOR
			       ,XmVaCHECKBUTTON,fix,'F',
			       "<Key>F",XmStringCreateLocalized("F")
			       ,XmVaSEPARATOR
			       ,XmVaPUSHBUTTON,snap,'S',
			       "<Key>S",XmStringCreateLocalized("S")
			       ,XmVaPUSHBUTTON,seq,'E',
			       "<Key>E",XmStringCreateLocalized("E")
			       ,NULL);
  XmStringFree(capture);
  XmStringFree(control);
  XmStringFree(channel);
  XmStringFree(snap);
  XmStringFree(seq);
  XmStringFree(fix);
  XmStringFree(options);

  aauto=XmStringCreateLocalized("Auto");
  mono=XmStringCreateLocalized("Mono");
  stereo=XmStringCreateLocalized("Stereo");
  bilang1=XmStringCreateLocalized("Language 1");
  bilang2=XmStringCreateLocalized("Language 2");
  
  XmVaCreateSimplePulldownMenu(menu,"audioMenu",2,audio_cb
			       ,XmVaPUSHBUTTON,aauto,'A',
			       "<Key>A",XmStringCreateLocalized("A")
			       ,XmVaPUSHBUTTON,mono,'M',
			       "<Key>M",XmStringCreateLocalized("M")
			       ,XmVaPUSHBUTTON,stereo,'S',
			       "<Key>S",XmStringCreateLocalized("S")
			       ,XmVaPUSHBUTTON,bilang1,'1',
			       "<Key>1",XmStringCreateLocalized("1")
			       ,XmVaPUSHBUTTON,bilang2,'2',
			       "<Key>2",XmStringCreateLocalized("2")
			       ,NULL);
  
  XmStringFree(aauto);
  XmStringFree(mono);
  XmStringFree(stereo);
  XmStringFree(bilang1);
  XmStringFree(bilang2);


  input_buttons(menu);

  XmVaCreateSimplePulldownMenu(menu,"helpMenu",3,help_cb
			       ,XmVaPUSHBUTTON,help,'H',NULL,NULL,NULL);
  XmStringFree(help);

  channel_arrows(menu);


  XtManageChild(menu);
}

/*                    */
/* Create main window */
/*                    */
void MainWin(Widget &win,char *name)
{
  Widget menu;

  win = XtVaCreateManagedWidget(name,xmMainWindowWidgetClass,toplevel,NULL);
  screen=XtVaCreateManagedWidget("screen",tvscreenWidgetClass,win,NULL);
  
#ifndef LesstifVersion
  Message=XtVaCreateManagedWidget("Message",xmTextWidgetClass,win,
				  XmNvalue,"XTV: ",
				  XmNeditable, False,
				  XmNeditMode,XmSINGLE_LINE_EDIT,
				  XmNcursorPositionVisible,False,
				  XmNscrollHorizontal,False,
				  XmNwordWrap,True,
				  NULL);
 
#endif

  Menu(menu,win);
  
  ControlPop(menu);  
  OptionsPop(menu);
  XmMainWindowSetAreas(win,menu,NULL,NULL,NULL,screen);

#ifndef LesstifVersion
  XtVaSetValues(win,XmNmessageWindow,Message,NULL);
#endif
  XtAddEventHandler(win,StructureNotifyMask,False,sizecheck,NULL);
}



void ControlPop(Widget &w)
{
  Widget scale,pbutt0,pbutt1,cha,lastwidget;
  XmString pbuttt;

  dialog=XtVaCreatePopupShell("Control",xmDialogShellWidgetClass,
			      GetTopShell(w),NULL);

  pane = XtVaCreateWidget("ControlPop",xmPanedWindowWidgetClass,dialog,
			  XmNsashWidth,1,XmNsashHeight,1,
			  XmNheight,410,XmNwidth,350,
  			  XmNresizable,False,
			  NULL);
  

  conwid = XtVaCreateWidget("conwid",xmFormWidgetClass,pane,
			    XmNheight,250,XmNwidth,300,
			    XmNresizable,False,
			    NULL);
  

  cha = XtVaCreateWidget("cha",xmFormWidgetClass,pane,
			  XmNheight,160,XmNwidth,300,
			  XmNresizable,False,
			  NULL);
  

  Message2=XtVaCreateManagedWidget("Message",xmTextWidgetClass,pane,
				  XmNvalue,"XTV: ",
				  XmNeditable, False,
				  XmNeditMode,XmSINGLE_LINE_EDIT,
				  XmNcursorPositionVisible,False,
				  XmNscrollHorizontal,False,
				  XmNwordWrap,True,
				  XmNresizable,False,
				  NULL);




  /* Bright Scale */
  int bright;
  XtVaGetValues(screen,XtNbright,&bright,NULL);
  scale = XtVaCreateManagedWidget("Bright",xmScaleWidgetClass,conwid,
				  XtVaTypedArg,XmNtitleString,XmRString,
				  "Brightness",11,
				  XmNmaximum, 127,
				  XmNminimum, -127,
				  XmNvalue, bright,
				  XmNshowValue, True,
				  XmNorientation, XmHORIZONTAL,
				  XmNtopAttachment,XmATTACH_FORM,
				  XmNleftAttachment,XmATTACH_FORM,
				  NULL);


  XtAddCallback(scale, XmNvalueChangedCallback, new_dbch,XtPointer(1));
  XtAddCallback(scale, XmNdragCallback, new_dbch,XtPointer(1));
  XtAddEventHandler(scale,EnterWindowMask,False,help_mess,XtPointer(BRIGHTMESS));

  /* Color Scale */
  lastwidget=scale;
  int col;
  XtVaGetValues(screen,XtNcol,&col,NULL);
  scale = XtVaCreateManagedWidget("Color",xmScaleWidgetClass,conwid,
				  XtVaTypedArg,XmNtitleString,XmRString,
				  "Color",11,
				  XmNmaximum, 511,
				  XmNminimum, 0,
				  XmNvalue, col,
				  XmNshowValue, True,
				  XmNorientation, XmHORIZONTAL,
				  XmNtopAttachment,XmATTACH_WIDGET,
				  XmNleftAttachment,XmATTACH_FORM,
				  XmNtopWidget,lastwidget,
				  NULL);

  XtAddCallback(scale, XmNvalueChangedCallback, new_dbch,XtPointer(2));
  XtAddCallback(scale, XmNdragCallback, new_dbch,XtPointer(2));
  XtAddEventHandler(scale,EnterWindowMask,False,help_mess,XtPointer(COLORMESS));

  /* Hue Scale */
  lastwidget=scale;
  int hue;
  XtVaGetValues(screen,XtNhue,&hue,NULL);
  scale = XtVaCreateManagedWidget("Hue",xmScaleWidgetClass,conwid,
				  XtVaTypedArg,XmNtitleString,XmRString,
				  "Hue",4,
				  XmNmaximum, 127,
				  XmNminimum, -127,
				  XmNvalue, hue,
				  XmNshowValue, True,
				  XmNorientation, XmHORIZONTAL,
				  XmNtopAttachment,XmATTACH_WIDGET,
				  XmNleftAttachment,XmATTACH_FORM,
				  XmNtopWidget,lastwidget,
				  NULL);


  XtAddCallback(scale, XmNvalueChangedCallback, new_dbch,XtPointer(3));
  XtAddCallback(scale, XmNdragCallback, new_dbch,XtPointer(3));
  XtAddEventHandler(scale,EnterWindowMask,False,help_mess,XtPointer(HUEMESS));

  /* Contrast Scale */
  lastwidget=scale;
  int contrast;
  XtVaGetValues(screen,XtNcontrast,&contrast,NULL);
  scale = XtVaCreateManagedWidget("Contrast",xmScaleWidgetClass,conwid,
				  XtVaTypedArg,XmNtitleString,XmRString,
				  "Contrast",9,
				  XmNmaximum, 511,
				  XmNminimum, 0,
				  XmNvalue, contrast,
				  XmNshowValue, True,
				  XmNorientation, XmHORIZONTAL,
				  XmNtopAttachment,XmATTACH_WIDGET,
				  XmNleftAttachment,XmATTACH_FORM,
				  XmNtopWidget,lastwidget,
				  NULL);

  XtAddCallback(scale, XmNvalueChangedCallback, new_dbch,XtPointer(4));
  XtAddCallback(scale, XmNdragCallback, new_dbch,XtPointer(4));
  XtAddEventHandler(scale,EnterWindowMask,False,help_mess,XtPointer(CONTRASTMESS));

  /* Delay Scale */
  scale = XtVaCreateManagedWidget("Delay",xmScaleWidgetClass,conwid,
				  XtVaTypedArg,XmNtitleString,XmRString,
				  "Delay (in ms)",11,
				  XmNmaximum, 2000,
				  XmNminimum, 10,
				  XmNvalue, 20,
				  XmNshowValue, True,
				  XmNorientation, XmHORIZONTAL,
				  XmNtopAttachment,XmATTACH_FORM,
				  XmNrightAttachment,XmATTACH_FORM,
				  NULL);


  XtAddCallback(scale, XmNvalueChangedCallback, new_dbch,XtPointer(0));
  XtAddCallback(scale, XmNdragCallback, new_dbch,XtPointer(0));
  XtAddEventHandler(scale,EnterWindowMask,False,help_mess,XtPointer(DELAYMESS));


/* radiobox for norm selection */
  Widget radiob = XmCreateRadioBox(conwid,"radiob",NULL,0);
  XtVaSetValues(radiob,XmNtopAttachment,XmATTACH_WIDGET,
		XmNrightAttachment,XmATTACH_FORM,
		XmNtopWidget,scale,
		NULL);


  norm[PAL]=XtVaCreateManagedWidget("PAL",
				    xmToggleButtonGadgetClass,radiob,NULL);
  XtAddCallback(norm[PAL],XmNvalueChangedCallback,norm_tog,XtPointer(PAL));


  norm[NTSC]=XtVaCreateManagedWidget("NTSC",
				     xmToggleButtonGadgetClass,radiob,NULL);
  XtAddCallback(norm[NTSC],XmNvalueChangedCallback,norm_tog,XtPointer(NTSC));

  norm[SECAM]=XtVaCreateManagedWidget("SECAM",
				      xmToggleButtonGadgetClass,radiob,NULL);
  XtAddCallback(norm[SECAM],XmNvalueChangedCallback,norm_tog,XtPointer(SECAM));

  XtManageChild(radiob);

  XtAddEventHandler(radiob,EnterWindowMask,False,help_mess,XtPointer(NORMMESS));

/* radiobox for input selection */
  Widget radiob2 = XmCreateRadioBox(conwid,"radiob2",NULL,0);
  XtVaSetValues(radiob2,XmNtopAttachment,XmATTACH_WIDGET,
		XmNrightAttachment,XmATTACH_WIDGET,
		XmNtopWidget,scale,
		XmNrightWidget,radiob,
		NULL);


  input2[TUNER]=XtVaCreateManagedWidget("Tuner",
				    xmToggleButtonGadgetClass,radiob2,NULL);
  XtAddCallback(input2[TUNER],XmNvalueChangedCallback,
		input_sel,XtPointer(TUNER));


  input2[COMP1]=XtVaCreateManagedWidget("Comp1",
				    xmToggleButtonGadgetClass,radiob2,NULL);
  XtAddCallback(input2[COMP1],XmNvalueChangedCallback,
		input_sel,XtPointer(COMP1));

  input2[COMP2]=XtVaCreateManagedWidget("Comp2",
				    xmToggleButtonGadgetClass,radiob2,NULL);
  XtAddCallback(input2[COMP2],XmNvalueChangedCallback,
		input_sel,XtPointer(COMP2));

  input2[SVHS]=XtVaCreateManagedWidget("SVHS",
				    xmToggleButtonGadgetClass,radiob2,NULL);
  XtAddCallback(input2[SVHS],XmNvalueChangedCallback,
		input_sel,XtPointer(SVHS));


  XtManageChild(radiob2);
  XtAddEventHandler(radiob2,EnterWindowMask,False,help_mess,XtPointer(INPUTMESS));



/* Close Button */
  pbuttt=XmStringCreateLocalized("Close");
  pbutt0=XtVaCreateManagedWidget("Close",xmPushButtonWidgetClass,conwid,
				 XmNlabelString,pbuttt,
				 XmNbottomAttachment,XmATTACH_FORM,
				 XmNrightAttachment,XmATTACH_FORM,
				 NULL);

  XmStringFree(pbuttt);
  XtAddCallback(pbutt0,XmNactivateCallback,close,XtPointer(dialog));
  XtAddEventHandler(pbutt0,EnterWindowMask,False,help_mess,XtPointer(CLOSEMESS));
  
  lastwidget=pbutt0;


/* End Button */
  pbuttt=XmStringCreateLocalized("Quit");
  pbutt0=XtVaCreateManagedWidget("Quit",xmPushButtonWidgetClass,conwid,
				 XmNlabelString,pbuttt,
				 XmNbottomAttachment,XmATTACH_FORM,
				 XmNleftAttachment,XmATTACH_FORM,
				 NULL);
  XmStringFree(pbuttt);

  XtAddCallback(pbutt0,XmNactivateCallback,file_cb,XtPointer(0));
  XtAddEventHandler(pbutt0,EnterWindowMask,False,help_mess,XtPointer(QUITMESS));



  /* Volume Scale */
  int vol;
  get_master_vol(vol);
  scale = XtVaCreateManagedWidget("Volume",xmScaleWidgetClass,conwid,
				  XtVaTypedArg,XmNtitleString,XmRString,
				  "Volume",9,
				  XmNmaximum, 255,
				  XmNminimum, 0,
				  XmNvalue, vol,
				  XmNshowValue, True,
				  XmNorientation, XmHORIZONTAL,
				  XmNbottomAttachment,XmATTACH_WIDGET,
				  XmNrightAttachment,XmATTACH_FORM,
				  XmNbottomWidget,lastwidget,
				  NULL);

  XtAddCallback(scale, XmNvalueChangedCallback, new_dbch,XtPointer(5));
  XtAddCallback(scale, XmNdragCallback, new_dbch,XtPointer(5));
  XtAddEventHandler(scale,EnterWindowMask,False,help_mess,XtPointer(VOLMESS));






/* Channel part */

  /* Frequency Scale */
  int f;
  XtVaGetValues(screen,XtNfreq,&f,NULL);
  if (!f) f=1000;

  freq = XtVaCreateManagedWidget("Freq",xmScaleWidgetClass,cha,
				  XtVaTypedArg,XmNtitleString,XmRString,
				  "Frequency [MHz]",10,
				  XmNmaximum, int(CountryList.fmax())*100,
				  XmNminimum, int(CountryList.fmin())*100,
				  XmNwidth, 300,
				  XmNvalue, f*100/16,
				  XmNdecimalPoints,2,
				  XmNshowValue, True,
				  XmNorientation, XmHORIZONTAL,
				  XmNtopAttachment,XmATTACH_FORM,
				  XmNleftAttachment,XmATTACH_FORM,
				  NULL);
  



  /* Channel scale */
  int ch=CountryList.freqtochan(f);
  char channelstr[85];
  strcpy(channelstr,"Channel (");
  strcat(channelstr,CountryList.name);
  strcat(channelstr,")");
  chan = XtVaCreateManagedWidget("Channel",xmScaleWidgetClass,cha,
				  XtVaTypedArg,XmNtitleString,XmRString,
				  channelstr,sizeof(channelstr),
				  XmNmaximum, CountryList.cmax(),
				  XmNminimum, CountryList.cmin(),
				  XmNwidth, 120,
				  XmNvalue, ch,
				  XmNshowValue, True,
				  XmNorientation, XmHORIZONTAL,
				  XmNtopAttachment,XmATTACH_WIDGET,
				  XmNleftAttachment,XmATTACH_FORM,
				  XmNtopWidget,freq,
				  NULL);

  /* fine scale */
  int fi=CountryList.freqtofine(f);
  fine = XtVaCreateManagedWidget("fine",xmScaleWidgetClass,cha,
				  XtVaTypedArg,XmNtitleString,XmRString,
				  "Fine Tune",10,
				  XmNmaximum, 8*int(CountryList.smax()),
				  XmNminimum, - 8*int(CountryList.smax()),
				  XmNwidth, 120,
				  XmNvalue, fi,
				  XmNshowValue, True,
				  XmNorientation, XmHORIZONTAL,
				  XmNtopAttachment,XmATTACH_WIDGET,
				  XmNrightAttachment,XmATTACH_FORM,
				  XmNtopWidget,freq,
				  NULL);

  XtAddCallback(fine, XmNvalueChangedCallback, new_freq,NULL);
  XtAddCallback(fine, XmNdragCallback, new_freq,NULL);
  XtAddEventHandler(fine,EnterWindowMask,False,help_mess,XtPointer(FINEMESS));
  XtAddCallback(freq, XmNvalueChangedCallback, new_freq,NULL);
  XtAddCallback(freq, XmNdragCallback, new_freq,NULL);
  XtAddEventHandler(freq,EnterWindowMask,False,help_mess,XtPointer(FREQMESS));
  XtAddCallback(chan, XmNvalueChangedCallback, new_freq,NULL);
  XtAddCallback(chan, XmNdragCallback, new_freq,NULL);
  XtAddEventHandler(chan,EnterWindowMask,False,help_mess,XtPointer(CHANMESS));

  /*text field*/
  
  text=XtVaCreateManagedWidget("text",xmTextFieldWidgetClass,cha,
			       XmNcolumns, 25,
			       XmNlabelString,pbuttt,
			       XmNtopAttachment,XmATTACH_WIDGET,
			       XmNleftAttachment,XmATTACH_FORM,
			       XmNtopWidget,chan,
			       XmNeditMode,XmSINGLE_LINE_EDIT,
			       XmNresizable,False,
			       NULL);

  /* Channel Button */
  pbuttt=XmStringCreateLocalized("Channel");
  pbutt1=XtVaCreateManagedWidget("Channel",xmPushButtonWidgetClass,cha,
				 XmNlabelString,pbuttt,
				 XmNbottomAttachment,XmATTACH_FORM,
				 XmNleftAttachment,XmATTACH_FORM,
				 XmNtopAttachment,XmATTACH_WIDGET,
				 XmNtopWidget,text,
				 NULL);
  XmStringFree(pbuttt);
  XtAddCallback(pbutt1,XmNactivateCallback,chan_cb,XtPointer(0));
  XtAddEventHandler(pbutt1,EnterWindowMask,False,help_mess,XtPointer(CHANNELMESS));
  channel_popwidget(pbutt1);

  
  /* Add Channel Button */
  pbuttt=XmStringCreateLocalized("Add Channel");
  pbutt1=XtVaCreateManagedWidget("AChannel",xmPushButtonWidgetClass,cha,
				 XmNlabelString,pbuttt,
				 XmNbottomAttachment,XmATTACH_FORM,
				 XmNrightAttachment,XmATTACH_FORM,
				 XmNtopAttachment,XmATTACH_WIDGET,
				 XmNtopWidget,text,
				 NULL);
  XtManageChild(cha);

  XmStringFree(pbuttt);
  XtAddCallback(pbutt1,XmNactivateCallback,chan_cb,XtPointer(1));
  XtAddEventHandler(pbutt1,EnterWindowMask,False,help_mess,XtPointer(ADDMESS));
  XtAddCallback(text,XmNactivateCallback,chan_cb,XtPointer(1));
  XtAddEventHandler(text,EnterWindowMask,False,help_mess,XtPointer(TEXTMESS));
}  

void channel_popwidget(Widget &w)
{
  XmString pbuttt;
  Widget pbutt0,pbutt1,pbutt2;
  int n=0;
  XmString str,*strlist;
  static Arg args[10];
  
  if(!ChanW){
    ChanW=XtVaCreatePopupShell("Channel",xmDialogShellWidgetClass,
			       GetTopShell(w),NULL);
    rowcol=XtVaCreateWidget("rowcol",
			    xmRowColumnWidgetClass,ChanW,
			    XmNheight,200,XmNwidth,60,
			    XmNresizable,False,
			    NULL);

    XtSetArg(args[n],XmNvisibleItemCount,10);n++;
    list=XmCreateScrolledList(rowcol,"ch_list",args,n);

    XtVaSetValues(list,XmNselectionPolicy,XmSINGLE_SELECT,NULL);
    XtAddCallback(list,XmNsingleSelectionCallback,select,NULL);
    XtAddCallback(list,XmNdefaultActionCallback,select,NULL);
    XtAddEventHandler(list,EnterWindowMask,False,help_mess,XtPointer(LISTMESS));


    channel_arrows(rowcol);

    pbuttt=XmStringCreateLocalized("Delete");
    pbutt0=XtVaCreateManagedWidget("Delete",xmPushButtonWidgetClass,rowcol,
				   XmNlabelString,pbuttt,
				   NULL);

    XmStringFree(pbuttt);
    XtAddCallback(pbutt0,XmNactivateCallback,delete_item,NULL);
    XtAddEventHandler(pbutt0,EnterWindowMask,False,help_mess,XtPointer(DELETEMESS));


    pbuttt=XmStringCreateLocalized("Save");
    pbutt0=XtVaCreateManagedWidget("Save",xmPushButtonWidgetClass,rowcol,
				   XmNlabelString,pbuttt,
				   NULL);

    XmStringFree(pbuttt);
    XtAddCallback(pbutt0,XmNactivateCallback,save_xtv,NULL);
    XtAddEventHandler(pbutt0,EnterWindowMask,False,help_mess,XtPointer(SAVEMESS));


    pbuttt=XmStringCreateLocalized("Load");
    pbutt1=XtVaCreateManagedWidget("Load",xmPushButtonWidgetClass,rowcol,
				   XmNlabelString,pbuttt,
				   NULL);

    XmStringFree(pbuttt);
    XtAddCallback(pbutt1,XmNactivateCallback,load_xtv,NULL);
    XtAddEventHandler(pbutt1,EnterWindowMask,False,help_mess,XtPointer(LOADMESS));
    load_xtv(pbutt1,NULL,NULL);


    pbuttt=XmStringCreateLocalized("Close");
    pbutt2=XtVaCreateManagedWidget("Close",xmPushButtonWidgetClass,rowcol,
				   XmNlabelString,pbuttt,
				   NULL);

    XmStringFree(pbuttt);
    XtAddCallback(pbutt2,XmNactivateCallback,close,ChanW);
    XtAddEventHandler(pbutt2,EnterWindowMask,False,help_mess,XtPointer(CLOSEMESS));

  }
}

void OptionsPop(Widget &w)
{
  Widget scale;

  if(!OptW){
    OptW=XtVaCreatePopupShell("Option",xmDialogShellWidgetClass,
			       GetTopShell(w),NULL);
    
    
    OptF = XtVaCreateWidget("OptForm",xmFormWidgetClass,OptW,
			      XmNheight,100,XmNwidth,200,
			      XmNresizable,False,
			      NULL);
      
    scale = XtVaCreateManagedWidget("SequLen",xmScaleWidgetClass
				    ,OptF,
				    XtVaTypedArg,XmNtitleString,XmRString,
				    "Images per Sequence",21,
				    XmNmaximum, MAXSEQ,
				    XmNminimum, 1,
				    XmNvalue, app_data.seqnum,
				    XmNshowValue, True,
				    XmNorientation, XmHORIZONTAL,
				    XmNleftAttachment,XmATTACH_FORM,
				    XmNrightAttachment,XmATTACH_FORM,
				    NULL);
    XtAddCallback(scale, XmNvalueChangedCallback, new_seq,NULL);
    XtAddCallback(scale, XmNdragCallback, new_seq,NULL);
    XtAddEventHandler(scale,EnterWindowMask,False,help_mess,XtPointer(SEQMESS));
  }
}

