1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
# Name: maketools.py
# Purpose: Script to create tool bitmaps by screen capture
# Author: Roman Rolinsky <rolinsky@femagsoft.com>
# Created: 04.07.2007
# RCS-ID: $Id$
import os,sys,time
import wx
import wx.xrc as xrc
import wx.grid as grid
def WTF(win, filename):
"""WindowToFile: save part of the screen as 'filename'."""
if wx.Platform == '__WXMAC__':
# Blit does not write color
os.system('screencapture scr.png')
screen = wx.Bitmap('scr.png')
rect = win.GetRect()
bitmap = screen.GetSubBitmap(rect)
else:
context = wx.ScreenDC()
memory = wx.MemoryDC()
x,y = win.GetPosition()
w,h = win.GetSize()
x0,y0 = win.ClientToScreen((0,0))
h += y0 - y + 5
w += 10
bitmap = wx.EmptyBitmap(w, h, -1)
memory.SelectObject(bitmap)
memory.Blit(0, 0, w, h, context, x, y)
memory.Destroy()
context.Destroy()
print 'Saving bitmap ', filename
bitmap.SaveFile(filename, wx.BITMAP_TYPE_PNG)
bitmap.Destroy()
def WTFC(win, filename, obj):
"""WindowToFileClient"""
if wx.Platform == '__WXMAC__':
# Blit does not write color
os.system('screencapture scr.png')
screen = wx.Bitmap('scr.png')
rect = win.GetClientRect()
rect.Offset(win.ClientToScreen((0, 0)))
bitmap = screen.GetSubBitmap(rect)
else:
context = wx.ScreenDC()
memory = wx.MemoryDC()
x,y = win.GetPosition()
w,h = win.GetSize()
x0,y0 = win.ClientToScreen((0,0))
h = obj.GetSize()[1]
# h += y0 - y + 5
# w += 10
bitmap = wx.EmptyBitmap(128, h, -1)
memory.SelectObject(bitmap)
memory.Blit(0, 0, 128, h, context, x0, y + 24)
memory.Destroy()
context.Destroy()
print 'Saving bitmap ', filename
bitmap.SaveFile(filename, wx.BITMAP_TYPE_PNG)
bitmap.Destroy()
def WCTF(win, dirname):
"""WindowChildrenToFile: save all child windows."""
if wx.Platform == '__WXMAC__':
# Blit does not write color
os.system('screencapture scr.png')
screen = wx.Bitmap('scr.png')
rect = win.GetClientRect()
rect.Offset(win.ClientToScreen((0, 0)))
bitmap = screen.GetSubBitmap(rect)
else:
context = wx.ClientDC(win)
memory = wx.MemoryDC()
x,y = win.GetPosition()
w,h = win.GetSize()
bitmap = wx.EmptyBitmap(w, h, -1)
memory.SelectObject(bitmap)
memory.Blit(0, 0, w, h, context, 0, 0)
memory.Destroy()
context.Destroy()
for w in win.GetChildren():
klass = w.GetClassName()
if w.GetName() != '-1': # replace by the true name
klass = w.GetName()
print 'Saving bitmap for', klass
filename = os.path.join(dirname, klass + '.png')
sub = bitmap.GetSubBitmap(w.GetRect())
sub.SaveFile(filename, wx.BITMAP_TYPE_PNG)
sub.Destroy()
bitmap.Destroy()
def create_panels(main_frame):
frame = res.LoadFrame(main_frame, 'FRAME_Panels')
if not frame:
print 'error loading FRAME_Panels'
return None
# Put some data
for w in frame.GetChildren():
klass = w.GetClassName()
if klass == 'wxTreeCtrl':
r = w.AddRoot('Items')
w.AppendItem(r, 'Item 1')
w.AppendItem(r, 'Item 2')
w.Expand(r)
elif klass == 'wxListCtrl':
w.InsertStringItem(0, "Item 1")
w.InsertStringItem(1, "Item 2")
w.InsertStringItem(2, "Item 3")
w.InsertStringItem(3, "Item 4")
w.InsertStringItem(4, "Item 5")
w.InsertStringItem(5, "Item 6")
w.InsertStringItem(6, "Item 7")
w.InsertStringItem(7, "Item 8")
elif klass == 'wxGrid':
w.CreateGrid(2,1)
w.AutoSizeRows()
w.AutoSizeColumns()
elif klass == 'wxScrolledWindow':
# w.SetVirtualSize((20,20))
sizer = wx.BoxSizer()
p = wx.Panel(w, size=(100,100), style=wx.SUNKEN_BORDER)
# w.SetScrollbars(1,1,10,10,1,1)
p.SetBackgroundColour(wx.WHITE)
sizer.Add(p)
w.SetSizer(sizer)
frame.Fit()
frame.Show()
return frame
def create_controls(main_frame):
frame = res.LoadFrame(main_frame, 'FRAME_Controls')
if not frame:
print 'error loading FRAME_Controls'
return None
frame.Fit()
frame.Show()
return frame
def snap(evt):
if evt.GetId() == xrc.XRCID('snap_panels'):
WCTF(app.frame_panels, 'bitmaps')
elif evt.GetId() == xrc.XRCID('snap_controls'):
WCTF(app.frame_controls, 'bitmaps')
elif evt.GetId() == xrc.XRCID('snap_frame'):
WTF(app.frame_frame, 'bitmaps/wxFrame.png')
elif evt.GetId() == xrc.XRCID('snap_dialog'):
WTF(app.frame_dialog, 'bitmaps/wxDialog.png')
elif evt.GetId() == xrc.XRCID('snap_propsheetdialog'):
print 'sleeping 1 sec'
time.sleep(1)
WTF(app.frame_propsheetdialog, 'bitmaps/wxPropertySheetDialog.png')
elif evt.GetId() == xrc.XRCID('snap_menubar'):
WTFC(app.frame_menubar, 'bitmaps/wxMenuBar.png', app.frame_menubar.GetMenuBar())
elif evt.GetId() == xrc.XRCID('snap_menu'):
WTFC(app.frame_menubar, 'bitmaps/wxMenuBar.png')
elif evt.GetId() == xrc.XRCID('snap_toolbar'):
WTFC(app.frame_menubar, 'bitmaps/wxToolBar.png', app.frame_menubar.GetToolBar())
if __name__ == '__main__':
try:
resFile = sys.argv[1]
except:
print 'usage: python maketools.py xrc_file'
sys.exit(1)
global app
app = wx.PySimpleApp(useBestVisual=False)
res = xrc.EmptyXmlResource()
res.Load(resFile)
# Main frame
main_frame = res.LoadFrame(None, 'main_frame')
assert main_frame
app.main_frame = main_frame
app.frame_panels = create_panels(main_frame)
app.frame_panels.SetPosition((0,100))
app.frame_controls = create_controls(main_frame)
app.frame_panels.SetPosition((350,100))
app.frame_frame = wx.Frame(main_frame, -1, '', (0,300), (128, 100))
app.frame_frame.Show()
app.frame_dialog = wx.Dialog(main_frame, -1, '', (140,300), (128, 100))
app.frame_dialog.SetSize((128,100))
app.frame_dialog.Show()
app.frame_propsheetdialog = res.LoadObject(main_frame, 'PROPSHEETDIALOG', 'wxPropertySheetDialog')
app.frame_propsheetdialog.Show()
app.frame_menubar = res.LoadFrame(main_frame, 'FRAME_MenuBar')
app.frame_menubar.Show()
app.frame_menubar = res.LoadFrame(main_frame, 'FRAME_ToolBar')
app.frame_menubar.Show()
if not os.path.exists('bitmaps'): os.mkdir('bitmaps')
main_frame.Bind(wx.EVT_MENU, snap, id=xrc.XRCID('snap_panels'))
main_frame.Bind(wx.EVT_MENU, snap, id=xrc.XRCID('snap_controls'))
main_frame.Bind(wx.EVT_MENU, snap, id=xrc.XRCID('snap_frame'))
main_frame.Bind(wx.EVT_MENU, snap, id=xrc.XRCID('snap_dialog'))
main_frame.Bind(wx.EVT_MENU, snap, id=xrc.XRCID('snap_propsheetdialog'))
main_frame.Bind(wx.EVT_MENU, snap, id=xrc.XRCID('snap_menubar'))
main_frame.Bind(wx.EVT_MENU, snap, id=xrc.XRCID('snap_toolbar'))
main_frame.Bind(wx.EVT_MENU, lambda evt: main_frame.Close(), id=wx.ID_EXIT)
main_frame.SetStatusText('status')
main_frame.Show()
app.MainLoop()
|