gecko-dev/content/html/document/src/nsImageDocument.cpp

338 lines
9.2 KiB
C++
Raw Normal View History

1998-07-27 17:50:58 +00:00
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1998
* Netscape Communications Corporation. All Rights Reserved.
*/
#include "nsCOMPtr.h"
#include "nsHTMLDocument.h"
1998-07-27 17:50:58 +00:00
#include "nsHTMLParts.h"
#include "nsHTMLAtoms.h"
#include "nsIHTMLContent.h"
#include "nsIImageGroup.h"
#include "nsIImageRequest.h"
#include "nsIStreamListener.h"
#include "nsIURL.h"
#include "nsHTMLValue.h"
#include "nsIHTMLStyleSheet.h"
#include "nsIHTMLCSSStyleSheet.h"
#include "nsIPresShell.h"
#include "nsIPresContext.h"
#include "nsIViewManager.h"
1998-07-27 17:50:58 +00:00
// XXX TODO:
// 1. hookup the original stream to the image library directly so that we
// don't have to load the image twice.
// 2. have a synthetic document url that we load that has patterns in it
// that we replace with the image url (so that we can customize the
// the presentation of the image): we could add script code to set the
// width/height to provide scale buttons, we could show the image
// attributes; etc.; should we have a seperate style sheet?
// 3. override the save-as methods so that we don't write out our synthetic
// html
static NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID);
static NS_DEFINE_IID(kIDocumentIID, NS_IDOCUMENT_IID);
class nsImageDocument : public nsHTMLDocument {
1998-07-27 17:50:58 +00:00
public:
nsImageDocument();
virtual ~nsImageDocument();
NS_IMETHOD StartDocumentLoad(nsIURI* aURL,
1998-07-27 17:50:58 +00:00
nsIContentViewerContainer* aContainer,
1998-11-11 11:55:32 +00:00
nsIStreamListener** aDocListener,
const char* aCommand);
1998-07-27 17:50:58 +00:00
nsresult CreateSyntheticDocument();
nsresult StartImageLoad(nsIURI* aURL, nsIStreamListener*& aListener);
1998-07-27 17:50:58 +00:00
void StartLayout();
nsIImageRequest* mImageRequest;
nscolor mBlack;
1998-07-27 17:50:58 +00:00
};
//----------------------------------------------------------------------
1998-07-27 17:50:58 +00:00
class ImageListener : public nsIStreamListener {
public:
ImageListener(nsImageDocument* aDoc);
virtual ~ImageListener();
1998-07-27 17:50:58 +00:00
NS_DECL_ISUPPORTS
NS_IMETHOD OnStartBinding(nsIURI* aURL, const char *aContentType);
NS_IMETHOD OnProgress(nsIURI* aURL, PRUint32 aProgress, PRUint32 aProgressMax);
NS_IMETHOD OnStatus(nsIURI* aURL, const PRUnichar* aMsg);
NS_IMETHOD OnStopBinding(nsIURI* aURL, nsresult aStatus,
const PRUnichar* aMsg);
NS_IMETHOD GetBindInfo(nsIURI* aURL, nsStreamBindingInfo* aInfo);
NS_IMETHOD OnDataAvailable(nsIURI* aURL, nsIInputStream* aStream,
PRUint32 aCount);
1998-07-27 17:50:58 +00:00
nsImageDocument* mDocument;
nsIStreamListener* mNextStream;
1998-07-27 17:50:58 +00:00
};
ImageListener::ImageListener(nsImageDocument* aDoc)
{
mDocument = aDoc;
NS_ADDREF(aDoc);
mRefCnt = 1;
}
ImageListener::~ImageListener()
{
NS_RELEASE(mDocument);
NS_IF_RELEASE(mNextStream);
1998-07-27 17:50:58 +00:00
}
NS_IMPL_ISUPPORTS(ImageListener, kIStreamListenerIID)
NS_IMETHODIMP
ImageListener::OnStartBinding(nsIURI* aURL, const char *aContentType)
1998-07-27 17:50:58 +00:00
{
mDocument->StartImageLoad(aURL, mNextStream);
if (nsnull == mNextStream) {
return NS_ERROR_FAILURE;
}
return mNextStream->OnStartBinding(aURL, aContentType);
1998-07-27 17:50:58 +00:00
}
NS_IMETHODIMP
ImageListener::OnProgress(nsIURI* aURL, PRUint32 aProgress,
PRUint32 aProgressMax)
1998-07-27 17:50:58 +00:00
{
if (nsnull == mNextStream) {
return NS_ERROR_FAILURE;
}
return mNextStream->OnProgress(aURL, aProgress, aProgressMax);
1998-07-27 17:50:58 +00:00
}
NS_IMETHODIMP
ImageListener::OnStatus(nsIURI* aURL, const PRUnichar* aMsg)
1998-07-27 17:50:58 +00:00
{
if (nsnull == mNextStream) {
return NS_ERROR_FAILURE;
}
return mNextStream->OnStatus(aURL, aMsg);
1998-07-27 17:50:58 +00:00
}
NS_IMETHODIMP
ImageListener::OnStopBinding(nsIURI* aURL, nsresult aStatus,
const PRUnichar* aMsg)
1998-07-27 17:50:58 +00:00
{
if (nsnull == mNextStream) {
return NS_ERROR_FAILURE;
}
return mNextStream->OnStopBinding(aURL, aStatus, aMsg);
1998-07-27 17:50:58 +00:00
}
NS_IMETHODIMP
ImageListener::GetBindInfo(nsIURI* aURL, nsStreamBindingInfo* aInfo)
1998-07-27 17:50:58 +00:00
{
if (nsnull == mNextStream) {
return NS_ERROR_FAILURE;
}
return mNextStream->GetBindInfo(aURL, aInfo);
1998-07-27 17:50:58 +00:00
}
NS_IMETHODIMP
ImageListener::OnDataAvailable(nsIURI* aURL, nsIInputStream* aStream,
PRUint32 aCount)
1998-07-27 17:50:58 +00:00
{
if (nsnull == mNextStream) {
return NS_ERROR_FAILURE;
1998-07-27 17:50:58 +00:00
}
return mNextStream->OnDataAvailable(aURL, aStream, aCount);
1998-07-27 17:50:58 +00:00
}
//----------------------------------------------------------------------
NS_LAYOUT nsresult
NS_NewImageDocument(nsIDocument** aInstancePtrResult)
{
nsImageDocument* doc = new nsImageDocument();
return doc->QueryInterface(kIDocumentIID, (void**) aInstancePtrResult);
}
nsImageDocument::nsImageDocument()
{
mImageRequest = nsnull;
mBlack = NS_RGB(0, 0, 0);
1998-07-27 17:50:58 +00:00
}
nsImageDocument::~nsImageDocument()
{
NS_IF_RELEASE(mImageRequest);
1998-07-27 17:50:58 +00:00
}
NS_IMETHODIMP
nsImageDocument::StartDocumentLoad(nsIURI* aURL,
1998-07-27 17:50:58 +00:00
nsIContentViewerContainer* aContainer,
1998-11-11 11:55:32 +00:00
nsIStreamListener** aDocListener,
const char* aCommand)
1998-07-27 17:50:58 +00:00
{
nsresult rv = nsDocument::StartDocumentLoad(aURL,
aContainer,
aDocListener,
aCommand);
1998-12-11 02:47:25 +00:00
if (NS_FAILED(rv)) {
return rv;
}
// Create synthetic document
rv = CreateSyntheticDocument();
if (NS_OK != rv) {
return rv;
1998-07-27 17:50:58 +00:00
}
*aDocListener = new ImageListener(this);
return NS_OK;
}
nsresult
nsImageDocument::StartImageLoad(nsIURI* aURL, nsIStreamListener*& aListener)
{
nsresult rv = NS_OK;
aListener = nsnull;
// Tell image group to load the stream now. This will get the image
// hooked up to the open stream and return the underlying listener
// so that we can pass it back upwards.
nsIPresShell* shell = GetShellAt(0);
if (nsnull != shell) {
nsCOMPtr<nsIPresContext> cx;
shell->GetPresContext(getter_AddRefs(cx));
if (cx) {
nsIImageGroup* group = nsnull;
cx->GetImageGroup(&group);
if (nsnull != group) {
const char* spec;
(void)aURL->GetSpec(&spec);
nsIStreamListener* listener = nsnull;
rv = group->GetImageFromStream(spec, nsnull, nsnull,
0, 0, 0,
mImageRequest, listener);
aListener = listener;
NS_RELEASE(group);
}
}
NS_RELEASE(shell);
}
// Finally, start the layout going
StartLayout();
return NS_OK;
}
1998-07-27 17:50:58 +00:00
nsresult
nsImageDocument::CreateSyntheticDocument()
{
// XXX Set title to "GIF image widthxheight pixels - Netscape"
// Wire up an image load request to the document
// mImageRequest = aGroup->GetImage(cp, this, aBackgroundColor, 0, 0, 0);
// Synthesize an html document that refers to the image
nsresult rv;
nsIHTMLContent* root;
rv = NS_NewHTMLHtmlElement(&root, nsHTMLAtoms::html);
1998-07-27 17:50:58 +00:00
if (NS_OK != rv) {
return rv;
}
root->SetDocument(this, PR_FALSE);
SetRootContent(root);
1998-07-27 17:50:58 +00:00
nsIHTMLContent* body;
rv = NS_NewHTMLBodyElement(&body, nsHTMLAtoms::body);
1998-07-27 17:50:58 +00:00
if (NS_OK != rv) {
return rv;
}
body->SetDocument(this, PR_FALSE);
1998-07-27 17:50:58 +00:00
nsIHTMLContent* center;
1999-02-12 06:19:07 +00:00
nsIAtom* centerAtom = NS_NewAtom("p");
rv = NS_NewHTMLParagraphElement(&center, centerAtom);
1998-07-27 17:50:58 +00:00
NS_RELEASE(centerAtom);
if (NS_OK != rv) {
return rv;
}
center->SetDocument(this, PR_FALSE);
1998-07-27 17:50:58 +00:00
nsIHTMLContent* image;
1999-02-12 06:19:07 +00:00
nsIAtom* imgAtom = NS_NewAtom("img");
rv = NS_NewHTMLImageElement(&image, imgAtom);
1998-07-27 17:50:58 +00:00
NS_RELEASE(imgAtom);
if (NS_OK != rv) {
return rv;
}
image->SetDocument(this, PR_FALSE);
PRUnichar* src;
mDocumentURL->ToString(&src);
1998-07-27 17:50:58 +00:00
nsHTMLValue val(src);
1999-05-26 22:53:12 +00:00
delete[] src;
1998-12-20 01:21:23 +00:00
image->SetHTMLAttribute(nsHTMLAtoms::src, val, PR_FALSE);
image->SetHTMLAttribute(nsHTMLAtoms::alt, val, PR_FALSE);
1998-07-27 17:50:58 +00:00
root->AppendChildTo(body, PR_FALSE);
center->AppendChildTo(image, PR_FALSE);
body->AppendChildTo(center, PR_FALSE);
1998-07-27 17:50:58 +00:00
NS_RELEASE(image);
NS_RELEASE(center);
NS_RELEASE(body);
NS_RELEASE(root);
return NS_OK;
}
void
nsImageDocument::StartLayout()
{
PRInt32 i, ns = GetNumberOfShells();
for (i = 0; i < ns; i++) {
nsIPresShell* shell = GetShellAt(i);
if (nsnull != shell) {
// Make shell an observer for next time
shell->BeginObservingDocument();
// Resize-reflow this time
nsCOMPtr<nsIPresContext> cx;
shell->GetPresContext(getter_AddRefs(cx));
1998-07-27 17:50:58 +00:00
nsRect r;
cx->GetVisibleArea(r);
shell->InitialReflow(r.width, r.height);
// Now trigger a refresh
// XXX It's unfortunate that this has to be here
nsCOMPtr<nsIViewManager> vm;
shell->GetViewManager(getter_AddRefs(vm));
if (vm) {
vm->EnableRefresh();
}
1998-07-27 17:50:58 +00:00
NS_RELEASE(shell);
}
}
}