Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

aqfinance / src / fox / lib / modules / accounts / fm_accpage.cpp @ 242027ca

History | View | Annotate | Download (7.87 KB)

1
/****************************************************************************
2
 * This file is part of the project AqFinance.
3
 * AqFinance (c) by 2009 Martin Preuss, all rights reserved.
4
 *
5
 * The license for this file can be found in the file COPYING which you
6
 * should have received along with this file.
7
 ****************************************************************************/
8

    
9
#ifdef HAVE_CONFIG_H
10
# include <config.h>
11
#endif
12

    
13

    
14
#include "ff_app.hpp"
15
#include "fm_accounts.hpp"
16
#include "fm_accpage.hpp"
17
#include "fm_accaccpage.hpp"
18
#include "fm_accstmpage.hpp"
19

    
20
#include <gwen-gui-fox16/theme.h>
21

    
22
#include <gwenhywfar/debug.h>
23
#include <gwenhywfar/i18n.h>
24

    
25

    
26
#define I18N(msg) GWEN_I18N_Translate(PACKAGE, msg)
27

    
28

    
29

    
30
FXDEFMAP(FM_AccountPage) FM_AccountPageMap[]= {
31
  FXMAPFUNC(SEL_COMMAND, FM_AccountPage::ID_TABBOOK,
32
            FM_AccountPage::onTabClicked),
33
  FXMAPFUNC(SEL_LEFTBUTTONPRESS, FM_AccountPage::ID_TABITEM,
34
            FM_AccountPage::onTabLeftButtonPressed),
35
};
36

    
37

    
38

    
39
FXIMPLEMENT(FM_AccountPage, FF_ModTabBook, FM_AccountPageMap, ARRAYNUMBER(FM_AccountPageMap))
40

    
41

    
42

    
43

    
44

    
45
FM_AccountPage::FM_AccountPage(FM_Accounts *modAccounts,
46
                               FXComposite *p,
47
                               FXuint opts,
48
                               FXint x, FXint y, FXint w, FXint h)
49
:FF_ModTabBook(modAccounts, p, opts, x, y, w, h)
50
, m_accountView(NULL)
51
, m_lastEnteredTab(-1)
52
, m_config(NULL)
53
, m_transactionMarkId(0)
54
, m_closeTabIcon(NULL)
55
{
56
  FF_App *app;
57

    
58
  app=dynamic_cast<FF_App *>(getApp());
59

    
60
  m_closeTabIcon=app->getIcon("closetab");
61

    
62
  /* create tab 1 */
63
  addTabHeader(I18N("Accounts"), false);
64
  m_accountView=new FM_AccountAccPage("accountTree", this, LAYOUT_FILL_X | LAYOUT_FILL_Y);
65
  addTabPage(m_accountView, false);
66
}
67

    
68

    
69

    
70
FM_AccountPage::FM_AccountPage()
71
:FF_ModTabBook()
72
, m_accountView(NULL)
73
, m_lastEnteredTab(-1)
74
, m_config(NULL)
75
, m_transactionMarkId(0)
76
, m_closeTabIcon(NULL)
77
{
78
}
79

    
80

    
81

    
82
FM_AccountPage::~FM_AccountPage()
83
{
84
}
85

    
86

    
87

    
88
void FM_AccountPage::create()
89
{
90
  FXHorizontalFrame::create();
91
  if (m_closeTabIcon)
92
    m_closeTabIcon->create();
93
}
94

    
95

    
96

    
97
void FM_AccountPage::init(GWEN_DB_NODE *db)
98
{
99
  DBG_INFO(0, "Init");
100
  m_config=GWEN_DB_Group_dup(db);
101
  std::list<FM_AccountStmPage *>::iterator it;
102

    
103
  m_accountView->init(m_config);
104
  for (it=m_accountPages.begin(); it!=m_accountPages.end(); it++)
105
    (*it)->init(m_config);
106

    
107
}
108

    
109

    
110

    
111
void FM_AccountPage::fini(GWEN_DB_NODE *db)
112
{
113
  std::list<FM_AccountStmPage *>::iterator it;
114

    
115
  DBG_INFO(0, "Fini");
116

    
117
  /* list open accounts */
118
  GWEN_DB_DeleteVar(m_config, "openAccounts");
119
  for (it=m_accountPages.begin(); it!=m_accountPages.end(); it++)
120
    GWEN_DB_SetIntValue(m_config, GWEN_DB_FLAGS_DEFAULT, "openAccounts",
121
                        (*it)->getBankAccountId());
122

    
123
  for (it=m_accountPages.begin(); it!=m_accountPages.end(); it++)
124
    (*it)->fini(m_config);
125
  m_accountView->fini(m_config);
126

    
127
  GWEN_DB_ClearGroup(db, NULL);
128
  GWEN_DB_AddGroupChildren(db, m_config);
129

    
130
  GWEN_DB_Group_free(m_config);
131
  m_config=NULL;
132
}
133

    
134

    
135

    
136
void FM_AccountPage::dbOpened(AE_BOOK *bk)
137
{
138
  std::list<FM_AccountStmPage *>::iterator it;
139

    
140
  m_accountView->dbOpened(bk);
141

    
142
  for (it=m_accountPages.begin(); it!=m_accountPages.end(); it++) {
143
    (*it)->dbOpened(bk);
144
  }
145
}
146

    
147

    
148

    
149
void FM_AccountPage::dbClosing()
150
{
151
  std::list<FM_AccountStmPage *>::iterator it;
152

    
153
  m_accountView->dbClosing();
154

    
155
  for (it=m_accountPages.begin(); it!=m_accountPages.end(); it++) {
156
    (*it)->dbClosing();
157
  }
158
}
159

    
160

    
161

    
162
void FM_AccountPage::dbClosed()
163
{
164
  std::list<FM_AccountStmPage *>::iterator it;
165

    
166
  m_accountView->dbClosed();
167

    
168
  for (it=m_accountPages.begin(); it!=m_accountPages.end(); it++) {
169
    (*it)->dbClosed();
170
  }
171
}
172

    
173

    
174

    
175
void FM_AccountPage::dbReload(AE_BOOK_TABLE_TYPE tt)
176
{
177
  std::list<FM_AccountStmPage *>::iterator it;
178

    
179
  m_accountView->dbReload(tt);
180

    
181
  for (it=m_accountPages.begin(); it!=m_accountPages.end(); it++) {
182
    (*it)->dbReload(tt);
183
  }
184
}
185

    
186

    
187

    
188
void FM_AccountPage::aboutToSendJobs()
189
{
190
  FF_App *app;
191
  AE_BOOK *b;
192
  std::list<FM_AccountStmPage *>::iterator it;
193

    
194
  app=dynamic_cast<FF_App *>(getApp());
195
  b=app->getBook();
196
  if (b) {
197
    int rv;
198
    AQDB_ID id;
199

    
200
    rv=AE_Book_GetLastId(b, AE_Book_TableType_Transaction, &id);
201
    if (rv<0) {
202
      DBG_ERROR(0, "here (%d)", rv);
203
    }
204
    else {
205
      m_transactionMarkId=id;
206
    }
207
  }
208

    
209
  m_accountView->aboutToSendJobs();
210
  for (it=m_accountPages.begin(); it!=m_accountPages.end(); it++) {
211
    (*it)->aboutToSendJobs();
212
  }
213
}
214

    
215

    
216

    
217
long FM_AccountPage::onTabClicked(FXObject *, FXSelector, void *ptr)
218
{
219
  FXint idx=(FXint)(FXival)ptr;
220
  FM_AccountStmPage *ap;
221

    
222
  if (m_lastEnteredTab!=-1) {
223
    if (m_lastEnteredTab==0)
224
      m_accountView->leave();
225
    else {
226
      ap=findPageAt(m_lastEnteredTab-1);
227
      if (ap)
228
        ap->leave();
229
    }
230
  }
231

    
232
  if (idx==0) {
233
    m_accountView->enter();
234
  }
235
  else {
236
    ap=findPageAt(idx-1);
237
    if (ap)
238
      ap->enter();
239
  }
240

    
241
  m_lastEnteredTab=idx;
242

    
243

    
244
  return 1;
245
}
246

    
247

    
248

    
249
long FM_AccountPage::onTabLeftButtonPressed(FXObject *obj, FXSelector, void *ptr)
250
{
251
  FXEvent *event=(FXEvent *) ptr;
252
  FXTabItem *item=(FXTabItem *) obj;
253

    
254
  if (item->isEnabled()) {
255
    if (m_closeTabIcon) {
256
      int ix;
257

    
258
      ix=item->getWidth()-m_closeTabIcon->getWidth();
259
      if (event->win_x>ix) {
260
        FM_AccountStmPage *page;
261

    
262
        page=(FM_AccountStmPage *)(item->getNext());
263
        page->fini(m_config);
264
        m_accountPages.remove(page);
265
        delete item;
266
        delete page;
267
        return 1;
268
      }
269
    }
270
    else {
271
      DBG_ERROR(0, "no icon");
272
    }
273
  }
274
  else {
275
    DBG_ERROR(0, "Not enabled");
276
  }
277

    
278
  return 0;
279
}
280

    
281

    
282

    
283
FM_AccountStmPage *FM_AccountPage::findPageForAccount(AQDB_ID aid)
284
{
285
  std::list<FM_AccountStmPage *>::iterator it;
286

    
287
  for (it=m_accountPages.begin(); it!=m_accountPages.end(); it++) {
288
    if ((*it)->getBankAccountId()==aid)
289
      return *it;
290
  }
291

    
292
  return NULL;
293
}
294

    
295

    
296

    
297
int FM_AccountPage::findIndexForAccount(AQDB_ID aid)
298
{
299
  std::list<FM_AccountStmPage *>::iterator it;
300
  int idx=0;
301

    
302
  for (it=m_accountPages.begin(); it!=m_accountPages.end(); it++) {
303
    if ((*it)->getBankAccountId()==aid)
304
      return idx;
305
    idx++;
306
  }
307

    
308
  return -1;
309
}
310

    
311

    
312

    
313
FM_AccountStmPage *FM_AccountPage::findPageAt(int idx)
314
{
315
  std::list<FM_AccountStmPage *>::iterator it;
316

    
317
  for (it=m_accountPages.begin(); it!=m_accountPages.end(); it++) {
318
    if (idx==0)
319
      return *it;
320
    idx--;
321
  }
322

    
323
  return NULL;
324
}
325

    
326

    
327

    
328
void FM_AccountPage::openAccountPage(const AE_ACCOUNT *acc)
329
{
330
  FM_AccountStmPage *ap;
331
  AQDB_ID aid;
332

    
333
  aid=AE_Account_GetId(acc);
334
  ap=findPageForAccount(aid);
335
  if (ap) {
336
    int idx=findIndexForAccount(aid);
337
    if (idx!=-1)
338
      m_tabBook->setCurrent(idx+1, true);
339
  }
340
  else {
341
    const char *s;
342
    FXString title;
343
    FXTabItem *tab;
344

    
345
    /* no page open for account, create one */
346
    s=AE_Account_GetName(acc);
347
    if (s==NULL || *s==0)
348
      s=AE_Account_GetCode(acc);
349
    if (s==NULL || *s==0)
350
      s=I18N("<unknown>");
351
    title=FXString(s);
352

    
353
    /* create tab entry */
354
    tab=addTabHeader(title, true);
355
    tab->create();
356

    
357
    /* create page */
358
    ap=new FM_AccountStmPage(title, this, acc, LAYOUT_FILL_X | LAYOUT_FILL_Y);
359
    ap->create();
360
    addTabPage(ap, true);
361
    m_accountPages.push_back(ap);
362
    ap->init(m_config);
363

    
364
    /* we correctly use .size() here, not size()-1, because the first tab is the account tree */
365
    m_tabBook->setCurrent(m_accountPages.size(), true);
366
  }
367
}
368

    
369

    
370

    
371
void FM_AccountPage::enter()
372
{
373
  if (m_lastEnteredTab!=-1) {
374
    FM_AccountStmPage *ap;
375

    
376
    if (m_lastEnteredTab==0)
377
      m_accountView->enter();
378
    else {
379
      ap=findPageAt(m_lastEnteredTab-1);
380
      if (ap)
381
        ap->enter();
382
    }
383
  }
384
}
385

    
386

    
387

    
388
void FM_AccountPage::leave()
389
{
390
  if (m_lastEnteredTab!=-1) {
391
    FM_AccountStmPage *ap;
392

    
393
    if (m_lastEnteredTab==0)
394
      m_accountView->leave();
395
    else {
396
      ap=findPageAt(m_lastEnteredTab-1);
397
      if (ap)
398
        ap->leave();
399
    }
400
  }
401
}
402

    
403

    
404
void FM_AccountPage::reset()
405
{
406
  std::list<FM_AccountStmPage *>::iterator it;
407

    
408
  m_accountView->reset();
409

    
410
  for (it=m_accountPages.begin(); it!=m_accountPages.end(); it++) {
411
    (*it)->reset();
412
  }
413
}
414

    
415

    
416

    
417

    
418