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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/*
Copyright (C) 1999-2001 Ulric Eriksson <ulric@siag.nu>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the Licence, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
*/
/*
* This widget was written by me, Ulric, by modifying the Gridbox widget
* by Edward A Falk, hence this separate copyright notice for my code.
*
* Unlike the original, this widget does not care what the children
* would like for a size, but simply assigns positions and sizes
* from two resources describing widths and heights. The idea is to
* be able to handle a) widgets that insist on being a size that is
* inappropriate for the application and b) widgets that are not
* clueful enough to figure out what size they should be.
*
* Example of a layout resource:
*
* "25 25 50% 50% 15"
*
* Grid has 5 columns. The first and second are 25 pixels each. The
* fifth is 15 pixels. The third and fourth share whatever is left.
*/
/*
* MwRudegrid.c - MwRudegrid composite widget
*
* The MwRudegrid widget aligns its children in a rectangular array
* of cells. Child widgets occupy a rectangular region of cells (default
* size is 1x1). A typical layout might look like this:
*
* +-------+-------+--+----------------------------+
* | | | | |
* |-------+-------| | |
* | | | | |
* |-------+-------| | |
* | | | | |
* |-------+-------| | |
* | | | | |
* |-------+-------+--+----------------------------|
* |__________________|____________________________|
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Xmu/Converters.h>
#include <X11/Xmu/CharSet.h>
#include "MwUtils.h"
#include "MwRudegridP.h"
#define DEFAULT_HEIGHT 100
#define DEFAULT_WIDTH 100
#define REALLYSMALL 2
#define Offset(field) XtOffsetOf(MwRudegridRec, rudegrid.field)
static XtResource resources[] = {
{
XtNxLayout,
XtCXLayout,
XtRString,
sizeof(String),
Offset(x_layout),
XtRImmediate,
(String)NULL
}, {
XtNyLayout,
XtCYLayout,
XtRString,
sizeof(String),
Offset(y_layout),
XtRImmediate,
(String)NULL
},
};
#undef Offset
#define Offset(field) XtOffsetOf(MwRudegridConstraintsRec, rudegrid.field)
static XtResource rudegridConstraintResources[] = {
{
XtNgridx,
XtCPosition,
XtRPosition,
sizeof(Position),
Offset(gridx),
XtRImmediate,
(XtPointer)0
}, {
XtNgridy,
XtCPosition,
XtRPosition,
sizeof(Position),
Offset(gridy),
XtRImmediate,
(XtPointer)0
}, {
XtNgridWidth,
XtCWidth,
XtRDimension,
sizeof(Dimension),
Offset(gridWidth),
XtRImmediate,
(XtPointer)1
}, {
XtNgridHeight,
XtCHeight,
XtRDimension,
sizeof(Dimension),
Offset(gridHeight),
XtRImmediate,
(XtPointer)1
},
};
#undef Offset
static void MwRudegridResize(Widget w) ;
static Boolean MwRudegridSetValues(Widget, Widget, Widget, ArgList, Cardinal *) ;
static void MwRudegridChangeManaged(Widget w) ;
static XtGeometryResult
MwRudegridGeometryManager(Widget, XtWidgetGeometry *, XtWidgetGeometry *) ;
static Boolean
MwRudegridConstraintSetValues(Widget, Widget, Widget, ArgList, Cardinal *);
static void Initialize(Widget, Widget, ArgList, Cardinal *);
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#endif
MwRudegridClassRec mwRudegridClassRec = {
{ /* core_class fields */
/* superclass */ (WidgetClass) &constraintClassRec,
/* class_name */ "MwRudegrid",
/* widget_size */ sizeof(MwRudegridRec),
/* class_initialize */ NULL,
/* class_part_init */ NULL,
/* class_inited */ FALSE,
/* initialize */ Initialize/*NULL*/,
/* initialize_hook */ NULL,
/* realize */ XtInheritRealize,
/* actions */ NULL,
/* num_actions */ 0,
/* resources */ resources,
/* num_resources */ XtNumber(resources),
/* xrm_class */ NULLQUARK,
/* compress_motion */ TRUE,
/* compress_exposure */ TRUE,
/* compress_enterleave*/ TRUE,
/* visible_interest */ FALSE,
/* destroy */ NULL,
/* resize */ MwRudegridResize,
/* expose */ NULL,
/* set_values */ MwRudegridSetValues,
/* set_values_hook */ NULL,
/* set_values_almost */ XtInheritSetValuesAlmost,
/* get_values_hook */ NULL,
/* accept_focus */ NULL,
/* version */ XtVersion,
/* callback_private */ NULL,
/* tm_table */ NULL,
/* query_geometry */ XtInheritQueryGeometry/*MwRudegridQueryGeometry*/,
/* display_accelerator*/ XtInheritDisplayAccelerator,
/* extension */ NULL
},
{ /* composite_class fields */
/* geometry_manager */ MwRudegridGeometryManager,
/* change_managed */ MwRudegridChangeManaged,
/* insert_child */ XtInheritInsertChild,
/* delete_child */ XtInheritDeleteChild,
/* extension */ NULL
},
{ /* constraint_class fields */
/* subresources */ rudegridConstraintResources,
/* subresource_count */ XtNumber(rudegridConstraintResources),
/* constraint_size */ sizeof(MwRudegridConstraintsRec),
/* initialize */ NULL,
/* destroy */ NULL,
/* set_values */ MwRudegridConstraintSetValues,
/* extension */ NULL
},
{ /* rudegrid_class fields */
/* extension */ NULL
}
};
WidgetClass mwRudegridWidgetClass = (WidgetClass)&mwRudegridClassRec;
/****************************************************************
*
* Class Procedures
*
****************************************************************/
/*
Allocate and return an array of column or row dimensions.
Caller must free.
*/
static int *parse_layout(int totw, char *layout, int *ncols)
{
int w, uw, col, *widths, *cols;
char *p, *q;
int nw, nc, i;
uw = 0; /* width used so far */
widths = NULL; /* widths we have figured out */
nw = 0; /* number of column widths */
cols = NULL; /* column boundaries */
nc = 0; /* number of boundaries */
p = layout;
if (p == NULL) p = "";
w = strtol(p, &q, 10);
while (p != q) {
if (*q == '%') {
w = -w;
++q;
} else {
uw += w;
}
widths = realloc(widths, (nw+1)*sizeof *widths);
widths[nw++] = w;
p = q;
w = strtol(p, &q, 10);
}
cols = malloc((nw+2)*sizeof *cols);
col = 0;
cols[nc++] = col;
for (i = 0; i < nw; i++) {
if (widths[i] < 0) widths[i] = -widths[i]*(totw-uw)/100;
col += widths[i];
cols[nc++] = col;
}
cols[nc++] = totw;
free(widths);
*ncols = nc;
return cols;
}
static void DoLayout(Widget w)
{
MwRudegridWidget rw = (MwRudegridWidget)w;
Widget child;
int *cols, *rows;
int x, y;
int w1, h1;
int gx, gy;
int gw, gh;
int i, ncols, nrows;
MwRudegridConstraints rc;
cols = parse_layout(rw->core.width, rw->rudegrid.x_layout, &ncols);
rows = parse_layout(rw->core.height, rw->rudegrid.y_layout, &nrows);
for (i = 0; i < rw->composite.num_children; i++) {
child = rw->composite.children[i];
if (!child->core.managed) continue;
rc = (MwRudegridConstraints)child->core.constraints;
gx = rc->rudegrid.gridx;
if (gx < 0) gx = 0;
if (gx >= ncols) gx = ncols-1;
gy = rc->rudegrid.gridy;
if (gy < 0) gy = 0;
if (gy >= nrows) gy = nrows-1;
gw = rc->rudegrid.gridWidth;
if (gw < 0) gw = 0;
if (gw+gx >= ncols) gw = ncols-gx-1;
gh = rc->rudegrid.gridHeight;
if (gh < 0) gh = 0;
if (gh+gy >= nrows) gh = nrows-gy-1;
x = cols[gx];
y = rows[gy];
w1 = cols[gw+gx]-x;
if (w1 < REALLYSMALL) w1 = REALLYSMALL;
h1 = rows[gh+gy]-y;
if (h1 < REALLYSMALL) h1 = REALLYSMALL;
XtConfigureWidget(child, x, y, w1, h1, 0);
}
free(cols);
free(rows);
}
static void MwRudegridResize(Widget w)
{
DoLayout(w);
}
static void Initialize(Widget request, Widget new,
ArgList args, Cardinal *num_args)
{
MwRudegridWidget rw = (MwRudegridWidget)new;
if (rw->core.height == 0) rw->core.height = DEFAULT_HEIGHT;
if (rw->core.width == 0) rw->core.width = DEFAULT_WIDTH;
if (rw->rudegrid.x_layout == NULL)
rw->rudegrid.x_layout = MwStrdup("100%");
else
rw->rudegrid.x_layout = MwStrdup(rw->rudegrid.x_layout);
if (rw->rudegrid.y_layout == NULL)
rw->rudegrid.y_layout = MwStrdup("100%");
else
rw->rudegrid.y_layout = MwStrdup(rw->rudegrid.y_layout);
}
/* ARGSUSED */
static Boolean
MwRudegridSetValues(current, request, new, args, num_args)
Widget current, request, new;
ArgList args;
Cardinal *num_args;
{
MwRudegridWidget oldrw = (MwRudegridWidget)current;
MwRudegridWidget newrw = (MwRudegridWidget)new;
if (newrw->rudegrid.x_layout != oldrw->rudegrid.x_layout) {
MwFree(oldrw->rudegrid.x_layout);
newrw->rudegrid.x_layout = MwStrdup(newrw->rudegrid.x_layout);
}
if (newrw->rudegrid.y_layout != oldrw->rudegrid.y_layout) {
MwFree(oldrw->rudegrid.y_layout);
newrw->rudegrid.y_layout = MwStrdup(newrw->rudegrid.y_layout);
}
DoLayout(new);
return FALSE;
}
/* COMPOSITE WIDGET FUNCTIONS */
static void MwRudegridChangeManaged(Widget w)
{
DoLayout(w);
}
/* Respond to size change requests from a child.
*/
static XtGeometryResult
MwRudegridGeometryManager(w, request, reply)
Widget w;
XtWidgetGeometry *request;
XtWidgetGeometry *reply; /* RETURN */
{
return XtGeometryYes; /* don't try to be clever */
}
/* CONSTRAINT WIDGET FUNCTIONS */
/* ARGSUSED */
static Boolean
MwRudegridConstraintSetValues(current, request, new, args, num_args)
Widget current, request, new;
ArgList args;
Cardinal *num_args;
{
return False ; /* what does this signify? */
}
|