gwenhywfar  4.3.1
w_pushbutton.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 "CocoaButton.h"
00011 
00012 
00013 static GWENHYWFAR_CB
00014 int CocoaGui_WPushButton_SetIntProperty(GWEN_WIDGET *w,
00015                                                                                 GWEN_DIALOG_PROPERTY prop,
00016                                                                                 int index,
00017                                                                                 int value,
00018                                                                                 int doSignal) {
00019         
00020         CocoaButton *button;
00021         
00022         button=(CocoaButton*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
00023         assert(button);
00024         
00025         switch(prop) {
00026                 case GWEN_DialogProperty_Enabled:
00027                         [button setEnabled:(value==0)?NO:YES];
00028                         return 0;
00029                         
00030                 case GWEN_DialogProperty_Focus:
00031                         if ([button window]) {
00032                                 [[button window] makeFirstResponder:button];
00033                         }
00034                         return 0;
00035                         
00036                 case GWEN_DialogProperty_Width: {
00037                         NSRect frame = [button frame];
00038                         frame.size.width = value;
00039                         [button setFrame:frame];
00040                 }
00041                         return 0;
00042                         
00043                 case GWEN_DialogProperty_Height: {
00044                         NSRect frame = [button frame];
00045                         frame.size.height = value;
00046                         [button setFrame:frame];
00047                 }
00048                         return 0;
00049                         
00050                 default:
00051                         break;
00052         }
00053         
00054         DBG_WARN(GWEN_LOGDOMAIN,
00055                          "Function is not appropriate for this type of widget (%s)",
00056                          GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
00057         return GWEN_ERROR_INVALID;
00058 }
00059 
00060 
00061 
00062 
00063 static GWENHYWFAR_CB
00064 int CocoaGui_WPushButton_GetIntProperty(GWEN_WIDGET *w,
00065                                                                                 GWEN_DIALOG_PROPERTY prop,
00066                                                                                 int index,
00067                                                                                 int defaultValue) {
00068         CocoaButton *button;
00069         
00070         button=(CocoaButton*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
00071         assert(button);
00072         
00073         switch(prop) {
00074                 case GWEN_DialogProperty_Enabled:
00075                         return ([button isEnabled]==YES)?1:0;
00076                         
00077                 case GWEN_DialogProperty_Focus:
00078                         if ([button window]) {
00079                                 if ([[button window] firstResponder] == button) return 1;
00080                         }
00081                         return 0;
00082                         
00083                 case GWEN_DialogProperty_Width:
00084                         return [button frame].size.width;
00085                         
00086                 case GWEN_DialogProperty_Height:
00087                         return [button 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_WPushButton_SetCharProperty(GWEN_WIDGET *w,
00103                                                                                  GWEN_DIALOG_PROPERTY prop,
00104                                                                                  int index,
00105                                                                                  const char *value,
00106                                                                                  int doSignal) {
00107         
00108         CocoaButton *button;
00109         
00110         button=(CocoaButton*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
00111         assert(button);
00112         
00113         switch(prop) {
00114                 case GWEN_DialogProperty_Title: {
00115                         NSString *stringValue = [[NSString alloc] initWithCString:value encoding:NSUTF8StringEncoding];
00116                         [button setTitle:stringValue];
00117                         [stringValue release];
00118                 }
00119                         return 0;
00120                 default:
00121                         break;
00122         }
00123         
00124         DBG_WARN(GWEN_LOGDOMAIN,
00125                          "Function is not appropriate for this type of widget (%s)",
00126                          GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
00127         return GWEN_ERROR_INVALID;
00128 }
00129 
00130 
00131 
00132 static GWENHYWFAR_CB
00133 const char* CocoaGui_WPushButton_GetCharProperty(GWEN_WIDGET *w,
00134                                                                                                  GWEN_DIALOG_PROPERTY prop,
00135                                                                                                  int index,
00136                                                                                                  const char *defaultValue) {
00137         CocoaButton *button;
00138         
00139         button=(CocoaButton*)(GWEN_Widget_GetImplData(w, COCOA_DIALOG_WIDGET_REAL));
00140         assert(button);
00141         
00142         switch(prop) {
00143                 case GWEN_DialogProperty_Title:
00144                         return [[button stringValue] cStringUsingEncoding:NSUTF8StringEncoding];
00145                 default:
00146                         break;
00147         }
00148         
00149         DBG_WARN(GWEN_LOGDOMAIN,
00150                          "Function is not appropriate for this type of widget (%s)",
00151                          GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
00152         return defaultValue;
00153 }
00154 
00155 
00156 
00157 static void CocoaGui_WPushButton_Clicked_handler(NSButton *button, void* data) {
00158         GWEN_WIDGET *w;
00159         int rv;
00160         
00161         DBG_ERROR(0, "Clicked");
00162         w=(GWEN_WIDGET*)data;
00163         assert(w);
00164         rv=GWEN_Dialog_EmitSignal(GWEN_Widget_GetDialog(w),
00165                                                           GWEN_DialogEvent_TypeActivated,
00166                                                           GWEN_Widget_GetName(w));
00167         if (rv==GWEN_DialogEvent_ResultAccept)
00168                 CocoaGui_Dialog_Leave(GWEN_Widget_GetTopDialog(w), 1);
00169         else if (rv==GWEN_DialogEvent_ResultReject)
00170                 CocoaGui_Dialog_Leave(GWEN_Widget_GetTopDialog(w), 0);
00171 }
00172 
00173 
00174 
00175 int CocoaGui_WPushButton_Setup(GWEN_WIDGET *w) {
00176         
00177         CocoaButton *button;
00178         const char *s;
00179         uint32_t flags;
00180         GWEN_WIDGET *wParent;
00181         
00182         flags=GWEN_Widget_GetFlags(w);
00183         wParent=GWEN_Widget_Tree_GetParent(w);
00184         s=GWEN_Widget_GetText(w, 0);
00185         
00186         
00187         //Create Button
00188         button = [[[CocoaButton alloc] initWithFrame:NSMakeRect(0.0, 0.0, 60.0, 24.0)] autorelease];
00189         if (flags & GWEN_WIDGET_FLAGS_FILLX) button.fillX = YES;
00190         if (flags & GWEN_WIDGET_FLAGS_FILLY) button.fillY = YES;
00191         [button setBezelStyle:NSRoundedBezelStyle];
00192         if (s && *s) {
00193                 NSString *title = [[NSString alloc] initWithCString:s encoding:NSUTF8StringEncoding];
00194                 [button setTitle:title];
00195                 [title release];
00196         }
00197         
00198         s=GWEN_Widget_GetIconFileName(w);
00199         if (s && *s) {
00200                 GWEN_STRINGLIST *sl;
00201                 
00202                 sl=GWEN_Dialog_GetMediaPaths(GWEN_Widget_GetDialog(w));
00203                 if (sl) {
00204                         int rv;
00205                         GWEN_BUFFER *tbuf;
00206                         
00207                         tbuf=GWEN_Buffer_new(0, 256, 0, 1);
00208                         rv=GWEN_Directory_FindFileInPaths(sl, s, tbuf);
00209                         if (rv<0) {
00210                                 DBG_ERROR(GWEN_LOGDOMAIN, "Image file [%s] not found (%d)", s, rv);
00211                                 // ignore result here, instead create GtkImage with "broken mage" later
00212                         }
00213                         else {
00214                                 NSString *pathToIconFile = [[NSString alloc] initWithCString:GWEN_Buffer_GetStart(tbuf) encoding:NSUTF8StringEncoding];
00215                                 if (pathToIconFile) {
00216                                         NSImage *icon = [[NSImage alloc] initWithContentsOfFile:pathToIconFile];
00217                                         CGFloat height = 15.0;
00218                                         NSSize imageSize = [icon size];
00219                                         imageSize.width = round(imageSize.width/(imageSize.height/height));
00220                                         imageSize.height = 15.0;
00221                                         [icon setSize:imageSize];
00222                                         [pathToIconFile release];
00223                                         if (icon) {
00224                                                 //[button setBezelStyle:NSRegularSquareBezelStyle];
00225                                                 [button setImage:icon];
00226                                                 [button setImagePosition:NSImageLeft];
00227                                                 //[[button cell] setImageScaling:NSImageScaleProportionallyUpOrDown];
00228                                                 [icon release];
00229                                         }
00230                                 }
00231                         }
00232                         GWEN_Buffer_free(tbuf);
00233                 }
00234         }
00235         
00236         GWEN_Widget_SetImplData(w, COCOA_DIALOG_WIDGET_REAL, (void*) button);
00237         GWEN_Widget_SetImplData(w, COCOA_DIALOG_WIDGET_CONTENT, (void*) button);
00238         
00239         GWEN_Widget_SetSetIntPropertyFn(w, CocoaGui_WPushButton_SetIntProperty);
00240         GWEN_Widget_SetGetIntPropertyFn(w, CocoaGui_WPushButton_GetIntProperty);
00241         GWEN_Widget_SetSetCharPropertyFn(w, CocoaGui_WPushButton_SetCharProperty);
00242         GWEN_Widget_SetGetCharPropertyFn(w, CocoaGui_WPushButton_GetCharProperty);
00243         
00244         gwenActionPtr ptr = CocoaGui_WPushButton_Clicked_handler;
00245         [button setC_ActionPtr:ptr Data:w];
00246         
00247         if (wParent)
00248                 GWEN_Widget_AddChildGuiWidget(wParent, w);
00249         
00250         return 0;
00251 }
00252 
00253