0028110: Configuration - specify Unicode charset instead of multibyte in project...
[occt.git] / src / Draw / Draw_Window.cxx
index 56517af..ab84f69 100644 (file)
@@ -5,8 +5,8 @@
 //
 // This file is part of Open CASCADE Technology software library.
 //
-// This library is free software; you can redistribute it and / or modify it
-// under the terms of the GNU Lesser General Public version 2.1 as published
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
 // by the Free Software Foundation, with special exception defined in the file
 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
 // distribution for complete text of the license and disclaimer of any warranty.
@@ -15,7 +15,7 @@
 // commercial license or contractual agreement.
 
 // include windows.h first to have all definitions available
-#ifdef WNT
+#ifdef _WIN32
 #include <windows.h>
 #endif
 
 
 #include <tcl.h>
 #include <Draw_Interpretor.hxx>
+#include <Draw_Window.hxx>
 #include <Draw_Appli.hxx>
 #include <TCollection_AsciiString.hxx>
+#include <TCollection_ExtendedString.hxx>
 #include <Image_AlienPixMap.hxx>
+#include <NCollection_List.hxx>
 
 extern Draw_Interpretor theCommands;
 extern Standard_Boolean Draw_VirtualWindows;
 static Tcl_Interp *interp;        /* Interpreter for this application. */
+static NCollection_List<Draw_Window::FCallbackBeforeTerminate> MyCallbacks;
+
+void Draw_Window::AddCallbackBeforeTerminate(FCallbackBeforeTerminate theCB)
+{
+  MyCallbacks.Append(theCB);
+}
+
+void Draw_Window::RemoveCallbackBeforeTerminate(FCallbackBeforeTerminate theCB)
+{
+  NCollection_List<Draw_Window::FCallbackBeforeTerminate>::Iterator Iter(MyCallbacks);
+  for(; Iter.More(); Iter.Next())
+  {
+    if (Iter.Value() == theCB)
+    {
+      MyCallbacks.Remove(Iter);
+      break;
+    }
+  }
+}
 
 /*
  *----------------------------------------------------------------------
@@ -94,43 +116,9 @@ defaultPrompt:
 
 #if !defined(_WIN32) && !defined(__WIN32__)
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
 #include <OSD_Timer.hxx>
-
-#ifdef HAVE_SYS_TIME_H
-# include <sys/time.h>
-#endif
-
-#ifdef HAVE_SYS_TYPES_H
-# include <sys/types.h>
-#endif
-
-#ifdef HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
-
-#ifdef HAVE_SYS_FILIO_H
-#include <sys/filio.h>
-#else
-#include <sys/ioctl.h>
-#endif
-
-#include <fcntl.h>
-
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
-
 #include <Draw_Window.hxx>
-
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-
-#include <stdio.h>
+#include <unistd.h>
 
 #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
   // use forward declaration for small subset of used Tk functions
@@ -179,8 +167,6 @@ static Standard_Boolean tty;        /* Non-zero means standard input is a
                                  * terminal-like device.  Zero means it's
                                  * a file. */
 
-static unsigned long thePixels[MAXCOLOR];
-
 Standard_Integer Draw_WindowScreen = 0;
 Standard_Boolean Draw_BlackBackGround = Standard_True;
 
@@ -194,6 +180,8 @@ Draw_Window* Draw_Window::firstWindow = NULL;
 #include <X11/Xutil.h>
 #include <Aspect_DisplayConnection.hxx>
 
+static unsigned long thePixels[MAXCOLOR];
+
 Display* Draw_WindowDisplay = NULL;
 Colormap Draw_WindowColorMap;
 static Handle(Aspect_DisplayConnection) Draw_DisplayConnection;
@@ -539,20 +527,20 @@ Standard_Integer Draw_Window::WidthWin() const
 //function : SetTitle
 //purpose  :
 //=======================================================================
-void Draw_Window::SetTitle(const char* title)
+void Draw_Window::SetTitle(const TCollection_AsciiString& theTitle)
 {
-  XStoreName(Draw_WindowDisplay, win, title);
+  XStoreName (Draw_WindowDisplay, win, theTitle.ToCString());
 }
 
 //=======================================================================
 //function : GetTitle
 //purpose  :
 //=======================================================================
-char* Draw_Window::GetTitle()
+TCollection_AsciiString Draw_Window::GetTitle() const
 {
-  char* title;
-  XFetchName(Draw_WindowDisplay, win, &title);
-  return title;
+  char* aTitle = NULL;
+  XFetchName (Draw_WindowDisplay, win, &aTitle);
+  return TCollection_AsciiString (aTitle);
 }
 
 //=======================================================================
@@ -801,8 +789,7 @@ void Draw_Window::Wait (Standard_Boolean wait)
 
 void ProcessEvent(Draw_Window& win, XEvent& xev)
 {
-  Standard_Integer X,Y,button,lenk;
-  char c;
+  Standard_Integer X,Y,button;
   KeySym keysym;
   XComposeStatus stat;
   char chainekey[10];
@@ -829,17 +816,11 @@ void ProcessEvent(Draw_Window& win, XEvent& xev)
     break;
 
   case KeyPress :
-    lenk = XLookupString(&(xev.xkey),
+    XLookupString(&(xev.xkey),
                          chainekey,
                          10,
                          &keysym,
                          &stat);
-    if (lenk==1)
-      c = chainekey[0];
-    else
-      c = '\0';
-
-    //WKeyPress(c,keysym);
     break;
 
   case MotionNotify :
@@ -1082,6 +1063,11 @@ void Run_Appli(Standard_Boolean (*interprete) (const char*))
     }
 
 #endif
+  NCollection_List<Draw_Window::FCallbackBeforeTerminate>::Iterator Iter(MyCallbacks);
+  for(; Iter.More(); Iter.Next())
+  {
+      (*Iter.Value())();
+  }
 }
 
 //======================================================
@@ -1347,7 +1333,7 @@ int modeTab[16] = {R2_BLACK, R2_MASKPEN, R2_MASKPENNOT, R2_COPYPEN,
 HWND DrawWindow::CreateDrawWindow(HWND hWndClient, int nitem)
 {
   if (Draw_IsConsoleSubsystem) {
-    HWND aWin = CreateWindow (DRAWCLASS, DRAWTITLE,
+    HWND aWin = CreateWindowW (DRAWCLASS, DRAWTITLE,
                               WS_OVERLAPPEDWINDOW,
                               1,1,1,1,
                               NULL, NULL,::GetModuleHandle(NULL), NULL);
@@ -1359,14 +1345,9 @@ HWND DrawWindow::CreateDrawWindow(HWND hWndClient, int nitem)
     return aWin;
   }
   else {
-    HANDLE hInstance;
-#ifndef _WIN64
-    hInstance = (HANDLE)GetWindowLong(hWndClient,GWL_HINSTANCE);
-#else
-    hInstance = (HANDLE)GetWindowLong(hWndClient,GWLP_HINSTANCE);
-#endif
+    HANDLE hInstance = (HANDLE )GetWindowLongPtrW (hWndClient, GWLP_HINSTANCE);
 
-    return CreateMDIWindow(DRAWCLASS, DRAWTITLE,
+    return CreateMDIWindowW(DRAWCLASS, DRAWTITLE,
                            WS_CAPTION | WS_CHILD | WS_THICKFRAME,
                            1,1,0,0,
                            hWndClient, (HINSTANCE)hInstance, nitem);
@@ -1379,43 +1360,46 @@ HWND DrawWindow::CreateDrawWindow(HWND hWndClient, int nitem)
 \*--------------------------------------------------------*/
 LRESULT APIENTRY DrawWindow::DrawProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam )
 {
-  DrawWindow* localObjet = (DrawWindow*)GetWindowLong(hWnd, CLIENTWND);
+  DrawWindow* localObjet = (DrawWindow* )GetWindowLongPtrW (hWnd, CLIENTWND);
   if (!localObjet)
+  {
+    return Draw_IsConsoleSubsystem
+         ? DefWindowProcW   (hWnd, wMsg, wParam, lParam)
+         : DefMDIChildProcW (hWnd, wMsg, wParam, lParam);
+  }
+
+  switch (wMsg)
+  {
+    case WM_PAINT:
     {
-      if (Draw_IsConsoleSubsystem)
-        return (DefWindowProc(hWnd, wMsg, wParam, lParam));
+      PAINTSTRUCT ps;
+      BeginPaint (hWnd, &ps);
+      if (localObjet->GetUseBuffer())
+      {
+        localObjet->Redraw();
+      }
       else
-        return(DefMDIChildProc(hWnd, wMsg, wParam, lParam));
+      {
+        localObjet->WExpose();
+      }
+      EndPaint (hWnd, &ps);
+      return 0;
     }
-
-  PAINTSTRUCT ps;
-
-  switch(wMsg)
-  {
-  case WM_PAINT :
-    BeginPaint(hWnd, &ps);
-    if (localObjet->GetUseBuffer())
-      localObjet->Redraw();
-    else
-      localObjet->WExpose();
-    EndPaint(hWnd, &ps);
-    return 0l;
-    break;
-
-  case WM_SIZE:
-    if (localObjet->GetUseBuffer()) {
-      localObjet->InitBuffer();
-      localObjet->WExpose();
-      localObjet->Redraw();
-      return 0l;
+    case WM_SIZE:
+    {
+      if (localObjet->GetUseBuffer())
+      {
+        localObjet->InitBuffer();
+        localObjet->WExpose();
+        localObjet->Redraw();
+        return 0;
+      }
+      break;
     }
-
-  default:
-    if (Draw_IsConsoleSubsystem)
-      return (DefWindowProc(hWnd, wMsg, wParam, lParam));
-    else
-      return(DefMDIChildProc(hWnd, wMsg, wParam, lParam));
   }
+  return Draw_IsConsoleSubsystem
+       ? DefWindowProcW   (hWnd, wMsg, wParam, lParam)
+       : DefMDIChildProcW (hWnd, wMsg, wParam, lParam);
 }
 
 
@@ -1449,7 +1433,7 @@ DrawWindow::DrawWindow() :
 }
 
 //________________________
-DrawWindow::DrawWindow(char* title,
+DrawWindow::DrawWindow(const char* title,
                        Standard_Integer X, Standard_Integer Y,
                        Standard_Integer dX,Standard_Integer dY) :
        win(0),        next(firstWindow), previous(NULL), myMemHbm(NULL), myUseBuffer(Standard_False)
@@ -1459,7 +1443,7 @@ DrawWindow::DrawWindow(char* title,
   Init(X, Y, dX, dY);
   SetTitle(title);
 }
-DrawWindow::DrawWindow(char* title,
+DrawWindow::DrawWindow(const char* title,
                        Standard_Integer X, Standard_Integer Y,
                        Standard_Integer dX,Standard_Integer dY,
                        HWND theWin) :
@@ -1507,8 +1491,8 @@ void DrawWindow::Init(Standard_Integer theXLeft, Standard_Integer theYTop,
 
   // include decorations in the window dimensions
   // to reproduce same behaviour of Xlib window.
-  DWORD aWinStyle   = GetWindowLong (win, GWL_STYLE);
-  DWORD aWinStyleEx = GetWindowLong (win, GWL_EXSTYLE);
+  DWORD aWinStyle   = GetWindowLongW (win, GWL_STYLE);
+  DWORD aWinStyleEx = GetWindowLongW (win, GWL_EXSTYLE);
   HMENU aMenu       = GetMenu (win);
 
   RECT aRect;
@@ -1521,7 +1505,7 @@ void DrawWindow::Init(Standard_Integer theXLeft, Standard_Integer theYTop,
   SetPosition  (aRect.left, aRect.top);
   SetDimension (aRect.right - aRect.left, aRect.bottom - aRect.top);
   // Save the pointer at the instance associated to the window
-  SetWindowLong(win, CLIENTWND, (LONG)this);
+  SetWindowLongPtrW (win, CLIENTWND, (LONG_PTR)this);
   HDC hDC = GetDC(win);
   SetBkColor(hDC, RGB(0, 0, 0));
   myCurrPen  = 3;
@@ -1676,21 +1660,21 @@ Standard_Integer DrawWindow::WidthWin() const
 /*--------------------------------------------------------*\
 |  SetTitle
 \*--------------------------------------------------------*/
-void DrawWindow::SetTitle(char* title)
+void DrawWindow::SetTitle (const TCollection_AsciiString& theTitle)
 {
-  SetWindowText(win, title);
+  const TCollection_ExtendedString aTitleW (theTitle);
+  SetWindowTextW (win, aTitleW.ToWideString());
 }
 
 
 /*--------------------------------------------------------*\
 |  GetTitle
-|    Attention do not forget to unallocate the memory
 \*--------------------------------------------------------*/
-char* DrawWindow::GetTitle()
+TCollection_AsciiString DrawWindow::GetTitle() const
 {
-  char* title=new char[31];
-  GetWindowText(win, title, 30);
-  return title;
+  wchar_t aTitleW[32];
+  GetWindowTextW (win, aTitleW, 30);
+  return TCollection_AsciiString (aTitleW);
 }
 
 
@@ -1829,7 +1813,8 @@ void DrawWindow::DrawString(int x,int y, char* text)
   HDC hDC = GetDC(win);
   HDC aWorkDC = myUseBuffer ? GetMemDC(hDC) : hDC;
 
-  TextOut(aWorkDC, x, y, text, (int )strlen(text));
+  TCollection_ExtendedString textW (text);
+  TextOutW(aWorkDC, x, y, (const wchar_t*)textW.ToExtString(), (int )strlen(text));
 
   if (myUseBuffer) ReleaseMemDC(aWorkDC);
   ReleaseDC(win,hDC);
@@ -1979,9 +1964,8 @@ static Tk_Window mainWindow;
 //* threads sinchronization *//
 DWORD  dwMainThreadId;
 console_semaphore_value volatile console_semaphore = WAIT_CONSOLE_COMMAND;
-//char console_command[1000];
-#define COMMAND_SIZE 1000     /* Console Command size */
-char console_command[COMMAND_SIZE];
+#define THE_COMMAND_SIZE 1000     /* Console Command size */
+wchar_t console_command[THE_COMMAND_SIZE];
 bool volatile isTkLoopStarted = false;
 
 /*--------------------------------------------------------*\
@@ -2050,16 +2034,41 @@ Standard_Boolean Draw_Interprete (const char*);
 /*--------------------------------------------------------*\
 |  readStdinThreadFunc
 \*--------------------------------------------------------*/
-static DWORD WINAPI readStdinThreadFunc(VOID)
+static DWORD WINAPI readStdinThreadFunc()
 {
-  if (!Draw_IsConsoleSubsystem) return 1;
-  for(;;) {
+  if (!Draw_IsConsoleSubsystem)
+  {
+    return 1;
+  }
+
+  // set console locale
+  //setlocale (LC_ALL, ".OCP");
+  for (;;)
+  {
     while (console_semaphore != WAIT_CONSOLE_COMMAND)
-      Sleep(100);
-      if (fgets(console_command,COMMAND_SIZE,stdin))
+    {
+      Sleep (100);
+    }
+
+    DWORD aNbRead = 0;
+    if (ReadConsoleW (GetStdHandle(STD_INPUT_HANDLE), console_command, THE_COMMAND_SIZE, &aNbRead, NULL))
+    //if (fgetws (console_command, THE_COMMAND_SIZE, stdin)) // fgetws() works only for characters within active locale (see setlocale())
+    {
+      console_command[aNbRead] = L'\0';
+      // tcl will skip newline symbols - no need to strip them here
+      /*--aNbRead;
+      for (; aNbRead >= 0; --aNbRead)
       {
-        console_semaphore = HAS_CONSOLE_COMMAND;
-      }
+        if (console_command[aNbRead] == L'\r'
+         || console_command[aNbRead] == L'\n')
+        {
+          console_command[aNbRead] = '\0';
+          continue;
+        }
+        break;
+      }*/
+      console_semaphore = HAS_CONSOLE_COMMAND;
+    }
   }
 }
 
@@ -2068,6 +2077,11 @@ static DWORD WINAPI readStdinThreadFunc(VOID)
 \*--------------------------------------------------------*/
 void exitProc(ClientData /*dc*/)
 {
+  NCollection_List<Draw_Window::FCallbackBeforeTerminate>::Iterator Iter(MyCallbacks);
+  for(; Iter.More(); Iter.Next())
+  {
+      (*Iter.Value())();
+  }
   HANDLE proc = GetCurrentProcess();
   TerminateProcess(proc, 0);
 }
@@ -2079,9 +2093,23 @@ static DWORD WINAPI tkLoop(VOID)
 {
   Tcl_CreateExitHandler(exitProc, 0);
 #if (TCL_MAJOR_VERSION > 8) || ((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 5))
-  Tcl_RegisterChannel(theCommands.Interp(),  Tcl_GetStdChannel(TCL_STDIN));
-  Tcl_RegisterChannel(theCommands.Interp(),  Tcl_GetStdChannel(TCL_STDOUT));
-  Tcl_RegisterChannel(theCommands.Interp(),  Tcl_GetStdChannel(TCL_STDERR));
+  {
+    Tcl_Channel aChannelIn  = Tcl_GetStdChannel (TCL_STDIN);
+    Tcl_Channel aChannelOut = Tcl_GetStdChannel (TCL_STDOUT);
+    Tcl_Channel aChannelErr = Tcl_GetStdChannel (TCL_STDERR);
+    if (aChannelIn != NULL)
+    {
+      Tcl_RegisterChannel (theCommands.Interp(), aChannelIn);
+    }
+    if (aChannelOut != NULL)
+    {
+      Tcl_RegisterChannel (theCommands.Interp(), aChannelOut);
+    }
+    if (aChannelErr != NULL)
+    {
+      Tcl_RegisterChannel (theCommands.Interp(), aChannelErr);
+    }
+  }
 #endif
 
 #ifdef _TK
@@ -2124,7 +2152,7 @@ static DWORD WINAPI tkLoop(VOID)
 #endif //#ifdef _TK
 
   // set signal handler in the new thread
-  OSD::SetSignal();
+  OSD::SetSignal(Standard_False);
 
   // inform the others that we have started
   isTkLoopStarted = true;
@@ -2142,7 +2170,8 @@ static DWORD WINAPI tkLoop(VOID)
     while(Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT));
     if (console_semaphore == HAS_CONSOLE_COMMAND)
     {
-      if (Draw_Interprete (console_command))
+      TCollection_AsciiString aCmdUtf8 (console_command);
+      if (Draw_Interprete (aCmdUtf8.ToCString()))
       {
         if (Draw_IsConsoleSubsystem) Prompt (interp, 0);
       }
@@ -2190,9 +2219,9 @@ void Run_Appli(HWND hWnd)
     if (!hThread) {
       cout << "pb in creation of the thread reading stdin" << endl;
       Draw_IsConsoleSubsystem = Standard_False;
-      Init_Appli(GetModuleHandle(NULL),
-                 GetModuleHandle(NULL),
-                 1, hWnd); // reinit => create MDI client wnd
+      Init_Appli (GetModuleHandleW (NULL),
+                  GetModuleHandleW (NULL),
+                  1, hWnd); // reinit => create MDI client wnd
     }
   }
 
@@ -2201,12 +2230,12 @@ void Run_Appli(HWND hWnd)
     console_semaphore = WAIT_CONSOLE_COMMAND;
 
   //simple Win32 message loop
-  while (GetMessage(&msg, NULL, 0, 0) > 0)
+  while (GetMessage(&msg, NULL, 0, 0) > 0)
   {
-    if (!TranslateAccelerator(hWnd, hAccel, &msg))
+    if (!TranslateAccelerator(hWnd, hAccel, &msg))
     {
-      TranslateMessage(&msg);
-      DispatchMessage(&msg);
+      TranslateMessage (&msg);
+      DispatchMessage(&msg);
     }
   }
   ExitProcess(0);
@@ -2232,10 +2261,12 @@ void DrawWindow::SelectWait(HANDLE& hWnd, int& x, int& y, int& button)
 
   msg.wParam = 1;
 
-  GetMessage(&msg,NULL,0,0);
+  GetMessageW (&msg, NULL, 0, 0);
   while((msg.message != WM_RBUTTONDOWN && msg.message != WM_LBUTTONDOWN) ||
         ! ( Draw_IsConsoleSubsystem || IsChild(DrawWindow::hWndClientMDI,msg.hwnd)) )
-    GetMessage(&msg,NULL,0,0);
+  {
+    GetMessageW (&msg, NULL, 0, 0);
+  }
 
   hWnd = msg.hwnd;
   x = LOWORD(msg.lParam);
@@ -2255,11 +2286,13 @@ void DrawWindow::SelectNoWait(HANDLE& hWnd, int& x, int& y, int& button)
 
   msg.wParam = 1;
 
-  GetMessage(&msg,NULL,0,0);
+  GetMessage(&msg,NULL,0,0);
   while((msg.message != WM_RBUTTONDOWN && msg.message != WM_LBUTTONDOWN &&
         msg.message != WM_MOUSEMOVE) ||
         ! ( Draw_IsConsoleSubsystem || IsChild(DrawWindow::hWndClientMDI,msg.hwnd) ) )
-    GetMessage(&msg,NULL,0,0);
+  {
+    GetMessageW(&msg,NULL,0,0);
+  }
   hWnd = msg.hwnd;
   x = LOWORD(msg.lParam);
   y = HIWORD(msg.lParam);