Buttons 14.7 KB
Newer Older
Neil Turton's avatar
Neil Turton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* 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.
 */
#if 0
#define debugbut
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
22
#include <stdint.h>
Neil Turton's avatar
Neil Turton committed
23 24 25 26 27 28 29 30 31
#include <ctype.h>

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

32
#include "Options.h"
Neil Turton's avatar
Neil Turton committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#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 * );
48
#ifdef USE_FASTER_BUTTON
49 50
static void faster_operation( action_environment * );
#endif
Neil Turton's avatar
Neil Turton committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
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,
68
#ifdef USE_FASTER_BUTTON
69 70
                faster_operation,
#else
Neil Turton's avatar
Neil Turton committed
71
                null_activity,
72
#endif
Neil Turton's avatar
Neil Turton committed
73 74 75 76 77
                {
                        "3",
                        "",
                        "",
                        "4",
78
#ifdef USE_FASTER_BUTTON
79 80
                        "FS"
#else
Neil Turton's avatar
Neil Turton committed
81
                        ""
82
#endif
Neil Turton's avatar
Neil Turton committed
83 84 85 86 87 88 89
                }
        },
        {
                "5",
                NULL,
                NULL,
                "6",
90
#ifdef USE_FASTER_BUTTON
91 92
                "F"
#else
Neil Turton's avatar
Neil Turton committed
93
                NULL
94
#endif
Neil Turton's avatar
Neil Turton committed
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
        }
};

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"
        }
};

280

Neil Turton's avatar
Neil Turton committed
281 282
static void set_button( action_environment *env, int button, const char *text )
{
283
    int dbox_handle = dbox_syshandle( env->status_box );
Neil Turton's avatar
Neil Turton committed
284 285


286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    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
301

302 303 304 305
        /*
             delete button
        */
        err = wimp_get_icon_info( dbox_handle, button, &icondata );
Neil Turton's avatar
Neil Turton committed
306

307 308 309 310 311
        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
312

313 314
            r.w = dbox_handle;
            r.box = icondata.box;
Neil Turton's avatar
Neil Turton committed
315

316
            wimp_force_redraw( &r );
Neil Turton's avatar
Neil Turton committed
317
        }
318
    }
Neil Turton's avatar
Neil Turton committed
319 320 321 322
}

static void set_all_buttons( action_environment *env, const button_texts *texts )
{
323 324 325 326 327
    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
328 329 330 331
}

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

335 336 337 338 339 340
    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
341

342
        show_faster_stuff( env );
Neil Turton's avatar
Neil Turton committed
343

344 345 346 347 348 349 350
        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
351
        {
352 353
                switch_dbox_on_off( env, -1, Remove_Delay );
        }
Neil Turton's avatar
Neil Turton committed
354

355 356 357 358
        /*
                Things might have changed, so update ourselves
        */
        read_next_node_parameters( env->test_search );
Neil Turton's avatar
Neil Turton committed
359

360
        wimp_emask mask = (wimp_emask) (wimp_EMPTRLEAVE | wimp_EMPTRENTER | wimp_EMUSERDRAG);
361

362 363 364
        event_setmask( mask );
    }

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

void abort_operation( action_environment *env )
{
        dispose_search_context( env->test_search );

        exit( 0 );
}

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

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

387
    env->button_actions.misc_action = continue_operation;
Neil Turton's avatar
Neil Turton committed
388

389
    set_top_info_field_with_current_info(env, "80a", "80");
Neil Turton's avatar
Neil Turton committed
390

391
    dbox_showstatic( env->status_box );
Neil Turton's avatar
Neil Turton committed
392

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

395
    event_setmask( mask );
Neil Turton's avatar
Neil Turton committed
396 397
}

398
#ifdef USE_FASTER_BUTTON
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
/* JRF: Calls the same thing as the menu option */
static void faster_operation( action_environment *env )
{
        toggle_faster( env );
}

void set_faster_state( action_environment *env )
{
        if ( !env->button_actions.requires_interaction )
        {
          if ( env->faster )
            set_button( env, Skip_Button, "S" );
          else
            set_button( env, Skip_Button, "F" );
        }
}
#endif

Neil Turton's avatar
Neil Turton committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 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 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
static void continue_operation( action_environment *env )
{
        switch_buttons( env, &abort_pause_buttons );
}

static void null_activity( action_environment *env )
{
        env = env;     /* keep the compiler quiet */
}

/*
     confirm Yes button
*/
static void operate_on_file( action_environment *env )
{
        switch_buttons( env, &abort_pause_buttons );
}

/*
     confirm Quiet button
*/
static void to_end_of_list( action_environment *env )
{
        switch_buttons( env, &abort_pause_buttons );

        env->confirm = No;
}

/*
     confirm Skip button
*/
static void skip_file( action_environment *env )
{
        env->action = Next_File;

        skip_list_file( env->test_search );

        switch_buttons( env, &abort_pause_buttons );
}

/*
     error Skip button
*/
static void skip_operation( action_environment *env )
{
        if ( env->action == Next_File )
        {
                skip_failed_selection( env->test_search );
        }
        else
        {
                switch ( env->operation )
                {
                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;
                }
        }

        switch_buttons( env, &abort_pause_buttons );
}

static void run_object( action_environment *env )
{
        char *filename;
        char *command;
        os_error *err;

        err = next_nodename( env->test_search, &filename );

        if ( err )
        {
                wimpt_complain( err );
                return;
        }

        command = overflowing_malloc( strlen( "Filer_Run " ) + strlen( filename ) + 1 );

        if ( !command )
        {
                wimpt_complain( error( mb_malloc_failed ) );
                return;
        }

        sprintf( command, "Filer_Run %s", filename );

        overflowing_free( filename );

        wimpt_complain( os_cli( command ));

        overflowing_free( command );
}

static void view_object( action_environment *env )
{
        wimp_msgstr m;
        char *filename;
        os_error *err;

        err = next_nodename( env->test_search, &filename );

        if ( err )
        {
                wimpt_complain( err );
                return;
        }

        /*
                strrchr is guaranteed to work correctly due to the nature of the
                filename.
        */
        *strrchr( filename, '.' ) = '\0';

        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 );

        overflowing_free( filename );

        wimpt_complain( wimp_sendmessage( wimp_ESEND, &m, 0 ));
}

/*
     error Restart button
*/
static void restart_operation( action_environment *env )
{
#ifdef debugbut
573
        DEBUG(( "restart_operation: env=&%8X\n", (int)env ));
Neil Turton's avatar
Neil Turton committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
#endif
        if ( env->action == Check_Full_Reading )
                restart_file_read();

        if ( env->action == Check_Empty_Writing )
                restart_file_write();

        switch_buttons( env, &abort_pause_buttons );
}

/*
     error Retry button
*/
static void retry_operation( action_environment *env )
{
        switch_buttons( env, &abort_pause_buttons );
}

/*
     This intercepts and handles the button events
*/
void button_event_handler( dbox db, void *handle )
{
        action_environment *env = handle;

        db = db; /* keep the compiler quiet */

        switch( dbox_get( env->status_box ))
        {
        case Abort_Button:
                env->button_actions.abort_action( env );
                break;

        case No_Skip_Button:
                env->button_actions.no_skip_action( env );
                break;

        case Yes_Retry_Button:
                env->button_actions.yes_retry_action( env );
                break;

        case Misc_Button:
                env->button_actions.misc_action( env );
                break;

        case Skip_Button:
                env->button_actions.skip_action( env );
                break;

        default:
                /* do nothing on other fields */
                break;
        }
}