-
Notifications
You must be signed in to change notification settings - Fork 1
/
XPMEditorPanel.cpp
7819 lines (6907 loc) · 269 KB
/
XPMEditorPanel.cpp
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************
* Plugin: XPMEditor for Code::Blocks
* Name: XPMEditorPanel.cpp
* Purpose: the panel containing all the drawing tools and the draw canvas - code
* Author: Seb ([email protected])
* Created: 2009-04-23
* Copyright: Seb ()
* License: GPL 3.0
**************************************************************/
#include <wx/log.h>
#include <wx/dcclient.h>
#include <wx/dcbuffer.h>
#include <wx/brush.h>
#include <wx/pen.h>
#include <wx/utils.h>
#include <wx/clipbrd.h>
#include <wx/dataobj.h>
#include <wx/rawbmp.h>
#include <wx/region.h>
#include <wx/graphics.h>
#include <wx/fontdlg.h>
#include <wx/textfile.h>
#include <wx/stattext.h>
#include <wx/textfile.h>
#include <wx/bitmap.h>
#include <wx/utils.h>
#include "wxDragImageExt.h"
#include <filefilters.h>
#include <manager.h>
#include <configmanager.h>
#include <logmanager.h>
#include <math.h>
#define PI 3.14159265358979
//allow for line an angle of 45 degrees when SHIFT KEY is pressed
#define XPM_LINEORIENTATION PI/4.0
//allow for pen an angle of 90 degrees when SHIFT KEY is pressed
#define XPM_PENORIENTATION PI/2.0
#include "XPMEditor.h"
#include "wxStretchImage.h"
#include "wxMirror.h"
#include "wxRotate.h"
#include "wxBlur.h"
#include "wxRotateHue.h"
#include "wxConversion.h"
#include "wxInvertDialog.h"
#include "wxImageDataObject.h"
#include "XPMToolPanel.h"
#include "XPMHelpPanel.h"
#include "XPMImagePropertiesPanel.h"
#include "XPMImageManipulationPanel.h"
//(*InternalHeaders(XPMEditorPanel)
#include "XPMInterfacePanel.h"
#include "XPMDrawCanvasPanel.h"
#include <wx/aui/aui.h>
#include "XPMColourPickerPanel.h"
#include <wx/intl.h>
#include "XPMFoldPanel.h"
#include <wx/string.h>
//*)
#define _DEBUG_XPM_EDITOR_PANEL 0
//----------- INIITIALISATION & DECLARATIONS ------------------------------
//(*IdInit(XPMEditorPanel)
const long XPMEditorPanel::ID_DRAWCANVASPANEL = wxNewId();
const long XPMEditorPanel::ID_FOLDPANEL = wxNewId();
const long XPMEditorPanel::ID_COLOURPICKERPANEL = wxNewId();
const long XPMEditorPanel::ID_INTERFACEPANEL = wxNewId();
const long XPMEditorPanel::ID_POPUP_COPYTO = wxNewId();
const long XPMEditorPanel::ID_POPUP_PASTEFROM = wxNewId();
//*)
BEGIN_EVENT_TABLE(XPMEditorPanel,wxPanel)
//(*EventTable(XPMEditorPanel)
//*)
END_EVENT_TABLE()
//---------------------------- CONSTRUCTORS ------------------------------
/** Constructor - Create a new Image Editor panel
* \param parent: the parent window for the panel
* \param id = wxID_ANY : the window identifier for this panel
* \param pos = wxDefaultPosition : the desired position of the panel after creation
* \param size = wxDefaultSize : the desired size of the panel after creation
*/
XPMEditorPanel::XPMEditorPanel(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
int i;
BuildContent(parent,id,pos,size);
m_Bitmap = wxBitmap(XPM_DEFAULT_WIDTH, XPM_DEFAULT_HEIGHT);
m_Image = wxImage(1,1);
UpdateScaledBitmap();
m_ImageFormat = wxBITMAP_TYPE_ANY;
bCanResizeX = false;
bCanResizeY = false;
bSizingX = false;
bSizingY = false;
OldX = 0; OldY = 0;
m_dScale = 1;
//selection initialisation
NbPointsMax = 100;
NbPoints = 0;
pSelection = (wxPoint*) malloc(NbPointsMax * sizeof(wxPoint));
m_bDrawSelection = true;
//Undo and Redo buffer
m_undo_buffer = new XPMUndo;
if (m_undo_buffer) m_undo_buffer->SetParentPanel(this);
bUsingTool = false;
m_DragImage = NULL;
m_bDragging = false;
m_bEraseSelection = false;
m_SelectionImage = wxImage();
pStartDragging = wxPoint(0,0);
cMaskColour = *wxBLACK;
m_iSizeAction = 0;
m_bSizing = false;
iHotSpotX = -1;
iHotSpotY = -1;
m_bDrawToolDynamic = false;
//set tooltips & Help text
for(i=0;i<XPM_NUMBER_TOOLS;i++)
{
iToolHelpIndex[i] = -1;
}
SetToolTips();
SetHelpTexts();
UpdateConfiguration();
}
/** Create all the widgets in the panel. Called by the constructor
* \param parent: the parent window for the panel
* \param id = wxID_ANY : the window identifier for this panel
* \param pos = wxDefaultPosition : the desired position of the panel after creation
* \param size = wxDefaultSize : the desired size of the panel after creation
*/
void XPMEditorPanel::BuildContent(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
Freeze();
//(*Initialize(XPMEditorPanel)
Create(parent, wxID_ANY, wxDefaultPosition, wxSize(199,144), wxTAB_TRAVERSAL|wxFULL_REPAINT_ON_RESIZE, _T("wxID_ANY"));
m_AUIXPMEditor = new wxAuiManager(this, wxAUI_MGR_ALLOW_FLOATING|wxAUI_MGR_ALLOW_ACTIVE_PANE|wxAUI_MGR_DEFAULT);
DrawCanvasPanel = new XPMDrawCanvasPanel(this);
m_AUIXPMEditor->AddPane(DrawCanvasPanel, wxAuiPaneInfo().Name(_T("Image")).CenterPane().Caption(_("Image")).PinButton());
FoldPanel = new XPMFoldPanel(this);
m_AUIXPMEditor->AddPane(FoldPanel, wxAuiPaneInfo().Name(_T("Tools")).DefaultPane().Caption(_("Tools")).PinButton().CloseButton(false).Left());
ColourPicker = new XPMColourPickerPanel(this);
m_AUIXPMEditor->AddPane(ColourPicker, wxAuiPaneInfo().Name(_T("ColourPicker")).DefaultPane().Caption(_("Colours")).PinButton().CloseButton(false).Top());
InterfacePanel = new XPMInterfacePanel(this);
m_AUIXPMEditor->AddPane(InterfacePanel, wxAuiPaneInfo().Name(_T("Interface")).DefaultPane().Caption(_("Interface")).PinButton().CloseButton(false).Top());
m_AUIXPMEditor->Update();
MenuItem1 = new wxMenuItem((&PopupMenuCopy), ID_POPUP_COPYTO, _("Copy to File..."), wxEmptyString, wxITEM_NORMAL);
PopupMenuCopy.Append(MenuItem1);
MenuItem2 = new wxMenuItem((&PopupMenuCopy), ID_POPUP_PASTEFROM, _("Paste from File..."), wxEmptyString, wxITEM_NORMAL);
PopupMenuCopy.Append(MenuItem2);
//*)
//to do : register an event handler when AUI colours are updated
//EVT_MENU(idSettingsEnvironment, MainFrame::OnSettingsEnvironment)
//UpdateMinimalSizes(); //is done in UpdateConfiguration()
//UpdateAUIColours(); //is done in UpdateConfiguration()
Thaw();
wxString sFilePath;
sFilePath = ConfigManager::GetExecutableFolder();
sFilePath = sFilePath + _("\\XPMEditor_log.txt");
LogToFile(_("Step 1"), sFilePath);
if (DrawCanvasPanel)
{
DrawCanvas = DrawCanvasPanel->DrawCanvas;
sCursorPos = DrawCanvasPanel->sCursorPos;
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_PAINT,(wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasPaint,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_ERASE_BACKGROUND,(wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasEraseBackground,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_KEY_DOWN,(wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasKeyDown,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_LEFT_DOWN,(wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasLeftDown,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_LEFT_UP,(wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasLeftUp,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_LEFT_DCLICK,(wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasLeftDClick,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_RIGHT_UP,(wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasRightUp,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_MOTION,(wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasMouseMove,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_LEAVE_WINDOW,(wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasMouseLeave,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_SIZE,(wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasResize,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_CONTEXT_MENU, (wxObjectEventFunction)&XPMEditorPanel::OnContextMenu,0,this);
//scroll events
#if _XPM_SCALE_BMP_INPAINT_==0
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_SCROLLWIN_TOP, (wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasScrollEvent,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_SCROLLWIN_BOTTOM, (wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasScrollEvent,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_SCROLLWIN_LINEUP, (wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasScrollEvent,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_SCROLLWIN_LINEDOWN, (wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasScrollEvent,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_SCROLLWIN_PAGEUP, (wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasScrollEvent,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_SCROLLWIN_PAGEDOWN, (wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasScrollEvent,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_SCROLLWIN_THUMBTRACK, (wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasScrollEvent,0,this);
DrawCanvasPanel->DrawCanvas->Connect(wxEVT_SCROLLWIN_THUMBRELEASE, (wxObjectEventFunction)&XPMEditorPanel::OnDrawCanvasScrollEvent,0,this);
#endif
}
else
{
DrawCanvas = NULL;
sCursorPos = NULL;
}
//event handler for popup menu
PopupMenuCopy.Connect(ID_POPUP_COPYTO, wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)&XPMEditorPanel::OnCopyTo,0,this);
PopupMenuCopy.Connect(ID_POPUP_PASTEFROM, wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)&XPMEditorPanel::OnPasteFrom,0,this);
Connect(wxEVT_MENU_OPEN, (wxObjectEventFunction)&XPMEditorPanel::OnOpenPopupMenu,0,this);
Connect(wxEVT_MENU_CLOSE, (wxObjectEventFunction)&XPMEditorPanel::OnClosePopupMenu,0,this);
SetPopupMenu(false);
m_bIsMenuBeingClosed = false;
m_bMenuCommandSelected = false;
//init pointers to panels
if (FoldPanel)
{
ToolPanel = FoldPanel->GetToolPanel();
HelpPanel = FoldPanel->GetHelpPanel();
PropertiesPanel = FoldPanel->GetPropertiesPanel();
ImageManipulationPanel = FoldPanel->GetImageManipulationPanel();
}
else
{
ToolPanel = NULL;
HelpPanel = NULL;
PropertiesPanel = NULL;
ImageManipulationPanel = NULL;
}
#if __WXMSW__
//Mouse Capture lost event
DrawCanvas->Connect(wxEVT_MOUSE_CAPTURE_LOST ,(wxObjectEventFunction)&XPMEditorPanel::OnMouseCaptureLost,0,this);
#endif
//set the parents of the child panels
if (ColourPicker) ColourPicker->SetParentPanel(this);
if (InterfacePanel) InterfacePanel->SetParentPanel(this);
if (FoldPanel) FoldPanel->SetParentPanel(this);
ToggleButtons(-1, false); //Toggle All buttons off.
m_dScale = 1;
bShowGrid = false;
cGridColour = *wxBLACK;
}
/** Debugging function : writes a string to a text file
* The identifer _DEBUG_XPM_EDITOR_PANEL must be defined with a value different from 0
* see at the beginning of this file
* \param sLogText : the string to write in the file.
* a new line will be created
* \param sFilePath: the path of the file
* if the file does not exist, it will be created
*/
void XPMEditorPanel::LogToFile(wxString sLogText, wxString sFilePath)
{
#if _DEBUG_XPM_EDITOR_PANEL != 0
wxTextFile tf(sFilePath);
if (tf.Exists())
{
if (!tf.Open()) return;
}
else
{
if (!tf.Create()) return;
}
tf.AddLine(sLogText);
tf.Write();
tf.Close();
#endif
}
/** Set the new image format to use when saving.
* If the format is not supported, nothing is done.
* When the image format is set to wxBITMAP_TYPE_ANY, then the saving file format
* will be decided based on file extension (*.bmp for bitmap, *.jpg for JPEG, ...)
* If another value is used, then this format will be forced used when saving,
* regardless of the filename
* \param btFormat : one of the saving format supported:
*
* see wxImage / wxBitmapType in wxWidgets doc for more information
*/
void XPMEditorPanel::SetImageFormat(wxBitmapType btFormat)
{
if (IsValidFormat(btFormat))
{
m_ImageFormat = btFormat;
if (PropertiesPanel) PropertiesPanel->SetImageFormat(btFormat);
}
}
/** Indicates if a format is supported or not
* \param btFormat : the format to test
* see wxImage / wxBitmapType in wxWidgets doc for more information
* \return true if supported for saving, false otherwise
* Note that some format are supported only for reading (GIFF for example)
* In this case, the method will return false
*/
bool XPMEditorPanel::IsValidFormat(wxBitmapType btFormat)
{
if (XPM_Plugin()) return(XPM_Plugin()->IsFormatValidForWriting(btFormat));
return(false);
}
/** Autodetect the file format for the image, based on the file extension
* \param sFilename: the full or relative path of the file to test
* \return true on success, false on failure
* on failure, m_ImageFormat will have the value wxBITMAP_TYPE_ANY
*/
bool XPMEditorPanel::SetImageFormatFromFilename(wxString sFilename)
{
if (XPM_Plugin())
{
return(XPM_Plugin()->GetImageFormatFromFileName(sFilename, &m_ImageFormat));
}
else
{
m_ImageFormat = wxBITMAP_TYPE_ANY;
}
return(false);
}
/** Gets the format used to save the image.
* \return the image format. @see SetImageFormat for more information
*/
wxBitmapType XPMEditorPanel::GetImageFormat(void)
{
return(m_ImageFormat);
}
/** Set minimal sizes for the AUI Panes
* Minimal sizes will be set for :
* - the tools panel (FoldPanel)
* - the interface panel (InterfacePanel)
* - the colour picker panel
* Floating size, and minimal sizes are given
*/
void XPMEditorPanel::UpdateMinimalSizes(void)
{
if (m_AUIXPMEditor)
{
wxAuiPaneInfo auiColPickerInfo;
wxAuiPaneInfo auiInterfaceInfo;
wxAuiPaneInfo auiFoldInfo;
auiColPickerInfo = m_AUIXPMEditor->GetPane(ColourPicker);
auiInterfaceInfo = m_AUIXPMEditor->GetPane(InterfacePanel);
auiFoldInfo = m_AUIXPMEditor->GetPane(FoldPanel);
if ((auiColPickerInfo.IsOk()) && (ColourPicker))
{
//Colour Picker
wxSize sMinSize;
wxSize sBestSize;
if (ColourPicker->ColourPicker)
{
sMinSize = ColourPicker->ColourPicker->DoGetMinSize();
sBestSize = ColourPicker->ColourPicker->DoGetBestSize();
}
auiColPickerInfo = auiColPickerInfo.BestSize(sBestSize)
.FloatingSize(sBestSize)
.MinSize(sMinSize);
if (m_AUIXPMEditor->DetachPane(ColourPicker))
{
m_AUIXPMEditor->AddPane(ColourPicker, auiColPickerInfo);
}
}
if ((auiInterfaceInfo.IsOk()) && (InterfacePanel))
{
//Interface Panel
wxSize sMinSize;
wxSize sBestSize;
if (InterfacePanel->GetSizer())
{
sMinSize = InterfacePanel->GetSizer()->GetMinSize();
sBestSize = InterfacePanel->GetBestSize();
}
auiInterfaceInfo = auiInterfaceInfo.BestSize(sBestSize)
.FloatingSize(sBestSize)
.MinSize(sMinSize);
if (m_AUIXPMEditor->DetachPane(InterfacePanel))
{
m_AUIXPMEditor->AddPane(InterfacePanel, auiInterfaceInfo);
}
}
if ((auiFoldInfo.IsOk()) && (FoldPanel))
{
//Tools panel
wxSize sMinSize;
wxSize sAbsMinSize;
wxSize sBestSize;
if (FoldPanel)
{
sMinSize = FoldPanel->DoGetMinSize();
sAbsMinSize = FoldPanel->DoGetAbsoluteMinimalSize();
sBestSize = FoldPanel->DoGetBestSize();
}
/*
Log(_("-----------------------------------------------------------"));
Log(wxString::Format(_("XPMEditorPanel::UpdateMinimalSizes Min Size w=%d h=%d"), sMinSize.GetWidth(), sMinSize.GetHeight()));
Log(wxString::Format(_("XPMEditorPanel::UpdateMinimalSizesBest Size w=%d h=%d"), sBestSize.GetWidth(), sBestSize.GetHeight()));
Log(_("-----------------------------------------------------------"));
*/
auiFoldInfo = auiFoldInfo.BestSize(sBestSize)
.FloatingSize(sMinSize)
.MinSize(sAbsMinSize);
if (m_AUIXPMEditor->DetachPane(FoldPanel))
{
m_AUIXPMEditor->AddPane(FoldPanel, auiFoldInfo);
}
}
m_AUIXPMEditor->Update();
}
}
/** Get the same colours as codeblocks configuration
* Code::Blocks wxAUIManager is fetched, and its colours & metrics are read
* These colours & metrics are then applied to the current wxAUIManager
*/
void XPMEditorPanel::UpdateAUIColours(void)
{
wxAuiManager *auiCodeBlocksManager;
wxAuiDockArt *auiCodeBlocksDockArt;
wxAuiDockArt *auiXPMEditorDockArt;
ConfigManager* cfg = Manager::Get()->GetConfigManager(_T("app"));
//recover wxAUIManager from Code::Blocks main window
auiCodeBlocksManager = wxAuiManager::GetManager(GetParent());
if (!auiCodeBlocksManager) return;
//recover wxAuiDockArt from Code::Blocks main window
auiCodeBlocksDockArt = auiCodeBlocksManager->GetArtProvider();
if (!auiCodeBlocksDockArt) return;
//recover wxAuiDockArt from this panel
if (!m_AUIXPMEditor) return;
auiXPMEditorDockArt = m_AUIXPMEditor->GetArtProvider();
if (!auiXPMEditorDockArt) return;
//copy the colours
auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_ACTIVE_CAPTION_COLOUR, cfg->ReadColour(_T("/environment/aui/active_caption_colour"), auiXPMEditorDockArt->GetColour(wxAUI_DOCKART_ACTIVE_CAPTION_COLOUR)));
auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_ACTIVE_CAPTION_GRADIENT_COLOUR, cfg->ReadColour(_T("/environment/aui/active_caption_gradient_colour"), auiXPMEditorDockArt->GetColour(wxAUI_DOCKART_ACTIVE_CAPTION_GRADIENT_COLOUR)));
auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_ACTIVE_CAPTION_TEXT_COLOUR, cfg->ReadColour(_T("/environment/aui/active_caption_text_colour"), auiXPMEditorDockArt->GetColour(wxAUI_DOCKART_ACTIVE_CAPTION_TEXT_COLOUR)));
auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_INACTIVE_CAPTION_COLOUR, cfg->ReadColour(_T("/environment/aui/inactive_caption_colour"), auiXPMEditorDockArt->GetColour(wxAUI_DOCKART_INACTIVE_CAPTION_COLOUR)));
auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_INACTIVE_CAPTION_GRADIENT_COLOUR, cfg->ReadColour(_T("/environment/aui/inactive_caption_gradient_colour"), auiXPMEditorDockArt->GetColour(wxAUI_DOCKART_INACTIVE_CAPTION_GRADIENT_COLOUR)));
auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_INACTIVE_CAPTION_TEXT_COLOUR, cfg->ReadColour(_T("/environment/aui/inactive_caption_text_colour"), auiXPMEditorDockArt->GetColour(wxAUI_DOCKART_INACTIVE_CAPTION_TEXT_COLOUR)));
auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_BACKGROUND_COLOUR, auiCodeBlocksDockArt->GetColour(wxAUI_DOCKART_BACKGROUND_COLOUR));
auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_SASH_COLOUR, auiCodeBlocksDockArt->GetColour(wxAUI_DOCKART_SASH_COLOUR));
//auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_ACTIVE_CAPTION_COLOUR, auiCodeBlocksDockArt->GetColour(wxAUI_DOCKART_ACTIVE_CAPTION_COLOUR));
//auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_ACTIVE_CAPTION_GRADIENT_COLOUR, auiCodeBlocksDockArt->GetColour(wxAUI_DOCKART_ACTIVE_CAPTION_GRADIENT_COLOUR));
//auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_INACTIVE_CAPTION_GRADIENT_COLOUR, auiCodeBlocksDockArt->GetColour(wxAUI_DOCKART_INACTIVE_CAPTION_GRADIENT_COLOUR));
//auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_ACTIVE_CAPTION_TEXT_COLOUR, auiCodeBlocksDockArt->GetColour(wxAUI_DOCKART_ACTIVE_CAPTION_TEXT_COLOUR));
//auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_INACTIVE_CAPTION_TEXT_COLOUR, auiCodeBlocksDockArt->GetColour(wxAUI_DOCKART_INACTIVE_CAPTION_TEXT_COLOUR));
//auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_INACTIVE_CAPTION_COLOUR, auiCodeBlocksDockArt->GetColour(wxAUI_DOCKART_INACTIVE_CAPTION_COLOUR));
auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_BORDER_COLOUR, auiCodeBlocksDockArt->GetColour(wxAUI_DOCKART_BORDER_COLOUR));
auiXPMEditorDockArt->SetColour(wxAUI_DOCKART_GRIPPER_COLOUR, auiCodeBlocksDockArt->GetColour(wxAUI_DOCKART_GRIPPER_COLOUR));
//copy the font
auiXPMEditorDockArt->SetFont(wxAUI_DOCKART_CAPTION_FONT, auiCodeBlocksDockArt->GetFont(wxAUI_DOCKART_CAPTION_FONT));
auiXPMEditorDockArt->SetFont(wxAUI_DOCKART_GRADIENT_TYPE, auiCodeBlocksDockArt->GetFont(wxAUI_DOCKART_GRADIENT_TYPE));
//copy the metrics
auiXPMEditorDockArt->SetMetric(wxAUI_DOCKART_PANE_BORDER_SIZE, cfg->ReadInt(_T("/environment/aui/border_size"), auiXPMEditorDockArt->GetMetric(wxAUI_DOCKART_PANE_BORDER_SIZE)));
auiXPMEditorDockArt->SetMetric(wxAUI_DOCKART_SASH_SIZE, cfg->ReadInt(_T("/environment/aui/sash_size"), auiXPMEditorDockArt->GetMetric(wxAUI_DOCKART_SASH_SIZE)));
auiXPMEditorDockArt->SetMetric(wxAUI_DOCKART_CAPTION_SIZE, cfg->ReadInt(_T("/environment/aui/caption_size"), auiXPMEditorDockArt->GetMetric(wxAUI_DOCKART_CAPTION_SIZE)));
//auiXPMEditorDockArt->SetMetric(wxAUI_DOCKART_SASH_SIZE, auiCodeBlocksDockArt->GetMetric(wxAUI_DOCKART_SASH_SIZE));
//auiXPMEditorDockArt->SetMetric(wxAUI_DOCKART_CAPTION_SIZE, auiCodeBlocksDockArt->GetMetric(wxAUI_DOCKART_CAPTION_SIZE));
auiXPMEditorDockArt->SetMetric(wxAUI_DOCKART_GRIPPER_SIZE, auiCodeBlocksDockArt->GetMetric(wxAUI_DOCKART_GRIPPER_SIZE));
//auiXPMEditorDockArt->SetMetric(wxAUI_DOCKART_PANE_BORDER_SIZE, auiCodeBlocksDockArt->GetMetric(wxAUI_DOCKART_PANE_BORDER_SIZE));
auiXPMEditorDockArt->SetMetric(wxAUI_DOCKART_PANE_BUTTON_SIZE, auiCodeBlocksDockArt->GetMetric(wxAUI_DOCKART_PANE_BUTTON_SIZE));
//apply the colours & metrics
m_AUIXPMEditor->Update();
}
/** Destructor
*/
XPMEditorPanel::~XPMEditorPanel()
{
//(*Destroy(XPMEditorPanel)
//*)
//selection memory release
ClearSelection();
free(pSelection);
if (m_DragImage) delete m_DragImage;
//free UNDO buffer
if (m_undo_buffer) delete m_undo_buffer;
}
//--------------------- MODIFICATION FLAGS METHODS -----------------------
/** Update the modification flag
* \param bModified : the modification flag
*/
void XPMEditorPanel::SetModified(bool bModified)
{
XPMEditorBase *editor;
editor = (XPMEditorBase *) GetParent();
if (editor) editor->SetModified(bModified);
}
/** Get the modification flag
* \return : the modification flag
*/
bool XPMEditorPanel::GetModified(void)
{
XPMEditorBase *editor;
editor = (XPMEditorBase *) GetParent();
if (editor) return(editor->GetModified());
return(false);
}
//----------------- IMAGE & BITMAP MANAGEMENTS METHODS -------------------
/** Return the scaled internal bitmap.
* The returned pointer to the wxBitmap is NOT a copy: do NOT delete it.
* \return a pointer to the wxBitmap used internally by the editor
* this wxBitmap is used for scaling and all draw modification.
* It is converted as an image before saving, or before any manipulation requiring a wxImage
* Both a wxBitmap and wxImage are stored because it is the only way to work around a wxWidgets bug:
* Scaling (wxDC.SetUserScale()) is innacurate.
*/
wxBitmap* XPMEditorPanel::GetBitmap(void)
{
//return the associated bitmap
return(&m_Bitmap);
}
/** Return an image representing the selection
*/
wxImage XPMEditorPanel::GetImageFromSelection(void)
{
//Return an image representing the selection
if (!HasSelection()) return(wxImage());
//get Selection bounding rect
wxRect rSelection;
GetBoundingRect(&rSelection);
//reduce width & Height by 1 because the rectangle enclose completly the selection
//if not done, the copied image will be 1 pixel to large
int iWidth, iHeight;
iWidth = rSelection.GetWidth();
iHeight = rSelection.GetHeight();
if (iWidth > 1) iWidth--;
if (iHeight > 1) iHeight--;
rSelection.SetWidth(iWidth);
rSelection.SetHeight(iHeight);
//get sub-image from selection bounding rect
wxImage imgCopy(m_Image);
imgCopy = imgCopy.GetSubImage(rSelection);
//create a bitmap mask
wxBitmap bmpMask(rSelection.GetWidth(), rSelection.GetHeight(), 1);
wxMemoryDC memDC;
memDC.SelectObject(bmpMask);
memDC.SetBackground(*wxBLACK_BRUSH);
ClearDC(memDC);
memDC.SetPen(*wxWHITE_PEN);
memDC.SetBrush(*wxWHITE_BRUSH);
//draw the mask
wxPoint *tmp;
tmp = new wxPoint[NbPoints];
if (tmp)
{
int i;
for(i=0;i<NbPoints;i++)
{
tmp[i].x = pSelection[i].x - rSelection.GetLeft() ;
tmp[i].y = pSelection[i].y - rSelection.GetTop();
}
memDC.DrawPolygon(NbPoints, tmp);
delete[] tmp;
}
//apply the mask (convert it to wxImage first)
wxImage imgMask = bmpMask.ConvertToImage();
imgCopy.SetMask(true);
imgCopy.SetMaskFromImage(imgMask,0,0,0);
m_SelectionBitmap = wxBitmap(imgCopy);
return(imgCopy);
}
/** Replace the Selection with the mask colour
*/
void XPMEditorPanel::CutSelection(void)
{
if (!HasSelection()) return;
//get Selection bounding rect
wxRect rSelection;
GetBoundingRect(&rSelection);
//reduce width & Height by 1 because the rectangle enclose completly the selection
//if not done, the copied image will be 1 pixel to large
int iWidth, iHeight;
iWidth = rSelection.GetWidth();
iHeight = rSelection.GetHeight();
if (iWidth > 1) iWidth--;
if (iHeight > 1) iHeight--;
rSelection.SetWidth(iWidth);
rSelection.SetHeight(iHeight);
//create a bitmap mask
wxBitmap bmpMask(rSelection.GetWidth(), rSelection.GetHeight(), -1);
wxMemoryDC memDC;
memDC.SelectObject(bmpMask);
memDC.SetBackground(*wxBLACK_BRUSH);
ClearDC(memDC);
memDC.SetPen(*wxWHITE_PEN);
memDC.SetBrush(*wxWHITE_BRUSH);
//draw the mask
wxPoint *tmp;
tmp = new wxPoint[NbPoints];
if (tmp)
{
int i;
for(i=0;i<NbPoints;i++)
{
tmp[i].x = pSelection[i].x - rSelection.GetLeft() ;
tmp[i].y = pSelection[i].y - rSelection.GetTop();
}
memDC.DrawPolygon(NbPoints, tmp);
delete[] tmp;
}
//replace the white area by the mask colour
wxImage imgMask = bmpMask.ConvertToImage();
imgMask.Replace(255, 255, 255, cMaskColour.Red(), cMaskColour.Green(), cMaskColour.Blue());
//Set a mask colour (Black) for this image
imgMask.SetMaskColour(0,0,0);
PasteImage(imgMask, rSelection.GetLeft(), rSelection.GetTop());
}
/** Move the selection by dx / dy
* no border checks are performed.
* if the selection is beyond the image boundaries (may happen when pasting a bigger image from clipboard,
* or when moving the selection), then the selection image beyond the boundaries is hidden.
* \param dx : the increment by which the selection must be moved horizontally.
Positive value moves the selection to the right
* \param dy : the increment by which the selection must be moved vertically.
Positive value moves the selection to the bottom
*/
void XPMEditorPanel::MoveSelection(int dx, int dy)
{
//Move the selection
if (!HasSelection()) return;
int i;
for(i=0; i<NbPoints; i++)
{
pSelection[i].x = pSelection[i].x + dx;
pSelection[i].y = pSelection[i].y + dy;
/*
//border check
if (pSelection[i].x < 0) pSelection[i].x = 0;
if (pSelection[i].y < 0) pSelection[i].y = 0;
if (pSelection[i].x > m_Image.GetWidth()) pSelection[i].x = m_Image.GetWidth();
if (pSelection[i].y > m_Image.GetHeight()) pSelection[i].y = m_Image.GetHeight();
*/
}
}
/** Return the current associated image
* All changes buffered are flushed first
* \return the wxImage stored.
* This is a copy of the wxImage
*/
wxImage XPMEditorPanel::GetImage(void)
{
//return the associated image
wxImage img(m_Image);
img.SetMaskColour(cMaskColour.Red(), cMaskColour.Green(), cMaskColour.Blue());
//hot spot information
if ((iHotSpotX >= 0) && (iHotSpotX < m_Image.GetWidth()) && (iHotSpotY >= 0) && (iHotSpotY < m_Image.GetHeight()))
{
img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, iHotSpotX);
img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, iHotSpotY);
}
return(img);
}
/** Set the image to be edited
* the previous one is discarded and deleted
* Scale factor is kept.
* \param : a pointer to the new wxImage.
* a copy is created by the method. It is therefore safe to delete it afterwards.
* if NULL, a blank wxImage with default size will be created
*/
void XPMEditorPanel::SetImage(wxImage *img)
{
//set the associated bitmap
if (DrawCanvas)
{
if (img)
{
//create a copy of the bitmap
m_Image = wxImage(*img);
if ((m_Image.HasAlpha()) && (!m_Image.HasMask()))
{
//convert Alpha channel to mask data
//Manager::Get()->GetLogManager()->Log(_("Image has alpha channel and no mask"));
m_Image.ConvertAlphaToMask(128);
}
unsigned char r, g, b;
m_Image.GetOrFindMaskColour(&r, &g, &b);
cMaskColour = wxColour(r, g , b);
ColourPicker->SetTransparentColour(cMaskColour, false);
ColourPicker->Repaint();
if (m_Image.HasMask())
{
//remove the mask
unsigned char r2, g2, b2;
wxColour cTransparent;
cTransparent = ColourPicker->GetTransparentColour();
//r2 = cTransparent.Red();
//g2 = cTransparent.Green();
//b2 = cTransparent.Blue();
//wxMessageBox(wxString::Format(_("Has mask r=%d g=%d b=%d r2=%d g2=%d b2=%d"),r,g,b, r2, g2, b2));
m_Image.SetMask(false);
}
}
else
{
//create a blank image
m_Image = wxImage(iXPMDefaultWidth, iXPMDefaultHeight, true);
cMaskColour = ColourPicker->GetTransparentColour();
}
//check for HotSpot coordinates
if ((m_Image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X)) && (m_Image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y)))
{
iHotSpotX = m_Image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
iHotSpotY = m_Image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
}
wxBitmap bm(m_Image);
SetBitmap(&bm);
}
}
/** recreate the m_Bitmap member from the m_Image member
* The Draw Canvas is not updated. It must be done manually by the caller
* The scrollbars are updated
*/
void XPMEditorPanel::UpdateBitmap(void)
{
//recreate the m_Bitmap member from the m_Image member
wxBitmap bm(m_Image);
SetBitmap(&bm);
}
/** recreate the m_ScaledBitmap member from the m_Bitmap member
* The Draw Canvas is not updated. It must be done manually by the caller
* The scrollbars are updated
*/
void XPMEditorPanel::UpdateScaledBitmap(void)
{
//recreate the m_ScaledBitmap member from the m_Bitmap member
#if _XPM_SCALE_BMP_INPAINT_==0
StretchBitmap(m_Bitmap, m_ScaledBitmap, 1.0, m_dScale);
#endif
}
/** recreate the m_Bitmap member from the m_ScaledBitmap member
* The Draw Canvas is not updated. It must be done manually by the caller
* The scrollbars are updated
*/
void XPMEditorPanel::UpdateUnscaledBitmap(void)
{
//recreate the m_Bitmap member from the m_ScaledBitmap member
#if _XPM_SCALE_BMP_INPAINT_==0
StretchBitmap(m_ScaledBitmap, m_Bitmap, m_dScale, 1.0);
#endif
}
/** Set the unscaled internal bitmap.
* \param bm: a pointer to the wxBitmap to be used internally by the editor
* this wxBitmap is used for scaling and all draw modification.
* The method will create a copy of the wxBitmap passed in parameter
* It is therefore safe to delete it afterwards
* It is converted as an image before saving, or before any manipulation requiring a wxImage
* Both a wxBitmap and wxImage are stored because it is the only way to work around a wxWidgets bug:
* Scaling (wxDC.SetUserScale()) is innacurate.
*/
void XPMEditorPanel::SetBitmap(wxBitmap *bm)
{
//set the associated bitmap
if (DrawCanvas)
{
if (bm)
{
//create a copy of the bitmap
m_Bitmap = *bm;
}
else
{
//create a blank bitmap
m_Bitmap = wxBitmap(iXPMDefaultWidth, iXPMDefaultHeight);
}
//Set the scrollbars and draw area size
sDrawAreaSize = wxSize(m_Bitmap.GetWidth(), m_Bitmap.GetHeight());
if (PropertiesPanel)
{
if (PropertiesPanel->BMPWidth) PropertiesPanel->BMPWidth->SetValue(m_Bitmap.GetWidth());
if (PropertiesPanel->BMPHeight) PropertiesPanel->BMPHeight->SetValue(m_Bitmap.GetHeight());
}
int iFactor;
iFactor = m_dScale * 100;
if (InterfacePanel)
{
if (InterfacePanel->ZoomFactor) InterfacePanel->ZoomFactor->SetValue(wxString::Format(_("%d%%"), iFactor));
}
DoSetScrollBars();
UpdateScaledBitmap();
m_bmDrawBitmap = wxBitmap(m_Bitmap.GetWidth(), m_Bitmap.GetHeight());
Repaint();
}
}
/** recreate the m_Image member from the m_Bitmap member
* The Draw Canvas is not updated. It must be done manually by the caller
* The scrollbars are also not updated
*/
void XPMEditorPanel::UpdateImage(void)
{
//Ensure the Image is up-to-date (buffered user actions are flushed)
//recreate the m_Image member from the m_Bitmap member
wxImage img;
m_Image = m_Bitmap.ConvertToImage();
}
/** return a pointer to the wxScrolledWindow on which the Bitmap is drawn.
\return a pointer to the wxScrolledWindow on which the Bitmap is drawn on success, NULL otherwise
*/
wxScrolledWindow* XPMEditorPanel::GetDrawCanvas(void)
{
//get the DrawCanvas
return(DrawCanvas);
}
//--------------------------- PAINT HANDLERS -----------------------------
/** To avoid redrawing the window background.
* It is taken care of manually in OnDrawCanvasPaint
*/
void XPMEditorPanel::OnDrawCanvasEraseBackground(wxEraseEvent& event)
{
}
/** update the display
*/
void XPMEditorPanel::Repaint(void)
{
if (DrawCanvas)
{
DrawCanvas->Refresh(true, NULL);
DrawCanvas->Update();
}
}
/** This method is used to work around a wxWidgets bug in MSW port
* The bug occurs when scaling a DC (wxDC::SetUserScale).
* There is a cumulative round-off error.
* It can be bad enough to be 4 pixels off for a 500 x 300 bitmap.
* Another work-around is to invert the scaling factor in a memory DC, and blit
* the results in the destination DC (with scale 1).
* This works on Windows, but pose problems on wxGTK
* \param dc : the wxDC for which the option must be set
*/
void XPMEditorPanel::SetDCOptions(wxDC &dc)
{
#if defined(__WXMSW__)
#ifndef __WXWINCE__
//the error comes originally from VIEWPORT_EXTENT constant, defined if src/msw/dc.cpp at line 85
//the original value of 1000 is too low. 10000 is much better
int iViewPortExtent, width, height;
int iDeviceOriginX, iDeviceOriginY, iLogicalOriginX, iLogicalOriginY;
int iSignX, iSignY;
iViewPortExtent = 10000;
//dc.SetAxisOrientation(true, false);
iSignX = 1;
iSignY = 1;
dc.GetDeviceOrigin(&iDeviceOriginX, &iDeviceOriginY);
dc.GetLogicalOrigin(&iLogicalOriginX, &iLogicalOriginY);
width = dc.DeviceToLogicalXRel(iViewPortExtent) * iSignX,
height = dc.DeviceToLogicalYRel(iViewPortExtent) * iSignY;
HDC hdc;
hdc = (HDC) dc.GetHDC();
//this is the code that allows to work around the bug (redefinition of the reference frame
//with a larger size. The round off errors are minimized
::SetMapMode(hdc, MM_ANISOTROPIC);
::SetViewportExtEx(hdc, iViewPortExtent, iViewPortExtent, NULL);
::SetWindowExtEx(hdc, width, height, NULL);
::SetViewportOrgEx(hdc, iDeviceOriginX, iDeviceOriginY, NULL);
::SetWindowOrgEx(hdc, iLogicalOriginX, iLogicalOriginY, NULL);
#endif
#endif
}
/** This is a wrapper around dc.SetUserScale()
* This method is used to work around a wxWidgets bug in MSW port
* The bug occurs when scaling a DC (wxDC::SetUserScale).
* There is a cumulative round-off error.
* It can be bad enough to be 4 pixels off for a 500 x 300 bitmap.
* Another work-around is to invert the scaling factor in a memory DC, and blit
* the results in the destination DC (with scale 1).
* This works on Windows, but pose problems on wxGTK
* \param dc : the wxDC for which the option must be set
* \param dScale_X : the scaling factor in the X direction
* \param dScale_Y : the scaling factor in the Y direction
*/
void XPMEditorPanel::SetUserScale(wxDC &dc, double dScale_X, double dScale_Y)
{
dc.SetUserScale(dScale_X, dScale_Y);
#if defined(__WXMSW__)
#ifndef __WXWINCE__
SetDCOptions(dc);
#endif
#endif
}
/** This is a wrapper around dc.Clear()
* This method is used to work around a wxWidgets bug in MSW port
* The bug occurs when scaling a DC (wxDC::SetUserScale).
* There is a cumulative round-off error.
* It can be bad enough to be 4 pixels off for a 500 x 300 bitmap.
* Another work-around is to invert the scaling factor in a memory DC, and blit