1999-02-12 02:49:08 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2000-11-01 21:54:16 +03:00
|
|
|
|
1999-02-12 02:49:08 +03:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* 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/
|
1999-02-12 02:49:08 +03:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* 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.
|
1999-02-12 02:49:08 +03:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-02-12 02:49:08 +03:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:40:37 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2000-02-03 01:24:56 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
1999-02-12 02:49:08 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsITimerCallback.h"
|
|
|
|
|
1999-07-15 02:18:29 +04:00
|
|
|
#include "nsIComponentManager.h"
|
1999-02-12 03:55:46 +03:00
|
|
|
#include "nsIFrameSelection.h"
|
1999-02-12 02:49:08 +03:00
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMRange.h"
|
2000-09-14 15:45:01 +04:00
|
|
|
#include "nsISelection.h"
|
|
|
|
#include "nsISelectionPrivate.h"
|
1999-02-12 02:49:08 +03:00
|
|
|
#include "nsIDOMCharacterData.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsIRenderingContext.h"
|
1999-03-02 07:26:49 +03:00
|
|
|
#include "nsIDeviceContext.h"
|
1999-02-12 02:49:08 +03:00
|
|
|
#include "nsIView.h"
|
2000-09-07 09:26:25 +04:00
|
|
|
#include "nsIScrollableView.h"
|
1999-03-02 07:26:49 +03:00
|
|
|
#include "nsIViewManager.h"
|
1999-02-12 02:49:08 +03:00
|
|
|
#include "nsIPresContext.h"
|
1999-07-15 02:18:29 +04:00
|
|
|
#include "nsILookAndFeel.h"
|
2000-09-07 09:26:25 +04:00
|
|
|
#include "nsWidgetsCID.h" // for NS_LOOKANDFEEL_CID
|
1999-09-15 03:41:19 +04:00
|
|
|
#include "nsBlockFrame.h"
|
2000-04-27 11:37:12 +04:00
|
|
|
#include "nsISelectionController.h"
|
1999-02-12 02:49:08 +03:00
|
|
|
|
|
|
|
#include "nsCaret.h"
|
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
// Because of drawing issues, we currently always make a new RC. See bug 28068
|
|
|
|
// Before removing this, stuff will need to be fixed and tested on all platforms.
|
|
|
|
// For example, turning this off on Mac right now causes drawing problems on pages
|
|
|
|
// with form elements.
|
|
|
|
#define DONT_REUSE_RENDERING_CONTEXT
|
1999-02-12 02:49:08 +03:00
|
|
|
|
2001-03-21 04:16:22 +03:00
|
|
|
#ifdef IBMBIDI
|
|
|
|
//-------------------------------IBM BIDI--------------------------------------
|
|
|
|
// Mamdouh : Modifiaction of the caret to work with Bidi in the LTR and RTL
|
|
|
|
#include "nsIPref.h"
|
|
|
|
#include "nsLayoutAtoms.h"
|
|
|
|
//------------------------------END OF IBM BIDI--------------------------------
|
|
|
|
#endif //IBMBIDI
|
|
|
|
|
1999-07-15 02:18:29 +04:00
|
|
|
static NS_DEFINE_IID(kLookAndFeelCID, NS_LOOKANDFEEL_CID);
|
|
|
|
|
1999-02-12 02:49:08 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
1999-10-09 00:41:19 +04:00
|
|
|
|
1999-02-12 02:49:08 +03:00
|
|
|
nsCaret::nsCaret()
|
2000-09-07 09:26:25 +04:00
|
|
|
: mPresShell(nsnull)
|
|
|
|
, mBlinkRate(500)
|
2000-07-29 02:12:45 +04:00
|
|
|
, mCaretTwipsWidth(-1)
|
|
|
|
, mCaretPixelsWidth(1)
|
2000-09-07 09:26:25 +04:00
|
|
|
, mVisible(PR_FALSE)
|
|
|
|
, mDrawn(PR_FALSE)
|
2001-03-21 08:32:22 +03:00
|
|
|
, mReadOnly(PR_FALSE)
|
2001-04-05 23:53:13 +04:00
|
|
|
, mShowDuringSelection(PR_FALSE)
|
1999-04-02 03:57:35 +04:00
|
|
|
, mLastCaretFrame(nsnull)
|
2000-09-08 00:21:22 +04:00
|
|
|
, mLastCaretView(nsnull)
|
1999-04-02 03:57:35 +04:00
|
|
|
, mLastContentOffset(0)
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
nsCaret::~nsCaret()
|
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
KillTimer();
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
1999-07-15 02:18:29 +04:00
|
|
|
NS_IMETHODIMP nsCaret::Init(nsIPresShell *inPresShell)
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
if (!inPresShell)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
mPresShell = getter_AddRefs(NS_GetWeakReference(inPresShell)); // the presshell owns us, so no addref
|
2000-04-27 11:37:12 +04:00
|
|
|
|
1999-07-15 02:18:29 +04:00
|
|
|
nsILookAndFeel* touchyFeely;
|
2000-02-03 01:24:56 +03:00
|
|
|
if (NS_SUCCEEDED(nsComponentManager::CreateInstance(kLookAndFeelCID, nsnull, NS_GET_IID(nsILookAndFeel), (void**)&touchyFeely)))
|
1999-07-15 02:18:29 +04:00
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
PRInt32 tempInt;
|
1999-07-15 02:18:29 +04:00
|
|
|
|
2000-07-29 02:12:45 +04:00
|
|
|
if (NS_SUCCEEDED(touchyFeely->GetMetric(nsILookAndFeel::eMetric_SingleLineCaretWidth, tempInt)))
|
|
|
|
mCaretTwipsWidth = (nscoord)tempInt;
|
1999-07-15 02:18:29 +04:00
|
|
|
if (NS_SUCCEEDED(touchyFeely->GetMetric(nsILookAndFeel::eMetric_CaretBlinkTime, tempInt)))
|
|
|
|
mBlinkRate = (PRUint32)tempInt;
|
2001-04-05 23:53:13 +04:00
|
|
|
if (NS_SUCCEEDED(touchyFeely->GetMetric(nsILookAndFeel::eMetric_ShowCaretDuringSelection, tempInt)))
|
|
|
|
mShowDuringSelection = tempInt ? PR_TRUE : PR_FALSE;
|
1999-07-15 02:18:29 +04:00
|
|
|
|
|
|
|
NS_RELEASE(touchyFeely);
|
|
|
|
}
|
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
// get the selection from the pres shell, and set ourselves up as a selection
|
|
|
|
// listener
|
|
|
|
|
2000-09-14 15:45:01 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> domSelection;
|
|
|
|
nsCOMPtr<nsISelectionPrivate> privateSelection;
|
2000-04-27 11:37:12 +04:00
|
|
|
nsCOMPtr<nsISelectionController> selCon = do_QueryReferent(mPresShell);
|
|
|
|
if (selCon)
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
2000-04-27 11:37:12 +04:00
|
|
|
if (NS_SUCCEEDED(selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(domSelection))))
|
|
|
|
{
|
2000-09-14 15:45:01 +04:00
|
|
|
privateSelection = do_QueryInterface(domSelection);
|
|
|
|
privateSelection->AddSelectionListener(this);
|
2000-05-04 12:35:42 +04:00
|
|
|
mDomSelectionWeak = getter_AddRefs( NS_GetWeakReference(domSelection) );
|
2000-09-07 09:26:25 +04:00
|
|
|
}
|
2000-04-27 11:37:12 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return NS_ERROR_FAILURE;
|
2000-09-07 09:26:25 +04:00
|
|
|
|
|
|
|
// set up the blink timer
|
|
|
|
if (mVisible)
|
|
|
|
{
|
|
|
|
nsresult err = StartBlinking();
|
|
|
|
if (NS_FAILED(err))
|
|
|
|
return err;
|
|
|
|
}
|
2001-03-21 04:16:22 +03:00
|
|
|
#ifdef IBMBIDI
|
|
|
|
PRBool isRTL;
|
|
|
|
mBidiKeyboard = do_GetService("@mozilla.org/widget/bidikeyboard;1");
|
|
|
|
mBidiKeyboard->IsLangRTL(&isRTL);
|
|
|
|
mKeyboardRTL = isRTL;
|
|
|
|
#endif
|
2000-09-07 09:26:25 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
NS_IMPL_ADDREF(nsCaret);
|
|
|
|
NS_IMPL_RELEASE(nsCaret);
|
|
|
|
//-----------------------------------------------------------------------------
|
1999-04-02 03:57:35 +04:00
|
|
|
NS_IMETHODIMP nsCaret::QueryInterface(const nsIID& aIID,
|
1999-02-12 02:49:08 +03:00
|
|
|
void** aInstancePtrResult)
|
|
|
|
{
|
2000-09-14 15:45:01 +04:00
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
NS_PRECONDITION(aInstancePtrResult, "null pointer");
|
|
|
|
if (!aInstancePtrResult)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsISupports* foundInterface;
|
|
|
|
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsISupports)))
|
|
|
|
foundInterface = (nsISupports*)(nsICaret*)this; // whoo boy
|
|
|
|
else if (aIID.Equals(NS_GET_IID(nsICaret)))
|
|
|
|
foundInterface = (nsICaret*)this;
|
2000-09-14 15:45:01 +04:00
|
|
|
else if (aIID.Equals(NS_GET_IID(nsISelectionListener)))
|
|
|
|
foundInterface = (nsISelectionListener*)this;
|
2000-09-07 09:26:25 +04:00
|
|
|
else
|
|
|
|
foundInterface = nsnull;
|
|
|
|
|
|
|
|
nsresult status;
|
|
|
|
if (! foundInterface)
|
|
|
|
status = NS_NOINTERFACE;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NS_ADDREF(foundInterface);
|
|
|
|
status = NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aInstancePtrResult = foundInterface;
|
|
|
|
return status;
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2000-09-14 15:45:01 +04:00
|
|
|
NS_IMETHODIMP nsCaret::SetCaretDOMSelection(nsISelection *aDOMSel)
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
2000-06-01 06:37:40 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aDOMSel);
|
2000-05-11 08:25:43 +04:00
|
|
|
mDomSelectionWeak = getter_AddRefs( NS_GetWeakReference(aDOMSel) ); // weak reference to pres shell
|
2000-06-01 06:37:40 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
NS_IMETHODIMP nsCaret::SetCaretVisible(PRBool inMakeVisible)
|
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
mVisible = inMakeVisible;
|
|
|
|
nsresult err = NS_OK;
|
|
|
|
if (mVisible)
|
|
|
|
err = StartBlinking();
|
|
|
|
else
|
|
|
|
err = StopBlinking();
|
|
|
|
|
|
|
|
return err;
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
2000-06-01 06:37:40 +04:00
|
|
|
NS_IMETHODIMP nsCaret::GetCaretVisible(PRBool *outMakeVisible)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(outMakeVisible);
|
|
|
|
*outMakeVisible = mVisible;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-02-12 02:49:08 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2000-06-01 06:37:40 +04:00
|
|
|
NS_IMETHODIMP nsCaret::SetCaretReadOnly(PRBool inMakeReadonly)
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
mReadOnly = inMakeReadonly;
|
|
|
|
return NS_OK;
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
1999-02-13 07:47:45 +03:00
|
|
|
|
1999-04-28 01:59:18 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-02-05 18:43:24 +03:00
|
|
|
NS_IMETHODIMP nsCaret::GetCaretCoordinates(EViewCoordinates aRelativeToType, nsISelection *aDOMSel, nsRect *outCoordinates, PRBool *outIsCollapsed)
|
1999-04-28 01:59:18 +04:00
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
if (!mPresShell)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2001-02-05 18:43:24 +03:00
|
|
|
if (!outCoordinates || !outIsCollapsed)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
2000-09-14 15:45:01 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> domSelection = aDOMSel;
|
|
|
|
nsCOMPtr<nsISelectionPrivate> privateSelection(do_QueryInterface(domSelection));
|
2000-04-27 11:37:12 +04:00
|
|
|
nsresult err;
|
2000-09-07 09:26:25 +04:00
|
|
|
if (!domSelection)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED; // no selection
|
2001-02-05 18:43:24 +03:00
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
// fill in defaults for failure
|
2001-02-05 18:43:24 +03:00
|
|
|
outCoordinates->x = -1;
|
|
|
|
outCoordinates->y = -1;
|
|
|
|
outCoordinates->width = -1;
|
|
|
|
outCoordinates->height = -1;
|
|
|
|
*outIsCollapsed = PR_FALSE;
|
2000-09-07 09:26:25 +04:00
|
|
|
|
2001-02-05 18:43:24 +03:00
|
|
|
err = domSelection->GetIsCollapsed(outIsCollapsed);
|
2000-09-07 09:26:25 +04:00
|
|
|
if (NS_FAILED(err))
|
|
|
|
return err;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> focusNode;
|
|
|
|
|
|
|
|
err = domSelection->GetFocusNode(getter_AddRefs(focusNode));
|
|
|
|
if (NS_FAILED(err))
|
|
|
|
return err;
|
|
|
|
if (!focusNode)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
PRInt32 focusOffset;
|
|
|
|
err = domSelection->GetFocusOffset(&focusOffset);
|
|
|
|
if (NS_FAILED(err))
|
|
|
|
return err;
|
|
|
|
|
1999-11-23 23:30:21 +03:00
|
|
|
/*
|
2000-09-07 09:26:25 +04:00
|
|
|
// is this a text node?
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(focusNode);
|
|
|
|
// note that we only work with text nodes here, unlike when drawing the caret.
|
|
|
|
// this is because this routine is intended for IME support, which only cares about text.
|
|
|
|
if (!nodeAsText)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIContent>contentNode = do_QueryInterface(focusNode);
|
|
|
|
if (!contentNode)
|
|
|
|
return NS_ERROR_FAILURE;
|
1999-05-19 02:24:55 +04:00
|
|
|
|
1999-09-11 04:18:02 +04:00
|
|
|
//get frame selection and find out what frame to use...
|
|
|
|
nsCOMPtr<nsIFrameSelection> frameSelection;
|
2000-04-27 11:37:12 +04:00
|
|
|
nsCOMPtr<nsIPresShell> presShell = do_QueryReferent(mPresShell);
|
|
|
|
if (presShell)
|
|
|
|
err = presShell->GetFrameSelection(getter_AddRefs(frameSelection));
|
|
|
|
else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
if (NS_FAILED(err) || !frameSelection)
|
|
|
|
return err?err : NS_ERROR_FAILURE;
|
1999-11-11 02:47:56 +03:00
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
// find the frame that contains the content node that has focus
|
|
|
|
nsIFrame* theFrame = nsnull;
|
|
|
|
PRInt32 theFrameOffset = 0;
|
2000-06-28 08:00:12 +04:00
|
|
|
PRBool hintRight;
|
2000-09-14 15:45:01 +04:00
|
|
|
privateSelection->GetInterlinePosition(&hintRight);//translate hint.
|
2000-06-28 08:00:12 +04:00
|
|
|
nsIFrameSelection::HINT hint;
|
|
|
|
if (hintRight)
|
|
|
|
hint = nsIFrameSelection::HINTRIGHT;
|
|
|
|
else
|
|
|
|
hint = nsIFrameSelection::HINTLEFT;
|
|
|
|
err = frameSelection->GetFrameForNodeOffset(contentNode, focusOffset, hint, &theFrame, &theFrameOffset);
|
2000-09-07 09:26:25 +04:00
|
|
|
if (NS_FAILED(err) || !theFrame)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
nsPoint viewOffset(0, 0);
|
|
|
|
nsRect clipRect;
|
|
|
|
nsIView *drawingView; // views are not refcounted
|
2001-02-05 18:43:24 +03:00
|
|
|
GetViewForRendering(theFrame, aRelativeToType, viewOffset, clipRect, drawingView);
|
2000-09-07 09:26:25 +04:00
|
|
|
if (!drawingView)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
// ramp up to make a rendering context for measuring text.
|
|
|
|
// First, we get the pres context ...
|
|
|
|
nsCOMPtr<nsIPresContext> presContext;
|
|
|
|
err = presShell->GetPresContext(getter_AddRefs(presContext));
|
|
|
|
if (NS_FAILED(err))
|
|
|
|
return err;
|
|
|
|
|
|
|
|
// ... then get a device context
|
|
|
|
nsCOMPtr<nsIDeviceContext> dx;
|
|
|
|
err = presContext->GetDeviceContext(getter_AddRefs(dx));
|
|
|
|
if (NS_FAILED(err))
|
|
|
|
return err;
|
|
|
|
if (!dx)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
// ... then tell it to make a rendering context
|
|
|
|
nsCOMPtr<nsIRenderingContext> rendContext;
|
|
|
|
err = dx->CreateRenderingContext(drawingView, *getter_AddRefs(rendContext));
|
|
|
|
if (NS_FAILED(err))
|
|
|
|
return err;
|
|
|
|
if (!rendContext)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
// now we can measure the offset into the frame.
|
|
|
|
nsPoint framePos(0, 0);
|
|
|
|
theFrame->GetPointFromOffset(presContext, rendContext, theFrameOffset, &framePos);
|
|
|
|
|
|
|
|
nsRect frameRect;
|
|
|
|
theFrame->GetRect(frameRect);
|
|
|
|
|
2001-02-05 18:43:24 +03:00
|
|
|
// we don't need drawingView anymore so reuse that; reset viewOffset values for our purposes
|
|
|
|
if (aRelativeToType == eClosestViewCoordinates)
|
|
|
|
theFrame->GetOffsetFromView(presContext, viewOffset, &drawingView);
|
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
// now add the frame offset to the view offset, and we're done
|
|
|
|
viewOffset += framePos;
|
2001-02-05 18:43:24 +03:00
|
|
|
outCoordinates->x = viewOffset.x;
|
|
|
|
outCoordinates->y = viewOffset.y;
|
|
|
|
outCoordinates->height = frameRect.height;
|
|
|
|
outCoordinates->width = frameRect.width;
|
2000-09-07 09:26:25 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
1999-04-28 01:59:18 +04:00
|
|
|
}
|
|
|
|
|
1999-04-02 03:57:35 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
NS_IMETHODIMP nsCaret::ClearFrameRefs(nsIFrame* aFrame)
|
|
|
|
{
|
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
if (mLastCaretFrame == aFrame)
|
|
|
|
{
|
|
|
|
mLastCaretFrame = nsnull; // frames are not refcounted.
|
2000-09-08 00:21:22 +04:00
|
|
|
mLastCaretView = nsnull;
|
2000-09-07 09:26:25 +04:00
|
|
|
mLastContentOffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDrawn = PR_FALSE; // assume that the view has been cleared, and ensure
|
|
|
|
// that we don't try to use the frame.
|
|
|
|
|
|
|
|
// should we just call StopBlinking() here?
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-04-02 03:57:35 +04:00
|
|
|
}
|
|
|
|
|
2000-06-09 08:22:20 +04:00
|
|
|
NS_IMETHODIMP nsCaret::EraseCaret()
|
|
|
|
{
|
|
|
|
if (mDrawn)
|
|
|
|
DrawCaret();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-04-02 03:57:35 +04:00
|
|
|
|
1999-03-02 07:26:49 +03:00
|
|
|
#ifdef XP_MAC
|
1999-02-12 02:49:08 +03:00
|
|
|
#pragma mark -
|
1999-03-02 07:26:49 +03:00
|
|
|
#endif
|
1999-02-12 02:49:08 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2000-09-14 15:45:01 +04:00
|
|
|
NS_IMETHODIMP nsCaret::NotifySelectionChanged(nsIDOMDocument *, nsISelection *aDomSel, short aReason)
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
2000-09-14 15:45:01 +04:00
|
|
|
if (aReason & nsISelectionListener::MOUSEUP_REASON)//this wont do
|
2000-06-09 08:22:20 +04:00
|
|
|
return NS_OK;
|
2000-09-14 15:45:01 +04:00
|
|
|
if (mVisible)
|
|
|
|
StopBlinking();
|
|
|
|
nsCOMPtr<nsISelection> domSel(do_QueryReferent(mDomSelectionWeak));
|
2000-06-07 05:18:41 +04:00
|
|
|
if (domSel.get() != aDomSel)
|
|
|
|
return NS_OK; //ignore this then.
|
2000-09-07 09:26:25 +04:00
|
|
|
if (mVisible)
|
|
|
|
StartBlinking();
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 07:26:49 +03:00
|
|
|
#ifdef XP_MAC
|
1999-02-12 02:49:08 +03:00
|
|
|
#pragma mark -
|
1999-03-02 07:26:49 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void nsCaret::KillTimer()
|
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
if (mBlinkTimer)
|
|
|
|
{
|
|
|
|
mBlinkTimer->Cancel();
|
|
|
|
}
|
1999-03-02 07:26:49 +03:00
|
|
|
}
|
1999-02-12 02:49:08 +03:00
|
|
|
|
1999-02-17 05:08:01 +03:00
|
|
|
|
1999-02-12 02:49:08 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
1999-02-17 05:08:01 +03:00
|
|
|
nsresult nsCaret::PrimeTimer()
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
KillTimer();
|
|
|
|
|
|
|
|
// set up the blink timer
|
|
|
|
if (!mReadOnly && mBlinkRate > 0)
|
|
|
|
{
|
|
|
|
nsresult err;
|
2000-09-14 03:57:52 +04:00
|
|
|
mBlinkTimer = do_CreateInstance("@mozilla.org/timer;1", &err);
|
2000-09-07 09:26:25 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(err))
|
|
|
|
return err;
|
|
|
|
|
|
|
|
mBlinkTimer->Init(CaretBlinkCallback, this, mBlinkRate, NS_PRIORITY_HIGH, NS_TYPE_REPEATING_PRECISE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-02-17 05:08:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
nsresult nsCaret::StartBlinking()
|
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
PrimeTimer();
|
1999-03-03 04:11:28 +03:00
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
//NS_ASSERTION(!mDrawn, "Caret should not be drawn here");
|
|
|
|
DrawCaret(); // draw it right away
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
nsresult nsCaret::StopBlinking()
|
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
if (mDrawn) // erase the caret if necessary
|
|
|
|
DrawCaret();
|
|
|
|
|
|
|
|
KillTimer();
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
1999-04-02 03:57:35 +04:00
|
|
|
// Get the nsIFrame and the content offset for the current caret position.
|
1999-04-28 01:59:18 +04:00
|
|
|
// Returns PR_TRUE if we should go ahead and draw, PR_FALSE otherwise.
|
1999-04-02 03:57:35 +04:00
|
|
|
//
|
|
|
|
PRBool nsCaret::SetupDrawingFrameAndOffset()
|
1999-02-12 02:49:08 +03:00
|
|
|
{
|
2000-05-04 12:35:42 +04:00
|
|
|
if (!mDomSelectionWeak)
|
|
|
|
return PR_FALSE;
|
2000-09-14 15:45:01 +04:00
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
|
2000-09-14 15:45:01 +04:00
|
|
|
nsCOMPtr<nsISelection> domSelection = do_QueryReferent(mDomSelectionWeak);
|
|
|
|
nsCOMPtr<nsISelectionPrivate> privateSelection(do_QueryInterface(domSelection));
|
2000-09-08 00:21:22 +04:00
|
|
|
if (!domSelection) return PR_FALSE;
|
|
|
|
|
|
|
|
PRBool isCollapsed = PR_FALSE;
|
|
|
|
domSelection->GetIsCollapsed(&isCollapsed);
|
2001-04-05 23:53:13 +04:00
|
|
|
if (!mShowDuringSelection && !isCollapsed) return PR_FALSE;
|
2000-09-07 09:26:25 +04:00
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
// start and end parent should be the same since we are collapsed
|
|
|
|
nsCOMPtr<nsIDOMNode> focusNode;
|
|
|
|
domSelection->GetFocusNode(getter_AddRefs(focusNode));
|
|
|
|
if (!focusNode) return PR_FALSE;
|
|
|
|
|
|
|
|
PRInt32 contentOffset;
|
|
|
|
if (NS_FAILED(domSelection->GetFocusOffset(&contentOffset)))
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> contentNode = do_QueryInterface(focusNode);
|
|
|
|
if (!contentNode) return PR_FALSE;
|
|
|
|
|
|
|
|
//get frame selection and find out what frame to use...
|
|
|
|
nsCOMPtr<nsIPresShell> presShell = do_QueryReferent(mPresShell);
|
|
|
|
if (!presShell)
|
|
|
|
return PR_FALSE;
|
2000-09-07 09:26:25 +04:00
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
nsCOMPtr<nsIFrameSelection> frameSelection;
|
|
|
|
presShell->GetFrameSelection(getter_AddRefs(frameSelection));
|
|
|
|
if (!frameSelection)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
PRBool hintRight;
|
2000-09-14 15:45:01 +04:00
|
|
|
privateSelection->GetInterlinePosition(&hintRight);//translate hint.
|
2000-09-08 00:21:22 +04:00
|
|
|
nsIFrameSelection::HINT hint;
|
|
|
|
hint = (hintRight) ? nsIFrameSelection::HINTRIGHT : nsIFrameSelection::HINTLEFT;
|
|
|
|
|
|
|
|
nsIFrame* theFrame = nsnull;
|
|
|
|
PRInt32 theFrameOffset = 0;
|
|
|
|
|
|
|
|
nsresult rv = frameSelection->GetFrameForNodeOffset(contentNode, contentOffset, hint, &theFrame, &theFrameOffset);
|
|
|
|
if (NS_FAILED(rv) || !theFrame)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
// now we have a frame, check whether it's appropriate to show the caret here
|
|
|
|
const nsStyleUserInterface* userinterface;
|
|
|
|
theFrame->GetStyleData(eStyleStruct_UserInterface, (const nsStyleStruct*&)userinterface);
|
|
|
|
if (userinterface)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
#ifdef SUPPORT_USER_MODIFY
|
|
|
|
// editable content still defaults to NS_STYLE_USER_MODIFY_READ_ONLY at present. See bug 15284
|
|
|
|
(userinterface->mUserModify == NS_STYLE_USER_MODIFY_READ_ONLY) ||
|
|
|
|
#endif
|
|
|
|
(userinterface->mUserInput == NS_STYLE_USER_INPUT_NONE) ||
|
|
|
|
(userinterface->mUserInput == NS_STYLE_USER_INPUT_DISABLED))
|
2000-09-07 09:26:25 +04:00
|
|
|
{
|
2000-09-08 00:21:22 +04:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2000-09-07 09:26:25 +04:00
|
|
|
}
|
2000-09-08 00:21:22 +04:00
|
|
|
|
|
|
|
// mark the frame, so we get notified on deletion.
|
|
|
|
// frames are never unmarked, which means that we'll touch every frame we visit.
|
|
|
|
// this is not ideal.
|
|
|
|
nsFrameState frameState;
|
|
|
|
theFrame->GetFrameState(&frameState);
|
|
|
|
frameState |= NS_FRAME_EXTERNAL_REFERENCE;
|
|
|
|
theFrame->SetFrameState(frameState);
|
|
|
|
|
2001-03-21 04:16:22 +03:00
|
|
|
#ifdef IBMBIDI
|
|
|
|
PRUint8 bidiLevel=0;
|
|
|
|
// Mamdouh : modification of the caret to work at rtl and ltr with Bidi
|
|
|
|
const nsStyleDisplay* display;
|
|
|
|
theFrame->GetStyleData(eStyleStruct_Display, (const nsStyleStruct*&) display);
|
|
|
|
//
|
|
|
|
// Direction Style from this->GetStyleData()
|
|
|
|
// now in (display->mDirection)
|
|
|
|
// ------------------
|
|
|
|
// NS_STYLE_DIRECTION_LTR : LTR or Default
|
|
|
|
// NS_STYLE_DIRECTION_RTL
|
|
|
|
// NS_STYLE_DIRECTION_INHERIT
|
|
|
|
nsCOMPtr<nsIPresContext> presContext;
|
|
|
|
rv = presShell->GetPresContext(getter_AddRefs(presContext));
|
|
|
|
|
|
|
|
PRBool bidiEnabled = PR_FALSE;
|
|
|
|
if (presContext)
|
|
|
|
presContext->BidiEnabled(bidiEnabled);
|
|
|
|
|
|
|
|
if (bidiEnabled)
|
|
|
|
{
|
|
|
|
presShell->GetCursorBidiLevel(&bidiLevel);
|
|
|
|
if (bidiLevel & BIDI_LEVEL_UNDEFINED)
|
|
|
|
{
|
|
|
|
PRUint8 newBidiLevel;
|
|
|
|
bidiLevel &= ~BIDI_LEVEL_UNDEFINED;
|
|
|
|
// There has been a reflow, so we reset the cursor Bidi level to the level of the current frame
|
|
|
|
if (!presContext) // Use the style default or default to 0
|
|
|
|
newBidiLevel = (display) ? display->mDirection : 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
theFrame->GetBidiProperty(presContext, nsLayoutAtoms::embeddingLevel,
|
|
|
|
(void**)&newBidiLevel, sizeof(PRUint8) );
|
|
|
|
presShell->SetCursorBidiLevel(newBidiLevel);
|
|
|
|
bidiLevel = newBidiLevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 start;
|
|
|
|
PRInt32 end;
|
|
|
|
nsIFrame* frameBefore;
|
|
|
|
nsIFrame* frameAfter;
|
|
|
|
PRUint8 levelBefore; // Bidi level of the character before the caret
|
|
|
|
PRUint8 levelAfter; // Bidi level of the character after the caret
|
|
|
|
|
|
|
|
theFrame->GetOffsets(start, end);
|
|
|
|
if (start == 0 || end == 0 || start == theFrameOffset || end == theFrameOffset)
|
|
|
|
{
|
|
|
|
/* Boundary condition, we need to know the Bidi levels of the characters before and after the cursor */
|
|
|
|
if (NS_SUCCEEDED(frameSelection->GetPrevNextBidiLevels(presContext, contentNode, contentOffset,
|
|
|
|
&frameBefore, &frameAfter,
|
|
|
|
&levelBefore, &levelAfter)))
|
|
|
|
{
|
|
|
|
if ((levelBefore != levelAfter) || (bidiLevel != levelBefore))
|
|
|
|
{
|
|
|
|
bidiLevel = PR_MAX(bidiLevel, PR_MIN(levelBefore, levelAfter)); // rule c3
|
|
|
|
bidiLevel = PR_MIN(bidiLevel, PR_MAX(levelBefore, levelAfter)); // rule c4
|
|
|
|
if (bidiLevel == levelBefore // rule c1
|
|
|
|
|| bidiLevel > levelBefore && bidiLevel < levelAfter && !((bidiLevel ^ levelBefore) & 1) // rule c5
|
|
|
|
|| bidiLevel < levelBefore && bidiLevel > levelAfter && !((bidiLevel ^ levelBefore) & 1)) // rule c9
|
|
|
|
{
|
|
|
|
if (theFrame != frameBefore)
|
|
|
|
{
|
|
|
|
if (frameBefore) // if there is a frameBefore, move into it, setting HINTLEFT to make sure we stay there
|
|
|
|
{
|
|
|
|
theFrame = frameBefore;
|
|
|
|
theFrame->GetOffsets(start, end);
|
|
|
|
theFrameOffset = end;
|
|
|
|
// frameSelection->SetHint(nsIFrameSelection::HINTLEFT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if there is no frameBefore, we must be at the beginning of the line
|
|
|
|
// so we stay with the current frame.
|
|
|
|
// Exception: when the first frame on the line has a different Bidi level from the paragraph level, there is no
|
|
|
|
// real frame for the caret to be in. We have to find the first frame whose level is the same as the
|
|
|
|
// paragraph level, and put the caret at the end of the frame before that.
|
|
|
|
PRUint8 baseLevel;
|
|
|
|
frameAfter->GetBidiProperty(presContext, nsLayoutAtoms::baseLevel,
|
|
|
|
(void**)&baseLevel, sizeof(PRUint8) );
|
|
|
|
if (baseLevel != levelAfter)
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(frameSelection->GetFrameFromLevel(presContext, frameAfter, eDirNext, baseLevel, &theFrame)))
|
|
|
|
{
|
|
|
|
theFrame->GetOffsets(start, end);
|
|
|
|
theFrame->GetBidiProperty(presContext, nsLayoutAtoms::embeddingLevel,
|
|
|
|
(void**)&levelAfter, sizeof(PRUint8) );
|
|
|
|
if (baseLevel & 1) // RTL paragraph: caret to the right of the rightmost character
|
|
|
|
theFrameOffset = (levelAfter & 1) ? start : end;
|
|
|
|
else // LTR paragraph: caret to the left of the leftmost character
|
|
|
|
theFrameOffset = (levelAfter & 1) ? end : start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (bidiLevel == levelAfter // rule c2
|
|
|
|
|| bidiLevel > levelBefore && bidiLevel < levelAfter && !((bidiLevel ^ levelAfter) & 1) // rule c6
|
|
|
|
|| bidiLevel < levelBefore && bidiLevel > levelAfter && !((bidiLevel ^ levelAfter) & 1)) // rule c10
|
|
|
|
{
|
|
|
|
if (theFrame != frameAfter)
|
|
|
|
{
|
|
|
|
if (frameAfter)
|
|
|
|
{
|
|
|
|
// if there is a frameAfter, move into it, setting HINTRIGHT to make sure we stay there
|
|
|
|
theFrame = frameAfter;
|
|
|
|
theFrame->GetOffsets(start, end);
|
|
|
|
theFrameOffset = start;
|
|
|
|
// frameSelection->SetHint(nsIFrameSelection::HINTRIGHT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if there is no frameAfter, we must be at the end of the line
|
|
|
|
// so we stay with the current frame.
|
|
|
|
//
|
|
|
|
// Exception: when the last frame on the line has a different Bidi level from the paragraph level, there is no
|
|
|
|
// real frame for the caret to be in. We have to find the last frame whose level is the same as the
|
|
|
|
// paragraph level, and put the caret at the end of the frame after that.
|
|
|
|
PRUint8 baseLevel;
|
|
|
|
frameBefore->GetBidiProperty(presContext, nsLayoutAtoms::baseLevel,
|
|
|
|
(void**)&baseLevel, sizeof(PRUint8) );
|
|
|
|
if (baseLevel != levelBefore)
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(frameSelection->GetFrameFromLevel(presContext, frameBefore, eDirPrevious, baseLevel, &theFrame)))
|
|
|
|
{
|
|
|
|
theFrame->GetOffsets(start, end);
|
|
|
|
theFrame->GetBidiProperty(presContext, nsLayoutAtoms::embeddingLevel,
|
|
|
|
(void**)&levelBefore, sizeof(PRUint8) );
|
|
|
|
if (baseLevel & 1) // RTL paragraph: caret to the left of the leftmost character
|
|
|
|
theFrameOffset = (levelBefore & 1) ? end : start;
|
|
|
|
else // RTL paragraph: caret to the right of the rightmost character
|
|
|
|
theFrameOffset = (levelBefore & 1) ? start : end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (bidiLevel > levelBefore && bidiLevel < levelAfter // rule c7/8
|
|
|
|
&& !((levelBefore ^ levelAfter) & 1) // before and after have the same parity
|
|
|
|
&& ((bidiLevel ^ levelAfter) & 1)) // cursor has different parity
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(frameSelection->GetFrameFromLevel(presContext, frameAfter, eDirNext, bidiLevel, &theFrame)))
|
|
|
|
{
|
|
|
|
theFrame->GetOffsets(start, end);
|
|
|
|
theFrame->GetBidiProperty(presContext, nsLayoutAtoms::embeddingLevel,
|
|
|
|
(void**)&levelAfter, sizeof(PRUint8) );
|
|
|
|
if (bidiLevel & 1) // c8: caret to the right of the rightmost character
|
|
|
|
theFrameOffset = (levelAfter & 1) ? start : end;
|
|
|
|
else // c7: caret to the left of the leftmost character
|
|
|
|
theFrameOffset = (levelAfter & 1) ? end : start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (bidiLevel < levelBefore && bidiLevel > levelAfter // rule c11/12
|
|
|
|
&& !((levelBefore ^ levelAfter) & 1) // before and after have the same parity
|
|
|
|
&& ((bidiLevel ^ levelAfter) & 1)) // cursor has different parity
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(frameSelection->GetFrameFromLevel(presContext, frameBefore, eDirPrevious, bidiLevel, &theFrame)))
|
|
|
|
{
|
|
|
|
theFrame->GetOffsets(start, end);
|
|
|
|
theFrame->GetBidiProperty(presContext, nsLayoutAtoms::embeddingLevel,
|
|
|
|
(void**)&levelBefore, sizeof(PRUint8) );
|
|
|
|
if (bidiLevel & 1) // c12: caret to the left of the leftmost character
|
|
|
|
theFrameOffset = (levelBefore & 1) ? end : start;
|
|
|
|
else // c11: caret to the right of the rightmost character
|
|
|
|
theFrameOffset = (levelBefore & 1) ? start : end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // IBMBIDI
|
2000-09-08 00:21:22 +04:00
|
|
|
mLastCaretFrame = theFrame;
|
|
|
|
mLastContentOffset = theFrameOffset;
|
|
|
|
return PR_TRUE;
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-02 03:57:35 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
2000-09-07 09:26:25 +04:00
|
|
|
void nsCaret::GetViewForRendering(nsIFrame *caretFrame, EViewCoordinates coordType, nsPoint &viewOffset, nsRect& outClipRect, nsIView* &outView)
|
1999-04-02 03:57:35 +04:00
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
outView = nsnull;
|
|
|
|
|
|
|
|
if (!caretFrame)
|
|
|
|
return;
|
|
|
|
|
|
|
|
NS_ASSERTION(caretFrame, "Should have frame here");
|
2000-04-27 11:37:12 +04:00
|
|
|
nsCOMPtr<nsIPresShell> presShell = do_QueryReferent(mPresShell);
|
2000-09-07 09:26:25 +04:00
|
|
|
if (!presShell)
|
2000-04-27 11:37:12 +04:00
|
|
|
return;
|
2000-09-07 09:26:25 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPresContext> presContext;
|
|
|
|
presShell->GetPresContext(getter_AddRefs(presContext));
|
|
|
|
|
|
|
|
viewOffset.x = 0;
|
|
|
|
viewOffset.y = 0;
|
2000-09-08 00:21:22 +04:00
|
|
|
|
|
|
|
nsPoint withinViewOffset(0, 0);
|
2000-09-07 09:26:25 +04:00
|
|
|
// get the offset of this frame from its parent view (walks up frame hierarchy)
|
|
|
|
nsIView* theView = nsnull;
|
2000-09-08 00:21:22 +04:00
|
|
|
caretFrame->GetOffsetFromView(presContext, withinViewOffset, &theView);
|
2000-09-07 09:26:25 +04:00
|
|
|
if (theView == nsnull) return;
|
|
|
|
|
|
|
|
nsIView* returnView = nsnull; // views are not refcounted
|
|
|
|
|
|
|
|
nscoord x, y;
|
|
|
|
|
|
|
|
// coorinates relative to the view we are going to use for drawing
|
2001-02-05 18:43:24 +03:00
|
|
|
if (coordType == eRenderingViewCoordinates)
|
2000-09-07 09:26:25 +04:00
|
|
|
{
|
|
|
|
nsIScrollableView* scrollableView = nsnull;
|
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
nsPoint drawViewOffset(0, 0); // offset to the view we are using to draw
|
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
// walk up to the first view with a widget
|
|
|
|
do {
|
|
|
|
theView->GetPosition(&x, &y);
|
|
|
|
|
|
|
|
//is this a scrollable view?
|
|
|
|
if (!scrollableView)
|
|
|
|
theView->QueryInterface(NS_GET_IID(nsIScrollableView), (void **)&scrollableView);
|
|
|
|
|
|
|
|
PRBool hasWidget;
|
|
|
|
theView->HasWidget(&hasWidget);
|
|
|
|
if (hasWidget)
|
|
|
|
{
|
|
|
|
returnView = theView;
|
|
|
|
break;
|
|
|
|
}
|
2000-09-08 00:21:22 +04:00
|
|
|
drawViewOffset.x += x;
|
|
|
|
drawViewOffset.y += y;
|
2000-09-07 09:26:25 +04:00
|
|
|
|
|
|
|
theView->GetParent(theView);
|
|
|
|
} while (theView);
|
2000-09-08 00:21:22 +04:00
|
|
|
|
|
|
|
viewOffset = withinViewOffset;
|
|
|
|
viewOffset += drawViewOffset;
|
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
if (scrollableView)
|
|
|
|
{
|
|
|
|
const nsIView* clipView = nsnull;
|
|
|
|
scrollableView->GetClipView(&clipView);
|
|
|
|
if (!clipView) return; // should always have one
|
|
|
|
|
|
|
|
nsRect bounds;
|
|
|
|
clipView->GetBounds(bounds);
|
|
|
|
scrollableView->GetScrollPosition(bounds.x, bounds.y);
|
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
bounds += drawViewOffset; // offset to coords of returned view
|
2000-09-07 09:26:25 +04:00
|
|
|
outClipRect = bounds;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
returnView->GetBounds(outClipRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// window-relative coordinates (walk right to the top of the view hierarchy)
|
|
|
|
// we don't do anything with clipping here
|
2000-09-20 01:55:59 +04:00
|
|
|
viewOffset = withinViewOffset;
|
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
do {
|
|
|
|
theView->GetPosition(&x, &y);
|
|
|
|
|
|
|
|
if (!returnView)
|
|
|
|
{
|
|
|
|
PRBool hasWidget;
|
|
|
|
theView->HasWidget(&hasWidget);
|
|
|
|
|
|
|
|
if (hasWidget)
|
|
|
|
returnView = theView;
|
|
|
|
}
|
|
|
|
// is this right?
|
|
|
|
viewOffset.x += x;
|
|
|
|
viewOffset.y += y;
|
|
|
|
|
|
|
|
theView->GetParent(theView);
|
|
|
|
} while (theView);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
outView = returnView;
|
1999-04-02 03:57:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-09-15 03:41:19 +04:00
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
MustDrawCaret
|
|
|
|
|
|
|
|
FInd out if we need to do any caret drawing. This returns true if
|
|
|
|
either a) or b)
|
|
|
|
a) caret has been drawn, and we need to erase it.
|
|
|
|
b) caret is not drawn, and selection is collapsed
|
|
|
|
|
1999-09-15 03:41:19 +04:00
|
|
|
----------------------------------------------------------------------------- */
|
|
|
|
PRBool nsCaret::MustDrawCaret()
|
|
|
|
{
|
2000-09-14 15:45:01 +04:00
|
|
|
if (mDrawn)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> domSelection = do_QueryReferent(mDomSelectionWeak);
|
2000-06-07 05:18:41 +04:00
|
|
|
if (!domSelection)
|
|
|
|
return PR_FALSE;
|
2000-09-07 09:26:25 +04:00
|
|
|
PRBool isCollapsed;
|
1999-09-15 03:41:19 +04:00
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
if (NS_FAILED(domSelection->GetIsCollapsed(&isCollapsed)))
|
|
|
|
return PR_FALSE;
|
2000-12-28 06:27:23 +03:00
|
|
|
|
2001-04-05 23:53:13 +04:00
|
|
|
if (mShowDuringSelection)
|
2000-12-28 06:27:23 +03:00
|
|
|
return PR_TRUE; // show the caret even in selections
|
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
return isCollapsed;
|
1999-09-15 03:41:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
DrawCaret
|
|
|
|
|
1999-09-15 03:41:19 +04:00
|
|
|
----------------------------------------------------------------------------- */
|
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
void nsCaret::DrawCaret()
|
1999-04-02 03:57:35 +04:00
|
|
|
{
|
2000-09-08 00:21:22 +04:00
|
|
|
// do we need to draw the caret at all?
|
|
|
|
if (!MustDrawCaret())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// if we are drawing, not erasing, then set up the frame etc.
|
|
|
|
if (!mDrawn)
|
|
|
|
{
|
|
|
|
if (!SetupDrawingFrameAndOffset())
|
|
|
|
return;
|
|
|
|
}
|
2000-09-07 09:26:25 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(mLastCaretFrame != nsnull, "Should have a frame here");
|
|
|
|
|
|
|
|
nsRect frameRect;
|
|
|
|
mLastCaretFrame->GetRect(frameRect);
|
|
|
|
frameRect.x = 0; // the origin is accounted for in GetViewForRendering()
|
|
|
|
frameRect.y = 0;
|
|
|
|
|
|
|
|
if (frameRect.height == 0) // we're in a BR frame which has zero height.
|
|
|
|
{
|
|
|
|
frameRect.height = 200;
|
|
|
|
frameRect.y -= 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPoint viewOffset(0, 0);
|
|
|
|
nsRect clipRect;
|
|
|
|
nsIView *drawingView;
|
2001-02-05 18:43:24 +03:00
|
|
|
GetViewForRendering(mLastCaretFrame, eRenderingViewCoordinates, viewOffset, clipRect, drawingView);
|
2000-09-07 09:26:25 +04:00
|
|
|
|
|
|
|
if (drawingView == nsnull)
|
|
|
|
return;
|
|
|
|
|
|
|
|
frameRect += viewOffset;
|
|
|
|
|
2000-04-27 11:37:12 +04:00
|
|
|
nsCOMPtr<nsIPresShell> presShell = do_QueryReferent(mPresShell);
|
2000-09-08 00:21:22 +04:00
|
|
|
if (!presShell) return;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresContext> presContext;
|
|
|
|
if (NS_FAILED(presShell->GetPresContext(getter_AddRefs(presContext))))
|
2000-04-27 11:37:12 +04:00
|
|
|
return;
|
1999-09-15 03:41:19 +04:00
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
// if the view changed, or we don't have a rendering context, make one
|
|
|
|
// because of drawing issues, always make a new RC at the momemt. See bug 28068
|
|
|
|
if (
|
|
|
|
#ifdef DONT_REUSE_RENDERING_CONTEXT
|
|
|
|
PR_TRUE ||
|
|
|
|
#endif
|
|
|
|
(mLastCaretView != drawingView) || !mRendContext)
|
2000-09-07 09:26:25 +04:00
|
|
|
{
|
2000-09-08 00:21:22 +04:00
|
|
|
mRendContext = nsnull; // free existing one if we have one
|
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
nsCOMPtr<nsIDeviceContext> dx;
|
|
|
|
if (NS_FAILED(presContext->GetDeviceContext(getter_AddRefs(dx))) || !dx)
|
|
|
|
return;
|
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
if (NS_FAILED(dx->CreateRenderingContext(drawingView, *getter_AddRefs(mRendContext))) || !mRendContext)
|
|
|
|
return;
|
2000-09-07 09:26:25 +04:00
|
|
|
}
|
2000-09-08 00:21:22 +04:00
|
|
|
|
|
|
|
// push a known good state
|
|
|
|
mRendContext->PushState();
|
|
|
|
|
|
|
|
// views are not refcounted
|
|
|
|
mLastCaretView = drawingView;
|
|
|
|
|
2000-09-07 09:26:25 +04:00
|
|
|
if (!mDrawn)
|
|
|
|
{
|
|
|
|
nsPoint framePos(0, 0);
|
|
|
|
nsRect caretRect = frameRect;
|
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
mLastCaretFrame->GetPointFromOffset(presContext, mRendContext, mLastContentOffset, &framePos);
|
2000-09-07 09:26:25 +04:00
|
|
|
caretRect += framePos;
|
|
|
|
|
2000-10-29 02:17:53 +04:00
|
|
|
//printf("Content offset %ld, frame offset %ld\n", focusOffset, framePos.x);
|
2000-09-07 09:26:25 +04:00
|
|
|
if(mCaretTwipsWidth < 0)
|
2000-07-29 02:12:45 +04:00
|
|
|
{// need to re-compute the pixel width
|
2000-11-01 21:54:16 +03:00
|
|
|
float tDevUnitsToTwips = 15;
|
|
|
|
nsCOMPtr<nsIDeviceContext> dx;
|
|
|
|
presContext->GetDeviceContext(getter_AddRefs(dx));
|
|
|
|
if (dx)
|
|
|
|
dx->GetDevUnitsToTwips(tDevUnitsToTwips);
|
|
|
|
mCaretTwipsWidth = tDevUnitsToTwips * mCaretPixelsWidth;
|
2000-07-29 02:12:45 +04:00
|
|
|
}
|
2000-09-07 09:26:25 +04:00
|
|
|
caretRect.width = mCaretTwipsWidth;
|
|
|
|
|
|
|
|
// Avoid view redraw problems by making sure the
|
|
|
|
// caret doesn't hang outside the right edge of
|
|
|
|
// the frame. This ensures that the caret gets
|
|
|
|
// erased properly if the frame's right edge gets
|
|
|
|
// invalidated.
|
|
|
|
|
|
|
|
nscoord cX = caretRect.x + caretRect.width;
|
|
|
|
nscoord fX = frameRect.x + frameRect.width;
|
|
|
|
|
|
|
|
if (caretRect.x <= fX && cX > fX)
|
|
|
|
{
|
|
|
|
caretRect.x -= cX - fX;
|
|
|
|
|
|
|
|
if (caretRect.x < frameRect.x)
|
|
|
|
caretRect.x = frameRect.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCaretRect.IntersectRect(clipRect, caretRect);
|
2001-03-21 04:16:22 +03:00
|
|
|
#ifdef IBMBIDI
|
|
|
|
// Simon -- make a hook to draw to the left or right of the caret to show keyboard language direction
|
|
|
|
PRBool bidiEnabled;
|
|
|
|
nsRect hookRect;
|
|
|
|
PRBool bidiLevel=PR_FALSE;
|
|
|
|
if (mBidiKeyboard)
|
|
|
|
mBidiKeyboard->IsLangRTL(&bidiLevel);
|
|
|
|
if (bidiLevel)
|
|
|
|
{
|
|
|
|
presContext->EnableBidi();
|
|
|
|
bidiEnabled = PR_TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
presContext->BidiEnabled(bidiEnabled);
|
|
|
|
if (bidiEnabled)
|
|
|
|
{
|
|
|
|
if (bidiLevel != mKeyboardRTL)
|
|
|
|
{
|
|
|
|
mKeyboardRTL = bidiLevel;
|
|
|
|
nsCOMPtr<nsISelection> domSelection = do_QueryReferent(mDomSelectionWeak);
|
|
|
|
if (domSelection)
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(domSelection->SelectionLanguageChange(mKeyboardRTL)))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If keyboard language is RTL, draw the hook on the left; if LTR, to the right
|
|
|
|
hookRect.SetRect(caretRect.x + caretRect.width * ((bidiLevel) ? -1 : 1),
|
|
|
|
caretRect.y + caretRect.width,
|
|
|
|
caretRect.width,
|
|
|
|
caretRect.width);
|
|
|
|
mHookRect.IntersectRect(clipRect, hookRect);
|
|
|
|
}
|
|
|
|
#endif //IBMBIDI
|
2000-09-07 09:26:25 +04:00
|
|
|
}
|
2001-03-21 04:16:22 +03:00
|
|
|
|
2001-02-15 08:07:46 +03:00
|
|
|
if (mReadOnly)
|
|
|
|
mRendContext->SetColor(NS_RGB(85, 85, 85)); // we are drawing it; gray
|
|
|
|
else
|
|
|
|
mRendContext->SetColor(NS_RGB(255,255,255));
|
2000-09-08 00:21:22 +04:00
|
|
|
mRendContext->InvertRect(mCaretRect);
|
2001-03-21 04:16:22 +03:00
|
|
|
#ifdef IBMBIDI
|
|
|
|
if (!mHookRect.IsEmpty()) // if Bidi support is disabled, the rectangle remains empty and won't be drawn
|
|
|
|
mRendContext->InvertRect(mHookRect);
|
|
|
|
#endif
|
2000-06-09 08:22:20 +04:00
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
PRBool emptyClip; // I know what you're thinking. "Did he fire six shots or only five?"
|
|
|
|
mRendContext->PopState(emptyClip);
|
1999-04-02 03:57:35 +04:00
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
ToggleDrawnStatus();
|
2000-09-07 09:26:25 +04:00
|
|
|
|
2000-09-08 00:21:22 +04:00
|
|
|
#ifdef DONT_REUSE_RENDERING_CONTEXT
|
|
|
|
mRendContext = nsnull;
|
|
|
|
#endif
|
1999-04-02 03:57:35 +04:00
|
|
|
}
|
|
|
|
|
1999-03-02 07:26:49 +03:00
|
|
|
#ifdef XP_MAC
|
1999-02-12 02:49:08 +03:00
|
|
|
#pragma mark -
|
1999-03-02 07:26:49 +03:00
|
|
|
#endif
|
1999-02-12 02:49:08 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
/* static */
|
|
|
|
void nsCaret::CaretBlinkCallback(nsITimer *aTimer, void *aClosure)
|
|
|
|
{
|
2000-09-07 09:26:25 +04:00
|
|
|
nsCaret *theCaret = NS_REINTERPRET_CAST(nsCaret*, aClosure);
|
|
|
|
if (!theCaret) return;
|
|
|
|
|
|
|
|
theCaret->DrawCaret();
|
2000-01-22 00:56:09 +03:00
|
|
|
|
|
|
|
#ifndef REPEATING_TIMERS
|
2000-09-07 09:26:25 +04:00
|
|
|
theCaret->PrimeTimer();
|
2000-01-22 00:56:09 +03:00
|
|
|
#endif
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
nsresult NS_NewCaret(nsICaret** aInstancePtrResult)
|
|
|
|
{
|
1999-09-15 03:41:19 +04:00
|
|
|
NS_PRECONDITION(aInstancePtrResult, "null ptr");
|
1999-02-12 02:49:08 +03:00
|
|
|
|
|
|
|
nsCaret* caret = new nsCaret();
|
|
|
|
if (nsnull == caret)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
1999-09-15 03:41:19 +04:00
|
|
|
return caret->QueryInterface(NS_GET_IID(nsICaret), (void**) aInstancePtrResult);
|
1999-02-12 02:49:08 +03:00
|
|
|
}
|
|
|
|
|
2000-07-29 02:12:45 +04:00
|
|
|
NS_IMETHODIMP nsCaret::SetCaretWidth(nscoord aPixels)
|
|
|
|
{
|
|
|
|
if(!aPixels)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
else
|
|
|
|
{ //no need to optimize this, but if it gets too slow, we can check for case aPixels==mCaretPixelsWidth
|
|
|
|
mCaretPixelsWidth = aPixels;
|
|
|
|
mCaretTwipsWidth = -1;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-04-05 23:53:13 +04:00
|
|
|
|
|
|
|
void nsCaret::SetVisibilityDuringSelection(PRBool aVisibility) {
|
|
|
|
mShowDuringSelection = aVisibility;
|
|
|
|
}
|