summaryrefslogtreecommitdiffstats
path: root/layout/xul/nsScrollbarButtonFrame.cpp
blob: c15be52e579f406f6045f8aa4695c946713487fc (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//
// Eric Vaughan
// Netscape Communications
//
// See documentation in associated header file
//

#include "nsScrollbarButtonFrame.h"
#include "nsPresContext.h"
#include "nsIContent.h"
#include "nsCOMPtr.h"
#include "nsNameSpaceManager.h"
#include "nsGkAtoms.h"
#include "nsLayoutUtils.h"
#include "nsSliderFrame.h"
#include "nsScrollbarFrame.h"
#include "nsIScrollbarMediator.h"
#include "nsRepeatService.h"
#include "mozilla/LookAndFeel.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/PresShell.h"
#include "mozilla/Telemetry.h"

using namespace mozilla;

//
// NS_NewToolbarFrame
//
// Creates a new Toolbar frame and returns it
//
nsIFrame* NS_NewScrollbarButtonFrame(PresShell* aPresShell,
                                     ComputedStyle* aStyle) {
  return new (aPresShell)
      nsScrollbarButtonFrame(aStyle, aPresShell->GetPresContext());
}
NS_IMPL_FRAMEARENA_HELPERS(nsScrollbarButtonFrame)

nsresult nsScrollbarButtonFrame::HandleEvent(nsPresContext* aPresContext,
                                             WidgetGUIEvent* aEvent,
                                             nsEventStatus* aEventStatus) {
  NS_ENSURE_ARG_POINTER(aEventStatus);

  // If a web page calls event.preventDefault() we still want to
  // scroll when scroll arrow is clicked. See bug 511075.
  if (!mContent->IsInNativeAnonymousSubtree() &&
      nsEventStatus_eConsumeNoDefault == *aEventStatus) {
    return NS_OK;
  }

  switch (aEvent->mMessage) {
    case eMouseDown:
      mCursorOnThis = true;
      // if we didn't handle the press ourselves, pass it on to the superclass
      if (HandleButtonPress(aPresContext, aEvent, aEventStatus)) {
        return NS_OK;
      }
      break;
    case eMouseUp:
      HandleRelease(aPresContext, aEvent, aEventStatus);
      break;
    case eMouseOut:
      mCursorOnThis = false;
      break;
    case eMouseMove: {
      nsPoint cursor = nsLayoutUtils::GetEventCoordinatesRelativeTo(
          aEvent, RelativeTo{this});
      nsRect frameRect(nsPoint(0, 0), GetSize());
      mCursorOnThis = frameRect.Contains(cursor);
      break;
    }
    default:
      break;
  }

  return SimpleXULLeafFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
}

bool nsScrollbarButtonFrame::HandleButtonPress(nsPresContext* aPresContext,
                                               WidgetGUIEvent* aEvent,
                                               nsEventStatus* aEventStatus) {
  // Get the desired action for the scrollbar button.
  LookAndFeel::IntID tmpAction;
  uint16_t button = aEvent->AsMouseEvent()->mButton;
  if (button == MouseButton::ePrimary) {
    tmpAction = LookAndFeel::IntID::ScrollButtonLeftMouseButtonAction;
  } else if (button == MouseButton::eMiddle) {
    tmpAction = LookAndFeel::IntID::ScrollButtonMiddleMouseButtonAction;
  } else if (button == MouseButton::eSecondary) {
    tmpAction = LookAndFeel::IntID::ScrollButtonRightMouseButtonAction;
  } else {
    return false;
  }

  // Get the button action metric from the pres. shell.
  int32_t pressedButtonAction;
  if (NS_FAILED(LookAndFeel::GetInt(tmpAction, &pressedButtonAction))) {
    return false;
  }

  // get the scrollbar control
  nsIFrame* scrollbar;
  GetParentWithTag(nsGkAtoms::scrollbar, this, scrollbar);

  if (scrollbar == nullptr) return false;

  static dom::Element::AttrValuesArray strings[] = {
      nsGkAtoms::increment, nsGkAtoms::decrement, nullptr};
  int32_t index = mContent->AsElement()->FindAttrValueIn(
      kNameSpaceID_None, nsGkAtoms::type, strings, eCaseMatters);
  int32_t direction;
  if (index == 0)
    direction = 1;
  else if (index == 1)
    direction = -1;
  else
    return false;

  bool repeat = pressedButtonAction != 2;

  PresShell::SetCapturingContent(mContent, CaptureFlags::IgnoreAllowedState);

  AutoWeakFrame weakFrame(this);

  if (nsScrollbarFrame* sb = do_QueryFrame(scrollbar)) {
    nsIScrollbarMediator* m = sb->GetScrollbarMediator();
    switch (pressedButtonAction) {
      case 0:
        sb->SetIncrementToLine(direction);
        if (m) {
          m->ScrollByLine(sb, direction, ScrollSnapFlags::IntendedDirection);
        }
        break;
      case 1:
        sb->SetIncrementToPage(direction);
        if (m) {
          m->ScrollByPage(sb, direction,
                          ScrollSnapFlags::IntendedDirection |
                              ScrollSnapFlags::IntendedEndPosition);
        }
        break;
      case 2:
        sb->SetIncrementToWhole(direction);
        if (m) {
          m->ScrollByWhole(sb, direction, ScrollSnapFlags::IntendedEndPosition);
        }
        break;
      case 3:
      default:
        // We were told to ignore this click, or someone assigned a non-standard
        // value to the button's action.
        return false;
    }
    if (!weakFrame.IsAlive()) {
      return false;
    }

    if (!m) {
      sb->MoveToNewPosition(nsScrollbarFrame::ImplementsScrollByUnit::No);
      if (!weakFrame.IsAlive()) {
        return false;
      }
    }
  }
  if (repeat) {
    StartRepeat();
  }
  return true;
}

NS_IMETHODIMP
nsScrollbarButtonFrame::HandleRelease(nsPresContext* aPresContext,
                                      WidgetGUIEvent* aEvent,
                                      nsEventStatus* aEventStatus) {
  PresShell::ReleaseCapturingContent();
  StopRepeat();
  nsIFrame* scrollbar;
  GetParentWithTag(nsGkAtoms::scrollbar, this, scrollbar);
  nsScrollbarFrame* sb = do_QueryFrame(scrollbar);
  if (sb) {
    nsIScrollbarMediator* m = sb->GetScrollbarMediator();
    if (m) {
      m->ScrollbarReleased(sb);
    }
  }
  return NS_OK;
}

void nsScrollbarButtonFrame::Notify() {
  if (mCursorOnThis ||
      LookAndFeel::GetInt(LookAndFeel::IntID::ScrollbarButtonAutoRepeatBehavior,
                          0)) {
    // get the scrollbar control
    nsIFrame* scrollbar;
    GetParentWithTag(nsGkAtoms::scrollbar, this, scrollbar);
    nsScrollbarFrame* sb = do_QueryFrame(scrollbar);
    if (sb) {
      nsIScrollbarMediator* m = sb->GetScrollbarMediator();
      if (m) {
        m->RepeatButtonScroll(sb);
      } else {
        sb->MoveToNewPosition(nsScrollbarFrame::ImplementsScrollByUnit::No);
      }
    }
  }
}

nsresult nsScrollbarButtonFrame::GetChildWithTag(nsAtom* atom, nsIFrame* start,
                                                 nsIFrame*& result) {
  // recursively search our children
  for (nsIFrame* childFrame : start->PrincipalChildList()) {
    // get the content node
    nsIContent* child = childFrame->GetContent();

    if (child) {
      // see if it is the child
      if (child->IsXULElement(atom)) {
        result = childFrame;

        return NS_OK;
      }
    }

    // recursive search the child
    GetChildWithTag(atom, childFrame, result);
    if (result != nullptr) return NS_OK;
  }

  result = nullptr;
  return NS_OK;
}

nsresult nsScrollbarButtonFrame::GetParentWithTag(nsAtom* toFind,
                                                  nsIFrame* start,
                                                  nsIFrame*& result) {
  while (start) {
    start = start->GetParent();

    if (start) {
      // get the content node
      nsIContent* child = start->GetContent();

      if (child && child->IsXULElement(toFind)) {
        result = start;
        return NS_OK;
      }
    }
  }

  result = nullptr;
  return NS_OK;
}

void nsScrollbarButtonFrame::Destroy(DestroyContext& aContext) {
  // Ensure our repeat service isn't going... it's possible that a scrollbar can
  // disappear out from under you while you're in the process of scrolling.
  StopRepeat();
  SimpleXULLeafFrame::Destroy(aContext);
}