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_Window.hxx>
27 #include <Draw_Appli.hxx>
28 #include <TCollection_AsciiString.hxx>
29 #include <Image_AlienPixMap.hxx>
30 #include <NCollection_List.hxx>
32 extern Draw_Interpretor theCommands;
33 extern Standard_Boolean Draw_VirtualWindows;
34 static Tcl_Interp *interp; /* Interpreter for this application. */
35 static NCollection_List<Draw_Window::FCallbackBeforeTerminate> MyCallbacks;
37 void Draw_Window::AddCallbackBeforeTerminate(FCallbackBeforeTerminate theCB)
39 MyCallbacks.Append(theCB);
42 void Draw_Window::RemoveCallbackBeforeTerminate(FCallbackBeforeTerminate theCB)
44 NCollection_List<Draw_Window::FCallbackBeforeTerminate>::Iterator Iter(MyCallbacks);
45 for(; Iter.More(); Iter.Next())
47 if (Iter.Value() == theCB)
49 MyCallbacks.Remove(Iter);
56 *----------------------------------------------------------------------
60 * Issue a prompt on standard output, or invoke a script
61 * to issue the prompt.
67 * A prompt gets output, and a Tcl script may be evaluated
70 *----------------------------------------------------------------------
73 static void Prompt(Tcl_Interp *Interp, int partial)
77 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4))) && !defined(USE_NON_CONST)
78 const char *promptCmd;
83 Tcl_Channel outChannel, errChannel;
84 outChannel = Tcl_GetStdChannel(TCL_STDOUT);
85 promptCmd = Tcl_GetVar(Interp,(char*)
86 (partial ? "tcl_prompt2" : "tcl_prompt1"), TCL_GLOBAL_ONLY);
88 if (promptCmd == NULL) {
90 if (!partial && outChannel) {
91 Tcl_Write(outChannel, "% ", 2);
94 code = Tcl_Eval(Interp, promptCmd);
95 outChannel = Tcl_GetStdChannel(TCL_STDOUT);
96 errChannel = Tcl_GetStdChannel(TCL_STDERR);
99 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
100 Tcl_Write(errChannel, Tcl_GetStringResult(Interp), -1);
102 Tcl_Write(errChannel, Interp->result, -1);
104 Tcl_Write(errChannel, "\n", 1);
106 Tcl_AddErrorInfo(Interp,
107 "\n (script that generates prompt)");
112 Tcl_Flush(outChannel);
116 #if !defined(_WIN32) && !defined(__WIN32__)
118 #include <OSD_Timer.hxx>
119 #include <Draw_Window.hxx>
122 #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
123 // use forward declaration for small subset of used Tk functions
124 // to workaround broken standard Tk framework installation within OS X SDKs
125 // which *HAS* X11 headers in Tk.framework but doesn't install them appropriately
127 typedef struct Tk_Window_* Tk_Window;
128 typedef const char* Tk_Uid;
130 extern "C" int Tk_Init (Tcl_Interp* interp);
131 extern "C" void Tk_MainLoop();
132 extern "C" Tk_Window Tk_MainWindow (Tcl_Interp* interp) ;
133 extern "C" Tk_Uid Tk_GetUid (const char* str);
134 extern "C" const char* Tk_SetAppName (Tk_Window tkwin, const char* name) ;
135 extern "C" void Tk_GeometryRequest (Tk_Window tkwin, int reqWidth, int reqHeight);
142 * Global variables used by the main program:
145 char *tcl_RcFileName = NULL; /* Name of a user-specific startup script
146 * to source if the application is being run
147 * interactively (e.g. "~/.wishrc"). Set
148 * by Tcl_AppInit. NULL means don't source
151 static Tcl_DString command; /* Used to assemble lines of terminal input
152 * into Tcl commands. */
153 static Tcl_DString line; /* Used to read the next line from the
155 //static char errorExitCmd[] = "exit 1";
158 * Forward declarations for procedures defined later in this file:
161 static void StdinProc (ClientData clientData, int mask);
163 static void Prompt (Tcl_Interp *Interp, int partial);
165 static Standard_Boolean tty; /* Non-zero means standard input is a
166 * terminal-like device. Zero means it's
169 Standard_Integer Draw_WindowScreen = 0;
170 Standard_Boolean Draw_BlackBackGround = Standard_True;
173 // Initialization of static variables of Draw_Window
174 //======================================================
175 Draw_Window* Draw_Window::firstWindow = NULL;
178 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
179 #include <X11/Xutil.h>
180 #include <Aspect_DisplayConnection.hxx>
182 static unsigned long thePixels[MAXCOLOR];
184 Display* Draw_WindowDisplay = NULL;
185 Colormap Draw_WindowColorMap;
186 static Handle(Aspect_DisplayConnection) Draw_DisplayConnection;
188 // Base_Window struct definition
189 //===================================
193 XSetWindowAttributes xswa;
196 //=======================================================================
197 //function : Draw_Window
199 //=======================================================================
200 Draw_Window::Draw_Window() :
201 base(*new Base_Window()),
206 myUseBuffer(Standard_False),
207 withWindowManager(Standard_True)
209 myMother = RootWindow(Draw_WindowDisplay,
212 if (firstWindow) firstWindow->previous = this;
216 //=======================================================================
217 //function : Draw_Window
219 //=======================================================================
220 Draw_Window::Draw_Window(Window mother) :
221 base(*new Base_Window()),
226 myUseBuffer(Standard_False),
227 withWindowManager(Standard_True)
231 if (firstWindow) firstWindow->previous = this;
235 //=======================================================================
236 //function : Draw_Window
238 //=======================================================================
239 Draw_Window::Draw_Window (const char* title,
240 Standard_Integer X, Standard_Integer Y,
241 Standard_Integer DX, Standard_Integer DY) :
242 base(*new Base_Window()),
247 myUseBuffer(Standard_False),
248 withWindowManager(Standard_True)
250 myMother = RootWindow(Draw_WindowDisplay,
253 if (firstWindow) firstWindow->previous = this;
259 //=======================================================================
260 //function : Draw_Window
262 //=======================================================================
263 Draw_Window::Draw_Window (const char* window ) :
264 base(*new Base_Window()),
269 myUseBuffer(Standard_False),
270 withWindowManager(Standard_True)
272 sscanf(window,"%lx",&win);
273 Standard_Integer X,Y,DX,DY;
275 if (firstWindow) firstWindow->previous = this;
284 //=======================================================================
285 //function : Draw_Window
287 //=======================================================================
288 Draw_Window::Draw_Window (Window mother,
290 Standard_Integer X, Standard_Integer Y,
291 Standard_Integer DX, Standard_Integer DY) :
292 base(*new Base_Window()),
297 myUseBuffer(Standard_False),
298 withWindowManager(Standard_True)
302 if (firstWindow) firstWindow->previous = this;
308 //=======================================================================
309 //function : ~Draw_Window
311 //=======================================================================
312 Draw_Window::~Draw_Window()
315 previous->next = next;
319 next->previous = previous;
323 XFreePixmap(Draw_WindowDisplay, myBuffer);
326 // Liberation pointer on Base_Window
330 //=======================================================================
333 //=======================================================================
334 void Draw_Window::Init(Standard_Integer X, Standard_Integer Y,
335 Standard_Integer DX, Standard_Integer DY)
337 unsigned long setmask;
339 if (Draw_BlackBackGround)
341 base.xswa.background_pixel = BlackPixel(Draw_WindowDisplay,Draw_WindowScreen);
342 base.xswa.border_pixel = WhitePixel(Draw_WindowDisplay,Draw_WindowScreen);
346 base.xswa.background_pixel = WhitePixel(Draw_WindowDisplay,Draw_WindowScreen);
347 base.xswa.border_pixel = BlackPixel(Draw_WindowDisplay,Draw_WindowScreen);
349 base.xswa.colormap = Draw_WindowColorMap;
350 setmask = CWBackPixel | CWBorderPixel ;
353 myHints.flags = USPosition;
359 win = XCreateWindow(Draw_WindowDisplay,
362 (unsigned int) DX,(unsigned int) DY,
364 DefaultDepth(Draw_WindowDisplay,Draw_WindowScreen),
366 DefaultVisual(Draw_WindowDisplay,Draw_WindowScreen),
368 XSelectInput(Draw_WindowDisplay, win, ButtonPressMask|ExposureMask|
369 StructureNotifyMask);
371 // advise to the window manager to place it where I need
372 XSetWMNormalHints(Draw_WindowDisplay,win,&myHints);
374 if (Draw_VirtualWindows)
376 myUseBuffer = Standard_True;
381 base.gc = XCreateGC(Draw_WindowDisplay, win, 0, NULL);
383 XSetPlaneMask(Draw_WindowDisplay,base.gc,AllPlanes);
384 XSetForeground(Draw_WindowDisplay,
385 base.gc, WhitePixel(Draw_WindowDisplay,Draw_WindowScreen));
386 XSetBackground(Draw_WindowDisplay,
387 base.gc, BlackPixel(Draw_WindowDisplay,Draw_WindowScreen));
388 // save in case of window recovery
390 base.xswa.backing_store = Always;
391 XChangeWindowAttributes(Draw_WindowDisplay, win,
392 CWBackingStore, &base.xswa);
394 XSetLineAttributes (Draw_WindowDisplay, base.gc,
395 0, LineSolid, CapButt, JoinMiter);
398 //=======================================================================
399 //function : InitBuffer
401 //=======================================================================
402 void Draw_Window::InitBuffer()
406 XFreePixmap (Draw_WindowDisplay, myBuffer);
408 XWindowAttributes winAttr;
409 XGetWindowAttributes (Draw_WindowDisplay, win, &winAttr);
410 myBuffer = XCreatePixmap (Draw_WindowDisplay, win, winAttr.width, winAttr.height, winAttr.depth);
412 else if (myBuffer != 0)
414 XFreePixmap (Draw_WindowDisplay, myBuffer);
419 //=======================================================================
420 //function : StopWinManager
422 //=======================================================================
423 void Draw_Window::StopWinManager()
426 XWindowAttributes winAttr;
427 XGetWindowAttributes (Draw_WindowDisplay, win, &winAttr);
431 myHints.flags = USPosition;
432 myHints.x = (int) 30;
433 myHints.y = (int) 100;
435 base.xswa.override_redirect = 1;
436 base.xswa.border_pixel = BlackPixel(Draw_WindowDisplay,
438 base.xswa.background_pixel = WhitePixel(Draw_WindowDisplay,
441 withWindowManager = Standard_False;
443 win = XCreateWindow(Draw_WindowDisplay, myMother,
444 winAttr.x, winAttr.y,
445 winAttr.width, winAttr.height,
447 CopyFromParent, InputOutput, CopyFromParent,
448 CWBorderPixel|CWOverrideRedirect|CWBackPixel, &base.xswa);
451 // adwise to the window manager to place it where I wish
452 XSetWMNormalHints(Draw_WindowDisplay,win,&myHints);
454 // all masks of the old window are reassigned to the new one.
455 XSelectInput(Draw_WindowDisplay,win,winAttr.your_event_mask);
458 //=======================================================================
459 //function : SetPosition
461 //=======================================================================
462 void Draw_Window::SetPosition(Standard_Integer NewXpos,
463 Standard_Integer NewYpos)
465 Standard_Integer x,y;
468 if ( (x != NewXpos) || (y != NewYpos) )
469 XMoveWindow(Draw_WindowDisplay, win, NewXpos, NewYpos);
472 //=======================================================================
473 //function : SetDimension
475 //=======================================================================
476 void Draw_Window::SetDimension(Standard_Integer NewDx,
477 Standard_Integer NewDy)
479 if ( (NewDx != WidthWin() ) || (NewDy != HeightWin() ) )
480 XResizeWindow(Draw_WindowDisplay, win, NewDx, NewDy);
483 //=======================================================================
484 //function : GetPosition
486 //=======================================================================
487 void Draw_Window::GetPosition(Standard_Integer &PosX,
488 Standard_Integer &PosY)
490 XWindowAttributes winAttr;
491 XGetWindowAttributes(Draw_WindowDisplay, win, &winAttr);
497 //=======================================================================
498 //function : HeightWin
500 //=======================================================================
501 Standard_Integer Draw_Window::HeightWin() const
504 XWindowAttributes winAttr;
505 XGetWindowAttributes(Draw_WindowDisplay, win, &winAttr);
511 //=======================================================================
512 //function : WidthWin
514 //=======================================================================
515 Standard_Integer Draw_Window::WidthWin() const
518 XWindowAttributes winAttr;
519 XGetWindowAttributes(Draw_WindowDisplay, win, &winAttr);
525 //=======================================================================
526 //function : SetTitle
528 //=======================================================================
529 void Draw_Window::SetTitle(const char* title)
531 XStoreName(Draw_WindowDisplay, win, title);
534 //=======================================================================
535 //function : GetTitle
537 //=======================================================================
538 char* Draw_Window::GetTitle()
541 XFetchName(Draw_WindowDisplay, win, &title);
545 //=======================================================================
546 //function : GetDrawable
548 //=======================================================================
549 Drawable Draw_Window::GetDrawable() const
551 return myUseBuffer ? myBuffer : win;
554 //=======================================================================
555 //function :DefineColor
557 //=======================================================================
558 Standard_Boolean Draw_Window::DefineColor(const Standard_Integer i, const char* colorName)
562 if (!XParseColor(Draw_WindowDisplay,Draw_WindowColorMap,colorName,&color))
563 return Standard_False;
564 if (!XAllocColor(Draw_WindowDisplay,Draw_WindowColorMap,&color))
565 return Standard_False;
566 thePixels[i % MAXCOLOR] = color.pixel;
567 return Standard_True;
570 //=======================================================================
571 //function : DisplayWindow
573 //=======================================================================
574 void Draw_Window::DisplayWindow()
576 if (Draw_VirtualWindows)
582 XMapRaised(Draw_WindowDisplay, win);
584 XFlush(Draw_WindowDisplay);
587 //=======================================================================
590 //=======================================================================
591 void Draw_Window::Hide()
593 XUnmapWindow(Draw_WindowDisplay, win);
596 //=======================================================================
599 //=======================================================================
600 void Draw_Window::Destroy()
602 XFreeGC (Draw_WindowDisplay, base.gc);
603 XDestroyWindow(Draw_WindowDisplay, win);
607 XFreePixmap(Draw_WindowDisplay, myBuffer);
612 //=======================================================================
615 //=======================================================================
616 void Draw_Window::Clear()
620 // XClearArea only applicable for windows
621 XGCValues currValues;
622 XGetGCValues(Draw_WindowDisplay, base.gc, GCBackground | GCForeground, &currValues);
623 XSetForeground(Draw_WindowDisplay, base.gc, currValues.background);
624 XFillRectangle(Draw_WindowDisplay, myBuffer, base.gc, 0, 0, WidthWin(), HeightWin());
625 XSetForeground(Draw_WindowDisplay, base.gc, currValues.foreground);
629 XClearArea(Draw_WindowDisplay, win, 0, 0, 0, 0, False);
633 //=======================================================================
636 //=======================================================================
637 void Draw_Window::Flush()
639 XFlush(Draw_WindowDisplay);
642 //=======================================================================
643 //function : DrawString
645 //=======================================================================
646 void Draw_Window::DrawString(int X, int Y, char *text)
648 XDrawString(Draw_WindowDisplay, GetDrawable(), base.gc, X, Y, text, strlen(text));
651 //=======================================================================
652 //function : DrawSegments
654 //=======================================================================
655 void Draw_Window::DrawSegments(Segment *tab, int nbElem)
657 XDrawSegments(Draw_WindowDisplay, GetDrawable(), base.gc, (XSegment*) tab, nbElem);
660 //=======================================================================
663 //=======================================================================
664 void Draw_Window::Redraw()
667 XCopyArea (Draw_WindowDisplay,
668 myBuffer, win, // source, destination Drawables
671 WidthWin(), HeightWin(),
672 0, 0); // destination x, y
676 //=======================================================================
677 //function : SetColor
679 //=======================================================================
680 void Draw_Window::SetColor(Standard_Integer color)
682 XSetForeground(Draw_WindowDisplay, base.gc, thePixels[color]);
685 //=======================================================================
688 //=======================================================================
689 void Draw_Window::SetMode( int mode)
691 XSetFunction(Draw_WindowDisplay, base.gc, mode);
694 //=======================================================================
697 //=======================================================================
698 Standard_Boolean Draw_Window::Save (const char* theFileName) const
700 // make sure all draw operations done
701 XSync (Draw_WindowDisplay, True);
704 XWindowAttributes winAttr;
705 XGetWindowAttributes (Draw_WindowDisplay, win, &winAttr);
709 // make sure that the whole window fit on display to prevent BadMatch error
710 XWindowAttributes winAttrRoot;
711 XGetWindowAttributes (Draw_WindowDisplay, XRootWindowOfScreen (winAttr.screen), &winAttrRoot);
713 Window winChildDummy;
716 XTranslateCoordinates (Draw_WindowDisplay, win, XRootWindowOfScreen (winAttr.screen),
717 0, 0, &winLeft, &winTop, &winChildDummy);
719 if (((winLeft + winAttr.width) > winAttrRoot.width) || winLeft < winAttrRoot.x ||
720 ((winTop + winAttr.height) > winAttrRoot.height) || winTop < winAttrRoot.y)
722 std::cerr << "The window not fully visible! Can't create the snapshot.\n";
723 return Standard_False;
728 if (XMatchVisualInfo (Draw_WindowDisplay, Draw_WindowScreen, 32, TrueColor, &aVInfo) == 0
729 && XMatchVisualInfo (Draw_WindowDisplay, Draw_WindowScreen, 24, TrueColor, &aVInfo) == 0)
731 std::cerr << "24-bit TrueColor visual is not supported by server!\n";
732 return Standard_False;
735 Image_AlienPixMap anImage;
736 bool isBigEndian = Image_PixMap::IsBigEndianHost();
737 const Standard_Size aSizeRowBytes = Standard_Size(winAttr.width) * 4;
738 if (!anImage.InitTrash (isBigEndian ? Image_PixMap::ImgRGB32 : Image_PixMap::ImgBGR32,
739 Standard_Size(winAttr.width), Standard_Size(winAttr.height), aSizeRowBytes))
741 return Standard_False;
743 anImage.SetTopDown (true);
745 XImage* anXImage = XCreateImage (Draw_WindowDisplay, aVInfo.visual,
746 32, ZPixmap, 0, (char* )anImage.ChangeData(), winAttr.width, winAttr.height, 32, int(aSizeRowBytes));
747 anXImage->bitmap_bit_order = anXImage->byte_order = (isBigEndian ? MSBFirst : LSBFirst);
748 if (XGetSubImage (Draw_WindowDisplay, GetDrawable(),
749 0, 0, winAttr.width, winAttr.height,
750 AllPlanes, ZPixmap, anXImage, 0, 0) == NULL)
752 anXImage->data = NULL;
753 XDestroyImage (anXImage);
754 return Standard_False;
758 anXImage->data = NULL;
759 XDestroyImage (anXImage);
762 return anImage.Save (theFileName);
765 //=======================================================================
768 //=======================================================================
770 void Draw_Window::Wait (Standard_Boolean wait)
774 XSelectInput(Draw_WindowDisplay,win,
775 ButtonPressMask|ExposureMask | StructureNotifyMask |
779 XSelectInput(Draw_WindowDisplay,win,
780 ButtonPressMask|ExposureMask | StructureNotifyMask);
784 //=======================================================================
785 //function : ProcessEvent
787 //=======================================================================
789 void ProcessEvent(Draw_Window& win, XEvent& xev)
791 Standard_Integer X,Y,button;
806 button = xev.xbutton.button;
807 win.WButtonPress(X,Y,button);
813 button = xev.xbutton.button;
814 win.WButtonRelease(X,Y,button);
818 XLookupString(&(xev.xkey),
828 win.WMotionNotify(X,Y);
831 case ConfigureNotify :
832 if (win.withWindowManager)
833 win.WConfigureNotify(xev.xconfigure.x, xev.xconfigure.y,
834 xev.xconfigure.width,
835 xev.xconfigure.height);
845 //=======================================================================
848 //=======================================================================
849 void Draw_Window::WExpose()
853 //=======================================================================
854 //function : WButtonPress
856 //=======================================================================
857 void Draw_Window::WButtonPress(const Standard_Integer,
858 const Standard_Integer,
859 const Standard_Integer&)
863 //=======================================================================
864 //function : WButtonRelease
866 //=======================================================================
867 void Draw_Window::WButtonRelease(const Standard_Integer,
868 const Standard_Integer,
869 const Standard_Integer&)
873 /**************************
874 //=======================================================================
875 //function : WKeyPress
877 //=======================================================================
879 void Draw_Window::WKeyPress(char, KeySym&)
882 ***************************/
884 //=======================================================================
885 //function : WMotionNotify
887 //=======================================================================
888 void Draw_Window::WMotionNotify(const Standard_Integer ,
889 const Standard_Integer )
893 //=======================================================================
894 //function : WConfigureNotify
896 //=======================================================================
898 void Draw_Window::WConfigureNotify(const Standard_Integer,
899 const Standard_Integer,
900 const Standard_Integer,
901 const Standard_Integer)
905 //=======================================================================
906 //function : WUnmapNotify
908 //=======================================================================
910 void Draw_Window::WUnmapNotify()
915 //======================================================
916 // funtion : ProcessEvents
917 // purpose : process pending X events
918 //======================================================
920 static void ProcessEvents(ClientData,int)
924 while (XPending(Draw_WindowDisplay)) {
929 XNextEvent(Draw_WindowDisplay,&xev);
931 /* search the window in the window list */
932 Draw_Window* w = Draw_Window::firstWindow;
933 Standard_Integer found=0;
935 if (xev.xany.window == w->win) {
936 ProcessEvent(*w, xev);
943 Tk_HandleEvent(&xev);
948 //======================================================
949 // funtion : GetNextEvent()
951 //======================================================
952 void GetNextEvent(Event& ev)
955 XNextEvent(Draw_WindowDisplay, &xev);
960 ev.window = xev.xbutton.window;
961 ev.button = xev.xbutton.button;
962 ev.x = xev.xbutton.x;
963 ev.y = xev.xbutton.y;
968 ev.window = xev.xmotion.window;
970 ev.x = xev.xmotion.x;
971 ev.y = xev.xmotion.y;
977 //======================================================
978 // funtion :Run_Appli
980 //======================================================
983 static Standard_Boolean(*Interprete) (const char*);
985 void Run_Appli(Standard_Boolean (*interprete) (const char*))
987 Tcl_Channel outChannel, inChannel ;
988 Interprete = interprete;
993 * Commands will come from standard input, so set up an event
994 * handler for standard input. If the input device is aEvaluate the
995 * .rc file, if one has been specified, set up an event handler
996 * for standard input, and print a prompt if the input
997 * device is a terminal.
999 inChannel = Tcl_GetStdChannel(TCL_STDIN);
1001 Tcl_CreateChannelHandler(inChannel, TCL_READABLE, StdinProc,
1002 (ClientData) inChannel);
1005 // Create a handler for the draw display
1007 // Adding of the casting into void* to be able to compile on AO1
1008 // ConnectionNumber(Draw_WindowDisplay) is an int 32 bits
1009 // (void*) is a pointer 64 bits ???????
1011 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
1012 #if TCL_MAJOR_VERSION < 8
1013 Tk_CreateFileHandler((void*) ConnectionNumber(Draw_WindowDisplay),
1014 TK_READABLE, ProcessEvents,(ClientData) 0 );
1016 Tk_CreateFileHandler(ConnectionNumber(Draw_WindowDisplay),
1017 TK_READABLE, ProcessEvents,(ClientData) 0 );
1023 if (tty) Prompt(theCommands.Interp(), 0);
1024 Prompt(theCommands.Interp(), 0);
1026 outChannel = Tcl_GetStdChannel(TCL_STDOUT);
1028 Tcl_Flush(outChannel);
1030 Tcl_DStringInit(&command);
1033 * Loop infinitely, waiting for commands to execute. When there
1034 * are no windows left, Tk_MainLoop returns and we exit.
1039 if (Draw_VirtualWindows) {
1040 // main window will never shown
1041 // but main loop will parse all Xlib messages
1042 Tcl_Eval(theCommands.Interp(), "wm withdraw .");
1049 Standard_Integer count = ConnectionNumber(Draw_WindowDisplay);
1050 Standard_Integer numfd;
1054 FD_SET(count,&readset);
1056 numfd = select(count+1,(Integer*)&readset,NULL,NULL,NULL);
1058 numfd = select(count+1,&readset,NULL,NULL,NULL);
1060 if (FD_ISSET(0,&readset)) StdinProc((ClientData)0,0);
1061 if (FD_ISSET(count,&readset)) ProcessEvents((ClientData)0,0);
1065 NCollection_List<Draw_Window::FCallbackBeforeTerminate>::Iterator Iter(MyCallbacks);
1066 for(; Iter.More(); Iter.Next())
1072 //======================================================
1073 // funtion : Init_Appli()
1075 //======================================================
1076 Standard_Boolean Init_Appli()
1079 interp = theCommands.Interp();
1085 } catch (Standard_Failure) {
1086 cout <<" Pb au lancement de TK_Init "<<endl;
1089 Tcl_StaticPackage(interp, "Tk", Tk_Init, (Tcl_PackageInitProc *) NULL);
1091 Tk_Window aMainWindow = Tk_MainWindow(interp) ;
1092 if (aMainWindow == NULL) {
1093 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
1094 fprintf(stderr, "%s\n", Tcl_GetStringResult(interp));
1096 fprintf(stderr, "%s\n", interp->result);
1100 #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
1101 Tk_SetAppName(aMainWindow, "Draw");
1103 Tk_Name(aMainWindow) = Tk_GetUid(Tk_SetAppName(aMainWindow, "Draw"));
1106 Tk_GeometryRequest (aMainWindow, 200, 200);
1108 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
1109 if (Draw_DisplayConnection.IsNull())
1113 Draw_DisplayConnection = new Aspect_DisplayConnection();
1115 catch (Standard_Failure)
1117 std::cout << "Cannot open display. Interpret commands in batch mode." << std::endl;
1118 return Standard_False;
1121 if (Draw_WindowDisplay == NULL)
1123 Draw_WindowDisplay = Draw_DisplayConnection->GetDisplay();
1126 // synchronize the display server : could be done within Tk_Init
1128 XSynchronize(Draw_WindowDisplay, True);
1129 XSetInputFocus(Draw_WindowDisplay,
1131 RevertToPointerRoot,
1134 Draw_WindowScreen = DefaultScreen(Draw_WindowDisplay);
1135 Draw_WindowColorMap = DefaultColormap(Draw_WindowDisplay,
1140 Tcl_SetVar(interp,"tcl_interactive",(char*)(tty ? "1" : "0"), TCL_GLOBAL_ONLY);
1141 // Tcl_SetVar(interp,"tcl_interactive",tty ? "1" : "0", TCL_GLOBAL_ONLY);
1142 return Standard_True;
1145 //======================================================
1146 // funtion : Destroy_Appli()
1148 //======================================================
1149 void Destroy_Appli()
1151 //XCloseDisplay(Draw_WindowDisplay);
1155 *----------------------------------------------------------------------
1159 * This procedure is invoked by the event dispatcher whenever
1160 * standard input becomes readable. It grabs the next line of
1161 * input characters, adds them to a command being assembled, and
1162 * executes the command if it's complete.
1168 * Could be almost arbitrary, depending on the command that's
1171 *----------------------------------------------------------------------
1175 //static void StdinProc(ClientData clientData, int mask)
1176 static void StdinProc(ClientData clientData, int )
1178 static int gotPartial = 0;
1182 Tcl_Channel chan = (Tcl_Channel) clientData;
1184 // MSV Nov 2, 2001: patch for TCL 8.3: initialize line to avoid exception
1185 // when first user input is an empty string
1186 Tcl_DStringFree(&line);
1187 count = Tcl_Gets(chan, &line);
1190 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)))
1191 Tcl_DString linetmp;
1192 Tcl_DStringInit(&linetmp);
1193 Tcl_UniChar * UniCharString;
1194 UniCharString = Tcl_UtfToUniCharDString(Tcl_DStringValue(&line),-1,&linetmp);
1195 Standard_Integer l = Tcl_UniCharLen(UniCharString);
1196 TCollection_AsciiString AsciiString("");
1197 Standard_Character Character;
1199 for (i=0; i<l; i++) {
1200 Character = UniCharString[i];
1201 AsciiString.AssignCat(Character);
1203 Tcl_DStringInit(&line);
1204 Tcl_DStringAppend(&line, AsciiString.ToCString(), -1);
1211 Tcl_DeleteChannelHandler(chan, StdinProc, (ClientData) chan);
1219 (void) Tcl_DStringAppend(&command, Tcl_DStringValue(&line), -1);
1220 cmd = Tcl_DStringAppend(&command, "\n", -1);
1221 Tcl_DStringFree(&line);
1224 if (!Tcl_CommandComplete(cmd)) {
1231 * Disable the stdin channel handler while evaluating the command;
1232 * otherwise if the command re-enters the event loop we might
1233 * process commands from stdin before the current command is
1234 * finished. Among other things, this will trash the text of the
1235 * command being evaluated.
1238 Tcl_CreateChannelHandler(chan, 0, StdinProc, (ClientData) chan);
1242 * Disable the stdin file handler while evaluating the command;
1243 * otherwise if the command re-enters the event loop we might
1244 * process commands from stdin before the current command is
1245 * finished. Among other things, this will trash the text of the
1246 * command being evaluated.
1250 // Tk_CreateFileHandler(0, 0, StdinProc, (ClientData) 0);
1253 // xab average to avoid an output SIGBUS of DRAW
1254 // to ultimately prescise or remove once
1255 // the problem of free on the global variable at the average
1262 Tcl_CreateChannelHandler(chan, TCL_READABLE, StdinProc,
1264 Tcl_DStringFree(&command);
1271 if (tty) Prompt(interp, gotPartial);
1273 } catch (Standard_Failure) {}
1279 // Source Specifique WNT
1281 /****************************************************\
1284 \****************************************************/
1286 #include "Draw_Window.hxx"
1287 #include "DrawRessource.h"
1290 #include <Draw_Appli.hxx>
1297 // Position of information in the extra memory
1299 // indicates SUBSYSTEM:CONSOLE linker option, to be set to True in main()
1301 Standard_Boolean Draw_IsConsoleSubsystem = Standard_False;
1304 Standard_Boolean Draw_BlackBackGround = Standard_True;
1306 // Creation of color stylos
1307 HPEN colorPenTab[MAXCOLOR] = {CreatePen(PS_SOLID, PENWIDTH, RGB(255,255,255)),
1308 CreatePen(PS_SOLID, PENWIDTH, RGB(255,0,0)),
1309 CreatePen(PS_SOLID, PENWIDTH, RGB(0,255,0)),
1310 CreatePen(PS_SOLID, PENWIDTH, RGB(0,0,255)),
1311 CreatePen(PS_SOLID, PENWIDTH, RGB(0,255,255)),
1312 CreatePen(PS_SOLID, PENWIDTH, RGB(255,215,0)),
1313 CreatePen(PS_SOLID, PENWIDTH, RGB(255,0,255)),
1314 CreatePen(PS_SOLID, PENWIDTH, RGB(255,52,179)),
1315 CreatePen(PS_SOLID, PENWIDTH, RGB(255,165,0)),
1316 CreatePen(PS_SOLID, PENWIDTH, RGB(255,228,225)),
1317 CreatePen(PS_SOLID, PENWIDTH, RGB(255,160,122)),
1318 CreatePen(PS_SOLID, PENWIDTH, RGB(199,21,133)),
1319 CreatePen(PS_SOLID, PENWIDTH, RGB(255,255,0)),
1320 CreatePen(PS_SOLID, PENWIDTH, RGB(240,230,140)),
1321 CreatePen(PS_SOLID, PENWIDTH, RGB(255,127,80))};
1323 // Correspondance mode X11 and WINDOWS NT
1324 int modeTab[16] = {R2_BLACK, R2_MASKPEN, R2_MASKPENNOT, R2_COPYPEN,
1325 R2_MASKNOTPEN, R2_NOP, R2_XORPEN, R2_MERGEPEN,
1326 R2_NOTMASKPEN, R2_NOTXORPEN, R2_NOT, R2_MERGEPENNOT,
1327 R2_NOTCOPYPEN, R2_MERGENOTPEN, R2_NOTMERGEPEN, R2_WHITE};
1329 /*--------------------------------------------------------*\
1330 | CREATE DRAW WINDOW PROCEDURE
1331 \*--------------------------------------------------------*/
1332 HWND DrawWindow::CreateDrawWindow(HWND hWndClient, int nitem)
1334 if (Draw_IsConsoleSubsystem) {
1335 HWND aWin = CreateWindow (DRAWCLASS, DRAWTITLE,
1336 WS_OVERLAPPEDWINDOW,
1338 NULL, NULL,::GetModuleHandle(NULL), NULL);
1339 if (!Draw_VirtualWindows)
1341 SetWindowPos(aWin, HWND_TOPMOST, 1,1,1,1, SWP_NOMOVE);
1342 SetWindowPos(aWin, HWND_NOTOPMOST, 1,1,1,1, SWP_NOMOVE);
1348 hInstance = (HANDLE)GetWindowLongPtr(hWndClient,GWLP_HINSTANCE);
1350 return CreateMDIWindow(DRAWCLASS, DRAWTITLE,
1351 WS_CAPTION | WS_CHILD | WS_THICKFRAME,
1353 hWndClient, (HINSTANCE)hInstance, nitem);
1358 /*--------------------------------------------------------*\
1359 | DRAW WINDOW PROCEDURE
1360 \*--------------------------------------------------------*/
1361 LRESULT APIENTRY DrawWindow::DrawProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
1363 DrawWindow* localObjet = (DrawWindow*)GetWindowLongPtr(hWnd, CLIENTWND);
1366 if (Draw_IsConsoleSubsystem)
1367 return (DefWindowProc(hWnd, wMsg, wParam, lParam));
1369 return(DefMDIChildProc(hWnd, wMsg, wParam, lParam));
1377 BeginPaint(hWnd, &ps);
1378 if (localObjet->GetUseBuffer())
1379 localObjet->Redraw();
1381 localObjet->WExpose();
1382 EndPaint(hWnd, &ps);
1387 if (localObjet->GetUseBuffer()) {
1388 localObjet->InitBuffer();
1389 localObjet->WExpose();
1390 localObjet->Redraw();
1395 if (Draw_IsConsoleSubsystem)
1396 return (DefWindowProc(hWnd, wMsg, wParam, lParam));
1398 return(DefMDIChildProc(hWnd, wMsg, wParam, lParam));
1405 ** IMPLEMENTATION of the CLASS DRAWWINDOW
1408 /*--------------------------------------------------------*\
1409 | Initialization of static variables of DrawWindow
1410 \*--------------------------------------------------------*/
1412 DrawWindow* DrawWindow::firstWindow = NULL;
1413 HWND DrawWindow::hWndClientMDI = 0;
1415 /*--------------------------------------------------------*\
1416 | Constructors of Draw_Window
1417 \*--------------------------------------------------------*/
1419 // Default Constructor
1420 //________________________
1421 DrawWindow::DrawWindow() :
1426 myUseBuffer(Standard_False)
1428 if (firstWindow) firstWindow->previous = this;
1432 //________________________
1433 DrawWindow::DrawWindow(char* title,
1434 Standard_Integer X, Standard_Integer Y,
1435 Standard_Integer dX,Standard_Integer dY) :
1436 win(0), next(firstWindow), previous(NULL), myMemHbm(NULL), myUseBuffer(Standard_False)
1438 if (firstWindow) firstWindow->previous = this;
1443 DrawWindow::DrawWindow(char* title,
1444 Standard_Integer X, Standard_Integer Y,
1445 Standard_Integer dX,Standard_Integer dY,
1447 win(theWin),next(firstWindow), previous(NULL), myMemHbm(NULL), myUseBuffer(Standard_False)
1449 if (firstWindow) firstWindow->previous = this;
1457 /*--------------------------------------------------------*\
1458 | Destructor of DrawWindow
1459 \*--------------------------------------------------------*/
1460 DrawWindow::~DrawWindow()
1463 previous->next = next;
1467 next->previous = previous;
1469 // Delete 'off-screen drawing'-related objects
1471 DeleteObject(myMemHbm);
1478 /*--------------------------------------------------------*\
1480 \*--------------------------------------------------------*/
1481 void DrawWindow::Init(Standard_Integer theXLeft, Standard_Integer theYTop,
1482 Standard_Integer theWidth, Standard_Integer theHeight)
1486 win = CreateDrawWindow(hWndClientMDI, 0);
1489 // include decorations in the window dimensions
1490 // to reproduce same behaviour of Xlib window.
1491 DWORD aWinStyle = GetWindowLong (win, GWL_STYLE);
1492 DWORD aWinStyleEx = GetWindowLong (win, GWL_EXSTYLE);
1493 HMENU aMenu = GetMenu (win);
1496 aRect.top = theYTop;
1497 aRect.bottom = theYTop + theHeight;
1498 aRect.left = theXLeft;
1499 aRect.right = theXLeft + theWidth;
1500 AdjustWindowRectEx (&aRect, aWinStyle, aMenu != NULL ? TRUE : FALSE, aWinStyleEx);
1502 SetPosition (aRect.left, aRect.top);
1503 SetDimension (aRect.right - aRect.left, aRect.bottom - aRect.top);
1504 // Save the pointer at the instance associated to the window
1505 SetWindowLongPtr(win, CLIENTWND, (LONG_PTR)this);
1506 HDC hDC = GetDC(win);
1507 SetBkColor(hDC, RGB(0, 0, 0));
1510 SelectObject(hDC, colorPenTab[myCurrPen]); // Default pencil
1511 SelectObject(hDC, GetStockObject(BLACK_BRUSH));
1512 SetTextColor(hDC, RGB(0,0,255));
1513 ReleaseDC(win, hDC);
1515 if (Draw_VirtualWindows)
1517 // create a virtual window
1518 SetUseBuffer (Standard_True);
1522 /*--------------------------------------------------------*\
1524 \*--------------------------------------------------------*/
1525 void DrawWindow::SetUseBuffer(Standard_Boolean use)
1531 /*--------------------------------------------------------*\
1533 \*--------------------------------------------------------*/
1534 void DrawWindow::InitBuffer()
1538 HDC hDC = GetDC(win);
1539 GetClientRect(win, &rc);
1542 GetObject(myMemHbm, sizeof(BITMAP), &aBmp);
1543 if (rc.right-rc.left == aBmp.bmWidth && rc.bottom-rc.top == aBmp.bmHeight) return;
1544 DeleteObject(myMemHbm);
1546 myMemHbm = (HBITMAP)CreateCompatibleBitmap(hDC,
1549 HDC aMemDC = GetMemDC(hDC);
1550 FillRect(aMemDC, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH));
1551 ReleaseMemDC(aMemDC);
1552 ReleaseDC(win, hDC);
1556 DeleteObject(myMemHbm);
1562 /*--------------------------------------------------------*\
1564 \*--------------------------------------------------------*/
1565 HDC DrawWindow::GetMemDC(HDC theWinDC)
1567 if (!myUseBuffer) return NULL;
1569 HDC aWorkDC = CreateCompatibleDC(theWinDC);
1570 myOldHbm = (HBITMAP)SelectObject(aWorkDC, myMemHbm);
1571 SetROP2(aWorkDC, modeTab[myCurrMode]);
1572 SelectObject(aWorkDC, colorPenTab[myCurrPen]);
1573 SetBkColor(aWorkDC, RGB(0, 0, 0));
1574 SelectObject(aWorkDC, GetStockObject(BLACK_BRUSH));
1575 SetTextColor(aWorkDC, RGB(0,0,255));
1580 /*--------------------------------------------------------*\
1582 \*--------------------------------------------------------*/
1583 void DrawWindow::ReleaseMemDC(HDC theMemDC)
1585 if (!myUseBuffer || !theMemDC) return;
1587 if (myOldHbm) SelectObject(theMemDC, myOldHbm);
1592 /*--------------------------------------------------------*\
1594 \*--------------------------------------------------------*/
1595 void DrawWindow::SetPosition(Standard_Integer posX, Standard_Integer posY)
1597 SetWindowPos(win, 0,
1600 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
1604 /*--------------------------------------------------------*\
1606 \*--------------------------------------------------------*/
1607 void DrawWindow::SetDimension(Standard_Integer dimX, Standard_Integer dimY)
1609 SetWindowPos(win, 0,
1612 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
1616 /*--------------------------------------------------------*\
1618 \*--------------------------------------------------------*/
1619 void DrawWindow::GetPosition(Standard_Integer &dimX,
1620 Standard_Integer &dimY)
1623 GetWindowRect(win, &rect);
1626 point.x = rect.left;
1629 ScreenToClient(hWndClientMDI, &point);
1635 /*--------------------------------------------------------*\
1637 \*--------------------------------------------------------*/
1638 Standard_Integer DrawWindow::HeightWin() const
1641 GetClientRect(win, &rect);
1642 return(rect.bottom-rect.top);
1646 /*--------------------------------------------------------*\
1648 \*--------------------------------------------------------*/
1649 Standard_Integer DrawWindow::WidthWin() const
1652 GetClientRect(win, &rect);
1653 return(rect.right-rect.left);
1657 /*--------------------------------------------------------*\
1659 \*--------------------------------------------------------*/
1660 void DrawWindow::SetTitle(char* title)
1662 SetWindowText(win, title);
1666 /*--------------------------------------------------------*\
1668 | Attention do not forget to unallocate the memory
1669 \*--------------------------------------------------------*/
1670 char* DrawWindow::GetTitle()
1672 char* title=new char[31];
1673 GetWindowText(win, title, 30);
1678 /*--------------------------------------------------------*\
1680 \*--------------------------------------------------------*/
1681 void DrawWindow::DisplayWindow()
1683 if (Draw_VirtualWindows)
1687 ShowWindow (win, SW_SHOW);
1692 /*--------------------------------------------------------*\
1694 \*--------------------------------------------------------*/
1695 void DrawWindow::Hide()
1697 ShowWindow(win, SW_HIDE);
1701 /*--------------------------------------------------------*\
1703 \*--------------------------------------------------------*/
1704 void DrawWindow::Destroy()
1711 /*--------------------------------------------------------*\
1713 \*--------------------------------------------------------*/
1714 void DrawWindow::Clear()
1716 HDC hDC = GetDC(win);
1717 HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
1720 SelectObject(aWorkDC,GetStockObject(BLACK_PEN));
1721 Rectangle(aWorkDC, 0, 0, WidthWin(), HeightWin());
1722 RestoreDC(aWorkDC,-1);
1724 if (myUseBuffer) ReleaseMemDC(aWorkDC);
1728 /*--------------------------------------------------------*\
1730 \*--------------------------------------------------------*/
1731 static Standard_Boolean SaveBitmap (HBITMAP theHBitmap,
1732 const char* theFileName)
1734 // Get informations about the bitmap
1736 if (GetObject (theHBitmap, sizeof(BITMAP), (LPSTR )&aBitmap) == 0)
1738 return Standard_False;
1741 Image_AlienPixMap anImage;
1742 const Standard_Size aSizeRowBytes = Standard_Size(aBitmap.bmWidth) * 4;
1743 if (!anImage.InitTrash (Image_PixMap::ImgBGR32, Standard_Size(aBitmap.bmWidth), Standard_Size(aBitmap.bmHeight), aSizeRowBytes))
1745 return Standard_False;
1747 anImage.SetTopDown (false);
1750 BITMAPINFOHEADER aBitmapInfo;
1751 memset (&aBitmapInfo, 0, sizeof(BITMAPINFOHEADER));
1752 aBitmapInfo.biSize = sizeof(BITMAPINFOHEADER);
1753 aBitmapInfo.biWidth = aBitmap.bmWidth;
1754 aBitmapInfo.biHeight = aBitmap.bmHeight; // positive means bottom-up!
1755 aBitmapInfo.biPlanes = 1;
1756 aBitmapInfo.biBitCount = 32; // use 32bit for automatic word-alignment per row
1757 aBitmapInfo.biCompression = BI_RGB;
1760 HDC aDC = GetDC (NULL);
1761 Standard_Boolean isSuccess = GetDIBits (aDC, theHBitmap,
1762 0, // first scan line to set
1763 aBitmap.bmHeight, // number of scan lines to copy
1764 anImage.ChangeData(), // array for bitmap bits
1765 (LPBITMAPINFO )&aBitmapInfo, // bitmap data info
1766 DIB_RGB_COLORS) != 0;
1767 ReleaseDC (NULL, aDC);
1768 return isSuccess && anImage.Save (theFileName);
1771 /*--------------------------------------------------------*\
1773 \*--------------------------------------------------------*/
1774 Standard_Boolean DrawWindow::Save (const char* theFileName) const
1778 return SaveBitmap (myMemHbm, theFileName);
1782 GetClientRect (win, &aRect);
1783 int aWidth = aRect.right - aRect.left;
1784 int aHeight = aRect.bottom - aRect.top;
1787 HDC aDstDC = GetDC (NULL);
1788 HDC aSrcDC = GetDC (win); // we copy only client area
1789 HDC aMemDC = CreateCompatibleDC (aDstDC);
1791 // Copy the screen to the bitmap
1792 HBITMAP anHBitmapDump = CreateCompatibleBitmap (aDstDC, aWidth, aHeight);
1793 HBITMAP anHBitmapOld = (HBITMAP )SelectObject (aMemDC, anHBitmapDump);
1794 BitBlt (aMemDC, 0, 0, aWidth, aHeight, aSrcDC, 0, 0, SRCCOPY);
1796 Standard_Boolean isSuccess = SaveBitmap (anHBitmapDump, theFileName);
1799 DeleteObject (SelectObject (aMemDC, anHBitmapOld));
1805 /*--------------------------------------------------------*\
1807 \*--------------------------------------------------------*/
1808 void DrawWindow::DrawString(int x,int y, char* text)
1810 HDC hDC = GetDC(win);
1811 HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
1813 TextOut(aWorkDC, x, y, text, (int )strlen(text));
1815 if (myUseBuffer) ReleaseMemDC(aWorkDC);
1819 /*--------------------------------------------------------*\
1821 \*--------------------------------------------------------*/
1822 void DrawWindow::DrawSegments(Segment *tab, int nbElem)
1824 HDC hDC = GetDC(win);
1825 HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
1827 for(int i = 0 ; i < nbElem ; i++)
1829 MoveToEx(aWorkDC, tab[i].x1, tab[i].y1, NULL);
1830 LineTo(aWorkDC, tab[i].x2, tab[i].y2);
1833 if (myUseBuffer) ReleaseMemDC(aWorkDC);
1837 /*--------------------------------------------------------*\
1839 \*--------------------------------------------------------*/
1840 void DrawWindow::Redraw()
1843 HDC hDC = GetDC(win);
1845 GetClientRect(win, &rc);
1846 HDC aMemDC = GetMemDC(hDC);
1849 rc.right-rc.left, rc.bottom-rc.top,
1852 ReleaseMemDC(aMemDC);
1857 /*--------------------------------------------------------*\
1859 \*--------------------------------------------------------*/
1860 void DrawWindow::SetMode(int mode)
1862 HDC hDC = GetDC(win);
1864 SetROP2(hDC, modeTab[mode]);
1869 /*--------------------------------------------------------*\
1871 \*--------------------------------------------------------*/
1872 void DrawWindow::SetColor(Standard_Integer color)
1874 HDC hDC = GetDC(win);
1876 SelectObject(hDC,colorPenTab[color]);
1881 /*--------------------------------------------------------*\
1883 \*--------------------------------------------------------*/
1884 void DrawWindow::WExpose()
1889 /*--------------------------------------------------------*\
1891 \*--------------------------------------------------------*/
1892 void DrawWindow::WButtonPress(const Standard_Integer,
1893 const Standard_Integer,
1894 const Standard_Integer&)
1899 /*--------------------------------------------------------*\
1901 \*--------------------------------------------------------*/
1902 void DrawWindow::WButtonRelease(const Standard_Integer,
1903 const Standard_Integer,
1904 const Standard_Integer&)
1909 /*--------------------------------------------------------*\
1911 \*--------------------------------------------------------*/
1912 void Draw_Window::WMotionNotify(const Standard_Integer ,
1913 const Standard_Integer )
1918 /*--------------------------------------------------------*\
1920 \*--------------------------------------------------------*/
1921 void DrawWindow::WConfigureNotify(const Standard_Integer,
1922 const Standard_Integer,
1923 const Standard_Integer,
1924 const Standard_Integer)
1929 /*--------------------------------------------------------*\
1931 \*--------------------------------------------------------*/
1932 void DrawWindow::WUnmapNotify()
1939 ** IMPLEMENTATION of the CLASS SEGMENT
1942 /*--------------------------------------------------------*\
1944 \*--------------------------------------------------------*/
1946 void Segment::Init(Standard_Integer a1, Standard_Integer a2,
1947 Standard_Integer a3, Standard_Integer a4)
1955 static DWORD WINAPI tkLoop(VOID);
1957 static Tk_Window mainWindow;
1960 //* threads sinchronization *//
1961 DWORD dwMainThreadId;
1962 console_semaphore_value volatile console_semaphore = WAIT_CONSOLE_COMMAND;
1963 //char console_command[1000];
1964 #define COMMAND_SIZE 1000 /* Console Command size */
1965 char console_command[COMMAND_SIZE];
1966 bool volatile isTkLoopStarted = false;
1968 /*--------------------------------------------------------*\
1970 \*--------------------------------------------------------*/
1971 Standard_Boolean Init_Appli(HINSTANCE hInst,
1972 HINSTANCE hPrevInst, int nShow, HWND& hWndFrame )
1976 console_semaphore = STOP_CONSOLE;
1978 interp = theCommands.Interp();
1981 dwMainThreadId = GetCurrentThreadId();
1983 //necessary for normal Tk operation
1984 hThread = CreateThread(NULL, // no security attributes
1985 0, // use default stack size
1986 (LPTHREAD_START_ROUTINE) tkLoop, // thread function
1987 NULL, // no thread function argument
1988 0, // use default creation flags
1991 cout << "Tcl/Tk main loop thread not created. Switching to batch mode..." << endl;
1996 } catch (Standard_Failure) {
1997 cout <<" Pb au lancement de TK_Init "<<endl;
2000 Tcl_StaticPackage(interp, "Tk", Tk_Init, (Tcl_PackageInitProc *) NULL);
2002 //since the main Tcl/Tk loop wasn't created --> switch to batch mode
2003 return Standard_False;
2006 // san - 06/08/2002 - Time for tkLoop to start; Tk fails to initialize otherwise
2007 while (!isTkLoopStarted)
2010 // Saving of window classes
2012 if(!RegisterAppClass(hInst))
2013 return(Standard_False);
2016 ** Enter the application message-polling loop. This is the anchor for
2019 hWndFrame = !Draw_IsConsoleSubsystem ? CreateAppWindow (hInst) : NULL;
2020 if (hWndFrame != NULL)
2022 ShowWindow(hWndFrame,nShow);
2023 UpdateWindow(hWndFrame);
2026 return Standard_True;
2029 Standard_Boolean Draw_Interprete (const char*);
2031 /*--------------------------------------------------------*\
2032 | readStdinThreadFunc
2033 \*--------------------------------------------------------*/
2034 static DWORD WINAPI readStdinThreadFunc(VOID)
2036 if (!Draw_IsConsoleSubsystem) return 1;
2038 while (console_semaphore != WAIT_CONSOLE_COMMAND)
2040 if (fgets(console_command,COMMAND_SIZE,stdin))
2042 console_semaphore = HAS_CONSOLE_COMMAND;
2047 /*--------------------------------------------------------*\
2048 | exitProc: finalization handler for Tcl/Tk thread. Forces parent process to die
2049 \*--------------------------------------------------------*/
2050 void exitProc(ClientData /*dc*/)
2052 NCollection_List<Draw_Window::FCallbackBeforeTerminate>::Iterator Iter(MyCallbacks);
2053 for(; Iter.More(); Iter.Next())
2057 HANDLE proc = GetCurrentProcess();
2058 TerminateProcess(proc, 0);
2061 /*--------------------------------------------------------*\
2062 | tkLoop: implements Tk_Main()-like behaviour in a separate thread
2063 \*--------------------------------------------------------*/
2064 static DWORD WINAPI tkLoop(VOID)
2066 Tcl_CreateExitHandler(exitProc, 0);
2067 #if (TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5))
2068 Tcl_RegisterChannel(theCommands.Interp(), Tcl_GetStdChannel(TCL_STDIN));
2069 Tcl_RegisterChannel(theCommands.Interp(), Tcl_GetStdChannel(TCL_STDOUT));
2070 Tcl_RegisterChannel(theCommands.Interp(), Tcl_GetStdChannel(TCL_STDERR));
2074 // initialize the Tk library if not in 'virtual windows' mode
2075 // (virtual windows are created by OCCT with native APIs,
2076 // thus Tk will be useless)
2077 if (!Draw_VirtualWindows)
2082 Standard_Integer res = Tk_Init (interp);
2085 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
2086 cout << "tkLoop: error in Tk initialization. Tcl reported: " << Tcl_GetStringResult(interp) << endl;
2088 cout << "tkLoop: error in Tk initialization. Tcl reported: " << interp->result << endl;
2092 catch (Standard_Failure)
2094 cout << "tkLoop: exception in TK_Init\n";
2096 Tcl_StaticPackage (interp, "Tk", Tk_Init, (Tcl_PackageInitProc* ) NULL);
2097 mainWindow = Tk_MainWindow (interp);
2098 if (mainWindow == NULL)
2100 #if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
2101 fprintf (stderr, "%s\n", Tcl_GetStringResult(interp));
2103 fprintf (stderr, "%s\n", interp->result);
2105 cout << "tkLoop: Tk_MainWindow() returned NULL. Exiting...\n";
2108 Tk_Name(mainWindow) = Tk_GetUid (Tk_SetAppName (mainWindow, "Draw"));
2112 // set signal handler in the new thread
2113 OSD::SetSignal(Standard_False);
2115 // inform the others that we have started
2116 isTkLoopStarted = true;
2118 while (console_semaphore == STOP_CONSOLE)
2119 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
2121 if (Draw_IsConsoleSubsystem && console_semaphore == WAIT_CONSOLE_COMMAND)
2125 Standard_Boolean toLoop = Standard_True;
2128 while(Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT));
2129 if (console_semaphore == HAS_CONSOLE_COMMAND)
2131 if (Draw_Interprete (console_command))
2133 if (Draw_IsConsoleSubsystem) Prompt (interp, 0);
2137 if (Draw_IsConsoleSubsystem) Prompt (interp, 1);
2139 console_semaphore = WAIT_CONSOLE_COMMAND;
2146 // We should not exit until the Main Tk window is closed
2147 toLoop = (Tk_GetNumMainWindows() > 0) || Draw_VirtualWindows;
2155 /*--------------------------------------------------------*\
2157 \*--------------------------------------------------------*/
2158 void Run_Appli(HWND hWnd)
2161 HACCEL hAccel = NULL;
2165 // if (!(hAccel = LoadAccelerators (hInstance, MAKEINTRESOURCE(ACCEL_ID))))
2166 // MessageBox(hWnd, "MDI: Load Accel failure!", "Error", MB_OK);
2169 if (Draw_IsConsoleSubsystem) {
2170 hThread = CreateThread(NULL, // no security attributes
2171 0, // use default stack size
2172 (LPTHREAD_START_ROUTINE) readStdinThreadFunc, // thread function
2173 NULL, // no thread function argument
2174 0, // use default creation flags
2175 &IDThread); // returns thread identifier
2177 cout << "pb in creation of the thread reading stdin" << endl;
2178 Draw_IsConsoleSubsystem = Standard_False;
2179 Init_Appli(GetModuleHandle(NULL),
2180 GetModuleHandle(NULL),
2181 1, hWnd); // reinit => create MDI client wnd
2185 //turn on the command interpretation mechanism (regardless of the mode)
2186 if (console_semaphore == STOP_CONSOLE)
2187 console_semaphore = WAIT_CONSOLE_COMMAND;
2189 //simple Win32 message loop
2190 while (GetMessage(&msg, NULL, 0, 0) > 0)
2192 if (!TranslateAccelerator(hWnd, hAccel, &msg))
2194 TranslateMessage(&msg);
2195 DispatchMessage(&msg);
2202 /*--------------------------------------------------------*\
2204 \*--------------------------------------------------------*/
2205 void Destroy_Appli(HINSTANCE hInst)
2207 UnregisterAppClass(hInst);
2208 for (int i = 0 ; i < MAXCOLOR ; i++)
2209 DeleteObject(colorPenTab[i]);
2212 /*--------------------------------------------------------*\
2214 \*--------------------------------------------------------*/
2215 void DrawWindow::SelectWait(HANDLE& hWnd, int& x, int& y, int& button)
2221 GetMessage(&msg,NULL,0,0);
2222 while((msg.message != WM_RBUTTONDOWN && msg.message != WM_LBUTTONDOWN) ||
2223 ! ( Draw_IsConsoleSubsystem || IsChild(DrawWindow::hWndClientMDI,msg.hwnd)) )
2224 GetMessage(&msg,NULL,0,0);
2227 x = LOWORD(msg.lParam);
2228 y = HIWORD(msg.lParam);
2229 if (msg.message == WM_LBUTTONDOWN)
2235 /*--------------------------------------------------------*\
2237 \*--------------------------------------------------------*/
2238 void DrawWindow::SelectNoWait(HANDLE& hWnd, int& x, int& y, int& button)
2244 GetMessage(&msg,NULL,0,0);
2245 while((msg.message != WM_RBUTTONDOWN && msg.message != WM_LBUTTONDOWN &&
2246 msg.message != WM_MOUSEMOVE) ||
2247 ! ( Draw_IsConsoleSubsystem || IsChild(DrawWindow::hWndClientMDI,msg.hwnd) ) )
2248 GetMessage(&msg,NULL,0,0);
2250 x = LOWORD(msg.lParam);
2251 y = HIWORD(msg.lParam);
2252 switch (msg.message)
2254 case WM_LBUTTONDOWN :
2258 case WM_RBUTTONDOWN :
2268 Standard_Boolean DrawWindow::DefineColor (const Standard_Integer, const char*)
2270 return Standard_True;