Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MUMessageBubbleTableViewCell.m « Classes « Source - github.com/mumble-voip/mumble-iphoneos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d6bfe3119b0d32b3d027d616fd46738388926687 (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
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
// Copyright 2009-2012 The 'Mumble for iOS' Developers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#import "MUMessageBubbleTableViewCell.h"
#import "MUTextMessage.h"
#import "MUColor.h"
#import "MUOperatingSystem.h"

#define kBalloonWidth                190.0f
#define kBalloonTopMargin            8.0f
#define kBalloonBottomMargin         10.0f
#define kBalloonMarginTailSide       19.0f
#define kBalloonMarginNonTailSide    11.0f
#define kBalloonTopPadding           3.0f
#define kBalloonBottomPadding        3.0f
#define kBalloonTimestampSpacing     5.0f
#define kPhoneWidth                  320.0f
#define kBalloonTopInset             14.0f
#define kBalloonBottomInset          17.0f
#define kBalloonTailInset            23.0f
#define kBalloonNoTailInset          16.0f
#define kBalloonFooterTopMargin      2.0f
#define kBalloonFooterBoxPadding     2.0f
#define kBalloonImageTopPadding      2.0f
#define kBalloonImageBottomPadding   2.0f

@interface MUMessageBubbleView : UIView {
    NSString                                  *_message;
    NSString                                  *_heading;
    NSString                                  *_footer;
    NSDate                                    *_date;
    BOOL                                      _rightSide;
    CGRect                                    _imageRect;
    BOOL                                      _selected;
    NSInteger                                 _numAttachments;
    NSArray                                   *_shownImages;
    MUMessageBubbleTableViewCell              *_cell;
}
- (void) setHeading:(NSString *)heading;
- (void) setFooter:(NSString *)footer;
- (void) setMessage:(NSString *)msg;
- (void) setDate:(NSDate *)date;
- (void) setShownImages:(NSArray *)shownImages;
- (void) setRightSide:(BOOL)rightSide;
- (CGRect) selectionRect;
- (void) setSelected:(BOOL)shouldBeSelected;
- (BOOL) isSelected;
+ (CGSize) cellSizeForText:(NSString *)text andHeading:(NSString *)heading andFooter:(NSString *)footer andDate:(NSDate *)date andImages:(NSArray *)images;
@end

@implementation MUMessageBubbleView

- (id) initWithFrame:(CGRect)frame andTableViewCell:(MUMessageBubbleTableViewCell *)cell {
    if ((self = [super initWithFrame:frame])) {
        [self setOpaque:NO];
        _rightSide = YES;
        _cell = cell;
    }
    return self;
}

- (void) dealloc {
    [_shownImages release];
    [_message release];
    [_heading release];
    [_date release];
    [super dealloc];
}

+ (CGSize) textSizeForText:(NSString *)text {
    CGSize constraintSize = CGSizeMake(kBalloonWidth-(kBalloonMarginTailSide+kBalloonMarginNonTailSide), CGFLOAT_MAX);
    return [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
}

+ (CGSize) headingSizeForText:(NSString *)text {
    CGSize constraintSize = CGSizeMake(kBalloonWidth-(kBalloonMarginTailSide+kBalloonMarginNonTailSide), CGFLOAT_MAX);
    return [text sizeWithFont:[UIFont boldSystemFontOfSize:14.0f] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
}

+ (CGSize) timestampSizeForText:(NSString *)text {
    CGSize constraintSize = CGSizeMake(kBalloonWidth-(kBalloonMarginTailSide+kBalloonMarginNonTailSide), CGFLOAT_MAX);
    return [text sizeWithFont:[UIFont italicSystemFontOfSize:11.0f] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeHeadTruncation];
}

+ (CGSize) footerSizeForText:(NSString *)text {
    if (text == nil)
        return CGSizeZero;
    CGSize constraintSize = CGSizeMake(kBalloonWidth-(kBalloonMarginTailSide+kBalloonMarginNonTailSide), CGFLOAT_MAX);
    return [text sizeWithFont:[UIFont italicSystemFontOfSize:11.0f] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
}

+ (NSString *) stringForDate:(NSDate *)date {
    NSDateFormatter *fmt = [[[NSDateFormatter alloc] init] autorelease];
    [fmt setDateFormat:@"HH:mm"];
    return [fmt stringFromDate:date];
}

+ (CGSize) imageSizeForImages:(NSArray *)images resizedToFitWithinSize:(CGSize)sz andNewImageSizes:(NSArray **)array {
    NSMutableArray *imageSizes = [[[NSMutableArray alloc] initWithCapacity:[images count]] autorelease];
    CGFloat imagesHeight = 0;
    for (UIImage *image in images) {
        CGSize imgSize = [image size];
        if (imgSize.width < (sz.width - 40)) {
            imagesHeight += kBalloonImageTopPadding + imgSize.height + kBalloonImageBottomPadding;
            [imageSizes addObject:NSStringFromCGSize(imgSize)];
        } else {
            CGFloat w = sz.width - 40;
            CGFloat h = imgSize.height * (w / imgSize.width);
            imagesHeight += kBalloonImageTopPadding + h + kBalloonImageBottomPadding;
            [imageSizes addObject:NSStringFromCGSize(CGSizeMake(w, h))];
        }
    }
    if (array) {
        *array = imageSizes;
    }
    return CGSizeMake(sz.width, imagesHeight);
}

+ (CGSize) cellSizeForText:(NSString *)text andHeading:(NSString *)heading andFooter:(NSString *)footer andDate:(NSDate *)date andImages:(NSArray *)images {
    CGSize textSize = [MUMessageBubbleView textSizeForText:text];
    CGSize headingSize = [MUMessageBubbleView headingSizeForText:heading];
    CGSize footerSize = [MUMessageBubbleView footerSizeForText:footer];
    NSString *str = [MUMessageBubbleView stringForDate:date];
    CGSize timestampSize = [MUMessageBubbleView timestampSizeForText:str];

    CGSize sz = CGSizeMake(MAX(textSize.width, headingSize.width + kBalloonTimestampSpacing + timestampSize.width)+(kBalloonMarginTailSide + kBalloonMarginNonTailSide), textSize.height+headingSize.height+footerSize.height+(kBalloonTopMargin+kBalloonBottomMargin)+(kBalloonTopPadding+kBalloonBottomPadding)+(footer?kBalloonFooterTopMargin:0));

    CGSize imgSz = [MUMessageBubbleView imageSizeForImages:images resizedToFitWithinSize:sz andNewImageSizes:nil];
    sz.height += imgSz.height;
    sz.height = ceilf(sz.height);
    
    return sz;
}

- (void) drawRect:(CGRect)rect {

    UIImage *balloon = nil;
    UIImage *stretchableBalloon = nil;

    if (MUGetOperatingSystemVersion() >= MUMBLE_OS_IOS_7) {
        if (_rightSide) {
            if (_selected) {
                balloon = [UIImage imageNamed:@"RightBalloonSelectedMono"];
            } else {
                balloon = [UIImage imageNamed:@"Balloon_2_Right"];
            }
            stretchableBalloon = [balloon resizableImageWithCapInsets:UIEdgeInsetsMake(kBalloonTopInset, kBalloonNoTailInset, kBalloonBottomInset, kBalloonTailInset)];
        } else {
            if (_selected) {
                balloon = [UIImage imageNamed:@"LeftBalloonSelectedMono"];
            } else {
                balloon = [UIImage imageNamed:@"Balloon_2"];
            }
            stretchableBalloon = [balloon resizableImageWithCapInsets:UIEdgeInsetsMake(kBalloonTopInset, kBalloonTailInset, kBalloonBottomInset, kBalloonNoTailInset)];
        }
    } else {
        if (_rightSide) {
            if (_selected) {
                balloon = [UIImage imageNamed:@"RightBalloonSelected"];
            } else {
                balloon = [UIImage imageNamed:@"Balloon_Blue"];
            }
            stretchableBalloon = [balloon resizableImageWithCapInsets:UIEdgeInsetsMake(kBalloonTopInset, kBalloonNoTailInset, kBalloonBottomInset, kBalloonTailInset)];
        } else {
            if (_selected) {
                balloon = [UIImage imageNamed:@"LeftBalloonSelected"];
            } else {
                balloon = [UIImage imageNamed:@"Balloon_2"];
            }
            stretchableBalloon = [balloon resizableImageWithCapInsets:UIEdgeInsetsMake(kBalloonTopInset, kBalloonTailInset, kBalloonBottomInset, kBalloonNoTailInset)];
        }
    }

    NSString *text = _message;
    NSString *heading = _heading;
    NSString *footer = _footer;
    NSArray *images = _shownImages;
    
    CGSize textSize = [MUMessageBubbleView textSizeForText:text];
    CGSize headingSize = [MUMessageBubbleView headingSizeForText:heading];
    CGSize footerSize = [MUMessageBubbleView footerSizeForText:footer];
    NSString *dateStr = [MUMessageBubbleView stringForDate:_date];
    CGSize timestampSize = [MUMessageBubbleView timestampSizeForText:dateStr];

    CGRect imgRect = CGRectMake(
        0.0f,
        kBalloonTopPadding,
        MAX(textSize.width, headingSize.width + kBalloonTimestampSpacing + timestampSize.width)+(kBalloonMarginTailSide + kBalloonMarginNonTailSide) + 10,
        textSize.height+headingSize.height+footerSize.height+(kBalloonTopMargin + kBalloonBottomMargin)+(footer?kBalloonFooterTopMargin:0)
    );
    imgRect = CGRectMake(
        (int) CGRectGetMinX(imgRect),
        (int) CGRectGetMinY(imgRect),
        (int) CGRectGetWidth(imgRect),
        (int) CGRectGetHeight(imgRect)
    );
    
    NSArray *newImageSizes = nil;
    CGSize imagesSize = [MUMessageBubbleView imageSizeForImages:images resizedToFitWithinSize:imgRect.size andNewImageSizes:&newImageSizes];
    imgRect.size.height += imagesSize.height;
    imgRect.size.height = ceilf(imgRect.size.height);
    
    CGRect headerRect = CGRectMake(kBalloonMarginTailSide, kBalloonTopPadding + kBalloonTopMargin, headingSize.width, headingSize.height);
    CGRect timestampRect = CGRectMake(imgRect.size.width - kBalloonMarginNonTailSide - timestampSize.width, headerRect.origin.y, timestampSize.width, timestampSize.height);
    CGRect textRect = CGRectMake(kBalloonMarginTailSide, kBalloonTopPadding + kBalloonTopMargin + headingSize.height, textSize.width, textSize.height);
    if (_rightSide) {
        imgRect.origin.x = kPhoneWidth - imgRect.size.width;
        headerRect.origin.x = imgRect.origin.x + kBalloonMarginNonTailSide;
        timestampRect.origin.x = kPhoneWidth - kBalloonMarginTailSide - timestampRect.size.width;
        textRect.origin.x = imgRect.origin.x + kBalloonMarginNonTailSide;
    }

    [stretchableBalloon drawInRect:imgRect];

    _imageRect = imgRect;

    int i = 0;
    CGFloat maxWidth = (timestampRect.origin.x + timestampRect.size.width) - headerRect.origin.x;
    CGRect shownImgRect = CGRectMake(0, textRect.origin.y+textRect.size.height, 0, 0);
    for (UIImage *shownImage in _shownImages) {
        shownImgRect.origin.y += kBalloonImageTopPadding + shownImgRect.size.height;
        CGSize imgSz = CGSizeFromString([newImageSizes objectAtIndex:i]);
        CGFloat leftPad = floorf((maxWidth - imgSz.width)/2);
        shownImgRect.origin.x = textRect.origin.x + leftPad;
        shownImgRect.size.width = imgSz.width;
        shownImgRect.size.height = imgSz.height;
        [shownImage drawInRect:shownImgRect];
        shownImgRect.size.height += kBalloonImageBottomPadding;
        ++i;
    }

    CGRect footerRect = CGRectMake(textRect.origin.x,
                                   textRect.origin.y + textRect.size.height+imagesSize.height + kBalloonFooterTopMargin,
                                   footerSize.width,
                                   footerSize.height);
    [[UIColor blackColor] set];
    [footer drawInRect:footerRect withFont:[UIFont italicSystemFontOfSize:11.0f] lineBreakMode:UILineBreakModeWordWrap];
    [heading drawInRect:headerRect withFont:[UIFont boldSystemFontOfSize:14.0f] lineBreakMode:UILineBreakModeWordWrap];
    [dateStr drawInRect:timestampRect withFont:[UIFont italicSystemFontOfSize:11.0f] lineBreakMode:UILineBreakModeHeadTruncation];
    [text drawInRect:textRect withFont:[UIFont systemFontOfSize:14.0f] lineBreakMode:UILineBreakModeWordWrap];
}

- (CGRect) selectionRect {    
    if (_rightSide) {
        return UIEdgeInsetsInsetRect(_imageRect, UIEdgeInsetsMake(kBalloonTopMargin, kBalloonMarginNonTailSide, kBalloonBottomMargin, kBalloonMarginTailSide));
    } else {
        return UIEdgeInsetsInsetRect(_imageRect, UIEdgeInsetsMake(kBalloonTopMargin, kBalloonMarginTailSide, kBalloonBottomMargin, kBalloonMarginNonTailSide));
    }
}

- (BOOL) isSelected {
    return _selected;
}

- (void) setSelected:(BOOL)selected {
    _selected = selected;
    [self setNeedsDisplay];
}

- (void) setHeading:(NSString *)heading {
    [_heading release];
    _heading = [heading copy];
    [self setNeedsDisplay];
}

- (void) setFooter:(NSString *)footer {
    [_footer release];
    _footer = [footer copy];
    [self setNeedsDisplay];
}

- (void) setMessage:(NSString *)msg {
    [_message release];
    _message = [msg copy];
    [self setNeedsDisplay];
}

- (void) setDate:(NSDate *)date {
    [_date release];
    _date = [date copy];
    [self setNeedsDisplay];
}

- (void) setRightSide:(BOOL)rightSide {
    _rightSide = rightSide;
    [self setNeedsDisplay];
}

- (void) setNumberOfAttachments:(NSInteger)numAttachments {
    _numAttachments = numAttachments;
    [self setNeedsDisplay];
}

- (void) setShownImages:(NSArray *)shownImages {
    _shownImages = [shownImages retain];
    [self setNeedsDisplay];
}

- (BOOL) canBecomeFirstResponder {
    return YES;
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([self isFirstResponder]) {
        // A bit of a hack. Oh well.
        [[UIMenuController sharedMenuController] setMenuVisible:NO animated:YES];
    }
}

- (BOOL) canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(copy:)) {
        return YES;
    }
    if (action == @selector(delete:)) {
        return YES;
    }
    return NO;
}

- (void) copy:(id)sender {
    [[_cell delegate] messageBubbleTableViewCellRequestedCopy:_cell];
}

- (void) delete:(id)sender {
    [[_cell delegate] messageBubbleTableViewCellRequestedDeletion:_cell];
}

@end

@interface MUMessageBubbleTableViewCell () {
    MUMessageBubbleView                       *_bubbleView;
    UILongPressGestureRecognizer              *_longPressRecognizer;
    UITapGestureRecognizer                    *_tapRecognizer;
    id<MUMessageBubbleTableViewCellDelegate>  _delegate;
}
@end

@implementation MUMessageBubbleTableViewCell

+ (CGFloat) heightForCellWithHeading:(NSString *)heading message:(NSString *)msg images:(NSArray *)images footer:(NSString *)footer date:(NSDate *)date {
    return [MUMessageBubbleView cellSizeForText:msg andHeading:heading andFooter:footer andDate:date andImages:images].height;
}

- (id) initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier])) {        
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    
        _bubbleView = [[MUMessageBubbleView alloc] initWithFrame:self.contentView.frame andTableViewCell:self];
        [_bubbleView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
        [_bubbleView setUserInteractionEnabled:YES];
        [[self contentView] addSubview:_bubbleView];
        
        _longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showMenu:)];
        [_bubbleView addGestureRecognizer:_longPressRecognizer];
        [_longPressRecognizer release];
        
        _tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showAttachments:)];
        [_bubbleView addGestureRecognizer:_tapRecognizer];
        [_tapRecognizer release];
    }
    return self;
}

- (void) dealloc {
    [_bubbleView release];
    [_longPressRecognizer release];
    [super dealloc];
}

- (void) setDelegate:(id<MUMessageBubbleTableViewCellDelegate>)delegate {
    _delegate = delegate;
}

- (id<MUMessageBubbleTableViewCellDelegate>) delegate {
    return _delegate;
}

- (void) showAttachments:(id)sender {
    if (![_bubbleView isSelected])
        [_delegate messageBubbleTableViewCellRequestedAttachmentViewer:self];
}

- (void) showMenu:(id)sender {
    if (_longPressRecognizer.state == UIGestureRecognizerStateBegan) {
        if ([_bubbleView canBecomeFirstResponder]) {
            [_bubbleView becomeFirstResponder];
            [_bubbleView setSelected:YES];
            UIMenuController *menuController = [UIMenuController sharedMenuController];
            [menuController setTargetRect:[_bubbleView selectionRect] inView:_bubbleView];
            [menuController setMenuVisible:YES animated:YES];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuWillHide:) name:UIMenuControllerWillHideMenuNotification object:nil];
        }
    }
}

- (void) menuWillHide:(NSNotification *)notification {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_bubbleView resignFirstResponder];
    [_bubbleView setSelected:NO];
}

- (void) setSelected:(BOOL)selected {
    [_bubbleView setSelected:selected];
}

- (BOOL) isSelected {
    return [_bubbleView isSelected];
}

- (void) setHeading:(NSString *)heading {
    [_bubbleView setHeading:heading];
}

- (void) setMessage:(NSString *)msg {
    [_bubbleView setMessage:msg];
}

- (void) setShownImages:(NSArray *)shownImages {
    [_bubbleView setShownImages:shownImages];
}

- (void) setFooter:(NSString *)footer {
    [_bubbleView setFooter:footer];
}

- (void) setDate:(NSDate *)date {
    [_bubbleView setDate:date];
}

- (void) setRightSide:(BOOL)rightSide {
    [_bubbleView setRightSide:rightSide];
}

@end