| 1 | package Wrangler::Wx::FormEditor; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'Wx::Panel'; | ||
| 7 | use Wx ':everything'; | ||
| 8 | use Wx::Event qw(EVT_BUTTON EVT_TEXT EVT_CHAR EVT_RIGHT_UP EVT_MENU EVT_COMBOBOX EVT_TEXT_ENTER); | ||
| 9 | use JSON::XS (); | ||
| 10 | use Path::Tiny; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $class = shift; | ||
| 14 | my $parent = shift; | ||
| 15 | my $self = $class->SUPER::new( $parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL); | ||
| 16 | |||
| 17 | bless $self, $class; | ||
| 18 | |||
| 19 | # hook-up access to $wrangler | ||
| 20 | $self->{wrangler} = $parent->{wrangler}; | ||
| 21 | |||
| 22 | $self->SetForegroundColour(Wx::Colour->new(@{ $self->{wrangler}->config()->{'ui.foreground_colour'} })) if $self->{wrangler}->config()->{'ui.foreground_colour'}; | ||
| 23 | |||
| 24 | ## pull-in valueshortcuts for this session | ||
| 25 | $self->{valueshortcuts} = $self->{wrangler}->config()->{'valueshortcuts'}; | ||
| 26 | |||
| 27 | ## get the editor/layout | ||
| 28 | if($self->{wrangler}->config()->{'ui.formeditor'}){ | ||
| 29 | if($self->{wrangler}->config()->{'ui.formeditor.selected'}){ | ||
| 30 | $self->{selected_editor} = $self->{wrangler}->config()->{'ui.formeditor.selected'}; | ||
| 31 | }else{ | ||
| 32 | ($self->{selected_editor}) = keys %{ $self->{wrangler}->config()->{'ui.formeditor'} }; | ||
| 33 | $self->{wrangler}->config()->{'ui.formeditor.selected'} = $self->{selected_editor} if $self->{selected_editor}; | ||
| 34 | } | ||
| 35 | $self->{editors} = $self->{wrangler}->config()->{'ui.formeditor'}; | ||
| 36 | if( $self->{selected_editor} && $self->{editors}->{ $self->{selected_editor} } ){ | ||
| 37 | $self->{layout} = $self->{editors}->{ $self->{selected_editor} }; | ||
| 38 | $self->{field_count} = @{ $self->{layout} }; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | ## add a selector element | ||
| 43 | my $label = Wx::StaticText->new($self, -1, 'Editor: ', wxDefaultPosition, wxDefaultSize); | ||
| 44 | my @editors_hashkeys = keys %{ $self->{editors} }; | ||
| 45 | $self->{editor_selector} = Wx::ComboBox->new($self, -1, ($self->{selected_editor} || ''), wxDefaultPosition, wxDefaultSize, \@editors_hashkeys, wxCB_DROPDOWN); # |wxCB_READONLY | ||
| 46 | for(0 .. $#editors_hashkeys){ | ||
| 47 | $self->{editor_selector}->SetSelection($_) if $editors_hashkeys[$_] eq $self->{selected_editor}; | ||
| 48 | } | ||
| 49 | |||
| 50 | my $select_sizer = Wx::BoxSizer->new(wxHORIZONTAL); | ||
| 51 | $select_sizer->Add( $label, 0, wxTOP, 7 ); | ||
| 52 | $select_sizer->Add( $self->{editor_selector}, 0); | ||
| 53 | |||
| 54 | my $sizer = Wx::BoxSizer->new(wxVERTICAL); | ||
| 55 | $sizer->Add($select_sizer); | ||
| 56 | |||
| 57 | |||
| 58 | ## build the form/dialog | ||
| 59 | my $form_sizer = Wx::FlexGridSizer->new( ($self->{field_count} ? ($self->{field_count} + 1) : 2), 2); | ||
| 60 | $form_sizer->AddGrowableCol(1); # zerobased | ||
| 61 | |||
| 62 | if($self->{selected_editor}){ | ||
| 63 | if($self->{field_count}){ | ||
| 64 | ## add field rows | ||
| 65 | my $ns; | ||
| 66 | foreach my $line (@{$self->{layout}}){ | ||
| 67 | my ($namespace,$key) = split('::',$line); | ||
| 68 | unless($ns && $ns eq $namespace){ | ||
| 69 | $form_sizer->Add( Wx::StaticText->new($self, -1, $namespace, wxDefaultPosition, wxDefaultSize), 0, wxTOP|wxEXPAND, 10 ); | ||
| 70 | $form_sizer->Add( Wx::Panel->new( $self, -1, ) ); | ||
| 71 | $ns = $namespace; | ||
| 72 | } | ||
| 73 | |||
| 74 | ## tell central $wishlist what we are displaying | ||
| 75 | $Wrangler::wishlist->{ $line } = 1; | ||
| 76 | |||
| 77 | my $node_label = Wx::StaticText->new($self, -1, ($key || '(empty)'), wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT, $line); # we use/abuse 'window name' as Data store, for the full key, used by RemoveField() | ||
| 78 | $form_sizer->Add($node_label, 0, wxTOP|wxLEFT, 5 ); | ||
| 79 | ## todo: lookup needed ctrl "renderer" against a wrangler built-in db of metadata fields and how to render them (similar to valuecb) | ||
| 80 | my $node_ctrl = Wx::TextCtrl->new( $self, -1, '', wxDefaultPosition, wxDefaultSize ); | ||
| 81 | $node_ctrl->SetEditable(0); | ||
| 82 | $node_ctrl->SetBackgroundColour( Wx::Colour->new( 222, 222, 222 ) ); | ||
| 83 | $form_sizer->Add($node_ctrl, 0, wxEXPAND); | ||
| 84 | |||
| 85 | $self->{ctrl_lookup}->{$line} = $node_ctrl; | ||
| 86 | |||
| 87 | EVT_TEXT($self, $node_ctrl, \&OnChange ); | ||
| 88 | EVT_CHAR($node_ctrl, \&OnChar ); | ||
| 89 | EVT_RIGHT_UP($node_label, sub { \&OnRightClick(@_,'on_label'); }); | ||
| 90 | } | ||
| 91 | |||
| 92 | ## add ok/cancel buttons | ||
| 93 | $form_sizer->AddSpacer(5); | ||
| 94 | my $btn_sizer = Wx::BoxSizer->new(wxHORIZONTAL); | ||
| 95 | my $btn_save = Wx::Button->new($self, wxID_OK, 'Save'); | ||
| 96 | my $btn_cancel = Wx::Button->new($self, -1, 'Cancel'); | ||
| 97 | $self->{changed_indicator} = Wx::StaticText->new($self, -1, '', wxDefaultPosition, [80,-1], wxALIGN_RIGHT|wxST_NO_AUTORESIZE|wxSUNKEN_BORDER); | ||
| 98 | $btn_sizer->Add($self->{changed_indicator}, 0, wxTOP|wxRIGHT, 5); | ||
| 99 | $btn_sizer->Add($btn_save, 0, wxRIGHT, 2); | ||
| 100 | $btn_sizer->Add($btn_cancel); | ||
| 101 | $form_sizer->Add($btn_sizer, 0, wxTOP|wxALIGN_RIGHT, 10); | ||
| 102 | |||
| 103 | EVT_BUTTON($self, $btn_save, \&OnSave ); | ||
| 104 | EVT_BUTTON($self, $btn_cancel, sub { $self->RePopulate(@_) } ); | ||
| 105 | }else{ | ||
| 106 | my $note = Wx::StaticText->new($self, -1, "No field layout defined.\n\n(Right-click to add fields to this editor)", wxDefaultPosition, wxDefaultSize); | ||
| 107 | $form_sizer->Add($note , 0, wxALL|wxEXPAND, 30 ); | ||
| 108 | EVT_RIGHT_UP($note, sub { \&OnRightClick(@_); }); | ||
| 109 | } | ||
| 110 | }else{ | ||
| 111 | my $note = Wx::StaticText->new($self, -1, "No editor defined.\n\n(Right-click to create a new editor)", wxDefaultPosition, wxDefaultSize); | ||
| 112 | $form_sizer->Add($note, 0, wxALL|wxEXPAND, 30 ); | ||
| 113 | EVT_RIGHT_UP($note, sub { \&OnRightClick(@_); }); | ||
| 114 | } | ||
| 115 | $sizer->Add( $form_sizer, 0, wxGROW); | ||
| 116 | |||
| 117 | $self->SetSizer($sizer); | ||
| 118 | |||
| 119 | ## hook up events | ||
| 120 | Wrangler::PubSub::subscribe('selection.changed', sub { | ||
| 121 | if($_[0] && $_[0] > 1){ | ||
| 122 | # Wrangler::debug("FormEditor: $_[0] files selected: event ignored."); | ||
| 123 | return; | ||
| 124 | } | ||
| 125 | |||
| 126 | $self->Populate(@_); | ||
| 127 | }, __PACKAGE__); | ||
| 128 | |||
| 129 | ## hook up events | ||
| 130 | EVT_COMBOBOX($self, $self->{editor_selector}, sub { $self->SelectEditor($_[0]->{editor_selector}->GetValue()); }); | ||
| 131 | EVT_TEXT_ENTER($self, $self->{editor_selector}, sub { $self->Rename($_[0]->{editor_selector}->GetValue()); }); | ||
| 132 | EVT_CHAR($label, \&OnChar ); | ||
| 133 | EVT_RIGHT_UP($label, sub { \&OnRightClick(@_); }); | ||
| 134 | EVT_CHAR($self, \&OnChar ); | ||
| 135 | EVT_RIGHT_UP($self, sub { \&OnRightClick(@_); }); | ||
| 136 | |||
| 137 | return $self; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub needed_values { | ||
| 141 | my $self = shift; | ||
| 142 | |||
| 143 | $self->{needed_values} = [ | ||
| 144 | { | ||
| 145 | ns => 'Filesystem', | ||
| 146 | label => 'mtime', | ||
| 147 | }, | ||
| 148 | ]; | ||
| 149 | |||
| 150 | return (@{$self->{needed_values}}); | ||
| 151 | } | ||
| 152 | |||
| 153 | sub Populate { | ||
| 154 | my ($editor,$selection_count,$richlist_items,$only_untouched,$no_poll) = @_; | ||
| 155 | Wrangler::debug("FormEditor::Populate: $selection_count, " . ($richlist_items ? "$richlist_items" : '') ); | ||
| 156 | |||
| 157 | $editor->Poll() unless $no_poll; | ||
| 158 | |||
| 159 | $editor->{current_richlist_item} = $richlist_items->[0]; | ||
| 160 | $editor->{current_path} = $richlist_items->[0] ? $richlist_items->[0]->{'Filesystem::Path'} : undef; | ||
| 161 | |||
| 162 | foreach my $line (@{$editor->{layout}}){ | ||
| 163 | my $value = $editor->{current_richlist_item}->{$line}; | ||
| 164 | if($only_untouched){ | ||
| 165 | $editor->{ctrl_lookup}->{$line}->ChangeValue($value || '') unless $editor->{ctrl_lookup}->{$line}->IsModified(); | ||
| 166 | }else{ | ||
| 167 | $editor->{ctrl_lookup}->{$line}->ChangeValue($value || ''); | ||
| 168 | } | ||
| 169 | |||
| 170 | if( $selection_count && $editor->{wrangler}->{fs}->can_mod($line) ){ | ||
| 171 | $editor->{ctrl_lookup}->{$line}->SetEditable(1); | ||
| 172 | $editor->{ctrl_lookup}->{$line}->SetBackgroundColour( wxNullColour ); | ||
| 173 | }else{ | ||
| 174 | $editor->{ctrl_lookup}->{$line}->SetEditable(0); | ||
| 175 | $editor->{ctrl_lookup}->{$line}->SetBackgroundColour( Wx::Colour->new( 222, 222, 222 ) ); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | if( $editor->{changed_indicator} ){ | ||
| 180 | $editor->{changed_indicator}->SetLabel(''); | ||
| 181 | $editor->{changed_indicator}->Update(); | ||
| 182 | $editor->{changed} = 0; | ||
| 183 | } | ||
| 184 | $editor->Refresh(); | ||
| 185 | } | ||
| 186 | |||
| 187 | sub RePopulate { | ||
| 188 | my $editor = shift; | ||
| 189 | Wrangler::debug("FormEditor::RePopulate: "); | ||
| 190 | $editor->Populate(1, [$editor->{current_richlist_item}], undef, 'no_poll'); | ||
| 191 | } | ||
| 192 | |||
| 193 | sub Poll { | ||
| 194 | my $editor = shift; | ||
| 195 | |||
| 196 | my @modified; | ||
| 197 | foreach my $line (@{$editor->{layout}}){ | ||
| 198 | push(@modified, { | ||
| 199 | key => $line, | ||
| 200 | value => $editor->{ctrl_lookup}->{$line}->GetValue(), | ||
| 201 | }) if $editor->{ctrl_lookup}->{$line}->IsModified(); | ||
| 202 | } | ||
| 203 | |||
| 204 | if(@modified){ | ||
| 205 | Wrangler::debug('FormEditor::Poll: editor has been modified:'); | ||
| 206 | foreach my $mod (@modified){ | ||
| 207 | if( $mod->{value} eq '' ){ ## FormEditor removes on empty | ||
| 208 | Wrangler::debug(" deleting '$mod->{key}': '$mod->{value}' on $editor->{current_path}"); | ||
| 209 | my $ok = $editor->{wrangler}->{fs}->del_property($editor->{current_path}, $mod->{key}); | ||
| 210 | delete($editor->{current_richlist_item}->{ $mod->{key} }); # update internal data structure as well | ||
| 211 | }else{ | ||
| 212 | Wrangler::debug(" setting '$mod->{key}': '$mod->{value}' on $editor->{current_path}"); | ||
| 213 | my $ok = $editor->{wrangler}->{fs}->set_property($editor->{current_path}, $mod->{key}, $mod->{value}); | ||
| 214 | $editor->{current_richlist_item}->{ $mod->{key} } = $mod->{value}; # update internal data structure as well | ||
| 215 | } | ||
| 216 | } | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | sub OnChange { | ||
| 221 | my $self = shift; | ||
| 222 | |||
| 223 | unless($self->{changed}){ # not every keystroke should trigger SetLabel | ||
| 224 | $self->{changed_indicator}->SetLabel('Changed...'); | ||
| 225 | $self->{changed} = 1; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | sub OnSave { | ||
| 230 | $_[0]->Populate(1, [$_[0]->{current_richlist_item}]); | ||
| 231 | } | ||
| 232 | |||
| 233 | sub OnDeselected { | ||
| 234 | shift->Poll(); | ||
| 235 | } | ||
| 236 | |||
| 237 | sub get_field { | ||
| 238 | my ($layout, $ns, $key) = @_; | ||
| 239 | |||
| 240 | ## the downside of having an array for fields... | ||
| 241 | foreach my $field (@$layout){ | ||
| 242 | if($field->{ns} eq $ns && $field->{key} eq $key){ | ||
| 243 | return $field; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | return {}; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub Create { | ||
| 251 | my $editor = shift; | ||
| 252 | |||
| 253 | my $dialog = Wx::TextEntryDialog->new( $editor, "New editor layout name", "New editor layout name", ""); | ||
| 254 | |||
| 255 | unless( $dialog->ShowModal == wxID_CANCEL ){ | ||
| 256 | my $new_editor = $dialog->GetValue(); | ||
| 257 | if( $new_editor ne '' && !defined($editor->{editors}->{ $new_editor }) ){ | ||
| 258 | # $editor->{editors}->{ $new_editor } = []; | ||
| 259 | $editor->{wrangler}->config()->{'ui.formeditor'}->{ $new_editor } = []; | ||
| 260 | $editor->{wrangler}->config()->{'ui.formeditor.selected'} = $new_editor; | ||
| 261 | |||
| 262 | Wrangler::PubSub::publish('main.formeditor.recreate'); | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | $dialog->Destroy(); | ||
| 267 | } | ||
| 268 | |||
| 269 | sub Rename { | ||
| 270 | my $editor = shift; | ||
| 271 | my $editor_name = $editor->{wrangler}->config()->{'ui.formeditor.selected'}; | ||
| 272 | my $new_editor_name = shift; | ||
| 273 | |||
| 274 | unless(defined($new_editor_name)){ | ||
| 275 | my $dialog = Wx::TextEntryDialog->new( $editor, "Rename editor", "Rename editor", $editor_name); | ||
| 276 | return if $dialog->ShowModal == wxID_CANCEL; | ||
| 277 | |||
| 278 | $new_editor_name = $dialog->GetValue(); | ||
| 279 | $dialog->Destroy(); | ||
| 280 | } | ||
| 281 | |||
| 282 | if( $new_editor_name eq ''){ | ||
| 283 | my $dialog = Wx::MessageDialog->new($editor, "Nothing. That's not a very helpful name", "Oops...", wxOK ); | ||
| 284 | $dialog->ShowModal(); | ||
| 285 | }elsif($editor_name eq $new_editor_name){ | ||
| 286 | # nothing | ||
| 287 | }elsif(defined($editor->{editors}->{ $new_editor_name }) ){ | ||
| 288 | my $dialog = Wx::MessageDialog->new($editor, "Editor names have to be unique", "Oops...", wxOK ); | ||
| 289 | $dialog->ShowModal(); | ||
| 290 | }else{ | ||
| 291 | $editor->{wrangler}->config()->{'ui.formeditor'}->{ $new_editor_name } = $editor->{wrangler}->config()->{'ui.formeditor'}->{ $editor_name }; | ||
| 292 | delete($editor->{wrangler}->config()->{'ui.formeditor'}->{ $editor_name }); | ||
| 293 | $editor->{wrangler}->config()->{'ui.formeditor.selected'} = $new_editor_name; | ||
| 294 | |||
| 295 | Wrangler::PubSub::publish('main.formeditor.recreate'); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | |||
| 299 | sub Delete { | ||
| 300 | my $editor = shift; | ||
| 301 | my $editor_name = $editor->{wrangler}->config()->{'ui.formeditor.selected'}; | ||
| 302 | |||
| 303 | my $dialog = Wx::MessageDialog->new($editor, "Really delete editor '$editor_name'?", "Confirm", wxYES_NO | wxNO_DEFAULT | wxICON_EXCLAMATION ); | ||
| 304 | |||
| 305 | if($dialog->ShowModal() == wxID_YES){ | ||
| 306 | print "Delete: $editor_name \n"; | ||
| 307 | # delete($editor->{wrangler}->config()->{'ui.formeditor'}->{ $editor_name }); | ||
| 308 | delete($editor->{editors}->{ $editor_name }); | ||
| 309 | $editor->{wrangler}->config()->{'ui.formeditor.selected'} = undef; | ||
| 310 | |||
| 311 | Wrangler::PubSub::publish('main.formeditor.recreate'); | ||
| 312 | } | ||
| 313 | |||
| 314 | $dialog->Destroy(); | ||
| 315 | } | ||
| 316 | |||
| 317 | # compare Add() in Wrangler::Wx::Dialog::Settings::FileBrowser | ||
| 318 | sub AddField { | ||
| 319 | my $editor = shift; | ||
| 320 | my $field_name = shift; | ||
| 321 | my $editor_name = $editor->{wrangler}->config()->{'ui.formeditor.selected'}; | ||
| 322 | |||
| 323 | unless($field_name){ | ||
| 324 | my $dialog = Wx::TextEntryDialog->new( $editor, "Add this field to editor", "Add this field to editor", ""); | ||
| 325 | |||
| 326 | $field_name = $dialog->GetValue() unless $dialog->ShowModal == wxID_CANCEL; | ||
| 327 | |||
| 328 | $dialog->Destroy(); | ||
| 329 | } | ||
| 330 | |||
| 331 | if( $field_name ne '' && $field_name !~ @{ $editor->{wrangler}->config()->{'ui.formeditor'}->{ $editor_name } } ){ | ||
| 332 | push(@{ $editor->{wrangler}->config()->{'ui.formeditor'}->{ $editor_name } }, $field_name); | ||
| 333 | |||
| 334 | # update $wishlist | ||
| 335 | $Wrangler::wishlist->{ $field_name } = 1; | ||
| 336 | |||
| 337 | Wrangler::PubSub::publish('main.formeditor.recreate'); | ||
| 338 | } | ||
| 339 | } | ||
| 340 | |||
| 341 | sub RemoveField { | ||
| 342 | my $editor = shift; | ||
| 343 | my $field_name = shift; | ||
| 344 | my $editor_name = $editor->{wrangler}->config()->{'ui.formeditor.selected'}; | ||
| 345 | |||
| 346 | my $i=0; | ||
| 347 | for(@{ $editor->{wrangler}->config()->{'ui.formeditor'}->{ $editor_name } }){ | ||
| 348 | last if $_ eq $field_name; | ||
| 349 | $i++; | ||
| 350 | } | ||
| 351 | # print "RemoveField: $field_name -> i:$i\n"; | ||
| 352 | |||
| 353 | splice(@{ $editor->{wrangler}->config()->{'ui.formeditor'}->{ $editor_name } },$i,1); # remove pos from array | ||
| 354 | Wrangler::PubSub::publish('main.formeditor.recreate'); | ||
| 355 | } | ||
| 356 | |||
| 357 | sub SelectEditor { | ||
| 358 | my $editor = shift; | ||
| 359 | |||
| 360 | $editor->{wrangler}->config()->{'ui.formeditor.selected'} = shift; | ||
| 361 | |||
| 362 | Wrangler::PubSub::publish('main.formeditor.recreate'); | ||
| 363 | } | ||
| 364 | |||
| 365 | sub SaveFieldLayout { | ||
| 366 | my $editor = shift; | ||
| 367 | my $editor_name = $editor->{wrangler}->config()->{'ui.formeditor.selected'}; | ||
| 368 | |||
| 369 | Wrangler::debug("FormEditor::SaveFieldLayout"); | ||
| 370 | my $file_dialog = Wx::FileDialog->new($editor, "Save field layout", '', $editor_name.'.wfl', "Wrangler Field Layout (*.wfl)|*.wfl;All files (*.*)|*.*", wxFD_SAVE); | ||
| 371 | |||
| 372 | return if $file_dialog->ShowModal == wxID_CANCEL; | ||
| 373 | |||
| 374 | my $path = $file_dialog->GetPath; | ||
| 375 | $file_dialog->Destroy; | ||
| 376 | |||
| 377 | my $json = eval { JSON::XS->new->utf8->pretty->encode( { $editor_name => $editor->{editors}->{ $editor_name } } ) }; | ||
| 378 | Wrangler::debug("Wrangler::Wx::FormEditor::SaveFieldLayout: error encoding fields: $@") if $@; | ||
| 379 | |||
| 380 | path($path)->spew_utf8(\$json) or Wrangler::debug("Wrangler::Wx::FormEditor::SaveFieldLayout: error writing layout file: $path: $!") | ||
| 381 | } | ||
| 382 | |||
| 383 | sub LoadFieldLayout { | ||
| 384 | my $editor = shift; | ||
| 385 | my $editor_name = $editor->{wrangler}->config()->{'ui.formeditor.selected'}; | ||
| 386 | |||
| 387 | Wrangler::debug("FormEditor::LoadFieldLayout"); | ||
| 388 | my $file_dialog = Wx::FileDialog->new($editor, "Load field layout", '', '', "Wrangler Field Layout (*.wfl)|*.wfl;|All files (*.*)|*.*", wxFD_OPEN); | ||
| 389 | |||
| 390 | return if $file_dialog->ShowModal == wxID_CANCEL; | ||
| 391 | |||
| 392 | my $path = $file_dialog->GetPath; | ||
| 393 | $file_dialog->Destroy; | ||
| 394 | |||
| 395 | my $json = path($path)->slurp_utf8 or Wrangler::debug("Wrangler::Wx::FormEditor::LoadFieldLayout: error reading layout file: $!"); | ||
| 396 | my $ref = eval { JSON::XS::decode_json( $json ) }; | ||
| 397 | Wrangler::debug("Wrangler::Wx::FormEditor::LoadFieldLayout: error decoding layout file: $@") if $@; | ||
| 398 | |||
| 399 | my $last; | ||
| 400 | for(keys %$ref){ | ||
| 401 | unless(defined($editor->{editors}->{ $_ })){ | ||
| 402 | Wrangler::debug("FormEditor::LoadFieldLayout: adding layout $_"); | ||
| 403 | $editor->{editors}->{ $_ } = $ref->{$_} ; | ||
| 404 | $last = $_; | ||
| 405 | } | ||
| 406 | } | ||
| 407 | |||
| 408 | $editor->{wrangler}->config()->{'ui.formeditor.selected'} = $last; | ||
| 409 | |||
| 410 | Wrangler::PubSub::publish('main.formeditor.recreate'); | ||
| 411 | } | ||
| 412 | |||
| 413 | sub OnChar { | ||
| 414 | my( $editor, $event ) = @_; | ||
| 415 | # OnChar usually happens in TextCtrls | ||
| 416 | my $element; | ||
| 417 | unless($editor->isa('Wrangler::Wx::FormEditor')){ | ||
| 418 | $element = $editor; | ||
| 419 | $editor = $element->GetParent(); | ||
| 420 | } | ||
| 421 | |||
| 422 | my $mod = $event->GetModifiers || 0; | ||
| 423 | my $keycode = $event->GetKeyCode(); | ||
| 424 | |||
| 425 | # Wrangler::debug('FormEditor::OnChar: mod:'.$mod .', code:'. $keycode); | ||
| 426 | |||
| 427 | if($keycode == WXK_UP){ | ||
| 428 | Wrangler::debug('FormEditor::OnChar: Arrow Up'); | ||
| 429 | |||
| 430 | # emit appropriate event | ||
| 431 | Wrangler::PubSub::publish('filebrowser.selection_move.up',$event); | ||
| 432 | # $editor->SetFocus(); | ||
| 433 | }elsif($keycode == WXK_DOWN){ | ||
| 434 | Wrangler::debug('FormEditor::OnChar: Arrow Down'); | ||
| 435 | |||
| 436 | # emit appropriate event | ||
| 437 | Wrangler::PubSub::publish('filebrowser.selection_move.down',$event); | ||
| 438 | # $editor->SetFocus(); | ||
| 439 | }else{ | ||
| 440 | if($editor->{valueshortcuts}){ | ||
| 441 | if(my $shortcut = $editor->{valueshortcuts}->{$mod.'-'.$keycode}){ | ||
| 442 | Wrangler::debug("FormEditor::OnChar: ValueShortcut for $shortcut->{name}"); | ||
| 443 | if( $editor->{ctrl_lookup}->{ $shortcut->{key} } ){ | ||
| 444 | Wrangler::debug(" inser into $shortcut->{key}"); | ||
| 445 | $editor->{ctrl_lookup}->{ $shortcut->{key} }->ChangeValue( $shortcut->{value} ); | ||
| 446 | $editor->{ctrl_lookup}->{ $shortcut->{key} }->MarkDirty(); | ||
| 447 | }else{ | ||
| 448 | if($element && ref($element) =~ /::TextCtrl/){ | ||
| 449 | Wrangler::debug(" inser into current element $element"); | ||
| 450 | $element->ChangeValue( $shortcut->{value} ); | ||
| 451 | $element->MarkDirty(); | ||
| 452 | } | ||
| 453 | } | ||
| 454 | } | ||
| 455 | } | ||
| 456 | } | ||
| 457 | $event->Skip(1); | ||
| 458 | } | ||
| 459 | |||
| 460 | sub OnRightClick { | ||
| 461 | my( $editor, $event ) = @_; | ||
| 462 | # clicks may be on StaticText elements | ||
| 463 | my $click_element; | ||
| 464 | unless($editor->isa('Wrangler::Wx::FormEditor')){ | ||
| 465 | $click_element = shift; | ||
| 466 | $editor = $click_element->GetParent(); | ||
| 467 | } | ||
| 468 | |||
| 469 | my $menu = Wx::Menu->new(); | ||
| 470 | |||
| 471 | if($click_element){ | ||
| 472 | EVT_MENU( $editor, $menu->Append(-1, "Remove field", 'Remove/hide this metadata key/value pair' ), sub { $editor->RemoveField($click_element->GetName()); } ); | ||
| 473 | $menu->AppendSeparator(); | ||
| 474 | } | ||
| 475 | |||
| 476 | if($editor->{selected_editor}){ | ||
| 477 | # if($editor->{field_count}){ | ||
| 478 | my $submenu = Wx::Menu->new(); | ||
| 479 | for( @{ $editor->{wrangler}->{fs}->available_properties($editor->{wrangler}->{current_dir}) } ){ | ||
| 480 | my $item = $submenu->Append(-1, $_); | ||
| 481 | $submenu->Enable($item->GetId(),0) if $editor->{ctrl_lookup}->{$_}; | ||
| 482 | EVT_MENU( $editor, $item, sub { $editor->AddField($item->GetText()); } ); # deprecated: use GetItemLabel text soon | ||
| 483 | } | ||
| 484 | $submenu->AppendSeparator(); | ||
| 485 | my $item = $submenu->Append(-1, 'Other...'); | ||
| 486 | EVT_MENU( $editor, $item, sub { $editor->AddField(); } ); | ||
| 487 | # } | ||
| 488 | $menu->Append(-1, "Add fields...", $submenu, 'Add a metadata key/value pair' ); | ||
| 489 | $menu->AppendSeparator(); | ||
| 490 | } | ||
| 491 | |||
| 492 | EVT_MENU( $editor, $menu->Append(-1, "Add editor...", 'Create a new editor layout'), sub { $editor->Create(); } ); | ||
| 493 | if(ref($editor->{editors})){ | ||
| 494 | if(keys(%{$editor->{editors}}) > 1){ | ||
| 495 | my $submenu = Wx::Menu->new(); | ||
| 496 | for( keys %{ $editor->{editors} } ){ | ||
| 497 | my $item = $submenu->Append(-1, $_); | ||
| 498 | $submenu->Enable($item->GetId(),0) if $_ eq $editor->{selected_editor}; | ||
| 499 | EVT_MENU( $editor, $item, sub { $editor->SelectEditor($item->GetText()); } ); # deprecated: use GetItemLabel text soon | ||
| 500 | } | ||
| 501 | $menu->Append(-1, "Switch editor...", $submenu, 'Change displayed editor'); | ||
| 502 | } | ||
| 503 | EVT_MENU( $editor, $menu->Append(-1, "Rename editor", 'Rename this editor layout'), sub { $editor->Rename(); } ); | ||
| 504 | EVT_MENU( $editor, $menu->Append(-1, "Delete editor", 'Delete this editor layout'), sub { $editor->Delete(); } ); | ||
| 505 | } | ||
| 506 | $menu->AppendSeparator(); | ||
| 507 | |||
| 508 | EVT_MENU( $editor, $menu->Append(-1, "Load field layout", 'Load a field layout from a file'), sub { $editor->LoadFieldLayout(); } ); | ||
| 509 | my $itemSave = Wx::MenuItem->new($menu, -1, "Save field layout", "Save this field layout to a file"); | ||
| 510 | $menu->Append($itemSave); | ||
| 511 | |||
| 512 | if($editor->{field_count} > 1){ | ||
| 513 | $menu->Enable($itemSave->GetId(),1); | ||
| 514 | EVT_MENU( $editor, $itemSave, sub { $editor->SaveFieldLayout(); } ); | ||
| 515 | }else{ | ||
| 516 | $menu->Enable($itemSave->GetId(),0); | ||
| 517 | } | ||
| 518 | $menu->AppendSeparator(); | ||
| 519 | |||
| 520 | EVT_MENU( $editor, $menu->Append(-1, "Settings", 'Settings'), sub { Wrangler::PubSub::publish('show.settings', 0, 1); } ); # send to "Value Shortcuts" until FormEditor has its own Settings | ||
| 521 | |||
| 522 | $editor->PopupMenu( $menu, wxDefaultPosition ); | ||
| 523 | } | ||
| 524 | |||
| 525 | sub Destroy { | ||
| 526 | my $self = shift; | ||
| 527 | |||
| 528 | Wrangler::PubSub::unsubscribe_owner(__PACKAGE__); | ||
| 529 | |||
| 530 | # $self->SUPER::Destroy(); # crash | ||
| 531 | } | ||
| 532 | |||
| 533 | 1; | ||
| 534 | |||
| 535 | __END__ |