File: EntityView.m

package info (click to toggle)
gnustep-dl2 0.12.0%2Bgit20250112-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 7,092 kB
  • sloc: objc: 68,223; makefile: 60; sh: 48
file content (210 lines) | stat: -rw-r--r-- 5,428 bytes parent folder | download | duplicates (5)
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
/**
    EntityView.m

    Author: Matt Rice <ratmice@gmail.com>
    Date: Oct 2006

    This file is part of DBModeler.

    <license>
    DBModeler is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    DBModeler 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DBModeler; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    </license>
**/

#include "EntityView.h"
#include "DiagramView.h"
#include "AttributeCell.h"
#include "NSView+Additions.h"


#ifdef NeXT_GUI_LIBRARY
#include <AppKit/AppKit.h>
#else
#include <AppKit/NSApplication.h>
#include <AppKit/NSColor.h>
#include <AppKit/NSFont.h>
#include <AppKit/NSGraphics.h>
#include <AppKit/NSTextFieldCell.h>
#include <AppKit/NSMatrix.h>
#include <AppKit/NSEvent.h>
#include <AppKit/NSBox.h>
#include <AppKit/NSImageCell.h>
#include <AppKit/NSImage.h>
#include <AppKit/NSScrollView.h>
#endif

#include <GNUstepBase/GNUstep.h>

@interface AttributeMatrix : NSMatrix
@end

@implementation AttributeMatrix : NSMatrix
- (void) mouseDown:(NSEvent *)ev
{
  [self orderViewFront:self];
  [super mouseDown:ev];
}
@end

@interface AttributeBox : NSBox

@end

@implementation AttributeBox : NSBox
- (void) drawRect:(NSRect)aRect
{
  [[NSColor blackColor] set];
  NSFrameRect(aRect);
}
@end

@implementation EntityView
- (NSString *) description
{
  return [NSString stringWithFormat:@"<%@ %p %@>", [self class], self, [_title stringValue]];
}
- (BOOL) isFlipped
{
  return YES;
}

- (void) setTitle:(NSString *)title
{
  [_title setStringValue:title];
}

- (AttributeCell *)cellAtRow:(int)row
{
  return [_attributesView cellAtRow:row column:0];
}

- (void) setNumberOfAttributes:(int)num
{
  [_attributesView renewRows:num columns:1];

}

- (void) sizeToFit
{
  NSSize s;
  NSRect r; 
  [_attributesView sizeToCells];
  [_attributesView sizeToFit];
  [_box sizeToFit];
  
  _titleRect.size = [_title cellSize];
  r = [_box bounds];
  s.height = _titleRect.size.height + r.size.height + 4;
  s.width = ((_titleRect.size.width > r.size.width) ? _titleRect.size.width : r.size.width) + 4;
  
  [_box setFrameOrigin:NSMakePoint(2, _titleRect.size.height + 2)];
  [self setFrameSize:s];
  [self setNeedsDisplay:YES];
}

- (void) dealloc
{
  RELEASE(_title); 
  [super dealloc];
}

- (id) initWithFrame:(NSRect)frameRect
{
  self = [super initWithFrame:frameRect];
  
  _title = [[TitleCell alloc] init];
  [_title setFont: [NSFont boldSystemFontOfSize:0]]; 
  [_title setEditable:NO];
  [_title setSelectable:NO];
  [_title setAlignment:NSLeftTextAlignment]; 
  _titleRect = NSMakeRect(2,2, 0, 0);
  
  _box = [[AttributeBox alloc] initWithFrame:NSMakeRect(2,2,0,0)];
  [_box setContentViewMargins:NSMakeSize(0,0)];
  [_box setBorderType:NSBezelBorder];
  [_box setTitlePosition:NSNoTitle];
  
  _attributesView = [[AttributeMatrix alloc] initWithFrame:NSMakeRect(1,1,1,1)];
  [_attributesView setBackgroundColor:[NSColor whiteColor]];
  [_attributesView setPrototype:[[AttributeCell alloc] init]];
  [_attributesView setAutosizesCells:YES];
  
  [[_box contentView] addSubview:_attributesView];
  RELEASE(_attributesView);
  [_box sizeToFit];
  
  [self addSubview:_box];  
  RELEASE(_box); 
  return self;
}

- (void) drawRect:(NSRect)dRect
{
  [[NSColor lightGrayColor] set];
  NSRectFill([self bounds]);

  [_title drawWithFrame:_titleRect inView:self];
  [[NSColor controlShadowColor] set];
  NSFrameRect(_bounds);

  [_attributesView setNeedsDisplay:YES];
}

- (void) orderViewFront:(NSView *)v
{
  [(DiagramView *)[self superview] orderViewFront:self];
}

- (void) mouseDown:(NSEvent *)ev
{
  NSPoint pt = [self convertPoint:[ev locationInWindow] fromView:nil];
  // allow clicking on title to move it..
  if (NSMouseInRect(pt, NSMakeRect(0, 0, _frame.size.width, _titleRect.size.height), YES))
    {
      NSView *dv = [self superview];      
      float in, up;
      pt = [dv convertPoint:pt fromView:self];
      up = pt.y - _frame.origin.y ;
      in = pt.x - _frame.origin.x;

      [dv orderViewFront:self];

      while ((ev = [NSApp nextEventMatchingMask:NSLeftMouseDraggedMask|NSLeftMouseUpMask untilDate:nil inMode:NSEventTrackingRunLoopMode dequeue:YES]))
        {
          float tox, toy;
          if ([ev type] == NSLeftMouseUp) break;
          NSPoint pt2 = [dv convertPoint:[ev locationInWindow] fromView:nil];
          tox = pt2.x - in;
          toy = pt2.y - up;
          /* contrain to a positive x,y. */ 
          // at 1.0 we can get some libart artifacts for some reason.. 
          pt2.x = tox > 1.0 ? tox : 1.1;
          pt2.y = toy > 1.0 ? toy : 1.1; 
          [self setFrameOrigin:pt2];
          [self autoscroll:ev];
          [dv setNeedsDisplay:YES];
          [self setNeedsDisplay:YES];
        }
    }
}

- (NSRect) attributeRectAtRow:(int)row
{
  NSRect cellFrame = [_attributesView cellFrameAtRow:row column:0];
  cellFrame.origin.y += _titleRect.origin.y + _titleRect.size.height;
  return cellFrame;
}
@end