WX: attempting to select value when clicking a text field - c++

We are implementing a text field to do numeric entry. We subclass the standard wxTextCtrl. Behavior is that when user decides to edit the value they get a full precision version and it is selected.
First attempt was to override the focus handler so that when the field gets focus the value is shown in full and selected. This works great except for the cases when you click on the field. In that case, the focus handler is called and the value is selected...but some time after that it is deselected and the edit point put where the mouse click occurred. I thought maybe I could override SetSelection and set a debug point but it seems to use some other method to set the selection! I'm stuck having to walk through the miles of event handling crap to figure it out...I'm thinking in the mouse button handlers but so far I haven't found any.
It would be nice if someone who might already know what I need to override to point it out. Where do I need to be looking?

I would try this code for your wxTextCtrl (but I'm not sure if this is what you want - focus and click)
m_textCtrl1->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( MyDialog1::LeftDown ), NULL, this );
m_textCtrl1->Connect( wxEVT_SET_FOCUS, wxFocusEventHandler( MyDialog1::SetFocus ), NULL, this );
void LeftDown( wxMouseEvent& event );
void SetFocus( wxFocusEvent& event );
In case what you do is overwritten by the default (system) handler of your control you could try 'wxPostEvent' to do what you want later (after the default handling).
I also suggest using a designer for this (for this question I stole some code generated by wxFormBuilder)

Answer is that it can't be done. The behavior I'm trying to override is implemented by the win32 component itself and wx doesn't provide any method to doing so....short of bypassing WX to get the win32 handle and using win32 api.
Would need to write a new component in the WX api to get the desired behavior.

Related

QComboBox::showPopup() steals focus from its QLineEdit

In order to create a QComboBox that can filter its values list when typing in it, I attached a slot to the QComboBox's editTextChanged-event, to open its list view popup when the user starts typing. This is done like so:
void SearchableComboBox::slotEditing(QString in_text)
{
this->showPopup();
}
Unfortunately, this immediatly steals the focus from the QLineEdit and I can't type anymore.
Calling lineEdit()->setFocus() makes no difference, and I don't want to grabKeyboard() since this creates a whole other world of pain.
Any recommendations?
This isn't really a combobox then, more of a completion listview for a lineedit. I implemented exactly that in ruqola (KDE client for rocket chat), you can see the source code at https://lxr.kde.org/source/network/ruqola/src/widgets/common/completionlistview.cpp. Notice the little dance with the focus proxy thing:
setFocusPolicy(Qt::NoFocus);
textWidget->setFocusPolicy(origPolicy);
setFocusProxy(textWidget);
and the long method slotCompletionAvailable() for positioning the completion popup at the right place...
Use the Focus Proxy method for this purpose. See https://doc.qt.io/qt-5/qwidget.html#setFocusProxy
With this the pop-up would relay its inputs to the lineedit.
In your case you could try something like
this->setFocusProxy( this->lineEdit() );
But maybe you should read how to use a QCompleter. This would provide Autocompletion while typing and is maybe also useful for you.

Double validation in QTreeWidget

I am building a program using QTreeWidget. When I double click to a child, it allows to edit this child. I write a function to validate new input value using QDoubleValidator and I want to show a error tooltip but it not work, so I have a question whether I can write a validator that check input value immediately ( like ajax in web development) and show a error tooltip ?
Can anyone have me?
Your question is pretty inspecific because you missed the chance of providing any code.
Anyway, I think you should not need any self implemented QValidator, if and only if the functionality of QDoubleValidator are sufficient.
Thing I would do is implementing a slot in the main widget/dialog whatever and connect the signal void QTreeWidget::itemChanged(QTreeWidgetItem *item, int column) to the created slot. Whenever any tree item changes the slot gets invoked.
Inside the slot you may do any magic for validating the change, wheter you use QDoubleValidator or not. After any invalid check you may display an error message or pop up a QMessageBox.
If you require more information or help you have to provide more information. Feel free to generate a MCVE. For a nice example look here.

How to know if the Gtk::ComboBoxText is popup

I am writing a simple GUI, in which I have a ComboBoxText. I write a log message when ever the user clicks on the ComboBoxText.
I have tried almost all the button release and popup signals but no results. The only thing which works is signal_changed() but I don't not need that. Please help me, below is my sample code :
myCombo->signal_button_release_event().connect(sigc::mem_fun(this,&ComboBoxText::ComboInput),false);
and here is the call back function:
bool ComboBoxText::ComboInput(GdkEventButton *pEvt) {
// Here do the desired stuffs !!
return false; }
Use GTK+ property popup-shown. Not sure about Gtkmm syntax, probably property_popup_shown().get_value().
If you need a signal to listen to, connect to popdown or notify::popup-shown (the latter is invoked when property value changes; again, I'm not sure about Gtkmm syntax).
The idea here was to fire an event when the ComboBoxText is clicked. After some readings I figured it out that the ComboBoxText does not fire any on_click event as such.
One could mask a key press event (which by the way gets fired) and call the signal handler. This might not be handy for people who specifically looking for a on_click event but for those who are working with a keyboard or touch screen device. Here is a small chunk of code :`
mCombo.add_events(Gdk::KEY_PRESS_MASK);
mCombo.signal_event().connect(sigc::mem_fun(this,&ClassName::Handler),false);
cheers :)

MFC how to automatically select all text in CEdit control

I have a CMFCToolBarComboBoxButton on a CMFCToolBar. I want that whenever the CMFCToolBarComboBoxButton gets the focus its entire text will be selected.
What is the most elegant way to implement it?
Thanks a lot!
Adi Barda
Not sure what the most elegant way is, but I guess the most common way to do this is to make a derived class and override OnSetFocus (exact method name not checked), and call SetCurSel() on the contained edit control. WM_FOCUS is only send to the control and there is no notification message for it afaik, so you'll have not many options besides doing something that will make the control handle the event - be it reflect it to somewhere else, or just implement the behavior itself. (I guess theoretically there's all sorts of finicking one can do with intercepting messages, but that's certainly not the most elegant way...)
That should be the default behavior of the standard edit control. If it's not, something else is removing that behavior explicitly. Maybe you can find some flag you can set on the combobox button?
If not: subclass the edit control, handle the WM_GETDLGCODE message. Return a value ORed with DLGC_HASSETSEL. If the control has this flag set, then Windows will automatically select all text on focus.

How to mix C++ and external buttons on seperate window?

I want to make a C++ button on Start>Run i.e but when I do it will not do signalled event?
Im sorry I have seen that you do not get the question.
Ok basically when you create a button with CreateWindowEx(); I want to do that but put on a different window with SetPArent which I have already done now the button does not work so I need my program to someone get when it is clicked from the Run window as example!
And yes you have it, but it's not making the button is the problem it's getting when it's clicked with my program since it does not belong to it anymore!
You need to apply the ancient but still-supported technique known in Windows as subclassing; it is well explained here (15-years-old article, but still quite valid;-). As this article puts it,
Subclassing is a technique that allows
an application to intercept messages
destined for another window. An
application can augment, monitor, or
modify the default behavior of a
window by intercepting messages meant
for another window.
You'll want "instance subclassing", since you're interested only in a single window (either your new button, or, the one you've SetParented your new button to); if you decide to subclass a window belonging to another process, you'll also need to use the injection techniques explained in the article, such as, injecting your DLL into system processes and watching over events with a WH_CBT hook, and the like. But I think you could keep the button in your own process even though you're SetParenting it to a window belonging to a different process, in which case you can do your instance subclassing entirely within your own process, which is much simpler if feasible.
"Superclassing" is an alternative to "subclassing", also explained in the article, but doesn't seem to offer that many advantages when compared to instance subclassing (though it may compared with global subclassing... but, that's not what you need here anyway).
You'll find other interesting articles on such topics here, here, and here (developing a big, rich C++ library for subclassing -- but, also showing a simpler approach based on hooks which you might prefer). Each article has a pretty difference stance and code examples, so I think that having many to look at may help you find the right mindset and code for your specific needs!
OK, I'll do my very best - as I understand you, you're trying to inject a button into some existing window. That meaning: Your tool creates a button in some window that does not belong to your application. Now, you want to be notified when that button is pressed. Am I correct so far?
To be notified about the button being pressed, you need to get the respective window message, which will only work if you also "inject" a different WndProc into the window. Actually I have no idea how that should work, but I faintly remember functions like GetWindowLong and SetWindowLong. Maybe they will help?
EDIT
I've searched MSDN a little: While you can get the address of a window's WndProc using GetWindowLong, you can not set the WndProc using SetWindowLong on Windows NT/2000/XP (and up I suppose). See here (MSDN).
So what you could do is install a global message hook that intercepts all window messages, filter those for the window you've injected the button into and then find your message. If you have trouble with this, however, I'm the wrong person to ask, because it's been years ago since I've done anything like that, but it would be stuff for a new question.
EDIT 2
Please see Alex Martinellis answer for how to define the hook. I think he's describing the technique I was referring to when I talked about defining global message hooks to intercept the window messages for the window you injected your button into.