0024312: Compilation issues on OS X 10.9 Maverics SDK
[occt.git] / src / Draw / Draw_Window.cxx
CommitLineData
b311480e 1// Created on: 1994-07-27
2// Created by: Remi LEQUETTE
3// Copyright (c) 1994-1999 Matra Datavision
4// Copyright (c) 1999-2012 OPEN CASCADE SAS
7fd59977 5//
b311480e 6// The content of this file is subject to the Open CASCADE Technology Public
7// License Version 6.5 (the "License"). You may not use the content of this file
8// except in compliance with the License. Please obtain a copy of the License
9// at http://www.opencascade.org and read it completely before using this file.
10//
11// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13//
14// The Original Code and all software distributed under the License is
15// distributed on an "AS IS" basis, without warranty of any kind, and the
16// Initial Developer hereby disclaims all such warranties, including without
17// limitation, any warranties of merchantability, fitness for a particular
18// purpose or non-infringement. Please see the License for the specific terms
19// and conditions governing the rights and limitations under the License.
20
7fd59977 21// include windows.h first to have all definitions available
22#ifdef WNT
23#include <windows.h>
24#endif
25
26#include <Standard_ErrorHandler.hxx>
27
28#include <tcl.h>
29#include <Draw_Interpretor.hxx>
30#include <Draw_Appli.hxx>
31#include <TCollection_AsciiString.hxx>
692613e5 32#include <Image_AlienPixMap.hxx>
7fd59977 33
34extern Draw_Interpretor theCommands;
bf03eb83 35extern Standard_Boolean Draw_VirtualWindows;
7fd59977 36static Tcl_Interp *interp; /* Interpreter for this application. */
37
38/*
39 *----------------------------------------------------------------------
40 *
41 * Prompt --
42 *
43 * Issue a prompt on standard output, or invoke a script
44 * to issue the prompt.
45 *
46 * Results:
47 * None.
48 *
49 * Side effects:
50 * A prompt gets output, and a Tcl script may be evaluated
51 * in interp.
52 *
53 *----------------------------------------------------------------------
54 */
55
56static void Prompt(Tcl_Interp *Interp, int partial)
57{
58
59 // MKV 29.03.05
60#if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4))) && !defined(USE_NON_CONST)
61 const char *promptCmd;
62#else
63 char *promptCmd;
64#endif
65 int code;
66 Tcl_Channel outChannel, errChannel;
67 outChannel = Tcl_GetStdChannel(TCL_STDOUT);
68 promptCmd = Tcl_GetVar(Interp,(char*)
69 (partial ? "tcl_prompt2" : "tcl_prompt1"), TCL_GLOBAL_ONLY);
70
71 if (promptCmd == NULL) {
72defaultPrompt:
73 if (!partial && outChannel) {
74 Tcl_Write(outChannel, "% ", 2);
75 }
76 } else {
77 code = Tcl_Eval(Interp, promptCmd);
78 outChannel = Tcl_GetStdChannel(TCL_STDOUT);
79 errChannel = Tcl_GetStdChannel(TCL_STDERR);
80 if (code != TCL_OK) {
81 if (errChannel) {
7fb60cfd 82#if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
83 Tcl_Write(errChannel, Tcl_GetStringResult(Interp), -1);
84#else
7fd59977 85 Tcl_Write(errChannel, Interp->result, -1);
7fb60cfd 86#endif
7fd59977 87 Tcl_Write(errChannel, "\n", 1);
88 }
89 Tcl_AddErrorInfo(Interp,
90 "\n (script that generates prompt)");
91 goto defaultPrompt;
92 }
93 }
94 if (outChannel) {
95 Tcl_Flush(outChannel);
96 }
97}
98
67d97f0e 99#if !defined(_WIN32) && !defined(__WIN32__)
7fd59977 100
101#ifdef HAVE_CONFIG_H
102# include <config.h>
103#endif
104
105#include <OSD_Timer.hxx>
106
107#ifdef HAVE_SYS_TIME_H
108# include <sys/time.h>
109#endif
110
111#ifdef HAVE_SYS_TYPES_H
112# include <sys/types.h>
113#endif
114
115#ifdef HAVE_SYS_SELECT_H
116#include <sys/select.h>
117#endif
118
119#ifdef HAVE_SYS_FILIO_H
120#include <sys/filio.h>
121#else
122#include <sys/ioctl.h>
123#endif
124
125#include <fcntl.h>
126
127#ifdef HAVE_UNISTD_H
128# include <unistd.h>
129#endif
130
131#include <Draw_Window.hxx>
132
133#ifdef HAVE_STRINGS_H
134# include <strings.h>
135#endif
136
7fd59977 137#include <stdio.h>
138#include <tk.h>
139
140/*
141 * Global variables used by the main program:
142 */
143
144static Tk_Window mainWindow; /* The main window for the application. If
145 * NULL then the application no longer
146 * exists. */
147char *tcl_RcFileName = NULL; /* Name of a user-specific startup script
148 * to source if the application is being run
149 * interactively (e.g. "~/.wishrc"). Set
150 * by Tcl_AppInit. NULL means don't source
151 * anything ever. */
152
153static Tcl_DString command; /* Used to assemble lines of terminal input
154 * into Tcl commands. */
155static Tcl_DString line; /* Used to read the next line from the
156 * terminal input. */
157//static char errorExitCmd[] = "exit 1";
158
159/*
160 * Forward declarations for procedures defined later in this file:
161 */
162
163static void StdinProc (ClientData clientData, int mask);
164
165static void Prompt (Tcl_Interp *Interp, int partial);
166
167static Standard_Boolean tty; /* Non-zero means standard input is a
168 * terminal-like device. Zero means it's
169 * a file. */
170
171static unsigned long thePixels[MAXCOLOR];
172
7fd59977 173Standard_Integer Draw_WindowScreen = 0;
7fd59977 174Standard_Boolean Draw_BlackBackGround = Standard_True;
7fd59977 175
176
177// Initialization of static variables of Draw_Window
178//======================================================
179Draw_Window* Draw_Window::firstWindow = NULL;
180
a6a96586 181// X11 specific part
67d97f0e 182#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
87225ffd 183#include <X11/Xutil.h>
a6a96586 184#include <Aspect_DisplayConnection.hxx>
185
87225ffd 186Display* Draw_WindowDisplay = NULL;
187Colormap Draw_WindowColorMap;
a6a96586 188static Handle(Aspect_DisplayConnection) Draw_DisplayConnection;
189
87225ffd 190// Base_Window struct definition
191//===================================
192struct Base_Window
193{
194 GC gc;
195 XSetWindowAttributes xswa;
196};
197
7fd59977 198//=======================================================================
199//function : Draw_Window
200//purpose :
201//=======================================================================
202Draw_Window::Draw_Window() :
203 base(*new Base_Window()),
204 win(0),
205 myBuffer(0),
206 next(firstWindow),
207 previous(NULL),
208 myUseBuffer(Standard_False),
209 withWindowManager(Standard_True)
210{
211 myMother = RootWindow(Draw_WindowDisplay,
212 Draw_WindowScreen);
213
214 if (firstWindow) firstWindow->previous = this;
215 firstWindow = this;
216}
217
218//=======================================================================
219//function : Draw_Window
220//purpose :
221//=======================================================================
222Draw_Window::Draw_Window(Window mother) :
223 base(*new Base_Window()),
224 win(0),
225 myBuffer(0),
226 next(firstWindow),
227 previous(NULL),
228 myUseBuffer(Standard_False),
229 withWindowManager(Standard_True)
230{
231 myMother = mother;
232
233 if (firstWindow) firstWindow->previous = this;
234 firstWindow = this;
235}
236
237//=======================================================================
238//function : Draw_Window
239//purpose :
240//=======================================================================
241Draw_Window::Draw_Window (const char* title,
242 Standard_Integer X, Standard_Integer Y,
243 Standard_Integer DX, Standard_Integer DY) :
244 base(*new Base_Window()),
245 win(0),
246 myBuffer(0),
247 next(firstWindow),
248 previous(NULL),
249 myUseBuffer(Standard_False),
250 withWindowManager(Standard_True)
251{
252 myMother = RootWindow(Draw_WindowDisplay,
253 Draw_WindowScreen);
254
255 if (firstWindow) firstWindow->previous = this;
256 firstWindow = this;
257 Init(X,Y,DX,DY);
258 SetTitle(title);
259}
260
261//=======================================================================
262//function : Draw_Window
263//purpose :
264//=======================================================================
265Draw_Window::Draw_Window (const char* window ) :
266 base(*new Base_Window()),
267 win(0),
268 myBuffer(0),
269 next(firstWindow),
270 previous(NULL),
271 myUseBuffer(Standard_False),
272 withWindowManager(Standard_True)
273{
274 sscanf(window,"%lx",&win);
275 Standard_Integer X,Y,DX,DY;
276
277 if (firstWindow) firstWindow->previous = this;
278 firstWindow = this;
279 GetPosition(X,Y);
280 DX=HeightWin();
281 DY=WidthWin();
282
283 Init(X,Y,DX,DY);
284}
285
286//=======================================================================
287//function : Draw_Window
288//purpose :
289//=======================================================================
290Draw_Window::Draw_Window (Window mother,
291 char* title,
292 Standard_Integer X, Standard_Integer Y,
293 Standard_Integer DX, Standard_Integer DY) :
294 base(*new Base_Window()),
295 win(0),
296 myBuffer(0),
297 next(firstWindow),
298 previous(NULL),
299 myUseBuffer(Standard_False),
300 withWindowManager(Standard_True)
301{
302 myMother = mother;
303
304 if (firstWindow) firstWindow->previous = this;
305 firstWindow = this;
306 Init(X,Y,DX,DY);
307 SetTitle(title);
308}
309
310//=======================================================================
311//function : ~Draw_Window
312//purpose :
313//=======================================================================
314Draw_Window::~Draw_Window()
315{
316 if (previous)
317 previous->next = next;
318 else
319 firstWindow = next;
320 if (next)
321 next->previous = previous;
322
323 if (myBuffer != 0)
324 {
325 XFreePixmap(Draw_WindowDisplay, myBuffer);
326 myBuffer = 0;
327 }
328 // Liberation pointer on Base_Window
329 delete &base;
330}
331
332//=======================================================================
333//function : Init
334//purpose :
335//=======================================================================
336void Draw_Window::Init(Standard_Integer X, Standard_Integer Y,
337 Standard_Integer DX, Standard_Integer DY)
338{
339 unsigned long setmask;
340
341 if (Draw_BlackBackGround)
342 {
343 base.xswa.background_pixel = BlackPixel(Draw_WindowDisplay,Draw_WindowScreen);
344 base.xswa.border_pixel = WhitePixel(Draw_WindowDisplay,Draw_WindowScreen);
345 }
346 else
347 {
348 base.xswa.background_pixel = WhitePixel(Draw_WindowDisplay,Draw_WindowScreen);
349 base.xswa.border_pixel = BlackPixel(Draw_WindowDisplay,Draw_WindowScreen);
350 }
351 base.xswa.colormap = Draw_WindowColorMap;
352 setmask = CWBackPixel | CWBorderPixel ;
353
354 XSizeHints myHints;
355 myHints.flags = USPosition;
356 myHints.x = (int) X;
357 myHints.y = (int) Y;
358
359 if (win == 0)
360 {
361 win = XCreateWindow(Draw_WindowDisplay,
362 myMother,
363 (int) X,(int) Y,
364 (unsigned int) DX,(unsigned int) DY,
365 5,
366 DefaultDepth(Draw_WindowDisplay,Draw_WindowScreen),
367 InputOutput,
368 DefaultVisual(Draw_WindowDisplay,Draw_WindowScreen),
369 setmask,&base.xswa);
370 XSelectInput(Draw_WindowDisplay, win, ButtonPressMask|ExposureMask|
371 StructureNotifyMask);
372
373 // advise to the window manager to place it where I need
374 XSetWMNormalHints(Draw_WindowDisplay,win,&myHints);
375
376 if (Draw_VirtualWindows)
377 {
378 myUseBuffer = Standard_True;
379 InitBuffer();
380 }
381 }
382
383 base.gc = XCreateGC(Draw_WindowDisplay, win, 0, NULL);
384
385 XSetPlaneMask(Draw_WindowDisplay,base.gc,AllPlanes);
386 XSetForeground(Draw_WindowDisplay,
387 base.gc, WhitePixel(Draw_WindowDisplay,Draw_WindowScreen));
388 XSetBackground(Draw_WindowDisplay,
389 base.gc, BlackPixel(Draw_WindowDisplay,Draw_WindowScreen));
390 // save in case of window recovery
391
392 base.xswa.backing_store = Always;
393 XChangeWindowAttributes(Draw_WindowDisplay, win,
394 CWBackingStore, &base.xswa);
395
396 XSetLineAttributes (Draw_WindowDisplay, base.gc,
397 0, LineSolid, CapButt, JoinMiter);
398}
399
400//=======================================================================
401//function : InitBuffer
402//purpose :
403//=======================================================================
404void Draw_Window::InitBuffer()
405{
406 if (myUseBuffer) {
407 if (myBuffer != 0) {
408 XFreePixmap (Draw_WindowDisplay, myBuffer);
409 }
410 XWindowAttributes winAttr;
411 XGetWindowAttributes (Draw_WindowDisplay, win, &winAttr);
412 myBuffer = XCreatePixmap (Draw_WindowDisplay, win, winAttr.width, winAttr.height, winAttr.depth);
413 }
414 else if (myBuffer != 0)
415 {
416 XFreePixmap (Draw_WindowDisplay, myBuffer);
417 myBuffer = 0;
418 }
419}
420
421//=======================================================================
422//function : StopWinManager
423//purpose :
424//=======================================================================
425void Draw_Window::StopWinManager()
426{
427// XGCValues winGc;
428 XWindowAttributes winAttr;
429 XGetWindowAttributes (Draw_WindowDisplay, win, &winAttr);
430 Destroy();
431
432 XSizeHints myHints;
433 myHints.flags = USPosition;
434 myHints.x = (int) 30;
435 myHints.y = (int) 100;
436
437 base.xswa.override_redirect = 1;
438 base.xswa.border_pixel = BlackPixel(Draw_WindowDisplay,
439 Draw_WindowScreen);
440 base.xswa.background_pixel = WhitePixel(Draw_WindowDisplay,
441 Draw_WindowScreen);
442
443 withWindowManager = Standard_False;
444
445 win = XCreateWindow(Draw_WindowDisplay, myMother,
446 winAttr.x, winAttr.y,
447 winAttr.width, winAttr.height,
448 2,
449 CopyFromParent, InputOutput, CopyFromParent,
450 CWBorderPixel|CWOverrideRedirect|CWBackPixel, &base.xswa);
451
452
453 // adwise to the window manager to place it where I wish
454 XSetWMNormalHints(Draw_WindowDisplay,win,&myHints);
455
456 // all masks of the old window are reassigned to the new one.
457 XSelectInput(Draw_WindowDisplay,win,winAttr.your_event_mask);
458}
459
460//=======================================================================
461//function : SetPosition
462//purpose :
463//=======================================================================
464void Draw_Window::SetPosition(Standard_Integer NewXpos,
465 Standard_Integer NewYpos)
466{
467 Standard_Integer x,y;
468 GetPosition(x, y);
469
470 if ( (x != NewXpos) || (y != NewYpos) )
471 XMoveWindow(Draw_WindowDisplay, win, NewXpos, NewYpos);
472}
473
474//=======================================================================
475//function : SetDimension
476//purpose :
477//=======================================================================
478void Draw_Window::SetDimension(Standard_Integer NewDx,
479 Standard_Integer NewDy)
480{
481 if ( (NewDx != WidthWin() ) || (NewDy != HeightWin() ) )
482 XResizeWindow(Draw_WindowDisplay, win, NewDx, NewDy);
483}
484
485//=======================================================================
486//function : GetPosition
487//purpose :
488//=======================================================================
489void Draw_Window::GetPosition(Standard_Integer &PosX,
490 Standard_Integer &PosY)
491{
492 XWindowAttributes winAttr;
493 XGetWindowAttributes(Draw_WindowDisplay, win, &winAttr);
494
495 PosX = winAttr.x;
496 PosY = winAttr.y;
497}
498
499//=======================================================================
500//function : HeightWin
501//purpose :
502//=======================================================================
503Standard_Integer Draw_Window::HeightWin() const
504{
505 Standard_Integer DY;
506 XWindowAttributes winAttr;
507 XGetWindowAttributes(Draw_WindowDisplay, win, &winAttr);
508
509 DY = winAttr.height;
510 return DY;
511}
512
513//=======================================================================
514//function : WidthWin
515//purpose :
516//=======================================================================
517Standard_Integer Draw_Window::WidthWin() const
518{
519 Standard_Integer DX;
520 XWindowAttributes winAttr;
521 XGetWindowAttributes(Draw_WindowDisplay, win, &winAttr);
522
523 DX = winAttr.width;
524 return DX;
525}
526
527//=======================================================================
528//function : SetTitle
529//purpose :
530//=======================================================================
531void Draw_Window::SetTitle(const char* title)
532{
533 XStoreName(Draw_WindowDisplay, win, title);
534}
535
536//=======================================================================
537//function : GetTitle
538//purpose :
539//=======================================================================
540char* Draw_Window::GetTitle()
541{
542 char* title;
543 XFetchName(Draw_WindowDisplay, win, &title);
544 return title;
545}
546
547//=======================================================================
548//function : GetDrawable
549//purpose :
550//=======================================================================
551Drawable Draw_Window::GetDrawable() const
552{
553 return myUseBuffer ? myBuffer : win;
554}
555
556//=======================================================================
557//function :DefineColor
558//purpose :
559//=======================================================================
560Standard_Boolean Draw_Window::DefineColor(const Standard_Integer i, const char* colorName)
561{
562 XColor color;
563
564 if (!XParseColor(Draw_WindowDisplay,Draw_WindowColorMap,colorName,&color))
565 return Standard_False;
566 if (!XAllocColor(Draw_WindowDisplay,Draw_WindowColorMap,&color))
567 return Standard_False;
568 thePixels[i % MAXCOLOR] = color.pixel;
569 return Standard_True;
570}
571
572//=======================================================================
573//function : DisplayWindow
574//purpose :
575//=======================================================================
576void Draw_Window::DisplayWindow()
577{
578 if (Draw_VirtualWindows)
579 {
580 return;
581 }
7fd59977 582 else
583 {
584 XMapRaised(Draw_WindowDisplay, win);
585 }
586 XFlush(Draw_WindowDisplay);
587}
588
589//=======================================================================
590//function : Hide
591//purpose :
592//=======================================================================
593void Draw_Window::Hide()
594{
595 XUnmapWindow(Draw_WindowDisplay, win);
596}
597
598//=======================================================================
599//function : Destroy
600//purpose :
601//=======================================================================
602void Draw_Window::Destroy()
603{
87225ffd 604 XFreeGC (Draw_WindowDisplay, base.gc);
7fd59977 605 XDestroyWindow(Draw_WindowDisplay, win);
606 win = 0;
607 if (myBuffer != 0)
608 {
609 XFreePixmap(Draw_WindowDisplay, myBuffer);
610 myBuffer = 0;
611 }
612}
613
614//=======================================================================
615//function : Clear
616//purpose :
617//=======================================================================
618void Draw_Window::Clear()
619{
620 if (myUseBuffer)
621 {
622 // XClearArea only applicable for windows
623 XGCValues currValues;
624 XGetGCValues(Draw_WindowDisplay, base.gc, GCBackground | GCForeground, &currValues);
625 XSetForeground(Draw_WindowDisplay, base.gc, currValues.background);
626 XFillRectangle(Draw_WindowDisplay, myBuffer, base.gc, 0, 0, WidthWin(), HeightWin());
627 XSetForeground(Draw_WindowDisplay, base.gc, currValues.foreground);
628 }
629 else
630 {
631 XClearArea(Draw_WindowDisplay, win, 0, 0, 0, 0, False);
632 }
633}
634
635//=======================================================================
636//function : Flush
637//purpose :
638//=======================================================================
639void Draw_Window::Flush()
640{
641 XFlush(Draw_WindowDisplay);
642}
643
644//=======================================================================
645//function : DrawString
646//purpose :
647//=======================================================================
648void Draw_Window::DrawString(int X, int Y, char *text)
649{
650 XDrawString(Draw_WindowDisplay, GetDrawable(), base.gc, X, Y, text, strlen(text));
651}
652
653//=======================================================================
654//function : DrawSegments
655//purpose :
656//=======================================================================
657void Draw_Window::DrawSegments(Segment *tab, int nbElem)
658{
659 XDrawSegments(Draw_WindowDisplay, GetDrawable(), base.gc, (XSegment*) tab, nbElem);
660}
661
662//=======================================================================
663//function : Redraw
664//purpose :
665//=======================================================================
666void Draw_Window::Redraw()
667{
668 if (myUseBuffer) {
669 XCopyArea (Draw_WindowDisplay,
670 myBuffer, win, // source, destination Drawables
671 base.gc,
672 0, 0, // source x, y
673 WidthWin(), HeightWin(),
674 0, 0); // destination x, y
675 }
676}
677
678//=======================================================================
679//function : SetColor
680//purpose :
681//=======================================================================
682void Draw_Window::SetColor(Standard_Integer color)
683{
684 XSetForeground(Draw_WindowDisplay, base.gc, thePixels[color]);
685}
686
687//=======================================================================
688//function : SetMode
689//purpose :
690//=======================================================================
691void Draw_Window::SetMode( int mode)
692{
693 XSetFunction(Draw_WindowDisplay, base.gc, mode);
694}
695
696//=======================================================================
697//function : Save
698//purpose :
699//=======================================================================
700Standard_Boolean Draw_Window::Save (const char* theFileName) const
701{
702 // make sure all draw operations done
703 XSync (Draw_WindowDisplay, True);
704
705 // the attributes
706 XWindowAttributes winAttr;
707 XGetWindowAttributes (Draw_WindowDisplay, win, &winAttr);
708
709 if (!myUseBuffer)
710 {
711 // make sure that the whole window fit on display to prevent BadMatch error
712 XWindowAttributes winAttrRoot;
713 XGetWindowAttributes (Draw_WindowDisplay, XRootWindowOfScreen (winAttr.screen), &winAttrRoot);
714
715 Window winChildDummy;
716 int winLeft = 0;
717 int winTop = 0;
718 XTranslateCoordinates (Draw_WindowDisplay, win, XRootWindowOfScreen (winAttr.screen),
719 0, 0, &winLeft, &winTop, &winChildDummy);
720
721 if (((winLeft + winAttr.width) > winAttrRoot.width) || winLeft < winAttrRoot.x ||
722 ((winTop + winAttr.height) > winAttrRoot.height) || winTop < winAttrRoot.y)
723 {
724 std::cerr << "The window not fully visible! Can't create the snapshot.\n";
725 return Standard_False;
726 }
727 }
728
692613e5 729 XVisualInfo aVInfo;
730 if (XMatchVisualInfo (Draw_WindowDisplay, Draw_WindowScreen, 32, TrueColor, &aVInfo) == 0
731 && XMatchVisualInfo (Draw_WindowDisplay, Draw_WindowScreen, 24, TrueColor, &aVInfo) == 0)
7fd59977 732 {
692613e5 733 std::cerr << "24-bit TrueColor visual is not supported by server!\n";
7fd59977 734 return Standard_False;
735 }
736
692613e5 737 Image_AlienPixMap anImage;
738 bool isBigEndian = Image_PixMap::IsBigEndianHost();
739 const Standard_Size aSizeRowBytes = Standard_Size(winAttr.width) * 4;
740 if (!anImage.InitTrash (isBigEndian ? Image_PixMap::ImgRGB32 : Image_PixMap::ImgBGR32,
741 Standard_Size(winAttr.width), Standard_Size(winAttr.height), aSizeRowBytes))
7fd59977 742 {
692613e5 743 return Standard_False;
7fd59977 744 }
692613e5 745 anImage.SetTopDown (true);
746
747 XImage* anXImage = XCreateImage (Draw_WindowDisplay, aVInfo.visual,
748 32, ZPixmap, 0, (char* )anImage.ChangeData(), winAttr.width, winAttr.height, 32, int(aSizeRowBytes));
749 anXImage->bitmap_bit_order = anXImage->byte_order = (isBigEndian ? MSBFirst : LSBFirst);
750 if (XGetSubImage (Draw_WindowDisplay, GetDrawable(),
751 0, 0, winAttr.width, winAttr.height,
752 AllPlanes, ZPixmap, anXImage, 0, 0) == NULL)
7fd59977 753 {
692613e5 754 anXImage->data = NULL;
755 XDestroyImage (anXImage);
7fd59977 756 return Standard_False;
757 }
7fd59977 758
692613e5 759 // destroy the image
760 anXImage->data = NULL;
761 XDestroyImage (anXImage);
762
763 // save the image
764 return anImage.Save (theFileName);
765}
7fd59977 766
67d97f0e 767//=======================================================================
768//function : Wait
769//purpose :
770//=======================================================================
771
772void Draw_Window::Wait (Standard_Boolean wait)
773{
774 Flush();
775 if (!wait) {
776 XSelectInput(Draw_WindowDisplay,win,
777 ButtonPressMask|ExposureMask | StructureNotifyMask |
778 PointerMotionMask);
779 }
780 else {
781 XSelectInput(Draw_WindowDisplay,win,
782 ButtonPressMask|ExposureMask | StructureNotifyMask);
783 }
784}
785
7fd59977 786//=======================================================================
787//function : ProcessEvent
788//purpose :
789//=======================================================================
790
791void ProcessEvent(Draw_Window& win, XEvent& xev)
792{
793 Standard_Integer X,Y,button,lenk;
794 char c;
795 KeySym keysym;
796 XComposeStatus stat;
797 char chainekey[10];
798
799
800 switch (xev.type) {
801
802 case Expose :
803 win.WExpose();
804 break;
805
806 case ButtonPress :
807 X = xev.xbutton.x;
808 Y = xev.xbutton.y;
809 button = xev.xbutton.button;
810 win.WButtonPress(X,Y,button);
811 break;
812
813 case ButtonRelease :
814 X = xev.xbutton.x;
815 Y = xev.xbutton.y;
816 button = xev.xbutton.button;
817 win.WButtonRelease(X,Y,button);
818 break;
819
820 case KeyPress :
821 lenk = XLookupString(&(xev.xkey),
822 chainekey,
823 10,
824 &keysym,
825 &stat);
826 if (lenk==1)
827 c = chainekey[0];
828 else
829 c = '\0';
830
831 //WKeyPress(c,keysym);
832 break;
833
834 case MotionNotify :
835 X = xev.xmotion.x;
836 Y = xev.xmotion.y;
837 win.WMotionNotify(X,Y);
838 break;
839
840 case ConfigureNotify :
841 if (win.withWindowManager)
842 win.WConfigureNotify(xev.xconfigure.x, xev.xconfigure.y,
843 xev.xconfigure.width,
844 xev.xconfigure.height);
845 break;
846
847 case UnmapNotify :
848
849 win.WUnmapNotify();
850 break;
851 }
852}
853
854//=======================================================================
855//function : WExpose
856//purpose :
857//=======================================================================
858void Draw_Window::WExpose()
859{
860}
861
862//=======================================================================
863//function : WButtonPress
864//purpose :
865//=======================================================================
866void Draw_Window::WButtonPress(const Standard_Integer,
867 const Standard_Integer,
868 const Standard_Integer&)
869{
870}
871
872//=======================================================================
873//function : WButtonRelease
874//purpose :
875//=======================================================================
876void Draw_Window::WButtonRelease(const Standard_Integer,
877 const Standard_Integer,
878 const Standard_Integer&)
879{
880}
881
882/**************************
883//=======================================================================
884//function : WKeyPress
885//purpose :
886//=======================================================================
887
888void Draw_Window::WKeyPress(char, KeySym&)
889{
890}
891***************************/
892
893//=======================================================================
894//function : WMotionNotify
895//purpose :
896//=======================================================================
897void Draw_Window::WMotionNotify(const Standard_Integer ,
898 const Standard_Integer )
899{
900}
901
902//=======================================================================
903//function : WConfigureNotify
904//purpose :
905//=======================================================================
906
907void Draw_Window::WConfigureNotify(const Standard_Integer,
908 const Standard_Integer,
909 const Standard_Integer,
910 const Standard_Integer)
911{
912}
913
7fd59977 914//=======================================================================
915//function : WUnmapNotify
916//purpose :
917//=======================================================================
918
919void Draw_Window::WUnmapNotify()
920{
921}
922
923
924//======================================================
925// funtion : ProcessEvents
926// purpose : process pending X events
927//======================================================
928
929static void ProcessEvents(ClientData,int)
930{
931 // test for X Event
932
933 while (XPending(Draw_WindowDisplay)) {
934
935 XEvent xev;
936 xev.type = 0;
937
938 XNextEvent(Draw_WindowDisplay,&xev);
939
940 /* search the window in the window list */
941 Draw_Window* w = Draw_Window::firstWindow;
942 Standard_Integer found=0;
943 while (w) {
944 if (xev.xany.window == w->win) {
945 ProcessEvent(*w, xev);
946 found=1;
947 break;
948 }
949 w = w->next;
950 }
951 if (found==0) {
952 Tk_HandleEvent(&xev);
953 }
954 }
955}
956
67d97f0e 957//======================================================
958// funtion : GetNextEvent()
959// purpose :
960//======================================================
961void GetNextEvent(Event& ev)
962{
963 XEvent xev;
964 XNextEvent(Draw_WindowDisplay, &xev);
965 switch(xev.type)
966 {
967 case ButtonPress :
968 ev.type = 4;
969 ev.window = xev.xbutton.window;
970 ev.button = xev.xbutton.button;
971 ev.x = xev.xbutton.x;
972 ev.y = xev.xbutton.y;
973 break;
974
975 case MotionNotify :
976 ev.type = 6;
977 ev.window = xev.xmotion.window;
978 ev.button = 0;
979 ev.x = xev.xmotion.x;
980 ev.y = xev.xmotion.y;
981 break;
982 }
983}
984#endif //__APPLE__
985
7fd59977 986//======================================================
987// funtion :Run_Appli
988// purpose :
989//======================================================
990
991
bf03eb83 992static Standard_Boolean(*Interprete) (const char*);
7fd59977 993
bf03eb83 994void Run_Appli(Standard_Boolean (*interprete) (const char*))
7fd59977 995{
996 Tcl_Channel outChannel, inChannel ;
997 Interprete = interprete;
998
999#ifdef _TK
1000
1001 /*
1002 * Commands will come from standard input, so set up an event
1003 * handler for standard input. If the input device is aEvaluate the
1004 * .rc file, if one has been specified, set up an event handler
1005 * for standard input, and print a prompt if the input
1006 * device is a terminal.
1007 */
1008 inChannel = Tcl_GetStdChannel(TCL_STDIN);
1009 if (inChannel) {
1010 Tcl_CreateChannelHandler(inChannel, TCL_READABLE, StdinProc,
1011 (ClientData) inChannel);
1012 }
1013
1014 // Create a handler for the draw display
1015
1016 // Adding of the casting into void* to be able to compile on AO1
1017 // ConnectionNumber(Draw_WindowDisplay) is an int 32 bits
1018 // (void*) is a pointer 64 bits ???????
1019
67d97f0e 1020#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
7fd59977 1021#if TCL_MAJOR_VERSION < 8
1022 Tk_CreateFileHandler((void*) ConnectionNumber(Draw_WindowDisplay),
1023 TK_READABLE, ProcessEvents,(ClientData) 0 );
1024#else
1025 Tk_CreateFileHandler(ConnectionNumber(Draw_WindowDisplay),
1026 TK_READABLE, ProcessEvents,(ClientData) 0 );
1027#endif
67d97f0e 1028#endif // __APPLE__
7fd59977 1029
1030#endif
1031
1032 if (tty) Prompt(theCommands.Interp(), 0);
1033 Prompt(theCommands.Interp(), 0);
1034
1035 outChannel = Tcl_GetStdChannel(TCL_STDOUT);
1036 if (outChannel) {
1037 Tcl_Flush(outChannel);
1038 }
1039 Tcl_DStringInit(&command);
1040
1041 /*
1042 * Loop infinitely, waiting for commands to execute. When there
1043 * are no windows left, Tk_MainLoop returns and we exit.
1044 */
1045
1046#ifdef _TK
1047
1048 if (Draw_VirtualWindows) {
1049 // main window will never shown
1050 // but main loop will parse all Xlib messages
1051 Tcl_Eval(theCommands.Interp(), "wm withdraw .");
1052 }
1053 Tk_MainLoop();
1054
1055#else
1056
1057 fd_set readset;
1058 Standard_Integer count = ConnectionNumber(Draw_WindowDisplay);
1059 Standard_Integer numfd;
1060 while (1) {
1061 FD_ZERO(&readset);
1062 FD_SET(0,&readset);
1063 FD_SET(count,&readset);
1064#ifdef HPUX
1065 numfd = select(count+1,(Integer*)&readset,NULL,NULL,NULL);
1066#else
1067 numfd = select(count+1,&readset,NULL,NULL,NULL);
1068#endif
1069 if (FD_ISSET(0,&readset)) StdinProc((ClientData)0,0);
1070 if (FD_ISSET(count,&readset)) ProcessEvents((ClientData)0,0);
1071 }
1072
1073#endif
1074}
1075
1076//======================================================
1077// funtion : Init_Appli()
1078// purpose :
1079//======================================================
1080Standard_Boolean Init_Appli()
1081{
1082 theCommands.Init();
1083 interp = theCommands.Interp();
1084
1085 Tcl_Init(interp) ;
1086 try {
1087 OCC_CATCH_SIGNALS
1088 Tk_Init(interp) ;
1089 } catch (Standard_Failure) {
1090 cout <<" Pb au lancement de TK_Init "<<endl;
1091 }
1092
1093 Tcl_StaticPackage(interp, "Tk", Tk_Init, (Tcl_PackageInitProc *) NULL);
1094
1095 mainWindow =
1096 Tk_MainWindow(interp) ;
1097 if (mainWindow == NULL) {
7fb60cfd 1098#if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
1099 fprintf(stderr, "%s\n", Tcl_GetStringResult(interp));
1100#else
7fd59977 1101 fprintf(stderr, "%s\n", interp->result);
7fb60cfd 1102#endif
7fd59977 1103 exit(1);
1104 }
1105 Tk_Name(mainWindow) =
1106 Tk_GetUid(Tk_SetAppName(mainWindow,
1107 "Draw")) ;
1108
1109 Tk_GeometryRequest(mainWindow, 200, 200);
1110
67d97f0e 1111#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
a6a96586 1112 if (Draw_DisplayConnection.IsNull())
1113 {
1114 try
1115 {
1116 Draw_DisplayConnection = new Aspect_DisplayConnection();
1117 }
1118 catch (Standard_Failure)
1119 {
1120 std::cout << "Cannot open display. Interpret commands in batch mode." << std::endl;
1121 return Standard_False;
1122 }
7fd59977 1123 }
a6a96586 1124 if (Draw_WindowDisplay == NULL)
1125 {
1126 Draw_WindowDisplay = Draw_DisplayConnection->GetDisplay();
7fd59977 1127 }
1128 //
1129 // synchronize the display server : could be done within Tk_Init
1130 //
1131 XSynchronize(Draw_WindowDisplay, True);
1132 XSetInputFocus(Draw_WindowDisplay,
1133 PointerRoot,
1134 RevertToPointerRoot,
1135 CurrentTime);
1136
1137 Draw_WindowScreen = DefaultScreen(Draw_WindowDisplay);
1138 Draw_WindowColorMap = DefaultColormap(Draw_WindowDisplay,
1139 Draw_WindowScreen);
67d97f0e 1140#endif // __APPLE__
1141
7fd59977 1142 tty = isatty(0);
1143 Tcl_SetVar(interp,"tcl_interactive",(char*)(tty ? "1" : "0"), TCL_GLOBAL_ONLY);
1144// Tcl_SetVar(interp,"tcl_interactive",tty ? "1" : "0", TCL_GLOBAL_ONLY);
1145 return Standard_True;
1146}
1147
1148//======================================================
1149// funtion : Destroy_Appli()
1150// purpose :
1151//======================================================
1152void Destroy_Appli()
1153{
1154 //XCloseDisplay(Draw_WindowDisplay);
1155}
1156
7fd59977 1157/*
1158 *----------------------------------------------------------------------
1159 *
1160 * StdinProc --
1161 *
1162 * This procedure is invoked by the event dispatcher whenever
1163 * standard input becomes readable. It grabs the next line of
1164 * input characters, adds them to a command being assembled, and
1165 * executes the command if it's complete.
1166 *
1167 * Results:
1168 * None.
1169 *
1170 * Side effects:
1171 * Could be almost arbitrary, depending on the command that's
1172 * typed.
1173 *
1174 *----------------------------------------------------------------------
1175 */
1176
1177 /* ARGSUSED */
1178//static void StdinProc(ClientData clientData, int mask)
1179static void StdinProc(ClientData clientData, int )
1180{
1181 static int gotPartial = 0;
1182 char *cmd;
1183// int code, count;
1184 int count;
1185 Tcl_Channel chan = (Tcl_Channel) clientData;
1186
1187 // MSV Nov 2, 2001: patch for TCL 8.3: initialize line to avoid exception
1188 // when first user input is an empty string
1189 Tcl_DStringFree(&line);
1190 count = Tcl_Gets(chan, &line);
1191
1192 // MKV 26.05.05
1193#if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)))
1194 Tcl_DString linetmp;
1195 Tcl_DStringInit(&linetmp);
1196 Tcl_UniChar * UniCharString;
1197 UniCharString = Tcl_UtfToUniCharDString(Tcl_DStringValue(&line),-1,&linetmp);
1198 Standard_Integer l = Tcl_UniCharLen(UniCharString);
1199 TCollection_AsciiString AsciiString("");
1200 Standard_Character Character;
1201 Standard_Integer i;
1202 for (i=0; i<l; i++) {
1203 Character = UniCharString[i];
1204 AsciiString.AssignCat(Character);
1205 }
1206 Tcl_DStringInit(&line);
1207 Tcl_DStringAppend(&line, AsciiString.ToCString(), -1);
1208#endif
1209 if (count < 0) {
1210 if (!gotPartial) {
1211 if (tty) {
1212 Tcl_Exit(0);
1213 } else {
1214 Tcl_DeleteChannelHandler(chan, StdinProc, (ClientData) chan);
1215 }
1216 return;
1217 } else {
1218 count = 0;
1219 }
1220 }
1221
1222 (void) Tcl_DStringAppend(&command, Tcl_DStringValue(&line), -1);
1223 cmd = Tcl_DStringAppend(&command, "\n", -1);
1224 Tcl_DStringFree(&line);
1225 try {
1226 OCC_CATCH_SIGNALS
1227 if (!Tcl_CommandComplete(cmd)) {
1228 gotPartial = 1;
1229 goto prompt;
1230 }
1231 gotPartial = 0;
1232
1233 /*
1234 * Disable the stdin channel handler while evaluating the command;
1235 * otherwise if the command re-enters the event loop we might
1236 * process commands from stdin before the current command is
1237 * finished. Among other things, this will trash the text of the
1238 * command being evaluated.
1239 */
1240
1241 Tcl_CreateChannelHandler(chan, 0, StdinProc, (ClientData) chan);
1242
1243
1244 /*
1245 * Disable the stdin file handler while evaluating the command;
1246 * otherwise if the command re-enters the event loop we might
1247 * process commands from stdin before the current command is
1248 * finished. Among other things, this will trash the text of the
1249 * command being evaluated.
1250 */
1251
1252#ifdef _TK
1253 // Tk_CreateFileHandler(0, 0, StdinProc, (ClientData) 0);
1254#endif
1255 //
1256 // xab average to avoid an output SIGBUS of DRAW
1257 // to ultimately prescise or remove once
1258 // the problem of free on the global variable at the average
1259 //
1260 //
1261
1262 Interprete(cmd);
1263
1264
1265 Tcl_CreateChannelHandler(chan, TCL_READABLE, StdinProc,
1266 (ClientData) chan);
1267 Tcl_DStringFree(&command);
1268
1269 /*
1270 * Output a prompt.
1271 */
1272
1273prompt:
1274 if (tty) Prompt(interp, gotPartial);
1275
1276 } catch (Standard_Failure) {}
1277
1278}
1279
1280#else
1281
1282// Source Specifique WNT
1283
1284/****************************************************\
1285* Draw_Window.cxx :
1286*
1287\****************************************************/
1288
1289#include "Draw_Window.hxx"
1290#include "DrawRessource.h"
1291#include "init.h"
1292
1293#include <Draw_Appli.hxx>
1294#include <OSD.hxx>
1295
1296#include <tk.h>
1297
1298#define PENWIDTH 1
1299#define CLIENTWND 0
1300// Position of information in the extra memory
1301
1302// indicates SUBSYSTEM:CONSOLE linker option, to be set to True in main()
1303Standard_EXPORT
1304Standard_Boolean Draw_IsConsoleSubsystem = Standard_False;
1305
1306
1307Standard_Boolean Draw_BlackBackGround = Standard_True;
1308
1309// Creation of color stylos
1310HPEN colorPenTab[MAXCOLOR] = {CreatePen(PS_SOLID, PENWIDTH, RGB(255,255,255)),
1311 CreatePen(PS_SOLID, PENWIDTH, RGB(255,0,0)),
1312 CreatePen(PS_SOLID, PENWIDTH, RGB(0,255,0)),
1313 CreatePen(PS_SOLID, PENWIDTH, RGB(0,0,255)),
1314 CreatePen(PS_SOLID, PENWIDTH, RGB(0,255,255)),
1315 CreatePen(PS_SOLID, PENWIDTH, RGB(255,215,0)),
1316 CreatePen(PS_SOLID, PENWIDTH, RGB(255,0,255)),
1317 CreatePen(PS_SOLID, PENWIDTH, RGB(255,52,179)),
1318 CreatePen(PS_SOLID, PENWIDTH, RGB(255,165,0)),
1319 CreatePen(PS_SOLID, PENWIDTH, RGB(255,228,225)),
1320 CreatePen(PS_SOLID, PENWIDTH, RGB(255,160,122)),
1321 CreatePen(PS_SOLID, PENWIDTH, RGB(199,21,133)),
1322 CreatePen(PS_SOLID, PENWIDTH, RGB(255,255,0)),
1323 CreatePen(PS_SOLID, PENWIDTH, RGB(240,230,140)),
1324 CreatePen(PS_SOLID, PENWIDTH, RGB(255,127,80))};
1325
1326// Correspondance mode X11 and WINDOWS NT
1327int modeTab[16] = {R2_BLACK, R2_MASKPEN, R2_MASKPENNOT, R2_COPYPEN,
1328 R2_MASKNOTPEN, R2_NOP, R2_XORPEN, R2_MERGEPEN,
1329 R2_NOTMASKPEN, R2_NOTXORPEN, R2_NOT, R2_MERGEPENNOT,
1330 R2_NOTCOPYPEN, R2_MERGENOTPEN, R2_NOTMERGEPEN, R2_WHITE};
1331
1332/*--------------------------------------------------------*\
1333| CREATE DRAW WINDOW PROCEDURE
1334\*--------------------------------------------------------*/
1335HWND DrawWindow::CreateDrawWindow(HWND hWndClient, int nitem)
1336{
1337 if (Draw_IsConsoleSubsystem) {
1338 HWND aWin = CreateWindow (DRAWCLASS, DRAWTITLE,
1339 WS_OVERLAPPEDWINDOW,
1340 1,1,1,1,
1341 NULL, NULL,::GetModuleHandle(NULL), NULL);
87c58d4f
K
1342 if (!Draw_VirtualWindows)
1343 {
1344 SetWindowPos(aWin, HWND_TOPMOST, 1,1,1,1, SWP_NOMOVE);
1345 SetWindowPos(aWin, HWND_NOTOPMOST, 1,1,1,1, SWP_NOMOVE);
1346 }
7fd59977 1347 return aWin;
1348 }
1349 else {
1350 HANDLE hInstance;
1351#ifndef _WIN64
1352 hInstance = (HANDLE)GetWindowLong(hWndClient,GWL_HINSTANCE);
1353#else
1354 hInstance = (HANDLE)GetWindowLong(hWndClient,GWLP_HINSTANCE);
1355#endif
1356
1357 return CreateMDIWindow(DRAWCLASS, DRAWTITLE,
1358 WS_CAPTION | WS_CHILD | WS_THICKFRAME,
1359 1,1,0,0,
1360 hWndClient, (HINSTANCE)hInstance, nitem);
1361 }
1362}
1363
1364
1365/*--------------------------------------------------------*\
1366| DRAW WINDOW PROCEDURE
1367\*--------------------------------------------------------*/
6a7d83c4 1368LRESULT APIENTRY DrawWindow::DrawProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
7fd59977 1369{
1370 DrawWindow* localObjet = (DrawWindow*)GetWindowLong(hWnd, CLIENTWND);
1371 if (!localObjet)
1372 {
1373 if (Draw_IsConsoleSubsystem)
1374 return (DefWindowProc(hWnd, wMsg, wParam, lParam));
1375 else
1376 return(DefMDIChildProc(hWnd, wMsg, wParam, lParam));
1377 }
1378
1379 PAINTSTRUCT ps;
1380
1381 switch(wMsg)
1382 {
1383 case WM_PAINT :
1384 BeginPaint(hWnd, &ps);
1385 if (localObjet->GetUseBuffer())
1386 localObjet->Redraw();
1387 else
1388 localObjet->WExpose();
1389 EndPaint(hWnd, &ps);
1390 return 0l;
1391 break;
1392
1393 case WM_SIZE:
1394 if (localObjet->GetUseBuffer()) {
1395 localObjet->InitBuffer();
1396 localObjet->WExpose();
1397 localObjet->Redraw();
1398 return 0l;
7fd59977 1399 }
1400
1401 default:
1402 if (Draw_IsConsoleSubsystem)
1403 return (DefWindowProc(hWnd, wMsg, wParam, lParam));
1404 else
1405 return(DefMDIChildProc(hWnd, wMsg, wParam, lParam));
1406 }
7fd59977 1407}
1408
1409
1410
1411/*
1412** IMPLEMENTATION of the CLASS DRAWWINDOW
1413 */
1414
1415/*--------------------------------------------------------*\
1416| Initialization of static variables of DrawWindow
1417\*--------------------------------------------------------*/
1418
1419DrawWindow* DrawWindow::firstWindow = NULL;
1420HWND DrawWindow::hWndClientMDI = 0;
1421
1422/*--------------------------------------------------------*\
1423| Constructors of Draw_Window
1424\*--------------------------------------------------------*/
1425
1426// Default Constructor
1427//________________________
1428DrawWindow::DrawWindow() :
1429 win(0),
1430 next(firstWindow),
1431 previous(NULL),
1432 myMemHbm(NULL),
1433 myUseBuffer(Standard_False)
1434{
1435 if (firstWindow) firstWindow->previous = this;
1436 firstWindow = this;
1437}
1438
1439//________________________
1440DrawWindow::DrawWindow(char* title,
1441 Standard_Integer X, Standard_Integer Y,
1442 Standard_Integer dX,Standard_Integer dY) :
1443 win(0), next(firstWindow), previous(NULL), myMemHbm(NULL), myUseBuffer(Standard_False)
1444{
1445 if (firstWindow) firstWindow->previous = this;
1446 firstWindow = this;
1447 Init(X, Y, dX, dY);
1448 SetTitle(title);
1449}
1450DrawWindow::DrawWindow(char* title,
1451 Standard_Integer X, Standard_Integer Y,
1452 Standard_Integer dX,Standard_Integer dY,
1453 HWND theWin) :
1454 win(theWin),next(firstWindow), previous(NULL), myMemHbm(NULL), myUseBuffer(Standard_False)
1455{
1456 if (firstWindow) firstWindow->previous = this;
1457 firstWindow = this;
1458 Init(X, Y, dX, dY);
1459 SetTitle(title);
1460}
1461
1462
1463
1464/*--------------------------------------------------------*\
1465| Destructor of DrawWindow
1466\*--------------------------------------------------------*/
1467DrawWindow::~DrawWindow()
1468{
1469 if (previous)
1470 previous->next = next;
1471 else
1472 firstWindow = next;
1473 if (next)
1474 next->previous = previous;
1475
1476 // Delete 'off-screen drawing'-related objects
1477 if (myMemHbm) {
1478 DeleteObject(myMemHbm);
1479 myMemHbm = NULL;
1480 }
1481}
1482
1483
1484
1485/*--------------------------------------------------------*\
1486| Init
1487\*--------------------------------------------------------*/
1488void DrawWindow::Init(Standard_Integer theXLeft, Standard_Integer theYTop,
1489 Standard_Integer theWidth, Standard_Integer theHeight)
1490{
6a7d83c4 1491 if (win == NULL)
7fd59977 1492 {
1493 win = CreateDrawWindow(hWndClientMDI, 0);
1494 }
1495
1496 // include decorations in the window dimensions
1497 // to reproduce same behaviour of Xlib window.
6a7d83c4 1498 DWORD aWinStyle = GetWindowLong (win, GWL_STYLE);
1499 DWORD aWinStyleEx = GetWindowLong (win, GWL_EXSTYLE);
7fe83417 1500 HMENU aMenu = GetMenu (win);
1501
1502 RECT aRect;
1503 aRect.top = theYTop;
1504 aRect.bottom = theYTop + theHeight;
1505 aRect.left = theXLeft;
1506 aRect.right = theXLeft + theWidth;
1507 AdjustWindowRectEx (&aRect, aWinStyle, aMenu != NULL ? TRUE : FALSE, aWinStyleEx);
1508
1509 SetPosition (aRect.left, aRect.top);
1510 SetDimension (aRect.right - aRect.left, aRect.bottom - aRect.top);
7fd59977 1511 // Save the pointer at the instance associated to the window
1512 SetWindowLong(win, CLIENTWND, (LONG)this);
1513 HDC hDC = GetDC(win);
1514 SetBkColor(hDC, RGB(0, 0, 0));
1515 myCurrPen = 3;
1516 myCurrMode = 3;
1517 SelectObject(hDC, colorPenTab[myCurrPen]); // Default pencil
1518 SelectObject(hDC, GetStockObject(BLACK_BRUSH));
1519 SetTextColor(hDC, RGB(0,0,255));
1520 ReleaseDC(win, hDC);
1521
1522 if (Draw_VirtualWindows)
1523 {
1524 // create a virtual window
1525 SetUseBuffer (Standard_True);
1526 }
1527}
1528
1529/*--------------------------------------------------------*\
1530| SetUseBuffer
1531\*--------------------------------------------------------*/
1532void DrawWindow::SetUseBuffer(Standard_Boolean use)
1533{
1534 myUseBuffer = use;
1535 InitBuffer();
1536}
1537
1538/*--------------------------------------------------------*\
1539| InitBuffer
1540\*--------------------------------------------------------*/
1541void DrawWindow::InitBuffer()
1542{
1543 if (myUseBuffer) {
1544 RECT rc;
1545 HDC hDC = GetDC(win);
1546 GetClientRect(win, &rc);
1547 if (myMemHbm) {
1548 BITMAP aBmp;
1549 GetObject(myMemHbm, sizeof(BITMAP), &aBmp);
1550 if (rc.right-rc.left == aBmp.bmWidth && rc.bottom-rc.top == aBmp.bmHeight) return;
1551 DeleteObject(myMemHbm);
1552 }
1553 myMemHbm = (HBITMAP)CreateCompatibleBitmap(hDC,
1554 rc.right-rc.left,
1555 rc.bottom-rc.top);
1556 HDC aMemDC = GetMemDC(hDC);
1557 FillRect(aMemDC, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH));
1558 ReleaseMemDC(aMemDC);
1559 ReleaseDC(win, hDC);
1560 }
1561 else {
1562 if (myMemHbm) {
1563 DeleteObject(myMemHbm);
1564 myMemHbm = NULL;
1565 }
1566 }
1567}
1568
1569/*--------------------------------------------------------*\
1570| GetMemDC
1571\*--------------------------------------------------------*/
1572HDC DrawWindow::GetMemDC(HDC theWinDC)
1573{
1574 if (!myUseBuffer) return NULL;
1575
1576 HDC aWorkDC = CreateCompatibleDC(theWinDC);
1577 myOldHbm = (HBITMAP)SelectObject(aWorkDC, myMemHbm);
1578 SetROP2(aWorkDC, modeTab[myCurrMode]);
1579 SelectObject(aWorkDC, colorPenTab[myCurrPen]);
1580 SetBkColor(aWorkDC, RGB(0, 0, 0));
1581 SelectObject(aWorkDC, GetStockObject(BLACK_BRUSH));
1582 SetTextColor(aWorkDC, RGB(0,0,255));
1583 return aWorkDC;
1584}
1585
1586
1587/*--------------------------------------------------------*\
1588| ReleaseMemDC
1589\*--------------------------------------------------------*/
1590void DrawWindow::ReleaseMemDC(HDC theMemDC)
1591{
1592 if (!myUseBuffer || !theMemDC) return;
1593
1594 if (myOldHbm) SelectObject(theMemDC, myOldHbm);
1595 DeleteDC(theMemDC);
1596}
1597
1598
1599/*--------------------------------------------------------*\
1600| SetPosition
1601\*--------------------------------------------------------*/
1602void DrawWindow::SetPosition(Standard_Integer posX, Standard_Integer posY)
1603{
1604 SetWindowPos(win, 0,
1605 posX, posY,
1606 0, 0,
1607 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
1608}
1609
1610
1611/*--------------------------------------------------------*\
1612| SetDimension
1613\*--------------------------------------------------------*/
1614void DrawWindow::SetDimension(Standard_Integer dimX, Standard_Integer dimY)
1615{
1616 SetWindowPos(win, 0,
1617 0, 0,
1618 dimX, dimY,
1619 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
1620}
1621
1622
1623/*--------------------------------------------------------*\
1624| GetPosition
1625\*--------------------------------------------------------*/
1626void DrawWindow::GetPosition(Standard_Integer &dimX,
1627 Standard_Integer &dimY)
1628{
1629 RECT rect;
1630 GetWindowRect(win, &rect);
1631
1632 POINT point;
1633 point.x = rect.left;
1634 point.y = rect.top;
1635
1636 ScreenToClient(hWndClientMDI, &point);
1637 dimX = point.x;
1638 dimY = point.y;
1639}
1640
1641
1642/*--------------------------------------------------------*\
1643| HeightWin
1644\*--------------------------------------------------------*/
1645Standard_Integer DrawWindow::HeightWin() const
1646{
1647 RECT rect;
1648 GetClientRect(win, &rect);
1649 return(rect.bottom-rect.top);
1650}
1651
1652
1653/*--------------------------------------------------------*\
1654| WidthWin
1655\*--------------------------------------------------------*/
1656Standard_Integer DrawWindow::WidthWin() const
1657{
1658 RECT rect;
1659 GetClientRect(win, &rect);
1660 return(rect.right-rect.left);
1661}
1662
1663
1664/*--------------------------------------------------------*\
1665| SetTitle
1666\*--------------------------------------------------------*/
1667void DrawWindow::SetTitle(char* title)
1668{
1669 SetWindowText(win, title);
1670}
1671
1672
1673/*--------------------------------------------------------*\
1674| GetTitle
1675| Attention do not forget to unallocate the memory
1676\*--------------------------------------------------------*/
1677char* DrawWindow::GetTitle()
1678{
1679 char* title=new char[31];
1680 GetWindowText(win, title, 30);
1681 return title;
1682}
1683
1684
1685/*--------------------------------------------------------*\
1686| DisplayWindow
1687\*--------------------------------------------------------*/
1688void DrawWindow::DisplayWindow()
1689{
1690 if (Draw_VirtualWindows)
1691 {
1692 return;
1693 }
1694 ShowWindow (win, SW_SHOW);
1695 UpdateWindow (win);
1696}
1697
1698
1699/*--------------------------------------------------------*\
1700| Hide
1701\*--------------------------------------------------------*/
1702void DrawWindow::Hide()
1703{
1704 ShowWindow(win, SW_HIDE);
1705}
1706
1707
1708/*--------------------------------------------------------*\
1709| Destroy
1710\*--------------------------------------------------------*/
1711void DrawWindow::Destroy()
1712{
1713 DestroyWindow(win);
1714}
1715
1716
1717
1718/*--------------------------------------------------------*\
1719| Clear
1720\*--------------------------------------------------------*/
1721void DrawWindow::Clear()
1722{
1723 HDC hDC = GetDC(win);
1724 HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
1725
7fd59977 1726 SaveDC(aWorkDC);
1727 SelectObject(aWorkDC,GetStockObject(BLACK_PEN));
1728 Rectangle(aWorkDC, 0, 0, WidthWin(), HeightWin());
1729 RestoreDC(aWorkDC,-1);
1730
1731 if (myUseBuffer) ReleaseMemDC(aWorkDC);
1732 ReleaseDC(win,hDC);
1733}
1734
1735/*--------------------------------------------------------*\
1736| SaveBitmap
1737\*--------------------------------------------------------*/
692613e5 1738static Standard_Boolean SaveBitmap (HBITMAP theHBitmap,
7fd59977 1739 const char* theFileName)
1740{
692613e5 1741 // Get informations about the bitmap
7fd59977 1742 BITMAP aBitmap;
692613e5 1743 if (GetObject (theHBitmap, sizeof(BITMAP), (LPSTR )&aBitmap) == 0)
1744 {
1745 return Standard_False;
1746 }
7fd59977 1747
692613e5 1748 Image_AlienPixMap anImage;
1749 const Standard_Size aSizeRowBytes = Standard_Size(aBitmap.bmWidth) * 4;
1750 if (!anImage.InitTrash (Image_PixMap::ImgBGR32, Standard_Size(aBitmap.bmWidth), Standard_Size(aBitmap.bmHeight), aSizeRowBytes))
1751 {
1752 return Standard_False;
1753 }
1754 anImage.SetTopDown (false);
7fd59977 1755
1756 // Setup image data
1757 BITMAPINFOHEADER aBitmapInfo;
1758 memset (&aBitmapInfo, 0, sizeof(BITMAPINFOHEADER));
692613e5 1759 aBitmapInfo.biSize = sizeof(BITMAPINFOHEADER);
1760 aBitmapInfo.biWidth = aBitmap.bmWidth;
1761 aBitmapInfo.biHeight = aBitmap.bmHeight; // positive means bottom-up!
1762 aBitmapInfo.biPlanes = 1;
1763 aBitmapInfo.biBitCount = 32; // use 32bit for automatic word-alignment per row
7fd59977 1764 aBitmapInfo.biCompression = BI_RGB;
1765
7fd59977 1766 // Copy the pixels
1767 HDC aDC = GetDC (NULL);
692613e5 1768 Standard_Boolean isSuccess = GetDIBits (aDC, theHBitmap,
1769 0, // first scan line to set
1770 aBitmap.bmHeight, // number of scan lines to copy
1771 anImage.ChangeData(), // array for bitmap bits
1772 (LPBITMAPINFO )&aBitmapInfo, // bitmap data info
1773 DIB_RGB_COLORS) != 0;
7fd59977 1774 ReleaseDC (NULL, aDC);
692613e5 1775 return isSuccess && anImage.Save (theFileName);
7fd59977 1776}
1777
1778/*--------------------------------------------------------*\
1779| Save
1780\*--------------------------------------------------------*/
1781Standard_Boolean DrawWindow::Save (const char* theFileName) const
1782{
1783 if (myUseBuffer)
1784 {
1785 return SaveBitmap (myMemHbm, theFileName);
1786 }
1787
1788 RECT aRect;
1789 GetClientRect (win, &aRect);
1790 int aWidth = aRect.right - aRect.left;
1791 int aHeight = aRect.bottom - aRect.top;
1792
1793 // Prepare the DCs
1794 HDC aDstDC = GetDC (NULL);
1795 HDC aSrcDC = GetDC (win); // we copy only client area
1796 HDC aMemDC = CreateCompatibleDC (aDstDC);
1797
1798 // Copy the screen to the bitmap
1799 HBITMAP anHBitmapDump = CreateCompatibleBitmap (aDstDC, aWidth, aHeight);
1800 HBITMAP anHBitmapOld = (HBITMAP )SelectObject (aMemDC, anHBitmapDump);
1801 BitBlt (aMemDC, 0, 0, aWidth, aHeight, aSrcDC, 0, 0, SRCCOPY);
1802
1803 Standard_Boolean isSuccess = SaveBitmap (anHBitmapDump, theFileName);
1804
1805 // Free objects
1806 DeleteObject (SelectObject (aMemDC, anHBitmapOld));
1807 DeleteDC (aMemDC);
1808
1809 return isSuccess;
1810}
1811
1812/*--------------------------------------------------------*\
1813| DrawString
1814\*--------------------------------------------------------*/
1815void DrawWindow::DrawString(int x,int y, char* text)
1816{
1817 HDC hDC = GetDC(win);
1818 HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
1819
6a7d83c4 1820 TextOut(aWorkDC, x, y, text, (int )strlen(text));
7fd59977 1821
1822 if (myUseBuffer) ReleaseMemDC(aWorkDC);
1823 ReleaseDC(win,hDC);
1824}
1825
1826/*--------------------------------------------------------*\
1827| DrawSegments
1828\*--------------------------------------------------------*/
1829void DrawWindow::DrawSegments(Segment *tab, int nbElem)
1830{
1831 HDC hDC = GetDC(win);
1832 HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
1833
1834 for(int i = 0 ; i < nbElem ; i++)
1835 {
1836 MoveToEx(aWorkDC, tab[i].x1, tab[i].y1, NULL);
1837 LineTo(aWorkDC, tab[i].x2, tab[i].y2);
1838 }
1839
1840 if (myUseBuffer) ReleaseMemDC(aWorkDC);
1841 ReleaseDC(win,hDC);
1842}
1843
1844/*--------------------------------------------------------*\
1845| Redraw
1846\*--------------------------------------------------------*/
1847void DrawWindow::Redraw()
1848{
1849 if (myUseBuffer) {
1850 HDC hDC = GetDC(win);
1851 RECT rc;
1852 GetClientRect(win, &rc);
1853 HDC aMemDC = GetMemDC(hDC);
1854 BitBlt(hDC,
1855 rc.left, rc.top,
1856 rc.right-rc.left, rc.bottom-rc.top,
1857 aMemDC,
1858 0, 0, SRCCOPY);
1859 ReleaseMemDC(aMemDC);
1860 ReleaseDC(win,hDC);
1861 }
1862}
1863
1864/*--------------------------------------------------------*\
1865| SetMode
1866\*--------------------------------------------------------*/
1867void DrawWindow::SetMode(int mode)
1868{
1869 HDC hDC = GetDC(win);
1870 myCurrMode = mode;
1871 SetROP2(hDC, modeTab[mode]);
1872 ReleaseDC(win,hDC);
1873}
1874
1875
1876/*--------------------------------------------------------*\
1877| SetColor
1878\*--------------------------------------------------------*/
1879void DrawWindow::SetColor(Standard_Integer color)
1880{
1881 HDC hDC = GetDC(win);
1882 myCurrPen = color;
1883 SelectObject(hDC,colorPenTab[color]);
1884 ReleaseDC(win,hDC);
1885}
1886
1887
1888/*--------------------------------------------------------*\
1889| WExpose
1890\*--------------------------------------------------------*/
1891void DrawWindow::WExpose()
1892{
1893}
1894
1895
1896/*--------------------------------------------------------*\
1897| WButtonPress
1898\*--------------------------------------------------------*/
1899void DrawWindow::WButtonPress(const Standard_Integer,
1900 const Standard_Integer,
1901 const Standard_Integer&)
1902{
1903}
1904
1905
1906/*--------------------------------------------------------*\
1907| WButtonRelease
1908\*--------------------------------------------------------*/
1909void DrawWindow::WButtonRelease(const Standard_Integer,
1910 const Standard_Integer,
1911 const Standard_Integer&)
1912{
1913}
1914
1915
1916/*--------------------------------------------------------*\
1917| WMotionNotify
1918\*--------------------------------------------------------*/
1919void Draw_Window::WMotionNotify(const Standard_Integer ,
1920 const Standard_Integer )
1921{
1922}
1923
1924
1925/*--------------------------------------------------------*\
1926| WConfigureNotify
1927\*--------------------------------------------------------*/
1928void DrawWindow::WConfigureNotify(const Standard_Integer,
1929 const Standard_Integer,
1930 const Standard_Integer,
1931 const Standard_Integer)
1932{
1933}
1934
1935
1936/*--------------------------------------------------------*\
1937| WUnmapNotify
1938\*--------------------------------------------------------*/
1939void DrawWindow::WUnmapNotify()
1940{
1941}
1942
1943
1944
1945/*
1946** IMPLEMENTATION of the CLASS SEGMENT
1947 */
1948
1949/*--------------------------------------------------------*\
1950| Init
1951\*--------------------------------------------------------*/
1952
1953void Segment::Init(Standard_Integer a1, Standard_Integer a2,
1954 Standard_Integer a3, Standard_Integer a4)
1955{
1956 x1=a1;
1957 y1=a2;
1958 x2=a3;
1959 y2=a4;
1960}
1961
1962static DWORD WINAPI tkLoop(VOID);
1963#ifdef _TK
1964static Tk_Window mainWindow;
1965#endif
1966
1967//* threads sinchronization *//
1968DWORD dwMainThreadId;
1969console_semaphore_value volatile console_semaphore = WAIT_CONSOLE_COMMAND;
4b943aab
S
1970//char console_command[1000];
1971#define COMMAND_SIZE 1000 /* Console Command size */
1972char console_command[COMMAND_SIZE];
7fd59977 1973bool volatile isTkLoopStarted = false;
1974
1975/*--------------------------------------------------------*\
1976| Init_Appli
1977\*--------------------------------------------------------*/
1978Standard_Boolean Init_Appli(HINSTANCE hInst,
1979 HINSTANCE hPrevInst, int nShow, HWND& hWndFrame )
1980{
1981 DWORD IDThread;
1982 HANDLE hThread;
1983 console_semaphore = STOP_CONSOLE;
7fd59977 1984 theCommands.Init();
1985 interp = theCommands.Interp();
1986 Tcl_Init(interp) ;
1987
1988 dwMainThreadId = GetCurrentThreadId();
1989
1990 //necessary for normal Tk operation
1991 hThread = CreateThread(NULL, // no security attributes
1992 0, // use default stack size
1993 (LPTHREAD_START_ROUTINE) tkLoop, // thread function
1994 NULL, // no thread function argument
1995 0, // use default creation flags
1996 &IDThread);
1997 if (!hThread) {
1998 cout << "Tcl/Tk main loop thread not created. Switching to batch mode..." << endl;
1999#ifdef _TK
2000 try {
2001 OCC_CATCH_SIGNALS
2002 Tk_Init(interp) ;
2003 } catch (Standard_Failure) {
2004 cout <<" Pb au lancement de TK_Init "<<endl;
2005 }
2006
2007 Tcl_StaticPackage(interp, "Tk", Tk_Init, (Tcl_PackageInitProc *) NULL);
2008#endif
2009 //since the main Tcl/Tk loop wasn't created --> switch to batch mode
2010 return Standard_False;
2011 }
2012
2013 // san - 06/08/2002 - Time for tkLoop to start; Tk fails to initialize otherwise
2014 while (!isTkLoopStarted)
2015 Sleep(10);
2016
2017 // Saving of window classes
2018 if(!hPrevInst)
2019 if(!RegisterAppClass(hInst))
2020 return(Standard_False);
2021
7fd59977 2022 /*
2023 ** Enter the application message-polling loop. This is the anchor for
2024 ** the application.
2025 */
773f53f1 2026 hWndFrame = !Draw_IsConsoleSubsystem ? CreateAppWindow (hInst) : NULL;
2027 if (hWndFrame != NULL)
7fd59977 2028 {
2029 ShowWindow(hWndFrame,nShow);
2030 UpdateWindow(hWndFrame);
2031 }
2032
2033 return Standard_True;
2034}
2035
bf03eb83 2036Standard_Boolean Draw_Interprete (const char*);
7fd59977 2037
2038/*--------------------------------------------------------*\
2039| readStdinThreadFunc
2040\*--------------------------------------------------------*/
2041static DWORD WINAPI readStdinThreadFunc(VOID)
2042{
2043 if (!Draw_IsConsoleSubsystem) return 1;
302f96fb 2044 for(;;) {
7fd59977 2045 while (console_semaphore != WAIT_CONSOLE_COMMAND)
2046 Sleep(100);
498ce76b 2047 if (fgets(console_command,COMMAND_SIZE,stdin))
7fd59977 2048 {
2049 console_semaphore = HAS_CONSOLE_COMMAND;
2050 }
7fd59977 2051 }
7fd59977 2052}
2053
2054/*--------------------------------------------------------*\
2055| exitProc: finalization handler for Tcl/Tk thread. Forces parent process to die
2056\*--------------------------------------------------------*/
2057void exitProc(ClientData /*dc*/)
2058{
2059 HANDLE proc = GetCurrentProcess();
2060 TerminateProcess(proc, 0);
2061}
2062
2063/*--------------------------------------------------------*\
2064| tkLoop: implements Tk_Main()-like behaviour in a separate thread
2065\*--------------------------------------------------------*/
2066static DWORD WINAPI tkLoop(VOID)
2067{
2068 Tcl_CreateExitHandler(exitProc, 0);
7fd59977 2069#if (TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5))
2070 Tcl_RegisterChannel(theCommands.Interp(), Tcl_GetStdChannel(TCL_STDIN));
2071 Tcl_RegisterChannel(theCommands.Interp(), Tcl_GetStdChannel(TCL_STDOUT));
2072 Tcl_RegisterChannel(theCommands.Interp(), Tcl_GetStdChannel(TCL_STDERR));
2073#endif
2074
2075#ifdef _TK
87c58d4f
K
2076 // initialize the Tk library if not in 'virtual windows' mode
2077 // (virtual windows are created by OCCT with native APIs,
2078 // thus Tk will be useless)
2079 if (!Draw_VirtualWindows)
2080 {
2081 try
2082 {
2083 OCC_CATCH_SIGNALS
2084 Standard_Integer res = Tk_Init (interp);
2085 if (res != TCL_OK)
2086 {
7fb60cfd 2087#if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
2088 cout << "tkLoop: error in Tk initialization. Tcl reported: " << Tcl_GetStringResult(interp) << endl;
2089#else
87c58d4f 2090 cout << "tkLoop: error in Tk initialization. Tcl reported: " << interp->result << endl;
7fb60cfd 2091#endif
87c58d4f
K
2092 }
2093 }
2094 catch (Standard_Failure)
2095 {
2096 cout << "tkLoop: exception in TK_Init\n";
2097 }
2098 Tcl_StaticPackage (interp, "Tk", Tk_Init, (Tcl_PackageInitProc* ) NULL);
2099 mainWindow = Tk_MainWindow (interp);
2100 if (mainWindow == NULL)
2101 {
7fb60cfd 2102#if ((TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5)))
2103 fprintf (stderr, "%s\n", Tcl_GetStringResult(interp));
2104#else
87c58d4f 2105 fprintf (stderr, "%s\n", interp->result);
7fb60cfd 2106#endif
87c58d4f
K
2107 cout << "tkLoop: Tk_MainWindow() returned NULL. Exiting...\n";
2108 Tcl_Exit (0);
2109 }
2110 Tk_Name(mainWindow) = Tk_GetUid (Tk_SetAppName (mainWindow, "Draw"));
7fd59977 2111 }
87c58d4f 2112#endif //#ifdef _TK
7fd59977 2113
87c58d4f 2114 // set signal handler in the new thread
7fd59977 2115 OSD::SetSignal();
2116
87c58d4f 2117 // inform the others that we have started
7fd59977 2118 isTkLoopStarted = true;
2119
2120 while (console_semaphore == STOP_CONSOLE)
2121 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
2122
2123 if (Draw_IsConsoleSubsystem && console_semaphore == WAIT_CONSOLE_COMMAND)
2124 Prompt(interp, 0);
2125
2126 //process a command
87c58d4f
K
2127 Standard_Boolean toLoop = Standard_True;
2128 while (toLoop)
2129 {
7fd59977 2130 while(Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT));
87c58d4f
K
2131 if (console_semaphore == HAS_CONSOLE_COMMAND)
2132 {
2133 if (Draw_Interprete (console_command))
2134 {
2135 if (Draw_IsConsoleSubsystem) Prompt (interp, 0);
2136 }
7fd59977 2137 else
87c58d4f
K
2138 {
2139 if (Draw_IsConsoleSubsystem) Prompt (interp, 1);
2140 }
7fd59977 2141 console_semaphore = WAIT_CONSOLE_COMMAND;
2142 }
2143 else
87c58d4f 2144 {
7fd59977 2145 Sleep(100);
87c58d4f
K
2146 }
2147 #ifdef _TK
2148 // We should not exit until the Main Tk window is closed
2149 toLoop = (Tk_GetNumMainWindows() > 0) || Draw_VirtualWindows;
2150 #endif
7fd59977 2151 }
7fd59977 2152 Tcl_Exit(0);
7fd59977 2153 return 0;
2154}
2155
2156
2157/*--------------------------------------------------------*\
2158| Run_Appli
2159\*--------------------------------------------------------*/
2160void Run_Appli(HWND hWnd)
2161{
2162 MSG msg;
2163 HACCEL hAccel = NULL;
2164
2165 msg.wParam = 1;
2166
2167// if (!(hAccel = LoadAccelerators (hInstance, MAKEINTRESOURCE(ACCEL_ID))))
2168// MessageBox(hWnd, "MDI: Load Accel failure!", "Error", MB_OK);
2169 DWORD IDThread;
2170 HANDLE hThread;
2171 if (Draw_IsConsoleSubsystem) {
2172 hThread = CreateThread(NULL, // no security attributes
2173 0, // use default stack size
2174 (LPTHREAD_START_ROUTINE) readStdinThreadFunc, // thread function
2175 NULL, // no thread function argument
2176 0, // use default creation flags
2177 &IDThread); // returns thread identifier
2178 if (!hThread) {
2179 cout << "pb in creation of the thread reading stdin" << endl;
2180 Draw_IsConsoleSubsystem = Standard_False;
2181 Init_Appli(GetModuleHandle(NULL),
2182 GetModuleHandle(NULL),
2183 1, hWnd); // reinit => create MDI client wnd
2184 }
2185 }
2186
2187 //turn on the command interpretation mechanism (regardless of the mode)
2188 if (console_semaphore == STOP_CONSOLE)
2189 console_semaphore = WAIT_CONSOLE_COMMAND;
2190
2191 //simple Win32 message loop
2192 while (GetMessage(&msg, NULL, 0, 0) > 0)
2193 {
2194 if (!TranslateAccelerator(hWnd, hAccel, &msg))
2195 {
2196 TranslateMessage(&msg);
2197 DispatchMessage(&msg);
2198 }
2199 }
2200 ExitProcess(0);
2201}
2202
2203
2204/*--------------------------------------------------------*\
2205| Destroy_Appli
2206\*--------------------------------------------------------*/
2207void Destroy_Appli(HINSTANCE hInst)
2208{
2209 UnregisterAppClass(hInst);
2210 for (int i = 0 ; i < MAXCOLOR ; i++)
2211 DeleteObject(colorPenTab[i]);
2212}
2213
2214/*--------------------------------------------------------*\
2215| SelectWait
2216\*--------------------------------------------------------*/
2217void DrawWindow::SelectWait(HANDLE& hWnd, int& x, int& y, int& button)
2218{
2219 MSG msg;
2220
2221 msg.wParam = 1;
2222
2223 GetMessage(&msg,NULL,0,0);
2224 while((msg.message != WM_RBUTTONDOWN && msg.message != WM_LBUTTONDOWN) ||
2225 ! ( Draw_IsConsoleSubsystem || IsChild(DrawWindow::hWndClientMDI,msg.hwnd)) )
2226 GetMessage(&msg,NULL,0,0);
2227
2228 hWnd = msg.hwnd;
2229 x = LOWORD(msg.lParam);
2230 y = HIWORD(msg.lParam);
2231 if (msg.message == WM_LBUTTONDOWN)
2232 button = 1;
2233 else
2234 button = 3;
2235}
2236
2237/*--------------------------------------------------------*\
2238| SelectNoWait
2239\*--------------------------------------------------------*/
2240void DrawWindow::SelectNoWait(HANDLE& hWnd, int& x, int& y, int& button)
2241{
2242 MSG msg;
2243
2244 msg.wParam = 1;
2245
2246 GetMessage(&msg,NULL,0,0);
2247 while((msg.message != WM_RBUTTONDOWN && msg.message != WM_LBUTTONDOWN &&
2248 msg.message != WM_MOUSEMOVE) ||
2249 ! ( Draw_IsConsoleSubsystem || IsChild(DrawWindow::hWndClientMDI,msg.hwnd) ) )
2250 GetMessage(&msg,NULL,0,0);
2251 hWnd = msg.hwnd;
2252 x = LOWORD(msg.lParam);
2253 y = HIWORD(msg.lParam);
2254 switch (msg.message)
2255 {
2256 case WM_LBUTTONDOWN :
2257 button = 1;
2258 break;
2259
2260 case WM_RBUTTONDOWN :
2261 button = 3;
2262 break;
2263
2264 case WM_MOUSEMOVE :
2265 button = 0;
2266 break;
2267 }
2268}
2269
2270Standard_Boolean DrawWindow::DefineColor (const Standard_Integer, const char*)
2271{
2272 return Standard_True;
2273};
2274
2275#endif