Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Program which shows new window u/ entry but not u/ renderer

By: brandizzi on Dec 5th, 2012  |  syntax: C  |  size: 4.54 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <gtk/gtk.h>
  2. #include <stdbool.h>
  3.  
  4. bool on_button_clicked(GtkWidget *button, gpointer data);
  5. static bool on_cell_renderer_text_editing_started(
  6.         GtkCellRenderer *renderer, GtkCellEditable *editable,
  7.         gchar *path, gpointer data);
  8.  
  9. int main(int argc, char *argv[]) {
  10.     gtk_init(&argc, &argv);
  11.    
  12.     // Creating entry
  13.     GtkWidget *entry = gtk_entry_new();
  14.     GtkWidget *open_button = gtk_button_new_with_mnemonic("_Open");
  15.    
  16.     g_signal_connect(G_OBJECT(open_button), "clicked",
  17.              G_CALLBACK(on_button_clicked), entry);
  18.    
  19.     // Creating tree view stuff
  20.     GtkListStore *store = gtk_list_store_new(2, G_TYPE_INT, G_TYPE_STRING);
  21.     GtkWidget *tree_view = gtk_tree_view_new();
  22.     gtk_tree_view_set_model(GTK_TREE_VIEW(tree_view), GTK_TREE_MODEL(store));
  23.     // Creating columns
  24.     GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
  25.     gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(tree_view),
  26.             -1, "Number", gtk_cell_renderer_text_new(),
  27.             "text", 0, NULL);
  28.     gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(tree_view),
  29.             -1, "Right", renderer,
  30.             "text", 1,
  31.             NULL);
  32.     g_object_set(G_OBJECT(renderer), "editable", true, NULL);
  33.     // Populating models
  34.     GtkTreeIter iter;
  35.     gtk_list_store_append(store, &iter);
  36.     gtk_list_store_set(store, &iter, 0, 42, 1, "right 1", -1);
  37.     gtk_list_store_append(store, &iter);
  38.     gtk_list_store_set(store, &iter, 0, 13, 1, "right 2", -1);
  39.     gtk_list_store_append(store, &iter);
  40.     gtk_list_store_set(store, &iter, 0, 9, 1, "right 3", -1);
  41.     // Callback for editing text
  42.     g_signal_connect(
  43.             G_OBJECT(renderer), "editing-started",
  44.             G_CALLBACK(on_cell_renderer_text_editing_started), NULL);
  45.    
  46.     // Packing together
  47.     GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL),
  48.             *hbox = gtk_hbox_new(false, 0),
  49.             *vbox = gtk_vbox_new(false, 0);
  50.     gtk_container_add(GTK_CONTAINER(hbox), entry);
  51.     gtk_container_add(GTK_CONTAINER(hbox), open_button);
  52.     gtk_container_add(GTK_CONTAINER(vbox), hbox);
  53.     gtk_container_add(GTK_CONTAINER(vbox), tree_view);
  54.     gtk_container_add(GTK_CONTAINER(window), vbox);
  55.    
  56.     gtk_widget_show_all(window);
  57.    
  58.     gtk_main();
  59.    
  60.     return 0;
  61. }
  62.  
  63. bool on_button_clicked(GtkWidget *button, gpointer data) {
  64.     GtkWidget *entry = data,
  65.             *subwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL),
  66.             *sublabel = gtk_label_new("This is a rather long message\n"
  67.                     "with many lines whose mere purpose is\n"
  68.                     "to test some stuff. It will appear in \n"
  69.                     "the RIGHT PLACE.");
  70.     gtk_container_add(GTK_CONTAINER(subwindow), sublabel);
  71.     gtk_window_set_decorated(GTK_WINDOW(subwindow), FALSE);
  72.     gtk_window_set_modal(GTK_WINDOW(subwindow), TRUE);
  73.  
  74.  
  75.     GtkWidget *window = gtk_widget_get_toplevel(entry);
  76.     gtk_window_set_transient_for(
  77.             GTK_WINDOW(subwindow), GTK_WINDOW(window));
  78.    
  79.     gint dx, dy;
  80.     gtk_window_get_position(GTK_WINDOW(window), &dx, &dy);
  81.    
  82.     GtkAllocation allocation;
  83.     gtk_widget_get_allocation(entry, &allocation);
  84.    
  85.     gtk_window_move(
  86.             GTK_WINDOW(subwindow),
  87.             allocation.x+dx, allocation.y+allocation.height*2+dy);
  88.    
  89.     gtk_widget_show_all(subwindow);
  90.     return false;
  91. }
  92.  
  93. static bool on_cell_renderer_text_editing_started(
  94.         GtkCellRenderer *renderer, GtkCellEditable *editable,
  95.         gchar *path, gpointer data) {
  96.     GtkWidget *subwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL),
  97.             *sublabel = gtk_label_new("This is a rather long message\n"
  98.                     "with many lines whose mere purpose is\n"
  99.                     "to test some stuff. It will appear in\n"
  100.                     "the WRONG PLACE!");
  101.     gtk_container_add(GTK_CONTAINER(subwindow), sublabel);
  102.     gtk_window_set_decorated(GTK_WINDOW(subwindow), FALSE);
  103.     gtk_window_set_modal(GTK_WINDOW(subwindow), TRUE);
  104.  
  105.     GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(editable));
  106.     gtk_window_set_transient_for(
  107.             GTK_WINDOW(subwindow), GTK_WINDOW(window));
  108.  
  109.     gint dx, dy;
  110.     gtk_window_get_position(GTK_WINDOW(window), &dx, &dy);
  111.  
  112.     GtkAllocation allocation;
  113.     gtk_widget_get_allocation(GTK_WIDGET(editable), &allocation);
  114.    
  115.     gtk_window_move(
  116.             GTK_WINDOW(subwindow),
  117.            allocation.x+dx, allocation.y+allocation.height*2+dy);
  118.     gtk_widget_show_all(subwindow);
  119.     return false;
  120. }