Kephra / lib / Kephra / App / Window.pm

use strict;
use warnings;
use Wx;
use Kephra::API;
use Kephra::App::Bar::Menu;
use Kephra::App::Bar::MainTool;
use Kephra::App::Bar::Search;
use Kephra::App::Bar::Status;
use Kephra::App::Dialog;
use Kephra::App::Panel::Editor;
use Kephra::App::Panel::TextLab;
use Kephra::App::Panel::IOUnit;
use Kephra::App::Panel::FileBrowser;
use Kephra::App::Panel::ProjectManager;
use Kephra::App::Panel::TreeLib;
use Kephra::App::Panel::OrgPad;
use Kephra::App::Panel::ScratchSheet;


package Kephra::App::Window;
our @ISA = 'Wx::Frame';
our $_ref;
my $title_end = "$Kephra::NAME $Kephra::VERSION";

Kephra::CommandList::register_cmd({
	'window-fullscreen-toggle' => { 
		sub => '$win->toggle_fullscreen', label => 'Fullscreen', keys => 'F11', 
		help => ''},
});


sub new {
	my( $class, $title ) = @_;
	my( $self ) = $_ref = $class->SUPER::new ( undef, -1, $title, [-1,-1],[1000, 800] );
	$self->SetIcon( Wx::GetWxPerlIcon() );
	$self->{'focus_stack'} = [$self];

	# container var for all panel, sizer and splitter
	my (%panel, %sizer, %splitter);
	$splitter{'left'}  = Wx::SplitterWindow->new( $self,             -1 );
	$splitter{'right'} = Wx::SplitterWindow->new( $splitter{'left'}, -1 );
	$splitter{'below'} = Wx::SplitterWindow->new( $splitter{'right'},-1 );
	$panel   {'above'} = Wx::Panel         ->new( $splitter{'below'});
	$splitter{'edit'}  = Wx::SplitterWindow->new( $panel   {'above'},-1 );

	for my $name (qw /left right above below edit/) {
		$panel{ $name } = Wx::Panel->new( $splitter{ $name } ) unless $name eq 'above';
		$sizer{ $name } = Wx::BoxSizer->new( &Wx::wxHORIZONTAL );
		$sizer{ $name } = Wx::BoxSizer->new( &Wx::wxVERTICAL ) if $name eq 'above'
															   or $name eq 'below';
		$panel{ $name }->SetSizer( $sizer{ $name } );
	}
	my $editor      = Kephra::App::Panel::Editor     ->new( $panel{'edit'} );
	my $searchbar   = Kephra::App::Bar::Search       ->new( $panel{'above'} );
	my $iounit      = Kephra::App::Panel::IOUnit     ->new( $panel{'below'} );
	my $filebrowser = Kephra::App::Panel::FileBrowser->new( $panel{'left'} );
	#my $projectman  = Kephra::App::Panel::ProjectManarger->new( $panel{'left'} );
	my $treelib     = Kephra::App::Panel::TreeLib    ->new( $panel{'right'} );
	#my $orgpad      = Kephra::App::Panel::OrgPad     ->new( $panel{'right'} );
	#my $scratchsheet= Kephra::App::Panel::ScratchSheet->new( $panel{'right'} );
	my $statusbar   = Kephra::App::Bar::Status       ->new( $self );

	$splitter{'left'}->SplitVertically( $panel{'left'}, $splitter{'right'}, 200);
	$splitter{'left'}->SetSashGravity(0);
	$splitter{'left'}->SetSashPosition(200);
	$splitter{'left'}->SetMinimumPaneSize(140);
	$splitter{'right'}->SplitVertically( $splitter{'below'}, $panel{'right'}, -200);
	$splitter{'right'}->SetSashGravity(1);
	$splitter{'right'}->SetSashPosition(-200);
	$splitter{'right'}->SetMinimumPaneSize(140);
	$splitter{'below'}->SplitHorizontally( $panel{'above'}, $panel{'below'}, -200);
	$splitter{'below'}->SetSashGravity(1);
	$splitter{'below'}->SetSashPosition(-200);
	$splitter{'below'}->SetMinimumPaneSize(100);
	$splitter{'edit'}->Initialize( $panel{'edit'} );

	$sizer{'edit'} ->Add( $editor,          1, &Wx::wxGROW );
	$sizer{'above'}->Add( $splitter{'edit'},1, &Wx::wxGROW );
	$sizer{'above'}->Add( $searchbar,       0, &Wx::wxGROW );
	$sizer{'below'}->Add( $iounit,          1, &Wx::wxGROW );
	$sizer{'right'}->Add( $treelib,         1, &Wx::wxGROW );
	$sizer{'left'} ->Add( $filebrowser,     1, &Wx::wxGROW );

	$sizer{'main'} = Wx::BoxSizer->new( &Wx::wxVERTICAL );
	$sizer{'main'}->Add( $splitter{'left'}, 1, &Wx::wxGROW);
	$sizer{'main'}->Add( $statusbar,        0, &Wx::wxGROW);
	$self->SetSizer( $sizer{'main'} );

	$self->SetMenuBar( Kephra::App::Bar::Menu::get() );
	$self->GetMenuBar->SetAcceleratorTable( Wx::AcceleratorTable->new() );

	#Wx::Event::EVT_SPLITTER_DCLICK( ,sub {});  #&Wx::wxSP_NOBORDER  );
	Wx::Event::EVT_CLOSE( $self, sub{ Kephra::App::close_save() });

	return $self;
}

sub close   { $_ref->Close() }
sub destroy { $_ref->Destroy() }

sub get_title { $_ref->GetTitle( )       }
sub set_title { $_ref->SetTitle( $_[1] ) }
sub refresh_title {
	my $self = shift;
	my $doc = Kephra::DocumentStash::active_doc();
	my $title = (ref $doc and $doc->file_name)
		? $doc->file_name.' ('.$doc->file_path.')'
		: Kephra::App::Panel::Editor::active_title();
	$self->set_title( $title . " - $title_end" );
}

sub toggle_fullscreen {
	my ($self) = @_;
	$self->{fullscreen} ^= 1;
	$self->ShowFullScreen ( 
		$self->{fullscreen}, 
		&Wx::wxFULLSCREEN_NOCAPTION | &Wx::wxFULLSCREEN_NOBORDER
	);
}

sub get_focus { $_[0]->{'focus_stack'}[0] }
sub set_focus {
	my ($self, $widget) = @_;
	die __PACKAGE__."::set_focus got no proper widget ($widget) from ", (caller)[0]
		unless Kephra::App::Util::is_widget($widget);
	unshift @{$self->{'focus_stack'}}, $widget;
	pop @{$self->{'focus_stack'}} if @{$self->{'focus_stack'}} > 12;
	$self->refocus;
}
sub set_focus_back {
	my ($self) = @_;
	shift @{$self->{'focus_stack'}} if @{$self->{'focus_stack'}} > 1;
	$self->refocus;
}
sub refocus { Wx::Window::SetFocus( $_[0]->{'focus_stack'}[0]) }

1;

__END__

sub get_fullscreen_mode { _config()->{fullscreen} }
sub switch_fullscreen_mode {
	_config()->{fullscreen} ^= 1;
	eval_fullscreen_flag();
}

sub eval_fullscreen_flag {
	_ref->ShowFullScreen( 
		get_fullscreen_mode(), 
		&Wx::wxFULLSCREEN_NOCAPTION | &Wx::wxFULLSCREEN_NOBORDER
);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.