gwenhywfar  4.3.1
w_checkbox.mm
Go to the documentation of this file.
00001 /***************************************************************************
00002  begin       : Aug 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 "CocoaCheckboxButton.h"
00011 
00012 
00013 static GWENHYWFAR_CB
00014 int CocoaGui_WCheckBox_SetIntProperty(GWEN_WIDGET *w,
00015                                                                           GWEN_DIALOG_PROPERTY prop,
00016                                                                           int index,
00017                                                                           int value,
00018                                                                           int doSignal) {
00019         CocoaCheckboxButton *checkbox;
00020         
00021         checkbox = (CocoaCheckboxButton*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
00022         assert(checkbox);
00023         
00024         switch(prop) {
00025                 case GWEN_DialogProperty_Enabled:
00026                         [checkbox setEnabled:(value==0)?NO:YES];
00027                         return 0;
00028                         
00029                 case GWEN_DialogProperty_Focus:
00030                         if ([checkbox window]) {
00031                                 [[checkbox window] makeFirstResponder:checkbox];
00032                         }
00033                         return 0;
00034                         
00035                 case GWEN_DialogProperty_Width: {
00036                         NSRect frame = [checkbox frame];
00037                         frame.size.width = value;
00038                         [checkbox setFrame:frame];
00039                 }
00040                         return 0;
00041                         
00042                 case GWEN_DialogProperty_Height: {
00043                         NSRect frame = [checkbox frame];
00044                         frame.size.height = value;
00045                         [checkbox setFrame:frame];
00046                 }
00047                         return 0;
00048                         
00049                 case GWEN_DialogProperty_Value:{
00050                         if (value==0) [checkbox setState:NSOffState];
00051                         else [checkbox setState:NSOnState];
00052                 }
00053                         return 0;
00054                         
00055                 default:
00056                         break;
00057         }
00058         
00059         DBG_WARN(GWEN_LOGDOMAIN,
00060                          "Function is not appropriate for this type of widget (%s)",
00061                          GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
00062         return GWEN_ERROR_INVALID;
00063 }
00064 
00065 
00066 
00067 
00068 static GWENHYWFAR_CB
00069 int CocoaGui_WCheckBox_GetIntProperty(GWEN_WIDGET *w,
00070                                                                           GWEN_DIALOG_PROPERTY prop,
00071                                                                           int index,
00072                                                                           int defaultValue) {
00073         CocoaCheckboxButton *checkbox;
00074         
00075         checkbox = (CocoaCheckboxButton*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
00076         assert(checkbox);
00077         
00078         switch(prop) {
00079                 case GWEN_DialogProperty_Enabled:
00080                         return ([checkbox isEnabled]==YES)?1:0;
00081                         
00082                 case GWEN_DialogProperty_Focus:
00083                         if ([checkbox window]) {
00084                                 if ([[checkbox window] firstResponder] == checkbox) return 1;
00085                         }
00086                         return 0;
00087                         
00088                 case GWEN_DialogProperty_Width:
00089                         return [checkbox frame].size.width;
00090                         
00091                 case GWEN_DialogProperty_Height:
00092                         return [checkbox frame].size.height;
00093                         
00094                 case GWEN_DialogProperty_Value:
00095                         return ([checkbox state]==NSOnState)?1:0;
00096                         
00097                 default:
00098                         break;
00099         }
00100         
00101         DBG_WARN(GWEN_LOGDOMAIN,
00102                          "Function is not appropriate for this type of widget (%s)",
00103                          GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
00104         return defaultValue;
00105 }
00106 
00107 
00108 
00109 static GWENHYWFAR_CB
00110 int CocoaGui_WCheckBox_SetCharProperty(GWEN_WIDGET *w,
00111                                                                            GWEN_DIALOG_PROPERTY prop,
00112                                                                            int index,
00113                                                                            const char *value,
00114                                                                            int doSignal) {
00115         CocoaCheckboxButton *checkbox;
00116         
00117         checkbox = (CocoaCheckboxButton*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
00118         assert(checkbox);
00119         
00120         switch(prop) {
00121                 case GWEN_DialogProperty_Title: {
00122                         NSString *stringValue = [[NSString alloc] initWithCString:value encoding:NSUTF8StringEncoding];
00123                         [checkbox setTitle:stringValue];
00124                         [stringValue release];
00125                 }
00126                         return 0;
00127                 default:
00128                         break;
00129         }
00130         
00131         DBG_WARN(GWEN_LOGDOMAIN,
00132                          "Function is not appropriate for this type of widget (%s)",
00133                          GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
00134         return GWEN_ERROR_INVALID;
00135 }
00136 
00137 
00138 
00139 static GWENHYWFAR_CB
00140 const char* CocoaGui_WCheckBox_GetCharProperty(GWEN_WIDGET *w,
00141                                                                                            GWEN_DIALOG_PROPERTY prop,
00142                                                                                            int index,
00143                                                                                            const char *defaultValue) {
00144         CocoaCheckboxButton *checkbox;
00145         
00146         checkbox = (CocoaCheckboxButton*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
00147         assert(checkbox);
00148         
00149         switch(prop) {
00150                 case GWEN_DialogProperty_Title:
00151                         return [[checkbox stringValue] cStringUsingEncoding:NSUTF8StringEncoding];
00152                 default:
00153                         break;
00154         }
00155         
00156         DBG_WARN(GWEN_LOGDOMAIN,
00157                          "Function is not appropriate for this type of widget (%s)",
00158                          GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
00159         return defaultValue;
00160 }
00161 
00162 
00163 
00164 static void CocoaGui_WCheckBox_Toggled_handler(NSButton *button, void* data) {
00165         GWEN_WIDGET *w;
00166         int rv;
00167         
00168         DBG_ERROR(0, "Toggled");
00169         w=(GWEN_WIDGET*)data;
00170         assert(w);
00171         rv=GWEN_Dialog_EmitSignal(GWEN_Widget_GetDialog(w),
00172                                                           GWEN_DialogEvent_TypeActivated,
00173                                                           GWEN_Widget_GetName(w));
00174         if (rv==GWEN_DialogEvent_ResultAccept)
00175                 CocoaGui_Dialog_Leave(GWEN_Widget_GetTopDialog(w), 1);
00176         else if (rv==GWEN_DialogEvent_ResultReject)
00177                 CocoaGui_Dialog_Leave(GWEN_Widget_GetTopDialog(w), 0);
00178 }
00179 
00180 
00181 
00182 int CocoaGui_WCheckBox_Setup(GWEN_WIDGET *w) {
00183         CocoaCheckboxButton *checkbox;
00184         const char *s;
00185         uint32_t flags;
00186         GWEN_WIDGET *wParent;
00187         
00188         flags=GWEN_Widget_GetFlags(w);
00189         wParent=GWEN_Widget_Tree_GetParent(w);
00190         s=GWEN_Widget_GetText(w, 0);
00191         
00192         //Create Button
00193         checkbox = [[[CocoaCheckboxButton alloc] initWithFrame:NSMakeRect(0.0, 0.0, 60.0, 24.0)] autorelease];
00194         if (flags & GWEN_WIDGET_FLAGS_FILLX) checkbox.fillX = YES;
00195         if (flags & GWEN_WIDGET_FLAGS_FILLY) checkbox.fillY = YES;
00196         if (s && *s) {
00197                 NSString *title = [[NSString alloc] initWithCString:s encoding:NSUTF8StringEncoding];
00198                 [checkbox setTitle:title];
00199                 [title release];
00200         }
00201         
00202         GWEN_Widget_SetImplData(w, COCOA_DIALOG_WIDGET_REAL, (void*) checkbox);
00203         GWEN_Widget_SetImplData(w, COCOA_DIALOG_WIDGET_CONTENT, (void*) checkbox);
00204         
00205         GWEN_Widget_SetSetIntPropertyFn(w, CocoaGui_WCheckBox_SetIntProperty);
00206         GWEN_Widget_SetGetIntPropertyFn(w, CocoaGui_WCheckBox_GetIntProperty);
00207         GWEN_Widget_SetSetCharPropertyFn(w, CocoaGui_WCheckBox_SetCharProperty);
00208         GWEN_Widget_SetGetCharPropertyFn(w, CocoaGui_WCheckBox_GetCharProperty);
00209         
00210         gwenActionPtr ptr = CocoaGui_WCheckBox_Toggled_handler;
00211         [checkbox setC_ActionPtr:ptr Data:w];
00212         
00213         if (wParent)
00214                 GWEN_Widget_AddChildGuiWidget(wParent, w);
00215         
00216         return 0;
00217 }
00218 
00219