terminal-window.c 108 KB
Newer Older
1
/*
Christian Persch's avatar
Christian Persch committed
2
3
 * Copyright © 2001 Havoc Pennington
 * Copyright © 2002 Red Hat, Inc.
Christian Persch's avatar
Christian Persch committed
4
 * Copyright © 2007, 2008, 2009, 2011, 2017 Christian Persch
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
10
 * (at your option) any later version.
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
15
 *
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
 */

20
#include "config.h"
21

22
23
#include <string.h>
#include <stdlib.h>
24
25
26
27

#include <glib.h>
#include <glib/gi18n.h>

28
#include <gtk/gtk.h>
29
#include <uuid.h>
30

31
#include "terminal-app.h"
32
#include "terminal-debug.h"
Christian Persch's avatar
Christian Persch committed
33
#include "terminal-enums.h"
34
#include "terminal-headerbar.h"
35
#include "terminal-icon-button.h"
36
#include "terminal-intl.h"
37
#include "terminal-mdi-container.h"
Christian Persch's avatar
Christian Persch committed
38
#include "terminal-menu-button.h"
39
#include "terminal-notebook.h"
Christian Persch's avatar
Christian Persch committed
40
#include "terminal-schemas.h"
41
#include "terminal-screen-container.h"
Christian Persch's avatar
Christian Persch committed
42
#include "terminal-search-popover.h"
43
#include "terminal-tab-label.h"
44
45
#include "terminal-util.h"
#include "terminal-window.h"
46
#include "terminal-libgsystem.h"
47

48
struct _TerminalWindowPrivate
49
{
50
51
  char *uuid;

52
53
  GtkClipboard *clipboard;

54
55
  TerminalScreenPopupInfo *popup_info;

56
  GtkWidget *menubar;
57
  TerminalMdiContainer *mdi_container;
58
  GtkWidget *main_vbox;
59
  TerminalScreen *active_screen;
60
61

  /* Size of a character cell in pixels */
Havoc Pennington's avatar
Havoc Pennington committed
62
63
  int old_char_width;
  int old_char_height;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

  /* Width and height added to the actual terminal grid by "chrome" inside
   * what was traditionally the X11 window: menu bar, title bar,
   * style-provided padding. This must be included when resizing the window
   * and also included in geometry hints. */
  int old_chrome_width;
  int old_chrome_height;

  /* Width and height added to the window by client-side decorations.
   * This must be included in geometry hints but must not be included when
   * resizing the window. */
  int old_csd_width;
  int old_csd_height;

  /* Width and height of the padding around the geometry widget. */
  int old_padding_width;
  int old_padding_height;

82
  void *old_geometry_widget; /* only used for pointer value as it may be freed */
83

84
  GtkWidget *confirm_close_dialog;
Christian Persch's avatar
Christian Persch committed
85
  TerminalSearchPopover *search_popover;
86

87
88
  guint use_default_menubar_visibility : 1;

89
90
  guint disposed : 1;
  guint present_on_insert : 1;
91

92
  guint realized : 1;
93
94
};

Christian Persch's avatar
Christian Persch committed
95
96
#define TERMINAL_WINDOW_CSS_NAME "terminal-window"

Christian Persch's avatar
Christian Persch committed
97
98
#define MIN_WIDTH_CHARS 4
#define MIN_HEIGHT_CHARS 1
Christian Persch's avatar
Christian Persch committed
99

100
101
102
103
104
105
106
107
108
109
110
111
112
113
#if 1
/*
 * We don't want to enable content saving until vte supports it async.
 * So we disable this code for stable versions.
 */
#include "terminal-version.h"

#if (TERMINAL_MINOR_VERSION & 1) != 0
#define ENABLE_SAVE
#else
#undef ENABLE_SAVE
#endif
#endif

Christian Persch's avatar
Christian Persch committed
114
115
116
117
118
119
120
/* See bug #789356 */
#define WINDOW_STATE_TILED (GDK_WINDOW_STATE_TILED       | \
                            GDK_WINDOW_STATE_LEFT_TILED  | \
                            GDK_WINDOW_STATE_RIGHT_TILED | \
                            GDK_WINDOW_STATE_TOP_TILED   | \
                            GDK_WINDOW_STATE_BOTTOM_TILED)

121
static void terminal_window_dispose     (GObject             *object);
122
static void terminal_window_finalize    (GObject             *object);
123
124
static gboolean terminal_window_state_event (GtkWidget            *widget,
                                             GdkEventWindowState  *event);
125

126
127
128
129
static gboolean terminal_window_delete_event (GtkWidget *widget,
                                              GdkEvent *event,
                                              gpointer data);

130
131
132
133
134
static gboolean notebook_button_press_cb     (GtkWidget *notebook,
                                              GdkEventButton *event,
                                              TerminalWindow *window);
static gboolean notebook_popup_menu_cb       (GtkWidget *notebook,
                                              TerminalWindow *window);
135
136
137
138
139
140
141
142
143
144
static void mdi_screen_switched_cb (TerminalMdiContainer *container,
                                    TerminalScreen *old_active_screen,
                                    TerminalScreen *screen,
                                    TerminalWindow *window);
static void mdi_screen_added_cb    (TerminalMdiContainer *container,
                                    TerminalScreen *screen,
                                    TerminalWindow *window);
static void mdi_screen_removed_cb  (TerminalMdiContainer *container,
                                    TerminalScreen *screen,
                                    TerminalWindow *window);
Christian Persch's avatar
Christian Persch committed
145
146
static void mdi_screens_reordered_cb (TerminalMdiContainer *container,
                                      TerminalWindow  *window);
147
148
149
static void screen_close_request_cb (TerminalMdiContainer *container,
                                     TerminalScreen *screen,
                                     TerminalWindow *window);
Tony Tsui's avatar
Tony Tsui committed
150

151
/* Menu action callbacks */
Christian Persch's avatar
Christian Persch committed
152
153
static gboolean find_larger_zoom_factor  (double *zoom);
static gboolean find_smaller_zoom_factor (double *zoom);
154
static void terminal_window_update_zoom_sensitivity (TerminalWindow *window);
155
156
static void terminal_window_update_search_sensitivity (TerminalScreen *screen,
                                                       TerminalWindow *window);
157
static void terminal_window_update_paste_sensitivity (TerminalWindow *window);
158

Havoc Pennington's avatar
Havoc Pennington committed
159
160
static void terminal_window_show (GtkWidget *widget);

161
162
163
static gboolean confirm_close_window_or_tab (TerminalWindow *window,
                                             TerminalScreen *screen);

164
G_DEFINE_TYPE (TerminalWindow, terminal_window, GTK_TYPE_APPLICATION_WINDOW)
165

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* Zoom helpers */

static const double zoom_factors[] = {
  TERMINAL_SCALE_MINIMUM,
  TERMINAL_SCALE_XXXXX_SMALL,
  TERMINAL_SCALE_XXXX_SMALL,
  TERMINAL_SCALE_XXX_SMALL,
  PANGO_SCALE_XX_SMALL,
  PANGO_SCALE_X_SMALL,
  PANGO_SCALE_SMALL,
  PANGO_SCALE_MEDIUM,
  PANGO_SCALE_LARGE,
  PANGO_SCALE_X_LARGE,
  PANGO_SCALE_XX_LARGE,
  TERMINAL_SCALE_XXX_LARGE,
  TERMINAL_SCALE_XXXX_LARGE,
  TERMINAL_SCALE_XXXXX_LARGE,
  TERMINAL_SCALE_MAXIMUM
};

static gboolean
Christian Persch's avatar
Christian Persch committed
187
find_larger_zoom_factor (double *zoom)
188
{
Christian Persch's avatar
Christian Persch committed
189
  double current = *zoom;
190
191
192
193
194
195
196
  guint i;

  for (i = 0; i < G_N_ELEMENTS (zoom_factors); ++i)
    {
      /* Find a font that's larger than this one */
      if ((zoom_factors[i] - current) > 1e-6)
        {
Christian Persch's avatar
Christian Persch committed
197
          *zoom = zoom_factors[i];
198
199
200
          return TRUE;
        }
    }
Christian Persch's avatar
Christian Persch committed
201

202
203
204
205
  return FALSE;
}

static gboolean
Christian Persch's avatar
Christian Persch committed
206
find_smaller_zoom_factor (double *zoom)
207
{
Christian Persch's avatar
Christian Persch committed
208
  double current = *zoom;
209
  int i;
Christian Persch's avatar
Christian Persch committed
210

211
212
213
214
215
216
  i = (int) G_N_ELEMENTS (zoom_factors) - 1;
  while (i >= 0)
    {
      /* Find a font that's smaller than this one */
      if ((current - zoom_factors[i]) > 1e-6)
        {
Christian Persch's avatar
Christian Persch committed
217
          *zoom = zoom_factors[i];
218
219
          return TRUE;
        }
Christian Persch's avatar
Christian Persch committed
220

221
222
223
224
225
226
      --i;
    }

  return FALSE;
}

Christian Persch's avatar
Christian Persch committed
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
static inline GSimpleAction *
lookup_action (TerminalWindow *window,
               const char *name)
{
  GAction *action;

  action = g_action_map_lookup_action (G_ACTION_MAP (window), name);
  g_return_val_if_fail (action != NULL, NULL);

  return G_SIMPLE_ACTION (action);
}

/* Context menu helpers */

/* We don't want context menus to show accelerators.
 * Setting the menu's accel group and/or accel path to NULL
 * unfortunately doesn't hide accelerators; we need to walk
 * the menu items and remove the accelerators on each,
 * manually.
 */
static void
popup_menu_remove_accelerators (GtkWidget *menu)
{
  gs_free_list GList *menu_items;
  GList *l;

  menu_items = gtk_container_get_children (GTK_CONTAINER (menu));
  for (l = menu_items; l != NULL; l = l ->next) {
255
256
    GtkMenuItem *item = (GtkMenuItem*) (l->data);
    GtkWidget *label, *submenu;
Christian Persch's avatar
Christian Persch committed
257

258
259
260
261
    if (!GTK_IS_MENU_ITEM (item))
      continue;

    if (GTK_IS_ACCEL_LABEL ((label = gtk_bin_get_child (GTK_BIN (item)))))
Christian Persch's avatar
Christian Persch committed
262
      gtk_accel_label_set_accel (GTK_ACCEL_LABEL (label), 0, 0);
263
264
265
266

    /* Recurse into submenus */
    if ((submenu = gtk_menu_item_get_submenu (item)))
      popup_menu_remove_accelerators (submenu);
Christian Persch's avatar
Christian Persch committed
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
  }
}

/* Because we're using gtk_menu_attach_to_widget(), the attach
 * widget holds a strong reference to the menu, causing it not to
 * be automatically destroyed once popped down. So we need to
 * detach the menu from the attach widget manually, which will
 * cause the menu to be destroyed. We cannot do so in the
 * "deactivate" handler however, since that causes the menu
 * item activation to be lost. The "selection-done" signal
 * appears to be the right place.
 */

static void
popup_menu_destroy_cb (GtkWidget *menu,
                       gpointer user_data)
{
  /* g_printerr ("Menu %p destroyed!\n", menu); */
}

static void
popup_menu_selection_done_cb (GtkMenu *menu,
                              gpointer user_data)
{
  g_signal_handlers_disconnect_by_func
    (menu, G_CALLBACK (popup_menu_selection_done_cb), user_data);

  /* g_printerr ("selection-done %p\n", menu); */

  /* This will remove the ref from the attach widget widget, and destroy the menu */
297
298
  if (gtk_menu_get_attach_widget (menu) != NULL)
    gtk_menu_detach (menu);
Christian Persch's avatar
Christian Persch committed
299
300
}

301
302
303
304
305
306
307
static void
popup_menu_detach_cb (GtkWidget *attach_widget,
                      GtkMenu *menu)
{
  gtk_menu_shell_deactivate (GTK_MENU_SHELL (menu));
}

Christian Persch's avatar
Christian Persch committed
308
309
310
311
312
313
314
315
316
static GtkWidget *
context_menu_new (GMenuModel *menu,
                  GtkWidget *widget)
{
  GtkWidget *popup_menu;

  popup_menu = gtk_menu_new_from_model (menu);
  gtk_style_context_add_class (gtk_widget_get_style_context (popup_menu),
                               GTK_STYLE_CLASS_CONTEXT_MENU);
317
318
  gtk_menu_attach_to_widget (GTK_MENU (popup_menu), widget,
                             (GtkMenuDetachFunc)popup_menu_detach_cb);
Christian Persch's avatar
Christian Persch committed
319
320
321
322
323
324
325
326
327
328
329
330

  popup_menu_remove_accelerators (popup_menu);

  /* Staggered destruction */
  g_signal_connect (popup_menu, "selection-done",
                    G_CALLBACK (popup_menu_selection_done_cb), widget);
  g_signal_connect (popup_menu, "destroy",
                    G_CALLBACK (popup_menu_destroy_cb), widget);

  return popup_menu;
}

331
332
333
334
335
336
337
338
339
340
/* GAction callbacks */

static void
action_new_terminal_cb (GSimpleAction *action,
                        GVariant *parameter,
                        gpointer user_data)
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
  TerminalApp *app;
341
  TerminalSettingsList *profiles_list;
342
  gs_unref_object GSettings *profile = NULL;
343
  gboolean can_toggle = FALSE;
344
345
346
347
348

  g_assert (TERMINAL_IS_WINDOW (window));

  app = terminal_app_get ();

Christian Persch's avatar
Christian Persch committed
349
  const char *mode_str, *uuid_str;
350
351
  g_variant_get (parameter, "(&s&s)", &mode_str, &uuid_str);

Christian Persch's avatar
Christian Persch committed
352
  TerminalNewTerminalMode mode;
353
354
355
356
  if (g_str_equal (mode_str, "tab"))
    mode = TERMINAL_NEW_TERMINAL_MODE_TAB;
  else if (g_str_equal (mode_str, "window"))
    mode = TERMINAL_NEW_TERMINAL_MODE_WINDOW;
357
358
359
360
  else if (g_str_equal (mode_str, "tab-default")) {
    mode = TERMINAL_NEW_TERMINAL_MODE_TAB;
    can_toggle = TRUE;
  } else {
361
362
    mode = g_settings_get_enum (terminal_app_get_global_settings (app),
                                TERMINAL_SETTING_NEW_TERMINAL_MODE_KEY);
363
364
    can_toggle = TRUE;
  }
Christian Persch's avatar
Christian Persch committed
365

366
  if (can_toggle) {
Christian Persch's avatar
Christian Persch committed
367
368
369
370
371
    GdkEvent *event = gtk_get_current_event ();
    if (event != NULL) {
      GdkModifierType modifiers;

      if ((gdk_event_get_state (event, &modifiers) &&
372
           (modifiers & gtk_accelerator_get_default_mod_mask () & GDK_CONTROL_MASK))) {
Christian Persch's avatar
Christian Persch committed
373
374
375
376
377
378
379
        /* Invert */
        if (mode == TERMINAL_NEW_TERMINAL_MODE_WINDOW)
          mode = TERMINAL_NEW_TERMINAL_MODE_TAB;
        else
          mode = TERMINAL_NEW_TERMINAL_MODE_WINDOW;
      }
      gdk_event_free (event);
380
381
382
    }
  }

Christian Persch's avatar
Christian Persch committed
383
384
  TerminalScreen *parent_screen = priv->active_screen;

385
386
  profiles_list = terminal_app_get_profiles_list (app);
  if (g_str_equal (uuid_str, "current"))
Christian Persch's avatar
Christian Persch committed
387
    profile = terminal_screen_ref_profile (parent_screen);
388
389
390
391
392
393
394
395
  else if (g_str_equal (uuid_str, "default"))
    profile = terminal_settings_list_ref_default_child (profiles_list);
  else
    profile = terminal_settings_list_ref_child (profiles_list, uuid_str);

  if (profile == NULL)
    return;

396
  if (mode == TERMINAL_NEW_TERMINAL_MODE_WINDOW)
Christian Persch's avatar
Christian Persch committed
397
398
399
400
401
402
403
404
405
406
    window = terminal_window_new (G_APPLICATION (app));

  TerminalScreen *screen = terminal_screen_new (profile,
                                                NULL /* title */,
                                                1.0);

  /* Now add the new screen to the window */
  terminal_window_add_screen (window, screen, -1);
  terminal_window_switch_screen (window, screen);
  gtk_widget_grab_focus (GTK_WIDGET (screen));
407

Christian Persch's avatar
Christian Persch committed
408
409
  /* Start child process, if possible by using the same args as the parent screen */
  terminal_screen_reexec_from_screen (screen, parent_screen, NULL, NULL);
410
411
412
413
414
415

  if (mode == TERMINAL_NEW_TERMINAL_MODE_WINDOW)
    gtk_window_present (GTK_WINDOW (window));
}

#ifdef ENABLE_SAVE
Christian Persch's avatar
Christian Persch committed
416

417
418
419
420
static void
save_contents_dialog_on_response (GtkDialog *dialog, gint response_id, gpointer terminal)
{
  GtkWindow *parent;
421
422
  gs_free gchar *filename_uri = NULL;
  gs_unref_object GFile *file = NULL;
423
  GOutputStream *stream;
424
  gs_free_error GError *error = NULL;
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

  if (response_id != GTK_RESPONSE_ACCEPT)
    {
      gtk_widget_destroy (GTK_WIDGET (dialog));
      return;
    }

  parent = (GtkWindow*) gtk_widget_get_ancestor (GTK_WIDGET (terminal), GTK_TYPE_WINDOW);
  filename_uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog));

  gtk_widget_destroy (GTK_WIDGET (dialog));

  if (filename_uri == NULL)
    return;

  file = g_file_new_for_uri (filename_uri);
  stream = G_OUTPUT_STREAM (g_file_replace (file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error));

  if (stream)
    {
      /* XXX
       * FIXME
       * This is a sync operation.
       * Should be replaced with the async version when vte implements that.
       */
450
451
452
      vte_terminal_write_contents_sync (terminal, stream,
					VTE_WRITE_DEFAULT,
					NULL, &error);
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
      g_object_unref (stream);
    }

  if (error)
    {
      terminal_util_show_error_dialog (parent, NULL, error,
				       "%s", _("Could not save contents"));
    }
}

static void
action_save_contents_cb (GSimpleAction *action,
                         GVariant *parameter,
                         gpointer user_data)
{
  TerminalWindow *window = user_data;
  GtkWidget *dialog = NULL;
  TerminalWindowPrivate *priv = window->priv;
  VteTerminal *terminal;

Christian Persch's avatar
Christian Persch committed
473
  if (priv->active_screen == NULL)
474
475
476
477
478
479
480
481
    return;

  terminal = VTE_TERMINAL (priv->active_screen);
  g_return_if_fail (VTE_IS_TERMINAL (terminal));

  dialog = gtk_file_chooser_dialog_new (_("Save as…"),
                                        GTK_WINDOW(window),
                                        GTK_FILE_CHOOSER_ACTION_SAVE,
482
483
                                        _("_Cancel"), GTK_RESPONSE_CANCEL,
                                        _("_Save"), GTK_RESPONSE_ACCEPT,
484
485
486
                                        NULL);

  gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
487
  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS));
488

Christian Persch's avatar
Christian Persch committed
489
  gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (window));
490
491
492
493
494
495
496
  gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
  gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);

  g_signal_connect (dialog, "response", G_CALLBACK (save_contents_dialog_on_response), terminal);
  g_signal_connect (dialog, "delete_event", G_CALLBACK (terminal_util_dialog_response_on_delete), NULL);

  gtk_window_present (GTK_WINDOW (dialog));
Christian Persch's avatar
Christian Persch committed
497
498
}

499
#endif /* ENABLE_SAVE */
Christian Persch's avatar
Christian Persch committed
500
501
502
503
504
505
506
507
508
509
510
511
512
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
569

#ifdef ENABLE_PRINT

static void
print_begin_cb (GtkPrintOperation *op,
                GtkPrintContext *context,
                TerminalApp *app)
{
  GtkPrintSettings *settings;
  GtkPageSetup *page_setup;

  /* Don't save if the print dialogue was cancelled */
  if (gtk_print_operation_get_status(op) == GTK_PRINT_STATUS_FINISHED_ABORTED)
    return;

  settings = gtk_print_operation_get_print_settings (op);
  page_setup = gtk_print_operation_get_default_page_setup (op);
  terminal_util_save_print_settings (settings, page_setup);
}

static void
print_done_cb (GtkPrintOperation *op,
               GtkPrintOperationResult result,
               TerminalWindow *window)
{
  if (result != GTK_PRINT_OPERATION_RESULT_ERROR)
    return;

  /* FIXME: show error */
}

static void
action_print_cb (GSimpleAction *action,
                 GVariant *parameter,
                 gpointer user_data)
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
  gs_unref_object GtkPrintSettings *settings = NULL;
  gs_unref_object GtkPageSetup *page_setup = NULL;
  gs_unref_object GtkPrintOperation *op = NULL;
  gs_free_error GError *error = NULL;
  GtkPrintOperationResult result;

  if (priv->active_screen == NULL)
    return;

  op = vte_print_operation_new (VTE_TERMINAL (priv->active_screen),
                                VTE_PRINT_OPERATION_DEFAULT /* flags */);
  if (op == NULL)
    return;

  terminal_util_load_print_settings (&settings, &page_setup);
  if (settings != NULL)
    gtk_print_operation_set_print_settings (op, settings);
  if (page_setup != NULL)
    gtk_print_operation_set_default_page_setup (op, page_setup);

  g_signal_connect (op, "begin-print", G_CALLBACK (print_begin_cb), window);
  g_signal_connect (op, "done", G_CALLBACK (print_done_cb), window);

  /* FIXME: show progress better */

  result = gtk_print_operation_run (op,
                                    /* this is the only supported one: */
                                    GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
                                    GTK_WINDOW (window),
                                    &error);
  /* VtePrintOperation always runs async */
  g_assert_cmpint (result, ==, GTK_PRINT_OPERATION_RESULT_IN_PROGRESS);
570
571
}

Christian Persch's avatar
Christian Persch committed
572
573
574
575
#endif /* ENABLE_PRINT */

#ifdef ENABLE_EXPORT

576
static void
Christian Persch's avatar
Christian Persch committed
577
578
579
action_export_cb (GSimpleAction *action,
                 GVariant *parameter,
                 gpointer user_data)
580
{
Christian Persch's avatar
Christian Persch committed
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
  gs_unref_object VteExportOperation *op = NULL;
  gs_free_error GError *error = NULL;

  if (priv->active_screen == NULL)
    return;

  op = vte_export_operation_new (VTE_TERMINAL (priv->active_screen),
                                 TRUE /* interactive */,
                                 VTE_EXPORT_FORMAT_ASK /* allow user to choose export format */,
                                 NULL, NULL /* GSettings & key to load/store default directory from, FIXME */,
                                 NULL, NULL /* progress callback & user data, FIXME */);
  if (op == NULL)
    return;

  /* FIXME: show progress better */

  vte_export_operation_run_async (op, GTK_WINDOW (window), NULL /* cancellable */);
600
601
}

Christian Persch's avatar
Christian Persch committed
602
603
#endif /* ENABLE_EXPORT */

604
605
606
607
608
609
610
611
612
613
static void
action_close_cb (GSimpleAction *action,
                 GVariant *parameter,
                 gpointer user_data)
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
  TerminalScreen *screen;
  const char *mode_str;

Christian Persch's avatar
Christian Persch committed
614
  g_assert_nonnull (parameter);
615
616
617
618
619
620
621
  g_variant_get (parameter, "&s", &mode_str);

  if (g_str_equal (mode_str, "tab"))
    screen = priv->active_screen;
  else if (g_str_equal (mode_str, "window"))
    screen = NULL;
  else
Christian Persch's avatar
Christian Persch committed
622
    return;
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639

  if (confirm_close_window_or_tab (window, screen))
    return;

  if (screen)
    terminal_window_remove_screen (window, screen);
  else
    gtk_widget_destroy (GTK_WIDGET (window));
}

static void
action_copy_cb (GSimpleAction *action,
                GVariant *parameter,
                gpointer user_data)
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
640
641
  const char *format_str;
  VteFormat format;
642

Christian Persch's avatar
Christian Persch committed
643
  if (priv->active_screen == NULL)
644
    return;
645

Christian Persch's avatar
Christian Persch committed
646
  g_assert_nonnull (parameter);
647
648
649
650
651
652
653
654
655
656
  g_variant_get (parameter, "&s", &format_str);

  if (g_str_equal (format_str, "text"))
    format = VTE_FORMAT_TEXT;
  else if (g_str_equal (format_str, "html"))
    format = VTE_FORMAT_HTML;
  else
    return;

  vte_terminal_copy_clipboard_format (VTE_TERMINAL (priv->active_screen), format);
657
658
}

Christian Persch's avatar
Christian Persch committed
659
660
661
662
663
664
/* Clipboard helpers */

typedef struct {
  GWeakRef screen_weak_ref;
} PasteData;

665
static void
Christian Persch's avatar
Christian Persch committed
666
667
668
clipboard_uris_received_cb (GtkClipboard *clipboard,
                            /* const */ char **uris,
                            PasteData *data)
669
{
Christian Persch's avatar
Christian Persch committed
670
  gs_unref_object TerminalScreen *screen = NULL;
671

Christian Persch's avatar
Christian Persch committed
672
673
674
675
676
677
678
679
680
681
682
683
684
685
  if (uris != NULL && uris[0] != NULL &&
      (screen = g_weak_ref_get (&data->screen_weak_ref))) {
    gs_free char *text;
    gsize len;

    /* This potentially modifies the strings in |uris| but that's ok */
    terminal_util_transform_uris_to_quoted_fuse_paths (uris);
    text = terminal_util_concat_uris (uris, &len);

    vte_terminal_feed_child (VTE_TERMINAL (screen), text, len);
  }

  g_weak_ref_clear (&data->screen_weak_ref);
  g_slice_free (PasteData, data);
686
687
688
}

static void
Christian Persch's avatar
Christian Persch committed
689
690
request_clipboard_contents_for_paste (TerminalWindow *window,
                                      gboolean paste_as_uris)
691
692
{
  TerminalWindowPrivate *priv = window->priv;
Christian Persch's avatar
Christian Persch committed
693
694
  GdkAtom *targets;
  int n_targets;
695

Christian Persch's avatar
Christian Persch committed
696
  if (priv->active_screen == NULL)
697
698
    return;

Christian Persch's avatar
Christian Persch committed
699
700
701
702
703
  targets = terminal_app_get_clipboard_targets (terminal_app_get (),
                                                priv->clipboard,
                                                &n_targets);
  if (targets == NULL)
    return;
704

Christian Persch's avatar
Christian Persch committed
705
706
707
  if (paste_as_uris && gtk_targets_include_uri (targets, n_targets)) {
    PasteData *data = g_slice_new (PasteData);
    g_weak_ref_init (&data->screen_weak_ref, priv->active_screen);
708

Christian Persch's avatar
Christian Persch committed
709
710
711
712
713
714
715
    gtk_clipboard_request_uris (priv->clipboard,
                                (GtkClipboardURIReceivedFunc) clipboard_uris_received_cb,
                                data);
    return;
  } else if (gtk_targets_include_text (targets, n_targets)) {
    vte_terminal_paste_clipboard (VTE_TERMINAL (priv->active_screen));
  }
716
717
718
}

static void
Christian Persch's avatar
Christian Persch committed
719
720
721
action_paste_text_cb (GSimpleAction *action,
                      GVariant *parameter,
                      gpointer user_data)
722
{
Christian Persch's avatar
Christian Persch committed
723
  TerminalWindow *window = user_data;
724

Christian Persch's avatar
Christian Persch committed
725
726
727
728
729
730
731
732
733
  request_clipboard_contents_for_paste (window, FALSE);
}

static void
action_paste_uris_cb (GSimpleAction *action,
                      GVariant *parameter,
                      gpointer user_data)
{
  TerminalWindow *window = user_data;
734

Christian Persch's avatar
Christian Persch committed
735
  request_clipboard_contents_for_paste (window, TRUE);
736
737
738
739
740
741
742
743
744
745
}

static void
action_select_all_cb (GSimpleAction *action,
                      GVariant *parameter,
                      gpointer user_data)
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;

Christian Persch's avatar
Christian Persch committed
746
  if (priv->active_screen == NULL)
747
748
749
750
751
752
753
754
755
756
757
758
759
    return;

  vte_terminal_select_all (VTE_TERMINAL (priv->active_screen));
}

static void
action_reset_cb (GSimpleAction *action,
                 GVariant *parameter,
                 gpointer user_data)
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;

Christian Persch's avatar
Christian Persch committed
760
  g_assert_nonnull (parameter);
761
762
763
764
765
766
767
768
769
770

  if (priv->active_screen == NULL)
    return;

  vte_terminal_reset (VTE_TERMINAL (priv->active_screen),
                      TRUE,
                      g_variant_get_boolean (parameter));
}

static void
Christian Persch's avatar
Christian Persch committed
771
772
tab_switch_relative (TerminalWindow *window,
                     int change)
773
774
{
  TerminalWindowPrivate *priv = window->priv;
Christian Persch's avatar
Christian Persch committed
775
  int n_screens, value;
776

Christian Persch's avatar
Christian Persch committed
777
778
  n_screens = terminal_mdi_container_get_n_screens (priv->mdi_container);
  value = terminal_mdi_container_get_active_screen_num (priv->mdi_container) + change;
779

Christian Persch's avatar
Christian Persch committed
780
781
782
783
784
785
786
787
788
789
  gboolean keynav_wrap_around;
  g_object_get (gtk_widget_get_settings (GTK_WIDGET (window)),
                "gtk-keynav-wrap-around", &keynav_wrap_around,
                NULL);
  if (keynav_wrap_around) {
    if (value < 0)
      value += n_screens;
    else if (value >= n_screens)
      value -= n_screens;
  }
790

Christian Persch's avatar
Christian Persch committed
791
792
793
794
795
  if (value < 0 || value >= n_screens)
    return;

  g_action_change_state (G_ACTION (lookup_action (window, "active-tab")),
                         g_variant_new_int32 (value));
796
797
798
}

static void
Christian Persch's avatar
Christian Persch committed
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
action_tab_switch_left_cb (GSimpleAction *action,
                           GVariant *parameter,
                           gpointer user_data)
{
  TerminalWindow *window = user_data;

  tab_switch_relative (window, -1);
}

static void
action_tab_switch_right_cb (GSimpleAction *action,
                            GVariant *parameter,
                            gpointer user_data)
{
  TerminalWindow *window = user_data;

  tab_switch_relative (window, 1);
}

static void
action_tab_move_left_cb (GSimpleAction *action,
                         GVariant *parameter,
                         gpointer user_data)
822
823
824
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
Christian Persch's avatar
Christian Persch committed
825
  int change;
826

Christian Persch's avatar
Christian Persch committed
827
828
829
830
  if (priv->active_screen == NULL)
    return;

  change = gtk_widget_get_direction (GTK_WIDGET (window)) == GTK_TEXT_DIR_RTL ? 1 : -1;
831
832
  terminal_mdi_container_reorder_screen (priv->mdi_container,
                                         terminal_mdi_container_get_active_screen (priv->mdi_container),
Christian Persch's avatar
Christian Persch committed
833
                                         change);
834
835
836
}

static void
Christian Persch's avatar
Christian Persch committed
837
838
839
action_tab_move_right_cb (GSimpleAction *action,
                          GVariant *parameter,
                          gpointer user_data)
840
841
842
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
Christian Persch's avatar
Christian Persch committed
843
844
  int change;

845
846
847
  if (priv->active_screen == NULL)
    return;

Christian Persch's avatar
Christian Persch committed
848
849
850
851
852
  change = gtk_widget_get_direction (GTK_WIDGET (window)) == GTK_TEXT_DIR_RTL ? -1 : 1;
  terminal_mdi_container_reorder_screen (priv->mdi_container,
                                         terminal_mdi_container_get_active_screen (priv->mdi_container),
                                         change);
}
853

Christian Persch's avatar
Christian Persch committed
854
855
856
857
858
859
860
861
static void
action_zoom_in_cb (GSimpleAction *action,
                   GVariant *parameter,
                   gpointer user_data)
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
  double zoom;
862

Christian Persch's avatar
Christian Persch committed
863
864
865
866
867
868
  if (priv->active_screen == NULL)
    return;

  zoom = vte_terminal_get_font_scale (VTE_TERMINAL (priv->active_screen));
  if (!find_larger_zoom_factor (&zoom))
    return;
869

870
  vte_terminal_set_font_scale (VTE_TERMINAL (priv->active_screen), zoom);
871
872
873
874
  terminal_window_update_zoom_sensitivity (window);
}

static void
Christian Persch's avatar
Christian Persch committed
875
876
877
action_zoom_out_cb (GSimpleAction *action,
                    GVariant *parameter,
                    gpointer user_data)
878
{
Christian Persch's avatar
Christian Persch committed
879
880
881
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
  double zoom;
882

Christian Persch's avatar
Christian Persch committed
883
884
885
886
887
888
889
890
891
  if (priv->active_screen == NULL)
    return;

  zoom = vte_terminal_get_font_scale (VTE_TERMINAL (priv->active_screen));
  if (!find_smaller_zoom_factor (&zoom))
    return;

  vte_terminal_set_font_scale (VTE_TERMINAL (priv->active_screen), zoom);
  terminal_window_update_zoom_sensitivity (window);
892
893
894
}

static void
Christian Persch's avatar
Christian Persch committed
895
896
897
action_zoom_normal_cb (GSimpleAction *action,
                       GVariant *parameter,
                       gpointer user_data)
898
{
Christian Persch's avatar
Christian Persch committed
899
900
901
902
903
904
905
906
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;

  if (priv->active_screen == NULL)
    return;

  vte_terminal_set_font_scale (VTE_TERMINAL (priv->active_screen), PANGO_SCALE_MEDIUM);
  terminal_window_update_zoom_sensitivity (window);
907
908
909
}

static void
Christian Persch's avatar
Christian Persch committed
910
action_tab_detach_cb (GSimpleAction *action,
911
912
913
914
915
916
917
918
                      GVariant *parameter,
                      gpointer user_data)
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
  TerminalApp *app;
  TerminalWindow *new_window;
  TerminalScreen *screen;
919
  char geometry[32];
920
921
922
923
924
925
926
  int width, height;

  app = terminal_app_get ();

  screen = priv->active_screen;

  terminal_screen_get_size (screen, &width, &height);
927
  g_snprintf (geometry, sizeof (geometry), "%dx%d", width, height);
928

Christian Persch's avatar
Christian Persch committed
929
  new_window = terminal_window_new (G_APPLICATION (app));
930
931
932
933
934
935
936
937
938
939
940
941
942

  terminal_window_move_screen (window, new_window, screen, -1);

  terminal_window_parse_geometry (new_window, geometry);

  gtk_window_present_with_time (GTK_WINDOW (new_window), gtk_get_current_event_time ());
}

static void
action_help_cb (GSimpleAction *action,
                GVariant *parameter,
                gpointer user_data)
{
Christian Persch's avatar
Christian Persch committed
943
  terminal_util_show_help (NULL);
944
945
946
947
948
949
950
}

static void
action_about_cb (GSimpleAction *action,
                 GVariant *parameter,
                 gpointer user_data)
{
Christian Persch's avatar
Christian Persch committed
951
  terminal_util_show_about ();
952
953
954
}

static void
Christian Persch's avatar
Christian Persch committed
955
956
957
action_edit_preferences_cb (GSimpleAction *action,
                            GVariant *parameter,
                            gpointer user_data)
958
959
960
961
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;

962
963
964
  terminal_app_edit_preferences (terminal_app_get (),
                                 terminal_screen_get_profile (priv->active_screen),
                                 NULL);
965
966
}

Christian Persch's avatar
Christian Persch committed
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
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
static void
action_size_to_cb (GSimpleAction *action,
                   GVariant      *parameter,
                   gpointer       user_data)
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
  guint width, height;

  g_assert_nonnull (parameter);

  if (priv->active_screen == NULL)
    return;

  g_variant_get (parameter, "(uu)", &width, &height);
  if (width < MIN_WIDTH_CHARS || height < MIN_HEIGHT_CHARS ||
      width > 256 || height > 256)
    return;

  vte_terminal_set_size (VTE_TERMINAL (priv->active_screen), width, height);
  terminal_window_update_size (window);
}

static void
action_open_match_cb (GSimpleAction *action,
                      GVariant      *parameter,
                      gpointer       user_data)
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
  TerminalScreenPopupInfo *info = priv->popup_info;

  if (info == NULL)
    return;
  if (info->url == NULL)
    return;

  terminal_util_open_url (GTK_WIDGET (window), info->url, info->url_flavor,
                          gtk_get_current_event_time ());
}

static void
action_copy_match_cb (GSimpleAction *action,
                      GVariant      *parameter,
                      gpointer       user_data)
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
  TerminalScreenPopupInfo *info = priv->popup_info;

  if (info == NULL)
    return;
  if (info->url == NULL)
    return;

  gtk_clipboard_set_text (priv->clipboard, info->url, -1);
}

static void
action_open_hyperlink_cb (GSimpleAction *action,
                          GVariant      *parameter,
                          gpointer       user_data)
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
  TerminalScreenPopupInfo *info = priv->popup_info;

  if (info == NULL)
    return;
  if (info->hyperlink == NULL)
    return;

  terminal_util_open_url (GTK_WIDGET (window), info->hyperlink, FLAVOR_AS_IS,
                          gtk_get_current_event_time ());
}

static void
action_copy_hyperlink_cb (GSimpleAction *action,
                          GVariant      *parameter,
                          gpointer       user_data)
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
  TerminalScreenPopupInfo *info = priv->popup_info;

  if (info == NULL)
    return;
  if (info->hyperlink == NULL)
    return;

  gtk_clipboard_set_text (priv->clipboard, info->hyperlink, -1);
}

1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
static void
action_enter_fullscreen_cb (GSimpleAction *action,
                            GVariant      *parameter,
                            gpointer       user_data)
{
  TerminalWindow *window = user_data;

  g_action_group_change_action_state (G_ACTION_GROUP (window), "fullscreen",
                                      g_variant_new_boolean (TRUE));
}

Christian Persch's avatar
Christian Persch committed
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
static void
action_leave_fullscreen_cb (GSimpleAction *action,
                            GVariant      *parameter,
                            gpointer       user_data)
{
  TerminalWindow *window = user_data;

  g_action_group_change_action_state (G_ACTION_GROUP (window), "fullscreen",
                                      g_variant_new_boolean (FALSE));
}

static void
action_inspector_cb (GSimpleAction *action,
                     GVariant      *parameter,
                     gpointer       user_data)
{
  gtk_window_set_interactive_debugging (TRUE);
}

1090
static void
Christian Persch's avatar
Christian Persch committed
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
search_popover_search_cb (TerminalSearchPopover *popover,
                          gboolean backward,
                          TerminalWindow *window)
{
  TerminalWindowPrivate *priv = window->priv;

  if (G_UNLIKELY (priv->active_screen == NULL))
    return;

  if (backward)
    vte_terminal_search_find_previous (VTE_TERMINAL (priv->active_screen));
  else
    vte_terminal_search_find_next (VTE_TERMINAL (priv->active_screen));
}

static void
search_popover_notify_regex_cb (TerminalSearchPopover *popover,
                                GParamSpec *pspec G_GNUC_UNUSED,
                                TerminalWindow *window)
1110
1111
{
  TerminalWindowPrivate *priv = window->priv;
1112
  VteRegex *regex;
1113

Christian Persch's avatar
Christian Persch committed
1114
  if (G_UNLIKELY (priv->active_screen == NULL))
1115
1116
    return;

Christian Persch's avatar
Christian Persch committed
1117
  regex = terminal_search_popover_get_regex (popover);
1118
  vte_terminal_search_set_regex (VTE_TERMINAL (priv->active_screen), regex, 0);
Christian Persch's avatar
Christian Persch committed
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131

  terminal_window_update_search_sensitivity (priv->active_screen, window);
}

static void
search_popover_notify_wrap_around_cb (TerminalSearchPopover *popover,
                                      GParamSpec *pspec G_GNUC_UNUSED,
                                      TerminalWindow *window)
{
  TerminalWindowPrivate *priv = window->priv;
  gboolean wrap;

  if (G_UNLIKELY (priv->active_screen == NULL))
1132
1133
    return;

Christian Persch's avatar
Christian Persch committed
1134
1135
1136
  wrap = terminal_search_popover_get_wrap_around (popover);
  vte_terminal_search_set_wrap_around (VTE_TERMINAL (priv->active_screen), wrap);
}
1137

Christian Persch's avatar
Christian Persch committed
1138
static void
Christian Persch's avatar
Christian Persch committed
1139
1140
1141
action_find_cb (GSimpleAction *action,
                GVariant *parameter,
                gpointer user_data)
Christian Persch's avatar
Christian Persch committed
1142
{
Christian Persch's avatar
Christian Persch committed
1143
  TerminalWindow *window = user_data;
Christian Persch's avatar
Christian Persch committed
1144
  TerminalWindowPrivate *priv = window->priv;
1145

Christian Persch's avatar
Christian Persch committed
1146
1147
  if (G_UNLIKELY(priv->active_screen == NULL))
    return;
1148

Christian Persch's avatar
Christian Persch committed
1149
1150
1151
1152
  if (priv->search_popover != NULL) {
    search_popover_notify_regex_cb (priv->search_popover, NULL, window);
    search_popover_notify_wrap_around_cb (priv->search_popover, NULL, window);

1153
1154
    gtk_window_present_with_time (GTK_WINDOW (priv->search_popover),
                                  gtk_get_current_event_time ());
1155
    gtk_widget_grab_focus (GTK_WIDGET (priv->search_popover));
Christian Persch's avatar
Christian Persch committed
1156
1157
1158
1159
1160
1161
    return;
  }

  if (priv->active_screen == NULL)
    return;

Christian Persch's avatar
Christian Persch committed
1162
  priv->search_popover = terminal_search_popover_new (GTK_WIDGET (window));
Christian Persch's avatar
Christian Persch committed
1163
1164
1165
1166
1167
1168
1169
1170
1171

  g_signal_connect (priv->search_popover, "search", G_CALLBACK (search_popover_search_cb), window);

  search_popover_notify_regex_cb (priv->search_popover, NULL, window);
  g_signal_connect (priv->search_popover, "notify::regex", G_CALLBACK (search_popover_notify_regex_cb), window);

  search_popover_notify_wrap_around_cb (priv->search_popover, NULL, window);
  g_signal_connect (priv->search_popover, "notify::wrap-around", G_CALLBACK (search_popover_notify_wrap_around_cb), window);

1172
1173
  g_signal_connect (priv->search_popover, "destroy", G_CALLBACK (gtk_widget_destroyed), &priv->search_popover);

1174
  gtk_window_present_with_time (GTK_WINDOW (priv->search_popover), gtk_get_current_event_time ());
1175
  gtk_widget_grab_focus (GTK_WIDGET (priv->search_popover));
1176
1177
1178
}

static void
Christian Persch's avatar
Christian Persch committed
1179
1180
1181
action_find_forward_cb (GSimpleAction *action,
                        GVariant *parameter,
                        gpointer user_data)
1182
1183
1184
1185
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;

Christian Persch's avatar
Christian Persch committed
1186
  if (priv->active_screen == NULL)
1187
1188
    return;

Christian Persch's avatar
Christian Persch committed
1189
1190
  vte_terminal_search_find_next (VTE_TERMINAL (priv->active_screen));
}
1191

Christian Persch's avatar
Christian Persch committed
1192
1193
1194
1195
1196
1197
1198
1199
1200
static void
action_find_backward_cb (GSimpleAction *action,
                         GVariant *parameter,
                         gpointer user_data)
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;

  if (priv->active_screen == NULL)
1201
    return;
Christian Persch's avatar
Christian Persch committed
1202
1203

  vte_terminal_search_find_previous (VTE_TERMINAL (priv->active_screen));
1204
1205
1206
}

static void
Christian Persch's avatar
Christian Persch committed
1207
1208
1209
action_find_clear_cb (GSimpleAction *action,
                      GVariant *parameter,
                      gpointer user_data)
1210
{
Christian Persch's avatar
Christian Persch committed
1211
1212
1213
1214
1215
1216
1217
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;

  if (priv->active_screen == NULL)
    return;

  vte_terminal_search_set_regex (VTE_TERMINAL (priv->active_screen), NULL, 0);
1218
  vte_terminal_unselect_all (VTE_TERMINAL (priv->active_screen));
1219
1220
1221
}

static void
Christian Persch's avatar
Christian Persch committed
1222
1223
1224
action_shadow_activate_cb (GSimpleAction *action,
                           GVariant *parameter,
                           gpointer user_data)
1225
{
Christian Persch's avatar
Christian Persch committed
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
  TerminalWindow *window = user_data;
  gs_free char *param = g_variant_print(parameter, TRUE);

  _terminal_debug_print (TERMINAL_DEBUG_ACCELS,
                         "Window %p shadow action activated for %s\n",
                         window, param);

  /* We make sure in terminal-accels to always install the keybinding
   * for the real action first, so that it's first in line for activation.
   * That means we can make this here a NOP, instead of forwarding the
   * activation to the shadowed action.
   */
1238
1239
1240
}

static void
Christian Persch's avatar
Christian Persch committed
1241
1242
1243
action_menubar_visible_state_cb (GSimpleAction *action,
                                 GVariant *state,
                                 gpointer user_data)
1244
{
Christian Persch's avatar
Christian Persch committed
1245
1246
1247
1248
1249
  TerminalWindow *window = user_data;
  gboolean active;

  active = g_variant_get_boolean (state);
  terminal_window_set_menubar_visible (window, active); /* this also sets the action state */
1250
1251
1252
}

static void
Christian Persch's avatar
Christian Persch committed
1253
1254
1255
action_fullscreen_state_cb (GSimpleAction *action,
                            GVariant *state,
                            gpointer user_data)
1256
{
Christian Persch's avatar
Christian Persch committed
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
  TerminalWindow *window = user_data;

  if (!gtk_widget_get_realized (GTK_WIDGET (window)))
    return;

  if (g_variant_get_boolean (state))
    gtk_window_fullscreen (GTK_WINDOW (window));
  else
    gtk_window_unfullscreen (GTK_WINDOW (window));

  /* The window-state-changed callback will update the action's actual state */
1268
1269
}

1270
static void
Christian Persch's avatar
Christian Persch committed
1271
1272
1273
action_read_only_state_cb (GSimpleAction *action,
                           GVariant *state,
                           gpointer user_data)
1274
{
Christian Persch's avatar
Christian Persch committed
1275
1276
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
1277

Christian Persch's avatar
Christian Persch committed
1278
1279
1280
1281
  g_assert_nonnull (state);

  g_simple_action_set_state (action, state);

1282
1283
  terminal_window_update_paste_sensitivity (window);

Christian Persch's avatar
Christian Persch committed
1284
1285
1286
1287
1288
  if (priv->active_screen == NULL)
    return;

  vte_terminal_set_input_enabled (VTE_TERMINAL (priv->active_screen),
                                  !g_variant_get_boolean (state));
1289
1290
1291
}

static void
Christian Persch's avatar
Christian Persch committed
1292
1293
1294
action_profile_state_cb (GSimpleAction *action,
                         GVariant *state,
                         gpointer user_data)
1295
1296
{
  TerminalWindow *window = user_data;
Christian Persch's avatar
Christian Persch committed
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
  TerminalWindowPrivate *priv = window->priv;
  TerminalSettingsList *profiles_list;
  const gchar *uuid;
  gs_unref_object GSettings *profile;

  g_assert_nonnull (state);

  uuid = g_variant_get_string (state, NULL);
  profiles_list = terminal_app_get_profiles_list (terminal_app_get ());
  profile = terminal_settings_list_ref_child (profiles_list, uuid);
  if (profile == NULL)
    return;
1309
1310
1311

  g_simple_action_set_state (action, state);

Christian Persch's avatar
Christian Persch committed
1312
1313
1314
  terminal_screen_set_profile (priv->active_screen, profile);
}

1315
static void
Christian Persch's avatar
Christian Persch committed
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
action_active_tab_set_cb (GSimpleAction *action,
                          GVariant *parameter,
                          gpointer user_data)
{
  TerminalWindow *window = user_data;
  TerminalWindowPrivate *priv = window->priv;
  int value, n_screens;

  g_assert_nonnull (parameter);

  n_screens = terminal_mdi_container_get_n_screens (priv->mdi_container);

  value = g_variant_get_int32 (parameter);
  if (value < 0)
    value += n_screens;
  if (value < 0 || value >= n_screens)
    return;

  g_action_change_state (G_ACTION (action), g_variant_new_int32 (value));
}

static void
action_active_tab_state_cb (GSimpleAction *action,
1339
1340
1341
1342
                            GVariant *state,
                            gpointer user_data)
{
  TerminalWindow *window = user_data;
Christian Persch's avatar
Christian Persch committed
1343
  TerminalWindowPrivate *priv = window->priv;
1344

Christian Persch's avatar
Christian Persch committed
1345
  g_assert_nonnull (state);
1346

Christian Persch's avatar
Christian Persch committed
1347
  g_simple_action_set_state (action, state);
1348

Christian Persch's avatar
Christian Persch committed
1349
  terminal_mdi_container_set_active_screen_num (priv->mdi_container, g_variant_get_int32 (state));
1350
1351
}

1352
1353
/* Menubar mnemonics & accel settings handling */

1354
static void
Christian Persch's avatar
Christian Persch committed
1355
1356
1357
enable_menubar_accel_changed_cb (GSettings *settings,
                                 const char *key,
                                 GtkSettings *gtk_settings)
1358
{
Christian Persch's avatar
Christian Persch committed
1359
1360
1361
1362
  if (g_settings_get_boolean (settings, key))
    gtk_settings_reset_property (gtk_settings, "gtk-menu-bar-accel");
  else
    g_object_set (gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
1363
1364
}

1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
/* The menubar is shown by the app, and the use of mnemonics (e.g. Alt+F for File) is toggled.
 * The mnemonic modifier is per window, so it doesn't affect the Find or Preferences windows.
 * If the menubar is shown by the shell, a non-mnemonic variant of the menu is loaded instead
 * in terminal-app.c. See over there for further details. */
static void
enable_mnemonics_changed_cb (GSettings *settings,
                             const char *key,
                             TerminalWindow *window)
{
  gboolean enabled = g_settings_get_boolean (settings, key);

  if (enabled)
    gtk_window_set_mnemonic_modifier (GTK_WINDOW (window), GDK_MOD1_MASK);
  else
    gtk_window_set_mnemonic_modifier (GTK_WINDOW (window), GDK_MODIFIER_MASK & ~GDK_RELEASE_MASK);
}

1382
static void
Christian Persch's avatar
Christian Persch committed
1383
app_setting_notify_destroy_cb (GtkSettings *gtk_settings)
1384
{
Christian Persch's avatar
Christian Persch committed
1385
1386
1387
  g_signal_handlers_disconnect_by_func (terminal_app_get_global_settings (terminal_app_get ()),
                                        G_CALLBACK (enable_menubar_accel_changed_cb),
                                        gtk_settings);
1388
1389
1390
1391
}

/* utility functions */

1392
1393
1394
1395
1396
1397
1398
1399
1400
static int
find_tab_num_at_pos (GtkNotebook *notebook,
                     int screen_x, 
                     int screen_y)
{
  GtkPositionType tab_pos;
  int page_num = 0;
  GtkNotebook *nb = GTK_NOTEBOOK (notebook);
  GtkWidget *page;