Skip to content

Instantly share code, notes, and snippets.

@anaselli
Created November 5, 2020 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anaselli/24de5fb4512a43d076f77e2d7327593d to your computer and use it in GitHub Desktop.
Save anaselli/24de5fb4512a43d076f77e2d7327593d to your computer and use it in GitHub Desktop.
Selection box example with icons
// Simple SelectionBox example.
//
// Compile with:
//
// g++ -I/usr/include/yui -lyui SelectionBox-icons.cc -o SelectionBox1-icons
#define YUILogComponent "example"
#include "YUILog.h"
#include "YUI.h"
#include "YWidgetFactory.h"
#include "YDialog.h"
#include "YLayoutBox.h"
#include "YSelectionBox.h"
#include "YLabel.h"
#include "YPushButton.h"
#include "YAlignment.h"
#include "YEvent.h"
int main( int argc, char **argv )
{
YUILog::setLogFileName( "/tmp/libyui-examples.log" );
YUILog::enableDebugLogging();
//
// Create and open dialog
//
YDialog * dialog = YUI::widgetFactory()->createPopupDialog();
YLayoutBox * vbox = YUI::widgetFactory()->createVBox( dialog );
YSelectionBox * selBox = YUI::widgetFactory()->createSelectionBox( vbox, "&Menu" );
YItemCollection items;
YItem *item = new YItem( "Theme icon document-open","document-open" );
items.push_back( item );
items.push_back( new YItem( "Full path icon document-share", "/usr/share/icons/hicolor/32x32/actions/document-share.png" ) );
selBox->addItems( items ); // This is more efficient than repeatedly calling selBox->addItem()
YLayoutBox * hbox = YUI::widgetFactory()->createHBox( vbox );
YLabel * valueField = YUI::widgetFactory()->createOutputField( hbox, "<SelectionBox value unknown>" );
valueField->setStretchable( YD_HORIZ, true ); // allow stretching over entire dialog width
YPushButton * valueButton = YUI::widgetFactory()->createPushButton( hbox, "&Value" );
YUI::widgetFactory()->createVSpacing( vbox, 0.3 );
YAlignment * rightAlignment = YUI::widgetFactory()->createRight( vbox );
YPushButton * closeButton = YUI::widgetFactory()->createPushButton( rightAlignment, "&Close" );
//
// Event loop
//
while ( true )
{
YEvent * event = dialog->waitForEvent();
if ( event )
{
if ( event->eventType() == YEvent::CancelEvent ) // window manager "close window" button
break; // leave event loop
valueField->setValue( "???" );
if ( event->widget() == closeButton )
break; // leave event loop
if ( event->widget() == valueButton ||
event->widget() == selBox ) // selBox will only send events with setNotify()
{
YItem * item = selBox->selectedItem();
if ( item )
valueField->setValue( item->label() );
else
valueField->setValue( "<none>" );
}
}
}
//
// Clean up
//
dialog->destroy();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment