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

247 lines
6.3 KiB
C++
Raw Normal View History

2007-09-07 23:17:30 +02:00
#include "insertpanel.h"
2007-09-08 00:25:30 +02:00
BEGIN_EVENT_TABLE ( InsertPanel, wxPanel )
EVT_TEXT_ENTER ( wxID_ANY, InsertPanel::OnEnter )
EVT_LISTBOX_DCLICK ( wxID_ANY, InsertPanel::OnDoubleClick )
EVT_LISTBOX ( wxID_ANY, InsertPanel::OnListSelection )
EVT_SIZE ( InsertPanel::OnSize )
2007-09-07 23:17:30 +02:00
END_EVENT_TABLE()
2007-09-08 00:25:30 +02:00
InsertPanel::InsertPanel (
wxWindow *parentWindowParameter,
int id,
int typeParameter ) :
wxPanel ( parentWindowParameter, id ), type ( typeParameter ), edit ( 0 ), list ( 0 )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
parentWindow = ( MyFrame * ) parentWindowParameter;
doc = lastDoc = NULL;
int width = 150;
SetSize ( wxSize ( width, -1 ) );
sizer = new wxBoxSizer ( wxVERTICAL );
SetSizer ( sizer );
edit = new wxTextCtrl (
this,
wxID_ANY,
wxEmptyString,
wxDefaultPosition,
wxDefaultSize,
wxTE_PROCESS_ENTER );
wxFont normalFont = wxSystemSettings::GetFont ( wxSYS_DEFAULT_GUI_FONT );
wxFont boldFont = normalFont;
boldFont.SetWeight ( wxFONTWEIGHT_BOLD );
edit->SetFont ( boldFont );
list = new wxListBox (
this,
wxID_ANY,
wxDefaultPosition,
wxDefaultSize,
0,
NULL,
wxLB_SORT | wxLB_HSCROLL );
sizer->Add ( edit, 0, wxGROW | wxTOP, 0 );
sizer->Add ( list, 0, wxGROW | wxTOP, 0 );
sizer->Layout();
list->Show ( false );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void InsertPanel::update (
XmlDoc *docParameter,
const wxString& parentParameter,
const wxString& grandparentParameter )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
doc = docParameter;
parent = parentParameter;
grandparent = grandparentParameter;
if ( !doc )
{
if ( lastDoc )
{
lastDoc = NULL;
edit->SetValue ( wxEmptyString );
list->Clear();
list->Show ( false );
}
return;
}
bool refreshEntities = false;
if ( doc != lastDoc )
{
refreshEntities = true;
lastDoc = doc;
}
if ( type == INSERT_PANEL_TYPE_ENTITY && refreshEntities )
{
list->Clear();
lastDoc = doc;
std::set<std::string> entitySet = doc->getEntitySet();
entitySet.insert ( "apos" );
entitySet.insert ( "gt" );
entitySet.insert ( "lt" );
entitySet.insert ( "quot" );
std::set<std::string>::iterator it;
for ( it = entitySet.begin(); it != entitySet.end(); it++ )
list->Append ( wxString ( it->c_str(), wxConvUTF8, it->size() ) );
list->Show ( true );
wxSize clientSize = GetClientSize();
wxSize editSize = edit->GetSize();
wxSize listSize =
wxSize ( clientSize.GetWidth(), clientSize.GetHeight() - editSize.GetHeight() );
list->SetSize ( listSize );
list->Update();
//sizer->Layout();
return;
}
if ( parent == lastParent )
return;
lastParent = parent;
if ( type == INSERT_PANEL_TYPE_CHILD ) // ignore for entity/sibling
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
doc->toggleLineBackground();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
edit->SetValue ( wxEmptyString );
2007-09-07 23:17:30 +02:00
list->Clear();
2007-09-08 00:25:30 +02:00
if ( parent.empty() || ( ( type == INSERT_PANEL_TYPE_SIBLING ) && grandparent.empty() ) )
{
list->Show ( false );
return;
}
std::set<wxString> elementSet;
elementSet = doc->getChildren ( ( type == INSERT_PANEL_TYPE_SIBLING ) ? grandparent : parent );
if ( elementSet.empty() )
{
list->Show ( false );
return;
}
std::set<wxString>::iterator it;
for ( it = elementSet.begin(); it != elementSet.end(); it++ )
list->Append ( *it );
list->Show ( true );
2007-09-07 23:17:30 +02:00
wxSize clientSize = GetClientSize();
wxSize editSize = edit->GetSize();
2007-09-08 00:25:30 +02:00
wxSize listSize =
wxSize ( clientSize.GetWidth(), clientSize.GetHeight() - editSize.GetHeight() );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( clientSize.IsFullySpecified() && editSize.IsFullySpecified() )
list->SetSize ( listSize );
list->Update();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void InsertPanel::OnEnter ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !doc )
return;
wxString choice = edit->GetValue();
if ( choice.empty() )
doc->SetFocus();
else
handleChoice ( choice );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void InsertPanel::OnDoubleClick ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !doc )
return;
if ( !doc )
return;
wxString choice = list->GetStringSelection();
handleChoice ( choice );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void InsertPanel::handleChoice ( const wxString& choice )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !doc || choice.empty() )
return;
if ( parentWindow )
parentWindow->closePane();
switch ( type )
{
2007-09-07 23:17:30 +02:00
case INSERT_PANEL_TYPE_SIBLING:
2007-09-08 00:25:30 +02:00
if ( !parent.empty() )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( !doc->insertSibling ( choice, parent ) && parentWindow )
{
wxString msg;
msg.Printf ( _T ( "Cannot insert sibling '%s'" ), choice.c_str() );
parentWindow->messagePane ( msg, CONST_STOP );
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
break;
2007-09-07 23:17:30 +02:00
case INSERT_PANEL_TYPE_CHILD:
2007-09-08 00:25:30 +02:00
if ( !doc->insertChild ( choice ) && parentWindow )
{
wxString msg;
msg.Printf ( _T ( "Cannot insert child '%s'" ), choice.c_str() );
parentWindow->messagePane ( msg, CONST_STOP );
}
break;
2007-09-07 23:17:30 +02:00
case INSERT_PANEL_TYPE_ENTITY:
2007-09-08 00:25:30 +02:00
if ( !doc->insertEntity ( choice ) && parentWindow )
{
wxString msg;
msg.Printf ( _T ( "Cannot insert entity '%s'" ), choice.c_str() );
parentWindow->messagePane ( msg, CONST_STOP );
}
break;
2007-09-07 23:17:30 +02:00
default:
2007-09-08 00:25:30 +02:00
break;
}
doc->setValidationRequired ( true );
doc->SetFocus();
2007-09-07 23:17:30 +02:00
}
void InsertPanel::setEditFocus()
{
2007-09-08 00:25:30 +02:00
if ( !edit )
return;
edit->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void InsertPanel::OnListSelection ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
edit->SetValue ( list->GetStringSelection() );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void InsertPanel::OnSize ( wxSizeEvent& e )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
adjustSize();
e.Skip();
2007-09-07 23:17:30 +02:00
}
void InsertPanel::adjustSize()
{
2007-09-08 00:25:30 +02:00
if ( !list || !edit )
return;
wxSize clientSize = GetClientSize();
wxSize editSize = edit->GetSize();
if ( !clientSize.IsFullySpecified() || !editSize.IsFullySpecified() )
return;
wxSize listSize =
wxSize ( clientSize.GetWidth(), clientSize.GetHeight() - editSize.GetHeight() );
if ( listSize.IsFullySpecified() )
list->SetSizeHints ( listSize, listSize, listSize );
2007-09-07 23:17:30 +02:00
}