source: golgotha/src/i4/menu/textitem.cc @ 608

Last change on this file since 608 was 80, checked in by Sam Hocevar, 15 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 5.8 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "menu/textitem.hh"
10#include "device/keys.hh"
11#include "window/win_evt.hh"
12#include "device/key_man.hh"
13#include "device/keys.hh"
14
15void i4_text_item_class::change_text(const i4_const_str &new_st)
16{
17  delete text;
18  text=new i4_str(new_st, new_st.length()+1);
19  request_redraw(i4_F);
20}
21
22i4_menu_item_class *i4_text_item_class::copy()
23{
24  return new i4_text_item_class(*text, hint, color, font,
25                                send.press ? send.press->copy() : 0,
26                                send.depress ? send.depress->copy() : 0,
27                                send.activate ? send.activate->copy() : 0,
28                                send.deactivate ? send.deactivate->copy() : 0);
29}
30
31
32i4_text_item_class::i4_text_item_class(const i4_const_str &_text,
33                                       i4_graphical_style_class *style,
34                                       i4_color_hint_class *_color,
35                                       i4_font_class *_font,
36                                       i4_event_reaction_class *press,
37                                       i4_event_reaction_class *depress,
38                                       i4_event_reaction_class *activate,
39                                       i4_event_reaction_class *deactivate,
40                                       w16 pad_left_right,
41                                       w16 pad_up_down)
42     
43  : i4_menu_item_class(0, style, 0,0, press,depress,activate,deactivate),
44    color(_color),
45    font(_font),
46    text(new i4_str(_text,_text.length()+1)),
47    pad_lr(pad_left_right)
48{
49  if (!color) color=style->color_hint;
50  if (!font)  font=style->font_hint->normal_font;
51
52  bg_color=style->color_hint->neutral();
53
54  resize(font->width(_text)+pad_left_right*2, font->height(_text)+pad_up_down*2);
55 
56  // show the keyboard short cut for commands
57  if (press && press->event && press->event->type()==i4_event::DO_COMMAND)
58  {
59    CAST_PTR(dev, i4_do_command_event_class, press->event);
60
61    i4_key key;
62    w16 mod;
63
64    if (i4_key_man.get_key_for_command(dev->command_id, key, mod))
65    {     
66      i4_str *key_name=i4_key_name(key, mod);
67      private_resize(width() + font->width(*key_name)+5, height());
68      delete key_name;
69    }
70  }
71
72
73}
74
75
76void i4_text_item_class::parent_draw(i4_draw_context_class &context)
77{
78  local_image->add_dirty(0,0,width()-1,height()-1,context);
79
80  i4_color fg,bg;
81
82  if (active)
83  {
84    fg=color->selected_text_foreground;
85    bg=color->selected_text_background;
86  }
87  else
88  {
89    fg=color->text_foreground;
90    bg=color->text_background;
91  }
92
93  if (!active)
94  {
95    if (bg_color==color->neutral())
96    {
97      for (i4_rect_list_class::area_iter c=context.clip.list.begin();c!=context.clip.list.end();++c)
98        hint->deco_neutral_fill(local_image, c->x1, c->y1, c->x2, c->y2, context);
99    }
100    else local_image->clear(bg_color, context);
101
102  }
103  else
104    local_image->clear(bg, context);
105
106  font->set_color(fg);
107
108  int dy=height()/2-font->largest_height()/2;
109  font->put_string(local_image,
110                   pad_lr,
111                   dy,
112                   *text,context); 
113 
114
115  // draw key name if there is one
116  if (send.press && send.press->event && send.press->event->type()==i4_event::DO_COMMAND)
117  {
118    CAST_PTR(dev, i4_do_command_event_class, send.press->event);
119
120    i4_key key;
121    w16 mod;
122
123    if (i4_key_man.get_key_for_command(dev->command_id, key, mod))
124    {     
125      i4_str *key_name=i4_key_name(key, mod);
126      font->put_string(local_image, width() - pad_lr -
127                       font->width(*key_name) + 1,
128                       dy, *key_name, context);
129    }
130  }
131
132
133}
134
135
136void i4_text_item_class::receive_event(i4_event *ev)
137{
138  if (ev->type()==i4_event::MOUSE_BUTTON_DOWN)
139  {
140    CAST_PTR(b,i4_mouse_button_down_event_class,ev);
141    if (b->but==i4_mouse_button_down_event_class::LEFT)
142    {       
143      do_press();
144      send_event(send.press, PRESSED);
145
146      do_depress();
147      send_event(send.depress, DEPRESSED);
148
149    } else i4_menu_item_class::receive_event(ev);
150  } else if (ev->type()==i4_event::KEY_PRESS)
151  {
152    CAST_PTR(k,i4_key_press_event_class,ev);
153    if (k->key==I4_ENTER)
154    {
155      do_press();
156      send_event(send.press, PRESSED);
157
158      do_depress();
159      send_event(send.depress, DEPRESSED);
160    } else if (k->key==I4_TAB)
161    {
162      i4_window_message_class tab(i4_window_message_class::REQUEST_NEXT_KEY_FOCUS,this);
163      i4_kernel.send_event(parent, &tab);     
164    }
165    else if (k->key==I4_LEFT)
166    {
167      i4_window_message_class l(i4_window_message_class::REQUEST_LEFT_KEY_FOCUS,this);
168      i4_kernel.send_event(parent, &l);
169    }
170    else if (k->key==I4_RIGHT)
171    {
172      i4_window_message_class r(i4_window_message_class::REQUEST_RIGHT_KEY_FOCUS,this);
173      i4_kernel.send_event(parent, &r);
174    }
175    else if (k->key==I4_UP)
176    {
177      i4_window_message_class u(i4_window_message_class::REQUEST_UP_KEY_FOCUS,this);
178      i4_kernel.send_event(parent, &u);
179    }
180    else if (k->key==I4_DOWN)
181    {
182      i4_window_message_class d(i4_window_message_class::REQUEST_DOWN_KEY_FOCUS,this);
183      i4_kernel.send_event(parent, &d);
184    }
185    else i4_menu_item_class::receive_event(ev);
186  } else
187    i4_menu_item_class::receive_event(ev);
188}
Note: See TracBrowser for help on using the repository browser.