Source

Kephra / lib / Kephra / App / Dialog / Keymap.pm

use strict;
use warnings;

use Wx;
use Kephra::CommandList;

package Kephra::App::Dialog::Keymap;
my $cache;

sub create {
	return $cache if defined $cache;
	my $dialog = $cache = Wx::Dialog->new(
		Kephra::App::Dialog::_parent(shift), -1,
		$Kephra::NAME . ' '.$Kephra::VERSION .' Keymap Settings', [-1,-1], [800,600]
	);
	my $list = Wx::ListCtrl->new(
		$dialog, -1,  &Wx::wxDefaultPosition, &Wx::wxDefaultSize, &Wx::wxLC_REPORT 
	);
	my $col;
	my $data = Kephra::CommandList::_raw();
	$list->InsertColumn($col++, $_) for qw(Command Keys Help);
	$list->SetColumnWidth(0, 220);
	$list->SetColumnWidth(1, 100);
	$list->SetColumnWidth(2, 480);
	my $row;
	for (sort keys %$data){
		my $iid = $list->InsertImageStringItem( $row++, $_, 0 ); # imd 3rd index
		$list->SetItem( $iid, 1, $data->{$_}{keys}, 0) if $data->{$_}{keys};
		$list->SetItem( $iid, 2, $data->{$_}{help}, 0) if $data->{$_}{help};
	}

	my @button_label = qw(Accept Cancel); # from left to right
	my %button;
	$button{lc $_} = Wx::Button->new( $dialog, -1, $_ ) for @button_label;
	my $keysizer = Wx::BoxSizer->new( &Wx::wxHORIZONTAL );
	$keysizer->AddStretchSpacer();
	$keysizer->Add($button{lc($_)}, 0, &Wx::wxGROW | &Wx::wxALL, 10) for @button_label;
	my $sizer = Wx::BoxSizer->new( &Wx::wxVERTICAL );
	$sizer->Add($list, 1, Wx::wxGROW, 0);
	$sizer->Add($keysizer, 0, &Wx::wxGROW, 0);
	$dialog->SetSizer($sizer);

	Wx::Event::EVT_BUTTON($dialog, $button{'accept'}, sub {
		$dialog->EndModal(0);
	});
	Wx::Event::EVT_BUTTON($dialog, $button{'cancel'}, sub {
		$dialog->EndModal(0);
	});

	$dialog;
}

1;