⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propeditor.cpp

📁 Wxpython Implemented on Windows CE, Source code
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        if (row < (int) m_item->GetProperties().GetCount())
        {
            ctProperty* prop = m_item->GetProperties().GetNth(row);
            return prop;
        }
    }
    return NULL;
}

/// Edit the details of this cell appropriately.
bool ctPropertyEditor::EditDetails(wxWindow* WXUNUSED(parent))
{
    if (CanEditDetails())
    {
        int row;
        ctProperty* prop = FindSelectedProperty(row);
        if (!prop)
            return false;

        wxString type(prop->GetEditorType());
        wxString value = m_attributeEditorGrid->GetCellValue(row, 1);

        if (type == _T("multiline"))
        {
            value = ctConvertToMultilineText(value);
            wxString msg;
            msg.Printf(wxT("Edit %s:"), (const wxChar*) prop->GetName());
            ctMultiLineTextEditor dialog(wxTheApp->GetTopWindow(),
                wxID_ANY, wxT("Edit Text Property"), msg, value);
            if (dialog.ShowModal() == wxID_OK)
            {
                value = ctConvertToSingleText(dialog.GetText());
                m_attributeEditorGrid->SetCellValue(row, 1, value);
                ApplyCellValueToProperty(row, 1);
                return true;
            }
            else
                return false;
        }
        else if (type == _T("filename"))
        {
            wxString fullPath = value;
            wxString defaultDir ;
            wxString defaultFilename = wxFileNameFromPath(fullPath);

            defaultDir = wxPathOnly(value);

            wxString msg = wxT("Choose a filename");
            wxFileDialog dialog(wxTheApp->GetTopWindow(),
                    msg, defaultDir, defaultFilename, wxT("*.*"));
            if (dialog.ShowModal() == wxID_OK)
            {
                fullPath = dialog.GetPath();
                value = fullPath;

                m_attributeEditorGrid->SetCellValue(row, 1, value);
                ApplyCellValueToProperty(row, 1);
                return true;
            }
            else
                return false;
        }
        else if (type == _T("configitems"))
        {
            wxArrayString items;
            ctConfigItem::StringToArray(value, items);

            ctConfigItemsSelector dialog(wxTheApp->GetTopWindow(),
                    wxID_ANY, wxT("Select Configuration Items"));
            dialog.SetConfigList(items);
            if (dialog.ShowModal() == wxID_OK)
            {
                wxString newValue;
                items = dialog.GetConfigList();
                ctConfigItem::ArrayToString(items, newValue);

                m_attributeEditorGrid->SetCellValue(row, 1, newValue);
                ApplyCellValueToProperty(row, 1);
                return true;
            }
            else
                return false;
        }
    }

    return false;
}

/// Intercept selection event.
void ctPropertyEditor::OnSelectCell(wxGridEvent& event)
{
    int row = event.GetRow();

    UpdateDescription(row);

    event.Skip();
}

/// Update the description
void ctPropertyEditor::UpdateDescription(int row)
{
    if (row == -1)
    {
        row = m_attributeEditorGrid->GetCursorRow();
    }
    if (row == -1)
    {
        wxString str = WrapDescription(wxEmptyString);
        m_propertyDescriptionWindow->SetPage(str);
    }
    else
    {
        ctProperty* prop = FindProperty(row);
        if (prop)
        {
            wxString str = WrapDescription(m_item->GetDescription(prop));
            m_propertyDescriptionWindow->SetPage(str);
        }
    }
}

/// Wraps a description string in HTML
wxString ctPropertyEditor::WrapDescription(const wxString& s)
{
    /// Convert a colour to a 6-digit hex string
    wxColour col = ctDESCRIPTION_BACKGROUND_COLOUR;
    wxString colStr = apColourToHexString(col);
    colStr = wxT("#") + colStr;

    wxString str;
    str << _T("<HTML><BODY BGCOLOR=\"") << colStr << wxT("\"><FONT SIZE=-1>") ;
    str << s;
    str << _T("</FONT></BODY></HTML>");

    return str;
}

/// Intercept cell data change event.
void ctPropertyEditor::OnChangeCell(wxGridEvent& event)
{
    int row = event.GetRow();
    int col = event.GetCol();

    ApplyCellValueToProperty(row, col);
}

/// Double-click to show specialised editor.
void ctPropertyEditor::OnDClickCell(wxGridEvent& WXUNUSED(event))
{
    wxWindow* parentFrame = this;
    while (parentFrame && !parentFrame->IsKindOf(CLASSINFO(wxFrame)))
        parentFrame = parentFrame->GetParent();

    EditDetails(parentFrame);
}

/// Apply the cell value to the property, and notify the
/// item object.
void ctPropertyEditor::ApplyCellValueToProperty(int row, int col)
{
    static bool s_Applying = false;

    if (s_Applying)
        return;

    s_Applying = true;
    if (col == 1 && m_item)
    {
        ctProperty* prop = m_item->GetProperties().GetNth(row);

        wxString value = m_attributeEditorGrid->GetCellValue(row, col);
        if (prop->GetEditorType() == wxT("multiline"))
            value = ctConvertToMultilineText(value);

        wxVariant variant = prop->GetVariant();

        if (prop->GetVariant().GetType() == _T("bool"))
        {
            if (value == _T("1"))
                variant = true;
            else
                variant = false;
        }
        else if (prop->GetVariant().GetType() == _T("long"))
        {
            long l;
            value.ToLong(& l);
            variant = l;
        }
        else if (prop->GetVariant().GetType() == _T("double"))
        {
            double d;
            value.ToDouble(& d);
            variant = d;
        }
        else
        {
            variant = value;
        }

        ApplyPropertyValue(m_item, prop, variant);

        if (prop->GetName() == _T("description"))
            UpdateDescription(row);
    }
    s_Applying = false;
}

/// Apply the cell value to the property, and notify the
/// item object.
void ctPropertyEditor::ApplyPropertyValue(ctConfigItem* item, ctProperty* property, const wxVariant& variant)
{
    static bool s_Applying = false;

    if (s_Applying)
        return;

    s_Applying = true;

    // Save the old values
    ctProperties* oldProperties = new ctProperties(item->GetProperties());

    wxVariant oldValue = property->GetVariant();

    // Apply the new value
    property->GetVariant() = variant;
    item->ApplyProperty(property, oldValue);
    item->Modify();

    UpdateItem();

    wxString menuLabel(_T("Change ") + property->GetName());

    // This won't do anything first time Do is applied,
    // since we've already done the action for this property.
    // But when we Undo or Redo, the changed properties will be applied.
    item->GetDocument()->GetCommandProcessor()->Submit(
        new ctConfigCommand(menuLabel, ctCMD_APPLY_PROPERTY,
        item, oldProperties, true));

    s_Applying = false;
}

/*!
 * Attribute editor grid
 */

IMPLEMENT_CLASS(ctPropertyEditorGrid, wxGrid)

BEGIN_EVENT_TABLE(ctPropertyEditorGrid, wxGrid)
    EVT_SIZE(ctPropertyEditorGrid::OnSize)
END_EVENT_TABLE()

ctPropertyEditorGrid::ctPropertyEditorGrid(wxWindow* parent, wxWindowID id,
                                             const wxPoint& pos,
                                             const wxSize& sz, long style):
    wxGrid(parent, id, pos, sz, style)

{
    m_stretchableColumn = -1;
}

void ctPropertyEditorGrid::OnSize(wxSizeEvent& event)
{
    if (m_stretchableColumn != -1)
    {
        // This window's client size = the internal window's
        // client size if it has no borders
        wxSize sz = GetClientSize();

        int totalSize = 0;
        int i;
        for (i = 0; i < GetNumberCols(); i ++)
        {
            if (i != m_stretchableColumn)
            {
                totalSize += GetColSize(i);
            }
        }

        // Allow for grid lines
        totalSize += 1;

        int stretchSize = wxMax(5, sz.x - totalSize);
        SetColSize(m_stretchableColumn, stretchSize);
    }

    event.Skip();
}

/// Use m_columnsToDisplay to set the label strings
void ctPropertyEditorGrid::DisplayLabels()
{
    size_t i;
    for (i = 0; i < m_columnsToDisplay.GetCount(); i++)
    {
        SetColLabelValue(i, m_columnsToDisplay[i]);
    }
}

/// Clear attributes
bool ctPropertyEditorGrid::ClearAttributes()
{
    if (GetNumberRows() > 0)
        DeleteRows(0, GetNumberRows());
    return true;
}

/*!
 * Use a single-line text control.
 */

void ctGridCellTextEditor::Create(wxWindow* parent, wxWindowID id,
        wxEvtHandler* evtHandler)
{
    m_control = new wxTextCtrl(parent, id, wxEmptyString,
                               wxDefaultPosition, wxDefaultSize
                              );

    wxGridCellEditor::Create(parent, id, evtHandler);
}


/// Translate the value to one which can be edited in a single-line
/// text control
wxString ctConvertToSingleText(const wxString& value)
{
    wxString value1(value);
    value1.Replace(wxT("\n"), wxT("\\n"));
    value1.Replace(wxT("\t"), wxT("\\t"));
    return value1;
}

/// Translate back to 'real' characters, i.e. newlines are real
/// newlines.
wxString ctConvertToMultilineText(const wxString& value)
{
    wxString value1(value);
    value1.Replace(wxT("\\n"), wxT("\n"));
    value1.Replace(wxT("\\t"), wxT("\t"));
    return value1;
}

//----------------------------------------------------------------------------
// ctMultiLineTextEditor
//----------------------------------------------------------------------------

BEGIN_EVENT_TABLE(ctMultiLineTextEditor, wxDialog)
END_EVENT_TABLE()

ctMultiLineTextEditor::ctMultiLineTextEditor( wxWindow *parent, wxWindowID id, const wxString &title,
        const wxString& msg,
        const wxString& value,
        const wxPoint& pos,
        const wxSize& size,
        long style):
    wxDialog(parent, id, title, pos, size, style)
{
    m_text = value;
    AddControls(this, msg);
    Centre();
}

bool ctMultiLineTextEditor::AddControls(wxWindow* parent, const wxString& msg)
{
    wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );

    wxBoxSizer *item1 = new wxBoxSizer( wxVERTICAL );

    wxStaticText *item2 = new wxStaticText( parent, wxID_STATIC, msg, wxDefaultPosition, wxDefaultSize, 0 );
    item1->Add( item2, 0, wxALIGN_CENTER_VERTICAL|wxTOP|wxLEFT|wxRIGHT, 5 );

    wxTextCtrl *item3 = new wxTextCtrl( parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(330,180), wxTE_MULTILINE|wxTE_RICH );
    item1->Add( item3, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );

    wxBoxSizer *item4 = new wxBoxSizer( wxHORIZONTAL );

    item4->Add( 5, 5, 1, wxALIGN_CENTRE|wxALL, 5 );

    wxButton *item5 = new wxButton( parent, wxID_OK);
    item4->Add( item5, 0, wxALIGN_CENTRE|wxALL, 5 );

    wxButton *item6 = new wxButton( parent, wxID_CANCEL);
    item4->Add( item6, 0, wxALIGN_CENTRE|wxALL, 5 );

    item1->Add( item4, 0, wxGROW|wxALIGN_CENTER_VERTICAL, 5 );

    item0->Add( item1, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );

    item3->SetValue(m_text);
    item3->SetValidator(wxGenericValidator(& m_text));


    item3->SetFocus();
    item5->SetDefault();

    parent->SetSizer(item0);
    item0->Fit(parent);

    return true;
}

/*
 * Special-purpose splitter window for changing sash look and
 * also saving sash width
 */

BEGIN_EVENT_TABLE(ctSplitterWindow, wxSplitterWindow)
    EVT_SPLITTER_SASH_POS_CHANGED(wxID_ANY, ctSplitterWindow::OnChangeSash)
END_EVENT_TABLE()

ctSplitterWindow::ctSplitterWindow(wxWindow* parent, wxWindowID id,
        const wxPoint& pos, const wxSize& size, long style):
      wxSplitterWindow(parent, id, pos, size, style)
{
    m_updateSettings = false;
    m_position = 0;
}

void ctSplitterWindow::OnChangeSash(wxSplitterEvent& event)
{
    if (!m_updateSettings)
        return;

    m_position = event.GetSashPosition();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -