// from gxedit.c, line 4239
void CB_open_window_ok(GtkWidget *widget, GtkFileSelection *fs)   
{
 sprintf(line, "%s %s &", progname, gtk_file_selection_get_filename(GTK_FILE_SELECTION(fs)));
 system(line);
 gtk_widget_destroy(window2);
 window2 = NULL;
}

void CB_open_hex_ok(GtkWidget *widget, GtkFileSelection *fs)
{
 int i;
 strncpy(filename, gtk_file_selection_get_filename(GTK_FILE_SELECTION(fs)), 100);
 fd = fopen(filename, "r");
 if(fd==NULL)
 {
  print_debug("Can't open file for reading\n");
  msgbox("Can't open file for reading");
  return;
 }
 gtk_text_freeze(GTK_TEXT(text));
 gtk_text_set_point(GTK_TEXT(text), 0);
 gtk_text_forward_delete(GTK_TEXT(text),gtk_text_get_length(GTK_TEXT(text)));
 i=0;
 while(fgets(line,512,fd)!=NULL)
 {
  i++;
  convert_to_hex(line);
 }
 gtk_text_thaw (GTK_TEXT (text));
 sprintf(temp, "File loaded (%d lines)\n", i+1);
 print_debug(temp);
 fclose(fd);
 sprintf(line, "GXedit %s [%s]\n", VERSION, filename);  
 gtk_window_set_title (GTK_WINDOW (window), line);
 gtk_widget_destroy(window2);
}

void CB_import_man(GtkWidget *widget, GtkFileSelection *fs)
{
 char buf[101];
 int lines, broke;
 strncpy(buf,gtk_file_selection_get_filename(GTK_FILE_SELECTION(fs)),100);
 if(buf[strlen(buf)-1]=='z')
 {
  system("rm -f /tmp/file.man.gz");
  sprintf(temp,"cp %s /tmp/file.man.gz",buf);
  system(temp);
  sprintf(temp,"%s /tmp/file.man.gz",gzip);
  system(temp);
 }
 else
 {
  sprintf(temp,"cp %s /tmp/file.man",buf);
  system(temp);
 }
 gtk_text_set_point(GTK_TEXT(text), 0);
 gtk_text_forward_delete(GTK_TEXT(text),
 gtk_text_get_length(GTK_TEXT(text)));
 fd = fopen("/tmp/file.man","r");
 if(fd==NULL)
 {
  print_debug("Can't open file for reading\n");
  msgbox("Can't open file for reading");
  return;
 }
 gtk_text_freeze(GTK_TEXT(text));
 lines = 0; broke = 0;
 while(fgets(line,512,fd)!=NULL)
 {
  lines++;
  line[strlen(line)-1]=' ';
  if(lindex(line,0)==NULL) strcpy(line,". ");
  if(!strcasecmp(lindex(line,0),".SH"))
  {
   gtk_text_insert (GTK_TEXT (text), NULL, &color, NULL, "\n\n", -1);
   gtk_text_insert (GTK_TEXT (text), NULL, &html, NULL, lrange(line,1), -1);
   gtk_text_insert (GTK_TEXT (text), NULL, &color, NULL, "\n\n", -1);
  }
  else if(!strcasecmp(lindex(line,0),".B") || !strcasecmp(lindex(line,0),".I") || !strcasecmp(lindex(line,0),".BI") || !strcasecmp(lindex(line,0),".IB"))
  {
   gtk_text_insert (GTK_TEXT (text), NULL, &html, NULL, lrange(line,1), -1);
  }
  else if(!strcasecmp(lindex(line,0),".BR") || !strcasecmp(lindex(line,0),".NL"))
  {
   gtk_text_insert (GTK_TEXT (text), NULL, &color, NULL, "\n", -1);
  }
  else if(!strcasecmp(lindex(line,0),".SO"))
  {
   gtk_text_insert (GTK_TEXT (text), NULL, &html, NULL, "See ", -1);
   gtk_text_insert (GTK_TEXT (text), NULL, &html, NULL, lrange(line,1), -1);
   gtk_text_insert (GTK_TEXT (text), NULL, &html, NULL, "\n", -1);
  }
  else if(!strcasecmp(lindex(line,0),".TP"))
  {
   gtk_text_insert (GTK_TEXT (text), NULL, &color, NULL, "\n", -1);
  }
  else if(*lindex(line,0)=='.')
  {
   broke++;
  }
  else
  {
   gtk_text_insert (GTK_TEXT (text), NULL, &color, NULL, " ", -1);
   gtk_text_insert (GTK_TEXT (text), NULL, &color, NULL, line, -1);
  }
 }
 gtk_text_thaw (GTK_TEXT (text));
 gtk_widget_destroy(window2);
 sprintf(temp, "Converted. Text broken at %d%%", ((broke*100)/lines));
 msgbox(temp);
 system("rm -f /tmp/file.man");
 fclose(fd);
}
