Accelerators stop responding when menubar gets hidden - gtk

I have a glade-based UI for gtk3, and I set accelerators fields for several menuitems.
I'm not sure what GtkBuilder does exactly behind the scenes when it's loading the glade file (use a global GtkAccelGroup?), but the end result is, when I hide the menubar, the accelerator shortcuts stop working.
I'm wondering whether there is a way of getting the accelerators working even when the menu is not visible, while still sticking to glade as much as possible.

Maybe you can try to stick the accelerators not to the menu, but one level higher in your application, for example the window? In my own application I do it like this.
accel_group = gtk_accel_group_new ();
gtk_window_add_accel_group (GTK_WINDOW (pad), accel_group);
pad->priv->menu = menu_get_popup_no_highlight (pad, accel_group);
pad->priv->highlight_menu = menu_get_popup_highlight (pad, accel_group);
gtk_accel_group_connect (accel_group, GDK_KEY_Q, GDK_CONTROL_MASK, 0, g_cclosure_new_swap (G_CALLBACK (xpad_app_quit), pad, NULL));
The two menu assignments have their own accelerators which are working even when not visible.
Does this help you?

Here is my solution for xournalpp which walks the menubar and rebinds every accelerator to the main window:
Header
class MainWindow: public GladeGui {
public:
void rebindMenubarAccelerators();
private:
static void rebindAcceleratorsMenuItem(GtkWidget* widget, gpointer user_data);
static void rebindAcceleratorsSubMenu(GtkWidget* widget, gpointer user_data);
static gboolean isKeyForClosure(GtkAccelKey* key, GClosure* closure, gpointer data);
static gboolean invokeMenu(GtkWidget* widget);
GtkAccelGroup* globalAccelGroup;
}
Implementation
gboolean MainWindow::isKeyForClosure(GtkAccelKey* key, GClosure* closure, gpointer data) { return closure == data; }
gboolean MainWindow::invokeMenu(GtkWidget* widget) {
// g_warning("invoke_menu %s", gtk_widget_get_name(widget));
gtk_widget_activate(widget);
return TRUE;
}
void MainWindow::rebindAcceleratorsMenuItem(GtkWidget* widget, gpointer user_data) {
if (GTK_IS_MENU_ITEM(widget)) {
GtkAccelGroup* newAccelGroup = reinterpret_cast<GtkAccelGroup*>(user_data);
GList* menuAccelClosures = gtk_widget_list_accel_closures(widget);
for (GList* l = menuAccelClosures; l != NULL; l = l->next) {
GClosure* closure = reinterpret_cast<GClosure*>(l->data);
GtkAccelGroup* accelGroup = gtk_accel_group_from_accel_closure(closure);
GtkAccelKey* key = gtk_accel_group_find(accelGroup, isKeyForClosure, closure);
// g_warning("Rebind %s : %s", gtk_accelerator_get_label(key->accel_key, key->accel_mods),
// gtk_widget_get_name(widget));
gtk_accel_group_connect(newAccelGroup, key->accel_key, key->accel_mods, GtkAccelFlags(0),
g_cclosure_new_swap(G_CALLBACK(MainWindow::invokeMenu), widget, NULL));
}
MainWindow::rebindAcceleratorsSubMenu(widget, newAccelGroup);
}
}
void MainWindow::rebindAcceleratorsSubMenu(GtkWidget* widget, gpointer user_data) {
if (GTK_IS_MENU_ITEM(widget)) {
GtkMenuItem* menuItem = reinterpret_cast<GtkMenuItem*>(widget);
GtkWidget* subMenu = gtk_menu_item_get_submenu(menuItem);
if (GTK_IS_CONTAINER(subMenu)) {
gtk_container_foreach(reinterpret_cast<GtkContainer*>(subMenu), rebindAcceleratorsMenuItem, user_data);
}
}
}
// When the Menubar is hidden, accelerators no longer work so rebind them to the MainWindow
// It should be called after all plugins have been initialised so that their injected menu items are captured
void MainWindow::rebindMenubarAccelerators() {
this->globalAccelGroup = gtk_accel_group_new();
gtk_window_add_accel_group(GTK_WINDOW(this->getWindow()), this->globalAccelGroup);
GtkMenuBar* menuBar = (GtkMenuBar*)this->get("mainMenubar");
gtk_container_foreach(reinterpret_cast<GtkContainer*>(menuBar), rebindAcceleratorsSubMenu, this->globalAccelGroup);
}

Related

Why GtkWidget is not being found

I have following simple application which creates a window with Label, Entry and Button:
using Gtk;
public static int main(string[] args) {
Gtk.init(ref args);
var mywin = new MyWindow("Entrypad");
mywin.show_all();
Gtk.main();
return 0;
}
public class MyWindow : Window{
public MyWindow(string stitle) {
this.title = stitle;
this.destroy.connect(Gtk.main_quit);
var grid = new Grid();
this.add(grid);
var lab = new Label("Mylabel:");
lab.set_xalign(1);
grid.attach(lab, 0, 0, 1, 1 );
Entry ent = new Entry();
grid.attach(ent, 1, 0, 1, 1 );
var printButton = new Button.with_label("Print Button");
grid.attach(printButton, 0, 1, 1, 1);
printButton.clicked.connect( printButtonFn );
}
}
private void printButtonFn(){
print("In print fn; \n");
}
Above code compiles and works all right. However, I want to show a dialog box, hence I modify printButtonFn and add code taken from here:
private void printButtonFn(){
print("In print fn; \n");
quick_message(this, "Message for dialog box.");
}
// Function to open a dialog box with a message
void quick_message (GtkWindow *parent, gchar *message) {
GtkWidget *dialog, *label, *content_area;
GtkDialogFlags flags;
flags = GTK_DIALOG_DESTROY_WITH_PARENT;
dialog = gtk_dialog_new_with_buttons ("Message",
parent,
flags,
_("_OK"),
GTK_RESPONSE_NONE,
NULL);
content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
label = gtk_label_new (message);
g_signal_connect_swapped (dialog,
"response",
G_CALLBACK (gtk_widget_destroy),
dialog);
gtk_container_add (GTK_CONTAINER (content_area), label);
gtk_widget_show_all (dialog);
}
However, above is giving error:
$ valac --pkg gtk+-3.0 mysrc.vala
rnskeletalGUI_dialog_soques.vala:50.21-50.21: error: syntax error, expected identifier
GtkWidget *dialog, *label, *content_area;
^
Compilation failed: 1 error(s), 0 warning(s)
Where is the problem and how can it be solved?
Two problems.
First, it's Gtk.Widget not GtkWidget. Or, if you have a using Gtk; in that code too, you can just use Widget.
Second, and the one valac is erroring about, is that unlike C, in Vala the pointer goes with the type, not the variable. So if you want three pointers to Gtk.Widget you should write GtkWidget* dialog, label, content_area; not Gtk.Widget *dialog, *label, *content_area;
That said, you shouldn't be using pointers here. Pointers in Vala basically opt you out of the memory management system, and you should almost never use them... think of it a bit like the "unsafe" keyword in some languages.

Type Find Function Not Called in GStreamer Plugin

I have an element that decodes a media type, mytype for example. I want to register the type so that the decodebin element can use my element if needed. I added the code for what I thought would work, but my type_find() function is never called. Any ideas on what I'm doing wrong? Here's what the code looks like:
#define MY_CAPS (gst_static_caps_get(&my_caps))
static GstStaticCaps my_caps = GST_STATIC_CAPS("audio/x-mycaps");
static gchar *my_exts[] = { "mtype", NULL };
static void type_find(GstTypeFind *_type_find, gpointer callback)
{
printf("Type Find Function\r\n");
gst_type_find_suggest(_type_find, GST_TYPE_FIND_POSSIBLE, gst_static_caps_get(&my_caps));
}
gboolean plugin_init(GstPlugin *plugin)
{
if(!gst_type_find_register(plugin, "mytype", GST_RANK_PRIMARY, type_find, my_exts, MY_CAPS, NULL, NULL))
return FALSE;
if(!gst_element_register(plugin, "myelement", GST_RANK_PRIMARY, MY_ELEMENT_TYPE)
return FALSE;
return(TRUE);
}

how to access the text entry in GtkFileChooser

In GTK+, is it possible to access the GtkWidget -- text entry for file name in GtkFileChooser? I want to disable the editable attribute of the text entry using gtk_entry_set_editable.
As far as I know, no.
What do you ultimately want to achieve? Perhaps there is another approach.
If one had a legitimate reason to get a pointer to the GtkEntry, then derive from GtkFileChooserDialog, which will probably mutate into a GtkFileChooserDefault. GObject will complain about an illegal cast when checking type instance even though it works and the data of derived object can be accessed without errors, use GTK_FILE_CHOOSER instead of MY_FILE_CHOOSER to avoid the warning messages and a local static for the entry pointer. The entry widget is NOT accessible during construction. Here is the pertinent code:
static GtkEntry *chooser_entry;
static void my_file_chooser_finalize (GObject *object)
{
chooser_entry = NULL;
(G_OBJECT_CLASS (my_file_chooser_parent_class))->finalize (object);
}
static void my_file_chooser_init (MyFileChooser *self)
{
chooser_entry = NULL;
}
static void look_for_entry(GtkWidget *widget, void *self)
{
if (GTK_IS_ENTRY(widget)) {
chooser_entry = (GtkEntry*)widget;
}
else if (GTK_IS_CONTAINER(widget)) {
gtk_container_forall ( GTK_CONTAINER (widget), look_for_entry, self);
}
}
static void file_chooser_find_entry (GtkWidget *chooser)
{
GList *children, *iter;
/* Get all objects inside the dialog */
children = gtk_container_get_children (GTK_CONTAINER (chooser));
for (iter = children; iter; iter = iter->next) {
if (GTK_IS_CONTAINER(iter->data)) {
gtk_container_forall ( GTK_CONTAINER (iter->data), look_for_entry, chooser);
if (chooser_entry != NULL) {
break;
}
}
}
g_list_free (children);
}
GtkEntry *my_file_chooser_get_entry (GtkWidget *widget)
{
if (chooser_entry == NULL) {
file_chooser_find_entry (widget);
}
return chooser_entry;
}
char *my_file_chooser_get_entry_text(GtkWidget *widget)
{
char *text;
GtkEntry *entry;
text = NULL;
if (GTK_IS_FILE_CHOOSER(widget)) {
entry = my_file_chooser_get_entry(widget);
if (GTK_IS_ENTRY(entry)) {
if (gtk_entry_get_text_length (entry)) {
text = g_strdup (gtk_entry_get_text(entry));
}
}
}
return text;
}
Maybe not ideal, but works.

why doesn't this right-click capture in GWT work in IE?

I'm trying to capture right-clicks on a widget, to popup my own context menu instead of the browser's. There are a couple references on this, but the most popular one here is a little dated, although some of the comments contain more recent code snippets.
I've pieced together bits and I've got it working in Chrome and FF but not IE. In IE it doesn't display the default browser context menu, but it doesn't display my menu. I'm just getting into GWT so I'm assuming I'm not doing something right with the right kinds of handlers or events. I'm also using the gwt-graphics module, that's where the Rectangle class that I'm extending comes from, in case that's relevant.
Here's my code:
public class RectangleRightClickable extends Rectangle {
public RectangleRightClickable(int x, int y, int width, int height) {
super(x, y, width, height);
sinkEvents(Event.ONCONTEXTMENU);
}
public void onBrowserEvent(Event event) {
GWT.log("onBrowserEvent");
event.stopPropagation();
event.preventDefault();
GWT.log("event type : " + DOM.eventGetType(event));
switch(DOM.eventGetType(event)) {
case Event.ONCONTEXTMENU:
if (DOM.eventGetButton(event) == Event.BUTTON_RIGHT) {
GWT.log("Event.BUTTON_RIGHT", null);
showMenu();
}
break;
default:
GWT.log(event.toString());
break;
}
}
protected void showMenu() {
final RectangleRightClickable parent = this;
final PopupMenu popMenu = new PopupMenu();
popMenu.addMenuItem(new Label("Add thing"));
popMenu.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
public void setPosition(int offsetWidth, int offsetHeight) {
int left = parent.getX() + parent.getWidth();
int top = parent.getY() + parent.getWidth();
popMenu.setPopupPosition(left, top);
}
});
}
}
Got this response on the GWT google groups list, which worked:
addDomHandler(new ContextMenuHandler()
{
#Override
public void onContextMenu(ContextMenuEvent event)
{
showMenu();
event.preventDefault();
}
}, ContextMenuEvent.getType());

gtk: how to hide a window when the application loses focus

I want to duplicate the behaviour of tool windows in OpenOfice. When the application loses focus, the tool windows (if they are not docked) are hidden.
So, I have a main window, and another utility window (win_dock). I want to hide win_dock when all the windows of the application loses focus and show it again if a window gain focus.
What I did is that I connected to the focus-in-event and focus-out-event of all windows of the application, and I maintain a counter of how many windows have focus. When this counter drops to zero, I want to hide win_dock, and if this counter is positive again, I want to show win_dock
The problem is with this solution I can never focus win_dock. Because when I click on it, the main window drops the focus, so it hides win_dock that still hadn't gained the focus. Nevertheless the focus-in-event is still sent to win_dock and the windows reappears. But in the meantime it has lost the focus.
Do you have a better solution?
Here is the Vala source code:
public class Main
{
private Gtk.Builder builder;
private Gtk.Window win_messages;
private Gtk.Window win_dock;
private int focus_count = 0;
public Main() {
builder = new Gtk.Builder();
builder.add_from_file("ui2.glade");
win_messages = builder.get_object("win_messages") as Gtk.Window;
win_dock = builder.get_object("win_dock") as Gtk.Window;
handle_focus(win_messages);
handle_focus(win_dock);
}
public void start(){
win_messages.show_all();
//win_dock.show_all();
Gtk.main();
}
private void handle_focus(Gtk.Window w) {
w.focus_in_event.connect ((w, e) => {
stdout.printf("Focus In (%s)\n", w.name);
focus_count++;
manage_focus(w == win_dock);
});
w.focus_out_event.connect((w, e) => {
stdout.printf("Focus Out (%s)\n", w.name);
focus_count--;
manage_focus(w == win_dock);
});
}
private void manage_focus(bool is_dock){
if(focus_count > 0) {
win_dock.show_all();
stdout.printf("Show (focus: %d)\n", focus_count);
} else if(is_dock) {
win_dock.hide_all();
stdout.printf("Hide (focus: %d, has: %d) dock\n", focus_count, win_dock.is_active ? 1 : 0);
} else if(!is_dock) {
if(win_dock.is_active) {
win_dock.hide_all();
stdout.printf("Hide (focus: %d, has: %d) !dock\n", focus_count, win_dock.is_active ? 1 : 0);
} else {
stdout.printf("Nop (focus: %d, has: %d) !dock\n", focus_count, win_dock.is_active ? 1 : 0);
}
}
}
public static int main (string[] args)
{
Gtk.init (ref args);
Main m = new Main();
m.start();
return 0;
}
}
Thanks.
Is there a good reason to make the dialog disappear? Wouldn't it be enough to make win_dock transient (win_dock.set_transient_for) for the main window?
Otherwise you could try using GLib.Idle.add to call manage_focuswhich will cause
manage_focus to run after all your focus event callbacks have run. It will then have the correct number of focused windows.