Debian Codegraph @

apps/samples/widgets/MyTextureEd.c++

raw
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) 1991-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.6 $
44 |
45 | Classes:
46 | MyTextureEditor
47 |
48 | Author(s): Alain Dumesny
49 |
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 <cstdio>
56 #include <cstring>
57 #include <sys/param.h>
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <unistd.h>
61 #include <dirent.h>
62 #include <cstdlib>
63
64 #include <X11/StringDefs.h>
65 #include <Xm/Xm.h>
66 #include <Xm/Form.h>
67 #include <Xm/RowColumn.h>
68 #include <Xm/CascadeBG.h>
69 #include <Xm/PushBG.h>
70 #include <Xm/LabelG.h>
71 #include <Xm/Text.h>
72 #include <Xm/Scale.h>
73 #include <Xm/SelectioB.h>
74 #include <Xm/MessageB.h>
75 #include <Xm/FileSB.h>
76 #ifdef INTERNALGLW
77 #include "GLwMDrawA.h"
78 #else
79 #include <GL/GLwMDrawA.h>
80 #endif
81
82 #include <Inventor/nodes/SoSeparator.h>
83 #include <Inventor/nodes/SoCube.h>
84 #include <Inventor/nodes/SoText2.h>
85 #include <Inventor/nodes/SoTexture2.h>
86 #include <Inventor/nodes/SoTexture2Transform.h>
87 #include <Inventor/nodes/SoTextureCoordinateDefault.h>
88 #include <Inventor/nodes/SoTextureCoordinateEnvironment.h>
89 #include <Inventor/nodes/SoTextureCoordinatePlane.h>
90 #include <Inventor/Xt/SoXt.h>
91 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
92 #include <Inventor/errors/SoDebugError.h>
93 #include <GL/gl.h>
94
95 #include "MyColorWheel.h"
96 #include "MyColorSlider.h"
97 #include "MyThumbWheel.h"
98 #include "MyTextureEditor.h"
99
100
101 // stuff to read images (similar to gl/image.h)
102 // Although the IMAGE stuff is in <gl/image.h> the prototypes are
103 // incomplete and wrong... So, we include this stuff here.
104 // Also, the types of some fields change from 5.3 to 6.2, so we use
105 // something to guarantee the size is 32 bits.
106
107 extern "C" {
108 #define CM_NORMAL 0 /* file contains rows of values which
109 * are either RGB values (zsize == 3)
110 * or greyramp values (zsize == 1) */
111 typedef struct {
112 unsigned short imagic; /* stuff saved on disk . . */
113 unsigned short type;
114 unsigned short dim;
115 unsigned short xsize;
116 unsigned short ysize;
117 unsigned short zsize;
118 uint32_t min;
119 uint32_t max;
120 uint32_t wastebytes;
121 char name[80];
122 uint32_t colormap;
123 } IMAGE;
124 extern IMAGE *iopen(const char *, const char *);
125 extern void getrow(IMAGE *, short *, int, int);
126 extern void iclose(IMAGE *);
127 extern void i_seterror(void (*func)(char *));
128 };
129
130
131 /*
132 * Defines
133 */
134
135
136 enum WidgetsID {
137 PALETTE_BUTTON = 0, // must start at 0 since we use array
138 PALETTE_MENU,
139 MENU_BAR,
140
141 // file menu
142 FILE_NEW,
143 FILE_RESET,
144 FILE_DELETE,
145
146 // text field widgets
147 SCALE_X_FIELD,
148 SCALE_Y_FIELD,
149 TRANS_X_FIELD,
150 TRANS_Y_FIELD,
151 ROT_FIELD,
152
153 // sliders widgets
154 TRANS_X_SLD,
155 TRANS_Y_SLD,
156 ROT_SLD,
157 SCALE_X_LABEL,
158 SCALE_Y_LABEL,
159
160 ACCEPT,
161 TEXTURE_GLX,
162 TEXTURE_NAME,
163
164 // mapping option menu types
165 MAPP_PULLDOWN,
166 MAP_DEFAULT,
167 MAP_ENV,
168 MAP_PLANE_XY,
169 MAP_PLANE_XZ,
170 MAP_PLANE_YZ,
171 MAP_UNKNOWN,
172
173 // other option menu types
174 OPT_PULLDOWN,
175 OPT_REPEAT,
176 OPT_CLAMP,
177 OPT_UNKNOWN,
178
179 // image dialog widgets
180 DIALOG_WINDOW,
181 DIALOG_IMAGE,
182 DIALOG_NAME,
183 DIALOG_INFO,
184 DIALOG_BUTTON_0,
185 DIALOG_FILE_BROWSER,
186
187 NUM, // this must be the last entry
188 };
189
190 // time in msec between consecutive clicks to consider
191 // the second click being a double click.
192 #define DOUBLE_CLICK_TIME 300
193
194 enum TileDrawStyle {
195 DRAW_NORM,
196 DRAW_CURRENT,
197 DRAW_SELECTED,
198 };
199
200 #define IMAGE_SIZE 38
201 #define IMAGE_NUM 5
202 #define IMAGE_TOTAL (IMAGE_NUM*IMAGE_NUM)
203 #define IMAGE_SPACE 2
204 #define GLX_SIZE (IMAGE_NUM * IMAGE_SIZE + (IMAGE_NUM + 1) * IMAGE_SPACE)
205
206 // ??? doing a GL_LINE_LOOP seems to be missing the top right
207 // ??? pixel due to subpixel == TRUE in openGL.
208 #define RECT(x1, y1, x2, y2) \
209 glBegin(GL_LINE_STRIP); \
210 glVertex2s(x2, y2); glVertex2s(x1, y2); \
211 glVertex2s(x1, y1); glVertex2s(x2, y1); \
212 glVertex2s(x2, y2+1); \
213 glEnd();
214
215 struct TextureNameStruct {
216 char *name;
217 char *fullName;
218 int zsize;
219 char *iconImage;
220 };
221
222 struct PaletteStruct {
223 char *name;
224 SbBool user;
225 SbBool system;
226 };
227
228
229 /*
230 * static vars
231 */
232
233
234 static char *customTextureDir = ".textures";
235 static char *defaultDir = IVTEXTURESDIR;
236 static char *editorTitle = "Texture Editor";
237 static char *noFileNameStr = "<empty>";
238
239 #define hourglass_width 32
240 #define hourglass_height 32
241 #define hourglass_x_hot 16
242 #define hourglass_y_hot 16
243 static char hourglass_bits[] = {
244 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
245 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
246 0x00, 0xfe, 0xff, 0x00, 0x00, 0x04, 0x40, 0x00, 0x00, 0x04, 0x40, 0x00,
247 0x00, 0x04, 0x40, 0x00, 0x00, 0xe8, 0x2e, 0x00, 0x00, 0xd0, 0x17, 0x00,
248 0x00, 0xa0, 0x0b, 0x00, 0x00, 0x40, 0x05, 0x00, 0x00, 0x40, 0x05, 0x00,
249 0x00, 0x40, 0x04, 0x00, 0x00, 0x40, 0x04, 0x00, 0x00, 0x40, 0x04, 0x00,
250 0x00, 0x20, 0x09, 0x00, 0x00, 0x10, 0x11, 0x00, 0x00, 0x88, 0x23, 0x00,
251 0x00, 0xc4, 0x47, 0x00, 0x00, 0xe4, 0x4f, 0x00, 0x00, 0xf4, 0x5f, 0x00,
252 0x00, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
253 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
254 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
255
256
257 //
258 // returns true if the passed file is a subdirectory in the current directory.
259 // (call chdir() before calling this).
260 //
261 static SbBool
262 isDirectory(char *file)
263 {
264 struct stat buf;
265
266 if (stat(file, &buf) == 0)
267 if ((buf.st_mode & S_IFMT) == S_IFDIR)
268 return TRUE;
269
270 return FALSE;
271 }
272
273 //
274 // return PaletteStruct pointer which contains the given name string
275 // in the given PbPlist of structs.
276 //
277 static PaletteStruct *
278 findPalette(char *str, SbPList *list)
279 {
280 PaletteStruct *pal = NULL;
281
282 for (int i=0; i<list->getLength(); i++) {
283 pal = (PaletteStruct *) (*list)[i];
284 if (strcmp(str, pal->name) == 0)
285 return pal;
286 else
287 pal = NULL;
288 }
289 return pal;
290 }
291
292 static void imageErrorHandler(char *)
293 { }
294
295
296 ////////////////////////////////////////////////////////////////////////
297 //
298 // constructor
299 //
300 MyTextureEditor::MyTextureEditor(
301 Widget parent,
302 const char *name,
303 SbBool buildInsideParent,
304 const char *dir)
305 //
306 ////////////////////////////////////////////////////////////////////////
307 : SoXtComponent(parent, name, buildInsideParent)
308 {
309 int i;
310
311 setClassName("MyTextureEditor");
312 paletteDir = (dir != NULL) ? strdup(dir) : strdup(defaultDir);
313 ignoreCallback = FALSE;
314 selectedItem = currentItem = -1;
315 curPalette = -1;
316 loadedPalette = FALSE;
317
318 // widget vars
319 widgetList = new Widget[NUM];
320 for (i=0; i<NUM; i++)
321 widgetList[i] = NULL;
322 oldXThumbVal = oldYThumbVal = 0;
323 fieldChanged = FALSE;
324 imageDialogCtx = 0;
325
326 // image dialog vars
327 dialogImage = NULL;
328 dialogImageName = NULL;
329 dialogImageInfo = NULL;
330
331 // scene graph vars
332 sceneRoot = new SoSeparator;
333 texXfNode = new SoTexture2Transform;
334 texFuncNode = new SoTextureCoordinateDefault;
335 texNode = new SoTexture2;
336 sceneRoot->ref();
337 sceneRoot->addChild(texFuncNode);
338 sceneRoot->addChild(texXfNode);
339 sceneRoot->addChild(texNode);
340 texXfNode->center.setValue(SbVec2f(.5, .5));
341 //???alain
342 // if (getgdesc(GD_TEXTURE))
343 if (1)
344 userGeometry = new SoCube;
345 else {
346 SoText2 *text = new SoText2;
347 text->string.set1Value(0, SbString("Texture not visible"));
348 text->string.set1Value(1, SbString("on this machine"));
349 text->spacing = 2;
350 text->justification = SoText2::CENTER;
351 userGeometry = text;
352 }
353 sceneRoot->addChild(userGeometry);
354
355 // allocate the texture name list
356 textureNames = new TextureNameStruct[IMAGE_TOTAL];
357 for (i=0; i<IMAGE_TOTAL; i++) {
358 textureNames[i].name = textureNames[i].fullName = NULL;
359 textureNames[i].iconImage = new char[IMAGE_SIZE*IMAGE_SIZE*3]; // for RGB
360 }
361
362 // set the image lib error handler to prevent iopen() to exit()
363 // when given a non image fileName.
364 i_seterror(imageErrorHandler);
365
366 // Build the widget tree, and let SoXtComponent know about our base widget.
367 getPaletteNames();
368 setBaseWidget(buildWidget(getParentWidget()));
369 }
370
371 ////////////////////////////////////////////////////////////////////////
372 //
373 // Destructor.
374 //
375 MyTextureEditor::~MyTextureEditor()
376 //
377 ////////////////////////////////////////////////////////////////////////
378 {
379 int i;
380
381 // delete components
382 delete colWheel;
383 delete colSlider;
384 delete viewer;
385 delete scaleXThumb;
386 delete scaleYThumb;
387
388 // delete stuff
389 delete [] widgetList;
390 free(paletteDir);
391
392 // delete palette names
393 PaletteStruct *pal;
394 for (i=0; i<paletteList.getLength(); i++) {
395 pal = (PaletteStruct *) paletteList[i];
396 free(pal->name);
397 delete pal;
398 }
399 paletteList.truncate(0);
400
401 // delete texture names
402 for (i=0; i<IMAGE_TOTAL; i++) {
403 deleteTextureEntry(i);
404 delete [] textureNames[i].iconImage;
405 }
406 delete [] textureNames;
407 }
408
409 ////////////////////////////////////////////////////////////////////////
410 //
411 // Description:
412 // sets the geometry to use
413 //
414 // Use: public
415 void
416 MyTextureEditor::setObjectGeometry(SoNode *newGeom)
417 //
418 ////////////////////////////////////////////////////////////////////////
419 {
420 //???alain
421 // if (! getgdesc(GD_TEXTURE))
422 // return;
423
424 // remove the old geometry, and replace it with supplied one
425 sceneRoot->replaceChild(userGeometry, newGeom);
426 userGeometry = newGeom;
427 viewer->viewAll();
428 }
429
430 ////////////////////////////////////////////////////////////////////////
431 //
432 // Description:
433 // sets the SoTexture2 node to use.
434 //
435 // Use: public
436 void
437 MyTextureEditor::setTextureNode(const SoTexture2 *node)
438 //
439 ////////////////////////////////////////////////////////////////////////
440 {
441 // copies values over
442 texNode->filename = node->filename.getValue();
443 texNode->wrapS = node->wrapS.getValue();
444 texNode->wrapT = node->wrapT.getValue();
445 texNode->model = node->model.getValue();
446 texNode->blendColor = node->blendColor.getValue();
447
448 // update the UI and unselect current texture
449 updateTexture2UI();
450 deselectCurrentItem();
451 }
452
453 ////////////////////////////////////////////////////////////////////////
454 //
455 // Description:
456 // sets the SoTexture2 Transform node to use.
457 //
458 // Use: public
459 void
460 MyTextureEditor::setTransformNode(const SoTexture2Transform *node)
461 //
462 ////////////////////////////////////////////////////////////////////////
463 {
464 // copies values over
465 texXfNode->translation = node->translation.getValue();
466 texXfNode->rotation = node->rotation.getValue();
467 texXfNode->scaleFactor = node->scaleFactor.getValue();
468 texXfNode->center = node->center.getValue();
469
470 //
471 // constrain the transform node to our paradigm
472 //
473
474 // change the center of rotation/scale to make it look like
475 // we are rotation around the center of the image
476 texXfNode->center.setValue(
477 SbVec2f(.5, .5) - texXfNode->translation.getValue());
478
479 // normalize rotation to [0,2PI]
480 float val = texXfNode->rotation.getValue();
481 while (val < 0) val += 2*M_PI;
482 while (val > 2*M_PI) val -= 2*M_PI;
483 texXfNode->rotation = val;
484
485 // update the UI
486 updateTextureXfUI();
487 }
488
489 ////////////////////////////////////////////////////////////////////////
490 //
491 // Description:
492 // sets the SoTextureCoordinateFunction node to use.
493 //
494 // Use: public
495 void
496 MyTextureEditor::setFunctionNode(const SoTextureCoordinateFunction *node)
497 //
498 ////////////////////////////////////////////////////////////////////////
499 {
500 // make our local func node reflect the given node
501 SoNode *newFunc = (node != NULL) ? node->copy() : new SoTextureCoordinateDefault;
502 sceneRoot->replaceChild(texFuncNode, newFunc);
503 texFuncNode = (SoTextureCoordinateFunction *) newFunc;
504
505 // finally update the UI
506 updateTextureFuncUI();
507 }
508
509 ////////////////////////////////////////////////////////////////////////
510 //
511 // Description:
512 // Builds the editor layout
513 //
514 // Use: protected
515 Widget
516 MyTextureEditor::buildWidget(Widget parent)
517 //
518 ////////////////////////////////////////////////////////////////////////
519 {
520 int n;
521 Arg args[12];
522
523 // create a top level form to hold everything together
524 Widget form = XmCreateForm(parent, "textureForm", NULL, 0);
525
526 //
527 // create all the parts
528 //
529
530 // create all the comps
531 Widget menu = buildMenu(form);
532
533 colWheel = new MyColorWheel(form);
534 colWheel->setSize(SbVec2s(110, 110));
535 colWheel->setWYSIWYG(TRUE);
536 colWheel->addValueChangedCallback(
537 MyTextureEditor::colWheelCB, this);
538 Widget wheelW = colWheel->getWidget();
539
540 colSlider = new MyColorSlider(form, NULL, TRUE, MyColorSlider::VALUE_SLIDER);
541 colSlider->setNumericFieldVisible(FALSE);
542 colSlider->setSize(SbVec2s(1, 23));
543 colSlider->addValueChangedCallback(
544 MyTextureEditor::colSliderCB, this);
545 colSlider->setWYSIWYG(TRUE);
546 Widget sliderW = colSlider->getWidget();
547
548 viewer = new SoXtExaminerViewer(form, NULL, TRUE, SoXtFullViewer::BUILD_NONE);
549 viewer->setSize(SbVec2s(150, 150));
550 viewer->setSceneGraph(sceneRoot);
551 viewer->setDrawStyle(SoXtViewer::STILL,SoXtViewer::VIEW_AS_IS);
552 viewer->setDrawStyle(SoXtViewer::INTERACTIVE,SoXtViewer::VIEW_AS_IS);
553 sceneRoot->unref();
554 Widget viewerW = viewer->getWidget();
555
556 Widget glx = buildTexturePaletteWidget(form);
557 Widget sldForm = buildSliders(form);
558 Widget butForm = buildButtons(form);
559
560 n = 0;
561 XtSetArg(args[n], XmNalignment, XmALIGNMENT_CENTER); n++;
562 Widget textName = XmCreateLabelGadget(form, "name", args, n);
563 widgetList[TEXTURE_NAME] = textName;
564
565 //
566 // layout !
567 //
568 n = 0;
569 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
570 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
571 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
572 XtSetValues(menu, args, n);
573
574 n = 0;
575 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
576 XtSetArg(args[n], XmNtopWidget, menu); n++;
577 XtSetArg(args[n], XmNtopOffset, 5); n++;
578 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
579 XtSetArg(args[n], XmNleftOffset, 5); n++;
580 XtSetValues(glx, args, n);
581
582 n = 0;
583 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
584 XtSetArg(args[n], XmNtopWidget, menu); n++;
585 XtSetArg(args[n], XmNtopOffset, 5); n++;
586 XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
587 XtSetArg(args[n], XmNleftWidget, glx); n++;
588 XtSetArg(args[n], XmNleftOffset, 5); n++;
589 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
590 XtSetArg(args[n], XmNrightOffset, 5); n++;
591 XtSetValues(viewerW, args, n);
592
593 n = 0;
594 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
595 XtSetArg(args[n], XmNtopWidget, glx); n++;
596 XtSetArg(args[n], XmNtopOffset, 5); n++;
597 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
598 XtSetArg(args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
599 XtSetArg(args[n], XmNrightWidget, glx); n++;
600 XtSetValues(textName, args, n);
601
602 n = 0;
603 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
604 XtSetArg(args[n], XmNtopWidget, butForm); n++;
605 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
606 XtSetArg(args[n], XmNleftOffset, 5); n++;
607 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
608 XtSetArg(args[n], XmNbottomOffset, 5); n++;
609 XtSetValues(sldForm, args, n);
610
611 n = 0;
612 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
613 XtSetArg(args[n], XmNtopWidget, viewerW); n++;
614 XtSetArg(args[n], XmNtopOffset, 2); n++;
615 XtSetArg(args[n], XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
616 XtSetArg(args[n], XmNleftWidget, viewerW); n++;
617 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
618 XtSetArg(args[n], XmNrightOffset, 5); n++;
619 XtSetValues(butForm, args, n);
620
621 n = 0;
622 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
623 XtSetArg(args[n], XmNtopWidget, butForm); n++;
624 XtSetArg(args[n], XmNtopOffset, 10); n++;
625 XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
626 XtSetArg(args[n], XmNleftWidget, sldForm); n++;
627 XtSetArg(args[n], XmNleftOffset, 20); n++;
628 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
629 XtSetArg(args[n], XmNrightOffset, 15); n++;
630 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
631 XtSetArg(args[n], XmNbottomOffset, 7+23); n++;
632 XtSetValues(wheelW, args, n);
633
634 n = 0;
635 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
636 XtSetArg(args[n], XmNtopWidget, wheelW); n++;
637 XtSetArg(args[n], XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
638 XtSetArg(args[n], XmNleftWidget, wheelW); n++;
639 XtSetArg(args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
640 XtSetArg(args[n], XmNrightWidget, wheelW); n++;
641 XtSetValues(sliderW, args, n);
642
643 updateTexture2UI();
644 updateTextureXfUI();
645 updateTextureFuncUI();
646 updateTextureName();
647 updateWindowTitle();
648 updateFileMenu();
649
650 // manage children
651 XtManageChild(menu);
652 XtManageChild(glx);
653 XtManageChild(textName);
654 XtManageChild(viewerW);
655 XtManageChild(butForm);
656 XtManageChild(sldForm);
657
658 return form;
659 }
660
661 ////////////////////////////////////////////////////////////////////////
662 //
663 // Description:
664 // creates the menu bar
665 //
666 // Use: private
667 Widget
668 MyTextureEditor::buildMenu(Widget parent)
669 //
670 ////////////////////////////////////////////////////////////////////////
671 {
672 Arg args[8];
673 Widget buttons[10], subButtons[10], subMenu;
674 XmString xmstr;
675 int n, butNum = 0, subButNum;
676
677 Widget menu = XmCreateMenuBar(parent, "menuBar", NULL, 0);
678 widgetList[MENU_BAR] = menu;
679
680 //
681 // create the "File" menu
682 //
683
684 subMenu = XmCreatePulldownMenu(menu, "subMenu", NULL, 0);
685
686 XtSetArg(args[0], XmNsubMenuId, subMenu);
687 buttons[butNum] = XmCreateCascadeButtonGadget(menu, "File", args, 1);
688
689 // create the menu entries
690 n = 0;
691 XtSetArg(args[n], XmNuserData, this); n++;
692
693 #define ADD_ENTRY(NAME, ID, ACC, ACCTEXT) \
694 xmstr = XmStringCreateSimple(ACCTEXT); \
695 XtSetArg(args[n], XmNaccelerator, ACC); \
696 XtSetArg(args[n+1], XmNacceleratorText, xmstr); \
697 subButtons[subButNum] = XmCreatePushButtonGadget(subMenu, NAME, args, n+2); \
698 XtAddCallback(subButtons[subButNum], XmNactivateCallback, \
699 (XtCallbackProc) MyTextureEditor::fileMenuCB, (XtPointer) ID); \
700 XmStringFree(xmstr); \
701 widgetList[ID] = subButtons[subButNum++];
702
703 subButNum = 0;
704 ADD_ENTRY("New...", FILE_NEW, "Alt <Key> n", "Alt+n")
705 ADD_ENTRY("Reset", FILE_RESET, "Alt <Key> r", "Alt+r")
706 ADD_ENTRY("Delete", FILE_DELETE, "Alt <Key> d", "Alt+d")
707 #undef ADD_ENTRY
708
709 XtManageChildren(subButtons, subButNum);
710 butNum++;
711
712 //
713 // create the "Palette" menu
714 //
715
716 widgetList[PALETTE_BUTTON] = buttons[butNum] = XmCreateCascadeButtonGadget(
717 menu, "Palettes", NULL, 0);
718 buildPaletteSubMenu();
719 butNum++;
720
721 XtManageChildren(buttons, butNum);
722
723 return menu;
724 }
725
726 ////////////////////////////////////////////////////////////////////////
727 //
728 // Description:
729 // adds an entry to the palette menu.
730 //
731 // Use: private
732 Widget
733 MyTextureEditor::buildPaletteMenuEntry(int id)
734 //
735 ////////////////////////////////////////////////////////////////////////
736 {
737 PaletteStruct *pal = (PaletteStruct *) paletteList[id];
738 char accel[20];
739 char accelText[20];
740 XmString xmstr = NULL;
741 Arg args[4];
742 int n = 0;
743
744 XtSetArg(args[n], XmNuserData, this); n++;
745 if (id < 10) {
746 sprintf(accel, "Alt <Key> %d", id);
747 sprintf(accelText, "Alt+%d", id);
748 xmstr = XmStringCreateSimple(accelText);
749
750 XtSetArg(args[n], XmNaccelerator, accel); n++;
751 XtSetArg(args[n], XmNacceleratorText, xmstr); n++;
752 }
753
754 Widget w = XmCreatePushButtonGadget(widgetList[PALETTE_MENU], pal->name, args, n);
755 XtAddCallback(w, XmNactivateCallback,
756 (XtCallbackProc) MyTextureEditor::paletteMenuCB,
757 (XtPointer) (unsigned long) id);
758
759 if (xmstr != NULL)
760 XmStringFree(xmstr);
761
762 return w;
763 }
764
765 ////////////////////////////////////////////////////////////////////////
766 //
767 // Description:
768 // Buils the palette popup menu.
769 //
770 // Use: private
771 void
772 MyTextureEditor::buildPaletteSubMenu()
773 //
774 ////////////////////////////////////////////////////////////////////////
775 {
776 // rebuild the palette popup
777 // ??? we cannot delete the old popup menu or things will brake.
778 // ??? Is it automatically deleted for us ?
779 widgetList[PALETTE_MENU] = XmCreatePulldownMenu(widgetList[MENU_BAR],
780 "widgetList", NULL, 0);
781
782 Arg args[1];
783 XtSetArg(args[0], XmNsubMenuId, widgetList[PALETTE_MENU]);
784 XtSetValues(widgetList[PALETTE_BUTTON], args, 1);
785
786 // build all of the entries
787 Widget *entries = new Widget[paletteList.getLength()];
788 for (int i=0; i<paletteList.getLength(); i++)
789 entries[i] = buildPaletteMenuEntry(i);
790
791 XtManageChildren(entries, paletteList.getLength());
792 delete [] entries;
793 }
794
795 ////////////////////////////////////////////////////////////////////////
796 //
797 // Description:
798 // Builds the GLX region where the textures will be displayed
799 //
800 // Use: private
801 Widget
802 MyTextureEditor::buildTexturePaletteWidget(Widget parent)
803 //
804 ////////////////////////////////////////////////////////////////////////
805 {
806 Arg args[12];
807 int n = 0;
808
809 XtSetArg(args[n], XtNwidth, GLX_SIZE); n++;
810 XtSetArg(args[n], XtNheight, GLX_SIZE); n++;
811 XtSetArg(args[n], GLwNrgba, TRUE); n++;
812 // This makes sure we get the maximum buffer configuration
813 // (by showing interest the number of bits)
814 XtSetArg(args[n], GLwNredSize, 1); n++;
815 XtSetArg(args[n], GLwNgreenSize, 1); n++;
816 XtSetArg(args[n], GLwNblueSize, 1); n++;
817
818 Widget glx = XtCreateWidget("paletteGLX", glwMDrawingAreaWidgetClass,
819 parent, args, n);
820 widgetList[TEXTURE_GLX] = glx;
821
822 XtUninstallTranslations(glx);
823
824 XtAddCallback(glx, GLwNginitCallback,
825 (XtCallbackProc) MyTextureEditor::glxInitCB, (XtPointer) this);
826 XtAddCallback(glx, GLwNexposeCallback,
827 (XtCallbackProc) MyTextureEditor::glxExposeCB, (XtPointer) this);
828 XtAddEventHandler(glx,
829 (PointerMotionMask | ButtonPressMask | ButtonReleaseMask | LeaveWindowMask),
830 FALSE, (XtEventHandler) MyTextureEditor::glxEventCB, (XtPointer) this);
831
832 return glx;
833 }
834
835 ////////////////////////////////////////////////////////////////////////
836 //
837 // Description:
838 // Builds the sliders into a form.
839 //
840 // Use: private
841 Widget
842 MyTextureEditor::buildSliders(Widget parent)
843 //
844 ////////////////////////////////////////////////////////////////////////
845 {
846 int i, n, num;
847 Arg args[12];
848 Widget labels[5], fields[5], sliders[5];
849
850 // create a form to hold everything together
851 Widget form = XmCreateForm(parent, "slidersForm", NULL, 0);
852
853 //
854 // create all the parts
855 //
856
857 repeatState = TRUE; // build the UI with repeat turned on (default)
858
859 // create all of the labels
860 n = 0;
861 XtSetArg(args[n], XmNalignment, XmALIGNMENT_END); n++;
862 labels[0] = XmCreateLabelGadget(form, "Translate X:", args, n);
863 labels[1] = XmCreateLabelGadget(form, "Translate Y:", args, n);
864 labels[2] = XmCreateLabelGadget(form, "Rotate:", args, n);
865 widgetList[SCALE_X_LABEL] = labels[3] = XmCreateLabelGadget(form, "Repeat X:", args, n);
866 widgetList[SCALE_Y_LABEL] = labels[4] = XmCreateLabelGadget(form, "Repeat Y:", args, n);
867
868 // create all of the text fields
869 n = 0;
870 XtSetArg(args[n], XmNuserData, this); n++;
871 XtSetArg(args[n], XmNhighlightThickness, 1); n++;
872 XtSetArg(args[n], XmNcolumns, 5); n++;
873
874 #define CREATE_FIELD(ID) \
875 widgetList[ID] = fields[num] = XmCreateText(form, "", args, n); \
876 XtAddCallback(fields[num], XmNvalueChangedCallback, \
877 (XtCallbackProc) MyTextureEditor::fieldChangedCB, (XtPointer) this); \
878 XtAddCallback(fields[num], XmNactivateCallback, \
879 (XtCallbackProc) MyTextureEditor::fieldsCB, (XtPointer) ID); \
880 XtAddCallback(fields[num], XmNlosingFocusCallback, \
881 (XtCallbackProc) MyTextureEditor::fieldsCB, (XtPointer) ID); \
882 num++;
883
884 num = 0;
885 CREATE_FIELD(TRANS_X_FIELD)
886 CREATE_FIELD(TRANS_Y_FIELD)
887 CREATE_FIELD(ROT_FIELD)
888 CREATE_FIELD(SCALE_X_FIELD)
889 CREATE_FIELD(SCALE_Y_FIELD)
890 #undef CREATE_FIELD
891
892 // create all of the sliders
893 n = 0;
894 XtSetArg(args[n], XmNuserData, this); n++;
895 XtSetArg(args[n], XmNorientation, XmHORIZONTAL); n++;
896 XtSetArg(args[n], XmNhighlightThickness, 0); n++;
897 XtSetArg(args[n], XmNwidth, 100); n++;
898 XtSetArg(args[n], XmNheight, 20); n++;
899
900 #define CREATE_SCALE(ID) \
901 widgetList[ID] = sliders[num] = XmCreateScale(form, "", args, n); \
902 XtAddCallback(sliders[num], XmNvalueChangedCallback, \
903 (XtCallbackProc) MyTextureEditor::slidersCB, (XtPointer) ID); \
904 XtAddCallback(sliders[num++], XmNdragCallback, \
905 (XtCallbackProc) MyTextureEditor::slidersCB, (XtPointer) ID);
906
907 num = 0;
908 CREATE_SCALE(TRANS_X_SLD)
909 CREATE_SCALE(TRANS_Y_SLD)
910 CREATE_SCALE(ROT_SLD)
911
912 scaleXThumb = new MyThumbWheel(form);
913 scaleXThumb->setSize(SbVec2s(100, 21));
914 scaleXThumb->addValueChangedCallback(
915 MyTextureEditor::scaleXThumbCB, this);
916
917 scaleYThumb = new MyThumbWheel(form);
918 scaleYThumb->setSize(SbVec2s(100, 21));
919 scaleYThumb->addValueChangedCallback(
920 MyTextureEditor::scaleYThumbCB, this);
921
922 sliders[num++] = scaleXThumb->getWidget();
923 sliders[num++] = scaleYThumb->getWidget();
924 #undef CREATE_SCALE
925
926 //
927 // layout !
928 //
929
930 // text field first
931 n = 0;
932 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
933 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
934 XtSetValues(fields[0], args, n);
935 n = 0;
936 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
937 XtSetArg(args[n], XmNtopOffset, 1); n++;
938 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
939 for (i=1; i<5; i++) {
940 XtSetArg(args[n], XmNtopWidget, fields[i-1]);
941 XtSetValues(fields[i], args, n+1);
942 }
943
944 // sliders
945 n = 0;
946 XtSetArg(args[n], XmNrightAttachment, XmATTACH_WIDGET); n++;
947 XtSetArg(args[n], XmNrightOffset, 1); n++;
948 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
949 for (i=0; i<5; i++) {
950 XtSetArg(args[n], XmNrightWidget, fields[i]);
951 XtSetArg(args[n+1], XmNbottomWidget, fields[i]);
952 XtSetArg(args[n+2], XmNbottomOffset, (i < 3) ? 7 : 4);
953 XtSetValues(sliders[i], args, n+3);
954 }
955
956 // labels (centered around text fields)
957 n = 0;
958 XtSetArg(args[n], XmNrightAttachment, XmATTACH_WIDGET); n++;
959 XtSetArg(args[n], XmNrightOffset, 3); n++;
960 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
961 XtSetArg(args[n], XmNleftOffset, 5); n++;
962 XtSetArg(args[n], XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
963 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
964 for (i=0; i<5; i++) {
965 XtSetArg(args[n], XmNrightWidget, sliders[i]);
966 XtSetArg(args[n+1], XmNtopWidget, fields[i]);
967 XtSetArg(args[n+2], XmNbottomWidget, fields[i]);
968 XtSetValues(labels[i], args, n+3);
969 }
970
971 // manage children
972 XtManageChildren(fields, 5);
973 XtManageChildren(sliders, 5);
974 XtManageChildren(labels, 5);
975
976 return form;
977 }
978
979 ////////////////////////////////////////////////////////////////////////
980 //
981 // Description:
982 // Builds the accept + option buttons
983 //
984 // Use: private
985 Widget
986 MyTextureEditor::buildButtons(Widget parent)
987 //
988 ////////////////////////////////////////////////////////////////////////
989 {
990 Widget acceptBut, labels[2], buttons[2];
991 Arg args[12];
992 int i, n;
993
994 // create a form to hold everything together
995 Widget form = XmCreateForm(parent, "buttonForm", NULL, 0);
996
997 //
998 // create all the parts
999 //
1000
1001 // create the accept button
1002 n = 0;
1003 XtSetArg(args[n], XmNhighlightThickness, 0); n++;
1004 acceptBut = XmCreatePushButtonGadget(form, "Accept", args, n);
1005 XtAddCallback(acceptBut, XmNactivateCallback,
1006 (XtCallbackProc) MyTextureEditor::acceptCB, (XtPointer) this);
1007
1008 // create the labels
1009 n = 0;
1010 XtSetArg(args[n], XmNalignment, XmALIGNMENT_END); n++;
1011 labels[0] = XmCreateLabelGadget(form, "Mapping:", args, n);
1012 labels[1] = XmCreateLabelGadget(form, "Options:", args, n);
1013 // make all the labels the same width (for layout)
1014 short w; int width = 0;
1015 for (i=0; i<2; i++) {
1016 XtVaGetValues(labels[i], XtNwidth, &w, NULL);
1017 if (w > width) width = w;
1018 }
1019 for (i=0; i<2; i++)
1020 XtVaSetValues(labels[i], XtNwidth, width, NULL);
1021
1022 // create the mapping button option
1023 Widget list[15];
1024 int num;
1025
1026 widgetList[MAPP_PULLDOWN] = XmCreatePulldownMenu(form, "widgetList", NULL, 0);
1027 n = 0;
1028 XtSetArg(args[n], XmNuserData, this); n++;
1029
1030 #define ADD_ENTRY(NAME, ID) \
1031 widgetList[ID] = list[num] = XmCreatePushButtonGadget(widgetList[MAPP_PULLDOWN], \
1032 NAME, args, n); \
1033 XtAddCallback(list[num++], XmNactivateCallback, \
1034 (XtCallbackProc) MyTextureEditor::mappingMenuCB, (XtPointer) ID);
1035
1036 num = 0;
1037 ADD_ENTRY("default", MAP_DEFAULT)
1038 ADD_ENTRY("reflection", MAP_ENV)
1039 ADD_ENTRY("xy plane", MAP_PLANE_XY)
1040 ADD_ENTRY("yz plane", MAP_PLANE_YZ)
1041 ADD_ENTRY("xz plane", MAP_PLANE_XZ)
1042 ADD_ENTRY("unknown", MAP_UNKNOWN)
1043 #undef ADD_ENTRY
1044
1045 XtVaSetValues(widgetList[MAP_UNKNOWN], XmNsensitive, FALSE, NULL);
1046 XtManageChildren(list, num);
1047
1048 n = 0;
1049 XtSetArg(args[n], XmNhighlightThickness, 0); n++;
1050 XtSetArg(args[n], XmNmarginWidth, 0); n++;
1051 XtSetArg(args[n], XmNmarginHeight, 0); n++;
1052 XtSetArg(args[n], XmNsubMenuId, widgetList[MAPP_PULLDOWN]); n++;
1053 buttons[0] = XmCreateOptionMenu(form, "optionMenu", args, n);
1054
1055 // create the options button
1056 widgetList[OPT_PULLDOWN] = XmCreatePulldownMenu(form, "widgetList", NULL, 0);
1057 n = 0;
1058 XtSetArg(args[n], XmNuserData, this); n++;
1059
1060 #define ADD_ENTRY(NAME, ID) \
1061 widgetList[ID] = list[num] = XmCreatePushButtonGadget(widgetList[OPT_PULLDOWN], \
1062 NAME, args, n); \
1063 XtAddCallback(list[num++], XmNactivateCallback, \
1064 (XtCallbackProc) MyTextureEditor::optionMenuCB, (XtPointer) ID);
1065
1066 num = 0;
1067 ADD_ENTRY("repeat", OPT_REPEAT)
1068 ADD_ENTRY("clamp", OPT_CLAMP)
1069 ADD_ENTRY("unknown", OPT_UNKNOWN)
1070 #undef ADD_ENTRY
1071
1072 XtVaSetValues(widgetList[OPT_UNKNOWN], XmNsensitive, FALSE, NULL);
1073 XtManageChildren(list, num);
1074
1075 n = 0;
1076 XtSetArg(args[n], XmNhighlightThickness, 0); n++;
1077 XtSetArg(args[n], XmNmarginWidth, 0); n++;
1078 XtSetArg(args[n], XmNmarginHeight, 0); n++;
1079 XtSetArg(args[n], XmNsubMenuId, widgetList[OPT_PULLDOWN]); n++;
1080 buttons[1] = XmCreateOptionMenu(form, "optionMenu", args, n);
1081
1082 //
1083 // layout !
1084 //
1085
1086 // center the accept button
1087 n = 0;
1088 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
1089 XtSetArg(args[n], XmNleftAttachment, XmATTACH_POSITION); n++;
1090 XtSetArg(args[n], XmNleftPosition, 50); n++;
1091 XtSetArg(args[n], XmNleftOffset, -30); n++;
1092 XtSetValues(acceptBut, args, n);
1093
1094 // attach option menu buttons to the right
1095 n = 0;
1096 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
1097 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
1098
1099 XtSetArg(args[n], XmNtopOffset, 5);
1100 XtSetArg(args[n+1], XmNtopWidget, acceptBut);
1101 XtSetValues(buttons[0], args, n+2);
1102 XtSetArg(args[n], XmNtopWidget, buttons[0]);
1103 XtSetValues(buttons[1], args, n+1);
1104
1105 // center the labels around the option menu
1106 n = 0;
1107 XtSetArg(args[n], XmNrightAttachment, XmATTACH_WIDGET); n++;
1108 XtSetArg(args[n], XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
1109 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
1110
1111 XtSetArg(args[n], XmNrightWidget, buttons[0]);
1112 XtSetArg(args[n+1], XmNtopWidget, buttons[0]);
1113 XtSetArg(args[n+2], XmNbottomWidget, buttons[0]);
1114 XtSetValues(labels[0], args, n+3);
1115 XtSetArg(args[n], XmNrightWidget, buttons[1]);
1116 XtSetArg(args[n+1], XmNtopWidget, buttons[1]);
1117 XtSetArg(args[n+2], XmNbottomWidget, buttons[1]);
1118 XtSetValues(labels[1], args, n+3);
1119
1120 // manage children
1121 XtManageChild(acceptBut);
1122 XtManageChildren(buttons, 2);
1123 XtManageChildren(labels, 2);
1124
1125 return form;
1126 }
1127
1128 ////////////////////////////////////////////////////////////////////////
1129 //
1130 // Description:
1131 // redraws the texture palette
1132 //
1133 // Use: private
1134 void
1135 MyTextureEditor::redrawPalette()
1136 //
1137 ////////////////////////////////////////////////////////////////////////
1138 {
1139 if (! isVisible())
1140 return;
1141
1142 glXMakeCurrent(getDisplay(), XtWindow(widgetList[TEXTURE_GLX]), paletteCtx);
1143
1144 for (int i=0; i<IMAGE_TOTAL; i++) {
1145 if (i != currentItem && i != selectedItem)
1146 drawTextureTile(i, DRAW_NORM);
1147 }
1148 if (currentItem != -1 && currentItem != selectedItem)
1149 drawTextureTile(currentItem, DRAW_CURRENT);
1150 if (selectedItem != -1)
1151 drawTextureTile(selectedItem, DRAW_SELECTED);
1152 }
1153
1154 ////////////////////////////////////////////////////////////////////////
1155 //
1156 // Description:
1157 // draws the given texture tile
1158 //
1159 // Use: private
1160 void
1161 MyTextureEditor::drawTextureTile(int id, int style)
1162 //
1163 ////////////////////////////////////////////////////////////////////////
1164 {
1165 if (id < 0)
1166 return;
1167
1168 int row = int(id / IMAGE_NUM);
1169 int col = id - IMAGE_NUM * row;
1170 int s = IMAGE_SPACE + IMAGE_SIZE;
1171 int x1 = col * s + IMAGE_SPACE;
1172 int x2 = x1 + IMAGE_SIZE - 1;
1173 int y1 = (IMAGE_NUM - row - 1) * s + IMAGE_SPACE;
1174 int y2 = y1 + IMAGE_SIZE - 1;
1175
1176 // paste the texture image
1177 if (textureNames[id].name != NULL) {
1178 glRasterPos2i(x1, y1);
1179 glDrawPixels(IMAGE_SIZE, IMAGE_SIZE, GL_RGB, GL_UNSIGNED_BYTE,
1180 textureNames[id].iconImage);
1181 }
1182 else {
1183 glColor3ub(150, 150, 150);
1184 glBegin(GL_POLYGON);
1185 glVertex2s(x1, y1); glVertex2s(x2+1, y1);
1186 glVertex2s(x2+1, y2+1); glVertex2s(x1, y2+1);
1187 glEnd();
1188 }
1189
1190 // show the boder highlighting
1191 switch(style) {
1192 case DRAW_NORM:
1193 glColor3ub(150, 150, 150);
1194 RECT(x1-2, y1-2, x2+2, y2+2);
1195 RECT(x1-1, y1-1, x2+1, y2+1);
1196 break;
1197 case DRAW_CURRENT:
1198 glColor3ub(230, 50, 50);
1199 RECT(x1-2, y1-2, x2+2, y2+2);
1200 RECT(x1-1, y1-1, x2+1, y2+1);
1201 break;
1202 case DRAW_SELECTED:
1203 glColor3ub(230, 50, 50);
1204 RECT(x1-2, y1-2, x2+2, y2+2);
1205 RECT(x1-1, y1-1, x2+1, y2+1);
1206 RECT(x1, y1, x2, y2);
1207 RECT(x1+1, y1+1, x2-1, y2-1);
1208 break;
1209 }
1210
1211 glFlush();
1212 }
1213
1214 ////////////////////////////////////////////////////////////////////////
1215 //
1216 // Description:
1217 // update the texture name based of what is selected
1218 //
1219 // Use: private
1220 void
1221 MyTextureEditor::updateTextureName()
1222 //
1223 ////////////////////////////////////////////////////////////////////////
1224 {
1225 char *str = (currentItem < 0) ? ((selectedItem < 0) ? (char *) " " :
1226 textureNames[selectedItem].name) : textureNames[currentItem].name;
1227 if (str == NULL)
1228 str = noFileNameStr;
1229 XmString xmstr = XmStringCreateSimple(str);
1230 XtVaSetValues(widgetList[TEXTURE_NAME], XmNlabelString, xmstr, NULL);
1231 XmStringFree(xmstr);
1232 }
1233
1234 ////////////////////////////////////////////////////////////////////////
1235 //
1236 // Description:
1237 // update the texture node based on currently selected texture
1238 //
1239 // Use: private
1240 void
1241 MyTextureEditor::updateTextureNode()
1242 //
1243 ////////////////////////////////////////////////////////////////////////
1244 {
1245 if (selectedItem < 0)
1246 return;
1247
1248 TextureNameStruct *txt = &textureNames[selectedItem];
1249 if (txt->fullName != NULL) {
1250 texNode->filename.setValue(txt->fullName);
1251
1252 // check MODULATE vs BLEND
1253 if (txt->zsize == 3 || txt->zsize == 4)
1254 texNode->model = SoTexture2::MODULATE;
1255 else
1256 texNode->model = SoTexture2::BLEND;
1257 }
1258 else // no texture
1259 texNode->filename.setValue("");
1260 }
1261
1262 ////////////////////////////////////////////////////////////////////////
1263 //
1264 // Description:
1265 // updates the UI to reflect the current texture2 node
1266 //
1267 // Use: private
1268 void
1269 MyTextureEditor::updateTexture2UI()
1270 //
1271 ////////////////////////////////////////////////////////////////////////
1272 {
1273 // update color wheel + color slider
1274 float hsv[3];
1275 SbColor rgb = texNode->blendColor.getValue();
1276 rgb.getHSVValue(hsv);
1277 ignoreCallback = TRUE;
1278 colWheel->setBaseColor(hsv);
1279 colSlider->setBaseColor(hsv);
1280 ignoreCallback = FALSE;
1281
1282 // update the option menu
1283 int ID = OPT_UNKNOWN;
1284 if (texNode->wrapS.getValue() == texNode->wrapT.getValue()) {
1285 if (texNode->wrapS.getValue() == SoTexture2::REPEAT)
1286 ID = OPT_REPEAT;
1287 else if (texNode->wrapS.getValue() == SoTexture2::CLAMP)
1288 ID = OPT_CLAMP;
1289 }
1290 XtVaSetValues(widgetList[OPT_PULLDOWN], XmNmenuHistory, widgetList[ID], NULL);
1291 }
1292
1293 ////////////////////////////////////////////////////////////////////////
1294 //
1295 // Description:
1296 // updates the UI to reflect the current texture2Transform node
1297 //
1298 // Use: private
1299 void
1300 MyTextureEditor::updateTextureXfUI()
1301 //
1302 ////////////////////////////////////////////////////////////////////////
1303 {
1304 updateTextureFieldAndSlider(SCALE_X_FIELD);
1305 updateTextureFieldAndSlider(SCALE_Y_FIELD);
1306 updateTextureFieldAndSlider(TRANS_X_FIELD);
1307 updateTextureFieldAndSlider(TRANS_Y_FIELD);
1308 updateTextureFieldAndSlider(ROT_FIELD);
1309 }
1310
1311 ////////////////////////////////////////////////////////////////////////
1312 //
1313 // Description:
1314 // updates the UI to reflect the current texture function node
1315 //
1316 // Use: private
1317 void
1318 MyTextureEditor::updateTextureFuncUI()
1319 //
1320 ////////////////////////////////////////////////////////////////////////
1321 {
1322 // update the option menu
1323 int ID = MAP_UNKNOWN;
1324 if (texFuncNode->isOfType(SoTextureCoordinateDefault::getClassTypeId()))
1325 ID = MAP_DEFAULT;
1326 else if (texFuncNode->isOfType(SoTextureCoordinateEnvironment::getClassTypeId()))
1327 ID = MAP_ENV;
1328 else if (texFuncNode->isOfType(SoTextureCoordinatePlane::getClassTypeId())) {
1329 SbVec3f directionS = ((SoTextureCoordinatePlane *)texFuncNode)->directionS.getValue();
1330 SbVec3f directionT = ((SoTextureCoordinatePlane *)texFuncNode)->directionT.getValue();
1331 if (directionS == SbVec3f(1, 0, 0) && directionT == SbVec3f(0, 1, 0))
1332 ID = MAP_PLANE_XY;
1333 else if (directionS == SbVec3f(1, 0, 0) && directionT == SbVec3f(0, 0, 1))
1334 ID = MAP_PLANE_XZ;
1335 else if (directionS == SbVec3f(0, 1, 0) && directionT == SbVec3f(0, 0, 1))
1336 ID = MAP_PLANE_YZ;
1337 }
1338 XtVaSetValues(widgetList[MAPP_PULLDOWN], XmNmenuHistory, widgetList[ID], NULL);
1339 }
1340
1341 ////////////////////////////////////////////////////////////////////////
1342 //
1343 // Description:
1344 // sets the repeate state and updates the UI. It returns TRUE if
1345 // set state has changed.
1346 //
1347 // Use: private
1348 SbBool
1349 MyTextureEditor::setRepeatState(SbBool flag)
1350 //
1351 ////////////////////////////////////////////////////////////////////////
1352 {
1353 if (repeatState == flag)
1354 return FALSE;
1355 repeatState = flag;
1356
1357 // XmToggleButtonSetState(widgetList[REPEAT_TOGGLE], repeatState, FALSE);
1358
1359 // update the scale labels
1360 XmString xmstr1, xmstr2;
1361 if (repeatState) {
1362 xmstr1 = XmStringCreateSimple("Repeat X:");
1363 xmstr2 = XmStringCreateSimple("Repeat Y:");
1364 }
1365 else {
1366 xmstr1 = XmStringCreateSimple("Scale X:");
1367 xmstr2 = XmStringCreateSimple("Scale Y:");
1368 }
1369 XtVaSetValues(widgetList[SCALE_X_LABEL], XmNlabelString, xmstr1, NULL);
1370 XtVaSetValues(widgetList[SCALE_Y_LABEL], XmNlabelString, xmstr2, NULL);
1371 XmStringFree(xmstr1);
1372 XmStringFree(xmstr2);
1373
1374 // update the scale text fields to new format
1375 updateTextureFieldAndSlider(SCALE_X_FIELD);
1376 updateTextureFieldAndSlider(SCALE_Y_FIELD);
1377
1378 return TRUE;
1379 }
1380
1381 ////////////////////////////////////////////////////////////////////////
1382 //
1383 // Description:
1384 // updates the given text field and slider to relfect current settings
1385 //
1386 // Use: private
1387 void
1388 MyTextureEditor::updateTextureFieldAndSlider(int fieldID)
1389 //
1390 ////////////////////////////////////////////////////////////////////////
1391 {
1392 float val;
1393 int intVal;
1394 char str[10];
1395
1396 switch(fieldID) {
1397 case TRANS_X_FIELD:
1398 val = texXfNode->translation.getValue()[0];
1399 sprintf(str, "%.2f", val);
1400 intVal = (val > 1.0) ? 100 : ((val < -1) ? 0 : int((val + 1) * 50));
1401 XmScaleSetValue(widgetList[TRANS_X_SLD], intVal);
1402 break;
1403
1404 case TRANS_Y_FIELD:
1405 val = texXfNode->translation.getValue()[1];
1406 sprintf(str, "%.2f", val);
1407 intVal = (val > 1.0) ? 100 : ((val < -1) ? 0 : int((val + 1) * 50));
1408 XmScaleSetValue(widgetList[TRANS_Y_SLD], intVal);
1409 break;
1410
1411 case ROT_FIELD:
1412 val = texXfNode->rotation.getValue() * 180.0 / M_PI; // ??? prevent roundoff
1413 intVal = int(val);
1414 sprintf(str, "%d", intVal);
1415 intVal = int(100 * intVal / 360.0);
1416 XmScaleSetValue(widgetList[ROT_SLD], intVal);
1417 break;
1418
1419 case SCALE_X_FIELD:
1420 val = texXfNode->scaleFactor.getValue()[0];
1421 // make it look like we are scaling the image, not the texture coord
1422 if (repeatState)
1423 sprintf(str, "%.1f", val);
1424 else
1425 sprintf(str, "%.2f", 1 / val);
1426 break;
1427
1428 case SCALE_Y_FIELD:
1429 val = texXfNode->scaleFactor.getValue()[1];
1430 // make it look like we are scaling the image, not the texture coord
1431 if (repeatState)
1432 sprintf(str, "%.1f", val);
1433 else
1434 sprintf(str, "%.2f", 1 / val);
1435 break;
1436 }
1437
1438 XmTextSetString(widgetList[fieldID], str);
1439 }
1440
1441 ////////////////////////////////////////////////////////////////////////
1442 //
1443 // Description:
1444 // Get the list of directories (palettes) from the env var, or the
1445 // paletteDir variable (constructor). This also searches for palettes
1446 // saved under the home directory, which would overide the system
1447 // installed palettes.
1448 //
1449 // Use: private
1450 void
1451 MyTextureEditor::getPaletteNames()
1452 //
1453 ////////////////////////////////////////////////////////////////////////
1454 {
1455 struct dirent *direntry;
1456 DIR *dirp;
1457 char *f;
1458 char currentDir[MAXPATHLEN];
1459
1460 // this is only called once at startup, so it doesn't
1461 // need to free the old paletteList.
1462
1463 //
1464 // look for palettes under the default installed place
1465 //
1466
1467 // see if SO_TEXTURE_DIR is set, if so use it...
1468 char *envDir = getenv("SO_TEXTURE_DIR");
1469 if (envDir != NULL) {
1470 free(paletteDir);
1471 paletteDir = strdup(envDir);
1472 }
1473
1474 if (dirp = opendir(paletteDir)) {
1475 getcwd(currentDir, MAXPATHLEN-1);
1476 chdir(paletteDir);
1477 while (direntry = readdir(dirp)) {
1478 f = direntry->d_name;
1479 // hide '.' files
1480 if (f[0] != '.' && isDirectory(f)) {
1481 PaletteStruct *pal = new PaletteStruct;
1482 pal->name = strdup(f);
1483 pal->system = TRUE;
1484 pal->user = FALSE;
1485 paletteList.append(pal);
1486 }
1487 }
1488 closedir(dirp);
1489 chdir(currentDir); // back to our working directory
1490 }
1491
1492 //
1493 // Now look for palettes under the user's home directory, which
1494 // would overide the installed palettes.
1495 //
1496
1497 char customDir[MAXPATHLEN];
1498 sprintf(customDir, "%s/%s", getenv("HOME"), customTextureDir);
1499 if (dirp = opendir(customDir)) {
1500 getcwd(currentDir, MAXPATHLEN-1);
1501 chdir(customDir);
1502 while (direntry = readdir(dirp)) {
1503 f = direntry->d_name;
1504 // hide '.' files
1505 if (f[0] != '.' && !isDirectory(f)) {
1506
1507 // check if palette name is already in the list
1508 // (user overide case), else create a new entry
1509 //
1510 PaletteStruct *pal = findPalette(f, &paletteList);
1511 if (pal != NULL)
1512 pal->user = TRUE;
1513 else {
1514 pal = new PaletteStruct;
1515 pal->name = strdup(f);
1516 pal->user = TRUE;
1517 pal->system = FALSE;
1518 paletteList.append(pal);
1519 }
1520 }
1521 }
1522 closedir(dirp);
1523 chdir(currentDir); // back to our working directory
1524 }
1525
1526 //
1527 // make sure we have at least one palette, else create an empty default
1528 // palette for things to work.
1529 //
1530 curPalette = 0;
1531 if (paletteList.getLength() == 0) {
1532 #ifdef DEBUG
1533 SoDebugError::post("MyTextureEditor::getPaletteNames",
1534 "cannot find palettes in directory %s or home directory. Try setting the environment variable SO_TEXTURE_DIR to a directory which has texture files in it.", paletteDir );
1535 #endif
1536
1537 // create an empty default palette, withough loading anything
1538 // since we started with a blank palette.
1539 PaletteStruct *pal = new PaletteStruct;
1540 pal->name = strdup("default");
1541 pal->user = TRUE;
1542 pal->system = FALSE;
1543 paletteList.append(pal);
1544 }
1545 }
1546
1547 ////////////////////////////////////////////////////////////////////////
1548 //
1549 // Description:
1550 // saves the current palette in the user's HOME directory.
1551 //
1552 // Use: private
1553 void
1554 MyTextureEditor::savePalette()
1555 //
1556 ////////////////////////////////////////////////////////////////////////
1557 {
1558 char dirName[MAXPATHLEN];
1559 char fileName[MAXPATHLEN];
1560 struct stat buf;
1561 FILE *file;
1562 PaletteStruct *pal = (PaletteStruct *) paletteList[curPalette];
1563
1564 // open the file for writting
1565 sprintf(dirName, "%s/%s/", getenv("HOME"), customTextureDir);
1566 if (stat(dirName, &buf) != 0)
1567 mkdir(dirName, 0x1ff);
1568 strcpy(fileName, dirName);
1569 strcat(fileName, pal->name);
1570 if ((file = fopen(fileName, "w")) == NULL) {
1571 #ifdef DEBUG
1572 SoDebugError::post("MyTextureEditor::savePalette",
1573 "couldn't create file: %s", fileName);
1574 #endif
1575 return;
1576 }
1577
1578 // write the names of the textures, on line at a time
1579 for (int i=0; i<IMAGE_TOTAL; i++) {
1580 if (textureNames[i].fullName != NULL)
1581 fprintf(file, "%s\n", textureNames[i].fullName);
1582 }
1583 fclose(file);
1584
1585 // finally update things
1586 if (! pal->user) {
1587 pal->user = TRUE;
1588 updateFileMenu();
1589 }
1590 }
1591
1592 ////////////////////////////////////////////////////////////////////////
1593 //
1594 // Description:
1595 // Given the current palette, loads all of the textures in the
1596 // current palette directory.
1597 //
1598 // Use: private
1599 void
1600 MyTextureEditor::loadPaletteItems()
1601 //
1602 ////////////////////////////////////////////////////////////////////////
1603 {
1604 if (curPalette == -1)
1605 return;
1606
1607 //
1608 // set the hour glass cursor on....
1609 //
1610 Display *display = getDisplay();
1611 Widget shell = SoXt::getShellWidget(getWidget());
1612 Window window = XtWindow(shell);
1613 static Cursor cursor = 0;
1614 if (cursor == 0) {
1615 Drawable d = DefaultRootWindow(display);
1616 XColor foreground;
1617 foreground.red = 65535;
1618 foreground.green = foreground.blue = 0;
1619
1620 Pixmap source = XCreateBitmapFromData(display, d,
1621 hourglass_bits, hourglass_width, hourglass_height);
1622 cursor = XCreatePixmapCursor(display, source, source,
1623 &foreground, &foreground, hourglass_x_hot, hourglass_y_hot);
1624 XFreePixmap(display, source);
1625 }
1626 XDefineCursor(display, window, cursor);
1627 XSync(display, FALSE); // better than XFlush() (we get the glClear() to show up)
1628
1629 // we are going to redraw each tile at a time once it is loaded
1630 // instead of waiting for all the textures to be read in
1631 // (feels a lot faster).
1632 glXMakeCurrent(getDisplay(), XtWindow(widgetList[TEXTURE_GLX]), paletteCtx);
1633 glClearColor(.6, .6, .6, 0);
1634 glClear(GL_COLOR_BUFFER_BIT);
1635
1636 char palDir[MAXPATHLEN];
1637 char currentDir[MAXPATHLEN];
1638 DIR *dirp;
1639 int i, num = 0;
1640
1641 // go to the palette directory (user saved palettes override system
1642 // installed palettes).
1643 PaletteStruct *pal = (PaletteStruct *) paletteList[curPalette];
1644 if (pal->user)
1645 sprintf(palDir, "%s/%s", getenv("HOME"), customTextureDir);
1646 else
1647 sprintf(palDir, "%s/%s", paletteDir, pal->name);
1648
1649 if ((dirp = opendir(palDir)) != NULL) {
1650 // cd to palette directory
1651 getcwd(currentDir, MAXPATHLEN-1);
1652 chdir(palDir);
1653
1654 //
1655 // get the names of the texture files
1656 //
1657
1658 char fullName[MAXPATHLEN];
1659 if (pal->user) {
1660 // open the custom file and read the content of it (list of textures)
1661 FILE *file;
1662 if ((file = fopen(pal->name, "r")) != NULL) {
1663
1664 // read each line entry (full texture path name) and add it to
1665 // the list of textures
1666 while(fscanf(file, " %s ", fullName) != EOF && num < (IMAGE_TOTAL-1)) {
1667 if (addTextureEntry(num, fullName)) {
1668 drawTextureTile(num, DRAW_NORM);
1669 num++;
1670 }
1671 }
1672 fclose(file);
1673 }
1674 }
1675 // system installed. Look for any image files under the current directory
1676 else {
1677 struct dirent *direntry;
1678
1679 while ((direntry = readdir(dirp)) && num < (IMAGE_TOTAL-1)) {
1680 char *file = direntry->d_name;
1681
1682 // ignore '.' files and directories
1683 if (file[0] == '.' || isDirectory(file))
1684 continue;
1685
1686 // add the file to the list of textures
1687 sprintf(fullName, "%s/%s", palDir, file);
1688 if (addTextureEntry(num, fullName)) {
1689 drawTextureTile(num, DRAW_NORM);
1690 num++;
1691 }
1692 }
1693 }
1694
1695 closedir(dirp);
1696 chdir(currentDir); // back to our working directory
1697 }
1698
1699 // clear the remaining texture names
1700 for (i=num; i<IMAGE_TOTAL; i++)
1701 deleteTextureEntry(i);
1702
1703 // restore the cursor
1704 XUndefineCursor(display, window);
1705
1706 loadedPalette = TRUE;
1707 }
1708
1709 ////////////////////////////////////////////////////////////////////////
1710 //
1711 // Description:
1712 // reads the given image file (1-4 channels) in the given buffer of the
1713 // given size (the image file will be scaled to fit the given size). and
1714 // returns if succesfull.
1715 //
1716 // Note: this ignores the alpha channel of the image, and scales the image
1717 // by skipping/duplicating pixels (VERY fast).
1718 //
1719 // Use: private
1720 SbBool
1721 MyTextureEditor::readScaledImage(
1722 char *file,
1723 int xsize, int ysize,
1724 char *buf,
1725 int &zsize)
1726 //
1727 ////////////////////////////////////////////////////////////////////////
1728 {
1729 // check to make sure file is a valid image file
1730 IMAGE *image;
1731 if (file == NULL || file[0] == '\0')
1732 return FALSE;
1733 if ((image = iopen(file, "r")) == NULL)
1734 return FALSE;
1735 if (image->colormap != CM_NORMAL) {
1736 iclose(image);
1737 return FALSE;
1738 }
1739
1740 zsize = image->zsize;
1741
1742 // allocate needed memory
1743 short *rbuf, *gbuf, *bbuf;
1744 rbuf = new short[image->xsize];
1745 if (image->zsize > 2) {
1746 gbuf = new short[image->xsize];
1747 bbuf = new short[image->xsize];
1748 }
1749
1750 // read image in, one row at a time
1751 char *p = buf;
1752 for (int row = 0; row < ysize; row++) {
1753 // The row we'll read
1754 int rrow = (row*image->ysize)/ysize;
1755
1756 if (zsize > 2) {
1757 getrow(image, rbuf, rrow, 0);
1758 getrow(image, gbuf, rrow, 1);
1759 getrow(image, bbuf, rrow, 2);
1760 }
1761 else
1762 getrow(image, rbuf, rrow, 0);
1763
1764 // store these into an unsigned byte RGB format
1765 for (int i=0; i < xsize; i++) {
1766 int ri = (i*image->xsize)/xsize;
1767 if (zsize > 2) {
1768 *p++ = rbuf[ri];
1769 *p++ = gbuf[ri];
1770 *p++ = bbuf[ri];
1771 }
1772 else {
1773 *p++ = rbuf[ri];
1774 *p++ = rbuf[ri];
1775 *p++ = rbuf[ri];
1776 }
1777 }
1778 }
1779
1780 // delete image buffers
1781 delete [] rbuf;
1782 if (zsize > 2) {
1783 delete [] gbuf;
1784 delete [] bbuf;
1785 }
1786
1787 iclose(image);
1788 return TRUE;
1789 }
1790
1791 ////////////////////////////////////////////////////////////////////////
1792 //
1793 // Description:
1794 // reads the given image file in (1-4 channels) and return a char *
1795 // RGB format of the image (withought the alpha channel)
1796 //
1797 // Use: private
1798 char *
1799 MyTextureEditor::readImage(char *file, int &xsize, int &ysize, int &zsize)
1800 //
1801 ////////////////////////////////////////////////////////////////////////
1802 {
1803 // check to make sure file is a valid image file
1804 IMAGE *image;
1805 if (file == NULL || file[0] == '\0')
1806 return NULL;
1807 if ((image = iopen(file, "r")) == NULL)
1808 return NULL;
1809 if (image->colormap != CM_NORMAL) {
1810 iclose(image);
1811 return NULL;
1812 }
1813
1814 xsize = image->xsize;
1815 ysize = image->ysize;
1816 zsize = image->zsize;
1817
1818 // allocate needed memory
1819 short *rbuf, *gbuf, *bbuf;
1820 char *buf = new char[image->xsize * image->ysize * 3];
1821 rbuf = new short[image->xsize];
1822 if (image->zsize > 2) {
1823 gbuf = new short[image->xsize];
1824 bbuf = new short[image->xsize];
1825 }
1826
1827 // read image in, one row at a time
1828 char *p = buf;
1829 for (int row = 0; row < image->ysize; row++) {
1830
1831 if (image->zsize > 2) {
1832 getrow(image, rbuf, row, 0);
1833 getrow(image, gbuf, row, 1);
1834 getrow(image, bbuf, row, 2);
1835 }
1836 else
1837 getrow(image, rbuf, row, 0);
1838
1839 // store these into an unsigned byte RGB format
1840 for (int i=0; i < image->xsize; i++) {
1841 if (image->zsize > 2) {
1842 *p++ = rbuf[i];
1843 *p++ = gbuf[i];
1844 *p++ = bbuf[i];
1845 }
1846 else {
1847 *p++ = rbuf[i];
1848 *p++ = rbuf[i];
1849 *p++ = rbuf[i];
1850 }
1851 }
1852 }
1853
1854 // delete image buffers
1855 delete [] rbuf;
1856 if (zsize > 2) {
1857 delete [] gbuf;
1858 delete [] bbuf;
1859 }
1860
1861 iclose(image);
1862 return buf;
1863 }
1864
1865 ////////////////////////////////////////////////////////////////////////
1866 //
1867 // Description:
1868 // switches to the given palette number
1869 //
1870 // Use: private
1871 void
1872 MyTextureEditor::switchPalette(int id)
1873 //
1874 ////////////////////////////////////////////////////////////////////////
1875 {
1876 curPalette = id;
1877
1878 // get new textures, update material and selection
1879 loadPaletteItems();
1880 deselectCurrentItem(FALSE); // don't redraw palette changes there
1881 redrawPalette();
1882 updateWindowTitle();
1883 updateFileMenu();
1884 }
1885
1886 ////////////////////////////////////////////////////////////////////////
1887 //
1888 // Description:
1889 // deselect the currently selected item in the palette.
1890 //
1891 // Use: private
1892 void
1893 MyTextureEditor::deselectCurrentItem(SbBool drawHighlight)
1894 //
1895 ////////////////////////////////////////////////////////////////////////
1896 {
1897 if (selectedItem == -1)
1898 return;
1899
1900 // deselect the current item and update things that depend on it
1901 if (drawHighlight) {
1902 glXMakeCurrent(getDisplay(), XtWindow(widgetList[TEXTURE_GLX]), paletteCtx);
1903 if (selectedItem == currentItem)
1904 drawTextureTile(selectedItem, DRAW_CURRENT);
1905 else {
1906 drawTextureTile(selectedItem, DRAW_NORM);
1907 if (currentItem != -1)
1908 drawTextureTile(currentItem, DRAW_CURRENT);
1909 }
1910 }
1911 selectedItem = -1;
1912 updateTextureName();
1913
1914 // hide color wheel
1915 colSlider->hide();
1916 colWheel->hide();
1917 }
1918
1919 ////////////////////////////////////////////////////////////////////////
1920 //
1921 // Description:
1922 // deletes the names + images of the given texture.
1923 //
1924 // NOTE: we don't ever delete the iconImage buffer since those
1925 // will always be IMAGE_SIZE X IMAGE_SIZE
1926 //
1927 // Use: private
1928 void
1929 MyTextureEditor::deleteTextureEntry(int id)
1930 //
1931 ////////////////////////////////////////////////////////////////////////
1932 {
1933 if (textureNames[id].name != NULL)
1934 free(textureNames[id].name);
1935 if (textureNames[id].fullName != NULL)
1936 free(textureNames[id].fullName);
1937
1938 textureNames[id].name = textureNames[id].fullName = NULL;
1939 }
1940
1941 ////////////////////////////////////////////////////////////////////////
1942 //
1943 // Description:
1944 // adds a texture entry (name + fullName + load small size image)
1945 // at the given index. This returns TRUE if this routine was successfull
1946 // (valid image file).
1947 //
1948 // Use: private
1949 SbBool
1950 MyTextureEditor::addTextureEntry(int id, char *fullName)
1951 //
1952 ////////////////////////////////////////////////////////////////////////
1953 {
1954 // read the image in iconized form
1955 if (! readScaledImage(fullName, IMAGE_SIZE, IMAGE_SIZE,
1956 textureNames[id].iconImage, textureNames[id].zsize))
1957 return FALSE;
1958
1959 // extract the file name (text after the last '/' char)
1960 deleteTextureEntry(id);
1961 char *p = strrchr(fullName, '/');
1962 p = (p != NULL) ? p+1 : fullName;
1963 textureNames[id].name = strdup(p);
1964 textureNames[id].fullName = strdup(fullName);
1965
1966 return TRUE;
1967 }
1968
1969 ////////////////////////////////////////////////////////////////////////
1970 //
1971 // Description:
1972 // sets the window title based on the current palette and save status
1973 //
1974 // Use: private
1975 void
1976 MyTextureEditor::updateWindowTitle()
1977 //
1978 ////////////////////////////////////////////////////////////////////////
1979 {
1980 char str[150];
1981 sprintf(str, "%s: %s", editorTitle, ((PaletteStruct *)paletteList[curPalette])->name);
1982 setTitle(str);
1983 }
1984
1985 ////////////////////////////////////////////////////////////////////////
1986 //
1987 // Description:
1988 // Called whenever an event occurs in the texture palette window.
1989 //
1990 // Use: private
1991 void
1992 MyTextureEditor::handleEvent(XAnyEvent *xe)
1993 //
1994 ////////////////////////////////////////////////////////////////////////
1995 {
1996 switch(xe->type) {
1997 case ButtonPress:
1998 {
1999 XButtonEvent *be = (XButtonEvent *)xe;
2000 if (be->button != Button1)
2001 break;
2002
2003 //
2004 // check for double click. If the time difference between
2005 // successive mouse down is less than 3/10th sec, then
2006 // consider it a double click.
2007 // ??? do we need to worry about time warping ?
2008 //
2009 if ((be->time - prevTime) < DOUBLE_CLICK_TIME) {
2010
2011 // open the image dialog
2012 openImageDialog();
2013
2014 // if we don't have any images there, automatically open
2015 // the file browser
2016 // ??? make it look like the "Open..." button was pressed
2017 // if (textureNames[selectedItem].fullName == NULL)
2018 // imageDialogOpenCB(NULL, this, NULL);
2019 }
2020 else { // single click (or first click of the double click)
2021 // select the current texture (if it is not currently selected)
2022 if (currentItem != selectedItem) {
2023
2024 // update the feedback
2025 glXMakeCurrent(getDisplay(), XtWindow(widgetList[TEXTURE_GLX]), paletteCtx);
2026 drawTextureTile(selectedItem, DRAW_NORM);
2027 drawTextureTile(currentItem, DRAW_SELECTED);
2028 selectedItem = currentItem;
2029
2030 updateTextureNode();
2031
2032 // ??? update the image dialog (if visible)
2033 if (widgetList[DIALOG_WINDOW] != NULL)
2034 setNewDialogImage(textureNames[selectedItem].fullName);
2035
2036 // show/hide the color wheel
2037 if (textureNames[selectedItem].zsize < 3 &&
2038 textureNames[selectedItem].fullName != NULL) {
2039 colWheel->show();
2040 colSlider->show();
2041 }
2042 else {
2043 colSlider->hide();
2044 colWheel->hide();
2045 }
2046 }
2047 }
2048
2049 prevTime = be->time;
2050 }
2051 break;
2052
2053 case MotionNotify:
2054 {
2055 // do a region picking to figure which tile we are above
2056 XMotionEvent *me = (XMotionEvent *)xe;
2057 int xpos = int (floorf( IMAGE_NUM * me->x / float(GLX_SIZE) ));
2058 int ypos = int (floorf( IMAGE_NUM * me->y / float(GLX_SIZE) ));
2059 int whichTexture = xpos + IMAGE_NUM * ypos;
2060 if (whichTexture != currentItem) {
2061 glXMakeCurrent(getDisplay(), XtWindow(widgetList[TEXTURE_GLX]), paletteCtx);
2062 if (currentItem != selectedItem &&
2063 currentItem != -1)
2064 drawTextureTile(currentItem, DRAW_NORM);
2065 if (whichTexture != selectedItem)
2066 drawTextureTile(whichTexture, DRAW_CURRENT);
2067 drawTextureTile(selectedItem, DRAW_SELECTED);
2068 currentItem = whichTexture;
2069 updateTextureName();
2070 }
2071 }
2072 break;
2073
2074 case LeaveNotify:
2075 if (currentItem != selectedItem) {
2076 glXMakeCurrent(getDisplay(), XtWindow(widgetList[TEXTURE_GLX]), paletteCtx);
2077 drawTextureTile(currentItem, DRAW_NORM);
2078 drawTextureTile(selectedItem, DRAW_SELECTED);
2079 currentItem = -1;
2080 updateTextureName();
2081 }
2082 break;
2083 }
2084 }
2085
2086 ////////////////////////////////////////////////////////////////////////
2087 //
2088 // Description:
2089 // creates a dialog which lets the user type a name.
2090 //
2091 // Use: private
2092 void
2093 MyTextureEditor::createNewDialog()
2094 //
2095 ////////////////////////////////////////////////////////////////////////
2096 {
2097 Arg args[5];
2098 XmString xmstr = XmStringCreateSimple("New Palette Name:");
2099
2100 int n = 0;
2101 XtSetArg(args[n], XmNautoUnmanage, FALSE); n++;
2102 XtSetArg(args[n], XtNtitle, "New Palette Dialog"); n++;
2103 XtSetArg(args[n], XmNselectionLabelString, xmstr); n++;
2104 Widget dialog = XmCreatePromptDialog(SoXt::getShellWidget(getWidget()),
2105 "saveDialog", args, n);
2106 XmStringFree(xmstr);
2107
2108 XtUnmanageChild(XmSelectionBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
2109 XtUnmanageChild(XmSelectionBoxGetChild(dialog, XmDIALOG_SEPARATOR));
2110
2111 // register callback to destroy (and not just unmap) the dialog
2112 // and retreive the text (ok push button case).
2113 XtAddCallback(dialog, XmNokCallback,
2114 (XtCallbackProc) MyTextureEditor::newDialogCB, (XtPointer) this);
2115 XtAddCallback(dialog, XmNcancelCallback,
2116 (XtCallbackProc) MyTextureEditor::newDialogCB, (XtPointer) this);
2117
2118 XtManageChild(dialog);
2119 }
2120
2121 ////////////////////////////////////////////////////////////////////////
2122 //
2123 // Description:
2124 // create a dialog which lets the user change it's mind about deleting
2125 // the current palette.
2126 //
2127 // Use: private
2128 void
2129 MyTextureEditor::createDeleteDialog(char *title, char *str1, char *str2)
2130 //
2131 ////////////////////////////////////////////////////////////////////////
2132 {
2133 Arg args[5];
2134 XmString xmstr = XmStringCreateSimple(str1);
2135 xmstr = XmStringConcat(xmstr, XmStringSeparatorCreate());
2136 xmstr = XmStringConcat(xmstr, XmStringCreateSimple(str2));
2137
2138 int n = 0;
2139 XtSetArg(args[n], XmNautoUnmanage, FALSE); n++;
2140 XtSetArg(args[n], XtNtitle, title); n++;
2141 XtSetArg(args[n], XmNmessageString, xmstr); n++;
2142 Widget dialog = XmCreateWarningDialog(SoXt::getShellWidget(getWidget()),
2143 "DeleteDialog", args, n);
2144 XmStringFree(xmstr);
2145
2146 XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
2147 XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_SEPARATOR));
2148
2149 // register callback to destroy (and not just unmap) the dialog
2150 XtAddCallback(dialog, XmNokCallback,
2151 (XtCallbackProc) MyTextureEditor::deleteDialogCB, (XtPointer) this);
2152 XtAddCallback(dialog, XmNcancelCallback,
2153 (XtCallbackProc) MyTextureEditor::deleteDialogCB, (XtPointer) this);
2154
2155 XtManageChild(dialog);
2156 }
2157
2158 ////////////////////////////////////////////////////////////////////////
2159 //
2160 // Description:
2161 // updates the "File" menu (grey things out) to reflect current
2162 // palette.
2163 //
2164 // Use: private
2165 void
2166 MyTextureEditor::updateFileMenu()
2167 //
2168 ////////////////////////////////////////////////////////////////////////
2169 {
2170 PaletteStruct *pal = (PaletteStruct *) paletteList[curPalette];
2171
2172 XtVaSetValues(widgetList[FILE_RESET], XmNsensitive,
2173 pal->user && pal->system, NULL);
2174 XtVaSetValues(widgetList[FILE_DELETE], XmNsensitive,
2175 pal->user && !pal->system, NULL);
2176 }
2177
2178 ////////////////////////////////////////////////////////////////////////
2179 //
2180 // Description:
2181 // This creates a new palette with the given name (called when the
2182 // user wants a new palette using the 'new' button).
2183 //
2184 // Use: private
2185 void
2186 MyTextureEditor::createNewPalette(char *palName)
2187 //
2188 ////////////////////////////////////////////////////////////////////////
2189 {
2190 //
2191 // create that empty palette file (under the user's home
2192 // directory).
2193 //
2194 char dirName[MAXPATHLEN];
2195 char fileName[MAXPATHLEN];
2196 struct stat buf;
2197 FILE *file;
2198 sprintf(dirName, "%s/%s/", getenv("HOME"), customTextureDir);
2199 if (stat(dirName, &buf) != 0)
2200 mkdir(dirName, 0x1ff);
2201 strcpy(fileName, dirName);
2202 strcat(fileName, palName);
2203 if ((file = fopen(fileName, "w")) == NULL) {
2204 #ifdef DEBUG
2205 SoDebugError::post("MyTextureEditor::createNewPalette",
2206 "couldn't create file: %s", fileName);
2207 #endif
2208 return;
2209 }
2210 fclose(file);
2211
2212 //
2213 // add the palette to the popup menu
2214 //
2215 PaletteStruct *pal = new PaletteStruct;
2216 pal->name = strdup(palName);
2217 pal->user = TRUE;
2218 pal->system = FALSE;
2219 paletteList.append(pal);
2220 int id = paletteList.getLength() - 1;
2221 XtManageChild(buildPaletteMenuEntry(id));
2222
2223 // now switch to new palette
2224 switchPalette(id);
2225 }
2226
2227 ////////////////////////////////////////////////////////////////////////
2228 //
2229 // Description:
2230 // updates the image dialog based on the currently selected texture.
2231 //
2232 // Use: private
2233 void
2234 MyTextureEditor::redrawImageDialog()
2235 //
2236 ////////////////////////////////////////////////////////////////////////
2237 {
2238 // make sure window is on the screen
2239 Widget glx = widgetList[DIALOG_IMAGE];
2240 if (glx == NULL)
2241 return;
2242 Window window = XtWindow(glx);
2243 if (window == 0)
2244 return;
2245
2246 glXMakeCurrent(XtDisplay(glx), window, imageDialogCtx);
2247
2248 // reset projection
2249 short w, h;
2250 XtVaGetValues(glx, XmNwidth, &w, XmNheight, &h, NULL);
2251 glViewport(0, 0, w, h);
2252 glMatrixMode(GL_PROJECTION);
2253 glLoadIdentity();
2254 glOrtho(0, w, 0, h, -1, 1);
2255
2256 // draw image
2257 if (dialogImage != NULL) {
2258 glRasterPos2i(0, 0);
2259 glDrawPixels(dialogImageSize[0], dialogImageSize[1], GL_RGB,
2260 GL_UNSIGNED_BYTE, dialogImage);
2261 }
2262 else {
2263 glClearColor(.6, .6, .6, 0);
2264 glClear(GL_COLOR_BUFFER_BIT);
2265 }
2266
2267 glFlush();
2268 }
2269
2270 ////////////////////////////////////////////////////////////////////////
2271 //
2272 // Description:
2273 // sets which image should be displayed (NULL for clear) in the
2274 // image dialog.
2275 //
2276 // Use: private
2277 void
2278 MyTextureEditor::setNewDialogImage(char *fileName)
2279 //
2280 ////////////////////////////////////////////////////////////////////////
2281 {
2282 // make sure widget is built
2283 if (widgetList[DIALOG_WINDOW] == NULL)
2284 return;
2285
2286 // delete the existing dialog image and names
2287 delete dialogImage;
2288 if (dialogImageName != NULL)
2289 free(dialogImageName);
2290 if (dialogImageInfo != NULL)
2291 free(dialogImageInfo);
2292 dialogImage = NULL;
2293 dialogImageName = NULL;
2294 dialogImageInfo = NULL;
2295
2296 if (fileName != NULL) {
2297
2298 // read the full image in and update the picture
2299 dialogImage = readImage(fileName,
2300 dialogImageSize[0], dialogImageSize[1], dialogImageSize[2]);
2301 if (dialogImage != NULL) {
2302
2303 // resize the glx window
2304 XtVaSetValues(widgetList[DIALOG_IMAGE], XmNwidth, dialogImageSize[0],
2305 XmNheight, dialogImageSize[1], NULL);
2306
2307 // save the texture name
2308 dialogImageName = strdup(fileName);
2309
2310 // format the image info
2311 char str[100];
2312 sprintf(str, "%d x %d %d component", dialogImageSize[0],
2313 dialogImageSize[1], dialogImageSize[2]);
2314 if (dialogImageSize[2] > 1)
2315 strcat(str, "s");
2316 dialogImageInfo = strdup(str);
2317 }
2318 else {
2319
2320 // bogus file name was given. print an error dialog
2321 char str[MAXPATHLEN+100];
2322 sprintf(str, "Error opening image file: %s", fileName);
2323 SoXt::createSimpleErrorDialog(widgetList[DIALOG_WINDOW], "File Error", str);
2324 }
2325 }
2326
2327 if (dialogImageName == NULL)
2328 // no images so make the glx tiny size (non zero)
2329 XtVaSetValues(widgetList[DIALOG_IMAGE], XmNwidth, 1, XmNheight, 1, NULL);
2330
2331 // update the name label
2332 XmString xmstr = (dialogImageName != NULL) ?
2333 XmStringCreateSimple(dialogImageName) : XmStringCreateSimple(noFileNameStr);
2334 XtVaSetValues(widgetList[DIALOG_NAME], XmNlabelString, xmstr, NULL);
2335 XmStringFree(xmstr);
2336
2337 // update the info label
2338 xmstr = (dialogImageInfo != NULL) ? XmStringCreateSimple(dialogImageInfo) :
2339 XmStringCreateSimple("");
2340 XtVaSetValues(widgetList[DIALOG_INFO], XmNlabelString, xmstr, NULL);
2341 XmStringFree(xmstr);
2342
2343 //
2344 // center everything in the window
2345 //
2346
2347 // get the max size of all the pieces
2348 short width[4];
2349 int maxWidth;
2350 width[0] = buttonsTotalWidth; // constant size
2351 XtVaGetValues(widgetList[DIALOG_INFO], XmNwidth, &width[1], NULL);
2352 XtVaGetValues(widgetList[DIALOG_NAME], XmNwidth, &width[2], NULL);
2353 XtVaGetValues(widgetList[DIALOG_IMAGE], XmNwidth, &width[3], NULL);
2354 maxWidth = width[0];
2355 for (int i=1; i<4; i++)
2356 if (width[i] > maxWidth)
2357 maxWidth = width[i];
2358
2359 // now center things
2360 XtVaSetValues(widgetList[DIALOG_BUTTON_0], XmNleftOffset,
2361 (maxWidth - width[0]) / 2, NULL);
2362 XtVaSetValues(widgetList[DIALOG_INFO], XmNleftOffset,
2363 (maxWidth - width[1]) / 2, NULL);
2364 XtVaSetValues(widgetList[DIALOG_NAME], XmNleftOffset,
2365 (maxWidth - width[2]) / 2, NULL);
2366 XtVaSetValues(widgetList[DIALOG_IMAGE], XmNleftOffset,
2367 (maxWidth - width[3]) / 2, NULL);
2368
2369 // ??? redraw the image now (instead of waiting for an expose event)
2370 // to minimize the weird display when resizing a window.
2371 redrawImageDialog();
2372 }
2373
2374 ////////////////////////////////////////////////////////////////////////
2375 //
2376 // Description:
2377 // Builds the image dialog window
2378 //
2379 // Use: private
2380 void
2381 MyTextureEditor::openImageDialog()
2382 //
2383 ////////////////////////////////////////////////////////////////////////
2384 {
2385 // check to see if dialog has already been built
2386 Widget shell = widgetList[DIALOG_WINDOW];
2387 if (shell != NULL) {
2388 SoXt::show(shell);
2389 return;
2390 }
2391
2392 int i, n;
2393 Arg args[12];
2394 Widget buttons[4], labels[2], glx;
2395
2396 //
2397 // create the topLevel Shell window
2398 //
2399 n = 0;
2400 XtSetArg(args[n], XtNtitle, "Texture Image Dialog"); n++;
2401 XtSetArg(args[n], XmNiconName, "Image Dialog"); n++;
2402 XtSetArg(args[n], XmNallowShellResize, TRUE); n++;
2403 shell = XtCreatePopupShell("SoXtImageDialog", topLevelShellWidgetClass,
2404 SoXt::getShellWidget(getWidget()), args, n);
2405
2406 widgetList[DIALOG_WINDOW] = shell;
2407 XtAddCallback(shell, XmNdestroyCallback,
2408 (XtCallbackProc) MyTextureEditor::imageDialogDestroyCB,
2409 (XtPointer) this);
2410
2411 // create a top level form to hold everything together
2412 n = 0;
2413 XtSetArg(args[n], XmNmarginHeight, 10); n++;
2414 XtSetArg(args[n], XmNmarginWidth, 10); n++;
2415 Widget dummyForm = XmCreateForm(shell, "dummyForm", args, n);
2416 Widget form = XmCreateForm(dummyForm, "imageDialogForm", NULL, 0);
2417
2418 //
2419 // create all the parts
2420 //
2421
2422 // create the push buttons
2423 n = 0;
2424 XtSetArg(args[n], XmNhighlightThickness, 0); n++;
2425 buttons[0] = XmCreatePushButtonGadget(form, "Open...", args, n);
2426 buttons[1] = XmCreatePushButtonGadget(form, "Clear", args, n);
2427 buttons[2] = XmCreatePushButtonGadget(form, "Apply", args, n);
2428 buttons[3] = XmCreatePushButtonGadget(form, "CloseWin", args, n);
2429 widgetList[DIALOG_BUTTON_0] = buttons[0];
2430
2431 // make them all the same size
2432 short w; int width = 0;
2433 for (i=0; i<4; i++) {
2434 XtVaGetValues(buttons[i], XtNwidth, &w, NULL);
2435 if (w > width) width = w;
2436 }
2437 for (i=0; i<4; i++)
2438 XtVaSetValues(buttons[i], XtNwidth, width, NULL);
2439 buttonsTotalWidth = 4 * width + 3 * 5; // see layout spacing
2440
2441 XtAddCallback(buttons[0], XmNactivateCallback,
2442 (XtCallbackProc) MyTextureEditor::imageDialogOpenCB, (XtPointer) this);
2443 XtAddCallback(buttons[1], XmNactivateCallback,
2444 (XtCallbackProc) MyTextureEditor::imageDialogClearCB, (XtPointer) this);
2445 XtAddCallback(buttons[2], XmNactivateCallback,
2446 (XtCallbackProc) MyTextureEditor::imageDialogApplyCB, (XtPointer) this);
2447 XtAddCallback(buttons[3], XmNactivateCallback,
2448 (XtCallbackProc) MyTextureEditor::imageDialogCloseCB, (XtPointer) this);
2449
2450 // create the image name label
2451 widgetList[DIALOG_INFO] = labels[0] = XmCreateLabelGadget(form, "imageInfo", NULL, 0);
2452 widgetList[DIALOG_NAME] = labels[1] = XmCreateLabelGadget(form, "imageName", NULL, 0);
2453
2454 // create the image glx window
2455 n = 0;
2456 XtSetArg(args[n], GLwNrgba, TRUE); n++;
2457 XtSetArg(args[n], GLwNredSize, 1); n++;
2458 XtSetArg(args[n], GLwNgreenSize, 1); n++;
2459 XtSetArg(args[n], GLwNblueSize, 1); n++;
2460 glx = XtCreateWidget("imageDialogGLX", glwMDrawingAreaWidgetClass, form, args, n);
2461 widgetList[DIALOG_IMAGE] = glx;
2462
2463 XtUninstallTranslations(glx);
2464 XtAddCallback(glx, GLwNginitCallback,
2465 (XtCallbackProc) MyTextureEditor::imageDialogInitCB, (XtPointer) this);
2466 XtAddCallback(glx, GLwNexposeCallback,
2467 (XtCallbackProc) MyTextureEditor::imageDialogExposeCB, (XtPointer) this);
2468
2469 //
2470 // layout !
2471 //
2472 n = 0;
2473 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
2474
2475 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM);
2476 XtSetValues(buttons[0], args, n+1);
2477 XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET);
2478 XtSetArg(args[n+1], XmNleftOffset, 5);
2479 for (i=1; i<4; i++) {
2480 XtSetArg(args[n+2], XmNleftWidget, buttons[i-1]);
2481 XtSetValues(buttons[i], args, n+3);
2482 }
2483
2484 n = 0;
2485 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
2486 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
2487
2488 XtSetArg(args[n], XmNbottomWidget, buttons[0]);
2489 XtSetArg(args[n+1], XmNbottomOffset, 10);
2490 XtSetValues(labels[0], args, n+2);
2491 XtSetArg(args[n], XmNbottomWidget, labels[0]);
2492 XtSetArg(args[n+1], XmNbottomOffset, 7);
2493 XtSetValues(labels[1], args, n+2);
2494
2495 n = 0;
2496 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
2497 XtSetArg(args[n], XmNbottomWidget, labels[1]); n++;
2498 XtSetArg(args[n], XmNbottomOffset, 10); n++;
2499 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
2500 XtSetValues(glx, args, n);
2501
2502 n = 0;
2503 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
2504 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
2505 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
2506 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
2507 XtSetValues(form, args, n);
2508
2509 setNewDialogImage(textureNames[selectedItem].fullName);
2510
2511 // manage children
2512 XtManageChildren(buttons, 4);
2513 XtManageChildren(labels, 2);
2514 XtManageChild(glx);
2515 XtManageChild(form);
2516 XtManageChild(dummyForm);
2517 SoXt::show(shell);
2518 }
2519
2520
2521
2522
2523 //
2524 // redefine those generic virtual functions
2525 //
2526 const char *
2527 MyTextureEditor::getDefaultWidgetName() const
2528 { return "MyTextureEditor"; }
2529
2530 const char *
2531 MyTextureEditor::getDefaultTitle() const
2532 { return "Texture Editor"; }
2533
2534 const char *
2535 MyTextureEditor::getDefaultIconTitle() const
2536 { return "Texture Editor"; }
2537
2538
2539
2540
2541 //
2542 ////////////////////////////////////////////////////////////////////////
2543 // static callbacks stubs
2544 ////////////////////////////////////////////////////////////////////////
2545 //
2546
2547
2548 void
2549 MyTextureEditor::fieldChangedCB(Widget, MyTextureEditor *p, void *)
2550 { p->fieldChanged = TRUE; }
2551
2552 void
2553 MyTextureEditor::glxExposeCB(Widget, MyTextureEditor *p, void *)
2554 {
2555 if (p->loadedPalette)
2556 p->redrawPalette();
2557 else
2558 p->loadPaletteItems(); // this will also redraw the tiles
2559 }
2560
2561 void
2562 MyTextureEditor::glxInitCB(Widget glx, MyTextureEditor *p, void *)
2563 {
2564 // create a GLX context
2565 XVisualInfo *vis;
2566 XtVaGetValues(glx, GLwNvisualInfo, &vis, NULL);
2567 p->paletteCtx = glXCreateContext(XtDisplay(glx), vis, NULL, GL_TRUE);
2568
2569 glXMakeCurrent(XtDisplay(glx), XtWindow(glx), p->paletteCtx);
2570
2571 // set the projection
2572 short w, h;
2573 XtVaGetValues(glx, XmNwidth, &w, XmNheight, &h, NULL);
2574 glViewport(0, 0, w, h);
2575 glMatrixMode(GL_PROJECTION);
2576 glLoadIdentity();
2577 glOrtho(0, w, 0, h, -1, 1);
2578
2579 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2580 }
2581
2582 void
2583 MyTextureEditor::glxEventCB(Widget, MyTextureEditor *p, XAnyEvent *xe, Boolean *)
2584 { p->handleEvent(xe); }
2585
2586 void
2587 MyTextureEditor::imageDialogExposeCB(Widget, MyTextureEditor *p, void *)
2588 { p->redrawImageDialog(); }
2589
2590 void
2591 MyTextureEditor::imageDialogInitCB(Widget glx, MyTextureEditor *p, void *)
2592 {
2593 // create a GLX context
2594 XVisualInfo *vis;
2595 XtVaGetValues(glx, GLwNvisualInfo, &vis, NULL);
2596 p->imageDialogCtx = glXCreateContext(XtDisplay(glx), vis, NULL, GL_TRUE);
2597
2598 glXMakeCurrent(XtDisplay(glx), XtWindow(glx), p->imageDialogCtx);
2599 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2600 }
2601
2602 void
2603 MyTextureEditor::imageDialogClearCB(Widget, MyTextureEditor *p, void *)
2604 { p->setNewDialogImage(NULL); }
2605
2606 void
2607 MyTextureEditor::imageDialogCloseCB(Widget, MyTextureEditor *p, void *)
2608 { SoXt::hide(p->widgetList[DIALOG_WINDOW]); }
2609
2610 void
2611 MyTextureEditor::acceptCB(Widget, MyTextureEditor *p, void *)
2612 { p->callbackList.invokeCallbacks(p); }
2613
2614 //
2615 // called by the color wheel when the color changes
2616 //
2617 void
2618 MyTextureEditor::colWheelCB(void *pt, const float hsv[3])
2619 {
2620 MyTextureEditor *p = (MyTextureEditor *)pt;
2621 if (p->ignoreCallback)
2622 return;
2623
2624 p->ignoreCallback = TRUE;
2625 p->colSlider->setBaseColor(hsv);
2626 p->ignoreCallback = FALSE;
2627
2628 // update the texture
2629 SbColor rgb;
2630 rgb.setHSVValue(hsv);
2631 p->texNode->blendColor = rgb;
2632 }
2633
2634 //
2635 // called by color slider when the color changes
2636 //
2637 void
2638 MyTextureEditor::colSliderCB(void *pt, float)
2639 {
2640 MyTextureEditor *p = (MyTextureEditor *)pt;
2641 if (p->ignoreCallback)
2642 return;
2643
2644 const float *hsv = p->colSlider->getBaseColor();
2645 p->ignoreCallback = TRUE;
2646 p->colWheel->setBaseColor(hsv);
2647 p->ignoreCallback = FALSE;
2648
2649 // update the texture
2650 SbColor rgb;
2651 rgb.setHSVValue(hsv);
2652 p->texNode->blendColor = rgb;
2653 }
2654
2655 //
2656 // called whenever the scale X thumbwheel changes
2657 //
2658 void
2659 MyTextureEditor::scaleXThumbCB(void *pt, float val)
2660 {
2661 MyTextureEditor *p = (MyTextureEditor *)pt;
2662
2663 SbVec2f scale = p->texXfNode->scaleFactor.getValue();
2664
2665 // shorter/grow the scale value
2666 if (p->repeatState)
2667 scale[0] *= pow(1.5, val - p->oldXThumbVal);
2668 else
2669 scale[0] /= pow(1.5, val - p->oldXThumbVal);
2670 p->texXfNode->scaleFactor.setValue(scale);
2671 p->oldXThumbVal = val;
2672
2673 p->updateTextureFieldAndSlider(SCALE_X_FIELD);
2674 }
2675
2676 //
2677 // called whenever the scale Y thumbwheel changes
2678 //
2679 void
2680 MyTextureEditor::scaleYThumbCB(void *pt, float val)
2681 {
2682 MyTextureEditor *p = (MyTextureEditor *)pt;
2683
2684 SbVec2f scale = p->texXfNode->scaleFactor.getValue();
2685
2686 // shorter/grow the scale value
2687 if (p->repeatState)
2688 scale[1] *= pow(1.5, val - p->oldYThumbVal);
2689 else
2690 scale[1] /= pow(1.5, val - p->oldYThumbVal);
2691 p->texXfNode->scaleFactor.setValue(scale);
2692 p->oldYThumbVal = val;
2693
2694 p->updateTextureFieldAndSlider(SCALE_Y_FIELD);
2695 }
2696
2697 //
2698 // called whenever the slider's text fields changes
2699 //
2700 void
2701 MyTextureEditor::fieldsCB(Widget w, int id, void *)
2702 {
2703 // get the class pointer
2704 MyTextureEditor *p;
2705 XtVaGetValues(w, XmNuserData, &p, NULL);
2706
2707 if (! p->fieldChanged)
2708 return;
2709 p->fieldChanged = FALSE;
2710
2711 // get the text field value and update the texture node
2712 char *str = XmTextGetString(w);
2713 float val;
2714 SbVec2f trans, scale, cent;
2715 if (sscanf(str, "%f", &val)) {
2716 switch(id) {
2717 case SCALE_X_FIELD:
2718 // make it look like we are scaling the image, not the texture coord
2719 scale = p->texXfNode->scaleFactor.getValue();
2720 if (p->repeatState)
2721 scale[0] = val;
2722 else
2723 scale[0] = 1 / val;
2724 p->texXfNode->scaleFactor.setValue(scale);
2725 break;
2726
2727 case SCALE_Y_FIELD:
2728 // make it look like we are scaling the image, not the texture coord
2729 scale = p->texXfNode->scaleFactor.getValue();
2730 if (p->repeatState)
2731 scale[1] = val;
2732 else
2733 scale[1] = 1 / val;
2734 p->texXfNode->scaleFactor.setValue(scale);
2735 break;
2736
2737 case TRANS_X_FIELD:
2738 trans = p->texXfNode->translation.getValue();
2739 trans[0] = val;
2740 p->texXfNode->translation.setValue(trans);
2741
2742 // update to rotate around center of image
2743 cent = p->texXfNode->center.getValue();
2744 cent[0] = 0.5 - trans[0];
2745 p->texXfNode->center.setValue(cent);
2746 break;
2747
2748 case TRANS_Y_FIELD:
2749 trans = p->texXfNode->translation.getValue();
2750 trans[1] = val;
2751 p->texXfNode->translation.setValue(trans);
2752
2753 // update to rotate around center of image
2754 cent = p->texXfNode->center.getValue();
2755 cent[1] = 0.5 - trans[1];
2756 p->texXfNode->center.setValue(cent);
2757 break;
2758
2759 case ROT_FIELD:
2760 while (val < 0) val += 360;
2761 while (val > 360) val -= 360;
2762 p->texXfNode->rotation = val * M_PI / 180.0;
2763 break;
2764 }
2765 }
2766 XtFree(str);
2767
2768 // reformat the text field and update the slider
2769 p->updateTextureFieldAndSlider(id);
2770
2771 // make the text field loose the focus
2772 XmProcessTraversal(SoXt::getShellWidget(p->getWidget()), XmTRAVERSE_CURRENT);
2773 }
2774
2775 //
2776 // called whenever the motif sliders changes
2777 //
2778 void
2779 MyTextureEditor::slidersCB(Widget w, int id, void *)
2780 {
2781 // get the class pointer
2782 MyTextureEditor *p;
2783 XtVaGetValues(w, XmNuserData, &p, NULL);
2784
2785 // get the slider value
2786 int intVal;
2787 XmScaleGetValue(w, &intVal);
2788 float val = intVal / 100.0;
2789
2790 char str[10];
2791 SbVec2f trans, cent;
2792
2793 switch(id) {
2794 case TRANS_X_SLD:
2795 val = 1 - 2 * val; // make inverted [-1,1]
2796 trans = p->texXfNode->translation.getValue();
2797 trans[0] = val;
2798 p->texXfNode->translation.setValue(trans);
2799
2800 // update to rotate around center of image
2801 cent = p->texXfNode->center.getValue();
2802 cent[0] = 0.5 - trans[0];
2803 p->texXfNode->center.setValue(cent);
2804
2805 sprintf(str, "%.2f", val);
2806 XmTextSetString(p->widgetList[TRANS_X_FIELD], str);
2807 break;
2808
2809 case TRANS_Y_SLD:
2810 val = 1 - 2 * val; // make inverted [-1,1]
2811 trans = p->texXfNode->translation.getValue();
2812 trans[1] = val;
2813 p->texXfNode->translation.setValue(trans);
2814
2815 // update to rotate around center of image
2816 cent = p->texXfNode->center.getValue();
2817 cent[1] = 0.5 - trans[1];
2818 p->texXfNode->center.setValue(cent);
2819
2820 sprintf(str, "%.2f", val);
2821 XmTextSetString(p->widgetList[TRANS_Y_FIELD], str);
2822 break;
2823
2824 case ROT_SLD:
2825 p->texXfNode->rotation = val * 2 * M_PI;
2826 intVal = int(val * 360);
2827 sprintf(str, "%d", intVal);
2828 XmTextSetString(p->widgetList[ROT_FIELD], str);
2829 break;
2830 }
2831 }
2832
2833 //
2834 // called whenever an entry within the mapping menu gets selected
2835 //
2836 void
2837 MyTextureEditor::mappingMenuCB(Widget w, int id, void *)
2838 {
2839 // get the class pointer
2840 MyTextureEditor *p;
2841 XtVaGetValues(w, XmNuserData, &p, NULL);
2842
2843 SoTextureCoordinateFunction *newFunc;
2844
2845 switch(id) {
2846 case MAP_DEFAULT:
2847 newFunc = new SoTextureCoordinateDefault;
2848 break;
2849
2850 case MAP_ENV:
2851 newFunc = new SoTextureCoordinateEnvironment;
2852 break;
2853
2854 case MAP_PLANE_XY:
2855 newFunc = new SoTextureCoordinatePlane;
2856 ((SoTextureCoordinatePlane *)newFunc)->directionS = SbVec3f(1, 0, 0);
2857 ((SoTextureCoordinatePlane *)newFunc)->directionT = SbVec3f(0, 1, 0);
2858 break;
2859
2860 case MAP_PLANE_XZ:
2861 newFunc = new SoTextureCoordinatePlane;
2862 ((SoTextureCoordinatePlane *)newFunc)->directionS = SbVec3f(1, 0, 0);
2863 ((SoTextureCoordinatePlane *)newFunc)->directionT = SbVec3f(0, 0, 1);
2864 break;
2865
2866 case MAP_PLANE_YZ:
2867 newFunc = new SoTextureCoordinatePlane;
2868 ((SoTextureCoordinatePlane *)newFunc)->directionS = SbVec3f(0, 1, 0);
2869 ((SoTextureCoordinatePlane *)newFunc)->directionT = SbVec3f(0, 0, 1);
2870 break;
2871
2872 case MAP_UNKNOWN:
2873 // this should never happen !
2874 #ifdef DEBUG
2875 SoDebugError::post("MyTextureEditor::mappingMenuCB",
2876 "MAP_UNKNOWN selected!");
2877 #endif
2878 return;
2879 }
2880
2881 // replace the old func node
2882 p->sceneRoot->replaceChild(p->texFuncNode, newFunc);
2883 p->texFuncNode = newFunc;
2884 }
2885
2886 //
2887 // called whenever an entry within the option menu gets selected
2888 //
2889 void
2890 MyTextureEditor::optionMenuCB(Widget w, int id, void *)
2891 {
2892 // get the class pointer
2893 MyTextureEditor *p;
2894 XtVaGetValues(w, XmNuserData, &p, NULL);
2895
2896 switch(id) {
2897 case OPT_REPEAT:
2898 p->texNode->wrapS = SoTexture2::REPEAT;
2899 p->texNode->wrapT = SoTexture2::REPEAT;
2900 p->setRepeatState(TRUE);
2901 break;
2902
2903 case OPT_CLAMP:
2904 p->texNode->wrapS = SoTexture2::CLAMP;
2905 p->texNode->wrapT = SoTexture2::CLAMP;
2906 p->setRepeatState(FALSE);
2907 break;
2908
2909 case OPT_UNKNOWN:
2910 // this should never happen !
2911 #ifdef DEBUG
2912 SoDebugError::post("MyTextureEditor::optionMenuCB",
2913 "OPT_UNKNOWN selected!");
2914 #endif
2915 break;
2916 }
2917 }
2918
2919 //
2920 // called when an entry within the "File" menu is selected
2921 //
2922 void
2923 MyTextureEditor::fileMenuCB(Widget w, int id, void *)
2924 {
2925 // get the class pointer
2926 MyTextureEditor *p;
2927 XtVaGetValues(w, XmNuserData, &p, NULL);
2928
2929 switch(id) {
2930 case FILE_NEW:
2931 p->createNewDialog();
2932 break;
2933
2934 case FILE_RESET:
2935 p->createDeleteDialog("Reset Palette Dialog",
2936 "Reset to default palette ?", "(all changes will be lost)");
2937 break;
2938
2939 case FILE_DELETE:
2940 p->createDeleteDialog("Delete Palette Dialog",
2941 "Delete current palette ?", "(list of texture files will be lost)");
2942 break;
2943 }
2944 }
2945
2946 //
2947 // Called whenever a new item menu is selected from the palette
2948 // popup menu.
2949 //
2950 void
2951 MyTextureEditor::paletteMenuCB(Widget w, int num, void *)
2952 {
2953 // get the class pointer
2954 MyTextureEditor *p;
2955 XtVaGetValues(w, XmNuserData, &p, NULL);
2956
2957 // return if the same palette is choosen
2958 if (p->curPalette == num)
2959 return;
2960
2961 p->switchPalette(num);
2962 }
2963
2964 //
2965 // called when the delete dialog "ok"/"Cancel" buttons gets pressed. This
2966 // will delete the palette in the user's home directory.
2967 //
2968 void
2969 MyTextureEditor::deleteDialogCB(Widget dialog, MyTextureEditor *p,
2970 XmAnyCallbackStruct *cb)
2971 {
2972 // remove the user's palette
2973 if (cb->reason == XmCR_OK) {
2974
2975 PaletteStruct *pal = (PaletteStruct *) p->paletteList[p->curPalette];
2976
2977 // remove the palette in user's home
2978 char palDir[MAXPATHLEN];
2979 sprintf(palDir, "%s/%s/%s", getenv("HOME"), customTextureDir, pal->name);
2980 unlink(palDir);
2981
2982 // check if palette was also in the installed place (reset vs delete)
2983 if (pal->system)
2984 pal->user = FALSE;
2985 else {
2986 // remove the palette from the list, making sure we have
2987 // at least one palette entry
2988 free(pal->name);
2989 if (p->paletteList.getLength() == 1) {
2990 // create an empty default palette
2991 pal->name = strdup("default");
2992 }
2993 else {
2994 delete pal;
2995 p->paletteList.remove( p->curPalette );
2996
2997 // check what palette will be next
2998 if (p->curPalette == p->paletteList.getLength())
2999 p->curPalette--;
3000 }
3001
3002 // rebuild the new menu ( ??? have to rebuild, since we can't
3003 // remove entries)
3004 p->buildPaletteSubMenu();
3005 }
3006
3007 // finaly load the new palette
3008 p->switchPalette(p->curPalette);
3009 }
3010
3011 XtDestroyWidget(dialog);
3012 }
3013
3014 //
3015 // called whenever the "ok"/"Cancel" buttons within the "New Palette" dialog
3016 // gets pressed.
3017 //
3018 void
3019 MyTextureEditor::newDialogCB(Widget dialog, MyTextureEditor *p,
3020 XmAnyCallbackStruct *cb)
3021 {
3022 if (cb->reason == XmCR_OK) {
3023
3024 // retreive text and create palette
3025 Widget field = XmSelectionBoxGetChild(dialog, XmDIALOG_TEXT);
3026 char *str = XmTextGetString(field);
3027 if (str[0] != '\0')
3028 p->createNewPalette(str);
3029 XtFree(str);
3030 }
3031
3032 XtDestroyWidget(dialog);
3033 }
3034
3035 //
3036 // called when image dialog gets destroyed (reset widget pointers).
3037 //
3038 void
3039 MyTextureEditor::imageDialogDestroyCB(Widget, MyTextureEditor *p, void *)
3040 {
3041 //printf("shell destroyed\n");
3042 // reset widget pointers
3043 p->widgetList[DIALOG_WINDOW] = NULL;
3044 p->widgetList[DIALOG_IMAGE] = NULL;
3045 p->widgetList[DIALOG_NAME] = NULL;
3046 p->widgetList[DIALOG_FILE_BROWSER] = NULL;
3047
3048 // free image data and name
3049 delete p->dialogImage;
3050 if (p->dialogImageName != NULL)
3051 free(p->dialogImageName);
3052 if (p->dialogImageInfo != NULL)
3053 free(p->dialogImageInfo);
3054 p->dialogImage = NULL;
3055 p->dialogImageName = NULL;
3056 p->dialogImageInfo = NULL;
3057
3058 // free the glx contex
3059 if (p->imageDialogCtx)
3060 glXDestroyContext(p->getDisplay(), p->imageDialogCtx);
3061 p->imageDialogCtx = 0;
3062 }
3063
3064 //
3065 // called when image dialog "Open..." button gets pressed
3066 //
3067 void
3068 MyTextureEditor::imageDialogOpenCB(Widget, MyTextureEditor *p, void *)
3069 {
3070 if (p->widgetList[DIALOG_FILE_BROWSER] == NULL) {
3071
3072 Arg args[5];
3073 int n = 0;
3074
3075 // ??? need to find a way to save the motif file browser current directory
3076 // ??? for next time around (we really shouldn't delete this guy, but its
3077 // ??? parent gets destroyed when closed).
3078 // unmanage when ok/cancel are pressed
3079 XtSetArg(args[n], XmNautoUnmanage, TRUE); n++;
3080 XtSetArg(args[n], XmNtitle, "Image File Browser"); n++;
3081 Widget fileDialog = XmCreateFileSelectionDialog(
3082 p->widgetList[DIALOG_WINDOW], "fileBrowser", args, n);
3083
3084 p->widgetList[DIALOG_FILE_BROWSER] = fileDialog;
3085 XtAddCallback(fileDialog, XmNokCallback,
3086 (XtCallbackProc) MyTextureEditor::fileDialogOkCB,
3087 (XtPointer) p);
3088 }
3089
3090 XtManageChild(p->widgetList[DIALOG_FILE_BROWSER]);
3091 }
3092
3093 //
3094 // called when file dialog "Ok" button gets pressed
3095 //
3096 void
3097 MyTextureEditor::fileDialogOkCB(Widget, MyTextureEditor *p,
3098 XmFileSelectionBoxCallbackStruct *data)
3099 {
3100 // Get the file name
3101 char *fileName;
3102 if (!XmStringGetLtoR(data->value, XmSTRING_DEFAULT_CHARSET, &fileName))
3103 return;
3104
3105 p->setNewDialogImage(fileName);
3106 XtFree(fileName);
3107 }
3108
3109 //
3110 // called when image dialog "Apply" button gets pressed
3111 //
3112 void
3113 MyTextureEditor::imageDialogApplyCB(Widget, MyTextureEditor *p, void *)
3114 {
3115 if (p->selectedItem == -1)
3116 return;
3117
3118 TextureNameStruct *txt = &p->textureNames[ p->selectedItem ];
3119 SbBool textureChanged = FALSE;
3120
3121 // assign new texture
3122 if (p->dialogImageName != NULL) {
3123 if (txt->fullName == NULL || strcmp(p->dialogImageName, txt->fullName) != 0) {
3124 // add the new texture
3125 p->addTextureEntry(p->selectedItem, p->dialogImageName);
3126 textureChanged = TRUE;
3127 }
3128 }
3129 // else clear existing texture
3130 else {
3131 if (txt->fullName != NULL) {
3132 p->deleteTextureEntry(p->selectedItem);
3133 textureChanged = TRUE;
3134 }
3135 }
3136
3137 if (textureChanged) {
3138 // update the feedback and texture node
3139 glXMakeCurrent(p->getDisplay(), XtWindow(p->widgetList[TEXTURE_GLX]),
3140 p->paletteCtx);
3141 p->drawTextureTile(p->selectedItem, DRAW_SELECTED);
3142 p->updateTextureNode();
3143
3144 p->savePalette();
3145 }
3146 }
3147