-
Notifications
You must be signed in to change notification settings - Fork 1
/
XPMEditor.cpp
1786 lines (1552 loc) · 72.8 KB
/
XPMEditor.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: XPMEditor.cpp
* Purpose: plugin main entry point - code
* Author: Seb ([email protected])
* Created: 2009-04-23
* Copyright: Seb ()
* License: GPL 3.0
**************************************************************/
#include <sdk.h> // Code::Blocks SDK
#include <cbproject.h>
#include <filegroupsandmasks.h>
#include <manager.h>
#include <configmanager.h>
#include <editormanager.h>
#include <logmanager.h>
#include <projectmanager.h>
#include <configurationpanel.h>
#include <filefilters.h>
#include <cbauibook.h>
#include <wx/aui/auibook.h>
#include <wx/filesys.h>
#include <wx/fs_zip.h>
#include <wx/xrc/xmlres.h>
#include "XPMEditor.h"
#include "XPMColorPicker.h"
#include "imagxpm2.h"
// Register the plugin with Code::Blocks.
// We are using an anonymous namespace so we don't litter the global one.
namespace
{
PluginRegistrant<XPMEditor> regMain(_T("XPMEditor"));
}
const long XPMEditor::idFileNewImage = wxNewId();
const long XPMEditor::idOpenImage = wxNewId();
const long XPMEditor::idwxSmithNewImage = wxNewId();
const long XPMEditor::idOpenImageModuleMenu = wxNewId();
const long XPMEditor::idOpenImageModuleMenu2 = wxNewId();
XPMEditor* XPMEditor::m_Singleton = 0;
/** Constructor
*/
XPMEditor::XPMEditor()
{
m_XPMEditorLogger = NULL;
m_Singleton = NULL;
// Make sure our resources are available.
// In the generated boilerplate code we have no resources but when
// we add some, it will be nice that this code is in place already ;)
if(!Manager::LoadResource(_T("XPMEditor.zip")))
{
NotifyMissingFile(_T("XPMEditor.zip"));
}
ReadConfiguration(true);
}
/** destructor
*/
XPMEditor::~XPMEditor()
{
if (m_Singleton == this)
{
if (DefaultColourArray) delete[] DefaultColourArray;
m_Singleton = NULL;
}
}
/** Test if a file can be opened by the plugin
* \param filename : the path to the file to be tested
* \return true if the file can be opened by the plugin, false otherwise
*/
bool XPMEditor::CanHandleFile(const wxString& filename) const
{
wxString fn;
if (!XPMEditor::Get()) return(false); //plugin not loaded
fn = filename;
fn.MakeUpper();
//special handling for XPM image
if (fn.Right(4) == _(".XPM"))
{
//iOpenXPM==1 : 1: open the XPM in text editor. 2: ask the user. Other values: open the XPM in Image Editor.
if (iOpenXPM == 1) return(false);
if (iOpenXPM == 2)
{
wxString sMsg;
sMsg = _("Do you want to open this file in the Image Editor ?");
sMsg += _("\n");
sMsg += _("If you click NO, then the file will be opened in the text editor");
if (::wxMessageBox(sMsg, _("Question"),wxICON_QUESTION | wxYES_NO) == wxID_NO) return(false);
}
return(true);
}
//see if an handler is available
wxBitmapType bt;
if (GetImageFormatFromFileName(filename, &bt)) return(true);
//try to open it directly
wxImage img;
if (LoadImage(&img, filename, &bt)) return(true);
return(false);
}
/** Open a file in the editor
* \param filename : the path to the file to open
* \return 0 on success, 1 on failure.
*/
int XPMEditor::OpenFile(const wxString& filename)
{
if (!XPM_Plugin()) return(1);
if (XPM_Plugin()->OpenInEditor(filename)) return(0);
return(1);
}
/** return always false
*/
bool XPMEditor::HandlesEverything() const
{
return(false);
}
/** Find the path to the plugin resource files, containing the HTLM help files
* \return the path to the resource file on success
* an empty string on failure
*/
wxString XPMEditor::GetHTMLHelp(void)
{
wxString resourceFile = ConfigManager::LocateDataFile(_T("XPMEditor.zip"), sdDataGlobal | sdDataUser);
if(wxFile::Access(resourceFile, wxFile::read) == false) return(_(""));
return(resourceFile);
}
/** return a pointer to the unique instance of the plugin class.
* static method
* \return a pointer to the unique class instance on success, NULL on failure (plugin not initialized)
*/
XPMEditor* XPMEditor::Get(void)
{
//This method return a pointer to the the unique instance of this class
return(m_Singleton);
}
/** Plugin initialization
*/
void XPMEditor::OnAttach()
{
// do whatever initialization you need for your plugin
// NOTE: after this function, the inherited member variable
// m_IsAttached will be TRUE...
// You should check for it in other functions, because if it
// is FALSE, it means that the application did *not* "load"
// (see: does not need) this plugin...
if (m_Singleton == NULL)
{
const FilesGroupsAndMasks *fm = Manager::Get()->GetProjectManager()->GetFilesGroupsAndMasks();
wxString sMask;
if (fm) sMask = fm->GetFileMasks(0);
wxInitAllImageHandlers();
wxImage::RemoveHandler(wxT("XPM file"));
wxImage::AddHandler(new wxXPMHandler2);
wxBitmap::InitStandardHandlers();
wxFileSystem::AddHandler(new wxZipFSHandler);
//this piece of code works only on Windows yet. Linux crashes for unknown reasons
#ifdef __WXMSW__
AddFileMasksToProjectManager();
#endif
m_Singleton = this;
m_FileNameSelected = _("");
// Create a new log window.
if(LogManager *LogMan = Manager::Get()->GetLogManager())
{
m_XPMEditorLogger = new XPMEditorLogger();
m_LogPageIndex = LogMan->SetLog(m_XPMEditorLogger);
LogMan->Slot(m_LogPageIndex).title = wxT("XPMEditor");
CodeBlocksLogEvent evtAdd(cbEVT_ADD_LOG_WINDOW, m_XPMEditorLogger, LogMan->Slot(m_LogPageIndex).title);
Manager::Get()->ProcessEvent(evtAdd);
}
}
}
/** Plugin release
*/
void XPMEditor::OnRelease(bool appShutDown)
{
// do de-initialization for your plugin
// if appShutDown is true, the plugin is unloaded because Code::Blocks is being shut down,
// which means you must not use any of the SDK Managers
// NOTE: after this function, the inherited member variable
// m_IsAttached will be FALSE...
if ( !appShutDown )
{
CloseMyEditors();
}
// Remove the log window.
if(Manager::Get()->GetLogManager())
{
if(m_XPMEditorLogger)
{
CodeBlocksLogEvent evt(cbEVT_REMOVE_LOG_WINDOW, m_XPMEditorLogger);
Manager::Get()->ProcessEvent(evt);
}
}
}
/** This method adds Filemasks, such as "*.bmp" to the File Open or File Save dialog
*/
void XPMEditor::AddFileMasksToFileOpenSaveDialog(wxArrayString sFileMasks)
{
wxString sImageMasks;
wxString sMask;
size_t i;
//sImageMasks = wxImage::GetImageExtWildcard();
sImageMasks = _("");
for(i=0;i<sFileMasks.GetCount();i++)
{
sMask = sFileMasks.Item(i);
if (sImageMasks.Len() > 0) sImageMasks += _(",");
sImageMasks += sMask;
}
FileFilters::Add(_("Image Files"), sImageMasks);
}
/** This method is called by Code::Blocks core modules (EditorManager,
* ProjectManager etc) and is used by the plugin to add any menu
* items it needs in the module's popup menu. For example, when
* the user right-clicks on a project file in the project tree,
* ProjectManager prepares a popup menu to display with context
* sensitive options for that file. Before it displays this popup
* menu, it asks all attached plugins (by asking PluginManager to call
* this method), if they need to add any entries
* in that menu. This method is called.\n
* If the plugin does not need to add items in the menu,
* just do nothing ;)
* \param type the module that's preparing a popup menu
* \param menu pointer to the popup menu
* \param data pointer to FileTreeData object (to access/modify the file tree)
*/
void XPMEditor::BuildModuleMenu(const ModuleType type, wxMenu* menu, const FileTreeData* data)
{
if ( !menu || !IsAttached() ) return;
wxMenu* menu2;
switch ( type )
{
case mtProjectManager:
if ( data && data->GetKind()==FileTreeData::ftdkFile )
{
wxMenuItem* child = menu->FindItem( menu->FindItem( _("Open with") ) );
if ( child && child->IsSubMenu() )
{
menu2 = child->GetSubMenu();
if (menu2) menu = menu2;
}
menu->AppendSeparator();
menu->Append(idOpenImageModuleMenu, _( "XPM Editor" ), _( "Open this file in image editor" ));
Connect(idOpenImageModuleMenu,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&XPMEditor::OnOpenFromProjectManager);
}
break;
case mtFileExplorer:
if(data && data->GetKind()==FileTreeData::ftdkFile) //right clicked on folder in file explorer
{
wxFileName f(data->GetFolder());
m_FileNameSelected = f.GetFullPath();
wxMenuItem* child = menu->FindItem( menu->FindItem( _("Open with") ) );
if ( child && child->IsSubMenu() )
{
menu2 = child->GetSubMenu();
if (menu2) menu = menu2;
}
menu->Append( idOpenImageModuleMenu2, _( "Open with XPM Editor" ), _( "Open this file in image editor" ) );
Connect(idOpenImageModuleMenu2,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&XPMEditor::OnOpenFromFileManager);
}
break;
default:
break;
}
}
/** This method is called by Code::Blocks and is used by the plugin
* to add any menu items it needs on Code::Blocks's menu bar.\n
* It is a pure virtual method that needs to be implemented by all
* plugins. If the plugin does not need to add items on the menu,
* just do nothing ;)
* \param menuBar the wxMenuBar to create items in
*/
void XPMEditor::BuildMenu(wxMenuBar* menuBar)
{
int idFileNewProject = XRCID("idFileNewProject");
int idFileOpen = XRCID("idFileOpen");
wxMenu* popup;
if (!menuBar) return;
//create new item in sub menu "New"
menuBar->FindItem(idFileNewProject, &popup);
if (popup)
{
popup->Append(idFileNewImage, _("New Image"), _("Create a new blank image or icon"));
Connect(idFileNewImage,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&XPMEditor::OnNewImage);
}
//create a new item in menu "File"
menuBar->FindItem(idFileOpen, &popup);
if (popup)
{
//we need to insert a menu item, so we need to know its position.
//there are no other way than iterating the whole menu to find the insertion position
size_t pos;
int id;
wxMenuItemList& list = popup->GetMenuItems();
pos = 0;
for ( wxMenuItemList::iterator i = list.begin(); i != list.end(); ++i)
{
wxMenuItem* item = *i;
if (item)
{
id = item->GetId();
if (id == idFileOpen)
{
popup->Insert( pos+1, idOpenImage, _("Open with XPMEditor"), _("Open file using the image editor") );
Connect(idOpenImage,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&XPMEditor::OnOpenImage);
}
}
pos++;
}
}
//create a new menu in "wxSmith"
int idwxSmith;
idwxSmith = menuBar->FindMenu(_("&wxSmith"));
if (idwxSmith != wxNOT_FOUND)
{
//append a separator and a menu item
wxMenu *mSmithMenu;
mSmithMenu = menuBar->GetMenu(idwxSmith);
if (mSmithMenu)
{
mSmithMenu->AppendSeparator();
mSmithMenu->Append(idwxSmithNewImage, _("Add Image"), _("Add a new image or icon to the project"));
Connect(idwxSmithNewImage,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&XPMEditor::OnNewProjectImage);
}
}
}
/** This method adds Filemasks, such as "*.bmp" to the project manager
* This allows the display of all images into 1 virtual folder "Images"
*/
void XPMEditor::AddFileMasksToProjectManager(void)
{
ProjectManager *pm;
pm = Manager::Get()->GetProjectManager();
wxArrayString sFileMasks;
//get the list of file extension supported by wxImage
wxList list = wxImage::GetHandlers();
sFileMasks.Clear();
for(wxList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext())
{
wxImageHandler *handler=(wxImageHandler*) node->GetData();
wxString sExtension = _("*.") + handler->GetExtension();
sExtension.MakeLower();
if ((sFileMasks.Index(sExtension, false) == wxNOT_FOUND) && (handler->GetExtension().Len() > 0)) sFileMasks.Add(sExtension);
}
//get the list of file extension supported by wxBitmap
wxBitmapHandler *handler;
wxString sExtension;
wxBitmapType bt[32];
int i;
bt[0] = wxBITMAP_TYPE_BMP;
bt[1] = wxBITMAP_TYPE_BMP_RESOURCE;
bt[2] = wxBITMAP_TYPE_RESOURCE;
bt[3] = wxBITMAP_TYPE_ICO;
bt[4] = wxBITMAP_TYPE_ICO_RESOURCE;
bt[5] = wxBITMAP_TYPE_CUR;
bt[6] = wxBITMAP_TYPE_CUR_RESOURCE;
bt[7] = wxBITMAP_TYPE_XBM;
bt[8] = wxBITMAP_TYPE_XBM_DATA;
bt[9] = wxBITMAP_TYPE_XPM;
bt[10] = wxBITMAP_TYPE_XPM_DATA;
bt[11] = wxBITMAP_TYPE_TIF;
bt[12] = wxBITMAP_TYPE_TIF_RESOURCE;
bt[13] = wxBITMAP_TYPE_GIF;
bt[14] = wxBITMAP_TYPE_GIF_RESOURCE;
bt[15] = wxBITMAP_TYPE_PNG;
bt[16] = wxBITMAP_TYPE_PNG_RESOURCE;
bt[17] = wxBITMAP_TYPE_JPEG;
bt[18] = wxBITMAP_TYPE_JPEG_RESOURCE;
bt[19] = wxBITMAP_TYPE_PNM;
bt[20] = wxBITMAP_TYPE_PNM_RESOURCE;
bt[21] = wxBITMAP_TYPE_PCX;
bt[22] = wxBITMAP_TYPE_PCX_RESOURCE;
bt[23] = wxBITMAP_TYPE_PICT;
bt[24] = wxBITMAP_TYPE_PICT_RESOURCE;
bt[25] = wxBITMAP_TYPE_ICON;
bt[26] = wxBITMAP_TYPE_ICON_RESOURCE;
bt[27] = wxBITMAP_TYPE_ANI;
bt[28] = wxBITMAP_TYPE_IFF;
bt[29] = wxBITMAP_TYPE_TGA;
bt[30] = wxBITMAP_TYPE_MACCURSOR;
bt[31] = wxBITMAP_TYPE_MACCURSOR_RESOURCE;
for(i=0;i<32;i++)
{
handler = (wxBitmapHandler*) wxBitmap::FindHandler(bt[i]);
if (handler)
{
sExtension = _("*.") + handler->GetExtension();
if ((sFileMasks.Index(sExtension, false) == wxNOT_FOUND) && (handler->GetExtension().Len() > 0)) sFileMasks.Add(sExtension);
}
}
//add additionnal exensions
if (sFileMasks.Index(_("*.dib"), false) == wxNOT_FOUND) sFileMasks.Add(_("*.dib"));
if (sFileMasks.Index(_("*.xbm"), false) == wxNOT_FOUND) sFileMasks.Add(_("*.xbm"));
if (sFileMasks.Index(_("*.iff"), false) == wxNOT_FOUND) sFileMasks.Add(_("*.iff"));
if (sFileMasks.Index(_("*.pic"), false) == wxNOT_FOUND) sFileMasks.Add(_("*.pic"));
if (sFileMasks.Index(_("*.pict"), false) == wxNOT_FOUND) sFileMasks.Add(_("*.pict"));
if (sFileMasks.Index(_("*.jpe"), false) == wxNOT_FOUND) sFileMasks.Add(_("*.jpe"));
if (sFileMasks.Index(_("*.icon"), false) == wxNOT_FOUND) sFileMasks.Add(_("*.icon"));
if (sFileMasks.Index(_("*.tiff"), false) == wxNOT_FOUND) sFileMasks.Add(_("*.tiff"));
if (sFileMasks.Index(_("*.jpeg"), false) == wxNOT_FOUND) sFileMasks.Add(_("*.jpeg"));
if (sFileMasks.Index(_("*.jfif"), false) == wxNOT_FOUND) sFileMasks.Add(_("*.jfif"));
//add these file masks to the project manager
if (pm)
{
FilesGroupsAndMasks *fm;
fm = (FilesGroupsAndMasks *) pm->GetFilesGroupsAndMasks();
if (fm)
{
//test if the filemasks are already associated
unsigned int i, iMax, iMatch;
size_t j, jMax;
bool bMatch;
wxString sMask;
bMatch = false;
jMax = sFileMasks.GetCount();
iMax = fm->GetGroupsCount();
for(j=0;j<jMax;j++) //loop over all the possible image files extensions
{
sMask = sFileMasks.Item(j);
for(i=0;i<iMax;i++) //loop over the groups managed by the project manager
{
if (fm->MatchesMask(sMask, i))
{
bMatch= true;
iMatch = i;
}
}
}
if (!bMatch)
{
//create a new group
wxString sName;
sName = _("Images");
while (IsGroupNameExisting(sName, fm)) sName = sName + _("_");
fm->AddGroup(sName);
iMatch = fm->GetGroupsCount() - 1;
}
//add all the filemasks to the group
sMask = fm->GetFileMasks(iMatch);
for(j=0;j<jMax;j++) //loop over all the possible image files extensions
{
if (!(fm->MatchesMask(sFileMasks.Item(j), i))) //to avoid having twice the same mask
{
if (sMask.Len() > 0) sMask = sMask + _(";");
sMask = sMask + sFileMasks.Item(j);
}
}
fm->SetFileMasks(iMatch, sMask);
fm->Save();
pm->GetUI().RebuildTree();
}
}
AddFileMasksToFileOpenSaveDialog(sFileMasks);
}
/** Test if a group name is already existing in the Project Manager
* \param sName : the group name to test. Example: _("Images")
* \param fm : the FilesGroupAndMasks to search
* \return true if the Name already exist, false otherwise
*/
bool XPMEditor::IsGroupNameExisting(wxString sName, const FilesGroupsAndMasks *fm)
{
if (!fm) return(false);
unsigned int i, iMax;
wxString sName2;
iMax = fm->GetGroupsCount();
for(i=0;i<iMax;i++) //loop over the groups managed by the project manager
{
sName2 = fm->GetGroupName(i);
if (sName == sName2) return(true);
}
return(false);
}
/** Load an image from a file and open it in a new Image Editor
* If the file is already opened in an editor, then activate this editor
* \param FileName : the full path to the file to open
* \return true on success, false on failure
*/
bool XPMEditor::OpenInEditor(wxString FileName)
{
//This method opens an image file in the Image editor
if (Manager::Get()->GetEditorManager()->IsOpen(FileName))
{
//file already opened - activate the editor
if (Manager::Get()->GetEditorManager()->GetEditor(FileName))
Manager::Get()->GetEditorManager()->GetEditor(FileName)->Activate();
return(true);
}
else
{
//file not opened - create a new editor
XPMEditorBase *NewEditor;
wxString title = wxFileName(FileName).GetFullName();
//get the bitmap type and load it
wxBitmapType bt;
wxImage img;
if (!(LoadImage(&img, FileName, &bt))) return(false);
NewEditor = new XPMEditorBase(Manager::Get()->GetEditorManager()->GetNotebook(),
title,
&img,
FileName,
wxBITMAP_TYPE_ANY //and not bt: this ensure automatic format detection using file extension. Default behaviour
);
if (NewEditor)
{
ProjectFile *pf;
pf = FindProjectFile(FileName);
if (pf)
{
NewEditor->SetTitle(pf->relativeToCommonTopLevelPath);
NewEditor->SetProjectFile(pf);
}
Manager::Get()->GetEditorManager()->SetActiveEditor(NewEditor);
return(true);
}
}
return(false);
}
/** This method will load an image from a file
* It will use wxImage or wxBitmap, depending on the format
* \param img [out] : a pointer to the wxImage loaded
* \param sFileName [in] : the full path to the image to load
* \param bt [out] : a pointer to the file format detected
* \return : true on success, false on failure
* on failure, img is not modified, and bt = wxBITMAP_TYPE_INVALID
*/
bool XPMEditor::LoadImage(wxImage *img, wxString sFileName, wxBitmapType *bt) const
{
//load an image
bool bRecognized;
wxFileName fn(sFileName);
wxString sExt;
wxBitmapType bt2;
sExt = fn.GetExt();
//get the file format
bRecognized = GetImageFormatFromFileName(sFileName, bt);
if (bRecognized)
{
//try the simplest case
//Manager::Get()->GetLogManager()->Log(wxString::Format(_("Image format %d"), *bt));
if (img->LoadFile(sFileName, *bt)) return(true);
//try with auto-detection
bt2 = wxBITMAP_TYPE_ANY;
if (img->LoadFile(sFileName, bt2))
{
*bt = bt2;
return(true);
}
}
else
{
//try with auto-detection
bt2 = wxBITMAP_TYPE_ANY;
if (img->LoadFile(sFileName, bt2))
{
*bt = bt2;
return(true);
}
}
//try using a bitmap
wxBitmap bmp;
if (bmp.LoadFile(sFileName, *bt))
{
if (bmp.IsOk())
{
wxImage img2;
img2 = bmp.ConvertToImage();
if (img2.IsOk())
{
*img = img2;
return(true);
}
}
}
if (bmp.LoadFile(sFileName, wxBITMAP_TYPE_ANY))
{
if (bmp.IsOk())
{
wxImage img2;
img2 = bmp.ConvertToImage();
if (img2.IsOk())
{
*bt = wxBITMAP_TYPE_ANY;
*img = img2;
return(true);
}
}
}
//try using an icon
wxIcon ico;
if (ico.LoadFile(sFileName, *bt))
{
if (ico.IsOk())
{
wxImage img2;
wxBitmap bmp;
bmp.CopyFromIcon(ico);
if (bmp.IsOk())
{
img2 = bmp.ConvertToImage();
if (img2.IsOk())
{
*img = img2;
return(true);
}
}
}
}
if (ico.LoadFile(sFileName, wxBITMAP_TYPE_ANY))
{
if (ico.IsOk())
{
wxImage img2;
wxBitmap bmp;
bmp.CopyFromIcon(ico);
if (bmp.IsOk())
{
img2 = bmp.ConvertToImage();
if (img2.IsOk())
{
*img = img2;
return(true);
}
}
}
}
return(false);
}
/** This method will save an image to a file
* It will use wxImage or wxBitmap, depending on the format
* \param img [in] : a pointer to the wxImage to save
* \param sFileName [in/out] : the full path to the image to save
* \param bt [int] : the file format detected
* \param editor: a pointer to the editor for which the image must be saved
* Can be NULL (in which case, no notifications are sent, and the modification flag is not reseted)
* \return : true on success, false on failure
* on failure, img is not modified, and bt = wxBITMAP_TYPE_INVALID
*/
bool XPMEditor::SaveImage(wxImage *img, wxString sFileName, wxBitmapType bt, XPMEditorBase *Editor)
{
//get the file format to use for saving (bmp, jpeg, png, ...)
bool bRecognized;
wxBitmapType bt2;
if (!img) return(false);
bt2 = bt;
bRecognized = false;
if (bt2 == wxBITMAP_TYPE_ANY)
{
//get fileformat requested, based on file extension
if (XPM_Plugin())
{
XPM_Plugin()->GetImageFormatFromFileName(sFileName, &bt2);
if (XPM_Plugin()->IsFormatValidForWriting(bt2)) bRecognized = true;
}
}
else
{
//the fileformat is requested by the user specifically
if (XPM_Plugin())
{
if (XPM_Plugin()->IsFormatValidForWriting(bt2)) bRecognized = true;
}
}
if (!bRecognized)
{
wxString sMsg;
sMsg = _("The file format is not supported on this platform, or is not recognized. Try one of this one:");
sMsg += _(" *.bmp - bitmap format\n");
sMsg += _(" *.dib - bitmap format\n");
sMsg += _(" *.xpm - pixmap XPM format\n");
sMsg += _(" *.xbm - bitmap XBM format\n");
sMsg += _(" *.png - bitmap PNG format\n");
sMsg += _(" *.jpg, *.jpeg, *.jfif - bitmap JPEG format\n");
sMsg += _(" *.tif, *.tiff - bitmap TIFF format\n");
sMsg += _(" *.pnm - bitmap PNM format\n");
sMsg += _(" *.pcx - PCX format\n");
sMsg += _(" *.pict - PICT format\n");
sMsg += _(" *.icon - icon format\n");
sMsg += _(" *.ico - icon format - Windows only\n");
sMsg += _(" *.cur - cursor format - Windows only\n");
sMsg += _(" *.ani - animated cursor format - Windows only\n");
::wxMessageBox(sMsg, _("File saving error"), wxOK | wxICON_ERROR);
return(false);
}
//check if hotspot is present
if ((img->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) > 0) &&
(img->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) > 0) &&
(bt2 != wxBITMAP_TYPE_CUR)
)
{
//hot spot not supported in this format
wxString sMsg;
sMsg = _("There is a Hot Spot in this image - however, the selected format does not support ");
sMsg += _("Hot Spot information. The information will be lost.");
sMsg += _("The following formats support Hot Spot:\n");
sMsg += _("*.cur - cursor format - Windows only\n");
sMsg += _("Do you want to continue ?");
if (::wxMessageBox(sMsg, _("File saving error"), wxYES_NO | wxICON_QUESTION) == wxNO) return(false);
}
//save the file
/// \todo : the NotifyPlugins provokes a crash on save. Understand why
//if (Editor) Editor->NotifyPlugins(cbEVT_EDITOR_BEFORE_SAVE);
img->SaveFile(sFileName, bt2);
if (Editor) Editor->NotifyPlugins(cbEVT_EDITOR_SAVE);
//flags, quitting
if (Editor) Editor->SetModified(false);
return(true);
}
/** Return a list of file saving format supported
* \param array : a wxString Array containing the results
* The array will be cleared : all previous contents will be lost
* The results will be in the form:
* "Bitmap (*.bmp)"
* one format per array item
*/
void XPMEditor::GetFileSavingFormat(wxArrayString &array)
{
array.Clear();
array.Add(GetFormatString(wxBITMAP_TYPE_ANY));
if (IsFormatValidForWriting(wxBITMAP_TYPE_BMP) || IsFormatValidForWriting(wxBITMAP_TYPE_BMP_RESOURCE)) array.Add(GetFormatString(wxBITMAP_TYPE_BMP));
if (IsFormatValidForWriting(wxBITMAP_TYPE_CUR) || IsFormatValidForWriting(wxBITMAP_TYPE_CUR_RESOURCE) ||
IsFormatValidForWriting(wxBITMAP_TYPE_MACCURSOR) || IsFormatValidForWriting(wxBITMAP_TYPE_MACCURSOR_RESOURCE)
) array.Add(GetFormatString(wxBITMAP_TYPE_CUR));
if (IsFormatValidForWriting(wxBITMAP_TYPE_ICON) || IsFormatValidForWriting(wxBITMAP_TYPE_ICON_RESOURCE) ||
IsFormatValidForWriting(wxBITMAP_TYPE_ICO) || IsFormatValidForWriting(wxBITMAP_TYPE_ICO_RESOURCE)
) array.Add(GetFormatString(wxBITMAP_TYPE_ICO));
if (IsFormatValidForWriting(wxBITMAP_TYPE_JPEG) || IsFormatValidForWriting(wxBITMAP_TYPE_JPEG_RESOURCE)) array.Add(GetFormatString(wxBITMAP_TYPE_JPEG));
if (IsFormatValidForWriting(wxBITMAP_TYPE_PICT) || IsFormatValidForWriting(wxBITMAP_TYPE_PICT_RESOURCE)) array.Add(GetFormatString(wxBITMAP_TYPE_PICT));
if (IsFormatValidForWriting(wxBITMAP_TYPE_PCX) || IsFormatValidForWriting(wxBITMAP_TYPE_PCX_RESOURCE)) array.Add(GetFormatString(wxBITMAP_TYPE_PCX));
if (IsFormatValidForWriting(wxBITMAP_TYPE_PNG) || IsFormatValidForWriting(wxBITMAP_TYPE_PNG_RESOURCE)) array.Add(GetFormatString(wxBITMAP_TYPE_PNG));
if (IsFormatValidForWriting(wxBITMAP_TYPE_PNM) || IsFormatValidForWriting(wxBITMAP_TYPE_PNM_RESOURCE)) array.Add(GetFormatString(wxBITMAP_TYPE_PNM));
if (IsFormatValidForWriting(wxBITMAP_TYPE_TIF) || IsFormatValidForWriting(wxBITMAP_TYPE_TIF_RESOURCE)) array.Add(GetFormatString(wxBITMAP_TYPE_TIF));
if (IsFormatValidForWriting(wxBITMAP_TYPE_XPM) || IsFormatValidForWriting(wxBITMAP_TYPE_XPM_DATA)) array.Add(GetFormatString(wxBITMAP_TYPE_XPM));
if (IsFormatValidForWriting(wxBITMAP_TYPE_XBM) || IsFormatValidForWriting(wxBITMAP_TYPE_XBM_DATA)) array.Add(GetFormatString(wxBITMAP_TYPE_XBM));
}
/** Return the string associated to the file format (ex: "bitmap (*.bmp)
* \param bt : the wxBitmapType to test
* \return : the string associated
*/
wxString XPMEditor::GetFormatString(wxBitmapType bt)
{
wxString sResult;
sResult = _("Automatic (*.*)");
switch(bt)
{
case wxBITMAP_TYPE_XBM :
case wxBITMAP_TYPE_XBM_DATA : sResult = _("X bitmap (*.xbm)");
break;
case wxBITMAP_TYPE_BMP :
case wxBITMAP_TYPE_BMP_RESOURCE :
//case wxBITMAP_TYPE_RESOURCE :
sResult = _("bitmaps (*.bmp)");
break;
//png format
case wxBITMAP_TYPE_PNG :
case wxBITMAP_TYPE_PNG_RESOURCE :
sResult = _("png (*.png)");
break;
//jpeg format
case wxBITMAP_TYPE_JPEG :
case wxBITMAP_TYPE_JPEG_RESOURCE :
//sResult = _("jpeg (*.jpg, *.jpeg, *.jpe)");
sResult = _("jpeg (*.jpg,...)");
break;
//PCX format
case wxBITMAP_TYPE_PCX :
case wxBITMAP_TYPE_PCX_RESOURCE :
sResult = _("pcx (*.pcx)");
break;
//PNM format
case wxBITMAP_TYPE_PNM :
case wxBITMAP_TYPE_PNM_RESOURCE :
sResult = _("pnm (*.pnm)");
break;
//TIFF format
case wxBITMAP_TYPE_TIF :
case wxBITMAP_TYPE_TIF_RESOURCE :
sResult = _("Tiff (*.tif, *.tiff)");
break;
case wxBITMAP_TYPE_ICO :
case wxBITMAP_TYPE_ICO_RESOURCE :
sResult = _("Icon (*.ico)");
break;
case wxBITMAP_TYPE_ICON :
case wxBITMAP_TYPE_ICON_RESOURCE :
sResult = _("Icon (*.icon)");
break;
//Cursor format
//Mac Cursor Revert to CUR as default
case wxBITMAP_TYPE_MACCURSOR :
case wxBITMAP_TYPE_MACCURSOR_RESOURCE :
case wxBITMAP_TYPE_CUR :
case wxBITMAP_TYPE_CUR_RESOURCE :
sResult = _("Cursor (*.cur)");
break;
//XPM format - saving supported
case wxBITMAP_TYPE_XPM :
case wxBITMAP_TYPE_XPM_DATA :
sResult = _("XPM (*.xpm)");
break;
//GIFF, IFF, TGA, ANI, PICT formats - saving NOT supported - revert to PNG as default
case wxBITMAP_TYPE_ANI : sResult = _("Animated cursor (*.ani)");
break;
case wxBITMAP_TYPE_IFF : sResult = _("Iff (*.iff)");
break;
case wxBITMAP_TYPE_TGA : sResult = _("tga (*.tga)");
break;
case wxBITMAP_TYPE_GIF :
case wxBITMAP_TYPE_GIF_RESOURCE :
sResult = _("Giff (*.gif, *.giff)");
break;
case wxBITMAP_TYPE_PICT :
case wxBITMAP_TYPE_PICT_RESOURCE :
sResult = _("Picture (*.pic, *.pict)");
break;
//use autodetection based on filename
case wxBITMAP_TYPE_INVALID :
case wxBITMAP_TYPE_ANY :
default : sResult = _("Automatic (*.*)");
break;
}
return(sResult);
}
/** Return the wxBitmapFormat associated to the file format (ex: "bitmap (*.bmp))"
* \param sFormat: the format to test. It is one of the format returned by GetFormatString()
* \return the wxBitmapFormat associated
*/
wxBitmapType XPMEditor::GetFormatBitmap(wxString sFormat)
{
if (sFormat == _("Automatic (*.*)")) return(wxBITMAP_TYPE_ANY);
if (sFormat == _("Picture (*.pic, *.pict)")) return(wxBITMAP_TYPE_PICT);
if (sFormat == _("Giff (*.gif, *.giff)")) return(wxBITMAP_TYPE_GIF);
if (sFormat == _("tga (*.tga)")) return(wxBITMAP_TYPE_TGA);
if (sFormat == _("Iff (*.iff)")) return(wxBITMAP_TYPE_IFF);
if (sFormat == _("Animated cursor (*.ani)")) return(wxBITMAP_TYPE_ANI);
if (sFormat == _("XPM (*.xpm)")) return(wxBITMAP_TYPE_XPM);
if (sFormat == _("Cursor (*.cur)")) return(wxBITMAP_TYPE_CUR);
if (sFormat == _("Icon (*.icon)")) return(wxBITMAP_TYPE_ICON);
if (sFormat == _("Icon (*.ico)")) return(wxBITMAP_TYPE_ICO);
if (sFormat == _("Tiff (*.tif, *.tiff)")) return(wxBITMAP_TYPE_TIF);
if (sFormat == _("pnm (*.pnm)")) return(wxBITMAP_TYPE_PNM);
if (sFormat == _("pcx (*.pcx)")) return(wxBITMAP_TYPE_PCX);
if (sFormat == _("jpeg (*.jpg,...)")) return(wxBITMAP_TYPE_JPEG);
if (sFormat == _("png (*.png)")) return(wxBITMAP_TYPE_PNG);
if (sFormat == _("bitmaps (*.bmp)")) return(wxBITMAP_TYPE_BMP);
if (sFormat == _("X bitmap (*.xbm)")) return(wxBITMAP_TYPE_XBM);
return(wxBITMAP_TYPE_ANY);
}
/** This method checks if the given saving file format is available.
* It returns a compatible file saving format available on the platform
* \param btFormat : the wxBitmapType to test
* \return if btFormat is available on saving, then the return value is btFormat
* otherwise, a format as close as possible is given
*/
wxBitmapType XPMEditor::GetCompatibleSavingFormat(wxBitmapType btFormat)
{
//simplest case: the format is recognized
if (IsFormatValidForWriting(btFormat)) return(btFormat);
//find an available format
switch(btFormat)
{
//bitmap format - saving supported
case wxBITMAP_TYPE_XBM :
case wxBITMAP_TYPE_XBM_DATA :
case wxBITMAP_TYPE_BMP :
case wxBITMAP_TYPE_BMP_RESOURCE :
//case wxBITMAP_TYPE_RESOURCE :
if (IsFormatValidForWriting(wxBITMAP_TYPE_XBM)) return(wxBITMAP_TYPE_XBM);