#include "stdafx.h"
#include "Global.h"
#include "MainForm.h"
#include <windows.h>

#include "NPCForm.h"
#include "AddPowerForm.h"
#include "ArmorForm.h"
#include "BodyObjForm.h"
#include "ItemForm.h"
#include "PhantomForm.h"
#include "KeyItemForm.h"
#include "MagicBaseForm.h"
#include "MagicCollectForm.h"
#include "PlayerForm.h"
#include "RuneForm.h"
#include "TitleForm.h"
#include "WeaponForm.h"
#include "QuestDetailForm.h"
#include "TableForm.h"
#include "QuestNPCForm.h"
#include "MineForm.h"
#include "ImageForm.h"
#include "FlagObjForm.h"
#include "RecipeForm.h"
#include "ScriptForm.h"
#include "LearnMagicForm.h"
#include "ShopForm.h"
#include "SuitForm.h"
#include "StringForm.h"
#include "CampForm.h"
#include "TreasureForm.h"
#include "SelectAbilityForm.h"
#include "RefineForm.h"
#include "ZoneForm.h"
#include "CreateRoleForm.h"
#include "CardForm.h"
#include "CreatePasswordForm.h"
#include "ShowDataForm.h"
#include "BatchUpdateForm.h"
#include "DataExportForm.h"
#include "PEForm.h"

char g_szCmd[1024];

using namespace System::Diagnostics;
using namespace ObjDataEdit;
//--------------------------------------------------------------------------------------------------------------------
int APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
{
//	char	FileName[512];
//	GetModuleFileName( hInstance , FileName , 512 );

	bool _NormalMode = true;

	String* cmdLine = new String(lpCmdLine);

	wchar_t SplitToken __gc [] = {' '};

	String* lines[] = cmdLine->Split(SplitToken, StringSplitOptions::RemoveEmptyEntries);

	if (lines->Length >= 1)
	{
		String* _Cmd = lines[0]->ToLower();

		if (String::Equals(_Cmd, "-strout"))
		{
			//---------------------------------------
			//Set IniPath
			char    szBuff[512];
			GetCurrentDirectory(sizeof(szBuff), szBuff);

			string strCurDir = szBuff;

			sprintf(szBuff, "%s\\ObjectDataEdit.ini", strCurDir.c_str());

			Global::IniRootPath = szBuff;
			//---------------------------------------
			//create new string from instance
			StringForm* pForm = new StringForm(  "String" , "" , "String" , true );

			//output string data
			pForm->_OutputFileLanguage();
			_NormalMode = false;
		}
	}

	if (_NormalMode)
	{
		strcpy( g_szCmd, lpCmdLine );
		System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
		Application::Run( new MainForm() );
	}

    return 0;
}
//--------------------------------------------------------------------------------------------------------------------
System::Void MainForm::FormExit(System::Object *  sender, System::EventArgs *  e)
{
    this->Close();;
}
//--------------------------------------------------------------------------------------------------------------------
System::Void MainForm::OnLogin_Click(System::Object *  sender, System::EventArgs *  e)
{
    if( _IsLogin == true )
        return;

    LoginForm* Login;
    Login = new LoginForm();
    Login->ShowDialog();
    if( Login->IsAccept() == false )
        return;

    _Account  = Login->Account( );
    _Password = Login->Password( );

    sqlConnection1->Open();

    sqlCommand1->CommandText = String::Concat( "Select Account_Id , Password from AccountDB Where Account_Id = '" , _Account ,"'") ;
    SqlDataReader* myReader = sqlCommand1->ExecuteReader();

    if( myReader->Read() == false )
    {
        MessageBox::Show( "帳號錯誤" , "警告" );
        goto End;
    }

    if( String::Compare( myReader->GetString(1) , _Password ) != 0 )
    {               
        MessageBox::Show( "密碼錯誤" , "警告" );
        goto End;
    }
    //---------------------------------------------------------------------------            
//    this->Text = String::Concat( "物件編輯 " , _DEF_VERSION , " User:[" , _Account , "]");
	//this->Text = String::Format( "物件編輯 {0} User:[{1}] DS: {2} DB: {3}", (String*)_DEF_VERSION  , _Account, Global::DBSource.c_str(), Global::DataBase.c_str() );

	//String sDataSource		= String(  Global::DBSource.c_str() );
	//String sDataBase		= String(  Global::DataBase.c_str() );

	//String* sDataSource = new String( _DEF_VERSION );
	//String* sDataSource = new String( Global::DBSource.data() );
	//String* sDataBase = new String( Global::DataBase.data() );
	//String* sAccount	= new String ( _Account );
	//std::string sAccount	= 

	ProcessModule* currentModule= Process::GetCurrentProcess()->MainModule;

	char szBuff[1024];
	sprintf( szBuff, "Objeditor %s, User:[ %s ], DS:[ %s ], DB:[ %s ], FileVersion:[ %s ]", _DEF_VERSION, DotNetStr::StringToChar( _Account ).c_str() , Global::DBSource.c_str(), Global::DataBase.c_str(), DotNetStr::StringToChar(currentModule->FileVersionInfo->FileVersion).c_str());

	String* sText		= new String( szBuff );


	//this->Text = String::Format( "{0} User:[{1}] DS: {2} DB: {3}", (String*)_DEF_VERSION, _Account, sDataSource, sDataBase );
	this->Text = sText;
    menuItem_Login->Enabled = false;
    panel_SelectObj->Enabled = true;
    _IsLogin = true;


	if( strcmpi( Global::DataBase.c_str(), "ObjectData" ) == 0 )
	{
		menuItem12->Enabled = false;		
	}
    BaseFormControl::SetAccount( _Account );
    //---------------------------------------------------------------------------        
    InsertForm();
End:                 
    myReader->Close();
    sqlConnection1->Close();
}
//--------------------------------------------------------------------------------------------------------------------
System::Void MainForm::OnLogout_Click(System::Object *  sender, System::EventArgs *  e)
{
    menuItem_Login->Enabled = true;
    _IsLogin = false;
    panel_SelectObj->Enabled = false;


    CB_SelectForm->Text = " ";
    this->Text = String::Concat( "物件編輯 " , _DEF_VERSION );
    for( int i = 0 ; i < _FormList->Count ; i++ )
    {
        System::Object* AddItem[] = new System::Object*[1];
        BaseFormControl* AddForm = ( __try_cast<BaseFormControl*> (_FormList->Item[i]  ) );
        AddForm->Close();                

    }           

    _FormList->Clear();

    MI_MaintainForm->Enabled = false;
}
//--------------------------------------------------------------------------------------------------------------------
/*
void    MainForm::LogoutClear( )
{
    CB_SelectForm->Text = " ";
    this->Text = "物件編輯";

    for( int i = 0 ; i < _FormList->Count ; i++ )
    {
        System::Object* AddItem[] = new System::Object*[1];
        BaseFormControl* AddForm = ( __try_cast<BaseFormControl*> (_FormList->Item[i]  ) );
        AddForm->Close();                

    }           

    _FormList->Clear();
}
*/
//--------------------------------------------------------------------------------------------------------------------
System::Void MainForm::OnSelectForm(System::Object *  sender, System::EventArgs *  e)
{
	static bool	IsFirstTime = true;
    ComboBox*   CBox = static_cast<ComboBox*>( sender );
    int         SelectID = CBox->SelectedIndex;
    if( SelectID == -1 )
    {
        _ActiveForm = NULL;
        return;
    }
    BaseFormControl* SelectForm  = ( __try_cast<BaseFormControl*> (_FormList->Item[ SelectID ] ) );

    if( _ActiveForm != SelectForm )
    {
        _ActiveForm = SelectForm;
		if( _ActiveForm->Visible == false )
		{
			if( IsFirstTime == true )
			{
				IsFirstTime = false;
				_ActiveForm->WindowState = FormWindowState::Maximized;
			}
			_ActiveForm->Show();		
		}
        _ActiveForm->Focus();
    }          

}
//--------------------------------------------------------------------------------------------------------------------
System::Void MainForm::OnBaseForm_Deactivate(System::Object *  sender, System::EventArgs *  e)
{

	BaseFormControl* SelectForm  = ( static_cast<BaseForm*> ( sender ) );

	if( _ActiveForm == SelectForm )
	{
		CB_SelectForm->Text = "";
		_ActiveForm = NULL;
	}
}
//--------------------------------------------------------------------------------------------------------------------
System::Void MainForm::OnBaseForm_Active(System::Object *  sender, System::EventArgs *  e)
{

    BaseFormControl* SelectForm  = ( static_cast<BaseForm*> ( sender ) );
    _ActiveForm = SelectForm;

    CB_SelectForm->Text = SelectForm->FormName();
//    ViewForm::g_ViewForm->AddOwnedForm( this );
//    ViewForm::g_ViewForm->Parent = this;
}
//--------------------------------------------------------------------------------------------------------------------
void    MainForm::InsertForm( )
{
    if( Global::AccountInfo.NPC != EM_AccountState_None )
        _FormList->Add( new NPCForm         (  "NPC"        , "NPCObjectDB"         , "NPCObject" 
                                            , (Global::AccountInfo.NPC == EM_AccountState_Read ) 
                                            , EM_SearchFormTypeENUM_NPC) );

    if( Global::AccountInfo.Weapon != EM_AccountState_None )    
        _FormList->Add( new WeaponForm      (  "武器"       , "WeaponObjectDB"      , "WeaponObject" 
                                            , (Global::AccountInfo.Weapon == EM_AccountState_Read ) 
                                            , EM_SearchFormTypeENUM_Weapon ) );

    if( Global::AccountInfo.Armor != EM_AccountState_None )
        _FormList->Add( new ArmorForm       (  "防具"       , "ArmorObjectDB"       , "ArmorObject" 
                                            , (Global::AccountInfo.Armor == EM_AccountState_Read ) 
                                            , EM_SearchFormTypeENUM_Armor) );

    if( Global::AccountInfo.Item != EM_AccountState_None )
        _FormList->Add( new ItemForm        (  "物品"       , "ItemObjectDB"        , "ItemObject" 
                                            , (Global::AccountInfo.Item == EM_AccountState_Read ) 
                                            , EM_SearchFormTypeENUM_Item) );

    if( Global::AccountInfo.Rune != EM_AccountState_None )
        _FormList->Add( new RuneForm        (  "符文"       , "RuneObjectDB"        , "RuneObject" 
                                            , (Global::AccountInfo.Rune == EM_AccountState_Read )
											, EM_SearchFormTypeENUM_Rune ) );


	if( Global::AccountInfo.Item != EM_AccountState_None )
		_FormList->Add( new PhantomForm     (  "幻靈"       , "PhantomObjectDB"        , "PhantomObject" 
									, (Global::AccountInfo.Item == EM_AccountState_Read ) 
									, EM_SearchFormTypeENUM_Phantom) );

    if( Global::AccountInfo.KeyItem != EM_AccountState_None )
        _FormList->Add( new KeyItemForm     (  "重要物品"   , "KeyItemObjectDB"     , "KeyItemObject"
                                            , (Global::AccountInfo.KeyItem == EM_AccountState_Read ) 
                                            , EM_SearchFormTypeENUM_KeyItem ) );

    if( Global::AccountInfo.Magic != EM_AccountState_None )
        _FormList->Add( new MagicBaseForm   (  "基本法術"       , "MagicObjectDB"       , "MagicObject"
                                            , (Global::AccountInfo.Magic == EM_AccountState_Read ) 
                                            , EM_SearchFormTypeENUM_Magic) );

	if( Global::AccountInfo.MagicCollect != EM_AccountState_None )
		_FormList->Add( new MagicCollectForm   (  "法術集合"       , "MagicCollectObjectDB"       , "MagicCollectObject"
											, (Global::AccountInfo.MagicCollect == EM_AccountState_Read ) 
											, EM_SearchFormTypeENUM_MagicCollect) );

    if( Global::AccountInfo.AddPower != EM_AccountState_None )
        _FormList->Add( new AddPowerForm    (  "附加能力"   , "AddPowerObjectDB"    , "AddPowerObject" 
                                            , (Global::AccountInfo.AddPower == EM_AccountState_Read ) 
                                            , EM_SearchFormTypeENUM_Attribute ) );

    if( Global::AccountInfo.BodyObj != EM_AccountState_None )    
        _FormList->Add( new BodyObjForm     (  "身體物件"   , "BodyObjectDB"        , "BodyObject" 
                                            , (Global::AccountInfo.BodyObj == EM_AccountState_Read )
                                            , EM_SearchFormTypeENUM_BodyObj ) );

    if( Global::AccountInfo.Player != EM_AccountState_None )
        _FormList->Add( new PlayerForm      (  "玩家角色"   , "PlayerObjectDB"      , "PlayerObject" 
                                            , (Global::AccountInfo.Player == EM_AccountState_Read )
                                            , EM_SearchFormTypeENUM_Player ) );

    if( Global::AccountInfo.Title != EM_AccountState_None )
        _FormList->Add( new TitleForm       (  "頭銜"       , "TitleObjectDB"       , "TitleObject"
                                            , (Global::AccountInfo.Title == EM_AccountState_Read ) 
                                            , EM_SearchFormTypeENUM_Title ) );

    if( Global::AccountInfo.Mine != EM_AccountState_None )
        _FormList->Add( new MineForm        (  "礦產"       , "MineObjectDB"        , "MineObject" 
                                            , (Global::AccountInfo.Mine == EM_AccountState_Read ) 
                                            , EM_SearchFormTypeENUM_Mine) );

    if( Global::AccountInfo.QuestNPC != EM_AccountState_None )
        _FormList->Add( new QuestNPCForm    (  "任務NPC"    , "QuestNPCObjectDB"    , "QuestNPCObject" 
                                            , (Global::AccountInfo.QuestNPC == EM_AccountState_Read ) 
                                            , EM_SearchFormTypeENUM_QuestObj ) );

//    if( Global::AccountInfo.QuestCollect != EM_AccountState_None )
//        _FormList->Add( new QuestCollectForm(  "QuestCollect", "QuestCollectObjectDB" , "QuestCollectObject.DB" 
//                                            , (Global::AccountInfo.QuestCollect == EM_AccountState_Read )
//                                            , EM_SearchFormTypeENUM_Unknow ) );

    if( Global::AccountInfo.QuestDetail != EM_AccountState_None )
        _FormList->Add( new QuestDetailForm (  "QuestDetail", "QuestDetailObjectDB" , "QuestDetailObject" 
                                            , (Global::AccountInfo.QuestDetail == EM_AccountState_Read )
                                            , EM_SearchFormTypeENUM_Quest ) );

    if( Global::AccountInfo.Image != EM_AccountState_None )
        _FormList->Add( new ImageForm       (  "圖形", "ImageObjectDB" , "ImageObject" 
                                            , (Global::AccountInfo.Image == EM_AccountState_Read )
                                            , EM_SearchFormTypeENUM_Image ) );

    if( Global::AccountInfo.Flag != EM_AccountState_None )
        _FormList->Add( new FlagObjForm     (  "顯像旗子", "FlagObjectDB" , "FlagObject" 
                                            , (Global::AccountInfo.Flag == EM_AccountState_Read )
                                            , EM_SearchFormTypeENUM_Flag ) );

    if( Global::AccountInfo.LearnMagic  != EM_AccountState_None )
        _FormList->Add( new LearnMagicForm (  "職業技能列表", "LearnMagicExDB" , "LearnMagic" 
                                            , (Global::AccountInfo.LearnMagic == EM_AccountState_Read )
                                            , EM_SearchFormTypeENUM_LearnMagic ) );


    if( Global::AccountInfo.Recipe      != EM_AccountState_None )
        _FormList->Add( new RecipeForm     (  "配方表", "RecipeObjectDB" , "RecipeObject" 
                                            , (Global::AccountInfo.Recipe == EM_AccountState_Read )
                                            , EM_SearchFormTypeENUM_Recipe ) );

    if( Global::AccountInfo.Shop        != EM_AccountState_None )
        _FormList->Add( new ShopForm     (  "商店", "ShopObjectDB" , "ShopObject" 
                                            , (Global::AccountInfo.Shop == EM_AccountState_Read )
                                            , EM_SearchFormTypeENUM_Shop ) );

    if( Global::AccountInfo.Suit        != EM_AccountState_None )
        _FormList->Add( new SuitForm     (  "套裝", "SuitObjectDB" , "SuitObject" 
                                            , (Global::AccountInfo.Suit == EM_AccountState_Read )
                                            , EM_SearchFormTypeENUM_Suit ) );

    if( Global::AccountInfo.Script      != EM_AccountState_None )
        _FormList->Add( new ScriptForm     (  "Server 端 Script", "ScriptObjectDB" , "LUAScript" 
                                            , (Global::AccountInfo.Script == EM_AccountState_Read )
                                            , EM_SearchFormTypeENUM_SrvScript ) );

    if( Global::AccountInfo.Script      != EM_AccountState_None )
        _FormList->Add( new ScriptForm     (  "Client 端 Script", "CliScriptObjectDB" , "CliLUAScript" 
                                            , (Global::AccountInfo.Script == EM_AccountState_Read )
                                            , EM_SearchFormTypeENUM_CliScript ) );

	if( Global::AccountInfo.Camp      != EM_AccountState_None )
		_FormList->Add( new CampForm     (  "陣營", "CampObjectDB" , "CampObject" 
											,  (Global::AccountInfo.Camp == EM_AccountState_Read )
											, EM_SearchFormTypeENUM_Camp ) );

	if( Global::AccountInfo.Treasure   != EM_AccountState_None )
		_FormList->Add( new TreasureForm (  "掉寶", "TreasureObjectDB" , "TreasureObject" 
											,  (Global::AccountInfo.Treasure == EM_AccountState_Read )
											, EM_SearchFormTypeENUM_Treasure ) );

	if( Global::AccountInfo.EqRefineAbility   != EM_AccountState_None )
		_FormList->Add( new RefineTableForm (  "衝等表", "EqRefineAbilityObjectDB" , "EqRefineAbilityObject" 
											,  (Global::AccountInfo.EqRefineAbility == EM_AccountState_Read )
											, EM_SearchFormTypeENUM_EqRefineAbility ) );

	if( Global::AccountInfo.Zone   != EM_AccountState_None )
		_FormList->Add( new ZoneForm		(  "區域資訊", "ZoneObjectDB" , "ZoneObject" 
											,  (Global::AccountInfo.Zone == EM_AccountState_Read )
											, EM_SearchFormTypeENUM_Zone ) );

	if( Global::AccountInfo.CreateRole   != EM_AccountState_None )
		_FormList->Add( new CreateRoleForm		(  "建角資訊", "CreateRoleObjectDB" , "CreateRoleObject" 
											,  (Global::AccountInfo.CreateRole == EM_AccountState_Read )
											, EM_SearchFormTypeENUM_CreateRole ) );

	if( Global::AccountInfo.Card   != EM_AccountState_None )
		_FormList->Add( new CardForm		(  "卡片", "CardObjectDB" , "CardObject" 
											,  (Global::AccountInfo.Card == EM_AccountState_Read )
											, EM_SearchFormTypeENUM_Card ) );

	if( Global::AccountInfo.PE   != EM_AccountState_None )
		_FormList->Add( new PEForm		(  "公眾任務", "PEObjectDB" , "PEObject" 
										,  (Global::AccountInfo.PE == EM_AccountState_Read )
										, EM_SearchFormTypeENUM_PE ) );


    if( Global::AccountInfo.ValueTable != EM_AccountState_None )
        _FormList->Add( new TableForm       (  "Table" , "" , "" , true ) );

	if( Global::AccountInfo.String != EM_AccountState_None )
		_FormList->Add( new StringForm       (  "String" , "" , "String" , true ) );


    CB_SelectForm->Items->Clear();
    CB_SelectForm->SelectedIndex = -1;
    for( int i = 0 ; i < _FormList->Count ; i++ )
    {
        System::Object* AddItem[] = new System::Object*[1];
        BaseFormControl* AddForm = ( __try_cast<BaseFormControl*> (_FormList->Item[i]  ) );
        String* Str = AddForm->FormName();                

        AddItem[0]  = Str; 
        CB_SelectForm->Items->AddRange(AddItem);
        AddForm->MdiParent = this;
        AddForm->Deactivate += new System::EventHandler(this, &ObjDataEdit::MainForm::OnBaseForm_Deactivate );     
        AddForm->Activated  += new System::EventHandler(this, &ObjDataEdit::MainForm::OnBaseForm_Active );
    }           

    if( Global::AccountInfo.Admin == EM_AccountState_Write )
    {
        MI_MaintainForm->Enabled = true;
    }
    else
    {
        MI_MaintainForm->Enabled = false;
    }
}

void    MainForm::LoadString( )
{
	sqlConnection1->ConnectionString = Global::GetDBSource();
    sqlConnection1->Open();

    sqlCommand1->CommandText = "Select ID , Name from FightArg where Type = 0";
    SqlDataReader* myReader = sqlCommand1->ExecuteReader();
    while( myReader->Read())
    {
        String* Str =  myReader->GetString( 1 ) ;
        Global::RaceName.push_back( DotNetStr::StringToChar( Str ) );
    }
    myReader->Close();

    sqlCommand1->CommandText = "Select ID , Name from FightArg where Type = 1";
    myReader = sqlCommand1->ExecuteReader();
    while( myReader->Read())
    {
        String* Str =  myReader->GetString( 1 ) ;
        Global::JobName.push_back( DotNetStr::StringToChar( Str ) );
    }
    myReader->Close();

    sqlCommand1->CommandText = "Select ID , Name from FightArg where Type = 2";
    myReader = sqlCommand1->ExecuteReader();
    while( myReader->Read())
    {
        String* Str =  myReader->GetString( 1 ) ;
        Global::SexName.push_back( DotNetStr::StringToChar( Str ) );
    }
    myReader->Close();

    sqlCommand1->CommandText = "Select GUID , Name from Table_ZoneDB";
    myReader = sqlCommand1->ExecuteReader();


    String* Str = "0 無";
    Global::ZoneName.push_back( DotNetStr::StringToChar( Str ) );
    while( myReader->Read() )
    {                
        int     Value =  myReader->GetInt32( 0 );
        String* Str   =  myReader->GetString( 1 );

        Str = String::Format( "{0} {1}", __box( Value ) , Str );
        Global::ZoneName.push_back( DotNetStr::StringToChar( Str ) );
    }
    myReader->Close();


    sqlCommand1->CommandText = "Select ID , Name from Table_String Where Type = '參考點' ORDER BY  ID";
    myReader = sqlCommand1->ExecuteReader();

    while( myReader->Read() )
    {                
        int     Value =  myReader->GetInt32( 0 );
        String* Str   =  myReader->GetString( 1 );

        Global::PointEnumName.push_back( DotNetStr::StringToChar( Str ) );
    }
    myReader->Close();
   

    sqlCommand1->CommandText = "Select ID , Name from Table_String Where Type = '動作' ORDER BY  ID";
    myReader = sqlCommand1->ExecuteReader();

    while( myReader->Read() )
    {                
        int     Value =  myReader->GetInt32( 0 );
        String* Str   =  myReader->GetString( 1 );

        Global::MotionEnumName.push_back( DotNetStr::StringToChar( Str ) );
    }
    myReader->Close();

    sqlConnection1->Close();

}

/*
System::Void MainForm::OutputAllString(System::Object *  sender, System::EventArgs *  e)
{
	progressBar1->Visible = true;
	progressBar1->Maximum = _FormList->Count;
	progressBar1->Value = 0;
	for( int i = 0 ; i < _FormList->Count ; i++ )
	{                 
		BaseFormControl* AddForm = ( __try_cast<BaseFormControl*> (_FormList->Item[i]  ) );
		AddForm->OutputStringToDB();
		progressBar1->Value = i + 1;
	}

	MessageBox::Show( "字串輸出到DB完成", "訊息" );
	progressBar1->Visible = false;
}
*/
/*
System::Void MainForm::OutputAllObjByStringTable(System::Object *  sender, System::EventArgs *  e)
{
	progressBar1->Visible = true;
	progressBar1->Maximum = _FormList->Count;
	progressBar1->Value = 0;
	for( int i = 0 ; i < _FormList->Count ; i++ )
	{                 
		BaseFormControl* AddForm = ( __try_cast<BaseFormControl*> (_FormList->Item[i]  ) );
		//((BaseForm*)AddForm)->InitTable();
		AddForm->InitTable();
		AddForm->OutputFileByStringTable();
		progressBar1->Value = i + 1;
	}

	MessageBox::Show( "由String Table輸出物件完成", "訊息" );
	progressBar1->Visible = false;
}
*/
void	MainForm::OutputFileLanguage( int LanguageID )
{
	progressBar1->Visible = true;
	progressBar1->Maximum = _FormList->Count;
	progressBar1->Value = 0;

	bool bGetQuestLink = false;

	//BaseForm::GetQuestLink();

	for( int i = 0 ; i < _FormList->Count ; i++ )
	{                 
		BaseFormControl* AddForm = ( __try_cast<BaseFormControl*> (_FormList->Item[i]  ) );

		if( bGetQuestLink == false && AddForm->GetQuestLink() == true )
		{
			bGetQuestLink = true;
		}

		AddForm->InitTable();
		AddForm->OutputFileLanguage( LanguageID );		

		progressBar1->Value = i + 1;
	}

	MessageBox::Show( "輸出物件完成", "訊息" );
	progressBar1->Visible = false;
}
System::Void MainForm::OutputAllFileDesign(System::Object*  sender, System::EventArgs*  e)
{
	OutputFileLanguage( -2 );
}
System::Void MainForm::OutputAllFile1(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 0 );
}
System::Void MainForm::OutputAllFile2(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 1 );
}
System::Void MainForm::OutputAllFile3(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 2 );
}
System::Void MainForm::OutputAllFile4(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 3 );
}
System::Void MainForm::OutputAllFile5(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 4 );
}
System::Void MainForm::OutputAllFile6(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 5 );
}
System::Void MainForm::OutputAllFile7(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 6 );
}
System::Void MainForm::OutputAllFile8(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 7 );
}
System::Void MainForm::OutputAllFile9(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 8 );
}
System::Void MainForm::OutputAllFile10(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 9 );
}
System::Void MainForm::OutputAllFile11(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 10 );
}
System::Void MainForm::OutputAllFile12(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 11 );
}
System::Void MainForm::OutputAllFile13(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 12 );
}
System::Void MainForm::OutputAllFile14(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 13 );
}
System::Void MainForm::OutputAllFile15(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 14 );
}
System::Void MainForm::OutputAllFile16(System::Object *  sender, System::EventArgs *  e)
{
	OutputFileLanguage( 15 );
}

System::Void MainForm::OutputAllFile_AllLanguage(System::Object *  sender, System::EventArgs *  e)
{
	progressBar1->Visible = true;
	progressBar1->Maximum = _FormList->Count;
	progressBar1->Value = 0;
	/*
	for( int i = 0 ; i < _FormList->Count ; i++ )
	{                 
		BaseFormControl* AddForm = ( __try_cast<BaseFormControl*> (_FormList->Item[i]  ) );

		AddForm->InitTable();

		for( int i=0; i<7;i++)
		{
			AddForm->OutputFileLanguage( i );		
		}

		progressBar1->Value = i + 1;
	}
	*/


	bool bGetQuestLink = false;

	for( int i = 0 ; i < _FormList->Count ; i++ )
	{                 
		BaseFormControl* AddForm = ( __try_cast<BaseFormControl*> (_FormList->Item[i]  ) );

		if( bGetQuestLink == false && AddForm->GetQuestLink() == true )
		{
			bGetQuestLink = true;
		}

		AddForm->InitTable();
		AddForm->OutputFileLanguage( -1 );		

		progressBar1->Value = i + 1;
	}

	MessageBox::Show( "輸出物件完成", "訊息" );
	progressBar1->Visible = false;

}

System::Void MainForm::Setting_Click(System::Object *  sender, System::EventArgs *  e)
{
	SetForm* Set;
	Set = new SetForm();
	Set->ShowDialog();
}

System::Void MainForm::menuItem12_Popup(System::Object*  sender, System::EventArgs*  e)
{
	int a = 3;
	//string sInput = Microsoft.VisualBasic.Interaction.InputBox("Hello", "Enter something fun", "Type something"); 

}

System::Void MainForm::menuItem22_Click(System::Object *  sender, System::EventArgs *  e)
{
	sqlConnection1->Open();
}

System::Void MainForm::menuItem12_Click(System::Object *  sender, System::EventArgs *  e)
{
	sqlConnection1->Open();

	SqlDataReader* myReader;
	// 同步資料庫
	//sqlCommand1->CommandText = "BACKUP DATABASE [ObjectData] TO  DISK = N'D:\\DatabaseBackup\\ObjectData_backup_Exchange.bak' WITH NOFORMAT, INIT,  NAME = N'ObjectData-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10;";
	//myReader = sqlCommand1->ExecuteReader();
	//myReader->Close();

	bool bExist = true;

	sqlCommand1->CommandText = "SELECT NAME FROM SYS.DATABASES WHERE NAME = N'ObjectData_Test1'";
	myReader = sqlCommand1->ExecuteReader();

	if( myReader->Read() == false )
	{
		bExist = false;
	}
	myReader->Close();

	if( bExist == true )
	{
		sqlCommand1->CommandText = "EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'ObjectData_Test1'";
		myReader = sqlCommand1->ExecuteReader();
		myReader->Close();


		sqlCommand1->CommandText = "ALTER DATABASE [ObjectData_Test1] SET  SINGLE_USER WITH ROLLBACK IMMEDIATE";
		myReader = sqlCommand1->ExecuteReader();
		myReader->Close();


		sqlCommand1->CommandText = "ALTER DATABASE [ObjectData_Test1] SET  SINGLE_USER";
		myReader = sqlCommand1->ExecuteReader();
		myReader->Close();


		sqlCommand1->CommandText = "DROP DATABASE [ObjectData_Test1]";
		myReader = sqlCommand1->ExecuteReader();
		myReader->Close();
	}


	sqlCommand1->CommandText = "RESTORE DATABASE [ObjectData_Test1] FROM  DISK = 'D:\\DatabaseBackup\\ObjectData_backup_Exchange.bak' WITH  FILE = 1,  MOVE N'ObjectData_Data' TO N'D:\\DataBase\\ObjectData_Test1Data.MDF',  MOVE N'ObjectData_Log' TO N'D:\\DataBase\\ObjectData_Test1Data.LDF',  NOUNLOAD,  REPLACE,  STATS = 10;";
	myReader = sqlCommand1->ExecuteReader();
	myReader->Close();


	sqlCommand1->CommandText = "DBCC SHRINKDATABASE(N'ObjectData_Test1' )";
	myReader = sqlCommand1->ExecuteReader();
	myReader->Close();


	sqlCommand1->CommandText = "USE [ObjectData_Test1]";
	myReader = sqlCommand1->ExecuteReader();
	myReader->Close();


	sqlCommand1->CommandText = "DROP USER [hsiangren]";
	myReader = sqlCommand1->ExecuteReader();
	myReader->Close();


	sqlCommand1->CommandText = "CREATE USER [hsiangren] FOR LOGIN [hsiangren]";
	myReader = sqlCommand1->ExecuteReader();
	myReader->Close();


	sqlCommand1->CommandText = "EXEC sp_addrolemember N'db_owner', N'hsiangren'";
	myReader = sqlCommand1->ExecuteReader();
	myReader->Close();

	sqlConnection1->Close();

	MessageBox::Show( "同步測試資料庫成功", "訊息" );
}
System::Void MainForm::MainForm_Closing(System::Object *  sender, System::ComponentModel::CancelEventArgs *  e)
{

	BaseFormControl::IsExit( true );
	for( int i = 0 ; i < _FormList->Count ; i++ )
	{                 
		BaseFormControl* AddForm = ( __try_cast<BaseFormControl*> (_FormList->Item[i]  ) );
		AddForm->Close();
	}
	e->Cancel = false;

}
System::Void MainForm::Maintain_Click(System::Object *  sender, System::EventArgs *  e)
{
	MaintainForm* Maintain;
	Maintain = new MaintainForm();
	Maintain->ShowDialog();
}
System::Void MainForm::MainForm_Load(System::Object *  sender, System::EventArgs *  e)
{
	this->Text = String::Concat( "物件編輯 " , _DEF_VERSION );

	//--------------------------------------------------------------------------------------
	//設定Ini檔案錄徑
	char    Buf[256];	
	char	Path[256];
	GetCurrentDirectory(sizeof(Buf),Buf);
	sprintf( Path , "%s\\ObjectDataEdit.ini", Buf );	
	Global::IniRootPath = Path;

	sqlConnection1->ConnectionString = Global::GetDBSource();
	//--------------------------------------------------------------------------------------


	LoadString( );

	BaseForm::Init();
	SearchObjForm::Init();
	ViewForm::Init( );
	BaseFormControl::IsExit( false );
	SelectAbilityForm::Init( );
	ShowDataForm::Init( );
	BatchUpdateForm::Init();
	DataExportForm::Init( );

	ViewForm::g_ViewForm->SetDockPanel( ViewPanelScreen , ViewPanelControl );
	//            SearchObjForm::g_SearchObjForm->ShowDialogBox( EM_SearchFormTypeENUM_Item );           



	if( strlen( g_szCmd ) != 0 )
	{
		StringForm* pStringform = new StringForm(  "String" , "" , "String" , true );
		pStringform->OutputFileLanguage( 1 );
	}


	OnLogin_Click( NULL , NULL );
}

System::Void MainForm::SetCloseViewForm(System::Object *  sender, DevExpress::XtraBars::Docking::DockPanelCancelEventArgs *  e)
{
 	e->Cancel = true;
 	dockPanel1->Hide();
 	dockPanel2->Hide();
 	MI_Preview->Checked = false;
}

System::Void MainForm::MI_Preview_Click(System::Object *  sender, System::EventArgs *  e)
{
 	if( MI_Preview->Checked == true )
 	{
		dockPanel1->Hide();
		dockPanel2->Hide();
		MI_Preview->Checked = false;
 	}
 	else
 	{
		dockPanel1->Show();
		dockPanel2->Show();
		MI_Preview->Checked = true;
 	}
}

System::Void MainForm::CreatePasswordClick(System::Object*  sender, System::EventArgs*  e)
{
	CreatePasswordForm* form = new CreatePasswordForm;
	form->ShowDialog();
}