bad scrolling example
Nigel W. Moriarty
nw_moriarty at yahoo.com
Wed Aug 1 15:39:02 PDT 2007
Folks
I have attached a scrolled window that loses the top button on Mac.
Nigel
---
Nigel W. Moriarty
____________________________________________________________________________________
Fussy? Opinionated? Impossible to please? Perfect. Join Yahoo!'s user panel and lay it on us. http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7
-------------- next part --------------
"""
A collection of dialogs.
"""
import os
import copy
import string
import wx
from wx.html import *
from wx.lib.mixins.listctrl import ColumnSorterMixin
from wx.lib.filebrowsebutton import DirBrowseButton
class WinSzr:
def __init__(self, parent = None, win = None, szr = None,
is_notebook = 0, is_notebook_page = 0,
info = None):
self.parent = parent
self.win = win
self.szr = szr
self.is_notebook = is_notebook
self.is_notebook_page = is_notebook_page
self.info = info
assert self.parent == None or self.info != None
if (self.parent != None): self.parent.__RegisterWinChild(self)
if (self.win != None and self.szr != None):
self.win.SetAutoLayout(True)
if (not self.is_notebook):
self.win.SetSizer(self.szr)
self.winszr_children = []
self.szr_children = []
def __repr__(self):
outl = "Class: %s" % self.__class__.__name__
outl += "\n\tparent:%s" % self.parent
outl += "\n\twin:%s" % self.win
outl += "\n\tszr:%s" % self.szr
return outl
def get_winszr_children(self): return self.winszr_children
def __RegisterWinChild(self, child):
self.winszr_children.append(child)
def Destroy(self):
if (not self.is_notebook_page):
self.parent.__SzrRemoveWindow(self.win)
self.win.Destroy()
else:
index = self.parent.szr_children.index(self.win)
self.parent.__NbDeletePage(index)
self.parent.winszr_children.remove(self)
self.parent.szr.Fit(self.parent.win) # work-around
self.__dict__ = {}
def SzrAddWindow(self, window, option = 0, flag = 0, border = 0,
userData = None):
assert not self.is_notebook
if (userData == None):
self.szr.Add(window, option, flag, border)
else:
self.szr.Add(window, option, flag, border, userData)
self.szr_children.append(window)
def SzrInsertWindow(self, before, window, option = 0, flag = 0, border = 0,
userData = None):
assert not self.is_notebook
if (userData == None):
self.szr.Insert(before, window, option, flag, border)
else:
self.szr.Insert(before, window, option, flag, border, userData)
self.szr_children.insert(before, window)
def __SzrRemoveWindow(self, window):
assert not self.is_notebook
self.szr_children.remove(window)
self.szr.Remove(window)
def NbAddPage(self, page, text, select = False, imageId = -1):
assert self.is_notebook
assert page.is_notebook_page
self.win.AddPage(page.win, text, select, imageId)
self.szr_children.append(page.win)
def NbInsertPage(self, index, page, text, select = False, imageId = -1):
assert self.is_notebook
assert page.is_notebook_page
self.win.InsertPage(index, page.win, text, select, imageId)
self.szr_children.insert(index, page.win)
def __NbDeletePage(self, index):
assert self.is_notebook
self.win.DeletePage(index)
del self.szr_children[index]
def GetSzrSortedChildrenWithInfoKey(self, info_key):
siblings = [None] * len(self.szr_children)
for child in self.winszr_children:
if (child.info.has_key(info_key)):
i = self.szr_children.index(child.win)
siblings[i] = child
return filter(lambda s: s != None, siblings)
def __getitem__(self, key):
win = self.szr_children[key]
for child in self.winszr_children:
if (child.win == win): return child
raise IndexError, 'sizer child is not of type WinSzr'
def FindInSizer(self, window):
SearchWinId = window.GetId()
ix = 0
for child in self.szr_children:
if (child.GetId() == SearchWinId): return ix
ix = ix + 1
raise ValueError, 'window is not a child of this sizer'
def FindInParentSizer(self, window, groups = (), keys = ()):
# try to find window in my sizer
SearchWinId = window.GetId()
for child in self.szr_children:
if (child.GetId() == SearchWinId):
return {'parent_winszr': self,
'child_win': child,
'group_list': groups,
'key_list': keys}
# window not in my sizer: recursively try all children
siblings = None
for child in self.winszr_children:
try: child_groups = groups + (child.info['group'],)
except KeyError: child_groups = groups
child_keys = keys
if (child.info.has_key('group_param_list')):
if (siblings == None):
siblings = self.GetSzrSortedChildrenWithInfoKey('group_param_list')
ix = siblings.index(child)
child_keys = child_keys + (ix,)
result = child.FindInParentSizer(window, child_groups, child_keys)
if (result != None): return result
def RecursiveFit(self):
for child in self.winszr_children:
child.RecursiveFit()
if (self.szr != None):
S = child.win.GetSize()
self.szr.SetItemMinSize(child.win, S.width, S.height)
if (self.win != None and self.szr != None):
self.szr.Fit(self.win)
self.szr.SetSizeHints(self.win)
def FindFirstTextCtrl(self):
for child in self.szr_children:
if (string.find(str(child), 'C wxTextCtrl instance') >= 0): return child
if (child.GetName()=="textctrl"): return child
for child in self.winszr_children:
result = child.FindFirstTextCtrl()
if (result != None): return result
def CheckStatus(self):
for child in self.winszr_children:
try: status = child.info['status']
except: pass
else:
if (type(status) == type(0)):
if (status <= 0): return 0
elif status == None:
return 0
else:
for r in status:
for c in r:
if (c <= 0): return 0
if (child.CheckStatus() == 0): return 0
return 1
def FindAllTextCtrl(self, text_ctrls=[]):
for child in self.szr_children:
if (child.GetName()=="textctrl"):
text_ctrls.append(child)
for child in self.winszr_children:
text_ctrls = child.FindAllTextCtrl(text_ctrls=text_ctrls)
return text_ctrls
class wxTaskParameters:
def __init__(self, parent, task=None,
style = wx.SUNKEN_BORDER|wx.NO_FULL_REPAINT_ON_RESIZE,
wizard_buttons = False,
):
self.parent = parent
self.task = task
self.wizard_buttons = wizard_buttons
self.style = style
self.associations = {'signal': [], 'slot': []}
self.VarBorder = 5
dc = wx.WindowDC(self.parent)
dc.SetFont(self.parent.GetFont())
self.em_width = dc.GetTextExtent('m')[0]
self.jay_height = dc.GetTextExtent('J')[1]
del dc
self.sw = wx.ScrolledWindow(self.parent, wx.NewId(), style = self.style)
self.parent_is_closing = 0
wx.EVT_CLOSE(self.parent, self.OnClose)
self.RootWinSzr = None
self.Redraw()
def SetFocusOnFirstTextCtrl(self):
ctrl = self.RootWinSzr.FindFirstTextCtrl()
if (ctrl != None):
ctrl.SetFocus()
TransitionYield(onlyIfNeeded=True)
return ctrl
def ValidationOnClose(self):
ctrl = self.SetFocusOnFirstTextCtrl()
if (ctrl != None): self.validate_ctrl_d_param(ctrl)
response = None
if (self.RootWinSzr and self.RootWinSzr.CheckStatus() != 1):
dlg = wx.MessageDialog(self.parent,
'There are invalid parameters.\n'
+ 'Do you want to close the window anyway?',
'Incorrect Input',
wx.YES_NO | wx.NO_DEFAULT | wx.ICON_EXCLAMATION)
response = dlg.ShowModal()
dlg.Destroy()
TransitionYield(onlyIfNeeded=True)
return response
def OnClose(self, event):
ctrl = self.SetFocusOnFirstTextCtrl()
if (ctrl != None): self.validate_ctrl_d_param(ctrl)
response = wx.ID_YES
if not hasattr(self, "check_parameters_on_close"):
setattr(self, "check_parameters_on_close", False)
if (self.RootWinSzr and self.RootWinSzr.CheckStatus() != 1
and self.check_parameters_on_close):
dlg = wx.MessageDialog(self.parent,
'There are invalid parameters.\n'
+ 'Do you want to close the window anyway?',
'Incorrect Input',
wx.YES_NO | wx.NO_DEFAULT | wx.ICON_EXCLAMATION)
response = dlg.ShowModal()
dlg.Destroy()
TransitionYield(onlyIfNeeded=True)
if (response == wx.ID_YES):
self.parent_is_closing = 1
event.Skip()
def OnPaint(self, event):
print 'OnPaint'*10
print event
print dir(event)
def Redraw(self):
self.process_evt_kill_focus = 0
self.idle_info = None
wx.EVT_IDLE(self.parent, self.OnIdle)
if (self.RootWinSzr != None):
for child in self.RootWinSzr.get_winszr_children():
child.win.Destroy()
self.RootWinSzr = None
self.RootWinSzr = WinSzr()
self.main_panel = WinSzr(self.RootWinSzr,
wx.Panel(self.sw, wx.NewId()),
wx.BoxSizer(wx.VERTICAL),
info = {'main_panel': None})
for i in range(15):
self.show_commit_and_cancel_button(i+1)
self.SetFocusOnFirstTextCtrl()
self.FitAll()
def show_commit_and_cancel_button(self, i=0):
btn_box = WinSzr(self.main_panel,
wx.Panel(self.main_panel.win, wx.NewId(), style = wx.TAB_TRAVERSAL),
wx.BoxSizer(wx.HORIZONTAL),
info = {'commit_cancel': None})
self.main_panel.SzrAddWindow(btn_box.win)
if self.wizard_buttons:
go_btn = wx.Button(btn_box.win, wx.NewId(), 'Co&ntinue')
btn_box.SzrAddWindow(go_btn, 0, wx.ALL, border=10)#self.VarBorder)
wx.EVT_BUTTON(btn_box.win, go_btn.GetId(), self.OnGoButton)
else:
## commit_btn = wx.Button(btn_box.win, wx.NewId(), '&Save')
## btn_box.SzrAddWindow(commit_btn, 0, wx.ALL, border=self.VarBorder)
## wx.EVT_BUTTON(btn_box.win, commit_btn.GetId(), self.OnCommitButton)
go_btn = wx.Button(btn_box.win, wx.NewId(), '&Save %d' % i)
btn_box.SzrAddWindow(go_btn, 0, wx.ALL, border=10)#self.VarBorder)
wx.EVT_BUTTON(btn_box.win, go_btn.GetId(), self.OnCommitButton)
x,y = self.parent.GetSize()
cancel_btn = wx.Button(btn_box.win, wx.NewId(), '&Close %d' % i)
x = x - cancel_btn.GetSize()[0] - go_btn.GetSize()[0]
btn_box.szr.Add((x,0),0)
btn_box.SzrAddWindow(cancel_btn, 0, wx.ALL, border=10)#self.VarBorder)
wx.EVT_BUTTON(btn_box.win, cancel_btn.GetId(), self.OnCancelButton)
def OnCancelButton(self, event):
try: self.parent.OnCancel()
except AttributeError: pass
self.parent.Destroy()
def OnCommitButton(self, event):
response = self.ValidationOnClose()
if response == wx.ID_YES or response == None:
try: self.parent.OnCommit()
except AttributeError: pass
self.parent.Destroy()
def OnGoButton(self, event):
response = self.ValidationOnClose()
if response == wx.ID_YES:
if 0:
dlg = wx.MessageDialog(None,
"""
The wizard will proceed
with questionable inputs.
""","Warning",style=wx.OK)
dlg.ShowModal()
dlg.Destroy()
try: self.parent.OnGo()
except AttributeError: pass
self.parent.Close()
elif response == wx.ID_NO:
pass
else:
self.parent.Destroy()
try: self.parent.OnGo()
except AttributeError: pass
#self.parent.Destroy()
def OnIdle(self, event):
if (self.idle_info != None):
i = self.idle_info
self.idle_info = None
i.tbox.Destroy()
self.RenderTable(i.t_param, i.panel, i.panel_szr_pos, i.set_focus)
self.FitAll()
del i
def Show(self, flag):
if (flag == False): self.process_evt_kill_focus = 0
self.main_panel.win.Show(flag)
if (flag == True): self.process_evt_kill_focus = 1
def FitAll(self):
self.RootWinSzr.RecursiveFit()
(W, H) = self.main_panel.win.GetSizeTuple()
print 'main',W,H
(WP, HP) = self.sw.GetViewStart()
print 'start',WP,HP
thumb = 1
self.sw.SetScrollbars(thumb, thumb, W/thumb, H/thumb, WP, HP)
self.Show(True)
###########################################################################
# Parameter Dialog
###########################################################################
class ParameterDialog(wx.Frame):
def __init__(self, parent):
self.parent = parent
wx.Frame.__init__(self, self.parent, wx.NewId(),
" Input Parameters",
)
self.parameters = wxTaskParameters(self)
print self.parameters
display = wx.DisplaySize()
BestSize = self.parameters.main_panel.win.GetBestSize()
(W, H) = (BestSize.width, BestSize.height)
W = W + 20
H = H + 20
if (W < 400): W = 400
if (W > 800): W = 800
if (H < 400): H = 400
if (H > 800): H = 800
W = min(W,display[0]-50)
H = min(W,display[1]-50)
#wx.EVT_CLOSE(self, self.OnCancel)
self.SetClientSize(wx.Size(W, H))
self.Show(True)
def OnCommit(self):
print 'on commit'
def OnGo(self):
print 'ongo'
def OnCancel(self, event=False):
print 'cancel done'
###########################################################################
# Test loop
###########################################################################
if __name__ == '__main__':
class MyApp(wx.App):
def OnInit(self):
self.frame = ParameterDialog(None)
return True
app = MyApp(0)
app.MainLoop()
More information about the wxpython-users
mailing list