class TestFrame(wx.Frame): def __init__(self, parent, log): wx.Frame.__init__(self, parent, title="Test ExpandoTextCtrl") self.log = log self.pnl = p = wx.Panel(self) self.eom = ExpandoTextCtrl( p, size=(250, -1), value="This control will expand as you type") self.Bind(EVT_ETC_LAYOUT_NEEDED, self.OnRefit, self.eom) # create some buttons and sizers to use in testing some # features and also the layout vBtnSizer = wx.BoxSizer(wx.VERTICAL) btn = wx.Button(p, -1, "Set MaxHeight") self.Bind(wx.EVT_BUTTON, self.OnSetMaxHeight, btn) vBtnSizer.Add(btn, 0, wx.ALL | wx.EXPAND, 5) btn = wx.Button(p, -1, "Set Font") self.Bind(wx.EVT_BUTTON, self.OnSetFont, btn) vBtnSizer.Add(btn, 0, wx.ALL | wx.EXPAND, 5) btn = wx.Button(p, -1, "Write Text") self.Bind(wx.EVT_BUTTON, self.OnWriteText, btn) vBtnSizer.Add(btn, 0, wx.ALL | wx.EXPAND, 5) btn = wx.Button(p, -1, "Append Text") self.Bind(wx.EVT_BUTTON, self.OnAppendText, btn) vBtnSizer.Add(btn, 0, wx.ALL | wx.EXPAND, 5) btn = wx.Button(p, -1, "Set Value") self.Bind(wx.EVT_BUTTON, self.OnSetValue, btn) vBtnSizer.Add(btn, 0, wx.ALL | wx.EXPAND, 5) btn = wx.Button(p, -1, "Get Value") self.Bind(wx.EVT_BUTTON, self.OnGetValue, btn) vBtnSizer.Add(btn, 0, wx.ALL | wx.EXPAND, 5) for x in range(3): btn = wx.Button(p, -1, " ") vBtnSizer.Add(btn, 0, wx.ALL | wx.EXPAND, 5) self.Bind(wx.EVT_BUTTON, self.OnOtherBtn, btn) hBtnSizer = wx.BoxSizer(wx.HORIZONTAL) for x in range(3): btn = wx.Button(p, -1, " ") hBtnSizer.Add(btn, 0, wx.ALL, 5) self.Bind(wx.EVT_BUTTON, self.OnOtherBtn, btn) sizer = wx.BoxSizer(wx.HORIZONTAL) col1 = wx.BoxSizer(wx.VERTICAL) col1.Add(self.eom, 0, wx.ALL, 10) col1.Add(hBtnSizer) sizer.Add(col1) sizer.Add(vBtnSizer) p.SetSizer(sizer) # Put the panel in a sizer for the frame so we can use self.Fit() frameSizer = wx.BoxSizer() frameSizer.Add(p, 1, wx.EXPAND) self.SetSizer(frameSizer) self.Fit() def OnRefit(self, evt): # The Expando control will redo the layout of the # sizer it belongs to, but sometimes this may not be # enough, so it will send us this event so we can do any # other layout adjustments needed. In this case we'll # just resize the frame to fit the new needs of the sizer. self.Fit() def OnSetMaxHeight(self, evt): mh = self.eom.GetMaxHeight() dlg = wx.NumberEntryDialog(self, "", "Enter new max height:", "MaxHeight", mh, -1, 1000) if dlg.ShowModal() == wx.ID_OK: self.eom.SetMaxHeight(dlg.GetValue()) dlg.Destroy() def OnSetFont(self, evt): dlg = wx.FontDialog(self, wx.FontData()) dlg.GetFontData().SetInitialFont(self.eom.GetFont()) if dlg.ShowModal() == wx.ID_OK: self.eom.SetFont(dlg.GetFontData().GetChosenFont()) dlg.Destroy() def OnWriteText(self, evt): self.eom.WriteText("\nThis is a test... Only a test. If this had " "been a real emergency you would have seen the " "quick brown fox jump over the lazy dog.\n") def OnAppendText(self, evt): self.eom.AppendText("\nAppended text.") def OnSetValue(self, evt): self.eom.SetValue("A new value.") def OnGetValue(self, evt): self.log.write("-----------------\n" + self.eom.GetValue()) def OnOtherBtn(self, evt): # just for testing... #print(self.eom.numLines) self.eom._adjustCtrl()
class TextLayerDialog(wx.Dialog): """ Controls setting options and displaying/hiding map overlay decorations """ def __init__(self, parent, ovlId, title, name='text', pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_DIALOG_STYLE): wx.Dialog.__init__(self, parent, wx.ID_ANY, title, pos, size, style) from wx.lib.expando import ExpandoTextCtrl, EVT_ETC_LAYOUT_NEEDED self.ovlId = ovlId self.parent = parent if self.ovlId in self.parent.MapWindow.textdict.keys(): self.currText = self.parent.MapWindow.textdict[self.ovlId]['text'] self.currFont = self.parent.MapWindow.textdict[self.ovlId]['font'] self.currClr = self.parent.MapWindow.textdict[self.ovlId]['color'] self.currRot = self.parent.MapWindow.textdict[ self.ovlId]['rotation'] self.currCoords = self.parent.MapWindow.textdict[ self.ovlId]['coords'] self.currBB = self.parent.MapWindow.textdict[self.ovlId]['bbox'] else: self.currClr = wx.BLACK self.currText = '' self.currFont = self.GetFont() self.currRot = 0.0 self.currCoords = [10, 10] self.currBB = wx.Rect() self.sizer = wx.BoxSizer(wx.VERTICAL) box = wx.GridBagSizer(vgap=5, hgap=5) # show/hide self.chkbox = wx.CheckBox(parent=self, id=wx.ID_ANY, label=_('Show text object')) if self.parent.Map.GetOverlay(self.ovlId) is None: self.chkbox.SetValue(True) else: self.chkbox.SetValue( self.parent.MapWindow.overlays[self.ovlId]['layer'].IsActive()) box.Add(item=self.chkbox, span=(1, 2), flag=wx.ALIGN_LEFT | wx.ALL, border=5, pos=(0, 0)) # text entry label = wx.StaticText(parent=self, id=wx.ID_ANY, label=_("Enter text:")) box.Add(item=label, flag=wx.ALIGN_CENTER_VERTICAL, pos=(1, 0)) self.textentry = ExpandoTextCtrl(parent=self, id=wx.ID_ANY, value="", size=(300, -1)) self.textentry.SetFont(self.currFont) self.textentry.SetForegroundColour(self.currClr) self.textentry.SetValue(self.currText) # get rid of unneeded scrollbar when text box first opened self.textentry.SetClientSize((300, -1)) box.Add(item=self.textentry, pos=(1, 1)) # rotation label = wx.StaticText(parent=self, id=wx.ID_ANY, label=_("Rotation:")) box.Add(item=label, flag=wx.ALIGN_CENTER_VERTICAL, pos=(2, 0)) self.rotation = wx.SpinCtrl(parent=self, id=wx.ID_ANY, value="", pos=(30, 50), size=(75, -1), style=wx.SP_ARROW_KEYS) self.rotation.SetRange(-360, 360) self.rotation.SetValue(int(self.currRot)) box.Add(item=self.rotation, flag=wx.ALIGN_RIGHT, pos=(2, 1)) # font fontbtn = wx.Button(parent=self, id=wx.ID_ANY, label=_("Set font")) box.Add(item=fontbtn, flag=wx.ALIGN_RIGHT, pos=(3, 1)) self.sizer.Add(item=box, proportion=1, flag=wx.ALL, border=10) # note box = wx.BoxSizer(wx.HORIZONTAL) label = wx.StaticText( parent=self, id=wx.ID_ANY, label=_("Drag text with mouse in pointer mode " "to position.\nDouble-click to change options")) box.Add(item=label, proportion=0, flag=wx.ALIGN_CENTRE | wx.ALL, border=5) self.sizer.Add(item=box, proportion=0, flag=wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER | wx.ALL, border=5) line = wx.StaticLine(parent=self, id=wx.ID_ANY, size=(20, -1), style=wx.LI_HORIZONTAL) self.sizer.Add(item=line, proportion=0, flag=wx.EXPAND | wx.ALIGN_CENTRE | wx.ALL, border=5) btnsizer = wx.StdDialogButtonSizer() btn = wx.Button(parent=self, id=wx.ID_OK) btn.SetDefault() btnsizer.AddButton(btn) btn = wx.Button(parent=self, id=wx.ID_CANCEL) btnsizer.AddButton(btn) btnsizer.Realize() self.sizer.Add(item=btnsizer, proportion=0, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5) self.SetSizer(self.sizer) self.sizer.Fit(self) # bindings self.Bind(EVT_ETC_LAYOUT_NEEDED, self.OnRefit, self.textentry) self.Bind(wx.EVT_BUTTON, self.OnSelectFont, fontbtn) self.Bind(wx.EVT_TEXT, self.OnText, self.textentry) self.Bind(wx.EVT_SPINCTRL, self.OnRotation, self.rotation) def OnRefit(self, event): """Resize text entry to match text""" self.sizer.Fit(self) def OnText(self, event): """Change text string""" self.currText = event.GetString() def OnRotation(self, event): """Change rotation""" self.currRot = event.GetInt() event.Skip() def OnSelectFont(self, event): """Change font""" data = wx.FontData() data.EnableEffects(True) data.SetColour(self.currClr) # set colour data.SetInitialFont(self.currFont) dlg = wx.FontDialog(self, data) if dlg.ShowModal() == wx.ID_OK: data = dlg.GetFontData() self.currFont = data.GetChosenFont() self.currClr = data.GetColour() self.textentry.SetFont(self.currFont) self.textentry.SetForegroundColour(self.currClr) self.Layout() dlg.Destroy() def GetValues(self): """Get text properties""" return { 'text': self.currText, 'font': self.currFont, 'color': self.currClr, 'rotation': self.currRot, 'coords': self.currCoords, 'active': self.chkbox.IsChecked() }