File: wxinfo.cpp

package info (click to toggle)
golly 2.6-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,440 kB
  • ctags: 8,558
  • sloc: cpp: 55,629; python: 7,963; sh: 4,420; objc: 3,613; java: 2,791; ansic: 2,512; xml: 1,379; perl: 1,172; makefile: 54
file content (262 lines) | stat: -rw-r--r-- 7,797 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
/*** /
 
 This file is part of Golly, a Game of Life Simulator.
 Copyright (C) 2013 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(wxNullColour, wxNullColour,
#if defined(__WXOSX_COCOA__)
        // we need to specify facename to get Monaco instead of Courier
        wxFont(12, wxMODERN, wxNORMAL, wxNORMAL, false, wxT("Monaco")));
#elif defined(__WXMAC__)
        wxFont(11, wxMODERN, wxNORMAL, wxNORMAL));
#else
        wxFont(10, wxMODERN, wxNORMAL, wxNORMAL));
#endif
    textctrl->SetDefaultStyle(textattr);
    
    if (comments[0] == 0) {
        textctrl->WriteText(_("No comments found."));
    } else {
        textctrl->WriteText(wxString(comments,wxConvLocal));
#if defined(__WXOSX_COCOA__)        
        // sigh... wxOSX seems to ignore SetDefaultStyle
        textctrl->SetStyle(0, strlen(comments), textattr);
#endif
    }

    textctrl->ShowPosition(0);
    textctrl->SetInsertionPoint(0);
    
    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();
    }
    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();
        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);
        }
    }
    
    if (commptr) free(commptr);
}