Buttons 14 KB
Newer Older
Neil Turton's avatar
Neil Turton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright 1996 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
15

Neil Turton's avatar
Neil Turton committed
16
#if 0
17 18 19
#define debugbut(k) dprintf k
#else
#define debugbut(k) /* Disabled */
Neil Turton's avatar
Neil Turton committed
20 21 22 23 24
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
25
#include <stdint.h>
Neil Turton's avatar
Neil Turton committed
26
#include <ctype.h>
27
#include <time.h>
Neil Turton's avatar
Neil Turton committed
28 29 30 31 32 33 34 35

#include "os.h"
#include "wimp.h"
#include "wimpt.h"
#include "event.h"
#include "dbox.h"
#include "msgs.h"

36
#include "Options.h"
Neil Turton's avatar
Neil Turton committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include "malloc+.h"
#include "listfiles.h"
#include "memmanage.h"
#include "allerrs.h"
#include "actionwind.h"
#include "Buttons.h"
#include "debug.h"

#define No 0
#define Yes (!No)


static void null_activity( action_environment * );
static void pause_operation( action_environment * );
static void continue_operation( action_environment * );
52
#ifdef USE_FASTER_BUTTON
53 54
static void faster_operation( action_environment * );
#endif
Neil Turton's avatar
Neil Turton committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
static void skip_file( action_environment * );
static void operate_on_file( action_environment * );
static void to_end_of_list( action_environment * );
static void skip_operation( action_environment * );
static void retry_operation( action_environment * );
static void restart_operation( action_environment * );
static void run_object( action_environment * );
static void view_object( action_environment * );

const button_set abort_pause_buttons =
{
        {
                No,
                abort_operation,
                null_activity,
                null_activity,
                pause_operation,
72
#ifdef USE_FASTER_BUTTON
73 74
                faster_operation,
#else
Neil Turton's avatar
Neil Turton committed
75
                null_activity,
76
#endif
Neil Turton's avatar
Neil Turton committed
77 78 79 80 81
                {
                        "3",
                        "",
                        "",
                        "4",
82
#ifdef USE_FASTER_BUTTON
83 84
                        "FS"
#else
Neil Turton's avatar
Neil Turton committed
85
                        ""
86
#endif
Neil Turton's avatar
Neil Turton committed
87 88 89 90 91 92 93
                }
        },
        {
                "5",
                NULL,
                NULL,
                "6",
94
#ifdef USE_FASTER_BUTTON
95 96
                "F"
#else
Neil Turton's avatar
Neil Turton committed
97
                NULL
98
#endif
Neil Turton's avatar
Neil Turton committed
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
        }
};

const button_set confirm_buttons =
{
        {
                Yes,
                abort_operation,
                skip_file,
                operate_on_file,
                to_end_of_list,
                null_activity,
                {
                        "3",
                        "8",
                        "9",
                        "10",
                        ""
                }
        },
        {
                "5",
                "11",
                "12",
                "13",
                NULL
        }
};

const button_set ok_button =
{
        {
                Yes,
                null_activity,
                null_activity,
                null_activity,
                null_activity,
                abort_operation,
                {
                        "",
                        "",
                        "",
                        "",
                        "14"
                }
        },
        {
                NULL,
                NULL,
                NULL,
                NULL,
                "15"
        }
};

const button_set norestart_button =
{
        {
                Yes,
                abort_operation,
                null_activity,
                null_activity,
                retry_operation,
                skip_operation,
                {
                        "3",
                        "",
                        "",
                        "16",
                        "8"
                }
        },
        {
                "5",
                NULL,
                NULL,
                "17",
                "18"
        }
};

const button_set restart_button =
{
        {
                Yes,
                abort_operation,
                skip_operation,
                restart_operation,
                retry_operation,
                null_activity,
                {
                        "5",
                        "8",
                        "19",
                        "16",
                        ""
                }
        },
        {
                "5",
                "18",
                "20",
                "17",
                NULL
        }
};

const button_set abort_retry_buttons =
{
        {
                Yes,
                abort_operation,
                null_activity,
                null_activity,
                retry_operation,
                null_activity,
                {
                        "3",
                        "",
                        "",
                        "16",
                        ""
                }
        },
        {
                "5",
                NULL,
                NULL,
                "17",
                NULL
        }
};

const button_set run_view_buttons =
{
        {
                Yes,
                abort_operation,
                run_object,
                view_object,
                continue_operation,
                null_activity,
                {
                        "3",
                        "21",
                        "22",
                        "23",
                        ""
                }
        },
        {
                "5",
                "24",
                "25",
                "26",
                NULL
        }
};

const button_set open_buttons =
{
        {
                Yes,
                abort_operation,
                null_activity,
                null_activity,
                continue_operation,
                run_object,
                {
                        "3",
                        "",
                        "",
                        "23",
                        "27"
                }
        },
        {
                "5",
                NULL,
                NULL,
                "26",
                "28"
        }
};

284

Neil Turton's avatar
Neil Turton committed
285 286
static void set_button( action_environment *env, int button, const char *text )
{
287
    int dbox_handle = dbox_syshandle( env->status_box );
Neil Turton's avatar
Neil Turton committed
288 289


290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    if ( text != NULL )
    {
        /*
                Ensure button exists before setting field
        */
        wimp_iconflags flags = (wimp_iconflags) 0;
        wimp_iconflags mask = (wimp_iconflags) (wimp_IDELETED | wimp_IREDRAW);
        wimp_set_icon_state( dbox_handle, button, flags, mask );
        dbox_setfield( env->status_box, button, msgs_lookup((char *)text) );
    }
    else
    {
        wimp_redrawstr r;
        wimp_icon icondata;
        os_error *err;
Neil Turton's avatar
Neil Turton committed
305

306 307 308 309
        /*
             delete button
        */
        err = wimp_get_icon_info( dbox_handle, button, &icondata );
Neil Turton's avatar
Neil Turton committed
310

311 312 313 314 315
        if ( !err )
        {
            wimp_iconflags flags = (wimp_iconflags) (wimp_IDELETED | wimp_IREDRAW);
            wimp_iconflags mask = flags;
            wimp_set_icon_state( dbox_handle, button, flags, mask );
Neil Turton's avatar
Neil Turton committed
316

317 318
            r.w = dbox_handle;
            r.box = icondata.box;
Neil Turton's avatar
Neil Turton committed
319

320
            wimp_force_redraw( &r );
Neil Turton's avatar
Neil Turton committed
321
        }
322
    }
Neil Turton's avatar
Neil Turton committed
323 324 325 326
}

static void set_all_buttons( action_environment *env, const button_texts *texts )
{
327 328 329 330 331
    set_button( env, Abort_Button, texts->abort_text );
    set_button( env, No_Skip_Button, texts->no_skip_text );
    set_button( env, Yes_Retry_Button, texts->yes_retry_text );
    set_button( env, Misc_Button, texts->misc_text );
    set_button( env, Skip_Button, texts->skip_text );
Neil Turton's avatar
Neil Turton committed
332 333 334 335
}

void switch_buttons( action_environment *env, const button_set *new_buttons )
{
336 337
    env->button_actions = new_buttons->actions;
    set_all_buttons( env, &new_buttons->texts );
Neil Turton's avatar
Neil Turton committed
338

339 340 341 342 343 344
    if ( new_buttons->actions.requires_interaction )
    {
        /*
             Pop up the dialogue box and turn off NULL events
        */
        dbox_showstatic( env->status_box );
Neil Turton's avatar
Neil Turton committed
345

346
        show_faster_stuff( env );
Neil Turton's avatar
Neil Turton committed
347

348 349 350 351 352 353 354
        wimp_emask mask = (wimp_emask) (wimp_EMNULL | wimp_EMPTRLEAVE | wimp_EMPTRENTER | wimp_EMUSERDRAG);

        event_setmask( mask );
    }
    else
    {
        if ( !env->verbose )
Neil Turton's avatar
Neil Turton committed
355
        {
356 357
                switch_dbox_on_off( env, -1, Remove_Delay );
        }
Neil Turton's avatar
Neil Turton committed
358

359 360 361 362
        /*
                Things might have changed, so update ourselves
        */
        read_next_node_parameters( env->test_search );
Neil Turton's avatar
Neil Turton committed
363

364
        wimp_emask mask = (wimp_emask) (wimp_EMPTRLEAVE | wimp_EMPTRENTER | wimp_EMUSERDRAG);
365

366 367 368
        event_setmask( mask );
    }

369
    #ifdef USE_FASTER_BUTTON
370 371 372
    /* Ensure we get the buttons we really need */
    if ( new_buttons == &abort_pause_buttons )
    {
373
        set_faster_state( env );
374 375
    }
    #endif
Neil Turton's avatar
Neil Turton committed
376 377 378 379
}

void abort_operation( action_environment *env )
{
380
    dispose_search_context( env->test_search );
Neil Turton's avatar
Neil Turton committed
381

382
    exit( 0 );
Neil Turton's avatar
Neil Turton committed
383 384 385 386
}

static void pause_operation( action_environment *env )
{
387
    set_button( env, Misc_Button, "26" );
Neil Turton's avatar
Neil Turton committed
388

389
    env->button_actions.button_helps[ Misc_Button - Abort_Button ] = "79";
Neil Turton's avatar
Neil Turton committed
390

391
    env->button_actions.misc_action = continue_operation;
Neil Turton's avatar
Neil Turton committed
392

393
    set_top_info_field_with_current_info(env, "80a", "80");
Neil Turton's avatar
Neil Turton committed
394

395
    dbox_showstatic( env->status_box );
Neil Turton's avatar
Neil Turton committed
396

397
    wimp_emask mask = (wimp_emask) (wimp_EMNULL | wimp_EMPTRLEAVE | wimp_EMPTRENTER | wimp_EMUSERDRAG );
Neil Turton's avatar
Neil Turton committed
398

399
    event_setmask( mask );
Neil Turton's avatar
Neil Turton committed
400 401
}

402
#ifdef USE_FASTER_BUTTON
403 404 405
/* JRF: Calls the same thing as the menu option */
static void faster_operation( action_environment *env )
{
406
    toggle_faster( env );
407 408 409 410
}

void set_faster_state( action_environment *env )
{
411 412 413
    if ( !env->button_actions.requires_interaction )
    {
        if ( env->faster )
414
            set_button( env, Skip_Button, "S" );
415
        else
416
            set_button( env, Skip_Button, "F" );
417
    }
418 419 420
}
#endif

Neil Turton's avatar
Neil Turton committed
421 422
static void continue_operation( action_environment *env )
{
423
    switch_buttons( env, &abort_pause_buttons );
Neil Turton's avatar
Neil Turton committed
424 425 426 427
}

static void null_activity( action_environment *env )
{
428
    IGNORE(env);
Neil Turton's avatar
Neil Turton committed
429 430 431 432 433 434 435
}

/*
     confirm Yes button
*/
static void operate_on_file( action_environment *env )
{
436
    switch_buttons( env, &abort_pause_buttons );
Neil Turton's avatar
Neil Turton committed
437 438 439 440 441 442 443
}

/*
     confirm Quiet button
*/
static void to_end_of_list( action_environment *env )
{
444
    switch_buttons( env, &abort_pause_buttons );
Neil Turton's avatar
Neil Turton committed
445

446
    env->confirm = No;
Neil Turton's avatar
Neil Turton committed
447 448 449 450 451 452 453
}

/*
     confirm Skip button
*/
static void skip_file( action_environment *env )
{
454
    env->action = Next_File;
Neil Turton's avatar
Neil Turton committed
455

456
    skip_list_file( env->test_search );
Neil Turton's avatar
Neil Turton committed
457

458
    switch_buttons( env, &abort_pause_buttons );
Neil Turton's avatar
Neil Turton committed
459 460 461 462 463 464 465
}

/*
     error Skip button
*/
static void skip_operation( action_environment *env )
{
466 467 468 469 470 471 472
    if ( env->action == Next_File )
    {
        skip_failed_selection( env->test_search );
    }
    else
    {
        switch ( env->operation )
Neil Turton's avatar
Neil Turton committed
473
        {
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
        case Action_Copying:
        case Action_CopyMoving:
        case Action_CopyLocal:
            if ( env->action == Check_Full_Reading )
            {
                    skip_file_read();
                    switch_to_reading( env );
            }
            else if ( env->action == Check_Empty_Writing )
            {
                    skip_file_write();
                    switch_to_writing( env );
            }
            else
            {
                    env->action = Next_File;
            }
            break;

        case Action_Moving:
        case Action_Deleting:
        case Action_Setting_Access:
        case Action_Setting_Type:
        case Action_Counting:
        case Action_Stamping:
        case Action_Finding:
            env->action = Next_File;
            break;
Neil Turton's avatar
Neil Turton committed
502
        }
503
    }
Neil Turton's avatar
Neil Turton committed
504

505
    switch_buttons( env, &abort_pause_buttons );
Neil Turton's avatar
Neil Turton committed
506 507 508 509
}

static void run_object( action_environment *env )
{
510 511 512
    char *filename;
    char *command;
    os_error *err;
Neil Turton's avatar
Neil Turton committed
513

514
    err = next_nodename( env->test_search, &filename );
Neil Turton's avatar
Neil Turton committed
515

516 517 518 519 520
    if ( err )
    {
        wimpt_complain( err );
        return;
    }
Neil Turton's avatar
Neil Turton committed
521

522
    command = overflowing_malloc( strlen( "Filer_Run " ) + strlen( filename ) + 1 );
Neil Turton's avatar
Neil Turton committed
523

524 525 526 527 528
    if ( !command )
    {
        wimpt_complain( error( mb_malloc_failed ) );
        return;
    }
Neil Turton's avatar
Neil Turton committed
529

530
    sprintf( command, "Filer_Run %s", filename );
Neil Turton's avatar
Neil Turton committed
531

532
    overflowing_free( filename );
Neil Turton's avatar
Neil Turton committed
533

534
    wimpt_complain( os_cli( command ));
Neil Turton's avatar
Neil Turton committed
535

536
    overflowing_free( command );
Neil Turton's avatar
Neil Turton committed
537 538 539 540
}

static void view_object( action_environment *env )
{
541 542 543
    wimp_msgstr m;
    char *filename;
    os_error *err;
Neil Turton's avatar
Neil Turton committed
544

545
    err = next_nodename( env->test_search, &filename );
Neil Turton's avatar
Neil Turton committed
546

547 548 549 550 551
    if ( err )
    {
        wimpt_complain( err );
        return;
    }
Neil Turton's avatar
Neil Turton committed
552

553 554 555 556 557
    /*
        strrchr is guaranteed to work correctly due to the nature of the
        filename.
    */
    *strrchr( filename, '.' ) = '\0';
Neil Turton's avatar
Neil Turton committed
558

559 560 561 562 563 564
    m.hdr.size = 256;
    m.hdr.your_ref = 0;
    m.hdr.action = wimp_FilerOpenDir;
    m.data.words[ 0 ] = 0;
    m.data.words[ 1 ] = 0;
    strcpy( (char *)&m.data.words[2], filename );
Neil Turton's avatar
Neil Turton committed
565

566
    overflowing_free( filename );
Neil Turton's avatar
Neil Turton committed
567

568
    wimpt_complain( wimp_sendmessage( wimp_ESEND, &m, 0 ));
Neil Turton's avatar
Neil Turton committed
569 570 571 572 573 574 575
}

/*
     error Restart button
*/
static void restart_operation( action_environment *env )
{
576 577 578
    debugbut(( "restart_operation: env=&%8X\n", (int)env ));
    if ( env->action == Check_Full_Reading )
        restart_file_read();
Neil Turton's avatar
Neil Turton committed
579

580 581
    if ( env->action == Check_Empty_Writing )
        restart_file_write();
Neil Turton's avatar
Neil Turton committed
582

583
    switch_buttons( env, &abort_pause_buttons );
Neil Turton's avatar
Neil Turton committed
584 585 586 587 588 589 590
}

/*
     error Retry button
*/
static void retry_operation( action_environment *env )
{
591
    switch_buttons( env, &abort_pause_buttons );
Neil Turton's avatar
Neil Turton committed
592 593 594 595 596 597 598
}

/*
     This intercepts and handles the button events
*/
void button_event_handler( dbox db, void *handle )
{
599
    action_environment *env = handle;
Neil Turton's avatar
Neil Turton committed
600

601
    IGNORE(db);
Neil Turton's avatar
Neil Turton committed
602

603 604 605 606 607
    switch( dbox_get( env->status_box ))
    {
    case Abort_Button:
        env->button_actions.abort_action( env );
        break;
Neil Turton's avatar
Neil Turton committed
608

609 610 611
    case No_Skip_Button:
        env->button_actions.no_skip_action( env );
        break;
Neil Turton's avatar
Neil Turton committed
612

613 614 615
    case Yes_Retry_Button:
        env->button_actions.yes_retry_action( env );
        break;
Neil Turton's avatar
Neil Turton committed
616

617 618 619
    case Misc_Button:
        env->button_actions.misc_action( env );
        break;
Neil Turton's avatar
Neil Turton committed
620

621 622 623
    case Skip_Button:
        env->button_actions.skip_action( env );
        break;
Neil Turton's avatar
Neil Turton committed
624

625 626 627 628
    default:
        /* do nothing on other fields */
        break;
    }
Neil Turton's avatar
Neil Turton committed
629
}