How can I create new window in GTK after click? - c

I'm new at GTK, and I was wondering how can I create a new window after a click button. I've got this function
void cb_create_entry(GtkWidget *, gpointer);
int create_window(int argc, char *argv[]){
GtkWidget *p_window = NULL;
GtkWidget *p_main_box = NULL;
GtkWidget *p_button[5];
gtk_init (&argc, &argv);
//Create window
p_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(p_window), "Hello");
gtk_window_set_default_size(GTK_WINDOW(p_window), 320, 200);
p_main_box = gtk_vbox_new(TRUE, 0);
gtk_container_add(GTK_CONTAINER(p_window), p_main_box);
{
p_button[0] = gtk_button_new_with_label("Create entry");
g_signal_connect(G_OBJECT(p_button[0]), "clicked",
G_CALLBACK(cb_create_entry), NULL);
gtk_box_pack_start(GTK_BOX(p_main_box), p_button[0], FALSE, FALSE, 0);
}
gtk_widget_show_all(p_window);
gtk_main ();
return EXIT_SUCCESS;
and callback.h
#ifndef CALLBACK_H_INCLUDED
#define CALLBACK_H_INCLUDED
#include <gtk/gtk.h>
void cb_create_entry(GtkWidget *p_widget, gpointer user_data){
gtk_button_released(p_widget);
GtkWidget *p_window;
GtkWidget *p_v_box;
GtkWidget *p_entry;
p_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(p_window), "Create DB");
gtk_window_set_default_size(GTK_WINDOW(p_window), 320, 200);
p_v_box = gtk_vbox_new(TRUE, 0);
gtk_container_add(GTK_CONTAINER(p_window), p_v_box);
p_entry = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(p_v_box), p_entry, TRUE, FALSE, 0);
}
and main
int main(int argc, char const *argv[]) {
create_window(argc, argv);
return 0;
}
But it doesn't work. I'd like to create a new window with an input. But when I click on button, nothing happens.

I'm a bit confused on how you laid out your file structure. Since there's no input on that I'll assume that the file with create_window function is the same where you have main. Then, callback.h should not have implementation code.
Nevertheless, i don't see any gtk_widget_show or gtk_widget_show_all calls and not sure you suppressed or just missed them. I'll assume the later because by your description it seems that you can see the initial window.
It's also missing a call to gtk_main.
Adding GtkWidget show functions and gtk_main to your code, it does work like expected:
Lets call the first file main.c :
#include <gtk/gtk.h>
#include "callback.h"
void cb_create_entry(GtkWidget *, gpointer);
int create_window(int argc, char *argv[]){
GtkWidget *p_window = NULL;
GtkWidget *p_main_box = NULL;
GtkWidget *p_button[5];
gtk_init (&argc, &argv);
//Create window
p_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(p_window), "Hello");
gtk_window_set_default_size(GTK_WINDOW(p_window), 320, 200);
p_main_box = gtk_vbox_new(TRUE, 0);
gtk_container_add(GTK_CONTAINER(p_window), p_main_box);
p_button[0] = gtk_button_new_with_label("Create entry");
g_signal_connect(G_OBJECT(p_button[0]), "clicked",
G_CALLBACK(cb_create_entry), NULL);
gtk_box_pack_start(GTK_BOX(p_main_box), p_button[0], FALSE, FALSE, 0);
gtk_widget_show_all(p_window);
}
int main (int argc, char *argv[]) {
create_window(argc, argv);
gtk_main ();
return 0;
}
And the other file callbacks.h :
#include <gtk/gtk.h>
void cb_create_entry(GtkWidget *p_widget, gpointer user_data){
gtk_button_released(p_widget);
GtkWidget *p_window;
GtkWidget *p_v_box;
GtkWidget *p_entry;
p_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(p_window), "Create DB");
gtk_window_set_default_size(GTK_WINDOW(p_window), 320, 200);
p_v_box = gtk_vbox_new(TRUE, 0);
gtk_container_add(GTK_CONTAINER(p_window), p_v_box);
p_entry = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(p_v_box), p_entry, TRUE, FALSE, 0);
gtk_widget_show_all(p_window);
}
Then compiling with:
gcc -o test main.c callback.h `pkg-config --cflags --libs gtk+-3.0`
will result in a window with a button, which after being pressed will create and show a new window with a GtkEntry:

now callback.h looks like that :
int cb_create_entry(GtkWidget *p_widget, gpointer user_data){
GtkWidget *p_window = NULL;
GtkWidget *p_entry = NULL;
GtkWidget *p_button = NULL;
GtkWidget *p_main_box = NULL;
GtkWidget *p_label = NULL;
p_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(p_window), "Create entry");
gtk_window_set_default_size(GTK_WINDOW(p_window), 320, 200);
g_signal_connect (G_OBJECT (p_window), "destroy", G_CALLBACK (cb_quit), NULL);
p_main_box = gtk_vbox_new(TRUE, 0);
gtk_container_add(GTK_CONTAINER(p_window), p_main_box);
p_label = gtk_label_new("Please, name your DB");
gtk_container_add(GTK_CONTAINER(p_main_box), p_label);
p_entry = gtk_entry_new();
gtk_container_add(GTK_CONTAINER(p_main_box), p_entry);
p_button = gtk_button_new_with_label("Create !");
gtk_container_add(GTK_CONTAINER(p_main_box), p_button);
{
GtkWidget *p_quit = NULL;
p_quit = gtk_button_new_with_label("Quit");
g_signal_connect(G_OBJECT(p_quit), "clicked", G_CALLBACK(cb_quit), NULL);
gtk_box_pack_start(GTK_BOX(p_main_box), p_quit, FALSE, FALSE, 0);
}
//gtk_widget_show(p_entry);
gtk_widget_show_all(p_window);
return EXIT_SUCCESS;}

Related

How to remove an image from a widget gtk c?

I am currently working on an user interface with gtk.
What I've done for now is that you can select an image from your file and display it with the button "open".
The problem is that when I open a second image it add the image to the window without removing the current one.
I would like to know how to change the displayed image when the user choose to open another image.
My code:
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "../Threshold/delete_color.h"
#define GTK_STOCK_CANCEL ((GtkStock)"gtk-cancel")
GtkWidget *image;
gchar* path;
void cb_quit(GtkWidget *p_widget, gpointer user_data)
{
gtk_main_quit();
(void)p_widget;
(void)user_data;
}
void solve(const gchar* path)
{
SDL_Surface* image;
// Binarization
image = binarization((char*) file_name);
SDL_SaveBMP(image, "binarize.bmp");
// Rotation
//rotation(image);
SDL_SaveBMP(image, "rotation.bmp");
// Grid Detection
SDL_SaveBMP(image, "grid.bmp");
// Case detection
// TODO
// Get the grid.txt
// TODO
// Get the grid solution
// TODO
// Create the solution image
// TODO
}
static void set_image(const gchar *file_name, gpointer user_data)
{
GtkWidget *pVBox;
GdkPixbuf *pixbuf;
GError *error = NULL;
pVBox = (GtkWidget*) user_data;
int width = gtk_widget_get_allocated_width(pVBox);
int height = gtk_widget_get_allocated_height(pVBox);
pixbuf = gdk_pixbuf_new_from_file("result.bmp", &error);
if (!error)
{
GdkPixbuf *pixbuf_mini = NULL;
pixbuf_mini = gdk_pixbuf_scale_simple(pixbuf,
width,
height - 200,
GDK_INTERP_NEAREST);
image = gtk_image_new_from_pixbuf(pixbuf_mini);
gtk_container_add(GTK_CONTAINER(pVBox), image);
gtk_widget_show(image);
}
}
void cb_open(GtkWidget *p_widget, gpointer user_data)
{
GtkWidget *p_dialog = NULL;
p_dialog = gtk_file_chooser_dialog_new("Open file", NULL,
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL);
if(gtk_dialog_run (GTK_DIALOG (p_dialog)) == GTK_RESPONSE_ACCEPT)
{
gchar *file_name = NULL;
path = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (p_dialog));
file_name = path;
g_print("%s\n", path);
set_image(file_name, user_data);
g_free (file_name), file_name = NULL;
}
gtk_widget_destroy(p_dialog);
(void)p_widget;
}
int main(int argc, char **argv)
{
GtkWidget *p_window = NULL;
GtkWidget *p_main_box = NULL;
// Initialisation
gtk_init (&argc, &argv);
// Main window
p_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(p_window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(p_window), 600, 400);
g_signal_connect (G_OBJECT (p_window), "destroy",
G_CALLBACK (cb_quit), NULL);
// Main container
p_main_box = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER (p_window), p_main_box);
// Exit button
{
GtkWidget *p_button = NULL;
p_button = gtk_button_new_with_label("Exit");
g_signal_connect(G_OBJECT (p_button), "clicked", G_CALLBACK (cb_quit), NULL);
gtk_box_pack_start(GTK_BOX (p_main_box), p_button, FALSE, FALSE, 0);
}
// Open button
{
GtkWidget *p_button = NULL;
p_button = gtk_button_new_with_label ("Open");
g_signal_connect (G_OBJECT (p_button), "clicked", G_CALLBACK (cb_open), (gpointer*) p_main_box);
gtk_box_pack_start (GTK_BOX (p_main_box), p_button, FALSE, FALSE, 0);
}
// Display
gtk_widget_show_all(p_window);
g_print("%s\n", path);
gtk_main();
return EXIT_SUCCESS;
}
You can try multiple approaches.
Remove old image and add new image
Use gtk_container_get_children to retrieve your old image if present and remove it using gtk_container_remove. Then add as you have done before.
You can replace the content of the image
Again, use gtk_container_get_children to retrieve your old image if present and replace the content using gtk_image_set_from_pixbuf.
(additionally to 1 or 2 above)
You can also create an image already when you create the container and use the pointer to that image as user_data for your callback. That would remove the need for retrieving the child from your container.

C - creating a menu with gtk3

I'm trying to make a simple menu in gtk3. I was looking at the official documentation and other code I found in the web, but still I am not able to get it to work. The menu does not appear.
I would appreciate any help. Thank you.
This is the code I have so far:
#include <stdio.h>
#include <gtk/gtk.h>
//#include "minefield.h"
enum difficulty {BEGINNER, ADVANCED, EXPERT};
static void activate(GtkApplication* app, gpointer user_data);
GdkPixbuf *create_pixbuf(const gchar * filename);
void newGame();
void openSetupDialog();
void openOptionDialog();
int main(int argc, char *argv[]) {
GtkApplication *app;
int status;
app = gtk_application_new("my.ne.sweeper", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run(G_APPLICATION (app), argc, argv);
g_object_unref(app);
return status;
}
static void activate(GtkApplication* app, gpointer user_data) {
GdkPixbuf *app_icon;
GtkWidget *app_window;
GtkWidget *app_box;
GtkWidget *menubar;
GtkWidget *gameMenu;
GtkWidget *gameMenu_game;
GtkWidget *gameMenu_newGame;
GtkWidget *gameMenu_setup;
GtkWidget *gameMenu_options;
GtkWidget *gameMenu_quit;
GtkWidget *sep;
// App_window
app_window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(app_window), "Minesweeper");
gtk_window_set_default_size(GTK_WINDOW(app_window), 600, 400);
gtk_window_set_position(GTK_WINDOW(app_window), GTK_WIN_POS_CENTER);
// App_box
app_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add(GTK_CONTAINER(app_window), app_box);
// App_icon
app_icon = create_pixbuf("../img/app_icon.svg");
gtk_window_set_icon(GTK_WINDOW(app_window), app_icon);
// Menubar
menubar = gtk_menu_bar_new();
gtk_box_pack_start(GTK_BOX(app_box), menubar, FALSE, FALSE, 0);
// Start menu
gameMenu = gtk_menu_new();
gameMenu_game = gtk_menu_item_new_with_label("Game");
gameMenu_newGame = gtk_menu_item_new_with_label("New Game");
gameMenu_setup = gtk_menu_item_new_with_label("Setup");
gameMenu_options = gtk_menu_item_new_with_label("Options");
gameMenu_quit = gtk_menu_item_new_with_label("Quit");
sep = gtk_separator_menu_item_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(gameMenu_game), gameMenu);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), gameMenu_game);
gtk_menu_shell_append(GTK_MENU_SHELL(gameMenu), gameMenu_newGame);
gtk_menu_shell_append(GTK_MENU_SHELL(gameMenu), gameMenu_setup);
gtk_menu_shell_append(GTK_MENU_SHELL(gameMenu), gameMenu_options);
gtk_menu_shell_append(GTK_MENU_SHELL(gameMenu), sep);
gtk_menu_shell_append(GTK_MENU_SHELL(gameMenu), gameMenu_quit);
gtk_widget_show_all(app_window);
// Signalhandlers
g_signal_connect(app_window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(gameMenu_newGame, "activate", G_CALLBACK(newGame), NULL);
g_signal_connect(gameMenu_setup, "activate", G_CALLBACK(openSetupDialog), NULL);
g_signal_connect(gameMenu_options, "activate", G_CALLBACK(openOptionDialog), NULL);
g_signal_connect(gameMenu_quit, "activate", G_CALLBACK(gtk_main_quit), NULL);
g_object_unref(app_icon);
}
GdkPixbuf *create_pixbuf(const gchar * filename) {
GdkPixbuf *pixbuf;
GError *error = NULL;
pixbuf = gdk_pixbuf_new_from_file(filename, &error);
if (!pixbuf) {
fprintf(stderr, "[!] %s\n", error->message);
g_error_free(error);
}
return pixbuf;
}
void newGame() {
fprintf(stdout, "[*] %s\n", "new game..");
}
void openSetupDialog() {
fprintf(stdout, "[*] %s\n", "open setup..");
}
void openOptionDialog() {
fprintf(stdout, "[*] %s\n", "open options..");
}

How do I make a GTK3 image-in-a-window shrinkable?

I have a simple GTK3 app that displays an image from a file in a window.
When you resize the window, the image is scaled in the expose callback to fit the window.
However, once the window has grown, you can't shrink it again; the resize handles only let you make the window ever bigger.
With GTK2 it was trivial to allow grow and shrink with gtk_window_set_policy(w,1,1,1).
How can the same effect be achieved in GTK3?
Here's the ever-growing code example:
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
gboolean resize_image(GtkWidget *widget, GdkEvent *event, void *data)
{
GdkPixbuf *pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(widget));
if (pixbuf == NULL)
{
g_printerr("Failed to get pixbuf\n");
return 1;
}
pixbuf = gdk_pixbuf_scale_simple(pixbuf,
widget->allocation.width, widget->allocation.height,
GDK_INTERP_BILINEAR);
gtk_image_set_from_pixbuf(GTK_IMAGE(widget), pixbuf);
return FALSE;
}
int main(int argc, char **argv)
{
GtkWidget *window = NULL;
GtkWidget *image = NULL;
if (argc < 2 || argc > 3)
{
g_printerr("Usage: %s <image>\n", argv[0]);
return 1;
}
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
image = gtk_image_new_from_file(argv[1]);
if (image == NULL)
{
g_printerr("Could not open \"%s\"\n", argv[1]);
return 1;
}
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(image, "expose-event", G_CALLBACK(resize_image), NULL);
gtk_container_add(GTK_CONTAINER(window), image);
gtk_widget_show_all(GTK_WIDGET(window));
gtk_main();
return 0;
}
Eric Cecashon on the gtk-list mailing list suggests using a cairo drawing area inside a 1x1 grid container, which works fairly well:
/*
gcc -Wall da_resize.c -o da_resize `pkg-config gtk+-3.0 --cflags --libs`
Tested on Ubuntu16.04, GTK3.18.
*/
#include<gtk/gtk.h>
gboolean draw_picture(GtkWidget *da, cairo_t *cr, gpointer data)
{
gint width=gtk_widget_get_allocated_width(da);
gint height=gtk_widget_get_allocated_height(da);
GdkPixbuf *temp=gdk_pixbuf_scale_simple((GdkPixbuf*)data, width, height, GDK_INTERP_BILINEAR);
gdk_cairo_set_source_pixbuf(cr, temp, 0, 0);
cairo_paint(cr);
g_object_unref(temp);
return FALSE;
}
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), "Resize Picture");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
//Needs a valid picture.
GdkPixbuf *pixbuf=gdk_pixbuf_new_from_file(argc>1 ? argv[1] : "image.jpg", NULL);
GtkWidget *da1=gtk_drawing_area_new();
gtk_widget_set_hexpand(da1, TRUE);
gtk_widget_set_vexpand(da1, TRUE);
g_signal_connect(da1, "draw", G_CALLBACK(draw_picture), pixbuf);
GtkWidget *grid=gtk_grid_new();
gtk_grid_attach(GTK_GRID(grid), da1, 0, 0, 1, 1);
gtk_container_add(GTK_CONTAINER(window), grid);
gtk_widget_show_all(window);
gtk_main();
g_object_unref(pixbuf);
return 0;
}

GTK Application doesn't quit from dialog

I'm programming my first bigger GTK+-Application and i have some troubles with exiting the application.
I want to have a Quit-Button in a dialog box, because normally you should run the program in full-screen-mode.
First I tried to call "gtk_main_quit" direktly from a signal but it also didn't work. Now i tried it through an event, the console output works but "gtk_main_quit" doesn't do anything!
Can somebody explain what I'm doing wrong? If you want to give me some tips for better coding, I will really welcome that, too!
Thanks for you help in advance!
#include <stdlib.h>
#include <gtk/gtk.h>
#include <time.h>
static gboolean gtk_delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
g_message("delete event occured\n");
gtk_main_quit();
return TRUE;
}
static void check_toggle_fullscreen (GtkToggleButton *checkButton_fullscreen, GtkWindow *window)
{
if (gtk_toggle_button_get_active(checkButton_fullscreen))
{
gtk_window_fullscreen(GTK_WINDOW(window));
}
else
{
gtk_window_unfullscreen(GTK_WINDOW(window));
}
}
static gboolean double_clicked (GtkWidget *eventbox, GdkEventButton *event, GtkWindow *window)
{
GtkWidget *dialog, *hbox, *checkButton_fullscreen, *image, *button_preferences, *button_closeApp;
dialog = gtk_dialog_new_with_buttons("Schnelleinstellung", window, GTK_DIALOG_MODAL, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
checkButton_fullscreen = gtk_check_button_new_with_label("Fullscreen");
image = gtk_image_new_from_stock(GTK_STOCK_FULLSCREEN, GTK_ICON_SIZE_BUTTON);
hbox = gtk_hbox_new(FALSE, 10);
gtk_box_pack_start_defaults (GTK_BOX(hbox), image);
gtk_box_pack_start_defaults (GTK_BOX(hbox), checkButton_fullscreen);
if (gdk_window_get_state(gtk_widget_get_window(GTK_WIDGET(window))) & GDK_WINDOW_STATE_FULLSCREEN)
{
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkButton_fullscreen), TRUE);
}
button_closeApp = gtk_button_new_from_stock(GTK_STOCK_QUIT);
button_preferences = gtk_button_new_from_stock(GTK_STOCK_PREFERENCES);
//Fill dialog with content
gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), button_preferences);
gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), button_closeApp);
g_signal_connect(G_OBJECT(checkButton_fullscreen), "toggled", G_CALLBACK(check_toggle_fullscreen), (gpointer)window);
g_signal_connect(G_OBJECT(button_closeApp), "clicked", G_CALLBACK(gtk_delete_event), NULL);
if (event-> type == GDK_2BUTTON_PRESS)
{
gtk_widget_show_all(dialog);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
return FALSE;
}
int main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *vbox2;
GtkWidget *label[5];
GtkWidget *frame1, *frame2;
GtkWidget *eventbox;
PangoFontDescription *font;
gtk_init(&argc, &argv);
//Window_TOPLEVEL
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"Abnahme");
gtk_window_maximize(GTK_WINDOW(window));
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_NONE);
gtk_window_set_decorated(GTK_WINDOW(window), TRUE);
vbox = gtk_vbox_new(FALSE, 10);
vbox2 = gtk_vbox_new(FALSE, 10);
frame1 = gtk_frame_new("Naechster Skid");
frame2 = gtk_frame_new("Warteschlange");
eventbox = gtk_event_box_new();
gtk_frame_set_shadow_type(GTK_FRAME(frame1), GTK_SHADOW_IN);
gtk_frame_set_shadow_type(GTK_FRAME(frame2), GTK_SHADOW_IN);
label[0] = gtk_label_new("1");
font = pango_font_description_from_string("Arial 40");
gtk_widget_modify_font(label[0], font);
gtk_label_set_markup(GTK_LABEL(label[0]), "<b>Erster Skid</b>");
int j = 1;
while(j<5)
{
gchar buffer[10];
label[j] = gtk_label_new("0");
g_snprintf(buffer, sizeof(buffer), "%i",j+1);
gtk_label_set_markup(GTK_LABEL(label[j]), buffer);
gtk_widget_modify_font(label[j], font);
j++;
}
//Level 0
gtk_container_add(GTK_CONTAINER(window), eventbox);
gtk_event_box_set_above_child(GTK_EVENT_BOX(eventbox), TRUE);
gtk_container_add(GTK_CONTAINER(eventbox), vbox);
//Level 1
gtk_box_pack_start(GTK_BOX(vbox),frame1, TRUE, TRUE, 0);
//Level 2
gtk_container_add(GTK_CONTAINER(frame1), label[0]);
//Level 1
gtk_box_pack_start(GTK_BOX(vbox),frame2, TRUE, TRUE, 0);
//Level 2
gtk_container_add(GTK_CONTAINER(frame2), vbox2);
//Level 3
int i = 1;
while(i<5)
{
gtk_box_pack_start(GTK_BOX(vbox2), label[i], TRUE, TRUE, 0);
i++;
}
//Signals
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_delete_event), NULL);
g_signal_connect(G_OBJECT(eventbox), "button_press_event", G_CALLBACK(double_clicked), (gpointer) window);
gtk_widget_set_events(eventbox, GDK_BUTTON_PRESS_MASK);
gtk_widget_realize(eventbox);
/* Enter the main loop */
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
gtk_dialog_run() enters a recursive main loop. gtk_main_quit() only exits the innermost recursion of the main loop.
I don't know if GTK+ provides a clean way to do what you want; you might have to do that yourself somehow.
To delete gtk_dialog_run() and gtk_widget_destroy() like under the code is available to quit dialog button.
if (event-> type == GDK_2BUTTON_PRESS)
{
gtk_widget_show_all(dialog);
/*
* gtk_dialog_run(GTK_DIALOG(dialog));
* gtk_widget_destroy(dialog);
*/
}
It seems that the reason is running dialog is blocking to quit app.

How to set placeholder text in GtkEntry

I tried this example:
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
GdkPixbuf *create_pixbuf(const gchar * filename) {
GdkPixbuf *pixbuf;
GError *error = NULL;
pixbuf = gdk_pixbuf_new_from_file(filename, &error);
if(!pixbuf) {
fprintf(stderr, "%s\n", error->message); g_error_free(error);
}
return pixbuf;
}
void implement() {
printf("HI");
}
int main( int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *frame;
GtkWidget *label;
GtkWidget *text;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "New Message");
gtk_window_set_default_size(GTK_WINDOW(window), 310, 390);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_icon(GTK_WINDOW(window), create_pixbuf("web.png"));
frame = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), frame);
text = gtk_entry_new();
gtk_entry_set_max_length (GTK_ENTRY (text), 0);
gtk_entry_set_width_chars (GTK_ENTRY (text), 37);
gtk_entry_set_placeholder_text(GTK_ENTRY (text), "Send a message");
gtk_fixed_put(GTK_FIXED(frame), text, 2, 360);
gtk_widget_show_all(window);
g_signal_connect(window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_main();
return 0;
}
but getting error : undefined reference togtk_entry_set_placeholder_text'`
Make sure your GTK+ version is 3.2 or later, since that's when that particular function was added.

Resources