File: TShelfView.m

package info (click to toggle)
gworkspace 0.9.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 20,832 kB
  • sloc: objc: 69,382; sh: 488; makefile: 39
file content (525 lines) | stat: -rw-r--r-- 12,528 bytes parent folder | download
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/* TShelfView.m
 *  
 * Copyright (C) 2003-2012 Free Software Foundation, Inc.
 *
 * Author: Enrico Sersale <enrico@imago.ro>
 *         Riccardo Mottola <rm@gnu.org>
 * Date: August 2001
 *
 * This file is part of the GNUstep GWorkspace application
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
 */

#import <AppKit/AppKit.h>
#import <GNUstepBase/GNUstep.h>

#import "TShelfView.h"
#import "TShelfViewItem.h"
#import "TShelfIconsView.h"
#import "GWorkspace.h"


#define SPECIAL_TAB_W 54
#define BUTTORX 25
#define BUTTSZ 9
#define BUTTSPACE 12
#define TAB_H 24
#define LAB_MARGIN 13
#define BEZ_TAB_W 14

@implementation TShelfView

- (id)initWithFrame:(NSRect)rect
{
  self = [super initWithFrame: rect];
  
  if (self) {
    NSRect r = NSZeroRect;
    
    ASSIGN (items, [NSMutableArray array]);
    
    font = [NSFont fontWithName: @"Helvetica-Bold" size: 12];
    if (font == nil) {
      font = [NSFont boldSystemFontOfSize: 0];
    }
    RETAIN (font);

    italicFont = [NSFont fontWithName: @"Helvetica-BoldOblique" size: 12];
    if (italicFont == nil) {
      italicFont = [NSFont boldSystemFontOfSize: 0];
    }
    RETAIN (italicFont);
    
    r.size = NSMakeSize(BUTTSZ, BUTTSZ);
    r.origin.y = rect.size.height - TAB_H + (int)((TAB_H - BUTTSZ) / 2);
    r.origin.x = rect.size.width - (BUTTSZ * 2 + BUTTSPACE);
                  
    rewButt = [[NSButton alloc] initWithFrame: r];
    [rewButt setButtonType: NSMomentaryLight];    
    [rewButt setBordered: NO];    
    [rewButt setTarget: self];
    [rewButt setAction: @selector (buttonsAction:)];
    [self addSubview: rewButt];
    RELEASE (rewButt);        
    
    r.origin.x += BUTTSPACE;
    
    ffButt = [[NSButton alloc] initWithFrame: r];
    [ffButt setButtonType: NSMomentaryLight];    
    [ffButt setBordered: NO];    
    [ffButt setTarget: self];
    [ffButt setAction: @selector (buttonsAction:)];
    [self addSubview: ffButt];
    RELEASE (ffButt);        

    [self setButtonsEnabled: NO];   
                    
    lastItem = nil;
    selected = nil;
  }
  
  return self;
}

- (void)dealloc
{
  RELEASE (items);
  RELEASE (font);
  RELEASE (italicFont);
  
  [super dealloc];
}

- (void)addTabItem:(TShelfViewItem *)item
{
  [self insertTabItem: item atIndex: [items count]];
}

- (BOOL)insertTabItem:(TShelfViewItem *)item
	      atIndex:(NSUInteger)index
{
  if (lastItem) {
    if (index == [items count]) {
      index--;
    }
    RETAIN (lastItem);
    [items removeObject: lastItem];
  }

  [item setTShelfView: self];
  [items insertObject: item atIndex: index];
  
  if (lastItem) {
    [items insertObject: lastItem atIndex: [items count]];
    RELEASE (lastItem);
  }  
  
  return YES;
}

- (void)setLastTabItem:(TShelfViewItem *)item
{
  lastItem = item;
  [item setTShelfView: self];
  [items insertObject: item atIndex: [items count]];
}

- (BOOL)removeTabItem:(TShelfViewItem *)item
{
  NSUInteger i = [items indexOfObject: item];
  
  if ((i == NSNotFound) || (item == lastItem)) {
    return NO;
  }
  
  if (item == selected) {
    [[selected view] removeFromSuperview];
    selected = nil;
  }

  [items removeObjectAtIndex: i];
  
  return YES;
}

- (NSUInteger)indexOfItem:(TShelfViewItem *)item
{
  return [items indexOfObject: item];
}

- (TShelfViewItem *)selectedTabItem
{
  if ((selectedItem == NSNotFound) || ([items count] == 0)) {
    return nil;
  }
  
  return [items objectAtIndex: selectedItem];
}

- (void)selectTabItem:(TShelfViewItem *)item
{
  TShelfIconsView *selectedView;

  if (item == nil)
    return;

  if (selected != nil)
    {
      [selected setTabState: NSBackgroundTab];
      selectedView = (TShelfIconsView *)[selected view];
      if ([selectedView iconsType] == DATA_TAB) {
	[selectedView setCurrentPBIcon: nil];
      }
      [[selected view] removeFromSuperview];
    }

  selected = item;

  selectedItem = [items indexOfObject: selected];
  [selected setTabState: NSSelectedTab];

  selectedView = (TShelfIconsView *)[selected view];

  if (selectedView != nil)
    {
      [self addSubview: selectedView];
      [selectedView setFrame: [self contentRect]];
      [selectedView resizeWithOldSuperviewSize: [selectedView frame].size]; 
      [selectedView unselectOtherIcons: nil];
      if ([selectedView iconsType] == DATA_TAB)
	{
	  [selectedView setCurrentPBIcon: nil];
	}
      [[self window] makeFirstResponder: [selected initialFirstResponder]];
    }
  
  [self setButtonsEnabled: (lastItem && (lastItem == selected))];
      
  [self setNeedsDisplay: YES]; 
}

- (void)selectTabItemAtIndex:(NSUInteger)index
{
  [self selectTabItem: [items objectAtIndex: index]];
}

- (void)selectLastItem
{
  if (lastItem)
    {
      [self selectTabItem: lastItem];
    }
}

- (NSFont *)font
{
  return font;
}

- (NSFont *)italicFont
{
  return italicFont;
}

- (NSRect)contentRect
{
  NSRect cRect = [self bounds];

  cRect.origin.y += 1; 
  cRect.size.height -= 26.5;

  return cRect;
}

void drawLeftTabBezier(NSPoint origin, float tabh, 
		       NSColor *sc, NSColor *fc, BOOL seltab)
{
  NSBezierPath *path = [NSBezierPath bezierPath];
  NSPoint endp = NSMakePoint(origin.x + BEZ_TAB_W, origin.y + tabh);
  NSPoint cp1 = NSMakePoint(origin.x + (BEZ_TAB_W / 2), origin.y);
  NSPoint cp2 = NSMakePoint(origin.x + BEZ_TAB_W - (BEZ_TAB_W / 2), origin.y + tabh);
  
  [path moveToPoint: origin];
  [path curveToPoint: endp controlPoint1: cp1 controlPoint2: cp2];
  [sc set];
  [path stroke];
  
  [path lineToPoint: NSMakePoint(origin.x + BEZ_TAB_W, origin.y)];
  [path closePath];
  [fc set];
  [path fill];
  
  if (seltab) {
    path = [NSBezierPath bezierPath];
    [path moveToPoint: origin];
    [path lineToPoint: NSMakePoint(origin.x + BEZ_TAB_W, origin.y)];
    [path stroke];
  }
}

void drawRightTabBezier(NSPoint origin, float tabh, 
			NSColor *sc, NSColor *fc, BOOL seltab)
{
  NSBezierPath *path = [NSBezierPath bezierPath];
  NSPoint endp = NSMakePoint(origin.x - BEZ_TAB_W, origin.y + tabh);
  NSPoint cp1 = NSMakePoint(origin.x - (BEZ_TAB_W / 2), origin.y);
  NSPoint cp2 = NSMakePoint(origin.x - BEZ_TAB_W + (BEZ_TAB_W / 2), origin.y + tabh);
  
  [path moveToPoint: origin];
  [path curveToPoint: endp controlPoint1: cp1 controlPoint2: cp2];
  [sc set];
  [path stroke];
  
  [path lineToPoint: NSMakePoint(origin.x - BEZ_TAB_W, origin.y)];
  [path closePath];
  [fc set];
  [path fill];

  if (seltab) {
    path = [NSBezierPath bezierPath];
    [path moveToPoint: origin];
    [path lineToPoint: NSMakePoint(origin.x - BEZ_TAB_W, origin.y)];
    [path stroke];
  }
}

- (void)drawRect:(NSRect)rect
{
  NSRect aRect = [self bounds];
  NSPoint p = aRect.origin;
  NSSize s = aRect.size;
  NSUInteger count;  
  int itemxspace;
  NSImage *backImage;
  NSColor *scolor;
  NSColor *fcolor;
  NSPoint selp[2];
  NSBezierPath *bpath;
  int i;
  NSPoint ipoint;
  
  backImage = [[GWorkspace gworkspace] tshelfBackground];
  if (backImage)
    [backImage compositeToPoint: NSZeroPoint operation: NSCompositeSourceOver];

  count = [items count];
  itemxspace = (int)((aRect.size.width - SPECIAL_TAB_W) / (count - 1));
  
  [[NSColor controlColor] set];
  NSRectFill(NSMakeRect(p.x, p.y, s.width, s.height - TAB_H));

  if (selected == nil) {
    [self selectTabItemAtIndex: 0];
  }
	  
  selp[0] = NSZeroPoint;
  selp[1] = NSZeroPoint;

  aRect.size.height -= TAB_H;
  
  ipoint = NSMakePoint (0,0);
  for (i = count - 1; i >= 0; i--)
    {
      TShelfViewItem *anItem = [items objectAtIndex: i];
      NSRect r;
    
      if (i == (count - 1))
	{
	  ipoint.x = (int)(aRect.size.width - SPECIAL_TAB_W);
	  ipoint.y = aRect.size.height;
	  
	  if ([anItem tabState] == NSSelectedTab)
	    {
	      selp[0] = ipoint;
	      fcolor = [NSColor controlColor];
	    } 
	  else
	    {
	      fcolor = [NSColor controlBackgroundColor];
	    }
      
	  scolor = [NSColor whiteColor];
      
	  drawLeftTabBezier(ipoint, TAB_H, scolor, fcolor, NO);

	  r.origin.x = ipoint.x + LAB_MARGIN;
	  r.origin.y = aRect.size.height;
	  r.size.width = SPECIAL_TAB_W - LAB_MARGIN;
	  r.size.height = TAB_H -1;
      
	  bpath = [NSBezierPath bezierPath];
	  [bpath setLineWidth: 1];
	  [bpath moveToPoint: NSMakePoint(r.origin.x, r.origin.y + TAB_H)];
	  [bpath relativeLineToPoint: NSMakePoint(r.size.width, 0)];
	  [scolor set];
	  [bpath stroke];
	  
	  [anItem drawImage: nil inRect: r];

	  }
      else
	{
	  ipoint.y = aRect.size.height;
	  
	  if ([anItem tabState] == NSSelectedTab)
	    {
	      selp[1] = NSMakePoint(ipoint.x + BEZ_TAB_W, ipoint.y);
	      fcolor = [NSColor controlColor];
	    }
	  else
	    {
	      fcolor = [NSColor controlBackgroundColor];
	    }
      
      scolor = [NSColor blackColor];
      drawRightTabBezier(NSMakePoint(ipoint.x + BEZ_TAB_W, ipoint.y), 
                                                  TAB_H, scolor, fcolor, NO);
      ipoint.x -= itemxspace;

      if (i != 0) {
        if ([anItem tabState] == NSSelectedTab) {
          selp[0] = ipoint;
        }
        
        scolor = [NSColor whiteColor];
        drawLeftTabBezier(ipoint, TAB_H, scolor, fcolor, NO);
              
	      r.origin.x = ipoint.x + LAB_MARGIN;
	      r.origin.y = aRect.size.height;
	      r.size.width = itemxspace - LAB_MARGIN;
	      r.size.height = TAB_H -1;
        
      } else {
	      r.origin.x = ipoint.x;
	      r.origin.y = aRect.size.height;
	      r.size.width = itemxspace;
	      r.size.height = TAB_H -1;
      }
      
      scolor = [NSColor whiteColor];

      bpath = [NSBezierPath bezierPath];
      [bpath setLineWidth: 1];
      [bpath moveToPoint: NSMakePoint(r.origin.x, r.origin.y + TAB_H)];
      [bpath relativeLineToPoint: NSMakePoint(r.size.width, 0)];
      [scolor set];
      [bpath stroke];
      
	    [anItem drawLabelInRect: r];
	  }  
	}
  
  fcolor = [NSColor controlColor];
  
  if (NSEqualPoints(selp[0], NSZeroPoint) == NO) {
    scolor = [NSColor whiteColor];
    drawLeftTabBezier(selp[0], TAB_H, scolor, fcolor, YES);

    bpath = [NSBezierPath bezierPath];
    [bpath setLineWidth: 1];
    [bpath moveToPoint: NSMakePoint(p.x - 2, aRect.size.height)];
    [bpath lineToPoint: selp[0]];
    [scolor set];
    [bpath stroke];
  }
  
  if (NSEqualPoints(selp[1], NSZeroPoint) == NO) {
    scolor = [NSColor blackColor];
    drawRightTabBezier(selp[1], TAB_H, scolor, fcolor, YES);
        
    scolor = [NSColor whiteColor];
    bpath = [NSBezierPath bezierPath];
    [bpath setLineWidth: 1];
    [bpath moveToPoint: selp[1]];
    [bpath lineToPoint: NSMakePoint(s.width, aRect.size.height)];
    [scolor set];
    [bpath stroke];
  }
}

- (BOOL)isOpaque
{
  return YES;
}

- (TShelfViewItem *)tabItemAtPoint:(NSPoint)point
{
  NSUInteger count = [items count];
  NSUInteger i;

  point = [self convertPoint: point fromView: nil];

  for (i = 0; i < count; i++) {
    TShelfViewItem *anItem = [items objectAtIndex: i];

    if (NSPointInRect(point, [anItem tabRect])) {
	    return anItem;
    }
  }
  
  return nil;
}

- (TShelfViewItem *)lastTabItem
{
  return lastItem;
}

- (NSArray *)items
{
  return items;
}

- (void)buttonsAction:(id)sender
{

}

- (void)setButtonsEnabled:(BOOL)enabled
{
  [rewButt setEnabled: enabled];  
  [ffButt setEnabled: enabled];  

  if (enabled) {
    [rewButt setImage: [NSImage imageNamed: @"REWArrow.tiff"]];
    [ffButt setImage: [NSImage imageNamed: @"FFArrow.tiff"]];
  } else {
    [rewButt setImage: [NSImage imageNamed: @"REWArrow_disabled.tiff"]];
    [ffButt setImage: [NSImage imageNamed: @"FFArrow_disabled.tiff"]];
  }
}

- (void)mouseDown:(NSEvent *)theEvent
{
  NSPoint location = [theEvent locationInWindow];
  TShelfViewItem *anItem = [self tabItemAtPoint: location];
    
  if (anItem && (anItem != selected)) {
    [self selectTabItem: anItem];
  }
}

- (BOOL)acceptsFirstResponder
{
  return YES;
}

- (BOOL)acceptsFirstMouse:(NSEvent*)theEvent
{
  return YES;
}

@end