1%% options 2 3copyright owner = Dirk Krause 4copyright year = 2015-xxxx 5SPDX-License-Identifier: BSD-3-Clause 6 7 8%% header 9 10/** @file Dk4WxHelpController.h Help controller class. 11*/ 12 13#ifndef DK4CONF_H_INCLUDED 14#if DK4_BUILDING_DKTOOLS4 15#include "dk4conf.h" 16#else 17#include <dktools-4/dk4conf.h> 18#endif 19#endif 20 21#ifndef DK4TYPES_H_INCLUDED 22#if DK4_BUILDING_DKTOOLS4 23#include <libdk4base/dk4types.h> 24#else 25#include <dktools-4/dk4types.h> 26#endif 27#endif 28 29#ifndef WX_WXPREC_H_INCLUDED 30#include <wx/wxprec.h> 31#define WX_WXPREC_H_INCLUDED 1 32#endif 33 34#ifdef __BORLANDC__ 35#pragma hdrstop 36#endif 37#ifndef WX_PRECOMP 38#ifndef WX_WX_H_INCLUDED 39#include <wx/wx.h> 40#define WX_WX_H_INCLUDED 1 41#endif 42#endif 43#ifndef WX_CONFIG_H_INCLUDED 44#include <wx/config.h> 45#define WX_CONFIG_H_INCLUDED 1 46#endif 47#ifndef WX_FILENAME_H_INCLUDED 48#include <wx/filename.h> 49#define WX_FILENAME_H_INCLUDED 1 50#endif 51#ifndef WX_HELP_H_INCLUDED 52#include <wx/help.h> 53#define WX_HELP_H_INCLUDED 1 54#endif 55#ifndef WX_FS_ZIP_H_INCLUDED 56#include <wx/fs_zip.h> 57#define WX_FS_ZIP_H_INCLUDED 1 58#endif 59#ifndef WX_THREAD_H_INCLUDED 60#include <wx/thread.h> 61#define WX_THREAD_H_INCLUDED 1 62#endif 63 64#ifndef DK4WXAPPLICATIONHELPER_H_INCLUDED 65#if DK4_BUILDING_DKTOOLS4 66#include <libdk4wx/Dk4WxApplicationHelper.h> 67#else 68#include <dktools-4/Dk4WxApplicationHelper.h> 69#endif 70#endif 71 72 73/** Help controller dealing with both CHM and HTB files. 74*/ 75class Dk4WxHelpController 76{ 77 protected: 78 79 /** Synchronized access. 80 */ 81 wxCriticalSection csProtect; 82 83#ifdef __WXMSW__ 84 /** Help controller for CHM files used on Windows. 85 */ 86 wxCHMHelpController oHelpController; 87#else 88 /** Help controller for HTB files used on non-Windows systems. 89 */ 90 wxHtmlHelpController oHelpController; 91#endif 92 93 /** Application helper, used for for search. 94 */ 95 Dk4WxApplicationHelper *pAppHelp; 96 97 /** Short file name of help file. 98 */ 99 dkChar *pdksShortFileName; 100 101 /** Flag: Contents was already loaded. 102 */ 103 bool bLoaded; 104 105 /** Flag: Help file was found. 106 */ 107 bool bFileFound; 108 109 protected: 110 111 /** Attempt to load the help file. 112 */ 113 void 114 AttemptToLoadFile(void); 115 116 /** Show error message. 117 */ 118 void 119 ShowErrorMessage(size_t i_title = 0, size_t i_text = 1); 120 121 public: 122 123 /** Default constructor. 124 Just set the pointers to NULL. 125 */ 126 Dk4WxHelpController(); 127 128 /** Destructor. 129 */ 130 ~Dk4WxHelpController(); 131 132 /** Initialize the controller (keep pointers). 133 @param ahptr Application helper for file search. 134 @param chmName CHM help file name for windows. 135 @param htbName HTB help file name for other systems. 136 @return True on success, false on error. 137 */ 138 bool 139 Initialize( 140 Dk4WxApplicationHelper *ahptr, 141 const dkChar *chmName, 142 const dkChar *htbName 143 ); 144 145 /** Clean up. 146 */ 147 void 148 Cleanup(void); 149 150 /** Open help system. 151 */ 152 void 153 DisplayContents(void); 154 155 /** Open help section specified by name. 156 @param name Section name. 157 */ 158 void 159 DisplaySection(wxString const & name); 160 161 /** Open a help section specified by number. 162 @param number Section number (context ID). 163 */ 164 void 165 DisplaySection(int number); 166 167}; 168 169%% module 170 171#include "dk4conf.h" 172#include <libdk4base/dk4strd.h> 173#include <libdk4wx/dk4strx.h> 174#include <libdk4c/dk4fs.h> 175#include <libdk4base/dk4mem.h> 176#include <libdk4base/dk4mpl.h> 177#include <libdk4wx/Dk4WxHelpController.h> 178 179 180 181$!trace-include 182 183 184 185/** Constant text fragments used by module. 186*/ 187static const wxChar * const dk4wxhc_kw_def[] = { 188$!string-table macro=wxT 189Help file not found 190The file containing online help text was not found on your computer! 191$!end 192}; 193 194 195 196/** Pointer type, defined to calculate dk4wxhc_kw_def array size. 197*/ 198typedef const wxChar *PTR_CWXC; 199 200 201 202/** Number of array elements in dk4wxhc_kw_def. 203*/ 204static const size_t sz_dk4wxhc_kw_def = 205sizeof(dk4wxhc_kw_def) / sizeof(PTR_CWXC); 206 207 208 209Dk4WxHelpController::Dk4WxHelpController() 210#ifdef __WXMSW__ 211 212#else 213: oHelpController(wxHF_DEFAULT_STYLE | wxHF_MERGE_BOOKS) 214#endif 215{ 216 pAppHelp = NULL; 217 pdksShortFileName = NULL; 218 bLoaded = false; 219 bFileFound = false; 220} 221 222 223 224Dk4WxHelpController::~Dk4WxHelpController() 225{ 226 dk4mem_release(pdksShortFileName); 227 pAppHelp = NULL; 228 bLoaded = false; 229 bFileFound = false; 230} 231 232 233 234bool 235Dk4WxHelpController::Initialize( 236 Dk4WxApplicationHelper *ahptr, 237#ifdef __WXMSW__ 238 const dkChar *chmName, 239#else 240 const dkChar * WXUNUSED(chmName), 241#endif 242#ifdef __WXMSW__ 243 const dkChar * WXUNUSED(htbName) 244#else 245 const dkChar *htbName 246#endif 247) 248{ 249 const dkChar *fn = NULL; 250 bool back = false; 251#ifdef __WXMSW__ 252 fn = chmName; 253#else 254 fn = htbName; 255#endif 256 if ((NULL != ahptr) && (NULL != fn)) { 257 pAppHelp = ahptr; 258 bLoaded = false; 259 bFileFound = false; 260 pdksShortFileName = dk4str_dup(fn, NULL); 261 if (NULL != pdksShortFileName) { 262 back = true; 263 } else { 264 /* ERROR: Memory */ 265 ShowErrorMessage(6, 7); 266 } 267 } else { 268 /* ERROR: Illegal arguments to function */ 269 ShowErrorMessage(6, 8); 270 } 271 return back; 272} 273 274 275 276void 277Dk4WxHelpController::Cleanup(void) 278{ 279 { 280 wxCriticalSectionLocker lock(csProtect); 281 dk4mem_release(pdksShortFileName); 282 pAppHelp = NULL; 283 bLoaded = false; 284 bFileFound = false; 285 } 286} 287 288 289 290void 291Dk4WxHelpController::AttemptToLoadFile(void) 292{ 293 wxChar fnb[DK4_MAX_PATH]; 294 size_t szfnb = DK4_SIZEOF(fnb,wxChar); 295 296 if (!bLoaded) { 297 bLoaded = true; 298 if ((NULL != pAppHelp) && (NULL != pdksShortFileName)) { 299 if (pAppHelp->FindHelpFile(fnb, szfnb, pdksShortFileName)) { 300 bFileFound = true; 301#ifdef __WXMSW__ 302 oHelpController.Initialize(fnb); 303#else 304 oHelpController.AddBook(fnb); 305#endif 306 } 307 } 308 } 309} 310 311 312 313void 314Dk4WxHelpController::ShowErrorMessage(size_t i_title, size_t i_text) 315{ 316 const wxChar *strTitle = NULL; 317 const wxChar *strText = NULL; 318 { 319 wxCriticalSectionLocker lock(csProtect); 320 if (NULL != pAppHelp) { 321 strTitle = pAppHelp->GetBasicString(i_title); 322 strText = pAppHelp->GetBasicString(i_text); 323 } 324 } 325 if (NULL == strTitle) { 326 if (i_title < sz_dk4wxhc_kw_def) { 327 strTitle = dk4wxhc_kw_def[i_title]; 328 } 329 } 330 if (NULL == strText) { 331 if (i_text < sz_dk4wxhc_kw_def) { 332 strText = dk4wxhc_kw_def[i_text]; 333 } 334 } 335 wxMessageBox(strText, strTitle, (wxOK | wxCENTRE | wxICON_ERROR)); 336} 337 338 339 340void 341Dk4WxHelpController::DisplayContents() 342{ 343 bool canOpenHelp = false; 344 { 345 wxCriticalSectionLocker lock(csProtect); 346 AttemptToLoadFile(); 347 if (bLoaded) { 348 if (bFileFound) { 349 canOpenHelp = true; 350 } 351 } 352 } 353 if (canOpenHelp) { 354 oHelpController.DisplayContents(); 355 } else { 356 ShowErrorMessage(); 357 } 358} 359 360 361 362void 363Dk4WxHelpController::DisplaySection(wxString const & name) 364{ 365 bool canOpenHelp = false; 366 { 367 wxCriticalSectionLocker lock(csProtect); 368 AttemptToLoadFile(); 369 if (bLoaded) { 370 if (bFileFound) { 371 canOpenHelp = true; 372 } 373 } 374 } 375 if (canOpenHelp) { 376 oHelpController.DisplaySection(name); 377 } else { 378 ShowErrorMessage(); 379 } 380} 381 382 383 384void 385Dk4WxHelpController::DisplaySection(int number) 386{ 387 bool canOpenHelp = false; 388 { 389 wxCriticalSectionLocker lock(csProtect); 390 AttemptToLoadFile(); 391 if (bLoaded) { 392 if (bFileFound) { 393 canOpenHelp = true; 394 } 395 } 396 } 397 if (canOpenHelp) { 398 oHelpController.DisplaySection(number); 399 } else { 400 ShowErrorMessage(); 401 } 402} 403 404 405