1/***************************************************************************
2 begin       : August 10 2010
3 copyright   : (C) 2010 by Samuel Strupp
4
5 ***************************************************************************
6 *          Please see toplevel file COPYING for license details           *
7 ***************************************************************************/
8
9
10#import "CocoaLineTextField.h"
11
12
13static GWENHYWFAR_CB
14int CocoaGui_WTextEdit_SetIntProperty(GWEN_WIDGET *w,
15									 GWEN_DIALOG_PROPERTY prop,
16									 int index,
17									 int value,
18									 int doSignal) {
19	CocoaLineTextField *textField;
20
21	textField=(CocoaLineTextField*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
22	assert(textField);
23
24	switch(prop) {
25		case GWEN_DialogProperty_Enabled:
26			[textField setEnabled:(value==0)?NO:YES];
27			return 0;
28
29		case GWEN_DialogProperty_Focus:
30			if ([textField window]) {
31				[[textField window] makeFirstResponder:textField];
32			}
33			return 0;
34
35		case GWEN_DialogProperty_Width: {
36			NSRect frame = [textField frame];
37			frame.size.width = value;
38			[textField setFrame:frame];
39		}
40			return 0;
41
42		case GWEN_DialogProperty_Height:{
43			NSRect frame = [textField frame];
44			frame.size.height = value;
45			[textField setFrame:frame];
46		}
47			return 0;
48
49		default:
50			break;
51	}
52
53
54	DBG_WARN(GWEN_LOGDOMAIN,
55			 "Function is not appropriate for this type of widget (%s)",
56			 GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
57	return GWEN_ERROR_INVALID;
58}
59
60
61
62
63static GWENHYWFAR_CB
64int CocoaGui_WTextEdit_GetIntProperty(GWEN_WIDGET *w,
65									 GWEN_DIALOG_PROPERTY prop,
66									 int index,
67									 int defaultValue) {
68	CocoaLineTextField *textField;
69
70	textField=(CocoaLineTextField*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
71	assert(textField);
72
73	switch(prop) {
74		case GWEN_DialogProperty_Enabled:
75			return ([textField isEnabled])?1:0;
76
77
78		case GWEN_DialogProperty_Focus:
79			if ([textField window]) {
80				if ([[textField window] firstResponder] == textField) return 1;
81			}
82			return 0;
83
84		case GWEN_DialogProperty_Width:
85			return [textField frame].size.width;
86
87		case GWEN_DialogProperty_Height:
88			return [textField frame].size.height;
89
90		default:
91			break;
92	}
93
94	DBG_WARN(GWEN_LOGDOMAIN,
95			 "Function is not appropriate for this type of widget (%s)",
96			 GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
97	return defaultValue;
98}
99
100
101
102static GWENHYWFAR_CB
103int CocoaGui_WTextEdit_SetCharProperty(GWEN_WIDGET *w,
104									  GWEN_DIALOG_PROPERTY prop,
105									  int index,
106									  const char *value,
107									  int doSignal) {
108	CocoaLineTextField *textField;
109
110	textField=(CocoaLineTextField*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
111	assert(textField);
112
113	switch(prop) {
114		case GWEN_DialogProperty_Value:{
115			NSString *stringValue = [[NSString alloc] initWithCString:value encoding:NSUTF8StringEncoding];
116			[textField setStringValue:stringValue];
117			[stringValue release];
118		}
119		default:
120			break;
121	}
122
123	DBG_WARN(GWEN_LOGDOMAIN,
124			 "Function is not appropriate for this type of widget (%s)",
125			 GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
126	return GWEN_ERROR_INVALID;
127}
128
129
130
131static GWENHYWFAR_CB
132const char* CocoaGui_WTextEdit_GetCharProperty(GWEN_WIDGET *w,
133											  GWEN_DIALOG_PROPERTY prop,
134											  int index,
135											  const char *defaultValue) {
136	CocoaLineTextField *textField;
137
138	textField=(CocoaLineTextField*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
139	assert(textField);
140
141	switch(prop) {
142		case GWEN_DialogProperty_Title:
143			return [[textField stringValue] cStringUsingEncoding:NSUTF8StringEncoding];
144		default:
145			break;
146	}
147
148	DBG_WARN(GWEN_LOGDOMAIN,
149			 "Function is not appropriate for this type of widget (%s)",
150			 GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
151	return defaultValue;
152}
153
154
155
156static void CocoaGui_WTextEdit_End_Editing_text_handler(NSTextField *textField, void* data) {
157	GWEN_WIDGET *w;
158	int rv;
159
160	w=data;
161	assert(w);
162	rv=GWEN_Dialog_EmitSignal(GWEN_Widget_GetDialog(w),
163							  GWEN_DialogEvent_TypeActivated,
164							  GWEN_Widget_GetName(w));
165	if (rv==GWEN_DialogEvent_ResultAccept)
166		CocoaGui_Dialog_Leave(GWEN_Widget_GetTopDialog(w), 1);
167	else if (rv==GWEN_DialogEvent_ResultReject)
168		CocoaGui_Dialog_Leave(GWEN_Widget_GetTopDialog(w), 0);
169}
170
171
172
173static void CocoaGui_WTextEdit_Changed_text_handler(NSTextField *textField, void* data) {
174	GWEN_WIDGET *w;
175	int rv;
176
177	w=data;
178	assert(w);
179	rv=GWEN_Dialog_EmitSignal(GWEN_Widget_GetDialog(w),
180							  GWEN_DialogEvent_TypeValueChanged,
181							  GWEN_Widget_GetName(w));
182	if (rv==GWEN_DialogEvent_ResultAccept)
183		CocoaGui_Dialog_Leave(GWEN_Widget_GetTopDialog(w), 1);
184	else if (rv==GWEN_DialogEvent_ResultReject)
185		CocoaGui_Dialog_Leave(GWEN_Widget_GetTopDialog(w), 0);
186}
187
188
189
190int CocoaGui_WTextEdit_Setup(GWEN_WIDGET *w) {
191	CocoaLineTextField *textField;
192	const char *s;
193	uint32_t flags;
194	GWEN_WIDGET *wParent;
195
196	//gulong changed_handler_id;
197
198	flags=GWEN_Widget_GetFlags(w);
199	wParent=GWEN_Widget_Tree_GetParent(w);
200	s=GWEN_Widget_GetText(w, 0);
201
202	/* create widget */
203	textField = [[[CocoaLineTextField alloc] initWithFrame:NSMakeRect(10.0, 10.0, 100.0, 22.0)] autorelease];
204	if (flags & GWEN_WIDGET_FLAGS_FILLX) textField.fillX = YES;
205	if (flags & GWEN_WIDGET_FLAGS_FILLY) textField.fillY = YES;
206	//[textField setBordered:YES];
207	[textField setEditable:YES];
208
209	if (s && *s) {
210		NSString *stringValue = [[NSString alloc] initWithCString:s encoding:NSUTF8StringEncoding];
211		[textField setStringValue:stringValue];
212		[stringValue release];
213	}
214
215	GWEN_Widget_SetImplData(w, COCOA_DIALOG_WIDGET_REAL, (void*) textField);
216	GWEN_Widget_SetImplData(w, COCOA_DIALOG_WIDGET_CONTENT, (void*) textField);
217
218	GWEN_Widget_SetSetIntPropertyFn(w, CocoaGui_WTextEdit_SetIntProperty);
219	GWEN_Widget_SetGetIntPropertyFn(w, CocoaGui_WTextEdit_GetIntProperty);
220	GWEN_Widget_SetSetCharPropertyFn(w, CocoaGui_WTextEdit_SetCharProperty);
221	GWEN_Widget_SetGetCharPropertyFn(w, CocoaGui_WTextEdit_GetCharProperty);
222
223
224	gwenTextFieldActionPtr ptr = CocoaGui_WTextEdit_End_Editing_text_handler;
225	[textField setC_ActionPtr:ptr Data:w];
226
227	gwenTextFieldActionPtr changed_ptr = CocoaGui_WTextEdit_Changed_text_handler;
228	[textField setC_TextChanged_ActionPtr:changed_ptr Data:w];
229
230	if (wParent)
231		GWEN_Widget_AddChildGuiWidget(wParent, w);
232
233	return 0;
234}
235
236
237