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 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
83 Tcl_Write(errChannel, Tcl_GetStringResult(Interp), -1);
85 Tcl_Write(errChannel, Interp->result, -1);
87 Tcl_Write(errChannel, "\n", 1);
89 Tcl_AddErrorInfo(Interp,
90 "\n (script that generates prompt)");
95 Tcl_Flush(outChannel);
99 #if !defined(_WIN32) && !defined(__WIN32__)
105 #include <OSD_Timer.hxx>
107 #ifdef HAVE_SYS_TIME_H
108 # include <sys/time.h>
111 #ifdef HAVE_SYS_TYPES_H
112 # include <sys/types.h>
115 #ifdef HAVE_SYS_SELECT_H
116 #include <sys/select.h>
119 #ifdef HAVE_SYS_FILIO_H
120 #include <sys/filio.h>
122 #include <sys/ioctl.h>
131 #include <Draw_Window.hxx>
133 #ifdef HAVE_STRINGS_H
134 # include <strings.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];
173 Standard_Integer Draw_WindowScreen = 0;
174 Standard_Boolean Draw_BlackBackGround = Standard_True;
177 // Initialization of static variables of Draw_Window
178 //======================================================
179 Draw_Window* Draw_Window::firstWindow = NULL;
182 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
183 #include <X11/Xutil.h>
184 #include <Aspect_DisplayConnection.hxx>
186 Display* Draw_WindowDisplay = NULL;
187 Colormap Draw_WindowColorMap;
188 static Handle(Aspect_DisplayConnection) Draw_DisplayConnection;
190 // Base_Window struct definition
191 //===================================
195 XSetWindowAttributes xswa;
198 //=======================================================================
199 //function : Draw_Window
201 //=======================================================================
202 Draw_Window::Draw_Window() :
203 base(*new Base_Window()),
208 myUseBuffer(Standard_False),
209 withWindowManager(Standard_True)
211 myMother = RootWindow(Draw_WindowDisplay,
214 if (firstWindow) firstWindow->previous = this;
218 //=======================================================================
219 //function : Draw_Window
221 //=======================================================================
222 Draw_Window::Draw_Window(Window mother) :
223 base(*new Base_Window()),
228 myUseBuffer(Standard_False),
229 withWindowManager(Standard_True)
233 if (firstWindow) firstWindow->previous = this;
237 //=======================================================================
238 //function : Draw_Window
240 //=======================================================================
241 Draw_Window::Draw_Window (const char* title,
242 Standard_Integer X, Standard_Integer Y,
243 Standard_Integer DX, Standard_Integer DY) :
244 base(*new Base_Window()),
249 myUseBuffer(Standard_False),
250 withWindowManager(Standard_True)
252 myMother = RootWindow(Draw_WindowDisplay,
255 if (firstWindow) firstWindow->previous = this;
261 //=======================================================================
262 //function : Draw_Window
264 //=======================================================================
265 Draw_Window::Draw_Window (const char* window ) :
266 base(*new Base_Window()),
271 myUseBuffer(Standard_False),
272 withWindowManager(Standard_True)
274 sscanf(window,"%lx",&win);
275 Standard_Integer X,Y,DX,DY;
277 if (firstWindow) firstWindow->previous = this;
286 //=======================================================================
287 //function : Draw_Window
289 //=======================================================================
290 Draw_Window::Draw_Window (Window mother,
292 Standard_Integer X, Standard_Integer Y,
293 Standard_Integer DX, Standard_Integer DY) :
294 base(*new Base_Window()),
299 myUseBuffer(Standard_False),
300 withWindowManager(Standard_True)
304 if (firstWindow) firstWindow->previous = this;
310 //=======================================================================
311 //function : ~Draw_Window
313 //=======================================================================
314 Draw_Window::~Draw_Window()
317 previous->next = next;
321 next->previous = previous;
325 XFreePixmap(Draw_WindowDisplay, myBuffer);
328 // Liberation pointer on Base_Window
332 //=======================================================================
335 //=======================================================================
336 void Draw_Window::Init(Standard_Integer X, Standard_Integer Y,
337 Standard_Integer DX, Standard_Integer DY)
339 unsigned long setmask;
341 if (Draw_BlackBackGround)
343 base.xswa.background_pixel = BlackPixel(Draw_WindowDisplay,Draw_WindowScreen);
344 base.xswa.border_pixel = WhitePixel(Draw_WindowDisplay,Draw_WindowScreen);
348 base.xswa.background_pixel = WhitePixel(Draw_WindowDisplay,Draw_WindowScreen);
349 base.xswa.border_pixel = BlackPixel(Draw_WindowDisplay,Draw_WindowScreen);
351 base.xswa.colormap = Draw_WindowColorMap;
352 setmask = CWBackPixel | CWBorderPixel ;
355 myHints.flags = USPosition;
361 win = XCreateWindow(Draw_WindowDisplay,
364 (unsigned int) DX,(unsigned int) DY,
366 DefaultDepth(Draw_WindowDisplay,Draw_WindowScreen),
368 DefaultVisual(Draw_WindowDisplay,Draw_WindowScreen),
370 XSelectInput(Draw_WindowDisplay, win, ButtonPressMask|ExposureMask|
371 StructureNotifyMask);
373 // advise to the window manager to place it where I need
374 XSetWMNormalHints(Draw_WindowDisplay,win,&myHints);
376 if (Draw_VirtualWindows)
378 myUseBuffer = Standard_True;
383 base.gc = XCreateGC(Draw_WindowDisplay, win, 0, NULL);
385 XSetPlaneMask(Draw_WindowDisplay,base.gc,AllPlanes);
386 XSetForeground(Draw_WindowDisplay,
387 base.gc, WhitePixel(Draw_WindowDisplay,Draw_WindowScreen));
388 XSetBackground(Draw_WindowDisplay,
389 base.gc, BlackPixel(Draw_WindowDisplay,Draw_WindowScreen));
390 // save in case of window recovery
392 base.xswa.backing_store = Always;
393 XChangeWindowAttributes(Draw_WindowDisplay, win,
394 CWBackingStore, &base.xswa);
396 XSetLineAttributes (Draw_WindowDisplay, base.gc,
397 0, LineSolid, CapButt, JoinMiter);
400 //=======================================================================
401 //function : InitBuffer
403 //=======================================================================
404 void Draw_Window::InitBuffer()
408 XFreePixmap (Draw_WindowDisplay, myBuffer);
410 XWindowAttributes winAttr;
411 XGetWindowAttributes (Draw_WindowDisplay, win, &winAttr);
412 myBuffer = XCreatePixmap (Draw_WindowDisplay, win, winAttr.width, winAttr.height, winAttr.depth);
414 else if (myBuffer != 0)
416 XFreePixmap (Draw_WindowDisplay, myBuffer);
421 //=======================================================================
422 //function : StopWinManager
424 //=======================================================================
425 void Draw_Window::StopWinManager()
428 XWindowAttributes winAttr;
429 XGetWindowAttributes (Draw_WindowDisplay, win, &winAttr);
433 myHints.flags = USPosition;
434 myHints.x = (int) 30;
435 myHints.y = (int) 100;
437 base.xswa.override_redirect = 1;
438 base.xswa.border_pixel = BlackPixel(Draw_WindowDisplay,
440 base.xswa.background_pixel = WhitePixel(Draw_WindowDisplay,
443 withWindowManager = Standard_False;
445 win = XCreateWindow(Draw_WindowDisplay, myMother,
446 winAttr.x, winAttr.y,
447 winAttr.width, winAttr.height,
449 CopyFromParent, InputOutput, CopyFromParent,
450 CWBorderPixel|CWOverrideRedirect|CWBackPixel, &base.xswa);
453 // adwise to the window manager to place it where I wish
454 XSetWMNormalHints(Draw_WindowDisplay,win,&myHints);
456 // all masks of the old window are reassigned to the new one.
457 XSelectInput(Draw_WindowDisplay,win,winAttr.your_event_mask);
460 //=======================================================================
461 //function : SetPosition
463 //=======================================================================
464 void Draw_Window::SetPosition(Standard_Integer NewXpos,
465 Standard_Integer NewYpos)
467 Standard_Integer x,y;
470 if ( (x != NewXpos) || (y != NewYpos) )
471 XMoveWindow(Draw_WindowDisplay, win, NewXpos, NewYpos);
474 //=======================================================================
475 //function : SetDimension
477 //=======================================================================
478 void Draw_Window::SetDimension(Standard_Integer NewDx,
479 Standard_Integer NewDy)
481 if ( (NewDx != WidthWin() ) || (NewDy != HeightWin() ) )
482 XResizeWindow(Draw_WindowDisplay, win, NewDx, NewDy);
485 //=======================================================================
486 //function : GetPosition
488 //=======================================================================
489 void Draw_Window::GetPosition(Standard_Integer &PosX,
490 Standard_Integer &PosY)
492 XWindowAttributes winAttr;
493 XGetWindowAttributes(Draw_WindowDisplay, win, &winAttr);
499 //=======================================================================
500 //function : HeightWin
502 //=======================================================================
503 Standard_Integer Draw_Window::HeightWin() const
506 XWindowAttributes winAttr;
507 XGetWindowAttributes(Draw_WindowDisplay, win, &winAttr);
513 //=======================================================================
514 //function : WidthWin
516 //=======================================================================
517 Standard_Integer Draw_Window::WidthWin() const
520 XWindowAttributes winAttr;
521 XGetWindowAttributes(Draw_WindowDisplay, win, &winAttr);
527 //=======================================================================
528 //function : SetTitle
530 //=======================================================================
531 void Draw_Window::SetTitle(const char* title)
533 XStoreName(Draw_WindowDisplay, win, title);
536 //=======================================================================
537 //function : GetTitle
539 //=======================================================================
540 char* Draw_Window::GetTitle()
543 XFetchName(Draw_WindowDisplay, win, &title);
547 //=======================================================================
548 //function : GetDrawable
550 //=======================================================================
551 Drawable Draw_Window::GetDrawable() const
553 return myUseBuffer ? myBuffer : win;
556 //=======================================================================
557 //function :DefineColor
559 //=======================================================================
560 Standard_Boolean Draw_Window::DefineColor(const Standard_Integer i, const char* colorName)
564 if (!XParseColor(Draw_WindowDisplay,Draw_WindowColorMap,colorName,&color))
565 return Standard_False;
566 if (!XAllocColor(Draw_WindowDisplay,Draw_WindowColorMap,&color))
567 return Standard_False;
568 thePixels[i % MAXCOLOR] = color.pixel;
569 return Standard_True;
572 //=======================================================================
573 //function : DisplayWindow
575 //=======================================================================
576 void Draw_Window::DisplayWindow()
578 if (Draw_VirtualWindows)
584 XMapRaised(Draw_WindowDisplay, win);
586 XFlush(Draw_WindowDisplay);
589 //=======================================================================
592 //=======================================================================
593 void Draw_Window::Hide()
595 XUnmapWindow(Draw_WindowDisplay, win);
598 //=======================================================================
601 //=======================================================================
602 void Draw_Window::Destroy()
604 XFreeGC (Draw_WindowDisplay, base.gc);
605 XDestroyWindow(Draw_WindowDisplay, win);
609 XFreePixmap(Draw_WindowDisplay, myBuffer);
614 //=======================================================================
617 //=======================================================================
618 void Draw_Window::Clear()
622 // XClearArea only applicable for windows
623 XGCValues currValues;
624 XGetGCValues(Draw_WindowDisplay, base.gc, GCBackground | GCForeground, &currValues);
625 XSetForeground(Draw_WindowDisplay, base.gc, currValues.background);
626 XFillRectangle(Draw_WindowDisplay, myBuffer, base.gc, 0, 0, WidthWin(), HeightWin());
627 XSetForeground(Draw_WindowDisplay, base.gc, currValues.foreground);
631 XClearArea(Draw_WindowDisplay, win, 0, 0, 0, 0, False);
635 //=======================================================================
638 //=======================================================================
639 void Draw_Window::Flush()
641 XFlush(Draw_WindowDisplay);
644 //=======================================================================
645 //function : DrawString
647 //=======================================================================
648 void Draw_Window::DrawString(int X, int Y, char *text)
650 XDrawString(Draw_WindowDisplay, GetDrawable(), base.gc, X, Y, text, strlen(text));
653 //=======================================================================
654 //function : DrawSegments
656 //=======================================================================
657 void Draw_Window::DrawSegments(Segment *tab, int nbElem)
659 XDrawSegments(Draw_WindowDisplay, GetDrawable(), base.gc, (XSegment*) tab, nbElem);
662 //=======================================================================
665 //=======================================================================
666 void Draw_Window::Redraw()
669 XCopyArea (Draw_WindowDisplay,
670 myBuffer, win, // source, destination Drawables
673 WidthWin(), HeightWin(),
674 0, 0); // destination x, y
678 //=======================================================================
679 //function : SetColor
681 //=======================================================================
682 void Draw_Window::SetColor(Standard_Integer color)
684 XSetForeground(Draw_WindowDisplay, base.gc, thePixels[color]);
687 //=======================================================================
690 //=======================================================================
691 void Draw_Window::SetMode( int mode)
693 XSetFunction(Draw_WindowDisplay, base.gc, mode);
696 //=======================================================================
699 //=======================================================================
700 Standard_Boolean Draw_Window::Save (const char* theFileName) const
702 // make sure all draw operations done
703 XSync (Draw_WindowDisplay, True);
706 XWindowAttributes winAttr;
707 XGetWindowAttributes (Draw_WindowDisplay, win, &winAttr);
711 // make sure that the whole window fit on display to prevent BadMatch error
712 XWindowAttributes winAttrRoot;
713 XGetWindowAttributes (Draw_WindowDisplay, XRootWindowOfScreen (winAttr.screen), &winAttrRoot);
715 Window winChildDummy;
718 XTranslateCoordinates (Draw_WindowDisplay, win, XRootWindowOfScreen (winAttr.screen),
719 0, 0, &winLeft, &winTop, &winChildDummy);
721 if (((winLeft + winAttr.width) > winAttrRoot.width) || winLeft < winAttrRoot.x ||
722 ((winTop + winAttr.height) > winAttrRoot.height) || winTop < winAttrRoot.y)
724 std::cerr << "The window not fully visible! Can't create the snapshot.\n";
725 return Standard_False;
730 if (XMatchVisualInfo (Draw_WindowDisplay, Draw_WindowScreen, 32, TrueColor, &aVInfo) == 0
731 && XMatchVisualInfo (Draw_WindowDisplay, Draw_WindowScreen, 24, TrueColor, &aVInfo) == 0)
733 std::cerr << "24-bit TrueColor visual is not supported by server!\n";
734 return Standard_False;
737 Image_AlienPixMap anImage;
738 bool isBigEndian = Image_PixMap::IsBigEndianHost();
739 const Standard_Size aSizeRowBytes = Standard_Size(winAttr.width) * 4;
740 if (!anImage.InitTrash (isBigEndian ? Image_PixMap::ImgRGB32 : Image_PixMap::ImgBGR32,
741 Standard_Size(winAttr.width), Standard_Size(winAttr.height), aSizeRowBytes))
743 return Standard_False;
745 anImage.SetTopDown (true);
747 XImage* anXImage = XCreateImage (Draw_WindowDisplay, aVInfo.visual,
748 32, ZPixmap, 0, (char* )anImage.ChangeData(), winAttr.width, winAttr.height, 32, int(aSizeRowBytes));
749 anXImage->bitmap_bit_order = anXImage->byte_order = (isBigEndian ? MSBFirst : LSBFirst);
750 if (XGetSubImage (Draw_WindowDisplay, GetDrawable(),
751 0, 0, winAttr.width, winAttr.height,
752 AllPlanes, ZPixmap, anXImage, 0, 0) == NULL)
754 anXImage->data = NULL;
755 XDestroyImage (anXImage);
756 return Standard_False;
760 anXImage->data = NULL;
761 XDestroyImage (anXImage);
764 return anImage.Save (theFileName);
767 //=======================================================================
770 //=======================================================================
772 void Draw_Window::Wait (Standard_Boolean wait)
776 XSelectInput(Draw_WindowDisplay,win,
777 ButtonPressMask|ExposureMask | StructureNotifyMask |
781 XSelectInput(Draw_WindowDisplay,win,
782 ButtonPressMask|ExposureMask | StructureNotifyMask);
786 //=======================================================================
787 //function : ProcessEvent
789 //=======================================================================
791 void ProcessEvent(Draw_Window& win, XEvent& xev)
793 Standard_Integer X,Y,button,lenk;
809 button = xev.xbutton.button;
810 win.WButtonPress(X,Y,button);
816 button = xev.xbutton.button;
817 win.WButtonRelease(X,Y,button);
821 lenk = XLookupString(&(xev.xkey),
831 //WKeyPress(c,keysym);
837 win.WMotionNotify(X,Y);
840 case ConfigureNotify :
841 if (win.withWindowManager)
842 win.WConfigureNotify(xev.xconfigure.x, xev.xconfigure.y,
843 xev.xconfigure.width,
844 xev.xconfigure.height);
854 //=======================================================================
857 //=======================================================================
858 void Draw_Window::WExpose()
862 //=======================================================================
863 //function : WButtonPress
865 //=======================================================================
866 void Draw_Window::WButtonPress(const Standard_Integer,
867 const Standard_Integer,
868 const Standard_Integer&)
872 //=======================================================================
873 //function : WButtonRelease
875 //=======================================================================
876 void Draw_Window::WButtonRelease(const Standard_Integer,
877 const Standard_Integer,
878 const Standard_Integer&)
882 /**************************
883 //=======================================================================
884 //function : WKeyPress
886 //=======================================================================
888 void Draw_Window::WKeyPress(char, KeySym&)
891 ***************************/
893 //=======================================================================
894 //function : WMotionNotify
896 //=======================================================================
897 void Draw_Window::WMotionNotify(const Standard_Integer ,
898 const Standard_Integer )
902 //=======================================================================
903 //function : WConfigureNotify
905 //=======================================================================
907 void Draw_Window::WConfigureNotify(const Standard_Integer,
908 const Standard_Integer,
909 const Standard_Integer,
910 const Standard_Integer)
914 //=======================================================================
915 //function : WUnmapNotify
917 //=======================================================================
919 void Draw_Window::WUnmapNotify()
924 //======================================================
925 // funtion : ProcessEvents
926 // purpose : process pending X events
927 //======================================================
929 static void ProcessEvents(ClientData,int)
933 while (XPending(Draw_WindowDisplay)) {
938 XNextEvent(Draw_WindowDisplay,&xev);
940 /* search the window in the window list */
941 Draw_Window* w = Draw_Window::firstWindow;
942 Standard_Integer found=0;
944 if (xev.xany.window == w->win) {
945 ProcessEvent(*w, xev);
952 Tk_HandleEvent(&xev);
957 //======================================================
958 // funtion : GetNextEvent()
960 //======================================================
961 void GetNextEvent(Event& ev)
964 XNextEvent(Draw_WindowDisplay, &xev);
969 ev.window = xev.xbutton.window;
970 ev.button = xev.xbutton.button;
971 ev.x = xev.xbutton.x;
972 ev.y = xev.xbutton.y;
977 ev.window = xev.xmotion.window;
979 ev.x = xev.xmotion.x;
980 ev.y = xev.xmotion.y;
986 //======================================================
987 // funtion :Run_Appli
989 //======================================================
992 static Standard_Boolean(*Interprete) (const char*);
994 void Run_Appli(Standard_Boolean (*interprete) (const char*))
996 Tcl_Channel outChannel, inChannel ;
997 Interprete = interprete;
1002 * Commands will come from standard input, so set up an event
1003 * handler for standard input. If the input device is aEvaluate the
1004 * .rc file, if one has been specified, set up an event handler
1005 * for standard input, and print a prompt if the input
1006 * device is a terminal.
1008 inChannel = Tcl_GetStdChannel(TCL_STDIN);
1010 Tcl_CreateChannelHandler(inChannel, TCL_READABLE, StdinProc,
1011 (ClientData) inChannel);
1014 // Create a handler for the draw display
1016 // Adding of the casting into void* to be able to compile on AO1
1017 // ConnectionNumber(Draw_WindowDisplay) is an int 32 bits
1018 // (void*) is a pointer 64 bits ???????
1020 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
1021 #if TCL_MAJOR_VERSION < 8
1022 Tk_CreateFileHandler((void*) ConnectionNumber(Draw_WindowDisplay),
1023 TK_READABLE, ProcessEvents,(ClientData) 0 );
1025 Tk_CreateFileHandler(ConnectionNumber(Draw_WindowDisplay),
1026 TK_READABLE, ProcessEvents,(ClientData) 0 );
1032 if (tty) Prompt(theCommands.Interp(), 0);
1033 Prompt(theCommands.Interp(), 0);
1035 outChannel = Tcl_GetStdChannel(TCL_STDOUT);
1037 Tcl_Flush(outChannel);
1039 Tcl_DStringInit(&command);
1042 * Loop infinitely, waiting for commands to execute. When there
1043 * are no windows left, Tk_MainLoop returns and we exit.
1048 if (Draw_VirtualWindows) {
1049 // main window will never shown
1050 // but main loop will parse all Xlib messages
1051 Tcl_Eval(theCommands.Interp(), "wm withdraw .");
1058 Standard_Integer count = ConnectionNumber(Draw_WindowDisplay);
1059 Standard_Integer numfd;
1063 FD_SET(count,&readset);
1065 numfd = select(count+1,(Integer*)&readset,NULL,NULL,NULL);
1067 numfd = select(count+1,&readset,NULL,NULL,NULL);
1069 if (FD_ISSET(0,&readset)) StdinProc((ClientData)0,0);
1070 if (FD_ISSET(count,&readset)) ProcessEvents((ClientData)0,0);
1076 //======================================================
1077 // funtion : Init_Appli()
1079 //======================================================
1080 Standard_Boolean Init_Appli()
1083 interp = theCommands.Interp();
1089 } catch (Standard_Failure) {
1090 cout <<" Pb au lancement de TK_Init "<<endl;
1093 Tcl_StaticPackage(interp, "Tk", Tk_Init, (Tcl_PackageInitProc *) NULL);
1096 Tk_MainWindow(interp) ;
1097 if (mainWindow == NULL) {
1098 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
1099 fprintf(stderr, "%s\n", Tcl_GetStringResult(interp));
1101 fprintf(stderr, "%s\n", interp->result);
1105 Tk_Name(mainWindow) =
1106 Tk_GetUid(Tk_SetAppName(mainWindow,
1109 Tk_GeometryRequest(mainWindow, 200, 200);
1111 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
1112 if (Draw_DisplayConnection.IsNull())
1116 Draw_DisplayConnection = new Aspect_DisplayConnection();
1118 catch (Standard_Failure)
1120 std::cout << "Cannot open display. Interpret commands in batch mode." << std::endl;
1121 return Standard_False;
1124 if (Draw_WindowDisplay == NULL)
1126 Draw_WindowDisplay = Draw_DisplayConnection->GetDisplay();
1129 // synchronize the display server : could be done within Tk_Init
1131 XSynchronize(Draw_WindowDisplay, True);
1132 XSetInputFocus(Draw_WindowDisplay,
1134 RevertToPointerRoot,
1137 Draw_WindowScreen = DefaultScreen(Draw_WindowDisplay);
1138 Draw_WindowColorMap = DefaultColormap(Draw_WindowDisplay,
1143 Tcl_SetVar(interp,"tcl_interactive",(char*)(tty ? "1" : "0"), TCL_GLOBAL_ONLY);
1144 // Tcl_SetVar(interp,"tcl_interactive",tty ? "1" : "0", TCL_GLOBAL_ONLY);
1145 return Standard_True;
1148 //======================================================
1149 // funtion : Destroy_Appli()
1151 //======================================================
1152 void Destroy_Appli()
1154 //XCloseDisplay(Draw_WindowDisplay);
1158 *----------------------------------------------------------------------
1162 * This procedure is invoked by the event dispatcher whenever
1163 * standard input becomes readable. It grabs the next line of
1164 * input characters, adds them to a command being assembled, and
1165 * executes the command if it's complete.
1171 * Could be almost arbitrary, depending on the command that's
1174 *----------------------------------------------------------------------
1178 //static void StdinProc(ClientData clientData, int mask)
1179 static void StdinProc(ClientData clientData, int )
1181 static int gotPartial = 0;
1185 Tcl_Channel chan = (Tcl_Channel) clientData;
1187 // MSV Nov 2, 2001: patch for TCL 8.3: initialize line to avoid exception
1188 // when first user input is an empty string
1189 Tcl_DStringFree(&line);
1190 count = Tcl_Gets(chan, &line);
1193 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)))
1194 Tcl_DString linetmp;
1195 Tcl_DStringInit(&linetmp);
1196 Tcl_UniChar * UniCharString;
1197 UniCharString = Tcl_UtfToUniCharDString(Tcl_DStringValue(&line),-1,&linetmp);
1198 Standard_Integer l = Tcl_UniCharLen(UniCharString);
1199 TCollection_AsciiString AsciiString("");
1200 Standard_Character Character;
1202 for (i=0; i<l; i++) {
1203 Character = UniCharString[i];
1204 AsciiString.AssignCat(Character);
1206 Tcl_DStringInit(&line);
1207 Tcl_DStringAppend(&line, AsciiString.ToCString(), -1);
1214 Tcl_DeleteChannelHandler(chan, StdinProc, (ClientData) chan);
1222 (void) Tcl_DStringAppend(&command, Tcl_DStringValue(&line), -1);
1223 cmd = Tcl_DStringAppend(&command, "\n", -1);
1224 Tcl_DStringFree(&line);
1227 if (!Tcl_CommandComplete(cmd)) {
1234 * Disable the stdin channel handler while evaluating the command;
1235 * otherwise if the command re-enters the event loop we might
1236 * process commands from stdin before the current command is
1237 * finished. Among other things, this will trash the text of the
1238 * command being evaluated.
1241 Tcl_CreateChannelHandler(chan, 0, StdinProc, (ClientData) chan);
1245 * Disable the stdin file handler while evaluating the command;
1246 * otherwise if the command re-enters the event loop we might
1247 * process commands from stdin before the current command is
1248 * finished. Among other things, this will trash the text of the
1249 * command being evaluated.
1253 // Tk_CreateFileHandler(0, 0, StdinProc, (ClientData) 0);
1256 // xab average to avoid an output SIGBUS of DRAW
1257 // to ultimately prescise or remove once
1258 // the problem of free on the global variable at the average
1265 Tcl_CreateChannelHandler(chan, TCL_READABLE, StdinProc,
1267 Tcl_DStringFree(&command);
1274 if (tty) Prompt(interp, gotPartial);
1276 } catch (Standard_Failure) {}
1282 // Source Specifique WNT
1284 /****************************************************\
1287 \****************************************************/
1289 #include "Draw_Window.hxx"
1290 #include "DrawRessource.h"
1293 #include <Draw_Appli.hxx>
1300 // Position of information in the extra memory
1302 // indicates SUBSYSTEM:CONSOLE linker option, to be set to True in main()
1304 Standard_Boolean Draw_IsConsoleSubsystem = Standard_False;
1307 Standard_Boolean Draw_BlackBackGround = Standard_True;
1309 // Creation of color stylos
1310 HPEN colorPenTab[MAXCOLOR] = {CreatePen(PS_SOLID, PENWIDTH, RGB(255,255,255)),
1311 CreatePen(PS_SOLID, PENWIDTH, RGB(255,0,0)),
1312 CreatePen(PS_SOLID, PENWIDTH, RGB(0,255,0)),
1313 CreatePen(PS_SOLID, PENWIDTH, RGB(0,0,255)),
1314 CreatePen(PS_SOLID, PENWIDTH, RGB(0,255,255)),
1315 CreatePen(PS_SOLID, PENWIDTH, RGB(255,215,0)),
1316 CreatePen(PS_SOLID, PENWIDTH, RGB(255,0,255)),
1317 CreatePen(PS_SOLID, PENWIDTH, RGB(255,52,179)),
1318 CreatePen(PS_SOLID, PENWIDTH, RGB(255,165,0)),
1319 CreatePen(PS_SOLID, PENWIDTH, RGB(255,228,225)),
1320 CreatePen(PS_SOLID, PENWIDTH, RGB(255,160,122)),
1321 CreatePen(PS_SOLID, PENWIDTH, RGB(199,21,133)),
1322 CreatePen(PS_SOLID, PENWIDTH, RGB(255,255,0)),
1323 CreatePen(PS_SOLID, PENWIDTH, RGB(240,230,140)),
1324 CreatePen(PS_SOLID, PENWIDTH, RGB(255,127,80))};
1326 // Correspondance mode X11 and WINDOWS NT
1327 int modeTab[16] = {R2_BLACK, R2_MASKPEN, R2_MASKPENNOT, R2_COPYPEN,
1328 R2_MASKNOTPEN, R2_NOP, R2_XORPEN, R2_MERGEPEN,
1329 R2_NOTMASKPEN, R2_NOTXORPEN, R2_NOT, R2_MERGEPENNOT,
1330 R2_NOTCOPYPEN, R2_MERGENOTPEN, R2_NOTMERGEPEN, R2_WHITE};
1332 /*--------------------------------------------------------*\
1333 | CREATE DRAW WINDOW PROCEDURE
1334 \*--------------------------------------------------------*/
1335 HWND DrawWindow::CreateDrawWindow(HWND hWndClient, int nitem)
1337 if (Draw_IsConsoleSubsystem) {
1338 HWND aWin = CreateWindow (DRAWCLASS, DRAWTITLE,
1339 WS_OVERLAPPEDWINDOW,
1341 NULL, NULL,::GetModuleHandle(NULL), NULL);
1342 if (!Draw_VirtualWindows)
1344 SetWindowPos(aWin, HWND_TOPMOST, 1,1,1,1, SWP_NOMOVE);
1345 SetWindowPos(aWin, HWND_NOTOPMOST, 1,1,1,1, SWP_NOMOVE);
1352 hInstance = (HANDLE)GetWindowLong(hWndClient,GWL_HINSTANCE);
1354 hInstance = (HANDLE)GetWindowLong(hWndClient,GWLP_HINSTANCE);
1357 return CreateMDIWindow(DRAWCLASS, DRAWTITLE,
1358 WS_CAPTION | WS_CHILD | WS_THICKFRAME,
1360 hWndClient, (HINSTANCE)hInstance, nitem);
1365 /*--------------------------------------------------------*\
1366 | DRAW WINDOW PROCEDURE
1367 \*--------------------------------------------------------*/
1368 LRESULT APIENTRY DrawWindow::DrawProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
1370 DrawWindow* localObjet = (DrawWindow*)GetWindowLong(hWnd, CLIENTWND);
1373 if (Draw_IsConsoleSubsystem)
1374 return (DefWindowProc(hWnd, wMsg, wParam, lParam));
1376 return(DefMDIChildProc(hWnd, wMsg, wParam, lParam));
1384 BeginPaint(hWnd, &ps);
1385 if (localObjet->GetUseBuffer())
1386 localObjet->Redraw();
1388 localObjet->WExpose();
1389 EndPaint(hWnd, &ps);
1394 if (localObjet->GetUseBuffer()) {
1395 localObjet->InitBuffer();
1396 localObjet->WExpose();
1397 localObjet->Redraw();
1403 if (Draw_IsConsoleSubsystem)
1404 return (DefWindowProc(hWnd, wMsg, wParam, lParam));
1406 return(DefMDIChildProc(hWnd, wMsg, wParam, lParam));
1414 ** IMPLEMENTATION of the CLASS DRAWWINDOW
1417 /*--------------------------------------------------------*\
1418 | Initialization of static variables of DrawWindow
1419 \*--------------------------------------------------------*/
1421 DrawWindow* DrawWindow::firstWindow = NULL;
1422 HWND DrawWindow::hWndClientMDI = 0;
1424 /*--------------------------------------------------------*\
1425 | Constructors of Draw_Window
1426 \*--------------------------------------------------------*/
1428 // Default Constructor
1429 //________________________
1430 DrawWindow::DrawWindow() :
1435 myUseBuffer(Standard_False)
1437 if (firstWindow) firstWindow->previous = this;
1441 //________________________
1442 DrawWindow::DrawWindow(char* title,
1443 Standard_Integer X, Standard_Integer Y,
1444 Standard_Integer dX,Standard_Integer dY) :
1445 win(0), next(firstWindow), previous(NULL), myMemHbm(NULL), myUseBuffer(Standard_False)
1447 if (firstWindow) firstWindow->previous = this;
1452 DrawWindow::DrawWindow(char* title,
1453 Standard_Integer X, Standard_Integer Y,
1454 Standard_Integer dX,Standard_Integer dY,
1456 win(theWin),next(firstWindow), previous(NULL), myMemHbm(NULL), myUseBuffer(Standard_False)
1458 if (firstWindow) firstWindow->previous = this;
1466 /*--------------------------------------------------------*\
1467 | Destructor of DrawWindow
1468 \*--------------------------------------------------------*/
1469 DrawWindow::~DrawWindow()
1472 previous->next = next;
1476 next->previous = previous;
1478 // Delete 'off-screen drawing'-related objects
1480 DeleteObject(myMemHbm);
1487 /*--------------------------------------------------------*\
1489 \*--------------------------------------------------------*/
1490 void DrawWindow::Init(Standard_Integer theXLeft, Standard_Integer theYTop,
1491 Standard_Integer theWidth, Standard_Integer theHeight)
1495 win = CreateDrawWindow(hWndClientMDI, 0);
1498 // include decorations in the window dimensions
1499 // to reproduce same behaviour of Xlib window.
1500 DWORD aWinStyle = GetWindowLong (win, GWL_STYLE);
1501 DWORD aWinStyleEx = GetWindowLong (win, GWL_EXSTYLE);
1502 HMENU aMenu = GetMenu (win);
1505 aRect.top = theYTop;
1506 aRect.bottom = theYTop + theHeight;
1507 aRect.left = theXLeft;
1508 aRect.right = theXLeft + theWidth;
1509 AdjustWindowRectEx (&aRect, aWinStyle, aMenu != NULL ? TRUE : FALSE, aWinStyleEx);
1511 SetPosition (aRect.left, aRect.top);
1512 SetDimension (aRect.right - aRect.left, aRect.bottom - aRect.top);
1513 // Save the pointer at the instance associated to the window
1514 SetWindowLong(win, CLIENTWND, (LONG)this);
1515 HDC hDC = GetDC(win);
1516 SetBkColor(hDC, RGB(0, 0, 0));
1519 SelectObject(hDC, colorPenTab[myCurrPen]); // Default pencil
1520 SelectObject(hDC, GetStockObject(BLACK_BRUSH));
1521 SetTextColor(hDC, RGB(0,0,255));
1522 ReleaseDC(win, hDC);
1524 if (Draw_VirtualWindows)
1526 // create a virtual window
1527 SetUseBuffer (Standard_True);
1531 /*--------------------------------------------------------*\
1533 \*--------------------------------------------------------*/
1534 void DrawWindow::SetUseBuffer(Standard_Boolean use)
1540 /*--------------------------------------------------------*\
1542 \*--------------------------------------------------------*/
1543 void DrawWindow::InitBuffer()
1547 HDC hDC = GetDC(win);
1548 GetClientRect(win, &rc);
1551 GetObject(myMemHbm, sizeof(BITMAP), &aBmp);
1552 if (rc.right-rc.left == aBmp.bmWidth && rc.bottom-rc.top == aBmp.bmHeight) return;
1553 DeleteObject(myMemHbm);
1555 myMemHbm = (HBITMAP)CreateCompatibleBitmap(hDC,
1558 HDC aMemDC = GetMemDC(hDC);
1559 FillRect(aMemDC, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH));
1560 ReleaseMemDC(aMemDC);
1561 ReleaseDC(win, hDC);
1565 DeleteObject(myMemHbm);
1571 /*--------------------------------------------------------*\
1573 \*--------------------------------------------------------*/
1574 HDC DrawWindow::GetMemDC(HDC theWinDC)
1576 if (!myUseBuffer) return NULL;
1578 HDC aWorkDC = CreateCompatibleDC(theWinDC);
1579 myOldHbm = (HBITMAP)SelectObject(aWorkDC, myMemHbm);
1580 SetROP2(aWorkDC, modeTab[myCurrMode]);
1581 SelectObject(aWorkDC, colorPenTab[myCurrPen]);
1582 SetBkColor(aWorkDC, RGB(0, 0, 0));
1583 SelectObject(aWorkDC, GetStockObject(BLACK_BRUSH));
1584 SetTextColor(aWorkDC, RGB(0,0,255));
1589 /*--------------------------------------------------------*\
1591 \*--------------------------------------------------------*/
1592 void DrawWindow::ReleaseMemDC(HDC theMemDC)
1594 if (!myUseBuffer || !theMemDC) return;
1596 if (myOldHbm) SelectObject(theMemDC, myOldHbm);
1601 /*--------------------------------------------------------*\
1603 \*--------------------------------------------------------*/
1604 void DrawWindow::SetPosition(Standard_Integer posX, Standard_Integer posY)
1606 SetWindowPos(win, 0,
1609 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
1613 /*--------------------------------------------------------*\
1615 \*--------------------------------------------------------*/
1616 void DrawWindow::SetDimension(Standard_Integer dimX, Standard_Integer dimY)
1618 SetWindowPos(win, 0,
1621 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
1625 /*--------------------------------------------------------*\
1627 \*--------------------------------------------------------*/
1628 void DrawWindow::GetPosition(Standard_Integer &dimX,
1629 Standard_Integer &dimY)
1632 GetWindowRect(win, &rect);
1635 point.x = rect.left;
1638 ScreenToClient(hWndClientMDI, &point);
1644 /*--------------------------------------------------------*\
1646 \*--------------------------------------------------------*/
1647 Standard_Integer DrawWindow::HeightWin() const
1650 GetClientRect(win, &rect);
1651 return(rect.bottom-rect.top);
1655 /*--------------------------------------------------------*\
1657 \*--------------------------------------------------------*/
1658 Standard_Integer DrawWindow::WidthWin() const
1661 GetClientRect(win, &rect);
1662 return(rect.right-rect.left);
1666 /*--------------------------------------------------------*\
1668 \*--------------------------------------------------------*/
1669 void DrawWindow::SetTitle(char* title)
1671 SetWindowText(win, title);
1675 /*--------------------------------------------------------*\
1677 | Attention do not forget to unallocate the memory
1678 \*--------------------------------------------------------*/
1679 char* DrawWindow::GetTitle()
1681 char* title=new char[31];
1682 GetWindowText(win, title, 30);
1687 /*--------------------------------------------------------*\
1689 \*--------------------------------------------------------*/
1690 void DrawWindow::DisplayWindow()
1692 if (Draw_VirtualWindows)
1696 ShowWindow (win, SW_SHOW);
1701 /*--------------------------------------------------------*\
1703 \*--------------------------------------------------------*/
1704 void DrawWindow::Hide()
1706 ShowWindow(win, SW_HIDE);
1710 /*--------------------------------------------------------*\
1712 \*--------------------------------------------------------*/
1713 void DrawWindow::Destroy()
1720 /*--------------------------------------------------------*\
1722 \*--------------------------------------------------------*/
1723 void DrawWindow::Clear()
1725 HDC hDC = GetDC(win);
1726 HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
1729 SelectObject(aWorkDC,GetStockObject(BLACK_PEN));
1730 Rectangle(aWorkDC, 0, 0, WidthWin(), HeightWin());
1731 RestoreDC(aWorkDC,-1);
1733 if (myUseBuffer) ReleaseMemDC(aWorkDC);
1737 /*--------------------------------------------------------*\
1739 \*--------------------------------------------------------*/
1740 static Standard_Boolean SaveBitmap (HBITMAP theHBitmap,
1741 const char* theFileName)
1743 // Get informations about the bitmap
1745 if (GetObject (theHBitmap, sizeof(BITMAP), (LPSTR )&aBitmap) == 0)
1747 return Standard_False;
1750 Image_AlienPixMap anImage;
1751 const Standard_Size aSizeRowBytes = Standard_Size(aBitmap.bmWidth) * 4;
1752 if (!anImage.InitTrash (Image_PixMap::ImgBGR32, Standard_Size(aBitmap.bmWidth), Standard_Size(aBitmap.bmHeight), aSizeRowBytes))
1754 return Standard_False;
1756 anImage.SetTopDown (false);
1759 BITMAPINFOHEADER aBitmapInfo;
1760 memset (&aBitmapInfo, 0, sizeof(BITMAPINFOHEADER));
1761 aBitmapInfo.biSize = sizeof(BITMAPINFOHEADER);
1762 aBitmapInfo.biWidth = aBitmap.bmWidth;
1763 aBitmapInfo.biHeight = aBitmap.bmHeight; // positive means bottom-up!
1764 aBitmapInfo.biPlanes = 1;
1765 aBitmapInfo.biBitCount = 32; // use 32bit for automatic word-alignment per row
1766 aBitmapInfo.biCompression = BI_RGB;
1769 HDC aDC = GetDC (NULL);
1770 Standard_Boolean isSuccess = GetDIBits (aDC, theHBitmap,
1771 0, // first scan line to set
1772 aBitmap.bmHeight, // number of scan lines to copy
1773 anImage.ChangeData(), // array for bitmap bits
1774 (LPBITMAPINFO )&aBitmapInfo, // bitmap data info
1775 DIB_RGB_COLORS) != 0;
1776 ReleaseDC (NULL, aDC);
1777 return isSuccess && anImage.Save (theFileName);
1780 /*--------------------------------------------------------*\
1782 \*--------------------------------------------------------*/
1783 Standard_Boolean DrawWindow::Save (const char* theFileName) const
1787 return SaveBitmap (myMemHbm, theFileName);
1791 GetClientRect (win, &aRect);
1792 int aWidth = aRect.right - aRect.left;
1793 int aHeight = aRect.bottom - aRect.top;
1796 HDC aDstDC = GetDC (NULL);
1797 HDC aSrcDC = GetDC (win); // we copy only client area
1798 HDC aMemDC = CreateCompatibleDC (aDstDC);
1800 // Copy the screen to the bitmap
1801 HBITMAP anHBitmapDump = CreateCompatibleBitmap (aDstDC, aWidth, aHeight);
1802 HBITMAP anHBitmapOld = (HBITMAP )SelectObject (aMemDC, anHBitmapDump);
1803 BitBlt (aMemDC, 0, 0, aWidth, aHeight, aSrcDC, 0, 0, SRCCOPY);
1805 Standard_Boolean isSuccess = SaveBitmap (anHBitmapDump, theFileName);
1808 DeleteObject (SelectObject (aMemDC, anHBitmapOld));
1814 /*--------------------------------------------------------*\
1816 \*--------------------------------------------------------*/
1817 void DrawWindow::DrawString(int x,int y, char* text)
1819 HDC hDC = GetDC(win);
1820 HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
1822 TextOut(aWorkDC, x, y, text, (int )strlen(text));
1824 if (myUseBuffer) ReleaseMemDC(aWorkDC);
1828 /*--------------------------------------------------------*\
1830 \*--------------------------------------------------------*/
1831 void DrawWindow::DrawSegments(Segment *tab, int nbElem)
1833 HDC hDC = GetDC(win);
1834 HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
1836 for(int i = 0 ; i < nbElem ; i++)
1838 MoveToEx(aWorkDC, tab[i].x1, tab[i].y1, NULL);
1839 LineTo(aWorkDC, tab[i].x2, tab[i].y2);
1842 if (myUseBuffer) ReleaseMemDC(aWorkDC);
1846 /*--------------------------------------------------------*\
1848 \*--------------------------------------------------------*/
1849 void DrawWindow::Redraw()
1852 HDC hDC = GetDC(win);
1854 GetClientRect(win, &rc);
1855 HDC aMemDC = GetMemDC(hDC);
1858 rc.right-rc.left, rc.bottom-rc.top,
1861 ReleaseMemDC(aMemDC);
1866 /*--------------------------------------------------------*\
1868 \*--------------------------------------------------------*/
1869 void DrawWindow::SetMode(int mode)
1871 HDC hDC = GetDC(win);
1873 SetROP2(hDC, modeTab[mode]);
1878 /*--------------------------------------------------------*\
1880 \*--------------------------------------------------------*/
1881 void DrawWindow::SetColor(Standard_Integer color)
1883 HDC hDC = GetDC(win);
1885 SelectObject(hDC,colorPenTab[color]);
1890 /*--------------------------------------------------------*\
1892 \*--------------------------------------------------------*/
1893 void DrawWindow::WExpose()
1898 /*--------------------------------------------------------*\
1900 \*--------------------------------------------------------*/
1901 void DrawWindow::WButtonPress(const Standard_Integer,
1902 const Standard_Integer,
1903 const Standard_Integer&)
1908 /*--------------------------------------------------------*\
1910 \*--------------------------------------------------------*/
1911 void DrawWindow::WButtonRelease(const Standard_Integer,
1912 const Standard_Integer,
1913 const Standard_Integer&)
1918 /*--------------------------------------------------------*\
1920 \*--------------------------------------------------------*/
1921 void Draw_Window::WMotionNotify(const Standard_Integer ,
1922 const Standard_Integer )
1927 /*--------------------------------------------------------*\
1929 \*--------------------------------------------------------*/
1930 void DrawWindow::WConfigureNotify(const Standard_Integer,
1931 const Standard_Integer,
1932 const Standard_Integer,
1933 const Standard_Integer)
1938 /*--------------------------------------------------------*\
1940 \*--------------------------------------------------------*/
1941 void DrawWindow::WUnmapNotify()
1948 ** IMPLEMENTATION of the CLASS SEGMENT
1951 /*--------------------------------------------------------*\
1953 \*--------------------------------------------------------*/
1955 void Segment::Init(Standard_Integer a1, Standard_Integer a2,
1956 Standard_Integer a3, Standard_Integer a4)
1964 static DWORD WINAPI tkLoop(VOID);
1966 static Tk_Window mainWindow;
1969 //* threads sinchronization *//
1970 DWORD dwMainThreadId;
1971 console_semaphore_value volatile console_semaphore = WAIT_CONSOLE_COMMAND;
1972 //char console_command[1000];
1973 #define COMMAND_SIZE 1000 /* Console Command size */
1974 char console_command[COMMAND_SIZE];
1975 bool volatile isTkLoopStarted = false;
1977 /*--------------------------------------------------------*\
1979 \*--------------------------------------------------------*/
1980 Standard_Boolean Init_Appli(HINSTANCE hInst,
1981 HINSTANCE hPrevInst, int nShow, HWND& hWndFrame )
1985 console_semaphore = STOP_CONSOLE;
1987 interp = theCommands.Interp();
1990 dwMainThreadId = GetCurrentThreadId();
1992 //necessary for normal Tk operation
1993 hThread = CreateThread(NULL, // no security attributes
1994 0, // use default stack size
1995 (LPTHREAD_START_ROUTINE) tkLoop, // thread function
1996 NULL, // no thread function argument
1997 0, // use default creation flags
2000 cout << "Tcl/Tk main loop thread not created. Switching to batch mode..." << endl;
2005 } catch (Standard_Failure) {
2006 cout <<" Pb au lancement de TK_Init "<<endl;
2009 Tcl_StaticPackage(interp, "Tk", Tk_Init, (Tcl_PackageInitProc *) NULL);
2011 //since the main Tcl/Tk loop wasn't created --> switch to batch mode
2012 return Standard_False;
2015 // san - 06/08/2002 - Time for tkLoop to start; Tk fails to initialize otherwise
2016 while (!isTkLoopStarted)
2019 // Saving of window classes
2021 if(!RegisterAppClass(hInst))
2022 return(Standard_False);
2025 ** Enter the application message-polling loop. This is the anchor for
2028 if(Draw_IsConsoleSubsystem)
2032 else if (hWndFrame = CreateAppWindow(hInst))
2034 ShowWindow(hWndFrame,nShow);
2035 UpdateWindow(hWndFrame);
2038 return Standard_True;
2041 Standard_Boolean Draw_Interprete (const char*);
2043 /*--------------------------------------------------------*\
2044 | readStdinThreadFunc
2045 \*--------------------------------------------------------*/
2046 static DWORD WINAPI readStdinThreadFunc(VOID)
2048 if (!Draw_IsConsoleSubsystem) return 1;
2051 while (console_semaphore != WAIT_CONSOLE_COMMAND)
2053 //if (gets(console_command))
2054 if (fgets(console_command,COMMAND_SIZE,stdin))
2056 console_semaphore = HAS_CONSOLE_COMMAND;
2063 /*--------------------------------------------------------*\
2064 | exitProc: finalization handler for Tcl/Tk thread. Forces parent process to die
2065 \*--------------------------------------------------------*/
2066 void exitProc(ClientData /*dc*/)
2068 HANDLE proc = GetCurrentProcess();
2069 TerminateProcess(proc, 0);
2072 /*--------------------------------------------------------*\
2073 | tkLoop: implements Tk_Main()-like behaviour in a separate thread
2074 \*--------------------------------------------------------*/
2075 static DWORD WINAPI tkLoop(VOID)
2077 Tcl_CreateExitHandler(exitProc, 0);
2078 #if (TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5))
2079 Tcl_RegisterChannel(theCommands.Interp(), Tcl_GetStdChannel(TCL_STDIN));
2080 Tcl_RegisterChannel(theCommands.Interp(), Tcl_GetStdChannel(TCL_STDOUT));
2081 Tcl_RegisterChannel(theCommands.Interp(), Tcl_GetStdChannel(TCL_STDERR));
2085 // initialize the Tk library if not in 'virtual windows' mode
2086 // (virtual windows are created by OCCT with native APIs,
2087 // thus Tk will be useless)
2088 if (!Draw_VirtualWindows)
2093 Standard_Integer res = Tk_Init (interp);
2096 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
2097 cout << "tkLoop: error in Tk initialization. Tcl reported: " << Tcl_GetStringResult(interp) << endl;
2099 cout << "tkLoop: error in Tk initialization. Tcl reported: " << interp->result << endl;
2103 catch (Standard_Failure)
2105 cout << "tkLoop: exception in TK_Init\n";
2107 Tcl_StaticPackage (interp, "Tk", Tk_Init, (Tcl_PackageInitProc* ) NULL);
2108 mainWindow = Tk_MainWindow (interp);
2109 if (mainWindow == NULL)
2111 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
2112 fprintf (stderr, "%s\n", Tcl_GetStringResult(interp));
2114 fprintf (stderr, "%s\n", interp->result);
2116 cout << "tkLoop: Tk_MainWindow() returned NULL. Exiting...\n";
2119 Tk_Name(mainWindow) = Tk_GetUid (Tk_SetAppName (mainWindow, "Draw"));
2123 // set signal handler in the new thread
2126 // inform the others that we have started
2127 isTkLoopStarted = true;
2129 while (console_semaphore == STOP_CONSOLE)
2130 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
2132 if (Draw_IsConsoleSubsystem && console_semaphore == WAIT_CONSOLE_COMMAND)
2136 Standard_Boolean toLoop = Standard_True;
2139 while(Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT));
2140 if (console_semaphore == HAS_CONSOLE_COMMAND)
2142 if (Draw_Interprete (console_command))
2144 if (Draw_IsConsoleSubsystem) Prompt (interp, 0);
2148 if (Draw_IsConsoleSubsystem) Prompt (interp, 1);
2150 console_semaphore = WAIT_CONSOLE_COMMAND;
2157 // We should not exit until the Main Tk window is closed
2158 toLoop = (Tk_GetNumMainWindows() > 0) || Draw_VirtualWindows;
2166 /*--------------------------------------------------------*\
2168 \*--------------------------------------------------------*/
2169 void Run_Appli(HWND hWnd)
2172 HACCEL hAccel = NULL;
2176 // if (!(hAccel = LoadAccelerators (hInstance, MAKEINTRESOURCE(ACCEL_ID))))
2177 // MessageBox(hWnd, "MDI: Load Accel failure!", "Error", MB_OK);
2180 if (Draw_IsConsoleSubsystem) {
2181 hThread = CreateThread(NULL, // no security attributes
2182 0, // use default stack size
2183 (LPTHREAD_START_ROUTINE) readStdinThreadFunc, // thread function
2184 NULL, // no thread function argument
2185 0, // use default creation flags
2186 &IDThread); // returns thread identifier
2188 cout << "pb in creation of the thread reading stdin" << endl;
2189 Draw_IsConsoleSubsystem = Standard_False;
2190 Init_Appli(GetModuleHandle(NULL),
2191 GetModuleHandle(NULL),
2192 1, hWnd); // reinit => create MDI client wnd
2196 //turn on the command interpretation mechanism (regardless of the mode)
2197 if (console_semaphore == STOP_CONSOLE)
2198 console_semaphore = WAIT_CONSOLE_COMMAND;
2200 //simple Win32 message loop
2201 while (GetMessage(&msg, NULL, 0, 0) > 0)
2203 if (!TranslateAccelerator(hWnd, hAccel, &msg))
2205 TranslateMessage(&msg);
2206 DispatchMessage(&msg);
2213 /*--------------------------------------------------------*\
2215 \*--------------------------------------------------------*/
2216 void Destroy_Appli(HINSTANCE hInst)
2218 UnregisterAppClass(hInst);
2219 for (int i = 0 ; i < MAXCOLOR ; i++)
2220 DeleteObject(colorPenTab[i]);
2223 /*--------------------------------------------------------*\
2225 \*--------------------------------------------------------*/
2226 void DrawWindow::SelectWait(HANDLE& hWnd, int& x, int& y, int& button)
2232 GetMessage(&msg,NULL,0,0);
2233 while((msg.message != WM_RBUTTONDOWN && msg.message != WM_LBUTTONDOWN) ||
2234 ! ( Draw_IsConsoleSubsystem || IsChild(DrawWindow::hWndClientMDI,msg.hwnd)) )
2235 GetMessage(&msg,NULL,0,0);
2238 x = LOWORD(msg.lParam);
2239 y = HIWORD(msg.lParam);
2240 if (msg.message == WM_LBUTTONDOWN)
2246 /*--------------------------------------------------------*\
2248 \*--------------------------------------------------------*/
2249 void DrawWindow::SelectNoWait(HANDLE& hWnd, int& x, int& y, int& button)
2255 GetMessage(&msg,NULL,0,0);
2256 while((msg.message != WM_RBUTTONDOWN && msg.message != WM_LBUTTONDOWN &&
2257 msg.message != WM_MOUSEMOVE) ||
2258 ! ( Draw_IsConsoleSubsystem || IsChild(DrawWindow::hWndClientMDI,msg.hwnd) ) )
2259 GetMessage(&msg,NULL,0,0);
2261 x = LOWORD(msg.lParam);
2262 y = HIWORD(msg.lParam);
2263 switch (msg.message)
2265 case WM_LBUTTONDOWN :
2269 case WM_RBUTTONDOWN :
2279 Standard_Boolean DrawWindow::DefineColor (const Standard_Integer, const char*)
2281 return Standard_True;