| 1 |
/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */ |
| 2 |
|
| 3 |
/* AbiSource Application Framework |
| 4 |
* Copyright (C) 1998 AbiSource, Inc. |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2 |
| 9 |
* of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 |
* 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "ut_debugmsg.h" |
| 23 |
#include "ut_types.h" |
| 24 |
#include "ut_vector.h" |
| 25 |
#include "xap_App.h" |
| 26 |
#include "xap_Frame.h" |
| 27 |
#include "xap_DialogFactory.h" |
| 28 |
#include "xap_Dialog_Id.h" |
| 29 |
|
| 30 |
// save some typing |
| 31 |
typedef std::multimap<XAP_Dialog_Id, const XAP_NotebookDialog::Page*> NotebookPages; |
| 32 |
typedef NotebookPages::iterator NotebookPagesIter; |
| 33 |
|
| 34 |
static NotebookPages s_mapNotebookPages; |
| 35 |
|
| 36 |
/*****************************************************************/ |
| 37 |
|
| 38 |
XAP_DialogFactory::XAP_DialogFactory(XAP_App * pApp, int nrElem, const struct _dlg_table * pDlgTable, XAP_Frame * pFrame) |
| 39 |
: m_pApp(pApp), |
| 40 |
m_pFrame(pFrame), |
| 41 |
m_dialogType(XAP_DLGT_APP_PERSISTENT), |
| 42 |
m_nrElementsDlgTable(nrElem) |
| 43 |
{ |
| 44 |
UT_ASSERT(pApp); |
| 45 |
UT_sint32 i; |
| 46 |
|
| 47 |
for (i = 0; i < nrElem; i++) |
| 48 |
{ |
| 49 |
m_vec_dlg_table.addItem(&pDlgTable[i]); |
| 50 |
} |
| 51 |
|
| 52 |
#ifdef DEBUG |
| 53 |
// getNextId() assumes that the last item is the greater. We should |
| 54 |
// keep greater ID always the latest on ap_[PLATFORM]Dialog_All.h |
| 55 |
UT_sint32 greatest = 0; |
| 56 |
for(i=0; i< nrElem; i++) |
| 57 |
{ |
| 58 |
if (pDlgTable[i].m_id > greatest) |
| 59 |
greatest = pDlgTable[i].m_id; |
| 60 |
} |
| 61 |
|
| 62 |
if (nrElem) |
| 63 |
UT_ASSERT(greatest == pDlgTable[nrElem - 1].m_id); |
| 64 |
#endif |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
XAP_DialogFactory::~XAP_DialogFactory(void) |
| 69 |
{ |
| 70 |
UT_VECTOR_PURGEALL(XAP_Dialog *, m_vecDialogs); |
| 71 |
UT_VECTOR_PURGEALL( _dlg_table *, m_vecDynamicTable); |
| 72 |
} |
| 73 |
|
| 74 |
bool XAP_DialogFactory::_findDialogInTable(XAP_Dialog_Id id, UT_sint32 * pIndex) const |
| 75 |
{ |
| 76 |
// search the table and return the index of the entry with this id. |
| 77 |
UT_return_val_if_fail(pIndex, false); |
| 78 |
|
| 79 |
for (UT_sint32 k=0; k < m_vec_dlg_table.getItemCount(); k++) |
| 80 |
{ |
| 81 |
if (m_vec_dlg_table.getNthItem(k)->m_id == id) |
| 82 |
{ |
| 83 |
*pIndex = k; |
| 84 |
return true; |
| 85 |
} |
| 86 |
} |
| 87 |
UT_DEBUGMSG(("Could not find a match for id %d \n",id)); |
| 88 |
UT_ASSERT_HARMLESS(UT_NOT_IMPLEMENTED); |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
XAP_Dialog_Id XAP_DialogFactory::getNextId(void) |
| 93 |
{ |
| 94 |
UT_sint32 i = m_vec_dlg_table.getItemCount()-1; |
| 95 |
UT_sint32 id = static_cast<UT_sint32>(m_vec_dlg_table.getNthItem(i)->m_id); |
| 96 |
return static_cast<XAP_Dialog_Id>(id+1); |
| 97 |
} |
| 98 |
|
| 99 |
XAP_Dialog_Id XAP_DialogFactory::registerDialog(XAP_Dialog *(* pStaticConstructor)(XAP_DialogFactory *, XAP_Dialog_Id id),XAP_Dialog_Type iDialogType) |
| 100 |
{ |
| 101 |
_dlg_table * pDlgTable = new _dlg_table; |
| 102 |
pDlgTable->m_id = getNextId(); |
| 103 |
pDlgTable->m_type = iDialogType; |
| 104 |
pDlgTable->m_pfnStaticConstructor = pStaticConstructor; |
| 105 |
pDlgTable->m_tabbed = FALSE; |
| 106 |
m_vec_dlg_table.addItem(pDlgTable); |
| 107 |
m_vecDynamicTable.addItem(pDlgTable); |
| 108 |
return pDlgTable->m_id; |
| 109 |
} |
| 110 |
|
| 111 |
void XAP_DialogFactory::unregisterDialog(XAP_Dialog_Id id) |
| 112 |
{ |
| 113 |
for (UT_sint32 i = 0; i < m_vecDialogs.getItemCount(); i++) |
| 114 |
{ |
| 115 |
const XAP_Dialog * pDialog = reinterpret_cast<const XAP_Dialog *>(m_vecDialogs.getNthItem(i)); |
| 116 |
if(pDialog && pDialog->getDialogId() == id) |
| 117 |
{ |
| 118 |
m_vecDialogs.deleteNthItem(i); |
| 119 |
m_vecDialogIds.deleteNthItem(i); |
| 120 |
delete pDialog; |
| 121 |
return; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/*****************************************************************/ |
| 127 |
|
| 128 |
/*! |
| 129 |
* This method just creates a new instance of the dialog without remembering |
| 130 |
* anything about it. It's up the to calling prgram to delete the dialog |
| 131 |
* when it is finished with it. |
| 132 |
\param XAP_Dialog_Id id the identification number of the dialog. |
| 133 |
\returns XAP_Dialog * pointer to the new instance of the dialog. |
| 134 |
*/ |
| 135 |
XAP_Dialog * XAP_DialogFactory::justMakeTheDialog(XAP_Dialog_Id id) |
| 136 |
{ |
| 137 |
UT_sint32 index; |
| 138 |
XAP_Dialog * pDialog = NULL; |
| 139 |
|
| 140 |
if(_findDialogInTable(id,&index)) |
| 141 |
{ |
| 142 |
pDialog = (XAP_Dialog *)((m_vec_dlg_table.getNthItem(index)->m_pfnStaticConstructor)(this,id)); |
| 143 |
return pDialog; |
| 144 |
} |
| 145 |
return NULL; |
| 146 |
} |
| 147 |
|
| 148 |
XAP_Dialog * XAP_DialogFactory::requestDialog(XAP_Dialog_Id id) |
| 149 |
{ |
| 150 |
const _dlg_table * dlg = NULL; |
| 151 |
XAP_Dialog * pDialog = NULL; |
| 152 |
UT_sint32 index; |
| 153 |
|
| 154 |
if(_findDialogInTable(id, &index)) |
| 155 |
{ |
| 156 |
dlg = m_vec_dlg_table.getNthItem(index); |
| 157 |
switch (dlg->m_type) |
| 158 |
{ |
| 159 |
case XAP_DLGT_NON_PERSISTENT: |
| 160 |
// construct a non-persistent dialog and just return it. |
| 161 |
goto CreateItSimple; |
| 162 |
|
| 163 |
case XAP_DLGT_FRAME_PERSISTENT: // if requested frame-persistent dialog |
| 164 |
if (m_dialogType == XAP_DLGT_FRAME_PERSISTENT) // from a frame-persistent factory. |
| 165 |
goto CreateItPersistent; |
| 166 |
break; |
| 167 |
|
| 168 |
case XAP_DLGT_APP_PERSISTENT: // if requested app-persistent dialog |
| 169 |
if (m_dialogType == XAP_DLGT_APP_PERSISTENT) // if from a app-persistent factory |
| 170 |
goto CreateItPersistent; |
| 171 |
if (m_dialogType == XAP_DLGT_FRAME_PERSISTENT) // if from a frame-persistent factory, |
| 172 |
goto HandToAppFactory; // let the app's factory do it.... |
| 173 |
break; |
| 174 |
|
| 175 |
case XAP_DLGT_MODELESS: // if requested app-persistent dialog |
| 176 |
if (m_dialogType == XAP_DLGT_APP_PERSISTENT) // if from a app-persistent factory |
| 177 |
goto CreateItPersistent; |
| 178 |
if (m_dialogType == XAP_DLGT_FRAME_PERSISTENT) // if from a frame-persistent factory, |
| 179 |
goto HandToAppFactory; // let the app's factory do it.... |
| 180 |
break; |
| 181 |
|
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
// UT_ASSERT_NOT_REACHED(); |
| 186 |
return NULL; |
| 187 |
|
| 188 |
CreateItSimple: |
| 189 |
{ |
| 190 |
// create a fresh dialog object and return it -- no strings attached. |
| 191 |
pDialog = (XAP_Dialog *)((dlg->m_pfnStaticConstructor)(this,id)); |
| 192 |
if (dlg->m_tabbed) { |
| 193 |
XAP_NotebookDialog * d = dynamic_cast<XAP_NotebookDialog *>(pDialog); |
| 194 |
UT_ASSERT(d); |
| 195 |
addPages(d, id); |
| 196 |
} |
| 197 |
return pDialog; |
| 198 |
} |
| 199 |
|
| 200 |
CreateItPersistent: |
| 201 |
{ |
| 202 |
// see if we already have an instance of this object in our vector. |
| 203 |
// if so, just return it. otherwise, create a fresh one and remember it. |
| 204 |
UT_sint32 indexVec = m_vecDialogIds.findItem(index+1); |
| 205 |
if (indexVec < 0) // not present, create new object and add it to vector |
| 206 |
{ |
| 207 |
pDialog = (XAP_Dialog *)((dlg->m_pfnStaticConstructor)(this,id)); |
| 208 |
m_vecDialogIds.addItem(index+1); |
| 209 |
m_vecDialogs.addItem(pDialog); |
| 210 |
} |
| 211 |
else // already present, reuse this object |
| 212 |
{ |
| 213 |
pDialog = (XAP_Dialog *)m_vecDialogs.getNthItem(indexVec); |
| 214 |
} |
| 215 |
if (dlg->m_tabbed) { |
| 216 |
XAP_NotebookDialog * d = dynamic_cast<XAP_NotebookDialog *>(pDialog); |
| 217 |
UT_ASSERT(d); |
| 218 |
addPages(d, id); |
| 219 |
} |
| 220 |
|
| 221 |
// let the dialog object know that we are reusing it. |
| 222 |
|
| 223 |
XAP_Dialog_Persistent * pDialogPersistent = (XAP_Dialog_Persistent *)pDialog; |
| 224 |
pDialogPersistent->useStart(); |
| 225 |
|
| 226 |
return pDialog; |
| 227 |
} |
| 228 |
|
| 229 |
HandToAppFactory: |
| 230 |
{ |
| 231 |
// pass the request to the factory with the appropriate scope. |
| 232 |
pDialog = XAP_App::getApp()->getDialogFactory()->requestDialog(id); |
| 233 |
return pDialog; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
void XAP_DialogFactory::releaseDialog(XAP_Dialog * pDialog) |
| 238 |
{ |
| 239 |
// the caller is done with the dialog. if it is non-persistent, we |
| 240 |
// can just delete it. otherwise, we should just store it for later |
| 241 |
// reuse. |
| 242 |
|
| 243 |
UT_return_if_fail(pDialog); |
| 244 |
XAP_Dialog_Id id = pDialog->getDialogId(); |
| 245 |
|
| 246 |
UT_sint32 index; |
| 247 |
_findDialogInTable(id,&index); |
| 248 |
|
| 249 |
switch (m_vec_dlg_table.getNthItem(index)->m_type) |
| 250 |
{ |
| 251 |
case XAP_DLGT_NON_PERSISTENT: // for non-persistent dialog objects, we |
| 252 |
delete pDialog; // just delete it now. |
| 253 |
return; |
| 254 |
|
| 255 |
case XAP_DLGT_FRAME_PERSISTENT: // if requested frame-persistent dialog |
| 256 |
if (m_dialogType == XAP_DLGT_FRAME_PERSISTENT) // from a frame-persistent factory. |
| 257 |
goto FinishedUsingObject; // we remember it in our vector. |
| 258 |
break; |
| 259 |
|
| 260 |
case XAP_DLGT_APP_PERSISTENT: // if requested app-persistent dialog |
| 261 |
if (m_dialogType == XAP_DLGT_APP_PERSISTENT) // if from a app-persistent factory |
| 262 |
goto FinishedUsingObject; // we remember it in our vector. |
| 263 |
if (m_dialogType == XAP_DLGT_FRAME_PERSISTENT) // if from a frame-persistent factory, |
| 264 |
goto HandToAppFactory; // let the app's factory do it.... |
| 265 |
break; |
| 266 |
|
| 267 |
case XAP_DLGT_MODELESS: // if requested app-persistent dialog |
| 268 |
if (m_dialogType == XAP_DLGT_APP_PERSISTENT) // if from a app-persistent factory |
| 269 |
goto FinishedUsingObject; // we remember it in our vector. |
| 270 |
if (m_dialogType == XAP_DLGT_FRAME_PERSISTENT) // if from a frame-persistent factory, |
| 271 |
goto HandToAppFactory; // let the app's factory do it.... |
| 272 |
break; |
| 273 |
} |
| 274 |
|
| 275 |
UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN); |
| 276 |
return; |
| 277 |
|
| 278 |
FinishedUsingObject: |
| 279 |
{ |
| 280 |
// let the dialog object know that we are reusing it. |
| 281 |
|
| 282 |
XAP_Dialog_Persistent * pDialogPersistent = (XAP_Dialog_Persistent *)pDialog; |
| 283 |
pDialogPersistent->useEnd(); |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
HandToAppFactory: |
| 288 |
{ |
| 289 |
// pass the request to the factory with the appropriate scope. |
| 290 |
XAP_App::getApp()->getDialogFactory()->releaseDialog(pDialog); |
| 291 |
return; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/*! |
| 296 |
* Add a notebook page to a builtin dialog. |
| 297 |
*/ |
| 298 |
bool XAP_DialogFactory::registerNotebookPage(XAP_Dialog_Id dialog, const XAP_NotebookDialog::Page * page) |
| 299 |
{ |
| 300 |
// check that widget is unique for dialog |
| 301 |
std::pair<NotebookPagesIter, NotebookPagesIter> bounds = s_mapNotebookPages.equal_range(dialog); |
| 302 |
while (bounds.first != bounds.second) |
| 303 |
{ |
| 304 |
if (bounds.first->second == page) |
| 305 |
{ |
| 306 |
UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN); |
| 307 |
return FALSE; |
| 308 |
} |
| 309 |
bounds.first++; |
| 310 |
} |
| 311 |
|
| 312 |
s_mapNotebookPages.insert(NotebookPages::value_type(dialog, page)); |
| 313 |
return TRUE; |
| 314 |
} |
| 315 |
|
| 316 |
/*! |
| 317 |
* Remove a previously added page from a dialog. |
| 318 |
*/ |
| 319 |
bool XAP_DialogFactory::unregisterNotebookPage(XAP_Dialog_Id dialog, const XAP_NotebookDialog::Page * page) |
| 320 |
{ |
| 321 |
std::pair<NotebookPagesIter, NotebookPagesIter> bounds = s_mapNotebookPages.equal_range(dialog); |
| 322 |
while (bounds.first != bounds.second) |
| 323 |
{ |
| 324 |
// widget per dialog must be unique, that's made sure in registerNotebookPage. |
| 325 |
if (bounds.first->second == page) |
| 326 |
{ |
| 327 |
s_mapNotebookPages.erase(bounds.first); |
| 328 |
return TRUE; |
| 329 |
} |
| 330 |
bounds.first++; |
| 331 |
} |
| 332 |
return FALSE; |
| 333 |
} |
| 334 |
|
| 335 |
/*! |
| 336 |
* Add registered pages to the dialog instance. |
| 337 |
*/ |
| 338 |
void XAP_DialogFactory::addPages(XAP_NotebookDialog * pDialog, XAP_Dialog_Id id) |
| 339 |
{ |
| 340 |
std::pair<NotebookPagesIter, NotebookPagesIter> bounds = s_mapNotebookPages.equal_range(id); |
| 341 |
while (bounds.first != bounds.second) |
| 342 |
{ |
| 343 |
pDialog->addPage(bounds.first->second); |
| 344 |
bounds.first++; |
| 345 |
} |
| 346 |
} |