xml-copy-editor-code/src/commandpanel.cpp

282 lines
9.0 KiB
C++
Raw Normal View History

/*
* Copyright 2005-2007 Gerald Schmidt.
*
* This file is part of Xml Copy Editor.
*
* Xml Copy Editor is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* Xml Copy Editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xml Copy Editor; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
2007-09-07 23:17:30 +02:00
#include <wx/utils.h>
#include "commandpanel.h"
#include "xmlcopyeditor.h"
#include "wraptempfilename.h"
#include "readfile.h"
2007-09-08 00:25:30 +02:00
BEGIN_EVENT_TABLE ( CommandPanel, wxPanel )
EVT_BUTTON ( ID_RUN, CommandPanel::OnRun )
EVT_IDLE ( CommandPanel::OnIdle )
EVT_BUTTON ( ID_BUTTON_NAME, CommandPanel::OnVariableButton )
EVT_BUTTON ( ID_BUTTON_PATH, CommandPanel::OnVariableButton )
EVT_BUTTON ( ID_BUTTON_EXTENSION, CommandPanel::OnVariableButton )
EVT_BUTTON ( ID_BUTTON_FULLPATH, CommandPanel::OnVariableButton )
2007-09-07 23:17:30 +02:00
END_EVENT_TABLE()
2007-09-08 00:25:30 +02:00
CommandPanel::CommandPanel (
wxWindow *parentParameter,
int id,
const wxString& cmd,
bool sync,
int output,
const wxString& command ) : wxPanel ( parentParameter, id )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
parent = ( MyFrame * ) parentParameter;
path = _ ( "{path}" );
name = _ ( "{name}" );
extension = _ ( "{extension}" );
fullpath = _ ( "{fullpath}" );
wxButton *pathButton = new wxButton ( this, ID_BUTTON_PATH, path, wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT | wxNO_BORDER );
wxButton *nameButton = new wxButton ( this, ID_BUTTON_NAME, name, wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT | wxNO_BORDER );
wxButton *extensionButton = new wxButton ( this, ID_BUTTON_EXTENSION, extension, wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT | wxNO_BORDER );
wxButton *fullpathButton = new wxButton ( this, ID_BUTTON_FULLPATH, fullpath, wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT | wxNO_BORDER );
int editWidth = 480;
commandEdit = new wxTextCtrl (
this,
wxID_ANY,
_T ( "" ),
wxDefaultPosition,
wxSize ( editWidth, -1 )
);
commandEdit->SetValue ( cmd );
runButton = new wxButton (
this,
ID_RUN,
_ ( "&Run" ),
wxDefaultPosition,
wxDefaultSize,
wxBU_EXACTFIT | wxNO_BORDER );
syncBox = new wxCheckBox (
this,
ID_SYNC,
_ ( "&Wait" ) );
syncBox->SetValue ( sync );
wxStaticBox *outputBox = new wxStaticBox (
this,
wxID_ANY,
_ ( "Output options" ) );
outputIgnore = new wxRadioButton (
this,
ID_COMMAND_OUTPUT_IGNORE,
_ ( "I&gnore" ) );
outputIgnore->SetValue ( true );
outputInsert = new wxRadioButton (
this,
ID_COMMAND_OUTPUT_IGNORE,
_ ( "I&nsert" ) );
outputNewDocument = new wxRadioButton (
this,
ID_COMMAND_OUTPUT_IGNORE,
_ ( "New &document" ) );
switch ( output )
{
2007-09-07 23:17:30 +02:00
case ID_COMMAND_OUTPUT_IGNORE:
2007-09-08 00:25:30 +02:00
outputIgnore->SetValue ( true );
break;
2007-09-07 23:17:30 +02:00
case ID_COMMAND_OUTPUT_INSERT:
2007-09-08 00:25:30 +02:00
outputInsert->SetValue ( true );
break;
case ID_COMMAND_OUTPUT_NEW_DOCUMENT:
outputNewDocument->SetValue ( true );
2007-09-07 23:17:30 +02:00
break;
2007-09-08 00:25:30 +02:00
default:
outputIgnore->SetValue ( true );
break;
}
wxStaticBox *variablesBox = new wxStaticBox (
this,
wxID_ANY,
_ ( "Variables" ) );
int sizerOffset = 2;
wxStaticBoxSizer *outputSizer = new wxStaticBoxSizer ( outputBox, wxHORIZONTAL );
outputSizer->Add ( outputIgnore, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
outputSizer->Add ( outputInsert, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
outputSizer->Add ( outputNewDocument, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
wxStaticBoxSizer *variablesSizer = new wxStaticBoxSizer ( variablesBox, wxHORIZONTAL );
//variablesSizer->Add(label, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset);
variablesSizer->Add ( pathButton, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
variablesSizer->Add ( nameButton, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
variablesSizer->Add ( extensionButton, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
variablesSizer->Add ( fullpathButton, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
topSizer = new wxBoxSizer ( wxHORIZONTAL );
topSizer->Add ( commandEdit, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL | wxEXPAND, sizerOffset );
topSizer->Add ( runButton, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
topSizer->Add ( syncBox, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
topSizer->Layout();
bottomSizer = new wxBoxSizer ( wxHORIZONTAL );
bottomSizer->Add ( variablesSizer, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
bottomSizer->Add ( outputSizer, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
bottomSizer->Layout();
mainSizer = new wxBoxSizer ( wxVERTICAL );
mainSizer->Add ( topSizer );
mainSizer->Add ( bottomSizer );
mainSizer->Layout();
this->SetSizer ( mainSizer );
this->SetSize ( -1, ( runButton->GetSize().GetHeight() * 2 ) + 25 );
2007-09-07 23:17:30 +02:00
}
CommandPanel::~CommandPanel()
2007-09-08 00:25:30 +02:00
{}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
void CommandPanel::OnRun ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
parent->statusProgress ( wxEmptyString );
wxString command = commandEdit->GetValue();
if ( command.empty() )
return;
// need to test for NULL ptr doc for each use
XmlDoc *doc = parent->getActiveDocument();
wxString fullPath = ( doc ) ? doc->getFullFileName() : _T ( "" );
wxString path, name, extension;
wxFileName::SplitPath ( fullPath, &path, &name, &extension );
command.Replace ( _ ( "{fullpath}" ), fullPath, true );
command.Replace ( _ ( "{path}" ), path, true );
command.Replace ( _ ( "{name}" ), name, true );
command.Replace ( _ ( "{extension}" ), extension, true );
bool redirect = ( getSync() && !outputIgnore->GetValue() );
if ( !redirect )
{
wxExecute ( command, ( getSync() ) ? wxEXEC_SYNC : wxEXEC_ASYNC );
return;
}
wxArrayString stringArray;
wxExecute ( command, stringArray, wxEXEC_NOHIDE );
if ( stringArray.empty() )
{
parent->statusProgress ( _T ( "No output" ) );
return;
}
size_t count = stringArray.GetCount();
wxString outputBuffer;
for ( size_t i = 0 ; i < count; i++ )
{
outputBuffer += stringArray[i];
outputBuffer += L"\n";
}
if ( outputInsert->GetValue() )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( doc )
{
doc->ReplaceSelection ( outputBuffer );
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
else if ( outputNewDocument->GetValue() )
parent->newDocument ( outputBuffer );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
XmlDoc *newActiveDoc = parent->getActiveDocument();
if ( newActiveDoc )
newActiveDoc->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void CommandPanel::OnVariableButton ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
int id = event.GetId();
wxString label;
switch ( id )
{
2007-09-07 23:17:30 +02:00
case ID_BUTTON_NAME:
2007-09-08 00:25:30 +02:00
label = name;
break;
2007-09-07 23:17:30 +02:00
case ID_BUTTON_PATH:
2007-09-08 00:25:30 +02:00
label = path;
break;
2007-09-07 23:17:30 +02:00
case ID_BUTTON_EXTENSION:
2007-09-08 00:25:30 +02:00
label = extension;
break;
2007-09-07 23:17:30 +02:00
case ID_BUTTON_FULLPATH:
2007-09-08 00:25:30 +02:00
label = fullpath;
break;
2007-09-07 23:17:30 +02:00
default:
2007-09-08 00:25:30 +02:00
label = wxEmptyString;
return;
}
long to, from;
commandEdit->GetSelection ( &from, &to );
commandEdit->Replace ( from, to, label );
commandEdit->SetFocus();
2007-09-07 23:17:30 +02:00
}
void CommandPanel::focusOnCommand()
{
2007-09-08 00:25:30 +02:00
commandEdit->SelectAll();
commandEdit->SetFocus();
2007-09-07 23:17:30 +02:00
}
bool CommandPanel::getSync()
{
2007-09-08 00:25:30 +02:00
return syncBox->GetValue();
2007-09-07 23:17:30 +02:00
}
int CommandPanel::getOutput()
{
2007-09-08 00:25:30 +02:00
if ( outputIgnore->GetValue() )
return ID_COMMAND_OUTPUT_IGNORE;
else if ( outputInsert->GetValue() )
return ID_COMMAND_OUTPUT_INSERT;
else if ( outputNewDocument->GetValue() )
return ID_COMMAND_OUTPUT_NEW_DOCUMENT;
else
return ID_COMMAND_OUTPUT_IGNORE; // default
2007-09-07 23:17:30 +02:00
}
wxString CommandPanel::getCommand()
{
2007-09-08 00:25:30 +02:00
return commandEdit->GetValue();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void CommandPanel::OnIdle ( wxIdleEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
bool b = getSync();
outputIgnore->Enable ( b );
outputInsert->Enable ( b );
outputNewDocument->Enable ( b );
2007-09-07 23:17:30 +02:00
}