~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina6/tina-tools/tinatool/wdgts/gtk2/wdgtsGtk_tw_fvalue.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :  TINA
 36  * File    :  $Source: /home/tina/cvs/tina-tools/tinatool/wdgts/gtk2/wdgtsGtk_tw_fvalue.c,v $
 37  * Date    :  $Date: 2009/03/13 17:16:50 $
 38  * Version :  $Revision: 1.3 $
 39  * CVS Id  :  $Id: wdgtsGtk_tw_fvalue.c,v 1.3 2009/03/13 17:16:50 paul Exp $
 40  *
 41  * Author  :  a.lacey@man.ac.uk, giob@man.ac.uk, paul.bromiley@manchester.ac.uk
 42  *
 43  *********
 44 */
 45 /** 
 46  *  @file  wdgtsGtk_tw_fvalue.c
 47  *  @brief Define and create GTK float entry field and its associated callbacks.
 48  *
 49  *  Functions to create a GTK float entry field and to define its use by linking it into the TINA 
 50  *  the TINA callback system using a Tw_callback struct.  Also defines the callback functions
 51  *  invoked when the buttons are used.  Note that a float entry field in GTK is just a text
 52  *  entry field, but TINA handles it differently.
 53  *  tw_fvalue differs from tw_fglobal in that the latter uses a pointer to locate the global
 54  *  data whereas the former uses setter and getter functions.  Both forms are used in the Xv
 55  *  Tina and both are retained to maximise cross-compatibility in the Gtk Tina.
 56  *
 57  *  Note that the functions in this file are duplicated for each widget set (xv, GTK, xm, etc ...).
 58  *
 59  */
 60 
 61 #include "wdgtsGtk_tw_fvalue.h"
 62 
 63 #if HAVE_CONFIG_H
 64 #   include <config.h>
 65 #endif
 66 
 67 #include <stdio.h>
 68 #include <string.h>
 69 #include <gtk/gtk.h>
 70 #include <tinatool/wdgts/gtk2/wdgts_GtkDef.h>
 71 #include <tinatool/wdgts/gtk2/wdgtsGtk_tw_command.h>
 72 #include <tinatool/wdgts/gtk2/wdgtsGtk_tw_tool.h>
 73 
 74 
 75 /**
 76  * @brief Copy float from entry widget to stored callback data field.  
 77  * @param twc Pointer to Tw_callback associated with this float entry widget.
 78  * 
 79  * Callback function for float entry widgets.  Each of these has an associated 
 80  * Tw_callback, and this function uses a setter function stored in the data field 
 81  * of the Tw_callback to copy the current widget float to the desired variable.
 82  *
 83  * Note that this callback is not invoked directly by the widget, but via a call
 84  * to tw_text_callback (in wdgtsGtk_tw_command.c).
 85  */
 86 static void fvalue_call(Tw_callback * twc)
 87 {
 88   double   x; 
 89   void   (*set)() = (void (*) ()) twc->data2;
 90   char    *string = (char *) gtk_entry_get_text(GTK_ENTRY(twc->widget));
 91 
 92   if (sscanf(string, "%lf", &x) == 1)
 93     (*set) (x);
 94 }
 95 
 96 /**
 97  * @brief   Replay float entry from current macro file.  
 98  * @param   twc   Pointer to Tw_callback associated with this widget.
 99  * @param   args  Pointer to required data, stored in the macro file.
100  * 
101  */
102 static void fvalue_cmnd(Tw_callback * twc, char *args)
103 {
104   double   x;
105   void   (*set)() = (void (*) ()) twc->data2;
106   char     string[64];
107 
108   (void) sscanf(args, "%*s %lf", &x);
109   (*set) (x);
110   (void) sscanf(args, "%*s %s", string);
111   gtk_entry_set_text(GTK_ENTRY(twc->widget), string);
112 }
113 
114 /**
115  * @brief  Record float entry in current macro file.  
116  * @param  fp   Pointer to current macro file.
117  * @param  twc  Pointer to Tw_callback associated with this widget.
118  *
119  */
120 static void fvalue_prnt(FILE * fp, Tw_callback * twc)
121 {
122   double  (*get) () = (double (*) ()) twc->data1;
123 
124   (void) fprintf(fp, "%s %lf\n", twc->name, (*get)());
125 }
126 
127 /**
128  * @brief  Create new float entry field and its associated callback struct.  
129  * @param  name    String to be used as entry field identifier and label.
130  * @param  get()   Pointer to getter function for float entered in the field.
131  * @param  set()   Pointer to setter function for float entered in the field.
132  * @param  nchars  Maximum number of characters for input field.
133  * @return Tw_callback  Pointer to new Tw_callback associated with this entry field.
134  * 
135  * Creates new hbox for float entry field, and packs field and label in it.  
136  *
137  * Invokes callback for each keypress, via call to tw_text_callback 
138  * (in wdgtsGtk_tw_command.c).
139  *
140  */
141 Tw_callback *tw_fvalue(char *name, double (*get)(), void (*set)(), int nchars)
142 {
143   GtkWidget *panel= tw_get_panel();
144   GtkWidget *box;
145   GtkWidget *ftxlabel;
146   GtkWidget *ftxentry;
147 
148   char    string[32];
149   double  val;
150 
151   Tw_callback  *twc;
152   char         *fullname = tw_extend_fullname(tw_get_toolname(), name);
153 
154   box = gtk_hbox_new(FALSE, 8);
155   gtk_container_add(GTK_CONTAINER(panel), box);
156   gtk_widget_show(box);
157 
158   ftxlabel = gtk_label_new(name);
159   ftxentry = gtk_entry_new();
160   gtk_widget_set_usize(ftxentry, (10+6*nchars), 22);
161   gtk_box_pack_start(GTK_BOX(box), ftxlabel, FALSE, FALSE, 0);
162   gtk_box_pack_start(GTK_BOX(box), ftxentry, FALSE, FALSE, 0);
163   gtk_widget_show(ftxlabel);
164   gtk_widget_show(ftxentry);
165 
166   val = (*get) ();
167   (void) sprintf(string, "%g", val);
168   if (!strchr(string, '.') && !strchr(string, 'e'))
169   {
170     /* Ensure at least one decimal place is shown (JB 2/3/93) */
171     (void) sprintf(string, "%.1f", val);
172   }
173   gtk_entry_set_text(GTK_ENTRY(ftxentry), string);
174 
175   twc = tw_callback_make(fullname, ftxentry,
176                          fvalue_call, fvalue_cmnd, fvalue_prnt, (void (*) ()) NULL,
177                          (void *) get, (void *) set);
178 
179   /*  Invoke callback for GTK entry "changed" event (i.e. for each keypress) */
180   gtk_signal_connect(GTK_OBJECT(ftxentry), "changed",
181                      GTK_SIGNAL_FUNC(tw_gtk_callback),
182                      (gpointer) twc);
183   
184   return (twc);
185 }
186 
187 /**
188  * @brief Reset float entry field when value has been altered.  
189  * @param twc    Pointer to Tw_callback associated with this text entry widget.
190  * 
191  * NOTE: NOT TESTED YET - GAB 10 Oct 2003.
192  *
193  */
194 void tw_fvalue_reset(Tw_callback * twc)
195 {
196   double (*get)();
197   double   val;
198   char     string[64];
199 
200   if (twc == NULL)
201     return;
202 
203   get = (double (*)()) twc->data1;
204   val = get();
205   (void) sprintf(string, "%g", val);
206   if (!strchr(string, '.') && !strchr(string, 'e'))
207   {
208     /* Ensure at least one decimal place is shown (JB 2/3/93) */
209     (void) sprintf(string, "%.1f", val);
210   }
211   gtk_entry_set_text(GTK_ENTRY(twc->widget), string);
212 }
213 
214 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.