Problems with XmlResourceHandler for FlatNotebook

Evan Grim evan at mirgnave.com
Sun Dec 2 15:27:15 PST 2007


I'm using the FlatNotebook widget in a program I'm developing and am trying
to transition my code to use XRC.  To do this I currently need to write my
own XmlResourceHandler for XRC to use when loading my resource file.  I
built a FlatNotebookHandler class to do this (fashioned after the Notebook
handler I found in the wxWidgets code) but am having problems getting it to
work.  I've stripped this down to a (somewhat) small example program showing
what I've got and what's not working.  Hopefully one of the very smart
people that subscribe to this list can help me target what I'm not doing
correctly.  Enough chat, here's the code (my platform specifics are included
in the output text included below the code):

##BEGIN CODE#################################

import wx
from wx import xrc

from wx.lib import flatnotebook as fnb

DEBUG =3D True

xrc_source =3D """\
<?xml version=3D"1.0" ?>
<resource>
  <object class=3D"wxFrame" name=3D"mainFrame">
    <object class=3D"FlatNotebook">
      <object class=3D"flatnotebookpage">
        <object class=3D"wxPanel">
          <object class=3D"wxBoxSizer">
            <orient>wxVERTICAL</orient>
            <object class=3D"sizeritem">
              <object class=3D"wxTextCtrl">
                <value>Some multiline
text.</value>
                <style>wxTE_MULTILINE</style>
              </object>
              <option>1</option>
              <flag>wxEXPAND</flag>
            </object>
          </object>
        </object>
      </object>
      <object class=3D"flatnotebookpage">
        <object class=3D"wxPanel">
          <object class=3D"wxBoxSizer">
            <orient>wxVERTICAL</orient>
            <object class=3D"sizeritem">
              <object class=3D"wxTextCtrl">
                <value>Some multiline
text.</value>
                <style>wxTE_MULTILINE</style>
              </object>
              <option>1</option>
              <flag>wxEXPAND</flag>
            </object>
          </object>
        </object>
      </object>
      <style>FNB_NO_X_BUTTON|FNB_VC8</style>
    </object>
  </object>
</resource>
"""

class FlatNotebookXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)

        self.AddWindowStyles()

        self.AddStyle("FNB_VC71", fnb.FNB_VC71)
        self.AddStyle("FNB_FANCY_TABS", fnb.FNB_FANCY_TABS)
        self.AddStyle("FNB_TABS_BORDER_SIMPLE", fnb.FNB_TABS_BORDER_SIMPLE)
        self.AddStyle("FNB_NO_X_BUTTON", fnb.FNB_NO_X_BUTTON)
        self.AddStyle("FNB_NO_NAV_BUTTONS", fnb.FNB_NO_NAV_BUTTONS)
        self.AddStyle("FNB_MOUSE_MIDDLE_CLOSES_TABS",
fnb.FNB_MOUSE_MIDDLE_CLOSES_TABS)
        self.AddStyle("FNB_BOTTOM", fnb.FNB_BOTTOM)
        self.AddStyle("FNB_NODRAG", fnb.FNB_NODRAG)
        self.AddStyle("FNB_VC8", fnb.FNB_VC8)
        self.AddStyle("FNB_FF2", fnb.FNB_FF2)
        self.AddStyle("FNB_X_ON_TAB", fnb.FNB_X_ON_TAB)
        self.AddStyle("FNB_BACKGROUND_GRADIENT", fnb.FNB_BACKGROUND_GRADIENT
)
        self.AddStyle("FNB_COLORFUL_TABS", fnb.FNB_COLORFUL_TABS)
        self.AddStyle("FNB_DCLICK_CLOSES_TABS", fnb.FNB_DCLICK_CLOSES_TABS)
        self.AddStyle("FNB_SMART_TABS", fnb.FNB_SMART_TABS)
        self.AddStyle("FNB_DROPDOWN_TABS_LIST", fnb.FNB_DROPDOWN_TABS_LIST)
        self.AddStyle("FNB_ALLOW_FOREIGN_DND", fnb.FNB_ALLOW_FOREIGN_DND)
        self.AddStyle("FNB_HIDE_ON_SINGLE_TAB", fnb.FNB_HIDE_ON_SINGLE_TAB)

        self.isInside =3D False
        self.fnb =3D None

    def CanHandle(self, node):
        if DEBUG:
            if self.IsOfClass(node, "FlatNotebook"):
                print "XmlResource:CanHandle called with FlatNotebook node"
            elif self.IsOfClass(node, "flatnotebookpage"):
                print "XmlResource:CanHandle called with flatnotebookpage
node"
            else:
                print "XmlResource:CanHandle called with unsupported node
type"

        retVal =3D ((not self.isInside and self.IsOfClass(node,
"FlatNotebook") or
                     self.isInside and self.IsOfClass(node,
"flatnotebookpage")))
        if DEBUG:
            print "XmlResource:CanHandle returning " + str(retVal)

        return retVal

    def DoCreateResource(self):
        if self.GetClass() =3D=3D "flatnotebookpage":
            if DEBUG:
                print "XmlResource:DoCreateResource called for
flatnotebookpage"

            node =3D self.GetParamNode("object")

            if not node:
                node =3D self.GetParamNode("object_ref")

            if not node:
                wx.LogError("Error in resource: no control within
choicebook's <page> tag.")
                return None


            old_isInside =3D self.isInside
            self.isInside =3D False
            wnd =3D self.CreateResFromNode(node, self.fnb, None)
            self.isInside =3D old_isInside

            if not type(wnd, wx.Window):
                wx.LogError("Error in resource.")
                return wnd

            self.fnb.AddPage(wnd, self.GetText("label"), self.GetBool
("selected"))

            if self.HasParam("bitmap"):
                bmp =3D self.GetBitmap("bitmap", wx.ART_OTHER)
                imgList =3D self.fnb.GetImageList()

                if not imgList:
                    imgList =3D wx.ImageList(bmp.GetWidth(), bmp.GetHeight(=
))
                    self.fnb.AssignImageList(imgList)

                imgIndex =3D imgList.Add(bmp)
                self.fnb.SetPageImage(self.fnb.GetPageCount()-1, imgIndex)

            return wnd

        elif self.GetClass() =3D=3D "FlatNotebook":
            if DEBUG:
                print "XmlResource:DoCreateResource called for FlatNotebook"

            assert self.GetInstance() is None

            book =3D fnb.FlatNotebook(self.GetParentAsWindow(),
                                    self.GetID(),
                                    self.GetPosition(),
                                    self.GetSize(),
                                    self.GetStyle("style"),
                                    self.GetName())

            self.SetupWindow(book)

            oldBook =3D self.fnb
            oldInside =3D self.isInside

            self.fnb =3D book
            self.isInside =3D True

            self.CreateChildren(book, True)

            self.isInside =3D oldInside
            self.fnb =3D oldBook

            return book
        else:
            if DEBUG:
                print "XmlResource:DoCreateResource called for unknown type"


class MyApp(wx.App):

    def OnInit(self):
        self.res =3D xrc.EmptyXmlResource()
        self.res.InsertHandler(FlatNotebookXmlHandler())
        self.res.LoadFromString(xrc_source)
        self.init_frame()
        return True

    def init_frame(self):
        self.frame =3D self.res.LoadFrame(None, 'mainFrame')
        self.frame.Show()


if __name__ =3D=3D '__main__':
    if DEBUG:
        import sys
        print "Starting test application on the following platform:"
        print "\tPython: " + sys.version
        print "\twx: " + wx.version()
        print

    app =3D MyApp(False)
    app.MainLoop()

##End Code#################################

The result of running this code (at least on my machine) provides a frame,
but with none of the notebook's pages populated.  Indeed, looking at the
debugging print statements, you can see that the resource handler sees all
the calls I would expect to see for the CanHandle method, but neither of the
flatnotebookpage nodes result in a DoCreateResource call (even though
CanHandle is seen and returns True).  Here's the debug output I get:

Starting test application on the following platform:
    Python: 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
(Intel)]
    wx: 2.8.4.0 (msw-unicode)

XmlResource:CanHandle called with unsupported node type
XmlResource:CanHandle returning False
XmlResource:CanHandle called with FlatNotebook node
XmlResource:CanHandle returning True
XmlResource:DoCreateResource called for FlatNotebook
XmlResource:CanHandle called with flatnotebookpage node
XmlResource:CanHandle returning True
XmlResource:CanHandle called with flatnotebookpage node
XmlResource:CanHandle returning True

Any ideas?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/200712=
02/4eee25f1/attachment.htm


More information about the wxpython-users mailing list