StyledTextCtrl Margins

Attached is my code where I’ve added 3 margins to a StyleTextCtrl. I’m using the StyledTextCtrl from the demo folder.

Starting on line 221 I’ve added 3 margins:

0: line numbers

1: VLINE symbol

2: BOOKMARK symbol

My question is: I thought each one of these items would be in their own column, but the VLINE symbol and the BOOKMARK symbol over lap each other. Is that correct or am I missing something in my code to separate them?

margins.zip (3.5 KB)

Hi,

Quote from your code:

    ed.SetMarginType(1, stc.STC_MARGIN_SYMBOL)
    ed.SetMarginWidth(1, 16)
    ed.MarkerDefine(1, stc.STC_MARK_VLINE, '#ffff00', '#ffff00')  # yellow

    ed.SetMarginType(2, stc.STC_MARGIN_SYMBOL)
    ed.SetMarginWidth(2, 16)
    ed.MarkerDefine(2, stc.STC_MARK_BOOKMARK, '#ff00ff', '#ff00ff')  # magenta

Firstly, MarkerDefine arg 1 is not a margin number but a marker number. So, what you are doing above is the definition that Marker number # 1 is VLINE, # 2 is BOOKMARK, and so on.

Next, you have to set a margin mask to select what marker should be displayed. You can check the current mask by GetMarginMask, and the result will be

>>> ed.GetMarginMask(1)
33554431
>>> ed.GetMarginMask(2)
0

The mask of margin=1 default is 0x1fffffff (=~stc.STC_MASK_FOLDERS), but margin=2 is 0
Therefore, both markers are displayed in the margin=1 but no markers in the margin=2.
If you want to let margin=1 show marker=1 only and margin=2 show marker=2 only, set each bitmask as,

ed.SetMarginMask(1, 1<<1)
ed.SetMarginMask(2, 1<<2)

good luck :yum:

I don’t understand this. My margins.py is giving me this error: AttributeError: module ‘run’ has no attribute ‘main’. I had to add: from subprocess import run to get past the run AttributeError I was seeing. I don’t get it, this py file was running ok a few days ago. I’ll try and get it to run and apply your suggestions.

While I’m working on getting margins.py to run again, can you please explain what SetMarginMask() is doing? I’ve read the docs on this but it’s still not clear to me how it works. Thank you.

If you are using Windows, you will find run.py in the wxdemo directory (~\AppData\Local\wxPython\wxPython-demo-4.1.1\demo), which is created when Python38\Scripts\wxdemo.exe runs.

The margin mask is a 32-bit value, and each bit corresponds to one of 32 markers.
For example, marker=0 corresponds to 0b0001, marker=1 to 0b0010, marker=2 to 0b0100, and marker=n to (1<<n).

I think this site (wxStyledTextCtrl - Margins) will help you.

I’m attaching margins2.zip as my latest working file.

VLINE is displaying in margin 1 as I want.

I’m getting the editor line color changed for the bookmark. I may not have been clear that I just want the bookmark symbol to display in margin 2 and not change the line color.

" If a line has an associated marker that does not not appear in the mask of any margin with a non-zero width, the marker changes the background colour of the line"
I cannot understand this line. My brain just cannot make sense of it. While my current margins2 file does change the line color for bookmark, and this is not what I want, I cannot understand WHY it does change the color. If I can understand this, then I could apply the right value to margin 2 to get the bookmark to display.

margins2.zip (3.57 KB)

I’m attaching margins2.zip as my latest working file.

VLINE is displaying in margin 1 as I want.

I’m getting the editor line color changed for the bookmark. I may not have been clear that I just want the bookmark symbol to display in margin 2 and not change the line color.

" If a line has an associated marker that does not not appear in the mask of any margin with a non-zero width, the marker changes the background colour of the line"

I cannot understand this line. My brain just cannot make sense of it. While my current margins2 file does change the line color for bookmark, and this is not what I want, I cannot understand WHY it does change the color. If I can understand this, then I could apply the right value to margin 2 to get the bookmark to display.margins2.zip (3.6 KB)

I finally figured it out. I got the VLINE and BOOKMARK to display in margins 1 and 2, respectively.

Thank you for all the help!!!