PropertyGrid problem

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
greg
Earned some good credits
Earned some good credits
Posts: 111
Joined: Fri Sep 12, 2008 7:17 am

PropertyGrid problem

Post by greg » Fri Jul 13, 2012 7:05 am

Hi all
I have strange problem using wxPropertyGrid.
When calling from DLL wxPropertyGrid I'd like to delete item using DeleteProperty method
when selecting active row, but I can not delete it.
I'm using popup menu on Right Click on seleceted cell.
I'd like to mention that the same code on EXE (not DLL) final file works fine.
What's wrong with my code?

I'm using Wx 2.9.4

This my DLL code

Code: Select all

#include "wx/wx.h"

#include "wx/propgrid/propgrid.h"
#include "wx/propgrid/manager.h"
#include "wx/propgrid/advprops.h"


BOOL APIENTRY DllMain(HMODULE Module,DWORD Reason,LPVOID)
{
        if(Reason==DLL_PROCESS_DETACH)
        {
                wxEntryCleanup();
        }
        else if(Reason==DLL_PROCESS_ATTACH)
        {
                int argc = 0;
                char** argv = NULL;
                if(!wxEntryStart(argc, argv) ||
                        !wxTheApp ||
                        !wxTheApp->CallOnInit())
                {
                        return FALSE;
                }
        }

        return TRUE;
}

class CMyApp : public wxApp
{
public:
    CMyApp() { m_showImages = true; m_showButtons = false; }
      bool OnInit();

    bool ShowImages() const { return m_showImages; }

private:
    bool m_showImages, m_showButtons;
};

IMPLEMENT_APP_NO_MAIN(CMyApp)
DECLARE_APP(CMyApp)

bool CMyApp::OnInit()
{
    return true;
};

class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString& title, const wxPoint& pos = wxDefaultPosition,
                const wxSize& size = wxDefaultSize,
                long style = wxDEFAULT_FRAME_STYLE);

    wxPropertyGrid *pg;

	void ShowContextMenu(const wxPoint& pos, wxString text);

	wxPGProperty *active;

    void OnPropertyGridItemRightClick( wxPropertyGridEvent& event );
    void OnMenuLeftClick(wxCommandEvent& event);

public:
	void start_pg ();
	void add_pg ();

protected:

    DECLARE_EVENT_TABLE()
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_PG_RIGHT_CLICK(wxID_ANY,MyFrame::OnPropertyGridItemRightClick )
	EVT_MENU(wxID_ANY, MyFrame::OnMenuLeftClick)
END_EVENT_TABLE()

/*----------------------------------------------------------------------+
|	start_pg		                                       				|
+----------------------------------------------------------------------*/
void MyFrame::start_pg ()
{
	pg->SetColumnCount(2);

	pg->SetSplitterLeft(true);

	pg->SetSplitterPosition(120);

}

/*----------------------------------------------------------------------+
|	OnMenuLeftClick	                                       				|
+----------------------------------------------------------------------*/
void MyFrame::OnMenuLeftClick(wxCommandEvent& event)
{
	wxPGProperty *p = active;

	wxString name = p->GetLabel();
	wxString data = p->GetValueAsString();

	pg->DeleteProperty ( p );

}

/*----------------------------------------------------------------------+
|	ShowContextMenu                                       				|
+----------------------------------------------------------------------*/
void MyFrame::ShowContextMenu(const wxPoint& pos, wxString text)
{
	wxMenu m_pMenuPopup;

    wxFont font = GetFont();
    font.SetWeight(wxFONTWEIGHT_BOLD);
    wxMenuItem *pItem = new wxMenuItem(&m_pMenuPopup, wxID_ANY, wxString::Format(_T("DEL %s"), text));
    pItem->SetFont(font);
    m_pMenuPopup.Append(pItem);

	PopupMenu (&m_pMenuPopup, pos.x, pos.y);
}

/*----------------------------------------------------------------------+
|	OnPropertyGridItemRightClick                           				|
+----------------------------------------------------------------------*/
void MyFrame::OnPropertyGridItemRightClick( wxPropertyGridEvent& event )
{
	wxPGProperty* p = event.GetProperty();

	if ( !p )
		return;

	if ( p )
		{
//		printf ("%s IsCategory=%i HasVisibleChildren=%i\n", event.GetPropertyLabel(), p->IsCategory(), p->HasVisibleChildren() );

		if (!p->IsCategory())
			{
			active = p;
			ShowContextMenu(ScreenToClient(::wxGetMousePosition()),p->GetLabel());
			}
		}
}

/*----------------------------------------------------------------------+
|	add_pg			                                       				|
+----------------------------------------------------------------------*/
void MyFrame::add_pg ()
{

	wxPGProperty *p = new wxPropertyCategory("category");

	pg->Append (p);

	int size=3;

	for (int i=0; i<size; i++)
		{
		wxString col = wxString::Format(_T("col%ld"), i+1);
		wxString dat = wxString::Format(_T("data%ld"), i+1);

		wxPGProperty *p;

		if(!i)
			{
			wxPGChoices soc;

			int size1=35;

			for (int j=0; j<size1; j++)
				{
				if(j==10)
					soc.Add( wxString::Format(_T("śtest%ld"), j+1), j );
				else
					soc.Add( wxString::Format(_T("choice%ld"), j+1), j );
				}

			p = new wxEnumProperty(col, wxPG_LABEL, soc, 0);
			}
		else
			{
			p = new wxStringProperty(col, wxPG_LABEL, dat);
			}


		if(p)
			{
			pg->Append( p );

			pg->AppendIn( p, new wxStringProperty(wxT("b"),
										wxPG_LABEL,
										wxT("2")) );
			}


		}


}

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const
                wxSize&     size, long style)
: wxFrame((wxFrame *)NULL, -1, title, pos, size, style)
{
    pg = new wxPropertyGrid(this,wxID_ANY,wxDefaultPosition,wxSize(100,100),
                        wxPG_SPLITTER_AUTO_CENTER |
                        wxPG_BOLD_MODIFIED );

	start_pg ();
	add_pg ();

	int x=0,y=0;
	int width=240,height=240;

	SetSize( x, y, width, height );


//    SetSizerAndFit (sizerBtns);

}

extern "C"
void __declspec(dllexport)
ShowDlg(HWND parent)
{

        MyFrame *frame = new MyFrame(_("Test"));
        frame->Centre();
        frame->Show();


}

This my EXE code

Code: Select all

#include "windows.h"

int APIENTRY WinMain(HINSTANCE hInstance,
                                           HINSTANCE hPrevInstance,
                                           LPTSTR    lpCmdLine,
                                           int       nCmdShow);

#include "assert.h"
typedef void (*PF)(HWND);

typedef void (*DLLFunctionPtr) (HWND);
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);

// Entry point
int APIENTRY WinMain(HINSTANCE hInstance,
                                           HINSTANCE hPrevInstance,
                                           LPTSTR    lpCmdLine,
                                           int       nCmdShow)
{
        WNDCLASSEX wcx;

        wcx.cbSize = sizeof(wcx);
        wcx.style = CS_HREDRAW | CS_VREDRAW;
        wcx.lpfnWndProc = MainWndProc;
        wcx.cbClsExtra = 0;
        wcx.cbWndExtra = 0;
        wcx.hInstance = hInstance;
        wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
        wcx.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 );
        wcx.lpszMenuName =  "MainMenu";
        wcx.lpszClassName = "MainWClass";
        wcx.hIconSm = NULL;

        ATOM a = RegisterClassEx(&wcx);
        assert(a);

        HWND hwnd = CreateWindow("MainWClass",
                                                        "Test DLL",
                                                        WS_OVERLAPPEDWINDOW,
                                                        CW_USEDEFAULT,
                                                        CW_USEDEFAULT,
                                                        CW_USEDEFAULT,
                                                        CW_USEDEFAULT,
                                                        (HWND) NULL,
                                                        (HMENU) NULL,
                                                        hInstance,
                                                        (LPVOID) NULL);
        assert(hwnd);

        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);

        long bRet;
        MSG msg;
        while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0 )
        {
                if (bRet != -1)
                {
                        TranslateMessage( &msg );
                        DispatchMessage( &msg );
                }
        }

        return 0;
}

// Windows Callback Procedure
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
        PAINTSTRUCT ps;
        HDC hDC;

        switch( msg ) {
          case WM_PAINT:
                  hDC = BeginPaint( hWnd, &ps );
                  TextOut( hDC, 10, 10, "Left Click on this form to launch function: DLLFunction", 55 );
                  TextOut( hDC, 10, 30, "Right Click on this form to launch function: TestReport", 55 );
                  EndPaint( hWnd, &ps );
                  break;

          case WM_DESTROY:
                  PostQuitMessage( 0 );
                  break;

          case WM_LBUTTONUP:
                  {
					  HMODULE h = LoadLibrary("FromDLL.dll");
					  if( h )
					  {
						  PF pf = (PF)GetProcAddress(h, "ShowDlg");
						  if( pf )
						  {
							  pf(hWnd);
						  }
					  }

                  }
                  break;

          case WM_RBUTTONUP:
                  {
                          // Load up the DLL and call DLLFunction(
                                HMODULE hModule = LoadLibrary("FromDLL.dll");
                                assert(hModule);
                                DLLFunctionPtr pProc = (DLLFunctionPtr)GetProcAddress(hModule, "DLLFunction");
                                assert(pProc);
                                (pProc)(hWnd);
                                FreeLibrary(hModule);
                  }
                  break;

          default:
                  return( DefWindowProc( hWnd, msg, wParam, lParam ));
        }
        return 0;
}


Thanks for any help
http://forums.wxwidgets.org/posting.php?mode=post&f=1#
Regards
Greg

Post Reply