File: pokemem.c

package info (click to toggle)
fuse-emulator 1.1.1%2Bdfsg1-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,120 kB
  • ctags: 8,968
  • sloc: ansic: 78,960; sh: 11,228; perl: 3,742; makefile: 1,104; yacc: 236; lex: 140
file content (656 lines) | stat: -rw-r--r-- 19,638 bytes parent folder | download
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
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
573
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/* pokemem.c: Win32 interface to the poke memory
   Copyright (c) 2011 Philip Kendall, Sergio BaldovĂ­

   $Id: pokemem.c 4785 2012-12-07 23:56:40Z sbaldovi $

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

   Author contact information:

   E-mail: philip-fuse@shadowmagic.org.uk

*/

#include <config.h>

#include <tchar.h>
#include <windows.h>

#include "fuse.h"
#include "pokemem.h"
#include "pokefinder/pokemem.h"
#include "ui/ui.h"
#include "win32internals.h"

void menu_machine_pokememory( int action );
void initialize_dialog( HWND hwnd_dialog );
void create_custom_edit( HWND parent, int item, int subitem );
void move_custom_edit( HWND hwnd_c_edit, HWND hwnd_parent );
void trainer_add( gpointer data, gpointer user_data );
void pokemem_update_list( void );
void pokemem_update_trainer( int index );
void pokemem_add_custom_poke( void );
INT_PTR CALLBACK win32ui_pokemem_proc( HWND hWnd, UINT msg, WPARAM wParam,
                                       LPARAM lParam );
LRESULT CALLBACK listview_proc( HWND hWnd, UINT msg, WPARAM wParam,
                                LPARAM lParam );
LRESULT CALLBACK edit_proc( HWND hWnd, UINT msg, WPARAM wParam,
                            LPARAM lParam );

HWND fuse_hPMWnd = NULL;
HWND hwnd_edit = NULL;
int item_edit, subitem_edit;
BOOL cancel_edit = FALSE;

void
menu_machine_pokememory( int action GCC_UNUSED )
{
  fuse_emulation_pause();

  pokemem_autoload_pokfile();

  DialogBox( fuse_hInstance, MAKEINTRESOURCE( IDD_POKEMEM ),
             fuse_hWnd, (DLGPROC) win32ui_pokemem_proc );

  fuse_emulation_unpause();
}

void
ui_pokemem_selector( const char *filename )
{
  fuse_emulation_pause();

  pokemem_read_from_file( filename );

  DialogBox( fuse_hInstance, MAKEINTRESOURCE( IDD_POKEMEM ),
             fuse_hWnd, (DLGPROC) win32ui_pokemem_proc );

  fuse_emulation_unpause();
}

INT_PTR CALLBACK
win32ui_pokemem_proc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
  switch( msg ) {
    case WM_INITDIALOG:
      initialize_dialog( hWnd );
      return TRUE;

    case WM_COMMAND:
      switch( LOWORD( wParam ) ) {
        case IDOK:
          pokemem_update_list();
          EndDialog( hWnd, wParam );
          return 0;
        case IDCLOSE:
        case IDCANCEL:
          EndDialog( hWnd, wParam );
          return 0;
        case IDC_PM_ADD:
          pokemem_add_custom_poke();
          return 0;
      }
      break;

    case WM_CLOSE:
      EndDialog( hWnd, wParam );
      return 0;

    case WM_NOTIFY:
      if( LOWORD( wParam ) == IDC_PM_LIST ) {
        NMHDR *nmhdr = (NMHDR *) lParam;
        LPNMLISTVIEW lpnmitem;

        switch( nmhdr->code ) {

          case LVN_ITEMCHANGING:
          {
            lpnmitem = (LPNMLISTVIEW) lParam;
            if( lpnmitem->uChanged & LVIF_STATE ) {
              unsigned int new_state, old_state;
              new_state = ( lpnmitem->uNewState & LVIS_STATEIMAGEMASK ) >> 12;
              old_state = ( lpnmitem->uOldState & LVIS_STATEIMAGEMASK ) >> 12;
              trainer_t *trainer = (trainer_t *)lpnmitem->lParam;

              /* Prevent the check of disabled trainers */
              if( new_state != old_state && trainer->disabled )
              {
                SetWindowLongPtr( hWnd, DWLP_MSGRESULT, TRUE );
                return TRUE;
              }
            }
            break;
          }

          case LVN_ITEMCHANGED:
          {
            lpnmitem = (LPNMLISTVIEW) lParam;

            if( lpnmitem->uChanged & LVIF_STATE && lpnmitem->iItem >= 0 ) {
              unsigned int new_state, old_state;
              new_state = ( lpnmitem->uNewState & LVIS_STATEIMAGEMASK ) >> 12;
              old_state = ( lpnmitem->uOldState & LVIS_STATEIMAGEMASK ) >> 12;
              trainer_t *trainer = (trainer_t *)lpnmitem->lParam;

              /* Trainer checked, ask for custom value if needed */
              if( new_state != old_state && new_state == 2 && !trainer->active
                  && trainer->ask_value ) {
                ListView_SetItemState( nmhdr->hwndFrom, lpnmitem->iItem,
                                       LVIS_SELECTED, LVIS_SELECTED );
                create_custom_edit( nmhdr->hwndFrom, lpnmitem->iItem, 1 );
              }

              break;
            }
          }
        }
      }
      break;
  }

  return FALSE;
}

LRESULT CALLBACK
listview_proc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
  switch( msg ) {

    case WM_DESTROY:
    {
      WNDPROC orig_proc = (WNDPROC) GetProp( hWnd, "original_proc" );
      SetWindowLongPtr( hWnd, GWLP_WNDPROC, (LONG_PTR) orig_proc );
      RemoveProp( hWnd, "original_proc" );
      break;
    }

    case WM_VSCROLL:
      /* do scroll and move edit */
      if( hwnd_edit ) {
        PostMessage( hWnd, WM_USER, (WPARAM) hwnd_edit, (LPARAM) hWnd );
      }
      break;

    case WM_HSCROLL:
      /* do scroll and move edit */
      if( hwnd_edit ) {
        PostMessage( hWnd, WM_USER, (WPARAM) hwnd_edit, (LPARAM) hWnd );
      }
      break;

    case WM_USER:
      move_custom_edit( (HWND)wParam, (HWND)lParam );
      break;

    case WM_LBUTTONDOWN:
    {
      if( hwnd_edit ) SendMessage( hwnd_edit, WM_KILLFOCUS,0,0 );

      LVHITTESTINFO itemclicked;
      itemclicked.pt.x = (long) LOWORD( lParam );
      itemclicked.pt.y = (long) HIWORD( lParam );

      int result = ListView_SubItemHitTest( hWnd, &itemclicked );

      /* Clicked on Value column? */
      if( result >= 0 && itemclicked.iSubItem ) {

        /* Get trainer */
        LV_ITEM lvi;
        memset( &lvi, 0, sizeof( lvi ) );
        lvi.mask = LVIF_PARAM;
        lvi.iItem = itemclicked.iItem;
        SendMessage( hWnd, LVM_GETITEM, 0, (LPARAM)&lvi );
        trainer_t *trainer = (trainer_t *)lvi.lParam;

        /* Ask for custom value */
        if( !trainer->active && trainer->ask_value  ) {
          ListView_SetItemState( hWnd, itemclicked.iItem, LVIS_SELECTED,
                                 LVIS_SELECTED );
          create_custom_edit( hWnd, itemclicked.iItem, 1 );
          return TRUE;
        }
      }

      break;
    }

    case WM_NOTIFY:
    {
      NMHDR *nmhdr = (NMHDR *) lParam;

      switch( nmhdr->code ) {

        case LVN_ENDLABELEDIT:
        {
          LV_DISPINFO* dispinfo = (LV_DISPINFO*)lParam;
          LVITEM lvi;
          long val;

          /* Edit cancelled? */
          if( !dispinfo->item.pszText ) return TRUE;

          memset( &lvi, 0, sizeof( lvi ) );
          lvi.iItem = dispinfo->item.iItem;
          lvi.iSubItem = dispinfo->item.iSubItem;
          lvi.pszText = ( dispinfo->item.cchTextMax > 1 )?
                        dispinfo->item.pszText : TEXT( "0" );

          /* Validate value */
          val = _ttol( lvi.pszText );
          if( val > 256 ) {
            val = 0;
            lvi.pszText = TEXT( "0" );
          }

          /* Update listview */
          SendMessage( hWnd, LVM_SETITEMTEXT, (WPARAM) lvi.iItem,
                       (LPARAM) &lvi );

          /* Update trainer */
          memset( &lvi, 0, sizeof( lvi ) );
          lvi.mask = LVIF_PARAM;
          lvi.iItem = dispinfo->item.iItem;
          SendMessage( hWnd, LVM_GETITEM, 0, (LPARAM) &lvi );
          trainer_t *trainer = (trainer_t *)lvi.lParam;
          trainer->value = val;

          return TRUE;
        }
      }
    }
  }

  WNDPROC orig_proc = (WNDPROC) GetProp( hWnd, "original_proc" );
  return CallWindowProc( orig_proc, hWnd, msg, wParam, lParam );
}

LRESULT CALLBACK
edit_proc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
  switch( msg ) {

    case WM_DESTROY:
    {
      WNDPROC orig_proc = (WNDPROC) GetProp( hWnd, "original_proc" );
      SetWindowLongPtr( hWnd, GWLP_WNDPROC, (LONG_PTR) orig_proc );
      RemoveProp( hWnd, "original_proc" );
      hwnd_edit = NULL;
      break;
    }

    case WM_GETDLGCODE:
    {
      /* Allow ESC and Return keystroke capture */
      MSG *m = (MSG *)lParam;
      if( m && ( m->wParam == VK_RETURN || m->wParam == VK_ESCAPE ) )
        return DLGC_WANTALLKEYS;
      break;
    }

    case WM_KEYDOWN:
    {
      if( wParam == VK_RETURN ) {
        /* Lost focus to confirm value */
        SetFocus( GetParent( hWnd ) );
        return TRUE;
      }
      else if ( wParam == VK_ESCAPE ) {
        /* Lost focus to cancel edit */
        cancel_edit = TRUE;
        SetFocus( GetParent( hWnd ) );
        return TRUE;
      }
      break;
    }

    case WM_KILLFOCUS:
    {
      /* Notify list end of label edit */
      LV_DISPINFO lvDispinfo;
      memset( &lvDispinfo, 0, sizeof( LV_DISPINFO ) );
      lvDispinfo.hdr.hwndFrom = hWnd;
      lvDispinfo.hdr.idFrom = GetDlgCtrlID( hWnd );
      lvDispinfo.hdr.code = LVN_ENDLABELEDIT;
      lvDispinfo.item.mask = LVIF_TEXT;
      lvDispinfo.item.iItem = item_edit;
      lvDispinfo.item.iSubItem = subitem_edit;
      lvDispinfo.item.pszText = NULL;

      if( !cancel_edit ) {
        TCHAR szEditText[4];
        GetWindowText( hWnd, szEditText, 4 );
        lvDispinfo.item.pszText = szEditText;
        lvDispinfo.item.cchTextMax = lstrlen( szEditText ) + 1;
      }
      SendMessage( GetParent( hWnd ), WM_NOTIFY, (WPARAM) IDC_PM_LIST,
                   (LPARAM) &lvDispinfo );

      DestroyWindow( hWnd );
      return TRUE;
    }
  }

  WNDPROC orig_proc = (WNDPROC) GetProp( hWnd, "original_proc" );
  return CallWindowProc( orig_proc, hWnd, msg, wParam, lParam );
}

void
initialize_dialog( HWND hwnd_dialog )
{
  HWND hwnd_list;
  RECT rect;
  int cx;

  fuse_hPMWnd = hwnd_dialog;
  hwnd_list = GetDlgItem( hwnd_dialog, IDC_PM_LIST );

  /* Replace message handler */
  WNDPROC orig_proc = (WNDPROC) GetWindowLongPtr( hwnd_list, GWLP_WNDPROC );
  SetProp( hwnd_list, "original_proc", (HANDLE) orig_proc );
  SetWindowLongPtr( hwnd_list, GWLP_WNDPROC, (LONG_PTR) (WNDPROC) listview_proc );

  /* Set text limits */
  SendDlgItemMessage( hwnd_dialog, IDC_PM_BANK_EDIT,  EM_LIMITTEXT, 1, 0 );
  SendDlgItemMessage( hwnd_dialog, IDC_PM_ADDR_EDIT,  EM_LIMITTEXT, 5, 0 );
  SendDlgItemMessage( hwnd_dialog, IDC_PM_VALUE_EDIT, EM_LIMITTEXT, 3, 0 );

  /* Set extended listview style to select full row, when an item
     is selected */
  DWORD lv_ext_style;
  lv_ext_style = SendMessage( hwnd_list, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0 );
  lv_ext_style |= LVS_EX_FULLROWSELECT;
  lv_ext_style |= LVS_EX_CHECKBOXES;
  SendMessage( hwnd_list, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, lv_ext_style );

  /* Calculate columns width, reserve space for vertical scrollbar */
  NONCLIENTMETRICS ncm;
  ncm.cbSize = sizeof( NONCLIENTMETRICS );
  SystemParametersInfo( SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0 );
  GetClientRect( hwnd_list, &rect );
  cx = rect.right - rect.left - ncm.iScrollWidth;

  /* Create trainer column */
  LVCOLUMN lvc;
  lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT;
  lvc.fmt = LVCFMT_LEFT;
  lvc.cx = cx - ( cx >> 2 );
  lvc.pszText = TEXT( "Trainer" );
  SendMessage( hwnd_list, LVM_INSERTCOLUMN, 0, (LPARAM) &lvc );

  /* Create value column */
  lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
  lvc.cx = cx >> 2;
  lvc.pszText = TEXT( "Value" );
  SendMessage( hwnd_list, LVM_INSERTCOLUMN, 1, (LPARAM) &lvc );

  /* Fill listview with data */
  if( trainer_list ) {
    /* Allocate memory */
    guint length = g_slist_length( trainer_list );
    SendMessage( hwnd_list, LVM_SETITEMCOUNT, length, 0 );

    /* Loop data model */
    g_slist_foreach( trainer_list, trainer_add, hwnd_list );
  }
}

void
trainer_add( gpointer data, gpointer user_data )
{
  trainer_t *trainer = data;
  HWND hwnd_list = (HWND) user_data;
  TCHAR buffer[81];
  int i, state;
  LV_ITEM lvi;

  if( !trainer ) return;

  /* Get count of items */
  i = SendMessage( hwnd_list, LVM_GETITEMCOUNT, 0, 0 );

  /* add trainer */
  memset( &lvi, 0, sizeof( lvi ) );
  lvi.mask = LVIF_TEXT | LVIF_PARAM;
  lvi.iItem = i;
  lvi.iSubItem = 0;
  _sntprintf( buffer, 80, "%s", trainer->name );
  buffer[80] = '\0';
  lvi.pszText = buffer;
  lvi.lParam = (LPARAM)trainer;
  SendMessage( hwnd_list, LVM_INSERTITEM, 0, (LPARAM) &lvi );

  /* add value */
  if( trainer->ask_value ) {
    _sntprintf( buffer, 80, "%d", trainer->value );
    memset( &lvi, 0, sizeof( lvi ) );
    lvi.mask = LVIF_TEXT;
    lvi.iItem = i;
    lvi.iSubItem = 1;
    lvi.pszText = buffer;
    SendMessage( hwnd_list, LVM_SETITEM, 0, (LPARAM) &lvi );
  }

  /* mark trainer checked or disabled */
  if( trainer->disabled )
    state = 0;
  else
    state = ( trainer->active )? 2 : 1;

  memset( &lvi, 0, sizeof( lvi ) );
  lvi.mask = LVIF_STATE;
  lvi.state = INDEXTOSTATEIMAGEMASK( state );
  lvi.stateMask = LVIS_STATEIMAGEMASK;
  SendMessage( hwnd_list, LVM_SETITEMSTATE, i, (LPARAM) &lvi );
}

void
create_custom_edit( HWND parent, int item, int subitem )
{
  RECT subitemrect;
  int width, height;

  /* Get item bounds */
  subitemrect.top = subitem;
  subitemrect.left = LVIR_BOUNDS;
  SendMessage( parent, LVM_GETSUBITEMRECT, item, (LPARAM) &subitemrect );
  height = subitemrect.bottom - subitemrect.top;
  width = subitemrect.right - subitemrect.left;

  /* Create custom edit */
  hwnd_edit = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "",
                              WS_CHILD | WS_VISIBLE | ES_NUMBER,
                              subitemrect.left, subitemrect.top, width, height,
                              parent, (HMENU) IDC_PM_LIST_EDIT,
                              GetModuleHandle( NULL ), NULL );
  if( !hwnd_edit ) return;
 
  /* Replace message handler */
  WNDPROC orig_proc = (WNDPROC) GetWindowLongPtr( hwnd_edit, GWLP_WNDPROC );
  SetProp( hwnd_edit, "original_proc", (HANDLE) orig_proc );
  SetWindowLongPtr( hwnd_edit, GWLP_WNDPROC, (LONG_PTR) (WNDPROC) edit_proc );

  /* Set proper font custom edit */
  HFONT hFont = (HFONT) SendMessage( parent, WM_GETFONT, 0, 0 );
  SendMessage( hwnd_edit, WM_SETFONT, (WPARAM) hFont, (LPARAM) FALSE );

  /* Set custom edit text */
  TCHAR szEditText[4];
  SendMessage( hwnd_edit, EM_LIMITTEXT, 3, 0 );
  ListView_GetItemText( parent, item, subitem, szEditText, 4 );
  SendMessage( hwnd_edit, WM_SETTEXT, 0, (LPARAM) szEditText );

  /* Focus edit control and select text */
  SetFocus( hwnd_edit );
  SendMessage( hwnd_edit, EM_SETSEL, 0, -1 );

  item_edit = item;
  subitem_edit = subitem;
  cancel_edit = FALSE;
}

void
move_custom_edit( HWND hwnd_c_edit, HWND hwnd_parent )
{
  HWND hwnd_header;
  RECT header_rect, item_rect;
  int width, height;

  /* Get current item position */
  item_rect.top = subitem_edit;
  item_rect.left = LVIR_BOUNDS;
  SendMessage( hwnd_parent, LVM_GETSUBITEMRECT, item_edit, (LPARAM) &item_rect );

  /* Get header position */
  hwnd_header = (HWND)SendMessage( hwnd_parent, LVM_GETHEADER, 0, 0 );
  GetWindowRect( hwnd_header, &header_rect );
  MapWindowPoints( HWND_DESKTOP, hwnd_parent, (LPPOINT) &header_rect, 2 );

  /* Move Edit along with item */
  if( item_rect.top >= header_rect.bottom ) {
    width = item_rect.right - item_rect.left;
    height = item_rect.bottom - item_rect.top;
    InvalidateRect( hwnd_parent, &item_rect, FALSE );
    SetWindowPos( hwnd_c_edit, NULL, item_rect.left, item_rect.top,
                  width, height, SWP_NOZORDER | SWP_SHOWWINDOW );
  }
  else {
    /* Hide custom edit when scrolls above header */
    SetWindowPos( hwnd_c_edit, NULL, 0, 0, 0, 0,
                  SWP_NOMOVE | SWP_NOZORDER | SWP_NOSIZE | SWP_HIDEWINDOW );
  }
}

void
pokemem_update_list( void )
{
  int i, items;

  /* Get count of items */
  items = SendDlgItemMessage( fuse_hPMWnd, IDC_PM_LIST, LVM_GETITEMCOUNT,
                              0, 0 );

  for( i = 0; i < items; i++ ) {
    pokemem_update_trainer( i );
  }
}

void
pokemem_update_trainer( int index )
{
  int selected;
  trainer_t *trainer;
  LV_ITEM lvi;

  memset( &lvi, 0, sizeof( lvi ) );
  lvi.mask = LVIF_STATE | LVIF_PARAM;
  lvi.iItem = index;
  lvi.stateMask = LVIS_STATEIMAGEMASK;
  SendDlgItemMessage( fuse_hPMWnd, IDC_PM_LIST, LVM_GETITEM, 0, (LPARAM)&lvi );

  trainer = (trainer_t *) lvi.lParam;
  selected = ( ( lvi.state >> 12 ) == 2 );

  if( selected ) {
    pokemem_trainer_activate( trainer );
  } else {
    pokemem_trainer_deactivate( trainer );
  }
}

void
pokemem_add_custom_poke( void )
{
  long b, a, v;
  TCHAR buffer[8];
  int length, buffer_size = 8;
  HWND hwnd_control;
  trainer_t *trainer;

  /* Parse bank */
  length = SendDlgItemMessage( fuse_hPMWnd, IDC_PM_BANK_EDIT, WM_GETTEXT,
                               (WPARAM) buffer_size, (LPARAM) buffer );

  b = ( length )? _ttol( buffer ) : 8;

  if( b < 0 || b > 8 ) {
    ui_error( UI_ERROR_ERROR, "Invalid bank: use a number from 0 to 8" );
    hwnd_control = GetDlgItem( fuse_hPMWnd, IDC_PM_BANK_EDIT );
    SendMessage( fuse_hPMWnd, WM_NEXTDLGCTL, (WPARAM) hwnd_control, TRUE );
    return;
  }

  /* Parse address */
  length = SendDlgItemMessage( fuse_hPMWnd, IDC_PM_ADDR_EDIT, WM_GETTEXT,
                               (WPARAM) buffer_size, (LPARAM) buffer );

  /* TODO: accept hex address */
  a = ( length )? _ttol( buffer ) : 0;

  if( !length || a < 0 || a > 65535  ) {
    ui_error( UI_ERROR_ERROR,
              "Invalid address: use a number from 0 to 65535" );
    hwnd_control = GetDlgItem( fuse_hPMWnd, IDC_PM_ADDR_EDIT );
    SendMessage( fuse_hPMWnd, WM_NEXTDLGCTL, (WPARAM) hwnd_control, TRUE );
    return;
  }

  /* Parse value */
  length = SendDlgItemMessage( fuse_hPMWnd, IDC_PM_VALUE_EDIT, WM_GETTEXT,
                               (WPARAM) buffer_size, (LPARAM) buffer );

  v = ( length )? _ttol( buffer ) : 0;

  if( !length || v < 0 || v > 256 ) {
    ui_error( UI_ERROR_ERROR, "Invalid value: use a number from 0 to 256" );
    hwnd_control = GetDlgItem( fuse_hPMWnd, IDC_PM_VALUE_EDIT );
    SendMessage( fuse_hPMWnd, WM_NEXTDLGCTL, (WPARAM) hwnd_control, TRUE );
    return;
  }

  /* Updadate model and view */
  trainer = pokemem_trainer_list_add( b, a, v );
  if( !trainer ) {
    ui_error( UI_ERROR_ERROR, "Cannot add trainer" );
    return;
  }

  hwnd_control = GetDlgItem( fuse_hPMWnd, IDC_PM_LIST );
  trainer_add( trainer, hwnd_control );

  /* Mark custom trainer for activate */
  if( !trainer->active && !trainer->disabled ) {
    LV_ITEM lvi;
    int index;

    memset( &lvi, 0, sizeof( lvi ) );
    lvi.mask = LVIF_STATE;
    lvi.state = INDEXTOSTATEIMAGEMASK( 2 );
    lvi.stateMask = LVIS_STATEIMAGEMASK;
    index = SendDlgItemMessage( fuse_hPMWnd, IDC_PM_LIST, LVM_GETITEMCOUNT,
                                0, 0 ) - 1;
    SendDlgItemMessage( fuse_hPMWnd, IDC_PM_LIST, LVM_SETITEMSTATE,
                        (WPARAM) index, (LPARAM) &lvi );
  }

  /* Clear custom fields */
  SetDlgItemText( fuse_hPMWnd, IDC_PM_BANK_EDIT,  NULL );
  SetDlgItemText( fuse_hPMWnd, IDC_PM_ADDR_EDIT,  NULL );
  SetDlgItemText( fuse_hPMWnd, IDC_PM_VALUE_EDIT, NULL );

  /* Focus for new input */
  hwnd_control = GetDlgItem( fuse_hPMWnd, IDC_PM_ADDR_EDIT );
  SendMessage( fuse_hPMWnd, WM_NEXTDLGCTL, (WPARAM) hwnd_control, TRUE );
}