开发者

Writing bullets and number text GTKTextView

How can i add bullets and number using GTKTextView in Linux working e开发者_如何学运维nvironment.


First begin with reading Text Widget Overview

Then I think you need to combine GtkTextTag (for formatting the text) with some creativity.

This is all assuming you want bullet/numbered lists.


So, I needed exactly that and just implemented it. Here's how I did it..

    let mut tab_ar = pango::TabArray::new(2, true);
    tab_ar.set_tab(0, pango::TabAlign::Left, 0);
    tab_ar.set_tab(1, pango::TabAlign::Left, 14);
    tag_table.add(
        &gtk::TextTagBuilder::new()
            .name("list_item")
            .indent(-14)
            .left_margin(14)
            .wrap_mode(gtk::WrapMode::Word)
            .tabs(&tab_ar)
            .build(),
    );

(this is rust, but it would be similar in other languages)

To add a new bullet, you'd insert in the textview "1.\titem" or "•\titem" (\t being the tab character), with that "list_item" tag.

Here is the explanation:

  1. indent applies only to the first line of the bullet, NOT the following lines if the text for the bullet gets wrapped on multiple lines. In this case we set a NEGATIVE INDENT. meaning the first line will be offset to the left compared to the following wrapped lines. We will use that negative offset to put the bullet or the bullet number.
  2. left margin applies to all the lines of the bullet, including the wrapped lines. So the whole bullet text is moved to the right
  3. we specify two tabs: the first at 0px, the second at 14px. Meaning when we put the text "1.\titem", "1." will be at the full left, "item" will start at 14px

Put all this together...

The first line of the bullet starts at the horizontal offset 14px (left margin) -14px (indent) => 0px. There we put "1.". Then there is a \t and so we move to offset 14px. Then the text wraps. On the second line (if the bullet doesn't fit on one line), the text starts at 14px (left margin). indent doesn't apply. So we stay at 14px.

In the screenshot... blue is the margins, red is the indent, and green is the second tab. And you can see everything lines up well.

Writing bullets and number text GTKTextView

To be 100% safe it would be best to measure the width of "1." and add some percents for safety, then use this for the offset instead of 14px, in case the user has a larger font or something like that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜