Debian Codegraph @

pTk/mTk/win/tkWinEmbed.c

raw
1 /*
2 * tkWinEmbed.c --
3 *
4 * This file contains platform specific procedures for Windows platforms
5 * to provide basic operations needed for application embedding (where
6 * one application can use as its main window an internal window from
7 * another application).
8 *
9 * Copyright (c) 1996-1997 Sun Microsystems, Inc.
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 * RCS: @(#) $Id: tkWinEmbed.c,v 1.7 2002/08/05 04:30:41 dgp Exp $
15 */
16
17 #include "tkWinInt.h"
18
19
20 /*
21 * One of the following structures exists for each container in this
22 * application. It keeps track of the container window and its
23 * associated embedded window.
24 */
25
26 typedef struct Container {
27 HWND parentHWnd; /* Windows HWND to the parent window */
28 TkWindow *parentPtr; /* Tk's information about the container
29 * or NULL if the container isn't
30 * in this process. */
31 HWND embeddedHWnd; /* Windows HWND to the embedded window
32 */
33 TkWindow *embeddedPtr; /* Tk's information about the embedded
34 * window, or NULL if the
35 * embedded application isn't in
36 * this process. */
37 struct Container *nextPtr; /* Next in list of all containers in
38 * this process. */
39 } Container;
40
41 typedef struct ThreadSpecificData {
42 Container *firstContainerPtr; /* First in list of all containers
43 * managed by this process. */
44 } ThreadSpecificData;
45 static Tcl_ThreadDataKey dataKey;
46
47 static void CleanupContainerList _ANSI_ARGS_((
48 ClientData clientData));
49 static void ContainerEventProc _ANSI_ARGS_((ClientData clientData,
50 XEvent *eventPtr));
51 static void EmbeddedEventProc _ANSI_ARGS_((
52 ClientData clientData, XEvent *eventPtr));
53 static void EmbedGeometryRequest _ANSI_ARGS_((
54 Container*containerPtr, int width, int height));
55 static void EmbedWindowDeleted _ANSI_ARGS_((TkWindow *winPtr));
56
57 /*
58 *----------------------------------------------------------------------
59 *
60 * CleanupContainerList --
61 *
62 * Finalizes the list of containers.
63 *
64 * Results:
65 * None.
66 *
67 * Side effects:
68 * Releases memory occupied by containers of embedded windows.
69 *
70 *----------------------------------------------------------------------
71 */
72
73 /* ARGSUSED */
74 static void
75 CleanupContainerList(clientData)
76 ClientData clientData;
77 {
78 Container *nextPtr;
79 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
80 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
81
82 for (;
83 tsdPtr->firstContainerPtr != (Container *) NULL;
84 tsdPtr->firstContainerPtr = nextPtr) {
85 nextPtr = tsdPtr->firstContainerPtr->nextPtr;
86 ckfree((char *) tsdPtr->firstContainerPtr);
87 }
88 tsdPtr->firstContainerPtr = (Container *) NULL;
89 }
90
91 /*
92 *----------------------------------------------------------------------
93 *
94 * TkpTestembedCmd --
95 *
96 * Test command for the embedding facility.
97 *
98 * Results:
99 * Always returns TCL_OK.
100 *
101 * Side effects:
102 * Currently it does not do anything.
103 *
104 *----------------------------------------------------------------------
105 */
106
107 /* ARGSUSED */
108 int
109 TkpTestembedCmd(clientData, interp, argc, argv)
110 ClientData clientData;
111 Tcl_Interp *interp;
112 int argc;
113 CONST char **argv;
114 {
115 return TCL_OK;
116 }
117
118 /*
119 *----------------------------------------------------------------------
120 *
121 * TkpUseWindow --
122 *
123 * This procedure causes a Tk window to use a given Windows handle
124 * for a window as its underlying window, rather than a new Windows
125 * window being created automatically. It is invoked by an embedded
126 * application to specify the window in which the application is
127 * embedded.
128 *
129 * Results:
130 * The return value is normally TCL_OK. If an error occurred (such as
131 * if the argument does not identify a legal Windows window handle),
132 * the return value is TCL_ERROR and an error message is left in the
133 * the interp's result if interp is not NULL.
134 *
135 * Side effects:
136 * None.
137 *
138 *----------------------------------------------------------------------
139 */
140
141 int
142 TkpUseWindow(interp, tkwin, string)
143 Tcl_Interp *interp; /* If not NULL, used for error reporting
144 * if string is bogus. */
145 Tk_Window tkwin; /* Tk window that does not yet have an
146 * associated X window. */
147 Tcl_Obj *string; /* String identifying an X window to use
148 * for tkwin; must be an integer value. */
149 {
150 TkWindow *winPtr = (TkWindow *) tkwin;
151 TkWindow *usePtr;
152 int id;
153 HWND hwnd;
154 Container *containerPtr;
155 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
156 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
157
158 if (winPtr->window != None) {
159 panic("TkpUseWindow: Already assigned a window");
160 }
161
162 if (Tcl_GetIntFromObj(interp, string, &id) != TCL_OK) {
163 char *s = Tcl_GetString(string);
164 char *e = NULL;
165 id = strtoul(s,&e,0);
166 if (!e || *e)
167 return TCL_ERROR;
168 }
169 hwnd = (HWND) id;
170
171 /*
172 * Check if the window is a valid handle. If it is invalid, return
173 * TCL_ERROR and potentially leave an error message in the interp's
174 * result.
175 */
176
177 if (!IsWindow(hwnd)) {
178 if (interp != (Tcl_Interp *) NULL) {
179 Tcl_AppendResult(interp, "window \"", Tcl_GetString(string),
180 "\" doesn't exist", (char *) NULL);
181 }
182 return TCL_ERROR;
183 }
184
185 usePtr = (TkWindow *) Tk_HWNDToWindow(hwnd);
186 if (usePtr != NULL) {
187 if (!(usePtr->flags & TK_CONTAINER)) {
188 Tcl_AppendResult(interp, "window \"", usePtr->pathName,
189 "\" doesn't have -container option set", (char *) NULL);
190 return TCL_ERROR;
191 }
192 }
193
194 /*
195 * Store the parent window in the platform private data slot so
196 * TkWmMapWindow can use it when creating the wrapper window.
197 */
198
199 winPtr->privatePtr = (struct TkWindowPrivate*) hwnd;
200
201 /*
202 * Create an event handler to clean up the Container structure when
203 * tkwin is eventually deleted.
204 */
205
206 Tk_CreateEventHandler(tkwin, StructureNotifyMask, EmbeddedEventProc,
207 (ClientData) winPtr);
208
209 /*
210 * If this is the first container, register an exit handler so that
211 * things will get cleaned up at finalization.
212 */
213
214 if (tsdPtr->firstContainerPtr == (Container *) NULL) {
215 Tcl_CreateExitHandler(CleanupContainerList, (ClientData) NULL);
216 }
217
218 /*
219 * Save information about the container and the embedded window
220 * in a Container structure. If there is already an existing
221 * Container structure, it means that both container and embedded
222 * app. are in the same process.
223 */
224
225 for (containerPtr = tsdPtr->firstContainerPtr;
226 containerPtr != NULL; containerPtr = containerPtr->nextPtr) {
227 if (containerPtr->parentHWnd == hwnd) {
228 winPtr->flags |= TK_BOTH_HALVES;
229 containerPtr->parentPtr->flags |= TK_BOTH_HALVES;
230 break;
231 }
232 }
233 if (containerPtr == NULL) {
234 containerPtr = (Container *) ckalloc(sizeof(Container));
235 containerPtr->parentPtr = NULL;
236 containerPtr->parentHWnd = hwnd;
237 containerPtr->nextPtr = tsdPtr->firstContainerPtr;
238 tsdPtr->firstContainerPtr = containerPtr;
239 }
240
241 /*
242 * embeddedHWnd is not created yet. It will be created by TkWmMapWindow(),
243 * which will send a TK_ATTACHWINDOW to the container window.
244 * TkWinEmbeddedEventProc will process this message and set the embeddedHWnd
245 * variable
246 */
247
248 containerPtr->embeddedPtr = winPtr;
249 containerPtr->embeddedHWnd = NULL;
250
251 winPtr->flags |= TK_EMBEDDED;
252 winPtr->flags &= (~(TK_MAPPED));
253
254 return TCL_OK;
255 }
256
257 /*
258 *----------------------------------------------------------------------
259 *
260 * TkpMakeContainer --
261 *
262 * This procedure is called to indicate that a particular window will
263 * be a container for an embedded application. This changes certain
264 * aspects of the window's behavior, such as whether it will receive
265 * events anymore.
266 *
267 * Results:
268 * None.
269 *
270 * Side effects:
271 * None.
272 *
273 *----------------------------------------------------------------------
274 */
275
276 void
277 TkpMakeContainer(tkwin)
278 Tk_Window tkwin;
279 {
280 TkWindow *winPtr = (TkWindow *) tkwin;
281 Container *containerPtr;
282 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
283 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
284
285 /*
286 * If this is the first container, register an exit handler so that
287 * things will get cleaned up at finalization.
288 */
289
290 if (tsdPtr->firstContainerPtr == (Container *) NULL) {
291 Tcl_CreateExitHandler(CleanupContainerList, (ClientData) NULL);
292 }
293
294 /*
295 * Register the window as a container so that, for example, we can
296 * find out later if the embedded app. is in the same process.
297 */
298
299 Tk_MakeWindowExist(tkwin);
300 containerPtr = (Container *) ckalloc(sizeof(Container));
301 containerPtr->parentPtr = winPtr;
302 containerPtr->parentHWnd = Tk_GetHWND(Tk_WindowId(tkwin));
303 containerPtr->embeddedHWnd = NULL;
304 containerPtr->embeddedPtr = NULL;
305 containerPtr->nextPtr = tsdPtr->firstContainerPtr;
306 tsdPtr->firstContainerPtr = containerPtr;
307 winPtr->flags |= TK_CONTAINER;
308
309 /*
310 * Unlike in tkUnixEmbed.c, we don't make any requests for events
311 * in the embedded window here. Now we just allow the embedding
312 * of another TK application into TK windows. When the embedded
313 * window makes a request, that will be done by sending to the
314 * container window a WM_USER message, which will be intercepted
315 * by TkWinContainerProc.
316 *
317 * We need to get structure events of the container itself, though.
318 */
319
320 Tk_CreateEventHandler(tkwin, StructureNotifyMask,
321 ContainerEventProc, (ClientData) containerPtr);
322 }
323
324 /*
325 *----------------------------------------------------------------------
326 *
327 * EmbeddedEventProc --
328 *
329 * This procedure is invoked by the Tk event dispatcher when various
330 * useful events are received for a window that is embedded in
331 * another application.
332 *
333 * Results:
334 * None.
335 *
336 * Side effects:
337 * Our internal state gets cleaned up when an embedded window is
338 * destroyed.
339 *
340 *----------------------------------------------------------------------
341 */
342
343 static void
344 EmbeddedEventProc(clientData, eventPtr)
345 ClientData clientData; /* Token for container window. */
346 XEvent *eventPtr; /* ResizeRequest event. */
347 {
348 TkWindow *winPtr = (TkWindow *) clientData;
349
350 if (eventPtr->type == DestroyNotify) {
351 EmbedWindowDeleted(winPtr);
352 }
353 }
354
355 /*
356 *----------------------------------------------------------------------
357 *
358 * TkWinEmbeddedEventProc --
359 *
360 * This procedure is invoked by the Tk event dispatcher when
361 * various useful events are received for the *children* of a
362 * container window. It forwards relevant information, such as
363 * geometry requests, from the events into the container's
364 * application.
365 *
366 * Results:
367 * None.
368 *
369 * Side effects:
370 * Depends on the event. For example, when ConfigureRequest events
371 * occur, geometry information gets set for the container window.
372 *
373 *----------------------------------------------------------------------
374 */
375
376 LRESULT
377 TkWinEmbeddedEventProc(hwnd, message, wParam, lParam)
378 HWND hwnd;
379 UINT message;
380 WPARAM wParam;
381 LPARAM lParam;
382 {
383 Container *containerPtr;
384 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
385 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
386
387 /*
388 * Find the Container structure associated with the parent window.
389 */
390
391 for (containerPtr = tsdPtr->firstContainerPtr;
392 containerPtr->parentHWnd != hwnd;
393 containerPtr = containerPtr->nextPtr) {
394 if (containerPtr == NULL) {
395 panic("TkWinContainerProc couldn't find Container record");
396 }
397 }
398
399 switch (message) {
400 case TK_ATTACHWINDOW:
401 /* An embedded window (either from this application or from
402 * another application) is trying to attach to this container.
403 * We attach it only if this container is not yet containing any
404 * window.
405 */
406 if (containerPtr->embeddedHWnd == NULL) {
407 containerPtr->embeddedHWnd = (HWND)wParam;
408 } else {
409 return 0;
410 }
411
412 break;
413 case TK_GEOMETRYREQ:
414 EmbedGeometryRequest(containerPtr, (int) wParam, lParam);
415 break;
416 }
417 return 1;
418 }
419
420 /*
421 *----------------------------------------------------------------------
422 *
423 * EmbedGeometryRequest --
424 *
425 * This procedure is invoked when an embedded application requests
426 * a particular size. It processes the request (which may or may
427 * not actually resize the window) and reflects the results back
428 * to the embedded application.
429 *
430 * Results:
431 * None.
432 *
433 * Side effects:
434 * If we deny the child's size change request, a Configure event
435 * is synthesized to let the child know that the size is the same
436 * as it used to be. Events get processed while we're waiting for
437 * the geometry managers to do their thing.
438 *
439 *----------------------------------------------------------------------
440 */
441
442 void
443 EmbedGeometryRequest(containerPtr, width, height)
444 Container *containerPtr; /* Information about the container window. */
445 int width, height; /* Size that the child has requested. */
446 {
447 TkWindow * winPtr = containerPtr->parentPtr;
448
449 /*
450 * Forward the requested size into our geometry management hierarchy
451 * via the container window. We need to send a Configure event back
452 * to the embedded application even if we decide not to resize
453 * the window; to make this happen, process all idle event handlers
454 * synchronously here (so that the geometry managers have had a
455 * chance to do whatever they want to do), and if the window's size
456 * didn't change then generate a configure event.
457 */
458 Tk_GeometryRequest((Tk_Window)winPtr, width, height);
459
460 if (containerPtr->embeddedHWnd != NULL) {
461 while (Tcl_DoOneEvent(TCL_IDLE_EVENTS)) {
462 /* Empty loop body. */
463 }
464
465 SetWindowPos(containerPtr->embeddedHWnd, NULL,
466 0, 0, winPtr->changes.width, winPtr->changes.height, SWP_NOZORDER);
467 }
468 }
469
470 /*
471 *----------------------------------------------------------------------
472 *
473 * ContainerEventProc --
474 *
475 * This procedure is invoked by the Tk event dispatcher when
476 * various useful events are received for the container window.
477 *
478 * Results:
479 * None.
480 *
481 * Side effects:
482 * Depends on the event. For example, when ConfigureRequest events
483 * occur, geometry information gets set for the container window.
484 *
485 *----------------------------------------------------------------------
486 */
487
488 static void
489 ContainerEventProc(clientData, eventPtr)
490 ClientData clientData; /* Token for container window. */
491 XEvent *eventPtr; /* ResizeRequest event. */
492 {
493 Container *containerPtr = (Container *)clientData;
494 Tk_Window tkwin = (Tk_Window)containerPtr->parentPtr;
495
496 if (eventPtr->type == ConfigureNotify) {
497 if (containerPtr->embeddedPtr == NULL) {
498 return;
499 }
500 /* Resize the embedded window, if there is any */
501 if (containerPtr->embeddedHWnd) {
502 SetWindowPos(containerPtr->embeddedHWnd, NULL,
503 0, 0, Tk_Width(tkwin), Tk_Height(tkwin), SWP_NOZORDER);
504 }
505 } else if (eventPtr->type == DestroyNotify) {
506 /* The container is gone, remove it from the list */
507 EmbedWindowDeleted(containerPtr->parentPtr);
508 }
509 }
510
511 /*
512 *----------------------------------------------------------------------
513 *
514 * TkpGetOtherWindow --
515 *
516 * If both the container and embedded window are in the same
517 * process, this procedure will return either one, given the other.
518 *
519 * Results:
520 * If winPtr is a container, the return value is the token for the
521 * embedded window, and vice versa. If the "other" window isn't in
522 * this process, NULL is returned.
523 *
524 * Side effects:
525 * None.
526 *
527 *----------------------------------------------------------------------
528 */
529
530 TkWindow *
531 TkpGetOtherWindow(winPtr)
532 TkWindow *winPtr; /* Tk's structure for a container or
533 * embedded window. */
534 {
535 Container *containerPtr;
536 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
537 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
538
539 for (containerPtr = tsdPtr->firstContainerPtr; containerPtr != NULL;
540 containerPtr = containerPtr->nextPtr) {
541 if (containerPtr->embeddedPtr == winPtr) {
542 return containerPtr->parentPtr;
543 } else if (containerPtr->parentPtr == winPtr) {
544 return containerPtr->embeddedPtr;
545 }
546 }
547 panic("TkpGetOtherWindow couldn't find window");
548 return NULL;
549 }
550
551 /*
552 *----------------------------------------------------------------------
553 *
554 * TkpClaimFocus --
555 *
556 * This procedure is invoked when someone asks or the input focus
557 * to be put on a window in an embedded application, but the
558 * application doesn't currently have the focus. It requests the
559 * input focus from the container application.
560 *
561 * Results:
562 * None.
563 *
564 * Side effects:
565 * The input focus may change.
566 *
567 *----------------------------------------------------------------------
568 */
569
570 void
571 TkpClaimFocus(topLevelPtr, force)
572 TkWindow *topLevelPtr; /* Top-level window containing desired
573 * focus window; should be embedded. */
574 int force; /* One means that the container should
575 * claim the focus if it doesn't
576 * currently have it. */
577 {
578 HWND hwnd = GetParent(Tk_GetHWND(topLevelPtr->window));
579 SendMessage(hwnd, TK_CLAIMFOCUS, (WPARAM) force, 0);
580 }
581
582 /*
583 *----------------------------------------------------------------------
584 *
585 * TkpRedirectKeyEvent --
586 *
587 * This procedure is invoked when a key press or release event
588 * arrives for an application that does not believe it owns the
589 * input focus. This can happen because of embedding; for example,
590 * X can send an event to an embedded application when the real
591 * focus window is in the container application and is an ancestor
592 * of the container. This procedure's job is to forward the event
593 * back to the application where it really belongs.
594 *
595 * Results:
596 * None.
597 *
598 * Side effects:
599 * The event may get sent to a different application.
600 *
601 *----------------------------------------------------------------------
602 */
603
604 void
605 TkpRedirectKeyEvent(winPtr, eventPtr)
606 TkWindow *winPtr; /* Window to which the event was originally
607 * reported. */
608 XEvent *eventPtr; /* X event to redirect (should be KeyPress
609 * or KeyRelease). */
610 {
611 /* not implemented */
612 }
613
614 /*
615 *----------------------------------------------------------------------
616 *
617 * EmbedWindowDeleted --
618 *
619 * This procedure is invoked when a window involved in embedding
620 * (as either the container or the embedded application) is
621 * destroyed. It cleans up the Container structure for the window.
622 *
623 * Results:
624 * None.
625 *
626 * Side effects:
627 * A Container structure may be freed.
628 *
629 *----------------------------------------------------------------------
630 */
631
632 static void
633 EmbedWindowDeleted(winPtr)
634 TkWindow *winPtr; /* Tk's information about window that
635 * was deleted. */
636 {
637 Container *containerPtr, *prevPtr;
638 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
639 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
640
641 /*
642 * Find the Container structure for this window work. Delete the
643 * information about the embedded application and free the container's
644 * record.
645 * The main container may be null. [Bug #476176]
646 */
647
648 prevPtr = NULL;
649 containerPtr = tsdPtr->firstContainerPtr;
650 if (containerPtr == NULL) return;
651 while (1) {
652 if (containerPtr->embeddedPtr == winPtr) {
653 containerPtr->embeddedHWnd = NULL;
654 containerPtr->embeddedPtr = NULL;
655 break;
656 }
657 if (containerPtr->parentPtr == winPtr) {
658 containerPtr->parentPtr = NULL;
659 break;
660 }
661 prevPtr = containerPtr;
662 containerPtr = containerPtr->nextPtr;
663 if (containerPtr == NULL) {
664 panic("EmbedWindowDeleted couldn't find window");
665 }
666 }
667 if ((containerPtr->embeddedPtr == NULL)
668 && (containerPtr->parentPtr == NULL)) {
669 if (prevPtr == NULL) {
670 tsdPtr->firstContainerPtr = containerPtr->nextPtr;
671 } else {
672 prevPtr->nextPtr = containerPtr->nextPtr;
673 }
674 ckfree((char *) containerPtr);
675 }
676 }
677