1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : //
39 : // David Hyatt & Eric Vaughan
40 : // Netscape Communications
41 : //
42 : // See documentation in associated header file
43 : //
44 :
45 : #include "nsProgressMeterFrame.h"
46 : #include "nsCSSRendering.h"
47 : #include "nsIContent.h"
48 : #include "nsPresContext.h"
49 : #include "nsGkAtoms.h"
50 : #include "nsINameSpaceManager.h"
51 : #include "nsCOMPtr.h"
52 : #include "nsBoxLayoutState.h"
53 : #include "nsIReflowCallback.h"
54 : #include "nsContentUtils.h"
55 : //
56 : // NS_NewToolbarFrame
57 : //
58 : // Creates a new Toolbar frame and returns it
59 : //
60 : nsIFrame*
61 0 : NS_NewProgressMeterFrame (nsIPresShell* aPresShell, nsStyleContext* aContext)
62 : {
63 0 : return new (aPresShell) nsProgressMeterFrame(aPresShell, aContext);
64 : }
65 :
66 0 : NS_IMPL_FRAMEARENA_HELPERS(nsProgressMeterFrame)
67 :
68 : //
69 : // nsProgressMeterFrame dstr
70 : //
71 : // Cleanup, if necessary
72 : //
73 0 : nsProgressMeterFrame :: ~nsProgressMeterFrame ( )
74 : {
75 0 : }
76 :
77 : class nsAsyncProgressMeterInit : public nsIReflowCallback
78 0 : {
79 : public:
80 0 : nsAsyncProgressMeterInit(nsIFrame* aFrame) : mWeakFrame(aFrame) {}
81 :
82 0 : virtual bool ReflowFinished()
83 : {
84 0 : bool shouldFlush = false;
85 0 : nsIFrame* frame = mWeakFrame.GetFrame();
86 0 : if (frame) {
87 0 : nsAutoScriptBlocker scriptBlocker;
88 0 : frame->AttributeChanged(kNameSpaceID_None, nsGkAtoms::value, 0);
89 0 : shouldFlush = true;
90 : }
91 0 : delete this;
92 0 : return shouldFlush;
93 : }
94 :
95 0 : virtual void ReflowCallbackCanceled()
96 : {
97 0 : delete this;
98 0 : }
99 :
100 : nsWeakFrame mWeakFrame;
101 : };
102 :
103 : NS_IMETHODIMP
104 0 : nsProgressMeterFrame::DoLayout(nsBoxLayoutState& aState)
105 : {
106 0 : if (mNeedsReflowCallback) {
107 0 : nsIReflowCallback* cb = new nsAsyncProgressMeterInit(this);
108 0 : if (cb) {
109 0 : PresContext()->PresShell()->PostReflowCallback(cb);
110 : }
111 0 : mNeedsReflowCallback = false;
112 : }
113 0 : return nsBoxFrame::DoLayout(aState);
114 : }
115 :
116 : NS_IMETHODIMP
117 0 : nsProgressMeterFrame::AttributeChanged(PRInt32 aNameSpaceID,
118 : nsIAtom* aAttribute,
119 : PRInt32 aModType)
120 : {
121 0 : NS_ASSERTION(!nsContentUtils::IsSafeToRunScript(),
122 : "Scripts not blocked in nsProgressMeterFrame::AttributeChanged!");
123 : nsresult rv = nsBoxFrame::AttributeChanged(aNameSpaceID, aAttribute,
124 0 : aModType);
125 0 : if (NS_OK != rv) {
126 0 : return rv;
127 : }
128 :
129 : // did the progress change?
130 0 : if (nsGkAtoms::value == aAttribute || nsGkAtoms::max == aAttribute) {
131 0 : nsIFrame* barChild = GetFirstPrincipalChild();
132 0 : if (!barChild) return NS_OK;
133 0 : nsIFrame* remainderChild = barChild->GetNextSibling();
134 0 : if (!remainderChild) return NS_OK;
135 0 : nsCOMPtr<nsIContent> remainderContent = remainderChild->GetContent();
136 0 : if (!remainderContent) return NS_OK;
137 :
138 0 : nsAutoString value, maxValue;
139 0 : mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::value, value);
140 0 : mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::max, maxValue);
141 :
142 : PRInt32 error;
143 0 : PRInt32 flex = value.ToInteger(&error);
144 0 : PRInt32 maxFlex = maxValue.ToInteger(&error);
145 0 : if (NS_FAILED(error) || maxValue.IsEmpty()) {
146 0 : maxFlex = 100;
147 : }
148 0 : if (maxFlex < 1) {
149 0 : maxFlex = 1;
150 : }
151 0 : if (flex < 0) {
152 0 : flex = 0;
153 : }
154 0 : if (flex > maxFlex) {
155 0 : flex = maxFlex;
156 : }
157 :
158 : nsContentUtils::AddScriptRunner(new nsSetAttrRunnable(
159 0 : barChild->GetContent(), nsGkAtoms::flex, flex));
160 : nsContentUtils::AddScriptRunner(new nsSetAttrRunnable(
161 0 : remainderContent, nsGkAtoms::flex, maxFlex - flex));
162 : nsContentUtils::AddScriptRunner(new nsReflowFrameRunnable(
163 0 : this, nsIPresShell::eTreeChange, NS_FRAME_IS_DIRTY));
164 : }
165 0 : return NS_OK;
166 : }
167 :
168 : #ifdef NS_DEBUG
169 : NS_IMETHODIMP
170 0 : nsProgressMeterFrame::GetFrameName(nsAString& aResult) const
171 : {
172 0 : return MakeFrameName(NS_LITERAL_STRING("ProgressMeter"), aResult);
173 : }
174 : #endif
|