/* 
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "stdafx.h"

#include "mforms/mforms.h"
#include "wf_view.h"
#include "wf_webbrowser.h"

#include "ConvUtils.h"

using namespace MySQL::Forms;

//----------------- WebBrowserImpl -----------------------------------------------------------------

bool WebBrowserImpl::create(mforms::WebBrowser *self)
{
  WebBrowserImpl^ browser= gcnew WebBrowserImpl(self);
  if (browser != nullptr)
  {
    Windows::Forms::WebBrowser^ control= ViewImpl::create<Windows::Forms::WebBrowser>(self, browser);
    control->ScriptErrorsSuppressed = true; // No script error popup please.

    control->DocumentCompleted += gcnew WebBrowserDocumentCompletedEventHandler(browser, &DocumentLoaded);
    control->NewWindow += gcnew CancelEventHandler(browser, &OnNewWindow);
    control->StatusTextChanged += gcnew EventHandler(browser, &StatusTextChanged);
    control->Navigating += gcnew WebBrowserNavigatingEventHandler(browser, &OnNavigating);


    return true;
  }
  return false;
}

//-------------------------------------------------------------------------------------------------

void WebBrowserImpl::set_html(mforms::WebBrowser *self, const std::string& code)
{
  WebBrowserImpl^ browser= (WebBrowserImpl^)ObjectImpl::FromUnmanaged(self);

  if (browser != nullptr)
    browser->set_html(code);
}

//-------------------------------------------------------------------------------------------------

void WebBrowserImpl::navigate(mforms::WebBrowser *self, const std::string& url)
{
  WebBrowserImpl^ browser= (WebBrowserImpl^)ObjectImpl::FromUnmanaged(self);

  if (browser != nullptr)
    browser->navigate(url);
}

//-------------------------------------------------------------------------------------------------

std::string WebBrowserImpl::get_document_title(mforms::WebBrowser *self)
{
  WebBrowserImpl^ browser= (WebBrowserImpl^)ObjectImpl::FromUnmanaged(self);

  if (browser != nullptr)
    return browser->get_document_title();

  return "";
}

//-------------------------------------------------------------------------------------------------

void WebBrowserImpl::DocumentLoaded(Object^ sender, WebBrowserDocumentCompletedEventArgs^ e)
{
  Windows::Forms::WebBrowser^ control= (Windows::Forms::WebBrowser^) sender;

  if (control->Tag != nullptr)
  {
    mforms::WebBrowser* browser= get_backend_control<mforms::WebBrowser>(control);
    if (browser != 0)
      browser->document_loaded(NativeToCppString(e->Url->AbsoluteUri));
  }
}

//-------------------------------------------------------------------------------------------------

void WebBrowserImpl::OnNewWindow(Object^ sender, CancelEventArgs^ e)
{
  Windows::Forms::WebBrowser^ control= dynamic_cast<Windows::Forms::WebBrowser^>(sender);

  HtmlElement^ element= control->Document->ActiveElement;
  String^ url= element->GetAttribute("href");

  // We don't want to let IE open an external IE window if a new window is requested.
  // Instead we cancel the action here and open the default browser.
  mforms::Utilities::open_url(NativeToCppString(url));
  e->Cancel= true;
}

//-------------------------------------------------------------------------------------------------

void WebBrowserImpl::StatusTextChanged(Object^ sender, EventArgs^ e)
{
  Windows::Forms::WebBrowser^ control= dynamic_cast<Windows::Forms::WebBrowser^>(sender);

  mforms::App::get()->set_status_text(NativeToCppString(control->StatusText));
}

//-------------------------------------------------------------------------------------------------

void WebBrowserImpl::OnNavigating(Object^ sender, WebBrowserNavigatingEventArgs^ args)
{
  Windows::Forms::WebBrowser^ control= dynamic_cast<Windows::Forms::WebBrowser^>(sender);
  if (control && control->Tag != nullptr)
  {
    mforms::WebBrowser* browser= get_backend_control<mforms::WebBrowser>(control);
    if (browser)
    {
      if (browser->on_link_clicked(NativeToCppString(args->Url->ToString())))
        args->Cancel = true;
    }
  }
}

//----------------- WebBrowserImpl actual implementation -------------------------------------------

void WebBrowserImpl::set_html(const std::string& code)
{
  Windows::Forms::WebBrowser^ browser= get_control<Windows::Forms::WebBrowser>();
  browser->DocumentText = CppStringToNative(code);
}

//-------------------------------------------------------------------------------------------------

void WebBrowserImpl::navigate(const std::string& url)
{
  Windows::Forms::WebBrowser^ browser= get_control<Windows::Forms::WebBrowser>();
  browser->Navigate(CppStringToNative(url));
}

//-------------------------------------------------------------------------------------------------

std::string WebBrowserImpl::get_document_title()
{
  Windows::Forms::WebBrowser^ browser= get_control<Windows::Forms::WebBrowser>();
  return NativeToCppString(browser->DocumentTitle);
}

//-------------------------------------------------------------------------------------------------
