xref: /haiku/src/apps/resedit/MiscEditors.cpp (revision bc3955fea5b07e2e94a27fc05e4bb58fe6f0319b)
1 #include "InternalEditors.h"
2 #include "ResourceData.h"
3 #include <Messenger.h>
4 #include <Message.h>
5 #include <String.h>
6 #include <stdlib.h>
7 
8 StringEditor::StringEditor(const BRect &frame, ResourceData *data,
9 							BHandler *owner)
10   :	Editor(frame, data, owner)
11 {
12 	if (data->GetName())
13 		SetTitle(data->GetName());
14 
15 	fView = new StringEditView(Bounds());
16 	AddChild(fView);
17 
18 	if (data->IsAttribute())
19 		fView->EnableID(false);
20 	else
21 		fView->SetID(data->GetIDString());
22 	fView->SetName(data->GetName());
23 	fView->SetValue(data->GetData());
24 
25 	SetFlags(Flags() | B_NOT_V_RESIZABLE);
26 }
27 
28 
29 void
30 StringEditor::MessageReceived(BMessage *msg)
31 {
32 	if (msg->what == M_UPDATE_RESOURCE) {
33 		// We have to have an ID, so if the squirrely developer didn't give us
34 		// one, don't do anything
35 		if (fView->GetID()) {
36 			int32 newid = atol(fView->GetID());
37 			GetData()->SetID(newid);
38 		}
39 		GetData()->SetName(fView->GetName());
40 		GetData()->SetData(fView->GetValue(),strlen(fView->GetValue()));
41 
42 		BMessage updatemsg(M_UPDATE_RESOURCE);
43 		updatemsg.AddPointer("item",GetData());
44 		BMessenger msgr(GetOwner());
45 		msgr.SendMessage(&updatemsg);
46 		PostMessage(B_QUIT_REQUESTED);
47 
48 	} else {
49 		Editor::MessageReceived(msg);
50 	}
51 }
52 
53 
54 Editor::Editor(const BRect &frame, ResourceData *data, BHandler *owner)
55   :	BWindow(frame, "Untitled", B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS),
56   	fResData(data),
57   	fOwner(owner)
58 {
59 }
60 
61 
62 Editor::~Editor(void)
63 {
64 }
65 
66 
67 StringEditView::StringEditView(const BRect &frame)
68   :	BView(frame, "edit", B_FOLLOW_ALL, B_WILL_DRAW)
69 {
70 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
71 
72 	BRect r;
73 
74 	float labelwidth = be_plain_font->StringWidth("ID: ");
75 	float strwidth = be_plain_font->StringWidth("(attr) ");
76 
77 	font_height fh;
78 	be_plain_font->GetHeight(&fh);
79 	float strheight = fh.ascent + fh.descent + fh.leading + 5;
80 
81 	fIDBox = new BTextControl(BRect(10,10,10 + (strwidth + labelwidth) + 15,
82 									10 + strheight),
83 							  "id","ID: ","", NULL);
84 	fIDBox->SetDivider(labelwidth + 5);
85 	AddChild(fIDBox);
86 
87 	r = fIDBox->Frame();
88 	r.OffsetBy(r.Width() + 10, 0);
89 	r.right = Bounds().right - 10;
90 	fNameBox = new BTextControl(r,"name","Name: ","", NULL,
91 								B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP);
92 	fNameBox->SetDivider(be_plain_font->StringWidth("Name: ") + 5);
93 	AddChild(fNameBox);
94 
95 	r.OffsetBy(0,r.Height() + 10);
96 	r.left = 10;
97 	fValueBox = new BTextControl(r,"value","Value: ","", NULL,
98 								B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP);
99 	fValueBox->SetDivider(be_plain_font->StringWidth("Value: ") + 5);
100 	AddChild(fValueBox);
101 
102 	fOK = new BButton(BRect(10,10,11,11),"ok","Cancel",new BMessage(M_UPDATE_RESOURCE),
103 					  B_FOLLOW_RIGHT);
104 	fOK->ResizeToPreferred();
105 	fOK->SetLabel("OK");
106 	fOK->MakeDefault(true);
107 	AddChild(fOK);
108 
109 	fOK->MoveTo(r.right - fOK->Bounds().Width(), r.bottom + 10);
110 	r = fOK->Frame();
111 	r.OffsetBy(-r.Width() - 10, 0);
112 	fCancel = new BButton(r,"cancel","Cancel",new BMessage(B_QUIT_REQUESTED),
113 					  B_FOLLOW_RIGHT);
114 	AddChild(fCancel);
115 }
116 
117 
118 StringEditView::~StringEditView(void)
119 {
120 }
121 
122 
123 void
124 StringEditView::AttachedToWindow(void)
125 {
126 	if (Bounds().Height() < fCancel->Frame().bottom + 10)
127 		Window()->ResizeTo(Window()->Bounds().Width(), fCancel->Frame().bottom + 10);
128 
129 	Window()->SetSizeLimits(Window()->Bounds().Width(), 30000,
130 							Window()->Bounds().Height(), 30000);
131 }
132 
133 
134 float
135 StringEditView::GetPreferredWidth(void) const
136 {
137 	float idwidth = be_plain_font->StringWidth("ID: ") +
138 					be_plain_font->StringWidth("(attr) ") + 15;
139 	float namewidth = be_plain_font->StringWidth("Name: ") +
140 					be_plain_font->StringWidth(fNameBox->Text()) + 15;
141 	return idwidth + namewidth + 30;
142 }
143 
144 
145 float
146 StringEditView::GetPreferredHeight(void) const
147 {
148 	font_height fh;
149 	be_plain_font->GetHeight(&fh);
150 	float strheight = fh.ascent + fh.descent + fh.leading + 5;
151 	return fOK->Frame().Height() + (strheight * 2) + 40;
152 }
153 
154