#include windows.sh
#include mew_dlgs.sh
#include dialog.sh
#include fonts.sh
//#include mewhelp.sh

//#define ASCII_Modeless 0
#define ASCII_Modeless (DLG_Modeless | DLG_NoParDisable) // parens necessary for proper evaluation
#define ASCII_Paste_Ctrl  101
#define ASCII_OEM_Ctrl    106
#define ASCII_ANSI_Ctrl   105
#define ASCII_List_Ctrl   1000

global {
  int ascii_active_window   "!ASCII_ACTIVE_WINDOW"; // temporary kludge
  int ascii_window          "!ASCII_WINDOW";
  int ascii_hdlg            "!ASCII_HDLG";
  int ascii_dlg             "!ASCII_DLG";
  int ansi_font             "!ASCII_ANSI_FONT";
  int oem_font              "!ASCII_OEM_FONT";
  str g_ansi_fontname       "!ANSI_FONTNAME";
  str g_oem_fontname        "!OEM_FONTNAME";
}


void ASCII TRANS2 NO_BREAK {

	if (ascii_hdlg != 0) {
// don't allow multiple occurances, switch to currently active ascii table
		SetActiveWindow(ascii_hdlg);
		Return();
	}
ascii_active_window = window_id; // remove this after window switch problem is fixed
	int active_window = window_id,
			t_int,
			ascii_window_num;

			OEM_Font = 0;
			ANSI_Font = 0;

	str t_hex_str[2],
			t_dec_str[3],
//			t_char_str1[2],
			t_char_str2[2],
			OEM_fontname = "",
			ANSI_fontname = "";

  int lh,fh,fw,dlgfont,
			fontsize = 0,
			charset = 0,
			selected_char;

	struct TLogFont cur_font;

	ascii_window = 0;
	OEM_Font = 0;
	ANSI_Font = 0;
  Get_MEW_Dlg_Metrics (0, lh, fh,fw,dlgfont);

//	GetTextMetrics();



  if (global_str("ASC_CHRT_FONTS") == "") {
		OEM_fontname = "Terminal";
		ANSI_fontname = "Fixedsys";
	} else {
    ANSI_fontname = parse_str("/ANAME=",global_str("ASC_CHRT_FONTS"));
    OEM_fontname = parse_str("/ONAME=",global_str("ASC_CHRT_FONTS"));
	}

	g_ansi_fontname = ansi_fontname;
	g_oem_fontname = oem_fontname;
	create_window;
	file_name = me_path + "ASCIITBL.TMP";

	ascii_window = window_id;
	ascii_window_num = cur_window;

	window_attr = 0x80;


AGAIN:
  cur_font.lfFaceName       = OEM_fontname;
  cur_font.lfCharSet        = OEM_Charset;
  cur_font.lfHeight         = -fh;
  cur_font.lfWidth          = 0;
  cur_font.lfEscapement     = 0;
  cur_font.lfWeight         = fw_normal;
  cur_font.lfItalic         = 0;
  cur_font.lfUnderline      = 0;
  cur_font.lfStrikeOut      = 0;
  cur_font.lfOutPrecision   = OUT_DEFAULT_PRECIS;
  cur_font.lfCLipPrecision  = Clip_Default_Precis;
  cur_font.lfQuality        = Draft_Quality;
  cur_font.lfPitchAndFamily = Default_Pitch;

  OEM_font  = CreateFontIndirect(&cur_Font);

  cur_font.lfFaceName  = ANSI_fontname;
 	cur_font.lfCharSet = ANSI_Charset;
  cur_font.lfHeight = -fh;
  cur_font.lfWidth = 0;
 	cur_font.lfEscapement = 0;
  cur_font.lfWeight = fw_normal;
 	cur_font.lfItalic = 0;
 	cur_font.lfUnderline = 0;
 	cur_font.lfStrikeOut = 0;
  cur_font.lfOutPrecision = OUT_DEFAULT_PRECIS;
 	cur_font.lfCLipPrecision = Clip_Default_Precis;
  cur_font.lfQuality = Draft_Quality;
  cur_font.lfPitchAndFamily = Default_Pitch;

  ANSI_font  = CreateFontIndirect(&cur_Font);

	DlgCreate(t_int);
	ASCII_dlg = t_int;
	Working;

  str fstr1[40] = "/FT=" + str(ANSI_font)      // TMJ 8/13/94 8:14AM
        + "/FC=" + str(GetSysColor(Color_WindowText))
        + "/BC=" + str(GetSysColor(Color_Window)) ;

  str fstr2[40] = "/FT=" + str(OEM_font)       // TMJ 8/13/94 8:14AM
        + "/FC=" + str(GetSysColor(Color_WindowText))
        + "/BC=" + str(GetSysColor(Color_Window));

  int tundo = undo_stat;

  undo_stat = false;

	for (t_int = 0; t_int < 256; ++t_int) {
		t_hex_str = hex_str(t_int);
		if (svl(t_hex_str) == 1) {
			t_hex_str = "0" + t_hex_str;
		}

		t_dec_str = str(t_int);
		t_dec_str = copy("  ",1,3 - svl(t_dec_str)) + t_dec_str;

		t_char_str2 = char(t_int);
//		t_char_str1 = t_char_str2;

		if (t_int == 47) {
			t_char_str2 += t_char_str2;
		}
		if (t_int == 127) {
//			t_char_str1 += t_char_str1;
			t_char_str2 += t_char_str2;
		}

/*
    put_line( "DEC=" + t_dec_str + "HEX=" + t_hex_str +
			"AC=" + t_char_str1 +
			"OC=/TX=" + t_char_str2 + "/FT=" + str(OEM_font)
        + "/FC=" + str(GetSysColor(Color_WindowText))
				+	"/BC=" + str(GetSysColor(Color_Window)) );
*/
    put_line( "DEC=" + t_dec_str + "HEX=" + t_hex_str +  // TMJ 8/13/94 8:14AM
      "AC=/TX=" + t_char_str2 + fstr1 +
      "OC=/TX=" + t_char_str2 + fstr2 );
		down;
	}

  undo_stat = tundo;
	tof;
  DlgAddCtrl( ASCII_dlg, DLG_BitmapStatic, "BT_GN_123",
    1,
    1,
		0,0,1001,0,"");

  DlgAddCtrl( ASCII_dlg, DLG_Static, "Dec  Hex",
    2,DLG_Units + 22,0,0,110,0,"");

  DlgAddCtrl( ASCII_dlg, DLG_PushButton, "&Oem",
    DLg_Units + 43,
    Dlg_units + 19,
    Dlg_units + 20,
    Dlg_units + 10,
    ASCII_OEM_Ctrl,0,"/R=6/M=ASCII_CHANGE_OEM");
  DlgAddCtrl( ASCII_dlg, DLG_PushButton, "&Ansi",
		DLG_PosOffset + 6,
		Dlg_PosOffset,
		Dlg_units + 20,
    Dlg_units + 10,
		ASCII_ANSI_Ctrl,0,"/R=5/M=ASCII_CHANGE_ANS");

	DlgAddCtrl( ASCII_dlg, DLG_ListBox, "DEC=/W=5HEX=/W=6OC=/W=5/T=5AC=/W=3/T=5",
    2,
		DLG_Units + 30,
		23,
		10,
		ASCII_List_Ctrl,0,"/INCO=1/WIN=" + str(cur_window));

  DlgAddCtrl( ASCII_dlg, DLG_PushButton, "Paste", 1,Dlg_PosOffset + 11,0,0,ASCII_Paste_ctrl,DLGF_DefButton,"/R=1");
  DlgAddCtrl( ASCII_dlg, DLG_PushButton, "Close", dlg_PosOffset + 9,DLG_PosOffset,0,0,102,0,"/R=0");
  DlgAddCtrl( ASCII_dlg, DLG_PushButton, "&Help",
    dlg_PosOffset + 9,
		DLG_PosOffset,
		0,0,103,0,"/R=2");
  return_int = DlgExecute( ASCII_dlg, ASCII_List_Ctrl, "Ascii Table", "ASCII TABLE","/HOOK=AsciiMsgProc",ASCII_Modeless);

	switch_win_id(active_window);

//set_global_int("!@XXX",0);

  return();
}

void ASCII_CHANGE_OEM() {
	int active_window = window_id,
			t_insert_mode = insert_mode,
			lh, fh, fw, dlgfont,
			fontsize = 0,
			charset = 0,
			hdlg = parse_int("/DLGHANDLE=",mparm_str);
	str oem_fontname = g_oem_fontname,
			t_str;
	struct TLogFont cur_font;

int t_int;

	switch_win_id(ascii_window);
	mark_pos;

  Get_MEW_Dlg_Metrics (0, lh, fh,fw,dlgfont);
	insert_mode = t_insert_mode;
//	GetTextMetrics();

//	make_message(str(oem_font));
  if (SelectFont(0, OEM_fontname, fontsize, charset, "")) {
		g_oem_fontname = OEM_fontname;

//make_message("[" + g_oem_fontname + "]");

		DeleteObject(OEM_font);
	  cur_font.lfFaceName  = OEM_fontname;
	 	cur_font.lfCharSet = OEM_Charset;
	  cur_font.lfHeight = -fh;
	  cur_font.lfWidth = 0;
	 	cur_font.lfEscapement = 0;
	  cur_font.lfWeight = fw_normal;
	 	cur_font.lfItalic = 0;
	 	cur_font.lfUnderline = 0;
	 	cur_font.lfStrikeOut = 0;
	  cur_font.lfOutPrecision = OUT_DEFAULT_PRECIS;
	 	cur_font.lfCLipPrecision = Clip_Default_Precis;
	  cur_font.lfQuality = Draft_Quality;
	  cur_font.lfPitchAndFamily = Default_Pitch;

	  OEM_font  = CreateFontIndirect(&cur_Font);

		tof;
		while (!at_eof) {
			t_str = get_line;
			if (find_text("OC=",1,0)) {
        if (find_text("/FT=",1,0)) {

          right;
          right;
          right;
          right;
          while (cur_char != "/") {
            del_char;
          }
          text(str(oem_font));
        }
      }

			down;
			goto_col(1);
		}
		RedrawWindow(GetDlgItem(hdlg, ASCII_List_Ctrl), 0, 0, RDW_INVALIDATE);

	}

//make_message("AFTER");
	goto_mark;
	insert_mode = t_insert_mode;
	switch_win_id(active_window);
	SetFocus(GetDlgItem(hdlg, ASCII_Paste_Ctrl));
	SetDefaultButton(hdlg,ASCII_Paste_ctrl); // this does not seem to work
	SetFocus(GetDlgItem(hdlg, ASCII_List_Ctrl));
	return_int = 0;
}

void ASCII_CHANGE_ANS() {
	int active_window = window_id,
			t_insert_mode = insert_mode,
			lh, fh, fw, dlgfont,
			fontsize = 0,
			charset = 0,
			hdlg = parse_int("/DLGHANDLE=",mparm_str);
	str ansi_fontname = g_ansi_fontname,
			t_str;
	struct TLogFont cur_font;

int t_int;

	switch_win_id(ascii_window);
	mark_pos;

  Get_MEW_Dlg_Metrics (0, lh, fh,fw,dlgfont);
	insert_mode = t_insert_mode;
//	GetTextMetrics();

//	make_message(str(ansi_font));
  if (SelectFont(0, ANSI_fontname, fontsize, charset, "")) {
		g_ansi_fontname = ANSI_fontname;

//make_message("[" + g_ANSI_fontname + "]");

		DeleteObject(ANSI_font);
	  cur_font.lfFaceName  = ANSI_fontname;
	 	cur_font.lfCharSet = ANSI_Charset;
	  cur_font.lfHeight = -fh;
	  cur_font.lfWidth = 0;
	 	cur_font.lfEscapement = 0;
	  cur_font.lfWeight = fw_normal;
	 	cur_font.lfItalic = 0;
	 	cur_font.lfUnderline = 0;
	 	cur_font.lfStrikeOut = 0;
	  cur_font.lfOutPrecision = OUT_DEFAULT_PRECIS;
	 	cur_font.lfCLipPrecision = Clip_Default_Precis;
	  cur_font.lfQuality = Draft_Quality;
	  cur_font.lfPitchAndFamily = Default_Pitch;

	  ANSI_font  = CreateFontIndirect(&cur_Font);

//make_message(str(ANSI_font));

//make_message("BEFORE");
		tof;
		while (!at_eof) {
			t_str = get_line;
			if (find_text("AC=",1,0)) {
        if (find_text("/FT=",1,0)) {

          right;
          right;
          right;
          right;
          while (cur_char != "/") {
            del_char;
          }
          text(str(ansi_font));
        }
      }

			down;
			goto_col(1);
		}
		RedrawWindow(GetDlgItem(hdlg, ASCII_List_Ctrl), 0, 0, RDW_INVALIDATE);

	}

//make_message("AFTER");
	goto_mark;
	insert_mode = t_insert_mode;
	switch_win_id(active_window);
	SetFocus(GetDlgItem(hdlg, ASCII_List_Ctrl));
	return_int = 0;
}

int AsciiMsgProc( int &retval, int window, int message, int wparam, int lparam, str parms ) no_break
{
  switch ( message )
  {
    case WM_DESTROY :
      Set_global_str("ASC_CHRT_FONTS",
				"/ANAME=" + g_ANSI_fontname +
				"/ONAME=" + g_OEM_fontname);

			if(OEM_font)
				DeleteObject(OEM_font);
			if(ANSI_font)
				DeleteObject(ANSI_font);
	    retval = 0;

			if (!ASCII_Modeless) {
		  	DlgKill(ASCII_dlg);
			}

// deallocate globals
			ansi_font = 0;
			oem_font = 0;
			ascii_window = 0;
			ascii_hdlg = 0;
			ascii_dlg = 0;
			g_ansi_fontname = "";
			g_oem_fontname = "";
			break;

    case WM_COMMAND :
         switch ( wparam )
         {
				  case DLG_WCMD_INIT :
						ascii_hdlg = window;
						break;

					case 101 :
          case 102 :
						int selection = -1,
								t_win_id = window_id;
						if (!ASCII_Modeless) {
							t_win_id = ASCII_Active_window;
						}
						if (switch_win_id(ascii_window)) {
							selection = c_line - 1;
							delete_window;
						}
						switch_win_id(t_win_id);
						if (wparam == 101) {
							if (selection > -1) {
								text(char(selection));
							}
						}
						if (ASCII_Modeless) {
            	DestroyWindow( window );
              return (1);
						}
         }
  }
  return(DlgMessageProc(retval, window,message,wparam,lparam, parms ));
}