static guint scb_key_handler_id;
static guint init_key_handler_id;
static guint init_mouse_handler_id;
static guint keyer_key_handler_id;
static guint keyer_mouse_handler_id;
static guint set_key_handler_id;
static guint set_mouse_handler_id;
static void set_fixed_font(GtkWidget *widget);
static void select_contest_box(GtkWidget *widget, gint idraw);
static gint scb_key_handler(GtkWidget *widget, GdkEventKey *event,
                            gpointer da);
static gint scbkh_delay(gpointer widget);
static void initial_screen(GtkWidget *widget, gint idraw);
static gint init_key_handler(GtkWidget *widget, GdkEventKey *event,
                             gpointer data);
static gint init_mouse_handler(GtkWidget *widget, GdkEventButton *event,
                               gpointer data);
static gint init_cursor(gpointer data);
static gint init_error_check(gpointer data);
static void keyer_screen(GtkWidget *widget, gint idraw);
static gint keyer_key_handler(GtkWidget *widget, GdkEventKey *event,
                             gpointer data);
static gint keyer_mouse_handler(GtkWidget *widget, GdkEventButton *event,
                                gpointer data);
static gint keyer_cursor(gpointer data);
static void set_screen(GtkWidget *widget, gint idraw);
static gint set_key_handler(GtkWidget *widget, GdkEventKey *event,
                             gpointer data);
static gint set_mouse_handler(GtkWidget *widget, GdkEventButton *event,
                              gpointer data);
static gint set_cursor(gpointer data);
static gint set_error_check(gpointer data);
static void set_error_message(gchar *s1, gint ssdpi);
static gint check_precedence(void);
static gint butt_kick(gpointer widget);
struct init_vars
{
  gint row1;
  gint col1;
  gint num1c_items;
  gint shift_col[2];
  gint cursor_row;
  gint cursor_col;
  gint item;
  gint done;
};
typedef struct init_vars ISVars;
typedef struct init_vars KSVars;
typedef struct init_vars SSVars;

static void welcome_screen(GtkWidget *widget)
{
  GdkRectangle update_rect;
  PangoFontDescription *pfd;
  gchar *kb_t1;
  gint i, j, ih, lcmd, idot, scini, sccfn, iusz, font_size, last_col;
  gchar copyright[14], version[15], error_msg[41];
  gchar cfb_hold[9], cfn_hold[13], qnum[5], qptv[2];
  gchar comp_mon[4], comp_yr[5];
  gchar font_desc[14];
  gint row1=1, col1=1;
  gint icm=0;
  gint qso_ptv[NUMBER_OF_CONTESTS][6] = {
   [DX] = {0, 3, 0, 0, 0, 0},
   [WPX]= {0, 1, 2, 3, 4, 6},
   [FD] = {0, 1, 2, 0, 0, 0},
   [WAE]= {0, 1, 2, 0, 0, 0},
   [NYQP]={0, 1, 2, 0, 0, 0},
   [WW] = {0, 2, 3, 0, 0, 0},
   [SS] = {0, 2, 0, 0, 0, 0},
   [A160]={0, 2, 5, 0, 0, 0},
   [TEN]= {0, 2, 4, 0, 0, 0},
   [WAA]= {0, 1, 0, 0, 0, 0}
  };
  gint cbv_fc[NUMBER_OF_CONTESTS][NUMBER_OF_FIELDS] = {
   [DX] = {7, 0, 63, 36, 50, 19, 22, 29, 0, 1, 14, 35, 79, 0, 36, 50, 64},
   [WPX]= {2, 0, 63, 36, 50, 14, 17, 26, 71, 30, 9, 35, 79, 0, 36, 50, 64},
   [FD] = {7, 13, 63, 36, 50, 19, 22, 29, 0, 1, 0, 35, 79, 0, 36, 50, 64},
   [NYQP]={7, 13, 63, 36, 50, 19, 22, 28, 0, 1, 0, 35, 79, 0, 36, 50, 64},
   [WW] = {7, 0, 63, 36, 50, 19, 22, 29, 0, 1, 14, 35, 79, 0, 36, 50, 64},
   [SS] = {2, 0, 66, 36, 50, 14, 17, 23, 60, 30, 9, 35, 79, 56, 36, 50, 64},
   [A160]={0, 8, 63, 36, 50, 19, 22, 28, 0, 1, 14, 35, 79, 0, 36, 50, 64},
   [TEN]= {0, 8, 63, 36, 50, 19, 22, 29, 0, 1, 14, 35, 79, 0, 36, 50, 64},
   [WAA]= {7, 13, 63, 36, 50, 19, 22, 28, 0, 1, 0, 35, 79, 0, 36, 50, 64}
  };
  const gchar *off_time[NUMBER_OF_CONTESTS] = {
   [DX]="15", [WPX]="60", [FD]="15", [WAE]="15", [NYQP]="15", [WW]="15",
   [SS]="30", [A160]="15", [TEN]="30", [WAA]="15"};
  const gchar *us_zones[3] = {"05", "04", "03"};

  utc_offset = atoi(init_screen_choices[10]);
  get_kb_time ();
  kb_t1 = asctime(&kb_time);
  concat(year_char, kb_t1, 22, 23, COPY);
  concat(full_year, kb_t1, 20, 23, COPY);
  update_rect.x = 0;
  update_rect.y = 0;
  update_rect.width = widget->allocation.width;
  update_rect.height = widget->allocation.height;
  draw_rectangle (&update_rect, BLUE, SAVEPIX);
  font_size = (48*widget->allocation.width)/800;
  snprintf(font_desc, sizeof(font_desc), "luxi mono %d", font_size);
  pfd = pango_font_description_from_string (font_desc);
  pango_layout_set_font_description (layout, pfd);
  pango_font_description_free (pfd);
  write_line (row1, col1+9, "KB (Kick Butt!)", HI_GREEN, SAVEPIX);
  pango_layout_set_text (layout, "Contest Program", 15);
  gdk_draw_layout (pixmap,
                   gc,
                   (col1+9)*wcol,
                   (row1+9)*hrow+hrow/2,
                   layout);
  set_fixed_font (widget);
  concat(comp_mon, __DATE__, 0, 2, COPY);
  concat(comp_yr, __DATE__, 7, 10, COPY);
  while(strcmp(comp_mon, month_array[icm]) && icm<11)
   icm++;
  strcpy(version, "Version: ");
  if(*(comp_yr+2)=='0')
   concat(version, comp_yr, 3, 3, !COPY);
  else
   concat(version, comp_yr, 2, 3, !COPY);
  strcat(version, ".");
  concat(version, month_num, 2*icm, 2*icm+1, !COPY);
  write_line (row1+18, col1+60, version, HI_GREEN, SAVEPIX);
  write_line (row1+17, col1+4, "WA1KKM + K7RI", HI_GREEN, SAVEPIX);
  write_line (row1+18, col1+7, "Software", HI_GREEN, SAVEPIX);
  strcpy(copyright, "(C) 1990-");
  strcat(copyright, comp_yr);
  write_line (row1+19, col1+4, copyright, HI_GREEN, SAVEPIX);
  write_line (row1+19, col1+60, "License: GPL", HI_GREEN, SAVEPIX);
  gtk_widget_queue_draw_area (widget,
                              update_rect.x,
                              update_rect.y,
                              update_rect.width,
                              update_rect.height);
  butt_kick (widget);
  g_timeout_add (250, butt_kick, widget);
  switch (kb_time.tm_mon+1)
   {
    case 1 :
     mode = CW;
     isel_contest = -(DX+NUMBER_OF_CONTESTS);
     break;
    case 2 :
     mode = CW;
     isel_contest = DX;
     break;
    case 3 :
     mode = PH;
     if(kb_time.tm_mday<23)
      isel_contest = DX;
     else
      isel_contest = WPX;
     break;
    case 4 :
     mode = CW;
     isel_contest = -(WPX+NUMBER_OF_CONTESTS);
     break;
    case 5 :
     mode = CW;
     isel_contest = WPX;
     break;
    case 6 :
     mode = CW;
     if(kb_time.tm_mday<8)
      isel_contest = -(WPX+NUMBER_OF_CONTESTS);
     else
      isel_contest = FD;
     break;
    case 7 :
     mode = CW;
     isel_contest = -(FD+NUMBER_OF_CONTESTS);
     break;
    case 8 :
     mode = CW;
     isel_contest = WAE;
     break;
    case 9 :
     mode = PH;
     isel_contest = WAE;
     break;
    case 10 :
     mode = PH;
     if(kb_time.tm_mday<22)
      isel_contest = NYQP;
     else
      isel_contest = WW;
     break;
    case 11 :
//----fix mode and days for ss-----
//----latest ssph=23,earliest wwcw=22-----
     mode = CW;
     if(kb_time.tm_mday<22)
      {
       isel_contest = SS;
       if(kb_time.tm_mday>11) mode = PH;
      }
     else
      isel_contest = WW;
     break;
    case 12 :
     mode = CW;
     if(kb_time.tm_mday<9)
      isel_contest = A160;
     else
      isel_contest = TEN;
     break;
   }
  switch (argc_hold)
   {
    case 1 :
//     printf("no command line\n");
     switch (isel_contest)
      {
       case DX :
       case WPX :
       case WAE :
       case WW :
       case SS :
        strcpy(contest_file_base, contest_abvs[isel_contest]); 
        strcat(contest_file_base, mode_char[mode]);
        strcpy(contest_file_name, contest_file_base);
        strcat(contest_file_name, ".");
        strcat(contest_file_name, year_char);
        if(!inquire(contest_file_name))
         select_contest_box (widget, REDRAW);
        break;
       case FD :
        strcpy(contest_file_base, contest_abvs[isel_contest]);

#ifdef KBVHF
        strcat(contest_file_base, "VHF");
#endif

        strcpy(contest_file_name, contest_file_base);
        strcat(contest_file_name, ".");
        strcat(contest_file_name, year_char);
        if(!inquire(contest_file_name))
         select_contest_box (widget, REDRAW);
        break;
       case NYQP :
       case A160 :
       case TEN :
       case WAA :
        strcpy(contest_file_base, contest_abvs[isel_contest]); 
        strcpy(contest_file_name, contest_file_base);
        strcat(contest_file_name, ".");
        strcat(contest_file_name, year_char);
        if(!inquire(contest_file_name))
         select_contest_box (widget, REDRAW);
        break;
       default :
        isel_contest = -(isel_contest+NUMBER_OF_CONTESTS);
        select_contest_box (widget, REDRAW);
      }
     strcpy(ini_file_name, contest_file_base);
     strcat(ini_file_name, ".INI");
     scini = inquire(ini_file_name);
     break;
    case 2 :
     if(isel_contest<0)
      isel_contest = -(isel_contest+NUMBER_OF_CONTESTS);
     lcmd = strlen(argv_hold[1]);
     idot = strcspn(argv_hold[1], ".");
     if(lcmd<13 && idot<9 && idot && idot>lcmd-5 && idot != lcmd-1)
      {
       strcpy(contest_file_name, argv_hold[1]);
       for (i=0; i<lcmd; i++)
        contest_file_name[i] = toupper(contest_file_name[i]);
       if(idot==lcmd)
        strcpy(contest_file_base, contest_file_name);
       else
        {
         strncpy(contest_file_base, contest_file_name, idot);
         contest_file_base[idot] = '\0';
        }
       strcpy(ini_file_name, contest_file_base);
       strcat(ini_file_name, ".INI");
       printf("ini_file_name = %s\n", ini_file_name);
       scini = inquire(ini_file_name);
       if(!scini)
        {
         if(inquire(contest_file_name))
          {
           strcpy(error_msg, "INI file for ");
           strcat(error_msg, contest_file_name);
           strcat(error_msg, " was not found!");
           fatal_error_box (widget, error_msg);
          }
         else
          {
           strcpy(cfb_hold, contest_file_base);
           strcpy(cfn_hold, contest_file_name);
           select_contest_box (widget, REDRAW);
           strcpy(contest_file_base, cfb_hold);
           strcpy(contest_file_name, cfn_hold);
          }
        }
//       else
//        printf("INI file exists!\n");
       break;
      }
    default :
     fatal_error_box (widget, "Error in command line!");
   }
  if(isel_contest<0) return;
  read_ini_file ();
  if(scini)
   {
    isel_contest = 0;
    while(strcmp(init_screen_choices[13], contest_abvs[isel_contest])
     && isel_contest<NUMBER_OF_CONTESTS-1)
      isel_contest++;
    while(strcmp(init_screen_choices[14], bandw_bracket[iband])
     && iband<5)
      iband++;
    if(!strcmp(init_screen_choices[15], mode_bracket[CW]))
     mode = CW;
    else
     mode = PH;
   }
  else
   {
    strcpy(init_screen_choices[10], "");
    strcpy(init_screen_choices[13], contest_abvs[isel_contest]);
    switch (isel_contest)
     {
      case DX :
       iband = 2;
       strcpy(set_screen_choices[0], fnn_char[mode]);
       strcat(set_screen_choices[0], init_screen_choices[6]);
       break;
      case WPX :
       iband = 2;
       strcpy(set_screen_choices[0], fnn_char[mode]);
       break;
      case FD :
       iband = 2;
       break;
      case NYQP :
       iband = 3;
       strcpy(set_screen_choices[0], fnn_char[mode]);
       if(strcmp(init_screen_choices[6], "NY"))
        strcat(set_screen_choices[0], init_screen_choices[6]);
       break;
      case WW :
       iband = 2;
       if(init_screen_choices[9][strlen(init_screen_choices[9])-1]=='W')
        {
         iusz = ((atoi(init_screen_choices[9])+10)/30)-2;
         if(iusz>-1 && iusz<3)
          {
           strcpy(set_screen_choices[0], fnn_char[mode]);
           strcat(set_screen_choices[0], us_zones[iusz]);
          }
        }
       break;
      case SS :
       iband = 2;
       break;
      case A160 :
       iband = 0;
       mode = CW;
       break;
      case TEN :
       iband = 5;
       strcpy(set_screen_choices[0], fnn_char[mode]);
       strcat(set_screen_choices[0], init_screen_choices[6]);
       break;
      case WAA :
       iband = 2;
       break;
     }
    strcpy(init_screen_choices[14], bandw_bracket[iband]);
    strcpy(init_screen_choices[15], mode_bracket[mode]);
   }
  last_col = widget->allocation.width/wcol;
  if(widget->allocation.width%wcol)
   last_col++;
  if(last_col>80)
   black_bar (widget, 1, 81, 26, last_col);
  sccfn = inquire(contest_file_name);
  if(!sccfn)
   {
    idisplay_type = IDT_INITIAL;
    update_display (widget, REDRAW);
   }
  else
   {
    utc_offset = atoi(init_screen_choices[10]);
    if(strcmp(init_screen_choices[16], port_bracket[0]))
     set_keyer_port (OPEN_SP);
    if(strcmp(init_screen_choices[18], port_bracket[0]))
     set_radio_port (OPEN_SP);
   }
  get_kb_time ();
  iband = 0;
  while(strcmp(init_screen_choices[14], bandw_bracket[iband]) && iband<5)
   iband++;
  if(!strcmp(init_screen_choices[15], mode_bracket[CW]))
   mode = CW;
  else
   mode = PH;
  if(!sccfn && strcmp(init_screen_choices[16], port_bracket[0]))
   {
    idisplay_type = IDT_KEYER;
    update_display (widget, REDRAW);
   }
  else
   shift_fkeys = (strcmp(set_screen_choices[11], yn_bracket[1]));
  for(i=0; i<6; i++)
   {
    int_to_char (qso_ptv[isel_contest][i], qptv);
    strcpy(score_mb.values_char[i], qptv);
   }
  for(i=0; i<6500; i++)
   {
    int_to_char (i, qnum);
    left_blank_pad (qnum, sizeof(qnum));
    strcpy(cr_array[i].qso, qnum);
   }
  fill_iend_array ();
  for(i=0; i<NUMBER_OF_FIELDS; i++)
   cbv.col1[i] = cbv_fc[isel_contest][i];
  switch (isel_contest)
   {
    case DX :
     cbv.field_length[FT_RC] = 7;
     cbv.field_length[FT_SENT] = 5-mode;
     cbv.field_length[FT_RN] = 7-mode;
     set_length[0] = 5-mode;
     break;
    case WPX :
     cbv.field_length[FT_RC] = 7;
     cbv.field_length[FT_SENT] = 3-mode;
     cbv.field_length[FT_RN] = 7-mode;
     set_length[0] = 3-mode;
     break;
    case FD :
     cbv.field_length[FT_RC] = 3;
     cbv.field_length[FT_SENT] = 5;
     cbv.field_length[FT_RN] = 3;
     cbv.vf_w[2] = FT_RC;
     log_lines[IDT_INFO6] = 9;
     set_length[0] = 5;
     break;
    case NYQP :
     cbv.field_length[FT_RC] = 3;
     cbv.field_length[FT_SENT] = 6;
     cbv.field_length[FT_RN] = 6;
     log_lines[IDT_INFO6] = 9;
     set_length[0] = 6;
     break;
    case WW :
     cbv.field_length[FT_RC] = 7;
     cbv.field_length[FT_SENT] = 5-mode;
     cbv.field_length[FT_RN] = 5-mode;
     set_length[0] = 5-mode;
     break;
    case SS :
     cbv.field_length[FT_RC] = 3;
     cbv.field_length[FT_SENT] = 6;
     cbv.field_length[FT_RN] = 5;
     cbv.vf_w[2] = FT_TX;
     cbv.vf_w[3] = FT_PX;
     cbv.vf_w[4] = FT_RC;
     set_length[0] = 6;
     break;
    case A160 :
     log_lines[IDT_INFO6] = 14;
     cbv.field_length[FT_RC] = 7;
     set_length[0] = 6;
     break;
    case TEN :
     log_lines[IDT_INFO6] = 14;
     cbv.field_length[FT_RC] = 7;
     set_length[0] = 5;
     break;
    case WAA :
     cbv.field_length[FT_RC] = 7;
     cbv.field_length[FT_SENT] = 6;
     cbv.field_length[FT_RN] = 7;
     cbv.vf_w[2] = FT_RC;
     log_lines[IDT_INFO6] = 9;
     set_length[0] = 6;
     break;
    default :
     cbv.col1[0] = 2;
     cbv.col1[3] = 36;
     cbv.col1[9] = 30;
   }
  cbv.cursor_row = cbv.row1;
  cbv.cursor_col = cbv.col1[cbv.item];
  for(i=0; i<NUMBER_OF_FIELDS; i++)
   cbv.item_order[i] = i;
  for(i=0; i<NUMBER_OF_FIELDS-3; i++)
   {
    for(j=i+1; j<NUMBER_OF_FIELDS-3; j++)
     if(cbv.col1[cbv.item_order[j]]<cbv.col1[cbv.item_order[i]])
      {
       ih = cbv.item_order[i];
       cbv.item_order[i] = cbv.item_order[j];
       cbv.item_order[j] = ih;
      }
   }
  read_locus_file ();
  if(!sccfn)
   {
    strcpy(set_screen_choices[9], off_time[isel_contest]);
    idisplay_type = IDT_SUMMARY;
    update_display (widget, REDRAW);
    idisplay_type = IDT_SET;
    update_display (widget, REDRAW);
   }
  read_country_file ();
  if(isel_contest==WPX || isel_contest==WW)
   get_your_country ();
  read_call_library_file ();
  read_help_file ();
  if(sccfn)
   read_contest_file ();
  if(!icall_count)
   strcpy(cr_array[1].on_off, on_off_char[1]);
  cr_array[icall_count+1].iband = iband;
  cr_array[icall_count+1].mode = mode;
  strcpy(cr_array[icall_count+1].sent, set_screen_choices[0]);
  switch (isel_contest)
   {
    case FD :
    case SS :
    case WAA :
     strcpy(cr_array[icall_count+1].rec_num, "");
     break;
    default :
     strcpy(cr_array[icall_count+1].rec_num, fnn_char[mode]);
   }
  idisplay_type = IDT_SUMMARY;
  update_display (widget, REDRAW);
  if(strcmp(cr_array[icall_count+1].on_off, on_off_char[2]))
   message_box (widget, "Kick Butt!", !REDRAW, CLEAR);
  check_for_conflict (widget);
  isys_server = get_running_pid ("kb_server");
  if(!strcmp(init_screen_choices[0], "WA1KKM") ||
     !strcmp(init_screen_choices[0], "N2LBR"))
   pbm.spot_filter = TRUE;
  read_radio_file ();
  if(!icall_count)
   {
    change_radio_mode (VFOA);
    change_radio_frequency (radio_default_freq[mode][iband], VFOA);
   }
  rcv.ready = TRUE;
}

static void set_fixed_font(GtkWidget *widget)
{
  PangoFontDescription *pfd;
  PangoContext *pc;
  PangoLanguage *pl;
  PangoFontMetrics *pfm;
  gint nff, font_size, ipfmgacw, ipfmgadw, ipfmgd, ipfmga;
  gchar font_desc[19];
  gint nfound=FALSE;
  const gchar *font_fam[3] = {"liberation mono", "luxi mono",
                              "monospace bold"};

  pc = gtk_widget_get_pango_context (widget);
  pl = pango_context_get_language (pc);
  pfd = pango_font_description_from_string ("misc fixed 16");
  pfm = pango_context_get_metrics (pc, pfd, pl);
  ipfmgacw = pango_font_metrics_get_approximate_char_width (pfm);
  ipfmgadw = pango_font_metrics_get_approximate_digit_width (pfm);
  if(ipfmgacw/PANGO_SCALE==wcol && ipfmgadw/PANGO_SCALE==wcol)
   {
    cursor_offset = 4;
    pango_layout_set_font_description (layout, pfd);
    pango_font_metrics_unref (pfm);
    pango_font_description_free (pfd);
   }
  else
   {
    for(nff=0; nff<3; nff++)
     {
      for(font_size=10; font_size<=48; font_size++)
       {
        pango_font_metrics_unref (pfm);
        pango_font_description_free (pfd);
        snprintf(font_desc, sizeof(font_desc), "%s %d",
                 font_fam[nff], font_size);
        pfd = pango_font_description_from_string (font_desc);
        pfm = pango_context_get_metrics (pc, pfd, pl);
        ipfmgacw = pango_font_metrics_get_approximate_char_width (pfm);
        ipfmgadw = pango_font_metrics_get_approximate_digit_width (pfm);
        if(ipfmgacw/PANGO_SCALE==wcol && ipfmgadw/PANGO_SCALE==wcol)
         {
          nfound = TRUE;
          ipfmgd = pango_font_metrics_get_descent (pfm);
          ipfmga = pango_font_metrics_get_ascent (pfm);
          cursor_offset = ipfmgd/PANGO_SCALE;
          if(((ipfmga-ipfmgd)/PANGO_SCALE<12) || (nff==2 && font_size>21))
           cursor_offset -= 2;
          pango_layout_set_font_description (layout, pfd);
          break;
         }
       }
      if(nfound) break;
     }
    pango_font_metrics_unref (pfm);
    pango_font_description_free (pfd);
    if(nfound)
     printf("Found a good font [%s]\n", font_desc);
    else
     {
      printf("Cannot find a suitable font for your system!\n");
      printf("Try a different screen resolution.\n");
      gtk_widget_destroy (main_window);
      exit(1);
     }
   }
}

static void select_contest_box(GtkWidget *widget, gint idraw)
{
  GdkRectangle update_rect;
  gint i, sci_color, udac, istrt;
  gint row1=16, col1=22, row2=24, col2=57;
  const gchar *title = "Select Contest";
  const gchar *subtitle = "Select:   Run:Enter Exit:Esc";

  update_rect.x = col1*wcol;
  update_rect.y = row1*hrow;
  update_rect.width = (col2-col1-1)*wcol;
  update_rect.height = (row2-row1-1)*hrow;
  if(idraw)
   {
    g_signal_handler_block (main_window, main_key_handler_id);
    scb_key_handler_id = g_signal_connect (G_OBJECT (main_window),
                         "key_press_event",
                         G_CALLBACK (scb_key_handler), widget);
    draw_box (row1, col1, row2, col2, 2, LT_GRAY, BLACK,
              title, WHITE, subtitle, LT_GRAY, SAVEPIX);
    udac = (col2-col1-strlen(subtitle))/2+col1+kb_index (subtitle, ":")+1;
    draw_updn_arrows (row2, udac, LT_GRAY);
   }
  draw_rectangle (&update_rect, ORANGE, SAVEPIX);
  istrt = isel_contest<7 ? 0 : isel_contest-6;
  for(i=istrt; i<istrt+7; i++)
   {
    if(i==isel_contest)
     sci_color = YELLOW;
    else
     sci_color = BLACK;
    write_line (row1+i-istrt, col1+1, select_contest_items[i], sci_color,
                SAVEPIX);
   }
  gtk_widget_queue_draw_area (widget,
                              update_rect.x-idraw*wcol,
                              update_rect.y-idraw*hrow,
                              update_rect.width+idraw*2*wcol,
                              update_rect.height+idraw*2*hrow);
  if(idraw) gtk_main ();
}

static gint scb_key_handler(GtkWidget *widget, GdkEventKey *event,
                            gpointer da)
{
  switch (event->keyval)
   {
    case 65362 : /*up_arrow*/
     if(isel_contest>0)
      {
       isel_contest--;       
       select_contest_box (da, !REDRAW);
      }
     else
      play_bells (BELL_SOUND);
     break;
    case 65364 : /*down_arrow*/
     if(isel_contest<NUMBER_OF_CONTESTS-1)
      {
       isel_contest++;
       select_contest_box (da, !REDRAW);
      }
     else
      play_bells (BELL_SOUND);
     break;
    case 65293 : /*enter*/
     if(argc_hold==1)
      {
       switch (isel_contest)
        {
         case DX :
         case WPX :
         case WAE :
         case WW :
         case SS :
          strcpy(contest_file_base, contest_abvs[isel_contest]); 
          strcat(contest_file_base, mode_char[mode]);
          strcpy(contest_file_name, contest_file_base);
          strcat(contest_file_name, ".");
          strcat(contest_file_name, year_char);
          break;
         case FD :
          strcpy(contest_file_base, contest_abvs[isel_contest]);

#ifdef KBVHF
          strcat(contest_file_base, "VHF");
#endif

          strcpy(contest_file_name, contest_file_base);
          strcat(contest_file_name, ".");
          strcat(contest_file_name, year_char);
          break;
         case NYQP :
         case A160 :
         case TEN :
         case WAA :
          strcpy(contest_file_base, contest_abvs[isel_contest]); 
          strcpy(contest_file_name, contest_file_base);
          strcat(contest_file_name, ".");
          strcat(contest_file_name, year_char);
          break;
        }
      }
     g_signal_handler_disconnect (main_window, scb_key_handler_id);
     g_signal_handler_unblock (main_window, main_key_handler_id);
     gtk_main_quit ();
     switch (isel_contest)
      {
       case WAE :
        printf("%s is not supported yet\n",
               select_contest_items[isel_contest]);
        isel_contest = -1;
        g_timeout_add (GTK_TIME_DELAY, scbkh_delay, widget);
        break;
      }
     break;
    case 65307 : /*escape*/
     isel_contest = -1;
     gtk_main_quit ();
     g_timeout_add (GTK_TIME_DELAY, scbkh_delay, widget);
     break;
    default :
     play_bells (BAD_KEY_SOUND);
   }
  return TRUE;
}

static gint scbkh_delay(gpointer widget)
{
  gtk_widget_destroy (widget);
  return FALSE;
}

static void initial_screen(GtkWidget *widget, gint idraw)
{
  GdkRectangle update_rect, update_bar;
  static ISVars isd;
  gchar title[61];
  gint i, row, col, init_color, i11, lsi, udac;
  gint row1=1, col1=1, row2=25, col2=80;
  gint item=9, shift_col[2] = {0, 45};
  const gchar *subtitle =
               "Prev./Next Item:      <Select>:F1,F2    Resume:F10";

  lsi = strlen(init_screen_items[0]);
  update_rect.x = col1*wcol;
  update_rect.y = row1*hrow;
  update_rect.width = (col2-col1-1)*wcol;
  update_rect.height = (row2-row1-1)*hrow;
  if(idraw)
   {
    g_signal_handler_block (main_window, main_key_handler_id);
    g_signal_handler_block (widget, main_mouse_handler_id);
    repeat_message = FALSE;
    for(i=0; i<MAX_INIT_ITEMS; i++)
     {
      if(i==4) continue;
      if(!strlen(init_screen_choices[i]))
       {
        item = i;
        break;
       }
     }
    isd.row1 = row1;
    isd.col1 = col1;
    isd.shift_col[0] = shift_col[0];
    isd.shift_col[1] = shift_col[1];
    isd.cursor_row = 2*(item%11)+row1+2;
    isd.cursor_col = col1+shift_col[item/11]+lsi+3
     +strlen(init_screen_choices[item]);
    isd.item = item;
    isd.done = FALSE;
    init_key_handler_id = g_signal_connect (G_OBJECT (main_window),
                          "key_press_event",
                          G_CALLBACK (init_key_handler), &isd);
    init_mouse_handler_id = g_signal_connect (G_OBJECT (widget),
                            "button_press_event",
		            G_CALLBACK (init_mouse_handler), &isd);
    strcpy(title, full_year);
    strcat(title, " ");
    strcat(title, select_contest_items[isel_contest]);
    switch (isel_contest)
     {
      case DX :
      case WPX :
      case WW :
      case SS :
       strcat(title, mode_title[mode]);
       break;
     }
    draw_box (row1, col1, row2, col2, 2, LT_GRAY, BLUE,
              title, WHITE, subtitle, LT_GRAY, SAVEPIX);
    udac = (col2-col1-strlen(subtitle))/2+col1+kb_index (subtitle, ":")+1;
    draw_updn_arrows (row2, udac, LT_GRAY);
    draw_rectangle (&update_rect, BLUE, SAVEPIX);
    black_bar (widget, 26, 1, 26, 80);
    g_timeout_add (250, init_cursor, &isd);
   }
  col = col1+1;
  for(i=0; i<MAX_INIT_ITEMS; i++)
   {
    i11 = i/11;
    row = 2*(i%11)+row1+1;
    if(i==isd.item)
     init_color = YELLOW;
    else
     init_color = LT_GRAY;
    write_line (row, col+shift_col[i11], init_screen_items[i],
     init_color, SAVEPIX);
   }
  col = col1+lsi+2;
  for(i=0; i<MAX_INIT_ITEMS; i++)
   {
    i11 = i/11;
    row = 2*(i%11)+row1+1;
    update_bar.x = (col+shift_col[i11])*wcol;
    update_bar.y = row*hrow;
    update_bar.width = init_length[i]*wcol;
    update_bar.height = hrow;
    draw_rectangle (&update_bar, CYAN, SAVEPIX);
    write_line (row, col+shift_col[i11], init_screen_choices[i],
     WHITE, SAVEPIX);
   }
  gtk_widget_queue_draw_area (widget,
                              update_rect.x-idraw*wcol,
                              update_rect.y-idraw*hrow,
                              update_rect.width+idraw*2*wcol,
                              update_rect.height+idraw*2*hrow);
  if(idraw) gtk_main ();
}

static gint init_key_handler(GtkWidget *widget, GdkEventKey *event,
                             gpointer data)
{
  ISVars *isd;
  ESVars esd;
  guint ikey;
  gint ii, beep, lsi;
  gchar ssc_hold[9], msg[51];
  
  isd = (ISVars*)data;
  ikey = event->keyval;
  lsi = strlen(init_screen_items[0]);
  esd.ikey = ikey;
  esd.cursor_row = isd->cursor_row;
  esd.cursor_colp = &isd->cursor_col;
  esd.field_col = isd->col1+isd->shift_col[isd->item/11]+lsi+3;
  esd.field_length = init_length[isd->item];
  esd.field_linep = init_screen_choices[isd->item];
  switch (ikey)
   {
    case 65505 : /*left shift*/
    case 65506 : /*right shift*/
    case 65509 : /*caps lock*/
    case 65407 : /*num lock*/
     break;
    case 65361 : /*left_arrow*/
    case 65363 : /*right_arrow*/
    case 65360 : /*home*/
    case 65367 : /*end*/
    case 65535 : /*delete*/
    case 65288 : /*backspace*/
    case 65307 : /*escape*/
     if(isd->item<11)
      edit_line (&esd);
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65379 : /*insert*/
     if(isd->item<11)
      {
       insert_mode = !insert_mode;
       edit_line (&esd);
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65470 : /*F1*/
     switch (isd->item)
      {
       case 11 :
        ii = 0;
        while(strcmp(init_screen_choices[isd->item],
         category_bracket[ii]) && ii<7)
          ii++;
        ii++;
        if(ii==1 && (isel_contest==NYQP || isel_contest==A160 ||
         isel_contest==TEN)) ii++;
        if(ii==2 && isel_contest!=WPX) ii++;
        if(ii==4 && isel_contest!=DX && isel_contest!=WW) ii++;
        if(ii==5 && isel_contest!=DX && isel_contest!=WW) ii++;
        if(ii==6 && (isel_contest==SS || isel_contest==A160 ||
         isel_contest==TEN)) ii++;
        if(ii==7 && isel_contest!=SS && isel_contest!=NYQP) ii++;
        if(ii==8) ii=0;
        strcpy(init_screen_choices[isd->item], category_bracket[ii]);
        initial_screen (main_drawing_area, !REDRAW);
        break;
       case 12 :
        ii = 0;
        while(strcmp(init_screen_choices[isd->item],
         power_bracket[ii]) && ii<2)
          ii++;
        ii++;
        if(ii==3) ii=0;
        strcpy(init_screen_choices[isd->item], power_bracket[ii]);
        initial_screen (main_drawing_area, !REDRAW);
        break;
       case 14 :
        ii = 0;
        while(strcmp(init_screen_choices[isd->item],
         bandw_bracket[ii]) && ii<5)
          ii++;
        ii++;
        if(ii==6) ii=0;
        strcpy(init_screen_choices[isd->item], bandw_bracket[ii]);
        initial_screen (main_drawing_area, !REDRAW);
        break;
       case 15 :
        if(strcmp(init_screen_choices[isd->item], mode_bracket[CW]))
         ii = CW;
        else
         ii = PH;
        strcpy(init_screen_choices[isd->item], mode_bracket[ii]);
        initial_screen (main_drawing_area, !REDRAW);
        break;
       case 16 :
       case 18 :
        ii = 0;
        while(strcmp(init_screen_choices[isd->item],
         port_bracket[ii]) && ii<NUMBER_OF_PORTS-2)
          ii++;
        ii++;
        if(ii==NUMBER_OF_PORTS-1) ii=0;
        strcpy(init_screen_choices[isd->item], port_bracket[ii]);
        initial_screen (main_drawing_area, !REDRAW);
        break;
       case 17 :
        ii = 0;
        while(strcmp(init_screen_choices[isd->item],
         port_bracket[ii]) && ii<NUMBER_OF_PORTS-1)
          ii++;
        ii++;
        if(ii==NUMBER_OF_PORTS) ii=0;
        strcpy(init_screen_choices[isd->item], port_bracket[ii]);
        initial_screen (main_drawing_area, !REDRAW);
        break;
       case 19 :
        ii = 0;
        while(strcmp(init_screen_choices[isd->item],
         radio_bracket[ii]) && ii<NUMBER_OF_RMS)
          ii++;
        ii++;
        if(ii==NUMBER_OF_RMS+1) ii=0;
        strcpy(init_screen_choices[isd->item], radio_bracket[ii]);
        initial_screen (main_drawing_area, !REDRAW);
        break;
       default :
        play_bells (BAD_KEY_SOUND);
      }
     break;
    case 65471 : /*F2*/
     switch (isd->item)
      {
       case 11 :
        ii = 0;
        while(strcmp(init_screen_choices[isd->item],
         category_bracket[ii]) && ii<7)
          ii++;
        ii--;
        if(ii==-1) ii=7;
        if(ii==7 && isel_contest!=SS && isel_contest!=NYQP) ii--;
        if(ii==6 && (isel_contest==SS || isel_contest==A160 ||
         isel_contest==TEN)) ii--;
        if(ii==5 && isel_contest!=DX && isel_contest!=WW) ii--;
        if(ii==4 && isel_contest!=DX && isel_contest!=WW) ii--;
        if(ii==2 && isel_contest!=WPX) ii--;
        if(ii==1 && (isel_contest==NYQP || isel_contest==A160 ||
         isel_contest==TEN)) ii--;
        strcpy(init_screen_choices[isd->item], category_bracket[ii]);
        initial_screen (main_drawing_area, !REDRAW);
        break;
       case 12 :
        ii = 0;
        while(strcmp(init_screen_choices[isd->item],
         power_bracket[ii]) && ii<2)
          ii++;
        ii--;
        if(ii==-1) ii=2;
        strcpy(init_screen_choices[isd->item], power_bracket[ii]);
        initial_screen (main_drawing_area, !REDRAW);
        break;
       case 14 :
        ii = 0;
        while(strcmp(init_screen_choices[isd->item],
         bandw_bracket[ii]) && ii<5)
          ii++;
        ii--;
        if(ii==-1) ii=5;
        strcpy(init_screen_choices[isd->item], bandw_bracket[ii]);
        initial_screen (main_drawing_area, !REDRAW);
        break;
       case 15 :
        if(strcmp(init_screen_choices[isd->item], mode_bracket[CW]))
         ii = CW;
        else
         ii = PH;
        strcpy(init_screen_choices[isd->item], mode_bracket[ii]);
        initial_screen (main_drawing_area, !REDRAW);
        break;
       case 16 :
       case 18 :
        ii = 0;
        while(strcmp(init_screen_choices[isd->item],
         port_bracket[ii]) && ii<NUMBER_OF_PORTS-2)
          ii++;
        ii--;
        if(ii==-1) ii=NUMBER_OF_PORTS-2;
        strcpy(init_screen_choices[isd->item], port_bracket[ii]);
        initial_screen (main_drawing_area, !REDRAW);
        break;
       case 17 :
        ii = 0;
        while(strcmp(init_screen_choices[isd->item],
         port_bracket[ii]) && ii<NUMBER_OF_PORTS-1)
          ii++;
        ii--;
        if(ii==-1) ii=NUMBER_OF_PORTS-1;
        strcpy(init_screen_choices[isd->item], port_bracket[ii]);
        initial_screen (main_drawing_area, !REDRAW);
        break;
       case 19 :
        ii = 0;
        while(strcmp(init_screen_choices[isd->item],
         radio_bracket[ii]) && ii<NUMBER_OF_RMS)
          ii++;
        ii--;
        if(ii==-1) ii=NUMBER_OF_RMS;
        strcpy(init_screen_choices[isd->item], radio_bracket[ii]);
        initial_screen (main_drawing_area, !REDRAW);
        break;
       default :
        play_bells (BAD_KEY_SOUND);
      }
     break;
    case 65362 : /*up_arrow*/
     if(init_error_check (isd))
      {
       if(insert_mode && isd->item<11)
        edit_line (&esd);
       break;
      }
     if(isd->item>0)
      isd->item--;       
     else
      isd->item = MAX_INIT_ITEMS-1;
     if(isd->item==17 && isys_packet>-1) isd->item--;
     if(isd->item==15 &&
       (icall_count || !both_modes[isel_contest]))
      isd->item--;
     if(isd->item==14 &&
       (icall_count || isel_contest==A160 || isel_contest==TEN))
      isd->item--;
     if(isd->item==13) isd->item--;
     if(isd->item==10 && icall_count)
      isd->item--;
     isd->cursor_row = 2*(isd->item%11)+isd->row1+2;
     isd->cursor_col = isd->col1+isd->shift_col[isd->item/11]+lsi+3
      +strlen(init_screen_choices[isd->item]);
     initial_screen (main_drawing_area, !REDRAW);
     if(isd->item<11 && insert_mode)
      {
       esd.cursor_row = isd->cursor_row;
       esd.cursor_colp = &isd->cursor_col;
       esd.field_col = isd->col1+isd->shift_col[isd->item/11]+lsi+3;
       esd.field_length = init_length[isd->item];
       esd.field_linep = init_screen_choices[isd->item];
       edit_line (&esd);
      }
     break;
    case 65364 : /*down_arrow*/
     if(init_error_check (isd))
      {
       if(insert_mode && isd->item<11)
        edit_line (&esd);
       break;
      }
     if(isd->item<MAX_INIT_ITEMS-1)
      isd->item++;
     else
      isd->item = 0;
     if(isd->item==10 && icall_count)
      isd->item++;
     if(isd->item==13) isd->item++;
     if(isd->item==14 &&
       (icall_count || isel_contest==A160 || isel_contest==TEN))
      isd->item++;
     if(isd->item==15 &&
       (icall_count || !both_modes[isel_contest]))
      isd->item++;
     if(isd->item==17 && isys_packet>-1) isd->item++;
     isd->cursor_row = 2*(isd->item%11)+isd->row1+2;
     isd->cursor_col = isd->col1+isd->shift_col[isd->item/11]+lsi+3
      +strlen(init_screen_choices[isd->item]);
     initial_screen (main_drawing_area, !REDRAW);
     if(isd->item<11 && insert_mode)
      {
       esd.cursor_row = isd->cursor_row;
       esd.cursor_colp = &isd->cursor_col;
       esd.field_col = isd->col1+isd->shift_col[isd->item/11]+lsi+3;
       esd.field_length = init_length[isd->item];
       esd.field_linep = init_screen_choices[isd->item];
       edit_line (&esd);
      }
     break;
    case 65482 : /*F10*/
     if(init_error_check (isd))
      {
       if(insert_mode && isd->item<11)
        {
         esd.ikey = 65293; /*enter*/
         edit_line (&esd);
        }
       break;
      }
     if(strcmp(init_screen_choices[16], port_bracket[0]))
      {
       if(!strcmp(init_screen_choices[16], init_screen_choices[17]) ||
          !strcmp(init_screen_choices[16], init_screen_choices[18]))
        {
         strcpy(msg, "Keyer Port Conflict!");
         play_bells (BAD_KEY_SOUND);
         boxed_message (msg, 2000, YELLOW);
         break;
        }
      }
     else if(strcmp(init_screen_choices[17], port_bracket[0]))
      {
       if(!strcmp(init_screen_choices[17], init_screen_choices[18]))
        {
         strcpy(msg, "Packet Port Conflict!");
         play_bells (BAD_KEY_SOUND);
         boxed_message (msg, 2000, YELLOW);
         break;
        }
      }
     else if(strcmp(init_screen_choices[18], port_bracket[0]))
      {
       if(!strcmp(init_screen_choices[19], radio_bracket[0]))
        {
         strcpy(msg, "Select a Radio Model!");
         play_bells (BAD_KEY_SOUND);
         boxed_message (msg, 2000, YELLOW);
         break;
        }
      }
     if(!icall_count)
      {
       iband = 0;
       while(strcmp(init_screen_choices[14],
             bandw_bracket[iband]) && iband<5)
        iband++;
       if(!strcmp(init_screen_choices[15], mode_bracket[CW]))
        mode = CW;
       else
        mode = PH;
       if(isel_contest==SS)
        {
         if(strlen(set_screen_choices[0]))
          set_screen_choices[0][0] = check_precedence ();
        }
       else if(isel_contest==TEN || isel_contest==NYQP)
        {
         if(mode==CW && atoi(set_screen_choices[0])<111)
          {
           strcpy(ssc_hold, set_screen_choices[0]);
           concat(set_screen_choices[0], ssc_hold, 0, 1, COPY);
           strcat(set_screen_choices[0], "9");
           concat(set_screen_choices[0], ssc_hold, 2, 3, !COPY);
          }
         else if(mode==PH && atoi(set_screen_choices[0])>59)
          {
           strcpy(ssc_hold, set_screen_choices[0]);
           concat(set_screen_choices[0], ssc_hold, 0, 1, COPY);
           concat(set_screen_choices[0], ssc_hold, 3, 4, !COPY);
          }
        }
       cr_array[icall_count+1].iband = iband;
       cr_array[icall_count+1].mode = mode;
       strcpy(cr_array[icall_count+1].sent, set_screen_choices[0]);
       if(isel_contest==FD || isel_contest==SS || isel_contest==WAA)
        strcpy(cr_array[icall_count+1].rec_num, "");
       else
        strcpy(cr_array[icall_count+1].rec_num, fnn_char[mode]);
      }
     write_ini_file ();
     utc_offset = atoi(init_screen_choices[10]);
     if(isel_contest==WPX || isel_contest==WW)
      get_your_country ();
     if(fp_keyer)
      set_keyer_port (CLOSE_SP);
     if(fp_radio)
      set_radio_port (CLOSE_SP);
     if(strcmp(init_screen_choices[16], port_bracket[0]))
      set_keyer_port (OPEN_SP);
     if(strcmp(init_screen_choices[18], port_bracket[0]))
      set_radio_port (OPEN_SP);
     read_radio_file ();
     insert_mode = 0;
     isd->done = TRUE;
     break;
    default :
     beep = 1;
     switch (isd->item)
      {
       case 0 :
       case 1 :
        ikey = toupper_ui(ikey);
        if(isupper_ui(ikey) || isdigit_ui(ikey) || ikey=='/')
         beep = 0;
        break;
       case 2 :
        if(isalpha_ui(ikey) || ikey=='.' || ikey=='-' || ikey==' ')
         beep = 0;
        break;
       case 3 :
       case 4 :
        if(isalpha_ui(ikey) || isdigit_ui(ikey) ||
         ikey=='.' || ikey=='-' || ikey==' ')
         beep = 0;
        break;
       case 5 :
        if(isalpha_ui(ikey) || ikey=='.' || ikey=='-' || ikey==' ')
         beep = 0;
        break;
       case 6 :
        ikey = toupper_ui(ikey);
        if(isupper_ui(ikey))
         beep = 0;
        break;
       case 7 :
        ikey = toupper_ui(ikey);
        if(isdigit_ui(ikey) || isalpha_ui(ikey) || ikey=='-' || ikey==' ')
         beep = 0;
        break;
       case 8 :
        ikey = toupper_ui(ikey);
        if(isdigit_ui(ikey) || ikey=='N' || ikey=='S' || ikey=='.')
         beep = 0;
        break;
       case 9 :
        ikey = toupper_ui(ikey);
        if(isdigit_ui(ikey) || ikey=='E' || ikey=='W' || ikey=='.')
         beep = 0;
        break;
       case 10 :
        if(isdigit_ui(ikey) || ikey=='-')
         beep = 0;
        break;
      }
     if(!beep)
      {
       esd.ikey = ikey;
       edit_line (&esd);
      }
     else
      play_bells (BAD_KEY_SOUND);
   }
  return TRUE;
}

static gint init_mouse_handler(GtkWidget *widget, GdkEventButton *event,
                               gpointer data)
{
  int x, y;
  GdkModifierType state;
  ISVars *isd;
  ESVars esd;
  gint mouse_row, mouse_col, mouse_item, min_col, max_col, lsi, mfs;

  if(event->type==GDK_BUTTON_PRESS)
   {
    isd = (ISVars*)data;
    lsi = strlen(init_screen_items[0]);
    if(init_error_check (isd))
     {
      if(insert_mode && isd->item<11)
       {
        esd.ikey = 65293; /*enter*/
        esd.cursor_row = isd->cursor_row;
        esd.cursor_colp = &isd->cursor_col;
        esd.field_col = isd->col1+isd->shift_col[isd->item/11]+lsi+3;
        esd.field_length = init_length[isd->item];
        esd.field_linep = init_screen_choices[isd->item];
        edit_line (&esd);
       }
      return TRUE;
     }
    gdk_window_get_pointer (event->window, &x, &y, &state);
    mouse_col = (x-1)/wcol+1;
    mouse_row = (y-1)/hrow+1;
    if(mouse_row>isd->row1+1 && mouse_row<25)
     {
      mouse_item = (mouse_row-isd->row1-2)/2;
      switch (mouse_item)
       {
        case 2 :
        case 3 :
        case 4 :
        case 5 :
         mfs = 3;
         break;
        default :
         mfs = 0;
       }
      if(mouse_col>isd->col1+isd->shift_col[1]+mfs)
       mouse_item += 11;
      if(mouse_item>MAX_INIT_ITEMS-1)
       return TRUE;
      if(mouse_item==10 && icall_count)
       return TRUE;
      if(mouse_item==13)
       return TRUE;
      if(mouse_item==14 && (isel_contest==A160 || isel_contest==TEN))
       return TRUE;
      if(mouse_item==15 && !both_modes[isel_contest])
       return TRUE;
      if(mouse_item==17 && isys_packet>-1)
       return TRUE;
      min_col = isd->col1+isd->shift_col[mouse_item/11]+lsi+3;
      if(mouse_col<=min_col+init_length[mouse_item]+2)
       {
        isd->item = mouse_item;
        isd->cursor_row = 2*(isd->item%11)+isd->row1+2;
        max_col = min_col+strlen(init_screen_choices[isd->item]);
        if(mouse_col<min_col)
         isd->cursor_col = min_col;
        else if(mouse_col>max_col)
         isd->cursor_col = max_col;
        else
         isd->cursor_col = mouse_col;
        initial_screen (widget, !REDRAW);
        if(isd->item<11 && insert_mode)
         {
          esd.ikey = 65293; /*enter*/
          esd.cursor_row = isd->cursor_row;
          esd.cursor_colp = &isd->cursor_col;
          esd.field_col = min_col;
          esd.field_length = init_length[isd->item];
          esd.field_linep = init_screen_choices[isd->item];
          edit_line (&esd);
         }
       }
     }
   }
  return TRUE;
}

static gint init_cursor(gpointer data)
{
  ISVars *isd;
  gint cursor_fg_color, cursor_bg_color;
  static gint iadd = 0;

  isd = (ISVars*)data;
  if(isd->done)
   {
    g_signal_handler_disconnect (main_window, init_key_handler_id);
    g_signal_handler_disconnect (main_drawing_area,
                                 init_mouse_handler_id);
    g_signal_handler_unblock (main_window, main_key_handler_id);
    g_signal_handler_unblock (main_drawing_area, main_mouse_handler_id);
    gtk_main_quit ();
    return FALSE;
   }
  if(isd->item<11)
   {
    iadd++;
    iadd %= 4;
    if(insert_mode)
     {
      cursor_fg_color = HI_CYAN;
      cursor_bg_color = LT_GRAY;
     }
    else
     {
      cursor_fg_color = WHITE;
      cursor_bg_color = CYAN;
     }
    cursor_control (main_drawing_area, iadd, LINE_CURSOR,
                    isd->cursor_row, isd->cursor_col,
                    cursor_fg_color, cursor_bg_color);
   }
  return TRUE;
}

static gint init_error_check(gpointer data)
{
  ISVars *isd;
  ESVars esd;
  gint i, lsc, lsi;
  gchar msg[65];
  gint init_error=FALSE;

  isd = (ISVars*)data;
  switch (isd->item)
   {
    case 0 :
     if(strlen(init_screen_choices[isd->item])<3)
      {
       strcpy(msg, "Invalid Entry:");
       strcat(msg, init_screen_choices[isd->item]);
       init_error = TRUE;
      }
     break;
    case 1 :
     if(strlen(init_screen_choices[isd->item])<3)
      {
       strcpy(msg, "Invalid Entry:");
       strcat(msg, init_screen_choices[isd->item]);
       init_error = TRUE;
      }
     break;
    case 2 :
    case 3 :
    case 5 :
     if(!strlen(init_screen_choices[isd->item]))
      {
       strcpy(msg, "Invalid Entry:");
       strcat(msg, init_screen_choices[isd->item]);
       init_error = TRUE;
      }
     break;
    case 6 :
     if(strlen(init_screen_choices[isd->item])<2)
      {
       strcpy(msg, "Invalid Entry:");
       strcat(msg, init_screen_choices[isd->item]);
       init_error = TRUE;
      }
     break;
    case 7 :
     lsi = strlen(init_screen_items[0]);
     right_blank_unpad (init_screen_choices[isd->item]);
     esd.ikey = 65293; /*enter*/
     esd.cursor_row = isd->cursor_row;
     esd.cursor_colp = &isd->cursor_col;
     esd.field_col = isd->col1+isd->shift_col[isd->item/11]+lsi+3;
     esd.field_length = init_length[isd->item];
     esd.field_linep = init_screen_choices[isd->item];
     *esd.cursor_colp = esd.field_col+strlen(esd.field_linep);
     edit_line (&esd);
     lsc = strlen(init_screen_choices[isd->item]);
     switch (lsc)
      {
       case 5:
        for(i=0; i<lsc; i++)
         if(!isdigit(init_screen_choices[isd->item][i]))
          {
           init_error = TRUE;
           break;
          }
        break;
       case 7:
        for(i=0; i<lsc; i++)
         {
          switch (i)
           {
            case 0:
            case 2:
            case 5:
             if(!isalpha(init_screen_choices[isd->item][i]))
              init_error = TRUE;
             break;
            case 1:
            case 4:
            case 6:
             if(!isdigit(init_screen_choices[isd->item][i]))
              init_error = TRUE;
             break;
            case 3:
             if(init_screen_choices[isd->item][i]!=' ')
              init_error = TRUE;
             break;
           }
          if(init_error) break;
         }
        break;
       case 10:
        for(i=0; i<5; i++)
         if(!isdigit(init_screen_choices[isd->item][i]))
          {
           init_error = TRUE;
           break;
          }
        if(init_error) break;
        if(init_screen_choices[isd->item][5]!='-')
         {
          init_error = TRUE;
          break;
         }
        for(i=6; i<lsc; i++)
         if(!isdigit(init_screen_choices[isd->item][i]))
          {
           init_error = TRUE;
           break;
          }
        break;
       default:
        init_error = TRUE;
      }
     if(init_error)
      {
       strcpy(msg, "Invalid Entry:");
       strcat(msg, init_screen_choices[isd->item]);
      }
     break;
    case 8 :
     lsc = strlen(init_screen_choices[isd->item]);
     if(lsc<2 || atof(init_screen_choices[isd->item])>90.0 ||
        (init_screen_choices[isd->item][lsc-1]!='N' &&
        init_screen_choices[isd->item][lsc-1]!='S'))
      {
       strcpy(msg, "Invalid Entry:");
       strcat(msg, init_screen_choices[isd->item]);
       init_error = TRUE;
      }
     break;
    case 9 :
     lsc = strlen(init_screen_choices[isd->item]);
     if(lsc<2 || atof(init_screen_choices[isd->item])>180.0 ||
        (init_screen_choices[isd->item][lsc-1]!='E' &&
        init_screen_choices[isd->item][lsc-1]!='W'))
      {
       strcpy(msg, "Invalid Entry:");
       strcat(msg, init_screen_choices[isd->item]);
       init_error = TRUE;
      }
     break;
    case 10 :
     lsc = strlen(init_screen_choices[isd->item]);
     if(!lsc || (lsc==1 && init_screen_choices[isd->item][0]=='-') ||
      atoi(init_screen_choices[isd->item])>12 ||
      atoi(init_screen_choices[isd->item])<-12 ||
      init_screen_choices[isd->item][1]=='-' ||
      init_screen_choices[isd->item][2]=='-')
      {
       strcpy(msg, "Invalid Entry:");
       strcat(msg, init_screen_choices[isd->item]);
       init_error = TRUE;
      }
     break;
   }
  if(init_error)
   {
    play_bells (BAD_KEY_SOUND);
    boxed_message (msg, 2000, YELLOW);
   }
  return init_error;
}

static void keyer_screen(GtkWidget *widget, gint idraw)
{
  GdkRectangle update_rect, update_bar;
  static KSVars ksd;
  gint i, row, col, keyer_color, i11, lsi, udac;
  gint row1=1, col1=1, row2=25, col2=80;
  gint item=0, shift_col[2] = {0, 45};
  const gchar *title = "Set Keyer Functions and Messages";
  const gchar *subtitle =
               "Prev./Next Item:      <Select>:F1,F2    Resume:F10";

  lsi = strlen(keyer_screen_items[0]);
  update_rect.x = col1*wcol;
  update_rect.y = row1*hrow;
  update_rect.width = (col2-col1-1)*wcol;
  update_rect.height = (row2-row1-1)*hrow;
  if(idraw)
   {
    g_signal_handler_block (main_window, main_key_handler_id);
    g_signal_handler_block (widget, main_mouse_handler_id);
    repeat_message = FALSE;
    item = 0;
    ksd.row1 = row1;
    ksd.col1 = col1;
    ksd.shift_col[0] = shift_col[0];
    ksd.shift_col[1] = shift_col[1];
    ksd.cursor_row = 2*(item%11)+row1+2;
    ksd.cursor_col = col1+shift_col[item/11]+lsi+3
     +strlen(keyer_screen_choices[item]);
    ksd.item = item;
    ksd.done = FALSE;
    keyer_key_handler_id = g_signal_connect (G_OBJECT (main_window),
                           "key_press_event",
                           G_CALLBACK (keyer_key_handler), &ksd);
    keyer_mouse_handler_id = g_signal_connect (G_OBJECT (widget),
                             "button_press_event",
		             G_CALLBACK (keyer_mouse_handler), &ksd);
    draw_box (row1, col1, row2, col2, 2, LT_GRAY, BLUE,
              title, WHITE, subtitle, LT_GRAY, SAVEPIX);
    udac = (col2-col1-strlen(subtitle))/2+col1+kb_index (subtitle, ":")+1;
    draw_updn_arrows (row2, udac, LT_GRAY);
    draw_rectangle (&update_rect, BLUE, SAVEPIX);
    black_bar (widget, 26, 1, 26, 80);
    write_line (row1+1, col1+33, "Special Symbol for:  Received Call: ^",
     LT_GRAY, SAVEPIX);
    write_line (row1+3, col1+54, "Serial Number: #",
     LT_GRAY, SAVEPIX);
    g_timeout_add (250, keyer_cursor, &ksd);
   }
  col = col1+1;
  for(i=0; i<MAX_KEYER_ITEMS; i++)
   {
    i11 = i/11;
    row = 2*(i%11)+row1+1;
    if(i==ksd.item)
     keyer_color = YELLOW;
    else
     keyer_color = LT_GRAY;
    write_line (row, col+shift_col[i11], keyer_screen_items[i],
     keyer_color, SAVEPIX);
   }
  col = col1+lsi+2;
  for(i=0; i<MAX_KEYER_ITEMS; i++)
   {
    i11 = i/11;
    row = 2*(i%11)+row1+1;
    update_bar.x = (col+shift_col[i11])*wcol;
    update_bar.y = row*hrow;
    update_bar.width = keyer_length[i]*wcol;
    update_bar.height = hrow;
    draw_rectangle (&update_bar, CYAN, SAVEPIX);
    write_line (row, col+shift_col[i11], keyer_screen_choices[i],
     WHITE, SAVEPIX);
   }
  gtk_widget_queue_draw_area (widget,
                              update_rect.x-idraw*wcol,
                              update_rect.y-idraw*hrow,
                              update_rect.width+idraw*2*wcol,
                              update_rect.height+idraw*2*hrow);
  if(idraw) gtk_main ();
}

static gint keyer_key_handler(GtkWidget *widget, GdkEventKey *event,
                             gpointer data)
{
  KSVars *ksd;
  ESVars esd;
  guint ikey;
  gint ii, beep, lsi;
  
  ksd = (KSVars*)data;
  ikey = event->keyval;
  lsi = strlen(keyer_screen_items[0]);
  esd.ikey = ikey;
  esd.cursor_row = ksd->cursor_row;
  esd.cursor_colp = &ksd->cursor_col;
  esd.field_col = ksd->col1+ksd->shift_col[ksd->item/11]+lsi+3;
  esd.field_length = keyer_length[ksd->item];
  esd.field_linep = keyer_screen_choices[ksd->item];
  switch (ikey)
   {
    case 65505 : /*left shift*/
    case 65506 : /*right shift*/
    case 65509 : /*caps lock*/
    case 65407 : /*num lock*/
     break;
    case 65361 : /*left_arrow*/
    case 65363 : /*right_arrow*/
    case 65360 : /*home*/
    case 65367 : /*end*/
    case 65535 : /*delete*/
    case 65288 : /*backspace*/
    case 65307 : /*escape*/
     if(ksd->item>1)
      edit_line (&esd);
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65379 : /*insert*/
     if(ksd->item>1)
      {
       insert_mode = !insert_mode;
       edit_line (&esd);
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65362 : /*up_arrow*/
     if(ksd->item>0)
      ksd->item--;
     else
      ksd->item = MAX_KEYER_ITEMS-1;
     if(ksd->item==1)
      ksd->item--;
     ksd->cursor_row = 2*(ksd->item%11)+ksd->row1+2;
     ksd->cursor_col = ksd->col1+ksd->shift_col[ksd->item/11]+lsi+3
      +strlen(keyer_screen_choices[ksd->item]);
     keyer_screen (main_drawing_area, !REDRAW);
     if(ksd->item>1 && insert_mode)
      {
       esd.cursor_row = ksd->cursor_row;
       esd.cursor_colp = &ksd->cursor_col;
       esd.field_col = ksd->col1+ksd->shift_col[ksd->item/11]+lsi+3;
       esd.field_length = keyer_length[ksd->item];
       esd.field_linep = keyer_screen_choices[ksd->item];
       edit_line (&esd);
      }
     break;
    case 65364 : /*down_arrow*/
     if(ksd->item<MAX_KEYER_ITEMS-1)
      ksd->item++;
     else
      ksd->item = 0;
     if(ksd->item==1)
      ksd->item++;
     ksd->cursor_row = 2*(ksd->item%11)+ksd->row1+2;
     ksd->cursor_col = ksd->col1+ksd->shift_col[ksd->item/11]+lsi+3
      +strlen(keyer_screen_choices[ksd->item]);
     keyer_screen (main_drawing_area, !REDRAW);
     if(ksd->item>1 && insert_mode)
      {
       esd.cursor_row = ksd->cursor_row;
       esd.cursor_colp = &ksd->cursor_col;
       esd.field_col = ksd->col1+ksd->shift_col[ksd->item/11]+lsi+3;
       esd.field_length = keyer_length[ksd->item];
       esd.field_linep = keyer_screen_choices[ksd->item];
       edit_line (&esd);
      }
     break;
    case 65470 : /*F1*/
     switch (ksd->item)
      {
       case 0 :
        ii = 0;
        while(strcmp(keyer_screen_choices[ksd->item],
         speed_bracket[ii]) && ii<NUMBER_OF_SPEEDS-1)
          ii++;
        if(ii<NUMBER_OF_SPEEDS-1)
         {
          ii++;
          strcpy(keyer_screen_choices[ksd->item], speed_bracket[ii]);
          keyer_screen (main_drawing_area, !REDRAW);
         }
        else
         play_bells (BELL_SOUND);
        break;
       default :
        play_bells (BAD_KEY_SOUND);
      }
     break;
    case 65471 : /*F2*/
     switch (ksd->item)
      {
       case 0 :
        ii = 0;
        while(strcmp(keyer_screen_choices[ksd->item],
         speed_bracket[ii]) && ii<NUMBER_OF_SPEEDS-1)
          ii++;
        if(ii>0)
         {
          ii--;
          strcpy(keyer_screen_choices[ksd->item], speed_bracket[ii]);
          keyer_screen (main_drawing_area, !REDRAW);
         }
        else
         play_bells (BELL_SOUND);
        break;
       default :
        play_bells (BAD_KEY_SOUND);
      }
     break;
    case 65482 : /*F10*/
     write_ini_file ();
     insert_mode = 0;
     ksd->done = TRUE;
     break;
    default :
     beep = 1;
     if(ksd->item>1)
      {
       ikey = toupper_ui(ikey);
       if(isupper_ui(ikey) || isdigit_ui(ikey) || ikey=='/' ||
        ikey=='?' || ikey==' ' || ikey=='#' || ikey=='^')
        beep = 0;
      }
     if(!beep)
      {
       esd.ikey = ikey;
       edit_line (&esd);
      }
     else
      play_bells (BAD_KEY_SOUND);
   }
  return TRUE;
}

static gint keyer_mouse_handler(GtkWidget *widget, GdkEventButton *event,
                                gpointer data)
{
  int x, y;
  GdkModifierType state;
  KSVars *ksd;
  ESVars esd;
  gint mouse_row, mouse_col, mouse_item, min_col, max_col, lsi;

  if(event->type==GDK_BUTTON_PRESS)
   {
    ksd = (KSVars*)data;
    gdk_window_get_pointer (event->window, &x, &y, &state);
    mouse_col = (x-1)/wcol+1;
    mouse_row = (y-1)/hrow+1;
    if(mouse_row>ksd->row1+1 && mouse_row<25)
     {
      lsi = strlen(keyer_screen_items[0]);
      mouse_item = (mouse_row-ksd->row1-2)/2;
      if(mouse_item==1)
       return TRUE;
      min_col = ksd->col1+ksd->shift_col[mouse_item/11]+lsi+3;
      if(mouse_col<=min_col+keyer_length[mouse_item]+2)
       {
        ksd->item = mouse_item;
        ksd->cursor_row = 2*(ksd->item%11)+ksd->row1+2;
        max_col = min_col+strlen(keyer_screen_choices[ksd->item]);
        if(mouse_col<min_col)
         ksd->cursor_col = min_col;
        else if(mouse_col>max_col)
         ksd->cursor_col = max_col;
        else
         ksd->cursor_col = mouse_col;
        keyer_screen (widget, !REDRAW);
        if(ksd->item>1 && insert_mode)
         {
          esd.ikey = 65293; /*enter*/
          esd.cursor_row = ksd->cursor_row;
          esd.cursor_colp = &ksd->cursor_col;
          esd.field_col = min_col;
          esd.field_length = keyer_length[ksd->item];
          esd.field_linep = keyer_screen_choices[ksd->item];
          edit_line (&esd);
         }
       }
     }
   }
  return TRUE;
}

static gint keyer_cursor(gpointer data)
{
  KSVars *ksd;
  gint cursor_fg_color, cursor_bg_color;
  static gint iadd = 0;

  ksd = (KSVars*)data;
  if(ksd->done)
   {
    g_signal_handler_disconnect (main_window, keyer_key_handler_id);
    g_signal_handler_disconnect (main_drawing_area,
                                 keyer_mouse_handler_id);
    g_signal_handler_unblock (main_window, main_key_handler_id);
    g_signal_handler_unblock (main_drawing_area, main_mouse_handler_id);
    gtk_main_quit ();
    return FALSE;
   }
  if(ksd->item>1)
   {
    iadd++;
    iadd %= 4;
    if(insert_mode)
     {
      cursor_fg_color = HI_CYAN;
      cursor_bg_color = LT_GRAY;
     }
    else
     {
      cursor_fg_color = WHITE;
      cursor_bg_color = CYAN;
     }
    cursor_control (main_drawing_area, iadd, LINE_CURSOR,
                    ksd->cursor_row, ksd->cursor_col,
                    cursor_fg_color, cursor_bg_color);
   }
  return TRUE;
}

static void set_screen(GtkWidget *widget, gint idraw)
{
  GdkRectangle update_rect, update_bar;
  static SSVars ssd;
  gint i, row, col, set_color, lsi, udac;
  gint row1=4, col1=8, row2=20, col2=73;
  gint item=6, num1c_items=7, shift_col[2] = {0, 35};
  const gchar *title = "Set User-Defined Defaults";
  const gchar *subtitle =
               "Prev./Next Item:      <Select>:F1    Resume:F10";

  lsi = strlen(set_screen_items[0]);
  update_rect.x = col1*wcol;
  update_rect.y = row1*hrow;
  update_rect.width = (col2-col1-1)*wcol;
  update_rect.height = (row2-row1-1)*hrow;
  if(idraw)
   {
    g_signal_handler_block (main_window, main_key_handler_id);
    g_signal_handler_block (widget, main_mouse_handler_id);
    repeat_message = FALSE;
    if(isel_contest==NYQP && strlen(set_screen_choices[0])<4)
     item = 0;
    for(i=0; i<MAX_SET_ITEMS; i++)
     {
      if(!strlen(set_screen_choices[i]))
       {
        item = i;
        break;
       }
     }
    ssd.row1 = row1;
    ssd.col1 = col1;
    ssd.num1c_items = num1c_items;
    ssd.shift_col[0] = shift_col[0];
    ssd.shift_col[1] = shift_col[1];
    ssd.cursor_row = 2*(item%num1c_items)+row1+2;
    ssd.cursor_col = col1+shift_col[item/num1c_items]+lsi+3
     +strlen(set_screen_choices[item]);
    ssd.item = item;
    ssd.done = FALSE;
    set_key_handler_id = g_signal_connect (G_OBJECT (main_window),
                         "key_press_event",
                         G_CALLBACK (set_key_handler), &ssd);
    set_mouse_handler_id = g_signal_connect (G_OBJECT (widget),
                           "button_press_event",
		           G_CALLBACK (set_mouse_handler), &ssd);
    draw_box (row1, col1, row2, col2, 2, LT_GRAY, BLUE,
              title, WHITE, subtitle, LT_GRAY, SAVEPIX);
    udac = (col2-col1-strlen(subtitle))/2+col1+kb_index (subtitle, ":")+1;
    draw_updn_arrows (row2, udac, LT_GRAY);
    draw_rectangle (&update_rect, BLUE, SAVEPIX);
    draw_shadow (widget, row1, col1, row2, col2, SAVEPIX);
    g_timeout_add (250, set_cursor, &ssd);
   }
  col = col1+1;
  for(i=0; i<MAX_SET_ITEMS; i++)
   {
    row = 2*(i%num1c_items)+row1+1;
    if(i==ssd.item)
     set_color = YELLOW;
    else
     set_color = LT_GRAY;
    write_line (row, col+shift_col[i/num1c_items], set_screen_items[i],
     set_color, SAVEPIX);
   }
  col = col1+lsi+2;
  for(i=0; i<MAX_SET_ITEMS; i++)
   {
    row = 2*(i%num1c_items)+row1+1;
    update_bar.x = (col+shift_col[i/num1c_items])*wcol;
    update_bar.y = row*hrow;
    update_bar.width = set_length[i]*wcol;
    update_bar.height = hrow;
    draw_rectangle (&update_bar, CYAN, SAVEPIX);
    write_line (row, col+shift_col[i/num1c_items], set_screen_choices[i],
     WHITE, SAVEPIX);
   }
  gtk_widget_queue_draw_area (widget,
                              update_rect.x-idraw*wcol,
                              update_rect.y-idraw*hrow,
                              update_rect.width+idraw*2*wcol,
                              update_rect.height+idraw*2*hrow);
  if(idraw) gtk_main ();
}

static gint set_key_handler(GtkWidget *widget, GdkEventKey *event,
                             gpointer data)
{
  SSVars *ssd;
  ESVars esd;
  guint ikey;
  gint ii, beep, lsi;
  
  ssd = (SSVars*)data;
  ikey = event->keyval;
  lsi = strlen(set_screen_items[0]);
  esd.ikey = ikey;
  esd.cursor_row = ssd->cursor_row;
  esd.cursor_colp = &ssd->cursor_col;
  esd.field_col = ssd->col1+ssd->shift_col[ssd->item/ssd->num1c_items]+lsi+3;
  esd.field_length = set_length[ssd->item];
  esd.field_linep = set_screen_choices[ssd->item];
  switch (ikey)
   {
    case 65505 : /*left shift*/
    case 65506 : /*right shift*/
    case 65509 : /*caps lock*/
    case 65407 : /*num lock*/
     break;
    case 65361 : /*left_arrow*/
    case 65363 : /*right_arrow*/
    case 65360 : /*home*/
    case 65367 : /*end*/
    case 65535 : /*delete*/
    case 65288 : /*backspace*/
    case 65307 : /*escape*/
     if(ssd->item==0 || (ssd->item>4 && ssd->item<10))
      edit_line (&esd);
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65379 : /*insert*/
     if(ssd->item==0 || (ssd->item>4 && ssd->item<10))
      {
       insert_mode = !insert_mode;
       edit_line (&esd);
      }
     else
      play_bells (BAD_KEY_SOUND);
     break;
    case 65470 : /*F1*/
     switch (ssd->item)
      {
       case 1 :
        ii = 0;
        while(strcmp(set_screen_choices[ssd->item],
         dupe_bracket[ii]) && ii<1)
          ii++;
        ii++;
        if(ii==2) ii=0;
        strcpy(set_screen_choices[ssd->item], dupe_bracket[ii]);
        set_screen (main_drawing_area, !REDRAW);
        break;
       case 2 :
       case 3 :
       case 4 :
       case 11 :
        ii = 0;
        while(strcmp(set_screen_choices[ssd->item],
         yn_bracket[ii]) && ii<1)
          ii++;
        ii++;
        if(ii==2) ii=0;
        strcpy(set_screen_choices[ssd->item], yn_bracket[ii]);
        set_screen (main_drawing_area, !REDRAW);
        break;
       case 10 :
        ii = 0;
        while(strcmp(set_screen_choices[ssd->item],
         partial_bracket[ii]) && ii<2)
          ii++;
        ii++;
        if(ii==3) ii=0;
        strcpy(set_screen_choices[ssd->item], partial_bracket[ii]);
        set_screen (main_drawing_area, !REDRAW);
        break;
       case 12 :
        ii = 0;
        while(strcmp(set_screen_choices[ssd->item],
         cat_bracket[ii]) && ii<2)
          ii++;
        ii++;
        if(ii==3) ii=0;
        strcpy(set_screen_choices[ssd->item], cat_bracket[ii]);
        set_screen (main_drawing_area, !REDRAW);
        break;
       default :
        play_bells (BAD_KEY_SOUND);
      }
     break;
    case 65482 : /*F10*/
     if(set_error_check (ssd))
      {
       if(insert_mode && (ssd->item==0 || (ssd->item>4 && ssd->item<10)))
        {
         esd.ikey = 65293; /*enter*/
         edit_line (&esd);
        }
       break;
      }
     write_ini_file ();
     insert_mode = 0;
     shift_fkeys = (strcmp(set_screen_choices[11], yn_bracket[1]));
     ssd->done = TRUE;
     break;
    case 65362 : /*up_arrow*/
     if(set_error_check (ssd))
      {
       if(insert_mode && (ssd->item==0 || (ssd->item>4 && ssd->item<10)))
        edit_line (&esd);
       break;
      }
     if(ssd->item>0)
      {
       ssd->item--;
       if(ssd->item==9)
        switch (isel_contest)
         {
          case WPX :
          case SS :
          case TEN :
           ssd->item--;
           break;
         }
      }       
     else
      ssd->item = MAX_SET_ITEMS-1;
     ssd->cursor_row = 2*(ssd->item%ssd->num1c_items)+ssd->row1+2;
     ssd->cursor_col = ssd->col1
      +ssd->shift_col[ssd->item/ssd->num1c_items]+lsi+3
      +strlen(set_screen_choices[ssd->item]);
     set_screen (main_drawing_area, !REDRAW);
     if((ssd->item==0 || (ssd->item>4 && ssd->item<10)) && insert_mode)
      {
       esd.cursor_row = ssd->cursor_row;
       esd.cursor_colp = &ssd->cursor_col;
       esd.field_col = ssd->col1
        +ssd->shift_col[ssd->item/ssd->num1c_items]+lsi+3;
       esd.field_length = set_length[ssd->item];
       esd.field_linep = set_screen_choices[ssd->item];
       edit_line (&esd);
      }
     break;
    case 65364 : /*down_arrow*/
     if(set_error_check (ssd))
      {
       if(insert_mode && (ssd->item==0 || (ssd->item>4 && ssd->item<10)))
        edit_line (&esd);
       break;
      }
     if(ssd->item<MAX_SET_ITEMS-1)
      {
       ssd->item++;
       if(ssd->item==9)
        switch (isel_contest)
         {
          case WPX :
          case SS :
          case TEN :
           ssd->item++;
           break;
         }  
      }
     else
      ssd->item = 0;
     ssd->cursor_row = 2*(ssd->item%ssd->num1c_items)+ssd->row1+2;
     ssd->cursor_col = ssd->col1
      +ssd->shift_col[ssd->item/ssd->num1c_items]+lsi+3
      +strlen(set_screen_choices[ssd->item]);
     set_screen (main_drawing_area, !REDRAW);
     if((ssd->item==0 || (ssd->item>4 && ssd->item<10)) && insert_mode)
      {
       esd.cursor_row = ssd->cursor_row;
       esd.cursor_colp = &ssd->cursor_col;
       esd.field_col = ssd->col1
        +ssd->shift_col[ssd->item/ssd->num1c_items]+lsi+3;
       esd.field_length = set_length[ssd->item];
       esd.field_linep = set_screen_choices[ssd->item];
       edit_line (&esd);
      }
     break;
    default :
     beep = 1;
     switch (ssd->item)
      {
       case 0 :
        ikey = toupper_ui(ikey);
        if(isupper_ui(ikey) || isdigit_ui(ikey))
         beep = 0;
        break;
       case 5 :
       case 6 :
       case 7 :
       case 8 :
       case 9 :
        if(isdigit_ui(ikey))
         beep = 0;
        break;
      }
     if(!beep)
      {
       esd.ikey = ikey;
       edit_line (&esd);
      }
     else
      play_bells (BAD_KEY_SOUND);
   }
  return TRUE;
}

static gint set_mouse_handler(GtkWidget *widget, GdkEventButton *event,
                              gpointer data)
{
  int x, y;
  GdkModifierType state;
  SSVars *ssd;
  ESVars esd;
  gint mouse_row, mouse_col, mouse_item, min_col, max_col, lsi;

  if(event->type==GDK_BUTTON_PRESS)
   {
    lsi = strlen(set_screen_items[0]);
    ssd = (SSVars*)data;
    if(set_error_check (ssd))
     {
      if(insert_mode && (ssd->item==0 || (ssd->item>4 && ssd->item<10)))
       {
        esd.ikey = 65293; /*enter*/
        esd.cursor_row = ssd->cursor_row;
        esd.cursor_colp = &ssd->cursor_col;
        esd.field_col = ssd->col1
         +ssd->shift_col[ssd->item/ssd->num1c_items]+lsi+3;
        esd.field_length = set_length[ssd->item];
        esd.field_linep = set_screen_choices[ssd->item];
        edit_line (&esd);
       }
      return TRUE;
     }
    gdk_window_get_pointer (event->window, &x, &y, &state);
    mouse_col = (x-1)/wcol+1;
    mouse_row = (y-1)/hrow+1;
    if(mouse_row>ssd->row1+1 && mouse_row<ssd->row1+2*ssd->num1c_items+2)
     {
      mouse_item = (mouse_row-ssd->row1-2)/2;
      if(mouse_col>ssd->col1+ssd->shift_col[1])
       mouse_item += ssd->num1c_items;
      if(mouse_item>MAX_SET_ITEMS-1)
       return TRUE;
      if(mouse_item==9)
       switch (isel_contest)
        {
         case WPX :
         case SS :
         case TEN :
          return TRUE;
          break;
        }
      min_col = ssd->col1+ssd->shift_col[mouse_item/ssd->num1c_items]+lsi+3;
      if(mouse_col<=min_col+set_length[mouse_item]+2)
       {
        ssd->item = mouse_item;
        ssd->cursor_row = 2*(ssd->item%ssd->num1c_items)+ssd->row1+2;
        max_col = min_col+strlen(set_screen_choices[ssd->item]);
        if(mouse_col<min_col)
         ssd->cursor_col = min_col;
        else if(mouse_col>max_col)
         ssd->cursor_col = max_col;
        else
         ssd->cursor_col = mouse_col;
        set_screen (widget, !REDRAW);
        if(insert_mode)
         {
          switch (ssd->item)
           {
            case 0 :
            case 5 :
            case 6 :
            case 7 :
            case 8 :
            case 9 :
             esd.ikey = 65293; /*enter*/
             esd.cursor_row = ssd->cursor_row;
             esd.cursor_colp = &ssd->cursor_col;
             esd.field_col = min_col;
             esd.field_length = set_length[ssd->item];
             esd.field_linep = set_screen_choices[ssd->item];
             edit_line (&esd);
             break;
           }
         }
       }
     }
   }
  return TRUE;
}

static gint set_cursor(gpointer data)
{
  SSVars *ssd;
  gint cursor_fg_color, cursor_bg_color;
  static gint iadd = 0;

  ssd = (SSVars*)data;
  if(ssd->done)
   {
    g_signal_handler_disconnect (main_window, set_key_handler_id);
    g_signal_handler_disconnect (main_drawing_area,
                                 set_mouse_handler_id);
    g_signal_handler_unblock (main_window, main_key_handler_id);
    g_signal_handler_unblock (main_drawing_area, main_mouse_handler_id);
    gtk_main_quit ();
    return FALSE;
   }
  if(ssd->item==0 || (ssd->item>4 && ssd->item<10))
   {
    iadd++;
    iadd %= 4;
    if(insert_mode)
     {
      cursor_fg_color = HI_CYAN;
      cursor_bg_color = LT_GRAY;
     }
    else
     {
      cursor_fg_color = WHITE;
      cursor_bg_color = CYAN;
     }
    cursor_control (main_drawing_area, iadd, LINE_CURSOR,
                    ssd->cursor_row, ssd->cursor_col,
                    cursor_fg_color, cursor_bg_color);
   }
  return TRUE;
}

static gint set_error_check(gpointer data)
{
  SSVars *ssd;
  gint i, ihold, lsc;
  gchar msg[65], line_hold[4], digit_char[2];
  gint set_error=FALSE;
  guint msg_time=2000;

  ssd = (SSVars*)data;
  switch (ssd->item)
   {
    case 0 :
     switch (isel_contest)
      {
       case DX :
        if(strlen(set_screen_choices[ssd->item])!=5-mode)
         set_error = TRUE;
        else
         {
          for(i=0; i<3-mode; i++)
           if(!isdigit(set_screen_choices[ssd->item][i]) ||
              set_screen_choices[ssd->item][i]=='0')
            {
             set_error = TRUE;
             break;
            }
          if(!set_error)
           {
            concat(digit_char, set_screen_choices[ssd->item], 0, 0, COPY);
            if(atoi(digit_char)>5)
             set_error = TRUE;
           }
          if(!set_error)
           for(i=3-mode; i<5-mode; i++)
            if(!isupper(set_screen_choices[ssd->item][i]))
             {
              set_error = TRUE;
              break;
             }
         }
        if(set_error)
         {
          set_error_message (msg, ssd->item);
          strcat(msg, " (Syntax:RSTSt/Pr)");
          msg_time = 5000;
         }
        break;
       case WPX :
        if(strlen(set_screen_choices[ssd->item])!=3-mode)
         set_error = TRUE;
        else
         {
          for(i=0; i<3-mode; i++)
           if(!isdigit(set_screen_choices[ssd->item][i]) ||
              set_screen_choices[ssd->item][i]=='0')
            {
             set_error = TRUE;
             break;
            }
          if(!set_error)
           {
            concat(digit_char, set_screen_choices[ssd->item], 0, 0, COPY);
            if(atoi(digit_char)>5)
             set_error = TRUE;
           }
         }
        if(set_error)
         {
          set_error_message (msg, ssd->item);
          strcat(msg, " (Syntax:RST)");
          msg_time = 5000;
         }
        break;
       case FD :
        lsc = strlen(set_screen_choices[ssd->item]);
        if(lsc<4)
         set_error = TRUE;
        else if(!isdigit(set_screen_choices[ssd->item][0]))
         set_error = TRUE;
        else if(!isalpha(set_screen_choices[ssd->item][1]))
         set_error = TRUE;
        else
         {
          concat(line_hold, set_screen_choices[ssd->item],
                 2, lsc-1, COPY);
          set_error = !valid_locus (line_hold);
         }
        if(set_error)
         {
          set_error_message (msg, ssd->item);
          strcat(msg, " (Syntax:NrClSec)");
          msg_time = 5000;
         }
        break;
       case WW :
        if(strlen(set_screen_choices[ssd->item])!=5-mode)
         set_error = TRUE;
        else
         {
          for(i=0; i<3-mode; i++)
           if(!isdigit(set_screen_choices[ssd->item][i]) ||
              set_screen_choices[ssd->item][i]=='0')
            {
             set_error = TRUE;
             break;
            }
          if(!set_error)
           {
            concat(digit_char, set_screen_choices[ssd->item], 0, 0, COPY);
            if(atoi(digit_char)>5)
             set_error = TRUE;
           }
          if(!set_error)
           for(i=3-mode; i<5-mode; i++)
            if(!isdigit(set_screen_choices[ssd->item][i]))
             {
              set_error = TRUE;
              break;
             }
          if(!set_error)
           {
            concat(line_hold, set_screen_choices[ssd->item],
                   3-mode, 4-mode, COPY);
            ihold = atoi(line_hold);
            if(ihold<1 || ihold>40)
             set_error = TRUE;
           }
         }
        if(set_error)
         {
          set_error_message (msg, ssd->item);
          strcat(msg, " (Syntax:RSTZn)");
          msg_time = 5000;
         }
        break;
       case SS :
        lsc = strlen(set_screen_choices[ssd->item]);
        if(lsc<5)
         set_error = TRUE;
        else
         {
          set_error = !valid_precedence(set_screen_choices[ssd->item][0]);
          if(!set_error)
           for(i=1; i<3; i++)
            if(!isdigit(set_screen_choices[ssd->item][i]))
             {
              set_error = TRUE;
              break;
             }
          if(!set_error)
           {
            concat(line_hold, set_screen_choices[ssd->item],
                   3, lsc-1, COPY);
            set_error = !valid_locus (line_hold);
           }
         }
        if(set_error)
         {
          set_error_message (msg, ssd->item);
          strcat(msg, " (Syntax:PrCkSec)");
          msg_time = 5000;
         }
        else
         {
          ihold = check_precedence ();
          if(ihold!=set_screen_choices[ssd->item][0])
           {
            set_error = TRUE;
            strcpy(msg, "Precedence for your entry category is: ");
            lsc = strlen(msg);
            msg[lsc] = ihold;
            msg[lsc+1] = '\0';
            msg_time = 5000;
            set_screen_choices[ssd->item][0] = ihold;
            set_screen (main_drawing_area, !REDRAW);
           }
         }
        break;
       case NYQP :
       case A160 :
       case TEN :
        lsc = strlen(set_screen_choices[ssd->item]);
        if(lsc<5-mode)
         set_error = TRUE;
        else
         {
          for(i=0; i<3-mode; i++)
           if(!isdigit(set_screen_choices[ssd->item][i]) ||
              set_screen_choices[ssd->item][i]=='0')
            {
             set_error = TRUE;
             break;
            }
          if(!set_error)
           {
            concat(digit_char, set_screen_choices[ssd->item], 0, 0, COPY);
            if(atoi(digit_char)>5)
             set_error = TRUE;
           }
          if(!set_error)
           {
            concat(line_hold, set_screen_choices[ssd->item],
                   3-mode, lsc-1, COPY);
            set_error = !valid_locus (line_hold);
            if(isel_contest==NYQP && set_error)
             if(strcmp(init_screen_choices[6], "NY"))
              {
               set_error = FALSE;
               for(i=0; i<strlen(line_hold); i++)
                if(!isalpha(line_hold[i]))
                 {
                  set_error = TRUE;
                  break;
                 }
              }
           }
         }
        if(set_error)
         {
          set_error_message (msg, ssd->item);
          if(isel_contest==A160)
           strcat(msg, " (Syntax:RSTSec)");
          else if(isel_contest==NYQP)
           strcat(msg, " (Syntax:RSTCty/St/Pr)");
          else
           strcat(msg, " (Syntax:RSTSt/Pr)");
          msg_time = 5000;
         }
        break;
       case WAA :
        lsc = strlen(set_screen_choices[ssd->item]);
        if(lsc<1)
         set_error = TRUE;
        if(set_error)
         set_error_message (msg, ssd->item);
      }
     break;
    case 5 :
     if(!strlen(set_screen_choices[ssd->item]) ||
        atoi(set_screen_choices[ssd->item])<1 ||
        atoi(set_screen_choices[ssd->item])>12)
      {
       set_error_message (msg, ssd->item);
       set_error = TRUE;
       msg_time = 3000;
      }
     break;
    case 6 :
     if(!strlen(set_screen_choices[ssd->item]) ||
        atoi(set_screen_choices[ssd->item])<100)
      {
       set_error_message (msg, ssd->item);
       set_error = TRUE;
       msg_time = 3000;
      }
     break;
    case 7 :
    case 8 :
    case 9 :
     if(!strlen(set_screen_choices[ssd->item]) ||
        atoi(set_screen_choices[ssd->item])<5)
      {
       set_error_message (msg, ssd->item);
       set_error = TRUE;
       msg_time = 3000;
      }
     break;
   }
  if(set_error)
   {
    play_bells (BAD_KEY_SOUND);
    boxed_message (msg, msg_time, YELLOW);
   }
  return set_error;
}

static void set_error_message(gchar *s1, gint ssdpi)
{
  gchar ssi[20];

  strcpy(ssi, set_screen_items[ssdpi]);
  left_blank_unpad (ssi);
  strcpy(s1, "Invalid Entry for ");
  strcat(s1, ssi);
  strcat(s1, ":");
  strcat(s1, set_screen_choices[ssdpi]);
}

static gint check_precedence(void)
{
  gint cp='Z';

  if(!strcmp(init_screen_choices[11], "<SINGLE-OP>") &&
     !strcmp(init_screen_choices[12], "<LOW>"))
   cp = 'A';
  else if(!strcmp(init_screen_choices[11], "<SINGLE-OP>") &&
          !strcmp(init_screen_choices[12], "<HIGH>"))
   cp = 'B';
  else if(!strcmp(init_screen_choices[11], "<SINGLE-OP>") &&
          !strcmp(init_screen_choices[12], "<QRP>"))
   cp = 'Q';
  else if(!strcmp(init_screen_choices[11], "<M-SINGLE>"))
   cp = 'M';
  else if(!strcmp(init_screen_choices[11], "<SCHOOL>"))
   cp = 'S';
  else if(!strcmp(init_screen_choices[11], "<SO-ASSIST>"))
   cp = 'U';
  return cp;
}

static gint butt_kick(gpointer widget)
{
  GdkRectangle update_rect;
  GdkPoint animation_points[15];
  gint i, asf;
  gint row1=6, col1=25;
  gint boot_xpoints[10] = {3, 3, 11, 14, 19, 19, 16, 13, 7, 3};
  gint boot_ypoints[10] = {11, 15, 25, 25, 20, 18, 16, 19, 11, 11};
  gint tush_xpoints[15] = {17, 17, 18, 18, 21, 20, 20, 22, 22, 24,
                           24, 26, 28, 22, 17};
  gint tush_ypoints[15] = {11, 16, 16, 17, 16, 18, 21, 21, 22, 22,
                           21, 21, 19, 11, 11};
  gint bolt1_xpoints[7] = {24, 25, 27, 27, 28, 28, 30};
  gint bolt1_ypoints[7] = {23, 24, 24, 26, 26, 28, 28};
  gint bolt2_xpoints[6] = {20, 20, 22, 21, 23, 23};
  gint bolt2_ypoints[6] = {23, 25, 25, 27, 27, 29};
  gint bolt3_xpoints[7] = {18, 17, 17, 16, 16, 15, 15};
  gint bolt3_ypoints[7] = {25, 26, 27, 28, 29, 29, 30};
  gint bolt4_xpoints[7] = {16, 15, 14, 14, 15, 15, 14};
  gint bolt4_ypoints[7] = {14, 14, 13, 11, 10, 6, 5};
  gint bolt5_xpoints[7] = {14, 13, 13, 12, 12, 11, 10};
  gint bolt5_ypoints[7] = {11, 11, 10, 10, 9, 7, 6};
  gint bolt6_xpoints[7] = {12, 12, 11, 10, 10, 6, 5};
  gint bolt6_ypoints[7] = {13, 12, 12, 11, 9, 9, 8};
  static gint iadd = 3;

  if(idisplay_type>IDT_WELCOME) return FALSE;
  iadd++;
  iadd %= 4;
  update_rect.x = col1*wcol;
  update_rect.y = (row1-2)*hrow;
  update_rect.width = 31*wcol;
  update_rect.height = 7*hrow;
  draw_rectangle (&update_rect, BLUE, SAVEPIX);
  new_pen (BLACK);
  asf = (6*main_drawing_area->allocation.width)/800;
  for(i=0; i<10; i++)
   {
    animation_points[i].x = asf*(boot_xpoints[i]-3) + (col1+5*iadd)*wcol;
    animation_points[i].y = asf*(boot_ypoints[i]-11) + row1*hrow;
   }
  gdk_draw_polygon (pixmap, gc, TRUE, animation_points, 10);
  new_pen (HI_MAGENTA);
  for(i=0; i<15; i++)
   {
    animation_points[i].x = asf*(tush_xpoints[i]-4) + (col1+14+iadd/3)*wcol;
    animation_points[i].y = asf*(tush_ypoints[i]-11) + row1*hrow;
   }
  gdk_draw_polygon (pixmap, gc, TRUE, animation_points, 15);
  if(iadd==3)
   {
    new_pen (YELLOW);
    for(i=0; i<7; i++)
     {
      animation_points[i].x = asf*(bolt1_xpoints[i]-4) + (col1+15)*wcol;
      animation_points[i].y = asf*(bolt1_ypoints[i]-11) + row1*hrow;
     }
    gdk_draw_lines (pixmap, gc, animation_points, 7);
    for(i=0; i<6; i++)
     {
      animation_points[i].x = asf*(bolt2_xpoints[i]-4) + (col1+15)*wcol;
      animation_points[i].y = asf*(bolt2_ypoints[i]-11) + row1*hrow;
     }
    gdk_draw_lines (pixmap, gc, animation_points, 6);
    for(i=0; i<7; i++)
     {
      animation_points[i].x = asf*(bolt3_xpoints[i]-4) + (col1+15)*wcol;
      animation_points[i].y = asf*(bolt3_ypoints[i]-11) + row1*hrow -4;
     }
    gdk_draw_lines (pixmap, gc, animation_points, 7);
    for(i=0; i<7; i++)
     {
      animation_points[i].x = asf*(bolt4_xpoints[i]-4) + (col1+15)*wcol;
      animation_points[i].y = asf*(bolt4_ypoints[i]-11) + row1*hrow;
     }
    gdk_draw_lines (pixmap, gc, animation_points, 7);
    for(i=0; i<7; i++)
     {
      animation_points[i].x = asf*(bolt5_xpoints[i]-4) + (col1+15)*wcol;
      animation_points[i].y = asf*(bolt5_ypoints[i]-11) + row1*hrow;
     }
    gdk_draw_lines (pixmap, gc, animation_points, 7);
    for(i=0; i<7; i++)
     {
      animation_points[i].x = asf*(bolt6_xpoints[i]-4) + (col1+15)*wcol;
      animation_points[i].y = asf*(bolt6_ypoints[i]-11) + row1*hrow;
     }
    gdk_draw_lines (pixmap, gc, animation_points, 7);
   }
  gtk_widget_queue_draw_area (widget,
                              update_rect.x,
                              update_rect.y,
                              update_rect.width,
                              update_rect.height);
  return TRUE;
}
