X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1e4a197e4c60e461b8068b0619692ea083e30b8b..0a5bb138a71dfc1c706fc0858fb2801500e2c2e8:/wxPython/demo/DragAndDrop.py diff --git a/wxPython/demo/DragAndDrop.py b/wxPython/demo/DragAndDrop.py index e99abc3660..95b192dbbb 100644 --- a/wxPython/demo/DragAndDrop.py +++ b/wxPython/demo/DragAndDrop.py @@ -1,85 +1,113 @@ -from wxPython.wx import * +import wx #---------------------------------------------------------------------- -class ClipTextPanel(wxPanel): - def __init__(self, parent, log): - wxPanel.__init__(self, parent, -1) - self.log = log - - #self.SetFont(wxFont(10, wxSWISS, wxNORMAL, wxBOLD, False)) - - sizer = wxBoxSizer(wxVERTICAL) - sizer.Add(wxStaticText(self, -1, - "Copy/Paste text to/from\n" - "this window and other apps"), 0, wxEXPAND|wxALL, 2) +ID_CopyBtn = wx.NewId() +ID_PasteBtn = wx.NewId() +ID_BitmapBtn = wx.NewId() - self.text = wxTextCtrl(self, -1, "", style=wxTE_MULTILINE|wxHSCROLL) - sizer.Add(self.text, 1, wxEXPAND) +#---------------------------------------------------------------------- - hsz = wxBoxSizer(wxHORIZONTAL) - hsz.Add(wxButton(self, 6050, " Copy "), 1, wxEXPAND|wxALL, 2) - hsz.Add(wxButton(self, 6051, " Paste "), 1, wxEXPAND|wxALL, 2) - sizer.Add(hsz, 0, wxEXPAND) - sizer.Add(wxButton(self, 6052, " Copy Bitmap "), 0, wxEXPAND|wxALL, 2) +class ClipTextPanel(wx.Panel): + def __init__(self, parent, log): + wx.Panel.__init__(self, parent, -1) + self.log = log - EVT_BUTTON(self, 6050, self.OnCopy) - EVT_BUTTON(self, 6051, self.OnPaste) - EVT_BUTTON(self, 6052, self.OnCopyBitmap) + #self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False)) + + sizer = wx.BoxSizer(wx.VERTICAL) + sizer.Add( + wx.StaticText( + self, -1, "Copy/Paste text to/from\n" + "this window and other apps" + ), + 0, wx.EXPAND|wx.ALL, 2 + ) + + self.text = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.HSCROLL) + sizer.Add(self.text, 1, wx.EXPAND) + + hsz = wx.BoxSizer(wx.HORIZONTAL) + hsz.Add(wx.Button(self, ID_CopyBtn, " Copy "), 1, wx.EXPAND|wx.ALL, 2) + hsz.Add(wx.Button(self, ID_PasteBtn, " Paste "), 1, wx.EXPAND|wx.ALL, 2) + sizer.Add(hsz, 0, wx.EXPAND) + sizer.Add( + wx.Button(self, ID_BitmapBtn, " Copy Bitmap "), + 0, wx.EXPAND|wx.ALL, 2 + ) + + self.Bind(wx.EVT_BUTTON, self.OnCopy, id=ID_CopyBtn) + self.Bind(wx.EVT_BUTTON, self.OnPaste, id=ID_PasteBtn) + self.Bind(wx.EVT_BUTTON, self.OnCopyBitmap, id=ID_BitmapBtn) self.SetAutoLayout(True) self.SetSizer(sizer) def OnCopy(self, evt): - self.do = wxTextDataObject() + self.do = wx.TextDataObject() self.do.SetText(self.text.GetValue()) - wxTheClipboard.Open() - wxTheClipboard.SetData(self.do) - wxTheClipboard.Close() + if wx.TheClipboard.Open(): + wx.TheClipboard.SetData(self.do) + wx.TheClipboard.Close() + else: + wx.MessageBox("Unable to open the clipboard", "Error") def OnPaste(self, evt): - do = wxTextDataObject() - wxTheClipboard.Open() - success = wxTheClipboard.GetData(do) - wxTheClipboard.Close() + success = False + do = wx.TextDataObject() + if wx.TheClipboard.Open(): + success = wx.TheClipboard.GetData(do) + wx.TheClipboard.Close() + if success: self.text.SetValue(do.GetText()) else: - wxMessageBox("There is no data in the clipboard in the required format", - "Error") + wx.MessageBox( + "There is no data in the clipboard in the required format", + "Error" + ) def OnCopyBitmap(self, evt): - dlg = wxFileDialog(self, "Choose a bitmap to copy", wildcard="*.bmp") - if dlg.ShowModal() == wxID_OK: - bmp = wxBitmap(dlg.GetFilename(), wxBITMAP_TYPE_BMP) - bmpdo = wxBitmapDataObject(bmp) - wxTheClipboard.Open() - wxTheClipboard.SetData(bmpdo) - wxTheClipboard.Close() - - wxMessageBox("The bitmap is now in the Clipboard. Switch to a graphics\n" - "editor and try pasting it in...") + dlg = wx.FileDialog(self, "Choose a bitmap to copy", wildcard="*.bmp") + + if dlg.ShowModal() == wx.ID_OK: + bmp = wx.Bitmap(dlg.GetPath(), wx.BITMAP_TYPE_BMP) + bmpdo = wx.BitmapDataObject(bmp) + if wx.TheClipboard.Open(): + wx.TheClipboard.SetData(bmpdo) + wx.TheClipboard.Close() + + wx.MessageBox( + "The bitmap is now in the Clipboard. Switch to a graphics\n" + "editor and try pasting it in..." + ) + else: + wx.MessageBox( + "There is no data in the clipboard in the required format", + "Error" + ) + dlg.Destroy() #---------------------------------------------------------------------- -class OtherDropTarget(wxPyDropTarget): +class OtherDropTarget(wx.PyDropTarget): def __init__(self, window, log): - wxPyDropTarget.__init__(self) + wx.PyDropTarget.__init__(self) self.log = log - self.do = wxFileDataObject() + self.do = wx.FileDataObject() self.SetDataObject(self.do) def OnEnter(self, x, y, d): self.log.WriteText("OnEnter: %d, %d, %d\n" % (x, y, d)) - return wxDragCopy + return wx.DragCopy #def OnDragOver(self, x, y, d): # self.log.WriteText("OnDragOver: %d, %d, %d\n" % (x, y, d)) - # return wxDragCopy + # return wx.DragCopy def OnLeave(self): self.log.WriteText("OnLeave\n") @@ -95,11 +123,9 @@ class OtherDropTarget(wxPyDropTarget): return d - - -class MyFileDropTarget(wxFileDropTarget): +class MyFileDropTarget(wx.FileDropTarget): def __init__(self, window, log): - wxFileDropTarget.__init__(self) + wx.FileDropTarget.__init__(self) self.window = window self.log = log @@ -107,13 +133,14 @@ class MyFileDropTarget(wxFileDropTarget): self.window.SetInsertionPointEnd() self.window.WriteText("\n%d file(s) dropped at %d,%d:\n" % (len(filenames), x, y)) + for file in filenames: self.window.WriteText(file + '\n') -class MyTextDropTarget(wxTextDropTarget): +class MyTextDropTarget(wx.TextDropTarget): def __init__(self, window, log): - wxTextDropTarget.__init__(self) + wx.TextDropTarget.__init__(self) self.window = window self.log = log @@ -121,32 +148,43 @@ class MyTextDropTarget(wxTextDropTarget): self.window.WriteText("(%d, %d)\n%s\n" % (x, y, text)) def OnDragOver(self, x, y, d): - return wxDragCopy + return wx.DragCopy -class FileDropPanel(wxPanel): +class FileDropPanel(wx.Panel): def __init__(self, parent, log): - wxPanel.__init__(self, parent, -1) + wx.Panel.__init__(self, parent, -1) + + #self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD, False)) - #self.SetFont(wxFont(10, wxSWISS, wxNORMAL, wxBOLD, False)) + sizer = wx.BoxSizer(wx.VERTICAL) + sizer.Add( + wx.StaticText(self, -1, " \nDrag some files here:"), + 0, wx.EXPAND|wx.ALL, 2 + ) - sizer = wxBoxSizer(wxVERTICAL) - sizer.Add(wxStaticText(self, -1, " \nDrag some files here:"), - 0, wxEXPAND|wxALL, 2) + self.text = wx.TextCtrl( + self, -1, "", + style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY + ) - self.text = wxTextCtrl(self, -1, "", - style = wxTE_MULTILINE|wxHSCROLL|wxTE_READONLY) dt = MyFileDropTarget(self, log) self.text.SetDropTarget(dt) - sizer.Add(self.text, 1, wxEXPAND) + sizer.Add(self.text, 1, wx.EXPAND) + + sizer.Add( + wx.StaticText(self, -1, " \nDrag some text here:"), + 0, wx.EXPAND|wx.ALL, 2 + ) + + self.text2 = wx.TextCtrl( + self, -1, "", + style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY + ) - sizer.Add(wxStaticText(self, -1, " \nDrag some text here:"), - 0, wxEXPAND|wxALL, 2) - self.text2 = wxTextCtrl(self, -1, "", - style = wxTE_MULTILINE|wxHSCROLL|wxTE_READONLY) dt = MyTextDropTarget(self.text2, log) self.text2.SetDropTarget(dt) - sizer.Add(self.text2, 1, wxEXPAND) + sizer.Add(self.text2, 1, wx.EXPAND) self.SetAutoLayout(True) self.SetSizer(sizer) @@ -162,28 +200,29 @@ class FileDropPanel(wxPanel): #---------------------------------------------------------------------- #---------------------------------------------------------------------- -class TestPanel(wxPanel): +class TestPanel(wx.Panel): def __init__(self, parent, log): - wxPanel.__init__(self, parent, -1) + wx.Panel.__init__(self, parent, -1) self.SetAutoLayout(True) - outsideSizer = wxBoxSizer(wxVERTICAL) + outsideSizer = wx.BoxSizer(wx.VERTICAL) msg = "Clipboard / Drag-And-Drop" - text = wxStaticText(self, -1, "", style=wxALIGN_CENTRE) - text.SetFont(wxFont(24, wxSWISS, wxNORMAL, wxBOLD, False)) + text = wx.StaticText(self, -1, "", style=wx.ALIGN_CENTRE) + text.SetFont(wx.Font(24, wx.SWISS, wx.NORMAL, wx.BOLD, False)) text.SetLabel(msg) + w,h = text.GetTextExtent(msg) - text.SetSize(wxSize(w,h+1)) - text.SetForegroundColour(wxBLUE) - outsideSizer.Add(text, 0, wxEXPAND|wxALL, 5) - outsideSizer.Add(wxStaticLine(self, -1), 0, wxEXPAND) + text.SetSize(wx.Size(w,h+1)) + text.SetForegroundColour(wx.BLUE) + outsideSizer.Add(text, 0, wx.EXPAND|wx.ALL, 5) + outsideSizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND) - inSizer = wxBoxSizer(wxHORIZONTAL) - inSizer.Add(ClipTextPanel(self, log), 1, wxEXPAND) - inSizer.Add(FileDropPanel(self, log), 1, wxEXPAND) + inSizer = wx.BoxSizer(wx.HORIZONTAL) + inSizer.Add(ClipTextPanel(self, log), 1, wx.EXPAND) + inSizer.Add(FileDropPanel(self, log), 1, wx.EXPAND) - outsideSizer.Add(inSizer, 1, wxEXPAND) + outsideSizer.Add(inSizer, 1, wx.EXPAND) self.SetSizer(outsideSizer) @@ -196,21 +235,39 @@ def runTest(frame, nb, log): #---------------------------------------------------------------------- - - - - - - - - - - overview = """\ -This demo shows some examples of data transfer through clipboard or drag and drop. In wxWindows, these two ways to transfer data (either between different applications or inside one and the same) are very similar which allows to implement both of them using almost the same code - or, in other words, if you implement drag and drop support for your application, you get clipboard support for free and vice versa. + + +This demo shows some examples of data transfer through clipboard or +drag and drop. In wxWindows, these two ways to transfer data (either +between different applications or inside one and the same) are very +similar which allows to implement both of them using almost the same +code - or, in other words, if you implement drag and drop support for +your application, you get clipboard support for free and vice versa. +

+At the heart of both clipboard and drag and drop operations lies the +wxDataObject class. The objects of this class (or, to be precise, +classes derived from it) represent the data which is being carried by +the mouse during drag and drop operation or copied to or pasted from +the clipboard. wxDataObject is a "smart" piece of data because it +knows which formats it supports (see GetFormatCount and GetAllFormats) +and knows how to render itself in any of them (see GetDataHere). It +can also receive its value from the outside in a format it supports if +it implements the SetData method. Please see the documentation of this +class for more details. +

+Both clipboard and drag and drop operations have two sides: the source +and target, the data provider and the data receiver. These which may +be in the same application and even the same window when, for example, +you drag some text from one position to another in a word +processor. Let us describe what each of them should do. + + +""" -At the heart of both clipboard and drag and drop operations lies the wxDataObject class. The objects of this class (or, to be precise, classes derived from it) represent the data which is being carried by the mouse during drag and drop operation or copied to or pasted from the clipboard. wxDataObject is a "smart" piece of data because it knows which formats it supports (see GetFormatCount and GetAllFormats) and knows how to render itself in any of them (see GetDataHere). It can also receive its value from the outside in a format it supports if it implements the SetData method. Please see the documentation of this class for more details. -Both clipboard and drag and drop operations have two sides: the source and target, the data provider and the data receiver. These which may be in the same application and even the same window when, for example, you drag some text from one position to another in a word processor. Let us describe what each of them should do. +if __name__ == '__main__': + import sys,os + import run + run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) -"""