Bumbieris112
Bumbieris112

Reputation: 121

GTK3 C Menu button with Popover menu sample code (ONLY using .c, not .ui)

How I can get working simple gtk_menu_button_new() with gtk_popover_menu_new()? When I tried that using Glade, it worked flawlessly, however, I need .c code, not .ui code. I can't find single sample code on the internet that uses only .c for Popovermenu

Can someone provide very simple example code where popover menu contains few buttons? Preferably, where window design code is written in int main, not static void activate.

There will be .gif that will show what I want to make

P.S. I am aware, that in order to fit multiple buttons inside, I need to use GTK_BOX.

EDIT: I have added sample code, which contains Menu button, but it doesn't work. Please, finish this code to work like in .gif. Compilation: gcc pkg-config --cflags gtk+-3.0 -o './testprogram.run' './testprogramcode.c' pkg-config --libs gtk+-3.0

Edit 2: I added one button to testBox

Code:

#include <gtk/gtk.h>

int main(int argc, char *argv[]) 
{

    GtkWidget *window;


    gtk_init(&argc, &argv);
    //Create window
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
    gtk_window_set_title(GTK_WINDOW(window), "Simple menu");


    //Creating fixed container
    GtkWidget *fixedContainer = gtk_fixed_new ();
    gtk_container_add(GTK_CONTAINER(window), fixedContainer);

    //Creating and adding new menu button in fixed container
    GtkWidget *testMenuButton = gtk_menu_button_new ();
    gtk_fixed_put (GTK_FIXED(fixedContainer), testMenuButton, 50, 50);

    //Adding popover to menu button
    GtkWidget *testPopOver = gtk_popover_new (testMenuButton);

    //Creating GTK_BOX to hold popover box contents
    GtkWidget *testBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);

    //Creating and adding new button in testBox
    GtkWidget *testButton = gtk_button_new ();
    gtk_box_pack_start(GTK_BOX(testBox), testButton, TRUE, TRUE, 5);

    //How to add testBox to testPopOver?









    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

Upvotes: 2

Views: 2024

Answers (1)

Alexander Dmitriev
Alexander Dmitriev

Reputation: 2525

You can try this snippet:

  GtkWidget *mb = gtk_menu_button_new ();
  GtkWidget *po = gtk_popover_new (mb);
  GtkWidget *lb = gtk_label_new ("hello");

  gtk_container_add (po, lb);
  g_object_set (mb, "margin", 150, NULL);
  gtk_menu_button_set_popover (mb, po);

However, avoiding .ui is being penny-wise. You'd better avoid premature optimisations.

Upvotes: 1

Related Questions