0

I have programed the GUI in C++.

#include <gtk/gtk.h>
#include <unistd.h>
#include <cstddef>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
int main(){
    GtkWidget *_LinuxWindow, *_Box, *_Button;
    int argC = 0;
    char** argV;
    // Setup the window and fixed grid
    gtk_init(&argC,&argV);
    _LinuxWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    _Box = gtk_fixed_new();
    // Set the title
    gtk_window_set_title(GTK_WINDOW(_LinuxWindow),"Title");
    // Finish up
    gtk_widget_show(_LinuxWindow);
    g_signal_connect(G_OBJECT(_LinuxWindow),"destroy",G_CALLBACK(gtk_main_quit), NULL);
    // Add controls
    _Button = gtk_button_new_with_label("Click Me!");
    gtk_fixed_put(GTK_FIXED(_Box),_Button,20,20);
    gtk_fixed_move(GTK_FIXED(_Box),_Button,20,20);
    gtk_widget_show(_Box);
    gtk_widget_set_size_request(_Button,30,100);
    // Create a dialog
    GtkWidget *dialog;
    dialog =  gtk_message_dialog_new(GTK_WINDOW(_LinuxWindow), GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_INFO,GTK_BUTTONS_OK_CANCEL,"OK or Cancel?",NULL,g_strerror(errno));
    gint ret = gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(GTK_WIDGET(dialog));
    printf("%i", ret);
    // Add the fixed grid and go the to the main window loop
    gtk_container_add(GTK_CONTAINER(_LinuxWindow),_Box);
    gtk_widget_show(_Box);
    gtk_widget_show_all(_LinuxWindow);
    gtk_main();


    return 0;
}

When I run it using

g++ -o out-withoutCross without-cross.cpp -lX11 `pkg-config --cflags gtk+-3.0`pkg-config --libs gtk+-3.0`

A message box and a window appears. The problem is the controls in the window do not appear while the message box is open. I think this may be because the message box is being ran on the same thread that the main window is. Is there any way to add multi-threading to GTK?

Also the return value of the message box printed to console is running after the main window is exited.

  • Unrelated to your problem, but please note that all symbols beginning with an underscore and followed by an upper-case letter (like e.g. `_LinuxWindow`) are *reserved* for the "implementation" (compiler and standard library). Please don't define your own symbols anywhere. See e.g. [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for more information. – Some programmer dude Apr 23 '20 at 07:47
  • More related to your problem, you show your dialog and let it end before you run the main event loop (`gtk_main()`). Try adding a button in the window, and show the dialog when that button is pressed instead. – Some programmer dude Apr 23 '20 at 07:50

1 Answers1

0

The problem is the structure of your code..\

#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
  GtkWidget *_LinuxWindow, *_Box, *_Button;
  int argC = 0;
  char **argV;
  // Setup the window and fixed grid
  gtk_init(&argC, &argV);
  _LinuxWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  _Box = gtk_fixed_new();
  // Set the title
  gtk_window_set_title(GTK_WINDOW(_LinuxWindow), "Title");
  // Finish up
  gtk_widget_show_all(_LinuxWindow);
  g_signal_connect(G_OBJECT(_LinuxWindow), "destroy", G_CALLBACK(gtk_main_quit),
                   NULL);
  // Add controls
  _Button = gtk_button_new_with_label("Click Me!");
  gtk_fixed_put(GTK_FIXED(_Box), _Button, 20, 20);
  gtk_fixed_move(GTK_FIXED(_Box), _Button, 20, 20);
  gtk_widget_set_size_request(_Button, 30, 100);
  gtk_container_add(GTK_CONTAINER(_LinuxWindow), _Box);
  gtk_widget_show_all(_LinuxWindow);
  // Create a dialog
  GtkWidget *dialog;
  dialog = gtk_message_dialog_new(
      GTK_WINDOW(_LinuxWindow), GTK_DIALOG_DESTROY_WITH_PARENT,
      GTK_MESSAGE_INFO, GTK_BUTTONS_OK_CANCEL, "OK or Cancel?", NULL);
  gint ret = gtk_dialog_run(GTK_DIALOG(dialog));
  gtk_widget_destroy(GTK_WIDGET(dialog));
  printf("%i", ret);
  // Add the fixed grid and go the to the main window loop

  gtk_main();

  return 0;
}

gtk_dialog_run is a blocking function so it will wait until it gets a response from the dialog box.. to proceed further

In your code, you are adding the button to the window only after running the dialog box window (which is a blocking call) so after its execution, the button is being added to the window

Siva Guru
  • 694
  • 4
  • 12