From 7513b7afc1148085db4178b57cbdb0be5adc2958 Mon Sep 17 00:00:00 2001 From: "hyatt%netscape.com" Date: Sun, 22 Aug 1999 06:53:01 +0000 Subject: [PATCH] More selection work. --- content/xul/content/src/nsXULTreeElement.cpp | 50 ++++++++++++++---- dom/public/idl/xul/XULTreeElement.idl | 4 +- dom/public/xul/nsIDOMXULTreeElement.h | 10 ++-- dom/src/xul/nsJSXULTreeElement.cpp | 54 ++++++++++++++++++-- rdf/content/public/idl/XULTreeElement.idl | 4 +- rdf/content/public/nsIDOMXULTreeElement.h | 10 ++-- rdf/content/src/nsJSXULTreeElement.cpp | 54 ++++++++++++++++++-- rdf/content/src/nsXULTreeElement.cpp | 50 ++++++++++++++---- 8 files changed, 196 insertions(+), 40 deletions(-) diff --git a/content/xul/content/src/nsXULTreeElement.cpp b/content/xul/content/src/nsXULTreeElement.cpp index fade86ebca5..8da79f23974 100644 --- a/content/xul/content/src/nsXULTreeElement.cpp +++ b/content/xul/content/src/nsXULTreeElement.cpp @@ -87,8 +87,20 @@ nsXULTreeElement::GetSelectedCells(nsIDOMNodeList** aSelectedCells) NS_IMETHODIMP nsXULTreeElement::SelectItem(nsIDOMXULElement* aTreeItem) { + // Sanity check. If we're the only item, just bail. + PRUint32 length; + mSelectedItems->GetLength(&length); + if (length == 1) { + // See if the single item already selected is us. + nsCOMPtr domNode; + mSelectedItems->Item(0, getter_AddRefs(domNode)); + nsCOMPtr treeItem = do_QueryInterface(domNode); + if (treeItem.get() == aTreeItem) + return NS_OK; + } + // First clear our selection. - ClearSelection(); + ClearItemSelection(); // Now add ourselves to the selection by setting our selected attribute. AddItemToSelection(aTreeItem); @@ -99,8 +111,20 @@ nsXULTreeElement::SelectItem(nsIDOMXULElement* aTreeItem) NS_IMETHODIMP nsXULTreeElement::SelectCell(nsIDOMXULElement* aTreeCell) { + // Sanity check. If we're the only item, just bail. + PRUint32 length; + mSelectedCells->GetLength(&length); + if (length == 1) { + // See if the single item already selected is us. + nsCOMPtr domNode; + mSelectedCells->Item(0, getter_AddRefs(domNode)); + nsCOMPtr treeCell = do_QueryInterface(domNode); + if (treeCell.get() == aTreeCell) + return NS_OK; + } + // First clear our selection. - ClearSelection(); + ClearCellSelection(); // Now add ourselves to the selection by setting our selected attribute. AddCellToSelection(aTreeCell); @@ -109,21 +133,26 @@ nsXULTreeElement::SelectCell(nsIDOMXULElement* aTreeCell) } NS_IMETHODIMP -nsXULTreeElement::ClearSelection() +nsXULTreeElement::ClearItemSelection() { // Enumerate the elements and remove them from the selection. PRUint32 length; mSelectedItems->GetLength(&length); - PRUint32 i; - for (i = 0; i < length; i++) { + for (PRUint32 i = 0; i < length; i++) { nsCOMPtr node; mSelectedItems->Item(0, getter_AddRefs(node)); nsCOMPtr content = do_QueryInterface(node); content->UnsetAttribute(kNameSpaceID_None, kSelectedAtom, PR_TRUE); } - + return NS_OK; +} + +NS_IMETHODIMP +nsXULTreeElement::ClearCellSelection() +{ + PRUint32 length; mSelectedCells->GetLength(&length); - for (i = 0; i < length; i++) { + for (PRUint32 i = 0; i < length; i++) { nsCOMPtr node; mSelectedCells->Item(0, getter_AddRefs(node)); nsCOMPtr content = do_QueryInterface(node); @@ -188,8 +217,8 @@ nsXULTreeElement::ToggleCellSelection(nsIDOMXULElement* aTreeCell) nsAutoString isSelected; aTreeCell->GetAttribute("selected", isSelected); if (isSelected == "true") - RemoveItemFromSelection(aTreeCell); - else AddItemToSelection(aTreeCell); + RemoveCellFromSelection(aTreeCell); + else AddCellToSelection(aTreeCell); return NS_OK; } @@ -213,7 +242,8 @@ NS_IMETHODIMP nsXULTreeElement::SelectAll() { // Do a clear. - ClearSelection(); + ClearItemSelection(); + ClearCellSelection(); // Now do an invert. That will select everything. InvertSelection(); diff --git a/dom/public/idl/xul/XULTreeElement.idl b/dom/public/idl/xul/XULTreeElement.idl index 7f16d7fdca8..e20500c3276 100644 --- a/dom/public/idl/xul/XULTreeElement.idl +++ b/dom/public/idl/xul/XULTreeElement.idl @@ -7,7 +7,8 @@ interface XULTreeElement : XULElement { void selectItem(in XULElement treeItem); void selectCell(in XULElement treeCell); - void clearSelection(); + void clearItemSelection(); + void clearCellSelection(); void addItemToSelection(in XULElement treeItem); void removeItemFromSelection(in XULElement treeItem); @@ -22,6 +23,5 @@ interface XULTreeElement : XULElement { void selectCellRange(in XULElement startItem, in XULElement endItem); void selectAll(); - void invertSelection(); }; diff --git a/dom/public/xul/nsIDOMXULTreeElement.h b/dom/public/xul/nsIDOMXULTreeElement.h index 29c1a49f4bd..1c5f6eb63a9 100644 --- a/dom/public/xul/nsIDOMXULTreeElement.h +++ b/dom/public/xul/nsIDOMXULTreeElement.h @@ -44,7 +44,9 @@ public: NS_IMETHOD SelectCell(nsIDOMXULElement* aTreeCell)=0; - NS_IMETHOD ClearSelection()=0; + NS_IMETHOD ClearItemSelection()=0; + + NS_IMETHOD ClearCellSelection()=0; NS_IMETHOD AddItemToSelection(nsIDOMXULElement* aTreeItem)=0; @@ -73,7 +75,8 @@ public: NS_IMETHOD GetSelectedCells(nsIDOMNodeList** aSelectedCells); \ NS_IMETHOD SelectItem(nsIDOMXULElement* aTreeItem); \ NS_IMETHOD SelectCell(nsIDOMXULElement* aTreeCell); \ - NS_IMETHOD ClearSelection(); \ + NS_IMETHOD ClearItemSelection(); \ + NS_IMETHOD ClearCellSelection(); \ NS_IMETHOD AddItemToSelection(nsIDOMXULElement* aTreeItem); \ NS_IMETHOD RemoveItemFromSelection(nsIDOMXULElement* aTreeItem); \ NS_IMETHOD AddCellToSelection(nsIDOMXULElement* aTreeCell); \ @@ -92,7 +95,8 @@ public: NS_IMETHOD GetSelectedCells(nsIDOMNodeList** aSelectedCells) { return _to GetSelectedCells(aSelectedCells); } \ NS_IMETHOD SelectItem(nsIDOMXULElement* aTreeItem) { return _to SelectItem(aTreeItem); } \ NS_IMETHOD SelectCell(nsIDOMXULElement* aTreeCell) { return _to SelectCell(aTreeCell); } \ - NS_IMETHOD ClearSelection() { return _to ClearSelection(); } \ + NS_IMETHOD ClearItemSelection() { return _to ClearItemSelection(); } \ + NS_IMETHOD ClearCellSelection() { return _to ClearCellSelection(); } \ NS_IMETHOD AddItemToSelection(nsIDOMXULElement* aTreeItem) { return _to AddItemToSelection(aTreeItem); } \ NS_IMETHOD RemoveItemFromSelection(nsIDOMXULElement* aTreeItem) { return _to RemoveItemFromSelection(aTreeItem); } \ NS_IMETHOD AddCellToSelection(nsIDOMXULElement* aTreeCell) { return _to AddCellToSelection(aTreeCell); } \ diff --git a/dom/src/xul/nsJSXULTreeElement.cpp b/dom/src/xul/nsJSXULTreeElement.cpp index 3072c87ad9b..34ab1669cac 100644 --- a/dom/src/xul/nsJSXULTreeElement.cpp +++ b/dom/src/xul/nsJSXULTreeElement.cpp @@ -298,10 +298,10 @@ XULTreeElementSelectCell(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, // -// Native method ClearSelection +// Native method ClearItemSelection // PR_STATIC_CALLBACK(JSBool) -XULTreeElementClearSelection(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +XULTreeElementClearItemSelection(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { nsIDOMXULTreeElement *nativeThis = (nsIDOMXULTreeElement*)nsJSUtils::nsGetNativeThis(cx, obj); @@ -314,7 +314,7 @@ XULTreeElementClearSelection(JSContext *cx, JSObject *obj, uintN argc, jsval *ar } { PRBool ok; - secMan->CheckScriptAccess(scriptCX, obj, "xultreeelement.clearselection", &ok); + secMan->CheckScriptAccess(scriptCX, obj, "xultreeelement.clearitemselection", &ok); if (!ok) { //Need to throw error here return JS_FALSE; @@ -329,7 +329,50 @@ XULTreeElementClearSelection(JSContext *cx, JSObject *obj, uintN argc, jsval *ar { - if (NS_OK != nativeThis->ClearSelection()) { + if (NS_OK != nativeThis->ClearItemSelection()) { + return JS_FALSE; + } + + *rval = JSVAL_VOID; + } + + return JS_TRUE; +} + + +// +// Native method ClearCellSelection +// +PR_STATIC_CALLBACK(JSBool) +XULTreeElementClearCellSelection(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMXULTreeElement *nativeThis = (nsIDOMXULTreeElement*)nsJSUtils::nsGetNativeThis(cx, obj); + + *rval = JSVAL_NULL; + + nsIScriptContext *scriptCX = (nsIScriptContext *)JS_GetContextPrivate(cx); + nsIScriptSecurityManager *secMan; + if (NS_OK != scriptCX->GetSecurityManager(&secMan)) { + return JS_FALSE; + } + { + PRBool ok; + secMan->CheckScriptAccess(scriptCX, obj, "xultreeelement.clearcellselection", &ok); + if (!ok) { + //Need to throw error here + return JS_FALSE; + } + NS_RELEASE(secMan); + } + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + { + + if (NS_OK != nativeThis->ClearCellSelection()) { return JS_FALSE; } @@ -926,7 +969,8 @@ static JSFunctionSpec XULTreeElementMethods[] = { {"selectItem", XULTreeElementSelectItem, 1}, {"selectCell", XULTreeElementSelectCell, 1}, - {"clearSelection", XULTreeElementClearSelection, 0}, + {"clearItemSelection", XULTreeElementClearItemSelection, 0}, + {"clearCellSelection", XULTreeElementClearCellSelection, 0}, {"addItemToSelection", XULTreeElementAddItemToSelection, 1}, {"removeItemFromSelection", XULTreeElementRemoveItemFromSelection, 1}, {"addCellToSelection", XULTreeElementAddCellToSelection, 1}, diff --git a/rdf/content/public/idl/XULTreeElement.idl b/rdf/content/public/idl/XULTreeElement.idl index 7f16d7fdca8..e20500c3276 100644 --- a/rdf/content/public/idl/XULTreeElement.idl +++ b/rdf/content/public/idl/XULTreeElement.idl @@ -7,7 +7,8 @@ interface XULTreeElement : XULElement { void selectItem(in XULElement treeItem); void selectCell(in XULElement treeCell); - void clearSelection(); + void clearItemSelection(); + void clearCellSelection(); void addItemToSelection(in XULElement treeItem); void removeItemFromSelection(in XULElement treeItem); @@ -22,6 +23,5 @@ interface XULTreeElement : XULElement { void selectCellRange(in XULElement startItem, in XULElement endItem); void selectAll(); - void invertSelection(); }; diff --git a/rdf/content/public/nsIDOMXULTreeElement.h b/rdf/content/public/nsIDOMXULTreeElement.h index 29c1a49f4bd..1c5f6eb63a9 100644 --- a/rdf/content/public/nsIDOMXULTreeElement.h +++ b/rdf/content/public/nsIDOMXULTreeElement.h @@ -44,7 +44,9 @@ public: NS_IMETHOD SelectCell(nsIDOMXULElement* aTreeCell)=0; - NS_IMETHOD ClearSelection()=0; + NS_IMETHOD ClearItemSelection()=0; + + NS_IMETHOD ClearCellSelection()=0; NS_IMETHOD AddItemToSelection(nsIDOMXULElement* aTreeItem)=0; @@ -73,7 +75,8 @@ public: NS_IMETHOD GetSelectedCells(nsIDOMNodeList** aSelectedCells); \ NS_IMETHOD SelectItem(nsIDOMXULElement* aTreeItem); \ NS_IMETHOD SelectCell(nsIDOMXULElement* aTreeCell); \ - NS_IMETHOD ClearSelection(); \ + NS_IMETHOD ClearItemSelection(); \ + NS_IMETHOD ClearCellSelection(); \ NS_IMETHOD AddItemToSelection(nsIDOMXULElement* aTreeItem); \ NS_IMETHOD RemoveItemFromSelection(nsIDOMXULElement* aTreeItem); \ NS_IMETHOD AddCellToSelection(nsIDOMXULElement* aTreeCell); \ @@ -92,7 +95,8 @@ public: NS_IMETHOD GetSelectedCells(nsIDOMNodeList** aSelectedCells) { return _to GetSelectedCells(aSelectedCells); } \ NS_IMETHOD SelectItem(nsIDOMXULElement* aTreeItem) { return _to SelectItem(aTreeItem); } \ NS_IMETHOD SelectCell(nsIDOMXULElement* aTreeCell) { return _to SelectCell(aTreeCell); } \ - NS_IMETHOD ClearSelection() { return _to ClearSelection(); } \ + NS_IMETHOD ClearItemSelection() { return _to ClearItemSelection(); } \ + NS_IMETHOD ClearCellSelection() { return _to ClearCellSelection(); } \ NS_IMETHOD AddItemToSelection(nsIDOMXULElement* aTreeItem) { return _to AddItemToSelection(aTreeItem); } \ NS_IMETHOD RemoveItemFromSelection(nsIDOMXULElement* aTreeItem) { return _to RemoveItemFromSelection(aTreeItem); } \ NS_IMETHOD AddCellToSelection(nsIDOMXULElement* aTreeCell) { return _to AddCellToSelection(aTreeCell); } \ diff --git a/rdf/content/src/nsJSXULTreeElement.cpp b/rdf/content/src/nsJSXULTreeElement.cpp index 3072c87ad9b..34ab1669cac 100644 --- a/rdf/content/src/nsJSXULTreeElement.cpp +++ b/rdf/content/src/nsJSXULTreeElement.cpp @@ -298,10 +298,10 @@ XULTreeElementSelectCell(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, // -// Native method ClearSelection +// Native method ClearItemSelection // PR_STATIC_CALLBACK(JSBool) -XULTreeElementClearSelection(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +XULTreeElementClearItemSelection(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { nsIDOMXULTreeElement *nativeThis = (nsIDOMXULTreeElement*)nsJSUtils::nsGetNativeThis(cx, obj); @@ -314,7 +314,7 @@ XULTreeElementClearSelection(JSContext *cx, JSObject *obj, uintN argc, jsval *ar } { PRBool ok; - secMan->CheckScriptAccess(scriptCX, obj, "xultreeelement.clearselection", &ok); + secMan->CheckScriptAccess(scriptCX, obj, "xultreeelement.clearitemselection", &ok); if (!ok) { //Need to throw error here return JS_FALSE; @@ -329,7 +329,50 @@ XULTreeElementClearSelection(JSContext *cx, JSObject *obj, uintN argc, jsval *ar { - if (NS_OK != nativeThis->ClearSelection()) { + if (NS_OK != nativeThis->ClearItemSelection()) { + return JS_FALSE; + } + + *rval = JSVAL_VOID; + } + + return JS_TRUE; +} + + +// +// Native method ClearCellSelection +// +PR_STATIC_CALLBACK(JSBool) +XULTreeElementClearCellSelection(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMXULTreeElement *nativeThis = (nsIDOMXULTreeElement*)nsJSUtils::nsGetNativeThis(cx, obj); + + *rval = JSVAL_NULL; + + nsIScriptContext *scriptCX = (nsIScriptContext *)JS_GetContextPrivate(cx); + nsIScriptSecurityManager *secMan; + if (NS_OK != scriptCX->GetSecurityManager(&secMan)) { + return JS_FALSE; + } + { + PRBool ok; + secMan->CheckScriptAccess(scriptCX, obj, "xultreeelement.clearcellselection", &ok); + if (!ok) { + //Need to throw error here + return JS_FALSE; + } + NS_RELEASE(secMan); + } + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + { + + if (NS_OK != nativeThis->ClearCellSelection()) { return JS_FALSE; } @@ -926,7 +969,8 @@ static JSFunctionSpec XULTreeElementMethods[] = { {"selectItem", XULTreeElementSelectItem, 1}, {"selectCell", XULTreeElementSelectCell, 1}, - {"clearSelection", XULTreeElementClearSelection, 0}, + {"clearItemSelection", XULTreeElementClearItemSelection, 0}, + {"clearCellSelection", XULTreeElementClearCellSelection, 0}, {"addItemToSelection", XULTreeElementAddItemToSelection, 1}, {"removeItemFromSelection", XULTreeElementRemoveItemFromSelection, 1}, {"addCellToSelection", XULTreeElementAddCellToSelection, 1}, diff --git a/rdf/content/src/nsXULTreeElement.cpp b/rdf/content/src/nsXULTreeElement.cpp index fade86ebca5..8da79f23974 100644 --- a/rdf/content/src/nsXULTreeElement.cpp +++ b/rdf/content/src/nsXULTreeElement.cpp @@ -87,8 +87,20 @@ nsXULTreeElement::GetSelectedCells(nsIDOMNodeList** aSelectedCells) NS_IMETHODIMP nsXULTreeElement::SelectItem(nsIDOMXULElement* aTreeItem) { + // Sanity check. If we're the only item, just bail. + PRUint32 length; + mSelectedItems->GetLength(&length); + if (length == 1) { + // See if the single item already selected is us. + nsCOMPtr domNode; + mSelectedItems->Item(0, getter_AddRefs(domNode)); + nsCOMPtr treeItem = do_QueryInterface(domNode); + if (treeItem.get() == aTreeItem) + return NS_OK; + } + // First clear our selection. - ClearSelection(); + ClearItemSelection(); // Now add ourselves to the selection by setting our selected attribute. AddItemToSelection(aTreeItem); @@ -99,8 +111,20 @@ nsXULTreeElement::SelectItem(nsIDOMXULElement* aTreeItem) NS_IMETHODIMP nsXULTreeElement::SelectCell(nsIDOMXULElement* aTreeCell) { + // Sanity check. If we're the only item, just bail. + PRUint32 length; + mSelectedCells->GetLength(&length); + if (length == 1) { + // See if the single item already selected is us. + nsCOMPtr domNode; + mSelectedCells->Item(0, getter_AddRefs(domNode)); + nsCOMPtr treeCell = do_QueryInterface(domNode); + if (treeCell.get() == aTreeCell) + return NS_OK; + } + // First clear our selection. - ClearSelection(); + ClearCellSelection(); // Now add ourselves to the selection by setting our selected attribute. AddCellToSelection(aTreeCell); @@ -109,21 +133,26 @@ nsXULTreeElement::SelectCell(nsIDOMXULElement* aTreeCell) } NS_IMETHODIMP -nsXULTreeElement::ClearSelection() +nsXULTreeElement::ClearItemSelection() { // Enumerate the elements and remove them from the selection. PRUint32 length; mSelectedItems->GetLength(&length); - PRUint32 i; - for (i = 0; i < length; i++) { + for (PRUint32 i = 0; i < length; i++) { nsCOMPtr node; mSelectedItems->Item(0, getter_AddRefs(node)); nsCOMPtr content = do_QueryInterface(node); content->UnsetAttribute(kNameSpaceID_None, kSelectedAtom, PR_TRUE); } - + return NS_OK; +} + +NS_IMETHODIMP +nsXULTreeElement::ClearCellSelection() +{ + PRUint32 length; mSelectedCells->GetLength(&length); - for (i = 0; i < length; i++) { + for (PRUint32 i = 0; i < length; i++) { nsCOMPtr node; mSelectedCells->Item(0, getter_AddRefs(node)); nsCOMPtr content = do_QueryInterface(node); @@ -188,8 +217,8 @@ nsXULTreeElement::ToggleCellSelection(nsIDOMXULElement* aTreeCell) nsAutoString isSelected; aTreeCell->GetAttribute("selected", isSelected); if (isSelected == "true") - RemoveItemFromSelection(aTreeCell); - else AddItemToSelection(aTreeCell); + RemoveCellFromSelection(aTreeCell); + else AddCellToSelection(aTreeCell); return NS_OK; } @@ -213,7 +242,8 @@ NS_IMETHODIMP nsXULTreeElement::SelectAll() { // Do a clear. - ClearSelection(); + ClearItemSelection(); + ClearCellSelection(); // Now do an invert. That will select everything. InvertSelection();