create an headbar with about dialog in GTK+3 - c

I'm trying to create a head bar that should contain an "About" labelled button to show up a dialog when clicked.
#include <gtk/gtk.h>
static void activate_about(GtkWidget *window, gpointer data);
int main(int argc, char *argv[]) {
gtk_init(&argc, &argv);
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *about_button;
GtkWidget *headbar;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
headbar = gtk_header_bar_new();
gtk_header_bar_set_title (GTK_HEADER_BAR (headbar), "Welcome to GTK");
gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (headbar), TRUE);
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 12);
about_button = gtk_button_new_with_mnemonic("_About");
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_container_add(GTK_CONTAINER(vbox), headbar);
gtk_container_add(GTK_CONTAINER(headbar), about_button);
g_signal_connect(about_button, "clicked",
G_CALLBACK(activate_about), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
static void activate_about(GtkWidget *window, gpointer data) {
const gchar *authors[] = {"ad Chi", NULL};
gtk_show_about_dialog(GTK_WINDOW(window),
"program-name", "jjkjk",
"version", "0.0.1",
"copyright", "(C) 2017 ad Chi",
"license-type", GTK_LICENSE_GPL_3_0,
"website", "https://github.com/kjk",
"comments", "Totally kj",
"authors", authors,
"documenters", NULL,
"logo-icon-name", "start-here",
"title", "About kj kj",
NULL);
return;
}
The first issue is that now I end up with two bars the window one and that of the headbar widget. I need just one. The second problem is, when I click the button I get
(button:1727): GLib-GObject-WARNING **: 17:59:47.965: invalid cast from 'GtkButton' to 'GtkWindow'
(button:1727): Gtk-CRITICAL **: 17:59:47.982: gtk_window_set_transient_for: assertion 'parent == NULL || GTK_IS_WINDOW (parent)' failed. I am new to GTK. I suppose that gtk_show_about_dialog(GTK_WINDOW(window)... is not the proper widget to use.

#include <gtk/gtk.h>
static void activate_about(GtkWidget *widget, gpointer window);
int main(int argc, char *argv[]) {
gtk_init(&argc, &argv);
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *about_button;
GtkWidget *headbar;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
headbar = gtk_header_bar_new();
gtk_header_bar_set_title (GTK_HEADER_BAR (headbar), "Welcome to GTK");
gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (headbar), TRUE);
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 12);
about_button = gtk_button_new_with_mnemonic("_About");
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_window_set_titlebar(GTK_WINDOW(window), headbar);
gtk_header_bar_pack_start(GTK_HEADER_BAR(headbar), about_button);
g_signal_connect(about_button, "clicked",
G_CALLBACK(activate_about), window);
g_signal_connect_swapped (window, "destroy",
G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
static void activate_about(GtkWidget *widget, gpointer window) {
const gchar *authors[] = {"ad Chi", NULL};
gtk_show_about_dialog(GTK_WINDOW(window),
"program-name", "jjkjk",
"version", "0.0.1",
"copyright", "(C) 2017 ad Chi",
"license-type", GTK_LICENSE_GPL_3_0,
"website", "https://github.com/kjk",
"comments", "Totally kj",
"authors", authors,
"documenters", NULL,
"logo-icon-name", "start-here",
"title", "About kj kj",
NULL);
return;
}

Related

Setting a custom GtkAccelLabel

I am trying to set a custom GtkAccelLabel to my GtkMenuItem, though I cant figure out how to use that widget ... for me just nothing is displayed.
Why are the following calls wrong / not sufficient ?
...
fileMi = gtk_menu_item_new_with_label("File");
label = gtk_accel_label_new ("Strg+X");
gtk_accel_label_set_accel_widget (label, quitMi);
...
Here a complete reproducer:
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *menubar;
GtkWidget *fileMenu;
GtkWidget *fileMi;
GtkWidget *quitMi;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
gtk_window_set_title(GTK_WINDOW(window), "Simple menu");
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
GtkAccelGroup *accel_group;
// Create a GtkAccelGroup and add it to the window.
accel_group = gtk_accel_group_new ();
gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
menubar = gtk_menu_bar_new();
fileMenu = gtk_menu_new();
fileMi = gtk_menu_item_new_with_label("File");
quitMi = gtk_menu_item_new_with_label("Quit");
gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileMi), fileMenu);
gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), quitMi);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), fileMi);
gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
GtkWidget *label = gtk_accel_label_new ("Strg+X");
gtk_accel_label_set_accel_widget (label, quitMi);
gtk_accel_label_refetch (label);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(quitMi), "activate",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
Hah, I found a solution .. though no idea if it actually should be done like that.
The key is, that there is already a gtk_accel_label on the MenuItem, and I can modify it:
for (GList* lp = gtk_container_get_children (quitMi); lp != NULL; lp = lp->next)
{
if (GTK_IS_ACCEL_LABEL (lp->data))
gtk_accel_label_set_accel (lp->data, GDK_KEY_0, GDK_MOD1_MASK);
}

Printing value of entry box in a dialog in gtk c

I have worked recently with gtk+ in c language.
I have tried to communicate between two callbacks in a dialog and not in the main window (like here). But, unlike the solutions in the link, the program crushes (in the second callback when I try to use the widget I get from the parameters).
This is my code for This:
#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
static void print_text (GtkWidget *widget, gpointer data)
{
GtkEntry* entry = data;
printf("%s", gtk_entry_get_text(entry));
}
static void open_dialog (GtkWidget *widget, gpointer data)
{
GtkWidget *window = data;
GtkWidget *dialog;
GtkWidget *content_area;
GtkWidget *grid;
GtkWidget *label;
GtkWidget *button;
static GtkEntry *textbox;
dialog = gtk_dialog_new_with_buttons ("Get Text",
window,
GTK_DIALOG_MODAL,
GTK_STOCK_OK,
GTK_RESPONSE_OK,
NULL);
content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
grid = gtk_grid_new();
gtk_container_add (GTK_CONTAINER (content_area), grid);
label = gtk_label_new("Value: ");
gtk_grid_attach(GTK_GRID(grid), label, 0, 0, 1, 1);
textbox = gtk_entry_new();
gtk_entry_set_text(textbox, "<Value>");
gtk_grid_attach(GTK_GRID(grid), textbox, 1, 0, 1, 1);
gtk_widget_show_all (dialog);
g_signal_connect (GTK_DIALOG (dialog), "response", G_CALLBACK (print_text), textbox);
}
static void activate (GtkApplication *app, gpointer user_data)
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *entry;
GtkWidget *grid;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
grid = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), grid);
button = gtk_button_new_with_label ("Print Text");
g_signal_connect (button, "clicked", G_CALLBACK (open_dialog), window);
gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 1, 1);
gtk_widget_show_all (window);
}
int main (int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", 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;
}
The program should have printed the value of the entry box which in the dialog after closing the dialog. Note: in the main window there is a button that opens the dialog.
Thanks for helping!
Problem is on the print_text function. The prototype for GtkDialog response signal is:
void user_function (GtkDialog *dialog,
gint response_id,
gpointer user_data)
So you must change your function to:
static void print_text (GtkWidget *widget, gint response_id, gpointer data)
{
GtkEntry* entry = data;
printf("%s", gtk_entry_get_text(entry));
gtk_widget_destroy (widget); // This will close the dialog
}

Make the menu items of menu created, functionable using GTK+

Here in this code I have created a menu bar and added menus and menu items to it...
But i dont know how to make the created menu items functionable... for eg: I want the 'Open' menu item of 'File' in the menu bar, open a file and display it...
I'm making a project on a Text Editor using GTK+ 3
#include <gtk/gtk.h>
GtkWidget *window;
GdkPixbuf *icon;
GtkWidget *vbox;
GtkWidget *menubar;
GtkWidget *fileMenu;
GtkWidget *fileMi;
GtkWidget *editMenu;
GtkWidget *editMi;
GtkWidget *searchMenu;
GtkWidget *searchMi;
GtkWidget *newMi;
GtkWidget *openMi;
GtkWidget *saveMi;
GtkWidget *saveasMi;
GtkWidget *quitMi;
GtkWidget *undoMi;
GtkWidget *redoMi;
GtkWidget *cutMi;
GtkWidget *copyMi;
GtkWidget *pasteMi;
GtkWidget *fontMi;
GtkWidget *findMi;
GtkWidget *replaceMi;
GtkWidget *text_view;
GtkTextBuffer *buffer;
void txt(){
text_view = gtk_text_view_new ();
gtk_box_pack_start (GTK_BOX (vbox), text_view, TRUE, TRUE, 0);
}
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;
}
static void fileOpen(GtkWidget *load, gpointer window)
{
GtkWidget *choose;
GtkFileChooserAction action= GTK_FILE_CHOOSER_ACTION_OPEN;
choose = gtk_file_chooser_dialog_new("Choose a file to open", GTK_WINDOW(window), GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_OK, GTK_RESPONSE_OK, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
if (gtk_dialog_run(GTK_DIALOG(choose)) == GTK_RESPONSE_ACCEPT)
{
char *path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(choose));
gtk_text_buffer_set_text(buffer, path, -1);
}
gtk_widget_destroy(choose);
}
static void fileSave(GtkWidget *save, gpointer window)
{
GtkWidget *saved;
saved = gtk_file_chooser_dialog_new("Choose a file to open", GTK_WINDOW(window), GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_OK, GTK_RESPONSE_OK, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
gtk_widget_show_all(saved);
gint resp = gtk_dialog_run(GTK_DIALOG(saved));
gtk_widget_destroy(saved);
}
int main (int argc, char *argv[])
{
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
icon = create_pixbuf ("Web.png");
gtk_window_set_title (GTK_WINDOW (window), "Write Pad");
gtk_window_set_default_size (GTK_WINDOW (window), 500, 400);
gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
gtk_window_set_icon (GTK_WINDOW (window), icon);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add (GTK_CONTAINER (window), vbox);
menubar = gtk_menu_bar_new ();
fileMenu = gtk_menu_new ();
fileMi = gtk_menu_item_new_with_label ("File");
newMi = gtk_menu_item_new_with_label ("New");
openMi = gtk_menu_item_new_with_label ("Open");
saveMi = gtk_menu_item_new_with_label ("Save");
saveasMi = gtk_menu_item_new_with_label ("Save As");
quitMi = gtk_menu_item_new_with_label ("Quit");
menubar = gtk_menu_bar_new ();
editMenu = gtk_menu_new ();
editMi = gtk_menu_item_new_with_label ("Edit");
undoMi = gtk_menu_item_new_with_label ("Undo");
redoMi = gtk_menu_item_new_with_label ("Redo");
cutMi = gtk_menu_item_new_with_label ("Cut");
copyMi = gtk_menu_item_new_with_label ("Copy");
pasteMi = gtk_menu_item_new_with_label ("Paste");
fontMi = gtk_menu_item_new_with_label ("Font");
menubar = gtk_menu_bar_new ();
searchMenu = gtk_menu_new ();
searchMi = gtk_menu_item_new_with_label ("Search");
findMi = gtk_menu_item_new_with_label ("Find");
replaceMi = gtk_menu_item_new_with_label ("Replace");
gtk_menu_item_set_submenu (GTK_MENU_ITEM (fileMi), fileMenu);
gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), newMi);
gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), openMi);
gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), saveMi);
gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), saveasMi);
gtk_menu_shell_append (GTK_MENU_SHELL (fileMenu), quitMi);
gtk_menu_shell_append (GTK_MENU_SHELL (menubar), fileMi);
gtk_menu_item_set_submenu (GTK_MENU_ITEM (editMi), editMenu);
gtk_menu_shell_append(GTK_MENU_SHELL(editMenu), undoMi);
gtk_menu_shell_append(GTK_MENU_SHELL(editMenu), redoMi);
gtk_menu_shell_append(GTK_MENU_SHELL(editMenu), cutMi);
gtk_menu_shell_append(GTK_MENU_SHELL(editMenu), copyMi);
gtk_menu_shell_append (GTK_MENU_SHELL (editMenu), pasteMi);
gtk_menu_shell_append (GTK_MENU_SHELL (editMenu), fontMi);
gtk_menu_shell_append (GTK_MENU_SHELL (menubar), editMi);
gtk_menu_item_set_submenu (GTK_MENU_ITEM (searchMi), searchMenu);
gtk_menu_shell_append(GTK_MENU_SHELL(searchMenu), findMi);
gtk_menu_shell_append(GTK_MENU_SHELL(searchMenu), replaceMi);
gtk_box_pack_start (GTK_BOX (vbox), menubar, FALSE, FALSE, 0);
txt();
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect (G_OBJECT (quitMi), "activate", G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect (G_OBJECT (newMi), "activate", G_CALLBACK (main), NULL);
g_signal_connect(G_OBJECT (openMi), "activate", G_CALLBACK(fileOpen), NULL);
g_signal_connect(G_OBJECT (saveMi), "activate", G_CALLBACK(fileSave), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
Basically, the problem you have is caused by the fact that all of your widgets are global variables, but your fileOpen function takes a gpointer window argument:
//global
GtkWidget *window;
//gpointer window argument -> local variable, not global
static void fileOpen(GtkWidget *load, gpointer window)
{
GtkWidget *choose;
GtkFileChooserAction action= GTK_FILE_CHOOSER_ACTION_OPEN;
choose = gtk_file_chooser_dialog_new(
"Choose a file to open",
GTK_WINDOW(window),//window is not global, but function argument here !
GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_OK, GTK_RESPONSE_OK, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL
);
if (gtk_dialog_run(GTK_DIALOG(choose)) == GTK_RESPONSE_ACCEPT)
{
char *path = gtk_file_chooser_get_filename(
GTK_FILE_CHOOSER(choose)
);
gtk_text_buffer_set_text(buffer, path, -1);
}
gtk_widget_destroy(choose);
}
A quick fix would be to rename the argument to something like gp_data or something. A better fix would be to not use a global variable, and instead rely on the gpointer to pass on the objects you need:
g_signal_connect(
G_OBJECT (openMi),
"activate",
G_CALLBACK(fileOpen),
(gpointer)text_view // text_view and buffer needn't be global now
);
And then use the gpointer argument to get the window and anything else you might need. You could also use a custom struct or array of widgets to pass to the callback in case you need more stuff to work with, but in this case, you should be OK with just passing the text_view widget, and changing the callback to something like this:
static void fileOpen(GtkWidget *load, gpointer widget)
{
//get the buffer
GtkTextBuffer *buffer = gtk_text_view_get_buffer(
GTK_TEXT_VIEW (widget)
);
//get window from text_View widget passed to callback
//so window doesn't have to be global either...
GtkWidget *window = gtk_widget_get_toplevel(
widget
), *choose;
GtkFileChooserAction action= GTK_FILE_CHOOSER_ACTION_OPEN;
choose = gtk_file_chooser_dialog_new(
"Choose a file to open",
GTK_WINDOW(window),
GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_OK, GTK_RESPONSE_OK, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL
);
if (gtk_dialog_run(GTK_DIALOG(choose)) == GTK_RESPONSE_ACCEPT)
{
char *path = gtk_file_chooser_get_filename(
GTK_FILE_CHOOSER(choose)
);
gtk_text_buffer_set_text(buffer, path, -1);
}
gtk_widget_destroy(choose);
}
Note that I haven't tested this code, but this should be enough to get you started. You'll have to change the code you have a bit, but you should be able to get rid of all of those global variables simply by passing the right widget to the callbacks.
As always: Read the documentation, just adding the link to the answer for future reference

Menu doesn't display while creating it in GTK+ 3 C

Screenshot of the output
i am new with gtk+. while learning to create menus in gtk+ i simply copy paste the following code and when i run it....i don't know what is the problem but only it shows window. it does not show any menus or menu bar... help me if anyone has been through this problem...
#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;
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *icon;
GtkWidget *vbox;
GtkWidget *menubar;
GtkWidget *fileMenu;
GtkWidget *fileMi;
GtkWidget *quitMi;
GtkWidget *text_view;
GtkWidget *buffer;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Write Pad");
gtk_window_set_default_size(GTK_WINDOW(window), 500, 400);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
icon = create_pixbuf("Web.png");
gtk_window_set_icon(GTK_WINDOW(window), icon);
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
text_view = gtk_text_view_new ();
gtk_box_pack_start (GTK_BOX (vbox), text_view, 1, 1, 0);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
menubar = gtk_menu_bar_new();
fileMenu = gtk_menu_new();
fileMi = gtk_menu_item_new_with_label("File");
quitMi = gtk_menu_item_new_with_label("Quit");
gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileMi), fileMenu);
gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), quitMi);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), fileMi);
gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(quitMi), "activate", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
There are a few problems with the code. All of the variables you declare use the GtkWidget * type, while the functions create_pixbuf and gtk_text_view_get_buffer return data of the GdkPixbuf * and GtkTextBuffer * types. You also use a deprecated function.
Moreover, you call the menu related functions after the GtkTextView related functions, thereby placing the menu at the bottom of the window.
Here is the working code:
#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;
}
int
main (int argc, char *argv[])
{
GtkWidget *window;
GdkPixbuf *icon;
GtkWidget *vbox;
GtkWidget *menubar;
GtkWidget *fileMenu;
GtkWidget *fileMi;
GtkWidget *quitMi;
GtkWidget *text_view;
GtkTextBuffer *buffer;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
icon = create_pixbuf ("Web.png");
gtk_window_set_title (GTK_WINDOW (window), "Write Pad");
gtk_window_set_default_size (GTK_WINDOW (window), 500, 400);
gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
gtk_window_set_icon (GTK_WINDOW (window), icon);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add (GTK_CONTAINER (window), vbox);
menubar = gtk_menu_bar_new ();
fileMenu = gtk_menu_new ();
fileMi = gtk_menu_item_new_with_label ("File");
quitMi = gtk_menu_item_new_with_label ("Quit");
gtk_menu_item_set_submenu (GTK_MENU_ITEM (fileMi), fileMenu);
gtk_menu_shell_append (GTK_MENU_SHELL (fileMenu), quitMi);
gtk_menu_shell_append (GTK_MENU_SHELL (menubar), fileMi);
gtk_box_pack_start (GTK_BOX (vbox), menubar, FALSE, FALSE, 0);
text_view = gtk_text_view_new ();
gtk_box_pack_start (GTK_BOX (vbox), text_view, TRUE, TRUE, 0);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect (G_OBJECT (quitMi), "activate", G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}

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