source: CMT/v1r18p20041201/Visual/cmtw_utils.cxx @ 1

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

Import all tags

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