#include <gtk/gtk.h>

static GtkWidget *notebook, *list_box;

static gboolean on_keypress(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
{
   if (widget == list_box)
   {
      if (event->keyval == GDK_KEY_Left || event->keyval == GDK_KEY_Right)
      {
         gtk_widget_grab_focus(notebook);
         gtk_main_do_event((GdkEvent *)event);
         return 1;
      }
   }
   else if (widget == notebook)
   {
      if (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_Up)
      {
         gtk_widget_grab_focus(list_box);
         gtk_main_do_event((GdkEvent *)event);
         return 1;
      }
   }

   return 0;  // propagate keypress
}

int main(int argc, char **argv)
{
   GtkWidget *window, *grid;
   GtkWidget *row1, *row2, *row3;

   gtk_init(&argc, &argv);

   window   = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   grid     = gtk_grid_new();
   list_box = gtk_list_box_new();
   notebook = gtk_notebook_new();

   g_signal_connect(window,   "destroy",         G_CALLBACK(gtk_main_quit), NULL);
   g_signal_connect(list_box, "key-press-event", G_CALLBACK(on_keypress),   NULL);
   g_signal_connect(notebook, "key-press-event", G_CALLBACK(on_keypress),   NULL);

   g_object_set(list_box, "can-focus",       1, NULL);
   g_object_set(list_box, "width-request", 100, NULL);
   g_object_set(notebook, "expand",          1, NULL);

   row1 = gtk_list_box_row_new();
   gtk_container_add(GTK_CONTAINER(row1), gtk_label_new("label1"));
   gtk_list_box_insert(GTK_LIST_BOX(list_box), row1, -1);

   row2 = gtk_list_box_row_new();
   gtk_container_add(GTK_CONTAINER(row2), gtk_label_new("label2"));
   gtk_list_box_insert(GTK_LIST_BOX(list_box), row2, -1);

   row3 = gtk_list_box_row_new();
   gtk_container_add(GTK_CONTAINER(row3), gtk_label_new("label3"));
   gtk_list_box_insert(GTK_LIST_BOX(list_box), row3, -1);

   gtk_notebook_append_page(GTK_NOTEBOOK(notebook), gtk_text_view_new(), gtk_label_new("page1"));
   gtk_notebook_append_page(GTK_NOTEBOOK(notebook), gtk_text_view_new(), gtk_label_new("page2"));

   gtk_grid_attach(GTK_GRID(grid), list_box, 0, 0, 1, 1);  // left, top, width, height
   gtk_grid_attach(GTK_GRID(grid), notebook, 1, 0, 1, 1);  // left, top, width, height
   gtk_container_add(GTK_CONTAINER(window), grid);
   gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
   gtk_widget_show_all(window);

   gtk_list_box_select_row(GTK_LIST_BOX(list_box), GTK_LIST_BOX_ROW(row1));
   gtk_widget_grab_focus(list_box);
   gtk_main();
   return 0;
}
