gwenhywfar  4.3.3
w_progressbar.c
Go to the documentation of this file.
00001 /***************************************************************************
00002     begin       : Sun May 16 2010
00003     copyright   : (C) 2010 by Martin Preuss
00004     email       : martin@libchipcard.de
00005 
00006  ***************************************************************************
00007  *          Please see toplevel file COPYING for license details           *
00008  ***************************************************************************/
00009 
00010 
00011 
00012 typedef struct W_PROGRESSBAR W_PROGRESSBAR;
00013 struct W_PROGRESSBAR {
00014   int minValue;
00015   int maxValue;
00016   int currentValue;
00017 };
00018 
00019 
00020 GWEN_INHERIT(GWEN_WIDGET, W_PROGRESSBAR)
00021 
00022 
00023 
00024 static GWENHYWFAR_CB
00025 int Gtk2Gui_WProgressBar_SetIntProperty(GWEN_WIDGET *w,
00026                                         GWEN_DIALOG_PROPERTY prop,
00027                                         int index,
00028                                         int value,
00029                                         int doSignal) {
00030   GtkProgressBar *g;
00031   W_PROGRESSBAR *xw;
00032 
00033   assert(w);
00034   xw=GWEN_INHERIT_GETDATA(GWEN_WIDGET, W_PROGRESSBAR, w);
00035   assert(xw);
00036 
00037   g=GTK_PROGRESS_BAR(GWEN_Widget_GetImplData(w, GTK2_DIALOG_WIDGET_REAL));
00038   assert(g);
00039 
00040   switch(prop) {
00041   case GWEN_DialogProperty_Enabled:
00042     gtk_widget_set_sensitive(GTK_WIDGET(g), (value==0)?FALSE:TRUE);
00043     return 0;
00044   
00045   case GWEN_DialogProperty_Focus:
00046     gtk_widget_grab_focus(GTK_WIDGET(g));
00047     return 0;
00048 
00049   case GWEN_DialogProperty_Width:
00050   case GWEN_DialogProperty_Height:
00051     /* just ignore these for now */
00052     return 0;
00053 
00054   case GWEN_DialogProperty_Value: {
00055 
00056     xw->currentValue=value;
00057     if (xw->maxValue) {
00058       gdouble d;
00059       char numbuf[32];
00060 
00061       d=(gdouble)(xw->currentValue-xw->minValue)/(gdouble)(xw->maxValue);
00062       gtk_progress_bar_set_fraction(g, d);
00063       snprintf(numbuf, sizeof(numbuf)-1, "%d %%", (int)(d*100.0));
00064       numbuf[sizeof(numbuf)-1]=0;
00065       gtk_progress_bar_set_text(g, numbuf);
00066     }
00067     else {
00068       gtk_progress_bar_set_fraction(g, 0.0);
00069       gtk_progress_bar_set_text(g, "");
00070     }
00071     return 0;
00072   }
00073 
00074   case GWEN_DialogProperty_MinValue: {
00075     xw->minValue=value;
00076     if (xw->maxValue) {
00077       gdouble d;
00078       char numbuf[32];
00079 
00080       d=(gdouble)(xw->currentValue-xw->minValue)/(gdouble)(xw->maxValue);
00081       gtk_progress_bar_set_fraction(g, d);
00082       snprintf(numbuf, sizeof(numbuf)-1, "%d %%", (int)(d*100.0));
00083       numbuf[sizeof(numbuf)-1]=0;
00084       gtk_progress_bar_set_text(g, numbuf);
00085     }
00086     else {
00087       gtk_progress_bar_set_fraction(g, 0.0);
00088       gtk_progress_bar_set_text(g, "");
00089     }
00090     return 0;
00091   }
00092 
00093   case GWEN_DialogProperty_MaxValue: {
00094     xw->maxValue=value;
00095     if (xw->maxValue) {
00096       gdouble d;
00097       char numbuf[32];
00098 
00099       d=(gdouble)(xw->currentValue-xw->minValue)/(gdouble)(xw->maxValue);
00100       gtk_progress_bar_set_fraction(g, d);
00101       snprintf(numbuf, sizeof(numbuf)-1, "%d %%", (int)(d*100.0));
00102       numbuf[sizeof(numbuf)-1]=0;
00103       gtk_progress_bar_set_text(g, numbuf);
00104       return 0;
00105     }
00106     else {
00107       gtk_progress_bar_set_fraction(g, 0.0);
00108       gtk_progress_bar_set_text(g, "");
00109     }
00110   }
00111 
00112   default:
00113     break;
00114   }
00115 
00116   DBG_WARN(GWEN_LOGDOMAIN,
00117            "Function is not appropriate for this type of widget (%s)",
00118            GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
00119   return GWEN_ERROR_INVALID;
00120 }
00121 
00122 
00123 
00124 
00125 static GWENHYWFAR_CB
00126 int Gtk2Gui_WProgressBar_GetIntProperty(GWEN_WIDGET *w,
00127                                         GWEN_DIALOG_PROPERTY prop,
00128                                         int index,
00129                                         int defaultValue) {
00130   GtkProgressBar *g;
00131   W_PROGRESSBAR *xw;
00132 
00133   assert(w);
00134   xw=GWEN_INHERIT_GETDATA(GWEN_WIDGET, W_PROGRESSBAR, w);
00135   assert(xw);
00136 
00137   g=GTK_PROGRESS_BAR(GWEN_Widget_GetImplData(w, GTK2_DIALOG_WIDGET_REAL));
00138   assert(g);
00139 
00140   switch(prop) {
00141   case GWEN_DialogProperty_Enabled:
00142     return (gtk_widget_get_sensitive(GTK_WIDGET(g))==TRUE)?1:0;
00143 
00144   case GWEN_DialogProperty_Focus:
00145     return (gtk_widget_has_focus(GTK_WIDGET(g))==TRUE)?1:0;
00146     return 0;
00147 
00148   case GWEN_DialogProperty_Width:
00149   case GWEN_DialogProperty_Height:
00150     /* just ignore these for now */
00151     return 0;
00152 
00153   case GWEN_DialogProperty_Value:
00154     return xw->currentValue;
00155 
00156   case GWEN_DialogProperty_MinValue:
00157     return xw->minValue;
00158 
00159   case GWEN_DialogProperty_MaxValue:
00160     return xw->maxValue;
00161 
00162   default:
00163     break;
00164   }
00165 
00166   DBG_WARN(GWEN_LOGDOMAIN,
00167            "Function is not appropriate for this type of widget (%s)",
00168            GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
00169   return defaultValue;
00170 }
00171 
00172 
00173 
00174 static GWENHYWFAR_CB
00175 int Gtk2Gui_WProgressBar_SetCharProperty(GWEN_WIDGET *w,
00176                                          GWEN_DIALOG_PROPERTY prop,
00177                                          int index,
00178                                          const char *value,
00179                                          int doSignal) {
00180   DBG_WARN(GWEN_LOGDOMAIN,
00181            "Function is not appropriate for this type of widget (%s)",
00182            GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
00183   return GWEN_ERROR_INVALID;
00184 }
00185 
00186 
00187 
00188 static GWENHYWFAR_CB
00189 const char* Gtk2Gui_WProgressBar_GetCharProperty(GWEN_WIDGET *w,
00190                                                  GWEN_DIALOG_PROPERTY prop,
00191                                                  int index,
00192                                                  const char *defaultValue) {
00193   DBG_WARN(GWEN_LOGDOMAIN,
00194            "Function is not appropriate for this type of widget (%s)",
00195            GWEN_Widget_Type_toString(GWEN_Widget_GetType(w)));
00196   return defaultValue;
00197 }
00198 
00199 
00200 
00201 static void GWENHYWFAR_CB Gtk2Gui_WProgressBar_FreeData(void *bp, void *p) {
00202   W_PROGRESSBAR *xw;
00203 
00204   xw=(W_PROGRESSBAR*) p;
00205   GWEN_FREE_OBJECT(xw);
00206 }
00207 
00208 
00209 
00210 int Gtk2Gui_WProgressBar_Setup(GWEN_WIDGET *w) {
00211   GtkWidget *g;
00212   const char *s;
00213   uint32_t flags;
00214   GWEN_WIDGET *wParent;
00215   W_PROGRESSBAR *xw;
00216 
00217   GWEN_NEW_OBJECT(W_PROGRESSBAR, xw);
00218   GWEN_INHERIT_SETDATA(GWEN_WIDGET, W_PROGRESSBAR, w, xw, Gtk2Gui_WProgressBar_FreeData);
00219 
00220   flags=GWEN_Widget_GetFlags(w);
00221   wParent=GWEN_Widget_Tree_GetParent(w);
00222   s=GWEN_Widget_GetText(w, 0);
00223 
00224   g=gtk_progress_bar_new();
00225   GWEN_Widget_SetImplData(w, GTK2_DIALOG_WIDGET_REAL, (void*) g);
00226   GWEN_Widget_SetImplData(w, GTK2_DIALOG_WIDGET_CONTENT, (void*) g);
00227 
00228   GWEN_Widget_SetSetIntPropertyFn(w, Gtk2Gui_WProgressBar_SetIntProperty);
00229   GWEN_Widget_SetGetIntPropertyFn(w, Gtk2Gui_WProgressBar_GetIntProperty);
00230   GWEN_Widget_SetSetCharPropertyFn(w, Gtk2Gui_WProgressBar_SetCharProperty);
00231   GWEN_Widget_SetGetCharPropertyFn(w, Gtk2Gui_WProgressBar_GetCharProperty);
00232 
00233   if (wParent)
00234     GWEN_Widget_AddChildGuiWidget(wParent, w);
00235 
00236   return 0;
00237 }
00238 
00239