diff --git a/layout/forms/nsGfxButtonControlFrame.cpp b/layout/forms/nsGfxButtonControlFrame.cpp index 082db4933567..6cf267e33c7a 100644 --- a/layout/forms/nsGfxButtonControlFrame.cpp +++ b/layout/forms/nsGfxButtonControlFrame.cpp @@ -28,6 +28,10 @@ #include "nsISupportsArray.h" #include "nsINameSpaceManager.h" +// Saving PresState +#include "nsIPresState.h" +#include "nsIHTMLContent.h" + const nscoord kSuggestedNotSet = -1; nsGfxButtonControlFrame::nsGfxButtonControlFrame() @@ -456,14 +460,17 @@ nsGfxButtonControlFrame::CreateFrameFor(nsIPresContext* aPresContext, NS_IMETHODIMP nsGfxButtonControlFrame::QueryInterface(const nsIID& aIID, void** aInstancePtr) { - NS_PRECONDITION(0 != aInstancePtr, "null ptr"); - if (NULL == aInstancePtr) { - return NS_ERROR_NULL_POINTER; - } else if (aIID.Equals(NS_GET_IID(nsIAnonymousContentCreator))) { - *aInstancePtr = (void*)(nsIAnonymousContentCreator*) this; - return NS_OK; + NS_ENSURE_ARG_POINTER(aInstancePtr); + + if (aIID.Equals(NS_GET_IID(nsIAnonymousContentCreator))) { + *aInstancePtr = NS_STATIC_CAST(nsIAnonymousContentCreator*, this); + } else if (aIID.Equals(NS_GET_IID(nsIStatefulFrame))) { + *aInstancePtr = NS_STATIC_CAST(nsIStatefulFrame*, this); + } else { + return nsHTMLButtonControlFrame::QueryInterface(aIID, aInstancePtr); } - return nsHTMLButtonControlFrame::QueryInterface(aIID, aInstancePtr); + + return NS_OK; } // Initially we hardcoded the default strings here. @@ -665,4 +672,50 @@ nsGfxButtonControlFrame::HandleEvent(nsIPresContext* aPresContext, return NS_OK; } +NS_IMETHODIMP +nsGfxButtonControlFrame::GetStateType(nsIPresContext* aPresContext, nsIStatefulFrame::StateType* aStateType) +{ + *aStateType = nsIStatefulFrame::eTextType; + return NS_OK; +} +NS_IMETHODIMP +nsGfxButtonControlFrame::SaveState(nsIPresContext* aPresContext, nsIPresState** aState) +{ + NS_ENSURE_ARG_POINTER(aState); + + // Get the value string + nsAutoString stateString; + nsresult res = GetProperty(nsHTMLAtoms::value, stateString); + NS_ENSURE_SUCCESS(res, res); + + // Compare to default value, and only save if needed (Bug 62713) + nsAutoString defaultStateString; + nsCOMPtr formControl(do_QueryInterface(mContent)); + if (formControl) { + formControl->GetAttribute(kNameSpaceID_None, nsHTMLAtoms::value, defaultStateString); + } + + if (! stateString.Equals(defaultStateString)) { + + // Construct a pres state and store value in it. + res = NS_NewPresState(aState); + NS_ENSURE_SUCCESS(res, res); + res = (*aState)->SetStateProperty(NS_LITERAL_STRING("value"), stateString); + } + + return res; +} + +NS_IMETHODIMP +nsGfxButtonControlFrame::RestoreState(nsIPresContext* aPresContext, nsIPresState* aState) +{ + NS_ENSURE_ARG_POINTER(aState); + + // Set the value to the stored state. + nsAutoString stateString; + nsresult res = aState->GetStateProperty(NS_LITERAL_STRING("value"), stateString); + NS_ENSURE_SUCCESS(res, res); + + return SetProperty(aPresContext, nsHTMLAtoms::value, stateString); +} diff --git a/layout/forms/nsGfxButtonControlFrame.h b/layout/forms/nsGfxButtonControlFrame.h index 299eafa91924..02b1c5800832 100644 --- a/layout/forms/nsGfxButtonControlFrame.h +++ b/layout/forms/nsGfxButtonControlFrame.h @@ -28,14 +28,18 @@ #include "nsCOMPtr.h" #include "nsIAnonymousContentCreator.h" #include "nsITextContent.h" +#include "nsIStatefulFrame.h" // Class which implements the input[type=button, reset, submit] and // browse button for input[type=file]. // The label for button is specified through generated content // in the ua.css file. +class nsIPresState; + class nsGfxButtonControlFrame : public nsHTMLButtonControlFrame, - public nsIAnonymousContentCreator + public nsIAnonymousContentCreator, + public nsIStatefulFrame { public: nsGfxButtonControlFrame(); @@ -90,6 +94,12 @@ protected: virtual PRBool IsReset(PRInt32 type); virtual PRBool IsSubmit(PRInt32 type); virtual PRBool IsBrowse(PRInt32 type); // Browse button of file input + +//nsIStatefulFrame + NS_IMETHOD GetStateType(nsIPresContext* aPresContext, nsIStatefulFrame::StateType* aStateType); + NS_IMETHOD SaveState(nsIPresContext* aPresContext, nsIPresState** aState); + NS_IMETHOD RestoreState(nsIPresContext* aPresContext, nsIPresState* aState); + private: NS_IMETHOD_(nsrefcnt) AddRef() { return NS_OK; } NS_IMETHOD_(nsrefcnt) Release() { return NS_OK; } diff --git a/layout/forms/nsHTMLButtonControlFrame.cpp b/layout/forms/nsHTMLButtonControlFrame.cpp index a64202530e41..acd25703b7c3 100644 --- a/layout/forms/nsHTMLButtonControlFrame.cpp +++ b/layout/forms/nsHTMLButtonControlFrame.cpp @@ -815,12 +815,23 @@ nsresult nsHTMLButtonControlFrame::RequiresWidget(PRBool& aRequiresWidget) NS_IMETHODIMP nsHTMLButtonControlFrame::SetProperty(nsIPresContext* aPresContext, nsIAtom* aName, const nsAReadableString& aValue) { + if (nsHTMLAtoms::value == aName) { + nsCOMPtr formControl(do_QueryInterface(mContent)); + if (formControl) { + return formControl->SetAttribute(kNameSpaceID_None, nsHTMLAtoms::value, aValue, PR_TRUE); + } + } return NS_OK; } NS_IMETHODIMP nsHTMLButtonControlFrame::GetProperty(nsIAtom* aName, nsAWritableString& aValue) { - aValue.Truncate(); + if (nsHTMLAtoms::value == aName) { + nsCOMPtr formControl(do_QueryInterface(mContent)); + if (formControl) { + formControl->GetAttribute(kNameSpaceID_None, nsHTMLAtoms::value, aValue); + } + } return NS_OK; } diff --git a/layout/html/forms/src/nsGfxButtonControlFrame.cpp b/layout/html/forms/src/nsGfxButtonControlFrame.cpp index 082db4933567..6cf267e33c7a 100644 --- a/layout/html/forms/src/nsGfxButtonControlFrame.cpp +++ b/layout/html/forms/src/nsGfxButtonControlFrame.cpp @@ -28,6 +28,10 @@ #include "nsISupportsArray.h" #include "nsINameSpaceManager.h" +// Saving PresState +#include "nsIPresState.h" +#include "nsIHTMLContent.h" + const nscoord kSuggestedNotSet = -1; nsGfxButtonControlFrame::nsGfxButtonControlFrame() @@ -456,14 +460,17 @@ nsGfxButtonControlFrame::CreateFrameFor(nsIPresContext* aPresContext, NS_IMETHODIMP nsGfxButtonControlFrame::QueryInterface(const nsIID& aIID, void** aInstancePtr) { - NS_PRECONDITION(0 != aInstancePtr, "null ptr"); - if (NULL == aInstancePtr) { - return NS_ERROR_NULL_POINTER; - } else if (aIID.Equals(NS_GET_IID(nsIAnonymousContentCreator))) { - *aInstancePtr = (void*)(nsIAnonymousContentCreator*) this; - return NS_OK; + NS_ENSURE_ARG_POINTER(aInstancePtr); + + if (aIID.Equals(NS_GET_IID(nsIAnonymousContentCreator))) { + *aInstancePtr = NS_STATIC_CAST(nsIAnonymousContentCreator*, this); + } else if (aIID.Equals(NS_GET_IID(nsIStatefulFrame))) { + *aInstancePtr = NS_STATIC_CAST(nsIStatefulFrame*, this); + } else { + return nsHTMLButtonControlFrame::QueryInterface(aIID, aInstancePtr); } - return nsHTMLButtonControlFrame::QueryInterface(aIID, aInstancePtr); + + return NS_OK; } // Initially we hardcoded the default strings here. @@ -665,4 +672,50 @@ nsGfxButtonControlFrame::HandleEvent(nsIPresContext* aPresContext, return NS_OK; } +NS_IMETHODIMP +nsGfxButtonControlFrame::GetStateType(nsIPresContext* aPresContext, nsIStatefulFrame::StateType* aStateType) +{ + *aStateType = nsIStatefulFrame::eTextType; + return NS_OK; +} +NS_IMETHODIMP +nsGfxButtonControlFrame::SaveState(nsIPresContext* aPresContext, nsIPresState** aState) +{ + NS_ENSURE_ARG_POINTER(aState); + + // Get the value string + nsAutoString stateString; + nsresult res = GetProperty(nsHTMLAtoms::value, stateString); + NS_ENSURE_SUCCESS(res, res); + + // Compare to default value, and only save if needed (Bug 62713) + nsAutoString defaultStateString; + nsCOMPtr formControl(do_QueryInterface(mContent)); + if (formControl) { + formControl->GetAttribute(kNameSpaceID_None, nsHTMLAtoms::value, defaultStateString); + } + + if (! stateString.Equals(defaultStateString)) { + + // Construct a pres state and store value in it. + res = NS_NewPresState(aState); + NS_ENSURE_SUCCESS(res, res); + res = (*aState)->SetStateProperty(NS_LITERAL_STRING("value"), stateString); + } + + return res; +} + +NS_IMETHODIMP +nsGfxButtonControlFrame::RestoreState(nsIPresContext* aPresContext, nsIPresState* aState) +{ + NS_ENSURE_ARG_POINTER(aState); + + // Set the value to the stored state. + nsAutoString stateString; + nsresult res = aState->GetStateProperty(NS_LITERAL_STRING("value"), stateString); + NS_ENSURE_SUCCESS(res, res); + + return SetProperty(aPresContext, nsHTMLAtoms::value, stateString); +} diff --git a/layout/html/forms/src/nsGfxButtonControlFrame.h b/layout/html/forms/src/nsGfxButtonControlFrame.h index 299eafa91924..02b1c5800832 100644 --- a/layout/html/forms/src/nsGfxButtonControlFrame.h +++ b/layout/html/forms/src/nsGfxButtonControlFrame.h @@ -28,14 +28,18 @@ #include "nsCOMPtr.h" #include "nsIAnonymousContentCreator.h" #include "nsITextContent.h" +#include "nsIStatefulFrame.h" // Class which implements the input[type=button, reset, submit] and // browse button for input[type=file]. // The label for button is specified through generated content // in the ua.css file. +class nsIPresState; + class nsGfxButtonControlFrame : public nsHTMLButtonControlFrame, - public nsIAnonymousContentCreator + public nsIAnonymousContentCreator, + public nsIStatefulFrame { public: nsGfxButtonControlFrame(); @@ -90,6 +94,12 @@ protected: virtual PRBool IsReset(PRInt32 type); virtual PRBool IsSubmit(PRInt32 type); virtual PRBool IsBrowse(PRInt32 type); // Browse button of file input + +//nsIStatefulFrame + NS_IMETHOD GetStateType(nsIPresContext* aPresContext, nsIStatefulFrame::StateType* aStateType); + NS_IMETHOD SaveState(nsIPresContext* aPresContext, nsIPresState** aState); + NS_IMETHOD RestoreState(nsIPresContext* aPresContext, nsIPresState* aState); + private: NS_IMETHOD_(nsrefcnt) AddRef() { return NS_OK; } NS_IMETHOD_(nsrefcnt) Release() { return NS_OK; } diff --git a/layout/html/forms/src/nsHTMLButtonControlFrame.cpp b/layout/html/forms/src/nsHTMLButtonControlFrame.cpp index a64202530e41..acd25703b7c3 100644 --- a/layout/html/forms/src/nsHTMLButtonControlFrame.cpp +++ b/layout/html/forms/src/nsHTMLButtonControlFrame.cpp @@ -815,12 +815,23 @@ nsresult nsHTMLButtonControlFrame::RequiresWidget(PRBool& aRequiresWidget) NS_IMETHODIMP nsHTMLButtonControlFrame::SetProperty(nsIPresContext* aPresContext, nsIAtom* aName, const nsAReadableString& aValue) { + if (nsHTMLAtoms::value == aName) { + nsCOMPtr formControl(do_QueryInterface(mContent)); + if (formControl) { + return formControl->SetAttribute(kNameSpaceID_None, nsHTMLAtoms::value, aValue, PR_TRUE); + } + } return NS_OK; } NS_IMETHODIMP nsHTMLButtonControlFrame::GetProperty(nsIAtom* aName, nsAWritableString& aValue) { - aValue.Truncate(); + if (nsHTMLAtoms::value == aName) { + nsCOMPtr formControl(do_QueryInterface(mContent)); + if (formControl) { + formControl->GetAttribute(kNameSpaceID_None, nsHTMLAtoms::value, aValue); + } + } return NS_OK; }