selcolor.cpp 6.76 KB
Newer Older
dickelbeck's avatar
dickelbeck committed
1

2
/****************/
3
/* SELCOLOR.CPP */
4
/****************/
5
/* Affichage et selection de la palette des couleurs disponibles
6 7
 * dans une frame
 */
8 9 10 11 12 13

#include "fctsys.h"
#include "gr_basic.h"

#include "common.h"
#include "colors.h"
14
#include "macros.h"
15

16 17
#include "wx/statline.h"

18 19

enum colors_id {
20
    ID_COLOR_BLACK = 2000 // ID_COLOR_ = ID_COLOR_BLACK a ID_COLOR_BLACK + 31
21 22 23 24 25 26 27
};


/*******************************************/
class WinEDA_SelColorFrame: public wxDialog
/*******************************************/
/* Frame d'affichage de la palette des couleurs disponibles
28
 */
29 30 31 32
{
private:
public:

33 34 35 36
    // Constructor and destructor
    WinEDA_SelColorFrame( wxWindow *parent,
                          const wxPoint& framepos, int OldColor );
    ~WinEDA_SelColorFrame() {};
37 38

private:
39 40 41
    void OnCancel(wxCommandEvent& event);
    void SelColor(wxCommandEvent& event);
    DECLARE_EVENT_TABLE()
42
};
43 44


45 46
/* Construction de la table des evenements pour FrameClassMain */
BEGIN_EVENT_TABLE(WinEDA_SelColorFrame, wxDialog)
47 48 49 50
    EVT_BUTTON( wxID_CANCEL, WinEDA_SelColorFrame::OnCancel )
    EVT_COMMAND_RANGE( ID_COLOR_BLACK, ID_COLOR_BLACK + 31,
                       wxEVT_COMMAND_BUTTON_CLICKED,
                       WinEDA_SelColorFrame::SelColor )
51 52
END_EVENT_TABLE()

53

54
/***************************************/
55
int DisplayColorFrame(wxWindow * parent, int OldColor)
56 57 58 59 60
/***************************************/
{
wxPoint framepos;
int color;

61
    wxGetMousePosition(&framepos.x, &framepos.y);
62

63 64 65 66 67 68 69
    WinEDA_SelColorFrame * frame = new WinEDA_SelColorFrame( parent,
                                       framepos, OldColor );
    color = frame->ShowModal();
    frame->Destroy();
    if( color > NBCOLOR )
        color = -1;
    return color;
70 71
}

72

73
/*******************************************************************/
74 75 76
WinEDA_SelColorFrame::WinEDA_SelColorFrame( wxWindow *parent,
                             const wxPoint& framepos, int OldColor ):
        wxDialog( parent, -1, _("Colors"), framepos, wxDefaultSize,
77
                  wxDEFAULT_DIALOG_STYLE|MAYBE_RESIZE_BORDER )
78 79
/*******************************************************************/
{
80 81 82 83 84 85 86 87 88 89 90 91
    wxBoxSizer*             OuterBoxSizer        = NULL;
    wxBoxSizer*             MainBoxSizer         = NULL;
    wxFlexGridSizer*        FlexColumnBoxSizer   = NULL;
    wxBitmapButton*         BitmapButton         = NULL;
    wxStaticText*           Label                = NULL;
    wxStaticLine*           Line                 = NULL;
    wxStdDialogButtonSizer* StdDialogButtonSizer = NULL;
    wxButton*               Button               = NULL;

    int ii, butt_ID, buttcolor;
    int w = 20, h = 20;
    bool ColorFound = false;
92

93 94 95 96 97 98 99 100
    SetFont( *g_DialogFont );

    SetReturnCode( -1 );

    OuterBoxSizer = new wxBoxSizer(wxVERTICAL);
    SetSizer(OuterBoxSizer);

    MainBoxSizer = new wxBoxSizer(wxHORIZONTAL);
101
    OuterBoxSizer->Add(MainBoxSizer, 1, wxGROW|wxLEFT|wxRIGHT|wxTOP, 5);
102 103 104

    for( ii = 0; ColorRefs[ii].m_Name != NULL; ii++ )
    {
105 106
        // Provide a separate column for every eight buttons (and their associated text
        // strings), so provide a FlexGrid Sizer with eight rows and two columns.
107 108
        if( ii % 8 == 0 )
        {
109 110 111 112 113 114 115 116 117 118
            FlexColumnBoxSizer = new wxFlexGridSizer(8, 2, 0, 0);

            // Specify that all of the rows can be expanded.
            for( int ii = 0; ii < 8; ii++ )
            {
                FlexColumnBoxSizer->AddGrowableRow(ii);
            }

            // Specify that the second column can also be expanded.
            FlexColumnBoxSizer->AddGrowableCol(1);
119

120 121
            MainBoxSizer->Add(FlexColumnBoxSizer, 1, wxGROW|wxTOP, 5);
        }
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

        butt_ID = ID_COLOR_BLACK + ii;
        wxMemoryDC iconDC;
        wxBitmap ButtBitmap( w, h );
        wxBrush Brush;
        iconDC.SelectObject( ButtBitmap );
        buttcolor = ColorRefs[ii].m_Numcolor;
        iconDC.SetPen( *wxBLACK_PEN );
        Brush.SetColour(
                        ColorRefs[buttcolor].m_Red,
                        ColorRefs[buttcolor].m_Green,
                        ColorRefs[buttcolor].m_Blue
                        );
        Brush.SetStyle( wxSOLID );

        iconDC.SetBrush( Brush );
        iconDC.SetBackground( *wxGREY_BRUSH );
        iconDC.Clear();
        iconDC.DrawRoundedRectangle( 0, 0, w, h, (double)h / 3 );

        BitmapButton = new wxBitmapButton( this, butt_ID, ButtBitmap,
                                           wxDefaultPosition, wxSize( w, h ) );
144
        FlexColumnBoxSizer->Add(BitmapButton, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT|wxBOTTOM, 5);
145 146 147 148 149 150 151 152 153 154

        // Set focus to this button if its color matches the
        // color which had been selected previously (for
        // whichever layer's color is currently being edited).
        if( OldColor == buttcolor )
        {
            ColorFound = true;
            BitmapButton->SetFocus();
        }

155 156 157
        Label = new wxStaticText( this, -1, ColorRefs[ii].m_Name,
                                  wxDefaultPosition, wxDefaultSize, 0 );
        FlexColumnBoxSizer->Add(Label, 1, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxBOTTOM, 5);
158 159 160 161 162 163 164
    }

    // Provide a Cancel button as well, so that this dialog
    // box can also be cancelled by pressing the Esc key
    // (and also provide a horizontal static line to separate
    // that button from all of the other buttons).

165 166
    Line = new wxStaticLine( this, -1, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
    OuterBoxSizer->Add(Line, 0, wxGROW|wxLEFT|wxRIGHT|wxTOP, 5);
167

168 169
    StdDialogButtonSizer = new wxStdDialogButtonSizer;
    OuterBoxSizer->Add(StdDialogButtonSizer, 0, wxGROW|wxALL, 10);
170

171
    Button = new wxButton( this, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
172
    Button->SetForegroundColour( *wxBLUE );
173 174 175
    StdDialogButtonSizer->AddButton(Button);

    StdDialogButtonSizer->Realize();
176 177 178 179 180 181 182 183

    // Set focus to the Cancel button if the currently selected color
    // does not match any of the colors provided by this dialog box.
    // (That shouldn't ever happen in practice though.)
    if( !ColorFound )
        Button->SetFocus();

    // Resize the dialog
184
    if( GetSizer() )
185 186 187
    {
        GetSizer()->SetSizeHints(this);
    }
188 189 190 191 192 193 194 195 196
}


/***************************************************************/
void WinEDA_SelColorFrame::OnCancel(wxCommandEvent& WXUNUSED(event))
/***************************************************************/
/* Called by the Cancel button
 */
{
197 198 199
    // Setting the return value to -1 indicates that the
    // dialog box has been cancelled (and thus that the
    // previously selected color is to be retained).
200
    EndModal( -1 );
201 202 203 204 205 206 207 208 209
}


/*********************************************************/
void WinEDA_SelColorFrame::SelColor(wxCommandEvent& event)
/*********************************************************/
{
int id = event.GetId();

210
    EndModal( id - ID_COLOR_BLACK );
211
}