list.c 39.5 KB
Newer Older
Victor Ananjesky's avatar
Victor Ananjesky committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * This file is part of YAD.
 *
 * YAD is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * YAD 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with YAD. If not, see <http://www.gnu.org/licenses/>.
 *
17
 * Copyright (C) 2008-2019, Victor Ananjevsky <ananasik@gmail.com>
Victor Ananjesky's avatar
Victor Ananjesky committed
18 19 20 21 22 23 24 25 26 27
 */

#include <string.h>
#include <stdlib.h>

#include "yad.h"

static GtkWidget *list_view;

static gint fore_col, back_col, font_col;
Victor Ananjevsky's avatar
Victor Ananjevsky committed
28
static guint n_cols = 0;
Victor Ananjesky's avatar
Victor Ananjesky committed
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 70 71 72

static gulong select_hndl = 0;

static inline void
yad_list_add_row (GtkListStore *m, GtkTreeIter *it)
{
  if (options.list_data.add_on_top)
    gtk_list_store_prepend (m, it);
  else
    gtk_list_store_append (m, it);

  if (options.common_data.tail)
    gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (list_view), gtk_tree_model_get_path (GTK_TREE_MODEL (m), it),
                                  NULL, FALSE, 1.0, 1.0);
}

static gboolean
list_activate_cb (GtkWidget *widget, GdkEventKey *event, gpointer data)
{
  if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)
    {
      if (options.list_data.dclick_action)
        {
          /* FIXME: check this under gtk-3.0 */
          if (event->state & GDK_CONTROL_MASK)
            {
              if (options.plug == -1)
                yad_exit (options.data.def_resp);
            }
          else
            return FALSE;
        }
      else
        {
          if (options.plug == -1)
            yad_exit (options.data.def_resp);
        }

      return TRUE;
    }

  return FALSE;
}

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
/* custom tooltip signal handler for no-markup mode */
static gboolean
tooltip_cb (GtkWidget *w, gint x, gint y, gboolean mode, GtkTooltip *tip, gpointer data)
{
  gchar *str;
  gint bx, by;
  GtkTreePath *path;
  GtkTreeIter iter;
  GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (w));

  gtk_tree_view_convert_widget_to_bin_window_coords (GTK_TREE_VIEW (w), x, y, &bx, &by);
  if (!gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (w), bx, by, &path, NULL, NULL, NULL))
    return FALSE;

  gtk_tree_model_get_iter (model, &iter, path);
  gtk_tree_path_free (path);

  gtk_tree_model_get (model, &iter, options.list_data.tooltip_column - 1, &str, -1);
  if (str)
    {
      gtk_tooltip_set_text (tip, str);
      return TRUE;
    }

  return FALSE;
}

Victor Ananjesky's avatar
Victor Ananjesky committed
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
static void
toggled_cb (GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
{
  gint column;
  gboolean fixed;
  GtkTreeIter iter;
  GtkTreePath *path = gtk_tree_path_new_from_string (path_str);
  GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (list_view));

  column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cell), "column"));
  gtk_tree_model_get_iter (model, &iter, path);
  gtk_tree_model_get (model, &iter, column, &fixed, -1);

  fixed ^= 1;

  gtk_list_store_set (GTK_LIST_STORE (model), &iter, column, fixed, -1);

  gtk_tree_path_free (path);
}

static gboolean
runtoggle (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
{
  gint col = GPOINTER_TO_INT (data);
  gtk_list_store_set (GTK_LIST_STORE (model), iter, col, FALSE, -1);
  return FALSE;
}

static void
rtoggled_cb (GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
{
  gint column;
  GtkTreeIter iter;
  GtkTreePath *path = gtk_tree_path_new_from_string (path_str);
  GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (list_view));

  column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cell), "column"));

  gtk_tree_model_foreach (model, runtoggle, GINT_TO_POINTER (column));

  gtk_tree_model_get_iter (model, &iter, path);
  gtk_list_store_set (GTK_LIST_STORE (model), &iter, column, TRUE, -1);

  gtk_tree_path_free (path);
}

static void
cell_edited_cb (GtkCellRendererText *cell, const gchar *path_string, const gchar *new_text, gpointer data)
{
  gint column;
  GtkTreeIter iter;
  GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
  GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (list_view));
  YadColumn *col;

  column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cell), "column"));
  gtk_tree_model_get_iter (model, &iter, path);
  col = (YadColumn *) g_slist_nth_data (options.list_data.columns, column);

  if (col->type == YAD_COLUMN_NUM)
    gtk_list_store_set (GTK_LIST_STORE (model), &iter, column, g_ascii_strtoll (new_text, NULL, 10), -1);
  else if (col->type == YAD_COLUMN_FLOAT)
    gtk_list_store_set (GTK_LIST_STORE (model), &iter, column, g_ascii_strtod (new_text, NULL), -1);
  else
    gtk_list_store_set (GTK_LIST_STORE (model), &iter, column, new_text, -1);

  gtk_tree_path_free (path);
}

static gboolean
regex_search (GtkTreeModel *model, gint col, const gchar *key, GtkTreeIter *iter, gpointer data)
{
  static GRegex *pattern = NULL;
  static guint pos = 0;
  gchar *str;

  if (key[pos])
    {
      if (pattern)
        g_regex_unref (pattern);
      pattern = g_regex_new (key, G_REGEX_CASELESS | G_REGEX_EXTENDED | G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTEMPTY, NULL);
      pos = strlen (key);
    }

  if (pattern)
    {
      gboolean ret;

      gtk_tree_model_get (model, iter, col, &str, -1);

      ret = g_regex_match (pattern, str, G_REGEX_MATCH_NOTEMPTY, NULL);
      /* if get it, clear key end position */
      if (!ret)
        pos = 0;

      return !ret;
    }
  else
    return TRUE;
}

static GtkTreeModel *
Victor Ananjevsky's avatar
Victor Ananjevsky committed
202
create_model ()
Victor Ananjesky's avatar
Victor Ananjesky committed
203 204 205 206 207
{
  GtkListStore *store;
  GType *ctypes;
  gint i;

Victor Ananjevsky's avatar
Victor Ananjevsky committed
208
  ctypes = g_new0 (GType, n_cols);
Victor Ananjesky's avatar
Victor Ananjesky committed
209 210 211 212 213 214 215 216 217 218 219 220

  if (options.list_data.checkbox)
    {
      YadColumn *col = (YadColumn *) g_slist_nth_data (options.list_data.columns, 0);
      col->type = YAD_COLUMN_CHECK;
    }
  else if (options.list_data.radiobox)
    {
      YadColumn *col = (YadColumn *) g_slist_nth_data (options.list_data.columns, 0);
      col->type = YAD_COLUMN_RADIO;
    }

Victor Ananjevsky's avatar
Victor Ananjevsky committed
221
  for (i = 0; i < n_cols; i++)
Victor Ananjesky's avatar
Victor Ananjesky committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    {
      YadColumn *col = (YadColumn *) g_slist_nth_data (options.list_data.columns, i);

      switch (col->type)
        {
        case YAD_COLUMN_CHECK:
        case YAD_COLUMN_RADIO:
          ctypes[i] = G_TYPE_BOOLEAN;
          break;
        case YAD_COLUMN_NUM:
        case YAD_COLUMN_SIZE:
        case YAD_COLUMN_BAR:
          ctypes[i] = G_TYPE_INT64;
          break;
        case YAD_COLUMN_FLOAT:
          ctypes[i] = G_TYPE_DOUBLE;
          break;
        case YAD_COLUMN_IMAGE:
          ctypes[i] = GDK_TYPE_PIXBUF;
          break;
        case YAD_COLUMN_ATTR_FORE:
          ctypes[i] = G_TYPE_STRING;
          fore_col = i;
          break;
        case YAD_COLUMN_ATTR_BACK:
          ctypes[i] = G_TYPE_STRING;
          back_col = i;
          break;
        case YAD_COLUMN_ATTR_FONT:
          ctypes[i] = G_TYPE_STRING;
          font_col = i;
          break;
        default:
          ctypes[i] = G_TYPE_STRING;
          break;
        }
    }

Victor Ananjevsky's avatar
Victor Ananjevsky committed
260
  store = gtk_list_store_newv (n_cols, ctypes);
Victor Ananjesky's avatar
Victor Ananjesky committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

  return GTK_TREE_MODEL (store);
}

static void
float_col_format (GtkTreeViewColumn *col, GtkCellRenderer *cell, GtkTreeModel *model,
                  GtkTreeIter *iter, gpointer data)
{
  gdouble val;
  gchar buf[20];

  gtk_tree_model_get (model, iter, GPOINTER_TO_INT (data), &val, -1);
  g_snprintf (buf, sizeof (buf), "%.*f", options.common_data.float_precision, val);
  g_object_set (cell, "text", buf, NULL);
}

static void
size_col_format (GtkTreeViewColumn *col, GtkCellRenderer *cell, GtkTreeModel *model,
                  GtkTreeIter *iter, gpointer data)
{
  guint64 val;
  gchar buf[20], *sz;

  gtk_tree_model_get (model, iter, GPOINTER_TO_INT (data), &val, -1);
#if GLIB_CHECK_VERSION(2,30,0)
  sz = g_format_size_full (val, options.common_data.size_fmt);
#elif  GLIB_CHECK_VERSION(2,16,0)
  sz = g_format_size_for_display (val);
#else
  sz = g_strdup_printf ("%d", val);
#endif
  g_snprintf (buf, sizeof (buf), "%s", sz);
  g_free (sz);
  g_object_set (cell, "text", buf, NULL);
}

static void
Victor Ananjevsky's avatar
Victor Ananjevsky committed
298
add_columns ()
Victor Ananjesky's avatar
Victor Ananjesky committed
299 300 301 302 303
{
  gint i;
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;

Victor Ananjevsky's avatar
Victor Ananjevsky committed
304
  for (i = 0; i < n_cols; i++)
Victor Ananjesky's avatar
Victor Ananjesky committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    {
      YadColumn *col = (YadColumn *) g_slist_nth_data (options.list_data.columns, i);

      if (i == options.list_data.hide_column - 1 || col->type == YAD_COLUMN_HIDDEN ||
          i == fore_col || i == back_col || i == font_col)
        continue;

      switch (col->type)
        {
        case YAD_COLUMN_CHECK:
        case YAD_COLUMN_RADIO:
          renderer = gtk_cell_renderer_toggle_new ();
          column = gtk_tree_view_column_new_with_attributes (col->name, renderer, "active", i, NULL);
          if (back_col != -1)
            gtk_tree_view_column_add_attribute (column, renderer, "cell-background", back_col);
          if (col->type == YAD_COLUMN_RADIO)
            {
              gtk_cell_renderer_toggle_set_radio (GTK_CELL_RENDERER_TOGGLE (renderer), TRUE);
              g_signal_connect (renderer, "toggled", G_CALLBACK (rtoggled_cb), NULL);
            }
          else
            g_signal_connect (renderer, "toggled", G_CALLBACK (toggled_cb), NULL);
          break;
        case YAD_COLUMN_IMAGE:
          renderer = gtk_cell_renderer_pixbuf_new ();
          column = gtk_tree_view_column_new_with_attributes (col->name, renderer, "pixbuf", i, NULL);
          if (back_col != -1)
            gtk_tree_view_column_add_attribute (column, renderer, "cell-background", back_col);
          break;
        case YAD_COLUMN_NUM:
        case YAD_COLUMN_SIZE:
        case YAD_COLUMN_FLOAT:
          renderer = gtk_cell_renderer_text_new ();
          if (col->editable)
            {
              g_object_set (G_OBJECT (renderer), "editable", TRUE, NULL);
              g_signal_connect (renderer, "edited", G_CALLBACK (cell_edited_cb), NULL);
            }
          column = gtk_tree_view_column_new_with_attributes (col->name, renderer, "text", i, NULL);
          if (fore_col != -1)
            gtk_tree_view_column_add_attribute (column, renderer, "foreground", fore_col);
          if (back_col != -1)
            gtk_tree_view_column_add_attribute (column, renderer, "cell-background", back_col);
          if (font_col != -1)
            gtk_tree_view_column_add_attribute (column, renderer, "font", font_col);
          gtk_tree_view_column_set_sort_column_id (column, i);
          gtk_tree_view_column_set_resizable (column, TRUE);
          if (col->type == YAD_COLUMN_FLOAT)
            gtk_tree_view_column_set_cell_data_func (column, renderer, float_col_format, GINT_TO_POINTER (i), NULL);
          else if (col->type == YAD_COLUMN_SIZE)
            gtk_tree_view_column_set_cell_data_func (column, renderer, size_col_format, GINT_TO_POINTER (i), NULL);
          break;
        case YAD_COLUMN_BAR:
          renderer = gtk_cell_renderer_progress_new ();
          column = gtk_tree_view_column_new_with_attributes (col->name, renderer, "value", i, NULL);
          if (back_col != -1)
            gtk_tree_view_column_add_attribute (column, renderer, "cell-background", back_col);
          gtk_tree_view_column_set_sort_column_id (column, i);
          gtk_tree_view_column_set_resizable (column, TRUE);
          break;
        default:
          renderer = gtk_cell_renderer_text_new ();
          if (col->editable)
            {
              g_object_set (G_OBJECT (renderer), "editable", TRUE, NULL);
              g_signal_connect (renderer, "edited", G_CALLBACK (cell_edited_cb), NULL);
            }
          if (options.data.no_markup)
            column = gtk_tree_view_column_new_with_attributes (col->name, renderer, "text", i, NULL);
          else
            column = gtk_tree_view_column_new_with_attributes (col->name, renderer, "markup", i, NULL);
376 377
          if (col->ellipsize)
            g_object_set (G_OBJECT (renderer), "ellipsize", options.list_data.ellipsize, NULL);
Victor Ananjesky's avatar
Victor Ananjesky committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
          if (col->wrap)
            {
              g_object_set (G_OBJECT (renderer), "wrap-width", options.list_data.wrap_width, NULL);
              g_object_set (G_OBJECT (renderer), "wrap-mode", PANGO_WRAP_WORD_CHAR, NULL);
            }
          if (fore_col != -1)
            gtk_tree_view_column_add_attribute (column, renderer, "foreground", fore_col);
          if (back_col != -1)
            gtk_tree_view_column_add_attribute (column, renderer, "cell-background", back_col);
          if (font_col != -1)
            gtk_tree_view_column_add_attribute (column, renderer, "font", font_col);
          gtk_tree_view_column_set_sort_column_id (column, i);
          gtk_tree_view_column_set_resizable (column, TRUE);
          break;
        }
      g_object_set_data (G_OBJECT (renderer), "column", GINT_TO_POINTER (i));
      gtk_tree_view_append_column (GTK_TREE_VIEW (list_view), column);

      gtk_tree_view_column_set_clickable (column, options.list_data.clickable);

      if (col->type != YAD_COLUMN_CHECK && col->type != YAD_COLUMN_IMAGE)
        {
          if (i == options.list_data.expand_column - 1 || options.list_data.expand_column == 0)
            gtk_tree_view_column_set_expand (column, TRUE);
        }
    }

  if (options.list_data.checkbox && !options.list_data.search_column)
    options.list_data.search_column += 1;
Victor Ananjevsky's avatar
Victor Ananjevsky committed
407
  if (options.list_data.search_column <= n_cols)
Victor Ananjesky's avatar
Victor Ananjesky committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    {
      options.list_data.search_column -= 1;
      gtk_tree_view_set_search_column (GTK_TREE_VIEW (list_view), options.list_data.search_column);
    }
}

static void
cell_set_data (GtkTreeIter *it, guint num, gchar *data)
{
  GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (list_view));
  YadColumn *col = (YadColumn *) g_slist_nth_data (options.list_data.columns, num);

  switch (col->type)
    {
    case YAD_COLUMN_CHECK:
    case YAD_COLUMN_RADIO:
424
        gtk_list_store_set (GTK_LIST_STORE (model), it, num, get_bool_val (data), -1);
Victor Ananjesky's avatar
Victor Ananjesky committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
      break;
    case YAD_COLUMN_NUM:
    case YAD_COLUMN_SIZE:
      gtk_list_store_set (GTK_LIST_STORE (model), it, num, g_ascii_strtoll (data, NULL, 10), -1);
      break;
    case YAD_COLUMN_FLOAT:
      gtk_list_store_set (GTK_LIST_STORE (model), it, num, g_ascii_strtod (data, NULL), -1);
      break;
    case YAD_COLUMN_BAR:
      {
        gint64 val = g_ascii_strtoll (data, NULL, 10);
        if (val < 0)
          val = 0;
        if (val > 100)
          val = 100;
        gtk_list_store_set (GTK_LIST_STORE (model), it, num, val, -1);
        break;
      }
    case YAD_COLUMN_IMAGE:
      {
445
        GdkPixbuf *pb = get_pixbuf (data, YAD_SMALL_ICON, FALSE);
Victor Ananjesky's avatar
Victor Ananjesky committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
        if (pb)
          {
            gtk_list_store_set (GTK_LIST_STORE (model), it, num, pb, -1);
            g_object_unref (pb);
          }
        break;
      }
    default:
      if (data && *data)
        gtk_list_store_set (GTK_LIST_STORE (model), it, num, data, -1);
      break;
    }
}

static gchar *
cell_get_data (GtkTreeIter *it, guint num)
{
  gchar *data = NULL;
  GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (list_view));
  YadColumn *col = (YadColumn *) g_slist_nth_data (options.list_data.columns, num);

  switch (col->type)
    {
    case YAD_COLUMN_CHECK:
    case YAD_COLUMN_RADIO:
      {
        gboolean bval;
        gtk_tree_model_get (model, it, num, &bval, -1);
474
        data = g_strdup (print_bool_val (bval));
Victor Ananjesky's avatar
Victor Ananjesky committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
        break;
      }
    case YAD_COLUMN_NUM:
    case YAD_COLUMN_SIZE:
    case YAD_COLUMN_BAR:
      {
        gint64 nval;
        gtk_tree_model_get (model, it, num, &nval, -1);
        data = g_strdup_printf ("%ld", (long) nval);
        break;
      }
    case YAD_COLUMN_FLOAT:
      {
        gdouble nval;
        gtk_tree_model_get (model, it, num, &nval, -1);
        data = g_strdup_printf ("%lf", nval);
        break;
      }
    case YAD_COLUMN_IMAGE:
      {
        data = g_strdup ("''");
        break;
      }
    default:
      {
        gchar *cval;
        gtk_tree_model_get (model, it, num, &cval, -1);
        if (cval)
          data = g_shell_quote (cval);
        break;
      }
    }

  return data;
}

static gboolean
Victor Ananjevsky's avatar
Victor Ananjevsky committed
512
handle_stdin (GIOChannel *channel, GIOCondition condition, gpointer data)
Victor Ananjesky's avatar
Victor Ananjesky committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
{
  static GtkTreeIter iter;
  static gint column_count = 0;
  static gint row_count = 0;
  GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (list_view));

  if ((condition == G_IO_IN) || (condition == G_IO_IN + G_IO_HUP))
    {
      GError *err = NULL;
      GString *string = g_string_new (NULL);

      while (channel->is_readable != TRUE);

      do
        {
          gint status;

          do
            {
              status = g_io_channel_read_line_string (channel, string, NULL, &err);

              while (gtk_events_pending ())
                gtk_main_iteration ();
            }
          while (status == G_IO_STATUS_AGAIN);

          if (status != G_IO_STATUS_NORMAL)
            {
              if (err)
                {
                  g_printerr ("yad_list_handle_stdin(): %s\n", err->message);
                  g_error_free (err);
                  err = NULL;
                }
              /* stop handling */
              g_io_channel_shutdown (channel, TRUE, NULL);
              return FALSE;
            }

          strip_new_line (string->str);

          /* clear list if ^L received */
          if (string->str[0] == '\014')
            {
              GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (list_view));
              if (select_hndl)
                g_signal_handler_block (G_OBJECT (sel), select_hndl);
              gtk_list_store_clear (GTK_LIST_STORE (model));
              row_count = column_count = 0;
              if (select_hndl)
                g_signal_handler_unblock (G_OBJECT (sel), select_hndl);
              continue;
            }

          if (row_count == 0 && column_count == 0)
            yad_list_add_row (GTK_LIST_STORE (model), &iter);
Victor Ananjevsky's avatar
Victor Ananjevsky committed
569
          else if (column_count == n_cols)
Victor Ananjesky's avatar
Victor Ananjesky committed
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
            {
              /* We're starting a new row */
              column_count = 0;
              row_count++;
              if (options.list_data.limit && row_count >= options.list_data.limit)
                {
                  gtk_tree_model_get_iter_first (model, &iter);
                  gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
                }
              yad_list_add_row (GTK_LIST_STORE (model), &iter);
            }

          cell_set_data (&iter, column_count, string->str);
          column_count++;
        }
      while (g_io_channel_get_buffer_condition (channel) == G_IO_IN);
      g_string_free (string, TRUE);
    }

  if ((condition != G_IO_IN) && (condition != G_IO_IN + G_IO_HUP))
    {
      g_io_channel_shutdown (channel, TRUE, NULL);
      return FALSE;
    }

  return TRUE;
}

static void
Victor Ananjevsky's avatar
Victor Ananjevsky committed
599
fill_data ()
Victor Ananjesky's avatar
Victor Ananjesky committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
{
  GtkTreeIter iter;
  GtkListStore *model = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (list_view)));
  GIOChannel *channel;

  if (options.extra_data && *options.extra_data)
    {
      gchar **args = options.extra_data;
      gint i = 0;

      gtk_widget_freeze_child_notify (list_view);

      while (args[i] != NULL)
        {
          gint j;

          yad_list_add_row (model, &iter);
Victor Ananjevsky's avatar
Victor Ananjevsky committed
617
          for (j = 0; j < n_cols; j++, i++)
Victor Ananjesky's avatar
Victor Ananjesky committed
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
            {
              if (args[i] == NULL)
                break;

              cell_set_data (&iter, j, args[i]);
            }
        }

      gtk_widget_thaw_child_notify (list_view);
    }

  if (options.common_data.listen || !(options.extra_data && *options.extra_data))
    {
      channel = g_io_channel_unix_new (0);
      g_io_channel_set_encoding (channel, NULL, NULL);
      g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
Victor Ananjevsky's avatar
Victor Ananjevsky committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
      g_io_add_watch (channel, G_IO_IN | G_IO_HUP, handle_stdin, NULL);
    }
}

static gchar *
get_data_as_string (GtkTreeIter *iter)
{
  GString *str;
  gchar *res;
  guint i;

  str = g_string_new (NULL);

  for (i = 0; i < n_cols; i++)
    {
      gchar *val = cell_get_data (iter, i);
      if (val)
        {
          g_string_append_printf (str, "%s ", val);
          g_free (val);
        }
Victor Ananjesky's avatar
Victor Ananjesky committed
655
    }
Victor Ananjevsky's avatar
Victor Ananjevsky committed
656 657 658 659 660 661

  str->str[str->len-1] = '\0';
  res = str->str;
  g_string_free (str, FALSE);

  return res;
Victor Ananjesky's avatar
Victor Ananjesky committed
662 663
}

664 665
static void edit_row_cb (GtkMenuItem *item, gpointer data);

Victor Ananjesky's avatar
Victor Ananjesky committed
666
static void
Victor Ananjevsky's avatar
Victor Ananjevsky committed
667
double_click_cb (GtkTreeView *view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer data)
Victor Ananjesky's avatar
Victor Ananjesky committed
668 669 670 671 672 673 674 675
{
  GtkTreeModel *model;
  GtkTreeIter iter;

  model = gtk_tree_view_get_model (view);

  if (options.list_data.dclick_action)
    {
Victor Ananjevsky's avatar
Victor Ananjevsky committed
676
      gchar *cmd, *args = NULL;
Victor Ananjesky's avatar
Victor Ananjesky committed
677 678

      if (gtk_tree_model_get_iter (model, &iter, path))
Victor Ananjevsky's avatar
Victor Ananjevsky committed
679 680 681
        args = get_data_as_string (&iter);
      else
        args = g_strdup ("");
Victor Ananjesky's avatar
Victor Ananjesky committed
682 683 684 685 686 687 688

      if (g_strstr_len (options.list_data.dclick_action, -1, "%s"))
        {
          static GRegex *regex = NULL;

          if (!regex)
            regex = g_regex_new ("\%s", G_REGEX_OPTIMIZE, 0, NULL);
Victor Ananjevsky's avatar
Victor Ananjevsky committed
689
          cmd = g_regex_replace_literal (regex, options.list_data.dclick_action, -1, 0, args, 0, NULL);
Victor Ananjesky's avatar
Victor Ananjesky committed
690 691
        }
      else
Victor Ananjevsky's avatar
Victor Ananjevsky committed
692 693
        cmd = g_strdup_printf ("%s %s", options.list_data.dclick_action, args);
      g_free (args);
Victor Ananjesky's avatar
Victor Ananjesky committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721

      if (cmd[0] == '@')
        {
          gchar *data = NULL;
          gint exit;

          g_spawn_command_line_sync (cmd + 1, &data, NULL, &exit, NULL);
          if (exit == 0)
            {
              gint i;
              gchar **lines = g_strsplit (data, "\n", 0);

              for (i = 0; i < n_cols; i++)
                {
                  if (lines[i] == NULL)
                    break;

                  cell_set_data (&iter, i, lines[i]);
                }
              g_strfreev (lines);
            }
          g_free (data);
        }
      else
        g_spawn_command_line_async (cmd, NULL);

      g_free (cmd);
    }
722 723
  else if (options.common_data.editable && options.list_data.row_action)
    edit_row_cb (NULL, NULL);
Victor Ananjesky's avatar
Victor Ananjesky committed
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
  else
    {
      if (options.list_data.checkbox)
        {
          if (gtk_tree_model_get_iter (model, &iter, path))
            {
              gboolean chk;

              gtk_tree_model_get (model, &iter, 0, &chk, -1);
              chk = !chk;
              gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, chk, -1);
            }
        }
      else if (options.list_data.radiobox)
        {
          if (gtk_tree_model_get_iter (model, &iter, path))
            {
              gtk_tree_model_foreach (model, runtoggle, GINT_TO_POINTER (0));
              gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, TRUE, -1);
            }
        }
      else if (options.plug == -1)
        yad_exit (options.data.def_resp);
    }
}

static void
select_cb (GtkTreeSelection *sel, gpointer data)
{
  GtkTreeModel *model;
  GtkTreeIter iter;
Victor Ananjevsky's avatar
Victor Ananjevsky committed
755
  gchar *cmd, *args;
Victor Ananjesky's avatar
Victor Ananjesky committed
756 757 758 759

  if (!gtk_tree_selection_get_selected (sel, &model, &iter))
    return;

Victor Ananjevsky's avatar
Victor Ananjevsky committed
760 761 762
  args = get_data_as_string (&iter);
  if (!args)
    args = g_strdup ("");
Victor Ananjesky's avatar
Victor Ananjesky committed
763 764 765 766 767 768 769

  if (g_strstr_len (options.list_data.select_action, -1, "%s"))
    {
      static GRegex *regex = NULL;

      if (!regex)
        regex = g_regex_new ("\%s", G_REGEX_OPTIMIZE, 0, NULL);
Victor Ananjevsky's avatar
Victor Ananjevsky committed
770
      cmd = g_regex_replace_literal (regex, options.list_data.select_action, -1, 0, args, 0, NULL);
Victor Ananjesky's avatar
Victor Ananjesky committed
771 772
    }
  else
Victor Ananjevsky's avatar
Victor Ananjevsky committed
773 774
    cmd = g_strdup_printf ("%s %s", options.list_data.select_action, args);
  g_free (args);
Victor Ananjesky's avatar
Victor Ananjesky committed
775 776 777 778 779 780 781

  g_spawn_command_line_async (cmd, NULL);

  g_free (cmd);
}

static void
Victor Ananjevsky's avatar
Victor Ananjevsky committed
782
add_row_cb (GtkMenuItem *item, gpointer data)
Victor Ananjesky's avatar
Victor Ananjesky committed
783 784 785
{
  GtkTreeModel *model;
  GtkTreeIter iter;
786
  gchar *cmd;
Victor Ananjesky's avatar
Victor Ananjesky committed
787 788 789 790

  model = gtk_tree_view_get_model (GTK_TREE_VIEW (list_view));
  yad_list_add_row (GTK_LIST_STORE (model), &iter);

791
  if (options.list_data.row_action)
Victor Ananjesky's avatar
Victor Ananjesky committed
792 793 794 795 796 797 798 799 800 801
    {
      gchar *out = NULL;
      gint exit;

      /* hide menu first */
      gtk_menu_popdown (GTK_MENU (data));
      while (gtk_events_pending ())
        gtk_main_iteration ();

      /* run command */
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
      cmd = g_strdup_printf ("%s add", options.list_data.row_action);
      g_spawn_command_line_sync (cmd, &out, NULL, &exit, NULL);
      g_free (cmd);
      if (exit == 0)
        {
          guint i;
          gchar **lines = g_strsplit (out, "\n", 0);

          for (i = 0; i < n_cols; i++)
            {
              if (lines[i] == NULL)
                break;

              cell_set_data (&iter, i, lines[i]);
            }
          g_strfreev (lines);
        }
      g_free (out);
    }
}

static void
edit_row_cb (GtkMenuItem *item, gpointer data)
{
  GtkTreeIter iter;
  GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (list_view));

  if (!gtk_tree_selection_get_selected (sel, NULL, &iter))
    return;

  if (options.list_data.row_action)
    {
      gchar *cmd, *args, *out = NULL;
      gint exit;

      /* hide menu first */
      gtk_menu_popdown (GTK_MENU (data));
      while (gtk_events_pending ())
        gtk_main_iteration ();

      /* run command */
      args = get_data_as_string (&iter);
      cmd = g_strdup_printf ("%s edit %s", options.list_data.row_action, args);
      g_free (args);
      g_spawn_command_line_sync (cmd, &out, NULL, &exit, NULL);
      g_free (cmd);
Victor Ananjesky's avatar
Victor Ananjesky committed
848 849
      if (exit == 0)
        {
Victor Ananjevsky's avatar
Victor Ananjevsky committed
850
          guint i;
Victor Ananjesky's avatar
Victor Ananjesky committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
          gchar **lines = g_strsplit (out, "\n", 0);

          for (i = 0; i < n_cols; i++)
            {
              if (lines[i] == NULL)
                break;

              cell_set_data (&iter, i, lines[i]);
            }
          g_strfreev (lines);
        }
      g_free (out);
    }
}

static void
Victor Ananjevsky's avatar
Victor Ananjevsky committed
867
del_row_cb (GtkMenuItem *item, gpointer data)
Victor Ananjesky's avatar
Victor Ananjesky committed
868 869 870 871 872 873
{
  GtkTreeIter iter;
  GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (list_view));
  GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (list_view));

  if (gtk_tree_selection_get_selected (sel, NULL, &iter))
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
    {
      if (options.list_data.row_action)
        {
          gchar *cmd, *args;
          gint exit;

          /* hide menu first */
          gtk_menu_popdown (GTK_MENU (data));
          while (gtk_events_pending ())
            gtk_main_iteration ();

          /* run command */
          args = get_data_as_string (&iter);
          cmd = g_strdup_printf ("%s del %s", options.list_data.row_action, args);
          g_free (args);
          g_spawn_command_line_sync (cmd, NULL, NULL, &exit, NULL);
          g_free (cmd);
          if (exit == 0)
            gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
        }
      else
        gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
    }
Victor Ananjesky's avatar
Victor Ananjesky committed
897 898 899
}

static void
Victor Ananjevsky's avatar
Victor Ananjevsky committed
900
copy_row_cb (GtkMenuItem *item, gpointer data)
Victor Ananjesky's avatar
Victor Ananjesky committed
901 902 903 904 905 906 907
{
  GtkTreeIter iter, new_iter;
  GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (list_view));
  GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (list_view));

  if (gtk_tree_selection_get_selected (sel, NULL, &iter))
    {
Victor Ananjevsky's avatar
Victor Ananjevsky committed
908
      gint i;
Victor Ananjesky's avatar
Victor Ananjesky committed
909 910 911

      gtk_list_store_insert_after (GTK_LIST_STORE (model), &new_iter, &iter);

Victor Ananjevsky's avatar
Victor Ananjevsky committed
912
      for (i = 0; i < n_cols; i++)
Victor Ananjesky's avatar
Victor Ananjesky committed
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
        {
          GdkPixbuf *pb;
          gchar *tv;
          gint64 iv;
          gfloat fv;
          gboolean bv;
          YadColumn *col = (YadColumn *) g_slist_nth_data (options.list_data.columns, i);

          switch (col->type)
            {
            case YAD_COLUMN_CHECK:
            case YAD_COLUMN_RADIO:
              gtk_tree_model_get (model, &iter, i, &bv, -1);
              gtk_list_store_set (GTK_LIST_STORE (model), &new_iter, i, bv, -1);
              break;
            case YAD_COLUMN_NUM:
            case YAD_COLUMN_SIZE:
            case YAD_COLUMN_BAR:
              gtk_tree_model_get (model, &iter, i, &iv, -1);
              gtk_list_store_set (GTK_LIST_STORE (model), &new_iter, i, iv, -1);
              break;
            case YAD_COLUMN_FLOAT:
              gtk_tree_model_get (model, &iter, i, &fv, -1);
              gtk_list_store_set (GTK_LIST_STORE (model), &new_iter, i, fv, -1);
              break;
            case YAD_COLUMN_IMAGE:
              gtk_tree_model_get (model, &iter, i, &pb, -1);
              gtk_list_store_set (GTK_LIST_STORE (model), &new_iter, i, pb, -1);
              break;
            default:
              gtk_tree_model_get (model, &iter, i, &tv, -1);
              gtk_list_store_set (GTK_LIST_STORE (model), &new_iter, i, tv, -1);
              break;
            }
        }
    }
}

static gboolean
Victor Ananjevsky's avatar
Victor Ananjevsky committed
952
popup_menu_cb (GtkWidget *w, GdkEventButton *ev, gpointer data)
Victor Ananjesky's avatar
Victor Ananjesky committed
953 954 955 956 957 958 959 960 961
{
  static GtkWidget *menu = NULL;
  if (ev->button == 3)
    {
      GtkWidget *item;

      if (menu == NULL)
        {
          menu = gtk_menu_new ();
962
          gtk_menu_set_reserve_toggle_size (GTK_MENU (menu), FALSE);
Victor Ananjesky's avatar
Victor Ananjesky committed
963

964
          item = gtk_menu_item_new_with_label (_("Add row"));
Victor Ananjesky's avatar
Victor Ananjesky committed
965 966 967 968
          gtk_widget_show (item);
          gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
          g_signal_connect (G_OBJECT (item), "activate", G_CALLBACK (add_row_cb), menu);

969
          item = gtk_menu_item_new_with_label (_("Delete row"));
Victor Ananjesky's avatar
Victor Ananjesky committed
970 971 972 973
          gtk_widget_show (item);
          gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
          g_signal_connect (G_OBJECT (item), "activate", G_CALLBACK (del_row_cb), menu);

974 975 976 977 978 979 980 981
          if (options.list_data.row_action)
            {
              item = gtk_menu_item_new_with_label (_("Edit row"));
              gtk_widget_show (item);
              gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
              g_signal_connect (G_OBJECT (item), "activate", G_CALLBACK (del_row_cb), menu);
            }

982
          item = gtk_menu_item_new_with_label (_("Duplicate row"));
Victor Ananjesky's avatar
Victor Ananjesky committed
983 984 985 986 987 988
          gtk_widget_show (item);
          gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
          g_signal_connect (G_OBJECT (item), "activate", G_CALLBACK (copy_row_cb), menu);

          gtk_widget_show (menu);
        }
989
      gtk_menu_popup_at_pointer (GTK_MENU (menu), NULL);
Victor Ananjesky's avatar
Victor Ananjesky committed
990 991 992 993 994
    }
  return FALSE;
}

static gboolean
Victor Ananjevsky's avatar
Victor Ananjevsky committed
995
row_sep_func (GtkTreeModel *m, GtkTreeIter *it, gpointer data)
Victor Ananjesky's avatar
Victor Ananjesky committed
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
{
  gchar *name;

  if (!options.list_data.sep_value)
    return FALSE;

  gtk_tree_model_get (m, it, options.list_data.sep_column - 1, &name, -1);
  return (strcmp (name, options.list_data.sep_value) == 0);
}

static inline void
parse_cols_props ()
{
  /* set editable property for columns */
  if (options.common_data.editable)
    {
      if (options.list_data.editable_cols)
        {
          gchar **cnum;
          guint i = 0;

          cnum = g_strsplit (options.list_data.editable_cols, ",", -1);

          while (cnum[i])
            {
              gint num = atoi (cnum[i]);
              if (num)
                {
                  YadColumn *col = (YadColumn *) g_slist_nth_data (options.list_data.columns, num - 1);
                  if (col)
                    col->editable = TRUE;
                }
              i++;
            }
          g_strfreev (cnum);
        }
      else
        {
          GSList *c;
          for (c = options.list_data.columns; c; c = c->next)
            {
              YadColumn *col = (YadColumn *) c->data;
              col->editable = TRUE;
            }
        }
    }

  /* set wrap property for columns */
  if (options.list_data.wrap_width > 0)
    {
      if (options.list_data.wrap_cols)
        {
          gchar **cnum;
          guint i = 0;

          cnum = g_strsplit (options.list_data.wrap_cols, ",", -1);

          while (cnum[i])
            {
              gint num = atoi (cnum[i]);
              if (num)
                {
                  YadColumn *col = (YadColumn *) g_slist_nth_data (options.list_data.columns, num - 1);
                  if (col)
                    col->wrap = TRUE;
                }
              i++;
            }
          g_strfreev (cnum);
        }
      else
        {
          GSList *c;
          for (c = options.list_data.columns; c; c = c->next)
            {
              YadColumn *col = (YadColumn *) c->data;
              col->wrap = TRUE;
            }
        }
    }

  /* set ellipsize property for columns */
  if (options.list_data.ellipsize)
    {
      if (options.list_data.ellipsize_cols)
        {
          gchar **cnum;
          guint i = 0;

          cnum = g_strsplit (options.list_data.ellipsize_cols, ",", -1);

          while (cnum[i])
            {
              gint num = atoi (cnum[i]);
              if (num)
                {
                  YadColumn *col = (YadColumn *) g_slist_nth_data (options.list_data.columns, num - 1);
                  if (col)
                    col->ellipsize = TRUE;
                }
              i++;
            }
          g_strfreev (cnum);
        }
      else
        {
          GSList *c;
          for (c = options.list_data.columns; c; c = c->next)
            {
              YadColumn *col = (YadColumn *) c->data;
              col->ellipsize = TRUE;
            }
        }
    }
}

GtkWidget *
Victor Ananjevsky's avatar
Victor Ananjevsky committed
1113
list_create_widget (GtkWidget *dlg)
Victor Ananjesky's avatar
Victor Ananjesky committed
1114 1115 1116 1117 1118 1119
{
  GtkWidget *w;
  GtkTreeModel *model;

  fore_col = back_col = font_col = -1;

Victor Ananjevsky's avatar
Victor Ananjevsky committed
1120 1121
  n_cols = g_slist_length (options.list_data.columns);
  if (n_cols == 0)
Victor Ananjesky's avatar
Victor Ananjesky committed
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
    {
      g_printerr (_("No column titles specified for List dialog.\n"));
      return NULL;
    }

  parse_cols_props ();

  /* create widget */
  w = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (w), GTK_SHADOW_ETCHED_IN);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (w), options.hscroll_policy, options.vscroll_policy);

Victor Ananjevsky's avatar
Victor Ananjevsky committed
1134
  model = create_model ();
Victor Ananjesky's avatar
Victor Ananjesky committed
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144

  list_view = gtk_tree_view_new_with_model (model);
  gtk_widget_set_name (list_view, "yad-list-widget");
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (list_view), !options.list_data.no_headers);
  gtk_tree_view_set_grid_lines (GTK_TREE_VIEW (list_view), options.list_data.grid_lines);
  gtk_tree_view_set_reorderable (GTK_TREE_VIEW (list_view), options.common_data.editable);
  g_object_unref (model);

  gtk_container_add (GTK_CONTAINER (w), list_view);

Victor Ananjevsky's avatar
Victor Ananjevsky committed
1145
  add_columns ();
Victor Ananjesky's avatar
Victor Ananjesky committed
1146 1147 1148 1149 1150 1151 1152

  /* add popup menu */
  if (options.common_data.editable)
    g_signal_connect_swapped (G_OBJECT (list_view), "button_press_event", G_CALLBACK (popup_menu_cb), NULL);

  /* add tooltip column */
  if (options.list_data.tooltip_column > 0)
1153
    {
1154
      if (options.list_data.simple_tips || options.data.no_markup)
1155 1156 1157 1158 1159 1160 1161
        {
          gtk_widget_set_has_tooltip (list_view, TRUE);
          g_signal_connect (G_OBJECT (list_view), "query-tooltip", G_CALLBACK (tooltip_cb), NULL);
        }
      else
        gtk_tree_view_set_tooltip_column (GTK_TREE_VIEW (list_view), options.list_data.tooltip_column - 1);
    }
Victor Ananjesky's avatar
Victor Ananjesky committed
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197

  /* set search function for regex search */
  if (options.list_data.search_column != -1 && options.list_data.regex_search)
    {
      YadColumn *col = (YadColumn *) g_slist_nth_data (options.list_data.columns,
                                                       options.list_data.search_column);

      if (col->type == YAD_COLUMN_TEXT)
        gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (list_view), regex_search, NULL, NULL);
    }

  /* add row separator function */
  if (options.list_data.sep_column > 0)
    gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (list_view), row_sep_func, NULL, NULL);

  if (options.list_data.no_selection)
    {
      GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (list_view));
      gtk_tree_selection_set_mode (sel, GTK_SELECTION_NONE);
      gtk_widget_set_can_focus (list_view, FALSE);
    }
  else
    {
      GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (list_view));

      if (options.common_data.multi && !options.list_data.checkbox && !options.list_data.radiobox)
        gtk_tree_selection_set_mode (sel, GTK_SELECTION_MULTIPLE);

      if (!options.common_data.multi && options.list_data.select_action)
        select_hndl = g_signal_connect (G_OBJECT (sel), "changed", G_CALLBACK (select_cb), NULL);

      g_signal_connect (G_OBJECT (list_view), "row-activated", G_CALLBACK (double_click_cb), dlg);
      g_signal_connect (G_OBJECT (list_view), "key-press-event", G_CALLBACK (list_activate_cb), dlg);
    }

  /* load data */
Victor Ananjevsky's avatar
Victor Ananjevsky committed
1198
  fill_data ();
Victor Ananjesky's avatar
Victor Ananjesky committed
1199 1200 1201 1202 1203

  return w;
}

static void
Victor Ananjevsky's avatar
Victor Ananjevsky committed
1204
print_col (GtkTreeModel *model, GtkTreeIter *iter, gint num)
Victor Ananjesky's avatar
Victor Ananjesky committed
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
{
  YadColumn *col = (YadColumn *) g_slist_nth_data (options.list_data.columns, num);

  /* don't print attributes */
  if (col->type == YAD_COLUMN_ATTR_FORE || col->type == YAD_COLUMN_ATTR_BACK || col->type == YAD_COLUMN_ATTR_FONT)
    return;

  switch (col->type)
    {
    case YAD_COLUMN_CHECK:
    case YAD_COLUMN_RADIO:
      {
        gboolean bval;
        gtk_tree_model_get (model, iter, num, &bval, -1);
        if (options.common_data.quoted_output)
1220
          g_printf ("'%s'", print_bool_val (bval));
Victor Ananjesky's avatar
Victor Ananjesky committed
1221
        else
1222
          g_printf ("%s", print_bool_val (bval));
Victor Ananjesky's avatar
Victor Ananjesky committed
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
        break;
      }
    case YAD_COLUMN_NUM:
    case YAD_COLUMN_SIZE:
    case YAD_COLUMN_BAR:
      {
        gint64 nval;
        gtk_tree_model_get (model, iter, num, &nval, -1);
        if (options.common_data.quoted_output)
          g_printf ("'%ld'", (long) nval);
        else
          g_printf ("%ld", (long) nval);
        break;
      }
    case YAD_COLUMN_FLOAT:
      {
        gdouble nval;
        gtk_tree_model_get (model, iter, num, &nval, -1);
        if (options.common_data.quoted_output)
          g_printf ("'%.*f'", options.common_data.float_precision, nval);
        else
          g_printf ("%.*f", options.common_data.float_precision, nval);
        break;
      }
    case YAD_COLUMN_IMAGE:
      if (options.common_data.quoted_output)
        g_printf ("''");
      break;
    default:
      {
        gchar *val;
        gtk_tree_model_get (model, iter, num, &val, -1);
        if (options.common_data.quoted_output)
          {
            gchar *buf = g_shell_quote (val);
            g_printf ("%s", buf);
            g_free (buf);
          }
        else
1262
          g_printf ("%s", val ? val : "");
Victor Ananjesky's avatar
Victor Ananjesky committed
1263 1264 1265 1266 1267 1268 1269
        break;
      }
    }
  g_printf ("%s", options.common_data.separator);
}

static void
Victor Ananjevsky's avatar
Victor Ananjevsky committed
1270
print_selected (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
Victor Ananjesky's avatar
Victor Ananjesky committed
1271
{
Victor Ananjevsky's avatar
Victor Ananjevsky committed
1272
  gint i,col;
Victor Ananjesky's avatar
Victor Ananjesky committed
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286

  col = options.list_data.print_column;

  if (col && col <= n_cols)
    print_col (model, iter, col - 1);
  else
    {
      for (i = 0; i < n_cols; i++)
        print_col (model, iter, i);
    }
  g_printf ("\n");
}

static void
Victor Ananjevsky's avatar
Victor Ananjevsky committed
1287
print_all (GtkTreeModel *model)
Victor Ananjesky's avatar
Victor Ananjesky committed
1288 1289
{
  GtkTreeIter iter;
Victor Ananjevsky's avatar
Victor Ananjevsky committed
1290
  gint i;
Victor Ananjesky's avatar
Victor Ananjesky committed
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352

  if (gtk_tree_model_get_iter_first (model, &iter))
    {
      do
        {
          for (i = 0; i < n_cols; i++)
            print_col (model, &iter, i);
          g_printf ("\n");
        }
      while (gtk_tree_model_iter_next (model, &iter));
    }
}

void
list_print_result (void)
{
  GtkTreeModel *model;
  gint col = options.list_data.print_column;

  model = gtk_tree_view_get_model (GTK_TREE_VIEW (list_view));

  if (options.list_data.print_all)
    {
      print_all (model);
      return;
    }

  if (options.list_data.checkbox || options.list_data.radiobox)
    {
      // don't check in cycle
      if (col > 0)
        {
          GtkTreeIter iter;

          if (gtk_tree_model_get_iter_first (model, &iter))
            {
              do
                {
                  gboolean chk;
                  gtk_tree_model_get (model, &iter, 0, &chk, -1);
                  if (chk)
                    {
                      print_col (model, &iter, col - 1);
                      g_printf ("\n");
                    }
                }
              while (gtk_tree_model_iter_next (model, &iter));
            }
        }
      else
        {
          GtkTreeIter iter;

          if (gtk_tree_model_get_iter_first (model, &iter))
            {
              do
                {
                  gboolean chk;
                  gtk_tree_model_get (model, &iter, 0, &chk, -1);
                  if (chk)
                    {
                      gint i;
Victor Ananjevsky's avatar
Victor Ananjevsky committed
1353
                      for (i = 0; i < n_cols; i++)
Victor Ananjesky's avatar
Victor Ananjesky committed
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
                        print_col (model, &iter, i);
                      g_printf ("\n");
                    }
                }
              while (gtk_tree_model_iter_next (model, &iter));
            }
        }
    }
  else
    {
      GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (list_view));
      gtk_tree_selection_selected_foreach (sel, print_selected, NULL);
    }
}