X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9f84eccdeaf8e3706f7861ce19da3a256814d83e..8b1ed8d979f193aa78b74c7d7a4a4ac33e50c121:/samples/dnd/dnd.cpp?ds=sidebyside diff --git a/samples/dnd/dnd.cpp b/samples/dnd/dnd.cpp index e7a4b69008..f2c9da7f78 100644 --- a/samples/dnd/dnd.cpp +++ b/samples/dnd/dnd.cpp @@ -19,22 +19,6 @@ #include "wx/wx.h" #endif -#if !wxUSE_DRAG_AND_DROP - #error This sample requires drag and drop support in the library -#endif - -// under Windows we also support data transfer of metafiles as an extra bonus, -// but they're not available under other platforms -#ifdef __WINDOWS__ - #define USE_METAFILES -#endif // Windows - -#define USE_RESOURCES 0 -#if !wxUSE_RESOURCES -#undef USE_RESOURCES -#define USE_RESOURCES 0 -#endif - #include "wx/intl.h" #include "wx/log.h" @@ -44,15 +28,11 @@ #include "wx/image.h" #include "wx/clipbrd.h" #include "wx/colordlg.h" -#if USE_RESOURCES - #include "wx/resource.h" -#else - #include "wx/sizer.h" -#endif +#include "wx/sizer.h" -#ifdef USE_METAFILES +#if wxUSE_METAFILES #include "wx/metafile.h" -#endif // Windows +#endif // wxUSE_METAFILES #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) #include "mondrian.xpm" @@ -62,6 +42,8 @@ #include "dnd_none.xpm" #endif +#if wxUSE_DRAG_AND_DROP + // ---------------------------------------------------------------------------- // Derive two simple classes which just put in the listbox the strings (text or // file names) we drop on them @@ -126,6 +108,8 @@ public: } }; +#endif // wxUSE_DRAG_AND_DROP + // ---------------------------------------------------------------------------- // Define a new application type // ---------------------------------------------------------------------------- @@ -138,6 +122,8 @@ public: IMPLEMENT_APP(DnDApp); +#if wxUSE_DRAG_AND_DROP + // ---------------------------------------------------------------------------- // Define canvas class to show a bitmap // ---------------------------------------------------------------------------- @@ -175,7 +161,7 @@ private: DECLARE_EVENT_TABLE() }; -#ifdef USE_METAFILES +#if wxUSE_METAFILES // and the same thing fo metafiles class DnDCanvasMetafile : public wxScrolledWindow @@ -211,7 +197,7 @@ private: DECLARE_EVENT_TABLE() }; -#endif // USE_METAFILES +#endif // wxUSE_METAFILES // ---------------------------------------------------------------------------- // Define a new frame type for the main frame @@ -240,9 +226,9 @@ public: void OnCopyBitmap(wxCommandEvent& event); void OnPasteBitmap(wxCommandEvent& event); -#ifdef USE_METAFILES +#if wxUSE_METAFILES void OnPasteMetafile(wxCommandEvent& event); -#endif // USE_METAFILES +#endif // wxUSE_METAFILES void OnCopyFiles(wxCommandEvent& event); @@ -347,7 +333,7 @@ protected: //get a point 1 up and 1 left, otherwise the mid-point of a triangle is on the line wxPoint GetCentre() const { return wxPoint(m_pos.x + m_size.x / 2 - 1, m_pos.y + m_size.y / 2 - 1); } - + struct ShapeDump { int x, y, // position @@ -393,7 +379,7 @@ public: dc.DrawLine(p3, p1); //works in multicolor modes; on GTK (at least) will fail in 16-bit color - dc.SetBrush(wxBrush(m_col, wxSOLID)); + dc.SetBrush(wxBrush(m_col, wxSOLID)); dc.FloodFill(GetCentre(), m_col, wxFLOOD_BORDER); } }; @@ -499,9 +485,9 @@ public: // we don't draw the shape to a bitmap until it's really needed (i.e. // we're asked to do so) m_hasBitmap = FALSE; -#ifdef USE_METAFILES +#if wxUSE_METAFILES m_hasMetaFile = FALSE; -#endif // Windows +#endif // wxUSE_METAFILES } virtual ~DnDShapeDataObject() { delete m_shape; } @@ -518,9 +504,9 @@ public: m_shape = (DnDShape *)NULL; m_hasBitmap = FALSE; -#ifdef USE_METAFILES +#if wxUSE_METAFILES m_hasMetaFile = FALSE; -#endif // Windows +#endif // wxUSE_METAFILES return shape; } @@ -542,9 +528,9 @@ public: // but the bitmap format(s) are only supported for output nFormats += m_dobjBitmap.GetFormatCount(dir); -#ifdef USE_METAFILES +#if wxUSE_METAFILES nFormats += m_dobjMetaFile.GetFormatCount(dir); -#endif // Windows +#endif // wxUSE_METAFILES } return nFormats; @@ -559,11 +545,11 @@ public: // under Windows m_dobjBitmap.GetAllFormats(&formats[1], dir); -#ifdef USE_METAFILES +#if wxUSE_METAFILES // don't assume that m_dobjBitmap has only 1 format m_dobjMetaFile.GetAllFormats(&formats[1 + m_dobjBitmap.GetFormatCount(dir)], dir); -#endif // Windows +#endif // wxUSE_METAFILES } } @@ -573,7 +559,7 @@ public: { return m_shape->GetDataSize(); } -#ifdef USE_METAFILES +#if wxUSE_METAFILES else if ( m_dobjMetaFile.IsSupported(format) ) { if ( !m_hasMetaFile ) @@ -581,7 +567,7 @@ public: return m_dobjMetaFile.GetDataSize(format); } -#endif // Windows +#endif // wxUSE_METAFILES else { wxASSERT_MSG( m_dobjBitmap.IsSupported(format), @@ -602,7 +588,7 @@ public: return TRUE; } -#ifdef USE_METAFILES +#if wxUSE_METAFILES else if ( m_dobjMetaFile.IsSupported(format) ) { if ( !m_hasMetaFile ) @@ -610,7 +596,7 @@ public: return m_dobjMetaFile.GetDataHere(format, pBuf); } -#endif // Windows +#endif // wxUSE_METAFILES else { wxASSERT_MSG( m_dobjBitmap.IsSupported(format), @@ -635,9 +621,9 @@ public: // the shape has changed m_hasBitmap = FALSE; -#ifdef USE_METAFILES +#if wxUSE_METAFILES m_hasMetaFile = FALSE; -#endif // Windows +#endif // wxUSE_METAFILES return TRUE; } @@ -645,19 +631,19 @@ public: private: // creates a bitmap and assigns it to m_dobjBitmap (also sets m_hasBitmap) void CreateBitmap() const; -#ifdef USE_METAFILES +#if wxUSE_METAFILES void CreateMetaFile() const; -#endif // Windows +#endif // wxUSE_METAFILES wxDataFormat m_formatShape; // our custom format wxBitmapDataObject m_dobjBitmap; // it handles bitmaps bool m_hasBitmap; // true if m_dobjBitmap has valid bitmap -#ifdef USE_METAFILES +#if wxUSE_METAFILES wxMetaFileDataObject m_dobjMetaFile;// handles metafiles bool m_hasMetaFile; // true if we have valid metafile -#endif // Windows +#endif // wxUSE_METAFILES DnDShape *m_shape; // our data }; @@ -709,6 +695,10 @@ public: ~DnDShapeFrame(); void SetShape(DnDShape *shape); + virtual bool SetShape(const wxRegion ®ion) + { + return wxFrame::SetShape( region ); + } // callbacks void OnNewShape(wxCommandEvent& event); @@ -776,9 +766,9 @@ private: static void ShowBitmap(const wxBitmap& bitmap); -#ifdef USE_METAFILES +#if wxUSE_METAFILES static void ShowMetaFile(const wxMetaFile& metafile); -#endif // USE_METAFILES +#endif // wxUSE_METAFILES // ---------------------------------------------------------------------------- // IDs for the menu commands @@ -821,9 +811,9 @@ BEGIN_EVENT_TABLE(DnDFrame, wxFrame) EVT_MENU(Menu_Paste, DnDFrame::OnPaste) EVT_MENU(Menu_CopyBitmap, DnDFrame::OnCopyBitmap) EVT_MENU(Menu_PasteBitmap,DnDFrame::OnPasteBitmap) -#ifdef USE_METAFILES +#if wxUSE_METAFILES EVT_MENU(Menu_PasteMFile, DnDFrame::OnPasteMetafile) -#endif // USE_METAFILES +#endif // wxUSE_METAFILES EVT_MENU(Menu_CopyFiles, DnDFrame::OnCopyFiles) EVT_UPDATE_UI(Menu_DragMoveDef, DnDFrame::OnUpdateUIMoveByDefault) @@ -861,11 +851,13 @@ BEGIN_EVENT_TABLE(DnDCanvasBitmap, wxScrolledWindow) EVT_PAINT(DnDCanvasBitmap::OnPaint) END_EVENT_TABLE() -#ifdef USE_METAFILES +#if wxUSE_METAFILES BEGIN_EVENT_TABLE(DnDCanvasMetafile, wxScrolledWindow) EVT_PAINT(DnDCanvasMetafile::OnPaint) END_EVENT_TABLE() -#endif // USE_METAFILES +#endif // wxUSE_METAFILES + +#endif // wxUSE_DRAG_AND_DROP // ============================================================================ // implementation @@ -874,27 +866,7 @@ END_EVENT_TABLE() // `Main program' equivalent, creating windows and returning main app frame bool DnDApp::OnInit() { -#if USE_RESOURCES - // load our ressources - wxPathList pathList; - pathList.Add(_T(".")); -#ifdef __WXMSW__ - pathList.Add(_T("./Debug")); - pathList.Add(_T("./Release")); -#endif // wxMSW - - wxString path = pathList.FindValidPath(_T("dnd.wxr")); - if ( !path ) - { - wxLogError(wxT("Can't find the resource file dnd.wxr in the current ") - wxT("directory, aborting.")); - - return FALSE; - } - - wxDefaultResourceTable->ParseResourceFile(path); -#endif - +#if wxUSE_DRAG_AND_DROP // switch on trace messages #if defined(__WXGTK__) wxLog::AddTraceMask(_T("clipboard")); @@ -921,8 +893,14 @@ bool DnDApp::OnInit() SetTopWindow(frame); return TRUE; +#else + wxMessageBox( _T("This sample has to be compiled with wxUSE_DRAG_AND_DROP"), _T("Building error"), wxOK); + return FALSE; +#endif // wxUSE_DRAG_AND_DROP } +#if wxUSE_DRAG_AND_DROP + DnDFrame::DnDFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h) : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h)), m_strText(_T("wxWindows drag & drop works :-)")) @@ -957,10 +935,10 @@ DnDFrame::DnDFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h) clip_menu->AppendSeparator(); clip_menu->Append(Menu_CopyBitmap, _T("Copy &bitmap\tCtrl-Shift-C")); clip_menu->Append(Menu_PasteBitmap, _T("Paste b&itmap\tCtrl-Shift-V")); -#ifdef USE_METAFILES +#if wxUSE_METAFILES clip_menu->AppendSeparator(); clip_menu->Append(Menu_PasteMFile, _T("Paste &metafile\tCtrl-M")); -#endif // USE_METAFILES +#endif // wxUSE_METAFILES clip_menu->AppendSeparator(); clip_menu->Append(Menu_CopyFiles, _T("Copy &files\tCtrl-F")); @@ -1221,10 +1199,10 @@ DnDFrame::~DnDFrame() void DnDFrame::OnCopyBitmap(wxCommandEvent& WXUNUSED(event)) { // PNG support is not always compiled in under Windows, so use BMP there -#ifdef __WXMSW__ - wxFileDialog dialog(this, _T("Open a BMP file"), _T(""), _T(""), _T("BMP files (*.bmp)|*.bmp"), 0); -#else +#if wxUSE_LIBPNG wxFileDialog dialog(this, _T("Open a PNG file"), _T(""), _T(""), _T("PNG files (*.png)|*.png"), 0); +#else + wxFileDialog dialog(this, _T("Open a BMP file"), _T(""), _T(""), _T("BMP files (*.bmp)|*.bmp"), 0); #endif if (dialog.ShowModal() != wxID_OK) @@ -1247,10 +1225,10 @@ void DnDFrame::OnCopyBitmap(wxCommandEvent& WXUNUSED(event)) wxImage image; image.LoadFile( dialog.GetPath(), -#ifdef __WXMSW__ - wxBITMAP_TYPE_BMP -#else +#if wxUSE_LIBPNG wxBITMAP_TYPE_PNG +#else + wxBITMAP_TYPE_BMP #endif ); if (!image.Ok()) @@ -1321,7 +1299,7 @@ void DnDFrame::OnPasteBitmap(wxCommandEvent& WXUNUSED(event)) wxTheClipboard->Close(); } -#ifdef USE_METAFILES +#if wxUSE_METAFILES void DnDFrame::OnPasteMetafile(wxCommandEvent& WXUNUSED(event)) { @@ -1357,7 +1335,7 @@ void DnDFrame::OnPasteMetafile(wxCommandEvent& WXUNUSED(event)) wxTheClipboard->Close(); } -#endif // USE_METAFILES +#endif // wxUSE_METAFILES // ---------------------------------------------------------------------------- // file clipboard @@ -1482,7 +1460,7 @@ bool DnDFile::OnDropFiles(wxCoord, wxCoord, const wxArrayString& filenames) { size_t nFiles = filenames.GetCount(); wxString str; - str.Printf( _T("%d files dropped"), nFiles); + str.Printf( _T("%d files dropped"), (int)nFiles); m_pOwner->Append(str); for ( size_t n = 0; n < nFiles; n++ ) { m_pOwner->Append(filenames[n]); @@ -1496,23 +1474,11 @@ bool DnDFile::OnDropFiles(wxCoord, wxCoord, const wxArrayString& filenames) // ---------------------------------------------------------------------------- DnDShapeDialog::DnDShapeDialog(wxFrame *parent, DnDShape *shape) -#if !USE_RESOURCES :wxDialog( parent, 6001, wxT("Choose Shape"), wxPoint( 10, 10 ), wxSize( 40, 40 ), wxRAISED_BORDER|wxCAPTION|wxTHICK_FRAME|wxSYSTEM_MENU ) -#endif { m_shape = shape; -#if USE_RESOURCES - LoadFromResource(parent, _T("dialogShape")); - - m_textX = (wxTextCtrl *)wxFindWindowByName(_T("textX"), this); - m_textY = (wxTextCtrl *)wxFindWindowByName(_T("textY"), this); - m_textW = (wxTextCtrl *)wxFindWindowByName(_T("textW"), this); - m_textH = (wxTextCtrl *)wxFindWindowByName(_T("textH"), this); - - m_radio = (wxRadioBox *)wxFindWindowByName(_T("radio"), this); -#else wxBoxSizer* topSizer = new wxBoxSizer( wxVERTICAL ); // radio box @@ -1574,7 +1540,6 @@ DnDShapeDialog::DnDShapeDialog(wxFrame *parent, DnDShape *shape) SetAutoLayout( TRUE ); SetSizer( topSizer ); topSizer->Fit( this ); -#endif } DnDShape *DnDShapeDialog::GetShape() const @@ -1767,7 +1732,7 @@ void DnDShapeFrame::OnDrop(wxCoord x, wxCoord y, DnDShape *shape) SetShape(shape); } -void DnDShapeFrame::OnEditShape(wxCommandEvent& event) +void DnDShapeFrame::OnEditShape(wxCommandEvent& WXUNUSED(event)) { DnDShapeDialog dlg(this, m_shape); if ( dlg.ShowModal() == wxID_OK ) @@ -1781,19 +1746,19 @@ void DnDShapeFrame::OnEditShape(wxCommandEvent& event) } } -void DnDShapeFrame::OnNewShape(wxCommandEvent& event) +void DnDShapeFrame::OnNewShape(wxCommandEvent& WXUNUSED(event)) { SetShape(new DnDEllipticShape(wxPoint(10, 10), wxSize(80, 60), *wxRED)); SetStatusText(_T("You can now drag the shape to another frame")); } -void DnDShapeFrame::OnClearShape(wxCommandEvent& event) +void DnDShapeFrame::OnClearShape(wxCommandEvent& WXUNUSED(event)) { SetShape(NULL); } -void DnDShapeFrame::OnCopyShape(wxCommandEvent& event) +void DnDShapeFrame::OnCopyShape(wxCommandEvent& WXUNUSED(event)) { if ( m_shape ) { @@ -1809,7 +1774,7 @@ void DnDShapeFrame::OnCopyShape(wxCommandEvent& event) } } -void DnDShapeFrame::OnPasteShape(wxCommandEvent& event) +void DnDShapeFrame::OnPasteShape(wxCommandEvent& WXUNUSED(event)) { wxClipboardLocker clipLocker; if ( !clipLocker ) @@ -1888,7 +1853,7 @@ DnDShape *DnDShape::New(const void *buf) // DnDShapeDataObject // ---------------------------------------------------------------------------- -#ifdef USE_METAFILES +#if wxUSE_METAFILES void DnDShapeDataObject::CreateMetaFile() const { @@ -1908,7 +1873,7 @@ void DnDShapeDataObject::CreateMetaFile() const delete mf; } -#endif // Windows +#endif // wxUSE_METAFILES void DnDShapeDataObject::CreateBitmap() const { @@ -1948,7 +1913,7 @@ static void ShowBitmap(const wxBitmap& bitmap) frame->Show(TRUE); } -#ifdef USE_METAFILES +#if wxUSE_METAFILES static void ShowMetaFile(const wxMetaFile& metafile) { @@ -1965,4 +1930,6 @@ static void ShowMetaFile(const wxMetaFile& metafile) frame->Show(); } -#endif // USE_METAFILES +#endif // wxUSE_METAFILES + +#endif // wxUSE_DRAG_AND_DROP