1
/*___INFO__MARK_BEGIN__*/
2
/*************************************************************************
4
* The Contents of this file are made available subject to the terms of
5
* the Sun Industry Standards Source License Version 1.2
7
* Sun Microsystems Inc., March, 2001
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
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.
24
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
26
* Copyright: 2001 by Sun Microsystems, Inc.
28
* All Rights Reserved.
30
************************************************************************/
31
/*___INFO__MARK_END__*/
39
#include <Xm/ToggleB.h>
42
#include <Xmt/Layout.h>
44
#include "qmon_util.h"
45
#include "qmon_cull.h"
46
#include "qmon_rmon.h"
51
/*-------------------------------------------------------------------------*/
52
void XmListDeletePositions(Widget list, int *pos_list, int pos_count)
56
for (i=0; i<pos_count; i++) {
57
XmListDeleteItemsPos(list, 1, pos_list[i] - i);
63
/*-------------------------------------------------------------------------*/
64
/*-------------------------------------------------------------------------*/
65
String* XmStringTableToStringTable(XmStringTable items, Cardinal itemCount,
69
String* str_table = NULL;
71
DENTER(GUI_LAYER, "XmStringTableToStringTable");
73
if (!itemCount || !items) {
74
DPRINTF(("itemCount/items is 0/NULL\n"));
79
str_table = (String*) XtMalloc(sizeof(String) * itemCount);
82
DPRINTF(("malloc failure\n"));
87
for (i=0; i<itemCount; i++) {
88
if (!XmStringGetLtoR(items[i], tag ? tag : XmFONTLIST_DEFAULT_TAG,
90
DPRINTF(("XmStringGetLtoR failure\n"));
91
StringTableFree(str_table, i);
101
/*-------------------------------------------------------------------------*/
102
XmString *StringTableToXmStringTable(String *items, Cardinal itemCount,
105
XmString *xstr_table;
108
DENTER(GUI_LAYER, "StringTableToXmStringTable");
110
if (!itemCount || !items) {
111
DPRINTF(("itemCount/items is 0/NULL\n"));
116
xstr_table = (XmString*) XtMalloc(sizeof(XmString) * itemCount);
119
DPRINTF(("malloc failure\n"));
124
for (i=0; i<itemCount; i++) {
125
xstr_table[i] = XmStringCreateLtoR( items[i],
126
tag ? tag : XmFONTLIST_DEFAULT_TAG);
133
/*-------------------------------------------------------------------------*/
134
void StringTableFree( String *str_table, Cardinal str_table_size)
138
DENTER(GUI_LAYER, "StringTableFree");
140
for (i=0; i<str_table_size; i++)
141
XtFree((char *) str_table[i]);
143
XtFree((char*)str_table);
148
/*-------------------------------------------------------------------------*/
149
void XmStringTableFree(XmString *xstr_table, Cardinal xstr_table_size)
153
DENTER(GUI_LAYER, "XmStringTableFree");
155
for (i=0; i<xstr_table_size; i++)
157
XmStringFree(xstr_table[i]);
159
XtFree((char*)xstr_table);
166
/*-------------------------------------------------------------------------*/
167
Boolean AddStringToXmList(Widget list, String str, XmStringCharSet tag,
173
DENTER(GUI_LAYER, "AddStringToXmList");
175
item = XmStringCreateLtoR(str, tag);
176
if ( ! XmListItemExists(list, item)) {
177
XmListAddItem(list, item, list_pos);
187
/*-------------------------------------------------------------------------*/
188
void UpdateXmList(Widget list, String *str_table, Cardinal size,
191
XmString *items = NULL;
193
DENTER(GUI_LAYER, "UpdateXmList");
196
** replace the old items
198
items = StringTableToXmStringTable(str_table, size, tag);
205
XmStringTableFree(items, size);
210
XmNitemCount, &itemCount,
213
DPRINTF(("UpdateXmList: itemCount = %d\n", itemCount));
215
for (i=0; i<size; i++) {
216
/* strings are equal */
218
DPRINTF(("in_str = '%s'\n", str_table[i]));
220
if (items[i] && XmStringGetLtoR(items[i], tag, &str1)) {
221
DPRINTF(("str1='%s'\n", str1));
223
if (!strcmp(str1, str_table[i])) {
225
XtFree((char *)str1);
231
XtFree((char *)str1);
236
XmListDeletePos(list, i+1); /* first element is 1 */
239
new = XmStringCreateLtoR(str_table[i], tag);
241
XmListAddItem(list, new, i+1);
246
/* size < itemCount -> delete the rest */
249
XmListDeleteItemsPos(list, itemCount-size, i+1);
253
for (;i<itemCount;i++) {
254
XmListDeletePos(list, i+1);
263
/*-------------------------------------------------------------------------*/
264
Boolean UpdateXmListFromCull(Widget list, XmStringCharSet tag, lList *lp,
272
const lDescr *listDescriptor = NULL;
274
Boolean found = False;
276
DENTER(GUI_LAYER, "UpdateXmListFromCull");
277
XtVaGetValues(list, XmNitemCount, &itemCount, NULL);
280
XmListDeleteAllItems(list);
282
if (lGetNumberOfElem(lp) <= 0) {
287
for (lw = XtParent(list); lw; lw = XtParent(lw)) {
288
if (XtIsSubclass(lw, xmtLayoutWidgetClass)) {
295
XmtLayoutDisableLayout(lw);
297
listDescriptor = lGetListDescr(lp);
298
pos = lGetPosInDescr(listDescriptor, field);
299
dataType = lGetPosType(listDescriptor,pos);
300
for (ep=lFirst(lp); ep; ep=lNext(ep)) {
303
str = lGetPosString(ep, pos);
306
str = lGetPosHost(ep, pos);
309
str = "(null) UpdateXmListFromCull";
310
DPRINTF(("UpdateXmListFromCull: unexpected data type\n"));
314
XmListAddItemUniqueSorted(list, (String)str);
315
DPRINTF(("UpdateXmListFromCull: str = '%s'\n", str));
319
XmtLayoutEnableLayout(lw);
325
/*-------------------------------------------------------------------------*/
326
lList *XmStringToCull(Widget list, lDescr *dp, int nm, int selected)
335
DENTER(GUI_LAYER, "XmStringToCull");
337
if (selected == ALL_ITEMS)
340
XmNitemCount, &itemCount,
344
XmNselectedItems, &items,
345
XmNselectedItemCount, &itemCount,
349
strings = XmStringTableToStringTable(items, itemCount,
350
XmFONTLIST_DEFAULT_TAG);
352
for (i=0; i<itemCount; i++) {
353
dataType = lGetPosType(dp, lGetPosInDescr(dp, nm));
356
lAddElemStr(&lp, nm, strings[i], dp);
359
lAddElemHost(&lp, nm, strings[i], dp);
362
DPRINTF(("XmStringToCull - unexpected data type\n"));
366
StringTableFree(strings, itemCount);
377
/*-------------------------------------------------------------------------*/
378
Boolean DelStringFromXmList(Widget list, String str, XmStringCharSet tag,
383
DENTER(GUI_LAYER, "DelStringFromXmList");
385
if (!list_pos && !str) {
391
XmListDeletePos(list, list_pos);
394
item = XmStringCreateLtoR(str, tag);
395
XmListDeleteItem(list, item);
403
/*-------------------------------------------------------------------------*/
404
void XmListAddTextItem(Widget list_w, String item, XmStringCharSet tag,
409
str = XmStringCreateLtoR(item, tag);
411
XmListAddItem(list_w, str, position);
416
/*-------------------------------------------------------------------------*/
417
void XmListAddTextItems(Widget list_w, String *items, Cardinal itemCount,
418
XmStringCharSet tag, int position)
422
str = StringTableToXmStringTable(items, itemCount, tag);
424
XmListAddItems(list_w, str, itemCount, position);
426
XmStringTableFree(str, itemCount);
430
/*-------------------------------------------------------------------------*/
431
void XmListAddTextItemUnselected(Widget list_w, String item,
432
XmStringCharSet tag, int position)
437
str = XmStringCreateLtoR(item, tag);
439
XmListAddItemUnselected(list_w, str, position);
444
/*-------------------------------------------------------------------------*/
445
void XmListAddTextItemsUnselected(Widget list_w, String *items,
446
Cardinal itemCount, XmStringCharSet tag, int position)
450
str = StringTableToXmStringTable(items, itemCount, tag);
452
XmListAddItemsUnselected(list_w, str, itemCount, position);
454
XmStringTableFree(str, itemCount);
458
/*-------------------------------------------------------------------------*/
459
void XmListSelectTextItem(Widget list_w, String item, XmStringCharSet tag,
464
str = XmStringCreateLtoR(item, tag);
466
XmListSelectItem(list_w, str, notify);
471
/*-------------------------------------------------------------------------*/
472
void qmonPositionDialog(Widget dialog, XtPointer cld, XtPointer cad)
477
DENTER(GUI_LAYER, "qmonPositionDialog");
479
XtVaGetValues(dialog, XmNwidth, &w, XmNheight, &h, NULL);
481
ws = WidthOfScreen(XtScreen(dialog));
482
hs = HeightOfScreen(XtScreen(dialog));
493
XtVaSetValues(dialog, XmNx, x, XmNy, y, NULL);
498
/*-------------------------------------------------------------------------*/
499
void SetMinShellSize(Widget shell, XtPointer cld, XEvent *event, Boolean *ctd)
501
XConfigureEvent *cevent = (XConfigureEvent*)event;
503
DENTER(GUI_LAYER, "SetMinShellSize");
505
if (cevent->type != ConfigureNotify) {
511
XtVaSetValues( shell,
512
XmNminWidth, cevent->width,
513
XmNminHeight, cevent->height,
516
else if ((long)cld == SHELL_WIDTH) {
517
XtVaSetValues( shell,
518
XmNminWidth, cevent->width,
521
else if ((long)cld == SHELL_HEIGHT) {
522
XtVaSetValues( shell,
523
XmNminHeight, cevent->height,
527
XtRemoveEventHandler(shell, StructureNotifyMask, False,
528
SetMinShellSize, NULL);
533
/*-------------------------------------------------------------------------*/
534
void SetMaxShellSize(Widget shell, XtPointer cld, XEvent *event, Boolean *ctd)
536
XConfigureEvent *cevent = (XConfigureEvent*)event;
538
DENTER(GUI_LAYER, "SetMinShellSize");
540
if (cevent->type != ConfigureNotify) {
546
XtVaSetValues( shell,
547
XmNmaxWidth, cevent->width,
548
XmNmaxHeight, cevent->height,
551
else if ((long)cld == SHELL_WIDTH) {
552
XtVaSetValues( shell,
553
XmNmaxWidth, cevent->width,
556
else if ((long)cld == SHELL_HEIGHT) {
557
XtVaSetValues( shell,
558
XmNmaxHeight, cevent->height,
562
XtRemoveEventHandler(shell, StructureNotifyMask, False,
563
SetMaxShellSize, NULL);
569
/*-------------------------------------------------------------------------*/
570
int qmonForkEditor(String file)
574
char command[BUFSIZ];
575
static char default_editor[] = "vi";
578
DENTER(GUI_LAYER, "qmonForkEditor");
580
if (!(editor = getenv("EDITOR")))
581
editor = default_editor;
583
cp = strrchr(editor, '/');
584
if (cp && !strcmp(cp + 1, "jot"))
585
strcpy(command, editor);
587
sprintf(command, "xterm -e %s", editor);
590
sprintf(command, "%s %s &", command, file ? file : "");
592
strcat(command, "&");
594
status = system(command);
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)
607
DENTER(GUI_LAYER, "qmonDrawRequestEntry");
609
/* GET THE BOUNDING BOX */
610
XmStringExtent(fontlist, str, width, height);
612
/* DRAW RECTANGLE AND LABEL */
613
XmStringDrawImage(dpy, da, fontlist, str, gc, x, y, *width,
614
layout, XmSTRING_DIRECTION_L_TO_R,
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)
625
DENTER(GUI_LAYER, "qmonXmStringDraw");
627
/* GET THE BOUNDING BOX */
628
XmStringExtent(fontlist, str, width, height);
630
/* DRAW RECTANGLE AND LABEL */
631
XmStringDraw(dpy, da, fontlist, str, gc, x, y, *width,
632
layout, XmSTRING_DIRECTION_L_TO_R,
639
/*-------------------------------------------------------------------------*/
640
void setButtonLabel(Widget w, String label)
644
DENTER(GUI_LAYER, "setButtonLabel");
646
xlab = XmtCreateLocalizedXmString(w, label);
647
XtVaSetValues(w, XmNlabelString, xlab, NULL);
654
/*-------------------------------------------------------------------------*/
655
void qmonScaleWeightToggle(Widget w, XtPointer cld, XtPointer cad)
657
XmToggleButtonCallbackStruct *cbs = (XmToggleButtonCallbackStruct*)cad;
658
tScaleWeight *weights = (tScaleWeight*) cld;
662
DENTER(GUI_LAYER, "qmonScaleWeightToggle");
664
XtVaGetValues(w, XmNuserData, &ip, NULL);
666
DPRINTF(("index = %d\n", index));
669
weights[index].lock = 1;
670
XtSetSensitive(weights[index].scale, False);
673
weights[index].lock = 0;
674
XtSetSensitive(weights[index].scale, True);
680
/*-------------------------------------------------------------------------*/
681
void qmonScaleWeight(Widget w, XtPointer cld, XtPointer cad)
683
XmScaleCallbackStruct *cbs = (XmScaleCallbackStruct*)cad;
684
tScaleWeight *weights = (tScaleWeight*) cld;
694
DENTER(GUI_LAYER, "qmonScaleWeight");
696
XtVaGetValues(w, XmNuserData, &ip, NULL);
698
DPRINTF(("index = %d\n", index));
700
if (cbs->value != weights[index].weight) {
703
** get the max_value and the number of non-locked scales
705
for (i=0; i<weights[0].nr; i++) {
706
if (!weights[i].lock) {
707
max_value += weights[i].weight;
717
if (cbs->value > max_value)
718
weights[index].weight = max_value;
720
weights[index].weight = cbs->value;
722
max_value -= weights[index].weight;
726
** set the remaining non-locked scales to zero
728
for (i=0; i<weights[0].nr; i++) {
729
if (!weights[i].lock && i != index) {
730
weights[i].weight = 0;
736
** calculate the delta
737
** subtract the old weights
738
** so delta might be positive or negative
741
for (i=0; i<weights[0].nr; i++) {
742
if (!weights[i].lock && i != index) {
743
delta -= weights[i].weight;
748
** divide by number of unlocked scales to have a starting point
750
delta_part = delta/count;
753
** set the new values
755
for (i=0; i<weights[0].nr; i++) {
756
if (!weights[i].lock && i != index) {
758
weights[i].weight += delta_part;
759
if (weights[i].weight < 0) {
760
weights[i].weight = 0;
762
else if (weights[i].weight > max_value) {
763
weights[i].weight = max_value;
766
max_value -= weights[i].weight;
769
for (j=i+1; j<weights[0].nr; j++) {
770
if (!weights[j].lock && j != index) {
771
delta -= weights[j].weight;
775
delta_part = delta/count;
780
for (j=i+1; j<weights[0].nr; j++) {
781
if (!weights[j].lock && j != index) {
782
weights[j].weight = 0;
793
** the new values have been calculated, display them
795
for (i=0; i<weights[0].nr; i++) {
796
/* printf("weights[%d].weight: %d\n", i, weights[i].weight); */
798
sum += weights[i].weight;
799
XmScaleSetValue(weights[i].scale,
802
/* printf("Sum: %d\n", sum); */
805
XmScaleSetValue(weights[index].scale,
806
weights[index].weight);
814
/*-------------------------------------------------------------------------*/
815
Boolean is_empty_word(char *str)
820
while (*str && isspace(*str))
827
/*-------------------------------------------------------------------------*/
828
void xmui_manage(Widget w)
832
DENTER(GUI_LAYER, "xmui_manage");
834
if (XtIsTopLevelShell(w))
839
if (XtIsTopLevelShell(shell))
840
XtPopup(shell, XtGrabNone);
844
if (XtIsRealized(shell))
845
XMapWindow(XtDisplay(shell), XtWindow(shell));
850
XRaiseWindow(XtDisplay(shell), XtWindow(shell));
857
/*-------------------------------------------------------------------------*/
858
void xmui_unmanage(Widget w)
862
DENTER(GUI_LAYER, "xmui_unmanage");
864
if (XtIsTopLevelShell(w))
869
if (XtIsTopLevelShell(shell))
878
/*-------------------------------------------------------------------------*/
879
void DeleteItems(Widget w, XtPointer cld, XtPointer cad)
881
XmString *selectedItems;
882
Cardinal selectedItemCount;
883
Widget list = (Widget) cld;
885
DENTER(GUI_LAYER, "DeleteItems");
888
XmNselectedItems, &selectedItems,
889
XmNselectedItemCount, &selectedItemCount,
892
if (selectedItemCount)
893
XmListDeleteItems(list, selectedItems, selectedItemCount);
898
/*-------------------------------------------------------------------------*/
899
void XmListAddXmStringUniqueSorted(Widget list, XmString xmitem)
901
XmString *items = NULL;
902
int upperBound = 0, lowerBound = 0;
903
String text = NULL, item2 = NULL;
905
DENTER(GUI_LAYER, "AddItemUniqueSorted");
908
if (XmListItemExists(list, xmitem)) {
912
if (!XmStringGetLtoR(xmitem, XmFONTLIST_DEFAULT_TAG, &item2)) {
919
XmNitemCount, &upperBound,
922
** binary search through the items in the list
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))
930
if (strcmp(text, item2) > 0)
931
upperBound = i - 1; /* text comes after item2 */
933
lowerBound = i + 1; /* text comes before item2 */
937
XmListAddItemUnselected(list, xmitem, lowerBound + 1);
943
/*-------------------------------------------------------------------------*/
944
void XmListAddItemUniqueSorted(Widget list, String item)
946
XmString xmitem = NULL;
947
XmString *items = NULL;
948
int upperBound = 0, lowerBound = 0;
949
String text = NULL, item2 = NULL;
951
DENTER(GUI_LAYER, "AddItemUniqueSorted");
953
if (item && item[0] != '\0') {
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
960
if (item[0] == '@' && item[1] != '{')
961
sprintf(buf, "@%s", item);
964
xmitem = XmtCreateLocalizedXmString(list, buf);
965
if (XmListItemExists(list, xmitem)) {
966
XmStringFree(xmitem);
970
if (!XmStringGetLtoR(xmitem, XmFONTLIST_DEFAULT_TAG, &item2)) {
971
XmStringFree(xmitem);
978
XmNitemCount, &upperBound,
981
** binary search through the items in the list
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))
989
if (strcmp(text, item2) > 0)
990
upperBound = i - 1; /* text comes after item2 */
992
lowerBound = i + 1; /* text comes before item2 */
996
XmListAddItemUnselected(list, xmitem, lowerBound + 1);
997
XmStringFree(xmitem);
1003
/*-------------------------------------------------------------------------*/
1004
void XmListMoveItemToPos(Widget list, String item, int pos)
1006
XmString xmitem = NULL;
1008
DENTER(GUI_LAYER, "XmListMoveItemToPos");
1010
if (item && item[0] != '\0') {
1011
xmitem = XmtCreateXmString(item);
1012
if (XmListItemExists(list, xmitem)) {
1013
XmListDeleteItem(list, xmitem);
1014
XmListAddItem(list, xmitem, pos);
1016
XmStringFree(xmitem);
1023
** removes leading and trailing whitespace
1024
** the string is changed
1035
** remove leading whitespace
1037
while (*s && isspace(*s))
1040
len = strlen(s) - 1;
1042
for (i=len; i>0 && *s && isspace(s[i]); i--)
1052
Widget diashell, topshell;
1053
Window diawindow, topwindow;
1054
XtAppContext cxt = XtWidgetToApplicationContext(w);
1056
XWindowAttributes xwa;
1059
for (diashell = w; !XtIsShell(diashell); diashell = XtParent(diashell))
1061
for (topshell = diashell; !XtIsTopLevelShell(topshell); topshell = XtParent(topshell))
1064
if (XtIsRealized(diashell) && XtIsRealized(topshell)) {
1065
dpy = XtDisplay(topshell);
1066
diawindow = XtWindow(diashell);
1067
topwindow = XtWindow(topshell);
1069
while (XGetWindowAttributes(dpy, diawindow, &xwa) && xwa.map_state != IsViewable) {
1070
if (XGetWindowAttributes(dpy, topwindow, &xwa) && xwa.map_state != IsViewable)
1072
XtAppNextEvent(cxt, &event);
1073
XtDispatchEvent(&event);
1076
XmUpdateDisplay(topshell);