//****************************************************************************
//
// Copyright (c) ALTAP, spol. s r.o. All rights reserved.
//
// This is a part of the Altap Salamander SDK library.
//
// The SDK is provided "AS IS" and without warranty of any kind and 
// ALTAP EXPRESSLY DISCLAIMS ALL WARRANTIES, EXPRESS AND IMPLIED, INCLUDING,
// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE and NON-INFRINGEMENT.
//
//****************************************************************************

#include "precomp.h"

// objekt interfacu pluginu, jeho metody se volaji ze Salamandera
CPluginInterface PluginInterface;
// dalsi casti interfacu CPluginInterface
CPluginInterfaceForMenuExt InterfaceForMenuExt;

// globalni data
const char *PluginNameEN = "DemoMenu";          // neprekladane jmeno pluginu, pouziti pred loadem jazykoveho modulu + pro debug veci
const char *PluginNameShort = "DEMOMENU";       // jmeno pluginu (kratce, bez mezer)

HINSTANCE DLLInstance = NULL;       // handle k SPL-ku - jazykove nezavisle resourcy
HINSTANCE HLanguage = NULL;         // handle k SLG-cku - jazykove zavisle resourcy

// obecne rozhrani Salamandera - platne od startu az do ukonceni pluginu
CSalamanderGeneralAbstract *SalamanderGeneral = NULL;

// definice promenne pro "dbg.h"
CSalamanderDebugAbstract *SalamanderDebug = NULL;

// definice promenne pro "spl_com.h"
int SalamanderVersion = 0;

// rozhrani poskytujici upravene Windows controly pouzivane v Salamanderovi
//CSalamanderGUIAbstract *SalamanderGUI = NULL;

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
  if (fdwReason == DLL_PROCESS_ATTACH)
  {
    DLLInstance = hinstDLL;

    INITCOMMONCONTROLSEX initCtrls;
    initCtrls.dwSize = sizeof(INITCOMMONCONTROLSEX);
    initCtrls.dwICC = ICC_BAR_CLASSES;
    if (!InitCommonControlsEx(&initCtrls))
    {
      MessageBox(NULL, "InitCommonControlsEx failed!", "Error", MB_OK | MB_ICONERROR);
      return FALSE;  // DLL won't start
    }
  }

  return TRUE;    // DLL can be loaded
}

// ****************************************************************************

char *LoadStr(int resID)
{
  return SalamanderGeneral->LoadStr(HLanguage, resID);
}

void OnAbout(HWND hParent)
{
  char buf[1000];
  _snprintf_s(buf, _TRUNCATE, 
              "%s " VERSINFO_VERSION "\n\n"
              VERSINFO_COPYRIGHT "\n\n"
              "%s",
              LoadStr(IDS_PLUGINNAME),
              LoadStr(IDS_PLUGIN_DESCRIPTION));
  SalamanderGeneral->SalMessageBox(hParent, buf, LoadStr(IDS_ABOUT), MB_OK | MB_ICONINFORMATION);
}

//
// ****************************************************************************
// SalamanderPluginGetReqVer
//

#ifdef __BORLANDC__
extern "C"{
int WINAPI SalamanderPluginGetReqVer();
CPluginInterfaceAbstract * WINAPI SalamanderPluginEntry(CSalamanderPluginEntryAbstract *salamander);
};
#endif // __BORLANDC__

int WINAPI SalamanderPluginGetReqVer()
{
  return LAST_VERSION_OF_SALAMANDER;
}

//
// ****************************************************************************
// SalamanderPluginEntry
//

CPluginInterfaceAbstract * WINAPI SalamanderPluginEntry(CSalamanderPluginEntryAbstract *salamander)
{
  // nastavime SalamanderDebug pro "dbg.h"
  SalamanderDebug = salamander->GetSalamanderDebug();
  // nastavime SalamanderVersion pro "spl_com.h"
  SalamanderVersion = salamander->GetVersion();
  HANDLES_CAN_USE_TRACE();
  CALL_STACK_MESSAGE1("SalamanderPluginEntry()");

  // tento plugin je delany pro aktualni verzi Salamandera a vyssi - provedeme kontrolu
  if (SalamanderVersion < LAST_VERSION_OF_SALAMANDER)
  {  // starsi verze odmitneme
    MessageBox(salamander->GetParentWindow(),
               REQUIRE_LAST_VERSION_OF_SALAMANDER,
               PluginNameEN, MB_OK | MB_ICONERROR);
    return NULL;
  }

  // nechame nacist jazykovy modul (.slg)
  HLanguage = salamander->LoadLanguageModule(salamander->GetParentWindow(), PluginNameEN);
  if (HLanguage == NULL) return NULL;

  // ziskame obecne rozhrani Salamandera
  SalamanderGeneral = salamander->GetSalamanderGeneral();
  // ziskame rozhrani poskytujici upravene Windows controly pouzivane v Salamanderovi
//  SalamanderGUI = salamander->GetSalamanderGUI();

  // nastavime jmeno souboru s helpem
  SalamanderGeneral->SetHelpFileName("demomenu.chm");

  // nastavime zakladni informace o pluginu
  salamander->SetBasicPluginData(LoadStr(IDS_PLUGINNAME), 0, VERSINFO_VERSION_NO_PLATFORM, VERSINFO_COPYRIGHT,
                                 LoadStr(IDS_PLUGIN_DESCRIPTION), PluginNameShort,
                                 NULL, NULL);

  // nastavime URL home-page pluginu
  salamander->SetPluginHomePageURL(LoadStr(IDS_PLUGIN_HOME));

  return &PluginInterface;
}

//
// ****************************************************************************
// CPluginInterface
//

void WINAPI
CPluginInterface::About(HWND parent)
{
  OnAbout(parent);
}

void WINAPI
CPluginInterface::Connect(HWND parent, CSalamanderConnectAbstract *salamander)
{
  CALL_STACK_MESSAGE1("CPluginInterface::Connect(,)");

  // zakladni cast:
  salamander->AddMenuItem(-1, LoadStr(IDS_TESTCMD), SALHOTKEY('M', HOTKEYF_CONTROL | HOTKEYF_SHIFT),
                          MENUCMD_TESTCMD, FALSE, MENU_EVENT_TRUE, MENU_EVENT_TRUE, MENU_SKILLLEVEL_ALL);

/*
  CGUIIconListAbstract *iconList = SalamanderGUI->CreateIconList();
  iconList->Create(16, 16, 1);
  HICON hIcon = (HICON)LoadImage(DLLInstance, MAKEINTRESOURCE(IDI_PLUGINICON), IMAGE_ICON, 16, 16, SalamanderGeneral->GetIconLRFlags());
  iconList->ReplaceIcon(0, hIcon);
  DestroyIcon(hIcon);
  salamander->SetIconListForGUI(iconList); // o destrukci iconlistu se postara Salamander

  salamander->SetPluginIcon(0);
  salamander->SetPluginMenuAndToolbarIcon(0);
*/
}

CPluginInterfaceForMenuExtAbstract * WINAPI
CPluginInterface::GetInterfaceForMenuExt()
{
  return &InterfaceForMenuExt;
}
