AltRename 7.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright 2007 Castle Technology 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 16 17
#include <swis.h>
#include <string.h>
#include "os.h"
18 19 20
#include "bbc.h"
#include "wimpt.h"
#include "werr.h"
21 22 23
#include "sprite.h"

#include "AltRename.h"
24
#include "PSprite.h"
25 26
#include "PaintLib.h"
#include "main.h"
27
#include "SprWindow.h"
28 29
#include "akbd.h"

Robert Sprowson's avatar
Robert Sprowson committed
30
static wimp_i        rename_icon = -1;
31
static char          rename_buffer[16];
32 33
static main_window  *rename_window;
static main_sprite  *rename_sprite;
34 35 36
static wimp_box      rename_bbox;
static int           rename_sprites_per_row;

Robert Sprowson's avatar
Robert Sprowson committed
37 38 39
static void altrename_renamesprite (main_sprite *sprite, char *newname)

{ sprite_id sid;
40 41 42 43
  char name[NAME_LIMIT+1];
  sid.tag = sprite_id_name; /*doesn't flex*/
  sid.s.name = newname;

Robert Sprowson's avatar
Robert Sprowson committed
44
  if (*newname == 0) return;
45

Robert Sprowson's avatar
Robert Sprowson committed
46 47 48
  strncpy (name, psprite_address (sprite)->name, NAME_LIMIT);
  name[NAME_LIMIT] = 0;
  sid.s.name = name;
49 50 51 52
  if (wimpt_complain (sprite_rename
      (sprite->file->spritearea, &sid, newname)) !=
      NULL) return;

Robert Sprowson's avatar
Robert Sprowson committed
53
  main_set_modified (sprite->file);
54 55 56 57 58
  sprwindow_set_window_titles (sprite);

  /* rename must reset the titles of all the windows open on
    the sprite */
  if (sprite->colourdialogue != NULL)
ROOL's avatar
ROOL committed
59
  { char new_title [128];
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    colourpicker_dialogue dialogue;

    dialogue.title = new_title;
    sprintf (new_title, msgs_lookup ("PntW7"), newname);
    ftracef0 ("ColourPicker_UpdateDialogue: setting new title");
    wimpt_complain (os_swix3 (ColourPicker_UpdateDialogue,
        colourpicker_UPDATE_TITLE, sprite->colourdialogue,
        &dialogue));
  }
  else if (sprite->colourhandle != NULL)
  { wimp_winfo winfo;
    wimp_redrawstr redraw_str;
    wimp_wstate wstate;

    winfo.w = sprite->colourhandle;
Robert Sprowson's avatar
Robert Sprowson committed
75
    wimpt_noerr (paintlib_get_wind_info (&winfo));
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

    sprintf (winfo.info.title.indirecttext.buffer,
        msgs_lookup ("PntW7"), newname);

    /*Force redraw of the title bar.*/
    wimpt_noerr (wimp_get_wind_state
        (sprite->colourhandle, &wstate));

    redraw_str.w = sprite->colourhandle;
    wimpt_noerr (wimp_getwindowoutline (&redraw_str));

    redraw_str.w = -1;
    redraw_str.box.y0 = wstate.o.box.y1;
    wimpt_noerr (wimp_force_redraw (&redraw_str));
  }
ROOL's avatar
ROOL committed
91 92 93 94 95 96 97 98 99 100 101
  if (sprite->bgcolourdialogue != NULL)
  { char new_title [128];
    colourpicker_dialogue dialogue;

    dialogue.title = new_title;
    sprintf (new_title, msgs_lookup ("PntWF"), newname);
    ftracef0 ("ColourPicker_UpdateDialogue: setting new bgcolour title");
    wimpt_complain (os_swix3 (ColourPicker_UpdateDialogue,
        colourpicker_UPDATE_TITLE, sprite->bgcolourdialogue,
        &dialogue));
  }
102 103
}

Robert Sprowson's avatar
Robert Sprowson committed
104 105 106 107
void altrename_delete (void)

{ wimp_redrawstr ds;

108
  if (!rename_window) return;
Robert Sprowson's avatar
Robert Sprowson committed
109 110 111 112
  wimp_delete_icon (rename_window->handle, rename_icon);
  ds.w = rename_window->handle;
  ds.box = rename_bbox;
  wimp_force_redraw (&ds);
113

Robert Sprowson's avatar
Robert Sprowson committed
114
  rename_window = NULL;
115 116
}

Robert Sprowson's avatar
Robert Sprowson committed
117 118 119
static int altrename_sprites_per_row (int *toggled)

{ wimp_wstate ws;
120 121 122 123 124 125 126 127 128
  int width;

  if (!rename_window) return 0;

  if (rename_window->data->file.fullinfo)
    width = main_FILER_FullInfoWidth;
  else
    width = main_FILER_TotalWidth;

Robert Sprowson's avatar
Robert Sprowson committed
129
  wimp_get_wind_state(rename_window->handle, &ws);
130

Robert Sprowson's avatar
Robert Sprowson committed
131
  if (toggled) *toggled = (ws.flags & wimp_WCLICK_TOGGLE);
132

Robert Sprowson's avatar
Robert Sprowson committed
133
  width = (ws.o.box.x1-ws.o.box.x0)/width;
134

Robert Sprowson's avatar
Robert Sprowson committed
135
  return width ? width : 1;
136 137
}

Robert Sprowson's avatar
Robert Sprowson committed
138 139 140
void altrename_start (main_window *window, main_sprite *sprite)

{ wimp_box bbox;
141 142 143
  psprite_info sinfo;
  wimp_icreate icon;
  wimp_wstate ws;
Robert Sprowson's avatar
Robert Sprowson committed
144 145 146
  int y, scy;
  wimp_redrawstr ds;
  wimp_caretstr  caret;
147

Robert Sprowson's avatar
Robert Sprowson committed
148
  if (!window || !sprite) return;
149 150
  if (window==rename_window && sprite==rename_sprite) return;

Robert Sprowson's avatar
Robert Sprowson committed
151
  altrename_delete();
152

Robert Sprowson's avatar
Robert Sprowson committed
153
  if (!main_get_all_sprite_bboxes (window, sprite, &bbox, 0, &icon.i.box))
154 155 156
    return;

  if (window->data->file.fullinfo)
Robert Sprowson's avatar
Robert Sprowson committed
157
    icon.i.box.x1 = icon.i.box.x0+12*main_FILER_TextWidth;
158
  else
Robert Sprowson's avatar
Robert Sprowson committed
159 160
  { icon.i.box.x0 = bbox.x0;
    icon.i.box.x1 = bbox.x1;
161 162
  }

Robert Sprowson's avatar
Robert Sprowson committed
163 164 165 166 167
  icon.w = window->handle;
  icon.i.flags = (wimp_iconflags)(wimp_ITEXT | wimp_INDIRECT | wimp_IHCENTRE |
                                 wimp_IFILLED | wimp_IBORDER |
                                 (7 * wimp_IFORECOL) | (0 * wimp_IBACKCOL) |
                                 (wimp_BWRITABLE * wimp_IBTYPE));
168

Robert Sprowson's avatar
Robert Sprowson committed
169 170 171
  icon.i.data.indirecttext.buffer = rename_buffer;
  icon.i.data.indirecttext.validstring = "";
  icon.i.data.indirecttext.bufflen = NAME_LIMIT+1;
172
  psprite_read_full_info (sprite, &sinfo);
Robert Sprowson's avatar
Robert Sprowson committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
  strcpy (rename_buffer, sinfo.name);

  if (wimpt_complain (wimp_create_icon (&icon, &rename_icon))) return;

  rename_window = window;
  rename_sprite = sprite;
  ds.w = icon.w;
  ds.box = icon.i.box;
  rename_bbox = ds.box;
  rename_sprites_per_row = altrename_sprites_per_row (0);
  caret.w = window->handle;
  caret.i = rename_icon;
  caret.x = caret.y = 0;
  caret.height = -1;
  caret.index = strlen (sinfo.name);
  wimp_set_caret_pos (&caret);
  wimp_force_redraw (&ds);
  wimp_get_wind_state (window->handle,&ws);

  scy = ws.o.y;
  y = WORKAREA_TO_SCREEN_Y(&ws, bbox.y1);
194
  if (y>ws.o.box.y1)
Robert Sprowson's avatar
Robert Sprowson committed
195
    ws.o.y += (y-ws.o.box.y1);
196

Robert Sprowson's avatar
Robert Sprowson committed
197
  y = WORKAREA_TO_SCREEN_Y(&ws, bbox.y0);
198
  if (y<ws.o.box.y0)
Robert Sprowson's avatar
Robert Sprowson committed
199
    ws.o.y += (y-ws.o.box.y0);
200

Robert Sprowson's avatar
Robert Sprowson committed
201
  if (scy != ws.o.y) wimp_open_wind (&ws.o);
202 203
}

Robert Sprowson's avatar
Robert Sprowson committed
204 205 206
BOOL altrename_claim_event (wimp_eventstr *e, main_window *window)

{ if (!rename_window) return FALSE;
207 208 209 210
  switch (e->e)
  {
    case wimp_EBUT:
      if (window->handle!=rename_window->handle ||
211
          (e->data.but.m.bbits & wimp_BMID))
Robert Sprowson's avatar
Robert Sprowson committed
212 213
      { altrename_delete ();
        break;
214 215
      }
      if ( e->data.but.m.i==rename_icon &&
Robert Sprowson's avatar
Robert Sprowson committed
216
           window->handle==rename_window->handle) return TRUE;
217 218

      if (e->data.but.m.bbits & (wimp_BCLICKLEFT | wimp_BCLICKRIGHT))
Robert Sprowson's avatar
Robert Sprowson committed
219
      { altrename_delete ();
220 221
        break;
      }
Robert Sprowson's avatar
Robert Sprowson committed
222 223
      return TRUE;

224
    case wimp_EOPEN:
Robert Sprowson's avatar
Robert Sprowson committed
225
    { int wid;
226
      if (window->handle==rename_window->handle)
Robert Sprowson's avatar
Robert Sprowson committed
227 228 229 230
      { int toggled;
        wid = altrename_sprites_per_row (&toggled);
        if (wid != rename_sprites_per_row || toggled)
          altrename_delete();
231 232 233
      }
      break;
    }
Robert Sprowson's avatar
Robert Sprowson committed
234

235 236 237 238
    case wimp_EKEY:
      if (window->handle!=rename_window->handle) break;
      switch (e->data.key.chcode)
      {
Robert Sprowson's avatar
Robert Sprowson committed
239 240 241 242
        case 13:
          altrename_renamesprite (rename_sprite, rename_buffer);
          if (!akbd_pollctl ())
          { altrename_delete ();
243 244 245 246
            break;
          }
          /*fall through*/

Robert Sprowson's avatar
Robert Sprowson committed
247 248
        case 0x18A: /* TAB -  next sprite */
          { main_sprite *sprite=rename_sprite->link;
249
            if (sprite==0) sprite=rename_window->data->file.sprites;
Robert Sprowson's avatar
Robert Sprowson committed
250
            altrename_start (rename_window, sprite);
251 252
          }
          break;
Robert Sprowson's avatar
Robert Sprowson committed
253

254
        case 0x19A: /* Shift TAB -  previous sprite */
Robert Sprowson's avatar
Robert Sprowson committed
255 256 257 258 259
          { main_sprite *sprite=rename_sprite;
            for (sprite = rename_window->data->file.sprites;
                 !(sprite->link == NULL || sprite->link == rename_sprite);
                 sprite = sprite->link);
            altrename_start (rename_window, sprite);
260 261 262 263
          }
          break;
      }
  }
Robert Sprowson's avatar
Robert Sprowson committed
264
  return FALSE;
265
}