* BMenu now scrolls when you press page up/down, if possible.

* BMenuWindow no longer uses a fixed scroll step - instead, the menu sets it
  to the height of its first item.
* Cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@31389 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2009-07-03 12:09:16 +00:00
parent 9515252e02
commit 4e23bc0383
3 changed files with 136 additions and 80 deletions
+23 -18
View File
@@ -1,13 +1,13 @@
/* /*
* Copyright 2001-2006, Haiku, Inc. * Copyright 2001-2009, Haiku, Inc.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
* Marc Flerackers ([email protected]) * Marc Flerackers ([email protected])
* Stefano Ceccherini ([email protected]) * Stefano Ceccherini ([email protected])
*/ */
#ifndef __MENUWINDOW_H #ifndef MENU_WINDOW_H
#define __MENUWINDOW_H #define MENU_WINDOW_H
#include <Window.h> #include <Window.h>
@@ -22,34 +22,39 @@ class BMenuScroller;
class BMenuWindow : public BWindow { class BMenuWindow : public BWindow {
public: public:
BMenuWindow(const char *name); BMenuWindow(const char* name);
virtual ~BMenuWindow(); virtual ~BMenuWindow();
virtual void DispatchMessage(BMessage *message, BHandler *handler); virtual void DispatchMessage(BMessage* message,
BHandler* handler);
void AttachMenu(BMenu *menu); void AttachMenu(BMenu* menu);
void DetachMenu(); void DetachMenu();
void AttachScrollers(); void AttachScrollers();
void DetachScrollers(); void DetachScrollers();
bool CheckForScrolling(const BPoint &cursor); void SetSmallStep(float step);
bool TryScrollBy(const float &step); void GetSteps(float* _smallStep, float* _largeStep);
bool HasScrollers() const;
bool CheckForScrolling(const BPoint& cursor);
bool TryScrollBy(const float& step);
private: private:
BMenu *fMenu; bool _Scroll(const BPoint& cursor);
BMenuFrame *fMenuFrame; void _ScrollBy(const float& step);
BMenuScroller *fUpperScroller;
BMenuScroller *fLowerScroller;
BMenu* fMenu;
BMenuFrame* fMenuFrame;
BMenuScroller* fUpperScroller;
BMenuScroller* fLowerScroller;
float fScrollStep;
float fValue; float fValue;
float fLimit; float fLimit;
bool _Scroll(const BPoint &cursor);
void _ScrollBy(const float &step);
}; };
} // namespace BPrivate } // namespace BPrivate
#endif // __MENUWINDOW_H #endif // MENU_WINDOW_H
+29 -5
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2008, Haiku, Inc. * Copyright 2001-2009, Haiku, Inc.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -8,6 +8,8 @@
* Rene Gollent ([email protected]) * Rene Gollent ([email protected])
*/ */
#include <Menu.h>
#include <new> #include <new>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
@@ -18,13 +20,13 @@
#include <FindDirectory.h> #include <FindDirectory.h>
#include <Layout.h> #include <Layout.h>
#include <LayoutUtils.h> #include <LayoutUtils.h>
#include <Menu.h>
#include <MenuBar.h> #include <MenuBar.h>
#include <MenuItem.h> #include <MenuItem.h>
#include <Messenger.h> #include <Messenger.h>
#include <Path.h> #include <Path.h>
#include <PropertyInfo.h> #include <PropertyInfo.h>
#include <Screen.h> #include <Screen.h>
#include <ScrollBar.h>
#include <Window.h> #include <Window.h>
#include <AppServerLink.h> #include <AppServerLink.h>
@@ -819,9 +821,7 @@ BMenu::MessageReceived(BMessage *msg)
switch (msg->what) { switch (msg->what) {
case B_MOUSE_WHEEL_CHANGED: case B_MOUSE_WHEEL_CHANGED:
{ {
//float deltaX = 0
float deltaY = 0; float deltaY = 0;
//msg->FindFloat("be:wheel_delta_x", &deltaX);
msg->FindFloat("be:wheel_delta_y", &deltaY); msg->FindFloat("be:wheel_delta_y", &deltaY);
if (deltaY == 0) if (deltaY == 0)
return; return;
@@ -830,7 +830,9 @@ BMenu::MessageReceived(BMessage *msg)
if (window == NULL) if (window == NULL)
return; return;
window->TryScrollBy(deltaY); float smallStep;
window->GetSteps(&smallStep, NULL);
window->TryScrollBy(deltaY * smallStep);
break; break;
} }
default: default:
@@ -890,6 +892,21 @@ BMenu::KeyDown(const char *bytes, int32 numBytes)
} }
break; break;
case B_PAGE_UP:
case B_PAGE_DOWN:
{
BMenuWindow *window = dynamic_cast<BMenuWindow *>(Window());
if (window == NULL || !window->HasScrollers())
break;
int32 deltaY = bytes[0] == B_PAGE_UP ? -1 : 1;
float largeStep;
window->GetSteps(NULL, &largeStep);
window->TryScrollBy(deltaY * largeStep);
break;
}
case B_ENTER: case B_ENTER:
case B_SPACE: case B_SPACE:
if (fSelected) { if (fSelected) {
@@ -1426,6 +1443,13 @@ BMenu::_Show(bool selectFirstItem)
fAttachAborted = false; fAttachAborted = false;
window->AttachMenu(this); window->AttachMenu(this);
if (ItemAt(0) != NULL) {
float width, height;
ItemAt(0)->GetContentSize(&width, &height);
window->SetSmallStep(ceilf(height));
}
// Menu didn't have the time to add its items: aborting... // Menu didn't have the time to add its items: aborting...
if (fAttachAborted) { if (fAttachAborted) {
window->DetachMenu(); window->DetachMenu();
+47 -20
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2007, Haiku, Inc. * Copyright 2001-2009, Haiku, Inc.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -14,6 +14,8 @@
#include <ControlLook.h> #include <ControlLook.h>
#include <Debug.h> #include <Debug.h>
#include <Menu.h> #include <Menu.h>
#include <MenuItem.h>
#include <MenuPrivate.h> #include <MenuPrivate.h>
#include <WindowPrivate.h> #include <WindowPrivate.h>
@@ -67,7 +69,6 @@ using namespace BPrivate;
const int kScrollerHeight = 10; const int kScrollerHeight = 10;
const int kScrollStep = 19;
BMenuScroller::BMenuScroller(BRect frame) BMenuScroller::BMenuScroller(BRect frame)
@@ -243,7 +244,8 @@ BMenuWindow::BMenuWindow(const char *name)
fMenu(NULL), fMenu(NULL),
fMenuFrame(NULL), fMenuFrame(NULL),
fUpperScroller(NULL), fUpperScroller(NULL),
fLowerScroller(NULL) fLowerScroller(NULL),
fScrollStep(19)
{ {
SetSizeLimits(2, 10000, 2, 10000); SetSizeLimits(2, 10000, 2, 10000);
} }
@@ -347,6 +349,35 @@ BMenuWindow::DetachScrollers()
} }
void
BMenuWindow::SetSmallStep(float step)
{
fScrollStep = step;
}
void
BMenuWindow::GetSteps(float* _smallStep, float* _largeStep)
{
if (_smallStep != NULL)
*_smallStep = fScrollStep;
if (_largeStep != NULL) {
if (fMenuFrame != NULL)
*_largeStep = fMenuFrame->Bounds().Height() - fScrollStep;
else
*_largeStep = fScrollStep * 2;
}
}
bool
BMenuWindow::HasScrollers() const
{
return fMenuFrame != NULL && fUpperScroller != NULL
&& fLowerScroller != NULL;
}
bool bool
BMenuWindow::CheckForScrolling(const BPoint &cursor) BMenuWindow::CheckForScrolling(const BPoint &cursor)
{ {
@@ -358,19 +389,18 @@ BMenuWindow::CheckForScrolling(const BPoint &cursor)
bool bool
BMenuWindow::TryScrollBy(const float &step) BMenuWindow::TryScrollBy(const float& step)
{ {
if (!fMenuFrame || !fUpperScroller || !fLowerScroller) if (!fMenuFrame || !fUpperScroller || !fLowerScroller)
return false; return false;
_ScrollBy(step); _ScrollBy(step);
return true; return true;
} }
bool bool
BMenuWindow::_Scroll(const BPoint &where) BMenuWindow::_Scroll(const BPoint& where)
{ {
ASSERT((fLowerScroller != NULL)); ASSERT((fLowerScroller != NULL));
ASSERT((fUpperScroller != NULL)); ASSERT((fUpperScroller != NULL));
@@ -380,11 +410,11 @@ BMenuWindow::_Scroll(const BPoint &where)
BRect lowerFrame = fLowerScroller->Frame(); BRect lowerFrame = fLowerScroller->Frame();
BRect upperFrame = fUpperScroller->Frame(); BRect upperFrame = fUpperScroller->Frame();
if (fLowerScroller->IsEnabled() && lowerFrame.Contains(cursor)) { if (fLowerScroller->IsEnabled() && lowerFrame.Contains(cursor))
_ScrollBy(1); _ScrollBy(1);
} else if (fUpperScroller->IsEnabled() && upperFrame.Contains(cursor)) { else if (fUpperScroller->IsEnabled() && upperFrame.Contains(cursor))
_ScrollBy(-1); _ScrollBy(-1);
} else else
return false; return false;
snooze(5000); snooze(5000);
@@ -394,7 +424,7 @@ BMenuWindow::_Scroll(const BPoint &where)
void void
BMenuWindow::_ScrollBy(const float &step) BMenuWindow::_ScrollBy(const float& step)
{ {
if (step > 0) { if (step > 0) {
if (fValue == 0) { if (fValue == 0) {
@@ -402,17 +432,15 @@ BMenuWindow::_ScrollBy(const float &step)
fUpperScroller->Invalidate(); fUpperScroller->Invalidate();
} }
if (fValue + kScrollStep >= fLimit) { if (fValue + step >= fLimit) {
// If we reached the limit, we don't want to scroll a whole // If we reached the limit, only scroll to the end
// 'step' if not needed.
fMenu->ScrollBy(0, fLimit - fValue); fMenu->ScrollBy(0, fLimit - fValue);
fValue = fLimit; fValue = fLimit;
fLowerScroller->SetEnabled(false); fLowerScroller->SetEnabled(false);
fLowerScroller->Invalidate(); fLowerScroller->Invalidate();
} else { } else {
fMenu->ScrollBy(0, kScrollStep); fMenu->ScrollBy(0, step);
fValue += kScrollStep; fValue += step;
} }
} else if (step < 0) { } else if (step < 0) {
if (fValue == fLimit) { if (fValue == fLimit) {
@@ -420,15 +448,14 @@ BMenuWindow::_ScrollBy(const float &step)
fLowerScroller->Invalidate(); fLowerScroller->Invalidate();
} }
if (fValue - kScrollStep <= 0) { if (fValue + step <= 0) {
fMenu->ScrollBy(0, -fValue); fMenu->ScrollBy(0, -fValue);
fValue = 0; fValue = 0;
fUpperScroller->SetEnabled(false); fUpperScroller->SetEnabled(false);
fUpperScroller->Invalidate(); fUpperScroller->Invalidate();
} else { } else {
fMenu->ScrollBy(0, -kScrollStep); fMenu->ScrollBy(0, step);
fValue -= kScrollStep; fValue += step;
} }
} }
} }