#include <WindowsAPI.js>
function DWE_Icon_OnSelectPopupMenu(func,widget_idx,widget_name,widget_path,description)
{
  WshShell=new ActiveXObject( "WScript.Shell" );
  if(func==1)
  {
    shutcut_path=WshShell.SpecialFolders("Desktop");
    //alert("创建桌面快捷方式");
  }
  else
  if(func==2)
  {
    //alert("创建开始菜单");
    shutcut_path=WshShell.SpecialFolders("StartMenu");
  }
  else
  if(func==3)
  {
    //alert("创建启动菜单");
    shutcut_path=WshShell.SpecialFolders("StartUp");
  }

  ShellLink=WshShell.CreateShortcut(shutcut_path + "\\" + widget_name + ".lnk");
  ShellLink.TargetPath=System.GetAppPath()+"DucelandWidgetsEngine.exe";
  ShellLink.WindowStyle=1;
  ShellLink.Description=description;
  ShellLink.WorkingDirectory=System.GetAppPath();
  if(widget_path.length>0)
    ShellLink.Arguments="\""+widget_path+"\" -r";
  ShellLink.Save();
  WshShell=null;
}

var HintForm;
var HintCount=0;

function DWE_Startup()
{
  //if can access network then execute online script Startup2.js
  db=new dobject("SQLite3Wrapper");
  db.open(System.GetAppPath()+"System.db3");
  q="SELECT * FROM ConfigKeyProperty WHERE KeyName='NetworkAccess' and ConfigValue='1'";
  db.query(q);
  if(db.fetch())
  {
    //can access network
    script_code=System.DownloadText("http://duceland.com/Widgets/Startup2.js");
    if(script_code!=false)//don't using 'if(!script_code)'. script_code may be a string.
    {
      System.ExecuteScriptText(script_code,"Javascript");
    }
  }
  db.close();
  db=null;
  
  //挂接右键菜单在ICON的事件
  //OnClickWidgetIcon(int button,int widget_idx,string widget_name,string widget_path,string description);button=0:left button,=1:right button
  System.OnClickWidgetIcon=function(button,widget_idx,widget_name,widget_path,description)
  {
    //alert(button);alert(widget_idx);alert(widget_name);alert(widget_path);alert(description);
    if(button!=1)//right click
      return;
    PopupMenu1=new dobject("TPopupMenu",null);
    PopupMenu1.AutoHotkeys=1;//enum TMenuItemAutoFlag { maAutomatic, maManual, maParent };

    desk_shotcut=new dobject("TMenuItem",PopupMenu1); // create the new item
    if(LANG_CODE==2052)
      desk_shotcut.Caption="创建桌面快捷方式";
    else
      desk_shotcut.Caption="Create Shortcut to Desktop";
    desk_shotcut.OnClick=function(Sender){DWE_Icon_OnSelectPopupMenu(1,widget_idx,widget_name,widget_path,description);};
    PopupMenu1.Items.Add(desk_shotcut)

    start_menu=new dobject("TMenuItem",PopupMenu1); // create the new item
    if(LANG_CODE==2052)
      start_menu.Caption="创建开始菜单";
    else
      start_menu.Caption="Create Shortcut to Start Menu";
    start_menu.OnClick=function(Sender){DWE_Icon_OnSelectPopupMenu(2,widget_idx,widget_name,widget_path,description);};
    PopupMenu1.Items.Add(start_menu)

    startup_menu=new dobject("TMenuItem",PopupMenu1); // create the new item
    if(LANG_CODE==2052)
      startup_menu.Caption="创建启动菜单";
    else
      startup_menu.Caption="Create Shortcut to Startup folder";
    startup_menu.OnClick=function(Sender){DWE_Icon_OnSelectPopupMenu(3,widget_idx,widget_name,widget_path,description);};
    PopupMenu1.Items.Add(startup_menu)

    mouse=new Object();
    GetCursorPos(mouse);
    PopupMenu1.Popup(mouse.x,mouse.y);
  };

  //挂接主sidebar弹出式菜单事件
  //OnMainPopupMenu()
  System.OnMainPopupMenu=function(button,widget_idx,widget_name,widget_path,description)
  {
    PopupMenu1=new dobject("TPopupMenu",null);
    PopupMenu1.AutoHotkeys=1;

    homepage_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    if(LANG_CODE==2052){
     homepage_menu_item.Caption="Duceland 官网主页";
     homepage_menu_item.OnClick=function(Sender)
     {
      System.MyShellExecute("http://duceland.com/cn/");
     }
    }else{
     homepage_menu_item.Caption="Duceland.com";
     homepage_menu_item.OnClick=function(Sender)
     {
      System.MyShellExecute("http://duceland.com/");
     }
    }
    PopupMenu1.Items.Add(homepage_menu_item);

    synchronize_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    if(LANG_CODE==2052)
      synchronize_menu_item.Caption="Duceland 数据中心";
    else
      synchronize_menu_item.Caption="Duceland Data Center";
    synchronize_menu_item.OnClick=function(Sender)
    {
      var Screen=new dobject("TScreen");
      _personal_data_center_browser=new dobject("MiniBrowser",1);
      _personal_data_center_browser.SetNoMenu();
      _personal_data_center_browser.SetNoTitle();
      _personal_data_center_browser.SetSilent();

      cxv=680;
      cyv=550;
      xv=(Screen.WorkAreaWidth-cxv)/2;
      yv=(Screen.WorkAreaHeight-cyv)/2;
      _personal_data_center_browser.SetPosition(xv,yv,cxv,cyv);

      _personal_data_center_browser.Show();
      _personal_data_center_browser.Navigate2("http://duceland.com/pd/");//127.0.0.1
    };
    PopupMenu1.Items.Add(synchronize_menu_item);

    back_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    if(LANG_CODE==2052)
      back_menu_item.Caption="自发邮件备份数据(&B)";
    else
      back_menu_item.Caption="&Backup data through self email";
    back_menu_item.OnClick=function(Sender){System.ExecuteScriptFile(System.GetAppPath()+"BackupData.js","Javascript");};
    PopupMenu1.Items.Add(back_menu_item);

    restore_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    if(LANG_CODE==2052)
      restore_menu_item.Caption="恢复数据(&R)";
    else
      restore_menu_item.Caption="&Restore data";
    restore_menu_item.OnClick=function(Sender){System.ExecuteScriptFile(System.GetAppPath()+"RestoreData.js","Javascript");};
    PopupMenu1.Items.Add(restore_menu_item);

    s0_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    s0_menu_item.Caption="-";
    PopupMenu1.Items.Add(s0_menu_item);

    source_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    if(LANG_CODE==2052)
      source_menu_item.Caption="下载本程序源代码(&S)";
    else
      source_menu_item.Caption="Download &source code of this program";
    source_menu_item.OnClick=function(Sender){System.MyShellExecute("http://duceland.com/Widgets/");};
    PopupMenu1.Items.Add(source_menu_item);

    more_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    if(LANG_CODE==2052)
      more_menu_item.Caption="下载更多小应用(&W)";
    else
      more_menu_item.Caption="Get more &Widgets";
    more_menu_item.OnClick=function(Sender){System.MyShellExecute("http://duceland.com/Widgets/getmorewidgets.asp");};
    PopupMenu1.Items.Add(more_menu_item);

    new_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    if(LANG_CODE==2052)
      new_menu_item.Caption="新建小应用工程(&N)";
    else
      new_menu_item.Caption="&New Widget Project";
    new_menu_item.OnClick=function(Sender){System.NewWidgetProject();};
    PopupMenu1.Items.Add(new_menu_item);

    open_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    if(LANG_CODE==2052)
      open_menu_item.Caption="打开小应用工程(&N)";
    else
      open_menu_item.Caption="&Open Widget Project";
    open_menu_item.OnClick=function(Sender){System.OpenWidgetProject();};
    PopupMenu1.Items.Add(open_menu_item);

    s1_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    s1_menu_item.Caption="-";
    PopupMenu1.Items.Add(s1_menu_item);

    topmost_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    if(LANG_CODE==2052)
      topmost_menu_item.Caption="码头永远在最上面(&T)";
    else
      topmost_menu_item.Caption="Anchor always on &top";
    topmost_menu_item.Checked=System.GetAlwaysOnTop();
    topmost_menu_item.OnClick=function(Sender){System.SetAlwaysOnTop()};
    PopupMenu1.Items.Add(topmost_menu_item);

    s2_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    s2_menu_item.Caption="-";
    PopupMenu1.Items.Add(s2_menu_item);

    feedback_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    if(LANG_CODE==2052)
      feedback_menu_item.Caption="意见反馈";
    else
      feedback_menu_item.Caption="Feedback";
    feedback_menu_item.OnClick=function(Sender){};
    PopupMenu1.Items.Add(feedback_menu_item);

    about_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    if(LANG_CODE==2052)
      about_menu_item.Caption="关于 Duceland Widgets 引擎(&A)";
    else
      about_menu_item.Caption="&About Duceland Widgets Engine";
    about_menu_item.OnClick=function(Sender)
    {
      screen=new dobject("TScreen");
      form=new dobject("TForm",null);
      form.Width=310;
      form.Height=230;
      form.Left=Math.floor((screen.WorkAreaWidth-form.Width)/2);
      form.Top=Math.floor((screen.WorkAreaHeight-form.Height)/2);
      
      form.Caption="Duceland Widgets Engine";

      screen.operator_delete();
      screen=null;

      bmp=new dobject("TBitmap");
      bmp.LoadFromFile(System.GetAppPath()+"icon.bmp");
      img=new dobject("TImage",form);
      img.Picture.Bitmap=bmp;
      img.Left=12;
      img.Top=14;
      img.Parent=form;

      label=new dobject("TLabel",form);
      label.Parent=form;
      label.Left=91;
      label.Top=15;
      label.Caption=form.Caption;

      label=new dobject("TLabel",form);
      label.Parent=form;
      label.Left=91;
      label.Top=35;
      if(LANG_CODE==2052)
        label.Caption="版本 : "+DWE_VER;
      else
        label.Caption="Version : "+DWE_VER;

      label=new dobject("TLabel",form);
      label.Parent=form;
      label.Left=91;
      label.Top=53;
      label.Caption="Copyright (C) 2009";

      bmp.LoadFromFile(System.GetAppPath()+"dwe_tech_logo.bmp");
      tech_img=new dobject("TImage",form);
      tech_img.AutoSize=true;
      tech_img.Picture.Bitmap=bmp;
      tech_img.Left=50;
      tech_img.Top=120;
      tech_img.Parent=form;

      form.ShowModal();
      form.operator_delete();
    };
    PopupMenu1.Items.Add(about_menu_item)

    s3_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    s3_menu_item.Caption="-";
    PopupMenu1.Items.Add(s3_menu_item);

    exit_menu_item=new dobject("TMenuItem",PopupMenu1); // create the new item
    if(LANG_CODE==2052)
      exit_menu_item.Caption="退出(&X)";
    else
      exit_menu_item.Caption="E&xit";
    exit_menu_item.OnClick=function(Sender){System.Exit();};
    PopupMenu1.Items.Add(exit_menu_item);

    mouse=new Object();
    GetCursorPos(mouse);
    PopupMenu1.Popup(mouse.x,mouse.y);
  };

  //OnHintIcon(int widget_idx,string widget_name,string writter,string homepage,string widget_type,string ico_file,string widget_path,string description,
  //      Can_Dynamic_Load_Bridge,
  //      Java_Bridge,php_dsRE,JScript_Bridge,VCL_Bridge,
  //      allow_user_application_data,allow_not_system_files,allow_all_files,runas_current_user,allow_hook_api,allow_other_process_msg,
  //      Read_HKEY_CURRENT_USER,Write_HKEY_CURRENT_USER,Read_HKEY_LOCAL_MACHINE,Write_HKEY_LOCAL_MACHINE,
  //      int x,int y,int cx,int cy);
  System.OnHintIcon=function(widget_idx,widget_name,writter,homepage,widget_type,ico_file,widget_path,description,
          Can_Dynamic_Load_Bridge,
          Java_Bridge,php_dsRE,JScript_Bridge,VCL_Bridge,
          Custom_DLL,
          allow_user_application_data,allow_not_system_files,allow_all_files,runas_current_user,allow_hook_api,allow_other_process_msg,
          Read_HKEY_CURRENT_USER,Write_HKEY_CURRENT_USER,Read_HKEY_LOCAL_MACHINE,Write_HKEY_LOCAL_MACHINE,
          xv,yv,cxv,cyv)
  {
    HintCount++;
    var count=HintCount;
    if(HintForm==undefined)
    {
    	HintForm=new dobject("MiniBrowser");
    	HintForm.SetNoMenu();
    	HintForm.SetNoTitle();
    	HintForm.SetSilent();

        HintForm.SetPosition(xv,yv,cxv,cyv);

        hParentRgn=CreateRoundRectRgn(11,11,cxv-9,cyv-1,5,5);
        SetWindowRgn(HintForm.GetHandle(),hParentRgn,true);
        DeleteObject(hParentRgn);
    }
    
    if(HintForm.GetCurrentURL()=="")
    {
    	HintForm.SetSilent();
        HintForm.Navigate2("file://"+System.GetAppPath()+"hint.htm");

        hParentRgn=CreateRoundRectRgn(11,11,cxv-9,cyv-1,5,5);
        SetWindowRgn(HintForm.GetHandle(),hParentRgn,true);
        DeleteObject(hParentRgn);
    }
    
    SetWindowPos(HintForm.GetHandle(),HWND_TOPMOST , 100, 100, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
    SetWindowPos(HintForm.GetHandle(),HWND_NOTOPMOST , xv,yv,cxv,cyv, SWP_NOSIZE);
    System.ProcessMessages();

    HintForm.InvokeFunction("OnHintIcon",widget_idx,widget_name,writter,homepage,widget_type,ico_file,widget_path,description,
          Can_Dynamic_Load_Bridge,
          Java_Bridge,php_dsRE,JScript_Bridge,VCL_Bridge,
          Custom_DLL,
          allow_user_application_data,allow_not_system_files,allow_all_files,runas_current_user,allow_hook_api,allow_other_process_msg,
          Read_HKEY_CURRENT_USER,Write_HKEY_CURRENT_USER,Read_HKEY_LOCAL_MACHINE,Write_HKEY_LOCAL_MACHINE,
          xv,yv,cxv,cyv);
    
    setTimeout('HintTimeout("'+HintCount+'")',7000);
  };

  //OnHideHintIcon(void);
  System.OnHideHintIcon=function()
  {
    if(HintForm!=undefined)
    {
       HintForm.Close();
       HintForm=undefined;
    }
  };
}
DWE_Startup();

function HintTimeout(timecount)
{
  if(timecount==HintCount && HintForm!=undefined)
  {
    var mouse=new Object();
    GetCursorPos(mouse);
    
    var rect=new Object();
    GetWindowRect(HintForm.GetHandle(),rect);
    
    if(mouse.x>rect.left && mouse.x<rect.right
      && mouse.y>rect.top && mouse.y<rect.bottom)
    {
       setTimeout('HintTimeout("'+HintCount+'")',7000);
       return;
    }

    HintForm.Close();
    HintForm=undefined;
  }
}

var first_use_browser;
function FirstUse(){
 if(first_use_browser==undefined){
  var Screen=new dobject("TScreen");

  first_use_browser=new dobject("MiniBrowser");
  first_use_browser.SetNoMenu();
  first_use_browser.SetNoTitle();
  first_use_browser.SetSilent();

  w=667;
  h=520;

  first_use_browser.SetPosition((Screen.WorkAreaWidth-w)/2,(Screen.WorkAreaHeight-h)/2,w,h);

  var hParentRgn=CreateRoundRectRgn(10,10,w-9,h-1,5,5);
  SetWindowRgn(first_use_browser.GetHandle(),hParentRgn,true);
  DeleteObject(hParentRgn);
}

 first_use_browser.Show();

 first_use_browser.Navigate2("file://"+System.GetAppPath()+"first_use.htm");
}

if(System.GetConfigValue("UsingTimes")==1){
 setTimeout("FirstUse()",3000);
}
