| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
* Copyright (C) 1990-93 Silicon Graphics, Inc. |
| 39 |
* |
| 40 |
_______________________________________________________________________ |
| 41 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 |
| |
| 43 |
| $Revision: 1.3 $ |
| 44 |
| |
| 45 |
| Classes: |
| 46 |
| SoXt |
| 47 |
| SoXtEventHandler |
| 48 |
| |
| 49 |
| Author(s): David Mott, Nick Thompson, Paul Isaacs, Alain Dumesny |
| 50 |
| |
| 51 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 52 |
_______________________________________________________________________ |
| 53 |
*/ |
| 54 |
|
| 55 |
#include <Inventor/errors/SoDebugError.h> |
| 56 |
#include <Inventor/SoDB.h> |
| 57 |
#include <Inventor/SoInteraction.h> |
| 58 |
#include <Inventor/nodekits/SoNodeKit.h> |
| 59 |
#include <Inventor/sensors/SoSensorManager.h> |
| 60 |
#include <Inventor/Xt/SoXt.h> |
| 61 |
#include <Xm/MessageB.h> |
| 62 |
#include <SoDebug.h> |
| 63 |
#include <X11/Intrinsic.h> |
| 64 |
#include <X11/Xresource.h> |
| 65 |
#include <X11/StringDefs.h> |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
// This SG_ routine is from /usr/src/X11/motif/overlay_demos. |
| 70 |
// Someday, it may be a standard function in a lib, but for |
| 71 |
// now it is just sample code. We include it here for convenience |
| 72 |
// to our users. |
| 73 |
extern "C" { |
| 74 |
int iv_SG_getPopupArgs(Display *, int, ArgList, int *); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
#include <SoDebug.h> |
| 80 |
|
| 81 |
|
| 82 |
static void overlayMenuMappedCB(Widget w, Widget shell, XtPointer); |
| 83 |
static void overlayMenuUnmappedCB(Widget w, Widget shell, XtPointer); |
| 84 |
|
| 85 |
|
| 86 |
// this class ties Inventor sensors into Xt events |
| 87 |
class SoXtEventHandler { |
| 88 |
public: |
| 89 |
SoXtEventHandler(XtAppContext context); |
| 90 |
|
| 91 |
private: |
| 92 |
XtAppContext appContext; |
| 93 |
|
| 94 |
SbBool timerOn; |
| 95 |
XtIntervalId xtTimer; |
| 96 |
SbTime currentDeadline; |
| 97 |
|
| 98 |
SbBool workProcOn; |
| 99 |
XtWorkProcId xtWorkProc; |
| 100 |
|
| 101 |
// Callback used by the timer |
| 102 |
static void xtTimerCallback(XtPointer closure, XtIntervalId *id); |
| 103 |
|
| 104 |
// Callback used by the workproc |
| 105 |
static Boolean xtWorkProcCallback(XtPointer closure); |
| 106 |
|
| 107 |
// Callback for changes to the sensor manager |
| 108 |
static void sensorManagerChangedCallback(void *data); |
| 109 |
|
| 110 |
void setUpCallbacks(); |
| 111 |
}; |
| 112 |
|
| 113 |
class SoExtensionEventHandler { |
| 114 |
public: |
| 115 |
SoExtensionEventHandler( |
| 116 |
Widget widget, |
| 117 |
int eventType, |
| 118 |
XtEventHandler proc, |
| 119 |
XtPointer data); |
| 120 |
|
| 121 |
Widget getWidget() { return widget; } |
| 122 |
int getEventType() { return eventType; } |
| 123 |
XtEventHandler getEventHandler() { return proc; } |
| 124 |
XtPointer getClientData() { return data; } |
| 125 |
|
| 126 |
private: |
| 127 |
Widget widget; |
| 128 |
int eventType; |
| 129 |
XtEventHandler proc; |
| 130 |
XtPointer data; |
| 131 |
}; |
| 132 |
|
| 133 |
// static members |
| 134 |
Widget SoXt::mainWidget = 0; |
| 135 |
SoXtEventHandler * SoXt::eventHandler = NULL; |
| 136 |
SbPList * SoXt::handlerList = NULL; |
| 137 |
|
| 138 |
|
| 139 |
//////////////////////////////////////////////////////////////////////// |
| 140 |
// |
| 141 |
// Description: |
| 142 |
// This initializes the database, interaction, and Xt. |
| 143 |
// It creates a returns a top level shell widget. |
| 144 |
// |
| 145 |
// static public |
| 146 |
// |
| 147 |
Widget |
| 148 |
SoXt::init( |
| 149 |
const char *appName, |
| 150 |
const char *className) |
| 151 |
// |
| 152 |
//////////////////////////////////////////////////////////////////////// |
| 153 |
{ |
| 154 |
// see if we are already initialized! |
| 155 |
if (mainWidget != NULL) |
| 156 |
return mainWidget; |
| 157 |
|
| 158 |
// init Xt, and store the widget it returns. |
| 159 |
XtAppContext appContext; |
| 160 |
char *argv[1]; |
| 161 |
int argc = 1; |
| 162 |
argv[0] = (char *) appName; |
| 163 |
|
| 164 |
mainWidget = XtAppInitialize( |
| 165 |
&appContext, // context will be returned |
| 166 |
className, // application class |
| 167 |
NULL, 0, // options |
| 168 |
(int *) &argc, argv, // command line args (appName!) |
| 169 |
NULL, // fallback_resources |
| 170 |
NULL, 0); // args, num_args |
| 171 |
|
| 172 |
// init Inventor |
| 173 |
SoDB::init(); |
| 174 |
SoNodeKit::init(); |
| 175 |
SoInteraction::init(); |
| 176 |
|
| 177 |
// bind Inventor with Xt |
| 178 |
eventHandler = new SoXtEventHandler(XtWidgetToApplicationContext(mainWidget)); |
| 179 |
|
| 180 |
return mainWidget; |
| 181 |
} |
| 182 |
|
| 183 |
//////////////////////////////////////////////////////////////////////// |
| 184 |
// |
| 185 |
// Description: |
| 186 |
// This initializes the database and interaction. Xt was initialized |
| 187 |
// by the caller. |
| 188 |
// |
| 189 |
// static public |
| 190 |
// |
| 191 |
void |
| 192 |
SoXt::init(Widget topLevelWidget) |
| 193 |
// |
| 194 |
//////////////////////////////////////////////////////////////////////// |
| 195 |
{ |
| 196 |
// see if we are already initialized! |
| 197 |
if (mainWidget != NULL) |
| 198 |
return; |
| 199 |
|
| 200 |
// see if the passed widget is ok |
| 201 |
if (topLevelWidget == NULL) |
| 202 |
return; |
| 203 |
|
| 204 |
// save the widget |
| 205 |
mainWidget = topLevelWidget; |
| 206 |
|
| 207 |
// init Inventor |
| 208 |
SoDB::init(); |
| 209 |
SoNodeKit::init(); |
| 210 |
SoInteraction::init(); |
| 211 |
|
| 212 |
// bind Inventor with Xt |
| 213 |
eventHandler = new SoXtEventHandler(XtWidgetToApplicationContext(mainWidget)); |
| 214 |
} |
| 215 |
|
| 216 |
//////////////////////////////////////////////////////////////////////// |
| 217 |
// |
| 218 |
// Description: |
| 219 |
// This displays the passed widget. |
| 220 |
// |
| 221 |
// static public |
| 222 |
// |
| 223 |
void |
| 224 |
SoXt::show(Widget w) |
| 225 |
// |
| 226 |
//////////////////////////////////////////////////////////////////////// |
| 227 |
{ |
| 228 |
if (w == NULL) |
| 229 |
return; |
| 230 |
|
| 231 |
if (XtIsShell(w)) { |
| 232 |
// raise and de-iconify the window if it already exists |
| 233 |
if (XtWindow(w) != 0) |
| 234 |
XMapRaised(XtDisplay(w), XtWindow(w)); |
| 235 |
else { |
| 236 |
XtRealizeWidget(w); |
| 237 |
XMapWindow(XtDisplay(w), XtWindow(w)); |
| 238 |
} |
| 239 |
} |
| 240 |
else |
| 241 |
XtManageChild(w); |
| 242 |
} |
| 243 |
|
| 244 |
//////////////////////////////////////////////////////////////////////// |
| 245 |
// |
| 246 |
// Description: |
| 247 |
// This hides the passed widget. |
| 248 |
// |
| 249 |
// static public |
| 250 |
// |
| 251 |
void |
| 252 |
SoXt::hide(Widget w) |
| 253 |
// |
| 254 |
//////////////////////////////////////////////////////////////////////// |
| 255 |
{ |
| 256 |
if (w == NULL) |
| 257 |
return; |
| 258 |
|
| 259 |
// ??? temporary disable the idle queue timer to prevent a redraw timer |
| 260 |
// ??? from happening before we get chance to process all unmap events |
| 261 |
// ??? that will be generated from this hide(). SoXtComponents will |
| 262 |
// ??? depend of those unmap events to know that they are not visible. |
| 263 |
// ??? .....see comment below about XSync()...... |
| 264 |
SbTime savedTime = SoDB::getDelaySensorTimeout(); |
| 265 |
if (savedTime != SbTime::zero()) |
| 266 |
SoDB::setDelaySensorTimeout(0.0); |
| 267 |
|
| 268 |
if (XtIsShell(w)) { |
| 269 |
if (XtWindow(w)) |
| 270 |
XUnmapWindow(XtDisplay(w), XtWindow(w)); |
| 271 |
} |
| 272 |
else |
| 273 |
XtUnmanageChild(w); |
| 274 |
|
| 275 |
// call XSync() to force the unmap events to be received by the X server, |
| 276 |
// which are needed to prevent SoXtComponents from redrawing into a |
| 277 |
// window that is unmaped. Redrawing to an unmapped window seems to |
| 278 |
// screw up some hardware gfx the NEXT time you redraw to the |
| 279 |
// front buffer. (see bug 290854) |
| 280 |
XSync(XtDisplay(w), FALSE); |
| 281 |
|
| 282 |
// ??? restore the timer. This actually will give us about 1/10 second to |
| 283 |
// ??? process all unmap events...idealy we could use an XSendEvent() to |
| 284 |
// ??? restore the timer with our onw event. |
| 285 |
if (savedTime != SbTime::zero()) |
| 286 |
SoDB::setDelaySensorTimeout(savedTime); |
| 287 |
} |
| 288 |
|
| 289 |
//////////////////////////////////////////////////////////////////////// |
| 290 |
// |
| 291 |
// Description: |
| 292 |
// This adds the passed proc as an event handler for an X extension |
| 293 |
// event. |
| 294 |
// |
| 295 |
// static public |
| 296 |
// |
| 297 |
void |
| 298 |
SoXt::addExtensionEventHandler( |
| 299 |
Widget w, |
| 300 |
int extensionEventType, |
| 301 |
XtEventHandler proc, |
| 302 |
XtPointer clientData) |
| 303 |
// |
| 304 |
//////////////////////////////////////////////////////////////////////// |
| 305 |
{ |
| 306 |
if (handlerList == NULL) |
| 307 |
handlerList = new SbPList; |
| 308 |
|
| 309 |
// add the passed proc as an event handler for extension events on w |
| 310 |
SoExtensionEventHandler *data = new SoExtensionEventHandler |
| 311 |
(w, extensionEventType, proc, clientData); |
| 312 |
|
| 313 |
handlerList->append(data); |
| 314 |
} |
| 315 |
|
| 316 |
//////////////////////////////////////////////////////////////////////// |
| 317 |
// |
| 318 |
// Description: |
| 319 |
// This removes the passed proc as an event handler for and X extension |
| 320 |
// event. |
| 321 |
// |
| 322 |
// static public |
| 323 |
// |
| 324 |
void |
| 325 |
SoXt::removeExtensionEventHandler( |
| 326 |
Widget w, |
| 327 |
int extensionEventType, |
| 328 |
XtEventHandler proc, |
| 329 |
XtPointer clientData) |
| 330 |
// |
| 331 |
//////////////////////////////////////////////////////////////////////// |
| 332 |
{ |
| 333 |
// loop through each item in handlerList, |
| 334 |
// and see if w,eventType,proc, and data match. |
| 335 |
SbBool found = FALSE; |
| 336 |
for (int i = 0; !found && (i < handlerList->getLength()); i++) { |
| 337 |
SoExtensionEventHandler *handler = |
| 338 |
(SoExtensionEventHandler *) (*handlerList)[i]; |
| 339 |
|
| 340 |
if ((handler->getWidget() == w) && |
| 341 |
(handler->getEventType() == extensionEventType) && |
| 342 |
(handler->getEventHandler() == proc) && |
| 343 |
(handler->getClientData() == clientData)) { |
| 344 |
found = TRUE; |
| 345 |
handlerList->remove(i); |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
//////////////////////////////////////////////////////////////////////// |
| 351 |
// |
| 352 |
// Description: |
| 353 |
// This locates the widget which will handle the event. |
| 354 |
// Input: event |
| 355 |
// Output: w, proc, clientData |
| 356 |
// |
| 357 |
// static protected |
| 358 |
// |
| 359 |
void |
| 360 |
SoXt::getExtensionEventHandler( |
| 361 |
XEvent *event, |
| 362 |
Widget &w, |
| 363 |
XtEventHandler &proc, |
| 364 |
XtPointer &clientData) |
| 365 |
// |
| 366 |
//////////////////////////////////////////////////////////////////////// |
| 367 |
{ |
| 368 |
// extension event. look up in our list of event handlers |
| 369 |
// for a match |
| 370 |
proc = NULL; |
| 371 |
for (int i = 0; (proc == NULL) && (i < handlerList->getLength()); i++) { |
| 372 |
SoExtensionEventHandler *handler = |
| 373 |
(SoExtensionEventHandler *) (*handlerList)[i]; |
| 374 |
|
| 375 |
w = XtWindowToWidget(event->xany.display, |
| 376 |
event->xany.window); |
| 377 |
|
| 378 |
if (handler->getWidget() == w) { |
| 379 |
if (handler->getEventType() == event->type) { |
| 380 |
// we found a match! |
| 381 |
proc = handler->getEventHandler(); |
| 382 |
clientData = handler->getClientData(); |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
//////////////////////////////////////////////////////////////////////// |
| 389 |
// |
| 390 |
// Description: |
| 391 |
// This dispatches events. |
| 392 |
// |
| 393 |
// static public |
| 394 |
// |
| 395 |
Boolean |
| 396 |
SoXt::dispatchEvent(XEvent *event) |
| 397 |
// |
| 398 |
//////////////////////////////////////////////////////////////////////// |
| 399 |
{ |
| 400 |
Boolean success = True; |
| 401 |
|
| 402 |
if(event->type >= LASTEvent) { |
| 403 |
XtEventHandler proc; |
| 404 |
XtPointer clientData; |
| 405 |
Widget w; |
| 406 |
Boolean dummy; |
| 407 |
|
| 408 |
// Get the event handling function which was |
| 409 |
// registered with Inventor for handling this |
| 410 |
// event type in this widget |
| 411 |
SoXt::getExtensionEventHandler(event, w, proc, clientData); |
| 412 |
|
| 413 |
// Call the event handler! |
| 414 |
if (proc == NULL) |
| 415 |
success = False; |
| 416 |
else |
| 417 |
(*proc) (w, clientData, event, &dummy); |
| 418 |
|
| 419 |
} |
| 420 |
|
| 421 |
// else it is not an extension event - let Xt dispatch it |
| 422 |
else success = XtDispatchEvent(event); |
| 423 |
|
| 424 |
return success; |
| 425 |
} |
| 426 |
|
| 427 |
//////////////////////////////////////////////////////////////////////// |
| 428 |
// |
| 429 |
// Description: |
| 430 |
// This retrieves and dispatches events (it loops forever!) |
| 431 |
// |
| 432 |
// static public |
| 433 |
// |
| 434 |
void |
| 435 |
SoXt::mainLoop() |
| 436 |
// |
| 437 |
//////////////////////////////////////////////////////////////////////// |
| 438 |
{ |
| 439 |
if (mainWidget == NULL) { |
| 440 |
#ifdef DEBUG |
| 441 |
SoDebugError::post("SoXt::mainLoop", |
| 442 |
"exiting -- SoXt::init never called"); |
| 443 |
#endif |
| 444 |
return; |
| 445 |
} |
| 446 |
|
| 447 |
// We cannot call XtAppMainLoop, because we need to handle |
| 448 |
// extension devices, like the spaceball. (Xt will ignore them). |
| 449 |
XtAppContext context = getAppContext(); |
| 450 |
XEvent event; |
| 451 |
|
| 452 |
for (;;) { |
| 453 |
SoXt::nextEvent(context, &event); |
| 454 |
SoXt::dispatchEvent(&event); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
//////////////////////////////////////////////////////////////////////// |
| 459 |
// |
| 460 |
// Description: |
| 461 |
// This returns the application context for the mainWidget. |
| 462 |
// |
| 463 |
// static public |
| 464 |
// |
| 465 |
XtAppContext |
| 466 |
SoXt::getAppContext() |
| 467 |
// |
| 468 |
//////////////////////////////////////////////////////////////////////// |
| 469 |
{ |
| 470 |
return mainWidget == NULL ? NULL : XtWidgetToApplicationContext(mainWidget); |
| 471 |
} |
| 472 |
|
| 473 |
//////////////////////////////////////////////////////////////////////// |
| 474 |
// |
| 475 |
// Description: |
| 476 |
// This returns the display for the mainWidget. |
| 477 |
// |
| 478 |
// static public |
| 479 |
// |
| 480 |
Display * |
| 481 |
SoXt::getDisplay() |
| 482 |
// |
| 483 |
//////////////////////////////////////////////////////////////////////// |
| 484 |
{ |
| 485 |
return mainWidget == NULL ? NULL : XtDisplay(mainWidget); |
| 486 |
} |
| 487 |
|
| 488 |
//////////////////////////////////////////////////////////////////////// |
| 489 |
// |
| 490 |
// Description: |
| 491 |
// This returns the display for the mainWidget. |
| 492 |
// |
| 493 |
// static public |
| 494 |
// |
| 495 |
Widget |
| 496 |
SoXt::getTopLevelWidget() |
| 497 |
// |
| 498 |
//////////////////////////////////////////////////////////////////////// |
| 499 |
{ |
| 500 |
return mainWidget; |
| 501 |
} |
| 502 |
|
| 503 |
//////////////////////////////////////////////////////////////////////// |
| 504 |
// |
| 505 |
// Description: |
| 506 |
// Convert the char* to an XmString. |
| 507 |
// |
| 508 |
// static public |
| 509 |
// |
| 510 |
XmString |
| 511 |
SoXt::encodeString(char *s) |
| 512 |
// |
| 513 |
//////////////////////////////////////////////////////////////////////// |
| 514 |
{ |
| 515 |
XmString xs = NULL; |
| 516 |
|
| 517 |
if (s != NULL) |
| 518 |
xs = XmStringCreate(s, XmSTRING_DEFAULT_CHARSET); |
| 519 |
|
| 520 |
return xs; |
| 521 |
} |
| 522 |
|
| 523 |
//////////////////////////////////////////////////////////////////////// |
| 524 |
// |
| 525 |
// Description: |
| 526 |
// Convert the XmString to a char*. |
| 527 |
// |
| 528 |
// static public |
| 529 |
// |
| 530 |
char * |
| 531 |
SoXt::decodeString(XmString xs) |
| 532 |
// |
| 533 |
//////////////////////////////////////////////////////////////////////// |
| 534 |
{ |
| 535 |
XmStringContext ctx; |
| 536 |
char *text = NULL; |
| 537 |
XmStringCharSet charset; |
| 538 |
XmStringDirection dirRtoL; |
| 539 |
Boolean separator; |
| 540 |
Boolean initOK = TRUE; |
| 541 |
Boolean getOK = TRUE; |
| 542 |
|
| 543 |
initOK = XmStringInitContext(&ctx, xs); |
| 544 |
|
| 545 |
if (initOK) |
| 546 |
getOK = XmStringGetNextSegment(ctx, &text, |
| 547 |
&charset, &dirRtoL, &separator); |
| 548 |
|
| 549 |
#ifdef DEBUG |
| 550 |
if (!initOK) |
| 551 |
SoDebugError::post("SoXt::decodeString", |
| 552 |
"XmStringInitContext failed."); |
| 553 |
if (!getOK) |
| 554 |
SoDebugError::post("SoXt::decodeString", |
| 555 |
"XmStringGetNextSegment failed."); |
| 556 |
#endif |
| 557 |
|
| 558 |
return text; |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
//---------------------------------------------------------------------- |
| 563 |
// |
| 564 |
// SoXtEventHandler - this class ties in sensors to Xt events |
| 565 |
// |
| 566 |
//---------------------------------------------------------------------- |
| 567 |
|
| 568 |
SoXtEventHandler::SoXtEventHandler(XtAppContext context) |
| 569 |
{ |
| 570 |
appContext = context; |
| 571 |
timerOn = FALSE; |
| 572 |
workProcOn = FALSE; |
| 573 |
|
| 574 |
SoDB::getSensorManager()->setChangedCallback |
| 575 |
(SoXtEventHandler::sensorManagerChangedCallback, |
| 576 |
this); |
| 577 |
// Call this once because things might already be in the timer queue. |
| 578 |
// If we don't call this and no new things happen, then the callbacks |
| 579 |
// will never be set up. |
| 580 |
setUpCallbacks(); |
| 581 |
} |
| 582 |
|
| 583 |
void |
| 584 |
SoXtEventHandler::setUpCallbacks() |
| 585 |
{ |
| 586 |
SbTime nextEvent; |
| 587 |
|
| 588 |
// If we have a timer at some point, schedule that |
| 589 |
if (SoDB::getSensorManager()->isTimerSensorPending(nextEvent)) { |
| 590 |
// Only change the timer if its deadline is different from the |
| 591 |
// current one. |
| 592 |
if (!timerOn || nextEvent != currentDeadline) { |
| 593 |
long msec; // System long |
| 594 |
|
| 595 |
if (timerOn) { // get rid of existing timer if present |
| 596 |
XtRemoveTimeOut(xtTimer); |
| 597 |
timerOn = FALSE; |
| 598 |
} |
| 599 |
|
| 600 |
currentDeadline = nextEvent; |
| 601 |
msec = (nextEvent - SbTime::getTimeOfDay()).getMsecValue(); |
| 602 |
|
| 603 |
xtTimer = XtAppAddTimeOut(appContext, msec > 0 ? msec : 0, |
| 604 |
(XtTimerCallbackProc) |
| 605 |
SoXtEventHandler::xtTimerCallback, |
| 606 |
(XtPointer) this); |
| 607 |
timerOn = TRUE; |
| 608 |
#ifdef DEBUG |
| 609 |
if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) { |
| 610 |
SoDebug::RTPrintf("XtAppAddTimeOut %d msec\n", msec); |
| 611 |
} |
| 612 |
#endif |
| 613 |
} |
| 614 |
} |
| 615 |
else if (timerOn) { // get rid of existing timer |
| 616 |
XtRemoveTimeOut(xtTimer); |
| 617 |
timerOn = FALSE; |
| 618 |
#ifdef DEBUG |
| 619 |
if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) { |
| 620 |
SoDebug::RTPrintf("XtRemoveTimeOut\n"); |
| 621 |
} |
| 622 |
#endif |
| 623 |
} |
| 624 |
|
| 625 |
// If we have an idle task, schedule a workproc for it |
| 626 |
if (SoDB::getSensorManager()->isDelaySensorPending()) { |
| 627 |
// only schedule it if one is not already active |
| 628 |
if (!workProcOn) { |
| 629 |
xtWorkProc = |
| 630 |
XtAppAddWorkProc(appContext, |
| 631 |
(XtWorkProc) |
| 632 |
SoXtEventHandler::xtWorkProcCallback, |
| 633 |
(XtPointer) this); |
| 634 |
workProcOn = TRUE; |
| 635 |
#ifdef DEBUG |
| 636 |
if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) { |
| 637 |
SoDebug::RTPrintf("XtAppAddWorkProc\n"); |
| 638 |
} |
| 639 |
#endif |
| 640 |
} |
| 641 |
} |
| 642 |
else if (workProcOn) { // remove obsolete workproc |
| 643 |
XtRemoveWorkProc(xtWorkProc); |
| 644 |
workProcOn = FALSE; |
| 645 |
#ifdef DEBUG |
| 646 |
if (SoDebug::GetEnv("IV_DEBUG_SENSORS")) { |
| 647 |
SoDebug::RTPrintf("XtRemoveWorkProc\n"); |
| 648 |
} |
| 649 |
#endif |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
void |
| 654 |
SoXtEventHandler::xtTimerCallback(XtPointer closure, XtIntervalId *) |
| 655 |
{ |
| 656 |
SoXtEventHandler *eh = (SoXtEventHandler *) closure; |
| 657 |
|
| 658 |
// process the timer queue |
| 659 |
SoDB::getSensorManager()->processTimerQueue(); |
| 660 |
|
| 661 |
// set up for the next time around |
| 662 |
// hide the fact that this timer exists so that setUpEvents |
| 663 |
// won't try to delete it (Xt will do that automatically) |
| 664 |
eh->timerOn = FALSE; |
| 665 |
eh->setUpCallbacks(); |
| 666 |
} |
| 667 |
|
| 668 |
Boolean |
| 669 |
SoXtEventHandler::xtWorkProcCallback(XtPointer closure) |
| 670 |
{ |
| 671 |
SoXtEventHandler *eh = (SoXtEventHandler *) closure; |
| 672 |
|
| 673 |
// process the idle queue |
| 674 |
SoDB::getSensorManager()->processDelayQueue(TRUE); |
| 675 |
|
| 676 |
// set up for the next time around |
| 677 |
// hide the fact that this workproc exists so that setUpEvents |
| 678 |
// won't try to delete it (Xt will do that automatically) |
| 679 |
eh->workProcOn = FALSE; |
| 680 |
eh->setUpCallbacks(); |
| 681 |
|
| 682 |
// Always return TRUE, meaning delete the workproc now. The |
| 683 |
// workproc will be installed again if it is necessary. This |
| 684 |
// should be pretty cheap, as Xt keeps a pool of these. |
| 685 |
return TRUE; |
| 686 |
} |
| 687 |
|
| 688 |
// Callback for changes to the sensor manager |
| 689 |
void |
| 690 |
SoXtEventHandler::sensorManagerChangedCallback(void *data) |
| 691 |
{ |
| 692 |
SoXtEventHandler *eh = (SoXtEventHandler *) data; |
| 693 |
|
| 694 |
// update the Xt timeout and workproc if necessary |
| 695 |
eh->setUpCallbacks(); |
| 696 |
} |
| 697 |
|
| 698 |
SoExtensionEventHandler::SoExtensionEventHandler( |
| 699 |
Widget w, |
| 700 |
int et, |
| 701 |
XtEventHandler p, |
| 702 |
XtPointer d) |
| 703 |
{ |
| 704 |
widget = w; |
| 705 |
eventType = et; |
| 706 |
proc = p; |
| 707 |
data = d; |
| 708 |
} |
| 709 |
|
| 710 |
//////////////////////////////////////////////////////////////////////// |
| 711 |
// |
| 712 |
// Resize the given widget. It calls XtSetArg/Values on the widget. |
| 713 |
// |
| 714 |
// Use: static public |
| 715 |
void |
| 716 |
SoXt::setWidgetSize(Widget w, const SbVec2s &size) |
| 717 |
// |
| 718 |
//////////////////////////////////////////////////////////////////////// |
| 719 |
{ |
| 720 |
if (w == NULL) |
| 721 |
return; |
| 722 |
|
| 723 |
Arg args[2]; |
| 724 |
XtSetArg(args[0], XtNwidth, size[0]); |
| 725 |
XtSetArg(args[1], XtNheight, size[1]); |
| 726 |
XtSetValues(w, args, 2); |
| 727 |
} |
| 728 |
|
| 729 |
//////////////////////////////////////////////////////////////////////// |
| 730 |
// |
| 731 |
// Returns the given widget size. |
| 732 |
// |
| 733 |
// Use: static public |
| 734 |
SbVec2s |
| 735 |
SoXt::getWidgetSize(Widget w) |
| 736 |
// |
| 737 |
//////////////////////////////////////////////////////////////////////// |
| 738 |
{ |
| 739 |
if (w == NULL) |
| 740 |
return SbVec2s(0, 0); |
| 741 |
|
| 742 |
Arg args[2]; |
| 743 |
short _w, _h; // HAS to be shorts for XtGetValues to work !! |
| 744 |
XtSetArg(args[0], XtNheight, &_h); |
| 745 |
XtSetArg(args[1], XtNwidth, &_w); |
| 746 |
XtGetValues(w, args, 2); |
| 747 |
return SbVec2s(_w, _h); |
| 748 |
} |
| 749 |
|
| 750 |
//////////////////////////////////////////////////////////////////////// |
| 751 |
// |
| 752 |
// Returns the shell widget which contains this widget |
| 753 |
// |
| 754 |
// Use: static public |
| 755 |
Widget |
| 756 |
SoXt::getShellWidget(Widget w) |
| 757 |
// |
| 758 |
//////////////////////////////////////////////////////////////////////// |
| 759 |
{ |
| 760 |
for (; w != NULL; w = XtParent(w)) |
| 761 |
if (XtIsShell(w)) |
| 762 |
return w; |
| 763 |
|
| 764 |
return NULL; |
| 765 |
} |
| 766 |
|
| 767 |
static void destroyDialogCB(Widget dialog, void *, void *) |
| 768 |
{ XtDestroyWidget(dialog); } |
| 769 |
|
| 770 |
//////////////////////////////////////////////////////////////////////// |
| 771 |
// |
| 772 |
// creates a simple error dialog box with the given error |
| 773 |
// |
| 774 |
// Use: static public |
| 775 |
void |
| 776 |
SoXt::createSimpleErrorDialog(Widget widget, |
| 777 |
char *dialogTitle, char *str1, char *str2) |
| 778 |
// |
| 779 |
//////////////////////////////////////////////////////////////////////// |
| 780 |
{ |
| 781 |
// ??? Make the motif dialog on the shell of the 'widget' variable |
| 782 |
// ??? instead of the 'widget' itself otherwise the color editor |
| 783 |
// ??? has problem building once the dialog has been opened. |
| 784 |
// ??? I can't figure that one out. Weird... (alain) |
| 785 |
Widget shell = SoXt::getShellWidget(widget); |
| 786 |
if (shell == NULL) |
| 787 |
return; |
| 788 |
|
| 789 |
Arg args[5]; |
| 790 |
XmString xmstr = XmStringCreateSimple(str1); |
| 791 |
if (str2 != NULL) { |
| 792 |
xmstr = XmStringConcat(xmstr, XmStringSeparatorCreate()); |
| 793 |
xmstr = XmStringConcat(xmstr, XmStringSeparatorCreate()); |
| 794 |
xmstr = XmStringConcat(xmstr, XmStringCreateSimple(str2)); |
| 795 |
} |
| 796 |
|
| 797 |
int n = 0; |
| 798 |
XtSetArg(args[n], XmNautoUnmanage, FALSE); n++; |
| 799 |
XtSetArg(args[n], XtNtitle, dialogTitle); n++; |
| 800 |
XtSetArg(args[n], XmNmessageString, xmstr); n++; |
| 801 |
XtSetArg(args[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++; |
| 802 |
Widget dialog = XmCreateErrorDialog(shell, "Error Dialog", args, n); |
| 803 |
XmStringFree(xmstr); |
| 804 |
|
| 805 |
XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON)); |
| 806 |
XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON)); |
| 807 |
|
| 808 |
// register callback to destroy (and not just unmap) the dialog |
| 809 |
XtAddCallback(dialog, XmNokCallback, |
| 810 |
(XtCallbackProc) destroyDialogCB, (XtPointer)NULL); |
| 811 |
|
| 812 |
XtManageChild(dialog); |
| 813 |
} |
| 814 |
|
| 815 |
//////////////////////////////////////////////////////////////////////// |
| 816 |
// |
| 817 |
// Convenience routine for creating menus in the popup planes. |
| 818 |
// |
| 819 |
// Use: static public |
| 820 |
void |
| 821 |
SoXt::getPopupArgs( |
| 822 |
Display *d, |
| 823 |
int scr, |
| 824 |
ArgList args, |
| 825 |
int *n) |
| 826 |
// |
| 827 |
//////////////////////////////////////////////////////////////////////// |
| 828 |
{ |
| 829 |
iv_SG_getPopupArgs(d, scr, args, n); |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
//////////////////////////////////////////////////////////////////////// |
| 834 |
// |
| 835 |
// register map/unmap callbacks to load/unload the given color maps |
| 836 |
// when the pulldown menu (or popup menu) gets mapped. |
| 837 |
// |
| 838 |
// Use: static public |
| 839 |
void |
| 840 |
SoXt::registerColormapLoad(Widget w, Widget shell) |
| 841 |
// |
| 842 |
//////////////////////////////////////////////////////////////////////// |
| 843 |
{ |
| 844 |
if (! w || ! shell || ! XtIsShell(shell)) |
| 845 |
return; |
| 846 |
|
| 847 |
XtAddCallback(w, XmNmapCallback, (XtCallbackProc) overlayMenuMappedCB, shell); |
| 848 |
XtAddCallback(w, XmNunmapCallback, (XtCallbackProc) overlayMenuUnmappedCB, shell); |
| 849 |
} |
| 850 |
|
| 851 |
// |
| 852 |
// Called when an overlay menu (pulldown or popup) is about to be |
| 853 |
// mapped - this will load the correct color map on the window. |
| 854 |
// |
| 855 |
static void |
| 856 |
overlayMenuMappedCB(Widget w, Widget shell, XtPointer) |
| 857 |
{ |
| 858 |
//printf("MAP overlayMenuMappedCB called\n"); |
| 859 |
SoXt::addColormapToShell(w, shell); |
| 860 |
} |
| 861 |
|
| 862 |
// |
| 863 |
// Called when an overlay menu (pulldown or popup) is no longer |
| 864 |
// mapped - this will unload the color map to make sure that the OGL |
| 865 |
// overlay (really popup planes on 8 bit machine) colormap gets used. |
| 866 |
// |
| 867 |
static void |
| 868 |
overlayMenuUnmappedCB(Widget w, Widget shell, XtPointer) |
| 869 |
{ |
| 870 |
//printf("UNMAP overlayMenuUnmappedCB called\n"); |
| 871 |
SoXt::removeColormapFromShell(w, shell); |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
//////////////////////////////////////////////////////////////////////// |
| 876 |
// |
| 877 |
// Convenience routine to load the given colormap |
| 878 |
// |
| 879 |
// Use: static public |
| 880 |
void |
| 881 |
SoXt::addColormapToShell(Widget w, Widget shell) |
| 882 |
// |
| 883 |
//////////////////////////////////////////////////////////////////////// |
| 884 |
{ |
| 885 |
if (! w || ! shell || ! XtIsShell(shell)) |
| 886 |
return; |
| 887 |
|
| 888 |
// load the color map RIGH AWAY to reduce pulldown menu flickers |
| 889 |
Colormap map; |
| 890 |
XtVaGetValues(w, XmNcolormap, &map, NULL); |
| 891 |
XInstallColormap(XtDisplay(w), map); |
| 892 |
|
| 893 |
// check to see if there is already a property |
| 894 |
Window *windowsReturn; |
| 895 |
int countReturn; |
| 896 |
Status status = XGetWMColormapWindows(XtDisplay(shell), |
| 897 |
XtWindow(shell), &windowsReturn, &countReturn); |
| 898 |
|
| 899 |
// if no property, just create one |
| 900 |
if (!status) |
| 901 |
{ |
| 902 |
Window windows[2]; |
| 903 |
windows[0] = XtWindow(w); |
| 904 |
windows[1] = XtWindow(shell); |
| 905 |
XSetWMColormapWindows(XtDisplay(shell), XtWindow(shell), |
| 906 |
windows, 2); |
| 907 |
} |
| 908 |
// else there was a property, add myself to the beginning |
| 909 |
else { |
| 910 |
Window *windows = (Window *)XtMalloc((sizeof(Window))* |
| 911 |
(countReturn+1)); |
| 912 |
windows[0] = XtWindow(w); |
| 913 |
for (int i=0; i<countReturn; i++) |
| 914 |
windows[i+1] = windowsReturn[i]; |
| 915 |
XSetWMColormapWindows(XtDisplay(shell), XtWindow(shell), |
| 916 |
windows, countReturn+1); |
| 917 |
XtFree((char *)windows); |
| 918 |
XFree((char *)windowsReturn); |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
//////////////////////////////////////////////////////////////////////// |
| 923 |
// |
| 924 |
// Convenience routine to remove the given colormap |
| 925 |
// |
| 926 |
void |
| 927 |
SoXt::removeColormapFromShell(Widget w, Widget shell) |
| 928 |
// |
| 929 |
//////////////////////////////////////////////////////////////////////// |
| 930 |
{ |
| 931 |
if (! w || ! shell || ! XtIsShell(shell)) |
| 932 |
return; |
| 933 |
|
| 934 |
// check to see if there is a property |
| 935 |
Window *windowsReturn; |
| 936 |
int countReturn; |
| 937 |
Status status = XGetWMColormapWindows(XtDisplay(shell), |
| 938 |
XtWindow(shell), &windowsReturn, &countReturn); |
| 939 |
|
| 940 |
// if no property, just return. If there was a property, continue |
| 941 |
if (status) |
| 942 |
{ |
| 943 |
// search for a match |
| 944 |
for (int i=0; i<countReturn; i++) |
| 945 |
{ |
| 946 |
if (windowsReturn[i] == XtWindow(w)) |
| 947 |
{ |
| 948 |
// we found a match, now copu the rest down |
| 949 |
for (i++; i<countReturn; i++) |
| 950 |
windowsReturn[i-1] = windowsReturn[i]; |
| 951 |
|
| 952 |
XSetWMColormapWindows(XtDisplay(w), XtWindow(shell), |
| 953 |
windowsReturn, countReturn-1); |
| 954 |
break; // from outer for |
| 955 |
} |
| 956 |
} |
| 957 |
XFree((char *)windowsReturn); |
| 958 |
} |
| 959 |
} |
| 960 |
|