File: winutils.cpp

package info (click to toggle)
amsn 0.98.3-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 33,876 kB
  • ctags: 10,292
  • sloc: tcl: 117,923; ansic: 32,173; cpp: 17,387; xml: 6,643; objc: 1,251; sh: 667; makefile: 544; perl: 215; python: 126
file content (408 lines) | stat: -rw-r--r-- 12,723 bytes parent folder | download | duplicates (2)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
		File : winutils.cpp

		Description :	Ariehs library for windows utilities

		MAN :

			NAME :
				WinLoadFile
			SYNOPSYS :
				WinLoadFile file
			DESCRIPTION :
				This command loads a file (or url) with the default application.


			NAME :
				WinPlaySound
			SYNOPSYS :
				WinPlaySound file
			DESCRIPTION :
				Plays a sound file


			NAME :
				WinSayit
			SYNOPSYS :
				WinSayit text
			DESCRIPTION :
				Speaks the text "text"

  
			NAME :
				WinRemoveTitle
			SYNOPSYS :
				WinRemoveTitle window titlebarheight
			DESCRIPTION :
				Removes the title bar and border of the window (plus a little extra)


			NAME :
				WinReplaceTitle
			SYNOPSYS :
				WinReplaceTitle window
			DESCRIPTION :
				Replaces the title bar and border of the window (whatever was removed with WinRemoveTitle)


		Author : Arieh Schneier ( lio_lion - lio_lion@user.sourceforge.net)

		In order to be able to use UNICODE functions with Win9x we must use the unicows system
		http://libunicows.sourceforge.net/ provides a free implementation of the lib that is compatible with opencow and unicows

*/

//HWND hWnd = Tk_GetHWND(Tk_WindowId((Tk_Window) clientData));

// Include the header file
#include "winutils.h"

static int Tk_WinLoadFile (ClientData clientData,
								 Tcl_Interp *interp,
								 int objc,
								 Tcl_Obj *CONST objv[]) {

	WCHAR *file = NULL;
	Tcl_Obj *argsObj = NULL;
	WCHAR* argsStr = NULL;
	int res;

	// We verify the arguments, we must have one arg, not more
	if( objc < 2) {
		Tcl_AppendResult (interp, "Wrong number of args.\nShould be \"WinLoadFile file [arguments]\"" , (char *) NULL);
		return TCL_ERROR;
	}

	// Get the first argument string (file)
	file = Tcl_GetUnicode(objv[1]);

	if (objc >= 3) {
		argsObj = Tcl_NewStringObj("", 0);
		//Get the arguments
		for (int i=2; i<objc; i++)
		{
			Tcl_AppendObjToObj(argsObj, objv[i]);
			if (i == objc-1) Tcl_AppendToObj(argsObj," ",-1);
		}
		argsStr = Tcl_GetUnicode(argsObj);
	}

	res = (int) ShellExecute(NULL, L"open", file, argsStr, NULL, SW_SHOWNORMAL);
	if (res <= 32) {
		Tcl_Obj *result = Tcl_NewStringObj("Unable to open file : ", strlen("Unable to open file : "));
		Tcl_AppendUnicodeToObj(result, file, lstrlen(file));
		Tcl_AppendToObj(result, " " , strlen(" "));
		Tcl_AppendUnicodeToObj(result, argsStr, lstrlen(argsStr));
		Tcl_AppendToObj(result, " : " , strlen(" : "));
		Tcl_AppendObjToObj(result, Tcl_NewIntObj(res));

		Tcl_SetObjResult(interp, result);
		return TCL_ERROR;
	}

	return TCL_OK;
}

static int Tk_WinPlaySound (ClientData clientData,
								 Tcl_Interp *interp,
								 int objc,
								 Tcl_Obj *CONST objv[]) {

	WCHAR *file = NULL;


	// We verify the arguments, we must have one arg, not more
	if( objc < 2) {
		Tcl_AppendResult (interp, "Wrong number of args.\nShould be \"WinPlaySound file\"" , (char *) NULL);
		return TCL_ERROR;
	}

	// Get the first argument string (file)
	file = Tcl_GetUnicode(objv[1]);

	PlaySound(file, NULL, SND_ASYNC | SND_NODEFAULT);


	return TCL_OK;
}

static int Tk_WinSayit (ClientData clientData,
								 Tcl_Interp *interp,
								 int objc,
								 Tcl_Obj *CONST objv[]) {

	WCHAR * text = NULL;

	// We verify the arguments, we must have one arg, not more
	if( objc < 2) {
		Tcl_AppendResult (interp, "Wrong number of args.\nShould be \"WinSayit text\"" , (char *) NULL);
		return TCL_ERROR;
	}

	// Get the first argument string (file)
	text=Tcl_GetUnicode(objv[1]);

    ISpVoice * pVoice = NULL;

    if (FAILED(::CoInitialize(NULL))){
		Tcl_AppendResult (interp, "Failed to run ::CoInitialize" , (char *) NULL);
		return TCL_ERROR;
	}

    HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
    if( SUCCEEDED( hr ) )
    {
        hr = pVoice->Speak(text, SPF_DEFAULT | SPF_IS_NOT_XML, NULL); //| SPF_ASYNC 
        pVoice->Release();
        pVoice = NULL;
    }

    ::CoUninitialize();
	return TCL_OK;
}

static int Tk_WinRemoveTitle (ClientData clientData,
								 Tcl_Interp *interp,
								 int objc,
								 Tcl_Obj *CONST objv[]) {
	HWND hWnd;
	Tk_Window tkwin;
	Window window;
	char * win = NULL;

	// We verify the arguments, we must have one arg, not more
	if( objc < 3) {
		Tcl_AppendResult (interp, "Wrong number of args.\nShould be \"WinRemoveTitle window titlebarheight\"" , (char *) NULL);
		return TCL_ERROR;
	}


	// Get the first argument string (object name) and check it 
	win=Tcl_GetStringFromObj(objv[1], NULL);
	// We check if the pathname is valid, this means it must beguin with a "." 
	// the strncmp(win, ".", 1) is used to compare the first char of the pathname
	if (strncmp(win,".",1)) {
		Tcl_AppendResult (interp, "Bad window path name : ",
			Tcl_GetStringFromObj(objv[1], NULL) , (char *) NULL);
		return TCL_ERROR;
	}

	// Here we ge the long pathname (tk window name), from the short pathname, using the MainWindow from the interpreter
	tkwin = Tk_NameToWindow(interp, win, Tk_MainWindow(interp));
	// Error check
	if ( tkwin == NULL) return TCL_ERROR;

	// We then get the windowId (the X token) of the window, from it's long pathname
	window = Tk_WindowId(tkwin);
	// Error check
	if ( window == NULL ) {
		Tcl_AppendResult (interp, "error while processing WindowId : Window probably not viewable", (char *) NULL);
		return TCL_ERROR;
	}
	
	// We then get the HWND (Window Handler) from the X token.
	hWnd = Tk_GetHWND(window);
	// Error check
	if ( hWnd == NULL ) {
		Tcl_AppendResult (interp, "error while processing GetHWND ", (char *) NULL);
		return TCL_ERROR;
	}

	// We get it's parent window handler. This is a bit tricky, in fact, in Tk, toplevel windows are 
	// just widgets embeded in a container, and we need to get that container's HWND to make it flash
	hWnd = GetParent(hWnd);
	// Error check
	if ( hWnd == NULL || ! IsWindow(hWnd)) {
		Tcl_AppendResult(interp, "Unknown error, pathname is not a valid toplevel window" , (char *) NULL);
		return TCL_ERROR;
	}


	// --------------------------------------------------
	// change window style (get rid of the caption bar)
	// --------------------------------------------------
	DWORD dwStyle = GetWindowLong(hWnd, GWL_STYLE);
	//dwStyle &= ~(WS_CAPTION|WS_SIZEBOX|WS_SYSMENU|WS_MAXIMIZEBOX|WS_MINIMIZEBOX);
	dwStyle &= ~(WS_CAPTION|WS_SIZEBOX);
	//SetWindowLong(hWnd, GWL_STYLE, dwStyle);
	if ( SetWindowLong(hWnd, GWL_STYLE, dwStyle) == 0 ) {
		Tcl_AppendResult (interp, "error while SetWindowLong ", (char *) NULL);
		return TCL_ERROR;
	}

	// --------------------------------------------------
	// set the visible regions
	// --------------------------------------------------
//	HRGN hRegion1 = CreateRectRgn(Tcl_GetIntFromObj(interp,objv[2], NULL),
//									Tcl_GetIntFromObj(interp,objv[3], NULL),
//									Tcl_GetIntFromObj(interp,objv[4], NULL),
//									Tcl_GetIntFromObj(interp,objv[5], NULL));

	RECT rc;
	if(!GetWindowRect(hWnd,&rc))
	{
		Tcl_AppendResult (interp, "error while GetWindowRect ", (char *) NULL);
		return TCL_ERROR;
	}
	int width = (rc.right)-(rc.left);
	int height = rc.bottom-rc.top;
	//int menuheight = Tcl_GetIntFromObj(interp,objv[2], NULL);
	int menuheight;
	char * ob = Tcl_GetStringFromObj(objv[2], NULL);
	sscanf(ob,"%d",&menuheight);

	//region 1 is menu size
	HRGN hRegion1 = CreateRectRgn(menuheight,0,width/2,menuheight);
	//region 2 is main area
	HRGN hRegion2 = CreateRectRgn(0,menuheight,width,height);
	//this if for rounded corners
	//HRGN hRegion2 = CreateRoundRectRgn(0,menuheight,width,height,menuheight,menuheight);
	//combine both into region1
	CombineRgn(hRegion1, hRegion1, hRegion2, RGN_OR);
	//using regions 2, as hiding menu bar for now (to show it just set its height to 0)
	SetWindowRgn(hWnd, hRegion2, true);
	DeleteObject(hRegion1);
	DeleteObject(hRegion2);

	// --------------------------------------------------
	// force a window repainting
	// --------------------------------------------------
	InvalidateRect(hWnd, NULL, TRUE);
	SetWindowPos(hWnd, NULL, 0,0,320,242, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
//	SetWindowPos(hWnd, NULL, 0,0,320,242, SWP_NOMOVE|SWP_NOZORDER);
//	SetWindowPos(hWnd, NULL, 0,0,320,242, SWP_NOREPOSITION | SWP_NOZORDER | SWP_FRAMECHANGED);


	char res[200];
	sprintf(res, "%d , %d , %d , %d , %d , %d , %d",rc.left,rc.right,rc.top,rc.bottom, width, height, menuheight);
	Tcl_SetResult (interp, res, TCL_VOLATILE);
	return TCL_OK;
}


static int Tk_WinReplaceTitle (ClientData clientData,
								 Tcl_Interp *interp,
								 int objc,
								 Tcl_Obj *CONST objv[]) {
	HWND hWnd;
	Tk_Window tkwin;
	Window window;
	char * win = NULL;

	// We verify the arguments, we must have one arg, not more
	if( objc < 2) {
		Tcl_AppendResult (interp, "Wrong number of args.\nShould be \"WinReplaceTitle window\"" , (char *) NULL);
		return TCL_ERROR;
	}


	// Get the first argument string (object name) and check it 
	win=Tcl_GetStringFromObj(objv[1], NULL);
	// We check if the pathname is valid, this means it must beguin with a "." 
	// the strncmp(win, ".", 1) is used to compare the first char of the pathname
	if (strncmp(win,".",1)) {
		Tcl_AppendResult (interp, "Bad window path name : ",
			Tcl_GetStringFromObj(objv[1], NULL) , (char *) NULL);
		return TCL_ERROR;
	}

	// Here we ge the long pathname (tk window name), from the short pathname, using the MainWindow from the interpreter
	tkwin = Tk_NameToWindow(interp, win, Tk_MainWindow(interp));
	// Error check
	if ( tkwin == NULL) return TCL_ERROR;

	// We then get the windowId (the X token) of the window, from it's long pathname
	window = Tk_WindowId(tkwin);
	// Error check
	if ( window == NULL ) {
		Tcl_AppendResult (interp, "error while processing WindowId : Window probably not viewable", (char *) NULL);
		return TCL_ERROR;
	}
	
	// We then get the HWND (Window Handler) from the X token.
	hWnd = Tk_GetHWND(window);
	// Error check
	if ( hWnd == NULL ) {
		Tcl_AppendResult (interp, "error while processing GetHWND ", (char *) NULL);
		return TCL_ERROR;
	}

	// We get it's parent window handler. This is a bit tricky, in fact, in Tk, toplevel windows are 
	// just widgets embeded in a container, and we need to get that container's HWND to make it flash
	hWnd = GetParent(hWnd);
	// Error check
	if ( hWnd == NULL || ! IsWindow(hWnd)) {
		Tcl_AppendResult(interp, "Unknown error, pathname is not a valid toplevel window" , (char *) NULL);
		return TCL_ERROR;
	}


	// --------------------------------------------------
	// change window style (replace the caption bar)
	// --------------------------------------------------
	DWORD dwStyle = GetWindowLong(hWnd, GWL_STYLE);
	dwStyle |= (WS_CAPTION|WS_SIZEBOX);
	if ( SetWindowLong(hWnd, GWL_STYLE, dwStyle) == 0 ) {
		Tcl_AppendResult (interp, "error while SetWindowLong ", (char *) NULL);
		return TCL_ERROR;
	}

	// --------------------------------------------------
	// remove any regions
	// --------------------------------------------------
	SetWindowRgn(hWnd, NULL, true);

	// --------------------------------------------------
	// force a window repainting
	// --------------------------------------------------
	InvalidateRect(hWnd, NULL, TRUE);
	SetWindowPos(hWnd, NULL, 0,0,320,242, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);


	return TCL_OK;
}

/*
	Function	:	Winutils_Init
	Description :	The Init function that will be called when the extension is loaded to your tk shell
	Arguments   :	Tcl_Interp *interp    :	This is the interpreter from which the load was made and to 
											which we'll add the new command
	Return value : TCL_OK in case everything is ok, or TCL_ERROR in case there is an error (Tk version < 8.3)
	Comments     : 
*/
int Winutils_Init (Tcl_Interp *interp ) {
	
	//Check TCl version
	if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) {
		return TCL_ERROR;
	}

	//Check TK version
	if (Tk_InitStubs(interp, TK_VERSION, 0) == NULL) {
		return TCL_ERROR;
	}
	
	
	// Create the new command "WinLoadFile" linked to the ShellExecute function
	Tcl_CreateObjCommand(interp, "WinLoadFile", Tk_WinLoadFile,
		(ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);

	// Create the new command "WinPlaySound" linked to the PlaySound function
	Tcl_CreateObjCommand(interp, "WinPlaySound", Tk_WinPlaySound,
		(ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);

	// Create the new command "WinSayit"
	Tcl_CreateObjCommand(interp, "WinSayit", Tk_WinSayit,
		(ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);

	// Create the new command "WinRemoveTitle"
	Tcl_CreateObjCommand(interp, "WinRemoveTitle", Tk_WinRemoveTitle,
		(ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);

	// Create the new command "WinReplaceTitle"
	Tcl_CreateObjCommand(interp, "WinReplaceTitle", Tk_WinReplaceTitle,
		(ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);

	// end
	return TCL_OK;
}