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

New Widget: MenuBar #169

Merged
merged 13 commits into from
Aug 12, 2020
11 changes: 9 additions & 2 deletions SOURCECONF.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ SET( ${TARGETLIB}_SOURCES
YItem.cc
YIconLoader.cc
YMacro.cc
YMenuItem.cc
YProperty.cc
YShortcut.cc
YShortcutManager.cc
Expand Down Expand Up @@ -54,7 +55,9 @@ SET( ${TARGETLIB}_SOURCES
YLabel.cc
YLayoutBox.cc
YLogView.cc
YMenuBar.cc
YMenuButton.cc
YMenuWidget.cc
YMultiLineEdit.cc
YMultiProgressMeter.cc
YMultiSelectionBox.cc
Expand Down Expand Up @@ -107,10 +110,10 @@ SET( ${TARGETLIB}_HEADERS
YItem.h
YItemCustomStatus.h
YIconLoader.h
YMenuItem.h
YMacro.h
YMacroPlayer.h
YMacroRecorder.h
YMenuItem.h
YPackageSelectorPlugin.h
YGraphPlugin.h
YProperty.h
Expand Down Expand Up @@ -154,7 +157,9 @@ SET( ${TARGETLIB}_HEADERS
YLabel.h
YLayoutBox.h
YLogView.h
YMenuBar.h
YMenuButton.h
YMenuWidget.h
YMultiLineEdit.h
YMultiProgressMeter.h
YMultiSelectionBox.h
Expand Down Expand Up @@ -198,7 +203,9 @@ SET( EXAMPLES_LIST
ItemSelector1.cc
ItemSelector2-minimalistic.cc
ManyWidgets.cc
MenuButtons.cc
MenuBar1.cc
MenuBar2.cc
MenuButton1.cc
PollEvent.cc
SelectionBox1.cc
SelectionBox2.cc
Expand Down
248 changes: 248 additions & 0 deletions examples/MenuBar1.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
/*
Copyright (c) [2020] SUSE LLC
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) version 3.0 of the License. This library
is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details. You should have received a copy of the GNU
Lesser General Public License along with this library; if not, write
to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
Floor, Boston, MA 02110-1301 USA
*/


// MenuBar example.
//
// Compile with:
//
// g++ -I/usr/include/yui -lyui MenuBar1.cc -o MenuBar1

#define YUILogComponent "example"
#include "YUILog.h"

#include "YUI.h"
#include "YAlignment.h"
#include "YDialog.h"
#include "YLabel.h"
#include "YCheckBox.h"
#include "YLayoutBox.h"
#include "YMenuBar.h"
#include "YSquash.h"
#include "YWidgetFactory.h"
#include "YShortcut.h"
#include "YEvent.h"


using std::string;


// Widgets

YDialog * dialog = 0;
YMenuBar * menuBar = 0;
YLabel * lastEventLabel = 0;
YCheckBox * readOnlyCheckBox = 0;


// Menus and Menu Items

YMenuItem * fileMenu = 0;
YMenuItem * editMenu = 0;
YMenuItem * viewMenu = 0;
YMenuItem * optionsMenu = 0;


// "File" menu

YMenuItem * actionOpen = 0;
YMenuItem * actionSave = 0;
YMenuItem * actionSaveAs = 0;
YMenuItem * actionQuit = 0;


// "Edit" menu

YMenuItem * actionCut = 0;
YMenuItem * actionCopy = 0;
YMenuItem * actionPaste = 0;


// "View" menu

YMenuItem * actionViewNormal = 0;
YMenuItem * actionViewCompact = 0;
YMenuItem * actionViewDetailed = 0;


// "View" -> "Zoom" submenu

YMenuItem * zoomMenu = 0;
YMenuItem * actionZoomIn = 0;
YMenuItem * actionZoomOut = 0;
YMenuItem * actionZoomDefault = 0;

// "Options" menu

YMenuItem * actionSettings = 0;


// Function Prototypes

void createWidgets();
void addMenus( YMenuBar * menuBar );
void handleEvents();
void showEvent( YMenuEvent * event );
void updateActions();




int main( int argc, char **argv )
{
YUILog::setLogFileName( "/tmp/libyui-examples.log" );
YUILog::enableDebugLogging();

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

dialog->destroy();
}


/**
* Create the widget tree for the main dialog
**/
void createWidgets()
{
yuiMilestone() << endl;

YWidgetFactory * fac = YUI::widgetFactory();

dialog = fac->createPopupDialog();
YAlignment * minSize = fac->createMinSize( dialog, 50, 20 );
YLayoutBox * vbox1 = fac->createVBox( minSize );
menuBar = fac->createMenuBar( vbox1 );

addMenus( menuBar );

YAlignment * center = fac->createHVCenter( vbox1 );
YSquash * squash = fac->createHVSquash( center );
YLayoutBox * vbox2 = fac->createVBox( squash );

YAlignment * left = fac->createLeft( vbox2 );
fac->createLabel ( left, "Last Event:" );
fac->createVSpacing( vbox2, 0.2 );
YAlignment * minWidth = fac->createMinWidth( vbox2, 15 );
lastEventLabel = fac->createOutputField( minWidth, "<none>" );

fac->createVSpacing( vbox2, 2 );
readOnlyCheckBox = fac->createCheckBox( vbox2, "Read &Only", true );
readOnlyCheckBox->setNotify( true );
}


void addMenus( YMenuBar * menuBar )
{
// The difference between a menu and a plain menu item is just that the
// menu has child items whild the plain item does not.

fileMenu = menuBar->addMenu( "&File" );
editMenu = menuBar->addMenu( "&Edit" );
viewMenu = menuBar->addMenu( "&View" );
optionsMenu = menuBar->addMenu( "&Options" );

actionOpen = fileMenu->addItem( "&Open..." );
actionSave = fileMenu->addItem( "&Save" );
actionSaveAs = fileMenu->addItem( "Save &As..." );
fileMenu->addSeparator();
actionQuit = fileMenu->addItem( "&Quit" );

actionCut = editMenu->addItem( "C&ut" );
actionCopy = editMenu->addItem( "&Copy" );
actionPaste = editMenu->addItem( "&Paste" );

actionViewNormal = viewMenu->addItem( "&Normal" );
actionViewCompact = viewMenu->addItem( "&Compact" );
actionViewDetailed = viewMenu->addItem( "&Detailed" );
viewMenu->addSeparator();
zoomMenu = viewMenu->addMenu( "&Zoom" );

actionZoomIn = zoomMenu->addItem( "Zoom &In" );
actionZoomOut = zoomMenu->addItem( "Zoom &Out" );
actionZoomDefault = zoomMenu->addItem( "Zoom &Default" );

actionSettings = optionsMenu->addItem( "&Settings..." );

menuBar->resolveShortcutConflicts();
menuBar->rebuildMenuTree();
}


/**
* Event loop and event handling for the main dialog
**/
void handleEvents()
{
yuiMilestone() << endl;

while ( true )
{
YEvent * event = dialog->waitForEvent();

if ( event )
{
if ( event->eventType() == YEvent::CancelEvent ) // window manager "close window" button
{
break; // leave event loop
}

if ( event->eventType() == YEvent::MenuEvent )
{
YMenuEvent * menuEvent = dynamic_cast<YMenuEvent *>( event );

if ( menuEvent )
{
showEvent( menuEvent );

if ( menuEvent->item() == actionQuit )
break; // leave event loop
}
}

if ( event->widget() == readOnlyCheckBox )
updateActions();
}
}
}


/**
* Show the (label of) the item causing a menu event in the 'lastEventLabel'
* widget.
**/
void showEvent( YMenuEvent * event )
{
if ( event && event->item() )
{
string text = YShortcut::cleanShortcutString( event->item()->label() );
lastEventLabel->setLabel( text );
}
}


/**
* Update the available menu actions: Enable or disable some of them according
* to the current status of the "Read Only" check box.
**/
void updateActions()
{
bool readOnly = readOnlyCheckBox->isChecked();

menuBar->setItemEnabled( actionSave, ! readOnly );
menuBar->setItemEnabled( actionCut, ! readOnly );
menuBar->setItemEnabled( actionPaste, ! readOnly );
}