简体   繁体   中英

How to do Bold, Italic, Underline buttons in Gtk# aka How to dynamically format text in gtk

Maybe this is a little to broad or vague of a question.

I can't find anything that makes sense to me online, I am working with Gtk2.0 and C#, and every reference on how to do this online, is either in a different language and just seems to be a list of names of functions, or only has examples of formatting text in a textview while generating the text from code (Ie, make a sentence "Hello World." then make the hello bold . It seems that no one is talking about how to do this, and it seems like a pretty fundamental bit of functionality.

It's perfectly easy to make some text bold, for example:

protected void Command_bold(object sender, EventArgs e)
{
    if (selectedTextView.Buffer.GetSelectionBounds(out A, out B))
    {
        selectedTextView.Buffer.ApplyTag("bold", A, B);
    }
}

But, when I trigger this function with a button, it only makes things bold (or italics... etc). Normal functionality of a bold button would make all of the selection bold if some or none of the selection is already bold, or make it all not bold if all of it is bold.

So, how do you detect in a Gtk TextView object, if your text is bold already or not?

Ok, so I figured it out. More or less. The logic below is flawed but this is a way to detect tags and control the logic of a bold button:

protected void Action_Bold(object sender, EventArgs e)
{
    TextIter iA, A, B;
    bool isBold = false;
    if (selectedTextView.Buffer.GetSelectionBounds(out A, out B))
    {
        iA = A;
        while (iA.Compare(B) < 0)
        {
            foreach (TextTag tag in A.Tags)
            {
                if (tag.Name == "bold") isBold = true;
            }

            iA.ForwardChar();

        }

        if (isBold == true)
        {
            selectedTextView.Buffer.RemoveTag("bold", A, B);
        }
        else
        {
            selectedTextView.Buffer.ApplyTag("bold", A, B);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM