| 1 | package Wrangler::Wx::Main; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Wx qw(:everything); | ||
| 7 | use base qw(Wx::Frame); | ||
| 8 | use Wx::Event qw(EVT_MOVE EVT_CLOSE EVT_SPLITTER_SASH_POS_CHANGED); | ||
| 9 | use Wrangler::Images; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my ($self,$parent) = @_; | ||
| 13 | |||
| 14 | $self = $self->SUPER::new(undef, wxID_ANY, 'Wrangler', wxDefaultPosition, [$parent->config('ui.main.width'),$parent->config('ui.main.height')], | ||
| 15 | wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE | wxCLIP_CHILDREN | ||
| 16 | ); | ||
| 17 | |||
| 18 | $self->{wrangler} = $parent; | ||
| 19 | |||
| 20 | Wx::InitAllImageHandlers(); # is this still leaking memory? | ||
| 21 | |||
| 22 | # load icons (at least on linux, scaling doesn't work, so we skip most versions) | ||
| 23 | my $iconbundle = Wx::IconBundle->new(); | ||
| 24 | # $iconbundle->AddIcon('sources/logo/logo_2.x_icon256.png', wxBITMAP_TYPE_PNG); | ||
| 25 | # $iconbundle->AddIcon('sources/logo/logo_2.x_icon128.png', wxBITMAP_TYPE_PNG); | ||
| 26 | $iconbundle->AddIcon( Wx::Icon->newFromXPM($Wrangler::Images::image{'logo_2.x_icon64'}) ); | ||
| 27 | # $iconbundle->AddIcon('sources/logo/logo_2.x_icon48.png', wxBITMAP_TYPE_PNG); | ||
| 28 | # $iconbundle->AddIcon('sources/logo/logo_2.x_icon32.png', wxBITMAP_TYPE_PNG); | ||
| 29 | # $iconbundle->AddIcon('sources/logo/logo_2.x_icon24.png', wxBITMAP_TYPE_PNG); | ||
| 30 | # $iconbundle->AddIcon('sources/logo/logo_2.x_icon16.png', wxBITMAP_TYPE_PNG); | ||
| 31 | $self->SetIcons( $iconbundle ); | ||
| 32 | |||
| 33 | # Prepare images into a lookup-hash (as in Padre::Wx::Outline) | ||
| 34 | my $imagelist = Wx::ImageList->new( 16, 16 ); | ||
| 35 | my $images = { | ||
| 36 | folder => $imagelist->Add( | ||
| 37 | Wx::ArtProvider::GetBitmap( 'wxART_FOLDER', 'wxART_OTHER_C', [ 16, 16 ], ), | ||
| 38 | ), | ||
| 39 | file => $imagelist->Add( | ||
| 40 | Wx::ArtProvider::GetBitmap( 'wxART_NORMAL_FILE', 'wxART_OTHER_C', [ 16, 16 ], ), | ||
| 41 | ), | ||
| 42 | go_up => $imagelist->Add( | ||
| 43 | Wx::ArtProvider::GetBitmap( 'wxART_GO_TO_PARENT', 'wxART_OTHER_C', [ 16, 16 ], ), | ||
| 44 | ), | ||
| 45 | missing_image => $imagelist->Add( | ||
| 46 | Wx::ArtProvider::GetBitmap( 'wxART_MISSING_IMAGE', 'wxART_OTHER_C', [ 16, 16 ], ), | ||
| 47 | ), | ||
| 48 | go_home => $imagelist->Add( | ||
| 49 | Wx::ArtProvider::GetBitmap( 'wxART_GO_HOME', 'wxART_OTHER_C', [ 16, 16 ], ), | ||
| 50 | ), | ||
| 51 | generic_image => $imagelist->Add( | ||
| 52 | Wx::ArtProvider::GetBitmap( 'wxART_MISSING_IMAGE', 'wxART_OTHER_C', [ 16, 16 ], ), | ||
| 53 | ), | ||
| 54 | generic_audio => $imagelist->AddWithColourMask( | ||
| 55 | Wx::Bitmap->newFromXPM($Wrangler::Images::image{'audio-x-generic'}), | ||
| 56 | Wx::Colour->new(255,255,255) | ||
| 57 | ), | ||
| 58 | generic_video => $imagelist->AddWithColourMask( | ||
| 59 | Wx::Bitmap->newFromXPM($Wrangler::Images::image{'video-x-generic'}), | ||
| 60 | Wx::Colour->new(0,0,0) | ||
| 61 | ), | ||
| 62 | brick => $imagelist->AddWithColourMask( | ||
| 63 | Wx::Bitmap->newFromXPM($Wrangler::Images::image{'brick'}), | ||
| 64 | Wx::Colour->new(255,255,255) | ||
| 65 | ), | ||
| 66 | device => $imagelist->Add( | ||
| 67 | Wx::ArtProvider::GetBitmap( 'wxART_HARDDISK', 'wxART_OTHER_C', [ 16, 16 ], ), | ||
| 68 | ), | ||
| 69 | trash => $imagelist->AddWithColourMask( | ||
| 70 | Wx::Bitmap->newFromXPM($Wrangler::Images::image{'user-trash'}), | ||
| 71 | Wx::Colour->new(0,0,0) | ||
| 72 | ), | ||
| 73 | # cdrom => $imagelist->Add( | ||
| 74 | # Wx::ArtProvider::GetBitmap( 'gtk-cdrom', 'wxART_OTHER_C', [ 16, 16 ], ), # only works on Linux+GTK | ||
| 75 | # ), | ||
| 76 | }; | ||
| 77 | |||
| 78 | if( $parent->config('ui.main.centered') ){ | ||
| 79 | $self->Centre( wxBOTH ); | ||
| 80 | $self->Centred(1); | ||
| 81 | }else{ | ||
| 82 | $self->Move( # instead of 'on construction' | ||
| 83 | $parent->config('ui.main.posX') || -1, | ||
| 84 | $parent->config('ui.main.posY') || -1, | ||
| 85 | ); | ||
| 86 | } | ||
| 87 | |||
| 88 | Wrangler::PubSub::freeze(); ## << | ||
| 89 | |||
| 90 | ## create and associate a MenuBar | ||
| 91 | if( $parent->config('ui.layout.menubar') ){ | ||
| 92 | $self->OnToggleMenuBar(1); | ||
| 93 | } | ||
| 94 | |||
| 95 | ## create and associate a Status Bar | ||
| 96 | if( $parent->config('ui.layout.statusbar') ){ | ||
| 97 | $self->OnToggleStatusBar(1); | ||
| 98 | } | ||
| 99 | |||
| 100 | $self->{splitter1} = Wx::SplitterWindow->new( $self, -1, wxDefaultPosition, [780,1000], wxNO_FULL_REPAINT_ON_RESIZE|wxCLIP_CHILDREN|wxSP_NOBORDER|wxSP_LIVE_UPDATE ); | ||
| 101 | $self->{splitter1}->SetMinimumPaneSize( 30 ); # As per rt#84591 | ||
| 102 | $self->{splitter1}->{imagelist} = \$imagelist; | ||
| 103 | $self->{splitter1}->{images} = $images; | ||
| 104 | $self->{splitter1}->{wrangler} = $parent; | ||
| 105 | |||
| 106 | $self->{splitter2} = Wx::SplitterWindow->new( $self->{splitter1}, -1, wxDefaultPosition, [740,1000], wxNO_FULL_REPAINT_ON_RESIZE|wxCLIP_CHILDREN|wxSP_NOBORDER|wxSP_LIVE_UPDATE ); | ||
| 107 | $self->{splitter2}->SetMinimumPaneSize( 30 ); # As per rt#84591 | ||
| 108 | $self->{splitter2}->{imagelist} = \$imagelist; | ||
| 109 | $self->{splitter2}->{images} = $images; | ||
| 110 | $self->{splitter2}->{wrangler} = $parent; | ||
| 111 | |||
| 112 | # require Wrangler::Wx::Sidebar; | ||
| 113 | # $self->{sidebar} = Wrangler::Wx::Sidebar->new($self->{splitter2}); | ||
| 114 | # $self->{widgets}->{sidebar} = 'splitter2'; | ||
| 115 | |||
| 116 | $self->{splitter3} = Wx::SplitterWindow->new( $self->{splitter2}, -1, wxDefaultPosition, [740,870], wxNO_FULL_REPAINT_ON_RESIZE|wxCLIP_CHILDREN|wxSP_NOBORDER|wxSP_LIVE_UPDATE ); | ||
| 117 | $self->{splitter3}->SetMinimumPaneSize( 30 ); # As per rt#84591 | ||
| 118 | $self->{splitter3}->{imagelist} = \$imagelist; | ||
| 119 | $self->{splitter3}->{images} = $images; | ||
| 120 | $self->{splitter3}->{wrangler} = $parent; | ||
| 121 | |||
| 122 | # require Wrangler::Wx::FileBrowserTreeList; | ||
| 123 | # $self->{filebrowser} = Wrangler::Wx::FileBrowserTreeList->new($self->{splitter3}); | ||
| 124 | require Wrangler::Wx::FileBrowser; | ||
| 125 | $self->{filebrowser} = Wrangler::Wx::FileBrowser->new($self->{splitter3}); | ||
| 126 | $self->{widgets}->{filebrowser} = 'splitter2'; # $self->{widgets} keeps track of where widgets are/were positioned | ||
| 127 | |||
| 128 | $self->{splitter4} = Wx::SplitterWindow->new( $self->{splitter3}, -1, wxDefaultPosition, [740,250], wxNO_FULL_REPAINT_ON_RESIZE|wxCLIP_CHILDREN|wxSP_NOBORDER|wxSP_LIVE_UPDATE ); | ||
| 129 | $self->{splitter4}->SetMinimumPaneSize( 30 ); # As per rt#84591 | ||
| 130 | $self->{splitter4}->{imagelist} = \$imagelist; | ||
| 131 | $self->{splitter4}->{images} = $images; | ||
| 132 | $self->{splitter4}->{wrangler} = $parent; | ||
| 133 | |||
| 134 | require Wrangler::Wx::Previewer; | ||
| 135 | $self->{previewer} = Wrangler::Wx::Previewer->new($self->{splitter4}); | ||
| 136 | $self->{widgets}->{previewer} = 'splitter3'; | ||
| 137 | |||
| 138 | $self->{splitter1}->Initialize($self->{splitter2}); | ||
| 139 | $self->{widgets}->{navbar} = 'splitter1'; # $self->{widgets} keeps track of where widgets are/were positioned | ||
| 140 | if( $parent->config('ui.layout.navbar') ){ | ||
| 141 | $self->OnToggleNavbar(1); | ||
| 142 | } | ||
| 143 | |||
| 144 | # $self->{splitter2}->SplitVertically( | ||
| 145 | # $self->{sidebar}, | ||
| 146 | # $self->{splitter3}, | ||
| 147 | # 130 | ||
| 148 | # ); | ||
| 149 | $self->{splitter2}->Initialize($self->{splitter3}); | ||
| 150 | $self->{widgets}->{sidebar} = 'splitter2'; # $self->{widgets} keeps track of where widgets are/were positioned | ||
| 151 | if( $parent->config('ui.layout.sidebar') ){ | ||
| 152 | $self->OnToggleSidebar(1); | ||
| 153 | } | ||
| 154 | |||
| 155 | $self->{splitter3}->SplitVertically( | ||
| 156 | $self->{filebrowser}, | ||
| 157 | $self->{splitter4}, | ||
| 158 | 600 # doesn't react, thus enforced with MinimumPaneSize | ||
| 159 | ); | ||
| 160 | |||
| 161 | $self->{widgets}->{formeditor} = 'splitter4'; | ||
| 162 | #- $self->{widgets}->{treeeditor} = 'splitter4'; | ||
| 163 | |||
| 164 | # $self->{splitter4}->SplitHorizontally( | ||
| 165 | # $self->{previewer}, | ||
| 166 | # $self->{formeditor}, | ||
| 167 | # 300 | ||
| 168 | # ); | ||
| 169 | $self->{splitter4}->Initialize($self->{previewer}); | ||
| 170 | $self->OnReCreateFormEditor(); | ||
| 171 | #- $self->OnReCreateTreeEditor(); | ||
| 172 | |||
| 173 | Wrangler::PubSub::thaw(); ## << | ||
| 174 | |||
| 175 | $self->SetMinSize( Wx::Size->new(150,100) ); | ||
| 176 | |||
| 177 | $self->Show(1); | ||
| 178 | |||
| 179 | ## register our event listeners | ||
| 180 | Wrangler::PubSub::subscribe('dir.activated', sub { | ||
| 181 | return unless $_[0]; | ||
| 182 | Wrangler::debug("Main: OnDirActivated: @_"); | ||
| 183 | $self->SetTitle($_[0]); | ||
| 184 | $parent->{current_dir} = $_[0]; # keep track of a "current directory scope" globally, used by available_properties() calls | ||
| 185 | $0 = 'wrangler '.$_[0]; # set program_name, for ps | ||
| 186 | },__PACKAGE__); | ||
| 187 | Wrangler::PubSub::subscribe('show.settings', sub { | ||
| 188 | require Wrangler::Wx::Dialog::Settings; | ||
| 189 | Wrangler::Wx::Dialog::Settings->new($self, @_); | ||
| 190 | },__PACKAGE__); | ||
| 191 | Wrangler::PubSub::subscribe('show.about', sub { | ||
| 192 | require Wrangler::Wx::Dialog::About; | ||
| 193 | Wrangler::Wx::Dialog::About->new($self, @_); | ||
| 194 | },__PACKAGE__); | ||
| 195 | |||
| 196 | ## Wx events | ||
| 197 | EVT_CLOSE($self, \&OnClose); | ||
| 198 | EVT_SPLITTER_SASH_POS_CHANGED( $self, $self->{splitter}, \&OnSashPosChanged ); | ||
| 199 | |||
| 200 | return $self; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub OnToggleMenuBar { | ||
| 204 | my $self = shift; | ||
| 205 | my $force = shift; # force toggle into associating a MenuBar | ||
| 206 | |||
| 207 | if( !$force && $self->GetMenuBar() ){ | ||
| 208 | $self->SetMenuBar( undef ); | ||
| 209 | delete($self->{menu}); | ||
| 210 | }else{ | ||
| 211 | require Wrangler::Wx::Menu; | ||
| 212 | $self->{menu} = Wrangler::Wx::Menu->new($self); | ||
| 213 | $self->SetMenuBar( $self->{menu} ); | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | sub OnToggleStatusBar { | ||
| 218 | my $self = shift; | ||
| 219 | my $force = shift; # force toggle into associating a StatusBar | ||
| 220 | |||
| 221 | if( !$force && $self->GetStatusBar() ){ | ||
| 222 | # StatusBar behaves differently from MenuBar; $self->SetStatusBar( undef ) has no effect | ||
| 223 | $self->{statusbar}->Destroy(); | ||
| 224 | delete($self->{statusbar}); | ||
| 225 | }else{ | ||
| 226 | require Wrangler::Wx::StatusBar; | ||
| 227 | $self->{statusbar} = Wrangler::Wx::StatusBar->new($self); | ||
| 228 | $self->SetStatusBar($self->{statusbar}); | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | sub OnToggleNavbar { | ||
| 233 | my $self = shift; | ||
| 234 | my $force = shift; # force toggle into associating a Navbar | ||
| 235 | |||
| 236 | if( !$force && $self->{navbar} ){ | ||
| 237 | $self->{ $self->{widgets}->{navbar} }->Unsplit($self->{navbar}); | ||
| 238 | $self->{navbar}->Destroy(); | ||
| 239 | delete($self->{navbar}); | ||
| 240 | }else{ | ||
| 241 | require Wrangler::Wx::Navbar; | ||
| 242 | $self->{navbar} = Wrangler::Wx::Navbar->new($self->{ $self->{widgets}->{navbar} }, -1, wxDefaultPosition, wxDefaultSize); | ||
| 243 | my $window1 = $self->{ $self->{widgets}->{navbar} }->GetWindow1(); | ||
| 244 | $self->{ $self->{widgets}->{navbar} }->SplitHorizontally($self->{navbar},$window1,40); | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | sub OnToggleSidebar { | ||
| 249 | my $self = shift; | ||
| 250 | my $force = shift; # force toggle into associating a Navbar | ||
| 251 | |||
| 252 | if( !$force && $self->{sidebar} ){ | ||
| 253 | $self->{ $self->{widgets}->{sidebar} }->Unsplit($self->{sidebar}); | ||
| 254 | $self->{sidebar}->Destroy(); | ||
| 255 | delete($self->{sidebar}); | ||
| 256 | }else{ | ||
| 257 | require Wrangler::Wx::Sidebar; | ||
| 258 | $self->{sidebar} = Wrangler::Wx::Sidebar->new($self->{ $self->{widgets}->{sidebar} }, -1, wxDefaultPosition, wxDefaultSize); | ||
| 259 | my $window1 = $self->{ $self->{widgets}->{sidebar} }->GetWindow1(); | ||
| 260 | $self->{ $self->{widgets}->{sidebar} }->SplitVertically($self->{sidebar},$window1,180); | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | sub OnReCreateFormEditor { | ||
| 265 | my $self = shift; | ||
| 266 | my $force = shift; # force toggle into associating a FormEditor | ||
| 267 | |||
| 268 | if( $self->{formeditor} ){ | ||
| 269 | $self->{ $self->{widgets}->{formeditor} }->Unsplit($self->{formeditor}); | ||
| 270 | $self->{formeditor}->Destroy(); | ||
| 271 | delete($self->{formeditor}); | ||
| 272 | } | ||
| 273 | |||
| 274 | require Wrangler::Wx::FormEditor; | ||
| 275 | $self->{formeditor} = Wrangler::Wx::FormEditor->new($self->{ $self->{widgets}->{formeditor} }); | ||
| 276 | |||
| 277 | unless($self->{ $self->{widgets}->{formeditor} }->IsSplit() ){ | ||
| 278 | my $window1 = $self->{ $self->{widgets}->{formeditor} }->GetWindow1(); | ||
| 279 | $self->{ $self->{widgets}->{formeditor} }->SplitHorizontally($window1,$self->{formeditor},300); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | sub OnReCreateTreeEditor { | ||
| 284 | my $self = shift; | ||
| 285 | my $force = shift; # force toggle into associating a TreeEditor | ||
| 286 | |||
| 287 | if( $self->{treeeditor} ){ | ||
| 288 | $self->{ $self->{widgets}->{treeeditor} }->Unsplit($self->{treeeditor}); | ||
| 289 | $self->{treeeditor}->Destroy(); | ||
| 290 | delete($self->{treeeditor}); | ||
| 291 | } | ||
| 292 | |||
| 293 | require Wrangler::Wx::TreeEditor; | ||
| 294 | $self->{treeeditor} = Wrangler::Wx::TreeEditor->new($self->{ $self->{widgets}->{treeeditor} }); | ||
| 295 | |||
| 296 | unless($self->{ $self->{widgets}->{treeeditor} }->IsSplit() ){ | ||
| 297 | my $window1 = $self->{ $self->{widgets}->{treeeditor} }->GetWindow1(); | ||
| 298 | $self->{ $self->{widgets}->{treeeditor} }->SplitHorizontally($window1,$self->{treeeditor},300); | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | sub OnSashPosChanged { | ||
| 303 | my( $self, $event ) = @_; | ||
| 304 | |||
| 305 | print "Final sash position = ". $event->GetSashPosition ."\n"; | ||
| 306 | } | ||
| 307 | |||
| 308 | sub Centred { | ||
| 309 | if($_[1]){ | ||
| 310 | # pin position+size (don't check if frame is actually centred) | ||
| 311 | @{ $_[0]->{CenteredPos} } = $_[0]->GetPositionXY(); | ||
| 312 | @{ $_[0]->{CenteredSize} } = $_[0]->GetSizeWH(); | ||
| 313 | return; | ||
| 314 | } | ||
| 315 | delete($_[0]->{CenteredPos}); | ||
| 316 | } | ||
| 317 | sub IsCentred { | ||
| 318 | my @pos = $_[0]->GetPositionXY(); | ||
| 319 | my @size = $_[0]->GetSizeWH(); | ||
| 320 | |||
| 321 | if(${ $_[0]->{CenteredPos} }[0] && ${ $_[0]->{CenteredPos} }[1]){ | ||
| 322 | if($pos[0] == ${ $_[0]->{CenteredPos} }[0] && $pos[1] == ${ $_[0]->{CenteredPos} }[1]){ | ||
| 323 | if( ${ $_[0]->{CenteredSize} }[0] && ${ $_[0]->{CenteredSize} }[1]){ | ||
| 324 | return 1 if $size[0] == ${ $_[0]->{CenteredSize} }[0] && $size[1] == ${ $_[0]->{CenteredSize} }[1]; | ||
| 325 | } | ||
| 326 | } | ||
| 327 | } | ||
| 328 | return 0; | ||
| 329 | } | ||
| 330 | |||
| 331 | sub OnMove { | ||
| 332 | my ($self,$event) = @_; | ||
| 333 | my $p = $event->GetPosition(); | ||
| 334 | print "Wrangler::Main::OnMove ". $p->x ."\n"; | ||
| 335 | |||
| 336 | $self->IsCentered(0); | ||
| 337 | } | ||
| 338 | |||
| 339 | sub OnClose { | ||
| 340 | my ($self,$event) = @_; | ||
| 341 | |||
| 342 | my $maximized = $self->IsMaximized(); | ||
| 343 | my $centered = $self->IsCentred(); | ||
| 344 | my ($posX,$posY) = $self->GetPositionXY(); # ok to use instead of GetScreenPosition() | ||
| 345 | my ($width,$height) = $self->GetSizeWH(); | ||
| 346 | Wrangler::debug("Wrangler::Wx::Main::OnClose: Window: maximized:$maximized,centred:$centered, posX:$posX,posY:$posY, width:$width,height:$height"); | ||
| 347 | |||
| 348 | ## store ui. config-values | ||
| 349 | my $config = $self->{wrangler}->config(); | ||
| 350 | $config->{'ui.main.maximized'} = $maximized; | ||
| 351 | $config->{'ui.main.width'} = $width; | ||
| 352 | $config->{'ui.main.height'} = $height; | ||
| 353 | if($centered){ | ||
| 354 | delete($config->{'ui.main.posX'}); | ||
| 355 | delete($config->{'ui.main.posY'}); | ||
| 356 | $config->{'ui.main.centered'} = 1; | ||
| 357 | }else{ | ||
| 358 | $config->{'ui.main.posX'} = $posX; | ||
| 359 | $config->{'ui.main.posY'} = $posY; | ||
| 360 | delete($config->{'ui.main.centered'}); | ||
| 361 | } | ||
| 362 | |||
| 363 | $event->Skip(); # keep on propagating | ||
| 364 | } | ||
| 365 | |||
| 366 | 1; |