KiCad PCB EDA Suite
gtk/ui.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2020 Ian McInerney <Ian.S.McInerney at ieee.org>
5 * Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <kiplatform/ui.h>
22
23#include <wx/choice.h>
24#include <wx/nonownedwnd.h>
25#include <wx/settings.h>
26#include <wx/window.h>
27
28#include <gtk/gtk.h>
29#include <gdk/gdk.h>
30
32{
33 wxColour bg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
34
35 // Weighted W3C formula
36 double brightness = ( bg.Red() / 255.0 ) * 0.299 +
37 ( bg.Green() / 255.0 ) * 0.587 +
38 ( bg.Blue() / 255.0 ) * 0.117;
39
40 return brightness < 0.5;
41}
42
43
44void KIPLATFORM::UI::ForceFocus( wxWindow* aWindow )
45{
46 aWindow->SetFocus();
47}
48
49
50bool KIPLATFORM::UI::IsWindowActive( wxWindow* aWindow )
51{
52 if( !aWindow )
53 return false;
54
55 GtkWindow* window = GTK_WINDOW( aWindow->GetHandle() );
56
57 if( window )
58 return gtk_window_is_active( window );
59
60 // We shouldn't really ever reach this point
61 return false;
62}
63
64
65void KIPLATFORM::UI::ReparentQuasiModal( wxNonOwnedWindow* aWindow )
66{
67 // Not needed on this platform
68}
69
70
72{
73 // Not needed on this platform
74}
75
76
77bool KIPLATFORM::UI::IsStockCursorOk( wxStockCursor aCursor )
78{
79 switch( aCursor )
80 {
81 case wxCURSOR_BULLSEYE:
82 case wxCURSOR_HAND:
83 case wxCURSOR_ARROW:
84 case wxCURSOR_BLANK:
85 return true;
86 default:
87 return false;
88 }
89}
90
91
92void KIPLATFORM::UI::EllipsizeChoiceBox( wxChoice* aChoice )
93{
94 // This function is based on the code inside the function post_process_ui in
95 // gtkfilechooserwidget.c
96 GList* cells = gtk_cell_layout_get_cells( GTK_CELL_LAYOUT( aChoice->m_widget ) );
97
98 if( !cells )
99 return;
100
101 GtkCellRenderer* cell = (GtkCellRenderer*) cells->data;
102
103 if( !cell )
104 return;
105
106 g_object_set( G_OBJECT( cell ), "ellipsize", PANGO_ELLIPSIZE_END, nullptr );
107
108 // Only the list of cells must be freed, the renderer isn't ours to free
109 g_list_free( cells );
110}
111
112
113double KIPLATFORM::UI::GetSystemScaleFactor( const wxWindow* aWindow )
114{
115 double val = 1.0;
116
117 GtkWidget* widget = static_cast<GtkWidget*>( aWindow->GetHandle() );
118
119 if( widget && gtk_check_version( 3, 10, 0 ) == nullptr )
120 val = gtk_widget_get_scale_factor( widget );
121
122 return val;
123}
124
125
126wxSize KIPLATFORM::UI::GetUnobscuredSize( const wxWindow* aWindow )
127{
128 return wxSize( aWindow->GetSize().GetX() - wxSystemSettings::GetMetric( wxSYS_VSCROLL_X ),
129 aWindow->GetSize().GetY() - wxSystemSettings::GetMetric( wxSYS_HSCROLL_Y ) );
130}
131
132
133void KIPLATFORM::UI::SetOverlayScrolling( const wxWindow* aWindow, bool overlay )
134{
135 gtk_scrolled_window_set_overlay_scrolling( GTK_SCROLLED_WINDOW( aWindow->GetHandle() ),
136 overlay );
137}
138
139
141{
142 gboolean allowed = 1;
143
144 g_object_get( gtk_settings_get_default(), "gtk-menu-images", &allowed, NULL );
145
146 return !!allowed;
147}
148
149
150void KIPLATFORM::UI::WarpPointer( wxWindow* aWindow, int aX, int aY )
151{
152 if( !wxGetEnv( wxT( "WAYLAND_DISPLAY" ), nullptr ) )
153 {
154 aWindow->WarpPointer( aX, aY );
155 }
156 else
157 {
158 GdkDisplay* disp = gtk_widget_get_display( static_cast<GtkWidget*>( aWindow->GetHandle() ) );
159 GdkSeat* seat = gdk_display_get_default_seat( disp );
160 GdkDevice* dev = gdk_seat_get_pointer( seat );
161 GdkWindow* win = gdk_device_get_window_at_position( dev, nullptr, nullptr );
162 GdkCursor* blank_cursor = gdk_cursor_new_for_display( disp, GDK_BLANK_CURSOR );
163 GdkCursor* cur_cursor = gdk_window_get_cursor( win );
164
165 if( cur_cursor )
166 g_object_ref( cur_cursor );
167
168 gdk_window_set_cursor( win, blank_cursor );
169 aWindow->WarpPointer( aX, aY );
170 gdk_window_set_cursor( win, cur_cursor );
171 }
172}
bool AllowIconsInMenus()
If the user has disabled icons system-wide, we check that here.
Definition: gtk/ui.cpp:140
void EllipsizeChoiceBox(wxChoice *aChoice)
Configure a wxChoice control to ellipsize the shown text in the button with the ellipses placed at th...
Definition: gtk/ui.cpp:92
void FixupCancelButtonCmdKeyCollision(wxWindow *aWindow)
Definition: gtk/ui.cpp:71
void SetOverlayScrolling(const wxWindow *aWindow, bool overlay)
Used to set overlay/non-overlay scrolling mode in a window.
Definition: gtk/ui.cpp:133
double GetSystemScaleFactor(const wxWindow *aWindow)
Tries to determine the system scaling factor currently in use for the window.
Definition: gtk/ui.cpp:113
bool IsStockCursorOk(wxStockCursor aCursor)
Checks if we designated a stock cursor for this OS as "OK" or else we may need to load a custom one.
Definition: gtk/ui.cpp:77
void WarpPointer(wxWindow *aWindow, int aX, int aY)
Move the mouse cursor to a specific position relative to the window.
Definition: gtk/ui.cpp:150
void ReparentQuasiModal(wxNonOwnedWindow *aWindow)
Move a window's parent to be the top-level window and force the window to be on top.
Definition: gtk/ui.cpp:65
bool IsWindowActive(wxWindow *aWindow)
Check to see if the given window is the currently active window (e.g.
Definition: gtk/ui.cpp:50
wxSize GetUnobscuredSize(const wxWindow *aWindow)
Tries to determine the size of the viewport of a scrollable widget (wxDataViewCtrl,...
Definition: gtk/ui.cpp:126
void ForceFocus(wxWindow *aWindow)
Pass the current focus to the window.
Definition: gtk/ui.cpp:44
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition: gtk/ui.cpp:31
std::shared_ptr< PNS_LOG_VIEWER_OVERLAY > overlay
Definition: playground.cpp:36