1 /*
2  * This file is part of XForms.
3  *
4  *  XForms is free software; you can redistribute it and/or modify it
5  *  under the terms of the GNU Lesser General Public License as
6  *  published by the Free Software Foundation; either version 2.1, or
7  *  (at your option) any later version.
8  *
9  *  XForms is distributed in the hope that it will be useful, but
10  *  WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public License
15  *  along with XForms.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
19 /**
20  * \file fd_pallette.c
21  *
22  *  This file is part of XForms package
23  *  Copyright (c) 1996-2002  T.C. Zhao and Mark Overmars
24  *  All rights reserved.
25  *
26  *  handles the object pallette
27  */
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include "fd_main.h"
34 #include "fd/pallette.h"
35 
36 #include <stdlib.h>
37 
38 static FD_pmain *pmain;
39 
40 typedef struct {
41     FL_OBJECT * ob;        /* the object (radio button) handle */
42     int         cls;       /* the obj class it represents      */
43 } Entry;
44 
45 static Entry *entries;
46 static int nentries;
47 
48 
49 /***************************************
50  ***************************************/
51 
52 static void
init_entry_table(FL_FORM * form)53 init_entry_table( FL_FORM * form )
54 {
55     FL_OBJECT *ob;
56     int i;
57 
58     if ( ! entries )
59         entries = fl_calloc( 1, MAXCLASSES * sizeof *entries );
60 
61     /* we obtain the class by looking at the callback function */
62 
63     for ( i = 0, ob = form->first; i < MAXCLASSES && ob; ob = ob->next, i++ )
64         if ( ob->object_callback )
65         {
66             entries[ nentries ].ob = ob;
67             entries[ nentries ].cls = ob->argument;
68             nentries++;
69         }
70 
71     if ( i >= MAXCLASSES )
72         fprintf( stderr, "fd_pallette - Internal Error: MAXCLASSES "
73                  "too small?\n");
74 }
75 
76 
77 /***************************************
78  ***************************************/
79 
80 static FL_FORM *
create_all(void)81 create_all( void )
82 {
83     FD_buttform *bf;
84     FD_staticform *sf;
85     FD_valuatorform *vf;
86     FD_choiceform *cf;
87     FD_miscform *mf;
88 
89     if ( pmain )
90         return pmain->pmain;
91 
92     pmain = create_form_pmain( );
93     bf = create_form_buttform( );
94     sf = create_form_staticform( );
95     vf = create_form_valuatorform( );
96     cf = create_form_choiceform( );
97     mf = create_form_miscform( );
98 
99     fl_addto_tabfolder( pmain->folder, " Static ", sf->staticform );
100     init_entry_table( sf->staticform );
101     fl_addto_tabfolder( pmain->folder, " Button ", bf->buttform );
102     init_entry_table( bf->buttform );
103     fl_addto_tabfolder( pmain->folder, " Valuator ", vf->valuatorform );
104     init_entry_table( vf->valuatorform );
105     fl_addto_tabfolder( pmain->folder, " Choice ", cf->choiceform );
106     init_entry_table( cf->choiceform );
107     fl_addto_tabfolder( pmain->folder, " Misc. ", mf->miscform );
108     init_entry_table( mf->miscform );
109 
110     return pmain->pmain;
111 }
112 
113 
114 /***************************************
115  ***************************************/
116 
117 void
dismiss_pallete(FL_OBJECT * ob,long data FL_UNUSED_ARG)118 dismiss_pallete( FL_OBJECT * ob,
119                  long        data  FL_UNUSED_ARG )
120 {
121     fl_hide_form( ob->form );
122     reset_pallette_menu_status( );
123 }
124 
125 
126 /***************************************
127  ***************************************/
128 
129 void
hide_pallette(void)130 hide_pallette( void )
131 {
132     FL_FORM *form = create_all( );
133 
134     if ( form->visible )
135         fl_hide_form( form );
136 }
137 
138 
139 /***************************************
140  ***************************************/
141 
142 void
show_pallette(void)143 show_pallette( void )
144 {
145     FL_FORM *form = create_all( );
146     static int first = 1;
147 
148     if ( first )
149     {
150         fl_set_form_position( form, -form->w - 50, 20 );
151         first = 0;
152     }
153 
154     select_pallette_entry( cur_class );
155 
156     fl_show_form( form, FL_PLACE_POSITION, FL_TRANSIENT, "Pallette" );
157 }
158 
159 
160 /***************************************
161  * All pallette entries are radio buttons
162  ***************************************/
163 
164 void
pallette_entry_callback(FL_OBJECT * ob,long data)165 pallette_entry_callback( FL_OBJECT * ob,
166                          long        data )
167 {
168     if ( pmain->vdata && ( ( FL_OBJECT * ) pmain->vdata )->form != ob->form )
169         fl_set_button( pmain->vdata, 0 );
170 
171     pmain->vdata = ob;
172     pmain->ldata = data;
173 
174     /* selects the object in the object browser */
175 
176     select_object_by_class( data );
177 }
178 
179 
180 /***************************************
181  ***************************************/
182 
183 void
reset_pallette(void)184 reset_pallette( void )
185 {
186     if ( pmain && pmain->vdata )
187     {
188         fl_set_button( pmain->vdata, 0 );
189         pmain->vdata = 0;
190     }
191 }
192 
193 
194 /***************************************
195  * This function will be called on object class browser (de)selections
196  ***************************************/
197 
198 void
select_pallette_entry(int cls)199 select_pallette_entry( int cls )
200 {
201     int i;
202 
203     if ( ! entries || cls < 0 )
204         return;
205 
206     for ( i = 0; i < nentries; i++ )
207         if ( entries[ i ].cls == cls )
208         {
209             fl_set_folder( pmain->folder, entries[ i ].ob->form );
210             fl_set_button( entries[ i ].ob, 1 );
211             break;
212         }
213 }
214 
215 
216 #include "fd/pallette.c"
217 
218 
219 /*
220  * Local variables:
221  * tab-width: 4
222  * indent-tabs-mode: nil
223  * End:
224  */
225