summaryrefslogtreecommitdiffstats
path: root/beos/PicWindow.cpp
blob: 6b30f27c273dedd20f2d7cff0f988840a420b290 (plain)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <app/Application.h>
#include <interface/Bitmap.h>
#include <interface/Box.h>
#include <interface/Button.h>
#include <interface/CheckBox.h>
#include <interface/RadioButton.h>
#include <interface/Screen.h>
#include <interface/StringView.h>

#include "PicWindow.h"
#include "Stepper.h"

#define MSG_PREV   'prev'
#define MSG_NEXT   'next'
#define MSG_CLOSE  'clos'
#define MSG_WIDTH  'widt'
#define MSG_HEIGHT 'heig'
#define MSG_RADIO  'radi'

PicView::PicView( hb_handle_t * handle, int index )
    : BView( BRect( 0,0,10,10 ), NULL, B_FOLLOW_NONE, B_WILL_DRAW )
{
    fHandle = handle;

    /* Get the title and the job */
    hb_list_t * list;
    list   = hb_get_titles( fHandle );
    fTitle = (hb_title_t *) hb_list_item( list, index );
    fJob   = fTitle->job;

    /* We'll start with the first picture */
    fIndex = 0;

    /* Allocate a buffer large enough to call hb_get_preview() later */
    fRawPic = (uint8_t *) malloc( ( fTitle->width + 2 ) *
                ( fTitle->height + 2 ) * 4 );

    /* Create the RGB BBitmap we'll use to display */
    fBitmap = new BBitmap( BRect( 0, 0, fTitle->width + 1,
                                  fTitle->height + 1 ), 0, B_RGB32 );

    /* Now build the interface */
    BRect r, b;
    BBox * box;
    BButton * button;
    BStringView * stringView;

    /* Resize ourselves so the picture fits just fine */
    b = fBitmap->Bounds();
    ResizeTo( b.Width()+170, b.Height()+65 );

    /* Now build the UI around the BBitmap */
    SetViewColor( ui_color( B_PANEL_BACKGROUND_COLOR ) );

    /* "Size" box */
    b = Bounds();
    r = BRect( b.right-150,10,b.right-10,105 );
    box = new BBox( r );
    box->SetLabel( "Size" );
    AddChild( box );

    b = box->Bounds();

    /* Width */
    r = BRect( 10,15,b.right/2,35 );
    stringView = new BStringView( r, NULL, "Width:" );
    box->AddChild( stringView );
    r = BRect( b.right/2+1,15,b.right-10,35 );
    fWidthStepper = new HBStepper( r, 16, 16, fTitle->width,
        fJob->width, new BMessage( MSG_WIDTH ) );
    box->AddChild( fWidthStepper );

    /* Height */
    r = BRect( 10,40,b.right/2,60 );
    stringView = new BStringView( r, NULL, "Height:" );
    box->AddChild( stringView );
    r = BRect( b.right/2+1,40,b.right-10,60 );
    fHeightStepper = new HBStepper( r, 16, 16, fTitle->height,
        fJob->height, new BMessage( MSG_HEIGHT ) );
    box->AddChild( fHeightStepper );

    /* Aspect ratio */
    r = BRect( 10,65,b.right-10,85 );
    fRatioCheck = new BCheckBox( r, NULL, "Keep aspect ratio",
                                 new BMessage( MSG_WIDTH ) );
    fRatioCheck->SetValue( fJob->keep_ratio );
    box->AddChild( fRatioCheck );

    /* "Crop" box */
    b = Bounds();
    r = BRect( b.right-150,115,b.right-10,260 );
    box = new BBox( r );
    box->SetLabel( "Crop" );
    AddChild( box );

    b = box->Bounds();

    /* Automatic */
    r = BRect( 10,15,b.right-10,35 );
    fAutoRadio = new BRadioButton( r, NULL, "Automatic",
                                   new BMessage( MSG_RADIO ) );
    box->AddChild( fAutoRadio );

    /* Custom */
    r = BRect( 10,40,b.right-10,60 );
    fCustomRadio = new BRadioButton( r, NULL, "Custom:",
                                     new BMessage( MSG_RADIO ) );
    box->AddChild( fCustomRadio );
    float width = ( b.Width() - 30 ) / 2;
    r = BRect( (b.right-width)/2,65,(b.right+width)/2,85 );
    fCropSteppers[0] = new HBStepper( r, 2, 0, fTitle->height/2-2,
        fJob->crop[0], new BMessage( MSG_WIDTH ) );
    box->AddChild( fCropSteppers[0] );
    r = BRect( (b.right-width)/2,115,(b.right+width)/2,135 );
    fCropSteppers[1] = new HBStepper( r, 2, 0, fTitle->height/2-2,
        fJob->crop[1], new BMessage( MSG_WIDTH ) );
    box->AddChild( fCropSteppers[1] );
    r = BRect( 10,90,10+width,110 );
    fCropSteppers[2] = new HBStepper( r, 2, 0, fTitle->width/2-2,
        fJob->crop[2], new BMessage( MSG_HEIGHT ) );
    box->AddChild( fCropSteppers[2] );
    r = BRect( width+20,90,b.right-10,110 );
    fCropSteppers[3] = new HBStepper( r, 2, 0, fTitle->width/2-2,
        fJob->crop[3], new BMessage( MSG_HEIGHT ) );
    box->AddChild( fCropSteppers[3] );

    if( memcmp( fTitle->crop, fJob->crop, 4 * sizeof( int ) ) )
    {
        fCustomRadio->SetValue( 1 );
    }
    else
    {
        fAutoRadio->SetValue( 1 );
    }

    /* "Misc" box */
    b = Bounds();
    r = BRect( b.right-150,270,b.right-10,315 );
    box = new BBox( r );
    box->SetLabel( "Misc" );
    AddChild( box );

    b = box->Bounds();

    /* Deinterlace */
    r = BRect( 10,15,b.right-10,35 );
    fDeintCheck = new BCheckBox( r, NULL, "Deinterlace picture",
                                 new BMessage( MSG_WIDTH ) );
    fDeintCheck->SetValue( fJob->deinterlace );
    box->AddChild( fDeintCheck );

    b = Bounds();

    /* Next/Prev buttons */
    r = BRect( b.right-90,325,b.right-10,350 );
    fPrevButton = new BButton( r, NULL, "Previous",
                               new BMessage( MSG_PREV ) );
    AddChild( fPrevButton );
    r = BRect( b.right-90,355,b.right-10,380 );
    fNextButton = new BButton( r, NULL, "Next",
                               new BMessage( MSG_NEXT ) );
    AddChild( fNextButton );

    /* Info string and OK button */
    r = BRect( 10,b.bottom-30,b.right-100,b.bottom-10 );
    fInfoString = new BStringView( r, NULL, "" );
    AddChild( fInfoString );
    r = BRect( b.right-90,b.bottom-35,b.right-10,b.bottom-10 );
    button = new BButton( r, NULL, "OK", new BMessage( MSG_CLOSE ) );
    AddChild( button );

    /* Process and draw a first picture */
    RadioChanged();
    UpdateBitmap();
}

PicView::~PicView()
{
    free( fRawPic );
    delete fBitmap;
}

/************************************************************************
 * PicView::Draw
 ************************************************************************
 * Calls the inherited BView::Draw, plus draws the BBitmap preview
 * and the horizontal line above the info string and OK button
 ***********************************************************************/
void PicView::Draw( BRect rect )
{
    BRect b;

    BView::Draw( rect );

    if( LockLooper() )
    {
        b = fBitmap->Bounds();
        DrawBitmap( fBitmap, BRect( 10,10,b.Width()+10,b.Height()+10 ) );
        UnlockLooper();
    }

    b = Bounds();
    SetHighColor( 128,128,128 );
    StrokeLine( BPoint( 10,b.bottom-45 ), BPoint( b.right-10,b.bottom-45 ) );
    SetHighColor( 255,255,255 );
    StrokeLine( BPoint( 10,b.bottom-44 ), BPoint( b.right-10,b.bottom-44 ) );
}

void PicView::HandleMessage( BMessage * msg )
{
    switch( msg->what )
    {
        case MSG_PREV:
            fIndex--;
            UpdateBitmap();
            break;

        case MSG_NEXT:
            fIndex++;
            UpdateBitmap();
            break;

        case MSG_WIDTH:
        case MSG_HEIGHT:
            UpdateSettings( msg->what );
            UpdateBitmap();
            break;

        case MSG_RADIO:
            RadioChanged();
            break;

    }
}

void PicView::RadioChanged()
{
    int cus = fCustomRadio->Value();
    for( int i = 0; i < 4; i++ )
    {
        fCropSteppers[i]->SetEnabled( cus );
    }
    if( !cus )
    {
        memcpy( fJob->crop, fTitle->crop, 4 * sizeof( int ) );
        for( int i = 0; i < 4; i++ )
        {
            fCropSteppers[i]->SetValue( fJob->crop[i] );
        }
        UpdateSettings( MSG_WIDTH );
        UpdateBitmap();
    }
}

void PicView::UpdateSettings( uint32 what )
{
    fJob->width       = fWidthStepper->Value();
    fJob->height      = fHeightStepper->Value();
    fJob->keep_ratio  = fRatioCheck->Value();
    fJob->deinterlace = fDeintCheck->Value();
    for( int i = 0; i < 4; i++ )
    {
        fJob->crop[i] = fCropSteppers[i]->Value();
    }

    if( fJob->keep_ratio )
    {
        if( what == MSG_WIDTH )
        {
            hb_fix_aspect( fJob, HB_KEEP_WIDTH );
            if( fJob->height > fTitle->height )
            {
                fJob->height = fTitle->height;
                hb_fix_aspect( fJob, HB_KEEP_HEIGHT );
            }
        }
        else
        {
            hb_fix_aspect( fJob, HB_KEEP_HEIGHT );
            if( fJob->width > fTitle->width )
            {
                fJob->width = fTitle->width;
                hb_fix_aspect( fJob, HB_KEEP_WIDTH );
            }
        }
    }

    fWidthStepper->SetValue( fJob->width );
    fHeightStepper->SetValue( fJob->height );
}

void PicView::UpdateBitmap()
{
    /* Sanity checks */
    if( fIndex < 0 )
    {
        fIndex = 0;
        return;
    }
    if( fIndex > 9 )
    {
        fIndex = 9;
        return;
    }

    /* Enable/disable buttons */
    fPrevButton->SetEnabled( fIndex > 0 );
    fNextButton->SetEnabled( fIndex < 9 );

    /* Get new preview and copy it in our BBitmap */
    hb_get_preview( fHandle, fTitle, fIndex, fRawPic );
    for( int i = 0; i < fTitle->height + 2; i++ )
    {
        memcpy( ( (uint8_t *) fBitmap->Bits() ) +
                    i * fBitmap->BytesPerRow(),
                fRawPic + 4 * ( fTitle->width + 2 ) * i,
                4 * ( fTitle->width + 2 ) );
    }

    /* Update size info */
    char text[1024];
    snprintf( text, 1024, "Source: %dx%d, output: %dx%d",
              fTitle->width, fTitle->height,
              fJob->width, fJob->height );
    fInfoString->SetText( text );

    /* Force redraw */
    Draw( Bounds() );
}

PicWindow::PicWindow( hb_handle_t * handle, int index )
    : BWindow( BRect( 0,0,10,10 ), "Picture settings",
               B_FLOATING_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL,
               B_NOT_CLOSABLE | B_NOT_RESIZABLE | B_NOT_ZOOMABLE )
{
    /* Add the PicView */
    fView = new PicView( handle, index );
    AddChild( fView );
    
    /* Resize to fit */
    ResizeTo( fView->Bounds().Width(), fView->Bounds().Height() );
    
    /* Center */
    BScreen screen;
    MoveTo( ( screen.Frame().Width() - fView->Bounds().Width() ) / 2,
            ( screen.Frame().Height() - fView->Bounds().Height() ) / 2 );
}

void PicWindow::MessageReceived( BMessage * msg )
{
    switch( msg->what )
    {
        case MSG_PREV:
        case MSG_NEXT:
        case MSG_WIDTH:
        case MSG_HEIGHT:
        case MSG_RADIO:
            fView->HandleMessage( msg );
            break;

        case MSG_CLOSE:
            Lock();
            Quit();
            break;

        default:
            BWindow::MessageReceived( msg );
    }
}