Source

oberchatten / InfoBoxUnit.cpp

  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
/*
Copyright (C) 2001-2006 Daybo Logic.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. Neither the name of the project nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
//---------------------------------------------------------------------------
#ifdef HAVE_CONFIG_H
# include "overchat_config.h"
#endif //HAVE_CONFIG_H

#include <new>
#include <vcl>
#include <limits>
#include <time>
#include <stdio>
#include <string>
#include <except>
#include <assert>
#ifdef HDRSTOP
# pragma hdrstop
#endif //HDRSTOP

#include "master.h"
#include "ShadowUnit.h"
#include "InfoBoxUnit.h"
#include "General.h"
#include "Options.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TInfoBox *InfoBox = NULL; // We don't use the global version so make it safe
using namespace Oberchatten;

#ifdef OVERCHAT_RCS_STAMPS
static const char *rcsid =
  "$Id: InfoBoxUnit.cpp,v 1.0 2006/03/04 00:14:00 ddrp Exp $";
#endif /*OVERCHAT_RCS_STAMPS*/
//---------------------------------------------------------------------------
int InformationBox(TComponent* Owner,    // handle of owner window
                   LPCTSTR lpszMessage,  // message box text
                   LPCTSTR lpszCaption,  // message box title
                   UINT nStyle,          // style of message box
                   UINT nHelpId          // help context id
)
{
  int returnValue = 0; // In keeping with M$' failure code (not enough memory)
  TInfoBox* infoBox = new TInfoBox(Owner); // Create a dialog box
  if ( infoBox ) { // Created?
    infoBox->IconVersion(6); // XP style is my preference for this release
    infoBox->Caption = lpszCaption;
    infoBox->SetText(lpszMessage);
    infoBox->SetStyle(nStyle);
    infoBox->ShowModal(); // Display the dialog
    returnValue = infoBox->GetIDValue(); // Recover user selection
    delete infoBox; // Destroy the dialog
  }
  return returnValue; // Give user selection to caller
}
//---------------------------------------------------------------------------
__fastcall TInfoBox::TInfoBox(TComponent* Owner)
        : TForm(Owner),
          idValue(0),
          style((UINT)0),
          iconVer(1),
          shadow(NULL)
{
  #ifdef OVERCHAT_RCS_STAMPS
    blake_Unused(rcsid);
  #endif /*OVERCHAT_RCS_STAMPS*/

  daybothreads_CreateLock(&this->moveHandlingLock);
}
//---------------------------------------------------------------------------
__fastcall TInfoBox::~TInfoBox()
{
  daybothreads_DestroyLock(&this->moveHandlingLock);
}
//---------------------------------------------------------------------------
void __fastcall TInfoBox::FormShow(TObject *Sender)
{
  CentreMyForm(this, Screen);
  this->UpdateShadow();
  this->SetDefFocus();
}
//---------------------------------------------------------------------------
void TInfoBox::SetStyle(UINT Style)
{
  this->style = Style;
  this->SetIcon(Style);
  this->SetButtons(Style);
}
//---------------------------------------------------------------------------
void TInfoBox::SetIcon(UINT Style)
{
  AnsiString resname("IDB_INFOICONS_");
  bool setting = false;

  if ( (Style & 0x00f0) == MB_ICONEXCLAMATION ) {
    resname += (AnsiString)"EXCLAMATION_";
    setting = true;
  }
  else if ( (Style & 0x00f0) == MB_ICONINFORMATION ) {
    resname += (AnsiString)"INFORMATION_";
    setting = true;
  }
  else if ( (Style & 0x00f0) == MB_ICONQUESTION ) {
    resname += (AnsiString)"QUESTION_";
    setting = true;
  }
  else if ( (Style & 0x00f0) == MB_ICONSTOP ) {
    resname += (AnsiString)"STOP_";
    setting = true;
  }
  else { // Unload the icon
    this->IconImage->Picture->Assign(NULL); // Clear it
  }

  if ( setting ) {
    resname += (AnsiString)this->IconVersion();

    this->IconImage->Picture->Bitmap->LoadFromResourceName(
      (int)INFOBOX_ICONS_RESHANDLE,
      resname
    );

    // Reduce memory requirements
    this->IconImage->Picture->Bitmap->FreeImage();
  }
}
//---------------------------------------------------------------------------
void TInfoBox::SetButtons(UINT Style)
{
  unsigned int nButtons;
  if ( (Style & 0x000f) == MB_ABORTRETRYIGNORE ) {
    this->Button1->Caption = "&Abort";
    this->Button1->Tag = IDABORT;

    this->Button2->Caption = "&Retry";
    this->Button2->Tag = IDRETRY;

    this->Button3->Caption = "&Ignore";
    this->Button3->Tag = IDIGNORE;

    nButtons = 3;
  }
  else if ( (Style & 0x000f) == MB_OKCANCEL ) {
    this->Button1->Caption = "&OK";
    this->Button1->Tag = IDOK;

    this->Button2->Caption = "&Cancel";
    this->Button2->Tag = IDCANCEL;

    nButtons = 2;
  }
  else if ( (Style & 0x000f) == MB_RETRYCANCEL ) {
    this->Button1->Caption = "&Retry";
    this->Button1->Tag = IDRETRY;

    this->Button2->Caption = "&Cancel";
    this->Button2->Tag = IDCANCEL;

    nButtons = 2;
  }
  else if ( (Style & 0x000f) == MB_YESNO ) {
    this->Button1->Caption = "&Yes";
    this->Button1->Tag = IDYES;

    this->Button2->Caption = "&No";
    this->Button2->Tag = IDNO;

    nButtons = 2;
  }
  else if ( (Style & 0x000f) == MB_YESNOCANCEL ) {
    this->Button1->Caption = "&Yes";
    this->Button1->Tag = IDYES;

    this->Button2->Caption = "&No";
    this->Button2->Tag = IDNO;

    this->Button3->Caption = "&Cancel";
    this->Button3->Tag = IDCANCEL;

    nButtons = 3;
  }
  else { // MB_OK (0)
    this->Button1->Caption = "&OK";
    this->Button1->Tag = IDOK;

    nButtons = 1;
  }
  this->NumberOfButtons(nButtons);

  // Now set default buttons (see also SetDefFocus())
  if ( (Style & 0x0f00) == MB_DEFBUTTON1 )
    this->Button1->Default = true;
  else if ( (Style & 0x0f00) == MB_DEFBUTTON2 )
    this->Button2->Default = true;
  else
    this->Button3->Default = true;

  // Set up cancel for IDCANCEL buttons
  if ( this->Button1->Tag == IDCANCEL )
    this->Button1->Cancel = true;
  else
    this->Button1->Cancel = false;

  if ( this->Button2->Tag == IDCANCEL )
    this->Button2->Cancel = true;
  else
    this->Button2->Cancel = false;

  if ( this->Button3->Tag == IDCANCEL )
    this->Button3->Cancel = true;
  else
    this->Button3->Cancel = false;

  return;
}
//---------------------------------------------------------------------------
unsigned int TInfoBox::GetNumberOfButtons()
{
  unsigned int count = 1;
  if ( this->Button2->Visible ) count++;
  if ( this->Button3->Visible ) count++;
  return count;
}
//---------------------------------------------------------------------------
void TInfoBox::NumberOfButtons(unsigned int NumBtns)
{
  unsigned int oldNumber;
  assert(NumBtns > 0 && NumBtns < 4);

  oldNumber = this->GetNumberOfButtons();
  // There will always be at least one button, hide the other two
  this->Button2->Visible = false;
  this->Button3->Visible = false;

  if ( NumBtns >= 2 )
    this->Button2->Visible = true;
  if ( NumBtns >= 3 )
    this->Button3->Visible = true;

  if ( oldNumber != NumBtns ) { // Button count changed?
    // Need to reposition the buttons to look better
    if ( NumBtns == 1 ) {
      this->Button1->Left = this->ClientWidth /2 - this->Button1->Width /2;
    }
    else if ( NumBtns == 2 ) {
      this->Button1->Left = this->ClientWidth /3 - this->Button1->Width /2;
      this->Button2->Left = this->Button1->Left + this->ClientWidth /3;
    }
    else { // 3
      this->Button1->Left = this->ClientWidth /4 - this->Button1->Width /2;
      this->Button2->Left = this->Button1->Left + this->ClientWidth /4;

      this->Button3->Left = this->Button1->Left +
        this->Button2->Left + this->ClientWidth /4;
    }
  }
}
//---------------------------------------------------------------------------
unsigned int TInfoBox::IconVersion(unsigned int NewIconVersion)
{
  if ( NewIconVersion ) // Changing?
    this->iconVer = NewIconVersion; // Assign new

  // Correct the icon to one we support
  if ( this->iconVer >= 6 )
    this->iconVer = 6;
  else if ( this->iconVer >= 4 )
    this->iconVer = 4;
  else if ( this->iconVer >= 1 )
    this->iconVer = 3;

  return this->iconVer;
}
//---------------------------------------------------------------------------
void TInfoBox::LockOnMove(bool Locking)
{
  if ( Locking )
    daybothreads_EnterLock(&this->moveHandlingLock);
  else
    daybothreads_LeaveLock(&this->moveHandlingLock);
}
//---------------------------------------------------------------------------
void TInfoBox::UpdateShadow()
{
  if ( this->shadow ) {
    this->shadow->Follow();
    if ( !this->shadow->Visible ) this->shadow->Show();
  }
}
//---------------------------------------------------------------------------
void __fastcall TInfoBox::OnMove(TWMMove& Message)
{
  if ( this->shadow ) {
    this->LockOnMove(true);
    this->UpdateShadow();
    this->LockOnMove(false);
  }
}
//---------------------------------------------------------------------------
void __fastcall TInfoBox::FormCreate(TObject *Sender)
{
  if ( Options.GetOptionBool(Oberchatten::oShadows) )
    this->shadow = new TShadowForm(this);
}
//---------------------------------------------------------------------------
void __fastcall TInfoBox::FormDestroy(TObject *Sender)
{
  if ( this->shadow ) {
    delete this->shadow;
    this->shadow = NULL;
  }
}
//---------------------------------------------------------------------------
void TInfoBox::SetText(TStrings& Strings)
{
  this->InfoMemo->Lines->Clear();
  this->InfoMemo->Lines->AddStrings(&Strings);
}
//---------------------------------------------------------------------------
void TInfoBox::SetText(const char* Text)
{
  char defText[] = "Not enough memory to display this message";
  char* text; // Modifiable copy of text
  char* ptr; // Offset into text
  bool allocated = false;
  unsigned int c, modCount = 1U; // There is always at least one line (even if it's blank)

  if ( !Text ) {
    this->SetText((const char*)""); // Fool-proofing
    return;
  }
  c = strlen(Text);
  try {
    text = new char[c+1];
    allocated = true;
  }
  catch ( std::bad_alloc ) {
    text = defText;
  }

  if ( allocated ) {
    text[0] = '\0';
    strncat(text, Text, c); // Take a copy of the original string
  }
  this->InfoMemo->Lines->Clear();
  ptr = text; // Start at the beginning
  // Convert \n sequenced in text to \0s
  for ( unsigned int i = 0U; i < c; i++ ) {
    if ( text[i] == '\n' ) {
      text[i] = '\0';
      modCount++;
    }
  }
  for ( unsigned int i = 0U; i < modCount; i++ ) {
    this->InfoMemo->Lines->Add(ptr);
    ptr = static_cast<char*>( memchr(ptr, '\0', c) ); // Shift string to next line
    ptr++;
  }

  if ( allocated ) delete[] text;
}
//---------------------------------------------------------------------------
void __fastcall TInfoBox::BtnClick(TObject *Sender)
{
  TButton* btn = dynamic_cast<TButton*>(Sender);
  assert(btn);
  if ( btn ) {
    this->idValue = btn->Tag;
    this->Close();
  }
}
//---------------------------------------------------------------------------
void TInfoBox::SetDefFocus(UINT Style)
{
  // If this is called when we're hidden...
  // VCL will cause an exception (beware)
  if ( (Style & 0x0f00) == MB_DEFBUTTON1 )
    this->Button1->SetFocus();
  else if ( (Style & 0x0f00) == MB_DEFBUTTON2 )
    this->Button2->SetFocus();
  else
    this->Button3->SetFocus();

  return;
}
//---------------------------------------------------------------------------
void TInfoBox::SetDefFocus()
{
  this->SetDefFocus(this->style);
}
//---------------------------------------------------------------------------
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.