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
|
/* -*-objc-*-
GSMarkupTagBox.m
Copyright (C) 2002 Free Software Foundation, Inc.
Author: Nicola Pero <n.pero@mi.flashnet.it>
Date: March 2002
This file is part of GNUstep Renaissance
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 License, 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; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "GSMarkupTagBox.h"
#include "GSAutoLayoutDefaults.h"
#ifndef GNUSTEP
# include <Foundation/Foundation.h>
# include <AppKit/AppKit.h>
# include "GNUstep.h"
#else
# include <Foundation/NSString.h>
# include <AppKit/NSBox.h>
#endif
/* We need this intermediate class so that we can ignore the sizeToFit
* calls made by the NSBox to its content view ... */
@interface GSMarkupBoxContentView : NSView
- (void) sizeToFit;
@end
@implementation GSMarkupBoxContentView
- (NSView *)firstSubview
{
NSArray *subviews = [self subviews];
if (subviews != nil && [subviews count] > 0)
{
return [subviews objectAtIndex: 0];
}
else
{
return nil;
}
}
/* This is only used when setting up the thing at startup, and never
* afterwards. */
- (void) sizeToFit
{
NSView *firstSubview = [self firstSubview];
[self setAutoresizesSubviews: NO];
if (firstSubview)
{
[self setFrameSize: [firstSubview frame].size];
}
else
{
[self setFrameSize: NSMakeSize (50, 50)];
}
[self setAutoresizesSubviews: YES];
}
/* Use the autolayout defaults of the first subview. */
- (GSAutoLayoutAlignment) autolayoutDefaultVerticalAlignment
{
NSView *firstSubview = [self firstSubview];
if (firstSubview)
{
return [firstSubview autolayoutDefaultVerticalAlignment];
}
else
{
return [super autolayoutDefaultVerticalAlignment];
}
}
- (GSAutoLayoutAlignment) autolayoutDefaultHorizontalAlignment
{
NSView *firstSubview = [self firstSubview];
if (firstSubview)
{
return [firstSubview autolayoutDefaultHorizontalAlignment];
}
else
{
return [super autolayoutDefaultHorizontalAlignment];
}
}
@end
@implementation GSMarkupTagBox
+ (NSString *) tagName
{
return @"box";
}
+ (Class) defaultPlatformObjectClass
{
return [NSBox class];
}
/* Basically, we only recognize the following options -
*
* title (yes / no); if yes, it is always on top
* border (yes / no); if yes, the platform default border is always used.
*/
- (void) platformObjectInit
{
[self setPlatformObject: [_platformObject init]];
/* title */
{
NSString *title = [self localizedStringValueForAttribute: @"title"];
if (title == nil)
{
/* Set the title even if nil ... to remove the default 'Title'
* label! */
/* Mac OS X barfs on nil title */
[_platformObject setTitle: @""];
/* Make sure to remove it completely if nil. */
[_platformObject setTitlePosition: NSNoTitle];
}
else
{
[_platformObject setTitle: title];
}
}
/* no border - FIXME tag attribute name */
{
if ([self boolValueForAttribute: @"hasBorder"] == 0)
{
[_platformObject setBorderType: NSNoBorder];
}
}
/* Content view. */
if (_content != nil && [_content count] > 0)
{
NSView *subview = [(GSMarkupTagObject *)[_content objectAtIndex: 0]
platformObject];
if ([subview isKindOfClass: [NSView class]])
{
GSMarkupBoxContentView *v;
v = [GSMarkupBoxContentView new];
[v setAutoresizesSubviews: YES];
[(NSBox *)_platformObject setContentView: v];
RELEASE (v);
[v addSubview: subview];
}
}
}
+ (NSArray *) localizableAttributes
{
return [NSArray arrayWithObject: @"title"];
}
/*
* NSBox is special because it's a container outside our control :-(
*
* Standard boxes/containers under our control keep track of the
* autolayout flags of the views they enclose, and can compute their
* own autolayout flags (used by other boxes/containers enclosing
* them) from those. When they are added to an enclosing window /
* box, the autolayout flags they compute are used.
*
* NSBox can not keep track of the autolayout flags of the view it
* encloses, but we still want to fake the correct behaviour, so that
* for example if you put something which expands in an NSBox, the
* NSBox will expand; if you put something which does not expand, the
* NSBox will not expand.
*
* In practice, if you put an NSBox in a container, the NSBox will
* first be asked to compute default autolayout flags. That requests
* is managed by NSBox's default autolayout stuff, and by our
* GSMarkupBoxContentView class above; it will compute correct
* autolayout flags unless the NSBox, or its content, have manual
* hardcoded hexpand/vexpand flags set.
*
* The library still gives us a chance to manage that case by calling
* the following method asking if manual flags are set. In that case,
* we examine the NSBox's manually hardcoded flags (like super does),
* and then the content's manually hardcoded flags if any.
*/
- (int) gsAutoLayoutVAlignment
{
/* If an align flag was manually specified by the user, return it. */
int flag = [super gsAutoLayoutVAlignment];
if (flag != 255)
{
return flag;
}
/* Else, check if the content has a flag which was manually
* specified by the user. If so, that should override the default
* computations. */
{
GSMarkupTagObject *view = (GSMarkupTagObject *)[_content objectAtIndex: 0];
if ([view isKindOfClass: [GSMarkupTagView class]])
{
flag = [(GSMarkupTagView *)view gsAutoLayoutVAlignment];
if (flag != 255)
{
if (flag == GSAutoLayoutExpand || flag == GSAutoLayoutWeakExpand)
{
return flag;
}
else
{
/* If the content does not expand, we center ourselves
* by default. */
return GSAutoLayoutAlignCenter;
}
}
}
}
/* Else, return 255. That will cause the autolayout default to be
* used.
*/
return 255;
}
- (int) gsAutoLayoutHAlignment
{
/* If an align flag was manually specified by the user, return it. */
int flag = [super gsAutoLayoutHAlignment];
if (flag != 255)
{
return flag;
}
/* Else, check if the content has a flag which was manually
* specified by the user. If so, that should override the default
* computations. */
{
GSMarkupTagObject *view = (GSMarkupTagObject *)[_content objectAtIndex: 0];
if ([view isKindOfClass: [GSMarkupTagView class]])
{
flag = [(GSMarkupTagView *)view gsAutoLayoutHAlignment];
if (flag != 255)
{
if (flag == GSAutoLayoutExpand || flag == GSAutoLayoutWeakExpand)
{
return flag;
}
else
{
/* If the content does not expand, we center ourselves
* by default. */
return GSAutoLayoutAlignCenter;
}
}
}
}
/* Else, return 255. That will cause the autolayout default to be
* used. */
return 255;
}
@end
|