Skip to content

Commit

Permalink
add split Menu into Menu,MenuEnter,and MenuLeave events
Browse files Browse the repository at this point in the history
  • Loading branch information
dk committed Oct 6, 2024
1 parent e482370 commit e54f6b3
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 38 deletions.
2 changes: 2 additions & 0 deletions Prima/Classes.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,8 @@ my %RNT = (
KeyUp => nt::Command,
Leave => nt::Default,
Menu => nt::Default,
MenuEnter => nt::Default,
MenuLeave => nt::Default,
MenuSelect => nt::Default,
MouseClick => nt::Command,
MouseDown => nt::Command,
Expand Down
8 changes: 4 additions & 4 deletions Prima/Menus.pm
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,8 @@ sub _export_constants
{
my %RNT = (
%{Prima::Widget-> notification_types()},
Select => nt::Default,
Submenu => nt::Default,
MenuSelect => nt::Default,
Menu => nt::Default,
);

sub notification_types { return \%RNT; }
Expand Down Expand Up @@ -611,7 +611,7 @@ sub selectedItem
$self->invalidate_rect( $self->get_item_rect($old) ) if $old >= 0;
$self->invalidate_rect( $self->get_item_rect($i) ) if $i >= 0;
if ( defined ( my $itemid = $self->{cache}->[$i]->[ITEMID] )) {
$self->root->notify(qw(Select), $itemid);
$self->root->notify(qw(MenuSelect), $itemid);
}
}

Expand All @@ -637,7 +637,7 @@ sub enter_submenu
$index = $self-> parentIndex + @{ $self->{cache} } - 1;
}

$self->root->notify(qw(Submenu), $itemid);
$self->root->notify(qw(Menu), $itemid);
return unless $self->alive;

$self->{submenu_index} = $i;
Expand Down
5 changes: 4 additions & 1 deletion Prima/VB/Classes.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,10 @@ sub prf_events
onHint => 'my ($self, $show) = @_;',
onKeyDown => 'my ($self, $code, $key, $mod, $repeat) = @_;',
onKeyUp => 'my ($self, $code, $key, $mod) = @_;',
onMenu => 'my ($self, $menu, $variable) = @_;',
onMenu => 'my ($self, $menu, $item) = @_;',
onMenuEnter => 'my ($self, $menu) = @_;',
onMenuLeave => 'my ($self, $menu) = @_;',
onMenuSelect => 'my ($self, $menu, $item) = @_;',
onMouseDown => 'my ($self, $btn, $mod, $x, $y) = @_;',
onMouseUp => 'my ($self, $btn, $mod, $x, $y) = @_;',
onMouseClick => 'my ($self, $btn, $mod, $x, $y, $dblclk) = @_;',
Expand Down
22 changes: 15 additions & 7 deletions class/Widget/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,15 +498,20 @@ void Widget_handle_event( Handle self, PEvent event)
apc_menu_get_font( event-> gen. source, &var-> popupFont);
break;
case cmMenu:
if ( event-> gen. H) {
if ( event-> gen.i >= 0 ) {
char buffer[16], *context;
context = ((( PAbstractMenu) event-> gen. H)-> self)-> make_id_context( event-> gen. H, event-> gen. i, buffer);
my-> notify( self, "<sHs", "Menu", event-> gen. H, context);
} else
my-> notify( self, "<sHS", "Menu", event-> gen. H, &PL_sv_undef);
if ( event-> gen. H ) {
char buffer[16], *context;
context = ((( PAbstractMenu) event-> gen. H)-> self)-> make_id_context( event-> gen. H, event-> gen. i, buffer);
my-> notify( self, "<sHs", "Menu", event-> gen. H, context);
}
break;
case cmMenuEnter:
if ( event-> gen. H)
my-> notify( self, "<sH", "MenuEnter", event-> gen. H);
break;
case cmMenuLeave:
if ( event-> gen. H)
my-> notify( self, "<sH", "MenuLeave", event-> gen. H);
break;
case cmMenuSelect:
if ( event-> gen. H) {
char buffer[16], *context;
Expand Down Expand Up @@ -727,6 +732,9 @@ void Widget_on_enter( Handle self) {}
void Widget_on_keydown( Handle self, int code , int key , int shiftState, int repeat ) {}
void Widget_on_keyup( Handle self, int code , int key , int shiftState ) {}
void Widget_on_menu( Handle self, Handle menu, char * variable) {}
void Widget_on_menuselect( Handle self, Handle menu, char * variable) {}
void Widget_on_menuenter( Handle self, Handle menu) {}
void Widget_on_menuleave( Handle self, Handle menu) {}
void Widget_on_setup( Handle self) {}
void Widget_on_size( Handle self, Point oldSize, Point newSize) {}
void Widget_on_move( Handle self, Point oldPos, Point newPos) {}
Expand Down
19 changes: 11 additions & 8 deletions examples/menu.pl
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,17 @@ sub on_menuselect
$self->Hint->text( $menu->hint($item) // '' );
}

sub on_menu
sub on_menuenter
{
my ( $self, $menu, $item ) = @_;
if ( defined $item ) {
$self->Hint->{saved} //= $self->Hint->text;
} else {
$self->Hint->text( delete $self->Hint->{saved} );
}
my ( $self ) = @_;
$self->Hint->{saved} = $self->Hint->text;
}

sub on_menuleave
{
my ( $self ) = @_;
warn;
$self->Hint->text( delete $self->Hint->{saved} );
}

package UserInit;
Expand All @@ -312,7 +315,7 @@ package UserInit;
onMenuLeave => sub {
$w->Hint->text( delete $w->Hint->{saved} );
},
onSelect => sub {
onMenuSelect => sub {
my ( $self, $item ) = @_;
$w->Hint->text( $self->menu->hint($item) // '' );
},
Expand Down
4 changes: 4 additions & 0 deletions include/apricot.h
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,10 @@ CM(MenuItemPaint)
CM(Clipboard)
#define cmMenuSelect 0x00000030 /* window notification to menu highlight */
CM(MenuSelect)
#define cmMenuEnter 0x00000031 /* start menu session */
CM(MenuEnter)
#define cmMenuLeave 0x00000032 /* stop menu session */
CM(MenuLeave)

#define cmMenuCmd 0x00000050 /* interactive menu command */
CM(MenuCmd)
Expand Down
17 changes: 15 additions & 2 deletions pod/Prima/Widget.pod
Original file line number Diff line number Diff line change
Expand Up @@ -2874,11 +2874,11 @@ Called when the input focus is removed from the widget

See also: C<Enter>, C<focused>, C<selected>

=item Menu MENU VAR_NAME
=item Menu MENU, ITEM

Called before the user-navigated menu ( pop-up or pull-down ) is about to show
another level of submenu on the screen. MENU is a Prima::AbstractMenu
descendant, that is also a direct child to the widget. VAR_NAME is the name of
descendant, that is also a direct child to the widget. ITEM is the name of
the menu item that is about to be shown.

Can be used for making dynamic changes in the menu structures, f.ex. enabling
Expand All @@ -2887,6 +2887,19 @@ pasted.

See also: C<popupItems>

=item MenuEnter MENU

Called before the user invokes a menu or a popup

=item MenuLeave MENU

Called after the user ends a menu or a popup session

=item MenuSelect MENU, ITEM

Called when the user selects (but not executes) a menu item. ITEM is the name
of the menu item that is about to be shown.

=item MouseClick BUTTON, MOD, X, Y, NTH

Called when the mouse click ( a button is pressed, then released, within the
Expand Down
19 changes: 7 additions & 12 deletions unix/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ menu_item_size( PMenuSysData XX, PMenuWindow w, int index)
}

static Bool
send_command( Handle self, PMenuItemReg m, int cmd)
send_command( Handle self, int cmd, PMenuItemReg m)
{
Event ev;
Handle owner = PComponent( self)-> owner;
Expand All @@ -696,8 +696,6 @@ send_command( Handle self, PMenuItemReg m, int cmd)
return true;
}

#define send_cmMenu(s,m) send_command(s,m,cmMenu)

static void
menu_select_item( Handle self, PMenuWindow w, int index)
{
Expand Down Expand Up @@ -728,12 +726,9 @@ menu_select_item( Handle self, PMenuWindow w, int index)
if (index >= 0) {
PMenuItemReg m = w->m;
while (index--) m = m->next;
send_command( self, m, cmMenuSelect );
} else if (index == -100) {
MenuItemReg m;
m.id = -1;
send_cmMenu(self, &m);
}
send_command( self, cmMenuSelect, m );
} else if (index == -100)
send_command(self, cmMenuLeave, NULL);
}
}

Expand Down Expand Up @@ -766,7 +761,7 @@ menu_enter_item( PMenuSysData XX, PMenuWindow w, int index, int type)
}

if ( index != w-> last + 1) {
if ( !send_cmMenu( w-> self, m)) return false;
if ( !send_command( w-> self, cmMenu, m)) return false;
m = m-> down;
}

Expand Down Expand Up @@ -1747,7 +1742,7 @@ handle_menu_button( XEvent *ev, XWindow win, Handle self)
}
XSetInputFocus( DISP, XX-> w-> w, RevertToNone, CurrentTime);
guts. currentMenu = self;
if ( first && ( ev-> type == ButtonPress) && ( !send_cmMenu( self, NULL)))
if ( first && ( ev-> type == ButtonPress) && ( !send_command( self, cmMenuEnter, NULL)))
return;
if ( !first && ( ev-> type == ButtonPress)) return;
apc_timer_stop( MENU_TIMER);
Expand Down Expand Up @@ -2473,7 +2468,7 @@ apc_popup( Handle self, int x, int y, Rect *anchor)
prima_end_menu();
if (!(m=TREE)) return false;
guts. currentMenu = self;
if ( !send_cmMenu( self, NULL)) return false;
if ( !send_command( self, cmMenuEnter, NULL)) return false;
if (!(w = get_window(self,m))) return false;
update_menu_window(XX, w);
if ( anchor-> left == 0 && anchor-> right == 0 && anchor-> top == 0 && anchor-> bottom == 0) {
Expand Down
11 changes: 7 additions & 4 deletions win32/global.c
Original file line number Diff line number Diff line change
Expand Up @@ -780,9 +780,13 @@ handle_widget_initmenu_events(dWM_HANDLER, UINT msg)
if ( mwd && mwd-> menu && ( PAbstractMenu(mwd-> menu)->stage <= csNormal)) {
m = ( PMenuItemReg) AbstractMenu_first_that( mwd-> menu, find_oid, INT2PTR(void*,mwd->id), true);
hiStage = true;
ev->cmd = cmMenu;
ev->gen. H = mwd-> menu;
ev->gen. i = m ? m-> id : 0;
if ( m ) {
ev->cmd = cmMenuEnter;
} else {
ev->cmd = cmMenu;
ev->gen. i = m-> id;
}
}
if (( msg == WM_INITMENUPOPUP) && ( m == NULL))
ev->cmd = 0;
Expand Down Expand Up @@ -1029,9 +1033,8 @@ handle_widget_wm_menuselect( dWM_HANDLER )
return;

if ( HIWORD(mp1) == 0xffff && mp2 == 0 ) {
ev-> cmd = cmMenu;
ev-> cmd = cmMenuLeave;
ev-> gen.H = sys last_menu;
ev-> gen.i = -1;
return;
}

Expand Down

0 comments on commit e54f6b3

Please sign in to comment.