FileBrowseButton now has history, updated demo

Added ActiveXWrapper and demos.  It can't do callback events yet, but
I'm working on it.  Can curently embed a control and make method calls
to it.

Updated the wxCalendar (python version)  and it's demo.  It now has
printing support.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_2_BRANCH@7136 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2000-04-12 03:06:57 +00:00
parent 7b84e4c356
commit aaca0586f2
11 changed files with 757 additions and 128 deletions

View File

@@ -0,0 +1,127 @@
"""
ActiveXControl sample usage.
Based on original code by Willy Heineman, wheineman@uconect.net, 14 February 2000
ActiveXControl provides a simplified wrapper for creating activeX
controls within wxPython GUIs. It allows you to include the activeX
control in a sizer, and includes a flag to prevent redrawing the
background behind the control (reduces the annoying flicker on
Win32 systems).
"""
from wxPython.wx import *
from wxPython.lib.activexwrapper import ActiveXWrapper
import pythoncom
import pywin.mfc.activex
import win32com.client.gencache
import win32con
import win32ui
acrobatOCX = win32com.client.gencache.EnsureModule('{CA8A9783-280D-11CF-A24D-444553540000}', 0x0, 1, 3)
if acrobatOCX is None:
win32ui.MessageBox("Can't load PDF.OCX, install Acrobat 4.0",
'ActiveX Demo',
win32con.MB_OK | win32con.MB_ICONSTOP)
raise ImportError
#----------------------------------------------------------------------
# NOTE:
# Your ActiveX control _must_ be an instance of pywin.mfc.activex.Control,
# as we use the CreateControl method to bind the GUI systems.
class PDFControl(pywin.mfc.activex.Control, acrobatOCX.Pdf):
def __init__(self):
pywin.mfc.activex.Control.__init__(self)
acrobatOCX.Pdf.__init__(self)
#----------------------------------------------------------------------
class TestPanel(wxPanel):
def __init__(self, parent, log):
wxPanel.__init__(self, parent, -1)
self.log = log
sizer = wxBoxSizer(wxVERTICAL)
btnSizer = wxBoxSizer(wxHORIZONTAL)
# Build the wxPython wrapper window and put the Acrobat
# control in it
self.axw = ActiveXWrapper( self, -1, style=wxSUNKEN_BORDER, eraseBackground=0)
self.axw.SetControl( PDFControl() )
sizer.Add(self.axw, 1, wxEXPAND)
btn = wxButton(self, wxNewId(), "Open PDF File")
EVT_BUTTON(self, btn.GetId(), self.OnOpenButton)
btnSizer.Add(btn, 1, wxEXPAND|wxALL, 5)
btn = wxButton(self, wxNewId(), "<-- Previous Page")
EVT_BUTTON(self, btn.GetId(), self.OnPrevPageButton)
btnSizer.Add(btn, 1, wxEXPAND|wxALL, 5)
btn = wxButton(self, wxNewId(), "Next Page -->")
EVT_BUTTON(self, btn.GetId(), self.OnNextPageButton)
btnSizer.Add(btn, 1, wxEXPAND|wxALL, 5)
btnSizer.Add(50, -1, 2, wxEXPAND)
sizer.Add(btnSizer, 0, wxEXPAND)
self.SetSizer(sizer)
self.SetAutoLayout(true)
def __del__(self):
self.axw.Cleanup()
self.axw = None
def OnOpenButton(self, event):
dlg = wxFileDialog(self, wildcard="*.pdf")
if dlg.ShowModal() == wxID_OK:
self.axw.control.LoadFile(dlg.GetPath())
dlg.Destroy()
def OnPrevPageButton(self, event):
self.axw.control.gotoPreviousPage()
def OnNextPageButton(self, event):
self.axw.control.gotoNextPage()
#----------------------------------------------------------------------
def runTest(frame, nb, log):
win = TestPanel(nb, log)
return win
overview = __doc__
#----------------------------------------------------------------------
if __name__ == '__main__':
class TestFrame(wxFrame):
def __init__(self):
wxFrame.__init__(self, None, -1, "ActiveX test -- Acrobat", size=(640, 480))
self.tp = TestPanel(self, sys.stdout)
def OnCloseWindow(self, event):
self.tp.axw.Cleanup()
self.Destroy()
app = wxPySimpleApp()
frame = TestFrame()
frame.Show(true)
app.MainLoop()

View File

@@ -0,0 +1,133 @@
"""
ActiveXControl sample usage.
Based on original code by Willy Heineman, wheineman@uconect.net, 14 February 2000
ActiveXControl provides a simplified wrapper for creating activeX
controls within wxPython GUIs.
"""
from wxPython.wx import *
from wxPython.lib.activexwrapper import ActiveXWrapper
import pythoncom
import pywin.mfc.activex
import win32com.client.gencache
import win32con
import win32ui
browserModule = win32com.client.gencache.EnsureModule("{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1)
if browserModule is None:
win32ui.MessageBox("IE4 or greater does not appear to be installed.",
'ActiveX Demo',
win32con.MB_OK | win32con.MB_ICONSTOP)
raise ImportError
#----------------------------------------------------------------------
# NOTE:
# Your ActiveX control _must_ be an instance of pywin.mfc.activex.Control,
# as we use the CreateControl method to bind the GUI systems.
class WebBrowserControl(pywin.mfc.activex.Control,
browserModule.WebBrowser):
def __init__(self):
pywin.mfc.activex.Control.__init__(self)
browserModule.WebBrowser.__init__(self)
#----------------------------------------------------------------------
class TestPanel(wxPanel):
def __init__(self, parent, log):
wxPanel.__init__(self, parent, -1)
self.log = log
self.current = "http://alldunn.com/"
sizer = wxBoxSizer(wxVERTICAL)
btnSizer = wxBoxSizer(wxHORIZONTAL)
# Build the wxPython wrapper window and put the Acrobat
# control in it
self.axw = ActiveXWrapper( self, -1, style=wxSUNKEN_BORDER, eraseBackground=0)
self.axw.SetControl( WebBrowserControl() )
sizer.Add(self.axw, 1, wxEXPAND)
self.axw.control.Navigate(self.current)
btn = wxButton(self, wxNewId(), "Open Location")
EVT_BUTTON(self, btn.GetId(), self.OnOpenButton)
btnSizer.Add(btn, 1, wxEXPAND|wxALL, 5)
btn = wxButton(self, wxNewId(), "<-- Back Page")
EVT_BUTTON(self, btn.GetId(), self.OnPrevPageButton)
btnSizer.Add(btn, 1, wxEXPAND|wxALL, 5)
btn = wxButton(self, wxNewId(), "Forward Page -->")
EVT_BUTTON(self, btn.GetId(), self.OnNextPageButton)
btnSizer.Add(btn, 1, wxEXPAND|wxALL, 5)
btnSizer.Add(50, -1, 2, wxEXPAND)
sizer.Add(btnSizer, 0, wxEXPAND)
self.SetSizer(sizer)
self.SetAutoLayout(true)
def __del__(self):
self.axw.Cleanup()
self.axw = None
def OnOpenButton(self, event):
dlg = wxTextEntryDialog(self, "Open Location",
"Enter a full URL or local path",
self.current, wxOK|wxCANCEL)
dlg.CentreOnParent()
if dlg.ShowModal() == wxID_OK:
self.current = dlg.GetValue()
self.axw.control.Navigate(self.current)
dlg.Destroy()
def OnPrevPageButton(self, event):
self.axw.control.GoBack()
def OnNextPageButton(self, event):
self.axw.control.GoForward()
#----------------------------------------------------------------------
# for the demo framework...
def runTest(frame, nb, log):
win = TestPanel(nb, log)
return win
overview = __doc__
#----------------------------------------------------------------------
if __name__ == '__main__':
class TestFrame(wxFrame):
def __init__(self):
wxFrame.__init__(self, None, -1, "ActiveX test -- Internet Explorer", size=(640, 480))
self.tp = TestPanel(self, sys.stdout)
def OnCloseWindow(self, event):
self.tp.axw.Cleanup()
self.Destroy()
app = wxPySimpleApp()
frame = TestFrame()
frame.Show(true)
app.MainLoop()

View File

@@ -1,13 +1,38 @@
from wxPython.wx import *
from wxPython.lib.filebrowsebutton import FileBrowseButton
from wxPython.lib.filebrowsebutton import FileBrowseButton, FileBrowseButtonWithHistory
#----------------------------------------------------------------------
class TestPanel(wxPanel):
def __init__(self, parent, ID, log):
wxPanel.__init__(self, parent, ID)
self.log = log
self.fbb = FileBrowseButton(self, -1, wxPoint(20,20), wxSize(350, -1),
changeCallback = self.fbbCallback)
self.fbbh = FileBrowseButtonWithHistory(self, -1, wxPoint(20, 50),
wxSize(350, -1),
changeCallback = self.fbbhCallback)
self.fbbh.SetHistory([])
def fbbCallback(self, evt):
self.log.write('FileBrowseButton: %s\n' % evt.GetString())
def fbbhCallback(self, evt):
value = evt.GetString()
self.log.write('FileBrowseButtonWithHistory: %s\n' % value)
history = self.fbbh.GetHistory()
history.append(value)
self.fbbh.SetHistory(history)
#----------------------------------------------------------------------
def runTest(frame, nb, log):
win = wxPanel(nb, -1)
fbb = FileBrowseButton(win, -1, wxPoint(20,20), wxSize(350, -1))
win = TestPanel(nb, -1, log)
return win

View File

@@ -72,7 +72,7 @@ class CustomDataTable(wxPyGridTableBase):
#--------------------------------------------------
# optional methods
# Some optional methods
# Called when the grid needs to display labels
def GetColLabelValue(self, col):
@@ -101,8 +101,6 @@ class CustomDataTable(wxPyGridTableBase):
#---------------------------------------------------------------------------

View File

@@ -21,7 +21,9 @@ _useSplitter = true
_useNestedSplitter = true
_treeList = [
('New since last release', ['wxGrid']),
('New since last release', ['wxGrid', 'wxStyledTextCtrl',
'ActiveXWrapper_Acrobat', 'ActiveXWrapper_IE',
'FileBrowseButton', 'wxCalendar']),
('Managed Windows', ['wxFrame', 'wxDialog', 'wxMiniFrame']),

View File

@@ -0,0 +1,83 @@
#----------------------------------------------------------------------
# Name: wxPython.lib.activexwrapper
# Purpose: a wxWindow derived class that can hold an ActiveX control
#
# Author: Mike Fletcher, Robin Dunn
#
# RCS-ID: $Id$
# Copyright: (c) 2000 by Total Control Software
# Licence: wxWindows license
#----------------------------------------------------------------------
from wxPython.wx import *
try:
import win32ui
except ImportError:
raise ImportError( "Cannot find win32ui module, wxWindows ActiveXControl module cannot be loaded")
### Following makes it possible to include this module without
# needing to include the (rather large) win32con module...
##from win32con import WS_TABSTOP, WS_VISIBLE
WS_TABSTOP = 0x10000
WS_VISIBLE = 0x10000000
class ActiveXWrapper( wxWindow ):
"""
Class providing easy integration of ActiveXControls into
wxPython GUI environments. Pass an instantiated control class (one
which inherits from pywin.mfc.activex.Control and a particular control
class generated by gencache) in the argument "control". Alternatively,
pass the instantiated control to the SetControl method to allow for
specifying the win32 style, id, etceteras
"""
def __init__( self, parent, ID=-1, pos = wxDefaultPosition, size = wxDefaultSize,
style = 0,
name = "ActiveXControlWindow",
control = None,
controlName = "Control",
eraseBackground = 1,
):
wxWindow.__init__( self, parent, ID, pos, size, style, name )
win32ui.EnableControlContainer()
self.windowPointer = win32ui.CreateWindowFromHandle(self.GetHandle())
if control:
self.SetControl( control, name )
EVT_SIZE( self, self.OnSize)
if not eraseBackground:
def nullfunction( event ): pass
EVT_ERASE_BACKGROUND( self, nullfunction )
def SetControl( self, controlObject, name="Control",
style=WS_TABSTOP | WS_VISIBLE, ID=None):
"""
Pass a pre-built control object, calls the control's
CreateControl method with the proper arguments to make it a
child of this window
"""
self.control = controlObject
w,h = self.GetSizeTuple()
if not ID:
ID = wxNewId()
self.control.CreateControl( name, style, (0,0,w,h),
self.windowPointer,
ID)
def Cleanup( self ):
"""Delete the reference to the control, to ensure that it is dereferenced."""
self.control = None
self.windowPointer = None
def OnSize( self, event=None ):
"""wxPython resize event, call MoveWindow on the control"""
size = self.GetClientSize()
self.control.MoveWindow( (0,0,size.width,size.height),1)

View File

@@ -5,7 +5,7 @@
# Author: Lorne White (email: lwhite1@planet.eon.net)
#
# Created:
# Version 0.5 1999/11/03
# Version 0.6 2000/03/30
# Licence: wxWindows license
#----------------------------------------------------------------------------
@@ -17,24 +17,75 @@ import string, time
CalDays = [6, 0, 1, 2, 3, 4, 5]
AbrWeekday = {6:"Sun", 0:"Mon", 1:"Tue", 2:"Wed", 3:"Thu", 4:"Fri", 5:"Sat"}
_MIDSIZE = 100
_MIDSIZE = 160
# calendar drawing routing
class CalDraw:
def __init__(self):
self.rg = {}
self.y_st = 15 # start of vertical draw default
def __init__(self, parent):
self.pwidth = 1
self.pheight = 1
try:
self.scale = parent.scale
except:
self.scale = 1
self.DefParms()
def DefParms(self):
self.grid_color = 'BLACK'
self.back_color = 'WHITE'
self.sel_color = 'RED'
self.high_color = 'LIGHT BLUE'
self.border_color = 'BLACK'
self.week_color = 'LIGHT GREY'
self.font = wxSWISS
self.bold = wxNORMAL
self.hide_title = FALSE
self.hide_grid = FALSE
self.outer_border = TRUE
self.title_offset = 0
def SetSize(self, size):
self.sizew = size.width
self.sizeh = size.height
self.set_sizew = size.width
self.set_sizeh = size.height
# draw the various elements of the calendar
def InitValues(self): # default dimensions of various elements of the calendar
self.rg = {}
self.set_cy_st = 0 # start position
self.set_cx_st = 0
self.set_y_mrg = 15 # start of vertical draw default
self.set_x_mrg = 10
self.set_y_end = 10
def SetPos(self, xpos, ypos):
self.set_cx_st = xpos
self.set_cy_st = ypos
def SetMarg(self, xmarg, ymarg):
self.set_x_st = xmarg
self.set_y_st = ymarg
self.set_y_end = ymarg
def InitScale(self): # scale position values
self.sizew = self.set_sizew * self.pwidth
self.sizeh = self.set_sizeh * self.pheight
self.cx_st = self.set_cx_st * self.pwidth # draw start position
self.cy_st = self.set_cy_st * self.pheight
self.x_mrg = self.set_x_mrg * self.pwidth # calendar draw margins
self.y_mrg = self.set_y_mrg * self.pheight
self.y_end = self.set_y_end * self.pheight
def DrawCal(self, DC, sel_lst):
self.DC = DC
self.InitScale()
self.DrawBorder()
@@ -50,11 +101,14 @@ class CalDraw:
self.DrawWeek()
self.DrawNum()
# draw border around the outside of the main display rectangle
def DrawBorder(self): # draw border around the outside of the main display rectangle
brush = wxBrush(wxNamedColour(self.back_color), wxSOLID)
self.DC.SetBrush(brush)
self.DC.SetPen(wxPen(wxNamedColour(self.border_color), 1))
def DrawBorder(self):
rect = wxRect(0, 0, self.sizew, self.sizeh) # full display window area
self.DC.DrawRectangle(rect.x, rect.y, rect.width, rect.height)
if self.outer_border is TRUE:
rect = wxRect(self.cx_st, self.cy_st, self.sizew, self.sizeh) # full display window area
self.DC.DrawRectangle(rect.x, rect.y, rect.width, rect.height)
def DrawNumVal(self):
self.DrawNum()
@@ -62,6 +116,8 @@ class CalDraw:
# calculate the calendar days and offset position
def SetCal(self, year, month):
self.InitValues() # reset initial values
self.year = year
self.month = month
@@ -81,9 +137,7 @@ class CalDraw:
i = i + 1
return start_pos
# get the display rectange list of the day grid
def GetRect(self):
def GetRect(self): # get the display rectange list of the day grid
cnt = 0
for y in self.gridy[1:-1]:
for x in self.gridx[:-1]:
@@ -98,12 +152,10 @@ class CalDraw:
def GetOffset(self):
return self.st_pos
# month and year title
def DrawMonth(self):
def DrawMonth(self): # month and year title
month = Month[self.month]
sizef = 12
sizef = 11
if self.sizeh < _MIDSIZE:
sizef = 10
@@ -111,25 +163,23 @@ class CalDraw:
self.DC.SetFont(f)
tw,th = self.DC.GetTextExtent(month)
adjust = (self.sizew-tw)/2
self.DC.DrawText(month, adjust, 10)
adjust = self.cx_st + (self.sizew-tw)/2
self.DC.DrawText(month, adjust, self.cy_st + th)
year = str(self.year)
tw,th = self.DC.GetTextExtent(year)
adjust = self.sizew-tw-20
adjust = self.cx_st + self.sizew-tw-20
self.y_st = th * 3
self.title_offset = th * 2
f = wxFont(sizef, self.font, wxNORMAL, self.bold)
self.DC.SetFont(f)
self.DC.DrawText(year, adjust, 10)
self.DC.DrawText(year, adjust, self.cy_st + th)
# draw the week days
def DrawWeek(self):
sizef = 10
def DrawWeek(self): # draw the week days
sizef = 8
if self.sizeh < _MIDSIZE:
sizef = 8
sizef = 7
f = wxFont(sizef, self.font, wxNORMAL, self.bold)
self.DC.SetFont(f)
@@ -139,6 +189,12 @@ class CalDraw:
width = self.gridx[1]-self.gridx[0]
height = self.gridy[1] - self.gridy[0]
rect_w = self.gridx[7]-self.gridx[0]
brush = wxBrush(wxNamedColour(self.week_color), wxSOLID)
self.DC.SetBrush(brush)
# self.DC.DrawRectangle(self.gridx[0], self.gridy[0], rect_w+1, height)
for val in CalDays:
day = AbrWeekday[val]
if self.sizew < 200:
@@ -149,12 +205,12 @@ class CalDraw:
x = self.gridx[cnt_x]
y = self.gridy[cnt_y]
self.DC.DrawRectangle(self.gridx[cnt_x], self.gridy[0], width+1, height)
self.DC.DrawText(day, x+diffx, y+diffy)
cnt_x = cnt_x + 1
# draw the day numbers
def DrawNum(self):
def DrawNum(self): # draw the day numbers
sizef = 10
if self.sizeh < _MIDSIZE:
sizef = 8
@@ -173,24 +229,18 @@ class CalDraw:
cnt_x = 0
cnt_y = cnt_y + 1
# calculate the dimensions in the center of the drawing area
def Center(self): # calculate the dimensions in the center of the drawing area
bdw = self.x_mrg * 2
bdh = self.y_mrg + self.y_end + self.title_offset
def Center(self):
self.x_st = 10
self.y_end = 10
bdw = self.x_st * 2
bdh = self.y_st + self.y_end
self.dl_w = (self.sizew-bdw)/7
self.dl_h = (self.sizeh-bdh)/7
self.dl_w = int((self.sizew-bdw)/7)
self.dl_h = int((self.sizeh-bdh)/7)
self.dl_th = int(2*self.dl_h/3) # week title adjustment
self.cwidth = self.dl_w * 7
self.cheight = self.dl_h * 6 + self.dl_h/2
self.cheight = self.dl_h * 6 + self.dl_th
# highlighted selectioned days
def DrawSel(self, sel_lst):
def DrawSel(self, sel_lst): # highlighted selected days
for key in sel_lst:
brush = wxBrush(wxNamedColour(self.high_color), wxSOLID)
self.DC.SetBrush(brush)
@@ -202,18 +252,19 @@ class CalDraw:
rect = self.rg[nkey]
self.DC.DrawRectangle(rect.x, rect.y, rect.width+1, rect.height+1)
# calculate and draw the grid lines
def DrawGrid(self):
def DrawGrid(self): # calculate and draw the grid lines
self.DC.SetPen(wxPen(wxNamedColour(self.grid_color), 0))
self.gridx = []
self.gridy = []
self.x_st = self.cx_st + self.x_mrg
self.y_st = self.cy_st + self.y_mrg + self.title_offset # start postion of draw
x1 = self.x_st
y1 = self.y_st
y1 = self.y_st
y2 = self.y_st + self.cheight
y2 = y1 + self.cheight
for i in range(8):
if self.hide_grid is FALSE:
self.DC.DrawLine(x1, y1, x1, y2)
@@ -222,17 +273,35 @@ class CalDraw:
x1 = self.x_st
y1 = self.y_st
x2 = self.x_st + self.cwidth
x2 = x1 + self.cwidth
for i in range(8):
if self.hide_grid is FALSE:
self.DC.DrawLine(x1, y1, x2, y1)
self.gridy.append(y1)
if i == 0:
y1 = y1 + self.dl_h/2
y1 = y1 + self.dl_th
else:
y1 = y1 + self.dl_h
class PrtCalDraw(CalDraw):
def InitValues(self):
self.rg = {}
self.set_cx_st = 1.0 # start draw border location
self.set_cy_st = 1.0
self.set_y_mrg = 0.2 # draw offset position
self.set_x_mrg = 0.2
self.set_y_end = 0.2
def SetPSize(self, pwidth, pheight): # calculate the dimensions in the center of the drawing area
self.pwidth = int(pwidth)/self.scale
self.pheight = int(pheight)/self.scale
def SetPreview(self, preview):
self.preview = preview
class wxCalendar(wxWindow):
def __init__(self, parent, id, pos=wxDefaultPosition, size=wxDefaultSize):
wxWindow.__init__(self, parent, id, pos, size)
@@ -241,10 +310,9 @@ class wxCalendar(wxWindow):
self.grid_color = 'BLACK'
self.back_color = 'WHITE'
self.hide_grid = FALSE
self.sel_color = 'RED'
self.high_color = 'LIGHT BLUE'
self.font = wxSWISS
self.bold = wxNORMAL
self.hide_title = FALSE
self.SetBackgroundColour(wxNamedColor(self.back_color))
self.Connect(-1, -1, wxEVT_LEFT_DOWN, self.OnLeftEvent)
@@ -258,8 +326,6 @@ class wxCalendar(wxWindow):
self.SetNow() # default calendar for current month
self.size = None
self.hide_title = FALSE
self.hide_grid = FALSE
self.set_day = None
# control some of the main calendar attributes
@@ -390,9 +456,8 @@ class wxCalendar(wxWindow):
def GetDayHit(self, mx, my):
for key in self.rg.keys():
val = self.rg[key]
rt = wxRegion()
rt.UnionRect(val)
if rt.Contains(mx, my) != 0:
ms_rect = wxRect(mx, my, 1, 1)
if wxIntersectRect(ms_rect, val) != None:
result = self.TestDay(key)
return result
return None
@@ -407,7 +472,8 @@ class wxCalendar(wxWindow):
DC = wxPaintDC(self)
DC.BeginDrawing()
self.cal = cal = CalDraw()
self.cal = cal = CalDraw(self)
if self.size is None:
size = self.GetClientSize()
else:
@@ -415,14 +481,11 @@ class wxCalendar(wxWindow):
# drawing attributes
cal.hide_title = self.hide_title
cal.hide_grid = self.hide_grid
cal.grid_color = self.grid_color
cal.high_color = self.high_color
cal.grid_color = 'BLUE'
cal.back_color = self.back_color
cal.font = self.font
cal.bold = self.bold
cal.hide_grid = self.hide_grid
cal.grid_color = self.grid_color
cal.hide_title = self.hide_title
cal.SetSize(size)
cal.SetCal(self.year, self.month)

View File

@@ -1,10 +1,24 @@
#----------------------------------------------------------------------
# Name: wxPython.lib.filebrowsebutton
# Purpose: Composite controls that provide a Browse button next to
# either a wxTextCtrl or a wxComboBox. The Browse button
# launches a wxFileDialog and loads the result into the
# other control.
#
# Author: Mike Fletcher
#
# RCS-ID: $Id$
# Copyright: (c) 2000 by Total Control Software
# Licence: wxWindows license
#----------------------------------------------------------------------
from wxPython.wx import *
import os
#----------------------------------------------------------------------
class FileBrowseButton(wxPanel):
''' A control to allow the user to type in a filename
""" A control to allow the user to type in a filename
or browse with the standard file dialog to select file
__init__ (
@@ -24,49 +38,55 @@ class FileBrowseButton(wxPanel):
label -- pointer to internal label widget
textControl -- pointer to internal text control
browseButton -- pointer to button
'''
"""
def __init__ (self, parent, id= -1,
pos = wxDefaultPosition, size = wxDefaultSize,
style = wxTAB_TRAVERSAL,
labelText= "File Entry:",
buttonText= "Browse",
toolTip= "Type filename or browse computer to choose file",
# following are the values for a file dialog box
dialogTitle = "Choose a file",
startDirectory = ".",
initialValue = "",
fileMask = "*.*",
fileMode = wxOPEN,
# callback for when value changes (optional)
changeCallback= None
pos = wxDefaultPosition, size = wxDefaultSize,
style = wxTAB_TRAVERSAL,
labelText= "File Entry:",
buttonText= "Browse",
toolTip= "Type filename or click browse to choose file",
# following are the values for a file dialog box
dialogTitle = "Choose a file",
startDirectory = ".",
initialValue = "",
fileMask = "*.*",
fileMode = wxOPEN,
# callback for when value changes (optional)
changeCallback= lambda x:x
):
wxPanel.__init__ (self, parent, id, pos, size, style)
# store variables
self.labelText = labelText
self.buttonText = buttonText
self.toolTip = toolTip
self.dialogTitle = dialogTitle
self.startDirectory = startDirectory
self.initialValue = initialValue
self.fileMask = fileMask
self.fileMode = fileMode
self.changeCallback = changeCallback
# create the dialog
self.createDialog(parent, id, pos, size, style )
# Setting a value causes the changeCallback to be called.
# In this case that would be before the return of the
# constructor. Not good. So a default value on
# SetValue is used to disable the callback
self.SetValue( initialValue, 0)
def createDialog( self, parent, id, pos, size, style ):
"""Setup the graphic representation of the dialog"""
wxPanel.__init__ (self, parent, id, pos, size, style)
box = wxBoxSizer(wxHORIZONTAL)
self.label = wxStaticText(self, -1, labelText, style =wxALIGN_RIGHT )
font = self.label.GetFont()
w, h, d, e = self.GetFullTextExtent(labelText, font)
self.label.SetSize(wxSize(w+5, h))
self.label = self.createLabel( )
box.Add( self.label, 0, wxCENTER )
ID = wxNewId()
self.textControl = wxTextCtrl(self, ID)
self.textControl.SetToolTipString( toolTip )
self.textControl = self.createTextControl()
box.Add( self.textControl, 1, wxLEFT|wxCENTER, 5)
if changeCallback:
EVT_TEXT(self.textControl, ID, changeCallback)
ID = wxNewId()
self.browseButton = button =wxButton(self, ID, buttonText)
box.Add( button, 0, wxCENTER)
button.SetToolTipString( toolTip )
EVT_BUTTON(button, ID, self.OnBrowse)
self.browseButton = self.createBrowseButton()
box.Add( self.browseButton, 0, wxCENTER)
# add a border around the whole thing and resize the panel to fit
outsidebox = wxBoxSizer(wxVERTICAL)
@@ -79,14 +99,38 @@ class FileBrowseButton(wxPanel):
if size.width != -1 or size.height != -1:
self.SetSize(size)
def createLabel( self ):
"""Create the label/caption"""
label = wxStaticText(self, -1, self.labelText, style =wxALIGN_RIGHT )
font = label.GetFont()
w, h, d, e = self.GetFullTextExtent(self.labelText, font)
label.SetSize(wxSize(w+5, h))
return label
def createTextControl( self):
"""Create the text control"""
ID = wxNewId()
textControl = wxTextCtrl(self, ID)
textControl.SetToolTipString( self.toolTip )
if self.changeCallback:
EVT_TEXT(textControl, ID, self.changeCallback)
EVT_COMBOBOX(textControl, ID, self.changeCallback)
return textControl
def createBrowseButton( self):
"""Create the browse-button control"""
ID = wxNewId()
button =wxButton(self, ID, self.buttonText)
button.SetToolTipString( self.toolTip )
EVT_BUTTON(button, ID, self.OnBrowse)
return button
def OnBrowse (self, event = None):
''' Going to browse for file... '''
""" Going to browse for file... """
current = self.GetValue ()
directory = os.path.split(current)
if os.path.isdir( current):
directory =current
directory = current
elif directory and os.path.isdir( directory[0] ):
directory = directory [0]
else:
@@ -95,56 +139,210 @@ class FileBrowseButton(wxPanel):
if dlg.ShowModal() == wxID_OK:
self.SetValue (dlg.GetPath())
dlg.Destroy()
self.textControl.SetFocus()
def GetValue (self):
''' Convenient access to text control value '''
""" Convenient access to text control value """
return self.textControl.GetValue ()
def SetValue (self, value):
''' Convenient setting of text control value '''
return self.textControl.SetValue (value)
def SetValue (self, value, callBack=1):
""" Convenient setting of text control value """
# Removed the return from here because SetValue doesn't return anything.
self.textControl.SetValue (value)
def Enable (self, value):
''' Convenient enabling/disabling of entire control '''
""" Convenient enabling/disabling of entire control """
self.label.Enable (value)
self.textControl.Enable (value)
return self.browseButton.Enable (value)
def GetLabel( self ):
''' Retrieve the label's current text '''
""" Retrieve the label's current text """
return self.label.GetLabel()
def SetLabel( self, value ):
''' Set the label's current text '''
return self.label.SetLabel( value )
""" Set the label's current text """
rvalue = self.label.SetLabel( value )
self.Refresh( true )
return rvalue
class FileBrowseButtonWithHistory( FileBrowseButton ):
""" with following additions:
__init__(..., history=None)
history -- optional list of paths for initial history drop-down
(must be passed by name, not a positional argument)
If history is callable it will must return a list used
for the history drop-down
changeCallback -- as for FileBrowseButton, but with a work-around
for win32 systems which don't appear to create EVT_COMBOBOX
events properly. There is a (slight) chance that this work-around
will cause some systems to create two events for each Combobox
selection. If you discover this condition, please report it!
As for a FileBrowseButton.__init__ otherwise.
GetHistoryControl()
Return reference to the control which implements interfaces
required for manipulating the history list. See GetHistoryControl
documentation for description of what that interface is.
GetHistory()
Return current history list
SetHistory( value=(), selectionIndex = None )
Set current history list, if selectionIndex is not None, select that index
"""
def __init__( self, *arguments, **namedarguments):
self.history = namedarguments.get( "history" )
if self.history:
del namedarguments["history"]
self.historyCallBack=None
if callable(self.history):
self.historyCallBack=self.history
self.history=None
apply( FileBrowseButton.__init__, ( self,)+arguments, namedarguments)
def createTextControl( self):
"""Create the text control"""
ID = wxNewId()
textControl = wxComboBox(self, ID, style = wxCB_DROPDOWN )
textControl.SetToolTipString( self.toolTip )
EVT_SET_FOCUS(textControl, self.OnSetFocus)
if self.changeCallback:
EVT_TEXT(textControl, ID, self.changeCallback)
EVT_COMBOBOX(textControl, ID, self.changeCallback)
if self.history:
history=self.history
self.history=None
self.SetHistory( history, control=textControl)
return textControl
def GetHistoryControl( self ):
"""Return a pointer to the control which provides (at least)
the following methods for manipulating the history list.:
Append( item ) -- add item
Clear() -- clear all items
Delete( index ) -- 0-based index to delete from list
SetSelection( index ) -- 0-based index to select in list
Semantics of the methods follow those for the wxComboBox control
"""
return self.textControl
def SetHistory( self, value=(), selectionIndex = None, control=None ):
"""Set the current history list"""
if control is None:
control = self.GetHistoryControl()
if self.history == value:
return
self.history = value
# Clear history values not the selected one.
tempValue=control.GetValue()
# clear previous values
control.Clear()
control.SetValue(tempValue)
# walk through, appending new values
for path in value:
control.Append( path )
if selectionIndex is not None:
control.SetSelection( selectionIndex )
def GetHistory( self ):
"""Return the current history list"""
if self.historyCallBack != None:
return self.historyCallBack()
else:
return list( self.history )
def OnSetFocus(self, event):
"""When the history scroll is selected, update the history"""
if self.historyCallBack != None:
self.SetHistory( self.historyCallBack(), control=self.textControl)
event.Skip()
if wxPlatform == "__WXMSW__":
def SetValue (self, value, callBack=1):
""" Convenient setting of text control value, works
around limitation of wxComboBox """
# Removed the return from here because SetValue doesn't return anything.
self.textControl.SetValue (value)
# Hack to call an event handler
class LocalEvent:
def __init__(self, string):
self._string=string
def GetString(self):
return self._string
if callBack==1:
# The callback wasn't being called when SetValue was used ??
# So added this explicit call to it
self.changeCallback(LocalEvent(value))
#----------------------------------------------------------------------
if __name__ == "__main__":
#from skeletonbuilder import rulesfile
class SimpleCallback:
def __init__( self, tag ):
self.tag = tag
def __call__( self, event ):
print self.tag, event.GetString()
class DemoFrame( wxFrame ):
def __init__(self, parent):
wxFrame.__init__(self, parent, 2400, "File entry with browse", size=(500,100) )
wxFrame.__init__(self, parent, 2400, "File entry with browse", size=(500,220) )
panel = wxPanel (self,-1)
innerbox = wxBoxSizer(wxVERTICAL)
control = FileBrowseButton(panel)
control = FileBrowseButton(
panel,
initialValue = "z:\\temp",
)
innerbox.Add( control, 0, wxEXPAND )
control = FileBrowseButton(panel, labelText = "With Callback", style = wxSUNKEN_BORDER ,changeCallback= self.OnFileNameChanged)
innerbox.Add( control, 0, wxEXPAND|wxALIGN_BOTTOM )
middlecontrol = FileBrowseButtonWithHistory(
panel,
labelText = "With History",
initialValue = "d:\\temp",
history = ["c:\\temp", "c:\\tmp", "r:\\temp","z:\\temp"],
changeCallback= SimpleCallback( "With History" ),
)
innerbox.Add( middlecontrol, 0, wxEXPAND )
middlecontrol = FileBrowseButtonWithHistory(
panel,
labelText = "History callback",
initialValue = "d:\\temp",
history = self.historyCallBack,
changeCallback= SimpleCallback( "History callback" ),
)
innerbox.Add( middlecontrol, 0, wxEXPAND )
self.bottomcontrol = control = FileBrowseButton(
panel,
labelText = "With Callback",
style = wxSUNKEN_BORDER|wxCLIP_CHILDREN ,
changeCallback= SimpleCallback( "With Callback" ),
)
innerbox.Add( control, 0, wxEXPAND)
ID = wxNewId()
innerbox.Add( wxButton( panel, ID,"Change Label", ), 1, wxEXPAND)
EVT_BUTTON( self, ID, self.OnChangeLabel )
ID = wxNewId()
innerbox.Add( wxButton( panel, ID,"Change Value", ), 1, wxEXPAND)
EVT_BUTTON( self, ID, self.OnChangeValue )
panel.SetAutoLayout(true)
panel.SetSizer( innerbox )
self.history={"c:\\temp":1, "c:\\tmp":1, "r:\\temp":1,"z:\\temp":1}
def OnFileNameChanged (self, event):
print "Filename changed", event.GetString ()
def historyCallBack(self):
keys=self.history.keys()
keys.sort()
return keys
def OnFileNameChangedHistory (self, event):
self.history[event.GetString ()]=1
def OnCloseMe(self, event):
self.Close(true)
def OnChangeLabel( self, event ):
self.bottomcontrol.SetLabel( "Label Updated" )
def OnChangeValue( self, event ):
self.bottomcontrol.SetValue( "r:\\somewhere\\over\\the\\rainbow.htm" )
def OnCloseWindow(self, event):
self.Destroy()
@@ -163,7 +361,7 @@ if __name__ == "__main__":
def test( ):
app = DemoApp(0)
app.MainLoop()
print 'Creating dialogue'
print 'Creating dialog'
test( )

View File

@@ -1,5 +1,5 @@
/*
* FILE : oglbasic.cpp
* FILE : ./oglbasic.cpp
*
* This file was automatically generated by :
* Simplified Wrapper and Interface Generator (SWIG)

View File

@@ -1,5 +1,5 @@
/*
* FILE : oglshapes.cpp
* FILE : ./oglshapes.cpp
*
* This file was automatically generated by :
* Simplified Wrapper and Interface Generator (SWIG)

View File

@@ -1,5 +1,5 @@
/*
* FILE : oglshapes2.cpp
* FILE : ./oglshapes2.cpp
*
* This file was automatically generated by :
* Simplified Wrapper and Interface Generator (SWIG)