Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wbox.c
Commit [+]AuthorDateLineData
19160e8d kojima2000-11-09 05:01:58 +00001
19160e8d kojima2000-11-09 05:01:58 +00002#include "WINGsP.h"
3
19160e8d kojima2000-11-09 05:01:58 +00004typedef struct {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02005 WMView *view;
6 int minSize;
7 int maxSize;
8 int space;
9 unsigned expand:1;
10 unsigned fill:1;
11 unsigned end:1;
19160e8d kojima2000-11-09 05:01:58 +000012} SubviewItem;
13
19160e8d kojima2000-11-09 05:01:58 +000014typedef struct W_Box {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020015 W_Class widgetClass;
16 W_View *view;
19160e8d kojima2000-11-09 05:01:58 +000017
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020018 WMArray *subviews;
19160e8d kojima2000-11-09 05:01:58 +000019
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020020 short borderWidth;
19160e8d kojima2000-11-09 05:01:58 +000021
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020022 unsigned horizontal:1;
19160e8d kojima2000-11-09 05:01:58 +000023} Box;
24
19160e8d kojima2000-11-09 05:01:58 +000025#define DEFAULT_WIDTH 40
26#define DEFAULT_HEIGHT 40
27
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020028static void destroyBox(Box * bPtr);
19160e8d kojima2000-11-09 05:01:58 +000029
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020030static void handleEvents(XEvent * event, void *data);
5c621975 kojima2001-01-11 16:31:23 +000031
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020032static void didResize(struct W_ViewDelegate *, WMView *);
19160e8d kojima2000-11-09 05:01:58 +000033
34static W_ViewDelegate delegate = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020035 NULL,
36 NULL,
37 didResize,
38 NULL,
39 NULL
19160e8d kojima2000-11-09 05:01:58 +000040};
41
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020042WMBox *WMCreateBox(WMWidget * parent)
19160e8d kojima2000-11-09 05:01:58 +000043{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020044 Box *bPtr;
77b8fe05 dan2002-02-20 22:22:40 +000045
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020046 bPtr = wmalloc(sizeof(Box));
47 memset(bPtr, 0, sizeof(Box));
19160e8d kojima2000-11-09 05:01:58 +000048
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020049 bPtr->widgetClass = WC_Box;
19160e8d kojima2000-11-09 05:01:58 +000050
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020051 bPtr->view = W_CreateView(W_VIEW(parent));
52 if (!bPtr->view) {
53 wfree(bPtr);
54 return NULL;
55 }
56 bPtr->view->self = bPtr;
77b8fe05 dan2002-02-20 22:22:40 +000057
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020058 bPtr->view->delegate = &delegate;
77b8fe05 dan2002-02-20 22:22:40 +000059
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020060 bPtr->subviews = WMCreateArrayWithDestructor(2, wfree);
77b8fe05 dan2002-02-20 22:22:40 +000061
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020062 WMCreateEventHandler(bPtr->view, StructureNotifyMask, handleEvents, bPtr);
19160e8d kojima2000-11-09 05:01:58 +000063
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020064 WMResizeWidget(bPtr, DEFAULT_WIDTH, DEFAULT_HEIGHT);
77b8fe05 dan2002-02-20 22:22:40 +000065
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020066 return bPtr;
19160e8d kojima2000-11-09 05:01:58 +000067}
68
77b8fe05 dan2002-02-20 22:22:40 +000069typedef struct {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020070 WMBox *box;
71 int total;
72 int expands;
73 int x, y;
74 int xe, ye;
75 int w, h;
77b8fe05 dan2002-02-20 22:22:40 +000076} BoxData;
77
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020078static void computeExpansion(void *object, void *cdata)
77b8fe05 dan2002-02-20 22:22:40 +000079{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020080 SubviewItem *item = (SubviewItem *) object;
81 BoxData *eData = (BoxData *) cdata;
82
83 eData->total -= item->minSize;
84 eData->total -= item->space;
85 if (item->expand) {
86 eData->expands++;
87 }
77b8fe05 dan2002-02-20 22:22:40 +000088}
89
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020090static void doRearrange(void *object, void *cdata)
77b8fe05 dan2002-02-20 22:22:40 +000091{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020092 SubviewItem *item = (SubviewItem *) object;
93 BoxData *eData = (BoxData *) cdata;
94
95 if (eData->box->horizontal) {
96 eData->w = item->minSize;
97 if (item->expand)
98 eData->w += eData->total / eData->expands;
99 } else {
100 eData->h = item->minSize;
101 if (item->expand)
102 eData->h += eData->total / eData->expands;
103 }
104 if (!item->end) {
105 W_MoveView(item->view, eData->x, eData->y);
106 }
107 W_ResizeView(item->view, eData->w, eData->h);
108 if (eData->box->horizontal) {
109 if (item->end)
110 eData->xe -= eData->w + item->space;
111 else
112 eData->x += eData->w + item->space;
113 } else {
114 if (item->end)
115 eData->ye -= eData->h + item->space;
116 else
117 eData->y += eData->h + item->space;
118 }
119 if (item->end) {
120 W_MoveView(item->view, eData->xe, eData->ye);
121 }
77b8fe05 dan2002-02-20 22:22:40 +0000122}
123
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200124static void rearrange(WMBox * box)
19160e8d kojima2000-11-09 05:01:58 +0000125{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200126 BoxData eData;
127
128 eData.box = box;
129 eData.x = eData.y = box->borderWidth;
130 eData.w = eData.h = 1;
131 eData.expands = 0;
132
133 if (box->horizontal) {
134 eData.ye = box->borderWidth;
135 eData.xe = WMWidgetWidth(box) - box->borderWidth;
136 eData.h = WMWidgetHeight(box) - 2 * box->borderWidth;
137 eData.total = WMWidgetWidth(box) - 2 * box->borderWidth;
138 } else {
139 eData.xe = box->borderWidth;
140 eData.ye = WMWidgetHeight(box) - box->borderWidth;
141 eData.w = WMWidgetWidth(box) - 2 * box->borderWidth;
142 eData.total = WMWidgetHeight(box) - 2 * box->borderWidth;
143 }
144
145 if (eData.w <= 0 || eData.h <= 0 || eData.total <= 0) {
146 return;
147 }
148
149 WMMapArray(box->subviews, computeExpansion, &eData);
150 WMMapArray(box->subviews, doRearrange, &eData);
19160e8d kojima2000-11-09 05:01:58 +0000151}
152
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200153void WMSetBoxBorderWidth(WMBox * box, unsigned width)
19160e8d kojima2000-11-09 05:01:58 +0000154{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200155 if (box->borderWidth != width) {
156 box->borderWidth = width;
157 rearrange(box);
158 }
19160e8d kojima2000-11-09 05:01:58 +0000159}
160
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200161void WMAddBoxSubview(WMBox * bPtr, WMView * view, Bool expand, Bool fill, int minSize, int maxSize, int space)
19160e8d kojima2000-11-09 05:01:58 +0000162{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200163 SubviewItem *subView;
77b8fe05 dan2002-02-20 22:22:40 +0000164
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200165 subView = wmalloc(sizeof(SubviewItem));
166 subView->view = view;
167 subView->minSize = minSize;
168 subView->maxSize = maxSize;
169 subView->expand = expand;
170 subView->fill = fill;
171 subView->space = space;
172 subView->end = 0;
77b8fe05 dan2002-02-20 22:22:40 +0000173
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200174 WMAddToArray(bPtr->subviews, subView);
5c621975 kojima2001-01-11 16:31:23 +0000175
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200176 rearrange(bPtr);
e99511b0 kojima2001-01-02 14:17:26 +0000177}
178
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200179void WMAddBoxSubviewAtEnd(WMBox * bPtr, WMView * view, Bool expand, Bool fill, int minSize, int maxSize, int space)
e99511b0 kojima2001-01-02 14:17:26 +0000180{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200181 SubviewItem *subView;
77b8fe05 dan2002-02-20 22:22:40 +0000182
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200183 subView = wmalloc(sizeof(SubviewItem));
184 subView->view = view;
185 subView->minSize = minSize;
186 subView->maxSize = maxSize;
187 subView->expand = expand;
188 subView->fill = fill;
189 subView->space = space;
190 subView->end = 1;
77b8fe05 dan2002-02-20 22:22:40 +0000191
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200192 WMAddToArray(bPtr->subviews, subView);
5c621975 kojima2001-01-11 16:31:23 +0000193
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200194 rearrange(bPtr);
19160e8d kojima2000-11-09 05:01:58 +0000195}
196
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200197static int matchView(void *item, void *cdata)
77b8fe05 dan2002-02-20 22:22:40 +0000198{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200199 return (((SubviewItem *) item)->view == (WMView *) cdata);
77b8fe05 dan2002-02-20 22:22:40 +0000200}
201
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200202void WMRemoveBoxSubview(WMBox * bPtr, WMView * view)
5c621975 kojima2001-01-11 16:31:23 +0000203{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200204 if (WMRemoveFromArrayMatching(bPtr->subviews, matchView, view)) {
205 rearrange(bPtr);
206 }
5c621975 kojima2001-01-11 16:31:23 +0000207}
208
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200209void WMSetBoxHorizontal(WMBox * box, Bool flag)
19160e8d kojima2000-11-09 05:01:58 +0000210{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200211 /* make sure flag is either 0 or 1 no matter what true value was passed */
212 flag = ((flag == 0) ? 0 : 1);
213 if (box->horizontal != flag) {
214 box->horizontal = flag;
215 rearrange(box);
216 }
19160e8d kojima2000-11-09 05:01:58 +0000217}
218
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200219static void destroyBox(Box * bPtr)
19160e8d kojima2000-11-09 05:01:58 +0000220{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200221 WMFreeArray(bPtr->subviews);
222 wfree(bPtr);
19160e8d kojima2000-11-09 05:01:58 +0000223}
224
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200225static void didResize(struct W_ViewDelegate *delegate, WMView * view)
19160e8d kojima2000-11-09 05:01:58 +0000226{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200227 rearrange(view->self);
19160e8d kojima2000-11-09 05:01:58 +0000228}
229
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200230static void handleEvents(XEvent * event, void *data)
19160e8d kojima2000-11-09 05:01:58 +0000231{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200232 Box *bPtr = (Box *) data;
19160e8d kojima2000-11-09 05:01:58 +0000233
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200234 CHECK_CLASS(data, WC_Box);
19160e8d kojima2000-11-09 05:01:58 +0000235
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200236 switch (event->type) {
237 case DestroyNotify:
238 destroyBox(bPtr);
239 break;
6830b057 dan2004-10-12 21:28:27 +0000240
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200241 case ConfigureNotify:
242 rearrange(bPtr);
243 break;
244 }
19160e8d kojima2000-11-09 05:01:58 +0000245}