| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | /* |
| 3 | * This file is part of the LibreOffice project. |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * This file incorporates work covered by the following license notice: |
| 10 | * |
| 11 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 | * contributor license agreements. See the NOTICE file distributed |
| 13 | * with this work for additional information regarding copyright |
| 14 | * ownership. The ASF licenses this file to you under the Apache |
| 15 | * License, Version 2.0 (the "License"); you may not use this file |
| 16 | * except in compliance with the License. You may obtain a copy of |
| 17 | * the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 | */ |
| 19 | |
| 20 | #include <unx/gtk/gtkbackend.hxx> |
| 21 | #include <unx/gtk/gtkobject.hxx> |
| 22 | #include <unx/gtk/gtkframe.hxx> |
| 23 | #include <unx/gtk/gtkdata.hxx> |
| 24 | #include <vcl/event.hxx> |
| 25 | |
| 26 | GtkSalObjectBase::GtkSalObjectBase(GtkSalFrame* pParent) |
| 27 | : m_pSocket(nullptr) |
| 28 | , m_pParent(pParent) |
| 29 | , m_pRegion(nullptr) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | GtkSalObject::GtkSalObject(GtkSalFrame* pParent, bool bShow) |
| 34 | : GtkSalObjectBase(pParent) |
| 35 | { |
| 36 | if (!m_pParent) |
| 37 | return; |
| 38 | |
| 39 | // our plug window |
| 40 | m_pSocket = gtk_grid_new(); |
| 41 | Show( bShow ); |
| 42 | // insert into container |
| 43 | gtk_fixed_put( pParent->getFixedContainer(), |
| 44 | m_pSocket, |
| 45 | 0, 0 ); |
| 46 | |
| 47 | Init(); |
| 48 | |
| 49 | g_signal_connect( G_OBJECT(m_pSocket), "destroy", G_CALLBACK(signalDestroy), this ); |
| 50 | |
| 51 | // #i59255# necessary due to sync effects with java child windows |
| 52 | pParent->Flush(); |
| 53 | } |
| 54 | |
| 55 | void GtkSalObjectBase::Init() |
| 56 | { |
| 57 | // realize so we can get a window id |
| 58 | gtk_widget_realize( m_pSocket ); |
| 59 | |
| 60 | // system data |
| 61 | // tdf#139609 deliberately defer using m_pParent->GetNativeWindowHandle(m_pSocket)) to set m_aSystemData.aWindow |
| 62 | // unless its explicitly needed |
| 63 | m_aSystemData.pSalFrame = nullptr; |
| 64 | m_aSystemData.pWidget = m_pSocket; |
| 65 | m_aSystemData.nScreen = m_pParent->getXScreenNumber().getXScreen(); |
| 66 | m_aSystemData.toolkit = SystemEnvData::Toolkit::Gtk; |
| 67 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 68 | GdkScreen* pScreen = gtk_widget_get_screen(m_pParent->getWindow()); |
| 69 | GdkVisual* pVisual = gdk_screen_get_system_visual(pScreen); |
| 70 | |
| 71 | #if defined(GDK_WINDOWING_X11) |
| 72 | GdkDisplay *pDisplay = GtkSalFrame::getGdkDisplay(); |
| 73 | if (DLSYM_GDK_IS_X11_DISPLAY(pDisplay)) |
| 74 | { |
| 75 | m_aSystemData.pDisplay = gdk_x11_display_get_xdisplay(pDisplay); |
| 76 | m_aSystemData.pVisual = gdk_x11_visual_get_xvisual(pVisual); |
| 77 | m_aSystemData.platform = SystemEnvData::Platform::Xcb; |
| 78 | } |
| 79 | #endif |
| 80 | #if defined(GDK_WINDOWING_WAYLAND) |
| 81 | if (DLSYM_GDK_IS_WAYLAND_DISPLAY(pDisplay)) |
| 82 | { |
| 83 | m_aSystemData.pDisplay = gdk_wayland_display_get_wl_display(pDisplay); |
| 84 | m_aSystemData.platform = SystemEnvData::Platform::Wayland; |
| 85 | } |
| 86 | #endif |
| 87 | |
| 88 | g_signal_connect( G_OBJECT(m_pSocket), "button-press-event", G_CALLBACK(signalButton), this ); |
| 89 | g_signal_connect( G_OBJECT(m_pSocket), "button-release-event", G_CALLBACK(signalButton), this ); |
| 90 | g_signal_connect( G_OBJECT(m_pSocket), "focus-in-event", G_CALLBACK(signalFocus), this ); |
| 91 | g_signal_connect( G_OBJECT(m_pSocket), "focus-out-event", G_CALLBACK(signalFocus), this ); |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | GtkSalObjectBase::~GtkSalObjectBase() |
| 96 | { |
| 97 | if( m_pRegion ) |
| 98 | { |
| 99 | cairo_region_destroy( m_pRegion ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | GtkSalObject::~GtkSalObject() |
| 104 | { |
| 105 | if( !m_pSocket ) |
| 106 | return; |
| 107 | |
| 108 | #if GTK_CHECK_VERSION(4, 0, 0) |
| 109 | gtk_widget_unparent(m_pSocket); |
| 110 | #else |
| 111 | // remove socket from parent frame's fixed container |
| 112 | gtk_container_remove( GTK_CONTAINER(gtk_widget_get_parent(m_pSocket)), |
| 113 | m_pSocket ); |
| 114 | // get rid of the socket |
| 115 | // actually the gtk_container_remove should let the ref count |
| 116 | // of the socket sink to 0 and destroy it (see signalDestroy) |
| 117 | // this is just a sanity check |
| 118 | if( m_pSocket ) |
| 119 | gtk_widget_destroy( m_pSocket ); |
| 120 | #endif |
| 121 | } |
| 122 | |
| 123 | void GtkSalObject::ResetClipRegion() |
| 124 | { |
| 125 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 126 | if( m_pSocket ) |
| 127 | gdk_window_shape_combine_region( widget_get_surface(m_pSocket), nullptr, 0, 0 ); |
| 128 | #endif |
| 129 | } |
| 130 | |
| 131 | void GtkSalObjectBase::BeginSetClipRegion( sal_uInt32 ) |
| 132 | { |
| 133 | if (m_pRegion) |
| 134 | cairo_region_destroy(m_pRegion); |
| 135 | m_pRegion = cairo_region_create(); |
| 136 | } |
| 137 | |
| 138 | void GtkSalObjectBase::UnionClipRegion( tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight ) |
| 139 | { |
| 140 | GdkRectangle aRect; |
| 141 | aRect.x = nX; |
| 142 | aRect.y = nY; |
| 143 | aRect.width = nWidth; |
| 144 | aRect.height = nHeight; |
| 145 | |
| 146 | cairo_region_union_rectangle( m_pRegion, &aRect ); |
| 147 | } |
| 148 | |
| 149 | void GtkSalObject::EndSetClipRegion() |
| 150 | { |
| 151 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 152 | if( m_pSocket ) |
| 153 | gdk_window_shape_combine_region( widget_get_surface(m_pSocket), m_pRegion, 0, 0 ); |
| 154 | #endif |
| 155 | } |
| 156 | |
| 157 | void GtkSalObject::SetPosSize(tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight) |
| 158 | { |
| 159 | if (m_pSocket) |
| 160 | { |
| 161 | GtkFixed* pContainer = GTK_FIXED(gtk_widget_get_parent(m_pSocket)); |
| 162 | gtk_fixed_move( pContainer, m_pSocket, nX, nY ); |
| 163 | gtk_widget_set_size_request( m_pSocket, nWidth, nHeight ); |
| 164 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 165 | m_pParent->nopaint_container_resize_children(GTK_CONTAINER(pContainer)); |
| 166 | #endif |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void GtkSalObject::Reparent(SalFrame* pFrame) |
| 171 | { |
| 172 | GtkSalFrame* pNewParent = static_cast<GtkSalFrame*>(pFrame); |
| 173 | if (m_pSocket) |
| 174 | { |
| 175 | GtkFixed* pContainer = GTK_FIXED(gtk_widget_get_parent(m_pSocket)); |
| 176 | |
| 177 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 178 | gint nX(0), nY(0); |
| 179 | gtk_container_child_get(GTK_CONTAINER(pContainer), m_pSocket, |
| 180 | "x", &nX, |
| 181 | "y", &nY, |
| 182 | nullptr); |
| 183 | #else |
| 184 | double nX(0), nY(0); |
| 185 | gtk_fixed_get_child_position(pContainer, m_pSocket, &nX, &nY); |
| 186 | #endif |
| 187 | |
| 188 | g_object_ref(m_pSocket); |
| 189 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 190 | gtk_container_remove(GTK_CONTAINER(pContainer), m_pSocket); |
| 191 | #else |
| 192 | gtk_fixed_remove(pContainer, m_pSocket); |
| 193 | #endif |
| 194 | |
| 195 | gtk_fixed_put(pNewParent->getFixedContainer(), |
| 196 | m_pSocket, |
| 197 | nX, nY); |
| 198 | |
| 199 | g_object_unref(m_pSocket); |
| 200 | } |
| 201 | m_pParent = pNewParent; |
| 202 | } |
| 203 | |
| 204 | void GtkSalObject::Show( bool bVisible ) |
| 205 | { |
| 206 | if( m_pSocket ) |
| 207 | gtk_widget_set_visible(m_pSocket, bVisible); |
| 208 | } |
| 209 | |
| 210 | Size GtkSalObjectBase::GetOptimalSize() const |
| 211 | { |
| 212 | if (m_pSocket) |
| 213 | { |
| 214 | bool bVisible = gtk_widget_get_visible(m_pSocket); |
| 215 | if (!bVisible) |
| 216 | gtk_widget_set_visible(m_pSocket, true); |
| 217 | |
| 218 | // Undo SetPosSize before getting its preferred size |
| 219 | gint width(-1), height(-1); |
| 220 | gtk_widget_get_size_request(m_pSocket, &width, &height); |
| 221 | gtk_widget_set_size_request(m_pSocket, -1, -1); |
| 222 | |
| 223 | GtkRequisition size; |
| 224 | gtk_widget_get_preferred_size(m_pSocket, nullptr, &size); |
| 225 | |
| 226 | // Restore SetPosSize size |
| 227 | gtk_widget_set_size_request(m_pSocket, width, height); |
| 228 | |
| 229 | if (!bVisible) |
| 230 | gtk_widget_set_visible(m_pSocket, false); |
| 231 | return Size(size.width, size.height); |
| 232 | } |
| 233 | return Size(); |
| 234 | } |
| 235 | |
| 236 | const SystemEnvData& GtkSalObjectBase::GetSystemData() const |
| 237 | { |
| 238 | return m_aSystemData; |
| 239 | } |
| 240 | |
| 241 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 242 | gboolean GtkSalObjectBase::signalButton( GtkWidget*, GdkEventButton* pEvent, gpointer object ) |
| 243 | { |
| 244 | GtkSalObjectBase* pThis = static_cast<GtkSalObject*>(object); |
| 245 | |
| 246 | if( pEvent->type == GDK_BUTTON_PRESS ) |
| 247 | { |
| 248 | pThis->CallCallback( SalObjEvent::ToTop ); |
| 249 | } |
| 250 | |
| 251 | return FALSE; |
| 252 | } |
| 253 | |
| 254 | gboolean GtkSalObjectBase::signalFocus( GtkWidget*, GdkEventFocus* pEvent, gpointer object ) |
| 255 | { |
| 256 | GtkSalObjectBase* pThis = static_cast<GtkSalObject*>(object); |
| 257 | |
| 258 | pThis->CallCallback( pEvent->in ? SalObjEvent::GetFocus : SalObjEvent::LoseFocus ); |
| 259 | |
| 260 | return FALSE; |
| 261 | } |
| 262 | #endif |
| 263 | |
| 264 | void GtkSalObject::signalDestroy( GtkWidget* pObj, gpointer object ) |
| 265 | { |
| 266 | GtkSalObject* pThis = static_cast<GtkSalObject*>(object); |
| 267 | if( pObj == pThis->m_pSocket ) |
| 268 | { |
| 269 | pThis->m_pSocket = nullptr; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | void GtkSalObjectBase::SetForwardKey( bool bEnable ) |
| 274 | { |
| 275 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 276 | if( bEnable ) |
| 277 | gtk_widget_add_events( GTK_WIDGET( m_pSocket ), GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK ); |
| 278 | else |
| 279 | gtk_widget_set_events( GTK_WIDGET( m_pSocket ), ~(GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK) & gtk_widget_get_events( GTK_WIDGET( m_pSocket ) ) ); |
| 280 | #else |
| 281 | (void)bEnable; |
| 282 | #endif |
| 283 | } |
| 284 | |
| 285 | GtkSalObjectWidgetClip::GtkSalObjectWidgetClip(GtkSalFrame* pParent, bool bShow) |
| 286 | : GtkSalObjectBase(pParent) |
| 287 | , m_pScrolledWindow(nullptr) |
| 288 | , m_pViewPort(nullptr) |
| 289 | , m_pBgCssProvider(nullptr) |
| 290 | { |
| 291 | if( !pParent ) |
| 292 | return; |
| 293 | |
| 294 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 295 | m_pScrolledWindow = gtk_scrolled_window_new(nullptr, nullptr); |
| 296 | g_signal_connect(m_pScrolledWindow, "scroll-event", G_CALLBACK(signalScroll), this); |
| 297 | #else |
| 298 | m_pScrolledWindow = gtk_scrolled_window_new(); |
| 299 | GtkEventController* pScrollController = gtk_event_controller_scroll_new(GTK_EVENT_CONTROLLER_SCROLL_BOTH_AXES); |
| 300 | g_signal_connect(pScrollController, "scroll", G_CALLBACK(signalScroll), this); |
| 301 | gtk_widget_add_controller(m_pScrolledWindow, pScrollController); |
| 302 | #endif |
| 303 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(m_pScrolledWindow), |
| 304 | GTK_POLICY_EXTERNAL, GTK_POLICY_EXTERNAL); |
| 305 | |
| 306 | // insert into container |
| 307 | gtk_fixed_put( pParent->getFixedContainer(), |
| 308 | m_pScrolledWindow, |
| 309 | 0, 0 ); |
| 310 | |
| 311 | // deliberately without adjustments to avoid gtk's auto adjustment on changing focus |
| 312 | m_pViewPort = gtk_viewport_new(nullptr, nullptr); |
| 313 | |
| 314 | // force in a fake background of a suitable color |
| 315 | SetViewPortBackground(); |
| 316 | |
| 317 | ImplGetDefaultWindow()->AddEventListener(LINK(this, GtkSalObjectWidgetClip, SettingsChangedHdl)); |
| 318 | |
| 319 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 320 | gtk_container_add(GTK_CONTAINER(m_pScrolledWindow), m_pViewPort); |
| 321 | #else |
| 322 | gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(m_pScrolledWindow), m_pViewPort); |
| 323 | #endif |
| 324 | gtk_widget_set_visible(m_pViewPort, true); |
| 325 | |
| 326 | // our plug window |
| 327 | m_pSocket = gtk_grid_new(); |
| 328 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 329 | gtk_container_add(GTK_CONTAINER(m_pViewPort), m_pSocket); |
| 330 | #else |
| 331 | gtk_viewport_set_child(GTK_VIEWPORT(m_pViewPort), m_pSocket); |
| 332 | #endif |
| 333 | gtk_widget_set_visible(m_pSocket, true); |
| 334 | |
| 335 | Show(bShow); |
| 336 | |
| 337 | Init(); |
| 338 | |
| 339 | g_signal_connect( G_OBJECT(m_pSocket), "destroy", G_CALLBACK(signalDestroy), this ); |
| 340 | } |
| 341 | |
| 342 | // force in a fake background of a suitable color |
| 343 | void GtkSalObjectWidgetClip::SetViewPortBackground() |
| 344 | { |
| 345 | GtkStyleContext *pWidgetContext = gtk_widget_get_style_context(m_pViewPort); |
| 346 | if (m_pBgCssProvider) |
| 347 | gtk_style_context_remove_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pBgCssProvider)); |
| 348 | m_pBgCssProvider = gtk_css_provider_new(); |
| 349 | OUString sColor = Application::GetSettings().GetStyleSettings().GetDialogColor().AsRGBHexString(); |
| 350 | OUString aBuffer = "* { background-color: #" + sColor + "; }"; |
| 351 | OString aResult = OUStringToOString(aBuffer, RTL_TEXTENCODING_UTF8); |
| 352 | css_provider_load_from_data(m_pBgCssProvider, aResult.getStr(), aResult.getLength()); |
| 353 | gtk_style_context_add_provider(pWidgetContext, GTK_STYLE_PROVIDER(m_pBgCssProvider), |
| 354 | GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); |
| 355 | } |
| 356 | |
| 357 | IMPL_LINK(GtkSalObjectWidgetClip, SettingsChangedHdl, VclWindowEvent&, rEvent, void) |
| 358 | { |
| 359 | if (rEvent.GetId() != VclEventId::WindowDataChanged) |
| 360 | return; |
| 361 | |
| 362 | DataChangedEvent* pData = static_cast<DataChangedEvent*>(rEvent.GetData()); |
| 363 | if (pData->GetType() == DataChangedEventType::SETTINGS) |
| 364 | SetViewPortBackground(); |
| 365 | } |
| 366 | |
| 367 | GtkSalObjectWidgetClip::~GtkSalObjectWidgetClip() |
| 368 | { |
| 369 | ImplGetDefaultWindow()->RemoveEventListener(LINK(this, GtkSalObjectWidgetClip, SettingsChangedHdl)); |
| 370 | |
| 371 | if( !m_pSocket ) |
| 372 | return; |
| 373 | |
| 374 | // remove socket from parent frame's fixed container |
| 375 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 376 | gtk_container_remove( GTK_CONTAINER(gtk_widget_get_parent(m_pScrolledWindow)), |
| 377 | m_pScrolledWindow ); |
| 378 | |
| 379 | // get rid of the socket |
| 380 | // actually the gtk_container_remove should let the ref count |
| 381 | // of the socket sink to 0 and destroy it (see signalDestroy) |
| 382 | // this is just a sanity check |
| 383 | if( m_pScrolledWindow ) |
| 384 | gtk_widget_destroy( m_pScrolledWindow ); |
| 385 | #else |
| 386 | gtk_fixed_remove(GTK_FIXED(gtk_widget_get_parent(m_pScrolledWindow)), |
| 387 | m_pScrolledWindow); |
| 388 | #endif |
| 389 | } |
| 390 | |
| 391 | void GtkSalObjectWidgetClip::ResetClipRegion() |
| 392 | { |
| 393 | m_aClipRect = tools::Rectangle(); |
| 394 | ApplyClipRegion(); |
| 395 | } |
| 396 | |
| 397 | void GtkSalObjectWidgetClip::EndSetClipRegion() |
| 398 | { |
| 399 | int nRects = cairo_region_num_rectangles(m_pRegion); |
| 400 | assert(nRects == 0 || nRects == 1); |
| 401 | if (nRects == 0) |
| 402 | m_aClipRect = tools::Rectangle(); |
| 403 | else |
| 404 | { |
| 405 | cairo_rectangle_int_t rectangle; |
| 406 | cairo_region_get_rectangle(m_pRegion, 0, &rectangle); |
| 407 | m_aClipRect = tools::Rectangle(Point(rectangle.x, rectangle.y), Size(rectangle.width, rectangle.height)); |
| 408 | } |
| 409 | ApplyClipRegion(); |
| 410 | } |
| 411 | |
| 412 | void GtkSalObjectWidgetClip::ApplyClipRegion() |
| 413 | { |
| 414 | if( !m_pSocket ) |
| 415 | return; |
| 416 | |
| 417 | GtkFixed* pContainer = GTK_FIXED(gtk_widget_get_parent(m_pScrolledWindow)); |
| 418 | |
| 419 | GtkAllocation allocation; |
| 420 | allocation.x = m_aRect.Left() + m_aClipRect.Left(); |
| 421 | allocation.y = m_aRect.Top() + m_aClipRect.Top(); |
| 422 | if (m_aClipRect.IsEmpty()) |
| 423 | { |
| 424 | allocation.width = m_aRect.GetWidth(); |
| 425 | allocation.height = m_aRect.GetHeight(); |
| 426 | } |
| 427 | else |
| 428 | { |
| 429 | allocation.width = m_aClipRect.GetWidth(); |
| 430 | allocation.height = m_aClipRect.GetHeight(); |
| 431 | } |
| 432 | |
| 433 | if (AllSettings::GetLayoutRTL()) |
| 434 | { |
| 435 | GtkAllocation aParentAllocation; |
| 436 | gtk_widget_get_allocation(GTK_WIDGET(pContainer), &aParentAllocation); |
| 437 | gtk_fixed_move(pContainer, m_pScrolledWindow, aParentAllocation.width - allocation.width - 1 - allocation.x, allocation.y); |
| 438 | } |
| 439 | else |
| 440 | gtk_fixed_move(pContainer, m_pScrolledWindow, allocation.x, allocation.y); |
| 441 | gtk_widget_set_size_request(m_pScrolledWindow, allocation.width, allocation.height); |
| 442 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 443 | gtk_widget_size_allocate(m_pScrolledWindow, &allocation); |
| 444 | #else |
| 445 | gtk_widget_size_allocate(m_pScrolledWindow, &allocation, 0); |
| 446 | #endif |
| 447 | |
| 448 | gtk_adjustment_set_value(gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(m_pScrolledWindow)), m_aClipRect.Left()); |
| 449 | gtk_adjustment_set_value(gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(m_pScrolledWindow)), m_aClipRect.Top()); |
| 450 | } |
| 451 | |
| 452 | void GtkSalObjectWidgetClip::SetPosSize(tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight) |
| 453 | { |
| 454 | m_aRect = tools::Rectangle(Point(nX, nY), Size(nWidth, nHeight)); |
| 455 | if (m_pSocket) |
| 456 | { |
| 457 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 458 | GtkFixed* pContainer = GTK_FIXED(gtk_widget_get_parent(m_pScrolledWindow)); |
| 459 | #endif |
| 460 | gtk_widget_set_size_request(m_pSocket, nWidth, nHeight); |
| 461 | ApplyClipRegion(); |
| 462 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 463 | m_pParent->nopaint_container_resize_children(GTK_CONTAINER(pContainer)); |
| 464 | #endif |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | void GtkSalObjectWidgetClip::Reparent(SalFrame* pFrame) |
| 469 | { |
| 470 | GtkSalFrame* pNewParent = static_cast<GtkSalFrame*>(pFrame); |
| 471 | if (m_pSocket) |
| 472 | { |
| 473 | GtkFixed* pContainer = GTK_FIXED(gtk_widget_get_parent(m_pScrolledWindow)); |
| 474 | |
| 475 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 476 | gint nX(0), nY(0); |
| 477 | gtk_container_child_get(GTK_CONTAINER(pContainer), m_pScrolledWindow, |
| 478 | "x", &nX, |
| 479 | "y", &nY, |
| 480 | nullptr); |
| 481 | #else |
| 482 | double nX(0), nY(0); |
| 483 | gtk_fixed_get_child_position(pContainer, m_pScrolledWindow, &nX, &nY); |
| 484 | #endif |
| 485 | |
| 486 | g_object_ref(m_pScrolledWindow); |
| 487 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 488 | gtk_container_remove(GTK_CONTAINER(pContainer), m_pScrolledWindow); |
| 489 | #else |
| 490 | gtk_fixed_remove(pContainer, m_pScrolledWindow); |
| 491 | #endif |
| 492 | |
| 493 | gtk_fixed_put(pNewParent->getFixedContainer(), |
| 494 | m_pScrolledWindow, |
| 495 | nX, nY); |
| 496 | |
| 497 | g_object_unref(m_pScrolledWindow); |
| 498 | } |
| 499 | m_pParent = pNewParent; |
| 500 | } |
| 501 | |
| 502 | void GtkSalObjectWidgetClip::Show( bool bVisible ) |
| 503 | { |
| 504 | if (!m_pSocket) |
| 505 | return; |
| 506 | bool bCurrentVis = gtk_widget_get_visible(m_pScrolledWindow); |
| 507 | if (bVisible == bCurrentVis) |
| 508 | return; |
| 509 | if( bVisible ) |
| 510 | { |
| 511 | gtk_widget_set_visible(m_pScrolledWindow, true); |
| 512 | // tdf#146641 allocations attempted while hidden are discarded by gtk, |
| 513 | // so on transition to visible ApplyClipRegion needs to be called |
| 514 | ApplyClipRegion(); |
| 515 | } |
| 516 | else |
| 517 | { |
| 518 | // on hiding the widget, if a child has focus gtk will want to move the focus out of the widget |
| 519 | // but we want to keep the focus where it is, e.g. writer's comments in margin feature put |
| 520 | // cursor in a sidebar comment and scroll the page so the comment is invisible, we want the focus |
| 521 | // to stay in the invisible widget, so its there when we scroll back or on a keypress the widget |
| 522 | // gets the keystroke and scrolls back to make it visible again |
| 523 | GtkWidget* pTopLevel = widget_get_toplevel(m_pScrolledWindow); |
| 524 | GtkWidget* pOldFocus = GTK_IS_WINDOW(pTopLevel) ? gtk_window_get_focus(GTK_WINDOW(pTopLevel)) : nullptr; |
| 525 | |
| 526 | g_object_set_data(G_OBJECT(pTopLevel), "g-lo-BlockFocusChange", GINT_TO_POINTER(true) ); |
| 527 | |
| 528 | gtk_widget_set_visible(m_pScrolledWindow, false); |
| 529 | |
| 530 | GtkWidget* pNewFocus = GTK_IS_WINDOW(pTopLevel) ? gtk_window_get_focus(GTK_WINDOW(pTopLevel)) : nullptr; |
| 531 | if (pOldFocus && pOldFocus != pNewFocus) |
| 532 | gtk_widget_grab_focus(pOldFocus); |
| 533 | |
| 534 | g_object_set_data(G_OBJECT(pTopLevel), "g-lo-BlockFocusChange", GINT_TO_POINTER(false) ); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | void GtkSalObjectWidgetClip::signalDestroy( GtkWidget* pObj, gpointer object ) |
| 539 | { |
| 540 | GtkSalObjectWidgetClip* pThis = static_cast<GtkSalObjectWidgetClip*>(object); |
| 541 | if( pObj == pThis->m_pSocket ) |
| 542 | { |
| 543 | pThis->m_pSocket = nullptr; |
| 544 | pThis->m_pScrolledWindow = nullptr; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 549 | gboolean GtkSalObjectWidgetClip::signalScroll(GtkWidget* pScrolledWindow, GdkEvent* pEvent, gpointer object) |
| 550 | { |
| 551 | GtkSalObjectWidgetClip* pThis = static_cast<GtkSalObjectWidgetClip*>(object); |
| 552 | return pThis->signal_scroll(pScrolledWindow, pEvent); |
| 553 | } |
| 554 | #else |
| 555 | gboolean GtkSalObjectWidgetClip::signalScroll(GtkEventControllerScroll* pController, double delta_x, double delta_y, gpointer object) |
| 556 | { |
| 557 | GtkSalObjectWidgetClip* pThis = static_cast<GtkSalObjectWidgetClip*>(object); |
| 558 | return pThis->signal_scroll(pController, delta_x, delta_y); |
| 559 | } |
| 560 | #endif |
| 561 | |
| 562 | // forward the wheel scroll events onto the main window instead |
| 563 | #if !GTK_CHECK_VERSION(4, 0, 0) |
| 564 | bool GtkSalObjectWidgetClip::signal_scroll(GtkWidget*, GdkEvent* pEvent) |
| 565 | { |
| 566 | GtkWidget* pEventWidget = gtk_get_event_widget(pEvent); |
| 567 | |
| 568 | GtkWidget* pMouseEventWidget = m_pParent->getMouseEventWidget(); |
| 569 | |
| 570 | gtk_coord dest_x, dest_y; |
| 571 | gtk_widget_translate_coordinates(pEventWidget, |
| 572 | pMouseEventWidget, |
| 573 | pEvent->scroll.x, |
| 574 | pEvent->scroll.y, |
| 575 | &dest_x, |
| 576 | &dest_y); |
| 577 | pEvent->scroll.x = dest_x; |
| 578 | pEvent->scroll.y = dest_y; |
| 579 | |
| 580 | GtkSalFrame::signalScroll(pMouseEventWidget, pEvent, m_pParent); |
| 581 | return true; |
| 582 | } |
| 583 | #else |
| 584 | bool GtkSalObjectWidgetClip::signal_scroll(GtkEventControllerScroll* pController, double delta_x, double delta_y) |
| 585 | { |
| 586 | GtkWidget* pEventWidget = m_pScrolledWindow; |
| 587 | |
| 588 | GtkWidget* pMouseEventWidget = m_pParent->getMouseEventWidget(); |
| 589 | |
| 590 | gtk_coord dest_x, dest_y; |
| 591 | gtk_widget_translate_coordinates(pEventWidget, |
| 592 | pMouseEventWidget, |
| 593 | delta_x, |
| 594 | delta_y, |
| 595 | &dest_x, |
| 596 | &dest_y); |
| 597 | delta_x = dest_x; |
| 598 | delta_y = dest_y; |
| 599 | |
| 600 | GtkSalFrame::signalScroll(pController, delta_x, delta_y, m_pParent); |
| 601 | return true; |
| 602 | } |
| 603 | #endif |
| 604 | |
| 605 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |