]>
Commit | Line | Data |
---|---|---|
bbf1f0e5 KB |
1 | /* |
2 | * File: simple.cpp | |
be5a51fb | 3 | * Purpose: Minimal wxWidgets plugin |
bbf1f0e5 KB |
4 | * Author: Julian Smart |
5 | * Created: 1997 | |
6 | * Updated: | |
7 | * Copyright: (c) Julian Smart | |
8 | */ | |
9 | ||
10 | /* static const char sccsid[] = "%W% %G%"; */ | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation | |
14 | #pragma interface | |
15 | #endif | |
16 | ||
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/wx.h" | |
25 | #endif | |
26 | ||
27 | #include <fstream.h> | |
28 | ||
29 | #include "NPApp.h" | |
30 | #include "NPFrame.h" | |
31 | ||
32 | #define ID_HELLO 10 | |
33 | ||
34 | // Define a new application type | |
35 | class MyApp: public wxPluginApp | |
36 | { public: | |
37 | virtual wxFrame *OnInit(void); | |
38 | virtual wxPluginFrame* OnNewInstance(const wxPluginData& data); | |
39 | }; | |
40 | ||
41 | // Define a new frame type | |
42 | class MyFrame: public wxPluginFrame | |
43 | { public: | |
44 | MyFrame(const wxPluginData& data); | |
45 | ||
46 | public: | |
47 | // Let's paint directly onto the 'frame'; we don't need a subwindow | |
48 | void OnPaint(wxPaintEvent& event); | |
49 | void OnMouseEvent(wxMouseEvent& event); | |
50 | void OnHello(wxCommandEvent& event); | |
51 | ||
52 | // Called when the file has been downloaded | |
53 | virtual void OnNPNewFile(NPStream *stream, const wxString& fname); | |
54 | ||
55 | void CentreStrings(wxDC& dc); | |
56 | ||
57 | DECLARE_EVENT_TABLE() | |
58 | ||
59 | protected: | |
60 | wxStringList m_strings; | |
61 | float m_xpos; | |
62 | float m_ypos; | |
63 | }; | |
64 | ||
65 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
66 | EVT_SIZE(MyFrame::OnSize) | |
67 | EVT_PAINT(MyFrame::OnPaint) | |
68 | EVT_MOUSE_EVENTS(MyFrame::OnMouseEvent) | |
69 | EVT_BUTTON(ID_HELLO, MyFrame::OnHello) | |
70 | END_EVENT_TABLE() | |
71 | ||
72 | IMPLEMENT_APP(MyApp) | |
73 | ||
74 | // No app initialisation necessary, and for a plugin there is no | |
75 | // top frame. | |
76 | wxFrame *MyApp::OnInit(void) | |
77 | { | |
78 | return NULL; | |
79 | } | |
80 | ||
81 | // Called whenever a new plugin instance is called. We could check | |
82 | // various things here in 'data' but we won't bother. | |
83 | wxPluginFrame* MyApp::OnNewInstance(const wxPluginData& data) | |
84 | { | |
85 | // Implicitly added to list of plugin frames | |
86 | return new MyFrame(data); | |
87 | } | |
88 | ||
89 | // My frame constructor | |
90 | MyFrame::MyFrame(const wxPluginData& data): | |
91 | wxPluginFrame(data) | |
92 | { | |
93 | m_xpos = -1; | |
94 | m_ypos = -1; | |
95 | ||
96 | wxMenuBar *menuBar = new wxMenuBar; | |
97 | wxMenu *menu = new wxMenu; | |
98 | menu->Append(1, "E&xit"); | |
99 | menuBar->Append(menu, "&File"); | |
100 | ||
101 | SetMenuBar(menuBar); | |
102 | ||
103 | new wxTextCtrl(this, -1, "", wxPoint(10, 30), wxSize(200, 25), wxSUNKEN_BORDER); | |
104 | new wxButton(this, ID_HELLO, "Hello", wxPoint(10, 70)); | |
105 | } | |
106 | ||
107 | void MyFrame::OnPaint(wxPaintEvent& event) | |
108 | { | |
109 | wxPaintDC dc(this); | |
110 | ||
111 | dc.SetBrush(*wxCYAN_BRUSH); | |
112 | dc.SetPen(*wxRED_PEN); | |
113 | ||
114 | int w, h; | |
115 | GetClientSize(&w, &h); | |
116 | ||
117 | dc.DrawRectangle(0, 0, w, h); | |
118 | ||
119 | wxFont swissFont(10, wxSWISS, wxNORMAL, wxNORMAL); | |
120 | dc.SetFont(swissFont); | |
121 | dc.SetBackgroundMode(wxTRANSPARENT); | |
122 | ||
123 | CentreStrings(dc); | |
124 | } | |
125 | ||
126 | // Called when the file has been downloaded | |
127 | void MyFrame::OnNPNewFile(NPStream *stream, const wxString& fname) | |
128 | { | |
129 | ifstream str(fname); | |
130 | char buf[201]; | |
131 | ||
132 | while ( !str.eof() ) | |
133 | { | |
134 | buf[0] = 0; | |
135 | str.getline(buf, 200); | |
136 | ||
137 | if ( buf[0] != 0 ) | |
138 | m_strings.Add(buf); | |
139 | } | |
140 | Refresh(); | |
141 | } | |
142 | ||
143 | void MyFrame::CentreStrings(wxDC& dc) | |
144 | { | |
145 | int y = 5; | |
146 | int cw, ch; | |
147 | GetClientSize(&cw, &ch); | |
148 | ||
149 | wxNode *node = m_strings.First(); | |
150 | while ( node ) | |
151 | { | |
152 | char *s = (char *)node->Data(); | |
153 | float w, h; | |
154 | dc.GetTextExtent(s, &w, &h); | |
155 | ||
156 | int x = wxMax(0, (cw - w)/2); | |
157 | dc.DrawText(s, x, y); | |
158 | ||
159 | y += h + (h/2); | |
160 | ||
161 | node = node->Next(); | |
162 | } | |
163 | } | |
164 | ||
165 | // This implements a tiny doodling program. Drag the mouse using | |
166 | // the left button. | |
167 | void MyFrame::OnMouseEvent(wxMouseEvent& event) | |
168 | { | |
169 | float x, y; | |
170 | event.Position(&x, &y); | |
171 | wxClientDC dc(this); | |
172 | ||
173 | if (m_xpos > -1 && m_ypos > -1 && event.Dragging() && event.LeftIsDown()) | |
174 | { | |
175 | dc.SetPen(wxBLACK_PEN); | |
176 | dc.SetBrush(wxTRANSPARENT_BRUSH); | |
177 | dc.DrawLine(m_xpos, m_ypos, x, y); | |
178 | } | |
179 | m_xpos = x; | |
180 | m_ypos = y; | |
181 | } | |
182 | ||
183 | void MyFrame::OnHello(wxCommandEvent& event) | |
184 | { | |
185 | wxMessageBox("Hello!"); | |
186 | } |