ui/motif/label.c

branch
newapi
changeset 429
0921f8a5d535
parent 428
c7dcd2ab2d3d
child 430
ea949c0109d8
equal deleted inserted replaced
428:c7dcd2ab2d3d 429:0921f8a5d535
31 31
32 #include "label.h" 32 #include "label.h"
33 #include "container.h" 33 #include "container.h"
34 #include "../common/context.h" 34 #include "../common/context.h"
35 #include "../common/object.h" 35 #include "../common/object.h"
36
37 #include "Grid.h"
36 38
37 static UIWIDGET label_create(UiObject *obj, UiLabelArgs args, int align) { 39 static UIWIDGET label_create(UiObject *obj, UiLabelArgs args, int align) {
38 Arg xargs[16]; 40 Arg xargs[16];
39 int n = 0; 41 int n = 0;
40 42
68 } 70 }
69 71
70 UIWIDGET ui_rlabel_create(UiObject* obj, UiLabelArgs args) { 72 UIWIDGET ui_rlabel_create(UiObject* obj, UiLabelArgs args) {
71 return label_create(obj, args, XmALIGNMENT_END); 73 return label_create(obj, args, XmALIGNMENT_END);
72 } 74 }
75
76
77 /* ------------------------------ progressbar ------------------------------ */
78
79 static void ui_destroy_progressbar(Widget w, UiProgressBar *pb, XtPointer d) {
80 // TODO: free other stuff
81 free(pb);
82 }
83
84 static void ui_progressbar_expose(Widget widget, UiProgressBar *pb, XtPointer c) {
85 Display *dp = XtDisplay(widget);
86 Window w = XtWindow(widget);
87 if(!pb->gc) {
88 XGCValues gcvals;
89 gcvals.foreground = pb->color;
90 pb->gc = XCreateGC(dp, w, (GCForeground), &gcvals);
91 }
92
93 Dimension width = widget->core.width;
94 Dimension height = widget->core.height;
95
96 double value = (pb->value - pb->min) / (pb->max - pb->min);
97 Dimension valueW = (double)width * value;
98
99 XClearArea(dp, w, 0, 0, width, height, False);
100 XFillRectangle(dp, w, pb->gc, 0, 0, valueW, widget->core.height);
101 }
102
103 UIWIDGET ui_progressbar_create(UiObject *obj, UiProgressbarArgs args) {
104 Arg xargs[16];
105 int n = 0;
106
107 UiContainerPrivate *ctn = ui_obj_container(obj);
108 UI_APPLY_LAYOUT(ctn->layout, args);
109
110 Widget parent = ctn->prepare(ctn, xargs, &n);
111
112 char *name = args.name ? (char*)args.name : "progressbar";
113 Widget frame = XmCreateFrame(parent, name, xargs, n);
114
115 // create a button and get some informations about the height, shadow, highlight, ....
116 // we want the frame to have the same dimensions as a normal button
117 Widget test = XmCreatePushButton(frame, "button", NULL, 0);
118 XtManageChild(test);
119 Dimension h, highlightThickness, shadowThickness;
120 Pixel highlightColor;
121 XtVaGetValues(test, XmNheight, &h, XmNhighlightThickness, &highlightThickness,
122 XmNshadowThickness, &shadowThickness, XmNhighlightColor, &highlightColor, NULL);
123 XtDestroyWidget(test);
124
125 // adjust frame
126 XtVaSetValues(frame, XmNshadowThickness, shadowThickness, gridMarginLeft, highlightThickness,
127 gridMarginRight, highlightThickness, gridMarginTop, highlightThickness,
128 gridMarginBottom, highlightThickness, NULL);
129
130 // create drawing area
131 Dimension da_height = h - 2*highlightThickness - 2*shadowThickness;
132 n = 0;
133 XtSetArg(xargs[n], XmNheight, da_height); n++;
134 Widget drawingArea = XmCreateDrawingArea(frame, "progressbar_drawingarea", xargs, n);
135 XtManageChild(drawingArea);
136
137 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_DOUBLE);
138
139 UiProgressBar *progressbarData = malloc(sizeof(UiProgressBar));
140 progressbarData->widget = drawingArea;
141 progressbarData->min = args.min;
142 progressbarData->max = args.max == 0 ? 100 : args.max;
143 progressbarData->value = 50;
144 progressbarData->var = var;
145 progressbarData->color = highlightColor;
146 progressbarData->gc = NULL; // initialize on first expose
147
148 if(var) {
149 UiDouble *d = var->value;
150 progressbarData->value = d->value;
151 d->obj = progressbarData;
152 d->get = ui_progressbar_get;
153 d->set = ui_progressbar_set;
154 }
155
156 XtAddCallback(
157 drawingArea,
158 XmNexposeCallback,
159 (XtCallbackProc)ui_progressbar_expose,
160 progressbarData);
161
162
163 XtManageChild(frame);
164 return frame;
165 }
166
167 double ui_progressbar_get(UiDouble *d) {
168 UiProgressBar *pb = d->obj;
169 d->value = pb->value;
170 return d->value;
171 }
172
173 void ui_progressbar_set(UiDouble *d, double value) {
174 UiProgressBar *pb = d->obj;
175 d->value = value;
176 pb->value = value;
177 ui_progressbar_expose(pb->widget, pb, NULL);
178 }

mercurial