Debian Codegraph @

src/af/xap/cocoa/xap_CocoaDialog_Utilities.cpp

raw
1 /* AbiWord
2 * Copyright (C) 2002-2005 Hubert Figuiere
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA.
18 */
19
20 #import <Cocoa/Cocoa.h>
21
22 #include "xap_CocoaDialog_Utilities.h"
23
24 void _SetNSControlLabel (id control, const char *label)
25 {
26 char buf [1024];
27 NSString* str;
28
29 _convertLabelToMac(buf, sizeof (buf), label);
30
31 str = [[NSString alloc] initWithUTF8String:buf];
32
33 if ([control isKindOfClass:[NSButton class]]
34 || [control isKindOfClass:[NSBox class]]
35 || [control isKindOfClass:[NSCell class]]) {
36 [control setTitle:str];
37 }
38 else if ([control isKindOfClass:[NSTabViewItem class]]) {
39 [control setLabel:str];
40 }
41 else if ([control isKindOfClass:[NSTextField class]]) {
42 [control setStringValue:str];
43 }
44 else if ([control isKindOfClass:[NSWindow class]]) {
45 [control setTitle:str];
46 }
47 else {
48 NSLog(@"Unknown control type to localize: %@", [control class]);
49 }
50 [str release];
51 }
52
53
54 /** set the label of a NSControl
55 \param control the control AppKit object
56 \param label UT_UTF8String to set. It is a well formed XP label
57 that will be converted.
58 \note if the control object you pass is of an unknown type, the function will
59 NSLog something on the console. Feel g_free to update the function if you want to
60 handle that kind of object.
61 */
62 void SetNSControlLabel (id control, const UT_UTF8String &label)
63 {
64 _SetNSControlLabel(control, label.utf8_str());
65 }
66
67 void SetNSControlLabel (id control, const std::string &label)
68 {
69 _SetNSControlLabel(control, label.c_str());
70 }
71
72 /*!
73 Localize a control with a string from a string set
74
75 \param control the control AppKit object
76 \param pSS the string set
77 \param stringID the string ID from the string set
78 \seealso SetNSControlLabel
79 */
80 void LocalizeControl (id control, const XAP_StringSet * pSS, XAP_String_Id stringId)
81 {
82 std::string label;
83 pSS->getValueUTF8(stringId, label);
84 SetNSControlLabel(control, label);
85 }
86
87 /*!
88 Fetch a string from the string set and return a NSString
89
90 \param pSS the string set
91 \param stringId the string id
92 \return a NSString
93 */
94 NSString* LocalizedString (const XAP_StringSet * pSS, XAP_String_Id stringId)
95 {
96 std::string label;
97 pSS->getValueUTF8(stringId, label);
98 return [NSString stringWithUTF8String:label.c_str()];
99 }
100
101 /*!
102 Append a menu item to the popup button
103
104 \param menu the popup button
105 \param pSS the string set
106 \param stringId the string id
107 */
108 void AppendLocalizedMenuItem (NSPopUpButton * menu, const XAP_StringSet * pSS, XAP_String_Id stringId, int tag)
109 {
110 NSString * str = LocalizedString(pSS, stringId);
111
112 [menu addItemWithTitle:str];
113
114 [[menu lastItem] setTag:tag];
115 }
116
117 /*!
118 Strip the '&' from the label
119
120 \param buf the result buffer
121 \param bufSize the allocated size for buf
122 \param label the label to convert as an UT_String
123 */
124 void _convertLabelToMac (char * buf, size_t bufSize, const char * label)
125 {
126 UT_ASSERT(buf);
127 UT_ASSERT(strlen(label) < bufSize);
128
129 strncpy (buf, label, bufSize);
130
131 char * src, *dst;
132 src = dst = buf;
133 while (*src)
134 {
135 *dst = *src;
136 src++;
137 if (*dst != '&') {
138 dst++;
139 }
140 }
141 *dst = 0;
142 }