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