Hello!
I made a custom GTK widget and I need to add a GTK Button to it so to inherit its signals in the custom one. The custom widget is a timeline so I thought to use GTK Buttons to avoid messing with mouse coordinates check. When the user wants to move the picture represented by the button or just extend its delay on the timeline itself, signals will be emitted from the button widget and my life will be much easier. My custom widget inherits from GtkDrawingArea. I tried to use a GtkLayout instead but with no luck.
The button of course is not shown on the timeline. that’s my problem. I know I have to put the button inside a container but how to do it in the draw function? Does anyone have a clue on how to do it? I googled and looked here but with no luck. I know it’s possibile as gtk_textview can add a child widget into it. I looked at the source code but I couldn’t understand how it achieves this.
This is the custom widget draw function:
static gboolean img_timeline_draw(GtkWidget *da, cairo_t *cr)
{
GtkWidget *button;
ImgTimelinePrivate *priv = img_timeline_get_instance_private((ImgTimeline*)da);
gint width = gtk_widget_get_allocated_width(da);
cairo_translate (cr, 0 , 12);
img_timeline_draw_time_ticks(da, cr, width);
//Video timeline
cairo_translate (cr, 0 , 20);
cairo_set_source_rgba(cr, priv->video_background[0], priv->video_background[1], priv->video_background[2], priv->video_background[3]);
cairo_set_line_width(cr, 1);
cairo_rectangle(cr, 0,10, width - 2, 59);
cairo_fill(cr);
//separator
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_rectangle(cr, 0,69, width - 2, 3);
cairo_fill(cr);
//Audio timeline
cairo_set_source_rgba(cr, priv->audio_background[0], priv->audio_background[1], priv->audio_background[2], priv->audio_background[3]);
cairo_rectangle(cr, 0,72, width - 2, 59);
cairo_fill(cr);
button = gtk_button_new_with_label("I'm invisible...");
gtk_widget_realize(button);
gtk_widget_show(button);
return FALSE;
}
And this is the main.c:
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Imagination timeline widget");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 800, 200);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *timeline = img_timeline_new();
g_object_set(timeline, "total_time", 300, NULL);
g_object_set(timeline, "video_background", "#0084ff", NULL);
g_object_set(timeline, "audio_background", "#0084ff", NULL);
gtk_widget_add_events( timeline, GDK_BUTTON1_MOTION_MASK
| GDK_POINTER_MOTION_MASK
| GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
| GDK_SCROLL_MASK
| GDK_KEY_PRESS_MASK );
GtkWidget *scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
GtkWidget *viewport = gtk_viewport_new(NULL,NULL);
g_signal_connect( G_OBJECT(timeline), "scroll-event", G_CALLBACK(img_timeline_scroll), scrolledwindow1);
gtk_widget_set_hexpand(viewport, TRUE);
gtk_widget_set_vexpand(viewport, TRUE);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_POLICY_AUTOMATIC, GTK_POLICY_NEVER);
gtk_container_add (GTK_CONTAINER(viewport), timeline);
gtk_container_add (GTK_CONTAINER(scrolledwindow1), viewport);
gtk_container_add (GTK_CONTAINER (window), scrolledwindow1);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
Would anyone please help? Or point me to some software which did this so I can look the source code?
Many thanks
Giuseppe