gwenhywfar 4.0.3

w_lineedit.mm

Go to the documentation of this file.
00001 /***************************************************************************
00002  begin       : August 10 2010
00003  copyright   : (C) 2010 by Samuel Strupp
00004  
00005  ***************************************************************************
00006  *          Please see toplevel file COPYING for license details           *
00007  ***************************************************************************/
00008 
00009 
00010 #import "CocoaLineTextField.h"
00011 
00012 
00013 static GWENHYWFAR_CB
00014 int CocoaGui_WLineEdit_SetIntProperty(GWEN_WIDGET *w,
00015                                                                           GWEN_DIALOG_PROPERTY prop,
00016                                                                           int index,
00017                                                                           int value,
00018                                                                           int doSignal) {
00019         CocoaLineTextField *textField;
00020         
00021         textField=(CocoaLineTextField*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
00022         assert(textField);
00023         
00024         switch(prop) {
00025                 case GWEN_DialogProperty_Enabled:
00026                         [textField setEnabled:(value==0)?NO:YES];
00027                         return 0;
00028                         
00029                 case GWEN_DialogProperty_Focus:
00030                         if ([textField window]) {
00031                                 [[textField window] makeFirstResponder:textField];
00032                         }
00033                         return 0;
00034                         
00035                 case GWEN_DialogProperty_Width: {
00036                         NSRect frame = [textField frame];
00037                         frame.size.width = value;
00038                         [textField setFrame:frame];
00039                 }
00040                         return 0;
00041                         
00042                 case GWEN_DialogProperty_Height:{
00043                         NSRect frame = [textField frame];
00044                         frame.size.height = value;
00045                         [textField setFrame:frame];
00046                 }
00047                         return 0;
00048                         
00049                 default:
00050                         break;
00051         }
00052         
00053         DBG_WARN(GWEN_LOGDOMAIN,
00054                          "Function is not appropriate for this type of widget (%s)",
00055                          GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
00056         return GWEN_ERROR_INVALID;
00057 }
00058 
00059 
00060 
00061 
00062 static GWENHYWFAR_CB
00063 int CocoaGui_WLineEdit_GetIntProperty(GWEN_WIDGET *w,
00064                                                                           GWEN_DIALOG_PROPERTY prop,
00065                                                                           int index,
00066                                                                           int defaultValue) {
00067         CocoaLineTextField *textField;
00068         
00069         textField=(CocoaLineTextField*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
00070         assert(textField);
00071         
00072         switch(prop) {
00073                 case GWEN_DialogProperty_Enabled:
00074                         return ([textField isEnabled])?1:0;
00075 
00076                         
00077                 case GWEN_DialogProperty_Focus:
00078                         if ([textField window]) {
00079                                 if ([[textField window] firstResponder] == textField) return 1;
00080                         }
00081                         return 0;
00082                         
00083                 case GWEN_DialogProperty_Width:
00084                         return [textField frame].size.width;
00085                         
00086                 case GWEN_DialogProperty_Height:
00087                         return [textField frame].size.height;
00088                         
00089                 default:
00090                         break;
00091         }
00092         
00093         DBG_WARN(GWEN_LOGDOMAIN,
00094                          "Function is not appropriate for this type of widget (%s)",
00095                          GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
00096         return defaultValue;
00097 }
00098 
00099 
00100 
00101 static GWENHYWFAR_CB
00102 int CocoaGui_WLineEdit_SetCharProperty(GWEN_WIDGET *w,
00103                                                                            GWEN_DIALOG_PROPERTY prop,
00104                                                                            int index,
00105                                                                            const char *value,
00106                                                                            int doSignal) {
00107         CocoaLineTextField *textField;
00108         
00109         textField=(CocoaLineTextField*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
00110         assert(textField);
00111         
00112         switch(prop) {
00113                 case GWEN_DialogProperty_Value:{
00114                         NSString *stringValue = [[NSString alloc] initWithCString:value encoding:NSUTF8StringEncoding];
00115                         [textField setStringValue:stringValue];
00116                         [stringValue release];
00117                 }
00118                 default:
00119                         break;
00120         }
00121         
00122         DBG_WARN(GWEN_LOGDOMAIN,
00123                          "Function is not appropriate for this type of widget (%s)",
00124                          GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
00125         return GWEN_ERROR_INVALID;
00126 }
00127 
00128 
00129 
00130 static GWENHYWFAR_CB
00131 const char* CocoaGui_WLineEdit_GetCharProperty(GWEN_WIDGET *w,
00132                                                                                            GWEN_DIALOG_PROPERTY prop,
00133                                                                                            int index,
00134                                                                                            const char *defaultValue) {
00135         CocoaLineTextField *textField;
00136         
00137         textField=(CocoaLineTextField*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
00138         assert(textField);
00139         
00140         switch(prop) {
00141                 case GWEN_DialogProperty_Title:
00142                         return [[textField stringValue] cStringUsingEncoding:NSUTF8StringEncoding];
00143                 default:
00144                         break;
00145         }
00146         
00147         DBG_WARN(GWEN_LOGDOMAIN,
00148                          "Function is not appropriate for this type of widget (%s)",
00149                          GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
00150         return defaultValue;
00151 }
00152 
00153 
00154 
00155 static void CocoaGui_WLineEdit_End_Editing_text_handler(NSTextField *textField, void* data) {
00156         GWEN_WIDGET *w;
00157         int rv;
00158         
00159         //NSLog(@"CocoaGui_WLineEdit_End_Editing_text_handler");
00160         
00161         w=data;
00162         assert(w);
00163         rv=GWEN_Dialog_EmitSignal(GWEN_Widget_GetDialog(w),
00164                                                           GWEN_DialogEvent_TypeActivated,
00165                                                           GWEN_Widget_GetName(w));
00166         if (rv==GWEN_DialogEvent_ResultAccept)
00167                 CocoaGui_Dialog_Leave(GWEN_Widget_GetTopDialog(w), 1);
00168         else if (rv==GWEN_DialogEvent_ResultReject)
00169                 CocoaGui_Dialog_Leave(GWEN_Widget_GetTopDialog(w), 0);
00170 }
00171 
00172 
00173 
00174 static void CocoaGui_WLineEdit_Changed_text_handler(NSTextField *textField, void* data) {
00175         GWEN_WIDGET *w;
00176         int rv;
00177         
00178         //NSLog(@"CocoaGui_WLineEdit_Changed_text_handler");
00179         
00180         w=data;
00181         assert(w);
00182         rv=GWEN_Dialog_EmitSignal(GWEN_Widget_GetDialog(w),
00183                                                           GWEN_DialogEvent_TypeValueChanged,
00184                                                           GWEN_Widget_GetName(w));
00185         if (rv==GWEN_DialogEvent_ResultAccept)
00186                 CocoaGui_Dialog_Leave(GWEN_Widget_GetTopDialog(w), 1);
00187         else if (rv==GWEN_DialogEvent_ResultReject)
00188                 CocoaGui_Dialog_Leave(GWEN_Widget_GetTopDialog(w), 0);
00189 }
00190 
00191 
00192 
00193 int CocoaGui_WLineEdit_Setup(GWEN_WIDGET *w) {
00194         CocoaLineTextField *textField;
00195         const char *s;
00196         uint32_t flags;
00197         GWEN_WIDGET *wParent;
00198         
00199         //gulong deleted_text_handler_id;
00200         //gulong inserted_text_handler_id;
00201         
00202         flags=GWEN_Widget_GetFlags(w);
00203         wParent=GWEN_Widget_Tree_GetParent(w);
00204         s=GWEN_Widget_GetText(w, 0);
00205         
00206         /* create widget */
00207         textField = [[[CocoaLineTextField alloc] initWithFrame:NSMakeRect(10.0, 10.0, 100.0, 22.0)] autorelease];
00208         if (flags & GWEN_WIDGET_FLAGS_FILLX) textField.fillX = YES;
00209         if (flags & GWEN_WIDGET_FLAGS_FILLY) textField.fillY = YES;
00210         if (flags & GWEN_WIDGET_FLAGS_PASSWORD) {
00211                 [textField setCell:[[[NSSecureTextFieldCell alloc] init] autorelease]];
00212                 [textField setDrawsBackground:YES];
00213                 [textField setBezeled:YES];
00214         }
00215         //[labelTextField setDrawsBackground:NO];
00216         [[textField cell] setLineBreakMode:NSLineBreakByClipping];
00217         //[[textField cell] setWraps:NO];
00218         //[textField setBordered:YES];
00219         [textField setEditable:YES];
00220         
00221         
00222         
00223         if (s && *s) {
00224                 NSString *stringValue = [[NSString alloc] initWithCString:s encoding:NSUTF8StringEncoding];
00225                 [textField setStringValue:stringValue];
00226                 [stringValue release];
00227         }
00228         
00229         GWEN_Widget_SetImplData(w, COCOA_DIALOG_WIDGET_REAL, (void*) textField);
00230         GWEN_Widget_SetImplData(w, COCOA_DIALOG_WIDGET_CONTENT, (void*) textField);
00231         
00232         GWEN_Widget_SetSetIntPropertyFn(w, CocoaGui_WLineEdit_SetIntProperty);
00233         GWEN_Widget_SetGetIntPropertyFn(w, CocoaGui_WLineEdit_GetIntProperty);
00234         GWEN_Widget_SetSetCharPropertyFn(w, CocoaGui_WLineEdit_SetCharProperty);
00235         GWEN_Widget_SetGetCharPropertyFn(w, CocoaGui_WLineEdit_GetCharProperty);
00236         
00237         gwenTextFieldActionPtr ptr = CocoaGui_WLineEdit_End_Editing_text_handler;
00238         [textField setC_ActionPtr:ptr Data:w];
00239         
00240         gwenTextFieldActionPtr changed_ptr = CocoaGui_WLineEdit_Changed_text_handler;
00241         [textField setC_TextChanged_ActionPtr:changed_ptr Data:w];
00242 
00243         
00244         if (wParent)
00245                 GWEN_Widget_AddChildGuiWidget(wParent, w);
00246         
00247         return 0;
00248 }
00249 
00250