forked from boardhead/aged
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PLabel.cxx
107 lines (96 loc) · 2.66 KB
/
PLabel.cxx
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
//==============================================================================
// File: PLabel.cxx
//
// Description: A wrapper to avoid unnecessary screen updates if the label doesn't
// change
//
// Created: PH 10/15/99
//
// Copyright (c) 2017, Phil Harvey, Queen's University
//==============================================================================
#include <string.h>
#include <Xm/Label.h>
#include "PLabel.h"
#include "PUtils.h"
PLabel::PLabel(Widget aLabel)
{
mString = NULL;
mLabel = aLabel;
}
PLabel::~PLabel()
{
FreeString();
}
// CreateLabel - create XmLabel widget
// - if managed is 0, the widget is created but not managed
void PLabel::CreateLabel(char *name, Widget parent, ArgList args, Cardinal num_args, int managed)
{
if (managed) {
mLabel = XtCreateManagedWidget(name,xmLabelWidgetClass,parent,args,num_args);
} else {
mLabel = XtCreateWidget(name,xmLabelWidgetClass,parent,args,num_args);
}
// the widget name is the initial string, so save it
SaveString(name);
}
// DestroyLabel - destroy the label widget
void PLabel::DestroyLabel()
{
if (mLabel) {
XtDestroyWidget(mLabel);
mLabel = NULL;
}
FreeString();
}
// CreateString - create string and save length of allocated string
void PLabel::CreateString(int len)
{
mStringLen = len;
mString = XtMalloc(mStringLen + 1); // need extra character for NULL terminator
}
// FreeString - free our copy of the string
void PLabel::FreeString()
{
if (mString) {
XtFree(mString);
mString = NULL;
}
}
// SaveString - save string to our local copy
// - returns zero if string has not changed
int PLabel::SaveString(char *str)
{
int len;
if (mString) {
if (!strcmp(mString, str)) return(0); // text is the same
len = strlen(str);
if (len > mStringLen) {
// only reallocate string memory if new string is larger
FreeString();
CreateString(len);
}
} else {
CreateString(strlen(str));
}
if (mString) {
strcpy(mString, str);
}
return(1);
}
// SetString - set the label string (must create a label before this is called!)
void PLabel::SetString(char *str)
{
if (SaveString(str)) {
setLabelString(mLabel, str); // set the XmLabel string
}
}
// SetStringNow - set the label and draw immediately
// - call SetString() instead of this routine unless you specifically need
// to update the string before the next time through the event loop
void PLabel::SetStringNow(char *str)
{
if (SaveString(str)) {
setLabelString(mLabel, str);
XmUpdateDisplay(mLabel);
}
}