aboutsummaryrefslogtreecommitdiff
path: root/generic/WindowUtils.c
blob: 37c2086b1d5773a61b0101e3b9dac99f58b3c36a (plain)
1
2
3
4
5
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * WindowUtils.c -- Implementation of routines to manipulate windows
 *
 * Authors              : Christophe Berthuet, Alexandre Lemort
 * Creation date        : Fri oct 12 14:47:42 2007
 *
 */


#include "WindowUtils.h"



/*
*************************************
 *
 * Platform specific functions 
 *
 *************************************
 */ 
#ifdef _WIN32

/*
 *-----------------------------------
 *
 * Win32
 *
 *-----------------------------------
 */ 

/*
 * Enumwindows callback struct
 */
typedef struct {
   HWND window;
   char *windowtitle;
} EnumWindowsCallbackStruct;


/*
 * EnumWindows Callback
 */
BOOL CALLBACK SearchWindowCallback(HWND window, LPARAM userData)
{
   char windowTitle[1024];
   EnumWindowsCallbackStruct *myStruct = (EnumWindowsCallbackStruct *)userData;

   /* Get title of our window */
   GetWindowText(window, (LPSTR)windowTitle, 1024);
   
   /* Test if we have found our window */
   if (Tcl_StringMatch((const char*)windowTitle, myStruct->windowtitle)) {
      /* we have found our window */
      myStruct->window = window;
      
      /* Exit EnumWindows */
      return FALSE;
   }

   return TRUE;
}


/*
 * Retrieves the window handler of a window identified by its title
 */
HWND SearchWindowByTitle_simple(char *title, Display *display, HWND root, int depth)
{
   return FindWindow(NULL, (LPCTSTR)title);
}

HWND SearchWindowByTitle(char *title, Display *display, HWND root, int depth)
{
   EnumWindowsCallbackStruct myStruct;
   myStruct.window = NULL;
   myStruct.windowtitle = title;

   /* Try to find our window */
   EnumWindows(SearchWindowCallback, (LPARAM)&myStruct);

   /* Return result */
   return myStruct.window;
}


/*
 * Remove window decoration
 */
LONG removeWindowDecoration(HWND window)
{
   /* Get style of our window */
   LONG previousWindowStyle = GetWindowLong(window, GWL_STYLE);
   LONG windowStyle = previousWindowStyle;
  
   /* Change style */
   windowStyle &= WS_DLGFRAME;
   windowStyle &= WS_CAPTION;
   windowStyle &= WS_POPUP;
   windowStyle |= WS_CHILD;
   
   SetWindowLong(window, GWL_STYLE, windowStyle);//windowStyle);
   
   /* Apply change */
   /* NB: SetWindowPos parameters (HWND_BOTTOM, 0, 0, 100, 100) will not be applied */
   /* SWP_NOZORDER inhibits HWND_BOTTOM */
   /* SWP_NOMOVE inhibits 0, 0 */
   /* SWP_NOSIZE inhibits 100, 100 */
   /* SWP_FRAMECHANGED applies our new style */
   SetWindowPos(window, HWND_BOTTOM, 0, 0, 100, 100, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
   
   /* return previous style */
   return previousWindowStyle;
}


/*
 * Restore window style
 */
void restoreWindowStyle(HWND window, LONG windowStyle) 
{
   /* Restore style */
   SetWindowLong(window, GWL_STYLE, windowStyle);
   
   /* Apply change */
   /* NB: SetWindowPos parameters (HWND_BOTTOM, 0, 0, 100, 100) will not be applied */
   /* SWP_NOZORDER inhibits HWND_BOTTOM */
   /* SWP_NOMOVE inhibits 0, 0 */
   /* SWP_NOSIZE inhibits 100, 100 */
   /* SWP_FRAMECHANGED applies our new style */
   SetWindowPos(window, HWND_BOTTOM, 0, 0, 100, 100, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
}




#else 


/*
 *-----------------------------------
 *
 * Linux
 *
 *-----------------------------------
 */ 


/*
 * Retrieves the window handler of a window identified by its title
 */
Window SearchWindowByTitle(char* wndTitle, Display *dpy, Window wndRoot, int depth)
{
   Window wndFound = 0;

   // Variables used in the search
   Window root_return; 
   Window parent_return; 
   Window *children_return = 0 ;
   unsigned int nchildren_return = 0;
   
   char msg[255]; 
   int j = 0;

   if( XQueryTree( dpy, wndRoot, &root_return, &parent_return, &children_return, &nchildren_return) != 0 )
   {
      XTextProperty text_prop_return;
      for( j = 0; j < nchildren_return; j++ )
      {
         if( XGetWMName(dpy, children_return[j], &text_prop_return) != 0 )
         {
            if (Tcl_StringMatch(((const char *)(text_prop_return.value)), wndTitle)) 
            { 
               // Window found !!
               wndFound = children_return[j];
               break;
            }
            else
            {
               wndFound = SearchWindowByTitle(wndTitle, dpy, children_return[j], depth +  1 );
               if( wndFound != 0) 
               {
                  break;
               }
            }
         }
         else
         {
            wndFound = SearchWindowByTitle(wndTitle, dpy, children_return[j], depth +  1);
            if( wndFound != 0) 
            {
               break;
            }
         }
      }  
      XFree( children_return );
   }

   return wndFound;
}


/*
 * Get WM_STATE value of a window
 */
static int 
getWM_STATE (Display *display, Window window)
{
 int status = 0;
 long state = 0;
 static Atom aWM_STATE = 0;
 Atom actual_type = 0;
 int actual_format = 0;
 unsigned long num_ret = 0, num_left = 0;
 unsigned char *data = NULL;

 if (!aWM_STATE) {
  aWM_STATE = XInternAtom(display, "WM_STATE", False);
 }

 status = XGetWindowProperty (display, window, aWM_STATE, 0L, sizeof (long), False, aWM_STATE, &actual_type, &actual_format, &num_ret, &num_left, &data);

 /*I should point out that Success is defined as 0 in X11*/
 if ((status == Success) && (NULL != data)) {
   state = *(long *)data;
 } else {
   state = -1;
 }

 if (data) {
   XFree (data);
 }
 return state;
}


/*
 * Withdraw a window from desktop
 */
void withdrawWindowFromDesktop (Display *display, Window window, int screenNum) {
 int wm_state;

 /* Try to withdraw window */
 XWithdrawWindow (display, window, screenNum);
 XSync (display, 0);
 
 /* Test if we succeed. Otherwise, try until it works */
 while ((wm_state = getWM_STATE (display, window)) != 0 && wm_state != -1) {
  XSync (display, 0);
  XWithdrawWindow (display, window, screenNum);
 }
}




#endif /* ifdef _WIN32 */




/*
 ************************************
 *
 * Common functions
 *
 ************************************
 */