I have a requirement like follows,
in this there will be list of user names with some status icons on there left in one row... when it receive clicked event i need to change the background color to vis开发者_如何学编程ualize that it is selected... i tried every way but i can't put image and label together plus i don't know how to change background color of label...
This whole list need to have scroll bars as there can be n numbers of items...
can anyone suggest me how to do it... which widgets to use for this... can some one point to tutorials examples.
Thanks.
Combining the image and the text should be no problem; just create a GtkImage and a GtkLabel, pack both into a GtkHBox, and add that to the button. In GTK+, buttons are containers, and can hold any combination of widgets. Adjust the packing parameters to the image is small, and the label gets the remaining space.
The flashing of the background is harder; GtkLabels don't render their background, so they can't affect the color of that area. You could probably flash the foreground color easily enough (using inline HTML in the label text, for instance), you might want to start off with that and then revisit the issue once you've learned more of GTK+.
Stuff each finished button into a GtkVBox, and place that in a GtkScrolledWindow to get the scrolling display.
UPDATE: To learn which button gets clicked, you need to connect a handler for the "clicked" signal:
static void cb_button_clicked(GtkButton *button, gpointer user)
{
printf("Whaddaya know, button number %d was clicked!\n", GPOINTER_TO_INT(user));
}
... elsewhere, when building the button ...
GtkWidget *btn;
btn = gtk_button_new_with_label("My fancy button");
g_signal_connect(G_OBJECT(btn), "clicked", G_CALLBACK(cb_button_clicked), GINT_TO_POINTER(42));
The above code builds a simple labelled button, your code will obviously be more complex but this doesn't change how the signal handler is attached.
Just use the existing GtkTreeView (even if your "tree" just has a single branch). Follow the tutorial to learn how to create the labels and how to render them.
For a really good tutorial/introduction to GtkTreeView, check out GTK+ 2.0 Tree View Tutorial.
To get the icon and the label you could use a GtkImage and a GtkLabel packed in a GtkHBox. In order to set the background color, you have to put the HBox inside a GtkEventBox. Labels can't render its background, so you have to put them in a widget capable of that, in this case, the EventBox. Then you can set the background using the gtk_widget_modify_bg
function of the EventBox.
With GtkEventBox + GtkHBox + GtkImage + GtkLabel you have your item. Now, put every item in a GtkVBox. To get scrolling support, you should put your VBox in a GtkScrolledWindow. Add it using the gtk_scrolled_window_add_with_viewport
function. If you are using Glade, then you will have to put the GtkVbox inside a GtkViewPort and then the GtkViewPort inside the GtkScrolledWindow. The ViewPort widget does all the magic needed for scrolling.
Then you can change the background of the selected item catching the "clicked" signal.
精彩评论