You need to sign in or sign up before continuing.
main.c 27.6 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-2022, Victor Ananjevsky <victor@sanana.kiev.ua>
Victor Ananjesky's avatar
Victor Ananjesky committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <locale.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>

#ifndef G_OS_WIN32
# include <sys/shm.h>
# include <gdk/gdkx.h>
#endif

#include "yad.h"

YadOptions options;
37 38
GtkIconTheme *yad_icon_theme;

39 40
#ifndef STANDALONE
GSettings *settings;
41
GSettings *sv_settings;
42 43
#endif

44 45 46
GdkPixbuf *big_fallback_image = NULL;
GdkPixbuf *small_fallback_image = NULL;

47 48 49
/* global flag for temporary disable ESC handling (needed for searchbar) */
gboolean ignore_esc = FALSE;

Victor Ananjesky's avatar
Victor Ananjesky committed
50
static GtkWidget *dialog = NULL;
51
static GtkWidget *text = NULL;
Victor Ananjesky's avatar
Victor Ananjesky committed
52 53 54

static gint ret = YAD_RESPONSE_ESC;

55 56
static gboolean is_x11 = FALSE;

Victor Ananjesky's avatar
Victor Ananjesky committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
YadNTabs *tabs;

#ifndef G_OS_WIN32
static void
sa_usr1 (gint sig)
{
  if (options.plug != -1)
    yad_print_result ();
  else
    yad_exit (options.data.def_resp);
}

static void
sa_usr2 (gint sig)
{
  if (options.plug != -1)
    gtk_main_quit ();
  else
    yad_exit (YAD_RESPONSE_CANCEL);
}
#endif

static gboolean
keys_cb (GtkWidget *w, GdkEventKey *ev, gpointer d)
{
  if (options.plug != -1)
    return FALSE;

  switch (ev->keyval)
    {
87 88 89 90 91 92 93
    case GDK_KEY_F1:
      if (options.data.f1_action)
        {
          run_command_async (options.data.f1_action);
          return TRUE;
        }
      break;
Victor Ananjesky's avatar
Victor Ananjesky committed
94
    case GDK_KEY_Escape:
95 96 97 98 99 100 101 102 103
      if (!ignore_esc)
        {
          if (options.data.escape_ok)
            yad_exit (options.data.def_resp);
          else if (!options.data.no_escape)
            yad_exit (YAD_RESPONSE_ESC);
          return TRUE;
        }
      break;
Victor Ananjesky's avatar
Victor Ananjesky committed
104 105 106 107 108 109 110
    case GDK_KEY_Return:
    case GDK_KEY_KP_Enter:
      if (ev->state & GDK_CONTROL_MASK)
        {
          yad_exit (options.data.def_resp);
          return TRUE;
        }
111
      break;
Victor Ananjesky's avatar
Victor Ananjesky committed
112
     }
113

Victor Ananjesky's avatar
Victor Ananjesky committed
114 115 116 117 118 119 120
   return FALSE;
}

static void
btn_cb (GtkWidget *b, gchar *cmd)
{
  if (cmd)
121
    run_command_async (cmd);
Victor Ananjesky's avatar
Victor Ananjesky committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  else
    {
      gint resp = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (b), "resp"));
      yad_exit (resp);
    }
}

static gboolean
timeout_cb (gpointer data)
{
  static guint count = 1;
  GtkWidget *w = (GtkWidget *) data;

  if (options.data.timeout < count)
    {
      yad_exit (YAD_RESPONSE_TIMEOUT);
      return FALSE;
    }

  if (w)
    {
      gdouble percent = ((gdouble) options.data.timeout - count) / (gdouble) options.data.timeout;
      gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (w), percent);
145
#ifndef STANDALONE
146
      if (g_settings_get_boolean (settings, "show-remain"))
147 148 149
#else
      if (SHOW_REMAIN)
#endif
Victor Ananjesky's avatar
Victor Ananjesky committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        {
          gchar *lbl = g_strdup_printf (_("%d sec"), options.data.timeout - count);
          gtk_progress_bar_set_text (GTK_PROGRESS_BAR (w), lbl);
          g_free (lbl);
        }
    }

  count++;

  return TRUE;
}

static gboolean
unfocus_cb (GtkWidget *w, GdkEventFocus *ev, gpointer d)
{
  if (options.data.close_on_unfocus)
    gtk_main_quit ();
  return FALSE;
}

170 171 172 173 174 175 176
static gboolean
link_cb (GtkLabel *l, gchar *uri, gpointer d)
{
  open_uri (uri);
  return TRUE;
}

Victor Ananjesky's avatar
Victor Ananjesky committed
177 178 179
void
yad_exit (gint id)
{
180
  if ((options.mode == YAD_MODE_FILE) && !(id & 1))
Victor Ananjesky's avatar
Victor Ananjesky committed
181 182 183 184 185 186 187 188 189 190 191 192 193
    {
      /* show custom confirmation dialog */
      if (!file_confirm_overwrite (dialog))
        return;
    }

  ret = id;
  gtk_main_quit ();
}

static GtkWidget *
create_layout (GtkWidget *dlg)
{
194
  GtkWidget *image, *mw, *imw, *layout, *exp, *box;
Victor Ananjesky's avatar
Victor Ananjesky committed
195

196
  layout = image = mw = exp = NULL;
Victor Ananjesky's avatar
Victor Ananjesky committed
197 198 199 200 201 202

  /* create image */
  if (options.data.dialog_image)
    {
      GdkPixbuf *pb = NULL;

203
      pb = get_pixbuf (options.data.dialog_image, YAD_BIG_ICON, FALSE);
Victor Ananjesky's avatar
Victor Ananjesky committed
204 205 206 207 208
      image = gtk_image_new_from_pixbuf (pb);
      if (pb)
        g_object_unref (pb);

      gtk_widget_set_name (image, "yad-dialog-image");
209 210
      gtk_widget_set_halign (image, GTK_ALIGN_CENTER);
      gtk_widget_set_valign (image, GTK_ALIGN_START);
Victor Ananjesky's avatar
Victor Ananjesky committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224
    }

  /* create text label */
  if (options.data.dialog_text)
    {
      /* for dnd's tooltip we don't need text label */
      if (options.mode != YAD_MODE_DND || !options.dnd_data.tooltip)
        {
          gchar *buf = g_strcompress (options.data.dialog_text);

          text = gtk_label_new (NULL);
          gtk_widget_set_name (text, "yad-dialog-label");
          gtk_label_set_line_wrap (GTK_LABEL (text), TRUE);
          gtk_label_set_selectable (GTK_LABEL (text), options.data.selectable_labels);
225
          gtk_widget_set_state_flags (text, GTK_STATE_FLAG_NORMAL, FALSE);
226
          gtk_widget_set_can_focus (text, FALSE);
227 228

          if (options.data.text_width > 0)
229
            gtk_label_set_width_chars (GTK_LABEL (text), options.data.text_width);
230

231 232 233 234 235 236 237 238
          /* set label align and justification */
          switch (options.data.text_align)
            {
            case GTK_JUSTIFY_LEFT:
            case GTK_JUSTIFY_FILL:
              gtk_label_set_xalign (GTK_LABEL (text), 0.0);
              break;
            case GTK_JUSTIFY_RIGHT:
239
              gtk_label_set_xalign (GTK_LABEL (text), 1.0);
240 241
              break;
            case GTK_JUSTIFY_CENTER:
242
              gtk_label_set_xalign (GTK_LABEL (text), 0.5);
243 244 245 246
              break;
            }
          gtk_label_set_justify (GTK_LABEL (text), options.data.text_align);

247
          if (!options.data.no_markup)
248 249 250 251
            {
              gtk_label_set_markup (GTK_LABEL (text), buf);
              g_signal_connect (G_OBJECT (text), "activate-link", G_CALLBACK (link_cb), NULL);
            }
252 253 254
          else
            gtk_label_set_text (GTK_LABEL (text), buf);
          g_free (buf);
Victor Ananjesky's avatar
Victor Ananjesky committed
255 256 257 258 259 260
        }
    }

  /* create main widget */
  switch (options.mode)
    {
261 262 263
    case YAD_MODE_APP:
      mw = app_create_widget (dlg);
      break;
Victor Ananjesky's avatar
Victor Ananjesky committed
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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    case YAD_MODE_CALENDAR:
      mw = calendar_create_widget (dlg);
      break;
    case YAD_MODE_COLOR:
      mw = color_create_widget (dlg);
      break;
    case YAD_MODE_ENTRY:
      mw = entry_create_widget (dlg);
      break;
    case YAD_MODE_FILE:
      mw = file_create_widget (dlg);
      break;
    case YAD_MODE_FONT:
      mw = font_create_widget (dlg);
      break;
    case YAD_MODE_FORM:
      mw = form_create_widget (dlg);
      break;
#ifdef HAVE_HTML
    case YAD_MODE_HTML:
      mw = html_create_widget (dlg);
      break;
#endif
    case YAD_MODE_ICONS:
      mw = icons_create_widget (dlg);
      break;
    case YAD_MODE_LIST:
      mw = list_create_widget (dlg);
      break;
    case YAD_MODE_NOTEBOOK:
      if (options.plug == -1)
        mw = notebook_create_widget (dlg);
      break;
    case YAD_MODE_PANED:
      if (options.plug == -1)
        mw = paned_create_widget (dlg);
      break;
    case YAD_MODE_PICTURE:
      mw = picture_create_widget (dlg);
      break;
    case YAD_MODE_PROGRESS:
      mw = progress_create_widget (dlg);
      break;
    case YAD_MODE_SCALE:
      mw = scale_create_widget (dlg);
      break;
    case YAD_MODE_TEXTINFO:
      mw = text_create_widget (dlg);
      break;
    default: ;
    }

  /* add expander */
  if (mw && options.data.expander)
    {
Victor Ananjevsky's avatar
Victor Ananjevsky committed
319
      imw = exp = gtk_expander_new_with_mnemonic (options.data.expander);
Victor Ananjesky's avatar
Victor Ananjesky committed
320
      gtk_expander_set_expanded (GTK_EXPANDER (exp), FALSE);
Victor Ananjevsky's avatar
Victor Ananjevsky committed
321
      gtk_container_add (GTK_CONTAINER (exp), mw);
Victor Ananjesky's avatar
Victor Ananjesky committed
322 323 324 325 326
    }
  else
    imw = mw;

  /* create layout */
327 328
  layout = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
Victor Ananjesky's avatar
Victor Ananjesky committed
329

330 331 332 333
  if (image)
    gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 2);
  if (text)
    gtk_box_pack_start (GTK_BOX (box), text, TRUE, TRUE, 2);
Victor Ananjesky's avatar
Victor Ananjesky committed
334

335 336
  gtk_box_pack_start (GTK_BOX (layout), box, FALSE, FALSE, 0);
  if (imw)
337 338 339 340 341 342
    {
      if (options.mode == YAD_MODE_ENTRY)
        gtk_box_pack_start (GTK_BOX (layout), imw, TRUE, FALSE, 0);
      else
        gtk_box_pack_start (GTK_BOX (layout), imw, TRUE, TRUE, 0);
    }
Victor Ananjesky's avatar
Victor Ananjesky committed
343 344 345 346 347 348 349

  if (options.mode == YAD_MODE_DND)
    dnd_init (layout);

  return layout;
}

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 376 377 378 379 380 381
static void
realize_cb (GtkWidget *dlg, gpointer d)
{
  gint cw, ch;
  /* get current window size for gtk_window_resize */
  gtk_window_get_size (GTK_WINDOW (dlg), &cw, &ch);
  if (options.data.width == -1)
    options.data.width = cw;
  if (options.data.height == -1)
    options.data.height = ch;

  gtk_window_resize (GTK_WINDOW (dlg), options.data.width, options.data.height);
  gtk_window_set_resizable (GTK_WINDOW (dlg), !options.data.fixed);

  if (options.data.use_posx || options.data.use_posy)
    {
      gint ww, wh, sw, sh;
      gtk_window_get_size (GTK_WINDOW (dlg), &ww, &wh);
      gdk_window_get_geometry (gdk_get_default_root_window (), NULL, NULL, &sw, &sh);
      /* place window to specified coordinates */
      if (!options.data.use_posx)
        gtk_window_get_position (GTK_WINDOW (dlg), &options.data.posx, NULL);
      if (!options.data.use_posy)
        gtk_window_get_position (GTK_WINDOW (dlg), NULL, &options.data.posy);
      if (options.data.posx < 0)
        options.data.posx = sw - ww + options.data.posx;
      if (options.data.posy < 0)
        options.data.posy = sh - wh + options.data.posy;
      gtk_window_move (GTK_WINDOW (dlg), options.data.posx, options.data.posy);
    }
}

Victor Ananjesky's avatar
Victor Ananjesky committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
static GtkWidget *
create_dialog (void)
{
  GtkWidget *dlg, *vbox, *layout;

  /* create dialog window */
  dlg = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  if (options.data.splash)
    gtk_window_set_type_hint (GTK_WINDOW (dlg), GDK_WINDOW_TYPE_HINT_SPLASHSCREEN);
  gtk_window_set_title (GTK_WINDOW (dlg), options.data.dialog_title);
  gtk_widget_set_name (dlg, "yad-dialog-window");

  g_signal_connect (G_OBJECT (dlg), "delete-event", G_CALLBACK (gtk_main_quit), NULL);
  g_signal_connect (G_OBJECT (dlg), "key-press-event", G_CALLBACK (keys_cb), NULL);
  g_signal_connect (G_OBJECT (dlg), "focus-out-event", G_CALLBACK (unfocus_cb), NULL);

  /* set window icon */
  if (options.data.window_icon)
    {
      if (g_file_test (options.data.window_icon, G_FILE_TEST_EXISTS))
        gtk_window_set_icon_from_file (GTK_WINDOW (dlg), options.data.window_icon, NULL);
      else
        gtk_window_set_icon_name (GTK_WINDOW (dlg), options.data.window_icon);
    }
406 407
  else
    gtk_window_set_icon_name (GTK_WINDOW (dlg), "yad");
Victor Ananjesky's avatar
Victor Ananjesky committed
408 409 410 411 412 413 414

  /* set window borders */
  if (options.data.borders < 0)
    options.data.borders = 2;
  gtk_container_set_border_width (GTK_CONTAINER (dlg), (guint) options.data.borders);

  /* set window size and position */
415
  if (!options.data.maximized && !options.data.fullscreen)
Victor Ananjesky's avatar
Victor Ananjesky committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 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 474 475 476 477 478 479 480 481 482 483
    {
      if (options.data.center)
        gtk_window_set_position (GTK_WINDOW (dlg), GTK_WIN_POS_CENTER_ALWAYS);
      else if (options.data.mouse)
        gtk_window_set_position (GTK_WINDOW (dlg), GTK_WIN_POS_MOUSE);
    }

  /* set window behavior */
  if (options.data.sticky)
    gtk_window_stick (GTK_WINDOW (dlg));
  gtk_window_set_keep_above (GTK_WINDOW (dlg), options.data.ontop);
  gtk_window_set_decorated (GTK_WINDOW (dlg), !options.data.undecorated);
  gtk_window_set_skip_taskbar_hint (GTK_WINDOW (dlg), options.data.skip_taskbar);
  gtk_window_set_skip_pager_hint (GTK_WINDOW (dlg), options.data.skip_taskbar);
  gtk_window_set_accept_focus (GTK_WINDOW (dlg), options.data.focus);

  /* create box */
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
  gtk_container_add (GTK_CONTAINER (dlg), vbox);

  layout = create_layout (dlg);

  /* create timeout indicator widget */
  if (options.data.timeout)
    {
      GtkWidget *cbox = NULL, *topb = NULL;

      if (G_LIKELY (options.data.to_indicator) && strcasecmp (options.data.to_indicator, "none") != 0)
        {
          topb = gtk_progress_bar_new ();
          gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (topb), 1.0);
          gtk_widget_set_name (topb, "yad-timeout-indicator");
        }

      /* add timeout indicator */
      if (topb)
        {
          if (strcasecmp (options.data.to_indicator, "top") == 0)
            {
              gtk_orientable_set_orientation (GTK_ORIENTABLE (topb), GTK_ORIENTATION_HORIZONTAL);
              cbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
              gtk_box_pack_start (GTK_BOX (cbox), topb, FALSE, FALSE, 2);
              gtk_box_pack_end (GTK_BOX (cbox), layout, TRUE, TRUE, 0);
            }
          else if (strcasecmp (options.data.to_indicator, "bottom") == 0)
            {
              gtk_orientable_set_orientation (GTK_ORIENTABLE (topb), GTK_ORIENTATION_HORIZONTAL);
              cbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
              gtk_box_pack_start (GTK_BOX (cbox), layout, TRUE, TRUE, 0);
              gtk_box_pack_end (GTK_BOX (cbox), topb, FALSE, FALSE, 2);
            }
          else if (strcasecmp (options.data.to_indicator, "left") == 0)
            {
              gtk_orientable_set_orientation (GTK_ORIENTABLE (topb), GTK_ORIENTATION_VERTICAL);
              gtk_progress_bar_set_inverted (GTK_PROGRESS_BAR (topb), TRUE);
              cbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
              gtk_box_pack_start (GTK_BOX (cbox), topb, FALSE, FALSE, 2);
              gtk_box_pack_end (GTK_BOX (cbox), layout, TRUE, TRUE, 0);
            }
          else if (strcasecmp (options.data.to_indicator, "right") == 0)
            {
              gtk_orientable_set_orientation (GTK_ORIENTABLE (topb), GTK_ORIENTATION_VERTICAL);
              gtk_progress_bar_set_inverted (GTK_PROGRESS_BAR (topb), TRUE);
              cbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
              gtk_box_pack_start (GTK_BOX (cbox), layout, TRUE, TRUE, 0);
              gtk_box_pack_end (GTK_BOX (cbox), topb, FALSE, FALSE, 2);
            }

484
#ifndef STANDALONE
485
          if (g_settings_get_boolean (settings, "show-remain"))
486 487 488
#else
          if (SHOW_REMAIN)
#endif
Victor Ananjesky's avatar
Victor Ananjesky committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
            {
              gchar *lbl = g_strdup_printf (_("%d sec"), options.data.timeout);
              gtk_progress_bar_set_show_text (GTK_PROGRESS_BAR (topb), TRUE);
              gtk_progress_bar_set_text (GTK_PROGRESS_BAR (topb), lbl);
              g_free (lbl);
            }
        }
      else
        {
          cbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
          gtk_box_pack_start (GTK_BOX (cbox), layout, TRUE, TRUE, 0);
        }

      if (cbox)
        gtk_box_pack_start (GTK_BOX (vbox), cbox, TRUE, TRUE, 0);

      /* set timeout handler */
      g_timeout_add_seconds (1, timeout_cb, topb);
    }
  else
    gtk_box_pack_start (GTK_BOX (vbox), layout, TRUE, TRUE, 0);

#ifdef HAVE_HTML
  /* enable no-buttons mode if --browser is specified and sets no custom buttons */
  if (options.mode == YAD_MODE_HTML && options.html_data.browser && !options.data.buttons)
    options.data.no_buttons = TRUE;
#endif

  if (!options.data.no_buttons)
    {
      GtkWidget *btn;
      /* create buttons container */
      GtkWidget *bbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
522
      gtk_box_set_homogeneous (GTK_BOX (bbox), TRUE);
Victor Ananjesky's avatar
Victor Ananjesky committed
523 524 525 526 527 528 529 530 531 532 533 534 535
      gtk_container_set_border_width (GTK_CONTAINER (bbox), 2);
      gtk_box_set_spacing (GTK_BOX (bbox), 5);
      gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), options.data.buttons_layout);

      /* add buttons */
      if (options.data.buttons)
        {
          GSList *tmp = options.data.buttons;
          do
            {
              YadButton *b = (YadButton *) tmp->data;

              btn = gtk_button_new ();
536
              gtk_container_add (GTK_CONTAINER (btn), get_label (b->name, 2, btn));
Victor Ananjesky's avatar
Victor Ananjesky committed
537 538
              g_object_set_data (G_OBJECT (btn), "resp", GINT_TO_POINTER (b->response));
              g_signal_connect (G_OBJECT (btn), "clicked", G_CALLBACK (btn_cb), b->cmd);
539
              gtk_box_pack_start (GTK_BOX (bbox), btn, TRUE, TRUE, 0);
Victor Ananjesky's avatar
Victor Ananjesky committed
540 541 542 543 544 545 546

              tmp = tmp->next;
            }
          while (tmp != NULL);
        }
      else
        {
547
          if (options.mode == YAD_MODE_PROGRESS || options.mode == YAD_MODE_DND || options.mode == YAD_MODE_PICTURE)
Victor Ananjesky's avatar
Victor Ananjesky committed
548 549
            {
              /* add close button */
550
              btn = gtk_button_new ();
551
              gtk_container_add (GTK_CONTAINER (btn), get_label ("yad-close", 2, btn));
Victor Ananjesky's avatar
Victor Ananjesky committed
552 553
              g_object_set_data (G_OBJECT (btn), "resp", GINT_TO_POINTER (YAD_RESPONSE_OK));
              g_signal_connect (G_OBJECT (btn), "clicked", G_CALLBACK (btn_cb), NULL);
554
              gtk_box_pack_start (GTK_BOX (bbox), btn, TRUE, TRUE, 0);
Victor Ananjesky's avatar
Victor Ananjesky committed
555 556 557 558

            }
          else
            {
559
              /* add cancel button */
560
              btn = gtk_button_new ();
561
              gtk_container_add (GTK_CONTAINER (btn), get_label ("yad-cancel", 2, btn));
562 563
              g_object_set_data (G_OBJECT (btn), "resp", GINT_TO_POINTER (YAD_RESPONSE_CANCEL));
              g_signal_connect (G_OBJECT (btn), "clicked", G_CALLBACK (btn_cb), NULL);
564
              gtk_box_pack_start (GTK_BOX (bbox), btn, TRUE, TRUE, 0);
565 566

              /*add ok button */
567
              btn = gtk_button_new ();
568
              gtk_container_add (GTK_CONTAINER (btn), get_label ("yad-ok", 2, btn));
569 570
              g_object_set_data (G_OBJECT (btn), "resp", GINT_TO_POINTER (YAD_RESPONSE_OK));
              g_signal_connect (G_OBJECT (btn), "clicked", G_CALLBACK (btn_cb), NULL);
571
              gtk_box_pack_start (GTK_BOX (bbox), btn, TRUE, TRUE, 0);
Victor Ananjesky's avatar
Victor Ananjesky committed
572 573 574 575 576 577 578
            }
        }
      /* add buttons box to main window */
      gtk_box_pack_start (GTK_BOX (vbox), bbox, FALSE, FALSE, 0);
    }

  /* show widgets */
579
  gtk_widget_show_all (vbox);
Victor Ananjevsky's avatar
Victor Ananjevsky committed
580

Victor Ananjesky's avatar
Victor Ananjesky committed
581 582 583
  /* parse geometry or move window, if given. must be after showing widget */
  if (!options.data.maximized && !options.data.fullscreen)
    {
584
      parse_geometry ();
585 586
      g_signal_connect (G_OBJECT (dlg), "realize", G_CALLBACK (realize_cb), NULL);
      gtk_widget_show_all (dlg);
Victor Ananjesky's avatar
Victor Ananjesky committed
587 588 589
    }
  else
    {
590 591 592 593 594 595
      gtk_widget_show (dlg);
      /* set maximized or fixed size after showing widget */
      if (options.data.maximized)
        gtk_window_maximize (GTK_WINDOW (dlg));
      else if (options.data.fullscreen)
        gtk_window_fullscreen (GTK_WINDOW (dlg));
Victor Ananjesky's avatar
Victor Ananjesky committed
596 597 598
    }

  /* print xid */
599
  if (is_x11 && options.print_xid)
Victor Ananjesky's avatar
Victor Ananjesky committed
600 601 602 603 604 605 606 607 608 609
    {
      FILE *xf;

      if (options.xid_file)
        xf = fopen (options.xid_file, "w");
      else
        xf = stderr;

      if (xf)
        {
610
          fprintf (xf, "0x%lX\n", GDK_WINDOW_XID (gtk_widget_get_window (dlg)));
Victor Ananjesky's avatar
Victor Ananjesky committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

          if (options.xid_file)
            fclose (xf);
          else
            fflush (xf);
        }
    }

  return dlg;
}

static void
create_plug (void)
{
  GtkWidget *win, *box;

  tabs = get_tabs (options.plug, FALSE);
  while (!tabs)
    {
      usleep (1000);
      tabs = get_tabs (options.plug, FALSE);
    }
Victor Ananjevsky's avatar
Victor Ananjevsky committed
633

634
  while (!tabs[0].xid)
Victor Ananjevsky's avatar
Victor Ananjevsky committed
635
    usleep (1000);
Victor Ananjesky's avatar
Victor Ananjesky committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649

  win = gtk_plug_new (0);
  /* set window borders */
  if (options.data.borders == -1)
    options.data.borders = (gint) gtk_container_get_border_width (GTK_CONTAINER (win));
  gtk_container_set_border_width (GTK_CONTAINER (win), (guint) options.data.borders);

  box = create_layout (win);
  if (box)
    gtk_container_add (GTK_CONTAINER (win), box);

  gtk_widget_show_all (win);

  /* add plug data */
650
  /* notebook/paned will count non-zero xids */
Victor Ananjesky's avatar
Victor Ananjesky committed
651 652 653 654 655 656 657 658 659 660
  tabs[options.tabnum].pid = getpid ();
  tabs[options.tabnum].xid = gtk_plug_get_id (GTK_PLUG (win));
  shmdt (tabs);
}

void
yad_print_result (void)
{
  switch (options.mode)
    {
661 662 663
    case YAD_MODE_APP:
      app_print_result ();
      break;
Victor Ananjesky's avatar
Victor Ananjesky committed
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 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
    case YAD_MODE_CALENDAR:
      calendar_print_result ();
      break;
    case YAD_MODE_COLOR:
      color_print_result ();
      break;
    case YAD_MODE_ENTRY:
      entry_print_result ();
      break;
    case YAD_MODE_FILE:
      file_print_result ();
      break;
    case YAD_MODE_FONT:
      font_print_result ();
      break;
    case YAD_MODE_FORM:
      form_print_result ();
      break;
    case YAD_MODE_LIST:
      list_print_result ();
      break;
    case YAD_MODE_NOTEBOOK:
      notebook_print_result ();
      break;
    case YAD_MODE_PANED:
      paned_print_result ();
      break;
    case YAD_MODE_SCALE:
      scale_print_result ();
      break;
    case YAD_MODE_TEXTINFO:
      text_print_result ();
      break;
    default:;
    }
}

gint
main (gint argc, gchar ** argv)
{
  GOptionContext *ctx;
  GError *err = NULL;
  gint w, h;
  gchar *str;

  setlocale (LC_ALL, "");

#ifdef ENABLE_NLS
  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);
#endif

  gtk_init (&argc, &argv);
  g_set_application_name ("YAD");
719

720
#ifndef STANDALONE
721
  settings = g_settings_new ("yad.settings");
722
  sv_settings = g_settings_new ("yad.sourceview");
723
#endif
724

725 726
  yad_icon_theme = gtk_icon_theme_get_default ();

Victor Ananjesky's avatar
Victor Ananjesky committed
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 755 756 757 758 759 760 761 762 763 764 765
  yad_options_init ();

  ctx = yad_create_context ();
  /* parse YAD_OPTIONS */
  if (g_getenv ("YAD_OPTIONS"))
    {
      gchar *cmd, **args = NULL;
      gint cnt;

      cmd = g_strdup_printf ("yad %s", g_getenv ("YAD_OPTIONS"));

      if (g_shell_parse_argv (cmd, &cnt, &args, &err))
        {
          g_option_context_parse (ctx, &cnt, &args, &err);
          if (err)
            {
              g_printerr (_("Unable to parse YAD_OPTIONS: %s\n"), err->message);
              g_error_free (err);
              err = NULL;
            }
        }
      else
        {
          g_printerr (_("Unable to parse YAD_OPTIONS: %s\n"), err->message);
          g_error_free (err);
          err = NULL;
        }

      g_free (cmd);
    }
  /* parse command line */
  g_option_context_parse (ctx, &argc, &argv, &err);
  if (err)
    {
      g_printerr (_("Unable to parse command line: %s\n"), err->message);
      return -1;
    }
  yad_set_mode ();

766 767 768 769 770 771
  /* check for current GDK backend */
#ifdef GDK_WINDOWING_X11
  if (GDK_IS_X11_DISPLAY (gdk_display_get_default ()))
    is_x11 = TRUE;
#endif

772 773
  /* parse custom css */
  if (options.css)
Victor Ananjesky's avatar
Victor Ananjesky committed
774
    {
775
      GtkCssProvider *css = gtk_css_provider_new ();
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791

      if (g_file_test (options.css, G_FILE_TEST_EXISTS))
        gtk_css_provider_load_from_path (css, options.css, NULL);
      else
        gtk_css_provider_load_from_data (css, options.css, -1, NULL);
      gtk_style_context_add_provider_for_screen (gdk_screen_get_default (), GTK_STYLE_PROVIDER (css),
                                                 GTK_STYLE_PROVIDER_PRIORITY_USER);
      g_object_unref (css);
    }
  else if (options.gtkrc_file)
    {
      GtkCssProvider *css = gtk_css_provider_new ();

      if (options.debug)
        g_printerr (_("WARNING: Using gtkrc option is deprecated, please use --css instead\n"));

Victor Ananjesky's avatar
Victor Ananjesky committed
792
      gtk_css_provider_load_from_path (css, options.gtkrc_file, NULL);
793 794 795
      gtk_style_context_add_provider_for_screen (gdk_screen_get_default (), GTK_STYLE_PROVIDER (css),
                                                 GTK_STYLE_PROVIDER_PRIORITY_USER);
      g_object_unref (css);
Victor Ananjesky's avatar
Victor Ananjesky committed
796 797 798 799
    }

  /* set default icons and icon theme */
  if (options.data.icon_theme)
800
    gtk_icon_theme_set_custom_theme (yad_icon_theme, options.data.icon_theme);
Victor Ananjesky's avatar
Victor Ananjesky committed
801
  gtk_icon_size_lookup (GTK_ICON_SIZE_DIALOG, &w, &h);
802
  big_fallback_image = gtk_icon_theme_load_icon (yad_icon_theme, "yad", MIN (w, h), GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL);
Victor Ananjesky's avatar
Victor Ananjesky committed
803
  gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &w, &h);
804
  small_fallback_image = gtk_icon_theme_load_icon (yad_icon_theme, "yad", MIN (w, h), GTK_ICON_LOOKUP_GENERIC_FALLBACK, NULL);
Victor Ananjesky's avatar
Victor Ananjesky committed
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 848 849 850 851 852 853 854

  /* correct separators */
  str = g_strcompress (options.common_data.separator);
  options.common_data.separator = str;
  str = g_strcompress (options.common_data.item_separator);
  options.common_data.item_separator = str;

  /* loads an extra arguments, if specified */
  if (options.rest_file)
    {
      GIOChannel *ioc;
      gchar *buf;
      guint len, line = 0;

      g_strfreev (options.extra_data);
      options.extra_data = NULL;

      ioc = g_io_channel_new_file (options.rest_file, "r", NULL);
      while (TRUE)
        {
          gint status = g_io_channel_read_line (ioc, &buf, NULL, NULL, NULL);

          if (status != G_IO_STATUS_NORMAL)
            break;

          /* remove \n at the end of string */
          len = strlen (buf);
          if (buf[len - 1] == '\n')
            buf[len - 1] = '\0';

          /* add line to arguments array */
          options.extra_data = g_realloc (options.extra_data, (line + 2) * sizeof (gchar *));
          options.extra_data[line] = g_strcompress (buf);
          options.extra_data[line + 1] = NULL;

          g_free (buf);
          line++;
        }
      g_io_channel_shutdown (ioc, FALSE, NULL);
    }

#ifndef G_OS_WIN32
  /* add YAD_PID variable */
  str = g_strdup_printf ("%d", getpid ());
  g_setenv ("YAD_PID", str, TRUE);
  /* set signal handlers */
  signal (SIGUSR1, sa_usr1);
  signal (SIGUSR2, sa_usr2);
#endif

855
  if (!is_x11 && options.plug != -1)
856 857 858
    {
      options.plug = -1;
      if (options.debug)
Victor Ananjevsky's avatar
Victor Ananjevsky committed
859
        g_printerr (_("WARNING: --plug mode not supported outside X11\n"));
860 861
    }

Victor Ananjesky's avatar
Victor Ananjesky committed
862 863 864 865 866 867 868 869 870
  /* plug mode */
  if (options.plug != -1)
    {
      create_plug ();
      gtk_main ();
      shmdt (tabs);
      return ret;
    }

871 872 873
  if (!is_x11)
    {
      if (options.mode == YAD_MODE_NOTEBOOK || options.mode == YAD_MODE_PANED
874
#ifdef HAVE_TRAY
875
          || options.mode == YAD_MODE_NOTIFICATION
876
#endif
877 878
         )
        {
Victor Ananjevsky's avatar
Victor Ananjevsky committed
879
          g_printerr (_("WARNING: This mode not supported outside X11\n"));
880 881
          return 1;
        }
882 883
    }

Victor Ananjesky's avatar
Victor Ananjesky committed
884 885 886 887 888 889 890 891 892 893
  switch (options.mode)
    {
    case YAD_MODE_ABOUT:
      ret = yad_about ();
      break;

    case YAD_MODE_VERSION:
      g_print ("%s (GTK+ %d.%d.%d)\n", VERSION, gtk_major_version, gtk_minor_version, gtk_micro_version);
      break;

894
#ifdef HAVE_TRAY
Victor Ananjesky's avatar
Victor Ananjesky committed
895 896 897
    case YAD_MODE_NOTIFICATION:
      ret = yad_notification_run ();
      break;
898
#endif
Victor Ananjesky's avatar
Victor Ananjesky committed
899 900 901 902 903 904 905 906

    case YAD_MODE_PRINT:
      ret = yad_print_run ();
      break;

    default:
      dialog = create_dialog ();

907 908 909
      if (is_x11)
        {
          /* add YAD_XID variable */
910
          str = g_strdup_printf ("0x%lX", GDK_WINDOW_XID (gtk_widget_get_window (dialog)));
911 912
          g_setenv ("YAD_XID", str, TRUE);
        }
Victor Ananjesky's avatar
Victor Ananjesky committed
913 914 915 916 917 918

      /* make some specific init actions */
      if (options.mode == YAD_MODE_NOTEBOOK)
        notebook_swallow_childs ();
      else if (options.mode == YAD_MODE_PANED)
        paned_swallow_childs ();
919 920
      else if (options.mode == YAD_MODE_TEXTINFO)
        text_goto_line ();
Victor Ananjesky's avatar
Victor Ananjesky committed
921 922 923 924 925 926
      else if (options.mode == YAD_MODE_PICTURE)
        {
          if (options.picture_data.size == YAD_PICTURE_FIT)
            picture_fit_to_window ();
        }

927 928 929
      if (text && options.data.selectable_labels)
        gtk_label_select_region (GTK_LABEL (text), 0, 0);

Victor Ananjesky's avatar
Victor Ananjesky committed
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
      /* run main loop */
      gtk_main ();

      /* print results */
      if (ret != YAD_RESPONSE_TIMEOUT && ret != YAD_RESPONSE_ESC)
        {
          if (options.data.always_print)
            yad_print_result ();
          else
            {
              /* standard OK button pressed */
              if (ret == YAD_RESPONSE_OK && options.data.buttons == NULL)
                yad_print_result ();
              /* custom even button pressed */
              else if (!(ret & 1))
                yad_print_result ();
            }
        }
#ifndef G_OS_WIN32
      if (options.mode == YAD_MODE_NOTEBOOK)
        notebook_close_childs ();
      else if (options.mode == YAD_MODE_PANED)
        paned_close_childs ();
      /* autokill option for progress dialog */
      if (!options.kill_parent)
        {
956
          if (options.mode == YAD_MODE_PROGRESS && options.progress_data.autokill && ret != YAD_RESPONSE_OK)
Victor Ananjesky's avatar
Victor Ananjesky committed
957 958 959 960 961 962 963 964 965 966 967 968 969
            kill (getppid (), SIGHUP);
        }
#endif
    }

#ifndef G_OS_WIN32
  /* NSIG defined in signal.h */
  if (options.kill_parent > 0 && options.kill_parent < NSIG)
    kill (getppid (), options.kill_parent);
#endif

  return ret;
}