source: CMT/v1r12p20020606/src/install_utils.cxx @ 1

Last change on this file since 1 was 1, checked in by arnault, 19 years ago

Import all tags

File size: 16.7 KB
Line 
1
2//---------------------------------------------------------------------
3#include "install_utils.h"
4#include <windowsx.h>
5#include "install_resource.h"
6//---------------------------------------------------------------------
7
8//---------------------------------------------------------------------
9WWindow::WWindow ()
10{
11  _window = NULL;
12}
13
14void WWindow::enable ()
15{
16  EnableWindow (_window, TRUE);
17}
18
19void WWindow::disable ()
20{
21  EnableWindow (_window, FALSE);
22}
23//---------------------------------------------------------------------
24
25//---------------------------------------------------------------------
26WEdit::WEdit ()
27{
28  _edit = NULL;
29}
30
31WEdit::WEdit (HWND edit)
32{
33  _edit = edit;
34  _window = _edit;
35}
36
37WEdit::WEdit (HWND container, int id)
38{
39  _edit = GetDlgItem (container, id);
40  _window = _edit;
41}
42
43void WEdit::init (HWND container, int id)
44{
45  _edit = GetDlgItem (container, id);
46  _window = _edit;
47}
48
49WEdit& WEdit::operator = (HWND edit)
50{
51  _edit = edit;
52  _window = _edit;
53  return (*this);
54}
55
56void WEdit::set (const char* text)
57{
58  int status = SendMessage (_edit, WM_SETTEXT, (WPARAM) 0, (LPARAM) text);
59}
60
61void WEdit::set (const cmt_string& text)
62{
63  set (text.c_str ());
64}
65
66void WEdit::clear ()
67{
68  set ("");
69}
70
71cmt_string WEdit::get () const
72{
73  int size = SendMessage (_edit, WM_GETTEXTLENGTH, (WPARAM) 0, (LPARAM) 0);
74
75  cmt_string text (size);
76
77  SendMessage (_edit, WM_GETTEXT, (WPARAM) size + 1, (LPARAM) text.c_str ());
78
79  return (text);
80}
81
82void WEdit::copy (const WEdit& other)
83{
84  clear ();
85  set (other.get ());
86}
87//---------------------------------------------------------------------
88
89//---------------------------------------------------------------------
90WList::WList ()
91{
92  _list = NULL;
93  _selected_index = -1;
94  _width = 0;
95}
96
97WList::WList (HWND list)
98{
99  _list = list;
100  _window = _list;
101  _width = get_width ();
102  set_extent ();
103  get_selected_index ();
104}
105
106WList::WList (HWND container, int id)
107{
108  _list = GetDlgItem (container, id);
109  _window = _list;
110  _width = get_width ();
111  set_extent ();
112  get_selected_index ();
113}
114
115void WList::init (HWND container, int id)
116{
117  _list = GetDlgItem (container, id);
118  _window = _list;
119  _width = get_width ();
120  set_extent ();
121  get_selected_index ();
122}
123
124WList& WList::operator = (HWND list)
125{
126  _list = list;
127  _window = _list;
128  _width = get_width ();
129  set_extent ();
130  get_selected_index ();
131  return (*this);
132}
133
134void WList::append (const char* text)
135{
136  SendMessage (_list, LB_INSERTSTRING, (WPARAM) -1, (LPARAM) text);
137  int w = strlen (text);
138  if (w > _width) 
139  {
140    _width = w;
141    set_extent ();
142  }
143}
144
145void WList::append (const cmt_string& text)
146{
147  SendMessage (_list, LB_INSERTSTRING, (WPARAM) -1, (LPARAM) text.c_str ());
148  int w = text.size ();
149  if (w > _width) 
150  {
151    _width = w;
152    set_extent ();
153  }
154}
155
156void WList::insert (int index, const cmt_string& text)
157{
158  SendMessage (_list, LB_INSERTSTRING, (WPARAM) index, (LPARAM) text.c_str ());
159  int w = text.size ();
160  if (w > _width) 
161  {
162    _width = w;
163    set_extent ();
164  }
165}
166
167void WList::modify (int index, const cmt_string& text)
168{
169  SendMessage (_list, LB_DELETESTRING, (WPARAM) index, (LPARAM) 0);
170  SendMessage (_list, LB_INSERTSTRING, (WPARAM) index, (LPARAM) text.c_str ());
171
172  _width = get_width ();
173  set_extent ();
174}
175
176void WList::delete_item (int index)
177{
178  SendMessage (_list, LB_DELETESTRING, (WPARAM) index, (LPARAM) 0);
179
180  _width = get_width ();
181  set_extent ();
182}
183
184void WList::clear ()
185{
186  SendMessage (_list, LB_RESETCONTENT, (WPARAM) 0, (LPARAM) 0);
187  _selected_index = -1;
188  _width = 0;
189  set_extent ();
190}
191
192void WList::cancel_selection ()
193{
194  SendMessage (_list, LB_SETCURSEL, (WPARAM) -1, (LPARAM) 0);
195  _selected_index = -1;
196}
197
198int WList::get_selected_index ()
199{
200  int index;
201               
202  index = SendMessage (_list, LB_GETCURSEL, (WPARAM) 0, (LPARAM) 0);
203  _selected_index = index;
204
205  return (index);
206}
207
208void WList::set_selected_index (int index)
209{
210  SendMessage (_list, LB_SETCURSEL, (WPARAM) index, (LPARAM) 0);
211  _selected_index = index;
212}
213
214cmt_string WList::get_selected ()
215{
216  int index = get_selected_index ();
217
218  int size = SendMessage (_list, LB_GETTEXTLEN, (WPARAM) index, (LPARAM) 0);
219
220  cmt_string text (size + 1);
221
222  SendMessage (_list, LB_GETTEXT, (WPARAM) index, (LPARAM) text.c_str ());
223
224  return (text);
225}
226
227int WList::get_item_count () const
228{
229  int item;
230       
231  item = SendMessage (_list, LB_GETCOUNT, (WPARAM) 0, (LPARAM) 0);
232
233  return (item);
234}
235
236cmt_string WList::get_item (int index) const
237{
238  int size = SendMessage (_list, LB_GETTEXTLEN, (WPARAM) index, (LPARAM) 0);
239
240  cmt_string text (size + 1);
241       
242  SendMessage (_list, LB_GETTEXT, (WPARAM) index, (LPARAM) text.c_str ());
243
244  return (text);
245}
246
247void WList::get_item (int index, cmt_string& text) const
248{
249  int size = SendMessage (_list, LB_GETTEXTLEN, (WPARAM) index, (LPARAM) 0);
250
251  static cmt_string temp;
252  temp.resize (size);
253         
254  SendMessage (_list, LB_GETTEXT, (WPARAM) index, (LPARAM) temp.c_str ());
255
256  text = temp;
257}
258
259int WList::get_item_length (int index) const
260{
261  int size = SendMessage (_list, LB_GETTEXTLEN, (WPARAM) index, (LPARAM) 0);
262
263  return (size);
264}
265
266void WList::copy (const WList& other)
267{
268  int items;
269  int i;
270
271  clear ();
272
273  items = other.get_item_count ();
274  for (i = 0; i < items; i++)
275    {
276      append (other.get_item (i));
277    }
278}
279
280void WList::set (const CmtSystem::cmt_string_vector& v, int first)
281{
282  int i;
283
284  clear ();
285
286  for (i = first; i < v.size (); i++)
287    {
288      append (v[i]);
289    }
290}
291
292void WList::set (const cmt_string& file_name)
293{
294  clear ();
295
296  cmt_string text;
297  cmt_string line;
298  int pos;
299  int max_pos;
300
301  text.read (file_name);
302
303  pos = 0;
304  max_pos = text.size ();
305
306  for (pos = 0; pos < max_pos;)
307    {
308      int cr = text.find (pos, "\r\n");
309      int nl = text.find (pos, '\n'); 
310      int first = nl;
311      int length = 1;
312
313      if (cr != cmt_string::npos)
314        {
315          if (nl == cmt_string::npos)
316            {
317              first = cr;
318              length = 2;
319            }
320          else
321            {
322              first = (nl < cr) ? nl : cr;
323              length = (nl < cr) ? 1 : 2;
324            }
325        }
326
327      if (first == cmt_string::npos)
328        {
329          text.substr (pos, line);
330          pos = max_pos;
331        }
332      else if (first > pos)
333        {
334          text.substr (pos, first - pos, line);
335          pos = first + length;
336        }
337      else
338        {
339          line = "";
340          pos += length;
341        }
342
343      append (line);
344    }
345}
346
347void WList::get (CmtSystem::cmt_string_vector& v) const
348{
349  int count = get_item_count ();
350  int i;
351
352  v.clear ();
353
354  for (i = 0; i < count; i++)
355    {
356      v.push_back (get_item (i));
357    }
358}
359
360void WList::get (cmt_string& s) const
361{
362  int count = get_item_count ();
363  int i;
364
365  s = "";
366
367  for (i = 0; i < count; i++)
368    {
369      if (i > 0) s += '\n';
370      s += get_item (i);
371    }
372}
373
374void WList::set_extent ()
375{
376  int width = get_extent ();
377  ListBox_SetHorizontalExtent (_list, width);
378}
379
380int WList::get_width () const
381{
382  if (_list == 0) return (0);
383
384  int count = get_item_count ();
385  int w = 0;
386
387  int i;
388  for (i = 0; i < count; i++)
389  {
390          int length = get_item_length (i);
391          if (length > w) w = length;
392  }
393
394  return (w);
395}
396
397int WList::get_extent () const
398{
399  if (_width == 0) return (0);
400
401  cmt_string text (_width + 1);
402
403  HDC context = GetDC (_list);
404  int saved_context = SaveDC (context);
405  HFONT font = GetWindowFont (_list);
406  if (font != NULL) SelectFont (context, font);
407
408  int w = 0;
409
410  int count = get_item_count ();
411
412  int i;
413  for (i = 0; i < count; i++)
414  {
415          SIZE size;
416
417          get_item (i, text);
418          GetTextExtentPoint32 (context, text.c_str (), text.size (), &size);
419          if (size.cx > w) w = size.cx;
420  }
421
422  RestoreDC (context, saved_context);
423  ReleaseDC (_list, context);
424
425  w += GetSystemMetrics (SM_CXVSCROLL);
426
427  return (w);
428}
429//---------------------------------------------------------------------
430
431//---------------------------------------------------------------------
432mysb::mysb (WList& list) : _list (list)
433{
434    //list_clear (list);
435  _buf = 0;
436#ifdef OLD_HEADERS
437  _old = cout.rdbuf ();
438  cout = this;
439#else
440  _old = cout.rdbuf (this);
441#endif
442}
443
444mysb::~mysb ()
445{
446  if (_buf != 0)
447  {
448    //delete _buf;
449        _buf = 0;
450  }
451
452#ifdef OLD_HEADERS
453  cout = _old;
454#else
455  cout.rdbuf (_old);
456#endif
457}
458
459int_type mysb::overflow (int_type c)
460{
461  if (_buf == 0)
462  {
463    _buf = new cmt_string;
464  }
465
466  cmt_string& buf = *_buf;
467
468  if (c == '\n')
469    {
470      _list.append (buf);
471      buf = "";
472    }
473  else
474    {
475      buf += c;
476    }
477  return (c);
478}
479       
480int mysb::underflow ()
481{
482  return (EOF);
483}
484//---------------------------------------------------------------------
485
486//---------------------------------------------------------------------
487WDialog::WDialog ()
488{
489}
490
491void WDialog::action (UINT message_type,
492                      WPARAM parameter1,
493                      LPARAM parameter2)
494{
495}
496
497void WDialog::init_action ()
498{
499}
500
501void WDialog::menu_action (int menu_item,
502                           WPARAM parameter1,
503                           LPARAM parameter2)
504{
505  switch (menu_item) 
506    {
507      case IDOK:
508        break;
509      case IDCANCEL:
510        break;
511      default:
512        break;
513    }
514}
515
516int WDialog::run (HINSTANCE instance, HWND father, int id)
517{
518  return (DialogBoxParam (instance, 
519                          MAKEINTRESOURCE(id), 
520                          father,
521                          (DLGPROC) proc,
522                          (LPARAM) this));
523}
524
525LRESULT CALLBACK WDialog::proc (HWND dialog,
526                                UINT message_type,
527                                WPARAM parameter1,
528                                LPARAM parameter2)
529{
530  static cmt_vector <WDialog*> objects;
531  WDialog* object = NULL;
532  int menu_item;
533
534  switch (message_type) 
535    {
536      case WM_CREATE:
537        break;
538      case WM_SETFONT:
539        break;
540      case WM_INITDIALOG:
541        object = (WDialog*) parameter2;
542        if (object != NULL)
543          {
544            objects.push_back (object);
545
546            object->_dialog = dialog;
547            object->init_action ();
548          }
549        break;
550      case WM_COMMAND:
551        menu_item = LOWORD (parameter1);
552
553        if (objects.size () > 0)
554          {
555            object = objects.back ();
556          }
557
558        if (object != NULL)
559          {
560            object->menu_action (menu_item, parameter1, parameter2);
561          }
562
563        switch (menu_item) 
564          {
565            case IDOK:
566              EndDialog (dialog, IDOK);
567              objects.pop_back ();
568              break;
569            case IDCANCEL:
570              EndDialog (dialog, IDCANCEL);
571              objects.pop_back ();
572              break;
573            default:
574              break;
575          }
576        break;
577      default:
578        if (objects.size () > 0)
579          {
580            object = objects.back ();
581          }
582
583        if (object != NULL)
584          {
585            object->action (message_type, parameter1, parameter2);
586          }
587        return (FALSE);
588    }
589  return (TRUE);
590}
591//---------------------------------------------------------------------
592
593//---------------------------------------------------------------------
594TextEditor::TextEditor (cmt_string& ref) : _ref (ref)
595{
596}
597
598int TextEditor::run (HINSTANCE instance, HWND father)
599{
600  return (WDialog::run (instance, father, IDD_TEXTEDIT));
601}
602
603void TextEditor::action (UINT message_type,
604                         WPARAM parameter1,
605                         LPARAM parameter2)
606{
607}
608
609void TextEditor::init_action ()
610{
611  _edit = GetDlgItem (_dialog, IDTE_TEXT);
612  _edit.set (_ref);
613}
614
615void TextEditor::menu_action (int menu_item, WPARAM parameter1, LPARAM parameter2)
616{
617  switch (menu_item) 
618    {
619      case IDOK:
620        _ref = _edit.get ();
621        break;
622      case IDCANCEL:
623        break;
624      default:
625        break;
626    }
627}
628//---------------------------------------------------------------------
629
630//---------------------------------------------------------------------
631ListEditor::ListEditor (WList& list) : _ref_list (list)
632{
633}
634
635int ListEditor::run (HINSTANCE instance, HWND father)
636{
637  _instance = instance;
638  return (WDialog::run (instance, father, IDD_LISTEDIT));
639}
640
641void ListEditor::action (UINT message_type,
642                         WPARAM parameter1,
643                         LPARAM parameter2)
644{
645}
646
647void ListEditor::set_position (int index)
648{
649  _list.set_selected_index (index);
650  _selected_index = index;
651  _last_index = index;
652}
653
654void ListEditor::init_action ()
655{
656  _list.init (_dialog, IDLE_LIST);
657  _list.copy (_ref_list);
658  _ref_selected_index = _ref_list.get_selected_index ();
659  _list.set_selected_index (_ref_selected_index);
660  _selected_index = _ref_selected_index;
661  _last_index = _ref_selected_index;
662}
663
664void ListEditor::menu_action (int menu_item, WPARAM parameter1, LPARAM parameter2)
665{
666  int index = _list.get_selected_index ();
667
668  switch (menu_item) 
669    {
670      case IDOK:
671        _ref_list.copy (_list);
672        _ref_list.set_selected_index (_ref_selected_index);
673        break;
674      case IDCANCEL:
675        break;
676      case IDLE_ADD:
677      {
678        cmt_string s;
679        TextEditor editor (s);
680
681        if (editor.run (_instance, _dialog) == IDOK)
682          {
683            _list.insert (index, s);
684          }
685      }
686      break;
687      case IDLE_REMOVE:
688      {
689        if (index != -1)
690          {
691            _list.delete_item (index);
692          }
693      }
694      break;
695      case IDLE_LIST:
696     {
697        switch (HIWORD(parameter1))
698          {
699            case LBN_DBLCLK:
700            {
701              if (index == -1) index = _last_index;
702              cmt_string s = _list.get_item (index);
703              TextEditor editor (s);
704       
705              if (editor.run (_instance, _dialog) == IDOK)
706                {
707                  _list.modify (index, s);
708                }
709              _list.set_selected_index (index);
710              _selected_index = index;
711              _last_index = index;
712            }
713            break;
714            case LBN_SELCHANGE:
715              if (_selected_index == -1)
716                {
717                  _selected_index = index;
718                  _last_index = index;
719                }
720              else if (_selected_index == index)
721                {
722                  _list.cancel_selection ();
723                  _selected_index = -1;
724                }
725              else
726                {
727                  _selected_index = index;
728                  _last_index = index;
729                }
730              break;
731            case LBN_SELCANCEL:
732            {
733            }
734            break;
735          }
736      }
737      break;
738      default:
739        break;
740    }
741}
742//---------------------------------------------------------------------
743
744//---------------------------------------------------------------------
745CmtMutex::CmtMutex ()
746{
747  InitializeCriticalSection (&_section);
748}
749
750CmtMutex::~CmtMutex ()
751{
752  DeleteCriticalSection (&_section);
753}
754
755void CmtMutex::lock ()
756{
757  EnterCriticalSection (&_section);
758}
759
760void CmtMutex::unlock ()
761{
762  LeaveCriticalSection (&_section);
763}
764//---------------------------------------------------------------------
765
766//---------------------------------------------------------------------
767CmtThread::CmtThread ()
768{
769  _shutdown = FALSE;
770  _running = FALSE;
771}
772
773CmtThread::~CmtThread ()
774{
775}
776
777void CmtThread::run ()
778{
779}
780
781bool CmtThread::is_running ()
782{
783  bool result = false;
784
785  _mutex.lock ();
786  result = _running;
787  _mutex.unlock ();
788
789  return (result);
790}
791
792void CmtThread::set_running ()
793{
794  _mutex.lock ();
795  _running = TRUE;
796  _mutex.unlock ();
797}
798
799void CmtThread::reset_running ()
800{
801  _mutex.lock ();
802  _running = FALSE;
803  _mutex.unlock ();
804}
805
806bool CmtThread::start ()
807{
808  if (!is_running ())
809    {
810      _thread = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) run_it,
811                              this, 0, &_id);
812
813          return (TRUE);
814    }
815  else
816    {
817          return (FALSE);
818    }
819}
820
821void CmtThread::stop ()
822{
823  _mutex.lock ();
824  _shutdown = TRUE;
825  _mutex.unlock ();
826
827  WaitForSingleObject (_thread, INFINITE);
828}
829
830void CmtThread::run_it (CmtThread* thread)
831{
832  thread->set_running ();
833  thread->run ();
834  thread->reset_running ();
835}
836//---------------------------------------------------------------------
837
Note: See TracBrowser for help on using the repository browser.