I Created a window and vbox with 3 buttons in Glade. All buttons have connected "Clicked" event to same handler. Handler looks like this:
CLICKED_btn(GtkObject *object, gpointer user_data)
{
g_print("CLICKED\n");
}
CLICKED appears on terminal for any click to any button.
Is here a way, through object or other to know which button invokes event Clicked in case where all buttons uses same handler?
The object parameter refers to the object that generates the event, in your case the button. Then you can use gtk_widget_get_name() or any other GtkObject/GtkWidget/GtkButton function to make a difference.
UPDATE:
As it seems, newer versions of GTK/Glade do not set the name of the widgets to their id, so it is left to the default, that is the name of the type. In order to get the id of the object you can use the function gtk_buildable_get_name() that works with any buildable object.
With that you'll get button1, button2 or whatever name you put to these buttons.
Please, do not use the label to make a difference between the buttons. Yes, it works, but it is a bad habit: hard to maintain, bad with internationalization, and defeats the main purpose of Glade: to have the interface and the code separated.
First use the documentation to have the right prototype for the "clicked" signal of a GtkButton.
You then know that your callback's prototype should look like:
void on_button_cliked (GtkButton *button, gpointer user_data)
The button parameter is the object which received the signal, ie. the button on which you clicked.
Related
Suppose there are several buttons created programmatically, and there is a common button handler.
onButtonClicked(){
// print button attribute
}
How to understand inside the handler which button is pressed?
WITHOUT passing something special inside the function call. That won't do:
onButtonClicked("button" + i);
In JavaScript, for example, inside the handler, there is $(this), which immediately "understands" which button is pressed.
Maybe there is a similar mechanism in Flutter? I want to hang attributes on the button (without knowing which ones in advance), and then have access to them in the handler.
I think there is only one way to write the print('your function name') inside your function
Just pass the Parameter inside your method.
for example:-
onButtonClicked(Parameter){
// print button attribute
print("${parameter}");
}
And pass different parameter for different buttons.
I have downloaded familyTree project and it doesn't have the ability to search for an specific family member!
I have added a search button to it, and in the handler method section, I need the code that searches for a member that has the specified socialID , and select it (scroll it to the sight, and make it blue (selected)). But I don't know how to programmatically select a treeItem, and make it visible and selected?
My code:
#FXML
private void btnSearch_click(ActionEvent event){
for(TreeItem<FamilyMember> treeItem:root.getChildren()){
if(treeItem.getValue().getNationality().toString()=="22"){
// treeView.setSelectionModel(item);
treeView.getSelectionModel().select(treeItem);
//it still doesnt select the item with nationality=="22"
break;
}
}
}
You can select the item with
treeView.getSelectionModel().select(item);
and if you still need to scroll (I think selecting it might automatically scroll to it), do
treeView.scrollTo(treeView.getRow(item));
A couple of notes:
I do not understand the for loop. Why are you doing
TreeItem<FamilyMember> item = root.getChildren().get(i);
and why are you creating the index i? What is wrong with the treeItem variable you already defined in the loop syntax? Isn't this necessarily exactly the same thing as item?
You need to read How do I compare strings in Java?
I am using GWT. I have a textbox and a drop down list box that have change handlers on them. I also sometimes change the text or selected value from the source code but I don't want the change handler to run when I do this, I only want it to run when the user changes it.
How can I implement this?
For the TextBox, use setValue(T value, boolean fireEvents) using false as second argument, to avoid firing any ValueChangeEvent.
For the ListBox, when you call setSelectedIndex(int index) or setItemSelected(int index, boolean selected) the ChangeEvent is never fired, so you are free to use them programmatically and rely on the ChangeHandler on user action.
I am a newbie to GWT ... need some help in understanding the below class -
What is the use of GWT ClickableTextCell ??
Is there something specific use of it. It uses something called FieldUpdater, why is that used ?
Think of a ClickableTextCell as hot spot. It looks like a regular bit of screen real estate with text in it, but it responds to clicks. What happens when you click it? The click "updates" the field. An update to a field calls the method update(). What does update() do? Whatever you want it to. You provide it by specifying the FieldUpdater. FieldUpdater is an interface, so you can construct one anonymously. Say you have a CellTable, and you have a Column that displays a String inside a ClickableTextCell. You provide your FieldUpdater to the Column:
Column<DataType, String> myIntegerColumn
= new Column<DataType, String>(new ClickableTextCell());
myIntegerColumn.setFieldUpdater(new FieldUpdater<DataType, String>(){
#Override
public void update(int index, DataType object, String value){
// execute code that reacts to a click on this hot spot
}
});
Now whenever the cell gets clicked, that code in update() fires.
A ClickableTextCell is a specific kind of cell. You can see a demo of all the different kinds of cells in this GWT showcase.
This GWT documentation explains what cell widgets are for, goes over all of the different types, and also has examples of how to use them and the ValueUpdater type.
I find that GtkSpinButton can be useful for controlled input numbers on GTK GUI.
But here are some nice features of GTK button which can be unwanted in many cases.
If we have GtkSpinButton inside scrolledwindow then user can accidentally change value, or GtkSpinButton can take scroll behaviour from scrolledwindow.
Question: Is here any possibility to make GtkScrollButton insensible to mouse wheel, at way like is GtkEntry. Or better, could GtkSpinButton be shown without up/down buttons.
If not, how to redirect scroll signal from GtkSpinButton to scrolledwindow?
I try this:
SCROLL_numgreen (GtkObject *object, GdkEvent *event, gpointer user_data)
{
switch (((GdkEventScroll *)event)->direction)
{
case GDK_SCROLL_UP:
return TRUE;
break;
case GDK_SCROLL_DOWN:
return TRUE;
break;
... etc...
but this only "eats" scroll signal from GtkSpinButton and block scrolledwindow at place.
I would most like some general solution without intervention to events of every GtkSpinButton.
Here a few pointers to your queries:
Is here any possibility to make GtkScrollButton insensible to mouse wheel, at way like is GtkEntry?
Mouse wheel scroll, mouse click are events. The events can be masked. This can be done at two levels.
At GtkWidget level: you can use gtk_widget_get_events() which will return the event mask in the form of GdkEventMask. You can modify this as per your need & set it using gtk_widget_set_events()
At GdkWindow level: GtkWidget which has its own drawing/eventing area has a GdkWindow associated with it. You can get the event mask of this window using gdk_window_get_events() and change returned GdkEventMask as per your need & set it to the GdkWindow using gdk_window_set_events(). You can modify event mask through bit-wise operations. If GdkWindow is shared between more than one widget then this mask will effect all the widgets. For masking scroll events you can look into GDK_SCROLL_MASK, GDK_BUTTON_PRESS_MASK & GDK_BUTTON_RELEASE_MASK. You can always check the mask for the event which you are looking for is already set or not. Note: GdkWindow related calls will succeed only after GdkWindow is created for GtkWidget. You can make these calls after gtk_widget_show() of the widget or gtk_widget_show_all of the window which contains these widgets.
Or better, could GtkSpinButton be shown without up/down buttons.
AFAIK GtkSpinButton is implemented to have up/down button indicative of the functionality it provides. If you don't want this, then you can choose another widget say GtkEntry (from which GtkSpinButton is "derived") or GtkLabel. Of course you can create your own widget (from scratch or "derive" from an existing GtkWidget) as per your need & use that same; there is no one stopping you from doing this :)
How to redirect scroll signal from GtkSpinButton to scrolledwindow?
It is possible to do this in the "scroll-event" callback of GtkSpinButton. You can stop the emission of the signal on GtkSpinButton & return FALSE to propagate the event.
...
/* Event callback */
gboolean spinbutton_scroll_handler(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
/* Stop emission on current widget. Default handler also not called */
/* Refer http://developer.gnome.org/gobject/stable/gobject-Signals.html#g-signal-stop-emission-by-name */
g_signal_stop_emission_by_name(widget, "scroll-event");
/* Return FALSE to propagate the event further; thus scroll window will scroll
If TRUE returned other handlers not invoked for this event,
thus no scroll on scroll window */
return FALSE;
}
...
/* Connect scroll-event to the callback */
g_signal_connect(spinbutton, "scroll-event",
G_CALLBACK(spinbutton_scroll_handler),
(gpointer)0);
...
Hope this helps!