#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#if (defined(__unix__) || defined(unix)) && !defined(USG)
#include <sys/param.h>
#endif
#ifndef BSD
#include <sys/io.h>
#endif

#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <sys/stat.h>
#include <gtk/gtk.h>
#include "globs.h"
#include "boxes.h"
#include "couzon.h"
#include "network.h"
#include "radio.h"
#include "kb_bells/kb_bells.h"
static int fd_bells;
static guint main_key_handler_id;
static guint pec_key_handler_id;
static guint main_mouse_handler_id;
static GtkWidget *main_window;
static GtkWidget *main_drawing_area;
static gint argc_hold;
static gchar **argv_hold;
static gint configure_event(GtkWidget *widget, GdkEventConfigure *event);
static gint ce_delay(gpointer widget);
static gint expose_event(GtkWidget *widget, GdkEventExpose *event);
static gint update_display(GtkWidget *widget, gint idraw);
static gint timeout_callback(gpointer widget);
static gint kp_event(GtkWidget *widget, GdkEventKey *event, gpointer da);
static void tip_message(GtkWidget *widget, gint tip);
static gint process_word_command(GtkWidget *widget, ESVars *esd,
                                 gpointer da);
static void process_ctrl_key(guint ikey, ESVars *esd, gpointer da);
static void process_alt_key(guint ikey, ESVars *esd, gpointer da);
static void process_beam_command(GtkWidget *widget, guint istate);
static void process_delete_command(GtkWidget *widget);
static void delete_log_entry(GtkWidget *widget, gint qnum,
                             gint send_record);
static void process_edit_command(GtkWidget *widget);
static gint pec_key_handler(GtkWidget *widget, GdkEventKey *event,
                            gpointer data);
static void edit_time(guint ikey, gint qnum, ESVars *esd, gpointer da);
static void process_initial_command(GtkWidget *widget);
static void process_keyer_command(GtkWidget *widget);
static gint process_knowledge_command(GtkWidget *widget);
static void process_set_command(GtkWidget *widget);
static void process_ctrld_command(GtkWidget *widget, ESVars *esd);
static gint process_continent_command(void);
static gint process_zone_command(void);
static void change_bands(guint istate);
static void change_mode(void);
static gint quit_kb(GtkWidget *window);
static gint bp_event(GtkWidget *widget, GdkEventButton *event,
                     gpointer data);

#include "support.c"
#include "fields.c"
#include "couzon.c"
#include "tree.c"
#include "io.c"
#include "fboxes.c"
#include "sboxes.c"
#include "init.c"
#include "elogs.c"
#include "boxes.c"
#include "network.c"
#include "radio.c"

static gint configure_event(GtkWidget *widget, GdkEventConfigure *event)
{
  GdkRectangle update_rect;
  GdkGCValues values;
  PangoFontDescription *pfd;
  static gint first_time_bug = 0;

  first_time_bug++;
  if(first_time_bug<2) return TRUE;

  update_rect.x = 0;
  update_rect.y = 0;
  update_rect.width = widget->allocation.width;
  update_rect.height = widget->allocation.height;
  if(pixmap) g_object_unref (pixmap);
  pixmap = gdk_pixmap_new (widget->window,
			   update_rect.width,
			   update_rect.height,
			   -1);
  hrow = update_rect.height/NUMBER_OF_ROWS;
  wcol = update_rect.width/NUMBER_OF_COLUMNS;

  if(gc) g_object_unref (gc);
  gc = gdk_gc_new (pixmap);
  gdk_gc_get_values (gc, &values);
  values.line_width = 2;
  gdk_gc_set_line_attributes (gc,
                              values.line_width,
                              values.line_style,
                              values.cap_style,
                              values.join_style);

  if(layout) g_object_unref (layout);
  layout = gtk_widget_create_pango_layout (widget, NULL);
  pfd = pango_font_description_from_string ("misc fixed 16");
  pango_layout_set_font_description (layout, pfd);
  pango_font_description_free (pfd);

  draw_rectangle (&update_rect, BLUE, SAVEPIX);
  g_timeout_add (GTK_TIME_DELAY, ce_delay, widget);
  return TRUE;
}

static gint ce_delay(gpointer widget)
{
  update_display (widget, REDRAW);
  return FALSE;
}

static gint expose_event(GtkWidget *widget, GdkEventExpose *event)
{
  gdk_draw_drawable (widget->window,
		     widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
		     pixmap,
		     event->area.x,
                     event->area.y,
		     event->area.x,
                     event->area.y,
		     event->area.width,
                     event->area.height);
  return TRUE;
}

static gint update_display(GtkWidget *widget, gint idraw)
{
  switch (idisplay_type)
   {
    case IDT_WELCOME :
     welcome_screen (widget);
     break;
    case IDT_INITIAL :
     initial_screen (widget, REDRAW);
     break;
    case IDT_KEYER :
     keyer_screen (widget, REDRAW);
     break;
    case IDT_SET :
     set_screen (widget, REDRAW);
     break;
    case IDT_SUMMARY :
     if(idraw)
      {
       menu_bar (widget);
       date_and_time_bar (widget);
       black_bar (widget, 18, 1, 18, 80);
       black_bar (widget, 26, 1, 26, 80);
      }
     message_box (widget, "", idraw, !CLEAR);
     rate_box (widget, idraw);
     if(fp_keyer && mode==CW)
      keyer_box (widget, idraw);
     knowledge_box (widget, REDRAW);
     if(isel_contest==WW)
      zone_box (widget, idraw);
     summary_box (widget, idraw);
     scroll_log (widget);
     cyan_bar (widget);
     cyan_bar_titles (widget);
     break;
    case IDT_LOG :
     if(idraw)
      {
       menu_bar (widget);
       date_and_time_bar (widget);
       black_bar (widget, 26, 1, 26, 80);
      }
     message_box (widget, "", idraw, !CLEAR);
     scroll_log (widget);
     cyan_bar (widget);
     cyan_bar_titles (widget);
     break;
    case IDT_COUNTRY :
     if(idraw)
      {
       menu_bar (widget);
       date_and_time_bar (widget);
       black_bar (widget, 26, 1, 26, 80);
      }
     country_by_continent_box (widget);
     message_box (widget, "", idraw, !CLEAR);
     scroll_log (widget);
     cyan_bar (widget);
     cyan_bar_titles (widget);
     break;
    case IDT_COUZON :
     if(idraw)
      {
       menu_bar (widget);
       date_and_time_bar (widget);
       black_bar (widget, 26, 1, 26, 80);
      }
     country_by_zone_box (widget);
     message_box (widget, "", idraw, !CLEAR);
     scroll_log (widget);
     cyan_bar (widget);
     cyan_bar_titles (widget);
     break;
    case IDT_HELP :
     if(idraw)
      {
       menu_bar (widget);
       date_and_time_bar (widget);
       black_bar (widget, 26, 1, 26, 80);
      }
     help_box (widget);
     message_box (widget, "", idraw, !CLEAR);
     scroll_log (widget);
     cyan_bar (widget);
     cyan_bar_titles (widget);
     break;
    case IDT_ZONE6 :
     if(idraw)
      {
       menu_bar (widget);
       date_and_time_bar (widget);
       black_bar (widget, 26, 1, 26, 80);
      }
     zone6_box (widget);
     message_box (widget, "", idraw, !CLEAR);
     scroll_log (widget);
     cyan_bar (widget);
     cyan_bar_titles (widget);
     break;
    case IDT_INFO6 :
     if(idraw)
      {
       menu_bar (widget);
       date_and_time_bar (widget);
       black_bar (widget, 26, 1, 26, 80);
      }
     info6_box (widget);
     message_box (widget, "", idraw, !CLEAR);
     scroll_log (widget);
     cyan_bar (widget);
     cyan_bar_titles (widget);
     break;
    case IDT_LOCUS :
     if(idraw)
      {
       menu_bar (widget);
       date_and_time_bar (widget);
       black_bar (widget, 26, 1, 26, 80);
      }
     locus_box (widget);
     message_box (widget, "", idraw, !CLEAR);
     scroll_log (widget);
     cyan_bar (widget);
     cyan_bar_titles (widget);
     break;
    case IDT_PREFIX :
     if(idraw)
      {
       menu_bar (widget);
       date_and_time_bar (widget);
       black_bar (widget, 26, 1, 26, 80);
      }
     prefix_box (widget);
     message_box (widget, "", idraw, !CLEAR);
     scroll_log (widget);
     cyan_bar (widget);
     cyan_bar_titles (widget);
     break;
    case IDT_SUPER :
     mcv.row = 0;
     mcv.col = 0;
     mcv.button = 0;
     super_partial_box (widget);
     break;
    case IDT_WHO :
     if(idraw)
      {
       mcv.row = 0;
       mcv.col = 0;
       mcv.button = 0;
       menu_bar (widget);
       date_and_time_bar (widget);
       black_bar (widget, 26, 1, 26, 80);
      }
     who_box (widget, idraw);
     message_box (widget, "", idraw, !CLEAR);
     scroll_log (widget);
     cyan_bar (widget);
     cyan_bar_titles (widget);
     break;
    case IDT_PACKET :
     if(idraw)
      {
       mcv.row = 0;
       mcv.col = 0;
       mcv.button = 0;
       menu_bar (widget);
       date_and_time_bar (widget);
       black_bar (widget, 26, 1, 26, 80);
      }
     packet_box (widget, idraw, FALSE);
     message_box (widget, "", idraw, !CLEAR);
     scroll_log (widget);
     cyan_bar (widget);
     cyan_bar_titles (widget);
     break;
    case IDT_CREADER :
     if(idraw)
      {
       menu_bar (widget);
       date_and_time_bar (widget);
       black_bar (widget, 26, 1, 26, 80);
      }
     creader_volume_box (widget, idraw);
     creader_tuning_box (widget, idraw);
     creader_speed_box (widget, idraw);
     creader_box (widget, idraw);
     message_box (widget, "", idraw, !CLEAR);
     scroll_log (widget);
     cyan_bar (widget);
     cyan_bar_titles (widget);
     break;
    default :
     if(idraw)
      {
       menu_bar (widget);
       date_and_time_bar (widget);
       black_bar (widget, 26, 1, 26, 80);
      }
//     black_bar (widget, 18, 1, 18, 80);
     message_box (widget, "Kick Butt!", idraw, !CLEAR);
     rate_box (widget, idraw);
     if(fp_keyer && mode==CW)
      keyer_box (widget, idraw);
     knowledge_box (widget, REDRAW);
     if(isel_contest==WW)
      zone_box (widget, idraw);
     summary_box (widget, idraw);
     scroll_log (widget);
     cyan_bar (widget);
     cyan_bar_titles (widget);
   }
  return TRUE;
}

static gint timeout_callback(gpointer widget)
{
  gint cc_index;
  const gint cursor_fg_colors[4]={WHITE, HI_CYAN, YELLOW, RED};
  const gint cursor_bg_colors[4]={CYAN, LT_GRAY, RED, YELLOW};
  static gint iadd=0;

  iadd++;
  iadd %= 4;
  if(!ialt_box && !ialt_de && iscroll_log==icall_count)
   process_network_command (widget);
  if(iadd==0 || iadd==2)
   {
    if(isys_creader>-1)
     {
      fill_creader_array ();
      if(idisplay_type==IDT_CREADER && !ireset_display && !ialt_box)
       {
        creader_box (widget, !REDRAW);
        creader_volume_box (widget, !REDRAW);
        creader_tuning_box (widget, !REDRAW);
        creader_speed_box (widget, !REDRAW);
       }
     }
   }
  else if(iadd==1)
   get_kb_time ();
  if(idisplay_type>IDT_KEYER)
   {
    if(iadd==1)
     date_and_time_bar (widget);
    if(!strcmp(cr_array[icall_count+1].on_off, on_off_char[2]))
     on_off_box (widget);
    if(icaps_lock)
     capnum_lock_box (widget, 0);
    else if(inum_lock)
     capnum_lock_box (widget, 1);
   }
  if(idisplay_type>IDT_SET)
   {
    if(iadd==1 || iadd==2)
     if(!sending_cw)
      read_radio_port (iadd);
    radio_split_ind (widget, iadd);
    if(iscroll_log==icall_count || ialt_de=='E')
     {
      cc_index = 0;
      if(insert_mode)
       cc_index++;
      if(ialt_de=='E')
       cc_index += 2;
      cursor_control (widget, iadd, main_cursor_type, cbv.cursor_row,
                      cbv.cursor_col, cursor_fg_colors[cc_index],
                      cursor_bg_colors[cc_index]);
     }
   }
  return TRUE;
}

static gint kp_event(GtkWidget *widget, GdkEventKey *event, gpointer da)
{
  ESVars esd;
  guint dupe, answer;
  guint ikey, istate;
  gint i, ii, work_it, new_mult, icont, idt_phold, irnf, lfield, item_hold;
  gchar msg[51], rnf[5], call_px[14], spot_line[58], radio_freq[8];
  static guint ikey_kpe=0;
  
  ikey = event->keyval;
  istate = event->state;
  if(istate<16)
   inum_lock = TRUE;
  else if(istate==18)
   icaps_lock = TRUE;
  ikey_kpe = ikey;
  if(ikey==65421) /*keypad enter*/
   ikey = 65293; /*enter*/
  if(iscroll_log==icall_count)
   {
    if(cbv.item<FT_NEXT1)
     cbv.cursor_row = 24;
    else
     cbv.cursor_row = 25;
    esd.ikey = ikey;
    esd.cursor_row = cbv.cursor_row;
    esd.cursor_colp = &cbv.cursor_col;
    esd.field_col = cbv.col1[cbv.item];
    esd.field_length = cbv.field_length[cbv.item];
    set_esd_flp (icall_count+1, &esd);
   }
  if(idisplay_type==IDT_WHO)
   if(ikey!=65365 && ikey!=65366)
    {
     idisplay_type = idt_save;
     update_display (da, REDRAW);
    }
  switch (ikey)
   {
    case 65505 : /*left shift*/
    case 65506 : /*right shift*/
    case 65507 : /*left ctrl*/
    case 65508 : /*right ctrl*/
    case 65513 : /*left alt*/
    case 65514 : /*right alt*/
     break;
    case 65509 : /*caps lock*/
     if(istate==16 || istate==0)
      {
       icaps_lock = TRUE;
       message_box (da, "TURN OFF CAPS LOCK!!!", REDRAW, CLEAR);
       play_bells (MESSAGE_SOUND);
      }
     else if(istate==18 || istate==2)
      {
       icaps_lock = FALSE;
       scroll_log (da);
       message_box (da, "Caps Lock is off. Thank you.", REDRAW, CLEAR);
       play_bells (MESSAGE_SOUND);
      }
     if(ireset_display)
      {
       ireset_display = FALSE;
       update_display (da, REDRAW);
      }
     break;
    case 65407 : /*num lock*/
     if(istate==16 || istate==18)
      {
       inum_lock = TRUE;
       message_box (da, "TURN ON NUM LOCK!!!", REDRAW, CLEAR);
       play_bells (MESSAGE_SOUND);
      }
     else if(istate==0 || istate==2)
      {
       inum_lock = FALSE;
       scroll_log (da);
       message_box (da, "Num Lock is on. Thank you.", REDRAW, CLEAR);
       play_bells (MESSAGE_SOUND);
      }
     if(ireset_display)
      {
       ireset_display = FALSE;
       update_display (da, REDRAW);
      }
     break;
    case 65379 : /*insert*/
     if(iscroll_log==icall_count)
      {
       insert_mode = !insert_mode;
       edit_line (&esd);
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65289 : /*tab*/
    case 65056 : /*shift_tab*/
     if(iscroll_log==icall_count)
      {
       if(cbv.item==FT_CALL)
        {
         ialt_p = FALSE;
         reassemble_field (cr_array[icall_count+1].call);
         dupe = check_for_dupe (icall_count+1, icall_count, TRUE, da);
         if(dupe=='n' || dupe=='N')
          {
           clear_cyan_bar (da, &esd);
           increment_que ();
           if(idisplay_type==IDT_INFO6)
            {
             search_tree (icall_count, icall_count);
             update_display (da, REDRAW);
            }
           else
            cyan_bar_titles (da);
           break;
          }
         else if(dupe)
          strcpy(cr_array[icall_count+1].dupe, "*");
         else
          strcpy(cr_array[icall_count+1].dupe, "");
         if(isel_contest==WW && strlen(cr_array[icall_count+1].call))
          {
           get_worked_country (icall_count+1, call_px);
           if(countries.icountry_index==-1)
            get_country (icall_count+1, call_px);
          }
         fill_empty_fields ();
        }
       if(isel_contest==SS)
        {
         if(cbv.item==FT_RN)
          {
           lfield=strlen(cr_array[icall_count+1].rec_num);
           if(!check_field_line (icall_count+1))
            if(ikey==65289 && strlen(cr_array[icall_count+1].rec_num)<lfield)
             next_field (ikey, icall_count+1, &esd, da);
          }
         else if(cbv.item==FT_PX)
          {
           lfield=strlen(cr_array[icall_count+1].prefix);
           if(!check_field_line (icall_count+1))
            if(ikey==65289 && strlen(cr_array[icall_count+1].prefix)<lfield)
             next_field (ikey, icall_count+1, &esd, da);
          }
        }
       next_field (ikey, icall_count+1, &esd, da);
      }
     else
      tip_message (da, TIP_SCROLL);
     break;
    case 65361 : /*left_arrow*/
    case 65363 : /*right_arrow*/
    case 65360 : /*home*/
    case 65367 : /*end*/
     if(iscroll_log==icall_count)
      {
       if(valid_field (icall_count+1))
        edit_line (&esd);
       else
        play_bells (BAD_KEY_SOUND);
      }
     else
      tip_message (da, TIP_SCROLL);
     break;
    case 65307 : /*escape*/
     if(sending_cw)
      tip_message (da, TIP_PAUSE);
     else if(mode==PH)
      {
       if(get_running_pid ("sox")>-1)
        tip_message (da, TIP_PAUSE);
      }
    case 65535 : /*delete*/
    case 65288 : /*backspace*/
     if(iscroll_log==icall_count)
      {
       if(valid_field (icall_count+1))
        {
         edit_line (&esd);
         if(cbv.item==FT_CALL)
          {
           ipage_screen[0][IDT_SUPER] = 0;
           ipage_screen[1][IDT_SUPER] = 0;
           mcv.row = 0;
           mcv.col = 0;
           mcv.button = 0;
           if(!strlen(cr_array[icall_count+1].call))
            {
             ialt_p = FALSE;
             strcpy(cr_array[icall_count+1].dupe, "");
             cyan_bar (da);
             if(insert_mode)
              {
               esd.ikey = 65379;
               edit_line (&esd);
              }
            }
           super_partial_box (da);
          }
        }
       else
        play_bells (BAD_KEY_SOUND);
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65299 : /*pause_break*/
     if(mode==CW || tuning_up)
      {
       if(fp_keyer)
        {
         if(istate==24)
          {
           repeat_message = TRUE;
           process_cw_message (ikey, widget);
          }
         else
          {
           repeat_message = FALSE;
           tuning_up = FALSE;
           memset(cw_line, '\0', sizeof(cw_line));
           tcflush(fileno(fp_keyer), TCOFLUSH);
          }
        }
       else
        play_bells (BAD_KEY_SOUND);
      }
     else
      {
       if(istate==24)
        repeat_dvk_msg ();
       else
        {
         repeat_message = FALSE;
         stop_dvk_msg ();
        }
      }
     break;
    case 65362 : /*up_arrow*/
     if(iscroll_log>0)
      {
       iscroll_log--;       
       scroll_log (da);
       cyan_bar (da);
       if(idisplay_type==IDT_SUMMARY)
        {
         knowledge_index = get_locus_index (iscroll_log+1);
         if(knowledge_index>-1)
          knowledge_index += 1000;
         else
          knowledge_index = get_country_index (iscroll_log+1);
         if(isel_contest==WW)
          knowledge_zindex = get_zone_index (iscroll_log+1);
         if(isel_contest==WPX)
          strcpy(knowledge_prefix, cr_array[iscroll_log+1].prefix);
         if(ireset_display)
          {
           ireset_display = FALSE;
           update_display (da, REDRAW);
          }
         else
          knowledge_box (da, REDRAW);
        }
       else if(idisplay_type==IDT_INFO6)
        {
         search_tree (iscroll_log+1, icall_count);
         if(ireset_display)
          {
           ireset_display = FALSE;
           update_display (da, REDRAW);
          }
         else
          info6_box (da);
        }
      }
     else
      play_bells (BELL_SOUND);
     break;
    case 65364 : /*down_arrow*/
     if(iscroll_log<icall_count)
      {
       iscroll_log++;
       scroll_log (da);
       cyan_bar (da);
       if(insert_mode && (iscroll_log==icall_count))
        {
         esd.ikey = ikey;
         esd.cursor_row = cbv.cursor_row;
         esd.cursor_colp = &cbv.cursor_col;
         esd.field_col = cbv.col1[cbv.item];
         esd.field_length = cbv.field_length[cbv.item];
         set_esd_flp (icall_count+1, &esd);
         edit_line (&esd);
        }
       if(idisplay_type==IDT_SUMMARY)
        {
         knowledge_index =
          get_locus_index (iscroll_log<icall_count ?
                           iscroll_log+1 : icall_count);
         if(knowledge_index>-1)
          knowledge_index += 1000;
         else
          knowledge_index =
           get_country_index (iscroll_log<icall_count ?
                              iscroll_log+1 : icall_count);
         if(isel_contest==WW)
          knowledge_zindex = 
           get_zone_index (iscroll_log<icall_count ?
                           iscroll_log+1 : icall_count);
         if(isel_contest==WPX)
          strcpy(knowledge_prefix,
                 cr_array[(iscroll_log<icall_count ?
                 iscroll_log+1 : icall_count)].prefix);
         knowledge_box (da, REDRAW);
        }
       else if(idisplay_type==IDT_INFO6)
        {
         if(strlen(cr_array[icall_count+1].call) &&
            iscroll_log==icall_count)
          search_tree (icall_count+1, icall_count);
         else
          search_tree (iscroll_log<icall_count ?
                       iscroll_log+1 : icall_count, icall_count);
         info6_box (da);
        }
      }
     else
      play_bells (BELL_SOUND);
     break;
    case 65365 : /*page_up*/
     if(ireset_display)
      {
       idt_phold = idisplay_type;
       idisplay_type = IDT_SUPER;
      }
     if(ipage_screen[1][idisplay_type])
      {
       ipage_screen[0][idisplay_type]--;
       if(ipage_screen[0][idisplay_type]<0)
        ipage_screen[0][idisplay_type] = ipage_screen[1][idisplay_type];
       if(idisplay_type==IDT_PACKET)
        update_display (da, REDRAW);
       else
        update_display (da, !REDRAW);
      }
     else
      play_bells (BAD_KEY_SOUND);
     if(ireset_display)
      idisplay_type = idt_phold;
     break;
    case 65366 : /*page_down*/
     if(ireset_display)
      {
       idt_phold = idisplay_type;
       idisplay_type = IDT_SUPER;
      }
     if(ipage_screen[1][idisplay_type])
      {
       ipage_screen[0][idisplay_type]++;
       if(ipage_screen[0][idisplay_type]>ipage_screen[1][idisplay_type])
        ipage_screen[0][idisplay_type] = 0;
       if(idisplay_type==IDT_PACKET)
        update_display (da, REDRAW);
       else
        update_display (da, !REDRAW);
      }
     else
      play_bells (BAD_KEY_SOUND);
     if(ireset_display)
      idisplay_type = idt_phold;
     break;
    case 65458 : /*keypad down_arrow*/
     if(idisplay_type==IDT_PACKET && ipage_screen[0][idisplay_type])
      {
       mcv.row = 0;
       mcv.col = 0;
       mcv.button = 0;
       packet_bmap_page = 0;
       packet_band--;
       if(packet_band<0)
        packet_band = 5;
       packet_box (da, !REDRAW, FALSE);
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65464 : /*keypad up_arrow*/
     if(idisplay_type==IDT_PACKET && ipage_screen[0][idisplay_type])
      {
       mcv.row = 0;
       mcv.col = 0;
       mcv.button = 0;
       packet_bmap_page = 0;
       packet_band++;
       if(packet_band>5)
        packet_band = 0;
       packet_box (da, !REDRAW, FALSE);
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65460 : /*keypad left_arrow*/
     if(idisplay_type==IDT_PACKET && ipage_screen[0][idisplay_type])
      {
       mcv.row = 0;
       mcv.col = 0;
       mcv.button = 0;
       packet_bmap_page--;
       if(packet_bmap_page>-1)
        packet_box (da, !REDRAW, FALSE);
       else
        {
         packet_bmap_page = 0;
         play_bells (BELL_SOUND);
        }
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65462 : /*keypad right_arrow*/
     if(idisplay_type==IDT_PACKET && ipage_screen[0][idisplay_type])
      {
       mcv.row = 0;
       mcv.col = 0;
       mcv.button = 0;
       packet_bmap_page++;
       packet_box (da, !REDRAW, FALSE);
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65461 : /*keypad 5*/
     if(idisplay_type==IDT_PACKET && ipage_screen[0][idisplay_type])
      {
       mcv.row = 0;
       mcv.col = 0;
       mcv.button = 0;
       packet_box (da, !REDRAW, FALSE);
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65455 : /*keypad "/"*/
     if(fp_radio)
      split_freq_box ();
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65450 : /*keypad "*"*/
     lfield = strlen(cr_array[icall_count+1].call);
     if(icall_count || lfield)
      {
       strcpy(spot_line, "DX ");
       if(lfield)
        strcat(spot_line, cr_array[icall_count+1].call);
       else
        strcat(spot_line, cr_array[icall_count].call);
       if(fp_radio && rcv.radio_freq>1799.9)
        {
         strcat(spot_line, " ");
         real_to_char (rcv.radio_freq, radio_freq, 1);
         strcat(spot_line, radio_freq);
        }
       strcat(spot_line, " ");
       send_packet_box (spot_line, TRUE);
      }
     else
      play_bells (BELL_SOUND);
     break;
    case 65470 : /*F1*/
     if((shift_fkeys && istate==16) || (!shift_fkeys && istate==17))
      {
       if(mode==CW)
        {
         if(fp_keyer)
          process_cw_message (ikey, widget);
         else
          play_bells (BAD_KEY_SOUND);
        }
       else
        send_dvk_msg (ikey);
      }
     else
      {
       idisplay_type = IDT_SUMMARY;
       knowledge_index =
        get_locus_index (iscroll_log<icall_count ?
                         iscroll_log+1 : icall_count);
       if(knowledge_index>-1)
        knowledge_index += 1000;
       else
        knowledge_index =
         get_country_index (iscroll_log<icall_count ?
                            iscroll_log+1 : icall_count);
       if(isel_contest==WW)
        knowledge_zindex = 
         get_zone_index (iscroll_log<icall_count ?
                         iscroll_log+1 : icall_count);
       else if(isel_contest==WPX)
        strcpy(knowledge_prefix,
               cr_array[(iscroll_log<icall_count ?
               iscroll_log+1 : icall_count)].prefix);
       ireset_display = FALSE;
       update_display (da, REDRAW);
      }
     break;
    case 65471 : /*F2*/
     if((shift_fkeys && istate==16) || (!shift_fkeys && istate==17))
      {
       if(mode==CW)
        {
         if(fp_keyer)
          process_cw_message (ikey, widget);
         else
          play_bells (BAD_KEY_SOUND);
        }
       else
        send_dvk_msg (ikey);
      }
     else
      switch (isel_contest)
       {
        case DX :
        case WPX :
        case WW :
        case A160 :
        case TEN :
         idisplay_type = IDT_COUNTRY;
         icont = get_continent_index (icall_count);
         if(icont>-1)
          ipage_screen[0][idisplay_type] = icont;
         ireset_display = FALSE;
         update_display (da, REDRAW);
         break;
        case FD :
        case NYQP :
        case SS :
        case WAA :
         idisplay_type = IDT_LOCUS;
         ireset_display = FALSE;
         update_display (da, REDRAW);
         break;
        default :
         play_bells (BAD_KEY_SOUND);
       }
     break;
    case 65472 : /*F3*/
     if((shift_fkeys && istate==16) || (!shift_fkeys && istate==17))
      {
       if(mode==CW)
        {
         if(fp_keyer)
          process_cw_message (ikey, widget);
         else
          play_bells (BAD_KEY_SOUND);
        }
       else
        send_dvk_msg (ikey);
      }
     else
      {
       if(icall_count)
        {
         if(!strcmp(cr_array[icall_count+1].on_off, on_off_char[1]))
          strcpy(cr_array[icall_count+1].on_off, on_off_char[2]);
         else
          {
           strcpy(msg, "Terminate off-period? (Y/N)");
           play_bells (DUPE_SOUND);
           answer = boxed_message (msg, 0, LT_GRAY);
           if(answer=='y' || answer=='Y')
            {
             strcpy(cr_array[icall_count+1].on_off, on_off_char[1]);
             on_off_box (da);
             message_box (da, "Kick Butt!", !REDRAW, CLEAR);
             play_bells (MESSAGE_SOUND);
             scroll_log (da);
            }
          }
         cyan_bar (da);
         if(insert_mode && (iscroll_log==icall_count))
          {
           esd.ikey = 65379;
           edit_line (&esd);
          }
        }
       else
        play_bells (BAD_KEY_SOUND);
      }
     break;
    case 65473 : /*F4*/
     if((shift_fkeys && istate==16) || (!shift_fkeys && istate==17))
      {
       if(mode==CW)
        {
         if(fp_keyer)
          process_cw_message (ikey, widget);
         else
          play_bells (BAD_KEY_SOUND);
        }
       else
        send_dvk_msg (ikey);
      }
     else
      switch (isel_contest)
       {
        case WPX :
         idisplay_type = IDT_PREFIX;
         ireset_display = FALSE;
         update_display (da, REDRAW);
         break;
        case WW :
         idisplay_type = IDT_COUZON;
         if(strlen(cr_array[icall_count+1].rec_num)>3-mode)
          ipage_screen[0][idisplay_type] = get_zone_index (icall_count+1);
         else
          ipage_screen[0][idisplay_type] = get_zone_index (icall_count);
         ireset_display = FALSE;
         update_display (da, REDRAW);
         break;
        case FD :
        case NYQP :
        case TEN :
        case WAA :
         if(cbv.item==FT_CALL)
          {
           change_mode ();
           ireset_display = FALSE;
           update_display (da, REDRAW);
          }
         else
          {
           strcpy(msg, "Must be in call field to change mode!");
           message_box (da, msg, !REDRAW, CLEAR);
           play_bells (MESSAGE_SOUND);
          }
         break;
        default :
         play_bells (BAD_KEY_SOUND);
       }
     break;
    case 65474 : /*F5*/
     if((shift_fkeys && istate==16) || (!shift_fkeys && istate==17))
      {
       if(mode==CW)
        {
         if(fp_keyer)
          process_cw_message (ikey, widget);
         else
          play_bells (BAD_KEY_SOUND);
        }
       else
        send_dvk_msg (ikey);
      }
     else
      {
       switch (isel_contest)
        {
         case FD :
         case NYQP :
         case A160 :
         case TEN :
         case WAA :
          idisplay_type = IDT_CREADER;
          ireset_display = FALSE;
          update_display (da, REDRAW);
          break;
         default :
          if(mode==CW)
           {
            idisplay_type = IDT_CREADER;
            ireset_display = FALSE;
            update_display (da, REDRAW);
           }
          else
           play_bells (BAD_KEY_SOUND);
        }
      }
     break;
    case 65475 : /*F6*/
     if((shift_fkeys && istate==16) || (!shift_fkeys && istate==17))
      {
       if(mode==CW)
        {
         if(fp_keyer)
          process_cw_message (ikey, widget);
         else
          play_bells (BAD_KEY_SOUND);
        }
       else
        send_dvk_msg (ikey);
      }
     else
      {
       idisplay_type = IDT_INFO6;
       if(strlen(cr_array[icall_count+1].call) &&
          iscroll_log==icall_count)
        search_tree (icall_count+1, icall_count);
       else
        search_tree (iscroll_log<icall_count ?
                     iscroll_log+1 : icall_count, icall_count);
       ireset_display = FALSE;
       update_display (da, REDRAW);
      }
     break;
    case 65476 : /*F7*/
     if((shift_fkeys && istate==16) || (!shift_fkeys && istate==17))
      {
       if(mode==CW)
        {
         if(fp_keyer)
          process_cw_message (ikey, widget);
         else
          play_bells (BAD_KEY_SOUND);
        }
       else
        send_dvk_msg (ikey);
      }
     else if(cbv.item==FT_CALL && iscroll_log==icall_count)
      {
       switch (isel_contest)
        {
         case FD :
         case NYQP :
         case SS :
         case A160 :
         case TEN :
          if(idisplay_type!=IDT_WHO)
           idt_save = idisplay_type;
          idisplay_type = IDT_WHO;
          ireset_display = FALSE;
          update_display (da, REDRAW);
          break;
         case WW :
          idisplay_type = IDT_ZONE6;
          ireset_display = FALSE;
          update_display (da, REDRAW);
          break;
         default :
          play_bells (BAD_KEY_SOUND);
        }
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65477 : /*F8*/
     if((shift_fkeys && istate==16) || (!shift_fkeys && istate==17))
      {
       if(mode==CW)
        {
         if(fp_keyer)
          process_cw_message (ikey, widget);
         else
          play_bells (BAD_KEY_SOUND);
        }
       else
        send_dvk_msg (ikey);
      }
     else
      {
       idisplay_type = IDT_PACKET;
       ireset_display = FALSE;
       update_display (da, REDRAW);
      }
     break;
    case 65478 : /*F9*/
     if((shift_fkeys && istate==16) || (!shift_fkeys && istate==17))
      {
       if(mode==CW)
        {
         if(fp_keyer)
          process_cw_message (ikey, widget);
         else
          play_bells (BAD_KEY_SOUND);
        }
       else
        send_dvk_msg (ikey);
      }
     else
      switch (isel_contest)
       {
        case DX :
        case WPX :
        case FD :
        case NYQP :
        case WW :
        case SS :
        case WAA :
         if(cbv.item==FT_CALL)
          {
           change_bands (istate);
           ireset_display = FALSE;
           update_display (da, REDRAW);
           gdk_window_process_updates (widget->window, TRUE);
          }
         else
          {
           strcpy(msg, "Must be in call field to change bands!");
           message_box (da, msg, !REDRAW, CLEAR);
           play_bells (MESSAGE_SOUND);
          }
         break;
        case A160 :
        case TEN :
         idisplay_type = IDT_LOCUS;
         ireset_display = FALSE;
         update_display (da, REDRAW);
         break;
        default :
         play_bells (BAD_KEY_SOUND);
       }
     break;
    case 65482 : /*F10*/
     if((shift_fkeys && istate==16) || (!shift_fkeys && istate==17))
      {
       if(mode==CW)
        {
         if(fp_keyer)
          process_cw_message (ikey, widget);
         else
          play_bells (BAD_KEY_SOUND);
        }
       else
        send_dvk_msg (ikey);
      }
     else
      {
       if(iscroll_log<icall_count)
        {
         iscroll_log = icall_count;
         scroll_log (da);
         cyan_bar (da);
         if(insert_mode)
          {
           esd.ikey = 65379;
           esd.cursor_row = cbv.cursor_row;
           esd.cursor_colp = &cbv.cursor_col;
           esd.field_col = cbv.col1[cbv.item];
           esd.field_length = cbv.field_length[cbv.item];
           set_esd_flp (icall_count+1, &esd);
           edit_line (&esd);
          }
         if(idisplay_type==IDT_SUMMARY)
          {
           knowledge_index = get_locus_index (icall_count);
           if(knowledge_index>-1)
            knowledge_index += 1000;
           else
            knowledge_index = get_country_index (icall_count);
           if(isel_contest==WW)
            knowledge_zindex =  get_zone_index (icall_count);
           if(isel_contest==WPX)
            strcpy(knowledge_prefix, cr_array[icall_count].prefix);
           knowledge_box (da, REDRAW);
          }
         else if(idisplay_type==IDT_INFO6)
          {
           if(strlen(cr_array[icall_count+1].call))
            search_tree (icall_count+1, icall_count);
           else
            search_tree (icall_count, icall_count);
           info6_box (da);
          }
        } 
       else
        play_bells (BAD_KEY_SOUND);
      }
     break;
    case 65480 : /*F11*/
     if((shift_fkeys && istate==16) || (!shift_fkeys && istate==17))
      {
       if(mode==CW)
        {
         if(fp_keyer)
          process_cw_message (ikey, widget);
         else
          play_bells (BAD_KEY_SOUND);
        }
       else
        send_dvk_msg (ikey);
      }
     else
      {
       strcpy(msg, "Notes feature is not yet available.");
       play_bells (BAD_KEY_SOUND);
       boxed_message (msg, 2000, LT_GRAY);
      }
     break;
    case 65481 : /*F12*/
     if((shift_fkeys && istate==16) || (!shift_fkeys && istate==17))
      {
       if(mode==CW)
        {
         if(fp_keyer)
          process_cw_message (ikey, widget);
         else
          play_bells (BAD_KEY_SOUND);
        }
       else
        send_dvk_msg (ikey);
      }
     else
      update_box (REDRAW);
     break;
    case 65377 : /*PrtScrn*/
     if((shift_fkeys && istate==16) || (!shift_fkeys && istate==17))
      {
       if(fp_keyer && mode==CW)
        process_cw_message (ikey, widget);
       else
        play_bells (BAD_KEY_SOUND);
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case '<' :
     if(fp_keyer && mode==CW)
      {
       if(sending_cw)
        {
         cw_speed_adj *= 1.2;
         play_bells (SPEED_CHANGE_SOUND);
        }
       else
        {
         ii = 0;
         while(strcmp(keyer_screen_choices[0],
          speed_bracket[ii]) && ii<NUMBER_OF_SPEEDS-1)
           ii++;
         if(ii>0)
          {
           ii--;
           strcpy(keyer_screen_choices[0], speed_bracket[ii]);
           play_bells (SPEED_CHANGE_SOUND);
           if(idisplay_type!=IDT_SUMMARY || ireset_display)
            {
             strcpy(msg, "Keyer speed: ");
             concat(msg, speed_bracket[ii], 1, 4, !COPY);
             strcat(msg, " wpm");
             boxed_message (msg, 2000, LT_GRAY);
            }
           else
            keyer_box (da, !REDRAW);
           gdk_window_process_updates (widget->window, TRUE);
           write_ini_file ();
          }
         else
          play_bells (BELL_SOUND);
        }
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case '>' :
     if(fp_keyer && mode==CW)
      {
       if(sending_cw)
        {
         cw_speed_adj /= 1.2;
         play_bells (SPEED_CHANGE_SOUND);
        }
       else
        {
         ii = 0;
         while(strcmp(keyer_screen_choices[0],
          speed_bracket[ii]) && ii<NUMBER_OF_SPEEDS-1)
           ii++;
         if(ii<NUMBER_OF_SPEEDS-1)
          {
           ii++;
           strcpy(keyer_screen_choices[0], speed_bracket[ii]);
           play_bells (SPEED_CHANGE_SOUND);
           if(idisplay_type!=IDT_SUMMARY || ireset_display)
            {
             strcpy(msg, "Keyer speed: ");
             concat(msg, speed_bracket[ii], 1, 4, !COPY);
             strcat(msg, " wpm");
             boxed_message (msg, 2000, LT_GRAY);
            }
           else
            keyer_box (da, !REDRAW);
           gdk_window_process_updates (widget->window, TRUE);
           write_ini_file ();
          }
         else
          play_bells (BELL_SOUND);
        }
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case ',' :
     if(iscroll_log==icall_count)
      {
       qued.inq = TRUE;
       next_field (ikey, icall_count+1, &esd, da);
       esd.ikey = ikey;
       esd.cursor_row = cbv.cursor_row;
       esd.cursor_colp = &cbv.cursor_col;
       esd.field_col = cbv.col1[cbv.item];
       esd.field_length = cbv.field_length[cbv.item];
       set_esd_flp (icall_count+1, &esd);
       edit_line (&esd);
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65293 : /*enter*/
     if(iscroll_log==icall_count)
      {
       if(cbv.item==FT_CALL)
        {
         if(strlen(cr_array[icall_count+1].call))
          {
           if(process_word_command (widget, &esd, da))
            break;
           if(!strcmp(cr_array[icall_count+1].on_off, on_off_char[2]))
            {
             if(ireset_display)
              {
               ireset_display = FALSE;
               update_display (da, REDRAW);
              }
             tip_message (da, TIP_OFF);
             break;
            }
          }
         else
          {
           tip_message (da, TIP_ENTER);
           break;
          }
         ialt_p = FALSE;
         reassemble_field (cr_array[icall_count+1].call);
         dupe = check_for_dupe (icall_count+1, icall_count, TRUE, da);
         if(dupe=='n' || dupe=='N')
          {
           clear_cyan_bar (da, &esd);
           increment_que ();
           if(idisplay_type==IDT_INFO6)
            {
             search_tree (icall_count, icall_count);
             update_display (da, REDRAW);
            }
           else
            cyan_bar_titles (da);
           break;
          }
         else if(dupe)
          strcpy(cr_array[icall_count+1].dupe, "*");
         else
          strcpy(cr_array[icall_count+1].dupe, "");
        }
       work_it = FALSE;
       if(valid_call (icall_count+1) &&
          cbv.item!=FT_NEXT1 && cbv.item!=FT_NEXT2 &&
          cbv.item!=FT_NEXT3)
        switch (isel_contest)
         {
          case DX :
          case WPX :
          case NYQP :
          case A160 :
          case TEN :
           if(strlen(cr_array[icall_count+1].rec_num)>3-mode)
            {
             cbv.item = FT_RN;
             if(!check_field_line (icall_count+1))
              work_it = TRUE;
             else
              {
               strcpy(msg, "Invalid received report!");
               play_bells (BAD_KEY_SOUND);
               boxed_message (msg, 2000, LT_GRAY);
               cbv.item = FT_CALL;
              }
            }
           break;
          case WW :
           lfield = strlen(cr_array[icall_count+1].rec_num);
           if(lfield==3-mode && cbv.item==FT_CALL)
            work_it = TRUE;
           else if(lfield>3-mode)
            {
             item_hold = cbv.item;
             cbv.item = FT_RN;
             irnf = check_field_line (icall_count+1);
             if(!irnf)
              {
               work_it = TRUE;
               cbv.item = item_hold;
              }
             else if(irnf==2)
              {
               concat(rnf, cr_array[icall_count+1].rec_num, 3-mode,
                      lfield-1, COPY);
               strcpy(msg, "Invalid zone! - ");
               strcat(msg, rnf);
               play_bells (BAD_KEY_SOUND);
               boxed_message (msg, 2000, LT_GRAY);
               cbv.item = FT_CALL;
              }
             else if(irnf==3)
              {
               strcpy(msg, "Ambiguous entry!");
               play_bells (BAD_KEY_SOUND);
               boxed_message (msg, 2000, LT_GRAY);
               cbv.item = FT_CALL;
              }
             else
              {
               strcpy(msg, "Invalid received report!");
               play_bells (BAD_KEY_SOUND);
               boxed_message (msg, 2000, LT_GRAY);
               cbv.item = FT_CALL;
              }
            }
           break;
          case FD :
           if(strlen(cr_array[icall_count+1].rec_num)>1)
            {
             cbv.item = FT_RN;
             if(!check_field_line (icall_count+1))
              {
               if(strlen(cr_array[icall_count+1].rec_county)>1)
                {
                 cbv.item = FT_RC;
                 if(!check_field_line (icall_count+1))
                  work_it = TRUE;
                 else
                  {
                   strcpy(msg, "Invalid section!");
                   play_bells (BAD_KEY_SOUND);
                   boxed_message (msg, 2000, LT_GRAY);
                   cbv.item = FT_RN;
                  }
                }
              }
             else
              {
               strcpy(msg, "Invalid received report!");
               play_bells (BAD_KEY_SOUND);
               boxed_message (msg, 2000, LT_GRAY);
               cbv.item = FT_CALL;
              }
            }
           break;
          case SS :
           if(cbv.item==FT_RN)
            {
             lfield=strlen(cr_array[icall_count+1].rec_num);
             if(lfield)
              {
               if(!check_field_line (icall_count+1))
                {
                 if(strlen(cr_array[icall_count+1].rec_num)<lfield)
                  next_field (65289, icall_count+1, &esd, da);
                }
               else
                {
                 strcpy(msg, "Invalid received number!");
                 play_bells (BAD_KEY_SOUND);
                 boxed_message (msg, 2000, LT_GRAY);
                 cbv.item = FT_CALL;
                 ikey = 65289;
                 break;
                }
              }
            }
           else if(cbv.item==FT_TX)
            {
             if(strlen(cr_array[icall_count+1].tx))
              if(check_field_line (icall_count+1))
               {
                strcpy(msg, "Invalid precedence!");
                play_bells (BAD_KEY_SOUND);
                boxed_message (msg, 2000, LT_GRAY);
                cbv.item = FT_RN;
                ikey = 65289;
                break;
               }
            }
           else if(cbv.item==FT_PX)
            {
             lfield=strlen(cr_array[icall_count+1].prefix);
             if(lfield)
              {
               if(!check_field_line (icall_count+1))
                {
                 if(strlen(cr_array[icall_count+1].prefix)<lfield)
                  next_field (65289, icall_count+1, &esd, da);
                }
               else
                {
                 strcpy(msg, "Invalid check!");
                 play_bells (BAD_KEY_SOUND);
                 boxed_message (msg, 2000, LT_GRAY);
                 cbv.item = FT_TX;
                 ikey = 65289;
                 break;
                }
              }
            }
           else if(cbv.item==FT_RC)
            {
             if(strlen(cr_array[icall_count+1].rec_county))
              if(check_field_line (icall_count+1))
               {
                strcpy(msg, "Invalid section!");
                play_bells (BAD_KEY_SOUND);
                boxed_message (msg, 2000, LT_GRAY);
                cbv.item = FT_PX;
                ikey = 65289;
                break;
               }
            }
           if(strlen(cr_array[icall_count+1].rec_num) &&
              strlen(cr_array[icall_count+1].tx) &&
              strlen(cr_array[icall_count+1].prefix) &&
              strlen(cr_array[icall_count+1].rec_county))
            {
             cbv.item = FT_RN;
             if(!check_field_line (icall_count+1))
              {
               cbv.item = FT_TX;
               if(!check_field_line (icall_count+1))
                {
                 cbv.item = FT_PX;
                 if(!check_field_line (icall_count+1))
                  {
                   cbv.item = FT_RC;
                   if(!check_field_line (icall_count+1))
                    work_it = TRUE;
                   else
                    {
                     strcpy(msg, "Invalid section!");
                     play_bells (BAD_KEY_SOUND);
                     boxed_message (msg, 2000, LT_GRAY);
                     cbv.item = FT_PX;
                     ikey = 65289;
                    }
                  }
                 else
                  {
                   strcpy(msg, "Invalid check!");
                   play_bells (BAD_KEY_SOUND);
                   boxed_message (msg, 2000, LT_GRAY);
                   cbv.item = FT_TX;
                   ikey = 65289;
                  }
                }
               else
                {
                 strcpy(msg, "Invalid precedence!");
                 play_bells (BAD_KEY_SOUND);
                 boxed_message (msg, 2000, LT_GRAY);
                 cbv.item = FT_RN;
                 ikey = 65289;
                }
              }
             else
              {
               strcpy(msg, "Invalid received number!");
               play_bells (BAD_KEY_SOUND);
               boxed_message (msg, 2000, LT_GRAY);
               cbv.item = FT_CALL;
               ikey = 65289;
              }
            }
           break;
          case WAA :
           work_it = FALSE;
           if(strlen(cr_array[icall_count+1].rec_num) &&
              strlen(cr_array[icall_count+1].rec_county))
            work_it = TRUE;
           break;
         }
       if(work_it)
        {
         fill_cr_array_dt (icall_count+1);
         strcpy(cr_array[icall_count+1].on_off, on_off_char[0]);
         if(isel_contest!=SS)
          {
           if(!strcmp(init_screen_choices[11], "<M-TWO/1>") ||
              !strcmp(init_screen_choices[11], "<M-TWO/2>"))
            concat(cr_array[icall_count+1].tx, init_screen_choices[11],
                   7, 7, COPY);
           else
            strcpy(cr_array[icall_count+1].tx, "0");
          }
         new_mult = check_for_mult (icall_count+1, !RESTART);
         if(new_mult)
          {
           new_mult_message (da);
           new_mult_sound ();
          }
         fill_cr_array_pts (icall_count+1);
         increment_totals (icall_count+1);
         insert_tree (icall_count+1, icall_count);
         icall_count++;
         iscroll_log = icall_count;
         write_call_record (TRUE);
         cr_array[icall_count+1].iband = iband;
         cr_array[icall_count+1].mode = mode;
         if(isel_contest==FD || isel_contest==WAA)
          strcpy(cr_array[icall_count+1].rec_num, "");
         else if(isel_contest==SS)
          {
           strcpy(cr_array[icall_count+1].rec_num, "");
           strcpy(cr_array[icall_count+1].tx, "");
          }
         else
          strcpy(cr_array[icall_count+1].rec_num, fnn_char[mode]);
         strcpy(cr_array[icall_count+1].sent, set_screen_choices[0]);
         strcpy(cr_array[icall_count+1].on_off, on_off_char[1]);
         strcpy(cr_array[icall_count+1].dupe, "");
         strcpy(cr_array[icall_count+1].call, qued.call[0]);
         increment_que ();
         cbv.item = FT_CALL;
         cbv.cursor_col = cbv.col1[cbv.item]+
                          strlen(cr_array[icall_count+1].call);
         if(idisplay_type==IDT_INFO6)
          search_tree (icall_count, icall_count);
         else if(idisplay_type==IDT_COUNTRY)
          {
           for(i=0; i<7; i++)
            if(!strcmp(countries.continent_abv[countries.icountry_index],
             continent_abvs[i]))
             {
              ipage_screen[0][idisplay_type] = i;
              break;
             }
          }
         else if(idisplay_type==IDT_COUZON)
          ipage_screen[0][idisplay_type] = countries.izone_index;
         if(ireset_display)
          {
           ireset_display = FALSE;
           update_display (da, REDRAW);
          }
         else
          update_display (da, !REDRAW);
         if(insert_mode)
          {
           esd.cursor_colp = &cbv.cursor_col;
           esd.field_col = cbv.col1[cbv.item];
           esd.field_length = cbv.field_length[cbv.item];
           esd.field_linep = cr_array[icall_count+1].call;
           edit_line (&esd);
          }
         if(ikey_kpe==65421) /*keypad enter*/
          if(mode==CW && fp_keyer)
           process_cw_message (ikey_kpe, widget);
        }
       else
        {
         if(cbv.item==FT_CALL)
          fill_empty_fields ();
         next_field (ikey, icall_count+1, &esd, da);
        }
      }
     else
      tip_message (da, TIP_SCROLL);
     break;
    default :
     ikey = toupper_ui(ikey);
     if(valid_key (ikey, istate))
      {
       if(istate==20) /*ctrl-keys*/
        process_ctrl_key (ikey, &esd, da);
       else if(istate==24) /*alt-keys*/
        process_alt_key (ikey, &esd, da);
       else
        {
         if(iscroll_log==icall_count)
          {
           esd.ikey = ikey;
           edit_line (&esd);
           ipage_screen[0][IDT_SUPER] = 0;
           ipage_screen[1][IDT_SUPER] = 0;
           mcv.row = 0;
           mcv.col = 0;
           mcv.button = 0;
           super_partial_box (da);
          }
         else
          tip_message (da, TIP_SCROLL);
        }
      }
     else
      {
       strcpy(msg, "Invalid key!");
       boxed_message (msg, 2000, LT_GRAY);
       play_bells (BAD_KEY_SOUND);
      }
   }
  return TRUE;
}

static void tip_message(GtkWidget *widget, gint tip)
{
  gchar msg[49];

  switch (tip)
   {
    case TIP_REDRAW :
     strcpy(msg, "press alt-r");
     message_box (widget, msg, REDRAW, !CLEAR);
     strcpy(msg, "Tip: To restore menu bar");
     message_box (widget, msg, !REDRAW, !CLEAR);
     break;
    case TIP_SCROLL :
     if(!strcmp(set_screen_choices[11], "<YES>"))
      strcpy(msg, "press Shift-F10");
     else
      strcpy(msg, "press F10");
     message_box (widget, msg, REDRAW, CLEAR);
     strcpy(msg, "Tip: To return home");
     message_box (widget, msg, !REDRAW, !CLEAR);
     play_bells (MESSAGE_SOUND);
     break;
    case TIP_TIMER :
     if(!strcmp(set_screen_choices[11], "<YES>"))
      strcpy(msg, "press Shift-F3");
     else
      strcpy(msg, "press F3");
     message_box (widget, msg, REDRAW, CLEAR);
     strcpy(msg, "Tip: To resume operating");
     message_box (widget, msg, !REDRAW, !CLEAR);
     play_bells (MESSAGE_SOUND);
     break;
    case TIP_PAUSE :
     strcpy(msg, "Pause/Break stops a message");
     message_box (widget, msg, REDRAW, CLEAR);
     strcpy(msg, "Tip: Escape clears a field");
     message_box (widget, msg, !REDRAW, !CLEAR);
     play_bells (MESSAGE_SOUND);
     break;
    case TIP_DELETE :
     strcpy(msg, "then press alt_d");
     message_box (widget, msg, !REDRAW, CLEAR);
     strcpy(msg, "Scroll to desired entry,");
     message_box (widget, msg, !REDRAW, !CLEAR);
     play_bells (MESSAGE_SOUND);
     break;
    case TIP_EDIT :
     strcpy(msg, "then press alt_e");
     message_box (widget, msg, !REDRAW, CLEAR);
     strcpy(msg, "Scroll to desired entry,");
     message_box (widget, msg, !REDRAW, !CLEAR);
     play_bells (MESSAGE_SOUND);
     break;
    case TIP_CPACKET :
     strcpy(msg, "before starting packet connection!");
     message_box (widget, msg, !REDRAW, CLEAR);
     strcpy(msg, "Network client must be running");
     message_box (widget, msg, !REDRAW, !CLEAR);
     play_bells (MESSAGE_SOUND);
     break;
    case TIP_SPACKET :
     strcpy(msg, "before starting packet connection!");
     message_box (widget, msg, !REDRAW, CLEAR);
     strcpy(msg, "Network server must be running");
     message_box (widget, msg, !REDRAW, !CLEAR);
     play_bells (MESSAGE_SOUND);
     break;
    case TIP_ENTER :
     strcpy(msg, "a blank call field.");
     message_box (widget, msg, !REDRAW, CLEAR);
     strcpy(msg, "Use Tab to move from");
     message_box (widget, msg, !REDRAW, !CLEAR);
     play_bells (MESSAGE_SOUND);
     break;
    case TIP_OFF :
     strcpy(msg, "during an off-period!");
     message_box (widget, msg, !REDRAW, CLEAR);
     strcpy(msg, "Cannot enter a call");
     message_box (widget, msg, !REDRAW, !CLEAR);
     play_bells (MESSAGE_SOUND);
     break;
   }
}

static gint process_word_command(GtkWidget *widget, ESVars *esd,
                                 gpointer da)
{
  gchar msg[49];
  gint crdsp;
  guint answer;
  gint ipwc=FALSE;

  if(!strcmp(cr_array[icall_count+1].call, "QUIT") ||
     !strcmp(cr_array[icall_count+1].call, "EXIT"))
   {
    ipwc = TRUE;
    strcpy(msg, "Really exit? (Y/N)");
    answer = boxed_message (msg, 0, LT_GRAY);
    if(answer=='y' || answer=='Y')
     {
      quit_kb (widget);
      return ipwc;
     }
    else
     clear_cyan_bar (da, esd);
   }
  else if(!strcmp(cr_array[icall_count+1].call, "CLEAR"))
   {
    clear_cyan_bar (da, esd);
    message_box (da, "", !REDRAW, CLEAR);
    ipwc = TRUE;
   }
  else if(!strcmp(cr_array[icall_count+1].call, "CLIENT"))
   {
    if(sockfd<0)
     start_client (da);
    else
     {
      message_box (da, "Network client is running", !REDRAW, CLEAR);
      play_bells (MESSAGE_SOUND);
     }
    clear_cyan_bar (da, esd);
    ipwc = TRUE;
   }
  else if(!strcmp(cr_array[icall_count+1].call, "CREADER"))
   {
    if(mode==CW)
     {
      if(isys_creader<0)
       {
        strcpy(msg, "Start code reader? (Y/N)");
        answer = boxed_message (msg, 0, LT_GRAY);
        if(answer=='y' || answer=='Y')
         {
          crdsp = start_creader ();
          if(isys_creader<0)
           {
            message_box (da, "Error starting code reader!", !REDRAW, CLEAR);
            play_bells (MESSAGE_SOUND);
           }
          else
           {
            restart_creader = TRUE;
            idisplay_type = IDT_CREADER;
            update_display (da, REDRAW);
            if(crdsp)
             message_box (da, "Code reader is running with dsp.",
                          !REDRAW, CLEAR);
            else
             message_box (da, "Code reader is running.", !REDRAW, CLEAR);
            play_bells (MESSAGE_SOUND);
           }
         }
       }
      else
       {
        strcpy(msg, "Stop code reader? (Y/N)");
        answer = boxed_message (msg, 0, LT_GRAY);
        if(answer=='y' || answer=='Y')
         {
          stop_creader ();
          restart_creader = FALSE;
          message_box (da, "Code reader is stopped.", !REDRAW, CLEAR);
          play_bells (MESSAGE_SOUND);
         }
       }
     }
    else
     {
      message_box (da, "Code reader is for CW only!", !REDRAW, CLEAR);
      play_bells (MESSAGE_SOUND);
     }
    clear_cyan_bar (da, esd);
    ipwc = TRUE;
   }
  else if(!strcmp(cr_array[icall_count+1].call, "DIT3"))
   {
    if(fp_keyer)
     {
      repeat_message = FALSE;
      process_cw_message ('`', da);
     }
    else
     {
      if(ireset_display)
       {
        ireset_display = FALSE;
        update_display (da, REDRAW);
       }
      message_box (da, "Keyer port is not open", !REDRAW, CLEAR);
      play_bells (MESSAGE_SOUND);
     }
    clear_cyan_bar (da, esd);
    ipwc = TRUE;
   }
  else if(!strcmp(cr_array[icall_count+1].call, "DITZ"))
   {
    if(fp_keyer)
     {
      repeat_message = FALSE;
      process_cw_message (':', da);
     }
    else
     {
      if(ireset_display)
       {
        ireset_display = FALSE;
        update_display (da, REDRAW);
       }
      message_box (da, "Keyer port is not open", !REDRAW, CLEAR);
      play_bells (MESSAGE_SOUND);
     }
    clear_cyan_bar (da, esd);
    ipwc = TRUE;
   }
  else if(!strcmp(cr_array[icall_count+1].call, "FILTER"))
   {
    if(pbm.spot_filter)
     strcpy(msg, "Turn off spot filter? (Y/N)");
    else
     strcpy(msg, "Turn on spot filter? (Y/N)");
    answer = boxed_message (msg, 0, LT_GRAY);
    if(answer=='y' || answer=='Y')
     pbm.spot_filter = !pbm.spot_filter;
    clear_cyan_bar (da, esd);
    ipwc = TRUE;
   }
  else if(!strcmp(cr_array[icall_count+1].call, "GOTO"))
   {
    clear_cyan_bar (da, esd);
    goto_box (REDRAW);
    ipwc = TRUE;
   }
  else if(!strcmp(cr_array[icall_count+1].call, "HELP"))
   {
    clear_cyan_bar (da, esd);
    idisplay_type = IDT_HELP;
    update_display (da, REDRAW);
    ipwc = TRUE;
   }
  else if(!strcmp(cr_array[icall_count+1].call, "INITIAL"))
   {
    clear_cyan_bar (da, esd);
    process_initial_command (da);
    ipwc = TRUE;
   }
  else if(!strcmp(cr_array[icall_count+1].call, "KEYER"))
   {
    clear_cyan_bar (da, esd);
    process_keyer_command (da);
    ipwc = TRUE;
   }
  else if(!strcmp(cr_array[icall_count+1].call, "LOG"))
   {
    clear_cyan_bar (da, esd);
    idisplay_type = IDT_LOG;
    ireset_display = FALSE;
    update_display (da, REDRAW);
    ipwc = TRUE;
   }
  else if(!strcmp(cr_array[icall_count+1].call, "PACKET"))
   {
    if(strcmp(init_screen_choices[17], port_bracket[0]))
     {
      if(isys_packet<0)
       {
        if(isys_server>-1)
         {
          if(sockfd>-1)
           {
            strcpy(msg, "Start packet connection? (Y/N)");
            answer = boxed_message (msg, 0, LT_GRAY);
            if(answer=='y' || answer=='Y')
             {
              ipage_screen[0][idisplay_type] = 0;
              idisplay_type = IDT_PACKET;
              update_display (da, REDRAW);
              send_to_server (init_screen_choices[17], "N");
             }
           }
          else
           tip_message (da, TIP_CPACKET);
         }
        else
         tip_message (da, TIP_SPACKET);
       }
      else
       {
        message_box (da, "Packet client is running", !REDRAW, CLEAR);
        play_bells (MESSAGE_SOUND);
       }
     }
    else
     {
      message_box (da, "Packet port is off!", !REDRAW, CLEAR);
      play_bells (MESSAGE_SOUND);
     }
    clear_cyan_bar (da, esd);
    ipwc = TRUE;
   }
  else if(!strcmp(cr_array[icall_count+1].call, "REDRAW"))
   {
    clear_cyan_bar (da, esd);
    network_message_box (da, "", CLEAR);
    ireset_display = FALSE;
    update_display (da, REDRAW);
    ipwc = TRUE;
   }
  else if(!strcmp(cr_array[icall_count+1].call, "SERVER"))
   {
    if(isys_server<0)
     {
      strcpy(msg, "Start network server? (Y/N)");
      answer = boxed_message (msg, 0, LT_GRAY);
      if(answer=='y' || answer=='Y')
       {

#ifdef __FreeBSD__
//-----keep kb_server from inheriting /dev/speaker-----
        if(fd_bells > 0)
         close(fd_bells);
#endif

        system("./kb_server &");

#ifdef __FreeBSD__
        fd_bells = open("/dev/speaker", O_RDWR);
#endif

//-----wait for process to be added to ps, needed with fast machines!-----
        sleep(1);
        isys_server = get_running_pid ("kb_server");
        if(isys_server>-1)
         {
          message_box (da, "Network server started", !REDRAW, CLEAR);
          if(sockfd<0)
           start_client (da);
         }
        else
         {
          message_box (da, "Cannot start network server!", !REDRAW, CLEAR);
          play_bells (MESSAGE_SOUND);
         }
       }
     }
    else
     {
      message_box (da, "Network server is running", !REDRAW, CLEAR);
      play_bells (MESSAGE_SOUND);
     }
    clear_cyan_bar (da, esd);
    ipwc = TRUE;
   }
  else if(!strcmp(cr_array[icall_count+1].call, "SET"))
   {
    clear_cyan_bar (da, esd);
    process_set_command (da);
    ipwc = TRUE;
   }
  else if(!strcmp(cr_array[icall_count+1].call, "TUNE"))
   {
    if(fp_keyer)
     {
      repeat_message = FALSE;
      process_cw_message (';', da);
     }
    else
     {
      if(ireset_display)
       {
        ireset_display = FALSE;
        update_display (da, REDRAW);
       }
      message_box (da, "Keyer port is not open", !REDRAW, CLEAR);
      play_bells (MESSAGE_SOUND);
     }
    clear_cyan_bar (da, esd);
    ipwc = TRUE;
   }
  else if(process_continent_command ())
   {
    clear_cyan_bar (da, esd);
    ireset_display = FALSE;
    update_display (da, REDRAW);
    ipwc = TRUE;
   }
  else if(process_zone_command ())
   {
    clear_cyan_bar (da, esd);
    ireset_display = FALSE;
    update_display (da, REDRAW);
    ipwc = TRUE;
   }
  else if(process_frequency_command (cr_array[icall_count+1].call))
   {
    clear_cyan_bar (da, esd);
    ireset_display = FALSE;
    update_display (da, REDRAW);
    ipwc = TRUE;
   }
  if(ipwc)
   {
    increment_que ();
    cyan_bar_titles (da);
   }
  return ipwc;
}

static void process_ctrl_key(guint ikey, ESVars *esd, gpointer da)
{
  switch (ikey)
   {
    case 'B' :
     process_beam_command (da, 20);
     break;
    case 'D' :
     process_ctrld_command (da, esd);
     break;
    default :
     play_bells (BAD_KEY_SOUND);
   }
}

static void process_alt_key(guint ikey, ESVars *esd, gpointer da)
{
  gint pkc;
  guint dupe;
  gchar blank[2]="";

  switch (ikey)
   {
    case 'B' :
     process_beam_command (da, 24);
     break;
    case 'C' :
     send_packet_box (blank, FALSE);
     break;
    case 'D' :
     process_delete_command (da);
     break;
    case 'E' :
     process_edit_command (da);
     break;
    case 'G' :
     goto_box (REDRAW);
     break;
    case 'H' :
     idisplay_type = IDT_HELP;
     ireset_display = FALSE;
     update_display (da, REDRAW);
     break;
    case 'I' :
     process_initial_command (da);
     break;
    case 'K' :
     if(isel_contest==WW && cbv.item==FT_CALL && iscroll_log==icall_count)
      {
       ialt_p = FALSE;
       reassemble_field (cr_array[icall_count+1].call);
       dupe = check_for_dupe (icall_count+1, icall_count, TRUE, da);
       if(dupe=='n' || dupe=='N')
        {
         clear_cyan_bar (da, esd);
         increment_que ();
         if(idisplay_type==IDT_INFO6)
          {
           search_tree (icall_count, icall_count);
           update_display (da, REDRAW);
          }
         else
          cyan_bar_titles (da);
         break;
        }
       else if(dupe)
        strcpy(cr_array[icall_count+1].dupe, "*");
       else
        strcpy(cr_array[icall_count+1].dupe, "");
      }
     pkc = process_knowledge_command (da);
     if(isel_contest==WW && iscroll_log==icall_count && pkc)
      {
       esd->ikey = 65289; /*65289 = tab*/
       next_field (65289, icall_count+1, esd, da);
      }
     break;
    case 'L' :
     idisplay_type = IDT_LOG;
     ireset_display = FALSE;
     update_display (da, REDRAW);
     break;
    case 'M' :
     send_msg_box ();
     break;
    case 'O' :
     output_box (REDRAW);
     break;
    case 'P' :
     if(cbv.item==FT_CALL && iscroll_log==icall_count)
      {
       ialt_p = TRUE;
       super_partial_box (da);
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 'R' :
     network_message_box (da, "", CLEAR);
     ireset_display = FALSE;
     update_display (da, REDRAW);
     break;
    case 'S' :
     process_set_command (da);
     break;
    case 'W' :
     process_ctrld_command (da, esd);
     break;
    case 'Y' :
     process_keyer_command (da);
     break;
    default :
     play_bells (BAD_KEY_SOUND);
   }
}

static void process_beam_command(GtkWidget *widget, guint istate)
{
  gchar call_px[14];
  gchar msg[41]="Make an entry, then press ";

  if(ireset_display)
   {
    ireset_display = FALSE;
    update_display (widget, REDRAW);
   }
  if(iscroll_log==icall_count)
   {
    beam_index = get_locus_index_cr ();
    if(beam_index>-1)
     beam_index += 1000;
    else
     {
      if(strlen(cr_array[icall_count+1].call))
       {
        get_country (icall_count+1, call_px);
        if(countries.iplus_country_index>0)
         beam_index = countries.iplus_country_index;
        else
         beam_index = countries.icountry_index;
        if(!beam_index)
         {
          search_tree (icall_count+1, icall_count);
          get_worked_country (icall_count+1, call_px);
          if(countries.icountry_index>0)
           beam_index = countries.icountry_index;
          else
           countries.icountry_index = beam_index;
         }
       }
      else
       {
        if(istate==20)
         strcat(msg, "ctrl_b");
        else
         strcat(msg, "alt_b");
        message_box (widget, msg, !REDRAW, CLEAR);
        play_bells (MESSAGE_SOUND);
       }
     }
   }
  else
   {
    beam_index = get_locus_index (iscroll_log+1);
    if(beam_index>-1)
     beam_index += 1000;
    else
     beam_index = get_country_index (iscroll_log+1);
   }
  beam_heading (widget, istate);
}

static void process_delete_command(GtkWidget *widget)
{
  gchar msg[49];
  guint answer;

  if(icall_count)
   {
    if(iscroll_log==icall_count)
     {
      if(ireset_display)
       {
        ireset_display = FALSE;
        update_display (widget, REDRAW);
       }
      tip_message (widget, TIP_DELETE);
      return;
     }
    ialt_de = 'D';
    if(ireset_display)
     {
      ireset_display = FALSE;
      update_display (widget, REDRAW);
     }
    else
     {
      scroll_log (widget);
      cyan_bar (widget);
     }
    strcpy(msg, "Delete log entry? (Y/N)");
    answer = boxed_message (msg, 0, HI_RED);
    if(answer=='y' || answer=='Y')
     {
      delete_log_entry (widget, iscroll_log+1, TRUE);
      cbv.item = FT_CALL;
      cbv.cursor_col = cbv.col1[cbv.item]+
                       strlen(cr_array[icall_count+1].call);
     }
    ialt_de = FALSE;
    insert_mode = FALSE;
    if(strcmp(set_screen_choices[2], yn_bracket[1]))
     iscroll_log = icall_count;
    update_display (widget, REDRAW);
   }
  else
   play_bells (BAD_KEY_SOUND);
}

static void delete_log_entry(GtkWidget *widget, gint qnum,
                             gint send_record)
{
  gint i;

  write_backup_file (contest_file_name);
  cr_array[6498] = cr_array[qnum];
  switch (isel_contest)
   {
    case DX :
    case FD :
    case NYQP :
    case WW :
    case TEN :
    case A160 :
    case WAA :
     for(i=qnum; i<=icall_count; i++)
      {
       cr_array[i] = cr_array[i+1];
       int_to_char (i, cr_array[i].qso);
       left_blank_pad (cr_array[i].qso, sizeof(cr_array[i].qso));
      }
     for(i=1; i<4; i++)
      strcpy(cr_array[icall_count+i].call,
             cr_array[icall_count+i+1].call);
     icall_count--;
     break;
    case WPX :
     strcpy(cr_array[qnum].prefix, "");
     strcpy(cr_array[qnum].call, "NOQSO");
     strcpy(cr_array[qnum].rec_county, countries.country_abv[0]);
     if(qnum==icall_count)
      {
       cr_array[qnum] = cr_array[qnum+1];
       int_to_char (qnum, cr_array[qnum].qso);
       left_blank_pad (cr_array[qnum].qso, sizeof(cr_array[qnum].qso));
       for(i=1; i<4; i++)
        strcpy(cr_array[icall_count+i].call,
               cr_array[icall_count+i+1].call);
       icall_count--;
      }
     break;
    case SS :
     strcpy(cr_array[qnum].call, "NOQSO");
     strcpy(cr_array[qnum].rec_county, loci.locus_abv[0]);
     if(qnum==icall_count)
      {
       cr_array[qnum] = cr_array[qnum+1];
       int_to_char (qnum, cr_array[qnum].qso);
       left_blank_pad (cr_array[qnum].qso, sizeof(cr_array[qnum].qso));
       for(i=1; i<4; i++)
        strcpy(cr_array[icall_count+i].call,
               cr_array[icall_count+i+1].call);
       icall_count--;
      }
     break;
   }
  reset_arrays ();
  for(i=0; i<icall_count; i++)
   {
    if(check_for_dupe (i+1, i, FALSE, widget))
     strcpy(cr_array[i+1].dupe, "*");
    insert_tree (i+1, i);
   }
  for(i=1; i<=icall_count; i++)
   {
    check_for_mult (i, RESTART);
    fill_cr_array_pts (i);
    increment_totals (i);
   }
  rewrite_contest_file ();
  write_ded_file (6498, "D", send_record);
}

static void process_edit_command(GtkWidget *widget)
{
  gint idt_hold;
  ESVars esd;
  struct cyan_bar_vars cbv_save;

  if(icall_count)
   {
    if(iscroll_log==icall_count)
     {
      if(ireset_display)
       {
        ireset_display = FALSE;
        update_display (widget, REDRAW);
       }
      tip_message (widget, TIP_EDIT);
      return;
     }
    ialt_de = 'E';
    cbv.cursor_row = 24;
    cbv_save = cbv;
    idt_hold = idisplay_type;
    if(ireset_display)
     {
      ireset_display = FALSE;
      update_display (widget, REDRAW);
     }
    else
     {
      scroll_log (widget);
      cyan_bar (widget);
     }
    write_backup_file (contest_file_name);
    cr_array[6498] = cr_array[iscroll_log+1];
    cbv.item = FT_CALL;
    cbv.cursor_col = cbv.col1[cbv.item]+
                     strlen(cr_array[iscroll_log+1].call);
    esd.ikey = 65362; /*up_arrow*/
    esd.cursor_row = cbv.cursor_row;
    esd.cursor_colp = &cbv.cursor_col;
    esd.field_col = cbv.col1[cbv.item];
    esd.field_length = cbv.field_length[cbv.item];
    set_esd_flp (iscroll_log+1, &esd);
    edit_line (&esd);
    g_signal_handler_block (main_window, main_key_handler_id);
    pec_key_handler_id = g_signal_connect (G_OBJECT (main_window),
                           "key_press_event",
                           G_CALLBACK (pec_key_handler), &esd);
    gtk_main ();
    ialt_de = FALSE;
    insert_mode = FALSE;
    cbv = cbv_save;
    cbv.item = FT_CALL;
    cbv.cursor_col = cbv.col1[cbv.item]+
                     strlen(cr_array[icall_count+1].call);
    idisplay_type = idt_hold;
    if(strcmp(set_screen_choices[2], yn_bracket[1]))
     {
      iscroll_log = icall_count;
      search_tree (icall_count, icall_count);
     }
    main_cursor_type = LINE_CURSOR;
    update_display (widget, REDRAW);
   }
  else
   play_bells (BAD_KEY_SOUND);
}

static gint pec_key_handler(GtkWidget *widget, GdkEventKey *event,
                            gpointer data)
{
  gint i, icont;
  gchar msg[51], line_hold[8];
  ESVars *esd;
  guint ikey, istate;
  
  esd = (ESVars*)data;
  ikey = event->keyval;
  istate = event->state;
  esd->ikey = ikey;
  esd->cursor_row = cbv.cursor_row;
  esd->cursor_colp = &cbv.cursor_col;
  esd->field_col = cbv.col1[cbv.item];
  if(cbv.item==FT_SENT)
   if(isel_contest!=FD && isel_contest!=SS && isel_contest!=WAA)
    esd->field_col += cr_array[iscroll_log+1].mode;
  esd->field_length = cbv.field_length[cbv.item];
  set_esd_flp (iscroll_log+1, esd);
  switch (ikey)
   {
    case 65505 : /*left shift*/
    case 65506 : /*right shift*/
    case 65507 : /*left ctrl*/
    case 65508 : /*right ctrl*/
    case 65509 : /*caps lock*/
    case 65407 : /*num lock*/
    case 65513 : /*left alt*/
    case 65514 : /*right alt*/
     break;
    case 65379 : /*insert*/
     insert_mode = !insert_mode;
     edit_line (esd);
     break;
    case 65289 : /*tab*/
    case 65056 : /*shift_tab*/
     if(cbv.item==FT_CALL)
      reassemble_field (cr_array[iscroll_log+1].call);
     if(check_field_line (iscroll_log+1))
      {
       edit_line (esd);
       play_bells (BAD_CALL_SOUND);
       strcpy(msg, "Invalid Entry!");
       boxed_message (msg, 2000, LT_GRAY);
      }
     else
      next_field (ikey, iscroll_log+1, esd, main_drawing_area);
     break;
    case 65361 : /*left_arrow*/
    case 65363 : /*right_arrow*/
    case 65360 : /*home*/
    case 65367 : /*end*/
    case 65535 : /*delete*/
    case 65288 : /*backspace*/
     if(valid_field (iscroll_log+1) && cbv.item!=FT_IBAND &&
        cbv.item!=FT_MODE && cbv.item!=FT_TIME)
      edit_line (esd);
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65307 : /*escape*/
     if(istate==24)
      {
       if(valid_field (iscroll_log+1))
        {
         esd->ikey = ',';
         switch (cbv.item)
          {
           case FT_IBAND :
            cr_array[iscroll_log+1].iband = cr_array[6498].iband;
            break;
           case FT_MODE :
            cr_array[iscroll_log+1].mode = cr_array[6498].mode;
            if(isel_contest==TEN || isel_contest==NYQP)
             {
              strcpy(cr_array[iscroll_log+1].rec_num,
                     cr_array[6498].rec_num);
              strcpy(cr_array[iscroll_log+1].sent, cr_array[6498].sent);
             }
            break;
           case FT_TIME :
            strcpy(cr_array[iscroll_log+1].date, cr_array[6498].date);
            strcpy(cr_array[iscroll_log+1].time, cr_array[6498].time);
            cyan_bar (main_drawing_area);
            break;
           case FT_RC :
            strcpy(cr_array[iscroll_log+1].rec_county,
                   cr_array[6498].rec_county);
            break;
           case FT_CALL :
            strcpy(cr_array[iscroll_log+1].call, cr_array[6498].call);
            break;
           case FT_RN :
            strcpy(cr_array[iscroll_log+1].rec_num,
                   cr_array[6498].rec_num);
            break;
           case FT_SENT :
            strcpy(cr_array[iscroll_log+1].sent, cr_array[6498].sent);
            break;
          }
         set_esd_flp (iscroll_log+1, esd);
         edit_line (esd);
        }
       else
        play_bells (BAD_KEY_SOUND);
      }
     else
      {
       if(valid_field (iscroll_log+1) && cbv.item!=FT_IBAND &&
          cbv.item!=FT_MODE && cbv.item!=FT_TIME)
        edit_line (esd);
       else
        play_bells (BAD_KEY_SOUND);
      }
     break;
    case 65362 : /*up_arrow*/
     switch (cbv.item)
      {
       case FT_IBAND :
        cr_array[iscroll_log+1].iband++;
        if(cr_array[iscroll_log+1].iband>5)
         cr_array[iscroll_log+1].iband = 0;
        set_esd_flp (iscroll_log+1, esd);
        edit_line (esd);
        break;
       case FT_MODE :
        cr_array[iscroll_log+1].mode--;
        if(cr_array[iscroll_log+1].mode<0)
         cr_array[iscroll_log+1].mode = 1;
        if(isel_contest==TEN || isel_contest==NYQP)
         {
          if(cr_array[iscroll_log+1].mode==1)
           {
            concat(line_hold, cr_array[iscroll_log+1].sent, 0, 1, COPY);
            concat(line_hold, cr_array[iscroll_log+1].sent,
                   3, strlen(cr_array[iscroll_log+1].sent)-1, !COPY);
            strcpy(cr_array[iscroll_log+1].sent, line_hold);
            concat(line_hold, cr_array[iscroll_log+1].rec_num, 0, 1, COPY);
            concat(line_hold, cr_array[iscroll_log+1].rec_num,
                   3, strlen(cr_array[iscroll_log+1].rec_num)-1, !COPY);
            strcpy(cr_array[iscroll_log+1].rec_num, line_hold);
           }
          else
           {
            concat(line_hold, cr_array[iscroll_log+1].sent, 0, 1, COPY);
            strcat(line_hold, "9");
            concat(line_hold, cr_array[iscroll_log+1].sent,
                   2, strlen(cr_array[iscroll_log+1].sent)-1, !COPY);
            strcpy(cr_array[iscroll_log+1].sent, line_hold);
            concat(line_hold, cr_array[iscroll_log+1].rec_num, 0, 1, COPY);
            strcat(line_hold, "9");
            concat(line_hold, cr_array[iscroll_log+1].rec_num,
                   2, strlen(cr_array[iscroll_log+1].rec_num)-1, !COPY);
            strcpy(cr_array[iscroll_log+1].rec_num, line_hold);
           }
         }
        set_esd_flp (iscroll_log+1, esd);
        edit_line (esd);
        break;
       case FT_TIME :
        edit_time (ikey, iscroll_log+1, esd, main_drawing_area);
        break;
       default :
        play_bells (BAD_KEY_SOUND);
      }
     break;
    case 65364 : /*down_arrow*/
     switch (cbv.item)
      {
       case FT_IBAND :
        cr_array[iscroll_log+1].iband--;
        if(cr_array[iscroll_log+1].iband<0)
         cr_array[iscroll_log+1].iband = 5;
        set_esd_flp (iscroll_log+1, esd);
        edit_line (esd);
        break;
       case FT_MODE :
        cr_array[iscroll_log+1].mode++;
        if(cr_array[iscroll_log+1].mode>1)
         cr_array[iscroll_log+1].mode = 0;
        if(isel_contest==TEN || isel_contest==NYQP)
         {
          if(cr_array[iscroll_log+1].mode==1)
           {
            concat(line_hold, cr_array[iscroll_log+1].sent, 0, 1, COPY);
            concat(line_hold, cr_array[iscroll_log+1].sent,
                   3, strlen(cr_array[iscroll_log+1].sent)-1, !COPY);
            strcpy(cr_array[iscroll_log+1].sent, line_hold);
            concat(line_hold, cr_array[iscroll_log+1].rec_num, 0, 1, COPY);
            concat(line_hold, cr_array[iscroll_log+1].rec_num,
                   3, strlen(cr_array[iscroll_log+1].rec_num)-1, !COPY);
            strcpy(cr_array[iscroll_log+1].rec_num, line_hold);
           }
          else
           {
            concat(line_hold, cr_array[iscroll_log+1].sent, 0, 1, COPY);
            strcat(line_hold, "9");
            concat(line_hold, cr_array[iscroll_log+1].sent,
                   2, strlen(cr_array[iscroll_log+1].sent)-1, !COPY);
            strcpy(cr_array[iscroll_log+1].sent, line_hold);
            concat(line_hold, cr_array[iscroll_log+1].rec_num, 0, 1, COPY);
            strcat(line_hold, "9");
            concat(line_hold, cr_array[iscroll_log+1].rec_num,
                   2, strlen(cr_array[iscroll_log+1].rec_num)-1, !COPY);
            strcpy(cr_array[iscroll_log+1].rec_num, line_hold);
           }
         }
        set_esd_flp (iscroll_log+1, esd);
        edit_line (esd);
        break;
       case FT_TIME :
        edit_time (ikey, iscroll_log+1, esd, main_drawing_area);
        break;
       default :
        play_bells (BAD_KEY_SOUND);
      }
     break;
    case 65365 : /*page_up*/
     if(ipage_screen[1][idisplay_type])
      {
       ipage_screen[0][idisplay_type]--;
       if(ipage_screen[0][idisplay_type]<0)
        ipage_screen[0][idisplay_type] = ipage_screen[1][idisplay_type];
       update_display (main_drawing_area, !REDRAW);
       edit_line (esd);
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65366 : /*page_down*/
     if(ipage_screen[1][idisplay_type])
      {
       ipage_screen[0][idisplay_type]++;
       if(ipage_screen[0][idisplay_type]>ipage_screen[1][idisplay_type])
        ipage_screen[0][idisplay_type] = 0;
       update_display (main_drawing_area, !REDRAW);
       edit_line (esd);
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65470 : /*F1*/
     if((!shift_fkeys && istate==16) || (shift_fkeys && istate==17))
      {
       idisplay_type = IDT_SUMMARY;
       knowledge_index = get_locus_index (iscroll_log+1);
       if(knowledge_index>-1)
        knowledge_index += 1000;
       else
        knowledge_index = get_country_index (iscroll_log+1);
       if(isel_contest==WW)
        knowledge_zindex = get_zone_index (iscroll_log+1);
       else if(isel_contest==WPX)
        strcpy(knowledge_prefix, cr_array[iscroll_log+1].prefix);
       update_display (main_drawing_area, REDRAW);
       edit_line (esd);
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65471 : /*F2*/
     if((!shift_fkeys && istate==16) || (shift_fkeys && istate==17))
      {
       switch (isel_contest)
        {
         case DX :
         case WPX :
         case WW :
         case A160 :
         case TEN :
          idisplay_type = IDT_COUNTRY;
          icont = get_continent_index (iscroll_log+1);
          if(icont>-1)
           ipage_screen[0][idisplay_type] = icont;
          update_display (main_drawing_area, REDRAW);
          edit_line (esd);
          break;
         case FD :
         case NYQP :
         case SS :
         case WAA :
          idisplay_type = IDT_LOCUS;
          update_display (main_drawing_area, REDRAW);
          edit_line (esd);
          break;
         default :
          play_bells (BAD_KEY_SOUND);
        }
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65473 : /*F4*/
     if((!shift_fkeys && istate==16) || (shift_fkeys && istate==17))
      {
       switch (isel_contest)
        {
         case WW :
          if(strlen(cr_array[iscroll_log+1].rec_num)>3-mode)
           {
            idisplay_type = IDT_COUZON;
            ipage_screen[0][idisplay_type] =
             get_zone_index (iscroll_log+1);
            update_display (main_drawing_area, REDRAW);
            edit_line (esd);
           }
          else
           play_bells (BAD_KEY_SOUND);
          break;
         default :
          play_bells (BAD_KEY_SOUND);
        }
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65293 : /*enter*/
     if(cbv.item==FT_CALL)
      reassemble_field (cr_array[iscroll_log+1].call);
     if(check_field_line (iscroll_log+1))
      {
       edit_line (esd);
       play_bells (BAD_CALL_SOUND);
       strcpy(msg, "Invalid Entry!");
       boxed_message (msg, 2000, LT_GRAY);
       break;
      }
     reset_arrays ();
     for(i=0; i<icall_count; i++)
      {
       if(check_for_dupe (i+1, i, FALSE, main_drawing_area))
        strcpy(cr_array[i+1].dupe, "*");
       insert_tree (i+1, i);
      }
     if(strcmp(cr_array[6498].rec_county,
        cr_array[iscroll_log+1].rec_county))
      {
       search_tree (iscroll_log+1, icall_count);
       for(i=call_tree.clow; i<=call_tree.chigh; i++)
        if(call_tree.sort_order[i]!=iscroll_log+1)
         strcpy(cr_array[call_tree.sort_order[i]].rec_county,
                cr_array[iscroll_log+1].rec_county);
      }
     if(isel_contest==WW)
      {
       if((cr_array[6498].rec_num[3-mode] !=
          cr_array[iscroll_log+1].rec_num[3-mode]) ||
          (cr_array[6498].rec_num[4-mode] !=
          cr_array[iscroll_log+1].rec_num[4-mode]))
        {
         search_tree (iscroll_log+1, icall_count);
         for(i=call_tree.clow; i<=call_tree.chigh; i++)
          if(call_tree.sort_order[i]!=iscroll_log+1)
           {
            cr_array[call_tree.sort_order[i]].rec_num[3-mode] =
            cr_array[iscroll_log+1].rec_num[3-mode];
            cr_array[call_tree.sort_order[i]].rec_num[4-mode] =
            cr_array[iscroll_log+1].rec_num[4-mode];
           }
        }
      }
     for(i=1; i<=icall_count; i++)
      {
       if(i==iscroll_log+1 &&
          strcmp(cr_array[6498].call, cr_array[iscroll_log+1].call))
        {
         search_tree (i, icall_count);
         check_for_mult (i, !RESTART);
        }
       else
        check_for_mult (i, RESTART);
       fill_cr_array_pts (i);
       increment_totals (i);
      }
     rewrite_contest_file ();
     write_ded_file (6498, "E", TRUE);
     write_ded_file (iscroll_log+1, "e", TRUE);
     g_signal_handler_disconnect (main_window, pec_key_handler_id);
     g_signal_handler_unblock (main_window, main_key_handler_id);
     gtk_main_quit ();
     break;
    default :
     ikey = toupper_ui(ikey);
     if(valid_key (ikey, istate))
      {
       esd->ikey = ikey;
       edit_line (esd);
      }
     else
      play_bells (BAD_KEY_SOUND);
   }
  return TRUE;
}

static void edit_time(guint ikey, gint qnum, ESVars *esd, gpointer da)
{
  gint iqp1;
  guint qnum_sec, idd, ihh, imm, sperd, qm1_sec, qp1_sec;
  gchar dd[3], hh[3], mm[3], ss[3], date_save[7];

  sperd = 24*3600;
  if(qnum==1)
   qm1_sec = sperd;
  else
   {
    concat(dd, cr_array[qnum-1].date, 2, 3, COPY);
    concat(hh, cr_array[qnum-1].time, 0, 1, COPY);
    concat(mm, cr_array[qnum-1].time, 2, 3, COPY);
    concat(ss, cr_array[qnum-1].time, 4, 5, COPY);
    qm1_sec = atoi(dd)*sperd+atoi(hh)*3600+atoi(mm)*60+atoi(ss);
   }
  if(qnum==icall_count)
   {
    iqp1 = 6496;
    fill_cr_array_dt (iqp1);
   }
  else
   iqp1 = qnum+1;
  concat(dd, cr_array[iqp1].date, 2, 3, COPY);
  concat(hh, cr_array[iqp1].time, 0, 1, COPY);
  concat(mm, cr_array[iqp1].time, 2, 3, COPY);
  concat(ss, cr_array[iqp1].time, 4, 5, COPY);
  qp1_sec = atoi(dd)*sperd+atoi(hh)*3600+atoi(mm)*60+atoi(ss);
  strcpy(date_save, cr_array[qnum].date);
  concat(dd, cr_array[qnum].date, 2, 3, COPY);
  concat(hh, cr_array[qnum].time, 0, 1, COPY);
  concat(mm, cr_array[qnum].time, 2, 3, COPY);
  concat(ss, cr_array[qnum].time, 4, 5, COPY);
  qnum_sec = atoi(dd)*sperd+atoi(hh)*3600+atoi(mm)*60+atoi(ss);
  switch (ikey)
   {
    case 65362 : /*up_arrow*/
     if(qnum_sec>qm1_sec+60)
      qnum_sec -= 60;
     else
      play_bells (BELL_SOUND);
     break;
    case 65364 : /*down_arrow*/
     if(qnum_sec<qp1_sec-60)
      qnum_sec += 60;
     else
      play_bells (BELL_SOUND);
     break;
   }
  idd = qnum_sec/sperd;
  int_to_char (idd, dd);
  left_zero_pad (dd, sizeof(dd));
  ihh = (qnum_sec-idd*sperd)/3600;
  int_to_char (ihh, hh);
  left_zero_pad (hh, sizeof(hh));
  imm = (qnum_sec-idd*sperd-ihh*3600)/60;
  int_to_char (imm, mm);
  left_zero_pad (mm, sizeof(mm));
  concat(cr_array[qnum].date, date_save, 0, 1, COPY);
  concat(cr_array[qnum].date, dd, 0, 1, !COPY);
  concat(cr_array[qnum].date, date_save, 4, 5, !COPY);
  strcpy(cr_array[qnum].time, hh);
  strcat(cr_array[qnum].time, mm);
  strcat(cr_array[qnum].time, ss);
  cyan_bar (da);
  set_esd_flp (iscroll_log+1, esd);
  edit_line (esd);
}

static void process_initial_command(GtkWidget *widget)
{
  gint idt_hold;

  insert_mode = 0;
  idt_hold = idisplay_type;
  idisplay_type = IDT_INITIAL;
  ireset_display = FALSE;
  update_display (widget, REDRAW);
  idisplay_type = idt_hold;
  update_display (widget, REDRAW);
}

static void process_keyer_command(GtkWidget *widget)
{
  gint idt_hold;

  insert_mode = 0;
  idt_hold = idisplay_type;
  idisplay_type = IDT_KEYER;
  ireset_display = FALSE;
  update_display (widget, REDRAW);
  idisplay_type = idt_hold;
  update_display (widget, REDRAW);
}

static gint process_knowledge_command(GtkWidget *widget)
{
  gint lpx, kix_save;
  gchar call_px[14];
  gint pkc=TRUE;
  const gchar *pkcmsg = "Make an entry, then press alt_k";

  if(idisplay_type!=IDT_SUMMARY || ireset_display)
   {
    idisplay_type = IDT_SUMMARY;
    ireset_display = FALSE;
    update_display (widget, REDRAW);
   }
  if(iscroll_log==icall_count)
   {
    kix_save = knowledge_index;
    knowledge_index = get_locus_index_cr ();
    if(knowledge_index>-1)
     knowledge_index += 1000;
    else
     {
      if(strlen(cr_array[icall_count+1].call))
       {
        search_tree (icall_count+1, icall_count);
        get_worked_country (icall_count+1, call_px);
        if(countries.icountry_index==-1)
         get_country (icall_count+1, call_px);
        knowledge_index = countries.icountry_index;
        if(isel_contest==WW)
         knowledge_zindex = countries.izone_index;
       }
      else
       {
        knowledge_index = kix_save;
        message_box (widget, pkcmsg, !REDRAW, CLEAR);
        play_bells (MESSAGE_SOUND);
        pkc = FALSE;
        return pkc;
       }
     }
   }
  else
   {
    knowledge_index = get_locus_index (iscroll_log+1);
    if(knowledge_index>-1)
     knowledge_index += 1000;
    else
     {
      knowledge_index = get_country_index (iscroll_log+1);
      if(isel_contest==WW)
       knowledge_zindex = get_zone_index (iscroll_log+1);
     }
   }
  if(isel_contest==WPX)
   {
    if(iscroll_log==icall_count)
     {
      lpx = strlen(cr_array[icall_count+1].call);
      lpx = lpx<5 ? lpx : 5;
      concat(knowledge_prefix, cr_array[icall_count+1].call,
             0, lpx-1, COPY);
     }
    else
     strcpy(knowledge_prefix, cr_array[iscroll_log+1].prefix);
   }
  knowledge_box (widget, REDRAW);
  return pkc;
}

static void process_set_command(GtkWidget *widget)
{
  gint idt_hold;

  insert_mode = 0;
  idt_hold = idisplay_type;
  idisplay_type = IDT_SET;
  ireset_display = FALSE;
  update_display (widget, REDRAW);
  strcpy(cr_array[icall_count+1].sent, set_screen_choices[0]);
  idisplay_type = idt_hold;
  update_display (widget, REDRAW);
}

static void process_ctrld_command(GtkWidget *widget, ESVars *esd)
{
  clear_cyan_bar (widget, esd);
  increment_que ();
  if(idisplay_type==IDT_INFO6)
   {
    search_tree (icall_count, icall_count);
    ireset_display = FALSE;
    update_display (widget, REDRAW);
   }
  else if(ireset_display)
   {
    ireset_display = FALSE;
    update_display (widget, REDRAW);
   }
  else
   cyan_bar_titles (widget);
}

static gint process_continent_command(void)
{
  gint i;
  gint ipcc=FALSE;

  switch (isel_contest)
   {
    case DX :
    case WPX :
    case WW :
    case A160 :
    case TEN :
     for(i=0; i<7; i++)
      if(!strcmp(cr_array[icall_count+1].call, continent_abvs[i]))
       {
        ipcc = TRUE;
        idisplay_type = IDT_COUNTRY;
        ipage_screen[0][idisplay_type] = i;
        break;
       }
   }
  return ipcc;
}

static gint process_zone_command(void)
{
  gint i, izone, lcall;
  gint ipzc=FALSE;

  lcall = strlen(cr_array[icall_count+1].call);
  if(isel_contest==WW && lcall)
   {
    for(i=0; i<lcall; i++)
     if(!isdigit(cr_array[icall_count+1].call[i]))
      return ipzc;
    izone = atoi(cr_array[icall_count+1].call);
    if(izone>-1 && izone<41)
     {
      ipzc = TRUE;
      idisplay_type = IDT_COUZON;
      ipage_screen[0][idisplay_type] = izone;
     }
   }
  return ipzc;
}

static void change_bands(guint istate)
{
  if(istate==24)
   iband--;
  else
   iband++;
  if(iband<0)
   iband = 5;
  else if(iband>5)
   iband = 0;
  cr_array[icall_count+1].iband = iband;
  rcv.kbd_band_change = TRUE;
  play_bells (BAND_CHANGE_SOUND);
  if(!icall_count)
   {
    strcpy(init_screen_choices[14], bandw_bracket[iband]);
    write_ini_file ();
   }
  if(mode==PH)
   change_radio_mode (VFOA);
  change_radio_frequency (radio_default_freq[mode][iband], VFOA);
}

static void change_mode(void)
{
  gchar ssc_hold[9];

  repeat_message = FALSE;
  if(mode==CW)
   mode = PH;
  else
   mode = CW;
  cr_array[icall_count+1].mode = mode;
  if(!icall_count)
   strcpy(init_screen_choices[15], mode_bracket[mode]);
  play_bells (BAND_CHANGE_SOUND);
  switch (isel_contest)
   {
    case FD :
    case WAA :
     if(!icall_count)
      write_ini_file ();
     break;
    case NYQP :
    case TEN :
     strcpy(ssc_hold, set_screen_choices[0]);
     if(mode==CW)
      {
       concat(set_screen_choices[0], ssc_hold, 0, 1, COPY);
       strcat(set_screen_choices[0], "9");
       concat(set_screen_choices[0], ssc_hold, 2, strlen(ssc_hold), !COPY);
      }
     else
      {
       concat(set_screen_choices[0], ssc_hold, 0, 1, COPY);
       concat(set_screen_choices[0], ssc_hold, 3, strlen(ssc_hold), !COPY);
      }
     strcpy(cr_array[icall_count+1].rec_num, fnn_char[mode]);
     strcpy(cr_array[icall_count+1].sent, set_screen_choices[0]);
     write_ini_file ();
     break;
   }
  change_radio_mode (VFOA);
  change_radio_frequency (radio_default_freq[mode][iband], VFOA);
  if(mode==CW)
   {
    if(restart_creader)
     start_creader ();
   }
  else
   {
    if(isys_creader>0)
     stop_creader ();
   }
}

static gint quit_kb(GtkWidget *window)
{
  if(!inquire(contest_file_name))
   write_empty_file ();
  if(fp_keyer)
   set_keyer_port (CLOSE_SP);
  if(fp_radio)
   set_radio_port (CLOSE_SP);
  stop_dvk_msg ();
  if(sockfd>-1)
   close(sockfd);
  stop_creader ();
  gtk_widget_destroy (window);
  return TRUE;
}

static gint help_kb(GtkWidget *window)
{
  gchar msg[51];

  if(idisplay_type>IDT_SET)
   {
    if(ialt_box!='A')
     {
      idisplay_type = IDT_HELP;
      ireset_display = FALSE;
      update_display (main_drawing_area, REDRAW);
     }
    else
     {
      message_box (main_drawing_area, "ANSWER THE QUESTION!!!",
                   REDRAW, CLEAR);
      play_bells (MESSAGE_SOUND);
     }
   }
  else
   {
    strcpy(msg, "Help not available in this screen.");
    play_bells (BAD_KEY_SOUND);
    boxed_message (msg, 2000, LT_GRAY);
   }
  return TRUE;
}

static gint bp_event(GtkWidget *widget, GdkEventButton *event,
                     gpointer data)
{
  int x, y;
  GdkModifierType state;

  if(event->type==GDK_BUTTON_PRESS)
   {
    gdk_window_get_pointer (event->window, &x, &y, &state);
    if(ireset_display)
     {
      mcv.row_prev = mcv.row;
      mcv.col_prev = mcv.col;
      mcv.row = (y-1)/hrow+1;
      mcv.col = (x-1)/wcol+1;
      mcv.button = event->button;
      super_partial_box (widget);
     }
    else if(!pbv.done)
     {
      mcv.row_prev = mcv.row;
      mcv.col_prev = mcv.col;
      mcv.row = (y-1)/hrow+1;
      mcv.col = (x-1)/wcol+1;
      mcv.button = event->button;
      if(mcv.col>pbv.mouse_cols[0] && mcv.col<pbv.mouse_cols[1] &&
         mcv.row>21-pbv.num_match && mcv.row<23)
       {
        if(pbv.item==mcv.row-22+pbv.num_match)
         {
          pbv.done = TRUE;
          mcv.row = 0;
          mcv.col = 0;
          mcv.button = 0;
          if(pbv.mouse_cols[0]==49)
           pick_zone_box (widget, !REDRAW);
          else
           pick_country_box (widget, !REDRAW);
         }
        else
         {
          pbv.item = mcv.row-22+pbv.num_match;
          if(pbv.mouse_cols[0]==49)
           pick_zone_box (widget, !REDRAW);
          else
           pick_country_box (widget, !REDRAW);
         }
       }
     }
    else if(idisplay_type==IDT_PACKET && ipage_screen[0][idisplay_type])
     {
      mcv.row_prev = mcv.row;
      mcv.col_prev = mcv.col;
      mcv.button_prev = mcv.button;
      mcv.row = (y-1)/hrow+1;
      mcv.col = (x-1)/wcol+1;
      mcv.button = event->button;
      packet_box (widget, !REDRAW, FALSE);
     }
    else if(idisplay_type==IDT_WHO)
     {
      mcv.row_prev = mcv.row;
      mcv.col_prev = mcv.col;
      mcv.row = (y-1)/hrow+1;
      mcv.col = (x-1)/wcol+1;
      mcv.button = event->button;
      who_box (widget, !REDRAW);
     }
    else
     mcv.button = 0;
   }
  return TRUE;
}

int main (int argc, char *argv[])
{
  GtkWidget *window, *drawing_area, *vbox;
  GtkWidget *window_menu_bar, *menu_items;
  GtkWidget *root_file_menu, *file_menu;
  GtkWidget *root_help_menu, *help_menu, *help_menu_items;
  GtkKeySnoopFunc (p) (GtkWidget *window, GdkEventKey *event,
                       gpointer data);

  gtk_init (&argc, &argv);
  argc_hold = argc;
  argv_hold = argv;

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  main_window = window;

  gtk_window_fullscreen (GTK_WINDOW (window));

  gtk_widget_set_name (window, "Boxes");
  g_signal_connect_swapped (G_OBJECT (window), "destroy",
                            G_CALLBACK (gtk_main_quit),
                            G_OBJECT (window));

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), vbox);
  gtk_widget_show (vbox);

  file_menu = gtk_menu_new ();

  menu_items = gtk_menu_item_new_with_label ("Quit");
  gtk_menu_shell_append (GTK_MENU_SHELL (file_menu), menu_items);
  g_signal_connect_swapped (G_OBJECT (menu_items), "activate",
                            G_CALLBACK (quit_kb),
                            G_OBJECT (window));
  gtk_widget_show (menu_items);

  window_menu_bar = gtk_menu_bar_new ();
  gtk_box_pack_start (GTK_BOX (vbox), window_menu_bar, FALSE, FALSE, 2);
  gtk_widget_show (window_menu_bar);

  root_file_menu = gtk_menu_item_new_with_label ("File");
  gtk_widget_show (root_file_menu);

  gtk_menu_item_set_submenu (GTK_MENU_ITEM (root_file_menu), file_menu);
  gtk_menu_shell_append (GTK_MENU_SHELL (window_menu_bar), root_file_menu);

  help_menu = gtk_menu_new ();
  help_menu_items = gtk_menu_item_new_with_label ("KB Help");
  gtk_menu_shell_append (GTK_MENU_SHELL (help_menu), help_menu_items);
  g_signal_connect_swapped (G_OBJECT (help_menu_items), "activate",
                            G_CALLBACK (help_kb),
                            G_OBJECT (window));
  gtk_widget_show (help_menu_items);
  root_help_menu = gtk_menu_item_new_with_label ("Help");
  gtk_widget_show (root_help_menu);

  gtk_menu_item_set_submenu (GTK_MENU_ITEM (root_help_menu), help_menu);
  gtk_menu_shell_append (GTK_MENU_SHELL (window_menu_bar), root_help_menu);

  drawing_area = gtk_drawing_area_new ();
  main_drawing_area = drawing_area;

  gtk_widget_set_size_request (GTK_WIDGET (drawing_area), 200, 200);
  gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);

  gtk_widget_set_events (drawing_area,
                         gtk_widget_get_events (drawing_area)
                         | GDK_BUTTON_PRESS_MASK);

  g_signal_connect (G_OBJECT (drawing_area), "expose_event",
		    G_CALLBACK (expose_event), NULL);
  g_signal_connect (G_OBJECT (drawing_area), "configure_event",
		    G_CALLBACK (configure_event), NULL);
  gtk_widget_show (drawing_area);

  gtk_widget_show (window);

  g_timeout_add (246, timeout_callback, drawing_area);

  main_key_handler_id = g_signal_connect (G_OBJECT (window),
                        "key_press_event",
		        G_CALLBACK (kp_event), drawing_area);

  main_mouse_handler_id = g_signal_connect (G_OBJECT (drawing_area),
                          "button_press_event",
		          G_CALLBACK (bp_event), NULL);

  gtk_key_snooper_install (kp_snooper, drawing_area);

#ifdef __FreeBSD__
 fd_bells = open("/dev/speaker", O_RDWR);
#else
 fd_bells = open("/dev/kb_bells", O_RDWR);
#endif

  g_thread_init (NULL);
  gtk_main ();
  if(fd_bells > 0)
   close(fd_bells);

  return 0;
}
