remmina_connection_window.c 137 KB
Newer Older
Antenore Gatta's avatar
Antenore Gatta committed
1 2 3 4
/*
 * Remmina - The GTK+ Remote Desktop Client
 * Copyright (C) 2009-2011 Vic Lee
 * Copyright (C) 2014-2015 Antenore Gatta, Fabio Castelli, Giovanni Panozzo
5
 * Copyright (C) 2016-2019 Antenore Gatta, Giovanni Panozzo
Antenore Gatta's avatar
Antenore Gatta committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of portions of this program with the
 *  OpenSSL library under certain conditions as described in each
 *  individual source file, and distribute linked combinations
 *  including the two.
 *  You must obey the GNU General Public License in all respects
 *  for all of the code used other than OpenSSL. *  If you modify
 *  file(s) with this exception, you may extend this exception to your
 *  version of the file(s), but you are not obligated to do so. *  If you
 *  do not wish to do so, delete this exception statement from your
 *  version. *  If you delete this exception statement from all source
 *  files in the program, then also delete it here.
 *
 */
36

37 38
#include "config.h"

39
#include <cairo/cairo-xlib.h>
40 41
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
42
#include <glib/gi18n.h>
43
#include <gtk/gtk.h>
44
#include <stdlib.h>
45

46
#include "remmina.h"
47
#include "remmina_connection_window.h"
48 49
#include "remmina_file.h"
#include "remmina_file_manager.h"
50
#include "remmina_message_panel.h"
51
#include "remmina_ext_exec.h"
52
#include "remmina_plugin_manager.h"
53
#include "remmina_pref.h"
54 55
#include "remmina_protocol_widget.h"
#include "remmina_public.h"
56
#include "remmina_scrolled_viewport.h"
57
#include "remmina_utils.h"
58
#include "remmina_widget_pool.h"
59
#include "remmina_log.h"
60
#include "remmina/remmina_trace_calls.h"
61

62
#define DEBUG_KB_GRABBING 0
63
#include "remmina_exec.h"
64

65 66 67
gchar *remmina_pref_file;
RemminaPref remmina_pref;

68
G_DEFINE_TYPE( RemminaConnectionWindow, remmina_connection_window, GTK_TYPE_WINDOW)
69 70 71

#define MOTION_TIME 100

72 73 74
/* default timeout used to hide the floating toolbar wen switching profile */
#define TB_HIDE_TIME_TIME 1000

75
typedef struct _RemminaConnectionHolder RemminaConnectionHolder;
76

Antenore Gatta's avatar
Antenore Gatta committed
77
struct _RemminaConnectionWindowPriv {
78
	RemminaConnectionHolder* cnnhld;
79

80
	GtkWidget* notebook;
81

82
	guint switch_page_handler;
83

Giovanni Panozzo's avatar
Giovanni Panozzo committed
84 85 86 87 88
	GtkWidget* floating_toolbar_widget;
	GtkWidget* overlay;
	GtkWidget* revealer;
	GtkWidget* overlay_ftb_overlay;

89
	GtkWidget* floating_toolbar_label;
90 91 92
	gdouble floating_toolbar_opacity;
	/* To avoid strange event-loop */
	guint floating_toolbar_motion_handler;
Giovanni Panozzo's avatar
Giovanni Panozzo committed
93 94
	/* Other event sources to remove when deleting the object */
	guint ftb_hide_eventsource;
95 96 97
	/* Timer to hide the toolbar */
	guint hidetb_timer;

98 99 100
	/* Timer to save new window state and wxh */
	guint savestate_eventsourceid;

101
	GtkWidget* toolbar;
102
	GtkWidget* grid;
103

104
	/* Toolitems that need to be handled */
105 106 107
	GtkToolItem* toolitem_autofit;
	GtkToolItem* toolitem_fullscreen;
	GtkToolItem* toolitem_switch_page;
108
	GtkToolItem* toolitem_dynres;
109 110 111 112
	GtkToolItem* toolitem_scale;
	GtkToolItem* toolitem_grab;
	GtkToolItem* toolitem_preferences;
	GtkToolItem* toolitem_tools;
113
	GtkToolItem* toolitem_screenshot;
114
	GtkWidget* fullscreen_option_button;
115 116
	GtkWidget* fullscreen_scaler_button;
	GtkWidget* scaler_option_button;
117 118

	GtkWidget* pin_button;
119
	gboolean pin_down;
120

121
	gboolean sticky;
122

123 124 125 126
	/* flag to disable toolbar signal handling when toolbar is
	 * reconfiguring, usually due to a tab switch */
	gboolean toolbar_is_reconfiguring;

127
	gint view_mode;
Giovanni Panozzo's avatar
Giovanni Panozzo committed
128

129 130 131
	gboolean kbcaptured;
	gboolean mouse_pointer_entered;

132 133
	RemminaConnectionWindowOnDeleteConfirmMode on_delete_confirm_mode;

134 135
};

Antenore Gatta's avatar
Antenore Gatta committed
136
typedef struct _RemminaConnectionObject {
137
	RemminaConnectionHolder* cnnhld;
138

139
	RemminaFile* remmina_file;
140

141
	GtkWidget* proto;
142
	GtkWidget* aspectframe;
143
	GtkWidget* viewport;
144

145
	GtkWidget* page;
146
	GtkWidget* scrolled_container;
147

148 149
	gboolean plugin_can_scale;

150
	gboolean connected;
151 152
	gboolean dynres_unlocked;

153 154 155

	gulong deferred_open_size_allocate_handler;

156 157
} RemminaConnectionObject;

Antenore Gatta's avatar
Antenore Gatta committed
158
struct _RemminaConnectionHolder {
159
	RemminaConnectionWindow* cnnwin;
160
	gint fullscreen_view_mode;
161
	gint grab_retry_eventsourceid;
162

163 164
	gboolean hostkey_activated;
	gboolean hostkey_used;
Giovanni Panozzo's avatar
Giovanni Panozzo committed
165

166 167
};

Antenore Gatta's avatar
Antenore Gatta committed
168
enum {
169 170 171 172 173 174 175
	TOOLBARPLACE_SIGNAL,
	LAST_SIGNAL
};

static guint remmina_connection_window_signals[LAST_SIGNAL] =
{ 0 };

176
#define DECLARE_CNNOBJ \
Antenore Gatta's avatar
Antenore Gatta committed
177 178 179 180
	if (!cnnhld || !cnnhld->cnnwin || gtk_notebook_get_current_page(GTK_NOTEBOOK(cnnhld->cnnwin->priv->notebook)) < 0) return; \
	RemminaConnectionObject* cnnobj = (RemminaConnectionObject*)g_object_get_data( \
	G_OBJECT(gtk_notebook_get_nth_page(GTK_NOTEBOOK(cnnhld->cnnwin->priv->notebook), \
			gtk_notebook_get_current_page(GTK_NOTEBOOK(cnnhld->cnnwin->priv->notebook)))), "cnnobj");
181 182

#define DECLARE_CNNOBJ_WITH_RETURN(r) \
Antenore Gatta's avatar
Antenore Gatta committed
183 184 185 186
	if (!cnnhld || !cnnhld->cnnwin || gtk_notebook_get_current_page(GTK_NOTEBOOK(cnnhld->cnnwin->priv->notebook)) < 0) return r; \
	RemminaConnectionObject* cnnobj = (RemminaConnectionObject*)g_object_get_data( \
	G_OBJECT(gtk_notebook_get_nth_page(GTK_NOTEBOOK(cnnhld->cnnwin->priv->notebook), \
			gtk_notebook_get_current_page(GTK_NOTEBOOK(cnnhld->cnnwin->priv->notebook)))), "cnnobj");
187

188 189
static void remmina_connection_holder_create_scrolled(RemminaConnectionHolder* cnnhld, RemminaConnectionObject* cnnobj);
static void remmina_connection_holder_create_fullscreen(RemminaConnectionHolder* cnnhld, RemminaConnectionObject* cnnobj,
Antenore Gatta's avatar
Antenore Gatta committed
190
							gint view_mode);
191
static gboolean remmina_connection_window_hostkey_func(RemminaProtocolWidget* gp, guint keyval, gboolean release);
192

193
static void remmina_connection_holder_grab_focus(GtkNotebook *notebook);
194 195
static GtkWidget* remmina_connection_holder_create_toolbar(RemminaConnectionHolder* cnnhld, gint mode);
static void remmina_connection_holder_place_toolbar(GtkToolbar *toolbar, GtkGrid *grid, GtkWidget *sibling, int toolbar_placement);
196
static void remmina_connection_holder_keyboard_grab(RemminaConnectionHolder* cnnhld);
197

Giovanni Panozzo's avatar
Giovanni Panozzo committed
198 199
static void remmina_connection_window_ftb_drag_begin(GtkWidget *widget, GdkDragContext *context, gpointer user_data);

200
static const GtkTargetEntry dnd_targets_ftb[] =
201
{
Giovanni Panozzo's avatar
Giovanni Panozzo committed
202
	{
Antenore Gatta's avatar
Antenore Gatta committed
203 204
		(char*)"text/x-remmina-ftb",
		GTK_TARGET_SAME_APP | GTK_TARGET_OTHER_WIDGET,
Giovanni Panozzo's avatar
Giovanni Panozzo committed
205 206 207 208
		0
	},
};

209 210 211
static const GtkTargetEntry dnd_targets_tb[] =
{
	{
Antenore Gatta's avatar
Antenore Gatta committed
212
		(char*)"text/x-remmina-tb",
213 214 215 216
		GTK_TARGET_SAME_APP,
		0
	},
};
217

218
static void remmina_connection_window_class_init(RemminaConnectionWindowClass* klass)
219
{
220
	TRACE_CALL(__func__);
221 222 223
	GtkCssProvider  *provider;

	provider = gtk_css_provider_new();
224 225 226 227

	/* It's important to remove padding, border and shadow from GtkViewport or
	 * we will never know its internal area size, because GtkViweport::viewport_get_view_allocation,
	 * which returns the internal size of the GtkViewport, is private and we cannot access it */
228 229

#if GTK_CHECK_VERSION(3, 14, 0)
Antenore Gatta's avatar
Antenore Gatta committed
230 231 232 233
	gtk_css_provider_load_from_data(provider,
		"#remmina-cw-viewport, #remmina-cw-aspectframe {\n"
		"  padding:0;\n"
		"  border:0;\n"
234
		"  background-color: black;\n"
Antenore Gatta's avatar
Antenore Gatta committed
235 236 237 238 239 240 241
		"}\n"
		"GtkDrawingArea {\n"
		"}\n"
		"GtkToolbar {\n"
		"  -GtkWidget-window-dragging: 0;\n"
		"}\n"
		"#remmina-connection-window-fullscreen {\n"
242 243
		"  border-color: black;\n"
		"  background-color: black;\n"
Antenore Gatta's avatar
Antenore Gatta committed
244 245 246 247 248 249 250 251 252 253 254 255 256
		"}\n"
		"#remmina-small-button {\n"
		"  outline-offset: 0;\n"
		"  outline-width: 0;\n"
		"  padding: 0;\n"
		"  border: 0;\n"
		"}\n"
		"#remmina-pin-button {\n"
		"  outline-offset: 0;\n"
		"  outline-width: 0;\n"
		"  padding: 2px;\n"
		"  border: 0;\n"
		"}\n"
Giovanni Panozzo's avatar
Giovanni Panozzo committed
257
		"#remmina-page {\n"
258
		"  background-color: black;\n"
Giovanni Panozzo's avatar
Giovanni Panozzo committed
259
		"}\n"
Antenore Gatta's avatar
Antenore Gatta committed
260 261 262 263 264 265
		"#remmina-scrolled-container {\n"
		"}\n"
		"#remmina-scrolled-container.undershoot {\n"
		"  background: none\n"
		"}\n"
		"#ftbbox-upper {\n"
266 267
		"  background-color: white;\n"
		"  color: black;\n"
Antenore Gatta's avatar
Antenore Gatta committed
268 269 270 271 272 273
		"  border-style: none solid solid solid;\n"
		"  border-width: 1px;\n"
		"  border-radius: 4px;\n"
		"  padding: 0px;\n"
		"}\n"
		"#ftbbox-lower {\n"
274 275
		"  background-color: white;\n"
		"  color: black;\n"
Antenore Gatta's avatar
Antenore Gatta committed
276 277 278 279 280 281 282
		"  border-style: solid solid none solid;\n"
		"  border-width: 1px;\n"
		"  border-radius: 4px;\n"
		"  padding: 0px;\n"
		"}\n"
		"#ftb-handle {\n"
		"}\n"
283
		".message_panel {\n"
284
		"  border: 0px solid;\n"
285 286 287 288 289 290 291 292 293 294
		"  padding: 20px 20px 20px 20px;\n"
		"}\n"
		".message_panel entry {\n"
		"  background-image: none;\n"
		"  border-width: 4px;\n"
		"  border-radius: 8px;\n"
		"}\n"
		".message_panel .title_label {\n"
		"  font-size: 2em; \n"
		"}\n"
Antenore Gatta's avatar
Antenore Gatta committed
295
		, -1, NULL);
296 297

#else
Antenore Gatta's avatar
Antenore Gatta committed
298 299 300 301
	gtk_css_provider_load_from_data(provider,
		"#remmina-cw-viewport, #remmina-cw-aspectframe {\n"
		"  padding:0;\n"
		"  border:0;\n"
302
		"  background-color: black;\n"
Antenore Gatta's avatar
Antenore Gatta committed
303
		"}\n"
304 305
		"#remmina-cw-message-panel {\n"
		"}\n"
Antenore Gatta's avatar
Antenore Gatta committed
306 307 308 309 310 311
		"GtkDrawingArea {\n"
		"}\n"
		"GtkToolbar {\n"
		"  -GtkWidget-window-dragging: 0;\n"
		"}\n"
		"#remmina-connection-window-fullscreen {\n"
312
		"  border-color: black;\n"
313
		"  background-color: black;\n"
Antenore Gatta's avatar
Antenore Gatta committed
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
		"}\n"
		"#remmina-small-button {\n"
		"  -GtkWidget-focus-padding: 0;\n"
		"  -GtkWidget-focus-line-width: 0;\n"
		"  padding: 0;\n"
		"  border: 0;\n"
		"}\n"
		"#remmina-pin-button {\n"
		"  -GtkWidget-focus-padding: 0;\n"
		"  -GtkWidget-focus-line-width: 0;\n"
		"  padding: 2px;\n"
		"  border: 0;\n"
		"}\n"
		"#remmina-scrolled-container {\n"
		"}\n"
		"#remmina-scrolled-container.undershoot {\n"
		"  background: none\n"
		"}\n"
		"#ftbbox-upper {\n"
		"  border-style: none solid solid solid;\n"
		"  border-width: 1px;\n"
		"  border-radius: 4px;\n"
		"  padding: 0px;\n"
		"}\n"
		"#ftbbox-lower {\n"
		"  border-style: solid solid none solid;\n"
		"  border-width: 1px;\n"
		"  border-radius: 4px;\n"
		"  padding: 0px;\n"
		"}\n"
		"#ftb-handle {\n"
		"}\n"

		, -1, NULL);
348
#endif
349

Antenore Gatta's avatar
Antenore Gatta committed
350 351 352
	gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
		GTK_STYLE_PROVIDER(provider),
		GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
Giovanni Panozzo's avatar
Giovanni Panozzo committed
353

354
	g_object_unref(provider);
355 356 357

	/* Define a signal used to notify all remmina_connection_windows of toolbar move */
	remmina_connection_window_signals[TOOLBARPLACE_SIGNAL] = g_signal_new("toolbar-place", G_TYPE_FROM_CLASS(klass),
Antenore Gatta's avatar
Antenore Gatta committed
358 359
		G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET(RemminaConnectionWindowClass, toolbar_place), NULL, NULL,
		g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
360

361 362
}

363 364
static RemminaScaleMode get_current_allowed_scale_mode(RemminaConnectionObject* cnnobj, gboolean *dynres_avail, gboolean *scale_avail)
{
365
	TRACE_CALL(__func__);
366 367 368 369 370 371
	RemminaScaleMode scalemode;
	gboolean plugin_has_dynres, plugin_can_scale;

	scalemode = remmina_protocol_widget_get_current_scale_mode(REMMINA_PROTOCOL_WIDGET(cnnobj->proto));

	plugin_has_dynres = remmina_protocol_widget_query_feature_by_type(REMMINA_PROTOCOL_WIDGET(cnnobj->proto),
Antenore Gatta's avatar
Antenore Gatta committed
372
		REMMINA_PROTOCOL_FEATURE_TYPE_SCALE);
373 374

	plugin_can_scale = remmina_protocol_widget_query_feature_by_type(REMMINA_PROTOCOL_WIDGET(cnnobj->proto),
Antenore Gatta's avatar
Antenore Gatta committed
375
		REMMINA_PROTOCOL_FEATURE_TYPE_SCALE);
376 377

	/* forbid scalemode REMMINA_PROTOCOL_WIDGET_SCALE_MODE_DYNRES when not possible */
378
	if ((!plugin_has_dynres) && scalemode == REMMINA_PROTOCOL_WIDGET_SCALE_MODE_DYNRES)
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
		scalemode = REMMINA_PROTOCOL_WIDGET_SCALE_MODE_NONE;

	/* forbid scalemode REMMINA_PROTOCOL_WIDGET_SCALE_MODE_SCALED when not possible */
	if (!plugin_can_scale && scalemode == REMMINA_PROTOCOL_WIDGET_SCALE_MODE_SCALED)
		scalemode = REMMINA_PROTOCOL_WIDGET_SCALE_MODE_NONE;

	if (scale_avail)
		*scale_avail = plugin_can_scale;
	if (dynres_avail)
		*dynres_avail = (plugin_has_dynres && cnnobj->dynres_unlocked);

	return scalemode;

}

Giovanni Panozzo's avatar
Giovanni Panozzo committed
394
static void remmina_connection_holder_disconnect_current_page(RemminaConnectionHolder* cnnhld)
395
{
396
	TRACE_CALL(__func__);
397 398
	DECLARE_CNNOBJ

Antenore Gatta's avatar
Antenore Gatta committed
399
	/* Disconnects the connection which is currently in view in the notebook */
Giovanni Panozzo's avatar
Giovanni Panozzo committed
400

Antenore Gatta's avatar
Antenore Gatta committed
401
	remmina_protocol_widget_close_connection(REMMINA_PROTOCOL_WIDGET(cnnobj->proto));
402 403
}

404
static void remmina_connection_holder_keyboard_ungrab(RemminaConnectionHolder* cnnhld)
405
{
406
	TRACE_CALL(__func__);
Antenore Gatta's avatar
Antenore Gatta committed
407
	GdkDisplay *display;
408 409 410
#if GTK_CHECK_VERSION(3, 20, 0)
	GdkSeat *seat;
#else
411
	GdkDeviceManager *manager;
412
#endif
413
	GdkDevice *keyboard = NULL;
414

415 416 417 418 419
	if (cnnhld->grab_retry_eventsourceid) {
		g_source_remove(cnnhld->grab_retry_eventsourceid);
		cnnhld->grab_retry_eventsourceid = 0;
	}

420
	display = gtk_widget_get_display(GTK_WIDGET(cnnhld->cnnwin));
421 422 423 424
#if GTK_CHECK_VERSION(3, 20, 0)
	seat = gdk_display_get_default_seat(display);
	keyboard = gdk_seat_get_pointer(seat);
#else
425 426
	manager = gdk_display_get_device_manager(display);
	keyboard = gdk_device_manager_get_client_pointer(manager);
427 428
#endif

Antenore Gatta's avatar
Antenore Gatta committed
429
	if (!cnnhld->cnnwin->priv->kbcaptured) {
430 431
		return;
	}
432

Antenore Gatta's avatar
Antenore Gatta committed
433 434
	if (keyboard != NULL) {
		if ( gdk_device_get_source(keyboard) != GDK_SOURCE_KEYBOARD ) {
435
			keyboard = gdk_device_get_associated_device(keyboard);
436
		}
437 438 439
#if DEBUG_KB_GRABBING
		printf("DEBUG_KB_GRABBING: --- ungrabbing\n");
#endif
440

441 442
#if GTK_CHECK_VERSION(3, 24, 0)
		/* We can use gtk_seat_grab()/_ungrab() only after GTK 3.24 */
443
		gdk_seat_ungrab(seat);
444
#else
445
		G_GNUC_BEGIN_IGNORE_DEPRECATIONS
446
		gdk_device_ungrab(keyboard, GDK_CURRENT_TIME);
447
		G_GNUC_END_IGNORE_DEPRECATIONS
448 449 450
#endif
		cnnhld->cnnwin->priv->kbcaptured = FALSE;

451 452 453
	}
}

454 455 456 457 458 459 460 461 462 463 464
static gboolean remmina_connection_holder_keyboard_grab_retry(gpointer user_data)
{
	TRACE_CALL(__func__);
	RemminaConnectionHolder* cnnhld;
	cnnhld = (RemminaConnectionHolder *)user_data;

	remmina_connection_holder_keyboard_grab(cnnhld);
	cnnhld->grab_retry_eventsourceid = 0;
	return G_SOURCE_REMOVE;
}

465
static void remmina_connection_holder_keyboard_grab(RemminaConnectionHolder* cnnhld)
466
{
467
	TRACE_CALL(__func__);
468
	DECLARE_CNNOBJ
469
	GdkDisplay *display;
470 471 472
#if GTK_CHECK_VERSION(3, 20, 0)
	GdkSeat *seat;
#else
473
	GdkDeviceManager *manager;
474
#endif
475
	GdkGrabStatus ggs;
476 477
	GdkDevice *keyboard = NULL;

Antenore Gatta's avatar
Antenore Gatta committed
478
	if (cnnhld->cnnwin->priv->kbcaptured || !cnnhld->cnnwin->priv->mouse_pointer_entered) {
479 480 481
		return;
	}

482
	display = gtk_widget_get_display(GTK_WIDGET(cnnhld->cnnwin));
483 484 485 486
#if GTK_CHECK_VERSION(3, 20, 0)
	seat = gdk_display_get_default_seat(display);
	keyboard = gdk_seat_get_pointer(seat);
#else
487 488
	manager = gdk_display_get_device_manager(display);
	keyboard = gdk_device_manager_get_client_pointer(manager);
489
#endif
490

Antenore Gatta's avatar
Antenore Gatta committed
491
	if (keyboard != NULL) {
492

Antenore Gatta's avatar
Antenore Gatta committed
493
		if ( gdk_device_get_source(keyboard) != GDK_SOURCE_KEYBOARD) {
494
			keyboard = gdk_device_get_associated_device( keyboard );
495 496
		}

Antenore Gatta's avatar
Antenore Gatta committed
497
		if (remmina_file_get_int(cnnobj->remmina_file, "keyboard_grab", FALSE)) {
498
#if DEBUG_KB_GRABBING
499
			printf("DEBUG_KB_GRABBING: profile asks for grabbing, let's try.\n");
500
#endif
501 502 503 504 505 506 507 508 509 510 511
	/* Up to GTK version 3.20 we can grab the keyboard with gdk_device_grab().
	 * in GTK 3.20 gdk_seat_grab() should be used instead of gdk_device_grab().
	 * There is a bug in GTK up to 3.22: when gdk_device_grab() fails
	 * the widget is hidden:
	 * https://gitlab.gnome.org/GNOME/gtk/commit/726ad5a5ae7c4f167e8dd454cd7c250821c400ab
	 * The bug fix will be released with GTK 3.24.
	 * Also pease note that the newer gdk_seat_grab() is still calling gdk_device_grab().
	 */
#if GTK_CHECK_VERSION(3, 24, 0)
			ggs = gdk_seat_grab(seat, gtk_widget_get_window(GTK_WIDGET(cnnhld->cnnwin)),
				GDK_SEAT_CAPABILITY_KEYBOARD, FALSE, NULL, NULL, NULL, NULL);
512
#else
513 514 515 516
			G_GNUC_BEGIN_IGNORE_DEPRECATIONS
			ggs = gdk_device_grab(keyboard, gtk_widget_get_window(GTK_WIDGET(cnnhld->cnnwin)), GDK_OWNERSHIP_WINDOW,
				    TRUE, GDK_KEY_PRESS | GDK_KEY_RELEASE, NULL, GDK_CURRENT_TIME);
			G_GNUC_END_IGNORE_DEPRECATIONS
517
#endif
518
			if ( ggs != GDK_GRAB_SUCCESS )
519
			{
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
				/* Failure to GRAB keyboard */
				#if DEBUG_KB_GRABBING
					printf("GRAB FAILED. GdkGrabStatus: %d\n", (int)ggs);
				#endif
				/* Reschedule grabbing in half a second if not already done */
				if (cnnhld->grab_retry_eventsourceid == 0) {
					cnnhld->grab_retry_eventsourceid = g_timeout_add(500, (GSourceFunc)remmina_connection_holder_keyboard_grab_retry, cnnhld);
				}
			} else {
			#if DEBUG_KB_GRABBING
				printf("GRAB SUCCESS\n");
			#endif
				if (cnnhld->grab_retry_eventsourceid != 0) {
					g_source_remove(cnnhld->grab_retry_eventsourceid);
					cnnhld->grab_retry_eventsourceid = 0;
				}
536 537
				cnnhld->cnnwin->priv->kbcaptured = TRUE;
			}
538
		}else {
539 540 541
			remmina_connection_holder_keyboard_ungrab(cnnhld);
		}
	}
542 543
}

Giovanni Panozzo's avatar
Giovanni Panozzo committed
544 545 546 547 548 549 550 551
static void remmina_connection_window_close_all_connections(RemminaConnectionWindow* cnnwin)
{
	RemminaConnectionWindowPriv* priv = cnnwin->priv;
	GtkNotebook* notebook = GTK_NOTEBOOK(priv->notebook);
	GtkWidget* w;
	RemminaConnectionObject* cnnobj;
	gint i, n;

Antenore Gatta's avatar
Antenore Gatta committed
552
	if (GTK_IS_WIDGET(notebook)) {
Giovanni Panozzo's avatar
Giovanni Panozzo committed
553
		n = gtk_notebook_get_n_pages(notebook);
Antenore Gatta's avatar
Antenore Gatta committed
554
		for (i = n - 1; i >= 0; i--) {
Giovanni Panozzo's avatar
Giovanni Panozzo committed
555
			w = gtk_notebook_get_nth_page(notebook, i);
Antenore Gatta's avatar
Antenore Gatta committed
556
			cnnobj = (RemminaConnectionObject*)g_object_get_data(G_OBJECT(w), "cnnobj");
Giovanni Panozzo's avatar
Giovanni Panozzo committed
557 558 559 560 561 562 563
			/* Do close the connection on this tab */
			remmina_protocol_widget_close_connection(REMMINA_PROTOCOL_WIDGET(cnnobj->proto));
		}
	}

}

564
gboolean remmina_connection_window_delete(RemminaConnectionWindow* cnnwin)
565
{
566
	TRACE_CALL(__func__);
Giovanni Panozzo's avatar
Giovanni Panozzo committed
567 568
	RemminaConnectionWindowPriv* priv = cnnwin->priv;
	RemminaConnectionHolder *cnnhld = cnnwin->priv->cnnhld;
569 570
	GtkNotebook* notebook = GTK_NOTEBOOK(priv->notebook);
	GtkWidget* dialog;
571 572
	gint i, n;

Giovanni Panozzo's avatar
Giovanni Panozzo committed
573
	if (!REMMINA_IS_CONNECTION_WINDOW(cnnwin))
574
		return TRUE;
Giovanni Panozzo's avatar
Giovanni Panozzo committed
575

576 577
	if (cnnwin->priv->on_delete_confirm_mode != REMMINA_CONNECTION_WINDOW_ONDELETE_NOCONFIRM) {
		n = gtk_notebook_get_n_pages(notebook);
Antenore Gatta's avatar
Antenore Gatta committed
578
		if (n > 1) {
579
			dialog = gtk_message_dialog_new(GTK_WINDOW(cnnwin), GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION,
Antenore Gatta's avatar
Antenore Gatta committed
580 581
				GTK_BUTTONS_YES_NO,
				_("There are %i active connections in the current window. Are you sure to close?"), n);
582 583 584 585 586
			i = gtk_dialog_run(GTK_DIALOG(dialog));
			gtk_widget_destroy(dialog);
			if (i != GTK_RESPONSE_YES)
				return FALSE;
		}
587
	}
Giovanni Panozzo's avatar
Giovanni Panozzo committed
588
	remmina_connection_window_close_all_connections(cnnwin);
Giovanni Panozzo's avatar
Giovanni Panozzo committed
589

590 591 592 593 594
	/* After remmina_connection_window_close_all_connections() call, cnnwin
	 * has been already destroyed during a last page of notebook removal.
	 * So we must rely on cnnhld */
	if (cnnhld->cnnwin != NULL && GTK_IS_WIDGET(cnnhld->cnnwin))
		gtk_widget_destroy(GTK_WIDGET(cnnhld->cnnwin));
Giovanni Panozzo's avatar
Giovanni Panozzo committed
595 596
	cnnhld->cnnwin = NULL;

597
	return TRUE;
Giovanni Panozzo's avatar
Giovanni Panozzo committed
598 599 600 601
}

static gboolean remmina_connection_window_delete_event(GtkWidget* widget, GdkEvent* event, gpointer data)
{
602
	TRACE_CALL(__func__);
Giovanni Panozzo's avatar
Giovanni Panozzo committed
603
	remmina_connection_window_delete(REMMINA_CONNECTION_WINDOW(widget));
604 605 606
	return TRUE;
}

607
static void remmina_connection_window_destroy(GtkWidget* widget, RemminaConnectionHolder* cnnhld)
608
{
609
	TRACE_CALL(__func__);
610
	RemminaConnectionWindowPriv* priv = REMMINA_CONNECTION_WINDOW(widget)->priv;
611

612 613 614
	if (priv->kbcaptured)
		remmina_connection_holder_keyboard_ungrab(cnnhld);

Antenore Gatta's avatar
Antenore Gatta committed
615
	if (priv->floating_toolbar_motion_handler) {
616 617 618
		g_source_remove(priv->floating_toolbar_motion_handler);
		priv->floating_toolbar_motion_handler = 0;
	}
Antenore Gatta's avatar
Antenore Gatta committed
619
	if (priv->ftb_hide_eventsource) {
Giovanni Panozzo's avatar
Giovanni Panozzo committed
620 621
		g_source_remove(priv->ftb_hide_eventsource);
		priv->ftb_hide_eventsource = 0;
622
	}
Antenore Gatta's avatar
Antenore Gatta committed
623
	if (priv->savestate_eventsourceid) {
624 625 626
		g_source_remove(priv->savestate_eventsourceid);
		priv->savestate_eventsourceid = 0;
	}
Giovanni Panozzo's avatar
Giovanni Panozzo committed
627 628 629

	/* There is no need to destroy priv->floating_toolbar_widget,
	 * because it's our child and will be destroyed automatically */
630

631
	/* Timer used to hide the toolbar */
Antenore Gatta's avatar
Antenore Gatta committed
632
	if (priv->hidetb_timer) {
633
		g_source_remove(priv->hidetb_timer);
Antenore Gatta's avatar
Antenore Gatta committed
634
		priv->hidetb_timer = 0;
635
	}
Antenore Gatta's avatar
Antenore Gatta committed
636
	if (priv->switch_page_handler) {
637 638 639 640
		g_source_remove(priv->switch_page_handler);
		priv->switch_page_handler = 0;
	}
	g_free(priv);
641

Antenore Gatta's avatar
Antenore Gatta committed
642
	if (GTK_WIDGET(cnnhld->cnnwin) == widget) {
643 644 645
		cnnhld->cnnwin->priv = NULL;
		cnnhld->cnnwin = NULL;
	}
Giovanni Panozzo's avatar
Giovanni Panozzo committed
646

647 648
}

649 650
gboolean remmina_connection_window_notify_widget_toolbar_placement(GtkWidget *widget, gpointer data)
{
651
	TRACE_CALL(__func__);
652 653 654 655 656 657 658 659 660 661
	GType rcwtype;
	rcwtype = remmina_connection_window_get_type();
	if (G_TYPE_CHECK_INSTANCE_TYPE(widget, rcwtype)) {
		g_signal_emit_by_name(G_OBJECT(widget), "toolbar-place");
		return TRUE;
	}
	return FALSE;
}

static gboolean remmina_connection_window_tb_drag_failed(GtkWidget *widget, GdkDragContext *context,
Antenore Gatta's avatar
Antenore Gatta committed
662
							 GtkDragResult result, gpointer user_data)
663
{
664
	TRACE_CALL(__func__);
665 666 667 668 669 670 671 672 673 674 675 676 677
	RemminaConnectionHolder* cnnhld;
	RemminaConnectionWindowPriv* priv;

	cnnhld = (RemminaConnectionHolder*)user_data;
	priv = cnnhld->cnnwin->priv;

	if (priv->toolbar)
		gtk_widget_show(GTK_WIDGET(priv->toolbar));

	return TRUE;
}

static gboolean remmina_connection_window_tb_drag_drop(GtkWidget *widget, GdkDragContext *context,
Antenore Gatta's avatar
Antenore Gatta committed
678
						       gint x, gint y, guint time, gpointer user_data)
679
{
680
	TRACE_CALL(__func__);
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
	GtkAllocation wa;
	gint new_toolbar_placement;
	RemminaConnectionHolder* cnnhld;
	RemminaConnectionWindowPriv* priv;

	cnnhld = (RemminaConnectionHolder*)user_data;
	priv = cnnhld->cnnwin->priv;

	gtk_widget_get_allocation(widget, &wa);

	if (wa.width * y >= wa.height * x) {
		if (wa.width * y > wa.height * ( wa.width - x) )
			new_toolbar_placement = TOOLBAR_PLACEMENT_BOTTOM;
		else
			new_toolbar_placement = TOOLBAR_PLACEMENT_LEFT;
696
	}else {
697 698 699 700 701 702 703 704
		if (wa.width * y > wa.height * ( wa.width - x) )
			new_toolbar_placement = TOOLBAR_PLACEMENT_RIGHT;
		else
			new_toolbar_placement = TOOLBAR_PLACEMENT_TOP;
	}

	gtk_drag_finish(context, TRUE, TRUE, time);

Antenore Gatta's avatar
Antenore Gatta committed
705
	if (new_toolbar_placement !=  remmina_pref.toolbar_placement) {
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
		/* Save new position */
		remmina_pref.toolbar_placement = new_toolbar_placement;
		remmina_pref_save();

		/* Signal all windows that the toolbar must be moved */
		remmina_widget_pool_foreach(remmina_connection_window_notify_widget_toolbar_placement, NULL);

	}
	if (priv->toolbar)
		gtk_widget_show(GTK_WIDGET(priv->toolbar));

	return TRUE;

}

static void remmina_connection_window_tb_drag_begin(GtkWidget *widget, GdkDragContext *context, gpointer user_data)
{
723
	TRACE_CALL(__func__);
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

	cairo_surface_t *surface;
	cairo_t *cr;
	GtkAllocation wa;
	double dashes[] = { 10 };

	gtk_widget_get_allocation(widget, &wa);

	surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 16, 16);
	cr = cairo_create(surface);
	cairo_set_source_rgb(cr, 0.6, 0.6, 0.6);
	cairo_set_line_width(cr, 4);
	cairo_set_dash(cr, dashes, 1, 0 );
	cairo_rectangle(cr, 0, 0, 16, 16);
	cairo_stroke(cr);
	cairo_destroy(cr);

	gtk_widget_hide(widget);

	gtk_drag_set_icon_surface(context, surface);

}

747
static void remmina_connection_holder_update_toolbar_opacity(RemminaConnectionHolder* cnnhld)
748
{
749
	TRACE_CALL(__func__);
750
	DECLARE_CNNOBJ
Antenore Gatta's avatar
Antenore Gatta committed
751
	RemminaConnectionWindowPriv* priv = cnnhld->cnnwin->priv;
752

Antenore Gatta's avatar
Antenore Gatta committed
753 754 755 756
	priv->floating_toolbar_opacity = (1.0 - TOOLBAR_OPACITY_MIN) / ((gdouble)TOOLBAR_OPACITY_LEVEL)
					 * ((gdouble)(TOOLBAR_OPACITY_LEVEL - remmina_file_get_int(cnnobj->remmina_file, "toolbar_opacity", 0)))
					 + TOOLBAR_OPACITY_MIN;
	if (priv->floating_toolbar_widget) {
Giovanni Panozzo's avatar
Giovanni Panozzo committed
757 758
		gtk_widget_set_opacity(GTK_WIDGET(priv->overlay_ftb_overlay), priv->floating_toolbar_opacity);
	}
759
}
Giovanni Panozzo's avatar
Giovanni Panozzo committed
760 761 762

static gboolean remmina_connection_holder_floating_toolbar_make_invisible(gpointer data)
{
763
	TRACE_CALL(__func__);
Giovanni Panozzo's avatar
Giovanni Panozzo committed
764 765 766 767 768
	RemminaConnectionWindowPriv* priv = (RemminaConnectionWindowPriv*)data;
	gtk_widget_set_opacity(GTK_WIDGET(priv->overlay_ftb_overlay), 0.0);
	priv->ftb_hide_eventsource = 0;
	return FALSE;
}
769

770
static void remmina_connection_holder_floating_toolbar_show(RemminaConnectionHolder* cnnhld, gboolean show)
771
{
772
	TRACE_CALL(__func__);
773
	RemminaConnectionWindowPriv* priv = cnnhld->cnnwin->priv;
774

Giovanni Panozzo's avatar
Giovanni Panozzo committed
775 776 777
	if (priv->floating_toolbar_widget == NULL)
		return;

Antenore Gatta's avatar
Antenore Gatta committed
778
	if (show || priv->pin_down) {
Giovanni Panozzo's avatar
Giovanni Panozzo committed
779 780 781
		/* Make the FTB no longer transparent, in case we have an hidden toolbar */
		remmina_connection_holder_update_toolbar_opacity(cnnhld);
		/* Remove outstanding hide events, if not yet active */
Antenore Gatta's avatar
Antenore Gatta committed
782
		if (priv->ftb_hide_eventsource) {
Giovanni Panozzo's avatar
Giovanni Panozzo committed
783 784 785
			g_source_remove(priv->ftb_hide_eventsource);
			priv->ftb_hide_eventsource = 0;
		}
786
	}else {
Giovanni Panozzo's avatar
Giovanni Panozzo committed
787 788
		/* If we are hiding and the toolbar must be made invisible, schedule
		 * a later toolbar hide */
Antenore Gatta's avatar
Antenore Gatta committed
789
		if (remmina_pref.fullscreen_toolbar_visibility == FLOATING_TOOLBAR_VISIBILITY_INVISIBLE) {
Giovanni Panozzo's avatar
Giovanni Panozzo committed
790 791 792 793 794 795
			if (priv->ftb_hide_eventsource == 0)
				priv->ftb_hide_eventsource = g_timeout_add(1000, remmina_connection_holder_floating_toolbar_make_invisible, priv);
		}
	}

	gtk_revealer_set_reveal_child(GTK_REVEALER(priv->revealer), show || priv->pin_down);
796 797 798

}

799
static void remmina_connection_holder_get_desktop_size(RemminaConnectionHolder* cnnhld, gint* width, gint* height)
800
{
801
	TRACE_CALL(__func__);
802
	DECLARE_CNNOBJ
Antenore Gatta's avatar
Antenore Gatta committed
803
	RemminaProtocolWidget* gp = REMMINA_PROTOCOL_WIDGET(cnnobj->proto);
804

805

806 807 808 809
	*width = remmina_protocol_widget_get_width(gp);
	*height = remmina_protocol_widget_get_height(gp);
}

810
static void remmina_connection_object_set_scrolled_policy(RemminaConnectionObject* cnnobj, GtkScrolledWindow* scrolled_window)
811
{
812
	TRACE_CALL(__func__);
813 814 815 816 817
	RemminaScaleMode scalemode;
	scalemode = get_current_allowed_scale_mode(cnnobj, NULL, NULL);
	gtk_scrolled_window_set_policy(scrolled_window,
		scalemode == REMMINA_PROTOCOL_WIDGET_SCALE_MODE_SCALED ? GTK_POLICY_NEVER : GTK_POLICY_AUTOMATIC,
		scalemode == REMMINA_PROTOCOL_WIDGET_SCALE_MODE_SCALED ? GTK_POLICY_NEVER : GTK_POLICY_AUTOMATIC);
818
}
819

820
static gboolean remmina_connection_holder_toolbar_autofit_restore(RemminaConnectionHolder* cnnhld)
821
{
822
	TRACE_CALL(__func__);
823
	DECLARE_CNNOBJ_WITH_RETURN(FALSE)
Antenore Gatta's avatar
Antenore Gatta committed
824
	RemminaConnectionWindowPriv * priv = cnnhld->cnnwin->priv;
825 826
	gint dwidth, dheight;
	GtkAllocation nba, ca, ta;
827

828 829 830
	if (priv->toolbar_is_reconfiguring)
		return FALSE;

Antenore Gatta's avatar
Antenore Gatta committed
831
	if (cnnobj->connected && GTK_IS_SCROLLED_WINDOW(cnnobj->scrolled_container)) {
832 833
		remmina_connection_holder_get_desktop_size(cnnhld, &dwidth, &dheight);
		gtk_widget_get_allocation(priv->notebook, &nba);
834
		gtk_widget_get_allocation(cnnobj->scrolled_container, &ca);
835
		gtk_widget_get_allocation(priv->toolbar, &ta);
836
		if (remmina_pref.toolbar_placement == TOOLBAR_PLACEMENT_LEFT ||
Antenore Gatta's avatar
Antenore Gatta committed
837
		    remmina_pref.toolbar_placement == TOOLBAR_PLACEMENT_RIGHT) {
838
			gtk_window_resize(GTK_WINDOW(cnnhld->cnnwin), MAX(1, dwidth + ta.width + nba.width - ca.width),
Antenore Gatta's avatar
Antenore Gatta committed
839
				MAX(1, dheight + nba.height - ca.height));
840
		}else {
841
			gtk_window_resize(GTK_WINDOW(cnnhld->cnnwin), MAX(1, dwidth + nba.width - ca.width),
Antenore Gatta's avatar
Antenore Gatta committed
842
				MAX(1, dheight + ta.height + nba.height - ca.height));
843
		}
844 845
		gtk_container_check_resize(GTK_CONTAINER(cnnhld->cnnwin));
	}
Antenore Gatta's avatar
Antenore Gatta committed
846
	if (GTK_IS_SCROLLED_WINDOW(cnnobj->scrolled_container)) {
847 848 849
		remmina_connection_object_set_scrolled_policy(cnnobj, GTK_SCROLLED_WINDOW(cnnobj->scrolled_container));
	}
	return FALSE;
850 851
}

852
static void remmina_connection_holder_toolbar_autofit(GtkWidget* widget, RemminaConnectionHolder* cnnhld)
853
{
854
	TRACE_CALL(__func__);
855
	DECLARE_CNNOBJ
856

Antenore Gatta's avatar
Antenore Gatta committed
857 858 859 860
	if (GTK_IS_SCROLLED_WINDOW(cnnobj->scrolled_container)) {
		if ((gdk_window_get_state(gtk_widget_get_window(GTK_WIDGET(cnnhld->cnnwin))) & GDK_WINDOW_STATE_MAXIMIZED) != 0) {
			gtk_window_unmaximize(GTK_WINDOW(cnnhld->cnnwin));
		}
861

Antenore Gatta's avatar
Antenore Gatta committed
862 863 864 865
		/* It's tricky to make the toolbars disappear automatically, while keeping scrollable.
		   Please tell me if you know a better way to do this */
		gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(cnnobj->scrolled_container), GTK_POLICY_NEVER,
			GTK_POLICY_NEVER);
866

867
		/** @todo save returned source id in priv->something and then delete when main object is destroyed */
Antenore Gatta's avatar
Antenore Gatta committed
868 869
		g_timeout_add(200, (GSourceFunc)remmina_connection_holder_toolbar_autofit_restore, cnnhld);
	}
870 871 872 873

}


874
static void remmina_connection_holder_check_resize(RemminaConnectionHolder* cnnhld)
875
{
876
	TRACE_CALL(__func__);
877
	DECLARE_CNNOBJ
Antenore Gatta's avatar
Antenore Gatta committed
878
	gboolean scroll_required = FALSE;
879 880 881 882 883

#if GTK_CHECK_VERSION(3, 22, 0)
	GdkDisplay* display;
	GdkMonitor* monitor;
#else
884
	GdkScreen* screen;
885
	gint monitor;
886
#endif
887
	GdkRectangle screen_size;
888 889
	gint screen_width, screen_height;
	gint server_width, server_height;
Giovanni Panozzo's avatar
Giovanni Panozzo committed
890
	gint bordersz;
891

892
	remmina_connection_holder_get_desktop_size(cnnhld, &server_width, &server_height);
893 894 895 896
#if GTK_CHECK_VERSION(3, 22, 0)
	display = gtk_widget_get_display(GTK_WIDGET(cnnhld->cnnwin));
	monitor = gdk_display_get_monitor_at_window(display, gtk_widget_get_window(GTK_WIDGET(cnnhld->cnnwin)));
#else
897 898
	screen = gtk_window_get_screen(GTK_WINDOW(cnnhld->cnnwin));
	monitor = gdk_screen_get_monitor_at_window(screen, gtk_widget_get_window(GTK_WIDGET(cnnhld->cnnwin)));
899 900 901 902
#endif
#if GTK_CHECK_VERSION(3, 22, 0)
	gdk_monitor_get_workarea(monitor, &screen_size);
#elif gdk_screen_get_monitor_workarea
903 904 905 906 907 908
	gdk_screen_get_monitor_workarea(screen, monitor, &screen_size);
#else
	gdk_screen_get_monitor_geometry(screen, monitor, &screen_size);
#endif
	screen_width = screen_size.width;
	screen_height = screen_size.height;
909 910

	if (!remmina_protocol_widget_get_expand(REMMINA_PROTOCOL_WIDGET(cnnobj->proto))
Antenore Gatta's avatar
Antenore Gatta committed
911 912
	    && (server_width <= 0 || server_height <= 0 || screen_width < server_width
		|| screen_height < server_height)) {
913 914
		scroll_required = TRUE;
	}
915

Antenore Gatta's avatar
Antenore Gatta committed
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
	switch (cnnhld->cnnwin->priv->view_mode) {
	case SCROLLED_FULLSCREEN_MODE:
		gtk_window_resize(GTK_WINDOW(cnnhld->cnnwin), screen_width, screen_height);
		gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(cnnobj->scrolled_container),
			(scroll_required ? GTK_POLICY_AUTOMATIC : GTK_POLICY_NEVER),
			(scroll_required ? GTK_POLICY_AUTOMATIC : GTK_POLICY_NEVER));
		break;

	case VIEWPORT_FULLSCREEN_MODE:
		bordersz = scroll_required ? 1 : 0;
		gtk_window_resize(GTK_WINDOW(cnnhld->cnnwin), screen_width, screen_height);
		if (REMMINA_IS_SCROLLED_VIEWPORT(cnnobj->scrolled_container)) {
			/* Put a border around Notebook content (RemminaScrolledViewpord), so we can
			 * move the mouse over the border to scroll */
			gtk_container_set_border_width(GTK_CONTAINER(cnnobj->scrolled_container), bordersz);
		}
932

Antenore Gatta's avatar
Antenore Gatta committed
933 934 935 936 937 938 939 940 941
		break;

	case SCROLLED_WINDOW_MODE:
		if (remmina_file_get_int(cnnobj->remmina_file, "viewmode", AUTO_MODE) == AUTO_MODE) {
			gtk_window_set_default_size(GTK_WINDOW(cnnhld->cnnwin),
				MIN(server_width, screen_width), MIN(server_height, screen_height));
			if (server_width >= screen_width || server_height >= screen_height) {
				gtk_window_maximize(GTK_WINDOW(cnnhld->cnnwin));
				remmina_file_set_int(cnnobj->remmina_file, "window_maximize", TRUE);
942
			}else {
Antenore Gatta's avatar
Antenore Gatta committed
943 944
				remmina_connection_holder_toolbar_autofit(NULL, cnnhld);
				remmina_file_set_int(cnnobj->remmina_file, "window_maximize", FALSE);
Jean-Louis Dupond's avatar
Jean-Louis Dupond committed
945
			}
946
		}else {
Antenore Gatta's avatar
Antenore Gatta committed
947 948
			if (remmina_file_get_int(cnnobj->remmina_file, "window_maximize", FALSE)) {
				gtk_window_maximize(GTK_WINDOW(cnnhld->cnnwin));
Jean-Louis Dupond's avatar
Jean-Louis Dupond committed
949
			}
Antenore Gatta's avatar
Antenore Gatta committed
950 951
		}
		break;
Jean-Louis Dupond's avatar
Jean-Louis Dupond committed
952

Antenore Gatta's avatar
Antenore Gatta committed
953 954
	default:
		break;
Jean-Louis Dupond's avatar
Jean-Louis Dupond committed
955 956
	}
}
957

958
static void remmina_connection_holder_set_tooltip(GtkWidget* item, const gchar* tip, guint key1, guint key2)
959
{
960
	TRACE_CALL(__func__);
961 962
	gchar* s1;
	gchar* s2;
963

Antenore Gatta's avatar
Antenore Gatta committed
964 965
	if (remmina_pref.hostkey && key1) {
		if (key2) {
966
			s1 = g_strdup_printf(" (%s + %s,%s)", gdk_keyval_name(remmina_pref.hostkey),
Antenore Gatta's avatar
Antenore Gatta committed
967 968
				gdk_keyval_name(gdk_keyval_to_upper(key1)), gdk_keyval_name(gdk_keyval_to_upper(key2)));
		}else if (key1 == remmina_pref.hostkey) {
Antenore Gatta's avatar
Antenore Gatta committed
969
			s1 = g_strdup_printf(" (%s)", gdk_keyval_name(remmina_pref.hostkey));
970
		}else {
Antenore Gatta's avatar
Antenore Gatta committed
971
			s1 = g_strdup_printf(" (%s + %s)", gdk_keyval_name(remmina_pref.hostkey),
Antenore Gatta's avatar
Antenore Gatta committed
972
				gdk_keyval_name(gdk_keyval_to_upper(key1)));
973
		}
974
	}else {
975 976 977 978 979 980
		s1 = NULL;
	}
	s2 = g_strdup_printf("%s%s", tip, s1 ? s1 : "");
	gtk_widget_set_tooltip_text(item, s2);
	g_free(s2);
	g_free(s1);
981 982
}

983 984
static void remmina_protocol_widget_update_alignment(RemminaConnectionObject* cnnobj)
{
985
	TRACE_CALL(__func__);
986
	RemminaScaleMode scalemode;
987 988 989 990
	gboolean scaledexpandedmode;
	int rdwidth, rdheight;
	gfloat aratio;

Antenore Gatta's avatar
Antenore Gatta committed
991
	if (!cnnobj->plugin_can_scale) {
992 993
		/* If we have a plugin that cannot scale,
		 * (i.e. SFTP plugin), then we expand proto */
Antenore Gatta's avatar
Antenore Gatta committed
994 995
		gtk_widget_set_halign(GTK_WIDGET(cnnobj->proto), GTK_ALIGN_FILL);
		gtk_widget_set_valign(GTK_WIDGET(cnnobj->proto), GTK_ALIGN_FILL);
996
	}else {
997 998
		/* Plugin can scale */

999
		scalemode = get_current_allowed_scale_mode(cnnobj, NULL, NULL);
1000 1001 1002
		scaledexpandedmode = remmina_protocol_widget_get_expand(REMMINA_PROTOCOL_WIDGET(cnnobj->proto));

		/* Check if we need aspectframe and create/destroy it accordingly */
Antenore Gatta's avatar
Antenore Gatta committed
1003
		if (scalemode == REMMINA_PROTOCOL_WIDGET_SCALE_MODE_SCALED && !scaledexpandedmode) {
1004 1005 1006 1007
			/* We need an aspectframe as a parent of proto */
			rdwidth = remmina_protocol_widget_get_width(REMMINA_PROTOCOL_WIDGET(cnnobj->proto));
			rdheight = remmina_protocol_widget_get_height(REMMINA_PROTOCOL_WIDGET(cnnobj->proto));
			aratio = (gfloat)rdwidth / (gfloat)rdheight;
Antenore Gatta's avatar
Antenore Gatta committed
1008
			if (!cnnobj->aspectframe) {
1009 1010
				/* We need a new aspectframe */
				cnnobj->aspectframe = gtk_aspect_frame_new(NULL, 0.5, 0.5, aratio, FALSE);
1011
				gtk_widget_set_name(cnnobj->aspectframe, "remmina-cw-aspectframe");
1012 1013 1014 1015 1016 1017 1018
				gtk_frame_set_shadow_type(GTK_FRAME(cnnobj->aspectframe), GTK_SHADOW_NONE);
				g_object_ref(cnnobj->proto);
				gtk_container_remove(GTK_CONTAINER(cnnobj->viewport), cnnobj->proto);
				gtk_container_add(GTK_CONTAINER(cnnobj->viewport), cnnobj->aspectframe);
				gtk_container_add(GTK_CONTAINER(cnnobj->aspectframe), cnnobj->proto);
				g_object_unref(cnnobj->proto);
				gtk_widget_show(cnnobj->aspectframe);
1019 1020
				if (cnnobj->cnnhld != NULL && cnnobj->cnnhld->cnnwin != NULL && cnnobj->cnnhld->cnnwin->priv->notebook != NULL)
					remmina_connection_holder_grab_focus(GTK_NOTEBOOK(cnnobj->cnnhld->cnnwin->priv->notebook));
1021
			}else {
1022 1023
				gtk_aspect_frame_set(GTK_ASPECT_FRAME(cnnobj->aspectframe), 0.5, 0.5, aratio, FALSE);
			}
1024
		}else {
1025
			/* We do not need an aspectframe as a parent of proto */
Antenore Gatta's avatar
Antenore Gatta committed
1026
			if (cnnobj->aspectframe) {
1027 1028 1029 1030 1031 1032 1033 1034 1035
				/* We must remove the old aspectframe reparenting proto to viewport */
				g_object_ref(cnnobj->aspectframe);
				g_object_ref(cnnobj->proto);
				gtk_container_remove(GTK_CONTAINER(cnnobj->aspectframe), cnnobj->proto);
				gtk_container_remove(GTK_CONTAINER(cnnobj->viewport), cnnobj->aspectframe);
				g_object_unref(cnnobj->aspectframe);
				cnnobj->aspectframe = NULL;
				gtk_container_add(GTK_CONTAINER(cnnobj->viewport), cnnobj->proto);
				g_object_unref(cnnobj->proto);
1036 1037
				if (cnnobj->cnnhld != NULL && cnnobj->cnnhld->cnnwin != NULL && cnnobj->cnnhld->cnnwin->priv->notebook != NULL)
					remmina_connection_holder_grab_focus(GTK_NOTEBOOK(cnnobj->cnnhld->cnnwin->priv->notebook));
1038 1039 1040
			}
		}

Antenore Gatta's avatar
Antenore Gatta committed
1041
		if (scalemode == REMMINA_PROTOCOL_WIDGET_SCALE_MODE_SCALED || scalemode == REMMINA_PROTOCOL_WIDGET_SCALE_MODE_DYNRES) {
1042 1043 1044
			/* We have a plugin that can be scaled, and the scale button
			 * has been pressed. Give it the correct WxH maintaining aspect
			 * ratio of remote destkop size */
Antenore Gatta's avatar
Antenore Gatta committed
1045 1046
			gtk_widget_set_halign(GTK_WIDGET(cnnobj->proto), GTK_ALIGN_FILL);
			gtk_widget_set_valign(GTK_WIDGET(cnnobj->proto), GTK_ALIGN_FILL);
1047
		}else {
1048 1049
			/* Plugin can scale, but no scaling is active. Ensure that we have
			 * aspectframe with a ratio of 1 */
Antenore Gatta's avatar
Antenore Gatta committed
1050 1051
			gtk_widget_set_halign(GTK_WIDGET(cnnobj->proto), GTK_ALIGN_CENTER);
			gtk_widget_set_valign(GTK_WIDGET(cnnobj->proto), GTK_ALIGN_CENTER);
1052 1053 1054 1055 1056
		}
	}
}


1057
static void remmina_connection_holder_toolbar_fullscreen(GtkWidget* widget, RemminaConnectionHolder* cnnhld)
1058
{
1059
	TRACE_CALL(__func__);
1060 1061 1062 1063 1064 1065

	RemminaConnectionWindowPriv* priv = cnnhld->cnnwin->priv;

	if (priv->toolbar_is_reconfiguring)
		return;

Antenore Gatta's avatar
Antenore Gatta committed
1066
	if (gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(widget))) {
1067
		remmina_connection_holder_create_fullscreen(cnnhld, NULL, cnnhld->fullscreen_view_mode);
1068
	}else {
1069 1070 1071 1072
		remmina_connection_holder_create_scrolled(cnnhld, NULL);
	}
}

1073
static void remmina_connection_holder_viewport_fullscreen_mode(GtkWidget* widget, RemminaConnectionHolder* cnnhld)
1074
{
1075
	TRACE_CALL(__func__);
1076 1077 1078 1079 1080 1081
	if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
		return;
	cnnhld->fullscreen_view_mode = VIEWPORT_FULLSCREEN_MODE;
	remmina_connection_holder_create_fullscreen(cnnhld, NULL, cnnhld->fullscreen_view_mode);
}

1082
static void remmina_connection_holder_scrolled_fullscreen_mode(GtkWidget* widget, RemminaConnectionHolder* cnnhld)
1083
{
1084
	TRACE_CALL(__func__);
1085 1086 1087 1088 1089 1090
	if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
		return;
	cnnhld->fullscreen_view_mode = SCROLLED_FULLSCREEN_MODE;
	remmina_connection_holder_create_fullscreen(cnnhld, NULL, cnnhld->fullscreen_view_mode);
}

1091
static void remmina_connection_holder_fullscreen_option_popdown(GtkWidget* widget, RemminaConnectionHolder* cnnhld)
1092
{
1093
	TRACE_CALL(__func__);
1094
	RemminaConnectionWindowPriv* priv = cnnhld->cnnwin->priv;
1095 1096 1097 1098 1099 1100 1101

	priv->sticky = FALSE;

	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->fullscreen_option_button), FALSE);
	remmina_connection_holder_floating_toolbar_show(cnnhld, FALSE);
}

1102
static void remmina_connection_holder_toolbar_fullscreen_option(GtkWidget* widget, RemminaConnectionHolder* cnnhld)
1103
{
1104
	TRACE_CALL(__func__);
1105 1106 1107 1108
	RemminaConnectionWindowPriv* priv = cnnhld->cnnwin->priv;
	GtkWidget* menu;
	GtkWidget* menuitem;
	GSList* group;
1109

1110 1111 1112
	if (priv->toolbar_is_reconfiguring)
		return;

1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
	if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
		return;

	priv->sticky = TRUE;

	menu = gtk_menu_new();

	menuitem = gtk_radio_menu_item_new_with_label(NULL, _("Viewport fullscreen mode"));
	gtk_widget_show(menuitem);
	gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
	group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(menuitem));
Antenore Gatta's avatar
Antenore Gatta committed
1124
	if (priv->view_mode == VIEWPORT_FULLSCREEN_MODE) {
1125 1126 1127 1128 1129 1130 1131
		gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
	}
	g_signal_connect(G_OBJECT(menuitem), "toggled", G_CALLBACK(remmina_connection_holder_viewport_fullscreen_mode), cnnhld);

	menuitem = gtk_radio_menu_item_new_with_label(group, _("Scrolled fullscreen mode"));
	gtk_widget_show(menuitem);
	gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
Antenore Gatta's avatar
Antenore Gatta committed
1132
	if (priv->view_mode == SCROLLED_FULLSCREEN_MODE) {
1133 1134 1135 1136 1137 1138
		gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
	}
	g_signal_connect(G_OBJECT(menuitem), "toggled", G_CALLBACK(remmina_connection_holder_scrolled_fullscreen_mode), cnnhld);

	g_signal_connect(G_OBJECT(menu), "deactivate", G_CALLBACK(remmina_connection_holder_fullscreen_option_popdown), cnnhld);

1139 1140 1141 1142
#if GTK_CHECK_VERSION(3, 22, 0)
	gtk_menu_popup_at_widget(GTK_MENU(menu), widget,
		GDK_GRAVITY_SOUTH_WEST, GDK_GRAVITY_NORTH_WEST, NULL);
#else
1143
	gtk_menu_popup(GTK_MENU(menu), NULL, NULL, remmina_public_popup_position, priv->toolitem_fullscreen, 0,
Antenore Gatta's avatar
Antenore Gatta committed
1144
		gtk_get_current_event_time());
1145
#endif
1146 1147
}

1148 1149

static void remmina_connection_holder_scaler_option_popdown(GtkWidget* widget, RemminaConnectionHolder* cnnhld)
1150
{
1151
	TRACE_CALL(__func__);
1152
	RemminaConnectionWindowPriv* priv = cnnhld->cnnwin->priv;
1153 1154
	if (priv->toolbar_is_reconfiguring)
		return;
1155 1156 1157 1158
	priv->sticky = FALSE;
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->scaler_option_button), FALSE);
	remmina_connection_holder_floating_toolbar_show(cnnhld, FALSE);
}
1159

1160 1161
static void remmina_connection_holder_scaler_expand(GtkWidget* widget, RemminaConnectionHolder* cnnhld)
{
1162
	TRACE_CALL(__func__);
1163
	DECLARE_CNNOBJ
Antenore Gatta's avatar
Antenore Gatta committed
1164 1165
	if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
		return;
1166 1167 1168 1169 1170 1171
	remmina_protocol_widget_set_expand(REMMINA_PROTOCOL_WIDGET(cnnobj->proto), TRUE);
	remmina_file_set_int