X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/4693b20c7500190c3cc8c02919c45436bf5efbc9..75cc04751cd2c2a928988af49a99c9ba4f386feb:/samples/dnd/dnd.cpp diff --git a/samples/dnd/dnd.cpp b/samples/dnd/dnd.cpp index 164dd9c712..41b7851501 100644 --- a/samples/dnd/dnd.cpp +++ b/samples/dnd/dnd.cpp @@ -54,7 +54,7 @@ #include "wx/metafile.h" #endif // Windows -#if defined(__WXGTK__) || defined(__WXMOTIF__) +#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) #include "mondrian.xpm" #include "dnd_copy.xpm" @@ -109,7 +109,10 @@ public: // URLs can't be moved, only copied virtual wxDragResult OnDragOver(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult def) - { return def == wxDragMove ? wxDragCopy : def; } + { + return wxDragLink; // At least IE 5.x needs wxDragLink, the + // other browsers on MSW seem okay with it too. + } // translate this to calls to OnDropURL() just for convenience virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def) @@ -218,12 +221,15 @@ class DnDFrame : public wxFrame { public: DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h); - ~DnDFrame(); + virtual ~DnDFrame(); void OnPaint(wxPaintEvent& event); - void OnQuit (wxCommandEvent& event); + void OnSize(wxSizeEvent& event); + void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); - void OnDrag (wxCommandEvent& event); + void OnDrag(wxCommandEvent& event); + void OnDragMoveByDefault(wxCommandEvent& event); + void OnDragMoveAllow(wxCommandEvent& event); void OnNewFrame(wxCommandEvent& event); void OnHelp (wxCommandEvent& event); void OnLogClear(wxCommandEvent& event); @@ -243,12 +249,15 @@ public: void OnLeftDown(wxMouseEvent& event); void OnRightDown(wxMouseEvent& event); + void OnUpdateUIMoveByDefault(wxUpdateUIEvent& event); + void OnUpdateUIPasteText(wxUpdateUIEvent& event); void OnUpdateUIPasteBitmap(wxUpdateUIEvent& event); DECLARE_EVENT_TABLE() private: + // GUI controls wxListBox *m_ctrlFile, *m_ctrlText; wxTextCtrl *m_ctrlLog; @@ -256,6 +265,13 @@ private: wxLog *m_pLog, *m_pLogPrev; + // move the text by default (or copy)? + bool m_moveByDefault; + + // allow moving the text at all? + bool m_moveAllow; + + // the text we drag wxString m_strText; }; @@ -328,9 +344,10 @@ public: } 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, m_pos.y + m_size.y / 2); } - + { return wxPoint(m_pos.x + m_size.x / 2 - 1, m_pos.y + m_size.y / 2 - 1); } + struct ShapeDump { int x, y, // position @@ -375,9 +392,9 @@ public: dc.DrawLine(p2, p3); dc.DrawLine(p3, p1); -#ifdef __WXMSW__ + //works in multicolor modes; on GTK (at least) will fail in 16-bit color + dc.SetBrush(wxBrush(m_col, wxSOLID)); dc.FloodFill(GetCentre(), m_col, wxFLOOD_BORDER); -#endif } }; @@ -412,9 +429,8 @@ public: dc.DrawLine(p3, p4); dc.DrawLine(p4, p1); -#ifdef __WXMSW__ + dc.SetBrush(wxBrush(m_col, wxSOLID)); dc.FloodFill(GetCentre(), m_col, wxFLOOD_BORDER); -#endif } }; @@ -441,9 +457,8 @@ public: dc.DrawEllipse(m_pos, m_size); -#ifdef __WXMSW__ + dc.SetBrush(wxBrush(m_col, wxSOLID)); dc.FloodFill(GetCentre(), m_col, wxFLOOD_BORDER); -#endif } }; @@ -773,6 +788,8 @@ enum { Menu_Quit = 1, Menu_Drag, + Menu_DragMoveDef, + Menu_DragMoveAllow, Menu_NewFrame, Menu_About = 101, Menu_Help, @@ -795,6 +812,8 @@ BEGIN_EVENT_TABLE(DnDFrame, wxFrame) EVT_MENU(Menu_Quit, DnDFrame::OnQuit) EVT_MENU(Menu_About, DnDFrame::OnAbout) EVT_MENU(Menu_Drag, DnDFrame::OnDrag) + EVT_MENU(Menu_DragMoveDef, DnDFrame::OnDragMoveByDefault) + EVT_MENU(Menu_DragMoveAllow,DnDFrame::OnDragMoveAllow) EVT_MENU(Menu_NewFrame, DnDFrame::OnNewFrame) EVT_MENU(Menu_Help, DnDFrame::OnHelp) EVT_MENU(Menu_Clear, DnDFrame::OnLogClear) @@ -807,12 +826,15 @@ BEGIN_EVENT_TABLE(DnDFrame, wxFrame) #endif // USE_METAFILES EVT_MENU(Menu_CopyFiles, DnDFrame::OnCopyFiles) + EVT_UPDATE_UI(Menu_DragMoveDef, DnDFrame::OnUpdateUIMoveByDefault) + EVT_UPDATE_UI(Menu_Paste, DnDFrame::OnUpdateUIPasteText) EVT_UPDATE_UI(Menu_PasteBitmap, DnDFrame::OnUpdateUIPasteBitmap) EVT_LEFT_DOWN( DnDFrame::OnLeftDown) EVT_RIGHT_DOWN( DnDFrame::OnRightDown) EVT_PAINT( DnDFrame::OnPaint) + EVT_SIZE( DnDFrame::OnSize) END_EVENT_TABLE() BEGIN_EVENT_TABLE(DnDShapeFrame, wxFrame) @@ -914,10 +936,12 @@ DnDFrame::DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h) // construct menu wxMenu *file_menu = new wxMenu; file_menu->Append(Menu_Drag, "&Test drag..."); + file_menu->AppendCheckItem(Menu_DragMoveDef, "&Move by default"); + file_menu->AppendCheckItem(Menu_DragMoveAllow, "&Allow moving"); file_menu->AppendSeparator(); file_menu->Append(Menu_NewFrame, "&New frame\tCtrl-N"); file_menu->AppendSeparator(); - file_menu->Append(Menu_Quit, "E&xit"); + file_menu->Append(Menu_Quit, "E&xit\tCtrl-Q"); wxMenu *log_menu = new wxMenu; log_menu->Append(Menu_Clear, "Clear\tCtrl-L"); @@ -928,17 +952,17 @@ DnDFrame::DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h) help_menu->Append(Menu_About, "&About"); wxMenu *clip_menu = new wxMenu; - clip_menu->Append(Menu_Copy, "&Copy text\tCtrl+C"); - clip_menu->Append(Menu_Paste, "&Paste text\tCtrl+V"); + clip_menu->Append(Menu_Copy, "&Copy text\tCtrl-C"); + clip_menu->Append(Menu_Paste, "&Paste text\tCtrl-V"); clip_menu->AppendSeparator(); - clip_menu->Append(Menu_CopyBitmap, "Copy &bitmap\tAlt+C"); - clip_menu->Append(Menu_PasteBitmap, "Paste b&itmap\tAlt+V"); + clip_menu->Append(Menu_CopyBitmap, "Copy &bitmap\tCtrl-Shift-C"); + clip_menu->Append(Menu_PasteBitmap, "Paste b&itmap\tCtrl-Shift-V"); #ifdef USE_METAFILES clip_menu->AppendSeparator(); clip_menu->Append(Menu_PasteMFile, "Paste &metafile\tCtrl-M"); #endif // USE_METAFILES clip_menu->AppendSeparator(); - clip_menu->Append(Menu_CopyFiles, "Copy &files\tCtrl+F"); + clip_menu->Append(Menu_CopyFiles, "Copy &files\tCtrl-F"); wxMenuBar *menu_bar = new wxMenuBar; menu_bar->Append(file_menu, "&File"); @@ -999,6 +1023,11 @@ DnDFrame::DnDFrame(wxFrame *frame, char *title, int x, int y, int w, int h) m_ctrlLog->SetConstraints(c); SetAutoLayout(TRUE); + + // copy data by default but allow moving it as well + m_moveByDefault = FALSE; + m_moveAllow = TRUE; + menu_bar->Check(Menu_DragMoveAllow, TRUE); } void DnDFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) @@ -1006,6 +1035,13 @@ void DnDFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) Close(TRUE); } +void DnDFrame::OnSize(wxSizeEvent& event) +{ + Refresh(); + + event.Skip(); +} + void DnDFrame::OnPaint(wxPaintEvent& WXUNUSED(event)) { int w = 0; @@ -1013,10 +1049,17 @@ void DnDFrame::OnPaint(wxPaintEvent& WXUNUSED(event)) GetClientSize( &w, &h ); wxPaintDC dc(this); + // dc.Clear(); -- this kills wxGTK dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL, FALSE, "charter" ) ); dc.DrawText( "Drag text from here!", 100, h-50 ); } +void DnDFrame::OnUpdateUIMoveByDefault(wxUpdateUIEvent& event) +{ + // only can move by default if moving is allowed at all + event.Enable(m_moveAllow); +} + void DnDFrame::OnUpdateUIPasteText(wxUpdateUIEvent& event) { #ifdef __WXDEBUG__ @@ -1060,6 +1103,16 @@ void DnDFrame::OnDrag(wxCommandEvent& WXUNUSED(event)) m_strText = strText; } +void DnDFrame::OnDragMoveByDefault(wxCommandEvent& event) +{ + m_moveByDefault = event.IsChecked(); +} + +void DnDFrame::OnDragMoveAllow(wxCommandEvent& event) +{ + m_moveAllow = event.IsChecked(); +} + void DnDFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) { wxMessageBox("Drag-&-Drop Demo\n" @@ -1121,9 +1174,14 @@ void DnDFrame::OnLeftDown(wxMouseEvent &WXUNUSED(event) ) wxDROP_ICON(dnd_move), wxDROP_ICON(dnd_none)); - const char *pc; + int flags = 0; + if ( m_moveByDefault ) + flags |= wxDrag_DefaultMove; + else if ( m_moveAllow ) + flags |= wxDrag_AllowMove; - switch ( source.DoDragDrop(TRUE) ) + const char *pc; + switch ( source.DoDragDrop(flags) ) { case wxDragError: pc = "Error!"; break; case wxDragNone: pc = "Nothing"; break; @@ -1204,7 +1262,7 @@ void DnDFrame::OnCopyBitmap(wxCommandEvent& WXUNUSED(event)) wxLogStatus( _T("Decoding image file...") ); wxYield(); - wxBitmap bitmap( image.ConvertToBitmap() ); + wxBitmap bitmap( image ); if ( !wxTheClipboard->Open() ) { @@ -1462,7 +1520,7 @@ DnDShapeDialog::DnDShapeDialog(wxFrame *parent, DnDShape *shape) const wxString choices[] = { wxT("None"), wxT("Triangle"), wxT("Rectangle"), wxT("Ellipse") }; - m_radio = new wxRadioBox( this, -1, wxT("&Shape"), + m_radio = new wxRadioBox( this, -1, wxT("&Shape"), wxDefaultPosition, wxDefaultSize, 4, choices, 4, wxRA_SPECIFY_COLS ); shapesSizer->Add( m_radio, 0, wxGROW|wxALL, 5 ); @@ -1472,32 +1530,32 @@ DnDShapeDialog::DnDShapeDialog(wxFrame *parent, DnDShape *shape) wxStaticBox* box = new wxStaticBox( this, -1, wxT("&Attributes") ); wxStaticBoxSizer* attrSizer = new wxStaticBoxSizer( box, wxHORIZONTAL ); wxFlexGridSizer* xywhSizer = new wxFlexGridSizer( 4, 2 ); - + wxStaticText* st; st = new wxStaticText( this, -1, wxT("Position &X:") ); m_textX = new wxTextCtrl( this, -1, wxEmptyString, wxDefaultPosition, wxSize( 30, 20 ) ); - xywhSizer->Add( st, 1, wxGROW|wxALL, 2 ); - xywhSizer->Add( m_textX, 1, wxGROW|wxALL, 2 ); + xywhSizer->Add( st, 1, wxGROW|wxALL, 2 ); + xywhSizer->Add( m_textX, 1, wxGROW|wxALL, 2 ); st = new wxStaticText( this, -1, wxT("Size &width:") ); m_textW = new wxTextCtrl( this, -1, wxEmptyString, wxDefaultPosition, wxSize( 30, 20 ) ); - xywhSizer->Add( st, 1, wxGROW|wxALL, 2 ); - xywhSizer->Add( m_textW, 1, wxGROW|wxALL, 2 ); + xywhSizer->Add( st, 1, wxGROW|wxALL, 2 ); + xywhSizer->Add( m_textW, 1, wxGROW|wxALL, 2 ); st = new wxStaticText( this, -1, wxT("&Y:") ); m_textY = new wxTextCtrl( this, -1, wxEmptyString, wxDefaultPosition, wxSize( 30, 20 ) ); - xywhSizer->Add( st, 1, wxALL|wxALIGN_RIGHT, 2 ); - xywhSizer->Add( m_textY, 1, wxGROW|wxALL, 2 ); + xywhSizer->Add( st, 1, wxALL|wxALIGN_RIGHT, 2 ); + xywhSizer->Add( m_textY, 1, wxGROW|wxALL, 2 ); st = new wxStaticText( this, -1, wxT("&height:") ); m_textH = new wxTextCtrl( this, -1, wxEmptyString, wxDefaultPosition, wxSize( 30, 20 ) ); - xywhSizer->Add( st, 1, wxALL|wxALIGN_RIGHT, 2 ); - xywhSizer->Add( m_textH, 1, wxGROW|wxALL, 2 ); + xywhSizer->Add( st, 1, wxALL|wxALIGN_RIGHT, 2 ); + xywhSizer->Add( m_textH, 1, wxGROW|wxALL, 2 ); wxButton* col = new wxButton( this, Button_Colour, wxT("&Colour...") ); attrSizer->Add( xywhSizer, 1, wxGROW ); @@ -1702,7 +1760,7 @@ void DnDShapeFrame::OnDrop(wxCoord x, wxCoord y, DnDShape *shape) wxPoint pt(x, y); wxString s; - s.Printf(wxT("Shape dropped at (%ld, %ld)"), pt.x, pt.y); + s.Printf(wxT("Shape dropped at (%d, %d)"), pt.x, pt.y); SetStatusText(s); shape->Move(pt); @@ -1861,7 +1919,7 @@ void DnDShapeDataObject::CreateBitmap() const wxBitmap bitmap(x, y); wxMemoryDC dc; dc.SelectObject(bitmap); - dc.SetBrush(wxBrush("white", wxSOLID)); + dc.SetBrush(wxBrush(wxT("white"), wxSOLID)); dc.Clear(); m_shape->Draw(dc); dc.SelectObject(wxNullBitmap);