i have a simple program which is compiled with gtk2.0 in ubuntu.In ubuntu11.04 i installed gtk3.then i compile the same code,i got an error in the following line
/* Add a timer callback to update the value of the progress bar */
timer = gtk_timeout_add (100, progress_timeout, pdata);
i just comment the line and recompile it.then i got output file.but it is not properly working without the commented line.
in gtk2.0 i compiled by the following command
gcc progressbar.c `pkg-config --cflags --libs gtk+-2.0`
and in gtk3
gcc progressbar.c `pkg-config --cflags --libs gtk+-3.0`
I have a doubt that,is there any deprecation in that method in gtk3.please give me a link to an easy documentation with examples.what are the main difference between 2 and 3.
The full source code is as shown below
#include <gtk/gtk.h>
typedef struct _ProgressData {
GtkWidget *pbar;
} ProgressData;
gint progress_timeout( gpointer data )
{
ProgressData *pdata = (ProgressData *)data;
gdouble new_val;
new_val = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (pdata->pbar)) + 0.01;
if (new_val > 1.0)
new_val = 0.0;
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pdata->pbar), new_val);
return TRUE;
}
int main( int argc,
char *argv[])
{
ProgressData *pdata;
GtkWidget *align;
GtkWidget *window;
int timer;
GtkWidget *vbox;
gtk_init (&argc, &argv);
/* Allocate memory for the data that is passed to the callbacks */
pdata = g_malloc (sizeof (ProgressData));
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
g_signal_connect ( window, "destroy", gtk_main_quit, NULL ) ;
gtk_window_set_title (GTK_WINDOW (window), "GtkProgressBar");
gtk_container_set_border_width (GTK_CONTAINER (window), 0);
vbox = gtk_vbox_new (FALSE, 5);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show (vbox);
/* Create a centering alignment object */
align = gtk_alignment_new (0.5, 0.5, 0, 0);
gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 5);
gtk_widget_show (align);
/* Create the GtkProgressBar */
pdata->pbar = gtk_progress_bar_new ();
gtk_container_add (GTK_CONTAINER (align), pdata->pbar);
gtk_widget_show (pdata->pbar);
/* Add a timer callback to update the value of the progress bar */
timer = gtk_timeout_add (100, progress_timeout, pdata);
gtk_widget_show (window);
gtk_main ();
return 0;
}
You need to change the gtk_timeout_add call to g_timeout_add.
gtk_timeout_add ()
guint gtk_timeout_add (guint32 interval,
GtkFunction function,
gpointer data);
Warning
gtk_timeout_add has been deprecated since version 2.4 and should not be used in
newly-written code. Use g_timeout_add() instead.
Google "gtk_timeout_add g_timeout_add" will get you examples, e.g. this one, http://gna.org/patch/?2563.
As jesse told you, you're using gtk_timeout_add, which has been deprecated in GTK2. All the symbols deprecated in GTK2 were removed in GTK3.
To have your program work in GTK3, you need to make sure you don't use any GTK2-deprecated symbols. For this, use symbols like G_DISABLE_DEPRECATED (for GLib), GTK_DISABLE_DEPRECATED, etc. that can help you make sure when you compile with GTK2 that you're not using a symbol that was removed in GTK3.
There is also a GTK2 to GTK3 migration guide that you can use, as well as some GNOME Goals that link to the patches that were used in GNOME to accomplish that same task, for GLib and GTK symbols.
Related
My ultimate goal is to compile and build a GTK application for Windows using only the C programming language
I'm using a Macbook Pro with the M1 chip. So instead of developing on Mac and cross compiling for windows, I decided to try and develop on Windows for windows.
I'm using parallels to run a Windows 10 virtual machine and after a lot of trial and error I was finally able to compile and execute a GTK program using msys2.
There is just one problem: No keyboard functionality in the GTK application. With the error message in the command prompt: "Failed to load keyboard layout DLL for layout A0000409: C:\WINDOWS\system32\KbdPrlUS.dll"
I solved my previous DLL issue by adding a path to the system variables. How might I approach addressing this keyboard layout DLL issue? The file definitely already exists at that location.
EDIT Here is the code that compiles (and works) but doesn't register and letter keypress from the keyboard. The same code works on Mac OS just fine.
#include <gtk/gtk.h>
static GtkTextBuffer *tb = NULL;
static GtkTextBuffer *tb2 = NULL;
static void
quit_activated(GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
GApplication *app = G_APPLICATION (user_data);
g_application_quit (app);
}
static void
click1_cb (GtkButton *btn,
gpointer user_data)
{
GtkTextIter start, end;
gtk_text_buffer_get_bounds(tb, &start, &end);
const GtkTextIter* start2 = &start;
const GtkTextIter* end2 = &end;
char* text = gtk_text_buffer_get_text(tb,start2,end2,false);
gtk_text_buffer_set_text (tb2, text, -1);
}
static void
app_activate (GApplication *app,
gpointer user_data)
{
GtkWidget *win;
GtkWidget *tv;
GtkWidget *tv2;
GtkWidget *box;
GtkWidget *btn;
GtkWidget *btn2;
gchar *text;
text =
"Input some text "
"As many lines as you want\n"
"One day, he went into a mountain and found a shining bamboo. "
"\"What a mysterious bamboo it is!,\" he said. "
"He cut it, then there was a small cute baby girl in it. "
"The girl was shining faintly. "
"He thought this baby girl is a gift from Heaven and took her home.\n"
"His wife was surprized at his tale. "
"They were very happy because they had no children. "
;
win = gtk_application_window_new (GTK_APPLICATION (app));
gtk_window_set_title (GTK_WINDOW (win), "Taketori");
gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
gtk_box_set_homogeneous (GTK_BOX (box), TRUE);
gtk_window_set_child (GTK_WINDOW (win), box);
btn = gtk_button_new_with_label ("Execute.");
g_signal_connect (btn, "clicked", G_CALLBACK (click1_cb), NULL);
tv = gtk_text_view_new ();
tv2 = gtk_text_view_new ();
tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
tb2 = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv2));
gtk_text_buffer_set_text (tb2, "", -1);
gtk_text_buffer_set_text (tb, text, -1);
gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR);
gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv2), GTK_WRAP_WORD_CHAR);
gtk_box_append (GTK_BOX (box),tv);
gtk_box_append (GTK_BOX (box), tv2);
gtk_box_append (GTK_BOX (box), btn);
/*
GSimpleAction *act_quit = g_simple_action_new ("quit", NULL);
g_action_map_add_action (G_ACTION_MAP (app), G_ACTION(act_quit));
g_signal_connect (act_quit,"activate",G_CALLBACK (quit_activated),app);
GMenu *menubar = g_menu_new();
GMenuItem *menu_item_menu = g_menu_item_new ("Menu",NULL);
GMenu *menu = g_menu_new();
GMenuItem *menu_item_quit = g_menu_item_new ("Quit","app.quit");
g_menu_append_item (menu, menu_item_quit);
g_object_unref (menu_item_quit);
g_menu_set_submenu (menu_item_menu,G_MENU_MODEL(menu));
g_menu_append_item (menubar,menu_item_menu);
g_object_unref(menu_item_menu);*/
//gtk_application_set_menubar (GTK_APPLICATION (app), G_MENU_MODEL (menubar));
//gtk_application_window_set_show_menubar (GTK_APPLICATION_WINDOW (win), TRUE);
gtk_widget_show (win);
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int stat;
app = gtk_application_new ("com.gitbut.fff", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (app_activate), NULL);
stat = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return stat;
}
So I found some tutorials on how to configure gtk to dev c++. I followed that tutorial very closely but when i tried to run this:
#include <gtk/gtk.h>
static void hello( GtkWidget *widget,
gpointer data )
{
g_print ("Hello World\n");
}
static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
g_print ("delete event occurred\n");
return TRUE;
}
static void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *button;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "delete-event",
G_CALLBACK (delete_event), NULL);
g_signal_connect (window, "destroy",
G_CALLBACK (destroy), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
button = gtk_button_new_with_label ("Hello World");
g_signal_connect (button, "clicked",
G_CALLBACK (hello), NULL);
g_signal_connect_swapped (button, "clicked",
G_CALLBACK (gtk_widget_destroy),
window);
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main ();
return 0;
}
It produces linker error like this:
[Linker error] undefined reference to g_print
And many more of this type.
How can I fix this?
I have installed last all-in-one bundle GTK+ for windows 32 bit.
I have a problem with function gtk_label_set_text: it leaks memory when it is called recursively
There is an example code below. It leaks memory about 1Mb every 20 seconds
#include <gtk/gtk.h>
gboolean update_label(gpointer);
int main(int argc, char ** argv)
{
GtkWidget *window;
GtkWidget *label = NULL;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
label = gtk_label_new(NULL);
gtk_container_add(GTK_CONTAINER(window),label);
gtk_widget_show_all(window);
g_timeout_add(10,(GtkFunction)update_label,label);
gtk_main();
return 0;
}
gboolean update_label(gpointer data)
{
GtkWidget *label = data;
gchar tmpbuf[100];
sprintf(tmpbuf , "Random text %i\n",rand());
gtk_label_set_text(GTK_LABEL(label),tmpbuf);
return TRUE;
}
The code creates a windows with label and updates it every 10 ms.
Can someone help me? Is there something wrong in GTK+ library or in my code?
Thanks
This is most probably a duplicate Memory leak in GTK under Windows 7 in gtk_widget_queue_draw. What is the version of GTK you use ?
I found out that Color Selector from the GTK+ Code Demos is a quite nice color chooser. Thus, I would like to use it as a standalone application without having to run gtk-demos every time.
I tried to compile the example code which is:
#include <gtk/gtk.h>
static GtkWidget *window = NULL;
static GtkWidget *da;
static GdkColor color;
static GtkWidget *frame;
/* Expose callback for the drawing area
*/
static gboolean
expose_event_callback (GtkWidget *widget,
GdkEventExpose *event,
gpointer data)
{
GdkWindow *window = gtk_widget_get_window (widget);
if (window)
{
GtkStyle *style;
cairo_t *cr;
style = gtk_widget_get_style (widget);
cr = gdk_cairo_create (window);
gdk_cairo_set_source_color (cr, &style->bg[GTK_STATE_NORMAL]);
gdk_cairo_rectangle (cr, &event->area);
cairo_fill (cr);
cairo_destroy (cr);
}
return TRUE;
}
static void
change_color_callback (GtkWidget *button,
gpointer data)
{
GtkWidget *dialog;
GtkColorSelection *colorsel;
gint response;
dialog = gtk_color_selection_dialog_new ("Changing color");
gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (window));
colorsel =
GTK_COLOR_SELECTION (gtk_color_selection_dialog_get_color_selection (GTK_COLOR_SELECTION_DIALOG (dialog)));
gtk_color_selection_set_previous_color (colorsel, &color);
gtk_color_selection_set_current_color (colorsel, &color);
gtk_color_selection_set_has_palette (colorsel, TRUE);
response = gtk_dialog_run (GTK_DIALOG (dialog));
if (response == GTK_RESPONSE_OK)
{
gtk_color_selection_get_current_color (colorsel,
&color);
gtk_widget_modify_bg (da, GTK_STATE_NORMAL, &color);
}
gtk_widget_destroy (dialog);
}
GtkWidget *
do_colorsel (GtkWidget *do_widget)
{
GtkWidget *vbox;
GtkWidget *button;
GtkWidget *alignment;
if (!window)
{
color.red = 0;
color.blue = 65535;
color.green = 0;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_screen (GTK_WINDOW (window),
gtk_widget_get_screen (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "Color Selection");
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window);
gtk_container_set_border_width (GTK_CONTAINER (window), 8);
vbox = gtk_vbox_new (FALSE, 8);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
gtk_container_add (GTK_CONTAINER (window), vbox);
/*
* Create the color swatch area
*/
frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
da = gtk_drawing_area_new ();
g_signal_connect (da, "expose_event",
G_CALLBACK (expose_event_callback), NULL);
/* set a minimum size */
gtk_widget_set_size_request (da, 200, 200);
/* set the color */
gtk_widget_modify_bg (da, GTK_STATE_NORMAL, &color);
gtk_container_add (GTK_CONTAINER (frame), da);
alignment = gtk_alignment_new (1.0, 0.5, 0.0, 0.0);
button = gtk_button_new_with_mnemonic ("_Change the above color");
gtk_container_add (GTK_CONTAINER (alignment), button);
gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
g_signal_connect (button, "clicked",
G_CALLBACK (change_color_callback), NULL);
}
if (!gtk_widget_get_visible (window))
{
gtk_widget_show_all (window);
}
else
{
gtk_widget_destroy (window);
window = NULL;
}
return window;
}
The error is:
[orschiro#thinkpad ~]$ gcc `pkg-config --cflags --libs gtk+-3.0` -o colorSelector colorSelector.ccolorSelector.c: In function ‘expose_event_callback’:
colorSelector.c:26:7: warning: ‘gdk_cairo_set_source_color’ is deprecated (declared at /usr/include/gtk-3.0/gdk/gdkcairo.h:58): Use 'gdk_cairo_set_source_rgba' instead [-Wdeprecated-declarations]
colorSelector.c: In function ‘change_color_callback’:
colorSelector.c:44:3: warning: ‘gtk_color_selection_dialog_new’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorseldialog.h:73): Use 'gtk_color_chooser_dialog_new' instead [-Wdeprecated-declarations]
colorSelector.c:49:5: warning: ‘gtk_color_selection_dialog_get_color_selection’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorseldialog.h:75): Use 'GtkColorChooser' instead [-Wdeprecated-declarations]
colorSelector.c:51:3: warning: ‘gtk_color_selection_set_previous_color’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorsel.h:148) [-Wdeprecated-declarations]
colorSelector.c:52:3: warning: ‘gtk_color_selection_set_current_color’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorsel.h:142): Use 'gtk_color_chooser_set_rgba' instead [-Wdeprecated-declarations]
colorSelector.c:53:3: warning: ‘gtk_color_selection_set_has_palette’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorsel.h:100) [-Wdeprecated-declarations]
colorSelector.c:59:7: warning: ‘gtk_color_selection_get_current_color’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkcolorsel.h:145): Use 'gtk_color_chooser_get_rgba' instead [-Wdeprecated-declarations]
colorSelector.c: In function ‘do_colorsel’:
colorSelector.c:91:7: warning: ‘gtk_vbox_new’ is deprecated (declared at /usr/include/gtk-3.0/gtk/deprecated/gtkvbox.h:60): Use 'gtk_box_new' instead [-Wdeprecated-declarations]
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Installed packages:
gtk2 2.24.14-1
gtk3 3.6.2-1
gcc-libs-multilib 4.7.2-2
gcc-multilib 4.7.2-2
Could someone please explain me the error?
Your source code is incomplete, the main function is missing. gtk-demo is organised so it has a single main that calls some submodules and processes them the same way, and you're missing that main.
The other warning come from the fact that this example code you're trying to compile was done for GTK 2, and you're trying to build it against GTK 3.
You'll find all the missing parts in the gtk-demo directory of the GTK+ repository.
int main( int argc,
char *argv[] )
{
GtkWidget *window;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_show (window);
gtk_main ();
return 0;
}
The above is just a empty winform,I want to output dynamic information in it(not editable),
how should I do that?
You need a GtkTextView which you can set to be not editable. I suggest you look at this excellent GTK tutorial which explains what widgets are available in GTK and how to put them together, accompanied by lots of example code.