OK, I came up with a solution on my own. Maybe one of the embedding API
developers could verify this approach is OK & if so possibly add a function
to the embedding API to prevent others having to re-invent the wheel. Code
below.

Merry xmas all,

- Steve.


void GetWebPageExtents(nsCOMPtr<nsIWebBrowser> web_browser, int &width, int
&height)
{
 width = 0; height = 0;

 nsCOMPtr <nsIDocShell> doc_shell = do_GetInterface(web_browser);

 if (!doc_shell)
  return ;

 nsCOMPtr<nsIPresShell> presShell;
 doc_shell->GetPresShell(getter_AddRefs(presShell));

 if (presShell)
 {
  nsCOMPtr<nsIViewManager> vm;
  presShell->GetViewManager(getter_AddRefs(vm));

  nsIScrollableView *scrollableView = nsnull;
  vm->GetRootScrollableView(&scrollableView);

  if (scrollableView)
  {
   nscoord ns_width, ns_height;
   scrollableView->GetContainerSize(&ns_width,&ns_height);

   nsIDeviceContext  *dx=NULL;
   float             t2p;

   vm->GetDeviceContext(dx);
   dx->GetAppUnitsToDevUnits(t2p);

   width  = NSTwipsToIntPixels(ns_width, t2p);
   height = NSTwipsToIntPixels(ns_height, t2p);
  }
 }
}

... The results from this function could be used (for example) to resize a
native window to the extents of the page.

E.g. on win32 :-

   SetWindowPos(hwnd, NULL, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER |
SWP_NOACTIVATE);

It would seem that the best way to integrate this functionality would be to
add a member to the nsIWebBrowser class.
Comments welcome.



"Steve Williams" <[EMAIL PROTECTED]> wrote in message
9vqfe9$[EMAIL PROTECTED]">news:9vqfe9$[EMAIL PROTECTED]...
> Hi guys,
>
> I'm attempting to determine the extents of a webpage in native device
window
> coordinates. I have been unable
> to find a function that does this (I'm still pretty green with the
codebase,
> so I've probably missed it). The code
> below should illustrate what I'm trying to do - it's based very closely on
> the page loading code in the winembed sample.
> My current thinking is to write a function that iterates through all
views,
> grabbing the mRect field from each associated
> nsIFrame and form a union to generate a total bounding rectangle, then
> convert to the appropriate coord system.
> Will this work ? Is it overkill - ie. does a method exist somewhere
already
> that will achieve what I'm attempting to do ?
>
> Thanks in advance for any help,
> Steve.
>
>
> class MyClass
> {
>  public:
>      void LoadWebPage(const char *url);
>
> private:
>       nsCOMPtr<nsIWebBrowser> m_web_browser;
>       nsCOMPtr<nsIWebBrowserChrome> m_chrome;
>
>       // Rest of class definition ...
> };
>
> void MyClass::LoadWebPage(const char *url)
> {
>     nsresult  rv;
>
>      // Create the chrome object. Note that it leaves this function
>      // with an extra reference so that it can released correctly during
>      // destruction (via Win32UI::Destroy)
>
>      rv =
> AppCallbacks::CreateBrowserWindow(nsIWebBrowserChrome::CHROME_DEFAULT,
> nsnull, getter_AddRefs(m_chrome));
>
>     if (!m_chrome || !NS_SUCCEEDED(rv))
>        return;
>
>      // Start loading a page
>      m_chrome->GetWebBrowser(getter_AddRefs(m_web_browser));
>      nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(m_web_browser));
>
>      rv = webNav->LoadURI(NS_ConvertASCIItoUCS2(url).get(),
> nsIWebNavigation::LOAD_FLAGS_NONE);
>
>     if (!NS_SUCCEEDED(rv))
>       return;
>
>      nsCOMPtr<nsIBaseWindow> webBrowserAsWin =
> do_QueryInterface(m_web_browser);
>
>
>     // [PSEUDO CODE]  Get webpage extents ...
>
>     nsRect rect;
>     m_web_browser->GetExtents(rect);  // How do I do this using the
> embedding API ???
>
>    // [END PSEUDO CODE]
>
>
>
>   // Resize window so it's exactly large enough to display the web page
> without the need for scrollbars ...
>     if (webBrowserAsWin)
>          webBrowserAsWin->SetPositionAndSize(rect.x, rect.y, rect.width,
> rect.height, PR_TRUE);
> }
>
>
>
>



Reply via email to