1998-06-13 10:09:13 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
1998-05-22 19:57:05 +00:00
|
|
|
// Name: dnd.cpp
|
|
|
|
// Purpose: Drag and drop sample
|
|
|
|
// Author: Vadim Zeitlin
|
1998-06-13 10:09:13 +00:00
|
|
|
// Modified by:
|
|
|
|
// Created: 04/01/98
|
1998-12-09 17:30:17 +00:00
|
|
|
// RCS-ID: $Id$
|
1998-06-13 10:09:13 +00:00
|
|
|
// Copyright:
|
1998-12-09 17:30:17 +00:00
|
|
|
// Licence: wxWindows licence
|
1998-06-13 10:09:13 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1998-05-22 19:57:05 +00:00
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/intl.h"
|
|
|
|
#include "wx/log.h"
|
|
|
|
|
|
|
|
#include "wx/dnd.h"
|
|
|
|
|
1998-07-31 20:04:04 +00:00
|
|
|
#ifdef __WXGTK__
|
|
|
|
#include "mondrian.xpm"
|
|
|
|
#endif
|
|
|
|
|
1998-05-22 19:57:05 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-01-13 18:21:04 +00:00
|
|
|
// Derive two simple classes which just put in the listbox the strings (text or
|
1998-05-22 19:57:05 +00:00
|
|
|
// file names) we drop on them
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
class DnDText : public wxTextDropTarget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DnDText(wxListBox *pOwner) { m_pOwner = pOwner; }
|
|
|
|
|
|
|
|
virtual bool OnDropText(long x, long y, const char *psz);
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxListBox *m_pOwner;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DnDFile : public wxFileDropTarget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DnDFile(wxListBox *pOwner) { m_pOwner = pOwner; }
|
|
|
|
|
|
|
|
virtual bool OnDropFiles(long x, long y,
|
|
|
|
size_t nFiles, const char * const aszFiles[]);
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxListBox *m_pOwner;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Define a new application type
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
class DnDApp : public wxApp
|
1998-12-09 17:30:17 +00:00
|
|
|
{
|
1998-05-22 19:57:05 +00:00
|
|
|
public:
|
|
|
|
bool OnInit();
|
|
|
|
};
|
|
|
|
|
|
|
|
IMPLEMENT_APP(DnDApp);
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Define a new frame type
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
class DnDFrame : public wxFrame
|
1998-12-09 17:30:17 +00:00
|
|
|
{
|
1998-05-22 19:57:05 +00:00
|
|
|
public:
|
|
|
|
DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
|
|
|
|
~DnDFrame();
|
|
|
|
|
|
|
|
void OnPaint(wxPaintEvent& event);
|
|
|
|
void OnQuit (wxCommandEvent& event);
|
|
|
|
void OnAbout(wxCommandEvent& event);
|
1998-06-13 10:09:13 +00:00
|
|
|
void OnDrag (wxCommandEvent& event);
|
1998-05-22 19:57:05 +00:00
|
|
|
void OnHelp (wxCommandEvent& event);
|
1998-06-13 10:09:13 +00:00
|
|
|
void OnLogClear(wxCommandEvent& event);
|
|
|
|
|
1998-08-09 13:06:25 +00:00
|
|
|
void OnLeftDown(wxMouseEvent& event);
|
|
|
|
void OnRightDown(wxMouseEvent& event);
|
1998-12-09 17:30:17 +00:00
|
|
|
|
1998-05-22 19:57:05 +00:00
|
|
|
bool OnClose();
|
1998-12-09 17:30:17 +00:00
|
|
|
|
1998-05-22 19:57:05 +00:00
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxListBox *m_ctrlFile,
|
|
|
|
*m_ctrlText;
|
|
|
|
wxTextCtrl *m_ctrlLog;
|
|
|
|
|
1998-06-13 10:09:13 +00:00
|
|
|
wxLog *m_pLog, *m_pLogPrev;
|
|
|
|
|
|
|
|
wxString m_strText;
|
1998-05-22 19:57:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// IDs for the menu commands
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
Menu_Quit = 1,
|
1998-06-13 10:09:13 +00:00
|
|
|
Menu_Drag,
|
1998-05-22 19:57:05 +00:00
|
|
|
Menu_About = 101,
|
|
|
|
Menu_Help,
|
1998-06-13 10:09:13 +00:00
|
|
|
Menu_Clear,
|
1998-05-22 19:57:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(DnDFrame, wxFrame)
|
1998-06-13 10:09:13 +00:00
|
|
|
EVT_MENU(Menu_Quit, DnDFrame::OnQuit)
|
|
|
|
EVT_MENU(Menu_About, DnDFrame::OnAbout)
|
|
|
|
EVT_MENU(Menu_Drag, DnDFrame::OnDrag)
|
|
|
|
EVT_MENU(Menu_Help, DnDFrame::OnHelp)
|
|
|
|
EVT_MENU(Menu_Clear, DnDFrame::OnLogClear)
|
1998-12-08 08:40:00 +00:00
|
|
|
EVT_LEFT_DOWN( DnDFrame::OnLeftDown)
|
|
|
|
EVT_RIGHT_DOWN( DnDFrame::OnRightDown)
|
|
|
|
EVT_PAINT( DnDFrame::OnPaint)
|
1998-05-22 19:57:05 +00:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
// `Main program' equivalent, creating windows and returning main app frame
|
1998-12-09 17:30:17 +00:00
|
|
|
bool DnDApp::OnInit()
|
1998-05-22 19:57:05 +00:00
|
|
|
{
|
|
|
|
// create the main frame window
|
1998-12-09 17:30:17 +00:00
|
|
|
DnDFrame *frame = new DnDFrame((wxFrame *) NULL, "Drag & Drop wxWindows App",
|
1998-05-22 19:57:05 +00:00
|
|
|
50, 50, 450, 340);
|
|
|
|
|
|
|
|
// activate it
|
|
|
|
frame->Show(TRUE);
|
|
|
|
|
|
|
|
SetTopWindow(frame);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DnDFrame::DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h)
|
1998-06-13 10:09:13 +00:00
|
|
|
: wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h)),
|
|
|
|
m_strText("wxWindows drag & drop works :-)")
|
|
|
|
|
1998-05-22 19:57:05 +00:00
|
|
|
{
|
1999-01-13 18:01:39 +00:00
|
|
|
SetBackgroundColour(* wxWHITE);
|
1998-07-27 20:50:48 +00:00
|
|
|
|
1998-06-13 10:09:13 +00:00
|
|
|
// frame icon and status bar
|
1998-12-08 08:40:00 +00:00
|
|
|
SetIcon(wxICON(mondrian));
|
1998-12-09 17:30:17 +00:00
|
|
|
|
1998-09-12 17:18:12 +00:00
|
|
|
// const int widths[] = { -1 };
|
1998-06-13 10:09:13 +00:00
|
|
|
CreateStatusBar();
|
1998-05-22 19:57:05 +00:00
|
|
|
|
|
|
|
// construct menu
|
|
|
|
wxMenu *file_menu = new wxMenu;
|
1998-06-13 10:09:13 +00:00
|
|
|
file_menu->Append(Menu_Drag, "&Test drag...");
|
1998-05-22 19:57:05 +00:00
|
|
|
file_menu->AppendSeparator();
|
|
|
|
file_menu->Append(Menu_Quit, "E&xit");
|
1998-06-13 10:09:13 +00:00
|
|
|
|
|
|
|
wxMenu *log_menu = new wxMenu;
|
|
|
|
log_menu->Append(Menu_Clear, "Clear");
|
|
|
|
|
|
|
|
wxMenu *help_menu = new wxMenu;
|
|
|
|
help_menu->Append(Menu_Help, "&Help...");
|
|
|
|
help_menu->AppendSeparator();
|
|
|
|
help_menu->Append(Menu_About, "&About");
|
|
|
|
|
1998-05-22 19:57:05 +00:00
|
|
|
wxMenuBar *menu_bar = new wxMenuBar;
|
|
|
|
menu_bar->Append(file_menu, "&File");
|
1998-06-13 10:09:13 +00:00
|
|
|
menu_bar->Append(log_menu, "&Log");
|
|
|
|
menu_bar->Append(help_menu, "&Help");
|
|
|
|
|
1998-05-22 19:57:05 +00:00
|
|
|
SetMenuBar(menu_bar);
|
1998-12-09 17:30:17 +00:00
|
|
|
|
1998-05-22 19:57:05 +00:00
|
|
|
// make a panel with 3 subwindows
|
|
|
|
wxPoint pos(0, 0);
|
|
|
|
wxSize size(400, 200);
|
|
|
|
|
|
|
|
wxString strFile("Drop files here!"), strText("Drop text on me");
|
|
|
|
|
|
|
|
m_ctrlFile = new wxListBox(this, -1, pos, size, 1, &strFile, wxLB_HSCROLL);
|
|
|
|
m_ctrlText = new wxListBox(this, -1, pos, size, 1, &strText, wxLB_HSCROLL);
|
1998-07-27 20:50:48 +00:00
|
|
|
|
1998-12-09 17:30:17 +00:00
|
|
|
m_ctrlLog = new wxTextCtrl(this, -1, "", pos, size,
|
|
|
|
wxTE_MULTILINE | wxTE_READONLY |
|
1998-05-22 19:57:05 +00:00
|
|
|
wxSUNKEN_BORDER| wxHSCROLL);
|
|
|
|
|
|
|
|
// redirect log messages to the text window (don't forget to delete it!)
|
1998-06-13 10:09:13 +00:00
|
|
|
m_pLog = new wxLogTextCtrl(m_ctrlLog);
|
|
|
|
m_pLogPrev = wxLog::SetActiveTarget(m_pLog);
|
1998-05-22 19:57:05 +00:00
|
|
|
|
|
|
|
// associate drop targets with 2 text controls
|
1998-08-12 20:30:23 +00:00
|
|
|
m_ctrlFile->SetDropTarget(new DnDFile(m_ctrlFile));
|
1998-12-09 17:30:17 +00:00
|
|
|
m_ctrlText->SetDropTarget(new DnDText(m_ctrlText));
|
1998-05-22 19:57:05 +00:00
|
|
|
|
1998-08-12 20:30:23 +00:00
|
|
|
wxLayoutConstraints *c;
|
1998-05-22 19:57:05 +00:00
|
|
|
|
1998-06-13 17:09:59 +00:00
|
|
|
// Top-left listbox
|
1998-05-22 19:57:05 +00:00
|
|
|
c = new wxLayoutConstraints;
|
1998-12-09 17:30:17 +00:00
|
|
|
c->left.SameAs(this, wxLeft);
|
|
|
|
c->top.SameAs(this, wxTop);
|
1998-05-22 19:57:05 +00:00
|
|
|
c->right.PercentOf(this, wxRight, 50);
|
1998-06-13 10:09:13 +00:00
|
|
|
c->height.PercentOf(this, wxHeight, 40);
|
1998-05-22 19:57:05 +00:00
|
|
|
m_ctrlFile->SetConstraints(c);
|
|
|
|
|
1998-06-13 17:09:59 +00:00
|
|
|
// Top-right listbox
|
1998-05-22 19:57:05 +00:00
|
|
|
c = new wxLayoutConstraints;
|
|
|
|
c->left.SameAs (m_ctrlFile, wxRight);
|
|
|
|
c->top.SameAs (this, wxTop);
|
|
|
|
c->right.SameAs (this, wxRight);
|
1998-06-13 10:09:13 +00:00
|
|
|
c->height.PercentOf(this, wxHeight, 40);
|
1998-05-22 19:57:05 +00:00
|
|
|
m_ctrlText->SetConstraints(c);
|
|
|
|
|
1998-06-13 17:09:59 +00:00
|
|
|
// Lower text control
|
1998-05-22 19:57:05 +00:00
|
|
|
c = new wxLayoutConstraints;
|
|
|
|
c->left.SameAs (this, wxLeft);
|
|
|
|
c->right.SameAs (this, wxRight);
|
1998-06-13 10:09:13 +00:00
|
|
|
c->height.PercentOf(this, wxHeight, 40);
|
|
|
|
c->top.SameAs(m_ctrlText, wxBottom);
|
1998-05-22 19:57:05 +00:00
|
|
|
m_ctrlLog->SetConstraints(c);
|
|
|
|
|
|
|
|
SetAutoLayout(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DnDFrame::OnQuit(wxCommandEvent& /* event */)
|
|
|
|
{
|
|
|
|
Close(TRUE);
|
|
|
|
}
|
|
|
|
|
1998-12-08 08:40:00 +00:00
|
|
|
void DnDFrame::OnPaint(wxPaintEvent& /*event*/)
|
|
|
|
{
|
|
|
|
int w = 0;
|
|
|
|
int h = 0;
|
|
|
|
GetClientSize( &w, &h );
|
1998-12-09 17:30:17 +00:00
|
|
|
|
1998-12-08 08:40:00 +00:00
|
|
|
wxPaintDC dc(this);
|
|
|
|
dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL ) );
|
1999-01-13 18:01:39 +00:00
|
|
|
dc.DrawText( "Drag text from here!", 20, h-30 );
|
1998-12-08 08:40:00 +00:00
|
|
|
}
|
|
|
|
|
1998-06-13 10:09:13 +00:00
|
|
|
void DnDFrame::OnDrag(wxCommandEvent& /* event */)
|
|
|
|
{
|
|
|
|
wxString strText = wxGetTextFromUser
|
|
|
|
(
|
|
|
|
"After you enter text in this dialog, press any mouse\n"
|
|
|
|
"button in the bottom (empty) part of the frame and \n"
|
|
|
|
"drag it anywhere - you will be in fact dragging the\n"
|
|
|
|
"text object containing this text",
|
|
|
|
"Please enter some text", m_strText, this
|
|
|
|
);
|
|
|
|
|
|
|
|
m_strText = strText;
|
|
|
|
}
|
|
|
|
|
1998-05-22 19:57:05 +00:00
|
|
|
void DnDFrame::OnAbout(wxCommandEvent& /* event */)
|
|
|
|
{
|
1998-12-09 17:30:17 +00:00
|
|
|
wxMessageDialog dialog(this,
|
1998-05-22 19:57:05 +00:00
|
|
|
"Drag-&-Drop Demo\n"
|
1998-06-13 10:09:13 +00:00
|
|
|
"Please see \"Help|Help...\" for details\n"
|
|
|
|
"Copyright (c) 1998 Vadim Zeitlin",
|
1998-05-22 19:57:05 +00:00
|
|
|
"About wxDnD");
|
|
|
|
|
|
|
|
dialog.ShowModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DnDFrame::OnHelp(wxCommandEvent& /* event */)
|
|
|
|
{
|
1998-12-09 17:30:17 +00:00
|
|
|
wxMessageDialog dialog(this,
|
1998-08-02 20:38:05 +00:00
|
|
|
"This small program demonstrates drag & drop support in wxWindows. The program window\n"
|
|
|
|
"consists of 3 parts: the bottom pane is for debug messages, so that you can see what's\n"
|
1998-12-09 17:30:17 +00:00
|
|
|
"going on inside. The top part is split into 2 listboxes, the left one accepts files\n"
|
1998-08-12 20:30:23 +00:00
|
|
|
"and the right one accepts text.\n"
|
1998-08-02 20:38:05 +00:00
|
|
|
"\n"
|
1998-12-09 17:30:17 +00:00
|
|
|
"To test wxDropTarget: open wordpad (write.exe), select some text in it and drag it to\n"
|
|
|
|
"the right listbox (you'll notice the usual visual feedback, i.e. the cursor will change).\n"
|
1998-08-02 20:38:05 +00:00
|
|
|
"Also, try dragging some files (you can select several at once) from Windows Explorer (or \n"
|
|
|
|
"File Manager) to the left pane. Hold down Ctrl/Shift keys when you drop text (doesn't \n"
|
1998-08-12 20:30:23 +00:00
|
|
|
"work with files) and see what changes.\n"
|
1998-08-02 20:38:05 +00:00
|
|
|
"\n"
|
1998-12-09 17:30:17 +00:00
|
|
|
"To test wxDropSource: just press any mouse button on the empty zone of the window and drag\n"
|
1998-08-02 20:38:05 +00:00
|
|
|
"it to wordpad or any other droptarget accepting text (and of course you can just drag it\n"
|
|
|
|
"to the right pane). Due to a lot of trace messages, the cursor might take some time to \n"
|
1998-12-09 17:30:17 +00:00
|
|
|
"change, don't release the mouse button until it does. You can change the string being\n"
|
1998-08-02 20:38:05 +00:00
|
|
|
"dragged in in \"File|Test drag...\" dialog.\n"
|
1998-08-12 20:30:23 +00:00
|
|
|
"\n"
|
|
|
|
"\n"
|
1998-08-02 20:38:05 +00:00
|
|
|
"Please send all questions/bug reports/suggestions &c to \n"
|
1998-05-22 19:57:05 +00:00
|
|
|
"Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>",
|
|
|
|
"wxDnD Help");
|
|
|
|
|
|
|
|
dialog.ShowModal();
|
|
|
|
}
|
|
|
|
|
1998-07-27 20:50:48 +00:00
|
|
|
void DnDFrame::OnLogClear(wxCommandEvent& /* event */ )
|
1998-06-13 10:09:13 +00:00
|
|
|
{
|
1998-12-09 17:30:17 +00:00
|
|
|
m_ctrlLog->Clear();
|
1998-06-13 10:09:13 +00:00
|
|
|
}
|
|
|
|
|
1998-12-09 17:30:17 +00:00
|
|
|
bool DnDFrame::OnClose()
|
|
|
|
{
|
|
|
|
return TRUE;
|
1998-05-22 19:57:05 +00:00
|
|
|
}
|
|
|
|
|
1998-08-09 13:06:25 +00:00
|
|
|
void DnDFrame::OnLeftDown(wxMouseEvent &WXUNUSED(event) )
|
1998-06-13 10:09:13 +00:00
|
|
|
{
|
|
|
|
if ( !m_strText.IsEmpty() ) {
|
|
|
|
// start drag operation
|
1998-12-09 17:30:17 +00:00
|
|
|
|
1998-06-13 10:09:13 +00:00
|
|
|
wxTextDataObject data(m_strText);
|
1998-07-27 20:50:48 +00:00
|
|
|
wxDropSource dragSource(data, this);
|
1998-06-13 10:09:13 +00:00
|
|
|
const char *pc;
|
|
|
|
|
|
|
|
switch ( dragSource.DoDragDrop(TRUE) ) {
|
1998-09-17 16:57:01 +00:00
|
|
|
case wxDragError: pc = "Error!"; break;
|
|
|
|
case wxDragNone: pc = "Nothing"; break;
|
|
|
|
case wxDragCopy: pc = "Copied"; break;
|
|
|
|
case wxDragMove: pc = "Moved"; break;
|
|
|
|
case wxDragCancel: pc = "Cancelled"; break;
|
1998-06-13 10:09:13 +00:00
|
|
|
default: pc = "Huh?"; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetStatusText(wxString("Drag result: ") + pc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-08-09 13:06:25 +00:00
|
|
|
void DnDFrame::OnRightDown(wxMouseEvent &event )
|
|
|
|
{
|
|
|
|
wxMenu *menu = new wxMenu;
|
1998-12-09 17:30:17 +00:00
|
|
|
|
1998-08-09 13:06:25 +00:00
|
|
|
menu->Append(Menu_Drag, "&Test drag...");
|
|
|
|
menu->Append(Menu_About, "&About");
|
|
|
|
menu->Append(Menu_Quit, "E&xit");
|
1998-12-09 17:30:17 +00:00
|
|
|
|
1998-08-09 13:06:25 +00:00
|
|
|
PopupMenu( menu, event.GetX(), event.GetY() );
|
|
|
|
}
|
|
|
|
|
1998-05-22 19:57:05 +00:00
|
|
|
DnDFrame::~DnDFrame()
|
|
|
|
{
|
|
|
|
if ( m_pLog != NULL ) {
|
1998-06-13 10:09:13 +00:00
|
|
|
if ( wxLog::SetActiveTarget(m_pLogPrev) == m_pLog )
|
1998-05-22 19:57:05 +00:00
|
|
|
delete m_pLog;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Notifications called by the base class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
bool DnDText::OnDropText(long, long, const char *psz)
|
|
|
|
{
|
|
|
|
m_pOwner->Append(psz);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
1998-12-09 17:30:17 +00:00
|
|
|
bool DnDFile::OnDropFiles(long, long, size_t nFiles,
|
1998-05-22 19:57:05 +00:00
|
|
|
const char * const aszFiles[])
|
|
|
|
{
|
|
|
|
wxString str;
|
|
|
|
str.Printf("%d files dropped", nFiles);
|
|
|
|
m_pOwner->Append(str);
|
|
|
|
for ( size_t n = 0; n < nFiles; n++ ) {
|
|
|
|
m_pOwner->Append(aszFiles[n]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|