File: GSAutocompleteWindow.m

package info (click to toggle)
gnustep-gui 0.25.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,088 kB
  • ctags: 3,417
  • sloc: objc: 153,800; ansic: 18,239; cpp: 579; yacc: 462; makefile: 143; sh: 5
file content (514 lines) | stat: -rw-r--r-- 13,281 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
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
/*
   Copyright (C) 2013 Free Software Foundation, Inc.

   Author: German A. Arias <german@xelalug.org>
   Date: 2013

   This file is part of the GNUstep GUI Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/

#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSRunLoop.h>
#import <Foundation/NSNotification.h>
#import "AppKit/NSApplication.h"
#import "AppKit/NSBox.h"
#import "AppKit/NSEvent.h"
#import "AppKit/NSScreen.h"
#import "AppKit/NSScrollView.h"
#import "AppKit/NSTableView.h"
#import "AppKit/NSTableColumn.h"
#import "AppKit/NSText.h"
#import "AppKit/NSTextView.h"
#import "GNUstepGUI/GSTheme.h"
#import "GSAutocompleteWindow.h"

static GSAutocompleteWindow *gsWindow = nil;

@interface NSTextView (Additions)
- (NSRect) rectForCharacterRange: (NSRange)aRange;
@end

@interface GSAutocompleteView : NSTableView
{
}
@end

@implementation GSAutocompleteView
- (BOOL) acceptsFirstMouse: (NSEvent *)event
{
  return YES;
}
@end

@implementation GSAutocompleteWindow

+ (GSAutocompleteWindow *) defaultWindow
{
  if (gsWindow == nil)
    gsWindow = [[self alloc] initWithContentRect: NSMakeRect(0,0,200,200)
			               styleMask: NSBorderlessWindowMask
			                 backing: NSBackingStoreNonretained
			                   defer: YES];

  return gsWindow;
}

- (id) initWithContentRect: (NSRect)contentRect
		 styleMask: (NSUInteger)aStyle
		   backing: (NSBackingStoreType)bufferingType
		     defer: (BOOL)flag
{
  NSBox *box;
  NSScrollView *scrollView;
  NSTableColumn *column;
  NSCell *cell;
   
  self = [super initWithContentRect: contentRect
		          styleMask: aStyle
		            backing: bufferingType
		              defer: flag];	  
  if (nil == self)
    return self;

  // Init vars
  _words = nil;
  _originalWord = nil;

  [self setLevel: NSPopUpMenuWindowLevel];
  [self setBecomesKeyOnlyIfNeeded: YES];
  
  box = [[NSBox alloc] initWithFrame: contentRect];
  [box setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
  [box setBorderType: NSLineBorder];
  [box setTitlePosition: NSNoTitle];
  [box setContentViewMargins: NSMakeSize(0, 0)];
  [self setContentView: box];
  [box release];
  
  _tableView = [[GSAutocompleteView alloc] 
                     initWithFrame: NSMakeRect(0, 0, 100, 100)];
  [_tableView setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
  [_tableView setDrawsGrid: NO];
  [_tableView setAllowsEmptySelection: YES];
  [_tableView setAllowsMultipleSelection: NO];
  [_tableView setAutoresizesAllColumnsToFit: YES];
  [_tableView setHeaderView: nil];
  [_tableView setCornerView: nil];
  
  column = [[NSTableColumn alloc] initWithIdentifier: @"content"];
  cell = [[NSCell alloc] initTextCell: @""];
  [column setDataCell: cell];
  [cell release];
  [_tableView addTableColumn: column];
  [column release];
  
  [_tableView setDataSource: self];
  [_tableView setDelegate: self];
  [_tableView setAction: @selector(clickItem:)];
  [_tableView setTarget: self];
  
  scrollView = [[NSScrollView alloc] initWithFrame:
				       NSMakeRect(contentRect.origin.x, 
						  contentRect.origin.y,
						  contentRect.size.width, 
						  contentRect.size.height)];
  [scrollView setHasVerticalScroller: YES];
  [scrollView setDocumentView: _tableView];
  [_tableView release];
  [box setContentView: scrollView];
  [scrollView release];

  return self;
}

- (void) dealloc
{
  /* Don't release _words and _originalWord, since these are
   * released when the autocomplete is final or canceled.
   */
  [super dealloc];
}

- (BOOL) canBecomeKeyWindow 
{ 
  return YES; 
}

- (void) layout
{
  NSSize bsize = [[GSTheme theme] sizeForBorderType: NSLineBorder];
  CGFloat windowWidth, windowHeight;
  NSInteger num = [_words count];
  NSUInteger widest = 0;
  NSCell *cell;
  NSString *word, *widestWord = nil;
  NSEnumerator *enumerator;

  /* If the suggested words are more than 10,
   * we limit the window to show 10.
   */
  if (num > 10)
    {
      num = 10;
    }

  /* Lookup the widest word to calculate the width
   * of the window.
   */
  enumerator = [_words objectEnumerator];
  while ((word = [enumerator nextObject]))
    {
      if ([word length] > widest)
	{
	  widest = [word length];
	  widestWord = word;
	}
    }

  // Width
  cell = [[_tableView tableColumnWithIdentifier: @"content"] dataCell];
  windowWidth = 1.1*[cell _sizeText: widestWord].width
    + [NSScroller scrollerWidth] + 2*bsize.width;

  //Height
  windowHeight = 2*bsize.height + [_tableView rowHeight]*num
    + [_tableView intercellSpacing].height;

  [self setFrame: NSMakeRect(0, 0, windowWidth, windowHeight) display: NO];
}

- (void) computePosition
{
  NSRect  screenFrame;
  NSRect  rect;
  NSRect  stringRect;
  NSPoint point;
  NSInterfaceStyle style;

  style = NSInterfaceStyleForKey(@"NSScrollViewInterfaceStyle", nil);

  rect = [self frame];
  screenFrame = [[[_textView window] screen] frame];

  // Get the rectangle to draw the current word.
  stringRect = [_textView rectForCharacterRange: _range];

  // Convert the origin point to screen coordinates.
  point = [[_textView window] convertBaseToScreen:
	   [_textView convertRect: stringRect toView: nil].origin];

  // Calculate the origin point to the window.
  if (style == NSMacintoshInterfaceStyle
      || style == NSWindows95InterfaceStyle)
    {
      rect.origin.x = point.x - 4;
    }
  else
    {
      rect.origin.x = point.x - [NSScroller scrollerWidth] - 4;
    }
  rect.origin.y = point.y - rect.size.height;

  // If part of the window is off screen, change the origin point.
  if (screenFrame.size.width < (rect.origin.x + rect.size.width))
    {
      rect.origin.x = screenFrame.size.width - rect.size.width;
    }
  else if (rect.origin.x < 0)
    {
      rect.origin.x = 0;
    }

  // If no space under the string, we display the window over this.
  if (rect.origin.y < 0)
    {
      rect.origin.y = point.y + stringRect.size.height;
    }

  [self setFrame: rect display: NO];
}

- (NSArray *) words
{
  return _words;
}

- (void) displayForTextView: (NSTextView *)textView
{
  _textView = textView;
  _range = [_textView rangeForUserCompletion];
  [self reloadData];

  if ([_words count] > 0)
    {
      [self runModalWindow];
    }
  else
    {
      [self close];
    }
}

- (void) runModalWindow
{
  NSWindow	*onWindow;
  NSNotificationCenter *notificationCenter;

  onWindow = [_textView window];
  notificationCenter = [NSNotificationCenter defaultCenter];

  // Get the appropriate notifications to cancel the autocomplete.

  [notificationCenter addObserver: self selector: @selector(onWindowEdited:)
    name: NSWindowWillCloseNotification object: onWindow];
  [notificationCenter addObserver: self selector: @selector(onWindowEdited:)
    name: NSWindowWillMiniaturizeNotification object: onWindow];
  // The notification below don't seems to work.
  [notificationCenter addObserver: self selector: @selector(onWindowEdited:)
    name: NSWindowWillMoveNotification object: onWindow];
    
  // FIX ME: The notification below doesn't exist currently
  // [nc addObserver: self selector: @selector(onWindowEdited:) 
  //   name: NSWindowWillResizeNotification object: onWindow];
  
  // FIXME: The code below must be removed when the notifications over will work
  [notificationCenter addObserver: self selector: @selector(onWindowEdited:) 
    name: NSWindowDidMoveNotification object: onWindow];
  [notificationCenter addObserver: self selector: @selector(onWindowEdited:)
    name: NSWindowDidResizeNotification object: onWindow];
  // End of the code to remove
  
  [self orderFront: self];
  [self makeFirstResponder: _tableView];

  [self runLoop];
  
  [notificationCenter removeObserver: self name: nil object: onWindow];
  [self close];
  [onWindow makeFirstResponder: _textView];
}

- (void) runLoop
{
  NSEvent *event;
  NSDate *limit = [NSDate distantFuture];
  unichar key;
  CREATE_AUTORELEASE_POOL (pool);

  _stopped = NO;

  while (YES)
    {
      event = [NSApp nextEventMatchingMask: NSAnyEventMask
		                 untilDate: limit
		                    inMode: NSDefaultRunLoopMode
		                   dequeue: YES];

      if ([event type] == NSLeftMouseDown
	  || [event type] == NSRightMouseDown)
        {
          if ([event window] != self)
	    {
	      [self updateTextViewWithMovement: NSCancelTextMovement
				       isFinal: NO];
              break;
	    }
	  else
	    {
	      [NSApp sendEvent: event];
	    }
        }
      else if ([event type] == NSKeyDown)
        {
	  key = [[event characters] characterAtIndex: 0];

          if (key == NSUpArrowFunctionKey)
            {
              [self moveUpSelection];
	      [self updateTextViewWithMovement: NSUpTextMovement
				       isFinal: NO];
            }
          else if (key == NSDownArrowFunctionKey)
            {
              [self moveDownSelection];
	      [self updateTextViewWithMovement: NSDownTextMovement
				       isFinal: NO];
            }
          else if (key == NSEnterCharacter ||
		   key == NSCarriageReturnCharacter ||
		   key == NSNewlineCharacter)
	    {
	      [self clickItem: self];
	      break;
	    }
	  else if (key == 0x001b ||
		   key == NSRightArrowFunctionKey ||
		   key == NSLeftArrowFunctionKey)
	    {
	      [self updateTextViewWithMovement: NSCancelTextMovement
				       isFinal: NO];
	      break;
	    }
          else
            {
	      // First remove the selected text.
	      [_textView replaceCharactersInRange: [_textView selectedRange]
				       withString: @""];
	      // Send the even to update the text container.
	      [NSApp sendEvent: event];
	      // Reload data.
	      [self reloadData];
	    }
	}
      else
        {
	  [NSApp sendEvent: event];
	}
      
      if (_stopped)
        break;      
    }

  [pool drain];
}

- (void) onWindowEdited: (NSNotification *)notification
{
  _stopped = YES;
  [self updateTextViewWithMovement: NSCancelTextMovement
			   isFinal: NO];
}

- (void) reloadData
{
  _range = [_textView rangeForUserCompletion];

  if (_range.location == NSNotFound || _range.length == 0)
    {
      _stopped = YES;
    }
  else
    {
      NSInteger index = 0;
      NSString *word;
      NSArray *newWords;

      word = [[_textView string] substringWithRange: _range];
      ASSIGN(_originalWord, word);

      newWords = [_textView completionsForPartialWordRange: _range
				       indexOfSelectedItem: &index];

      if ([newWords count] > 0)
	{
	  ASSIGN(_words, newWords);
	  [_tableView reloadData];
	  [self layout];
	  [self computePosition];
	  [_tableView selectRow: index byExtendingSelection: NO];
	  [_tableView scrollRowToVisible: index];
	  [self updateTextViewWithMovement: NSOtherTextMovement
				   isFinal: NO];
	}
      else
	{
	  [_tableView reloadData];
	  _stopped = YES;
	  [self updateTextViewWithMovement: NSCancelTextMovement
				   isFinal: NO];
	}
    }
}

- (void) updateTextViewWithMovement: (NSInteger)movement
			    isFinal: (BOOL)flag
{
  NSString *word;

  if (movement != NSCancelTextMovement)
    {
      NSInteger rowIndex = [_tableView selectedRow];
      word = [[_words objectAtIndex: rowIndex] description];
    }
  else
    {
      word = _originalWord;
    }

  [_textView insertCompletion: word
	  forPartialWordRange: _range
		     movement: movement
		      isFinal: flag];

  // Release _words and _originalWords if
  // autocomplete is final or canceled.
  if ( (flag) ||
       (movement == NSCancelTextMovement) )
    {
      ASSIGN(_originalWord, nil);
      ASSIGN(_words, nil);
    }
}

// Action method
- (void) clickItem: (id)sender
{
  [self updateTextViewWithMovement: NSOtherTextMovement
			   isFinal: YES];

  _stopped = YES;
}

// Key actions methods
- (void) moveUpSelection
{
  NSInteger index = [_tableView selectedRow] - 1;

  if (index > -1 && index < [_tableView numberOfRows])
    {
      [_tableView selectRow: index byExtendingSelection: NO];
      [_tableView scrollRowToVisible: index];
    }
}

- (void) moveDownSelection
{
  NSInteger index = [_tableView selectedRow] + 1;
  
  if (index > -1 && index < [_tableView numberOfRows])
    {
      [_tableView selectRow: index byExtendingSelection: NO];
      [_tableView scrollRowToVisible: index];
    }
}

// Delegate
- (int) numberOfRowsInTableView: (NSTableView *)aTableView
{
  return [_words count];
}

- (id) tableView: (NSTableView *)aTableView
  objectValueForTableColumn: (NSTableColumn *)aTableColumn
	     row: (int)rowIndex
{
  return [[_words objectAtIndex: rowIndex] description];
}
@end