/
tkWindow.c
2969 lines (2682 loc) · 85 KB
/
tkWindow.c
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* tkWindow.c --
*
* This file provides basic window-manipulation procedures,
* which are equivalent to procedures in Xlib (and even
* invoke them) but also maintain the local Tk_Window
* structure.
*
* Copyright (c) 1989-1994 The Regents of the University of California.
* Copyright (c) 1994-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tkWindow.c,v 1.8 1999/04/21 21:53:28 rjohnson Exp $
*/
#include "tkPort.h"
#include "tkInt.h"
#if !defined(__WIN32__) && !defined(MAC_TCL)
#include "tkUnixInt.h"
#endif
typedef struct ThreadSpecificData {
int numMainWindows; /* Count of numver of main windows currently
* open in this thread. */
TkMainInfo *mainWindowList;
/* First in list of all main windows managed
* by this thread. */
TkDisplay *displayList;
/* List of all displays currently in use by
* the current thread. */
int initialized; /* 0 means the structures above need
* initializing. */
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
/*
* The Mutex below is used to lock access to the Tk_Uid structs above.
*/
TCL_DECLARE_MUTEX(windowMutex)
/*
* Default values for "changes" and "atts" fields of TkWindows. Note
* that Tk always requests all events for all windows, except StructureNotify
* events on internal windows: these events are generated internally.
*/
static XWindowChanges defChanges = {
0, 0, 1, 1, 0, 0, Above
};
#define ALL_EVENTS_MASK \
KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask| \
EnterWindowMask|LeaveWindowMask|PointerMotionMask|ExposureMask| \
VisibilityChangeMask|PropertyChangeMask|ColormapChangeMask
static XSetWindowAttributes defAtts= {
None, /* background_pixmap */
0, /* background_pixel */
CopyFromParent, /* border_pixmap */
0, /* border_pixel */
NorthWestGravity, /* bit_gravity */
NorthWestGravity, /* win_gravity */
NotUseful, /* backing_store */
(unsigned) ~0, /* backing_planes */
0, /* backing_pixel */
False, /* save_under */
ALL_EVENTS_MASK, /* event_mask */
0, /* do_not_propagate_mask */
False, /* override_redirect */
CopyFromParent, /* colormap */
None /* cursor */
};
/*
* The following structure defines all of the commands supported by
* Tk, and the C procedures that execute them.
*/
typedef struct {
char *name; /* Name of command. */
Tcl_CmdProc *cmdProc; /* Command's string-based procedure. */
Tcl_ObjCmdProc *objProc; /* Command's object-based procedure. */
int isSafe; /* If !0, this command will be exposed in
* a safe interpreter. Otherwise it will be
* hidden in a safe interpreter. */
int passMainWindow; /* 0 means provide NULL clientData to
* command procedure; 1 means pass main
* window as clientData to command
* procedure. */
} TkCmd;
static TkCmd commands[] = {
/*
* Commands that are part of the intrinsics:
*/
{"bell", NULL, Tk_BellObjCmd, 0, 1},
{"bind", Tk_BindCmd, NULL, 1, 1},
{"bindtags", Tk_BindtagsCmd, NULL, 1, 1},
{"clipboard", Tk_ClipboardCmd, NULL, 0, 1},
{"destroy", Tk_DestroyCmd, NULL, 1, 1},
{"event", NULL, Tk_EventObjCmd, 1, 1},
{"focus", NULL, Tk_FocusObjCmd, 1, 1},
{"font", NULL, Tk_FontObjCmd, 1, 1},
{"grab", Tk_GrabCmd, NULL, 0, 1},
{"grid", Tk_GridCmd, NULL, 1, 1},
{"image", Tk_ImageCmd, NULL, 1, 1},
{"lower", Tk_LowerCmd, NULL, 1, 1},
{"option", Tk_OptionCmd, NULL, 1, 1},
{"pack", Tk_PackCmd, NULL, 1, 1},
{"place", Tk_PlaceCmd, NULL, 1, 1},
{"raise", Tk_RaiseCmd, NULL, 1, 1},
{"selection", Tk_SelectionCmd, NULL, 0, 1},
{"tk", NULL, Tk_TkObjCmd, 0, 1},
{"tkwait", Tk_TkwaitCmd, NULL, 1, 1},
#if defined(__WIN32__) || defined(MAC_TCL)
{"tk_chooseColor", NULL, Tk_ChooseColorObjCmd, 0, 1},
{"tk_chooseDirectory", NULL, Tk_ChooseDirectoryObjCmd, 0, 1},
{"tk_getOpenFile", NULL, Tk_GetOpenFileObjCmd, 0, 1},
{"tk_getSaveFile", NULL, Tk_GetSaveFileObjCmd, 0, 1},
#endif
#ifdef __WIN32__
{"tk_messageBox", NULL, Tk_MessageBoxObjCmd, 0, 1},
#endif
{"update", NULL, Tk_UpdateObjCmd, 1, 1},
{"winfo", NULL, Tk_WinfoObjCmd, 1, 1},
{"wm", Tk_WmCmd, NULL, 0, 1},
/*
* Widget class commands.
*/
{"button", NULL, Tk_ButtonObjCmd, 1, 0},
{"canvas", Tk_CanvasCmd, NULL, 1, 1},
{"checkbutton", NULL, Tk_CheckbuttonObjCmd, 1, 0},
{"entry", NULL, Tk_EntryObjCmd, 1, 0},
{"frame", Tk_FrameCmd, NULL, 1, 1},
{"label", NULL, Tk_LabelObjCmd, 1, 0},
{"listbox", Tk_ListboxCmd, NULL, 1, 1},
{"menubutton", NULL, Tk_MenubuttonObjCmd, 1, 0},
{"message", Tk_MessageCmd, NULL, 1, 1},
{"radiobutton", NULL, Tk_RadiobuttonObjCmd, 1, 0},
{"scale", NULL, Tk_ScaleObjCmd, 1, 0},
{"scrollbar", Tk_ScrollbarCmd, NULL, 1, 1},
{"text", Tk_TextCmd, NULL, 1, 1},
{"toplevel", Tk_ToplevelCmd, NULL, 0, 1},
/*
* Misc.
*/
#ifdef MAC_TCL
{"unsupported1", TkUnsupported1Cmd, NULL, 1, 1},
#endif
{(char *) NULL, (int (*) _ANSI_ARGS_((ClientData, Tcl_Interp *, int, char **))) NULL, NULL, 0}
};
/*
* The variables and table below are used to parse arguments from
* the "argv" variable in Tk_Init.
*/
static int synchronize = 0;
static char *name = NULL;
static char *display = NULL;
static char *geometry = NULL;
static char *colormap = NULL;
static char *use = NULL;
static char *visual = NULL;
static int rest = 0;
static Tk_ArgvInfo argTable[] = {
{"-colormap", TK_ARGV_STRING, (char *) NULL, (char *) &colormap,
"Colormap for main window"},
{"-display", TK_ARGV_STRING, (char *) NULL, (char *) &display,
"Display to use"},
{"-geometry", TK_ARGV_STRING, (char *) NULL, (char *) &geometry,
"Initial geometry for window"},
{"-name", TK_ARGV_STRING, (char *) NULL, (char *) &name,
"Name to use for application"},
{"-sync", TK_ARGV_CONSTANT, (char *) 1, (char *) &synchronize,
"Use synchronous mode for display server"},
{"-visual", TK_ARGV_STRING, (char *) NULL, (char *) &visual,
"Visual for main window"},
{"-use", TK_ARGV_STRING, (char *) NULL, (char *) &use,
"Id of window in which to embed application"},
{"--", TK_ARGV_REST, (char *) 1, (char *) &rest,
"Pass all remaining arguments through to script"},
{(char *) NULL, TK_ARGV_END, (char *) NULL, (char *) NULL,
(char *) NULL}
};
/*
* Forward declarations to procedures defined later in this file:
*/
static Tk_Window CreateTopLevelWindow _ANSI_ARGS_((Tcl_Interp *interp,
Tk_Window parent, char *name, char *screenName));
static void DeleteWindowsExitProc _ANSI_ARGS_((
ClientData clientData));
static TkDisplay * GetScreen _ANSI_ARGS_((Tcl_Interp *interp,
char *screenName, int *screenPtr));
static int Initialize _ANSI_ARGS_((Tcl_Interp *interp));
static int NameWindow _ANSI_ARGS_((Tcl_Interp *interp,
TkWindow *winPtr, TkWindow *parentPtr,
char *name));
static void OpenIM _ANSI_ARGS_((TkDisplay *dispPtr));
static void UnlinkWindow _ANSI_ARGS_((TkWindow *winPtr));
/*
*----------------------------------------------------------------------
*
* CreateTopLevelWindow --
*
* Make a new window that will be at top-level (its parent will
* be the root window of a screen).
*
* Results:
* The return value is a token for the new window, or NULL if
* an error prevented the new window from being created. If
* NULL is returned, an error message will be left in
* the interp's result.
*
* Side effects:
* A new window structure is allocated locally. An X
* window is NOT initially created, but will be created
* the first time the window is mapped.
*
*----------------------------------------------------------------------
*/
static Tk_Window
CreateTopLevelWindow(interp, parent, name, screenName)
Tcl_Interp *interp; /* Interpreter to use for error reporting. */
Tk_Window parent; /* Token for logical parent of new window
* (used for naming, options, etc.). May
* be NULL. */
char *name; /* Name for new window; if parent is
* non-NULL, must be unique among parent's
* children. */
char *screenName; /* Name of screen on which to create
* window. NULL means use DISPLAY environment
* variable to determine. Empty string means
* use parent's screen, or DISPLAY if no
* parent. */
{
register TkWindow *winPtr;
register TkDisplay *dispPtr;
int screenId;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
if (!tsdPtr->initialized) {
tsdPtr->initialized = 1;
/*
* Create built-in image types.
*/
Tk_CreateImageType(&tkBitmapImageType);
Tk_CreateImageType(&tkPhotoImageType);
/*
* Create built-in photo image formats.
*/
Tk_CreatePhotoImageFormat(&tkImgFmtGIF);
Tk_CreatePhotoImageFormat(&tkImgFmtPPM);
/*
* Create exit handler to delete all windows when the application
* exits.
*/
Tcl_CreateExitHandler(DeleteWindowsExitProc, (ClientData) NULL);
}
if ((parent != NULL) && (screenName != NULL) && (screenName[0] == '\0')) {
dispPtr = ((TkWindow *) parent)->dispPtr;
screenId = Tk_ScreenNumber(parent);
} else {
dispPtr = GetScreen(interp, screenName, &screenId);
if (dispPtr == NULL) {
return (Tk_Window) NULL;
}
}
winPtr = TkAllocWindow(dispPtr, screenId, (TkWindow *) parent);
/*
* Force the window to use a border pixel instead of border pixmap.
* This is needed for the case where the window doesn't use the
* default visual. In this case, the default border is a pixmap
* inherited from the root window, which won't work because it will
* have the wrong visual.
*/
winPtr->dirtyAtts |= CWBorderPixel;
/*
* (Need to set the TK_TOP_LEVEL flag immediately here; otherwise
* Tk_DestroyWindow will core dump if it is called before the flag
* has been set.)
*/
winPtr->flags |= TK_TOP_LEVEL;
if (parent != NULL) {
if (NameWindow(interp, winPtr, (TkWindow *) parent, name) != TCL_OK) {
Tk_DestroyWindow((Tk_Window) winPtr);
return (Tk_Window) NULL;
}
}
TkWmNewWindow(winPtr);
return (Tk_Window) winPtr;
}
/*
*----------------------------------------------------------------------
*
* GetScreen --
*
* Given a string name for a display-plus-screen, find the
* TkDisplay structure for the display and return the screen
* number too.
*
* Results:
* The return value is a pointer to information about the display,
* or NULL if the display couldn't be opened. In this case, an
* error message is left in the interp's result. The location at
* *screenPtr is overwritten with the screen number parsed from
* screenName.
*
* Side effects:
* A new connection is opened to the display if there is no
* connection already. A new TkDisplay data structure is also
* setup, if necessary.
*
*----------------------------------------------------------------------
*/
static TkDisplay *
GetScreen(interp, screenName, screenPtr)
Tcl_Interp *interp; /* Place to leave error message. */
char *screenName; /* Name for screen. NULL or empty means
* use DISPLAY envariable. */
int *screenPtr; /* Where to store screen number. */
{
register TkDisplay *dispPtr;
char *p;
int screenId;
size_t length;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
/*
* Separate the screen number from the rest of the display
* name. ScreenName is assumed to have the syntax
* <display>.<screen> with the dot and the screen being
* optional.
*/
screenName = TkGetDefaultScreenName(interp, screenName);
if (screenName == NULL) {
Tcl_SetResult(interp,
"no display name and no $DISPLAY environment variable",
TCL_STATIC);
return (TkDisplay *) NULL;
}
length = strlen(screenName);
screenId = 0;
p = screenName+length-1;
while (isdigit(UCHAR(*p)) && (p != screenName)) {
p--;
}
if ((*p == '.') && (p[1] != '\0')) {
length = p - screenName;
screenId = strtoul(p+1, (char **) NULL, 10);
}
/*
* See if we already have a connection to this display. If not,
* then open a new connection.
*/
for (dispPtr = TkGetDisplayList(); ; dispPtr = dispPtr->nextPtr) {
if (dispPtr == NULL) {
dispPtr = TkpOpenDisplay(screenName);
if (dispPtr == NULL) {
Tcl_AppendResult(interp, "couldn't connect to display \"",
screenName, "\"", (char *) NULL);
return (TkDisplay *) NULL;
}
dispPtr->nextPtr = TkGetDisplayList();
dispPtr->name = (char *) ckalloc((unsigned) (length+1));
dispPtr->lastEventTime = CurrentTime;
dispPtr->borderInit = 0;
dispPtr->atomInit = 0;
dispPtr->bindInfoStale = 1;
dispPtr->modeModMask = 0;
dispPtr->metaModMask = 0;
dispPtr->altModMask = 0;
dispPtr->numModKeyCodes = 0;
dispPtr->modKeyCodes = NULL;
dispPtr->bitmapInit = 0;
dispPtr->bitmapAutoNumber = 0;
dispPtr->numIdSearches = 0;
dispPtr->numSlowSearches = 0;
dispPtr->colorInit = 0;
dispPtr->stressPtr = NULL;
dispPtr->cursorInit = 0;
dispPtr->cursorString[0] = '\0';
dispPtr->cursorFont = None;
dispPtr->errorPtr = NULL;
dispPtr->deleteCount = 0;
dispPtr->delayedMotionPtr = NULL;
dispPtr->focusDebug = 0;
dispPtr->implicitWinPtr = NULL;
dispPtr->focusPtr = NULL;
dispPtr->gcInit = 0;
dispPtr->geomInit = 0;
dispPtr->uidInit = 0;
dispPtr->grabWinPtr = NULL;
dispPtr->eventualGrabWinPtr = NULL;
dispPtr->buttonWinPtr = NULL;
dispPtr->serverWinPtr = NULL;
dispPtr->firstGrabEventPtr = NULL;
dispPtr->lastGrabEventPtr = NULL;
dispPtr->grabFlags = 0;
dispPtr->gridInit = 0;
dispPtr->imageId = 0;
dispPtr->packInit = 0;
dispPtr->placeInit = 0;
dispPtr->selectionInfoPtr = NULL;
dispPtr->multipleAtom = None;
dispPtr->clipWindow = NULL;
dispPtr->clipboardActive = 0;
dispPtr->clipboardAppPtr = NULL;
dispPtr->clipTargetPtr = NULL;
dispPtr->commTkwin = NULL;
dispPtr->wmTracing = 0;
dispPtr->firstWmPtr = NULL;
dispPtr->foregroundWmPtr = NULL;
dispPtr->destroyCount = 0;
dispPtr->lastDestroyRequest = 0;
dispPtr->cmapPtr = NULL;
Tcl_InitHashTable(&dispPtr->winTable, TCL_ONE_WORD_KEYS);
dispPtr->refCount = 0;
strncpy(dispPtr->name, screenName, length);
dispPtr->name[length] = '\0';
OpenIM(dispPtr);
TkInitXId(dispPtr);
tsdPtr->displayList = dispPtr;
break;
}
if ((strncmp(dispPtr->name, screenName, length) == 0)
&& (dispPtr->name[length] == '\0')) {
break;
}
}
if (screenId >= ScreenCount(dispPtr->display)) {
char buf[32 + TCL_INTEGER_SPACE];
sprintf(buf, "bad screen number \"%d\"", screenId);
Tcl_SetResult(interp, buf, TCL_VOLATILE);
return (TkDisplay *) NULL;
}
*screenPtr = screenId;
return dispPtr;
}
/*
*----------------------------------------------------------------------
*
* TkGetDisplay --
*
* Given an X display, TkGetDisplay returns the TkDisplay
* structure for the display.
*
* Results:
* The return value is a pointer to information about the display,
* or NULL if the display did not have a TkDisplay structure.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
TkDisplay *
TkGetDisplay(display)
Display *display; /* X's display pointer */
{
TkDisplay *dispPtr;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
for (dispPtr = tsdPtr->displayList; dispPtr != NULL;
dispPtr = dispPtr->nextPtr) {
if (dispPtr->display == display) {
break;
}
}
return dispPtr;
}
/*
*--------------------------------------------------------------
*
* TkGetDisplayList --
*
* This procedure returns a pointer to the thread-local
* list of TkDisplays corresponding to the open displays.
*
* Results:
* The return value is a pointer to the first TkDisplay
* structure in thread-local-storage.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
TkDisplay *
TkGetDisplayList()
{
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
return tsdPtr->displayList;
}
/*
*--------------------------------------------------------------
*
* TkGetMainInfoList --
*
* This procedure returns a pointer to the list of structures
* containing information about all main windows for the
* current thread.
*
* Results:
* The return value is a pointer to the first TkMainInfo
* structure in thread local storage.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
TkMainInfo *
TkGetMainInfoList()
{
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
return tsdPtr->mainWindowList;
}
/*
*--------------------------------------------------------------
*
* TkAllocWindow --
*
* This procedure creates and initializes a TkWindow structure.
*
* Results:
* The return value is a pointer to the new window.
*
* Side effects:
* A new window structure is allocated and all its fields are
* initialized.
*
*--------------------------------------------------------------
*/
TkWindow *
TkAllocWindow(dispPtr, screenNum, parentPtr)
TkDisplay *dispPtr; /* Display associated with new window. */
int screenNum; /* Index of screen for new window. */
TkWindow *parentPtr; /* Parent from which this window should
* inherit visual information. NULL means
* use screen defaults instead of
* inheriting. */
{
register TkWindow *winPtr;
winPtr = (TkWindow *) ckalloc(sizeof(TkWindow));
winPtr->display = dispPtr->display;
winPtr->dispPtr = dispPtr;
winPtr->screenNum = screenNum;
if ((parentPtr != NULL) && (parentPtr->display == winPtr->display)
&& (parentPtr->screenNum == winPtr->screenNum)) {
winPtr->visual = parentPtr->visual;
winPtr->depth = parentPtr->depth;
} else {
winPtr->visual = DefaultVisual(dispPtr->display, screenNum);
winPtr->depth = DefaultDepth(dispPtr->display, screenNum);
}
winPtr->window = None;
winPtr->childList = NULL;
winPtr->lastChildPtr = NULL;
winPtr->parentPtr = NULL;
winPtr->nextPtr = NULL;
winPtr->mainPtr = NULL;
winPtr->pathName = NULL;
winPtr->nameUid = NULL;
winPtr->classUid = NULL;
winPtr->changes = defChanges;
winPtr->dirtyChanges = CWX|CWY|CWWidth|CWHeight|CWBorderWidth;
winPtr->atts = defAtts;
if ((parentPtr != NULL) && (parentPtr->display == winPtr->display)
&& (parentPtr->screenNum == winPtr->screenNum)) {
winPtr->atts.colormap = parentPtr->atts.colormap;
} else {
winPtr->atts.colormap = DefaultColormap(dispPtr->display, screenNum);
}
winPtr->dirtyAtts = CWEventMask|CWColormap|CWBitGravity;
winPtr->flags = 0;
winPtr->handlerList = NULL;
#ifdef TK_USE_INPUT_METHODS
winPtr->inputContext = NULL;
#endif /* TK_USE_INPUT_METHODS */
winPtr->tagPtr = NULL;
winPtr->numTags = 0;
winPtr->optionLevel = -1;
winPtr->selHandlerList = NULL;
winPtr->geomMgrPtr = NULL;
winPtr->geomData = NULL;
winPtr->reqWidth = winPtr->reqHeight = 1;
winPtr->internalBorderWidth = 0;
winPtr->wmInfoPtr = NULL;
winPtr->classProcsPtr = NULL;
winPtr->instanceData = NULL;
winPtr->privatePtr = NULL;
return winPtr;
}
/*
*----------------------------------------------------------------------
*
* NameWindow --
*
* This procedure is invoked to give a window a name and insert
* the window into the hierarchy associated with a particular
* application.
*
* Results:
* A standard Tcl return value.
*
* Side effects:
* See above.
*
*----------------------------------------------------------------------
*/
static int
NameWindow(interp, winPtr, parentPtr, name)
Tcl_Interp *interp; /* Interpreter to use for error reporting. */
register TkWindow *winPtr; /* Window that is to be named and inserted. */
TkWindow *parentPtr; /* Pointer to logical parent for winPtr
* (used for naming, options, etc.). */
char *name; /* Name for winPtr; must be unique among
* parentPtr's children. */
{
#define FIXED_SIZE 200
char staticSpace[FIXED_SIZE];
char *pathName;
int new;
Tcl_HashEntry *hPtr;
int length1, length2;
/*
* Setup all the stuff except name right away, then do the name stuff
* last. This is so that if the name stuff fails, everything else
* will be properly initialized (needed to destroy the window cleanly
* after the naming failure).
*/
winPtr->parentPtr = parentPtr;
winPtr->nextPtr = NULL;
if (parentPtr->childList == NULL) {
parentPtr->childList = winPtr;
} else {
parentPtr->lastChildPtr->nextPtr = winPtr;
}
parentPtr->lastChildPtr = winPtr;
winPtr->mainPtr = parentPtr->mainPtr;
winPtr->mainPtr->refCount++;
winPtr->nameUid = Tk_GetUid(name);
/*
* Don't permit names that start with an upper-case letter: this
* will just cause confusion with class names in the option database.
*/
if (isupper(UCHAR(name[0]))) {
Tcl_AppendResult(interp,
"window name starts with an upper-case letter: \"",
name, "\"", (char *) NULL);
return TCL_ERROR;
}
/*
* To permit names of arbitrary length, must be prepared to malloc
* a buffer to hold the new path name. To run fast in the common
* case where names are short, use a fixed-size buffer on the
* stack.
*/
length1 = strlen(parentPtr->pathName);
length2 = strlen(name);
if ((length1+length2+2) <= FIXED_SIZE) {
pathName = staticSpace;
} else {
pathName = (char *) ckalloc((unsigned) (length1+length2+2));
}
if (length1 == 1) {
pathName[0] = '.';
strcpy(pathName+1, name);
} else {
strcpy(pathName, parentPtr->pathName);
pathName[length1] = '.';
strcpy(pathName+length1+1, name);
}
hPtr = Tcl_CreateHashEntry(&parentPtr->mainPtr->nameTable, pathName, &new);
if (pathName != staticSpace) {
ckfree(pathName);
}
if (!new) {
Tcl_AppendResult(interp, "window name \"", name,
"\" already exists in parent", (char *) NULL);
return TCL_ERROR;
}
Tcl_SetHashValue(hPtr, winPtr);
winPtr->pathName = Tcl_GetHashKey(&parentPtr->mainPtr->nameTable, hPtr);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TkCreateMainWindow --
*
* Make a new main window. A main window is a special kind of
* top-level window used as the outermost window in an
* application.
*
* Results:
* The return value is a token for the new window, or NULL if
* an error prevented the new window from being created. If
* NULL is returned, an error message will be left in
* the interp's result.
*
* Side effects:
* A new window structure is allocated locally; "interp" is
* associated with the window and registered for "send" commands
* under "baseName". BaseName may be extended with an instance
* number in the form "#2" if necessary to make it globally
* unique. Tk-related commands are bound into interp.
*
*----------------------------------------------------------------------
*/
Tk_Window
TkCreateMainWindow(interp, screenName, baseName)
Tcl_Interp *interp; /* Interpreter to use for error reporting. */
char *screenName; /* Name of screen on which to create
* window. Empty or NULL string means
* use DISPLAY environment variable. */
char *baseName; /* Base name for application; usually of the
* form "prog instance". */
{
Tk_Window tkwin;
int dummy;
int isSafe;
Tcl_HashEntry *hPtr;
register TkMainInfo *mainPtr;
register TkWindow *winPtr;
register TkCmd *cmdPtr;
ClientData clientData;
ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
/*
* Panic if someone updated the TkWindow structure without
* also updating the Tk_FakeWin structure (or vice versa).
*/
if (sizeof(TkWindow) != sizeof(Tk_FakeWin)) {
panic("TkWindow and Tk_FakeWin are not the same size");
}
/*
* Create the basic TkWindow structure.
*/
tkwin = CreateTopLevelWindow(interp, (Tk_Window) NULL, baseName,
screenName);
if (tkwin == NULL) {
return NULL;
}
/*
* Create the TkMainInfo structure for this application, and set
* up name-related information for the new window.
*/
winPtr = (TkWindow *) tkwin;
mainPtr = (TkMainInfo *) ckalloc(sizeof(TkMainInfo));
mainPtr->winPtr = winPtr;
mainPtr->refCount = 1;
mainPtr->interp = interp;
Tcl_InitHashTable(&mainPtr->nameTable, TCL_STRING_KEYS);
TkEventInit();
TkBindInit(mainPtr);
TkFontPkgInit(mainPtr);
mainPtr->tlFocusPtr = NULL;
mainPtr->displayFocusPtr = NULL;
mainPtr->optionRootPtr = NULL;
Tcl_InitHashTable(&mainPtr->imageTable, TCL_STRING_KEYS);
mainPtr->strictMotif = 0;
if (Tcl_LinkVar(interp, "tk_strictMotif", (char *) &mainPtr->strictMotif,
TCL_LINK_BOOLEAN) != TCL_OK) {
Tcl_ResetResult(interp);
}
mainPtr->nextPtr = tsdPtr->mainWindowList;
tsdPtr->mainWindowList = mainPtr;
winPtr->mainPtr = mainPtr;
hPtr = Tcl_CreateHashEntry(&mainPtr->nameTable, ".", &dummy);
Tcl_SetHashValue(hPtr, winPtr);
winPtr->pathName = Tcl_GetHashKey(&mainPtr->nameTable, hPtr);
/*
* We have just created another Tk application; increment the refcount
* on the display pointer.
*/
winPtr->dispPtr->refCount++;
/*
* Register the interpreter for "send" purposes.
*/
winPtr->nameUid = Tk_GetUid(Tk_SetAppName(tkwin, baseName));
/*
* Bind in Tk's commands.
*/
isSafe = Tcl_IsSafe(interp);
for (cmdPtr = commands; cmdPtr->name != NULL; cmdPtr++) {
if ((cmdPtr->cmdProc == NULL) && (cmdPtr->objProc == NULL)) {
panic("TkCreateMainWindow: builtin command with NULL string and object procs");
}
if (cmdPtr->passMainWindow) {
clientData = (ClientData) tkwin;
} else {
clientData = (ClientData) NULL;
}
if (cmdPtr->cmdProc != NULL) {
Tcl_CreateCommand(interp, cmdPtr->name, cmdPtr->cmdProc,
clientData, (void (*) _ANSI_ARGS_((ClientData))) NULL);
} else {
Tcl_CreateObjCommand(interp, cmdPtr->name, cmdPtr->objProc,
clientData, NULL);
}
if (isSafe) {
if (!(cmdPtr->isSafe)) {
Tcl_HideCommand(interp, cmdPtr->name, cmdPtr->name);
}
}
}
TkCreateMenuCmd(interp);
/*
* Set variables for the intepreter.
*/
Tcl_SetVar(interp, "tk_patchLevel", TK_PATCH_LEVEL, TCL_GLOBAL_ONLY);
Tcl_SetVar(interp, "tk_version", TK_VERSION, TCL_GLOBAL_ONLY);
tsdPtr->numMainWindows++;
return tkwin;
}
/*
*--------------------------------------------------------------
*
* Tk_CreateWindow --
*
* Create a new internal or top-level window as a child of an
* existing window.
*
* Results:
* The return value is a token for the new window. This
* is not the same as X's token for the window. If an error
* occurred in creating the window (e.g. no such display or
* screen), then an error message is left in the interp's result and
* NULL is returned.
*
* Side effects:
* A new window structure is allocated locally. An X
* window is not initially created, but will be created
* the first time the window is mapped.
*
*--------------------------------------------------------------
*/
Tk_Window
Tk_CreateWindow(interp, parent, name, screenName)
Tcl_Interp *interp; /* Interpreter to use for error reporting.
* the interp's result is assumed to be
* initialized by the caller. */
Tk_Window parent; /* Token for parent of new window. */
char *name; /* Name for new window. Must be unique
* among parent's children. */
char *screenName; /* If NULL, new window will be internal on
* same screen as its parent. If non-NULL,
* gives name of screen on which to create
* new window; window will be a top-level
* window. */
{
TkWindow *parentPtr = (TkWindow *) parent;
TkWindow *winPtr;
if ((parentPtr != NULL) && (parentPtr->flags & TK_ALREADY_DEAD)) {
Tcl_AppendResult(interp,
"can't create window: parent has been destroyed",
(char *) NULL);
return NULL;
} else if ((parentPtr != NULL) &&
(parentPtr->flags & TK_CONTAINER)) {
Tcl_AppendResult(interp,
"can't create window: its parent has -container = yes",
(char *) NULL);
return NULL;
}
if (screenName == NULL) {
winPtr = TkAllocWindow(parentPtr->dispPtr, parentPtr->screenNum,
parentPtr);
if (NameWindow(interp, winPtr, parentPtr, name) != TCL_OK) {
Tk_DestroyWindow((Tk_Window) winPtr);
return NULL;
} else {
return (Tk_Window) winPtr;
}
} else {
return CreateTopLevelWindow(interp, parent, name, screenName);
}
}
/*
*----------------------------------------------------------------------
*
* Tk_CreateWindowFromPath --
*
* This procedure is similar to Tk_CreateWindow except that
* it uses a path name to create the window, rather than a
* parent and a child name.
*
* Results:
* The return value is a token for the new window. This
* is not the same as X's token for the window. If an error
* occurred in creating the window (e.g. no such display or
* screen), then an error message is left in the interp's result and
* NULL is returned.
*
* Side effects:
* A new window structure is allocated locally. An X
* window is not initially created, but will be created
* the first time the window is mapped.
*
*----------------------------------------------------------------------
*/
Tk_Window
Tk_CreateWindowFromPath(interp, tkwin, pathName, screenName)
Tcl_Interp *interp; /* Interpreter to use for error reporting.
* the interp's result is assumed to be
* initialized by the caller. */
Tk_Window tkwin; /* Token for any window in application
* that is to contain new window. */
char *pathName; /* Path name for new window within the
* application of tkwin. The parent of
* this window must already exist, but
* the window itself must not exist. */
char *screenName; /* If NULL, new window will be on same
* screen as its parent. If non-NULL,
* gives name of screen on which to create
* new window; window will be a top-level
* window. */
{
#define FIXED_SPACE 5