Setting wxTextCtrl Margins Problem on macOS  [SOLVED]

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
TextEdit
In need of some credit
In need of some credit
Posts: 8
Joined: Fri May 11, 2018 7:31 am

Setting wxTextCtrl Margins Problem on macOS

Postby TextEdit » Fri May 11, 2018 7:40 am

Hi everyone,

I tried to set margins for wxTextCtrl using `wxTextCtrl::SetMargins` on macOS, but it never succeeds. I tried with margin value of 0 and 7, both didn't work. The code snippets I tested was below. Copy and paste it below Line 1258 in the samples/text/text.cpp. Also screenshots are attached. The first screenshot is setting margins to (7, 7), and (0, 0) the second one.

Code: Select all

    int m = 0;
    m_text->SetMargins(m, m);
    wxPoint ms = m_text->GetMargins();
    wxLogMessage("%s", m_text->GetValue());
    wxLogMessage("expect %d, left %d, top %d", m, ms.x, ms.y);


The development environment is macOS (10.13.4) with wxWidgets 3.1.1.

BTW, to avoid the X-Y problem, what I am trying to do is to make a wxTextCtrl auto expand as more text is entered. Multi-line, no border. So I use `textCtrl->SetSize(dc.GetMultiLineTextExtent(text))` to do that. Is there a better way?

Any suggestion? Thanks!
Attachments
Screen Shot 2018-05-11 at 12.27.30 AM.png
Margin 7
Screen Shot 2018-05-11 at 12.27.05 AM.png
Margin 0
Last edited by TextEdit on Fri May 11, 2018 7:47 am, edited 1 time in total.

catalin
Moderator
Moderator
Posts: 1473
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: Setting wxTextCtrl Margins Problem on macOS

Postby catalin » Fri May 11, 2018 7:46 am

What did SetMargins() return?
If true, then it's a bug; if false then they might not be supported on that OS version or there is a bug. In both cases you can step into the call and investigate it yourself.

TextEdit
In need of some credit
In need of some credit
Posts: 8
Joined: Fri May 11, 2018 7:31 am

Re: Setting wxTextCtrl Margins Problem on macOS

Postby TextEdit » Fri May 11, 2018 8:11 am

Emmm... Just found this.

Code: Select all

bool wxTextEntryBase::DoSetMargins(const wxPoint& WXUNUSED(pt))
{
    return false;
}

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 2787
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Setting wxTextCtrl Margins Problem on macOS

Postby ONEEYEMAN » Fri May 11, 2018 2:52 pm

Hi,
Patches welcome. ;-)

Thank you.

Manolo
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 611
Joined: Mon Apr 30, 2012 11:07 pm

Re: Setting wxTextCtrl Margins Problem on macOS

Postby Manolo » Fri May 11, 2018 3:32 pm

Margins in controls such as wxTextCtrl, wxComboBox or wxChoice were allowed time ago. Some OSs, or at least some controls in some OSs, have dismissed it (I'm thinking of GTK+). That's why I can't be sure if margins will be set or not.

The workaround is setting a fixed or minimal size to the control.
You can find this size by using wxControl::GetSizeFromTextSize (see http://docs.wxwidgets.org/trunk/classwx ... 506784efc9). Be aware that the size is calculated with the current font in that control.

Once you have a size, you can increment it slightly. This may be not enough due to aligment of the contents of the control.

TextEdit
In need of some credit
In need of some credit
Posts: 8
Joined: Fri May 11, 2018 7:31 am

Re: Setting wxTextCtrl Margins Problem on macOS

Postby TextEdit » Fri May 11, 2018 5:53 pm

Manolo wrote:Margins in controls such as wxTextCtrl, wxComboBox or wxChoice were allowed time ago. Some OSs, or at least some controls in some OSs, have dismissed it (I'm thinking of GTK+). That's why I can't be sure if margins will be set or not.


Yes. I found related discussion on margins. That's why I am surprised to find it's not supported any more.

Manolo wrote:The workaround is setting a fixed or minimal size to the control.
You can find this size by using wxControl::GetSizeFromTextSize (see http://docs.wxwidgets.org/trunk/classwx ... 506784efc9). Be aware that the size is calculated with the current font in that control.

Once you have a size, you can increment it slightly. This may be not enough due to aligment of the contents of the control.


The wxTextCtrl::GetSizeFromTextSize() also returns {-1, -1}. :lol:

Well, a super wired thing is that I find that margins on macOS are related to the initial size of the control. For example, I add 5 to the left and 2 to the top as margins, there will be no wrap; but if they are changed to 5 and 1, the text is wrapped. This is just like what was described in this bug http://trac.wxwidgets.org/ticket/2438, but not sure if they are of the same cause. Didn't test what the values will be if font size changed, but it didn't feel right. (This is incorrect. There is a bug in my code.)

Anyway, I will tweak coordinates, then will see what else I can do. Thanks everyone!
Last edited by TextEdit on Fri May 11, 2018 7:10 pm, edited 1 time in total.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 2787
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Setting wxTextCtrl Margins Problem on macOS

Postby ONEEYEMAN » Fri May 11, 2018 7:05 pm

Hi,
Does this help?

Thank you.

TextEdit
In need of some credit
In need of some credit
Posts: 8
Joined: Fri May 11, 2018 7:31 am

Re: Setting wxTextCtrl Margins Problem on macOS

Postby TextEdit » Fri May 11, 2018 7:10 pm

I also saw that about how to set margins in NSTextField. It would work, but I am working on wxWidgets only. And I barely know Objective-C.

TextEdit
In need of some credit
In need of some credit
Posts: 8
Joined: Fri May 11, 2018 7:31 am

Re: Setting wxTextCtrl Margins Problem on macOS  [SOLVED]

Postby TextEdit » Mon May 14, 2018 12:23 am

After "playing" with the coordinates, I looked into customizing the NSTextView. I added following code and bunch of scaffoldings.

Code: Select all

bool wxNSTextViewControl::DoSetMargins(const wxPoint& pt)
{
    [m_textView setTextContainerInset:NSMakeSize(static_cast<float>(pt.x),
                                            static_cast<float>(pt.y))];
    return true;
}

wxPoint wxNSTextViewControl::DoGetMargins() const
{
    NSSize s = [m_textView textContainerInset];
    return wxPoint(s.width, s.height);
}


However, now I can set the inset to 0 and wxTextCtrl::GetMargins() returns the inset 0, but on UI it makes no difference. Setting margin width to 0 change nothing, but setting to 10 does work. Margin height doesn't affect anything. But the whole stackoverflow is suggesting to set the inset.

Could anyone familiar with NSTextView shed some light on this? And what is the wxNSTextFieldControl used for? It seems has nothing to do with wxTextCtrl. If I want to implement the Get/SetMargins, should I leave wxNSTextFieldControl with the default implementation? (which does nothing)

Thanks!

-------------------------------------

Okay... macOS text display is complicated. By adding more code, I have made it. Reference of those text layout names at https://developer.apple.com/library/con ... rgins.html

Code: Select all

bool wxNSTextViewControl::DoSetMargins(const wxPoint& pt)
{
    [m_textView setTextContainerInset:NSMakeSize(static_cast<float>(pt.x),
                                            static_cast<float>(pt.y))];
    [[m_textView textContainer] setLineFragmentPadding:0.f];       // hard-coded 0 here
    return true;
}


Then it seems to be a very specific implementation. I will try to make a patch.

In case anyone is looking for a solution before I submit the patch, here it is: https://github.com/xinhuang/wxWidgets/c ... 8a4098cd66


Return to “Platform Related Issues”

Who is online

Users browsing this forum: No registered users and 3 guests