~efargaspro/+junk/codeblocks-16.01-release

« back to all changes in this revision

Viewing changes to src/plugins/contrib/source_exporter/exporter.cpp

  • Committer: damienlmoore at gmail
  • Date: 2016-02-02 02:43:22 UTC
  • Revision ID: damienlmoore@gmail.com-20160202024322-yql5qmtbwdyamdwd
Code::BlocksĀ 16.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************
 
2
 * Name:      exporter.cpp
 
3
 * Purpose:   Code::Blocks plugin
 
4
 * Author:    Ceniza<ceniza@gda.utp.edu.co>
 
5
 * Copyright: (c) Ceniza
 
6
 * License:   GPL
 
7
 **************************************************************/
 
8
 
 
9
#include <wx/frame.h> // GetMenuBar
 
10
#include "exporter.h"
 
11
#include <configmanager.h>
 
12
#include <manager.h>
 
13
#include <cbeditor.h>
 
14
#include <editormanager.h>
 
15
#include <editorcolourset.h>
 
16
#include <logmanager.h>
 
17
#include <cbexception.h>
 
18
#include "HTMLExporter.h"
 
19
#include "RTFExporter.h"
 
20
#include "ODTExporter.h"
 
21
#include "PDFExporter.h"
 
22
#include "cbstyledtextctrl.h"
 
23
 
 
24
static int idFileExport = wxNewId();
 
25
static int idFileExportHTML = wxNewId();
 
26
static int idFileExportRTF = wxNewId();
 
27
static int idFileExportODT = wxNewId();
 
28
static int idFileExportPDF = wxNewId();
 
29
 
 
30
// Register the plugin
 
31
namespace
 
32
{
 
33
    PluginRegistrant<Exporter> reg(_T("Exporter"));
 
34
};
 
35
 
 
36
BEGIN_EVENT_TABLE(Exporter, cbPlugin)
 
37
  EVT_MENU(idFileExportHTML, Exporter::OnExportHTML)
 
38
  EVT_MENU(idFileExportRTF, Exporter::OnExportRTF)
 
39
  EVT_MENU(idFileExportODT, Exporter::OnExportODT)
 
40
  EVT_MENU(idFileExportPDF, Exporter::OnExportPDF)
 
41
  EVT_UPDATE_UI(idFileExportHTML, Exporter::OnUpdateUI)
 
42
  EVT_UPDATE_UI(idFileExportRTF, Exporter::OnUpdateUI)
 
43
  EVT_UPDATE_UI(idFileExportODT, Exporter::OnUpdateUI)
 
44
END_EVENT_TABLE()
 
45
 
 
46
Exporter::Exporter()
 
47
{
 
48
  //ctor
 
49
}
 
50
 
 
51
Exporter::~Exporter()
 
52
{
 
53
  //dtor
 
54
}
 
55
 
 
56
void Exporter::OnAttach()
 
57
{
 
58
  // do whatever initialization you need for your plugin
 
59
  // NOTE: after this function, the inherited member variable
 
60
  // IsAttached() will be TRUE...
 
61
  // You should check for it in other functions, because if it
 
62
  // is FALSE, it means that the application did *not* "load"
 
63
  // (see: does not need) this plugin...
 
64
}
 
65
 
 
66
void Exporter::OnRelease(bool /*appShutDown*/)
 
67
{
 
68
  // do de-initialization for your plugin
 
69
  // if appShutDown is false, the plugin is unloaded because Code::Blocks is being shut down,
 
70
  // which means you must not use any of the SDK Managers
 
71
  // NOTE: after this function, the inherited member variable
 
72
  // IsAttached() will be FALSE...
 
73
}
 
74
 
 
75
void Exporter::BuildMenu(wxMenuBar *menuBar)
 
76
{
 
77
  // find "File" menu position
 
78
  int fileMenuPos = menuBar->FindMenu(_("&File"));
 
79
 
 
80
  if (fileMenuPos == -1)
 
81
  {
 
82
    //cbThrow(_T("Can't find \"File\" menu position?!?"));
 
83
    return;
 
84
  }
 
85
 
 
86
  // find actual "File" menu
 
87
  wxMenu *file = menuBar->GetMenu(fileMenuPos);
 
88
 
 
89
  if (!file)
 
90
  {
 
91
    //cbThrow(_T("Can't find \"File\" menu?!?"));
 
92
    return;
 
93
  }
 
94
 
 
95
  // decide where to insert in "File" menu
 
96
  size_t printPos = file->GetMenuItemCount() - 4; // the default location
 
97
  int printID = file->FindItem(_("Print..."));
 
98
 
 
99
  if (printID != wxNOT_FOUND)
 
100
  {
 
101
    file->FindChildItem(printID, &printPos);
 
102
    ++printPos; // after "Print"
 
103
  }
 
104
 
 
105
  // insert menu items
 
106
  wxMenu *export_submenu = new wxMenu;
 
107
  export_submenu->Append(idFileExportHTML, _("As &HTML..."), _("Exports the current file to HTML"));
 
108
  export_submenu->Append(idFileExportRTF, _("As &RTF..."), _("Exports the current file to RTF"));
 
109
  export_submenu->Append(idFileExportODT, _("As &ODT..."), _("Exports the current file to ODT"));
 
110
  export_submenu->Append(idFileExportPDF, _("As &PDF..."), _("Exports the current file to PDF"));
 
111
 
 
112
  wxMenuItem *export_menu = new wxMenuItem(0, idFileExport, _("&Export"), _T(""), wxITEM_NORMAL);
 
113
  export_menu->SetSubMenu(export_submenu);
 
114
 
 
115
  file->Insert(printPos, export_menu);
 
116
}
 
117
 
 
118
void Exporter::RemoveMenu(wxMenuBar *menuBar)
 
119
{
 
120
  wxMenu *menu = 0;
 
121
  wxMenuItem *item = menuBar->FindItem(idFileExport, &menu);
 
122
 
 
123
  if (menu && item)
 
124
  {
 
125
    menu->Remove(item);
 
126
  }
 
127
}
 
128
 
 
129
void Exporter::OnUpdateUI(wxUpdateUIEvent &event)
 
130
{
 
131
  if (Manager::IsAppShuttingDown())
 
132
  {
 
133
    event.Skip();
 
134
    return;
 
135
  }
 
136
 
 
137
  wxMenuBar *mbar = Manager::Get()->GetAppFrame()->GetMenuBar();
 
138
 
 
139
  if (mbar)
 
140
  {
 
141
    EditorManager *em = Manager::Get()->GetEditorManager();
 
142
 
 
143
    // Enabled if there's a source file opened (be sure it isn't the "Start here" page)
 
144
    bool disable = !em || !em->GetActiveEditor() || !em->GetBuiltinActiveEditor();
 
145
    mbar->Enable(idFileExportHTML, !disable);
 
146
    mbar->Enable(idFileExportRTF, !disable);
 
147
    mbar->Enable(idFileExportODT, !disable);
 
148
    mbar->Enable(idFileExportPDF, !disable);
 
149
  }
 
150
 
 
151
  event.Skip();
 
152
}
 
153
 
 
154
void Exporter::OnExportHTML(wxCommandEvent & /*event*/)
 
155
{
 
156
  HTMLExporter exp;
 
157
  ExportFile(&exp, _T("html"), _("HTML files|*.html;*.htm"));
 
158
}
 
159
 
 
160
void Exporter::OnExportRTF(wxCommandEvent & /*event*/)
 
161
{
 
162
  RTFExporter exp;
 
163
  ExportFile(&exp, _T("rtf"), _("RTF files|*.rtf"));
 
164
}
 
165
 
 
166
 
 
167
void Exporter::OnExportODT(wxCommandEvent & /*event*/)
 
168
{
 
169
  ODTExporter exp;
 
170
  ExportFile(&exp, _T("odt"), _("ODT files|*.odt"));
 
171
}
 
172
 
 
173
void Exporter::OnExportPDF(wxCommandEvent & /*event*/)
 
174
{
 
175
  PDFExporter exp;
 
176
  ExportFile(&exp, _T("pdf"), _("PDF files|*.pdf"));
 
177
}
 
178
 
 
179
void Exporter::ExportFile(BaseExporter *exp, const wxString &default_extension, const wxString &wildcard)
 
180
{
 
181
  if (!IsAttached())
 
182
  {
 
183
    return;
 
184
  }
 
185
 
 
186
  EditorManager* em = Manager::Get()->GetEditorManager();
 
187
  cbEditor*      cb = em->GetBuiltinActiveEditor();
 
188
 
 
189
  wxString filename = wxFileSelector(_("Choose the filename"), _T(""), wxFileName(cb->GetFilename()).GetName() + _T(".") + default_extension, default_extension, wildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
 
190
  if (filename.IsEmpty())
 
191
  {
 
192
    return;
 
193
  }
 
194
 
 
195
  cbStyledTextCtrl* stc = cb->GetControl();
 
196
  if (!stc)
 
197
      return;
 
198
 
 
199
  int lineCount = -1;
 
200
  if (wxMessageBox(_("Would you like to have the line numbers printed in the exported file?"), _("Export line numbers"), wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION) == wxYES)
 
201
  {
 
202
    lineCount = stc->GetLineCount();
 
203
  }
 
204
 
 
205
  exp->Export(filename, cb->GetFilename(), stc->GetStyledText(0, stc->GetLength() - 1), cb->GetColourSet(), lineCount, stc->GetTabWidth());
 
206
}