tags:

views:

116

answers:

4

I have a requirement like follows,

alt text

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 visualize 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.

+1  A: 

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.

unwind
Just love the way you have explained... thanks.... i am just going to try it!... but i have created this screen using Glade and in that i have one vBox (with one empty place) i need to add this whole stuff (list) to that place... so should i add GtkScrolledWindow to that place?
PP
@Pradeep: Thanks. I have a ... few years of GTK+ experience, it helps with questions like these. :) And yes, start from the "outside", by adding a GtkScrolledWindow.
unwind
ok Done with all the basic things, now i have attached clicked event to each button in list.. but how should i get to know that which button is clicked?
PP
out of the n buttons in the list how should i get to know that which button is clicked...can i add some UNIQUE ID that will represent the button which i will receive with call back.
PP
@Pradeep: Yes, that's what the second (gpointer-typed) argument to the signal handler is for. I added code to pass an integer, but you can of course pass a pointer to some structure that describes the event, or whatever.
unwind
+1  A: 

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.

Aaron Digulla
+1  A: 

For a really good tutorial/introduction to GtkTreeView, check out GTK+ 2.0 Tree View Tutorial.

axel_c
A: 

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.

Manuel Ceron