Ticket #1 (new defect)

Opened 2 years ago

Last modified 2 years ago

Layout von RichTextCtrl (etc.) in wxWidgets

Reported by: torsten Owned by: somebody
Priority: major Milestone:
Component: component1 Version:
Keywords: Cc:

Description

Das RichTextCtrl? gibt (wie auch einige andere wxWidgets) stumpf eine fest Größe (10x10 oder so) als preferred size zurück. Das ist nur für einfache Anforderungen brauchbar, z.B. wenn das Widget allein in einem wxFrame liegt.

Ich sollte mal untersuchen, ob man das nicht einfach verbessern kann. Grundsätzlich kann das RTC ja seine Ausdehnung berechnen...

Change History

Changed 2 years ago by torsten

Großartig. Die Größe initial zu setzen funktioniert fast mit angehängtem Code. Leider nicht ganz:

  • Die initiale Breite haut nicht hin (proportional vs. fixed width!?) - zu breit.
  • Die initiale Höhe haut auch nicht hin - es fehlen ein paar Pixel, es gibt also eine Scrollbar.
import wx
import wx.richtext as wxrt
import wx.lib.inspection

t1_content = """This is some text to put into the first rich text control.
Let's see how we succeed in laying this out."""
t2_content = """This is some text to put into the second rich text control.
It is longer on purpose as to have the window resized to fit both. I am not
sure how this is going to work out. We'll see..."""

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent=None, title='Layout Demo', size=(1024,768))
        self.t1 = wxrt.RichTextCtrl(self)
        self.t1.AppendText(t1_content)
        self.t2 = wxrt.RichTextCtrl(self)
        self.t2.AppendText(t2_content)

        self.dumpSize("t1")
        self.dumpSize("t2")

        sz = wx.BoxSizer(wx.VERTICAL)
        sz.Add(self.t1, flag=wx.EXPAND)
        sz.Add(self.t2, flag=wx.EXPAND)
        self.SetSizerAndFit(sz)
    
    def dumpSize(self, name):
        t = getattr(self, name)
        b = t.GetBuffer()
        b.Invalidate()
        b.Dirty = True
        b.Layout(wx.ScreenDC(), wx.Rect(0, 0, 1000000, 1000000), 0)
        size = b.GetCachedSize()
        print("Size of %s: %s" % (name, size))    
        size.height += 4
        t.SetInitialSize(size)
        t.Bind(wx.EVT_SIZE, self.sizeEvent)

    def sizeEvent(self, evt):
        print("Size event: %s" % evt.Size)
        t = evt.EventObject
        b = t.GetBuffer()
        size = b.GetCachedSize()
        print("Size of %s: %s" % (t, size))    
        
app = wx.App()
frame = MainFrame()
frame.Show()
wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
Note: See TracTickets for help on using tickets.