]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/colrdlgg.cpp
b03589aa7dc839e60c519ae5155e9df1b31906af
   1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/generic/colrdlgg.cpp 
   3 // Purpose:     Choice dialogs 
   4 // Author:      Julian Smart 
   8 // Copyright:   (c) Julian Smart 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // For compilers that support precompilation, includes "wx.h". 
  13 #include "wx/wxprec.h" 
  19 #if wxUSE_COLOURDLG && (!defined(__WXGTK20__) || defined(__WXUNIVERSAL__)) 
  24     #include "wx/dialog.h" 
  25     #include "wx/listbox.h" 
  26     #include "wx/button.h" 
  27     #include "wx/stattext.h" 
  28     #include "wx/layout.h" 
  29     #include "wx/dcclient.h" 
  31     #include "wx/slider.h" 
  35     #include "wx/statline.h" 
  38 #include "wx/generic/colrdlgg.h" 
  40 IMPLEMENT_DYNAMIC_CLASS(wxGenericColourDialog
, wxDialog
) 
  42 BEGIN_EVENT_TABLE(wxGenericColourDialog
, wxDialog
) 
  43     EVT_BUTTON(wxID_ADD_CUSTOM
, wxGenericColourDialog::OnAddCustom
) 
  45     EVT_SLIDER(wxID_RED_SLIDER
, wxGenericColourDialog::OnRedSlider
) 
  46     EVT_SLIDER(wxID_GREEN_SLIDER
, wxGenericColourDialog::OnGreenSlider
) 
  47     EVT_SLIDER(wxID_BLUE_SLIDER
, wxGenericColourDialog::OnBlueSlider
) 
  49     EVT_PAINT(wxGenericColourDialog::OnPaint
) 
  50     EVT_MOUSE_EVENTS(wxGenericColourDialog::OnMouseEvent
) 
  51     EVT_CLOSE(wxGenericColourDialog::OnCloseWindow
) 
  56  * Generic wxColourDialog 
  59 // don't change the number of elements (48) in this array, the code below is 
  60 // hardcoded to use it 
  61 static const wxChar 
*wxColourDialogNames
[] = 
  69     wxT("MEDIUM VIOLET RED"), 
  74     wxT("MEDIUM SPRING GREEN"), 
  77     wxT("LIGHT STEEL BLUE"), 
  97     wxT("MEDIUM VIOLET RED"), 
 101     wxT("MEDIUM SEA GREEN"), 
 103     wxT("MIDNIGHT BLUE"), 
 109     wxT("MEDIUM FOREST GREEN"), 
 114     wxT("MEDIUM SLATE BLUE"), 
 118 wxGenericColourDialog::wxGenericColourDialog() 
 122     colourSelection 
= -1; 
 125 wxGenericColourDialog::wxGenericColourDialog(wxWindow 
*parent
, 
 129     colourSelection 
= -1; 
 130     Create(parent
, data
); 
 133 wxGenericColourDialog::~wxGenericColourDialog() 
 137 void wxGenericColourDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
)) 
 139     EndModal(wxID_CANCEL
); 
 142 bool wxGenericColourDialog::Create(wxWindow 
*parent
, wxColourData 
*data
) 
 144     if ( !wxDialog::Create(parent
, wxID_ANY
, _("Choose colour"), 
 145                            wxPoint(0,0), wxSize(900, 900)) ) 
 148     dialogParent 
= parent
; 
 154     CalculateMeasurements(); 
 160 int wxGenericColourDialog::ShowModal() 
 162     return wxDialog::ShowModal(); 
 166 // Internal functions 
 167 void wxGenericColourDialog::OnMouseEvent(wxMouseEvent
& event
) 
 169   if (event
.ButtonDown(1)) 
 171     int x 
= (int)event
.GetX(); 
 172     int y 
= (int)event
.GetY(); 
 175     // Handle OS/2's reverse coordinate system and account for the dialog title 
 178     GetClientSize(NULL
, &nClientHeight
); 
 179     y 
= (nClientHeight 
- y
) + 20; 
 181     if ((x 
>= standardColoursRect
.x 
&& x 
<= (standardColoursRect
.x 
+ standardColoursRect
.width
)) && 
 182         (y 
>= standardColoursRect
.y 
&& y 
<= (standardColoursRect
.y 
+ standardColoursRect
.height
))) 
 184       int selX 
= (int)(x 
- standardColoursRect
.x
)/(smallRectangleSize
.x 
+ gridSpacing
); 
 185       int selY 
= (int)(y 
- standardColoursRect
.y
)/(smallRectangleSize
.y 
+ gridSpacing
); 
 186       int ptr 
= (int)(selX 
+ selY
*8); 
 187       OnBasicColourClick(ptr
); 
 189     else if ((x 
>= customColoursRect
.x 
&& x 
<= (customColoursRect
.x 
+ customColoursRect
.width
)) && 
 190         (y 
>= customColoursRect
.y 
&& y 
<= (customColoursRect
.y 
+ customColoursRect
.height
))) 
 192       int selX 
= (int)(x 
- customColoursRect
.x
)/(smallRectangleSize
.x 
+ gridSpacing
); 
 193       int selY 
= (int)(y 
- customColoursRect
.y
)/(smallRectangleSize
.y 
+ gridSpacing
); 
 194       int ptr 
= (int)(selX 
+ selY
*8); 
 195       OnCustomColourClick(ptr
); 
 204 void wxGenericColourDialog::OnPaint(wxPaintEvent
& event
) 
 206 #if !defined(__WXMOTIF__) && !defined(__WXPM__) && !defined(__WXCOCOA__) 
 207     wxDialog::OnPaint(event
); 
 214     PaintBasicColours(dc
); 
 215     PaintCustomColours(dc
); 
 216     PaintCustomColour(dc
); 
 217     PaintHighlight(dc
, true); 
 220 void wxGenericColourDialog::CalculateMeasurements() 
 222     smallRectangleSize
.x 
= 18; 
 223     smallRectangleSize
.y 
= 14; 
 224     customRectangleSize
.x 
= 40; 
 225     customRectangleSize
.y 
= 40; 
 230     standardColoursRect
.x 
= 10; 
 232     standardColoursRect
.y 
= 15 + 20; /* OS/2 needs to account for dialog titlebar */ 
 234     standardColoursRect
.y 
= 15; 
 236     standardColoursRect
.width 
= (8*smallRectangleSize
.x
) + (7*gridSpacing
); 
 237     standardColoursRect
.height 
= (6*smallRectangleSize
.y
) + (5*gridSpacing
); 
 239     customColoursRect
.x 
= standardColoursRect
.x
; 
 240     customColoursRect
.y 
= standardColoursRect
.y 
+ standardColoursRect
.height  
+ 20; 
 241     customColoursRect
.width 
= (8*smallRectangleSize
.x
) + (7*gridSpacing
); 
 242     customColoursRect
.height 
= (2*smallRectangleSize
.y
) + (1*gridSpacing
); 
 244     singleCustomColourRect
.x 
= customColoursRect
.width 
+ customColoursRect
.x 
+ sectionSpacing
; 
 245     singleCustomColourRect
.y 
= 80; 
 246     singleCustomColourRect
.width 
= customRectangleSize
.x
; 
 247     singleCustomColourRect
.height 
= customRectangleSize
.y
; 
 250     customButtonX 
= singleCustomColourRect
.x 
; 
 251     buttonY 
= customColoursRect
.y 
+ customColoursRect
.height 
+ 10; 
 254 void wxGenericColourDialog::CreateWidgets() 
 258     wxBoxSizer 
*topSizer 
= new wxBoxSizer( wxVERTICAL 
); 
 260     const int sliderHeight 
= 160; 
 264     const int sliderX 
= singleCustomColourRect
.x 
+ singleCustomColourRect
.width 
+ sectionSpacing
; 
 266     redSlider 
= new wxSlider(this, wxID_RED_SLIDER
, colourData
.m_dataColour
.Red(), 0, 255, 
 267         wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
); 
 268     greenSlider 
= new wxSlider(this, wxID_GREEN_SLIDER
, colourData
.m_dataColour
.Green(), 0, 255, 
 269         wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
); 
 270     blueSlider 
= new wxSlider(this, wxID_BLUE_SLIDER
, colourData
.m_dataColour
.Blue(), 0, 255, 
 271         wxDefaultPosition
, wxSize(wxDefaultCoord
, sliderHeight
), wxSL_VERTICAL
|wxSL_LABELS
|wxSL_INVERSE
); 
 273     wxBoxSizer 
*sliderSizer 
= new wxBoxSizer( wxHORIZONTAL 
); 
 275     sliderSizer
->Add(sliderX
, sliderHeight 
); 
 277     wxSizerFlags flagsRight
; 
 278     flagsRight
.Align(wxALIGN_RIGHT 
| wxALIGN_CENTER_VERTICAL
).DoubleBorder(); 
 280     sliderSizer
->Add(redSlider
, flagsRight
); 
 281     sliderSizer
->Add(greenSlider
,flagsRight
); 
 282     sliderSizer
->Add(blueSlider
,flagsRight
); 
 284     topSizer
->Add(sliderSizer
, wxSizerFlags().Centre().DoubleBorder()); 
 286     topSizer
->Add(1, sliderHeight
, wxSizerFlags(1).Centre().TripleBorder()); 
 287 #endif // wxUSE_SLIDER 
 289     // then the custom button 
 290     topSizer
->Add(new wxButton(this, wxID_ADD_CUSTOM
, 
 291                                   _("Add to custom colours") ), 
 292                      wxSizerFlags().DoubleHorzBorder()); 
 294     // then the standard buttons 
 295     wxSizer 
*buttonsizer 
= CreateSeparatedButtonSizer(wxOK 
| wxCANCEL
); 
 298         topSizer
->Add(buttonsizer
, wxSizerFlags().Expand().DoubleBorder()); 
 301     SetAutoLayout( true ); 
 302     SetSizer( topSizer 
); 
 304     topSizer
->SetSizeHints( this ); 
 305     topSizer
->Fit( this ); 
 312 void wxGenericColourDialog::InitializeColours(void) 
 316     for (i 
= 0; i 
< WXSIZEOF(wxColourDialogNames
); i
++) 
 318         wxColour col 
= wxTheColourDatabase
->Find(wxColourDialogNames
[i
]); 
 320             standardColours
[i
].Set(col
.Red(), col
.Green(), col
.Blue()); 
 322             standardColours
[i
].Set(0, 0, 0); 
 325     for (i 
= 0; i 
< WXSIZEOF(customColours
); i
++) 
 327         wxColour c 
= colourData
.GetCustomColour(i
); 
 329             customColours
[i
] = colourData
.GetCustomColour(i
); 
 331             customColours
[i
] = wxColour(255, 255, 255); 
 334     wxColour curr 
= colourData
.GetColour(); 
 337         bool initColourFound 
= false; 
 339         for (i 
= 0; i 
< WXSIZEOF(wxColourDialogNames
); i
++) 
 341             if ( standardColours
[i
] == curr 
&& !initColourFound 
) 
 345                 initColourFound 
= true; 
 349         if ( !initColourFound 
) 
 351             for ( i 
= 0; i 
< WXSIZEOF(customColours
); i
++ ) 
 353                 if ( customColours
[i
] == curr 
) 
 361         colourData
.m_dataColour
.Set( curr
.Red(), curr
.Green(), curr
.Blue() ); 
 367         colourData
.m_dataColour
.Set( 0, 0, 0 ); 
 371 void wxGenericColourDialog::PaintBasicColours(wxDC
& dc
) 
 374     for (i 
= 0; i 
< 6; i
++) 
 377         for (j 
= 0; j 
< 8; j
++) 
 381             int x 
= (j
*(smallRectangleSize
.x
+gridSpacing
) + standardColoursRect
.x
); 
 382             int y 
= (i
*(smallRectangleSize
.y
+gridSpacing
) + standardColoursRect
.y
); 
 384             dc
.SetPen(*wxBLACK_PEN
); 
 385             wxBrush 
brush(standardColours
[ptr
], wxSOLID
); 
 388             dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
); 
 393 void wxGenericColourDialog::PaintCustomColours(wxDC
& dc
) 
 396   for (i 
= 0; i 
< 2; i
++) 
 399     for (j 
= 0; j 
< 8; j
++) 
 403       int x 
= (j
*(smallRectangleSize
.x
+gridSpacing
)) + customColoursRect
.x
; 
 404       int y 
= (i
*(smallRectangleSize
.y
+gridSpacing
)) + customColoursRect
.y
; 
 406       dc
.SetPen(*wxBLACK_PEN
); 
 408       wxBrush 
brush(customColours
[ptr
], wxSOLID
); 
 411       dc
.DrawRectangle( x
, y
, smallRectangleSize
.x
, smallRectangleSize
.y
); 
 416 void wxGenericColourDialog::PaintHighlight(wxDC
& dc
, bool draw
) 
 418   if ( colourSelection 
< 0 ) 
 421   // Number of pixels bigger than the standard rectangle size 
 422   // for drawing a highlight 
 429     int y 
= (int)(colourSelection 
/ 8); 
 430     int x 
= (int)(colourSelection 
- (y
*8)); 
 432     x 
= (x
*(smallRectangleSize
.x 
+ gridSpacing
) + standardColoursRect
.x
) - deltaX
; 
 433     y 
= (y
*(smallRectangleSize
.y 
+ gridSpacing
) + standardColoursRect
.y
) - deltaY
; 
 436       dc
.SetPen(*wxBLACK_PEN
); 
 438       dc
.SetPen(*wxLIGHT_GREY_PEN
); 
 440     dc
.SetBrush(*wxTRANSPARENT_BRUSH
); 
 441     dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x 
+ (2*deltaX
)), (smallRectangleSize
.y 
+ (2*deltaY
))); 
 445     // User-defined colours 
 446     int y 
= (int)(colourSelection 
/ 8); 
 447     int x 
= (int)(colourSelection 
- (y
*8)); 
 449     x 
= (x
*(smallRectangleSize
.x 
+ gridSpacing
) + customColoursRect
.x
) - deltaX
; 
 450     y 
= (y
*(smallRectangleSize
.y 
+ gridSpacing
) + customColoursRect
.y
) - deltaY
; 
 453       dc
.SetPen(*wxBLACK_PEN
); 
 455       dc
.SetPen(*wxLIGHT_GREY_PEN
); 
 457     dc
.SetBrush(*wxTRANSPARENT_BRUSH
); 
 458     dc
.DrawRectangle( x
, y
, (smallRectangleSize
.x 
+ (2*deltaX
)), (smallRectangleSize
.y 
+ (2*deltaY
))); 
 462 void wxGenericColourDialog::PaintCustomColour(wxDC
& dc
) 
 464     dc
.SetPen(*wxBLACK_PEN
); 
 466     wxBrush 
*brush 
= new wxBrush(colourData
.m_dataColour
, wxSOLID
); 
 469     dc
.DrawRectangle( singleCustomColourRect
.x
, singleCustomColourRect
.y
, 
 470                       customRectangleSize
.x
, customRectangleSize
.y
); 
 472     dc
.SetBrush(wxNullBrush
); 
 476 void wxGenericColourDialog::OnBasicColourClick(int which
) 
 480     PaintHighlight(dc
, false); 
 482     colourSelection 
= which
; 
 485     redSlider
->SetValue( standardColours
[colourSelection
].Red() ); 
 486     greenSlider
->SetValue( standardColours
[colourSelection
].Green() ); 
 487     blueSlider
->SetValue( standardColours
[colourSelection
].Blue() ); 
 488 #endif // wxUSE_SLIDER 
 490     colourData
.m_dataColour
.Set(standardColours
[colourSelection
].Red(), 
 491                                 standardColours
[colourSelection
].Green(), 
 492                                 standardColours
[colourSelection
].Blue()); 
 494     PaintCustomColour(dc
); 
 495     PaintHighlight(dc
, true); 
 498 void wxGenericColourDialog::OnCustomColourClick(int which
) 
 501     PaintHighlight(dc
, false); 
 503     colourSelection 
= which
; 
 506     redSlider
->SetValue( customColours
[colourSelection
].Red() ); 
 507     greenSlider
->SetValue( customColours
[colourSelection
].Green() ); 
 508     blueSlider
->SetValue( customColours
[colourSelection
].Blue() ); 
 509 #endif // wxUSE_SLIDER 
 511     colourData
.m_dataColour
.Set(customColours
[colourSelection
].Red(), 
 512                                 customColours
[colourSelection
].Green(), 
 513                                 customColours
[colourSelection
].Blue()); 
 515     PaintCustomColour(dc
); 
 516     PaintHighlight(dc
, true); 
 520 void wxGenericColourDialog::OnOk(void) 
 525 void wxGenericColourDialog::OnCancel(void) 
 527   colourDialogCancelled = true; 
 532 void wxGenericColourDialog::OnAddCustom(wxCommandEvent
& WXUNUSED(event
)) 
 537     PaintHighlight(dc
, false); 
 540     PaintHighlight(dc
, true); 
 543   customColours
[colourSelection
].Set(colourData
.m_dataColour
.Red(), 
 544                                      colourData
.m_dataColour
.Green(), 
 545                                      colourData
.m_dataColour
.Blue()); 
 547   colourData
.SetCustomColour(colourSelection
, customColours
[colourSelection
]); 
 549   PaintCustomColours(dc
); 
 554 void wxGenericColourDialog::OnRedSlider(wxCommandEvent
& WXUNUSED(event
)) 
 560   colourData
.m_dataColour
.Set((unsigned char)redSlider
->GetValue(), colourData
.m_dataColour
.Green(), colourData
.m_dataColour
.Blue()); 
 561   PaintCustomColour(dc
); 
 564 void wxGenericColourDialog::OnGreenSlider(wxCommandEvent
& WXUNUSED(event
)) 
 570   colourData
.m_dataColour
.Set(colourData
.m_dataColour
.Red(), (unsigned char)greenSlider
->GetValue(), colourData
.m_dataColour
.Blue()); 
 571   PaintCustomColour(dc
); 
 574 void wxGenericColourDialog::OnBlueSlider(wxCommandEvent
& WXUNUSED(event
)) 
 580   colourData
.m_dataColour
.Set(colourData
.m_dataColour
.Red(), colourData
.m_dataColour
.Green(), (unsigned char)blueSlider
->GetValue()); 
 581   PaintCustomColour(dc
); 
 584 #endif // wxUSE_SLIDER 
 586 #endif // wxUSE_COLOURDLG && !defined(__WXGTK20__)