Last active
June 30, 2020 00:09
-
-
Save andreldm/83c9b99e7aa133c924fb4165acc8427a to your computer and use it in GitHub Desktop.
GTK Popover sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Build: | |
* gcc $(pkg-config --cflags gtk+-3.0) popover_sample.c -o popover_sample $(pkg-config --libs gtk+-3.0) | |
*/ | |
#include <gtk/gtk.h> | |
int main (int argc, char *argv[]) | |
{ | |
GtkWidget *window; | |
GtkWidget *button, *popover, *vbox; | |
gtk_init (&argc, &argv); | |
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); | |
gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER); | |
gtk_container_set_border_width (GTK_CONTAINER (window), 5); | |
gtk_window_set_default_size (GTK_WINDOW (window), 640, 480); | |
button = gtk_menu_button_new (); | |
gtk_button_set_image (GTK_BUTTON (button), gtk_image_new_from_icon_name ("open-menu-symbolic", GTK_ICON_SIZE_BUTTON)); | |
gtk_widget_set_valign (button, GTK_ALIGN_CENTER); | |
gtk_widget_set_halign (button, GTK_ALIGN_CENTER); | |
gtk_container_add (GTK_CONTAINER (window), button); | |
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); | |
gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new ("Hello World 1"), FALSE, FALSE, 0); | |
gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new ("Hello World 2"), FALSE, FALSE, 0); | |
gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new ("Hello World 3"), FALSE, FALSE, 0); | |
popover = gtk_popover_new (button); | |
gtk_container_add (GTK_CONTAINER (popover), vbox); | |
gtk_menu_button_set_popover (GTK_MENU_BUTTON (button), popover); | |
gtk_widget_show_all (popover); | |
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL); | |
gtk_widget_show_all (window); | |
gtk_main (); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/panel-plugin/sample.c b/panel-plugin/sample.c | |
index 97a0cc6..8927da2 100644 | |
--- a/panel-plugin/sample.c | |
+++ b/panel-plugin/sample.c | |
@@ -135,7 +135,7 @@ sample_new (XfcePanelPlugin *plugin) | |
{ | |
SamplePlugin *sample; | |
GtkOrientation orientation; | |
- GtkWidget *label; | |
+ GtkWidget *button, *popover, *vbox; | |
/* allocate memory for the plugin structure */ | |
sample = g_slice_new0 (SamplePlugin); | |
@@ -158,13 +158,22 @@ sample_new (XfcePanelPlugin *plugin) | |
gtk_container_add (GTK_CONTAINER (sample->ebox), sample->hvbox); | |
/* some sample widgets */ | |
- label = gtk_label_new (_("Sample")); | |
- gtk_widget_show (label); | |
- gtk_box_pack_start (GTK_BOX (sample->hvbox), label, FALSE, FALSE, 0); | |
+ button = gtk_menu_button_new (); | |
+ gtk_button_set_image (GTK_BUTTON (button), gtk_image_new_from_icon_name ("open-menu-symbolic", GTK_ICON_SIZE_BUTTON)); | |
+ gtk_box_pack_start (GTK_BOX (sample->hvbox), button, FALSE, FALSE, 0); | |
+ gtk_widget_show (button); | |
- label = gtk_label_new (_("Plugin")); | |
- gtk_widget_show (label); | |
- gtk_box_pack_start (GTK_BOX (sample->hvbox), label, FALSE, FALSE, 0); | |
+ vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); | |
+ gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new ("Hello World 1"), FALSE, FALSE, 0); | |
+ gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new ("Hello World 2"), FALSE, FALSE, 0); | |
+ gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new ("Hello World 3"), FALSE, FALSE, 0); | |
+ | |
+ popover = gtk_popover_new (button); | |
+ gtk_container_add (GTK_CONTAINER (popover), vbox); | |
+ | |
+ gtk_widget_show_all (popover); | |
+ | |
+ gtk_menu_button_set_popover (GTK_MENU_BUTTON (button), popover); | |
return sample; | |
} |
I still don't understand what you really want to accomplish, seems like you want to replicate Cinnamon menu or Elementary OS' app launcher. If that's the case you can take a look at their code, but I'll be surprised if that's easily supported by GTK, perhaps it's a popover that belongs to the panel itself, not a plugin in its own process.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have an update on popover. I managed to make a popover without the transparency involved.
Still needs a full gtk window behind to draw the popup.
https://www.youtube.com/watch?v=eXZzwDDQlZ8
Now I'm struggling with positioning the popover arrows and a window.
It's harder than I thought. Should have listened to you when you said. :)
I'm using some dummy widgets, 1 pixel transparent png images to set popover pointing to.
I see no other way to "inform" popover where the toggle button is.
I've sent the code to ToZ to have a look here https://forum.xfce.org/viewtopic.php?pid=58793#p58793