PageRenderTime 124ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/boxvivid/examples/UWord/UWord.cpp

http://boxvivid.googlecode.com/
C++ | 271 lines | 237 code | 34 blank | 0 comment | 15 complexity | 94b41e89dae7fe45e516d2c284c40659 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, LGPL-3.0
  1. #include <RichEdit/RichEdit.h>
  2. #include <PdfDraw/PdfDraw.h>
  3. using namespace Upp;
  4. #define IMAGECLASS UWordImg
  5. #define IMAGEFILE <UWord/UWord.iml>
  6. #include <Draw/iml.h>
  7. FileSel& UWordFs()
  8. {
  9. static FileSel fs;
  10. return fs;
  11. }
  12. FileSel& PdfFs()
  13. {
  14. static FileSel fs;
  15. return fs;
  16. }
  17. class UWord : public TopWindow {
  18. public:
  19. virtual void DragAndDrop(Point, PasteClip& d);
  20. virtual void FrameDragAndDrop(Point, PasteClip& d);
  21. protected:
  22. RichEdit editor;
  23. MenuBar menubar;
  24. ToolBar toolbar;
  25. StatusBar statusbar;
  26. String filename;
  27. static LRUList& lrufile() { static LRUList l; return l; }
  28. void Load(const String& filename);
  29. void OpenFile(const String& fn);
  30. void New();
  31. void Open();
  32. void Save0();
  33. void Save();
  34. void SaveAs();
  35. void Print();
  36. void Pdf();
  37. void About();
  38. void Destroy();
  39. void SetBar();
  40. void FileBar(Bar& bar);
  41. void AboutMenu(Bar& bar);
  42. void MainMenu(Bar& bar);
  43. void MainBar(Bar& bar);
  44. public:
  45. typedef UWord CLASSNAME;
  46. static void SerializeApp(Stream& s);
  47. UWord();
  48. };
  49. void UWord::FileBar(Bar& bar)
  50. {
  51. bar.Add("New", CtrlImg::new_doc(), THISBACK(New))
  52. .Key(K_CTRL_N)
  53. .Help("Open new window");
  54. bar.Add("Open..", CtrlImg::open(), THISBACK(Open))
  55. .Key(K_CTRL_O)
  56. .Help("Open existing document");
  57. bar.Add(editor.IsModified(), "Save", CtrlImg::save(), THISBACK(Save))
  58. .Key(K_CTRL_S)
  59. .Help("Save current document");
  60. bar.Add("SaveAs", CtrlImg::save_as(), THISBACK(SaveAs))
  61. .Help("Save current document with a new name");
  62. bar.ToolGap();
  63. bar.MenuSeparator();
  64. bar.Add("Print..", CtrlImg::print(), THISBACK(Print))
  65. .Key(K_CTRL_P)
  66. .Help("Print document");
  67. bar.Add("Export to PDF..", UWordImg::pdf(), THISBACK(Pdf))
  68. .Help("Export document to PDF file");
  69. if(bar.IsMenuBar()) {
  70. if(lrufile().GetCount())
  71. lrufile()(bar, THISBACK(OpenFile));
  72. bar.Separator();
  73. bar.Add("Exit", THISBACK(Destroy));
  74. }
  75. }
  76. void UWord::AboutMenu(Bar& bar)
  77. {
  78. bar.Add("About..", THISBACK(About));
  79. }
  80. void UWord::MainMenu(Bar& bar)
  81. {
  82. bar.Add("File", THISBACK(FileBar));
  83. bar.Add("Window", callback(WindowsMenu));
  84. bar.Add("Help", THISBACK(AboutMenu));
  85. }
  86. void UWord::New()
  87. {
  88. new UWord;
  89. }
  90. void UWord::Load(const String& name)
  91. {
  92. lrufile().NewEntry(name);
  93. editor.SetQTF(LoadFile(name));
  94. filename = name;
  95. editor.ClearModify();
  96. Title(filename);
  97. }
  98. void UWord::OpenFile(const String& fn)
  99. {
  100. if(filename.IsEmpty() && !editor.IsModified())
  101. Load(fn);
  102. else
  103. (new UWord)->Load(fn);
  104. }
  105. void UWord::Open()
  106. {
  107. FileSel& fs = UWordFs();
  108. if(fs.ExecuteOpen())
  109. OpenFile(fs);
  110. else
  111. statusbar.Temporary("Loading aborted.");
  112. }
  113. void UWord::DragAndDrop(Point, PasteClip& d)
  114. {
  115. if(AcceptFiles(d)) {
  116. Vector<String> fn = GetFiles(d);
  117. for(int i = 0; i < fn.GetCount(); i++)
  118. if(FileExists(fn[i]))
  119. OpenFile(fn[i]);
  120. }
  121. }
  122. void UWord::FrameDragAndDrop(Point p, PasteClip& d)
  123. {
  124. DragAndDrop(p, d);
  125. }
  126. void UWord::Save0()
  127. {
  128. lrufile().NewEntry(filename);
  129. if(filename.IsEmpty())
  130. SaveAs();
  131. else
  132. if(SaveFile(filename, editor.GetQTF())) {
  133. statusbar.Temporary("File " + filename + " was saved.");
  134. ClearModify();
  135. }
  136. else
  137. Exclamation("Error saving the file [* " + DeQtf(filename) + "]!");
  138. }
  139. void UWord::Save()
  140. {
  141. if(!editor.IsModified()) return;
  142. Save0();
  143. }
  144. void UWord::SaveAs()
  145. {
  146. FileSel& fs = UWordFs();
  147. if(fs.ExecuteSaveAs()) {
  148. filename = fs;
  149. Title(filename);
  150. Save0();
  151. }
  152. }
  153. void UWord::Print()
  154. {
  155. editor.Print();
  156. }
  157. void UWord::Pdf()
  158. {
  159. FileSel& fs = PdfFs();
  160. if(!fs.ExecuteSaveAs("Output PDF file"))
  161. return;
  162. Size page = Size(3968, 6074);
  163. PdfDraw pdf;
  164. UPP::Print(pdf, editor.Get(), page);
  165. SaveFile(~fs, pdf.Finish());
  166. }
  167. void UWord::About()
  168. {
  169. PromptOK("[A5 uWord]&Using [*^www://upp.sf.net^ Ultimate`+`+] technology.");
  170. }
  171. void UWord::Destroy()
  172. {
  173. if(editor.IsModified()) {
  174. switch(PromptYesNoCancel("Do you want to save the changes to the document?")) {
  175. case 1:
  176. Save();
  177. break;
  178. case -1:
  179. return;
  180. }
  181. }
  182. delete this;
  183. }
  184. void UWord::MainBar(Bar& bar)
  185. {
  186. FileBar(bar);
  187. bar.Separator();
  188. editor.DefaultBar(bar);
  189. }
  190. void UWord::SetBar()
  191. {
  192. toolbar.Set(THISBACK(MainBar));
  193. }
  194. UWord::UWord()
  195. {
  196. AddFrame(menubar);
  197. AddFrame(TopSeparatorFrame());
  198. AddFrame(toolbar);
  199. AddFrame(statusbar);
  200. Add(editor.SizePos());
  201. menubar.Set(THISBACK(MainMenu));
  202. Sizeable().Zoomable();
  203. WhenClose = THISBACK(Destroy);
  204. menubar.WhenHelp = toolbar.WhenHelp = statusbar;
  205. static int doc;
  206. Title(Format("Document%d", ++doc));
  207. Icon(CtrlImg::File());
  208. editor.ClearModify();
  209. SetBar();
  210. editor.WhenRefreshBar = THISBACK(SetBar);
  211. OpenMain();
  212. ActiveFocus(editor);
  213. }
  214. void UWord::SerializeApp(Stream& s)
  215. {
  216. int version = 1;
  217. s / version;
  218. s % UWordFs()
  219. % PdfFs();
  220. if(version >= 1)
  221. s % lrufile();
  222. }
  223. GUI_APP_MAIN
  224. {
  225. SetLanguage(LNG_ENGLISH);
  226. SetDefaultCharset(CHARSET_UTF8);
  227. UWordFs().Type("QTF files", "*.qtf")
  228. .AllFilesType()
  229. .DefaultExt("qtf");
  230. PdfFs().Type("PDF files", "*.pdf")
  231. .AllFilesType()
  232. .DefaultExt("pdf");
  233. LoadFromFile(callback(UWord::SerializeApp));
  234. new UWord;
  235. Ctrl::EventLoop();
  236. StoreToFile(callback(UWord::SerializeApp));
  237. }