nsresult
BrowserWindow::Shutdown ()
{
	PRINT("BrowserWindow::Shutdown\n");
	if (!webBrowser)
		return NS_OK;

	nsCOMPtr<nsIWebProgressListener> wpl ((nsIWebProgressListener*) this);
    nsCOMPtr<nsIWeakReference> weakWpl (NS_GetWeakReference (wpl));
	if (weakWpl)
		webBrowser->RemoveWebBrowserListener (weakWpl, NS_GET_IID (nsIWebProgressListener));
	listeners.clear ();
	return NS_OK;
}
// nsIWebProgressListener implementation
NS_IMETHODIMP
nsMsgPrintEngine::OnStateChange(nsIWebProgress* aWebProgress, 
                   nsIRequest *aRequest, 
                   PRUint32 progressStateFlags, 
                   nsresult aStatus)
{
  nsresult rv = NS_OK;

  // top-level document load data
  if (progressStateFlags & nsIWebProgressListener::STATE_IS_DOCUMENT) {
    if (progressStateFlags & nsIWebProgressListener::STATE_START) {
      // Tell the user we are loading...
      nsString msg;
      GetString(NS_LITERAL_STRING("LoadingMessageToPrint").get(), msg);
      SetStatusMessage(msg);
    }

    if (progressStateFlags & nsIWebProgressListener::STATE_STOP) {
      nsCOMPtr<nsIDocumentLoader> docLoader(do_QueryInterface(aWebProgress));
      if (docLoader) 
      {
        // Check to see if the document DOMWin that is finished loading is the same
        // one as the mail msg that we started to load.
        // We only want to print when the entire msg and all of its attachments
        // have finished loading.
        // The mail msg doc is the last one to receive the STATE_STOP notification
        nsCOMPtr<nsISupports> container;
        docLoader->GetContainer(getter_AddRefs(container));
        nsCOMPtr<nsIDOMWindow> domWindow(do_GetInterface(container));
        if (domWindow.get() != mMsgDOMWin.get()) {
          return NS_OK;
        }
      }
      nsCOMPtr<nsIWebProgressListener> wpl(do_QueryInterface(mPrintPromptService));
      if (wpl) {
        wpl->OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP|nsIWebProgressListener::STATE_IS_DOCUMENT, nsnull);
        mPrintProgressListener = nsnull;
        mPrintProgress         = nsnull;
        mPrintProgressParams   = nsnull;
      }

      bool isPrintingCancelled = false;
      if (mPrintSettings)
      {
        mPrintSettings->GetIsCancelled(&isPrintingCancelled);
      }
      if (!isPrintingCancelled) {
        // if aWebProgress is a documentloader than the notification from
        // loading the documents. If it is NULL (or not a DocLoader) then it 
        // it coming from Printing
        if (docLoader) {
          // Now, fire off the print operation!
          rv = NS_ERROR_FAILURE;

          // Tell the user the message is loaded...
          nsString msg;
          GetString(NS_LITERAL_STRING("MessageLoaded").get(), msg);
          SetStatusMessage(msg);

          NS_ASSERTION(mDocShell,"can't print, there is no docshell");
          if ( (!mDocShell) || (!aRequest) ) 
          {
            return StartNextPrintOperation();
          }
          nsCOMPtr<nsIChannel> aChannel = do_QueryInterface(aRequest);
          if (!aChannel) return NS_ERROR_FAILURE;

          // Make sure this isn't just "about:blank" finishing....
          nsCOMPtr<nsIURI> originalURI = nsnull;
          if (NS_SUCCEEDED(aChannel->GetOriginalURI(getter_AddRefs(originalURI))) && originalURI)
          {
            nsCAutoString spec;

            if (NS_SUCCEEDED(originalURI->GetSpec(spec)))
            {      
              if (spec.Equals("about:blank"))
              {
                return StartNextPrintOperation();
              }
            }
          }

          // If something bad happens here (menaing we can fire the PLEvent, highly unlikely)
          // we will still ask the msg to print, but if the user "cancels" out of the 
          // print dialog the hidden print window will not be "closed"
          if (!FirePrintEvent()) 
          {
            PrintMsgWindow();
          }
        } else {
          FireStartNextEvent();
          rv = NS_OK;
        }
      } 
      else 
      {
        mWindow->Close();
      }
    }
  }

  return rv;
}
nsresult
BrowserWindow::Create ( Handle * hwnd, PRInt32 width, PRInt32 height)
{
	PRINT("BrowserWindow::Create\n");
	nsresult result;

	webBrowser = do_CreateInstance( NS_WEBBROWSER_CONTRACTID );
	if ( ! webBrowser )
		return NS_ERROR_FAILURE;

    (void) webBrowser->SetContainerWindow (static_cast<nsIWebBrowserChrome *> (this));

    nsCOMPtr<nsIDocShellTreeItem> dsti = do_QueryInterface( webBrowser );
    dsti->SetItemType( nsIDocShellTreeItem::typeContentWrapper );

	webNav = do_QueryInterface(webBrowser);
	sessionHistory = do_CreateInstance(NS_SHISTORY_CONTRACTID);
	webNav->SetSessionHistory(sessionHistory);

    nsCOMPtr<nsIWindowCreator> windowCreator (static_cast<nsIWindowCreator *> (this));

    // Attach it via the watcher service
    nsCOMPtr<nsIWindowWatcher> watcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID);
    if (watcher)
      watcher->SetWindowCreator(windowCreator);

	
    nsCOMPtr<nsIX509CertDB> certdb = do_GetService(NS_X509CERTDB_CONTRACTID, &result);

	/** Component registration... ***/
	RegisterComponents ();

    baseWindow = do_QueryInterface (webBrowser);
	
	result = baseWindow->InitWindow( hwnd, nsnull,  0, 0, width, height );
	if (!NS_SUCCEEDED(result)) {
		SHOUT("BrowserWindow: Failed to initialize window\n");
		return NS_ERROR_FAILURE;
	}
    result = baseWindow->Create();
	if (!NS_SUCCEEDED(result)) {
		SHOUT("BrowserWindow: Failed to create window\n");
		return NS_ERROR_FAILURE;
	}

    nsCOMPtr<nsIWebProgressListener> wpl (static_cast<nsIWebProgressListener*>(this));
    nsCOMPtr<nsIWeakReference> weakWpl (NS_GetWeakReference (wpl));
    webBrowser->AddWebBrowserListener (weakWpl, NS_GET_IID (nsIWebProgressListener));

	baseWindow->SetVisibility( PR_TRUE );

	webNav = do_QueryInterface( webBrowser, &result );
	if ( NS_FAILED( result ) || ! webNav ) {
	    return NS_ERROR_FAILURE;
	}


    if ( webBrowser ) {
		// I really hope we don't need this, it calls in nsIWidget.h which calls
		// in a bunch of local includes we don't want
/*		
#ifdef NS_UNIX
		// save the window id of the newly created window
		nsCOMPtr<nsIWidget> mozWidget;
		baseWindow->GetMainWidget(getter_AddRefs(mozWidget));

		GdkWindow *tmp_window = static_cast<GdkWindow *> (mozWidget->GetNativeData(NS_NATIVE_WINDOW));

		// and, thanks to superwin we actually need the parent of that.
		tmp_window = gdk_window_get_parent(tmp_window);
		
		// save the widget ID - it should be the mozarea of the window.
		gpointer data = nsnull;
		gdk_window_get_user_data(tmp_window, &data);
		this->nativeMozWidget = static_cast<Handle *> (data);
#endif		
*/
		return NS_OK;
    }

    return NS_ERROR_FAILURE;
}
void
arrangement::compute_pointInCells(Arrangement_2 &arr, std::vector<std::vector<double> > &points)
{
    Walk_pl walk_pl(arr);

    int cpt = 0;
    for (Arrangement_2::Face_iterator face = arr.faces_begin(); face != arr.faces_end(); ++face)
    {
        if (face->is_unbounded())
            face->set_data(-1);
        else
        {
            // set data to each face
            face->set_data(cpt++);
            // find a point in this face
            Arrangement_2::Ccb_halfedge_circulator previous = face->outer_ccb();
            Arrangement_2::Ccb_halfedge_circulator first_edge = face->outer_ccb();
            Arrangement_2::Ccb_halfedge_circulator edge = face->outer_ccb();
            ++edge;
            do
            {
                std::vector<double> p1 = getPointMiddle(previous);
                std::vector<double> p2 = getPointMiddle(edge);
                std::vector<double> m;
                m.push_back((p1[0]+p2[0])/2);
                m.push_back((p1[1]+p2[1])/2);
                Rational x_(m[0]);
                Rational y_(m[1]);
                Conic_point_2 p(x_,y_);

                Arrangement_2::Vertex_handle v = insert_point(arr, p, walk_pl);
                try
                {
                    if (v->face()->data() == (cpt-1))
                    {
                        bool flag = false;
                        // test if it is not in holes and not in unbounded face
                        for (int i = 0; i < (int) convolutions_o.size(); ++i)
                        {
                            Walk_pl wpl(convolutions_o[i]);
                            Arrangement_2::Vertex_handle t = insert_point(convolutions_o[i], p, wpl);
                            if (t->face()->data() == 1)
                            {
                                convolutions_o[i].remove_isolated_vertex(t);
                                break;
                            }
                            else if (t->face()->data() == 2 || t->face()->data() == 0)
                            {
                                flag = true;
                                convolutions_o[i].remove_isolated_vertex(t);
                                break;
                            }
                        }

                        // then continue
                        if (!flag)
                            points.push_back(m);
                        else
                        {
                            --cpt;
                            face->set_data(-1);
                        }

                        arr.remove_isolated_vertex(v);
                        break;
                    }
                    arr.remove_isolated_vertex(v);
                }
                catch (const std::exception exn) {}
                previous = edge;

                ++edge;
            } while (edge != first_edge);
        }
    }
}