From a97d3948208f3f823b691cb1714c841538ae755c Mon Sep 17 00:00:00 2001 From: "pollmann%netscape.com" Date: Wed, 21 Jun 2000 00:40:11 +0000 Subject: [PATCH] Bug 25300: Enable activation of buttons by the keyboard (space or enter) r=nisheeth --- .../html/content/src/nsHTMLButtonElement.cpp | 32 ++++++++++++ .../html/content/src/nsHTMLInputElement.cpp | 50 ++++++++----------- layout/forms/nsHTMLButtonControlFrame.cpp | 8 +-- .../html/content/src/nsHTMLButtonElement.cpp | 32 ++++++++++++ .../html/content/src/nsHTMLInputElement.cpp | 50 ++++++++----------- .../forms/src/nsHTMLButtonControlFrame.cpp | 8 +-- 6 files changed, 108 insertions(+), 72 deletions(-) diff --git a/content/html/content/src/nsHTMLButtonElement.cpp b/content/html/content/src/nsHTMLButtonElement.cpp index c0ea0ba03c02..919d07d21d55 100644 --- a/content/html/content/src/nsHTMLButtonElement.cpp +++ b/content/html/content/src/nsHTMLButtonElement.cpp @@ -481,6 +481,38 @@ nsHTMLButtonElement::HandleDOMEvent(nsIPresContext* aPresContext, if ((NS_OK == ret) && (nsEventStatus_eIgnore == *aEventStatus) && !(aFlags & NS_EVENT_FLAG_CAPTURE)) { switch (aEvent->message) { + + case NS_KEY_PRESS: + { + // For backwards compat, trigger buttons with space or enter (bug 25300) + nsKeyEvent * keyEvent = (nsKeyEvent *)aEvent; + if (keyEvent->keyCode == NS_VK_RETURN || keyEvent->charCode == NS_VK_SPACE) { + nsEventStatus status = nsEventStatus_eIgnore; + nsMouseEvent event; + event.eventStructType = NS_GUI_EVENT; + event.message = NS_MOUSE_LEFT_CLICK; + event.isShift = PR_FALSE; + event.isControl = PR_FALSE; + event.isAlt = PR_FALSE; + event.isMeta = PR_FALSE; + event.clickCount = 0; + event.widget = nsnull; + rv = HandleDOMEvent(aPresContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status); + } + } + break;// NS_KEY_PRESS + + case NS_MOUSE_LEFT_CLICK: + { + // Tell the frame about the click + nsIFormControlFrame* formControlFrame = nsnull; + rv = nsGenericHTMLElement::GetPrimaryFrame(this, formControlFrame); + if (NS_SUCCEEDED(rv)) { + formControlFrame->MouseClicked(aPresContext); + } + } + break;// NS_MOUSE_LEFT_CLICK + case NS_MOUSE_LEFT_BUTTON_DOWN: { nsIEventStateManager *stateManager; diff --git a/content/html/content/src/nsHTMLInputElement.cpp b/content/html/content/src/nsHTMLInputElement.cpp index 557b9cfc0cf2..554d03e3d501 100644 --- a/content/html/content/src/nsHTMLInputElement.cpp +++ b/content/html/content/src/nsHTMLInputElement.cpp @@ -845,40 +845,32 @@ nsHTMLInputElement::HandleDOMEvent(nsIPresContext* aPresContext, case NS_KEY_PRESS: { + // For backwards compat, trigger checks/radios/buttons with space or enter (bug 25300) nsKeyEvent * keyEvent = (nsKeyEvent *)aEvent; - if (keyEvent->keyCode == NS_VK_RETURN || keyEvent->charCode == 0x20) { + if (keyEvent->keyCode == NS_VK_RETURN || keyEvent->charCode == NS_VK_SPACE) { PRInt32 type; GetType(&type); switch(type) { - case NS_FORM_INPUT_CHECKBOX: + case NS_FORM_INPUT_CHECKBOX: + case NS_FORM_INPUT_RADIO: + case NS_FORM_INPUT_BUTTON: + case NS_FORM_INPUT_RESET: + case NS_FORM_INPUT_SUBMIT: +// case NS_FORM_INPUT_IMAGE: // XXX should we do this for images too? { - PRBool checked; - GetChecked(&checked); - SetChecked(!checked); - } - break; - case NS_FORM_INPUT_RADIO: - SetChecked(PR_TRUE); - break; - case NS_FORM_INPUT_BUTTON: - case NS_FORM_INPUT_RESET: - case NS_FORM_INPUT_SUBMIT: - { - //Checkboxes and radio trigger off return or space but buttons - //just trigger off space, go figure. - if (keyEvent->charCode == 0x20) { - //XXX We should just be able to call Click() here but then - //Click wouldn't have a PresContext. - nsIFormControlFrame* formControlFrame = nsnull; - if (NS_OK == nsGenericHTMLElement::GetPrimaryFrame(this, formControlFrame)) { - if (formControlFrame) { - formControlFrame->MouseClicked(aPresContext); - } - } - } - } - break; - } + nsEventStatus status = nsEventStatus_eIgnore; + nsMouseEvent event; + event.eventStructType = NS_GUI_EVENT; + event.message = NS_MOUSE_LEFT_CLICK; + event.isShift = PR_FALSE; + event.isControl = PR_FALSE; + event.isAlt = PR_FALSE; + event.isMeta = PR_FALSE; + event.clickCount = 0; + event.widget = nsnull; + rv = HandleDOMEvent(aPresContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status); + } // case + } // switch } } break;// NS_KEY_PRESS diff --git a/layout/forms/nsHTMLButtonControlFrame.cpp b/layout/forms/nsHTMLButtonControlFrame.cpp index 456b875bd4a0..7242bd1fed81 100644 --- a/layout/forms/nsHTMLButtonControlFrame.cpp +++ b/layout/forms/nsHTMLButtonControlFrame.cpp @@ -390,13 +390,7 @@ nsHTMLButtonControlFrame::HandleEvent(nsIPresContext* aPresContext, return NS_OK; } - // lets see if the button was clicked. -EDV - switch (aEvent->message) { - case NS_MOUSE_LEFT_CLICK: - MouseClicked(aPresContext); - break; - } - + // mouse clicks are handled by content // we don't want our children to get any events. So just pass it to frame. return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus); } diff --git a/layout/html/content/src/nsHTMLButtonElement.cpp b/layout/html/content/src/nsHTMLButtonElement.cpp index c0ea0ba03c02..919d07d21d55 100644 --- a/layout/html/content/src/nsHTMLButtonElement.cpp +++ b/layout/html/content/src/nsHTMLButtonElement.cpp @@ -481,6 +481,38 @@ nsHTMLButtonElement::HandleDOMEvent(nsIPresContext* aPresContext, if ((NS_OK == ret) && (nsEventStatus_eIgnore == *aEventStatus) && !(aFlags & NS_EVENT_FLAG_CAPTURE)) { switch (aEvent->message) { + + case NS_KEY_PRESS: + { + // For backwards compat, trigger buttons with space or enter (bug 25300) + nsKeyEvent * keyEvent = (nsKeyEvent *)aEvent; + if (keyEvent->keyCode == NS_VK_RETURN || keyEvent->charCode == NS_VK_SPACE) { + nsEventStatus status = nsEventStatus_eIgnore; + nsMouseEvent event; + event.eventStructType = NS_GUI_EVENT; + event.message = NS_MOUSE_LEFT_CLICK; + event.isShift = PR_FALSE; + event.isControl = PR_FALSE; + event.isAlt = PR_FALSE; + event.isMeta = PR_FALSE; + event.clickCount = 0; + event.widget = nsnull; + rv = HandleDOMEvent(aPresContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status); + } + } + break;// NS_KEY_PRESS + + case NS_MOUSE_LEFT_CLICK: + { + // Tell the frame about the click + nsIFormControlFrame* formControlFrame = nsnull; + rv = nsGenericHTMLElement::GetPrimaryFrame(this, formControlFrame); + if (NS_SUCCEEDED(rv)) { + formControlFrame->MouseClicked(aPresContext); + } + } + break;// NS_MOUSE_LEFT_CLICK + case NS_MOUSE_LEFT_BUTTON_DOWN: { nsIEventStateManager *stateManager; diff --git a/layout/html/content/src/nsHTMLInputElement.cpp b/layout/html/content/src/nsHTMLInputElement.cpp index 557b9cfc0cf2..554d03e3d501 100644 --- a/layout/html/content/src/nsHTMLInputElement.cpp +++ b/layout/html/content/src/nsHTMLInputElement.cpp @@ -845,40 +845,32 @@ nsHTMLInputElement::HandleDOMEvent(nsIPresContext* aPresContext, case NS_KEY_PRESS: { + // For backwards compat, trigger checks/radios/buttons with space or enter (bug 25300) nsKeyEvent * keyEvent = (nsKeyEvent *)aEvent; - if (keyEvent->keyCode == NS_VK_RETURN || keyEvent->charCode == 0x20) { + if (keyEvent->keyCode == NS_VK_RETURN || keyEvent->charCode == NS_VK_SPACE) { PRInt32 type; GetType(&type); switch(type) { - case NS_FORM_INPUT_CHECKBOX: + case NS_FORM_INPUT_CHECKBOX: + case NS_FORM_INPUT_RADIO: + case NS_FORM_INPUT_BUTTON: + case NS_FORM_INPUT_RESET: + case NS_FORM_INPUT_SUBMIT: +// case NS_FORM_INPUT_IMAGE: // XXX should we do this for images too? { - PRBool checked; - GetChecked(&checked); - SetChecked(!checked); - } - break; - case NS_FORM_INPUT_RADIO: - SetChecked(PR_TRUE); - break; - case NS_FORM_INPUT_BUTTON: - case NS_FORM_INPUT_RESET: - case NS_FORM_INPUT_SUBMIT: - { - //Checkboxes and radio trigger off return or space but buttons - //just trigger off space, go figure. - if (keyEvent->charCode == 0x20) { - //XXX We should just be able to call Click() here but then - //Click wouldn't have a PresContext. - nsIFormControlFrame* formControlFrame = nsnull; - if (NS_OK == nsGenericHTMLElement::GetPrimaryFrame(this, formControlFrame)) { - if (formControlFrame) { - formControlFrame->MouseClicked(aPresContext); - } - } - } - } - break; - } + nsEventStatus status = nsEventStatus_eIgnore; + nsMouseEvent event; + event.eventStructType = NS_GUI_EVENT; + event.message = NS_MOUSE_LEFT_CLICK; + event.isShift = PR_FALSE; + event.isControl = PR_FALSE; + event.isAlt = PR_FALSE; + event.isMeta = PR_FALSE; + event.clickCount = 0; + event.widget = nsnull; + rv = HandleDOMEvent(aPresContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status); + } // case + } // switch } } break;// NS_KEY_PRESS diff --git a/layout/html/forms/src/nsHTMLButtonControlFrame.cpp b/layout/html/forms/src/nsHTMLButtonControlFrame.cpp index 456b875bd4a0..7242bd1fed81 100644 --- a/layout/html/forms/src/nsHTMLButtonControlFrame.cpp +++ b/layout/html/forms/src/nsHTMLButtonControlFrame.cpp @@ -390,13 +390,7 @@ nsHTMLButtonControlFrame::HandleEvent(nsIPresContext* aPresContext, return NS_OK; } - // lets see if the button was clicked. -EDV - switch (aEvent->message) { - case NS_MOUSE_LEFT_CLICK: - MouseClicked(aPresContext); - break; - } - + // mouse clicks are handled by content // we don't want our children to get any events. So just pass it to frame. return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus); }