2001-08-28 15:21:45 -04:00
#-----------------------------------------------------------------------------
# Name: STCStyleEditor.py
# Purpose: Style editor for the wxStyledTextCtrl
#
# Author: Riaan Booysen
#
# Created: 2001/08/20
# RCS-ID: $Id$
2006-02-09 10:41:19 -05:00
# Copyright: (c) 2001 - 2005 Riaan Booysen
# Licence: wxWidgets license
2001-08-28 15:21:45 -04:00
#-----------------------------------------------------------------------------
#Boa:Dialog:STCStyleEditDlg
""" Style editor for the wxStyledTextCtrl.
Reads in property style definitions from a config file .
Modified styled can be saved ( and optionally applied to a given list of STCs )
2002-05-03 19:32:42 -04:00
It can also maintain a Common definition dictionary of font names , colours and
2001-08-28 15:21:45 -04:00
sizes which can be shared across multiple language style definitions .
2006-02-09 10:41:19 -05:00
This is also used to store platform specific settings as fonts and sizes
2001-08-28 15:21:45 -04:00
vary with platform .
The following items are defined in the stc - styles . rc . cfg file .
common . defs . msw - Common definition dictionary used on wxMSW
common . defs . gtk - Common definition dictionary used on wxGTK
2006-02-09 10:41:19 -05:00
common . defs . mac - Common definition dictionary used on wxMAC
2001-08-28 15:21:45 -04:00
common . styleidnames - STC styles shared by all languages
Each supported language defines the following groups :
[ < language > ]
displaysrc - Example source to display in the editor
braces - Dictionary defining the ( line , column ) for showing ' good ' and ' bad '
brace matching ( both keys optional )
keywords - Space separated list of keywords
lexer - wxSTC constant for the language lexer
2006-02-09 10:41:19 -05:00
styleidnames - Dictionary of language specific style numbers and names
2001-08-28 15:21:45 -04:00
[ style . < language > ] - The users current style values
[ style . < language > . default ] - Default style values ( can be reverted from )
0 or more predefined style groups or ' themes '
[ style . < language > . < predefined name > ]
2002-05-03 19:32:42 -04:00
Currently the following languages are supported :
2001-08-28 15:21:45 -04:00
python , html , xml , cpp , text , props
Other languages can be added by just defining the above settings for them in
the config file ( if wxSTC implements them ) .
Use the initSTC function to initialise your wxSTC from a config file .
"""
import os , sys , string , pprint , copy
2006-02-09 10:41:19 -05:00
import wx
from wx . lib . anchors import LayoutAnchors
import wx . stc
import wxPython . stc # needed names from 2.4 for config files
2001-08-28 15:21:45 -04:00
settingsIdNames = { - 1 : ' Selection ' , - 2 : ' Caret ' , - 3 : ' Edge ' }
2006-02-09 10:41:19 -05:00
commonPropDefs = { ' fore ' : ' #888888 ' , ' size ' : 8 ,
' face ' : ' ' }
2001-08-28 15:21:45 -04:00
styleCategoryDescriptions = {
2006-02-09 10:41:19 -05:00
' ----Language---- ' : ' Styles specific to the language ' ,
2003-03-25 01:35:27 -05:00
' ----Standard---- ' : ' Styles shared by all languages ' ,
' ----Settings---- ' : ' Properties set by STC methods ' ,
' ----Common---- ' : ' User definable values that can be shared between languages ' }
2006-02-09 10:41:19 -05:00
platformSettings = { ' __WXMSW__ ' : ( ' msw ' , 8 ) ,
' __WXGTK__ ' : ( ' gtk ' , 10 ) ,
' __WXMAC__ ' : ( ' mac ' , 11 ) }
[ wxID_STCSTYLEEDITDLG , wxID_STCSTYLEEDITDLGADDCOMMONITEMBTN ,
wxID_STCSTYLEEDITDLGBGCOLBTN , wxID_STCSTYLEEDITDLGBGCOLCB ,
wxID_STCSTYLEEDITDLGBGCOLDEFCB , wxID_STCSTYLEEDITDLGBGCOLOKBTN ,
wxID_STCSTYLEEDITDLGCANCELBTN , wxID_STCSTYLEEDITDLGCONTEXTHELPBUTTON1 ,
wxID_STCSTYLEEDITDLGELEMENTLB , wxID_STCSTYLEEDITDLGFACECB ,
wxID_STCSTYLEEDITDLGFACEDEFCB , wxID_STCSTYLEEDITDLGFACEOKBTN ,
wxID_STCSTYLEEDITDLGFGCOLBTN , wxID_STCSTYLEEDITDLGFGCOLCB ,
wxID_STCSTYLEEDITDLGFGCOLDEFCB , wxID_STCSTYLEEDITDLGFGCOLOKBTN ,
wxID_STCSTYLEEDITDLGFIXEDWIDTHCHK , wxID_STCSTYLEEDITDLGOKBTN ,
wxID_STCSTYLEEDITDLGPANEL1 , wxID_STCSTYLEEDITDLGPANEL2 ,
wxID_STCSTYLEEDITDLGPANEL3 , wxID_STCSTYLEEDITDLGPANEL4 ,
wxID_STCSTYLEEDITDLGREMOVECOMMONITEMBTN , wxID_STCSTYLEEDITDLGSIZECB ,
wxID_STCSTYLEEDITDLGSIZEOKBTN , wxID_STCSTYLEEDITDLGSPEEDSETTINGCH ,
wxID_STCSTYLEEDITDLGSTATICBOX1 , wxID_STCSTYLEEDITDLGSTATICBOX2 ,
wxID_STCSTYLEEDITDLGSTATICLINE1 , wxID_STCSTYLEEDITDLGSTATICTEXT2 ,
wxID_STCSTYLEEDITDLGSTATICTEXT3 , wxID_STCSTYLEEDITDLGSTATICTEXT4 ,
wxID_STCSTYLEEDITDLGSTATICTEXT6 , wxID_STCSTYLEEDITDLGSTATICTEXT7 ,
wxID_STCSTYLEEDITDLGSTATICTEXT8 , wxID_STCSTYLEEDITDLGSTATICTEXT9 ,
wxID_STCSTYLEEDITDLGSTC , wxID_STCSTYLEEDITDLGSTYLEDEFST ,
wxID_STCSTYLEEDITDLGTABOLDCB , wxID_STCSTYLEEDITDLGTABOLDDEFCB ,
wxID_STCSTYLEEDITDLGTAEOLFILLEDCB , wxID_STCSTYLEEDITDLGTAEOLFILLEDDEFCB ,
wxID_STCSTYLEEDITDLGTAITALICCB , wxID_STCSTYLEEDITDLGTAITALICDEFCB ,
wxID_STCSTYLEEDITDLGTASIZEDEFCB , wxID_STCSTYLEEDITDLGTAUNDERLINEDCB ,
wxID_STCSTYLEEDITDLGTAUNDERLINEDDEFCB ,
] = [ wx . NewId ( ) for _init_ctrls in range ( 47 ) ]
class STCStyleEditDlg ( wx . Dialog ) :
2001-08-28 15:21:45 -04:00
""" Style editor for the wxStyledTextCtrl """
2006-02-09 10:41:19 -05:00
_custom_classes = { ' wx.Window ' : [ ' wx.stc.StyledTextCtrl ' ] , }
2002-05-03 19:32:42 -04:00
def _init_ctrls ( self , prnt ) :
2003-03-25 01:35:27 -05:00
# generated method, don't edit
2006-02-09 10:41:19 -05:00
wx . Dialog . __init__ ( self , id = wxID_STCSTYLEEDITDLG ,
name = ' STCStyleEditDlg ' , parent = prnt , pos = wx . Point ( 567 , 292 ) ,
size = wx . Size ( 493 , 482 ) ,
style = wx . WANTS_CHARS | wx . DEFAULT_DIALOG_STYLE | wx . RESIZE_BORDER ,
2003-03-25 01:35:27 -05:00
title = self . stc_title )
2006-02-09 10:41:19 -05:00
self . SetClientSize ( wx . Size ( 485 , 455 ) )
2003-03-25 01:35:27 -05:00
self . SetAutoLayout ( True )
2001-08-28 15:21:45 -04:00
self . SetSizeHints ( 425 , 400 , - 1 , - 1 )
2006-02-09 10:41:19 -05:00
self . Center ( wx . BOTH )
self . Bind ( wx . EVT_SIZE , self . OnStcstyleeditdlgSize )
2001-08-28 15:21:45 -04:00
2006-02-09 10:41:19 -05:00
self . speedsettingCh = wx . Choice ( choices = [ ] ,
2003-03-25 01:35:27 -05:00
id = wxID_STCSTYLEEDITDLGSPEEDSETTINGCH , name = ' speedsettingCh ' ,
2006-02-09 10:41:19 -05:00
parent = self , pos = wx . Point ( 96 , 28 ) , size = wx . Size ( 380 , 21 ) ,
style = 0 )
2003-03-25 01:35:27 -05:00
self . speedsettingCh . SetConstraints ( LayoutAnchors ( self . speedsettingCh ,
True , True , True , False ) )
self . speedsettingCh . SetHelpText ( ' The speed setting allows you to revert to one of the predefined style sets. This will overwrite your current settings when tha dialog is posted. ' )
2006-02-09 10:41:19 -05:00
self . speedsettingCh . Bind ( wx . EVT_CHOICE , self . OnSpeedsettingchChoice ,
id = wxID_STCSTYLEEDITDLGSPEEDSETTINGCH )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . elementLb = wx . ListBox ( choices = [ ] ,
id = wxID_STCSTYLEEDITDLGELEMENTLB , name = ' elementLb ' , parent = self ,
pos = wx . Point ( 8 , 70 ) , size = wx . Size ( 175 , 128 ) , style = 0 )
2003-03-25 01:35:27 -05:00
self . elementLb . SetConstraints ( LayoutAnchors ( self . elementLb , True , True ,
True , False ) )
self . elementLb . SetHelpText ( ' Select a style here to edit it. Common definitions can be added and maintained here. A common definition is a property that can be shared between styles and special cased per platform. ' )
2006-02-09 10:41:19 -05:00
self . elementLb . Bind ( wx . EVT_LISTBOX , self . OnElementlbListbox ,
id = wxID_STCSTYLEEDITDLGELEMENTLB )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . addCommonItemBtn = wx . Button ( id = wxID_STCSTYLEEDITDLGADDCOMMONITEMBTN ,
label = ' Add ' , name = ' addCommonItemBtn ' , parent = self , pos = wx . Point ( 8 ,
200 ) , size = wx . Size ( 88 , 17 ) , style = 0 )
2003-03-25 01:35:27 -05:00
self . addCommonItemBtn . SetToolTipString ( ' Add new Common definition ' )
2006-02-09 10:41:19 -05:00
self . addCommonItemBtn . Bind ( wx . EVT_BUTTON , self . OnAddsharebtnButton ,
id = wxID_STCSTYLEEDITDLGADDCOMMONITEMBTN )
2001-08-28 15:21:45 -04:00
2006-02-09 10:41:19 -05:00
self . removeCommonItemBtn = wx . Button ( id = wxID_STCSTYLEEDITDLGREMOVECOMMONITEMBTN ,
2003-03-25 01:35:27 -05:00
label = ' Remove ' , name = ' removeCommonItemBtn ' , parent = self ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 96 , 200 ) , size = wx . Size ( 88 , 17 ) , style = 0 )
2003-03-25 01:35:27 -05:00
self . removeCommonItemBtn . SetToolTipString ( ' Remove the selected Common definition ' )
2006-02-09 10:41:19 -05:00
self . removeCommonItemBtn . Bind ( wx . EVT_BUTTON ,
self . OnRemovesharebtnButton ,
id = wxID_STCSTYLEEDITDLGREMOVECOMMONITEMBTN )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . styleDefST = wx . StaticText ( id = wxID_STCSTYLEEDITDLGSTYLEDEFST ,
2003-03-25 01:35:27 -05:00
label = ' (nothing selected) ' , name = ' styleDefST ' , parent = self ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 96 , 8 ) , size = wx . Size ( 376 , 16 ) ,
style = wx . ST_NO_AUTORESIZE )
self . styleDefST . SetFont ( wx . Font ( self . style_font_size , wx . SWISS ,
wx . NORMAL , wx . BOLD , False , ' ' ) )
2003-03-25 01:35:27 -05:00
self . styleDefST . SetConstraints ( LayoutAnchors ( self . styleDefST , True ,
True , True , False ) )
2006-02-09 10:41:19 -05:00
self . staticLine1 = wx . StaticLine ( id = wxID_STCSTYLEEDITDLGSTATICLINE1 ,
name = ' staticLine1 ' , parent = self , pos = wx . Point ( 48 , 64 ) ,
size = wx . Size ( 135 , 0 ) , style = wx . LI_HORIZONTAL )
2003-03-25 01:35:27 -05:00
self . staticLine1 . SetConstraints ( LayoutAnchors ( self . staticLine1 , True ,
True , True , False ) )
2006-02-09 10:41:19 -05:00
self . staticText6 = wx . StaticText ( id = wxID_STCSTYLEEDITDLGSTATICTEXT6 ,
label = ' Style ' , name = ' staticText6 ' , parent = self , pos = wx . Point ( 8 ,
56 ) , size = wx . Size ( 40 , 13 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . staticText8 = wx . StaticText ( id = wxID_STCSTYLEEDITDLGSTATICTEXT8 ,
2003-03-25 01:35:27 -05:00
label = ' Style def: ' , name = ' staticText8 ' , parent = self ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 8 , 8 ) , size = wx . Size ( 88 , 13 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . staticText9 = wx . StaticText ( id = wxID_STCSTYLEEDITDLGSTATICTEXT9 ,
2003-03-25 01:35:27 -05:00
label = ' SpeedSetting: ' , name = ' staticText9 ' , parent = self ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 8 , 32 ) , size = wx . Size ( 88 , 13 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . panel3 = wx . Panel ( id = wxID_STCSTYLEEDITDLGPANEL3 , name = ' panel3 ' ,
parent = self , pos = wx . Point ( 199 , 56 ) , size = wx . Size ( 160 , 120 ) ,
style = wx . TAB_TRAVERSAL )
2003-03-25 01:35:27 -05:00
self . panel3 . SetConstraints ( LayoutAnchors ( self . panel3 , False , True , True ,
False ) )
2006-02-09 10:41:19 -05:00
self . panel4 = wx . Panel ( id = wxID_STCSTYLEEDITDLGPANEL4 , name = ' panel4 ' ,
parent = self , pos = wx . Point ( 364 , 56 ) , size = wx . Size ( 114 , 120 ) ,
style = wx . TAB_TRAVERSAL )
2003-03-25 01:35:27 -05:00
self . panel4 . SetConstraints ( LayoutAnchors ( self . panel4 , False , True , True ,
False ) )
2006-02-09 10:41:19 -05:00
self . panel1 = wx . Panel ( id = wxID_STCSTYLEEDITDLGPANEL1 , name = ' panel1 ' ,
parent = self , pos = wx . Point ( 202 , 177 ) , size = wx . Size ( 149 , 40 ) ,
style = wx . TAB_TRAVERSAL )
2003-03-25 01:35:27 -05:00
self . panel1 . SetConstraints ( LayoutAnchors ( self . panel1 , False , True , True ,
False ) )
2006-02-09 10:41:19 -05:00
self . panel2 = wx . Panel ( id = wxID_STCSTYLEEDITDLGPANEL2 , name = ' panel2 ' ,
parent = self , pos = wx . Point ( 364 , 178 ) , size = wx . Size ( 112 , 40 ) ,
style = wx . TAB_TRAVERSAL )
2003-03-25 01:35:27 -05:00
self . panel2 . SetConstraints ( LayoutAnchors ( self . panel2 , False , True , True ,
False ) )
2006-02-09 10:41:19 -05:00
self . stc = wx . stc . StyledTextCtrl ( id = wxID_STCSTYLEEDITDLGSTC , name = ' stc ' ,
parent = self , pos = wx . Point ( 8 , 224 ) , size = wx . Size ( 469 , 191 ) ,
style = wx . SUNKEN_BORDER )
2003-03-25 01:35:27 -05:00
self . stc . SetConstraints ( LayoutAnchors ( self . stc , True , True , True , True ) )
2006-02-09 10:41:19 -05:00
self . stc . SetHelpText ( ' The style preview window. Click or move the cursor over a specific style to select the style for editing in the editors above. ' )
self . stc . Bind ( wx . EVT_LEFT_UP , self . OnUpdateUI )
self . stc . Bind ( wx . EVT_KEY_UP , self . OnUpdateUI )
2001-08-28 15:21:45 -04:00
2006-02-09 10:41:19 -05:00
self . contextHelpButton1 = wx . ContextHelpButton ( parent = self ,
pos = wx . Point ( 8 , 423 ) , size = wx . Size ( 24 , 24 ) , style = wx . BU_AUTODRAW )
2003-03-25 01:35:27 -05:00
self . contextHelpButton1 . SetConstraints ( LayoutAnchors ( self . contextHelpButton1 ,
True , False , False , True ) )
2001-08-28 15:21:45 -04:00
2006-02-09 10:41:19 -05:00
self . okBtn = wx . Button ( id = wxID_STCSTYLEEDITDLGOKBTN , label = ' OK ' ,
name = ' okBtn ' , parent = self , pos = wx . Point ( 316 , 423 ) ,
size = wx . Size ( 75 , 23 ) , style = 0 )
2003-03-25 01:35:27 -05:00
self . okBtn . SetConstraints ( LayoutAnchors ( self . okBtn , False , False , True ,
True ) )
2001-08-28 15:21:45 -04:00
self . okBtn . SetToolTipString ( ' Save changes to the config file ' )
2006-02-09 10:41:19 -05:00
self . okBtn . Bind ( wx . EVT_BUTTON , self . OnOkbtnButton ,
id = wxID_STCSTYLEEDITDLGOKBTN )
2001-08-28 15:21:45 -04:00
2006-02-09 10:41:19 -05:00
self . cancelBtn = wx . Button ( id = wxID_STCSTYLEEDITDLGCANCELBTN ,
label = ' Cancel ' , name = ' cancelBtn ' , parent = self , pos = wx . Point ( 400 ,
423 ) , size = wx . Size ( 75 , 23 ) , style = 0 )
2003-03-25 01:35:27 -05:00
self . cancelBtn . SetConstraints ( LayoutAnchors ( self . cancelBtn , False ,
False , True , True ) )
2001-08-28 15:21:45 -04:00
self . cancelBtn . SetToolTipString ( ' Close dialog without saving changes ' )
2006-02-09 10:41:19 -05:00
self . cancelBtn . Bind ( wx . EVT_BUTTON , self . OnCancelbtnButton ,
id = wxID_STCSTYLEEDITDLGCANCELBTN )
2001-08-28 15:21:45 -04:00
2006-02-09 10:41:19 -05:00
self . staticText4 = wx . StaticText ( id = wxID_STCSTYLEEDITDLGSTATICTEXT4 ,
2003-03-25 01:35:27 -05:00
label = ' Face: ' , name = ' staticText4 ' , parent = self . panel1 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 0 , 0 ) , size = wx . Size ( 48 , 13 ) , style = 0 )
2001-08-28 15:21:45 -04:00
2006-02-09 10:41:19 -05:00
self . fixedWidthChk = wx . CheckBox ( id = wxID_STCSTYLEEDITDLGFIXEDWIDTHCHK ,
label = ' ' , name = ' fixedWidthChk ' , parent = self . panel1 ,
pos = wx . Point ( 0 , 23 ) , size = wx . Size ( 16 , 19 ) , style = 0 )
2001-08-28 15:21:45 -04:00
self . fixedWidthChk . SetToolTipString ( ' Check this for Fixed Width fonts ' )
2006-02-09 10:41:19 -05:00
self . fixedWidthChk . Bind ( wx . EVT_CHECKBOX , self . OnFixedwidthchkCheckbox ,
id = wxID_STCSTYLEEDITDLGFIXEDWIDTHCHK )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . faceCb = wx . ComboBox ( choices = [ ] , id = wxID_STCSTYLEEDITDLGFACECB ,
name = ' faceCb ' , parent = self . panel1 , pos = wx . Point ( 17 , 18 ) ,
size = wx . Size ( 101 , 21 ) , style = 0 , value = ' ' )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . staticText7 = wx . StaticText ( id = wxID_STCSTYLEEDITDLGSTATICTEXT7 ,
2003-03-25 01:35:27 -05:00
label = ' Size: ' , name = ' staticText7 ' , parent = self . panel2 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 0 , 0 ) , size = wx . Size ( 40 , 13 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . sizeCb = wx . ComboBox ( choices = [ ] , id = wxID_STCSTYLEEDITDLGSIZECB ,
name = ' sizeCb ' , parent = self . panel2 , pos = wx . Point ( 0 , 17 ) ,
size = wx . Size ( 80 , 21 ) , style = 0 , value = ' ' )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . sizeOkBtn = wx . Button ( id = wxID_STCSTYLEEDITDLGSIZEOKBTN , label = ' ok ' ,
name = ' sizeOkBtn ' , parent = self . panel2 , pos = wx . Point ( 80 , 17 ) ,
size = wx . Size ( 32 , 21 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . faceOkBtn = wx . Button ( id = wxID_STCSTYLEEDITDLGFACEOKBTN , label = ' ok ' ,
name = ' faceOkBtn ' , parent = self . panel1 , pos = wx . Point ( 117 , 18 ) ,
size = wx . Size ( 32 , 21 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . fgColBtn = wx . Button ( id = wxID_STCSTYLEEDITDLGFGCOLBTN ,
2003-03-25 01:35:27 -05:00
label = ' Foreground ' , name = ' fgColBtn ' , parent = self . panel3 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 8 , 16 ) , size = wx . Size ( 96 , 16 ) , style = 0 )
self . fgColBtn . Bind ( wx . EVT_BUTTON , self . OnFgcolbtnButton ,
id = wxID_STCSTYLEEDITDLGFGCOLBTN )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . fgColCb = wx . ComboBox ( choices = [ ] , id = wxID_STCSTYLEEDITDLGFGCOLCB ,
name = ' fgColCb ' , parent = self . panel3 , pos = wx . Point ( 8 , 32 ) ,
size = wx . Size ( 96 , 21 ) , style = 0 , value = ' ' )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . fgColOkBtn = wx . Button ( id = wxID_STCSTYLEEDITDLGFGCOLOKBTN ,
label = ' ok ' , name = ' fgColOkBtn ' , parent = self . panel3 ,
pos = wx . Point ( 104 , 32 ) , size = wx . Size ( 32 , 21 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . staticText3 = wx . StaticText ( id = wxID_STCSTYLEEDITDLGSTATICTEXT3 ,
2003-03-25 01:35:27 -05:00
label = ' default ' , name = ' staticText3 ' , parent = self . panel3 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 112 , 15 ) , size = wx . Size ( 38 , 16 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . fgColDefCb = wx . CheckBox ( id = wxID_STCSTYLEEDITDLGFGCOLDEFCB ,
2003-03-25 01:35:27 -05:00
label = ' checkBox1 ' , name = ' fgColDefCb ' , parent = self . panel3 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 136 , 31 ) , size = wx . Size ( 16 , 16 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . bgColBtn = wx . Button ( id = wxID_STCSTYLEEDITDLGBGCOLBTN ,
2003-03-25 01:35:27 -05:00
label = ' Background ' , name = ' bgColBtn ' , parent = self . panel3 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 8 , 64 ) , size = wx . Size ( 96 , 16 ) , style = 0 )
self . bgColBtn . Bind ( wx . EVT_BUTTON , self . OnBgcolbtnButton ,
id = wxID_STCSTYLEEDITDLGBGCOLBTN )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . bgColCb = wx . ComboBox ( choices = [ ] , id = wxID_STCSTYLEEDITDLGBGCOLCB ,
name = ' bgColCb ' , parent = self . panel3 , pos = wx . Point ( 8 , 80 ) ,
size = wx . Size ( 96 , 21 ) , style = 0 , value = ' ' )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . bgColOkBtn = wx . Button ( id = wxID_STCSTYLEEDITDLGBGCOLOKBTN ,
label = ' ok ' , name = ' bgColOkBtn ' , parent = self . panel3 ,
pos = wx . Point ( 104 , 80 ) , size = wx . Size ( 32 , 21 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . staticBox2 = wx . StaticBox ( id = wxID_STCSTYLEEDITDLGSTATICBOX2 ,
2003-03-25 01:35:27 -05:00
label = ' Text attributes ' , name = ' staticBox2 ' , parent = self . panel4 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 0 , 0 ) , size = wx . Size ( 112 , 112 ) , style = 0 )
2003-03-25 01:35:27 -05:00
self . staticBox2 . SetConstraints ( LayoutAnchors ( self . staticBox2 , False ,
True , True , False ) )
self . staticBox2 . SetHelpText ( ' Text attribute flags. ' )
2006-02-09 10:41:19 -05:00
self . staticText2 = wx . StaticText ( id = wxID_STCSTYLEEDITDLGSTATICTEXT2 ,
2003-03-25 01:35:27 -05:00
label = ' default ' , name = ' staticText2 ' , parent = self . panel4 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 64 , 12 ) , size = wx . Size ( 40 , 16 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . taBoldDefCb = wx . CheckBox ( id = wxID_STCSTYLEEDITDLGTABOLDDEFCB ,
2003-03-25 01:35:27 -05:00
label = ' checkBox1 ' , name = ' taBoldDefCb ' , parent = self . panel4 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 88 , 27 ) , size = wx . Size ( 16 , 16 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . taItalicDefCb = wx . CheckBox ( id = wxID_STCSTYLEEDITDLGTAITALICDEFCB ,
2003-03-25 01:35:27 -05:00
label = ' checkBox1 ' , name = ' taItalicDefCb ' , parent = self . panel4 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 88 , 48 ) , size = wx . Size ( 16 , 16 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . taUnderlinedDefCb = wx . CheckBox ( id = wxID_STCSTYLEEDITDLGTAUNDERLINEDDEFCB ,
2003-03-25 01:35:27 -05:00
label = ' checkBox1 ' , name = ' taUnderlinedDefCb ' , parent = self . panel4 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 88 , 70 ) , size = wx . Size ( 16 , 16 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . taEOLfilledDefCb = wx . CheckBox ( id = wxID_STCSTYLEEDITDLGTAEOLFILLEDDEFCB ,
2003-03-25 01:35:27 -05:00
label = ' checkBox1 ' , name = ' taEOLfilledDefCb ' , parent = self . panel4 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 88 , 92 ) , size = wx . Size ( 16 , 16 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . taEOLfilledCb = wx . CheckBox ( id = wxID_STCSTYLEEDITDLGTAEOLFILLEDCB ,
2003-03-25 01:35:27 -05:00
label = ' EOL filled ' , name = ' taEOLfilledCb ' , parent = self . panel4 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 8 , 92 ) , size = wx . Size ( 80 , 16 ) , style = 0 )
self . taEOLfilledCb . Bind ( wx . EVT_CHECKBOX , self . OnTaeoffilledcbCheckbox ,
id = wxID_STCSTYLEEDITDLGTAEOLFILLEDCB )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . taUnderlinedCb = wx . CheckBox ( id = wxID_STCSTYLEEDITDLGTAUNDERLINEDCB ,
2003-03-25 01:35:27 -05:00
label = ' Underlined ' , name = ' taUnderlinedCb ' , parent = self . panel4 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 8 , 70 ) , size = wx . Size ( 80 , 16 ) , style = 0 )
self . taUnderlinedCb . Bind ( wx . EVT_CHECKBOX , self . OnTaunderlinedcbCheckbox ,
id = wxID_STCSTYLEEDITDLGTAUNDERLINEDCB )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . taItalicCb = wx . CheckBox ( id = wxID_STCSTYLEEDITDLGTAITALICCB ,
2003-03-25 01:35:27 -05:00
label = ' Italic ' , name = ' taItalicCb ' , parent = self . panel4 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 8 , 48 ) , size = wx . Size ( 80 , 16 ) , style = 0 )
self . taItalicCb . Bind ( wx . EVT_CHECKBOX , self . OnTaitaliccbCheckbox ,
id = wxID_STCSTYLEEDITDLGTAITALICCB )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . taBoldCb = wx . CheckBox ( id = wxID_STCSTYLEEDITDLGTABOLDCB ,
label = ' Bold ' , name = ' taBoldCb ' , parent = self . panel4 , pos = wx . Point ( 8 ,
27 ) , size = wx . Size ( 80 , 16 ) , style = 0 )
self . taBoldCb . Bind ( wx . EVT_CHECKBOX , self . OnTaboldcbCheckbox ,
id = wxID_STCSTYLEEDITDLGTABOLDCB )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . bgColDefCb = wx . CheckBox ( id = wxID_STCSTYLEEDITDLGBGCOLDEFCB ,
2003-03-25 01:35:27 -05:00
label = ' checkBox1 ' , name = ' bgColDefCb ' , parent = self . panel3 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 136 , 79 ) , size = wx . Size ( 16 , 16 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . staticBox1 = wx . StaticBox ( id = wxID_STCSTYLEEDITDLGSTATICBOX1 ,
2003-03-25 01:35:27 -05:00
label = ' Colour ' , name = ' staticBox1 ' , parent = self . panel3 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 0 , 0 ) , size = wx . Size ( 157 , 112 ) , style = 0 )
2003-03-25 01:35:27 -05:00
self . staticBox1 . SetConstraints ( LayoutAnchors ( self . staticBox1 , False ,
True , True , False ) )
2006-02-09 10:41:19 -05:00
self . faceDefCb = wx . CheckBox ( id = wxID_STCSTYLEEDITDLGFACEDEFCB ,
2003-03-25 01:35:27 -05:00
label = ' checkBox1 ' , name = ' faceDefCb ' , parent = self . panel1 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 117 , 0 ) , size = wx . Size ( 16 , 16 ) , style = 0 )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
self . taSizeDefCb = wx . CheckBox ( id = wxID_STCSTYLEEDITDLGTASIZEDEFCB ,
2003-03-25 01:35:27 -05:00
label = ' checkBox1 ' , name = ' taSizeDefCb ' , parent = self . panel2 ,
2006-02-09 10:41:19 -05:00
pos = wx . Point ( 80 , 0 ) , size = wx . Size ( 16 , 16 ) , style = 0 )
2001-08-28 15:21:45 -04:00
2002-05-03 19:32:42 -04:00
def __init__ ( self , parent , langTitle , lang , configFile , STCsToUpdate = ( ) ) :
2001-08-28 15:21:45 -04:00
self . stc_title = ' wxStyledTextCtrl Style Editor '
self . stc_title = ' wxStyledTextCtrl Style Editor - %s ' % langTitle
2006-02-09 10:41:19 -05:00
self . style_font_size = 8
self . style_font_size = platformSettings [ wx . Platform ] [ 1 ]
2001-08-28 15:21:45 -04:00
self . _init_ctrls ( parent )
self . lang = lang
self . configFile = configFile
self . style = ' '
2003-03-25 01:35:27 -05:00
self . styleNum = 0
2001-08-28 15:21:45 -04:00
self . names = [ ]
self . values = { }
self . STCsToUpdate = STCsToUpdate
2003-03-25 01:35:27 -05:00
self . _blockUpdate = False
2006-02-09 10:41:19 -05:00
global commonPropDefs
commonPropDefs = { ' fore ' : ' #888888 ' , ' size ' : 8 ,
' face ' : wx . SystemSettings . GetFont ( wx . SYS_DEFAULT_GUI_FONT ) . GetFaceName ( ) }
2003-03-25 01:35:27 -05:00
for combo , okBtn , evtRet , evtCB , evtRDC in (
( self . fgColCb , self . fgColOkBtn , self . OnfgColRet , self . OnfgColCombobox , self . OnGotoCommonDef ) ,
( self . bgColCb , self . bgColOkBtn , self . OnbgColRet , self . OnbgColCombobox , self . OnGotoCommonDef ) ,
( self . faceCb , self . faceOkBtn , self . OnfaceRet , self . OnfaceCombobox , self . OnGotoCommonDef ) ,
( self . sizeCb , self . sizeOkBtn , self . OnsizeRet , self . OnsizeCombobox , self . OnGotoCommonDef ) ) :
self . bindComboEvts ( combo , okBtn , evtRet , evtCB , evtRDC )
2002-05-03 19:32:42 -04:00
( self . config , self . commonDefs , self . styleIdNames , self . styles ,
self . styleGroupNames , self . predefStyleGroups ,
2001-08-28 15:21:45 -04:00
self . otherLangStyleGroupNames , self . otherLangStyleGroups ,
self . displaySrc , self . lexer , self . keywords , self . braceInfo ) = \
2002-05-03 19:32:42 -04:00
initFromConfig ( configFile , lang )
2001-08-28 15:21:45 -04:00
self . currSpeedSetting = ' style. %s ' % self . lang
for grp in [ self . currSpeedSetting ] + self . styleGroupNames :
self . speedsettingCh . Append ( grp )
self . speedsettingCh . SetSelection ( 0 )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
margin = 0
2006-02-09 10:41:19 -05:00
self . stc . SetMarginType ( margin , wx . stc . STC_MARGIN_NUMBER )
2001-08-28 15:21:45 -04:00
self . stc . SetMarginWidth ( margin , 25 )
2003-03-25 01:35:27 -05:00
self . stc . SetMarginSensitive ( margin , True )
2006-02-09 10:41:19 -05:00
self . stc . Bind ( wx . stc . EVT_STC_MARGINCLICK , self . OnMarginClick , id = wxID_STCSTYLEEDITDLGSTC )
2001-08-28 15:21:45 -04:00
2003-03-25 01:35:27 -05:00
self . stc . SetUseTabs ( False )
2001-08-28 15:21:45 -04:00
self . stc . SetTabWidth ( 4 )
2003-03-25 01:35:27 -05:00
self . stc . SetIndentationGuides ( True )
2006-02-09 10:41:19 -05:00
self . stc . SetEdgeMode ( wx . stc . STC_EDGE_BACKGROUND )
2001-08-28 15:21:45 -04:00
self . stc . SetEdgeColumn ( 44 )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
self . setStyles ( )
self . populateStyleSelector ( )
self . defNames , self . defValues = parseProp ( \
2006-02-09 10:41:19 -05:00
self . styleDict . get ( wx . stc . STC_STYLE_DEFAULT , ' ' ) )
2001-08-28 15:21:45 -04:00
self . stc . SetText ( self . displaySrc )
self . stc . EmptyUndoBuffer ( )
self . stc . SetCurrentPos ( self . stc . GetTextLength ( ) )
self . stc . SetAnchor ( self . stc . GetTextLength ( ) )
self . populateCombosWithCommonDefs ( )
# Logical grouping of controls and the property they edit
2003-03-25 01:35:27 -05:00
self . allCtrls = [ ( ( self . fgColBtn , self . fgColCb , self . fgColOkBtn ) , self . fgColDefCb ,
2001-08-28 15:21:45 -04:00
' fore ' , wxID_STCSTYLEEDITDLGFGCOLDEFCB ) ,
2003-03-25 01:35:27 -05:00
( ( self . bgColBtn , self . bgColCb , self . bgColOkBtn ) , self . bgColDefCb ,
2001-08-28 15:21:45 -04:00
' back ' , wxID_STCSTYLEEDITDLGBGCOLDEFCB ) ,
2002-05-03 19:32:42 -04:00
( self . taBoldCb , self . taBoldDefCb ,
2001-08-28 15:21:45 -04:00
' bold ' , wxID_STCSTYLEEDITDLGTABOLDDEFCB ) ,
2002-05-03 19:32:42 -04:00
( self . taItalicCb , self . taItalicDefCb ,
' italic ' , wxID_STCSTYLEEDITDLGTAITALICDEFCB ) ,
( self . taUnderlinedCb , self . taUnderlinedDefCb ,
2001-08-28 15:21:45 -04:00
' underline ' , wxID_STCSTYLEEDITDLGTAUNDERLINEDDEFCB ) ,
2002-05-03 19:32:42 -04:00
( self . taEOLfilledCb , self . taEOLfilledDefCb ,
2001-08-28 15:21:45 -04:00
' eolfilled ' , wxID_STCSTYLEEDITDLGTAEOLFILLEDDEFCB ) ,
2003-03-25 01:35:27 -05:00
( ( self . sizeCb , self . sizeOkBtn ) , self . taSizeDefCb ,
2001-08-28 15:21:45 -04:00
' size ' , wxID_STCSTYLEEDITDLGTASIZEDEFCB ) ,
2003-03-25 01:35:27 -05:00
( ( self . faceCb , self . faceOkBtn , self . fixedWidthChk ) , self . faceDefCb ,
2001-08-28 15:21:45 -04:00
' face ' , wxID_STCSTYLEEDITDLGFACEDEFCB ) ]
2003-03-25 01:35:27 -05:00
self . clearCtrls ( disableDefs = True )
2001-08-28 15:21:45 -04:00
# centralised default checkbox event handler
self . chbIdMap = { }
for ctrl , chb , prop , wid in self . allCtrls :
self . chbIdMap [ wid ] = ctrl , chb , prop , wid
2006-02-09 10:41:19 -05:00
chb . Bind ( wx . EVT_CHECKBOX , self . OnDefaultCheckBox , id = wid )
2001-08-28 15:21:45 -04:00
chb . SetToolTipString ( ' Toggle defaults ' )
2002-05-03 19:32:42 -04:00
2006-02-09 10:41:19 -05:00
self . Center ( wx . BOTH )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
#---Property methods------------------------------------------------------------
def getCtrlForProp ( self , findprop ) :
for ctrl , chb , prop , wid in self . allCtrls :
if findprop == prop :
return ctrl , chb
raise Exception ( ' PropNotFound ' , findprop )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
def editProp ( self , on , prop , val = ' ' ) :
oldstyle = self . rememberStyles ( )
if on :
if not self . names . count ( prop ) :
self . names . append ( prop )
self . values [ prop ] = val
else :
try : self . names . remove ( prop )
except ValueError : pass
try : del self . values [ prop ]
except KeyError : pass
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
try :
self . updateStyle ( )
2003-03-25 01:35:27 -05:00
return True
2001-08-28 15:21:45 -04:00
except KeyError , errkey :
2006-02-09 10:41:19 -05:00
wx . LogError ( ' Name not found in Common definition, ' \
2001-08-28 15:21:45 -04:00
' please enter valid reference. ( %s ) ' % errkey )
self . restoreStyles ( oldstyle )
2003-03-25 01:35:27 -05:00
return False
2001-08-28 15:21:45 -04:00
#---Control population methods--------------------------------------------------
def setStyles ( self ) :
2003-03-25 01:35:27 -05:00
if self . _blockUpdate : return
2001-08-28 15:21:45 -04:00
self . styles , self . styleDict , self . styleNumIdxMap = \
2002-05-03 19:32:42 -04:00
setSTCStyles ( self . stc , self . styles , self . styleIdNames ,
2001-08-28 15:21:45 -04:00
self . commonDefs , self . lang , self . lexer , self . keywords )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
def updateStyle ( self ) :
# called after a control edited self.names, self.values
# Special case for saving common defs settings
if self . styleNum == ' common ' :
2006-02-09 10:41:19 -05:00
#if not self.values:
# return
2001-08-28 15:21:45 -04:00
strVal = self . style [ 2 ] = self . values . values ( ) [ 0 ]
if self . style [ 1 ] == ' size ' : self . style [ 2 ] = int ( strVal )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
self . commonDefs [ self . style [ 0 ] ] = self . style [ 2 ]
self . styleDefST . SetLabel ( strVal )
else :
self . style = writePropVal ( self . names , self . values )
styleDecl = writeProp ( self . styleNum , self . style , self . lang )
self . styles [ self . styleNumIdxMap [ self . styleNum ] ] = styleDecl
self . styleDefST . SetLabel ( self . style )
self . setStyles ( )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
def findInStyles ( self , txt , styles ) :
for style in styles :
2006-02-09 10:41:19 -05:00
if style . find ( txt ) != - 1 :
2003-03-25 01:35:27 -05:00
return True
return False
2001-08-28 15:21:45 -04:00
def rememberStyles ( self ) :
return self . names [ : ] , copy . copy ( self . values )
def restoreStyles ( self , style ) :
self . names , self . values = style
self . updateStyle ( )
2003-03-25 01:35:27 -05:00
def clearCtrls ( self , isDefault = False , disableDefs = False ) :
self . _blockUpdate = True
try :
for ctrl , chb , prop , wid in self . allCtrls :
if prop in ( ' fore ' , ' back ' ) :
cbtn , txt , btn = ctrl
cbtn . SetBackgroundColour ( \
2006-02-09 10:41:19 -05:00
wx . SystemSettings . GetColour ( wx . SYS_COLOUR_BTNFACE ) )
cbtn . SetForegroundColour ( wx . Colour ( 255 , 255 , 255 ) )
2003-03-25 01:35:27 -05:00
cbtn . Enable ( isDefault )
txt . SetValue ( ' ' )
txt . Enable ( isDefault )
btn . Enable ( isDefault )
elif prop == ' size ' :
cmb , btn = ctrl
cmb . SetValue ( ' ' )
cmb . Enable ( isDefault )
btn . Enable ( isDefault )
elif prop == ' face ' :
cmb , btn , chk = ctrl
cmb . SetValue ( ' ' )
cmb . Enable ( isDefault )
btn . Enable ( isDefault )
chk . Enable ( isDefault )
chk . SetValue ( False )
elif prop in ( ' bold ' , ' italic ' , ' underline ' , ' eolfilled ' ) :
ctrl . SetValue ( False )
ctrl . Enable ( isDefault )
chb . Enable ( not isDefault and not disableDefs )
chb . SetValue ( True )
finally :
self . _blockUpdate = False
def populateProp ( self , items , default , forceDisable = False ) :
self . _blockUpdate = True
try :
for name , val in items :
if name :
ctrl , chb = self . getCtrlForProp ( name )
if name in ( ' fore ' , ' back ' ) :
cbtn , txt , btn = ctrl
repval = val % self . commonDefs
cbtn . SetBackgroundColour ( strToCol ( repval ) )
2006-02-09 10:41:19 -05:00
cbtn . SetForegroundColour ( wx . Colour ( 0 , 0 , 0 ) )
2003-03-25 01:35:27 -05:00
cbtn . Enable ( not forceDisable )
txt . SetValue ( val )
txt . Enable ( not forceDisable )
btn . Enable ( not forceDisable )
chb . SetValue ( default )
elif name == ' size ' :
cmb , btn = ctrl
cmb . SetValue ( val )
cmb . Enable ( not forceDisable )
btn . Enable ( not forceDisable )
chb . SetValue ( default )
elif name == ' face ' :
cmb , btn , chk = ctrl
cmb . SetValue ( val )
cmb . Enable ( not forceDisable )
btn . Enable ( not forceDisable )
chk . Enable ( not forceDisable )
chb . SetValue ( default )
elif name in ( ' bold ' , ' italic ' , ' underline ' , ' eolfilled ' ) :
ctrl . Enable ( not forceDisable )
ctrl . SetValue ( True )
chb . SetValue ( default )
finally :
self . _blockUpdate = False
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
def valIsCommonDef ( self , val ) :
return len ( val ) > = 5 and val [ : 2 ] == ' % ( '
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
def populateCtrls ( self ) :
2006-02-09 10:41:19 -05:00
self . clearCtrls ( self . styleNum == wx . stc . STC_STYLE_DEFAULT ,
2001-08-28 15:21:45 -04:00
disableDefs = self . styleNum < 0 )
# handle colour controls for settings
if self . styleNum < 0 :
2003-03-25 01:35:27 -05:00
self . fgColDefCb . Enable ( True )
2001-08-28 15:21:45 -04:00
if self . styleNum == - 1 :
2003-03-25 01:35:27 -05:00
self . bgColDefCb . Enable ( True )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
# populate with default style
2003-03-25 01:35:27 -05:00
self . populateProp ( self . defValues . items ( ) , True ,
2006-02-09 10:41:19 -05:00
self . styleNum != wx . stc . STC_STYLE_DEFAULT )
2001-08-28 15:21:45 -04:00
# override with current settings
2003-03-25 01:35:27 -05:00
self . populateProp ( self . values . items ( ) , False )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
def getCommonDefPropType ( self , commonDefName ) :
val = self . commonDefs [ commonDefName ]
if type ( val ) == type ( 0 ) : return ' size '
if len ( val ) == 7 and val [ 0 ] == ' # ' : return ' fore '
return ' face '
2003-03-25 01:35:27 -05:00
def bindComboEvts ( self , combo , btn , btnEvtMeth , comboEvtMeth , rdclickEvtMeth ) :
2006-02-09 10:41:19 -05:00
combo . Bind ( wx . EVT_COMBOBOX , comboEvtMeth , id = combo . GetId ( ) )
btn . Bind ( wx . EVT_BUTTON , btnEvtMeth , id = btn . GetId ( ) )
combo . Bind ( wx . EVT_RIGHT_DCLICK , rdclickEvtMeth )
2003-03-25 01:35:27 -05:00
combo . SetToolTipString ( ' Select from list or click " ok " button on the right to change a manual entry, right double-click \n ' \
' the drop down button to select Common definition in the Style Editor (if applicable) ' )
btn . SetToolTipString ( ' Accept value ' )
2001-08-28 15:21:45 -04:00
def populateCombosWithCommonDefs ( self , fixedWidthOnly = None ) :
2003-03-25 01:35:27 -05:00
self . _blockUpdate = True
try :
commonDefs = { ' fore ' : [ ] , ' face ' : [ ] , ' size ' : [ ] }
if self . elementLb . GetSelection ( ) < self . commonDefsStartIdx :
for common in self . commonDefs . keys ( ) :
prop = self . getCommonDefPropType ( common )
commonDefs [ prop ] . append ( ' %% ( %s ) %s ' % ( common ,
prop == ' size ' and ' d ' or ' s ' ) )
# Colours
currFg , currBg = self . fgColCb . GetValue ( ) , self . bgColCb . GetValue ( )
self . fgColCb . Clear ( ) ; self . bgColCb . Clear ( )
for colCommonDef in commonDefs [ ' fore ' ] :
self . fgColCb . Append ( colCommonDef )
self . bgColCb . Append ( colCommonDef )
self . fgColCb . SetValue ( currFg ) ; self . bgColCb . SetValue ( currBg )
# Font
if fixedWidthOnly is None :
fixedWidthOnly = self . fixedWidthChk . GetValue ( )
2006-02-09 10:41:19 -05:00
fontEnum = wx . FontEnumerator ( )
2003-03-25 01:35:27 -05:00
fontEnum . EnumerateFacenames ( fixedWidthOnly = fixedWidthOnly )
fontNameList = fontEnum . GetFacenames ( )
2006-02-09 10:41:19 -05:00
fontNameList . sort ( )
2003-03-25 01:35:27 -05:00
currFace = self . faceCb . GetValue ( )
self . faceCb . Clear ( )
2006-02-09 10:41:19 -05:00
for colCommonDef in fontNameList + commonDefs [ ' face ' ] :
2003-03-25 01:35:27 -05:00
self . faceCb . Append ( colCommonDef )
self . faceCb . SetValue ( currFace )
# Size (XXX add std font sizes)
currSize = self . sizeCb . GetValue ( )
self . sizeCb . Clear ( )
for colCommonDef in commonDefs [ ' size ' ] :
self . sizeCb . Append ( colCommonDef )
self . sizeCb . SetValue ( currSize )
finally :
self . _blockUpdate = False
2001-08-28 15:21:45 -04:00
def populateStyleSelector ( self ) :
numStyles = self . styleIdNames . items ( )
numStyles . sort ( )
self . styleNumLookup = { }
stdStart = - 1
stdOffset = 0
extrOffset = 0
# add styles
for num , name in numStyles :
2006-02-09 10:41:19 -05:00
if num == wx . stc . STC_STYLE_DEFAULT :
2003-03-25 01:35:27 -05:00
self . elementLb . InsertItems ( [ name , ' ----Language---- ' ] , 0 )
self . elementLb . Append ( ' ----Standard---- ' )
2004-03-02 18:51:23 -05:00
stdStart = stdPos = self . elementLb . GetCount ( )
2001-08-28 15:21:45 -04:00
else :
# std styles
if num > = 33 and num < 40 :
self . elementLb . InsertItems ( [ name ] , stdStart + stdOffset )
stdOffset = stdOffset + 1
# extra styles
2002-05-03 19:32:42 -04:00
elif num > = 40 :
2001-08-28 15:21:45 -04:00
self . elementLb . InsertItems ( [ name ] , stdStart + extrOffset - 1 )
extrOffset = extrOffset + 1
# normal lang styles
else :
self . elementLb . Append ( name )
self . styleNumLookup [ name ] = num
# add settings
2003-03-25 01:35:27 -05:00
self . elementLb . Append ( ' ----Settings---- ' )
2001-08-28 15:21:45 -04:00
settings = settingsIdNames . items ( )
settings . sort ( ) ; settings . reverse ( )
for num , name in settings :
self . elementLb . Append ( name )
self . styleNumLookup [ name ] = num
# add definitions
2003-03-25 01:35:27 -05:00
self . elementLb . Append ( ' ----Common---- ' )
2004-03-02 18:51:23 -05:00
self . commonDefsStartIdx = self . elementLb . GetCount ( )
2001-08-28 15:21:45 -04:00
for common in self . commonDefs . keys ( ) :
tpe = type ( self . commonDefs [ common ] )
self . elementLb . Append ( ' % ( ' + common + ' ) ' + ( tpe is type ( ' ' ) and ' s ' or ' d ' ) )
self . styleNumLookup [ common ] = num
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
#---Colour methods--------------------------------------------------------------
def getColourDlg ( self , colour , title = ' ' ) :
2006-02-09 10:41:19 -05:00
data = wx . ColourData ( )
2001-08-28 15:21:45 -04:00
data . SetColour ( colour )
2003-03-25 01:35:27 -05:00
data . SetChooseFull ( True )
2006-02-09 10:41:19 -05:00
dlg = wx . ColourDialog ( self , data )
2001-08-28 15:21:45 -04:00
try :
dlg . SetTitle ( title )
2006-02-09 10:41:19 -05:00
if dlg . ShowModal ( ) == wx . ID_OK :
2001-08-28 15:21:45 -04:00
data = dlg . GetColourData ( )
return data . GetColour ( )
finally :
dlg . Destroy ( )
return None
colDlgTitles = { ' fore ' : ' Foreground ' , ' back ' : ' Background ' }
def editColProp ( self , colBtn , colCb , prop ) :
2002-05-03 19:32:42 -04:00
col = self . getColourDlg ( colBtn . GetBackgroundColour ( ) ,
2001-08-28 15:21:45 -04:00
self . colDlgTitles [ prop ] + ' colour ' )
2002-05-03 19:32:42 -04:00
if col :
2006-02-09 10:41:19 -05:00
colBtn . SetForegroundColour ( wx . Colour ( 0 , 0 , 0 ) )
2001-08-28 15:21:45 -04:00
colBtn . SetBackgroundColour ( col )
colStr = colToStr ( col )
colCb . SetValue ( colStr )
2003-03-25 01:35:27 -05:00
self . editProp ( True , prop , colStr )
2001-08-28 15:21:45 -04:00
def OnFgcolbtnButton ( self , event ) :
self . editColProp ( self . fgColBtn , self . fgColCb , ' fore ' )
def OnBgcolbtnButton ( self , event ) :
self . editColProp ( self . bgColBtn , self . bgColCb , ' back ' )
2003-03-25 01:35:27 -05:00
def editColTCProp ( self , colCb , colBtn , prop , val = None ) :
if val is None :
colStr = colCb . GetValue ( )
else :
colStr = val
2002-05-03 19:32:42 -04:00
if colStr :
2001-08-28 15:21:45 -04:00
col = strToCol ( colStr % self . commonDefs )
if self . editProp ( colStr != ' ' , prop , colStr ) :
2002-05-03 19:32:42 -04:00
if colStr :
2006-02-09 10:41:19 -05:00
colBtn . SetForegroundColour ( wx . Colour ( 0 , 0 , 0 ) )
2001-08-28 15:21:45 -04:00
colBtn . SetBackgroundColour ( col )
else :
2006-02-09 10:41:19 -05:00
colBtn . SetForegroundColour ( wx . Colour ( 255 , 255 , 255 ) )
2001-08-28 15:21:45 -04:00
colBtn . SetBackgroundColour ( \
2006-02-09 10:41:19 -05:00
wxSystemSettings . GetColour ( wx . SYS_COLOUR_BTNFACE ) )
2001-08-28 15:21:45 -04:00
def OnfgColRet ( self , event ) :
try : self . editColTCProp ( self . fgColCb , self . fgColBtn , ' fore ' )
2006-02-09 10:41:19 -05:00
except AssertionError : wx . LogError ( ' Not a valid colour value ' )
2001-08-28 15:21:45 -04:00
2003-03-25 01:35:27 -05:00
def OnfgColCombobox ( self , event ) :
if self . _blockUpdate : return
try : self . editColTCProp ( self . fgColCb , self . fgColBtn , ' fore ' , event . GetString ( ) )
2006-02-09 10:41:19 -05:00
except AssertionError : wx . LogError ( ' Not a valid colour value ' )
2003-03-25 01:35:27 -05:00
2001-08-28 15:21:45 -04:00
def OnbgColRet ( self , event ) :
try : self . editColTCProp ( self . bgColCb , self . bgColBtn , ' back ' )
2006-02-09 10:41:19 -05:00
except AssertionError : wx . LogError ( ' Not a valid colour value ' )
2001-08-28 15:21:45 -04:00
2003-03-25 01:35:27 -05:00
def OnbgColCombobox ( self , event ) :
if self . _blockUpdate : return
try : self . editColTCProp ( self . bgColCb , self . bgColBtn , ' back ' , event . GetString ( ) )
2006-02-09 10:41:19 -05:00
except AssertionError : wx . LogError ( ' Not a valid colour value ' )
2003-03-25 01:35:27 -05:00
2001-08-28 15:21:45 -04:00
#---Text attribute events-------------------------------------------------------
def OnTaeoffilledcbCheckbox ( self , event ) :
self . editProp ( event . IsChecked ( ) , ' eolfilled ' )
def OnTaitaliccbCheckbox ( self , event ) :
self . editProp ( event . IsChecked ( ) , ' italic ' )
def OnTaboldcbCheckbox ( self , event ) :
self . editProp ( event . IsChecked ( ) , ' bold ' )
def OnTaunderlinedcbCheckbox ( self , event ) :
self . editProp ( event . IsChecked ( ) , ' underline ' )
def OnGotoCommonDef ( self , event ) :
val = event . GetEventObject ( ) . GetValue ( )
if self . valIsCommonDef ( val ) :
idx = self . elementLb . FindString ( val )
if idx != - 1 :
2003-03-25 01:35:27 -05:00
self . elementLb . SetSelection ( idx , True )
2001-08-28 15:21:45 -04:00
self . OnElementlbListbox ( None )
def OnfaceRet ( self , event ) :
2003-03-25 01:35:27 -05:00
self . setFace ( self . faceCb . GetValue ( ) )
def OnfaceCombobox ( self , event ) :
if self . _blockUpdate : return
self . setFace ( event . GetString ( ) )
def setFace ( self , val ) :
2001-08-28 15:21:45 -04:00
try : val % self . commonDefs
2006-02-09 10:41:19 -05:00
except KeyError : wx . LogError ( ' Invalid common definition ' )
2001-08-28 15:21:45 -04:00
else : self . editProp ( val != ' ' , ' face ' , val )
def OnsizeRet ( self , event ) :
2003-03-25 01:35:27 -05:00
self . setSize ( self . sizeCb . GetValue ( ) )
def OnsizeCombobox ( self , event ) :
if self . _blockUpdate : return
self . setSize ( event . GetString ( ) )
def setSize ( self , val ) :
2001-08-28 15:21:45 -04:00
try : int ( val % self . commonDefs )
2006-02-09 10:41:19 -05:00
except ValueError : wx . LogError ( ' Not a valid integer size value ' )
except KeyError : wx . LogError ( ' Invalid common definition ' )
2001-08-28 15:21:45 -04:00
else : self . editProp ( val != ' ' , ' size ' , val )
#---Main GUI events-------------------------------------------------------------
def OnElementlbListbox ( self , event ) :
isCommon = self . elementLb . GetSelection ( ) > = self . commonDefsStartIdx
self . removeCommonItemBtn . Enable ( isCommon )
styleIdent = self . elementLb . GetStringSelection ( )
# common definition selected
if isCommon :
common = styleIdent [ 2 : - 2 ]
prop = self . getCommonDefPropType ( common )
2003-03-25 01:35:27 -05:00
self . clearCtrls ( disableDefs = True )
2001-08-28 15:21:45 -04:00
if prop == ' fore ' :
2003-03-25 01:35:27 -05:00
self . fgColBtn . Enable ( True )
self . fgColCb . Enable ( True )
self . fgColOkBtn . Enable ( True )
2001-08-28 15:21:45 -04:00
elif prop == ' face ' :
2003-03-25 01:35:27 -05:00
self . faceCb . Enable ( True )
self . fixedWidthChk . Enable ( True )
self . faceOkBtn . Enable ( True )
2001-08-28 15:21:45 -04:00
elif prop == ' size ' :
2003-03-25 01:35:27 -05:00
self . sizeCb . Enable ( True )
self . sizeOkBtn . Enable ( True )
2001-08-28 15:21:45 -04:00
commonDefVal = str ( self . commonDefs [ common ] )
self . styleDefST . SetLabel ( commonDefVal )
2003-03-25 01:35:27 -05:00
self . populateProp ( [ ( prop , commonDefVal ) ] , True )
2001-08-28 15:21:45 -04:00
self . styleNum = ' common '
self . style = [ common , prop , commonDefVal ]
self . names , self . values = [ prop ] , { prop : commonDefVal }
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
# normal style element selected
elif len ( styleIdent ) > = 2 and styleIdent [ : 2 ] != ' -- ' :
self . styleNum = self . styleNumLookup [ styleIdent ]
self . style = self . styleDict [ self . styleNum ]
self . names , self . values = parseProp ( self . style )
2006-02-09 10:41:19 -05:00
if self . styleNum == wx . stc . STC_STYLE_DEFAULT :
2001-08-28 15:21:45 -04:00
self . defNames , self . defValues = \
self . names , self . values
self . checkBraces ( self . styleNum )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
self . styleDefST . SetLabel ( self . style )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
self . populateCtrls ( )
# separator selected
else :
2003-03-25 01:35:27 -05:00
self . clearCtrls ( disableDefs = True )
2001-08-28 15:21:45 -04:00
if styleIdent :
self . styleDefST . SetLabel ( styleCategoryDescriptions [ styleIdent ] )
self . populateCombosWithCommonDefs ( )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
def OnDefaultCheckBox ( self , event ) :
2003-03-25 01:35:27 -05:00
if self . chbIdMap . has_key ( event . GetId ( ) ) :
ctrl , chb , prop , wid = self . chbIdMap [ event . GetId ( ) ]
restore = not event . IsChecked ( )
if prop in ( ' fore ' , ' back ' ) :
cbtn , cmb , btn = ctrl
cbtn . Enable ( restore )
cmb . Enable ( restore )
btn . Enable ( restore )
if restore :
colStr = cmb . GetValue ( )
#if prop == 'fore': colStr = self.fgColCb.GetValue()
#else: colStr = self.bgColCb.GetValue()
if colStr : self . editProp ( True , prop , colStr )
else :
self . editProp ( False , prop )
elif prop == ' size ' :
cmb , btn = ctrl
val = cmb . GetValue ( )
if val : self . editProp ( restore , prop , val )
cmb . Enable ( restore )
btn . Enable ( restore )
elif prop == ' face ' :
cmb , btn , chk = ctrl
val = cmb . GetStringSelection ( )
if val : self . editProp ( restore , prop , val )
cmb . Enable ( restore )
btn . Enable ( restore )
chk . Enable ( restore )
elif prop in ( ' bold ' , ' italic ' , ' underline ' , ' eolfilled ' ) :
ctrl . Enable ( restore )
if ctrl . GetValue ( ) : self . editProp ( restore , prop )
2001-08-28 15:21:45 -04:00
def OnOkbtnButton ( self , event ) :
# write styles and common defs to the config
2006-02-09 10:41:19 -05:00
wx . BeginBusyCursor ( )
2003-03-25 01:35:27 -05:00
try :
writeStylesToConfig ( self . config , ' style. %s ' % self . lang , self . styles )
self . config . SetPath ( ' ' )
self . config . Write ( commonDefsFile , ` self . commonDefs ` )
self . config . Flush ( )
for stc in self . STCsToUpdate :
setSTCStyles ( stc , self . styles , self . styleIdNames , self . commonDefs ,
self . lang , self . lexer , self . keywords )
finally :
2006-02-09 10:41:19 -05:00
wx . EndBusyCursor ( )
self . EndModal ( wx . ID_OK )
2001-08-28 15:21:45 -04:00
def OnCancelbtnButton ( self , event ) :
2006-02-09 10:41:19 -05:00
self . EndModal ( wx . ID_CANCEL )
2001-08-28 15:21:45 -04:00
def OnCommondefsbtnButton ( self , event ) :
2006-02-09 10:41:19 -05:00
dlg = wx . TextEntryDialog ( self , ' Edit common definitions dictionary ' ,
2001-08-28 15:21:45 -04:00
' Common definitions ' , pprint . pformat ( self . commonDefs ) ,
2006-02-09 10:41:19 -05:00
style = wx . TE_MULTILINE | wx . OK | wx . CANCEL | wx . CENTRE )
2001-08-28 15:21:45 -04:00
try :
2006-02-09 10:41:19 -05:00
if dlg . ShowModal ( ) == wx . ID_OK :
answer = eval ( dlg . GetValue ( ) , stc . __dict__ )
2001-08-28 15:21:45 -04:00
assert type ( answer ) is type ( { } ) , ' Not a valid dictionary '
oldDefs = self . commonDefs
self . commonDefs = answer
try :
self . setStyles ( )
except KeyError , badkey :
2006-02-09 10:41:19 -05:00
wx . LogError ( str ( badkey ) + ' not defined but required, \n ' \
2001-08-28 15:21:45 -04:00
' reverting to previous common definition ' )
self . commonDefs = oldDefs
self . setStyles ( )
self . populateCombosWithCommonDefs ( )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
finally :
dlg . Destroy ( )
def OnSpeedsettingchChoice ( self , event ) :
group = event . GetString ( )
if group :
userStyles = ' style. %s ' % self . lang
if self . currSpeedSetting == userStyles :
self . predefStyleGroups [ userStyles ] = self . styles
self . styles = self . predefStyleGroups [ group ]
self . setStyles ( )
self . defNames , self . defValues = parseProp ( \
2006-02-09 10:41:19 -05:00
self . styleDict . get ( wx . stc . STC_STYLE_DEFAULT , ' ' ) )
2001-08-28 15:21:45 -04:00
self . OnElementlbListbox ( None )
self . currSpeedSetting = group
def OnFixedwidthchkCheckbox ( self , event ) :
2006-02-09 10:41:19 -05:00
self . populateCombosWithCommonDefs ( event . IsChecked ( ) )
2001-08-28 15:21:45 -04:00
def OnAddsharebtnButton ( self , event ) :
dlg = CommonDefDlg ( self )
try :
2006-02-09 10:41:19 -05:00
if dlg . ShowModal ( ) == wx . ID_OK :
2001-08-28 15:21:45 -04:00
prop , name = dlg . result
if not self . commonDefs . has_key ( name ) :
self . commonDefs [ name ] = commonPropDefs [ prop ]
self . elementLb . Append ( ' % ( ' + name + ' ) ' + \
( type ( commonPropDefs [ prop ] ) is type ( ' ' ) and ' s ' or ' d ' ) )
2004-03-02 18:51:23 -05:00
self . elementLb . SetSelection ( self . elementLb . GetCount ( ) - 1 , True )
2001-08-28 15:21:45 -04:00
self . populateCombosWithCommonDefs ( )
self . OnElementlbListbox ( None )
finally :
dlg . Destroy ( )
def OnRemovesharebtnButton ( self , event ) :
ownGroup = ' style. %s ' % self . lang
2002-05-03 19:32:42 -04:00
comDef = self . elementLb . GetStringSelection ( )
2001-08-28 15:21:45 -04:00
# Search ALL styles before removing
srchDct = { ownGroup : self . styles }
srchDct . update ( self . predefStyleGroups )
srchDct . update ( self . otherLangStyleGroups )
matchList = [ ]
for grpName , styles in srchDct . items ( ) :
if self . findInStyles ( comDef , styles ) :
matchList . append ( grpName )
if matchList :
2006-02-09 10:41:19 -05:00
wx . LogError ( ' Aborted: ' + comDef + ' is still used in the styles of the \n ' \
2001-08-28 15:21:45 -04:00
' following groups in the config file (stc-styles.rc.cfg): \n ' + \
2006-02-09 10:41:19 -05:00
' \n ' . join ( matchList ) )
2001-08-28 15:21:45 -04:00
else :
del self . commonDefs [ comDef [ 2 : - 2 ] ]
self . setStyles ( )
self . populateCombosWithCommonDefs ( )
selIdx = self . elementLb . GetSelection ( )
self . elementLb . Delete ( selIdx )
2004-03-02 18:51:23 -05:00
if selIdx == self . elementLb . GetCount ( ) :
2001-08-28 15:21:45 -04:00
selIdx = selIdx - 1
2003-03-25 01:35:27 -05:00
self . elementLb . SetSelection ( selIdx , True )
2001-08-28 15:21:45 -04:00
self . OnElementlbListbox ( None )
#---STC events------------------------------------------------------------------
def OnUpdateUI ( self , event ) :
2003-03-25 01:35:27 -05:00
styleBefore = self . stc . GetStyleAt ( self . stc . GetCurrentPos ( ) )
if self . styleIdNames . has_key ( styleBefore ) :
self . elementLb . SetStringSelection ( self . styleIdNames [ styleBefore ] ,
True )
else :
self . elementLb . SetSelection ( 0 , False )
self . styleDefST . SetLabel ( ' Style %d not defined, sorry. ' % styleBefore )
self . OnElementlbListbox ( None )
2001-08-28 15:21:45 -04:00
event . Skip ( )
def checkBraces ( self , style ) :
2006-02-09 10:41:19 -05:00
if style == wx . stc . STC_STYLE_BRACELIGHT and self . braceInfo . has_key ( ' good ' ) :
2001-08-28 15:21:45 -04:00
line , col = self . braceInfo [ ' good ' ]
pos = self . stc . PositionFromLine ( line - 1 ) + col
braceOpposite = self . stc . BraceMatch ( pos )
if braceOpposite != - 1 :
self . stc . BraceHighlight ( pos , braceOpposite )
2006-02-09 10:41:19 -05:00
elif style == wx . stc . STC_STYLE_BRACEBAD and self . braceInfo . has_key ( ' bad ' ) :
2001-08-28 15:21:45 -04:00
line , col = self . braceInfo [ ' bad ' ]
pos = self . stc . PositionFromLine ( line - 1 ) + col
self . stc . BraceBadLight ( pos )
else :
self . stc . BraceBadLight ( - 1 )
return
def OnStcstyleeditdlgSize ( self , event ) :
self . Layout ( )
# Without this refresh, resizing leaves artifacts
self . Refresh ( 1 )
event . Skip ( )
def OnMarginClick ( self , event ) :
2003-03-25 01:35:27 -05:00
self . elementLb . SetStringSelection ( ' Line numbers ' , True )
2001-08-28 15:21:45 -04:00
self . OnElementlbListbox ( None )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
#---Common definition dialog----------------------------------------------------
2006-02-09 10:41:19 -05:00
[ wxID_COMMONDEFDLG , wxID_COMMONDEFDLGCANCELBTN , wxID_COMMONDEFDLGCOMDEFNAMETC , wxID_COMMONDEFDLGOKBTN , wxID_COMMONDEFDLGPROPTYPERBX , wxID_COMMONDEFDLGSTATICBOX1 ] = [ wx . NewId ( ) for _init_ctrls in range ( 6 ) ]
2001-08-28 15:21:45 -04:00
2006-02-09 10:41:19 -05:00
class CommonDefDlg ( wx . Dialog ) :
2001-08-28 15:21:45 -04:00
def _init_ctrls ( self , prnt ) :
2006-02-09 10:41:19 -05:00
wx . Dialog . __init__ ( self , id = wxID_COMMONDEFDLG , name = ' CommonDefDlg ' , parent = prnt , pos = wx . Point ( 398 , 249 ) , size = wx . Size ( 192 , 220 ) , style = wx . DEFAULT_DIALOG_STYLE , title = ' Common definition ' )
self . SetClientSize ( wx . Size ( 184 , 200 ) )
2001-08-28 15:21:45 -04:00
2006-02-09 10:41:19 -05:00
self . propTypeRBx = wx . RadioBox ( choices = [ ' Colour value ' , ' Font face ' , ' Size value ' ] , id = wxID_COMMONDEFDLGPROPTYPERBX , label = ' Property type ' , majorDimension = 1 , name = ' propTypeRBx ' , parent = self , point = wx . Point ( 8 , 8 ) , size = wx . Size ( 168 , 92 ) , style = wx . RA_SPECIFY_COLS )
2001-08-28 15:21:45 -04:00
self . propTypeRBx . SetSelection ( self . _propTypeIdx )
2006-02-09 10:41:19 -05:00
self . staticBox1 = wx . StaticBox ( id = wxID_COMMONDEFDLGSTATICBOX1 , label = ' Name ' , name = ' staticBox1 ' , parent = self , pos = wx . Point ( 8 , 108 ) , size = wx . Size ( 168 , 46 ) , style = 0 )
2001-08-28 15:21:45 -04:00
2006-02-09 10:41:19 -05:00
self . comDefNameTC = wx . TextCtrl ( id = wxID_COMMONDEFDLGCOMDEFNAMETC , name = ' comDefNameTC ' , parent = self , pos = wx . Point ( 16 , 124 ) , size = wx . Size ( 152 , 21 ) , style = 0 , value = ' ' )
2001-08-28 15:21:45 -04:00
self . comDefNameTC . SetLabel ( self . _comDefName )
2006-02-09 10:41:19 -05:00
self . okBtn = wx . Button ( id = wxID_COMMONDEFDLGOKBTN , label = ' OK ' , name = ' okBtn ' , parent = self , pos = wx . Point ( 8 , 164 ) , size = wx . Size ( 80 , 23 ) , style = 0 )
self . okBtn . Bind ( wx . EVT_BUTTON , self . OnOkbtnButton , id = wxID_COMMONDEFDLGOKBTN )
2001-08-28 15:21:45 -04:00
2006-02-09 10:41:19 -05:00
self . cancelBtn = wx . Button ( id = wxID_COMMONDEFDLGCANCELBTN , label = ' Cancel ' , name = ' cancelBtn ' , parent = self , pos = wx . Point ( 96 , 164 ) , size = wx . Size ( 80 , 23 ) , style = 0 )
self . cancelBtn . Bind ( wx . EVT_BUTTON , self . OnCancelbtnButton , id = wxID_COMMONDEFDLGCANCELBTN )
2001-08-28 15:21:45 -04:00
def __init__ ( self , parent , name = ' ' , propIdx = 0 ) :
self . _comDefName = ' '
self . _comDefName = name
self . _propTypeIdx = 0
self . _propTypeIdx = propIdx
self . _init_ctrls ( parent )
2002-05-03 19:32:42 -04:00
self . propMap = { 0 : ' fore ' , 1 : ' face ' , 2 : ' size ' }
2001-08-28 15:21:45 -04:00
self . result = ( ' ' , ' ' )
2006-02-09 10:41:19 -05:00
self . Center ( wx . BOTH )
2001-08-28 15:21:45 -04:00
def OnOkbtnButton ( self , event ) :
self . result = ( self . propMap [ self . propTypeRBx . GetSelection ( ) ] ,
self . comDefNameTC . GetValue ( ) )
2006-02-09 10:41:19 -05:00
self . EndModal ( wx . ID_OK )
2001-08-28 15:21:45 -04:00
def OnCancelbtnButton ( self , event ) :
self . result = ( ' ' , ' ' )
2006-02-09 10:41:19 -05:00
self . EndModal ( wx . ID_CANCEL )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
#---Functions useful outside of the editor----------------------------------
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
def setSelectionColour ( stc , style ) :
names , values = parseProp ( style )
if ' fore ' in names :
2003-03-25 01:35:27 -05:00
stc . SetSelForeground ( True , strToCol ( values [ ' fore ' ] ) )
2001-08-28 15:21:45 -04:00
if ' back ' in names :
2003-03-25 01:35:27 -05:00
stc . SetSelBackground ( True , strToCol ( values [ ' back ' ] ) )
2001-08-28 15:21:45 -04:00
def setCursorColour ( stc , style ) :
names , values = parseProp ( style )
if ' fore ' in names :
stc . SetCaretForeground ( strToCol ( values [ ' fore ' ] ) )
def setEdgeColour ( stc , style ) :
names , values = parseProp ( style )
if ' fore ' in names :
stc . SetEdgeColour ( strToCol ( values [ ' fore ' ] ) )
def strToCol ( strCol ) :
2006-02-09 10:41:19 -05:00
assert len ( strCol ) == 7 and strCol [ 0 ] == ' # ' , ' Not a valid colour string: ' + strCol
return wx . Colour ( string . atoi ( ' 0x ' + strCol [ 1 : 3 ] , 16 ) ,
2002-05-03 19:32:42 -04:00
string . atoi ( ' 0x ' + strCol [ 3 : 5 ] , 16 ) ,
2001-08-28 15:21:45 -04:00
string . atoi ( ' 0x ' + strCol [ 5 : 7 ] , 16 ) )
def colToStr ( col ) :
2002-05-03 19:32:42 -04:00
return ' # %s %s %s ' % ( string . zfill ( string . upper ( hex ( col . Red ( ) ) [ 2 : ] ) , 2 ) ,
2001-08-28 15:21:45 -04:00
string . zfill ( string . upper ( hex ( col . Green ( ) ) [ 2 : ] ) , 2 ) ,
string . zfill ( string . upper ( hex ( col . Blue ( ) ) [ 2 : ] ) , 2 ) )
def writeProp ( num , style , lang ) :
if num > = 0 :
return ' style. %s . %s = ' % ( lang , string . zfill ( ` num ` , 3 ) ) + style
else :
return ' setting. %s . %d = ' % ( lang , num ) + style
def writePropVal ( names , values ) :
res = [ ]
for name in names :
if name :
res . append ( values [ name ] and name + ' : ' + values [ name ] or name )
2006-02-09 10:41:19 -05:00
return ' , ' . join ( res )
2001-08-28 15:21:45 -04:00
def parseProp ( prop ) :
2006-02-09 10:41:19 -05:00
items = prop . split ( ' , ' )
2001-08-28 15:21:45 -04:00
names = [ ]
values = { }
for item in items :
2006-02-09 10:41:19 -05:00
nameVal = item . split ( ' : ' )
names . append ( nameVal [ 0 ] . strip ( ) )
2001-08-28 15:21:45 -04:00
if len ( nameVal ) == 1 :
values [ nameVal [ 0 ] ] = ' '
else :
2006-02-09 10:41:19 -05:00
values [ nameVal [ 0 ] ] = nameVal [ 1 ] . strip ( )
2001-08-28 15:21:45 -04:00
return names , values
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
def parsePropLine ( prop ) :
2006-02-09 10:41:19 -05:00
name , value = prop . split ( ' = ' )
return int ( name . split ( ' . ' ) [ - 1 ] ) , value
2001-08-28 15:21:45 -04:00
def setSTCStyles ( stc , styles , styleIdNames , commonDefs , lang , lexer , keywords ) :
styleDict = { }
styleNumIdxMap = { }
# build style dict based on given styles
for numStyle in styles :
num , style = parsePropLine ( numStyle )
styleDict [ num ] = style
# Add blank style entries for undefined styles
newStyles = [ ]
styleItems = styleIdNames . items ( ) + settingsIdNames . items ( )
styleItems . sort ( )
idx = 0
for num , name in styleItems :
styleNumIdxMap [ num ] = idx
if not styleDict . has_key ( num ) :
styleDict [ num ] = ' '
newStyles . append ( writeProp ( num , styleDict [ num ] , lang ) )
idx = idx + 1
2003-03-25 01:35:27 -05:00
# Set background colour to reduce flashing effect on refresh or page switch
bkCol = None
if styleDict . has_key ( 0 ) : prop = styleDict [ 0 ]
2006-02-09 10:41:19 -05:00
else : prop = styleDict [ wx . stc . STC_STYLE_DEFAULT ]
2003-03-25 01:35:27 -05:00
names , vals = parseProp ( prop )
if ' back ' in names :
2006-02-09 10:41:19 -05:00
bkCol = strToCol ( vals [ ' back ' ] % commonDefs )
2003-03-25 01:35:27 -05:00
if bkCol is None :
2006-02-09 10:41:19 -05:00
bkCol = wx . WHITE
2003-03-25 01:35:27 -05:00
stc . SetBackgroundColour ( bkCol )
2001-08-28 15:21:45 -04:00
# Set the styles on the wxSTC
stc . StyleResetDefault ( )
stc . ClearDocumentStyle ( )
stc . SetLexer ( lexer )
stc . SetKeyWords ( 0 , keywords )
2006-02-09 10:41:19 -05:00
stc . StyleSetSpec ( wx . stc . STC_STYLE_DEFAULT ,
styleDict [ wx . stc . STC_STYLE_DEFAULT ] % commonDefs )
2001-08-28 15:21:45 -04:00
stc . StyleClearAll ( )
for num , style in styleDict . items ( ) :
if num > = 0 :
stc . StyleSetSpec ( num , style % commonDefs )
elif num == - 1 :
setSelectionColour ( stc , style % commonDefs )
elif num == - 2 :
setCursorColour ( stc , style % commonDefs )
elif num == - 3 :
setEdgeColour ( stc , style % commonDefs )
stc . Colourise ( 0 , stc . GetTextLength ( ) )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
return newStyles , styleDict , styleNumIdxMap
#---Config reading and writing -------------------------------------------------
2006-02-09 10:41:19 -05:00
commonDefsFile = ' common.defs. %s ' % ( platformSettings [ wx . Platform ] [ 0 ] )
2001-08-28 15:21:45 -04:00
2003-03-25 01:35:27 -05:00
def readPyValFromConfig ( conf , name ) :
2006-02-09 10:41:19 -05:00
ns = { }
#ns.update(wx.stc.__dict__)
ns . update ( wxPython . stc . __dict__ )
ns [ ' wx ' ] = wx
value = conf . Read ( name ) . replace ( ' \r \n ' , ' \n ' ) + ' \n '
try :
return eval ( value , ns )
except :
print value
raise
2003-03-25 01:35:27 -05:00
2001-08-28 15:21:45 -04:00
def initFromConfig ( configFile , lang ) :
2006-02-09 10:41:19 -05:00
if not os . path . exists ( configFile ) :
raise Exception , ' Config file %s not found ' % configFile
cfg = wx . FileConfig ( localFilename = configFile , style = wx . CONFIG_USE_LOCAL_FILE )
2003-03-25 01:35:27 -05:00
cfg . SetExpandEnvVars ( False )
2001-08-28 15:21:45 -04:00
# read in all group names for this language
groupPrefix = ' style. %s ' % lang
gpLen = len ( groupPrefix )
predefStyleGroupNames , otherLangStyleGroupNames = [ ] , [ ]
cont , val , idx = cfg . GetFirstGroup ( )
while cont :
if val != groupPrefix and len ( val ) > = 5 and val [ : 5 ] == ' style ' :
if len ( val ) > gpLen and val [ : gpLen ] == groupPrefix :
predefStyleGroupNames . append ( val )
else :
otherLangStyleGroupNames . append ( val )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
cont , val , idx = cfg . GetNextGroup ( idx )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
# read in common elements
2003-03-25 01:35:27 -05:00
commonDefs = readPyValFromConfig ( cfg , commonDefsFile )
2001-08-28 15:21:45 -04:00
assert type ( commonDefs ) is type ( { } ) , \
' Common definitions ( %s ) not a valid dict ' % commonDefsFile
2003-03-25 01:35:27 -05:00
commonStyleIdNames = readPyValFromConfig ( cfg , ' common.styleidnames ' )
2001-08-28 15:21:45 -04:00
assert type ( commonStyleIdNames ) is type ( { } ) , \
' Common definitions ( %s ) not a valid dict ' % ' common.styleidnames '
2006-02-09 10:41:19 -05:00
# Lang specific settings
2002-05-03 19:32:42 -04:00
cfg . SetPath ( lang )
2003-03-25 01:35:27 -05:00
styleIdNames = readPyValFromConfig ( cfg , ' styleidnames ' )
2001-08-28 15:21:45 -04:00
assert type ( commonStyleIdNames ) is type ( { } ) , \
' Not a valid dict [ %s ] styleidnames) ' % lang
styleIdNames . update ( commonStyleIdNames )
2003-03-25 01:35:27 -05:00
braceInfo = readPyValFromConfig ( cfg , ' braces ' )
2001-08-28 15:21:45 -04:00
assert type ( commonStyleIdNames ) is type ( { } ) , \
' Not a valid dict [ %s ] braces) ' % lang
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
displaySrc = cfg . Read ( ' displaysrc ' )
2003-03-25 01:35:27 -05:00
lexer = readPyValFromConfig ( cfg , ' lexer ' )
2001-08-28 15:21:45 -04:00
keywords = cfg . Read ( ' keywords ' )
cfg . SetPath ( ' ' )
# read in current styles
styles = readStylesFromConfig ( cfg , groupPrefix )
2002-05-03 19:32:42 -04:00
# read in predefined styles
2001-08-28 15:21:45 -04:00
predefStyleGroups = { }
for group in predefStyleGroupNames :
predefStyleGroups [ group ] = readStylesFromConfig ( cfg , group )
# read in all other style sections
otherLangStyleGroups = { }
for group in otherLangStyleGroupNames :
otherLangStyleGroups [ group ] = readStylesFromConfig ( cfg , group )
2002-05-03 19:32:42 -04:00
return ( cfg , commonDefs , styleIdNames , styles , predefStyleGroupNames ,
2001-08-28 15:21:45 -04:00
predefStyleGroups , otherLangStyleGroupNames , otherLangStyleGroups ,
displaySrc , lexer , keywords , braceInfo )
def readStylesFromConfig ( config , group ) :
config . SetPath ( ' ' )
2002-05-03 19:32:42 -04:00
config . SetPath ( group )
2001-08-28 15:21:45 -04:00
styles = [ ]
cont , val , idx = config . GetFirstEntry ( )
while cont :
styles . append ( val + ' = ' + config . Read ( val ) )
cont , val , idx = config . GetNextEntry ( idx )
config . SetPath ( ' ' )
return styles
def writeStylesToConfig ( config , group , styles ) :
config . SetPath ( ' ' )
config . DeleteGroup ( group )
config . SetPath ( group )
for style in styles :
2006-02-09 10:41:19 -05:00
name , value = style . split ( ' = ' )
config . Write ( name , value . strip ( ) )
2001-08-28 15:21:45 -04:00
config . SetPath ( ' ' )
#-------------------------------------------------------------------------------
def initSTC ( stc , config , lang ) :
""" Main module entry point. Initialise a wxSTC from given config file. """
2002-05-03 19:32:42 -04:00
( cfg , commonDefs , styleIdNames , styles , predefStyleGroupNames ,
2001-08-28 15:21:45 -04:00
predefStyleGroups , otherLangStyleGroupNames , otherLangStyleGroups ,
displaySrc , lexer , keywords , braceInfo ) = initFromConfig ( config , lang )
2002-05-03 19:32:42 -04:00
2001-08-28 15:21:45 -04:00
setSTCStyles ( stc , styles , styleIdNames , commonDefs , lang , lexer , keywords )
#-------------------------------------------------------------------------------
if __name__ == ' __main__ ' :
2006-02-09 10:41:19 -05:00
app = wx . PySimpleApp ( )
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
provider = wx . SimpleHelpProvider ( )
wx . HelpProvider . Set ( provider )
2002-05-03 19:32:42 -04:00
2006-02-09 10:41:19 -05:00
home = os . environ . get ( ' HOME ' )
if home :
home = os . path . join ( home , ' .boa-constructor ' )
if not os . path . exists ( home ) :
home = ' . '
else :
home = ' . '
2003-03-25 01:35:27 -05:00
2006-02-09 10:41:19 -05:00
config = os . path . abspath ( os . path . join ( home , ' stc-styles.rc.cfg ' ) )
2001-08-28 15:21:45 -04:00
if 0 :
2006-02-09 10:41:19 -05:00
f = wx . Frame ( None , - 1 , ' Test frame (double click for editor) ' )
stc = wx . stc . StyledTextCtrl ( f , - 1 )
2001-08-28 15:21:45 -04:00
def OnDblClick ( evt , stc = stc ) :
dlg = STCStyleEditDlg ( None , ' Python ' , ' python ' , config , ( stc , ) )
try : dlg . ShowModal ( )
finally : dlg . Destroy ( )
stc . SetText ( open ( ' STCStyleEditor.py ' ) . read ( ) )
2006-02-09 10:41:19 -05:00
stc . Bind ( wx . EVT_LEFT_DCLICK , OnDblClick )
2001-08-28 15:21:45 -04:00
initSTC ( stc , config , ' python ' )
2003-03-25 01:35:27 -05:00
f . Show ( True )
2001-08-28 15:21:45 -04:00
app . MainLoop ( )
else :
2002-05-03 19:32:42 -04:00
dlg = STCStyleEditDlg ( None ,
2001-08-28 15:21:45 -04:00
' Python ' , ' python ' ,
#'HTML', 'html',
#'XML', 'xml',
2002-05-03 19:32:42 -04:00
#'C++', 'cpp',
#'Text', 'text',
#'Properties', 'prop',
2001-08-28 15:21:45 -04:00
config )
try : dlg . ShowModal ( )
finally : dlg . Destroy ( )