File: vdialog.cxx

package info (click to toggle)
v1 1.20-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 6,240 kB
  • ctags: 9,439
  • sloc: cpp: 48,033; ansic: 8,939; makefile: 1,369; sh: 30
file content (522 lines) | stat: -rw-r--r-- 13,716 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//===============================================================
// vdialog.cxx - vDialog class
//
// Copyright (C) 1995,1996  Bruce E. Wampler
//
// This file is part of the V C++ GUI Framework, and is covered
// under the terms of the GNU Library General Public License,
// Version 2. This library has NO WARRANTY. See the source file
// vapp.cxx for more complete information about license terms.
//===============================================================

#include <v/vxutil.h>	// Motif/Athena mapping
#include <v/vdialog.h>		// our header
#include <v/vapp.h>
#include <v/vctlclrs.h>

extern "C"
{
#include <X11/Shell.h>

#ifdef Motif

#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/DialogS.h>
#include <Xm/Frame.h>

#else

#include <X11/Xaw/Form.h>
#include <X11/Xaw/Label.h>

#endif
}

#ifdef Motif	// --------------------------------------

#define formWidgetC xmFormWidgetClass
#define Ncallback XmNactivateCallback
#define Nheight XmNheight
#define Nlabel XmNlabelString
#define NmappedWhenManaged XmNmappedWhenManaged
#define Nwidth XmNwidth
#define NborderWidth XmNborderWidth

#define Ntitle XmNtitle
#define NallowShellResize XmNallowShellResize
#define XNx XmNx
#define XNy XmNy

#define setLabel(x,y) XmString x = XmStringCreateSimple(y);
#define freeLabel(x) XmStringFree(x);

#else	//------------------------------------------------------

#define formWidgetC formWidgetClass
#define NborderWidth XtNborderWidth
#define Ncallback XtNcallback
#define Nheight XtNheight
#define Nlabel XtNlabel
#define NmappedWhenManaged XtNmappedWhenManaged
#define Nwidth XtNwidth
#define setLabel(x,y) char* x = y;
#define freeLabel(x)

#define Ntitle XtNtitle
#define NallowShellResize XtNallowShellResize
#define XNx XtNx
#define XNy XtNy

#endif	// -----------------------------------------------------

// Define static data of the class

#ifndef Motif
#define GRAY_BACKGROUND
#else
#undef GRAY_BACKGROUND
#endif

#ifdef GRAY_BACKGROUND
#define gray_width 2
#define gray_height 2
static char gray_bits[] = {
   0x01, 0x02};
static Pixmap gray_bitmap = 0;
#endif

//===================>>> vDialog::vDialog <<<=======================
  vDialog::vDialog(const vDialog& d) : vBaseWindow(d), vCmdParent()
  {
    vSysError("vDialog - V semantics do not support copy constructors!");
  }

//=================>>> vDialog::~vDialog <<<=======================
  vDialog::~vDialog()
  {

    SysDebug(Destructor,"vDialog::~vDialog() destructor\n")

    _IsDisplayed = 0;		// The dialog is not up
    closeBaseWindow();		// close this window
  }

//===================>>> vDialog::vDialog <<<=======================
  vDialog::vDialog(vBaseWindow* creator, int modal, char* title) :
    vBaseWindow(title)		// constructor
  {
    _parentHandle = creator->vHandle();
    init(modal,title);
  }

//===================>>> vDialog::vDialog <<<=======================
  vDialog::vDialog(vApp* creator, int modal, char* title) :
    vBaseWindow(title)		// constructor
  {
    _parentHandle = creator->vHandle();
    init(modal,title);
  }

//===================>>> vDialog::vDialog <<<=======================
  void vDialog::init(int modal, char* title)
  {

    // Initialize dialog box

    _modal = modal;		// settable modal

    SysDebug(Constructor,"vDialog::vDialog() constructor\n")

    _wType = DIALOG;		// a dialog

    _IsDisplayed = 0;		// The dialog is not up

#ifdef GRAY_BACKGROUND
    // Create pixmap
    if (gray_bitmap == 0)		// haven't built the pixmap yet
      {
	gray_bitmap = XCreatePixmapFromBitmapData(
	    theApp->display(),				// our display
	    DefaultRootWindow(theApp->display()),	// a Drawable
	    gray_bits,		// the gray bitmap data
	    gray_width,		// from the file
	    gray_height,
	    (unsigned long) theApp->Xfg(),	// Forground color
	    (unsigned long) theApp->Xbg(),	// Background color
	    DefaultDepth(theApp->display(),DefaultScreen(theApp->display())));

      }
#endif

   // create a transient shell for the dialog

    _vHandle = XtVaCreatePopupShell(_name,	// name supplied
	    transientShellWidgetClass,		// a transient shell
	    theApp->vHandle(),			// based on App window
	    XtNtitle, _name,			// no label
	    XtNallowShellResize, TRUE,
	    NULL);

    // Now the dialog widget

#ifdef Motif
    
    unsigned char st;
    if (_modal)
	st = XmSHADOW_ETCHED_OUT;
    else
	st = XmSHADOW_OUT;

    _wDialog = XtVaCreateWidget(
	"vDialog",		// widget name  
	formWidgetC,		// widget class 
	_vHandle,		// based on our popup window
	XmNmarginHeight,10,
	XmNmarginWidth,10,
	XmNshadowThickness,4,
	XmNshadowType, st,
	XmNverticalSpacing,3,
	XmNhorizontalSpacing,3,
	XmNresizePolicy, XmRESIZE_ANY,
	NULL);			// terminate varargs list 

#else	// Athena

    _wDialog = XtVaCreateWidget(
	"vDialog",		// widget name  
	formWidgetC,		// widget class 
	_vHandle,		// based on our popup window
	XtNresizable,1,
	NULL);			// terminate varargs list 

#ifdef GRAY_BACKGROUND
    // set the background pattern
    if (theApp->Xdepth() <= 1)
	XtVaSetValues(_wDialog,		// the widget to set
	  XtNbackgroundPixmap, gray_bitmap, NULL);
    else
#endif
	XtVaSetValues(_wDialog,		// the widget to set
	  XtNbackground, _vDialogBG, NULL);

#endif	// Motif/Athena

    _DefaultButton = 0;	// No default button described
    _FirstTextIn = 0;	// No text in field
  }

//===================>>> vDialog::AddDialogCmds <<<========================
  void vDialog::AddDialogCmds(CommandObject* cList)	// add commands
  {
    // This is called to add commands from the supplied
    // CommandObject list.

    vCmd* defButton = 0;
    DlgCmdList* curCmd;
    int textIns = 0;		// how many text ins

    // scan the entire list

    for (int ix = 0 ; cList && (cList[ix].cmdType != C_EndOfList) ; ++ix)
      {
	curCmd = new DlgCmdList;		// get a new cell

	curCmd->nextDCL = _cmdList;		// add in at front
	_cmdList = curCmd;	

	curCmd->cmdP = AddCmd(&cList[ix]);

	// Track default button

	if (cList[ix].attrs & CA_DefaultButton)
	  {
	     defButton = curCmd->cmdP;
	  }

	if (cList[ix].cmdType == C_TextIn)
	  {
	    ++textIns;
 	    if (_FirstTextIn == 0)
		_FirstTextIn = curCmd->cmdP;
	  }
      }

    if (defButton)			// we have a default
      {
	_DefaultButton = defButton;	// Track the default button

	// This lets us use Enter to activate the default button.
	// The accelerators are defined elsewhere - this just
	// allows the accelerators for _wDialog to apply to
	// the default button as well.

#ifdef Motif
	// @@@ need to do something
#else
	XtInstallAccelerators(_wDialog, defButton->wCmd); // add accelerators
#endif
      }
#ifdef Motif
	// @@@ do something
#else // Athena
    if (textIns == 1 && _FirstTextIn != 0)	// we had a textin field
      {
	XtSetKeyboardFocus(_wDialog,_FirstTextIn->wCmd); // set focus to first text
      }
#endif
  }

#ifdef useAddDialogCmdObj
// move next line to .h if used
virtual void vDialog::AddDialogCmdObj(CommandObject* Cmd, vCmd* CmdInstance);
//===================>>> vDialog::AddDialogCmdObj <<<========================
  void vDialog::AddDialogCmdObj(CommandObject* Cmd, vCmd* CmdInstance)
  {
    // This is called to add an already instantiated command object to the
    // CommandObject list.

    vCmd* defButton = 0;

    // scan the entire list

    DlgCmdList* curCmd = new DlgCmdList;		// get a new cell

    curCmd->nextDCL = _cmdList;		// add in at front of list

    _cmdList = curCmd;	

    curCmd->cmdP = CmdInstance;		// point to the object

    // Track default button

    if (Cmd->attrs & CA_DefaultButton)
      {
	_DefaultButton = CmdInstance;	// This is the default button
	// This lets us use Enter to activate the default button.
	// The accelerators are defined elsewhere - this just
	// allows the accelerators for _wDialog to apply to
	// the default button as well.

#ifdef Motif
	// @@@ need to do something
#else
	XtInstallAccelerators(_wDialog, defButton->wCmd); // add accelerators
#endif
      }
  }
#endif

//====================>>> vDialog::CancelDialog <<<=======================
  void vDialog::CancelDialog(void)
  {
    // Cancel selected - reset all values to original values


    SysDebug(CmdEvents,"vDialog::CancelDialog()\n")

    for (DlgCmdList* cc = _cmdList ; cc != 0  ; cc = cc->nextDCL)
      {
	(cc->cmdP)->ResetItemValue();
      }

    // And close the dialog

    CloseDialog();
  }

//====================>>> vDialog::CloseDialog <<<=======================
  void vDialog::CloseDialog(void)
  {

    SysDebug(Build,"vDialog::CloseDialog()\n")

    // This is UGLY, but so are combo boxes, and we gotta do it!
    for (DlgCmdList* cc = _cmdList ; cc != 0  ; cc = cc->nextDCL)
      {
	if ((cc->cmdP)->dlgCmd->cmdType == C_ComboBox)
	    (cc->cmdP)->SetCmdVal(0,Checked);
      }

    XtPopdown(_vHandle);	// pop down
    _IsDisplayed = 0;		// The dialog is not up
  }

//====================>>> vDialog::DialogCommand <<<=======================
  void vDialog::DialogCommand(ItemVal id, ItemVal retval, CmdType ctype)
  {
    // After the user has selected a command from the dialog,
    // this routine is called with the value


    SysDebug2(CmdEvents,"vDialog::DialogCommand(id:%d, val:%d)\n",id, retval)

    switch (id)			// We will do some things depending on value
      {
	case M_Cancel:
	    CancelDialog();
	    break;

	case M_Done:		// default, close dialog
	case M_OK:
	  {
	    CloseDialog();
	    break;
	  }
      }
  }

//====================>>> vDialog::DialogDisplayed <<<=======================
  void vDialog::DialogDisplayed()
  {
  }

//==================>>> vDialog::ProcessCmd <<<=======================
  void vDialog::ProcessCmd(ItemVal id, ItemVal rv, CmdType ct)
  {
    // simple interface between command objects and the
    // inherited vCmdParent ProcessCmd to the CommandObject which does the work

    if (ct == C_TextIn)		// TextIn special
      {
	if (_DefaultButton == 0)	// There is NOTa default button
	    return;
	// change the textin return to the default button
	id = _DefaultButton->_cmdId;	// get the id of the default cmd
	rv = _DefaultButton->_retVal;
        ct = (_DefaultButton->dlgCmd)->cmdType;
      }

    DialogCommand(id, rv, ct);
  }

//====================>>> vDialog::SetDialogTitle <<<=======================
  void vDialog::SetDialogTitle(char* title)
  {
    XtVaSetValues(_vHandle, XtNtitle, title, NULL);
  }

//================>>> vDialog::GetDialogPosition <<<========================
  void vDialog::GetDialogPosition(int& left, int& top, int& width, int& height)
  {
    Dimension w,h,b;
    Position l,t;

    XtVaGetValues(_vHandle, Nwidth, &w, Nheight, &h,
		  XNx, &l, XNy, &t, NborderWidth, &b, NULL);
    
    left = l; top = t; width = w+b+b; height = h+b+b;

  }

//====================>>> vDialog::SetDialogPosition <<<======================
  void vDialog::SetDialogPosition(int left, int top)
  {
    Position l = left;
    Position t = top;

    XtVaSetValues(_vHandle,XNx,l, XNy,t,NULL);
  }

//====================>>> vDialog::ShowDialog <<<=======================
  void vDialog::ShowDialog(const char* msg)
  {
    //	Show the dialog with the default message

    //  We will control the position of the dialog box based on
    //	the parent's location. The user can move the dialog later.

    Position x, y;
    Dimension width, height;
    Dimension mywidth, myheight;


    SysDebug1(Build,"vDialog::ShowDialog(%s)\n",msg)

    // First, get the values from our parent

    if (IsDisplayed())
	return;				// make this a no-op

    XtVaGetValues(_parentHandle,	// parent widget 
	Nwidth, &width,		// get these values 
	Nheight, &height,
	NULL);
	
    //	Now, calculate the center of our parent using its width
    //	and height, translate them to root widow world coordinates

    XtTranslateCoords(_parentHandle,	// Widget 
	(Position) width/2,		// x 
	(Position) height/2,		// y 
	&x, &y);			// coords on root window 

    XtVaGetValues(_vHandle,	// parent widget 
	Nwidth, &mywidth,		// get these values 
	Nheight, &myheight,
	NULL);
	
    // x and y are the world coordinates of the center of our parent.
    // Adjust them so the dialog  will be centered over the parent,
    // but not above or left of the parent

    if (myheight == 0 || myheight > height)	// I'm taller than my parent
	y = y - (height/2) + 35;
    else
	y = y - (myheight/2);

    if (mywidth == 0 || mywidth > width)
	x = x - (width/2) + 35;
    else
	x = x - (mywidth/2);
	
    XtVaSetValues(_vHandle, XtNx, x,  XtNy, y, NULL);

    // set the message field 

    if (msg && *msg)
      {
	for (DlgCmdList* cc = _cmdList ; cc != 0  ; cc = cc->nextDCL)
	  {
	    if (((cc->cmdP)->dlgCmd)->attrs & CA_MainMsg)
	      {
		setLabel(tmp,(char *)msg)
		XtVaSetValues((cc->cmdP)->wCmd,	// change the message
		    Nlabel,tmp, NULL);
		freeLabel(tmp)
	      }
	  }
      }

    // Since the dialog isn't necessarily re-created each time it is
    // popped up, we need to loop through now to set the original values

    for (DlgCmdList* cc = _cmdList ; cc != 0  ; cc = cc->nextDCL)
      {
	(cc->cmdP)->SaveItemValue();
      }

    // Finally, make sure form is managed
    if (!XtIsManaged(_wDialog))
	XtManageChild(_wDialog);

    // Now ready to popup the dialog
    if (_modal)
	XtPopup(_vHandle, XtGrabExclusive);
    else
	XtPopup(_vHandle, XtGrabNone);

#ifndef DONT_WARP_MOUSE
    if (_DefaultButton != 0)		// There is a default button
      {
	Widget wDef = _DefaultButton->wCmd;	// get the def button widget

	XWarpPointer(XtDisplay(wDef),None, XtWindow(wDef),
				0,0,0,0, 10, 10);	// Warp to button
	XSync(XtDisplay(wDef),False);
      }
#endif
    _IsDisplayed = 1;			// The dialog is up
    DialogDisplayed();
  }
// ---------------------------------------------------------------------