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