gecko-dev/layout/forms/nsButtonFrameRenderer.cpp

342 строки
10 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (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 the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsButtonFrameRenderer.h"
1999-02-19 21:23:02 +03:00
#include "nsIRenderingContext.h"
#include "nsCSSRendering.h"
#include "nsIPresContext.h"
#include "nsIView.h"
#include "nsIViewManager.h"
1999-03-13 02:47:52 +03:00
#include "nsHTMLAtoms.h"
#include "nsCSSPseudoElements.h"
#include "nsINameSpaceManager.h"
#include "nsStyleSet.h"
1999-02-19 21:23:02 +03:00
#define ACTIVE "active"
#define HOVER "hover"
#define FOCUS "focus"
MOZ_DECL_CTOR_COUNTER(nsButtonFrameRenderer)
1999-02-19 21:23:02 +03:00
nsButtonFrameRenderer::nsButtonFrameRenderer()
{
MOZ_COUNT_CTOR(nsButtonFrameRenderer);
1999-02-19 21:23:02 +03:00
}
nsButtonFrameRenderer::~nsButtonFrameRenderer()
{
MOZ_COUNT_DTOR(nsButtonFrameRenderer);
}
void
nsButtonFrameRenderer::SetFrame(nsFrame* aFrame, nsIPresContext* aPresContext)
1999-02-19 21:23:02 +03:00
{
mFrame = aFrame;
ReResolveStyles(aPresContext);
1999-02-19 21:23:02 +03:00
}
nsIFrame*
nsButtonFrameRenderer::GetFrame()
{
return mFrame;
}
void
nsButtonFrameRenderer::SetDisabled(PRBool aDisabled, PRBool notify)
{
1999-03-13 02:47:52 +03:00
if (aDisabled)
2003-07-04 16:50:32 +04:00
mFrame->GetContent()->SetAttr(kNameSpaceID_None, nsHTMLAtoms::disabled, nsString(),
notify);
1999-03-13 02:47:52 +03:00
else
2003-07-04 16:50:32 +04:00
mFrame->GetContent()->UnsetAttr(kNameSpaceID_None, nsHTMLAtoms::disabled, notify);
}
PRBool
nsButtonFrameRenderer::isDisabled()
{
1999-03-13 02:47:52 +03:00
// get the content
nsAutoString value;
2003-07-04 16:50:32 +04:00
if (NS_CONTENT_ATTR_HAS_VALUE ==
mFrame->GetContent()->GetAttr(kNameSpaceID_None, nsHTMLAtoms::disabled, value))
1999-03-13 02:47:52 +03:00
return PR_TRUE;
return PR_FALSE;
}
1999-02-19 21:23:02 +03:00
void
nsButtonFrameRenderer::PaintButton (nsIPresContext* aPresContext,
nsIRenderingContext& aRenderingContext,
const nsRect& aDirtyRect,
const nsRect& aRect)
1999-02-19 21:23:02 +03:00
{
//printf("painted width='%d' height='%d'\n",aRect.width, aRect.height);
// draw the border and background inside the focus and outline borders
PaintBorderAndBackground(aPresContext, aRenderingContext, aDirtyRect, aRect);
// draw the focus and outline borders
PaintOutlineAndFocusBorders(aPresContext, aRenderingContext, aDirtyRect, aRect);
1999-02-19 21:23:02 +03:00
}
void
nsButtonFrameRenderer::PaintOutlineAndFocusBorders(nsIPresContext* aPresContext,
nsIRenderingContext& aRenderingContext,
const nsRect& aDirtyRect,
const nsRect& aRect)
1999-02-19 21:23:02 +03:00
{
// once we have all that let draw the focus if we have it. We will need to draw 2 focuses.
// the inner and the outer. This is so we can do any kind of look and feel some buttons have
// focus on the outside like mac and motif. While others like windows have it inside (dotted line).
// Usually only one will be specifed. But I guess you could have both if you wanted to.
1999-02-19 21:23:02 +03:00
nsRect rect;
1999-02-19 21:23:02 +03:00
if (mOuterFocusStyle) {
// ---------- paint the outer focus border -------------
1999-02-19 21:23:02 +03:00
GetButtonOuterFocusRect(aRect, rect);
1999-02-19 21:23:02 +03:00
const nsStyleBorder* border = mOuterFocusStyle->GetStyleBorder();
nsCSSRendering::PaintBorder(aPresContext, aRenderingContext, mFrame,
aDirtyRect, rect, *border, mOuterFocusStyle, 0);
}
1999-02-19 21:23:02 +03:00
if (mInnerFocusStyle) {
// ---------- paint the inner focus border -------------
1999-02-19 21:23:02 +03:00
GetButtonInnerFocusRect(aRect, rect);
1999-02-19 21:23:02 +03:00
const nsStyleBorder* border = mInnerFocusStyle->GetStyleBorder();
nsCSSRendering::PaintBorder(aPresContext, aRenderingContext, mFrame,
aDirtyRect, rect, *border, mInnerFocusStyle, 0);
}
1999-02-19 21:23:02 +03:00
}
void
nsButtonFrameRenderer::PaintBorderAndBackground(nsIPresContext* aPresContext,
nsIRenderingContext& aRenderingContext,
const nsRect& aDirtyRect,
const nsRect& aRect)
1999-02-19 21:23:02 +03:00
{
// get the button rect this is inside the focus and outline rects
nsRect buttonRect;
GetButtonRect(aRect, buttonRect);
1999-02-19 21:23:02 +03:00
nsStyleContext* context = mFrame->GetStyleContext();
1999-02-19 21:23:02 +03:00
const nsStyleBorder* border = context->GetStyleBorder();
const nsStylePadding* padding = context->GetStylePadding();
nsCSSRendering::PaintBackground(aPresContext, aRenderingContext, mFrame,
aDirtyRect, buttonRect, *border, *padding,
PR_FALSE);
nsCSSRendering::PaintBorder(aPresContext, aRenderingContext, mFrame,
aDirtyRect, buttonRect, *border, context, 0);
}
1999-02-19 21:23:02 +03:00
void
nsButtonFrameRenderer::GetButtonOutlineRect(const nsRect& aRect, nsRect& outlineRect)
{
outlineRect = aRect;
outlineRect.Inflate(GetButtonOutlineBorderAndPadding());
}
1999-02-19 21:23:02 +03:00
void
nsButtonFrameRenderer::GetButtonOuterFocusRect(const nsRect& aRect, nsRect& focusRect)
{
focusRect = aRect;
}
1999-02-19 21:23:02 +03:00
void
nsButtonFrameRenderer::GetButtonRect(const nsRect& aRect, nsRect& r)
{
r = aRect;
r.Deflate(GetButtonOuterFocusBorderAndPadding());
}
1999-02-19 21:23:02 +03:00
void
nsButtonFrameRenderer::GetButtonInnerFocusRect(const nsRect& aRect, nsRect& focusRect)
{
GetButtonRect(aRect, focusRect);
focusRect.Deflate(GetButtonBorderAndPadding());
focusRect.Deflate(GetButtonInnerFocusMargin());
}
1999-02-19 21:23:02 +03:00
void
nsButtonFrameRenderer::GetButtonContentRect(const nsRect& aRect, nsRect& r)
{
GetButtonInnerFocusRect(aRect, r);
r.Deflate(GetButtonInnerFocusBorderAndPadding());
}
1999-02-19 21:23:02 +03:00
nsMargin
nsButtonFrameRenderer::GetButtonOuterFocusBorderAndPadding()
{
nsMargin focusBorderAndPadding(0,0,0,0);
1999-02-19 21:23:02 +03:00
if (mOuterFocusStyle) {
nsStyleBorderPadding bPad;
mOuterFocusStyle->GetBorderPaddingFor(bPad);
if (!bPad.GetBorderPadding(focusBorderAndPadding)) {
NS_NOTYETIMPLEMENTED("percentage border");
}
}
return focusBorderAndPadding;
1999-02-19 21:23:02 +03:00
}
nsMargin
nsButtonFrameRenderer::GetButtonBorderAndPadding()
1999-02-19 21:23:02 +03:00
{
nsStyleContext* context = mFrame->GetStyleContext();
nsMargin innerFocusBorderAndPadding(0,0,0,0);
nsStyleBorderPadding bPad;
context->GetBorderPaddingFor(bPad);
if (!bPad.GetBorderPadding(innerFocusBorderAndPadding)) {
NS_NOTYETIMPLEMENTED("percentage border");
}
return innerFocusBorderAndPadding;
1999-02-19 21:23:02 +03:00
}
/**
* Gets the size of the buttons border this is the union of the normal and disabled borders.
*/
nsMargin
nsButtonFrameRenderer::GetButtonInnerFocusMargin()
1999-02-19 21:23:02 +03:00
{
nsMargin innerFocusMargin(0,0,0,0);
1999-02-19 21:23:02 +03:00
if (mInnerFocusStyle) {
// get the outer focus border and padding
const nsStyleMargin* margin = mInnerFocusStyle->GetStyleMargin();
margin->GetMargin(innerFocusMargin);
}
return innerFocusMargin;
1999-02-19 21:23:02 +03:00
}
nsMargin
nsButtonFrameRenderer::GetButtonInnerFocusBorderAndPadding()
1999-02-19 21:23:02 +03:00
{
nsMargin innerFocusBorderAndPadding(0,0,0,0);
1999-02-19 21:23:02 +03:00
if (mInnerFocusStyle) {
// get the outer focus border and padding
nsStyleBorderPadding bPad;
mInnerFocusStyle->GetBorderPaddingFor(bPad);
if (!bPad.GetBorderPadding(innerFocusBorderAndPadding)) {
NS_NOTYETIMPLEMENTED("percentage border");
}
}
return innerFocusBorderAndPadding;
1999-02-19 21:23:02 +03:00
}
nsMargin
nsButtonFrameRenderer::GetButtonOutlineBorderAndPadding()
1999-02-19 21:23:02 +03:00
{
nsMargin borderAndPadding(0,0,0,0);
return borderAndPadding;
1999-02-19 21:23:02 +03:00
}
1999-03-27 03:58:29 +03:00
// gets the full size of our border with all the focus borders
nsMargin
nsButtonFrameRenderer::GetFullButtonBorderAndPadding()
{
1999-03-27 03:58:29 +03:00
return GetAddedButtonBorderAndPadding() + GetButtonBorderAndPadding();
}
// gets all the focus borders and padding that will be added to the regular border
nsMargin
nsButtonFrameRenderer::GetAddedButtonBorderAndPadding()
{
return GetButtonOuterFocusBorderAndPadding() + GetButtonInnerFocusMargin() + GetButtonInnerFocusBorderAndPadding();
}
/**
* Call this when styles change
*/
void
nsButtonFrameRenderer::ReResolveStyles(nsIPresContext* aPresContext)
1999-02-19 21:23:02 +03:00
{
// get all the styles
nsStyleContext* context = mFrame->GetStyleContext();
nsStyleSet *styleSet = aPresContext->StyleSet();
// style for the inner such as a dotted line (Windows)
mInnerFocusStyle = styleSet->ProbePseudoStyleFor(mFrame->GetContent(),
nsCSSPseudoElements::mozFocusInner,
context);
// style for outer focus like a ridged border (MAC).
mOuterFocusStyle = styleSet->ProbePseudoStyleFor(mFrame->GetContent(),
nsCSSPseudoElements::mozFocusOuter,
context);
1999-02-19 21:23:02 +03:00
}
nsStyleContext*
nsButtonFrameRenderer::GetStyleContext(PRInt32 aIndex) const
{
switch (aIndex) {
case NS_BUTTON_RENDERER_FOCUS_INNER_CONTEXT_INDEX:
return mInnerFocusStyle;
case NS_BUTTON_RENDERER_FOCUS_OUTER_CONTEXT_INDEX:
return mOuterFocusStyle;
1999-09-21 11:56:18 +04:00
default:
return nsnull;
}
}
void
nsButtonFrameRenderer::SetStyleContext(PRInt32 aIndex, nsStyleContext* aStyleContext)
{
switch (aIndex) {
case NS_BUTTON_RENDERER_FOCUS_INNER_CONTEXT_INDEX:
mInnerFocusStyle = aStyleContext;
break;
case NS_BUTTON_RENDERER_FOCUS_OUTER_CONTEXT_INDEX:
mOuterFocusStyle = aStyleContext;
break;
}
}