~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/clients/qmon/qmon_util.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 * 
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 * 
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 * 
 
9
 * 
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 * 
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 * 
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 * 
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 * 
 
28
 *   All Rights Reserved.
 
29
 * 
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
#include <stdio.h>
 
33
#include <stdlib.h>
 
34
#include <ctype.h>
 
35
 
 
36
#include <Xm/Xm.h>
 
37
#include <Xm/List.h>
 
38
#include <Xm/Scale.h>
 
39
#include <Xm/ToggleB.h>
 
40
 
 
41
#include <Xmt/Xmt.h>
 
42
#include <Xmt/Layout.h>
 
43
 
 
44
#include "qmon_util.h"
 
45
#include "qmon_cull.h"
 
46
#include "qmon_rmon.h"
 
47
 
 
48
 
 
49
#if XmVersion < 1002
 
50
 
 
51
/*-------------------------------------------------------------------------*/
 
52
void XmListDeletePositions(Widget list, int *pos_list, int pos_count)
 
53
{
 
54
   int i;
 
55
 
 
56
   for (i=0; i<pos_count; i++) {
 
57
      XmListDeleteItemsPos(list, 1, pos_list[i] - i);
 
58
   }
 
59
}
 
60
 
 
61
#endif
 
62
 
 
63
/*-------------------------------------------------------------------------*/
 
64
/*-------------------------------------------------------------------------*/
 
65
String* XmStringTableToStringTable(XmStringTable items, Cardinal itemCount, 
 
66
                                    XmStringCharSet tag)
 
67
{
 
68
   Cardinal i;
 
69
   String* str_table = NULL;
 
70
 
 
71
   DENTER(GUI_LAYER, "XmStringTableToStringTable");
 
72
   
 
73
   if (!itemCount || !items) {
 
74
      DPRINTF(("itemCount/items is 0/NULL\n"));
 
75
      DEXIT;
 
76
      return NULL;
 
77
   }
 
78
   
 
79
   str_table = (String*) XtMalloc(sizeof(String) * itemCount);
 
80
 
 
81
   if (!str_table) {
 
82
      DPRINTF(("malloc failure\n"));
 
83
      DEXIT;
 
84
      return NULL;
 
85
   }
 
86
 
 
87
   for (i=0; i<itemCount; i++) {
 
88
      if (!XmStringGetLtoR(items[i], tag ? tag : XmFONTLIST_DEFAULT_TAG, 
 
89
                     str_table + i) ) {
 
90
         DPRINTF(("XmStringGetLtoR failure\n"));
 
91
         StringTableFree(str_table, i);
 
92
         DEXIT;
 
93
         return NULL;
 
94
      }
 
95
   }
 
96
 
 
97
   DEXIT;
 
98
   return str_table;
 
99
}
 
100
 
 
101
/*-------------------------------------------------------------------------*/
 
102
XmString *StringTableToXmStringTable(String *items, Cardinal itemCount,
 
103
                                       XmStringCharSet tag)
 
104
{
 
105
   XmString *xstr_table;
 
106
   Cardinal i;
 
107
   
 
108
   DENTER(GUI_LAYER, "StringTableToXmStringTable");
 
109
 
 
110
   if (!itemCount || !items) {
 
111
      DPRINTF(("itemCount/items is 0/NULL\n"));
 
112
      DEXIT;
 
113
      return NULL;
 
114
   }
 
115
   
 
116
   xstr_table = (XmString*) XtMalloc(sizeof(XmString) * itemCount);
 
117
 
 
118
   if (!xstr_table) {
 
119
      DPRINTF(("malloc failure\n"));
 
120
      DEXIT;
 
121
      return NULL;
 
122
   }
 
123
 
 
124
   for (i=0; i<itemCount; i++) {
 
125
      xstr_table[i] = XmStringCreateLtoR( items[i], 
 
126
                         tag ? tag : XmFONTLIST_DEFAULT_TAG); 
 
127
   }
 
128
 
 
129
   DEXIT;
 
130
   return xstr_table;
 
131
}
 
132
   
 
133
/*-------------------------------------------------------------------------*/
 
134
void StringTableFree( String *str_table, Cardinal str_table_size)
 
135
{
 
136
   int i;
 
137
   
 
138
   DENTER(GUI_LAYER, "StringTableFree");
 
139
 
 
140
   for (i=0; i<str_table_size; i++)
 
141
      XtFree((char *) str_table[i]);
 
142
 
 
143
   XtFree((char*)str_table);
 
144
 
 
145
   DEXIT;
 
146
}
 
147
 
 
148
/*-------------------------------------------------------------------------*/
 
149
void XmStringTableFree(XmString *xstr_table, Cardinal xstr_table_size)
 
150
{
 
151
   int i;
 
152
   
 
153
   DENTER(GUI_LAYER, "XmStringTableFree");
 
154
 
 
155
   for (i=0; i<xstr_table_size; i++)
 
156
      if (xstr_table[i])
 
157
         XmStringFree(xstr_table[i]);
 
158
 
 
159
   XtFree((char*)xstr_table);
 
160
 
 
161
   DEXIT;
 
162
}
 
163
 
 
164
 
 
165
 
 
166
/*-------------------------------------------------------------------------*/
 
167
Boolean AddStringToXmList(Widget list, String str, XmStringCharSet tag, 
 
168
                           int list_pos)
 
169
{
 
170
   XmString item;
 
171
   Boolean ret = False;
 
172
   
 
173
   DENTER(GUI_LAYER, "AddStringToXmList");
 
174
 
 
175
   item = XmStringCreateLtoR(str, tag);
 
176
   if ( ! XmListItemExists(list, item)) {
 
177
      XmListAddItem(list, item, list_pos);
 
178
      ret = True;
 
179
   }
 
180
   
 
181
   XmStringFree(item);
 
182
 
 
183
   DEXIT;
 
184
   return ret;
 
185
}
 
186
   
 
187
/*-------------------------------------------------------------------------*/
 
188
void UpdateXmList(Widget list, String *str_table, Cardinal size,
 
189
                     XmStringCharSet tag)
 
190
{
 
191
   XmString *items = NULL;
 
192
   
 
193
   DENTER(GUI_LAYER, "UpdateXmList");
 
194
 
 
195
   /*
 
196
   ** replace the old items
 
197
   */
 
198
   items = StringTableToXmStringTable(str_table, size, tag);
 
199
 
 
200
   XtVaSetValues( list,
 
201
                  XmNitems, items,
 
202
                  XmNitemCount, size,
 
203
                  NULL);
 
204
 
 
205
   XmStringTableFree(items, size);
 
206
 
 
207
#if 0
 
208
   XtVaGetValues( list,
 
209
                  XmNitems, &items,
 
210
                  XmNitemCount, &itemCount,
 
211
                  NULL);
 
212
 
 
213
   DPRINTF(("UpdateXmList: itemCount = %d\n", itemCount));
 
214
 
 
215
   for (i=0; i<size; i++) {
 
216
      /* strings are equal */
 
217
      if (i<itemCount) {
 
218
         DPRINTF(("in_str = '%s'\n", str_table[i]));
 
219
DTRACE;
 
220
         if (items[i] && XmStringGetLtoR(items[i], tag, &str1)) { 
 
221
            DPRINTF(("str1='%s'\n", str1));
 
222
DTRACE;
 
223
            if (!strcmp(str1, str_table[i])) {
 
224
DTRACE;
 
225
               XtFree((char *)str1);
 
226
               str1 = NULL;
 
227
DTRACE;
 
228
               continue;
 
229
            }
 
230
DTRACE;
 
231
            XtFree((char *)str1);
 
232
            str1 = NULL;
 
233
DTRACE;
 
234
         }
 
235
DTRACE;
 
236
         XmListDeletePos(list, i+1);   /* first element is 1 */
 
237
      }
 
238
DTRACE;
 
239
      new = XmStringCreateLtoR(str_table[i], tag);
 
240
DTRACE;
 
241
      XmListAddItem(list, new, i+1);
 
242
DTRACE;
 
243
      XmStringFree(new);
 
244
   }
 
245
 
 
246
   /* size < itemCount -> delete the rest */
 
247
DTRACE;
 
248
   if (i<itemCount)
 
249
      XmListDeleteItemsPos(list, itemCount-size, i+1);
 
250
 
 
251
DTRACE;
 
252
/*
 
253
   for (;i<itemCount;i++) {   
 
254
      XmListDeletePos(list, i+1);
 
255
      itemCount--;
 
256
   }
 
257
*/
 
258
#endif
 
259
   DEXIT;
 
260
}
 
261
 
 
262
 
 
263
/*-------------------------------------------------------------------------*/
 
264
Boolean UpdateXmListFromCull(Widget list, XmStringCharSet tag, lList *lp, 
 
265
                              int field)
 
266
{
 
267
   lListElem *ep;
 
268
   Cardinal itemCount;
 
269
   StringConst str;
 
270
   int pos;
 
271
   int dataType;
 
272
   const lDescr *listDescriptor = NULL;
 
273
   Widget lw;
 
274
   Boolean found = False;
 
275
   
 
276
   DENTER(GUI_LAYER, "UpdateXmListFromCull");
 
277
   XtVaGetValues(list, XmNitemCount, &itemCount, NULL);
 
278
 
 
279
   if (itemCount)
 
280
      XmListDeleteAllItems(list);
 
281
 
 
282
   if (lGetNumberOfElem(lp) <= 0) {
 
283
      DEXIT;
 
284
      return False;
 
285
   }
 
286
 
 
287
   for (lw = XtParent(list); lw; lw = XtParent(lw)) {
 
288
      if (XtIsSubclass(lw, xmtLayoutWidgetClass)) {
 
289
         found = True;
 
290
         break;
 
291
      }  
 
292
   }
 
293
   
 
294
   if (found && lw)
 
295
      XmtLayoutDisableLayout(lw);
 
296
   
 
297
   listDescriptor = lGetListDescr(lp);
 
298
   pos = lGetPosInDescr(listDescriptor, field); 
 
299
   dataType = lGetPosType(listDescriptor,pos);
 
300
   for (ep=lFirst(lp); ep; ep=lNext(ep)) {
 
301
      switch (dataType) {
 
302
         case lStringT:
 
303
              str = lGetPosString(ep, pos);
 
304
           break;
 
305
         case lHostT:
 
306
              str = lGetPosHost(ep, pos);
 
307
           break;
 
308
         default:
 
309
              str = "(null) UpdateXmListFromCull";
 
310
              DPRINTF(("UpdateXmListFromCull: unexpected data type\n"));
 
311
           break; 
 
312
      }
 
313
      /* FIX_CONST_GUI */
 
314
      XmListAddItemUniqueSorted(list, (String)str);
 
315
      DPRINTF(("UpdateXmListFromCull: str = '%s'\n", str));
 
316
   }   
 
317
 
 
318
   if (found && lw)
 
319
      XmtLayoutEnableLayout(lw);
 
320
      
 
321
   DEXIT;
 
322
   return True;
 
323
}
 
324
 
 
325
/*-------------------------------------------------------------------------*/
 
326
lList *XmStringToCull(Widget list, lDescr *dp, int nm, int selected)
 
327
{
 
328
   XmString *items;
 
329
   Cardinal itemCount;
 
330
   String *strings;
 
331
   lList *lp = NULL;
 
332
   int i;
 
333
   int dataType;
 
334
   
 
335
   DENTER(GUI_LAYER, "XmStringToCull");
 
336
 
 
337
   if (selected == ALL_ITEMS)
 
338
      XtVaGetValues( list,
 
339
                  XmNitems, &items,
 
340
                  XmNitemCount, &itemCount,
 
341
                  NULL);
 
342
   else
 
343
      XtVaGetValues( list,
 
344
                  XmNselectedItems, &items,
 
345
                  XmNselectedItemCount, &itemCount,
 
346
                  NULL);
 
347
   
 
348
   if (itemCount) {
 
349
      strings = XmStringTableToStringTable(items, itemCount, 
 
350
                                          XmFONTLIST_DEFAULT_TAG);
 
351
      if (strings) { 
 
352
         for (i=0; i<itemCount; i++) {
 
353
            dataType = lGetPosType(dp, lGetPosInDescr(dp, nm));
 
354
            switch (dataType) {
 
355
               case lStringT:
 
356
                  lAddElemStr(&lp, nm, strings[i], dp);
 
357
                  break;
 
358
               case lHostT:
 
359
                  lAddElemHost(&lp, nm, strings[i], dp);
 
360
                  break;
 
361
               default:
 
362
                  DPRINTF(("XmStringToCull - unexpected data type\n"));
 
363
                  break;
 
364
            }
 
365
         }
 
366
         StringTableFree(strings, itemCount);
 
367
      }
 
368
   }
 
369
 
 
370
   DEXIT;
 
371
   return lp;
 
372
}
 
373
   
 
374
 
 
375
 
 
376
   
 
377
/*-------------------------------------------------------------------------*/
 
378
Boolean DelStringFromXmList(Widget list, String str, XmStringCharSet tag,
 
379
                              int list_pos)
 
380
{
 
381
   XmString item;
 
382
   
 
383
   DENTER(GUI_LAYER, "DelStringFromXmList");
 
384
   
 
385
   if (!list_pos && !str) {
 
386
      DEXIT;
 
387
      return False;
 
388
   }
 
389
 
 
390
   if (list_pos) {
 
391
      XmListDeletePos(list, list_pos);
 
392
   }
 
393
   else {
 
394
      item = XmStringCreateLtoR(str, tag);
 
395
      XmListDeleteItem(list, item);
 
396
   }
 
397
 
 
398
   DEXIT;
 
399
   return True;
 
400
}
 
401
   
 
402
 
 
403
/*-------------------------------------------------------------------------*/
 
404
void XmListAddTextItem(Widget list_w, String item, XmStringCharSet tag,
 
405
                        int position)
 
406
{
 
407
   XmString str;
 
408
 
 
409
   str = XmStringCreateLtoR(item, tag);
 
410
 
 
411
   XmListAddItem(list_w, str, position);
 
412
 
 
413
   XmStringFree(str);
 
414
}
 
415
 
 
416
/*-------------------------------------------------------------------------*/
 
417
void XmListAddTextItems(Widget list_w, String *items, Cardinal itemCount,  
 
418
                        XmStringCharSet tag, int position)
 
419
{
 
420
   XmString *str;
 
421
 
 
422
   str = StringTableToXmStringTable(items, itemCount, tag);
 
423
 
 
424
   XmListAddItems(list_w, str, itemCount, position);
 
425
 
 
426
   XmStringTableFree(str, itemCount);
 
427
}
 
428
 
 
429
#ifdef _MOTIF_12_
 
430
/*-------------------------------------------------------------------------*/
 
431
void XmListAddTextItemUnselected(Widget list_w, String item, 
 
432
                                 XmStringCharSet tag, int position)
 
433
{
 
434
   XmString str;
 
435
 
 
436
 
 
437
   str = XmStringCreateLtoR(item, tag);
 
438
 
 
439
   XmListAddItemUnselected(list_w, str, position);
 
440
 
 
441
   XmStringFree(str);
 
442
}
 
443
 
 
444
/*-------------------------------------------------------------------------*/
 
445
void XmListAddTextItemsUnselected(Widget list_w, String *items, 
 
446
                     Cardinal itemCount, XmStringCharSet tag, int position)
 
447
{
 
448
   XmString *str;
 
449
 
 
450
   str = StringTableToXmStringTable(items, itemCount, tag);
 
451
 
 
452
   XmListAddItemsUnselected(list_w, str, itemCount, position);
 
453
 
 
454
   XmStringTableFree(str, itemCount);
 
455
}
 
456
#endif
 
457
 
 
458
/*-------------------------------------------------------------------------*/
 
459
void XmListSelectTextItem(Widget list_w, String item, XmStringCharSet tag, 
 
460
                           Boolean notify)
 
461
{
 
462
   XmString str;
 
463
 
 
464
   str = XmStringCreateLtoR(item, tag);
 
465
   
 
466
   XmListSelectItem(list_w, str, notify);
 
467
 
 
468
   XmStringFree(str);
 
469
}
 
470
 
 
471
/*-------------------------------------------------------------------------*/
 
472
void qmonPositionDialog(Widget dialog, XtPointer cld, XtPointer cad)
 
473
{
 
474
   static int x, y;
 
475
   int w, h, ws, hs;
 
476
 
 
477
   DENTER(GUI_LAYER, "qmonPositionDialog");
 
478
 
 
479
   XtVaGetValues(dialog, XmNwidth, &w, XmNheight, &h, NULL);
 
480
 
 
481
   ws = WidthOfScreen(XtScreen(dialog));
 
482
   hs = HeightOfScreen(XtScreen(dialog));
 
483
   if ((x + w) >= ws)
 
484
      x = 0;
 
485
   else
 
486
      x = (ws - w) / 2;
 
487
 
 
488
   if ((y + h) >= hs)
 
489
      y = 0;
 
490
   else
 
491
      y = (hs - h) / 2;
 
492
 
 
493
   XtVaSetValues(dialog, XmNx, x, XmNy, y, NULL);
 
494
 
 
495
   DEXIT;
 
496
}
 
497
 
 
498
/*-------------------------------------------------------------------------*/
 
499
void SetMinShellSize(Widget shell, XtPointer cld, XEvent *event, Boolean *ctd)
 
500
{
 
501
   XConfigureEvent *cevent = (XConfigureEvent*)event;
 
502
 
 
503
   DENTER(GUI_LAYER, "SetMinShellSize");
 
504
 
 
505
   if (cevent->type != ConfigureNotify) {
 
506
      DEXIT;
 
507
      return;
 
508
   }
 
509
 
 
510
   if (!cld) {
 
511
      XtVaSetValues( shell,
 
512
                  XmNminWidth, cevent->width,
 
513
                  XmNminHeight, cevent->height,
 
514
                  NULL);
 
515
   }
 
516
   else if ((long)cld == SHELL_WIDTH) {
 
517
            XtVaSetValues( shell,
 
518
                           XmNminWidth, cevent->width,
 
519
                           NULL);
 
520
   }
 
521
   else if ((long)cld == SHELL_HEIGHT) {
 
522
      XtVaSetValues( shell,
 
523
                  XmNminHeight, cevent->height,
 
524
                  NULL);
 
525
   } 
 
526
 
 
527
   XtRemoveEventHandler(shell, StructureNotifyMask, False, 
 
528
                           SetMinShellSize, NULL);
 
529
   
 
530
   DEXIT;
 
531
}
 
532
                  
 
533
/*-------------------------------------------------------------------------*/
 
534
void SetMaxShellSize(Widget shell, XtPointer cld, XEvent *event, Boolean *ctd)
 
535
{
 
536
   XConfigureEvent *cevent = (XConfigureEvent*)event;
 
537
 
 
538
   DENTER(GUI_LAYER, "SetMinShellSize");
 
539
 
 
540
   if (cevent->type != ConfigureNotify) {
 
541
      DEXIT;
 
542
      return;
 
543
   }
 
544
 
 
545
   if (!cld) {
 
546
      XtVaSetValues( shell,
 
547
                  XmNmaxWidth, cevent->width,
 
548
                  XmNmaxHeight, cevent->height,
 
549
                  NULL);
 
550
   }
 
551
   else if ((long)cld == SHELL_WIDTH) {
 
552
            XtVaSetValues( shell,
 
553
                           XmNmaxWidth, cevent->width,
 
554
                           NULL);
 
555
   }
 
556
   else if ((long)cld == SHELL_HEIGHT) {
 
557
      XtVaSetValues( shell,
 
558
                  XmNmaxHeight, cevent->height,
 
559
                  NULL);
 
560
   } 
 
561
 
 
562
   XtRemoveEventHandler(shell, StructureNotifyMask, False, 
 
563
                           SetMaxShellSize, NULL);
 
564
   
 
565
   DEXIT;
 
566
}
 
567
                  
 
568
 
 
569
/*-------------------------------------------------------------------------*/
 
570
int qmonForkEditor(String file)
 
571
{
 
572
   char *cp;
 
573
   char *editor = NULL;
 
574
   char command[BUFSIZ];
 
575
   static char default_editor[] = "vi";
 
576
   int status;
 
577
   
 
578
   DENTER(GUI_LAYER, "qmonForkEditor");
 
579
 
 
580
   if (!(editor = getenv("EDITOR")))
 
581
      editor = default_editor;
 
582
 
 
583
   cp = strrchr(editor, '/'); 
 
584
   if (cp && !strcmp(cp + 1, "jot"))
 
585
      strcpy(command, editor);
 
586
   else
 
587
      sprintf(command, "xterm -e %s", editor);
 
588
 
 
589
   if (file)
 
590
      sprintf(command, "%s %s &", command, file ? file : "");
 
591
   else
 
592
      strcat(command, "&");
 
593
 
 
594
   status = system(command);
 
595
   
 
596
   DEXIT;
 
597
   return status;
 
598
}
 
599
 
 
600
 
 
601
/*-------------------------------------------------------------------------*/
 
602
void qmonXmStringDrawImage(Display *dpy, Drawable da, GC gc, Position x, 
 
603
                           Position y, XmFontList fontlist, XmString str,
 
604
                           unsigned char layout, XRectangle *clip,
 
605
                           Dimension *width, Dimension *height)
 
606
{
 
607
   DENTER(GUI_LAYER, "qmonDrawRequestEntry");
 
608
   
 
609
   /* GET THE BOUNDING BOX */
 
610
   XmStringExtent(fontlist, str, width, height);
 
611
 
 
612
   /* DRAW RECTANGLE AND LABEL */
 
613
   XmStringDrawImage(dpy, da, fontlist, str, gc, x, y, *width,  
 
614
                  layout, XmSTRING_DIRECTION_L_TO_R, 
 
615
                  clip);
 
616
   DEXIT;
 
617
}
 
618
 
 
619
 
 
620
/*-------------------------------------------------------------------------*/
 
621
void qmonXmStringDraw(Display *dpy, Drawable da, GC gc, Position x, Position y,
 
622
                      XmFontList fontlist, XmString str, unsigned char layout,
 
623
                      XRectangle *clip, Dimension *width, Dimension *height)
 
624
{
 
625
   DENTER(GUI_LAYER, "qmonXmStringDraw");
 
626
   
 
627
   /* GET THE BOUNDING BOX */
 
628
   XmStringExtent(fontlist, str, width, height);
 
629
 
 
630
   /* DRAW RECTANGLE AND LABEL */
 
631
   XmStringDraw(dpy, da, fontlist, str, gc, x, y, *width,  
 
632
                  layout, XmSTRING_DIRECTION_L_TO_R, 
 
633
                  clip);
 
634
   DEXIT;
 
635
}
 
636
 
 
637
 
 
638
 
 
639
/*-------------------------------------------------------------------------*/
 
640
void setButtonLabel(Widget w, String label)
 
641
{
 
642
   XmString xlab;
 
643
 
 
644
   DENTER(GUI_LAYER, "setButtonLabel");
 
645
 
 
646
   xlab = XmtCreateLocalizedXmString(w, label);
 
647
   XtVaSetValues(w, XmNlabelString, xlab, NULL);
 
648
   XmStringFree(xlab);
 
649
 
 
650
   DEXIT;
 
651
}
 
652
   
 
653
   
 
654
/*-------------------------------------------------------------------------*/
 
655
void qmonScaleWeightToggle(Widget w, XtPointer cld, XtPointer cad)
 
656
{
 
657
   XmToggleButtonCallbackStruct *cbs = (XmToggleButtonCallbackStruct*)cad;
 
658
   tScaleWeight *weights = (tScaleWeight*) cld;
 
659
   int index;
 
660
   int *ip;
 
661
 
 
662
   DENTER(GUI_LAYER, "qmonScaleWeightToggle");
 
663
   
 
664
   XtVaGetValues(w, XmNuserData, &ip, NULL);
 
665
   index = *ip;
 
666
   DPRINTF(("index = %d\n", index));
 
667
 
 
668
   if (cbs->set) {
 
669
      weights[index].lock = 1;    
 
670
      XtSetSensitive(weights[index].scale, False);
 
671
   }
 
672
   else {
 
673
      weights[index].lock = 0;    
 
674
      XtSetSensitive(weights[index].scale, True);
 
675
   }
 
676
 
 
677
   DEXIT;
 
678
}
 
679
 
 
680
/*-------------------------------------------------------------------------*/
 
681
void qmonScaleWeight(Widget w, XtPointer cld, XtPointer cad)
 
682
{
 
683
   XmScaleCallbackStruct *cbs = (XmScaleCallbackStruct*)cad;
 
684
   tScaleWeight *weights = (tScaleWeight*) cld;
 
685
   int delta;
 
686
   int *ip;
 
687
   int index;
 
688
   int count = 0;
 
689
   int i, j;
 
690
   int max_value = 0;
 
691
   int delta_part;
 
692
   int sum = 0;
 
693
 
 
694
   DENTER(GUI_LAYER, "qmonScaleWeight");
 
695
 
 
696
   XtVaGetValues(w, XmNuserData, &ip, NULL);
 
697
   index = *ip;
 
698
   DPRINTF(("index = %d\n", index));
 
699
 
 
700
   if (cbs->value != weights[index].weight) {
 
701
 
 
702
      /*
 
703
      ** get the max_value and the number of non-locked scales 
 
704
      */
 
705
      for (i=0; i<weights[0].nr; i++) {
 
706
         if (!weights[i].lock) {
 
707
            max_value += weights[i].weight; 
 
708
            if (i!= index)
 
709
               count++;
 
710
         }
 
711
      }
 
712
 
 
713
      if (count) {
 
714
         /*
 
715
         ** check max value
 
716
         */
 
717
         if (cbs->value > max_value)
 
718
            weights[index].weight = max_value;
 
719
         else
 
720
            weights[index].weight = cbs->value;
 
721
         
 
722
         max_value -= weights[index].weight; 
 
723
          
 
724
         if (!max_value) {
 
725
            /*
 
726
            ** set the remaining non-locked scales to zero
 
727
            */
 
728
            for (i=0; i<weights[0].nr; i++) {
 
729
               if (!weights[i].lock && i != index) {
 
730
                  weights[i].weight = 0;
 
731
               }
 
732
            }
 
733
         }
 
734
         else {
 
735
            /*
 
736
            ** calculate the delta
 
737
            ** subtract the old weights
 
738
            ** so delta might be positive or negative
 
739
            */
 
740
            delta = max_value;
 
741
            for (i=0; i<weights[0].nr; i++) {
 
742
               if (!weights[i].lock && i != index) {
 
743
                  delta -= weights[i].weight;
 
744
               }
 
745
            }
 
746
 
 
747
            /*
 
748
            ** divide by number of unlocked scales to have a starting point
 
749
            */
 
750
            delta_part = delta/count;
 
751
 
 
752
            /*
 
753
            ** set the new values
 
754
            */
 
755
            for (i=0; i<weights[0].nr; i++) {
 
756
               if (!weights[i].lock && i != index) {
 
757
                  count--;
 
758
                  weights[i].weight += delta_part;
 
759
                  if (weights[i].weight < 0) {
 
760
                     weights[i].weight = 0;
 
761
                  }
 
762
                  else if (weights[i].weight > max_value) {
 
763
                     weights[i].weight = max_value;
 
764
                  }
 
765
 
 
766
                  max_value -= weights[i].weight;
 
767
                  delta = max_value;
 
768
                  if (max_value) {
 
769
                     for (j=i+1; j<weights[0].nr; j++) {
 
770
                        if (!weights[j].lock && j != index) {
 
771
                           delta -= weights[j].weight;
 
772
                        }
 
773
                     }
 
774
                     if (count)
 
775
                        delta_part = delta/count;
 
776
                     else
 
777
                        delta_part = delta;
 
778
                  }
 
779
                  else {
 
780
                     for (j=i+1; j<weights[0].nr; j++) {
 
781
                        if (!weights[j].lock && j != index) {
 
782
                           weights[j].weight = 0;
 
783
                        }
 
784
                     }
 
785
                     break;
 
786
                  }
 
787
               }
 
788
            }
 
789
         }
 
790
 
 
791
 
 
792
         /*
 
793
         ** the new values have been calculated, display them
 
794
         */
 
795
         for (i=0; i<weights[0].nr; i++) {
 
796
/* printf("weights[%d].weight: %d\n", i, weights[i].weight); */
 
797
            
 
798
            sum += weights[i].weight;
 
799
            XmScaleSetValue(weights[i].scale, 
 
800
                              weights[i].weight);
 
801
         }
 
802
/* printf("Sum: %d\n", sum); */
 
803
      }
 
804
      else {
 
805
         XmScaleSetValue(weights[index].scale,
 
806
                           weights[index].weight);
 
807
      }
 
808
      
 
809
   }
 
810
 
 
811
   DEXIT;
 
812
}
 
813
 
 
814
/*-------------------------------------------------------------------------*/
 
815
Boolean is_empty_word(char *str)
 
816
{
 
817
   if (!str)
 
818
      return 1;
 
819
 
 
820
   while (*str && isspace(*str))
 
821
      str++;
 
822
 
 
823
   return (*str=='\0');
 
824
}
 
825
 
 
826
 
 
827
/*-------------------------------------------------------------------------*/
 
828
void xmui_manage(Widget w)
 
829
{
 
830
   Widget shell;
 
831
 
 
832
   DENTER(GUI_LAYER, "xmui_manage");
 
833
 
 
834
   if (XtIsTopLevelShell(w))
 
835
      shell = w;
 
836
   else
 
837
      shell = XtParent(w);
 
838
 
 
839
   if (XtIsTopLevelShell(shell))
 
840
      XtPopup(shell, XtGrabNone);
 
841
   else
 
842
      XtManageChild(w);
 
843
   
 
844
   if (XtIsRealized(shell))
 
845
      XMapWindow(XtDisplay(shell), XtWindow(shell));
 
846
 
 
847
   /*
 
848
   ** raise window
 
849
   */
 
850
   XRaiseWindow(XtDisplay(shell), XtWindow(shell));
 
851
 
 
852
   DEXIT;
 
853
}
 
854
 
 
855
 
 
856
 
 
857
/*-------------------------------------------------------------------------*/
 
858
void xmui_unmanage(Widget w)
 
859
{
 
860
   Widget shell;
 
861
 
 
862
   DENTER(GUI_LAYER, "xmui_unmanage");
 
863
 
 
864
   if (XtIsTopLevelShell(w))
 
865
      shell = w;
 
866
   else
 
867
      shell = XtParent(w);
 
868
 
 
869
   if (XtIsTopLevelShell(shell))
 
870
      XtPopdown(shell);
 
871
   else
 
872
      XtUnmanageChild(w);
 
873
 
 
874
   DEXIT;
 
875
}
 
876
 
 
877
 
 
878
/*-------------------------------------------------------------------------*/
 
879
void DeleteItems(Widget w, XtPointer cld, XtPointer cad)
 
880
{
 
881
   XmString *selectedItems;
 
882
   Cardinal selectedItemCount;
 
883
   Widget list = (Widget) cld;
 
884
   
 
885
   DENTER(GUI_LAYER, "DeleteItems");
 
886
 
 
887
   XtVaGetValues( list,
 
888
                  XmNselectedItems, &selectedItems,
 
889
                  XmNselectedItemCount, &selectedItemCount,
 
890
                  NULL);
 
891
 
 
892
   if (selectedItemCount)
 
893
      XmListDeleteItems(list, selectedItems, selectedItemCount); 
 
894
 
 
895
   DEXIT;
 
896
}
 
897
 
 
898
/*-------------------------------------------------------------------------*/
 
899
void XmListAddXmStringUniqueSorted(Widget list, XmString xmitem)
 
900
{
 
901
   XmString *items = NULL;
 
902
   int upperBound = 0, lowerBound = 0;
 
903
   String text = NULL, item2 = NULL;
 
904
   
 
905
   DENTER(GUI_LAYER, "AddItemUniqueSorted");
 
906
 
 
907
   if (xmitem) {
 
908
      if (XmListItemExists(list, xmitem)) {
 
909
         DEXIT;
 
910
         return;
 
911
      }
 
912
      if (!XmStringGetLtoR(xmitem, XmFONTLIST_DEFAULT_TAG, &item2)) {
 
913
         DEXIT;
 
914
         return;
 
915
      }   
 
916
 
 
917
      XtVaGetValues( list,
 
918
                  XmNitems, &items,
 
919
                  XmNitemCount, &upperBound,
 
920
                  NULL);
 
921
      /*
 
922
      ** binary search through the items in the list
 
923
      */
 
924
      upperBound--;
 
925
      while (upperBound >= lowerBound) {
 
926
         int i = lowerBound + (upperBound - lowerBound) / 2;
 
927
         /* convert XmString to String */
 
928
         if (!XmStringGetLtoR(items[i], XmFONTLIST_DEFAULT_TAG, &text))
 
929
            break;
 
930
         if (strcmp(text, item2) > 0)
 
931
            upperBound = i - 1; /* text comes after item2 */
 
932
         else
 
933
            lowerBound = i + 1; /* text comes before item2 */
 
934
         XtFree(text); 
 
935
      }
 
936
      XtFree(item2);
 
937
      XmListAddItemUnselected(list, xmitem, lowerBound + 1);
 
938
   }
 
939
 
 
940
   DEXIT;
 
941
}
 
942
 
 
943
/*-------------------------------------------------------------------------*/
 
944
void XmListAddItemUniqueSorted(Widget list, String item)
 
945
{
 
946
   XmString xmitem = NULL;
 
947
   XmString *items = NULL;
 
948
   int upperBound = 0, lowerBound = 0;
 
949
   String text = NULL, item2 = NULL;
 
950
   
 
951
   DENTER(GUI_LAYER, "AddItemUniqueSorted");
 
952
 
 
953
   if (item && item[0] != '\0') {
 
954
      /*
 
955
      ** fix the strange behavior of @f* hostgroup names, this is caused
 
956
      ** by the Xmt font converter that interpretes @f as the beginning of
 
957
      ** a font change
 
958
      */
 
959
      char buf[10000];
 
960
      if (item[0] == '@' && item[1] != '{')
 
961
         sprintf(buf, "@%s", item);
 
962
      else
 
963
         sprintf(buf, item);
 
964
      xmitem = XmtCreateLocalizedXmString(list, buf);
 
965
      if (XmListItemExists(list, xmitem)) {
 
966
         XmStringFree(xmitem);
 
967
         DEXIT;
 
968
         return;
 
969
      }
 
970
      if (!XmStringGetLtoR(xmitem, XmFONTLIST_DEFAULT_TAG, &item2)) {
 
971
         XmStringFree(xmitem);
 
972
         DEXIT;
 
973
         return;
 
974
      }   
 
975
 
 
976
      XtVaGetValues( list,
 
977
                  XmNitems, &items,
 
978
                  XmNitemCount, &upperBound,
 
979
                  NULL);
 
980
      /*
 
981
      ** binary search through the items in the list
 
982
      */
 
983
      upperBound--;
 
984
      while (upperBound >= lowerBound) {
 
985
         int i = lowerBound + (upperBound - lowerBound) / 2;
 
986
         /* convert XmString to String */
 
987
         if (!XmStringGetLtoR(items[i], XmFONTLIST_DEFAULT_TAG, &text))
 
988
            break;
 
989
         if (strcmp(text, item2) > 0)
 
990
            upperBound = i - 1; /* text comes after item2 */
 
991
         else
 
992
            lowerBound = i + 1; /* text comes before item2 */
 
993
         XtFree(text); 
 
994
      }
 
995
      XtFree(item2);
 
996
      XmListAddItemUnselected(list, xmitem, lowerBound + 1);
 
997
      XmStringFree(xmitem);
 
998
   }
 
999
 
 
1000
   DEXIT;
 
1001
}
 
1002
 
 
1003
/*-------------------------------------------------------------------------*/
 
1004
void XmListMoveItemToPos(Widget list, String item, int pos)
 
1005
{
 
1006
   XmString xmitem = NULL;
 
1007
   
 
1008
   DENTER(GUI_LAYER, "XmListMoveItemToPos");
 
1009
 
 
1010
   if (item && item[0] != '\0') {
 
1011
      xmitem = XmtCreateXmString(item);
 
1012
      if (XmListItemExists(list, xmitem)) {
 
1013
         XmListDeleteItem(list, xmitem);
 
1014
         XmListAddItem(list, xmitem, pos);
 
1015
      }
 
1016
      XmStringFree(xmitem);
 
1017
   }   
 
1018
   DEXIT;
 
1019
}
 
1020
 
 
1021
 
 
1022
/*
 
1023
** removes leading and trailing whitespace
 
1024
** the string is changed
 
1025
*/
 
1026
char *qmon_trim(
 
1027
char *s 
 
1028
) {
 
1029
   int len, i;
 
1030
 
 
1031
   if (!s)
 
1032
      return NULL;
 
1033
 
 
1034
   /*
 
1035
   ** remove leading whitespace
 
1036
   */
 
1037
   while (*s && isspace(*s))
 
1038
      s++;
 
1039
      
 
1040
   len = strlen(s) - 1;
 
1041
 
 
1042
   for (i=len; i>0 && *s && isspace(s[i]); i--)
 
1043
      s[i] = '\0';
 
1044
 
 
1045
   return s;
 
1046
}   
 
1047
 
 
1048
 
 
1049
void ForceUpdate(
 
1050
Widget w 
 
1051
) {
 
1052
   Widget diashell, topshell;
 
1053
   Window diawindow, topwindow;
 
1054
   XtAppContext cxt = XtWidgetToApplicationContext(w);
 
1055
   Display *dpy;
 
1056
   XWindowAttributes xwa;
 
1057
   XEvent event;
 
1058
 
 
1059
   for (diashell = w; !XtIsShell(diashell); diashell = XtParent(diashell))
 
1060
      ;
 
1061
   for (topshell = diashell; !XtIsTopLevelShell(topshell); topshell = XtParent(topshell))
 
1062
      ;
 
1063
 
 
1064
   if (XtIsRealized(diashell) && XtIsRealized(topshell)) {
 
1065
      dpy = XtDisplay(topshell);
 
1066
      diawindow = XtWindow(diashell);
 
1067
      topwindow = XtWindow(topshell);
 
1068
 
 
1069
      while (XGetWindowAttributes(dpy, diawindow, &xwa) && xwa.map_state != IsViewable) {
 
1070
         if (XGetWindowAttributes(dpy, topwindow, &xwa) && xwa.map_state != IsViewable)
 
1071
            break;
 
1072
         XtAppNextEvent(cxt, &event);
 
1073
         XtDispatchEvent(&event);
 
1074
      }
 
1075
   }
 
1076
   XmUpdateDisplay(topshell);
 
1077
}
 
1078
 
 
1079
   
 
1080