You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
7.9 KiB

13 years ago
13 years ago
13 years ago
13 years ago
  1. /****************/
  2. /* SELCOLOR.CPP */
  3. /****************/
  4. /* Dialog for selecting color from the palette of available colors.
  5. */
  6. #include <fctsys.h>
  7. #include <gr_basic.h>
  8. #include <common.h>
  9. #include <colors.h>
  10. #include <wx/statline.h>
  11. enum colors_id {
  12. ID_COLOR_BLACK = 2000 // ID_COLOR_ = ID_COLOR_BLACK a ID_COLOR_BLACK + 31
  13. };
  14. class WinEDA_SelColorFrame : public wxDialog
  15. {
  16. private:
  17. public:
  18. WinEDA_SelColorFrame( wxWindow* parent,
  19. const wxPoint& framepos, int OldColor );
  20. ~WinEDA_SelColorFrame() {};
  21. private:
  22. void Init_Dialog( int aOldColor );
  23. void OnCancel( wxCommandEvent& event );
  24. void SelColor( wxCommandEvent& event );
  25. DECLARE_EVENT_TABLE()
  26. };
  27. BEGIN_EVENT_TABLE( WinEDA_SelColorFrame, wxDialog )
  28. EVT_BUTTON( wxID_CANCEL, WinEDA_SelColorFrame::OnCancel )
  29. EVT_COMMAND_RANGE( ID_COLOR_BLACK, ID_COLOR_BLACK + 31,
  30. wxEVT_COMMAND_BUTTON_CLICKED,
  31. WinEDA_SelColorFrame::SelColor )
  32. END_EVENT_TABLE()
  33. EDA_COLOR_T DisplayColorFrame( wxWindow* parent, int OldColor )
  34. {
  35. wxPoint framepos;
  36. EDA_COLOR_T color;
  37. wxGetMousePosition( &framepos.x, &framepos.y );
  38. WinEDA_SelColorFrame* frame = new WinEDA_SelColorFrame( parent,
  39. framepos, OldColor );
  40. color = static_cast<EDA_COLOR_T>( frame->ShowModal() );
  41. frame->Destroy();
  42. if( color > NBCOLORS )
  43. color = UNSPECIFIED_COLOR;
  44. return color;
  45. }
  46. WinEDA_SelColorFrame::WinEDA_SelColorFrame( wxWindow* parent,
  47. const wxPoint& framepos,
  48. int OldColor ) :
  49. wxDialog( parent, -1, _( "Colors" ), framepos, wxDefaultSize,
  50. wxDEFAULT_DIALOG_STYLE | MAYBE_RESIZE_BORDER )
  51. {
  52. Init_Dialog( OldColor );
  53. // Resize the dialog
  54. GetSizer()->SetSizeHints( this );
  55. // Ensure the whole frame is visible, whenever the asked position.
  56. // Moreover with a work station having dual monitors, the asked position can be relative to a monitor
  57. // and this frame can be displayed on the other monitor, with an "out of screen" position.
  58. // Give also a small margin.
  59. int margin = 10;
  60. wxPoint windowPosition = GetPosition();
  61. if( framepos != wxDefaultPosition )
  62. {
  63. if( windowPosition.x < margin )
  64. windowPosition.x = margin;
  65. // Under MACOS, a vertical margin >= 20 is needed by the system menubar
  66. int v_margin = std::max(20, margin);
  67. if( windowPosition.y < v_margin )
  68. windowPosition.y = v_margin;
  69. if( windowPosition != framepos )
  70. SetPosition(windowPosition);
  71. }
  72. wxPoint endCornerPosition = GetPosition();
  73. endCornerPosition.x += GetSize().x + margin;
  74. endCornerPosition.y += GetSize().y + margin;
  75. windowPosition = GetPosition();
  76. wxRect freeScreenArea( wxGetClientDisplayRect( ) );
  77. if( freeScreenArea.GetRight() < endCornerPosition.x )
  78. {
  79. windowPosition.x += freeScreenArea.GetRight() - endCornerPosition.x;
  80. if( windowPosition.x < freeScreenArea.x )
  81. windowPosition.x = freeScreenArea.x;
  82. // Sligly modify the vertical position to avoid the mouse to be
  83. // exactly on the upper side of the window
  84. windowPosition.y +=5;
  85. endCornerPosition.y += 5;
  86. }
  87. if( freeScreenArea.GetBottom() < endCornerPosition.y )
  88. {
  89. windowPosition.y += freeScreenArea.GetBottom() - endCornerPosition.y;
  90. if( windowPosition.y < freeScreenArea.y )
  91. windowPosition.y = freeScreenArea.y;
  92. }
  93. SetPosition(windowPosition);
  94. }
  95. void WinEDA_SelColorFrame::Init_Dialog( int aOldColor )
  96. {
  97. wxBoxSizer* OuterBoxSizer = NULL;
  98. wxBoxSizer* MainBoxSizer = NULL;
  99. wxFlexGridSizer* FlexColumnBoxSizer = NULL;
  100. wxBitmapButton* BitmapButton = NULL;
  101. wxStaticText* Label = NULL;
  102. wxStaticLine* Line = NULL;
  103. wxStdDialogButtonSizer* StdDialogButtonSizer = NULL;
  104. wxButton* Button = NULL;
  105. int ii, butt_ID;
  106. int w = 20, h = 20;
  107. bool ColorFound = false;
  108. SetReturnCode( -1 );
  109. OuterBoxSizer = new wxBoxSizer( wxVERTICAL );
  110. SetSizer( OuterBoxSizer );
  111. MainBoxSizer = new wxBoxSizer( wxHORIZONTAL );
  112. OuterBoxSizer->Add( MainBoxSizer, 1, wxGROW | wxLEFT | wxRIGHT | wxTOP, 5 );
  113. for( ii = 0; ii < NBCOLORS; ++ii )
  114. {
  115. // Provide a separate column for every six buttons (and their
  116. // associated text strings), so provide a FlexGrid Sizer with
  117. // eight rows and two columns.
  118. if( ii % 6 == 0 )
  119. {
  120. FlexColumnBoxSizer = new wxFlexGridSizer( 6, 2, 0, 0 );
  121. // Specify that all of the rows can be expanded.
  122. for( int ii = 0; ii < 6; ii++ )
  123. {
  124. FlexColumnBoxSizer->AddGrowableRow( ii );
  125. }
  126. // Specify that the second column can also be expanded.
  127. FlexColumnBoxSizer->AddGrowableCol( 1 );
  128. MainBoxSizer->Add( FlexColumnBoxSizer, 1, wxGROW | wxTOP, 5 );
  129. }
  130. butt_ID = ID_COLOR_BLACK + ii;
  131. wxMemoryDC iconDC;
  132. wxBitmap ButtBitmap( w, h );
  133. wxBrush Brush;
  134. iconDC.SelectObject( ButtBitmap );
  135. EDA_COLOR_T buttcolor = g_ColorRefs[ii].m_Numcolor;
  136. iconDC.SetPen( *wxBLACK_PEN );
  137. ColorSetBrush( &Brush, buttcolor );
  138. Brush.SetStyle( wxSOLID );
  139. iconDC.SetBrush( Brush );
  140. iconDC.SetBackground( *wxGREY_BRUSH );
  141. iconDC.Clear();
  142. iconDC.DrawRoundedRectangle( 0, 0, w, h, (double) h / 3 );
  143. BitmapButton = new wxBitmapButton( this, butt_ID, ButtBitmap,
  144. wxDefaultPosition, wxSize( w+8, h+6 ) );
  145. FlexColumnBoxSizer->Add( BitmapButton, 0,
  146. wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL |
  147. wxLEFT | wxBOTTOM, 5 );
  148. // Set focus to this button if its color matches the
  149. // color which had been selected previously (for
  150. // whichever layer's color is currently being edited).
  151. if( aOldColor == buttcolor )
  152. {
  153. ColorFound = true;
  154. BitmapButton->SetFocus();
  155. }
  156. Label = new wxStaticText( this, -1, ColorGetName( buttcolor ),
  157. wxDefaultPosition, wxDefaultSize, 0 );
  158. FlexColumnBoxSizer->Add( Label, 1,
  159. wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL |
  160. wxLEFT | wxRIGHT | wxBOTTOM, 5 );
  161. }
  162. // Provide a Cancel button as well, so that this dialog
  163. // box can also be canceled by pressing the Esc key
  164. // (and also provide a horizontal static line to separate
  165. // that button from all of the other buttons).
  166. Line = new wxStaticLine( this, -1, wxDefaultPosition,
  167. wxDefaultSize, wxLI_HORIZONTAL );
  168. OuterBoxSizer->Add( Line, 0, wxGROW | wxLEFT | wxRIGHT | wxTOP, 5 );
  169. StdDialogButtonSizer = new wxStdDialogButtonSizer;
  170. OuterBoxSizer->Add( StdDialogButtonSizer, 0, wxGROW | wxALL, 10 );
  171. Button = new wxButton( this, wxID_CANCEL, _( "Cancel" ), wxDefaultPosition,
  172. wxDefaultSize, 0 );
  173. StdDialogButtonSizer->AddButton( Button );
  174. StdDialogButtonSizer->Realize();
  175. // Set focus to the Cancel button if the currently selected color
  176. // does not match any of the colors provided by this dialog box.
  177. // (That shouldn't ever happen in practice though.)
  178. if( !ColorFound )
  179. Button->SetFocus();
  180. }
  181. void WinEDA_SelColorFrame::OnCancel( wxCommandEvent& WXUNUSED( event ) )
  182. {
  183. // Setting the return value to -1 indicates that the
  184. // dialog box has been canceled (and thus that the
  185. // previously selected color is to be retained).
  186. EndModal( -1 );
  187. }
  188. void WinEDA_SelColorFrame::SelColor( wxCommandEvent& event )
  189. {
  190. int id = event.GetId();
  191. EndModal( id - ID_COLOR_BLACK );
  192. }