X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e3e65dac0c4e7ad19e3c270caa1e0eea138e5d8d..1fd8a4504da7215d889e3e6234476581b0ba7adf:/samples/dnd/dnd.cpp diff --git a/samples/dnd/dnd.cpp b/samples/dnd/dnd.cpp index 9c61559838..6a42410921 100644 --- a/samples/dnd/dnd.cpp +++ b/samples/dnd/dnd.cpp @@ -4,36 +4,52 @@ // Author: Vadim Zeitlin // Modified by: // Created: 04/01/98 -// RCS-ID: +// RCS-ID: $Id$ // Copyright: -// Licence: wxWindows licence +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#include "wx/wxprec.h" +#include #ifdef __BORLANDC__ #pragma hdrstop #endif #ifndef WX_PRECOMP -#include "wx/wx.h" +#include #endif -#include "wx/intl.h" -#include "wx/log.h" +#include +#include -#include "wx/dnd.h" +#include + +#ifdef __WXMOTIF__ +#error Sorry, drag and drop is not yet implemented on wxMotif. +#endif + +#if defined(__WXGTK__) || defined(__WXMOTIF__) +#include "mondrian.xpm" +#endif // ---------------------------------------------------------------------------- -// Derive 2 simple classes which just put in the listbox the strings (text or +// Derive two simple classes which just put in the listbox the strings (text or // file names) we drop on them // ---------------------------------------------------------------------------- + +// FIXME this is ugly and should be fixed in the library itself +#ifdef __WXMSW__ + typedef long wxDropPointCoord; +#else // wxGTK + typedef int wxDropPointCoord; +#endif // MSW/GTK + class DnDText : public wxTextDropTarget { public: DnDText(wxListBox *pOwner) { m_pOwner = pOwner; } - virtual bool OnDropText(long x, long y, const char *psz); + virtual bool OnDropText(wxDropPointCoord x, wxDropPointCoord y, const wxChar* psz ); private: wxListBox *m_pOwner; @@ -44,8 +60,8 @@ 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[]); + virtual bool OnDropFiles(wxDropPointCoord x, wxDropPointCoord y, + size_t nFiles, const wxChar* const aszFiles[] ); private: wxListBox *m_pOwner; @@ -54,8 +70,9 @@ private: // ---------------------------------------------------------------------------- // Define a new application type // ---------------------------------------------------------------------------- + class DnDApp : public wxApp -{ +{ public: bool OnInit(); }; @@ -66,7 +83,7 @@ IMPLEMENT_APP(DnDApp); // Define a new frame type // ---------------------------------------------------------------------------- class DnDFrame : public wxFrame -{ +{ public: DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h); ~DnDFrame(); @@ -78,10 +95,9 @@ public: void OnHelp (wxCommandEvent& event); void OnLogClear(wxCommandEvent& event); - void OnMouseBtnDown(wxMouseEvent& event); + void OnLeftDown(wxMouseEvent& event); + void OnRightDown(wxMouseEvent& event); - bool OnClose(); - DECLARE_EVENT_TABLE() private: @@ -97,6 +113,7 @@ private: // ---------------------------------------------------------------------------- // IDs for the menu commands // ---------------------------------------------------------------------------- + enum { Menu_Quit = 1, @@ -112,17 +129,16 @@ BEGIN_EVENT_TABLE(DnDFrame, wxFrame) EVT_MENU(Menu_Drag, DnDFrame::OnDrag) EVT_MENU(Menu_Help, DnDFrame::OnHelp) EVT_MENU(Menu_Clear, DnDFrame::OnLogClear) - - EVT_LEFT_DOWN(OnMouseBtnDown) - EVT_RIGHT_DOWN(OnMouseBtnDown) - EVT_MIDDLE_DOWN(OnMouseBtnDown) + EVT_LEFT_DOWN( DnDFrame::OnLeftDown) + EVT_RIGHT_DOWN( DnDFrame::OnRightDown) + EVT_PAINT( DnDFrame::OnPaint) END_EVENT_TABLE() // `Main program' equivalent, creating windows and returning main app frame -bool DnDApp::OnInit(void) +bool DnDApp::OnInit() { // create the main frame window - DnDFrame *frame = new DnDFrame(NULL, "Drag & Drop wxWindows App", + DnDFrame *frame = new DnDFrame((wxFrame *) NULL, "Drag & Drop wxWindows App", 50, 50, 450, 340); // activate it @@ -138,13 +154,12 @@ DnDFrame::DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h) m_strText("wxWindows drag & drop works :-)") { +// SetBackgroundColour(* wxWHITE); -#ifdef __WXMSW__ // frame icon and status bar - SetIcon(wxIcon("mondrian")); -#endif - - const int widths[] = { -1 }; + SetIcon(wxICON(mondrian)); + +// const int widths[] = { -1 }; CreateStatusBar(); // construct menu @@ -167,7 +182,7 @@ DnDFrame::DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h) menu_bar->Append(help_menu, "&Help"); SetMenuBar(menu_bar); - + // make a panel with 3 subwindows wxPoint pos(0, 0); wxSize size(400, 200); @@ -177,24 +192,24 @@ DnDFrame::DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h) m_ctrlFile = new wxListBox(this, -1, pos, size, 1, &strFile, wxLB_HSCROLL); m_ctrlText = new wxListBox(this, -1, pos, size, 1, &strText, wxLB_HSCROLL); - m_ctrlLog = new wxTextCtrl(this, -1, "", pos, size, - wxTE_MULTILINE | wxTE_READONLY | - wxSUNKEN_BORDER| wxHSCROLL); + m_ctrlLog = new wxTextCtrl(this, -1, "", pos, size, + wxTE_MULTILINE | wxTE_READONLY | + wxSUNKEN_BORDER ); // redirect log messages to the text window (don't forget to delete it!) m_pLog = new wxLogTextCtrl(m_ctrlLog); m_pLogPrev = wxLog::SetActiveTarget(m_pLog); // associate drop targets with 2 text controls -// m_ctrlFile->SetDropTarget(new DnDFile(m_ctrlFile)); - m_ctrlText->SetDropTarget(new DnDText(m_ctrlText)); + m_ctrlFile->SetDropTarget(new DnDFile(m_ctrlFile)); + m_ctrlText->SetDropTarget(new DnDText(m_ctrlText)); - wxLayoutConstraints *c; + wxLayoutConstraints *c; // Top-left listbox c = new wxLayoutConstraints; - c->left.SameAs (this, wxLeft); - c->top.SameAs (this, wxTop); + c->left.SameAs(this, wxLeft); + c->top.SameAs(this, wxTop); c->right.PercentOf(this, wxRight, 50); c->height.PercentOf(this, wxHeight, 40); m_ctrlFile->SetConstraints(c); @@ -213,7 +228,6 @@ DnDFrame::DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h) c->right.SameAs (this, wxRight); c->height.PercentOf(this, wxHeight, 40); c->top.SameAs(m_ctrlText, wxBottom); - m_ctrlLog->SetConstraints(c); SetAutoLayout(TRUE); @@ -224,6 +238,17 @@ void DnDFrame::OnQuit(wxCommandEvent& /* event */) Close(TRUE); } +void DnDFrame::OnPaint(wxPaintEvent& /*event*/) +{ + int w = 0; + int h = 0; + GetClientSize( &w, &h ); + + wxPaintDC dc(this); + dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL, FALSE, "charter" ) ); + dc.DrawText( "Drag text from here!", 20, h-35 ); +} + void DnDFrame::OnDrag(wxCommandEvent& /* event */) { wxString strText = wxGetTextFromUser @@ -240,7 +265,7 @@ void DnDFrame::OnDrag(wxCommandEvent& /* event */) void DnDFrame::OnAbout(wxCommandEvent& /* event */) { - wxMessageDialog dialog(this, + wxMessageDialog dialog(this, "Drag-&-Drop Demo\n" "Please see \"Help|Help...\" for details\n" "Copyright (c) 1998 Vadim Zeitlin", @@ -251,28 +276,26 @@ void DnDFrame::OnAbout(wxCommandEvent& /* event */) void DnDFrame::OnHelp(wxCommandEvent& /* event */) { - wxMessageDialog dialog(this, -"This small program demonstrates drag & drop support in wxWindows. " -"The program window consists of 3 parts: the bottom pane is for " -"debug messages, so that you can see what's going on inside. " -"The top part is split into 2 listboxes, the left one accepts " -"files and the right one accepts text." -"\n\n" -"To test wxDropTarget: open wordpad (write.exe), select some text in " -"it and drag it to the right listbox (you'll notice the usual visual " -"feedback, i.e. the cursor will change). Also, try dragging some " -"files (you can select several at once) from Windows Explorer (or " -"File Manager) to the left pane. Hold down Ctrl/Shift keys when " -"you drop text (doesn't work with files) and see what changes. " -"\n\n" -"To test wxDropSource: just press any mouse button on the empty zone of " -"the window and drag it to wordpad or any other droptarget accepting " -"text (and of course you can just drag it to the right pane). Due to " -"a lot of trace messages, the cursor might take some time to change, " -"don't release the mouse button until it does. You can change the " -"string being dragged in in \"File|Test drag...\" dialog." -"\n\n" -"Please send all questions/bug reports/suggestions &c to " + wxMessageDialog dialog(this, +"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" +"going on inside. The top part is split into 2 listboxes, the left one accepts files\n" +"and the right one accepts text.\n" +"\n" +"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" +"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" +"work with files) and see what changes.\n" +"\n" +"To test wxDropSource: just press any mouse button on the empty zone of the window and drag\n" +"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" +"change, don't release the mouse button until it does. You can change the string being\n" +"dragged in in \"File|Test drag...\" dialog.\n" +"\n" +"\n" +"Please send all questions/bug reports/suggestions &c to \n" "Vadim Zeitlin ", "wxDnD Help"); @@ -284,25 +307,26 @@ void DnDFrame::OnLogClear(wxCommandEvent& /* event */ ) m_ctrlLog->Clear(); } -bool DnDFrame::OnClose() -{ - return TRUE; -} - -void DnDFrame::OnMouseBtnDown(wxMouseEvent& /* event */ ) +void DnDFrame::OnLeftDown(wxMouseEvent &WXUNUSED(event) ) { - if ( !m_strText.IsEmpty() ) { + if ( !m_strText.IsEmpty() ) + { // start drag operation - wxTextDataObject data(m_strText); - wxDropSource dragSource(data, this); +#ifdef __WXMSW__ + wxTextDataObject textData(m_strText); + wxDropSource dragSource( textData, this ); +#else + wxDropSource dragSource( new wxTextDataObject (m_strText), this, wxIcon(mondrian_xpm) ); +#endif const char *pc; - switch ( dragSource.DoDragDrop(TRUE) ) { - case wxDropSource::Error: pc = "Error!"; break; - case wxDropSource::None: pc = "Nothing"; break; - case wxDropSource::Copy: pc = "Copied"; break; - case wxDropSource::Move: pc = "Moved"; break; - case wxDropSource::Cancel: pc = "Cancelled"; break; + switch ( dragSource.DoDragDrop(TRUE) ) + { + 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; default: pc = "Huh?"; break; } @@ -310,6 +334,17 @@ void DnDFrame::OnMouseBtnDown(wxMouseEvent& /* event */ ) } } +void DnDFrame::OnRightDown(wxMouseEvent &event ) +{ + wxMenu *menu = new wxMenu; + + menu->Append(Menu_Drag, "&Test drag..."); + menu->Append(Menu_About, "&About"); + menu->Append(Menu_Quit, "E&xit"); + + PopupMenu( menu, event.GetX(), event.GetY() ); +} + DnDFrame::~DnDFrame() { if ( m_pLog != NULL ) { @@ -321,18 +356,18 @@ DnDFrame::~DnDFrame() // ---------------------------------------------------------------------------- // Notifications called by the base class // ---------------------------------------------------------------------------- -bool DnDText::OnDropText(long, long, const char *psz) +bool DnDText::OnDropText( wxDropPointCoord, wxDropPointCoord, const wxChar *psz ) { m_pOwner->Append(psz); return TRUE; } -bool DnDFile::OnDropFiles(long, long, size_t nFiles, - const char * const aszFiles[]) +bool DnDFile::OnDropFiles( wxDropPointCoord, wxDropPointCoord, size_t nFiles, + const wxChar* const aszFiles[]) { wxString str; - str.Printf("%d files dropped", nFiles); + str.Printf( _T("%d files dropped"), nFiles); m_pOwner->Append(str); for ( size_t n = 0; n < nFiles; n++ ) { m_pOwner->Append(aszFiles[n]);