KWindowSystem

platforms/windows/kwindowinfo.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2008 Carlo Segato <[email protected]>
4  SPDX-FileCopyrightText: 2011 Pau Garcia i Quiles <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.1-or-later
7 */
8 
9 #include "kwindowinfo.h"
10 #include "kwindowsystem.h"
11 #include <QCoreApplication>
12 #include <stdlib.h>
13 #include <windows.h>
14 
15 class Q_DECL_HIDDEN KWindowInfo::Private
16 {
17 public:
18  Private()
19  : properties(0)
20  , properties2(0)
21  {
22  }
23 
24  ~Private()
25  {
26  }
27 
28  HWND win_;
29  int ref;
30  unsigned long properties;
31  unsigned long properties2;
32 
33 private:
34  Private(const Private &);
35  void operator=(const Private &);
36 };
37 
38 #include <QRect>
39 
40 KWindowInfo::KWindowInfo(WId win, unsigned long properties, unsigned long properties2)
41  : d(new Private)
42 {
43  d->ref = 1;
44  d->win_ = reinterpret_cast<HWND>(win);
45  d->properties = properties;
46  d->properties2 = properties2;
47 }
48 
50  : d(nullptr)
51 {
52 }
53 
54 KWindowInfo::~KWindowInfo()
55 {
56  if (d != nullptr) {
57  if (--d->ref == 0) {
58  delete d;
59  }
60  }
61 }
62 
64  : d(wininfo.d)
65 {
66  if (d != nullptr) {
67  ++d->ref;
68  }
69 }
70 
72 {
73  if (d != wininfo.d) {
74  if (d != nullptr)
75  if (--d->ref == 0) {
76  delete d;
77  }
78  d = wininfo.d;
79  if (d != nullptr) {
80  ++d->ref;
81  }
82  }
83  return *this;
84 }
85 
86 bool KWindowInfo::valid(bool withdrawn_is_valid) const
87 {
88  return true;
89 }
90 
91 WId KWindowInfo::win() const
92 {
93  return reinterpret_cast<WId>(d->win_);
94 }
95 
96 unsigned long KWindowInfo::state() const
97 {
98  unsigned long state = 0;
99 #ifndef _WIN32_WCE
100  if (IsZoomed(d->win_)) {
101  state |= NET::Max;
102  }
103 #endif
104  if (!IsWindowVisible(d->win_)) {
105  state |= NET::Hidden;
106  }
107 
108 #ifndef _WIN32_WCE
109  LONG_PTR lp = GetWindowLongPtr(d->win_, GWL_EXSTYLE);
110  if (lp & WS_EX_TOOLWINDOW) {
111  state |= NET::SkipTaskbar;
112  }
113 #endif
114 
115  return state;
116 }
117 
118 bool KWindowInfo::hasState(unsigned long s) const
119 {
120  return (state() & s);
121 }
122 
123 bool KWindowInfo::isMinimized() const
124 {
125 #ifndef _WIN32_WCE
126  return IsIconic(d->win_);
127 #else
128  return false;
129 #endif
130 }
131 
133 {
134 #ifndef _WIN32_WCE
135  if (IsIconic(d->win_)) {
136  return NET::Iconic;
137  }
138 #endif
139  if (!IsWindowVisible(d->win_)) {
140  return NET::Withdrawn;
141  }
142  return NET::Visible;
143 }
144 
146 {
147  return NETExtendedStrut();
148 }
149 
150 NET::WindowType KWindowInfo::windowType(int supported_types) const
151 {
153 
154  long windowStyle = GetWindowLong(d->win_, GWL_STYLE);
155  long windowStyleEx = GetWindowLong(d->win_, GWL_EXSTYLE);
156 
157  if (windowStyle & WS_POPUP && supported_types & NET::PopupMenuMask) {
158  return NET::PopupMenu;
159  } else if (windowStyleEx & WS_EX_TOOLWINDOW && supported_types & NET::TooltipMask) {
160  return NET::Tooltip;
161  } else if (!(windowStyle & WS_CHILD) && supported_types & NET::NormalMask) {
162  return NET::Normal;
163  }
164 
165  return wt;
166 }
167 
169 {
170  QString s = visibleName();
171  if (isMinimized()) {
172  s.prepend(QLatin1Char('('));
173  s.append(QLatin1Char(')'));
174  }
175  return s;
176 }
177 
179 {
180  return name();
181 }
182 
184 {
185  QByteArray windowText = QByteArray((GetWindowTextLength(d->win_) + 1) * sizeof(wchar_t), 0);
186  GetWindowTextW(d->win_, (LPWSTR)windowText.data(), windowText.size());
187  return QString::fromWCharArray((wchar_t *)windowText.data());
188 }
189 
191 {
192  return QString();
193 }
194 
196 {
197  return QString();
198 }
199 
201 {
202  return QString();
203 }
204 
206 {
207  return true;
208 }
209 
210 bool KWindowInfo::isOnDesktop(int desk) const
211 {
212  return desk == desktop();
213 }
214 
215 bool KWindowInfo::onAllDesktops() const
216 {
217  return false;
218 }
219 
220 int KWindowInfo::desktop() const
221 {
222  return 1;
223 }
224 
226 {
227  RECT wndRect;
228  memset(&wndRect, 0, sizeof(wndRect));
229 
230  // fetch the geometry INCLUDING the frames
231  if (GetWindowRect(d->win_, &wndRect)) {
232  QRect result;
233  result.setCoords(wndRect.left, wndRect.top, wndRect.right, wndRect.bottom);
234  return result;
235  }
236 
237  return QRect();
238 }
239 
241 {
242  RECT wndRect;
243  memset(&wndRect, 0, sizeof(wndRect));
244 
245  // fetch only client area geometries ... i hope that's right
246  if (GetClientRect(d->win_, &wndRect)) {
247  QRect result;
248  result.setCoords(wndRect.left, wndRect.top, wndRect.right, wndRect.bottom);
249  return result;
250  }
251 
252  return QRect();
253 }
254 
255 bool KWindowInfo::actionSupported(NET::Action action) const
256 {
257  return true; // no idea if it's supported or not -> pretend it is
258 }
259 
260 #if 0
261 WId KWindowInfo::transientFor() const
262 {
263  kWarning((d->info->passedProperties()[ NETWinInfo::PROTOCOLS2 ] & NET::WM2TransientFor) == 0, 176)
264  << "Pass NET::WM2TransientFor to KWindowInfo";
265  return d->info->transientFor();
266 }
267 
268 WId KWindowInfo::groupLeader() const
269 {
270  kWarning((d->info->passedProperties()[ NETWinInfo::PROTOCOLS2 ] & NET::WM2GroupLeader) == 0, 176)
271  << "Pass NET::WM2GroupLeader to KWindowInfo";
272  return d->info->groupLeader();
273 }
274 #endif
275 
277 {
278  // kWarning(( d->info->passedProperties()[ NETWinInfo::PROTOCOLS2 ] & NET::WM2WindowClass ) == 0, 176 )
279  // << "Pass NET::WM2WindowClass to KWindowInfo";
280  // return d->info->windowClassClass();
281 
282  // Implemented per http://tronche.com/gui/x/icccm/sec-4.html#WM_CLASS (but only 2nd and 3rd choices, -name ignored)
283  char *resourcenamevar;
284  resourcenamevar = getenv("RESOURCE_NAME");
285  if (resourcenamevar != nullptr) {
286  return QByteArray(resourcenamevar);
287  }
288 
290 }
291 
293 {
294  // kWarning(( d->info->passedProperties()[ NETWinInfo::PROTOCOLS2 ] & NET::WM2WindowClass ) == 0, 176 )
295  // << "Pass NET::WM2WindowClass to KWindowInfo";
296  // return d->info->windowClassName();
297 
298  // Maybe should use RealGetWindowClass instead of GetClassName? See
299  // http://blogs.msdn.com/b/oldnewthing/archive/2010/12/31/10110524.aspx
300 
301  const int max = 256; // truncate to 255 characters
302  TCHAR name[max];
303  int count = GetClassName(d->win_, name, max);
304  return QString::fromUtf16((ushort *)name).toLocal8Bit();
305 }
306 
307 #if 0
309 {
310  kWarning((d->info->passedProperties()[ NETWinInfo::PROTOCOLS2 ] & NET::WM2WindowRole) == 0, 176)
311  << "Pass NET::WM2WindowRole to KWindowInfo";
312  return d->info->windowRole();
313 }
314 
316 {
317  kWarning((d->info->passedProperties()[ NETWinInfo::PROTOCOLS2 ] & NET::WM2ClientMachine) == 0, 176)
318  << "Pass NET::WM2ClientMachine to KWindowInfo";
319  return d->info->clientMachine();
320 }
321 
322 bool KWindowInfo::actionSupported(NET::Action action) const
323 {
324  kWarning((d->info->passedProperties()[ NETWinInfo::PROTOCOLS2 ] & NET::WM2AllowedActions) == 0, 176)
325  << "Pass NET::WM2AllowedActions to KWindowInfo";
327  return d->info->allowedActions() & action;
328  } else {
329  return true; // no idea if it's supported or not -> pretend it is
330  }
331 }
332 #endif
QString fromWCharArray(const wchar_t *string, int size)
indicates that this is a normal, top-level window
Definition: netwm_def.h:365
QString & append(QChar ch)
indicates that a window should not be included on a taskbar.
Definition: netwm_def.h:530
QString iconName() const
Returns the name of the window that should be shown in taskbar.
QString visibleName() const
Returns the visible name of the window.
KWindowInfo & operator=(const KWindowInfo &)
Assignment operator.
QString visibleIconName() const
Returns the visible name of the window that should be shown in a taskbar.
QRect frameGeometry() const
Returns the frame geometry of the window, i.e.
indicates a popup menu (a context menu typically)
Definition: netwm_def.h:415
QString & prepend(QChar ch)
indicates that the client window is not visible, but its icon is.
Definition: netwm_def.h:638
This class provides information about a given window in the platform specific windowing system...
Definition: kwindowinfo.h:62
indicates a tooltip window
Definition: netwm_def.h:419
Partial strut class for NET classes.
Definition: netwm_def.h:180
NET::States state() const
Returns the window&#39;s state flags.
NET::WindowType windowType(NET::WindowTypes supported_types) const
Returns the window type of this window.
void ref()
QString visibleNameWithState() const
Returns a visible name with state.
QByteArray clientMachine() const
Returns the client machine for the window.
bool isMinimized() const
Returns true if the window is minimized.
indicates that a window should not be visible on the screen (e.g.
Definition: netwm_def.h:550
bool onAllDesktops() const
Returns true if the window is on all desktops.
MappingState
Client window mapping state.
Definition: netwm_def.h:624
indicates that neither the client window nor its icon is visible.
Definition: netwm_def.h:632
bool actionSupported(NET::Action action) const
Returns true if the given action is currently supported for the window.
NET::MappingState mappingState() const
Returns the mapping state of the window.
NETExtendedStrut extendedStrut() const
Returns the window extended (partial) strut.
bool valid(bool withdrawn_is_valid=false) const
Returns false if this window info is not valid.
QString fromUtf16(const ushort *unicode, int size)
WindowType
Window type.
Definition: netwm_def.h:357
KGuiItem properties()
QByteArray windowRole() const
Returns the window role for the window.
void setCoords(int x1, int y1, int x2, int y2)
WId groupLeader() const
Returns the leader window for the group the window is in, if any.
bool isOnDesktop(int desktop) const
Returns true if the window is on the given virtual desktop.
KWindowInfo(WId window, NET::Properties properties, NET::Properties2 properties2=NET::Properties2())
Reads all the info about the given window.
QByteArray toLocal8Bit() const const
QRect geometry() const
Returns the position and size of the window contents.
WId win() const
Returns the window identifier.
bool hasState(NET::States s) const
Returns true if the window has the given state flag set.
indicates the client window is visible to the user.
Definition: netwm_def.h:628
static bool allowedActionsSupported()
Returns true if the WM announces which actions it allows for windows.
WId transientFor() const
Returns the window identifier of the main window this window belongs to.
QByteArray windowClassName() const
Returns the name component of the window class for the window.
char * data()
QString name() const
Returns the name of the window, as specified by the application.
Action
Actions that can be done with a window (_NET_WM_ALLOWED_ACTIONS).
Definition: netwm_def.h:645
int desktop() const
Returns the virtual desktop this window is on.
convenience value.
Definition: netwm_def.h:522
bool isOnCurrentDesktop() const
Returns true if the window is on the currently active virtual desktop.
int size() const const
QByteArray windowClassClass() const
Returns the class component of the window class for the window.
QString visibleIconNameWithState() const
Returns a visible icon name with state.
QString applicationName()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Oct 15 2021 22:41:50 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.