gitkrwoptionchoice.c

Go to the documentation of this file.
00001 
00012 #define GITK_RENDERER_C
00013 #define GITKR_WIDGET_OPTIONCHOICE_C
00014 
00015 #include "gitkrincludes.h"
00016 
00018 void gitkr_widget_optionchoice_new(GitkrTextWidgetPtr widget,GitkDialogPtr dialog,xmlNodePtr node) {
00019   widget->icon='?';
00020   gitkr_widget_new(widget,dialog,node);
00021   widget->type=GITK_WIDGET_TYPE_OPTIONCHOICE;
00022   widget->output=gitkr_widget_optionchoice_output;
00023   widget->handle=gitkr_widget_optionchoice_handle;
00024 }
00025 
00027 void gitkr_widget_optionchoice_output(GitkrTextWidgetPtr widget,gint line,gboolean active) {
00028   gint str_len,col;
00029   gchar *value=gitkr_widget_get_value(widget->dialog,widget->id,"value");
00030   GList *choices=gitkr_widget_optionchoice_get_choices(widget);
00031 
00032   gitkr_widget_output_pre(widget,line,active);
00033 
00034   //-- display label
00035   if(widget->label) {
00036     mvaddstr(line,2,widget->label);
00037     str_len=strlen(widget->label);
00038   }
00039   else {
00040     mvaddstr(line,2,"-");
00041     str_len=1;
00042   }
00043   mvaddstr(line,2+str_len," : [");
00044   col=gitkr_widget_optionchoice_value_output(2+str_len+4,line,(value?atoi(value):0),choices);
00045   mvaddstr(line,col,"]");
00046   free(value);
00047   gitkr_widget_optionchoice_free_choices(choices);
00048 
00049   if(active && widget->label) { festival_say_text(ftinfo,widget->label); }
00050   gitkr_widget_output_post(widget,line,active);
00051 }
00052 
00054 gboolean gitkr_widget_optionchoice_handle(GitkrTextWidgetPtr widget,gint line) {
00055   gint key;
00056   gint xmin,xpos;
00057   gboolean sub_run=TRUE,result=TRUE;
00058   gchar *value=gitkr_widget_get_value(widget->dialog,widget->id,"value");
00059   GList *choices=gitkr_widget_optionchoice_get_choices(widget);
00060   gint cur_value=(value?atoi(value):0);  //-- determine selected entry
00061   char res[10]; //-- temporary buffer for setting the result
00062 
00063   //-- un-highlight widget
00064   widget->output(widget,line,FALSE);
00065   //-- activate style
00066   gitkr_widget_output_pre(widget,line,FALSE);
00067   //-- calculate widget size
00068   if(widget->label) {
00069     xmin=2+strlen(widget->label)+4;
00070   }
00071   else {
00072     xmin=2+1+4;
00073   }
00074 
00075   //-- position cursor
00076   xpos=gitkr_widget_optionchoice_get_position(choices,cur_value);
00077   move(line,xmin+xpos);
00078   refresh();
00079   festival_say_text(ftinfo,g_list_nth_data(choices,cur_value));
00080   //-- now run the sub-event-loop
00081   while(run && sub_run) {
00082     if((key=gitkr_event_loop_getkey())==ERR) continue;
00083     //-- process the key-code
00084     switch(key) {
00085       case KEY_F10:    // cancel edit
00086         sub_run=FALSE;result=FALSE;
00087         break;
00088       case KEY_RETURN: // submit edit
00089       case KEY_ENTER:
00090         sprintf(res,"%d",cur_value);
00095         gitkr_widget_set_value(widget->dialog,widget->id,"value",res);
00096         sub_run=FALSE;
00097         break;
00098       case KEY_LEFT:
00099         if(cur_value) {
00100           xpos=gitkr_widget_optionchoice_get_position(choices,cur_value);
00101           mvaddstr(line,xmin+xpos," ");
00102           addstr(g_list_nth_data(choices,cur_value));
00103           addstr(" ");
00104           cur_value--;
00105           xpos=gitkr_widget_optionchoice_get_position(choices,cur_value);
00106           mvaddstr(line,xmin+xpos,"<");
00107           addstr(g_list_nth_data(choices,cur_value));
00108           addstr(">");
00109           move(line,xmin+xpos);
00110           festival_say_text(ftinfo,g_list_nth_data(choices,cur_value));
00111         }
00112         else beep();
00113         refresh();
00114         break;
00115       case KEY_RIGHT:
00116         if(cur_value<(g_list_length(choices)-1)) {
00117           xpos=gitkr_widget_optionchoice_get_position(choices,cur_value);
00118           mvaddstr(line,xmin+xpos," ");
00119           addstr(g_list_nth_data(choices,cur_value));
00120           addstr(" ");
00121           cur_value++;
00122           xpos=gitkr_widget_optionchoice_get_position(choices,cur_value);
00123           mvaddstr(line,xmin+xpos,"<");
00124           addstr(g_list_nth_data(choices,cur_value));
00125           addstr(">");
00126           move(line,xmin+xpos);
00127           festival_say_text(ftinfo,g_list_nth_data(choices,cur_value));
00128         }
00129         else beep();
00130         refresh();
00131         break;
00132     }
00133   }
00134   //-- highlight widget
00135   widget->output(widget,line,TRUE);
00136   //-- re-position cursor
00137   move(line,0);
00138   refresh();
00139 
00140   return(result);
00141 }
00142 
00144 gint gitkr_widget_optionchoice_value_output(gint col,gint line,gint value,GList *choices) {
00145   gint i;
00146   GList *choice;
00147 
00148   g_assert(choices!=NULL);
00149 
00151   for(i=0,choice=g_list_first(choices);choice;i++,choice=g_list_next(choice)) {
00152     if(value!=i) { //-- unselected
00153       mvaddstr(line,col," ");
00154       mvaddstr(line,col+1,choice->data);
00155       col+=(strlen(choice->data)+1);
00156       mvaddstr(line,col," ");
00157       col++;
00158     }
00159     else { //-- selected
00160       mvaddstr(line,col,"<");
00161       mvaddstr(line,col+1,choice->data);
00162       col+=(strlen(choice->data)+1);
00163       mvaddstr(line,col,">");
00164       col++;
00165     }
00166   }
00167   return(col);
00168 }
00169 
00171 GList *gitkr_widget_optionchoice_get_choices(GitkrTextWidgetPtr widget) {
00172   GList *choices=NULL;
00173   xmlXPathObjectPtr options_xpoptr;
00174   xmlNodePtr node=gitk_dialog_get_widget(widget->dialog,widget->id);
00175 
00176   //-- get all options
00177   if((options_xpoptr=gitk_xpath_type_filter(
00178       gitk_xpath_get_object(widget->dialog,"./"GITK_NS_PREFIX":options/"GITK_NS_PREFIX":option",node),
00179       XPATH_NODESET))) {
00180     gint i;
00181     gchar *str;
00182     xmlNodePtr choice;
00183     xmlNodeSetPtr ns=(xmlNodeSetPtr)options_xpoptr->nodesetval;
00184     //-- iterate over nodeset
00185     for(i=0;i<xmlXPathNodeSetGetLength(ns);i++) {
00186       choice=xmlXPathNodeSetItem(ns,i);
00187       if(!xmlNodeIsText(choice)) {
00188         str=xmlNodeGetContent(choice);
00189         choices=g_list_append(choices,g_convert(str,-1,nl_langinfo(CODESET),"UTF-8",NULL,NULL,NULL));
00190         xmlFree(str);
00191       }
00192       else {
00193         gitk_err("unexpected textnode in dialog definition");
00194       }
00195     }
00196     xmlXPathFreeObject(options_xpoptr);
00197   }
00198   else { gitk_err("failed to get options"); }
00199   return(choices);
00200 }
00201 
00205 void gitkr_widget_optionchoice_free_choices(GList *choices) {
00206   gitkr_widget_optionchoice_free_list(choices);
00207 }
00208 
00210 gint gitkr_widget_optionchoice_get_position(GList *choices,gint value) {
00211   gint i,col=0;
00212   GList *choice;
00213 
00214   g_assert(choices!=NULL);
00215 
00216   for(i=0,choice=g_list_first(choices);(i<value && choice);i++,choice=g_list_next(choice)) {
00217     col+=(strlen(choice->data)+2);
00218   }
00219   return(col);
00220 }
00221 
00223 GList *gitkr_widget_optionchoice_get_ids(GitkrTextWidgetPtr widget) {
00224   GList *items=NULL;
00225   xmlXPathObjectPtr options_xpoptr;
00226   xmlNodePtr node=gitk_dialog_get_widget(widget->dialog,widget->id);
00227 
00228   //-- get all options
00229   if((options_xpoptr=gitk_xpath_type_filter(
00230       gitk_xpath_get_object(widget->dialog,"./"GITK_NS_PREFIX":options/"GITK_NS_PREFIX":option",node),
00231       XPATH_NODESET))) {
00232     gint i;
00233     xmlNodePtr item;
00234     xmlNodeSetPtr ns=(xmlNodeSetPtr)options_xpoptr->nodesetval;
00235     //-- iterate over nodeset
00236     for(i=0;i<xmlXPathNodeSetGetLength(ns);i++) {
00237       item=xmlXPathNodeSetItem(ns,i);
00238       if(!xmlNodeIsText(item)) {
00239         items=g_list_append(items,xmlGetProp(item,"id"));
00240       }
00241       else {
00242         gitk_err("unexpected textnode in dialog definition");
00243       }
00244     }
00245     xmlXPathFreeObject(options_xpoptr);
00246   }
00247   else { gitk_err("failed to get options"); }
00248   return(items);
00249 }
00250 
00254 void gitkr_widget_optionchoice_free_ids(GList *ids) {
00255   gitkr_widget_optionchoice_free_list(ids);
00256 }
00257 
00261 void gitkr_widget_optionchoice_free_list(GList *items) {
00262   GList *item;
00263 
00264   g_assert(items!=NULL);
00265 
00266   item=g_list_first(items);
00267   while(item) {
00268     free(item->data);
00269     item=g_list_next(item);
00270   }
00271   g_list_free(items);
00272 }
00273 

Generated on Thu Oct 28 10:59:14 2004 for gitk by doxygen 1.3.6