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
|
"""\
Spacers to use in sizers
@copyright: 2002-2007 Alberto Griggio
@copyright: 2014-2016 Carsten Grohmann
@copyright: 2016-2021 Dietmar Schwertberger
@license: MIT (see LICENSE.txt) - THIS PROGRAM COMES WITH NO WARRANTY
"""
import wx
import common, misc
import new_properties as np
from edit_windows import ManagedBase
class EditSpacer(ManagedBase):
"Class to handle spacers for sizers"
WX_CLASS = 'spacer'
IS_NAMED = False
_PROPERTIES = ["Layout", "width", "height", "proportion", "border", "flag"]
PROPERTIES = _PROPERTIES + ManagedBase.EXTRA_PROPERTIES
def __init__(self, parent, index, width, height):
ManagedBase.__init__(self, 'spacer', parent, index)
# initialise instance properties
self.width = np.SpinProperty(width, immediate=True)
self.height = np.SpinProperty(height, immediate=True)
def create_widget(self):
style = wx.SIMPLE_BORDER | wx.FULL_REPAINT_ON_RESIZE
self.widget = wx.Window(self.parent_window.widget, wx.ID_ANY, size=(self.width, self.height), style=style)
self.widget.GetBestSize = self.widget.GetSize
self.widget.Bind(wx.EVT_PAINT, self.on_paint)
def on_paint(self, event):
dc = wx.PaintDC(self.widget)
brush = wx.TheBrushList.FindOrCreateBrush( self.widget.GetBackgroundColour() )
dc.SetBrush(brush)
dc.SetPen(wx.ThePenList.FindOrCreatePen(wx.BLACK, 1, wx.SOLID))
dc.SetBackground(brush)
dc.Clear()
w, h = self.widget.GetClientSize()
dc.DrawLine(0, 0, w, h)
dc.DrawLine(w, 0, 0, h)
text = _('Spacer')
tw, th = dc.GetTextExtent(text)
x = (w - tw) // 2
y = (h - th) // 2
dc.SetPen(wx.ThePenList.FindOrCreatePen(wx.BLACK, 0, wx.TRANSPARENT))
dc.DrawRectangle(x-1, y-1, tw+2, th+2)
dc.DrawText(text, x, y)
def _properties_changed(self, modified, actions):
if not modified or "width" in modified or "height" in modified:
size = (self.width, self.height)
if self.widget: self.widget.SetSize(size)
self.parent.set_item_best_size(self, size=size)
actions.add("layout")
ManagedBase._properties_changed(self, modified, actions)
class _Dialog(wx.Dialog):
def __init__(self):
wx.Dialog.__init__(self, common.main, -1, _("Enter size"), wx.GetMousePosition())
# the controls
self.width = wx.SpinCtrl(self, -1, "20")
self.height = wx.SpinCtrl(self, -1, "20")
self.width.SetFocus()
self.width.SetSelection(-1, -1)
self.height.SetSelection(-1, -1)
# the main sizer
sizer = wx.BoxSizer(wx.VERTICAL)
# grid sizer with the controls
gsizer = wx.FlexGridSizer(cols=2)
for label, control in [("Width", self.width), ("Height", self.height)]:
gsizer.Add(wx.StaticText(self, -1, _(label)), 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
gsizer.Add(control, 0, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER_VERTICAL, 3)
sizer.Add(gsizer)
# buttons
btnbox = wx.StdDialogButtonSizer()
btn = wx.Button(self, wx.ID_OK)
btn.SetDefault()
btnbox.AddButton(btn)
btnbox.AddButton( wx.Button(self, wx.ID_CANCEL) )
btnbox.Realize()
sizer.Add(btnbox, 0, wx.EXPAND)
self.SetAutoLayout(True)
self.SetSizer(sizer)
sizer.Fit(self)
def builder(parent, index):
"factory function for EditSpacer objects"
dialog = _Dialog()
with misc.disable_stay_on_top(common.adding_window or parent):
res = dialog.ShowModal()
width = dialog.width.GetValue()
height = dialog.height.GetValue()
dialog.Destroy()
if res != wx.ID_OK:
return
with parent.frozen():
editor = EditSpacer( parent, index, width, height )
if parent.widget: editor.create()
return editor
def xml_builder(parser, base, name, parent, index):
"factory to build EditSpacer objects from a XML file"
return EditSpacer(parent, index, 1, 1)
def initialize():
"initialization function for the module: returns a wx.BitmapButton to be added to the main palette"
common.widget_classes['EditSpacer'] = EditSpacer
common.widgets['EditSpacer'] = builder
common.widgets_from_xml['EditSpacer'] = xml_builder
return common.make_object_button('EditSpacer', 'spacer.png')
|