File: wxinfo.cpp

package info (click to toggle)
golly 2.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 9,560 kB
  • ctags: 5,064
  • sloc: cpp: 38,119; python: 3,203; perl: 1,121; makefile: 58; java: 49; sh: 22
file content (266 lines) | stat: -rw-r--r-- 8,173 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
                        /*** /

This file is part of Golly, a Game of Life Simulator.
Copyright (C) 2009 Andrew Trevorrow and Tomas Rokicki.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

 Web site:  http://sourceforge.net/projects/golly
 Authors:   rokicki@gmail.com  andrew@trevorrow.com

                        / ***/

#include "wx/wxprec.h"     // for compilers that support precompilation
#ifndef WX_PRECOMP
   #include "wx/wx.h"      // for all others include the necessary headers
#endif

#include "readpattern.h"   // for readcomments

#include "wxgolly.h"       // for wxGetApp, mainptr
#include "wxmain.h"        // for mainptr->...
#include "wxutils.h"       // for Warning
#include "wxprefs.h"       // for infox/y/wd/ht and mininfo*
#include "wxinfo.h"

// -----------------------------------------------------------------------------

// define a modeless window to display pattern info:

class InfoFrame : public wxFrame
{
public:
   InfoFrame(char *comments);

private:
   // event handlers
   void OnActivate(wxActivateEvent& event);
   void OnCloseButton(wxCommandEvent& event);
   void OnClose(wxCloseEvent& event);

   // any class wishing to process wxWidgets events must use this macro
   DECLARE_EVENT_TABLE()
};

BEGIN_EVENT_TABLE(InfoFrame, wxFrame)
   EVT_ACTIVATE   (              InfoFrame::OnActivate)
   EVT_BUTTON     (wxID_CLOSE,   InfoFrame::OnCloseButton)
   EVT_CLOSE      (              InfoFrame::OnClose)
END_EVENT_TABLE()

// -----------------------------------------------------------------------------

InfoFrame *infoptr = NULL;    // pattern info window

wxFrame* GetInfoFrame()
{
   return infoptr;
}

// -----------------------------------------------------------------------------

// define a child window for viewing comments:

class TextView : public wxTextCtrl
{
public:
   TextView(wxWindow *parent, wxWindowID id, const wxString &value,
            const wxPoint& pos, const wxSize& size, long style)
      : wxTextCtrl(parent, id, value, pos, size, style) { }

private:
   // event handlers
   void OnKeyDown(wxKeyEvent& event);

   // any class wishing to process wxWidgets events must use this macro
   DECLARE_EVENT_TABLE()
};

BEGIN_EVENT_TABLE(TextView, wxTextCtrl)
   EVT_KEY_DOWN   (TextView::OnKeyDown)
END_EVENT_TABLE()

// -----------------------------------------------------------------------------

void TextView::OnKeyDown(wxKeyEvent& event)
{
   int key = event.GetKeyCode();
   #ifdef __WXMAC__
      // let cmd-W close info window
      if (event.CmdDown() && key == 'W') {
         infoptr->Close(true);
         return;
      }
      // let cmd-A select all text
      if (event.CmdDown() && key == 'A') {
         SetSelection(-1, -1);
         return;
      }
   #endif
   if ( event.CmdDown() || event.AltDown() ) {
      // let default handler see things like cmd-C 
      event.Skip();
   } else {
      // let escape/return/enter key close info window
      if ( key == WXK_ESCAPE || key == WXK_RETURN || key == WXK_NUMPAD_ENTER ) {
         infoptr->Close(true);
      } else {
         event.Skip();
      }
   }
}

// -----------------------------------------------------------------------------

// create the pattern info window
InfoFrame::InfoFrame(char *comments)
   : wxFrame(NULL, wxID_ANY, _("Pattern Info"),
             wxPoint(infox,infoy), wxSize(infowd,infoht))
{
   wxGetApp().SetFrameIcon(this);

   #ifdef __WXMSW__
      // use current theme's background colour
      SetBackgroundColour(wxNullColour);
   #endif

   TextView* textctrl = new TextView(this, wxID_ANY, wxEmptyString,
                                 wxDefaultPosition, wxDefaultSize,
                                 wxTE_RICH | // needed for font changing on Windows
                                 wxTE_MULTILINE | wxTE_READONLY | wxTE_DONTWRAP);

   // use a fixed-width font
   wxTextAttr textattr = wxTextAttr(wxNullColour, wxNullColour,
                                 #ifdef __WXMAC__
                                    wxFont(11, wxMODERN, wxNORMAL, wxNORMAL));
                                 #else
                                    wxFont(10, wxMODERN, wxNORMAL, wxNORMAL));
                                 #endif
   textctrl->SetDefaultStyle(textattr);   // doesn't change font on X11!!!
   if (comments[0] == 0) {
      textctrl->WriteText(_("No comments found."));
   } else {
      textctrl->WriteText(wxString(comments,wxConvLocal));
   }
   textctrl->ShowPosition(0);
   textctrl->SetInsertionPoint(0);        // needed to change pos on X11

   wxButton *closebutt = new wxButton(this, wxID_CLOSE, _("Close"));
   closebutt->SetDefault();
   
   wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
   vbox->Add(textctrl, 1, wxLEFT | wxRIGHT | wxTOP | wxEXPAND | wxALIGN_TOP, 10);
   vbox->Add(closebutt, 0, wxALL | wxALIGN_CENTER, 10);

   SetMinSize(wxSize(mininfowd, mininfoht));
   SetSizer(vbox);

   #ifdef __WXMAC__
      // expand sizer now to avoid flicker
      vbox->SetDimension(0, 0, infowd, infoht);
   #endif
   
   // need this on Linux
   textctrl->SetFocus();
}

// -----------------------------------------------------------------------------

void InfoFrame::OnActivate(wxActivateEvent& event)
{
   if ( event.GetActive() ) {
      // ensure correct menu items, esp after info window
      // is clicked while app is in background
      mainptr->UpdateMenuItems(false);
   }
   event.Skip();
}

// -----------------------------------------------------------------------------

void InfoFrame::OnCloseButton(wxCommandEvent& WXUNUSED(event))
{
   Close(true);
}

// -----------------------------------------------------------------------------

void InfoFrame::OnClose(wxCloseEvent& WXUNUSED(event))
{
   #ifdef __WXMSW__
   if (!IsIconized()) {
   #endif
      // save current location and size for later use in SavePrefs
      wxRect r = GetRect();
      infox = r.x;
      infoy = r.y;
      infowd = r.width;
      infoht = r.height;
   #ifdef __WXMSW__
   }
   #endif
   
   Destroy();        // also deletes all child windows (buttons, etc)
   infoptr = NULL;
}

// -----------------------------------------------------------------------------

#ifdef __WXMAC__
   // convert path to decomposed UTF8 so fopen will work
   #define FILEPATH filepath.fn_str()
#else
   #define FILEPATH filepath.mb_str(wxConvLocal)
#endif

void ShowInfo(const wxString& filepath)
{
   if (infoptr) {
      // info window exists so just bring it to front
      infoptr->Raise();
      #ifdef __WXX11__
         infoptr->SetFocus();    // activate window
      #endif
      return;
   }

   // buffer for receiving comment data (allocated by readcomments)
   char *commptr = NULL;

   // read and display comments in current pattern file
   const char *err = readcomments(FILEPATH, &commptr);
   if (err) {
      Warning(wxString(err,wxConvLocal));
   } else {
      infoptr = new InfoFrame(commptr);
      if (infoptr == NULL) {
         Warning(_("Could not create info window!"));
      } else {
         infoptr->Show(true);
         #ifdef __WXX11__
            // avoid wxX11 bug (probably caused by earlier SetMinSize call);
            // info window needs to be moved to infox,infoy
            infoptr->Lower();
            // don't call Yield -- doesn't work if we're generating
            while (wxGetApp().Pending()) wxGetApp().Dispatch();
            infoptr->Move(infox, infoy);
            // note that Move clobbers effect of SetMinSize!!!
            infoptr->Raise();
         #endif
      }
   }
   
   if (commptr) free(commptr);
}