···
#########################################################################
######################################################### MDINaviWnd class
#########################################################################
import wx
NAVI_WND_SIZE = (500, 300)
-----------------------------------------------------------------
class MDINaviWnd(wx.Frame):
-----------------------------------------------------------------
def init(self, parent):
wx.Frame.init(self, parent, -1, style=wx.STAY_ON_TOP|wx.FRAME_NO_TASKBAR)
self.SetBackgroundColour(wx.GREEN)
self.Hide()
self.naviTimer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.__OnNaviTimer)
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.choiceList = wx.ListBox(self, -1, style=wx.LB_SINGLE|wx.NO_BORDER)
self.choiceList.SetBackgroundColour(wx.WHITE
)
self.choiceList.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
self.mainSizer.Add(self.choiceList, 1, wx.EXPAND|wx.ALL, 5)
self.SetSizer(self.mainSizer)
self.choiceList.Bind(wx.EVT_LISTBOX
, self.__OnListSel)
self.choiceList.Bind(wx.EVT_LISTBOX_DCLICK, self.__OnListDblClick)
self.immediatelyMode = False # Switch MDIChild imediately when list changes
self.wait_tr = 10 # init timer step
self.wait_time = 200 # wait 200 ms when wait mode
self.wait_mode = False # True if navigator appear at once
self.navi_tr = 50 # timer for Ctrl key testing
def EnableWaitMode(self, mode=True):
self.wait_mode = mode
def EnableImmediatelyMode(self, mode=True):
self.immediatelyMode = mode
def SetDelayTime(self, time):
self.wait_time = time
-----------------------------------------------------------------
def __Wait(self):
self.naviTimer.Start(self.wait_tr)
while(self.wait):
wx.SafeYield()
self.naviTimer.Stop()
-----------------------------------------------------------------
def Navigate(self):
if not self.IsShown():
self.__Start()
if not self.wait_mode:
self.__SelectNext()
else:
self.__SelectNext()
-----------------------------------------------------------------
def __Start(self):
self.__InitMembers()
self.__Wait()
if(not wx.GetKeyState(wx.WXK_CONTROL)):
self.__SwitchParentChild()
return False
self.SetSize(NAVI_WND_SIZE)
self.__GetChoices()
if self.choiceList.GetCount()<1:
return False
self.naviTimer.Start(self.navi_tr)
self.CentreOnParent()
indx = self.__GetActiveSelection()
self.choiceList.SetSelection
(indx)
self.Show()
self.GetParent().SetFocus()
return True
def __SwitchParentChild(self):
if not wx.GetKeyState(wx.WXK_SHIFT):
self.GetParent().ActivateNext()
else:
self.GetParent().ActivatePrevious()
-----------------------------------------------------------------
def __Stop(self):
self.naviTimer.Stop()
if not self.immediatelyMode:
self.__GetSelectedFrame().Activate()
self.Hide()
self.GetParent().SetFocus()
-----------------------------------------------------------------
def __GetChoices(self):
frms = self.GetParent().GetChildren()
self.choiceList.Clear
()
max_width = self.choiceList.GetSize()[0]
dc = wx.ClientDC(self.choiceList)
dots_width = dc.GetFullTextExtent("… ", self.choiceList.GetFont())[0]
for frm in frms:
if frm == self or frm.GetName() != “frame”:
continue
title = frm.GetTitle()
title_width = dc.GetFullTextExtent(title, self.choiceList.GetFont())[0]
if title_width > max_width:
while(title_width > max_width-dots_width):
title = title[:len(title)-1]
title_width = dc.GetFullTextExtent(title, self.choiceList.GetFont())[0]
title += "... "
self.choiceList.Append(title, frm.GetId())
-----------------------------------------------------------------
def __SelectNext(self):
if self.wait:
print “wait - select next”
return
if not self.immediatelyMode:
self.__SelectNextOnlyList()
else:
self.__SelectNextNow()
-----------------------------------------------------------------
def __SelectNextNow(self):
self.__SwitchParentChild()
next = self.__GetActiveSelection()
self.choiceList.SetSelection(next)
-----------------------------------------------------------------
def __SelectNextOnlyList(self):
indx = self.__GetNextListIndx(
wx.GetKeyState(wx.WXK_SHIFT))
self.choiceList.SetSelection(indx)
def __InitMembers(self):
self.wait_counter = 0 # zlicza ms oczekiwania
self.wait = self.wait_mode
-----------------------------------------------------------------
def __GetNextListIndx(self, back):
indx = self.choiceList.GetSelection()
if not back:
indx = (indx+1) % self.choiceList.GetCount()
else:
indx -= 1
if indx==-1:
indx = self.choiceList.GetCount()-1
return indx
-----------------------------------------------------------------
def __GetSelectedFrame(self):
frm_id = self.choiceList.GetClientData(self.choiceList.GetSelection
())
frm = self.GetParent().FindWindowById(frm_id)
return frm
-----------------------------------------------------------------
def __GetActiveSelection(self):
active_frm = self.GetParent
().GetActiveChild()
if active_frm:
active_frm_id = active_frm.GetId()
for indx in xrange(self.choiceList.GetCount()):
if self.choiceList.GetClientData(indx) == active_frm_id:
return indx
return 0
-----------------------------------------------------------------
def __OnNaviTimer(self, event):
if self.wait:
self.wait_counter += self.wait_tr
fail_test = not wx.GetKeyState
(wx.WXK_CONTROL)
if self.wait_counter >= self.wait_time or fail_test:
self.wait = False
return
if not wx.GetKeyState(wx.WXK_CONTROL):
self.__Stop()
if wx.GetKeyState
(wx.WXK_SPACE):
self.__GetSelectedFrame().Activate()
-----------------------------------------------------------------
def __OnListSel(self, event):
frm = self.__GetSelectedFrame()
if frm:
frm.Activate()
frm.SetFocus()
-----------------------------------------------------------------
def __OnListDblClick(self, event):
self.choiceList.SetSelection(event.GetSelection())
self.__Stop()
#########################################################################
DEMO FILE
#########################################################################
import wx
#----------------------------------------------------------------------
ID_New = wx.NewId()
ID_Exit = wx.NewId()
ID_Navi = wx.NewId()
#----------------------------------------------------------------------
#from mdiNaviWnd import *
class MyParentFrame(wx.MDIParentFrame):
def init(self):
wx.MDIParentFrame.init(self, None, -1, “MDI Parent”, size=(600,400))
self.winCount = 0
menu = wx.Menu()
menu.Append(ID_New, "&New Window")
menu.AppendSeparator()
menu.Append(ID_Exit, "E&xit")
menubar = wx.MenuBar()
menubar.Append(menu, "&File")
self.SetMenuBar(menubar)
self.naviWnd = MDINaviWnd(self)
#self.naviWnd.EnableImmediatelyMode()
#self.naviWnd.EnableWaitMode()
aTable = wx.AcceleratorTable(
[
(
wx.ACCEL_CTRL, wx.WXK_TAB, ID_Navi),
(wx.ACCEL_CTRL|wx.ACCEL_SHIFT, wx.WXK_TAB, ID_Navi)
])
self.SetAcceleratorTable(aTable)
self.Bind(wx.EVT_MENU, self.OnNewWindow, id=ID_New)
self.Bind(wx.EVT_MENU, self.OnNavi, id=ID_Navi)
self.Bind(wx.EVT_MENU, self.OnExit, id=ID_Exit)
self.OnNewWindow(None, "Child Window with short title")
self.OnNewWindow(None, "Child Window with veeeeeeery veeeeeeeeeeeeeery loooong title")
self.OnNewWindow(None, "Child Window with veeeeeeeeeeery veeeeeeeeeeeeeery loooong title")
def OnExit(self, event):
self.Close(True)
def OnNavi(self, event):
self.naviWnd.Navigate
()
def OnNewWindow(self, event=None, title=None):
self.winCount = self.winCount + 1
if title is None:
title = “Child Window #%d title” % self.winCount
win = wx.MDIChildFrame
(self, -1, title)
win.Show(True)
#----------------------------------------------------------------------
if name == ‘main’:
class MyApp(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
frame = MyParentFrame()
self.Bind(wx.EVT_KEY_DOWN, self.OnKey)
frame.Show(True)
self.SetTopWindow(frame)
return True
def OnKey(self,event):
if event.ControlDown():
if event.m_keyCode==wx.WXK_TAB:
print "ctrl tab"
app = MyApp(False)
app.MainLoop()