Thread Kephra: Texteditor nur in Perl (538 answers)
Opened by lichtkind at 2008-03-09 00:08

pktm
 2008-07-31 05:04
#112928 #112928
User since
2003-08-07
2921 Artikel
BenutzerIn
[Homepage]
user image
Ja, dass man um das Setzen aller Dinge nicht drumherum kommt hatte ich auch schon befürchtet :-s

Weiter als bis zu diesem Test bin ich damals nicht gekommen:
Code: (dl )
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use Wx::Print;

package PrintingDemoWindow;

use strict;
use vars qw(@ISA); @ISA = qw(Wx::Panel);
use Wx qw(:sizer);
use Wx::Event qw(EVT_BUTTON);

sub new {
my $class = shift;
my $this = $class->SUPER::new( @_ );

my $top = Wx::BoxSizer->new( wxVERTICAL );
my $canvas = PrintingDemoCanvas->new( $this, -1 );

my $preview = Wx::Button->new( $this, -1, "Preview" );
my $print = Wx::Button->new( $this, -1, "Print" );

my $buttons = Wx::BoxSizer->new( wxHORIZONTAL );
$buttons->Add( $preview, 0, wxALL, 5 );
$buttons->Add( $print, 0, wxALL, 5 );

$top->Add( $canvas, 1, wxGROW );
$top->Add( $buttons, 0, wxGROW );

$this->SetSizer( $top );
$this->SetAutoLayout( 1 );

$this->{CANVAS} = $canvas;

EVT_BUTTON( $this, $preview, \&OnPreview );
EVT_BUTTON( $this, $print, \&OnPrint );

return $this;
}

sub canvas { $_[0]->{CANVAS} }

use Wx qw(wxTheApp);

sub OnPreview {
my( $this, $event ) = @_;

my $prev = PrintingDemoPrintout->new( $this->canvas, "Preview" );
my $print = PrintingDemoPrintout->new( $this->canvas, "Print" );
my $preview = Wx::PrintPreview->new( $prev, $print );
my $frame = PrintingDemoPreviewFrame->new( $preview, wxTheApp->GetTopWindow,
"Printing Demo Preview", [-1, -1], [600, -1] );
$frame->Initialize();

$frame->Show( 1 );
}

sub OnPrint {
my( $this, $event ) = @_;

my $printer = Wx::Printer->new;
my $printout = PrintingDemoPrintout->new( $this->canvas, "Print" );
$printer->Print( $this, $printout, 1 );

$printout->Destroy;
}

package PrintingDemoPreviewFrame;

use base 'Wx::PlPreviewFrame';

sub Initialize {
Wx::LogMessage( 'PrintingDemoPreviewFrame::Initialize' );

$_[0]->SUPER::Initialize;
}

sub CreateControlBar {
Wx::LogMessage( 'PrintingDemoPreviewFrame::CreateControlBar' );

$_[0]->SetPreviewControlBar
( PrintingDemoControlBar->new( $_[0]->GetPrintPreview, $_[0] ) );
$_[0]->GetPreviewControlBar->CreateButtons;
}

package PrintingDemoControlBar;

use base 'Wx::PlPreviewControlBar';

sub new {
Wx::LogMessage( 'PrintingDemoControlBar::new' );

$_[0]->SUPER::new( $_[1], 0xffffffff, $_[2], [0, 0], [400, 40] );
}

sub CreateButtons {
Wx::LogMessage( 'PrintingDemoControlBar::CreateButtons' );

shift->SUPER::CreateButtons;
}

package PrintingDemoPrintout;

use strict;
use vars qw(@ISA); @ISA = qw(Wx::Printout);

sub new {
my $class = shift;
my $canvas = shift;
my $this = $class->SUPER::new( @_ );

$this->{CANVAS} = $canvas;

return $this;
}

sub GetPageInfo {
my $this = shift;

Wx::LogMessage( "GetPageInfo" );

return ( 1, 2, 1, 2 );
}

sub HasPage {
my $this = shift;

Wx::LogMessage( "HasPage: %d", $_[0] );

return $_[0] == 1 || $_[0] == 2;
}

sub OnBeginDocument {
my $this = shift;

Wx::LogMessage( "OnBeginDocument: %d, %d", @_ );

return $this->SUPER::OnBeginDocument( @_ );
}

sub OnEndDocument {
my $this = shift;

Wx::LogMessage( "OnEndDocument" );

return $this->SUPER::OnEndDocument();
}

sub OnBeginPrinting {
my $this = shift;

Wx::LogMessage( "OnBeginPrinting" );

return $this->SUPER::OnBeginPrinting();
}

sub OnEndPrinting {
my $this = shift;

Wx::LogMessage( "OnEndPrinting" );

return $this->SUPER::OnEndPrinting();
}

sub OnPrintPage {
my( $this, $page ) = @_;
my $dc = $this->GetDC();

# we need to set the appropriate scale
my( $x_size, $y_size ) = ( $PrintingDemoCanvas::x_size,
$PrintingDemoCanvas::y_size );

my( $xmargin, $ymargin ) = ( 50, 50 );
# total size ( borders on top/bottom, left/right )
my( $xsize, $ysize ) = ( $x_size + 2 * $xmargin, $y_size + 2 * $ymargin );

# dc size
my( $xdc, $ydc ) = $dc->GetSizeWH();

# calculate the scale
my( $xscale, $yscale ) = ( $xdc / $xsize, $ydc / $ysize );
my $scale = ( $xscale < $yscale ) ? $xscale : $yscale;
# center the image
my( $xoff, $yoff ) = ( ( $xdc - ( $scale * $x_size ) ) / 2.0,
( $ydc - ( $scale * $y_size ) ) / 2.0 );

# set the DC origin / scale
$dc->SetUserScale( $scale, $scale );
$dc->SetDeviceOrigin( $xoff, $yoff );

if( $page == 1 ) { $this->{CANVAS}->OnDraw( $dc ); }
if( $page == 2 ) { } # empty page
}

package PrintingDemoCanvas;

use strict;
use vars qw(@ISA); @ISA = qw(Wx::ScrolledWindow);
use Wx qw(wxCURSOR_PENCIL wxWHITE);
use Wx::Event qw(EVT_MOTION EVT_LEFT_DOWN EVT_LEFT_UP);

use vars qw($x_size $y_size);

( $x_size, $y_size ) = ( 800, 800 );

sub new {
my $class = shift;
my $this = $class->SUPER::new( @_ );

$this->SetScrollbars( 1, 1, $x_size, $y_size );
$this->SetBackgroundColour( wxWHITE );
$this->SetCursor( Wx::Cursor->new( wxCURSOR_PENCIL ) );

EVT_MOTION( $this, \&OnMouseMove );
EVT_LEFT_DOWN( $this, \&OnButton );
EVT_LEFT_UP( $this, \&OnButton );

return $this;
}

use Wx qw(:font);
use Wx qw(:colour :pen);

sub OnDraw {
my $this = shift;
my $dc = shift;
# my $font = Wx::Font->new( 20, wxSCRIPT, wxSLANT, wxBOLD );

# $dc->SetFont( $font );
$dc->DrawRotatedText( "Draw Here", 200, 200, 35 );

$dc->DrawEllipse( 20, 20, 50, 50 );
$dc->DrawEllipse( 20, $y_size - 50 - 20, 50, 50 );
$dc->DrawEllipse( $x_size - 50 - 20, 20, 50, 50 );
$dc->DrawEllipse( $x_size - 50 - 20, $y_size - 50 - 20, 50, 50 );

$dc->SetPen( Wx::Pen->new( wxRED, 5, 0 ) );
# wxGTK does not like DrawLines in this context
foreach my $i ( @{$this->{LINES}} ) {
my $prev;

foreach my $j ( @$i ) {
if( $j != ${$i}[0] ) {
$dc->DrawLine( @$prev, @$j );
# $dc->DrawLines( $i );
}
$prev = $j;
}
}
}

sub OnMouseMove {
my( $this, $event ) = @_;

return unless $event->Dragging;

my $dc = Wx::ClientDC->new( $this );
$this->PrepareDC( $dc );
my $pos = $event->GetLogicalPosition( $dc );
my( $x, $y ) = ( $pos->x, $pos->y );

push @{$this->{CURRENT_LINE}}, [ $x, $y ];
my $elems = @{$this->{CURRENT_LINE}};

$dc->SetPen( Wx::Pen->new( wxRED, 5, 0 ) );
$dc->DrawLine( @{$this->{CURRENT_LINE}[$elems-2]},
@{$this->{CURRENT_LINE}[$elems-1]} );

}

sub OnButton {
my( $this, $event ) = @_;

my $dc = Wx::ClientDC->new( $this );
$this->PrepareDC( $dc );
my $pos = $event->GetLogicalPosition( $dc );
my( $x, $y ) = ( $pos->x, $pos->y );

if( $event->LeftUp ) {
push @{$this->{CURRENT_LINE}}, [ $x, $y ];
push @{$this->{LINES}}, $this->{CURRENT_LINE};
$this->ReleaseMouse();
} else {
$this->{CURRENT_LINE} = [ [ $x, $y ] ];
$this->CaptureMouse();
}

$dc->SetPen( Wx::Pen->new( wxRED, 5, 0 ) );
$dc->DrawLine( $x, $y, $x, $y );
}

1;



package main;

use strict;
use warnings;
use Wx::Print;
use ServusWelt;

ServusWelt->new->MainLoop; # Programminstanz erzeugen und starten


Wenn ich mich recht entsinne passiert da nichts anderes, als dass ich versucht habe ein Textfeld ausdrucken zu lassen. Im Moment wird aber noch die Canvas für den Druck-Job heran gezogen. Eigentlich sollte da aber das Zeug aus dem Textfeld rein.
http://www.intergastro-service.de (mein erstes CMS :) )

View full thread Kephra: Texteditor nur in Perl