summaryrefslogtreecommitdiff
path: root/tikzit/src/gtk/BoundingBoxTool.m
blob: 76c4f1084b9ea1c039e045323ba8ecbf6a6d5610 (plain)
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
/*
 * Copyright 2011-2012  Alex Merry <alex.merry@kdemail.net>
 *
 * This program 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 2 of
 * the License, or (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "BoundingBoxTool.h"

#import "GraphRenderer.h"
#import "TikzDocument.h"
#import "tzstockitems.h"

static const float handle_size = 8.0;
float sideHandleTop(NSRect bbox) {
    return (NSMinY(bbox) + NSMaxY(bbox) - handle_size)/2.0f;
}
float tbHandleLeft(NSRect bbox) {
    return (NSMinX(bbox) + NSMaxX(bbox) - handle_size)/2.0f;
}

@interface BoundingBoxTool (Private)
- (NSRect) screenBoundingBox;
- (ResizeHandle) boundingBoxResizeHandleAt:(NSPoint)p;
- (NSRect) boundingBoxResizeHandleRect:(ResizeHandle)handle;
- (void) setResizeCursorForHandle:(ResizeHandle)handle;
@end

@implementation BoundingBoxTool
- (NSString*) name { return @"Bounding Box Tool"; }
- (const gchar*) stockId { return TIKZIT_STOCK_BOUNDING_BOX; }
- (NSString*) helpText { return @"Set the bounding box"; }
- (NSString*) shortcut { return @"b"; }

+ (id) tool {
    return [[[self alloc] init] autorelease];
}

- (id) init {
    self = [super init];

    if (self) {
        currentResizeHandle = NoHandle;
    }

    return self;
}

- (void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    [renderer release];

    [super dealloc];
}

- (GraphRenderer*) activeRenderer { return renderer; }
- (void) setActiveRenderer:(GraphRenderer*)r {
    if (r == renderer)
        return;

    [[renderer surface] setCursor:NormalCursor];

    [r retain];
    [renderer release];
    renderer = r;
}

- (GtkWidget*) configurationWidget { return NULL; }

- (void) mousePressAt:(NSPoint)pos withButton:(MouseButton)button andMask:(InputMask)mask {
    if (button != LeftButton)
        return;

    dragOrigin = pos;
    currentResizeHandle = [self boundingBoxResizeHandleAt:pos];
    [[renderer document] startChangeBoundingBox];
    if (currentResizeHandle == NoHandle) {
        drawingNewBox = YES;
        [[[renderer document] graph] setBoundingBox:NSZeroRect];
    } else {
        drawingNewBox = NO;
    }
    [renderer invalidateGraph];
}

- (void) mouseMoveTo:(NSPoint)pos withButtons:(MouseButton)buttons andMask:(InputMask)mask {
    if (!(buttons & LeftButton)) {
        ResizeHandle handle = [self boundingBoxResizeHandleAt:pos];
        [self setResizeCursorForHandle:handle];
        return;
    }

    Transformer *transformer = [renderer transformer];
    Grid *grid = [renderer grid];
    Graph *graph = [[renderer document] graph];

    if (currentResizeHandle == NoHandle) {
        NSRect bbox = NSRectAroundPoints(
            [grid snapScreenPoint:dragOrigin],
            [grid snapScreenPoint:pos]
        );
        [graph setBoundingBox:[transformer rectFromScreen:bbox]];
    } else {
        NSRect bbox = [transformer rectToScreen:[graph boundingBox]];
        NSPoint p2 = [grid snapScreenPoint:pos];

        if (currentResizeHandle == NorthWestHandle ||
                currentResizeHandle == NorthHandle ||
                currentResizeHandle == NorthEastHandle) {

                float dy = p2.y - NSMinY(bbox);
                if (dy < bbox.size.height) {
                    bbox.origin.y += dy;
                    bbox.size.height -= dy;
                } else {
                    bbox.origin.y = NSMaxY(bbox);
                    bbox.size.height = 0;
                }

        } else if (currentResizeHandle == SouthWestHandle ||
                currentResizeHandle == SouthHandle ||
                currentResizeHandle == SouthEastHandle) {

                float dy = p2.y - NSMaxY(bbox);
                if (-dy < bbox.size.height) {
                    bbox.size.height += dy;
                } else {
                    bbox.size.height = 0;
                }
        }

        if (currentResizeHandle == NorthWestHandle ||
                currentResizeHandle == WestHandle ||
                currentResizeHandle == SouthWestHandle) {

                float dx = p2.x - NSMinX(bbox);
                if (dx < bbox.size.width) {
                    bbox.origin.x += dx;
                    bbox.size.width -= dx;
                } else {
                    bbox.origin.x = NSMaxX(bbox);
                    bbox.size.width = 0;
                }

        } else if (currentResizeHandle == NorthEastHandle ||
                currentResizeHandle == EastHandle ||
                currentResizeHandle == SouthEastHandle) {

                float dx = p2.x - NSMaxX(bbox);
                if (-dx < bbox.size.width) {
                    bbox.size.width += dx;
                } else {
                    bbox.size.width = 0;
                }
        }
        [graph setBoundingBox:[transformer rectFromScreen:bbox]];
    }
    [[renderer document] changeBoundingBoxCheckPoint];
    [renderer invalidateGraph];
}

- (void) mouseReleaseAt:(NSPoint)pos withButton:(MouseButton)button andMask:(InputMask)mask {
    if (button != LeftButton)
        return;

    [[renderer document] endChangeBoundingBox];
    drawingNewBox = NO;
    [renderer invalidateGraph];
}

- (void) renderWithContext:(id<RenderContext>)context onSurface:(id<Surface>)surface {
    if (!drawingNewBox && [[[renderer document] graph] hasBoundingBox]) {
        [context saveState];

        [context setAntialiasMode:AntialiasDisabled];
        [context setLineWidth:1.0];

        [context startPath];
        [context rect:[self boundingBoxResizeHandleRect:EastHandle]];
        [context rect:[self boundingBoxResizeHandleRect:SouthEastHandle]];
        [context rect:[self boundingBoxResizeHandleRect:SouthHandle]];
        [context rect:[self boundingBoxResizeHandleRect:SouthWestHandle]];
        [context rect:[self boundingBoxResizeHandleRect:WestHandle]];
        [context rect:[self boundingBoxResizeHandleRect:NorthWestHandle]];
        [context rect:[self boundingBoxResizeHandleRect:NorthHandle]];
        [context rect:[self boundingBoxResizeHandleRect:NorthEastHandle]];
        [context strokePathWithColor:MakeSolidRColor (0.5, 0.5, 0.5)];

        [context restoreState];
    }
}
- (void) loadConfiguration:(Configuration*)config {}
- (void) saveConfiguration:(Configuration*)config {}
@end

@implementation BoundingBoxTool (Private)
- (NSRect) screenBoundingBox {
    Transformer *transformer = [[renderer surface] transformer];
    Graph *graph = [[renderer document] graph];
    return [transformer rectToScreen:[graph boundingBox]];
}

- (ResizeHandle) boundingBoxResizeHandleAt:(NSPoint)p {
    NSRect bbox = [self screenBoundingBox];
    if (p.x >= NSMaxX(bbox)) {
        if (p.x <= NSMaxX(bbox) + handle_size) {
            if (p.y >= NSMaxY(bbox)) {
                if (p.y <= NSMaxY(bbox) + handle_size) {
                    return SouthEastHandle;
                }
            } else if (p.y <= NSMinY(bbox)) {
                if (p.y >= NSMinY(bbox) - handle_size) {
                    return NorthEastHandle;
                }
            } else {
                float eastHandleTop = sideHandleTop(bbox);
                if (p.y >= eastHandleTop && p.y <= (eastHandleTop + handle_size)) {
                    return EastHandle;
                }
            }
        }
    } else if (p.x <= NSMinX(bbox)) {
        if (p.x >= NSMinX(bbox) - handle_size) {
            if (p.y >= NSMaxY(bbox)) {
                if (p.y <= NSMaxY(bbox) + handle_size) {
                    return SouthWestHandle;
                }
            } else if (p.y <= NSMinY(bbox)) {
                if (p.y >= NSMinY(bbox) - handle_size) {
                    return NorthWestHandle;
                }
            } else {
                float westHandleTop = sideHandleTop(bbox);
                if (p.y >= westHandleTop && p.y <= (westHandleTop + handle_size)) {
                    return WestHandle;
                }
            }
        }
    } else if (p.y >= NSMaxY(bbox)) {
        if (p.y <= NSMaxY(bbox) + handle_size) {
            float southHandleLeft = tbHandleLeft(bbox);
            if (p.x >= southHandleLeft && p.x <= (southHandleLeft + handle_size)) {
                return SouthHandle;
            }
        }
    } else if (p.y <= NSMinY(bbox)) {
        if (p.y >= NSMinY(bbox) - handle_size) {
            float northHandleLeft = tbHandleLeft(bbox);
            if (p.x >= northHandleLeft && p.x <= (northHandleLeft + handle_size)) {
                return NorthHandle;
            }
        }
    }
    return NoHandle;
}

- (NSRect) boundingBoxResizeHandleRect:(ResizeHandle)handle {
    Graph *graph = [[renderer document] graph];
    if (![graph hasBoundingBox]) {
        return NSZeroRect;
    }
    NSRect bbox = [self screenBoundingBox];
    float x;
    float y;
    switch (handle) {
        case NorthEastHandle:
        case EastHandle:
        case SouthEastHandle:
            x = NSMaxX(bbox);
            break;
        case NorthWestHandle:
        case WestHandle:
        case SouthWestHandle:
            x = NSMinX(bbox) - handle_size;
            break;
        case SouthHandle:
        case NorthHandle:
            x = tbHandleLeft(bbox);
            break;
        default:
            return NSZeroRect;
    }
    switch (handle) {
        case EastHandle:
        case WestHandle:
            y = sideHandleTop(bbox);
            break;
        case SouthEastHandle:
        case SouthHandle:
        case SouthWestHandle:
            y = NSMaxY(bbox);
            break;
        case NorthEastHandle:
        case NorthHandle:
        case NorthWestHandle:
            y = NSMinY(bbox) - handle_size;
            break;
        default:
            return NSZeroRect;
    }
    return NSMakeRect(x, y, handle_size, handle_size);
}

- (void) setResizeCursorForHandle:(ResizeHandle)handle {
    if (handle != currentResizeHandle) {
        currentResizeHandle = handle;
        Cursor c = NormalCursor;
        switch (handle) {
            case EastHandle:
                c = ResizeRightCursor;
                break;
            case SouthEastHandle:
                c = ResizeBottomRightCursor;
                break;
            case SouthHandle:
                c = ResizeBottomCursor;
                break;
            case SouthWestHandle:
                c = ResizeBottomLeftCursor;
                break;
            case WestHandle:
                c = ResizeLeftCursor;
                break;
            case NorthWestHandle:
                c = ResizeTopLeftCursor;
                break;
            case NorthHandle:
                c = ResizeTopCursor;
                break;
            case NorthEastHandle:
                c = ResizeTopRightCursor;
                break;
            default:
                c = NormalCursor;
                break;
        }
        [[renderer surface] setCursor:c];
    }
}
@end

// vim:ft=objc:ts=8:et:sts=4:sw=4