Skip to content

Commit

Permalink
Merge pull request #172 from joseivanlopez/visible_menu_item
Browse files Browse the repository at this point in the history
Allow to show or hide menu items
  • Loading branch information
joseivanlopez authored Oct 2, 2020
2 parents 9c617c0 + df4fc74 commit 5395a61
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
23 changes: 20 additions & 3 deletions examples/MenuBar1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ YDialog * dialog = 0;
YMenuBar * menuBar = 0;
YLabel * lastEventLabel = 0;
YCheckBox * readOnlyCheckBox = 0;
YCheckBox * visibleCheckBox = 0;
YPushButton * closeButton = 0;


Expand Down Expand Up @@ -115,7 +116,6 @@ int main( int argc, char **argv )
YUILog::enableDebugLogging();

createWidgets();
updateActions();
handleEvents();

dialog->destroy();
Expand Down Expand Up @@ -151,7 +151,12 @@ void createWidgets()
fac->createVSpacing( vbox2, 2 );
readOnlyCheckBox = fac->createCheckBox( vbox2, "Read &Only", true );
readOnlyCheckBox->setNotify( true );

fac->createVSpacing( vbox2, 0.2 );
visibleCheckBox = fac->createCheckBox( vbox2, "&Visible", true );
visibleCheckBox->setNotify( true );

fac->createVSpacing( vbox2, 1 );
closeButton = fac->createPushButton( vbox2, "&Close" );
}

Expand Down Expand Up @@ -203,6 +208,9 @@ void handleEvents()
{
yuiMilestone() << endl;

dialog->open();
updateActions();

while ( true )
{
YEvent * event = dialog->waitForEvent();
Expand Down Expand Up @@ -230,6 +238,9 @@ void handleEvents()
if ( event->widget() == readOnlyCheckBox )
updateActions();

if ( event->widget() == visibleCheckBox )
updateActions();

if ( event->widget() == closeButton )
break;
}
Expand All @@ -252,8 +263,9 @@ void showEvent( YMenuEvent * event )


/**
* Update the available menu actions: Enable or disable some of them according
* to the current status of the "Read Only" check box.
* Update the available menu actions:
* * Enable or disable some of them according to the current status of the "Read Only" check box.
* * Show or hide some of them according to the current status of the "Visible" check box.
**/
void updateActions()
{
Expand All @@ -262,4 +274,9 @@ void updateActions()
menuBar->setItemEnabled( actionSave, ! readOnly );
menuBar->setItemEnabled( actionCut, ! readOnly );
menuBar->setItemEnabled( actionPaste, ! readOnly );

bool visible = visibleCheckBox->isChecked();

menuBar->setItemVisible( contentsMenu, visible );
menuBar->setItemVisible( actionOpen, visible );
}
19 changes: 18 additions & 1 deletion src/YMenuItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class YMenuItem: public YTreeItem
const std::string & iconName = "" )
: YTreeItem( label, iconName )
, _enabled( true )
, _visible( true )
, _uiItem( 0 )
{}

Expand All @@ -67,6 +68,7 @@ class YMenuItem: public YTreeItem
const std::string & iconName = "" )
: YTreeItem( parent, label, iconName )
, _enabled( true )
, _visible( true )
, _uiItem( 0 )
{}

Expand Down Expand Up @@ -131,6 +133,20 @@ class YMenuItem: public YTreeItem
**/
void setEnabled( bool enabled = true ) { _enabled = enabled; }

/**
* Return 'true' if this item is visible (which is the default).
**/
bool isVisible() const { return _visible; }

/**
* Show or hide this item.
*
* Applications should use YMenuWidget::setItemVisible() instead because
* that will also notify the widget so it can update the item's visual
* representation.
**/
void setVisible( bool visible = true ) { _visible = visible; }

/**
* Return the UI counterpart of this item (if the UI set any).
**/
Expand All @@ -154,7 +170,8 @@ class YMenuItem: public YTreeItem

// Data members

bool _enabled;
bool _enabled;
bool _visible;
void * _uiItem;
};

Expand Down
8 changes: 8 additions & 0 deletions src/YMenuWidget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ YMenuWidget::setItemEnabled( YMenuItem * item, bool enabled )
}


void
YMenuWidget::setItemVisible( YMenuItem * item, bool visible )
{
if ( item )
item->setVisible( visible );
}


YMenuItem *
YMenuWidget::findMenuItem( int index )
{
Expand Down
10 changes: 10 additions & 0 deletions src/YMenuWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ class YMenuWidget: public YSelectionWidget
**/
virtual void setItemEnabled( YMenuItem * item, bool enabled );

/**
* Show or hide an item. This default implementation only updates the
* item's 'visible' field.
*
* Derived classes should overwrite this method and either update the
* item's 'visible' field in their implementation or call this default
* implementation.
**/
virtual void setItemVisible( YMenuItem * item, bool visible );

/**
* Support for the Rest API for UI testing:
*
Expand Down

0 comments on commit 5395a61

Please sign in to comment.