1 // Created on: 1994-07-27
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
21 // include windows.h first to have all definitions available
26 #include <Standard_ErrorHandler.hxx>
29 #include <Draw_Interpretor.hxx>
30 #include <Draw_Appli.hxx>
31 #include <TCollection_AsciiString.hxx>
32 #include <Image_AlienPixMap.hxx>
34 extern Draw_Interpretor theCommands;
35 extern Standard_Boolean Draw_VirtualWindows;
36 static Tcl_Interp *interp; /* Interpreter for this application. */
39 *----------------------------------------------------------------------
43 * Issue a prompt on standard output, or invoke a script
44 * to issue the prompt.
50 * A prompt gets output, and a Tcl script may be evaluated
53 *----------------------------------------------------------------------
56 static void Prompt(Tcl_Interp *Interp, int partial)
60 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4))) && !defined(USE_NON_CONST)
61 const char *promptCmd;
66 Tcl_Channel outChannel, errChannel;
67 outChannel = Tcl_GetStdChannel(TCL_STDOUT);
68 promptCmd = Tcl_GetVar(Interp,(char*)
69 (partial ? "tcl_prompt2" : "tcl_prompt1"), TCL_GLOBAL_ONLY);
71 if (promptCmd == NULL) {
73 if (!partial && outChannel) {
74 Tcl_Write(outChannel, "% ", 2);
77 code = Tcl_Eval(Interp, promptCmd);
78 outChannel = Tcl_GetStdChannel(TCL_STDOUT);
79 errChannel = Tcl_GetStdChannel(TCL_STDERR);
82 Tcl_Write(errChannel, Interp->result, -1);
83 Tcl_Write(errChannel, "\n", 1);
85 Tcl_AddErrorInfo(Interp,
86 "\n (script that generates prompt)");
91 Tcl_Flush(outChannel);
101 #include <OSD_Timer.hxx>
103 #ifdef HAVE_SYS_TIME_H
104 # include <sys/time.h>
107 #ifdef HAVE_SYS_TYPES_H
108 # include <sys/types.h>
111 #ifdef HAVE_SYS_SELECT_H
112 #include <sys/select.h>
115 #ifdef HAVE_SYS_FILIO_H
116 #include <sys/filio.h>
118 #include <sys/ioctl.h>
127 #include <Draw_Window.hxx>
129 #ifdef HAVE_STRINGS_H
130 # include <strings.h>
134 #include <Draw_WindowBase.hxx>
135 #include <X11/XWDFile.h>
141 * Global variables used by the main program:
144 static Tk_Window mainWindow; /* The main window for the application. If
145 * NULL then the application no longer
147 char *tcl_RcFileName = NULL; /* Name of a user-specific startup script
148 * to source if the application is being run
149 * interactively (e.g. "~/.wishrc"). Set
150 * by Tcl_AppInit. NULL means don't source
153 static Tcl_DString command; /* Used to assemble lines of terminal input
154 * into Tcl commands. */
155 static Tcl_DString line; /* Used to read the next line from the
157 //static char errorExitCmd[] = "exit 1";
160 * Forward declarations for procedures defined later in this file:
163 static void StdinProc (ClientData clientData, int mask);
165 static void Prompt (Tcl_Interp *Interp, int partial);
167 static Standard_Boolean tty; /* Non-zero means standard input is a
168 * terminal-like device. Zero means it's
171 static unsigned long thePixels[MAXCOLOR];
174 Display* Draw_WindowDisplay = NULL;
175 Standard_Integer Draw_WindowScreen = 0;
176 Colormap Draw_WindowColorMap;
177 Standard_Boolean Draw_BlackBackGround = Standard_True;
180 // Initialization of static variables of Draw_Window
181 //======================================================
182 Draw_Window* Draw_Window::firstWindow = NULL;
185 //=======================================================================
186 //function : Draw_Window
188 //=======================================================================
189 Draw_Window::Draw_Window() :
190 base(*new Base_Window()),
195 myUseBuffer(Standard_False),
196 withWindowManager(Standard_True)
198 myMother = RootWindow(Draw_WindowDisplay,
201 if (firstWindow) firstWindow->previous = this;
205 //=======================================================================
206 //function : Draw_Window
208 //=======================================================================
209 Draw_Window::Draw_Window(Window mother) :
210 base(*new Base_Window()),
215 myUseBuffer(Standard_False),
216 withWindowManager(Standard_True)
220 if (firstWindow) firstWindow->previous = this;
224 //=======================================================================
225 //function : Draw_Window
227 //=======================================================================
228 Draw_Window::Draw_Window (const char* title,
229 Standard_Integer X, Standard_Integer Y,
230 Standard_Integer DX, Standard_Integer DY) :
231 base(*new Base_Window()),
236 myUseBuffer(Standard_False),
237 withWindowManager(Standard_True)
239 myMother = RootWindow(Draw_WindowDisplay,
242 if (firstWindow) firstWindow->previous = this;
248 //=======================================================================
249 //function : Draw_Window
251 //=======================================================================
252 Draw_Window::Draw_Window (const char* window ) :
253 base(*new Base_Window()),
258 myUseBuffer(Standard_False),
259 withWindowManager(Standard_True)
261 sscanf(window,"%lx",&win);
262 Standard_Integer X,Y,DX,DY;
264 if (firstWindow) firstWindow->previous = this;
273 //=======================================================================
274 //function : Draw_Window
276 //=======================================================================
277 Draw_Window::Draw_Window (Window mother,
279 Standard_Integer X, Standard_Integer Y,
280 Standard_Integer DX, Standard_Integer DY) :
281 base(*new Base_Window()),
286 myUseBuffer(Standard_False),
287 withWindowManager(Standard_True)
291 if (firstWindow) firstWindow->previous = this;
297 //=======================================================================
298 //function : ~Draw_Window
300 //=======================================================================
301 Draw_Window::~Draw_Window()
304 previous->next = next;
308 next->previous = previous;
312 XFreePixmap(Draw_WindowDisplay, myBuffer);
315 // Liberation pointer on Base_Window
319 //=======================================================================
322 //=======================================================================
323 void Draw_Window::Init(Standard_Integer X, Standard_Integer Y,
324 Standard_Integer DX, Standard_Integer DY)
326 unsigned long setmask;
328 if (Draw_BlackBackGround)
330 base.xswa.background_pixel = BlackPixel(Draw_WindowDisplay,Draw_WindowScreen);
331 base.xswa.border_pixel = WhitePixel(Draw_WindowDisplay,Draw_WindowScreen);
335 base.xswa.background_pixel = WhitePixel(Draw_WindowDisplay,Draw_WindowScreen);
336 base.xswa.border_pixel = BlackPixel(Draw_WindowDisplay,Draw_WindowScreen);
338 base.xswa.colormap = Draw_WindowColorMap;
339 setmask = CWBackPixel | CWBorderPixel ;
342 myHints.flags = USPosition;
348 win = XCreateWindow(Draw_WindowDisplay,
351 (unsigned int) DX,(unsigned int) DY,
353 DefaultDepth(Draw_WindowDisplay,Draw_WindowScreen),
355 DefaultVisual(Draw_WindowDisplay,Draw_WindowScreen),
357 XSelectInput(Draw_WindowDisplay, win, ButtonPressMask|ExposureMask|
358 StructureNotifyMask);
360 // advise to the window manager to place it where I need
361 XSetWMNormalHints(Draw_WindowDisplay,win,&myHints);
363 if (Draw_VirtualWindows)
365 myUseBuffer = Standard_True;
370 base.gc = XCreateGC(Draw_WindowDisplay, win, 0, NULL);
372 XSetPlaneMask(Draw_WindowDisplay,base.gc,AllPlanes);
373 XSetForeground(Draw_WindowDisplay,
374 base.gc, WhitePixel(Draw_WindowDisplay,Draw_WindowScreen));
375 XSetBackground(Draw_WindowDisplay,
376 base.gc, BlackPixel(Draw_WindowDisplay,Draw_WindowScreen));
377 // save in case of window recovery
379 base.xswa.backing_store = Always;
380 XChangeWindowAttributes(Draw_WindowDisplay, win,
381 CWBackingStore, &base.xswa);
383 XSetLineAttributes (Draw_WindowDisplay, base.gc,
384 0, LineSolid, CapButt, JoinMiter);
387 //=======================================================================
388 //function : InitBuffer
390 //=======================================================================
391 void Draw_Window::InitBuffer()
395 XFreePixmap (Draw_WindowDisplay, myBuffer);
397 XWindowAttributes winAttr;
398 XGetWindowAttributes (Draw_WindowDisplay, win, &winAttr);
399 myBuffer = XCreatePixmap (Draw_WindowDisplay, win, winAttr.width, winAttr.height, winAttr.depth);
401 else if (myBuffer != 0)
403 XFreePixmap (Draw_WindowDisplay, myBuffer);
408 //=======================================================================
409 //function : StopWinManager
411 //=======================================================================
412 void Draw_Window::StopWinManager()
415 XWindowAttributes winAttr;
416 XGetWindowAttributes (Draw_WindowDisplay, win, &winAttr);
420 myHints.flags = USPosition;
421 myHints.x = (int) 30;
422 myHints.y = (int) 100;
424 base.xswa.override_redirect = 1;
425 base.xswa.border_pixel = BlackPixel(Draw_WindowDisplay,
427 base.xswa.background_pixel = WhitePixel(Draw_WindowDisplay,
430 withWindowManager = Standard_False;
432 win = XCreateWindow(Draw_WindowDisplay, myMother,
433 winAttr.x, winAttr.y,
434 winAttr.width, winAttr.height,
436 CopyFromParent, InputOutput, CopyFromParent,
437 CWBorderPixel|CWOverrideRedirect|CWBackPixel, &base.xswa);
440 // adwise to the window manager to place it where I wish
441 XSetWMNormalHints(Draw_WindowDisplay,win,&myHints);
443 // all masks of the old window are reassigned to the new one.
444 XSelectInput(Draw_WindowDisplay,win,winAttr.your_event_mask);
447 //=======================================================================
448 //function : SetPosition
450 //=======================================================================
451 void Draw_Window::SetPosition(Standard_Integer NewXpos,
452 Standard_Integer NewYpos)
454 Standard_Integer x,y;
457 if ( (x != NewXpos) || (y != NewYpos) )
458 XMoveWindow(Draw_WindowDisplay, win, NewXpos, NewYpos);
461 //=======================================================================
462 //function : SetDimension
464 //=======================================================================
465 void Draw_Window::SetDimension(Standard_Integer NewDx,
466 Standard_Integer NewDy)
468 if ( (NewDx != WidthWin() ) || (NewDy != HeightWin() ) )
469 XResizeWindow(Draw_WindowDisplay, win, NewDx, NewDy);
472 //=======================================================================
473 //function : GetPosition
475 //=======================================================================
476 void Draw_Window::GetPosition(Standard_Integer &PosX,
477 Standard_Integer &PosY)
479 XWindowAttributes winAttr;
480 XGetWindowAttributes(Draw_WindowDisplay, win, &winAttr);
486 //=======================================================================
487 //function : HeightWin
489 //=======================================================================
490 Standard_Integer Draw_Window::HeightWin() const
493 XWindowAttributes winAttr;
494 XGetWindowAttributes(Draw_WindowDisplay, win, &winAttr);
500 //=======================================================================
501 //function : WidthWin
503 //=======================================================================
504 Standard_Integer Draw_Window::WidthWin() const
507 XWindowAttributes winAttr;
508 XGetWindowAttributes(Draw_WindowDisplay, win, &winAttr);
514 //=======================================================================
515 //function : SetTitle
517 //=======================================================================
518 void Draw_Window::SetTitle(const char* title)
520 XStoreName(Draw_WindowDisplay, win, title);
523 //=======================================================================
524 //function : GetTitle
526 //=======================================================================
527 char* Draw_Window::GetTitle()
530 XFetchName(Draw_WindowDisplay, win, &title);
534 //=======================================================================
535 //function : GetDrawable
537 //=======================================================================
538 Drawable Draw_Window::GetDrawable() const
540 return myUseBuffer ? myBuffer : win;
543 //=======================================================================
544 //function :DefineColor
546 //=======================================================================
547 Standard_Boolean Draw_Window::DefineColor(const Standard_Integer i, const char* colorName)
551 if (!XParseColor(Draw_WindowDisplay,Draw_WindowColorMap,colorName,&color))
552 return Standard_False;
553 if (!XAllocColor(Draw_WindowDisplay,Draw_WindowColorMap,&color))
554 return Standard_False;
555 thePixels[i % MAXCOLOR] = color.pixel;
556 return Standard_True;
559 //=======================================================================
560 //function : DisplayWindow
562 //=======================================================================
563 void Draw_Window::DisplayWindow()
565 if (Draw_VirtualWindows)
571 XMapRaised(Draw_WindowDisplay, win);
573 XFlush(Draw_WindowDisplay);
576 //=======================================================================
579 //=======================================================================
580 void Draw_Window::Hide()
582 XUnmapWindow(Draw_WindowDisplay, win);
585 //=======================================================================
588 //=======================================================================
589 void Draw_Window::Destroy()
591 XDestroyWindow(Draw_WindowDisplay, win);
595 XFreePixmap(Draw_WindowDisplay, myBuffer);
600 //=======================================================================
603 //=======================================================================
604 void Draw_Window::Clear()
608 // XClearArea only applicable for windows
609 XGCValues currValues;
610 XGetGCValues(Draw_WindowDisplay, base.gc, GCBackground | GCForeground, &currValues);
611 XSetForeground(Draw_WindowDisplay, base.gc, currValues.background);
612 XFillRectangle(Draw_WindowDisplay, myBuffer, base.gc, 0, 0, WidthWin(), HeightWin());
613 XSetForeground(Draw_WindowDisplay, base.gc, currValues.foreground);
617 XClearArea(Draw_WindowDisplay, win, 0, 0, 0, 0, False);
621 //=======================================================================
624 //=======================================================================
625 void Draw_Window::Flush()
627 XFlush(Draw_WindowDisplay);
630 //=======================================================================
631 //function : DrawString
633 //=======================================================================
634 void Draw_Window::DrawString(int X, int Y, char *text)
636 XDrawString(Draw_WindowDisplay, GetDrawable(), base.gc, X, Y, text, strlen(text));
639 //=======================================================================
640 //function : DrawSegments
642 //=======================================================================
643 void Draw_Window::DrawSegments(Segment *tab, int nbElem)
645 XDrawSegments(Draw_WindowDisplay, GetDrawable(), base.gc, (XSegment*) tab, nbElem);
648 //=======================================================================
651 //=======================================================================
652 void Draw_Window::Redraw()
655 XCopyArea (Draw_WindowDisplay,
656 myBuffer, win, // source, destination Drawables
659 WidthWin(), HeightWin(),
660 0, 0); // destination x, y
664 //=======================================================================
665 //function : SetColor
667 //=======================================================================
668 void Draw_Window::SetColor(Standard_Integer color)
670 XSetForeground(Draw_WindowDisplay, base.gc, thePixels[color]);
673 //=======================================================================
676 //=======================================================================
677 void Draw_Window::SetMode( int mode)
679 XSetFunction(Draw_WindowDisplay, base.gc, mode);
682 //=======================================================================
685 //=======================================================================
686 Standard_Boolean Draw_Window::Save (const char* theFileName) const
688 // make sure all draw operations done
689 XSync (Draw_WindowDisplay, True);
692 XWindowAttributes winAttr;
693 XGetWindowAttributes (Draw_WindowDisplay, win, &winAttr);
697 // make sure that the whole window fit on display to prevent BadMatch error
698 XWindowAttributes winAttrRoot;
699 XGetWindowAttributes (Draw_WindowDisplay, XRootWindowOfScreen (winAttr.screen), &winAttrRoot);
701 Window winChildDummy;
704 XTranslateCoordinates (Draw_WindowDisplay, win, XRootWindowOfScreen (winAttr.screen),
705 0, 0, &winLeft, &winTop, &winChildDummy);
707 if (((winLeft + winAttr.width) > winAttrRoot.width) || winLeft < winAttrRoot.x ||
708 ((winTop + winAttr.height) > winAttrRoot.height) || winTop < winAttrRoot.y)
710 std::cerr << "The window not fully visible! Can't create the snapshot.\n";
711 return Standard_False;
716 if (XMatchVisualInfo (Draw_WindowDisplay, Draw_WindowScreen, 32, TrueColor, &aVInfo) == 0
717 && XMatchVisualInfo (Draw_WindowDisplay, Draw_WindowScreen, 24, TrueColor, &aVInfo) == 0)
719 std::cerr << "24-bit TrueColor visual is not supported by server!\n";
720 return Standard_False;
723 Image_AlienPixMap anImage;
724 bool isBigEndian = Image_PixMap::IsBigEndianHost();
725 const Standard_Size aSizeRowBytes = Standard_Size(winAttr.width) * 4;
726 if (!anImage.InitTrash (isBigEndian ? Image_PixMap::ImgRGB32 : Image_PixMap::ImgBGR32,
727 Standard_Size(winAttr.width), Standard_Size(winAttr.height), aSizeRowBytes))
729 return Standard_False;
731 anImage.SetTopDown (true);
733 XImage* anXImage = XCreateImage (Draw_WindowDisplay, aVInfo.visual,
734 32, ZPixmap, 0, (char* )anImage.ChangeData(), winAttr.width, winAttr.height, 32, int(aSizeRowBytes));
735 anXImage->bitmap_bit_order = anXImage->byte_order = (isBigEndian ? MSBFirst : LSBFirst);
736 if (XGetSubImage (Draw_WindowDisplay, GetDrawable(),
737 0, 0, winAttr.width, winAttr.height,
738 AllPlanes, ZPixmap, anXImage, 0, 0) == NULL)
740 anXImage->data = NULL;
741 XDestroyImage (anXImage);
742 return Standard_False;
746 anXImage->data = NULL;
747 XDestroyImage (anXImage);
750 return anImage.Save (theFileName);
753 //=======================================================================
754 //function : ProcessEvent
756 //=======================================================================
758 void ProcessEvent(Draw_Window& win, XEvent& xev)
760 Standard_Integer X,Y,button,lenk;
776 button = xev.xbutton.button;
777 win.WButtonPress(X,Y,button);
783 button = xev.xbutton.button;
784 win.WButtonRelease(X,Y,button);
788 lenk = XLookupString(&(xev.xkey),
798 //WKeyPress(c,keysym);
804 win.WMotionNotify(X,Y);
807 case ConfigureNotify :
808 if (win.withWindowManager)
809 win.WConfigureNotify(xev.xconfigure.x, xev.xconfigure.y,
810 xev.xconfigure.width,
811 xev.xconfigure.height);
821 //=======================================================================
824 //=======================================================================
825 void Draw_Window::WExpose()
829 //=======================================================================
830 //function : WButtonPress
832 //=======================================================================
833 void Draw_Window::WButtonPress(const Standard_Integer,
834 const Standard_Integer,
835 const Standard_Integer&)
839 //=======================================================================
840 //function : WButtonRelease
842 //=======================================================================
843 void Draw_Window::WButtonRelease(const Standard_Integer,
844 const Standard_Integer,
845 const Standard_Integer&)
849 /**************************
850 //=======================================================================
851 //function : WKeyPress
853 //=======================================================================
855 void Draw_Window::WKeyPress(char, KeySym&)
858 ***************************/
860 //=======================================================================
861 //function : WMotionNotify
863 //=======================================================================
864 void Draw_Window::WMotionNotify(const Standard_Integer ,
865 const Standard_Integer )
869 //=======================================================================
870 //function : WConfigureNotify
872 //=======================================================================
874 void Draw_Window::WConfigureNotify(const Standard_Integer,
875 const Standard_Integer,
876 const Standard_Integer,
877 const Standard_Integer)
881 //=======================================================================
884 //=======================================================================
886 void Draw_Window::Wait (Standard_Boolean wait)
890 XSelectInput(Draw_WindowDisplay,win,
891 ButtonPressMask|ExposureMask | StructureNotifyMask |
895 XSelectInput(Draw_WindowDisplay,win,
896 ButtonPressMask|ExposureMask | StructureNotifyMask);
900 //=======================================================================
901 //function : WUnmapNotify
903 //=======================================================================
905 void Draw_Window::WUnmapNotify()
910 //======================================================
911 // funtion : ProcessEvents
912 // purpose : process pending X events
913 //======================================================
915 static void ProcessEvents(ClientData,int)
919 while (XPending(Draw_WindowDisplay)) {
924 XNextEvent(Draw_WindowDisplay,&xev);
926 /* search the window in the window list */
927 Draw_Window* w = Draw_Window::firstWindow;
928 Standard_Integer found=0;
930 if (xev.xany.window == w->win) {
931 ProcessEvent(*w, xev);
938 Tk_HandleEvent(&xev);
943 //======================================================
944 // funtion :Run_Appli
946 //======================================================
949 static Standard_Boolean(*Interprete) (const char*);
951 void Run_Appli(Standard_Boolean (*interprete) (const char*))
953 Tcl_Channel outChannel, inChannel ;
954 Interprete = interprete;
959 * Commands will come from standard input, so set up an event
960 * handler for standard input. If the input device is aEvaluate the
961 * .rc file, if one has been specified, set up an event handler
962 * for standard input, and print a prompt if the input
963 * device is a terminal.
965 inChannel = Tcl_GetStdChannel(TCL_STDIN);
967 Tcl_CreateChannelHandler(inChannel, TCL_READABLE, StdinProc,
968 (ClientData) inChannel);
971 // Create a handler for the draw display
973 // Adding of the casting into void* to be able to compile on AO1
974 // ConnectionNumber(Draw_WindowDisplay) is an int 32 bits
975 // (void*) is a pointer 64 bits ???????
977 #if TCL_MAJOR_VERSION < 8
978 Tk_CreateFileHandler((void*) ConnectionNumber(Draw_WindowDisplay),
979 TK_READABLE, ProcessEvents,(ClientData) 0 );
981 Tk_CreateFileHandler(ConnectionNumber(Draw_WindowDisplay),
982 TK_READABLE, ProcessEvents,(ClientData) 0 );
987 if (tty) Prompt(theCommands.Interp(), 0);
988 Prompt(theCommands.Interp(), 0);
990 outChannel = Tcl_GetStdChannel(TCL_STDOUT);
992 Tcl_Flush(outChannel);
994 Tcl_DStringInit(&command);
997 * Loop infinitely, waiting for commands to execute. When there
998 * are no windows left, Tk_MainLoop returns and we exit.
1003 if (Draw_VirtualWindows) {
1004 // main window will never shown
1005 // but main loop will parse all Xlib messages
1006 Tcl_Eval(theCommands.Interp(), "wm withdraw .");
1013 Standard_Integer count = ConnectionNumber(Draw_WindowDisplay);
1014 Standard_Integer numfd;
1018 FD_SET(count,&readset);
1020 numfd = select(count+1,(Integer*)&readset,NULL,NULL,NULL);
1022 numfd = select(count+1,&readset,NULL,NULL,NULL);
1024 if (FD_ISSET(0,&readset)) StdinProc((ClientData)0,0);
1025 if (FD_ISSET(count,&readset)) ProcessEvents((ClientData)0,0);
1031 //======================================================
1032 // funtion : Init_Appli()
1034 //======================================================
1035 Standard_Boolean Init_Appli()
1038 interp = theCommands.Interp();
1044 } catch (Standard_Failure) {
1045 cout <<" Pb au lancement de TK_Init "<<endl;
1048 Tcl_StaticPackage(interp, "Tk", Tk_Init, (Tcl_PackageInitProc *) NULL);
1051 Tk_MainWindow(interp) ;
1052 if (mainWindow == NULL) {
1053 fprintf(stderr, "%s\n", interp->result);
1056 Tk_Name(mainWindow) =
1057 Tk_GetUid(Tk_SetAppName(mainWindow,
1060 Tk_GeometryRequest(mainWindow, 200, 200);
1062 if (Draw_WindowDisplay == NULL) {
1063 Draw_WindowDisplay = XOpenDisplay(NULL);
1064 // Replaced Tk_Display(mainWindow) with XOpenDisplay; On Mac OS X Tk_Display
1065 // returns a pointer to Display structure defined in system Tcl/Tk libraries.
1066 // This structure differs from structure defined in X11 library and
1067 // this caused DRAWEXE crash on startup.
1069 if (Draw_WindowDisplay == NULL) {
1070 cout << "Cannot open display : "<<XDisplayName(NULL)<<endl;
1071 cout << "Interpret commands in batch mode."<<endl;
1072 return Standard_False;
1075 // synchronize the display server : could be done within Tk_Init
1077 XSynchronize(Draw_WindowDisplay, True);
1078 XSetInputFocus(Draw_WindowDisplay,
1080 RevertToPointerRoot,
1083 Draw_WindowScreen = DefaultScreen(Draw_WindowDisplay);
1084 Draw_WindowColorMap = DefaultColormap(Draw_WindowDisplay,
1087 Tcl_SetVar(interp,"tcl_interactive",(char*)(tty ? "1" : "0"), TCL_GLOBAL_ONLY);
1088 // Tcl_SetVar(interp,"tcl_interactive",tty ? "1" : "0", TCL_GLOBAL_ONLY);
1089 return Standard_True;
1092 //======================================================
1093 // funtion : Destroy_Appli()
1095 //======================================================
1096 void Destroy_Appli()
1098 //XCloseDisplay(Draw_WindowDisplay);
1101 //======================================================
1102 // funtion : GetNextEvent()
1104 //======================================================
1105 void GetNextEvent(Event& ev)
1108 XNextEvent(Draw_WindowDisplay, &xev);
1113 ev.window = xev.xbutton.window;
1114 ev.button = xev.xbutton.button;
1115 ev.x = xev.xbutton.x;
1116 ev.y = xev.xbutton.y;
1121 ev.window = xev.xmotion.window;
1123 ev.x = xev.xmotion.x;
1124 ev.y = xev.xmotion.y;
1130 *----------------------------------------------------------------------
1134 * This procedure is invoked by the event dispatcher whenever
1135 * standard input becomes readable. It grabs the next line of
1136 * input characters, adds them to a command being assembled, and
1137 * executes the command if it's complete.
1143 * Could be almost arbitrary, depending on the command that's
1146 *----------------------------------------------------------------------
1150 //static void StdinProc(ClientData clientData, int mask)
1151 static void StdinProc(ClientData clientData, int )
1153 static int gotPartial = 0;
1157 Tcl_Channel chan = (Tcl_Channel) clientData;
1159 // MSV Nov 2, 2001: patch for TCL 8.3: initialize line to avoid exception
1160 // when first user input is an empty string
1161 Tcl_DStringFree(&line);
1162 count = Tcl_Gets(chan, &line);
1165 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)))
1166 Tcl_DString linetmp;
1167 Tcl_DStringInit(&linetmp);
1168 Tcl_UniChar * UniCharString;
1169 UniCharString = Tcl_UtfToUniCharDString(Tcl_DStringValue(&line),-1,&linetmp);
1170 Standard_Integer l = Tcl_UniCharLen(UniCharString);
1171 TCollection_AsciiString AsciiString("");
1172 Standard_Character Character;
1174 for (i=0; i<l; i++) {
1175 Character = UniCharString[i];
1176 AsciiString.AssignCat(Character);
1178 Tcl_DStringInit(&line);
1179 Tcl_DStringAppend(&line, AsciiString.ToCString(), -1);
1186 Tcl_DeleteChannelHandler(chan, StdinProc, (ClientData) chan);
1194 (void) Tcl_DStringAppend(&command, Tcl_DStringValue(&line), -1);
1195 cmd = Tcl_DStringAppend(&command, "\n", -1);
1196 Tcl_DStringFree(&line);
1199 if (!Tcl_CommandComplete(cmd)) {
1206 * Disable the stdin channel handler while evaluating the command;
1207 * otherwise if the command re-enters the event loop we might
1208 * process commands from stdin before the current command is
1209 * finished. Among other things, this will trash the text of the
1210 * command being evaluated.
1213 Tcl_CreateChannelHandler(chan, 0, StdinProc, (ClientData) chan);
1217 * Disable the stdin file handler while evaluating the command;
1218 * otherwise if the command re-enters the event loop we might
1219 * process commands from stdin before the current command is
1220 * finished. Among other things, this will trash the text of the
1221 * command being evaluated.
1225 // Tk_CreateFileHandler(0, 0, StdinProc, (ClientData) 0);
1228 // xab average to avoid an output SIGBUS of DRAW
1229 // to ultimately prescise or remove once
1230 // the problem of free on the global variable at the average
1237 Tcl_CreateChannelHandler(chan, TCL_READABLE, StdinProc,
1239 Tcl_DStringFree(&command);
1246 if (tty) Prompt(interp, gotPartial);
1248 } catch (Standard_Failure) {}
1254 // Source Specifique WNT
1256 /****************************************************\
1259 \****************************************************/
1261 #include "Draw_Window.hxx"
1262 #include "DrawRessource.h"
1265 #include <Draw_Appli.hxx>
1272 // Position of information in the extra memory
1274 // indicates SUBSYSTEM:CONSOLE linker option, to be set to True in main()
1276 Standard_Boolean Draw_IsConsoleSubsystem = Standard_False;
1279 Standard_Boolean Draw_BlackBackGround = Standard_True;
1281 // Creation of color stylos
1282 HPEN colorPenTab[MAXCOLOR] = {CreatePen(PS_SOLID, PENWIDTH, RGB(255,255,255)),
1283 CreatePen(PS_SOLID, PENWIDTH, RGB(255,0,0)),
1284 CreatePen(PS_SOLID, PENWIDTH, RGB(0,255,0)),
1285 CreatePen(PS_SOLID, PENWIDTH, RGB(0,0,255)),
1286 CreatePen(PS_SOLID, PENWIDTH, RGB(0,255,255)),
1287 CreatePen(PS_SOLID, PENWIDTH, RGB(255,215,0)),
1288 CreatePen(PS_SOLID, PENWIDTH, RGB(255,0,255)),
1289 CreatePen(PS_SOLID, PENWIDTH, RGB(255,52,179)),
1290 CreatePen(PS_SOLID, PENWIDTH, RGB(255,165,0)),
1291 CreatePen(PS_SOLID, PENWIDTH, RGB(255,228,225)),
1292 CreatePen(PS_SOLID, PENWIDTH, RGB(255,160,122)),
1293 CreatePen(PS_SOLID, PENWIDTH, RGB(199,21,133)),
1294 CreatePen(PS_SOLID, PENWIDTH, RGB(255,255,0)),
1295 CreatePen(PS_SOLID, PENWIDTH, RGB(240,230,140)),
1296 CreatePen(PS_SOLID, PENWIDTH, RGB(255,127,80))};
1298 // Correspondance mode X11 and WINDOWS NT
1299 int modeTab[16] = {R2_BLACK, R2_MASKPEN, R2_MASKPENNOT, R2_COPYPEN,
1300 R2_MASKNOTPEN, R2_NOP, R2_XORPEN, R2_MERGEPEN,
1301 R2_NOTMASKPEN, R2_NOTXORPEN, R2_NOT, R2_MERGEPENNOT,
1302 R2_NOTCOPYPEN, R2_MERGENOTPEN, R2_NOTMERGEPEN, R2_WHITE};
1304 /*--------------------------------------------------------*\
1305 | CREATE DRAW WINDOW PROCEDURE
1306 \*--------------------------------------------------------*/
1307 HWND DrawWindow::CreateDrawWindow(HWND hWndClient, int nitem)
1309 if (Draw_IsConsoleSubsystem) {
1310 HWND aWin = CreateWindow (DRAWCLASS, DRAWTITLE,
1311 WS_OVERLAPPEDWINDOW,
1313 NULL, NULL,::GetModuleHandle(NULL), NULL);
1314 if (!Draw_VirtualWindows)
1316 SetWindowPos(aWin, HWND_TOPMOST, 1,1,1,1, SWP_NOMOVE);
1317 SetWindowPos(aWin, HWND_NOTOPMOST, 1,1,1,1, SWP_NOMOVE);
1324 hInstance = (HANDLE)GetWindowLong(hWndClient,GWL_HINSTANCE);
1326 hInstance = (HANDLE)GetWindowLong(hWndClient,GWLP_HINSTANCE);
1329 return CreateMDIWindow(DRAWCLASS, DRAWTITLE,
1330 WS_CAPTION | WS_CHILD | WS_THICKFRAME,
1332 hWndClient, (HINSTANCE)hInstance, nitem);
1337 /*--------------------------------------------------------*\
1338 | DRAW WINDOW PROCEDURE
1339 \*--------------------------------------------------------*/
1340 LONG APIENTRY DrawWindow::DrawProc(HWND hWnd, UINT wMsg, WPARAM wParam, LONG lParam )
1342 DrawWindow* localObjet = (DrawWindow*)GetWindowLong(hWnd, CLIENTWND);
1345 if (Draw_IsConsoleSubsystem)
1346 return (DefWindowProc(hWnd, wMsg, wParam, lParam));
1348 return(DefMDIChildProc(hWnd, wMsg, wParam, lParam));
1356 BeginPaint(hWnd, &ps);
1357 if (localObjet->GetUseBuffer())
1358 localObjet->Redraw();
1360 localObjet->WExpose();
1361 EndPaint(hWnd, &ps);
1366 if (localObjet->GetUseBuffer()) {
1367 localObjet->InitBuffer();
1368 localObjet->WExpose();
1369 localObjet->Redraw();
1375 if (Draw_IsConsoleSubsystem)
1376 return (DefWindowProc(hWnd, wMsg, wParam, lParam));
1378 return(DefMDIChildProc(hWnd, wMsg, wParam, lParam));
1386 ** IMPLEMENTATION of the CLASS DRAWWINDOW
1389 /*--------------------------------------------------------*\
1390 | Initialization of static variables of DrawWindow
1391 \*--------------------------------------------------------*/
1393 DrawWindow* DrawWindow::firstWindow = NULL;
1394 HWND DrawWindow::hWndClientMDI = 0;
1396 /*--------------------------------------------------------*\
1397 | Constructors of Draw_Window
1398 \*--------------------------------------------------------*/
1400 // Default Constructor
1401 //________________________
1402 DrawWindow::DrawWindow() :
1407 myUseBuffer(Standard_False)
1409 if (firstWindow) firstWindow->previous = this;
1413 //________________________
1414 DrawWindow::DrawWindow(char* title,
1415 Standard_Integer X, Standard_Integer Y,
1416 Standard_Integer dX,Standard_Integer dY) :
1417 win(0), next(firstWindow), previous(NULL), myMemHbm(NULL), myUseBuffer(Standard_False)
1419 if (firstWindow) firstWindow->previous = this;
1424 DrawWindow::DrawWindow(char* title,
1425 Standard_Integer X, Standard_Integer Y,
1426 Standard_Integer dX,Standard_Integer dY,
1428 win(theWin),next(firstWindow), previous(NULL), myMemHbm(NULL), myUseBuffer(Standard_False)
1430 if (firstWindow) firstWindow->previous = this;
1438 /*--------------------------------------------------------*\
1439 | Destructor of DrawWindow
1440 \*--------------------------------------------------------*/
1441 DrawWindow::~DrawWindow()
1444 previous->next = next;
1448 next->previous = previous;
1450 // Delete 'off-screen drawing'-related objects
1452 DeleteObject(myMemHbm);
1459 /*--------------------------------------------------------*\
1461 \*--------------------------------------------------------*/
1462 void DrawWindow::Init(Standard_Integer theXLeft, Standard_Integer theYTop,
1463 Standard_Integer theWidth, Standard_Integer theHeight)
1467 win = CreateDrawWindow(hWndClientMDI, 0);
1470 // include decorations in the window dimensions
1471 // to reproduce same behaviour of Xlib window.
1472 DWORD aWinStyle = GetWindowLongPtr (win, GWL_STYLE);
1473 DWORD aWinStyleEx = GetWindowLongPtr (win, GWL_EXSTYLE);
1474 HMENU aMenu = GetMenu (win);
1477 aRect.top = theYTop;
1478 aRect.bottom = theYTop + theHeight;
1479 aRect.left = theXLeft;
1480 aRect.right = theXLeft + theWidth;
1481 AdjustWindowRectEx (&aRect, aWinStyle, aMenu != NULL ? TRUE : FALSE, aWinStyleEx);
1483 SetPosition (aRect.left, aRect.top);
1484 SetDimension (aRect.right - aRect.left, aRect.bottom - aRect.top);
1485 // Save the pointer at the instance associated to the window
1486 SetWindowLong(win, CLIENTWND, (LONG)this);
1487 HDC hDC = GetDC(win);
1488 SetBkColor(hDC, RGB(0, 0, 0));
1491 SelectObject(hDC, colorPenTab[myCurrPen]); // Default pencil
1492 SelectObject(hDC, GetStockObject(BLACK_BRUSH));
1493 SetTextColor(hDC, RGB(0,0,255));
1494 ReleaseDC(win, hDC);
1496 if (Draw_VirtualWindows)
1498 // create a virtual window
1499 SetUseBuffer (Standard_True);
1503 /*--------------------------------------------------------*\
1505 \*--------------------------------------------------------*/
1506 void DrawWindow::SetUseBuffer(Standard_Boolean use)
1512 /*--------------------------------------------------------*\
1514 \*--------------------------------------------------------*/
1515 void DrawWindow::InitBuffer()
1519 HDC hDC = GetDC(win);
1520 GetClientRect(win, &rc);
1523 GetObject(myMemHbm, sizeof(BITMAP), &aBmp);
1524 if (rc.right-rc.left == aBmp.bmWidth && rc.bottom-rc.top == aBmp.bmHeight) return;
1525 DeleteObject(myMemHbm);
1527 myMemHbm = (HBITMAP)CreateCompatibleBitmap(hDC,
1530 HDC aMemDC = GetMemDC(hDC);
1531 FillRect(aMemDC, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH));
1532 ReleaseMemDC(aMemDC);
1533 ReleaseDC(win, hDC);
1537 DeleteObject(myMemHbm);
1543 /*--------------------------------------------------------*\
1545 \*--------------------------------------------------------*/
1546 HDC DrawWindow::GetMemDC(HDC theWinDC)
1548 if (!myUseBuffer) return NULL;
1550 HDC aWorkDC = CreateCompatibleDC(theWinDC);
1551 myOldHbm = (HBITMAP)SelectObject(aWorkDC, myMemHbm);
1552 SetROP2(aWorkDC, modeTab[myCurrMode]);
1553 SelectObject(aWorkDC, colorPenTab[myCurrPen]);
1554 SetBkColor(aWorkDC, RGB(0, 0, 0));
1555 SelectObject(aWorkDC, GetStockObject(BLACK_BRUSH));
1556 SetTextColor(aWorkDC, RGB(0,0,255));
1561 /*--------------------------------------------------------*\
1563 \*--------------------------------------------------------*/
1564 void DrawWindow::ReleaseMemDC(HDC theMemDC)
1566 if (!myUseBuffer || !theMemDC) return;
1568 if (myOldHbm) SelectObject(theMemDC, myOldHbm);
1573 /*--------------------------------------------------------*\
1575 \*--------------------------------------------------------*/
1576 void DrawWindow::SetPosition(Standard_Integer posX, Standard_Integer posY)
1578 SetWindowPos(win, 0,
1581 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
1585 /*--------------------------------------------------------*\
1587 \*--------------------------------------------------------*/
1588 void DrawWindow::SetDimension(Standard_Integer dimX, Standard_Integer dimY)
1590 SetWindowPos(win, 0,
1593 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
1597 /*--------------------------------------------------------*\
1599 \*--------------------------------------------------------*/
1600 void DrawWindow::GetPosition(Standard_Integer &dimX,
1601 Standard_Integer &dimY)
1604 GetWindowRect(win, &rect);
1607 point.x = rect.left;
1610 ScreenToClient(hWndClientMDI, &point);
1616 /*--------------------------------------------------------*\
1618 \*--------------------------------------------------------*/
1619 Standard_Integer DrawWindow::HeightWin() const
1622 GetClientRect(win, &rect);
1623 return(rect.bottom-rect.top);
1627 /*--------------------------------------------------------*\
1629 \*--------------------------------------------------------*/
1630 Standard_Integer DrawWindow::WidthWin() const
1633 GetClientRect(win, &rect);
1634 return(rect.right-rect.left);
1638 /*--------------------------------------------------------*\
1640 \*--------------------------------------------------------*/
1641 void DrawWindow::SetTitle(char* title)
1643 SetWindowText(win, title);
1647 /*--------------------------------------------------------*\
1649 | Attention do not forget to unallocate the memory
1650 \*--------------------------------------------------------*/
1651 char* DrawWindow::GetTitle()
1653 char* title=new char[31];
1654 GetWindowText(win, title, 30);
1659 /*--------------------------------------------------------*\
1661 \*--------------------------------------------------------*/
1662 void DrawWindow::DisplayWindow()
1664 if (Draw_VirtualWindows)
1668 ShowWindow (win, SW_SHOW);
1673 /*--------------------------------------------------------*\
1675 \*--------------------------------------------------------*/
1676 void DrawWindow::Hide()
1678 ShowWindow(win, SW_HIDE);
1682 /*--------------------------------------------------------*\
1684 \*--------------------------------------------------------*/
1685 void DrawWindow::Destroy()
1692 /*--------------------------------------------------------*\
1694 \*--------------------------------------------------------*/
1695 void DrawWindow::Clear()
1697 HDC hDC = GetDC(win);
1698 HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
1700 int debug = GetROP2(aWorkDC);
1702 SelectObject(aWorkDC,GetStockObject(BLACK_PEN));
1703 Rectangle(aWorkDC, 0, 0, WidthWin(), HeightWin());
1704 RestoreDC(aWorkDC,-1);
1706 if (myUseBuffer) ReleaseMemDC(aWorkDC);
1710 /*--------------------------------------------------------*\
1712 \*--------------------------------------------------------*/
1713 static Standard_Boolean SaveBitmap (HBITMAP theHBitmap,
1714 const char* theFileName)
1716 // Get informations about the bitmap
1718 if (GetObject (theHBitmap, sizeof(BITMAP), (LPSTR )&aBitmap) == 0)
1720 return Standard_False;
1723 Image_AlienPixMap anImage;
1724 const Standard_Size aSizeRowBytes = Standard_Size(aBitmap.bmWidth) * 4;
1725 if (!anImage.InitTrash (Image_PixMap::ImgBGR32, Standard_Size(aBitmap.bmWidth), Standard_Size(aBitmap.bmHeight), aSizeRowBytes))
1727 return Standard_False;
1729 anImage.SetTopDown (false);
1732 BITMAPINFOHEADER aBitmapInfo;
1733 memset (&aBitmapInfo, 0, sizeof(BITMAPINFOHEADER));
1734 aBitmapInfo.biSize = sizeof(BITMAPINFOHEADER);
1735 aBitmapInfo.biWidth = aBitmap.bmWidth;
1736 aBitmapInfo.biHeight = aBitmap.bmHeight; // positive means bottom-up!
1737 aBitmapInfo.biPlanes = 1;
1738 aBitmapInfo.biBitCount = 32; // use 32bit for automatic word-alignment per row
1739 aBitmapInfo.biCompression = BI_RGB;
1742 HDC aDC = GetDC (NULL);
1743 Standard_Boolean isSuccess = GetDIBits (aDC, theHBitmap,
1744 0, // first scan line to set
1745 aBitmap.bmHeight, // number of scan lines to copy
1746 anImage.ChangeData(), // array for bitmap bits
1747 (LPBITMAPINFO )&aBitmapInfo, // bitmap data info
1748 DIB_RGB_COLORS) != 0;
1749 ReleaseDC (NULL, aDC);
1750 return isSuccess && anImage.Save (theFileName);
1753 /*--------------------------------------------------------*\
1755 \*--------------------------------------------------------*/
1756 Standard_Boolean DrawWindow::Save (const char* theFileName) const
1760 return SaveBitmap (myMemHbm, theFileName);
1764 GetClientRect (win, &aRect);
1765 int aWidth = aRect.right - aRect.left;
1766 int aHeight = aRect.bottom - aRect.top;
1769 HDC aDstDC = GetDC (NULL);
1770 HDC aSrcDC = GetDC (win); // we copy only client area
1771 HDC aMemDC = CreateCompatibleDC (aDstDC);
1773 // Copy the screen to the bitmap
1774 HBITMAP anHBitmapDump = CreateCompatibleBitmap (aDstDC, aWidth, aHeight);
1775 HBITMAP anHBitmapOld = (HBITMAP )SelectObject (aMemDC, anHBitmapDump);
1776 BitBlt (aMemDC, 0, 0, aWidth, aHeight, aSrcDC, 0, 0, SRCCOPY);
1778 Standard_Boolean isSuccess = SaveBitmap (anHBitmapDump, theFileName);
1781 DeleteObject (SelectObject (aMemDC, anHBitmapOld));
1787 /*--------------------------------------------------------*\
1789 \*--------------------------------------------------------*/
1790 void DrawWindow::DrawString(int x,int y, char* text)
1792 HDC hDC = GetDC(win);
1793 HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
1795 TextOut(aWorkDC, x, y, text, strlen(text));
1797 if (myUseBuffer) ReleaseMemDC(aWorkDC);
1801 /*--------------------------------------------------------*\
1803 \*--------------------------------------------------------*/
1804 void DrawWindow::DrawSegments(Segment *tab, int nbElem)
1806 HDC hDC = GetDC(win);
1807 HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
1809 for(int i = 0 ; i < nbElem ; i++)
1811 MoveToEx(aWorkDC, tab[i].x1, tab[i].y1, NULL);
1812 LineTo(aWorkDC, tab[i].x2, tab[i].y2);
1815 if (myUseBuffer) ReleaseMemDC(aWorkDC);
1819 /*--------------------------------------------------------*\
1821 \*--------------------------------------------------------*/
1822 void DrawWindow::Redraw()
1825 HDC hDC = GetDC(win);
1827 GetClientRect(win, &rc);
1828 HDC aMemDC = GetMemDC(hDC);
1831 rc.right-rc.left, rc.bottom-rc.top,
1834 ReleaseMemDC(aMemDC);
1839 /*--------------------------------------------------------*\
1841 \*--------------------------------------------------------*/
1842 void DrawWindow::SetMode(int mode)
1844 HDC hDC = GetDC(win);
1846 SetROP2(hDC, modeTab[mode]);
1851 /*--------------------------------------------------------*\
1853 \*--------------------------------------------------------*/
1854 void DrawWindow::SetColor(Standard_Integer color)
1856 HDC hDC = GetDC(win);
1858 SelectObject(hDC,colorPenTab[color]);
1863 /*--------------------------------------------------------*\
1865 \*--------------------------------------------------------*/
1866 void DrawWindow::WExpose()
1871 /*--------------------------------------------------------*\
1873 \*--------------------------------------------------------*/
1874 void DrawWindow::WButtonPress(const Standard_Integer,
1875 const Standard_Integer,
1876 const Standard_Integer&)
1881 /*--------------------------------------------------------*\
1883 \*--------------------------------------------------------*/
1884 void DrawWindow::WButtonRelease(const Standard_Integer,
1885 const Standard_Integer,
1886 const Standard_Integer&)
1891 /*--------------------------------------------------------*\
1893 \*--------------------------------------------------------*/
1894 void Draw_Window::WMotionNotify(const Standard_Integer ,
1895 const Standard_Integer )
1900 /*--------------------------------------------------------*\
1902 \*--------------------------------------------------------*/
1903 void DrawWindow::WConfigureNotify(const Standard_Integer,
1904 const Standard_Integer,
1905 const Standard_Integer,
1906 const Standard_Integer)
1911 /*--------------------------------------------------------*\
1913 \*--------------------------------------------------------*/
1914 void DrawWindow::WUnmapNotify()
1921 ** IMPLEMENTATION of the CLASS SEGMENT
1924 /*--------------------------------------------------------*\
1926 \*--------------------------------------------------------*/
1928 void Segment::Init(Standard_Integer a1, Standard_Integer a2,
1929 Standard_Integer a3, Standard_Integer a4)
1937 static DWORD WINAPI tkLoop(VOID);
1939 static Tk_Window mainWindow;
1942 //* threads sinchronization *//
1943 DWORD dwMainThreadId;
1944 console_semaphore_value volatile console_semaphore = WAIT_CONSOLE_COMMAND;
1945 //char console_command[1000];
1946 #define COMMAND_SIZE 1000 /* Console Command size */
1947 char console_command[COMMAND_SIZE];
1948 bool volatile isTkLoopStarted = false;
1950 /*--------------------------------------------------------*\
1952 \*--------------------------------------------------------*/
1953 Standard_Boolean Init_Appli(HINSTANCE hInst,
1954 HINSTANCE hPrevInst, int nShow, HWND& hWndFrame )
1958 console_semaphore = STOP_CONSOLE;
1960 interp = theCommands.Interp();
1963 dwMainThreadId = GetCurrentThreadId();
1965 //necessary for normal Tk operation
1966 hThread = CreateThread(NULL, // no security attributes
1967 0, // use default stack size
1968 (LPTHREAD_START_ROUTINE) tkLoop, // thread function
1969 NULL, // no thread function argument
1970 0, // use default creation flags
1973 cout << "Tcl/Tk main loop thread not created. Switching to batch mode..." << endl;
1978 } catch (Standard_Failure) {
1979 cout <<" Pb au lancement de TK_Init "<<endl;
1982 Tcl_StaticPackage(interp, "Tk", Tk_Init, (Tcl_PackageInitProc *) NULL);
1984 //since the main Tcl/Tk loop wasn't created --> switch to batch mode
1985 return Standard_False;
1988 // san - 06/08/2002 - Time for tkLoop to start; Tk fails to initialize otherwise
1989 while (!isTkLoopStarted)
1992 // Saving of window classes
1994 if(!RegisterAppClass(hInst))
1995 return(Standard_False);
1998 ** Enter the application message-polling loop. This is the anchor for
2001 if(Draw_IsConsoleSubsystem)
2005 else if (hWndFrame = CreateAppWindow(hInst))
2007 ShowWindow(hWndFrame,nShow);
2008 UpdateWindow(hWndFrame);
2011 return Standard_True;
2014 Standard_Boolean Draw_Interprete (const char*);
2016 /*--------------------------------------------------------*\
2017 | readStdinThreadFunc
2018 \*--------------------------------------------------------*/
2019 static DWORD WINAPI readStdinThreadFunc(VOID)
2021 if (!Draw_IsConsoleSubsystem) return 1;
2024 while (console_semaphore != WAIT_CONSOLE_COMMAND)
2026 //if (gets(console_command))
2027 if (fgets(console_command,COMMAND_SIZE,stdin))
2029 console_semaphore = HAS_CONSOLE_COMMAND;
2036 /*--------------------------------------------------------*\
2037 | exitProc: finalization handler for Tcl/Tk thread. Forces parent process to die
2038 \*--------------------------------------------------------*/
2039 void exitProc(ClientData /*dc*/)
2041 HANDLE proc = GetCurrentProcess();
2042 TerminateProcess(proc, 0);
2045 /*--------------------------------------------------------*\
2046 | tkLoop: implements Tk_Main()-like behaviour in a separate thread
2047 \*--------------------------------------------------------*/
2048 static DWORD WINAPI tkLoop(VOID)
2050 Tcl_CreateExitHandler(exitProc, 0);
2051 #if (TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5))
2052 Tcl_RegisterChannel(theCommands.Interp(), Tcl_GetStdChannel(TCL_STDIN));
2053 Tcl_RegisterChannel(theCommands.Interp(), Tcl_GetStdChannel(TCL_STDOUT));
2054 Tcl_RegisterChannel(theCommands.Interp(), Tcl_GetStdChannel(TCL_STDERR));
2058 // initialize the Tk library if not in 'virtual windows' mode
2059 // (virtual windows are created by OCCT with native APIs,
2060 // thus Tk will be useless)
2061 if (!Draw_VirtualWindows)
2066 Standard_Integer res = Tk_Init (interp);
2069 cout << "tkLoop: error in Tk initialization. Tcl reported: " << interp->result << endl;
2072 catch (Standard_Failure)
2074 cout << "tkLoop: exception in TK_Init\n";
2076 Tcl_StaticPackage (interp, "Tk", Tk_Init, (Tcl_PackageInitProc* ) NULL);
2077 mainWindow = Tk_MainWindow (interp);
2078 if (mainWindow == NULL)
2080 fprintf (stderr, "%s\n", interp->result);
2081 cout << "tkLoop: Tk_MainWindow() returned NULL. Exiting...\n";
2084 Tk_Name(mainWindow) = Tk_GetUid (Tk_SetAppName (mainWindow, "Draw"));
2088 // set signal handler in the new thread
2091 // inform the others that we have started
2092 isTkLoopStarted = true;
2094 while (console_semaphore == STOP_CONSOLE)
2095 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
2097 if (Draw_IsConsoleSubsystem && console_semaphore == WAIT_CONSOLE_COMMAND)
2101 Standard_Boolean toLoop = Standard_True;
2104 while(Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT));
2105 if (console_semaphore == HAS_CONSOLE_COMMAND)
2107 if (Draw_Interprete (console_command))
2109 if (Draw_IsConsoleSubsystem) Prompt (interp, 0);
2113 if (Draw_IsConsoleSubsystem) Prompt (interp, 1);
2115 console_semaphore = WAIT_CONSOLE_COMMAND;
2122 // We should not exit until the Main Tk window is closed
2123 toLoop = (Tk_GetNumMainWindows() > 0) || Draw_VirtualWindows;
2131 /*--------------------------------------------------------*\
2133 \*--------------------------------------------------------*/
2134 void Run_Appli(HWND hWnd)
2137 HACCEL hAccel = NULL;
2141 // if (!(hAccel = LoadAccelerators (hInstance, MAKEINTRESOURCE(ACCEL_ID))))
2142 // MessageBox(hWnd, "MDI: Load Accel failure!", "Error", MB_OK);
2145 if (Draw_IsConsoleSubsystem) {
2146 hThread = CreateThread(NULL, // no security attributes
2147 0, // use default stack size
2148 (LPTHREAD_START_ROUTINE) readStdinThreadFunc, // thread function
2149 NULL, // no thread function argument
2150 0, // use default creation flags
2151 &IDThread); // returns thread identifier
2153 cout << "pb in creation of the thread reading stdin" << endl;
2154 Draw_IsConsoleSubsystem = Standard_False;
2155 Init_Appli(GetModuleHandle(NULL),
2156 GetModuleHandle(NULL),
2157 1, hWnd); // reinit => create MDI client wnd
2161 //turn on the command interpretation mechanism (regardless of the mode)
2162 if (console_semaphore == STOP_CONSOLE)
2163 console_semaphore = WAIT_CONSOLE_COMMAND;
2165 //simple Win32 message loop
2166 while (GetMessage(&msg, NULL, 0, 0) > 0)
2168 if (!TranslateAccelerator(hWnd, hAccel, &msg))
2170 TranslateMessage(&msg);
2171 DispatchMessage(&msg);
2178 /*--------------------------------------------------------*\
2180 \*--------------------------------------------------------*/
2181 void Destroy_Appli(HINSTANCE hInst)
2183 UnregisterAppClass(hInst);
2184 for (int i = 0 ; i < MAXCOLOR ; i++)
2185 DeleteObject(colorPenTab[i]);
2188 /*--------------------------------------------------------*\
2190 \*--------------------------------------------------------*/
2191 void DrawWindow::SelectWait(HANDLE& hWnd, int& x, int& y, int& button)
2197 GetMessage(&msg,NULL,0,0);
2198 while((msg.message != WM_RBUTTONDOWN && msg.message != WM_LBUTTONDOWN) ||
2199 ! ( Draw_IsConsoleSubsystem || IsChild(DrawWindow::hWndClientMDI,msg.hwnd)) )
2200 GetMessage(&msg,NULL,0,0);
2203 x = LOWORD(msg.lParam);
2204 y = HIWORD(msg.lParam);
2205 if (msg.message == WM_LBUTTONDOWN)
2211 /*--------------------------------------------------------*\
2213 \*--------------------------------------------------------*/
2214 void DrawWindow::SelectNoWait(HANDLE& hWnd, int& x, int& y, int& button)
2220 GetMessage(&msg,NULL,0,0);
2221 while((msg.message != WM_RBUTTONDOWN && msg.message != WM_LBUTTONDOWN &&
2222 msg.message != WM_MOUSEMOVE) ||
2223 ! ( Draw_IsConsoleSubsystem || IsChild(DrawWindow::hWndClientMDI,msg.hwnd) ) )
2224 GetMessage(&msg,NULL,0,0);
2226 x = LOWORD(msg.lParam);
2227 y = HIWORD(msg.lParam);
2228 switch (msg.message)
2230 case WM_LBUTTONDOWN :
2234 case WM_RBUTTONDOWN :
2244 Standard_Boolean DrawWindow::DefineColor (const Standard_Integer, const char*)
2246 return Standard_True;