Skip to content

Instantly share code, notes, and snippets.

@profmaad
Created September 4, 2010 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save profmaad/565078 to your computer and use it in GitHub Desktop.
Save profmaad/565078 to your computer and use it in GitHub Desktop.
# include <iostream>
# include <string>
# include <sstream>
# include <gtk/gtk.h>
# include <gdk/gdkkeysyms.h>
GtkWidget *mainWindow;
GtkWidget *tabBar;
GtkAccelGroup *hotkeys;
void pageAddedCallback(GtkNotebook *notebook, GtkWidget *child, guint pageNum)
{
std::cerr<<"page added: "<<child<<" @ "<<pageNum<<std::endl;
}
void pageRemovedCallback(GtkNotebook *notebook, GtkWidget *child, guint pageNum)
{
std::cerr<<"page removed: "<<child<<" @ "<<pageNum<<std::endl;
}
void plugAddedCallback(GtkSocket *socket)
{
if(!GTK_IS_SOCKET(socket))
{
std::cerr<<"plugAddedCallback called with invalid socket: "<<socket<<std::endl;
return;
}
std::cerr<<"plug added to "<<socket<<": "<<gtk_socket_get_plug_window(GTK_SOCKET(socket))<<" ("<<g_type_name(G_OBJECT_TYPE(gtk_socket_get_plug_window(GTK_SOCKET(socket))))<<")"<<std::endl;
}
gboolean plugRemovedCallback(GtkSocket *socket)
{
if(!GTK_IS_SOCKET(socket))
{
std::cerr<<"plugRemovedCallback called with invalid socket: "<<socket<<std::endl;
return true;
}
std::cerr<<"plug removed: "<<socket<<"|"<<gtk_socket_get_plug_window(GTK_SOCKET(socket))<<std::endl;
return true;
}
void createNewTab()
{
GtkWidget *gtkSocket = gtk_socket_new();
if(!(gtkSocket && GTK_IS_SOCKET(gtkSocket))) { return; }
g_signal_connect(gtkSocket, "plug-added", G_CALLBACK(plugAddedCallback), NULL);
g_signal_connect(gtkSocket, "plug-removed", G_CALLBACK(plugRemovedCallback), NULL);
int result = gtk_notebook_append_page(GTK_NOTEBOOK(tabBar), gtkSocket, NULL);
if(result < 0)
{
std::cerr<<"failed to append page"<<std::endl;
return;
}
gtk_widget_show(gtkSocket);
gtk_notebook_set_current_page(GTK_NOTEBOOK(tabBar), -1);
g_object_ref(G_OBJECT(gtkSocket));
std::ostringstream conversionStream;
conversionStream<<"view";
conversionStream<<result;
gtk_notebook_set_tab_label(GTK_NOTEBOOK(tabBar), gtkSocket, gtk_label_new(conversionStream.str().c_str()));
std::cerr<<"created new tab "<<result<<std::endl;
return;
}
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
if(!g_thread_supported())
{
g_thread_init(NULL);
}
mainWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
tabBar = gtk_notebook_new();
gtk_notebook_set_scrollable(GTK_NOTEBOOK(tabBar), TRUE);
gtk_notebook_popup_enable(GTK_NOTEBOOK(tabBar));
hotkeys = gtk_accel_group_new();
gtk_accel_group_connect(hotkeys, GDK_T, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE, g_cclosure_new(GCallback(createNewTab), NULL, NULL));
gtk_window_add_accel_group(GTK_WINDOW(mainWindow), hotkeys);
gtk_container_add(GTK_CONTAINER(mainWindow), tabBar);
createNewTab();
g_signal_connect(tabBar, "page-added", G_CALLBACK(pageAddedCallback), NULL);
g_signal_connect(tabBar, "page-removed", G_CALLBACK(pageRemovedCallback), NULL);
gtk_widget_show_all(mainWindow);
gtk_main();
return 0;
}
@profmaad
Copy link
Author

profmaad commented Sep 4, 2010

How to use:
CTRL-T creates a new page.
Create many pages, at some point the terminal should start outputting a huge amount of alternating "page added..." and "plug removed..." messages.
E voila, the bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment