Fixed crashes in 24 and 32bpp after introduction of hermeslib support
[wmaker-crm.git] / src / geomview.c
blob21d1e420ac2b75dcc3df14cfeb5247c2818d7fe0
4 #include <WINGs/WINGsP.h>
8 typedef struct W_GeometryView {
9 W_Class widgetClass;
10 WMView *view;
12 GC gc;
13 WMFont *font;
15 WMSize textSize;
17 union {
18 struct {
19 int x, y;
20 } pos;
21 struct {
22 unsigned width, height;
23 } size;
24 } data;
26 unsigned showPosition:1;
27 } WGeometryView;
31 static void handleEvents(XEvent *event, void *clientData);
32 static void paint(WGeometryView *gview);
35 WGeometryView*
36 WCreateGeometryView(WMScreen *scr)
38 WGeometryView *gview;
39 char buffer[64];
40 static W_Class widgetClass = 0;
42 if (!widgetClass) {
43 widgetClass = W_RegisterUserWidget();
46 gview = malloc(sizeof(WGeometryView));
47 if (!gview) {
48 return NULL;
50 memset(gview, 0, sizeof(WGeometryView));
52 gview->widgetClass = widgetClass;
54 gview->view = W_CreateTopView(scr);
55 if (!gview->view) {
56 wfree(gview);
58 return NULL;
60 gview->view->self = gview;
62 gview->font = WMSystemFontOfSize(scr, 12);
63 if (!gview->font) {
64 W_DestroyView(gview->view);
65 wfree(gview);
67 return NULL;
71 WMColor *black = WMBlackColor(scr);
72 WMColor *gray = WMGrayColor(scr);
73 XGCValues gcv;
75 gcv.foreground = WMColorPixel(black);
76 gcv.background = WMColorPixel(gray);
78 gview->gc = XCreateGC(WMScreenDisplay(scr), W_DRAWABLE(scr),
79 GCForeground|GCBackground, &gcv);
81 WMReleaseColor(black);
82 WMReleaseColor(gray);
85 WMCreateEventHandler(gview->view, ExposureMask, handleEvents, gview);
87 sprintf(buffer, "%+05i, %+05i", 0, 0);
89 gview->textSize.width = WMWidthOfString(gview->font, buffer,
90 strlen(buffer));
91 gview->textSize.height = WMFontHeight(gview->font);
93 W_ResizeView(gview->view, gview->textSize.width+8,
94 gview->textSize.height+6);
96 return gview;
100 void
101 WSetGeometryViewShownPosition(WGeometryView *gview, int x, int y)
103 gview->showPosition = 1;
104 gview->data.pos.x = x;
105 gview->data.pos.y = y;
107 paint(gview);
111 void
112 WSetGeometryViewShownSize(WGeometryView *gview,
113 unsigned width, unsigned height)
115 gview->showPosition = 0;
116 gview->data.size.width = width;
117 gview->data.size.height = height;
119 paint(gview);
124 static void
125 paint(WGeometryView *gview)
127 char buffer[64];
129 if (gview->showPosition) {
130 sprintf(buffer, "%+5i , %+5i ", gview->data.pos.x, gview->data.pos.y);
131 } else {
132 sprintf(buffer, "%+5i x %+5i ", gview->data.size.width, gview->data.size.height);
135 WMDrawImageString(W_VIEW_SCREEN(gview->view),
136 W_VIEW_DRAWABLE(gview->view),
137 gview->gc, gview->font,
138 (W_VIEW_WIDTH(gview->view)-gview->textSize.width)/2,
139 (W_VIEW_HEIGHT(gview->view)-gview->textSize.height)/2,
140 buffer, strlen(buffer));
142 W_DrawRelief(W_VIEW_SCREEN(gview->view), W_VIEW_DRAWABLE(gview->view),
143 0, 0, W_VIEW_WIDTH(gview->view), W_VIEW_HEIGHT(gview->view),
144 WRRaised);
149 static void
150 handleEvents(XEvent *event, void *clientData)
152 WGeometryView *gview = (WGeometryView*)clientData;
154 switch (event->type) {
155 case Expose:
156 paint(gview);
157 break;