1 // Created on: 1994-07-27
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 // include windows.h first to have all definitions available
22 #include <Standard_ErrorHandler.hxx>
25 #include <Draw_Interpretor.hxx>
26 #include <Draw_Appli.hxx>
27 #include <TCollection_AsciiString.hxx>
28 #include <Image_AlienPixMap.hxx>
30 extern Draw_Interpretor theCommands;
31 extern Standard_Boolean Draw_VirtualWindows;
32 static Tcl_Interp *interp; /* Interpreter for this application. */
35 *----------------------------------------------------------------------
39 * Issue a prompt on standard output, or invoke a script
40 * to issue the prompt.
46 * A prompt gets output, and a Tcl script may be evaluated
49 *----------------------------------------------------------------------
52 static void Prompt(Tcl_Interp *Interp, int partial)
56 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4))) && !defined(USE_NON_CONST)
57 const char *promptCmd;
62 Tcl_Channel outChannel, errChannel;
63 outChannel = Tcl_GetStdChannel(TCL_STDOUT);
64 promptCmd = Tcl_GetVar(Interp,(char*)
65 (partial ? "tcl_prompt2" : "tcl_prompt1"), TCL_GLOBAL_ONLY);
67 if (promptCmd == NULL) {
69 if (!partial && outChannel) {
70 Tcl_Write(outChannel, "% ", 2);
73 code = Tcl_Eval(Interp, promptCmd);
74 outChannel = Tcl_GetStdChannel(TCL_STDOUT);
75 errChannel = Tcl_GetStdChannel(TCL_STDERR);
78 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
79 Tcl_Write(errChannel, Tcl_GetStringResult(Interp), -1);
81 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);
95 #if !defined(_WIN32) && !defined(__WIN32__)
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>
135 #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
136 // use forward declaration for small subset of used Tk functions
137 // to workaround broken standard Tk framework installation within OS X SDKs
138 // which *HAS* X11 headers in Tk.framework but doesn't install them appropriately
140 typedef struct Tk_Window_* Tk_Window;
141 typedef const char* Tk_Uid;
143 extern "C" int Tk_Init (Tcl_Interp* interp);
144 extern "C" void Tk_MainLoop();
145 extern "C" Tk_Window Tk_MainWindow (Tcl_Interp* interp) ;
146 extern "C" Tk_Uid Tk_GetUid (const char* str);
147 extern "C" const char* Tk_SetAppName (Tk_Window tkwin, const char* name) ;
148 extern "C" void Tk_GeometryRequest (Tk_Window tkwin, int reqWidth, int reqHeight);
155 * Global variables used by the main program:
158 char *tcl_RcFileName = NULL; /* Name of a user-specific startup script
159 * to source if the application is being run
160 * interactively (e.g. "~/.wishrc"). Set
161 * by Tcl_AppInit. NULL means don't source
164 static Tcl_DString command; /* Used to assemble lines of terminal input
165 * into Tcl commands. */
166 static Tcl_DString line; /* Used to read the next line from the
168 //static char errorExitCmd[] = "exit 1";
171 * Forward declarations for procedures defined later in this file:
174 static void StdinProc (ClientData clientData, int mask);
176 static void Prompt (Tcl_Interp *Interp, int partial);
178 static Standard_Boolean tty; /* Non-zero means standard input is a
179 * terminal-like device. Zero means it's
182 Standard_Integer Draw_WindowScreen = 0;
183 Standard_Boolean Draw_BlackBackGround = Standard_True;
186 // Initialization of static variables of Draw_Window
187 //======================================================
188 Draw_Window* Draw_Window::firstWindow = NULL;
191 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
192 #include <X11/Xutil.h>
193 #include <Aspect_DisplayConnection.hxx>
195 static unsigned long thePixels[MAXCOLOR];
197 Display* Draw_WindowDisplay = NULL;
198 Colormap Draw_WindowColorMap;
199 static Handle(Aspect_DisplayConnection) Draw_DisplayConnection;
201 // Base_Window struct definition
202 //===================================
206 XSetWindowAttributes xswa;
209 //=======================================================================
210 //function : Draw_Window
212 //=======================================================================
213 Draw_Window::Draw_Window() :
214 base(*new Base_Window()),
219 myUseBuffer(Standard_False),
220 withWindowManager(Standard_True)
222 myMother = RootWindow(Draw_WindowDisplay,
225 if (firstWindow) firstWindow->previous = this;
229 //=======================================================================
230 //function : Draw_Window
232 //=======================================================================
233 Draw_Window::Draw_Window(Window mother) :
234 base(*new Base_Window()),
239 myUseBuffer(Standard_False),
240 withWindowManager(Standard_True)
244 if (firstWindow) firstWindow->previous = this;
248 //=======================================================================
249 //function : Draw_Window
251 //=======================================================================
252 Draw_Window::Draw_Window (const char* title,
253 Standard_Integer X, Standard_Integer Y,
254 Standard_Integer DX, Standard_Integer DY) :
255 base(*new Base_Window()),
260 myUseBuffer(Standard_False),
261 withWindowManager(Standard_True)
263 myMother = RootWindow(Draw_WindowDisplay,
266 if (firstWindow) firstWindow->previous = this;
272 //=======================================================================
273 //function : Draw_Window
275 //=======================================================================
276 Draw_Window::Draw_Window (const char* window ) :
277 base(*new Base_Window()),
282 myUseBuffer(Standard_False),
283 withWindowManager(Standard_True)
285 sscanf(window,"%lx",&win);
286 Standard_Integer X,Y,DX,DY;
288 if (firstWindow) firstWindow->previous = this;
297 //=======================================================================
298 //function : Draw_Window
300 //=======================================================================
301 Draw_Window::Draw_Window (Window mother,
303 Standard_Integer X, Standard_Integer Y,
304 Standard_Integer DX, Standard_Integer DY) :
305 base(*new Base_Window()),
310 myUseBuffer(Standard_False),
311 withWindowManager(Standard_True)
315 if (firstWindow) firstWindow->previous = this;
321 //=======================================================================
322 //function : ~Draw_Window
324 //=======================================================================
325 Draw_Window::~Draw_Window()
328 previous->next = next;
332 next->previous = previous;
336 XFreePixmap(Draw_WindowDisplay, myBuffer);
339 // Liberation pointer on Base_Window
343 //=======================================================================
346 //=======================================================================
347 void Draw_Window::Init(Standard_Integer X, Standard_Integer Y,
348 Standard_Integer DX, Standard_Integer DY)
350 unsigned long setmask;
352 if (Draw_BlackBackGround)
354 base.xswa.background_pixel = BlackPixel(Draw_WindowDisplay,Draw_WindowScreen);
355 base.xswa.border_pixel = WhitePixel(Draw_WindowDisplay,Draw_WindowScreen);
359 base.xswa.background_pixel = WhitePixel(Draw_WindowDisplay,Draw_WindowScreen);
360 base.xswa.border_pixel = BlackPixel(Draw_WindowDisplay,Draw_WindowScreen);
362 base.xswa.colormap = Draw_WindowColorMap;
363 setmask = CWBackPixel | CWBorderPixel ;
366 myHints.flags = USPosition;
372 win = XCreateWindow(Draw_WindowDisplay,
375 (unsigned int) DX,(unsigned int) DY,
377 DefaultDepth(Draw_WindowDisplay,Draw_WindowScreen),
379 DefaultVisual(Draw_WindowDisplay,Draw_WindowScreen),
381 XSelectInput(Draw_WindowDisplay, win, ButtonPressMask|ExposureMask|
382 StructureNotifyMask);
384 // advise to the window manager to place it where I need
385 XSetWMNormalHints(Draw_WindowDisplay,win,&myHints);
387 if (Draw_VirtualWindows)
389 myUseBuffer = Standard_True;
394 base.gc = XCreateGC(Draw_WindowDisplay, win, 0, NULL);
396 XSetPlaneMask(Draw_WindowDisplay,base.gc,AllPlanes);
397 XSetForeground(Draw_WindowDisplay,
398 base.gc, WhitePixel(Draw_WindowDisplay,Draw_WindowScreen));
399 XSetBackground(Draw_WindowDisplay,
400 base.gc, BlackPixel(Draw_WindowDisplay,Draw_WindowScreen));
401 // save in case of window recovery
403 base.xswa.backing_store = Always;
404 XChangeWindowAttributes(Draw_WindowDisplay, win,
405 CWBackingStore, &base.xswa);
407 XSetLineAttributes (Draw_WindowDisplay, base.gc,
408 0, LineSolid, CapButt, JoinMiter);
411 //=======================================================================
412 //function : InitBuffer
414 //=======================================================================
415 void Draw_Window::InitBuffer()
419 XFreePixmap (Draw_WindowDisplay, myBuffer);
421 XWindowAttributes winAttr;
422 XGetWindowAttributes (Draw_WindowDisplay, win, &winAttr);
423 myBuffer = XCreatePixmap (Draw_WindowDisplay, win, winAttr.width, winAttr.height, winAttr.depth);
425 else if (myBuffer != 0)
427 XFreePixmap (Draw_WindowDisplay, myBuffer);
432 //=======================================================================
433 //function : StopWinManager
435 //=======================================================================
436 void Draw_Window::StopWinManager()
439 XWindowAttributes winAttr;
440 XGetWindowAttributes (Draw_WindowDisplay, win, &winAttr);
444 myHints.flags = USPosition;
445 myHints.x = (int) 30;
446 myHints.y = (int) 100;
448 base.xswa.override_redirect = 1;
449 base.xswa.border_pixel = BlackPixel(Draw_WindowDisplay,
451 base.xswa.background_pixel = WhitePixel(Draw_WindowDisplay,
454 withWindowManager = Standard_False;
456 win = XCreateWindow(Draw_WindowDisplay, myMother,
457 winAttr.x, winAttr.y,
458 winAttr.width, winAttr.height,
460 CopyFromParent, InputOutput, CopyFromParent,
461 CWBorderPixel|CWOverrideRedirect|CWBackPixel, &base.xswa);
464 // adwise to the window manager to place it where I wish
465 XSetWMNormalHints(Draw_WindowDisplay,win,&myHints);
467 // all masks of the old window are reassigned to the new one.
468 XSelectInput(Draw_WindowDisplay,win,winAttr.your_event_mask);
471 //=======================================================================
472 //function : SetPosition
474 //=======================================================================
475 void Draw_Window::SetPosition(Standard_Integer NewXpos,
476 Standard_Integer NewYpos)
478 Standard_Integer x,y;
481 if ( (x != NewXpos) || (y != NewYpos) )
482 XMoveWindow(Draw_WindowDisplay, win, NewXpos, NewYpos);
485 //=======================================================================
486 //function : SetDimension
488 //=======================================================================
489 void Draw_Window::SetDimension(Standard_Integer NewDx,
490 Standard_Integer NewDy)
492 if ( (NewDx != WidthWin() ) || (NewDy != HeightWin() ) )
493 XResizeWindow(Draw_WindowDisplay, win, NewDx, NewDy);
496 //=======================================================================
497 //function : GetPosition
499 //=======================================================================
500 void Draw_Window::GetPosition(Standard_Integer &PosX,
501 Standard_Integer &PosY)
503 XWindowAttributes winAttr;
504 XGetWindowAttributes(Draw_WindowDisplay, win, &winAttr);
510 //=======================================================================
511 //function : HeightWin
513 //=======================================================================
514 Standard_Integer Draw_Window::HeightWin() const
517 XWindowAttributes winAttr;
518 XGetWindowAttributes(Draw_WindowDisplay, win, &winAttr);
524 //=======================================================================
525 //function : WidthWin
527 //=======================================================================
528 Standard_Integer Draw_Window::WidthWin() const
531 XWindowAttributes winAttr;
532 XGetWindowAttributes(Draw_WindowDisplay, win, &winAttr);
538 //=======================================================================
539 //function : SetTitle
541 //=======================================================================
542 void Draw_Window::SetTitle(const char* title)
544 XStoreName(Draw_WindowDisplay, win, title);
547 //=======================================================================
548 //function : GetTitle
550 //=======================================================================
551 char* Draw_Window::GetTitle()
554 XFetchName(Draw_WindowDisplay, win, &title);
558 //=======================================================================
559 //function : GetDrawable
561 //=======================================================================
562 Drawable Draw_Window::GetDrawable() const
564 return myUseBuffer ? myBuffer : win;
567 //=======================================================================
568 //function :DefineColor
570 //=======================================================================
571 Standard_Boolean Draw_Window::DefineColor(const Standard_Integer i, const char* colorName)
575 if (!XParseColor(Draw_WindowDisplay,Draw_WindowColorMap,colorName,&color))
576 return Standard_False;
577 if (!XAllocColor(Draw_WindowDisplay,Draw_WindowColorMap,&color))
578 return Standard_False;
579 thePixels[i % MAXCOLOR] = color.pixel;
580 return Standard_True;
583 //=======================================================================
584 //function : DisplayWindow
586 //=======================================================================
587 void Draw_Window::DisplayWindow()
589 if (Draw_VirtualWindows)
595 XMapRaised(Draw_WindowDisplay, win);
597 XFlush(Draw_WindowDisplay);
600 //=======================================================================
603 //=======================================================================
604 void Draw_Window::Hide()
606 XUnmapWindow(Draw_WindowDisplay, win);
609 //=======================================================================
612 //=======================================================================
613 void Draw_Window::Destroy()
615 XFreeGC (Draw_WindowDisplay, base.gc);
616 XDestroyWindow(Draw_WindowDisplay, win);
620 XFreePixmap(Draw_WindowDisplay, myBuffer);
625 //=======================================================================
628 //=======================================================================
629 void Draw_Window::Clear()
633 // XClearArea only applicable for windows
634 XGCValues currValues;
635 XGetGCValues(Draw_WindowDisplay, base.gc, GCBackground | GCForeground, &currValues);
636 XSetForeground(Draw_WindowDisplay, base.gc, currValues.background);
637 XFillRectangle(Draw_WindowDisplay, myBuffer, base.gc, 0, 0, WidthWin(), HeightWin());
638 XSetForeground(Draw_WindowDisplay, base.gc, currValues.foreground);
642 XClearArea(Draw_WindowDisplay, win, 0, 0, 0, 0, False);
646 //=======================================================================
649 //=======================================================================
650 void Draw_Window::Flush()
652 XFlush(Draw_WindowDisplay);
655 //=======================================================================
656 //function : DrawString
658 //=======================================================================
659 void Draw_Window::DrawString(int X, int Y, char *text)
661 XDrawString(Draw_WindowDisplay, GetDrawable(), base.gc, X, Y, text, strlen(text));
664 //=======================================================================
665 //function : DrawSegments
667 //=======================================================================
668 void Draw_Window::DrawSegments(Segment *tab, int nbElem)
670 XDrawSegments(Draw_WindowDisplay, GetDrawable(), base.gc, (XSegment*) tab, nbElem);
673 //=======================================================================
676 //=======================================================================
677 void Draw_Window::Redraw()
680 XCopyArea (Draw_WindowDisplay,
681 myBuffer, win, // source, destination Drawables
684 WidthWin(), HeightWin(),
685 0, 0); // destination x, y
689 //=======================================================================
690 //function : SetColor
692 //=======================================================================
693 void Draw_Window::SetColor(Standard_Integer color)
695 XSetForeground(Draw_WindowDisplay, base.gc, thePixels[color]);
698 //=======================================================================
701 //=======================================================================
702 void Draw_Window::SetMode( int mode)
704 XSetFunction(Draw_WindowDisplay, base.gc, mode);
707 //=======================================================================
710 //=======================================================================
711 Standard_Boolean Draw_Window::Save (const char* theFileName) const
713 // make sure all draw operations done
714 XSync (Draw_WindowDisplay, True);
717 XWindowAttributes winAttr;
718 XGetWindowAttributes (Draw_WindowDisplay, win, &winAttr);
722 // make sure that the whole window fit on display to prevent BadMatch error
723 XWindowAttributes winAttrRoot;
724 XGetWindowAttributes (Draw_WindowDisplay, XRootWindowOfScreen (winAttr.screen), &winAttrRoot);
726 Window winChildDummy;
729 XTranslateCoordinates (Draw_WindowDisplay, win, XRootWindowOfScreen (winAttr.screen),
730 0, 0, &winLeft, &winTop, &winChildDummy);
732 if (((winLeft + winAttr.width) > winAttrRoot.width) || winLeft < winAttrRoot.x ||
733 ((winTop + winAttr.height) > winAttrRoot.height) || winTop < winAttrRoot.y)
735 std::cerr << "The window not fully visible! Can't create the snapshot.\n";
736 return Standard_False;
741 if (XMatchVisualInfo (Draw_WindowDisplay, Draw_WindowScreen, 32, TrueColor, &aVInfo) == 0
742 && XMatchVisualInfo (Draw_WindowDisplay, Draw_WindowScreen, 24, TrueColor, &aVInfo) == 0)
744 std::cerr << "24-bit TrueColor visual is not supported by server!\n";
745 return Standard_False;
748 Image_AlienPixMap anImage;
749 bool isBigEndian = Image_PixMap::IsBigEndianHost();
750 const Standard_Size aSizeRowBytes = Standard_Size(winAttr.width) * 4;
751 if (!anImage.InitTrash (isBigEndian ? Image_PixMap::ImgRGB32 : Image_PixMap::ImgBGR32,
752 Standard_Size(winAttr.width), Standard_Size(winAttr.height), aSizeRowBytes))
754 return Standard_False;
756 anImage.SetTopDown (true);
758 XImage* anXImage = XCreateImage (Draw_WindowDisplay, aVInfo.visual,
759 32, ZPixmap, 0, (char* )anImage.ChangeData(), winAttr.width, winAttr.height, 32, int(aSizeRowBytes));
760 anXImage->bitmap_bit_order = anXImage->byte_order = (isBigEndian ? MSBFirst : LSBFirst);
761 if (XGetSubImage (Draw_WindowDisplay, GetDrawable(),
762 0, 0, winAttr.width, winAttr.height,
763 AllPlanes, ZPixmap, anXImage, 0, 0) == NULL)
765 anXImage->data = NULL;
766 XDestroyImage (anXImage);
767 return Standard_False;
771 anXImage->data = NULL;
772 XDestroyImage (anXImage);
775 return anImage.Save (theFileName);
778 //=======================================================================
781 //=======================================================================
783 void Draw_Window::Wait (Standard_Boolean wait)
787 XSelectInput(Draw_WindowDisplay,win,
788 ButtonPressMask|ExposureMask | StructureNotifyMask |
792 XSelectInput(Draw_WindowDisplay,win,
793 ButtonPressMask|ExposureMask | StructureNotifyMask);
797 //=======================================================================
798 //function : ProcessEvent
800 //=======================================================================
802 void ProcessEvent(Draw_Window& win, XEvent& xev)
804 Standard_Integer X,Y,button;
819 button = xev.xbutton.button;
820 win.WButtonPress(X,Y,button);
826 button = xev.xbutton.button;
827 win.WButtonRelease(X,Y,button);
831 XLookupString(&(xev.xkey),
841 win.WMotionNotify(X,Y);
844 case ConfigureNotify :
845 if (win.withWindowManager)
846 win.WConfigureNotify(xev.xconfigure.x, xev.xconfigure.y,
847 xev.xconfigure.width,
848 xev.xconfigure.height);
858 //=======================================================================
861 //=======================================================================
862 void Draw_Window::WExpose()
866 //=======================================================================
867 //function : WButtonPress
869 //=======================================================================
870 void Draw_Window::WButtonPress(const Standard_Integer,
871 const Standard_Integer,
872 const Standard_Integer&)
876 //=======================================================================
877 //function : WButtonRelease
879 //=======================================================================
880 void Draw_Window::WButtonRelease(const Standard_Integer,
881 const Standard_Integer,
882 const Standard_Integer&)
886 /**************************
887 //=======================================================================
888 //function : WKeyPress
890 //=======================================================================
892 void Draw_Window::WKeyPress(char, KeySym&)
895 ***************************/
897 //=======================================================================
898 //function : WMotionNotify
900 //=======================================================================
901 void Draw_Window::WMotionNotify(const Standard_Integer ,
902 const Standard_Integer )
906 //=======================================================================
907 //function : WConfigureNotify
909 //=======================================================================
911 void Draw_Window::WConfigureNotify(const Standard_Integer,
912 const Standard_Integer,
913 const Standard_Integer,
914 const Standard_Integer)
918 //=======================================================================
919 //function : WUnmapNotify
921 //=======================================================================
923 void Draw_Window::WUnmapNotify()
928 //======================================================
929 // funtion : ProcessEvents
930 // purpose : process pending X events
931 //======================================================
933 static void ProcessEvents(ClientData,int)
937 while (XPending(Draw_WindowDisplay)) {
942 XNextEvent(Draw_WindowDisplay,&xev);
944 /* search the window in the window list */
945 Draw_Window* w = Draw_Window::firstWindow;
946 Standard_Integer found=0;
948 if (xev.xany.window == w->win) {
949 ProcessEvent(*w, xev);
956 Tk_HandleEvent(&xev);
961 //======================================================
962 // funtion : GetNextEvent()
964 //======================================================
965 void GetNextEvent(Event& ev)
968 XNextEvent(Draw_WindowDisplay, &xev);
973 ev.window = xev.xbutton.window;
974 ev.button = xev.xbutton.button;
975 ev.x = xev.xbutton.x;
976 ev.y = xev.xbutton.y;
981 ev.window = xev.xmotion.window;
983 ev.x = xev.xmotion.x;
984 ev.y = xev.xmotion.y;
990 //======================================================
991 // funtion :Run_Appli
993 //======================================================
996 static Standard_Boolean(*Interprete) (const char*);
998 void Run_Appli(Standard_Boolean (*interprete) (const char*))
1000 Tcl_Channel outChannel, inChannel ;
1001 Interprete = interprete;
1006 * Commands will come from standard input, so set up an event
1007 * handler for standard input. If the input device is aEvaluate the
1008 * .rc file, if one has been specified, set up an event handler
1009 * for standard input, and print a prompt if the input
1010 * device is a terminal.
1012 inChannel = Tcl_GetStdChannel(TCL_STDIN);
1014 Tcl_CreateChannelHandler(inChannel, TCL_READABLE, StdinProc,
1015 (ClientData) inChannel);
1018 // Create a handler for the draw display
1020 // Adding of the casting into void* to be able to compile on AO1
1021 // ConnectionNumber(Draw_WindowDisplay) is an int 32 bits
1022 // (void*) is a pointer 64 bits ???????
1024 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
1025 #if TCL_MAJOR_VERSION < 8
1026 Tk_CreateFileHandler((void*) ConnectionNumber(Draw_WindowDisplay),
1027 TK_READABLE, ProcessEvents,(ClientData) 0 );
1029 Tk_CreateFileHandler(ConnectionNumber(Draw_WindowDisplay),
1030 TK_READABLE, ProcessEvents,(ClientData) 0 );
1036 if (tty) Prompt(theCommands.Interp(), 0);
1037 Prompt(theCommands.Interp(), 0);
1039 outChannel = Tcl_GetStdChannel(TCL_STDOUT);
1041 Tcl_Flush(outChannel);
1043 Tcl_DStringInit(&command);
1046 * Loop infinitely, waiting for commands to execute. When there
1047 * are no windows left, Tk_MainLoop returns and we exit.
1052 if (Draw_VirtualWindows) {
1053 // main window will never shown
1054 // but main loop will parse all Xlib messages
1055 Tcl_Eval(theCommands.Interp(), "wm withdraw .");
1062 Standard_Integer count = ConnectionNumber(Draw_WindowDisplay);
1063 Standard_Integer numfd;
1067 FD_SET(count,&readset);
1069 numfd = select(count+1,(Integer*)&readset,NULL,NULL,NULL);
1071 numfd = select(count+1,&readset,NULL,NULL,NULL);
1073 if (FD_ISSET(0,&readset)) StdinProc((ClientData)0,0);
1074 if (FD_ISSET(count,&readset)) ProcessEvents((ClientData)0,0);
1080 //======================================================
1081 // funtion : Init_Appli()
1083 //======================================================
1084 Standard_Boolean Init_Appli()
1087 interp = theCommands.Interp();
1093 } catch (Standard_Failure) {
1094 cout <<" Pb au lancement de TK_Init "<<endl;
1097 Tcl_StaticPackage(interp, "Tk", Tk_Init, (Tcl_PackageInitProc *) NULL);
1099 Tk_Window aMainWindow = Tk_MainWindow(interp) ;
1100 if (aMainWindow == NULL) {
1101 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
1102 fprintf(stderr, "%s\n", Tcl_GetStringResult(interp));
1104 fprintf(stderr, "%s\n", interp->result);
1108 #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
1109 Tk_SetAppName(aMainWindow, "Draw");
1111 Tk_Name(aMainWindow) = Tk_GetUid(Tk_SetAppName(aMainWindow, "Draw"));
1114 Tk_GeometryRequest (aMainWindow, 200, 200);
1116 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
1117 if (Draw_DisplayConnection.IsNull())
1121 Draw_DisplayConnection = new Aspect_DisplayConnection();
1123 catch (Standard_Failure)
1125 std::cout << "Cannot open display. Interpret commands in batch mode." << std::endl;
1126 return Standard_False;
1129 if (Draw_WindowDisplay == NULL)
1131 Draw_WindowDisplay = Draw_DisplayConnection->GetDisplay();
1134 // synchronize the display server : could be done within Tk_Init
1136 XSynchronize(Draw_WindowDisplay, True);
1137 XSetInputFocus(Draw_WindowDisplay,
1139 RevertToPointerRoot,
1142 Draw_WindowScreen = DefaultScreen(Draw_WindowDisplay);
1143 Draw_WindowColorMap = DefaultColormap(Draw_WindowDisplay,
1148 Tcl_SetVar(interp,"tcl_interactive",(char*)(tty ? "1" : "0"), TCL_GLOBAL_ONLY);
1149 // Tcl_SetVar(interp,"tcl_interactive",tty ? "1" : "0", TCL_GLOBAL_ONLY);
1150 return Standard_True;
1153 //======================================================
1154 // funtion : Destroy_Appli()
1156 //======================================================
1157 void Destroy_Appli()
1159 //XCloseDisplay(Draw_WindowDisplay);
1163 *----------------------------------------------------------------------
1167 * This procedure is invoked by the event dispatcher whenever
1168 * standard input becomes readable. It grabs the next line of
1169 * input characters, adds them to a command being assembled, and
1170 * executes the command if it's complete.
1176 * Could be almost arbitrary, depending on the command that's
1179 *----------------------------------------------------------------------
1183 //static void StdinProc(ClientData clientData, int mask)
1184 static void StdinProc(ClientData clientData, int )
1186 static int gotPartial = 0;
1190 Tcl_Channel chan = (Tcl_Channel) clientData;
1192 // MSV Nov 2, 2001: patch for TCL 8.3: initialize line to avoid exception
1193 // when first user input is an empty string
1194 Tcl_DStringFree(&line);
1195 count = Tcl_Gets(chan, &line);
1198 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)))
1199 Tcl_DString linetmp;
1200 Tcl_DStringInit(&linetmp);
1201 Tcl_UniChar * UniCharString;
1202 UniCharString = Tcl_UtfToUniCharDString(Tcl_DStringValue(&line),-1,&linetmp);
1203 Standard_Integer l = Tcl_UniCharLen(UniCharString);
1204 TCollection_AsciiString AsciiString("");
1205 Standard_Character Character;
1207 for (i=0; i<l; i++) {
1208 Character = UniCharString[i];
1209 AsciiString.AssignCat(Character);
1211 Tcl_DStringInit(&line);
1212 Tcl_DStringAppend(&line, AsciiString.ToCString(), -1);
1219 Tcl_DeleteChannelHandler(chan, StdinProc, (ClientData) chan);
1227 (void) Tcl_DStringAppend(&command, Tcl_DStringValue(&line), -1);
1228 cmd = Tcl_DStringAppend(&command, "\n", -1);
1229 Tcl_DStringFree(&line);
1232 if (!Tcl_CommandComplete(cmd)) {
1239 * Disable the stdin channel handler while evaluating the command;
1240 * otherwise if the command re-enters the event loop we might
1241 * process commands from stdin before the current command is
1242 * finished. Among other things, this will trash the text of the
1243 * command being evaluated.
1246 Tcl_CreateChannelHandler(chan, 0, StdinProc, (ClientData) chan);
1250 * Disable the stdin file handler while evaluating the command;
1251 * otherwise if the command re-enters the event loop we might
1252 * process commands from stdin before the current command is
1253 * finished. Among other things, this will trash the text of the
1254 * command being evaluated.
1258 // Tk_CreateFileHandler(0, 0, StdinProc, (ClientData) 0);
1261 // xab average to avoid an output SIGBUS of DRAW
1262 // to ultimately prescise or remove once
1263 // the problem of free on the global variable at the average
1270 Tcl_CreateChannelHandler(chan, TCL_READABLE, StdinProc,
1272 Tcl_DStringFree(&command);
1279 if (tty) Prompt(interp, gotPartial);
1281 } catch (Standard_Failure) {}
1287 // Source Specifique WNT
1289 /****************************************************\
1292 \****************************************************/
1294 #include "Draw_Window.hxx"
1295 #include "DrawRessource.h"
1298 #include <Draw_Appli.hxx>
1305 // Position of information in the extra memory
1307 // indicates SUBSYSTEM:CONSOLE linker option, to be set to True in main()
1309 Standard_Boolean Draw_IsConsoleSubsystem = Standard_False;
1312 Standard_Boolean Draw_BlackBackGround = Standard_True;
1314 // Creation of color stylos
1315 HPEN colorPenTab[MAXCOLOR] = {CreatePen(PS_SOLID, PENWIDTH, RGB(255,255,255)),
1316 CreatePen(PS_SOLID, PENWIDTH, RGB(255,0,0)),
1317 CreatePen(PS_SOLID, PENWIDTH, RGB(0,255,0)),
1318 CreatePen(PS_SOLID, PENWIDTH, RGB(0,0,255)),
1319 CreatePen(PS_SOLID, PENWIDTH, RGB(0,255,255)),
1320 CreatePen(PS_SOLID, PENWIDTH, RGB(255,215,0)),
1321 CreatePen(PS_SOLID, PENWIDTH, RGB(255,0,255)),
1322 CreatePen(PS_SOLID, PENWIDTH, RGB(255,52,179)),
1323 CreatePen(PS_SOLID, PENWIDTH, RGB(255,165,0)),
1324 CreatePen(PS_SOLID, PENWIDTH, RGB(255,228,225)),
1325 CreatePen(PS_SOLID, PENWIDTH, RGB(255,160,122)),
1326 CreatePen(PS_SOLID, PENWIDTH, RGB(199,21,133)),
1327 CreatePen(PS_SOLID, PENWIDTH, RGB(255,255,0)),
1328 CreatePen(PS_SOLID, PENWIDTH, RGB(240,230,140)),
1329 CreatePen(PS_SOLID, PENWIDTH, RGB(255,127,80))};
1331 // Correspondance mode X11 and WINDOWS NT
1332 int modeTab[16] = {R2_BLACK, R2_MASKPEN, R2_MASKPENNOT, R2_COPYPEN,
1333 R2_MASKNOTPEN, R2_NOP, R2_XORPEN, R2_MERGEPEN,
1334 R2_NOTMASKPEN, R2_NOTXORPEN, R2_NOT, R2_MERGEPENNOT,
1335 R2_NOTCOPYPEN, R2_MERGENOTPEN, R2_NOTMERGEPEN, R2_WHITE};
1337 /*--------------------------------------------------------*\
1338 | CREATE DRAW WINDOW PROCEDURE
1339 \*--------------------------------------------------------*/
1340 HWND DrawWindow::CreateDrawWindow(HWND hWndClient, int nitem)
1342 if (Draw_IsConsoleSubsystem) {
1343 HWND aWin = CreateWindow (DRAWCLASS, DRAWTITLE,
1344 WS_OVERLAPPEDWINDOW,
1346 NULL, NULL,::GetModuleHandle(NULL), NULL);
1347 if (!Draw_VirtualWindows)
1349 SetWindowPos(aWin, HWND_TOPMOST, 1,1,1,1, SWP_NOMOVE);
1350 SetWindowPos(aWin, HWND_NOTOPMOST, 1,1,1,1, SWP_NOMOVE);
1356 hInstance = (HANDLE)GetWindowLongPtr(hWndClient,GWLP_HINSTANCE);
1358 return CreateMDIWindow(DRAWCLASS, DRAWTITLE,
1359 WS_CAPTION | WS_CHILD | WS_THICKFRAME,
1361 hWndClient, (HINSTANCE)hInstance, nitem);
1366 /*--------------------------------------------------------*\
1367 | DRAW WINDOW PROCEDURE
1368 \*--------------------------------------------------------*/
1369 LRESULT APIENTRY DrawWindow::DrawProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
1371 DrawWindow* localObjet = (DrawWindow*)GetWindowLongPtr(hWnd, CLIENTWND);
1374 if (Draw_IsConsoleSubsystem)
1375 return (DefWindowProc(hWnd, wMsg, wParam, lParam));
1377 return(DefMDIChildProc(hWnd, wMsg, wParam, lParam));
1385 BeginPaint(hWnd, &ps);
1386 if (localObjet->GetUseBuffer())
1387 localObjet->Redraw();
1389 localObjet->WExpose();
1390 EndPaint(hWnd, &ps);
1395 if (localObjet->GetUseBuffer()) {
1396 localObjet->InitBuffer();
1397 localObjet->WExpose();
1398 localObjet->Redraw();
1403 if (Draw_IsConsoleSubsystem)
1404 return (DefWindowProc(hWnd, wMsg, wParam, lParam));
1406 return(DefMDIChildProc(hWnd, wMsg, wParam, lParam));
1413 ** IMPLEMENTATION of the CLASS DRAWWINDOW
1416 /*--------------------------------------------------------*\
1417 | Initialization of static variables of DrawWindow
1418 \*--------------------------------------------------------*/
1420 DrawWindow* DrawWindow::firstWindow = NULL;
1421 HWND DrawWindow::hWndClientMDI = 0;
1423 /*--------------------------------------------------------*\
1424 | Constructors of Draw_Window
1425 \*--------------------------------------------------------*/
1427 // Default Constructor
1428 //________________________
1429 DrawWindow::DrawWindow() :
1434 myUseBuffer(Standard_False)
1436 if (firstWindow) firstWindow->previous = this;
1440 //________________________
1441 DrawWindow::DrawWindow(char* title,
1442 Standard_Integer X, Standard_Integer Y,
1443 Standard_Integer dX,Standard_Integer dY) :
1444 win(0), next(firstWindow), previous(NULL), myMemHbm(NULL), myUseBuffer(Standard_False)
1446 if (firstWindow) firstWindow->previous = this;
1451 DrawWindow::DrawWindow(char* title,
1452 Standard_Integer X, Standard_Integer Y,
1453 Standard_Integer dX,Standard_Integer dY,
1455 win(theWin),next(firstWindow), previous(NULL), myMemHbm(NULL), myUseBuffer(Standard_False)
1457 if (firstWindow) firstWindow->previous = this;
1465 /*--------------------------------------------------------*\
1466 | Destructor of DrawWindow
1467 \*--------------------------------------------------------*/
1468 DrawWindow::~DrawWindow()
1471 previous->next = next;
1475 next->previous = previous;
1477 // Delete 'off-screen drawing'-related objects
1479 DeleteObject(myMemHbm);
1486 /*--------------------------------------------------------*\
1488 \*--------------------------------------------------------*/
1489 void DrawWindow::Init(Standard_Integer theXLeft, Standard_Integer theYTop,
1490 Standard_Integer theWidth, Standard_Integer theHeight)
1494 win = CreateDrawWindow(hWndClientMDI, 0);
1497 // include decorations in the window dimensions
1498 // to reproduce same behaviour of Xlib window.
1499 DWORD aWinStyle = GetWindowLong (win, GWL_STYLE);
1500 DWORD aWinStyleEx = GetWindowLong (win, GWL_EXSTYLE);
1501 HMENU aMenu = GetMenu (win);
1504 aRect.top = theYTop;
1505 aRect.bottom = theYTop + theHeight;
1506 aRect.left = theXLeft;
1507 aRect.right = theXLeft + theWidth;
1508 AdjustWindowRectEx (&aRect, aWinStyle, aMenu != NULL ? TRUE : FALSE, aWinStyleEx);
1510 SetPosition (aRect.left, aRect.top);
1511 SetDimension (aRect.right - aRect.left, aRect.bottom - aRect.top);
1512 // Save the pointer at the instance associated to the window
1513 SetWindowLongPtr(win, CLIENTWND, (LONG_PTR)this);
1514 HDC hDC = GetDC(win);
1515 SetBkColor(hDC, RGB(0, 0, 0));
1518 SelectObject(hDC, colorPenTab[myCurrPen]); // Default pencil
1519 SelectObject(hDC, GetStockObject(BLACK_BRUSH));
1520 SetTextColor(hDC, RGB(0,0,255));
1521 ReleaseDC(win, hDC);
1523 if (Draw_VirtualWindows)
1525 // create a virtual window
1526 SetUseBuffer (Standard_True);
1530 /*--------------------------------------------------------*\
1532 \*--------------------------------------------------------*/
1533 void DrawWindow::SetUseBuffer(Standard_Boolean use)
1539 /*--------------------------------------------------------*\
1541 \*--------------------------------------------------------*/
1542 void DrawWindow::InitBuffer()
1546 HDC hDC = GetDC(win);
1547 GetClientRect(win, &rc);
1550 GetObject(myMemHbm, sizeof(BITMAP), &aBmp);
1551 if (rc.right-rc.left == aBmp.bmWidth && rc.bottom-rc.top == aBmp.bmHeight) return;
1552 DeleteObject(myMemHbm);
1554 myMemHbm = (HBITMAP)CreateCompatibleBitmap(hDC,
1557 HDC aMemDC = GetMemDC(hDC);
1558 FillRect(aMemDC, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH));
1559 ReleaseMemDC(aMemDC);
1560 ReleaseDC(win, hDC);
1564 DeleteObject(myMemHbm);
1570 /*--------------------------------------------------------*\
1572 \*--------------------------------------------------------*/
1573 HDC DrawWindow::GetMemDC(HDC theWinDC)
1575 if (!myUseBuffer) return NULL;
1577 HDC aWorkDC = CreateCompatibleDC(theWinDC);
1578 myOldHbm = (HBITMAP)SelectObject(aWorkDC, myMemHbm);
1579 SetROP2(aWorkDC, modeTab[myCurrMode]);
1580 SelectObject(aWorkDC, colorPenTab[myCurrPen]);
1581 SetBkColor(aWorkDC, RGB(0, 0, 0));
1582 SelectObject(aWorkDC, GetStockObject(BLACK_BRUSH));
1583 SetTextColor(aWorkDC, RGB(0,0,255));
1588 /*--------------------------------------------------------*\
1590 \*--------------------------------------------------------*/
1591 void DrawWindow::ReleaseMemDC(HDC theMemDC)
1593 if (!myUseBuffer || !theMemDC) return;
1595 if (myOldHbm) SelectObject(theMemDC, myOldHbm);
1600 /*--------------------------------------------------------*\
1602 \*--------------------------------------------------------*/
1603 void DrawWindow::SetPosition(Standard_Integer posX, Standard_Integer posY)
1605 SetWindowPos(win, 0,
1608 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
1612 /*--------------------------------------------------------*\
1614 \*--------------------------------------------------------*/
1615 void DrawWindow::SetDimension(Standard_Integer dimX, Standard_Integer dimY)
1617 SetWindowPos(win, 0,
1620 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
1624 /*--------------------------------------------------------*\
1626 \*--------------------------------------------------------*/
1627 void DrawWindow::GetPosition(Standard_Integer &dimX,
1628 Standard_Integer &dimY)
1631 GetWindowRect(win, &rect);
1634 point.x = rect.left;
1637 ScreenToClient(hWndClientMDI, &point);
1643 /*--------------------------------------------------------*\
1645 \*--------------------------------------------------------*/
1646 Standard_Integer DrawWindow::HeightWin() const
1649 GetClientRect(win, &rect);
1650 return(rect.bottom-rect.top);
1654 /*--------------------------------------------------------*\
1656 \*--------------------------------------------------------*/
1657 Standard_Integer DrawWindow::WidthWin() const
1660 GetClientRect(win, &rect);
1661 return(rect.right-rect.left);
1665 /*--------------------------------------------------------*\
1667 \*--------------------------------------------------------*/
1668 void DrawWindow::SetTitle(char* title)
1670 SetWindowText(win, title);
1674 /*--------------------------------------------------------*\
1676 | Attention do not forget to unallocate the memory
1677 \*--------------------------------------------------------*/
1678 char* DrawWindow::GetTitle()
1680 char* title=new char[31];
1681 GetWindowText(win, title, 30);
1686 /*--------------------------------------------------------*\
1688 \*--------------------------------------------------------*/
1689 void DrawWindow::DisplayWindow()
1691 if (Draw_VirtualWindows)
1695 ShowWindow (win, SW_SHOW);
1700 /*--------------------------------------------------------*\
1702 \*--------------------------------------------------------*/
1703 void DrawWindow::Hide()
1705 ShowWindow(win, SW_HIDE);
1709 /*--------------------------------------------------------*\
1711 \*--------------------------------------------------------*/
1712 void DrawWindow::Destroy()
1719 /*--------------------------------------------------------*\
1721 \*--------------------------------------------------------*/
1722 void DrawWindow::Clear()
1724 HDC hDC = GetDC(win);
1725 HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
1728 SelectObject(aWorkDC,GetStockObject(BLACK_PEN));
1729 Rectangle(aWorkDC, 0, 0, WidthWin(), HeightWin());
1730 RestoreDC(aWorkDC,-1);
1732 if (myUseBuffer) ReleaseMemDC(aWorkDC);
1736 /*--------------------------------------------------------*\
1738 \*--------------------------------------------------------*/
1739 static Standard_Boolean SaveBitmap (HBITMAP theHBitmap,
1740 const char* theFileName)
1742 // Get informations about the bitmap
1744 if (GetObject (theHBitmap, sizeof(BITMAP), (LPSTR )&aBitmap) == 0)
1746 return Standard_False;
1749 Image_AlienPixMap anImage;
1750 const Standard_Size aSizeRowBytes = Standard_Size(aBitmap.bmWidth) * 4;
1751 if (!anImage.InitTrash (Image_PixMap::ImgBGR32, Standard_Size(aBitmap.bmWidth), Standard_Size(aBitmap.bmHeight), aSizeRowBytes))
1753 return Standard_False;
1755 anImage.SetTopDown (false);
1758 BITMAPINFOHEADER aBitmapInfo;
1759 memset (&aBitmapInfo, 0, sizeof(BITMAPINFOHEADER));
1760 aBitmapInfo.biSize = sizeof(BITMAPINFOHEADER);
1761 aBitmapInfo.biWidth = aBitmap.bmWidth;
1762 aBitmapInfo.biHeight = aBitmap.bmHeight; // positive means bottom-up!
1763 aBitmapInfo.biPlanes = 1;
1764 aBitmapInfo.biBitCount = 32; // use 32bit for automatic word-alignment per row
1765 aBitmapInfo.biCompression = BI_RGB;
1768 HDC aDC = GetDC (NULL);
1769 Standard_Boolean isSuccess = GetDIBits (aDC, theHBitmap,
1770 0, // first scan line to set
1771 aBitmap.bmHeight, // number of scan lines to copy
1772 anImage.ChangeData(), // array for bitmap bits
1773 (LPBITMAPINFO )&aBitmapInfo, // bitmap data info
1774 DIB_RGB_COLORS) != 0;
1775 ReleaseDC (NULL, aDC);
1776 return isSuccess && anImage.Save (theFileName);
1779 /*--------------------------------------------------------*\
1781 \*--------------------------------------------------------*/
1782 Standard_Boolean DrawWindow::Save (const char* theFileName) const
1786 return SaveBitmap (myMemHbm, theFileName);
1790 GetClientRect (win, &aRect);
1791 int aWidth = aRect.right - aRect.left;
1792 int aHeight = aRect.bottom - aRect.top;
1795 HDC aDstDC = GetDC (NULL);
1796 HDC aSrcDC = GetDC (win); // we copy only client area
1797 HDC aMemDC = CreateCompatibleDC (aDstDC);
1799 // Copy the screen to the bitmap
1800 HBITMAP anHBitmapDump = CreateCompatibleBitmap (aDstDC, aWidth, aHeight);
1801 HBITMAP anHBitmapOld = (HBITMAP )SelectObject (aMemDC, anHBitmapDump);
1802 BitBlt (aMemDC, 0, 0, aWidth, aHeight, aSrcDC, 0, 0, SRCCOPY);
1804 Standard_Boolean isSuccess = SaveBitmap (anHBitmapDump, theFileName);
1807 DeleteObject (SelectObject (aMemDC, anHBitmapOld));
1813 /*--------------------------------------------------------*\
1815 \*--------------------------------------------------------*/
1816 void DrawWindow::DrawString(int x,int y, char* text)
1818 HDC hDC = GetDC(win);
1819 HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
1821 TextOut(aWorkDC, x, y, text, (int )strlen(text));
1823 if (myUseBuffer) ReleaseMemDC(aWorkDC);
1827 /*--------------------------------------------------------*\
1829 \*--------------------------------------------------------*/
1830 void DrawWindow::DrawSegments(Segment *tab, int nbElem)
1832 HDC hDC = GetDC(win);
1833 HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
1835 for(int i = 0 ; i < nbElem ; i++)
1837 MoveToEx(aWorkDC, tab[i].x1, tab[i].y1, NULL);
1838 LineTo(aWorkDC, tab[i].x2, tab[i].y2);
1841 if (myUseBuffer) ReleaseMemDC(aWorkDC);
1845 /*--------------------------------------------------------*\
1847 \*--------------------------------------------------------*/
1848 void DrawWindow::Redraw()
1851 HDC hDC = GetDC(win);
1853 GetClientRect(win, &rc);
1854 HDC aMemDC = GetMemDC(hDC);
1857 rc.right-rc.left, rc.bottom-rc.top,
1860 ReleaseMemDC(aMemDC);
1865 /*--------------------------------------------------------*\
1867 \*--------------------------------------------------------*/
1868 void DrawWindow::SetMode(int mode)
1870 HDC hDC = GetDC(win);
1872 SetROP2(hDC, modeTab[mode]);
1877 /*--------------------------------------------------------*\
1879 \*--------------------------------------------------------*/
1880 void DrawWindow::SetColor(Standard_Integer color)
1882 HDC hDC = GetDC(win);
1884 SelectObject(hDC,colorPenTab[color]);
1889 /*--------------------------------------------------------*\
1891 \*--------------------------------------------------------*/
1892 void DrawWindow::WExpose()
1897 /*--------------------------------------------------------*\
1899 \*--------------------------------------------------------*/
1900 void DrawWindow::WButtonPress(const Standard_Integer,
1901 const Standard_Integer,
1902 const Standard_Integer&)
1907 /*--------------------------------------------------------*\
1909 \*--------------------------------------------------------*/
1910 void DrawWindow::WButtonRelease(const Standard_Integer,
1911 const Standard_Integer,
1912 const Standard_Integer&)
1917 /*--------------------------------------------------------*\
1919 \*--------------------------------------------------------*/
1920 void Draw_Window::WMotionNotify(const Standard_Integer ,
1921 const Standard_Integer )
1926 /*--------------------------------------------------------*\
1928 \*--------------------------------------------------------*/
1929 void DrawWindow::WConfigureNotify(const Standard_Integer,
1930 const Standard_Integer,
1931 const Standard_Integer,
1932 const Standard_Integer)
1937 /*--------------------------------------------------------*\
1939 \*--------------------------------------------------------*/
1940 void DrawWindow::WUnmapNotify()
1947 ** IMPLEMENTATION of the CLASS SEGMENT
1950 /*--------------------------------------------------------*\
1952 \*--------------------------------------------------------*/
1954 void Segment::Init(Standard_Integer a1, Standard_Integer a2,
1955 Standard_Integer a3, Standard_Integer a4)
1963 static DWORD WINAPI tkLoop(VOID);
1965 static Tk_Window mainWindow;
1968 //* threads sinchronization *//
1969 DWORD dwMainThreadId;
1970 console_semaphore_value volatile console_semaphore = WAIT_CONSOLE_COMMAND;
1971 //char console_command[1000];
1972 #define COMMAND_SIZE 1000 /* Console Command size */
1973 char console_command[COMMAND_SIZE];
1974 bool volatile isTkLoopStarted = false;
1976 /*--------------------------------------------------------*\
1978 \*--------------------------------------------------------*/
1979 Standard_Boolean Init_Appli(HINSTANCE hInst,
1980 HINSTANCE hPrevInst, int nShow, HWND& hWndFrame )
1984 console_semaphore = STOP_CONSOLE;
1986 interp = theCommands.Interp();
1989 dwMainThreadId = GetCurrentThreadId();
1991 //necessary for normal Tk operation
1992 hThread = CreateThread(NULL, // no security attributes
1993 0, // use default stack size
1994 (LPTHREAD_START_ROUTINE) tkLoop, // thread function
1995 NULL, // no thread function argument
1996 0, // use default creation flags
1999 cout << "Tcl/Tk main loop thread not created. Switching to batch mode..." << endl;
2004 } catch (Standard_Failure) {
2005 cout <<" Pb au lancement de TK_Init "<<endl;
2008 Tcl_StaticPackage(interp, "Tk", Tk_Init, (Tcl_PackageInitProc *) NULL);
2010 //since the main Tcl/Tk loop wasn't created --> switch to batch mode
2011 return Standard_False;
2014 // san - 06/08/2002 - Time for tkLoop to start; Tk fails to initialize otherwise
2015 while (!isTkLoopStarted)
2018 // Saving of window classes
2020 if(!RegisterAppClass(hInst))
2021 return(Standard_False);
2024 ** Enter the application message-polling loop. This is the anchor for
2027 hWndFrame = !Draw_IsConsoleSubsystem ? CreateAppWindow (hInst) : NULL;
2028 if (hWndFrame != NULL)
2030 ShowWindow(hWndFrame,nShow);
2031 UpdateWindow(hWndFrame);
2034 return Standard_True;
2037 Standard_Boolean Draw_Interprete (const char*);
2039 /*--------------------------------------------------------*\
2040 | readStdinThreadFunc
2041 \*--------------------------------------------------------*/
2042 static DWORD WINAPI readStdinThreadFunc(VOID)
2044 if (!Draw_IsConsoleSubsystem) return 1;
2046 while (console_semaphore != WAIT_CONSOLE_COMMAND)
2048 if (fgets(console_command,COMMAND_SIZE,stdin))
2050 console_semaphore = HAS_CONSOLE_COMMAND;
2055 /*--------------------------------------------------------*\
2056 | exitProc: finalization handler for Tcl/Tk thread. Forces parent process to die
2057 \*--------------------------------------------------------*/
2058 void exitProc(ClientData /*dc*/)
2060 HANDLE proc = GetCurrentProcess();
2061 TerminateProcess(proc, 0);
2064 /*--------------------------------------------------------*\
2065 | tkLoop: implements Tk_Main()-like behaviour in a separate thread
2066 \*--------------------------------------------------------*/
2067 static DWORD WINAPI tkLoop(VOID)
2069 Tcl_CreateExitHandler(exitProc, 0);
2070 #if (TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5))
2071 Tcl_RegisterChannel(theCommands.Interp(), Tcl_GetStdChannel(TCL_STDIN));
2072 Tcl_RegisterChannel(theCommands.Interp(), Tcl_GetStdChannel(TCL_STDOUT));
2073 Tcl_RegisterChannel(theCommands.Interp(), Tcl_GetStdChannel(TCL_STDERR));
2077 // initialize the Tk library if not in 'virtual windows' mode
2078 // (virtual windows are created by OCCT with native APIs,
2079 // thus Tk will be useless)
2080 if (!Draw_VirtualWindows)
2085 Standard_Integer res = Tk_Init (interp);
2088 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
2089 cout << "tkLoop: error in Tk initialization. Tcl reported: " << Tcl_GetStringResult(interp) << endl;
2091 cout << "tkLoop: error in Tk initialization. Tcl reported: " << interp->result << endl;
2095 catch (Standard_Failure)
2097 cout << "tkLoop: exception in TK_Init\n";
2099 Tcl_StaticPackage (interp, "Tk", Tk_Init, (Tcl_PackageInitProc* ) NULL);
2100 mainWindow = Tk_MainWindow (interp);
2101 if (mainWindow == NULL)
2103 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
2104 fprintf (stderr, "%s\n", Tcl_GetStringResult(interp));
2106 fprintf (stderr, "%s\n", interp->result);
2108 cout << "tkLoop: Tk_MainWindow() returned NULL. Exiting...\n";
2111 Tk_Name(mainWindow) = Tk_GetUid (Tk_SetAppName (mainWindow, "Draw"));
2115 // set signal handler in the new thread
2118 // inform the others that we have started
2119 isTkLoopStarted = true;
2121 while (console_semaphore == STOP_CONSOLE)
2122 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
2124 if (Draw_IsConsoleSubsystem && console_semaphore == WAIT_CONSOLE_COMMAND)
2128 Standard_Boolean toLoop = Standard_True;
2131 while(Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT));
2132 if (console_semaphore == HAS_CONSOLE_COMMAND)
2134 if (Draw_Interprete (console_command))
2136 if (Draw_IsConsoleSubsystem) Prompt (interp, 0);
2140 if (Draw_IsConsoleSubsystem) Prompt (interp, 1);
2142 console_semaphore = WAIT_CONSOLE_COMMAND;
2149 // We should not exit until the Main Tk window is closed
2150 toLoop = (Tk_GetNumMainWindows() > 0) || Draw_VirtualWindows;
2158 /*--------------------------------------------------------*\
2160 \*--------------------------------------------------------*/
2161 void Run_Appli(HWND hWnd)
2164 HACCEL hAccel = NULL;
2168 // if (!(hAccel = LoadAccelerators (hInstance, MAKEINTRESOURCE(ACCEL_ID))))
2169 // MessageBox(hWnd, "MDI: Load Accel failure!", "Error", MB_OK);
2172 if (Draw_IsConsoleSubsystem) {
2173 hThread = CreateThread(NULL, // no security attributes
2174 0, // use default stack size
2175 (LPTHREAD_START_ROUTINE) readStdinThreadFunc, // thread function
2176 NULL, // no thread function argument
2177 0, // use default creation flags
2178 &IDThread); // returns thread identifier
2180 cout << "pb in creation of the thread reading stdin" << endl;
2181 Draw_IsConsoleSubsystem = Standard_False;
2182 Init_Appli(GetModuleHandle(NULL),
2183 GetModuleHandle(NULL),
2184 1, hWnd); // reinit => create MDI client wnd
2188 //turn on the command interpretation mechanism (regardless of the mode)
2189 if (console_semaphore == STOP_CONSOLE)
2190 console_semaphore = WAIT_CONSOLE_COMMAND;
2192 //simple Win32 message loop
2193 while (GetMessage(&msg, NULL, 0, 0) > 0)
2195 if (!TranslateAccelerator(hWnd, hAccel, &msg))
2197 TranslateMessage(&msg);
2198 DispatchMessage(&msg);
2205 /*--------------------------------------------------------*\
2207 \*--------------------------------------------------------*/
2208 void Destroy_Appli(HINSTANCE hInst)
2210 UnregisterAppClass(hInst);
2211 for (int i = 0 ; i < MAXCOLOR ; i++)
2212 DeleteObject(colorPenTab[i]);
2215 /*--------------------------------------------------------*\
2217 \*--------------------------------------------------------*/
2218 void DrawWindow::SelectWait(HANDLE& hWnd, int& x, int& y, int& button)
2224 GetMessage(&msg,NULL,0,0);
2225 while((msg.message != WM_RBUTTONDOWN && msg.message != WM_LBUTTONDOWN) ||
2226 ! ( Draw_IsConsoleSubsystem || IsChild(DrawWindow::hWndClientMDI,msg.hwnd)) )
2227 GetMessage(&msg,NULL,0,0);
2230 x = LOWORD(msg.lParam);
2231 y = HIWORD(msg.lParam);
2232 if (msg.message == WM_LBUTTONDOWN)
2238 /*--------------------------------------------------------*\
2240 \*--------------------------------------------------------*/
2241 void DrawWindow::SelectNoWait(HANDLE& hWnd, int& x, int& y, int& button)
2247 GetMessage(&msg,NULL,0,0);
2248 while((msg.message != WM_RBUTTONDOWN && msg.message != WM_LBUTTONDOWN &&
2249 msg.message != WM_MOUSEMOVE) ||
2250 ! ( Draw_IsConsoleSubsystem || IsChild(DrawWindow::hWndClientMDI,msg.hwnd) ) )
2251 GetMessage(&msg,NULL,0,0);
2253 x = LOWORD(msg.lParam);
2254 y = HIWORD(msg.lParam);
2255 switch (msg.message)
2257 case WM_LBUTTONDOWN :
2261 case WM_RBUTTONDOWN :
2271 Standard_Boolean DrawWindow::DefineColor (const Standard_Integer, const char*)
2273 return Standard_True;