]> git.saurik.com Git - wxWidgets.git/blob - demos/poem/wxpoem.cpp
Update debugging macros overview in the docs.
[wxWidgets.git] / demos / poem / wxpoem.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxpoem.cpp
3 // Purpose: A small C++ program which displays a random poem on
4 // execution. It also allows search for poems containing a
5 // string.
6 // It requires winpoem.dat and creates winpoem.idx.
7 // Original version (WinPoem) written in 1994.
8 // This has not been rewritten in a long time so
9 // beware, inelegant code!
10 // Author: Julian Smart
11 // Created: 12/12/98
12 // RCS-ID: $Id$
13 // Copyright: (c) 1998 Julian Smart
14 // Licence: wxWindows licence
15 /////////////////////////////////////////////////////////////////////////////
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "wxpoem.h"
29
30 #include "corner1.xpm"
31 #include "corner2.xpm"
32 #include "corner3.xpm"
33 #include "corner4.xpm"
34 #include "wxpoem.xpm"
35
36 #define BUFFER_SIZE 10000
37 #define DEFAULT_POETRY_DAT "wxpoem"
38 #define DEFAULT_POETRY_IND "wxpoem"
39 #define DEFAULT_CHAR_HEIGHT 18
40 #define DEFAULT_FONT "Swiss"
41 #define DEFAULT_X_POS 0
42 #define DEFAULT_Y_POS 0
43 #define BORDER_SIZE 30
44 #define THIN_LINE_BORDER 10
45 #define THICK_LINE_BORDER 16
46 #define THICK_LINE_WIDTH 2
47 #define SHADOW_OFFSET 1
48 #define X_SIZE 30
49 #define Y_SIZE 20
50
51 static wxChar *poem_buffer; // Storage for each poem
52 static wxChar line[150]; // Storage for a line
53 static int pages[30]; // For multipage poems -
54 // store the start of each page
55 static long last_poem_start = 0; // Start of last found poem
56 static long last_find = -1; // Point in file of last found
57 // search string
58 static bool search_ok = false; // Search was successful
59 static bool same_search = false; // Searching on same string
60
61 static long poem_index[600]; // Index of poem starts
62 static long nitems = 0; // Number of poems
63 static int char_height = DEFAULT_CHAR_HEIGHT; // Actual height
64 static int index_ptr = -1; // Pointer into index
65 static int poem_height, poem_width; // Size of poem
66 static int XPos; // Startup X position
67 static int YPos; // Startup Y position
68 static int pointSize = 12; // Font size
69
70 static const wxChar *index_filename = NULL; // Index filename
71 static const wxChar *data_filename = NULL; // Data filename
72 static wxChar error_buf[300]; // Error message buffer
73 static bool loaded_ok = false; // Poem loaded ok
74 static bool index_ok = false; // Index loaded ok
75
76 static bool paging = false; // Are we paging?
77 static int current_page = 0; // Currently viewed page
78
79 // Backing bitmap
80 wxBitmap *backingBitmap = NULL;
81
82 void PoetryError(const wxChar *, const wxChar *caption=wxT("wxPoem Error"));
83 void PoetryNotify(const wxChar *Msg, const wxChar *caption=wxT("wxPoem"));
84 void TryLoadIndex();
85 bool LoadPoem(const wxChar *, long);
86 int GetIndex();
87 int LoadIndex(const wxChar *);
88 bool Compile(void);
89 void FindMax(int *max_thing, int thing);
90
91 #if wxUSE_CLIPBOARD
92 #include "wx/dataobj.h"
93 #include "wx/clipbrd.h"
94 #endif
95
96 #ifdef __WXWINCE__
97 STDAPI_(__int64) CeGetRandomSeed();
98 #endif
99
100 IMPLEMENT_APP(MyApp)
101
102 MainWindow *TheMainWindow = NULL;
103
104 // Create the fonts
105 void MainWindow::CreateFonts()
106 {
107 m_normalFont = wxTheFontList->FindOrCreateFont(pointSize, wxSWISS, wxNORMAL, wxNORMAL);
108 m_boldFont = wxTheFontList->FindOrCreateFont(pointSize, wxSWISS, wxNORMAL, wxBOLD);
109 m_italicFont = wxTheFontList->FindOrCreateFont(pointSize, wxSWISS, wxITALIC, wxNORMAL);
110 }
111
112 BEGIN_EVENT_TABLE(MainWindow, wxFrame)
113 EVT_CLOSE(MainWindow::OnCloseWindow)
114 EVT_CHAR(MainWindow::OnChar)
115 EVT_MENU(wxID_ANY, MainWindow::OnPopup)
116 END_EVENT_TABLE()
117
118 MainWindow::MainWindow(wxFrame *frame, wxWindowID id, const wxString& title,
119 const wxPoint& pos, const wxSize& size, long style):
120 wxFrame(frame, id, title, pos, size, style)
121 {
122 m_corners[0] = m_corners[1] = m_corners[2] = m_corners[3] = NULL;
123
124 ReadPreferences();
125 CreateFonts();
126
127 SetIcon(wxpoem_xpm);
128
129 m_corners[0] = new wxIcon( corner1_xpm );
130 m_corners[1] = new wxIcon( corner2_xpm );
131 m_corners[2] = new wxIcon( corner3_xpm );
132 m_corners[3] = new wxIcon( corner4_xpm );
133 }
134
135 MainWindow::~MainWindow()
136 {
137 for (int i=0;i<4;i++)
138 {
139 if(m_corners[i])
140 {
141 delete m_corners[i];
142 }
143 }
144 }
145
146 // Read the poetry buffer, either for finding the size
147 // or for writing to a bitmap (not to the window directly,
148 // since that displays messily)
149 // If DrawIt is true, we draw, otherwise we just determine the
150 // size the window should be.
151 void MainWindow::ScanBuffer(wxDC *dc, bool DrawIt, int *max_x, int *max_y)
152 {
153 int i = pages[current_page];
154 int ch = -1;
155 int y = 0;
156 int j;
157 wxChar *line_ptr;
158 int curr_width = 0;
159 bool page_break = false;
160
161 int width = 0;
162 int height = 0;
163
164 if (DrawIt)
165 {
166 y = (*max_y - poem_height)/2;
167 width = *max_x;
168 height = *max_y;
169 }
170
171 if (DrawIt && wxColourDisplay())
172 {
173 dc->SetBrush(*wxLIGHT_GREY_BRUSH);
174 dc->SetPen(*wxGREY_PEN);
175 dc->DrawRectangle(0, 0, width, height);
176 dc->SetBackgroundMode(wxTRANSPARENT);
177 }
178
179 // See what ACTUAL char height is
180 if(m_normalFont)
181 dc->SetFont(*m_normalFont);
182 wxCoord xx;
183 wxCoord yy;
184 dc->GetTextExtent(wxT("X"), &xx, &yy);
185 char_height = (int)yy;
186
187 if (current_page == 0)
188 {
189 m_title = wxEmptyString;
190 }
191 else if (!m_title.empty())
192 {
193 dc->SetFont(* m_boldFont);
194 dc->GetTextExtent(m_title, &xx, &yy);
195 FindMax(&curr_width, (int)xx);
196
197 if (DrawIt)
198 {
199 int x = (width - xx)/2;
200 dc->SetFont(* m_boldFont);
201
202 // Change text to BLACK!
203 dc->SetTextForeground(* wxBLACK);
204 dc->DrawText(m_title, x, y);
205 // Change text to WHITE!
206 dc->SetTextForeground(* wxWHITE);
207 dc->DrawText(m_title, x-SHADOW_OFFSET, y-SHADOW_OFFSET);
208 }
209 y += char_height;
210 y += char_height;
211 }
212
213 while (ch != 0 && !page_break)
214 {
215 j = 0;
216 #if defined(__WXMSW__) || defined(__WXMAC__)
217 while (((ch = poem_buffer[i]) != 13) && (ch != 0))
218 #else
219 while (((ch = poem_buffer[i]) != 10) && (ch != 0))
220 #endif
221 {
222 line[j] = (wxChar)ch;
223 j ++;
224 i ++;
225 }
226
227 #if defined(__WXMSW__) || defined(__WXMAC__)
228 if (ch == 13)
229 #else
230 if (ch == 10)
231 #endif
232 {
233 ch = -1;
234 i ++;
235 #if defined(__WXMSW__) || defined(__WXMAC__)
236 // Add another to skip the linefeed
237 i ++;
238 #endif
239 // If a single newline on its own, put a space in
240 if (j == 0)
241 {
242 line[j] = ' ';
243 j ++;
244 line[j] = 0;
245 }
246 }
247
248 if (j > 0)
249 {
250 line[j] = 0;
251 if (line[0] == '@')
252 {
253 switch (line[1])
254 {
255 case 'P':
256 paging = true;
257 page_break = true;
258 break;
259
260 case 'T':
261 dc->SetFont(* m_boldFont);
262 line_ptr = line+3;
263
264 m_title = line_ptr;
265 m_title << wxT(" (cont'd)");
266
267 dc->GetTextExtent(line_ptr, &xx, &yy);
268 FindMax(&curr_width, (int)xx);
269
270 if (DrawIt)
271 {
272 int x = (width - xx)/2;
273 dc->SetFont(* m_boldFont);
274
275 // Change text to BLACK!
276 dc->SetTextForeground(* wxBLACK);
277 dc->DrawText(line_ptr, x, y);
278
279 // Change text to WHITE!
280 dc->SetTextForeground(* wxWHITE);
281 dc->DrawText(line_ptr, x-SHADOW_OFFSET, y-SHADOW_OFFSET);
282 dc->SetTextForeground(* wxWHITE);
283 }
284 break;
285
286 case 'A':
287 line_ptr = line+3;
288 dc->SetFont(* m_italicFont);
289
290 dc->GetTextExtent(line_ptr, &xx, &yy);
291 FindMax(&curr_width, (int)xx);
292
293 if (DrawIt)
294 {
295 int x = (width - xx)/2;
296 dc->SetTextForeground(* wxBLACK);
297 dc->DrawText(line_ptr, x, y);
298 }
299 break;
300
301 // Default: just ignore this line
302 default:
303 y -= char_height;
304 }
305 }
306 else
307 {
308 dc->SetFont(* m_normalFont);
309
310 dc->GetTextExtent(line, &xx, &yy);
311 FindMax(&curr_width, (int)xx);
312
313 if (DrawIt)
314 {
315 int x = (int)((width - xx)/2.0);
316 dc->SetFont(* m_normalFont);
317 dc->SetTextForeground(* wxBLACK);
318 dc->DrawText(line, x, y);
319 }
320 }
321 }
322 y += char_height;
323 }
324
325 // Write (cont'd)
326 if (page_break)
327 {
328 const wxChar *cont = wxT("(cont'd)");
329
330 dc->SetFont(* m_normalFont);
331
332 dc->GetTextExtent(cont, &xx, &yy);
333 FindMax(&curr_width, (int)xx);
334 if (DrawIt)
335 {
336 int x = (int)((width - xx)/2.0);
337 dc->SetFont(* m_normalFont);
338 dc->SetTextForeground(* wxBLACK);
339 dc->DrawText(cont, x, y);
340 }
341 y += 2*char_height;
342 }
343
344 *max_x = (int)curr_width;
345 *max_y = (int)(y-char_height);
346
347 if (page_break)
348 pages[current_page+1] = i;
349 else
350 paging = false;
351
352 if (DrawIt)
353 {
354 // Draw dark grey thick border
355 if (wxColourDisplay())
356 {
357 dc->SetBrush(*wxGREY_BRUSH);
358 dc->SetPen(*wxGREY_PEN);
359
360 // Left side
361 dc->DrawRectangle(0, 0, THIN_LINE_BORDER, height);
362 // Top side
363 dc->DrawRectangle(THIN_LINE_BORDER, 0, width-THIN_LINE_BORDER, THIN_LINE_BORDER);
364 // Right side
365 dc->DrawRectangle(width-THIN_LINE_BORDER, THIN_LINE_BORDER, width, height-THIN_LINE_BORDER);
366 // Bottom side
367 dc->DrawRectangle(THIN_LINE_BORDER, height-THIN_LINE_BORDER, width-THIN_LINE_BORDER, height);
368 }
369 // Draw border
370 // Have grey background, plus 3-d border -
371 // One black rectangle.
372 // Inside this, left and top sides - dark grey. Bottom and right -
373 // white.
374
375 // Change pen to black
376 dc->SetPen(*wxBLACK_PEN);
377 dc->DrawLine(THIN_LINE_BORDER, THIN_LINE_BORDER, width-THIN_LINE_BORDER, THIN_LINE_BORDER);
378 dc->DrawLine(width-THIN_LINE_BORDER, THIN_LINE_BORDER, width-THIN_LINE_BORDER, height-THIN_LINE_BORDER);
379 dc->DrawLine(width-THIN_LINE_BORDER, height-THIN_LINE_BORDER, THIN_LINE_BORDER, height-THIN_LINE_BORDER);
380 dc->DrawLine(THIN_LINE_BORDER, height-THIN_LINE_BORDER, THIN_LINE_BORDER, THIN_LINE_BORDER);
381
382 // Right and bottom white lines - 'grey' (black!) if
383 // we're running on a mono display.
384 if (wxColourDisplay())
385 dc->SetPen(*wxWHITE_PEN);
386 else
387 dc->SetPen(*wxBLACK_PEN);
388
389 dc->DrawLine(width-THICK_LINE_BORDER, THICK_LINE_BORDER,
390 width-THICK_LINE_BORDER, height-THICK_LINE_BORDER);
391 dc->DrawLine(width-THICK_LINE_BORDER, height-THICK_LINE_BORDER,
392 THICK_LINE_BORDER, height-THICK_LINE_BORDER);
393
394 // Left and top grey lines
395 dc->SetPen(*wxBLACK_PEN);
396 dc->DrawLine(THICK_LINE_BORDER, height-THICK_LINE_BORDER,
397 THICK_LINE_BORDER, THICK_LINE_BORDER);
398 dc->DrawLine(THICK_LINE_BORDER, THICK_LINE_BORDER,
399 width-THICK_LINE_BORDER, THICK_LINE_BORDER);
400
401 // Draw icons
402 dc->DrawIcon(* m_corners[0], 0, 0);
403 dc->DrawIcon(* m_corners[1], int(width-32), 0);
404
405 int y2 = height - 32;
406 int x2 = (width-32);
407 dc->DrawIcon(* m_corners[2], 0, y2);
408 dc->DrawIcon(* m_corners[3], x2, y2);
409 }
410 }
411
412 // Get an index (randomly generated) and load the poem
413 void MainWindow::GetIndexLoadPoem(void)
414 {
415 if (index_ok)
416 index_ptr = GetIndex();
417
418 if (index_ptr > -1)
419 loaded_ok = LoadPoem(data_filename, -1);
420 }
421
422 // Find the size of the poem and resize the window accordingly
423 void MainWindow::Resize(void)
424 {
425 wxClientDC dc(canvas);
426
427 // Get the poem size
428 ScanBuffer(& dc, false, &poem_width, &poem_height);
429 int x = poem_width + (2*BORDER_SIZE);
430 int y = poem_height + (2*BORDER_SIZE);
431
432 SetClientSize(x, y);
433
434 // In case client size isn't what we set it to...
435 int xx, yy;
436 GetClientSize(&xx, &yy);
437
438 wxMemoryDC memDC;
439 if (backingBitmap) delete backingBitmap;
440 backingBitmap = new wxBitmap(x, yy);
441 memDC.SelectObject(* backingBitmap);
442
443 memDC.Clear();
444 ScanBuffer(&memDC, true, &xx, &yy);
445 }
446
447 // Which is more?
448 void FindMax(int *max_thing, int thing)
449 {
450 if (thing > *max_thing)
451 *max_thing = thing;
452 }
453
454 // Next page/poem
455 void MainWindow::NextPage(void)
456 {
457 if (paging)
458 current_page ++;
459 else
460 {
461 current_page = 0;
462 GetIndexLoadPoem();
463 }
464 Resize();
465 }
466
467 // Previous page
468 void MainWindow::PreviousPage(void)
469 {
470 if (current_page > 0)
471 {
472 current_page --;
473 Resize();
474 }
475 }
476
477 // Search for a string
478 void MainWindow::Search(bool ask)
479 {
480 long position;
481
482 if (ask || m_searchString.empty())
483 {
484 wxString s = wxGetTextFromUser( wxT("Enter search string"), wxT("Search"), m_searchString);
485 if (!s.empty())
486 {
487 s.MakeLower();
488 m_searchString = s;
489 search_ok = true;
490 }
491 else
492 {
493 search_ok = false;
494 }
495 }
496 else
497 {
498 same_search = true;
499 search_ok = true;
500 }
501
502 if (!m_searchString.empty() && search_ok)
503 {
504 position = DoSearch();
505 if (position > -1)
506 {
507 loaded_ok = LoadPoem(data_filename, position);
508 Resize();
509 }
510 else
511 {
512 last_poem_start = 0;
513 PoetryNotify(wxT("Search string not found."));
514 }
515 }
516 }
517
518 bool MyApp::OnInit()
519 {
520 poem_buffer = new wxChar[BUFFER_SIZE];
521
522 // Seed the random number generator
523 #ifdef __WXWINCE__
524 srand((unsigned) CeGetRandomSeed());
525 #else
526 time_t current_time;
527
528 (void)time(&current_time);
529 srand((unsigned int)current_time);
530 #endif
531
532 // randomize();
533 pages[0] = 0;
534
535 TheMainWindow = new MainWindow(NULL,
536 wxID_ANY,
537 wxT("wxPoem"),
538 wxPoint(XPos, YPos),
539 wxDefaultSize,
540 wxCAPTION|wxMINIMIZE_BOX|wxSYSTEM_MENU|wxCLOSE_BOX|wxFULL_REPAINT_ON_RESIZE
541 );
542
543 TheMainWindow->canvas = new MyCanvas(TheMainWindow);
544
545 if (argc > 1)
546 {
547 index_filename = wxStrcpy(new wxChar[wxStrlen(argv[1]) + 1], argv[1]);
548 data_filename = wxStrcpy(new wxChar[wxStrlen(argv[1]) + 1], argv[1]);
549 }
550 else
551 {
552 index_filename = wxT(DEFAULT_POETRY_IND);
553 data_filename = wxT(DEFAULT_POETRY_DAT);
554 }
555 TryLoadIndex();
556
557 TheMainWindow->GetIndexLoadPoem();
558 TheMainWindow->Resize();
559 TheMainWindow->Show(true);
560
561 return true;
562 }
563
564 int MyApp::OnExit()
565 {
566 if (backingBitmap)
567 delete backingBitmap;
568
569 delete[] poem_buffer;
570
571 return 0;
572 }
573
574 void MainWindow::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
575 {
576 WritePreferences();
577 this->Destroy();
578 }
579
580 void MainWindow::OnChar(wxKeyEvent& event)
581 {
582 canvas->OnChar(event);
583 }
584
585 BEGIN_EVENT_TABLE(MyCanvas, wxWindow)
586 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
587 EVT_CHAR(MyCanvas::OnChar)
588 EVT_PAINT(MyCanvas::OnPaint)
589 END_EVENT_TABLE()
590
591 // Define a constructor for my canvas
592 MyCanvas::MyCanvas(wxFrame *frame):
593 wxWindow(frame, wxID_ANY)
594 {
595 m_popupMenu = new wxMenu;
596 m_popupMenu->Append(POEM_NEXT, wxT("Next poem/page"));
597 m_popupMenu->Append(POEM_PREVIOUS, wxT("Previous page"));
598 m_popupMenu->AppendSeparator();
599 m_popupMenu->Append(POEM_SEARCH, wxT("Search"));
600 m_popupMenu->Append(POEM_NEXT_MATCH, wxT("Next match"));
601 m_popupMenu->Append(POEM_COPY, wxT("Copy to clipboard"));
602 m_popupMenu->Append(POEM_MINIMIZE, wxT("Minimize"));
603 m_popupMenu->AppendSeparator();
604 m_popupMenu->Append(POEM_BIGGER_TEXT, wxT("Bigger text"));
605 m_popupMenu->Append(POEM_SMALLER_TEXT, wxT("Smaller text"));
606 m_popupMenu->AppendSeparator();
607 m_popupMenu->Append(POEM_ABOUT, wxT("About wxPoem"));
608 m_popupMenu->AppendSeparator();
609 m_popupMenu->Append(POEM_EXIT, wxT("Exit"));
610 }
611
612 MyCanvas::~MyCanvas()
613 {
614 // Note: this must be done before the main window/canvas are destroyed
615 // or we get an error (no parent window for menu item button)
616 delete m_popupMenu;
617 m_popupMenu = NULL;
618 }
619
620 // Define the repainting behaviour
621 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
622 {
623 wxPaintDC dc(this);
624
625 if (backingBitmap)
626 {
627 int xx, yy;
628 TheMainWindow->GetClientSize(&xx, &yy);
629
630 dc.DrawBitmap(* backingBitmap, 0, 0);
631 #if 0
632 wxMemoryDC memDC;
633 memDC.SelectObject(* backingBitmap);
634 dc.Blit(0, 0, backingBitmap->GetWidth(), backingBitmap->GetHeight(), &memDC, 0, 0);
635 #endif
636 }
637 }
638
639 void MyCanvas::OnMouseEvent(wxMouseEvent& event)
640 {
641 static int startPosX, startPosY, startFrameX, startFrameY;
642
643 long x, y;
644 event.GetPosition(&x, &y);
645
646 if (event.RightDown())
647 {
648 // Versions from wxWin 1.67 are probably OK
649 PopupMenu(m_popupMenu, (int)x, (int)y );
650 }
651 else if (event.LeftDown())
652 {
653 this->CaptureMouse();
654 int x1 = (int)x;
655 int y1 = (int)y;
656 ClientToScreen(&x1, &y1);
657 startPosX = x1;
658 startPosY = y1;
659 GetParent()->GetPosition(&startFrameX, &startFrameY);
660 }
661 else if (event.LeftUp())
662 {
663 if (GetCapture() == this) this->ReleaseMouse();
664 }
665 else if (event.Dragging() && event.LeftIsDown())
666 {
667 int x1 = (int)x;
668 int y1 = (int)y;
669 ClientToScreen(&x1, &y1);
670
671 int dX = x1 - startPosX;
672 int dY = y1 - startPosY;
673 GetParent()->Move(startFrameX + dX, startFrameY + dY);
674 }
675 }
676
677 // Process characters
678 void MyCanvas::OnChar(wxKeyEvent& event)
679 {
680 switch (event.GetKeyCode())
681 {
682 case 'n':
683 case 'N':
684 // Next match
685 TheMainWindow->Search(false);
686 break;
687
688 case 's':
689 case 'S':
690 // New search
691 TheMainWindow->Search(true);
692 break;
693
694 case WXK_SPACE:
695 case WXK_RIGHT:
696 case WXK_DOWN:
697 // Another poem
698 TheMainWindow->NextPage();
699 break;
700
701 case WXK_ESCAPE:
702 TheMainWindow->Close(true);
703 default:
704 break;
705 }
706 }
707
708 // Load index file
709 int LoadIndex(const wxChar *file_name)
710 {
711 long data;
712 FILE *index_file;
713
714 wxChar buf[100];
715
716 if (file_name == NULL)
717 return 0;
718
719 wxSprintf(buf, wxT("%s.idx"), file_name);
720
721 index_file = wxFopen(buf, wxT("r"));
722 if (index_file == NULL)
723 return 0;
724
725 wxFscanf(index_file, wxT("%ld"), &nitems);
726
727 for (int i = 0; i < nitems; i++)
728 {
729 wxFscanf(index_file, wxT("%ld"), &data);
730 poem_index[i] = data;
731 }
732
733 fclose(index_file);
734
735 return 1;
736 }
737
738 // Get index
739 int GetIndex()
740 {
741 int indexn = (int)(rand() % nitems);
742
743 if ((indexn < 0) || (indexn > nitems))
744 { PoetryError(wxT("No such poem!"));
745 return -1;
746 }
747 else
748 return indexn;
749 }
750
751 // Read preferences
752 void MainWindow::ReadPreferences()
753 {
754 /* TODO: convert this code to use wxConfig
755 #if wxUSE_RESOURCES
756 wxGetResource(wxT("wxPoem"), wxT("FontSize"), &pointSize);
757 wxGetResource(wxT("wxPoem"), wxT("X"), &XPos);
758 wxGetResource(wxT("wxPoem"), wxT("Y"), &YPos);
759 #endif
760 */
761 }
762
763 // Write preferences to disk
764 void MainWindow::WritePreferences()
765 {
766 #ifdef __WXMSW__
767 TheMainWindow->GetPosition(&XPos, &YPos);
768 /* TODO: convert this code to use wxConfig
769 #if wxUSE_RESOURCES
770 wxWriteResource(wxT("wxPoem"), wxT("FontSize"), pointSize);
771 wxWriteResource(wxT("wxPoem"), wxT("X"), XPos);
772 wxWriteResource(wxT("wxPoem"), wxT("Y"), YPos);
773 #endif
774 */
775 #endif
776 }
777
778 // Load a poem from given file, at given point in file.
779 // If position is > -1, use this for the position in the
780 // file, otherwise use index[index_ptr] to find the correct position.
781 bool LoadPoem(const wxChar *file_name, long position)
782 {
783 // int j = 0;
784 // int indexn = 0;
785 wxChar buf[100];
786 long data;
787 FILE *data_file;
788
789 paging = false;
790 current_page = 0;
791
792 if (file_name == NULL)
793 {
794 wxSprintf(error_buf, wxT("Error in Poem loading."));
795 PoetryError(error_buf);
796 return false;
797 }
798
799 wxSprintf(buf, wxT("%s.dat"), file_name);
800 data_file = wxFopen(buf, wxT("r"));
801
802 if (data_file == NULL)
803 {
804 wxSprintf(error_buf, wxT("Data file %s not found."), buf);
805 PoetryError(error_buf);
806 return false;
807 }
808
809 if (position > -1)
810 data = position;
811 else
812 data = poem_index[index_ptr];
813
814 fseek(data_file, data, SEEK_SET);
815
816 int ch = 0;
817 int i = 0;
818 while ((ch != EOF) && (ch != '#'))
819 {
820 ch = getc(data_file);
821 // Add a linefeed so it will copy to the clipboard ok
822 if (ch == 10)
823 {
824 poem_buffer[i] = 13;
825 i++;
826 }
827
828 poem_buffer[i] = (wxChar)ch;
829 i ++;
830
831 if (i == BUFFER_SIZE)
832 {
833 wxSprintf(error_buf, wxT("%s"), wxT("Poetry buffer exceeded."));
834 PoetryError(error_buf);
835 return false;
836 }
837 }
838 fclose(data_file);
839 poem_buffer[i-1] = 0;
840 return true;
841 }
842
843 // Do the search
844 long MainWindow::DoSearch(void)
845 {
846 if (m_searchString.empty())
847 return false;
848
849 FILE *file;
850 size_t i = 0;
851 int ch = 0;
852 wxChar buf[100];
853 long find_start;
854 long previous_poem_start;
855
856 bool found = false;
857 size_t search_length = m_searchString.length();
858
859 if (same_search)
860 {
861 find_start = last_find + 1;
862 previous_poem_start = last_poem_start;
863 }
864 else
865 {
866 find_start = 0;
867 last_poem_start = 0;
868 previous_poem_start = -1;
869 }
870
871 if (data_filename)
872 wxSprintf(buf, wxT("%s.dat"), data_filename);
873
874 file = wxFopen(buf, wxT("r"));
875 if (! (data_filename && file))
876 {
877 wxSprintf(error_buf, wxT("Poetry data file %s not found\n"), buf);
878 PoetryError(error_buf);
879 return false;
880 }
881
882 fseek(file, find_start, SEEK_SET);
883
884 while ((ch != EOF) && !found)
885 {
886 ch = getc(file);
887 ch = wxTolower(ch); // Make lower case
888
889 // Only match if we're looking at a different poem
890 // (no point in displaying the same poem again)
891 if ((m_searchString[i] == ch) && (last_poem_start != previous_poem_start))
892 {
893 if (i == 0)
894 last_find = ftell(file);
895 if (i == search_length-1)
896 found = true;
897 i ++;
898 }
899 else
900 {
901 i = 0;
902 }
903
904 if (ch == '#')
905 {
906 ch = getc(file);
907 last_poem_start = ftell(file);
908 }
909 }
910 fclose(file);
911 if (ch == EOF)
912 {
913 last_find = -1;
914 }
915
916 if (found)
917 {
918 return last_poem_start;
919 }
920
921 return -1;
922 }
923
924 // Set up poetry filenames, preferences, load the index
925 // Load index (or compile it if none found)
926 void TryLoadIndex()
927 {
928 index_ok = (LoadIndex(index_filename) != 0);
929 if (!index_ok || (nitems == 0))
930 {
931 PoetryError(wxT("Index file not found; will compile new one"), wxT("wxPoem"));
932 index_ok = Compile();
933 }
934 }
935
936 // Error message
937 void PoetryError(const wxChar *msg, const wxChar *caption)
938 {
939 wxMessageBox(msg, caption, wxOK|wxICON_EXCLAMATION);
940 }
941
942 // Notification (change icon to something appropriate!)
943 void PoetryNotify(const wxChar *Msg, const wxChar *caption)
944 {
945 wxMessageBox(Msg, caption, wxOK | wxICON_INFORMATION);
946 }
947
948 // Build up and save an index into the poetry data file, for
949 // rapid random access
950 bool Compile(void)
951 {
952 FILE *file;
953 int j;
954 int ch;
955 wxChar buf[100];
956
957 if (data_filename)
958 wxSprintf(buf, wxT("%s.dat"), data_filename);
959
960 file = wxFopen(buf, wxT("r"));
961 if (! (data_filename && file))
962 {
963 wxSprintf(error_buf, wxT("Poetry data file %s not found\n"), buf);
964 PoetryError(error_buf);
965 return false;
966 }
967
968 nitems = 0;
969
970 // Do first one (?)
971 poem_index[nitems] = 0;
972 nitems ++;
973
974 // Do rest
975
976 do {
977 ch = getc(file);
978 if (ch == '#')
979 {
980 ch = getc(file);
981 long data;
982 data = ftell(file);
983 poem_index[nitems] = data;
984 nitems ++;
985 }
986 } while (ch != EOF);
987 fclose(file);
988
989 if (index_filename)
990 wxSprintf(buf, wxT("%s.idx"), index_filename);
991
992 file = wxFopen(buf, wxT("w"));
993 if (! (data_filename && file))
994 {
995 wxSprintf(error_buf, wxT("Poetry index file %s cannot be created\n"), buf);
996 PoetryError(error_buf);
997 return false;
998 }
999
1000 wxFprintf(file, wxT("%ld\n\n"), nitems);
1001 for (j = 0; j < nitems; j++)
1002 wxFprintf(file, wxT("%ld\n"), poem_index[j]);
1003
1004 fclose(file);
1005 PoetryNotify(wxT("Poetry index compiled."));
1006 return true;
1007 }
1008
1009 void MainWindow::OnPopup(wxCommandEvent& event)
1010 {
1011 switch (event.GetId())
1012 {
1013 case POEM_NEXT:
1014 // Another poem/page
1015 TheMainWindow->NextPage();
1016 break;
1017 case POEM_PREVIOUS:
1018 // Previous page
1019 TheMainWindow->PreviousPage();
1020 break;
1021 case POEM_SEARCH:
1022 // Search - with dialog
1023 TheMainWindow->Search(true);
1024 break;
1025 case POEM_NEXT_MATCH:
1026 // Search - without dialog (next match)
1027 TheMainWindow->Search(false);
1028 break;
1029 case POEM_MINIMIZE:
1030 TheMainWindow->Iconize(true);
1031 break;
1032 #if wxUSE_CLIPBOARD
1033 case POEM_COPY:
1034 wxTheClipboard->UsePrimarySelection();
1035 if (wxTheClipboard->Open())
1036 {
1037 static wxString s;
1038 s = poem_buffer;
1039 s.Replace( wxT("@P"),wxEmptyString);
1040 s.Replace( wxT("@A "),wxEmptyString);
1041 s.Replace( wxT("@A"),wxEmptyString);
1042 s.Replace( wxT("@T "),wxEmptyString);
1043 s.Replace( wxT("@T"),wxEmptyString);
1044 wxTextDataObject *data = new wxTextDataObject( s.c_str() );
1045 if (!wxTheClipboard->SetData( data ))
1046 wxMessageBox(wxT("Error while copying to the clipboard."));
1047 }
1048 else
1049 {
1050 wxMessageBox(wxT("Error opening the clipboard."));
1051 }
1052 wxTheClipboard->Close();
1053 break;
1054 #endif
1055 case POEM_BIGGER_TEXT:
1056 pointSize ++;
1057 CreateFonts();
1058 TheMainWindow->Resize();
1059 break;
1060 case POEM_SMALLER_TEXT:
1061 if (pointSize > 2)
1062 {
1063 pointSize --;
1064 CreateFonts();
1065 TheMainWindow->Resize();
1066 }
1067 break;
1068 case POEM_ABOUT:
1069 (void)wxMessageBox(wxT("wxPoem Version 1.1\nJulian Smart (c) 1995"),
1070 wxT("About wxPoem"), wxOK, TheMainWindow);
1071 break;
1072 case POEM_EXIT:
1073 // Exit
1074 TheMainWindow->Close(true);
1075 break;
1076 default:
1077 break;
1078 }
1079 }