Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to show or hide menu items #172

Merged
merged 2 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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