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

📄 propeditor.cpp

📁 Wxpython Implemented on Windows CE, Source code
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/////////////////////////////////////////////////////////////////////////////
// Name:        propeditor.cpp
// Purpose:     wxWidgets Configuration Tool property editor
// Author:      Julian Smart
// Modified by:
// Created:     2003-06-03
// RCS-ID:      $Id: propeditor.cpp,v 1.12 2006/04/14 17:01:14 ABX Exp $
// Copyright:   (c) Julian Smart
// Licence:
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP

#include "wx/grid.h"
#include "wx/filedlg.h"

#endif

#include "wx/html/htmlwin.h"
#include "wx/tokenzr.h"
#include "wx/valgen.h"
#include "propeditor.h"
#include "symbols.h"
#include "utils.h"
#include "configtooldoc.h"
#include "configitemselector.h"

#include "bitmaps/ellipsis.xpm"


/*!
 * A container window for the property editor.
 * and attribute editor.
 */

IMPLEMENT_CLASS(ctPropertyEditor, wxPanel)

BEGIN_EVENT_TABLE(ctPropertyEditor, wxPanel)
    EVT_BUTTON(ctID_ATTRIBUTE_EDITOR_EDIT_DETAILS, ctPropertyEditor::OnEditDetails)
    EVT_GRID_SELECT_CELL(ctPropertyEditor::OnSelectCell)
    EVT_GRID_CELL_CHANGE(ctPropertyEditor::OnChangeCell)
    EVT_GRID_CELL_LEFT_DCLICK(ctPropertyEditor::OnDClickCell)
    EVT_UPDATE_UI(ctID_ATTRIBUTE_EDITOR_EDIT_DETAILS, ctPropertyEditor::OnUpdateEditDetails)
END_EVENT_TABLE()

ctPropertyEditor::ctPropertyEditor(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style):
    wxPanel(parent, id, pos, size, style)
{
    m_splitterWindow = NULL;
    m_attributeEditorGrid = NULL;
    m_propertyDescriptionWindow = NULL;
    m_elementTitleTextCtrl = NULL;

    CreateControls(this);
}

void ctPropertyEditor::CreateControls(wxWindow* parent)
{
    m_elementTitleTextCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
    wxBitmap detailsIcon(ellipsis_xpm);

    wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );

    wxBoxSizer *item1 = new wxBoxSizer( wxHORIZONTAL );

    wxTextCtrl *item2 = m_elementTitleTextCtrl;
    item1->Add( item2, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );

    m_attributeEditorEditDetails = new wxButton( parent, ctID_ATTRIBUTE_EDITOR_EDIT_DETAILS, wxT("Edit..."));
    item1->Add( m_attributeEditorEditDetails, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 );

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

    // TODO: find sash pos from last time
    int sashPos = 100;

    m_splitterWindow = new wxSplitterWindow(parent, ctID_PROPERTY_EDITOR_SPLITTER, wxDefaultPosition, wxSize(500, 400), wxSP_3DSASH/*|wxCLIP_CHILDREN*/ |wxBORDER_NONE|wxNO_FULL_REPAINT_ON_RESIZE);
    m_splitterWindow->SetMinimumPaneSize(10);

    m_propertyDescriptionWindow = new wxHtmlWindow(m_splitterWindow, ctID_ATTRIBUTE_EDITOR_DESCRIPTION, wxDefaultPosition, wxSize(200, 60), wxSUNKEN_BORDER);
    m_propertyDescriptionWindow->SetBackgroundColour(ctDESCRIPTION_BACKGROUND_COLOUR);
    m_propertyDescriptionWindow->SetBorders(4);
    m_attributeEditorGrid = new ctPropertyEditorGrid(m_splitterWindow, ctID_ATTRIBUTE_EDITOR_GRID , wxPoint(0, 0), wxSize(200, 100), wxBORDER_SUNKEN | wxWANTS_CHARS);

    m_splitterWindow->SplitHorizontally(m_propertyDescriptionWindow, m_attributeEditorGrid, sashPos);

    // TODO: show or hide description window
//    if (some-setting)
//        ShowDescriptionWindow(false);

    item0->Add( m_splitterWindow, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxBOTTOM, 5 );

    this->SetSizer( item0 );

    /// Add help text
    m_elementTitleTextCtrl->SetHelpText(_("The title of the property being edited."));
    m_attributeEditorEditDetails->SetHelpText(_("Click to use an appropriate editor for the selected property (if any)."));
    m_attributeEditorGrid->SetHelpText(_("Shows the properties of the selected item."));
    m_propertyDescriptionWindow->SetHelpText(_("Shows a description of the selected property, or a summary of the whole item."));

    /// Set up the grid to display properties
    m_attributeEditorGrid->RegisterDataType(ctGRID_VALUE_STRING,
                             new wxGridCellStringRenderer,
                             new ctGridCellTextEditor);

    m_attributeEditorGrid->CreateGrid( 0, 2, wxGrid::wxGridSelectRows );
    m_attributeEditorGrid->SetRowLabelSize(0);
    m_attributeEditorGrid->SetColLabelSize(0 /* 18 */);

    wxArrayString columns;
    columns.Add(_T("Name"));
    columns.Add(_T("Value"));

    m_attributeEditorGrid->SetColSize(0, 140);
    m_attributeEditorGrid->SetColSize(1, 80);

    m_attributeEditorGrid->SetColumnsToDisplay(columns);
    m_attributeEditorGrid->DisplayLabels();

    m_attributeEditorGrid->SetStretchableColumn(1);

    m_attributeEditorGrid->SetDefaultCellBackgroundColour(ctCELL_BACKGROUND_COLOUR);

    UpdateDescription();
}

/// Show/hide the description control
void ctPropertyEditor::ShowDescriptionWindow(bool show)
{
    if (!show)
    {
        if (m_splitterWindow->IsSplit())
            m_splitterWindow->Unsplit(m_propertyDescriptionWindow);
    }
    else
    {
        // TODO
        int pos = 100;
        m_propertyDescriptionWindow->Show(true);
        if (!m_splitterWindow->IsSplit())
        {
            m_splitterWindow->SplitHorizontally(m_propertyDescriptionWindow, m_attributeEditorGrid, pos);
        }
    }
}

/// Clear grid editor
void ctPropertyEditor::ClearEditor()
{
    m_attributeEditorGrid->ClearAttributes();
    m_propertyDescriptionWindow->SetPage(WrapDescription(wxEmptyString));
    m_elementTitleTextCtrl->SetValue(wxEmptyString);
}

/// Handles detailed editing event.
void ctPropertyEditor::OnEditDetails(wxCommandEvent& WXUNUSED(event))
{
    wxWindow* parentFrame = this;
    while (parentFrame && !parentFrame->IsKindOf(CLASSINFO(wxFrame)))
        parentFrame = parentFrame->GetParent();

    EditDetails(parentFrame);
}

/// Handles detailed editing update event.
void ctPropertyEditor::OnUpdateEditDetails(wxUpdateUIEvent& event)
{
    event.Enable(CanEditDetails());
}

/// Can we edit the details of the selected property?
bool ctPropertyEditor::CanEditDetails()
{
    if (!m_item)
        return false;

    int row;
    ctProperty* prop = FindSelectedProperty(row);
    if (!prop || prop->GetEditorType().empty())
        return false;
    return true;
}

/// Shows the item
void ctPropertyEditor::ShowItem(ctConfigItem* item)
{
    if (m_attributeEditorGrid->IsCellEditControlEnabled())
        m_attributeEditorGrid->SaveEditControlValue();

    ClearEditor();
    if (item)
    {
        m_item = item;

        UpdateTitle();

        m_attributeEditorGrid->AppendRows(m_item->GetProperties().GetCount());

        wxObjectList::compatibility_iterator node = m_item->GetProperties().GetList().GetFirst();
        int i = 0;
        while (node)
        {
            ctProperty* prop = (ctProperty*) node->GetData();
            DisplayProperty(i, prop);

            i ++;
            node = node->GetNext();
        }
        // Make sure scrollbars are up-to-date, etc.
        wxSizeEvent event(m_attributeEditorGrid->GetSize(), m_attributeEditorGrid->GetId());
        m_attributeEditorGrid->GetEventHandler()->ProcessEvent(event);

        DisplayDefaultProperty();
    }
    else
    {
        m_item = NULL;
    }
}

/// Determine background colour for this property.
void ctPropertyEditor::DeterminePropertyColour(ctProperty* prop, wxColour& colour)
{
    if (prop->IsCustom())
        colour = ctCUSTOM_CELL_BACKGROUND_COLOUR;
    else
        colour = ctCELL_BACKGROUND_COLOUR;
}

/// Update the title at the top of the property editor
void ctPropertyEditor::UpdateTitle()
{
    if (m_item)
    {
        wxString name(m_item->GetTitle());
        m_elementTitleTextCtrl->SetValue(name);
    }
}

/// Updates the item display, assuming it was already displayed.
void ctPropertyEditor::UpdateItem()
{
    if (m_attributeEditorGrid->IsCellEditControlEnabled())
        m_attributeEditorGrid->SaveEditControlValue();
    if (m_item)
    {
        UpdateTitle();

        wxObjectList::compatibility_iterator node = m_item->GetProperties().GetList().GetFirst();
        int i = 0;
        while (node)
        {
            ctProperty* prop = (ctProperty*) node->GetData();
            DisplayProperty(i, prop, true);

            i ++;
            node = node->GetNext();
        }
        // Make sure scrollbars are up-to-date, etc.
        wxSizeEvent event(m_attributeEditorGrid->GetSize(), this->GetId());
        m_attributeEditorGrid->GetEventHandler()->ProcessEvent(event);
    }
}

/// Display attribute at given row
bool ctPropertyEditor::DisplayProperty(int row, ctProperty* prop, bool valueOnly)
{
    wxColour backgroundColour;

    DeterminePropertyColour(prop, backgroundColour);

    if (!valueOnly)
    {
        m_attributeEditorGrid->SetCellBackgroundColour(row, 0, backgroundColour);
        m_attributeEditorGrid->SetCellBackgroundColour(row, 1, backgroundColour);

        m_attributeEditorGrid->SetCellValue(row, 0, prop->GetName());

        m_attributeEditorGrid->SetReadOnly(row, 0);
        // Set the alignment
        m_attributeEditorGrid->SetCellAlignment(row, 1, wxALIGN_LEFT, wxALIGN_CENTER);
    }

    if (!m_item->CanEditProperty(prop->GetName()))
    {
        m_attributeEditorGrid->SetReadOnly(row, 1);

        wxColour col(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
        m_attributeEditorGrid->SetCellTextColour(row, 1, col);
    }
    else
    {
        m_attributeEditorGrid->SetReadOnly(row, 1, false);
        m_attributeEditorGrid->SetCellTextColour(row, 1, * wxBLACK);
    }

    // Set the value
    m_attributeEditorGrid->SetCellValue(row, 1, ctConvertToSingleText(prop->GetValue()));

    if (valueOnly)
        return true;

    // Set the value type
    if (prop->GetEditorType() == _T("choice"))
    {
#if 0
        wxString* strArr = prop->GetChoices().GetStringArray();

        m_attributeEditorGrid->SetCellEditor(row, 1,
                new wxGridCellChoiceEditor(prop->GetChoices().GetCount(), strArr));

        delete[] strArr;
#endif
        m_attributeEditorGrid->SetCellEditor(row, 1,
                new wxGridCellChoiceEditor(prop->GetChoices()));
    }
    else if (prop->GetEditorType() == _T("integer") || prop->GetVariant().GetType() == _T("long"))
    {
        m_attributeEditorGrid->SetCellEditor(row, 1,
                new wxGridCellNumberEditor);
    }
    else if (prop->GetEditorType() == _T("float") || prop->GetVariant().GetType() == _T("double"))
    {
        m_attributeEditorGrid->SetCellEditor(row, 1,
                new wxGridCellFloatEditor);
    }
    else if (prop->GetEditorType() == _T("bool") || prop->GetVariant().GetType() == _T("bool"))
    {
        m_attributeEditorGrid->SetCellValue(row, 1, prop->GetVariant().GetBool() ? _T("1") : _T("0"));
        m_attributeEditorGrid->SetCellEditor(row, 1,
                new wxGridCellBoolEditor);
        m_attributeEditorGrid->SetCellRenderer(row, 1, new wxGridCellBoolRenderer);
    }
    else
    {
        m_attributeEditorGrid->SetCellEditor(row, 1,
                new ctGridCellTextEditor);
    }

    return true;
}

/// Display attribute value
bool ctPropertyEditor::DisplayProperty(ctProperty* prop)
{
    if (!m_item)
        return false;

    int index = m_item->GetProperties().GetList().IndexOf(prop);
    return DisplayProperty(index, prop, true);
}

/// Display the default property
bool ctPropertyEditor::DisplayDefaultProperty()
{
    if (!m_item)
        return false;

    wxString str = m_item->GetDefaultProperty();

    ctProperty* prop = m_item->GetProperties().FindProperty(str);
    if (prop)
    {
        int index = m_item->GetProperties().GetList().IndexOf(prop);
        this->m_attributeEditorGrid->SelectRow(index);
        this->m_attributeEditorGrid->SetGridCursor(index, 1);
    }
    return true;
}

/// Edit the default property
bool ctPropertyEditor::EditDefaultProperty(ctConfigItem* item)
{
    wxString defaultPropertyName = item->GetDefaultProperty();
    if (!defaultPropertyName.empty())
    {
        ctProperty* prop = item->GetProperties().FindProperty(defaultPropertyName);
        if (prop)
        {
            int index = item->GetProperties().GetList().IndexOf(prop);
            if (index >= 0)
            {
                this->m_attributeEditorGrid->SelectRow(index);
                this->m_attributeEditorGrid->SetGridCursor(index, 1);
                EditDetails(wxTheApp->GetTopWindow());
                return true;
            }
        }

    }
    return false;
}

/// Find the selected property
ctProperty* ctPropertyEditor::FindSelectedProperty(int& row)
{
    if (!m_item)
        return NULL;

    int selRow = m_attributeEditorGrid->GetCursorRow();
    if (selRow > -1)
    {
        row = selRow;

        if (selRow < (int) m_item->GetProperties().GetCount())
        {
            ctProperty* prop = m_item->GetProperties().GetNth(selRow);
            return prop;
        }
    }
    return NULL;
}

/// Find the property
ctProperty* ctPropertyEditor::FindProperty(int row)
{
    if (!m_item)
        return NULL;

    if (row > -1)
    {

⌨️ 快捷键说明

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