vtkTkImageViewerWidget.cxx 30.1 KB
Newer Older
1 2 3 4 5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkTkImageViewerWidget.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
13 14 15 16 17

=========================================================================*/
#include <stdlib.h>

#include "vtkTkImageViewerWidget.h"
18
#include "vtkRenderWindowInteractor.h"
19
#include "vtkTkInternals.h"
20

21
#ifdef _MSC_VER
22
 #pragma warning ( disable : 4273 )
23
#else
24 25 26 27 28 29 30 31 32
 #ifdef VTK_USE_CARBON
  #include "vtkCarbonRenderWindow.h"
 #else
  #ifdef VTK_USE_COCOA
   #include "vtkCocoaRenderWindow.h"
  #else
   #include "vtkXOpenGLRenderWindow.h"
  #endif
 #endif
Yves Starreveld's avatar
 
Yves Starreveld committed
33
#endif
34 35

#define VTK_ALL_EVENTS_MASK \
36 37
    KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask|      \
    EnterWindowMask|LeaveWindowMask|PointerMotionMask|ExposureMask|     \
38 39
    VisibilityChangeMask|FocusChangeMask|PropertyChangeMask|ColormapChangeMask

40
#define VTK_MAX(a,b)    (((a)>(b))?(a):(b))
41
    
42 43 44 45 46 47 48 49 50 51
#if ( _MSC_VER >= 1300 ) // Visual studio .NET
#pragma warning ( disable : 4311 )
#pragma warning ( disable : 4312 )
#  define vtkGetWindowLong GetWindowLongPtr
#  define vtkSetWindowLong SetWindowLongPtr
#else // regular Visual studio 
#  define vtkGetWindowLong GetWindowLong
#  define vtkSetWindowLong SetWindowLong
#endif // 

52 53 54 55
// These are the options that can be set when the widget is created
// or with the command configure.  The only new one is "-rw" which allows
// the uses to set their own ImageViewer window.
static Tk_ConfigSpec vtkTkImageViewerWidgetConfigSpecs[] = {
56 57
    {TK_CONFIG_PIXELS, (char *) "-height", (char *) "height", (char *) "Height",
     (char *) "400", Tk_Offset(struct vtkTkImageViewerWidget, Height), 0, NULL},
58
  
59 60
    {TK_CONFIG_PIXELS, (char *) "-width", (char *) "width", (char *) "Width",
     (char *) "400", Tk_Offset(struct vtkTkImageViewerWidget, Width), 0, NULL},
61
  
62 63
    {TK_CONFIG_STRING, (char *) "-iv", (char *) "iv", (char *) "IV",
     (char *) "", Tk_Offset(struct vtkTkImageViewerWidget, IV), 0, NULL},
64 65 66 67 68 69

    {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
     (char *) NULL, 0, 0, NULL}
};


70
// Forward prototypes
Berk Geveci's avatar
Berk Geveci committed
71 72
extern "C"
{
Berk Geveci's avatar
Berk Geveci committed
73 74
  void vtkTkImageViewerWidget_EventProc(ClientData clientData, 
                                        XEvent *eventPtr);
Berk Geveci's avatar
Berk Geveci committed
75 76
}

77 78
static int vtkTkImageViewerWidget_MakeImageViewer(struct vtkTkImageViewerWidget *self);
extern int vtkImageViewerCommand(ClientData cd, Tcl_Interp *interp,
79
                                 int argc, char *argv[]);
80 81 82 83 84 85

    
//----------------------------------------------------------------------------
// It's possible to change with this function or in a script some
// options like width, hieght or the ImageViewer widget.
int vtkTkImageViewerWidget_Configure(Tcl_Interp *interp, 
86 87
                                     struct vtkTkImageViewerWidget *self,
                                     int argc, char *argv[], int flags) 
88 89
{
  // Let Tk handle generic configure options.
90 91
  if (Tk_ConfigureWidget(interp, 
                         self->TkWin, 
92
                         vtkTkImageViewerWidgetConfigSpecs,
93
                         argc, 
94
#if (TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4 && TCL_RELEASE_LEVEL >= TCL_FINAL_RELEASE)
95
                         const_cast<CONST84 char **>(argv), 
96 97 98 99 100
#else
                         argv, 
#endif
                         (char *)self, 
                         flags) == TCL_ERROR) 
101 102 103
    {
    return(TCL_ERROR);
    }
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  // Get the new  width and height of the widget
  Tk_GeometryRequest(self->TkWin, self->Width, self->Height);
  
  // Make sure the ImageViewer window has been set.  If not, create one.
  if (vtkTkImageViewerWidget_MakeImageViewer(self) == TCL_ERROR) 
    {
    return TCL_ERROR;
    }
  
  return TCL_OK;
}

//----------------------------------------------------------------------------
// This function is called when the ImageViewer widget name is 
// evaluated in a Tcl script.  It will compare string parameters
// to choose the appropriate method to invoke.
Berk Geveci's avatar
Berk Geveci committed
121
extern "C"
122
{
123 124 125
  int vtkTkImageViewerWidget_Widget(ClientData clientData, 
                                    Tcl_Interp *interp,
                                    int argc, 
126 127
#if (TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4 && TCL_RELEASE_LEVEL >= TCL_FINAL_RELEASE)
                                    CONST84
128 129
#endif
                                    char *argv[]) 
Berk Geveci's avatar
Berk Geveci committed
130 131 132 133 134 135 136
  {
    struct vtkTkImageViewerWidget *self = 
      (struct vtkTkImageViewerWidget *)clientData;
    int result = TCL_OK;
    
    // Check to see if the command has enough arguments.
    if (argc < 2) 
137
      {
Berk Geveci's avatar
Berk Geveci committed
138 139 140
      Tcl_AppendResult(interp, "wrong # args: should be \"",
                       argv[0], " ?options?\"", NULL);
      return TCL_ERROR;
141
      }
Berk Geveci's avatar
Berk Geveci committed
142 143 144 145 146 147 148 149
    
    // Make sure the widget is not deleted during this function
    Tk_Preserve((ClientData)self);
    
    
    // Handle render call to the widget
    if (strncmp(argv[1], "render", VTK_MAX(1, strlen(argv[1]))) == 0 || 
        strncmp(argv[1], "Render", VTK_MAX(1, strlen(argv[1]))) == 0) 
150
      {
Berk Geveci's avatar
Berk Geveci committed
151 152 153 154 155 156
      // make sure we have a window
      if (self->ImageViewer == NULL)
        {
        vtkTkImageViewerWidget_MakeImageViewer(self);
        }
      self->ImageViewer->Render();
157
      }
Berk Geveci's avatar
Berk Geveci committed
158 159
    // Handle configure method
    else if (!strncmp(argv[1], "configure", VTK_MAX(1, strlen(argv[1])))) 
160
      {
Berk Geveci's avatar
Berk Geveci committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
      if (argc == 2) 
        {
        /* Return list of all configuration parameters */
        result = Tk_ConfigureInfo(interp, self->TkWin, 
                                  vtkTkImageViewerWidgetConfigSpecs,
                                  (char *)self, (char *)NULL, 0);
        }
      else if (argc == 3) 
        {
        /* Return a specific configuration parameter */
        result = Tk_ConfigureInfo(interp, self->TkWin, 
                                  vtkTkImageViewerWidgetConfigSpecs,
                                  (char *)self, argv[2], 0);
        }
      else 
        {
        /* Execute a configuration change */
178 179 180
        result = vtkTkImageViewerWidget_Configure(interp, 
                                                  self, 
                                                  argc-2, 
181
#if (TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4 && TCL_RELEASE_LEVEL >= TCL_FINAL_RELEASE)
182 183 184 185 186
                                                  const_cast<char **>(argv+2), 
#else
                                                  argv+2, 
#endif
                                                  TK_CONFIG_ARGV_ONLY);
Berk Geveci's avatar
Berk Geveci committed
187
        }
188
      }
Berk Geveci's avatar
Berk Geveci committed
189 190 191 192 193 194 195 196 197
    else if (!strcmp(argv[1], "GetImageViewer"))
      { // Get ImageViewerWindow is my own method
      // Create a ImageViewerWidget if one has not been set yet.
      result = vtkTkImageViewerWidget_MakeImageViewer(self);
      if (result != TCL_ERROR)
        {
        // Return the name (Make Tcl copy the string)
        Tcl_SetResult(interp, self->IV, TCL_VOLATILE);
        }
198
      }
Berk Geveci's avatar
Berk Geveci committed
199
    else 
200
      {
Berk Geveci's avatar
Berk Geveci committed
201 202 203 204
      // Unknown method name.
      Tcl_AppendResult(interp, "vtkTkImageViewerWidget: Unknown option: ", argv[1], 
                       "\n", "Try: configure or GetImageViewer\n", NULL);
      result = TCL_ERROR;
205
      }
Berk Geveci's avatar
Berk Geveci committed
206 207 208 209 210
    
    // Unlock the object so it can be deleted.
    Tk_Release((ClientData)self);
    return result;
  }
211 212 213 214 215 216 217 218 219 220 221
}

//----------------------------------------------------------------------------
// vtkTkImageViewerWidget_Cmd
// Called when vtkTkImageViewerWidget is executed 
// - creation of a vtkTkImageViewerWidget widget.
//     * Creates a new window
//     * Creates an 'vtkTkImageViewerWidget' data structure
//     * Creates an event handler for this window
//     * Creates a command that handles this object
//     * Configures this vtkTkImageViewerWidget for the given arguments
Berk Geveci's avatar
Berk Geveci committed
222
extern "C"
223
{
Berk Geveci's avatar
Berk Geveci committed
224 225
  int vtkTkImageViewerWidget_Cmd(ClientData clientData, 
                                 Tcl_Interp *interp, 
226
                                 int argc, 
227 228
#if (TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4 && TCL_RELEASE_LEVEL >= TCL_FINAL_RELEASE)
                                 CONST84
229 230
#endif
                                 char **argv)
Berk Geveci's avatar
Berk Geveci committed
231
  {
232 233
#if (TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4 && TCL_RELEASE_LEVEL >= TCL_FINAL_RELEASE)
    CONST84
234
#endif
Berk Geveci's avatar
Berk Geveci committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    char *name;
    Tk_Window main = (Tk_Window)clientData;
    Tk_Window tkwin;
    struct vtkTkImageViewerWidget *self;
    
    // Make sure we have an instance name.
    if (argc <= 1) 
      {
      Tcl_ResetResult(interp);
      Tcl_AppendResult(interp, 
                       "wrong # args: should be \"pathName read filename\"", 
                       NULL);
      return(TCL_ERROR);
      }
    
    // Create the window.
    name = argv[1];
    // Possibly X dependent
    tkwin = Tk_CreateWindowFromPath(interp, main, name, (char *) NULL);
    if (tkwin == NULL) 
      {
      return TCL_ERROR;
      }
    
    // Tcl needs this for setting options and matching event bindings.
    Tk_SetClass(tkwin, (char *) "vtkTkImageViewerWidget");
    
    // Create vtkTkImageViewerWidget data structure 
    self = (struct vtkTkImageViewerWidget *)
      ckalloc(sizeof(struct vtkTkImageViewerWidget));
    
    self->TkWin = tkwin;
    self->Interp = interp;
    self->Width = 0;
    self->Height = 0;
    self->ImageViewer = NULL;
    self->IV = NULL;
    
    // ...
    // Create command event handler
    Tcl_CreateCommand(interp, Tk_PathName(tkwin), vtkTkImageViewerWidget_Widget, 
                      (ClientData)self, (void (*)(ClientData)) NULL);
    Tk_CreateEventHandler(tkwin, ExposureMask | StructureNotifyMask,
                          vtkTkImageViewerWidget_EventProc, (ClientData)self);
    
    // Configure vtkTkImageViewerWidget widget
281 282 283
    if (vtkTkImageViewerWidget_Configure(interp, 
                                         self, 
                                         argc-2, 
284
#if (TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4 && TCL_RELEASE_LEVEL >= TCL_FINAL_RELEASE)
285 286 287 288 289
                                         const_cast<char **>(argv+2), 
#else
                                         argv+2, 
#endif
                                         0) 
Berk Geveci's avatar
Berk Geveci committed
290 291 292 293 294 295 296 297 298 299 300 301
        == TCL_ERROR) 
      {
      Tk_DestroyWindow(tkwin);
      Tcl_DeleteCommand(interp, (char *) "vtkTkImageViewerWidget");
      // Don't free it, if we do a crash occurs later...
      //free(self);  
      return TCL_ERROR;
      }
    
    Tcl_AppendResult(interp, Tk_PathName(tkwin), NULL);
    return TCL_OK;
  }
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
}


//----------------------------------------------------------------------------
char *vtkTkImageViewerWidget_IV(const struct vtkTkImageViewerWidget *self)
{
  return self->IV;
}


//----------------------------------------------------------------------------
int vtkTkImageViewerWidget_Width( const struct vtkTkImageViewerWidget *self)
{
   return self->Width;
}


//----------------------------------------------------------------------------
int vtkTkImageViewerWidget_Height( const struct vtkTkImageViewerWidget *self)
{
   return self->Height;
}

Berk Geveci's avatar
Berk Geveci committed
325
extern "C"
326
{
Berk Geveci's avatar
Berk Geveci committed
327
  void vtkTkImageViewerWidget_Destroy(char *memPtr)
Berk Geveci's avatar
Berk Geveci committed
328 329 330 331
  {
    struct vtkTkImageViewerWidget *self = (struct vtkTkImageViewerWidget *)memPtr;
    
    if (self->ImageViewer)
332
      {
333
      int netRefCount = self->ImageViewer->GetReferenceCount();
Berk Geveci's avatar
Berk Geveci committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
      if (self->ImageViewer->GetRenderWindow()->GetInteractor() && 
          self->ImageViewer->GetRenderWindow()->GetInteractor()->GetRenderWindow() == self->ImageViewer->GetRenderWindow() &&
          self->ImageViewer->GetRenderWindow()->GetInteractor()->GetReferenceCount() == 1)
        {
        netRefCount = netRefCount - 1;
        }
      if (netRefCount > 1)
        {
        vtkGenericWarningMacro("A TkImageViewerWidget is being destroyed before it associated vtkImageViewer is destroyed. This is very bad and usually due to the order in which objects are being destroyed. Always destroy the vtkImageViewer before destroying the user interface components.");
        return;
        }
      // Squash the ImageViewer's WindowID
      self->ImageViewer->SetWindowId ( (void*)NULL );
      self->ImageViewer->UnRegister(NULL);
      self->ImageViewer = NULL;
      ckfree (self->IV);
350
      }
Berk Geveci's avatar
Berk Geveci committed
351
    ckfree((char *) memPtr);
Bill Lorensen's avatar
Bill Lorensen committed
352
  }
353 354
}

355 356 357
//----------------------------------------------------------------------------
// This gets called to handle vtkTkImageViewerWidget wind configuration events
// Possibly X dependent
Berk Geveci's avatar
Berk Geveci committed
358
extern "C"
359
{
Berk Geveci's avatar
Berk Geveci committed
360 361
  void vtkTkImageViewerWidget_EventProc(ClientData clientData, 
                                        XEvent *eventPtr) 
Berk Geveci's avatar
Berk Geveci committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
  {
    struct vtkTkImageViewerWidget *self = 
      (struct vtkTkImageViewerWidget *)clientData;
    
    switch (eventPtr->type) 
      {
      case Expose:
        if ((eventPtr->xexpose.count == 0)
            /* && !self->UpdatePending*/) 
          {
          // bid this in tcl now
          //self->ImageViewer->Render();
          }
        break;
      case ConfigureNotify:
        if ( 1 /*Tk_IsMapped(self->TkWin)*/ ) 
378
          {
Berk Geveci's avatar
Berk Geveci committed
379 380 381 382 383
          self->Width = Tk_Width(self->TkWin);
          self->Height = Tk_Height(self->TkWin);
          //Tk_GeometryRequest(self->TkWin,self->Width,self->Height);
          if (self->ImageViewer)
            {
384
#ifdef VTK_USE_CARBON
Berk Geveci's avatar
Berk Geveci committed
385 386 387
            TkWindow *winPtr = (TkWindow *)self->TkWin;
            self->ImageViewer->SetPosition(winPtr->privatePtr->xOff,
                                           winPtr->privatePtr->yOff);
388
#else
Berk Geveci's avatar
Berk Geveci committed
389
            self->ImageViewer->SetPosition(Tk_X(self->TkWin),Tk_Y(self->TkWin));
390
#endif
Berk Geveci's avatar
Berk Geveci committed
391 392 393 394
            self->ImageViewer->SetSize(self->Width, self->Height);
            }
          
          //vtkTkImageViewerWidget_PostRedisplay(self);
395
          }
Berk Geveci's avatar
Berk Geveci committed
396 397 398 399
        break;
      case MapNotify:
        break;
      case DestroyNotify:
Andy Cedilnik's avatar
Andy Cedilnik committed
400
#ifdef _WIN32
Berk Geveci's avatar
Berk Geveci committed
401 402 403 404 405 406 407
        if (self->ImageViewer->GetRenderWindow()->GetGenericWindowId())
          {
          SetWindowLong((HWND)self->ImageViewer->GetRenderWindow()->GetGenericWindowId(),
                        GWL_USERDATA,(LONG)((TkWindow *)self->TkWin)->window);
          SetWindowLong((HWND)self->ImageViewer->GetRenderWindow()->GetGenericWindowId(),
                        GWL_WNDPROC,(LONG)TkWinChildProc);
          }
Will Schroeder's avatar
Will Schroeder committed
408
#endif
Berk Geveci's avatar
Berk Geveci committed
409 410 411 412 413 414 415
        Tcl_EventuallyFree( (ClientData) self, vtkTkImageViewerWidget_Destroy );
        break;
      default:
        // nothing
        ;
      }
  }
416 417 418 419 420 421 422
}



//----------------------------------------------------------------------------
// vtkTkImageViewerWidget_Init
// Called upon system startup to create vtkTkImageViewerWidget command.
423
extern "C" {VTK_TK_EXPORT int Vtktkimageviewerwidget_Init(Tcl_Interp *interp);}
424 425
int Vtktkimageviewerwidget_Init(Tcl_Interp *interp)
{
Sebastien Barre's avatar
Sebastien Barre committed
426
  if(Tcl_PkgPresent(interp, (char *)"Tk", (char *)TK_VERSION, 0))
427
    {
428 429 430 431 432 433 434
    Tcl_CreateCommand(interp, (char *) "vtkTkImageViewerWidget", 
                      vtkTkImageViewerWidget_Cmd, 
                      Tk_MainWindow(interp), NULL);
    if (Tcl_PkgProvide(interp, (char *) "Vtktkimageviewerwidget", (char *) "1.2") != TCL_OK) 
      {
      return TCL_ERROR;
      }
435 436 437 438 439 440 441 442 443 444
    }
  return TCL_OK;
}


// Here is the windows specific code for creating the window
// The Xwindows version follows after this
#ifdef _WIN32

LRESULT APIENTRY vtkTkImageViewerWidgetProc(HWND hWnd, UINT message, 
445
                                            WPARAM wParam, LPARAM lParam)
446 447 448
{
  LRESULT rval;
  struct vtkTkImageViewerWidget *self = 
449
    (struct vtkTkImageViewerWidget *)vtkGetWindowLong(hWnd,GWL_USERDATA);
450
  
Jim Miller's avatar
Style  
Jim Miller committed
451 452 453 454
  if (!self)
    {
    return 0;
    }
455

456
  // forward message to Tk handler
457
  vtkSetWindowLong(hWnd,GWL_USERDATA,(LONG)((TkWindow *)self->TkWin)->window);
458 459
  if (((TkWindow *)self->TkWin)->parentPtr)
    {
460
    vtkSetWindowLong(hWnd,GWL_WNDPROC,(LONG)TkWinChildProc);
461 462 463 464
    rval = TkWinChildProc(hWnd,message,wParam,lParam);
    }
  else
    {
465 466 467 468 469
//
// TkWinTopLevelProc has been deprecated in Tcl/Tk8.0.  Not sure how 
// well this will actually work in 8.0.
//
#if (TK_MAJOR_VERSION < 8)
470
    vtkSetWindowLong(hWnd,GWL_WNDPROC,(LONG)TkWinTopLevelProc);
471
    rval = TkWinTopLevelProc(hWnd,message,wParam,lParam);
472
#else
Ken Martin's avatar
Ken Martin committed
473 474 475
    if (message == WM_WINDOWPOSCHANGED) 
      {
      XEvent event;
476 477
            WINDOWPOS *pos = (WINDOWPOS *) lParam;
            TkWindow *winPtr = (TkWindow *) Tk_HWNDToWindow(pos->hwnd);
Ken Martin's avatar
Ken Martin committed
478
    
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
            if (winPtr == NULL) {
              return 0;
              }

            /*
             * Update the shape of the contained window.
             */
            if (!(pos->flags & SWP_NOSIZE)) {
              winPtr->changes.width = pos->cx;
              winPtr->changes.height = pos->cy;
              }
            if (!(pos->flags & SWP_NOMOVE)) {
              winPtr->changes.x = pos->x;
              winPtr->changes.y = pos->y;
              }
Ken Martin's avatar
Ken Martin committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513


      /*
       *  Generate a ConfigureNotify event.
       */
      event.type = ConfigureNotify;
      event.xconfigure.serial = winPtr->display->request;
      event.xconfigure.send_event = False;
      event.xconfigure.display = winPtr->display;
      event.xconfigure.event = winPtr->window;
      event.xconfigure.window = winPtr->window;
      event.xconfigure.border_width = winPtr->changes.border_width;
      event.xconfigure.override_redirect = winPtr->atts.override_redirect;
      event.xconfigure.x = winPtr->changes.x;
      event.xconfigure.y = winPtr->changes.y;
      event.xconfigure.width = winPtr->changes.width;
      event.xconfigure.height = winPtr->changes.height;
      event.xconfigure.above = None;
      Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);

514 515
            Tcl_ServiceAll();
            return 0;
Ken Martin's avatar
Ken Martin committed
516
      }
517
    vtkSetWindowLong(hWnd,GWL_WNDPROC,(LONG)TkWinChildProc);
Ken Martin's avatar
Ken Martin committed
518
    rval = TkWinChildProc(hWnd,message,wParam,lParam);
519
#endif
520 521 522 523
    }

    if (message != WM_PAINT)
      {
524
      if (self->ImageViewer)
525
        {
526 527 528
        vtkSetWindowLong(hWnd,GWL_USERDATA,
                         (LONG)self->ImageViewer->GetRenderWindow());
        vtkSetWindowLong(hWnd,GWL_WNDPROC,(LONG)self->OldProc);
529 530
        CallWindowProc(self->OldProc,hWnd,message,wParam,lParam);
        }
531 532 533
      }

    // now reset to the original config
534 535
    vtkSetWindowLong(hWnd,GWL_USERDATA,(LONG)self);
    vtkSetWindowLong(hWnd,GWL_WNDPROC,(LONG)vtkTkImageViewerWidgetProc);
536 537 538 539 540 541 542 543 544 545 546
    return rval;
}

//-----------------------------------------------------------------------------
// Creates a ImageViewer window and forces Tk to use the window.
static int vtkTkImageViewerWidget_MakeImageViewer(struct vtkTkImageViewerWidget *self) 
{
  Display *dpy;
  TkWindow *winPtr = (TkWindow *) self->TkWin;
  Tcl_HashEntry *hPtr;
  int new_flag;
Sebastien Barre's avatar
Sebastien Barre committed
547
  vtkImageViewer *ImageViewer = NULL;
548 549
  TkWinDrawable *twdPtr;
  HWND parentWin;
550
  vtkRenderWindow *ImageWindow;
551 552 553 554 555

  if (self->ImageViewer)
    {
    return TCL_OK;
    }
Ken Martin's avatar
Ken Martin committed
556 557

  dpy = Tk_Display(self->TkWin);
558 559 560 561 562 563 564 565 566
  
  if (winPtr->window != None) 
    {
    // XDestroyWindow(dpy, winPtr->window);
    }

  if (self->IV[0] == '\0')
    {
    // Make the ImageViewer window.
567
    self->ImageViewer = ImageViewer = vtkImageViewer::New();
Ken Martin's avatar
Ken Martin committed
568
#ifndef VTK_PYTHON_BUILD
569
    vtkTclGetObjectFromPointer(self->Interp, self->ImageViewer,
570
                               vtkImageViewerCommand);
Ken Martin's avatar
Ken Martin committed
571
#endif
Ken Martin's avatar
Ken Martin committed
572
    ckfree (self->IV);
573 574 575 576 577
    self->IV = strdup(self->Interp->result);
    self->Interp->result[0] = '\0';
    }
  else
    {
578 579 580 581 582 583 584 585 586 587
    // is IV an address ? big ole python hack here
    if (self->IV[0] == 'A' && self->IV[1] == 'd' && 
        self->IV[2] == 'd' && self->IV[3] == 'r')
      {
      void *tmp;
      sscanf(self->IV+5,"%p",&tmp);
      ImageViewer = (vtkImageViewer *)tmp;
      }
    else
      {
Ken Martin's avatar
Ken Martin committed
588
#ifndef VTK_PYTHON_BUILD
589 590 591
      ImageViewer = (vtkImageViewer *)
        vtkTclGetPointerFromObject(self->IV, "vtkImageViewer", self->Interp,
                                   new_flag);
Ken Martin's avatar
Ken Martin committed
592
#endif
593
      }
594 595 596
    if (ImageViewer != self->ImageViewer)
      {
      if (self->ImageViewer != NULL)
597 598 599
        {
        self->ImageViewer->UnRegister(NULL);
        }
600 601
      self->ImageViewer = (vtkImageViewer *)(ImageViewer);
      if (self->ImageViewer != NULL)
602 603 604
        {
        self->ImageViewer->Register(NULL);
        }
605
      }
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
    }
  
  // Set the size
  self->ImageViewer->SetSize(self->Width, self->Height);
  
  // Set the parent correctly
  // Possibly X dependent
  if ((winPtr->parentPtr != NULL) && !(winPtr->flags & TK_TOP_LEVEL)) 
    {
    if (winPtr->parentPtr->window == None) 
      {
      Tk_MakeWindowExist((Tk_Window) winPtr->parentPtr);
      }

    parentWin = ((TkWinDrawable *)winPtr->parentPtr->window)->window.handle;
    ImageViewer->SetParentId(parentWin);
    }
  
  // Use the same display
  self->ImageViewer->SetDisplayId(dpy);
  
  /* Make sure Tk knows to switch to the new colormap when the cursor
   * is over this window when running in color index mode.
   */
  //Tk_SetWindowVisual(self->TkWin, ImageViewer->GetDesiredVisual(), 
  //ImageViewer->GetDesiredDepth(), 
  //ImageViewer->GetDesiredColormap());
  
  self->ImageViewer->Render();  
635
  ImageWindow = self->ImageViewer->GetRenderWindow();
636

Ken Martin's avatar
Ken Martin committed
637
#if(TK_MAJOR_VERSION >=  8)
Ken Martin's avatar
Ken Martin committed
638
  twdPtr = (TkWinDrawable*)Tk_AttachHWND(self->TkWin, (HWND)ImageWindow->GetGenericWindowId());
Ken Martin's avatar
Ken Martin committed
639
#else
640 641 642
  twdPtr = (TkWinDrawable*) ckalloc(sizeof(TkWinDrawable));
  twdPtr->type = TWD_WINDOW;
  twdPtr->window.winPtr = winPtr;
Ken Martin's avatar
Ken Martin committed
643
  twdPtr->window.handle = (HWND)ImageWindow->GetGenericWindowId();
Ken Martin's avatar
Ken Martin committed
644 645
#endif
  
646 647 648 649
  self->OldProc = (WNDPROC)vtkGetWindowLong(twdPtr->window.handle,GWL_WNDPROC);
  vtkSetWindowLong(twdPtr->window.handle,GWL_USERDATA,(LONG)self);
  vtkSetWindowLong(twdPtr->window.handle,GWL_WNDPROC,
                   (LONG)vtkTkImageViewerWidgetProc);
650 651 652 653

  winPtr->window = (Window)twdPtr;
  
  hPtr = Tcl_CreateHashEntry(&winPtr->dispPtr->winTable,
654
                             (char *) winPtr->window, &new_flag);
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
  Tcl_SetHashValue(hPtr, winPtr);
  
  winPtr->dirtyAtts = 0;
  winPtr->dirtyChanges = 0;
#ifdef TK_USE_INPUT_METHODS
  winPtr->inputContext = NULL;
#endif // TK_USE_INPUT_METHODS 

  if (!(winPtr->flags & TK_TOP_LEVEL)) 
    {
    /*
     * If this window has a different colormap than its parent, add
     * the window to the WM_COLORMAP_WINDOWS property for its top-level.
     */
    if ((winPtr->parentPtr != NULL) &&
670
              (winPtr->atts.colormap != winPtr->parentPtr->atts.colormap)) 
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
      {
      TkWmAddToColormapWindows(winPtr);
      }
    } 

  /*
   * Issue a ConfigureNotify event if there were deferred configuration
   * changes (but skip it if the window is being deleted;  the
   * ConfigureNotify event could cause problems if we're being called
   * from Tk_DestroyWindow under some conditions).
   */
  if ((winPtr->flags & TK_NEED_CONFIG_NOTIFY)
      && !(winPtr->flags & TK_ALREADY_DEAD))
    {
    XEvent event;
    
    winPtr->flags &= ~TK_NEED_CONFIG_NOTIFY;
    
    event.type = ConfigureNotify;
    event.xconfigure.serial = LastKnownRequestProcessed(winPtr->display);
    event.xconfigure.send_event = False;
    event.xconfigure.display = winPtr->display;
    event.xconfigure.event = winPtr->window;
    event.xconfigure.window = winPtr->window;
    event.xconfigure.x = winPtr->changes.x;
    event.xconfigure.y = winPtr->changes.y;
    event.xconfigure.width = winPtr->changes.width;
    event.xconfigure.height = winPtr->changes.height;
    event.xconfigure.border_width = winPtr->changes.border_width;
    if (winPtr->changes.stack_mode == Above) 
      {
      event.xconfigure.above = winPtr->changes.sibling;
      }
    else 
      {
      event.xconfigure.above = None;
      }
    event.xconfigure.override_redirect = winPtr->atts.override_redirect;
    Tk_HandleEvent(&event);
    }

  return TCL_OK;
}

715
// now the APPLE version - only available using the Carbon APIs
Yves Starreveld's avatar
 
Yves Starreveld committed
716
#else
717
#ifdef VTK_USE_CARBON
Yves Starreveld's avatar
 
Yves Starreveld committed
718 719 720 721 722 723
//----------------------------------------------------------------------------
// Creates a ImageViewer window and forces Tk to use the window.
static int
vtkTkImageViewerWidget_MakeImageViewer(struct vtkTkImageViewerWidget *self) 
{
  Display *dpy;
724
  TkWindow *winPtr = (TkWindow *)self->TkWin;
Yves Starreveld's avatar
 
Yves Starreveld committed
725
  vtkImageViewer *ImageViewer;
726
  vtkCarbonRenderWindow *ImageWindow;
727
  WindowPtr parentWin;
Yves Starreveld's avatar
 
Yves Starreveld committed
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
  
  if (self->ImageViewer)
    {
    return TCL_OK;
    }

  dpy = Tk_Display(self->TkWin);
  
  if (self->IV[0] == '\0')
    {
    // Make the ImageViewer window.
    self->ImageViewer = vtkImageViewer::New();
    ImageViewer = self->ImageViewer;
#ifndef VTK_PYTHON_BUILD
    vtkTclGetObjectFromPointer(self->Interp, self->ImageViewer,
                               vtkImageViewerCommand);
#endif
Ken Martin's avatar
Ken Martin committed
745
    ckfree (self->IV);
Yves Starreveld's avatar
 
Yves Starreveld committed
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
    self->IV = strdup(self->Interp->result);
    self->Interp->result[0] = '\0';
    }
  else
    {
    // is IV an address ? big ole python hack here
    if (self->IV[0] == 'A' && self->IV[1] == 'd' && 
        self->IV[2] == 'd' && self->IV[3] == 'r')
      {
      void *tmp;
      sscanf(self->IV+5,"%p",&tmp);
      ImageViewer = (vtkImageViewer *)tmp;
      }
    else
      {
#ifndef VTK_PYTHON_BUILD
      int new_flag;
      ImageViewer = (vtkImageViewer *)
        vtkTclGetPointerFromObject(self->IV, "vtkImageViewer", self->Interp,
                                   new_flag);
#endif
      }
    if (ImageViewer != self->ImageViewer)
      {
      if (self->ImageViewer != NULL)
        {
        self->ImageViewer->UnRegister(NULL);
        }
      self->ImageViewer = (vtkImageViewer *)(ImageViewer);
      if (self->ImageViewer != NULL)
        {
        self->ImageViewer->Register(NULL);
        }
      }
    }
  
        
  // get the window
784
  ImageWindow = static_cast<vtkCarbonRenderWindow *>(ImageViewer->GetRenderWindow());
Yves Starreveld's avatar
 
Yves Starreveld committed
785 786 787 788 789 790 791 792 793
  // If the imageviewer has already created it's window, throw up our hands and quit...
  if ( ImageWindow->GetWindowId() != (Window)NULL )
    {
    return TCL_ERROR;
    }
        
  // Use the same display
  ImageWindow->SetDisplayId(dpy);

794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
  // Set the parent correctly and get the actual OSX window on the screen
  // Window must be up so that the aglContext can be attached to it
  if ((winPtr->parentPtr != NULL) && !(winPtr->flags & TK_TOP_LEVEL))
    {
      if (winPtr->parentPtr->window == None)
        {
        // Look at each parent TK window in order until we run out
        // of windows or find the top level. Then the OSX window that will be
        // the parent is created so that we have a window to pass to the 
        // vtkRenderWindow so it can attach its openGL context.
        // Ideally the Tk_MakeWindowExist call would do the deed. (I think)
        TkWindow *curWin = winPtr->parentPtr;
        while ((NULL != curWin->parentPtr) && !(curWin->flags & TK_TOP_LEVEL))
          {
          curWin = curWin->parentPtr;
          }
        Tk_MakeWindowExist((Tk_Window) winPtr->parentPtr);
        if (NULL != curWin)
          {
          TkMacOSXMakeRealWindowExist(curWin);
          }
        else
          {
          vtkGenericWarningMacro("Could not find the TK_TOP_LEVEL. This is bad.");
          }
        }

      parentWin = GetWindowFromPort(TkMacOSXGetDrawablePort(
                                    Tk_WindowId(winPtr->parentPtr)));
      // Carbon does not have 'sub-windows', so the ParentId is used more
      // as a flag to indicate that the renderwindow is being used as a sub-
      // view of its 'parent' window.
      ImageWindow->SetParentId(parentWin);
      ImageWindow->SetWindowId(parentWin);
    }

Yves Starreveld's avatar
 
Yves Starreveld committed
830 831 832 833 834 835 836 837 838 839

  // Set the size
  self->ImageViewer->SetSize(self->Width, self->Height);


  self->ImageViewer->Render();          
  return TCL_OK;
}


840 841 842 843 844 845 846 847 848
// now the Xwindows version
#else

//----------------------------------------------------------------------------
// Creates a ImageViewer window and forces Tk to use the window.
static int
vtkTkImageViewerWidget_MakeImageViewer(struct vtkTkImageViewerWidget *self) 
{
  Display *dpy;
849
  vtkImageViewer *ImageViewer = 0;
Yves Starreveld's avatar
 
Yves Starreveld committed
850
  vtkXOpenGLRenderWindow *ImageWindow;
851 852 853 854 855
  
  if (self->ImageViewer)
    {
    return TCL_OK;
    }
Dan Blezek's avatar
Dan Blezek committed
856

857 858
  dpy = Tk_Display(self->TkWin);
  
859
  if (Tk_WindowId(self->TkWin) != None) 
860
    {
861
    XDestroyWindow(dpy, Tk_WindowId(self->TkWin) );
862 863 864 865 866 867
    }

  if (self->IV[0] == '\0')
    {
    // Make the ImageViewer window.
    self->ImageViewer = vtkImageViewer::New();
868
    ImageViewer = self->ImageViewer;
Ken Martin's avatar
Ken Martin committed
869
#ifndef VTK_PYTHON_BUILD
870
    vtkTclGetObjectFromPointer(self->Interp, self->ImageViewer,
871
                               vtkImageViewerCommand);
Ken Martin's avatar
Ken Martin committed
872
#endif
873 874 875 876 877
    self->IV = strdup(self->Interp->result);
    self->Interp->result[0] = '\0';
    }
  else
    {
878 879
    // is IV an address ? big ole python hack here
    if (self->IV[0] == 'A' && self->IV[1] == 'd' && 
880
        self->IV[2] == 'd' && self->IV[3] == 'r')
881 882 883 884 885 886 887
      {
      void *tmp;
      sscanf(self->IV+5,"%p",&tmp);
      ImageViewer = (vtkImageViewer *)tmp;
      }
    else
      {
Ken Martin's avatar
Ken Martin committed
888
#ifndef VTK_PYTHON_BUILD
889 890
      int new_flag;
      ImageViewer = (vtkImageViewer *)
891 892
        vtkTclGetPointerFromObject(self->IV, "vtkImageViewer", self->Interp,
                                   new_flag);
Ken Martin's avatar
Ken Martin committed
893
#endif
894
      }
895 896 897
    if (ImageViewer != self->ImageViewer)
      {
      if (self->ImageViewer != NULL)
898 899 900
        {
        self->ImageViewer->UnRegister(NULL);
        }
901 902
      self->ImageViewer = (vtkImageViewer *)(ImageViewer);
      if (self->ImageViewer != NULL)
903 904 905
        {
        self->ImageViewer->Register(NULL);
        }
906
      }
907 908
    }
  
909
        
910
  // get the window
Yves Starreveld's avatar
 
Yves Starreveld committed
911
  ImageWindow = static_cast<vtkXOpenGLRenderWindow *>(ImageViewer->GetRenderWindow());
912 913 914 915 916
  // If the imageviewer has already created it's window, throw up our hands and quit...
  if ( ImageWindow->GetWindowId() != (Window)NULL )
    {
    return TCL_ERROR;
    }
917
        
918 919 920 921
  // Use the same display
  ImageWindow->SetDisplayId(dpy);
  // The visual MUST BE SET BEFORE the window is created.
  Tk_SetWindowVisual(self->TkWin, ImageWindow->GetDesiredVisual(), 
922 923
                     ImageWindow->GetDesiredDepth(), 
                     ImageWindow->GetDesiredColormap());
924

925 926 927 928
  // Make this window exist, then use that information to make the vtkImageViewer in sync
  Tk_MakeWindowExist ( self->TkWin );
  ImageViewer->SetWindowId ( (void*)Tk_WindowId ( self->TkWin ) );

929 930
  // Set the size
  self->ImageViewer->SetSize(self->Width, self->Height);
931

932 933
  // Set the parent correctly
  // Possibly X dependent
934
  if ((Tk_Parent(self->TkWin) == NULL) || (Tk_IsTopLevel(self->TkWin))) 
935
    {
936
    ImageWindow->SetParentId(XRootWindow(Tk_Display(self->TkWin), Tk_ScreenNumber(self->TkWin)));
937 938 939
    }
  else 
    {
940
    ImageWindow->SetParentId(Tk_WindowId(Tk_Parent(self->TkWin) ));
941 942
    }

943
  self->ImageViewer->Render();          
944 945 946
  return TCL_OK;
}
#endif
Yves Starreveld's avatar
 
Yves Starreveld committed
947
#endif