Reputation: 442
The problem is that program just handles the command line arguments and exits, instead of displaying a GUI too.
For example:
#include <gtk/gtk.h>
static gint print_cmd_arg(GApplication *app, GApplicationCommandLine *app_cmd, int *argc)
{
if (*argc > 1)
{
char **argv = g_application_command_line_get_arguments(app_cmd, argc);
GFile *file = g_file_new_for_commandline_arg(argv[1]);
if (g_file_query_exists(file, NULL))
{
char *text;
g_file_load_contents(file, NULL, &text, NULL, NULL, NULL);
g_print("%s", text);
g_free(text);
g_object_unref(file);
return 0;
}
else
{
g_print("File \'%s\' does not exist.\n", argv[1]);
g_object_unref(file);
return 1;
}
}
return 2;
}
static void activation(GtkApplication *app, gpointer user_data)
{
GtkWidget *window = gtk_application_window_new(app);
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
gtk_window_set_title(GTK_WINDOW(window), "Test");
g_signal_connect(window, "delete-event", G_CALLBACK(gtk_widget_destroy), NULL);
GtkWidget *button = gtk_button_new_with_label("Quit");
g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), window);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_show_all(window);
}
int main(int argc, char **argv)
{
int status;
GtkApplication *test = gtk_application_new("this.is.only.a.test", G_APPLICATION_NON_UNIQUE | G_APPLICATION_HANDLES_COMMAND_LINE);
g_signal_connect(test, "activate", G_CALLBACK(activation), NULL);
g_signal_connect(G_APPLICATION(test), "command-line", G_CALLBACK(print_cmd_arg), &argc);
status = g_application_run(G_APPLICATION(test), argc, argv);
return status;
}
Try running this, you'll see that this program completely ignores function activation
.
What I want this program to do is to both handle command line arguments and display GUI.
Also, I know I'm supposed to use g_application_command_line_set_exit_status() in print_cmd_arg
instead of return
s, but I don't know how to do this and get compiler warnings.
Upvotes: 3
Views: 781
Reputation: 4114
Handling command line is a complex subject in the GtkApplication/GApplication context due to added flexibility.
If you are "overriding" the command line handling/parsing then a variety of methods to do so will be available.
Information on this subject is somewhat scattered in the documentation. Some of it is here:
From 1) you can read:
Dealing with the command line
Normally, GtkApplication will assume that arguments passed on the command line are files to be opened. If no arguments are passed, then it assumes that an application is being launched to show its main window or an empty document. In the case that files were given, you will receive these files (in the form of GFile) from the open signal. Otherwise you will receive an activate signal. It is recommended that new applications make use of this default handling of command line arguments.
If you want to deal with command line arguments in more advanced ways, there are several (complementary) mechanisms by which you can do this.
And an important bit is:
Note in particular that it is possible to use action activations instead of activate or open. It is perfectly reasonable that an application could start without an activate signal ever being emitted. activate is only supposed to be the default "started with no options" signal. Actions are meant to be used for anything else.
So, there are various ways to handle arguments. The questions goes to how and what are you trying to do. If the arguments are files then maybe handle them with the open
signal and avoid handling the command line.
To solve the actual problem, as you may conclude, activate won't be fired but you can do it by using g_application_activate
in your command line callback, e.g.:
static gint print_cmd_arg(GApplication *app, GApplicationCommandLine *app_cmd, int *argc)
{
/* ADD THIS TO YOUR CODE */
g_application_activate(app);
/* ENDS HERE */
if (*argc > 1)
{
char **argv = g_application_command_line_get_arguments(app_cmd, argc);
GFile *file = g_file_new_for_commandline_arg(argv[1]);
if (g_file_query_exists(file, NULL))
{
char *text;
g_file_load_contents(file, NULL, &text, NULL, NULL, NULL);
g_print("%s", text);
g_free(text);
g_object_unref(file);
return 0;
}
else
{
g_print("File \'%s\' does not exist.\n", argv[1]);
g_object_unref(file);
return 1;
}
}
return 2;
}
Upvotes: 2