C - creating a menu with gtk3 - c

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..");
}

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.

How do I quit a gtk application from a button click?

Im trying to quit an application from a button click. What I'm currently doing Segfaults.
I have tried calling g_application_quit(G_APPLICATION(app)); in main it still segfaults
Calling gtk_widget_destroy(window); also segfaults
#include <stdlib.h>
#include <gtk/gtk.h>
void activate(GtkApplication* app, gpointer data)
{
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_application_add_window(app, GTK_WINDOW(window));
GtkWidget *button = gtk_button_new_with_label("Button");
void shutdown()
{
g_application_quit(G_APPLICATION(app));
}
g_signal_connect(GTK_BUTTON(button), "clicked", G_CALLBACK(shutdown), NULL);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show(button);
gtk_window_present(GTK_WINDOW(window));
}
int main (int argc, char *argv[])
{
GApplicationFlags flags = G_APPLICATION_FLAGS_NONE;
GtkApplication *app = gtk_application_new("com.devab.daw", flags);
GApplication *gapp = G_APPLICATION(app);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
g_application_run(gapp, argc, argv);
g_object_unref (app);
//g_application_quit(G_APPLICATION(app));
return 0;
}
I am using gtk3 and this compiles and works for me.
#include <stdlib.h>
#include <gtk/gtk.h>
void activate(GtkApplication* app, gpointer data)
{
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_application_add_window(app, GTK_WINDOW(window));
GtkWidget *button = gtk_button_new_with_label("Button");
g_signal_connect_swapped(GTK_BUTTON(button), "clicked", G_CALLBACK(gtk_widget_destroy), window);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show(button);
gtk_window_present(GTK_WINDOW(window));
}
int main (int argc, char *argv[])
{
GApplicationFlags flags = G_APPLICATION_FLAGS_NONE;
GtkApplication *app = gtk_application_new("com.devab.daw", flags);
GApplication *gapp = G_APPLICATION(app);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
g_application_run(gapp, argc, argv);
g_object_unref (app);
//g_application_quit(G_APPLICATION(app));
return 0;
}
Move destroy outside activate.
#include <stdlib.h>
#include <gtk/gtk.h>
void destroy (GtkWidget* widget, gpointer data)
{
g_application_quit(G_APPLICATION(data));
}
void activate(GtkApplication* app, gpointer data)
{
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_application_add_window(app, GTK_WINDOW(window));
GtkWidget *button = gtk_button_new_with_label("Button");
g_signal_connect(GTK_BUTTON(button), "clicked", G_CALLBACK(destroy), app);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show(button);
gtk_window_present(GTK_WINDOW(window));
}
int main (int argc, char *argv[])
{
GApplicationFlags flags = G_APPLICATION_FLAGS_NONE;
GtkApplication *app = gtk_application_new("com.devab.daw", flags);
GApplication *gapp = G_APPLICATION(app);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
g_application_run(gapp, argc, argv);
g_object_unref (app);
//g_application_quit(G_APPLICATION(app));
return 0;
}

How to hide a .ui file and show another .ui file in GTK+

I have a button on my window on GTK+ 3. When the button is clicked, a window from another ui file should be opened and the current window's ui should be deleted (Hide). But I don't know how to do so.I am really new to Gtk+ programming on linux.
this is my .c file:
#include <gtk/gtk.h>
static void
buttonClicked(GtkWidget *widget,
gpointer data)
{
//here I need to open another window and close the current one
}
int main (int argc, char *argv[])
{
GtkBuilder *builder;
GObject *window;
GObject *button;
GError *error = NULL;
gtk_init (&argc, &argv);
builder = gtk_builder_new();
if(gtk_builder_add_from_file(builder, "main-window.ui", &error) == 0)
{
g_printerr ("Error loading file: %s\n", error->message);
g_clear_error (&error);
return 1;
}
window = gtk_builder_get_object (builder, "window");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
button = gtk_builder_get_object (builder, "buttonOpen");
g_signal_connect (button, "clicked", G_CALLBACK (buttonClicked), NULL);
gtk_main ();
return 0;
}
Use gtk_widget_show_all () and gtk_widget_hide ().
#include <gtk/gtk.h>
static void
buttonClicked(GtkWidget *widget, GObject* window)
{
GtkBuilder *builder;
GObject *window2;
GError *error = NULL;
builder = gtk_builder_new();
if(gtk_builder_add_from_file(builder, "second-window.ui", &error) == 0)
{
g_printerr ("Error loading file: %s\n", error->message);
g_clear_error (&error);
return;
}
window2 = gtk_builder_get_object (builder, "window");
gtk_widget_show_all(GTK_WIDGET(window2));
gtk_widget_hide(GTK_WIDGET(window));
}
int main (int argc, char *argv[])
{
GtkBuilder *builder;
GObject *window;
GObject *button;
GError *error = NULL;
gtk_init (&argc, &argv);
builder = gtk_builder_new();
if(gtk_builder_add_from_file(builder, "main-window.ui", &error) == 0)
{
g_printerr ("Error loading file: %s\n", error->message);
g_clear_error (&error);
return 1;
}
window = gtk_builder_get_object (builder, "window");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
button = gtk_builder_get_object (builder, "buttonOpen");
g_signal_connect (button, "clicked", G_CALLBACK (buttonClicked), window);
//some widgets don't draw if their parent window was not "show alled"
gtk_widget_show_all(GTK_WIDGET(window));
gtk_main ();
return 0;
}
Note that gpointer is actually a void* so you can pass to your function whatever you like.

How can I create new window in GTK after click?

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;}

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