#include "stdafx.h"

#include "wb_config.h"

#include "grtpp.h"
#include "grt/common.h"

#include "grts/structs.app.h"
#include "grts/structs.workbench.h"
#include "grts/structs.model.h"

#include "wbcanvas/model_diagram_impl.h"

#include "wb_module_printing.h"

#include "mdc_canvas_view_printing.h"




#ifdef _WIN32
# define FRONTEND_LIBNAME(obj, windows_dll, linux_so, osx_dylib)\
  obj->moduleName(windows_dll)
#elif defined(__APPLE__)
# define FRONTEND_LIBNAME(obj, windows_dll, linux_so, osx_dylib)\
  obj->moduleName(osx_dylib)
#else
# define FRONTEND_LIBNAME(obj, windows_dll, linux_so, osx_dylib)\
  obj->moduleName(linux_so)
#endif




#define def_export_plugin(aName, aCaption, aDialogCaption, aExtensions)\
  {\
    app_PluginRef plugin(get_grt());\
    app_PluginObjectInputRef pdef(get_grt());\
    plugin->name("wb.print."aName);\
    plugin->caption(aCaption);\
    plugin->moduleName("WbPrinting");\
    plugin->moduleFunctionName(aName);\
    plugin->pluginType("normal");\
    plugin->showProgress(1);\
    pdef->name("activeDiagram");\
    pdef->objectStructName(model_Diagram::static_class_name());\
    pdef->owner(plugin);\
    plugin->inputValues().insert(pdef);\
    app_PluginFileInputRef pdef2(get_grt());\
    pdef2->owner(plugin);\
    pdef2->dialogTitle(aDialogCaption);\
    pdef2->dialogType("save");\
    pdef2->fileExtensions(aExtensions);\
    plugin->inputValues().insert(pdef2);\
    plugin->groups().insert("Application/Workbench");\
    list.insert(plugin);\
  }


WbPrintingImpl::WbPrintingImpl(grt::CPPModuleLoader *ldr)
: super(ldr)
{
}



grt::ListRef<app_Plugin> WbPrintingImpl::getPluginInfo()
{
  grt::ListRef<app_Plugin> list(get_grt());

  def_export_plugin("printToPDFFile", "Print Model to a PDF File", "Print to PDF", "PDF Files (*.pdf)|*.pdf");
  def_export_plugin("printToPSFile", "Print Model to a PS File", "Print to PS", "PostScript Files (*.ps)|*.ps");

  {
    app_PluginRef plugin(get_grt());

    FRONTEND_LIBNAME(plugin,
                     ".\\wb.printing.wbp.fe.dll",
                     "wb.printing.wbp.so",
                     "wb.printing.mwbplugin");
    plugin->pluginType("gui");
    plugin->moduleFunctionName("PrintDialog");
    plugin->rating(100);
    plugin->name("wb.print.print");
    plugin->caption(_("Print Diagram"));
    app_PluginObjectInputRef pdef(get_grt());
    pdef->name("activeDiagram");
    pdef->objectStructName(model_Diagram::static_class_name());
    pdef->owner(plugin);
    plugin->inputValues().insert(pdef);
    plugin->groups().insert("Model/Printing");
    plugin->showProgress(2);

    list.insert(plugin);
  }

  {
    app_PluginRef plugin(get_grt());

    FRONTEND_LIBNAME(plugin,
                     ".\\wb.printing.wbp.fe.dll",
                     "",
                     "");
    plugin->pluginType("gui");
    plugin->moduleFunctionName("PrintPreviewDialog");
    plugin->rating(100);
    plugin->name("wb.print.printPreview");
    plugin->caption(_("Print "));
    app_PluginObjectInputRef pdef(get_grt());
    pdef->name("activeDiagram");
    pdef->objectStructName(model_Diagram::static_class_name());
    pdef->owner(plugin);
    plugin->inputValues().insert(pdef);
    plugin->groups().insert("Model/Printing");
    plugin->showProgress(2);

    list.insert(plugin);
  }

  {
    app_PluginRef plugin(get_grt());

    FRONTEND_LIBNAME(plugin,
                     "",
                     "wb.printing.wbp.so",
                     "");
    plugin->pluginType("gui");
    plugin->moduleFunctionName("PrintSetupDialog");
    plugin->rating(100);
    plugin->name("wb.print.setup");
    plugin->caption(_("Print "));
    plugin->groups().insert("Model/Printing");
    plugin->showProgress(2);

    list.insert(plugin);
  }

  return list;
}



int WbPrintingImpl::printToPDFFile(model_DiagramRef view, const std::string &path)
{
  mdc::CanvasViewExtras extras(view->get_data()->get_canvas_view());

  app_PageSettingsRef page(workbench_DocumentRef::cast_from(get_grt()->get("/wb/doc"))->pageSettings());

  extras.set_page_margins(page->marginTop(), page->marginLeft(), page->marginBottom(), page->marginRight());
  extras.set_paper_size(page->paperType()->width(), page->paperType()->height());
  extras.set_orientation(page->orientation()=="landscape"?mdc::Landscape:mdc::Portrait);
  extras.set_scale(page->scale());

  int pages= extras.print_to_pdf(path);

  return pages;
}


int WbPrintingImpl::printToPSFile(model_DiagramRef view, const std::string &path)
{
  mdc::CanvasViewExtras extras(view->get_data()->get_canvas_view());

  app_PageSettingsRef page(workbench_DocumentRef::cast_from(get_grt()->get("/wb/doc"))->pageSettings());

  extras.set_page_margins(page->marginTop(), page->marginLeft(), page->marginBottom(), page->marginRight());
  extras.set_paper_size(page->paperType()->width(), page->paperType()->height());
  extras.set_orientation(page->orientation()=="landscape"?mdc::Landscape:mdc::Portrait);
  extras.set_scale(page->scale());

  int pages= extras.print_to_ps(path);

  return pages;
}



int WbPrintingImpl::printToPrinter(model_DiagramRef view, const std::string &printer)
{
  return 0;
}




GRT_MODULE_ENTRY_POINT(WbPrintingImpl);




