No display for GTK in Visual Studio 2015 - c++

After a couple of rounds of troubleshooting I was able to compile GTK source code in Visual Studio with no errors. I followed a procedure not too dissimilar to 'How to configure gtk on Visual studio 2010'. The code is as follows,
#include <gtk-2.0\gtk\gtk.h>
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
int main(int argc, char* argv[])
{
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_init(&argc, &argv);
gtk_widget_set_usize(window, 300, 200);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_title(GTK_WINDOW(window), "GTK+ with VS2010");
gtk_widget_show(window);
gtk_main();
return 0;
}
However on starting the code no window appears. Visual Studio simply indicates the solution is running but no window appears. Any ideas?

There's problem here:
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_init(&argc, &argv);
You should call gtk_init before creating any windows:
Call this function before using any other GTK+ functions in your GUI
applications. It will initialize everything needed to operate the
toolkit and parses some standard command line options.
Try:
gtk_init(&argc, &argv);
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

Related

GTK Maximizing a Form c++

I am using Ubuntu 20.04 and I have a small GTK 3.0 Form I am trying to get to maximize on first startup. I later want to get this into a Full GLADE project, but first I'm just trying to get this to maximize to full screen.
The commented sections are what I have tried to no avail.
Nothing works and it throws compiler errors, can anyone help me in my endeavor?
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Center");
// gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
// gtk_window_fullscreen(GTK_WINDOW(window);
// gtk_window_fullscreen(GtkWindow *window);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_show(window);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return 0;
}
Thank you.
Use gtk_window_maximize(GtkWindow *window):
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Center");
gtk_window_maximize(GTK_WINDOW(window));
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_show(window);
//...
}
Note that you can call it before showing the window. Doc:
Asks to maximize window , so that it becomes full-screen. Note that you shouldn’t assume the window is definitely maximized afterward, because other entities (e.g. the user or window manager) could unmaximize it again, and not all window managers support maximization. But normally the window will end up maximized. Just don’t write code that crashes if not.
It’s permitted to call this function before showing a window, in which case the window will be maximized when it appears onscreen initially.
You can track maximization via the “window-state-event” signal on GtkWidget, or by listening to notifications on the “is-maximized” property.
Suggestion: Use gtkmm with C++
Example with gtkmm:
#include <gtkmm.h>
int main(int argc, char *argv[])
{
auto app =
Gtk::Application::create(argc, argv,
"org.gtkmm.maximize.base");
Gtk::Window window;
window.maximize();
return app->run(window);
}

How to build a sample project in gtk 3.0

I just started to learn programming in C++ using eclipse cdt and gtk in Ubuntu.
The packages I was able to install and configure are:
Install gtk3.0
install pkg-config from the market place
Added ${PKCFLAGS} in Cross G++ Compiler
Now: ${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}${PKCFLAGS}
Added ${PKCFLAGS} in Cross G++ Linker
Now: ${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}${PKCFLAGS}
I tried to run a sample program:
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
int main (int argc, char *argv[]) {
GtkWidget *window;
gtk_init (&amp;argc, &amp;argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), &quot;Window :-)&quot;);
g_signal_connect (window, &quot;destroy&quot;, G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show (window);
gtk_main ();
return 0;
}
But I am still getting an error "Unresolved inclusion: "
Run in a terminal
pkg-config --cflags gtk+-3.0
and put the output in the "compiler flags" field somewhere in eclipse's settings.
Run
pkg-config --libs gtk+-3.0
and put the output from this command in the "linker flags" field. Don't use cross compiler settings unless you are really cross compiling. This should compile as long as you code is right.
There were two problems,
I needed to add also gtk+2.0 in C/C++ Build => settings
I did not noticed but there are several issues with the code I attached it's
with some strange symbols, I changed it to this:
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
int main (int argc, char *argv[]) {
GtkWidget *window;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Hello world");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show (window);
gtk_main ();
return 0;
}

gtk_window_set_icon_from_file() not working properly

I'm trying to display my icon in my window's title bar. The program compiles and runs fine, but the icon is only being displayed in the launcher.
Here's my main function:
int main(int argc, char * argv[])
{
GtkWidget * window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "My Window");
gtk_window_set_default_size(GTK_WINDOW(window), 600, 600);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_icon_from_file(GTK_WINDOW(window), "img/icon.png", NULL);
gtk_widget_show(window);
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return 0;
}
Thanks in advance for any help.
The documentation for gtk_window_set_icon() and friends says,
This icon is used when the window is minimized (also known as iconified). Some window managers or desktop environments may also place it in the window frame, or display it in other contexts.
So it not required that the icon is displayed in the title bar; that is at the discretion of the window manager in your desktop environment. The default window managers in both Gnome and Unity currently do not do so.

Problem with linked libraries and GTK+

When trying to compile with Code::Blocks the example that comes up with GTK+:
#include stdlib.h
#include gtk/gtk.h
static void helloWorld (GtkWidget *wid, GtkWidget *win)
{
GtkWidget *dialog = NULL;
dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Hello World!");
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
int main (int argc, char *argv[])
{
GtkWidget *button = NULL;
GtkWidget *win = NULL;
GtkWidget *vbox = NULL;
/* Initialize GTK+ */
g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
gtk_init (&argc, &argv);
g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);
/* Create the main window */
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width (GTK_CONTAINER (win), 8);
gtk_window_set_title (GTK_WINDOW (win), "Hello World");
gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
gtk_widget_realize (win);
g_signal_connect (win, "destroy", gtk_main_quit, NULL);
/* Create a vertical box with buttons */
vbox = gtk_vbox_new (TRUE, 6);
gtk_container_add (GTK_CONTAINER (win), vbox);
button = gtk_button_new_from_stock (GTK_STOCK_DIALOG_INFO);
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (helloWorld), (gpointer) win);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
g_signal_connect (button, "clicked", gtk_main_quit, NULL);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
/* Enter the main loop */
gtk_widget_show_all (win);
gtk_main ();
return 0;
}
I receive the following errors:
ld.exe||cannot find -lgobject-2.0|
ld.exe||cannot find -lglib-2.0|
||=== Build finished: 2 errors, 0 warnings ===|
I have linked both to the project, but I can't find a way to make it work. I have tried both the bundled and separated packages from http://www.gtk.org/download-windows.html. I'm pretty sure it must be something simple/stupid but I couldn't find anything that could help me to solve this problem.
Have you added the GTK Directory containing the libglib-2.0.a, libgobject-2.0.a files to the Linker Search Directories.
This can be done from the following Path:
Settings | Compiler and debugger... | Search directories | Linker | Add.
The full command line for the compiler/linker can be viewed in the Build Log window. To enable this go to:
Settings | Compiler and debugger... | Other Settings | Compiler logging | Full command line
I didn't come across the missing entrance point issue so maybe your copy of GTK is corrupted. Download the All-in-one bundle from GTK+ 2.22
make sure you add all the Include Directories in Search directories | Compiler
As a side note you may need to compile your project using -mms-bitfields which can be added in:
Settings | Compiler and debugger... | Other options just paste the flag as is.
When code::blocks is running ld.exe instead of calling it with, for example: -l glib-2.0 it is calling it with -lglib-2.0
Adding a space before the lib's name in code::block's linker options should fix it.

Get text from Textbox in GTK+

I'm trying to get the input text from a text box in a callback function when the user changes something it it (on "changed").
The code goes as follow:
#include <stdio.h>
#include <gtk/gtk.h>
void enter_callback( GtkWidget *widget, GtkEditable *buffer)
{
printf("%s",gtk_editable_get_chars(buffer, 0, -1));
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *text;
GtkWidget *table;
gtk_init (&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
table = gtk_table_new (2, 2, TRUE);
gtk_container_add (GTK_CONTAINER (window), table);
text=gtk_text_new(NULL, NULL);
gtk_text_set_editable(text, TRUE);
gtk_signal_connect(GTK_OBJECT(text), "changed", GTK_SIGNAL_FUNC(enter_callback), (GtkEditable*)text);
gtk_table_attach_defaults(GTK_TABLE(table), text, 0, 1, 0, 1);
gtk_container_border_width (GTK_CONTAINER (window), 40);
gtk_window_set_default_size (GTK_WINDOW(window), 640, 200);
gtk_widget_show(text);
gtk_widget_show(window);
gtk_widget_show(table);
gtk_main();
return 0;
}
The code compiles just right, I'm compiling it on Code::Blocks on debug, checking output on the console by printf. The problem is I get <NULL> as a callback everytime I change something on the textbox. How can I get the correct output?
SOLUTION:
As noted by Washu, gtk_text is deprecated and gtk_text_view should be used instead.
According to the GTK documentation, GtkText is deprecated, buggy, and should not be used. You should instead be using the GtkTextView widget via gtk_text_view_new.
You can use GtkEntry widget too. And use gtk_entry_get_text () (which return const gchar * value) that to get text from GtkEntry, for instance.