OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tk/] [win/] [tkWinEmbed.c] - Blame information for rev 578

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.