1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla 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/MPL/
*
* 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 XForms support.
*
* The Initial Developer of the Original Code is
* Novell, Inc.
* Portions created by the Initial Developer are Copyright (C) 2004
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Allan Beaufour <abeaufour@novell.com>
* Olli Pettay <Olli.Pettay@helsinki.fi>
*
* 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 MPL, 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 MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsIModelElementPrivate.h"
#include "nsXFormsControlStub.h"
#include "nsXFormsMDGEngine.h"
#include "nsIDOMDocument.h"
#include "nsIDOMEvent.h"
#include "nsIDOMKeyEvent.h"
#include "nsIDOMEventTarget.h"
#include "nsIDOMXPathResult.h"
#include "nsIDocument.h"
#include "nsXFormsModelElement.h"
#include "nsPIDOMWindow.h"
#include "nsIFocusController.h"
#include "nsIServiceManager.h"
#include "nsIEventStateManager.h"
#include "nsIContent.h"
#include "nsIDOM3Node.h"
#include "nsIDOMAttr.h"
/** This class is used to generate xforms-hint and xforms-help events.*/
class nsXFormsHintHelpListener : public nsIDOMEventListener {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIDOMEVENTLISTENER
};
NS_IMPL_ISUPPORTS1(nsXFormsHintHelpListener, nsIDOMEventListener)
NS_IMETHODIMP
nsXFormsHintHelpListener::HandleEvent(nsIDOMEvent* aEvent)
{
if (!aEvent)
return NS_ERROR_UNEXPECTED;
nsCOMPtr<nsIDOMEventTarget> target;
aEvent->GetCurrentTarget(getter_AddRefs(target));
nsCOMPtr<nsIDOMNode> targetNode(do_QueryInterface(target));
if (nsXFormsUtils::EventHandlingAllowed(aEvent, targetNode)) {
nsCOMPtr<nsIDOMKeyEvent> keyEvent(do_QueryInterface(aEvent));
if (keyEvent) {
PRUint32 code = 0;
keyEvent->GetKeyCode(&code);
if (code == nsIDOMKeyEvent::DOM_VK_F1) {
PRBool defaultEnabled = PR_TRUE;
nsresult rv = nsXFormsUtils::DispatchEvent(targetNode, eEvent_Help,
&defaultEnabled);
// If the xforms event's default behavior was disabled, prevent the DOM
// event's default action as well. This means that the F1 key was
// handled (a help dialog was shown) and we don't want the browser to
// show it's help dialog.
if (NS_SUCCEEDED(rv) && !defaultEnabled)
aEvent->PreventDefault();
}
} else {
nsAutoString type;
aEvent->GetType(type);
nsXFormsUtils::DispatchEvent(targetNode,
(type.EqualsLiteral("mouseover") ||
type.EqualsLiteral("focus"))
? eEvent_Hint : eEvent_MozHintOff);
}
}
return NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::GetBoundNode(nsIDOMNode **aBoundNode)
{
NS_IF_ADDREF(*aBoundNode = mBoundNode);
return NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::GetDependencies(nsCOMArray<nsIDOMNode> **aDependencies)
{
if (aDependencies)
*aDependencies = &mDependencies;
return NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::GetElement(nsIDOMElement **aElement)
{
NS_IF_ADDREF(*aElement = mElement);
return NS_OK;
}
void
nsXFormsControlStubBase::RemoveIndexListeners()
{
if (!mIndexesUsed.Count())
return;
for (PRInt32 i = 0; i < mIndexesUsed.Count(); ++i) {
nsCOMPtr<nsIXFormsRepeatElement> rep = mIndexesUsed[i];
rep->RemoveIndexUser(this);
}
mIndexesUsed.Clear();
}
NS_IMETHODIMP
nsXFormsControlStubBase::ResetBoundNode(const nsString &aBindAttribute,
PRUint16 aResultType,
PRBool *aContextChanged)
{
NS_ENSURE_ARG(aContextChanged);
// Clear existing bound node, etc.
*aContextChanged = mBoundNode ? PR_TRUE : PR_FALSE;
nsCOMPtr<nsIDOMNode> oldBoundNode;
oldBoundNode.swap(mBoundNode);
mUsesModelBinding = PR_FALSE;
mAppearDisabled = PR_FALSE;
mDependencies.Clear();
RemoveIndexListeners();
if (!mHasParent || !mHasDoc || !HasBindingAttribute())
return NS_OK_XFORMS_NOTREADY;
nsCOMPtr<nsIDOMXPathResult> result;
nsresult rv = ProcessNodeBinding(aBindAttribute, aResultType,
getter_AddRefs(result));
if (NS_FAILED(rv)) {
nsXFormsUtils::ReportError(NS_LITERAL_STRING("controlBindError"), mElement);
return rv;
}
if (rv == NS_OK_XFORMS_DEFERRED || rv == NS_OK_XFORMS_NOTREADY || !result) {
// Binding was deferred, or not bound
return rv;
}
// Get context node, if any
if (mUsesModelBinding) {
// When bound via @bind, we'll get a snapshot back
result->SnapshotItem(0, getter_AddRefs(mBoundNode));
} else {
result->GetSingleNodeValue(getter_AddRefs(mBoundNode));
}
*aContextChanged = (oldBoundNode != mBoundNode);
// Some controls may not be bound to certain types of content. If the content
// is a disallowed type, report the error and dispatch a binding exception
// event.
PRBool isAllowed = IsContentAllowed();
if (!mBoundNode || !isAllowed) {
// If there's no result (ie, no instance node) returned by the above, it
// means that the binding is not pointing to an instance data node, so we
// should disable the control.
mAppearDisabled = PR_TRUE;
if (!isAllowed) {
// build the error string that we want output to the ErrorConsole
nsAutoString localName;
mElement->GetLocalName(localName);
const PRUnichar *strings[] = { localName.get() };
nsXFormsUtils::ReportError(
NS_LITERAL_STRING("boundTypeErrorComplexContent"),
strings, 1, mElement, mElement);
nsXFormsUtils::DispatchEvent(mElement, eEvent_BindingException);
}
nsCOMPtr<nsIXTFElementWrapper> wrapper(do_QueryInterface(mElement));
NS_ENSURE_STATE(wrapper);
PRInt32 iState;
GetDisabledIntrinsicState(&iState);
return wrapper->SetIntrinsicState(iState);
}
// Check for presence of @xsi:type on bound node and add as a dependency
nsCOMPtr<nsIDOMElement> boundEl(do_QueryInterface(mBoundNode));
if (boundEl) {
nsCOMPtr<nsIDOMAttr> attrNode;
rv = boundEl->GetAttributeNodeNS(NS_LITERAL_STRING(NS_NAMESPACE_XML_SCHEMA_INSTANCE),
NS_LITERAL_STRING("type"),
getter_AddRefs(attrNode));
if (NS_SUCCEEDED(rv) && attrNode) {
mDependencies.AppendObject(attrNode);
}
}
return NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::Bind(PRBool* aContextChanged)
{
return ResetBoundNode(NS_LITERAL_STRING("ref"),
nsIDOMXPathResult::FIRST_ORDERED_NODE_TYPE,
aContextChanged);
}
NS_IMETHODIMP
nsXFormsControlStubBase::Refresh()
{
// XXX: In theory refresh should never be called when there is no model,
// but that's definately not the case now.
return (mModel && !mAppearDisabled) ? mModel->SetStates(this, mBoundNode)
: NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::TryFocus(PRBool* aOK)
{
*aOK = PR_FALSE;
return NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::IsEventTarget(PRBool *aOK)
{
*aOK = PR_TRUE;
return NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::GetUsesModelBinding(PRBool *aRes)
{
*aRes = mUsesModelBinding;
return NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::GetUsesSingleNodeBinding(PRBool *aRes)
{
*aRes = PR_TRUE;
return NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::GetOnDeferredBindList(PRBool *aOnList)
{
NS_ENSURE_ARG_POINTER(aOnList);
*aOnList = mOnDeferredBindList;
return NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::SetOnDeferredBindList(PRBool aPutOnList)
{
mOnDeferredBindList = aPutOnList;
return NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::GetDefaultIntrinsicState(PRInt32 *aState)
{
NS_ENSURE_ARG_POINTER(aState);
*aState = kDefaultIntrinsicState;
return NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::GetDisabledIntrinsicState(PRInt32 *aState)
{
NS_ENSURE_ARG_POINTER(aState);
*aState = kDisabledIntrinsicState;
return NS_OK;
}
nsresult
nsXFormsControlStubBase::MaybeAddToModel(nsIModelElementPrivate *aOldModel,
nsIXFormsControl *aParent)
{
if (GetRepeatState() == eType_Template) {
// No sense going any further. A template control has no need to bind,
// refresh, etc. so no reason to put it on the model's control list
return NS_OK;
}
// XXX: just doing pointer comparison would be nice....
PRBool sameModel = PR_FALSE;
nsresult rv;
if (mModel) {
nsCOMPtr<nsIDOM3Node> n3Model(do_QueryInterface(mModel));
nsCOMPtr<nsIDOMNode> nOldModel(do_QueryInterface(aOldModel));
NS_ASSERTION(n3Model, "model element not supporting nsIDOM3Node?!");
rv = n3Model->IsSameNode(nOldModel, &sameModel);
NS_ENSURE_SUCCESS(rv, rv);
} else {
sameModel = !aOldModel;
}
if (!sameModel) {
if (aOldModel) {
rv = aOldModel->RemoveFormControl(this);
NS_ENSURE_SUCCESS(rv, rv);
}
if (mModel) {
rv = mModel->AddFormControl(this, aParent);
NS_ENSURE_SUCCESS(rv, rv);
}
}
return NS_OK;
}
nsresult
nsXFormsControlStubBase::ProcessNodeBinding(const nsString &aBindingAttr,
PRUint16 aResultType,
nsIDOMXPathResult **aResult,
nsIModelElementPrivate **aModel)
{
nsStringArray indexesUsed;
if (aResult) {
*aResult = nsnull;
}
if (aModel) {
*aModel = nsnull;
}
// let's not go through all of this rigamarol if we don't have a chance
// in heck of binding anyhow. Check to see if the models will be receptive
// to some binding. readyForBindProperty is set when they are. Make sure
// to return NS_OK so that we don't start complaining about binding
// failures in this situation.
if (!nsXFormsUtils::IsDocumentReadyForBind(mElement)) {
nsXFormsModelElement::DeferElementBind(this);
return NS_OK_XFORMS_DEFERRED;
}
nsresult rv;
PRBool usesModelBinding;
nsCOMPtr<nsIModelElementPrivate> oldModel(mModel);
nsCOMPtr<nsIXFormsControl> parentControl;
rv = nsXFormsUtils::EvaluateNodeBinding(mElement,
kElementFlags,
aBindingAttr,
EmptyString(),
aResultType,
getter_AddRefs(mModel),
aResult,
&usesModelBinding,
getter_AddRefs(parentControl),
&mDependencies,
&indexesUsed);
NS_ENSURE_SUCCESS(rv, rv);
// if NS_OK_XFORMS_NOTREADY, mModel probably won't exist or might no longer
// be valid, so shouldn't continue.
if (rv == NS_OK_XFORMS_NOTREADY) {
return rv;
}
NS_ENSURE_STATE(mModel);
rv = MaybeAddToModel(oldModel, parentControl);
NS_ENSURE_SUCCESS(rv, rv);
if (aModel)
NS_ADDREF(*aModel = mModel);
mUsesModelBinding = usesModelBinding;
if (indexesUsed.Count()) {
// add index listeners on repeat elements
for (PRInt32 i = 0; i < indexesUsed.Count(); ++i) {
// Find the repeat element and add |this| as a listener
nsCOMPtr<nsIDOMElement> repElem;
nsXFormsUtils::GetElementByContextId(mElement, *(indexesUsed[i]),
getter_AddRefs(repElem));
nsCOMPtr<nsIXFormsRepeatElement> rep(do_QueryInterface(repElem));
if (!rep)
continue;
rv = rep->AddIndexUser(this);
NS_ENSURE_SUCCESS(rv, rv);
rv = mIndexesUsed.AppendObject(rep);
NS_ENSURE_SUCCESS(rv, rv);
}
}
return NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::BindToModel(PRBool aSetBoundNode)
{
if (GetRepeatState() == eType_Template) {
// No sense going any further. A template control has no need to bind,
// refresh, etc. so no reason to put it on the model's control list
return NS_OK;
}
nsCOMPtr<nsIModelElementPrivate> oldModel(mModel);
nsCOMPtr<nsIXFormsControl> parentControl;
nsCOMPtr<nsIDOMNode> boundNode;
mModel = nsXFormsUtils::GetModel(mElement, getter_AddRefs(parentControl),
kElementFlags,
getter_AddRefs(boundNode));
if (aSetBoundNode) {
mBoundNode.swap(boundNode);
}
return MaybeAddToModel(oldModel, parentControl);
}
void
nsXFormsControlStubBase::ResetHelpAndHint(PRBool aInitialize)
{
nsCOMPtr<nsIDOMEventTarget> targ(do_QueryInterface(mElement));
if (!targ)
return;
NS_NAMED_LITERAL_STRING(mouseover, "mouseover");
NS_NAMED_LITERAL_STRING(mouseout, "mouseout");
NS_NAMED_LITERAL_STRING(focus, "focus");
NS_NAMED_LITERAL_STRING(blur, "blur");
NS_NAMED_LITERAL_STRING(keypress, "keypress");
if (mEventListener) {
targ->RemoveEventListener(mouseover, mEventListener, PR_TRUE);
targ->RemoveEventListener(mouseout, mEventListener, PR_TRUE);
targ->RemoveEventListener(focus, mEventListener, PR_TRUE);
targ->RemoveEventListener(blur, mEventListener, PR_TRUE);
targ->RemoveEventListener(keypress, mEventListener, PR_TRUE);
mEventListener = nsnull;
}
if (aInitialize) {
mEventListener = new nsXFormsHintHelpListener();
if (!mEventListener)
return;
targ->AddEventListener(mouseover, mEventListener, PR_TRUE);
targ->AddEventListener(mouseout, mEventListener, PR_TRUE);
targ->AddEventListener(focus, mEventListener, PR_TRUE);
targ->AddEventListener(blur, mEventListener, PR_TRUE);
targ->AddEventListener(keypress, mEventListener, PR_TRUE);
}
}
PRBool
nsXFormsControlStubBase::GetRelevantState()
{
PRBool res = PR_FALSE;
nsCOMPtr<nsIContent> content(do_QueryInterface(mElement));
if (content && (content->IntrinsicState() & NS_EVENT_STATE_ENABLED)) {
res = PR_TRUE;
}
return res;
}
nsresult
nsXFormsControlStubBase::HandleDefault(nsIDOMEvent *aEvent,
PRBool *aHandled)
{
NS_ENSURE_ARG(aHandled);
*aHandled = PR_FALSE;
if (nsXFormsUtils::EventHandlingAllowed(aEvent, mElement)) {
// Check that we are the target of the event
nsCOMPtr<nsIDOMEventTarget> target;
aEvent->GetTarget(getter_AddRefs(target));
nsCOMPtr<nsIDOMElement> targetE(do_QueryInterface(target));
if (targetE && targetE != mElement) {
return NS_OK;
}
// Handle event
nsAutoString type;
aEvent->GetType(type);
if (type.EqualsASCII(sXFormsEventsEntries[eEvent_Focus].name)) {
TryFocus(aHandled);
} else if (type.Equals(NS_LITERAL_STRING("keypress"))) {
nsCOMPtr<nsIDOMKeyEvent> keyEvent = do_QueryInterface(aEvent);
if (keyEvent) {
PRUint32 keycode;
keyEvent->GetKeyCode(&keycode);
if (keycode == nsIDOMKeyEvent::DOM_VK_TAB) {
PRBool extraKey = PR_FALSE;
keyEvent->GetAltKey(&extraKey);
if (extraKey) {
return NS_OK;
}
keyEvent->GetCtrlKey(&extraKey);
if (extraKey) {
return NS_OK;
}
keyEvent->GetMetaKey(&extraKey);
if (extraKey) {
return NS_OK;
}
keyEvent->GetShiftKey(&extraKey);
mPreventLoop = PR_TRUE;
if (extraKey) {
nsXFormsUtils::DispatchEvent(mElement, eEvent_Previous);
} else {
nsXFormsUtils::DispatchEvent(mElement, eEvent_Next);
}
}
}
} else if (type.EqualsASCII(sXFormsEventsEntries[eEvent_Next].name) ||
type.EqualsASCII(sXFormsEventsEntries[eEvent_Previous].name)) {
// only continue this processing if xforms-next or xforms-previous were
// dispatched by the form and not as part of the 'tab' and 'shift+tab'
// processing
if (mPreventLoop) {
mPreventLoop = PR_FALSE;
return NS_OK;
}
nsCOMPtr<nsIDOMDocument> domDoc;
mElement->GetOwnerDocument(getter_AddRefs(domDoc));
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
// element isn't in a document, yet? Odd, indeed. Well, if not in
// document, these two events have no meaning.
NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
// An inelegant way to retrieve this to be sure, but we are
// guaranteed that the focus controller outlives us, so it
// is safe to hold on to it (since we can't die until it has
// died).
nsIFocusController *focusController =
doc->GetWindow()->GetRootFocusController();
if (focusController &&
type.EqualsASCII(sXFormsEventsEntries[eEvent_Next].name)) {
focusController->MoveFocus(PR_TRUE, nsnull);
} else {
focusController->MoveFocus(PR_FALSE, nsnull);
}
} else if (type.EqualsASCII(sXFormsEventsEntries[eEvent_BindingException].name)) {
// we threw up a popup during the nsXFormsUtils::DispatchEvent that sent
// this error to this control
*aHandled = PR_TRUE;
}
}
return NS_OK;
}
#ifdef DEBUG_smaug
static nsVoidArray* sControlList = nsnull;
class ControlDebug
{
public:
ControlDebug() {
sControlList = new nsVoidArray();
NS_ASSERTION(sControlList, "Out of memory!");
}
~ControlDebug() {
for (PRInt32 i = 0; i < sControlList->Count(); ++i) {
nsXFormsControlStubBase* control =
NS_STATIC_CAST(nsXFormsControlStubBase*, sControlList->ElementAt(i));
if (control) {
printf("Possible leak, <xforms:%s>\n", control->Name());
}
}
delete sControlList;
sControlList = nsnull;
}
};
static ControlDebug tester = ControlDebug();
#endif
nsresult
nsXFormsControlStubBase::Create(nsIXTFElementWrapper *aWrapper)
{
aWrapper->SetNotificationMask(kStandardNotificationMask);
// It's ok to keep a weak pointer to mElement. mElement will have an
// owning reference to this object, so as long as we null out mElement in
// OnDestroyed, it will always be valid.
nsCOMPtr<nsIDOMElement> node;
aWrapper->GetElementNode(getter_AddRefs(node));
mElement = node;
NS_ASSERTION(mElement, "Wrapper is not an nsIDOMElement, we'll crash soon");
#ifdef DEBUG_smaug
sControlList->AppendElement(this);
#endif
return NS_OK;
}
nsresult
nsXFormsControlStubBase::OnDestroyed()
{
RemoveIndexListeners();
mDependencies.Clear();
if (mModel) {
mModel->RemoveFormControl(this);
mModel = nsnull;
}
mAbortedBindListContainer = nsnull;
#ifdef DEBUG_smaug
sControlList->RemoveElement(this);
#endif
mElement = nsnull;
return NS_OK;
}
nsresult
nsXFormsControlStubBase::ForceModelDetach(PRBool aRebind)
{
// We shouldn't bother binding if the control is part of a template, but we
// need to run through RemoveFormControl in the event that this control
// was just moved under a repeat or itemset and wasn't there originally.
if (mModel) {
// Remove from model, so Bind() will be forced to reattach
mModel->RemoveFormControl(this);
mModel = nsnull;
}
if (!aRebind || GetRepeatState() == eType_Template) {
return NS_OK;
}
PRBool dummy;
nsresult rv = Bind(&dummy);
NS_ENSURE_SUCCESS(rv, rv);
if (rv == NS_OK_XFORMS_DEFERRED || rv == NS_OK_XFORMS_NOTREADY) {
return NS_OK;
}
// If there were any controls that had already tried to bind but failed
// to because this control wasn't ready yet, then try them again.
PRInt32 arraySize = mAbortedBindList.Count();
if (arraySize) {
for (PRInt32 i = 0; i < arraySize; ++i) {
nsCOMPtr<nsIXFormsControl> control = mAbortedBindList.ObjectAt(i);
if (control) {
control->RebindAndRefresh();
control->SetAbortedBindListContainer(nsnull);
}
}
mAbortedBindList.Clear();
}
return Refresh();
}
nsresult
nsXFormsControlStubBase::WillChangeDocument(nsIDOMDocument *aNewDocument)
{
// This control is moving to another document or just plain getting removed
// from its current document. In either case, we know that we don't care
// about the bind-ready notification we might have been waiting on.
nsCOMPtr<nsIXFormsContextControl> ctxtControl;
GetAbortedBindListContainer(getter_AddRefs(ctxtControl));
if (ctxtControl) {
ctxtControl->AddRemoveAbortedControl(this, PR_FALSE);
}
// If we are in the middle of getting set up in the current document then
// then we need to make sure that if other controls getting set up in
// the current document are depending on this control to help them, that
// we remove that dependency.
PRInt32 arraySize = mAbortedBindList.Count();
if (arraySize) {
for (PRInt32 i = 0; i < arraySize; ++i) {
nsCOMPtr<nsIXFormsControl> control = mAbortedBindList.ObjectAt(i);
control->SetAbortedBindListContainer(nsnull);
}
mAbortedBindList.Clear();
}
SetRepeatState(eType_Unknown);
ResetHelpAndHint(PR_FALSE);
return NS_OK;
}
nsresult
nsXFormsControlStubBase::DocumentChanged(nsIDOMDocument *aNewDocument)
{
mHasDoc = aNewDocument != nsnull;
if (aNewDocument) {
ResetHelpAndHint(PR_TRUE);
// If we are inserted into a document and we have no model, we are probably
// being initialized, so we should set our intrinsic state to the default
// value
if (!mModel && mElement) {
nsCOMPtr<nsIXTFElementWrapper> xtfWrap(do_QueryInterface(mElement));
NS_ENSURE_STATE(xtfWrap);
PRInt32 iState;
GetDefaultIntrinsicState(&iState);
xtfWrap->SetIntrinsicState(iState);
}
}
nsCOMPtr<nsIDOMNode> parent;
mElement->GetParentNode(getter_AddRefs(parent));
UpdateRepeatState(parent);
return ForceModelDetach(mHasParent && mHasDoc);
}
nsresult
nsXFormsControlStubBase::WillChangeParent(nsIDOMElement *aNewParent)
{
nsCOMPtr<nsIXFormsContextControl> ctxtControl;
GetAbortedBindListContainer(getter_AddRefs(ctxtControl));
if (ctxtControl) {
ctxtControl->AddRemoveAbortedControl(this, PR_FALSE);
}
SetRepeatState(eType_Unknown);
return NS_OK;
}
nsresult
nsXFormsControlStubBase::ParentChanged(nsIDOMElement *aNewParent)
{
mHasParent = aNewParent != nsnull;
UpdateRepeatState(aNewParent);
// We need to re-evaluate our instance data binding when our parent changes,
// since xmlns declarations or our context could have changed.
return ForceModelDetach(mHasParent && mHasDoc);
}
nsresult
nsXFormsControlStubBase::WillSetAttribute(nsIAtom *aName, const nsAString &aValue)
{
BeforeSetAttribute(aName, aValue);
return NS_OK;
}
nsresult
nsXFormsControlStubBase::AttributeSet(nsIAtom *aName, const nsAString &aValue)
{
AfterSetAttribute(aName);
return NS_OK;
}
nsresult
nsXFormsControlStubBase::WillRemoveAttribute(nsIAtom *aName)
{
BeforeSetAttribute(aName, EmptyString());
return NS_OK;
}
nsresult
nsXFormsControlStubBase::AttributeRemoved(nsIAtom *aName)
{
AfterSetAttribute(aName);
return NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::RebindAndRefresh()
{
ForceModelDetach(PR_TRUE);
return NS_OK;
}
// nsIXFormsContextControl
NS_IMETHODIMP
nsXFormsControlStubBase::SetContext(nsIDOMNode *aContextNode,
PRInt32 aContextPosition,
PRInt32 aContextSize)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsXFormsControlStubBase::GetContext(nsAString &aModelID,
nsIDOMNode **aContextNode,
PRInt32 *aContextPosition,
PRInt32 *aContextSize)
{
NS_ENSURE_ARG(aContextSize);
NS_ENSURE_ARG(aContextPosition);
NS_ENSURE_ARG_POINTER(aContextNode);
*aContextPosition = 1;
*aContextSize = 1;
*aContextNode = nsnull;
if (aContextNode) {
if (mBoundNode) {
// We are bound to a node: This is the context node
NS_ADDREF(*aContextNode = mBoundNode);
} else if (mModel) {
// If there is no bound node, it could be because everything isn't ready,
// yet. If mHasDoc or mHasParent are false, then we haven't had a chance
// to bind. This could happen if the nodes in this chain are being added
// via DOM manipulation rather than via the parser.
if (HasBindingAttribute() && (!mHasDoc || !mHasParent)) {
return NS_OK_XFORMS_NOTREADY;
}
// We are bound to a model: The document element of its default instance
// document is the context node
nsCOMPtr<nsIDOMDocument> instanceDoc;
mModel->GetInstanceDocument(EmptyString(), getter_AddRefs(instanceDoc));
NS_ENSURE_STATE(instanceDoc);
nsIDOMElement* docElement;
instanceDoc->GetDocumentElement(&docElement); // addrefs
NS_ENSURE_STATE(docElement);
*aContextNode = docElement; // addref'ed above
} else {
if (HasBindingAttribute() && (!mHasDoc || !mHasParent)) {
return NS_OK_XFORMS_NOTREADY;
}
}
}
///
/// @todo expensive to run this
nsCOMPtr<nsIDOMElement> model = do_QueryInterface(mModel);
if (model) {
model->GetAttribute(NS_LITERAL_STRING("id"), aModelID);
}
return NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::AddRemoveAbortedControl(nsIXFormsControl *aControl,
PRBool aAdd)
{
nsresult rv = NS_OK;
if (aAdd) {
rv = mAbortedBindList.AppendObject(aControl);
aControl->SetAbortedBindListContainer(this);
} else {
rv = mAbortedBindList.RemoveObject(aControl);
aControl->SetAbortedBindListContainer(nsnull);
}
return rv;
}
NS_IMETHODIMP
nsXFormsControlStubBase::GetAbortedBindListContainer(
nsIXFormsContextControl **aControlContainingList)
{
NS_ENSURE_ARG_POINTER(aControlContainingList);
NS_IF_ADDREF(*aControlContainingList = mAbortedBindListContainer);
return NS_OK;
}
NS_IMETHODIMP
nsXFormsControlStubBase::SetAbortedBindListContainer(
nsIXFormsContextControl *aControlContainingList)
{
// A control should never be on more than one aborted bind list. There is
// probably a chance through some crazy DOM stirring for a control to be
// waiting on notification from a context control and then the control finds
// itself trying to bind again under a whole different parent tree. So we'll
// have the control remove itself from the old list and link itself to the
// new list.
if (mAbortedBindListContainer && aControlContainingList) {
if (!SameCOMIdentity(mAbortedBindListContainer, aControlContainingList)) {
mAbortedBindListContainer->AddRemoveAbortedControl(this, PR_FALSE);
}
}
mAbortedBindListContainer = aControlContainingList;
return NS_OK;
}
void
nsXFormsControlStubBase::AddRemoveSNBAttr(nsIAtom *aName, const nsAString &aValue)
{
nsAutoString attrStr, attrValue;
aName->ToString(attrStr);
mElement->GetAttribute(attrStr, attrValue);
// if we are setting a single node binding attribute that we don't already
// have, bump the count.
if (!aValue.IsEmpty() && attrValue.IsEmpty()) {
++mBindAttrsCount;
} else if (!attrValue.IsEmpty()) {
// if we are setting a currently existing binding attribute to have an
// empty value, treat it like the binding attr is being removed.
--mBindAttrsCount;
NS_ASSERTION(mBindAttrsCount>=0, "bad mojo! mBindAttrsCount < 0!");
}
}
PRBool
nsXFormsControlStubBase::IsBindingAttribute(const nsIAtom *aAttr) const
{
if (aAttr == nsXFormsAtoms::bind ||
aAttr == nsXFormsAtoms::ref ||
aAttr == nsXFormsAtoms::model) {
return PR_TRUE;
}
return PR_FALSE;
}
void
nsXFormsControlStubBase::AfterSetAttribute(nsIAtom *aName)
{
if (IsBindingAttribute(aName)) {
PRBool dummy;
nsresult rv = Bind(&dummy);
if (NS_SUCCEEDED(rv) &&
rv != NS_OK_XFORMS_DEFERRED && rv != NS_OK_XFORMS_NOTREADY)
Refresh();
}
}
void
nsXFormsControlStubBase::BeforeSetAttribute(nsIAtom *aName,
const nsAString &aValue)
{
if (IsBindingAttribute(aName)) {
AddRemoveSNBAttr(aName, aValue);
}
}
nsresult
nsXFormsControlStubBase::GetBoundBuiltinType(PRUint16 *aBuiltinType)
{
NS_ENSURE_ARG_POINTER(aBuiltinType);
*aBuiltinType = 0;
NS_ENSURE_STATE(mModel);
// get type of bound instance data
nsCOMPtr<nsISchemaType> schemaType;
nsresult rv = mModel->GetTypeForControl(this, getter_AddRefs(schemaType));
NS_ENSURE_SUCCESS(rv, rv);
return mModel->GetRootBuiltinType(schemaType, aBuiltinType);
}
PRBool
nsXFormsControlStubBase::IsContentAllowed()
{
return PR_TRUE;
}
PRBool
nsXFormsControlStubBase::IsContentComplex()
{
PRBool isComplex = PR_FALSE;
if (mBoundNode) {
// If the bound node has any Element children, then it has complexContent.
nsCOMPtr<nsIDOMNode> currentNode;
mBoundNode->GetFirstChild(getter_AddRefs(currentNode));
while (currentNode) {
PRUint16 nodeType;
currentNode->GetNodeType(&nodeType);
if (nodeType == nsIDOMNode::ELEMENT_NODE) {
isComplex = PR_TRUE;
break;
}
nsCOMPtr<nsIDOMNode> node;
currentNode->GetNextSibling(getter_AddRefs(node));
currentNode.swap(node);
}
}
return isComplex;
}
nsRepeatState
nsXFormsControlStubBase::GetRepeatState()
{
return mRepeatState;
}
void
nsXFormsControlStubBase::SetRepeatState(nsRepeatState aState)
{
mRepeatState = aState;
return;
}
nsRepeatState
nsXFormsControlStubBase::UpdateRepeatState(nsIDOMNode *aParent)
{
// Walk up the parent chain looking to see if the this control is contained
// in an item. If it is and that item is contained in a itemset, then we
// know that this control was generated as a clone from the itemset's
// template. Similarly, we'll check to see if this control lives in a
// contextcontainer (meaning it was cloned from a repeat's template).
// Otherwise, if neither of these are the case but it lives under a repeat
// or an itemset, then this control must be part of a template. A template
// is the content of a repeat or itemset that gets cloned once for every
// node in the bound nodeset.
//
// If none of this applies, we'll return eType_NotApplicable to show that this
// control isn't bound to a repeating nodeset.
nsRepeatState repeatState = eType_NotApplicable;
if (!mHasDoc || !mHasParent) {
// If we don't have a document or a parent, none of these tests will work
// correctly so no sense doing them now. If either of these are false the
// repeat state for the object should already be eType_Unknown so just
// return that now.
return eType_Unknown;
}
nsCOMPtr<nsIDOMNode> parent = aParent;
PRBool childIsItem = PR_FALSE;
while (parent) {
if (nsXFormsUtils::IsXFormsElement(parent,
NS_LITERAL_STRING("contextcontainer"))) {
repeatState = eType_GeneratedContent;
break;
}
if (nsXFormsUtils::IsXFormsElement(parent, NS_LITERAL_STRING("repeat"))) {
repeatState = eType_Template;
break;
}
if (nsXFormsUtils::IsXFormsElement(parent, NS_LITERAL_STRING("itemset"))) {
if (childIsItem) {
repeatState = eType_GeneratedContent;
break;
}
repeatState = eType_Template;
break;
}
if (nsXFormsUtils::IsXFormsElement(parent, NS_LITERAL_STRING("item"))) {
childIsItem = PR_TRUE;
} else {
nsCOMPtr<nsIDOMElement> parentEle(do_QueryInterface(parent));
if (!parentEle) {
// I don't know how this can possibly happen, but if it does I guess
// we should just ignore it and coninue on our way.
break;
}
// if this control is contained underneath an element that contains
// an xforms binding attribute that introduces an anonymous xf:repeat
// then the control is part of a template
PRBool repeatAttr = PR_FALSE;
parentEle->HasAttributeNS(NS_LITERAL_STRING(NS_NAMESPACE_XFORMS),
NS_LITERAL_STRING("repeat-bind"),
&repeatAttr);
if (repeatAttr) {
repeatState = eType_Template;
break;
}
parentEle->HasAttributeNS(NS_LITERAL_STRING(NS_NAMESPACE_XFORMS),
NS_LITERAL_STRING("repeat-nodeset"),
&repeatAttr);
if (repeatAttr) {
repeatState = eType_Template;
break;
}
}
nsCOMPtr<nsIDOMNode> tmp;
parent->GetParentNode(getter_AddRefs(tmp));
parent = tmp;
}
SetRepeatState(repeatState);
return repeatState;
}
NS_IMPL_ISUPPORTS_INHERITED3(nsXFormsBindableControlStub,
nsXFormsBindableStub,
nsIXFormsContextControl,
nsIXFormsControl,
nsIXFormsControlBase)
|