#! / usr / bin / perl -w use strict; use Tkx; our $ PROGNAME = 'app'; our $ VERSION = '0.1'; <habracut> my $ windowingsystem = Tkx :: tk_windowingsystem (); my $ IS_AQUA = ($ windowingsystem eq 'aqua'); # Given that the names of some buttons may vary, # in OSX this is Control, in Win / X11 this is Ctrl, add some more variables # (useful for hotkeys) my $ plat_acc_ctrl = ($ ^ O eq 'darwin')? ('Control-'): ('Ctrl +'); my $ plat_evt_ctrl = ($ ^ O eq 'darwin')? ('Control-'): ('Control-'); # Main window. my $ mw = Tkx :: widget-> new ('.'); Tkx :: tk (appname => $ PROGNAME); Tkx :: wm_minsize ($ mw => qw (320 200)); # new menu is attached using the -menu option $ mw-> configure ( -menu => make_menu ($ mw), ); Tkx :: MainLoop; one; sub on_quit { Tkx :: tk ___ messageBox (-message => 'Goodbye;]'); exit; } sub show_about { Tkx :: tk ___ messageBox (-message => "$ PROGNAME $ VERSION"); } sub make_menu { my $ mw = shift || return; # By default, all menus look like in GIMP, you can unpin them # Therefore, turn off this option. Tkx :: option_add ('* Menu.tearOff', 0) # ... continued below return $ m; }
my $ m = $ mw-> new_menu (); # Upper level my $ fm = $ m-> new_menu (); # File my $ em = $ m-> new_menu (); # Edit # Now, add our items to the top level $ m-> add_cascade (-label => 'File', -menu => $ fm, -underline => 0); $ m-> add_cascade (-label => 'Edit', -menu => $ em, -underline => 0);
# File menu $ fm-> add_command ( -label => 'Do something', -underline => 0, -command => sub {}, ); # separator $ fm-> add_separator (); $ fm-> add_command ( -label => 'Quit', -underline => 0, -accelerator => "$ {plat_acc_ctrl} Q", -command => [\ & on_quit], ); Tkx :: bind (all => "<$ {plat_evt_ctrl} q>" => [\ & on_quit]); # Edit menu $ em-> add_command ( -label => 'Cut', -underline => 2, -accelerator => "$ {plat_acc_ctrl} X", -command => sub {Tkx :: event_generate (Tkx :: focus (), '<< anchor> habracut </ anchor >>')}, ); $ em-> add_command ( -label => 'Copy', -underline => 0, -accelerator => "$ {plat_acc_ctrl} C", -command => sub {Tkx :: event_generate (Tkx :: focus (), '<< Copy >>')}, ); $ em-> add_command ( -label => 'Paste', -underline => 0, -accelerator => "$ {plat_acc_ctrl} V", -command => sub {Tkx :: event_generate (Tkx :: focus (), '<< Paste >>')}, );
my $ hname = ($ IS_AQUA? 'nothelp': 'help'); my $ hm = $ m-> new_menu (-name => $ hname); $ m-> add_cascade ( -label => 'Help', -menu => $ hm, -underline => 0, ); # .. add help, homepage, etc. if (! $ IS_AQUA) { $ hm-> add_command ( -label => 'About', -underline => 0, -command => [\ & show_about], ); } else { my $ apple_menu = $ m-> new_menu (-name => 'apple'); $ m-> add_cascade ( -label => $ PROGNAME, -menu => $ apple_menu, ); $ m-> add_command ( -label => "About $ PROGNAME", -command => [\ & show_about], ); }
Source: https://habr.com/ru/post/68840/
All Articles