Hi,

I've asked on another list regarding the use of superscripts and subscripts in a rich text TextCtrl widget, and here below is the answer I received.

I've also tried doing:

use Wx qw(wxTEXT_ATTR_EFFECT_SUBSCRIPT);

but it gave an error telling that this constant isn't exported by the Wx module. If these constants I need are not exported by Wx, so they can't be used in WxPerl, but if wxWidget supports using superscripts and subscripts, couldn't I use their values instead?

Here is the message I've received. Thank you.

Hi Octavian.  Here are some notes for you which i had gotten from my
research.  I have tried them and they work, but i would think the best way
would be to call the DC or draw context function.  Anyway, I had the 3
outlined methods tried out, all worked successfully, and you can choose
which method would suit you best.
Notes below.
Implementing superscript/subscript
1. Representation
In richtextbuffer.h (or textctrl.h in SVN trunk), we have the
wxTEXT_ATTR_EFFECT_SUBSCRIPT and wxTEXT_ATTR_EFFECT_SUPERSCRIPT text
effect styles defined. So you would do this programmatically:
wxTextAttrEx attr;
attr.SetTextEffects(wxTEXT_ATTR_EFFECT_SUPERSCRIPT);
attr.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_SUPERSCRIPT);
control->SetStyle(from, to, attr);
(The purpose of SetTextEffectFlags is to indicate which flags are
relevant to the current operation, i.e. setting the given styles.)
In Word and OpenOffice, the font you specify for the superscript text is
not the one that’s used – some multiplication factor is used to scale
the font. This could be hardwired, simplifying the API, or it could be
specified (optionally). You might use an existing field to do this, e.g.
m_outlineLevel since this is a paragraph attribute and wouldn’t be used
for at the character level anyway. You could have new functions to
access this, e.g. wxTextAttrEx::SetScriptSize,
wxTextAttrEx::GetScriptSize. A value of zero would cause the default to
be used. We would probably need an extra flag, wxTEXT_ATTR_SCRIPT_SIZE
to indicate whether this is set.
I’m also assuming that the positioning is hard-wired to be the top or
the bottom of the text to the left of the super/subscript text. This
probably doesn’t need customisation.
2. Drawing implementation
In wxRichTextPlainText::Draw, the new effect flags will need to be
checked – if either is present, the smaller font must be created from
the current attribute font and a scale factor, and x and y text
positions should be recalculated to allow for the smaller font. So,
something like:
if (textAttr.GetFont().Ok()) // this has been moved up w.r.t. current
SVN so GetCharHeight returns correct value
wxCheckSetFont(dc, textAttr.GetFont());
int charHeight = dc.GetCharHeight();
int x = rect.x;
int y = rect.y + (rect.height - charHeight - (descent - m_descent));
if (textAttr.HasTextEffects() & (textAttr.GetTextEffects() &
(wxTEXT_ATTR_EFFECT_SUBSCRIPT| wxTEXT_ATTR_EFFECT_SUPERSCRIPT)))
{
wxFont scriptFont(textAttr.GetFont().GetPointSize() * 0.5, ...);
wxCheckSetFont(dc, scriptFont);
// y is already correct for superscript
if (textAttr.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT)
{
y = rect.y + (rect.height – dc.GetCharHeight() – (descent – m_descent)
}
}
wxRichTextPlainText::GetRangeSize needs a similar treatment to make sure
the text width is calculated using the smaller font, but the height
should continue to reflect the specified (larger) font. So
dc.GetCharHeight() should be used to calculate the line height with the
_attribute_ font set in the DC, whereas ‘w’ should be calculated with
smaller font set.
3. UI implementation
The font page (richtextfontpage.h/cpp) needs to be updated to allow
specification of superscript or subscript. This can be done with 3-state
checkboxes via the DialogBlocks .pjd file in src/richtextctrl (copy an
existing one, e.g. Capitals). Then the code in TransferDataFromWindow/
TransferDataToWindow needs changing – should be obvious from the
existing handling what’s required. It probably also needs event handlers
for the two new checkboxes to clear (or rather make indeterminate) the
other checkbox so you can’t specify subscript and superscript
simultaneously.
UpdatePreview() can be updated to simply scale the font if either of the
new styles are set.
Cheers!


Octavian

Reply via email to