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 (UI Interpreter Part) #53

Merged
merged 8 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
156 changes: 156 additions & 0 deletions examples/MenuBar1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# encoding: utf-8

module Yast
class MenuBar1Client < Client
Yast.import "UI"
include Yast::Logger

def main
UI.OpenDialog(dialog_widgets)
update_actions
handle_events
UI.CloseDialog
nil
end

def dialog_widgets
MinSize( 50, 20,
VBox(
term(:MenuBar, Id(:menu_bar), main_menus),
HVCenter(
HVSquash(
VBox(
Left(Label("Last Event:")),
VSpacing( 0.2 ),
MinWidth( 20,
Label(Id(:last_event), Opt(:outputField), "<none>")
),
VSpacing( 2 ),
CheckBox(Id(:read_only), Opt(:notify), "Read &Only", true)
)
)
),
Right(PushButton(Id(:cancel), "&Quit"))
)
)
end

def main_menus
[
term(:menu, "&File", file_menu),
term(:menu, "&Edit", edit_menu),
term(:menu, "&View", view_menu),
term(:menu, "&Options", options_menu),
term(:menu, "&Debug", debug_menu)
].freeze
end

def file_menu
[
Item(Id(:open), "&Open..."),
Item(Id(:save), "&Save"),
Item(Id(:save_as), "Save &As..."),
Item("---"),
Item(Id(:quit), "&Quit"),
].freeze
end

def edit_menu
[
Item(Id(:cut), "C&ut"),
Item(Id(:copy), "&Copy"),
Item(Id(:paste), "&Paste"),
].freeze
end

def view_menu
[
Item(Id(:view_normal), "&Normal"),
Item(Id(:view_compact), "&Compact"),
Item(Id(:view_detailed), "&Detailed"),
Item("---"),
term(:menu, "&Zoom", zoom_menu),
].freeze
end

def zoom_menu
[
Item(Id(:zoom_in), "Zoom &In" ),
Item(Id(:zoom_out), "Zoom &Out" ),
Item(Id(:zoom_default), "Zoom &Default" ),
].freeze
end

def options_menu
[
Item(Id(:settings), "&Settings..."),
].freeze
end

def debug_menu
[
Item(Id(:dump_status), "Dump enabled / disabled &status"),
Item(Id(:dump_disabled), "Dump &disabled items")
].freeze
end

def handle_events
while true
id = UI.UserInput

case id
when :quit, :cancel # :cancel is WM_CLOSE
break # leave event loop
when :read_only
update_actions
when :dump_status
dump_item_status
when :dump_disabled
dump_disabled_ids
end

show_event(id)
end
id
end

def show_event(id)
UI.ChangeWidget(:last_event, :Value, id.to_s)
end

# Enable or disable menu items depending on the current content of the
# "Read Only" check box.
def update_actions
read_only = UI.QueryWidget(:read_only, :Value)

# Enable or disable individual menu entries (actions as well as submenus):
#
# Specify a hash of item IDs with a boolean indicating if it should be
# enabled (true) or disabled (false). Menu items that are not in this hash will
# not be touched, i.e. they retain their current enabled / disabled status.

UI.ChangeWidget(:menu_bar, :EnabledItems,
{
:save => !read_only,
:cut => !read_only,
:paste => !read_only
}
)
end

# Dump the enabled / disabled states of the menu items to the log.
def dump_item_status
states = UI.QueryWidget(:menu_bar, :EnabledItems)
log.info("Enabled/disabled: #{states}")
end

# Dump the IDs of all disabled menu items to the log.
def dump_disabled_ids
states = UI.QueryWidget(:menu_bar, :EnabledItems)
states.reject! { |k, v| v }
log.info("Disabled: #{states.keys}")
end
end
end

Yast::MenuBar1Client.new.main
2 changes: 1 addition & 1 deletion src/YCPBuiltinCaller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ you may find current contact information at www.novell.com

File: YCPBuiltinCaller.cc

Author: Stefan Hundhammer <sh@suse.de>
Author: Stefan Hundhammer <shundhammer@suse.de>

/-*/

Expand Down
2 changes: 1 addition & 1 deletion src/YCPBuiltinCaller.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ you may find current contact information at www.novell.com

File: YCPBuiltinCaller.h

Author: Stefan Hundhammer <sh@suse.de>
Author: Stefan Hundhammer <shundhammer@suse.de>

/-*/

Expand Down
59 changes: 56 additions & 3 deletions src/YCPDialogParser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ you may find current contact information at www.novell.com
#include <yui/YLabel.h>
#include <yui/YLayoutBox.h>
#include <yui/YLogView.h>
#include <yui/YMenuBar.h>
#include <yui/YMenuButton.h>
#include <yui/YMultiLineEdit.h>
#include <yui/YMultiProgressMeter.h>
Expand Down Expand Up @@ -287,6 +288,7 @@ YCPDialogParser::parseWidgetTreeTerm( YWidget * p,
else if ( s == YUIWidget_Left ) w = parseAlignment ( p, opt, term, ol, n, YAlignBegin, YAlignUnchanged );
else if ( s == YUIWidget_LogView ) w = parseLogView ( p, opt, term, ol, n );
else if ( s == YUIWidget_MarginBox ) w = parseMarginBox ( p, opt, term, ol, n );
else if ( s == YUIWidget_MenuBar ) w = parseMenuBar ( p, opt, term, ol, n );
else if ( s == YUIWidget_MenuButton ) w = parseMenuButton ( p, opt, term, ol, n );
else if ( s == YUIWidget_MinHeight ) w = parseMinSize ( p, opt, term, ol, n, false, true );
else if ( s == YUIWidget_MinSize ) w = parseMinSize ( p, opt, term, ol, n, true, true );
Expand Down Expand Up @@ -1511,6 +1513,57 @@ YCPDialogParser::parsePushButton( YWidget * parent, YWidgetOpt & opt,
}


/**
* @widget MenuBar
* @short Classic menu bar with pull-down menus
* @class YMenuBar
* @optarg itemList menu items
* @example MenuBar1.rb
*
*
* This is a classical menu bar. Use this with caution: YaST typically uses a
* wizard-driven approach where a menu bar is often very much out of place;
* it's a different UI philosophy.
*
* A menu bar is required to have only menus (not plain menu items) at the top
* level. Menus can have submenus or separators. A separator is a menu item
* with an empty label or a label that starts with "---".
*
* Invididual menu entries (both actions and submenus) can be enabled or
* disabled with the EnabledItems property: It receives a hash of item ID keys
* with a boolean value each that specifies if that item should be enabled
* (true) or disabled (false). Items that are not in that hash are not touched,
* i.e. they keep their current enabled or disabled status.
*
* See the MenuBar1.rb example how to do this.
**/

YWidget *
YCPDialogParser::parseMenuBar( YWidget * parent, YWidgetOpt & opt,
const YCPTerm & term, const YCPList & optList, int argnr )
{
int numArgs = term->size() - argnr;

if ( numArgs > 1 ||
( numArgs == 2 && ! term->value( argnr+1 )->isList() ) )
{
THROW_BAD_ARGS( term );
}

rejectAllOptions( term, optList );

YMenuBar * menuBar = YUI::widgetFactory()->createMenuBar( parent );

if ( numArgs == 1 )
{
YCPList itemList = term->value( argnr )->asList();
menuBar->addItems( YCPMenuItemParser::parseMenuItemList( itemList ) );
}

return menuBar;
}


/**
* @widget MenuButton
* @short Button with popup menu
Expand Down Expand Up @@ -2205,7 +2258,7 @@ YItemCustomStatusVector
YCPDialogParser::parseCustomStates( const YCPList & statesList )
{
const char * usage = "Expected: [ [\"iconName1\", \"textIndicator1\", int nextStatus], [...], ...]";

YItemCustomStatusVector customStates;

for ( int i=0; i < statesList.size(); i++ )
Expand All @@ -2214,7 +2267,7 @@ YCPDialogParser::parseCustomStates( const YCPList & statesList )

if ( ! val->isList() )
YUI_THROW( YCPDialogSyntaxErrorException( usage, statesList ) );

YCPList stat = val->asList();

if ( stat->size() < 2 || stat->size() > 3 ||
Expand All @@ -2237,7 +2290,7 @@ YCPDialogParser::parseCustomStates( const YCPList & statesList )

if ( customStates.size() < 2 )
YUI_THROW( YCPDialogSyntaxErrorException( "Need at least 2 custom status values", statesList ) );

return customStates;
}

Expand Down
7 changes: 5 additions & 2 deletions src/YCPDialogParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ you may find current contact information at www.novell.com

File: YCPDialogParser.h

Author: Stefan Hundhammer <sh@suse.de>
Author: Stefan Hundhammer <shundhammer@suse.de>

/-*/

Expand Down Expand Up @@ -197,7 +197,7 @@ class YCPDialogParser

static YWidget * parseMenuButton( YWidget *parent, YWidgetOpt & opt,
const YCPTerm & term, const YCPList & optList, int argnr );

static YWidget * parseCheckBox( YWidget *parent, YWidgetOpt & opt,
const YCPTerm & term, const YCPList & optList, int argnr );

Expand Down Expand Up @@ -233,6 +233,9 @@ class YCPDialogParser
static YWidget * parseCustomStatusItemSelector( YWidget *parent, YWidgetOpt & opt,
const YCPTerm & term, const YCPList & optList, int argnr );

static YWidget * parseMenuBar( YWidget *parent, YWidgetOpt & opt,
const YCPTerm & term, const YCPList & optList, int argnr );

static YWidget * parseComboBox( YWidget *parent, YWidgetOpt & opt,
const YCPTerm & term, const YCPList & optList, int argnr );

Expand Down
2 changes: 1 addition & 1 deletion src/YCPErrorDialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ you may find current contact information at www.novell.com

File: YCPErrorDialog.cc

Author: Stefan Hundhammer <sh@suse.de>
Author: Stefan Hundhammer <shundhammer@suse.de>

/-*/

Expand Down
2 changes: 1 addition & 1 deletion src/YCPErrorDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ you may find current contact information at www.novell.com

File: YCPErrorDialog.h

Author: Stefan Hundhammer <sh@suse.de>
Author: Stefan Hundhammer <shundhammer@suse.de>

/-*/

Expand Down
2 changes: 1 addition & 1 deletion src/YCPEvent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ you may find current contact information at www.novell.com

File: YCPEvent.cc

Author: Stefan Hundhammer <sh@suse.de>
Author: Stefan Hundhammer <shundhammer@suse.de>

/-*/

Expand Down
2 changes: 1 addition & 1 deletion src/YCPEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ you may find current contact information at www.novell.com

File: YCPEvent.h

Author: Stefan Hundhammer <sh@suse.de>
Author: Stefan Hundhammer <shundhammer@suse.de>

/-*/

Expand Down
2 changes: 1 addition & 1 deletion src/YCPItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ you may find current contact information at www.novell.com

File: YCPItem.h

Author: Stefan Hundhammer <sh@suse.de>
Author: Stefan Hundhammer <shundhammer@suse.de>

/-*/

Expand Down
2 changes: 1 addition & 1 deletion src/YCPItemParser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ you may find current contact information at www.novell.com

File: YCPItemParser.h

Author: Stefan Hundhammer <sh@suse.de>
Author: Stefan Hundhammer <shundhammer@suse.de>

/-*/

Expand Down
2 changes: 1 addition & 1 deletion src/YCPItemParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ you may find current contact information at www.novell.com

File: YCPItemParser.h

Author: Stefan Hundhammer <sh@suse.de>
Author: Stefan Hundhammer <shundhammer@suse.de>

/-*/

Expand Down
2 changes: 1 addition & 1 deletion src/YCPItemWriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ you may find current contact information at www.novell.com

File: YCPItemWriter.cc

Author: Stefan Hundhammer <sh@suse.de>
Author: Stefan Hundhammer <shundhammer@suse.de>

/-*/

Expand Down
2 changes: 1 addition & 1 deletion src/YCPItemWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ you may find current contact information at www.novell.com

File: YCPItemWriter.h

Author: Stefan Hundhammer <sh@suse.de>
Author: Stefan Hundhammer <shundhammer@suse.de>

/-*/

Expand Down
2 changes: 1 addition & 1 deletion src/YCPMacroPlayer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ you may find current contact information at www.novell.com

File: YCPMacroPlayer.cc

Author: Stefan Hundhammer <sh@suse.de>
Author: Stefan Hundhammer <shundhammer@suse.de>

/-*/

Expand Down
Loading