Author Topic: wxSmith Plugin: can't delete from form  (Read 7974 times)

olivetti

  • Guest
wxSmith Plugin: can't delete from form
« on: October 23, 2007, 06:31:36 pm »
Hi, I'm developing a wxFlatNotebook plugin for wxSmith completely based on wxsNotebook.cpp.
It's everything working okay but whenever I insert a flatnotebook on my wxSmith form builder, I cannot delete it!
Anyone have any ideas on why this happens?

olivetti

  • Guest
Re: wxSmith Plugin: can't delete from form
« Reply #1 on: October 23, 2007, 09:14:10 pm »
here's the code if it's of any help:

Code
#define wxPG_COMPATIBILITY_1_0_0

#include "wxsFlatNotebook.h"
#include <wx/wxflatnotebook/wxFlatNotebook.h>
#include <wx/wxflatnotebook/renderer.h>
#include <wxsadvqppchild.h>
#include <wx/propgrid/propgrid.h>
#include <wxwidgets/wxsitemresdata.h>
#include <wx/menu.h>
#include <wx/textdlg.h>
#include <wxwidgets/wxsflags.h>

using namespace wxsFlags;

//(*Headers(wxsNotebookParentQP)
#include <wx/sizer.h>
#include <wx/textctrl.h>
#include <wx/checkbox.h>
#include <wx/panel.h>
//*)

//(*InternalHeaders(wxsNotebookParentQP)
#include <wx/intl.h>
#include <wx/string.h>
//*)

namespace
{
    // Loading images from xpm files
    #include "images/flatnote16.xpm"
    #include "images/flatnote32.xpm"

    // This code provides basic informations about item and register
    // it inside wxSmith
    wxsRegisterItem<wxsFlatNotebook> Reg(
        _T("wxFlatNotebook"),                     // Class name
        wxsTContainer,                            // Item type
        _T("wxWindows"),                       // License
        _T("Eran Ifrah"),                      // Author
        _T("eranif@users.sourceforge.net"),  // Author's email (in real plugin there's no need to do anti-spam tricks ;) )
        _T("http://sourceforge.net/projects/wxflatnotebook"),    // Item's homepage
        _T("Contrib"),                         // Category in palette
        80,                                    // Priority in palette
        _T("FlatNoteBook"),                           // Base part of names for new items
        wxsCPP,                                // List of coding languages supported by this item
        1, 0,                                  // Version
        wxBitmap(flatnote32),               // 32x32 bitmap
        wxBitmap(flatnote16),               // 16x16 bitmap
        false);                                // We do not allow this item inside XRC files

    /** \brief Extra parameters for notebook's children */
    class wxsFlatNotebookExtra: public wxsPropertyContainer
    {
        public:

            wxsFlatNotebookExtra():
                m_Label(_("Page name")),
                m_Selected(false)
            {}

            wxString m_Label;
            bool m_Selected;

        protected:

            virtual void OnEnumProperties(long Flags)
            {
                WXS_SHORT_STRING(wxsFlatNotebookExtra,m_Label,_("Page name"),_T("label"),_T(""),false);
                WXS_BOOL(wxsFlatNotebookExtra,m_Selected,_("Page selected"),_T("selected"),false);
            }
    };

        /** \brief Inernal Quick properties panel */

    class wxsFlatNotebookParentQP: public wxsAdvQPPChild
    {
        public:

            wxsFlatNotebookParentQP(wxsAdvQPP* parent,wxsFlatNotebookExtra* Extra,wxWindowID id = -1):
                wxsAdvQPPChild(parent,_("FlatNotebook")),
                m_Extra(Extra)
            {
                //(*Initialize(wxsNotebookParentQP)
                Create(parent, id, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("id"));
                FlexGridSizer1 = new wxFlexGridSizer(0, 1, 0, 0);
                StaticBoxSizer1 = new wxStaticBoxSizer(wxVERTICAL, this, _("Label"));
                Label = new wxTextCtrl(this, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
                StaticBoxSizer1->Add(Label, 0, wxBOTTOM|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
                FlexGridSizer1->Add(StaticBoxSizer1, 1, wxLEFT|wxRIGHT|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
                StaticBoxSizer2 = new wxStaticBoxSizer(wxHORIZONTAL, this, _("Selection"));
                Selected = new wxCheckBox(this, ID_CHECKBOX1, _("Selected"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX1"));
                Selected->SetValue(false);
                StaticBoxSizer2->Add(Selected, 1, wxBOTTOM|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
                FlexGridSizer1->Add(StaticBoxSizer2, 1, wxLEFT|wxRIGHT|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
                SetSizer(FlexGridSizer1);
                FlexGridSizer1->Fit(this);
                FlexGridSizer1->SetSizeHints(this);

                Connect(ID_TEXTCTRL1,wxEVT_COMMAND_TEXT_ENTER,(wxObjectEventFunction)&wxsFlatNotebookParentQP::OnLabelText);
                Connect(ID_CHECKBOX1,wxEVT_COMMAND_CHECKBOX_CLICKED,(wxObjectEventFunction)&wxsFlatNotebookParentQP::OnSelectionChange);
                //*)
                ReadData();

                Label->Connect(-1,wxEVT_KILL_FOCUS,(wxObjectEventFunction)&wxsFlatNotebookParentQP::OnLabelKillFocus,0,this);
            }

            virtual ~wxsFlatNotebookParentQP()
            {
                //(*Destroy(wxsNotebookParentQP)
                //*)
            }

        private:

            virtual void Update()
            {
                ReadData();
            }

            void ReadData()
            {
                if ( !GetPropertyContainer() || !m_Extra ) return;
                Label->SetValue(m_Extra->m_Label);
                Selected->SetValue(m_Extra->m_Selected);
            }

            void SaveData()
            {
                if ( !GetPropertyContainer() || !m_Extra ) return;
                m_Extra->m_Label = Label->GetValue();
                m_Extra->m_Selected = Selected->GetValue();
                NotifyChange();
            }

            //(*Identifiers(wxsNotebookParentQP)
            static const long ID_TEXTCTRL1;
            static const long ID_CHECKBOX1;
            //*)

            //(*Handlers(wxsNotebookParentQP)
            void OnLabelText(wxCommandEvent& event);
            void OnLabelKillFocus(wxFocusEvent& event);
            void OnSelectionChange(wxCommandEvent& event);
            //*)

            //(*Declarations(wxsNotebookParentQP)
            wxStaticBoxSizer* StaticBoxSizer2;
            wxCheckBox* Selected;
            wxTextCtrl* Label;
            wxStaticBoxSizer* StaticBoxSizer1;
            wxFlexGridSizer* FlexGridSizer1;
            //*)

            wxsFlatNotebookExtra* m_Extra;

            DECLARE_EVENT_TABLE()
    };

    //(*IdInit(wxsNotebookParentQP)
    const long wxsFlatNotebookParentQP::ID_TEXTCTRL1 = wxNewId();
    const long wxsFlatNotebookParentQP::ID_CHECKBOX1 = wxNewId();
    //*)

    BEGIN_EVENT_TABLE(wxsFlatNotebookParentQP,wxPanel)
        //(*EventTable(wxsNotebookParentQP)
        //*)
    END_EVENT_TABLE()

    void wxsFlatNotebookParentQP::OnLabelText(wxCommandEvent& event)       { SaveData(); }
    void wxsFlatNotebookParentQP::OnLabelKillFocus(wxFocusEvent& event)    { SaveData(); event.Skip(); }
    void wxsFlatNotebookParentQP::OnSelectionChange(wxCommandEvent& event) { SaveData(); }

    WXS_ST_BEGIN(wxsFlatNotebookStyles,_T(""))
        WXS_ST_CATEGORY("wxFlatNotebook")
        WXS_ST(wxFNB_DEFAULT_STYLE)
        WXS_ST(wxFNB_VC71)
        WXS_ST(wxFNB_FANCY_TABS)
        WXS_ST(wxFNB_TABS_BORDER_SIMPLE)
        WXS_ST(wxFNB_NO_X_BUTTON)
        WXS_ST(wxFNB_NO_NAV_BUTTONS)
        WXS_ST(wxFNB_MOUSE_MIDDLE_CLOSES_TABS)
        WXS_ST(wxFNB_BOTTOM)
        WXS_ST(wxFNB_NODRAG)
        WXS_ST(wxFNB_VC8)
        WXS_ST(wxFNB_X_ON_TAB)
        WXS_ST(wxFNB_BACKGROUND_GRADIENT)
        WXS_ST(wxFNB_COLORFUL_TABS)
        WXS_ST(wxFNB_DCLICK_CLOSES_TABS)
        WXS_ST(wxFNB_SMART_TABS)
        WXS_ST(wxFNB_DROPDOWN_TABS_LIST)
        WXS_ST(wxFNB_ALLOW_FOREIGN_DND)
        WXS_ST(wxFNB_FF2)
        WXS_ST(wxFNB_CUSTOM_DLG)
        WXS_ST_DEFAULTS()
    WXS_ST_END()

    WXS_EV_BEGIN(wxsFlatNotebookEvents)
        WXS_EVI(EVT_NOTEBOOK_PAGE_CHANGED,wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,wxFlatNotebookEvent,PageChanged)
        WXS_EVI(EVT_NOTEBOOK_PAGE_CHANGING,wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING,wxFlatNotebookEvent,PageChanging)
    WXS_EV_END()

    const long popupNewPageId = wxNewId();
    const long popupPrevPageId = wxNewId();
    const long popupNextPageId = wxNewId();
    const long popupFirstId = wxNewId();
    const long popupLastId = wxNewId();


}

wxsFlatNotebook::wxsFlatNotebook(wxsItemResData* Data) : wxsContainer(
        Data,               // Data passed to constructor
        &Reg.Info,          // Info taken from Registering object previously created
        wxsFlatNotebookEvents,
        wxsFlatNotebookStyles),
    m_CurrentSelection(0)
{
    //ctor

}

void wxsFlatNotebook::OnEnumContainerProperties(long Flags)
{
}

bool wxsFlatNotebook::OnCanAddChild(wxsItem* Item,bool ShowMessage)
{
    if ( Item->GetType() == wxsTSizer )
    {
        if ( ShowMessage )
        {
            wxMessageBox(_("Can not add sizer into FlatNotebook.\nAdd panels first"));
        }
        return false;
    }

return wxsContainer::OnCanAddChild(Item,ShowMessage);
}

wxsPropertyContainer* wxsFlatNotebook::OnBuildExtra()
{
    return new wxsFlatNotebookExtra();
}

wxString wxsFlatNotebook::OnXmlGetExtraObjectClass()
{
    return _T("notebookpage");
}

void wxsFlatNotebook::OnAddChildQPP(wxsItem* Child,wxsAdvQPP* QPP)
{
    wxsFlatNotebookExtra* Extra = (wxsFlatNotebookExtra*)GetChildExtra(GetChildIndex(Child));
    if ( Extra )
    {
        QPP->Register(new wxsFlatNotebookParentQP(QPP,Extra),_("FlatNotebook"));
    }
}

wxObject* wxsFlatNotebook::OnBuildPreview(wxWindow* Parent,long PreviewFlags)
{
    UpdateCurrentSelection();

    long bookStyle = 0;
    bookStyle |= wxFNB_VC8;
bookStyle |= wxFNB_TABS_BORDER_SIMPLE;
bookStyle |= wxFNB_NODRAG;
bookStyle |= wxFNB_CUSTOM_DLG;
bookStyle |= wxFNB_BOTTOM;
bookStyle |= wxFNB_NO_NAV_BUTTONS;
bookStyle |= wxFNB_NO_X_BUTTON;

wxFlatNotebook* Notebook = new wxFlatNotebook(Parent,GetID(),Pos(Parent),Size(Parent),bookStyle);

if ( !GetChildCount() && !(PreviewFlags&pfExact) )
{
    // Adding additional empty notebook to prevent from having zero-sized notebook
    Notebook->AddPage(
            new wxPanel(Notebook,GetID(),wxDefaultPosition,wxSize(200,50)),
            _("No pages"));
}

AddChildrenPreview(Notebook,PreviewFlags);

for ( int i=0; i<GetChildCount(); i++ )
{
    wxsItem* Child = GetChild(i);
    wxsFlatNotebookExtra* Extra = (wxsFlatNotebookExtra*)GetChildExtra(i);

    wxWindow* ChildPreview = wxDynamicCast(GetChild(i)->GetLastPreview(),wxWindow);
    if ( !ChildPreview ) continue;

    bool Selected = (Child == m_CurrentSelection);
    if ( PreviewFlags & pfExact ) Selected = Extra->m_Selected;

    Notebook->AddPage(ChildPreview,Extra->m_Label,Selected);
}

return Notebook;
}

void wxsFlatNotebook::OnBuildCreatingCode()
{
    switch ( GetLanguage() )
    {
        case wxsCPP:
        {
            AddHeader(_T("<wx/wxFlatNotebook/wxFlatNotebook.h>"),GetInfo().ClassName,0);
            AddHeader(_T("<wx/wxFlatNotebook/renderer.h>"),GetInfo().ClassName);
            Codef(_T("%C(%W, %I, %P, %S, %T);\n"));
            BuildSetupWindowCode();
            AddChildrenCode();

            for ( int i=0; i<GetChildCount(); i++ )
            {
                wxsFlatNotebookExtra* Extra = (wxsFlatNotebookExtra*)GetChildExtra(i);
                Codef(_T("%AAddPage(%o, %t, %b);\n"),i,Extra->m_Label.c_str(),Extra->m_Selected);
            }

            break;
        }

        default:
        {
            wxsCodeMarks::Unknown(_T("wxsFlatNotebook::OnBuildCreatingCode"),GetLanguage());
        }
    }
}

bool wxsFlatNotebook::OnMouseClick(wxWindow* Preview,int PosX,int PosY)
{
    UpdateCurrentSelection();
    wxFlatNotebook* Notebook = (wxFlatNotebook*)Preview;
    int Hit = Notebook->HitTest(wxPoint(PosX,PosY));
    if ( Hit != wxNOT_FOUND )
    {
        wxsItem* OldSel = m_CurrentSelection;
        m_CurrentSelection = GetChild(Hit);
        GetResourceData()->SelectItem(m_CurrentSelection,true);
        return OldSel != m_CurrentSelection;
    }
    return false;
}

bool wxsFlatNotebook::OnIsChildPreviewVisible(wxsItem* Child)
{
    UpdateCurrentSelection();
    return Child == m_CurrentSelection;
}

bool wxsFlatNotebook::OnEnsureChildPreviewVisible(wxsItem* Child)
{
    if ( IsChildPreviewVisible(Child) ) return false;
    m_CurrentSelection = Child;
    UpdateCurrentSelection();
    return true;
}

void wxsFlatNotebook::UpdateCurrentSelection()
{
    wxsItem* NewCurrentSelection = 0;
    for ( int i=0; i<GetChildCount(); i++ )
    {
        if ( m_CurrentSelection == GetChild(i) ) return;
        wxsFlatNotebookExtra* Extra = (wxsFlatNotebookExtra*)GetChildExtra(i);
        if ( (i==0) || Extra->m_Selected )
        {
            NewCurrentSelection = GetChild(i);
        }
    }
    m_CurrentSelection = NewCurrentSelection;
}
void wxsFlatNotebook::OnPreparePopup(wxMenu* Menu)
{
    Menu->Append(popupNewPageId,_("Add new page"));
    Menu->AppendSeparator();
    // This require some extra fixing
    //wxMenuItem* Item1 = Menu->Append(popupPrevPageId,_("Go to previous page"));
    //wxMenuItem* Item2 = Menu->Append(popupNextPageId,_("Go to next page"));
    //Menu->AppendSeparator();
    wxMenuItem* Item3 = Menu->Append(popupFirstId,_("Make current page the first one"));
    wxMenuItem* Item4 = Menu->Append(popupLastId,_("Make current page the last one"));
    if ( !m_CurrentSelection || GetChildIndex(m_CurrentSelection)==0 )
    {
        //Item1->Enable(false);
        Item3->Enable(false);
    }
    if ( !m_CurrentSelection || GetChildIndex(m_CurrentSelection)==GetChildCount()-1 )
    {
        //Item2->Enable(false);
        Item4->Enable(false);
    }
}

bool wxsFlatNotebook::OnPopup(long Id)
{
    if ( Id == popupNewPageId )
    {
        wxTextEntryDialog Dlg(0,_("Enter name of new page"),_("Adding page"),_("New page"));
        if ( Dlg.ShowModal() == wxID_OK )
        {
            wxsItem* Panel = wxsItemFactory::Build(_T("wxPanel"),GetResourceData());
            if ( Panel )
            {
                GetResourceData()->BeginChange();
                if ( AddChild(Panel) )
                {
                    wxsFlatNotebookExtra* Extra = (wxsFlatNotebookExtra*)GetChildExtra(GetChildCount()-1);
                    if ( Extra )
                    {
                        Extra->m_Label = Dlg.GetValue();
                    }
                    m_CurrentSelection = Panel;
                }
                else
                {
                    delete Panel;
                }
                GetResourceData()->EndChange();
            }
        }
    }
    else if ( Id == popupNextPageId )
    {
        GetResourceData()->BeginChange();
        int Index = GetChildIndex(m_CurrentSelection);
        m_CurrentSelection = GetChild(Index-1);
        UpdateCurrentSelection();
        GetResourceData()->EndChange();
    }
    else if ( Id == popupPrevPageId )
    {
        GetResourceData()->BeginChange();
        int Index = GetChildIndex(m_CurrentSelection);
        m_CurrentSelection = GetChild(Index+1);
        UpdateCurrentSelection();
        GetResourceData()->EndChange();
    }
    else if ( Id == popupFirstId )
    {
        GetResourceData()->BeginChange();
        MoveChild(GetChildIndex(m_CurrentSelection),0);
        GetResourceData()->EndChange();
    }
    else if ( Id == popupLastId )
    {
        GetResourceData()->BeginChange();
        MoveChild(GetChildIndex(m_CurrentSelection),GetChildCount()-1);
        GetResourceData()->EndChange();
    }
    else
    {
        return wxsContainer::OnPopup(Id);
    }
    return true;
}

wxsFlatNotebook::~wxsFlatNotebook()
{
    //dtor
}

Offline byo

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 837
Re: wxSmith Plugin: can't delete from form
« Reply #2 on: October 23, 2007, 10:46:24 pm »
Hi :)

I haven't checked the code running but the source looks correct. There may be a problem with current selection while deleting. Try selecting FlatNotebook from resource browser and than try to delete it. Selecting notebook on editor may actually cause some of it's notebooks to be selected which will result in deleting only one notebook.

BTW. Your code could be included in main code::blocks sources inside wxSmithContribItems plugin, so if you would like to see it in official C::B repository there's no problem :) I'd be grateful to see it there ;)

Regards
   BYO

olivetti

  • Guest
Re: wxSmith Plugin: can't delete from form
« Reply #3 on: October 23, 2007, 11:04:23 pm »
I'll be glad to contribute to the community with this code!

I guess as soon as I solve this problem it will be good enough to be included!


Another side effect I noticed, after I insert a FlatNotebook on the form I can't even delete others components!

Edit: oh, and I tried to select it from the editor and it didn't work!
« Last Edit: October 23, 2007, 11:24:10 pm by olivetti »

Offline byo

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 837
Re: wxSmith Plugin: can't delete from form
« Reply #4 on: October 23, 2007, 11:48:52 pm »
This looks strange, maybe some bug in wxSmith...

What does happen when you try to delete ? Does it show any message ? And how did you try to delete ? There are at least 3 ways: by pressing delete key, by pushing X button on the right side of editor or by cutting current selection (either Ctrl+X or from menu).

You can also put all your code here (on forum so not yet into main source repository) so I'll also be able to test what's wrong since it looks more like  internal wxSmith's issue rather than problem in your code. But I'll be able to test it tomorow since I'm going to bed now :)

Regards
   BYO

olivetti

  • Guest
Re: wxSmith Plugin: can't delete from form
« Reply #5 on: October 24, 2007, 12:08:36 am »
hmmm it's just when I press the delete button, but I guess I know what's going on.
The wxFlatNotebook doesn't have a HitTest(), so the OnMouseClick event does not work and so the container isn't selected!
I'll try to fix tomorrow (going to be also :D) and I'll post the whole code!

olivetti

  • Guest
Re: wxSmith Plugin: can't delete from form
« Reply #6 on: October 24, 2007, 04:15:52 pm »
well, the plugin is more mature now, but the delete button still does not work! (though using the X or ctrl-x works)

here's a link to the source code (including project and the wxFlatNotebook plugin):

http://rapidshare.com/files/64849043/wxFlatNotebookPlugin.zip.html

Offline byo

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 837
Re: wxSmith Plugin: can't delete from form
« Reply #7 on: October 24, 2007, 11:14:22 pm »
Hi

I've tested this component on Linux and current I don't see any issues with deleting. Maybe this bug is windows-specific. I also notied that you include your own copy of wxFlatNotebook with the sources. That's not necessary since C::B already has this library linked in main dll-s so we can use that version. I also managed to successfully add your component into wxSmithContribItems plugin (currently on linux only) and it works nice so if you want I can add it into main source repository. Only one .cpp, one .h and bitmaps are needed since separate plugin sources and extra wxFlatNotebook sources are not required. It should also be easy to add it into windows project, but since I'll have to compile C::B from scratch on my windows box it will take some time.

So waiting for your answer, see you tomorrow :)

BYO

olivetti

  • Guest
Re: wxSmith Plugin: can't delete from form
« Reply #8 on: October 24, 2007, 11:40:48 pm »
oh it would be nice if you could add it there!
Maybe adding to wxSmithContribItems and compiling with the FlatNotebook included on C::B it solves the problem on windows as well!

I'm looking forward to that!

regards

Offline byo

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 837
Re: wxSmith Plugin: can't delete from form
« Reply #9 on: October 25, 2007, 09:56:01 pm »
Ok, it's now in official sources :) It should be inside next nightly.

Unfortunately the delete bug still occur on windows. And probably it's caused by wxFlatNotebook which blocks some keyboard events. I don't have enough time now to investigate it better so maybe you will find something. Currently Ctrl+X and X button must be sufficient :)

Regards
   BYO

BTW. There was no copyright header in your code, I asume that you want to publish this as GPL software. I can add those headers but since it's your code, it's also your decision :)

olivetti

  • Guest
Re: wxSmith Plugin: can't delete from form
« Reply #10 on: October 25, 2007, 09:59:39 pm »
hmmm I guess you're right about wxFlatNotebook blocking the event, I'll investigate it!

And you may put the gpl headers ;)

I didn't worry about those details because I was implementing it in a hurry for a work I'm doing, but my intetion was to contribute with the community from the beggining!

Thanks!