testiconview-keynav.c 7.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* testiconview-keynav.c
 * Copyright (C) 2010  Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
 *
 * Author: Matthias Clasen
 */

/*
 * This example demonstrates how to use the keynav-failed signal to
 * extend arrow keynav over adjacent icon views. This can be used when
 * grouping items.
 */

#include <gtk/gtk.h>

static GtkTreeModel *
get_model (void)
{
  static GtkListStore *store;
  GtkTreeIter iter;

  if (store)
    return (GtkTreeModel *) g_object_ref (store);

  store = gtk_list_store_new (1, G_TYPE_STRING);

  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter, 0, "One", -1);
  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter, 0, "Two", -1);
  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter, 0, "Three", -1);
  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter, 0, "Four", -1);
  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter, 0, "Five", -1);
  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter, 0, "Six", -1);
  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter, 0, "Seven", -1);
  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter, 0, "Eight", -1);

  return (GtkTreeModel *) store;
}

static gboolean
visible_func (GtkTreeModel *model,
              GtkTreeIter  *iter,
              gpointer      data)
{
  gboolean first = GPOINTER_TO_INT (data);
  gboolean visible;
  GtkTreePath *path;

  path = gtk_tree_model_get_path (model, iter);

70
  if (gtk_tree_path_get_indices (path)[0] < 4)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    visible = first;
  else
    visible = !first;

  gtk_tree_path_free (path);

  return visible;
}

GtkTreeModel *
get_filter_model (gboolean first)
{
  GtkTreeModelFilter *model;

  model = (GtkTreeModelFilter *)gtk_tree_model_filter_new (get_model (), NULL);

  gtk_tree_model_filter_set_visible_func (model, visible_func, GINT_TO_POINTER (first), NULL);

  return (GtkTreeModel *) model;
}

static GtkWidget *
get_view (gboolean first)
{
  GtkWidget *view;

  view = gtk_icon_view_new_with_model (get_filter_model (first));
  gtk_icon_view_set_text_column (GTK_ICON_VIEW (view), 0);
  gtk_widget_set_size_request (view, 0, -1);

  return view;
}

typedef struct
{
  GtkWidget *header1;
  GtkWidget *view1;
  GtkWidget *header2;
  GtkWidget *view2;
} Views;

static gboolean
keynav_failed (GtkWidget        *view,
               GtkDirectionType  direction,
               Views            *views)
{
  GtkTreePath *path;
  GtkTreeModel *model;
  GtkTreeIter iter;
  gint col;
  GtkTreePath *sel;

  if (view == views->view1 && direction == GTK_DIR_DOWN)
    {
      if (gtk_icon_view_get_cursor (GTK_ICON_VIEW (views->view1), &path, NULL))
        {
          col = gtk_icon_view_get_item_column (GTK_ICON_VIEW (views->view1), path);
          gtk_tree_path_free (path);

          sel = NULL;
          model = gtk_icon_view_get_model (GTK_ICON_VIEW (views->view2));
          gtk_tree_model_get_iter_first (model, &iter);
          do {
            path = gtk_tree_model_get_path (model, &iter);
            if (gtk_icon_view_get_item_column (GTK_ICON_VIEW (views->view2), path) == col)
              {
                sel = path;
                break;
              }
          } while (gtk_tree_model_iter_next (model, &iter));

          gtk_icon_view_set_cursor (GTK_ICON_VIEW (views->view2), sel, NULL, FALSE);
          gtk_tree_path_free (sel);
        }
      gtk_widget_grab_focus (views->view2);
      return TRUE;
    }

  if (view == views->view2 && direction == GTK_DIR_UP)
    {
      if (gtk_icon_view_get_cursor (GTK_ICON_VIEW (views->view2), &path, NULL))
        {
          col = gtk_icon_view_get_item_column (GTK_ICON_VIEW (views->view2), path);
          gtk_tree_path_free (path);

          sel = NULL;
          model = gtk_icon_view_get_model (GTK_ICON_VIEW (views->view1));
          gtk_tree_model_get_iter_first (model, &iter);
          do {
            path = gtk_tree_model_get_path (model, &iter);
            if (gtk_icon_view_get_item_column (GTK_ICON_VIEW (views->view1), path) == col)
              {
                if (sel)
                  gtk_tree_path_free (sel);
                sel = path;
              }
            else
              gtk_tree_path_free (path);
          } while (gtk_tree_model_iter_next (model, &iter));

          gtk_icon_view_set_cursor (GTK_ICON_VIEW (views->view1), sel, NULL, FALSE);
          gtk_tree_path_free (sel);
        }
      gtk_widget_grab_focus (views->view1);
      return TRUE;
    }

  return FALSE;
}

static gboolean
focus_out (GtkWidget     *view,
           GdkEventFocus *event,
           gpointer       data)
{
  gtk_icon_view_unselect_all (GTK_ICON_VIEW (view));

  return FALSE;
}

static gboolean
focus_in (GtkWidget     *view,
          GdkEventFocus *event,
          gpointer       data)
{
  GtkTreePath *path;

  if (!gtk_icon_view_get_cursor (GTK_ICON_VIEW (view), &path, NULL))
    {
      path = gtk_tree_path_new_from_indices (0, -1);
      gtk_icon_view_set_cursor (GTK_ICON_VIEW (view), path, NULL, FALSE);
    }

  gtk_icon_view_select_path (GTK_ICON_VIEW (view), path);
  gtk_tree_path_free (path);

  return FALSE;
}

210 211 212 213 214
#define CSS \
  "GtkWindow {\n" \
  "  background-color: @base_color;\n" \
  "}\n"

215
static void
216
set_styles (void)
217
{
218 219 220 221 222
  GtkCssProvider *provider;
  GdkScreen *screen;

  provider = gtk_css_provider_new ();

223
  gtk_css_provider_load_from_data (provider, CSS, -1);
224

225
  screen = gdk_display_get_default_screen (gdk_display_get_default ());
226

227 228
  gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider),
                                             GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
229 230 231 232 233 234 235 236 237 238 239
}

int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *vbox;
  Views views;

  gtk_init (&argc, &argv);

240 241
  set_styles ();

242
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
243
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  gtk_container_add (GTK_CONTAINER (window), vbox);

  views.header1 = g_object_new (GTK_TYPE_LABEL,
                                "label", "<b>Group 1</b>",
                                "use-markup", TRUE,
                                "xalign", 0.0,
                                NULL);
  views.view1 = get_view (TRUE);
  views.header2 = g_object_new (GTK_TYPE_LABEL,
                                "label", "<b>Group 2</b>",
                                "use-markup", TRUE,
                                "xalign", 0.0,
                                NULL);
  views.view2 = get_view (FALSE);

  g_signal_connect (views.view1, "keynav-failed",
                    G_CALLBACK (keynav_failed), &views);
  g_signal_connect (views.view2, "keynav-failed",
                    G_CALLBACK (keynav_failed), &views);
  g_signal_connect (views.view1, "focus-in-event",
                    G_CALLBACK (focus_in), NULL);
  g_signal_connect (views.view1, "focus-out-event",
                    G_CALLBACK (focus_out), NULL);
  g_signal_connect (views.view2, "focus-in-event",
                    G_CALLBACK (focus_in), NULL);
  g_signal_connect (views.view2, "focus-out-event",
                    G_CALLBACK (focus_out), NULL);

  gtk_container_add (GTK_CONTAINER (vbox), views.header1);
  gtk_container_add (GTK_CONTAINER (vbox), views.view1);
  gtk_container_add (GTK_CONTAINER (vbox), views.header2);
  gtk_container_add (GTK_CONTAINER (vbox), views.view2);

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}