Page 1 of 1

drag and drop

Posted: Mon Oct 11, 2004 3:07 pm
by jdratlif
I am writing an application which decodes a binary record file to plain text.

I had previously created a Java version, but became interested in wxWidgets and decided to rewrite it for a learning project.

Questions:

a) is DnD only in the win32 port? Are there any plans to extend it to other Ports?

b) How can I change the default behavior of a DnD component?

Example: I have a wxTextCtrl. I call DragAcceptFiles(true) on it. When a file is dragged to it, it opens the file. However, since this file is binary, that's no good. I want to change the behavior.

The docs say the default behavior is in OnDropFiles. So I subclassed wxTextCtrl and added the OnDropFiles method with the same signature as in the manual. This method is never called, and the default behavior doesn't go away.

I'm not a great C++ programmer. I've never written a real application in C++. I know C, and Java very well, but am just learning C++. But a lot of concepts still aren't 100% clear to me.

I tried making the method virtual, but this didn't help.

Code: Select all

#ifndef _CHTTEXTCONTROL_H
#define _CHTTEXTCONTROL_H

#include <wx/textctrl.h>
#include <wx/window.h>
#include <wx/event.h>

class CHTTextControl : public wxTextCtrl {
public:
    CHTTextControl(wxWindow *);
    void OnDropFiles(wxDropFilesEvent &);
};

#endif

CHTTextControl::CHTTextControl(wxWindow *parent) : 
    wxTextCtrl(parent, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize,
               wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH,
               wxDefaultValidator, wxTextCtrlNameStr) {
    DragAcceptFiles(true);
    SetFont(wxFont(12, wxMODERN, wxNORMAL, wxNORMAL));
}

void CHTTextControl::OnDropFiles(wxDropFilesEvent &event) {
    // just testing if we removed the default behavior
    printf("CHTTextControl::OnDropFiles was called...\n");
    fflush(stdout);
}
That printf() line is never reached, and the text control still tries to open the file (which is bad).

Any suggestions?

Posted: Mon Oct 11, 2004 9:54 pm
by geon
I think you need this line at the bottom of your class:

DECLARE_EVENT_TABLE()

... and in the .cpp file, there should be something like this:

BEGIN_EVENT_TABLE(CHTTextControl : public wxTextCtrl)
EVT_blablabla_EVENTS(CHTTextControl :: OnDropFiles)
END_EVENT_TABLE()

Posted: Tue Oct 12, 2004 3:01 pm
by jdratlif
geon wrote:I think you need this line at the bottom of your class:

DECLARE_EVENT_TABLE()

... and in the .cpp file, there should be something like this:

BEGIN_EVENT_TABLE(CHTTextControl : public wxTextCtrl)
EVT_blablabla_EVENTS(CHTTextControl :: OnDropFiles)
END_EVENT_TABLE()
I want to try this, but what kind of event would it be? Apologies. I'm new to wxWidgets.

I have seen EVT_MENU_EVENTS. But none of the EVT macros are in the windows compiled help (chm) file in the alphabetical functions and macros list. I'm pretty sure it's not a menu event.

I am also looking into the wxDropTarget which seems similar to the Java DnD API. I might be able to use that.

Posted: Wed Oct 13, 2004 7:19 am
by Lall
The event macro you're searching for is: EVT_DROP_FILES

BEGIN_EVENT_TABLE(CHTTextControl : public wxTextCtrl)
EVT_DROP_FILES(CHTTextControl :: OnDropFiles)
END_EVENT_TABLE()


I've actually never tried that method, I'm using the wxTextDropTarget class and SetDropTarget function of wxWindows class but that's for another application anyway... :)

Last thing, the method I'm using is successfully working under Win32 and Linux; I'm sure it must also work on other platforms ...

Posted: Wed Oct 13, 2004 2:19 pm
by jdratlif
Lall wrote:The event macro you're searching for is: EVT_DROP_FILES

BEGIN_EVENT_TABLE(CHTTextControl : public wxTextCtrl)
EVT_DROP_FILES(CHTTextControl :: OnDropFiles)
END_EVENT_TABLE()


I've actually never tried that method, I'm using the wxTextDropTarget class and SetDropTarget function of wxWindows class but that's for another application anyway... :)

Last thing, the method I'm using is successfully working under Win32 and Linux; I'm sure it must also work on other platforms ...
I should have looked more carefully. I saw the OnDropFiles thing and thought it was an easy way to add DragNDrop. Then it says it windows only. I hadn't thought they would make a win32 only method that for all intents and purposes isn't very useful.

Later I saw the wxDropTarget and looked at the example and I have it working now. But OnDropFiles is only implemented in Windows. I have working DragNDrop in Windows and Linux now for my app.

Thanks.