multiple fonts in a text control Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
forrestcupp
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Dec 28, 2006 5:12 pm
Location: Indiana, US

multiple fonts in a text control

Post by forrestcupp »

Does anyone know if it is possible to have multiple fonts in a text control? I have an app with a multi line text control and a font dialog. When I change the font, it changes the font of everything in the text control. I also tried setting it up as a rich text control and the same thing happens.

I would like to be able to type something, then use the font dialog to change the font for whatever is typed from that point on. Is this possible?
timg
Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post by timg »

How are you setting the new font in the wxTextCtrl? Are you specifying where to start with the new style?

Code: Select all

myTextCtrl->SetStyle(myTextCtrl->GetInsertionPoint(),myTextCtrl->GetInsertionPoint(),myStyle);
forrestcupp
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Dec 28, 2006 5:12 pm
Location: Indiana, US

Post by forrestcupp »

Well, this is what I was trying that caused everything to be changed.

Code: Select all

	if(font->ShowModal() == wxID_OK)
	{
	
	tcMain->SetFont(font->GetFontData().GetChosenFont());
	}
Based on what you said, I put the following right before the SetFont line.

Code: Select all

		tcMain->SetStyle(tcMain->GetInsertionPoint(), tcMain->GetInsertionPoint(),
			(wxTextAttr) wxTEXT_ATTR_FONT);
It didn't work. It still changed all of the text. If I'm doing it wrong, can you show me the right way?

Thank you.
timg
Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post by timg »

Don't do the first part - that sets the font for the whole text control.

Instead, do something like this:

Code: Select all


        if(font->ShowModal() == wxID_OK)
        {
          wxTextAttr   tmp;
          tmp.SetFont(font->GetFontData().GetChosenFont());

          tcMain->SetStyle(tcMain->GetInsertionPoint(), tcMain->GetInsertionPoint(), tmp);
         }
That should set the style for the textctrl from the insertion point forward.
forrestcupp
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Dec 28, 2006 5:12 pm
Location: Indiana, US

Post by forrestcupp »

Thank you for the reply, but that didn't work.

When I did that it didn't change the font at all. The arguments call for a long start, long end, and a wxTextAttr. I wonder if it's not working because the start and end are both the insertion point.
timg
Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post by timg »

It should only change the font for newly typed text.

Maybe you can explain a little more what you are doing. I am using similar code in one of my projects and it does work.
forrestcupp
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Dec 28, 2006 5:12 pm
Location: Indiana, US

Post by forrestcupp »

Well, I just figured it out. When I created my text control, I needed to add wxTE_RICH to the styles. I found out that on Windows, a wxTextCtrl will not support styles unless you use wxTE_RICH.

Thanks for your help. Now what you showed me works.
Post Reply