Menu and friends: Style fixes, no functional

... changes intended.

* 80 char limit fixes
* Indentation fixes
* Braces style fixes
* Use ternary operator where appropriate
* Rename menuItem to just item and declare it once outside
  the loop
* Omit 3rd param of GetMouse() because it is default
* Rename variables eg state => focused and menu => submenu
* Indent comments below line they apply to
* Reword some comments
* Add some #pragmas
This commit is contained in:
John Scipione
2013-08-25 00:05:02 -04:00
parent f5bb831108
commit cccc4076db
5 changed files with 112 additions and 89 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ public:
virtual void AllAttached(); virtual void AllAttached();
virtual void MouseDown(BPoint where); virtual void MouseDown(BPoint where);
virtual void KeyDown(const char* bytes, int32 numBytes); virtual void KeyDown(const char* bytes, int32 numBytes);
virtual void MakeFocus(bool state); virtual void MakeFocus(bool focused);
virtual void MessageReceived(BMessage* message); virtual void MessageReceived(BMessage* message);
virtual void WindowActivated(bool state); virtual void WindowActivated(bool state);
virtual void MouseUp(BPoint where); virtual void MouseUp(BPoint where);
+13 -4
View File
@@ -22,6 +22,12 @@
#include <Window.h> #include <Window.h>
static const float kPopUpIndicatorWidth = 13.0f;
// #pragma mark - _BMCFilter_
_BMCFilter_::_BMCFilter_(BMenuField* menuField, uint32 what) _BMCFilter_::_BMCFilter_(BMenuField* menuField, uint32 what)
: :
BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE, what), BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE, what),
@@ -52,10 +58,7 @@ _BMCFilter_::Filter(BMessage* message, BHandler** handler)
} }
// #pragma mark - // #pragma mark - _BMCMenuBar_
static const float kPopUpIndicatorWidth = 13.0f;
_BMCMenuBar_::_BMCMenuBar_(BRect frame, bool fixedSize, BMenuField* menuField) _BMCMenuBar_::_BMCMenuBar_(BRect frame, bool fixedSize, BMenuField* menuField)
@@ -105,6 +108,9 @@ _BMCMenuBar_::~_BMCMenuBar_()
} }
// #pragma mark - _BMCMenuBar_ public methods
BArchivable* BArchivable*
_BMCMenuBar_::Instantiate(BMessage* data) _BMCMenuBar_::Instantiate(BMessage* data)
{ {
@@ -311,6 +317,9 @@ _BMCMenuBar_::MaxSize()
} }
// #pragma mark - _BMCMenuBar_ private methods
void void
_BMCMenuBar_::_Init() _BMCMenuBar_::_Init()
{ {
+40 -38
View File
@@ -476,7 +476,7 @@ BMenu::MessageReceived(BMessage* msg)
void void
BMenu::KeyDown(const char* bytes, int32 numBytes) BMenu::KeyDown(const char* bytes, int32 numBytes)
{ {
// TODO: Test how it works on beos and implement it correctly // TODO: Test how it works on BeOS R5 and implement this correctly
switch (bytes[0]) { switch (bytes[0]) {
case B_UP_ARROW: case B_UP_ARROW:
if (fLayout == B_ITEMS_IN_COLUMN) if (fLayout == B_ITEMS_IN_COLUMN)
@@ -484,16 +484,16 @@ BMenu::KeyDown(const char* bytes, int32 numBytes)
break; break;
case B_DOWN_ARROW: case B_DOWN_ARROW:
{ {
BMenuBar* bar = dynamic_cast<BMenuBar*>(Supermenu()); BMenuBar* bar = dynamic_cast<BMenuBar*>(Supermenu());
if (bar != NULL && fState == MENU_STATE_CLOSED) { if (bar != NULL && fState == MENU_STATE_CLOSED) {
// tell MenuBar's _Track: // tell MenuBar's _Track:
bar->fState = MENU_STATE_KEY_TO_SUBMENU; bar->fState = MENU_STATE_KEY_TO_SUBMENU;
}
} }
if (fLayout == B_ITEMS_IN_COLUMN) if (fLayout == B_ITEMS_IN_COLUMN)
_SelectNextItem(fSelected, true); _SelectNextItem(fSelected, true);
break; break;
}
case B_LEFT_ARROW: case B_LEFT_ARROW:
if (fLayout == B_ITEMS_IN_ROW) if (fLayout == B_ITEMS_IN_ROW)
@@ -520,7 +520,7 @@ BMenu::KeyDown(const char* bytes, int32 numBytes)
if (fLayout == B_ITEMS_IN_ROW) if (fLayout == B_ITEMS_IN_ROW)
_SelectNextItem(fSelected, true); _SelectNextItem(fSelected, true);
else { else {
if (fSelected && fSelected->Submenu()) { if (fSelected != NULL && fSelected->Submenu() != NULL) {
fSelected->Submenu()->_SetStickyMode(true); fSelected->Submenu()->_SetStickyMode(true);
// fix me: this shouldn't be needed but dynamic menus // fix me: this shouldn't be needed but dynamic menus
// aren't getting it set correctly when keyboard // aren't getting it set correctly when keyboard
@@ -555,19 +555,20 @@ BMenu::KeyDown(const char* bytes, int32 numBytes)
case B_ENTER: case B_ENTER:
case B_SPACE: case B_SPACE:
if (fSelected) { if (fSelected != NULL) {
// preserve for exit handling
fChosenItem = fSelected; fChosenItem = fSelected;
// preserve for exit handling
_QuitTracking(false); _QuitTracking(false);
} }
break; break;
case B_ESCAPE: case B_ESCAPE:
_SelectItem(NULL); _SelectItem(NULL);
if (fState == MENU_STATE_CLOSED && dynamic_cast<BMenuBar*>(Supermenu())) { if (fState == MENU_STATE_CLOSED
&& dynamic_cast<BMenuBar*>(Supermenu())) {
// Keyboard may show menu without tracking it // Keyboard may show menu without tracking it
BMessenger msgr(Supermenu()); BMessenger messenger(Supermenu());
msgr.SendMessage(Window()->CurrentMessage()); messenger.SendMessage(Window()->CurrentMessage());
} else } else
_QuitTracking(false); _QuitTracking(false);
break; break;
@@ -1585,11 +1586,10 @@ BMenu::_Hide()
else else
#endif #endif
window->Quit(); window->Quit();
// it's our window, quit it // it's our window, quit it
// Delete the menu window used by our submenus
_DeleteMenuWindow(); _DeleteMenuWindow();
// Delete the menu window used by our submenus
} }
@@ -1611,8 +1611,8 @@ BMenu::_Track(int* action, long start)
bigtime_t navigationAreaTime = 0; bigtime_t navigationAreaTime = 0;
fState = MENU_STATE_TRACKING; fState = MENU_STATE_TRACKING;
// we will use this for keyboard selection:
fChosenItem = NULL; fChosenItem = NULL;
// we will use this for keyboard selection
BPoint location; BPoint location;
uint32 buttons = 0; uint32 buttons = 0;
@@ -1642,7 +1642,7 @@ BMenu::_Track(int* action, long start)
// then if the mouse is inside this menu, // then if the mouse is inside this menu,
// then if it's over a super menu. // then if it's over a super menu.
if (_OverSubmenu(fSelected, screenLocation) if (_OverSubmenu(fSelected, screenLocation)
|| fState == MENU_STATE_KEY_TO_SUBMENU) { || fState == MENU_STATE_KEY_TO_SUBMENU) {
if (fState == MENU_STATE_TRACKING) { if (fState == MENU_STATE_TRACKING) {
// not if from R.Arrow // not if from R.Arrow
fState = MENU_STATE_TRACKING_SUBMENU; fState = MENU_STATE_TRACKING_SUBMENU;
@@ -1669,7 +1669,7 @@ BMenu::_Track(int* action, long start)
fState = MENU_STATE_CLOSED; fState = MENU_STATE_CLOSED;
} else if (submenuAction == MENU_STATE_KEY_LEAVE_SUBMENU) { } else if (submenuAction == MENU_STATE_KEY_LEAVE_SUBMENU) {
if (LockLooper()) { if (LockLooper()) {
BMenuItem *temp = fSelected; BMenuItem* temp = fSelected;
// close the submenu: // close the submenu:
_SelectItem(NULL); _SelectItem(NULL);
// but reselect the item itself for user: // but reselect the item itself for user:
@@ -1686,14 +1686,16 @@ BMenu::_Track(int* action, long start)
_UpdateStateOpenSelect(item, location, navAreaRectAbove, _UpdateStateOpenSelect(item, location, navAreaRectAbove,
navAreaRectBelow, selectedTime, navigationAreaTime); navAreaRectBelow, selectedTime, navigationAreaTime);
releasedOnce = true; releasedOnce = true;
} else if (_OverSuper(screenLocation) && fSuper->fState != MENU_STATE_KEY_TO_SUBMENU) { } else if (_OverSuper(screenLocation)
&& fSuper->fState != MENU_STATE_KEY_TO_SUBMENU) {
fState = MENU_STATE_TRACKING; fState = MENU_STATE_TRACKING;
UnlockLooper(); UnlockLooper();
break; break;
} else if (fState == MENU_STATE_KEY_LEAVE_SUBMENU) { } else if (fState == MENU_STATE_KEY_LEAVE_SUBMENU) {
UnlockLooper(); UnlockLooper();
break; break;
} else if (fSuper == NULL || fSuper->fState != MENU_STATE_KEY_TO_SUBMENU) { } else if (fSuper == NULL
|| fSuper->fState != MENU_STATE_KEY_TO_SUBMENU) {
// Mouse pointer outside menu: // Mouse pointer outside menu:
// If there's no other submenu opened, // If there's no other submenu opened,
// deselect the current selected item // deselect the current selected item
@@ -1733,7 +1735,7 @@ BMenu::_Track(int* action, long start)
UnlockLooper(); UnlockLooper();
} while (newLocation == location && newButtons == buttons } while (newLocation == location && newButtons == buttons
&& !(item != NULL && item->Submenu() != NULL && !(item != NULL && item->Submenu() != NULL
&& item->Submenu()->Window() == NULL) && item->Submenu()->Window() == NULL)
&& fState == MENU_STATE_TRACKING); && fState == MENU_STATE_TRACKING);
if (newLocation != location || newButtons != buttons) { if (newLocation != location || newButtons != buttons) {
@@ -1754,9 +1756,10 @@ BMenu::_Track(int* action, long start)
// keyboard Enter will set this // keyboard Enter will set this
if (fChosenItem != NULL) if (fChosenItem != NULL)
item = fChosenItem; item = fChosenItem;
else if (fSelected == NULL) else if (fSelected == NULL) {
// needed to cover (rare) mouse/ESC combination // needed to cover (rare) mouse/ESC combination
item = NULL; item = NULL;
}
if (fSelected != NULL && LockLooper()) { if (fSelected != NULL && LockLooper()) {
_SelectItem(NULL); _SelectItem(NULL);
@@ -2591,12 +2594,12 @@ BMenu::_Uninstall()
void void
BMenu::_SelectItem(BMenuItem* menuItem, bool showSubmenu, BMenu::_SelectItem(BMenuItem* item, bool showSubmenu, bool selectFirstItem,
bool selectFirstItem, bool keyDown) bool keyDown)
{ {
// Avoid deselecting and then reselecting the same item // Avoid deselecting and then reselecting the same item
// which would cause flickering // which would cause flickering
if (menuItem != fSelected) { if (item != fSelected) {
if (fSelected != NULL) { if (fSelected != NULL) {
fSelected->Select(false); fSelected->Select(false);
BMenu* subMenu = fSelected->Submenu(); BMenu* subMenu = fSelected->Submenu();
@@ -2604,7 +2607,7 @@ BMenu::_SelectItem(BMenuItem* menuItem, bool showSubmenu,
subMenu->_Hide(); subMenu->_Hide();
} }
fSelected = menuItem; fSelected = item;
if (fSelected != NULL) if (fSelected != NULL)
fSelected->Select(true); fSelected->Select(true);
} }
@@ -2632,10 +2635,8 @@ BMenu::_SelectNextItem(BMenuItem* item, bool forward)
if (nextItem == NULL) if (nextItem == NULL)
return false; return false;
bool openMenu = false; _SelectItem(nextItem, dynamic_cast<BMenuBar*>(this) != NULL);
if (dynamic_cast<BMenuBar*>(this) != NULL)
openMenu = true;
_SelectItem(nextItem, openMenu);
return true; return true;
} }
@@ -2688,15 +2689,15 @@ BMenu::_SetStickyMode(bool on)
fStickyMode = on; fStickyMode = on;
// If we are switching to sticky mode, propagate the status if (fSuper != NULL) {
// back to the super menu // propagate the status to the super menu
if (fSuper != NULL)
fSuper->_SetStickyMode(on); fSuper->_SetStickyMode(on);
else { } else {
// TODO: Ugly hack, but it needs to be done right here in this method // TODO: Ugly hack, but it needs to be done in this method
BMenuBar* menuBar = dynamic_cast<BMenuBar*>(this); BMenuBar* menuBar = dynamic_cast<BMenuBar*>(this);
if (on && menuBar != NULL && menuBar->LockLooper()) { if (on && menuBar != NULL && menuBar->LockLooper()) {
// Steal the focus from the current focus view // If we are switching to sticky mode,
// steal the focus from the current focus view
// (needed to handle keyboard navigation) // (needed to handle keyboard navigation)
menuBar->_StealFocus(); menuBar->_StealFocus();
menuBar->UnlockLooper(); menuBar->UnlockLooper();
@@ -2946,8 +2947,9 @@ BMenu::_OkToProceed(BMenuItem* item, bool keyDown)
if ((buttons != 0 && stickyMode) if ((buttons != 0 && stickyMode)
|| ((dynamic_cast<BMenuBar*>(this) == NULL || ((dynamic_cast<BMenuBar*>(this) == NULL
&& (buttons == 0 && !stickyMode)) && (buttons == 0 && !stickyMode))
|| ((_HitTestItems(where) != item) && !keyDown))) || ((_HitTestItems(where) != item) && !keyDown))) {
return false; return false;
}
return true; return true;
} }
+40 -37
View File
@@ -528,8 +528,10 @@ BMenuItem*
BMenuBar::_Track(int32* action, int32 startIndex, bool showMenu) BMenuBar::_Track(int32* action, int32 startIndex, bool showMenu)
{ {
// TODO: Cleanup, merge some "if" blocks if possible // TODO: Cleanup, merge some "if" blocks if possible
fChosenItem = NULL; BMenuItem* item = NULL;
fState = MENU_STATE_TRACKING; fState = MENU_STATE_TRACKING;
fChosenItem = NULL;
// we will use this for keyboard selection
BPoint where; BPoint where;
uint32 buttons; uint32 buttons;
@@ -547,22 +549,19 @@ BMenuBar::_Track(int32* action, int32 startIndex, bool showMenu)
if (!LockLooper()) if (!LockLooper())
break; break;
BMenuItem* menuItem = NULL; item = dynamic_cast<_BMCMenuBar_*>(this) != NULL ? ItemAt(0)
if (dynamic_cast<_BMCMenuBar_*>(this)) : _HitTestItems(where, B_ORIGIN);
menuItem = ItemAt(0);
else
menuItem = _HitTestItems(where, B_ORIGIN);
if (_OverSubmenu(fSelected, ConvertToScreen(where)) if (_OverSubmenu(fSelected, ConvertToScreen(where))
|| fState == MENU_STATE_KEY_TO_SUBMENU) { || fState == MENU_STATE_KEY_TO_SUBMENU) {
// call _Track() from the selected sub-menu when the mouse cursor // call _Track() from the selected sub-menu when the mouse cursor
// is over its window // is over its window
BMenu* menu = fSelected->Submenu(); BMenu* submenu = fSelected->Submenu();
UnlockLooper(); UnlockLooper();
snoozeAmount = 30000; snoozeAmount = 30000;
bool wasSticky = _IsStickyMode(); submenu->_SetStickyMode(_IsStickyMode());
menu->_SetStickyMode(wasSticky);
int localAction; int localAction;
fChosenItem = menu->_Track(&localAction); fChosenItem = submenu->_Track(&localAction);
// The mouse could have meen moved since the last time we // The mouse could have meen moved since the last time we
// checked its position, or buttons might have been pressed. // checked its position, or buttons might have been pressed.
@@ -577,13 +576,13 @@ BMenuBar::_Track(int32* action, int32 startIndex, bool showMenu)
UnlockLooper(); UnlockLooper();
} }
// This code is needed to make menus // Needed to make BMenuField child menus "sticky"
// that are children of BMenuFields "sticky" (see ticket #953) // (see ticket #953)
if (localAction == MENU_STATE_CLOSED) { if (localAction == MENU_STATE_CLOSED) {
if (fExtraRect != NULL && fExtraRect->Contains(where) if (fExtraRect != NULL && fExtraRect->Contains(where)
&& point_distance(newWhere, where) < 9) {
// 9 = 3 pixels ^ 2 (since point_distance() returns the // 9 = 3 pixels ^ 2 (since point_distance() returns the
// square of the distance) // square of the distance)
&& point_distance(newWhere, where) < 9) {
_SetStickyMode(true); _SetStickyMode(true);
fExtraRect = NULL; fExtraRect = NULL;
} else } else
@@ -591,11 +590,11 @@ BMenuBar::_Track(int32* action, int32 startIndex, bool showMenu)
} }
if (!LockLooper()) if (!LockLooper())
break; break;
} else if (menuItem != NULL) { } else if (item != NULL) {
if (menuItem->Submenu() != NULL && menuItem != fSelected) { if (item->Submenu() != NULL && item != fSelected) {
if (menuItem->Submenu()->Window() == NULL) { if (item->Submenu()->Window() == NULL) {
// open the menu if it's not opened yet // open the menu if it's not opened yet
_SelectItem(menuItem); _SelectItem(item);
} else { } else {
// Menu was already opened, close it and bail // Menu was already opened, close it and bail
_SelectItem(NULL); _SelectItem(NULL);
@@ -604,9 +603,9 @@ BMenuBar::_Track(int32* action, int32 startIndex, bool showMenu)
} }
} else { } else {
// No submenu, just select the item // No submenu, just select the item
_SelectItem(menuItem); _SelectItem(item);
} }
} else if (menuItem == NULL && fSelected != NULL } else if (item == NULL && fSelected != NULL
&& !_IsStickyMode() && Bounds().Contains(where)) { && !_IsStickyMode() && Bounds().Contains(where)) {
_SelectItem(NULL); _SelectItem(NULL);
fState = MENU_STATE_TRACKING; fState = MENU_STATE_TRACKING;
@@ -615,38 +614,42 @@ BMenuBar::_Track(int32* action, int32 startIndex, bool showMenu)
UnlockLooper(); UnlockLooper();
if (fState != MENU_STATE_CLOSED) { if (fState != MENU_STATE_CLOSED) {
// If user doesn't move the mouse, loop here, BPoint newWhere = where;
// so we don't interfere with keyboard menu navigation
BPoint newLocation = where;
uint32 newButtons = buttons; uint32 newButtons = buttons;
do { do {
// If user doesn't move the mouse or change buttons loop
// here so that we don't interfere with keyboard menu
// navigation
snooze(snoozeAmount); snooze(snoozeAmount);
if (!LockLooper()) if (!LockLooper())
break; break;
GetMouse(&newLocation, &newButtons, true);
GetMouse(&newWhere, &newButtons);
UnlockLooper(); UnlockLooper();
} while (newLocation == where && newButtons == buttons } while (newWhere == where && newButtons == buttons
&& fState == MENU_STATE_TRACKING); && fState == MENU_STATE_TRACKING);
where = newLocation; if (newButtons != 0 && _IsStickyMode()) {
buttons = newButtons; if (item == NULL || (item->Submenu() != NULL
&& item->Submenu()->Window() != NULL)) {
if (buttons != 0 && _IsStickyMode()) { // clicked outside the menu bar or on item with already
if (menuItem == NULL
|| (menuItem->Submenu() && menuItem->Submenu()->Window())) {
// clicked outside menu bar or on item with already
// open sub menu // open sub menu
fState = MENU_STATE_CLOSED; fState = MENU_STATE_CLOSED;
} else } else
_SetStickyMode(false); _SetStickyMode(false);
} else if (buttons == 0 && !_IsStickyMode()) { } else if (newButtons == 0 && !_IsStickyMode()) {
if ((fSelected != NULL && fSelected->Submenu() == NULL) if ((fSelected != NULL && fSelected->Submenu() == NULL)
|| menuItem == NULL) { || item == NULL) {
// clicked on an item without a submenu or clicked and
// released the mouse button outside the menu bar
fChosenItem = fSelected; fChosenItem = fSelected;
fState = MENU_STATE_CLOSED; fState = MENU_STATE_CLOSED;
} else } else
_SetStickyMode(true); _SetStickyMode(true);
} }
where = newWhere;
buttons = newButtons;
} }
} }
@@ -656,6 +659,7 @@ BMenuBar::_Track(int32* action, int32 startIndex, bool showMenu)
if (fChosenItem != NULL) if (fChosenItem != NULL)
fChosenItem->Invoke(); fChosenItem->Invoke();
_RestoreFocus(); _RestoreFocus();
UnlockLooper(); UnlockLooper();
} }
@@ -681,9 +685,9 @@ BMenuBar::_StealFocus()
BWindow* window = Window(); BWindow* window = Window();
if (window != NULL && window->Lock()) { if (window != NULL && window->Lock()) {
BView* focus = window->CurrentFocus(); BView* focusView = window->CurrentFocus();
if (focus != NULL && focus != this) if (focusView != NULL && focusView != this)
fPrevFocusToken = _get_object_token_(focus); fPrevFocusToken = _get_object_token_(focusView);
MakeFocus(); MakeFocus();
window->Unlock(); window->Unlock();
} }
@@ -702,7 +706,6 @@ BMenuBar::_RestoreFocus()
BView* view = dynamic_cast<BView*>(handler); BView* view = dynamic_cast<BView*>(handler);
if (view != NULL && view->Window() == window) if (view != NULL && view->Window() == window)
view->MakeFocus(); view->MakeFocus();
} else if (IsFocus()) } else if (IsFocus())
MakeFocus(false); MakeFocus(false);
+18 -9
View File
@@ -47,6 +47,10 @@
#endif #endif
static const float kMinMenuBarWidth = 20.0f;
// found by experimenting on BeOS R5
namespace { namespace {
const char* const kFrameField = "BMenuField:layoutItem:frame"; const char* const kFrameField = "BMenuField:layoutItem:frame";
const char* const kMenuBarItemField = "BMenuField:barItem"; const char* const kMenuBarItemField = "BMenuField:barItem";
@@ -54,6 +58,9 @@ namespace {
} }
// #pragma mark - LabelLayoutItem
class BMenuField::LabelLayoutItem : public BAbstractLayoutItem { class BMenuField::LabelLayoutItem : public BAbstractLayoutItem {
public: public:
LabelLayoutItem(BMenuField* parent); LabelLayoutItem(BMenuField* parent);
@@ -82,6 +89,9 @@ private:
}; };
// #pragma mark - MenuBarLayoutItem
class BMenuField::MenuBarLayoutItem : public BAbstractLayoutItem { class BMenuField::MenuBarLayoutItem : public BAbstractLayoutItem {
public: public:
MenuBarLayoutItem(BMenuField* parent); MenuBarLayoutItem(BMenuField* parent);
@@ -110,6 +120,9 @@ private:
}; };
// #pragma mark - LayoutData
struct BMenuField::LayoutData { struct BMenuField::LayoutData {
LayoutData() LayoutData()
: :
@@ -133,11 +146,7 @@ struct BMenuField::LayoutData {
}; };
// #pragma mark - // #pragma mark - BMenuField
static const float kMinMenuBarWidth = 20.0f;
// found by experimenting on BeOS R5
using BPrivate::MenuPrivate; using BPrivate::MenuPrivate;
@@ -463,14 +472,14 @@ BMenuField::KeyDown(const char* bytes, int32 numBytes)
void void
BMenuField::MakeFocus(bool state) BMenuField::MakeFocus(bool focused)
{ {
if (IsFocus() == state) if (IsFocus() == focused)
return; return;
BView::MakeFocus(state); BView::MakeFocus(focused);
if (Window()) if (Window() != NULL)
Invalidate(); // TODO: use fLayoutData->label_width Invalidate(); // TODO: use fLayoutData->label_width
} }