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);
}
Related
I'm building a project like uLauncher where I can pop out a window and focus on the textbox right away. I was able to get the window out using libkeybinder for the shortcut and get the window out with win->show(), but the problem is that the keyboard cursor is in the window behind the window.
How do I get the focus to be on the textbox window?
This is the main code:
void shortcutHandler(const char *keystring, void *user_data) {
winroot->show();
}
int main(int argc, char *argv[]) {
Gtk::Main kit(argc, argv);
winroot = new MainWindow();
auto app = Gtk::Application::create(GTK_ID);
app->hold();
keybinder_init();
keybinder_bind(KEY_SHORTCUT, shortcutHandler, NULL);
return app->run(*winroot);
}
``
I'd like to have a picture element in my gui with text on it. My goal is to load pictures (for example a waterdrop) and place text on it which stands for a measured humidity (values come from MQTT).
What would be the best way to do this? I don't care if it's a label or any other kind of element (though I'm not happy with misusing a button for that). The text needs to be changable. Im very new to this framework so I didn't get the hang on it yet.
Thank you!
This is an example code. It uses an overlay to stack two widgets, an image and a label:
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *image;
GtkWidget *label;
GtkWidget *overlay;
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_title(GTK_WINDOW(window), "Sandbox");
image = gtk_image_new_from_file("image.png");
label = gtk_label_new("I've always been too lame\n\
To see what's before me\n\
And I know nothing sweeter than\n\
Champaign from last New Years\n\
Sweet music in my ears\n\
And a night full of no fears\n\
\n\
But if I had one wish fulfilled tonight\n\
I'd ask for the sun to never rise\n\
If God passed a mic to me to speak\n\
I'd say \"Stay in bed, world,\n\
Sleep in peace");
overlay = gtk_overlay_new ();
gtk_container_add(GTK_CONTAINER(window), overlay);
gtk_overlay_add_overlay(GTK_OVERLAY(overlay), image);
gtk_overlay_add_overlay(GTK_OVERLAY(overlay), label);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
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);
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.
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.