summaryrefslogtreecommitdiff
path: root/Source/FLTKDialog/FLTKPropertyItemRow.cxx
blob: 5c12637efd3aa31356229ae0a26b76fc4a1b1b9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Insight Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "FLTKPropertyItemRow.h"
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Tile.H>
#include <FL/fl_ask.H>
#include <FL/fl_file_chooser.H>
#include <FL/Fl_Color_Chooser.H>
#include <FL/Fl_Menu_Button.H>
#include "../cmCacheManager.h"
#include "FLTKPropertyList.h"
#include "CMakeSetupGUIImplementation.h"
#include <stdio.h>
#include "../cmake.h"
#include "../cmCacheManager.h"

namespace fltk {



CMakeSetupGUIImplementation * PropertyItemRow::m_CMakeSetup = 0;




PropertyItemRow
::PropertyItemRow( PropertyItem * pItem ):Fl_Tile(0,0,10,10,"")
{
   
  m_PropertyItem =     pItem;
  m_ItemValue    =     new ItemValue;

  const unsigned int fontsize     =         11;
  const unsigned int nameWidth    =        200;
  const unsigned int textWidth    =       1400;
  const unsigned int checkWidth   =  textWidth;
  const unsigned int browseWidth  =         20;
  const unsigned int firstColumn  =          0;

  const unsigned int secondColumn =  nameWidth;

  const unsigned int rowHeight    =         20;
 
  size( nameWidth + textWidth , rowHeight );

  // Make the parent Fl_Pack widget at least a row wide.
  parent()->size( nameWidth + textWidth , rowHeight );

  m_NameButton = new 
          Fl_Button( firstColumn, 0, nameWidth, rowHeight, 
                                    m_PropertyItem->m_propName.c_str() );

  m_NameButton->align( FL_ALIGN_CLIP | FL_ALIGN_LEFT | FL_ALIGN_INSIDE );
  m_NameButton->labelsize( fontsize );
  m_NameButton->box( FL_DOWN_BOX );
  m_NameButton->size( secondColumn, rowHeight );
  m_NameButton->callback( NameButtonCallback, (void *)m_PropertyItem );
  if( m_PropertyItem->m_NewValue ) 
    {
    m_NameButton->color(FL_RED);
    }
    
  switch( m_PropertyItem->m_nItemType )
  {
    case PropertyList::COMBO:
    {
      break;
    }
    case PropertyList::EDIT: 
    {

      Fl_Input * input = new 
                    Fl_Input( secondColumn, 0, textWidth ,rowHeight ,"");
      input->value( m_PropertyItem->m_curValue.c_str() );
      input->textsize( fontsize );
      input->callback( InputTextCallback, (void *)m_PropertyItem );
      input->when( FL_WHEN_CHANGED );

      break;
    }
    case PropertyList::COLOR:
    {
      Fl_Button * colorButton = new
            Fl_Button( secondColumn, 0, textWidth  ,rowHeight ,"");
      colorButton->labelsize( fontsize );
      //colorButton->color();
      colorButton->callback( ColorSelectionCallback, (void *)m_PropertyItem );

      break;
    }
    case PropertyList::FILE:
    {

      Fl_Button * browseButton = new 
            Fl_Button( secondColumn, 0, browseWidth  ,rowHeight ,"...");
      browseButton->labelsize( fontsize );

      Fl_Input * input = new 
                    Fl_Input( secondColumn+browseWidth, 0, textWidth ,rowHeight ,"");
      input->value( m_PropertyItem->m_curValue.c_str() );
      input->textsize( fontsize );

      m_ItemValue->m_InputText    = input;
      m_ItemValue->m_PropertyItem = m_PropertyItem;
        
      browseButton->callback( BrowsePathCallback, (void *)m_ItemValue );
      input->callback( InputTextCallback, m_PropertyItem );
      input->when( FL_WHEN_CHANGED );
      
      break;
    }
    case PropertyList::CHECKBOX:
    {
      Fl_Button * button = new 
            Fl_Button( secondColumn, 0, checkWidth  ,rowHeight ,"");
      button->align( FL_ALIGN_INSIDE | FL_ALIGN_LEFT );
      button->callback( CheckButtonCallback, (void *)m_PropertyItem );

      if( m_PropertyItem->m_curValue == "ON" ) 
      {
        button->label(" ON  ");
        button->value(1);
      }
      else if( m_PropertyItem->m_curValue == "OFF" )
      {
        button->label(" OFF ");
        button->value(0);
      }
      button->type( FL_TOGGLE_BUTTON );
      button->labelsize( fontsize );
      break;
    }
    case PropertyList::PATH:
    {

      Fl_Button * browseButton = new 
            Fl_Button( secondColumn, 0, browseWidth  ,rowHeight ,"...");
      browseButton->labelsize( fontsize );

      Fl_Input * input = new 
                    Fl_Input( secondColumn+browseWidth, 0, textWidth ,rowHeight ,"");
      input->value( m_PropertyItem->m_curValue.c_str() );
      input->textsize( fontsize );

      m_ItemValue->m_InputText    = input;
      m_ItemValue->m_PropertyItem = m_PropertyItem;
        
      browseButton->callback( BrowsePathCallback, (void *)m_ItemValue );
      input->callback( InputTextCallback, (void *)m_PropertyItem );
      input->when( FL_WHEN_CHANGED );
      
      break;
    }
    break;
  default:
    fl_alert("Unkown item type %d",m_PropertyItem->m_nItemType);
    break;
  }


  end(); // Close the inclusion of widgets in the Tile object

}





PropertyItemRow::~PropertyItemRow( )
{
  delete m_ItemValue;
}



void PropertyItemRow
::SetCMakeSetupGUI( CMakeSetupGUIImplementation * cmakeSetup )
{
  m_CMakeSetup   =     cmakeSetup;
}


  
void 
PropertyItemRow::
NameButtonCallback( Fl_Widget * widget, void * data) 
{
  Fl_Button    * button = (Fl_Button *)widget;
  PropertyItem * pItem  = (PropertyItem *)data;
  
  static Fl_Menu_Button * popupMenu = 0;
  if( !popupMenu )
  {
    int lastMousePositionX = Fl::event_x();
    int lastMousePositionY = Fl::event_y();
    popupMenu = new Fl_Menu_Button(lastMousePositionX,
                                   lastMousePositionY,100,200);
  }
  
  popupMenu->type( Fl_Menu_Button::POPUP3 );
  popupMenu->add("Help|Remove|Properties...");
  popupMenu->popup();
 
  typedef enum {
    HELP=0,
    REMOVE,
    PROPERTIES
  } MenuOptions;
    
  
  switch( popupMenu->value() )
  {
    case HELP:
      fl_message( pItem->m_HelpString.c_str() );
      break;
    case REMOVE: // Remove
    {
      const char * propertyName = pItem->m_propName.c_str();
      int answer = fl_ask( "Do you want to remove property %s", propertyName );
      if( answer == 1 )
      {
        // Remove the entry from the cache
        m_CMakeSetup->GetCMakeInstance()->GetCacheManager()->RemoveCacheEntry(propertyName);
          // Get the parent: Fl_Tile that manages the whole row in the GUI
        Fl_Group * parentGroup      = dynamic_cast<Fl_Group *>(button->parent());
        // Get the grandParent: Fl_Pack with the property list
        Fl_Group * grandParentGroup = dynamic_cast<Fl_Group *>( parentGroup->parent() );
        // Remove the row from the list
        grandParentGroup->remove( *parentGroup );
        // Destroy the row
        delete parentGroup;  // Patricide... ?
        // Redraw the list
        grandParentGroup->redraw();
        FillCacheManagerFromCacheGUI();        
        return;
      }
      break;
    }
    case PROPERTIES: // Properties
      break;
  }
}
  


void
PropertyItemRow::
FillCacheManagerFromCacheGUI( void )
{
  if( m_CMakeSetup )
  {
    m_CMakeSetup->FillCacheManagerFromCacheGUI();
  }
}



void 
PropertyItemRow::
CheckButtonCallback( Fl_Widget * widget, void * data) 
{
  Fl_Button    * button = (Fl_Button *)widget;
  PropertyItem * pItem  = (PropertyItem *)data;
  
  int value = button->value();

  if( value )
  {
    button->label(" ON ");
    pItem->m_curValue = "ON";
  }
  else 
  {
    button->label(" OFF ");
    pItem->m_curValue = "OFF";
  }

  pItem->m_Dirty = true;
    
  button->redraw();
  
  FillCacheManagerFromCacheGUI();

}



void 
PropertyItemRow::
InputTextCallback(   Fl_Widget * widget, void * data)
{
  Fl_Input  * input     = (Fl_Input *)widget;
  PropertyItem * item   = (PropertyItem *)data;
  
  item->m_curValue      = input->value();

  item->m_Dirty = true;

  FillCacheManagerFromCacheGUI();

}



void 
PropertyItemRow::
ColorSelectionCallback(   Fl_Widget * widget, void * data)
{
  Fl_Button    * colorButton   = (Fl_Button *)widget;
  PropertyItem * propertyItem  = (PropertyItem *)data;

  static Fl_Color colorIndex = FL_FREE_COLOR;

  unsigned char red   = 0;
  unsigned char blue  = 0;
  unsigned char green = 0;
  fl_color_chooser("Please pick a color",red,green,blue);
  char buffer[300];
  sprintf( buffer,"RGB(%d,%d,%d)", red, green, blue );
  propertyItem->m_curValue = buffer;
  Fl::set_color( colorIndex, red, green, blue );
  colorButton->color( colorIndex );
  colorIndex = (Fl_Color)( colorIndex + 1 );
  if( colorIndex == FL_FREE_COLOR + FL_NUM_FREE_COLOR )
  {
    fl_alert("Maximum number of free colors used, recycling...");
    colorIndex = FL_FREE_COLOR;
  }

  propertyItem->m_Dirty = true;

  colorButton->redraw();
 
  FillCacheManagerFromCacheGUI();

}






void 
PropertyItemRow::
BrowsePathCallback(   Fl_Widget * widget, void * data)
{
  ItemValue    * itemValue    = (ItemValue *)data;
  Fl_Input     * inputText    = itemValue->m_InputText;
  PropertyItem * propertyItem = itemValue->m_PropertyItem;

  const char * newpath = 
    fl_file_chooser("Select a path","*", inputText->value() );
  
  if( newpath ) 
  {
    propertyItem->m_curValue = newpath;
    inputText->value( newpath );
  }

  propertyItem->m_Dirty = true;

  FillCacheManagerFromCacheGUI();

}




} // end namespace fltk