X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/79ec2ce20ebe8d6ea2bb876e7aedb53801dbeb43..b8aa1680f21faef698d39a2bce9c09e45a5bbf7a:/samples/dnd/dnd.cpp diff --git a/samples/dnd/dnd.cpp b/samples/dnd/dnd.cpp index 7253d69fac..0b26e485d8 100644 --- a/samples/dnd/dnd.cpp +++ b/samples/dnd/dnd.cpp @@ -95,11 +95,15 @@ public: void OnNewFrame(wxCommandEvent& event); void OnHelp (wxCommandEvent& event); void OnLogClear(wxCommandEvent& event); + void OnCopy(wxCommandEvent& event); void OnPaste(wxCommandEvent& event); + void OnCopyBitmap(wxCommandEvent& event); void OnPasteBitmap(wxCommandEvent& event); + void OnCopyFiles(wxCommandEvent& event); + void OnLeftDown(wxMouseEvent& event); void OnRightDown(wxMouseEvent& event); @@ -110,10 +114,11 @@ public: private: wxListBox *m_ctrlFile, - *m_ctrlText; + *m_ctrlText; wxTextCtrl *m_ctrlLog; - wxLog *m_pLog, *m_pLogPrev; + wxLog *m_pLog, + *m_pLogPrev; wxString m_strText; wxBitmap m_bitmap; @@ -212,6 +217,7 @@ public: const wxColour& col) : DnDShape(pos, size, col) { + wxLogMessage("DnDTriangularShape is being created"); } virtual ~DnDTriangularShape() @@ -248,6 +254,7 @@ public: const wxColour& col) : DnDShape(pos, size, col) { + wxLogMessage("DnDRectangularShape is being created"); } virtual ~DnDRectangularShape() @@ -284,6 +291,7 @@ public: const wxColour& col) : DnDShape(pos, size, col) { + wxLogMessage("DnDEllipticShape is being created"); } virtual ~DnDEllipticShape() @@ -345,8 +353,21 @@ public: virtual ~DnDShapeDataObject() { delete m_shape; } - // accessors - DnDShape *GetShape() const { return m_shape; } + // after a call to this function, the shape is owned by the caller and it + // is responsible for deleting it! + // + // NB: a better solution would be to make DnDShapes ref counted and this + // is what should probably be done in a real life program, otherwise + // the ownership problems become too complicated really fast + DnDShape *GetShape() + { + DnDShape *shape = m_shape; + + m_shape = (DnDShape *)NULL; + m_hasBitmap = FALSE; + + return shape; + } // implement base class pure virtuals // ---------------------------------- @@ -403,8 +424,6 @@ public: } else { - wxASSERT_MSG( format == wxDF_BITMAP, "unsupported format" ); - if ( !m_hasBitmap ) CreateBitmap(); @@ -499,7 +518,7 @@ public: void OnDrag(wxMouseEvent& event); void OnPaint(wxPaintEvent& event); - void OnDrop(long x, long y, DnDShape *shape); + void OnDrop(wxCoord x, wxCoord y, DnDShape *shape); private: DnDShape *m_shape; @@ -523,23 +542,24 @@ public: } // override base class (pure) virtuals - virtual void OnEnter() - { m_frame->SetStatusText("Mouse entered the frame"); } + virtual wxDragResult OnEnter(wxCoord x, wxCoord y, wxDragResult def) + { m_frame->SetStatusText("Mouse entered the frame"); + return OnDragOver(x, y, def); } virtual void OnLeave() { m_frame->SetStatusText("Mouse left the frame"); } - virtual bool OnData(wxCoord x, wxCoord y) + virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def) { if ( !GetData() ) { wxLogError("Failed to get drag and drop data"); - return FALSE; + return wxDragNone; } m_frame->OnDrop(x, y, ((DnDShapeDataObject *)GetDataObject())->GetShape()); - return TRUE; + return def; } private: @@ -562,8 +582,7 @@ enum Menu_Paste, Menu_CopyBitmap, Menu_PasteBitmap, - Menu_ToBeGreyed, /* for testing */ - Menu_ToBeDeleted, /* for testing */ + Menu_CopyFiles, Menu_Shape_New = 500, Menu_Shape_Edit, Menu_Shape_Clear, @@ -583,6 +602,7 @@ BEGIN_EVENT_TABLE(DnDFrame, wxFrame) EVT_MENU(Menu_Paste, DnDFrame::OnPaste) EVT_MENU(Menu_CopyBitmap, DnDFrame::OnCopyBitmap) EVT_MENU(Menu_PasteBitmap,DnDFrame::OnPasteBitmap) + EVT_MENU(Menu_CopyFiles, DnDFrame::OnCopyFiles) EVT_UPDATE_UI(Menu_Paste, DnDFrame::OnUpdateUIPasteText) EVT_UPDATE_UI(Menu_PasteBitmap, DnDFrame::OnUpdateUIPasteBitmap) @@ -609,7 +629,7 @@ BEGIN_EVENT_TABLE(DnDShapeFrame, wxFrame) END_EVENT_TABLE() BEGIN_EVENT_TABLE(DnDShapeDialog, wxDialog) - EVT_BUTTON(Button_Colour, OnColour) + EVT_BUTTON(Button_Colour, DnDShapeDialog::OnColour) END_EVENT_TABLE() // ============================================================================ @@ -619,22 +639,50 @@ END_EVENT_TABLE() // `Main program' equivalent, creating windows and returning main app frame bool DnDApp::OnInit() { + // load our ressources + wxPathList pathList; + pathList.Add("."); +#ifdef __WXMSW__ + pathList.Add("./Debug"); + pathList.Add("./Release"); +#endif // wxMSW + + wxString path = pathList.FindValidPath("dnd.wxr"); + if ( !path ) + { + wxLogError("Can't find the resource file dnd.wxr in the current " + "directory, aborting."); + + return FALSE; + } + + wxDefaultResourceTable->ParseResourceFile(path); + + // switch on trace messages +#if defined(__WXGTK__) + wxLog::AddTraceMask(_T("clipboard")); +#elif defined(__WXMSW__) + wxLog::AddTraceMask(wxTRACE_OleCalls); +#endif + #if wxUSE_LIBPNG wxImage::AddHandler( new wxPNGHandler ); #endif + // under X we usually want to use the primary selection by default (which + // is shared with other apps) + wxTheClipboard->UsePrimarySelection(); + // create the main frame window DnDFrame *frame = new DnDFrame((wxFrame *) NULL, "Drag-and-Drop/Clipboard wxWindows Sample", - 50, 50, 450, 340); + 10, 10, 450, 340); // activate it frame->Show(TRUE); SetTopWindow(frame); - wxDefaultResourceTable->ParseResourceFile("dnd.wxr"); - return TRUE; } @@ -657,7 +705,7 @@ DnDFrame::DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h) file_menu->Append(Menu_Quit, "E&xit"); wxMenu *log_menu = new wxMenu; - log_menu->Append(Menu_Clear, "Clear\tDel"); + log_menu->Append(Menu_Clear, "Clear\tCtrl-L"); wxMenu *help_menu = new wxMenu; help_menu->Append(Menu_Help, "&Help..."); @@ -670,6 +718,8 @@ DnDFrame::DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h) clip_menu->AppendSeparator(); clip_menu->Append(Menu_CopyBitmap, "&Copy bitmap\tAlt+C"); clip_menu->Append(Menu_PasteBitmap, "&Paste bitmap\tAlt+V"); + clip_menu->AppendSeparator(); + clip_menu->Append(Menu_CopyFiles, "&Copy files\tCtrl+F"); wxMenuBar *menu_bar = new wxMenuBar; menu_bar->Append(file_menu, "&File"); @@ -694,11 +744,7 @@ DnDFrame::DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h) wxTE_MULTILINE | wxTE_READONLY | wxSUNKEN_BORDER ); -#ifdef __WXMSW__ - // redirect log messages to the text window and switch on OLE messages - // logging - wxLog::AddTraceMask(wxTRACE_OleCalls); -#endif + // redirect log messages to the text window m_pLog = new wxLogTextCtrl(m_ctrlLog); m_pLogPrev = wxLog::SetActiveTarget(m_pLog); @@ -761,11 +807,23 @@ void DnDFrame::OnPaint(wxPaintEvent& WXUNUSED(event)) void DnDFrame::OnUpdateUIPasteText(wxUpdateUIEvent& event) { +#ifdef __WXDEBUG__ + // too many trace messages if we don't do it - this function is called + // very often + wxLogNull nolog; +#endif + event.Enable( wxTheClipboard->IsSupported(wxDF_TEXT) ); } void DnDFrame::OnUpdateUIPasteBitmap(wxUpdateUIEvent& event) { +#ifdef __WXDEBUG__ + // too many trace messages if we don't do it - this function is called + // very often + wxLogNull nolog; +#endif + event.Enable( wxTheClipboard->IsSupported(wxDF_BITMAP) ); } @@ -831,6 +889,8 @@ void DnDFrame::OnHelp(wxCommandEvent& /* event */) void DnDFrame::OnLogClear(wxCommandEvent& /* event */ ) { m_ctrlLog->Clear(); + m_ctrlText->Clear(); + m_ctrlFile->Clear(); } void DnDFrame::OnLeftDown(wxMouseEvent &WXUNUSED(event) ) @@ -839,7 +899,19 @@ void DnDFrame::OnLeftDown(wxMouseEvent &WXUNUSED(event) ) { // start drag operation wxTextDataObject textData(m_strText); - wxDropSource source(textData, this, wxICON(mondrian)); +/* + wxFileDataObject textData; + textData.AddFile( "/file1.txt" ); + textData.AddFile( "/file2.txt" ); +*/ + wxDropSource source(textData, this + +#ifdef __WXMSW__ + ,wxCURSOR_PENCIL, // for copy + wxCURSOR_SPRAYCAN, // for move + wxCURSOR_QUESTION_ARROW // for nothing +#endif + ); const char *pc; @@ -859,18 +931,13 @@ void DnDFrame::OnLeftDown(wxMouseEvent &WXUNUSED(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"); - menu->Append(Menu_ToBeDeleted, "To be deleted"); - menu->Append(Menu_ToBeGreyed, "To be greyed"); + wxMenu menu("Dnd sample menu"); - menu->Delete( Menu_ToBeDeleted ); - menu->Enable( Menu_ToBeGreyed, FALSE ); + menu.Append(Menu_Drag, "&Test drag..."); + menu.AppendSeparator(); + menu.Append(Menu_About, "&About"); - PopupMenu( menu, event.GetX(), event.GetY() ); + PopupMenu( &menu, event.GetX(), event.GetY() ); } DnDFrame::~DnDFrame() @@ -986,6 +1053,58 @@ void DnDFrame::OnPasteBitmap(wxCommandEvent& WXUNUSED(event)) wxTheClipboard->Close(); } +// ---------------------------------------------------------------------------- +// file clipboard +// ---------------------------------------------------------------------------- + +void DnDFrame::OnCopyFiles(wxCommandEvent& WXUNUSED(event)) +{ +#ifdef __WXMSW__ + wxFileDialog dialog(this, "Select a file to copy", "", "", + "All files (*.*)|*.*", 0); + + wxArrayString filenames; + while ( dialog.ShowModal() == wxID_OK ) + { + filenames.Add(dialog.GetPath()); + } + + if ( !filenames.IsEmpty() ) + { + wxFileDataObject *dobj = new wxFileDataObject; + size_t count = filenames.GetCount(); + for ( size_t n = 0; n < count; n++ ) + { + dobj->AddFile(filenames[n]); + } + + wxClipboardLocker locker; + if ( !locker ) + { + wxLogError("Can't open clipboard"); + } + else + { + if ( !wxTheClipboard->AddData(dobj) ) + { + wxLogError("Can't copy file(s) to the clipboard"); + } + else + { + wxLogStatus(this, "%d file%s copied to the clipboard", + count, count == 1 ? "" : "s"); + } + } + } + else + { + wxLogStatus(this, "Aborted"); + } +#else // !MSW + wxLogError("Sorry, not implemented"); +#endif // MSW/!MSW +} + // --------------------------------------------------------------------------- // text clipboard // --------------------------------------------------------------------------- @@ -1168,8 +1287,6 @@ DnDShapeFrame::DnDShapeFrame(wxFrame *parent) : wxFrame(parent, -1, "Shape Frame", wxDefaultPosition, wxSize(250, 150)) { - SetBackgroundColour(*wxWHITE); - CreateStatusBar(); wxMenu *menuShape = new wxMenu; @@ -1193,16 +1310,20 @@ DnDShapeFrame::DnDShapeFrame(wxFrame *parent) SetDropTarget(new DnDShapeDropTarget(this)); m_shape = NULL; + + SetBackgroundColour(*wxWHITE); } DnDShapeFrame::~DnDShapeFrame() { - delete m_shape; + if (m_shape) + delete m_shape; } void DnDShapeFrame::SetShape(DnDShape *shape) { - delete m_shape; + if (m_shape) + delete m_shape; m_shape = shape; Refresh(); } @@ -1219,7 +1340,7 @@ void DnDShapeFrame::OnDrag(wxMouseEvent& event) // start drag operation DnDShapeDataObject shapeData(m_shape); - wxDropSource source(shapeData, this, wxICON(mondrian)); + wxDropSource source(shapeData, this); const char *pc = NULL; switch ( source.DoDragDrop(TRUE) ) @@ -1258,15 +1379,17 @@ void DnDShapeFrame::OnDrag(wxMouseEvent& event) //else: status text already set } -void DnDShapeFrame::OnDrop(long x, long y, DnDShape *shape) +void DnDShapeFrame::OnDrop(wxCoord x, wxCoord y, DnDShape *shape) { ms_lastDropTarget = this; + wxPoint pt(x, y); + wxString s; - s.Printf("Shape dropped at (%ld, %ld)", x, y); + s.Printf("Shape dropped at (%ld, %ld)", pt.x, pt.y); SetStatusText(s); - shape->Move(ScreenToClient(wxPoint(x, y))); + shape->Move(pt); SetShape(shape); } @@ -1299,11 +1422,29 @@ void DnDShapeFrame::OnClearShape(wxCommandEvent& event) void DnDShapeFrame::OnCopyShape(wxCommandEvent& event) { if ( m_shape ) + { + wxClipboardLocker clipLocker; + if ( !clipLocker ) + { + wxLogError("Can't open the clipboard"); + + return; + } + wxTheClipboard->AddData(new DnDShapeDataObject(m_shape)); + } } void DnDShapeFrame::OnPasteShape(wxCommandEvent& event) { + wxClipboardLocker clipLocker; + if ( !clipLocker ) + { + wxLogError("Can't open the clipboard"); + + return; + } + DnDShapeDataObject shapeDataObject(NULL); if ( wxTheClipboard->GetData(shapeDataObject) ) {