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

Linux Cross Reference
Tina6/tina-tools/tinatool/wdgts/gtk2/wdgtsGtk_tw_colourbutton.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    :  
 37  * Date    :  
 38  * Version :  
 39  * CVS Id  :  
 40  *
 41  * Notes :
 42  *
 43  * Extra widgets for the mousetool: colour button
 44  *
 45  *********
 46 */
 47 /**
 48  *  @file  wdgtsGtk_tw_colourbutton.c
 49  *  @brief Define and create GTK colour button widget.
 50  *
 51  *  The GTK+ colour button is an icon button in which the icon is a swatch of the 
 52  *  currently selected colour.  Pressing the button spawns a colour selection 
 53  *  dialog box, allowing selection via. colour name, rgb components, hsv components
 54  *  or grpahically using a colour wheel.  Colours are returned or saved to macro 
 55  *  files as rgb components (int 0->65535).  This function also prints a label to 
 56  *  the left of the colour button, using the title of the colour selection dialog
 57  *  box.
 58  *
 59  */
 60 
 61 
 62 #include <gtk/gtk.h>
 63 
 64 #include <stdio.h>
 65 #include <gtk/gtk.h>
 66 #include <tina/sys/sysDef.h>
 67 #include <tina/sys/sysPro.h>
 68 #include <tinatool/draw/drawDef.h>
 69 #include <tinatool/wdgts/gtk2/wdgts_GtkDef.h>
 70 #include <tinatool/wdgts/gtk2/wdgtsGtk_tw_tool.h>
 71 #include <tinatool/wdgts/gtk2/wdgtsGtk_tw_command.h>
 72 
 73 #include "wdgtsGtk_tw_colourbutton.h"
 74 
 75 #if HAVE_CONFIG_H
 76 #   include <config.h>
 77 #endif
 78 
 79 
 80 /**
 81  * @brief  Invoke colour button function.  
 82  * @param  twc  Pointer to Tw_callback associated with this colour button.
 83  *
 84  * Uses data2 field of Tw_callback to set the appropriate tool. 
 85  * (Via global variable in wdgtsGtk_tw_tool.c)
 86  *
 87 */
 88 static void colour_button_call(Tw_callback *twc)
 89 {
 90     int    red, green, blue;
 91     GdkColor *color = g_malloc(sizeof(GdkColor));
 92 
 93     gtk_color_button_get_color(GTK_COLOR_BUTTON(twc->widget), color);
 94 
 95     red   = (int)(color->red);
 96     green = (int)(color->green);
 97     blue  = (int)(color->blue);
 98 
 99     tw_set_tool((GtkWidget *)twc->data2);
100     (*twc->func)(red, green, blue);
101     g_free(color);
102 }
103 
104 
105 /**
106  * @brief  Replay colour button function from current macro file.  
107  * @param  twc  Pointer to Tw_callback associated with this colour button.
108  *
109 */
110 static void colour_button_cmnd(Tw_callback *twc, char *args)
111 {
112     char   name[64];
113     int    red, green, blue;
114     GdkColor *color = g_malloc(sizeof(GdkColor));
115 
116     (void) sscanf(args, "%s %d %d %d", name, &red, &green, &blue);
117     color->red = (guint16)red;
118     color->green = (guint16)green;
119     color->blue = (guint16)blue;
120     gtk_color_button_set_color(GTK_COLOR_BUTTON(twc->widget), color);
121 
122     tw_set_tool((GtkWidget *)twc->data2);
123     (*twc->func)(red, green, blue);
124     g_free(color);
125 }
126 
127 
128 /**
129  * @brief  Record name of colour button in current macro file.  
130  * @param  fp   Pointer to current macro file.
131  * @param  twc  Pointer to Tw_callback associated with this colour button.
132  *
133  * Records colour button press for macro replay facility. 
134  *
135 */
136 static void colour_button_prnt(FILE * fp, Tw_callback *twc)
137 {
138     int red, green, blue;
139     GdkColor *color = g_malloc(sizeof(GdkColor));
140 
141     gtk_color_button_get_color(GTK_COLOR_BUTTON(twc->widget), color);
142     red = (int)(color->red);
143     green = (int)(color->green);
144     blue = (int)(color->blue);
145 
146     (void) fprintf(fp, "%s %d %d %d\n", twc->name, red, green, blue);
147     g_free(color);
148 }
149 
150 
151 /**
152  * @brief Create a Gtk colour button
153  * @param  name    String to be used as the label for the button and dialog box title.
154  * @param  func    Function to be called when the OK button is pressed.
155  * @param  rgb     A TINA Rgb structure containing the initial colour (optional).
156  * @return Tw_callback  Pointer to new Tw_callback associated with the O.K. button for this dialog.
157  *
158  * Note: the internal representation of the colour is as a guint16 for each component of 
159  * rgb i.e. (0-65535).
160  *
161  * Note: the Rgb structure is not freed, so should be freed by the caller immediately after the call.
162 */
163 Tw_callback *tw_colour_button(char *name, void (*func)(), Rgb *rgb)
164 {
165   Tw_callback *twc=NULL;
166   GtkWidget   *tool  = tw_get_tool();
167   GtkWidget   *panel = tw_get_panel();
168   GtkWidget   *colour_button=NULL;
169   GtkWidget   *buttox=NULL;
170   GtkWidget   *label=NULL;
171   GdkColor    *color = g_malloc(sizeof(GdkColor));
172   char        *fullname = tw_extend_fullname(tw_get_toolname(), name);
173   int red = 65535, green = 65535, blue = 65535;
174 
175   /* Pack all buttons into a vbox inside the current hbox: this prevents the  
176      buttons expanding to fill the full vertical range of the current hbox, 
177      which causes them to be larger vertically if there is a choice or check 
178      list in the current hbox. PAB 27/03/2009 */
179 
180   label = gtk_label_new(name);
181   gtk_box_pack_start(GTK_BOX(panel), label, FALSE, FALSE, 0);
182   gtk_widget_show(label);
183 
184   buttox = gtk_vbox_new (FALSE, 0);
185   gtk_box_pack_start(GTK_BOX(panel), buttox, FALSE, FALSE, 0);
186   gtk_widget_show(buttox);
187 
188   if(rgb!=NULL)
189   {
190     red = 256 * ((Rgb *)rgb)->red;
191     green = 256 * ((Rgb *)rgb)->green;
192     blue = 256 * ((Rgb *)rgb)->blue;
193   }
194 
195   color->red = red;
196   color->green = green;
197   color->blue = blue;
198   colour_button = gtk_color_button_new_with_color(color);
199   gtk_color_button_set_title(GTK_COLOR_BUTTON(colour_button), name);
200   gtk_box_pack_end(GTK_BOX(buttox), colour_button, FALSE, FALSE, 0);
201   gtk_widget_show(colour_button);
202 
203   twc = tw_callback_make(fullname, colour_button, colour_button_call, colour_button_cmnd, 
204                          colour_button_prnt, func, NULL, (void *)tool);
205 
206   gtk_signal_connect(GTK_OBJECT(colour_button), "color_set", 
207                      GTK_SIGNAL_FUNC(tw_gtk_callback),
208                      (gpointer)twc);
209   g_free(color);
210   return (twc);
211 }
212 
213 
214 /**
215  * @brief Reset the colour of the colour button
216  * @param  twc The Tw_callback fo the colour button.
217  * @param  rgb An Rgb structure containing the colour to set (r,g,b = 0-256)
218  *
219  * Note: the internal representation of the colour is as a guint16 for each component of 
220  * rgb i.e. (0-65535).
221  *
222  * Note: the Rgb structure is not freed, so should be freed by the caller immediately after the call.
223 */
224 void tw_colour_button_reset(Tw_callback *twc, Rgb *rgb)
225 {
226   GdkColor    *color = g_malloc(sizeof(GdkColor));
227 
228   if(twc==NULL || rgb==NULL) return;
229 
230   color->red = (guint16)(256 * ((Rgb *)rgb)->red);
231   color->green = (guint16)(256 * ((Rgb *)rgb)->green);
232   color->blue = (guint16)(256 * ((Rgb *)rgb)->blue);
233   gtk_color_button_set_color(GTK_COLOR_BUTTON(twc->widget), color);
234 
235   g_free(color);
236 }
237 
238 

~ [ 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.