Advanced Strategic Command
selectionwindow.h
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 #ifndef selectionwindowH
22 #define selectionwindowH
23 
24 #include <algorithm>
25 #include <vector>
26 
27 #include <paragui.h>
28 #include <pgwidget.h>
29 #include <pgwidgetlist.h>
30 #include <pgwindow.h>
31 #include <pgapplication.h>
32 #include <pglineedit.h>
33 
34 #include "loki/Functor.h"
35 
36 #include "../paradialog.h"
37 #include "../global.h"
38 #include "../ascstring.h"
39 
40 
41 class SelectionWidget : public PG_Widget {
42  protected:
43  SelectionWidget ( PG_Widget *parent, const PG_Rect &rect ) : PG_Widget( parent, rect ), selectionCallBack(NULL ) {};
44 
45  public:
46  typedef Loki::Functor<bool, LOKI_TYPELIST_1(const SelectionWidget*) > SelectionCallBack;
47 
48  private:
49  SelectionCallBack* selectionCallBack;
50  public:
51 
53  selectionCallBack = callBack;
54  }
55 
56  virtual ~SelectionWidget() {};
57 
58  virtual ASCString getName() const = 0;
59 
60 
61  sigc::signal<void,const SelectionWidget*> itemSelected;
62  sigc::signal<void,const SelectionWidget*> itemMarked;
63 
64  virtual int gap() { return 5; };
65  protected:
66 
67  bool eventMouseButtonUp (const SDL_MouseButtonEvent *button);
68  bool eventMouseButtonDown (const SDL_MouseButtonEvent *button);
69  void eventBlit ( SDL_Surface * surface, const PG_Rect & src, const PG_Rect & dst );
70 
71  virtual void display( SDL_Surface * surface, const PG_Rect & src, const PG_Rect & dst ) = 0;
72 };
73 
74 class ItemSelectorWidget;
75 
77  public:
78  virtual void restart() = 0;
79  virtual SelectionWidget* spawnNextItem( PG_Widget* parent, const PG_Point& pos ) = 0;
80 
81  virtual void itemSelected( const SelectionWidget* widget, bool mouse ) = 0;
82  virtual void itemMarked ( const SelectionWidget* widget ) {};
83  virtual SelectionWidget* getDefaultItem() { return NULL; };
84  virtual ~SelectionItemFactory() {};
85 };
86 
87 
88 
89 class ItemSelectorWidget : public PG_Widget {
90 
91  bool namesConstrained;
92 
93  typedef vector<SelectionWidget*> WidgetList;
94  WidgetList widgets;
95 
96  int rowCount;
97  PG_ScrollWidget* scrollWidget;
98 
99  PG_LineEdit* nameSearch;
100  const SelectionWidget* selectedItem;
101 
102  SelectionItemFactory* factory;
103  int columnCount;
104  int visibleRowCount;
105 
106  SelectionWidget::SelectionCallBack selectionCallBack;
107 
108  protected:
109  bool moveSelection( int amount ) ;
110  bool eventKeyDown(const SDL_KeyboardEvent* key);
111  void itemSelected( const SelectionWidget* w, bool mouse );
112  void markItem( const SelectionWidget* w );
113  bool isItemMarked( const SelectionWidget* w );
114  bool locateObject( const ASCString& name );
115  bool nameMatch( const SelectionWidget* selection, const ASCString& name );
116 
117  public:
118  // the ItemSelectorWindow will take ownership over the itemFactory
119  ItemSelectorWidget( PG_Widget *parent, const PG_Rect &r , SelectionItemFactory* itemFactory ) ;
120 
121  sigc::signal<void,const SelectionWidget*> sigItemSelected;
122  sigc::signal<void,ASCString> nameEntered;
123  sigc::signal<bool> sigQuitModal;
124 
125  void constrainNames( bool constrain );
126  int getItemNum() const { return widgets.size(); };
127 
128  void reLoad( bool show = false );
129  void resetNamesearch();
131 };
132 
133 
135  ItemSelectorWidget* itemSelector;
136  virtual void itemSelected( const SelectionWidget* );
137  protected:
138  bool eventKeyDown(const SDL_KeyboardEvent* key);
139 
140  public:
141  // the ItemSelectorWindow will take ownership over the itemFactory
142  ItemSelectorWindow( PG_Widget *parent, const PG_Rect &r , const ASCString& title, SelectionItemFactory* itemFactory ) ;
143 
144  int RunModal();
145  void reLoad();
146 
147 };
148 
149 
150 #endif
virtual SelectionWidget * getDefaultItem()
void reLoad(bool show=false)
virtual ~SelectionItemFactory()
bool eventMouseButtonUp(const SDL_MouseButtonEvent *button)
bool nameMatch(const SelectionWidget *selection, const ASCString &name)
void itemSelected(const SelectionWidget *w, bool mouse)
virtual void restart()=0
void eventBlit(SDL_Surface *surface, const PG_Rect &src, const PG_Rect &dst)
sigc::signal< void, const SelectionWidget * > sigItemSelected
Loki::Functor< bool, LOKI_TYPELIST_1(const SelectionWidget *) > SelectionCallBack
bool eventKeyDown(const SDL_KeyboardEvent *key)
sigc::signal< bool > sigQuitModal
virtual ASCString getName() const =0
bool isItemMarked(const SelectionWidget *w)
bool eventKeyDown(const SDL_KeyboardEvent *key)
virtual void itemMarked(const SelectionWidget *widget)
sigc::signal< void, ASCString > nameEntered
bool moveSelection(int amount)
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
ItemSelectorWidget(PG_Widget *parent, const PG_Rect &r, SelectionItemFactory *itemFactory)
void constrainNames(bool constrain)
virtual SelectionWidget * spawnNextItem(PG_Widget *parent, const PG_Point &pos)=0
bool locateObject(const ASCString &name)
virtual int gap()
int getItemNum() const
virtual void itemSelected(const SelectionWidget *widget, bool mouse)=0
Adapter class for using Paragui Dialogs in ASC. This class transfers the event control from ASC to Pa...
Definition: paradialog.h:127
SelectionHolder selection
Definition: edglobal.cpp:189
void setSelectionCallback(SelectionCallBack *callBack)
ItemSelectorWindow(PG_Widget *parent, const PG_Rect &r, const ASCString &title, SelectionItemFactory *itemFactory)
bool eventMouseButtonDown(const SDL_MouseButtonEvent *button)
sigc::signal< void, const SelectionWidget * > itemSelected
sigc::signal< void, const SelectionWidget * > itemMarked
void markItem(const SelectionWidget *w)
SelectionWidget(PG_Widget *parent, const PG_Rect &rect)
virtual ~SelectionWidget()
virtual void display(SDL_Surface *surface, const PG_Rect &src, const PG_Rect &dst)=0