Debian Codegraph @

src/af/xap/cocoa/xap_CocoaToolPalette.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiSource Application Framework
4 * Copyright (C) 2004 AbiSource, Inc.
5 * Copyright (C) 2004 Francis James Franklin <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301 USA.
21 */
22
23
24 #include "ut_assert.h"
25 #include "ut_debugmsg.h"
26 #include "ut_string_class.h"
27
28 #import "xap_CocoaApp.h"
29 #import "xap_CocoaAppController.h"
30 #import "xap_CocoaToolPalette.h"
31 #import "xap_CocoaToolbar_Icons.h"
32 #import "xap_CocoaCompat.h"
33
34 #include "xap_Frame.h"
35 #include "xap_Toolbar_LabelSet.h"
36 #include "xav_View.h"
37 #include "xap_Prefs.h"
38 #include "xap_Prefs_SchemeIds.h"
39
40 #include "ev_EditEventMapper.h"
41 #include "ev_Menu_Actions.h"
42 #include "ev_Toolbar_Actions.h"
43 #include "ev_Toolbar_Layouts.h"
44 #include "ev_Toolbar_Labels.h"
45 #include "ev_Toolbar_Control.h"
46
47 #include "pd_Document.h"
48 #include "pd_Style.h"
49
50 #include "pp_AttrProp.h"
51
52 #include "fv_View.h"
53
54 #include "ap_Menu_Id.h"
55
56 enum _XAP_CocoaTool_Id
57 {
58 #define defn(T,X,Y) ToolID_##T,
59 #include "xap_CocoaTools.h"
60 #undef defn
61 XAP_COCOA_TOOL_ID__COUNT_
62 };
63
64 @interface XAP_CocoaPalette : NSObject
65 {
66 NSString * m_Name;
67 NSButton * m_Title;
68 NSBox * m_Box;
69
70 UT_uint32 m_heightTitle;
71 UT_uint32 m_heightBox;
72 }
73 - (id)initWithPalette:(const struct XAP_CocoaPaletteRef *)palette;
74 - (void)dealloc;
75
76 - (NSString *)Name;
77 - (NSButton *)Title;
78 - (NSBox *)Box;
79
80 - (BOOL)isExpanded;
81
82 - (UT_uint32)heightTitle;
83 - (UT_uint32)heightBox;
84 - (UT_uint32)heightConditional;
85 @end
86
87 @implementation XAP_CocoaPalette
88
89 - (id)initWithPalette:(const struct XAP_CocoaPaletteRef *)palette
90 {
91 if(![super init]) {
92 return nil;
93 }
94 UT_ASSERT(palette);
95 UT_ASSERT(palette->Name);
96
97 m_Name = [[NSString alloc] initWithString:(palette->Name)];
98 UT_ASSERT(palette->Title && palette->Box);
99
100 m_Title = palette->Title;
101 m_Box = palette->Box;
102
103 [m_Title retain];
104 [m_Box retain];
105
106 NSRect frame;
107
108 frame = [m_Title frame];
109 m_heightTitle = static_cast<UT_uint32>(frame.size.height);
110
111 frame = [m_Box frame];
112 m_heightBox = static_cast<UT_uint32>(frame.size.height);
113 return self;
114 }
115
116 - (void)dealloc
117 {
118 if (m_Name)
119 {
120 [m_Name release];
121 }
122 if (m_Title)
123 {
124 [m_Title release];
125 }
126 if (m_Box)
127 {
128 [m_Box release];
129 }
130 [super dealloc];
131 }
132
133 - (NSString *)Name
134 {
135 return m_Name;
136 }
137
138 - (NSButton *)Title
139 {
140 return m_Title;
141 }
142
143 - (NSBox *)Box
144 {
145 return m_Box;
146 }
147
148 - (BOOL)isExpanded
149 {
150 return ([m_Title tag] == 1) ? YES : NO;
151 }
152
153 - (UT_uint32)heightTitle
154 {
155 return m_heightTitle;
156 }
157
158 - (UT_uint32)heightBox
159 {
160 return m_heightBox;
161 }
162
163 - (UT_uint32)heightConditional
164 {
165 UT_uint32 height = m_heightTitle;
166
167 if ([m_Title tag] == 1)
168 {
169 height += m_heightBox;
170 }
171 return height;
172 }
173
174 @end
175
176 @implementation XAP_CocoaPaletteView
177
178 - (id)init
179 {
180 NSRect frame;
181 frame.origin.x = 0;
182 frame.origin.y = 0;
183 frame.size.width = 300;
184 frame.size.height = 100;
185
186 self = [super initWithFrame:frame];
187 if (self) // ignore frame here
188 {
189 m_Palette = [[NSMutableArray alloc] initWithCapacity:4];
190 }
191 return self;
192 }
193
194 - (void)dealloc
195 {
196 if (m_Palette)
197 {
198 [m_Palette release];
199 m_Palette = 0;
200 }
201 [super dealloc];
202 }
203
204 - (void)addPalette:(const struct XAP_CocoaPaletteRef *)paletteRef
205 {
206 XAP_CocoaPalette * palette = [[XAP_CocoaPalette alloc] initWithPalette:paletteRef];
207 if (palette)
208 {
209 [m_Palette addObject:palette];
210 [palette release];
211
212 [paletteRef->Title removeFromSuperviewWithoutNeedingDisplay];
213 [paletteRef->Box removeFromSuperviewWithoutNeedingDisplay];
214
215 [self sync]; // update frames
216
217 [self addSubview:(paletteRef->Title)];
218 [self addSubview:(paletteRef->Box )];
219
220 [self setNeedsDisplay:YES];
221 }
222 }
223
224 #define PALETTE_ELEMENT_ORIGIN_X -3
225 #define PALETTE_ELEMENT_WIDTH 219
226 #define PALETTE_PANEL_WIDTH 213
227
228 - (void)sync
229 {
230 unsigned count = [m_Palette count];
231 unsigned i;
232
233 if (!count) {
234 return; // huh. no palettes
235 }
236
237 /* First, calculate total height
238 */
239 UT_uint32 view_height = 0;
240
241 for (i = 0; i < count; i++)
242 {
243 XAP_CocoaPalette * palette = (XAP_CocoaPalette *) [m_Palette objectAtIndex:i];
244
245 view_height += [palette heightConditional];
246 }
247
248 NSSize size;
249 size.width = PALETTE_PANEL_WIDTH;
250 size.height = static_cast<float>(view_height);
251
252 NSRect frame;
253
254 UT_uint32 vh_remember = view_height;
255
256 /* Adjust frames of individual palettes
257 */
258 for (i = 0; i < count; i++)
259 {
260 XAP_CocoaPalette * palette = (XAP_CocoaPalette *) [m_Palette objectAtIndex:i];
261
262 UT_uint32 height = [palette heightTitle];
263
264 view_height -= height;
265
266 /* Title
267 */
268
269 /* Box
270 */
271 height = [palette heightBox];
272
273 if ([palette isExpanded])
274 {
275 view_height -= height;
276 }
277 else // move it out of sight
278 {
279 [self setNeedsDisplayInRect:[[palette Box] frame]];
280
281 frame.origin.x = PALETTE_ELEMENT_WIDTH;
282 frame.origin.y = static_cast<float>(view_height);
283 frame.size.width = PALETTE_ELEMENT_WIDTH;
284 frame.size.height = static_cast<float>(height);
285 [[palette Box] setFrame:frame];
286 }
287 }
288 [self displayIfNeeded];
289
290 view_height = vh_remember;
291
292 /* Update panel size, if in a panel, else set the frame size
293 */
294 NSWindow * window = [self window];
295 if (window)
296 {
297 // [self setNeedsDisplay:NO];
298
299 NSRect current = [window frame];
300
301 int y = static_cast<int>(current.origin.y) + static_cast<int>(current.size.height);
302
303 NSRect content;
304 content.origin.x = 0;
305 content.origin.y = 0;
306 content.size.width = size.width;
307 content.size.height = size.height;
308
309 frame = [NSWindow frameRectForContentRect:content styleMask:(NSTitledWindowMask|NSClosableWindowMask|NSUtilityWindowMask)];
310 frame.origin.x = current.origin.x;
311 frame.origin.y = static_cast<float>(y - static_cast<int>(frame.size.height));
312 [window setFrame:frame display:YES animate:YES];
313 }
314 else
315 {
316 frame.origin.x = 0;
317 frame.origin.y = 0;
318 frame.size.width = size.width;
319 frame.size.height = size.height;
320 [self setFrame:frame];
321 }
322
323 /* Adjust frames of individual palettes
324 */
325 for (i = 0; i < count; i++)
326 {
327 XAP_CocoaPalette * palette = (XAP_CocoaPalette *) [m_Palette objectAtIndex:i];
328
329 UT_uint32 height = [palette heightTitle];
330
331 view_height -= height;
332
333 /* Title
334 */
335 frame.origin.x = PALETTE_ELEMENT_ORIGIN_X;
336 frame.origin.y = static_cast<float>(view_height);
337 frame.size.width = PALETTE_ELEMENT_WIDTH;
338 frame.size.height = static_cast<float>(height);
339 [[palette Title] setFrame:frame];
340 [[palette Title] setNeedsDisplay:YES];
341 [[palette Title] displayIfNeeded];
342
343 /* Box
344 */
345 height = [palette heightBox];
346
347 if ([palette isExpanded])
348 {
349 view_height -= height;
350
351 frame.origin.x = PALETTE_ELEMENT_ORIGIN_X;
352 frame.origin.y = static_cast<float>(view_height);
353 frame.size.width = PALETTE_ELEMENT_WIDTH;
354 frame.size.height = static_cast<float>(height);
355 [[palette Box] setFrame:frame];
356 [[palette Box] setNeedsDisplay:YES];
357 [[palette Box] displayIfNeeded];
358 }
359 else // move it out of sight
360 {
361 frame.origin.x = PALETTE_ELEMENT_WIDTH;
362 frame.origin.y = static_cast<float>(view_height);
363 frame.size.width = PALETTE_ELEMENT_WIDTH;
364 frame.size.height = static_cast<float>(height);
365 [[palette Box] setFrame:frame];
366 }
367 }
368 }
369
370 @end
371
372 @interface XAP_PaletteProperties_Level : NSObject
373 {
374 BOOL m_IsLevel;
375
376 NSString * m_Name;
377 NSString * m_Value;
378
379 NSMutableArray * m_Properties;
380 }
381 - (id)initWithAP:(const PP_AttrProp *)pAP levelName:(NSString *)name;
382 - (id)initWithStyle:(PD_Style *)style levelName:(NSString *)name;
383 - (id)initWithPropertyName:(NSString *)name propertyValue:(NSString *)value;
384 - (void)dealloc;
385
386 - (int)numberOfChildrenOfItem;
387 - (BOOL)isItemExpandable;
388 - (id)child:(int)index;
389 - (id)objectValueForTableColumn:(NSTableColumn *)tableColumn;
390 @end
391
392 @implementation XAP_PaletteProperties_Level
393
394 - (id)initWithAP:(const PP_AttrProp *)pAP levelName:(NSString *)pname
395 {
396 if (![super init])
397 {
398 return nil;
399 }
400
401 m_IsLevel = YES;
402
403 m_Name = pname;
404 m_Value = @"";
405
406 [m_Name retain];
407 [m_Value retain];
408
409 m_Properties = nil;
410
411 UT_sint32 count = pAP->getPropertyCount();
412 if (count) {
413 m_Properties = [[NSMutableArray alloc] initWithCapacity:((unsigned) count)];
414 if (!m_Properties) {
415 [self release];
416 return nil;
417 }
418 for (UT_sint32 i = 0; i < count; i++) {
419 const char * szName = NULL;
420 const char * szValue = NULL;
421
422 if (pAP->getNthProperty(i, szName, szValue)) {
423 NSString * name = [NSString stringWithUTF8String:(szName )];
424 NSString * value = [NSString stringWithUTF8String:(szValue)];
425
426 XAP_PaletteProperties_Level * property = [[XAP_PaletteProperties_Level alloc] initWithPropertyName:name propertyValue:value];
427 if (property) {
428 [m_Properties addObject:property];
429 [property release];
430 }
431 }
432 }
433 }
434 return self;
435 }
436
437 - (id)initWithStyle:(PD_Style *)style levelName:(NSString *)pname
438 {
439 if (![super init]) {
440 return nil;
441 }
442 m_IsLevel = YES;
443
444 m_Name = pname;
445 m_Value = @"";
446
447 [m_Name retain];
448 [m_Value retain];
449
450 m_Properties = nil;
451
452 UT_sint32 count = static_cast<int>(style->getPropertyCount());
453 if (count) {
454 m_Properties = [[NSMutableArray alloc] initWithCapacity:((unsigned) count)];
455 if (!m_Properties) {
456 [self release];
457 return nil;
458 }
459 for (UT_sint32 i = 0; i < count; i++) {
460 const char * szName = NULL;
461 const char * szValue = NULL;
462
463 if (style->getNthProperty(i, szName, szValue)) {
464 NSString * name = [NSString stringWithUTF8String:(szName )];
465 NSString * value = [NSString stringWithUTF8String:(szValue)];
466
467 XAP_PaletteProperties_Level * property = [[XAP_PaletteProperties_Level alloc] initWithPropertyName:name propertyValue:value];
468 if (property) {
469 [m_Properties addObject:property];
470 [property release];
471 }
472 }
473 }
474 }
475 return self;
476 }
477
478 - (id)initWithPropertyName:(NSString *)name propertyValue:(NSString *)value
479 {
480 if(![super init]) {
481 return nil;
482 }
483 m_IsLevel = NO;
484
485 m_Name = name;
486 m_Value = value;
487
488 [m_Name retain];
489 [m_Value retain];
490
491 m_Properties = nil;
492 return self;
493 }
494
495 - (void)dealloc
496 {
497 if (m_Name)
498 {
499 [m_Name release];
500 }
501 if (m_Value)
502 {
503 [m_Value release];
504 }
505 if (m_Properties)
506 {
507 [m_Properties release];
508 }
509 [super dealloc];
510 }
511
512 - (int)numberOfChildrenOfItem
513 {
514 if (m_IsLevel && m_Properties)
515 {
516 return [m_Properties count];
517 }
518 return 0;
519 }
520
521 - (BOOL)isItemExpandable
522 {
523 return m_IsLevel;
524 }
525
526 - (id)child:(int)index
527 {
528 if (m_IsLevel /* && m_Properties */)
529 {
530 return [m_Properties objectAtIndex:((unsigned) index)];
531 }
532 return 0; // ??
533 }
534
535 - (id)objectValueForTableColumn:(NSTableColumn *)tableColumn
536 {
537 NSTableHeaderCell * headerCell = [tableColumn headerCell];
538
539 if ([[headerCell stringValue] isEqualToString:@"Value"])
540 {
541 return m_Value;
542 }
543 return m_Name;
544 }
545
546 @end
547
548 static PD_Style * _getStyle(const PP_AttrProp * pAttrProp, PD_Document * pDoc)
549 {
550 PD_Style * pStyle = 0;
551
552 const gchar * szValue = 0;
553
554 // This is where the style/name split gets really hairy. This index AP MIGHT be
555 // from a style definition in which case the name of the style is PT_NAME_ATTRIBUTE_NAME
556 // or it might be from the document in which case the attribute is
557 // PT_STYLE_ATTRIBUTE_NAME. - MES.
558
559 if (pAttrProp->getAttribute(PT_NAME_ATTRIBUTE_NAME, szValue))
560 {
561 UT_return_val_if_fail (szValue && szValue[0], 0);
562 if (pDoc) {
563 pDoc->getStyle(reinterpret_cast<const char*>(szValue), &pStyle);
564 }
565
566 // NOTE: we silently fail if style is referenced, but not defined
567 }
568 else if(pAttrProp->getAttribute(PT_STYLE_ATTRIBUTE_NAME, szValue))
569 {
570 UT_return_val_if_fail (szValue && szValue[0], 0);
571 if (pDoc) {
572 pDoc->getStyle(reinterpret_cast<const char*>(szValue), &pStyle);
573 }
574
575 // NOTE: we silently fail if style is referenced, but not defined
576 }
577 return pStyle;
578 }
579
580 @implementation XAP_PaletteProperties_DataSource
581
582 - (id)initWithOutlineView:(NSOutlineView *)outlineView
583 {
584 if (![super init])
585 {
586 return nil;
587 }
588 m_OutlineView = outlineView;
589
590 m_PropertyLevels = [[NSMutableArray alloc] initWithCapacity:6];
591 return self;
592 }
593
594 - (void)dealloc
595 {
596 [m_OutlineView setDelegate:nil];
597 [m_OutlineView setDataSource:nil];
598
599 if (m_PropertyLevels)
600 {
601 [m_PropertyLevels release];
602 m_PropertyLevels = 0;
603 }
604 [super dealloc];
605 }
606
607 - (void)syncWithView:(FV_View *)pView
608 {
609 if (m_PropertyLevels)
610 {
611 [m_PropertyLevels removeAllObjects];
612 }
613 if (pView)
614 {
615 const PP_AttrProp * pSpanAP = 0;
616 const PP_AttrProp * pBlockAP = 0;
617 const PP_AttrProp * pSectionAP = 0;
618 const PP_AttrProp * pDocAP = 0;
619
620 if (pView->getAllAttrProp(pSpanAP, pBlockAP, pSectionAP, pDocAP))
621 {
622 if (pSpanAP)
623 {
624 if (XAP_PaletteProperties_Level * pPPLevel = [[XAP_PaletteProperties_Level alloc] initWithAP:pSpanAP levelName:@"Span"])
625 {
626 [m_PropertyLevels addObject:pPPLevel];
627 [pPPLevel release];
628 }
629 if (PD_Style * style = _getStyle(pSpanAP, pView->getDocument()))
630 {
631 NSString * name = [NSString stringWithUTF8String:(style->getName())];
632 if (XAP_PaletteProperties_Level * pPPLevel = [[XAP_PaletteProperties_Level alloc] initWithStyle:style levelName:name])
633 {
634 [m_PropertyLevels addObject:pPPLevel];
635 [pPPLevel release];
636 }
637 }
638 }
639
640 if (pBlockAP)
641 {
642 if (XAP_PaletteProperties_Level * pPPLevel = [[XAP_PaletteProperties_Level alloc] initWithAP:pBlockAP levelName:@"Block"])
643 {
644 [m_PropertyLevels addObject:pPPLevel];
645 [pPPLevel release];
646 }
647 if (PD_Style * style = _getStyle(pBlockAP, pView->getDocument()))
648 {
649 NSString * name = [NSString stringWithUTF8String:(style->getName())];
650 if (XAP_PaletteProperties_Level * pPPLevel = [[XAP_PaletteProperties_Level alloc] initWithStyle:style levelName:name])
651 {
652 [m_PropertyLevels addObject:pPPLevel];
653 [pPPLevel release];
654 }
655 }
656 }
657
658 if (pSectionAP)
659 {
660 if (XAP_PaletteProperties_Level * pPPLevel = [[XAP_PaletteProperties_Level alloc] initWithAP:pSectionAP levelName:@"Section"])
661 {
662 [m_PropertyLevels addObject:pPPLevel];
663 [pPPLevel release];
664 }
665 }
666 if (pDocAP)
667 {
668 if (XAP_PaletteProperties_Level * pPPLevel = [[XAP_PaletteProperties_Level alloc] initWithAP:pDocAP levelName:@"Document"])
669 {
670 [m_PropertyLevels addObject:pPPLevel];
671 [pPPLevel release];
672 }
673 }
674 }
675 }
676 [m_OutlineView reloadData];
677 }
678
679 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
680 {
681 UT_UNUSED(outlineView);
682 if (item)
683 {
684 XAP_PaletteProperties_Level * pPPLevel = (XAP_PaletteProperties_Level *) item;
685 return [pPPLevel numberOfChildrenOfItem];
686 }
687 return (int) [m_PropertyLevels count];
688 }
689
690 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
691 {
692 UT_UNUSED(outlineView);
693 if (item)
694 {
695 XAP_PaletteProperties_Level * pPPLevel = (XAP_PaletteProperties_Level *) item;
696 return [pPPLevel isItemExpandable];
697 }
698 return YES; // ??
699 }
700
701 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
702 {
703 UT_UNUSED(outlineView);
704 if (item)
705 {
706 XAP_PaletteProperties_Level * pPPLevel = (XAP_PaletteProperties_Level *) item;
707 return [pPPLevel child:index];
708 }
709 return [m_PropertyLevels objectAtIndex:((unsigned) index)];
710 }
711
712 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
713 {
714 UT_UNUSED(outlineView);
715 if (item)
716 {
717 XAP_PaletteProperties_Level * pPPLevel = (XAP_PaletteProperties_Level *) item;
718 return [pPPLevel objectValueForTableColumn:tableColumn];
719 }
720 return @"??"; // ??
721 }
722
723 - (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
724 {
725 UT_UNUSED(outlineView);
726 UT_UNUSED(tableColumn);
727 UT_UNUSED(item);
728 [cell setFont:[NSFont systemFontOfSize:10.0f]];
729 }
730
731 - (void)outlineView:(NSOutlineView *)outlineView willDisplayOutlineCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
732 {
733 UT_UNUSED(outlineView);
734 UT_UNUSED(tableColumn);
735 UT_UNUSED(item);
736 [cell setFont:[NSFont systemFontOfSize:10.0f]];
737 }
738
739 @end
740
741 @implementation XAP_CocoaPreviewPanel
742
743 - (id)init
744 {
745 return [super init];
746 }
747
748 - (void)dealloc
749 {
750 //
751 [super dealloc];
752 }
753
754 - (void)windowDidLoad
755 {
756 NSPanel * panel = (NSPanel *) [self window];
757
758 [panel setBecomesKeyOnlyIfNeeded:YES];
759
760 [oPreview setStringValue:@"Preview"];
761
762 XAP_CocoaAppController * pController = (XAP_CocoaAppController *) [NSApp delegate];
763
764 if (NSMenuItem * item = [[NSMenuItem alloc] initWithTitle:@"Preview Panel" action:@selector(toggleVisibility:) keyEquivalent:@"I"])
765 {
766 [item setTarget:self];
767 [pController appendPanelItem:item];
768 [item release];
769 }
770 }
771
772 - (void)toggleVisibility:(id)sender
773 {
774 UT_UNUSED(sender);
775 NSPanel * panel = (NSPanel *) [self window];
776
777 if ([panel isVisible]) {
778 [panel orderOut:self];
779 }
780 else {
781 [panel orderFront:self];
782 }
783 }
784
785 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
786 {
787 NSPanel * panel = (NSPanel *) [self window];
788
789 [menuItem setState:([panel isVisible] ? NSOnState : NSOffState)];
790
791 return YES;
792 }
793
794 - (void)setPreviewString:(NSString *)previewString
795 {
796 [oPreview setStringValue:previewString];
797 }
798
799 @end
800
801 static XAP_CocoaToolPalette * s_instance = nil;
802
803 @implementation XAP_CocoaToolPalette
804
805 + (XAP_CocoaToolPalette *)instance:(id)sender
806 {
807 if (!s_instance) {
808 s_instance = [[XAP_CocoaToolPalette alloc] init];
809 if (!s_instance)
810 {
811 return nil;
812 }
813 bool visible = false;
814 XAP_Prefs *pPrefs = XAP_App::getApp()->getPrefs();
815 UT_ASSERT(pPrefs);
816
817 XAP_PrefsScheme *pPrefsScheme = pPrefs->getCurrentScheme();
818 UT_ASSERT(pPrefsScheme);
819
820 pPrefsScheme->getValueBool(XAP_PREF_KEY_ToolPaletteVisible, &visible);
821 if (visible) {
822 [s_instance showWindow:sender];
823 }
824 }
825 return s_instance;
826 }
827
828 + (BOOL)instantiated
829 {
830 return (s_instance ? YES : NO);
831 }
832
833 - (id)init
834 {
835 if(![super initWithWindowNibName:@"xap_CocoaToolPalette"]) {
836 return nil;
837 }
838 m_ToolChest = 0;
839 m_PaletteView = 0;
840 m_Properties_DataSource = 0;
841
842 m_pMenuActionSet = 0;
843 m_pToolbarActionSet = 0;
844 m_pEditMethodContainer = 0;
845 m_pFontFamilies = 0;
846 m_pCurrentFontFamily = 0;
847 m_Listener = 0;
848
849 XAP_CocoaApp * pCocoaApp = dynamic_cast<XAP_CocoaApp *>(XAP_App::getApp());
850 UT_ASSERT(pCocoaApp);
851
852 m_pMenuActionSet = pCocoaApp->getMenuActionSet();
853 m_pToolbarActionSet = pCocoaApp->getToolbarActionSet();
854 m_pEditMethodContainer = pCocoaApp->getEditMethodContainer();
855
856 if (!m_pToolbarActionSet || !m_pMenuActionSet || !m_pEditMethodContainer)
857 {
858 UT_ASSERT(m_pToolbarActionSet && m_pMenuActionSet && m_pEditMethodContainer);
859 }
860 NSArray * pAvailableFontFamilies = [[NSFontManager sharedFontManager] availableFontFamilies];
861
862 unsigned count = [pAvailableFontFamilies count];
863 if (count)
864 {
865 m_pFontFamilies = [[NSMutableArray alloc] initWithCapacity:count];
866 if (m_pFontFamilies) {
867 for (unsigned ff = 0; ff < count; ff++) {
868 NSString * pFontFamily = [pAvailableFontFamilies objectAtIndex:ff];
869
870 /* const char * szFF = [pFontFamily UTF8String]; */
871
872 if (true /* (*szFF != '.') && (*szFF != '#') */) // cf. Bug 6638
873 {
874 [m_pFontFamilies addObject:pFontFamily];
875 }
876 }
877 if ([m_pFontFamilies count]) {
878 [m_pFontFamilies sortUsingSelector:@selector(compare:)];
879 }
880 else {
881 UT_DEBUGMSG(("XAP_CocoaToolPalette -init: no usable font families?\n"));
882
883 [m_pFontFamilies release];
884 m_pFontFamilies = 0;
885 }
886 }
887 }
888 else
889 {
890 UT_DEBUGMSG(("XAP_CocoaToolPalette -init: no available font families?\n"));
891 }
892 try
893 {
894 m_ToolChest = new struct XAP_CocoaToolRef[XAP_COCOA_TOOL_ID__COUNT_];
895 }
896 catch(...)
897 {
898 m_ToolChest = nil;
899 }
900 m_PaletteView = [[XAP_CocoaPaletteView alloc] init];
901 try
902 {
903 m_Listener = new XAP_CocoaToolPaletteListener(self);
904 }
905 catch(...)
906 {
907 m_Listener = 0;
908 }
909 return self;
910 }
911
912 - (void)dealloc
913 {
914 if (m_Properties_DataSource)
915 {
916 [m_Properties_DataSource release];
917 m_Properties_DataSource = 0;
918 }
919 if (m_PaletteView) {
920 if (![m_PaletteView superview])
921 {
922 [m_PaletteView dealloc];
923 m_PaletteView = 0;
924 }
925 }
926 if (m_pFontFamilies)
927 {
928 [m_pFontFamilies release];
929 m_pFontFamilies = 0;
930 }
931 if (m_pCurrentFontFamily)
932 {
933 [m_pCurrentFontFamily release];
934 m_pCurrentFontFamily = 0;
935 }
936 DELETEP(m_ToolChest);
937 DELETEP(m_Listener);
938
939 [super dealloc];
940 }
941
942 - (void)windowDidLoad
943 {
944 UT_DEBUGMSG(("XAP_CocoaToolPalette -windowDidLoad\n"));
945
946 XAP_CocoaAppController * pController = (XAP_CocoaAppController *) [NSApp delegate];
947
948 if (m_PaletteView)
949 {
950 // TODO: localise palette titles based on toolbar names
951
952 struct XAP_CocoaPaletteRef palette;
953
954 [oTitle_Standard setTag:1];
955 palette.Name = @"Standard";
956 palette.Title = oTitle_Standard;
957 palette.Box = oBox_Standard;
958 [m_PaletteView addPalette:&palette];
959
960 [oTitle_Format setTag:1];
961 palette.Name = @"Format";
962 palette.Title = oTitle_Format;
963 palette.Box = oBox_Format;
964 [m_PaletteView addPalette:&palette];
965
966 [oTitle_Table setTag:0];
967 palette.Name = @"Table";
968 palette.Title = oTitle_Table;
969 palette.Box = oBox_Table;
970 [m_PaletteView addPalette:&palette];
971
972 [oTitle_Extra setTag:0];
973 palette.Name = @"Extra";
974 palette.Title = oTitle_Extra;
975 palette.Box = oBox_Extra;
976 [m_PaletteView addPalette:&palette];
977
978 [oTitle_Properties setTag:0];
979 palette.Name = @"Properties";
980 palette.Title = oTitle_Properties;
981 palette.Box = oBox_Properties;
982 [m_PaletteView addPalette:&palette];
983
984 NSRect content = [m_PaletteView frame];
985 NSRect frame = [NSWindow frameRectForContentRect:content styleMask:(NSTitledWindowMask|NSClosableWindowMask|NSUtilityWindowMask)];
986
987 NSRect visible_frame = [[oPanel screen] visibleFrame];
988 int x = static_cast<int>(visible_frame.size.width ) + static_cast<int>(visible_frame.origin.x);
989 int y = static_cast<int>(visible_frame.size.height) + static_cast<int>(visible_frame.origin.y);
990
991 frame.origin.x = static_cast<float>(x - static_cast<int>(frame.size.width ));
992 frame.origin.y = static_cast<float>(y - static_cast<int>(frame.size.height));
993
994 [oPanel setFrame:frame display:NO];
995 [oPanel setContentView:m_PaletteView];
996
997 [m_PaletteView setNeedsDisplay:YES];
998
999 [oPanel setFloatingPanel:YES];
1000 [oPanel setBecomesKeyOnlyIfNeeded:YES];
1001 [oPanel setReleasedWhenClosed:YES];
1002 [oPanel setDelegate:self];
1003
1004 [self setWindow:oPanel];
1005
1006 if (NSMenuItem * item = [[NSMenuItem alloc] initWithTitle:@"Tool Palette" action:@selector(toggleVisibility:) keyEquivalent:@"T"])
1007 {
1008 [item setTarget:self];
1009 [pController appendPanelItem:item];
1010 [item release];
1011 }
1012 }
1013
1014 #define defn(T,X,Y) m_ToolChest[ToolID_##T].button = T; \
1015 m_ToolChest[ToolID_##T].tlbrid = X; \
1016 m_ToolChest[ToolID_##T].ttipid = Y;
1017 #include "xap_CocoaTools.h"
1018 #undef defn
1019
1020 const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();
1021
1022 const char * tooltip = 0;
1023
1024 for (int i = 0; i < XAP_COCOA_TOOL_ID__COUNT_; i++)
1025 {
1026 [m_ToolChest[i].button setTag:i];
1027 [m_ToolChest[i].button setEnabled:NO];
1028 [m_ToolChest[i].button setBordered:NO];
1029
1030 if (m_ToolChest[i].ttipid != AP_STRING_ID__FIRST__) {
1031 tooltip = pSS->getValue(m_ToolChest[i].ttipid);
1032 if (tooltip)
1033 {
1034 [m_ToolChest[i].button setToolTip:[NSString stringWithUTF8String:tooltip]];
1035 }
1036 }
1037
1038 if ([m_ToolChest[i].button isKindOfClass:[XAP_CocoaToolbarButton class]])
1039 {
1040 NSButtonCell * cell = (NSButtonCell *) [m_ToolChest[i].button cell];
1041
1042 [cell setShowsStateBy:NSNoCellMask];
1043 [cell setHighlightsBy:NSNoCellMask];
1044 }
1045 }
1046
1047 tooltip = pSS->getValue(AP_STRING_ID_TOOLBAR_TOOLTIP_FMT_STYLE);
1048 if (tooltip)
1049 {
1050 [oDocumentStyle setToolTip:[NSString stringWithUTF8String:tooltip]];
1051 }
1052 tooltip = pSS->getValue(AP_STRING_ID_TOOLBAR_TOOLTIP_FMT_FONT);
1053 if (tooltip)
1054 {
1055 [oFontName setToolTip:[NSString stringWithUTF8String:tooltip]];
1056 }
1057 tooltip = pSS->getValue(AP_STRING_ID_TOOLBAR_TOOLTIP_FMT_SIZE);
1058 if (tooltip)
1059 {
1060 [oFontSize setToolTip:[NSString stringWithUTF8String:tooltip]];
1061 }
1062 tooltip = pSS->getValue(AP_STRING_ID_TOOLBAR_TOOLTIP_ZOOM);
1063 if (tooltip)
1064 {
1065 [oZoom setToolTip:[NSString stringWithUTF8String:tooltip]];
1066 }
1067
1068 [self rebuildFontFamilyPopUp];
1069
1070 [[oFontName menu] setAutoenablesItems:NO];
1071 [[oFontMemberName menu] setAutoenablesItems:NO];
1072
1073 [[oDocumentStyle menu] setAutoenablesItems:NO];
1074
1075 m_Properties_DataSource = [[XAP_PaletteProperties_DataSource alloc] initWithOutlineView:oProperties];
1076 if (m_Properties_DataSource)
1077 {
1078 [oProperties setIndentationPerLevel:10.0f];
1079 [oProperties setDataSource:m_Properties_DataSource];
1080 [oProperties setDelegate:m_Properties_DataSource];
1081 }
1082
1083 [oPreviewPanel windowDidLoad];
1084
1085 [self setCurrentView:[pController currentView] inFrame:[pController currentFrame]];
1086 [self sync];
1087 }
1088
1089 - (void)close
1090 {
1091 UT_DEBUGMSG(("XAP_CocoaToolPalette -close\n"));
1092 s_instance = nil;
1093 [super close];
1094 }
1095
1096 - (void)windowWillClose:(NSNotification *)aNotification
1097 {
1098 UT_UNUSED(aNotification);
1099 XAP_Prefs *pPrefs = XAP_App::getApp()->getPrefs();
1100 UT_return_if_fail (pPrefs);
1101
1102 XAP_PrefsScheme *pPrefsScheme = pPrefs->getCurrentScheme();
1103 UT_return_if_fail (pPrefsScheme);
1104
1105 pPrefsScheme->setValueBool(XAP_PREF_KEY_ToolPaletteVisible, false);
1106
1107
1108 UT_DEBUGMSG(("XAP_CocoaToolPalette -windowWillClose\n"));
1109 }
1110
1111 - (void)toggleVisibility:(id)sender
1112 {
1113 UT_UNUSED(sender);
1114 if ([oPanel isVisible]) {
1115 [oPanel orderOut:self];
1116 }
1117 else {
1118 [oPanel orderFront:self];
1119 }
1120 }
1121
1122 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
1123 {
1124 [menuItem setState:([oPanel isVisible] ? NSOnState : NSOffState)];
1125
1126 return YES;
1127 }
1128
1129 + (void)setPreviewText:(id)previewText
1130 {
1131 if (s_instance) {
1132 if ([previewText isKindOfClass:[NSString class]]) {
1133 NSString * str = (NSString *) previewText;
1134 [s_instance setPreviewString:str];
1135 }
1136 if ([previewText isKindOfClass:[NSAttributedString class]]) {
1137 NSAttributedString * str = (NSAttributedString *) previewText;
1138 [s_instance setPreviewString:[str string]];
1139 }
1140 }
1141 }
1142
1143 - (void)setPreviewString:(NSString *)previewString
1144 {
1145 [oPreview setStringValue:previewString];
1146
1147 [oPreviewPanel setPreviewString:previewString];
1148 }
1149
1150 - (NSWindow *)previewPanel
1151 {
1152 return [oPreviewPanel window];
1153 }
1154
1155 - (void)setColor:(XAP_Toolbar_Id)tlbrid
1156 {
1157 if (!m_pViewCurrent) {
1158 return;
1159 }
1160
1161 const EV_Toolbar_Action * pAction = m_pToolbarActionSet->getAction(tlbrid);
1162 UT_ASSERT(pAction);
1163 if (!pAction) {
1164 return;
1165 }
1166
1167 const char * szMethodName = pAction->getMethodName();
1168 if (!szMethodName) {
1169 return;
1170 }
1171
1172 EV_EditMethod * pEM = m_pEditMethodContainer->findEditMethodByName(szMethodName);
1173 if (!pEM) {
1174 return;
1175 }
1176
1177 UT_DEBUGMSG(("XAP_CocoaToolPalette -setColor: have edit method\n"));
1178
1179 XAP_CGFloat red;
1180 XAP_CGFloat green;
1181 XAP_CGFloat blue;
1182 XAP_CGFloat alpha;
1183
1184 NSColor * color = [[NSColorPanel sharedColorPanel] color];
1185
1186 [color getRed:&red green:&green blue:&blue alpha:&alpha]; // TODO: is color necessarily RGBA? if not, could be a problem...
1187
1188 int r = static_cast<int>(lrintf(red * 255)); r = (r < 0) ? 0 : r; r = (r > 255) ? 255 : r;
1189 int g = static_cast<int>(lrintf(green * 255)); g = (g < 0) ? 0 : g; g = (g > 255) ? 255 : g;
1190 int b = static_cast<int>(lrintf(blue * 255)); b = (b < 0) ? 0 : b; b = (b > 255) ? 255 : b;
1191
1192 UT_HashColor hash;
1193
1194 const char * color_string = hash.setColor(static_cast<unsigned char>(r),
1195 static_cast<unsigned char>(g),
1196 static_cast<unsigned char>(b));
1197 if (color_string)
1198 {
1199 UT_UCS4String color_data(color_string);
1200
1201 const UT_UCS4Char * pData = color_data.ucs4_str();
1202 UT_uint32 dataLength = static_cast<UT_uint32>(color_data.length());
1203
1204 EV_EditMethodCallData emcd(pData,dataLength);
1205 pEM->Fn(m_pViewCurrent, &emcd);
1206 }
1207 }
1208
1209 - (IBAction)aColor_FG:(id)sender
1210 {
1211 UT_UNUSED(sender);
1212 [self setColor:AP_TOOLBAR_ID_COLOR_FORE];
1213 }
1214
1215 - (IBAction)aColor_BG:(id)sender
1216 {
1217 UT_UNUSED(sender);
1218 [self setColor:AP_TOOLBAR_ID_COLOR_BACK];
1219 }
1220
1221 - (IBAction)aSwitch_FG:(id)sender
1222 {
1223 UT_UNUSED(sender);
1224 // TODO
1225 }
1226
1227 - (IBAction)aSwitch_BG:(id)sender
1228 {
1229 UT_UNUSED(sender);
1230 // TODO
1231 }
1232
1233 - (IBAction)aTitle_click:(id)sender
1234 {
1235 NSButton * title = (NSButton *) sender;
1236
1237 if ([title tag] == 0) {
1238 [title setTag:1];
1239 }
1240 else {
1241 [title setTag:0];
1242 }
1243
1244 [m_PaletteView sync];
1245 [m_PaletteView setNeedsDisplay:YES];
1246 }
1247
1248 - (IBAction)aDocumentStyle:(id)sender
1249 {
1250 UT_UNUSED(sender);
1251 if (!m_pViewCurrent) {
1252 return;
1253 }
1254
1255 const EV_Toolbar_Action * pAction = m_pToolbarActionSet->getAction(AP_TOOLBAR_ID_FMT_STYLE);
1256 UT_ASSERT(pAction);
1257 if (!pAction) {
1258 return;
1259 }
1260
1261 const char * szMethodName = pAction->getMethodName();
1262 if (!szMethodName) {
1263 return;
1264 }
1265
1266 EV_EditMethod * pEM = m_pEditMethodContainer->findEditMethodByName(szMethodName);
1267 if (!pEM) {
1268 return;
1269 }
1270
1271 UT_DEBUGMSG(("XAP_CocoaToolPalette -aDocumentStyle: have edit method\n"));
1272
1273 UT_UCS4String selection([[oDocumentStyle titleOfSelectedItem] UTF8String]);
1274
1275 const UT_UCS4Char * pData = selection.ucs4_str();
1276 UT_uint32 dataLength = static_cast<UT_uint32>(selection.length());
1277
1278 EV_EditMethodCallData emcd(pData,dataLength);
1279 pEM->Fn(m_pViewCurrent, &emcd);
1280 }
1281
1282 - (IBAction)aFontName:(id)sender
1283 {
1284 UT_UNUSED(sender);
1285 if (!m_pViewCurrent) {
1286 return;
1287 }
1288
1289 const EV_Toolbar_Action * pAction = m_pToolbarActionSet->getAction(AP_TOOLBAR_ID_FMT_FONT);
1290 UT_ASSERT(pAction);
1291 if (!pAction) {
1292 return;
1293 }
1294
1295 const char * szMethodName = pAction->getMethodName();
1296 if (!szMethodName) {
1297 return;
1298 }
1299
1300 EV_EditMethod * pEM = m_pEditMethodContainer->findEditMethodByName(szMethodName);
1301 if (!pEM) {
1302 return;
1303 }
1304
1305 UT_DEBUGMSG(("XAP_CocoaToolPalette -aFontName: have edit method\n"));
1306
1307 UT_UCS4String selection([[oFontName titleOfSelectedItem] UTF8String]);
1308
1309 const UT_UCS4Char * pData = selection.ucs4_str();
1310 UT_uint32 dataLength = static_cast<UT_uint32>(selection.length());
1311
1312 EV_EditMethodCallData emcd(pData,dataLength);
1313 pEM->Fn(m_pViewCurrent, &emcd);
1314 }
1315
1316 - (IBAction)aFontMemberName:(id)sender
1317 {
1318 UT_UNUSED(sender);
1319 if (!m_pViewCurrent) {
1320 return;
1321 }
1322
1323 if (!m_pCurrentFontFamily)
1324 {
1325 return;
1326 }
1327
1328 // int index = [[oFontMemberName selectedItem] tag];
1329
1330 NSString * fontFamilyName = m_pCurrentFontFamily;
1331
1332 BOOL bBold = ([oTB_text_bold state] == NSOnState) ? YES : NO; // current state
1333 BOOL bItalic = ([oTB_text_italic state] == NSOnState) ? YES : NO;
1334
1335 BOOL bSetBold = NO;
1336 BOOL bSetItalic = NO;
1337
1338 #if 0
1339 if (index == [m_pCurrentFontFamilyHelper indexBoldItalic])
1340 {
1341 bSetBold = YES;
1342 bSetItalic = YES;
1343 }
1344 else if (index == [m_pCurrentFontFamilyHelper indexBold])
1345 {
1346 bSetBold = YES;
1347 }
1348 else if (index == [m_pCurrentFontFamilyHelper indexItalic])
1349 {
1350 bSetItalic = YES;
1351 }
1352 else if (index == [m_pCurrentFontFamilyHelper indexRegular])
1353 {
1354 //
1355 }
1356 else // need to set an explicit font name rather than a font family name...
1357 {
1358 NSArray * fontNames = [m_pCurrentFontFamilyHelper fontNames];
1359
1360 fontFamilyName = [fontNames objectAtIndex:index];
1361 }
1362 #endif
1363 /* fontFamilyName gets corrupted for some probably obvious reason, so copy it here and now...
1364 */
1365 UT_UCS4String selection([fontFamilyName UTF8String]);
1366
1367 if (bBold != bSetBold)
1368 {
1369 [oTB_text_bold setState:(bSetBold ? NSOnState : NSOffState)];
1370 [self aTB_click:oTB_text_bold];
1371 }
1372
1373 if (bItalic != bSetItalic)
1374 {
1375 [oTB_text_italic setState:(bSetItalic ? NSOnState : NSOffState)];
1376 [self aTB_click:oTB_text_italic];
1377 }
1378
1379 const EV_Toolbar_Action * pAction = m_pToolbarActionSet->getAction(AP_TOOLBAR_ID_FMT_FONT);
1380 UT_ASSERT(pAction);
1381 if (!pAction) {
1382 return;
1383 }
1384
1385 const char * szMethodName = pAction->getMethodName();
1386 if (!szMethodName) {
1387 return;
1388 }
1389
1390 EV_EditMethod * pEM = m_pEditMethodContainer->findEditMethodByName(szMethodName);
1391 if (!pEM) {
1392 return;
1393 }
1394
1395 UT_DEBUGMSG(("XAP_CocoaToolPalette -aFontMemberName: have edit method\n"));
1396
1397 const UT_UCS4Char * pData = selection.ucs4_str();
1398 UT_uint32 dataLength = static_cast<UT_uint32>(selection.length());
1399
1400 EV_EditMethodCallData emcd(pData,dataLength);
1401 pEM->Fn(m_pViewCurrent, &emcd);
1402 }
1403
1404 - (IBAction)aFontSize:(id)sender
1405 {
1406 UT_UNUSED(sender);
1407 if (!m_pViewPrevious)
1408 {
1409 if (m_pFramePrevious) {
1410 m_pFramePrevious->raise();
1411 }
1412 return;
1413 }
1414
1415 const EV_Toolbar_Action * pAction = m_pToolbarActionSet->getAction(AP_TOOLBAR_ID_FMT_SIZE);
1416 UT_ASSERT(pAction);
1417 if (!pAction)
1418 {
1419 if (m_pFramePrevious) {
1420 m_pFramePrevious->raise();
1421 }
1422 return;
1423 }
1424
1425 const char * szMethodName = pAction->getMethodName();
1426 if (!szMethodName)
1427 {
1428 if (m_pFramePrevious) {
1429 m_pFramePrevious->raise();
1430 }
1431 return;
1432 }
1433
1434 EV_EditMethod * pEM = m_pEditMethodContainer->findEditMethodByName(szMethodName);
1435 if (!pEM)
1436 {
1437 if (m_pFramePrevious) {
1438 m_pFramePrevious->raise();
1439 }
1440 return;
1441 }
1442
1443 UT_DEBUGMSG(("XAP_CocoaToolPalette -aFontSize: have edit method\n"));
1444
1445 int size = [oFontSize intValue];
1446
1447 if (size < 1) {
1448 size = 1; // TODO: ??
1449 }
1450 if (size > 100) {
1451 size = 100; // TODO: ??
1452 }
1453
1454 char buf[8];
1455 sprintf(buf, "%d", size);
1456
1457 UT_UCS4String selection(buf);
1458
1459 const UT_UCS4Char * pData = selection.ucs4_str();
1460 UT_uint32 dataLength = static_cast<UT_uint32>(selection.length());
1461
1462 EV_EditMethodCallData emcd(pData,dataLength);
1463 pEM->Fn(m_pViewPrevious, &emcd);
1464
1465 if (m_pFramePrevious) {
1466 m_pFramePrevious->raise();
1467 }
1468 }
1469
1470 - (IBAction)aTB_click:(id)sender
1471 {
1472 NSButton * button = (NSButton *) sender;
1473
1474 int tag = [button tag];
1475
1476 if ((tag < 0) || (tag >= XAP_COCOA_TOOL_ID__COUNT_))
1477 {
1478 UT_DEBUGMSG(("XAP_CocoaToolPalette -aTB_click: invalid tag\n"));
1479 return;
1480 }
1481 // button == m_ToolChest[tag].button;
1482
1483 // TODO: other special buttons
1484
1485 XAP_Toolbar_Id tlbrid = m_ToolChest[tag].tlbrid;
1486
1487 if (!m_pViewCurrent)
1488 {
1489 if ((tlbrid == AP_TOOLBAR_ID_FILE_NEW ) ||
1490 (tlbrid == AP_TOOLBAR_ID_FILE_OPEN) ||
1491 (tlbrid == AP_TOOLBAR_ID_HELP))
1492 {
1493 // Only these are okay
1494 }
1495 else
1496 {
1497 return;
1498 }
1499 }
1500
1501 /* Toolbar fore- & background color buttons don't actually set the color, they just ensure that the
1502 * color-chooser is visible & correctly configured:
1503 */
1504 if ((tlbrid == AP_TOOLBAR_ID_COLOR_FORE) || (tlbrid == AP_TOOLBAR_ID_COLOR_BACK))
1505 {
1506 NSColorPanel * colorPanel = [NSColorPanel sharedColorPanel];
1507
1508 // ?? [NSColorPanel setPickerMask:(NSColorPanelRGBModeMask|NSColorPanelWheelModeMask|NSColorPanelGrayModeMask)];
1509
1510 [colorPanel setAction:0];
1511 [colorPanel setTarget:0];
1512
1513 if (tlbrid == AP_TOOLBAR_ID_COLOR_FORE)
1514 {
1515 // [colorPanel setTitle:@"Foreground Color"]; // TODO: Localize
1516 [colorPanel setColor:[oColor_FG color]];
1517 [colorPanel setAction:@selector(aColor_FG:)];
1518 }
1519 else
1520 {
1521 // [colorPanel setTitle:@"Background Color"]; // TODO: Localize
1522 [colorPanel setColor:[oColor_BG color]];
1523 [colorPanel setAction:@selector(aColor_BG:)];
1524 }
1525 [colorPanel orderFront:self];
1526 [colorPanel setTarget:self];
1527
1528 return;
1529 }
1530
1531 if (tlbrid)
1532 {
1533 const EV_Toolbar_Action * pAction = m_pToolbarActionSet->getAction(tlbrid);
1534 UT_ASSERT(pAction);
1535 if (!pAction) {
1536 return;
1537 }
1538
1539 const char * szMethodName = pAction->getMethodName();
1540 if (!szMethodName) {
1541 return;
1542 }
1543
1544 EV_EditMethod * pEM = m_pEditMethodContainer->findEditMethodByName(szMethodName);
1545 if (!pEM) {
1546 return;
1547 }
1548
1549 UT_DEBUGMSG(("XAP_CocoaToolPalette -aTB_click: have edit method\n"));
1550
1551 EV_EditMethodType t = pEM->getType();
1552
1553 if (t & EV_EMT_REQUIREDATA) // This method requires character data
1554 {
1555 UT_DEBUGMSG(("XAP_CocoaToolPalette -aTB_click: method requires data\n"));
1556 }
1557 else
1558 {
1559 const UT_UCS4Char * pData = NULL;
1560 UT_uint32 dataLength = 0;
1561
1562 EV_EditMethodCallData emcd(pData,dataLength);
1563 pEM->Fn(m_pViewCurrent, &emcd);
1564 }
1565 }
1566 else
1567 {
1568 switch (tag) {
1569 case ToolID_oTB_LineAll:
1570 case ToolID_oTB_LineBottom:
1571 case ToolID_oTB_LineLeft:
1572 case ToolID_oTB_LineNone:
1573 case ToolID_oTB_LineRight:
1574 case ToolID_oTB_LineTop:
1575 // TODO: this is something else. use menu methods, perhaps
1576 break;
1577
1578 case ToolID_oTB_search:
1579 {
1580 bool okay = false;
1581
1582 const EV_Menu_Action * pAction = m_pMenuActionSet->getAction(AP_MENU_ID_EDIT_FIND);
1583 if (m_pViewCurrent && pAction) {
1584 if (const char * szMethodName = pAction->getMethodName())
1585 {
1586 EV_EditMethod * pEM = m_pEditMethodContainer->findEditMethodByName(szMethodName);
1587 if (pEM)
1588 {
1589 EV_EditMethodCallData data;
1590 okay = pEM->Fn(m_pViewCurrent, &data);
1591 }
1592 }
1593 }
1594 if (!okay)
1595 {
1596 UT_DEBUGMSG(("XAP_CocoaToolPalette -aTB_click: ToolID_oTB_search: failed!\n"));
1597 }
1598 break;
1599 }
1600 case ToolID_oTB_search_replace:
1601 {
1602 bool okay = false;
1603
1604 const EV_Menu_Action * pAction = m_pMenuActionSet->getAction(AP_MENU_ID_EDIT_REPLACE);
1605 if (m_pViewCurrent && pAction) {
1606 if (const char * szMethodName = pAction->getMethodName())
1607 {
1608 EV_EditMethod * pEM = m_pEditMethodContainer->findEditMethodByName(szMethodName);
1609 if (pEM)
1610 {
1611 EV_EditMethodCallData data;
1612 okay = pEM->Fn(m_pViewCurrent, &data);
1613 }
1614 }
1615 }
1616 if (!okay)
1617 {
1618 UT_DEBUGMSG(("XAP_CocoaToolPalette -aTB_click: ToolID_oTB_search_replace: failed!\n"));
1619 }
1620 break;
1621 }
1622
1623 case ToolID_oTB_SplitAbove:
1624 case ToolID_oTB_SplitBelow:
1625 case ToolID_oTB_SplitHoriMid:
1626 case ToolID_oTB_SplitLeft:
1627 case ToolID_oTB_SplitRight:
1628 case ToolID_oTB_SplitVertMid:
1629 // TODO: this is something else. use menu methods, perhaps
1630 break;
1631
1632 default:
1633 UT_DEBUGMSG (("XAP_CocoaToolPalette -aTB_click: Unimplemented toolbar button!\n"));
1634 break;
1635 }
1636 }
1637 }
1638
1639 - (IBAction)aZoom:(id)sender
1640 {
1641 UT_UNUSED(sender);
1642 if (!m_pViewPrevious)
1643 {
1644 if (m_pFramePrevious) {
1645 m_pFramePrevious->raise();
1646 }
1647 return;
1648 }
1649
1650 const EV_Toolbar_Action * pAction = m_pToolbarActionSet->getAction(AP_TOOLBAR_ID_ZOOM);
1651 UT_ASSERT(pAction);
1652 if (!pAction)
1653 {
1654 if (m_pFramePrevious) {
1655 m_pFramePrevious->raise();
1656 }
1657 return;
1658 }
1659
1660 const char * szMethodName = pAction->getMethodName();
1661 if (!szMethodName)
1662 {
1663 if (m_pFramePrevious) {
1664 m_pFramePrevious->raise();
1665 }
1666 return;
1667 }
1668
1669 EV_EditMethod * pEM = m_pEditMethodContainer->findEditMethodByName(szMethodName);
1670 if (!pEM)
1671 {
1672 if (m_pFramePrevious) {
1673 m_pFramePrevious->raise();
1674 }
1675 return;
1676 }
1677
1678 UT_DEBUGMSG(("XAP_CocoaToolPalette -aZoom: have edit method\n"));
1679
1680 NSString * value = [oZoom stringValue];
1681
1682 if ([value isEqualToString:@"Whole Page"] || [value isEqualToString:@"Page Width"])
1683 {
1684 UT_UCS4String selection([value UTF8String]);
1685
1686 const UT_UCS4Char * pData = selection.ucs4_str();
1687 UT_uint32 dataLength = static_cast<UT_uint32>(selection.length());
1688
1689 EV_EditMethodCallData emcd(pData,dataLength);
1690 pEM->Fn(m_pViewPrevious, &emcd);
1691 }
1692 else
1693 {
1694 int size = [oZoom intValue];
1695
1696 if (size < 1) {
1697 size = 1; // TODO: ??
1698 }
1699 if (size > 1000) {
1700 size = 1000; // TODO: ??
1701 }
1702
1703 char buf[8];
1704 sprintf(buf, "%d", size);
1705
1706 UT_UCS4String selection(buf);
1707
1708 const UT_UCS4Char * pData = selection.ucs4_str();
1709 UT_uint32 dataLength = static_cast<UT_uint32>(selection.length());
1710
1711 EV_EditMethodCallData emcd(pData,dataLength);
1712 pEM->Fn(m_pViewPrevious, &emcd);
1713 }
1714 if (m_pFramePrevious) {
1715 m_pFramePrevious->raise();
1716 }
1717 }
1718
1719 - (void)sync
1720 {
1721 /* toolbar buttons
1722 */
1723 for (UT_uint32 i = 0; i < XAP_COCOA_TOOL_ID__COUNT_; i++)
1724 {
1725 if (m_ToolChest[i].tlbrid)
1726 {
1727 if (!m_pViewCurrent)
1728 {
1729 if ((m_ToolChest[i].tlbrid == AP_TOOLBAR_ID_FILE_NEW ) ||
1730 (m_ToolChest[i].tlbrid == AP_TOOLBAR_ID_FILE_OPEN) ||
1731 (m_ToolChest[i].tlbrid == AP_TOOLBAR_ID_HELP))
1732 {
1733 [m_ToolChest[i].button setEnabled:YES];
1734 }
1735 else
1736 {
1737 [m_ToolChest[i].button setEnabled:NO];
1738 }
1739 continue;
1740 }
1741
1742 if ((m_ToolChest[i].tlbrid == AP_TOOLBAR_ID_MERGEABOVE) ||
1743 (m_ToolChest[i].tlbrid == AP_TOOLBAR_ID_MERGEBELOW) ||
1744 (m_ToolChest[i].tlbrid == AP_TOOLBAR_ID_MERGELEFT ) ||
1745 (m_ToolChest[i].tlbrid == AP_TOOLBAR_ID_MERGERIGHT))
1746 {
1747 /* WARNING: MERGELEFT/RIGHT/ABOVE/BELOW need to be implemented properly in toolbar actions!
1748 */
1749 [m_ToolChest[i].button setEnabled:NO];
1750 continue;
1751 }
1752
1753 const EV_Toolbar_Action * pAction = m_pToolbarActionSet->getAction(m_ToolChest[i].tlbrid);
1754 UT_ASSERT(pAction);
1755 if (!pAction) {
1756 continue;
1757 }
1758
1759 const char * szState = 0;
1760 EV_Toolbar_ItemState tis = pAction->getToolbarItemState(m_pViewCurrent, &szState);
1761
1762 switch (pAction->getItemType()) {
1763
1764 case EV_TBIT_PushButton:
1765 case EV_TBIT_ColorFore:
1766 case EV_TBIT_ColorBack:
1767 {
1768 [m_ToolChest[i].button setEnabled:(EV_TIS_ShouldBeGray(tis) ? NO : YES)];
1769 }
1770 break;
1771
1772 case EV_TBIT_ToggleButton:
1773 case EV_TBIT_GroupButton:
1774 {
1775 [m_ToolChest[i].button setEnabled:(EV_TIS_ShouldBeGray(tis) ? NO : YES)];
1776 [m_ToolChest[i].button setState:(EV_TIS_ShouldBeToggled(tis) ? NSOnState : NSOffState)];
1777 }
1778 break;
1779
1780 case EV_TBIT_StaticLabel:
1781 case EV_TBIT_Spacer:
1782 case EV_TBIT_BOGUS:
1783 case EV_TBIT_EditText:
1784 case EV_TBIT_DropDown:
1785 case EV_TBIT_ComboBox: // handled elsewhere
1786 break;
1787
1788 default:
1789 UT_DEBUGMSG (("XAP_CocoaToolPalette -sync: Unexpected toolbar layout flags!\n"));
1790 UT_ASSERT(0);
1791 break;
1792 }
1793 }
1794 else
1795 {
1796 switch (i) {// well, okay, a little insane to switch this, but...
1797 case ToolID_oTB_LineAll:
1798 case ToolID_oTB_LineBottom:
1799 case ToolID_oTB_LineLeft:
1800 case ToolID_oTB_LineNone:
1801 case ToolID_oTB_LineRight:
1802 case ToolID_oTB_LineTop:
1803 // TODO: this is something else. use menu methods, perhaps
1804 [m_ToolChest[i].button setEnabled:NO];
1805 break;
1806
1807 case ToolID_oTB_search:
1808 case ToolID_oTB_search_replace:
1809 [m_ToolChest[i].button setEnabled:(m_pViewCurrent ? YES : NO)];
1810 break;
1811
1812 case ToolID_oTB_SplitAbove:
1813 case ToolID_oTB_SplitBelow:
1814 case ToolID_oTB_SplitHoriMid:
1815 case ToolID_oTB_SplitLeft:
1816 case ToolID_oTB_SplitRight:
1817 case ToolID_oTB_SplitVertMid:
1818 // TODO: this is something else. use menu methods, perhaps
1819 [m_ToolChest[i].button setEnabled:NO];
1820 break;
1821
1822 default:
1823 UT_DEBUGMSG (("XAP_CocoaToolPalette -sync: Unimplemented toolbar button!\n"));
1824 [m_ToolChest[i].button setEnabled:NO];
1825 break;
1826 }
1827 }
1828 }
1829
1830 /* Document styles
1831 */
1832 [oDocumentStyle removeAllItems];
1833 [oDocumentStyle setEnabled:NO];
1834
1835 if (m_pFrameCurrent) {
1836 if (PD_Document * pDoc = static_cast<PD_Document *>(m_pFrameCurrent->getCurrentDoc())) {
1837 const char * szName = 0;
1838 const PD_Style * pStyle = 0;
1839
1840 NSMutableArray * styles = [NSMutableArray arrayWithCapacity:32];
1841
1842 for (UT_uint32 k = 0; (pDoc->enumStyles(k, &szName, &pStyle)); k++) {
1843 if (szName) {
1844 #if 1
1845 [styles addObject:[NSString stringWithUTF8String:szName]];
1846 #else
1847 // TODO: Make style names reflect properties such as: font, size, alignment ??
1848 NSString * name = [NSString stringWithUTF8String:szName];
1849
1850 NSDictionary * attr = TODO
1851
1852 [oDocumentStyle addItemWithTitle:[NSAttributedString initWithString:name attributes:attr]];
1853 #endif
1854 }
1855 }
1856 [styles sortUsingSelector:@selector(compare:)];
1857
1858 [oDocumentStyle addItemsWithTitles:styles];
1859
1860 const EV_Toolbar_Action * pAction = m_pToolbarActionSet->getAction(AP_TOOLBAR_ID_FMT_STYLE);
1861 UT_ASSERT(pAction);
1862 if (pAction) {
1863 const char * szState = 0;
1864 EV_Toolbar_ItemState tis = pAction->getToolbarItemState(m_pViewCurrent, &szState);
1865
1866 [oDocumentStyle setEnabled:(EV_TIS_ShouldBeGray(tis) ? NO : YES)];
1867
1868 UT_ASSERT(szState);
1869 if (szState) {
1870 [oDocumentStyle selectItemWithTitle:[NSString stringWithUTF8String:szState]];
1871 }
1872 }
1873 }
1874 }
1875 /* Font name
1876 */
1877 [oFontName setEnabled:NO];
1878
1879 if (m_pViewCurrent)
1880 {
1881 const EV_Toolbar_Action * pAction = m_pToolbarActionSet->getAction(AP_TOOLBAR_ID_FMT_FONT);
1882 UT_ASSERT(pAction);
1883 if (pAction)
1884 {
1885 const char * szState = 0;
1886 EV_Toolbar_ItemState tis = pAction->getToolbarItemState(m_pViewCurrent, &szState);
1887
1888 [oFontName setEnabled:(EV_TIS_ShouldBeGray(tis) ? NO : YES)];
1889
1890 // UT_ASSERT(szState); // This assert is triggered if the selection contains multiple fonts?
1891 if (szState)
1892 {
1893 [self syncPopUpsForFont:[NSString stringWithUTF8String:szState]];
1894 }
1895 }
1896 }
1897
1898 /* Font size
1899 */
1900 [oFontSize setEnabled:NO];
1901
1902 if (m_pViewCurrent)
1903 {
1904 const EV_Toolbar_Action * pAction = m_pToolbarActionSet->getAction(AP_TOOLBAR_ID_FMT_SIZE);
1905 UT_ASSERT(pAction);
1906 if (pAction)
1907 {
1908 const char * szState = 0;
1909 EV_Toolbar_ItemState tis = pAction->getToolbarItemState(m_pViewCurrent, &szState);
1910
1911 [oFontSize setEnabled:(EV_TIS_ShouldBeGray(tis) ? NO : YES)];
1912
1913 if (szState)
1914 {
1915 NSString * selection = [NSString stringWithUTF8String:szState];
1916
1917 NSInteger index = [oFontSize indexOfItemWithObjectValue:selection];
1918
1919 if (index == NSNotFound)
1920 {
1921 index = [oFontSize indexOfSelectedItem];
1922
1923 if (index >= 0)
1924 {
1925 [oFontSize deselectItemAtIndex:index];
1926 }
1927 }
1928 else
1929 {
1930 [oFontSize selectItemAtIndex:index];
1931 }
1932 [oFontSize setStringValue:selection];
1933 }
1934 else // mixed selection
1935 {
1936 [oFontSize setStringValue:@""];
1937 }
1938 }
1939 }
1940
1941 /* Zoom
1942 */
1943 [oZoom setEnabled:NO];
1944
1945 if (m_pViewCurrent)
1946 {
1947 const EV_Toolbar_Action * pAction = m_pToolbarActionSet->getAction(AP_TOOLBAR_ID_ZOOM);
1948 UT_ASSERT(pAction);
1949 if (pAction)
1950 {
1951 const char * szState = 0;
1952 EV_Toolbar_ItemState tis = pAction->getToolbarItemState(m_pViewCurrent, &szState);
1953
1954 [oZoom setEnabled:(EV_TIS_ShouldBeGray(tis) ? NO : YES)];
1955
1956 UT_ASSERT(szState);
1957 if (szState)
1958 {
1959 NSString * selection = [NSString stringWithUTF8String:szState];
1960
1961 NSInteger index = [oZoom indexOfItemWithObjectValue:selection];
1962
1963 if (index == NSNotFound)
1964 {
1965 index = [oZoom indexOfSelectedItem];
1966
1967 if (index >= 0)
1968 {
1969 [oZoom deselectItemAtIndex:index];
1970 }
1971 }
1972 else
1973 {
1974 [oZoom selectItemAtIndex:index];
1975 }
1976 [oZoom setStringValue:selection];
1977 }
1978 }
1979 }
1980
1981 /* Color buttons
1982 */
1983 [oSwitch_FG setEnabled:NO]; // TODO
1984 [oSwitch_BG setEnabled:NO]; // TODO
1985
1986 if (m_pViewCurrent)
1987 {
1988 FV_View * pFView = static_cast<FV_View *>(m_pViewCurrent);
1989
1990 bool bMixedSelection;
1991 bool bExplicitlyDefined;
1992
1993 UT_UTF8String szColorValue;
1994
1995 if (pFView->queryCharFormat("color", szColorValue, bExplicitlyDefined, bMixedSelection))
1996 {
1997 [oSwitch_FG setState:(bExplicitlyDefined ? NSOnState : NSOffState)];
1998
1999 UT_HashColor hash;
2000
2001 bool bValid = (hash.setColor(szColorValue.utf8_str()) != 0);
2002 if (!bValid) {
2003 bValid = (hash.setHashIfValid(szColorValue.utf8_str()) != 0);
2004 }
2005
2006 if (bValid)
2007 {
2008 UT_RGBColor rgb = hash.rgb();
2009
2010 float r = static_cast<float>(rgb.m_red) / 255;
2011 float g = static_cast<float>(rgb.m_grn) / 255;
2012 float b = static_cast<float>(rgb.m_blu) / 255;
2013 float a = 1;
2014
2015 [oColor_FG setColor:[NSColor colorWithDeviceRed:r green:g blue:b alpha:a]];
2016 }
2017 else
2018 {
2019 UT_DEBUGMSG(("XAP_CocoaToolPalette -sync: fg=\"%s\"?\n", szColorValue.utf8_str()));
2020 }
2021 }
2022 if (pFView->queryCharFormat("bgcolor", szColorValue, bExplicitlyDefined, bMixedSelection))
2023 {
2024 [oSwitch_BG setState:(bExplicitlyDefined ? NSOnState : NSOffState)];
2025
2026 if (strcmp (szColorValue.utf8_str(), "transparent") == 0)
2027 {
2028 float r = 1;
2029 float g = 1;
2030 float b = 1;
2031 float a = 0;
2032
2033 [oColor_BG setColor:[NSColor colorWithDeviceRed:r green:g blue:b alpha:a]];
2034 }
2035 else
2036 {
2037 UT_HashColor hash;
2038
2039 bool bValid = (hash.setColor(szColorValue.utf8_str()) != 0);
2040 if (!bValid) {
2041 bValid = (hash.setHashIfValid(szColorValue.utf8_str()) != 0);
2042 }
2043 if (bValid)
2044 {
2045 UT_RGBColor rgb = hash.rgb();
2046
2047 float r = static_cast<float>(rgb.m_red) / 255;
2048 float g = static_cast<float>(rgb.m_grn) / 255;
2049 float b = static_cast<float>(rgb.m_blu) / 255;
2050 float a = 1;
2051
2052 [oColor_BG setColor:[NSColor colorWithDeviceRed:r green:g blue:b alpha:a]];
2053 }
2054 else
2055 {
2056 UT_DEBUGMSG(("XAP_CocoaToolPalette -sync: bg=\"%s\"?\n", szColorValue.utf8_str()));
2057 }
2058 }
2059 }
2060 }
2061
2062 /* Properties OutlineView
2063 */
2064 [m_Properties_DataSource syncWithView:(static_cast<FV_View *>(m_pViewCurrent))];
2065 }
2066
2067 - (void)setCurrentView:(AV_View *)view inFrame:(XAP_Frame *)frame
2068 {
2069 if (!self) return;
2070
2071 /* Need to be careful. A frame/view can switch out because keyboard
2072 * focus has changed (e.g., because a combo-box in the panel has stolen
2073 * focus) or it may have been killed...
2074 */
2075 m_pViewPrevious = m_pViewCurrent;
2076 m_pFramePrevious = m_pFrameCurrent;
2077
2078 bool bSync = false;
2079
2080 if (frame && view)
2081 {
2082 m_pViewCurrent = view;
2083 m_pFrameCurrent = frame;
2084
2085 bSync = true;
2086 }
2087 else if (!frame && !view)
2088 {
2089 m_pViewCurrent = 0;
2090 m_pFrameCurrent = 0;
2091
2092 // should we sync in this case?
2093 }
2094 else
2095 {
2096 UT_ASSERT(view && frame);
2097
2098 m_pViewCurrent = 0;
2099 m_pFrameCurrent = 0;
2100
2101 bSync = true;
2102 }
2103
2104 m_Listener->setCurrentView(m_pViewCurrent);
2105
2106 if (bSync) {
2107 [self sync];
2108 }
2109 }
2110
2111 - (void)rebuildFontFamilyPopUp
2112 {
2113 // XAP_CocoaAppController * pController = (XAP_CocoaAppController *) [NSApp delegate];
2114
2115 [oFontName removeAllItems];
2116
2117 unsigned count = [m_pFontFamilies count];
2118
2119 BOOL bAttributedTitles = YES;
2120
2121 for (unsigned i = 0; i < count; i++)
2122 {
2123 NSString * Family = [m_pFontFamilies objectAtIndex:i];
2124
2125 [oFontName addItemWithTitle:Family];
2126
2127 NSMenuItem * item = [oFontName lastItem];
2128
2129 [item setTag:((int) i)];
2130
2131 if (i == 0)
2132 {
2133 bAttributedTitles = [item respondsToSelector:@selector(setAttributedTitle:)];
2134 }
2135 // if (bAttributedTitles)
2136 // {
2137 // XAP_CocoaFontFamilyHelper * helper = [pController helperForFontFamily:Family];
2138 //
2139 // [item setAttributedTitle:[helper attributedFontFamilyName]];
2140 // }
2141 }
2142 }
2143
2144 - (void)syncPopUpsForFont:(NSString *)requestedFontFamilyName
2145 {
2146 /* requestedFontFamilyName is one of:
2147 *
2148 * (a) a recognized font family name
2149 * (b) really a font name
2150 * (c) completely unknown, so we pretend it's a new font family name
2151 */
2152
2153 NSString * fontFamilyName = 0;
2154
2155 // XAP_CocoaAppController * pController = (XAP_CocoaAppController *) [NSApp delegate];
2156
2157 // XAP_CocoaFontReference * fontRef = 0;
2158
2159 // XAP_CocoaFontFamilyHelper * helper = [pController helperForFontFamily:requestedFontFamilyName];
2160
2161 // if (helper)
2162 // {
2163 // // (a) a recognized font family name
2164 //
2165 // fontFamilyName = requestedFontFamilyName;
2166 // }
2167 // else if ((fontRef = [pController helperReferenceForFont:requestedFontFamilyName]))
2168 // {
2169 // // (b) really a font name
2170 //
2171 // fontFamilyName = [fontRef fontFamily];
2172 //
2173 // helper = [fontRef fontFamilyHelper];
2174 // }
2175 // else
2176 // {
2177 // // (c) completely unknown, so we pretend it's a new font family name
2178 //
2179 fontFamilyName = requestedFontFamilyName;
2180 //
2181 // helper = [pController helperForUnknownFontFamily:requestedFontFamilyName];
2182 //
2183 /* In this case we need to modify the list of font families
2184 */
2185
2186 [m_pFontFamilies addObject:fontFamilyName];
2187 [m_pFontFamilies sortUsingSelector:@selector(compare:)];
2188
2189 [self rebuildFontFamilyPopUp];
2190 // }
2191
2192 BOOL bRebuildMemberPopUp = YES;
2193
2194 if (m_pCurrentFontFamily)
2195 {
2196 if ([m_pCurrentFontFamily isEqualToString:fontFamilyName])
2197 {
2198 bRebuildMemberPopUp = NO;
2199 }
2200 [m_pCurrentFontFamily release];
2201 }
2202 m_pCurrentFontFamily = fontFamilyName;
2203 [m_pCurrentFontFamily retain];
2204
2205 #if 0
2206 if (m_pCurrentFontFamilyHelper)
2207 {
2208 [m_pCurrentFontFamilyHelper release];
2209 }
2210 m_pCurrentFontFamilyHelper = helper;
2211 [m_pCurrentFontFamilyHelper retain];
2212
2213 [oFontName selectItemWithTitle:m_pCurrentFontFamily];
2214
2215 unsigned count = [helper count];
2216
2217 NSArray * fontMembers = [helper fontMembers];
2218
2219 if (bRebuildMemberPopUp)
2220 {
2221 [oFontMemberName removeAllItems];
2222
2223 NSArray * attributedFontMembers = [helper attributedFontMembers];
2224
2225 BOOL bAttributedTitles = YES;
2226
2227 for (unsigned i = 0; i < count; i++)
2228 {
2229 NSString * fontMember = [fontMembers objectAtIndex:i];
2230
2231 [oFontMemberName addItemWithTitle:fontMember];
2232
2233 NSMenuItem * item = [oFontMemberName lastItem];
2234
2235 [item setTag:((int) i)];
2236
2237 if (i == 0)
2238 {
2239 bAttributedTitles = [item respondsToSelector:@selector(setAttributedTitle:)];
2240 }
2241 if (bAttributedTitles)
2242 {
2243 NSAttributedString * attr_title = [attributedFontMembers objectAtIndex:i];
2244
2245 [item setAttributedTitle:attr_title];
2246 }
2247 }
2248 [oFontMemberName setEnabled:((count > 1) ? YES : NO)];
2249 }
2250 if (count == 1)
2251 {
2252 return;
2253 }
2254 if (fontRef)
2255 {
2256 [oFontMemberName selectItemAtIndex:((int) [fontRef index])];
2257 return;
2258 }
2259 /* We have a font family name but we need to choose a particular member of the font...
2260 */
2261 BOOL bBold = ([oTB_text_bold state] == NSOnState) ? YES : NO;
2262 BOOL bItalic = ([oTB_text_italic state] == NSOnState) ? YES : NO;
2263
2264 if (bBold && bItalic)
2265 {
2266 int index = [helper indexBoldItalic];
2267 if (index >= 0)
2268 {
2269 [oFontMemberName selectItemAtIndex:index];
2270 return;
2271 }
2272 }
2273 else if (bBold)
2274 {
2275 int index = [helper indexBold];
2276 if (index >= 0)
2277 {
2278 [oFontMemberName selectItemAtIndex:index];
2279 return;
2280 }
2281 }
2282 else if (bItalic)
2283 {
2284 int index = [helper indexItalic];
2285 if (index >= 0)
2286 {
2287 [oFontMemberName selectItemAtIndex:index];
2288 return;
2289 }
2290 }
2291 else
2292 {
2293 int index = [helper indexRegular];
2294
2295 [oFontMemberName selectItemAtIndex:((index >= 0) ? index : 0)];
2296 }
2297 #endif
2298 }
2299
2300 @end
2301
2302 XAP_CocoaToolPaletteListener::XAP_CocoaToolPaletteListener(XAP_CocoaToolPalette * pPalette) :
2303 m_pPalette(pPalette),
2304 m_pView(0)
2305 {
2306 //
2307 }
2308
2309 XAP_CocoaToolPaletteListener::~XAP_CocoaToolPaletteListener()
2310 {
2311 setCurrentView(0);
2312 }
2313
2314 void XAP_CocoaToolPaletteListener::setCurrentView(AV_View * view)
2315 {
2316 if (m_pView)
2317 {
2318 if (!m_pView->removeListener(m_lid))
2319 {
2320 UT_DEBUGMSG(("XAP_CocoaToolPaletteListener::setCurrentView: failed to remove listener!\n"));
2321 }
2322 m_pView = 0;
2323 }
2324 if (view)
2325 {
2326 if (view->addListener(this, &m_lid))
2327 {
2328 m_pView = view;
2329 }
2330 else
2331 {
2332 UT_DEBUGMSG(("XAP_CocoaToolPaletteListener::setCurrentView: failed to add listener!\n"));
2333 }
2334 }
2335 }
2336
2337 bool XAP_CocoaToolPaletteListener::notify(AV_View * pView, const AV_ChangeMask /*mask*/)
2338 {
2339 if (pView == m_pView)
2340 {
2341 [m_pPalette sync];
2342 }
2343 return true; // why is this a bool function?
2344 }
2345
2346 AV_ListenerType XAP_CocoaToolPaletteListener::getType()
2347 {
2348 return AV_LISTENER_TOOLBAR;
2349 }